Subversion Repositories HelenOS-historic

Rev

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

Rev Author Line No. Line
1 jermar 1
/*
34 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
 
29
#ifdef __SMP__
30
 
31
#include <arch/pm.h>
32
#include <config.h>
33
#include <print.h>
34
#include <panic.h>
34 jermar 35
#include <arch/smp/mps.h>
11 jermar 36
#include <arch/smp/ap.h>
37
#include <arch/smp/apic.h>
1 jermar 38
#include <func.h>
39
#include <arch/types.h>
40
#include <typedefs.h>
41
#include <synch/waitq.h>
42
#include <time/delay.h>
43
#include <mm/heap.h>
44
#include <mm/page.h>
45
#include <mm/frame.h>
46
#include <cpu.h>
47
#include <arch/i8259.h>
48
#include <arch/asm.h>
30 jermar 49
#include <arch/bios/bios.h>
29 jermar 50
#include <arch/acpi/madt.h>
1 jermar 51
 
52
/*
34 jermar 53
 * MultiProcessor Specification detection code.
1 jermar 54
 */
55
 
56
#define FS_SIGNATURE    0x5f504d5f
57
#define CT_SIGNATURE    0x504d4350
58
 
34 jermar 59
int mps_fs_check(__u8 *base);
60
int mps_ct_check(void);
1 jermar 61
 
62
int configure_via_ct(void);
63
int configure_via_default(__u8 n);
64
 
65
int ct_processor_entry(struct __processor_entry *pr);
66
void ct_bus_entry(struct __bus_entry *bus);
67
void ct_io_apic_entry(struct __io_apic_entry *ioa);
68
void ct_io_intr_entry(struct __io_intr_entry *iointr);
69
void ct_l_intr_entry(struct __l_intr_entry *lintr);
70
 
71
void ct_extended_entries(void);
72
 
34 jermar 73
static struct mps_fs *fs;
74
static struct mps_ct *ct;
1 jermar 75
 
76
struct __processor_entry *processor_entries = NULL;
77
struct __bus_entry *bus_entries = NULL;
78
struct __io_apic_entry *io_apic_entries = NULL;
79
struct __io_intr_entry *io_intr_entries = NULL;
80
struct __l_intr_entry *l_intr_entries = NULL;
81
 
82
int processor_entry_cnt = 0;
83
int bus_entry_cnt = 0;
84
int io_apic_entry_cnt = 0;
85
int io_intr_entry_cnt = 0;
86
int l_intr_entry_cnt = 0;
87
 
88
waitq_t ap_completion_wq;
89
waitq_t kmp_completion_wq;
90
 
91
/*
92
 * Used to check the integrity of the MP Floating Structure.
93
 */
34 jermar 94
int mps_fs_check(__u8 *base)
1 jermar 95
{
96
    int i;
97
    __u8 sum;
98
 
99
    for (i = 0, sum = 0; i < 16; i++)
100
        sum += base[i];
101
 
102
    return !sum;
103
}
104
 
105
/*
106
 * Used to check the integrity of the MP Configuration Table.
107
 */
34 jermar 108
int mps_ct_check(void)
1 jermar 109
{
110
    __u8 *base = (__u8 *) ct;
111
    __u8 *ext = base + ct->base_table_length;
112
    __u8 sum;
113
    int i; 
114
 
115
    /* count the checksum for the base table */
116
    for (i=0,sum=0; i < ct->base_table_length; i++)
117
        sum += base[i];
118
 
119
    if (sum)
120
        return 0;
121
 
122
    /* count the checksum for the extended table */
123
    for (i=0,sum=0; i < ct->ext_table_length; i++)
124
        sum += ext[i];
125
 
12 jermar 126
    return sum == ct->ext_table_checksum;
1 jermar 127
}
128
 
