2
* Copyright (c) 2008 Jakub Jermar
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
9
* - Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* - Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
* - The name of the author may not be used to endorse or promote products
15
* derived from this software without specific prior written permission.
17
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40
#include <arch/smp/mps.h>
41
#include <arch/smp/apic.h>
42
#include <arch/smp/smp.h>
44
#include <arch/types.h>
47
#include <arch/bios/bios.h>
51
* MultiProcessor Specification detection code.
54
#define FS_SIGNATURE 0x5f504d5f
55
#define CT_SIGNATURE 0x504d4350
57
static int mps_fs_check(uint8_t *base);
58
static int mps_ct_check(void);
60
static int configure_via_ct(void);
61
static int configure_via_default(uint8_t n);
63
static int ct_processor_entry(struct __processor_entry *pr);
64
static void ct_bus_entry(struct __bus_entry *bus);
65
static void ct_io_apic_entry(struct __io_apic_entry *ioa);
66
static void ct_io_intr_entry(struct __io_intr_entry *iointr);
67
static void ct_l_intr_entry(struct __l_intr_entry *lintr);
69
static void ct_extended_entries(void);
71
static struct mps_fs *fs;
72
static struct mps_ct *ct;
74
struct __processor_entry *processor_entries = NULL;
75
struct __bus_entry *bus_entries = NULL;
76
struct __io_apic_entry *io_apic_entries = NULL;
77
struct __io_intr_entry *io_intr_entries = NULL;
78
struct __l_intr_entry *l_intr_entries = NULL;
80
unsigned int processor_entry_cnt = 0;
81
unsigned int bus_entry_cnt = 0;
82
unsigned int io_apic_entry_cnt = 0;
83
unsigned int io_intr_entry_cnt = 0;
84
unsigned int l_intr_entry_cnt = 0;
87
* Implementation of IA-32 SMP configuration interface.
89
static size_t get_cpu_count(void);
90
static bool is_cpu_enabled(size_t i);
91
static bool is_bsp(size_t i);
92
static uint8_t get_cpu_apic_id(size_t i);
93
static int mps_irq_to_pin(unsigned int irq);
95
struct smp_config_operations mps_config_operations = {
96
.cpu_count = get_cpu_count,
97
.cpu_enabled = is_cpu_enabled,
98
.cpu_bootstrap = is_bsp,
99
.cpu_apic_id = get_cpu_apic_id,
100
.irq_to_pin = mps_irq_to_pin
103
size_t get_cpu_count(void)
105
return processor_entry_cnt;
108
bool is_cpu_enabled(size_t i)
110
ASSERT(i < processor_entry_cnt);
111
return (bool) ((processor_entries[i].cpu_flags & 0x01) == 0x01);
114
bool is_bsp(size_t i)
116
ASSERT(i < processor_entry_cnt);
117
return (bool) ((processor_entries[i].cpu_flags & 0x02) == 0x02);
120
uint8_t get_cpu_apic_id(size_t i)
122
ASSERT(i < processor_entry_cnt);
123
return processor_entries[i].l_apic_id;
128
* Used to check the integrity of the MP Floating Structure.
130
int mps_fs_check(uint8_t *base)
135
for (i = 0, sum = 0; i < 16; i++)
136
sum = (uint8_t) (sum + base[i]);
142
* Used to check the integrity of the MP Configuration Table.
144
int mps_ct_check(void)
146
uint8_t *base = (uint8_t *) ct;
147
uint8_t *ext = base + ct->base_table_length;
151
/* count the checksum for the base table */
152
for (i = 0,sum = 0; i < ct->base_table_length; i++)
153
sum = (uint8_t) (sum + base[i]);
158
/* count the checksum for the extended table */
159
for (i = 0, sum = 0; i < ct->ext_table_length; i++)
160
sum = (uint8_t) (sum + ext[i]);
162
return sum == ct->ext_table_checksum;
167
uint8_t *addr[2] = { NULL, (uint8_t *) PA2KA(0xf0000) };
168
unsigned int i, j, length[2] = { 1024, 64 * 1024 };
172
* Find MP Floating Pointer Structure
173
* 1a. search first 1K of EBDA
174
* 1b. if EBDA is undefined, search last 1K of base memory
175
* 2. search 64K starting at 0xf0000
178
addr[0] = (uint8_t *) PA2KA(ebda ? ebda : 639 * 1024);
179
for (i = 0; i < 2; i++) {
180
for (j = 0; j < length[i]; j += 16) {
181
if (*((uint32_t *) &addr[i][j]) ==
182
FS_SIGNATURE && mps_fs_check(&addr[i][j])) {
183
fs = (struct mps_fs *) &addr[i][j];
192
printf("%p: MPS Floating Pointer Structure\n", fs);
194
if (fs->config_type == 0 && fs->configuration_table) {
195
if (fs->mpfib2 >> 7) {
196
printf("%s: PIC mode not supported\n", __func__);
200
ct = (struct mps_ct *)PA2KA((uintptr_t)fs->configuration_table);
201
config.cpu_count = configure_via_ct();
204
config.cpu_count = configure_via_default(fs->config_type);
209
int configure_via_ct(void)
214
if (ct->signature != CT_SIGNATURE) {
215
printf("%s: bad ct->signature\n", __func__);
218
if (!mps_ct_check()) {
219
printf("%s: bad ct checksum\n", __func__);
223
printf("%s: ct->oem_table not supported\n", __func__);
227
l_apic = (uint32_t *)(uintptr_t)ct->l_apic;
230
cur = &ct->base_table[0];
231
for (i = 0; i < ct->entry_count; i++) {
233
/* Processor entry */
235
processor_entries = processor_entries ?
237
(struct __processor_entry *) cur;
238
processor_entry_cnt++;
239
cnt += ct_processor_entry((struct __processor_entry *)
246
bus_entries = bus_entries ?
247
bus_entries : (struct __bus_entry *) cur;
249
ct_bus_entry((struct __bus_entry *) cur);
255
io_apic_entries = io_apic_entries ?
256
io_apic_entries : (struct __io_apic_entry *) cur;
258
ct_io_apic_entry((struct __io_apic_entry *) cur);
262
/* I/O Interrupt Assignment */
264
io_intr_entries = io_intr_entries ?
265
io_intr_entries : (struct __io_intr_entry *) cur;
267
ct_io_intr_entry((struct __io_intr_entry *) cur);
271
/* Local Interrupt Assignment */
273
l_intr_entries = l_intr_entries ?
274
l_intr_entries : (struct __l_intr_entry *) cur;
276
ct_l_intr_entry((struct __l_intr_entry *) cur);
282
* Something is wrong. Fallback to UP mode.
285
printf("%s: ct badness\n", __func__);
291
* Process extended entries.
293
ct_extended_entries();
297
int configure_via_default(uint8_t n __attribute__((unused)))
300
* Not yet implemented.
302
printf("%s: not supported\n", __func__);
307
int ct_processor_entry(struct __processor_entry *pr __attribute__((unused)))
310
* Ignore processors which are not marked enabled.
312
if ((pr->cpu_flags & (1 << 0)) == 0)
315
apic_id_mask |= (1 << pr->l_apic_id);
319
void ct_bus_entry(struct __bus_entry *bus __attribute__((unused)))
323
memcpy((void *) buf, (void *) bus->bus_type, 6);
325
printf("bus%d: %s\n", bus->bus_id, buf);
329
void ct_io_apic_entry(struct __io_apic_entry *ioa)
331
static unsigned int io_apic_count = 0;
333
/* this ioapic is marked unusable */
334
if ((ioa->io_apic_flags & 1) == 0)
337
if (io_apic_count++ > 0) {
339
* Multiple IO APIC's are currently not supported.
344
io_apic = (uint32_t *)(uintptr_t)ioa->io_apic;
347
//#define MPSCT_VERBOSE
348
void ct_io_intr_entry(struct __io_intr_entry *iointr __attribute__((unused)))
351
switch (iointr->intr_type) {
366
switch (iointr->poel & 3) {
371
printf("active high");
377
printf("active low");
381
switch ((iointr->poel >> 2) & 3) {
386
printf("edge-triggered");
392
printf("level-triggered");
396
printf("bus%d,irq%d", iointr->src_bus_id, iointr->src_bus_irq);
398
printf("io_apic%d,pin%d", iointr->dst_io_apic_id,
399
iointr->dst_io_apic_pin);
404
void ct_l_intr_entry(struct __l_intr_entry *lintr __attribute__((unused)))
407
switch (lintr->intr_type) {
422
switch (lintr->poel & 3) {
427
printf("active high");
433
printf("active low");
437
switch ((lintr->poel >> 2) & 3) {
442
printf("edge-triggered");
448
printf("level-triggered");
452
printf("bus%d,irq%d", lintr->src_bus_id, lintr->src_bus_irq);
454
printf("l_apic%d,pin%d", lintr->dst_l_apic_id, lintr->dst_l_apic_pin);
459
void ct_extended_entries(void)
461
uint8_t *ext = (uint8_t *) ct + ct->base_table_length;
464
for (cur = ext; cur < ext + ct->ext_table_length;
465
cur += cur[CT_EXT_ENTRY_LEN]) {
466
switch (cur[CT_EXT_ENTRY_TYPE]) {
468
printf("%p: skipping MP Configuration Table extended "
469
"entry type %d\n", cur, cur[CT_EXT_ENTRY_TYPE]);
475
int mps_irq_to_pin(unsigned int irq)
479
for (i = 0; i < io_intr_entry_cnt; i++) {
480
if (io_intr_entries[i].src_bus_irq == irq &&
481
io_intr_entries[i].intr_type == 0)
482
return io_intr_entries[i].dst_io_apic_pin;
488
#endif /* CONFIG_SMP */