USB: serial: option: add support for Telit ME910 PID 0x1101
[linux/fpc-iii.git] / drivers / firmware / efi / memattr.c
blob8986757eafafa29dbf287b9e45b4de592cbc9570
1 /*
2 * Copyright (C) 2016 Linaro Ltd. <ard.biesheuvel@linaro.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
9 #define pr_fmt(fmt) "efi: memattr: " fmt
11 #include <linux/efi.h>
12 #include <linux/init.h>
13 #include <linux/io.h>
14 #include <linux/memblock.h>
16 #include <asm/early_ioremap.h>
18 static int __initdata tbl_size;
21 * Reserve the memory associated with the Memory Attributes configuration
22 * table, if it exists.
24 int __init efi_memattr_init(void)
26 efi_memory_attributes_table_t *tbl;
28 if (efi.mem_attr_table == EFI_INVALID_TABLE_ADDR)
29 return 0;
31 tbl = early_memremap(efi.mem_attr_table, sizeof(*tbl));
32 if (!tbl) {
33 pr_err("Failed to map EFI Memory Attributes table @ 0x%lx\n",
34 efi.mem_attr_table);
35 return -ENOMEM;
38 if (tbl->version > 1) {
39 pr_warn("Unexpected EFI Memory Attributes table version %d\n",
40 tbl->version);
41 goto unmap;
44 tbl_size = sizeof(*tbl) + tbl->num_entries * tbl->desc_size;
45 memblock_reserve(efi.mem_attr_table, tbl_size);
46 set_bit(EFI_MEM_ATTR, &efi.flags);
48 unmap:
49 early_memunmap(tbl, sizeof(*tbl));
50 return 0;
54 * Returns a copy @out of the UEFI memory descriptor @in if it is covered
55 * entirely by a UEFI memory map entry with matching attributes. The virtual
56 * address of @out is set according to the matching entry that was found.
58 static bool entry_is_valid(const efi_memory_desc_t *in, efi_memory_desc_t *out)
60 u64 in_paddr = in->phys_addr;
61 u64 in_size = in->num_pages << EFI_PAGE_SHIFT;
62 efi_memory_desc_t *md;
64 *out = *in;
66 if (in->type != EFI_RUNTIME_SERVICES_CODE &&
67 in->type != EFI_RUNTIME_SERVICES_DATA) {
68 pr_warn("Entry type should be RuntimeServiceCode/Data\n");
69 return false;
72 if (!(in->attribute & (EFI_MEMORY_RO | EFI_MEMORY_XP))) {
73 pr_warn("Entry attributes invalid: RO and XP bits both cleared\n");
74 return false;
77 if (PAGE_SIZE > EFI_PAGE_SIZE &&
78 (!PAGE_ALIGNED(in->phys_addr) ||
79 !PAGE_ALIGNED(in->num_pages << EFI_PAGE_SHIFT))) {
81 * Since arm64 may execute with page sizes of up to 64 KB, the
82 * UEFI spec mandates that RuntimeServices memory regions must
83 * be 64 KB aligned. We need to validate this here since we will
84 * not be able to tighten permissions on such regions without
85 * affecting adjacent regions.
87 pr_warn("Entry address region misaligned\n");
88 return false;
91 for_each_efi_memory_desc(md) {
92 u64 md_paddr = md->phys_addr;
93 u64 md_size = md->num_pages << EFI_PAGE_SHIFT;
95 if (!(md->attribute & EFI_MEMORY_RUNTIME))
96 continue;
97 if (md->virt_addr == 0) {
98 /* no virtual mapping has been installed by the stub */
99 break;
102 if (md_paddr > in_paddr || (in_paddr - md_paddr) >= md_size)
103 continue;
106 * This entry covers the start of @in, check whether
107 * it covers the end as well.
109 if (md_paddr + md_size < in_paddr + in_size) {
110 pr_warn("Entry covers multiple EFI memory map regions\n");
111 return false;
114 if (md->type != in->type) {
115 pr_warn("Entry type deviates from EFI memory map region type\n");
116 return false;
119 out->virt_addr = in_paddr + (md->virt_addr - md_paddr);
121 return true;
124 pr_warn("No matching entry found in the EFI memory map\n");
125 return false;
129 * To be called after the EFI page tables have been populated. If a memory
130 * attributes table is available, its contents will be used to update the
131 * mappings with tightened permissions as described by the table.
132 * This requires the UEFI memory map to have already been populated with
133 * virtual addresses.
135 int __init efi_memattr_apply_permissions(struct mm_struct *mm,
136 efi_memattr_perm_setter fn)
138 efi_memory_attributes_table_t *tbl;
139 int i, ret;
141 if (tbl_size <= sizeof(*tbl))
142 return 0;
145 * We need the EFI memory map to be setup so we can use it to
146 * lookup the virtual addresses of all entries in the of EFI
147 * Memory Attributes table. If it isn't available, this
148 * function should not be called.
150 if (WARN_ON(!efi_enabled(EFI_MEMMAP)))
151 return 0;
153 tbl = memremap(efi.mem_attr_table, tbl_size, MEMREMAP_WB);
154 if (!tbl) {
155 pr_err("Failed to map EFI Memory Attributes table @ 0x%lx\n",
156 efi.mem_attr_table);
157 return -ENOMEM;
160 if (efi_enabled(EFI_DBG))
161 pr_info("Processing EFI Memory Attributes table:\n");
163 for (i = ret = 0; ret == 0 && i < tbl->num_entries; i++) {
164 efi_memory_desc_t md;
165 unsigned long size;
166 bool valid;
167 char buf[64];
169 valid = entry_is_valid((void *)tbl->entry + i * tbl->desc_size,
170 &md);
171 size = md.num_pages << EFI_PAGE_SHIFT;
172 if (efi_enabled(EFI_DBG) || !valid)
173 pr_info("%s 0x%012llx-0x%012llx %s\n",
174 valid ? "" : "!", md.phys_addr,
175 md.phys_addr + size - 1,
176 efi_md_typeattr_format(buf, sizeof(buf), &md));
178 if (valid) {
179 ret = fn(mm, &md);
180 if (ret)
181 pr_err("Error updating mappings, skipping subsequent md's\n");
184 memunmap(tbl);
185 return ret;