1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * fw_tables.c - Parsing support for ACPI and ACPI-like tables provided by
4 * platform or device firmware
6 * Copyright (C) 2001 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * Copyright (C) 2023 Intel Corp.
9 #include <linux/errno.h>
10 #include <linux/acpi.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/string.h>
14 #include <linux/types.h>
16 enum acpi_subtable_type
{
23 struct acpi_subtable_entry
{
24 union acpi_subtable_headers
*hdr
;
25 enum acpi_subtable_type type
;
28 static unsigned long __init_or_acpilib
29 acpi_get_entry_type(struct acpi_subtable_entry
*entry
)
31 switch (entry
->type
) {
32 case ACPI_SUBTABLE_COMMON
:
33 return entry
->hdr
->common
.type
;
34 case ACPI_SUBTABLE_HMAT
:
35 return entry
->hdr
->hmat
.type
;
36 case ACPI_SUBTABLE_PRMT
:
38 case ACPI_SUBTABLE_CEDT
:
39 return entry
->hdr
->cedt
.type
;
44 static unsigned long __init_or_acpilib
45 acpi_get_entry_length(struct acpi_subtable_entry
*entry
)
47 switch (entry
->type
) {
48 case ACPI_SUBTABLE_COMMON
:
49 return entry
->hdr
->common
.length
;
50 case ACPI_SUBTABLE_HMAT
:
51 return entry
->hdr
->hmat
.length
;
52 case ACPI_SUBTABLE_PRMT
:
53 return entry
->hdr
->prmt
.length
;
54 case ACPI_SUBTABLE_CEDT
:
55 return entry
->hdr
->cedt
.length
;
60 static unsigned long __init_or_acpilib
61 acpi_get_subtable_header_length(struct acpi_subtable_entry
*entry
)
63 switch (entry
->type
) {
64 case ACPI_SUBTABLE_COMMON
:
65 return sizeof(entry
->hdr
->common
);
66 case ACPI_SUBTABLE_HMAT
:
67 return sizeof(entry
->hdr
->hmat
);
68 case ACPI_SUBTABLE_PRMT
:
69 return sizeof(entry
->hdr
->prmt
);
70 case ACPI_SUBTABLE_CEDT
:
71 return sizeof(entry
->hdr
->cedt
);
76 static enum acpi_subtable_type __init_or_acpilib
77 acpi_get_subtable_type(char *id
)
79 if (strncmp(id
, ACPI_SIG_HMAT
, 4) == 0)
80 return ACPI_SUBTABLE_HMAT
;
81 if (strncmp(id
, ACPI_SIG_PRMT
, 4) == 0)
82 return ACPI_SUBTABLE_PRMT
;
83 if (strncmp(id
, ACPI_SIG_CEDT
, 4) == 0)
84 return ACPI_SUBTABLE_CEDT
;
85 return ACPI_SUBTABLE_COMMON
;
88 static __init_or_acpilib
bool has_handler(struct acpi_subtable_proc
*proc
)
90 return proc
->handler
|| proc
->handler_arg
;
93 static __init_or_acpilib
int call_handler(struct acpi_subtable_proc
*proc
,
94 union acpi_subtable_headers
*hdr
,
98 return proc
->handler(hdr
, end
);
99 if (proc
->handler_arg
)
100 return proc
->handler_arg(hdr
, proc
->arg
, end
);
105 * acpi_parse_entries_array - for each proc_num find a suitable subtable
107 * @id: table id (for debugging purposes)
108 * @table_size: size of the root table
109 * @table_header: where does the table start?
110 * @proc: array of acpi_subtable_proc struct containing entry id
111 * and associated handler with it
112 * @proc_num: how big proc is?
113 * @max_entries: how many entries can we process?
115 * For each proc_num find a subtable with proc->id and run proc->handler
116 * on it. Assumption is that there's only single handler for particular
119 * The table_size is not the size of the complete ACPI table (the length
120 * field in the header struct), but only the size of the root table; i.e.,
121 * the offset from the very first byte of the complete ACPI table, to the
122 * first byte of the very first subtable.
124 * On success returns sum of all matching entries for all proc handlers.
125 * Otherwise, -ENODEV or -EINVAL is returned.
127 int __init_or_acpilib
128 acpi_parse_entries_array(char *id
, unsigned long table_size
,
129 struct acpi_table_header
*table_header
,
130 struct acpi_subtable_proc
*proc
,
131 int proc_num
, unsigned int max_entries
)
133 unsigned long table_end
, subtable_len
, entry_len
;
134 struct acpi_subtable_entry entry
;
139 table_end
= (unsigned long)table_header
+ table_header
->length
;
141 /* Parse all entries looking for a match. */
143 entry
.type
= acpi_get_subtable_type(id
);
144 entry
.hdr
= (union acpi_subtable_headers
*)
145 ((unsigned long)table_header
+ table_size
);
146 subtable_len
= acpi_get_subtable_header_length(&entry
);
148 while (((unsigned long)entry
.hdr
) + subtable_len
< table_end
) {
149 if (max_entries
&& count
>= max_entries
)
152 for (i
= 0; i
< proc_num
; i
++) {
153 if (acpi_get_entry_type(&entry
) != proc
[i
].id
)
155 if (!has_handler(&proc
[i
]) ||
157 call_handler(&proc
[i
], entry
.hdr
, table_end
))) {
169 * If entry->length is 0, break from this loop to avoid
172 entry_len
= acpi_get_entry_length(&entry
);
173 if (entry_len
== 0) {
174 pr_err("[%4.4s:0x%02x] Invalid zero length\n", id
, proc
->id
);
178 entry
.hdr
= (union acpi_subtable_headers
*)
179 ((unsigned long)entry
.hdr
+ entry_len
);
182 if (max_entries
&& count
> max_entries
) {
183 pr_warn("[%4.4s:0x%02x] found the maximum %i entries\n",
184 id
, proc
->id
, count
);
187 return errs
? -EINVAL
: count
;