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