Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Hibernation support for x86-64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (c) 2007 Rafael J. Wysocki <rjw@sisk.pl>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (c) 2002 Pavel Machek <pavel@ucw.cz>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Copyright (c) 2001 Patrick Mochel <mochel@osdl.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/smp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/suspend.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/scatterlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/kdebug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/pgtable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <crypto/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <asm/e820/api.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <asm/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <asm/proto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <asm/page.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <asm/mtrr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <asm/sections.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <asm/suspend.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <asm/tlbflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static int set_up_temporary_text_mapping(pgd_t *pgd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	pmd_t *pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	pud_t *pud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	p4d_t *p4d = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	pgprot_t pgtable_prot = __pgprot(_KERNPG_TABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	pgprot_t pmd_text_prot = __pgprot(__PAGE_KERNEL_LARGE_EXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	/* Filter out unsupported __PAGE_KERNEL* bits: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	pgprot_val(pmd_text_prot) &= __default_kernel_pte_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	pgprot_val(pgtable_prot)  &= __default_kernel_pte_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	 * The new mapping only has to cover the page containing the image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	 * kernel's entry point (jump_address_phys), because the switch over to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	 * it is carried out by relocated code running from a page allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	 * specifically for this purpose and covered by the identity mapping, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	 * the temporary kernel text mapping is only needed for the final jump.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	 * Moreover, in that mapping the virtual address of the image kernel's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	 * entry point must be the same as its virtual address in the image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	 * kernel (restore_jump_address), so the image kernel's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	 * restore_registers() code doesn't find itself in a different area of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	 * the virtual address space after switching over to the original page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	 * tables used by the image kernel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	if (pgtable_l5_enabled()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		p4d = (p4d_t *)get_safe_page(GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		if (!p4d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	pud = (pud_t *)get_safe_page(GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	if (!pud)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	pmd = (pmd_t *)get_safe_page(GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	if (!pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	set_pmd(pmd + pmd_index(restore_jump_address),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		__pmd((jump_address_phys & PMD_MASK) | pgprot_val(pmd_text_prot)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	set_pud(pud + pud_index(restore_jump_address),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		__pud(__pa(pmd) | pgprot_val(pgtable_prot)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (p4d) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		p4d_t new_p4d = __p4d(__pa(pud) | pgprot_val(pgtable_prot));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		pgd_t new_pgd = __pgd(__pa(p4d) | pgprot_val(pgtable_prot));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		set_p4d(p4d + p4d_index(restore_jump_address), new_p4d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		set_pgd(pgd + pgd_index(restore_jump_address), new_pgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		/* No p4d for 4-level paging: point the pgd to the pud page table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		pgd_t new_pgd = __pgd(__pa(pud) | pgprot_val(pgtable_prot));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		set_pgd(pgd + pgd_index(restore_jump_address), new_pgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) static void *alloc_pgt_page(void *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	return (void *)get_safe_page(GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) static int set_up_temporary_mappings(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	struct x86_mapping_info info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		.alloc_pgt_page	= alloc_pgt_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		.page_flag	= __PAGE_KERNEL_LARGE_EXEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		.offset		= __PAGE_OFFSET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	unsigned long mstart, mend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	pgd_t *pgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	pgd = (pgd_t *)get_safe_page(GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (!pgd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	/* Prepare a temporary mapping for the kernel text */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	result = set_up_temporary_text_mapping(pgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	/* Set up the direct mapping from scratch */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	for (i = 0; i < nr_pfn_mapped; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		mstart = pfn_mapped[i].start << PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		mend   = pfn_mapped[i].end << PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		result = kernel_ident_mapping_init(&info, pgd, mstart, mend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 			return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	temp_pgt = __pa(pgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) asmlinkage int swsusp_arch_resume(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	/* We have got enough memory and from now on we cannot recover */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	error = set_up_temporary_mappings();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	error = relocate_restore_code();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	restore_image();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }