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