|
14 | 14 | */
|
15 | 15 |
|
16 | 16 | #include <linux/acpi.h>
|
| 17 | +#include <linux/efi.h> |
17 | 18 | #include <linux/io.h>
|
| 19 | +#include <linux/memblock.h> |
18 | 20 | #include <linux/pci.h>
|
19 |
| -#include <linux/efi.h> |
20 | 21 |
|
21 | 22 | int acpi_noirq = 1; /* skip ACPI IRQ initialization */
|
22 | 23 | int acpi_disabled = 1;
|
@@ -217,7 +218,89 @@ void __init __acpi_unmap_table(void __iomem *map, unsigned long size)
|
217 | 218 |
|
218 | 219 | void __iomem *acpi_os_ioremap(acpi_physical_address phys, acpi_size size)
|
219 | 220 | {
|
220 |
| - return (void __iomem *)memremap(phys, size, MEMREMAP_WB); |
| 221 | + efi_memory_desc_t *md, *region = NULL; |
| 222 | + pgprot_t prot; |
| 223 | + |
| 224 | + if (WARN_ON_ONCE(!efi_enabled(EFI_MEMMAP))) |
| 225 | + return NULL; |
| 226 | + |
| 227 | + for_each_efi_memory_desc(md) { |
| 228 | + u64 end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT); |
| 229 | + |
| 230 | + if (phys < md->phys_addr || phys >= end) |
| 231 | + continue; |
| 232 | + |
| 233 | + if (phys + size > end) { |
| 234 | + pr_warn(FW_BUG "requested region covers multiple EFI memory regions\n"); |
| 235 | + return NULL; |
| 236 | + } |
| 237 | + region = md; |
| 238 | + break; |
| 239 | + } |
| 240 | + |
| 241 | + /* |
| 242 | + * It is fine for AML to remap regions that are not represented in the |
| 243 | + * EFI memory map at all, as it only describes normal memory, and MMIO |
| 244 | + * regions that require a virtual mapping to make them accessible to |
| 245 | + * the EFI runtime services. |
| 246 | + */ |
| 247 | + prot = PAGE_KERNEL_IO; |
| 248 | + if (region) { |
| 249 | + switch (region->type) { |
| 250 | + case EFI_LOADER_CODE: |
| 251 | + case EFI_LOADER_DATA: |
| 252 | + case EFI_BOOT_SERVICES_CODE: |
| 253 | + case EFI_BOOT_SERVICES_DATA: |
| 254 | + case EFI_CONVENTIONAL_MEMORY: |
| 255 | + case EFI_PERSISTENT_MEMORY: |
| 256 | + if (memblock_is_map_memory(phys) || |
| 257 | + !memblock_is_region_memory(phys, size)) { |
| 258 | + pr_warn(FW_BUG "requested region covers kernel memory\n"); |
| 259 | + return NULL; |
| 260 | + } |
| 261 | + |
| 262 | + /* |
| 263 | + * Mapping kernel memory is permitted if the region in |
| 264 | + * question is covered by a single memblock with the |
| 265 | + * NOMAP attribute set: this enables the use of ACPI |
| 266 | + * table overrides passed via initramfs. |
| 267 | + * This particular use case only requires read access. |
| 268 | + */ |
| 269 | + fallthrough; |
| 270 | + |
| 271 | + case EFI_RUNTIME_SERVICES_CODE: |
| 272 | + /* |
| 273 | + * This would be unusual, but not problematic per se, |
| 274 | + * as long as we take care not to create a writable |
| 275 | + * mapping for executable code. |
| 276 | + */ |
| 277 | + prot = PAGE_KERNEL_RO; |
| 278 | + break; |
| 279 | + |
| 280 | + case EFI_ACPI_RECLAIM_MEMORY: |
| 281 | + /* |
| 282 | + * ACPI reclaim memory is used to pass firmware tables |
| 283 | + * and other data that is intended for consumption by |
| 284 | + * the OS only, which may decide it wants to reclaim |
| 285 | + * that memory and use it for something else. We never |
| 286 | + * do that, but we usually add it to the linear map |
| 287 | + * anyway, in which case we should use the existing |
| 288 | + * mapping. |
| 289 | + */ |
| 290 | + if (memblock_is_map_memory(phys)) |
| 291 | + return (void __iomem *)__va(phys); |
| 292 | + fallthrough; |
| 293 | + |
| 294 | + default: |
| 295 | + if (region->attribute & EFI_MEMORY_WB) |
| 296 | + prot = PAGE_KERNEL; |
| 297 | + else if ((region->attribute & EFI_MEMORY_WC) || |
| 298 | + (region->attribute & EFI_MEMORY_WT)) |
| 299 | + prot = pgprot_writecombine(PAGE_KERNEL); |
| 300 | + } |
| 301 | + } |
| 302 | + |
| 303 | + return ioremap_prot(phys, size, pgprot_val(prot)); |
221 | 304 | }
|
222 | 305 |
|
223 | 306 | #ifdef CONFIG_PCI
|
|
0 commit comments