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