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