mptable.c 31.3 KB
Newer Older
1
/*-
2
3
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
4
 * Copyright (c) 2003 John Baldwin <jhb@FreeBSD.org>
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 * Copyright (c) 1996, by Steve Passe
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the developer may NOT be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

obrien's avatar
obrien committed
29
30
31
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

32
#include "opt_mptable_force_htt.h"
bde's avatar
bde committed
33
#include <sys/param.h>
34
#include <sys/systm.h>
35
#include <sys/bus.h>
36
#include <sys/kernel.h>
37
#include <sys/limits.h>
38
#include <sys/malloc.h>
royger's avatar
royger committed
39
#include <sys/smp.h>
40
41
42
#ifdef NEW_PCIB
#include <sys/rman.h>
#endif
43

44
45
46
#include <vm/vm.h>
#include <vm/vm_param.h>
#include <vm/pmap.h>
47

48
49
50
51
#include <dev/pci/pcivar.h>
#ifdef NEW_PCIB
#include <dev/pci/pcib_private.h>
#endif
52
#include <x86/apicreg.h>
53
#include <x86/mptable.h>
54
55
#include <machine/frame.h>
#include <machine/intr_machdep.h>
56
#include <x86/apicvar.h>
57
#include <machine/md_var.h>
58
59
60
#ifdef NEW_PCIB
#include <machine/resource.h>
#endif
61
#include <machine/specialreg.h>
62

63
64
65
/* string defined by the Intel MP Spec as identifying the MP table */
#define	MP_SIG			0x5f504d5f	/* _MP_ */

66
67
68
#ifdef __amd64__
#define	MAX_LAPIC_ID		63	/* Max local APIC ID for HTT fixup */
#else
69
#define	MAX_LAPIC_ID		31	/* Max local APIC ID for HTT fixup */
70
#endif
71

72
73
74
#define BIOS_BASE		(0xf0000)
#define BIOS_SIZE		(0x10000)
#define BIOS_COUNT		(BIOS_SIZE/4)
75

76
typedef	void mptable_entry_handler(u_char *entry, void *arg);
77
typedef	void mptable_extended_entry_handler(ext_entry_ptr entry, void *arg);
fsmp's avatar
fsmp committed
78

79
80
81
82
83
84
85
/* descriptions of MP table entries */
typedef struct BASETABLE_ENTRY {
	uint8_t	type;
	uint8_t	length;
	uint8_t	name[16];
}       basetable_entry;

86
87
88
89
90
91
92
93
static basetable_entry basetable_entry_types[] =
{
	{0, 20, "Processor"},
	{1, 8, "Bus"},
	{2, 8, "I/O APIC"},
	{3, 8, "I/O INT"},
	{4, 8, "Local INT"}
};
fsmp's avatar
fsmp committed
94

95
96
97
98
99
100
typedef struct BUSDATA {
	u_char  bus_id;
	enum busTypes bus_type;
}       bus_datum;

typedef struct INTDATA {
fsmp's avatar
fsmp committed
101
102
103
104
105
106
	u_char  int_type;
	u_short int_flags;
	u_char  src_bus_id;
	u_char  src_bus_irq;
	u_char  dst_apic_id;
	u_char  dst_apic_int;
107
108
	u_char	int_vector;
}       io_int, local_int;
fsmp's avatar
fsmp committed
109

110
typedef struct BUSTYPENAME {
fsmp's avatar
fsmp committed
111
	u_char  type;
112
113
	char    name[7];
}       bus_type_name;
114

115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* From MP spec v1.4, table 4-8. */
static bus_type_name bus_type_table[] =
{
	{UNKNOWN_BUSTYPE, "CBUS  "},
	{UNKNOWN_BUSTYPE, "CBUSII"},
	{EISA, "EISA  "},
	{UNKNOWN_BUSTYPE, "FUTURE"},
	{UNKNOWN_BUSTYPE, "INTERN"},
	{ISA, "ISA   "},
	{UNKNOWN_BUSTYPE, "MBI   "},
	{UNKNOWN_BUSTYPE, "MBII  "},
	{MCA, "MCA   "},
	{UNKNOWN_BUSTYPE, "MPI   "},
	{UNKNOWN_BUSTYPE, "MPSA  "},
	{UNKNOWN_BUSTYPE, "NUBUS "},
	{PCI, "PCI   "},
	{UNKNOWN_BUSTYPE, "PCMCIA"},
	{UNKNOWN_BUSTYPE, "TC    "},
	{UNKNOWN_BUSTYPE, "VL    "},
	{UNKNOWN_BUSTYPE, "VME   "},
	{UNKNOWN_BUSTYPE, "XPRESS"}
};
137

