Skip to content

Commit e8065df

Browse files
vlsunilpalmer-dabbelt
authored andcommitted
RISC-V: ACPI: Enhance acpi_os_ioremap with MMIO remapping
Enhance the acpi_os_ioremap() to support opregions in MMIO space. Also, have strict checks using EFI memory map to allow remapping the RAM similar to arm64. Signed-off-by: Sunil V L <sunilvl@ventanamicro.com> Reviewed-by: Andrew Jones <ajones@ventanamicro.com> Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com> Acked-by: Conor Dooley <conor.dooley@microchip.com> Link: https://lore.kernel.org/r/20231018124007.1306159-2-sunilvl@ventanamicro.com Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
1 parent 0bb80ec commit e8065df

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

arch/riscv/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ config RISCV
3939
select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
4040
select ARCH_HAS_UBSAN_SANITIZE_ALL
4141
select ARCH_HAS_VDSO_DATA
42+
select ARCH_KEEP_MEMBLOCK if ACPI
4243
select ARCH_OPTIONAL_KERNEL_RWX if ARCH_HAS_STRICT_KERNEL_RWX
4344
select ARCH_OPTIONAL_KERNEL_RWX_DEFAULT
4445
select ARCH_STACKWALK

arch/riscv/kernel/acpi.c

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
*/
1515

1616
#include <linux/acpi.h>
17+
#include <linux/efi.h>
1718
#include <linux/io.h>
19+
#include <linux/memblock.h>
1820
#include <linux/pci.h>
19-
#include <linux/efi.h>
2021

2122
int acpi_noirq = 1; /* skip ACPI IRQ initialization */
2223
int acpi_disabled = 1;
@@ -217,7 +218,89 @@ void __init __acpi_unmap_table(void __iomem *map, unsigned long size)
217218

218219
void __iomem *acpi_os_ioremap(acpi_physical_address phys, acpi_size size)
219220
{
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));
221304
}
222305

223306
#ifdef CONFIG_PCI

0 commit comments

Comments
 (0)