34 jermar 129
void mps_init(void)
1 jermar 130
{
32 jermar 131
    __u8 *addr[2] = { NULL, (__u8 *) 0xf0000 };
132
    int i, j, length[2] = { 1024, 64*1024 };
1 jermar 133
 
134
 
135
    /*
32 jermar 136
     * Find MP Floating Pointer Structure
137
     * 1a. search first 1K of EBDA
138
     * 1b. if EBDA is undefined, search last 1K of base memory
139
     *  2. search 64K starting at 0xf0000
1 jermar 140
     */
32 jermar 141
 
142
    addr[0] = (__u8 *) (ebda ? ebda : 639 * 1024);
143
    for (i = 0; i < 2; i++) {
144
        for (j = 0; j < length[i]; j += 16) {
34 jermar 145
            if (*((__u32 *) &addr[i][j]) == FS_SIGNATURE && mps_fs_check(&addr[i][j])) {
146
                fs = (struct mps_fs *) &addr[i][j];
1 jermar 147
                goto fs_found;
32 jermar 148
            }
1 jermar 149
        }
150
    }
151
 
152
    return;
153
 
154
fs_found:
34 jermar 155
    printf("%L: MPS Floating Pointer Structure\n", fs);
1 jermar 156
 
157
    frame_not_free((__address) fs);
32 jermar 158
 
1 jermar 159
    if (fs->config_type == 0 && fs->configuration_table) {
160
        if (fs->mpfib2 >> 7) {
34 jermar 161
            printf("mps_init: PIC mode not supported\n");
1 jermar 162
            return;
163
        }
164
 
165
        ct = fs->configuration_table;
166
        frame_not_free((__address) ct);
167
        config.cpu_count = configure_via_ct();
168
    }
169
    else
170
        config.cpu_count = configure_via_default(fs->config_type);
171
 
172
    if (config.cpu_count > 1) {
173
        map_page_to_frame((__address) l_apic, (__address) l_apic, PAGE_NOT_CACHEABLE, 0);
174
    }      
175
 
176
 
177
    /*
178
     * Must be initialized outside the kmp thread, since it is waited
179
     * on before the kmp thread is created.
180
     */
181
    waitq_initialize(&kmp_completion_wq);
182
    return;
183
}
184
 
185
int configure_via_ct(void)
186
{
187
    __u8 *cur;
188
    int i, cnt;
189
 
190
    if (ct->signature != CT_SIGNATURE) {
191
        printf("configure_via_ct: bad ct->signature\n");
192
        return 1;
193
    }
34 jermar 194
    if (!mps_ct_check()) {
1 jermar 195
        printf("configure_via_ct: bad ct checksum\n");
196
        return 1;
197
    }
198
    if (ct->oem_table) {
199
        printf("configure_via_ct: ct->oem_table not supported\n");
200
        return 1;
201
    }
202
 
203
    l_apic = ct->l_apic;
204
 
205
    cnt = 0;
206
    cur = &ct->base_table[0];
207
    for (i=0; i < ct->entry_count; i++) {
208
        switch (*cur) {
209
            /* Processor entry */
210
            case 0:
211
                processor_entries = processor_entries ? processor_entries : (struct __processor_entry *) cur;
212
                processor_entry_cnt++;
213
                cnt += ct_processor_entry((struct __processor_entry *) cur);
214
                cur += 20;
215
                break;
216
 
217
            /* Bus entry */
218
            case 1:
219
                bus_entries = bus_entries ? bus_entries : (struct __bus_entry *) cur;
220
                bus_entry_cnt++;
221
                ct_bus_entry((struct __bus_entry *) cur);
222
                cur += 8;
223
                break;
224
 
225
            /* I/O Apic */
226
            case 2:
227
                io_apic_entries = io_apic_entries ? io_apic_entries : (struct __io_apic_entry *) cur;
228
                io_apic_entry_cnt++;
229
                ct_io_apic_entry((struct __io_apic_entry *) cur);
230
                cur += 8;
231
                break;
232
 
233
            /* I/O Interrupt Assignment */
234
            case 3:
235
                io_intr_entries = io_intr_entries ? io_intr_entries : (struct __io_intr_entry *) cur;
236
                io_intr_entry_cnt++;
237
                ct_io_intr_entry((struct __io_intr_entry *) cur);
238
                cur += 8;
239
                break;
240
 
241
            /* Local Interrupt Assignment */
242
            case 4:
243
                l_intr_entries = l_intr_entries ? l_intr_entries : (struct __l_intr_entry *) cur;
244
                l_intr_entry_cnt++;
245
                ct_l_intr_entry((struct __l_intr_entry *) cur);
246
                    cur += 8;
247
                break;
248
 
249
            default:
250
                /*
251
                 * Something is wrong. Fallback to UP mode.
252
                 */
253
 
254
                printf("configure_via_ct: ct badness\n");
255
                return 1;
256
        }
257
    }
258
 
259
    /*
260
     * Process extended entries.
261
     */
262
    ct_extended_entries();
263
    return cnt;
264
}
265
 
266
int configure_via_default(__u8 n)
267
{
268
    /*
269
     * Not yet implemented.
270
     */
271
    printf("configure_via_default: not supported\n");
272
    return 1;
273
}
274
 
275
 
