1 /*
2  *  acpi_tables.c - ACPI Boot-Time Table Parsing
3  *
4  *  Copyright (C) 2001 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
5  *
6  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; If not, see <http://www.gnu.org/licenses/>.
20  *
21  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22  *
23  */
24 
25 #include <xen/init.h>
26 #include <xen/kernel.h>
27 #include <xen/param.h>
28 #include <xen/smp.h>
29 #include <xen/string.h>
30 #include <xen/types.h>
31 #include <xen/irq.h>
32 #include <xen/errno.h>
33 #include <xen/acpi.h>
34 
35 #define PREFIX			"ACPI: "
36 
37 #define ACPI_MAX_TABLES		128
38 
39 static const char *__initdata
40 mps_inti_flags_polarity[] = { "dfl", "high", "res", "low" };
41 static const char *__initdata
42 mps_inti_flags_trigger[] = { "dfl", "edge", "res", "level" };
43 
44 static int acpi_apic_instance __initdata;
45 
acpi_table_print_madt_entry(struct acpi_subtable_header * header)46 void __init acpi_table_print_madt_entry(struct acpi_subtable_header *header)
47 {
48 	if (!header)
49 		return;
50 
51 	switch (header->type) {
52 
53 	case ACPI_MADT_TYPE_LOCAL_APIC:
54 		{
55 			struct acpi_madt_local_apic *p =
56 			    (struct acpi_madt_local_apic *)header;
57 			printk(KERN_INFO PREFIX
58 			       "LAPIC (acpi_id[0x%02x] lapic_id[0x%02x] %s)\n",
59 			       p->processor_id, p->id,
60 			       (p->lapic_flags & ACPI_MADT_ENABLED) ? "enabled" : "disabled");
61 		}
62 		break;
63 
64 	case ACPI_MADT_TYPE_LOCAL_X2APIC:
65 		{
66 			struct acpi_madt_local_x2apic *p =
67 			    (struct acpi_madt_local_x2apic *)header;
68 			printk(KERN_INFO PREFIX
69 			       "X2APIC (apic_id[0x%02x] uid[0x%02x] %s)\n",
70 			       p->local_apic_id, p->uid,
71 			       (p->lapic_flags & ACPI_MADT_ENABLED) ?
72 			       "enabled" : "disabled");
73 		}
74 		break;
75 
76 	case ACPI_MADT_TYPE_IO_APIC:
77 		{
78 			struct acpi_madt_io_apic *p =
79 			    (struct acpi_madt_io_apic *)header;
80 			printk(KERN_INFO PREFIX
81 			       "IOAPIC (id[0x%02x] address[0x%08x] gsi_base[%d])\n",
82 			       p->id, p->address, p->global_irq_base);
83 		}
84 		break;
85 
86 	case ACPI_MADT_TYPE_INTERRUPT_OVERRIDE:
87 		{
88 			struct acpi_madt_interrupt_override *p =
89 			    (struct acpi_madt_interrupt_override *)header;
90 			printk(KERN_INFO PREFIX
91 			       "INT_SRC_OVR (bus %d bus_irq %d global_irq %d %s %s)\n",
92 			       p->bus, p->source_irq, p->global_irq,
93 			       mps_inti_flags_polarity[p->inti_flags & ACPI_MADT_POLARITY_MASK],
94 			       mps_inti_flags_trigger[(p->inti_flags & ACPI_MADT_TRIGGER_MASK) >> 2]);
95 			if (p->inti_flags  &
96 			    ~(ACPI_MADT_POLARITY_MASK | ACPI_MADT_TRIGGER_MASK))
97 				printk(KERN_INFO PREFIX
98 				       "INT_SRC_OVR unexpected reserved flags: %#x\n",
99 				       p->inti_flags  &
100 					~(ACPI_MADT_POLARITY_MASK | ACPI_MADT_TRIGGER_MASK));
101 
102 		}
103 		break;
104 
105 	case ACPI_MADT_TYPE_NMI_SOURCE:
106 		{
107 			struct acpi_madt_nmi_source *p =
108 			    (struct acpi_madt_nmi_source *)header;
109 			printk(KERN_INFO PREFIX
110 			       "NMI_SRC (%s %s global_irq %d)\n",
111 			       mps_inti_flags_polarity[p->inti_flags & ACPI_MADT_POLARITY_MASK],
112 			       mps_inti_flags_trigger[(p->inti_flags & ACPI_MADT_TRIGGER_MASK) >> 2],
113 			       p->global_irq);
114 		}
115 		break;
116 
117 	case ACPI_MADT_TYPE_LOCAL_APIC_NMI:
118 		{
119 			struct acpi_madt_local_apic_nmi *p =
120 			    (struct acpi_madt_local_apic_nmi *)header;
121 			printk(KERN_INFO PREFIX
122 			       "LAPIC_NMI (acpi_id[0x%02x] %s %s lint[%#x])\n",
123 			       p->processor_id,
124 			       mps_inti_flags_polarity[p->inti_flags & ACPI_MADT_POLARITY_MASK	],
125 			       mps_inti_flags_trigger[(p->inti_flags & ACPI_MADT_TRIGGER_MASK) >> 2],
126 			       p->lint);
127 		}
128 		break;
129 
130 	case ACPI_MADT_TYPE_LOCAL_X2APIC_NMI:
131 		{
132 			u16 polarity, trigger;
133 			struct acpi_madt_local_x2apic_nmi *p =
134 			    (struct acpi_madt_local_x2apic_nmi *)header;
135 
136 			polarity = p->inti_flags & ACPI_MADT_POLARITY_MASK;
137 			trigger = (p->inti_flags & ACPI_MADT_TRIGGER_MASK) >> 2;
138 
139 			printk(KERN_INFO PREFIX
140 			       "X2APIC_NMI (uid[0x%02x] %s %s lint[%#x])\n",
141 			       p->uid,
142 			       mps_inti_flags_polarity[polarity],
143 			       mps_inti_flags_trigger[trigger],
144 			       p->lint);
145 		}
146 		break;
147 
148 	case ACPI_MADT_TYPE_LOCAL_APIC_OVERRIDE:
149 		{
150 			struct acpi_madt_local_apic_override *p =
151 			    (struct acpi_madt_local_apic_override *)header;
152 			printk(KERN_INFO PREFIX
153 			       "LAPIC_ADDR_OVR (address[%p])\n",
154 			       (void *)(unsigned long)p->address);
155 		}
156 		break;
157 
158 	case ACPI_MADT_TYPE_IO_SAPIC:
159 		{
160 			struct acpi_madt_io_sapic *p =
161 			    (struct acpi_madt_io_sapic *)header;
162 			printk(KERN_INFO PREFIX
163 			       "IOSAPIC (id[%#x] address[%p] gsi_base[%d])\n",
164 			       p->id, (void *)(unsigned long)p->address,
165 			       p->global_irq_base);
166 		}
167 		break;
168 
169 	case ACPI_MADT_TYPE_LOCAL_SAPIC:
170 		{
171 			struct acpi_madt_local_sapic *p =
172 			    (struct acpi_madt_local_sapic *)header;
173 			printk(KERN_INFO PREFIX
174 			       "LSAPIC (acpi_id[0x%02x] lsapic_id[0x%02x] lsapic_eid[0x%02x] %s)\n",
175 			       p->processor_id, p->id, p->eid,
176 			       (p->lapic_flags & ACPI_MADT_ENABLED) ? "enabled" : "disabled");
177 		}
178 		break;
179 
180 	case ACPI_MADT_TYPE_INTERRUPT_SOURCE:
181 		{
182 			struct acpi_madt_interrupt_source *p =
183 			    (struct acpi_madt_interrupt_source *)header;
184 			printk(KERN_INFO PREFIX
185 			       "PLAT_INT_SRC (%s %s type[%#x] id[0x%04x] eid[%#x] iosapic_vector[%#x] global_irq[%#x]\n",
186 			       mps_inti_flags_polarity[p->inti_flags & ACPI_MADT_POLARITY_MASK],
187 			       mps_inti_flags_trigger[(p->inti_flags & ACPI_MADT_TRIGGER_MASK) >> 2],
188 			       p->type, p->id, p->eid, p->io_sapic_vector,
189 			       p->global_irq);
190 		}
191 		break;
192 
193 	case ACPI_MADT_TYPE_GENERIC_INTERRUPT:
194 		{
195 			struct acpi_madt_generic_interrupt *p =
196 				container_of(header, struct acpi_madt_generic_interrupt, header);
197 
198 			printk(KERN_DEBUG PREFIX
199 			       "GICC (acpi_id[0x%04x] address[0x%"PRIx64"] MPIDR[0x%"PRIx64"] %s)\n",
200 			       p->uid, p->base_address,
201 			       p->arm_mpidr,
202 			       (p->flags & ACPI_MADT_ENABLED) ? "enabled" : "disabled");
203 
204 		}
205 		break;
206 
207 	case ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR:
208 		{
209 			struct acpi_madt_generic_distributor *p =
210 				container_of(header, struct acpi_madt_generic_distributor, header);
211 
212 			printk(KERN_DEBUG PREFIX
213 			       "GIC Distributor (gic_id[0x%04x] address[0x%"PRIx64"] gsi_base[%d])\n",
214 			       p->gic_id, p->base_address,
215 			       p->global_irq_base);
216 		}
217 		break;
218 
219 	default:
220 		printk(KERN_WARNING PREFIX
221 		       "Found unsupported MADT entry (type = %#x)\n",
222 		       header->type);
223 		break;
224 	}
225 }
226 
227 static struct acpi_subtable_header * __init
acpi_get_entry(const char * id,unsigned long table_size,const struct acpi_table_header * table_header,enum acpi_madt_type entry_id,unsigned int entry_index)228 acpi_get_entry(const char *id, unsigned long table_size,
229 	       const struct acpi_table_header *table_header,
230 	       enum acpi_madt_type entry_id, unsigned int entry_index)
231 {
232 	struct acpi_subtable_header *entry;
233 	int count = 0;
234 	unsigned long table_end;
235 
236 	if (!table_size)
237 		return NULL;
238 
239 	if (!table_header) {
240 		printk(KERN_WARNING PREFIX "%4.4s not present\n", id);
241 		return NULL;
242 	}
243 
244 	table_end = (unsigned long)table_header + table_header->length;
245 
246 	/* Parse all entries looking for a match. */
247 	entry = (void *)table_header + table_size;
248 
249 	while ((unsigned long)(entry + 1) < table_end) {
250 		if (entry->length < sizeof(*entry)) {
251 			printk(KERN_ERR PREFIX "[%4.4s:%#x] Invalid length\n",
252 			       id, entry_id);
253 			return NULL;
254 		}
255 
256 		if (entry->type == entry_id) {
257 			if (count == entry_index)
258 				return entry;
259 			count++;
260 		}
261 
262 		entry = (void *)entry + entry->length;
263 	}
264 
265 	return NULL;
266 }
267 
268 struct acpi_subtable_header * __init
acpi_table_get_entry_madt(enum acpi_madt_type entry_id,unsigned int entry_index)269 acpi_table_get_entry_madt(enum acpi_madt_type entry_id,
270 			  unsigned int entry_index)
271 {
272 	struct acpi_table_header *table_header;
273 	acpi_status status;
274 
275 	status = acpi_get_table(ACPI_SIG_MADT, acpi_apic_instance,
276 				&table_header);
277 	if (ACPI_FAILURE(status)) {
278 		printk(KERN_WARNING PREFIX "%4.4s not present\n",
279 		       ACPI_SIG_MADT);
280 		return NULL;
281 	}
282 
283 	return acpi_get_entry(ACPI_SIG_MADT, sizeof(struct acpi_table_madt),
284 			      table_header, entry_id, entry_index);
285 }
286 
287 int __init
acpi_parse_entries(char * id,unsigned long table_size,acpi_table_entry_handler handler,struct acpi_table_header * table_header,int entry_id,unsigned int max_entries)288 acpi_parse_entries(char *id, unsigned long table_size,
289 		   acpi_table_entry_handler handler,
290 		   struct acpi_table_header *table_header,
291 		   int entry_id, unsigned int max_entries)
292 {
293 	struct acpi_subtable_header *entry;
294 	int count = 0;
295 	unsigned long table_end;
296 
297 	if (acpi_disabled)
298 		return -ENODEV;
299 
300 	if (!id || !handler)
301 		return -EINVAL;
302 
303 	if (!table_size)
304 		return -EINVAL;
305 
306 	if (!table_header) {
307 		printk(KERN_WARNING PREFIX "%4.4s not present\n", id);
308 		return -ENODEV;
309 	}
310 
311 	table_end = (unsigned long)table_header + table_header->length;
312 
313 	/* Parse all entries looking for a match. */
314 
315 	entry = (struct acpi_subtable_header *)
316 	    ((unsigned long)table_header + table_size);
317 
318 	while (((unsigned long)entry) + sizeof(struct acpi_subtable_header) <
319 	       table_end) {
320 		if (entry->length < sizeof(*entry)) {
321 			printk(KERN_ERR PREFIX "[%4.4s:%#x] Invalid length\n",
322 			       id, entry_id);
323 			return -ENODATA;
324 		}
325 
326 		if (entry->type == entry_id
327 		    && (!max_entries || count < max_entries)) {
328 			if (handler(entry, table_end))
329 				return -EINVAL;
330 
331 			count++;
332 		}
333 
334 		entry = (struct acpi_subtable_header *)
335 		    ((unsigned long)entry + entry->length);
336 	}
337 
338 	if (max_entries && count > max_entries) {
339 		printk(KERN_WARNING PREFIX "[%4.4s:%#x] ignored %i entries of "
340 		       "%i found\n", id, entry_id, count - max_entries, count);
341 	}
342 
343 	return count;
344 }
345 
346 int __init
acpi_table_parse_entries(char * id,unsigned long table_size,int entry_id,acpi_table_entry_handler handler,unsigned int max_entries)347 acpi_table_parse_entries(char *id,
348 			 unsigned long table_size,
349 			 int entry_id,
350 			 acpi_table_entry_handler handler,
351 			 unsigned int max_entries)
352 {
353 	struct acpi_table_header *table_header = NULL;
354 	u32 instance = 0;
355 
356 	if (acpi_disabled)
357 		return -ENODEV;
358 
359 	if (!id || !handler)
360 		return -EINVAL;
361 
362 	if (!strncmp(id, ACPI_SIG_MADT, 4))
363 		instance = acpi_apic_instance;
364 
365 	acpi_get_table(id, instance, &table_header);
366 	if (!table_header) {
367 		printk(KERN_WARNING PREFIX "%4.4s not present\n", id);
368 		return -ENODEV;
369 	}
370 
371 	return acpi_parse_entries(id, table_size, handler, table_header,
372 				  entry_id, max_entries);
373 }
374 
375 int __init
acpi_table_parse_madt(enum acpi_madt_type id,acpi_table_entry_handler handler,unsigned int max_entries)376 acpi_table_parse_madt(enum acpi_madt_type id,
377 		      acpi_table_entry_handler handler, unsigned int max_entries)
378 {
379 	return acpi_table_parse_entries(ACPI_SIG_MADT,
380 					    sizeof(struct acpi_table_madt), id,
381 					    handler, max_entries);
382 }
383 
384 /**
385  * acpi_table_parse - find table with @id, run @handler on it
386  *
387  * @id: table id to find
388  * @handler: handler to run
389  *
390  * Scan the ACPI System Descriptor Table (STD) for a table matching @id,
391  * run @handler on it.
392  */
acpi_table_parse(char * id,acpi_table_handler handler)393 int __init acpi_table_parse(char *id, acpi_table_handler handler)
394 {
395 	struct acpi_table_header *table = NULL;
396 
397 	if (acpi_disabled)
398 		return -ENODEV;
399 
400 	if (!handler)
401 		return -EINVAL;
402 
403 	if (strncmp(id, ACPI_SIG_MADT, 4) == 0)
404 		acpi_get_table(id, acpi_apic_instance, &table);
405 	else
406 		acpi_get_table(id, 0, &table);
407 
408 	if (table) {
409 		return handler(table);
410 	} else
411 		return -ENODEV;
412 }
413 
414 /*
415  * The BIOS is supposed to supply a single APIC/MADT,
416  * but some report two.  Provide a knob to use either.
417  * (don't you wish instance 0 and 1 were not the same?)
418  */
check_multiple_madt(void)419 static void __init check_multiple_madt(void)
420 {
421 	struct acpi_table_header *table = NULL;
422 
423 	acpi_get_table(ACPI_SIG_MADT, 2, &table);
424 	if (table) {
425 		printk(KERN_WARNING PREFIX
426 		       "BIOS bug: multiple APIC/MADT found,"
427 		       " using %d\n", acpi_apic_instance);
428 		printk(KERN_WARNING PREFIX
429 		       "If \"acpi_apic_instance=%d\" works better, "
430 		       "notify linux-acpi@vger.kernel.org\n",
431 		       acpi_apic_instance ? 0 : 2);
432 
433 	} else
434 		acpi_apic_instance = 0;
435 
436 	return;
437 }
438 
439 /*
440  * acpi_table_init()
441  *
442  * find RSDP, find and checksum SDT/XSDT.
443  * checksum all tables, print SDT/XSDT
444  *
445  * result: sdt_entry[] is initialized
446  */
447 
acpi_table_init(void)448 int __init acpi_table_init(void)
449 {
450 	acpi_status status;
451 
452 	status = acpi_initialize_tables(NULL, ACPI_MAX_TABLES, 0);
453 	if (ACPI_FAILURE(status))
454 		return -EINVAL;
455 
456 	check_multiple_madt();
457 	return 0;
458 }
459 
acpi_parse_apic_instance(const char * str)460 static int __init acpi_parse_apic_instance(const char *str)
461 {
462 	const char *q;
463 
464 	acpi_apic_instance = simple_strtoul(str, &q, 0);
465 
466 	printk(KERN_NOTICE PREFIX "Shall use APIC/MADT table %d\n",
467 	       acpi_apic_instance);
468 
469 	return *q ? -EINVAL : 0;
470 }
471 custom_param("acpi_apic_instance", acpi_parse_apic_instance);
472