]> Git Repo - J-linux.git/blob - lib/fw_table.c
cxl/core: use sysfs_emit() for attr's _show()
[J-linux.git] / lib / fw_table.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  fw_tables.c - Parsing support for ACPI and ACPI-like tables provided by
4  *                platform or device firmware
5  *
6  *  Copyright (C) 2001 Paul Diefenbaugh <[email protected]>
7  *  Copyright (C) 2023 Intel Corp.
8  */
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>
15 #include <linux/fw_table.h>
16
17 enum acpi_subtable_type {
18         ACPI_SUBTABLE_COMMON,
19         ACPI_SUBTABLE_HMAT,
20         ACPI_SUBTABLE_PRMT,
21         ACPI_SUBTABLE_CEDT,
22         CDAT_SUBTABLE,
23 };
24
25 struct acpi_subtable_entry {
26         union acpi_subtable_headers *hdr;
27         enum acpi_subtable_type type;
28 };
29
30 static unsigned long __init_or_fwtbl_lib
31 acpi_get_entry_type(struct acpi_subtable_entry *entry)
32 {
33         switch (entry->type) {
34         case ACPI_SUBTABLE_COMMON:
35                 return entry->hdr->common.type;
36         case ACPI_SUBTABLE_HMAT:
37                 return entry->hdr->hmat.type;
38         case ACPI_SUBTABLE_PRMT:
39                 return 0;
40         case ACPI_SUBTABLE_CEDT:
41                 return entry->hdr->cedt.type;
42         case CDAT_SUBTABLE:
43                 return entry->hdr->cdat.type;
44         }
45         return 0;
46 }
47
48 static unsigned long __init_or_fwtbl_lib
49 acpi_get_entry_length(struct acpi_subtable_entry *entry)
50 {
51         switch (entry->type) {
52         case ACPI_SUBTABLE_COMMON:
53                 return entry->hdr->common.length;
54         case ACPI_SUBTABLE_HMAT:
55                 return entry->hdr->hmat.length;
56         case ACPI_SUBTABLE_PRMT:
57                 return entry->hdr->prmt.length;
58         case ACPI_SUBTABLE_CEDT:
59                 return entry->hdr->cedt.length;
60         case CDAT_SUBTABLE: {
61                 __le16 length = (__force __le16)entry->hdr->cdat.length;
62
63                 return le16_to_cpu(length);
64         }
65         }
66         return 0;
67 }
68
69 static unsigned long __init_or_fwtbl_lib
70 acpi_get_subtable_header_length(struct acpi_subtable_entry *entry)
71 {
72         switch (entry->type) {
73         case ACPI_SUBTABLE_COMMON:
74                 return sizeof(entry->hdr->common);
75         case ACPI_SUBTABLE_HMAT:
76                 return sizeof(entry->hdr->hmat);
77         case ACPI_SUBTABLE_PRMT:
78                 return sizeof(entry->hdr->prmt);
79         case ACPI_SUBTABLE_CEDT:
80                 return sizeof(entry->hdr->cedt);
81         case CDAT_SUBTABLE:
82                 return sizeof(entry->hdr->cdat);
83         }
84         return 0;
85 }
86
87 static enum acpi_subtable_type __init_or_fwtbl_lib
88 acpi_get_subtable_type(char *id)
89 {
90         if (strncmp(id, ACPI_SIG_HMAT, 4) == 0)
91                 return ACPI_SUBTABLE_HMAT;
92         if (strncmp(id, ACPI_SIG_PRMT, 4) == 0)
93                 return ACPI_SUBTABLE_PRMT;
94         if (strncmp(id, ACPI_SIG_CEDT, 4) == 0)
95                 return ACPI_SUBTABLE_CEDT;
96         if (strncmp(id, ACPI_SIG_CDAT, 4) == 0)
97                 return CDAT_SUBTABLE;
98         return ACPI_SUBTABLE_COMMON;
99 }
100
101 static unsigned long __init_or_fwtbl_lib
102 acpi_table_get_length(enum acpi_subtable_type type,
103                       union fw_table_header *header)
104 {
105         if (type == CDAT_SUBTABLE) {
106                 __le32 length = (__force __le32)header->cdat.length;
107
108                 return le32_to_cpu(length);
109         }
110
111         return header->acpi.length;
112 }
113
114 static __init_or_fwtbl_lib bool has_handler(struct acpi_subtable_proc *proc)
115 {
116         return proc->handler || proc->handler_arg;
117 }
118
119 static __init_or_fwtbl_lib int call_handler(struct acpi_subtable_proc *proc,
120                                             union acpi_subtable_headers *hdr,
121                                             unsigned long end)
122 {
123         if (proc->handler)
124                 return proc->handler(hdr, end);
125         if (proc->handler_arg)
126                 return proc->handler_arg(hdr, proc->arg, end);
127         return -EINVAL;
128 }
129
130 /**
131  * acpi_parse_entries_array - for each proc_num find a suitable subtable
132  *
133  * @id: table id (for debugging purposes)
134  * @table_size: size of the root table
135  * @table_header: where does the table start?
136  * @proc: array of acpi_subtable_proc struct containing entry id
137  *        and associated handler with it
138  * @proc_num: how big proc is?
139  * @max_entries: how many entries can we process?
140  *
141  * For each proc_num find a subtable with proc->id and run proc->handler
142  * on it. Assumption is that there's only single handler for particular
143  * entry id.
144  *
145  * The table_size is not the size of the complete ACPI table (the length
146  * field in the header struct), but only the size of the root table; i.e.,
147  * the offset from the very first byte of the complete ACPI table, to the
148  * first byte of the very first subtable.
149  *
150  * On success returns sum of all matching entries for all proc handlers.
151  * Otherwise, -ENODEV or -EINVAL is returned.
152  */
153 int __init_or_fwtbl_lib
154 acpi_parse_entries_array(char *id, unsigned long table_size,
155                          union fw_table_header *table_header,
156                          struct acpi_subtable_proc *proc,
157                          int proc_num, unsigned int max_entries)
158 {
159         unsigned long table_end, subtable_len, entry_len;
160         struct acpi_subtable_entry entry;
161         enum acpi_subtable_type type;
162         int count = 0;
163         int errs = 0;
164         int i;
165
166         type = acpi_get_subtable_type(id);
167         table_end = (unsigned long)table_header +
168                     acpi_table_get_length(type, table_header);
169
170         /* Parse all entries looking for a match. */
171
172         entry.type = type;
173         entry.hdr = (union acpi_subtable_headers *)
174             ((unsigned long)table_header + table_size);
175         subtable_len = acpi_get_subtable_header_length(&entry);
176
177         while (((unsigned long)entry.hdr) + subtable_len  < table_end) {
178                 if (max_entries && count >= max_entries)
179                         break;
180
181                 for (i = 0; i < proc_num; i++) {
182                         if (acpi_get_entry_type(&entry) != proc[i].id)
183                                 continue;
184                         if (!has_handler(&proc[i]) ||
185                             (!errs &&
186                              call_handler(&proc[i], entry.hdr, table_end))) {
187                                 errs++;
188                                 continue;
189                         }
190
191                         proc[i].count++;
192                         break;
193                 }
194                 if (i != proc_num)
195                         count++;
196
197                 /*
198                  * If entry->length is 0, break from this loop to avoid
199                  * infinite loop.
200                  */
201                 entry_len = acpi_get_entry_length(&entry);
202                 if (entry_len == 0) {
203                         pr_err("[%4.4s:0x%02x] Invalid zero length\n", id, proc->id);
204                         return -EINVAL;
205                 }
206
207                 entry.hdr = (union acpi_subtable_headers *)
208                     ((unsigned long)entry.hdr + entry_len);
209         }
210
211         if (max_entries && count > max_entries) {
212                 pr_warn("[%4.4s:0x%02x] found the maximum %i entries\n",
213                         id, proc->id, count);
214         }
215
216         return errs ? -EINVAL : count;
217 }
218
219 int __init_or_fwtbl_lib
220 cdat_table_parse(enum acpi_cdat_type type,
221                  acpi_tbl_entry_handler_arg handler_arg,
222                  void *arg,
223                  struct acpi_table_cdat *table_header)
224 {
225         struct acpi_subtable_proc proc = {
226                 .id             = type,
227                 .handler_arg    = handler_arg,
228                 .arg            = arg,
229         };
230
231         if (!table_header)
232                 return -EINVAL;
233
234         return acpi_parse_entries_array(ACPI_SIG_CDAT,
235                                         sizeof(struct acpi_table_cdat),
236                                         (union fw_table_header *)table_header,
237                                         &proc, 1, 0);
238 }
239 EXPORT_SYMBOL_FWTBL_LIB(cdat_table_parse);
This page took 0.042994 seconds and 4 git commands to generate.