138
139
140
141
142
143
144
145
146
147
148
149
/* From MP spec v1.4, table 5-1. */
static int default_data[7][5] =
{
/*   nbus, id0, type0, id1, type1 */
	{1, 0, ISA, 255, NOBUS},
	{1, 0, EISA, 255, NOBUS},
	{1, 0, EISA, 255, NOBUS},
	{1, 0, MCA, 255, NOBUS},
	{2, 0, ISA, 1, PCI},
	{2, 0, EISA, 1, PCI},
	{2, 0, MCA, 1, PCI}
};
150

151
152
153
154
struct pci_probe_table_args {
	u_char bus;
	u_char found;
};
155

156
157
158
159
160
struct pci_route_interrupt_args {
	u_char bus;		/* Source bus. */
	u_char irq;		/* Source slot:pin. */
	int vector;		/* Return value. */
};
161

162
163
static mpfps_t mpfps;
static mpcth_t mpct;
164
static ext_entry_ptr mpet;
royger's avatar
royger committed
165
static void *ioapics[IOAPIC_MAX_ID + 1];
166
167
168
169
static bus_datum *busses;
static int mptable_nioapics, mptable_nbusses, mptable_maxbusid;
static int pci0 = -1;

170
static MALLOC_DEFINE(M_MPTABLE, "mptable", "MP Table Items");
171

172
173
174
175
176
static enum intr_polarity conforming_polarity(u_char src_bus,
	    u_char src_bus_irq);
static enum intr_trigger conforming_trigger(u_char src_bus, u_char src_bus_irq);
static enum intr_polarity intentry_polarity(int_entry_ptr intr);
static enum intr_trigger intentry_trigger(int_entry_ptr intr);
177
178
179
static int	lookup_bus_type(char *name);
static void	mptable_count_items(void);
static void	mptable_count_items_handler(u_char *entry, void *arg);
180
#ifdef MPTABLE_FORCE_HTT
181
static void	mptable_hyperthread_fixup(u_int id_mask);
182
#endif
183
184
185
static void	mptable_parse_apics_and_busses(void);
static void	mptable_parse_apics_and_busses_handler(u_char *entry,
    void *arg);
186
static void	mptable_parse_default_config_ints(void);
187
188
189
190
191
192
193
194
195
196
static void	mptable_parse_ints(void);
static void	mptable_parse_ints_handler(u_char *entry, void *arg);
static void	mptable_parse_io_int(int_entry_ptr intr);
static void	mptable_parse_local_int(int_entry_ptr intr);
static void	mptable_pci_probe_table_handler(u_char *entry, void *arg);
static void	mptable_pci_route_interrupt_handler(u_char *entry, void *arg);
static void	mptable_pci_setup(void);
static int	mptable_probe(void);
static int	mptable_probe_cpus(void);
static void	mptable_probe_cpus_handler(u_char *entry, void *arg __unused);
197
static void	mptable_setup_cpus_handler(u_char *entry, void *arg __unused);
198
199
200
static void	mptable_register(void *dummy);
static int	mptable_setup_local(void);
static int	mptable_setup_io(void);
jhb's avatar
jhb committed
201
#ifdef NEW_PCIB
202
203
static void	mptable_walk_extended_table(
    mptable_extended_entry_handler *handler, void *arg);
jhb's avatar
jhb committed
204
#endif
205
static void	mptable_walk_table(mptable_entry_handler *handler, void *arg);
fsmp's avatar
fsmp committed
206
207
static int	search_for_sig(u_int32_t target, int count);

208
209
210
211
212
213
214
static struct apic_enumerator mptable_enumerator = {
	"MPTable",
	mptable_probe,
	mptable_probe_cpus,
	mptable_setup_local,
	mptable_setup_io
};
ps's avatar
ps committed
215

216
/*
217
 * look for the MP spec signature
218
219
 */

220
221
static int
search_for_sig(u_int32_t target, int count)
222
{
223
	int     x;
kib's avatar
kib committed
224
	u_int32_t *addr;
225

kib's avatar
kib committed
226
227
228
229
230
#ifdef __amd64__
	addr = (u_int32_t *) (KERNBASE + target);
#else /* __i386__ */
	addr = (u_int32_t *) (PMAP_MAP_LOW + target);
#endif
231
232
233
234
235
	for (x = 0; x < count; x += 4)
		if (addr[x] == MP_SIG)
			/* make array index a byte index */
			return (target + (x * sizeof(u_int32_t)));
	return (-1);
236
237
}

238
239
static int
lookup_bus_type(char *name)
240
{
241
	int     x;
242

243
244
245
	for (x = 0; x < MAX_BUSTYPE; ++x)
		if (strncmp(bus_type_table[x].name, name, 6) == 0)
			return (bus_type_table[x].type);
246

247
	return (UNKNOWN_BUSTYPE);
248
249
}

