]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/i386/i386/local_apic.c
Copy head to stable/8 as part of 8.0 Release cycle.
[FreeBSD/stable/8.git] / sys / i386 / i386 / local_apic.c
1 /*-
2  * Copyright (c) 2003 John Baldwin <jhb@FreeBSD.org>
3  * Copyright (c) 1996, by Steve Passe
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. The name of the developer may NOT be used to endorse or promote products
12  *    derived from this software without specific prior written permission.
13  * 3. Neither the name of the author nor the names of any co-contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 /*
31  * Local APIC support on Pentium and later processors.
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include "opt_hwpmc_hooks.h"
38 #include "opt_kdtrace.h"
39
40 #include "opt_ddb.h"
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/bus.h>
45 #include <sys/kernel.h>
46 #include <sys/lock.h>
47 #include <sys/mutex.h>
48 #include <sys/pcpu.h>
49 #include <sys/proc.h>
50 #include <sys/sched.h>
51 #include <sys/smp.h>
52
53 #include <vm/vm.h>
54 #include <vm/pmap.h>
55
56 #include <machine/apicreg.h>
57 #include <machine/cpu.h>
58 #include <machine/cputypes.h>
59 #include <machine/frame.h>
60 #include <machine/intr_machdep.h>
61 #include <machine/apicvar.h>
62 #include <machine/md_var.h>
63 #include <machine/smp.h>
64 #include <machine/specialreg.h>
65
66 #ifdef DDB
67 #include <sys/interrupt.h>
68 #include <ddb/ddb.h>
69 #endif
70
71 #ifdef KDTRACE_HOOKS
72 #include <sys/dtrace_bsd.h>
73 cyclic_clock_func_t     lapic_cyclic_clock_func[MAXCPU];
74 #endif
75
76 /* Sanity checks on IDT vectors. */
77 CTASSERT(APIC_IO_INTS + APIC_NUM_IOINTS == APIC_TIMER_INT);
78 CTASSERT(APIC_TIMER_INT < APIC_LOCAL_INTS);
79 CTASSERT(APIC_LOCAL_INTS == 240);
80 CTASSERT(IPI_STOP < APIC_SPURIOUS_INT);
81
82 /* Magic IRQ values for the timer and syscalls. */
83 #define IRQ_TIMER       (NUM_IO_INTS + 1)
84 #define IRQ_SYSCALL     (NUM_IO_INTS + 2)
85
86 /*
87  * Support for local APICs.  Local APICs manage interrupts on each
88  * individual processor as opposed to I/O APICs which receive interrupts
89  * from I/O devices and then forward them on to the local APICs.
90  *
91  * Local APICs can also send interrupts to each other thus providing the
92  * mechanism for IPIs.
93  */
94
95 struct lvt {
96         u_int lvt_edgetrigger:1;
97         u_int lvt_activehi:1;
98         u_int lvt_masked:1;
99         u_int lvt_active:1;
100         u_int lvt_mode:16;
101         u_int lvt_vector:8;
102 };
103
104 struct lapic {
105         struct lvt la_lvts[LVT_MAX + 1];
106         u_int la_id:8;
107         u_int la_cluster:4;
108         u_int la_cluster_id:2;
109         u_int la_present:1;
110         u_long *la_timer_count;
111         u_long la_hard_ticks;
112         u_long la_stat_ticks;
113         u_long la_prof_ticks;
114         /* Include IDT_SYSCALL to make indexing easier. */
115         int la_ioint_irqs[APIC_NUM_IOINTS + 1];
116 } static lapics[MAX_APIC_ID + 1];
117
118 /* XXX: should thermal be an NMI? */
119
120 /* Global defaults for local APIC LVT entries. */
121 static struct lvt lvts[LVT_MAX + 1] = {
122         { 1, 1, 1, 1, APIC_LVT_DM_EXTINT, 0 },  /* LINT0: masked ExtINT */
123         { 1, 1, 0, 1, APIC_LVT_DM_NMI, 0 },     /* LINT1: NMI */
124         { 1, 1, 1, 1, APIC_LVT_DM_FIXED, APIC_TIMER_INT },      /* Timer */
125         { 1, 1, 1, 1, APIC_LVT_DM_FIXED, APIC_ERROR_INT },      /* Error */
126         { 1, 1, 0, 1, APIC_LVT_DM_NMI, 0 },     /* PMC */
127         { 1, 1, 1, 1, APIC_LVT_DM_FIXED, APIC_THERMAL_INT },    /* Thermal */
128 };
129
130 static inthand_t *ioint_handlers[] = {
131         NULL,                   /* 0 - 31 */
132         IDTVEC(apic_isr1),      /* 32 - 63 */
133         IDTVEC(apic_isr2),      /* 64 - 95 */
134         IDTVEC(apic_isr3),      /* 96 - 127 */
135         IDTVEC(apic_isr4),      /* 128 - 159 */
136         IDTVEC(apic_isr5),      /* 160 - 191 */
137         IDTVEC(apic_isr6),      /* 192 - 223 */
138         IDTVEC(apic_isr7),      /* 224 - 255 */
139 };
140
141
142 static u_int32_t lapic_timer_divisors[] = {
143         APIC_TDCR_1, APIC_TDCR_2, APIC_TDCR_4, APIC_TDCR_8, APIC_TDCR_16,
144         APIC_TDCR_32, APIC_TDCR_64, APIC_TDCR_128
145 };
146
147 extern inthand_t IDTVEC(rsvd);
148
149 volatile lapic_t *lapic;
150 vm_paddr_t lapic_paddr;
151 static u_long lapic_timer_divisor, lapic_timer_period, lapic_timer_hz;
152
153 static void     lapic_enable(void);
154 static void     lapic_resume(struct pic *pic);
155 static void     lapic_timer_enable_intr(void);
156 static void     lapic_timer_oneshot(u_int count);
157 static void     lapic_timer_periodic(u_int count);
158 static void     lapic_timer_set_divisor(u_int divisor);
159 static uint32_t lvt_mode(struct lapic *la, u_int pin, uint32_t value);
160
161 struct pic lapic_pic = { .pic_resume = lapic_resume };
162
163 static uint32_t
164 lvt_mode(struct lapic *la, u_int pin, uint32_t value)
165 {
166         struct lvt *lvt;
167
168         KASSERT(pin <= LVT_MAX, ("%s: pin %u out of range", __func__, pin));
169         if (la->la_lvts[pin].lvt_active)
170                 lvt = &la->la_lvts[pin];
171         else
172                 lvt = &lvts[pin];
173
174         value &= ~(APIC_LVT_M | APIC_LVT_TM | APIC_LVT_IIPP | APIC_LVT_DM |
175             APIC_LVT_VECTOR);
176         if (lvt->lvt_edgetrigger == 0)
177                 value |= APIC_LVT_TM;
178         if (lvt->lvt_activehi == 0)
179                 value |= APIC_LVT_IIPP_INTALO;
180         if (lvt->lvt_masked)
181                 value |= APIC_LVT_M;
182         value |= lvt->lvt_mode;
183         switch (lvt->lvt_mode) {
184         case APIC_LVT_DM_NMI:
185         case APIC_LVT_DM_SMI:
186         case APIC_LVT_DM_INIT:
187         case APIC_LVT_DM_EXTINT:
188                 if (!lvt->lvt_edgetrigger) {
189                         printf("lapic%u: Forcing LINT%u to edge trigger\n",
190                             la->la_id, pin);
191                         value |= APIC_LVT_TM;
192                 }
193                 /* Use a vector of 0. */
194                 break;
195         case APIC_LVT_DM_FIXED:
196                 value |= lvt->lvt_vector;
197                 break;
198         default:
199                 panic("bad APIC LVT delivery mode: %#x\n", value);
200         }
201         return (value);
202 }
203
204 /*
205  * Map the local APIC and setup necessary interrupt vectors.
206  */
207 void
208 lapic_init(vm_paddr_t addr)
209 {
210
211         /* Map the local APIC and setup the spurious interrupt handler. */
212         KASSERT(trunc_page(addr) == addr,
213             ("local APIC not aligned on a page boundary"));
214         lapic = pmap_mapdev(addr, sizeof(lapic_t));
215         lapic_paddr = addr;
216         setidt(APIC_SPURIOUS_INT, IDTVEC(spuriousint), SDT_SYS386IGT, SEL_KPL,
217             GSEL(GCODE_SEL, SEL_KPL));
218
219         /* Perform basic initialization of the BSP's local APIC. */
220         lapic_enable();
221
222         /* Set BSP's per-CPU local APIC ID. */
223         PCPU_SET(apic_id, lapic_id());
224
225         /* Local APIC timer interrupt. */
226         setidt(APIC_TIMER_INT, IDTVEC(timerint), SDT_SYS386IGT, SEL_KPL,
227             GSEL(GCODE_SEL, SEL_KPL));
228
229         /* XXX: error/thermal interrupts */
230 }
231
232 /*
233  * Create a local APIC instance.
234  */
235 void
236 lapic_create(u_int apic_id, int boot_cpu)
237 {
238         int i;
239
240         if (apic_id > MAX_APIC_ID) {
241                 printf("APIC: Ignoring local APIC with ID %d\n", apic_id);
242                 if (boot_cpu)
243                         panic("Can't ignore BSP");
244                 return;
245         }
246         KASSERT(!lapics[apic_id].la_present, ("duplicate local APIC %u",
247             apic_id));
248
249         /*
250          * Assume no local LVT overrides and a cluster of 0 and
251          * intra-cluster ID of 0.
252          */
253         lapics[apic_id].la_present = 1;
254         lapics[apic_id].la_id = apic_id;
255         for (i = 0; i < LVT_MAX; i++) {
256                 lapics[apic_id].la_lvts[i] = lvts[i];
257                 lapics[apic_id].la_lvts[i].lvt_active = 0;
258         }
259         for (i = 0; i <= APIC_NUM_IOINTS; i++)
260             lapics[apic_id].la_ioint_irqs[i] = -1;
261         lapics[apic_id].la_ioint_irqs[IDT_SYSCALL - APIC_IO_INTS] = IRQ_SYSCALL;
262         lapics[apic_id].la_ioint_irqs[APIC_TIMER_INT - APIC_IO_INTS] =
263             IRQ_TIMER;
264
265 #ifdef SMP
266         cpu_add(apic_id, boot_cpu);
267 #endif
268 }
269
270 /*
271  * Dump contents of local APIC registers
272  */
273 void
274 lapic_dump(const char* str)
275 {
276
277         printf("cpu%d %s:\n", PCPU_GET(cpuid), str);
278         printf("     ID: 0x%08x   VER: 0x%08x LDR: 0x%08x DFR: 0x%08x\n",
279             lapic->id, lapic->version, lapic->ldr, lapic->dfr);
280         printf("  lint0: 0x%08x lint1: 0x%08x TPR: 0x%08x SVR: 0x%08x\n",
281             lapic->lvt_lint0, lapic->lvt_lint1, lapic->tpr, lapic->svr);
282         printf("  timer: 0x%08x therm: 0x%08x err: 0x%08x pcm: 0x%08x\n",
283             lapic->lvt_timer, lapic->lvt_thermal, lapic->lvt_error,
284             lapic->lvt_pcint);
285 }
286
287 void
288 lapic_setup(int boot)
289 {
290         struct lapic *la;
291         u_int32_t maxlvt;
292         register_t eflags;
293         char buf[MAXCOMLEN + 1];
294
295         la = &lapics[lapic_id()];
296         KASSERT(la->la_present, ("missing APIC structure"));
297         eflags = intr_disable();
298         maxlvt = (lapic->version & APIC_VER_MAXLVT) >> MAXLVTSHIFT;
299
300         /* Initialize the TPR to allow all interrupts. */
301         lapic_set_tpr(0);
302
303         /* Setup spurious vector and enable the local APIC. */
304         lapic_enable();
305
306         /* Program LINT[01] LVT entries. */
307         lapic->lvt_lint0 = lvt_mode(la, LVT_LINT0, lapic->lvt_lint0);
308         lapic->lvt_lint1 = lvt_mode(la, LVT_LINT1, lapic->lvt_lint1);
309
310 #ifdef  HWPMC_HOOKS
311         /* Program the PMC LVT entry if present. */
312         if (maxlvt >= LVT_PMC)
313                 lapic->lvt_pcint = lvt_mode(la, LVT_PMC, lapic->lvt_pcint);
314 #endif
315
316         /* Program timer LVT and setup handler. */
317         lapic->lvt_timer = lvt_mode(la, LVT_TIMER, lapic->lvt_timer);
318         if (boot) {
319                 snprintf(buf, sizeof(buf), "cpu%d: timer", PCPU_GET(cpuid));
320                 intrcnt_add(buf, &la->la_timer_count);
321         }
322
323         /* We don't setup the timer during boot on the BSP until later. */
324         if (!(boot && PCPU_GET(cpuid) == 0) && lapic_timer_hz != 0) {
325                 KASSERT(lapic_timer_period != 0, ("lapic%u: zero divisor",
326                     lapic_id()));
327                 lapic_timer_set_divisor(lapic_timer_divisor);
328                 lapic_timer_periodic(lapic_timer_period);
329                 lapic_timer_enable_intr();
330         }
331
332         /* XXX: Error and thermal LVTs */
333
334         intr_restore(eflags);
335 }
336
337 /*
338  * Called by cpu_initclocks() on the BSP to setup the local APIC timer so
339  * that it can drive hardclock, statclock, and profclock.  This function
340  * returns true if it is able to use the local APIC timer to drive the
341  * clocks and false if it is not able.
342  */
343 int
344 lapic_setup_clock(void)
345 {
346         u_long value;
347         int i;
348
349         /* Can't drive the timer without a local APIC. */
350         if (lapic == NULL)
351                 return (0);
352
353         if (resource_int_value("apic", 0, "clock", &i) == 0 && i == 0)
354                 return (0);
355
356         /* Start off with a divisor of 2 (power on reset default). */
357         lapic_timer_divisor = 2;
358
359         /* Try to calibrate the local APIC timer. */
360         do {
361                 lapic_timer_set_divisor(lapic_timer_divisor);
362                 lapic_timer_oneshot(APIC_TIMER_MAX_COUNT);
363                 DELAY(2000000);
364                 value = APIC_TIMER_MAX_COUNT - lapic->ccr_timer;
365                 if (value != APIC_TIMER_MAX_COUNT)
366                         break;
367                 lapic_timer_divisor <<= 1;
368         } while (lapic_timer_divisor <= 128);
369         if (lapic_timer_divisor > 128)
370                 panic("lapic: Divisor too big");
371         value /= 2;
372         if (bootverbose)
373                 printf("lapic: Divisor %lu, Frequency %lu hz\n",
374                     lapic_timer_divisor, value);
375
376         /*
377          * We want to run stathz in the neighborhood of 128hz.  We would
378          * like profhz to run as often as possible, so we let it run on
379          * each clock tick.  We try to honor the requested 'hz' value as
380          * much as possible.
381          *
382          * If 'hz' is above 1500, then we just let the lapic timer
383          * (and profhz) run at hz.  If 'hz' is below 1500 but above
384          * 750, then we let the lapic timer run at 2 * 'hz'.  If 'hz'
385          * is below 750 then we let the lapic timer run at 4 * 'hz'.
386          */
387         if (hz >= 1500)
388                 lapic_timer_hz = hz;
389         else if (hz >= 750)
390                 lapic_timer_hz = hz * 2;
391         else
392                 lapic_timer_hz = hz * 4;
393         if (lapic_timer_hz < 128)
394                 stathz = lapic_timer_hz;
395         else
396                 stathz = lapic_timer_hz / (lapic_timer_hz / 128);
397         profhz = lapic_timer_hz;
398         lapic_timer_period = value / lapic_timer_hz;
399
400         /*
401          * Start up the timer on the BSP.  The APs will kick off their
402          * timer during lapic_setup().
403          */
404         lapic_timer_periodic(lapic_timer_period);
405         lapic_timer_enable_intr();
406         return (1);
407 }
408
409 void
410 lapic_disable(void)
411 {
412         uint32_t value;
413
414         /* Software disable the local APIC. */
415         value = lapic->svr;
416         value &= ~APIC_SVR_SWEN;
417         lapic->svr = value;
418 }
419
420 static void
421 lapic_enable(void)
422 {
423         u_int32_t value;
424
425         /* Program the spurious vector to enable the local APIC. */
426         value = lapic->svr;
427         value &= ~(APIC_SVR_VECTOR | APIC_SVR_FOCUS);
428         value |= (APIC_SVR_FEN | APIC_SVR_SWEN | APIC_SPURIOUS_INT);
429         lapic->svr = value;
430 }
431
432 /* Reset the local APIC on the BSP during resume. */
433 static void
434 lapic_resume(struct pic *pic)
435 {
436
437         lapic_setup(0);
438 }
439
440 int
441 lapic_id(void)
442 {
443
444         KASSERT(lapic != NULL, ("local APIC is not mapped"));
445         return (lapic->id >> APIC_ID_SHIFT);
446 }
447
448 int
449 lapic_intr_pending(u_int vector)
450 {
451         volatile u_int32_t *irr;
452
453         /*
454          * The IRR registers are an array of 128-bit registers each of
455          * which only describes 32 interrupts in the low 32 bits..  Thus,
456          * we divide the vector by 32 to get the 128-bit index.  We then
457          * multiply that index by 4 to get the equivalent index from
458          * treating the IRR as an array of 32-bit registers.  Finally, we
459          * modulus the vector by 32 to determine the individual bit to
460          * test.
461          */
462         irr = &lapic->irr0;
463         return (irr[(vector / 32) * 4] & 1 << (vector % 32));
464 }
465
466 void
467 lapic_set_logical_id(u_int apic_id, u_int cluster, u_int cluster_id)
468 {
469         struct lapic *la;
470
471         KASSERT(lapics[apic_id].la_present, ("%s: APIC %u doesn't exist",
472             __func__, apic_id));
473         KASSERT(cluster <= APIC_MAX_CLUSTER, ("%s: cluster %u too big",
474             __func__, cluster));
475         KASSERT(cluster_id <= APIC_MAX_INTRACLUSTER_ID,
476             ("%s: intra cluster id %u too big", __func__, cluster_id));
477         la = &lapics[apic_id];
478         la->la_cluster = cluster;
479         la->la_cluster_id = cluster_id;
480 }
481
482 int
483 lapic_set_lvt_mask(u_int apic_id, u_int pin, u_char masked)
484 {
485
486         if (pin > LVT_MAX)
487                 return (EINVAL);
488         if (apic_id == APIC_ID_ALL) {
489                 lvts[pin].lvt_masked = masked;
490                 if (bootverbose)
491                         printf("lapic:");
492         } else {
493                 KASSERT(lapics[apic_id].la_present,
494                     ("%s: missing APIC %u", __func__, apic_id));
495                 lapics[apic_id].la_lvts[pin].lvt_masked = masked;
496                 lapics[apic_id].la_lvts[pin].lvt_active = 1;
497                 if (bootverbose)
498                         printf("lapic%u:", apic_id);
499         }
500         if (bootverbose)
501                 printf(" LINT%u %s\n", pin, masked ? "masked" : "unmasked");
502         return (0);
503 }
504
505 int
506 lapic_set_lvt_mode(u_int apic_id, u_int pin, u_int32_t mode)
507 {
508         struct lvt *lvt;
509
510         if (pin > LVT_MAX)
511                 return (EINVAL);
512         if (apic_id == APIC_ID_ALL) {
513                 lvt = &lvts[pin];
514                 if (bootverbose)
515                         printf("lapic:");
516         } else {
517                 KASSERT(lapics[apic_id].la_present,
518                     ("%s: missing APIC %u", __func__, apic_id));
519                 lvt = &lapics[apic_id].la_lvts[pin];
520                 lvt->lvt_active = 1;
521                 if (bootverbose)
522                         printf("lapic%u:", apic_id);
523         }
524         lvt->lvt_mode = mode;
525         switch (mode) {
526         case APIC_LVT_DM_NMI:
527         case APIC_LVT_DM_SMI:
528         case APIC_LVT_DM_INIT:
529         case APIC_LVT_DM_EXTINT:
530                 lvt->lvt_edgetrigger = 1;
531                 lvt->lvt_activehi = 1;
532                 if (mode == APIC_LVT_DM_EXTINT)
533                         lvt->lvt_masked = 1;
534                 else
535                         lvt->lvt_masked = 0;
536                 break;
537         default:
538                 panic("Unsupported delivery mode: 0x%x\n", mode);
539         }
540         if (bootverbose) {
541                 printf(" Routing ");
542                 switch (mode) {
543                 case APIC_LVT_DM_NMI:
544                         printf("NMI");
545                         break;
546                 case APIC_LVT_DM_SMI:
547                         printf("SMI");
548                         break;
549                 case APIC_LVT_DM_INIT:
550                         printf("INIT");
551                         break;
552                 case APIC_LVT_DM_EXTINT:
553                         printf("ExtINT");
554                         break;
555                 }
556                 printf(" -> LINT%u\n", pin);
557         }
558         return (0);
559 }
560
561 int
562 lapic_set_lvt_polarity(u_int apic_id, u_int pin, enum intr_polarity pol)
563 {
564
565         if (pin > LVT_MAX || pol == INTR_POLARITY_CONFORM)
566                 return (EINVAL);
567         if (apic_id == APIC_ID_ALL) {
568                 lvts[pin].lvt_activehi = (pol == INTR_POLARITY_HIGH);
569                 if (bootverbose)
570                         printf("lapic:");
571         } else {
572                 KASSERT(lapics[apic_id].la_present,
573                     ("%s: missing APIC %u", __func__, apic_id));
574                 lapics[apic_id].la_lvts[pin].lvt_active = 1;
575                 lapics[apic_id].la_lvts[pin].lvt_activehi =
576                     (pol == INTR_POLARITY_HIGH);
577                 if (bootverbose)
578                         printf("lapic%u:", apic_id);
579         }
580         if (bootverbose)
581                 printf(" LINT%u polarity: %s\n", pin,
582                     pol == INTR_POLARITY_HIGH ? "high" : "low");
583         return (0);
584 }
585
586 int
587 lapic_set_lvt_triggermode(u_int apic_id, u_int pin, enum intr_trigger trigger)
588 {
589
590         if (pin > LVT_MAX || trigger == INTR_TRIGGER_CONFORM)
591                 return (EINVAL);
592         if (apic_id == APIC_ID_ALL) {
593                 lvts[pin].lvt_edgetrigger = (trigger == INTR_TRIGGER_EDGE);
594                 if (bootverbose)
595                         printf("lapic:");
596         } else {
597                 KASSERT(lapics[apic_id].la_present,
598                     ("%s: missing APIC %u", __func__, apic_id));
599                 lapics[apic_id].la_lvts[pin].lvt_edgetrigger =
600                     (trigger == INTR_TRIGGER_EDGE);
601                 lapics[apic_id].la_lvts[pin].lvt_active = 1;
602                 if (bootverbose)
603                         printf("lapic%u:", apic_id);
604         }
605         if (bootverbose)
606                 printf(" LINT%u trigger: %s\n", pin,
607                     trigger == INTR_TRIGGER_EDGE ? "edge" : "level");
608         return (0);
609 }
610
611 /*
612  * Adjust the TPR of the current CPU so that it blocks all interrupts below
613  * the passed in vector.
614  */
615 void
616 lapic_set_tpr(u_int vector)
617 {
618 #ifdef CHEAP_TPR
619         lapic->tpr = vector;
620 #else
621         u_int32_t tpr;
622
623         tpr = lapic->tpr & ~APIC_TPR_PRIO;
624         tpr |= vector;
625         lapic->tpr = tpr;
626 #endif
627 }
628
629 void
630 lapic_eoi(void)
631 {
632
633         lapic->eoi = 0;
634 }
635
636 /*
637  * Read the contents of the error status register.  We have to write
638  * to the register first before reading from it.
639  */
640 u_int
641 lapic_error(void)
642 {
643
644         lapic->esr = 0;
645         return (lapic->esr);
646 }
647
648 void
649 lapic_handle_intr(int vector, struct trapframe *frame)
650 {
651         struct intsrc *isrc;
652
653         if (vector == -1)
654                 panic("Couldn't get vector from ISR!");
655         isrc = intr_lookup_source(apic_idt_to_irq(PCPU_GET(apic_id),
656             vector));
657         intr_execute_handlers(isrc, frame);
658 }
659
660 void
661 lapic_handle_timer(struct trapframe *frame)
662 {
663         struct lapic *la;
664
665         /* Send EOI first thing. */
666         lapic_eoi();
667
668 #if defined(SMP) && !defined(SCHED_ULE)
669         /*
670          * Don't do any accounting for the disabled HTT cores, since it
671          * will provide misleading numbers for the userland.
672          *
673          * No locking is necessary here, since even if we loose the race
674          * when hlt_cpus_mask changes it is not a big deal, really.
675          *
676          * Don't do that for ULE, since ULE doesn't consider hlt_cpus_mask
677          * and unlike other schedulers it actually schedules threads to
678          * those CPUs.
679          */
680         if ((hlt_cpus_mask & (1 << PCPU_GET(cpuid))) != 0)
681                 return;
682 #endif
683
684         /* Look up our local APIC structure for the tick counters. */
685         la = &lapics[PCPU_GET(apic_id)];
686         (*la->la_timer_count)++;
687         critical_enter();
688
689 #ifdef KDTRACE_HOOKS
690         /*
691          * If the DTrace hooks are configured and a callback function
692          * has been registered, then call it to process the high speed
693          * timers.
694          */
695         int cpu = PCPU_GET(cpuid);
696         if (lapic_cyclic_clock_func[cpu] != NULL)
697                 (*lapic_cyclic_clock_func[cpu])(frame);
698 #endif
699
700         /* Fire hardclock at hz. */
701         la->la_hard_ticks += hz;
702         if (la->la_hard_ticks >= lapic_timer_hz) {
703                 la->la_hard_ticks -= lapic_timer_hz;
704                 if (PCPU_GET(cpuid) == 0)
705                         hardclock(TRAPF_USERMODE(frame), TRAPF_PC(frame));
706                 else
707                         hardclock_cpu(TRAPF_USERMODE(frame));
708         }
709
710         /* Fire statclock at stathz. */
711         la->la_stat_ticks += stathz;
712         if (la->la_stat_ticks >= lapic_timer_hz) {
713                 la->la_stat_ticks -= lapic_timer_hz;
714                 statclock(TRAPF_USERMODE(frame));
715         }
716
717         /* Fire profclock at profhz, but only when needed. */
718         la->la_prof_ticks += profhz;
719         if (la->la_prof_ticks >= lapic_timer_hz) {
720                 la->la_prof_ticks -= lapic_timer_hz;
721                 if (profprocs != 0)
722                         profclock(TRAPF_USERMODE(frame), TRAPF_PC(frame));
723         }
724         critical_exit();
725 }
726
727 static void
728 lapic_timer_set_divisor(u_int divisor)
729 {
730
731         KASSERT(powerof2(divisor), ("lapic: invalid divisor %u", divisor));
732         KASSERT(ffs(divisor) <= sizeof(lapic_timer_divisors) /
733             sizeof(u_int32_t), ("lapic: invalid divisor %u", divisor));
734         lapic->dcr_timer = lapic_timer_divisors[ffs(divisor) - 1];
735 }
736
737 static void
738 lapic_timer_oneshot(u_int count)
739 {
740         u_int32_t value;
741
742         value = lapic->lvt_timer;
743         value &= ~APIC_LVTT_TM;
744         value |= APIC_LVTT_TM_ONE_SHOT;
745         lapic->lvt_timer = value;
746         lapic->icr_timer = count;
747 }
748
749 static void
750 lapic_timer_periodic(u_int count)
751 {
752         u_int32_t value;
753
754         value = lapic->lvt_timer;
755         value &= ~APIC_LVTT_TM;
756         value |= APIC_LVTT_TM_PERIODIC;
757         lapic->lvt_timer = value;
758         lapic->icr_timer = count;
759 }
760
761 static void
762 lapic_timer_enable_intr(void)
763 {
764         u_int32_t value;
765
766         value = lapic->lvt_timer;
767         value &= ~APIC_LVT_M;
768         lapic->lvt_timer = value;
769 }
770
771 u_int
772 apic_cpuid(u_int apic_id)
773 {
774 #ifdef SMP
775         return apic_cpuids[apic_id];
776 #else
777         return 0;
778 #endif
779 }
780
781 /* Request a free IDT vector to be used by the specified IRQ. */
782 u_int
783 apic_alloc_vector(u_int apic_id, u_int irq)
784 {
785         u_int vector;
786
787         KASSERT(irq < NUM_IO_INTS, ("Invalid IRQ %u", irq));
788
789         /*
790          * Search for a free vector.  Currently we just use a very simple
791          * algorithm to find the first free vector.
792          */
793         mtx_lock_spin(&icu_lock);
794         for (vector = 0; vector < APIC_NUM_IOINTS; vector++) {
795                 if (lapics[apic_id].la_ioint_irqs[vector] != -1)
796                         continue;
797                 lapics[apic_id].la_ioint_irqs[vector] = irq;
798                 mtx_unlock_spin(&icu_lock);
799                 return (vector + APIC_IO_INTS);
800         }
801         mtx_unlock_spin(&icu_lock);
802         return (0);
803 }
804
805 /*
806  * Request 'count' free contiguous IDT vectors to be used by 'count'
807  * IRQs.  'count' must be a power of two and the vectors will be
808  * aligned on a boundary of 'align'.  If the request cannot be
809  * satisfied, 0 is returned.
810  */
811 u_int
812 apic_alloc_vectors(u_int apic_id, u_int *irqs, u_int count, u_int align)
813 {
814         u_int first, run, vector;
815
816         KASSERT(powerof2(count), ("bad count"));
817         KASSERT(powerof2(align), ("bad align"));
818         KASSERT(align >= count, ("align < count"));
819 #ifdef INVARIANTS
820         for (run = 0; run < count; run++)
821                 KASSERT(irqs[run] < NUM_IO_INTS, ("Invalid IRQ %u at index %u",
822                     irqs[run], run));
823 #endif
824
825         /*
826          * Search for 'count' free vectors.  As with apic_alloc_vector(),
827          * this just uses a simple first fit algorithm.
828          */
829         run = 0;
830         first = 0;
831         mtx_lock_spin(&icu_lock);
832         for (vector = 0; vector < APIC_NUM_IOINTS; vector++) {
833
834                 /* Vector is in use, end run. */
835                 if (lapics[apic_id].la_ioint_irqs[vector] != -1) {
836                         run = 0;
837                         first = 0;
838                         continue;
839                 }
840
841                 /* Start a new run if run == 0 and vector is aligned. */
842                 if (run == 0) {
843                         if ((vector & (align - 1)) != 0)
844                                 continue;
845                         first = vector;
846                 }
847                 run++;
848
849                 /* Keep looping if the run isn't long enough yet. */
850                 if (run < count)
851                         continue;
852
853                 /* Found a run, assign IRQs and return the first vector. */
854                 for (vector = 0; vector < count; vector++)
855                         lapics[apic_id].la_ioint_irqs[first + vector] =
856                             irqs[vector];
857                 mtx_unlock_spin(&icu_lock);
858                 return (first + APIC_IO_INTS);
859         }
860         mtx_unlock_spin(&icu_lock);
861         printf("APIC: Couldn't find APIC vectors for %u IRQs\n", count);
862         return (0);
863 }
864
865 /*
866  * Enable a vector for a particular apic_id.  Since all lapics share idt
867  * entries and ioint_handlers this enables the vector on all lapics.  lapics
868  * which do not have the vector configured would report spurious interrupts
869  * should it fire.
870  */
871 void
872 apic_enable_vector(u_int apic_id, u_int vector)
873 {
874
875         KASSERT(vector != IDT_SYSCALL, ("Attempt to overwrite syscall entry"));
876         KASSERT(ioint_handlers[vector / 32] != NULL,
877             ("No ISR handler for vector %u", vector));
878         setidt(vector, ioint_handlers[vector / 32], SDT_SYS386IGT, SEL_KPL,
879             GSEL(GCODE_SEL, SEL_KPL));
880 }
881
882 void
883 apic_disable_vector(u_int apic_id, u_int vector)
884 {
885
886         KASSERT(vector != IDT_SYSCALL, ("Attempt to overwrite syscall entry"));
887         KASSERT(ioint_handlers[vector / 32] != NULL,
888             ("No ISR handler for vector %u", vector));
889 #ifdef notyet
890         /*
891          * We can not currently clear the idt entry because other cpus
892          * may have a valid vector at this offset.
893          */
894         setidt(vector, &IDTVEC(rsvd), SDT_SYS386TGT, SEL_KPL,
895             GSEL(GCODE_SEL, SEL_KPL));
896 #endif
897 }
898
899 /* Release an APIC vector when it's no longer in use. */
900 void
901 apic_free_vector(u_int apic_id, u_int vector, u_int irq)
902 {
903         struct thread *td;
904
905         KASSERT(vector >= APIC_IO_INTS && vector != IDT_SYSCALL &&
906             vector <= APIC_IO_INTS + APIC_NUM_IOINTS,
907             ("Vector %u does not map to an IRQ line", vector));
908         KASSERT(irq < NUM_IO_INTS, ("Invalid IRQ %u", irq));
909         KASSERT(lapics[apic_id].la_ioint_irqs[vector - APIC_IO_INTS] ==
910             irq, ("IRQ mismatch"));
911
912         /*
913          * Bind us to the cpu that owned the vector before freeing it so
914          * we don't lose an interrupt delivery race.
915          */
916         td = curthread;
917         thread_lock(td);
918         if (sched_is_bound(td))
919                 panic("apic_free_vector: Thread already bound.\n");
920         sched_bind(td, apic_cpuid(apic_id));
921         thread_unlock(td);
922         mtx_lock_spin(&icu_lock);
923         lapics[apic_id].la_ioint_irqs[vector - APIC_IO_INTS] = -1;
924         mtx_unlock_spin(&icu_lock);
925         thread_lock(td);
926         sched_unbind(td);
927         thread_unlock(td);
928
929 }
930
931 /* Map an IDT vector (APIC) to an IRQ (interrupt source). */
932 u_int
933 apic_idt_to_irq(u_int apic_id, u_int vector)
934 {
935         int irq;
936
937         KASSERT(vector >= APIC_IO_INTS && vector != IDT_SYSCALL &&
938             vector <= APIC_IO_INTS + APIC_NUM_IOINTS,
939             ("Vector %u does not map to an IRQ line", vector));
940         irq = lapics[apic_id].la_ioint_irqs[vector - APIC_IO_INTS];
941         if (irq < 0)
942                 irq = 0;
943         return (irq);
944 }
945
946 #ifdef DDB
947 /*
948  * Dump data about APIC IDT vector mappings.
949  */
950 DB_SHOW_COMMAND(apic, db_show_apic)
951 {
952         struct intsrc *isrc;
953         int i, verbose;
954         u_int apic_id;
955         u_int irq;
956
957         if (strcmp(modif, "vv") == 0)
958                 verbose = 2;
959         else if (strcmp(modif, "v") == 0)
960                 verbose = 1;
961         else
962                 verbose = 0;
963         for (apic_id = 0; apic_id <= MAX_APIC_ID; apic_id++) {
964                 if (lapics[apic_id].la_present == 0)
965                         continue;
966                 db_printf("Interrupts bound to lapic %u\n", apic_id);
967                 for (i = 0; i < APIC_NUM_IOINTS + 1 && !db_pager_quit; i++) {
968                         irq = lapics[apic_id].la_ioint_irqs[i];
969                         if (irq == -1 || irq == IRQ_SYSCALL)
970                                 continue;
971                         db_printf("vec 0x%2x -> ", i + APIC_IO_INTS);
972                         if (irq == IRQ_TIMER)
973                                 db_printf("lapic timer\n");
974                         else if (irq < NUM_IO_INTS) {
975                                 isrc = intr_lookup_source(irq);
976                                 if (isrc == NULL || verbose == 0)
977                                         db_printf("IRQ %u\n", irq);
978                                 else
979                                         db_dump_intr_event(isrc->is_event,
980                                             verbose == 2);
981                         } else
982                                 db_printf("IRQ %u ???\n", irq);
983                 }
984         }
985 }
986
987 static void
988 dump_mask(const char *prefix, uint32_t v, int base)
989 {
990         int i, first;
991
992         first = 1;
993         for (i = 0; i < 32; i++)
994                 if (v & (1 << i)) {
995                         if (first) {
996                                 db_printf("%s:", prefix);
997                                 first = 0;
998                         }
999                         db_printf(" %02x", base + i);
1000                 }
1001         if (!first)
1002                 db_printf("\n");
1003 }
1004
1005 /* Show info from the lapic regs for this CPU. */
1006 DB_SHOW_COMMAND(lapic, db_show_lapic)
1007 {
1008         uint32_t v;
1009
1010         db_printf("lapic ID = %d\n", lapic_id());
1011         v = lapic->version;
1012         db_printf("version  = %d.%d\n", (v & APIC_VER_VERSION) >> 4,
1013             v & 0xf);
1014         db_printf("max LVT  = %d\n", (v & APIC_VER_MAXLVT) >> MAXLVTSHIFT);
1015         v = lapic->svr;
1016         db_printf("SVR      = %02x (%s)\n", v & APIC_SVR_VECTOR,
1017             v & APIC_SVR_ENABLE ? "enabled" : "disabled");
1018         db_printf("TPR      = %02x\n", lapic->tpr);
1019
1020 #define dump_field(prefix, index)                                       \
1021         dump_mask(__XSTRING(prefix ## index), lapic->prefix ## index,   \
1022             index * 32)
1023
1024         db_printf("In-service Interrupts:\n");
1025         dump_field(isr, 0);
1026         dump_field(isr, 1);
1027         dump_field(isr, 2);
1028         dump_field(isr, 3);
1029         dump_field(isr, 4);
1030         dump_field(isr, 5);
1031         dump_field(isr, 6);
1032         dump_field(isr, 7);
1033
1034         db_printf("TMR Interrupts:\n");
1035         dump_field(tmr, 0);
1036         dump_field(tmr, 1);
1037         dump_field(tmr, 2);
1038         dump_field(tmr, 3);
1039         dump_field(tmr, 4);
1040         dump_field(tmr, 5);
1041         dump_field(tmr, 6);
1042         dump_field(tmr, 7);
1043
1044         db_printf("IRR Interrupts:\n");
1045         dump_field(irr, 0);
1046         dump_field(irr, 1);
1047         dump_field(irr, 2);
1048         dump_field(irr, 3);
1049         dump_field(irr, 4);
1050         dump_field(irr, 5);
1051         dump_field(irr, 6);
1052         dump_field(irr, 7);
1053
1054 #undef dump_field
1055 }
1056 #endif
1057
1058 /*
1059  * APIC probing support code.  This includes code to manage enumerators.
1060  */
1061
1062 static SLIST_HEAD(, apic_enumerator) enumerators =
1063         SLIST_HEAD_INITIALIZER(enumerators);
1064 static struct apic_enumerator *best_enum;
1065
1066 void
1067 apic_register_enumerator(struct apic_enumerator *enumerator)
1068 {
1069 #ifdef INVARIANTS
1070         struct apic_enumerator *apic_enum;
1071
1072         SLIST_FOREACH(apic_enum, &enumerators, apic_next) {
1073                 if (apic_enum == enumerator)
1074                         panic("%s: Duplicate register of %s", __func__,
1075                             enumerator->apic_name);
1076         }
1077 #endif
1078         SLIST_INSERT_HEAD(&enumerators, enumerator, apic_next);
1079 }
1080
1081 /*
1082  * Probe the APIC enumerators, enumerate CPUs, and initialize the
1083  * local APIC.
1084  */
1085 static void
1086 apic_init(void *dummy __unused)
1087 {
1088         struct apic_enumerator *enumerator;
1089         uint64_t apic_base;
1090         int retval, best;
1091
1092         /* We only support built in local APICs. */
1093         if (!(cpu_feature & CPUID_APIC))
1094                 return;
1095
1096         /* Don't probe if APIC mode is disabled. */
1097         if (resource_disabled("apic", 0))
1098                 return;
1099
1100         /* First, probe all the enumerators to find the best match. */
1101         best_enum = NULL;
1102         best = 0;
1103         SLIST_FOREACH(enumerator, &enumerators, apic_next) {
1104                 retval = enumerator->apic_probe();
1105                 if (retval > 0)
1106                         continue;
1107                 if (best_enum == NULL || best < retval) {
1108                         best_enum = enumerator;
1109                         best = retval;
1110                 }
1111         }
1112         if (best_enum == NULL) {
1113                 if (bootverbose)
1114                         printf("APIC: Could not find any APICs.\n");
1115                 return;
1116         }
1117
1118         if (bootverbose)
1119                 printf("APIC: Using the %s enumerator.\n",
1120                     best_enum->apic_name);
1121
1122         /*
1123          * To work around an errata, we disable the local APIC on some
1124          * CPUs during early startup.  We need to turn the local APIC back
1125          * on on such CPUs now.
1126          */
1127         if (cpu == CPU_686 && cpu_vendor_id == CPU_VENDOR_INTEL &&
1128             (cpu_id & 0xff0) == 0x610) {
1129                 apic_base = rdmsr(MSR_APICBASE);
1130                 apic_base |= APICBASE_ENABLED;
1131                 wrmsr(MSR_APICBASE, apic_base);
1132         }
1133
1134         /* Second, probe the CPU's in the system. */
1135         retval = best_enum->apic_probe_cpus();
1136         if (retval != 0)
1137                 printf("%s: Failed to probe CPUs: returned %d\n",
1138                     best_enum->apic_name, retval);
1139
1140         /* Third, initialize the local APIC. */
1141         retval = best_enum->apic_setup_local();
1142         if (retval != 0)
1143                 printf("%s: Failed to setup the local APIC: returned %d\n",
1144                     best_enum->apic_name, retval);
1145 }
1146 SYSINIT(apic_init, SI_SUB_CPU, SI_ORDER_SECOND, apic_init, NULL);
1147
1148 /*
1149  * Setup the I/O APICs.
1150  */
1151 static void
1152 apic_setup_io(void *dummy __unused)
1153 {
1154         int retval;
1155
1156         if (best_enum == NULL)
1157                 return;
1158         retval = best_enum->apic_setup_io();
1159         if (retval != 0)
1160                 printf("%s: Failed to setup I/O APICs: returned %d\n",
1161                     best_enum->apic_name, retval);
1162
1163 #ifdef XEN
1164         return;
1165 #endif
1166         /*
1167          * Finish setting up the local APIC on the BSP once we know how to
1168          * properly program the LINT pins.
1169          */
1170         lapic_setup(1);
1171         intr_register_pic(&lapic_pic);
1172         if (bootverbose)
1173                 lapic_dump("BSP");
1174
1175         /* Enable the MSI "pic". */
1176         msi_init();
1177 }
1178 SYSINIT(apic_setup_io, SI_SUB_INTR, SI_ORDER_SECOND, apic_setup_io, NULL);
1179
1180 #ifdef SMP
1181 /*
1182  * Inter Processor Interrupt functions.  The lapic_ipi_*() functions are
1183  * private to the sys/i386 code.  The public interface for the rest of the
1184  * kernel is defined in mp_machdep.c.
1185  */
1186 int
1187 lapic_ipi_wait(int delay)
1188 {
1189         int x, incr;
1190
1191         /*
1192          * Wait delay loops for IPI to be sent.  This is highly bogus
1193          * since this is sensitive to CPU clock speed.  If delay is
1194          * -1, we wait forever.
1195          */
1196         if (delay == -1) {
1197                 incr = 0;
1198                 delay = 1;
1199         } else
1200                 incr = 1;
1201         for (x = 0; x < delay; x += incr) {
1202                 if ((lapic->icr_lo & APIC_DELSTAT_MASK) == APIC_DELSTAT_IDLE)
1203                         return (1);
1204                 ia32_pause();
1205         }
1206         return (0);
1207 }
1208
1209 void
1210 lapic_ipi_raw(register_t icrlo, u_int dest)
1211 {
1212         register_t value, eflags;
1213
1214         /* XXX: Need more sanity checking of icrlo? */
1215         KASSERT(lapic != NULL, ("%s called too early", __func__));
1216         KASSERT((dest & ~(APIC_ID_MASK >> APIC_ID_SHIFT)) == 0,
1217             ("%s: invalid dest field", __func__));
1218         KASSERT((icrlo & APIC_ICRLO_RESV_MASK) == 0,
1219             ("%s: reserved bits set in ICR LO register", __func__));
1220
1221         /* Set destination in ICR HI register if it is being used. */
1222         eflags = intr_disable();
1223         if ((icrlo & APIC_DEST_MASK) == APIC_DEST_DESTFLD) {
1224                 value = lapic->icr_hi;
1225                 value &= ~APIC_ID_MASK;
1226                 value |= dest << APIC_ID_SHIFT;
1227                 lapic->icr_hi = value;
1228         }
1229
1230         /* Program the contents of the IPI and dispatch it. */
1231         value = lapic->icr_lo;
1232         value &= APIC_ICRLO_RESV_MASK;
1233         value |= icrlo;
1234         lapic->icr_lo = value;
1235         intr_restore(eflags);
1236 }
1237
1238 #define BEFORE_SPIN     1000000
1239 #ifdef DETECT_DEADLOCK
1240 #define AFTER_SPIN      1000
1241 #endif
1242
1243 void
1244 lapic_ipi_vectored(u_int vector, int dest)
1245 {
1246         register_t icrlo, destfield;
1247
1248         KASSERT((vector & ~APIC_VECTOR_MASK) == 0,
1249             ("%s: invalid vector %d", __func__, vector));
1250
1251         icrlo = vector | APIC_DELMODE_FIXED | APIC_DESTMODE_PHY |
1252             APIC_LEVEL_DEASSERT | APIC_TRIGMOD_EDGE;
1253         destfield = 0;
1254         switch (dest) {
1255         case APIC_IPI_DEST_SELF:
1256                 icrlo |= APIC_DEST_SELF;
1257                 break;
1258         case APIC_IPI_DEST_ALL:
1259                 icrlo |= APIC_DEST_ALLISELF;
1260                 break;
1261         case APIC_IPI_DEST_OTHERS:
1262                 icrlo |= APIC_DEST_ALLESELF;
1263                 break;
1264         default:
1265                 KASSERT((dest & ~(APIC_ID_MASK >> APIC_ID_SHIFT)) == 0,
1266                     ("%s: invalid destination 0x%x", __func__, dest));
1267                 destfield = dest;
1268         }
1269
1270         /* Wait for an earlier IPI to finish. */
1271         if (!lapic_ipi_wait(BEFORE_SPIN)) {
1272                 if (panicstr != NULL)
1273                         return;
1274                 else
1275                         panic("APIC: Previous IPI is stuck");
1276         }
1277
1278         lapic_ipi_raw(icrlo, destfield);
1279
1280 #ifdef DETECT_DEADLOCK
1281         /* Wait for IPI to be delivered. */
1282         if (!lapic_ipi_wait(AFTER_SPIN)) {
1283 #ifdef needsattention
1284                 /*
1285                  * XXX FIXME:
1286                  *
1287                  * The above function waits for the message to actually be
1288                  * delivered.  It breaks out after an arbitrary timeout
1289                  * since the message should eventually be delivered (at
1290                  * least in theory) and that if it wasn't we would catch
1291                  * the failure with the check above when the next IPI is
1292                  * sent.
1293                  *
1294                  * We could skip this wait entirely, EXCEPT it probably
1295                  * protects us from other routines that assume that the
1296                  * message was delivered and acted upon when this function
1297                  * returns.
1298                  */
1299                 printf("APIC: IPI might be stuck\n");
1300 #else /* !needsattention */
1301                 /* Wait until mesage is sent without a timeout. */
1302                 while (lapic->icr_lo & APIC_DELSTAT_PEND)
1303                         ia32_pause();
1304 #endif /* needsattention */
1305         }
1306 #endif /* DETECT_DEADLOCK */
1307 }
1308 #endif /* SMP */