mptable.c revision 28442
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.41 1997/08/10 19:32:38 fsmp Exp $
26 */
27
28#include "opt_smp.h"
29
30#include <sys/param.h>		/* for KERNBASE */
31#include <sys/systm.h>
32
33#include <vm/vm.h>		/* for KERNBASE */
34#include <vm/vm_param.h>	/* for KERNBASE */
35#include <vm/pmap.h>		/* for KERNBASE */
36#include <vm/vm_kern.h>
37#include <vm/vm_extern.h>
38
39#include <machine/smp.h>
40#include <machine/apic.h>
41#include <machine/mpapic.h>
42#include <machine/segments.h>
43#include <machine/smptests.h>	/** TEST_DEFAULT_CONFIG, TEST_TEST1 */
44#include <machine/tss.h>
45#include <machine/specialreg.h>
46
47#include <i386/i386/cons.h>	/* cngetc() */
48
49#if defined(APIC_IO)
50#include <machine/md_var.h>		/* setidt() */
51#include <i386/isa/icu.h>		/* IPIs */
52#include <i386/isa/intr_machdep.h>	/* IPIs */
53#endif	/* APIC_IO */
54
55#if defined(TEST_DEFAULT_CONFIG)
56#define MPFPS_MPFB1	TEST_DEFAULT_CONFIG
57#else
58#define MPFPS_MPFB1	mpfps->mpfb1
59#endif  /* TEST_DEFAULT_CONFIG */
60
61#define WARMBOOT_TARGET		0
62#define WARMBOOT_OFF		(KERNBASE + 0x0467)
63#define WARMBOOT_SEG		(KERNBASE + 0x0469)
64
65#define BIOS_BASE		(0xf0000)
66#define BIOS_SIZE		(0x10000)
67#define BIOS_COUNT		(BIOS_SIZE/4)
68
69#define CMOS_REG		(0x70)
70#define CMOS_DATA		(0x71)
71#define BIOS_RESET		(0x0f)
72#define BIOS_WARM		(0x0a)
73
74#define PROCENTRY_FLAG_EN	0x01
75#define PROCENTRY_FLAG_BP	0x02
76#define IOAPICENTRY_FLAG_EN	0x01
77
78
79/* MP Floating Pointer Structure */
80typedef struct MPFPS {
81	char    signature[4];
82	void   *pap;
83	u_char  length;
84	u_char  spec_rev;
85	u_char  checksum;
86	u_char  mpfb1;
87	u_char  mpfb2;
88	u_char  mpfb3;
89	u_char  mpfb4;
90	u_char  mpfb5;
91}      *mpfps_t;
92
93/* MP Configuration Table Header */
94typedef struct MPCTH {
95	char    signature[4];
96	u_short base_table_length;
97	u_char  spec_rev;
98	u_char  checksum;
99	u_char  oem_id[8];
100	u_char  product_id[12];
101	void   *oem_table_pointer;
102	u_short oem_table_size;
103	u_short entry_count;
104	void   *apic_address;
105	u_short extended_table_length;
106	u_char  extended_table_checksum;
107	u_char  reserved;
108}      *mpcth_t;
109
110
111typedef struct PROCENTRY {
112	u_char  type;
113	u_char  apic_id;
114	u_char  apic_version;
115	u_char  cpu_flags;
116	u_long  cpu_signature;
117	u_long  feature_flags;
118	u_long  reserved1;
119	u_long  reserved2;
120}      *proc_entry_ptr;
121
122typedef struct BUSENTRY {
123	u_char  type;
124	u_char  bus_id;
125	char    bus_type[6];
126}      *bus_entry_ptr;
127
128typedef struct IOAPICENTRY {
129	u_char  type;
130	u_char  apic_id;
131	u_char  apic_version;
132	u_char  apic_flags;
133	void   *apic_address;
134}      *io_apic_entry_ptr;
135
136typedef struct INTENTRY {
137	u_char  type;
138	u_char  int_type;
139	u_short int_flags;
140	u_char  src_bus_id;
141	u_char  src_bus_irq;
142	u_char  dst_apic_id;
143	u_char  dst_apic_int;
144}      *int_entry_ptr;
145
146/* descriptions of MP basetable entries */
147typedef struct BASETABLE_ENTRY {
148	u_char  type;
149	u_char  length;
150	char    name[16];
151}       basetable_entry;
152
153/*
154 * this code MUST be enabled here and in mpboot.s.
155 * it follows the very early stages of AP boot by placing values in CMOS ram.
156 * it NORMALLY will never be needed and thus the primitive method for enabling.
157 *
158#define CHECK_POINTS
159 */
160
161#if defined(CHECK_POINTS)
162#define CHECK_READ(A)	 (outb(CMOS_REG, (A)), inb(CMOS_DATA))
163#define CHECK_WRITE(A,D) (outb(CMOS_REG, (A)), outb(CMOS_DATA, (D)))
164
165#define CHECK_INIT(D);				\
166	CHECK_WRITE(0x34, (D));			\
167	CHECK_WRITE(0x35, (D));			\
168	CHECK_WRITE(0x36, (D));			\
169	CHECK_WRITE(0x37, (D));			\
170	CHECK_WRITE(0x38, (D));			\
171	CHECK_WRITE(0x39, (D));
172
173#define CHECK_PRINT(S);				\
174	printf("%s: %d, %d, %d, %d, %d, %d\n",	\
175	   (S),					\
176	   CHECK_READ(0x34),			\
177	   CHECK_READ(0x35),			\
178	   CHECK_READ(0x36),			\
179	   CHECK_READ(0x37),			\
180	   CHECK_READ(0x38),			\
181	   CHECK_READ(0x39));
182
183#else				/* CHECK_POINTS */
184
185#define CHECK_INIT(D)
186#define CHECK_PRINT(S)
187
188#endif				/* CHECK_POINTS */
189
190/*
191 * Values to send to the POST hardware.
192 */
193#define MP_BOOTADDRESS_POST	0x10
194#define MP_PROBE_POST		0x11
195#define MP_START_POST		0x12
196#define MP_ANNOUNCE_POST	0x13
197#define MPTABLE_PASS1_POST	0x14
198#define MPTABLE_PASS2_POST	0x15
199#define MP_ENABLE_POST		0x16
200#define START_ALL_APS_POST	0x17
201#define INSTALL_AP_TRAMP_POST	0x18
202#define START_AP_POST		0x19
203
204/** XXX FIXME: where does this really belong, isa.h/isa.c perhaps? */
205int	current_postcode;
206
207/** XXX FIXME: what system files declare these??? */
208extern struct region_descriptor r_gdt, r_idt;
209
210int	mp_ncpus;		/* # of CPUs, including BSP */
211int	mp_naps;		/* # of Applications processors */
212int	mp_nbusses;		/* # of busses */
213int	mp_napics;		/* # of IO APICs */
214int	boot_cpu_id;		/* designated BSP */
215vm_offset_t cpu_apic_address;
216vm_offset_t io_apic_address[NAPICID];	/* NAPICID is more than enough */
217
218u_int32_t cpu_apic_versions[NCPU];
219u_int32_t io_apic_versions[NAPIC];
220
221/*
222 * APIC ID logical/physical mapping structures.
223 * We oversize these to simplify boot-time config.
224 */
225int     cpu_num_to_apic_id[NAPICID];
226int     io_num_to_apic_id[NAPICID];
227int     apic_id_to_logical[NAPICID];
228
229/* Bitmap of all available CPUs */
230u_int	all_cpus;
231
232/* Boot of AP uses this PTD */
233u_int *bootPTD;
234
235/* Hotwire a 0->4MB V==P mapping */
236extern pt_entry_t KPTphys;
237
238/* Virtual address of per-cpu common_tss */
239extern struct i386tss common_tss;
240
241/*
242 * Local data and functions.
243 */
244
245static int	mp_capable;
246static u_int	boot_address;
247static u_int	base_memory;
248
249static int	picmode;		/* 0: virtual wire mode, 1: PIC mode */
250static mpfps_t	mpfps;
251static int	search_for_sig(u_int32_t target, int count);
252static void	mp_enable(u_int boot_addr);
253
254static int	mptable_pass1(void);
255static int	mptable_pass2(void);
256static void	default_mp_table(int type);
257static void	fix_mp_table(void);
258static void	init_locks(void);
259static int	start_all_aps(u_int boot_addr);
260static void	install_ap_tramp(u_int boot_addr);
261static int	start_ap(int logicalCpu, u_int boot_addr);
262
263
264/*
265 * Calculate usable address in base memory for AP trampoline code.
266 */
267u_int
268mp_bootaddress(u_int basemem)
269{
270	POSTCODE(MP_BOOTADDRESS_POST);
271
272	base_memory = basemem * 1024;	/* convert to bytes */
273
274	boot_address = base_memory & ~0xfff;	/* round down to 4k boundary */
275	if ((base_memory - boot_address) < bootMP_size)
276		boot_address -= 4096;	/* not enough, lower by 4k */
277
278	return boot_address;
279}
280
281
282/*
283 * Look for an Intel MP spec table (ie, SMP capable hardware).
284 */
285int
286mp_probe(void)
287{
288	int     x;
289	u_long  segment;
290	u_int32_t target;
291
292	POSTCODE(MP_PROBE_POST);
293
294	/* see if EBDA exists */
295	if (segment = (u_long) * (u_short *) (KERNBASE + 0x40e)) {
296		/* search first 1K of EBDA */
297		target = (u_int32_t) (segment << 4);
298		if ((x = search_for_sig(target, 1024 / 4)) >= 0)
299			goto found;
300	} else {
301		/* last 1K of base memory, effective 'top of base' passed in */
302		target = (u_int32_t) (base_memory - 0x400);
303		if ((x = search_for_sig(target, 1024 / 4)) >= 0)
304			goto found;
305	}
306
307	/* search the BIOS */
308	target = (u_int32_t) BIOS_BASE;
309	if ((x = search_for_sig(target, BIOS_COUNT)) >= 0)
310		goto found;
311
312	/* nothing found */
313	mpfps = (mpfps_t)0;
314	mp_capable = 0;
315	return 0;
316
317found:
318	/* calculate needed resources */
319	mpfps = (mpfps_t)x;
320	if (mptable_pass1())
321		panic("you must reconfigure your kernel");
322
323	/* flag fact that we are running multiple processors */
324	mp_capable = 1;
325	return 1;
326}
327
328
329/*
330 * Startup the SMP processors.
331 */
332void
333mp_start(void)
334{
335	POSTCODE(MP_START_POST);
336
337	/* look for MP capable motherboard */
338	if (mp_capable)
339		mp_enable(boot_address);
340	else
341		panic("MP hardware not found!");
342}
343
344
345/*
346 * Print various information about the SMP system hardware and setup.
347 */
348void
349mp_announce(void)
350{
351	int     x;
352
353	POSTCODE(MP_ANNOUNCE_POST);
354
355	printf("FreeBSD/SMP: Multiprocessor motherboard\n");
356	printf(" cpu0 (BSP): apic id: %2d", CPU_TO_ID(0));
357	printf(", version: 0x%08x", cpu_apic_versions[0]);
358	printf(", at 0x%08x\n", cpu_apic_address);
359	for (x = 1; x <= mp_naps; ++x) {
360		printf(" cpu%d (AP):  apic id: %2d", x, CPU_TO_ID(x));
361		printf(", version: 0x%08x", cpu_apic_versions[x]);
362		printf(", at 0x%08x\n", cpu_apic_address);
363	}
364
365#if defined(APIC_IO)
366	for (x = 0; x < mp_napics; ++x) {
367		printf(" io%d (APIC): apic id: %2d", x, IO_TO_ID(x));
368		printf(", version: 0x%08x", io_apic_versions[x]);
369		printf(", at 0x%08x\n", io_apic_address[x]);
370	}
371#else
372	printf(" Warning: APIC I/O disabled\n");
373#endif	/* APIC_IO */
374}
375
376/*
377 * AP cpu's call this to sync up protected mode.
378 */
379void
380init_secondary(void)
381{
382	int     gsel_tss, slot;
383
384	r_gdt.rd_limit = sizeof(gdt[0]) * (NGDT + NCPU) - 1;
385	r_gdt.rd_base = (int) gdt;
386	lgdt(&r_gdt);			/* does magic intra-segment return */
387	lidt(&r_idt);
388	lldt(_default_ldt);
389
390	slot = NGDT + cpuid;
391	gsel_tss = GSEL(slot, SEL_KPL);
392	gdt[slot].sd.sd_type = SDT_SYS386TSS;
393	common_tss.tss_esp0 = 0;	/* not used until after switch */
394	common_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
395	common_tss.tss_ioopt = (sizeof common_tss) << 16;
396	ltr(gsel_tss);
397
398	load_cr0(0x8005003b);		/* XXX! */
399
400	PTD[0] = 0;
401	pmap_set_opt((unsigned *)PTD);
402
403	invltlb();
404}
405
406
407#if defined(APIC_IO)
408/*
409 * Final configuration of the BSP's local APIC:
410 *  - disable 'pic mode'.
411 *  - disable 'virtual wire mode'.
412 *  - enable NMI.
413 */
414void
415bsp_apic_configure(void)
416{
417	u_char		byte;
418	u_int32_t	temp;
419
420	/* leave 'pic mode' if necessary */
421	if (picmode) {
422		outb(0x22, 0x70);	/* select IMCR */
423		byte = inb(0x23);	/* current contents */
424		byte |= 0x01;		/* mask external INTR */
425		outb(0x23, byte);	/* disconnect 8259s/NMI */
426	}
427
428	/* mask lint0 (the 8259 'virtual wire' connection) */
429	temp = lapic.lvt_lint0;
430	temp |= APIC_LVT_M;		/* set the mask */
431	lapic.lvt_lint0 = temp;
432
433        /* setup lint1 to handle NMI */
434        temp = lapic.lvt_lint1;
435        temp &= ~APIC_LVT_M;		/* clear the mask */
436        lapic.lvt_lint1 = temp;
437
438	if (bootverbose)
439		apic_dump("bsp_apic_configure()");
440}
441#endif  /* APIC_IO */
442
443
444/*******************************************************************
445 * local functions and data
446 */
447
448/*
449 * start the SMP system
450 */
451static void
452mp_enable(u_int boot_addr)
453{
454	int     x;
455#if defined(APIC_IO)
456	int     apic;
457	u_int   ux;
458#endif	/* APIC_IO */
459
460	POSTCODE(MP_ENABLE_POST);
461
462	/* turn on 4MB of V == P addressing so we can get to MP table */
463	*(int *)PTD = PG_V | PG_RW | ((u_long)KPTphys & PG_FRAME);
464	invltlb();
465
466	/* examine the MP table for needed info, uses physical addresses */
467	x = mptable_pass2();
468
469	*(int *)PTD = 0;
470	invltlb();
471
472	/* can't process default configs till the CPU APIC is pmapped */
473	if (x)
474		default_mp_table(x);
475
476	/* post scan cleanup */
477	fix_mp_table();
478
479#if defined(APIC_IO)
480
481	/* fill the LOGICAL io_apic_versions table */
482	for (apic = 0; apic < mp_napics; ++apic) {
483		ux = io_apic_read(apic, IOAPIC_VER);
484		io_apic_versions[apic] = ux;
485	}
486
487	/* program each IO APIC in the system */
488	for (apic = 0; apic < mp_napics; ++apic)
489		if (io_apic_setup(apic) < 0)
490			panic("IO APIC setup failure");
491
492	/* install a 'Spurious INTerrupt' vector */
493	setidt(XSPURIOUSINT_OFFSET, Xspuriousint,
494	       SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
495
496	/* install an inter-CPU IPI for TLB invalidation */
497	setidt(XINVLTLB_OFFSET, Xinvltlb,
498	       SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
499
500	/* install an inter-CPU IPI for CPU stop/restart */
501	setidt(XCPUSTOP_OFFSET, Xcpustop,
502	       SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
503
504#if defined(TEST_TEST1)
505	/* install a "fake hardware INTerrupt" vector */
506	setidt(XTEST1_OFFSET, Xtest1,
507	       SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
508#endif  /** TEST_TEST1 */
509
510#endif	/* APIC_IO */
511
512	/* initialize all SMP locks */
513	init_locks();
514
515	/* start each Application Processor */
516	start_all_aps(boot_addr);
517
518	/*
519	 * The init process might be started on a different CPU now,
520	 * and the boot CPU might not call prepare_usermode to get
521	 * cr0 correctly configured. Thus we initialize cr0 here.
522	 */
523	load_cr0(rcr0() | CR0_WP | CR0_AM);
524}
525
526
527/*
528 * look for the MP spec signature
529 */
530
531/* string defined by the Intel MP Spec as identifying the MP table */
532#define MP_SIG		0x5f504d5f	/* _MP_ */
533#define NEXT(X)		((X) += 4)
534static int
535search_for_sig(u_int32_t target, int count)
536{
537	int     x;
538	u_int32_t *addr = (u_int32_t *) (KERNBASE + target);
539
540	for (x = 0; x < count; NEXT(x))
541		if (addr[x] == MP_SIG)
542			/* make array index a byte index */
543			return (target + (x * sizeof(u_int32_t)));
544
545	return -1;
546}
547
548
549static basetable_entry basetable_entry_types[] =
550{
551	{0, 20, "Processor"},
552	{1, 8, "Bus"},
553	{2, 8, "I/O APIC"},
554	{3, 8, "I/O INT"},
555	{4, 8, "Local INT"}
556};
557
558typedef struct BUSDATA {
559	u_char  bus_id;
560	enum busTypes bus_type;
561}       bus_datum;
562
563typedef struct INTDATA {
564	u_char  int_type;
565	u_short int_flags;
566	u_char  src_bus_id;
567	u_char  src_bus_irq;
568	u_char  dst_apic_id;
569	u_char  dst_apic_int;
570}       io_int, local_int;
571
572typedef struct BUSTYPENAME {
573	u_char  type;
574	char    name[7];
575}       bus_type_name;
576
577static bus_type_name bus_type_table[] =
578{
579	{CBUS, "CBUS"},
580	{CBUSII, "CBUSII"},
581	{EISA, "EISA"},
582	{UNKNOWN_BUSTYPE, "---"},
583	{UNKNOWN_BUSTYPE, "---"},
584	{ISA, "ISA"},
585	{UNKNOWN_BUSTYPE, "---"},
586	{UNKNOWN_BUSTYPE, "---"},
587	{UNKNOWN_BUSTYPE, "---"},
588	{UNKNOWN_BUSTYPE, "---"},
589	{UNKNOWN_BUSTYPE, "---"},
590	{UNKNOWN_BUSTYPE, "---"},
591	{PCI, "PCI"},
592	{UNKNOWN_BUSTYPE, "---"},
593	{UNKNOWN_BUSTYPE, "---"},
594	{UNKNOWN_BUSTYPE, "---"},
595	{UNKNOWN_BUSTYPE, "---"},
596	{XPRESS, "XPRESS"},
597	{UNKNOWN_BUSTYPE, "---"}
598};
599/* from MP spec v1.4, table 5-1 */
600static int default_data[7][5] =
601{
602/*   nbus, id0, type0, id1, type1 */
603	{1, 0, ISA, 255, 255},
604	{1, 0, EISA, 255, 255},
605	{1, 0, EISA, 255, 255},
606	{0, 255, 255, 255, 255},/* MCA not supported */
607	{2, 0, ISA, 1, PCI},
608	{2, 0, EISA, 1, PCI},
609	{0, 255, 255, 255, 255}	/* MCA not supported */
610};
611
612
613/* the bus data */
614bus_datum bus_data[NBUS];
615
616/* the IO INT data, one entry per possible APIC INTerrupt */
617io_int  io_apic_ints[NINTR];
618
619static int nintrs;
620
621static int processor_entry	__P((proc_entry_ptr entry, int cpu));
622static int bus_entry		__P((bus_entry_ptr entry, int bus));
623static int io_apic_entry	__P((io_apic_entry_ptr entry, int apic));
624static int int_entry		__P((int_entry_ptr entry, int intr));
625static int lookup_bus_type	__P((char *name));
626
627
628/*
629 * 1st pass on motherboard's Intel MP specification table.
630 *
631 * initializes:
632 *	mp_ncpus = 1
633 *
634 * determines:
635 *	cpu_apic_address (common to all CPUs)
636 *	io_apic_address[N]
637 *	mp_naps
638 *	mp_nbusses
639 *	mp_napics
640 *	nintrs
641 */
642static int
643mptable_pass1(void)
644{
645	int	x;
646	mpcth_t	cth;
647	int	totalSize;
648	void*	position;
649	int	count;
650	int	type;
651	int	mustpanic;
652
653	POSTCODE(MPTABLE_PASS1_POST);
654
655	mustpanic = 0;
656
657	/* clear various tables */
658	for (x = 0; x < NAPICID; ++x) {
659		io_apic_address[x] = ~0;	/* IO APIC address table */
660	}
661
662	/* init everything to empty */
663	mp_naps = 0;
664	mp_nbusses = 0;
665	mp_napics = 0;
666	nintrs = 0;
667
668	/* check for use of 'default' configuration */
669	if (MPFPS_MPFB1 != 0) {
670		/* use default addresses */
671		cpu_apic_address = DEFAULT_APIC_BASE;
672		io_apic_address[0] = DEFAULT_IO_APIC_BASE;
673
674		/* fill in with defaults */
675		mp_naps = 2;		/* includes BSP */
676		mp_nbusses = default_data[MPFPS_MPFB1 - 1][0];
677#if defined(APIC_IO)
678		mp_napics = 1;
679		nintrs = 16;
680#endif	/* APIC_IO */
681	}
682	else {
683		if ((cth = mpfps->pap) == 0)
684			panic("MP Configuration Table Header MISSING!");
685
686		cpu_apic_address = (vm_offset_t) cth->apic_address;
687
688		/* walk the table, recording info of interest */
689		totalSize = cth->base_table_length - sizeof(struct MPCTH);
690		position = (u_char *) cth + sizeof(struct MPCTH);
691		count = cth->entry_count;
692
693		while (count--) {
694			switch (type = *(u_char *) position) {
695			case 0: /* processor_entry */
696				if (((proc_entry_ptr)position)->cpu_flags
697					& PROCENTRY_FLAG_EN)
698					++mp_naps;
699				break;
700			case 1: /* bus_entry */
701				++mp_nbusses;
702				break;
703			case 2: /* io_apic_entry */
704				if (((io_apic_entry_ptr)position)->apic_flags
705					& IOAPICENTRY_FLAG_EN)
706					io_apic_address[mp_napics++] =
707					    (vm_offset_t)((io_apic_entry_ptr)
708						position)->apic_address;
709				break;
710			case 3: /* int_entry */
711				++nintrs;
712				break;
713			case 4:	/* int_entry */
714				break;
715			default:
716				panic("mpfps Base Table HOSED!");
717				/* NOTREACHED */
718			}
719
720			totalSize -= basetable_entry_types[type].length;
721			(u_char*)position += basetable_entry_types[type].length;
722		}
723	}
724
725	/* qualify the numbers */
726	if (mp_naps > NCPU)
727#if 0 /* XXX FIXME: kern/4255 */
728		printf("Warning: only using %d of %d available CPUs!\n",
729			NCPU, mp_naps);
730#else
731	{
732		printf("NCPU cannot be different than actual CPU count.\n");
733		printf(" add 'options NCPU=%d' to your kernel config file,\n",
734			mp_naps);
735		printf(" then rerun config & rebuild your SMP kernel\n");
736		mustpanic = 1;
737	}
738#endif /* XXX FIXME: kern/4255 */
739	if (mp_nbusses > NBUS) {
740		printf("found %d busses, increase NBUS\n", mp_nbusses);
741		mustpanic = 1;
742	}
743	if (mp_napics > NAPIC) {
744		printf("found %d apics, increase NAPIC\n", mp_napics);
745		mustpanic = 1;
746	}
747	if (nintrs > NINTR) {
748		printf("found %d intrs, increase NINTR\n", nintrs);
749		mustpanic = 1;
750	}
751
752	/*
753	 * Count the BSP.
754	 * This is also used as a counter while starting the APs.
755	 */
756	mp_ncpus = 1;
757
758	--mp_naps;	/* subtract the BSP */
759
760	return mustpanic;
761}
762
763
764/*
765 * 2nd pass on motherboard's Intel MP specification table.
766 *
767 * sets:
768 *	boot_cpu_id
769 *	ID_TO_IO(N), phy APIC ID to log CPU/IO table
770 *	CPU_TO_ID(N), logical CPU to APIC ID table
771 *	IO_TO_ID(N), logical IO to APIC ID table
772 *	bus_data[N]
773 *	io_apic_ints[N]
774 */
775static int
776mptable_pass2(void)
777{
778	int     x;
779	mpcth_t cth;
780	int     totalSize;
781	void*   position;
782	int     count;
783	int     type;
784	int     apic, bus, cpu, intr;
785
786	POSTCODE(MPTABLE_PASS2_POST);
787
788	/* clear various tables */
789	for (x = 0; x < NAPICID; ++x) {
790		ID_TO_IO(x) = -1;	/* phy APIC ID to log CPU/IO table */
791		CPU_TO_ID(x) = -1;	/* logical CPU to APIC ID table */
792		IO_TO_ID(x) = -1;	/* logical IO to APIC ID table */
793	}
794
795	/* clear bus data table */
796	for (x = 0; x < NBUS; ++x)
797		bus_data[x].bus_id = 0xff;
798
799	/* clear IO APIC INT table */
800	for (x = 0; x < NINTR; ++x)
801		io_apic_ints[x].int_type = 0xff;
802
803	/* setup the cpu/apic mapping arrays */
804	boot_cpu_id = -1;
805
806	/* record whether PIC or virtual-wire mode */
807	picmode = (mpfps->mpfb2 & 0x80) ? 1 : 0;
808
809	/* check for use of 'default' configuration */
810	if (MPFPS_MPFB1 != 0)
811		return MPFPS_MPFB1;	/* return default configuration type */
812
813	if ((cth = mpfps->pap) == 0)
814		panic("MP Configuration Table Header MISSING!");
815
816	/* walk the table, recording info of interest */
817	totalSize = cth->base_table_length - sizeof(struct MPCTH);
818	position = (u_char *) cth + sizeof(struct MPCTH);
819	count = cth->entry_count;
820	apic = bus = intr = 0;
821	cpu = 1;				/* pre-count the BSP */
822
823	while (count--) {
824		switch (type = *(u_char *) position) {
825		case 0:
826			if (processor_entry(position, cpu))
827				++cpu;
828			break;
829		case 1:
830			if (bus_entry(position, bus))
831				++bus;
832			break;
833		case 2:
834			if (io_apic_entry(position, apic))
835				++apic;
836			break;
837		case 3:
838			if (int_entry(position, intr))
839				++intr;
840			break;
841		case 4:
842			/* int_entry(position); */
843			break;
844		default:
845			panic("mpfps Base Table HOSED!");
846			/* NOTREACHED */
847		}
848
849		totalSize -= basetable_entry_types[type].length;
850		(u_char *) position += basetable_entry_types[type].length;
851	}
852
853	if (boot_cpu_id == -1)
854		panic("NO BSP found!");
855
856	/* report fact that its NOT a default configuration */
857	return 0;
858}
859
860
861/*
862 * parse an Intel MP specification table
863 */
864static void
865fix_mp_table(void)
866{
867	int	x;
868	int	id;
869	int	bus_0;
870	int	bus_pci;
871	int	num_pci_bus;
872
873	/*
874	 * Fix mis-numbering of the PCI bus and its INT entries if the BIOS
875	 * did it wrong.  The MP spec says that when more than 1 PCI bus
876	 * exists the BIOS must begin with bus entries for the PCI bus and use
877	 * actual PCI bus numbering.  This implies that when only 1 PCI bus
878	 * exists the BIOS can choose to ignore this ordering, and indeed many
879	 * MP motherboards do ignore it.  This causes a problem when the PCI
880	 * sub-system makes requests of the MP sub-system based on PCI bus
881	 * numbers.	So here we look for the situation and renumber the
882	 * busses and associated INTs in an effort to "make it right".
883	 */
884
885	/* find bus 0, PCI bus, count the number of PCI busses */
886	for (num_pci_bus = 0, x = 0; x < mp_nbusses; ++x) {
887		if (bus_data[x].bus_id == 0) {
888			bus_0 = x;
889		}
890		if (bus_data[x].bus_type == PCI) {
891			++num_pci_bus;
892			bus_pci = x;
893		}
894	}
895	/*
896	 * bus_0 == slot of bus with ID of 0
897	 * bus_pci == slot of last PCI bus encountered
898	 */
899
900	/* check the 1 PCI bus case for sanity */
901	if (num_pci_bus == 1) {
902
903		/* if it is number 0 all is well */
904		if (bus_data[bus_pci].bus_id == 0)
905			return;
906
907		/* mis-numbered, swap with whichever bus uses slot 0 */
908
909		/* swap the bus entry types */
910		bus_data[bus_pci].bus_type = bus_data[bus_0].bus_type;
911		bus_data[bus_0].bus_type = PCI;
912
913		/* swap each relavant INTerrupt entry */
914		id = bus_data[bus_pci].bus_id;
915		for (x = 0; x < nintrs; ++x) {
916			if (io_apic_ints[x].src_bus_id == id) {
917				io_apic_ints[x].src_bus_id = 0;
918			}
919			else if (io_apic_ints[x].src_bus_id == 0) {
920				io_apic_ints[x].src_bus_id = id;
921			}
922		}
923	}
924	/* sanity check if more than 1 PCI bus */
925	else if (num_pci_bus > 1) {
926		for (x = 0; x < mp_nbusses; ++x) {
927			if (bus_data[x].bus_type != PCI)
928				continue;
929			if (bus_data[x].bus_id >= num_pci_bus)
930				panic("bad PCI bus numbering");
931		}
932	}
933}
934
935
936static int
937processor_entry(proc_entry_ptr entry, int cpu)
938{
939	/* check for usability */
940	if ((cpu >= NCPU) || !(entry->cpu_flags & PROCENTRY_FLAG_EN))
941		return 0;
942
943	/* check for BSP flag */
944	if (entry->cpu_flags & PROCENTRY_FLAG_BP) {
945		boot_cpu_id = entry->apic_id;
946		CPU_TO_ID(0) = entry->apic_id;
947		ID_TO_CPU(entry->apic_id) = 0;
948		return 0;	/* its already been counted */
949	}
950
951	/* add another AP to list, if less than max number of CPUs */
952	else {
953		CPU_TO_ID(cpu) = entry->apic_id;
954		ID_TO_CPU(entry->apic_id) = cpu;
955		return 1;
956	}
957}
958
959
960static int
961bus_entry(bus_entry_ptr entry, int bus)
962{
963	int     x;
964	char    c, name[8];
965
966	/* encode the name into an index */
967	for (x = 0; x < 6; ++x) {
968		if ((c = entry->bus_type[x]) == ' ')
969			break;
970		name[x] = c;
971	}
972	name[x] = '\0';
973
974	if ((x = lookup_bus_type(name)) == UNKNOWN_BUSTYPE)
975		panic("unknown bus type: '%s'", name);
976
977	bus_data[bus].bus_id = entry->bus_id;
978	bus_data[bus].bus_type = x;
979
980	return 1;
981}
982
983
984static int
985io_apic_entry(io_apic_entry_ptr entry, int apic)
986{
987	if (!(entry->apic_flags & IOAPICENTRY_FLAG_EN))
988		return 0;
989
990	IO_TO_ID(apic) = entry->apic_id;
991	ID_TO_IO(entry->apic_id) = apic;
992
993	return 1;
994}
995
996
997static int
998lookup_bus_type(char *name)
999{
1000	int     x;
1001
1002	for (x = 0; x < MAX_BUSTYPE; ++x)
1003		if (strcmp(bus_type_table[x].name, name) == 0)
1004			return bus_type_table[x].type;
1005
1006	return UNKNOWN_BUSTYPE;
1007}
1008
1009
1010static int
1011int_entry(int_entry_ptr entry, int intr)
1012{
1013	io_apic_ints[intr].int_type = entry->int_type;
1014	io_apic_ints[intr].int_flags = entry->int_flags;
1015	io_apic_ints[intr].src_bus_id = entry->src_bus_id;
1016	io_apic_ints[intr].src_bus_irq = entry->src_bus_irq;
1017	io_apic_ints[intr].dst_apic_id = entry->dst_apic_id;
1018	io_apic_ints[intr].dst_apic_int = entry->dst_apic_int;
1019
1020	return 1;
1021}
1022
1023
1024static int
1025apic_int_is_bus_type(int intr, int bus_type)
1026{
1027	int     bus;
1028
1029	for (bus = 0; bus < mp_nbusses; ++bus)
1030		if ((bus_data[bus].bus_id == io_apic_ints[intr].src_bus_id)
1031		    && ((int) bus_data[bus].bus_type == bus_type))
1032			return 1;
1033
1034	return 0;
1035}
1036
1037
1038/*
1039 * Given a traditional ISA INT mask, return an APIC mask.
1040 */
1041u_int
1042isa_apic_mask(u_int isa_mask)
1043{
1044	int isa_irq;
1045	int apic_pin;
1046
1047#if defined(SKIP_IRQ15_REDIRECT)
1048	if (isa_mask == (1 << 15)) {
1049		printf("skipping ISA IRQ15 redirect\n");
1050		return isa_mask;
1051	}
1052#endif  /* SKIP_IRQ15_REDIRECT */
1053
1054	isa_irq = ffs(isa_mask);		/* find its bit position */
1055	if (isa_irq == 0)			/* doesn't exist */
1056		return 0;
1057	--isa_irq;				/* make it zero based */
1058
1059	apic_pin = isa_apic_pin(isa_irq);	/* look for APIC connection */
1060	if (apic_pin == -1)
1061		return 0;
1062
1063	return (1 << apic_pin);			/* convert pin# to a mask */
1064}
1065
1066
1067/*
1068 * Determine which APIC pin an ISA/EISA INT is attached to.
1069 */
1070#define INTTYPE(I)	(io_apic_ints[(I)].int_type)
1071#define INTPIN(I)	(io_apic_ints[(I)].dst_apic_int)
1072
1073#define SRCBUSIRQ(I)	(io_apic_ints[(I)].src_bus_irq)
1074int
1075isa_apic_pin(int isa_irq)
1076{
1077	int     intr;
1078
1079	for (intr = 0; intr < nintrs; ++intr) {		/* check each record */
1080		if (INTTYPE(intr) == 0) {		/* standard INT */
1081			if (SRCBUSIRQ(intr) == isa_irq) {
1082				if (apic_int_is_bus_type(intr, ISA) ||
1083			            apic_int_is_bus_type(intr, EISA))
1084					return INTPIN(intr);	/* found */
1085			}
1086		}
1087	}
1088	return -1;					/* NOT found */
1089}
1090#undef SRCBUSIRQ
1091
1092
1093/*
1094 * Determine which APIC pin a PCI INT is attached to.
1095 */
1096#define SRCBUSID(I)	(io_apic_ints[(I)].src_bus_id)
1097#define SRCBUSDEVICE(I)	((io_apic_ints[(I)].src_bus_irq >> 2) & 0x1f)
1098#define SRCBUSLINE(I)	(io_apic_ints[(I)].src_bus_irq & 0x03)
1099int
1100pci_apic_pin(int pciBus, int pciDevice, int pciInt)
1101{
1102	int     intr;
1103
1104	--pciInt;					/* zero based */
1105
1106	for (intr = 0; intr < nintrs; ++intr)		/* check each record */
1107		if ((INTTYPE(intr) == 0)		/* standard INT */
1108		    && (SRCBUSID(intr) == pciBus)
1109		    && (SRCBUSDEVICE(intr) == pciDevice)
1110		    && (SRCBUSLINE(intr) == pciInt))	/* a candidate IRQ */
1111			if (apic_int_is_bus_type(intr, PCI))
1112				return INTPIN(intr);	/* exact match */
1113
1114	return -1;					/* NOT found */
1115}
1116#undef SRCBUSLINE
1117#undef SRCBUSDEVICE
1118#undef SRCBUSID
1119
1120#undef INTPIN
1121#undef INTTYPE
1122
1123
1124/*
1125 * Reprogram the MB chipset to NOT redirect an ISA INTerrupt.
1126 *
1127 * XXX FIXME:
1128 *  Exactly what this means is unclear at this point.  It is a solution
1129 *  for motherboards that redirect the MBIRQ0 pin.  Generically a motherboard
1130 *  could route any of the ISA INTs to upper (>15) IRQ values.  But most would
1131 *  NOT be redirected via MBIRQ0, thus "undirect()ing" them would NOT be an
1132 *  option.
1133 */
1134int
1135undirect_isa_irq(int rirq)
1136{
1137#if defined(READY)
1138	printf("Freeing redirected ISA irq %d.\n", rirq);
1139	/** FIXME: tickle the MB redirector chip */
1140	return ???;
1141#else
1142	printf("Freeing (NOT implemented) redirected ISA irq %d.\n", rirq);
1143	return 0;
1144#endif  /* READY */
1145}
1146
1147
1148/*
1149 * Reprogram the MB chipset to NOT redirect a PCI INTerrupt
1150 */
1151int
1152undirect_pci_irq(int rirq)
1153{
1154#if defined(READY)
1155	if (bootverbose)
1156		printf("Freeing redirected PCI irq %d.\n", rirq);
1157
1158	/** FIXME: tickle the MB redirector chip */
1159	return ???;
1160#else
1161	if (bootverbose)
1162		printf("Freeing (NOT implemented) redirected PCI irq %d.\n",
1163		       rirq);
1164	return 0;
1165#endif  /* READY */
1166}
1167
1168
1169/*
1170 * given a bus ID, return:
1171 *  the bus type if found
1172 *  -1 if NOT found
1173 */
1174int
1175apic_bus_type(int id)
1176{
1177	int     x;
1178
1179	for (x = 0; x < mp_nbusses; ++x)
1180		if (bus_data[x].bus_id == id)
1181			return bus_data[x].bus_type;
1182
1183	return -1;
1184}
1185
1186
1187/*
1188 * given a LOGICAL APIC# and pin#, return:
1189 *  the associated src bus ID if found
1190 *  -1 if NOT found
1191 */
1192int
1193apic_src_bus_id(int apic, int pin)
1194{
1195	int     x;
1196
1197	/* search each of the possible INTerrupt sources */
1198	for (x = 0; x < nintrs; ++x)
1199		if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
1200		    (pin == io_apic_ints[x].dst_apic_int))
1201			return (io_apic_ints[x].src_bus_id);
1202
1203	return -1;		/* NOT found */
1204}
1205
1206
1207/*
1208 * given a LOGICAL APIC# and pin#, return:
1209 *  the associated src bus IRQ if found
1210 *  -1 if NOT found
1211 */
1212int
1213apic_src_bus_irq(int apic, int pin)
1214{
1215	int     x;
1216
1217	for (x = 0; x < nintrs; x++)
1218		if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
1219		    (pin == io_apic_ints[x].dst_apic_int))
1220			return (io_apic_ints[x].src_bus_irq);
1221
1222	return -1;		/* NOT found */
1223}
1224
1225
1226/*
1227 * given a LOGICAL APIC# and pin#, return:
1228 *  the associated INTerrupt type if found
1229 *  -1 if NOT found
1230 */
1231int
1232apic_int_type(int apic, int pin)
1233{
1234	int     x;
1235
1236	/* search each of the possible INTerrupt sources */
1237	for (x = 0; x < nintrs; ++x)
1238		if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
1239		    (pin == io_apic_ints[x].dst_apic_int))
1240			return (io_apic_ints[x].int_type);
1241
1242	return -1;		/* NOT found */
1243}
1244
1245
1246/*
1247 * given a LOGICAL APIC# and pin#, return:
1248 *  the associated trigger mode if found
1249 *  -1 if NOT found
1250 */
1251int
1252apic_trigger(int apic, int pin)
1253{
1254	int     x;
1255
1256	/* search each of the possible INTerrupt sources */
1257	for (x = 0; x < nintrs; ++x)
1258		if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
1259		    (pin == io_apic_ints[x].dst_apic_int))
1260			return ((io_apic_ints[x].int_flags >> 2) & 0x03);
1261
1262	return -1;		/* NOT found */
1263}
1264
1265
1266/*
1267 * given a LOGICAL APIC# and pin#, return:
1268 *  the associated 'active' level if found
1269 *  -1 if NOT found
1270 */
1271int
1272apic_polarity(int apic, int pin)
1273{
1274	int     x;
1275
1276	/* search each of the possible INTerrupt sources */
1277	for (x = 0; x < nintrs; ++x)
1278		if ((apic == ID_TO_IO(io_apic_ints[x].dst_apic_id)) &&
1279		    (pin == io_apic_ints[x].dst_apic_int))
1280			return (io_apic_ints[x].int_flags & 0x03);
1281
1282	return -1;		/* NOT found */
1283}
1284
1285
1286/*
1287 * set data according to MP defaults
1288 * FIXME: probably not complete yet...
1289 */
1290static void
1291default_mp_table(int type)
1292{
1293	int     ap_cpu_id;
1294#if defined(APIC_IO)
1295	u_int32_t ux;
1296	int     io_apic_id;
1297	int     pin;
1298#endif	/* APIC_IO */
1299
1300#if 0
1301	printf("  MP default config type: %d\n", type);
1302	switch (type) {
1303	case 1:
1304		printf("   bus: ISA, APIC: 82489DX\n");
1305		break;
1306	case 2:
1307		printf("   bus: EISA, APIC: 82489DX\n");
1308		break;
1309	case 3:
1310		printf("   bus: EISA, APIC: 82489DX\n");
1311		break;
1312	case 4:
1313		printf("   bus: MCA, APIC: 82489DX\n");
1314		break;
1315	case 5:
1316		printf("   bus: ISA+PCI, APIC: Integrated\n");
1317		break;
1318	case 6:
1319		printf("   bus: EISA+PCI, APIC: Integrated\n");
1320		break;
1321	case 7:
1322		printf("   bus: MCA+PCI, APIC: Integrated\n");
1323		break;
1324	default:
1325		printf("   future type\n");
1326		break;
1327		/* NOTREACHED */
1328	}
1329#endif	/* 0 */
1330
1331	boot_cpu_id = (lapic.id & APIC_ID_MASK) >> 24;
1332	ap_cpu_id = (boot_cpu_id == 0) ? 1 : 0;
1333
1334	/* BSP */
1335	CPU_TO_ID(0) = boot_cpu_id;
1336	ID_TO_CPU(boot_cpu_id) = 0;
1337
1338	/* one and only AP */
1339	CPU_TO_ID(1) = ap_cpu_id;
1340	ID_TO_CPU(ap_cpu_id) = 1;
1341
1342#if defined(APIC_IO)
1343	/* one and only IO APIC */
1344	io_apic_id = (io_apic_read(0, IOAPIC_ID) & APIC_ID_MASK) >> 24;
1345
1346	/*
1347	 * sanity check, refer to MP spec section 3.6.6, last paragraph
1348	 * necessary as some hardware isn't properly setting up the IO APIC
1349	 */
1350#if defined(REALLY_ANAL_IOAPICID_VALUE)
1351	if (io_apic_id != 2) {
1352#else
1353	if ((io_apic_id == 0) || (io_apic_id == 1) || (io_apic_id == 15)) {
1354#endif	/* REALLY_ANAL_IOAPICID_VALUE */
1355		ux = io_apic_read(0, IOAPIC_ID);	/* get current contents */
1356		ux &= ~APIC_ID_MASK;	/* clear the ID field */
1357		ux |= 0x02000000;	/* set it to '2' */
1358		io_apic_write(0, IOAPIC_ID, ux);	/* write new value */
1359		ux = io_apic_read(0, IOAPIC_ID);	/* re-read && test */
1360		if ((ux & APIC_ID_MASK) != 0x02000000)
1361			panic("can't control IO APIC ID, reg: 0x%08x", ux);
1362		io_apic_id = 2;
1363	}
1364	IO_TO_ID(0) = io_apic_id;
1365	ID_TO_IO(io_apic_id) = 0;
1366#endif	/* APIC_IO */
1367
1368	/* fill out bus entries */
1369	switch (type) {
1370	case 1:
1371	case 2:
1372	case 3:
1373	case 5:
1374	case 6:
1375		bus_data[0].bus_id = default_data[type - 1][1];
1376		bus_data[0].bus_type = default_data[type - 1][2];
1377		bus_data[1].bus_id = default_data[type - 1][3];
1378		bus_data[1].bus_type = default_data[type - 1][4];
1379		break;
1380
1381	/* case 4: case 7:		   MCA NOT supported */
1382	default:		/* illegal/reserved */
1383		panic("BAD default MP config: %d", type);
1384		/* NOTREACHED */
1385	}
1386
1387#if defined(APIC_IO)
1388	/* general cases from MP v1.4, table 5-2 */
1389	for (pin = 0; pin < 16; ++pin) {
1390		io_apic_ints[pin].int_type = 0;
1391		io_apic_ints[pin].int_flags = 0x05;	/* edge/active-hi */
1392		io_apic_ints[pin].src_bus_id = 0;
1393		io_apic_ints[pin].src_bus_irq = pin;	/* IRQ2 caught below */
1394		io_apic_ints[pin].dst_apic_id = io_apic_id;
1395		io_apic_ints[pin].dst_apic_int = pin;	/* 1-to-1 */
1396	}
1397
1398	/* special cases from MP v1.4, table 5-2 */
1399	if (type == 2) {
1400		io_apic_ints[2].int_type = 0xff;	/* N/C */
1401		io_apic_ints[13].int_type = 0xff;	/* N/C */
1402#if !defined(APIC_MIXED_MODE)
1403		/** FIXME: ??? */
1404		panic("sorry, can't support type 2 default yet");
1405#endif	/* APIC_MIXED_MODE */
1406	}
1407	else
1408		io_apic_ints[2].src_bus_irq = 0;	/* ISA IRQ0 is on APIC INT 2 */
1409
1410	if (type == 7)
1411		io_apic_ints[0].int_type = 0xff;	/* N/C */
1412	else
1413		io_apic_ints[0].int_type = 3;	/* vectored 8259 */
1414#endif	/* APIC_IO */
1415}
1416
1417
1418/*
1419 * initialize all the SMP locks
1420 */
1421
1422/* lock the com (tty) data structures */
1423struct simplelock	com_lock;
1424
1425static void
1426init_locks(void)
1427{
1428	/*
1429	 * Get the initial mp_lock with a count of 1 for the BSP.
1430	 * This uses a LOGICAL cpu ID, ie BSP == 0.
1431	 */
1432	mp_lock = 0x00000001;
1433
1434	/* ISR uses its own "giant lock" */
1435	isr_lock = 0x00000000;
1436
1437	/* serializes FAST_INTR() accesses */
1438	s_lock_init((struct simplelock*)&fast_intr_lock);
1439
1440	/* serializes INTR() accesses */
1441	s_lock_init((struct simplelock*)&intr_lock);
1442
1443	/* locks the IO APIC and apic_imen accesses */
1444	s_lock_init((struct simplelock*)&imen_lock);
1445
1446	/* locks cpl accesses */
1447	s_lock_init((struct simplelock*)&cpl_lock);
1448
1449	/* locks com (tty) data/hardware accesses: a FASTINTR() */
1450	s_lock_init((struct simplelock*)&com_lock);
1451}
1452
1453
1454/*
1455 * start each AP in our list
1456 */
1457static int
1458start_all_aps(u_int boot_addr)
1459{
1460	int     x, i;
1461	u_char  mpbiosreason;
1462	u_long  mpbioswarmvec;
1463	pd_entry_t newptd;
1464	pt_entry_t newpt;
1465	int *newpp;
1466
1467	POSTCODE(START_ALL_APS_POST);
1468
1469	/* initialize BSP's local APIC */
1470	apic_initialize();
1471
1472	/* install the AP 1st level boot code */
1473	install_ap_tramp(boot_addr);
1474
1475
1476	/* save the current value of the warm-start vector */
1477	mpbioswarmvec = *((u_long *) WARMBOOT_OFF);
1478	outb(CMOS_REG, BIOS_RESET);
1479	mpbiosreason = inb(CMOS_DATA);
1480
1481	/* record BSP in CPU map */
1482	all_cpus = 1;
1483
1484	/* start each AP */
1485	for (x = 1; x <= mp_naps; ++x) {
1486
1487		/* HACK HACK HACK !!! */
1488
1489		/* alloc new page table directory */
1490		newptd = (pd_entry_t)(kmem_alloc(kernel_map, PAGE_SIZE));
1491
1492		/* clone currently active one (ie: IdlePTD) */
1493		bcopy(PTD, newptd, PAGE_SIZE);	/* inc prv page pde */
1494
1495		/* set up 0 -> 4MB P==V mapping for AP boot */
1496		newptd[0] = PG_V | PG_RW | ((u_long)KPTphys & PG_FRAME);
1497
1498		/* store PTD for this AP */
1499		bootPTD = (pd_entry_t)vtophys(newptd);
1500
1501		/* alloc new page table page */
1502		newpt = (pt_entry_t)(kmem_alloc(kernel_map, PAGE_SIZE));
1503
1504		/* set the new PTD's private page to point there */
1505		newptd[MPPTDI] = PG_V | PG_RW | vtophys(newpt);
1506
1507		/* install self referential entry */
1508		newptd[PTDPTDI] = PG_V | PG_RW | vtophys(newptd);
1509
1510		/* get a new private data page */
1511		newpp = (int *)kmem_alloc(kernel_map, PAGE_SIZE);
1512
1513		/* wire it into the private page table page */
1514		newpt[0] = PG_V | PG_RW | vtophys(newpp);
1515
1516		/* wire the ptp into itself for access */
1517		newpt[1] = PG_V | PG_RW | vtophys(newpt);
1518
1519		/* and the local apic */
1520		newpt[2] = SMP_prvpt[2];
1521
1522		/* and the IO apic mapping[s] */
1523		for (i = 16; i < 32; i++)
1524			newpt[i] = SMP_prvpt[i];
1525
1526		/* prime data page for it to use */
1527		newpp[0] = x;		/* cpuid */
1528		newpp[1] = 0;		/* curproc */
1529		newpp[2] = 0;		/* curpcb */
1530		newpp[3] = 0;		/* npxproc */
1531		newpp[4] = 0;		/* runtime.tv_sec */
1532		newpp[5] = 0;		/* runtime.tv_usec */
1533		newpp[6] = x << 24;	/* cpu_lockid */
1534
1535		/* XXX NOTE: ABANDON bootPTD for now!!!! */
1536
1537		/* END REVOLTING HACKERY */
1538
1539		/* setup a vector to our boot code */
1540		*((volatile u_short *) WARMBOOT_OFF) = WARMBOOT_TARGET;
1541		*((volatile u_short *) WARMBOOT_SEG) = (boot_addr >> 4);
1542		outb(CMOS_REG, BIOS_RESET);
1543		outb(CMOS_DATA, BIOS_WARM);	/* 'warm-start' */
1544
1545		/* attempt to start the Application Processor */
1546		CHECK_INIT(99);	/* setup checkpoints */
1547		if (!start_ap(x, boot_addr)) {
1548			printf("AP #%d (PHY# %d) failed!\n", x, CPU_TO_ID(x));
1549			CHECK_PRINT("trace");	/* show checkpoints */
1550			/* better panic as the AP may be running loose */
1551			printf("panic y/n? [y] ");
1552			if (cngetc() != 'n')
1553				panic("bye-bye");
1554		}
1555		CHECK_PRINT("trace");		/* show checkpoints */
1556
1557		/* record its version info */
1558		cpu_apic_versions[x] = cpu_apic_versions[0];
1559
1560		all_cpus |= (1 << x);		/* record AP in CPU map */
1561	}
1562
1563	/* build our map of 'other' CPUs */
1564	other_cpus = all_cpus & ~(1 << cpuid);
1565
1566	/* fill in our (BSP) APIC version */
1567	cpu_apic_versions[0] = lapic.version;
1568
1569	/* restore the warmstart vector */
1570	*(u_long *) WARMBOOT_OFF = mpbioswarmvec;
1571	outb(CMOS_REG, BIOS_RESET);
1572	outb(CMOS_DATA, mpbiosreason);
1573
1574	pmap_set_opt_bsp();
1575
1576	/* number of APs actually started */
1577	return mp_ncpus - 1;
1578}
1579
1580
1581/*
1582 * load the 1st level AP boot code into base memory.
1583 */
1584
1585/* targets for relocation */
1586extern void bigJump(void);
1587extern void bootCodeSeg(void);
1588extern void bootDataSeg(void);
1589extern void MPentry(void);
1590extern u_int MP_GDT;
1591extern u_int mp_gdtbase;
1592
1593static void
1594install_ap_tramp(u_int boot_addr)
1595{
1596	int     x;
1597	int     size = *(int *) ((u_long) & bootMP_size);
1598	u_char *src = (u_char *) ((u_long) bootMP);
1599	u_char *dst = (u_char *) boot_addr + KERNBASE;
1600	u_int   boot_base = (u_int) bootMP;
1601	u_int8_t *dst8;
1602	u_int16_t *dst16;
1603	u_int32_t *dst32;
1604
1605	POSTCODE(INSTALL_AP_TRAMP_POST);
1606
1607	for (x = 0; x < size; ++x)
1608		*dst++ = *src++;
1609
1610	/*
1611	 * modify addresses in code we just moved to basemem. unfortunately we
1612	 * need fairly detailed info about mpboot.s for this to work.  changes
1613	 * to mpboot.s might require changes here.
1614	 */
1615
1616	/* boot code is located in KERNEL space */
1617	dst = (u_char *) boot_addr + KERNBASE;
1618
1619	/* modify the lgdt arg */
1620	dst32 = (u_int32_t *) (dst + ((u_int) & mp_gdtbase - boot_base));
1621	*dst32 = boot_addr + ((u_int) & MP_GDT - boot_base);
1622
1623	/* modify the ljmp target for MPentry() */
1624	dst32 = (u_int32_t *) (dst + ((u_int) bigJump - boot_base) + 1);
1625	*dst32 = ((u_int) MPentry - KERNBASE);
1626
1627	/* modify the target for boot code segment */
1628	dst16 = (u_int16_t *) (dst + ((u_int) bootCodeSeg - boot_base));
1629	dst8 = (u_int8_t *) (dst16 + 1);
1630	*dst16 = (u_int) boot_addr & 0xffff;
1631	*dst8 = ((u_int) boot_addr >> 16) & 0xff;
1632
1633	/* modify the target for boot data segment */
1634	dst16 = (u_int16_t *) (dst + ((u_int) bootDataSeg - boot_base));
1635	dst8 = (u_int8_t *) (dst16 + 1);
1636	*dst16 = (u_int) boot_addr & 0xffff;
1637	*dst8 = ((u_int) boot_addr >> 16) & 0xff;
1638}
1639
1640
1641/*
1642 * this function starts the AP (application processor) identified
1643 * by the APIC ID 'physicalCpu'.  It does quite a "song and dance"
1644 * to accomplish this.  This is necessary because of the nuances
1645 * of the different hardware we might encounter.  It ain't pretty,
1646 * but it seems to work.
1647 */
1648static int
1649start_ap(int logical_cpu, u_int boot_addr)
1650{
1651	int     physical_cpu;
1652	int     vector;
1653	int     cpus;
1654	u_long  icr_lo, icr_hi;
1655
1656	POSTCODE(START_AP_POST);
1657
1658	/* get the PHYSICAL APIC ID# */
1659	physical_cpu = CPU_TO_ID(logical_cpu);
1660
1661	/* calculate the vector */
1662	vector = (boot_addr >> 12) & 0xff;
1663
1664	/* used as a watchpoint to signal AP startup */
1665	cpus = mp_ncpus;
1666
1667	/*
1668	 * first we do an INIT/RESET IPI this INIT IPI might be run, reseting
1669	 * and running the target CPU. OR this INIT IPI might be latched (P5
1670	 * bug), CPU waiting for STARTUP IPI. OR this INIT IPI might be
1671	 * ignored.
1672	 */
1673
1674	/* setup the address for the target AP */
1675	icr_hi = lapic.icr_hi & ~APIC_ID_MASK;
1676	icr_hi |= (physical_cpu << 24);
1677	lapic.icr_hi = icr_hi;
1678
1679	/* do an INIT IPI: assert RESET */
1680	icr_lo = lapic.icr_lo & 0xfff00000;
1681	lapic.icr_lo = icr_lo | 0x0000c500;
1682
1683	/* wait for pending status end */
1684	while (lapic.icr_lo & APIC_DELSTAT_MASK)
1685		 /* spin */ ;
1686
1687	/* do an INIT IPI: deassert RESET */
1688	lapic.icr_lo = icr_lo | 0x00008500;
1689
1690	/* wait for pending status end */
1691	u_sleep(10000);		/* wait ~10mS */
1692	while (lapic.icr_lo & APIC_DELSTAT_MASK)
1693		 /* spin */ ;
1694
1695	/*
1696	 * next we do a STARTUP IPI: the previous INIT IPI might still be
1697	 * latched, (P5 bug) this 1st STARTUP would then terminate
1698	 * immediately, and the previously started INIT IPI would continue. OR
1699	 * the previous INIT IPI has already run. and this STARTUP IPI will
1700	 * run. OR the previous INIT IPI was ignored. and this STARTUP IPI
1701	 * will run.
1702	 */
1703
1704	/* do a STARTUP IPI */
1705	lapic.icr_lo = icr_lo | 0x00000600 | vector;
1706	while (lapic.icr_lo & APIC_DELSTAT_MASK)
1707		 /* spin */ ;
1708	u_sleep(200);		/* wait ~200uS */
1709
1710	/*
1711	 * finally we do a 2nd STARTUP IPI: this 2nd STARTUP IPI should run IF
1712	 * the previous STARTUP IPI was cancelled by a latched INIT IPI. OR
1713	 * this STARTUP IPI will be ignored, as only ONE STARTUP IPI is
1714	 * recognized after hardware RESET or INIT IPI.
1715	 */
1716
1717	lapic.icr_lo = icr_lo | 0x00000600 | vector;
1718	while (lapic.icr_lo & APIC_DELSTAT_MASK)
1719		 /* spin */ ;
1720	u_sleep(200);		/* wait ~200uS */
1721
1722	/* wait for it to start */
1723	set_apic_timer(5000000);/* == 5 seconds */
1724	while (read_apic_timer())
1725		if (mp_ncpus > cpus)
1726			return 1;	/* return SUCCESS */
1727
1728	return 0;		/* return FAILURE */
1729}
1730
1731
1732/*
1733 * Flush the TLB on all other CPU's
1734 *
1735 * XXX: Needs to handshake and wait for completion before proceding.
1736 */
1737void
1738smp_invltlb(void)
1739{
1740#if defined(APIC_IO)
1741	if (smp_active && invltlb_ok)
1742		all_but_self_ipi(XINVLTLB_OFFSET);
1743#endif  /* APIC_IO */
1744}
1745
1746void
1747invlpg(u_int addr)
1748{
1749	__asm   __volatile("invlpg (%0)"::"r"(addr):"memory");
1750
1751	/* send a message to the other CPUs */
1752	smp_invltlb();
1753}
1754
1755void
1756invltlb(void)
1757{
1758	u_long  temp;
1759
1760	/*
1761	 * This should be implemented as load_cr3(rcr3()) when load_cr3() is
1762	 * inlined.
1763	 */
1764	__asm __volatile("movl %%cr3, %0; movl %0, %%cr3":"=r"(temp) :: "memory");
1765
1766	/* send a message to the other CPUs */
1767	smp_invltlb();
1768}
1769
1770
1771/*
1772 * When called the executing CPU will send an IPI to all other CPUs
1773 *  requesting that they halt execution.
1774 *
1775 * Usually (but not necessarily) called with 'other_cpus' as its arg.
1776 *
1777 *  - Signals all CPUs in map to stop.
1778 *  - Waits for each to stop.
1779 *
1780 * Returns:
1781 *  -1: error
1782 *   0: NA
1783 *   1: ok
1784 *
1785 * XXX FIXME: this is not MP-safe, needs a lock to prevent multiple CPUs
1786 *            from executing at same time.
1787 */
1788int
1789stop_cpus( u_int map )
1790{
1791	if (!smp_active)
1792		return 0;
1793
1794	/* send IPI to all CPUs in map */
1795	stopped_cpus = 0;
1796
1797	/* send the Xcpustop IPI to all CPUs in map */
1798	selected_apic_ipi(map, XCPUSTOP_OFFSET, APIC_DELMODE_FIXED);
1799
1800	while (stopped_cpus != map)
1801		/* spin */ ;
1802
1803	return 1;
1804}
1805
1806
1807/*
1808 * Called by a CPU to restart stopped CPUs.
1809 *
1810 * Usually (but not necessarily) called with 'stopped_cpus' as its arg.
1811 *
1812 *  - Signals all CPUs in map to restart.
1813 *  - Waits for each to restart.
1814 *
1815 * Returns:
1816 *  -1: error
1817 *   0: NA
1818 *   1: ok
1819 */
1820int
1821restart_cpus( u_int map )
1822{
1823	if (!smp_active)
1824		return 0;
1825
1826	started_cpus = map;		/* signal other cpus to restart */
1827
1828	while (started_cpus)		/* wait for each to clear its bit */
1829		/* spin */ ;
1830
1831	return 1;
1832}
1833