106ae9f44f109bf1b6a8ee5dcf479801c991e9c2
[dragonfly.git] / sys / platform / pc32 / i386 / mp_machdep.c
1 /*
2  * Copyright (c) 1996, by Steve Passe
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. The name of the developer may NOT be used to endorse or promote products
11  *    derived from this software without specific prior written permission.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD: src/sys/i386/i386/mp_machdep.c,v 1.115.2.15 2003/03/14 21:22:35 jhb Exp $
26  * $DragonFly: src/sys/platform/pc32/i386/mp_machdep.c,v 1.60 2008/06/07 12:03:52 mneumann Exp $
27  */
28
29 #include "opt_cpu.h"
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/sysctl.h>
35 #include <sys/malloc.h>
36 #include <sys/memrange.h>
37 #include <sys/cons.h>   /* cngetc() */
38 #include <sys/machintr.h>
39
40 #include <vm/vm.h>
41 #include <vm/vm_param.h>
42 #include <vm/pmap.h>
43 #include <vm/vm_kern.h>
44 #include <vm/vm_extern.h>
45 #include <sys/lock.h>
46 #include <vm/vm_map.h>
47 #include <sys/user.h>
48 #ifdef GPROF 
49 #include <sys/gmon.h>
50 #endif
51
52 #include <machine/smp.h>
53 #include <machine_base/apic/apicreg.h>
54 #include <machine/atomic.h>
55 #include <machine/cpufunc.h>
56 #include <machine_base/apic/mpapic.h>
57 #include <machine/psl.h>
58 #include <machine/segments.h>
59 #include <machine/tss.h>
60 #include <machine/specialreg.h>
61 #include <machine/globaldata.h>
62
63 #include <machine/md_var.h>             /* setidt() */
64 #include <machine_base/icu/icu.h>               /* IPIs */
65 #include <machine_base/isa/intr_machdep.h>      /* IPIs */
66
67 #define FIXUP_EXTRA_APIC_INTS   8       /* additional entries we may create */
68
69 #define WARMBOOT_TARGET         0
70 #define WARMBOOT_OFF            (KERNBASE + 0x0467)
71 #define WARMBOOT_SEG            (KERNBASE + 0x0469)
72
73 #define BIOS_BASE               (0xf0000)
74 #define BIOS_SIZE               (0x10000)
75 #define BIOS_COUNT              (BIOS_SIZE/4)
76
77 #define CMOS_REG                (0x70)
78 #define CMOS_DATA               (0x71)
79 #define BIOS_RESET              (0x0f)
80 #define BIOS_WARM               (0x0a)
81
82 #define PROCENTRY_FLAG_EN       0x01
83 #define PROCENTRY_FLAG_BP       0x02
84 #define IOAPICENTRY_FLAG_EN     0x01
85
86
87 /* MP Floating Pointer Structure */
88 typedef struct MPFPS {
89         char    signature[4];
90         u_int32_t pap;
91         u_char  length;
92         u_char  spec_rev;
93         u_char  checksum;
94         u_char  mpfb1;
95         u_char  mpfb2;
96         u_char  mpfb3;
97         u_char  mpfb4;
98         u_char  mpfb5;
99 }      *mpfps_t;
100
101 /* MP Configuration Table Header */
102 typedef struct MPCTH {
103         char    signature[4];
104         u_short base_table_length;
105         u_char  spec_rev;
106         u_char  checksum;
107         u_char  oem_id[8];
108         u_char  product_id[12];
109         void   *oem_table_pointer;
110         u_short oem_table_size;
111         u_short entry_count;
112         void   *apic_address;
113         u_short extended_table_length;
114         u_char  extended_table_checksum;
115         u_char  reserved;
116 }      *mpcth_t;
117
118
119 typedef struct PROCENTRY {
120         u_char  type;
121         u_char  apic_id;
122         u_char  apic_version;
123         u_char  cpu_flags;
124         u_long  cpu_signature;
125         u_long  feature_flags;
126         u_long  reserved1;
127         u_long  reserved2;
128 }      *proc_entry_ptr;
129
130 typedef struct BUSENTRY {
131         u_char  type;
132         u_char  bus_id;
133         char    bus_type[6];
134 }      *bus_entry_ptr;
135
136 typedef struct IOAPICENTRY {
137         u_char  type;
138         u_char  apic_id;
139         u_char  apic_version;
140         u_char  apic_flags;
141         void   *apic_address;
142 }      *io_apic_entry_ptr;
143
144 typedef struct INTENTRY {
145         u_char  type;
146         u_char  int_type;
147         u_short int_flags;
148         u_char  src_bus_id;
149         u_char  src_bus_irq;
150         u_char  dst_apic_id;
151         u_char  dst_apic_int;
152 }      *int_entry_ptr;
153
154 /* descriptions of MP basetable entries */
155 typedef struct BASETABLE_ENTRY {
156         u_char  type;
157         u_char  length;
158         char    name[16];
159 }       basetable_entry;
160
161 struct mptable_pos {
162         mpfps_t         mp_fps;
163         mpcth_t         mp_cth;
164         vm_size_t       mp_cth_mapsz;
165 };
166
167 /*
168  * this code MUST be enabled here and in mpboot.s.
169  * it follows the very early stages of AP boot by placing values in CMOS ram.
170  * it NORMALLY will never be needed and thus the primitive method for enabling.
171  *
172  */
173 #if defined(CHECK_POINTS)
174 #define CHECK_READ(A)    (outb(CMOS_REG, (A)), inb(CMOS_DATA))
175 #define CHECK_WRITE(A,D) (outb(CMOS_REG, (A)), outb(CMOS_DATA, (D)))
176
177 #define CHECK_INIT(D);                          \
178         CHECK_WRITE(0x34, (D));                 \
179         CHECK_WRITE(0x35, (D));                 \
180         CHECK_WRITE(0x36, (D));                 \
181         CHECK_WRITE(0x37, (D));                 \
182         CHECK_WRITE(0x38, (D));                 \
183         CHECK_WRITE(0x39, (D));
184
185 #define CHECK_PRINT(S);                         \
186         kprintf("%s: %d, %d, %d, %d, %d, %d\n", \
187            (S),                                 \
188            CHECK_READ(0x34),                    \
189            CHECK_READ(0x35),                    \
190            CHECK_READ(0x36),                    \
191            CHECK_READ(0x37),                    \
192            CHECK_READ(0x38),                    \
193            CHECK_READ(0x39));
194
195 #else                           /* CHECK_POINTS */
196
197 #define CHECK_INIT(D)
198 #define CHECK_PRINT(S)
199
200 #endif                          /* CHECK_POINTS */
201
202 /*
203  * Values to send to the POST hardware.
204  */
205 #define MP_BOOTADDRESS_POST     0x10
206 #define MP_PROBE_POST           0x11
207 #define MPTABLE_PASS1_POST      0x12
208
209 #define MP_START_POST           0x13
210 #define MP_ENABLE_POST          0x14
211 #define MPTABLE_PASS2_POST      0x15
212
213 #define START_ALL_APS_POST      0x16
214 #define INSTALL_AP_TRAMP_POST   0x17
215 #define START_AP_POST           0x18
216
217 #define MP_ANNOUNCE_POST        0x19
218
219 static int need_hyperthreading_fixup;
220 static u_int logical_cpus;
221 u_int   logical_cpus_mask;
222
223 /** XXX FIXME: where does this really belong, isa.h/isa.c perhaps? */
224 int     current_postcode;
225
226 /** XXX FIXME: what system files declare these??? */
227 extern struct region_descriptor r_gdt, r_idt;
228
229 int     bsp_apic_ready = 0;     /* flags useability of BSP apic */
230 int     mp_naps;                /* # of Applications processors */
231 int     mp_nbusses;             /* # of busses */
232 #ifdef APIC_IO
233 int     mp_napics;              /* # of IO APICs */
234 #endif
235 vm_offset_t cpu_apic_address;
236 #ifdef APIC_IO
237 vm_offset_t io_apic_address[NAPICID];   /* NAPICID is more than enough */
238 u_int32_t *io_apic_versions;
239 #endif
240 extern  int nkpt;
241
242 u_int32_t cpu_apic_versions[MAXCPU];
243 int64_t tsc0_offset;
244 extern int64_t tsc_offsets[];
245
246 extern u_long ebda_addr;
247
248 #ifdef APIC_IO
249 struct apic_intmapinfo  int_to_apicintpin[APIC_INTMAPSIZE];
250 #endif
251
252 /*
253  * APIC ID logical/physical mapping structures.
254  * We oversize these to simplify boot-time config.
255  */
256 int     cpu_num_to_apic_id[NAPICID];
257 #ifdef APIC_IO
258 int     io_num_to_apic_id[NAPICID];
259 #endif
260 int     apic_id_to_logical[NAPICID];
261
262 /* AP uses this during bootstrap.  Do not staticize.  */
263 char *bootSTK;
264 static int bootAP;
265
266 /* Hotwire a 0->4MB V==P mapping */
267 extern pt_entry_t *KPTphys;
268
269 /*
270  * SMP page table page.  Setup by locore to point to a page table
271  * page from which we allocate per-cpu privatespace areas io_apics,
272  * and so forth.
273  */
274
275 #define IO_MAPPING_START_INDEX  \
276                 (SMP_MAXCPU * sizeof(struct privatespace) / PAGE_SIZE)
277
278 extern pt_entry_t *SMPpt;
279 static int SMPpt_alloc_index = IO_MAPPING_START_INDEX;
280
281 struct pcb stoppcbs[MAXCPU];
282
283 /*
284  * Local data and functions.
285  */
286
287 static u_int    boot_address;
288 static u_int    base_memory;
289 static int      mp_finish;
290
291 static void     mp_enable(u_int boot_addr);
292
293 static int      mptable_probe(void);
294 static int      mptable_search_sig(u_int32_t target, int count);
295 static void     mptable_hyperthread_fixup(u_int id_mask);
296 static void     mptable_pass1(struct mptable_pos *);
297 static int      mptable_pass2(struct mptable_pos *);
298 static void     mptable_default(int type);
299 static void     mptable_fix(void);
300 static void     mptable_map(struct mptable_pos *, vm_paddr_t);
301 static void     mptable_unmap(struct mptable_pos *);
302
303 #ifdef APIC_IO
304 static void     setup_apic_irq_mapping(void);
305 static int      apic_int_is_bus_type(int intr, int bus_type);
306 #endif
307 static int      start_all_aps(u_int boot_addr);
308 static void     install_ap_tramp(u_int boot_addr);
309 static int      start_ap(struct mdglobaldata *gd, u_int boot_addr);
310
311 static cpumask_t smp_startup_mask = 1;  /* which cpus have been started */
312 cpumask_t smp_active_mask = 1;  /* which cpus are ready for IPIs etc? */
313 SYSCTL_INT(_machdep, OID_AUTO, smp_active, CTLFLAG_RD, &smp_active_mask, 0, "");
314
315 /*
316  * Calculate usable address in base memory for AP trampoline code.
317  */
318 u_int
319 mp_bootaddress(u_int basemem)
320 {
321         POSTCODE(MP_BOOTADDRESS_POST);
322
323         base_memory = basemem;
324
325         boot_address = base_memory & ~0xfff;    /* round down to 4k boundary */
326         if ((base_memory - boot_address) < bootMP_size)
327                 boot_address -= 4096;   /* not enough, lower by 4k */
328
329         return boot_address;
330 }
331
332
333 /*
334  * Look for an Intel MP spec table (ie, SMP capable hardware).
335  */
336 static int
337 mptable_probe(void)
338 {
339         int     x;
340         u_int32_t target;
341  
342         /*
343          * Make sure our SMPpt[] page table is big enough to hold all the
344          * mappings we need.
345          */
346         KKASSERT(IO_MAPPING_START_INDEX < NPTEPG - 2);
347
348         POSTCODE(MP_PROBE_POST);
349
350         /* see if EBDA exists */
351         if (ebda_addr != 0) {
352                 /* search first 1K of EBDA */
353                 target = (u_int32_t)ebda_addr;
354                 if ((x = mptable_search_sig(target, 1024 / 4)) > 0)
355                         return x;
356         } else {
357                 /* last 1K of base memory, effective 'top of base' passed in */
358                 target = (u_int32_t)(base_memory - 0x400);
359                 if ((x = mptable_search_sig(target, 1024 / 4)) > 0)
360                         return x;
361         }
362
363         /* search the BIOS */
364         target = (u_int32_t)BIOS_BASE;
365         if ((x = mptable_search_sig(target, BIOS_COUNT)) > 0)
366                 return x;
367
368         /* nothing found */
369         return 0;
370 }
371
372
373 /*
374  * Startup the SMP processors.
375  */
376 void
377 mp_start(void)
378 {
379         POSTCODE(MP_START_POST);
380         mp_enable(boot_address);
381 }
382
383
384 /*
385  * Print various information about the SMP system hardware and setup.
386  */
387 void
388 mp_announce(void)
389 {
390         int     x;
391
392         POSTCODE(MP_ANNOUNCE_POST);
393
394         kprintf("DragonFly/MP: Multiprocessor motherboard\n");
395         kprintf(" cpu0 (BSP): apic id: %2d", CPU_TO_ID(0));
396         kprintf(", version: 0x%08x", cpu_apic_versions[0]);
397         kprintf(", at 0x%08x\n", cpu_apic_address);
398         for (x = 1; x <= mp_naps; ++x) {
399                 kprintf(" cpu%d (AP):  apic id: %2d", x, CPU_TO_ID(x));
400                 kprintf(", version: 0x%08x", cpu_apic_versions[x]);
401                 kprintf(", at 0x%08x\n", cpu_apic_address);
402         }
403
404 #if defined(APIC_IO)
405         for (x = 0; x < mp_napics; ++x) {
406                 kprintf(" io%d (APIC): apic id: %2d", x, IO_TO_ID(x));
407                 kprintf(", version: 0x%08x", io_apic_versions[x]);
408                 kprintf(", at 0x%08x\n", io_apic_address[x]);
409         }
410 #else
411         kprintf(" Warning: APIC I/O disabled\n");
412 #endif  /* APIC_IO */
413 }
414
415 /*
416  * AP cpu's call this to sync up protected mode.
417  *
418  * WARNING!  We must ensure that the cpu is sufficiently initialized to
419  * be able to use to the FP for our optimized bzero/bcopy code before
420  * we enter more mainstream C code.
421  *
422  * WARNING! %fs is not set up on entry.  This routine sets up %fs.
423  */
424 void
425 init_secondary(void)
426 {
427         int     gsel_tss;
428         int     x, myid = bootAP;
429         u_int   cr0;
430         struct mdglobaldata *md;
431         struct privatespace *ps;
432
433         ps = &CPU_prvspace[myid];
434
435         gdt_segs[GPRIV_SEL].ssd_base = (int)ps;
436         gdt_segs[GPROC0_SEL].ssd_base =
437                 (int) &ps->mdglobaldata.gd_common_tss;
438         ps->mdglobaldata.mi.gd_prvspace = ps;
439
440         for (x = 0; x < NGDT; x++) {
441                 ssdtosd(&gdt_segs[x], &gdt[myid * NGDT + x].sd);
442         }
443
444         r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
445         r_gdt.rd_base = (int) &gdt[myid * NGDT];
446         lgdt(&r_gdt);                   /* does magic intra-segment return */
447
448         lidt(&r_idt);
449
450         lldt(_default_ldt);
451         mdcpu->gd_currentldt = _default_ldt;
452
453         gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
454         gdt[myid * NGDT + GPROC0_SEL].sd.sd_type = SDT_SYS386TSS;
455
456         md = mdcpu;     /* loaded through %fs:0 (mdglobaldata.mi.gd_prvspace)*/
457
458         md->gd_common_tss.tss_esp0 = 0; /* not used until after switch */
459         md->gd_common_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
460         md->gd_common_tss.tss_ioopt = (sizeof md->gd_common_tss) << 16;
461         md->gd_tss_gdt = &gdt[myid * NGDT + GPROC0_SEL].sd;
462         md->gd_common_tssd = *md->gd_tss_gdt;
463         ltr(gsel_tss);
464
465         /*
466          * Set to a known state:
467          * Set by mpboot.s: CR0_PG, CR0_PE
468          * Set by cpu_setregs: CR0_NE, CR0_MP, CR0_TS, CR0_WP, CR0_AM
469          */
470         cr0 = rcr0();
471         cr0 &= ~(CR0_CD | CR0_NW | CR0_EM);
472         load_cr0(cr0);
473         pmap_set_opt();         /* PSE/4MB pages, etc */
474
475         /* set up CPU registers and state */
476         cpu_setregs();
477
478         /* set up FPU state on the AP */
479         npxinit(__INITIAL_NPXCW__);
480
481         /* set up SSE registers */
482         enable_sse();
483 }
484
485 /*******************************************************************
486  * local functions and data
487  */
488
489 /*
490  * start the SMP system
491  */
492 static void
493 mp_enable(u_int boot_addr)
494 {
495         int     x;
496 #if defined(APIC_IO)
497         int     apic;
498         u_int   ux;
499 #endif  /* APIC_IO */
500         vm_paddr_t mpfps_paddr;
501         struct mptable_pos mpt;
502
503         POSTCODE(MP_ENABLE_POST);
504
505 #if 0
506         madt_probe();
507 #endif
508
509         mpfps_paddr = mptable_probe();
510         if (mpfps_paddr == 0)
511                 panic("mp_enable: mptable_probe failed\n");
512
513         mptable_map(&mpt, mpfps_paddr);
514
515         /*
516          * We can safely map physical memory into SMPpt after
517          * mptable_pass1() completes.
518          */
519         mptable_pass1(&mpt);
520
521         if (cpu_apic_address == 0)
522                 panic("mp_enable: no local apic!\n");
523
524         /* examine the MP table for needed info, uses physical addresses */
525         x = mptable_pass2(&mpt);
526
527         mptable_unmap(&mpt);
528
529         /* local apic is mapped on last page */
530         SMPpt[NPTEPG - 1] = (pt_entry_t)(PG_V | PG_RW | PG_N |
531             pmap_get_pgeflag() | (cpu_apic_address & PG_FRAME));
532
533         /* can't process default configs till the CPU APIC is pmapped */
534         if (x)
535                 mptable_default(x);
536
537         /* post scan cleanup */
538         mptable_fix();
539
540 #if defined(APIC_IO)
541
542         setup_apic_irq_mapping();
543
544         /* fill the LOGICAL io_apic_versions table */
545         for (apic = 0; apic < mp_napics; ++apic) {
546                 ux = io_apic_read(apic, IOAPIC_VER);
547                 io_apic_versions[apic] = ux;
548                 io_apic_set_id(apic, IO_TO_ID(apic));
549         }
550
551         /* program each IO APIC in the system */
552         for (apic = 0; apic < mp_napics; ++apic)
553                 if (io_apic_setup(apic) < 0)
554                         panic("IO APIC setup failure");
555
556 #endif  /* APIC_IO */
557
558         /*
559          * These are required for SMP operation
560          */
561
562         /* install a 'Spurious INTerrupt' vector */
563         setidt(XSPURIOUSINT_OFFSET, Xspuriousint,
564                SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
565
566         /* install an inter-CPU IPI for TLB invalidation */
567         setidt(XINVLTLB_OFFSET, Xinvltlb,
568                SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
569
570         /* install an inter-CPU IPI for IPIQ messaging */
571         setidt(XIPIQ_OFFSET, Xipiq,
572                SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
573
574         /* install a timer vector */
575         setidt(XTIMER_OFFSET, Xtimer,
576                SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
577         
578         /* install an inter-CPU IPI for CPU stop/restart */
579         setidt(XCPUSTOP_OFFSET, Xcpustop,
580                SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
581
582         /* start each Application Processor */
583         start_all_aps(boot_addr);
584 }
585
586
587 /*
588  * look for the MP spec signature
589  */
590
591 /* string defined by the Intel MP Spec as identifying the MP table */
592 #define MP_SIG          0x5f504d5f      /* _MP_ */
593 #define NEXT(X)         ((X) += 4)
594 static int
595 mptable_search_sig(u_int32_t target, int count)
596 {
597         vm_size_t map_size;
598         u_int32_t *addr;
599         int x, ret;
600
601         KKASSERT(target != 0);
602
603         map_size = count * sizeof(u_int32_t);
604         addr = pmap_mapdev((vm_paddr_t)target, map_size);
605
606         ret = 0;
607         for (x = 0; x < count; NEXT(x)) {
608                 if (addr[x] == MP_SIG) {
609                         /* make array index a byte index */
610                         ret = target + (x * sizeof(u_int32_t));
611                         break;
612                 }
613         }
614
615         pmap_unmapdev((vm_offset_t)addr, map_size);
616         return ret;
617 }
618
619
620 static basetable_entry basetable_entry_types[] =
621 {
622         {0, 20, "Processor"},
623         {1, 8, "Bus"},
624         {2, 8, "I/O APIC"},
625         {3, 8, "I/O INT"},
626         {4, 8, "Local INT"}
627 };
628
629 typedef struct BUSDATA {
630         u_char  bus_id;
631         enum busTypes bus_type;
632 }       bus_datum;
633
634 typedef struct INTDATA {
635         u_char  int_type;
636         u_short int_flags;
637         u_char  src_bus_id;
638         u_char  src_bus_irq;
639         u_char  dst_apic_id;
640         u_char  dst_apic_int;
641         u_char  int_vector;
642 }       io_int, local_int;
643
644 typedef struct BUSTYPENAME {
645         u_char  type;
646         char    name[7];
647 }       bus_type_name;
648
649 static bus_type_name bus_type_table[] =
650 {
651         {CBUS, "CBUS"},
652         {CBUSII, "CBUSII"},
653         {EISA, "EISA"},
654         {MCA, "MCA"},
655         {UNKNOWN_BUSTYPE, "---"},
656         {ISA, "ISA"},
657         {MCA, "MCA"},
658         {UNKNOWN_BUSTYPE, "---"},
659         {UNKNOWN_BUSTYPE, "---"},
660         {UNKNOWN_BUSTYPE, "---"},
661         {UNKNOWN_BUSTYPE, "---"},
662         {UNKNOWN_BUSTYPE, "---"},
663         {PCI, "PCI"},
664         {UNKNOWN_BUSTYPE, "---"},
665         {UNKNOWN_BUSTYPE, "---"},
666         {UNKNOWN_BUSTYPE, "---"},
667         {UNKNOWN_BUSTYPE, "---"},
668         {XPRESS, "XPRESS"},
669         {UNKNOWN_BUSTYPE, "---"}
670 };
671 /* from MP spec v1.4, table 5-1 */
672 static int default_data[7][5] =
673 {
674 /*   nbus, id0, type0, id1, type1 */
675         {1, 0, ISA, 255, 255},
676         {1, 0, EISA, 255, 255},
677         {1, 0, EISA, 255, 255},
678         {1, 0, MCA, 255, 255},
679         {2, 0, ISA, 1, PCI},
680         {2, 0, EISA, 1, PCI},
681         {2, 0, MCA, 1, PCI}
682 };
683
684
685 /* the bus data */
686 static bus_datum *bus_data;
687
688 #ifdef APIC_IO
689 /* the IO INT data, one entry per possible APIC INTerrupt */
690 static io_int  *io_apic_ints;
691 static int nintrs;
692 #endif
693
694 static int processor_entry      (proc_entry_ptr entry, int cpu);
695 static int bus_entry            (bus_entry_ptr entry, int bus);
696 #ifdef APIC_IO
697 static int io_apic_entry        (io_apic_entry_ptr entry, int apic);
698 static int int_entry            (int_entry_ptr entry, int intr);
699 #endif
700 static int lookup_bus_type      (char *name);
701
702
703 /*
704  * 1st pass on motherboard's Intel MP specification table.
705  *
706  * determines:
707  *      cpu_apic_address (common to all CPUs)
708  *      io_apic_address[N]
709  *      mp_naps
710  *      mp_nbusses
711  *      mp_napics
712  *      nintrs
713  *      need_hyperthreading_fixup
714  *      logical_cpus
715  */
716 static void
717 mptable_pass1(struct mptable_pos *mpt)
718 {
719 #ifdef APIC_IO
720         int     x;
721 #endif
722         mpfps_t fps;
723         mpcth_t cth;
724         int     totalSize;
725         void*   position;
726         int     count;
727         int     type;
728         u_int   id_mask;
729
730         POSTCODE(MPTABLE_PASS1_POST);
731
732         fps = mpt->mp_fps;
733         KKASSERT(fps != NULL);
734
735 #ifdef APIC_IO
736         /* clear various tables */
737         for (x = 0; x < NAPICID; ++x) {
738                 io_apic_address[x] = ~0;        /* IO APIC address table */
739         }
740 #endif
741
742         /* init everything to empty */
743         mp_naps = 0;
744         mp_nbusses = 0;
745 #ifdef APIC_IO
746         mp_napics = 0;
747         nintrs = 0;
748 #endif
749         id_mask = 0;
750
751         /* check for use of 'default' configuration */
752         if (fps->mpfb1 != 0) {
753                 /* use default addresses */
754                 cpu_apic_address = DEFAULT_APIC_BASE;
755 #ifdef APIC_IO
756                 io_apic_address[0] = DEFAULT_IO_APIC_BASE;
757 #endif
758
759                 /* fill in with defaults */
760                 mp_naps = 2;            /* includes BSP */
761                 mp_nbusses = default_data[fps->mpfb1 - 1][0];
762 #if defined(APIC_IO)
763                 mp_napics = 1;
764                 nintrs = 16;
765 #endif  /* APIC_IO */
766         }
767         else {
768                 cth = mpt->mp_cth;
769                 if (cth == NULL)
770                         panic("MP Configuration Table Header MISSING!");
771
772                 cpu_apic_address = (vm_offset_t) cth->apic_address;
773
774                 /* walk the table, recording info of interest */
775                 totalSize = cth->base_table_length - sizeof(struct MPCTH);
776                 position = (u_char *) cth + sizeof(struct MPCTH);
777                 count = cth->entry_count;
778
779                 while (count--) {
780                         switch (type = *(u_char *) position) {
781                         case 0: /* processor_entry */
782                                 if (((proc_entry_ptr)position)->cpu_flags
783                                     & PROCENTRY_FLAG_EN) {
784                                         ++mp_naps;
785                                         id_mask |= 1 <<
786                                             ((proc_entry_ptr)position)->apic_id;
787                                 }
788                                 break;
789                         case 1: /* bus_entry */
790                                 ++mp_nbusses;
791                                 break;
792                         case 2: /* io_apic_entry */
793 #ifdef APIC_IO
794                                 if (((io_apic_entry_ptr)position)->apic_flags
795                                         & IOAPICENTRY_FLAG_EN)
796                                         io_apic_address[mp_napics++] =
797                                             (vm_offset_t)((io_apic_entry_ptr)
798                                                 position)->apic_address;
799 #endif
800                                 break;
801                         case 3: /* int_entry */
802 #ifdef APIC_IO
803                                 ++nintrs;
804 #endif
805                                 break;
806                         case 4: /* int_entry */
807                                 break;
808                         default:
809                                 panic("mpfps Base Table HOSED!");
810                                 /* NOTREACHED */
811                         }
812
813                         totalSize -= basetable_entry_types[type].length;
814                         position = (uint8_t *)position +
815                             basetable_entry_types[type].length;
816                 }
817         }
818
819         /* qualify the numbers */
820         if (mp_naps > MAXCPU) {
821                 kprintf("Warning: only using %d of %d available CPUs!\n",
822                         MAXCPU, mp_naps);
823                 mp_naps = MAXCPU;
824         }
825
826         /* See if we need to fixup HT logical CPUs. */
827         mptable_hyperthread_fixup(id_mask);
828
829         --mp_naps;      /* subtract the BSP */
830 }
831
832
833 /*
834  * 2nd pass on motherboard's Intel MP specification table.
835  *
836  * sets:
837  *      logical_cpus_mask
838  *      ID_TO_IO(N), phy APIC ID to log CPU/IO table
839  *      CPU_TO_ID(N), logical CPU to APIC ID table
840  *      IO_TO_ID(N), logical IO to APIC ID table
841  *      bus_data[N]
842  *      io_apic_ints[N]
843  */
844 static int
845 mptable_pass2(struct mptable_pos *mpt)
846 {
847         struct PROCENTRY proc;
848         int     x;
849         mpfps_t fps;
850         mpcth_t cth;
851         int     totalSize;
852         void*   position;
853         int     count;
854         int     type;
855         int     apic, bus, cpu, intr;
856         int     i;
857
858         POSTCODE(MPTABLE_PASS2_POST);
859
860         fps = mpt->mp_fps;
861         KKASSERT(fps != NULL);
862
863         /* Initialize fake proc entry for use with HT fixup. */
864         bzero(&proc, sizeof(proc));
865         proc.type = 0;
866         proc.cpu_flags = PROCENTRY_FLAG_EN;
867
868 #ifdef APIC_IO
869         MALLOC(io_apic_versions, u_int32_t *, sizeof(u_int32_t) * mp_napics,
870             M_DEVBUF, M_WAITOK);
871         MALLOC(ioapic, volatile ioapic_t **, sizeof(ioapic_t *) * mp_napics,
872             M_DEVBUF, M_WAITOK | M_ZERO);
873         MALLOC(io_apic_ints, io_int *, sizeof(io_int) * (nintrs + FIXUP_EXTRA_APIC_INTS),
874             M_DEVBUF, M_WAITOK);
875 #endif
876         MALLOC(bus_data, bus_datum *, sizeof(bus_datum) * mp_nbusses,
877             M_DEVBUF, M_WAITOK);
878
879 #ifdef APIC_IO
880         for (i = 0; i < mp_napics; i++) {
881                 ioapic[i] = permanent_io_mapping(io_apic_address[i]);
882         }
883 #endif
884
885         /* clear various tables */
886         for (x = 0; x < NAPICID; ++x) {
887                 CPU_TO_ID(x) = -1;      /* logical CPU to APIC ID table */
888 #ifdef APIC_IO
889                 ID_TO_IO(x) = -1;       /* phy APIC ID to log CPU/IO table */
890                 IO_TO_ID(x) = -1;       /* logical IO to APIC ID table */
891 #endif
892         }
893
894         /* clear bus data table */
895         for (x = 0; x < mp_nbusses; ++x)
896                 bus_data[x].bus_id = 0xff;
897
898 #ifdef APIC_IO
899         /* clear IO APIC INT table */
900         for (x = 0; x < (nintrs + 1); ++x) {
901                 io_apic_ints[x].int_type = 0xff;
902                 io_apic_ints[x].int_vector = 0xff;
903         }
904 #endif
905
906         /* record whether PIC or virtual-wire mode */
907         machintr_setvar_simple(MACHINTR_VAR_IMCR_PRESENT, fps->mpfb2 & 0x80);
908
909         /* check for use of 'default' configuration */
910         if (fps->mpfb1 != 0)
911                 return fps->mpfb1;      /* return default configuration type */
912
913         cth = mpt->mp_cth;
914         if (cth == NULL)
915                 panic("MP Configuration Table Header MISSING!");
916
917         /* walk the table, recording info of interest */
918         totalSize = cth->base_table_length - sizeof(struct MPCTH);
919         position = (u_char *) cth + sizeof(struct MPCTH);
920         count = cth->entry_count;
921         apic = bus = intr = 0;
922         cpu = 1;                                /* pre-count the BSP */
923
924         while (count--) {
925                 switch (type = *(u_char *) position) {
926                 case 0:
927                         if (processor_entry(position, cpu))
928                                 ++cpu;
929
930                         if (need_hyperthreading_fixup) {
931                                 /*
932                                  * Create fake mptable processor entries
933                                  * and feed them to processor_entry() to
934                                  * enumerate the logical CPUs.
935                                  */
936                                 proc.apic_id = ((proc_entry_ptr)position)->apic_id;
937                                 for (i = 1; i < logical_cpus; i++) {
938                                         proc.apic_id++;
939                                         processor_entry(&proc, cpu);
940                                         logical_cpus_mask |= (1 << cpu);
941                                         cpu++;
942                                 }
943                         }
944                         break;
945                 case 1:
946                         if (bus_entry(position, bus))
947                                 ++bus;
948                         break;
949                 case 2:
950 #ifdef APIC_IO
951                         if (io_apic_entry(position, apic))
952                                 ++apic;
953 #endif
954                         break;
955                 case 3:
956 #ifdef APIC_IO
957                         if (int_entry(position, intr))
958                                 ++intr;
959 #endif
960                         break;
961                 case 4:
962                         /* int_entry(position); */
963                         break;
964                 default:
965                         panic("mpfps Base Table HOSED!");
966                         /* NOTREACHED */
967                 }
968
969                 totalSize -= basetable_entry_types[type].length;
970                 position = (uint8_t *)position + basetable_entry_types[type].length;
971         }
972
973         if (CPU_TO_ID(0) < 0)
974                 panic("NO BSP found!");
975
976         /* report fact that its NOT a default configuration */
977         return 0;
978 }
979
980 /*
981  * Check if we should perform a hyperthreading "fix-up" to
982  * enumerate any logical CPU's that aren't already listed
983  * in the table.
984  *
985  * XXX: We assume that all of the physical CPUs in the
986  * system have the same number of logical CPUs.
987  *
988  * XXX: We assume that APIC ID's are allocated such that
989  * the APIC ID's for a physical processor are aligned
990  * with the number of logical CPU's in the processor.
991  */
992 static void
993 mptable_hyperthread_fixup(u_int id_mask)
994 {
995         u_int i, id;
996
997         /* Nothing to do if there is no HTT support. */
998         if ((cpu_feature & CPUID_HTT) == 0)
999                 return;
1000         logical_cpus = (cpu_procinfo & CPUID_HTT_CORES) >> 16;
1001         if (logical_cpus <= 1)
1002                 return;
1003
1004         /*
1005          * For each APIC ID of a CPU that is set in the mask,
1006          * scan the other candidate APIC ID's for this
1007          * physical processor.  If any of those ID's are
1008          * already in the table, then kill the fixup.
1009          */
1010         for (id = 0; id <= MAXCPU; id++) {
1011                 if ((id_mask & 1 << id) == 0)
1012                         continue;
1013                 /* First, make sure we are on a logical_cpus boundary. */
1014                 if (id % logical_cpus != 0)
1015                         return;
1016                 for (i = id + 1; i < id + logical_cpus; i++)
1017                         if ((id_mask & 1 << i) != 0)
1018                                 return;
1019         }
1020
1021         /*
1022          * Ok, the ID's checked out, so enable the fixup.  We have to fixup
1023          * mp_naps right now.
1024          */
1025         need_hyperthreading_fixup = 1;
1026         mp_naps *= logical_cpus;
1027 }
1028
1029 static void
1030 mptable_map(struct mptable_pos *mpt, vm_paddr_t mpfps_paddr)
1031 {
1032         mpfps_t fps = NULL;
1033         mpcth_t cth = NULL;
1034         vm_size_t cth_mapsz = 0;
1035
1036         fps = pmap_mapdev(mpfps_paddr, sizeof(*fps));
1037         if (fps->pap != 0) {
1038                 /*
1039                  * Map configuration table header to get
1040                  * the base table size
1041                  */
1042                 cth = pmap_mapdev(fps->pap, sizeof(*cth));
1043                 cth_mapsz = cth->base_table_length;
1044                 pmap_unmapdev((vm_offset_t)cth, sizeof(*cth));
1045
1046                 /*
1047                  * Map the base table
1048                  */
1049                 cth = pmap_mapdev(fps->pap, cth_mapsz);
1050         }
1051
1052         mpt->mp_fps = fps;
1053         mpt->mp_cth = cth;
1054         mpt->mp_cth_mapsz = cth_mapsz;
1055 }
1056
1057 static void
1058 mptable_unmap(struct mptable_pos *mpt)
1059 {
1060         if (mpt->mp_cth != NULL) {
1061                 pmap_unmapdev((vm_offset_t)mpt->mp_cth, mpt->mp_cth_mapsz);
1062                 mpt->mp_cth = NULL;
1063                 mpt->mp_cth_mapsz = 0;
1064         }
1065         if (mpt->mp_fps != NULL) {
1066                 pmap_unmapdev((vm_offset_t)mpt->mp_fps, sizeof(*mpt->mp_fps));
1067                 mpt->mp_fps = NULL;
1068         }
1069 }
1070
1071 #ifdef APIC_IO
1072
1073 void
1074 assign_apic_irq(int apic, int intpin, int irq)
1075 {
1076         int x;
1077         
1078         if (int_to_apicintpin[irq].ioapic != -1)
1079                 panic("assign_apic_irq: inconsistent table");
1080         
1081         int_to_apicintpin[irq].ioapic = apic;
1082         int_to_apicintpin[irq].int_pin = intpin;
1083         int_to_apicintpin[irq].apic_address = ioapic[apic];
1084         int_to_apicintpin[irq].redirindex = IOAPIC_REDTBL + 2 * intpin;
1085         
1086         for (x = 0; x < nintrs; x++) {
1087                 if ((io_apic_ints[x].int_type == 0 || 
1088                      io_apic_ints[x].int_type == 3) &&
1089                     io_apic_ints[x].int_vector == 0xff &&
1090                     io_apic_ints[x].dst_apic_id == IO_TO_ID(apic) &&
1091                     io_apic_ints[x].dst_apic_int == intpin)
1092                         io_apic_ints[x].int_vector = irq;
1093         }
1094 }
1095
1096 void
1097 revoke_apic_irq(int irq)
1098 {
1099         int x;
1100         int oldapic;
1101         int oldintpin;
1102         
1103         if (int_to_apicintpin[irq].ioapic == -1)
1104                 panic("revoke_apic_irq: inconsistent table");
1105         
1106         oldapic = int_to_apicintpin[irq].ioapic;
1107         oldintpin = int_to_apicintpin[irq].int_pin;
1108
1109         int_to_apicintpin[irq].ioapic = -1;
1110         int_to_apicintpin[irq].int_pin = 0;
1111         int_to_apicintpin[irq].apic_address = NULL;
1112         int_to_apicintpin[irq].redirindex = 0;
1113         
1114         for (x = 0; x < nintrs; x++) {
1115                 if ((io_apic_ints[x].int_type == 0 || 
1116                      io_apic_ints[x].int_type == 3) &&
1117                     io_apic_ints[x].int_vector != 0xff &&
1118                     io_apic_ints[x].dst_apic_id == IO_TO_ID(oldapic) &&
1119                     io_apic_ints[x].dst_apic_int == oldintpin)
1120                         io_apic_ints[x].int_vector = 0xff;
1121         }
1122 }
1123
1124 /*
1125  * Allocate an IRQ 
1126  */
1127 static void
1128 allocate_apic_irq(int intr)
1129 {
1130         int apic;
1131         int intpin;
1132         int irq;
1133         
1134         if (io_apic_ints[intr].int_vector != 0xff)
1135                 return;         /* Interrupt handler already assigned */
1136         
1137         if (io_apic_ints[intr].int_type != 0 &&
1138             (io_apic_ints[intr].int_type != 3 ||
1139              (io_apic_ints[intr].dst_apic_id == IO_TO_ID(0) &&
1140               io_apic_ints[intr].dst_apic_int == 0)))
1141                 return;         /* Not INT or ExtInt on != (0, 0) */
1142         
1143         irq = 0;
1144         while (irq < APIC_INTMAPSIZE &&
1145                int_to_apicintpin[irq].ioapic != -1)
1146                 irq++;
1147         
1148         if (irq >= APIC_INTMAPSIZE)
1149                 return;         /* No free interrupt handlers */
1150         
1151         apic = ID_TO_IO(io_apic_ints[intr].dst_apic_id);
1152         intpin = io_apic_ints[intr].dst_apic_int;
1153         
1154         assign_apic_irq(apic, intpin, irq);
1155         io_apic_setup_intpin(apic, intpin);
1156 }
1157
1158
1159 static void
1160 swap_apic_id(int apic, int oldid, int newid)
1161 {
1162         int x;
1163         int oapic;
1164         
1165
1166         if (oldid == newid)
1167                 return;                 /* Nothing to do */
1168         
1169         kprintf("Changing APIC ID for IO APIC #%d from %d to %d in MP table\n",
1170                apic, oldid, newid);
1171         
1172         /* Swap physical APIC IDs in interrupt entries */
1173         for (x = 0; x < nintrs; x++) {
1174                 if (io_apic_ints[x].dst_apic_id == oldid)
1175                         io_apic_ints[x].dst_apic_id = newid;
1176                 else if (io_apic_ints[x].dst_apic_id == newid)
1177                         io_apic_ints[x].dst_apic_id = oldid;
1178         }
1179         
1180         /* Swap physical APIC IDs in IO_TO_ID mappings */
1181         for (oapic = 0; oapic < mp_napics; oapic++)
1182                 if (IO_TO_ID(oapic) == newid)
1183                         break;
1184         
1185         if (oapic < mp_napics) {
1186                 kprintf("Changing APIC ID for IO APIC #%d from "
1187                        "%d to %d in MP table\n",
1188                        oapic, newid, oldid);
1189                 IO_TO_ID(oapic) = oldid;
1190         }
1191         IO_TO_ID(apic) = newid;
1192 }
1193
1194
1195 static void
1196 fix_id_to_io_mapping(void)
1197 {
1198         int x;
1199
1200         for (x = 0; x < NAPICID; x++)
1201                 ID_TO_IO(x) = -1;
1202         
1203         for (x = 0; x <= mp_naps; x++)
1204                 if (CPU_TO_ID(x) < NAPICID)
1205                         ID_TO_IO(CPU_TO_ID(x)) = x;
1206         
1207         for (x = 0; x < mp_napics; x++)
1208                 if (IO_TO_ID(x) < NAPICID)
1209                         ID_TO_IO(IO_TO_ID(x)) = x;
1210 }
1211
1212
1213 static int
1214 first_free_apic_id(void)
1215 {
1216         int freeid, x;
1217         
1218         for (freeid = 0; freeid < NAPICID; freeid++) {
1219                 for (x = 0; x <= mp_naps; x++)
1220                         if (CPU_TO_ID(x) == freeid)
1221                                 break;
1222                 if (x <= mp_naps)
1223                         continue;
1224                 for (x = 0; x < mp_napics; x++)
1225                         if (IO_TO_ID(x) == freeid)
1226                                 break;
1227                 if (x < mp_napics)
1228                         continue;
1229                 return freeid;
1230         }
1231         return freeid;
1232 }
1233
1234
1235 static int
1236 io_apic_id_acceptable(int apic, int id)
1237 {
1238         int cpu;                /* Logical CPU number */
1239         int oapic;              /* Logical IO APIC number for other IO APIC */
1240
1241         if (id >= NAPICID)
1242                 return 0;       /* Out of range */
1243         
1244         for (cpu = 0; cpu <= mp_naps; cpu++)
1245                 if (CPU_TO_ID(cpu) == id)
1246                         return 0;       /* Conflict with CPU */
1247         
1248         for (oapic = 0; oapic < mp_napics && oapic < apic; oapic++)
1249                 if (IO_TO_ID(oapic) == id)
1250                         return 0;       /* Conflict with other APIC */
1251         
1252         return 1;               /* ID is acceptable for IO APIC */
1253 }
1254
1255 static
1256 io_int *
1257 io_apic_find_int_entry(int apic, int pin)
1258 {
1259         int     x;
1260
1261         /* search each of the possible INTerrupt sources */
1262         for (x = 0; x < nintrs; ++x) {
1263                 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
1264                     (pin == io_apic_ints[x].dst_apic_int))
1265                         return (&io_apic_ints[x]);
1266         }
1267         return NULL;
1268 }
1269
1270 #endif
1271
1272 /*
1273  * parse an Intel MP specification table
1274  */
1275 static void
1276 mptable_fix(void)
1277 {
1278         int     x;
1279 #ifdef APIC_IO
1280         int     id;
1281         int     apic;           /* IO APIC unit number */
1282         int     freeid;         /* Free physical APIC ID */
1283         int     physid;         /* Current physical IO APIC ID */
1284         io_int *io14;
1285 #endif
1286         int     bus_0 = 0;      /* Stop GCC warning */
1287         int     bus_pci = 0;    /* Stop GCC warning */
1288         int     num_pci_bus;
1289
1290         /*
1291          * Fix mis-numbering of the PCI bus and its INT entries if the BIOS
1292          * did it wrong.  The MP spec says that when more than 1 PCI bus
1293          * exists the BIOS must begin with bus entries for the PCI bus and use
1294          * actual PCI bus numbering.  This implies that when only 1 PCI bus
1295          * exists the BIOS can choose to ignore this ordering, and indeed many
1296          * MP motherboards do ignore it.  This causes a problem when the PCI
1297          * sub-system makes requests of the MP sub-system based on PCI bus
1298          * numbers.     So here we look for the situation and renumber the
1299          * busses and associated INTs in an effort to "make it right".
1300          */
1301
1302         /* find bus 0, PCI bus, count the number of PCI busses */
1303         for (num_pci_bus = 0, x = 0; x < mp_nbusses; ++x) {
1304                 if (bus_data[x].bus_id == 0) {
1305                         bus_0 = x;
1306                 }
1307                 if (bus_data[x].bus_type == PCI) {
1308                         ++num_pci_bus;
1309                         bus_pci = x;
1310                 }
1311         }
1312         /*
1313          * bus_0 == slot of bus with ID of 0
1314          * bus_pci == slot of last PCI bus encountered
1315          */
1316
1317         /* check the 1 PCI bus case for sanity */
1318         /* if it is number 0 all is well */
1319         if (num_pci_bus == 1 &&
1320             bus_data[bus_pci].bus_id != 0) {
1321                 
1322                 /* mis-numbered, swap with whichever bus uses slot 0 */
1323
1324                 /* swap the bus entry types */
1325                 bus_data[bus_pci].bus_type = bus_data[bus_0].bus_type;
1326                 bus_data[bus_0].bus_type = PCI;
1327
1328 #ifdef APIC_IO
1329                 /* swap each relavant INTerrupt entry */
1330                 id = bus_data[bus_pci].bus_id;
1331                 for (x = 0; x < nintrs; ++x) {
1332                         if (io_apic_ints[x].src_bus_id == id) {
1333                                 io_apic_ints[x].src_bus_id = 0;
1334                         }
1335                         else if (io_apic_ints[x].src_bus_id == 0) {
1336                                 io_apic_ints[x].src_bus_id = id;
1337                         }
1338                 }
1339 #endif
1340         }
1341
1342 #ifdef APIC_IO
1343         /* Assign IO APIC IDs.
1344          * 
1345          * First try the existing ID. If a conflict is detected, try
1346          * the ID in the MP table.  If a conflict is still detected, find
1347          * a free id.
1348          *
1349          * We cannot use the ID_TO_IO table before all conflicts has been
1350          * resolved and the table has been corrected.
1351          */
1352         for (apic = 0; apic < mp_napics; ++apic) { /* For all IO APICs */
1353                 
1354                 /* First try to use the value set by the BIOS */
1355                 physid = io_apic_get_id(apic);
1356                 if (io_apic_id_acceptable(apic, physid)) {
1357                         if (IO_TO_ID(apic) != physid)
1358                                 swap_apic_id(apic, IO_TO_ID(apic), physid);
1359                         continue;
1360                 }
1361
1362                 /* Then check if the value in the MP table is acceptable */
1363                 if (io_apic_id_acceptable(apic, IO_TO_ID(apic)))
1364                         continue;
1365
1366                 /* Last resort, find a free APIC ID and use it */
1367                 freeid = first_free_apic_id();
1368                 if (freeid >= NAPICID)
1369                         panic("No free physical APIC IDs found");
1370                 
1371                 if (io_apic_id_acceptable(apic, freeid)) {
1372                         swap_apic_id(apic, IO_TO_ID(apic), freeid);
1373                         continue;
1374                 }
1375                 panic("Free physical APIC ID not usable");
1376         }
1377         fix_id_to_io_mapping();
1378 #endif
1379
1380 #ifdef APIC_IO
1381         /* detect and fix broken Compaq MP table */
1382         if (apic_int_type(0, 0) == -1) {
1383                 kprintf("APIC_IO: MP table broken: 8259->APIC entry missing!\n");
1384                 io_apic_ints[nintrs].int_type = 3;      /* ExtInt */
1385                 io_apic_ints[nintrs].int_vector = 0xff; /* Unassigned */
1386                 /* XXX fixme, set src bus id etc, but it doesn't seem to hurt */
1387                 io_apic_ints[nintrs].dst_apic_id = IO_TO_ID(0);
1388                 io_apic_ints[nintrs].dst_apic_int = 0;  /* Pin 0 */
1389                 nintrs++;
1390         } else if (apic_int_type(0, 0) == 0) {
1391                 kprintf("APIC_IO: MP table broken: ExtINT entry corrupt!\n");
1392                 for (x = 0; x < nintrs; ++x)
1393                         if ((0 == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
1394                             (0 == io_apic_ints[x].dst_apic_int)) {
1395                                 io_apic_ints[x].int_type = 3;
1396                                 io_apic_ints[x].int_vector = 0xff;
1397                                 break;
1398                         }
1399         }
1400
1401         /*
1402          * Fix missing IRQ 15 when IRQ 14 is an ISA interrupt.  IDE
1403          * controllers universally come in pairs.  If IRQ 14 is specified
1404          * as an ISA interrupt, then IRQ 15 had better be too.
1405          *
1406          * [ Shuttle XPC / AMD Athlon X2 ]
1407          *      The MPTable is missing an entry for IRQ 15.  Note that the
1408          *      ACPI table has an entry for both 14 and 15.
1409          */
1410         if (apic_int_type(0, 14) == 0 && apic_int_type(0, 15) == -1) {
1411                 kprintf("APIC_IO: MP table broken: IRQ 15 not ISA when IRQ 14 is!\n");
1412                 io14 = io_apic_find_int_entry(0, 14);
1413                 io_apic_ints[nintrs] = *io14;
1414                 io_apic_ints[nintrs].src_bus_irq = 15;
1415                 io_apic_ints[nintrs].dst_apic_int = 15;
1416                 nintrs++;
1417         }
1418 #endif
1419 }
1420
1421 #ifdef APIC_IO
1422
1423 /* Assign low level interrupt handlers */
1424 static void
1425 setup_apic_irq_mapping(void)
1426 {
1427         int     x;
1428         int     int_vector;
1429
1430         /* Clear array */
1431         for (x = 0; x < APIC_INTMAPSIZE; x++) {
1432                 int_to_apicintpin[x].ioapic = -1;
1433                 int_to_apicintpin[x].int_pin = 0;
1434                 int_to_apicintpin[x].apic_address = NULL;
1435                 int_to_apicintpin[x].redirindex = 0;
1436         }
1437
1438         /* First assign ISA/EISA interrupts */
1439         for (x = 0; x < nintrs; x++) {
1440                 int_vector = io_apic_ints[x].src_bus_irq;
1441                 if (int_vector < APIC_INTMAPSIZE &&
1442                     io_apic_ints[x].int_vector == 0xff && 
1443                     int_to_apicintpin[int_vector].ioapic == -1 &&
1444                     (apic_int_is_bus_type(x, ISA) ||
1445                      apic_int_is_bus_type(x, EISA)) &&
1446                     io_apic_ints[x].int_type == 0) {
1447                         assign_apic_irq(ID_TO_IO(io_apic_ints[x].dst_apic_id), 
1448                                         io_apic_ints[x].dst_apic_int,
1449                                         int_vector);
1450                 }
1451         }
1452
1453         /* Assign ExtInt entry if no ISA/EISA interrupt 0 entry */
1454         for (x = 0; x < nintrs; x++) {
1455                 if (io_apic_ints[x].dst_apic_int == 0 &&
1456                     io_apic_ints[x].dst_apic_id == IO_TO_ID(0) &&
1457                     io_apic_ints[x].int_vector == 0xff && 
1458                     int_to_apicintpin[0].ioapic == -1 &&
1459                     io_apic_ints[x].int_type == 3) {
1460                         assign_apic_irq(0, 0, 0);
1461                         break;
1462                 }
1463         }
1464         /* PCI interrupt assignment is deferred */
1465 }
1466
1467 #endif
1468
1469 void
1470 mp_set_cpuids(int cpu_id, int apic_id)
1471 {
1472         CPU_TO_ID(cpu_id) = apic_id;
1473         ID_TO_CPU(apic_id) = cpu_id;
1474 }
1475
1476 static int
1477 processor_entry(proc_entry_ptr entry, int cpu)
1478 {
1479         KKASSERT(cpu > 0);
1480
1481         /* check for usability */
1482         if (!(entry->cpu_flags & PROCENTRY_FLAG_EN))
1483                 return 0;
1484
1485         if(entry->apic_id >= NAPICID)
1486                 panic("CPU APIC ID out of range (0..%d)", NAPICID - 1);
1487         /* check for BSP flag */
1488         if (entry->cpu_flags & PROCENTRY_FLAG_BP) {
1489                 mp_set_cpuids(0, entry->apic_id);
1490                 return 0;       /* its already been counted */
1491         }
1492
1493         /* add another AP to list, if less than max number of CPUs */
1494         else if (cpu < MAXCPU) {
1495                 mp_set_cpuids(cpu, entry->apic_id);
1496                 return 1;
1497         }
1498
1499         return 0;
1500 }
1501
1502
1503 static int
1504 bus_entry(bus_entry_ptr entry, int bus)
1505 {
1506         int     x;
1507         char    c, name[8];
1508
1509         /* encode the name into an index */
1510         for (x = 0; x < 6; ++x) {
1511                 if ((c = entry->bus_type[x]) == ' ')
1512                         break;
1513                 name[x] = c;
1514         }
1515         name[x] = '\0';
1516
1517         if ((x = lookup_bus_type(name)) == UNKNOWN_BUSTYPE)
1518                 panic("unknown bus type: '%s'", name);
1519
1520         bus_data[bus].bus_id = entry->bus_id;
1521         bus_data[bus].bus_type = x;
1522
1523         return 1;
1524 }
1525
1526 #ifdef APIC_IO
1527
1528 static int
1529 io_apic_entry(io_apic_entry_ptr entry, int apic)
1530 {
1531         if (!(entry->apic_flags & IOAPICENTRY_FLAG_EN))
1532                 return 0;
1533
1534         IO_TO_ID(apic) = entry->apic_id;
1535         if (entry->apic_id < NAPICID)
1536                 ID_TO_IO(entry->apic_id) = apic;
1537
1538         return 1;
1539 }
1540
1541 #endif
1542
1543 static int
1544 lookup_bus_type(char *name)
1545 {
1546         int     x;
1547
1548         for (x = 0; x < MAX_BUSTYPE; ++x)
1549                 if (strcmp(bus_type_table[x].name, name) == 0)
1550                         return bus_type_table[x].type;
1551
1552         return UNKNOWN_BUSTYPE;
1553 }
1554
1555 #ifdef APIC_IO
1556
1557 static int
1558 int_entry(int_entry_ptr entry, int intr)
1559 {
1560         int apic;
1561
1562         io_apic_ints[intr].int_type = entry->int_type;
1563         io_apic_ints[intr].int_flags = entry->int_flags;
1564         io_apic_ints[intr].src_bus_id = entry->src_bus_id;
1565         io_apic_ints[intr].src_bus_irq = entry->src_bus_irq;
1566         if (entry->dst_apic_id == 255) {
1567                 /* This signal goes to all IO APICS.  Select an IO APIC
1568                    with sufficient number of interrupt pins */
1569                 for (apic = 0; apic < mp_napics; apic++)
1570                         if (((io_apic_read(apic, IOAPIC_VER) & 
1571                               IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) >= 
1572                             entry->dst_apic_int)
1573                                 break;
1574                 if (apic < mp_napics)
1575                         io_apic_ints[intr].dst_apic_id = IO_TO_ID(apic);
1576                 else
1577                         io_apic_ints[intr].dst_apic_id = entry->dst_apic_id;
1578         } else
1579                 io_apic_ints[intr].dst_apic_id = entry->dst_apic_id;
1580         io_apic_ints[intr].dst_apic_int = entry->dst_apic_int;
1581
1582         return 1;
1583 }
1584
1585 static int
1586 apic_int_is_bus_type(int intr, int bus_type)
1587 {
1588         int     bus;
1589
1590         for (bus = 0; bus < mp_nbusses; ++bus)
1591                 if ((bus_data[bus].bus_id == io_apic_ints[intr].src_bus_id)
1592                     && ((int) bus_data[bus].bus_type == bus_type))
1593                         return 1;
1594
1595         return 0;
1596 }
1597
1598 /*
1599  * Given a traditional ISA INT mask, return an APIC mask.
1600  */
1601 u_int
1602 isa_apic_mask(u_int isa_mask)
1603 {
1604         int isa_irq;
1605         int apic_pin;
1606
1607 #if defined(SKIP_IRQ15_REDIRECT)
1608         if (isa_mask == (1 << 15)) {
1609                 kprintf("skipping ISA IRQ15 redirect\n");
1610                 return isa_mask;
1611         }
1612 #endif  /* SKIP_IRQ15_REDIRECT */
1613
1614         isa_irq = ffs(isa_mask);                /* find its bit position */
1615         if (isa_irq == 0)                       /* doesn't exist */
1616                 return 0;
1617         --isa_irq;                              /* make it zero based */
1618
1619         apic_pin = isa_apic_irq(isa_irq);       /* look for APIC connection */
1620         if (apic_pin == -1)
1621                 return 0;
1622
1623         return (1 << apic_pin);                 /* convert pin# to a mask */
1624 }
1625
1626 /*
1627  * Determine which APIC pin an ISA/EISA INT is attached to.
1628  */
1629 #define INTTYPE(I)      (io_apic_ints[(I)].int_type)
1630 #define INTPIN(I)       (io_apic_ints[(I)].dst_apic_int)
1631 #define INTIRQ(I)       (io_apic_ints[(I)].int_vector)
1632 #define INTAPIC(I)      (ID_TO_IO(io_apic_ints[(I)].dst_apic_id))
1633
1634 #define SRCBUSIRQ(I)    (io_apic_ints[(I)].src_bus_irq)
1635 int
1636 isa_apic_irq(int isa_irq)
1637 {
1638         int     intr;
1639
1640         for (intr = 0; intr < nintrs; ++intr) {         /* check each record */
1641                 if (INTTYPE(intr) == 0) {               /* standard INT */
1642                         if (SRCBUSIRQ(intr) == isa_irq) {
1643                                 if (apic_int_is_bus_type(intr, ISA) ||
1644                                     apic_int_is_bus_type(intr, EISA)) {
1645                                         if (INTIRQ(intr) == 0xff)
1646                                                 return -1; /* unassigned */
1647                                         return INTIRQ(intr);    /* found */
1648                                 }
1649                         }
1650                 }
1651         }
1652         return -1;                                      /* NOT found */
1653 }
1654
1655
1656 /*
1657  * Determine which APIC pin a PCI INT is attached to.
1658  */
1659 #define SRCBUSID(I)     (io_apic_ints[(I)].src_bus_id)
1660 #define SRCBUSDEVICE(I) ((io_apic_ints[(I)].src_bus_irq >> 2) & 0x1f)
1661 #define SRCBUSLINE(I)   (io_apic_ints[(I)].src_bus_irq & 0x03)
1662 int
1663 pci_apic_irq(int pciBus, int pciDevice, int pciInt)
1664 {
1665         int     intr;
1666
1667         --pciInt;                                       /* zero based */
1668
1669         for (intr = 0; intr < nintrs; ++intr) {         /* check each record */
1670                 if ((INTTYPE(intr) == 0)                /* standard INT */
1671                     && (SRCBUSID(intr) == pciBus)
1672                     && (SRCBUSDEVICE(intr) == pciDevice)
1673                     && (SRCBUSLINE(intr) == pciInt)) {  /* a candidate IRQ */
1674                         if (apic_int_is_bus_type(intr, PCI)) {
1675                                 if (INTIRQ(intr) == 0xff)
1676                                         allocate_apic_irq(intr);
1677                                 if (INTIRQ(intr) == 0xff)
1678                                         return -1;      /* unassigned */
1679                                 return INTIRQ(intr);    /* exact match */
1680                         }
1681                 }
1682         }
1683
1684         return -1;                                      /* NOT found */
1685 }
1686
1687 int
1688 next_apic_irq(int irq) 
1689 {
1690         int intr, ointr;
1691         int bus, bustype;
1692
1693         bus = 0;
1694         bustype = 0;
1695         for (intr = 0; intr < nintrs; intr++) {
1696                 if (INTIRQ(intr) != irq || INTTYPE(intr) != 0)
1697                         continue;
1698                 bus = SRCBUSID(intr);
1699                 bustype = apic_bus_type(bus);
1700                 if (bustype != ISA &&
1701                     bustype != EISA &&
1702                     bustype != PCI)
1703                         continue;
1704                 break;
1705         }
1706         if (intr >= nintrs) {
1707                 return -1;
1708         }
1709         for (ointr = intr + 1; ointr < nintrs; ointr++) {
1710                 if (INTTYPE(ointr) != 0)
1711                         continue;
1712                 if (bus != SRCBUSID(ointr))
1713                         continue;
1714                 if (bustype == PCI) {
1715                         if (SRCBUSDEVICE(intr) != SRCBUSDEVICE(ointr))
1716                                 continue;
1717                         if (SRCBUSLINE(intr) != SRCBUSLINE(ointr))
1718                                 continue;
1719                 }
1720                 if (bustype == ISA || bustype == EISA) {
1721                         if (SRCBUSIRQ(intr) != SRCBUSIRQ(ointr))
1722                                 continue;
1723                 }
1724                 if (INTPIN(intr) == INTPIN(ointr))
1725                         continue;
1726                 break;
1727         }
1728         if (ointr >= nintrs) {
1729                 return -1;
1730         }
1731         return INTIRQ(ointr);
1732 }
1733 #undef SRCBUSLINE
1734 #undef SRCBUSDEVICE
1735 #undef SRCBUSID
1736 #undef SRCBUSIRQ
1737
1738 #undef INTPIN
1739 #undef INTIRQ
1740 #undef INTAPIC
1741 #undef INTTYPE
1742
1743 #endif
1744
1745 /*
1746  * Reprogram the MB chipset to NOT redirect an ISA INTerrupt.
1747  *
1748  * XXX FIXME:
1749  *  Exactly what this means is unclear at this point.  It is a solution
1750  *  for motherboards that redirect the MBIRQ0 pin.  Generically a motherboard
1751  *  could route any of the ISA INTs to upper (>15) IRQ values.  But most would
1752  *  NOT be redirected via MBIRQ0, thus "undirect()ing" them would NOT be an
1753  *  option.
1754  */
1755 int
1756 undirect_isa_irq(int rirq)
1757 {
1758 #if defined(READY)
1759         if (bootverbose)
1760             kprintf("Freeing redirected ISA irq %d.\n", rirq);
1761         /** FIXME: tickle the MB redirector chip */
1762         return /* XXX */;
1763 #else
1764         if (bootverbose)
1765             kprintf("Freeing (NOT implemented) redirected ISA irq %d.\n", rirq);
1766         return 0;
1767 #endif  /* READY */
1768 }
1769
1770
1771 /*
1772  * Reprogram the MB chipset to NOT redirect a PCI INTerrupt
1773  */
1774 int
1775 undirect_pci_irq(int rirq)
1776 {
1777 #if defined(READY)
1778         if (bootverbose)
1779                 kprintf("Freeing redirected PCI irq %d.\n", rirq);
1780
1781         /** FIXME: tickle the MB redirector chip */
1782         return /* XXX */;
1783 #else
1784         if (bootverbose)
1785                 kprintf("Freeing (NOT implemented) redirected PCI irq %d.\n",
1786                        rirq);
1787         return 0;
1788 #endif  /* READY */
1789 }
1790
1791
1792 /*
1793  * given a bus ID, return:
1794  *  the bus type if found
1795  *  -1 if NOT found
1796  */
1797 int
1798 apic_bus_type(int id)
1799 {
1800         int     x;
1801
1802         for (x = 0; x < mp_nbusses; ++x)
1803                 if (bus_data[x].bus_id == id)
1804                         return bus_data[x].bus_type;
1805
1806         return -1;
1807 }
1808
1809 #ifdef APIC_IO
1810
1811 /*
1812  * given a LOGICAL APIC# and pin#, return:
1813  *  the associated src bus ID if found
1814  *  -1 if NOT found
1815  */
1816 int
1817 apic_src_bus_id(int apic, int pin)
1818 {
1819         int     x;
1820
1821         /* search each of the possible INTerrupt sources */
1822         for (x = 0; x < nintrs; ++x)
1823                 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
1824                     (pin == io_apic_ints[x].dst_apic_int))
1825                         return (io_apic_ints[x].src_bus_id);
1826
1827         return -1;              /* NOT found */
1828 }
1829
1830 /*
1831  * given a LOGICAL APIC# and pin#, return:
1832  *  the associated src bus IRQ if found
1833  *  -1 if NOT found
1834  */
1835 int
1836 apic_src_bus_irq(int apic, int pin)
1837 {
1838         int     x;
1839
1840         for (x = 0; x < nintrs; x++)
1841                 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
1842                     (pin == io_apic_ints[x].dst_apic_int))
1843                         return (io_apic_ints[x].src_bus_irq);
1844
1845         return -1;              /* NOT found */
1846 }
1847
1848
1849 /*
1850  * given a LOGICAL APIC# and pin#, return:
1851  *  the associated INTerrupt type if found
1852  *  -1 if NOT found
1853  */
1854 int
1855 apic_int_type(int apic, int pin)
1856 {
1857         int     x;
1858
1859         /* search each of the possible INTerrupt sources */
1860         for (x = 0; x < nintrs; ++x) {
1861                 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
1862                     (pin == io_apic_ints[x].dst_apic_int))
1863                         return (io_apic_ints[x].int_type);
1864         }
1865         return -1;              /* NOT found */
1866 }
1867
1868 /*
1869  * Return the IRQ associated with an APIC pin
1870  */
1871 int 
1872 apic_irq(int apic, int pin)
1873 {
1874         int x;
1875         int res;
1876
1877         for (x = 0; x < nintrs; ++x) {
1878                 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
1879                     (pin == io_apic_ints[x].dst_apic_int)) {
1880                         res = io_apic_ints[x].int_vector;
1881                         if (res == 0xff)
1882                                 return -1;
1883                         if (apic != int_to_apicintpin[res].ioapic)
1884                                 panic("apic_irq: inconsistent table %d/%d", apic, int_to_apicintpin[res].ioapic);
1885                         if (pin != int_to_apicintpin[res].int_pin)
1886                                 panic("apic_irq inconsistent table (2)");
1887                         return res;
1888                 }
1889         }
1890         return -1;
1891 }
1892
1893
1894 /*
1895  * given a LOGICAL APIC# and pin#, return:
1896  *  the associated trigger mode if found
1897  *  -1 if NOT found
1898  */
1899 int
1900 apic_trigger(int apic, int pin)
1901 {
1902         int     x;
1903
1904         /* search each of the possible INTerrupt sources */
1905         for (x = 0; x < nintrs; ++x)
1906                 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
1907                     (pin == io_apic_ints[x].dst_apic_int))
1908                         return ((io_apic_ints[x].int_flags >> 2) & 0x03);
1909
1910         return -1;              /* NOT found */
1911 }
1912
1913
1914 /*
1915  * given a LOGICAL APIC# and pin#, return:
1916  *  the associated 'active' level if found
1917  *  -1 if NOT found
1918  */
1919 int
1920 apic_polarity(int apic, int pin)
1921 {
1922         int     x;
1923
1924         /* search each of the possible INTerrupt sources */
1925         for (x = 0; x < nintrs; ++x)
1926                 if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
1927                     (pin == io_apic_ints[x].dst_apic_int))
1928                         return (io_apic_ints[x].int_flags & 0x03);
1929
1930         return -1;              /* NOT found */
1931 }
1932
1933 #endif
1934
1935 /*
1936  * set data according to MP defaults
1937  * FIXME: probably not complete yet...
1938  */
1939 static void
1940 mptable_default(int type)
1941 {
1942         int     ap_cpu_id, boot_cpu_id;
1943 #if defined(APIC_IO)
1944         int     io_apic_id;
1945         int     pin;
1946 #endif  /* APIC_IO */
1947
1948 #if 0
1949         kprintf("  MP default config type: %d\n", type);
1950         switch (type) {
1951         case 1:
1952                 kprintf("   bus: ISA, APIC: 82489DX\n");
1953                 break;
1954         case 2:
1955                 kprintf("   bus: EISA, APIC: 82489DX\n");
1956                 break;
1957         case 3:
1958                 kprintf("   bus: EISA, APIC: 82489DX\n");
1959                 break;
1960         case 4:
1961                 kprintf("   bus: MCA, APIC: 82489DX\n");
1962                 break;
1963         case 5:
1964                 kprintf("   bus: ISA+PCI, APIC: Integrated\n");
1965                 break;
1966         case 6:
1967                 kprintf("   bus: EISA+PCI, APIC: Integrated\n");
1968                 break;
1969         case 7:
1970                 kprintf("   bus: MCA+PCI, APIC: Integrated\n");
1971                 break;
1972         default:
1973                 kprintf("   future type\n");
1974                 break;
1975                 /* NOTREACHED */
1976         }
1977 #endif  /* 0 */
1978
1979         boot_cpu_id = (lapic.id & APIC_ID_MASK) >> 24;
1980         ap_cpu_id = (boot_cpu_id == 0) ? 1 : 0;
1981
1982         /* BSP */
1983         CPU_TO_ID(0) = boot_cpu_id;
1984         ID_TO_CPU(boot_cpu_id) = 0;
1985
1986         /* one and only AP */
1987         CPU_TO_ID(1) = ap_cpu_id;
1988         ID_TO_CPU(ap_cpu_id) = 1;
1989
1990 #if defined(APIC_IO)
1991         /* one and only IO APIC */
1992         io_apic_id = (io_apic_read(0, IOAPIC_ID) & APIC_ID_MASK) >> 24;
1993
1994         /*
1995          * sanity check, refer to MP spec section 3.6.6, last paragraph
1996          * necessary as some hardware isn't properly setting up the IO APIC
1997          */
1998 #if defined(REALLY_ANAL_IOAPICID_VALUE)
1999         if (io_apic_id != 2) {
2000 #else
2001         if ((io_apic_id == 0) || (io_apic_id == 1) || (io_apic_id == 15)) {
2002 #endif  /* REALLY_ANAL_IOAPICID_VALUE */
2003                 io_apic_set_id(0, 2);
2004                 io_apic_id = 2;
2005         }
2006         IO_TO_ID(0) = io_apic_id;
2007         ID_TO_IO(io_apic_id) = 0;
2008 #endif  /* APIC_IO */
2009
2010         /* fill out bus entries */
2011         switch (type) {
2012         case 1:
2013         case 2:
2014         case 3:
2015         case 4:
2016         case 5:
2017         case 6:
2018         case 7:
2019                 bus_data[0].bus_id = default_data[type - 1][1];
2020                 bus_data[0].bus_type = default_data[type - 1][2];
2021                 bus_data[1].bus_id = default_data[type - 1][3];
2022                 bus_data[1].bus_type = default_data[type - 1][4];
2023                 break;
2024
2025         /* case 4: case 7:                 MCA NOT supported */
2026         default:                /* illegal/reserved */
2027                 panic("BAD default MP config: %d", type);
2028                 /* NOTREACHED */
2029         }
2030
2031 #if defined(APIC_IO)
2032         /* general cases from MP v1.4, table 5-2 */
2033         for (pin = 0; pin < 16; ++pin) {
2034                 io_apic_ints[pin].int_type = 0;
2035                 io_apic_ints[pin].int_flags = 0x05;     /* edge/active-hi */
2036                 io_apic_ints[pin].src_bus_id = 0;
2037                 io_apic_ints[pin].src_bus_irq = pin;    /* IRQ2 caught below */
2038                 io_apic_ints[pin].dst_apic_id = io_apic_id;
2039                 io_apic_ints[pin].dst_apic_int = pin;   /* 1-to-1 */
2040         }
2041
2042         /* special cases from MP v1.4, table 5-2 */
2043         if (type == 2) {
2044                 io_apic_ints[2].int_type = 0xff;        /* N/C */
2045                 io_apic_ints[13].int_type = 0xff;       /* N/C */
2046 #if !defined(APIC_MIXED_MODE)
2047                 /** FIXME: ??? */
2048                 panic("sorry, can't support type 2 default yet");
2049 #endif  /* APIC_MIXED_MODE */
2050         }
2051         else
2052                 io_apic_ints[2].src_bus_irq = 0;        /* ISA IRQ0 is on APIC INT 2 */
2053
2054         if (type == 7)
2055                 io_apic_ints[0].int_type = 0xff;        /* N/C */
2056         else
2057                 io_apic_ints[0].int_type = 3;   /* vectored 8259 */
2058 #endif  /* APIC_IO */
2059 }
2060
2061 /*
2062  * Map a physical memory address representing I/O into KVA.  The I/O
2063  * block is assumed not to cross a page boundary.
2064  */
2065 void *
2066 permanent_io_mapping(vm_paddr_t pa)
2067 {
2068         vm_offset_t vaddr;
2069         int pgeflag;
2070         int i;
2071
2072         KKASSERT(pa < 0x100000000LL);
2073
2074         pgeflag = 0;    /* not used for SMP yet */
2075
2076         /*
2077          * If the requested physical address has already been incidently
2078          * mapped, just use the existing mapping.  Otherwise create a new
2079          * mapping.
2080          */
2081         for (i = IO_MAPPING_START_INDEX; i < SMPpt_alloc_index; ++i) {
2082                 if (((vm_offset_t)SMPpt[i] & PG_FRAME) ==
2083                     ((vm_offset_t)pa & PG_FRAME)) {
2084                         break;
2085                 }
2086         }
2087         if (i == SMPpt_alloc_index) {
2088                 if (i == NPTEPG - 2) {
2089                         panic("permanent_io_mapping: We ran out of space"
2090                               " in SMPpt[]!");
2091                 }
2092                 SMPpt[i] = (pt_entry_t)(PG_V | PG_RW | pgeflag |
2093                            ((vm_offset_t)pa & PG_FRAME));
2094                 ++SMPpt_alloc_index;
2095         }
2096         vaddr = (vm_offset_t)CPU_prvspace + (i * PAGE_SIZE) +
2097                 ((vm_offset_t)pa & PAGE_MASK);
2098         return ((void *)vaddr);
2099 }
2100
2101 /*
2102  * start each AP in our list
2103  */
2104 static int
2105 start_all_aps(u_int boot_addr)
2106 {
2107         int     x, i, pg;
2108         int     shift;
2109         u_char  mpbiosreason;
2110         u_long  mpbioswarmvec;
2111         struct mdglobaldata *gd;
2112         struct privatespace *ps;
2113         char *stack;
2114         uintptr_t kptbase;
2115
2116         POSTCODE(START_ALL_APS_POST);
2117
2118         /* Initialize BSP's local APIC */
2119         apic_initialize(TRUE);
2120         bsp_apic_ready = 1;
2121
2122         /* install the AP 1st level boot code */
2123         install_ap_tramp(boot_addr);
2124
2125
2126         /* save the current value of the warm-start vector */
2127         mpbioswarmvec = *((u_long *) WARMBOOT_OFF);
2128         outb(CMOS_REG, BIOS_RESET);
2129         mpbiosreason = inb(CMOS_DATA);
2130
2131         /* set up temporary P==V mapping for AP boot */
2132         /* XXX this is a hack, we should boot the AP on its own stack/PTD */
2133         kptbase = (uintptr_t)(void *)KPTphys;
2134         for (x = 0; x < NKPT; x++) {
2135                 PTD[x] = (pd_entry_t)(PG_V | PG_RW |
2136                     ((kptbase + x * PAGE_SIZE) & PG_FRAME));
2137         }
2138         cpu_invltlb();
2139
2140         /* start each AP */
2141         for (x = 1; x <= mp_naps; ++x) {
2142
2143                 /* This is a bit verbose, it will go away soon.  */
2144
2145                 /* first page of AP's private space */
2146                 pg = x * i386_btop(sizeof(struct privatespace));
2147
2148                 /* allocate new private data page(s) */
2149                 gd = (struct mdglobaldata *)kmem_alloc(&kernel_map, 
2150                                 MDGLOBALDATA_BASEALLOC_SIZE);
2151                 /* wire it into the private page table page */
2152                 for (i = 0; i < MDGLOBALDATA_BASEALLOC_SIZE; i += PAGE_SIZE) {
2153                         SMPpt[pg + i / PAGE_SIZE] = (pt_entry_t)
2154                             (PG_V | PG_RW | vtophys_pte((char *)gd + i));
2155                 }
2156                 pg += MDGLOBALDATA_BASEALLOC_PAGES;
2157
2158                 SMPpt[pg + 0] = 0;              /* *gd_CMAP1 */
2159                 SMPpt[pg + 1] = 0;              /* *gd_CMAP2 */
2160                 SMPpt[pg + 2] = 0;              /* *gd_CMAP3 */
2161                 SMPpt[pg + 3] = 0;              /* *gd_PMAP1 */
2162
2163                 /* allocate and set up an idle stack data page */
2164                 stack = (char *)kmem_alloc(&kernel_map, UPAGES*PAGE_SIZE);
2165                 for (i = 0; i < UPAGES; i++) {
2166                         SMPpt[pg + 4 + i] = (pt_entry_t)
2167                             (PG_V | PG_RW | vtophys_pte(PAGE_SIZE * i + stack));
2168                 }
2169
2170                 gd = &CPU_prvspace[x].mdglobaldata;     /* official location */
2171                 bzero(gd, sizeof(*gd));
2172                 gd->mi.gd_prvspace = ps = &CPU_prvspace[x];
2173
2174                 /* prime data page for it to use */
2175                 mi_gdinit(&gd->mi, x);
2176                 cpu_gdinit(gd, x);
2177                 gd->gd_CMAP1 = &SMPpt[pg + 0];
2178                 gd->gd_CMAP2 = &SMPpt[pg + 1];
2179                 gd->gd_CMAP3 = &SMPpt[pg + 2];
2180                 gd->gd_PMAP1 = &SMPpt[pg + 3];
2181                 gd->gd_CADDR1 = ps->CPAGE1;
2182                 gd->gd_CADDR2 = ps->CPAGE2;
2183                 gd->gd_CADDR3 = ps->CPAGE3;
2184                 gd->gd_PADDR1 = (unsigned *)ps->PPAGE1;
2185                 gd->mi.gd_ipiq = (void *)kmem_alloc(&kernel_map, sizeof(lwkt_ipiq) * (mp_naps + 1));
2186                 bzero(gd->mi.gd_ipiq, sizeof(lwkt_ipiq) * (mp_naps + 1));
2187
2188                 /* setup a vector to our boot code */
2189                 *((volatile u_short *) WARMBOOT_OFF) = WARMBOOT_TARGET;
2190                 *((volatile u_short *) WARMBOOT_SEG) = (boot_addr >> 4);
2191                 outb(CMOS_REG, BIOS_RESET);
2192                 outb(CMOS_DATA, BIOS_WARM);     /* 'warm-start' */
2193
2194                 /*
2195                  * Setup the AP boot stack
2196                  */
2197                 bootSTK = &ps->idlestack[UPAGES*PAGE_SIZE/2];
2198                 bootAP = x;
2199
2200                 /* attempt to start the Application Processor */
2201                 CHECK_INIT(99); /* setup checkpoints */
2202                 if (!start_ap(gd, boot_addr)) {
2203                         kprintf("AP #%d (PHY# %d) failed!\n", x, CPU_TO_ID(x));
2204                         CHECK_PRINT("trace");   /* show checkpoints */
2205                         /* better panic as the AP may be running loose */
2206                         kprintf("panic y/n? [y] ");
2207                         if (cngetc() != 'n')
2208                                 panic("bye-bye");
2209                 }
2210                 CHECK_PRINT("trace");           /* show checkpoints */
2211
2212                 /* record its version info */
2213                 cpu_apic_versions[x] = cpu_apic_versions[0];
2214         }
2215
2216         /* set ncpus to 1 + highest logical cpu.  Not all may have come up */
2217         ncpus = x;
2218
2219         /* ncpus2 -- ncpus rounded down to the nearest power of 2 */
2220         for (shift = 0; (1 << shift) <= ncpus; ++shift)
2221                 ;
2222         --shift;
2223         ncpus2_shift = shift;
2224         ncpus2 = 1 << shift;
2225         ncpus2_mask = ncpus2 - 1;
2226
2227         /* ncpus_fit -- ncpus rounded up to the nearest power of 2 */
2228         if ((1 << shift) < ncpus)
2229                 ++shift;
2230         ncpus_fit = 1 << shift;
2231         ncpus_fit_mask = ncpus_fit - 1;
2232
2233         /* build our map of 'other' CPUs */
2234         mycpu->gd_other_cpus = smp_startup_mask & ~(1 << mycpu->gd_cpuid);
2235         mycpu->gd_ipiq = (void *)kmem_alloc(&kernel_map, sizeof(lwkt_ipiq) * ncpus);
2236         bzero(mycpu->gd_ipiq, sizeof(lwkt_ipiq) * ncpus);
2237
2238         /* fill in our (BSP) APIC version */
2239         cpu_apic_versions[0] = lapic.version;
2240
2241         /* restore the warmstart vector */
2242         *(u_long *) WARMBOOT_OFF = mpbioswarmvec;
2243         outb(CMOS_REG, BIOS_RESET);
2244         outb(CMOS_DATA, mpbiosreason);
2245
2246         /*
2247          * NOTE!  The idlestack for the BSP was setup by locore.  Finish
2248          * up, clean out the P==V mapping we did earlier.
2249          */
2250         for (x = 0; x < NKPT; x++)
2251                 PTD[x] = 0;
2252         pmap_set_opt();
2253
2254         /* number of APs actually started */
2255         return ncpus - 1;
2256 }
2257
2258
2259 /*
2260  * load the 1st level AP boot code into base memory.
2261  */
2262
2263 /* targets for relocation */
2264 extern void bigJump(void);
2265 extern void bootCodeSeg(void);
2266 extern void bootDataSeg(void);
2267 extern void MPentry(void);
2268 extern u_int MP_GDT;
2269 extern u_int mp_gdtbase;
2270
2271 static void
2272 install_ap_tramp(u_int boot_addr)
2273 {
2274         int     x;
2275         int     size = *(int *) ((u_long) & bootMP_size);
2276         u_char *src = (u_char *) ((u_long) bootMP);
2277         u_char *dst = (u_char *) boot_addr + KERNBASE;
2278         u_int   boot_base = (u_int) bootMP;
2279         u_int8_t *dst8;
2280         u_int16_t *dst16;
2281         u_int32_t *dst32;
2282
2283         POSTCODE(INSTALL_AP_TRAMP_POST);
2284
2285         for (x = 0; x < size; ++x)
2286                 *dst++ = *src++;
2287
2288         /*
2289          * modify addresses in code we just moved to basemem. unfortunately we
2290          * need fairly detailed info about mpboot.s for this to work.  changes
2291          * to mpboot.s might require changes here.
2292          */
2293
2294         /* boot code is located in KERNEL space */
2295         dst = (u_char *) boot_addr + KERNBASE;
2296
2297         /* modify the lgdt arg */
2298         dst32 = (u_int32_t *) (dst + ((u_int) & mp_gdtbase - boot_base));
2299         *dst32 = boot_addr + ((u_int) & MP_GDT - boot_base);
2300
2301         /* modify the ljmp target for MPentry() */
2302         dst32 = (u_int32_t *) (dst + ((u_int) bigJump - boot_base) + 1);
2303         *dst32 = ((u_int) MPentry - KERNBASE);
2304
2305         /* modify the target for boot code segment */
2306         dst16 = (u_int16_t *) (dst + ((u_int) bootCodeSeg - boot_base));
2307         dst8 = (u_int8_t *) (dst16 + 1);
2308         *dst16 = (u_int) boot_addr & 0xffff;
2309         *dst8 = ((u_int) boot_addr >> 16) & 0xff;
2310
2311         /* modify the target for boot data segment */
2312         dst16 = (u_int16_t *) (dst + ((u_int) bootDataSeg - boot_base));
2313         dst8 = (u_int8_t *) (dst16 + 1);
2314         *dst16 = (u_int) boot_addr & 0xffff;
2315         *dst8 = ((u_int) boot_addr >> 16) & 0xff;
2316 }
2317
2318
2319 /*
2320  * this function starts the AP (application processor) identified
2321  * by the APIC ID 'physicalCpu'.  It does quite a "song and dance"
2322  * to accomplish this.  This is necessary because of the nuances
2323  * of the different hardware we might encounter.  It ain't pretty,
2324  * but it seems to work.
2325  *
2326  * NOTE: eventually an AP gets to ap_init(), which is called just 
2327  * before the AP goes into the LWKT scheduler's idle loop.
2328  */
2329 static int
2330 start_ap(struct mdglobaldata *gd, u_int boot_addr)
2331 {
2332         int     physical_cpu;
2333         int     vector;
2334         u_long  icr_lo, icr_hi;
2335
2336         POSTCODE(START_AP_POST);
2337
2338         /* get the PHYSICAL APIC ID# */
2339         physical_cpu = CPU_TO_ID(gd->mi.gd_cpuid);
2340
2341         /* calculate the vector */
2342         vector = (boot_addr >> 12) & 0xff;
2343
2344         /* Make sure the target cpu sees everything */
2345         wbinvd();
2346
2347         /*
2348          * first we do an INIT/RESET IPI this INIT IPI might be run, reseting
2349          * and running the target CPU. OR this INIT IPI might be latched (P5
2350          * bug), CPU waiting for STARTUP IPI. OR this INIT IPI might be
2351          * ignored.
2352          */
2353
2354         /* setup the address for the target AP */
2355         icr_hi = lapic.icr_hi & ~APIC_ID_MASK;
2356         icr_hi |= (physical_cpu << 24);
2357         lapic.icr_hi = icr_hi;
2358
2359         /* do an INIT IPI: assert RESET */
2360         icr_lo = lapic.icr_lo & 0xfff00000;
2361         lapic.icr_lo = icr_lo | 0x0000c500;
2362
2363         /* wait for pending status end */
2364         while (lapic.icr_lo & APIC_DELSTAT_MASK)
2365                  /* spin */ ;
2366
2367         /* do an INIT IPI: deassert RESET */
2368         lapic.icr_lo = icr_lo | 0x00008500;
2369
2370         /* wait for pending status end */
2371         u_sleep(10000);         /* wait ~10mS */
2372         while (lapic.icr_lo & APIC_DELSTAT_MASK)
2373                  /* spin */ ;
2374
2375         /*
2376          * next we do a STARTUP IPI: the previous INIT IPI might still be
2377          * latched, (P5 bug) this 1st STARTUP would then terminate
2378          * immediately, and the previously started INIT IPI would continue. OR
2379          * the previous INIT IPI has already run. and this STARTUP IPI will
2380          * run. OR the previous INIT IPI was ignored. and this STARTUP IPI
2381          * will run.
2382          */
2383
2384         /* do a STARTUP IPI */
2385         lapic.icr_lo = icr_lo | 0x00000600 | vector;
2386         while (lapic.icr_lo & APIC_DELSTAT_MASK)
2387                  /* spin */ ;
2388         u_sleep(200);           /* wait ~200uS */
2389
2390         /*
2391          * finally we do a 2nd STARTUP IPI: this 2nd STARTUP IPI should run IF
2392          * the previous STARTUP IPI was cancelled by a latched INIT IPI. OR
2393          * this STARTUP IPI will be ignored, as only ONE STARTUP IPI is
2394          * recognized after hardware RESET or INIT IPI.
2395          */
2396
2397         lapic.icr_lo = icr_lo | 0x00000600 | vector;
2398         while (lapic.icr_lo & APIC_DELSTAT_MASK)
2399                  /* spin */ ;
2400         u_sleep(200);           /* wait ~200uS */
2401
2402         /* wait for it to start, see ap_init() */
2403         set_apic_timer(5000000);/* == 5 seconds */
2404         while (read_apic_timer()) {
2405                 if (smp_startup_mask & (1 << gd->mi.gd_cpuid))
2406                         return 1;       /* return SUCCESS */
2407         }
2408         return 0;               /* return FAILURE */
2409 }
2410
2411
2412 /*
2413  * Lazy flush the TLB on all other CPU's.  DEPRECATED.
2414  *
2415  * If for some reason we were unable to start all cpus we cannot safely
2416  * use broadcast IPIs.
2417  */
2418 void
2419 smp_invltlb(void)
2420 {
2421 #ifdef SMP
2422         if (smp_startup_mask == smp_active_mask) {
2423                 all_but_self_ipi(XINVLTLB_OFFSET);
2424         } else {
2425                 selected_apic_ipi(smp_active_mask, XINVLTLB_OFFSET,
2426                         APIC_DELMODE_FIXED);
2427         }
2428 #endif
2429 }
2430
2431 /*
2432  * When called the executing CPU will send an IPI to all other CPUs
2433  *  requesting that they halt execution.
2434  *
2435  * Usually (but not necessarily) called with 'other_cpus' as its arg.
2436  *
2437  *  - Signals all CPUs in map to stop.
2438  *  - Waits for each to stop.
2439  *
2440  * Returns:
2441  *  -1: error
2442  *   0: NA
2443  *   1: ok
2444  *
2445  * XXX FIXME: this is not MP-safe, needs a lock to prevent multiple CPUs
2446  *            from executing at same time.
2447  */
2448 int
2449 stop_cpus(u_int map)
2450 {
2451         map &= smp_active_mask;
2452
2453         /* send the Xcpustop IPI to all CPUs in map */
2454         selected_apic_ipi(map, XCPUSTOP_OFFSET, APIC_DELMODE_FIXED);
2455         
2456         while ((stopped_cpus & map) != map)
2457                 /* spin */ ;
2458
2459         return 1;
2460 }
2461
2462
2463 /*
2464  * Called by a CPU to restart stopped CPUs. 
2465  *
2466  * Usually (but not necessarily) called with 'stopped_cpus' as its arg.
2467  *
2468  *  - Signals all CPUs in map to restart.
2469  *  - Waits for each to restart.
2470  *
2471  * Returns:
2472  *  -1: error
2473  *   0: NA
2474  *   1: ok
2475  */
2476 int
2477 restart_cpus(u_int map)
2478 {
2479         /* signal other cpus to restart */
2480         started_cpus = map & smp_active_mask;
2481
2482         while ((stopped_cpus & map) != 0) /* wait for each to clear its bit */
2483                 /* spin */ ;
2484
2485         return 1;
2486 }
2487
2488 /*
2489  * This is called once the mpboot code has gotten us properly relocated
2490  * and the MMU turned on, etc.   ap_init() is actually the idle thread,
2491  * and when it returns the scheduler will call the real cpu_idle() main
2492  * loop for the idlethread.  Interrupts are disabled on entry and should
2493  * remain disabled at return.
2494  */
2495 void
2496 ap_init(void)
2497 {
2498         u_int   apic_id;
2499
2500         /*
2501          * Adjust smp_startup_mask to signal the BSP that we have started
2502          * up successfully.  Note that we do not yet hold the BGL.  The BSP
2503          * is waiting for our signal.
2504          *
2505          * We can't set our bit in smp_active_mask yet because we are holding
2506          * interrupts physically disabled and remote cpus could deadlock
2507          * trying to send us an IPI.
2508          */
2509         smp_startup_mask |= 1 << mycpu->gd_cpuid;
2510         cpu_mfence();
2511
2512         /*
2513          * Interlock for finalization.  Wait until mp_finish is non-zero,
2514          * then get the MP lock.
2515          *
2516          * Note: We are in a critical section.
2517          *
2518          * Note: We have to synchronize td_mpcount to our desired MP state
2519          * before calling cpu_try_mplock().
2520          *
2521          * Note: we are the idle thread, we can only spin.
2522          *
2523          * Note: The load fence is memory volatile and prevents the compiler
2524          * from improperly caching mp_finish, and the cpu from improperly
2525          * caching it.
2526          */
2527         while (mp_finish == 0)
2528             cpu_lfence();
2529         ++curthread->td_mpcount;
2530         while (cpu_try_mplock() == 0)
2531             ;
2532
2533         if (cpu_feature & CPUID_TSC) {
2534             /*
2535              * The BSP is constantly updating tsc0_offset, figure out the
2536              * relative difference to synchronize ktrdump.
2537              */
2538             tsc_offsets[mycpu->gd_cpuid] = rdtsc() - tsc0_offset;
2539         }
2540
2541         /* BSP may have changed PTD while we're waiting for the lock */
2542         cpu_invltlb();
2543
2544 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
2545         lidt(&r_idt);
2546 #endif
2547
2548         /* Build our map of 'other' CPUs. */
2549         mycpu->gd_other_cpus = smp_startup_mask & ~(1 << mycpu->gd_cpuid);
2550
2551         kprintf("SMP: AP CPU #%d Launched!\n", mycpu->gd_cpuid);
2552
2553         /* A quick check from sanity claus */
2554         apic_id = (apic_id_to_logical[(lapic.id & 0x0f000000) >> 24]);
2555         if (mycpu->gd_cpuid != apic_id) {
2556                 kprintf("SMP: cpuid = %d\n", mycpu->gd_cpuid);
2557                 kprintf("SMP: apic_id = %d\n", apic_id);
2558                 kprintf("PTD[MPPTDI] = %p\n", (void *)PTD[MPPTDI]);
2559                 panic("cpuid mismatch! boom!!");
2560         }
2561
2562         /* Initialize AP's local APIC for irq's */
2563         apic_initialize(FALSE);
2564
2565         /* Set memory range attributes for this CPU to match the BSP */
2566         mem_range_AP_init();
2567
2568         /*
2569          * Once we go active we must process any IPIQ messages that may
2570          * have been queued, because no actual IPI will occur until we
2571          * set our bit in the smp_active_mask.  If we don't the IPI
2572          * message interlock could be left set which would also prevent
2573          * further IPIs.
2574          *
2575          * The idle loop doesn't expect the BGL to be held and while
2576          * lwkt_switch() normally cleans things up this is a special case
2577          * because we returning almost directly into the idle loop.
2578          *
2579          * The idle thread is never placed on the runq, make sure
2580          * nothing we've done put it there.
2581          */
2582         KKASSERT(curthread->td_mpcount == 1);
2583         smp_active_mask |= 1 << mycpu->gd_cpuid;
2584
2585         /*
2586          * Enable interrupts here.  idle_restore will also do it, but
2587          * doing it here lets us clean up any strays that got posted to
2588          * the CPU during the AP boot while we are still in a critical
2589          * section.
2590          */
2591         __asm __volatile("sti; pause; pause"::);
2592         mdcpu->gd_fpending = 0;
2593         mdcpu->gd_ipending = 0;
2594
2595         initclocks_pcpu();      /* clock interrupts (via IPIs) */
2596         lwkt_process_ipiq();
2597
2598         /*
2599          * Releasing the mp lock lets the BSP finish up the SMP init
2600          */
2601         rel_mplock();
2602         KKASSERT((curthread->td_flags & TDF_RUNQ) == 0);
2603 }
2604
2605 /*
2606  * Get SMP fully working before we start initializing devices.
2607  */
2608 static
2609 void
2610 ap_finish(void)
2611 {
2612         mp_finish = 1;
2613         if (bootverbose)
2614                 kprintf("Finish MP startup\n");
2615         if (cpu_feature & CPUID_TSC)
2616                 tsc0_offset = rdtsc();
2617         tsc_offsets[0] = 0;
2618         rel_mplock();
2619         while (smp_active_mask != smp_startup_mask) {
2620                 cpu_lfence();
2621                 if (cpu_feature & CPUID_TSC)
2622                         tsc0_offset = rdtsc();
2623         }
2624         while (try_mplock() == 0)
2625                 ;
2626         if (bootverbose)
2627                 kprintf("Active CPU Mask: %08x\n", smp_active_mask);
2628 }
2629
2630 SYSINIT(finishsmp, SI_BOOT2_FINISH_SMP, SI_ORDER_FIRST, ap_finish, NULL)
2631
2632 void
2633 cpu_send_ipiq(int dcpu)
2634 {
2635         if ((1 << dcpu) & smp_active_mask)
2636                 single_apic_ipi(dcpu, XIPIQ_OFFSET, APIC_DELMODE_FIXED);
2637 }
2638
2639 #if 0   /* single_apic_ipi_passive() not working yet */
2640 /*
2641  * Returns 0 on failure, 1 on success
2642  */
2643 int
2644 cpu_send_ipiq_passive(int dcpu)
2645 {
2646         int r = 0;
2647         if ((1 << dcpu) & smp_active_mask) {
2648                 r = single_apic_ipi_passive(dcpu, XIPIQ_OFFSET,
2649                                         APIC_DELMODE_FIXED);
2650         }
2651         return(r);
2652 }
2653 #endif
2654