10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51456Sdmick * Common Development and Distribution License (the "License").
61456Sdmick * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
213446Smrj
220Sstevel@tonic-gate /*
2312204Sjiang.liu@intel.com * Copyright (c) 1993, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
2512004Sjiang.liu@intel.com /*
2612004Sjiang.liu@intel.com * Copyright (c) 2010, Intel Corporation.
2712004Sjiang.liu@intel.com * All rights reserved.
2812004Sjiang.liu@intel.com */
290Sstevel@tonic-gate
3012683SJimmy.Vetayases@oracle.com
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate * PSMI 1.1 extensions are supported only in 2.6 and later versions.
330Sstevel@tonic-gate * PSMI 1.2 extensions are supported only in 2.7 and later versions.
340Sstevel@tonic-gate * PSMI 1.3 and 1.4 extensions are supported in Solaris 10.
350Sstevel@tonic-gate * PSMI 1.5 extensions are supported in Solaris Nevada.
365295Srandyf * PSMI 1.6 extensions are supported in Solaris Nevada.
3712004Sjiang.liu@intel.com * PSMI 1.7 extensions are supported in Solaris Nevada.
380Sstevel@tonic-gate */
3912004Sjiang.liu@intel.com #define PSMI_1_7
400Sstevel@tonic-gate
410Sstevel@tonic-gate #include <sys/processor.h>
420Sstevel@tonic-gate #include <sys/time.h>
430Sstevel@tonic-gate #include <sys/psm.h>
440Sstevel@tonic-gate #include <sys/smp_impldefs.h>
450Sstevel@tonic-gate #include <sys/cram.h>
460Sstevel@tonic-gate #include <sys/acpi/acpi.h>
470Sstevel@tonic-gate #include <sys/acpica.h>
480Sstevel@tonic-gate #include <sys/psm_common.h>
493446Smrj #include <sys/apic.h>
500Sstevel@tonic-gate #include <sys/pit.h>
510Sstevel@tonic-gate #include <sys/ddi.h>
520Sstevel@tonic-gate #include <sys/sunddi.h>
530Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
540Sstevel@tonic-gate #include <sys/pci.h>
550Sstevel@tonic-gate #include <sys/promif.h>
560Sstevel@tonic-gate #include <sys/x86_archext.h>
570Sstevel@tonic-gate #include <sys/cpc_impl.h>
580Sstevel@tonic-gate #include <sys/uadmin.h>
590Sstevel@tonic-gate #include <sys/panic.h>
600Sstevel@tonic-gate #include <sys/debug.h>
610Sstevel@tonic-gate #include <sys/archsystm.h>
620Sstevel@tonic-gate #include <sys/trap.h>
630Sstevel@tonic-gate #include <sys/machsystm.h>
643446Smrj #include <sys/sysmacros.h>
650Sstevel@tonic-gate #include <sys/cpuvar.h>
660Sstevel@tonic-gate #include <sys/rm_platter.h>
670Sstevel@tonic-gate #include <sys/privregs.h>
680Sstevel@tonic-gate #include <sys/note.h>
690Sstevel@tonic-gate #include <sys/pci_intr_lib.h>
703446Smrj #include <sys/spl.h>
715084Sjohnlev #include <sys/clock.h>
725107Seota #include <sys/dditypes.h>
735107Seota #include <sys/sunddi.h>
747349SAdrian.Frost@Sun.COM #include <sys/x_call.h>
757986SSaurabh.Mishra@Sun.COM #include <sys/reboot.h>
768906SEric.Saxe@Sun.COM #include <sys/hpet.h>
7712683SJimmy.Vetayases@oracle.com #include <sys/apic_common.h>
78*13029SKrishnendu.Sadhukhan@Sun.COM #include <sys/apic_timer.h>
790Sstevel@tonic-gate
800Sstevel@tonic-gate /*
810Sstevel@tonic-gate * Local Function Prototypes
820Sstevel@tonic-gate */
8312683SJimmy.Vetayases@oracle.com static void apic_init_intr(void);
840Sstevel@tonic-gate
850Sstevel@tonic-gate /*
860Sstevel@tonic-gate * standard MP entries
870Sstevel@tonic-gate */
8812683SJimmy.Vetayases@oracle.com static int apic_probe(void);
890Sstevel@tonic-gate static int apic_getclkirq(int ipl);
9012683SJimmy.Vetayases@oracle.com static void apic_init(void);
910Sstevel@tonic-gate static void apic_picinit(void);
920Sstevel@tonic-gate static int apic_post_cpu_start(void);
930Sstevel@tonic-gate static int apic_intr_enter(int ipl, int *vect);
940Sstevel@tonic-gate static void apic_setspl(int ipl);
957282Smishra static void x2apic_setspl(int ipl);
960Sstevel@tonic-gate static int apic_addspl(int ipl, int vector, int min_ipl, int max_ipl);
970Sstevel@tonic-gate static int apic_delspl(int ipl, int vector, int min_ipl, int max_ipl);
980Sstevel@tonic-gate static int apic_disable_intr(processorid_t cpun);
990Sstevel@tonic-gate static void apic_enable_intr(processorid_t cpun);
1000Sstevel@tonic-gate static int apic_get_ipivect(int ipl, int type);
1010Sstevel@tonic-gate static void apic_post_cyclic_setup(void *arg);
10212004Sjiang.liu@intel.com
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate * The following vector assignments influence the value of ipltopri and
1050Sstevel@tonic-gate * vectortoipl. Note that vectors 0 - 0x1f are not used. We can program
1063745Ssethg * idle to 0 and IPL 0 to 0xf to differentiate idle in case
1070Sstevel@tonic-gate * we care to do so in future. Note some IPLs which are rarely used
1080Sstevel@tonic-gate * will share the vector ranges and heavily used IPLs (5 and 6) have
1090Sstevel@tonic-gate * a wide range.
1103745Ssethg *
1113745Ssethg * This array is used to initialize apic_ipls[] (in apic_init()).
1123745Ssethg *
1130Sstevel@tonic-gate * IPL Vector range. as passed to intr_enter
1140Sstevel@tonic-gate * 0 none.
1150Sstevel@tonic-gate * 1,2,3 0x20-0x2f 0x0-0xf
1160Sstevel@tonic-gate * 4 0x30-0x3f 0x10-0x1f
1170Sstevel@tonic-gate * 5 0x40-0x5f 0x20-0x3f
1180Sstevel@tonic-gate * 6 0x60-0x7f 0x40-0x5f
1190Sstevel@tonic-gate * 7,8,9 0x80-0x8f 0x60-0x6f
1200Sstevel@tonic-gate * 10 0x90-0x9f 0x70-0x7f
1210Sstevel@tonic-gate * 11 0xa0-0xaf 0x80-0x8f
1220Sstevel@tonic-gate * ... ...
1233745Ssethg * 15 0xe0-0xef 0xc0-0xcf
1243745Ssethg * 15 0xf0-0xff 0xd0-0xdf
1250Sstevel@tonic-gate */
1260Sstevel@tonic-gate uchar_t apic_vectortoipl[APIC_AVAIL_VECTOR / APIC_VECTOR_PER_IPL] = {
1273745Ssethg 3, 4, 5, 5, 6, 6, 9, 10, 11, 12, 13, 14, 15, 15
1280Sstevel@tonic-gate };
1290Sstevel@tonic-gate /*
1303745Ssethg * The ipl of an ISR at vector X is apic_vectortoipl[X>>4]
1310Sstevel@tonic-gate * NOTE that this is vector as passed into intr_enter which is
1320Sstevel@tonic-gate * programmed vector - 0x20 (APIC_BASE_VECT)
1330Sstevel@tonic-gate */
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate uchar_t apic_ipltopri[MAXIPL + 1]; /* unix ipl to apic pri */
1360Sstevel@tonic-gate /* The taskpri to be programmed into apic to mask given ipl */
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate #if defined(__amd64)
1390Sstevel@tonic-gate uchar_t apic_cr8pri[MAXIPL + 1]; /* unix ipl to cr8 pri */
1400Sstevel@tonic-gate #endif
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate /*
1433745Ssethg * Correlation of the hardware vector to the IPL in use, initialized
1443745Ssethg * from apic_vectortoipl[] in apic_init(). The final IPLs may not correlate
1453745Ssethg * to the IPLs in apic_vectortoipl on some systems that share interrupt lines
1463745Ssethg * connected to errata-stricken IOAPICs
1473745Ssethg */
1483745Ssethg uchar_t apic_ipls[APIC_AVAIL_VECTOR];
1493745Ssethg
1503745Ssethg /*
1510Sstevel@tonic-gate * Patchable global variables.
1520Sstevel@tonic-gate */
1530Sstevel@tonic-gate int apic_enable_hwsoftint = 0; /* 0 - disable, 1 - enable */
1540Sstevel@tonic-gate int apic_enable_bind_log = 1; /* 1 - display interrupt binding log */
15512004Sjiang.liu@intel.com
1560Sstevel@tonic-gate /*
1570Sstevel@tonic-gate * Local static data
1580Sstevel@tonic-gate */
1590Sstevel@tonic-gate static struct psm_ops apic_ops = {
1600Sstevel@tonic-gate apic_probe,
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate apic_init,
1630Sstevel@tonic-gate apic_picinit,
1640Sstevel@tonic-gate apic_intr_enter,
1650Sstevel@tonic-gate apic_intr_exit,
1660Sstevel@tonic-gate apic_setspl,
1670Sstevel@tonic-gate apic_addspl,
1680Sstevel@tonic-gate apic_delspl,
1690Sstevel@tonic-gate apic_disable_intr,
1700Sstevel@tonic-gate apic_enable_intr,
1714652Scwb (int (*)(int))NULL, /* psm_softlvl_to_irq */
1724652Scwb (void (*)(int))NULL, /* psm_set_softintr */
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate apic_set_idlecpu,
1750Sstevel@tonic-gate apic_unset_idlecpu,
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate apic_clkinit,
1780Sstevel@tonic-gate apic_getclkirq,
1790Sstevel@tonic-gate (void (*)(void))NULL, /* psm_hrtimeinit */
1800Sstevel@tonic-gate apic_gethrtime,
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate apic_get_next_processorid,
1830Sstevel@tonic-gate apic_cpu_start,
1840Sstevel@tonic-gate apic_post_cpu_start,
1850Sstevel@tonic-gate apic_shutdown,
1860Sstevel@tonic-gate apic_get_ipivect,
1870Sstevel@tonic-gate apic_send_ipi,
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate (int (*)(dev_info_t *, int))NULL, /* psm_translate_irq */
1900Sstevel@tonic-gate (void (*)(int, char *))NULL, /* psm_notify_error */
1910Sstevel@tonic-gate (void (*)(int))NULL, /* psm_notify_func */
1920Sstevel@tonic-gate apic_timer_reprogram,
1930Sstevel@tonic-gate apic_timer_enable,
1940Sstevel@tonic-gate apic_timer_disable,
1950Sstevel@tonic-gate apic_post_cyclic_setup,
1960Sstevel@tonic-gate apic_preshutdown,
1975295Srandyf apic_intr_ops, /* Advanced DDI Interrupt framework */
1985295Srandyf apic_state, /* save, restore apic state for S3 */
19912004Sjiang.liu@intel.com apic_cpu_ops, /* CPU control interface. */
2000Sstevel@tonic-gate };
2010Sstevel@tonic-gate
20212683SJimmy.Vetayases@oracle.com struct psm_ops *psmops = &apic_ops;
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate static struct psm_info apic_psm_info = {
20512004Sjiang.liu@intel.com PSM_INFO_VER01_7, /* version */
2060Sstevel@tonic-gate PSM_OWN_EXCLUSIVE, /* ownership */
2070Sstevel@tonic-gate (struct psm_ops *)&apic_ops, /* operation */
2084397Sschwartz APIC_PCPLUSMP_NAME, /* machine name */
2096896Sdmick "pcplusmp v1.4 compatible",
2100Sstevel@tonic-gate };
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate static void *apic_hdlp;
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate /*
2150Sstevel@tonic-gate * apic_let_idle_redistribute can have the following values:
2160Sstevel@tonic-gate * 0 - If clock decremented it from 1 to 0, clock has to call redistribute.
2170Sstevel@tonic-gate * apic_redistribute_lock prevents multiple idle cpus from redistributing
2180Sstevel@tonic-gate */
2190Sstevel@tonic-gate int apic_num_idle_redistributions = 0;
2200Sstevel@tonic-gate static int apic_let_idle_redistribute = 0;
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate /* to gather intr data and redistribute */
2230Sstevel@tonic-gate static void apic_redistribute_compute(void);
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate /*
2260Sstevel@tonic-gate * This is the loadable module wrapper
2270Sstevel@tonic-gate */
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate int
_init(void)2300Sstevel@tonic-gate _init(void)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate if (apic_coarse_hrtime)
2330Sstevel@tonic-gate apic_ops.psm_gethrtime = &apic_gettime;
2340Sstevel@tonic-gate return (psm_mod_init(&apic_hdlp, &apic_psm_info));
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate int
_fini(void)2380Sstevel@tonic-gate _fini(void)
2390Sstevel@tonic-gate {
2400Sstevel@tonic-gate return (psm_mod_fini(&apic_hdlp, &apic_psm_info));
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate
2430Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2440Sstevel@tonic-gate _info(struct modinfo *modinfop)
2450Sstevel@tonic-gate {
2460Sstevel@tonic-gate return (psm_mod_info(&apic_hdlp, &apic_psm_info, modinfop));
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate
24912683SJimmy.Vetayases@oracle.com static int
apic_probe(void)25012683SJimmy.Vetayases@oracle.com apic_probe(void)
25112683SJimmy.Vetayases@oracle.com {
25212683SJimmy.Vetayases@oracle.com /* check if apix is initialized */
25312683SJimmy.Vetayases@oracle.com if (apix_enable && apix_loaded())
25412683SJimmy.Vetayases@oracle.com return (PSM_FAILURE);
25512683SJimmy.Vetayases@oracle.com else
25612683SJimmy.Vetayases@oracle.com apix_enable = 0; /* continue using pcplusmp PSM */
2570Sstevel@tonic-gate
2583446Smrj return (apic_probe_common(apic_psm_info.p_mach_idstring));
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate
26112683SJimmy.Vetayases@oracle.com static uchar_t
apic_xlate_vector_by_irq(uchar_t irq)26212683SJimmy.Vetayases@oracle.com apic_xlate_vector_by_irq(uchar_t irq)
26312683SJimmy.Vetayases@oracle.com {
26412683SJimmy.Vetayases@oracle.com if (apic_irq_table[irq] == NULL)
26512683SJimmy.Vetayases@oracle.com return (0);
26612683SJimmy.Vetayases@oracle.com
26712683SJimmy.Vetayases@oracle.com return (apic_irq_table[irq]->airq_vector);
26812683SJimmy.Vetayases@oracle.com }
26912683SJimmy.Vetayases@oracle.com
2700Sstevel@tonic-gate void
apic_init(void)27112683SJimmy.Vetayases@oracle.com apic_init(void)
2720Sstevel@tonic-gate {
2733446Smrj int i;
2743446Smrj int j = 1;
2750Sstevel@tonic-gate
27612683SJimmy.Vetayases@oracle.com psm_get_ioapicid = apic_get_ioapicid;
27712683SJimmy.Vetayases@oracle.com psm_get_localapicid = apic_get_localapicid;
27812683SJimmy.Vetayases@oracle.com psm_xlate_vector_by_irq = apic_xlate_vector_by_irq;
27912683SJimmy.Vetayases@oracle.com
2800Sstevel@tonic-gate apic_ipltopri[0] = APIC_VECTOR_PER_IPL; /* leave 0 for idle */
2810Sstevel@tonic-gate for (i = 0; i < (APIC_AVAIL_VECTOR / APIC_VECTOR_PER_IPL); i++) {
2820Sstevel@tonic-gate if ((i < ((APIC_AVAIL_VECTOR / APIC_VECTOR_PER_IPL) - 1)) &&
2830Sstevel@tonic-gate (apic_vectortoipl[i + 1] == apic_vectortoipl[i]))
2840Sstevel@tonic-gate /* get to highest vector at the same ipl */
2850Sstevel@tonic-gate continue;
2860Sstevel@tonic-gate for (; j <= apic_vectortoipl[i]; j++) {
2870Sstevel@tonic-gate apic_ipltopri[j] = (i << APIC_IPL_SHIFT) +
2880Sstevel@tonic-gate APIC_BASE_VECT;
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate for (; j < MAXIPL + 1; j++)
2920Sstevel@tonic-gate /* fill up any empty ipltopri slots */
2930Sstevel@tonic-gate apic_ipltopri[j] = (i << APIC_IPL_SHIFT) + APIC_BASE_VECT;
2943446Smrj apic_init_common();
2950Sstevel@tonic-gate #if defined(__amd64)
2960Sstevel@tonic-gate /*
2970Sstevel@tonic-gate * Make cpu-specific interrupt info point to cr8pri vector
2980Sstevel@tonic-gate */
2990Sstevel@tonic-gate for (i = 0; i <= MAXIPL; i++)
3000Sstevel@tonic-gate apic_cr8pri[i] = apic_ipltopri[i] >> APIC_IPL_SHIFT;
3010Sstevel@tonic-gate CPU->cpu_pri_data = apic_cr8pri;
30210080SJoe.Bonasera@sun.com #else
30310080SJoe.Bonasera@sun.com if (cpuid_have_cr8access(CPU))
30410080SJoe.Bonasera@sun.com apic_have_32bit_cr8 = 1;
3050Sstevel@tonic-gate #endif /* __amd64 */
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate
3080Sstevel@tonic-gate static void
apic_init_intr(void)30912683SJimmy.Vetayases@oracle.com apic_init_intr(void)
3100Sstevel@tonic-gate {
3110Sstevel@tonic-gate processorid_t cpun = psm_get_cpu_id();
3126896Sdmick uint_t nlvt;
3137282Smishra uint32_t svr = AV_UNIT_ENABLE | APIC_SPUR_INTR;
3140Sstevel@tonic-gate
3157282Smishra apic_reg_ops->apic_write_task_reg(APIC_MASK_ALL);
3160Sstevel@tonic-gate
3177282Smishra if (apic_mode == LOCAL_APIC) {
3187282Smishra /*
3197282Smishra * We are running APIC in MMIO mode.
3207282Smishra */
3217282Smishra if (apic_flat_model) {
3227282Smishra apic_reg_ops->apic_write(APIC_FORMAT_REG,
3237282Smishra APIC_FLAT_MODEL);
3247282Smishra } else {
3257282Smishra apic_reg_ops->apic_write(APIC_FORMAT_REG,
3267282Smishra APIC_CLUSTER_MODEL);
3277282Smishra }
3287282Smishra
3297282Smishra apic_reg_ops->apic_write(APIC_DEST_REG,
3307282Smishra AV_HIGH_ORDER >> cpun);
3317282Smishra }
3327282Smishra
33310345SSaurabh.Mishra@Sun.COM if (apic_directed_EOI_supported()) {
3347282Smishra /*
33510345SSaurabh.Mishra@Sun.COM * Setting the 12th bit in the Spurious Interrupt Vector
33610345SSaurabh.Mishra@Sun.COM * Register suppresses broadcast EOIs generated by the local
33710345SSaurabh.Mishra@Sun.COM * APIC. The suppression of broadcast EOIs happens only when
33810345SSaurabh.Mishra@Sun.COM * interrupts are level-triggered.
3397282Smishra */
34010345SSaurabh.Mishra@Sun.COM svr |= APIC_SVR_SUPPRESS_BROADCAST_EOI;
3417282Smishra }
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate /* need to enable APIC before unmasking NMI */
3447282Smishra apic_reg_ops->apic_write(APIC_SPUR_INT_REG, svr);
3450Sstevel@tonic-gate
3466896Sdmick /*
3476896Sdmick * Presence of an invalid vector with delivery mode AV_FIXED can
3486896Sdmick * cause an error interrupt, even if the entry is masked...so
3496896Sdmick * write a valid vector to LVT entries along with the mask bit
3506896Sdmick */
3516896Sdmick
3526896Sdmick /* All APICs have timer and LINT0/1 */
3537282Smishra apic_reg_ops->apic_write(APIC_LOCAL_TIMER, AV_MASK|APIC_RESV_IRQ);
3547282Smishra apic_reg_ops->apic_write(APIC_INT_VECT0, AV_MASK|APIC_RESV_IRQ);
3557282Smishra apic_reg_ops->apic_write(APIC_INT_VECT1, AV_NMI); /* enable NMI */
3560Sstevel@tonic-gate
3576896Sdmick /*
3586896Sdmick * On integrated APICs, the number of LVT entries is
3596896Sdmick * 'Max LVT entry' + 1; on 82489DX's (non-integrated
3606896Sdmick * APICs), nlvt is "3" (LINT0, LINT1, and timer)
3616896Sdmick */
3620Sstevel@tonic-gate
3636896Sdmick if (apic_cpus[cpun].aci_local_ver < APIC_INTEGRATED_VERS) {
3646896Sdmick nlvt = 3;
3656896Sdmick } else {
3668767SSaurabh.Mishra@Sun.COM nlvt = ((apic_reg_ops->apic_read(APIC_VERS_REG) >> 16) &
3678767SSaurabh.Mishra@Sun.COM 0xFF) + 1;
3686896Sdmick }
3696896Sdmick
3706896Sdmick if (nlvt >= 5) {
3716896Sdmick /* Enable performance counter overflow interrupt */
3726896Sdmick
37312826Skuriakose.kuruvilla@oracle.com if (!is_x86_feature(x86_featureset, X86FSET_MSR))
3746896Sdmick apic_enable_cpcovf_intr = 0;
3756896Sdmick if (apic_enable_cpcovf_intr) {
3766896Sdmick if (apic_cpcovf_vect == 0) {
3776896Sdmick int ipl = APIC_PCINT_IPL;
3786896Sdmick int irq = apic_get_ipivect(ipl, -1);
3790Sstevel@tonic-gate
3806896Sdmick ASSERT(irq != -1);
3816896Sdmick apic_cpcovf_vect =
3826896Sdmick apic_irq_table[irq]->airq_vector;
3836896Sdmick ASSERT(apic_cpcovf_vect);
3846896Sdmick (void) add_avintr(NULL, ipl,
3856896Sdmick (avfunc)kcpc_hw_overflow_intr,
3866896Sdmick "apic pcint", irq, NULL, NULL, NULL, NULL);
3876896Sdmick kcpc_hw_overflow_intr_installed = 1;
3886896Sdmick kcpc_hw_enable_cpc_intr =
3896896Sdmick apic_cpcovf_mask_clear;
3906896Sdmick }
3917282Smishra apic_reg_ops->apic_write(APIC_PCINT_VECT,
3927282Smishra apic_cpcovf_vect);
3936896Sdmick }
3946896Sdmick }
3950Sstevel@tonic-gate
3966896Sdmick if (nlvt >= 6) {
3976896Sdmick /* Only mask TM intr if the BIOS apparently doesn't use it */
3986896Sdmick
3996896Sdmick uint32_t lvtval;
4006896Sdmick
4017282Smishra lvtval = apic_reg_ops->apic_read(APIC_THERM_VECT);
4026896Sdmick if (((lvtval & AV_MASK) == AV_MASK) ||
4036896Sdmick ((lvtval & AV_DELIV_MODE) != AV_SMI)) {
4047282Smishra apic_reg_ops->apic_write(APIC_THERM_VECT,
4057282Smishra AV_MASK|APIC_RESV_IRQ);
4060Sstevel@tonic-gate }
4070Sstevel@tonic-gate }
4080Sstevel@tonic-gate
4090Sstevel@tonic-gate /* Enable error interrupt */
4100Sstevel@tonic-gate
4116896Sdmick if (nlvt >= 4 && apic_enable_error_intr) {
4120Sstevel@tonic-gate if (apic_errvect == 0) {
4130Sstevel@tonic-gate int ipl = 0xf; /* get highest priority intr */
4140Sstevel@tonic-gate int irq = apic_get_ipivect(ipl, -1);
4150Sstevel@tonic-gate
4160Sstevel@tonic-gate ASSERT(irq != -1);
4170Sstevel@tonic-gate apic_errvect = apic_irq_table[irq]->airq_vector;
4180Sstevel@tonic-gate ASSERT(apic_errvect);
4190Sstevel@tonic-gate /*
4200Sstevel@tonic-gate * Not PSMI compliant, but we are going to merge
4210Sstevel@tonic-gate * with ON anyway
4220Sstevel@tonic-gate */
4230Sstevel@tonic-gate (void) add_avintr((void *)NULL, ipl,
4240Sstevel@tonic-gate (avfunc)apic_error_intr, "apic error intr",
425916Sschwartz irq, NULL, NULL, NULL, NULL);
4260Sstevel@tonic-gate }
4277282Smishra apic_reg_ops->apic_write(APIC_ERR_VECT, apic_errvect);
4287282Smishra apic_reg_ops->apic_write(APIC_ERROR_STATUS, 0);
4297282Smishra apic_reg_ops->apic_write(APIC_ERROR_STATUS, 0);
4300Sstevel@tonic-gate }
4316896Sdmick
4327349SAdrian.Frost@Sun.COM /* Enable CMCI interrupt */
4337349SAdrian.Frost@Sun.COM if (cmi_enable_cmci) {
4347349SAdrian.Frost@Sun.COM
4357349SAdrian.Frost@Sun.COM mutex_enter(&cmci_cpu_setup_lock);
4367349SAdrian.Frost@Sun.COM if (cmci_cpu_setup_registered == 0) {
4377349SAdrian.Frost@Sun.COM mutex_enter(&cpu_lock);
4387349SAdrian.Frost@Sun.COM register_cpu_setup_func(cmci_cpu_setup, NULL);
4397349SAdrian.Frost@Sun.COM mutex_exit(&cpu_lock);
4407349SAdrian.Frost@Sun.COM cmci_cpu_setup_registered = 1;
4417349SAdrian.Frost@Sun.COM }
4427349SAdrian.Frost@Sun.COM mutex_exit(&cmci_cpu_setup_lock);
4437349SAdrian.Frost@Sun.COM
4447349SAdrian.Frost@Sun.COM if (apic_cmci_vect == 0) {
4457349SAdrian.Frost@Sun.COM int ipl = 0x2;
4467349SAdrian.Frost@Sun.COM int irq = apic_get_ipivect(ipl, -1);
4477349SAdrian.Frost@Sun.COM
4487349SAdrian.Frost@Sun.COM ASSERT(irq != -1);
4497349SAdrian.Frost@Sun.COM apic_cmci_vect = apic_irq_table[irq]->airq_vector;
4507349SAdrian.Frost@Sun.COM ASSERT(apic_cmci_vect);
4517349SAdrian.Frost@Sun.COM
4527349SAdrian.Frost@Sun.COM (void) add_avintr(NULL, ipl,
4537349SAdrian.Frost@Sun.COM (avfunc)cmi_cmci_trap,
4547349SAdrian.Frost@Sun.COM "apic cmci intr", irq, NULL, NULL, NULL, NULL);
4557349SAdrian.Frost@Sun.COM }
4567349SAdrian.Frost@Sun.COM apic_reg_ops->apic_write(APIC_CMCI_VECT, apic_cmci_vect);
4577349SAdrian.Frost@Sun.COM }
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate
4600Sstevel@tonic-gate static void
apic_picinit(void)4610Sstevel@tonic-gate apic_picinit(void)
4620Sstevel@tonic-gate {
4633446Smrj int i, j;
4640Sstevel@tonic-gate uint_t isr;
4650Sstevel@tonic-gate
4660Sstevel@tonic-gate /*
46711383SSaurabh.Mishra@Sun.COM * Initialize and enable interrupt remapping before apic
4689940SVikram.Hegde@Sun.COM * hardware initialization
4699940SVikram.Hegde@Sun.COM */
47011600SVikram.Hegde@Sun.COM apic_intrmap_init(apic_mode);
4719940SVikram.Hegde@Sun.COM
4729940SVikram.Hegde@Sun.COM /*
4730Sstevel@tonic-gate * On UniSys Model 6520, the BIOS leaves vector 0x20 isr
4740Sstevel@tonic-gate * bit on without clearing it with EOI. Since softint
4750Sstevel@tonic-gate * uses vector 0x20 to interrupt itself, so softint will
4760Sstevel@tonic-gate * not work on this machine. In order to fix this problem
4770Sstevel@tonic-gate * a check is made to verify all the isr bits are clear.
4780Sstevel@tonic-gate * If not, EOIs are issued to clear the bits.
4790Sstevel@tonic-gate */
4800Sstevel@tonic-gate for (i = 7; i >= 1; i--) {
4817282Smishra isr = apic_reg_ops->apic_read(APIC_ISR_REG + (i * 4));
4827282Smishra if (isr != 0)
4830Sstevel@tonic-gate for (j = 0; ((j < 32) && (isr != 0)); j++)
4840Sstevel@tonic-gate if (isr & (1 << j)) {
4857282Smishra apic_reg_ops->apic_write(
4867282Smishra APIC_EOI_REG, 0);
4870Sstevel@tonic-gate isr &= ~(1 << j);
4880Sstevel@tonic-gate apic_error |= APIC_ERR_BOOT_EOI;
4890Sstevel@tonic-gate }
4900Sstevel@tonic-gate }
4910Sstevel@tonic-gate
4920Sstevel@tonic-gate /* set a flag so we know we have run apic_picinit() */
4935084Sjohnlev apic_picinit_called = 1;
4940Sstevel@tonic-gate LOCK_INIT_CLEAR(&apic_gethrtime_lock);
4950Sstevel@tonic-gate LOCK_INIT_CLEAR(&apic_ioapic_lock);
4960Sstevel@tonic-gate LOCK_INIT_CLEAR(&apic_error_lock);
49712004Sjiang.liu@intel.com LOCK_INIT_CLEAR(&apic_mode_switch_lock);
4980Sstevel@tonic-gate
4990Sstevel@tonic-gate picsetup(); /* initialise the 8259 */
5000Sstevel@tonic-gate
5010Sstevel@tonic-gate /* add nmi handler - least priority nmi handler */
5020Sstevel@tonic-gate LOCK_INIT_CLEAR(&apic_nmi_lock);
5030Sstevel@tonic-gate
5040Sstevel@tonic-gate if (!psm_add_nmintr(0, (avfunc) apic_nmi_intr,
5050Sstevel@tonic-gate "pcplusmp NMI handler", (caddr_t)NULL))
5060Sstevel@tonic-gate cmn_err(CE_WARN, "pcplusmp: Unable to add nmi handler");
5070Sstevel@tonic-gate
50811383SSaurabh.Mishra@Sun.COM /*
50911383SSaurabh.Mishra@Sun.COM * Check for directed-EOI capability in the local APIC.
51011383SSaurabh.Mishra@Sun.COM */
51111383SSaurabh.Mishra@Sun.COM if (apic_directed_EOI_supported() == 1) {
51211383SSaurabh.Mishra@Sun.COM apic_set_directed_EOI_handler();
51311383SSaurabh.Mishra@Sun.COM }
51411383SSaurabh.Mishra@Sun.COM
5150Sstevel@tonic-gate apic_init_intr();
5160Sstevel@tonic-gate
5170Sstevel@tonic-gate /* enable apic mode if imcr present */
5180Sstevel@tonic-gate if (apic_imcrp) {
5190Sstevel@tonic-gate outb(APIC_IMCR_P1, (uchar_t)APIC_IMCR_SELECT);
5200Sstevel@tonic-gate outb(APIC_IMCR_P2, (uchar_t)APIC_IMCR_APIC);
5210Sstevel@tonic-gate }
5220Sstevel@tonic-gate
5233446Smrj ioapic_init_intr(IOAPIC_MASK);
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate
5267798SSaurabh.Mishra@Sun.COM #ifdef DEBUG
5270Sstevel@tonic-gate void
apic_break(void)52812683SJimmy.Vetayases@oracle.com apic_break(void)
5290Sstevel@tonic-gate {
5300Sstevel@tonic-gate }
5310Sstevel@tonic-gate #endif /* DEBUG */
5320Sstevel@tonic-gate
5330Sstevel@tonic-gate /*
5340Sstevel@tonic-gate * platform_intr_enter
5350Sstevel@tonic-gate *
5360Sstevel@tonic-gate * Called at the beginning of the interrupt service routine to
5370Sstevel@tonic-gate * mask all level equal to and below the interrupt priority
5380Sstevel@tonic-gate * of the interrupting vector. An EOI should be given to
5390Sstevel@tonic-gate * the interrupt controller to enable other HW interrupts.
5400Sstevel@tonic-gate *
5410Sstevel@tonic-gate * Return -1 for spurious interrupts
5420Sstevel@tonic-gate *
5430Sstevel@tonic-gate */
5440Sstevel@tonic-gate /*ARGSUSED*/
5450Sstevel@tonic-gate static int
apic_intr_enter(int ipl,int * vectorp)5460Sstevel@tonic-gate apic_intr_enter(int ipl, int *vectorp)
5470Sstevel@tonic-gate {
5480Sstevel@tonic-gate uchar_t vector;
5490Sstevel@tonic-gate int nipl;
5503446Smrj int irq;
5513446Smrj ulong_t iflag;
5520Sstevel@tonic-gate apic_cpus_info_t *cpu_infop;
5530Sstevel@tonic-gate
5540Sstevel@tonic-gate /*
5553745Ssethg * The real vector delivered is (*vectorp + 0x20), but our caller
5563745Ssethg * subtracts 0x20 from the vector before passing it to us.
5573745Ssethg * (That's why APIC_BASE_VECT is 0x20.)
5580Sstevel@tonic-gate */
5590Sstevel@tonic-gate vector = (uchar_t)*vectorp;
5600Sstevel@tonic-gate
5610Sstevel@tonic-gate /* if interrupted by the clock, increment apic_nsec_since_boot */
5620Sstevel@tonic-gate if (vector == apic_clkvect) {
5630Sstevel@tonic-gate if (!apic_oneshot) {
5640Sstevel@tonic-gate /* NOTE: this is not MT aware */
5650Sstevel@tonic-gate apic_hrtime_stamp++;
5660Sstevel@tonic-gate apic_nsec_since_boot += apic_nsec_per_intr;
5670Sstevel@tonic-gate apic_hrtime_stamp++;
5680Sstevel@tonic-gate last_count_read = apic_hertz_count;
5690Sstevel@tonic-gate apic_redistribute_compute();
5700Sstevel@tonic-gate }
5710Sstevel@tonic-gate
5720Sstevel@tonic-gate /* We will avoid all the book keeping overhead for clock */
5733745Ssethg nipl = apic_ipls[vector];
5743745Ssethg
5757282Smishra *vectorp = apic_vector_to_irq[vector + APIC_BASE_VECT];
5767282Smishra if (apic_mode == LOCAL_APIC) {
5770Sstevel@tonic-gate #if defined(__amd64)
5787282Smishra setcr8((ulong_t)(apic_ipltopri[nipl] >>
5797282Smishra APIC_IPL_SHIFT));
5800Sstevel@tonic-gate #else
58110080SJoe.Bonasera@sun.com if (apic_have_32bit_cr8)
58210080SJoe.Bonasera@sun.com setcr8((ulong_t)(apic_ipltopri[nipl] >>
58310080SJoe.Bonasera@sun.com APIC_IPL_SHIFT));
58410080SJoe.Bonasera@sun.com else
58510080SJoe.Bonasera@sun.com LOCAL_APIC_WRITE_REG(APIC_TASK_REG,
58610080SJoe.Bonasera@sun.com (uint32_t)apic_ipltopri[nipl]);
5870Sstevel@tonic-gate #endif
5887282Smishra LOCAL_APIC_WRITE_REG(APIC_EOI_REG, 0);
5897282Smishra } else {
5907282Smishra X2APIC_WRITE(APIC_TASK_REG, apic_ipltopri[nipl]);
5917282Smishra X2APIC_WRITE(APIC_EOI_REG, 0);
5927282Smishra }
5937282Smishra
5940Sstevel@tonic-gate return (nipl);
5950Sstevel@tonic-gate }
5960Sstevel@tonic-gate
5970Sstevel@tonic-gate cpu_infop = &apic_cpus[psm_get_cpu_id()];
5980Sstevel@tonic-gate
5990Sstevel@tonic-gate if (vector == (APIC_SPUR_INTR - APIC_BASE_VECT)) {
6000Sstevel@tonic-gate cpu_infop->aci_spur_cnt++;
6010Sstevel@tonic-gate return (APIC_INT_SPURIOUS);
6020Sstevel@tonic-gate }
6030Sstevel@tonic-gate
6040Sstevel@tonic-gate /* Check if the vector we got is really what we need */
6050Sstevel@tonic-gate if (apic_revector_pending) {
6060Sstevel@tonic-gate /*
6070Sstevel@tonic-gate * Disable interrupts for the duration of
6080Sstevel@tonic-gate * the vector translation to prevent a self-race for
6090Sstevel@tonic-gate * the apic_revector_lock. This cannot be done
6100Sstevel@tonic-gate * in apic_xlate_vector because it is recursive and
6110Sstevel@tonic-gate * we want the vector translation to be atomic with
6120Sstevel@tonic-gate * respect to other (higher-priority) interrupts.
6130Sstevel@tonic-gate */
6140Sstevel@tonic-gate iflag = intr_clear();
6150Sstevel@tonic-gate vector = apic_xlate_vector(vector + APIC_BASE_VECT) -
6160Sstevel@tonic-gate APIC_BASE_VECT;
6170Sstevel@tonic-gate intr_restore(iflag);
6180Sstevel@tonic-gate }
6190Sstevel@tonic-gate
6203745Ssethg nipl = apic_ipls[vector];
6210Sstevel@tonic-gate *vectorp = irq = apic_vector_to_irq[vector + APIC_BASE_VECT];
6220Sstevel@tonic-gate
6237282Smishra if (apic_mode == LOCAL_APIC) {
6240Sstevel@tonic-gate #if defined(__amd64)
6257282Smishra setcr8((ulong_t)(apic_ipltopri[nipl] >> APIC_IPL_SHIFT));
6260Sstevel@tonic-gate #else
62710080SJoe.Bonasera@sun.com if (apic_have_32bit_cr8)
62810080SJoe.Bonasera@sun.com setcr8((ulong_t)(apic_ipltopri[nipl] >>
62910080SJoe.Bonasera@sun.com APIC_IPL_SHIFT));
63010080SJoe.Bonasera@sun.com else
63110080SJoe.Bonasera@sun.com LOCAL_APIC_WRITE_REG(APIC_TASK_REG,
63210080SJoe.Bonasera@sun.com (uint32_t)apic_ipltopri[nipl]);
6330Sstevel@tonic-gate #endif
6347282Smishra } else {
6357282Smishra X2APIC_WRITE(APIC_TASK_REG, apic_ipltopri[nipl]);
6367282Smishra }
6370Sstevel@tonic-gate
6380Sstevel@tonic-gate cpu_infop->aci_current[nipl] = (uchar_t)irq;
6390Sstevel@tonic-gate cpu_infop->aci_curipl = (uchar_t)nipl;
6400Sstevel@tonic-gate cpu_infop->aci_ISR_in_progress |= 1 << nipl;
6410Sstevel@tonic-gate
6420Sstevel@tonic-gate /*
6430Sstevel@tonic-gate * apic_level_intr could have been assimilated into the irq struct.
6440Sstevel@tonic-gate * but, having it as a character array is more efficient in terms of
6450Sstevel@tonic-gate * cache usage. So, we leave it as is.
6460Sstevel@tonic-gate */
6477282Smishra if (!apic_level_intr[irq]) {
6488918SSaurabh.Mishra@Sun.COM if (apic_mode == LOCAL_APIC) {
6497282Smishra LOCAL_APIC_WRITE_REG(APIC_EOI_REG, 0);
6508918SSaurabh.Mishra@Sun.COM } else {
6517282Smishra X2APIC_WRITE(APIC_EOI_REG, 0);
6528918SSaurabh.Mishra@Sun.COM }
6537282Smishra }
6540Sstevel@tonic-gate
6550Sstevel@tonic-gate #ifdef DEBUG
6560Sstevel@tonic-gate APIC_DEBUG_BUF_PUT(vector);
6570Sstevel@tonic-gate APIC_DEBUG_BUF_PUT(irq);
6580Sstevel@tonic-gate APIC_DEBUG_BUF_PUT(nipl);
6590Sstevel@tonic-gate APIC_DEBUG_BUF_PUT(psm_get_cpu_id());
6600Sstevel@tonic-gate if ((apic_stretch_interrupts) && (apic_stretch_ISR & (1 << nipl)))
6610Sstevel@tonic-gate drv_usecwait(apic_stretch_interrupts);
6620Sstevel@tonic-gate
6630Sstevel@tonic-gate if (apic_break_on_cpu == psm_get_cpu_id())
6640Sstevel@tonic-gate apic_break();
6650Sstevel@tonic-gate #endif /* DEBUG */
6660Sstevel@tonic-gate return (nipl);
6670Sstevel@tonic-gate }
6680Sstevel@tonic-gate
6697282Smishra /*
6707986SSaurabh.Mishra@Sun.COM * This macro is a common code used by MMIO local apic and X2APIC
6717282Smishra * local apic.
6727282Smishra */
6737282Smishra #define APIC_INTR_EXIT() \
6747282Smishra { \
6757282Smishra cpu_infop = &apic_cpus[psm_get_cpu_id()]; \
6767282Smishra if (apic_level_intr[irq]) \
6777282Smishra apic_reg_ops->apic_send_eoi(irq); \
6787282Smishra cpu_infop->aci_curipl = (uchar_t)prev_ipl; \
6797282Smishra /* ISR above current pri could not be in progress */ \
6807282Smishra cpu_infop->aci_ISR_in_progress &= (2 << prev_ipl) - 1; \
6817282Smishra }
6827282Smishra
6837282Smishra /*
6847986SSaurabh.Mishra@Sun.COM * Any changes made to this function must also change X2APIC
6857282Smishra * version of intr_exit.
6867282Smishra */
6873446Smrj void
apic_intr_exit(int prev_ipl,int irq)6880Sstevel@tonic-gate apic_intr_exit(int prev_ipl, int irq)
6890Sstevel@tonic-gate {
6900Sstevel@tonic-gate apic_cpus_info_t *cpu_infop;
6910Sstevel@tonic-gate
6920Sstevel@tonic-gate #if defined(__amd64)
6930Sstevel@tonic-gate setcr8((ulong_t)apic_cr8pri[prev_ipl]);
6940Sstevel@tonic-gate #else
69510080SJoe.Bonasera@sun.com if (apic_have_32bit_cr8)
69610080SJoe.Bonasera@sun.com setcr8((ulong_t)(apic_ipltopri[prev_ipl] >> APIC_IPL_SHIFT));
69710080SJoe.Bonasera@sun.com else
69810080SJoe.Bonasera@sun.com apicadr[APIC_TASK_REG] = apic_ipltopri[prev_ipl];
6990Sstevel@tonic-gate #endif
7000Sstevel@tonic-gate
7017282Smishra APIC_INTR_EXIT();
7027282Smishra }
7030Sstevel@tonic-gate
7047282Smishra /*
7057282Smishra * Same as apic_intr_exit() except it uses MSR rather than MMIO
7067282Smishra * to access local apic registers.
7077282Smishra */
7087282Smishra void
x2apic_intr_exit(int prev_ipl,int irq)7097282Smishra x2apic_intr_exit(int prev_ipl, int irq)
7107282Smishra {
7117282Smishra apic_cpus_info_t *cpu_infop;
7127282Smishra
7137282Smishra X2APIC_WRITE(APIC_TASK_REG, apic_ipltopri[prev_ipl]);
7147282Smishra APIC_INTR_EXIT();
7150Sstevel@tonic-gate }
7160Sstevel@tonic-gate
7175084Sjohnlev intr_exit_fn_t
psm_intr_exit_fn(void)7185084Sjohnlev psm_intr_exit_fn(void)
7195084Sjohnlev {
7207282Smishra if (apic_mode == LOCAL_X2APIC)
7217282Smishra return (x2apic_intr_exit);
7227282Smishra
7235084Sjohnlev return (apic_intr_exit);
7245084Sjohnlev }
7255084Sjohnlev
7260Sstevel@tonic-gate /*
7277282Smishra * Mask all interrupts below or equal to the given IPL.
7287986SSaurabh.Mishra@Sun.COM * Any changes made to this function must also change X2APIC
7297282Smishra * version of setspl.
7300Sstevel@tonic-gate */
7310Sstevel@tonic-gate static void
apic_setspl(int ipl)7320Sstevel@tonic-gate apic_setspl(int ipl)
7330Sstevel@tonic-gate {
7340Sstevel@tonic-gate #if defined(__amd64)
7350Sstevel@tonic-gate setcr8((ulong_t)apic_cr8pri[ipl]);
7360Sstevel@tonic-gate #else
73710080SJoe.Bonasera@sun.com if (apic_have_32bit_cr8)
73810080SJoe.Bonasera@sun.com setcr8((ulong_t)(apic_ipltopri[ipl] >> APIC_IPL_SHIFT));
73910080SJoe.Bonasera@sun.com else
74010080SJoe.Bonasera@sun.com apicadr[APIC_TASK_REG] = apic_ipltopri[ipl];
7410Sstevel@tonic-gate #endif
7420Sstevel@tonic-gate
7430Sstevel@tonic-gate /* interrupts at ipl above this cannot be in progress */
7440Sstevel@tonic-gate apic_cpus[psm_get_cpu_id()].aci_ISR_in_progress &= (2 << ipl) - 1;
7450Sstevel@tonic-gate /*
7460Sstevel@tonic-gate * this is a patch fix for the ALR QSMP P5 machine, so that interrupts
7470Sstevel@tonic-gate * have enough time to come in before the priority is raised again
7480Sstevel@tonic-gate * during the idle() loop.
7490Sstevel@tonic-gate */
7500Sstevel@tonic-gate if (apic_setspl_delay)
7517282Smishra (void) apic_reg_ops->apic_get_pri();
7527282Smishra }
7537282Smishra
7547282Smishra /*
7557986SSaurabh.Mishra@Sun.COM * X2APIC version of setspl.
7567282Smishra * Mask all interrupts below or equal to the given IPL
7577282Smishra */
7587282Smishra static void
x2apic_setspl(int ipl)7597282Smishra x2apic_setspl(int ipl)
7607282Smishra {
7617282Smishra X2APIC_WRITE(APIC_TASK_REG, apic_ipltopri[ipl]);
7627282Smishra
7637282Smishra /* interrupts at ipl above this cannot be in progress */
7647282Smishra apic_cpus[psm_get_cpu_id()].aci_ISR_in_progress &= (2 << ipl) - 1;
7650Sstevel@tonic-gate }
7660Sstevel@tonic-gate
7670Sstevel@tonic-gate /*ARGSUSED*/
7680Sstevel@tonic-gate static int
apic_addspl(int irqno,int ipl,int min_ipl,int max_ipl)7690Sstevel@tonic-gate apic_addspl(int irqno, int ipl, int min_ipl, int max_ipl)
7700Sstevel@tonic-gate {
7713446Smrj return (apic_addspl_common(irqno, ipl, min_ipl, max_ipl));
7720Sstevel@tonic-gate }
7730Sstevel@tonic-gate
7740Sstevel@tonic-gate static int
apic_delspl(int irqno,int ipl,int min_ipl,int max_ipl)7750Sstevel@tonic-gate apic_delspl(int irqno, int ipl, int min_ipl, int max_ipl)
7760Sstevel@tonic-gate {
7773446Smrj return (apic_delspl_common(irqno, ipl, min_ipl, max_ipl));
7780Sstevel@tonic-gate }
7790Sstevel@tonic-gate
7800Sstevel@tonic-gate static int
apic_post_cpu_start(void)78112683SJimmy.Vetayases@oracle.com apic_post_cpu_start(void)
7820Sstevel@tonic-gate {
7836749Ssherrym int cpun;
7847986SSaurabh.Mishra@Sun.COM static int cpus_started = 1;
7857986SSaurabh.Mishra@Sun.COM
7867986SSaurabh.Mishra@Sun.COM /* We know this CPU + BSP started successfully. */
7877986SSaurabh.Mishra@Sun.COM cpus_started++;
7880Sstevel@tonic-gate
7897798SSaurabh.Mishra@Sun.COM /*
7907798SSaurabh.Mishra@Sun.COM * On BSP we would have enabled X2APIC, if supported by processor,
7917798SSaurabh.Mishra@Sun.COM * in acpi_probe(), but on AP we do it here.
7927986SSaurabh.Mishra@Sun.COM *
7937986SSaurabh.Mishra@Sun.COM * We enable X2APIC mode only if BSP is running in X2APIC & the
7947986SSaurabh.Mishra@Sun.COM * local APIC mode of the current CPU is MMIO (xAPIC).
7957798SSaurabh.Mishra@Sun.COM */
7967986SSaurabh.Mishra@Sun.COM if (apic_mode == LOCAL_X2APIC && apic_detect_x2apic() &&
7977986SSaurabh.Mishra@Sun.COM apic_local_mode() == LOCAL_APIC) {
7987798SSaurabh.Mishra@Sun.COM apic_enable_x2apic();
7997798SSaurabh.Mishra@Sun.COM }
8007798SSaurabh.Mishra@Sun.COM
8017986SSaurabh.Mishra@Sun.COM /*
80212004Sjiang.liu@intel.com * Switch back to x2apic IPI sending method for performance when target
80312004Sjiang.liu@intel.com * CPU has entered x2apic mode.
8047986SSaurabh.Mishra@Sun.COM */
80512004Sjiang.liu@intel.com if (apic_mode == LOCAL_X2APIC) {
80612004Sjiang.liu@intel.com apic_switch_ipi_callback(B_FALSE);
8077986SSaurabh.Mishra@Sun.COM }
8087986SSaurabh.Mishra@Sun.COM
8097798SSaurabh.Mishra@Sun.COM splx(ipltospl(LOCK_LEVEL));
8100Sstevel@tonic-gate apic_init_intr();
8110Sstevel@tonic-gate
8120Sstevel@tonic-gate /*
8130Sstevel@tonic-gate * since some systems don't enable the internal cache on the non-boot
8140Sstevel@tonic-gate * cpus, so we have to enable them here
8150Sstevel@tonic-gate */
8163446Smrj setcr0(getcr0() & ~(CR0_CD | CR0_NW));
8170Sstevel@tonic-gate
8187798SSaurabh.Mishra@Sun.COM #ifdef DEBUG
8197798SSaurabh.Mishra@Sun.COM APIC_AV_PENDING_SET();
8207798SSaurabh.Mishra@Sun.COM #else
8217798SSaurabh.Mishra@Sun.COM if (apic_mode == LOCAL_APIC)
8227798SSaurabh.Mishra@Sun.COM APIC_AV_PENDING_SET();
8237798SSaurabh.Mishra@Sun.COM #endif /* DEBUG */
8240Sstevel@tonic-gate
8257113Sbholler /*
8267113Sbholler * We may be booting, or resuming from suspend; aci_status will
8277113Sbholler * be APIC_CPU_INTR_ENABLE if coming from suspend, so we add the
8287113Sbholler * APIC_CPU_ONLINE flag here rather than setting aci_status completely.
8297113Sbholler */
8300Sstevel@tonic-gate cpun = psm_get_cpu_id();
8317113Sbholler apic_cpus[cpun].aci_status |= APIC_CPU_ONLINE;
8320Sstevel@tonic-gate
8337282Smishra apic_reg_ops->apic_write(APIC_DIVIDE_REG, apic_divide_reg_init);
8340Sstevel@tonic-gate return (PSM_SUCCESS);
8350Sstevel@tonic-gate }
8360Sstevel@tonic-gate
8370Sstevel@tonic-gate /*
8380Sstevel@tonic-gate * type == -1 indicates it is an internal request. Do not change
8390Sstevel@tonic-gate * resv_vector for these requests
8400Sstevel@tonic-gate */
8410Sstevel@tonic-gate static int
apic_get_ipivect(int ipl,int type)8420Sstevel@tonic-gate apic_get_ipivect(int ipl, int type)
8430Sstevel@tonic-gate {
8440Sstevel@tonic-gate uchar_t vector;
8450Sstevel@tonic-gate int irq;
8460Sstevel@tonic-gate
84711465SKerry.Shu@Sun.COM if ((irq = apic_allocate_irq(APIC_VECTOR(ipl))) != -1) {
8480Sstevel@tonic-gate if (vector = apic_allocate_vector(ipl, irq, 1)) {
8490Sstevel@tonic-gate apic_irq_table[irq]->airq_mps_intr_index =
8500Sstevel@tonic-gate RESERVE_INDEX;
8510Sstevel@tonic-gate apic_irq_table[irq]->airq_vector = vector;
8520Sstevel@tonic-gate if (type != -1) {
8530Sstevel@tonic-gate apic_resv_vector[ipl] = vector;
8540Sstevel@tonic-gate }
8550Sstevel@tonic-gate return (irq);
8560Sstevel@tonic-gate }
8570Sstevel@tonic-gate }
8580Sstevel@tonic-gate apic_error |= APIC_ERR_GET_IPIVECT_FAIL;
8590Sstevel@tonic-gate return (-1); /* shouldn't happen */
8600Sstevel@tonic-gate }
8610Sstevel@tonic-gate
8620Sstevel@tonic-gate static int
apic_getclkirq(int ipl)8630Sstevel@tonic-gate apic_getclkirq(int ipl)
8640Sstevel@tonic-gate {
8650Sstevel@tonic-gate int irq;
8660Sstevel@tonic-gate
8670Sstevel@tonic-gate if ((irq = apic_get_ipivect(ipl, -1)) == -1)
8680Sstevel@tonic-gate return (-1);
8690Sstevel@tonic-gate /*
8700Sstevel@tonic-gate * Note the vector in apic_clkvect for per clock handling.
8710Sstevel@tonic-gate */
8720Sstevel@tonic-gate apic_clkvect = apic_irq_table[irq]->airq_vector - APIC_BASE_VECT;
8730Sstevel@tonic-gate APIC_VERBOSE_IOAPIC((CE_NOTE, "get_clkirq: vector = %x\n",
8740Sstevel@tonic-gate apic_clkvect));
8750Sstevel@tonic-gate return (irq);
8760Sstevel@tonic-gate }
8770Sstevel@tonic-gate
8780Sstevel@tonic-gate /*
8790Sstevel@tonic-gate * Try and disable all interrupts. We just assign interrupts to other
8800Sstevel@tonic-gate * processors based on policy. If any were bound by user request, we
8810Sstevel@tonic-gate * let them continue and return failure. We do not bother to check
8820Sstevel@tonic-gate * for cache affinity while rebinding.
8830Sstevel@tonic-gate */
8840Sstevel@tonic-gate
8850Sstevel@tonic-gate static int
apic_disable_intr(processorid_t cpun)8860Sstevel@tonic-gate apic_disable_intr(processorid_t cpun)
8870Sstevel@tonic-gate {
8883446Smrj int bind_cpu = 0, i, hardbound = 0;
8890Sstevel@tonic-gate apic_irq_t *irq_ptr;
8903446Smrj ulong_t iflag;
8910Sstevel@tonic-gate
8920Sstevel@tonic-gate iflag = intr_clear();
8930Sstevel@tonic-gate lock_set(&apic_ioapic_lock);
8943139Ssethg
8953139Ssethg for (i = 0; i <= APIC_MAX_VECTOR; i++) {
8963139Ssethg if (apic_reprogram_info[i].done == B_FALSE) {
8973139Ssethg if (apic_reprogram_info[i].bindcpu == cpun) {
8983139Ssethg /*
8993139Ssethg * CPU is busy -- it's the target of
9003139Ssethg * a pending reprogramming attempt
9013139Ssethg */
9023139Ssethg lock_clear(&apic_ioapic_lock);
9033139Ssethg intr_restore(iflag);
9043139Ssethg return (PSM_FAILURE);
9053139Ssethg }
9063139Ssethg }
9073139Ssethg }
9083139Ssethg
9090Sstevel@tonic-gate apic_cpus[cpun].aci_status &= ~APIC_CPU_INTR_ENABLE;
9103139Ssethg
9110Sstevel@tonic-gate apic_cpus[cpun].aci_curipl = 0;
9123139Ssethg
9130Sstevel@tonic-gate i = apic_min_device_irq;
9140Sstevel@tonic-gate for (; i <= apic_max_device_irq; i++) {
9150Sstevel@tonic-gate /*
9160Sstevel@tonic-gate * If there are bound interrupts on this cpu, then
9170Sstevel@tonic-gate * rebind them to other processors.
9180Sstevel@tonic-gate */
9190Sstevel@tonic-gate if ((irq_ptr = apic_irq_table[i]) != NULL) {
9200Sstevel@tonic-gate ASSERT((irq_ptr->airq_temp_cpu == IRQ_UNBOUND) ||
9210Sstevel@tonic-gate (irq_ptr->airq_temp_cpu == IRQ_UNINIT) ||
92212004Sjiang.liu@intel.com (apic_cpu_in_range(irq_ptr->airq_temp_cpu)));
9230Sstevel@tonic-gate
9240Sstevel@tonic-gate if (irq_ptr->airq_temp_cpu == (cpun | IRQ_USER_BOUND)) {
9250Sstevel@tonic-gate hardbound = 1;
9260Sstevel@tonic-gate continue;
9270Sstevel@tonic-gate }
9280Sstevel@tonic-gate
9290Sstevel@tonic-gate if (irq_ptr->airq_temp_cpu == cpun) {
9300Sstevel@tonic-gate do {
93112204Sjiang.liu@intel.com bind_cpu =
93212204Sjiang.liu@intel.com apic_find_cpu(APIC_CPU_INTR_ENABLE);
9333139Ssethg } while (apic_rebind_all(irq_ptr, bind_cpu));
9340Sstevel@tonic-gate }
9350Sstevel@tonic-gate }
9360Sstevel@tonic-gate }
9373139Ssethg
9383139Ssethg lock_clear(&apic_ioapic_lock);
9393139Ssethg intr_restore(iflag);
9403139Ssethg
9410Sstevel@tonic-gate if (hardbound) {
9420Sstevel@tonic-gate cmn_err(CE_WARN, "Could not disable interrupts on %d"
9430Sstevel@tonic-gate "due to user bound interrupts", cpun);
9440Sstevel@tonic-gate return (PSM_FAILURE);
9450Sstevel@tonic-gate }
9460Sstevel@tonic-gate else
9470Sstevel@tonic-gate return (PSM_SUCCESS);
9480Sstevel@tonic-gate }
9490Sstevel@tonic-gate
9507113Sbholler /*
9517113Sbholler * Bind interrupts to the CPU's local APIC.
9527113Sbholler * Interrupts should not be bound to a CPU's local APIC until the CPU
9537113Sbholler * is ready to receive interrupts.
9547113Sbholler */
9550Sstevel@tonic-gate static void
apic_enable_intr(processorid_t cpun)9560Sstevel@tonic-gate apic_enable_intr(processorid_t cpun)
9570Sstevel@tonic-gate {
9583446Smrj int i;
9590Sstevel@tonic-gate apic_irq_t *irq_ptr;
9603446Smrj ulong_t iflag;
9610Sstevel@tonic-gate
9620Sstevel@tonic-gate iflag = intr_clear();
9630Sstevel@tonic-gate lock_set(&apic_ioapic_lock);
9643139Ssethg
9650Sstevel@tonic-gate apic_cpus[cpun].aci_status |= APIC_CPU_INTR_ENABLE;
9660Sstevel@tonic-gate
9670Sstevel@tonic-gate i = apic_min_device_irq;
9680Sstevel@tonic-gate for (i = apic_min_device_irq; i <= apic_max_device_irq; i++) {
9690Sstevel@tonic-gate if ((irq_ptr = apic_irq_table[i]) != NULL) {
9700Sstevel@tonic-gate if ((irq_ptr->airq_cpu & ~IRQ_USER_BOUND) == cpun) {
9710Sstevel@tonic-gate (void) apic_rebind_all(irq_ptr,
9723139Ssethg irq_ptr->airq_cpu);
9730Sstevel@tonic-gate }
9740Sstevel@tonic-gate }
9750Sstevel@tonic-gate }
9763139Ssethg
97712683SJimmy.Vetayases@oracle.com if (apic_cpus[cpun].aci_status & APIC_CPU_SUSPEND)
97812683SJimmy.Vetayases@oracle.com apic_cpus[cpun].aci_status &= ~APIC_CPU_SUSPEND;
97912683SJimmy.Vetayases@oracle.com
9803139Ssethg lock_clear(&apic_ioapic_lock);
9813139Ssethg intr_restore(iflag);
9820Sstevel@tonic-gate }
9830Sstevel@tonic-gate
9840Sstevel@tonic-gate /*
9855107Seota * If this module needs a periodic handler for the interrupt distribution, it
9865107Seota * can be added here. The argument to the periodic handler is not currently
9875107Seota * used, but is reserved for future.
9880Sstevel@tonic-gate */
9890Sstevel@tonic-gate static void
apic_post_cyclic_setup(void * arg)9900Sstevel@tonic-gate apic_post_cyclic_setup(void *arg)
9910Sstevel@tonic-gate {
9920Sstevel@tonic-gate _NOTE(ARGUNUSED(arg))
9930Sstevel@tonic-gate /* cpu_lock is held */
9945107Seota /* set up a periodic handler for intr redistribution */
9950Sstevel@tonic-gate
9960Sstevel@tonic-gate /*
9970Sstevel@tonic-gate * In peridoc mode intr redistribution processing is done in
9980Sstevel@tonic-gate * apic_intr_enter during clk intr processing
9990Sstevel@tonic-gate */
10000Sstevel@tonic-gate if (!apic_oneshot)
10010Sstevel@tonic-gate return;
10025107Seota /*
10035107Seota * Register a periodical handler for the redistribution processing.
10045107Seota * On X86, CY_LOW_LEVEL is mapped to the level 2 interrupt, so
10055107Seota * DDI_IPL_2 should be passed to ddi_periodic_add() here.
10065107Seota */
10075107Seota apic_periodic_id = ddi_periodic_add(
10085107Seota (void (*)(void *))apic_redistribute_compute, NULL,
10095107Seota apic_redistribute_sample_interval, DDI_IPL_2);
10100Sstevel@tonic-gate }
10110Sstevel@tonic-gate
10120Sstevel@tonic-gate static void
apic_redistribute_compute(void)10130Sstevel@tonic-gate apic_redistribute_compute(void)
10140Sstevel@tonic-gate {
10150Sstevel@tonic-gate int i, j, max_busy;
10160Sstevel@tonic-gate
10170Sstevel@tonic-gate if (apic_enable_dynamic_migration) {
10180Sstevel@tonic-gate if (++apic_nticks == apic_sample_factor_redistribution) {
10190Sstevel@tonic-gate /*
10200Sstevel@tonic-gate * Time to call apic_intr_redistribute().
10210Sstevel@tonic-gate * reset apic_nticks. This will cause max_busy
10220Sstevel@tonic-gate * to be calculated below and if it is more than
10230Sstevel@tonic-gate * apic_int_busy, we will do the whole thing
10240Sstevel@tonic-gate */
10250Sstevel@tonic-gate apic_nticks = 0;
10260Sstevel@tonic-gate }
10270Sstevel@tonic-gate max_busy = 0;
10280Sstevel@tonic-gate for (i = 0; i < apic_nproc; i++) {
102912004Sjiang.liu@intel.com if (!apic_cpu_in_range(i))
103012004Sjiang.liu@intel.com continue;
10310Sstevel@tonic-gate
10320Sstevel@tonic-gate /*
10330Sstevel@tonic-gate * Check if curipl is non zero & if ISR is in
10340Sstevel@tonic-gate * progress
10350Sstevel@tonic-gate */
10360Sstevel@tonic-gate if (((j = apic_cpus[i].aci_curipl) != 0) &&
10370Sstevel@tonic-gate (apic_cpus[i].aci_ISR_in_progress & (1 << j))) {
10380Sstevel@tonic-gate
10390Sstevel@tonic-gate int irq;
10400Sstevel@tonic-gate apic_cpus[i].aci_busy++;
10410Sstevel@tonic-gate irq = apic_cpus[i].aci_current[j];
10420Sstevel@tonic-gate apic_irq_table[irq]->airq_busy++;
10430Sstevel@tonic-gate }
10440Sstevel@tonic-gate
10450Sstevel@tonic-gate if (!apic_nticks &&
10460Sstevel@tonic-gate (apic_cpus[i].aci_busy > max_busy))
10470Sstevel@tonic-gate max_busy = apic_cpus[i].aci_busy;
10480Sstevel@tonic-gate }
10490Sstevel@tonic-gate if (!apic_nticks) {
10500Sstevel@tonic-gate if (max_busy > apic_int_busy_mark) {
10510Sstevel@tonic-gate /*
10520Sstevel@tonic-gate * We could make the following check be
10530Sstevel@tonic-gate * skipped > 1 in which case, we get a
10540Sstevel@tonic-gate * redistribution at half the busy mark (due to
10550Sstevel@tonic-gate * double interval). Need to be able to collect
10560Sstevel@tonic-gate * more empirical data to decide if that is a
10570Sstevel@tonic-gate * good strategy. Punt for now.
10580Sstevel@tonic-gate */
10593446Smrj if (apic_skipped_redistribute) {
10600Sstevel@tonic-gate apic_cleanup_busy();
10613446Smrj apic_skipped_redistribute = 0;
10623446Smrj } else {
10630Sstevel@tonic-gate apic_intr_redistribute();
10643446Smrj }
10650Sstevel@tonic-gate } else
10660Sstevel@tonic-gate apic_skipped_redistribute++;
10670Sstevel@tonic-gate }
10680Sstevel@tonic-gate }
10690Sstevel@tonic-gate }
10700Sstevel@tonic-gate
10710Sstevel@tonic-gate
10723446Smrj /*
10733446Smrj * The following functions are in the platform specific file so that they
10743446Smrj * can be different functions depending on whether we are running on
10753446Smrj * bare metal or a hypervisor.
10763446Smrj */
10770Sstevel@tonic-gate
10783446Smrj /*
10794937Sjohnny * Check to make sure there are enough irq slots
10803139Ssethg */
10813446Smrj int
apic_check_free_irqs(int count)10824937Sjohnny apic_check_free_irqs(int count)
10834937Sjohnny {
10844937Sjohnny int i, avail;
10854937Sjohnny
10864937Sjohnny avail = 0;
10874937Sjohnny for (i = APIC_FIRST_FREE_IRQ; i < APIC_RESV_IRQ; i++) {
10884937Sjohnny if ((apic_irq_table[i] == NULL) ||
10894937Sjohnny apic_irq_table[i]->airq_mps_intr_index == FREE_INDEX) {
10904937Sjohnny if (++avail >= count)
10914937Sjohnny return (PSM_SUCCESS);
10924937Sjohnny }
10934937Sjohnny }
10944937Sjohnny return (PSM_FAILURE);
10954937Sjohnny }
10964937Sjohnny
10974937Sjohnny /*
10984937Sjohnny * This function allocates "count" MSI vector(s) for the given "dip/pri/type"
10994937Sjohnny */
11004937Sjohnny int
apic_alloc_msi_vectors(dev_info_t * dip,int inum,int count,int pri,int behavior)11014937Sjohnny apic_alloc_msi_vectors(dev_info_t *dip, int inum, int count, int pri,
11023446Smrj int behavior)
11033139Ssethg {
11043446Smrj int rcount, i;
11057282Smishra uchar_t start, irqno;
11067282Smishra uint32_t cpu;
11073446Smrj major_t major;
11083446Smrj apic_irq_t *irqptr;
11093139Ssethg
11104937Sjohnny DDI_INTR_IMPLDBG((CE_CONT, "apic_alloc_msi_vectors: dip=0x%p "
11113446Smrj "inum=0x%x pri=0x%x count=0x%x behavior=%d\n",
11124937Sjohnny (void *)dip, inum, pri, count, behavior));
11133139Ssethg
11143446Smrj if (count > 1) {
11153446Smrj if (behavior == DDI_INTR_ALLOC_STRICT &&
11168925SEvan.Yan@Sun.COM apic_multi_msi_enable == 0)
11173446Smrj return (0);
11183446Smrj if (apic_multi_msi_enable == 0)
11193446Smrj count = 1;
11203446Smrj }
11213139Ssethg
11223446Smrj if ((rcount = apic_navail_vector(dip, pri)) > count)
11233446Smrj rcount = count;
11243446Smrj else if (rcount == 0 || (rcount < count &&
11253446Smrj behavior == DDI_INTR_ALLOC_STRICT))
11263446Smrj return (0);
11273139Ssethg
11283446Smrj /* if not ISP2, then round it down */
11293446Smrj if (!ISP2(rcount))
11303446Smrj rcount = 1 << (highbit(rcount) - 1);
11313139Ssethg
11323446Smrj mutex_enter(&airq_mutex);
11333446Smrj
11343446Smrj for (start = 0; rcount > 0; rcount >>= 1) {
11353446Smrj if ((start = apic_find_multi_vectors(pri, rcount)) != 0 ||
11363446Smrj behavior == DDI_INTR_ALLOC_STRICT)
11373446Smrj break;
11383139Ssethg }
11393139Ssethg
11403446Smrj if (start == 0) {
11413446Smrj /* no vector available */
11423446Smrj mutex_exit(&airq_mutex);
11433446Smrj return (0);
11443446Smrj }
11453446Smrj
11464937Sjohnny if (apic_check_free_irqs(rcount) == PSM_FAILURE) {
11474937Sjohnny /* not enough free irq slots available */
11484937Sjohnny mutex_exit(&airq_mutex);
11494937Sjohnny return (0);
11504937Sjohnny }
11514937Sjohnny
11528459SJerry.Gilliam@Sun.COM major = (dip != NULL) ? ddi_driver_major(dip) : 0;
11533446Smrj for (i = 0; i < rcount; i++) {
11543446Smrj if ((irqno = apic_allocate_irq(apic_first_avail_irq)) ==
11553446Smrj (uchar_t)-1) {
11564937Sjohnny /*
11574937Sjohnny * shouldn't happen because of the
11584937Sjohnny * apic_check_free_irqs() check earlier
11594937Sjohnny */
11603446Smrj mutex_exit(&airq_mutex);
11614937Sjohnny DDI_INTR_IMPLDBG((CE_CONT, "apic_alloc_msi_vectors: "
11623446Smrj "apic_allocate_irq failed\n"));
11633446Smrj return (i);
11643446Smrj }
11653446Smrj apic_max_device_irq = max(irqno, apic_max_device_irq);
11663446Smrj apic_min_device_irq = min(irqno, apic_min_device_irq);
11673446Smrj irqptr = apic_irq_table[irqno];
11683446Smrj #ifdef DEBUG
11693446Smrj if (apic_vector_to_irq[start + i] != APIC_RESV_IRQ)
11704937Sjohnny DDI_INTR_IMPLDBG((CE_CONT, "apic_alloc_msi_vectors: "
11713446Smrj "apic_vector_to_irq is not APIC_RESV_IRQ\n"));
11723446Smrj #endif
11733446Smrj apic_vector_to_irq[start + i] = (uchar_t)irqno;
11743446Smrj
11753446Smrj irqptr->airq_vector = (uchar_t)(start + i);
11763446Smrj irqptr->airq_ioapicindex = (uchar_t)inum; /* start */
11773446Smrj irqptr->airq_intin_no = (uchar_t)rcount;
11783446Smrj irqptr->airq_ipl = pri;
11793446Smrj irqptr->airq_vector = start + i;
11803446Smrj irqptr->airq_origirq = (uchar_t)(inum + i);
11813446Smrj irqptr->airq_share_id = 0;
11823446Smrj irqptr->airq_mps_intr_index = MSI_INDEX;
11833446Smrj irqptr->airq_dip = dip;
11843446Smrj irqptr->airq_major = major;
11853446Smrj if (i == 0) /* they all bound to the same cpu */
11863446Smrj cpu = irqptr->airq_cpu = apic_bind_intr(dip, irqno,
11874397Sschwartz 0xff, 0xff);
11883446Smrj else
11893446Smrj irqptr->airq_cpu = cpu;
11904937Sjohnny DDI_INTR_IMPLDBG((CE_CONT, "apic_alloc_msi_vectors: irq=0x%x "
11913446Smrj "dip=0x%p vector=0x%x origirq=0x%x pri=0x%x\n", irqno,
11923446Smrj (void *)irqptr->airq_dip, irqptr->airq_vector,
11933446Smrj irqptr->airq_origirq, pri));
11943446Smrj }
11953446Smrj mutex_exit(&airq_mutex);
11963446Smrj return (rcount);
11973139Ssethg }
11983139Ssethg
11993139Ssethg /*
12004937Sjohnny * This function allocates "count" MSI-X vector(s) for the given "dip/pri/type"
12014937Sjohnny */
12024937Sjohnny int
apic_alloc_msix_vectors(dev_info_t * dip,int inum,int count,int pri,int behavior)12034937Sjohnny apic_alloc_msix_vectors(dev_info_t *dip, int inum, int count, int pri,
12044937Sjohnny int behavior)
12054937Sjohnny {
12064937Sjohnny int rcount, i;
12074937Sjohnny major_t major;
12084937Sjohnny
12094937Sjohnny mutex_enter(&airq_mutex);
12104937Sjohnny
12114937Sjohnny if ((rcount = apic_navail_vector(dip, pri)) > count)
12124937Sjohnny rcount = count;
12134937Sjohnny else if (rcount == 0 || (rcount < count &&
12144937Sjohnny behavior == DDI_INTR_ALLOC_STRICT)) {
12154937Sjohnny rcount = 0;
12164937Sjohnny goto out;
12174937Sjohnny }
12184937Sjohnny
12194937Sjohnny if (apic_check_free_irqs(rcount) == PSM_FAILURE) {
12204937Sjohnny /* not enough free irq slots available */
12214937Sjohnny rcount = 0;
12224937Sjohnny goto out;
12234937Sjohnny }
12244937Sjohnny
12258459SJerry.Gilliam@Sun.COM major = (dip != NULL) ? ddi_driver_major(dip) : 0;
12264937Sjohnny for (i = 0; i < rcount; i++) {
12274937Sjohnny uchar_t vector, irqno;
12284937Sjohnny apic_irq_t *irqptr;
12294937Sjohnny
12304937Sjohnny if ((irqno = apic_allocate_irq(apic_first_avail_irq)) ==
12314937Sjohnny (uchar_t)-1) {
12324937Sjohnny /*
12334937Sjohnny * shouldn't happen because of the
12344937Sjohnny * apic_check_free_irqs() check earlier
12354937Sjohnny */
12364937Sjohnny DDI_INTR_IMPLDBG((CE_CONT, "apic_alloc_msix_vectors: "
12374937Sjohnny "apic_allocate_irq failed\n"));
12384937Sjohnny rcount = i;
12394937Sjohnny goto out;
12404937Sjohnny }
12414937Sjohnny if ((vector = apic_allocate_vector(pri, irqno, 1)) == 0) {
12424937Sjohnny /*
12434937Sjohnny * shouldn't happen because of the
12444937Sjohnny * apic_navail_vector() call earlier
12454937Sjohnny */
12464937Sjohnny DDI_INTR_IMPLDBG((CE_CONT, "apic_alloc_msix_vectors: "
12474937Sjohnny "apic_allocate_vector failed\n"));
12484937Sjohnny rcount = i;
12494937Sjohnny goto out;
12504937Sjohnny }
12514937Sjohnny apic_max_device_irq = max(irqno, apic_max_device_irq);
12524937Sjohnny apic_min_device_irq = min(irqno, apic_min_device_irq);
12534937Sjohnny irqptr = apic_irq_table[irqno];
12544937Sjohnny irqptr->airq_vector = (uchar_t)vector;
12554937Sjohnny irqptr->airq_ipl = pri;
12564937Sjohnny irqptr->airq_origirq = (uchar_t)(inum + i);
12574937Sjohnny irqptr->airq_share_id = 0;
12584937Sjohnny irqptr->airq_mps_intr_index = MSIX_INDEX;
12594937Sjohnny irqptr->airq_dip = dip;
12604937Sjohnny irqptr->airq_major = major;
12614937Sjohnny irqptr->airq_cpu = apic_bind_intr(dip, irqno, 0xff, 0xff);
12624937Sjohnny }
12634937Sjohnny out:
12644937Sjohnny mutex_exit(&airq_mutex);
12654937Sjohnny return (rcount);
12664937Sjohnny }
12674937Sjohnny
12684937Sjohnny /*
12693446Smrj * Allocate a free vector for irq at ipl. Takes care of merging of multiple
12703446Smrj * IPLs into a single APIC level as well as stretching some IPLs onto multiple
12713446Smrj * levels. APIC_HI_PRI_VECTS interrupts are reserved for high priority
12723446Smrj * requests and allocated only when pri is set.
12730Sstevel@tonic-gate */
12743446Smrj uchar_t
apic_allocate_vector(int ipl,int irq,int pri)12753446Smrj apic_allocate_vector(int ipl, int irq, int pri)
12760Sstevel@tonic-gate {
12773446Smrj int lowest, highest, i;
12780Sstevel@tonic-gate
12793446Smrj highest = apic_ipltopri[ipl] + APIC_VECTOR_MASK;
12803446Smrj lowest = apic_ipltopri[ipl - 1] + APIC_VECTOR_PER_IPL;
12810Sstevel@tonic-gate
12823446Smrj if (highest < lowest) /* Both ipl and ipl - 1 map to same pri */
12833446Smrj lowest -= APIC_VECTOR_PER_IPL;
12843139Ssethg
12853446Smrj #ifdef DEBUG
12863446Smrj if (apic_restrict_vector) /* for testing shared interrupt logic */
12873446Smrj highest = lowest + apic_restrict_vector + APIC_HI_PRI_VECTS;
12883446Smrj #endif /* DEBUG */
12893446Smrj if (pri == 0)
12903446Smrj highest -= APIC_HI_PRI_VECTS;
12913139Ssethg
129211641SKerry.Shu@Sun.COM for (i = lowest; i <= highest; i++) {
12933446Smrj if (APIC_CHECK_RESERVE_VECTORS(i))
12943446Smrj continue;
12953446Smrj if (apic_vector_to_irq[i] == APIC_RESV_IRQ) {
12963446Smrj apic_vector_to_irq[i] = (uchar_t)irq;
12973446Smrj return (i);
12980Sstevel@tonic-gate }
12990Sstevel@tonic-gate }
13000Sstevel@tonic-gate
13013446Smrj return (0);
13023446Smrj }
13033446Smrj
13043446Smrj /* Mark vector as not being used by any irq */
13053446Smrj void
apic_free_vector(uchar_t vector)13063446Smrj apic_free_vector(uchar_t vector)
13073446Smrj {
13083446Smrj apic_vector_to_irq[vector] = APIC_RESV_IRQ;
13093446Smrj }
13103446Smrj
131112204Sjiang.liu@intel.com /*
13123446Smrj * Call rebind to do the actual programming.
13133446Smrj * Must be called with interrupts disabled and apic_ioapic_lock held
13143446Smrj * 'p' is polymorphic -- if this function is called to process a deferred
13153446Smrj * reprogramming, p is of type 'struct ioapic_reprogram_data *', from which
13163446Smrj * the irq pointer is retrieved. If not doing deferred reprogramming,
13173446Smrj * p is of the type 'apic_irq_t *'.
13183446Smrj *
13193446Smrj * apic_ioapic_lock must be held across this call, as it protects apic_rebind
132012683SJimmy.Vetayases@oracle.com * and it protects apic_get_next_bind_cpu() from a race in which a CPU can be
132112004Sjiang.liu@intel.com * taken offline after a cpu is selected, but before apic_rebind is called to
13223446Smrj * bind interrupts to it.
13233446Smrj */
13243446Smrj int
apic_setup_io_intr(void * p,int irq,boolean_t deferred)13253446Smrj apic_setup_io_intr(void *p, int irq, boolean_t deferred)
13263446Smrj {
13273446Smrj apic_irq_t *irqptr;
13283446Smrj struct ioapic_reprogram_data *drep = NULL;
13293446Smrj int rv;
13303446Smrj
13313446Smrj if (deferred) {
13323446Smrj drep = (struct ioapic_reprogram_data *)p;
13333446Smrj ASSERT(drep != NULL);
13343446Smrj irqptr = drep->irqp;
13353446Smrj } else
13363446Smrj irqptr = (apic_irq_t *)p;
13373446Smrj
13383446Smrj ASSERT(irqptr != NULL);
13393446Smrj
13403446Smrj rv = apic_rebind(irqptr, apic_irq_table[irq]->airq_cpu, drep);
13413446Smrj if (rv) {
13423446Smrj /*
13433446Smrj * CPU is not up or interrupts are disabled. Fall back to
13443446Smrj * the first available CPU
13453446Smrj */
134612204Sjiang.liu@intel.com rv = apic_rebind(irqptr, apic_find_cpu(APIC_CPU_INTR_ENABLE),
134712204Sjiang.liu@intel.com drep);
13483446Smrj }
13493446Smrj
13503446Smrj return (rv);
13510Sstevel@tonic-gate }
13523446Smrj
13533446Smrj
13543446Smrj uchar_t
apic_modify_vector(uchar_t vector,int irq)13553446Smrj apic_modify_vector(uchar_t vector, int irq)
13563446Smrj {
13573446Smrj apic_vector_to_irq[vector] = (uchar_t)irq;
13583446Smrj return (vector);
13593446Smrj }
13604397Sschwartz
13614397Sschwartz char *
apic_get_apic_type(void)136212683SJimmy.Vetayases@oracle.com apic_get_apic_type(void)
13634397Sschwartz {
13644397Sschwartz return (apic_psm_info.p_mach_idstring);
13654397Sschwartz }
13667282Smishra
13677282Smishra void
x2apic_update_psm(void)136812683SJimmy.Vetayases@oracle.com x2apic_update_psm(void)
13697282Smishra {
13707282Smishra struct psm_ops *pops = &apic_ops;
13717282Smishra
13727282Smishra ASSERT(pops != NULL);
13737282Smishra
13747282Smishra pops->psm_intr_exit = x2apic_intr_exit;
13757282Smishra pops->psm_setspl = x2apic_setspl;
137612683SJimmy.Vetayases@oracle.com
137712004Sjiang.liu@intel.com pops->psm_send_ipi = x2apic_send_ipi;
13787282Smishra send_dirintf = pops->psm_send_ipi;
13797986SSaurabh.Mishra@Sun.COM
13807986SSaurabh.Mishra@Sun.COM apic_mode = LOCAL_X2APIC;
13817986SSaurabh.Mishra@Sun.COM apic_change_ops();
13827282Smishra }
1383