arm64: kernel: Add support for hibernate/suspend-to-disk
[firefly-linux-kernel-4.4.55.git] / arch / arm64 / kernel / hibernate.c
1 /*:
2  * Hibernate support specific for ARM64
3  *
4  * Derived from work on ARM hibernation support by:
5  *
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/
12  *
13  * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
14  *
15  * License terms: GNU General Public License (GPL) version 2
16  */
17 #define pr_fmt(x) "hibernate: " x
18 #include <linux/kvm_host.h>
19 #include <linux/mm.h>
20 #include <linux/pm.h>
21 #include <linux/sched.h>
22 #include <linux/suspend.h>
23 #include <linux/utsname.h>
24 #include <linux/version.h>
25
26 #include <asm/barrier.h>
27 #include <asm/cacheflush.h>
28 #include <asm/irqflags.h>
29 #include <asm/memory.h>
30 #include <asm/mmu_context.h>
31 #include <asm/pgalloc.h>
32 #include <asm/pgtable.h>
33 #include <asm/pgtable-hwdef.h>
34 #include <asm/sections.h>
35 #include <asm/suspend.h>
36 #include <asm/virt.h>
37
38 /*
39  * Hibernate core relies on this value being 0 on resume, and marks it
40  * __nosavedata assuming it will keep the resume kernel's '0' value. This
41  * doesn't happen with either KASLR.
42  *
43  * defined as "__visible int in_suspend __nosavedata" in
44  * kernel/power/hibernate.c
45  */
46 extern int in_suspend;
47
48 /* Find a symbols alias in the linear map */
49 #define LMADDR(x)       phys_to_virt(virt_to_phys(x))
50
51 /* Do we need to reset el2? */
52 #define el2_reset_needed() (is_hyp_mode_available() && !is_kernel_in_hyp_mode())
53
54 /*
55  * Start/end of the hibernate exit code, this must be copied to a 'safe'
56  * location in memory, and executed from there.
57  */
58 extern char __hibernate_exit_text_start[], __hibernate_exit_text_end[];
59
60 /* temporary el2 vectors in the __hibernate_exit_text section. */
61 extern char hibernate_el2_vectors[];
62
63 /* hyp-stub vectors, used to restore el2 during resume from hibernate. */
64 extern char __hyp_stub_vectors[];
65
66 /*
67  * Values that may not change over hibernate/resume. We put the build number
68  * and date in here so that we guarantee not to resume with a different
69  * kernel.
70  */
71 struct arch_hibernate_hdr_invariants {
72         char            uts_version[__NEW_UTS_LEN + 1];
73 };
74
75 /* These values need to be know across a hibernate/restore. */
76 static struct arch_hibernate_hdr {
77         struct arch_hibernate_hdr_invariants invariants;
78
79         /* These are needed to find the relocated kernel if built with kaslr */
80         phys_addr_t     ttbr1_el1;
81         void            (*reenter_kernel)(void);
82
83         /*
84          * We need to know where the __hyp_stub_vectors are after restore to
85          * re-configure el2.
86          */
87         phys_addr_t     __hyp_stub_vectors;
88 } resume_hdr;
89
90 static inline void arch_hdr_invariants(struct arch_hibernate_hdr_invariants *i)
91 {
92         memset(i, 0, sizeof(*i));
93         memcpy(i->uts_version, init_utsname()->version, sizeof(i->uts_version));
94 }
95
96 int pfn_is_nosave(unsigned long pfn)
97 {
98         unsigned long nosave_begin_pfn = virt_to_pfn(&__nosave_begin);
99         unsigned long nosave_end_pfn = virt_to_pfn(&__nosave_end - 1);
100
101         return (pfn >= nosave_begin_pfn) && (pfn <= nosave_end_pfn);
102 }
103
104 void notrace save_processor_state(void)
105 {
106         WARN_ON(num_online_cpus() != 1);
107 }
108
109 void notrace restore_processor_state(void)
110 {
111 }
112
113 int arch_hibernation_header_save(void *addr, unsigned int max_size)
114 {
115         struct arch_hibernate_hdr *hdr = addr;
116
117         if (max_size < sizeof(*hdr))
118                 return -EOVERFLOW;
119
120         arch_hdr_invariants(&hdr->invariants);
121         hdr->ttbr1_el1          = virt_to_phys(swapper_pg_dir);
122         hdr->reenter_kernel     = _cpu_resume;
123
124         /* We can't use __hyp_get_vectors() because kvm may still be loaded */
125         if (el2_reset_needed())
126                 hdr->__hyp_stub_vectors = virt_to_phys(__hyp_stub_vectors);
127         else
128                 hdr->__hyp_stub_vectors = 0;
129
130         return 0;
131 }
132 EXPORT_SYMBOL(arch_hibernation_header_save);
133
134 int arch_hibernation_header_restore(void *addr)
135 {
136         struct arch_hibernate_hdr_invariants invariants;
137         struct arch_hibernate_hdr *hdr = addr;
138
139         arch_hdr_invariants(&invariants);
140         if (memcmp(&hdr->invariants, &invariants, sizeof(invariants))) {
141                 pr_crit("Hibernate image not generated by this kernel!\n");
142                 return -EINVAL;
143         }
144
145         resume_hdr = *hdr;
146
147         return 0;
148 }
149 EXPORT_SYMBOL(arch_hibernation_header_restore);
150
151 /*
152  * Copies length bytes, starting at src_start into an new page,
153  * perform cache maintentance, then maps it at the specified address low
154  * address as executable.
155  *
156  * This is used by hibernate to copy the code it needs to execute when
157  * overwriting the kernel text. This function generates a new set of page
158  * tables, which it loads into ttbr0.
159  *
160  * Length is provided as we probably only want 4K of data, even on a 64K
161  * page system.
162  */
163 static int create_safe_exec_page(void *src_start, size_t length,
164                                  unsigned long dst_addr,
165                                  phys_addr_t *phys_dst_addr,
166                                  void *(*allocator)(gfp_t mask),
167                                  gfp_t mask)
168 {
169         int rc = 0;
170         pgd_t *pgd;
171         pud_t *pud;
172         pmd_t *pmd;
173         pte_t *pte;
174         unsigned long dst = (unsigned long)allocator(mask);
175
176         if (!dst) {
177                 rc = -ENOMEM;
178                 goto out;
179         }
180
181         memcpy((void *)dst, src_start, length);
182         flush_icache_range(dst, dst + length);
183
184         pgd = pgd_offset_raw(allocator(mask), dst_addr);
185         if (pgd_none(*pgd)) {
186                 pud = allocator(mask);
187                 if (!pud) {
188                         rc = -ENOMEM;
189                         goto out;
190                 }
191                 pgd_populate(&init_mm, pgd, pud);
192         }
193
194         pud = pud_offset(pgd, dst_addr);
195         if (pud_none(*pud)) {
196                 pmd = allocator(mask);
197                 if (!pmd) {
198                         rc = -ENOMEM;
199                         goto out;
200                 }
201                 pud_populate(&init_mm, pud, pmd);
202         }
203
204         pmd = pmd_offset(pud, dst_addr);
205         if (pmd_none(*pmd)) {
206                 pte = allocator(mask);
207                 if (!pte) {
208                         rc = -ENOMEM;
209                         goto out;
210                 }
211                 pmd_populate_kernel(&init_mm, pmd, pte);
212         }
213
214         pte = pte_offset_kernel(pmd, dst_addr);
215         set_pte(pte, __pte(virt_to_phys((void *)dst) |
216                          pgprot_val(PAGE_KERNEL_EXEC)));
217
218         /* Load our new page tables */
219         asm volatile("msr       ttbr0_el1, %0;"
220                      "isb;"
221                      "tlbi      vmalle1is;"
222                      "dsb       ish;"
223                      "isb" : : "r"(virt_to_phys(pgd)));
224
225         *phys_dst_addr = virt_to_phys((void *)dst);
226
227 out:
228         return rc;
229 }
230
231
232 int swsusp_arch_suspend(void)
233 {
234         int ret = 0;
235         unsigned long flags;
236         struct sleep_stack_data state;
237
238         local_dbg_save(flags);
239
240         if (__cpu_suspend_enter(&state)) {
241                 ret = swsusp_save();
242         } else {
243                 /* Clean kernel to PoC for secondary core startup */
244                 __flush_dcache_area(LMADDR(KERNEL_START), KERNEL_END - KERNEL_START);
245
246                 /*
247                  * Tell the hibernation core that we've just restored
248                  * the memory
249                  */
250                 in_suspend = 0;
251
252                 __cpu_suspend_exit();
253         }
254
255         local_dbg_restore(flags);
256
257         return ret;
258 }
259
260 static int copy_pte(pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long start,
261                     unsigned long end)
262 {
263         pte_t *src_pte;
264         pte_t *dst_pte;
265         unsigned long addr = start;
266
267         dst_pte = (pte_t *)get_safe_page(GFP_ATOMIC);
268         if (!dst_pte)
269                 return -ENOMEM;
270         pmd_populate_kernel(&init_mm, dst_pmd, dst_pte);
271         dst_pte = pte_offset_kernel(dst_pmd, start);
272
273         src_pte = pte_offset_kernel(src_pmd, start);
274         do {
275                 if (!pte_none(*src_pte))
276                         /*
277                          * Resume will overwrite areas that may be marked
278                          * read only (code, rodata). Clear the RDONLY bit from
279                          * the temporary mappings we use during restore.
280                          */
281                         set_pte(dst_pte, __pte(pte_val(*src_pte) & ~PTE_RDONLY));
282         } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
283
284         return 0;
285 }
286
287 static int copy_pmd(pud_t *dst_pud, pud_t *src_pud, unsigned long start,
288                     unsigned long end)
289 {
290         pmd_t *src_pmd;
291         pmd_t *dst_pmd;
292         unsigned long next;
293         unsigned long addr = start;
294
295         if (pud_none(*dst_pud)) {
296                 dst_pmd = (pmd_t *)get_safe_page(GFP_ATOMIC);
297                 if (!dst_pmd)
298                         return -ENOMEM;
299                 pud_populate(&init_mm, dst_pud, dst_pmd);
300         }
301         dst_pmd = pmd_offset(dst_pud, start);
302
303         src_pmd = pmd_offset(src_pud, start);
304         do {
305                 next = pmd_addr_end(addr, end);
306                 if (pmd_none(*src_pmd))
307                         continue;
308                 if (pmd_table(*src_pmd)) {
309                         if (copy_pte(dst_pmd, src_pmd, addr, next))
310                                 return -ENOMEM;
311                 } else {
312                         set_pmd(dst_pmd,
313                                 __pmd(pmd_val(*src_pmd) & ~PMD_SECT_RDONLY));
314                 }
315         } while (dst_pmd++, src_pmd++, addr = next, addr != end);
316
317         return 0;
318 }
319
320 static int copy_pud(pgd_t *dst_pgd, pgd_t *src_pgd, unsigned long start,
321                     unsigned long end)
322 {
323         pud_t *dst_pud;
324         pud_t *src_pud;
325         unsigned long next;
326         unsigned long addr = start;
327
328         if (pgd_none(*dst_pgd)) {
329                 dst_pud = (pud_t *)get_safe_page(GFP_ATOMIC);
330                 if (!dst_pud)
331                         return -ENOMEM;
332                 pgd_populate(&init_mm, dst_pgd, dst_pud);
333         }
334         dst_pud = pud_offset(dst_pgd, start);
335
336         src_pud = pud_offset(src_pgd, start);
337         do {
338                 next = pud_addr_end(addr, end);
339                 if (pud_none(*src_pud))
340                         continue;
341                 if (pud_table(*(src_pud))) {
342                         if (copy_pmd(dst_pud, src_pud, addr, next))
343                                 return -ENOMEM;
344                 } else {
345                         set_pud(dst_pud,
346                                 __pud(pud_val(*src_pud) & ~PMD_SECT_RDONLY));
347                 }
348         } while (dst_pud++, src_pud++, addr = next, addr != end);
349
350         return 0;
351 }
352
353 static int copy_page_tables(pgd_t *dst_pgd, unsigned long start,
354                             unsigned long end)
355 {
356         unsigned long next;
357         unsigned long addr = start;
358         pgd_t *src_pgd = pgd_offset_k(start);
359
360         dst_pgd = pgd_offset_raw(dst_pgd, start);
361         do {
362                 next = pgd_addr_end(addr, end);
363                 if (pgd_none(*src_pgd))
364                         continue;
365                 if (copy_pud(dst_pgd, src_pgd, addr, next))
366                         return -ENOMEM;
367         } while (dst_pgd++, src_pgd++, addr = next, addr != end);
368
369         return 0;
370 }
371
372 /*
373  * Setup then Resume from the hibernate image using swsusp_arch_suspend_exit().
374  *
375  * Memory allocated by get_safe_page() will be dealt with by the hibernate code,
376  * we don't need to free it here.
377  */
378 int swsusp_arch_resume(void)
379 {
380         int rc = 0;
381         void *zero_page;
382         size_t exit_size;
383         pgd_t *tmp_pg_dir;
384         void *lm_restore_pblist;
385         phys_addr_t phys_hibernate_exit;
386         void __noreturn (*hibernate_exit)(phys_addr_t, phys_addr_t, void *,
387                                           void *, phys_addr_t, phys_addr_t);
388
389         /*
390          * Locate the exit code in the bottom-but-one page, so that *NULL
391          * still has disastrous affects.
392          */
393         hibernate_exit = (void *)PAGE_SIZE;
394         exit_size = __hibernate_exit_text_end - __hibernate_exit_text_start;
395         /*
396          * Copy swsusp_arch_suspend_exit() to a safe page. This will generate
397          * a new set of ttbr0 page tables and load them.
398          */
399         rc = create_safe_exec_page(__hibernate_exit_text_start, exit_size,
400                                    (unsigned long)hibernate_exit,
401                                    &phys_hibernate_exit,
402                                    (void *)get_safe_page, GFP_ATOMIC);
403         if (rc) {
404                 pr_err("Failed to create safe executable page for hibernate_exit code.");
405                 goto out;
406         }
407
408         /*
409          * The hibernate exit text contains a set of el2 vectors, that will
410          * be executed at el2 with the mmu off in order to reload hyp-stub.
411          */
412         __flush_dcache_area(hibernate_exit, exit_size);
413
414         /*
415          * Restoring the memory image will overwrite the ttbr1 page tables.
416          * Create a second copy of just the linear map, and use this when
417          * restoring.
418          */
419         tmp_pg_dir = (pgd_t *)get_safe_page(GFP_ATOMIC);
420         if (!tmp_pg_dir) {
421                 pr_err("Failed to allocate memory for temporary page tables.");
422                 rc = -ENOMEM;
423                 goto out;
424         }
425         rc = copy_page_tables(tmp_pg_dir, PAGE_OFFSET, 0);
426         if (rc)
427                 goto out;
428
429         /*
430          * Since we only copied the linear map, we need to find restore_pblist's
431          * linear map address.
432          */
433         lm_restore_pblist = LMADDR(restore_pblist);
434
435         /*
436          * KASLR will cause the el2 vectors to be in a different location in
437          * the resumed kernel. Load hibernate's temporary copy into el2.
438          *
439          * We can skip this step if we booted at EL1, or are running with VHE.
440          */
441         if (el2_reset_needed()) {
442                 phys_addr_t el2_vectors = phys_hibernate_exit;  /* base */
443                 el2_vectors += hibernate_el2_vectors -
444                                __hibernate_exit_text_start;     /* offset */
445
446                 __hyp_set_vectors(el2_vectors);
447         }
448
449         /*
450          * We need a zero page that is zero before & after resume in order to
451          * to break before make on the ttbr1 page tables.
452          */
453         zero_page = (void *)get_safe_page(GFP_ATOMIC);
454
455         hibernate_exit(virt_to_phys(tmp_pg_dir), resume_hdr.ttbr1_el1,
456                        resume_hdr.reenter_kernel, lm_restore_pblist,
457                        resume_hdr.__hyp_stub_vectors, virt_to_phys(zero_page));
458
459 out:
460         return rc;
461 }