lib/firmware_table: tables: Add CDAT table parsing support
authorDave Jiang <dave.jiang@intel.com>
Thu, 21 Dec 2023 22:02:31 +0000 (15:02 -0700)
committerDan Williams <dan.j.williams@intel.com>
Fri, 22 Dec 2023 22:23:13 +0000 (14:23 -0800)
The CDAT table is very similar to ACPI tables when it comes to sub-table
and entry structures. The helper functions can be also used to parse the
CDAT table. Add support to the helper functions to deal with an external
CDAT table, and also handle the endieness since CDAT can be processed by a
BE host. Export a function cdat_table_parse() for CXL driver to parse
a CDAT table.

In order to minimize ACPICA code changes, __force is being utilized to deal
with the case of a big endian (BE) host parsing a CDAT. All CDAT data
structure variables are being force casted to __leX as appropriate.

Cc: Rafael J. Wysocki <rafael@kernel.org>
Cc: Len Brown <lenb@kernel.org>
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Signed-off-by: Dave Jiang <dave.jiang@intel.com>
Acked-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Link: https://lore.kernel.org/r/170319615131.2212653.10932785667981494238.stgit@djiang5-mobl3
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
drivers/acpi/tables.c
include/linux/fw_table.h
lib/fw_table.c

index c1516337f6682840bdee196e50c1de0cc2472a12..b07f7d091d133c6ade25749ca746b2a64c6bb5e8 100644 (file)
@@ -251,8 +251,9 @@ int __init_or_acpilib acpi_table_parse_entries_array(
                return -ENODEV;
        }
 
-       count = acpi_parse_entries_array(id, table_size, table_header,
-                       proc, proc_num, max_entries);
+       count = acpi_parse_entries_array(id, table_size,
+                                        (union fw_table_header *)table_header,
+                                        proc, proc_num, max_entries);
 
        acpi_put_table(table_header);
        return count;
index ca49947f0a775a16df3f086bb490c46b9f6efb17..95421860397a236d101bf4c3bc7a934a515bf847 100644 (file)
@@ -25,16 +25,35 @@ struct acpi_subtable_proc {
        int count;
 };
 
+union fw_table_header {
+       struct acpi_table_header acpi;
+       struct acpi_table_cdat cdat;
+};
+
 union acpi_subtable_headers {
        struct acpi_subtable_header common;
        struct acpi_hmat_structure hmat;
        struct acpi_prmt_module_header prmt;
        struct acpi_cedt_header cedt;
+       struct acpi_cdat_header cdat;
 };
 
 int acpi_parse_entries_array(char *id, unsigned long table_size,
-                            struct acpi_table_header *table_header,
+                            union fw_table_header *table_header,
                             struct acpi_subtable_proc *proc,
                             int proc_num, unsigned int max_entries);
 
+int cdat_table_parse(enum acpi_cdat_type type,
+                    acpi_tbl_entry_handler_arg handler_arg, void *arg,
+                    struct acpi_table_cdat *table_header);
+
+/* CXL is the only non-ACPI consumer of the FIRMWARE_TABLE library */
+#if IS_ENABLED(CONFIG_ACPI) && !IS_ENABLED(CONFIG_CXL_BUS)
+#define EXPORT_SYMBOL_FWTBL_LIB(x) EXPORT_SYMBOL_ACPI_LIB(x)
+#define __init_or_fwtbl_lib __init_or_acpilib
+#else
+#define EXPORT_SYMBOL_FWTBL_LIB(x) EXPORT_SYMBOL_NS_GPL(x, CXL)
+#define __init_or_fwtbl_lib
+#endif
+
 #endif
index 294df54e33b6fea2e328863ff00413825d78d46c..1e5e0b2f70120374577124beb3cb537a4a2e4baa 100644 (file)
 #include <linux/kernel.h>
 #include <linux/string.h>
 #include <linux/types.h>
