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)  * Extensible Firmware Interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Based on Extensible Firmware Interface Specification version 2.4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Copyright (C) 2013, 2014 Linaro Ltd.
^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/efi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <asm/efi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * Only regions of type EFI_RUNTIME_SERVICES_CODE need to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * executable, everything else can be mapped with the XN bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * set. Also take the new (optional) RO/XP bits into account.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) static __init pteval_t create_mapping_protection(efi_memory_desc_t *md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	u64 attr = md->attribute;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	u32 type = md->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	if (type == EFI_MEMORY_MAPPED_IO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 		return PROT_DEVICE_nGnRE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	if (WARN_ONCE(!PAGE_ALIGNED(md->phys_addr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 		      "UEFI Runtime regions are not aligned to 64 KB -- buggy firmware?"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 		 * If the region is not aligned to the page size of the OS, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		 * can not use strict permissions, since that would also affect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		 * the mapping attributes of the adjacent regions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		return pgprot_val(PAGE_KERNEL_EXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	/* R-- */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	if ((attr & (EFI_MEMORY_XP | EFI_MEMORY_RO)) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	    (EFI_MEMORY_XP | EFI_MEMORY_RO))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		return pgprot_val(PAGE_KERNEL_RO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	/* R-X */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	if (attr & EFI_MEMORY_RO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		return pgprot_val(PAGE_KERNEL_ROX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	/* RW- */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	if (((attr & (EFI_MEMORY_RP | EFI_MEMORY_WP | EFI_MEMORY_XP)) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	     EFI_MEMORY_XP) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	    type != EFI_RUNTIME_SERVICES_CODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		return pgprot_val(PAGE_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	/* RWX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	return pgprot_val(PAGE_KERNEL_EXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) /* we will fill this structure from the stub, so don't put it in .bss */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) struct screen_info screen_info __section(".data");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) int __init efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	pteval_t prot_val = create_mapping_protection(md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	bool page_mappings_only = (md->type == EFI_RUNTIME_SERVICES_CODE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 				   md->type == EFI_RUNTIME_SERVICES_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	if (!PAGE_ALIGNED(md->phys_addr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	    !PAGE_ALIGNED(md->num_pages << EFI_PAGE_SHIFT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		 * If the end address of this region is not aligned to page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		 * size, the mapping is rounded up, and may end up sharing a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		 * page frame with the next UEFI memory region. If we create
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		 * a block entry now, we may need to split it again when mapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		 * the next region, and support for that is going to be removed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		 * from the MMU routines. So avoid block mappings altogether in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		 * that case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		page_mappings_only = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	create_pgd_mapping(mm, md->phys_addr, md->virt_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 			   md->num_pages << EFI_PAGE_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			   __pgprot(prot_val | PTE_NG), page_mappings_only);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) static int __init set_permissions(pte_t *ptep, unsigned long addr, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	efi_memory_desc_t *md = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	pte_t pte = READ_ONCE(*ptep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	if (md->attribute & EFI_MEMORY_RO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		pte = set_pte_bit(pte, __pgprot(PTE_RDONLY));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	if (md->attribute & EFI_MEMORY_XP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		pte = set_pte_bit(pte, __pgprot(PTE_PXN));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	set_pte(ptep, pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) int __init efi_set_mapping_permissions(struct mm_struct *mm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 				       efi_memory_desc_t *md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	BUG_ON(md->type != EFI_RUNTIME_SERVICES_CODE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	       md->type != EFI_RUNTIME_SERVICES_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	 * Calling apply_to_page_range() is only safe on regions that are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	 * guaranteed to be mapped down to pages. Since we are only called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	 * for regions that have been mapped using efi_create_mapping() above
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	 * (and this is checked by the generic Memory Attributes table parsing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	 * routines), there is no need to check that again here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	return apply_to_page_range(mm, md->virt_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 				   md->num_pages << EFI_PAGE_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 				   set_permissions, md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)  * UpdateCapsule() depends on the system being shutdown via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)  * ResetSystem().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) bool efi_poweroff_required(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	return efi_enabled(EFI_RUNTIME_SERVICES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) asmlinkage efi_status_t efi_handle_corrupted_x18(efi_status_t s, const char *f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	pr_err_ratelimited(FW_BUG "register x18 corrupted by EFI %s\n", f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	return s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }