Subversion Repositories HelenOS

Rev

Rev 2441 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 jermar 1
/*
2071 jermar 2
 * Copyright (c) 2001-2005 Jakub Jermar
1 jermar 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
 *
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.
16
 *
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.
27
 */
28
 
1888 jermar 29
/** @addtogroup ia32   
1702 cejka 30
 * @{
31
 */
32
/** @file
33
 */
34
 
458 decky 35
#ifdef CONFIG_SMP
1 jermar 36
 
37
#include <config.h>
38
#include <print.h>
128 jermar 39
#include <debug.h>
34 jermar 40
#include <arch/smp/mps.h>
11 jermar 41
#include <arch/smp/apic.h>
128 jermar 42
#include <arch/smp/smp.h>
1 jermar 43
#include <func.h>
44
#include <arch/types.h>
45
#include <cpu.h>
46
#include <arch/asm.h>
30 jermar 47
#include <arch/bios/bios.h>
195 vana 48
#include <mm/frame.h>
1 jermar 49
 
50
/*
34 jermar 51
 * MultiProcessor Specification detection code.
1 jermar 52
 */
53
 
54
#define FS_SIGNATURE    0x5f504d5f
55
#define CT_SIGNATURE    0x504d4350
56
 
2441 decky 57
static int mps_fs_check(uint8_t *base);
58
static int mps_ct_check(void);
1 jermar 59
 
2441 decky 60
static int configure_via_ct(void);
61
static int configure_via_default(uint8_t n);
1 jermar 62
 
2441 decky 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);
1 jermar 68
 
2441 decky 69
static void ct_extended_entries(void);
1 jermar 70
 
34 jermar 71
static struct mps_fs *fs;
72
static struct mps_ct *ct;
1 jermar 73
 
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;
79
 
2095 decky 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;
1 jermar 85
 
86
/*
128 jermar 87
 * Implementation of IA-32 SMP configuration interface.
88
 */
89
static count_t get_cpu_count(void);
90
static bool is_cpu_enabled(index_t i);
91
static bool is_bsp(index_t i);
1780 jermar 92
static uint8_t get_cpu_apic_id(index_t i);
2101 decky 93
static int mps_irq_to_pin(unsigned int irq);
128 jermar 94
 
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,
512 jermar 99
    .cpu_apic_id = get_cpu_apic_id,
100
    .irq_to_pin = mps_irq_to_pin
128 jermar 101
};
102
 
103
count_t get_cpu_count(void)
104
{
105
    return processor_entry_cnt;
106
}
107
 
108
bool is_cpu_enabled(index_t i)
109
{
110
    ASSERT(i < processor_entry_cnt);
2441 decky 111
    return (bool) ((processor_entries[i].cpu_flags & 0x01) == 0x01);
128 jermar 112
}
113
 
114
bool is_bsp(index_t i)
115
{
116
    ASSERT(i < processor_entry_cnt);
2441 decky 117
    return (bool) ((processor_entries[i].cpu_flags & 0x02) == 0x02);
128 jermar 118
}
119
 
1780 jermar 120
uint8_t get_cpu_apic_id(index_t i)
128 jermar 121
{
122
    ASSERT(i < processor_entry_cnt);
123
    return processor_entries[i].l_apic_id;
124
}
125
 
126
 
127
/*
1 jermar 128
 * Used to check the integrity of the MP Floating Structure.
129
 */
1780 jermar 130
int mps_fs_check(uint8_t *base)
1 jermar 131
{
2095 decky 132
    unsigned int i;
1780 jermar 133
    uint8_t sum;
1 jermar 134
 
135
    for (i = 0, sum = 0; i < 16; i++)
2441 decky 136
        sum = (uint8_t) (sum + base[i]);
1 jermar 137
 
138
    return !sum;
139
}
140
 
141
/*
142
 * Used to check the integrity of the MP Configuration Table.
143
 */
34 jermar 144
int mps_ct_check(void)
1 jermar 145
{
1780 jermar 146
    uint8_t *base = (uint8_t *) ct;
147
    uint8_t *ext = base + ct->base_table_length;
148
    uint8_t sum;
1 jermar 149
    int i; 
150
 
151
    /* count the checksum for the base table */
152
    for (i=0,sum=0; i < ct->base_table_length; i++)
2441 decky 153
        sum = (uint8_t) (sum + base[i]);
1 jermar 154
 
155
    if (sum)
156
        return 0;
157
 
158
    /* count the checksum for the extended table */
159
    for (i=0,sum=0; i < ct->ext_table_length; i++)
2441 decky 160
        sum = (uint8_t) (sum + ext[i]);
1 jermar 161
 
12 jermar 162
    return sum == ct->ext_table_checksum;
1 jermar 163
}
164
 
34 jermar 165
void mps_init(void)
1 jermar 166
{
1780 jermar 167
    uint8_t *addr[2] = { NULL, (uint8_t *) PA2KA(0xf0000) };
2095 decky 168
    unsigned int i, j, length[2] = { 1024, 64 * 1024 };
1 jermar 169
 
170
 
171
    /*
32 jermar 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
1 jermar 176
     */
32 jermar 177
 
1780 jermar 178
    addr[0] = (uint8_t *) PA2KA(ebda ? ebda : 639 * 1024);
32 jermar 179
    for (i = 0; i < 2; i++) {
180
        for (j = 0; j < length[i]; j += 16) {
1780 jermar 181
            if (*((uint32_t *) &addr[i][j]) == FS_SIGNATURE && mps_fs_check(&addr[i][j])) {
34 jermar 182
                fs = (struct mps_fs *) &addr[i][j];
1 jermar 183
                goto fs_found;
32 jermar 184
            }
1 jermar 185
        }
186
    }
187
 
188
    return;
189
 
190
fs_found:
1221 decky 191
    printf("%p: MPS Floating Pointer Structure\n", fs);
1 jermar 192
 
193
    if (fs->config_type == 0 && fs->configuration_table) {
194
        if (fs->mpfib2 >> 7) {
2462 jermar 195
            printf("%s: PIC mode not supported\n", __func__);
1 jermar 196
            return;
197
        }
198
 
1780 jermar 199
        ct = (struct mps_ct *)PA2KA((uintptr_t)fs->configuration_table);
1 jermar 200
        config.cpu_count = configure_via_ct();
201
    }
202
    else
203
        config.cpu_count = configure_via_default(fs->config_type);
204
 
205
    return;
206
}
207
 
