]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/amd64/amd64/local_apic.c
MFC r196196:
[FreeBSD/stable/8.git] / sys / amd64 / amd64 / 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_SYSIGT, SEL_KPL, 0);
217
218         /* Perform basic initialization of the BSP's local APIC. */
219         lapic_enable();
220
221         /* Set BSP's per-CPU local APIC ID. */
222         PCPU_SET(apic_id, lapic_id());
223
224         /* Local APIC timer interrupt. */
225         setidt(APIC_TIMER_INT, IDTVEC(timerint), SDT_SYSIGT, SEL_KPL, 0);
226
227         /* XXX: error/thermal interrupts */
228 }
229
230 /*
231  * Create a local APIC instance.
232  */
233 void
234 lapic_create(u_int apic_id, int boot_cpu)
235 {
236         int i;
237
238         if (apic_id > MAX_APIC_ID) {
239                 printf("APIC: Ignoring local APIC with ID %d\n", apic_id);
240                 if (boot_cpu)
241                         panic("Can't ignore BSP");
242                 return;
243         }
244         KASSERT(!lapics[apic_id].la_present, ("duplicate local APIC %u",
245             apic_id));
246
247         /*
248          * Assume no local LVT overrides and a cluster of 0 and
249          * intra-cluster ID of 0.
250          */
251         lapics[apic_id].la_present = 1;
252         lapics[apic_id].la_id = apic_id;
253         for (i = 0; i < LVT_MAX; i++) {
254                 lapics[apic_id].la_lvts[i] = lvts[i];
255                 lapics[apic_id].la_lvts[i].lvt_active = 0;
256         }
257         for (i = 0; i <= APIC_NUM_IOINTS; i++)
258             lapics[apic_id].la_ioint_irqs[i] = -1;
259         lapics[apic_id].la_ioint_irqs[IDT_SYSCALL - APIC_IO_INTS] = IRQ_SYSCALL;
260         lapics[apic_id].la_ioint_irqs[APIC_TIMER_INT - APIC_IO_INTS] =
261             IRQ_TIMER;
262
263 #ifdef SMP
264         cpu_add(apic_id, boot_cpu);
265 #endif
266 }
267
268 /*
269  * Dump contents of local APIC registers
270  */
271 void
272 lapic_dump(const char* str)
273 {
274
275         printf("cpu%d %s:\n", PCPU_GET(cpuid), str);
276         printf("     ID: 0x%08x   VER: 0x%08x LDR: 0x%08x DFR: 0x%08x\n",
277             lapic->id, lapic->version, lapic->ldr, lapic->dfr);
278         printf("  lint0: 0x%08x lint1: 0x%08x TPR: 0x%08x SVR: 0x%08x\n",
279             lapic->lvt_lint0, lapic->lvt_lint1, lapic->tpr, lapic->svr);
280         printf("  timer: 0x%08x therm: 0x%08x err: 0x%08x pcm: 0x%08x\n",
281             lapic->lvt_timer, lapic->lvt_thermal, lapic->lvt_error,
282             lapic->lvt_pcint);
283 }
284
285 void
286 lapic_setup(int boot)
287 {
288         struct lapic *la;
289         u_int32_t maxlvt;
290         register_t eflags;
291         char buf[MAXCOMLEN + 1];
292
293         la = &lapics[lapic_id()];
294         KASSERT(la->la_present, ("missing APIC structure"));
295         eflags = intr_disable();
296         maxlvt = (lapic->version & APIC_VER_MAXLVT) >> MAXLVTSHIFT;
297
298         /* Initialize the TPR to allow all interrupts. */
299         lapic_set_tpr(0);
300
301         /* Setup spurious vector and enable the local APIC. */
302         lapic_enable();
303
304         /* Program LINT[01] LVT entries. */
305         lapic->lvt_lint0 = lvt_mode(la, LVT_LINT0, lapic->lvt_lint0);
306         lapic->lvt_lint1 = lvt_mode(la, LVT_LINT1, lapic->lvt_lint1);
307
308 #ifdef  HWPMC_HOOKS
309         /* Program the PMC LVT entry if present. */
310         if (maxlvt >= LVT_PMC)
311                 lapic->lvt_pcint = lvt_mode(la, LVT_PMC, lapic->lvt_pcint);
312 #endif
313
314         /* Program timer LVT and setup handler. */
315         lapic->lvt_timer = lvt_mode(la, LVT_TIMER, lapic->lvt_timer);
316         if (boot) {
317                 snprintf(buf, sizeof(buf), "cpu%d: timer", PCPU_GET(cpuid));
318                 intrcnt_add(buf, &la->la_timer_count);
319         }
320
321         /* We don't setup the timer during boot on the BSP until later. */
322         if (!(boot && PCPU_GET(cpuid) == 0) && lapic_timer_hz != 0) {
323                 KASSERT(lapic_timer_period != 0, ("lapic%u: zero divisor",
324                     lapic_id()));
325                 lapic_timer_set_divisor(lapic_timer_divisor);
326                 lapic_timer_periodic(lapic_timer_period);
327                 lapic_timer_enable_intr();
328         }
329
330         /* XXX: Error and thermal LVTs */
331
332         intr_restore(eflags);
333 }
334
335 /*
336  * Called by cpu_initclocks() on the BSP to setup the local APIC timer so
337  * that it can drive hardclock, statclock, and profclock.  This function
338  * returns true if it is able to use the local APIC timer to drive the
339  * clocks and false if it is not able.
340  */
341 int
342 lapic_setup_clock(void)
343 {
344         u_long value;
345         int i;
346
347         /* Can't drive the timer without a local APIC. */
348         if (lapic == NULL)
349                 return (0);
350
351         if (resource_int_value("apic", 0, "clock", &i) == 0 && i == 0)
352                 return (0);
353
354         /* Start off with a divisor of 2 (power on reset default). */
355         lapic_timer_divisor = 2;
356
357         /* Try to calibrate the local APIC timer. */
358         do {
359                 lapic_timer_set_divisor(lapic_timer_divisor);
360                 lapic_timer_oneshot(APIC_TIMER_MAX_COUNT);
361                 DELAY(2000000);
362                 value = APIC_TIMER_MAX_COUNT - lapic->ccr_timer;
363                 if (value != APIC_TIMER_MAX_COUNT)
364                         break;
365                 lapic_timer_divisor <<= 1;
366         } while (lapic_timer_divisor <= 128);
367         if (lapic_timer_divisor > 128)
368                 panic("lapic: Divisor too big");
369         value /= 2;
370         if (bootverbose)
371                 printf("lapic: Divisor %lu, Frequency %lu hz\n",
372                     lapic_timer_divisor, value);
373
374         /*
375          * We want to run stathz in the neighborhood of 128hz.  We would
376          * like profhz to run as often as possible, so we let it run on
377          * each clock tick.  We try to honor the requested 'hz' value as
378          * much as possible.
379          *
380          * If 'hz' is above 1500, then we just let the lapic timer
381          * (and profhz) run at hz.  If 'hz' is below 1500 but above
382          * 750, then we let the lapic timer run at 2 * 'hz'.  If 'hz'
383          * is below 750 then we let the lapic timer run at 4 * 'hz'.
384          */
385         if (hz >= 1500)
386                 lapic_timer_hz = hz;
387         else if (hz >= 750)
388                 lapic_timer_hz = hz * 2;
389         else
390                 lapic_timer_hz = hz * 4;
391         if (lapic_timer_hz < 128)
392                 stathz = lapic_timer_hz;
393         else
394                 stathz = lapic_timer_hz / (lapic_timer_hz / 128);
395         profhz = lapic_timer_hz;
396         lapic_timer_period = value / lapic_timer_hz;
397
398         /*
399          * Start up the timer on the BSP.  The APs will kick off their
400          * timer during lapic_setup().
401          */
402         lapic_timer_periodic(lapic_timer_period);
403         lapic_timer_enable_intr();
404         return (1);
405 }
406
407 void
408 lapic_disable(void)
409 {
410         uint32_t value;
411
412         /* Software disable the local APIC. */
413         value = lapic->svr;
414         value &= ~APIC_SVR_SWEN;
415         lapic->svr = value;
416 }
417
418 static void
419 lapic_enable(void)
420 {
421         u_int32_t value;
422
423         /* Program the spurious vector to enable the local APIC. */
424         value = lapic->svr;
425         value &= ~(APIC_SVR_VECTOR | APIC_SVR_FOCUS);
426         value |= (APIC_SVR_FEN | APIC_SVR_SWEN | APIC_SPURIOUS_INT);
427         lapic->svr = value;
428 }
429
430 /* Reset the local APIC on the BSP during resume. */
431 static void
432 lapic_resume(struct pic *pic)
433 {
434
435         lapic_setup(0);
436 }
437
438 int
439 lapic_id(void)
440 {
441
442         KASSERT(lapic != NULL, ("local APIC is not mapped"));
443         return (lapic->id >> APIC_ID_SHIFT);
444 }
445
446 int
447 lapic_intr_pending(u_int vector)
448 {
449         volatile u_int32_t *irr;
450
451         /*
452          * The IRR registers are an array of 128-bit registers each of
453          * which only describes 32 interrupts in the low 32 bits..  Thus,
454          * we divide the vector by 32 to get the 128-bit index.  We then
455          * multiply that index by 4 to get the equivalent index from
456          * treating the IRR as an array of 32-bit registers.  Finally, we
457          * modulus the vector by 32 to determine the individual bit to
458          * test.
459          */
460         irr = &lapic->irr0;
461         return (irr[(vector / 32) * 4] & 1 << (vector % 32));
462 }
463
464 void
465 lapic_set_logical_id(u_int apic_id, u_int cluster, u_int cluster_id)
466 {
467         struct lapic *la;
468
469         KASSERT(lapics[apic_id].la_present, ("%s: APIC %u doesn't exist",
470             __func__, apic_id));
471         KASSERT(cluster <= APIC_MAX_CLUSTER, ("%s: cluster %u too big",
472             __func__, cluster));
473         KASSERT(cluster_id <= APIC_MAX_INTRACLUSTER_ID,
474             ("%s: intra cluster id %u too big", __func__, cluster_id));
475         la = &lapics[apic_id];
476         la->la_cluster = cluster;
477         la->la_cluster_id = cluster_id;
478 }
479
480 int
481 lapic_set_lvt_mask(u_int apic_id, u_int pin, u_char masked)
482 {
483
484         if (pin > LVT_MAX)
485                 return (EINVAL);
486         if (apic_id == APIC_ID_ALL) {
487                 lvts[pin].lvt_masked = masked;
488                 if (bootverbose)
489                         printf("lapic:");
490         } else {
491                 KASSERT(lapics[apic_id].la_present,
492                     ("%s: missing APIC %u", __func__, apic_id));
493                 lapics[apic_id].la_lvts[pin].lvt_masked = masked;
494                 lapics[apic_id].la_lvts[pin].lvt_active = 1;
495                 if (bootverbose)
496                         printf("lapic%u:", apic_id);
497         }
498         if (bootverbose)
499                 printf(" LINT%u %s\n", pin, masked ? "masked" : "unmasked");
500         return (0);
501 }
502
503 int
504 lapic_set_lvt_mode(u_int apic_id, u_int pin, u_int32_t mode)
505 {
506         struct lvt *lvt;
507
508         if (pin > LVT_MAX)
509                 return (EINVAL);
510         if (apic_id == APIC_ID_ALL) {
511                 lvt = &lvts[pin];
512                 if (bootverbose)
513                         printf("lapic:");
514         } else {
515                 KASSERT(lapics[apic_id].la_present,
516                     ("%s: missing APIC %u", __func__, apic_id));
517                 lvt = &lapics[apic_id].la_lvts[pin];
518                 lvt->lvt_active = 1;
519                 if (bootverbose)
520                         printf("lapic%u:", apic_id);
521         }
522         lvt->lvt_mode = mode;
523         switch (mode) {
524         case APIC_LVT_DM_NMI:
525         case APIC_LVT_DM_SMI:
526         case APIC_LVT_DM_INIT:
527         case APIC_LVT_DM_EXTINT:
528                 lvt->lvt_edgetrigger = 1;
529                 lvt->lvt_activehi = 1;
530                 if (mode == APIC_LVT_DM_EXTINT)
531                         lvt->lvt_masked = 1;
532                 else
533                         lvt->lvt_masked = 0;
534                 break;
535         default:
536                 panic("Unsupported delivery mode: 0x%x\n", mode);
537         }
538         if (bootverbose) {
539                 printf(" Routing ");
540                 switch (mode) {
541                 case APIC_LVT_DM_NMI:
542                         printf("NMI");
543                         break;
544                 case APIC_LVT_DM_SMI:
545                         printf("SMI");
546                         break;
547                 case APIC_LVT_DM_INIT:
548                         printf("INIT");
549                         break;
550                 case APIC_LVT_DM_EXTINT:
551                         printf("ExtINT");
552                         break;
553                 }
554                 printf(" -> LINT%u\n", pin);
555         }
556         return (0);
557 }
558
559 int
560 lapic_set_lvt_polarity(u_int apic_id, u_int pin, enum intr_polarity pol)
561 {
562
563         if (pin > LVT_MAX || pol == INTR_POLARITY_CONFORM)
564                 return (EINVAL);
565         if (apic_id == APIC_ID_ALL) {
566                 lvts[pin].lvt_activehi = (pol == INTR_POLARITY_HIGH);
567                 if (bootverbose)
568                         printf("lapic:");
569         } else {
570                 KASSERT(lapics[apic_id].la_present,
571                     ("%s: missing APIC %u", __func__, apic_id));
572                 lapics[apic_id].la_lvts[pin].lvt_active = 1;
573                 lapics[apic_id].la_lvts[pin].lvt_activehi =
574                     (pol == INTR_POLARITY_HIGH);
575                 if (bootverbose)
576                         printf("lapic%u:", apic_id);
577         }
578         if (bootverbose)
579                 printf(" LINT%u polarity: %s\n", pin,
580                     pol == INTR_POLARITY_HIGH ? "high" : "low");
581         return (0);
582 }
583
584 int
585 lapic_set_lvt_triggermode(u_int apic_id, u_int pin, enum intr_trigger trigger)
586 {
587
588         if (pin > LVT_MAX || trigger == INTR_TRIGGER_CONFORM)
589                 return (EINVAL);
590         if (apic_id == APIC_ID_ALL) {
591                 lvts[pin].lvt_edgetrigger = (trigger == INTR_TRIGGER_EDGE);
592                 if (bootverbose)
593                         printf("lapic:");
594         } else {
595                 KASSERT(lapics[apic_id].la_present,
596                     ("%s: missing APIC %u", __func__, apic_id));
597                 lapics[apic_id].la_lvts[pin].lvt_edgetrigger =
598                     (trigger == INTR_TRIGGER_EDGE);
599                 lapics[apic_id].la_lvts[pin].lvt_active = 1;
600                 if (bootverbose)
601                         printf("lapic%u:", apic_id);
602         }
603         if (bootverbose)
604                 printf(" LINT%u trigger: %s\n", pin,
605                     trigger == INTR_TRIGGER_EDGE ? "edge" : "level");
606         return (0);
607 }
608
609 /*
610  * Adjust the TPR of the current CPU so that it blocks all interrupts below
611  * the passed in vector.
612  */
613 void
614 lapic_set_tpr(u_int vector)
615 {
616 #ifdef CHEAP_TPR
617         lapic->tpr = vector;
618 #else
619         u_int32_t tpr;
620
621         tpr = lapic->tpr & ~APIC_TPR_PRIO;
622         tpr |= vector;
623         lapic->tpr = tpr;
624 #endif
625 }
626
627 void
628 lapic_eoi(void)
629 {
630
631         lapic->eoi = 0;
632 }
633
634 /*
635  * Read the contents of the error status register.  We have to write
636  * to the register first before reading from it.
637  */
638 u_int
639 lapic_error(void)
640 {
641
642         lapic->esr = 0;
643         return (lapic->esr);
644 }
645
646 void
647 lapic_handle_intr(int vector, struct trapframe *frame)
648 {
649         struct intsrc *isrc;
650
651         if (vector == -1)
652                 panic("Couldn't get vector from ISR!");
653         isrc = intr_lookup_source(apic_idt_to_irq(PCPU_GET(apic_id),
654             vector));
655         intr_execute_handlers(isrc, frame);
656 }
657
658 void
659 lapic_handle_timer(struct trapframe *frame)
660 {
661         struct lapic *la;
662
663         /* Send EOI first thing. */
664         lapic_eoi();
665
666 #if defined(SMP) && !defined(SCHED_ULE)
667         /*
668          * Don't do any accounting for the disabled HTT cores, since it
669          * will provide misleading numbers for the userland.
670          *
671          * No locking is necessary here, since even if we loose the race
672          * when hlt_cpus_mask changes it is not a big deal, really.
673          *
674          * Don't do that for ULE, since ULE doesn't consider hlt_cpus_mask
675          * and unlike other schedulers it actually schedules threads to
676          * those CPUs.
677          */
678         if ((hlt_cpus_mask & (1 << PCPU_GET(cpuid))) != 0)
679                 return;
680 #endif
681
682         /* Look up our local APIC structure for the tick counters. */
683         la = &lapics[PCPU_GET(apic_id)];
684         (*la->la_timer_count)++;
685         critical_enter();
686
687 #ifdef KDTRACE_HOOKS
688         /*
689          * If the DTrace hooks are configured and a callback function
690          * has been registered, then call it to process the high speed
691          * timers.
692          */
693         int cpu = PCPU_GET(cpuid);
694         if (lapic_cyclic_clock_func[cpu] != NULL)
695                 (*lapic_cyclic_clock_func[cpu])(frame);
696 #endif
697
698         /* Fire hardclock at hz. */
699         la->la_hard_ticks += hz;
700         if (la->la_hard_ticks >= lapic_timer_hz) {
701                 la->la_hard_ticks -= lapic_timer_hz;
702                 if (PCPU_GET(cpuid) == 0)
703                         hardclock(TRAPF_USERMODE(frame), TRAPF_PC(frame));
704                 else
705                         hardclock_cpu(TRAPF_USERMODE(frame));
706         }
707
708         /* Fire statclock at stathz. */
709         la->la_stat_ticks += stathz;
710         if (la->la_stat_ticks >= lapic_timer_hz) {
711                 la->la_stat_ticks -= lapic_timer_hz;
712                 statclock(TRAPF_USERMODE(frame));
713         }
714
715         /* Fire profclock at profhz, but only when needed. */
716         la->la_prof_ticks += profhz;
717         if (la->la_prof_ticks >= lapic_timer_hz) {
718                 la->la_prof_ticks -= lapic_timer_hz;
719                 if (profprocs != 0)
720                         profclock(TRAPF_USERMODE(frame), TRAPF_PC(frame));
721         }
722         critical_exit();
723 }
724
725 static void
726 lapic_timer_set_divisor(u_int divisor)
727 {
728
729         KASSERT(powerof2(divisor), ("lapic: invalid divisor %u", divisor));
730         KASSERT(ffs(divisor) <= sizeof(lapic_timer_divisors) /
731             sizeof(u_int32_t), ("lapic: invalid divisor %u", divisor));
732         lapic->dcr_timer = lapic_timer_divisors[ffs(divisor) - 1];
733 }
734
735 static void
736 lapic_timer_oneshot(u_int count)
737 {
738         u_int32_t value;
739
740         value = lapic->lvt_timer;
741         value &= ~APIC_LVTT_TM;
742         value |= APIC_LVTT_TM_ONE_SHOT;
743         lapic->lvt_timer = value;
744         lapic->icr_timer = count;
745 }
746
747 static void
748 lapic_timer_periodic(u_int count)
749 {
750         u_int32_t value;
751
752         value = lapic->lvt_timer;
753         value &= ~APIC_LVTT_TM;
754         value |= APIC_LVTT_TM_PERIODIC;
755         lapic->lvt_timer = value;
756         lapic->icr_timer = count;
757 }
758
759 static void
760 lapic_timer_enable_intr(void)
761 {
762         u_int32_t value;
763
764         value = lapic->lvt_timer;
765         value &= ~APIC_LVT_M;
766         lapic->lvt_timer = value;
767 }
768
769 u_int
770 apic_cpuid(u_int apic_id)
771 {
772 #ifdef SMP
773         return apic_cpuids[apic_id];
774 #else
775         return 0;
776 #endif
777 }
778
779 /* Request a free IDT vector to be used by the specified IRQ. */
780 u_int
781 apic_alloc_vector(u_int apic_id, u_int irq)
782 {
783         u_int vector;
784
785         KASSERT(irq < NUM_IO_INTS, ("Invalid IRQ %u", irq));
786
787         /*
788          * Search for a free vector.  Currently we just use a very simple
789          * algorithm to find the first free vector.
790          */
791         mtx_lock_spin(&icu_lock);
792         for (vector = 0; vector < APIC_NUM_IOINTS; vector++) {
793                 if (lapics[apic_id].la_ioint_irqs[vector] != -1)
794                         continue;
795                 lapics[apic_id].la_ioint_irqs[vector] = irq;
796                 mtx_unlock_spin(&icu_lock);
797                 return (vector + APIC_IO_INTS);
798         }
799         mtx_unlock_spin(&icu_lock);
800         return (0);
801 }
802
803 /*
804  * Request 'count' free contiguous IDT vectors to be used by 'count'
805  * IRQs.  'count' must be a power of two and the vectors will be
806  * aligned on a boundary of 'align'.  If the request cannot be
807  * satisfied, 0 is returned.
808  */
809 u_int
810 apic_alloc_vectors(u_int apic_id, u_int *irqs, u_int count, u_int align)
811 {
812         u_int first, run, vector;
813
814         KASSERT(powerof2(count), ("bad count"));
815         KASSERT(powerof2(align), ("bad align"));
816         KASSERT(align >= count, ("align < count"));
817 #ifdef INVARIANTS
818         for (run = 0; run < count; run++)
819                 KASSERT(irqs[run] < NUM_IO_INTS, ("Invalid IRQ %u at index %u",
820                     irqs[run], run));
821 #endif
822
823         /*
824          * Search for 'count' free vectors.  As with apic_alloc_vector(),
825          * this just uses a simple first fit algorithm.
826          */
827         run = 0;
828         first = 0;
829         mtx_lock_spin(&icu_lock);
830         for (vector = 0; vector < APIC_NUM_IOINTS; vector++) {
831
832                 /* Vector is in use, end run. */
833                 if (lapics[apic_id].la_ioint_irqs[vector] != -1) {
834                         run = 0;
835                         first = 0;
836                         continue;
837                 }
838
839                 /* Start a new run if run == 0 and vector is aligned. */
840                 if (run == 0) {
841                         if ((vector & (align - 1)) != 0)
842                                 continue;
843                         first = vector;
844                 }
845                 run++;
846
847                 /* Keep looping if the run isn't long enough yet. */
848                 if (run < count)
849                         continue;
850
851                 /* Found a run, assign IRQs and return the first vector. */
852                 for (vector = 0; vector < count; vector++)
853                         lapics[apic_id].la_ioint_irqs[first + vector] =
854                             irqs[vector];
855                 mtx_unlock_spin(&icu_lock);
856                 return (first + APIC_IO_INTS);
857         }
858         mtx_unlock_spin(&icu_lock);
859         printf("APIC: Couldn't find APIC vectors for %u IRQs\n", count);
860         return (0);
861 }
862
863 /*
864  * Enable a vector for a particular apic_id.  Since all lapics share idt
865  * entries and ioint_handlers this enables the vector on all lapics.  lapics
866  * which do not have the vector configured would report spurious interrupts
867  * should it fire.
868  */
869 void
870 apic_enable_vector(u_int apic_id, u_int vector)
871 {
872
873         KASSERT(vector != IDT_SYSCALL, ("Attempt to overwrite syscall entry"));
874         KASSERT(ioint_handlers[vector / 32] != NULL,
875             ("No ISR handler for vector %u", vector));
876         setidt(vector, ioint_handlers[vector / 32], SDT_SYSIGT, SEL_KPL, 0);
877 }
878
879 void
880 apic_disable_vector(u_int apic_id, u_int vector)
881 {
882
883         KASSERT(vector != IDT_SYSCALL, ("Attempt to overwrite syscall entry"));
884         KASSERT(ioint_handlers[vector / 32] != NULL,
885             ("No ISR handler for vector %u", vector));
886 #ifdef notyet
887         /*
888          * We can not currently clear the idt entry because other cpus
889          * may have a valid vector at this offset.
890          */
891         setidt(vector, &IDTVEC(rsvd), SDT_SYSIGT, SEL_KPL, 0);
892 #endif
893 }
894
895 /* Release an APIC vector when it's no longer in use. */
896 void
897 apic_free_vector(u_int apic_id, u_int vector, u_int irq)
898 {
899         struct thread *td;
900
901         KASSERT(vector >= APIC_IO_INTS && vector != IDT_SYSCALL &&
902             vector <= APIC_IO_INTS + APIC_NUM_IOINTS,
903             ("Vector %u does not map to an IRQ line", vector));
904         KASSERT(irq < NUM_IO_INTS, ("Invalid IRQ %u", irq));
905         KASSERT(lapics[apic_id].la_ioint_irqs[vector - APIC_IO_INTS] ==
906             irq, ("IRQ mismatch"));
907
908         /*
909          * Bind us to the cpu that owned the vector before freeing it so
910          * we don't lose an interrupt delivery race.
911          */
912         td = curthread;
913         thread_lock(td);
914         if (sched_is_bound(td))
915                 panic("apic_free_vector: Thread already bound.\n");
916         sched_bind(td, apic_cpuid(apic_id));
917         thread_unlock(td);
918         mtx_lock_spin(&icu_lock);
919         lapics[apic_id].la_ioint_irqs[vector - APIC_IO_INTS] = -1;
920         mtx_unlock_spin(&icu_lock);
921         thread_lock(td);
922         sched_unbind(td);
923         thread_unlock(td);
924
925 }
926
927 /* Map an IDT vector (APIC) to an IRQ (interrupt source). */
928 u_int
929 apic_idt_to_irq(u_int apic_id, u_int vector)
930 {
931         int irq;
932
933         KASSERT(vector >= APIC_IO_INTS && vector != IDT_SYSCALL &&
934             vector <= APIC_IO_INTS + APIC_NUM_IOINTS,
935             ("Vector %u does not map to an IRQ line", vector));
936         irq = lapics[apic_id].la_ioint_irqs[vector - APIC_IO_INTS];
937         if (irq < 0)
938                 irq = 0;
939         return (irq);
940 }
941
942 #ifdef DDB
943 /*
944  * Dump data about APIC IDT vector mappings.
945  */
946 DB_SHOW_COMMAND(apic, db_show_apic)
947 {
948         struct intsrc *isrc;
949         int i, verbose;
950         u_int apic_id;
951         u_int irq;
952
953         if (strcmp(modif, "vv") == 0)
954                 verbose = 2;
955         else if (strcmp(modif, "v") == 0)
956                 verbose = 1;
957         else
958                 verbose = 0;
959         for (apic_id = 0; apic_id <= MAX_APIC_ID; apic_id++) {
960                 if (lapics[apic_id].la_present == 0)
961                         continue;
962                 db_printf("Interrupts bound to lapic %u\n", apic_id);
963                 for (i = 0; i < APIC_NUM_IOINTS + 1 && !db_pager_quit; i++) {
964                         irq = lapics[apic_id].la_ioint_irqs[i];
965                         if (irq == -1 || irq == IRQ_SYSCALL)
966                                 continue;
967                         db_printf("vec 0x%2x -> ", i + APIC_IO_INTS);
968                         if (irq == IRQ_TIMER)
969                                 db_printf("lapic timer\n");
970                         else if (irq < NUM_IO_INTS) {
971                                 isrc = intr_lookup_source(irq);
972                                 if (isrc == NULL || verbose == 0)
973                                         db_printf("IRQ %u\n", irq);
974                                 else
975                                         db_dump_intr_event(isrc->is_event,
976                                             verbose == 2);
977                         } else
978                                 db_printf("IRQ %u ???\n", irq);
979                 }
980         }
981 }
982
983 static void
984 dump_mask(const char *prefix, uint32_t v, int base)
985 {
986         int i, first;
987
988         first = 1;
989         for (i = 0; i < 32; i++)
990                 if (v & (1 << i)) {
991                         if (first) {
992                                 db_printf("%s:", prefix);
993                                 first = 0;
994                         }
995                         db_printf(" %02x", base + i);
996                 }
997         if (!first)
998                 db_printf("\n");
999 }
1000
1001 /* Show info from the lapic regs for this CPU. */
1002 DB_SHOW_COMMAND(lapic, db_show_lapic)
1003 {
1004         uint32_t v;
1005
1006         db_printf("lapic ID = %d\n", lapic_id());
1007         v = lapic->version;
1008         db_printf("version  = %d.%d\n", (v & APIC_VER_VERSION) >> 4,
1009             v & 0xf);
1010         db_printf("max LVT  = %d\n", (v & APIC_VER_MAXLVT) >> MAXLVTSHIFT);
1011         v = lapic->svr;
1012         db_printf("SVR      = %02x (%s)\n", v & APIC_SVR_VECTOR,
1013             v & APIC_SVR_ENABLE ? "enabled" : "disabled");
1014         db_printf("TPR      = %02x\n", lapic->tpr);
1015
1016 #define dump_field(prefix, index)                                       \
1017         dump_mask(__XSTRING(prefix ## index), lapic->prefix ## index,   \
1018             index * 32)
1019
1020         db_printf("In-service Interrupts:\n");
1021         dump_field(isr, 0);
1022         dump_field(isr, 1);
1023         dump_field(isr, 2);
1024         dump_field(isr, 3);
1025         dump_field(isr, 4);
1026         dump_field(isr, 5);
1027         dump_field(isr, 6);
1028         dump_field(isr, 7);
1029
1030         db_printf("TMR Interrupts:\n");
1031         dump_field(tmr, 0);
1032         dump_field(tmr, 1);
1033         dump_field(tmr, 2);
1034         dump_field(tmr, 3);
1035         dump_field(tmr, 4);
1036         dump_field(tmr, 5);
1037         dump_field(tmr, 6);
1038         dump_field(tmr, 7);
1039
1040         db_printf("IRR Interrupts:\n");
1041         dump_field(irr, 0);
1042         dump_field(irr, 1);
1043         dump_field(irr, 2);
1044         dump_field(irr, 3);
1045         dump_field(irr, 4);
1046         dump_field(irr, 5);
1047         dump_field(irr, 6);
1048         dump_field(irr, 7);
1049
1050 #undef dump_field
1051 }
1052 #endif
1053
1054 /*
1055  * APIC probing support code.  This includes code to manage enumerators.
1056  */
1057
1058 static SLIST_HEAD(, apic_enumerator) enumerators =
1059         SLIST_HEAD_INITIALIZER(enumerators);
1060 static struct apic_enumerator *best_enum;
1061
1062 void
1063 apic_register_enumerator(struct apic_enumerator *enumerator)
1064 {
1065 #ifdef INVARIANTS
1066         struct apic_enumerator *apic_enum;
1067
1068         SLIST_FOREACH(apic_enum, &enumerators, apic_next) {
1069                 if (apic_enum == enumerator)
1070                         panic("%s: Duplicate register of %s", __func__,
1071                             enumerator->apic_name);
1072         }
1073 #endif
1074         SLIST_INSERT_HEAD(&enumerators, enumerator, apic_next);
1075 }
1076
1077 /*
1078  * We have to look for CPU's very, very early because certain subsystems
1079  * want to know how many CPU's we have extremely early on in the boot
1080  * process.
1081  */
1082 static void
1083 apic_init(void *dummy __unused)
1084 {
1085         struct apic_enumerator *enumerator;
1086         int retval, best;
1087
1088         /* Don't probe if APIC mode is disabled. */
1089         if (resource_disabled("apic", 0))
1090                 return;
1091
1092         /* First, probe all the enumerators to find the best match. */
1093         best_enum = NULL;
1094         best = 0;
1095         SLIST_FOREACH(enumerator, &enumerators, apic_next) {
1096                 retval = enumerator->apic_probe();
1097                 if (retval > 0)
1098                         continue;
1099                 if (best_enum == NULL || best < retval) {
1100                         best_enum = enumerator;
1101                         best = retval;
1102                 }
1103         }
1104         if (best_enum == NULL) {
1105                 if (bootverbose)
1106                         printf("APIC: Could not find any APICs.\n");
1107                 return;
1108         }
1109
1110         if (bootverbose)
1111                 printf("APIC: Using the %s enumerator.\n",
1112                     best_enum->apic_name);
1113
1114         /* Second, probe the CPU's in the system. */
1115         retval = best_enum->apic_probe_cpus();
1116         if (retval != 0)
1117                 printf("%s: Failed to probe CPUs: returned %d\n",
1118                     best_enum->apic_name, retval);
1119 }
1120 SYSINIT(apic_init, SI_SUB_TUNABLES - 1, SI_ORDER_SECOND, apic_init, NULL);
1121
1122 /*
1123  * Setup the local APIC.  We have to do this prior to starting up the APs
1124  * in the SMP case.
1125  */
1126 static void
1127 apic_setup_local(void *dummy __unused)
1128 {
1129         int retval;
1130
1131         if (best_enum == NULL)
1132                 return;
1133         retval = best_enum->apic_setup_local();
1134         if (retval != 0)
1135                 printf("%s: Failed to setup the local APIC: returned %d\n",
1136                     best_enum->apic_name, retval);
1137 }
1138 SYSINIT(apic_setup_local, SI_SUB_CPU, SI_ORDER_SECOND, apic_setup_local,
1139     NULL);
1140
1141 /*
1142  * Setup the I/O APICs.
1143  */
1144 static void
1145 apic_setup_io(void *dummy __unused)
1146 {
1147         int retval;
1148
1149         if (best_enum == NULL)
1150                 return;
1151         retval = best_enum->apic_setup_io();
1152         if (retval != 0)
1153                 printf("%s: Failed to setup I/O APICs: returned %d\n",
1154                     best_enum->apic_name, retval);
1155
1156         /*
1157          * Finish setting up the local APIC on the BSP once we know how to
1158          * properly program the LINT pins.
1159          */
1160         lapic_setup(1);
1161         intr_register_pic(&lapic_pic);
1162         if (bootverbose)
1163                 lapic_dump("BSP");
1164
1165         /* Enable the MSI "pic". */
1166         msi_init();
1167 }
1168 SYSINIT(apic_setup_io, SI_SUB_INTR, SI_ORDER_SECOND, apic_setup_io, NULL);
1169
1170 #ifdef SMP
1171 /*
1172  * Inter Processor Interrupt functions.  The lapic_ipi_*() functions are
1173  * private to the sys/amd64 code.  The public interface for the rest of the
1174  * kernel is defined in mp_machdep.c.
1175  */
1176 int
1177 lapic_ipi_wait(int delay)
1178 {
1179         int x, incr;
1180
1181         /*
1182          * Wait delay loops for IPI to be sent.  This is highly bogus
1183          * since this is sensitive to CPU clock speed.  If delay is
1184          * -1, we wait forever.
1185          */
1186         if (delay == -1) {
1187                 incr = 0;
1188                 delay = 1;
1189         } else
1190                 incr = 1;
1191         for (x = 0; x < delay; x += incr) {
1192                 if ((lapic->icr_lo & APIC_DELSTAT_MASK) == APIC_DELSTAT_IDLE)
1193                         return (1);
1194                 ia32_pause();
1195         }
1196         return (0);
1197 }
1198
1199 void
1200 lapic_ipi_raw(register_t icrlo, u_int dest)
1201 {
1202         register_t value, eflags;
1203
1204         /* XXX: Need more sanity checking of icrlo? */
1205         KASSERT(lapic != NULL, ("%s called too early", __func__));
1206         KASSERT((dest & ~(APIC_ID_MASK >> APIC_ID_SHIFT)) == 0,
1207             ("%s: invalid dest field", __func__));
1208         KASSERT((icrlo & APIC_ICRLO_RESV_MASK) == 0,
1209             ("%s: reserved bits set in ICR LO register", __func__));
1210
1211         /* Set destination in ICR HI register if it is being used. */
1212         eflags = intr_disable();
1213         if ((icrlo & APIC_DEST_MASK) == APIC_DEST_DESTFLD) {
1214                 value = lapic->icr_hi;
1215                 value &= ~APIC_ID_MASK;
1216                 value |= dest << APIC_ID_SHIFT;
1217                 lapic->icr_hi = value;
1218         }
1219
1220         /* Program the contents of the IPI and dispatch it. */
1221         value = lapic->icr_lo;
1222         value &= APIC_ICRLO_RESV_MASK;
1223         value |= icrlo;
1224         lapic->icr_lo = value;
1225         intr_restore(eflags);
1226 }
1227
1228 #define BEFORE_SPIN     1000000
1229 #ifdef DETECT_DEADLOCK
1230 #define AFTER_SPIN      1000
1231 #endif
1232
1233 void
1234 lapic_ipi_vectored(u_int vector, int dest)
1235 {
1236         register_t icrlo, destfield;
1237
1238         KASSERT((vector & ~APIC_VECTOR_MASK) == 0,
1239             ("%s: invalid vector %d", __func__, vector));
1240
1241         icrlo = APIC_DESTMODE_PHY | APIC_TRIGMOD_EDGE;
1242
1243         /*
1244          * IPI_STOP_HARD is just a "fake" vector used to send a NMI.
1245          * Use special rules regard NMI if passed, otherwise specify
1246          * the vector.
1247          */
1248         if (vector == IPI_STOP_HARD)
1249                 icrlo |= APIC_DELMODE_NMI | APIC_LEVEL_ASSERT;
1250         else
1251                 icrlo |= vector | APIC_DELMODE_FIXED | APIC_LEVEL_DEASSERT;
1252         destfield = 0;
1253         switch (dest) {
1254         case APIC_IPI_DEST_SELF:
1255                 icrlo |= APIC_DEST_SELF;
1256                 break;
1257         case APIC_IPI_DEST_ALL:
1258                 icrlo |= APIC_DEST_ALLISELF;
1259                 break;
1260         case APIC_IPI_DEST_OTHERS:
1261                 icrlo |= APIC_DEST_ALLESELF;
1262                 break;
1263         default:
1264                 KASSERT((dest & ~(APIC_ID_MASK >> APIC_ID_SHIFT)) == 0,
1265                     ("%s: invalid destination 0x%x", __func__, dest));
1266                 destfield = dest;
1267         }
1268
1269         /* Wait for an earlier IPI to finish. */
1270         if (!lapic_ipi_wait(BEFORE_SPIN)) {
1271                 if (panicstr != NULL)
1272                         return;
1273                 else
1274                         panic("APIC: Previous IPI is stuck");
1275         }
1276
1277         lapic_ipi_raw(icrlo, destfield);
1278
1279 #ifdef DETECT_DEADLOCK
1280         /* Wait for IPI to be delivered. */
1281         if (!lapic_ipi_wait(AFTER_SPIN)) {
1282 #ifdef needsattention
1283                 /*
1284                  * XXX FIXME:
1285                  *
1286                  * The above function waits for the message to actually be
1287                  * delivered.  It breaks out after an arbitrary timeout
1288                  * since the message should eventually be delivered (at
1289                  * least in theory) and that if it wasn't we would catch
1290                  * the failure with the check above when the next IPI is
1291                  * sent.
1292                  *
1293                  * We could skip this wait entirely, EXCEPT it probably
1294                  * protects us from other routines that assume that the
1295                  * message was delivered and acted upon when this function
1296                  * returns.
1297                  */
1298                 printf("APIC: IPI might be stuck\n");
1299 #else /* !needsattention */
1300                 /* Wait until mesage is sent without a timeout. */
1301                 while (lapic->icr_lo & APIC_DELSTAT_PEND)
1302                         ia32_pause();
1303 #endif /* needsattention */
1304         }
1305 #endif /* DETECT_DEADLOCK */
1306 }
1307 #endif /* SMP */