fsmp's avatar
fsmp committed
250
251
252
/*
 * Look for an Intel MP spec table (ie, SMP capable hardware).
 */
253
254
static int
mptable_probe(void)
fsmp's avatar
fsmp committed
255
256
257
258
259
260
{
	int     x;
	u_long  segment;
	u_int32_t target;

	/* see if EBDA exists */
kib's avatar
kib committed
261
262
263
264
265
266
267
	if ((segment = (u_long) * (u_short *) (
#ifdef __amd64__
	    KERNBASE
#else /* __i386__ */
	    PMAP_MAP_LOW
#endif
	    + 0x40e)) != 0) {
fsmp's avatar
fsmp committed
268
269
270
271
272
273
		/* search first 1K of EBDA */
		target = (u_int32_t) (segment << 4);
		if ((x = search_for_sig(target, 1024 / 4)) >= 0)
			goto found;
	} else {
		/* last 1K of base memory, effective 'top of base' passed in */
274
		target = (u_int32_t) ((basemem * 1024) - 0x400);
fsmp's avatar
fsmp committed
275
276
277
278
279
280
281
282
283
284
		if ((x = search_for_sig(target, 1024 / 4)) >= 0)
			goto found;
	}

	/* search the BIOS */
	target = (u_int32_t) BIOS_BASE;
	if ((x = search_for_sig(target, BIOS_COUNT)) >= 0)
		goto found;

	/* nothing found */
285
	return (ENXIO);
fsmp's avatar
fsmp committed
286

fsmp's avatar
fsmp committed
287
found:
288
	mpfps = (mpfps_t)(KERNBASE + x);
fsmp's avatar
fsmp committed
289

290
	/* Map in the configuration table if it exists. */
291
292
293
294
295
296
297
298
299
300
301
	if (mpfps->config_type != 0) {
		if (bootverbose)
			printf(
		"MP Table version 1.%d found using Default Configuration %d\n",
			    mpfps->spec_rev, mpfps->config_type);
		if (mpfps->config_type != 5 && mpfps->config_type != 6) {
			printf(
			"MP Table Default Configuration %d is unsupported\n",
			    mpfps->config_type);
			return (ENXIO);
		}
302
		mpct = NULL;
303
	} else {
304
305
306
307
308
309
310
311
312
313
314
315
		if ((uintptr_t)mpfps->pap >= 1024 * 1024) {
			printf("%s: Unable to map MP Configuration Table\n",
			    __func__);
			return (ENXIO);
		}
		mpct = (mpcth_t)(KERNBASE + (uintptr_t)mpfps->pap);
		if (mpct->base_table_length + (uintptr_t)mpfps->pap >=
		    1024 * 1024) {
			printf("%s: Unable to map end of MP Config Table\n",
			    __func__);
			return (ENXIO);
		}
316
317
318
319
320
		if (mpct->extended_table_length != 0 &&
		    mpct->extended_table_length + mpct->base_table_length +
		    (uintptr_t)mpfps->pap < 1024 * 1024)
			mpet = (ext_entry_ptr)((char *)mpct +
			    mpct->base_table_length);
321
322
323
324
325
326
327
328
329
330
331
332
		if (mpct->signature[0] != 'P' || mpct->signature[1] != 'C' ||
		    mpct->signature[2] != 'M' || mpct->signature[3] != 'P') {
			printf("%s: MP Config Table has bad signature: %c%c%c%c\n",
			    __func__, mpct->signature[0], mpct->signature[1],
			    mpct->signature[2], mpct->signature[3]);
			return (ENXIO);
		}
		if (bootverbose)
			printf(
			"MP Configuration Table version 1.%d found at %p\n",
			    mpct->spec_rev, mpct);
	}
333

334
	return (-100);
335
}
fsmp's avatar
fsmp committed
336

337
/*
338
 * Run through the MP table enumerating CPUs.
339
 */
340
341
static int
mptable_probe_cpus(void)
342
{
343
	u_int cpu_mask;
344

345
346
	/* Is this a pre-defined config? */
	if (mpfps->config_type != 0) {
royger's avatar
royger committed
347
#ifdef SMP
348
349
		mp_ncpus = 2;
		mp_maxid = 1;
royger's avatar
royger committed
350
#endif
351
		max_apic_id = 1;
352
353
354
355
	} else {
		mptable_walk_table(mptable_probe_cpus_handler, &cpu_mask);
	}
	return (0);
356
357
358
}

/*
359
 * Initialize the local APIC on the BSP.
360
 */
361
362
static int
mptable_setup_local(void)
363
{
364
	vm_paddr_t addr;
royger's avatar
royger committed
365
	u_int cpu_mask;
366

367
368
369
	/* Is this a pre-defined config? */
	printf("MPTable: <");
	if (mpfps->config_type != 0) {
370
371
		lapic_create(0, 1);
		lapic_create(1, 0);
372
		addr = DEFAULT_APIC_BASE;
373
		printf("Default Configuration %d", mpfps->config_type);
374

375
	} else {
376
377
378
379
380
		cpu_mask = 0;
		mptable_walk_table(mptable_setup_cpus_handler, &cpu_mask);
#ifdef MPTABLE_FORCE_HTT
		mptable_hyperthread_fixup(cpu_mask);
#endif
381
		addr = mpct->apic_address;
382
383
		printf("%.*s %.*s", (int)sizeof(mpct->oem_id), mpct->oem_id,
		    (int)sizeof(mpct->product_id), mpct->product_id);
384
	}
385
	printf(">\n");
386
	lapic_init(addr);
387
	return (0);
388
389
390
}

/*
391
 * Run through the MP table enumerating I/O APICs.
392
 */
393
394
static int
mptable_setup_io(void)
395
{
396
397
	int i;
	u_char byte;
398

399
400
401
402
403
404
	/* First, we count individual items and allocate arrays. */
	mptable_count_items();
	busses = malloc((mptable_maxbusid + 1) * sizeof(bus_datum), M_MPTABLE,
	    M_WAITOK);
	for (i = 0; i <= mptable_maxbusid; i++)
		busses[i].bus_type = NOBUS;
405

cem's avatar
cem committed
406
	/* Second, we run through adding I/O APIC's and buses. */
407
	mptable_parse_apics_and_busses();	
408

409
410
	/* Third, we run through the table tweaking interrupt sources. */
	mptable_parse_ints();
411

412
	/* Fourth, we register all the I/O APIC's. */
royger's avatar
royger committed
413
	for (i = 0; i <= IOAPIC_MAX_ID; i++)
414
415
		if (ioapics[i] != NULL)
			ioapic_register(ioapics[i]);
416

417
418
	/* Fifth, we setup data structures to handle PCI interrupt routing. */
	mptable_pci_setup();
419

420
421
	/* Finally, we throw the switch to enable the I/O APIC's. */
	if (mpfps->mpfb2 & MPFB2_IMCR_PRESENT) {
422
423
		outb(0x22, 0x70);	/* select IMCR */
		byte = inb(0x23);	/* current contents */
fsmp's avatar
fsmp committed
424
		byte |= 0x01;		/* mask external INTR */
425
426
		outb(0x23, byte);	/* disconnect 8259s/NMI */
	}
fsmp's avatar
fsmp committed
427

428
	return (0);
429
430
}

431
432
433
static void
mptable_register(void *dummy __unused)
{
434

435
436
	apic_register_enumerator(&mptable_enumerator);
}
437
SYSINIT(mptable_register, SI_SUB_TUNABLES - 1, SI_ORDER_FIRST, mptable_register,
438
    NULL);
439
440

/*
441
 * Call the handler routine for each entry in the MP config base table.
442
443
 */
static void
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
mptable_walk_table(mptable_entry_handler *handler, void *arg)
{
	u_int i;
	u_char *entry;

	entry = (u_char *)(mpct + 1);
	for (i = 0; i < mpct->entry_count; i++) {
		switch (*entry) {
		case MPCT_ENTRY_PROCESSOR:
		case MPCT_ENTRY_IOAPIC:
		case MPCT_ENTRY_BUS:
		case MPCT_ENTRY_INT:
		case MPCT_ENTRY_LOCAL_INT:
			break;
		default:
			panic("%s: Unknown MP Config Entry %d\n", __func__,
			    (int)*entry);
		}
		handler(entry, arg);
		entry += basetable_entry_types[*entry].length;
	}
}
466

jhb's avatar
jhb committed
467
#ifdef NEW_PCIB
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
 * Call the handler routine for each entry in the MP config extended
 * table.
 */
static void
mptable_walk_extended_table(mptable_extended_entry_handler *handler, void *arg)
{
	ext_entry_ptr end, entry;

	if (mpet == NULL)
		return;
	entry = mpet;
	end = (ext_entry_ptr)((char *)mpet + mpct->extended_table_length);
	while (entry < end) {
		handler(entry, arg);
		entry = (ext_entry_ptr)((char *)entry + entry->length);
	}
}
jhb's avatar
jhb committed
486
#endif
487

