hibernate.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Hibernation support for RISCV
  4. *
  5. * Copyright (C) 2023 StarFive Technology Co., Ltd.
  6. *
  7. * Author: Jee Heng Sia <jeeheng.sia@starfivetech.com>
  8. */
  9. #include <asm/barrier.h>
  10. #include <asm/cacheflush.h>
  11. #include <asm/mmu_context.h>
  12. #include <asm/page.h>
  13. #include <asm/pgalloc.h>
  14. #include <asm/pgtable.h>
  15. #include <asm/sections.h>
  16. #include <asm/set_memory.h>
  17. #include <asm/smp.h>
  18. #include <asm/suspend.h>
  19. #include <linux/cpu.h>
  20. #include <linux/memblock.h>
  21. #include <linux/pm.h>
  22. #include <linux/sched.h>
  23. #include <linux/suspend.h>
  24. #include <linux/utsname.h>
  25. /* The logical cpu number we should resume on, initialised to a non-cpu number. */
  26. static int sleep_cpu = -EINVAL;
  27. /* Pointer to the temporary resume page table. */
  28. static pgd_t *resume_pg_dir;
  29. /* CPU context to be saved. */
  30. struct suspend_context *hibernate_cpu_context;
  31. EXPORT_SYMBOL_GPL(hibernate_cpu_context);
  32. unsigned long relocated_restore_code;
  33. EXPORT_SYMBOL_GPL(relocated_restore_code);
  34. /**
  35. * struct arch_hibernate_hdr_invariants - container to store kernel build version.
  36. * @uts_version: to save the build number and date so that we do not resume with
  37. * a different kernel.
  38. */
  39. struct arch_hibernate_hdr_invariants {
  40. char uts_version[__NEW_UTS_LEN + 1];
  41. };
  42. /**
  43. * struct arch_hibernate_hdr - helper parameters that help us to restore the image.
  44. * @invariants: container to store kernel build version.
  45. * @hartid: to make sure same boot_cpu executes the hibernate/restore code.
  46. * @saved_satp: original page table used by the hibernated image.
  47. * @restore_cpu_addr: the kernel's image address to restore the CPU context.
  48. */
  49. static struct arch_hibernate_hdr {
  50. struct arch_hibernate_hdr_invariants invariants;
  51. unsigned long hartid;
  52. unsigned long saved_satp;
  53. unsigned long restore_cpu_addr;
  54. } resume_hdr;
  55. static void arch_hdr_invariants(struct arch_hibernate_hdr_invariants *i)
  56. {
  57. memset(i, 0, sizeof(*i));
  58. memcpy(i->uts_version, init_utsname()->version, sizeof(i->uts_version));
  59. }
  60. /*
  61. * Check if the given pfn is in the 'nosave' section.
  62. */
  63. int pfn_is_nosave(unsigned long pfn)
  64. {
  65. unsigned long nosave_begin_pfn = sym_to_pfn(&__nosave_begin);
  66. unsigned long nosave_end_pfn = sym_to_pfn(&__nosave_end - 1);
  67. return ((pfn >= nosave_begin_pfn) && (pfn <= nosave_end_pfn));
  68. }
  69. void notrace save_processor_state(void)
  70. {
  71. }
  72. void notrace restore_processor_state(void)
  73. {
  74. }
  75. /*
  76. * Helper parameters need to be saved to the hibernation image header.
  77. */
  78. int arch_hibernation_header_save(void *addr, unsigned int max_size)
  79. {
  80. struct arch_hibernate_hdr *hdr = addr;
  81. if (max_size < sizeof(*hdr))
  82. return -EOVERFLOW;
  83. arch_hdr_invariants(&hdr->invariants);
  84. hdr->hartid = cpuid_to_hartid_map(sleep_cpu);
  85. hdr->saved_satp = csr_read(CSR_SATP);
  86. hdr->restore_cpu_addr = (unsigned long)__hibernate_cpu_resume;
  87. return 0;
  88. }
  89. EXPORT_SYMBOL_GPL(arch_hibernation_header_save);
  90. /*
  91. * Retrieve the helper parameters from the hibernation image header.
  92. */
  93. int arch_hibernation_header_restore(void *addr)
  94. {
  95. struct arch_hibernate_hdr_invariants invariants;
  96. struct arch_hibernate_hdr *hdr = addr;
  97. int ret = 0;
  98. arch_hdr_invariants(&invariants);
  99. if (memcmp(&hdr->invariants, &invariants, sizeof(invariants))) {
  100. pr_crit("Hibernate image not generated by this kernel!\n");
  101. return -EINVAL;
  102. }
  103. sleep_cpu = riscv_hartid_to_cpuid(hdr->hartid);
  104. if (sleep_cpu < 0) {
  105. pr_crit("Hibernated on a CPU not known to this kernel!\n");
  106. sleep_cpu = -EINVAL;
  107. return -EINVAL;
  108. }
  109. #ifdef CONFIG_SMP
  110. ret = bringup_hibernate_cpu(sleep_cpu);
  111. if (ret) {
  112. sleep_cpu = -EINVAL;
  113. return ret;
  114. }
  115. #endif
  116. resume_hdr = *hdr;
  117. return ret;
  118. }
  119. EXPORT_SYMBOL_GPL(arch_hibernation_header_restore);
  120. int swsusp_arch_suspend(void)
  121. {
  122. int ret = 0;
  123. if (__cpu_suspend_enter(hibernate_cpu_context)) {
  124. sleep_cpu = smp_processor_id();
  125. suspend_save_csrs(hibernate_cpu_context);
  126. ret = swsusp_save();
  127. } else {
  128. suspend_restore_csrs(hibernate_cpu_context);
  129. flush_tlb_all();
  130. flush_icache_all();
  131. /*
  132. * Tell the hibernation core that we've just restored the memory.
  133. */
  134. in_suspend = 0;
  135. sleep_cpu = -EINVAL;
  136. }
  137. return ret;
  138. }
  139. static int temp_pgtable_map_pte(pmd_t *dst_pmdp, pmd_t *src_pmdp, unsigned long start,
  140. unsigned long end, pgprot_t prot)
  141. {
  142. pte_t *src_ptep;
  143. pte_t *dst_ptep;
  144. if (pmd_none(READ_ONCE(*dst_pmdp))) {
  145. dst_ptep = (pte_t *)get_safe_page(GFP_ATOMIC);
  146. if (!dst_ptep)
  147. return -ENOMEM;
  148. pmd_populate_kernel(NULL, dst_pmdp, dst_ptep);
  149. }
  150. dst_ptep = pte_offset_kernel(dst_pmdp, start);
  151. src_ptep = pte_offset_kernel(src_pmdp, start);
  152. do {
  153. pte_t pte = READ_ONCE(*src_ptep);
  154. if (pte_present(pte))
  155. set_pte(dst_ptep, __pte(pte_val(pte) | pgprot_val(prot)));
  156. } while (dst_ptep++, src_ptep++, start += PAGE_SIZE, start < end);
  157. return 0;
  158. }
  159. static int temp_pgtable_map_pmd(pud_t *dst_pudp, pud_t *src_pudp, unsigned long start,
  160. unsigned long end, pgprot_t prot)
  161. {
  162. unsigned long next;
  163. unsigned long ret;
  164. pmd_t *src_pmdp;
  165. pmd_t *dst_pmdp;
  166. if (pud_none(READ_ONCE(*dst_pudp))) {
  167. dst_pmdp = (pmd_t *)get_safe_page(GFP_ATOMIC);
  168. if (!dst_pmdp)
  169. return -ENOMEM;
  170. pud_populate(NULL, dst_pudp, dst_pmdp);
  171. }
  172. dst_pmdp = pmd_offset(dst_pudp, start);
  173. src_pmdp = pmd_offset(src_pudp, start);
  174. do {
  175. pmd_t pmd = READ_ONCE(*src_pmdp);
  176. next = pmd_addr_end(start, end);
  177. if (pmd_none(pmd))
  178. continue;
  179. if (pmd_leaf(pmd)) {
  180. set_pmd(dst_pmdp, __pmd(pmd_val(pmd) | pgprot_val(prot)));
  181. } else {
  182. ret = temp_pgtable_map_pte(dst_pmdp, src_pmdp, start, next, prot);
  183. if (ret)
  184. return -ENOMEM;
  185. }
  186. } while (dst_pmdp++, src_pmdp++, start = next, start != end);
  187. return 0;
  188. }
  189. static int temp_pgtable_map_pud(p4d_t *dst_p4dp, p4d_t *src_p4dp, unsigned long start,
  190. unsigned long end, pgprot_t prot)
  191. {
  192. unsigned long next;
  193. unsigned long ret;
  194. pud_t *dst_pudp;
  195. pud_t *src_pudp;
  196. if (p4d_none(READ_ONCE(*dst_p4dp))) {
  197. dst_pudp = (pud_t *)get_safe_page(GFP_ATOMIC);
  198. if (!dst_pudp)
  199. return -ENOMEM;
  200. p4d_populate(NULL, dst_p4dp, dst_pudp);
  201. }
  202. dst_pudp = pud_offset(dst_p4dp, start);
  203. src_pudp = pud_offset(src_p4dp, start);
  204. do {
  205. pud_t pud = READ_ONCE(*src_pudp);
  206. next = pud_addr_end(start, end);
  207. if (pud_none(pud))
  208. continue;
  209. if (pud_leaf(pud)) {
  210. set_pud(dst_pudp, __pud(pud_val(pud) | pgprot_val(prot)));
  211. } else {
  212. ret = temp_pgtable_map_pmd(dst_pudp, src_pudp, start, next, prot);
  213. if (ret)
  214. return -ENOMEM;
  215. }
  216. } while (dst_pudp++, src_pudp++, start = next, start != end);
  217. return 0;
  218. }
  219. static int temp_pgtable_map_p4d(pgd_t *dst_pgdp, pgd_t *src_pgdp, unsigned long start,
  220. unsigned long end, pgprot_t prot)
  221. {
  222. unsigned long next;
  223. unsigned long ret;
  224. p4d_t *dst_p4dp;
  225. p4d_t *src_p4dp;
  226. if (pgd_none(READ_ONCE(*dst_pgdp))) {
  227. dst_p4dp = (p4d_t *)get_safe_page(GFP_ATOMIC);
  228. if (!dst_p4dp)
  229. return -ENOMEM;
  230. pgd_populate(NULL, dst_pgdp, dst_p4dp);
  231. }
  232. dst_p4dp = p4d_offset(dst_pgdp, start);
  233. src_p4dp = p4d_offset(src_pgdp, start);
  234. do {
  235. p4d_t p4d = READ_ONCE(*src_p4dp);
  236. next = p4d_addr_end(start, end);
  237. if (p4d_none(p4d))
  238. continue;
  239. if (p4d_leaf(p4d)) {
  240. set_p4d(dst_p4dp, __p4d(p4d_val(p4d) | pgprot_val(prot)));
  241. } else {
  242. ret = temp_pgtable_map_pud(dst_p4dp, src_p4dp, start, next, prot);
  243. if (ret)
  244. return -ENOMEM;
  245. }
  246. } while (dst_p4dp++, src_p4dp++, start = next, start != end);
  247. return 0;
  248. }
  249. static int temp_pgtable_mapping(pgd_t *pgdp, unsigned long start, unsigned long end, pgprot_t prot)
  250. {
  251. pgd_t *dst_pgdp = pgd_offset_pgd(pgdp, start);
  252. pgd_t *src_pgdp = pgd_offset_k(start);
  253. unsigned long next;
  254. unsigned long ret;
  255. do {
  256. pgd_t pgd = READ_ONCE(*src_pgdp);
  257. next = pgd_addr_end(start, end);
  258. if (pgd_none(pgd))
  259. continue;
  260. if (pgd_leaf(pgd)) {
  261. set_pgd(dst_pgdp, __pgd(pgd_val(pgd) | pgprot_val(prot)));
  262. } else {
  263. ret = temp_pgtable_map_p4d(dst_pgdp, src_pgdp, start, next, prot);
  264. if (ret)
  265. return -ENOMEM;
  266. }
  267. } while (dst_pgdp++, src_pgdp++, start = next, start != end);
  268. return 0;
  269. }
  270. static unsigned long relocate_restore_code(void)
  271. {
  272. void *page = (void *)get_safe_page(GFP_ATOMIC);
  273. if (!page)
  274. return -ENOMEM;
  275. copy_page(page, hibernate_core_restore_code);
  276. /* Make the page containing the relocated code executable. */
  277. set_memory_x((unsigned long)page, 1);
  278. return (unsigned long)page;
  279. }
  280. int swsusp_arch_resume(void)
  281. {
  282. unsigned long end = (unsigned long)pfn_to_virt(max_low_pfn);
  283. unsigned long start = PAGE_OFFSET;
  284. int ret;
  285. /*
  286. * Memory allocated by get_safe_page() will be dealt with by the hibernation core,
  287. * we don't need to free it here.
  288. */
  289. resume_pg_dir = (pgd_t *)get_safe_page(GFP_ATOMIC);
  290. if (!resume_pg_dir)
  291. return -ENOMEM;
  292. /*
  293. * Create a temporary page table and map the whole linear region as executable and
  294. * writable.
  295. */
  296. ret = temp_pgtable_mapping(resume_pg_dir, start, end, __pgprot(_PAGE_WRITE | _PAGE_EXEC));
  297. if (ret)
  298. return ret;
  299. /* Move the restore code to a new page so that it doesn't get overwritten by itself. */
  300. relocated_restore_code = relocate_restore_code();
  301. if (relocated_restore_code == -ENOMEM)
  302. return -ENOMEM;
  303. /*
  304. * Map the __hibernate_cpu_resume() address to the temporary page table so that the
  305. * restore code can jumps to it after finished restore the image. The next execution
  306. * code doesn't find itself in a different address space after switching over to the
  307. * original page table used by the hibernated image.
  308. * The __hibernate_cpu_resume() mapping is unnecessary for RV32 since the kernel and
  309. * linear addresses are identical, but different for RV64. To ensure consistency, we
  310. * map it for both RV32 and RV64 kernels.
  311. * Additionally, we should ensure that the page is writable before restoring the image.
  312. */
  313. start = (unsigned long)resume_hdr.restore_cpu_addr;
  314. end = start + PAGE_SIZE;
  315. ret = temp_pgtable_mapping(resume_pg_dir, start, end, __pgprot(_PAGE_WRITE));
  316. if (ret)
  317. return ret;
  318. hibernate_restore_image(resume_hdr.saved_satp, (PFN_DOWN(__pa(resume_pg_dir)) | satp_mode),
  319. resume_hdr.restore_cpu_addr);
  320. return 0;
  321. }
  322. #ifdef CONFIG_PM_SLEEP_SMP
  323. int hibernate_resume_nonboot_cpu_disable(void)
  324. {
  325. if (sleep_cpu < 0) {
  326. pr_err("Failing to resume from hibernate on an unknown CPU\n");
  327. return -ENODEV;
  328. }
  329. return freeze_secondary_cpus(sleep_cpu);
  330. }
  331. #endif
  332. static int __init riscv_hibernate_init(void)
  333. {
  334. hibernate_cpu_context = kzalloc(sizeof(*hibernate_cpu_context), GFP_KERNEL);
  335. if (WARN_ON(!hibernate_cpu_context))
  336. return -ENOMEM;
  337. return 0;
  338. }
  339. early_initcall(riscv_hibernate_init);