+#include <linux/fw_table.h>
 
 enum acpi_subtable_type {
        ACPI_SUBTABLE_COMMON,
        ACPI_SUBTABLE_HMAT,
        ACPI_SUBTABLE_PRMT,
        ACPI_SUBTABLE_CEDT,
+       CDAT_SUBTABLE,
 };
 
 struct acpi_subtable_entry {
@@ -25,7 +27,7 @@ struct acpi_subtable_entry {
        enum acpi_subtable_type type;
 };
 
-static unsigned long __init_or_acpilib
+static unsigned long __init_or_fwtbl_lib
 acpi_get_entry_type(struct acpi_subtable_entry *entry)
 {
        switch (entry->type) {
@@ -37,11 +39,13 @@ acpi_get_entry_type(struct acpi_subtable_entry *entry)
                return 0;
        case ACPI_SUBTABLE_CEDT:
                return entry->hdr->cedt.type;
+       case CDAT_SUBTABLE:
+               return entry->hdr->cdat.type;
        }
        return 0;
 }
 
-static unsigned long __init_or_acpilib
+static unsigned long __init_or_fwtbl_lib
 acpi_get_entry_length(struct acpi_subtable_entry *entry)
 {
        switch (entry->type) {
@@ -53,11 +57,16 @@ acpi_get_entry_length(struct acpi_subtable_entry *entry)
                return entry->hdr->prmt.length;
        case ACPI_SUBTABLE_CEDT:
                return entry->hdr->cedt.length;
+       case CDAT_SUBTABLE: {
+               __le16 length = (__force __le16)entry->hdr->cdat.length;
+
+               return le16_to_cpu(length);
+       }
        }
        return 0;
 }
 
-static unsigned long __init_or_acpilib
+static unsigned long __init_or_fwtbl_lib
 acpi_get_subtable_header_length(struct acpi_subtable_entry *entry)
 {
        switch (entry->type) {
@@ -69,11 +78,13 @@ acpi_get_subtable_header_length(struct acpi_subtable_entry *entry)
                return sizeof(entry->hdr->prmt);
        case ACPI_SUBTABLE_CEDT:
                return sizeof(entry->hdr->cedt);
+       case CDAT_SUBTABLE:
+               return sizeof(entry->hdr->cdat);
        }
        return 0;
 }
 
-static enum acpi_subtable_type __init_or_acpilib
+static enum acpi_subtable_type __init_or_fwtbl_lib
 acpi_get_subtable_type(char *id)
 {
        if (strncmp(id, ACPI_SIG_HMAT, 4) == 0)
@@ -82,17 +93,32 @@ acpi_get_subtable_type(char *id)
                return ACPI_SUBTABLE_PRMT;
        if (strncmp(id, ACPI_SIG_CEDT, 4) == 0)
                return ACPI_SUBTABLE_CEDT;
+       if (strncmp(id, ACPI_SIG_CDAT, 4) == 0)
+               return CDAT_SUBTABLE;
        return ACPI_SUBTABLE_COMMON;
 }
 
-static __init_or_acpilib bool has_handler(struct acpi_subtable_proc *proc)
+static unsigned long __init_or_fwtbl_lib
+acpi_table_get_length(enum acpi_subtable_type type,
+                     union fw_table_header *header)
+{
+       if (type == CDAT_SUBTABLE) {
+               __le32 length = (__force __le32)header->cdat.length;
+
+               return le32_to_cpu(length);
+       }
+
+       return header->acpi.length;
+}
+
+static __init_or_fwtbl_lib bool has_handler(struct acpi_subtable_proc *proc)
 {
        return proc->handler || proc->handler_arg;
 }
 
-static __init_or_acpilib int call_handler(struct acpi_subtable_proc *proc,
-                                         union acpi_subtable_headers *hdr,
-                                         unsigned long end)
+static __init_or_fwtbl_lib int call_handler(struct acpi_subtable_proc *proc,
+                                           union acpi_subtable_headers *hdr,
+                                           unsigned long end)
 {
        if (proc->handler)
                return proc->handler(hdr, end);
@@ -124,23 +150,26 @@ static __init_or_acpilib int call_handler(struct acpi_subtable_proc *proc,
  * On success returns sum of all matching entries for all proc handlers.
  * Otherwise, -ENODEV or -EINVAL is returned.
  */
-int __init_or_acpilib
+int __init_or_fwtbl_lib
 acpi_parse_entries_array(char *id, unsigned long table_size,
-                        struct acpi_table_header *table_header,
+                        union fw_table_header *table_header,
                         struct acpi_subtable_proc *proc,
                         int proc_num, unsigned int max_entries)
 {
        unsigned long table_end, subtable_len, entry_len;
        struct acpi_subtable_entry entry;
+       enum acpi_subtable_type type;
        int count = 0;
        int errs = 0;
        int i;
 
-       table_end = (unsigned long)table_header + table_header->length;
+       type = acpi_get_subtable_type(id);
+       table_end = (unsigned long)table_header +
+                   acpi_table_get_length(type, table_header);
 
        /* Parse all entries looking for a match. */
 
-       entry.type = acpi_get_subtable_type(id);
+       entry.type = type;
        entry.hdr = (union acpi_subtable_headers *)
            ((unsigned long)table_header + table_size);
        subtable_len = acpi_get_subtable_header_length(&entry);
@@ -186,3 +215,25 @@ acpi_parse_entries_array(char *id, unsigned long table_size,
 
        return errs ? -EINVAL : count;
 }
+
+int __init_or_fwtbl_lib
+cdat_table_parse(enum acpi_cdat_type type,
+                acpi_tbl_entry_handler_arg handler_arg,
+                void *arg,
+                struct acpi_table_cdat *table_header)
+{
+       struct acpi_subtable_proc proc = {
+               .id             = type,
+               .handler_arg    = handler_arg,
+               .arg            = arg,
+       };
+
+       if (!table_header)
+               return -EINVAL;
+
+       return acpi_parse_entries_array(ACPI_SIG_CDAT,
+                                       sizeof(struct acpi_table_cdat),
+                                       (union fw_table_header *)table_header,
+                                       &proc, 1, 0);
+}
+EXPORT_SYMBOL_FWTBL_LIB(cdat_table_parse);