source: mainline/kernel/arch/ia32/src/smp/apic.c@ 0095368

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0095368 was da1bafb, checked in by Martin Decky <martin@…>, 16 years ago

major code revision

  • replace spinlocks taken with interrupts disabled with irq_spinlocks
  • change spacing (not indendation) to be tab-size independent
  • use unsigned integer types where appropriate (especially bit flags)
  • visual separation
  • remove argument names in function prototypes
  • string changes
  • correct some formating directives
  • replace various cryptic single-character variables (t, a, m, c, b, etc.) with proper identifiers (thread, task, timeout, as, itm, itc, etc.)
  • unify some assembler constructs
  • unused page table levels are now optimized out in compile time
  • replace several ints (with boolean semantics) with bools
  • use specifically sized types instead of generic types where appropriate (size_t, uint32_t, btree_key_t)
  • improve comments
  • split asserts with conjuction into multiple independent asserts
  • Property mode set to 100644
File size: 14.3 KB
Line 
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/** @addtogroup ia32
30 * @{
31 */
32/** @file
33 */
34
35#include <typedefs.h>
36#include <arch/smp/apic.h>
37#include <arch/smp/ap.h>
38#include <arch/smp/mps.h>
39#include <arch/boot/boot.h>
40#include <mm/page.h>
41#include <time/delay.h>
42#include <interrupt.h>
43#include <arch/interrupt.h>
44#include <print.h>
45#include <arch/asm.h>
46#include <arch.h>
47#include <ddi/irq.h>
48#include <ddi/device.h>
49
50#ifdef CONFIG_SMP
51
52/*
53 * Advanced Programmable Interrupt Controller for SMP systems.
54 * Tested on:
55 * Bochs 2.0.2 - Bochs 2.2.6 with 2-8 CPUs
56 * Simics 2.0.28 - Simics 2.2.19 2-15 CPUs
57 * VMware Workstation 5.5 with 2 CPUs
58 * QEMU 0.8.0 with 2-15 CPUs
59 * ASUS P/I-P65UP5 + ASUS C-P55T2D REV. 1.41 with 2x 200Mhz Pentium CPUs
60 * ASUS PCH-DL with 2x 3000Mhz Pentium 4 Xeon (HT) CPUs
61 * MSI K7D Master-L with 2x 2100MHz Athlon MP CPUs
62 *
63 */
64
65/*
66 * These variables either stay configured as initilalized, or are changed by
67 * the MP configuration code.
68 *
69 * Pay special attention to the volatile keyword. Without it, gcc -O2 would
70 * optimize the code too much and accesses to l_apic and io_apic, that must
71 * always be 32-bit, would use byte oriented instructions.
72 *
73 */
74volatile uint32_t *l_apic = (uint32_t *) 0xfee00000;
75volatile uint32_t *io_apic = (uint32_t *) 0xfec00000;
76
77uint32_t apic_id_mask = 0;
78static irq_t l_apic_timer_irq;
79
80static int apic_poll_errors(void);
81
82#ifdef LAPIC_VERBOSE
83static const char *delmod_str[] = {
84 "Fixed",
85 "Lowest Priority",
86 "SMI",
87 "Reserved",
88 "NMI",
89 "INIT",
90 "STARTUP",
91 "ExtInt"
92};
93
94static const char *destmod_str[] = {
95 "Physical",
96 "Logical"
97};
98
99static const char *trigmod_str[] = {
100 "Edge",
101 "Level"
102};
103
104static const char *mask_str[] = {
105 "Unmasked",
106 "Masked"
107};
108
109static const char *delivs_str[] = {
110 "Idle",
111 "Send Pending"
112};
113
114static const char *tm_mode_str[] = {
115 "One-shot",
116 "Periodic"
117};
118
119static const char *intpol_str[] = {
120 "Polarity High",
121 "Polarity Low"
122};
123#endif /* LAPIC_VERBOSE */
124
125/** APIC spurious interrupt handler.
126 *
127 * @param n Interrupt vector.
128 * @param istate Interrupted state.
129 *
130 */
131static void apic_spurious(int n __attribute__((unused)),
132 istate_t *istate __attribute__((unused)))
133{
134#ifdef CONFIG_DEBUG
135 printf("cpu%u: APIC spurious interrupt\n", CPU->id);
136#endif
137}
138
139static irq_ownership_t l_apic_timer_claim(irq_t *irq)
140{
141 return IRQ_ACCEPT;
142}
143
144static void l_apic_timer_irq_handler(irq_t *irq)
145{
146 /*
147 * Holding a spinlock could prevent clock() from preempting
148 * the current thread. In this case, we don't need to hold the
149 * irq->lock so we just unlock it and then lock it again.
150 */
151 irq_spinlock_unlock(&irq->lock, false);
152 clock();
153 irq_spinlock_lock(&irq->lock, false);
154}
155
156/** Initialize APIC on BSP. */
157void apic_init(void)
158{
159 exc_register(VECTOR_APIC_SPUR, "apic_spurious", (iroutine) apic_spurious);
160
161 enable_irqs_function = io_apic_enable_irqs;
162 disable_irqs_function = io_apic_disable_irqs;
163 eoi_function = l_apic_eoi;
164
165 /*
166 * Configure interrupt routing.
167 * IRQ 0 remains masked as the time signal is generated by l_apic's themselves.
168 * Other interrupts will be forwarded to the lowest priority CPU.
169 */
170 io_apic_disable_irqs(0xffff);
171
172 irq_initialize(&l_apic_timer_irq);
173 l_apic_timer_irq.preack = true;
174 l_apic_timer_irq.devno = device_assign_devno();
175 l_apic_timer_irq.inr = IRQ_CLK;
176 l_apic_timer_irq.claim = l_apic_timer_claim;
177 l_apic_timer_irq.handler = l_apic_timer_irq_handler;
178 irq_register(&l_apic_timer_irq);
179
180 uint8_t i;
181 for (i = 0; i < IRQ_COUNT; i++) {
182 int pin;
183
184 if ((pin = smp_irq_to_pin(i)) != -1)
185 io_apic_change_ioredtbl((uint8_t) pin, DEST_ALL, (uint8_t) (IVT_IRQBASE + i), LOPRI);
186 }
187
188 /*
189 * Ensure that io_apic has unique ID.
190 */
191 io_apic_id_t idreg;
192
193 idreg.value = io_apic_read(IOAPICID);
194 if ((1 << idreg.apic_id) & apic_id_mask) { /* See if IO APIC ID is used already */
195 for (i = 0; i < APIC_ID_COUNT; i++) {
196 if (!((1 << i) & apic_id_mask)) {
197 idreg.apic_id = i;
198 io_apic_write(IOAPICID, idreg.value);
199 break;
200 }
201 }
202 }
203
204 /*
205 * Configure the BSP's lapic.
206 */
207 l_apic_init();
208 l_apic_debug();
209}
210
211/** Poll for APIC errors.
212 *
213 * Examine Error Status Register and report all errors found.
214 *
215 * @return 0 on error, 1 on success.
216 *
217 */
218int apic_poll_errors(void)
219{
220 esr_t esr;
221
222 esr.value = l_apic[ESR];
223
224 if (esr.send_checksum_error)
225 printf("Send Checksum Error\n");
226 if (esr.receive_checksum_error)
227 printf("Receive Checksum Error\n");
228 if (esr.send_accept_error)
229 printf("Send Accept Error\n");
230 if (esr.receive_accept_error)
231 printf("Receive Accept Error\n");
232 if (esr.send_illegal_vector)
233 printf("Send Illegal Vector\n");
234 if (esr.received_illegal_vector)
235 printf("Received Illegal Vector\n");
236 if (esr.illegal_register_address)
237 printf("Illegal Register Address\n");
238
239 return !esr.err_bitmap;
240}
241
242/** Send all CPUs excluding CPU IPI vector.
243 *
244 * @param vector Interrupt vector to be sent.
245 *
246 * @return 0 on failure, 1 on success.
247 *
248 */
249int l_apic_broadcast_custom_ipi(uint8_t vector)
250{
251 icr_t icr;
252
253 icr.lo = l_apic[ICRlo];
254 icr.delmod = DELMOD_FIXED;
255 icr.destmod = DESTMOD_LOGIC;
256 icr.level = LEVEL_ASSERT;
257 icr.shorthand = SHORTHAND_ALL_EXCL;
258 icr.trigger_mode = TRIGMOD_LEVEL;
259 icr.vector = vector;
260
261 l_apic[ICRlo] = icr.lo;
262
263 icr.lo = l_apic[ICRlo];
264 if (icr.delivs == DELIVS_PENDING) {
265#ifdef CONFIG_DEBUG
266 printf("IPI is pending.\n");
267#endif
268 }
269
270 return apic_poll_errors();
271}
272
273/** Universal Start-up Algorithm for bringing up the AP processors.
274 *
275 * @param apicid APIC ID of the processor to be brought up.
276 *
277 * @return 0 on failure, 1 on success.
278 *
279 */
280int l_apic_send_init_ipi(uint8_t apicid)
281{
282 /*
283 * Read the ICR register in and zero all non-reserved fields.
284 */
285 icr_t icr;
286
287 icr.lo = l_apic[ICRlo];
288 icr.hi = l_apic[ICRhi];
289
290 icr.delmod = DELMOD_INIT;
291 icr.destmod = DESTMOD_PHYS;
292 icr.level = LEVEL_ASSERT;
293 icr.trigger_mode = TRIGMOD_LEVEL;
294 icr.shorthand = SHORTHAND_NONE;
295 icr.vector = 0;
296 icr.dest = apicid;
297
298 l_apic[ICRhi] = icr.hi;
299 l_apic[ICRlo] = icr.lo;
300
301 /*
302 * According to MP Specification, 20us should be enough to
303 * deliver the IPI.
304 */
305 delay(20);
306
307 if (!apic_poll_errors())
308 return 0;
309
310 icr.lo = l_apic[ICRlo];
311 if (icr.delivs == DELIVS_PENDING) {
312#ifdef CONFIG_DEBUG
313 printf("IPI is pending.\n");
314#endif
315 }
316
317 icr.delmod = DELMOD_INIT;
318 icr.destmod = DESTMOD_PHYS;
319 icr.level = LEVEL_DEASSERT;
320 icr.shorthand = SHORTHAND_NONE;
321 icr.trigger_mode = TRIGMOD_LEVEL;
322 icr.vector = 0;
323 l_apic[ICRlo] = icr.lo;
324
325 /*
326 * Wait 10ms as MP Specification specifies.
327 */
328 delay(10000);
329
330 if (!is_82489DX_apic(l_apic[LAVR])) {
331 /*
332 * If this is not 82489DX-based l_apic we must send two STARTUP IPI's.
333 */
334 unsigned int i;
335 for (i = 0; i < 2; i++) {
336 icr.lo = l_apic[ICRlo];
337 icr.vector = (uint8_t) (((uintptr_t) ap_boot) >> 12); /* calculate the reset vector */
338 icr.delmod = DELMOD_STARTUP;
339 icr.destmod = DESTMOD_PHYS;
340 icr.level = LEVEL_ASSERT;
341 icr.shorthand = SHORTHAND_NONE;
342 icr.trigger_mode = TRIGMOD_LEVEL;
343 l_apic[ICRlo] = icr.lo;
344 delay(200);
345 }
346 }
347
348 return apic_poll_errors();
349}
350
351/** Initialize Local APIC. */
352void l_apic_init(void)
353{
354 /* Initialize LVT Error register. */
355 lvt_error_t error;
356
357 error.value = l_apic[LVT_Err];
358 error.masked = true;
359 l_apic[LVT_Err] = error.value;
360
361 /* Initialize LVT LINT0 register. */
362 lvt_lint_t lint;
363
364 lint.value = l_apic[LVT_LINT0];
365 lint.masked = true;
366 l_apic[LVT_LINT0] = lint.value;
367
368 /* Initialize LVT LINT1 register. */
369 lint.value = l_apic[LVT_LINT1];
370 lint.masked = true;
371 l_apic[LVT_LINT1] = lint.value;
372
373 /* Task Priority Register initialization. */
374 tpr_t tpr;
375
376 tpr.value = l_apic[TPR];
377 tpr.pri_sc = 0;
378 tpr.pri = 0;
379 l_apic[TPR] = tpr.value;
380
381 /* Spurious-Interrupt Vector Register initialization. */
382 svr_t svr;
383
384 svr.value = l_apic[SVR];
385 svr.vector = VECTOR_APIC_SPUR;
386 svr.lapic_enabled = true;
387 svr.focus_checking = true;
388 l_apic[SVR] = svr.value;
389
390 if (CPU->arch.family >= 6)
391 enable_l_apic_in_msr();
392
393 /* Interrupt Command Register initialization. */
394 icr_t icr;
395
396 icr.lo = l_apic[ICRlo];
397 icr.delmod = DELMOD_INIT;
398 icr.destmod = DESTMOD_PHYS;
399 icr.level = LEVEL_DEASSERT;
400 icr.shorthand = SHORTHAND_ALL_INCL;
401 icr.trigger_mode = TRIGMOD_LEVEL;
402 l_apic[ICRlo] = icr.lo;
403
404 /* Timer Divide Configuration Register initialization. */
405 tdcr_t tdcr;
406
407 tdcr.value = l_apic[TDCR];
408 tdcr.div_value = DIVIDE_1;
409 l_apic[TDCR] = tdcr.value;
410
411 /* Program local timer. */
412 lvt_tm_t tm;
413
414 tm.value = l_apic[LVT_Tm];
415 tm.vector = VECTOR_CLK;
416 tm.mode = TIMER_PERIODIC;
417 tm.masked = false;
418 l_apic[LVT_Tm] = tm.value;
419
420 /*
421 * Measure and configure the timer to generate timer
422 * interrupt with period 1s/HZ seconds.
423 */
424 uint32_t t1 = l_apic[CCRT];
425 l_apic[ICRT] = 0xffffffff;
426
427 while (l_apic[CCRT] == t1);
428
429 t1 = l_apic[CCRT];
430 delay(1000000 / HZ);
431 uint32_t t2 = l_apic[CCRT];
432
433 l_apic[ICRT] = t1 - t2;
434
435 /* Program Logical Destination Register. */
436 ASSERT(CPU->id < 8);
437 ldr_t ldr;
438
439 ldr.value = l_apic[LDR];
440 ldr.id = (uint8_t) (1 << CPU->id);
441 l_apic[LDR] = ldr.value;
442
443 /* Program Destination Format Register for Flat mode. */
444 dfr_t dfr;
445
446 dfr.value = l_apic[DFR];
447 dfr.model = MODEL_FLAT;
448 l_apic[DFR] = dfr.value;
449}
450
451/** Local APIC End of Interrupt. */
452void l_apic_eoi(void)
453{
454 l_apic[EOI] = 0;
455}
456
457/** Dump content of Local APIC registers. */
458void l_apic_debug(void)
459{
460#ifdef LAPIC_VERBOSE
461 printf("LVT on cpu%" PRIs ", LAPIC ID: %" PRIu8 "\n", CPU->id, l_apic_id());
462
463 lvt_tm_t tm;
464 tm.value = l_apic[LVT_Tm];
465 printf("LVT Tm: vector=%hhd, %s, %s, %s\n", tm.vector, delivs_str[tm.delivs], mask_str[tm.masked], tm_mode_str[tm.mode]);
466
467 lvt_lint_t lint;
468 lint.value = l_apic[LVT_LINT0];
469 printf("LVT LINT0: vector=%hhd, %s, %s, %s, irr=%d, %s, %s\n", tm.vector, delmod_str[lint.delmod], delivs_str[lint.delivs], intpol_str[lint.intpol], lint.irr, trigmod_str[lint.trigger_mode], mask_str[lint.masked]);
470 lint.value = l_apic[LVT_LINT1];
471 printf("LVT LINT1: vector=%hhd, %s, %s, %s, irr=%d, %s, %s\n", tm.vector, delmod_str[lint.delmod], delivs_str[lint.delivs], intpol_str[lint.intpol], lint.irr, trigmod_str[lint.trigger_mode], mask_str[lint.masked]);
472
473 lvt_error_t error;
474 error.value = l_apic[LVT_Err];
475 printf("LVT Err: vector=%hhd, %s, %s\n", error.vector, delivs_str[error.delivs], mask_str[error.masked]);
476#endif
477}
478
479/** Get Local APIC ID.
480 *
481 * @return Local APIC ID.
482 *
483 */
484uint8_t l_apic_id(void)
485{
486 l_apic_id_t idreg;
487
488 idreg.value = l_apic[L_APIC_ID];
489 return idreg.apic_id;
490}
491
492/** Read from IO APIC register.
493 *
494 * @param address IO APIC register address.
495 *
496 * @return Content of the addressed IO APIC register.
497 *
498 */
499uint32_t io_apic_read(uint8_t address)
500{
501 io_regsel_t regsel;
502
503 regsel.value = io_apic[IOREGSEL];
504 regsel.reg_addr = address;
505 io_apic[IOREGSEL] = regsel.value;
506 return io_apic[IOWIN];
507}
508
509/** Write to IO APIC register.
510 *
511 * @param address IO APIC register address.
512 * @param val Content to be written to the addressed IO APIC register.
513 *
514 */
515void io_apic_write(uint8_t address, uint32_t val)
516{
517 io_regsel_t regsel;
518
519 regsel.value = io_apic[IOREGSEL];
520 regsel.reg_addr = address;
521 io_apic[IOREGSEL] = regsel.value;
522 io_apic[IOWIN] = val;
523}
524
525/** Change some attributes of one item in I/O Redirection Table.
526 *
527 * @param pin IO APIC pin number.
528 * @param dest Interrupt destination address.
529 * @param vec Interrupt vector to trigger.
530 * @param flags Flags.
531 *
532 */
533void io_apic_change_ioredtbl(uint8_t pin, uint8_t dest, uint8_t vec,
534 unsigned int flags)
535{
536 unsigned int dlvr;
537
538 if (flags & LOPRI)
539 dlvr = DELMOD_LOWPRI;
540 else
541 dlvr = DELMOD_FIXED;
542
543 io_redirection_reg_t reg;
544 reg.lo = io_apic_read((uint8_t) (IOREDTBL + pin * 2));
545 reg.hi = io_apic_read((uint8_t) (IOREDTBL + pin * 2 + 1));
546
547 reg.dest = dest;
548 reg.destmod = DESTMOD_LOGIC;
549 reg.trigger_mode = TRIGMOD_EDGE;
550 reg.intpol = POLARITY_HIGH;
551 reg.delmod = dlvr;
552 reg.intvec = vec;
553
554 io_apic_write((uint8_t) (IOREDTBL + pin * 2), reg.lo);
555 io_apic_write((uint8_t) (IOREDTBL + pin * 2 + 1), reg.hi);
556}
557
558/** Mask IRQs in IO APIC.
559 *
560 * @param irqmask Bitmask of IRQs to be masked (0 = do not mask, 1 = mask).
561 *
562 */
563void io_apic_disable_irqs(uint16_t irqmask)
564{
565 unsigned int i;
566 for (i = 0; i < 16; i++) {
567 if (irqmask & (1 << i)) {
568 /*
569 * Mask the signal input in IO APIC if there is a
570 * mapping for the respective IRQ number.
571 */
572 int pin = smp_irq_to_pin(i);
573 if (pin != -1) {
574 io_redirection_reg_t reg;
575
576 reg.lo = io_apic_read((uint8_t) (IOREDTBL + pin * 2));
577 reg.masked = true;
578 io_apic_write((uint8_t) (IOREDTBL + pin * 2), reg.lo);
579 }
580
581 }
582 }
583}
584
585/** Unmask IRQs in IO APIC.
586 *
587 * @param irqmask Bitmask of IRQs to be unmasked (0 = do not unmask, 1 = unmask).
588 *
589 */
590void io_apic_enable_irqs(uint16_t irqmask)
591{
592 unsigned int i;
593 for (i = 0; i < 16; i++) {
594 if (irqmask & (1 << i)) {
595 /*
596 * Unmask the signal input in IO APIC if there is a
597 * mapping for the respective IRQ number.
598 */
599 int pin = smp_irq_to_pin(i);
600 if (pin != -1) {
601 io_redirection_reg_t reg;
602
603 reg.lo = io_apic_read((uint8_t) (IOREDTBL + pin * 2));
604 reg.masked = false;
605 io_apic_write((uint8_t) (IOREDTBL + pin * 2), reg.lo);
606 }
607
608 }
609 }
610}
611
612#endif /* CONFIG_SMP */
613
614/** @}
615 */
Note: See TracBrowser for help on using the repository browser.