acpi_s3.c 2.6 KB

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