488
489
static void
mptable_probe_cpus_handler(u_char *entry, void *arg)
490
491
492
493
494
495
496
497
{
	proc_entry_ptr proc;

	switch (*entry) {
	case MPCT_ENTRY_PROCESSOR:
		proc = (proc_entry_ptr)entry;
		if (proc->cpu_flags & PROCENTRY_FLAG_EN &&
		    proc->apic_id < MAX_LAPIC_ID && mp_ncpus < MAXCPU) {
royger's avatar
royger committed
498
#ifdef SMP
499
500
			mp_ncpus++;
			mp_maxid = mp_ncpus - 1;
royger's avatar
royger committed
501
#endif
502
503
504
505
506
507
508
509
510
			max_apic_id = max(max_apic_id, proc->apic_id);
		}
		break;
	}
}


static void
mptable_setup_cpus_handler(u_char *entry, void *arg)
511
512
513
514
515
516
517
518
519
520
{
	proc_entry_ptr proc;
	u_int *cpu_mask;

	switch (*entry) {
	case MPCT_ENTRY_PROCESSOR:
		proc = (proc_entry_ptr)entry;
		if (proc->cpu_flags & PROCENTRY_FLAG_EN) {
			lapic_create(proc->apic_id, proc->cpu_flags &
			    PROCENTRY_FLAG_BP);
521
522
523
524
			if (proc->apic_id < MAX_LAPIC_ID) {
				cpu_mask = (u_int *)arg;
				*cpu_mask |= (1ul << proc->apic_id);
			}
525
526
527
528
		}
		break;
	}
}
529

530
531
532
533
534
static void
mptable_count_items_handler(u_char *entry, void *arg __unused)
{
	io_apic_entry_ptr apic;
	bus_entry_ptr bus;
535

536
537
538
539
540
541
542
543
544
545
546
547
548
549
	switch (*entry) {
	case MPCT_ENTRY_BUS:
		bus = (bus_entry_ptr)entry;
		mptable_nbusses++;
		if (bus->bus_id > mptable_maxbusid)
			mptable_maxbusid = bus->bus_id;
		break;
	case MPCT_ENTRY_IOAPIC:
		apic = (io_apic_entry_ptr)entry;
		if (apic->apic_flags & IOAPICENTRY_FLAG_EN)
			mptable_nioapics++;
		break;
	}
}
550

551
552
553
554
555
556
/*
 * Count items in the table.
 */
static void
mptable_count_items(void)
{
557

558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
	/* Is this a pre-defined config? */
	if (mpfps->config_type != 0) {
		mptable_nioapics = 1;
		switch (mpfps->config_type) {
		case 1:
		case 2:
		case 3:
		case 4:
			mptable_nbusses = 1;
			break;
		case 5:
		case 6:
		case 7:
			mptable_nbusses = 2;
			break;
		default:
			panic("Unknown pre-defined MP Table config type %d",
			    mpfps->config_type);
		}
		mptable_maxbusid = mptable_nbusses - 1;
	} else
		mptable_walk_table(mptable_count_items_handler, NULL);
}
581

582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*
 * Add a bus or I/O APIC from an entry in the table.
 */
static void
mptable_parse_apics_and_busses_handler(u_char *entry, void *arg __unused)
{
	io_apic_entry_ptr apic;
	bus_entry_ptr bus;
	enum busTypes bus_type;
	int i;


	switch (*entry) {
	case MPCT_ENTRY_BUS:
		bus = (bus_entry_ptr)entry;
		bus_type = lookup_bus_type(bus->bus_type);
		if (bus_type == UNKNOWN_BUSTYPE) {
			printf("MPTable: Unknown bus %d type \"", bus->bus_id);
			for (i = 0; i < 6; i++)
				printf("%c", bus->bus_type[i]);
			printf("\"\n");
		}
		busses[bus->bus_id].bus_id = bus->bus_id;
		busses[bus->bus_id].bus_type = bus_type;
		break;
	case MPCT_ENTRY_IOAPIC:
		apic = (io_apic_entry_ptr)entry;
		if (!(apic->apic_flags & IOAPICENTRY_FLAG_EN))
			break;
royger's avatar
royger committed
611
		if (apic->apic_id > IOAPIC_MAX_ID)
612
613
614
615
616
			panic("%s: I/O APIC ID %d too high", __func__,
			    apic->apic_id);
		if (ioapics[apic->apic_id] != NULL)
			panic("%s: Double APIC ID %d", __func__,
			    apic->apic_id);
617
618
		ioapics[apic->apic_id] = ioapic_create(apic->apic_address,
		    apic->apic_id, -1);
619
620
621
		break;
	default:
		break;
622
	}
623
}
624

625
/*
cem's avatar
cem committed
626
 * Enumerate I/O APIC's and buses.
627
628
629
630
631
632
633
 */