276
int ct_processor_entry(struct __processor_entry *pr)
277
{
278
    /*
279
     * Ignore processors which are not marked enabled.
280
     */
281
    if ((pr->cpu_flags & (1<<0)) == 0)
282
        return 0;
283
 
284
    apic_id_mask |= (1<<pr->l_apic_id);
285
    return 1;
286
}
287
 
288
void ct_bus_entry(struct __bus_entry *bus)
289
{
34 jermar 290
#ifdef MPSCT_VERBOSE
1 jermar 291
    char buf[7];
292
    memcopy((__address) bus->bus_type, (__address) buf,6);
293
    buf[6] = 0;
294
    printf("bus%d: %s\n", bus->bus_id, buf);
295
#endif
296
}
297
 
298
void ct_io_apic_entry(struct __io_apic_entry *ioa)
299
{
300
    static int io_apic_count = 0;
301
 
302
    /* this ioapic is marked unusable */
303
    if (ioa->io_apic_flags & 1 == 0)
304
        return;
305
 
306
    if (io_apic_count++ > 0) {
307
        /*
308
         * Multiple IO APIC's are currently not supported.
309
         */
310
        return;
311
    }
312
 
313
    map_page_to_frame((__address) ioa->io_apic, (__address) ioa->io_apic, PAGE_NOT_CACHEABLE, 0);
314
 
315
    io_apic = ioa->io_apic;
316
}
317
 
34 jermar 318
//#define MPSCT_VERBOSE
1 jermar 319
void ct_io_intr_entry(struct __io_intr_entry *iointr)
320
{
34 jermar 321
#ifdef MPSCT_VERBOSE
1 jermar 322
    switch (iointr->intr_type) {
323
        case 0: printf("INT"); break;
324
        case 1: printf("NMI"); break;
325
        case 2: printf("SMI"); break;
326
        case 3: printf("ExtINT"); break;
327
    }
328
    putchar(',');
329
    switch (iointr->poel&3) {
330
        case 0: printf("bus-like"); break;
331
        case 1: printf("active high"); break;
332
        case 2: printf("reserved"); break;
333
        case 3: printf("active low"); break;
334
    }
335
    putchar(',');
336
    switch ((iointr->poel>>2)&3) {
337
        case 0: printf("bus-like"); break;
338
        case 1: printf("edge-triggered"); break;
339
        case 2: printf("reserved"); break;
340
        case 3: printf("level-triggered"); break;
341
    }
342
    putchar(',');
343
    printf("bus%d,irq%d", iointr->src_bus_id, iointr->src_bus_irq);
344
    putchar(',');
345
    printf("io_apic%d,pin%d", iointr->dst_io_apic_id, iointr->dst_io_apic_pin);
346
    putchar('\n'); 
347
#endif
348
}
349
 
350
void ct_l_intr_entry(struct __l_intr_entry *lintr)
351
{
34 jermar 352
#ifdef MPSCT_VERBOSE
1 jermar 353
    switch (lintr->intr_type) {
354
        case 0: printf("INT"); break;
355
        case 1: printf("NMI"); break;
356
        case 2: printf("SMI"); break;
357
        case 3: printf("ExtINT"); break;
358
    }
359
    putchar(',');
360
    switch (lintr->poel&3) {
361
        case 0: printf("bus-like"); break;
362
        case 1: printf("active high"); break;
363
        case 2: printf("reserved"); break;
364
        case 3: printf("active low"); break;
365
    }
366
    putchar(',');
367
    switch ((lintr->poel>>2)&3) {
368
        case 0: printf("bus-like"); break;
369
        case 1: printf("edge-triggered"); break;
370
        case 2: printf("reserved"); break;
371
        case 3: printf("level-triggered"); break;
372
    }
373
    putchar(',');
374
    printf("bus%d,irq%d", lintr->src_bus_id, lintr->src_bus_irq);
375
    putchar(',');
376
    printf("l_apic%d,pin%d", lintr->dst_l_apic_id, lintr->dst_l_apic_pin);
377
    putchar('\n');
378
#endif
379
}
380
 
381
void ct_extended_entries(void)
382
{
13 jermar 383
    __u8 *ext = (__u8 *) ct + ct->base_table_length;
384
    __u8 *cur;
385
 
386
    for (cur = ext; cur < ext + ct->ext_table_length; cur += cur[CT_EXT_ENTRY_LEN]) {
387
        switch (cur[CT_EXT_ENTRY_TYPE]) {
388
            default:
33 jermar 389
                printf("%L: skipping MP Configuration Table extended entry type %d\n", cur, cur[CT_EXT_ENTRY_TYPE]);
13 jermar 390
                break;
391
        }
392
    }
1 jermar 393
}
394
 
