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