208
int configure_via_ct(void)
209
{
1780 jermar 210
    uint8_t *cur;
2095 decky 211
    unsigned int i, cnt;
1 jermar 212
 
213
    if (ct->signature != CT_SIGNATURE) {
2462 jermar 214
        printf("%s: bad ct->signature\n", __func__);
1 jermar 215
        return 1;
216
    }
34 jermar 217
    if (!mps_ct_check()) {
2462 jermar 218
        printf("%s: bad ct checksum\n", __func__);
1 jermar 219
        return 1;
220
    }
221
    if (ct->oem_table) {
2462 jermar 222
        printf("%s: ct->oem_table not supported\n", __func__);
1 jermar 223
        return 1;
224
    }
225
 
1780 jermar 226
    l_apic = (uint32_t *)(uintptr_t)ct->l_apic;
1 jermar 227
 
228
    cnt = 0;
229
    cur = &ct->base_table[0];
230
    for (i=0; i < ct->entry_count; i++) {
231
        switch (*cur) {
232
            /* Processor entry */
233
            case 0:
234
                processor_entries = processor_entries ? processor_entries : (struct __processor_entry *) cur;
235
                processor_entry_cnt++;
236
                cnt += ct_processor_entry((struct __processor_entry *) cur);
237
                cur += 20;
238
                break;
239
 
240
            /* Bus entry */
241
            case 1:
242
                bus_entries = bus_entries ? bus_entries : (struct __bus_entry *) cur;
243
                bus_entry_cnt++;
244
                ct_bus_entry((struct __bus_entry *) cur);
245
                cur += 8;
246
                break;
247
 
248
            /* I/O Apic */
249
            case 2:
250
                io_apic_entries = io_apic_entries ? io_apic_entries : (struct __io_apic_entry *) cur;
251
                io_apic_entry_cnt++;
252
                ct_io_apic_entry((struct __io_apic_entry *) cur);
253
                cur += 8;
254
                break;
255
 
256
            /* I/O Interrupt Assignment */
257
            case 3:
258
                io_intr_entries = io_intr_entries ? io_intr_entries : (struct __io_intr_entry *) cur;
259
                io_intr_entry_cnt++;
260
                ct_io_intr_entry((struct __io_intr_entry *) cur);
261
                cur += 8;
262
                break;
263
 
264
            /* Local Interrupt Assignment */
265
            case 4:
266
                l_intr_entries = l_intr_entries ? l_intr_entries : (struct __l_intr_entry *) cur;
267
                l_intr_entry_cnt++;
268
                ct_l_intr_entry((struct __l_intr_entry *) cur);
125 jermar 269
                cur += 8;
1 jermar 270
                break;
125 jermar 271
 
1 jermar 272
            default:
273
                /*
274
                 * Something is wrong. Fallback to UP mode.
275
                 */
276
 
2462 jermar 277
                printf("%s: ct badness\n", __func__);
1 jermar 278
                return 1;
279
        }
280
    }
281
 
282
    /*
283
     * Process extended entries.
284
     */
285
    ct_extended_entries();
286
    return cnt;
287
}
288
 
2441 decky 289
int configure_via_default(uint8_t n __attribute__((unused)))
1 jermar 290
{
291
    /*
292
     * Not yet implemented.
293
     */
2462 jermar 294
    printf("%s: not supported\n", __func__);
1 jermar 295
    return 1;
296
}
297
 
298
 
2441 decky 299
int ct_processor_entry(struct __processor_entry *pr __attribute__((unused)))
1 jermar 300
{
301
    /*
302
     * Ignore processors which are not marked enabled.
303
     */
304
    if ((pr->cpu_flags & (1<<0)) == 0)
305
        return 0;
306
 
307
    apic_id_mask |= (1<<pr->l_apic_id);
308
    return 1;
309
}
310
 
2441 decky 311
void ct_bus_entry(struct __bus_entry *bus __attribute__((unused)))
1 jermar 312
{
34 jermar 313
#ifdef MPSCT_VERBOSE
1 jermar 314
    char buf[7];
205 jermar 315
    memcpy((void *) buf, (void *) bus->bus_type, 6);
1 jermar 316
    buf[6] = 0;
317
    printf("bus%d: %s\n", bus->bus_id, buf);
318
#endif
319
}
320
 
321
void ct_io_apic_entry(struct __io_apic_entry *ioa)
322
{
2095 decky 323
    static unsigned int io_apic_count = 0;
1 jermar 324
 
325
    /* this ioapic is marked unusable */
615 palkovsky 326
    if ((ioa->io_apic_flags & 1) == 0)
1 jermar 327
        return;
328
 
329
    if (io_apic_count++ > 0) {
330
        /*
331
         * Multiple IO APIC's are currently not supported.
332
         */
333
        return;
334
    }
335
 
1780 jermar 336
    io_apic = (uint32_t *)(uintptr_t)ioa->io_apic;
1 jermar 337
}
338
 
