[PATCH 2/6] resource: record EFI memory type and attributes
Ahmad Fatoum
a.fatoum at barebox.org
Wed May 21 05:53:05 PDT 2025
While struct resource has some defines for attributes like
IORESOURCE_MEM_CACHEABLE, there are not enough bits to cover all
memory attributes and types specified by EFI.
As the upper 32-bit of the existing struct resource::flags member on
64-bit systems is unused, let's repurpose it to hold the memory type and
attributes. As we want to make use of this information even without EFI,
we add generic sounding constants that happen to be identical to the EFI
constant values.
Signed-off-by: Ahmad Fatoum <a.fatoum at barebox.org>
---
include/efi.h | 37 +-------------------
include/efi/memtype.h | 79 ++++++++++++++++++++++++++++++++++++++++++
include/linux/ioport.h | 40 ++++++++++++++++++++-
3 files changed, 119 insertions(+), 37 deletions(-)
create mode 100644 include/efi/memtype.h
diff --git a/include/efi.h b/include/efi.h
index 217e3d9f56ff..82d3380a31a9 100644
--- a/include/efi.h
+++ b/include/efi.h
@@ -15,6 +15,7 @@
#include <linux/string.h>
#include <linux/types.h>
#include <efi/types.h>
+#include <efi/memtype.h>
/* Bit mask for EFI status code with error */
#define EFI_ERROR_MASK (1UL << (BITS_PER_LONG-1))
@@ -86,42 +87,6 @@ struct efi_memory_desc {
u64 attrs;
};
-/* Memory types: */
-enum efi_memory_type {
- EFI_RESERVED_TYPE,
- EFI_LOADER_CODE,
- EFI_LOADER_DATA,
- EFI_BOOT_SERVICES_CODE,
- EFI_BOOT_SERVICES_DATA,
- EFI_RUNTIME_SERVICES_CODE,
- EFI_RUNTIME_SERVICES_DATA,
- EFI_CONVENTIONAL_MEMORY,
- EFI_UNUSABLE_MEMORY,
- EFI_ACPI_RECLAIM_MEMORY,
- EFI_ACPI_MEMORY_NVS,
- EFI_MEMORY_MAPPED_IO,
- EFI_MEMORY_MAPPED_IO_PORT_SPACE,
- EFI_PAL_CODE,
- EFI_MAX_MEMORY_TYPE
-};
-
-/* Attribute values: */
-#define EFI_MEMORY_UC ((u64)0x0000000000000001ULL) /* uncached */
-#define EFI_MEMORY_WC ((u64)0x0000000000000002ULL) /* write-coalescing */
-#define EFI_MEMORY_WT ((u64)0x0000000000000004ULL) /* write-through */
-#define EFI_MEMORY_WB ((u64)0x0000000000000008ULL) /* write-back */
-#define EFI_MEMORY_UCE ((u64)0x0000000000000010ULL) /* uncached, exported */
-#define EFI_MEMORY_WP ((u64)0x0000000000001000ULL) /* write-protect */
-#define EFI_MEMORY_RP ((u64)0x0000000000002000ULL) /* read-protect */
-#define EFI_MEMORY_XP ((u64)0x0000000000004000ULL) /* execute-protect */
-#define EFI_MEMORY_NV ((u64)0x0000000000008000ULL) /* non-volatile */
-#define EFI_MEMORY_RUNTIME ((u64)0x8000000000000000ULL) /* range requires runtime mapping */
-#define EFI_MEMORY_MORE_RELIABLE \
- ((u64)0x0000000000010000ULL) /* higher reliability */
-#define EFI_MEMORY_RO ((u64)0x0000000000020000ULL) /* read-only */
-#define EFI_MEMORY_SP ((u64)0x0000000000040000ULL) /* specific-purpose memory (SPM) */
-#define EFI_MEMORY_DESCRIPTOR_VERSION 1
-
#define EFI_PAGE_SHIFT 12
#define EFI_PAGE_SIZE (1ULL << EFI_PAGE_SHIFT)
#define EFI_PAGE_MASK (EFI_PAGE_SIZE - 1)
diff --git a/include/efi/memtype.h b/include/efi/memtype.h
new file mode 100644
index 000000000000..3f33b911a545
--- /dev/null
+++ b/include/efi/memtype.h
@@ -0,0 +1,79 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _EFI_MEMTYPE_H_
+#define _EFI_MEMTYPE_H_
+
+#include <linux/ioport.h>
+#include <efi/types.h>
+
+/* Memory types: */
+enum efi_memory_type {
+ EFI_RESERVED_TYPE = MEMTYPE_RESERVED,
+ EFI_LOADER_CODE = MEMTYPE_LOADER_CODE,
+ EFI_LOADER_DATA = MEMTYPE_LOADER_DATA,
+ EFI_BOOT_SERVICES_CODE = MEMTYPE_BOOT_SERVICES_CODE,
+ EFI_BOOT_SERVICES_DATA = MEMTYPE_BOOT_SERVICES_DATA,
+ EFI_RUNTIME_SERVICES_CODE = MEMTYPE_RUNTIME_SERVICES_CODE,
+ EFI_RUNTIME_SERVICES_DATA = MEMTYPE_RUNTIME_SERVICES_DATA,
+ EFI_CONVENTIONAL_MEMORY = MEMTYPE_CONVENTIONAL,
+ EFI_UNUSABLE_MEMORY = MEMTYPE_UNUSABLE,
+ EFI_ACPI_RECLAIM_MEMORY = MEMTYPE_ACPI_RECLAIM,
+ EFI_ACPI_MEMORY_NVS = MEMTYPE_ACPI_NVS,
+ EFI_MEMORY_MAPPED_IO = MEMTYPE_MMIO,
+ EFI_MEMORY_MAPPED_IO_PORT_SPACE = MEMTYPE_MMIO_PORT,
+ EFI_PAL_CODE = MEMTYPE_PAL_CODE,
+ EFI_PERSISTENT_MEMORY_TYPE = MEMTYPE_PERSISTENT,
+ EFI_UNACCEPTED_MEMORY_TYPE = MEMTYPE_UNACCEPTED,
+ EFI_MAX_MEMORY_TYPE = MEMTYPE_MAX,
+};
+
+/* Attribute values: */
+#define EFI_MEMORY_UC ((u64)MEMATTR_UC) /* uncached */
+#define EFI_MEMORY_WC ((u64)MEMATTR_WC) /* write-coalescing */
+#define EFI_MEMORY_WT ((u64)MEMATTR_WT) /* write-through */
+#define EFI_MEMORY_WB ((u64)MEMATTR_WB) /* write-back */
+#define EFI_MEMORY_UCE ((u64)MEMATTR_UCE) /* uncached, exported */
+#define EFI_MEMORY_WP ((u64)MEMATTR_WP) /* write-protect */
+#define EFI_MEMORY_RP ((u64)MEMATTR_RP) /* read-protect */
+#define EFI_MEMORY_XP ((u64)MEMATTR_XP) /* execute-protect */
+#define EFI_MEMORY_NV ((u64)MEMATTR_NV) /* non-volatile */
+#define EFI_MEMORY_RUNTIME ((u64)0x8000000000000000) /* range requires runtime mapping */
+
+#define EFI_MEMORY_MORE_RELIABLE ((u64)MEMATTR_MORE_RELIABLE) /* higher reliability */
+#define EFI_MEMORY_RO ((u64)MEMATTR_RO) /* read-only */
+#define EFI_MEMORY_SP ((u64)MEMATTR_SP) /* specific-purpose memory (SPM) */
+
+static inline enum efi_memory_type resource_type_to_efi_memory_type(unsigned type)
+{
+ return type;
+}
+
+static inline unsigned efi_memory_type_to_resource_type(enum efi_memory_type type)
+{
+ return type;
+}
+
+static inline enum efi_memory_type resource_get_efi_memory_type(const struct resource *res)
+{
+ if (res->flags & IORESOURCE_TYPE_VALID)
+ return resource_type_to_efi_memory_type(res->type);
+ return EFI_CONVENTIONAL_MEMORY;
+}
+
+static inline u64 resource_get_efi_memory_attrs(const struct resource *res)
+{
+ if (res->flags & IORESOURCE_TYPE_VALID)
+ return res->attrs | (res->runtime ? EFI_MEMORY_RUNTIME : 0);
+ return 0;
+}
+
+static inline void resource_set_efi_memory_type_attrs(struct resource *res,
+ enum efi_memory_type type,
+ u64 attrs)
+{
+ res->type = type;
+ res->runtime = attrs & EFI_MEMORY_RUNTIME;
+ res->attrs = attrs & ~EFI_MEMORY_RUNTIME;
+ res->flags |= IORESOURCE_TYPE_VALID;
+}
+
+#endif
diff --git a/include/linux/ioport.h b/include/linux/ioport.h
index c6328e9a7fc2..1e266185651f 100644
--- a/include/linux/ioport.h
+++ b/include/linux/ioport.h
@@ -22,7 +22,10 @@ struct resource {
resource_size_t start;
resource_size_t end;
const char *name;
- unsigned long flags;
+ unsigned int flags;
+ unsigned int type:4;
+ unsigned int attrs:27;
+ unsigned int runtime:1;
struct resource *parent;
struct list_head children;
struct list_head sibling;
@@ -53,6 +56,8 @@ struct resource {
#define IORESOURCE_WINDOW 0x00200000 /* forwarded by bridge */
#define IORESOURCE_MUXED 0x00400000 /* Resource is software muxed */
+#define IORESOURCE_TYPE_VALID 0x00800000 /* type & attrs are valid */
+
#define IORESOURCE_EXCLUSIVE 0x08000000 /* Userland may not map this resource */
#define IORESOURCE_DISABLED 0x10000000
#define IORESOURCE_UNSET 0x20000000
@@ -95,6 +100,39 @@ struct resource {
#define IORESOURCE_MEM_SHADOWABLE (1<<5) /* dup: IORESOURCE_SHADOWABLE */
#define IORESOURCE_MEM_EXPANSIONROM (1<<6)
+enum resource_memtype {
+ MEMTYPE_RESERVED,
+ MEMTYPE_LOADER_CODE,
+ MEMTYPE_LOADER_DATA,
+ MEMTYPE_BOOT_SERVICES_CODE,
+ MEMTYPE_BOOT_SERVICES_DATA,
+ MEMTYPE_RUNTIME_SERVICES_CODE,
+ MEMTYPE_RUNTIME_SERVICES_DATA,
+ MEMTYPE_CONVENTIONAL,
+ MEMTYPE_UNUSABLE,
+ MEMTYPE_ACPI_RECLAIM,
+ MEMTYPE_ACPI_NVS,
+ MEMTYPE_MMIO,
+ MEMTYPE_MMIO_PORT,
+ MEMTYPE_PAL_CODE,
+ MEMTYPE_PERSISTENT,
+ MEMTYPE_UNACCEPTED,
+ MEMTYPE_MAX,
+};
+
+#define MEMATTR_UC 0x00000001 /* uncached */
+#define MEMATTR_WC 0x00000002 /* write-coalescing */
+#define MEMATTR_WT 0x00000004 /* write-through */
+#define MEMATTR_WB 0x00000008 /* write-back */
+#define MEMATTR_UCE 0x00000010 /* uncached, exported */
+#define MEMATTR_WP 0x00001000 /* write-protect */
+#define MEMATTR_RP 0x00002000 /* read-protect */
+#define MEMATTR_XP 0x00004000 /* execute-protect */
+#define MEMATTR_NV 0x00008000 /* non-volatile */
+#define MEMATTR_MORE_RELIABLE 0x00010000 /* higher reliability */
+#define MEMATTR_RO 0x00020000 /* read-only */
+#define MEMATTR_SP 0x00040000 /* specific-purpose */
+
/* PnP I/O specific bits (IORESOURCE_BITS) */
#define IORESOURCE_IO_16BIT_ADDR (1<<0)
#define IORESOURCE_IO_FIXED (1<<1)
--
2.39.5
More information about the barebox
mailing list