xref: /openbmc/u-boot/arch/x86/lib/acpi_s3.c (revision e0ed8332fa2fe684b4c8ba1caab991663730cbf0)
183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
23a34cae0SBin Meng /*
33a34cae0SBin Meng  * Copyright (C) 2017, Bin Meng <bmeng.cn@gmail.com>
43a34cae0SBin Meng  */
53a34cae0SBin Meng 
63a34cae0SBin Meng #include <common.h>
7*a0609a8dSBin Meng #include <asm/acpi.h>
83a34cae0SBin Meng #include <asm/acpi_s3.h>
90f4e2588SBin Meng #include <asm/acpi_table.h>
103a34cae0SBin Meng #include <asm/post.h>
11323a6d69SBin Meng #include <linux/linkage.h>
123a34cae0SBin Meng 
135ae5aa93SBin Meng DECLARE_GLOBAL_DATA_PTR;
145ae5aa93SBin Meng 
153a34cae0SBin Meng static void asmlinkage (*acpi_do_wakeup)(void *vector) = (void *)WAKEUP_BASE;
163a34cae0SBin Meng 
acpi_jump_to_wakeup(void * vector)173a34cae0SBin Meng static void acpi_jump_to_wakeup(void *vector)
183a34cae0SBin Meng {
193a34cae0SBin Meng 	/* Copy wakeup trampoline in place */
203a34cae0SBin Meng 	memcpy((void *)WAKEUP_BASE, __wakeup, __wakeup_size);
213a34cae0SBin Meng 
223a34cae0SBin Meng 	printf("Jumping to OS waking vector %p\n", vector);
233a34cae0SBin Meng 	acpi_do_wakeup(vector);
243a34cae0SBin Meng }
253a34cae0SBin Meng 
acpi_resume(struct acpi_fadt * fadt)260f4e2588SBin Meng void acpi_resume(struct acpi_fadt *fadt)
273a34cae0SBin Meng {
280f4e2588SBin Meng 	void *wake_vec;
290f4e2588SBin Meng 
3082a5648fSBin Meng 	/* Turn on ACPI mode for S3 */
3182a5648fSBin Meng 	enter_acpi_mode(fadt->pm1a_cnt_blk);
3282a5648fSBin Meng 
330f4e2588SBin Meng 	wake_vec = acpi_find_wakeup_vector(fadt);
340f4e2588SBin Meng 
355ae5aa93SBin Meng 	/*
365ae5aa93SBin Meng 	 * Restore the memory content starting from address 0x1000 which is
375ae5aa93SBin Meng 	 * used for the real mode interrupt handler stubs.
385ae5aa93SBin Meng 	 */
395ae5aa93SBin Meng 	memcpy((void *)0x1000, (const void *)gd->arch.backup_mem,
405ae5aa93SBin Meng 	       S3_RESERVE_SIZE);
415ae5aa93SBin Meng 
423a34cae0SBin Meng 	post_code(POST_OS_RESUME);
433a34cae0SBin Meng 	acpi_jump_to_wakeup(wake_vec);
443a34cae0SBin Meng }
455ae5aa93SBin Meng 
acpi_s3_reserve(void)465ae5aa93SBin Meng int acpi_s3_reserve(void)
475ae5aa93SBin Meng {
485ae5aa93SBin Meng 	/* adjust stack pointer for ACPI S3 resume backup memory */
495ae5aa93SBin Meng 	gd->start_addr_sp -= S3_RESERVE_SIZE;
505ae5aa93SBin Meng 	gd->arch.backup_mem = gd->start_addr_sp;
515ae5aa93SBin Meng 
525ae5aa93SBin Meng 	gd->start_addr_sp &= ~0xf;
535ae5aa93SBin Meng 
545ae5aa93SBin Meng 	/*
555ae5aa93SBin Meng 	 * U-Boot sets up the real mode interrupt handler stubs starting from
565ae5aa93SBin Meng 	 * address 0x1000. In most cases, the first 640K (0x00000 - 0x9ffff)
575ae5aa93SBin Meng 	 * system memory is reported as system RAM in E820 table to the OS.
585ae5aa93SBin Meng 	 * (see install_e820_map() implementation for each platform). So OS
595ae5aa93SBin Meng 	 * can use these memories whatever it wants.
605ae5aa93SBin Meng 	 *
615ae5aa93SBin Meng 	 * If U-Boot is in an S3 resume path, care must be taken not to corrupt
625ae5aa93SBin Meng 	 * these memorie otherwise OS data gets lost. Testing shows that, on
635ae5aa93SBin Meng 	 * Microsoft Windows 10 on Intel Baytrail its wake up vector happens to
645ae5aa93SBin Meng 	 * be installed at the same address 0x1000. While on Linux its wake up
655ae5aa93SBin Meng 	 * vector does not overlap this memory range, but after resume kernel
665ae5aa93SBin Meng 	 * checks low memory range per config option CONFIG_X86_RESERVE_LOW
675ae5aa93SBin Meng 	 * which is 64K by default to see whether a memory corruption occurs
685ae5aa93SBin Meng 	 * during the suspend/resume (it's harmless, but warnings are shown
695ae5aa93SBin Meng 	 * in the kernel dmesg logs).
705ae5aa93SBin Meng 	 *
715ae5aa93SBin Meng 	 * We cannot simply mark the these memory as reserved in E820 table
725ae5aa93SBin Meng 	 * because such configuration makes GRUB complain: unable to allocate
735ae5aa93SBin Meng 	 * real mode page. Hence we choose to back up these memories to the
745ae5aa93SBin Meng 	 * place where we reserved on our stack for our S3 resume work.
755ae5aa93SBin Meng 	 * Before jumping to OS wake up vector, we need restore the original
765ae5aa93SBin Meng 	 * content there (see acpi_resume() above).
775ae5aa93SBin Meng 	 */
785ae5aa93SBin Meng 	if (gd->arch.prev_sleep_state == ACPI_S3)
795ae5aa93SBin Meng 		memcpy((void *)gd->arch.backup_mem, (const void *)0x1000,
805ae5aa93SBin Meng 		       S3_RESERVE_SIZE);
815ae5aa93SBin Meng 
825ae5aa93SBin Meng 	return 0;
835ae5aa93SBin Meng }
84