34 jermar 339
//#define MPSCT_VERBOSE
2441 decky 340
void ct_io_intr_entry(struct __io_intr_entry *iointr __attribute__((unused)))
1 jermar 341
{
34 jermar 342
#ifdef MPSCT_VERBOSE
1 jermar 343
    switch (iointr->intr_type) {
344
        case 0: printf("INT"); break;
345
        case 1: printf("NMI"); break;
346
        case 2: printf("SMI"); break;
347
        case 3: printf("ExtINT"); break;
348
    }
349
    putchar(',');
350
    switch (iointr->poel&3) {
351
        case 0: printf("bus-like"); break;
352
        case 1: printf("active high"); break;
353
        case 2: printf("reserved"); break;
354
        case 3: printf("active low"); break;
355
    }
356
    putchar(',');
357
    switch ((iointr->poel>>2)&3) {
358
        case 0: printf("bus-like"); break;
359
        case 1: printf("edge-triggered"); break;
360
        case 2: printf("reserved"); break;
361
        case 3: printf("level-triggered"); break;
362
    }
363
    putchar(',');
364
    printf("bus%d,irq%d", iointr->src_bus_id, iointr->src_bus_irq);
365
    putchar(',');
366
    printf("io_apic%d,pin%d", iointr->dst_io_apic_id, iointr->dst_io_apic_pin);
367
    putchar('\n'); 
368
#endif
369
}
370
 
2441 decky 371
void ct_l_intr_entry(struct __l_intr_entry *lintr __attribute__((unused)))
1 jermar 372
{
34 jermar 373
#ifdef MPSCT_VERBOSE
1 jermar 374
    switch (lintr->intr_type) {
375
        case 0: printf("INT"); break;
376
        case 1: printf("NMI"); break;
377
        case 2: printf("SMI"); break;
378
        case 3: printf("ExtINT"); break;
379
    }
380
    putchar(',');
381
    switch (lintr->poel&3) {
382
        case 0: printf("bus-like"); break;
383
        case 1: printf("active high"); break;
384
        case 2: printf("reserved"); break;
385
        case 3: printf("active low"); break;
386
    }
387
    putchar(',');
388
    switch ((lintr->poel>>2)&3) {
389
        case 0: printf("bus-like"); break;
390
        case 1: printf("edge-triggered"); break;
391
        case 2: printf("reserved"); break;
392
        case 3: printf("level-triggered"); break;
393
    }
394
    putchar(',');
395
    printf("bus%d,irq%d", lintr->src_bus_id, lintr->src_bus_irq);
396
    putchar(',');
397
    printf("l_apic%d,pin%d", lintr->dst_l_apic_id, lintr->dst_l_apic_pin);
398
    putchar('\n');
399
#endif
400
}
401
 
402
void ct_extended_entries(void)
403
{
1780 jermar 404
    uint8_t *ext = (uint8_t *) ct + ct->base_table_length;
405
    uint8_t *cur;
13 jermar 406
 
407
    for (cur = ext; cur < ext + ct->ext_table_length; cur += cur[CT_EXT_ENTRY_LEN]) {
408
        switch (cur[CT_EXT_ENTRY_TYPE]) {
409
            default:
1221 decky 410
                printf("%p: skipping MP Configuration Table extended entry type %d\n", cur, cur[CT_EXT_ENTRY_TYPE]);
13 jermar 411
                break;
412
        }
413
    }
1 jermar 414
}
415
 
2101 decky 416
int mps_irq_to_pin(unsigned int irq)
1 jermar 417
{
2095 decky 418
    unsigned int i;
1 jermar 419
 
2101 decky 420
    for (i = 0; i < io_intr_entry_cnt; i++) {
1 jermar 421
        if (io_intr_entries[i].src_bus_irq == irq && io_intr_entries[i].intr_type == 0)
422
            return io_intr_entries[i].dst_io_apic_pin;
423
    }
424
 
425
    return -1;
426
}
427
 
458 decky 428
#endif /* CONFIG_SMP */
1702 cejka 429
 
1888 jermar 430
/** @}
1702 cejka 431
 */