2 * Hibernate support specific for ARM64
4 * Derived from work on ARM hibernation support by:
6 * Ubuntu project, hibernation support for mach-dove
7 * Copyright (C) 2010 Nokia Corporation (Hiroshi Doyu)
8 * Copyright (C) 2010 Texas Instruments, Inc. (Teerth Reddy et al.)
9 * https://lkml.org/lkml/2010/6/18/4
10 * https://lists.linux-foundation.org/pipermail/linux-pm/2010-June/027422.html
11 * https://patchwork.kernel.org/patch/96442/
13 * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
15 * License terms: GNU General Public License (GPL) version 2
17 #define pr_fmt(x) "hibernate: " x
18 #include <linux/cpu.h>
19 #include <linux/kvm_host.h>
22 #include <linux/sched.h>
23 #include <linux/suspend.h>
24 #include <linux/utsname.h>
25 #include <linux/version.h>
27 #include <asm/barrier.h>
28 #include <asm/cacheflush.h>
29 #include <asm/cputype.h>
30 #include <asm/irqflags.h>
31 #include <asm/memory.h>
32 #include <asm/mmu_context.h>
33 #include <asm/pgalloc.h>
34 #include <asm/pgtable.h>
35 #include <asm/pgtable-hwdef.h>
36 #include <asm/sections.h>
38 #include <asm/smp_plat.h>
39 #include <asm/suspend.h>
40 #include <asm/sysreg.h>
44 * Hibernate core relies on this value being 0 on resume, and marks it
45 * __nosavedata assuming it will keep the resume kernel's '0' value. This
46 * doesn't happen with either KASLR.
48 * defined as "__visible int in_suspend __nosavedata" in
49 * kernel/power/hibernate.c
51 extern int in_suspend;
53 /* Do we need to reset el2? */
54 #define el2_reset_needed() (is_hyp_mode_available() && !is_kernel_in_hyp_mode())
56 /* temporary el2 vectors in the __hibernate_exit_text section. */
57 extern char hibernate_el2_vectors[];
59 /* hyp-stub vectors, used to restore el2 during resume from hibernate. */
60 extern char __hyp_stub_vectors[];
63 * The logical cpu number we should resume on, initialised to a non-cpu
66 static int sleep_cpu = -EINVAL;
69 * Values that may not change over hibernate/resume. We put the build number
70 * and date in here so that we guarantee not to resume with a different
73 struct arch_hibernate_hdr_invariants {
74 char uts_version[__NEW_UTS_LEN + 1];
77 /* These values need to be know across a hibernate/restore. */
78 static struct arch_hibernate_hdr {
79 struct arch_hibernate_hdr_invariants invariants;
81 /* These are needed to find the relocated kernel if built with kaslr */
82 phys_addr_t ttbr1_el1;
83 void (*reenter_kernel)(void);
86 * We need to know where the __hyp_stub_vectors are after restore to
89 phys_addr_t __hyp_stub_vectors;
94 static inline void arch_hdr_invariants(struct arch_hibernate_hdr_invariants *i)
96 memset(i, 0, sizeof(*i));
97 memcpy(i->uts_version, init_utsname()->version, sizeof(i->uts_version));
100 int pfn_is_nosave(unsigned long pfn)
102 unsigned long nosave_begin_pfn = sym_to_pfn(&__nosave_begin);
103 unsigned long nosave_end_pfn = sym_to_pfn(&__nosave_end - 1);
105 return (pfn >= nosave_begin_pfn) && (pfn <= nosave_end_pfn);
108 void notrace save_processor_state(void)
110 WARN_ON(num_online_cpus() != 1);
113 void notrace restore_processor_state(void)
117 int arch_hibernation_header_save(void *addr, unsigned int max_size)
119 struct arch_hibernate_hdr *hdr = addr;
121 if (max_size < sizeof(*hdr))
124 arch_hdr_invariants(&hdr->invariants);
125 hdr->ttbr1_el1 = __pa_symbol(swapper_pg_dir);
126 hdr->reenter_kernel = _cpu_resume;
128 /* We can't use __hyp_get_vectors() because kvm may still be loaded */
129 if (el2_reset_needed())
130 hdr->__hyp_stub_vectors = __pa_symbol(__hyp_stub_vectors);
132 hdr->__hyp_stub_vectors = 0;
134 /* Save the mpidr of the cpu we called cpu_suspend() on... */
136 pr_err("Failing to hibernate on an unknown CPU.\n");
139 hdr->sleep_cpu_mpidr = cpu_logical_map(sleep_cpu);
140 pr_info("Hibernating on CPU %d [mpidr:0x%llx]\n", sleep_cpu,
141 hdr->sleep_cpu_mpidr);
145 EXPORT_SYMBOL(arch_hibernation_header_save);
147 int arch_hibernation_header_restore(void *addr)
150 struct arch_hibernate_hdr_invariants invariants;
151 struct arch_hibernate_hdr *hdr = addr;
153 arch_hdr_invariants(&invariants);
154 if (memcmp(&hdr->invariants, &invariants, sizeof(invariants))) {
155 pr_crit("Hibernate image not generated by this kernel!\n");
159 sleep_cpu = get_logical_index(hdr->sleep_cpu_mpidr);
160 pr_info("Hibernated on CPU %d [mpidr:0x%llx]\n", sleep_cpu,
161 hdr->sleep_cpu_mpidr);
163 pr_crit("Hibernated on a CPU not known to this kernel!\n");
167 if (!cpu_online(sleep_cpu)) {
168 pr_info("Hibernated on a CPU that is offline! Bringing CPU up.\n");
169 ret = cpu_up(sleep_cpu);
171 pr_err("Failed to bring hibernate-CPU up!\n");
181 EXPORT_SYMBOL(arch_hibernation_header_restore);
184 * Copies length bytes, starting at src_start into an new page,
185 * perform cache maintentance, then maps it at the specified address low
186 * address as executable.
188 * This is used by hibernate to copy the code it needs to execute when
189 * overwriting the kernel text. This function generates a new set of page
190 * tables, which it loads into ttbr0.
192 * Length is provided as we probably only want 4K of data, even on a 64K
195 static int create_safe_exec_page(void *src_start, size_t length,
196 unsigned long dst_addr,
197 phys_addr_t *phys_dst_addr,
198 void *(*allocator)(gfp_t mask),
206 unsigned long dst = (unsigned long)allocator(mask);
213 memcpy((void *)dst, src_start, length);
214 flush_icache_range(dst, dst + length);
216 pgd = pgd_offset_raw(allocator(mask), dst_addr);
217 if (pgd_none(*pgd)) {
218 pud = allocator(mask);
223 pgd_populate(&init_mm, pgd, pud);
226 pud = pud_offset(pgd, dst_addr);
227 if (pud_none(*pud)) {
228 pmd = allocator(mask);
233 pud_populate(&init_mm, pud, pmd);
236 pmd = pmd_offset(pud, dst_addr);
237 if (pmd_none(*pmd)) {
238 pte = allocator(mask);
243 pmd_populate_kernel(&init_mm, pmd, pte);
246 pte = pte_offset_kernel(pmd, dst_addr);
247 set_pte(pte, __pte(virt_to_phys((void *)dst) |
248 pgprot_val(PAGE_KERNEL_EXEC)));
251 * Load our new page tables. A strict BBM approach requires that we
252 * ensure that TLBs are free of any entries that may overlap with the
253 * global mappings we are about to install.
255 * For a real hibernate/resume cycle TTBR0 currently points to a zero
256 * page, but TLBs may contain stale ASID-tagged entries (e.g. for EFI
257 * runtime services), while for a userspace-driven test_resume cycle it
258 * points to userspace page tables (and we must point it at a zero page
259 * ourselves). Elsewhere we only (un)install the idmap with preemption
260 * disabled, so T0SZ should be as required regardless.
262 cpu_set_reserved_ttbr0();
263 local_flush_tlb_all();
264 write_sysreg(virt_to_phys(pgd), ttbr0_el1);
267 *phys_dst_addr = virt_to_phys((void *)dst);
273 #define dcache_clean_range(start, end) __flush_dcache_area(start, (end - start))
275 int swsusp_arch_suspend(void)
279 struct sleep_stack_data state;
281 if (cpus_are_stuck_in_kernel()) {
282 pr_err("Can't hibernate: no mechanism to offline secondary CPUs.\n");
286 local_dbg_save(flags);
288 if (__cpu_suspend_enter(&state)) {
289 sleep_cpu = smp_processor_id();
292 /* Clean kernel core startup/idle code to PoC*/
293 dcache_clean_range(__mmuoff_data_start, __mmuoff_data_end);
294 dcache_clean_range(__idmap_text_start, __idmap_text_end);
296 /* Clean kvm setup code to PoC? */
297 if (el2_reset_needed())
298 dcache_clean_range(__hyp_idmap_text_start, __hyp_idmap_text_end);
301 * Tell the hibernation core that we've just restored
307 __cpu_suspend_exit();
310 local_dbg_restore(flags);
315 static void _copy_pte(pte_t *dst_pte, pte_t *src_pte, unsigned long addr)
317 pte_t pte = *src_pte;
319 if (pte_valid(pte)) {
321 * Resume will overwrite areas that may be marked
322 * read only (code, rodata). Clear the RDONLY bit from
323 * the temporary mappings we use during restore.
325 set_pte(dst_pte, pte_clear_rdonly(pte));
326 } else if (debug_pagealloc_enabled() && !pte_none(pte)) {
328 * debug_pagealloc will removed the PTE_VALID bit if
329 * the page isn't in use by the resume kernel. It may have
330 * been in use by the original kernel, in which case we need
331 * to put it back in our copy to do the restore.
333 * Before marking this entry valid, check the pfn should
336 BUG_ON(!pfn_valid(pte_pfn(pte)));
338 set_pte(dst_pte, pte_mkpresent(pte_clear_rdonly(pte)));
342 static int copy_pte(pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long start,
347 unsigned long addr = start;
349 dst_pte = (pte_t *)get_safe_page(GFP_ATOMIC);
352 pmd_populate_kernel(&init_mm, dst_pmd, dst_pte);
353 dst_pte = pte_offset_kernel(dst_pmd, start);
355 src_pte = pte_offset_kernel(src_pmd, start);
357 _copy_pte(dst_pte, src_pte, addr);
358 } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
363 static int copy_pmd(pud_t *dst_pud, pud_t *src_pud, unsigned long start,
369 unsigned long addr = start;
371 if (pud_none(*dst_pud)) {
372 dst_pmd = (pmd_t *)get_safe_page(GFP_ATOMIC);
375 pud_populate(&init_mm, dst_pud, dst_pmd);
377 dst_pmd = pmd_offset(dst_pud, start);
379 src_pmd = pmd_offset(src_pud, start);
381 next = pmd_addr_end(addr, end);
382 if (pmd_none(*src_pmd))
384 if (pmd_table(*src_pmd)) {
385 if (copy_pte(dst_pmd, src_pmd, addr, next))
389 __pmd(pmd_val(*src_pmd) & ~PMD_SECT_RDONLY));
391 } while (dst_pmd++, src_pmd++, addr = next, addr != end);
396 static int copy_pud(pgd_t *dst_pgd, pgd_t *src_pgd, unsigned long start,
402 unsigned long addr = start;
404 if (pgd_none(*dst_pgd)) {
405 dst_pud = (pud_t *)get_safe_page(GFP_ATOMIC);
408 pgd_populate(&init_mm, dst_pgd, dst_pud);
410 dst_pud = pud_offset(dst_pgd, start);
412 src_pud = pud_offset(src_pgd, start);
414 next = pud_addr_end(addr, end);
415 if (pud_none(*src_pud))
417 if (pud_table(*(src_pud))) {
418 if (copy_pmd(dst_pud, src_pud, addr, next))
422 __pud(pud_val(*src_pud) & ~PMD_SECT_RDONLY));
424 } while (dst_pud++, src_pud++, addr = next, addr != end);
429 static int copy_page_tables(pgd_t *dst_pgd, unsigned long start,
433 unsigned long addr = start;
434 pgd_t *src_pgd = pgd_offset_k(start);
436 dst_pgd = pgd_offset_raw(dst_pgd, start);
438 next = pgd_addr_end(addr, end);
439 if (pgd_none(*src_pgd))
441 if (copy_pud(dst_pgd, src_pgd, addr, next))
443 } while (dst_pgd++, src_pgd++, addr = next, addr != end);
449 * Setup then Resume from the hibernate image using swsusp_arch_suspend_exit().
451 * Memory allocated by get_safe_page() will be dealt with by the hibernate code,
452 * we don't need to free it here.
454 int swsusp_arch_resume(void)
460 phys_addr_t phys_hibernate_exit;
461 void __noreturn (*hibernate_exit)(phys_addr_t, phys_addr_t, void *,
462 void *, phys_addr_t, phys_addr_t);
465 * Restoring the memory image will overwrite the ttbr1 page tables.
466 * Create a second copy of just the linear map, and use this when
469 tmp_pg_dir = (pgd_t *)get_safe_page(GFP_ATOMIC);
471 pr_err("Failed to allocate memory for temporary page tables.\n");
475 rc = copy_page_tables(tmp_pg_dir, PAGE_OFFSET, 0);
480 * We need a zero page that is zero before & after resume in order to
481 * to break before make on the ttbr1 page tables.
483 zero_page = (void *)get_safe_page(GFP_ATOMIC);
485 pr_err("Failed to allocate zero page.\n");
491 * Locate the exit code in the bottom-but-one page, so that *NULL
492 * still has disastrous affects.
494 hibernate_exit = (void *)PAGE_SIZE;
495 exit_size = __hibernate_exit_text_end - __hibernate_exit_text_start;
497 * Copy swsusp_arch_suspend_exit() to a safe page. This will generate
498 * a new set of ttbr0 page tables and load them.
500 rc = create_safe_exec_page(__hibernate_exit_text_start, exit_size,
501 (unsigned long)hibernate_exit,
502 &phys_hibernate_exit,
503 (void *)get_safe_page, GFP_ATOMIC);
505 pr_err("Failed to create safe executable page for hibernate_exit code.\n");
510 * The hibernate exit text contains a set of el2 vectors, that will
511 * be executed at el2 with the mmu off in order to reload hyp-stub.
513 __flush_dcache_area(hibernate_exit, exit_size);
516 * KASLR will cause the el2 vectors to be in a different location in
517 * the resumed kernel. Load hibernate's temporary copy into el2.
519 * We can skip this step if we booted at EL1, or are running with VHE.
521 if (el2_reset_needed()) {
522 phys_addr_t el2_vectors = phys_hibernate_exit; /* base */
523 el2_vectors += hibernate_el2_vectors -
524 __hibernate_exit_text_start; /* offset */
526 __hyp_set_vectors(el2_vectors);
529 hibernate_exit(virt_to_phys(tmp_pg_dir), resume_hdr.ttbr1_el1,
530 resume_hdr.reenter_kernel, restore_pblist,
531 resume_hdr.__hyp_stub_vectors, virt_to_phys(zero_page));
537 int hibernate_resume_nonboot_cpu_disable(void)
540 pr_err("Failing to resume from hibernate on an unknown CPU.\n");
544 return freeze_secondary_cpus(sleep_cpu);