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