]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/x86/x86/mptable.c
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r304659, and update
[FreeBSD/FreeBSD.git] / sys / x86 / x86 / mptable.c
1 /*-
2  * Copyright (c) 2003 John Baldwin <jhb@FreeBSD.org>
3  * Copyright (c) 1996, by Steve Passe
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. The name of the developer may NOT be used to endorse or promote products
12  *    derived from this software without specific prior written permission.
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 "opt_mptable_force_htt.h"
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/kernel.h>
35 #include <sys/limits.h>
36 #include <sys/malloc.h>
37 #ifdef NEW_PCIB
38 #include <sys/rman.h>
39 #endif
40
41 #include <vm/vm.h>
42 #include <vm/vm_param.h>
43 #include <vm/pmap.h>
44
45 #include <dev/pci/pcivar.h>
46 #ifdef NEW_PCIB
47 #include <dev/pci/pcib_private.h>
48 #endif
49 #include <x86/apicreg.h>
50 #include <x86/mptable.h>
51 #include <machine/frame.h>
52 #include <machine/intr_machdep.h>
53 #include <x86/apicvar.h>
54 #include <machine/md_var.h>
55 #ifdef NEW_PCIB
56 #include <machine/resource.h>
57 #endif
58 #include <machine/specialreg.h>
59
60 /* string defined by the Intel MP Spec as identifying the MP table */
61 #define MP_SIG                  0x5f504d5f      /* _MP_ */
62
63 #ifdef __amd64__
64 #define MAX_LAPIC_ID            63      /* Max local APIC ID for HTT fixup */
65 #else
66 #define MAX_LAPIC_ID            31      /* Max local APIC ID for HTT fixup */
67 #endif
68
69 #define BIOS_BASE               (0xf0000)
70 #define BIOS_SIZE               (0x10000)
71 #define BIOS_COUNT              (BIOS_SIZE/4)
72
73 typedef void mptable_entry_handler(u_char *entry, void *arg);
74 typedef void mptable_extended_entry_handler(ext_entry_ptr entry, void *arg);
75
76 /* descriptions of MP table entries */
77 typedef struct BASETABLE_ENTRY {
78         uint8_t type;
79         uint8_t length;
80         uint8_t name[16];
81 }       basetable_entry;
82
83 static basetable_entry basetable_entry_types[] =
84 {
85         {0, 20, "Processor"},
86         {1, 8, "Bus"},
87         {2, 8, "I/O APIC"},
88         {3, 8, "I/O INT"},
89         {4, 8, "Local INT"}
90 };
91
92 typedef struct BUSDATA {
93         u_char  bus_id;
94         enum busTypes bus_type;
95 }       bus_datum;
96
97 typedef struct INTDATA {
98         u_char  int_type;
99         u_short int_flags;
100         u_char  src_bus_id;
101         u_char  src_bus_irq;
102         u_char  dst_apic_id;
103         u_char  dst_apic_int;
104         u_char  int_vector;
105 }       io_int, local_int;
106
107 typedef struct BUSTYPENAME {
108         u_char  type;
109         char    name[7];
110 }       bus_type_name;
111
112 /* From MP spec v1.4, table 4-8. */
113 static bus_type_name bus_type_table[] =
114 {
115         {UNKNOWN_BUSTYPE, "CBUS  "},
116         {UNKNOWN_BUSTYPE, "CBUSII"},
117         {EISA, "EISA  "},
118         {UNKNOWN_BUSTYPE, "FUTURE"},
119         {UNKNOWN_BUSTYPE, "INTERN"},
120         {ISA, "ISA   "},
121         {UNKNOWN_BUSTYPE, "MBI   "},
122         {UNKNOWN_BUSTYPE, "MBII  "},
123         {MCA, "MCA   "},
124         {UNKNOWN_BUSTYPE, "MPI   "},
125         {UNKNOWN_BUSTYPE, "MPSA  "},
126         {UNKNOWN_BUSTYPE, "NUBUS "},
127         {PCI, "PCI   "},
128         {UNKNOWN_BUSTYPE, "PCMCIA"},
129         {UNKNOWN_BUSTYPE, "TC    "},
130         {UNKNOWN_BUSTYPE, "VL    "},
131         {UNKNOWN_BUSTYPE, "VME   "},
132         {UNKNOWN_BUSTYPE, "XPRESS"}
133 };
134
135 /* From MP spec v1.4, table 5-1. */
136 static int default_data[7][5] =
137 {
138 /*   nbus, id0, type0, id1, type1 */
139         {1, 0, ISA, 255, NOBUS},
140         {1, 0, EISA, 255, NOBUS},
141         {1, 0, EISA, 255, NOBUS},
142         {1, 0, MCA, 255, NOBUS},
143         {2, 0, ISA, 1, PCI},
144         {2, 0, EISA, 1, PCI},
145         {2, 0, MCA, 1, PCI}
146 };
147
148 struct pci_probe_table_args {
149         u_char bus;
150         u_char found;
151 };
152
153 struct pci_route_interrupt_args {
154         u_char bus;             /* Source bus. */
155         u_char irq;             /* Source slot:pin. */
156         int vector;             /* Return value. */
157 };
158
159 static mpfps_t mpfps;
160 static mpcth_t mpct;
161 static ext_entry_ptr mpet;
162 static void *ioapics[MAX_APIC_ID + 1];
163 static bus_datum *busses;
164 static int mptable_nioapics, mptable_nbusses, mptable_maxbusid;
165 static int pci0 = -1;
166
167 static MALLOC_DEFINE(M_MPTABLE, "mptable", "MP Table Items");
168
169 static enum intr_polarity conforming_polarity(u_char src_bus,
170             u_char src_bus_irq);
171 static enum intr_trigger conforming_trigger(u_char src_bus, u_char src_bus_irq);
172 static enum intr_polarity intentry_polarity(int_entry_ptr intr);
173 static enum intr_trigger intentry_trigger(int_entry_ptr intr);
174 static int      lookup_bus_type(char *name);
175 static void     mptable_count_items(void);
176 static void     mptable_count_items_handler(u_char *entry, void *arg);
177 #ifdef MPTABLE_FORCE_HTT
178 static void     mptable_hyperthread_fixup(u_int id_mask);
179 #endif
180 static void     mptable_parse_apics_and_busses(void);
181 static void     mptable_parse_apics_and_busses_handler(u_char *entry,
182     void *arg);
183 static void     mptable_parse_default_config_ints(void);
184 static void     mptable_parse_ints(void);
185 static void     mptable_parse_ints_handler(u_char *entry, void *arg);
186 static void     mptable_parse_io_int(int_entry_ptr intr);
187 static void     mptable_parse_local_int(int_entry_ptr intr);
188 static void     mptable_pci_probe_table_handler(u_char *entry, void *arg);
189 static void     mptable_pci_route_interrupt_handler(u_char *entry, void *arg);
190 static void     mptable_pci_setup(void);
191 static int      mptable_probe(void);
192 static int      mptable_probe_cpus(void);
193 static void     mptable_probe_cpus_handler(u_char *entry, void *arg __unused);
194 static void     mptable_register(void *dummy);
195 static int      mptable_setup_local(void);
196 static int      mptable_setup_io(void);
197 #ifdef NEW_PCIB
198 static void     mptable_walk_extended_table(
199     mptable_extended_entry_handler *handler, void *arg);
200 #endif
201 static void     mptable_walk_table(mptable_entry_handler *handler, void *arg);
202 static int      search_for_sig(u_int32_t target, int count);
203
204 static struct apic_enumerator mptable_enumerator = {
205         "MPTable",
206         mptable_probe,
207         mptable_probe_cpus,
208         mptable_setup_local,
209         mptable_setup_io
210 };
211
212 /*
213  * look for the MP spec signature
214  */
215
216 static int
217 search_for_sig(u_int32_t target, int count)
218 {
219         int     x;
220         u_int32_t *addr = (u_int32_t *) (KERNBASE + target);
221
222         for (x = 0; x < count; x += 4)
223                 if (addr[x] == MP_SIG)
224                         /* make array index a byte index */
225                         return (target + (x * sizeof(u_int32_t)));
226         return (-1);
227 }
228
229 static int
230 lookup_bus_type(char *name)
231 {
232         int     x;
233
234         for (x = 0; x < MAX_BUSTYPE; ++x)
235                 if (strncmp(bus_type_table[x].name, name, 6) == 0)
236                         return (bus_type_table[x].type);
237
238         return (UNKNOWN_BUSTYPE);
239 }
240
241 /*
242  * Look for an Intel MP spec table (ie, SMP capable hardware).
243  */
244 static int
245 mptable_probe(void)
246 {
247         int     x;
248         u_long  segment;
249         u_int32_t target;
250
251         /* see if EBDA exists */
252         if ((segment = (u_long) * (u_short *) (KERNBASE + 0x40e)) != 0) {
253                 /* search first 1K of EBDA */
254                 target = (u_int32_t) (segment << 4);
255                 if ((x = search_for_sig(target, 1024 / 4)) >= 0)
256                         goto found;
257         } else {
258                 /* last 1K of base memory, effective 'top of base' passed in */
259                 target = (u_int32_t) ((basemem * 1024) - 0x400);
260                 if ((x = search_for_sig(target, 1024 / 4)) >= 0)
261                         goto found;
262         }
263
264         /* search the BIOS */
265         target = (u_int32_t) BIOS_BASE;
266         if ((x = search_for_sig(target, BIOS_COUNT)) >= 0)
267                 goto found;
268
269         /* nothing found */
270         return (ENXIO);
271
272 found:
273         mpfps = (mpfps_t)(KERNBASE + x);
274
275         /* Map in the configuration table if it exists. */
276         if (mpfps->config_type != 0) {
277                 if (bootverbose)
278                         printf(
279                 "MP Table version 1.%d found using Default Configuration %d\n",
280                             mpfps->spec_rev, mpfps->config_type);
281                 if (mpfps->config_type != 5 && mpfps->config_type != 6) {
282                         printf(
283                         "MP Table Default Configuration %d is unsupported\n",
284                             mpfps->config_type);
285                         return (ENXIO);
286                 }
287                 mpct = NULL;
288         } else {
289                 if ((uintptr_t)mpfps->pap >= 1024 * 1024) {
290                         printf("%s: Unable to map MP Configuration Table\n",
291                             __func__);
292                         return (ENXIO);
293                 }
294                 mpct = (mpcth_t)(KERNBASE + (uintptr_t)mpfps->pap);
295                 if (mpct->base_table_length + (uintptr_t)mpfps->pap >=
296                     1024 * 1024) {
297                         printf("%s: Unable to map end of MP Config Table\n",
298                             __func__);
299                         return (ENXIO);
300                 }
301                 if (mpct->extended_table_length != 0 &&
302                     mpct->extended_table_length + mpct->base_table_length +
303                     (uintptr_t)mpfps->pap < 1024 * 1024)
304                         mpet = (ext_entry_ptr)((char *)mpct +
305                             mpct->base_table_length);
306                 if (mpct->signature[0] != 'P' || mpct->signature[1] != 'C' ||
307                     mpct->signature[2] != 'M' || mpct->signature[3] != 'P') {
308                         printf("%s: MP Config Table has bad signature: %c%c%c%c\n",
309                             __func__, mpct->signature[0], mpct->signature[1],
310                             mpct->signature[2], mpct->signature[3]);
311                         return (ENXIO);
312                 }
313                 if (bootverbose)
314                         printf(
315                         "MP Configuration Table version 1.%d found at %p\n",
316                             mpct->spec_rev, mpct);
317         }
318
319         return (-100);
320 }
321
322 /*
323  * Run through the MP table enumerating CPUs.
324  */
325 static int
326 mptable_probe_cpus(void)
327 {
328         u_int cpu_mask;
329
330         /* Is this a pre-defined config? */
331         if (mpfps->config_type != 0) {
332                 lapic_create(0, 1);
333                 lapic_create(1, 0);
334         } else {
335                 cpu_mask = 0;
336                 mptable_walk_table(mptable_probe_cpus_handler, &cpu_mask);
337 #ifdef MPTABLE_FORCE_HTT
338                 mptable_hyperthread_fixup(cpu_mask);
339 #endif
340         }
341         return (0);
342 }
343
344 /*
345  * Initialize the local APIC on the BSP.
346  */
347 static int
348 mptable_setup_local(void)
349 {
350         vm_paddr_t addr;
351
352         /* Is this a pre-defined config? */
353         printf("MPTable: <");
354         if (mpfps->config_type != 0) {
355                 addr = DEFAULT_APIC_BASE;
356                 printf("Default Configuration %d", mpfps->config_type);
357         } else {
358                 addr = mpct->apic_address;
359                 printf("%.*s %.*s", (int)sizeof(mpct->oem_id), mpct->oem_id,
360                     (int)sizeof(mpct->product_id), mpct->product_id);
361         }
362         printf(">\n");
363         lapic_init(addr);
364         return (0);
365 }
366
367 /*
368  * Run through the MP table enumerating I/O APICs.
369  */
370 static int
371 mptable_setup_io(void)
372 {
373         int i;
374         u_char byte;
375
376         /* First, we count individual items and allocate arrays. */
377         mptable_count_items();
378         busses = malloc((mptable_maxbusid + 1) * sizeof(bus_datum), M_MPTABLE,
379             M_WAITOK);
380         for (i = 0; i <= mptable_maxbusid; i++)
381                 busses[i].bus_type = NOBUS;
382
383         /* Second, we run through adding I/O APIC's and buses. */
384         mptable_parse_apics_and_busses();       
385
386         /* Third, we run through the table tweaking interrupt sources. */
387         mptable_parse_ints();
388
389         /* Fourth, we register all the I/O APIC's. */
390         for (i = 0; i <= MAX_APIC_ID; i++)
391                 if (ioapics[i] != NULL)
392                         ioapic_register(ioapics[i]);
393
394         /* Fifth, we setup data structures to handle PCI interrupt routing. */
395         mptable_pci_setup();
396
397         /* Finally, we throw the switch to enable the I/O APIC's. */
398         if (mpfps->mpfb2 & MPFB2_IMCR_PRESENT) {
399                 outb(0x22, 0x70);       /* select IMCR */
400                 byte = inb(0x23);       /* current contents */
401                 byte |= 0x01;           /* mask external INTR */
402                 outb(0x23, byte);       /* disconnect 8259s/NMI */
403         }
404
405         return (0);
406 }
407
408 static void
409 mptable_register(void *dummy __unused)
410 {
411
412         apic_register_enumerator(&mptable_enumerator);
413 }
414 SYSINIT(mptable_register, SI_SUB_TUNABLES - 1, SI_ORDER_FIRST, mptable_register,
415     NULL);
416
417 /*
418  * Call the handler routine for each entry in the MP config base table.
419  */
420 static void
421 mptable_walk_table(mptable_entry_handler *handler, void *arg)
422 {
423         u_int i;
424         u_char *entry;
425
426         entry = (u_char *)(mpct + 1);
427         for (i = 0; i < mpct->entry_count; i++) {
428                 switch (*entry) {
429                 case MPCT_ENTRY_PROCESSOR:
430                 case MPCT_ENTRY_IOAPIC:
431                 case MPCT_ENTRY_BUS:
432                 case MPCT_ENTRY_INT:
433                 case MPCT_ENTRY_LOCAL_INT:
434                         break;
435                 default:
436                         panic("%s: Unknown MP Config Entry %d\n", __func__,
437                             (int)*entry);
438                 }
439                 handler(entry, arg);
440                 entry += basetable_entry_types[*entry].length;
441         }
442 }
443
444 #ifdef NEW_PCIB
445 /*
446  * Call the handler routine for each entry in the MP config extended
447  * table.
448  */
449 static void
450 mptable_walk_extended_table(mptable_extended_entry_handler *handler, void *arg)
451 {
452         ext_entry_ptr end, entry;
453
454         if (mpet == NULL)
455                 return;
456         entry = mpet;
457         end = (ext_entry_ptr)((char *)mpet + mpct->extended_table_length);
458         while (entry < end) {
459                 handler(entry, arg);
460                 entry = (ext_entry_ptr)((char *)entry + entry->length);
461         }
462 }
463 #endif
464
465 static void
466 mptable_probe_cpus_handler(u_char *entry, void *arg)
467 {
468         proc_entry_ptr proc;
469         u_int *cpu_mask;
470
471         switch (*entry) {
472         case MPCT_ENTRY_PROCESSOR:
473                 proc = (proc_entry_ptr)entry;
474                 if (proc->cpu_flags & PROCENTRY_FLAG_EN) {
475                         lapic_create(proc->apic_id, proc->cpu_flags &
476                             PROCENTRY_FLAG_BP);
477                         if (proc->apic_id < MAX_LAPIC_ID) {
478                                 cpu_mask = (u_int *)arg;
479                                 *cpu_mask |= (1ul << proc->apic_id);
480                         }
481                 }
482                 break;
483         }
484 }
485
486 static void
487 mptable_count_items_handler(u_char *entry, void *arg __unused)
488 {
489         io_apic_entry_ptr apic;
490         bus_entry_ptr bus;
491
492         switch (*entry) {
493         case MPCT_ENTRY_BUS:
494                 bus = (bus_entry_ptr)entry;
495                 mptable_nbusses++;
496                 if (bus->bus_id > mptable_maxbusid)
497                         mptable_maxbusid = bus->bus_id;
498                 break;
499         case MPCT_ENTRY_IOAPIC:
500                 apic = (io_apic_entry_ptr)entry;
501                 if (apic->apic_flags & IOAPICENTRY_FLAG_EN)
502                         mptable_nioapics++;
503                 break;
504         }
505 }
506
507 /*
508  * Count items in the table.
509  */
510 static void
511 mptable_count_items(void)
512 {
513
514         /* Is this a pre-defined config? */
515         if (mpfps->config_type != 0) {
516                 mptable_nioapics = 1;
517                 switch (mpfps->config_type) {
518                 case 1:
519                 case 2:
520                 case 3:
521                 case 4:
522                         mptable_nbusses = 1;
523                         break;
524                 case 5:
525                 case 6:
526                 case 7:
527                         mptable_nbusses = 2;
528                         break;
529                 default:
530                         panic("Unknown pre-defined MP Table config type %d",
531                             mpfps->config_type);
532                 }
533                 mptable_maxbusid = mptable_nbusses - 1;
534         } else
535                 mptable_walk_table(mptable_count_items_handler, NULL);
536 }
537
538 /*
539  * Add a bus or I/O APIC from an entry in the table.
540  */
541 static void
542 mptable_parse_apics_and_busses_handler(u_char *entry, void *arg __unused)
543 {
544         io_apic_entry_ptr apic;
545         bus_entry_ptr bus;
546         enum busTypes bus_type;
547         int i;
548
549
550         switch (*entry) {
551         case MPCT_ENTRY_BUS:
552                 bus = (bus_entry_ptr)entry;
553                 bus_type = lookup_bus_type(bus->bus_type);
554                 if (bus_type == UNKNOWN_BUSTYPE) {
555                         printf("MPTable: Unknown bus %d type \"", bus->bus_id);
556                         for (i = 0; i < 6; i++)
557                                 printf("%c", bus->bus_type[i]);
558                         printf("\"\n");
559                 }
560                 busses[bus->bus_id].bus_id = bus->bus_id;
561                 busses[bus->bus_id].bus_type = bus_type;
562                 break;
563         case MPCT_ENTRY_IOAPIC:
564                 apic = (io_apic_entry_ptr)entry;
565                 if (!(apic->apic_flags & IOAPICENTRY_FLAG_EN))
566                         break;
567                 if (apic->apic_id > MAX_APIC_ID)
568                         panic("%s: I/O APIC ID %d too high", __func__,
569                             apic->apic_id);
570                 if (ioapics[apic->apic_id] != NULL)
571                         panic("%s: Double APIC ID %d", __func__,
572                             apic->apic_id);
573                 ioapics[apic->apic_id] = ioapic_create(apic->apic_address,
574                     apic->apic_id, -1);
575                 break;
576         default:
577                 break;
578         }
579 }
580
581 /*
582  * Enumerate I/O APIC's and buses.
583  */
584 static void
585 mptable_parse_apics_and_busses(void)
586 {
587
588         /* Is this a pre-defined config? */
589         if (mpfps->config_type != 0) {
590                 ioapics[2] = ioapic_create(DEFAULT_IO_APIC_BASE, 2, 0);
591                 busses[0].bus_id = 0;
592                 busses[0].bus_type = default_data[mpfps->config_type - 1][2];
593                 if (mptable_nbusses > 1) {
594                         busses[1].bus_id = 1;
595                         busses[1].bus_type =
596                             default_data[mpfps->config_type - 1][4];
597                 }
598         } else
599                 mptable_walk_table(mptable_parse_apics_and_busses_handler,
600                     NULL);
601 }
602
603 /*
604  * Determine conforming polarity for a given bus type.
605  */
606 static enum intr_polarity
607 conforming_polarity(u_char src_bus, u_char src_bus_irq)
608 {
609
610         KASSERT(src_bus <= mptable_maxbusid, ("bus id %d too large", src_bus));
611         switch (busses[src_bus].bus_type) {
612         case ISA:
613         case EISA:
614                 return (INTR_POLARITY_HIGH);
615         case PCI:
616                 return (INTR_POLARITY_LOW);
617         default:
618                 panic("%s: unknown bus type %d", __func__,
619                     busses[src_bus].bus_type);
620         }
621 }
622
623 /*
624  * Determine conforming trigger for a given bus type.
625  */
626 static enum intr_trigger
627 conforming_trigger(u_char src_bus, u_char src_bus_irq)
628 {
629
630         KASSERT(src_bus <= mptable_maxbusid, ("bus id %d too large", src_bus));
631         switch (busses[src_bus].bus_type) {
632         case ISA:
633                 if (elcr_found)
634                         return (elcr_read_trigger(src_bus_irq));
635                 else
636                         return (INTR_TRIGGER_EDGE);
637         case PCI:
638                 return (INTR_TRIGGER_LEVEL);
639
640         case EISA:
641                 KASSERT(src_bus_irq < 16, ("Invalid EISA IRQ %d", src_bus_irq));
642                 KASSERT(elcr_found, ("Missing ELCR"));
643                 return (elcr_read_trigger(src_bus_irq));
644
645         default:
646                 panic("%s: unknown bus type %d", __func__,
647                     busses[src_bus].bus_type);
648         }
649 }
650
651 static enum intr_polarity
652 intentry_polarity(int_entry_ptr intr)
653 {
654
655         switch (intr->int_flags & INTENTRY_FLAGS_POLARITY) {
656         case INTENTRY_FLAGS_POLARITY_CONFORM:
657                 return (conforming_polarity(intr->src_bus_id,
658                             intr->src_bus_irq));
659         case INTENTRY_FLAGS_POLARITY_ACTIVEHI:
660                 return (INTR_POLARITY_HIGH);
661         case INTENTRY_FLAGS_POLARITY_ACTIVELO:
662                 return (INTR_POLARITY_LOW);
663         default:
664                 panic("Bogus interrupt flags");
665         }
666 }
667
668 static enum intr_trigger
669 intentry_trigger(int_entry_ptr intr)
670 {
671
672         switch (intr->int_flags & INTENTRY_FLAGS_TRIGGER) {
673         case INTENTRY_FLAGS_TRIGGER_CONFORM:
674                 return (conforming_trigger(intr->src_bus_id,
675                             intr->src_bus_irq));
676         case INTENTRY_FLAGS_TRIGGER_EDGE:
677                 return (INTR_TRIGGER_EDGE);
678         case INTENTRY_FLAGS_TRIGGER_LEVEL:
679                 return (INTR_TRIGGER_LEVEL);
680         default:
681                 panic("Bogus interrupt flags");
682         }
683 }
684
685 /*
686  * Parse an interrupt entry for an I/O interrupt routed to a pin on an I/O APIC.
687  */
688 static void
689 mptable_parse_io_int(int_entry_ptr intr)
690 {
691         void *ioapic;
692         u_int pin, apic_id;
693
694         apic_id = intr->dst_apic_id;
695         if (intr->dst_apic_id == 0xff) {
696                 /*
697                  * An APIC ID of 0xff means that the interrupt is connected
698                  * to the specified pin on all I/O APICs in the system.  If
699                  * there is only one I/O APIC, then use that APIC to route
700                  * the interrupts.  If there is more than one I/O APIC, then
701                  * punt.
702                  */
703                 if (mptable_nioapics == 1) {
704                         apic_id = 0;
705                         while (ioapics[apic_id] == NULL)
706                                 apic_id++;
707                 } else {
708                         printf(
709                         "MPTable: Ignoring global interrupt entry for pin %d\n",
710                             intr->dst_apic_int);
711                         return;
712                 }
713         }
714         if (apic_id > MAX_APIC_ID) {
715                 printf("MPTable: Ignoring interrupt entry for ioapic%d\n",
716                     intr->dst_apic_id);
717                 return;
718         }
719         ioapic = ioapics[apic_id];
720         if (ioapic == NULL) {
721                 printf(
722         "MPTable: Ignoring interrupt entry for missing ioapic%d\n",
723                     apic_id);
724                 return;
725         }
726         pin = intr->dst_apic_int;
727         switch (intr->int_type) {
728         case INTENTRY_TYPE_INT:
729                 switch (busses[intr->src_bus_id].bus_type) {
730                 case NOBUS:
731                         panic("interrupt from missing bus");
732                 case ISA:
733                 case EISA:
734                         if (busses[intr->src_bus_id].bus_type == ISA)
735                                 ioapic_set_bus(ioapic, pin, APIC_BUS_ISA);
736                         else
737                                 ioapic_set_bus(ioapic, pin, APIC_BUS_EISA);
738                         if (intr->src_bus_irq == pin)
739                                 break;
740                         ioapic_remap_vector(ioapic, pin, intr->src_bus_irq);
741                         if (ioapic_get_vector(ioapic, intr->src_bus_irq) ==
742                             intr->src_bus_irq)
743                                 ioapic_disable_pin(ioapic, intr->src_bus_irq);
744                         break;
745                 case PCI:
746                         ioapic_set_bus(ioapic, pin, APIC_BUS_PCI);
747                         break;
748                 default:
749                         ioapic_set_bus(ioapic, pin, APIC_BUS_UNKNOWN);
750                         break;
751                 }
752                 break;
753         case INTENTRY_TYPE_NMI:
754                 ioapic_set_nmi(ioapic, pin);
755                 break;
756         case INTENTRY_TYPE_SMI:
757                 ioapic_set_smi(ioapic, pin);
758                 break;
759         case INTENTRY_TYPE_EXTINT:
760                 ioapic_set_extint(ioapic, pin);
761                 break;
762         default:
763                 panic("%s: invalid interrupt entry type %d\n", __func__,
764                     intr->int_type);
765         }
766         if (intr->int_type == INTENTRY_TYPE_INT ||
767             (intr->int_flags & INTENTRY_FLAGS_TRIGGER) !=
768             INTENTRY_FLAGS_TRIGGER_CONFORM)
769                 ioapic_set_triggermode(ioapic, pin, intentry_trigger(intr));
770         if (intr->int_type == INTENTRY_TYPE_INT ||
771             (intr->int_flags & INTENTRY_FLAGS_POLARITY) !=
772             INTENTRY_FLAGS_POLARITY_CONFORM)
773                 ioapic_set_polarity(ioapic, pin, intentry_polarity(intr));
774 }
775
776 /*
777  * Parse an interrupt entry for a local APIC LVT pin.
778  */
779 static void
780 mptable_parse_local_int(int_entry_ptr intr)
781 {
782         u_int apic_id, pin;
783
784         if (intr->dst_apic_id == 0xff)
785                 apic_id = APIC_ID_ALL;
786         else
787                 apic_id = intr->dst_apic_id;
788         if (intr->dst_apic_int == 0)
789                 pin = APIC_LVT_LINT0;
790         else
791                 pin = APIC_LVT_LINT1;
792         switch (intr->int_type) {
793         case INTENTRY_TYPE_INT:
794 #if 1
795                 printf(
796         "MPTable: Ignoring vectored local interrupt for LINTIN%d vector %d\n",
797                     intr->dst_apic_int, intr->src_bus_irq);
798                 return;
799 #else
800                 lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_FIXED);
801                 break;
802 #endif
803         case INTENTRY_TYPE_NMI:
804                 lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_NMI);
805                 break;
806         case INTENTRY_TYPE_SMI:
807                 lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_SMI);
808                 break;
809         case INTENTRY_TYPE_EXTINT:
810                 lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_EXTINT);
811                 break;
812         default:
813                 panic("%s: invalid interrupt entry type %d\n", __func__,
814                     intr->int_type);
815         }
816         if ((intr->int_flags & INTENTRY_FLAGS_TRIGGER) !=
817             INTENTRY_FLAGS_TRIGGER_CONFORM)
818                 lapic_set_lvt_triggermode(apic_id, pin,
819                     intentry_trigger(intr));
820         if ((intr->int_flags & INTENTRY_FLAGS_POLARITY) !=
821             INTENTRY_FLAGS_POLARITY_CONFORM)
822                 lapic_set_lvt_polarity(apic_id, pin, intentry_polarity(intr));
823 }
824
825 /*
826  * Parse interrupt entries.
827  */
828 static void
829 mptable_parse_ints_handler(u_char *entry, void *arg __unused)
830 {
831         int_entry_ptr intr;
832
833         intr = (int_entry_ptr)entry;
834         switch (*entry) {
835         case MPCT_ENTRY_INT:
836                 mptable_parse_io_int(intr);
837                 break;
838         case MPCT_ENTRY_LOCAL_INT:
839                 mptable_parse_local_int(intr);
840                 break;
841         }
842 }
843
844 /*
845  * Configure interrupt pins for a default configuration.  For details see
846  * Table 5-2 in Section 5 of the MP Table specification.
847  */
848 static void
849 mptable_parse_default_config_ints(void)
850 {
851         struct INTENTRY entry;
852         int pin;
853
854         /*
855          * All default configs route IRQs from bus 0 to the first 16 pins
856          * of the first I/O APIC with an APIC ID of 2.
857          */
858         entry.type = MPCT_ENTRY_INT;
859         entry.int_flags = INTENTRY_FLAGS_POLARITY_CONFORM |
860             INTENTRY_FLAGS_TRIGGER_CONFORM;
861         entry.src_bus_id = 0;
862         entry.dst_apic_id = 2;
863
864         /* Run through all 16 pins. */
865         for (pin = 0; pin < 16; pin++) {
866                 entry.dst_apic_int = pin;
867                 switch (pin) {
868                 case 0:
869                         /* Pin 0 is an ExtINT pin. */
870                         entry.int_type = INTENTRY_TYPE_EXTINT;
871                         break;
872                 case 2:
873                         /* IRQ 0 is routed to pin 2. */
874                         entry.int_type = INTENTRY_TYPE_INT;
875                         entry.src_bus_irq = 0;
876                         break;
877                 default:
878                         /* All other pins are identity mapped. */
879                         entry.int_type = INTENTRY_TYPE_INT;
880                         entry.src_bus_irq = pin;
881                         break;
882                 }
883                 mptable_parse_io_int(&entry);
884         }
885
886         /* Certain configs disable certain pins. */
887         if (mpfps->config_type == 7)
888                 ioapic_disable_pin(ioapics[2], 0);
889         if (mpfps->config_type == 2) {
890                 ioapic_disable_pin(ioapics[2], 2);
891                 ioapic_disable_pin(ioapics[2], 13);
892         }
893 }
894
895 /*
896  * Configure the interrupt pins
897  */
898 static void
899 mptable_parse_ints(void)
900 {
901
902         /* Is this a pre-defined config? */
903         if (mpfps->config_type != 0) {
904                 /* Configure LINT pins. */
905                 lapic_set_lvt_mode(APIC_ID_ALL, APIC_LVT_LINT0,
906                     APIC_LVT_DM_EXTINT);
907                 lapic_set_lvt_mode(APIC_ID_ALL, APIC_LVT_LINT1, APIC_LVT_DM_NMI);
908
909                 /* Configure I/O APIC pins. */
910                 mptable_parse_default_config_ints();
911         } else
912                 mptable_walk_table(mptable_parse_ints_handler, NULL);
913 }
914
915 #ifdef MPTABLE_FORCE_HTT
916 /*
917  * Perform a hyperthreading "fix-up" to enumerate any logical CPU's
918  * that aren't already listed in the table.
919  *
920  * XXX: We assume that all of the physical CPUs in the
921  * system have the same number of logical CPUs.
922  *
923  * XXX: We assume that APIC ID's are allocated such that
924  * the APIC ID's for a physical processor are aligned
925  * with the number of logical CPU's in the processor.
926  */
927 static void
928 mptable_hyperthread_fixup(u_int id_mask)
929 {
930         u_int i, id, logical_cpus;
931
932         /* Nothing to do if there is no HTT support. */
933         if ((cpu_feature & CPUID_HTT) == 0)
934                 return;
935         logical_cpus = (cpu_procinfo & CPUID_HTT_CORES) >> 16;
936         if (logical_cpus <= 1)
937                 return;
938
939         /*
940          * For each APIC ID of a CPU that is set in the mask,
941          * scan the other candidate APIC ID's for this
942          * physical processor.  If any of those ID's are
943          * already in the table, then kill the fixup.
944          */
945         for (id = 0; id <= MAX_LAPIC_ID; id++) {
946                 if ((id_mask & 1 << id) == 0)
947                         continue;
948                 /* First, make sure we are on a logical_cpus boundary. */
949                 if (id % logical_cpus != 0)
950                         return;
951                 for (i = id + 1; i < id + logical_cpus; i++)
952                         if ((id_mask & 1 << i) != 0)
953                                 return;
954         }
955
956         /*
957          * Ok, the ID's checked out, so perform the fixup by
958          * adding the logical CPUs.
959          */
960         while ((id = ffs(id_mask)) != 0) {
961                 id--;
962                 for (i = id + 1; i < id + logical_cpus; i++) {
963                         if (bootverbose)
964                                 printf(
965                         "MPTable: Adding logical CPU %d from main CPU %d\n",
966                                     i, id);
967                         lapic_create(i, 0);
968                 }
969                 id_mask &= ~(1 << id);
970         }
971 }
972 #endif /* MPTABLE_FORCE_HTT */
973
974 /*
975  * Support code for routing PCI interrupts using the MP Table.
976  */
977 static void
978 mptable_pci_setup(void)
979 {
980         int i;
981
982         /*
983          * Find the first pci bus and call it 0.  Panic if pci0 is not
984          * bus zero and there are multiple PCI buses.
985          */
986         for (i = 0; i <= mptable_maxbusid; i++)
987                 if (busses[i].bus_type == PCI) {
988                         if (pci0 == -1)
989                                 pci0 = i;
990                         else if (pci0 != 0)
991                                 panic(
992                 "MPTable contains multiple PCI buses but no PCI bus 0");
993                 }
994 }
995
996 static void
997 mptable_pci_probe_table_handler(u_char *entry, void *arg)
998 {
999         struct pci_probe_table_args *args;
1000         int_entry_ptr intr;
1001
1002         if (*entry != MPCT_ENTRY_INT)
1003                 return;
1004         intr = (int_entry_ptr)entry;
1005         args = (struct pci_probe_table_args *)arg;
1006         KASSERT(args->bus <= mptable_maxbusid,
1007             ("bus %d is too big", args->bus));
1008         KASSERT(busses[args->bus].bus_type == PCI, ("probing for non-PCI bus"));
1009         if (intr->src_bus_id == args->bus)
1010                 args->found = 1;
1011 }
1012
1013 int
1014 mptable_pci_probe_table(int bus)
1015 {
1016         struct pci_probe_table_args args;
1017
1018         if (bus < 0)
1019                 return (EINVAL);
1020         if (mpct == NULL || pci0 == -1 || pci0 + bus > mptable_maxbusid)
1021                 return (ENXIO);
1022         if (busses[pci0 + bus].bus_type != PCI)
1023                 return (ENXIO);
1024         args.bus = pci0 + bus;
1025         args.found = 0;
1026         mptable_walk_table(mptable_pci_probe_table_handler, &args);
1027         if (args.found == 0)
1028                 return (ENXIO);
1029         return (0);
1030 }
1031
1032 static void
1033 mptable_pci_route_interrupt_handler(u_char *entry, void *arg)
1034 {
1035         struct pci_route_interrupt_args *args;
1036         int_entry_ptr intr;
1037         int vector;
1038
1039         if (*entry != MPCT_ENTRY_INT)
1040                 return;
1041         intr = (int_entry_ptr)entry;
1042         args = (struct pci_route_interrupt_args *)arg;
1043         if (intr->src_bus_id != args->bus || intr->src_bus_irq != args->irq)
1044                 return;
1045
1046         /* Make sure the APIC maps to a known APIC. */
1047         KASSERT(ioapics[intr->dst_apic_id] != NULL,
1048             ("No I/O APIC %d to route interrupt to", intr->dst_apic_id));
1049
1050         /*
1051          * Look up the vector for this APIC / pin combination.  If we
1052          * have previously matched an entry for this PCI IRQ but it
1053          * has the same vector as this entry, just return.  Otherwise,
1054          * we use the vector for this APIC / pin combination.
1055          */
1056         vector = ioapic_get_vector(ioapics[intr->dst_apic_id],
1057             intr->dst_apic_int);
1058         if (args->vector == vector)
1059                 return;
1060         KASSERT(args->vector == -1,
1061             ("Multiple IRQs for PCI interrupt %d.%d.INT%c: %d and %d\n",
1062             args->bus, args->irq >> 2, 'A' + (args->irq & 0x3), args->vector,
1063             vector));
1064         args->vector = vector;
1065 }
1066
1067 int
1068 mptable_pci_route_interrupt(device_t pcib, device_t dev, int pin)
1069 {
1070         struct pci_route_interrupt_args args;
1071         int slot;
1072
1073         /* Like ACPI, pin numbers are 0-3, not 1-4. */
1074         pin--;
1075         KASSERT(pci0 != -1, ("do not know how to route PCI interrupts"));
1076         args.bus = pci_get_bus(dev) + pci0;
1077         slot = pci_get_slot(dev);
1078
1079         /*
1080          * PCI interrupt entries in the MP Table encode both the slot and
1081          * pin into the IRQ with the pin being the two least significant
1082          * bits, the slot being the next five bits, and the most significant
1083          * bit being reserved.
1084          */
1085         args.irq = slot << 2 | pin;
1086         args.vector = -1;
1087         mptable_walk_table(mptable_pci_route_interrupt_handler, &args);
1088         if (args.vector < 0) {
1089                 device_printf(pcib, "unable to route slot %d INT%c\n", slot,
1090                     'A' + pin);
1091                 return (PCI_INVALID_IRQ);
1092         }
1093         if (bootverbose)
1094                 device_printf(pcib, "slot %d INT%c routed to irq %d\n", slot,
1095                     'A' + pin, args.vector);
1096         return (args.vector);
1097 }
1098
1099 #ifdef NEW_PCIB
1100 struct host_res_args {
1101         struct mptable_hostb_softc *sc;
1102         device_t dev;
1103         u_char  bus;
1104 };
1105
1106 /*
1107  * Initialize a Host-PCI bridge so it can restrict resource allocation
1108  * requests to the resources it actually decodes according to MP
1109  * config table extended entries.
1110  */
1111 static void
1112 mptable_host_res_handler(ext_entry_ptr entry, void *arg)
1113 {
1114         struct host_res_args *args;
1115         cbasm_entry_ptr cbasm;
1116         sas_entry_ptr sas;
1117         const char *name;
1118         uint64_t start, end;
1119         int error, *flagp, flags, type;
1120
1121         args = arg;
1122         switch (entry->type) {
1123         case MPCT_EXTENTRY_SAS:
1124                 sas = (sas_entry_ptr)entry;
1125                 if (sas->bus_id != args->bus)
1126                         break;
1127                 switch (sas->address_type) {
1128                 case SASENTRY_TYPE_IO:
1129                         type = SYS_RES_IOPORT;
1130                         flags = 0;
1131                         break;
1132                 case SASENTRY_TYPE_MEMORY:
1133                         type = SYS_RES_MEMORY;
1134                         flags = 0;
1135                         break;
1136                 case SASENTRY_TYPE_PREFETCH:
1137                         type = SYS_RES_MEMORY;
1138                         flags = RF_PREFETCHABLE;
1139                         break;
1140                 default:
1141                         printf(
1142             "MPTable: Unknown systems address space type for bus %u: %d\n",
1143                             sas->bus_id, sas->address_type);
1144                         return;
1145                 }
1146                 start = sas->address_base;
1147                 end = sas->address_base + sas->address_length - 1;
1148 #ifdef __i386__
1149                 if (start > ULONG_MAX) {
1150                         device_printf(args->dev,
1151                             "Ignoring %d range above 4GB (%#jx-%#jx)\n",
1152                             type, (uintmax_t)start, (uintmax_t)end);
1153                         break;
1154                 }
1155                 if (end > ULONG_MAX) {
1156                         device_printf(args->dev,
1157                     "Truncating end of %d range above 4GB (%#jx-%#jx)\n",
1158                             type, (uintmax_t)start, (uintmax_t)end);
1159                         end = ULONG_MAX;
1160                 }
1161 #endif
1162                 error = pcib_host_res_decodes(&args->sc->sc_host_res, type,
1163                     start, end, flags);
1164                 if (error)
1165                         panic("Failed to manage %d range (%#jx-%#jx): %d",
1166                             type, (uintmax_t)start, (uintmax_t)end, error);
1167                 break;
1168         case MPCT_EXTENTRY_CBASM:
1169                 cbasm = (cbasm_entry_ptr)entry;
1170                 if (cbasm->bus_id != args->bus)
1171                         break;
1172                 switch (cbasm->predefined_range) {
1173                 case CBASMENTRY_RANGE_ISA_IO:
1174                         flagp = &args->sc->sc_decodes_isa_io;
1175                         name = "ISA I/O";
1176                         break;
1177                 case CBASMENTRY_RANGE_VGA_IO:
1178                         flagp = &args->sc->sc_decodes_vga_io;
1179                         name = "VGA I/O";
1180                         break;
1181                 default:
1182                         printf(
1183     "MPTable: Unknown compatiblity address space range for bus %u: %d\n",
1184                             cbasm->bus_id, cbasm->predefined_range);
1185                         return;
1186                 }
1187                 if (*flagp != 0)
1188                         printf(
1189                     "MPTable: Duplicate compatibility %s range for bus %u\n",
1190                             name, cbasm->bus_id);
1191                 switch (cbasm->address_mod) {
1192                 case CBASMENTRY_ADDRESS_MOD_ADD:
1193                         *flagp = 1;
1194                         if (bootverbose)
1195                                 device_printf(args->dev, "decoding %s ports\n",
1196                                     name);
1197                         break;
1198                 case CBASMENTRY_ADDRESS_MOD_SUBTRACT:
1199                         *flagp = -1;
1200                         if (bootverbose)
1201                                 device_printf(args->dev,
1202                                     "not decoding %s ports\n", name);
1203                         break;
1204                 default:
1205                         printf(
1206             "MPTable: Unknown compatibility address space modifier: %u\n",
1207                             cbasm->address_mod);
1208                         break;
1209                 }
1210                 break;
1211         }
1212 }
1213
1214 void
1215 mptable_pci_host_res_init(device_t pcib)
1216 {
1217         struct host_res_args args;
1218
1219         KASSERT(pci0 != -1, ("do not know how to map PCI bus IDs"));
1220         args.bus = pci_get_bus(pcib) + pci0;
1221         args.dev = pcib;
1222         args.sc = device_get_softc(pcib);
1223         if (pcib_host_res_init(pcib, &args.sc->sc_host_res) != 0)
1224                 panic("failed to init hostb resources");
1225         mptable_walk_extended_table(mptable_host_res_handler, &args);
1226 }
1227 #endif