static void
mptable_parse_apics_and_busses(void)
{

	/* Is this a pre-defined config? */
	if (mpfps->config_type != 0) {
634
		ioapics[2] = ioapic_create(DEFAULT_IO_APIC_BASE, 2, 0);
635
		busses[0].bus_id = 0;
636
		busses[0].bus_type = default_data[mpfps->config_type - 1][2];
637
638
639
		if (mptable_nbusses > 1) {
			busses[1].bus_id = 1;
			busses[1].bus_type =
640
			    default_data[mpfps->config_type - 1][4];
641
642
643
644
645
		}
	} else
		mptable_walk_table(mptable_parse_apics_and_busses_handler,
		    NULL);
}
646

647
648
649
/*
 * Determine conforming polarity for a given bus type.
 */
650
651
static enum intr_polarity
conforming_polarity(u_char src_bus, u_char src_bus_irq)
652
{
653

654
655
656
	KASSERT(src_bus <= mptable_maxbusid, ("bus id %d too large", src_bus));
	switch (busses[src_bus].bus_type) {
	case ISA:
657
	case EISA:
658
		return (INTR_POLARITY_HIGH);
659
	case PCI:
660
		return (INTR_POLARITY_LOW);
661
662
663
664
665
	default:
		panic("%s: unknown bus type %d", __func__,
		    busses[src_bus].bus_type);
	}
}
666

667
668
669
/*
 * Determine conforming trigger for a given bus type.
 */
670
static enum intr_trigger
671
672
conforming_trigger(u_char src_bus, u_char src_bus_irq)
{
673

674
675
676
	KASSERT(src_bus <= mptable_maxbusid, ("bus id %d too large", src_bus));
	switch (busses[src_bus].bus_type) {
	case ISA:
677
678
679
680
		if (elcr_found)
			return (elcr_read_trigger(src_bus_irq));
		else
			return (INTR_TRIGGER_EDGE);
681
	case PCI:
682
		return (INTR_TRIGGER_LEVEL);
nyan's avatar
nyan committed
683

684
685
	case EISA:
		KASSERT(src_bus_irq < 16, ("Invalid EISA IRQ %d", src_bus_irq));
686
		KASSERT(elcr_found, ("Missing ELCR"));
687
		return (elcr_read_trigger(src_bus_irq));
nyan's avatar
nyan committed
688

689
690
691
692
693
	default:
		panic("%s: unknown bus type %d", __func__,
		    busses[src_bus].bus_type);
	}
}
694

695
static enum intr_polarity
696
697
intentry_polarity(int_entry_ptr intr)
{
fsmp's avatar
fsmp committed
698

699
700
	switch (intr->int_flags & INTENTRY_FLAGS_POLARITY) {
	case INTENTRY_FLAGS_POLARITY_CONFORM:
701
702
		return (conforming_polarity(intr->src_bus_id,
			    intr->src_bus_irq));
703
	case INTENTRY_FLAGS_POLARITY_ACTIVEHI:
704
		return (INTR_POLARITY_HIGH);
705
	case INTENTRY_FLAGS_POLARITY_ACTIVELO:
706
		return (INTR_POLARITY_LOW);
707
708
709
	default:
		panic("Bogus interrupt flags");
	}
710
711
}

712
static enum intr_trigger
713
714
715
716
717
718
719
720
intentry_trigger(int_entry_ptr intr)
{

	switch (intr->int_flags & INTENTRY_FLAGS_TRIGGER) {
	case INTENTRY_FLAGS_TRIGGER_CONFORM:
		return (conforming_trigger(intr->src_bus_id,
			    intr->src_bus_irq));
	case INTENTRY_FLAGS_TRIGGER_EDGE:
721
		return (INTR_TRIGGER_EDGE);
722
	case INTENTRY_FLAGS_TRIGGER_LEVEL:
723
		return (INTR_TRIGGER_LEVEL);
724
725
726
727
	default:
		panic("Bogus interrupt flags");
	}
}
728
729

/*
730
 * Parse an interrupt entry for an I/O interrupt routed to a pin on an I/O APIC.
731
 */