395
/*
396
 * Kernel thread for bringing up application processors. It becomes clear
397
 * that we need an arrangement like this (AP's being initialized by a kernel
398
 * thread), for a thread has its dedicated stack. (The stack used during the
399
 * BSP initialization (prior the very first call to scheduler()) will be used
400
 * as an initialization stack for each AP.)
401
 */
402
void kmp(void *arg)
403
{
404
    struct __processor_entry *pr;
405
    __address src, dst;
406
    __address frame;
407
    int i;
408
 
409
    waitq_initialize(&ap_completion_wq);
410
 
411
    /*
412
     * Processor entries immediately follow the configuration table header.
413
     */
414
    pr = processor_entries;
415
 
416
    /*
417
     * Grab a frame and map its address to page 0. This is a hack which
418
     * accesses data in frame 0. Note that page 0 is not present because
419
     * of nil reference bug catching.
420
     */
421
    frame = frame_alloc(FRAME_KA);
422
    map_page_to_frame(frame,0,PAGE_CACHEABLE,0);
423
 
424
    /*
425
     * Set the warm-reset vector to the real-mode address of 4K-aligned ap_boot()
426
     */
427
    *((__u16 *) (frame + 0x467+0)) =  ((__address) ap_boot) >> 4;   /* segment */
428
    *((__u16 *) (frame + 0x467+2)) =  0x0;  /* offset */
429
 
430
    /*
431
     * Give back the borrowed frame and restore identity mapping for it.
432
     */
433
    map_page_to_frame(frame,frame,PAGE_CACHEABLE,0);
434
    frame_free(frame);
435
 
436
    /*
437
     * Save 0xa to address 0xf of the CMOS RAM.
438
     * BIOS will not do the POST after the INIT signal.
439
     */
440
    outb(0x70,0xf);
441
    outb(0x71,0xa);
442
 
443
    cpu_priority_high();
444
 
445
    pic_disable_irqs(0xffff);
446
    apic_init();
447
 
448
    for (i = 0; i < processor_entry_cnt; i++) {
449
        struct descriptor *gdt_new;
450
 
451
        /*
452
         * Skip processors marked unusable.
453
         */
454
        if (pr[i].cpu_flags & (1<<0) == 0)
455
            continue;
27 jermar 456
 
1 jermar 457
        /*
458
         * The bootstrap processor is already up.
459
         */
460
        if (pr[i].cpu_flags & (1<<1))
461
            continue;
27 jermar 462
 
463
        if (pr[i].l_apic_id == l_apic_id()) {
28 jermar 464
            printf("%L: bad processor entry #%d, will not send IPI to myself\n", &pr[i], i);
27 jermar 465
            continue;
466
        }
1 jermar 467
 
468
        /*
469
         * Prepare new GDT for CPU in question.
470
         */
471
        if (!(gdt_new = (struct descriptor *) malloc(GDT_ITEMS*sizeof(struct descriptor))))
472
            panic(PANIC "couldn't allocate memory for GDT\n");
473
 
474
        memcopy(gdt, gdt_new, GDT_ITEMS*sizeof(struct descriptor));
475
        gdtr.base = (__address) gdt_new;
27 jermar 476
 
1 jermar 477
        if (l_apic_send_init_ipi(pr[i].l_apic_id)) {
478
            /*
479
                 * There may be just one AP being initialized at
480
             * the time. After it comes completely up, it is
481
             * supposed to wake us up.
482
                 */
483
            waitq_sleep(&ap_completion_wq);
484
            cpu_priority_high();
485
        }
486
        else {
487
            printf("INIT IPI for l_apic%d failed\n", pr[i].l_apic_id);
488
        }
489
    }
490
 
491
    /*
492
     * Wakeup the kinit thread so that
493
     * system initialization can go on.
494
     */
495
    waitq_wakeup(&kmp_completion_wq, WAKEUP_FIRST);
496
}
497
 
34 jermar 498
int mps_irq_to_pin(int irq)
1 jermar 499
{
500
    int i;
501
 
502
    for(i=0;i<io_intr_entry_cnt;i++) {
503
        if (io_intr_entries[i].src_bus_irq == irq && io_intr_entries[i].intr_type == 0)
504
            return io_intr_entries[i].dst_io_apic_pin;
505
    }
506
 
507
    return -1;
508
}
509
 
510
#endif /* __SMP__ */