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