acpi_s3.c 2.6 KB

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