732
733
static void
mptable_parse_io_int(int_entry_ptr intr)
734
{
735
	void *ioapic;
736
	u_int pin, apic_id;
737

738
	apic_id = intr->dst_apic_id;
739
	if (intr->dst_apic_id == 0xff) {
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
		/*
		 * An APIC ID of 0xff means that the interrupt is connected
		 * to the specified pin on all I/O APICs in the system.  If
		 * there is only one I/O APIC, then use that APIC to route
		 * the interrupts.  If there is more than one I/O APIC, then
		 * punt.
		 */
		if (mptable_nioapics == 1) {
			apic_id = 0;
			while (ioapics[apic_id] == NULL)
				apic_id++;
		} else {
			printf(
			"MPTable: Ignoring global interrupt entry for pin %d\n",
			    intr->dst_apic_int);
			return;
		}
757
	}
royger's avatar
royger committed
758
	if (apic_id > IOAPIC_MAX_ID) {
759
760
761
762
		printf("MPTable: Ignoring interrupt entry for ioapic%d\n",
		    intr->dst_apic_id);
		return;
	}
763
	ioapic = ioapics[apic_id];
764
765
766
	if (ioapic == NULL) {
		printf(
	"MPTable: Ignoring interrupt entry for missing ioapic%d\n",
767
		    apic_id);
768
769
770
771
772
		return;
	}
	pin = intr->dst_apic_int;
	switch (intr->int_type) {
	case INTENTRY_TYPE_INT:
773
774
		switch (busses[intr->src_bus_id].bus_type) {
		case NOBUS:
775
			panic("interrupt from missing bus");
776
777
778
779
780
781
782
783
		case ISA:
		case EISA:
			if (busses[intr->src_bus_id].bus_type == ISA)
				ioapic_set_bus(ioapic, pin, APIC_BUS_ISA);
			else
				ioapic_set_bus(ioapic, pin, APIC_BUS_EISA);
			if (intr->src_bus_irq == pin)
				break;
784
			ioapic_remap_vector(ioapic, pin, intr->src_bus_irq);
785
786
787
			if (ioapic_get_vector(ioapic, intr->src_bus_irq) ==
			    intr->src_bus_irq)
				ioapic_disable_pin(ioapic, intr->src_bus_irq);
788
789
790
791
792
793
794
			break;
		case PCI:
			ioapic_set_bus(ioapic, pin, APIC_BUS_PCI);
			break;
		default:
			ioapic_set_bus(ioapic, pin, APIC_BUS_UNKNOWN);
			break;
795
		}
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
		break;
	case INTENTRY_TYPE_NMI:
		ioapic_set_nmi(ioapic, pin);
		break;
	case INTENTRY_TYPE_SMI:
		ioapic_set_smi(ioapic, pin);
		break;
	case INTENTRY_TYPE_EXTINT:
		ioapic_set_extint(ioapic, pin);
		break;
	default:
		panic("%s: invalid interrupt entry type %d\n", __func__,
		    intr->int_type);
	}
	if (intr->int_type == INTENTRY_TYPE_INT ||
	    (intr->int_flags & INTENTRY_FLAGS_TRIGGER) !=
	    INTENTRY_FLAGS_TRIGGER_CONFORM)
		ioapic_set_triggermode(ioapic, pin, intentry_trigger(intr));
	if (intr->int_type == INTENTRY_TYPE_INT ||
	    (intr->int_flags & INTENTRY_FLAGS_POLARITY) !=
	    INTENTRY_FLAGS_POLARITY_CONFORM)
		ioapic_set_polarity(ioapic, pin, intentry_polarity(intr));
818
819
}

820
821
822
823
824
/*
 * Parse an interrupt entry for a local APIC LVT pin.
 */
static void
mptable_parse_local_int(int_entry_ptr intr)
825
{
826
	u_int apic_id, pin;
827

828
829
830
831
832
	if (intr->dst_apic_id == 0xff)
		apic_id = APIC_ID_ALL;
	else
		apic_id = intr->dst_apic_id;
	if (intr->dst_apic_int == 0)
833
		pin = APIC_LVT_LINT0;
834
	else
835
		pin = APIC_LVT_LINT1;
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
	switch (intr->int_type) {
	case INTENTRY_TYPE_INT:
#if 1
		printf(
	"MPTable: Ignoring vectored local interrupt for LINTIN%d vector %d\n",
		    intr->dst_apic_int, intr->src_bus_irq);
		return;
#else
		lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_FIXED);
		break;
#endif
	case INTENTRY_TYPE_NMI:
		lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_NMI);
		break;
	case INTENTRY_TYPE_SMI:
		lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_SMI);
		break;
	case INTENTRY_TYPE_EXTINT:
		lapic_set_lvt_mode(apic_id, pin, APIC_LVT_DM_EXTINT);
		break;
	default:
		panic("%s: invalid interrupt entry type %d\n", __func__,
		    intr->int_type);
	}
	if ((intr->int_flags & INTENTRY_FLAGS_TRIGGER) !=
	    INTENTRY_FLAGS_TRIGGER_CONFORM)
		lapic_set_lvt_triggermode(apic_id, pin,
		    intentry_trigger(intr));
	if ((intr->int_flags & INTENTRY_FLAGS_POLARITY) !=
	    INTENTRY_FLAGS_POLARITY_CONFORM)
		lapic_set_lvt_polarity(apic_id, pin, intentry_polarity(intr));
}
868
869

