2 * Copyright (c) 1996, by Steve Passe
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. The name of the developer may NOT be used to endorse or promote products
11 * derived from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * $FreeBSD: src/sys/i386/i386/mpapic.c,v 1.37.2.7 2003/01/25 02:31:47 peter Exp $
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/kernel.h>
32 #include <sys/machintr.h>
33 #include <machine/globaldata.h>
34 #include <machine/clock.h>
35 #include <machine/limits.h>
36 #include <machine/smp.h>
37 #include <machine/md_var.h>
38 #include <machine/pmap.h>
39 #include <machine/specialreg.h>
40 #include <machine_base/apic/lapic.h>
41 #include <machine_base/apic/ioapic.h>
42 #include <machine_base/apic/ioapic_abi.h>
43 #include <machine_base/apic/apicvar.h>
44 #include <machine_base/icu/icu_var.h>
45 #include <machine/segments.h>
46 #include <sys/thread2.h>
47 #include <sys/spinlock2.h>
49 #include <machine/cputypes.h>
50 #include <machine/intr_machdep.h>
54 volatile lapic_t
*lapic
;
56 static void lapic_timer_calibrate(void);
57 static void lapic_timer_set_divisor(int);
58 static void lapic_timer_fixup_handler(void *);
59 static void lapic_timer_restart_handler(void *);
62 static int lapic_timer_enable
= 1;
63 TUNABLE_INT("hw.lapic_timer_enable", &lapic_timer_enable
);
65 static int lapic_timer_tscdeadline
= 1;
66 TUNABLE_INT("hw.lapic_timer_tscdeadline", &lapic_timer_tscdeadline
);
68 static void lapic_timer_tscdlt_reload(struct cputimer_intr
*, sysclock_t
);
69 static void lapic_timer_intr_reload(struct cputimer_intr
*, sysclock_t
);
70 static void lapic_timer_intr_enable(struct cputimer_intr
*);
71 static void lapic_timer_intr_restart(struct cputimer_intr
*);
72 static void lapic_timer_intr_pmfixup(struct cputimer_intr
*);
74 static struct cputimer_intr lapic_cputimer_intr
= {
76 .reload
= lapic_timer_intr_reload
,
77 .enable
= lapic_timer_intr_enable
,
78 .config
= cputimer_intr_default_config
,
79 .restart
= lapic_timer_intr_restart
,
80 .pmfixup
= lapic_timer_intr_pmfixup
,
81 .initclock
= cputimer_intr_default_initclock
,
83 .next
= SLIST_ENTRY_INITIALIZER
,
85 .type
= CPUTIMER_INTR_LAPIC
,
86 .prio
= CPUTIMER_INTR_PRIO_LAPIC
,
87 .caps
= CPUTIMER_INTR_CAP_NONE
,
91 static int lapic_timer_divisor_idx
= -1;
92 static const uint32_t lapic_timer_divisors
[] = {
93 APIC_TDCR_2
, APIC_TDCR_4
, APIC_TDCR_8
, APIC_TDCR_16
,
94 APIC_TDCR_32
, APIC_TDCR_64
, APIC_TDCR_128
, APIC_TDCR_1
96 #define APIC_TIMER_NDIVISORS (int)(NELEM(lapic_timer_divisors))
98 static int lapic_use_tscdeadline
= 0;
99 /* The raw TSC frequency might not fit into a sysclock_t value. */
100 static int lapic_timer_tscfreq_shift
;
103 * APIC ID <-> CPU ID mapping structures.
105 int cpu_id_to_apic_id
[NAPICID
];
106 int apic_id_to_cpu_id
[NAPICID
];
107 int lapic_enable
= 1;
109 /* Separate cachelines for each cpu's info. */
112 uint64_t downcount_time
;
115 struct deadlines
*tsc_deadlines
= NULL
;
118 * Enable LAPIC, configure interrupts.
121 lapic_init(boolean_t bsp
)
127 /* Decide whether we want to use TSC Deadline mode. */
128 if (lapic_timer_tscdeadline
!= 0 &&
129 (cpu_feature2
& CPUID2_TSCDLT
) &&
130 tsc_invariant
&& tsc_frequency
!= 0) {
131 lapic_use_tscdeadline
= 1;
132 tsc_deadlines
= kmalloc_cachealign(
133 sizeof(struct deadlines
) * (naps
+ 1),
134 M_DEVBUF
, M_WAITOK
| M_ZERO
);
141 * Since IDT is shared between BSP and APs, these vectors
142 * only need to be installed once; we do it on BSP.
145 if (cpu_vendor_id
== CPU_VENDOR_AMD
&&
146 CPUID_TO_FAMILY(cpu_id
) >= 0x0f &&
147 CPUID_TO_FAMILY(cpu_id
) < 0x17) { /* XXX */
151 * Set the LINTEN bit in the HyperTransport
152 * Transaction Control Register.
154 * This will cause EXTINT and NMI interrupts
155 * routed over the hypertransport bus to be
156 * fed into the LAPIC LINT0/LINT1. If the bit
157 * isn't set, the interrupts will go to the
158 * general cpu INTR/NMI pins. On a dual-core
159 * cpu the interrupt winds up going to BOTH cpus.
160 * The first cpu that does the interrupt ack
161 * cycle will get the correct interrupt. The
162 * second cpu that does it will get a spurious
163 * interrupt vector (typically IRQ 7).
166 (1 << 31) | /* enable */
167 (0 << 16) | /* bus */
168 (0x18 << 11) | /* dev (cpu + 0x18) */
169 (0 << 8) | /* func */
173 if ((tcr
& 0x00010000) == 0) {
174 kprintf("LAPIC: AMD LINTEN on\n");
175 outl(0xcfc, tcr
|0x00010000);
180 /* Install a 'Spurious INTerrupt' vector */
181 setidt_global(XSPURIOUSINT_OFFSET
, Xspuriousint
,
182 SDT_SYSIGT
, SEL_KPL
, 0);
184 /* Install a timer vector */
185 setidt_global(XTIMER_OFFSET
, Xtimer
,
186 SDT_SYSIGT
, SEL_KPL
, 0);
188 /* Install an inter-CPU IPI for TLB invalidation */
189 setidt_global(XINVLTLB_OFFSET
, Xinvltlb
,
190 SDT_SYSIGT
, SEL_KPL
, 0);
192 /* Install an inter-CPU IPI for IPIQ messaging */
193 setidt_global(XIPIQ_OFFSET
, Xipiq
,
194 SDT_SYSIGT
, SEL_KPL
, 0);
196 /* Install an inter-CPU IPI for CPU stop/restart */
197 setidt_global(XCPUSTOP_OFFSET
, Xcpustop
,
198 SDT_SYSIGT
, SEL_KPL
, 0);
200 /* Install an inter-CPU IPI for TLB invalidation */
201 setidt_global(XSNIFF_OFFSET
, Xsniff
,
202 SDT_SYSIGT
, SEL_KPL
, 0);
206 * Setup LINT0 as ExtINT on the BSP. This is theoretically an
207 * aggregate interrupt input from the 8259. The INTA cycle
208 * will be routed to the external controller (the 8259) which
209 * is expected to supply the vector.
211 * Must be setup edge triggered, active high.
213 * Disable LINT0 on BSP, if I/O APIC is enabled.
215 * Disable LINT0 on the APs. It doesn't matter what delivery
216 * mode we use because we leave it masked.
218 temp
= lapic
->lvt_lint0
;
219 temp
&= ~(APIC_LVT_MASKED
| APIC_LVT_TRIG_MASK
|
220 APIC_LVT_POLARITY_MASK
| APIC_LVT_DM_MASK
);
222 temp
|= APIC_LVT_DM_EXTINT
;
224 temp
|= APIC_LVT_MASKED
;
226 temp
|= APIC_LVT_DM_FIXED
| APIC_LVT_MASKED
;
228 lapic
->lvt_lint0
= temp
;
231 * Setup LINT1 as NMI.
233 * Must be setup edge trigger, active high.
235 * Enable LINT1 on BSP, if I/O APIC is enabled.
237 * Disable LINT1 on the APs.
239 temp
= lapic
->lvt_lint1
;
240 temp
&= ~(APIC_LVT_MASKED
| APIC_LVT_TRIG_MASK
|
241 APIC_LVT_POLARITY_MASK
| APIC_LVT_DM_MASK
);
242 temp
|= APIC_LVT_MASKED
| APIC_LVT_DM_NMI
;
243 if (bsp
&& ioapic_enable
)
244 temp
&= ~APIC_LVT_MASKED
;
245 lapic
->lvt_lint1
= temp
;
248 * Mask the LAPIC error interrupt, LAPIC performance counter
251 lapic
->lvt_error
= lapic
->lvt_error
| APIC_LVT_MASKED
;
252 lapic
->lvt_pcint
= lapic
->lvt_pcint
| APIC_LVT_MASKED
;
255 * Set LAPIC timer vector and mask the LAPIC timer interrupt.
257 timer
= lapic
->lvt_timer
;
258 timer
&= ~APIC_LVTT_VECTOR
;
259 timer
|= XTIMER_OFFSET
;
260 timer
|= APIC_LVTT_MASKED
;
261 lapic
->lvt_timer
= timer
;
264 * Set the Task Priority Register as needed. At the moment allow
265 * interrupts on all cpus (the APs will remain CLId until they are
269 temp
&= ~APIC_TPR_PRIO
; /* clear priority field */
275 if (cpu_vendor_id
== CPU_VENDOR_AMD
&&
276 (lapic
->version
& APIC_VER_AMD_EXT_SPACE
)) {
283 ext_feat
= lapic
->ext_feat
;
284 count
= (ext_feat
& APIC_EXTFEAT_MASK
) >> APIC_EXTFEAT_SHIFT
;
285 max_count
= sizeof(lapic
->ext_lvt
) / sizeof(lapic
->ext_lvt
[0]);
286 if (count
> max_count
)
288 for (i
= 0; i
< count
; ++i
) {
289 lvt
= lapic
->ext_lvt
[i
].lvt
;
291 lvt
&= ~(APIC_LVT_POLARITY_MASK
| APIC_LVT_TRIG_MASK
|
292 APIC_LVT_DM_MASK
| APIC_LVT_MASKED
);
293 lvt
|= APIC_LVT_MASKED
| APIC_LVT_DM_FIXED
;
296 case APIC_EXTLVT_IBS
:
298 case APIC_EXTLVT_MCA
:
300 case APIC_EXTLVT_DEI
:
302 case APIC_EXTLVT_SBI
:
308 kprintf(" LAPIC AMD elvt%d: 0x%08x",
309 i
, lapic
->ext_lvt
[i
].lvt
);
310 if (lapic
->ext_lvt
[i
].lvt
!= lvt
)
311 kprintf(" -> 0x%08x", lvt
);
314 lapic
->ext_lvt
[i
].lvt
= lvt
;
322 temp
|= APIC_SVR_ENABLE
; /* enable the LAPIC */
323 temp
&= ~APIC_SVR_FOCUS_DISABLE
; /* enable lopri focus processor */
325 if (lapic
->version
& APIC_VER_EOI_SUPP
) {
326 if (temp
& APIC_SVR_EOI_SUPP
) {
327 temp
&= ~APIC_SVR_EOI_SUPP
;
329 kprintf(" LAPIC disabling EOI supp\n");
334 * Set the spurious interrupt vector. The low 4 bits of the vector
337 if ((XSPURIOUSINT_OFFSET
& 0x0F) != 0x0F)
338 panic("bad XSPURIOUSINT_OFFSET: 0x%08x", XSPURIOUSINT_OFFSET
);
339 temp
&= ~APIC_SVR_VECTOR
;
340 temp
|= XSPURIOUSINT_OFFSET
;
345 * Pump out a few EOIs to clean out interrupts that got through
346 * before we were able to set the TPR.
353 lapic_timer_calibrate();
354 if (lapic_timer_enable
) {
355 if (cpu_thermal_feature
& CPUID_THERMAL_ARAT
) {
357 * Local APIC timer will not stop
360 lapic_cputimer_intr
.caps
|=
361 CPUTIMER_INTR_CAP_PS
;
363 if (lapic_use_tscdeadline
) {
364 lapic_cputimer_intr
.reload
=
365 lapic_timer_tscdlt_reload
;
367 cputimer_intr_register(&lapic_cputimer_intr
);
368 cputimer_intr_select(&lapic_cputimer_intr
, 0);
370 } else if (!lapic_use_tscdeadline
) {
371 lapic_timer_set_divisor(lapic_timer_divisor_idx
);
375 apic_dump("apic_initialize()");
379 lapic_timer_set_divisor(int divisor_idx
)
381 KKASSERT(divisor_idx
>= 0 && divisor_idx
< APIC_TIMER_NDIVISORS
);
382 lapic
->dcr_timer
= lapic_timer_divisors
[divisor_idx
];
386 lapic_timer_oneshot(u_int count
)
390 value
= lapic
->lvt_timer
;
391 value
&= ~(APIC_LVTT_PERIODIC
| APIC_LVTT_TSCDLT
);
392 lapic
->lvt_timer
= value
;
393 lapic
->icr_timer
= count
;
397 lapic_timer_oneshot_quick(u_int count
)
399 lapic
->icr_timer
= count
;
403 lapic_timer_tscdeadline_quick(uint64_t diff
)
405 uint64_t val
= rdtsc() + diff
;
407 wrmsr(MSR_TSC_DEADLINE
, val
);
408 tsc_deadlines
[mycpuid
].timestamp
= val
;
412 lapic_scale_to_tsc(unsigned value
, unsigned scale
)
417 val
*= tsc_frequency
;
424 lapic_timer_calibrate(void)
428 /* No need to calibrate lapic_timer, if we will use TSC Deadline mode */
429 if (lapic_use_tscdeadline
) {
430 lapic_timer_tscfreq_shift
= 0;
431 while ((tsc_frequency
>> lapic_timer_tscfreq_shift
) > INT_MAX
)
432 lapic_timer_tscfreq_shift
++;
433 lapic_cputimer_intr
.freq
=
434 tsc_frequency
>> lapic_timer_tscfreq_shift
;
436 "lapic: TSC Deadline Mode: shift %d, frequency %u Hz\n",
437 lapic_timer_tscfreq_shift
, lapic_cputimer_intr
.freq
);
441 /* Try to calibrate the local APIC timer. */
442 for (lapic_timer_divisor_idx
= 0;
443 lapic_timer_divisor_idx
< APIC_TIMER_NDIVISORS
;
444 lapic_timer_divisor_idx
++) {
445 lapic_timer_set_divisor(lapic_timer_divisor_idx
);
446 lapic_timer_oneshot(APIC_TIMER_MAX_COUNT
);
448 value
= APIC_TIMER_MAX_COUNT
- lapic
->ccr_timer
;
449 if (value
!= APIC_TIMER_MAX_COUNT
)
452 if (lapic_timer_divisor_idx
>= APIC_TIMER_NDIVISORS
)
453 panic("lapic: no proper timer divisor?!");
454 lapic_cputimer_intr
.freq
= value
/ 2;
456 kprintf("lapic: divisor index %d, frequency %u Hz\n",
457 lapic_timer_divisor_idx
, lapic_cputimer_intr
.freq
);
461 lapic_timer_tscdlt_reload(struct cputimer_intr
*cti
, sysclock_t reload
)
463 struct globaldata
*gd
= mycpu
;
464 uint64_t diff
, now
, val
;
466 if (reload
> 1000*1000*1000)
467 reload
= 1000*1000*1000;
468 diff
= (uint64_t)reload
* tsc_frequency
/ sys_cputimer
->freq
;
471 if (cpu_vendor_id
== CPU_VENDOR_INTEL
)
477 if (gd
->gd_timer_running
) {
478 uint64_t deadline
= tsc_deadlines
[mycpuid
].timestamp
;
479 if (deadline
== 0 || now
> deadline
|| val
< deadline
) {
480 wrmsr(MSR_TSC_DEADLINE
, val
);
481 tsc_deadlines
[mycpuid
].timestamp
= val
;
484 gd
->gd_timer_running
= 1;
485 wrmsr(MSR_TSC_DEADLINE
, val
);
486 tsc_deadlines
[mycpuid
].timestamp
= val
;
491 lapic_timer_intr_reload(struct cputimer_intr
*cti
, sysclock_t reload
)
493 struct globaldata
*gd
= mycpu
;
495 reload
= (int64_t)reload
* cti
->freq
/ sys_cputimer
->freq
;
499 if (gd
->gd_timer_running
) {
500 if (reload
< lapic
->ccr_timer
)
501 lapic_timer_oneshot_quick(reload
);
503 gd
->gd_timer_running
= 1;
504 lapic_timer_oneshot_quick(reload
);
509 lapic_timer_intr_enable(struct cputimer_intr
*cti __unused
)
513 timer
= lapic
->lvt_timer
;
514 timer
&= ~(APIC_LVTT_MASKED
| APIC_LVTT_PERIODIC
| APIC_LVTT_TSCDLT
);
515 if (lapic_use_tscdeadline
)
516 timer
|= APIC_LVTT_TSCDLT
;
517 lapic
->lvt_timer
= timer
;
518 if (lapic_use_tscdeadline
)
521 lapic_timer_fixup_handler(NULL
);
525 lapic_timer_fixup_handler(void *arg
)
532 if (cpu_vendor_id
== CPU_VENDOR_AMD
) {
534 * Detect the presence of C1E capability mostly on latest
535 * dual-cores (or future) k8 family. This feature renders
536 * the local APIC timer dead, so we disable it by reading
537 * the Interrupt Pending Message register and clearing both
538 * C1eOnCmpHalt (bit 28) and SmiOnCmpHalt (bit 27).
541 * "BIOS and Kernel Developer's Guide for AMD NPT
542 * Family 0Fh Processors"
543 * #32559 revision 3.00
545 if ((cpu_id
& 0x00000f00) == 0x00000f00 &&
546 (cpu_id
& 0x0fff0000) >= 0x00040000) {
549 msr
= rdmsr(0xc0010055);
550 if (msr
& 0x18000000) {
551 struct globaldata
*gd
= mycpu
;
553 kprintf("cpu%d: AMD C1E detected\n",
555 wrmsr(0xc0010055, msr
& ~0x18000000ULL
);
558 * We are kinda stalled;
561 gd
->gd_timer_running
= 1;
562 if (lapic_use_tscdeadline
) {
563 /* Maybe reached in Virtual Machines? */
564 lapic_timer_tscdeadline_quick(5000);
566 lapic_timer_oneshot_quick(2);
577 lapic_timer_restart_handler(void *dummy __unused
)
581 lapic_timer_fixup_handler(&started
);
583 struct globaldata
*gd
= mycpu
;
585 gd
->gd_timer_running
= 1;
586 if (lapic_use_tscdeadline
) {
587 /* Maybe reached in Virtual Machines? */
588 lapic_timer_tscdeadline_quick(5000);
590 lapic_timer_oneshot_quick(2);
596 * This function is called only by ACPICA code currently:
597 * - AMD C1E fixup. AMD C1E only seems to happen after ACPI
598 * module controls PM. So once ACPICA is attached, we try
599 * to apply the fixup to prevent LAPIC timer from hanging.
602 lapic_timer_intr_pmfixup(struct cputimer_intr
*cti __unused
)
604 lwkt_send_ipiq_mask(smp_active_mask
,
605 lapic_timer_fixup_handler
, NULL
);
609 lapic_timer_intr_restart(struct cputimer_intr
*cti __unused
)
611 lwkt_send_ipiq_mask(smp_active_mask
, lapic_timer_restart_handler
, NULL
);
616 * dump contents of local APIC registers
621 kprintf("SMP: CPU%d %s:\n", mycpu
->gd_cpuid
, str
);
622 kprintf(" lint0: 0x%08x lint1: 0x%08x TPR: 0x%08x SVR: 0x%08x\n",
623 lapic
->lvt_lint0
, lapic
->lvt_lint1
, lapic
->tpr
, lapic
->svr
);
627 * Inter Processor Interrupt functions.
631 * Send APIC IPI 'vector' to 'destType' via 'deliveryMode'.
633 * destType is 1 of: APIC_DEST_SELF, APIC_DEST_ALLISELF, APIC_DEST_ALLESELF
634 * vector is any valid SYSTEM INT vector
635 * delivery_mode is 1 of: APIC_DELMODE_FIXED, APIC_DELMODE_LOWPRIO
639 * We now implement a per-cpu interlock (gd->gd_npoll) to prevent more than
640 * one IPI from being sent to any given cpu at a time. Thus we no longer
641 * have to process incoming IPIs while waiting for the status to clear.
642 * No deadlock should be possible.
644 * We now physically disable interrupts for the lapic ICR operation. If
645 * we do not do this then it looks like an EOI sent to the lapic (which
646 * occurs even with a critical section) can interfere with the command
647 * register ready status and cause an IPI to be lost.
649 * e.g. an interrupt can occur, issue the EOI, IRET, and cause the command
650 * register to busy just before we write to icr_lo, resulting in a lost
651 * issuance. This only appears to occur on Intel cpus and is not
652 * documented. It could simply be that cpus are so fast these days that
653 * it was always an issue, but is only now rearing its ugly head. This
657 apic_ipi(int dest_type
, int vector
, int delivery_mode
)
664 if ((lapic
->icr_lo
& APIC_DELSTAT_MASK
) != 0) {
666 while ((lapic
->icr_lo
& APIC_DELSTAT_MASK
) != 0) {
668 if ((tsc_sclock_t
)(rdtsc() -
669 (tsc
+ tsc_frequency
)) > 0) {
670 kprintf("apic_ipi stall cpu %d (sing)\n",
678 icr_hi
= lapic
->icr_hi
& ~APIC_ID_MASK
;
679 icr_lo
= (lapic
->icr_lo
& APIC_ICRLO_RESV_MASK
) | dest_type
|
680 APIC_LEVEL_ASSERT
| delivery_mode
| vector
;
681 lapic
->icr_hi
= icr_hi
;
682 lapic
->icr_lo
= icr_lo
;
688 * Interrupts must be hard-disabled by caller
691 single_apic_ipi(int cpu
, int vector
, int delivery_mode
)
698 if ((lapic
->icr_lo
& APIC_DELSTAT_MASK
) != 0) {
700 while ((lapic
->icr_lo
& APIC_DELSTAT_MASK
) != 0) {
702 if ((tsc_sclock_t
)(rdtsc() -
703 (tsc
+ tsc_frequency
)) > 0) {
704 kprintf("single_apic_ipi stall cpu %d (sing)\n",
712 icr_hi
= lapic
->icr_hi
& ~APIC_ID_MASK
;
713 icr_hi
|= (CPUID_TO_APICID(cpu
) << 24);
716 icr_lo
= (lapic
->icr_lo
& APIC_ICRLO_RESV_MASK
) |
717 APIC_LEVEL_ASSERT
| APIC_DEST_DESTFLD
| delivery_mode
| vector
;
720 lapic
->icr_hi
= icr_hi
;
721 lapic
->icr_lo
= icr_lo
;
727 * Returns 0 if the apic is busy, 1 if we were able to queue the request.
729 * NOT WORKING YET! The code as-is may end up not queueing an IPI at all
730 * to the target, and the scheduler does not 'poll' for IPI messages.
733 single_apic_ipi_passive(int cpu
, int vector
, int delivery_mode
)
737 unsigned long rflags
;
739 rflags
= read_rflags();
741 if ((lapic
->icr_lo
& APIC_DELSTAT_MASK
) != 0) {
742 write_rflags(rflags
);
745 icr_hi
= lapic
->icr_hi
& ~APIC_ID_MASK
;
746 icr_hi
|= (CPUID_TO_APICID(cpu
) << 24);
747 lapic
->icr_hi
= icr_hi
;
750 icr_lo
= (lapic
->icr_lo
& APIC_RESV2_MASK
) |
751 APIC_DEST_DESTFLD
| delivery_mode
| vector
;
754 lapic
->icr_lo
= icr_lo
;
755 write_rflags(rflags
);
763 * Send APIC IPI 'vector' to 'target's via 'delivery_mode'.
765 * target is a bitmask of destination cpus. Vector is any
766 * valid system INT vector. Delivery mode may be either
767 * APIC_DELMODE_FIXED or APIC_DELMODE_LOWPRIO.
769 * Interrupts must be hard-disabled by caller
772 selected_apic_ipi(cpumask_t target
, int vector
, int delivery_mode
)
774 while (CPUMASK_TESTNZERO(target
)) {
775 int n
= BSFCPUMASK(target
);
776 CPUMASK_NANDBIT(target
, n
);
777 single_apic_ipi(n
, vector
, delivery_mode
);
782 * Load a 'downcount time' in uSeconds.
785 set_apic_timer(int us
)
789 if (lapic_use_tscdeadline
) {
792 val
= lapic_scale_to_tsc(us
, 1000000);
794 /* No need to arm the lapic here, just track the timeout. */
795 tsc_deadlines
[mycpuid
].downcount_time
= val
;
800 * When we reach here, lapic timer's frequency
801 * must have been calculated as well as the
802 * divisor (lapic->dcr_timer is setup during the
803 * divisor calculation).
805 KKASSERT(lapic_cputimer_intr
.freq
!= 0 &&
806 lapic_timer_divisor_idx
>= 0);
808 count
= ((us
* (int64_t)lapic_cputimer_intr
.freq
) + 999999) / 1000000;
809 lapic_timer_oneshot(count
);
814 * Read remaining time in timer, in microseconds (rounded up).
817 read_apic_timer(void)
821 if (lapic_use_tscdeadline
) {
824 val
= tsc_deadlines
[mycpuid
].downcount_time
;
826 if (val
== 0 || now
> val
) {
831 val
+= (tsc_frequency
- 1);
832 val
/= tsc_frequency
;
839 val
= lapic
->ccr_timer
;
843 KKASSERT(lapic_cputimer_intr
.freq
> 0);
845 val
+= (lapic_cputimer_intr
.freq
- 1);
846 val
/= lapic_cputimer_intr
.freq
;
854 * Spin-style delay, set delay time in uS, spin till it drains.
859 set_apic_timer(count
);
860 while (read_apic_timer())
865 lapic_unused_apic_id(int start
)
869 for (i
= start
; i
< APICID_MAX
; ++i
) {
870 if (APICID_TO_CPUID(i
) == -1)
877 lapic_map(vm_paddr_t lapic_addr
)
879 lapic
= pmap_mapdev_uncacheable(lapic_addr
, sizeof(struct LAPIC
));
882 static TAILQ_HEAD(, lapic_enumerator
) lapic_enumerators
=
883 TAILQ_HEAD_INITIALIZER(lapic_enumerators
);
888 struct lapic_enumerator
*e
;
889 int error
, i
, ap_max
;
891 KKASSERT(lapic_enable
);
893 for (i
= 0; i
< NAPICID
; ++i
)
894 APICID_TO_CPUID(i
) = -1;
896 TAILQ_FOREACH(e
, &lapic_enumerators
, lapic_link
) {
897 error
= e
->lapic_probe(e
);
902 kprintf("LAPIC: Can't find LAPIC\n");
906 error
= e
->lapic_enumerate(e
);
908 kprintf("LAPIC: enumeration failed\n");
913 TUNABLE_INT_FETCH("hw.ap_max", &ap_max
);
914 if (ap_max
> MAXCPU
- 1)
918 kprintf("LAPIC: Warning use only %d out of %d "
928 lapic_enumerator_register(struct lapic_enumerator
*ne
)
930 struct lapic_enumerator
*e
;
932 TAILQ_FOREACH(e
, &lapic_enumerators
, lapic_link
) {
933 if (e
->lapic_prio
< ne
->lapic_prio
) {
934 TAILQ_INSERT_BEFORE(e
, ne
, lapic_link
);
938 TAILQ_INSERT_TAIL(&lapic_enumerators
, ne
, lapic_link
);
942 lapic_set_cpuid(int cpu_id
, int apic_id
)
944 CPUID_TO_APICID(cpu_id
) = apic_id
;
945 APICID_TO_CPUID(apic_id
) = cpu_id
;
949 lapic_fixup_noioapic(void)
953 /* Only allowed on BSP */
954 KKASSERT(mycpuid
== 0);
955 KKASSERT(!ioapic_enable
);
957 temp
= lapic
->lvt_lint0
;
958 temp
&= ~APIC_LVT_MASKED
;
959 lapic
->lvt_lint0
= temp
;
961 temp
= lapic
->lvt_lint1
;
962 temp
|= APIC_LVT_MASKED
;
963 lapic
->lvt_lint1
= temp
;
967 lapic_sysinit(void *dummy __unused
)
972 error
= lapic_config();
978 /* Initialize BSP's local APIC */
980 } else if (ioapic_enable
) {
982 icu_reinit_noioapic();
985 SYSINIT(lapic
, SI_BOOT2_LAPIC
, SI_ORDER_FIRST
, lapic_sysinit
, NULL
);