mp_machdep.c revision 25361
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 *	$Id: mp_machdep.c,v 1.5 1997/04/29 22:12:32 fsmp Exp $
26 */
27
28#include "opt_smp.h"
29
30#define FIX_MP_TABLE_WORKS
31
32#include "opt_serial.h"
33
34#include <sys/param.h>		/* for KERNBASE */
35#include <sys/types.h>
36#include <sys/sysproto.h>
37#include <sys/time.h>
38#include <sys/systm.h>
39
40#include <vm/vm.h>		/* for KERNBASE */
41#include <vm/vm_param.h>	/* for KERNBASE */
42#include <vm/pmap.h>		/* for KERNBASE */
43#include <machine/pmap.h>	/* for KERNBASE */
44
45#include <machine/smp.h>
46#include <machine/apic.h>
47#include <machine/mpapic.h>
48#include <machine/cpufunc.h>
49#include <machine/segments.h>
50#include <machine/smptests.h>	/** TEST_DEFAULT_CONFIG */
51
52#include <i386/i386/cons.h>	/* cngetc() */
53
54#if defined(APIC_IO)
55#include <i386/include/md_var.h>	/* setidt() */
56#include <i386/isa/icu.h>		/* Xinvltlb() */
57#include <i386/isa/isa_device.h>	/* Xinvltlb() */
58#endif	/* APIC_IO */
59
60#define WARMBOOT_TARGET	0
61#define WARMBOOT_OFF	(KERNBASE + 0x0467)
62#define WARMBOOT_SEG	(KERNBASE + 0x0469)
63
64#define BIOS_BASE	(0xf0000)
65#define BIOS_SIZE	(0x10000)
66#define BIOS_COUNT	(BIOS_SIZE/4)
67
68#define CMOS_REG	(0x70)
69#define CMOS_DATA	(0x71)
70#define BIOS_RESET	(0x0f)
71#define BIOS_WARM	(0x0a)
72
73/*
74 * this code MUST be enabled here and in mpboot.s.
75 * it follows the very early stages of AP boot by placing values in CMOS ram.
76 * it NORMALLY will never be needed and thus the primitive method for enabling.
77 *
78#define CHECK_POINTS
79 */
80
81#if defined(CHECK_POINTS)
82#define CHECK_READ(A)	 (outb(CMOS_REG, (A)), inb(CMOS_DATA))
83#define CHECK_WRITE(A,D) (outb(CMOS_REG, (A)), outb(CMOS_DATA, (D)))
84
85#define CHECK_INIT(D);				\
86	CHECK_WRITE(0x34, (D));			\
87	CHECK_WRITE(0x35, (D));			\
88	CHECK_WRITE(0x36, (D));			\
89	CHECK_WRITE(0x37, (D));			\
90	CHECK_WRITE(0x38, (D));			\
91	CHECK_WRITE(0x39, (D));
92
93#define CHECK_PRINT(S);				\
94	printf("%s: %d, %d, %d, %d, %d, %d\n",	\
95	   (S),					\
96	   CHECK_READ(0x34),			\
97	   CHECK_READ(0x35),			\
98	   CHECK_READ(0x36),			\
99	   CHECK_READ(0x37),			\
100	   CHECK_READ(0x38),			\
101	   CHECK_READ(0x39));
102
103#else				/* CHECK_POINTS */
104
105#define CHECK_INIT(D)
106#define CHECK_PRINT(S)
107
108#endif				/* CHECK_POINTS */
109
110
111/** FIXME: what system files declare these??? */
112extern struct region_descriptor r_gdt, r_idt;
113
114/* global data */
115struct proc *SMPcurproc[NCPU];
116struct pcb *SMPcurpcb[NCPU];
117struct timeval SMPruntime[NCPU];
118
119int     mp_ncpus;		/* # of CPUs, including BSP */
120int     mp_naps;		/* # of Applications processors */
121int     mp_nbusses;		/* # of busses */
122int     mp_napics;		/* # of IO APICs */
123int     mpenabled;
124int     boot_cpu_id;		/* designated BSP */
125vm_offset_t cpu_apic_address;
126vm_offset_t io_apic_address[NAPIC];
127
128u_int32_t cpu_apic_versions[NCPU];
129u_int32_t io_apic_versions[NAPIC];
130
131/*
132 * APIC ID logical/physical mapping structures
133 */
134int     cpu_num_to_apic_id[NCPU];
135int     io_num_to_apic_id[NAPIC];
136int     apic_id_to_logical[NAPICID];
137
138/*
139 * look for MP compliant motherboard.
140 */
141
142static u_int boot_address;
143static u_int base_memory;
144
145static int picmode;		/* 0: virtual wire mode, 1: PIC mode */
146static u_int mpfps;
147static int search_for_sig(u_int32_t target, int count);
148static int mp_probe(u_int base_top);
149static void mp_enable(u_int boot_addr);
150
151
152/*
153 * calculate usable address in base memory for AP trampoline code
154 */
155u_int
156mp_bootaddress(u_int basemem)
157{
158	base_memory = basemem * 1024;	/* convert to bytes */
159
160	boot_address = base_memory & ~0xfff;	/* round down to 4k boundary */
161	if ((base_memory - boot_address) < bootMP_size)
162		boot_address -= 4096;	/* not enough, lower by 4k */
163
164	return boot_address;
165}
166
167
168/*
169 * startup the SMP processors
170 */
171void
172mp_start(void)
173{
174	/* look for MP capable motherboard */
175	if (mp_probe(base_memory))
176		mp_enable(boot_address);
177	else {
178		printf("MP FPS NOT FOUND, suggest use of 'mptable' program\n");
179		panic("can't continue!\n");
180	}
181
182	/* finish pmap initialization - turn off V==P mapping at zero */
183	pmap_bootstrap2();
184}
185
186
187/*
188 * print various information about the SMP system hardware and setup
189 */
190void
191mp_announce(void)
192{
193	int     x;
194
195	printf("FreeBSD/SMP: Multiprocessor motherboard\n");
196	printf(" cpu0 (BSP): apic id: %d", CPU_TO_ID(0));
197	printf(", version: 0x%08x\n", cpu_apic_versions[0]);
198	for (x = 1; x <= mp_naps; ++x) {
199		printf(" cpu%d (AP):  apic id: %d", x, CPU_TO_ID(x));
200		printf(", version: 0x%08x\n", cpu_apic_versions[x]);
201	}
202
203#if defined(APIC_IO)
204	for (x = 0; x < mp_napics; ++x) {
205		printf(" io%d (APIC): apic id: %d", x, IO_TO_ID(x));
206		printf(", version: 0x%08x\n", io_apic_versions[x]);
207	}
208#else
209	printf(" Warning: APIC I/O disabled\n");
210#endif	/* APIC_IO */
211}
212
213
214/*
215 * AP cpu's call this to sync up protected mode.
216 */
217void
218init_secondary(void)
219{
220	int     gsel_tss, slot;
221
222	r_gdt.rd_limit = sizeof(gdt[0]) * (NGDT + NCPU) - 1;
223	r_gdt.rd_base = (int) gdt;
224	lgdt(&r_gdt);		/* does magic intra-segment return */
225	lidt(&r_idt);
226	lldt(_default_ldt);
227
228	slot = NGDT + cpunumber();
229	gsel_tss = GSEL(slot, SEL_KPL);
230	gdt[slot].sd.sd_type = SDT_SYS386TSS;
231	ltr(gsel_tss);
232
233	load_cr0(0x8005003b);	/* XXX! */
234}
235
236
237#if defined(APIC_IO)
238void
239configure_local_apic(void)
240{
241	u_char  byte;
242	u_int32_t temp;
243
244	if (picmode) {
245		outb(0x22, 0x70);	/* select IMCR */
246		byte = inb(0x23);	/* current contents */
247		byte |= 0x01;	/* mask external INTR */
248		outb(0x23, byte);	/* disconnect 8259s/NMI */
249	}
250	/* mask the LVT1 */
251	temp = apic_base[APIC_LVT1];
252	temp |= APIC_LVT_M;
253	apic_base[APIC_LVT1] = temp;
254}
255#endif	/* APIC_IO */
256
257
258/*******************************************************************
259 * local functions and data
260 */
261
262static int
263mp_probe(u_int base_top)
264{
265	int     x;
266	u_long  segment;
267	u_int32_t target;
268
269	/* see if EBDA exists */
270	if (segment = (u_long) * (u_short *) (KERNBASE + 0x40e)) {
271		/* search first 1K of EBDA */
272		target = (u_int32_t) (segment << 4);
273		if ((x = search_for_sig(target, 1024 / 4)) >= 0)
274			goto found;
275	} else {
276		/*last 1K of base memory, effective 'top of base' is passed in*/
277		target = (u_int32_t) (base_top - 0x400);
278		if ((x = search_for_sig(target, 1024 / 4)) >= 0)
279			goto found;
280	}
281
282	/* search the BIOS */
283	target = (u_int32_t) BIOS_BASE;
284	if ((x = search_for_sig(target, BIOS_COUNT)) >= 0)
285		goto found;
286
287	/* nothing found */
288	mpfps = mpenabled = 0;
289	return 0;
290
291found:				/* please forgive the 'goto'! */
292	/* flag fact that we are running multiple processors */
293	mpfps = x;
294	mpenabled = 1;
295	return 1;
296}
297
298
299/*
300 * start the SMP system
301 */
302static int parse_mp_table(void);
303static void default_mp_table(int type);
304static int start_all_aps(u_int boot_addr);
305
306static void
307mp_enable(u_int boot_addr)
308{
309	int     x;
310#if defined(APIC_IO)
311	int     apic;
312	u_int   ux;
313#endif	/* APIC_IO */
314
315	/* examine the MP table for needed info */
316	x = parse_mp_table();
317
318	/* create pages for (address common) cpu APIC and each IO APIC */
319	pmap_bootstrap_apics();
320
321	/* can't process default configs till the CPU APIC is pmapped */
322	if (x)
323		default_mp_table(x);
324
325#if defined(APIC_IO)
326	/* fill the LOGICAL io_apic_versions table */
327	for (apic = 0; apic < mp_napics; ++apic) {
328		ux = io_apic_read(apic, IOAPIC_VER);
329		io_apic_versions[apic] = ux;
330	}
331
332	/* program each IO APIC in the system */
333	for (apic = 0; apic < mp_napics; ++apic)
334		if (io_apic_setup(apic) < 0)
335			panic("IO APIC setup failure\n");
336
337	/* install an inter-CPU IPI for TLB invalidation */
338	setidt(ICU_OFFSET + XINVLTLB_OFFSET, Xinvltlb,
339	       SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
340#endif	/* APIC_IO */
341
342	/* start each Application Processor */
343	start_all_aps(boot_addr);
344}
345
346
347/*
348 * look for the MP spec signature
349 */
350
351/* string defined by the Intel MP Spec as identifying the MP table */
352#define MP_SIG		0x5f504d5f	/* _MP_ */
353#define NEXT(X)		((X) += 4)
354static int
355search_for_sig(u_int32_t target, int count)
356{
357	int     x;
358	u_int32_t *addr = (u_int32_t *) (KERNBASE + target);
359
360	for (x = 0; x < count; NEXT(x))
361		if (addr[x] == MP_SIG)
362			/* make array index a byte index */
363			return (target + (x * sizeof(u_int32_t)));
364
365	return -1;
366}
367
368
369#define PROCENTRY_FLAG_EN	0x01
370#define PROCENTRY_FLAG_BP	0x02
371#define IOAPICENTRY_FLAG_EN	0x01
372
373/* MP Floating Pointer Structure */
374typedef struct MPFPS {
375	char    signature[4];
376	void   *pap;
377	u_char  length;
378	u_char  spec_rev;
379	u_char  checksum;
380	u_char  mpfb1;
381	u_char  mpfb2;
382	u_char  mpfb3;
383	u_char  mpfb4;
384	u_char  mpfb5;
385}      *mpfps_t;
386/* MP Configuration Table Header */
387typedef struct MPCTH {
388	char    signature[4];
389	u_short base_table_length;
390	u_char  spec_rev;
391	u_char  checksum;
392	u_char  oem_id[8];
393	u_char  product_id[12];
394	void   *oem_table_pointer;
395	u_short oem_table_size;
396	u_short entry_count;
397	void   *apic_address;
398	u_short extended_table_length;
399	u_char  extended_table_checksum;
400	u_char  reserved;
401}      *mpcth_t;
402
403
404typedef struct PROCENTRY {
405	u_char  type;
406	u_char  apic_id;
407	u_char  apic_version;
408	u_char  cpu_flags;
409	u_long  cpu_signature;
410	u_long  feature_flags;
411	u_long  reserved1;
412	u_long  reserved2;
413}      *proc_entry_ptr;
414
415typedef struct BUSENTRY {
416	u_char  type;
417	u_char  bus_id;
418	char    bus_type[6];
419}      *bus_entry_ptr;
420
421typedef struct IOAPICENTRY {
422	u_char  type;
423	u_char  apic_id;
424	u_char  apic_version;
425	u_char  apic_flags;
426	void   *apic_address;
427}      *io_apic_entry_ptr;
428
429typedef struct INTENTRY {
430	u_char  type;
431	u_char  int_type;
432	u_short int_flags;
433	u_char  src_bus_id;
434	u_char  src_bus_irq;
435	u_char  dst_apic_id;
436	u_char  dst_apic_int;
437}      *int_entry_ptr;
438/* descriptions of MP basetable entries */
439typedef struct BASETABLE_ENTRY {
440	u_char  type;
441	u_char  length;
442	char    name[16];
443}       basetable_entry;
444
445static basetable_entry basetable_entry_types[] =
446{
447	{0, 20, "Processor"},
448	{1, 8, "Bus"},
449	{2, 8, "I/O APIC"},
450	{3, 8, "I/O INT"},
451	{4, 8, "Local INT"}
452};
453
454typedef struct BUSDATA {
455	u_char  bus_id;
456	enum busTypes bus_type;
457}       bus_datum;
458
459typedef struct INTDATA {
460	u_char  int_type;
461	u_short int_flags;
462	u_char  src_bus_id;
463	u_char  src_bus_irq;
464	u_char  dst_apic_id;
465	u_char  dst_apic_int;
466}       io_int, local_int;
467
468typedef struct BUSTYPENAME {
469	u_char  type;
470	char    name[7];
471}       bus_type_name;
472
473static bus_type_name bus_type_table[] =
474{
475	{CBUS, "CBUS"},
476	{CBUSII, "CBUSII"},
477	{EISA, "EISA"},
478	{UNKNOWN_BUSTYPE, "---"},
479	{UNKNOWN_BUSTYPE, "---"},
480	{ISA, "ISA"},
481	{UNKNOWN_BUSTYPE, "---"},
482	{UNKNOWN_BUSTYPE, "---"},
483	{UNKNOWN_BUSTYPE, "---"},
484	{UNKNOWN_BUSTYPE, "---"},
485	{UNKNOWN_BUSTYPE, "---"},
486	{UNKNOWN_BUSTYPE, "---"},
487	{PCI, "PCI"},
488	{UNKNOWN_BUSTYPE, "---"},
489	{UNKNOWN_BUSTYPE, "---"},
490	{UNKNOWN_BUSTYPE, "---"},
491	{UNKNOWN_BUSTYPE, "---"},
492	{XPRESS, "XPRESS"},
493	{UNKNOWN_BUSTYPE, "---"}
494};
495/* from MP spec v1.4, table 5-1 */
496static int default_data[7][5] =
497{
498/*   nbus, id0, type0, id1, type1 */
499	{1, 0, ISA, 255, 255},
500	{1, 0, EISA, 255, 255},
501	{1, 0, EISA, 255, 255},
502	{0, 255, 255, 255, 255},/* MCA not supported */
503	{2, 0, ISA, 1, PCI},
504	{2, 0, EISA, 1, PCI},
505	{0, 255, 255, 255, 255}	/* MCA not supported */
506};
507
508
509/* the bus data */
510bus_datum bus_data[NBUS];
511
512/* the IO INT data, one entry per possible APIC INTerrupt */
513io_int  io_apic_ints[NINTR];
514
515static int nintrs;
516
517#if defined(FIX_MP_TABLE_WORKS)
518static void fix_mp_table __P((void));
519#endif /* FIX_MP_TABLE_WORKS */
520
521static void processor_entry __P((proc_entry_ptr entry, int *cpu));
522static void io_apic_entry __P((io_apic_entry_ptr entry, int *apic));
523static void bus_entry __P((bus_entry_ptr entry, int *bus));
524static void int_entry __P((int_entry_ptr entry, int *intr));
525static int lookup_bus_type __P((char *name));
526
527
528/*
529 * parse an Intel MP specification table
530 */
531static int
532parse_mp_table(void)
533{
534	int     x;
535	mpfps_t fps;
536	mpcth_t cth;
537	int     totalSize;
538	void   *position;
539	int     count;
540	int     type;
541	int     apic, bus, cpu, intr;
542
543	/* clear physical APIC ID to logical CPU/IO table */
544	for (x = 0; x < NAPICID; ++x)
545		ID_TO_IO(x) = -1;
546
547	/* clear logical CPU to APIC ID table */
548	for (x = 0; x < NCPU; ++x)
549		CPU_TO_ID(x) = -1;
550
551	/* clear logical IO to APIC ID table */
552	for (x = 0; x < NAPIC; ++x)
553		IO_TO_ID(x) = -1;
554
555	/* clear IO APIC address table */
556	for (x = 0; x < NAPIC; ++x)
557		io_apic_address[x] = ~0;
558
559	/* clear bus data table */
560	for (x = 0; x < NBUS; ++x)
561		bus_data[x].bus_id = 0xff;
562
563	/* clear IO APIC INT table */
564	for (x = 0; x < NINTR; ++x)
565		io_apic_ints[x].int_type = 0xff;
566	nintrs = 0;
567
568	/* count the BSP */
569	mp_ncpus = 1;
570
571	/* setup the cpu/apic mapping arrays */
572	boot_cpu_id = -1;
573
574	/* local pointer */
575	fps = (mpfps_t) mpfps;
576
577	/* record whether PIC or virtual-wire mode */
578	picmode = (fps->mpfb2 & 0x80) ? 1 : 0;
579
580	/* check for use of 'default' configuration */
581#if defined(TEST_DEFAULT_CONFIG)
582	/* use default addresses */
583	cpu_apic_address = DEFAULT_APIC_BASE;
584	io_apic_address[0] = DEFAULT_IO_APIC_BASE;
585
586	/* return default configuration type */
587	return TEST_DEFAULT_CONFIG;
588#else
589	if (fps->mpfb1 != 0) {
590		/* use default addresses */
591		cpu_apic_address = DEFAULT_APIC_BASE;
592		io_apic_address[0] = DEFAULT_IO_APIC_BASE;
593
594		/* return default configuration type */
595		return fps->mpfb1;
596	}
597#endif	/* TEST_DEFAULT_CONFIG */
598
599	if ((cth = fps->pap) == 0)
600		panic("MP Configuration Table Header MISSING!\n");
601
602	cpu_apic_address = (vm_offset_t) cth->apic_address;
603
604	totalSize = cth->base_table_length - sizeof(struct MPCTH);
605	position = (u_char *) cth + sizeof(struct MPCTH);
606	count = cth->entry_count;
607
608	apic = 0;		/* logical apic# start @ 0 */
609	bus = 0;		/* logical bus# start @ 0 */
610	cpu = 1;		/* logical cpu# start @ 0, BUT reserve 0 for */
611				/* BSP */
612	intr = 0;		/* unknown */
613
614	/* walk the table, recording info of interest */
615	while (count--) {
616		switch (type = *(u_char *) position) {
617		case 0:
618			processor_entry(position, &cpu);
619			break;
620		case 1:
621			bus_entry(position, &bus);
622			break;
623		case 2:
624			io_apic_entry(position, &apic);
625			break;
626		case 3:
627			int_entry(position, &intr);
628			break;
629		case 4:
630			/* int_entry(position); */
631			break;
632		default:
633			panic("mpfps Base Table HOSED!\n");
634			/* NOTREACHED */
635		}
636
637		totalSize -= basetable_entry_types[type].length;
638		(u_char *) position += basetable_entry_types[type].length;
639	}
640
641	if (boot_cpu_id == -1)
642		panic("NO BSP found!\n");
643
644	/* record # of APs found */
645	mp_naps = (cpu - 1);
646
647	/* record # of busses found */
648	mp_nbusses = bus;
649
650	/* record # of IO APICs found */
651	mp_napics = apic;
652
653	/* record # of IO APICs found */
654	nintrs = intr;
655
656#if defined(FIX_MP_TABLE_WORKS)
657	/* post scan cleanup */
658	fix_mp_table();
659#endif /* FIX_MP_TABLE_WORKS */
660
661	/* report fact that its NOT a default configuration */
662	return 0;
663}
664
665
666/*
667 * parse an Intel MP specification table
668 */
669#if defined(FIX_MP_TABLE_WORKS)
670static void
671fix_mp_table(void)
672{
673	int	x;
674	int	id;
675	int	bus_0;
676	int	bus_pci;
677	int	num_pci_bus;
678
679	/*
680	 * Fix mis-numbering of the PCI bus and its INT entries if the BIOS
681	 * did it wrong.  The MP spec says that when more than 1 PCI bus
682	 * exists the BIOS must begin with bus entries for the PCI bus and use
683	 * actual PCI bus numbering.  This implies that when only 1 PCI bus
684	 * exists the BIOS can choose to ignore this ordering, and indeed many
685	 * MP motherboards do ignore it.  This causes a problem when the PCI
686	 * sub-system makes requests of the MP sub-system based on PCI bus
687	 * numbers.	So here we look for the situation and renumber the
688	 * busses and associated INTs in an effort to "make it right".
689	 */
690
691	/* find bus 0, PCI bus, count the number of PCI busses */
692	for (num_pci_bus = 0, x = 0; x < mp_nbusses; ++x) {
693		if (bus_data[x].bus_id == 0) {
694			bus_0 = x;
695		}
696		if (bus_data[x].bus_type == PCI) {
697			++num_pci_bus;
698			bus_pci = x;
699		}
700	}
701	/*
702	 * bus_0 == slot of bus with ID of 0
703	 * bus_pci == slot of last PCI bus encountered
704	 */
705
706	/* check the 1 PCI bus case for sanity */
707	if (num_pci_bus == 1) {
708
709		/* if it is number 0 all is well */
710		if (bus_data[bus_pci].bus_id == 0)
711			return;
712
713		/* mis-numbered, swap with whichever bus uses slot 0 */
714
715		/* swap the bus entry types */
716		bus_data[bus_pci].bus_type = bus_data[bus_0].bus_type;
717		bus_data[bus_0].bus_type = PCI;
718
719		/* swap each relavant INTerrupt entry */
720		id = bus_data[bus_pci].bus_id;
721		for (x = 0; x < nintrs; ++x) {
722			if (io_apic_ints[x].src_bus_id == id) {
723				io_apic_ints[x].src_bus_id = 0;
724			}
725			else if (io_apic_ints[x].src_bus_id == 0) {
726				io_apic_ints[x].src_bus_id = id;
727			}
728		}
729	}
730	/* sanity check if more than 1 PCI bus */
731	else if (num_pci_bus > 1) {
732		for (x = 0; x < mp_nbusses; ++x) {
733			if (bus_data[x].bus_type != PCI)
734				continue;
735			if (bus_data[x].bus_id >= num_pci_bus ) {
736				printf("bad PCI bus numbering\n");
737				panic("\n");
738			}
739		}
740	}
741}
742#endif /* FIX_MP_TABLE_WORKS */
743
744
745static void
746processor_entry(proc_entry_ptr entry, int *cpu)
747{
748	int     x = *cpu;
749
750	/* check for usability */
751	if (!(entry->cpu_flags & PROCENTRY_FLAG_EN))
752		return;
753
754	/* check for BSP flag */
755	if (entry->cpu_flags & PROCENTRY_FLAG_BP) {
756		/* always give boot CPU the logical value of 0 */
757		x = 0;
758		boot_cpu_id = entry->apic_id;
759	} else {
760		/* add another AP to list, if less than max number of CPUs */
761		if (x == NCPU) {
762			printf("Warning: only using %d of the available CPUs!\n", x);
763			return;
764		}
765		++(*cpu);
766	}
767
768	CPU_TO_ID(x) = entry->apic_id;
769	ID_TO_CPU(entry->apic_id) = x;
770}
771
772
773static void
774bus_entry(bus_entry_ptr entry, int *bus)
775{
776	int     x, y;
777	char    name[8];
778	char    c;
779
780	if ((x = (*bus)++) == NBUS)
781		panic("too many busses, increase 'NBUS'\n");
782
783	/* encode the name into an index */
784	for (y = 0; y < 6; ++y) {
785		if ((c = entry->bus_type[y]) == ' ')
786			break;
787		name[y] = c;
788	}
789	name[y] = '\0';
790
791	if ((y = lookup_bus_type(name)) == UNKNOWN_BUSTYPE)
792		panic("unknown bus type: '%s'\n", name);
793
794	bus_data[x].bus_id = entry->bus_id;
795	bus_data[x].bus_type = y;
796}
797
798
799static void
800io_apic_entry(io_apic_entry_ptr entry, int *apic)
801{
802	int     x;
803
804	if (!(entry->apic_flags & IOAPICENTRY_FLAG_EN))
805		return;
806
807	if ((x = (*apic)++) == NAPIC)
808		panic("too many APICs, increase 'NAPIC'\n");
809
810	IO_TO_ID(x) = entry->apic_id;
811	ID_TO_IO(entry->apic_id) = x;
812
813	io_apic_address[x] = (vm_offset_t) entry->apic_address;
814}
815
816
817static int
818lookup_bus_type(char *name)
819{
820	int     x;
821
822	for (x = 0; x < MAX_BUSTYPE; ++x)
823		if (strcmp(bus_type_table[x].name, name) == 0)
824			return bus_type_table[x].type;
825
826	return UNKNOWN_BUSTYPE;
827}
828
829
830static void
831int_entry(int_entry_ptr entry, int *intr)
832{
833	int     x;
834
835	if ((x = (*intr)++) == NINTR)
836		panic("too many INTs, increase 'NINTR'\n");
837
838	io_apic_ints[x].int_type = entry->int_type;
839	io_apic_ints[x].int_flags = entry->int_flags;
840	io_apic_ints[x].src_bus_id = entry->src_bus_id;
841	io_apic_ints[x].src_bus_irq = entry->src_bus_irq;
842	io_apic_ints[x].dst_apic_id = entry->dst_apic_id;
843	io_apic_ints[x].dst_apic_int = entry->dst_apic_int;
844}
845
846
847static int
848apic_int_is_bus_type(int intr, int bus_type)
849{
850	int     bus;
851
852	for (bus = 0; bus < mp_nbusses; ++bus)
853		if ((bus_data[bus].bus_id == io_apic_ints[intr].src_bus_id)
854		    && ((int) bus_data[bus].bus_type == bus_type))
855			return 1;
856
857	return 0;
858}
859
860
861/*
862 * determine which APIC pin an ISA INT is attached to.
863 */
864#define INTTYPE(I)	(io_apic_ints[(I)].int_type)
865#define INTPIN(I)	(io_apic_ints[(I)].dst_apic_int)
866
867#define SRCBUSIRQ(I)	(io_apic_ints[(I)].src_bus_irq)
868int
869get_isa_apic_irq(int isaIRQ)
870{
871	int     intr;
872
873#if defined(SMP_TIMER_NC)
874	if (isaIRQ == 0)
875		return -1;
876#endif				/* SMP_TIMER_NC */
877
878	for (intr = 0; intr < nintrs; ++intr)	/* search each INT record */
879		if ((INTTYPE(intr) == 0)
880		    && (SRCBUSIRQ(intr) == isaIRQ))	/* a candidate IRQ */
881			if (apic_int_is_bus_type(intr, ISA))	/* check bus match */
882				return INTPIN(intr);	/* exact match */
883
884	return -1;		/* NOT found */
885}
886#undef SRCBUSIRQ
887
888
889/*
890 * determine which APIC pin an EISA INT is attached to.
891 */
892#define SRCBUSIRQ(I)	(io_apic_ints[(I)].src_bus_irq)
893int
894get_eisa_apic_irq(int eisaIRQ)
895{
896	int     intr;
897
898#if defined(SMP_TIMER_NC)
899	if (eisaIRQ == 0)
900		return -1;
901#endif				/* SMP_TIMER_NC */
902
903	for (intr = 0; intr < nintrs; ++intr)	/* search each INT record */
904		if ((INTTYPE(intr) == 0)
905		    && (SRCBUSIRQ(intr) == eisaIRQ))	/* a candidate IRQ */
906			if (apic_int_is_bus_type(intr, EISA))	/* check bus match */
907				return INTPIN(intr);	/* exact match */
908
909	return -1;		/* NOT found */
910}
911#undef SRCBUSIRQ
912
913
914/*
915 * determine which APIC pin a PCI INT is attached to.
916 */
917#define SRCBUSID(I)	(io_apic_ints[(I)].src_bus_id)
918#define SRCBUSDEVICE(I)	((io_apic_ints[(I)].src_bus_irq >> 2) & 0x1f)
919#define SRCBUSLINE(I)	(io_apic_ints[(I)].src_bus_irq & 0x03)
920int
921get_pci_apic_irq(int pciBus, int pciDevice, int pciInt)
922{
923	int     intr;
924
925	--pciInt;		/* zero based */
926
927	for (intr = 0; intr < nintrs; ++intr)	/* search each record */
928		if ((INTTYPE(intr) == 0)
929#if defined(FIX_MP_TABLE_WORKS)
930		    && (SRCBUSID(intr) == pciBus)
931#endif /* FIX_MP_TABLE_WORKS */
932		    && (SRCBUSDEVICE(intr) == pciDevice)
933		    && (SRCBUSLINE(intr) == pciInt))	/* a candidate IRQ */
934			if (apic_int_is_bus_type(intr, PCI))	/* check bus match */
935				return INTPIN(intr);	/* exact match */
936
937	return -1;		/* NOT found */
938}
939#undef SRCBUSLINE
940#undef SRCBUSDEVICE
941#undef SRCBUSID
942
943#undef INTPIN
944#undef INTTYPE
945
946
947int
948undirect_pci_irq(int rirq)
949{
950#if defined(READY)
951	printf("Freeing irq %d for ISA cards.\n", rirq);
952	/** FIXME: tickle the MB redirector chip */
953	return ???;
954#else
955	printf("Freeing (NOT implemented) irq %d for ISA cards.\n", rirq);
956	return 0;
957#endif				/* READY */
958}
959
960
961/*
962 * given a bus ID, return:
963 *  the bus type if found
964 *  -1 if NOT found
965 */
966int
967apic_bus_type(int id)
968{
969	int     x;
970
971	for (x = 0; x < mp_nbusses; ++x)
972		if (bus_data[x].bus_id == id)
973			return bus_data[x].bus_type;
974
975	return -1;
976}
977
978
979/*
980 * given a LOGICAL APIC# and pin#, return:
981 *  the associated src bus ID if found
982 *  -1 if NOT found
983 */
984int
985apic_src_bus_id(int apic, int pin)
986{
987	int     x;
988
989	/* search each of the possible INTerrupt sources */
990	for (x = 0; x < nintrs; ++x)
991		if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
992		    (pin == io_apic_ints[x].dst_apic_int))
993			return (io_apic_ints[x].src_bus_id);
994
995	return -1;		/* NOT found */
996}
997
998
999/*
1000 * given a LOGICAL APIC# and pin#, return:
1001 *  the associated src bus IRQ if found
1002 *  -1 if NOT found
1003 */
1004int
1005apic_src_bus_irq(int apic, int pin)
1006{
1007	int     x;
1008
1009	for (x = 0; x < nintrs; x++)
1010		if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
1011		    (pin == io_apic_ints[x].dst_apic_int))
1012			return (io_apic_ints[x].src_bus_irq);
1013
1014	return -1;		/* NOT found */
1015}
1016
1017
1018/*
1019 * given a LOGICAL APIC# and pin#, return:
1020 *  the associated INTerrupt type if found
1021 *  -1 if NOT found
1022 */
1023int
1024apic_int_type(int apic, int pin)
1025{
1026	int     x;
1027
1028	/* search each of the possible INTerrupt sources */
1029	for (x = 0; x < nintrs; ++x)
1030		if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
1031		    (pin == io_apic_ints[x].dst_apic_int))
1032			return (io_apic_ints[x].int_type);
1033
1034	return -1;		/* NOT found */
1035}
1036
1037
1038/*
1039 * given a LOGICAL APIC# and pin#, return:
1040 *  the associated trigger mode if found
1041 *  -1 if NOT found
1042 */
1043int
1044apic_trigger(int apic, int pin)
1045{
1046	int     x;
1047
1048	/* search each of the possible INTerrupt sources */
1049	for (x = 0; x < nintrs; ++x)
1050		if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
1051		    (pin == io_apic_ints[x].dst_apic_int))
1052			return ((io_apic_ints[x].int_flags >> 2) & 0x03);
1053
1054	return -1;		/* NOT found */
1055}
1056
1057
1058/*
1059 * given a LOGICAL APIC# and pin#, return:
1060 *  the associated 'active' level if found
1061 *  -1 if NOT found
1062 */
1063int
1064apic_polarity(int apic, int pin)
1065{
1066	int     x;
1067
1068	/* search each of the possible INTerrupt sources */
1069	for (x = 0; x < nintrs; ++x)
1070		if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
1071		    (pin == io_apic_ints[x].dst_apic_int))
1072			return (io_apic_ints[x].int_flags & 0x03);
1073
1074	return -1;		/* NOT found */
1075}
1076
1077
1078/*
1079 * set data according to MP defaults
1080 * FIXME: probably not complete yet...
1081 */
1082static void
1083default_mp_table(int type)
1084{
1085	int     ap_cpu_id;
1086#if defined(APIC_IO)
1087	u_int32_t ux;
1088	int     io_apic_id;
1089	int     pin;
1090#endif	/* APIC_IO */
1091
1092#if 0
1093	printf("  MP default config type: %d\n", type);
1094	switch (type) {
1095	case 1:
1096		printf("   bus: ISA, APIC: 82489DX\n");
1097		break;
1098	case 2:
1099		printf("   bus: EISA, APIC: 82489DX\n");
1100		break;
1101	case 3:
1102		printf("   bus: EISA, APIC: 82489DX\n");
1103		break;
1104	case 4:
1105		printf("   bus: MCA, APIC: 82489DX\n");
1106		break;
1107	case 5:
1108		printf("   bus: ISA+PCI, APIC: Integrated\n");
1109		break;
1110	case 6:
1111		printf("   bus: EISA+PCI, APIC: Integrated\n");
1112		break;
1113	case 7:
1114		printf("   bus: MCA+PCI, APIC: Integrated\n");
1115		break;
1116	default:
1117		printf("   future type\n");
1118		break;
1119		/* NOTREACHED */
1120	}
1121#endif	/* 0 */
1122
1123	boot_cpu_id = (apic_base[APIC_ID] & APIC_ID_MASK) >> 24;
1124	ap_cpu_id = (boot_cpu_id == 0) ? 1 : 0;
1125
1126	/* BSP */
1127	CPU_TO_ID(0) = boot_cpu_id;
1128	ID_TO_CPU(boot_cpu_id) = 0;
1129
1130	/* one and only AP */
1131	CPU_TO_ID(1) = ap_cpu_id;
1132	ID_TO_CPU(ap_cpu_id) = 1;
1133	mp_naps = 1;
1134
1135	/* one and only IO APIC */
1136#if defined(APIC_IO)
1137	io_apic_id = (io_apic_read(0, IOAPIC_ID) & APIC_ID_MASK) >> 24;
1138
1139	/*
1140	 * sanity check, refer to MP spec section 3.6.6, last paragraph
1141	 * necessary as some hardware isn't properly setting up the IO APIC
1142	 */
1143#if defined(REALLY_ANAL_IOAPICID_VALUE)
1144	if (io_apic_id != 2) {
1145#else
1146	if ((io_apic_id == 0) || (io_apic_id == 1) || (io_apic_id == 15)) {
1147#endif	/* REALLY_ANAL_IOAPICID_VALUE */
1148		ux = io_apic_read(0, IOAPIC_ID);	/* get current contents */
1149		ux &= ~APIC_ID_MASK;	/* clear the ID field */
1150		ux |= 0x02000000;	/* set it to '2' */
1151		io_apic_write(0, IOAPIC_ID, ux);	/* write new value */
1152		ux = io_apic_read(0, IOAPIC_ID);	/* re-read && test */
1153		if ((ux & APIC_ID_MASK) != 0x02000000)
1154			panic("Problem: can't control IO APIC ID, reg: 0x%08x\n", ux);
1155		io_apic_id = 2;
1156	}
1157	IO_TO_ID(0) = io_apic_id;
1158	ID_TO_IO(io_apic_id) = 0;
1159	mp_napics = 1;
1160#else
1161	mp_napics = 0;
1162#endif	/* APIC_IO */
1163
1164	/* fill out bus entries */
1165	switch (type) {
1166	case 1:
1167	case 2:
1168	case 3:
1169	case 5:
1170	case 6:
1171		mp_nbusses = default_data[type - 1][0];
1172		bus_data[0].bus_id = default_data[type - 1][1];
1173		bus_data[0].bus_type = default_data[type - 1][2];
1174		bus_data[1].bus_id = default_data[type - 1][3];
1175		bus_data[1].bus_type = default_data[type - 1][4];
1176		break;
1177
1178	/* case 4: case 7:		   MCA NOT supported */
1179	default:		/* illegal/reserved */
1180		panic("BAD default MP config: %d\n", type);
1181	}
1182
1183#if defined(APIC_IO)
1184	/* general cases from MP v1.4, table 5-2 */
1185	for (pin = 0; pin < 16; ++pin) {
1186		io_apic_ints[pin].int_type = 0;
1187		io_apic_ints[pin].int_flags = 0x05;	/* edge-triggered/active-hi */
1188		io_apic_ints[pin].src_bus_id = 0;
1189		io_apic_ints[pin].src_bus_irq = pin;	/* IRQ2 is caught below */
1190		io_apic_ints[pin].dst_apic_id = io_apic_id;
1191		io_apic_ints[pin].dst_apic_int = pin;	/* 1-to-1 correspondence */
1192	}
1193
1194	/* special cases from MP v1.4, table 5-2 */
1195	if (type == 2) {
1196		io_apic_ints[2].int_type = 0xff;	/* N/C */
1197		io_apic_ints[13].int_type = 0xff;	/* N/C */
1198#if !defined(APIC_MIXED_MODE)
1199		/** FIXME: ??? */
1200		panic("sorry, can't support type 2 default yet\n");
1201#endif	/* APIC_MIXED_MODE */
1202	} else
1203		io_apic_ints[2].src_bus_irq = 0;	/* ISA IRQ0 is on APIC INT 2 */
1204
1205	if (type == 7)
1206		io_apic_ints[0].int_type = 0xff;	/* N/C */
1207	else
1208		io_apic_ints[0].int_type = 3;	/* vectored 8259 */
1209
1210	nintrs = 16;
1211#endif	/* APIC_IO */
1212}
1213
1214
1215static void install_ap_tramp(u_int boot_addr);
1216static int start_ap(int logicalCpu, u_int boot_addr);
1217
1218/*
1219 * start each AP in our list
1220 */
1221static int
1222start_all_aps(u_int boot_addr)
1223{
1224	int     x;
1225	u_char  mpbiosreason;
1226	u_long  mpbioswarmvec;
1227
1228	/**
1229         * NOTE: this needs further thought:
1230         *        where does it get released?
1231         *        should it be set to empy?
1232         *
1233         * get the initial mp_lock with a count of 1 for the BSP
1234         */
1235	mp_lock = (apic_base[APIC_ID] & APIC_ID_MASK) + 1;
1236
1237	/* initialize BSP's local APIC */
1238	apic_initialize(1);
1239
1240	/* install the AP 1st level boot code */
1241	install_ap_tramp(boot_addr);
1242
1243	/* save the current value of the warm-start vector */
1244	mpbioswarmvec = *((u_long *) WARMBOOT_OFF);
1245	outb(CMOS_REG, BIOS_RESET);
1246	mpbiosreason = inb(CMOS_DATA);
1247
1248	/* start each AP */
1249	for (x = 1; x <= mp_naps; ++x) {
1250
1251		/* setup a vector to our boot code */
1252		*((volatile u_short *) WARMBOOT_OFF) = WARMBOOT_TARGET;
1253		*((volatile u_short *) WARMBOOT_SEG) = (boot_addr >> 4);
1254		outb(CMOS_REG, BIOS_RESET);
1255		outb(CMOS_DATA, BIOS_WARM);	/* 'warm-start' */
1256
1257		/* attempt to start the Application Processor */
1258		CHECK_INIT(99);	/* setup checkpoints */
1259		if (!start_ap(x, boot_addr)) {
1260			printf("AP #%d (PHY# %d) failed!\n", x, CPU_TO_ID(x));
1261			CHECK_PRINT("trace");	/* show checkpoints */
1262			/*
1263			 * better panic as the AP may be running loose
1264			 * somewhere
1265			 */
1266			printf("panic y/n? [n] ");
1267			if (cngetc() != 'n')
1268				panic("bye-bye\n");
1269		}
1270		CHECK_PRINT("trace");	/* show checkpoints */
1271
1272		/* record its version info */
1273		cpu_apic_versions[x] = cpu_apic_versions[0];
1274	}
1275
1276	/* fill in our (BSP) APIC version */
1277	cpu_apic_versions[0] = apic_base[APIC_VER];
1278
1279	/* restore the warmstart vector */
1280	*(u_long *) WARMBOOT_OFF = mpbioswarmvec;
1281	outb(CMOS_REG, BIOS_RESET);
1282	outb(CMOS_DATA, mpbiosreason);
1283
1284	/* number of APs actually started */
1285	return mp_ncpus - 1;
1286}
1287
1288
1289/*
1290 * load the 1st level AP boot code into base memory.
1291 */
1292
1293/* targets for relocation */
1294extern void bigJump(void);
1295extern void bootCodeSeg(void);
1296extern void bootDataSeg(void);
1297extern void MPentry(void);
1298extern u_int MP_GDT;
1299extern u_int mp_gdtbase;
1300
1301static void
1302install_ap_tramp(u_int boot_addr)
1303{
1304	int     x;
1305	int     size = *(int *) ((u_long) & bootMP_size);
1306	u_char *src = (u_char *) ((u_long) bootMP);
1307	u_char *dst = (u_char *) boot_addr + KERNBASE;
1308	u_int   boot_base = (u_int) bootMP;
1309	u_int8_t *dst8;
1310	u_int16_t *dst16;
1311	u_int32_t *dst32;
1312
1313	for (x = 0; x < size; ++x)
1314		*dst++ = *src++;
1315
1316	/*
1317	 * modify addresses in code we just moved to basemem. unfortunately we
1318	 * need fairly detailed info about mpboot.s for this to work.  changes
1319	 * to mpboot.s might require changes here.
1320	 */
1321
1322	/* boot code is located in KERNEL space */
1323	dst = (u_char *) boot_addr + KERNBASE;
1324
1325	/* modify the lgdt arg */
1326	dst32 = (u_int32_t *) (dst + ((u_int) & mp_gdtbase - boot_base));
1327	*dst32 = boot_addr + ((u_int) & MP_GDT - boot_base);
1328
1329	/* modify the ljmp target for MPentry() */
1330	dst32 = (u_int32_t *) (dst + ((u_int) bigJump - boot_base) + 1);
1331	*dst32 = ((u_int) MPentry - KERNBASE);
1332
1333	/* modify the target for boot code segment */
1334	dst16 = (u_int16_t *) (dst + ((u_int) bootCodeSeg - boot_base));
1335	dst8 = (u_int8_t *) (dst16 + 1);
1336	*dst16 = (u_int) boot_addr & 0xffff;
1337	*dst8 = ((u_int) boot_addr >> 16) & 0xff;
1338
1339	/* modify the target for boot data segment */
1340	dst16 = (u_int16_t *) (dst + ((u_int) bootDataSeg - boot_base));
1341	dst8 = (u_int8_t *) (dst16 + 1);
1342	*dst16 = (u_int) boot_addr & 0xffff;
1343	*dst8 = ((u_int) boot_addr >> 16) & 0xff;
1344}
1345
1346
1347/*
1348 * this function starts the AP (application processor) identified
1349 * by the APIC ID 'physicalCpu'.  It does quite a "song and dance"
1350 * to accomplish this.  This is necessary because of the nuances
1351 * of the different hardware we might encounter.  It ain't pretty,
1352 * but it seems to work.
1353 */
1354static int
1355start_ap(int logical_cpu, u_int boot_addr)
1356{
1357	int     physical_cpu;
1358	int     vector;
1359	int     cpus;
1360	u_long  icr_lo, icr_hi;
1361
1362	/* get the PHYSICAL APIC ID# */
1363	physical_cpu = CPU_TO_ID(logical_cpu);
1364
1365	/* calculate the vector */
1366	vector = (boot_addr >> 12) & 0xff;
1367
1368	/* used as a watchpoint to signal AP startup */
1369	cpus = mp_ncpus;
1370
1371	/*
1372	 * first we do an INIT/RESET IPI this INIT IPI might be run, reseting
1373	 * and running the target CPU. OR this INIT IPI might be latched (P5
1374	 * bug), CPU waiting for STARTUP IPI. OR this INIT IPI might be
1375	 * ignored.
1376	 */
1377
1378	/* setup the address for the target AP */
1379	icr_hi = apic_base[APIC_ICR_HI] & ~APIC_ID_MASK;
1380	icr_hi |= (physical_cpu << 24);
1381	apic_base[APIC_ICR_HI] = icr_hi;
1382
1383	/* do an INIT IPI: assert RESET */
1384	icr_lo = apic_base[APIC_ICR_LOW] & 0xfff00000;
1385	apic_base[APIC_ICR_LOW] = icr_lo | 0x0000c500;
1386
1387	/* wait for pending status end */
1388	while (apic_base[APIC_ICR_LOW] & APIC_DELSTAT_MASK)
1389		 /* spin */ ;
1390
1391	/* do an INIT IPI: deassert RESET */
1392	apic_base[APIC_ICR_LOW] = icr_lo | 0x00008500;
1393
1394	/* wait for pending status end */
1395	u_sleep(10000);		/* wait ~10mS */
1396	while (apic_base[APIC_ICR_LOW] & APIC_DELSTAT_MASK)
1397		 /* spin */ ;
1398
1399	/*
1400	 * next we do a STARTUP IPI: the previous INIT IPI might still be
1401	 * latched, (P5 bug) this 1st STARTUP would then terminate
1402	 * immediately, and the previously started INIT IPI would continue. OR
1403	 * the previous INIT IPI has already run. and this STARTUP IPI will
1404	 * run. OR the previous INIT IPI was ignored. and this STARTUP IPI
1405	 * will run.
1406	 */
1407
1408	/* do a STARTUP IPI */
1409	apic_base[APIC_ICR_LOW] = icr_lo | 0x00000600 | vector;
1410	while (apic_base[APIC_ICR_LOW] & APIC_DELSTAT_MASK)
1411		 /* spin */ ;
1412	u_sleep(200);		/* wait ~200uS */
1413
1414	/*
1415	 * finally we do a 2nd STARTUP IPI: this 2nd STARTUP IPI should run IF
1416	 * the previous STARTUP IPI was cancelled by a latched INIT IPI. OR
1417	 * this STARTUP IPI will be ignored, as only ONE STARTUP IPI is
1418	 * recognized after hardware RESET or INIT IPI.
1419	 */
1420
1421	apic_base[APIC_ICR_LOW] = icr_lo | 0x00000600 | vector;
1422	while (apic_base[APIC_ICR_LOW] & APIC_DELSTAT_MASK)
1423		 /* spin */ ;
1424	u_sleep(200);		/* wait ~200uS */
1425
1426	/* wait for it to start */
1427	set_apic_timer(5000000);/* == 5 seconds */
1428	while (read_apic_timer())
1429		if (mp_ncpus > cpus)
1430			return 1;	/* return SUCCESS */
1431
1432	return 0;		/* return FAILURE */
1433}
1434
1435
1436/*
1437 * Flush the TLB on all other CPU's
1438 *
1439 * XXX: Needs to handshake and wait for completion before proceding.
1440 */
1441void
1442smp_invltlb(void)
1443{
1444	if (smp_active && invltlb_ok)
1445		all_but_self_ipi(ICU_OFFSET + XINVLTLB_OFFSET);
1446}
1447
1448void
1449invlpg(u_int addr)
1450{
1451	__asm   __volatile("invlpg (%0)"::"r"(addr):"memory");
1452
1453	/* send a message to the other CPUs */
1454	smp_invltlb();
1455}
1456
1457void
1458invltlb(void)
1459{
1460	u_long  temp;
1461
1462	/*
1463	 * This should be implemented as load_cr3(rcr3()) when load_cr3() is
1464	 * inlined.
1465	 */
1466	__asm __volatile("movl %%cr3, %0; movl %0, %%cr3":"=r"(temp) :: "memory");
1467
1468	/* send a message to the other CPUs */
1469	smp_invltlb();
1470}
1471