xref: /OK3568_Linux_fs/u-boot/arch/x86/lib/acpi_s3.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (C) 2017, Bin Meng <bmeng.cn@gmail.com>
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <common.h>
8*4882a593Smuzhiyun #include <asm/acpi_s3.h>
9*4882a593Smuzhiyun #include <asm/acpi_table.h>
10*4882a593Smuzhiyun #include <asm/post.h>
11*4882a593Smuzhiyun #include <linux/linkage.h>
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun static void asmlinkage (*acpi_do_wakeup)(void *vector) = (void *)WAKEUP_BASE;
16*4882a593Smuzhiyun 
acpi_jump_to_wakeup(void * vector)17*4882a593Smuzhiyun static void acpi_jump_to_wakeup(void *vector)
18*4882a593Smuzhiyun {
19*4882a593Smuzhiyun 	/* Copy wakeup trampoline in place */
20*4882a593Smuzhiyun 	memcpy((void *)WAKEUP_BASE, __wakeup, __wakeup_size);
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun 	printf("Jumping to OS waking vector %p\n", vector);
23*4882a593Smuzhiyun 	acpi_do_wakeup(vector);
24*4882a593Smuzhiyun }
25*4882a593Smuzhiyun 
acpi_resume(struct acpi_fadt * fadt)26*4882a593Smuzhiyun void acpi_resume(struct acpi_fadt *fadt)
27*4882a593Smuzhiyun {
28*4882a593Smuzhiyun 	void *wake_vec;
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun 	/* Turn on ACPI mode for S3 */
31*4882a593Smuzhiyun 	enter_acpi_mode(fadt->pm1a_cnt_blk);
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 	wake_vec = acpi_find_wakeup_vector(fadt);
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun 	/*
36*4882a593Smuzhiyun 	 * Restore the memory content starting from address 0x1000 which is
37*4882a593Smuzhiyun 	 * used for the real mode interrupt handler stubs.
38*4882a593Smuzhiyun 	 */
39*4882a593Smuzhiyun 	memcpy((void *)0x1000, (const void *)gd->arch.backup_mem,
40*4882a593Smuzhiyun 	       S3_RESERVE_SIZE);
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	post_code(POST_OS_RESUME);
43*4882a593Smuzhiyun 	acpi_jump_to_wakeup(wake_vec);
44*4882a593Smuzhiyun }
45*4882a593Smuzhiyun 
acpi_s3_reserve(void)46*4882a593Smuzhiyun int acpi_s3_reserve(void)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun 	/* adjust stack pointer for ACPI S3 resume backup memory */
49*4882a593Smuzhiyun 	gd->start_addr_sp -= S3_RESERVE_SIZE;
50*4882a593Smuzhiyun 	gd->arch.backup_mem = gd->start_addr_sp;
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	gd->start_addr_sp &= ~0xf;
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	/*
55*4882a593Smuzhiyun 	 * U-Boot sets up the real mode interrupt handler stubs starting from
56*4882a593Smuzhiyun 	 * address 0x1000. In most cases, the first 640K (0x00000 - 0x9ffff)
57*4882a593Smuzhiyun 	 * system memory is reported as system RAM in E820 table to the OS.
58*4882a593Smuzhiyun 	 * (see install_e820_map() implementation for each platform). So OS
59*4882a593Smuzhiyun 	 * can use these memories whatever it wants.
60*4882a593Smuzhiyun 	 *
61*4882a593Smuzhiyun 	 * If U-Boot is in an S3 resume path, care must be taken not to corrupt
62*4882a593Smuzhiyun 	 * these memorie otherwise OS data gets lost. Testing shows that, on
63*4882a593Smuzhiyun 	 * Microsoft Windows 10 on Intel Baytrail its wake up vector happens to
64*4882a593Smuzhiyun 	 * be installed at the same address 0x1000. While on Linux its wake up
65*4882a593Smuzhiyun 	 * vector does not overlap this memory range, but after resume kernel
66*4882a593Smuzhiyun 	 * checks low memory range per config option CONFIG_X86_RESERVE_LOW
67*4882a593Smuzhiyun 	 * which is 64K by default to see whether a memory corruption occurs
68*4882a593Smuzhiyun 	 * during the suspend/resume (it's harmless, but warnings are shown
69*4882a593Smuzhiyun 	 * in the kernel dmesg logs).
70*4882a593Smuzhiyun 	 *
71*4882a593Smuzhiyun 	 * We cannot simply mark the these memory as reserved in E820 table
72*4882a593Smuzhiyun 	 * because such configuration makes GRUB complain: unable to allocate
73*4882a593Smuzhiyun 	 * real mode page. Hence we choose to back up these memories to the
74*4882a593Smuzhiyun 	 * place where we reserved on our stack for our S3 resume work.
75*4882a593Smuzhiyun 	 * Before jumping to OS wake up vector, we need restore the original
76*4882a593Smuzhiyun 	 * content there (see acpi_resume() above).
77*4882a593Smuzhiyun 	 */
78*4882a593Smuzhiyun 	if (gd->arch.prev_sleep_state == ACPI_S3)
79*4882a593Smuzhiyun 		memcpy((void *)gd->arch.backup_mem, (const void *)0x1000,
80*4882a593Smuzhiyun 		       S3_RESERVE_SIZE);
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	return 0;
83*4882a593Smuzhiyun }
84