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