Subversion Repositories HelenOS-historic

Rev

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