/*
870
 * Parse interrupt entries.
871
 */
872
static void
873
mptable_parse_ints_handler(u_char *entry, void *arg __unused)
874
{
875
	int_entry_ptr intr;
876

877
878
879
880
881
882
883
884
	intr = (int_entry_ptr)entry;
	switch (*entry) {
	case MPCT_ENTRY_INT:
		mptable_parse_io_int(intr);
		break;
	case MPCT_ENTRY_LOCAL_INT:
		mptable_parse_local_int(intr);
		break;
885
886
	}
}
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938

/*
 * Configure interrupt pins for a default configuration.  For details see
 * Table 5-2 in Section 5 of the MP Table specification.
 */
static void
mptable_parse_default_config_ints(void)
{
	struct INTENTRY entry;
	int pin;

	/*
	 * All default configs route IRQs from bus 0 to the first 16 pins
	 * of the first I/O APIC with an APIC ID of 2.
	 */
	entry.type = MPCT_ENTRY_INT;
	entry.int_flags = INTENTRY_FLAGS_POLARITY_CONFORM |
	    INTENTRY_FLAGS_TRIGGER_CONFORM;
	entry.src_bus_id = 0;
	entry.dst_apic_id = 2;

	/* Run through all 16 pins. */
	for (pin = 0; pin < 16; pin++) {
		entry.dst_apic_int = pin;
		switch (pin) {
		case 0:
			/* Pin 0 is an ExtINT pin. */
			entry.int_type = INTENTRY_TYPE_EXTINT;
			break;
		case 2:
			/* IRQ 0 is routed to pin 2. */
			entry.int_type = INTENTRY_TYPE_INT;
			entry.src_bus_irq = 0;
			break;
		default:
			/* All other pins are identity mapped. */
			entry.int_type = INTENTRY_TYPE_INT;
			entry.src_bus_irq = pin;
			break;
		}
		mptable_parse_io_int(&entry);
	}

	/* Certain configs disable certain pins. */
	if (mpfps->config_type == 7)
		ioapic_disable_pin(ioapics[2], 0);
	if (mpfps->config_type == 2) {
		ioapic_disable_pin(ioapics[2], 2);
		ioapic_disable_pin(ioapics[2], 13);
	}
}

939
/*
940
 * Configure the interrupt pins
941
 */
942
943
static void
mptable_parse_ints(void)
944
{
945

946
947
948
	/* Is this a pre-defined config? */
	if (mpfps->config_type != 0) {
		/* Configure LINT pins. */
949
950
951
		lapic_set_lvt_mode(APIC_ID_ALL, APIC_LVT_LINT0,
		    APIC_LVT_DM_EXTINT);
		lapic_set_lvt_mode(APIC_ID_ALL, APIC_LVT_LINT1, APIC_LVT_DM_NMI);
952

953
		/* Configure I/O APIC pins. */
954
		mptable_parse_default_config_ints();
955
956
	} else
		mptable_walk_table(mptable_parse_ints_handler, NULL);
957
958
}

959
#ifdef MPTABLE_FORCE_HTT
960
/*
961
962
 * Perform a hyperthreading "fix-up" to enumerate any logical CPU's
 * that aren't already listed in the table.
963
964
965
966
967
968
969
970
971
972
973
 *
 * XXX: We assume that all of the physical CPUs in the
 * system have the same number of logical CPUs.
 *
 * XXX: We assume that APIC ID's are allocated such that
 * the APIC ID's for a physical processor are aligned
 * with the number of logical CPU's in the processor.
 */
static void
mptable_hyperthread_fixup(u_int id_mask)
{
974
	u_int i, id, logical_cpus;
975
976
977
978

	/* Nothing to do if there is no HTT support. */
	if ((cpu_feature & CPUID_HTT) == 0)
		return;
979
	logical_cpus = (cpu_procinfo & CPUID_HTT_CORES) >> 16;
980
981
982
983
984
985
986
987
988
	if (logical_cpus <= 1)
		return;

	/*
	 * For each APIC ID of a CPU that is set in the mask,
	 * scan the other candidate APIC ID's for this
	 * physical processor.  If any of those ID's are
	 * already in the table, then kill the fixup.
	 */
989
	for (id = 0; id <= MAX_LAPIC_ID; id++) {
990
991
992
993
994
995
996
997
998
999
1000
		if ((id_mask & 1 << id) == 0)
			continue;
		/* First, make sure we are on a logical_cpus boundary. */
		if (id % logical_cpus != 0)
			return;
		for (i = id + 1; i < id + logical_cpus; i++)
			if ((id_mask & 1 << i) != 0)
				return;
	}

	/*
For faster browsing, not all history is shown. View entire blame