]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/x86/acpica/madt.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / x86 / acpica / madt.c
1 /*-
2  * Copyright (c) 2003 John Baldwin <jhb@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of any co-contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/smp.h>
39 #include <vm/vm.h>
40 #include <vm/pmap.h>
41
42 #include <x86/apicreg.h>
43 #include <machine/intr_machdep.h>
44 #include <machine/apicvar.h>
45
46 #include <contrib/dev/acpica/include/acpi.h>
47 #include <contrib/dev/acpica/include/actables.h>
48
49 #include <dev/acpica/acpivar.h>
50 #include <dev/pci/pcivar.h>
51
52 /* These two arrays are indexed by APIC IDs. */
53 struct ioapic_info {
54         void *io_apic;
55         UINT32 io_vector;
56 } ioapics[MAX_APIC_ID + 1];
57
58 struct lapic_info {
59         u_int la_enabled:1;
60         u_int la_acpi_id:8;
61 } lapics[MAX_APIC_ID + 1];
62
63 static int madt_found_sci_override;
64 static ACPI_TABLE_MADT *madt;
65 static vm_paddr_t madt_physaddr;
66 static vm_offset_t madt_length;
67
68 MALLOC_DEFINE(M_MADT, "madt_table", "ACPI MADT Table Items");
69
70 static enum intr_polarity interrupt_polarity(UINT16 IntiFlags, UINT8 Source);
71 static enum intr_trigger interrupt_trigger(UINT16 IntiFlags, UINT8 Source);
72 static int      madt_find_cpu(u_int acpi_id, u_int *apic_id);
73 static int      madt_find_interrupt(int intr, void **apic, u_int *pin);
74 static void     madt_parse_apics(ACPI_SUBTABLE_HEADER *entry, void *arg);
75 static void     madt_parse_interrupt_override(
76                     ACPI_MADT_INTERRUPT_OVERRIDE *intr);
77 static void     madt_parse_ints(ACPI_SUBTABLE_HEADER *entry,
78                     void *arg __unused);
79 static void     madt_parse_local_nmi(ACPI_MADT_LOCAL_APIC_NMI *nmi);
80 static void     madt_parse_nmi(ACPI_MADT_NMI_SOURCE *nmi);
81 static int      madt_probe(void);
82 static int      madt_probe_cpus(void);
83 static void     madt_probe_cpus_handler(ACPI_SUBTABLE_HEADER *entry,
84                     void *arg __unused);
85 static void     madt_register(void *dummy);
86 static int      madt_setup_local(void);
87 static int      madt_setup_io(void);
88 static void     madt_walk_table(acpi_subtable_handler *handler, void *arg);
89
90 static struct apic_enumerator madt_enumerator = {
91         "MADT",
92         madt_probe,
93         madt_probe_cpus,
94         madt_setup_local,
95         madt_setup_io
96 };
97
98 /*
99  * Look for an ACPI Multiple APIC Description Table ("APIC")
100  */
101 static int
102 madt_probe(void)
103 {
104
105         madt_physaddr = acpi_find_table(ACPI_SIG_MADT);
106         if (madt_physaddr == 0)
107                 return (ENXIO);
108         return (0);
109 }
110
111 /*
112  * Run through the MP table enumerating CPUs.
113  */
114 static int
115 madt_probe_cpus(void)
116 {
117
118         madt = acpi_map_table(madt_physaddr, ACPI_SIG_MADT);
119         madt_length = madt->Header.Length;
120         KASSERT(madt != NULL, ("Unable to re-map MADT"));
121         madt_walk_table(madt_probe_cpus_handler, NULL);
122         acpi_unmap_table(madt);
123         madt = NULL;
124         return (0);
125 }
126
127 /*
128  * Initialize the local APIC on the BSP.
129  */
130 static int
131 madt_setup_local(void)
132 {
133
134         madt = pmap_mapbios(madt_physaddr, madt_length);
135         lapic_init(madt->Address);
136         printf("ACPI APIC Table: <%.*s %.*s>\n",
137             (int)sizeof(madt->Header.OemId), madt->Header.OemId,
138             (int)sizeof(madt->Header.OemTableId), madt->Header.OemTableId);
139
140         /*
141          * We ignore 64-bit local APIC override entries.  Should we
142          * perhaps emit a warning here if we find one?
143          */
144         return (0);
145 }
146
147 /*
148  * Enumerate I/O APICs and setup interrupt sources.
149  */
150 static int
151 madt_setup_io(void)
152 {
153         void *ioapic;
154         u_int pin;
155         int i;
156
157         /* Try to initialize ACPI so that we can access the FADT. */
158         i = acpi_Startup();
159         if (ACPI_FAILURE(i)) {
160                 printf("MADT: ACPI Startup failed with %s\n",
161                     AcpiFormatException(i));
162                 printf("Try disabling either ACPI or apic support.\n");
163                 panic("Using MADT but ACPI doesn't work");
164         }
165                     
166         /* First, we run through adding I/O APIC's. */
167         madt_walk_table(madt_parse_apics, NULL);
168
169         /* Second, we run through the table tweaking interrupt sources. */
170         madt_walk_table(madt_parse_ints, NULL);
171
172         /*
173          * If there was not an explicit override entry for the SCI,
174          * force it to use level trigger and active-low polarity.
175          */
176         if (!madt_found_sci_override) {
177                 if (madt_find_interrupt(AcpiGbl_FADT.SciInterrupt, &ioapic,
178                     &pin) != 0)
179                         printf("MADT: Could not find APIC for SCI IRQ %u\n",
180                             AcpiGbl_FADT.SciInterrupt);
181                 else {
182                         printf(
183         "MADT: Forcing active-low polarity and level trigger for SCI\n");
184                         ioapic_set_polarity(ioapic, pin, INTR_POLARITY_LOW);
185                         ioapic_set_triggermode(ioapic, pin, INTR_TRIGGER_LEVEL);
186                 }
187         }
188
189         /* Third, we register all the I/O APIC's. */
190         for (i = 0; i <= MAX_APIC_ID; i++)
191                 if (ioapics[i].io_apic != NULL)
192                         ioapic_register(ioapics[i].io_apic);
193
194         /* Finally, we throw the switch to enable the I/O APIC's. */
195         acpi_SetDefaultIntrModel(ACPI_INTR_APIC);
196
197         return (0);
198 }
199
200 static void
201 madt_register(void *dummy __unused)
202 {
203
204         apic_register_enumerator(&madt_enumerator);
205 }
206 SYSINIT(madt_register, SI_SUB_TUNABLES - 1, SI_ORDER_FIRST, madt_register, NULL);
207
208 /*
209  * Call the handler routine for each entry in the MADT table.
210  */
211 static void
212 madt_walk_table(acpi_subtable_handler *handler, void *arg)
213 {
214
215         acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
216             handler, arg);
217 }
218
219 static void
220 madt_probe_cpus_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
221 {
222         ACPI_MADT_LOCAL_APIC *proc;
223         struct lapic_info *la;
224
225         switch (entry->Type) {
226         case ACPI_MADT_TYPE_LOCAL_APIC:
227                 /*
228                  * The MADT does not include a BSP flag, so we have to
229                  * let the MP code figure out which CPU is the BSP on
230                  * its own.
231                  */
232                 proc = (ACPI_MADT_LOCAL_APIC *)entry;
233                 if (bootverbose)
234                         printf("MADT: Found CPU APIC ID %u ACPI ID %u: %s\n",
235                             proc->Id, proc->ProcessorId,
236                             (proc->LapicFlags & ACPI_MADT_ENABLED) ?
237                             "enabled" : "disabled");
238                 if (!(proc->LapicFlags & ACPI_MADT_ENABLED))
239                         break;
240                 if (proc->Id > MAX_APIC_ID)
241                         panic("%s: CPU ID %u too high", __func__, proc->Id);
242                 la = &lapics[proc->Id];
243                 KASSERT(la->la_enabled == 0,
244                     ("Duplicate local APIC ID %u", proc->Id));
245                 la->la_enabled = 1;
246                 la->la_acpi_id = proc->ProcessorId;
247                 lapic_create(proc->Id, 0);
248                 break;
249         }
250 }
251
252
253 /*
254  * Add an I/O APIC from an entry in the table.
255  */
256 static void
257 madt_parse_apics(ACPI_SUBTABLE_HEADER *entry, void *arg __unused)
258 {
259         ACPI_MADT_IO_APIC *apic;
260
261         switch (entry->Type) {
262         case ACPI_MADT_TYPE_IO_APIC:
263                 apic = (ACPI_MADT_IO_APIC *)entry;
264                 if (bootverbose)
265                         printf(
266                             "MADT: Found IO APIC ID %u, Interrupt %u at %p\n",
267                             apic->Id, apic->GlobalIrqBase,
268                             (void *)(uintptr_t)apic->Address);
269                 if (apic->Id > MAX_APIC_ID)
270                         panic("%s: I/O APIC ID %u too high", __func__,
271                             apic->Id);
272                 if (ioapics[apic->Id].io_apic != NULL)
273                         panic("%s: Double APIC ID %u", __func__, apic->Id);
274                 if (apic->GlobalIrqBase >= FIRST_MSI_INT) {
275                         printf("MADT: Ignoring bogus I/O APIC ID %u", apic->Id);
276                         break;
277                 }
278                 ioapics[apic->Id].io_apic = ioapic_create(apic->Address,
279                     apic->Id, apic->GlobalIrqBase);
280                 ioapics[apic->Id].io_vector = apic->GlobalIrqBase;
281                 break;
282         default:
283                 break;
284         }
285 }
286
287 /*
288  * Determine properties of an interrupt source.  Note that for ACPI these
289  * functions are only used for ISA interrupts, so we assume ISA bus values
290  * (Active Hi, Edge Triggered) for conforming values except for the ACPI
291  * SCI for which we use Active Lo, Level Triggered.
292  */
293 static enum intr_polarity
294 interrupt_polarity(UINT16 IntiFlags, UINT8 Source)
295 {
296
297         switch (IntiFlags & ACPI_MADT_POLARITY_MASK) {
298         case ACPI_MADT_POLARITY_CONFORMS:
299                 if (Source == AcpiGbl_FADT.SciInterrupt)
300                         return (INTR_POLARITY_LOW);
301                 else
302                         return (INTR_POLARITY_HIGH);
303         case ACPI_MADT_POLARITY_ACTIVE_HIGH:
304                 return (INTR_POLARITY_HIGH);
305         case ACPI_MADT_POLARITY_ACTIVE_LOW:
306                 return (INTR_POLARITY_LOW);
307         default:
308                 panic("Bogus Interrupt Polarity");
309         }
310 }
311
312 static enum intr_trigger
313 interrupt_trigger(UINT16 IntiFlags, UINT8 Source)
314 {
315
316         switch (IntiFlags & ACPI_MADT_TRIGGER_MASK) {
317         case ACPI_MADT_TRIGGER_CONFORMS:
318                 if (Source == AcpiGbl_FADT.SciInterrupt)
319                         return (INTR_TRIGGER_LEVEL);
320                 else
321                         return (INTR_TRIGGER_EDGE);
322         case ACPI_MADT_TRIGGER_EDGE:
323                 return (INTR_TRIGGER_EDGE);
324         case ACPI_MADT_TRIGGER_LEVEL:
325                 return (INTR_TRIGGER_LEVEL);
326         default:
327                 panic("Bogus Interrupt Trigger Mode");
328         }
329 }
330
331 /*
332  * Find the local APIC ID associated with a given ACPI Processor ID.
333  */
334 static int
335 madt_find_cpu(u_int acpi_id, u_int *apic_id)
336 {
337         int i;
338
339         for (i = 0; i <= MAX_APIC_ID; i++) {
340                 if (!lapics[i].la_enabled)
341                         continue;
342                 if (lapics[i].la_acpi_id != acpi_id)
343                         continue;
344                 *apic_id = i;
345                 return (0);
346         }
347         return (ENOENT);
348 }
349
350 /*
351  * Find the IO APIC and pin on that APIC associated with a given global
352  * interrupt.
353  */
354 static int
355 madt_find_interrupt(int intr, void **apic, u_int *pin)
356 {
357         int i, best;
358
359         best = -1;
360         for (i = 0; i <= MAX_APIC_ID; i++) {
361                 if (ioapics[i].io_apic == NULL ||
362                     ioapics[i].io_vector > intr)
363                         continue;
364                 if (best == -1 ||
365                     ioapics[best].io_vector < ioapics[i].io_vector)
366                         best = i;
367         }
368         if (best == -1)
369                 return (ENOENT);
370         *apic = ioapics[best].io_apic;
371         *pin = intr - ioapics[best].io_vector;
372         if (*pin > 32)
373                 printf("WARNING: Found intpin of %u for vector %d\n", *pin,
374                     intr);
375         return (0);
376 }
377
378 /*
379  * Parse an interrupt source override for an ISA interrupt.
380  */
381 static void
382 madt_parse_interrupt_override(ACPI_MADT_INTERRUPT_OVERRIDE *intr)
383 {
384         void *new_ioapic, *old_ioapic;
385         u_int new_pin, old_pin;
386         enum intr_trigger trig;
387         enum intr_polarity pol;
388         char buf[64];
389
390         if (acpi_quirks & ACPI_Q_MADT_IRQ0 && intr->SourceIrq == 0 &&
391             intr->GlobalIrq == 2) {
392                 if (bootverbose)
393                         printf("MADT: Skipping timer override\n");
394                 return;
395         }
396         if (bootverbose)
397                 printf("MADT: Interrupt override: source %u, irq %u\n",
398                     intr->SourceIrq, intr->GlobalIrq);
399         KASSERT(intr->Bus == 0, ("bus for interrupt overrides must be zero"));
400         if (madt_find_interrupt(intr->GlobalIrq, &new_ioapic, &new_pin) != 0) {
401                 printf("MADT: Could not find APIC for vector %u (IRQ %u)\n",
402                     intr->GlobalIrq, intr->SourceIrq);
403                 return;
404         }
405
406         /*
407          * Lookup the appropriate trigger and polarity modes for this
408          * entry.
409          */
410         trig = interrupt_trigger(intr->IntiFlags, intr->SourceIrq);
411         pol = interrupt_polarity(intr->IntiFlags, intr->SourceIrq);
412         
413         /*
414          * If the SCI is identity mapped but has edge trigger and
415          * active-hi polarity or the force_sci_lo tunable is set,
416          * force it to use level/lo.
417          */
418         if (intr->SourceIrq == AcpiGbl_FADT.SciInterrupt) {
419                 madt_found_sci_override = 1;
420                 if (getenv_string("hw.acpi.sci.trigger", buf, sizeof(buf))) {
421                         if (tolower(buf[0]) == 'e')
422                                 trig = INTR_TRIGGER_EDGE;
423                         else if (tolower(buf[0]) == 'l')
424                                 trig = INTR_TRIGGER_LEVEL;
425                         else
426                                 panic(
427                                 "Invalid trigger %s: must be 'edge' or 'level'",
428                                     buf);
429                         printf("MADT: Forcing SCI to %s trigger\n",
430                             trig == INTR_TRIGGER_EDGE ? "edge" : "level");
431                 }
432                 if (getenv_string("hw.acpi.sci.polarity", buf, sizeof(buf))) {
433                         if (tolower(buf[0]) == 'h')
434                                 pol = INTR_POLARITY_HIGH;
435                         else if (tolower(buf[0]) == 'l')
436                                 pol = INTR_POLARITY_LOW;
437                         else
438                                 panic(
439                                 "Invalid polarity %s: must be 'high' or 'low'",
440                                     buf);
441                         printf("MADT: Forcing SCI to active %s polarity\n",
442                             pol == INTR_POLARITY_HIGH ? "high" : "low");
443                 }
444         }
445
446         /* Remap the IRQ if it is mapped to a different interrupt vector. */
447         if (intr->SourceIrq != intr->GlobalIrq) {
448                 /*
449                  * If the SCI is remapped to a non-ISA global interrupt,
450                  * then override the vector we use to setup and allocate
451                  * the interrupt.
452                  */
453                 if (intr->GlobalIrq > 15 &&
454                     intr->SourceIrq == AcpiGbl_FADT.SciInterrupt)
455                         acpi_OverrideInterruptLevel(intr->GlobalIrq);
456                 else
457                         ioapic_remap_vector(new_ioapic, new_pin,
458                             intr->SourceIrq);
459                 if (madt_find_interrupt(intr->SourceIrq, &old_ioapic,
460                     &old_pin) != 0)
461                         printf("MADT: Could not find APIC for source IRQ %u\n",
462                             intr->SourceIrq);
463                 else if (ioapic_get_vector(old_ioapic, old_pin) ==
464                     intr->SourceIrq)
465                         ioapic_disable_pin(old_ioapic, old_pin);
466         }
467
468         /* Program the polarity and trigger mode. */
469         ioapic_set_triggermode(new_ioapic, new_pin, trig);
470         ioapic_set_polarity(new_ioapic, new_pin, pol);
471 }
472
473 /*
474  * Parse an entry for an NMI routed to an IO APIC.
475  */
476 static void
477 madt_parse_nmi(ACPI_MADT_NMI_SOURCE *nmi)
478 {
479         void *ioapic;
480         u_int pin;
481
482         if (madt_find_interrupt(nmi->GlobalIrq, &ioapic, &pin) != 0) {
483                 printf("MADT: Could not find APIC for vector %u\n",
484                     nmi->GlobalIrq);
485                 return;
486         }
487
488         ioapic_set_nmi(ioapic, pin);
489         if (!(nmi->IntiFlags & ACPI_MADT_TRIGGER_CONFORMS))
490                 ioapic_set_triggermode(ioapic, pin,
491                     interrupt_trigger(nmi->IntiFlags, 0));
492         if (!(nmi->IntiFlags & ACPI_MADT_TRIGGER_CONFORMS))
493                 ioapic_set_polarity(ioapic, pin,
494                     interrupt_polarity(nmi->IntiFlags, 0));
495 }
496
497 /*
498  * Parse an entry for an NMI routed to a local APIC LVT pin.
499  */
500 static void
501 madt_parse_local_nmi(ACPI_MADT_LOCAL_APIC_NMI *nmi)
502 {
503         u_int apic_id, pin;
504
505         if (nmi->ProcessorId == 0xff)
506                 apic_id = APIC_ID_ALL;
507         else if (madt_find_cpu(nmi->ProcessorId, &apic_id) != 0) {
508                 if (bootverbose)
509                         printf("MADT: Ignoring local NMI routed to "
510                             "ACPI CPU %u\n", nmi->ProcessorId);
511                 return;
512         }
513         if (nmi->Lint == 0)
514                 pin = LVT_LINT0;
515         else
516                 pin = LVT_LINT1;
517         lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_NMI);
518         if (!(nmi->IntiFlags & ACPI_MADT_TRIGGER_CONFORMS))
519                 lapic_set_lvt_triggermode(apic_id, pin,
520                     interrupt_trigger(nmi->IntiFlags, 0));
521         if (!(nmi->IntiFlags & ACPI_MADT_POLARITY_CONFORMS))
522                 lapic_set_lvt_polarity(apic_id, pin,
523                     interrupt_polarity(nmi->IntiFlags, 0));
524 }
525
526 /*
527  * Parse interrupt entries.
528  */
529 static void
530 madt_parse_ints(ACPI_SUBTABLE_HEADER *entry, void *arg __unused)
531 {
532
533         switch (entry->Type) {
534         case ACPI_MADT_TYPE_INTERRUPT_OVERRIDE:
535                 madt_parse_interrupt_override(
536                         (ACPI_MADT_INTERRUPT_OVERRIDE *)entry);
537                 break;
538         case ACPI_MADT_TYPE_NMI_SOURCE:
539                 madt_parse_nmi((ACPI_MADT_NMI_SOURCE *)entry);
540                 break;
541         case ACPI_MADT_TYPE_LOCAL_APIC_NMI:
542                 madt_parse_local_nmi((ACPI_MADT_LOCAL_APIC_NMI *)entry);
543                 break;
544         }
545 }
546
547 /*
548  * Setup per-CPU ACPI IDs.
549  */
550 static void
551 madt_set_ids(void *dummy)
552 {
553         struct lapic_info *la;
554         struct pcpu *pc;
555         u_int i;
556
557         if (madt == NULL)
558                 return;
559         CPU_FOREACH(i) {
560                 pc = pcpu_find(i);
561                 KASSERT(pc != NULL, ("no pcpu data for CPU %u", i));
562                 la = &lapics[pc->pc_apic_id];
563                 if (!la->la_enabled)
564                         panic("APIC: CPU with APIC ID %u is not enabled",
565                             pc->pc_apic_id);
566                 pc->pc_acpi_id = la->la_acpi_id;
567                 if (bootverbose)
568                         printf("APIC: CPU %u has ACPI ID %u\n", i,
569                             la->la_acpi_id);
570         }
571 }
572 SYSINIT(madt_set_ids, SI_SUB_CPU, SI_ORDER_ANY, madt_set_ids, NULL);