17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
56f796756Sdmick * Common Development and Distribution License (the "License").
66f796756Sdmick * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21ae115bc7Smrj
227c478bd9Sstevel@tonic-gate /*
23e7c3cdaeSjiang.liu@intel.com * Copyright (c) 1993, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate */
25a3114836SGerry Liu /*
26a3114836SGerry Liu * Copyright (c) 2010, Intel Corporation.
27a3114836SGerry Liu * All rights reserved.
28a3114836SGerry Liu */
29a288e5a9SJoshua M. Clulow /*
30a288e5a9SJoshua M. Clulow * Copyright (c) 2013, Joyent, Inc. All rights reserved.
31a288e5a9SJoshua M. Clulow */
327c478bd9Sstevel@tonic-gate
33636dfb4bSJerry Jelinek /*
34636dfb4bSJerry Jelinek * To understand how the pcplusmp module interacts with the interrupt subsystem
35636dfb4bSJerry Jelinek * read the theory statement in uts/i86pc/os/intr.c.
36636dfb4bSJerry Jelinek */
377ff178cdSJimmy Vetayases
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate * PSMI 1.1 extensions are supported only in 2.6 and later versions.
407c478bd9Sstevel@tonic-gate * PSMI 1.2 extensions are supported only in 2.7 and later versions.
417c478bd9Sstevel@tonic-gate * PSMI 1.3 and 1.4 extensions are supported in Solaris 10.
427c478bd9Sstevel@tonic-gate * PSMI 1.5 extensions are supported in Solaris Nevada.
432df1fe9cSrandyf * PSMI 1.6 extensions are supported in Solaris Nevada.
44a3114836SGerry Liu * PSMI 1.7 extensions are supported in Solaris Nevada.
457c478bd9Sstevel@tonic-gate */
46a3114836SGerry Liu #define PSMI_1_7
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate #include <sys/processor.h>
497c478bd9Sstevel@tonic-gate #include <sys/time.h>
507c478bd9Sstevel@tonic-gate #include <sys/psm.h>
517c478bd9Sstevel@tonic-gate #include <sys/smp_impldefs.h>
527c478bd9Sstevel@tonic-gate #include <sys/cram.h>
537c478bd9Sstevel@tonic-gate #include <sys/acpi/acpi.h>
547c478bd9Sstevel@tonic-gate #include <sys/acpica.h>
557c478bd9Sstevel@tonic-gate #include <sys/psm_common.h>
56ae115bc7Smrj #include <sys/apic.h>
577c478bd9Sstevel@tonic-gate #include <sys/pit.h>
587c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
597c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
607c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
617c478bd9Sstevel@tonic-gate #include <sys/pci.h>
627c478bd9Sstevel@tonic-gate #include <sys/promif.h>
637c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>
647c478bd9Sstevel@tonic-gate #include <sys/cpc_impl.h>
657c478bd9Sstevel@tonic-gate #include <sys/uadmin.h>
667c478bd9Sstevel@tonic-gate #include <sys/panic.h>
677c478bd9Sstevel@tonic-gate #include <sys/debug.h>
687c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
697c478bd9Sstevel@tonic-gate #include <sys/trap.h>
707c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
71ae115bc7Smrj #include <sys/sysmacros.h>
727c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
737c478bd9Sstevel@tonic-gate #include <sys/rm_platter.h>
747c478bd9Sstevel@tonic-gate #include <sys/privregs.h>
757c478bd9Sstevel@tonic-gate #include <sys/note.h>
767c478bd9Sstevel@tonic-gate #include <sys/pci_intr_lib.h>
77ae115bc7Smrj #include <sys/spl.h>
78843e1988Sjohnlev #include <sys/clock.h>
79a288e5a9SJoshua M. Clulow #include <sys/cyclic.h>
80dd4eeefdSeota #include <sys/dditypes.h>
81dd4eeefdSeota #include <sys/sunddi.h>
82e3d60c9bSAdrian Frost #include <sys/x_call.h>
83325e77f4SSaurabh Misra #include <sys/reboot.h>
840e751525SEric Saxe #include <sys/hpet.h>
857ff178cdSJimmy Vetayases #include <sys/apic_common.h>
8641afdfa7SKrishnendu Sadhukhan - Sun Microsystems #include <sys/apic_timer.h>
877c478bd9Sstevel@tonic-gate
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate * Local Function Prototypes
907c478bd9Sstevel@tonic-gate */
917ff178cdSJimmy Vetayases static void apic_init_intr(void);
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate * standard MP entries
957c478bd9Sstevel@tonic-gate */
967ff178cdSJimmy Vetayases static int apic_probe(void);
977c478bd9Sstevel@tonic-gate static int apic_getclkirq(int ipl);
987ff178cdSJimmy Vetayases static void apic_init(void);
997c478bd9Sstevel@tonic-gate static void apic_picinit(void);
1007c478bd9Sstevel@tonic-gate static int apic_post_cpu_start(void);
1017c478bd9Sstevel@tonic-gate static int apic_intr_enter(int ipl, int *vect);
1027c478bd9Sstevel@tonic-gate static void apic_setspl(int ipl);
103b6917abeSmishra static void x2apic_setspl(int ipl);
1047c478bd9Sstevel@tonic-gate static int apic_addspl(int ipl, int vector, int min_ipl, int max_ipl);
1057c478bd9Sstevel@tonic-gate static int apic_delspl(int ipl, int vector, int min_ipl, int max_ipl);
1067c478bd9Sstevel@tonic-gate static int apic_disable_intr(processorid_t cpun);
1077c478bd9Sstevel@tonic-gate static void apic_enable_intr(processorid_t cpun);
1087c478bd9Sstevel@tonic-gate static int apic_get_ipivect(int ipl, int type);
1097c478bd9Sstevel@tonic-gate static void apic_post_cyclic_setup(void *arg);
110a3114836SGerry Liu
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate * The following vector assignments influence the value of ipltopri and
1137c478bd9Sstevel@tonic-gate * vectortoipl. Note that vectors 0 - 0x1f are not used. We can program
114c8589f13Ssethg * idle to 0 and IPL 0 to 0xf to differentiate idle in case
1157c478bd9Sstevel@tonic-gate * we care to do so in future. Note some IPLs which are rarely used
1167c478bd9Sstevel@tonic-gate * will share the vector ranges and heavily used IPLs (5 and 6) have
1177c478bd9Sstevel@tonic-gate * a wide range.
118c8589f13Ssethg *
119c8589f13Ssethg * This array is used to initialize apic_ipls[] (in apic_init()).
120c8589f13Ssethg *
1217c478bd9Sstevel@tonic-gate * IPL Vector range. as passed to intr_enter
1227c478bd9Sstevel@tonic-gate * 0 none.
1237c478bd9Sstevel@tonic-gate * 1,2,3 0x20-0x2f 0x0-0xf
1247c478bd9Sstevel@tonic-gate * 4 0x30-0x3f 0x10-0x1f
1257c478bd9Sstevel@tonic-gate * 5 0x40-0x5f 0x20-0x3f
1267c478bd9Sstevel@tonic-gate * 6 0x60-0x7f 0x40-0x5f
1277c478bd9Sstevel@tonic-gate * 7,8,9 0x80-0x8f 0x60-0x6f
1287c478bd9Sstevel@tonic-gate * 10 0x90-0x9f 0x70-0x7f
1297c478bd9Sstevel@tonic-gate * 11 0xa0-0xaf 0x80-0x8f
1307c478bd9Sstevel@tonic-gate * ... ...
131c8589f13Ssethg * 15 0xe0-0xef 0xc0-0xcf
132c8589f13Ssethg * 15 0xf0-0xff 0xd0-0xdf
1337c478bd9Sstevel@tonic-gate */
1347c478bd9Sstevel@tonic-gate uchar_t apic_vectortoipl[APIC_AVAIL_VECTOR / APIC_VECTOR_PER_IPL] = {
135c8589f13Ssethg 3, 4, 5, 5, 6, 6, 9, 10, 11, 12, 13, 14, 15, 15
1367c478bd9Sstevel@tonic-gate };
1377c478bd9Sstevel@tonic-gate /*
138c8589f13Ssethg * The ipl of an ISR at vector X is apic_vectortoipl[X>>4]
1397c478bd9Sstevel@tonic-gate * NOTE that this is vector as passed into intr_enter which is
1407c478bd9Sstevel@tonic-gate * programmed vector - 0x20 (APIC_BASE_VECT)
1417c478bd9Sstevel@tonic-gate */
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate uchar_t apic_ipltopri[MAXIPL + 1]; /* unix ipl to apic pri */
1447c478bd9Sstevel@tonic-gate /* The taskpri to be programmed into apic to mask given ipl */
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate /*
147c8589f13Ssethg * Correlation of the hardware vector to the IPL in use, initialized
148c8589f13Ssethg * from apic_vectortoipl[] in apic_init(). The final IPLs may not correlate
149c8589f13Ssethg * to the IPLs in apic_vectortoipl on some systems that share interrupt lines
150c8589f13Ssethg * connected to errata-stricken IOAPICs
151c8589f13Ssethg */
152c8589f13Ssethg uchar_t apic_ipls[APIC_AVAIL_VECTOR];
153c8589f13Ssethg
154c8589f13Ssethg /*
1557c478bd9Sstevel@tonic-gate * Patchable global variables.
1567c478bd9Sstevel@tonic-gate */
1577c478bd9Sstevel@tonic-gate int apic_enable_hwsoftint = 0; /* 0 - disable, 1 - enable */
1587c478bd9Sstevel@tonic-gate int apic_enable_bind_log = 1; /* 1 - display interrupt binding log */
159a3114836SGerry Liu
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate * Local static data
1627c478bd9Sstevel@tonic-gate */
1637c478bd9Sstevel@tonic-gate static struct psm_ops apic_ops = {
1647c478bd9Sstevel@tonic-gate apic_probe,
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate apic_init,
1677c478bd9Sstevel@tonic-gate apic_picinit,
1687c478bd9Sstevel@tonic-gate apic_intr_enter,
1697c478bd9Sstevel@tonic-gate apic_intr_exit,
1707c478bd9Sstevel@tonic-gate apic_setspl,
1717c478bd9Sstevel@tonic-gate apic_addspl,
1727c478bd9Sstevel@tonic-gate apic_delspl,
1737c478bd9Sstevel@tonic-gate apic_disable_intr,
1747c478bd9Sstevel@tonic-gate apic_enable_intr,
175a1af7ba0Scwb (int (*)(int))NULL, /* psm_softlvl_to_irq */
176a1af7ba0Scwb (void (*)(int))NULL, /* psm_set_softintr */
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate apic_set_idlecpu,
1797c478bd9Sstevel@tonic-gate apic_unset_idlecpu,
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate apic_clkinit,
1827c478bd9Sstevel@tonic-gate apic_getclkirq,
1837c478bd9Sstevel@tonic-gate (void (*)(void))NULL, /* psm_hrtimeinit */
1847c478bd9Sstevel@tonic-gate apic_gethrtime,
1857c478bd9Sstevel@tonic-gate
1867c478bd9Sstevel@tonic-gate apic_get_next_processorid,
1877c478bd9Sstevel@tonic-gate apic_cpu_start,
1887c478bd9Sstevel@tonic-gate apic_post_cpu_start,
1897c478bd9Sstevel@tonic-gate apic_shutdown,
1907c478bd9Sstevel@tonic-gate apic_get_ipivect,
1917c478bd9Sstevel@tonic-gate apic_send_ipi,
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate (int (*)(dev_info_t *, int))NULL, /* psm_translate_irq */
1947c478bd9Sstevel@tonic-gate (void (*)(int, char *))NULL, /* psm_notify_error */
1957c478bd9Sstevel@tonic-gate (void (*)(int))NULL, /* psm_notify_func */
1967c478bd9Sstevel@tonic-gate apic_timer_reprogram,
1977c478bd9Sstevel@tonic-gate apic_timer_enable,
1987c478bd9Sstevel@tonic-gate apic_timer_disable,
1997c478bd9Sstevel@tonic-gate apic_post_cyclic_setup,
2007c478bd9Sstevel@tonic-gate apic_preshutdown,
2012df1fe9cSrandyf apic_intr_ops, /* Advanced DDI Interrupt framework */
2022df1fe9cSrandyf apic_state, /* save, restore apic state for S3 */
203a3114836SGerry Liu apic_cpu_ops, /* CPU control interface. */
2047c478bd9Sstevel@tonic-gate };
2057c478bd9Sstevel@tonic-gate
2067ff178cdSJimmy Vetayases struct psm_ops *psmops = &apic_ops;
2077c478bd9Sstevel@tonic-gate
2087c478bd9Sstevel@tonic-gate static struct psm_info apic_psm_info = {
209a3114836SGerry Liu PSM_INFO_VER01_7, /* version */
2107c478bd9Sstevel@tonic-gate PSM_OWN_EXCLUSIVE, /* ownership */
2117c478bd9Sstevel@tonic-gate (struct psm_ops *)&apic_ops, /* operation */
2122917a9c9Sschwartz APIC_PCPLUSMP_NAME, /* machine name */
213da2743adSdmick "pcplusmp v1.4 compatible",
2147c478bd9Sstevel@tonic-gate };
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate static void *apic_hdlp;
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate /* to gather intr data and redistribute */
2197c478bd9Sstevel@tonic-gate static void apic_redistribute_compute(void);
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate /*
2227c478bd9Sstevel@tonic-gate * This is the loadable module wrapper
2237c478bd9Sstevel@tonic-gate */
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate int
_init(void)2267c478bd9Sstevel@tonic-gate _init(void)
2277c478bd9Sstevel@tonic-gate {
2287c478bd9Sstevel@tonic-gate if (apic_coarse_hrtime)
2297c478bd9Sstevel@tonic-gate apic_ops.psm_gethrtime = &apic_gettime;
2307c478bd9Sstevel@tonic-gate return (psm_mod_init(&apic_hdlp, &apic_psm_info));
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate int
_fini(void)2347c478bd9Sstevel@tonic-gate _fini(void)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate return (psm_mod_fini(&apic_hdlp, &apic_psm_info));
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2407c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate return (psm_mod_info(&apic_hdlp, &apic_psm_info, modinfop));
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate static int
apic_probe(void)2467ff178cdSJimmy Vetayases apic_probe(void)
2477c478bd9Sstevel@tonic-gate {
2487ff178cdSJimmy Vetayases /* check if apix is initialized */
2497ff178cdSJimmy Vetayases if (apix_enable && apix_loaded())
2507ff178cdSJimmy Vetayases return (PSM_FAILURE);
2517ff178cdSJimmy Vetayases else
2527ff178cdSJimmy Vetayases apix_enable = 0; /* continue using pcplusmp PSM */
2537ff178cdSJimmy Vetayases
254ae115bc7Smrj return (apic_probe_common(apic_psm_info.p_mach_idstring));
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate
2577ff178cdSJimmy Vetayases static uchar_t
apic_xlate_vector_by_irq(uchar_t irq)2587ff178cdSJimmy Vetayases apic_xlate_vector_by_irq(uchar_t irq)
2597ff178cdSJimmy Vetayases {
2607ff178cdSJimmy Vetayases if (apic_irq_table[irq] == NULL)
2617ff178cdSJimmy Vetayases return (0);
2627ff178cdSJimmy Vetayases
2637ff178cdSJimmy Vetayases return (apic_irq_table[irq]->airq_vector);
2647ff178cdSJimmy Vetayases }
2657ff178cdSJimmy Vetayases
2667c478bd9Sstevel@tonic-gate void
apic_init(void)2677ff178cdSJimmy Vetayases apic_init(void)
2687c478bd9Sstevel@tonic-gate {
2697c478bd9Sstevel@tonic-gate int i;
2707c478bd9Sstevel@tonic-gate int j = 1;
271ae115bc7Smrj
2727ff178cdSJimmy Vetayases psm_get_ioapicid = apic_get_ioapicid;
2737ff178cdSJimmy Vetayases psm_get_localapicid = apic_get_localapicid;
2747ff178cdSJimmy Vetayases psm_xlate_vector_by_irq = apic_xlate_vector_by_irq;
2757ff178cdSJimmy Vetayases
2767c478bd9Sstevel@tonic-gate apic_ipltopri[0] = APIC_VECTOR_PER_IPL; /* leave 0 for idle */
2777c478bd9Sstevel@tonic-gate for (i = 0; i < (APIC_AVAIL_VECTOR / APIC_VECTOR_PER_IPL); i++) {
2787c478bd9Sstevel@tonic-gate if ((i < ((APIC_AVAIL_VECTOR / APIC_VECTOR_PER_IPL) - 1)) &&
2797c478bd9Sstevel@tonic-gate (apic_vectortoipl[i + 1] == apic_vectortoipl[i]))
2807c478bd9Sstevel@tonic-gate /* get to highest vector at the same ipl */
2817c478bd9Sstevel@tonic-gate continue;
2827c478bd9Sstevel@tonic-gate for (; j <= apic_vectortoipl[i]; j++) {
2837c478bd9Sstevel@tonic-gate apic_ipltopri[j] = (i << APIC_IPL_SHIFT) +
2847c478bd9Sstevel@tonic-gate APIC_BASE_VECT;
2857c478bd9Sstevel@tonic-gate }
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate for (; j < MAXIPL + 1; j++)
2887c478bd9Sstevel@tonic-gate /* fill up any empty ipltopri slots */
2897c478bd9Sstevel@tonic-gate apic_ipltopri[j] = (i << APIC_IPL_SHIFT) + APIC_BASE_VECT;
290ae115bc7Smrj apic_init_common();
29132842aabSJosef 'Jeff' Sipek
29232842aabSJosef 'Jeff' Sipek #if !defined(__amd64)
2932ef50f01SJoe Bonasera if (cpuid_have_cr8access(CPU))
2942ef50f01SJoe Bonasera apic_have_32bit_cr8 = 1;
29532842aabSJosef 'Jeff' Sipek #endif
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate
2987c478bd9Sstevel@tonic-gate static void
apic_init_intr(void)2997ff178cdSJimmy Vetayases apic_init_intr(void)
3007c478bd9Sstevel@tonic-gate {
3017c478bd9Sstevel@tonic-gate processorid_t cpun = psm_get_cpu_id();
302da2743adSdmick uint_t nlvt;
303b6917abeSmishra uint32_t svr = AV_UNIT_ENABLE | APIC_SPUR_INTR;
3047c478bd9Sstevel@tonic-gate
305b6917abeSmishra apic_reg_ops->apic_write_task_reg(APIC_MASK_ALL);
306b6917abeSmishra
307b6917abeSmishra if (apic_mode == LOCAL_APIC) {
308b6917abeSmishra /*
309b6917abeSmishra * We are running APIC in MMIO mode.
310b6917abeSmishra */
311b6917abeSmishra if (apic_flat_model) {
312b6917abeSmishra apic_reg_ops->apic_write(APIC_FORMAT_REG,
313b6917abeSmishra APIC_FLAT_MODEL);
314b6917abeSmishra } else {
315b6917abeSmishra apic_reg_ops->apic_write(APIC_FORMAT_REG,
316b6917abeSmishra APIC_CLUSTER_MODEL);
317b6917abeSmishra }
318b6917abeSmishra
319b6917abeSmishra apic_reg_ops->apic_write(APIC_DEST_REG,
320b6917abeSmishra AV_HIGH_ORDER >> cpun);
321b6917abeSmishra }
322b6917abeSmishra
323e511d54dSSaurabh Misra if (apic_directed_EOI_supported()) {
324b6917abeSmishra /*
325e511d54dSSaurabh Misra * Setting the 12th bit in the Spurious Interrupt Vector
326e511d54dSSaurabh Misra * Register suppresses broadcast EOIs generated by the local
327e511d54dSSaurabh Misra * APIC. The suppression of broadcast EOIs happens only when
328e511d54dSSaurabh Misra * interrupts are level-triggered.
329b6917abeSmishra */
330e511d54dSSaurabh Misra svr |= APIC_SVR_SUPPRESS_BROADCAST_EOI;
331b6917abeSmishra }
3327c478bd9Sstevel@tonic-gate
3337c478bd9Sstevel@tonic-gate /* need to enable APIC before unmasking NMI */
334b6917abeSmishra apic_reg_ops->apic_write(APIC_SPUR_INT_REG, svr);
3357c478bd9Sstevel@tonic-gate
336da2743adSdmick /*
337da2743adSdmick * Presence of an invalid vector with delivery mode AV_FIXED can
338da2743adSdmick * cause an error interrupt, even if the entry is masked...so
339da2743adSdmick * write a valid vector to LVT entries along with the mask bit
340da2743adSdmick */
341da2743adSdmick
342da2743adSdmick /* All APICs have timer and LINT0/1 */
343b6917abeSmishra apic_reg_ops->apic_write(APIC_LOCAL_TIMER, AV_MASK|APIC_RESV_IRQ);
344b6917abeSmishra apic_reg_ops->apic_write(APIC_INT_VECT0, AV_MASK|APIC_RESV_IRQ);
345b6917abeSmishra apic_reg_ops->apic_write(APIC_INT_VECT1, AV_NMI); /* enable NMI */
3467c478bd9Sstevel@tonic-gate
347da2743adSdmick /*
348da2743adSdmick * On integrated APICs, the number of LVT entries is
349da2743adSdmick * 'Max LVT entry' + 1; on 82489DX's (non-integrated
350da2743adSdmick * APICs), nlvt is "3" (LINT0, LINT1, and timer)
351da2743adSdmick */
3527c478bd9Sstevel@tonic-gate
353da2743adSdmick if (apic_cpus[cpun].aci_local_ver < APIC_INTEGRATED_VERS) {
354da2743adSdmick nlvt = 3;
355da2743adSdmick } else {
356a898d95bSSaurabh Misra nlvt = ((apic_reg_ops->apic_read(APIC_VERS_REG) >> 16) &
357a898d95bSSaurabh Misra 0xFF) + 1;
358da2743adSdmick }
359da2743adSdmick
360da2743adSdmick if (nlvt >= 5) {
3617c478bd9Sstevel@tonic-gate /* Enable performance counter overflow interrupt */
3627c478bd9Sstevel@tonic-gate
3637417cfdeSKuriakose Kuruvilla if (!is_x86_feature(x86_featureset, X86FSET_MSR))
3647c478bd9Sstevel@tonic-gate apic_enable_cpcovf_intr = 0;
3657c478bd9Sstevel@tonic-gate if (apic_enable_cpcovf_intr) {
3667c478bd9Sstevel@tonic-gate if (apic_cpcovf_vect == 0) {
3677c478bd9Sstevel@tonic-gate int ipl = APIC_PCINT_IPL;
3687c478bd9Sstevel@tonic-gate int irq = apic_get_ipivect(ipl, -1);
3697c478bd9Sstevel@tonic-gate
3707c478bd9Sstevel@tonic-gate ASSERT(irq != -1);
371da2743adSdmick apic_cpcovf_vect =
372da2743adSdmick apic_irq_table[irq]->airq_vector;
3737c478bd9Sstevel@tonic-gate ASSERT(apic_cpcovf_vect);
3747c478bd9Sstevel@tonic-gate (void) add_avintr(NULL, ipl,
3757c478bd9Sstevel@tonic-gate (avfunc)kcpc_hw_overflow_intr,
3767a364d25Sschwartz "apic pcint", irq, NULL, NULL, NULL, NULL);
3777c478bd9Sstevel@tonic-gate kcpc_hw_overflow_intr_installed = 1;
378da2743adSdmick kcpc_hw_enable_cpc_intr =
379da2743adSdmick apic_cpcovf_mask_clear;
3807c478bd9Sstevel@tonic-gate }
381b6917abeSmishra apic_reg_ops->apic_write(APIC_PCINT_VECT,
382b6917abeSmishra apic_cpcovf_vect);
3837c478bd9Sstevel@tonic-gate }
384da2743adSdmick }
385da2743adSdmick
386da2743adSdmick if (nlvt >= 6) {
387da2743adSdmick /* Only mask TM intr if the BIOS apparently doesn't use it */
388da2743adSdmick
389da2743adSdmick uint32_t lvtval;
390da2743adSdmick
391b6917abeSmishra lvtval = apic_reg_ops->apic_read(APIC_THERM_VECT);
392da2743adSdmick if (((lvtval & AV_MASK) == AV_MASK) ||
393da2743adSdmick ((lvtval & AV_DELIV_MODE) != AV_SMI)) {
394b6917abeSmishra apic_reg_ops->apic_write(APIC_THERM_VECT,
395b6917abeSmishra AV_MASK|APIC_RESV_IRQ);
396da2743adSdmick }
397da2743adSdmick }
3987c478bd9Sstevel@tonic-gate
3997c478bd9Sstevel@tonic-gate /* Enable error interrupt */
4007c478bd9Sstevel@tonic-gate
401da2743adSdmick if (nlvt >= 4 && apic_enable_error_intr) {
4027c478bd9Sstevel@tonic-gate if (apic_errvect == 0) {
4037c478bd9Sstevel@tonic-gate int ipl = 0xf; /* get highest priority intr */
4047c478bd9Sstevel@tonic-gate int irq = apic_get_ipivect(ipl, -1);
4057c478bd9Sstevel@tonic-gate
4067c478bd9Sstevel@tonic-gate ASSERT(irq != -1);
4077c478bd9Sstevel@tonic-gate apic_errvect = apic_irq_table[irq]->airq_vector;
4087c478bd9Sstevel@tonic-gate ASSERT(apic_errvect);
4097c478bd9Sstevel@tonic-gate /*
4107c478bd9Sstevel@tonic-gate * Not PSMI compliant, but we are going to merge
4117c478bd9Sstevel@tonic-gate * with ON anyway
4127c478bd9Sstevel@tonic-gate */
4137c478bd9Sstevel@tonic-gate (void) add_avintr((void *)NULL, ipl,
4147c478bd9Sstevel@tonic-gate (avfunc)apic_error_intr, "apic error intr",
4157a364d25Sschwartz irq, NULL, NULL, NULL, NULL);
4167c478bd9Sstevel@tonic-gate }
417b6917abeSmishra apic_reg_ops->apic_write(APIC_ERR_VECT, apic_errvect);
418b6917abeSmishra apic_reg_ops->apic_write(APIC_ERROR_STATUS, 0);
419b6917abeSmishra apic_reg_ops->apic_write(APIC_ERROR_STATUS, 0);
4207c478bd9Sstevel@tonic-gate }
421da2743adSdmick
422e3d60c9bSAdrian Frost /* Enable CMCI interrupt */
423e3d60c9bSAdrian Frost if (cmi_enable_cmci) {
424e3d60c9bSAdrian Frost
425e3d60c9bSAdrian Frost mutex_enter(&cmci_cpu_setup_lock);
426e3d60c9bSAdrian Frost if (cmci_cpu_setup_registered == 0) {
427e3d60c9bSAdrian Frost mutex_enter(&cpu_lock);
428e3d60c9bSAdrian Frost register_cpu_setup_func(cmci_cpu_setup, NULL);
429e3d60c9bSAdrian Frost mutex_exit(&cpu_lock);
430e3d60c9bSAdrian Frost cmci_cpu_setup_registered = 1;
431e3d60c9bSAdrian Frost }
432e3d60c9bSAdrian Frost mutex_exit(&cmci_cpu_setup_lock);
433e3d60c9bSAdrian Frost
434e3d60c9bSAdrian Frost if (apic_cmci_vect == 0) {
435e3d60c9bSAdrian Frost int ipl = 0x2;
436e3d60c9bSAdrian Frost int irq = apic_get_ipivect(ipl, -1);
437e3d60c9bSAdrian Frost
438e3d60c9bSAdrian Frost ASSERT(irq != -1);
439e3d60c9bSAdrian Frost apic_cmci_vect = apic_irq_table[irq]->airq_vector;
440e3d60c9bSAdrian Frost ASSERT(apic_cmci_vect);
441e3d60c9bSAdrian Frost
442e3d60c9bSAdrian Frost (void) add_avintr(NULL, ipl,
443e3d60c9bSAdrian Frost (avfunc)cmi_cmci_trap,
444e3d60c9bSAdrian Frost "apic cmci intr", irq, NULL, NULL, NULL, NULL);
445e3d60c9bSAdrian Frost }
446e3d60c9bSAdrian Frost apic_reg_ops->apic_write(APIC_CMCI_VECT, apic_cmci_vect);
447e3d60c9bSAdrian Frost }
4487c478bd9Sstevel@tonic-gate }
4497c478bd9Sstevel@tonic-gate
4507c478bd9Sstevel@tonic-gate static void
apic_picinit(void)4517c478bd9Sstevel@tonic-gate apic_picinit(void)
4527c478bd9Sstevel@tonic-gate {
453ae115bc7Smrj int i, j;
4547c478bd9Sstevel@tonic-gate uint_t isr;
4557c478bd9Sstevel@tonic-gate
4567c478bd9Sstevel@tonic-gate /*
457f7a1836aSSaurabh Misra * Initialize and enable interrupt remapping before apic
4581de082f7SVikram Hegde * hardware initialization
4591de082f7SVikram Hegde */
4603a634bfcSVikram Hegde apic_intrmap_init(apic_mode);
4611de082f7SVikram Hegde
4621de082f7SVikram Hegde /*
4637c478bd9Sstevel@tonic-gate * On UniSys Model 6520, the BIOS leaves vector 0x20 isr
4647c478bd9Sstevel@tonic-gate * bit on without clearing it with EOI. Since softint
4657c478bd9Sstevel@tonic-gate * uses vector 0x20 to interrupt itself, so softint will
4667c478bd9Sstevel@tonic-gate * not work on this machine. In order to fix this problem
4677c478bd9Sstevel@tonic-gate * a check is made to verify all the isr bits are clear.
4687c478bd9Sstevel@tonic-gate * If not, EOIs are issued to clear the bits.
4697c478bd9Sstevel@tonic-gate */
4707c478bd9Sstevel@tonic-gate for (i = 7; i >= 1; i--) {
471b6917abeSmishra isr = apic_reg_ops->apic_read(APIC_ISR_REG + (i * 4));
472b6917abeSmishra if (isr != 0)
4737c478bd9Sstevel@tonic-gate for (j = 0; ((j < 32) && (isr != 0)); j++)
4747c478bd9Sstevel@tonic-gate if (isr & (1 << j)) {
475b6917abeSmishra apic_reg_ops->apic_write(
476b6917abeSmishra APIC_EOI_REG, 0);
4777c478bd9Sstevel@tonic-gate isr &= ~(1 << j);
4787c478bd9Sstevel@tonic-gate apic_error |= APIC_ERR_BOOT_EOI;
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate }
4817c478bd9Sstevel@tonic-gate
4827c478bd9Sstevel@tonic-gate /* set a flag so we know we have run apic_picinit() */
483843e1988Sjohnlev apic_picinit_called = 1;
4847c478bd9Sstevel@tonic-gate LOCK_INIT_CLEAR(&apic_gethrtime_lock);
4857c478bd9Sstevel@tonic-gate LOCK_INIT_CLEAR(&apic_ioapic_lock);
4867c478bd9Sstevel@tonic-gate LOCK_INIT_CLEAR(&apic_error_lock);
487a3114836SGerry Liu LOCK_INIT_CLEAR(&apic_mode_switch_lock);
4887c478bd9Sstevel@tonic-gate
4897c478bd9Sstevel@tonic-gate picsetup(); /* initialise the 8259 */
4907c478bd9Sstevel@tonic-gate
4917c478bd9Sstevel@tonic-gate /* add nmi handler - least priority nmi handler */
4927c478bd9Sstevel@tonic-gate LOCK_INIT_CLEAR(&apic_nmi_lock);
4937c478bd9Sstevel@tonic-gate
4947c478bd9Sstevel@tonic-gate if (!psm_add_nmintr(0, (avfunc) apic_nmi_intr,
4957c478bd9Sstevel@tonic-gate "pcplusmp NMI handler", (caddr_t)NULL))
4967c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "pcplusmp: Unable to add nmi handler");
4977c478bd9Sstevel@tonic-gate
498f7a1836aSSaurabh Misra /*
499f7a1836aSSaurabh Misra * Check for directed-EOI capability in the local APIC.
500f7a1836aSSaurabh Misra */
501f7a1836aSSaurabh Misra if (apic_directed_EOI_supported() == 1) {
502f7a1836aSSaurabh Misra apic_set_directed_EOI_handler();
503f7a1836aSSaurabh Misra }
504f7a1836aSSaurabh Misra
5057c478bd9Sstevel@tonic-gate apic_init_intr();
5067c478bd9Sstevel@tonic-gate
5077c478bd9Sstevel@tonic-gate /* enable apic mode if imcr present */
5087c478bd9Sstevel@tonic-gate if (apic_imcrp) {
5097c478bd9Sstevel@tonic-gate outb(APIC_IMCR_P1, (uchar_t)APIC_IMCR_SELECT);
5107c478bd9Sstevel@tonic-gate outb(APIC_IMCR_P2, (uchar_t)APIC_IMCR_APIC);
5117c478bd9Sstevel@tonic-gate }
5127c478bd9Sstevel@tonic-gate
513ae115bc7Smrj ioapic_init_intr(IOAPIC_MASK);
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate
5165d8efbbcSSaurabh Misra #ifdef DEBUG
5177c478bd9Sstevel@tonic-gate void
apic_break(void)5187ff178cdSJimmy Vetayases apic_break(void)
5197c478bd9Sstevel@tonic-gate {
5207c478bd9Sstevel@tonic-gate }
5217c478bd9Sstevel@tonic-gate #endif /* DEBUG */
5227c478bd9Sstevel@tonic-gate
5237c478bd9Sstevel@tonic-gate /*
5247c478bd9Sstevel@tonic-gate * platform_intr_enter
5257c478bd9Sstevel@tonic-gate *
5267c478bd9Sstevel@tonic-gate * Called at the beginning of the interrupt service routine to
5277c478bd9Sstevel@tonic-gate * mask all level equal to and below the interrupt priority
5287c478bd9Sstevel@tonic-gate * of the interrupting vector. An EOI should be given to
5297c478bd9Sstevel@tonic-gate * the interrupt controller to enable other HW interrupts.
5307c478bd9Sstevel@tonic-gate *
5317c478bd9Sstevel@tonic-gate * Return -1 for spurious interrupts
5327c478bd9Sstevel@tonic-gate *
5337c478bd9Sstevel@tonic-gate */
5347c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5357c478bd9Sstevel@tonic-gate static int
apic_intr_enter(int ipl,int * vectorp)5367c478bd9Sstevel@tonic-gate apic_intr_enter(int ipl, int *vectorp)
5377c478bd9Sstevel@tonic-gate {
5387c478bd9Sstevel@tonic-gate uchar_t vector;
5397c478bd9Sstevel@tonic-gate int nipl;
540ae115bc7Smrj int irq;
541ae115bc7Smrj ulong_t iflag;
5427c478bd9Sstevel@tonic-gate apic_cpus_info_t *cpu_infop;
5437c478bd9Sstevel@tonic-gate
5447c478bd9Sstevel@tonic-gate /*
545c8589f13Ssethg * The real vector delivered is (*vectorp + 0x20), but our caller
546c8589f13Ssethg * subtracts 0x20 from the vector before passing it to us.
547c8589f13Ssethg * (That's why APIC_BASE_VECT is 0x20.)
5487c478bd9Sstevel@tonic-gate */
5497c478bd9Sstevel@tonic-gate vector = (uchar_t)*vectorp;
5507c478bd9Sstevel@tonic-gate
5517c478bd9Sstevel@tonic-gate /* if interrupted by the clock, increment apic_nsec_since_boot */
5527c478bd9Sstevel@tonic-gate if (vector == apic_clkvect) {
5537c478bd9Sstevel@tonic-gate if (!apic_oneshot) {
5547c478bd9Sstevel@tonic-gate /* NOTE: this is not MT aware */
5557c478bd9Sstevel@tonic-gate apic_hrtime_stamp++;
5567c478bd9Sstevel@tonic-gate apic_nsec_since_boot += apic_nsec_per_intr;
5577c478bd9Sstevel@tonic-gate apic_hrtime_stamp++;
5587c478bd9Sstevel@tonic-gate last_count_read = apic_hertz_count;
5597c478bd9Sstevel@tonic-gate apic_redistribute_compute();
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate
5627c478bd9Sstevel@tonic-gate /* We will avoid all the book keeping overhead for clock */
563c8589f13Ssethg nipl = apic_ipls[vector];
564c8589f13Ssethg
5657c478bd9Sstevel@tonic-gate *vectorp = apic_vector_to_irq[vector + APIC_BASE_VECT];
56629ddbbefSJosef 'Jeff' Sipek
56729ddbbefSJosef 'Jeff' Sipek apic_reg_ops->apic_write_task_reg(apic_ipltopri[nipl]);
56829ddbbefSJosef 'Jeff' Sipek apic_reg_ops->apic_send_eoi(0);
569b6917abeSmishra
5707c478bd9Sstevel@tonic-gate return (nipl);
5717c478bd9Sstevel@tonic-gate }
5727c478bd9Sstevel@tonic-gate
5737c478bd9Sstevel@tonic-gate cpu_infop = &apic_cpus[psm_get_cpu_id()];
5747c478bd9Sstevel@tonic-gate
5757c478bd9Sstevel@tonic-gate if (vector == (APIC_SPUR_INTR - APIC_BASE_VECT)) {
5767c478bd9Sstevel@tonic-gate cpu_infop->aci_spur_cnt++;
5777c478bd9Sstevel@tonic-gate return (APIC_INT_SPURIOUS);
5787c478bd9Sstevel@tonic-gate }
5797c478bd9Sstevel@tonic-gate
5807c478bd9Sstevel@tonic-gate /* Check if the vector we got is really what we need */
5817c478bd9Sstevel@tonic-gate if (apic_revector_pending) {
5827c478bd9Sstevel@tonic-gate /*
5837c478bd9Sstevel@tonic-gate * Disable interrupts for the duration of
5847c478bd9Sstevel@tonic-gate * the vector translation to prevent a self-race for
5857c478bd9Sstevel@tonic-gate * the apic_revector_lock. This cannot be done
5867c478bd9Sstevel@tonic-gate * in apic_xlate_vector because it is recursive and
5877c478bd9Sstevel@tonic-gate * we want the vector translation to be atomic with
5887c478bd9Sstevel@tonic-gate * respect to other (higher-priority) interrupts.
5897c478bd9Sstevel@tonic-gate */
5907c478bd9Sstevel@tonic-gate iflag = intr_clear();
5917c478bd9Sstevel@tonic-gate vector = apic_xlate_vector(vector + APIC_BASE_VECT) -
5927c478bd9Sstevel@tonic-gate APIC_BASE_VECT;
5937c478bd9Sstevel@tonic-gate intr_restore(iflag);
5947c478bd9Sstevel@tonic-gate }
5957c478bd9Sstevel@tonic-gate
596c8589f13Ssethg nipl = apic_ipls[vector];
5977c478bd9Sstevel@tonic-gate *vectorp = irq = apic_vector_to_irq[vector + APIC_BASE_VECT];
5987c478bd9Sstevel@tonic-gate
59929ddbbefSJosef 'Jeff' Sipek apic_reg_ops->apic_write_task_reg(apic_ipltopri[nipl]);
6007c478bd9Sstevel@tonic-gate
6017c478bd9Sstevel@tonic-gate cpu_infop->aci_current[nipl] = (uchar_t)irq;
6027c478bd9Sstevel@tonic-gate cpu_infop->aci_curipl = (uchar_t)nipl;
6037c478bd9Sstevel@tonic-gate cpu_infop->aci_ISR_in_progress |= 1 << nipl;
6047c478bd9Sstevel@tonic-gate
6057c478bd9Sstevel@tonic-gate /*
6067c478bd9Sstevel@tonic-gate * apic_level_intr could have been assimilated into the irq struct.
6077c478bd9Sstevel@tonic-gate * but, having it as a character array is more efficient in terms of
6087c478bd9Sstevel@tonic-gate * cache usage. So, we leave it as is.
6097c478bd9Sstevel@tonic-gate */
610b6917abeSmishra if (!apic_level_intr[irq]) {
61129ddbbefSJosef 'Jeff' Sipek apic_reg_ops->apic_send_eoi(0);
61287cc6269SSaurabh Misra }
6137c478bd9Sstevel@tonic-gate
6147c478bd9Sstevel@tonic-gate #ifdef DEBUG
6157c478bd9Sstevel@tonic-gate APIC_DEBUG_BUF_PUT(vector);
6167c478bd9Sstevel@tonic-gate APIC_DEBUG_BUF_PUT(irq);
6177c478bd9Sstevel@tonic-gate APIC_DEBUG_BUF_PUT(nipl);
6187c478bd9Sstevel@tonic-gate APIC_DEBUG_BUF_PUT(psm_get_cpu_id());
6197c478bd9Sstevel@tonic-gate if ((apic_stretch_interrupts) && (apic_stretch_ISR & (1 << nipl)))
6207c478bd9Sstevel@tonic-gate drv_usecwait(apic_stretch_interrupts);
6217c478bd9Sstevel@tonic-gate
6227c478bd9Sstevel@tonic-gate if (apic_break_on_cpu == psm_get_cpu_id())
6237c478bd9Sstevel@tonic-gate apic_break();
6247c478bd9Sstevel@tonic-gate #endif /* DEBUG */
6257c478bd9Sstevel@tonic-gate return (nipl);
6267c478bd9Sstevel@tonic-gate }
6277c478bd9Sstevel@tonic-gate
628b6917abeSmishra /*
629325e77f4SSaurabh Misra * This macro is a common code used by MMIO local apic and X2APIC
630b6917abeSmishra * local apic.
631b6917abeSmishra */
632b6917abeSmishra #define APIC_INTR_EXIT() \
633b6917abeSmishra { \
634b6917abeSmishra cpu_infop = &apic_cpus[psm_get_cpu_id()]; \
635b6917abeSmishra if (apic_level_intr[irq]) \
636b6917abeSmishra apic_reg_ops->apic_send_eoi(irq); \
637b6917abeSmishra cpu_infop->aci_curipl = (uchar_t)prev_ipl; \
638b6917abeSmishra /* ISR above current pri could not be in progress */ \
639b6917abeSmishra cpu_infop->aci_ISR_in_progress &= (2 << prev_ipl) - 1; \
640b6917abeSmishra }
641b6917abeSmishra
642b6917abeSmishra /*
643325e77f4SSaurabh Misra * Any changes made to this function must also change X2APIC
644b6917abeSmishra * version of intr_exit.
645b6917abeSmishra */
646ae115bc7Smrj void
apic_intr_exit(int prev_ipl,int irq)6477c478bd9Sstevel@tonic-gate apic_intr_exit(int prev_ipl, int irq)
6487c478bd9Sstevel@tonic-gate {
6497c478bd9Sstevel@tonic-gate apic_cpus_info_t *cpu_infop;
6507c478bd9Sstevel@tonic-gate
65129ddbbefSJosef 'Jeff' Sipek apic_reg_ops->apic_write_task_reg(apic_ipltopri[prev_ipl]);
6527c478bd9Sstevel@tonic-gate
653b6917abeSmishra APIC_INTR_EXIT();
654b6917abeSmishra }
6557c478bd9Sstevel@tonic-gate
656b6917abeSmishra /*
657b6917abeSmishra * Same as apic_intr_exit() except it uses MSR rather than MMIO
658b6917abeSmishra * to access local apic registers.
659b6917abeSmishra */
660b6917abeSmishra void
x2apic_intr_exit(int prev_ipl,int irq)661b6917abeSmishra x2apic_intr_exit(int prev_ipl, int irq)
662b6917abeSmishra {
663b6917abeSmishra apic_cpus_info_t *cpu_infop;
664b6917abeSmishra
665b6917abeSmishra X2APIC_WRITE(APIC_TASK_REG, apic_ipltopri[prev_ipl]);
666b6917abeSmishra APIC_INTR_EXIT();
6677c478bd9Sstevel@tonic-gate }
6687c478bd9Sstevel@tonic-gate
669843e1988Sjohnlev intr_exit_fn_t
psm_intr_exit_fn(void)670843e1988Sjohnlev psm_intr_exit_fn(void)
671843e1988Sjohnlev {
672b6917abeSmishra if (apic_mode == LOCAL_X2APIC)
673b6917abeSmishra return (x2apic_intr_exit);
674b6917abeSmishra
675843e1988Sjohnlev return (apic_intr_exit);
676843e1988Sjohnlev }
677843e1988Sjohnlev
6787c478bd9Sstevel@tonic-gate /*
679b6917abeSmishra * Mask all interrupts below or equal to the given IPL.
680325e77f4SSaurabh Misra * Any changes made to this function must also change X2APIC
681b6917abeSmishra * version of setspl.
6827c478bd9Sstevel@tonic-gate */
6837c478bd9Sstevel@tonic-gate static void
apic_setspl(int ipl)6847c478bd9Sstevel@tonic-gate apic_setspl(int ipl)
6857c478bd9Sstevel@tonic-gate {
68629ddbbefSJosef 'Jeff' Sipek apic_reg_ops->apic_write_task_reg(apic_ipltopri[ipl]);
6877c478bd9Sstevel@tonic-gate
6887c478bd9Sstevel@tonic-gate /* interrupts at ipl above this cannot be in progress */
6897c478bd9Sstevel@tonic-gate apic_cpus[psm_get_cpu_id()].aci_ISR_in_progress &= (2 << ipl) - 1;
6907c478bd9Sstevel@tonic-gate /*
6917c478bd9Sstevel@tonic-gate * this is a patch fix for the ALR QSMP P5 machine, so that interrupts
6927c478bd9Sstevel@tonic-gate * have enough time to come in before the priority is raised again
6937c478bd9Sstevel@tonic-gate * during the idle() loop.
6947c478bd9Sstevel@tonic-gate */
6957c478bd9Sstevel@tonic-gate if (apic_setspl_delay)
696b6917abeSmishra (void) apic_reg_ops->apic_get_pri();
697b6917abeSmishra }
698b6917abeSmishra
699b6917abeSmishra /*
700325e77f4SSaurabh Misra * X2APIC version of setspl.
701b6917abeSmishra * Mask all interrupts below or equal to the given IPL
702b6917abeSmishra */
703b6917abeSmishra static void
x2apic_setspl(int ipl)704b6917abeSmishra x2apic_setspl(int ipl)
705b6917abeSmishra {
706b6917abeSmishra X2APIC_WRITE(APIC_TASK_REG, apic_ipltopri[ipl]);
707b6917abeSmishra
708b6917abeSmishra /* interrupts at ipl above this cannot be in progress */
709b6917abeSmishra apic_cpus[psm_get_cpu_id()].aci_ISR_in_progress &= (2 << ipl) - 1;
7107c478bd9Sstevel@tonic-gate }
7117c478bd9Sstevel@tonic-gate
7127c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7137c478bd9Sstevel@tonic-gate static int
apic_addspl(int irqno,int ipl,int min_ipl,int max_ipl)7147c478bd9Sstevel@tonic-gate apic_addspl(int irqno, int ipl, int min_ipl, int max_ipl)
7157c478bd9Sstevel@tonic-gate {
716ae115bc7Smrj return (apic_addspl_common(irqno, ipl, min_ipl, max_ipl));
7177c478bd9Sstevel@tonic-gate }
7187c478bd9Sstevel@tonic-gate
7197c478bd9Sstevel@tonic-gate static int
apic_delspl(int irqno,int ipl,int min_ipl,int max_ipl)7207c478bd9Sstevel@tonic-gate apic_delspl(int irqno, int ipl, int min_ipl, int max_ipl)
7217c478bd9Sstevel@tonic-gate {
722ae115bc7Smrj return (apic_delspl_common(irqno, ipl, min_ipl, max_ipl));
7237c478bd9Sstevel@tonic-gate }
7247c478bd9Sstevel@tonic-gate
7257c478bd9Sstevel@tonic-gate static int
apic_post_cpu_start(void)7267ff178cdSJimmy Vetayases apic_post_cpu_start(void)
7277c478bd9Sstevel@tonic-gate {
728afbc4541Ssherrym int cpun;
729325e77f4SSaurabh Misra static int cpus_started = 1;
730325e77f4SSaurabh Misra
731325e77f4SSaurabh Misra /* We know this CPU + BSP started successfully. */
732325e77f4SSaurabh Misra cpus_started++;
7337c478bd9Sstevel@tonic-gate
7345d8efbbcSSaurabh Misra /*
7355d8efbbcSSaurabh Misra * On BSP we would have enabled X2APIC, if supported by processor,
7365d8efbbcSSaurabh Misra * in acpi_probe(), but on AP we do it here.
737325e77f4SSaurabh Misra *
738325e77f4SSaurabh Misra * We enable X2APIC mode only if BSP is running in X2APIC & the
739325e77f4SSaurabh Misra * local APIC mode of the current CPU is MMIO (xAPIC).
7405d8efbbcSSaurabh Misra */
741325e77f4SSaurabh Misra if (apic_mode == LOCAL_X2APIC && apic_detect_x2apic() &&
742325e77f4SSaurabh Misra apic_local_mode() == LOCAL_APIC) {
7435d8efbbcSSaurabh Misra apic_enable_x2apic();
7445d8efbbcSSaurabh Misra }
7455d8efbbcSSaurabh Misra
746325e77f4SSaurabh Misra /*
747a3114836SGerry Liu * Switch back to x2apic IPI sending method for performance when target
748a3114836SGerry Liu * CPU has entered x2apic mode.
749325e77f4SSaurabh Misra */
750a3114836SGerry Liu if (apic_mode == LOCAL_X2APIC) {
751a3114836SGerry Liu apic_switch_ipi_callback(B_FALSE);
752325e77f4SSaurabh Misra }
753325e77f4SSaurabh Misra
7545d8efbbcSSaurabh Misra splx(ipltospl(LOCK_LEVEL));
7557c478bd9Sstevel@tonic-gate apic_init_intr();
7567c478bd9Sstevel@tonic-gate
7577c478bd9Sstevel@tonic-gate /*
7587c478bd9Sstevel@tonic-gate * since some systems don't enable the internal cache on the non-boot
7597c478bd9Sstevel@tonic-gate * cpus, so we have to enable them here
7607c478bd9Sstevel@tonic-gate */
761ae115bc7Smrj setcr0(getcr0() & ~(CR0_CD | CR0_NW));
7627c478bd9Sstevel@tonic-gate
7635d8efbbcSSaurabh Misra #ifdef DEBUG
7645d8efbbcSSaurabh Misra APIC_AV_PENDING_SET();
7655d8efbbcSSaurabh Misra #else
7665d8efbbcSSaurabh Misra if (apic_mode == LOCAL_APIC)
7675d8efbbcSSaurabh Misra APIC_AV_PENDING_SET();
7685d8efbbcSSaurabh Misra #endif /* DEBUG */
7697c478bd9Sstevel@tonic-gate
770643e2e74Sbholler /*
771643e2e74Sbholler * We may be booting, or resuming from suspend; aci_status will
772643e2e74Sbholler * be APIC_CPU_INTR_ENABLE if coming from suspend, so we add the
773643e2e74Sbholler * APIC_CPU_ONLINE flag here rather than setting aci_status completely.
774643e2e74Sbholler */
7757c478bd9Sstevel@tonic-gate cpun = psm_get_cpu_id();
776643e2e74Sbholler apic_cpus[cpun].aci_status |= APIC_CPU_ONLINE;
7777c478bd9Sstevel@tonic-gate
778b6917abeSmishra apic_reg_ops->apic_write(APIC_DIVIDE_REG, apic_divide_reg_init);
7797c478bd9Sstevel@tonic-gate return (PSM_SUCCESS);
7807c478bd9Sstevel@tonic-gate }
7817c478bd9Sstevel@tonic-gate
7827c478bd9Sstevel@tonic-gate /*
7837c478bd9Sstevel@tonic-gate * type == -1 indicates it is an internal request. Do not change
7847c478bd9Sstevel@tonic-gate * resv_vector for these requests
7857c478bd9Sstevel@tonic-gate */
7867c478bd9Sstevel@tonic-gate static int
apic_get_ipivect(int ipl,int type)7877c478bd9Sstevel@tonic-gate apic_get_ipivect(int ipl, int type)
7887c478bd9Sstevel@tonic-gate {
7897c478bd9Sstevel@tonic-gate uchar_t vector;
7907c478bd9Sstevel@tonic-gate int irq;
7917c478bd9Sstevel@tonic-gate
79286a9c507SGuoli Shu if ((irq = apic_allocate_irq(APIC_VECTOR(ipl))) != -1) {
793*a833a696SJosef 'Jeff' Sipek if ((vector = apic_allocate_vector(ipl, irq, 1))) {
7947c478bd9Sstevel@tonic-gate apic_irq_table[irq]->airq_mps_intr_index =
7957c478bd9Sstevel@tonic-gate RESERVE_INDEX;
7967c478bd9Sstevel@tonic-gate apic_irq_table[irq]->airq_vector = vector;
7977c478bd9Sstevel@tonic-gate if (type != -1) {
7987c478bd9Sstevel@tonic-gate apic_resv_vector[ipl] = vector;
7997c478bd9Sstevel@tonic-gate }
8007c478bd9Sstevel@tonic-gate return (irq);
8017c478bd9Sstevel@tonic-gate }
8027c478bd9Sstevel@tonic-gate }
8037c478bd9Sstevel@tonic-gate apic_error |= APIC_ERR_GET_IPIVECT_FAIL;
8047c478bd9Sstevel@tonic-gate return (-1); /* shouldn't happen */
8057c478bd9Sstevel@tonic-gate }
8067c478bd9Sstevel@tonic-gate
8077c478bd9Sstevel@tonic-gate static int
apic_getclkirq(int ipl)8087c478bd9Sstevel@tonic-gate apic_getclkirq(int ipl)
8097c478bd9Sstevel@tonic-gate {
8107c478bd9Sstevel@tonic-gate int irq;
8117c478bd9Sstevel@tonic-gate
8127c478bd9Sstevel@tonic-gate if ((irq = apic_get_ipivect(ipl, -1)) == -1)
8137c478bd9Sstevel@tonic-gate return (-1);
8147c478bd9Sstevel@tonic-gate /*
8157c478bd9Sstevel@tonic-gate * Note the vector in apic_clkvect for per clock handling.
8167c478bd9Sstevel@tonic-gate */
8177c478bd9Sstevel@tonic-gate apic_clkvect = apic_irq_table[irq]->airq_vector - APIC_BASE_VECT;
8187c478bd9Sstevel@tonic-gate APIC_VERBOSE_IOAPIC((CE_NOTE, "get_clkirq: vector = %x\n",
8197c478bd9Sstevel@tonic-gate apic_clkvect));
8207c478bd9Sstevel@tonic-gate return (irq);
8217c478bd9Sstevel@tonic-gate }
8227c478bd9Sstevel@tonic-gate
8237c478bd9Sstevel@tonic-gate /*
8247c478bd9Sstevel@tonic-gate * Try and disable all interrupts. We just assign interrupts to other
8257c478bd9Sstevel@tonic-gate * processors based on policy. If any were bound by user request, we
8267c478bd9Sstevel@tonic-gate * let them continue and return failure. We do not bother to check
8277c478bd9Sstevel@tonic-gate * for cache affinity while rebinding.
8287c478bd9Sstevel@tonic-gate */
8297c478bd9Sstevel@tonic-gate
8307c478bd9Sstevel@tonic-gate static int
apic_disable_intr(processorid_t cpun)8317c478bd9Sstevel@tonic-gate apic_disable_intr(processorid_t cpun)
8327c478bd9Sstevel@tonic-gate {
833ae115bc7Smrj int bind_cpu = 0, i, hardbound = 0;
8347c478bd9Sstevel@tonic-gate apic_irq_t *irq_ptr;
835ae115bc7Smrj ulong_t iflag;
8367c478bd9Sstevel@tonic-gate
8377c478bd9Sstevel@tonic-gate iflag = intr_clear();
8387c478bd9Sstevel@tonic-gate lock_set(&apic_ioapic_lock);
839e250f1e2Ssethg
840e250f1e2Ssethg for (i = 0; i <= APIC_MAX_VECTOR; i++) {
841e250f1e2Ssethg if (apic_reprogram_info[i].done == B_FALSE) {
842e250f1e2Ssethg if (apic_reprogram_info[i].bindcpu == cpun) {
843e250f1e2Ssethg /*
844e250f1e2Ssethg * CPU is busy -- it's the target of
845e250f1e2Ssethg * a pending reprogramming attempt
846e250f1e2Ssethg */
8477c478bd9Sstevel@tonic-gate lock_clear(&apic_ioapic_lock);
8487c478bd9Sstevel@tonic-gate intr_restore(iflag);
849e250f1e2Ssethg return (PSM_FAILURE);
850e250f1e2Ssethg }
851e250f1e2Ssethg }
852e250f1e2Ssethg }
853e250f1e2Ssethg
854e250f1e2Ssethg apic_cpus[cpun].aci_status &= ~APIC_CPU_INTR_ENABLE;
855e250f1e2Ssethg
8567c478bd9Sstevel@tonic-gate apic_cpus[cpun].aci_curipl = 0;
857e250f1e2Ssethg
8587c478bd9Sstevel@tonic-gate i = apic_min_device_irq;
8597c478bd9Sstevel@tonic-gate for (; i <= apic_max_device_irq; i++) {
8607c478bd9Sstevel@tonic-gate /*
8617c478bd9Sstevel@tonic-gate * If there are bound interrupts on this cpu, then
8627c478bd9Sstevel@tonic-gate * rebind them to other processors.
8637c478bd9Sstevel@tonic-gate */
8647c478bd9Sstevel@tonic-gate if ((irq_ptr = apic_irq_table[i]) != NULL) {
8657c478bd9Sstevel@tonic-gate ASSERT((irq_ptr->airq_temp_cpu == IRQ_UNBOUND) ||
8667c478bd9Sstevel@tonic-gate (irq_ptr->airq_temp_cpu == IRQ_UNINIT) ||
867a3114836SGerry Liu (apic_cpu_in_range(irq_ptr->airq_temp_cpu)));
8687c478bd9Sstevel@tonic-gate
8697c478bd9Sstevel@tonic-gate if (irq_ptr->airq_temp_cpu == (cpun | IRQ_USER_BOUND)) {
8707c478bd9Sstevel@tonic-gate hardbound = 1;
8717c478bd9Sstevel@tonic-gate continue;
8727c478bd9Sstevel@tonic-gate }
8737c478bd9Sstevel@tonic-gate
8747c478bd9Sstevel@tonic-gate if (irq_ptr->airq_temp_cpu == cpun) {
8757c478bd9Sstevel@tonic-gate do {
876e7c3cdaeSjiang.liu@intel.com bind_cpu =
877e7c3cdaeSjiang.liu@intel.com apic_find_cpu(APIC_CPU_INTR_ENABLE);
878e250f1e2Ssethg } while (apic_rebind_all(irq_ptr, bind_cpu));
8797c478bd9Sstevel@tonic-gate }
8807c478bd9Sstevel@tonic-gate }
8817c478bd9Sstevel@tonic-gate }
882e250f1e2Ssethg
883e250f1e2Ssethg lock_clear(&apic_ioapic_lock);
884e250f1e2Ssethg intr_restore(iflag);
885e250f1e2Ssethg
8867c478bd9Sstevel@tonic-gate if (hardbound) {
8877c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "Could not disable interrupts on %d"
8887c478bd9Sstevel@tonic-gate "due to user bound interrupts", cpun);
8897c478bd9Sstevel@tonic-gate return (PSM_FAILURE);
8907c478bd9Sstevel@tonic-gate }
8917c478bd9Sstevel@tonic-gate else
8927c478bd9Sstevel@tonic-gate return (PSM_SUCCESS);
8937c478bd9Sstevel@tonic-gate }
8947c478bd9Sstevel@tonic-gate
895643e2e74Sbholler /*
896643e2e74Sbholler * Bind interrupts to the CPU's local APIC.
897643e2e74Sbholler * Interrupts should not be bound to a CPU's local APIC until the CPU
898643e2e74Sbholler * is ready to receive interrupts.
899643e2e74Sbholler */
9007c478bd9Sstevel@tonic-gate static void
apic_enable_intr(processorid_t cpun)9017c478bd9Sstevel@tonic-gate apic_enable_intr(processorid_t cpun)
9027c478bd9Sstevel@tonic-gate {
903ae115bc7Smrj int i;
9047c478bd9Sstevel@tonic-gate apic_irq_t *irq_ptr;
905ae115bc7Smrj ulong_t iflag;
9067c478bd9Sstevel@tonic-gate
9077c478bd9Sstevel@tonic-gate iflag = intr_clear();
9087c478bd9Sstevel@tonic-gate lock_set(&apic_ioapic_lock);
909e250f1e2Ssethg
9107c478bd9Sstevel@tonic-gate apic_cpus[cpun].aci_status |= APIC_CPU_INTR_ENABLE;
9117c478bd9Sstevel@tonic-gate
9127c478bd9Sstevel@tonic-gate i = apic_min_device_irq;
9137c478bd9Sstevel@tonic-gate for (i = apic_min_device_irq; i <= apic_max_device_irq; i++) {
9147c478bd9Sstevel@tonic-gate if ((irq_ptr = apic_irq_table[i]) != NULL) {
9157c478bd9Sstevel@tonic-gate if ((irq_ptr->airq_cpu & ~IRQ_USER_BOUND) == cpun) {
9167c478bd9Sstevel@tonic-gate (void) apic_rebind_all(irq_ptr,
917e250f1e2Ssethg irq_ptr->airq_cpu);
9187c478bd9Sstevel@tonic-gate }
9197c478bd9Sstevel@tonic-gate }
9207c478bd9Sstevel@tonic-gate }
921e250f1e2Ssethg
9227ff178cdSJimmy Vetayases if (apic_cpus[cpun].aci_status & APIC_CPU_SUSPEND)
9237ff178cdSJimmy Vetayases apic_cpus[cpun].aci_status &= ~APIC_CPU_SUSPEND;
9247ff178cdSJimmy Vetayases
925e250f1e2Ssethg lock_clear(&apic_ioapic_lock);
926e250f1e2Ssethg intr_restore(iflag);
9277c478bd9Sstevel@tonic-gate }
9287c478bd9Sstevel@tonic-gate
9297c478bd9Sstevel@tonic-gate /*
930dd4eeefdSeota * If this module needs a periodic handler for the interrupt distribution, it
931dd4eeefdSeota * can be added here. The argument to the periodic handler is not currently
932dd4eeefdSeota * used, but is reserved for future.
9337c478bd9Sstevel@tonic-gate */
9347c478bd9Sstevel@tonic-gate static void
apic_post_cyclic_setup(void * arg)9357c478bd9Sstevel@tonic-gate apic_post_cyclic_setup(void *arg)
9367c478bd9Sstevel@tonic-gate {
9377c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(arg))
938a288e5a9SJoshua M. Clulow
939a288e5a9SJoshua M. Clulow cyc_handler_t cyh;
940a288e5a9SJoshua M. Clulow cyc_time_t cyt;
941a288e5a9SJoshua M. Clulow
9427c478bd9Sstevel@tonic-gate /* cpu_lock is held */
943dd4eeefdSeota /* set up a periodic handler for intr redistribution */
9447c478bd9Sstevel@tonic-gate
9457c478bd9Sstevel@tonic-gate /*
9467c478bd9Sstevel@tonic-gate * In peridoc mode intr redistribution processing is done in
9477c478bd9Sstevel@tonic-gate * apic_intr_enter during clk intr processing
9487c478bd9Sstevel@tonic-gate */
9497c478bd9Sstevel@tonic-gate if (!apic_oneshot)
9507c478bd9Sstevel@tonic-gate return;
951a288e5a9SJoshua M. Clulow
952dd4eeefdSeota /*
953dd4eeefdSeota * Register a periodical handler for the redistribution processing.
954a288e5a9SJoshua M. Clulow * Though we would generally prefer to use the DDI interface for
955a288e5a9SJoshua M. Clulow * periodic handler invocation, ddi_periodic_add(9F), we are
956a288e5a9SJoshua M. Clulow * unfortunately already holding cpu_lock, which ddi_periodic_add will
957a288e5a9SJoshua M. Clulow * attempt to take for us. Thus, we add our own cyclic directly:
958dd4eeefdSeota */
959a288e5a9SJoshua M. Clulow cyh.cyh_func = (void (*)(void *))apic_redistribute_compute;
960a288e5a9SJoshua M. Clulow cyh.cyh_arg = NULL;
961a288e5a9SJoshua M. Clulow cyh.cyh_level = CY_LOW_LEVEL;
962a288e5a9SJoshua M. Clulow
963a288e5a9SJoshua M. Clulow cyt.cyt_when = 0;
964a288e5a9SJoshua M. Clulow cyt.cyt_interval = apic_redistribute_sample_interval;
965a288e5a9SJoshua M. Clulow
966a288e5a9SJoshua M. Clulow apic_cyclic_id = cyclic_add(&cyh, &cyt);
9677c478bd9Sstevel@tonic-gate }
9687c478bd9Sstevel@tonic-gate
9697c478bd9Sstevel@tonic-gate static void
apic_redistribute_compute(void)9707c478bd9Sstevel@tonic-gate apic_redistribute_compute(void)
9717c478bd9Sstevel@tonic-gate {
9727c478bd9Sstevel@tonic-gate int i, j, max_busy;
9737c478bd9Sstevel@tonic-gate
9747c478bd9Sstevel@tonic-gate if (apic_enable_dynamic_migration) {
9757c478bd9Sstevel@tonic-gate if (++apic_nticks == apic_sample_factor_redistribution) {
9767c478bd9Sstevel@tonic-gate /*
9777c478bd9Sstevel@tonic-gate * Time to call apic_intr_redistribute().
9787c478bd9Sstevel@tonic-gate * reset apic_nticks. This will cause max_busy
9797c478bd9Sstevel@tonic-gate * to be calculated below and if it is more than
9807c478bd9Sstevel@tonic-gate * apic_int_busy, we will do the whole thing
9817c478bd9Sstevel@tonic-gate */
9827c478bd9Sstevel@tonic-gate apic_nticks = 0;
9837c478bd9Sstevel@tonic-gate }
9847c478bd9Sstevel@tonic-gate max_busy = 0;
9857c478bd9Sstevel@tonic-gate for (i = 0; i < apic_nproc; i++) {
986a3114836SGerry Liu if (!apic_cpu_in_range(i))
987a3114836SGerry Liu continue;
9887c478bd9Sstevel@tonic-gate
9897c478bd9Sstevel@tonic-gate /*
9907c478bd9Sstevel@tonic-gate * Check if curipl is non zero & if ISR is in
9917c478bd9Sstevel@tonic-gate * progress
9927c478bd9Sstevel@tonic-gate */
9937c478bd9Sstevel@tonic-gate if (((j = apic_cpus[i].aci_curipl) != 0) &&
9947c478bd9Sstevel@tonic-gate (apic_cpus[i].aci_ISR_in_progress & (1 << j))) {
9957c478bd9Sstevel@tonic-gate
9967c478bd9Sstevel@tonic-gate int irq;
9977c478bd9Sstevel@tonic-gate apic_cpus[i].aci_busy++;
9987c478bd9Sstevel@tonic-gate irq = apic_cpus[i].aci_current[j];
9997c478bd9Sstevel@tonic-gate apic_irq_table[irq]->airq_busy++;
10007c478bd9Sstevel@tonic-gate }
10017c478bd9Sstevel@tonic-gate
10027c478bd9Sstevel@tonic-gate if (!apic_nticks &&
10037c478bd9Sstevel@tonic-gate (apic_cpus[i].aci_busy > max_busy))
10047c478bd9Sstevel@tonic-gate max_busy = apic_cpus[i].aci_busy;
10057c478bd9Sstevel@tonic-gate }
10067c478bd9Sstevel@tonic-gate if (!apic_nticks) {
10077c478bd9Sstevel@tonic-gate if (max_busy > apic_int_busy_mark) {
10087c478bd9Sstevel@tonic-gate /*
10097c478bd9Sstevel@tonic-gate * We could make the following check be
10107c478bd9Sstevel@tonic-gate * skipped > 1 in which case, we get a
10117c478bd9Sstevel@tonic-gate * redistribution at half the busy mark (due to
10127c478bd9Sstevel@tonic-gate * double interval). Need to be able to collect
10137c478bd9Sstevel@tonic-gate * more empirical data to decide if that is a
10147c478bd9Sstevel@tonic-gate * good strategy. Punt for now.
10157c478bd9Sstevel@tonic-gate */
1016ae115bc7Smrj if (apic_skipped_redistribute) {
10177c478bd9Sstevel@tonic-gate apic_cleanup_busy();
1018ae115bc7Smrj apic_skipped_redistribute = 0;
1019ae115bc7Smrj } else {
10207c478bd9Sstevel@tonic-gate apic_intr_redistribute();
1021ae115bc7Smrj }
10227c478bd9Sstevel@tonic-gate } else
10237c478bd9Sstevel@tonic-gate apic_skipped_redistribute++;
10247c478bd9Sstevel@tonic-gate }
10257c478bd9Sstevel@tonic-gate }
10267c478bd9Sstevel@tonic-gate }
10277c478bd9Sstevel@tonic-gate
10287c478bd9Sstevel@tonic-gate
1029ae115bc7Smrj /*
1030ae115bc7Smrj * The following functions are in the platform specific file so that they
1031ae115bc7Smrj * can be different functions depending on whether we are running on
1032ae115bc7Smrj * bare metal or a hypervisor.
1033ae115bc7Smrj */
1034ae115bc7Smrj
1035ae115bc7Smrj /*
1036a7639048Sjohnny * Check to make sure there are enough irq slots
1037e250f1e2Ssethg */
1038ae115bc7Smrj int
apic_check_free_irqs(int count)1039a7639048Sjohnny apic_check_free_irqs(int count)
1040a7639048Sjohnny {
1041a7639048Sjohnny int i, avail;
1042a7639048Sjohnny
1043a7639048Sjohnny avail = 0;
1044a7639048Sjohnny for (i = APIC_FIRST_FREE_IRQ; i < APIC_RESV_IRQ; i++) {
1045a7639048Sjohnny if ((apic_irq_table[i] == NULL) ||
1046a7639048Sjohnny apic_irq_table[i]->airq_mps_intr_index == FREE_INDEX) {
1047a7639048Sjohnny if (++avail >= count)
1048a7639048Sjohnny return (PSM_SUCCESS);
1049a7639048Sjohnny }
1050a7639048Sjohnny }
1051a7639048Sjohnny return (PSM_FAILURE);
1052a7639048Sjohnny }
1053a7639048Sjohnny
1054a7639048Sjohnny /*
1055a7639048Sjohnny * This function allocates "count" MSI vector(s) for the given "dip/pri/type"
1056a7639048Sjohnny */
1057a7639048Sjohnny int
apic_alloc_msi_vectors(dev_info_t * dip,int inum,int count,int pri,int behavior)1058a7639048Sjohnny apic_alloc_msi_vectors(dev_info_t *dip, int inum, int count, int pri,
1059ae115bc7Smrj int behavior)
1060e250f1e2Ssethg {
1061ae115bc7Smrj int rcount, i;
1062b6917abeSmishra uchar_t start, irqno;
1063b6917abeSmishra uint32_t cpu;
1064ae115bc7Smrj major_t major;
1065ae115bc7Smrj apic_irq_t *irqptr;
1066ae115bc7Smrj
1067a7639048Sjohnny DDI_INTR_IMPLDBG((CE_CONT, "apic_alloc_msi_vectors: dip=0x%p "
1068ae115bc7Smrj "inum=0x%x pri=0x%x count=0x%x behavior=%d\n",
1069a7639048Sjohnny (void *)dip, inum, pri, count, behavior));
1070ae115bc7Smrj
1071ae115bc7Smrj if (count > 1) {
1072ae115bc7Smrj if (behavior == DDI_INTR_ALLOC_STRICT &&
107363ea9ad2SEvan Yan apic_multi_msi_enable == 0)
1074ae115bc7Smrj return (0);
1075ae115bc7Smrj if (apic_multi_msi_enable == 0)
1076ae115bc7Smrj count = 1;
1077ae115bc7Smrj }
1078ae115bc7Smrj
1079ae115bc7Smrj if ((rcount = apic_navail_vector(dip, pri)) > count)
1080ae115bc7Smrj rcount = count;
1081ae115bc7Smrj else if (rcount == 0 || (rcount < count &&
1082ae115bc7Smrj behavior == DDI_INTR_ALLOC_STRICT))
1083ae115bc7Smrj return (0);
1084ae115bc7Smrj
1085ae115bc7Smrj /* if not ISP2, then round it down */
1086ae115bc7Smrj if (!ISP2(rcount))
1087ae115bc7Smrj rcount = 1 << (highbit(rcount) - 1);
1088ae115bc7Smrj
1089ae115bc7Smrj mutex_enter(&airq_mutex);
1090ae115bc7Smrj
1091ae115bc7Smrj for (start = 0; rcount > 0; rcount >>= 1) {
1092ae115bc7Smrj if ((start = apic_find_multi_vectors(pri, rcount)) != 0 ||
1093ae115bc7Smrj behavior == DDI_INTR_ALLOC_STRICT)
1094ae115bc7Smrj break;
1095ae115bc7Smrj }
1096ae115bc7Smrj
1097ae115bc7Smrj if (start == 0) {
1098ae115bc7Smrj /* no vector available */
1099ae115bc7Smrj mutex_exit(&airq_mutex);
1100ae115bc7Smrj return (0);
1101ae115bc7Smrj }
1102ae115bc7Smrj
1103a7639048Sjohnny if (apic_check_free_irqs(rcount) == PSM_FAILURE) {
1104a7639048Sjohnny /* not enough free irq slots available */
1105a7639048Sjohnny mutex_exit(&airq_mutex);
1106a7639048Sjohnny return (0);
1107a7639048Sjohnny }
1108a7639048Sjohnny
11095c066ec2SJerry Gilliam major = (dip != NULL) ? ddi_driver_major(dip) : 0;
1110ae115bc7Smrj for (i = 0; i < rcount; i++) {
1111ae115bc7Smrj if ((irqno = apic_allocate_irq(apic_first_avail_irq)) ==
1112ae115bc7Smrj (uchar_t)-1) {
1113a7639048Sjohnny /*
1114a7639048Sjohnny * shouldn't happen because of the
1115a7639048Sjohnny * apic_check_free_irqs() check earlier
1116a7639048Sjohnny */
1117ae115bc7Smrj mutex_exit(&airq_mutex);
1118a7639048Sjohnny DDI_INTR_IMPLDBG((CE_CONT, "apic_alloc_msi_vectors: "
1119ae115bc7Smrj "apic_allocate_irq failed\n"));
1120ae115bc7Smrj return (i);
1121ae115bc7Smrj }
1122ae115bc7Smrj apic_max_device_irq = max(irqno, apic_max_device_irq);
1123ae115bc7Smrj apic_min_device_irq = min(irqno, apic_min_device_irq);
1124ae115bc7Smrj irqptr = apic_irq_table[irqno];
1125ae115bc7Smrj #ifdef DEBUG
1126ae115bc7Smrj if (apic_vector_to_irq[start + i] != APIC_RESV_IRQ)
1127a7639048Sjohnny DDI_INTR_IMPLDBG((CE_CONT, "apic_alloc_msi_vectors: "
1128ae115bc7Smrj "apic_vector_to_irq is not APIC_RESV_IRQ\n"));
1129ae115bc7Smrj #endif
1130ae115bc7Smrj apic_vector_to_irq[start + i] = (uchar_t)irqno;
1131ae115bc7Smrj
1132ae115bc7Smrj irqptr->airq_vector = (uchar_t)(start + i);
1133ae115bc7Smrj irqptr->airq_ioapicindex = (uchar_t)inum; /* start */
1134ae115bc7Smrj irqptr->airq_intin_no = (uchar_t)rcount;
1135ae115bc7Smrj irqptr->airq_ipl = pri;
1136ae115bc7Smrj irqptr->airq_vector = start + i;
1137ae115bc7Smrj irqptr->airq_origirq = (uchar_t)(inum + i);
1138ae115bc7Smrj irqptr->airq_share_id = 0;
1139ae115bc7Smrj irqptr->airq_mps_intr_index = MSI_INDEX;
1140ae115bc7Smrj irqptr->airq_dip = dip;
1141ae115bc7Smrj irqptr->airq_major = major;
1142ae115bc7Smrj if (i == 0) /* they all bound to the same cpu */
1143ae115bc7Smrj cpu = irqptr->airq_cpu = apic_bind_intr(dip, irqno,
1144ae115bc7Smrj 0xff, 0xff);
1145ae115bc7Smrj else
1146ae115bc7Smrj irqptr->airq_cpu = cpu;
1147a7639048Sjohnny DDI_INTR_IMPLDBG((CE_CONT, "apic_alloc_msi_vectors: irq=0x%x "
1148ae115bc7Smrj "dip=0x%p vector=0x%x origirq=0x%x pri=0x%x\n", irqno,
1149ae115bc7Smrj (void *)irqptr->airq_dip, irqptr->airq_vector,
1150ae115bc7Smrj irqptr->airq_origirq, pri));
1151ae115bc7Smrj }
1152ae115bc7Smrj mutex_exit(&airq_mutex);
1153ae115bc7Smrj return (rcount);
1154ae115bc7Smrj }
1155ae115bc7Smrj
1156ae115bc7Smrj /*
1157a7639048Sjohnny * This function allocates "count" MSI-X vector(s) for the given "dip/pri/type"
1158a7639048Sjohnny */
1159a7639048Sjohnny int
apic_alloc_msix_vectors(dev_info_t * dip,int inum,int count,int pri,int behavior)1160a7639048Sjohnny apic_alloc_msix_vectors(dev_info_t *dip, int inum, int count, int pri,
1161a7639048Sjohnny int behavior)
1162a7639048Sjohnny {
1163a7639048Sjohnny int rcount, i;
1164a7639048Sjohnny major_t major;
1165a7639048Sjohnny
1166a7639048Sjohnny mutex_enter(&airq_mutex);
1167a7639048Sjohnny
1168a7639048Sjohnny if ((rcount = apic_navail_vector(dip, pri)) > count)
1169a7639048Sjohnny rcount = count;
1170a7639048Sjohnny else if (rcount == 0 || (rcount < count &&
1171a7639048Sjohnny behavior == DDI_INTR_ALLOC_STRICT)) {
1172a7639048Sjohnny rcount = 0;
1173a7639048Sjohnny goto out;
1174a7639048Sjohnny }
1175a7639048Sjohnny
1176a7639048Sjohnny if (apic_check_free_irqs(rcount) == PSM_FAILURE) {
1177a7639048Sjohnny /* not enough free irq slots available */
1178a7639048Sjohnny rcount = 0;
1179a7639048Sjohnny goto out;
1180a7639048Sjohnny }
1181a7639048Sjohnny
11825c066ec2SJerry Gilliam major = (dip != NULL) ? ddi_driver_major(dip) : 0;
1183a7639048Sjohnny for (i = 0; i < rcount; i++) {
1184a7639048Sjohnny uchar_t vector, irqno;
1185a7639048Sjohnny apic_irq_t *irqptr;
1186a7639048Sjohnny
1187a7639048Sjohnny if ((irqno = apic_allocate_irq(apic_first_avail_irq)) ==
1188a7639048Sjohnny (uchar_t)-1) {
1189a7639048Sjohnny /*
1190a7639048Sjohnny * shouldn't happen because of the
1191a7639048Sjohnny * apic_check_free_irqs() check earlier
1192a7639048Sjohnny */
1193a7639048Sjohnny DDI_INTR_IMPLDBG((CE_CONT, "apic_alloc_msix_vectors: "
1194a7639048Sjohnny "apic_allocate_irq failed\n"));
1195a7639048Sjohnny rcount = i;
1196a7639048Sjohnny goto out;
1197a7639048Sjohnny }
1198a7639048Sjohnny if ((vector = apic_allocate_vector(pri, irqno, 1)) == 0) {
1199a7639048Sjohnny /*
1200a7639048Sjohnny * shouldn't happen because of the
1201a7639048Sjohnny * apic_navail_vector() call earlier
1202a7639048Sjohnny */
1203a7639048Sjohnny DDI_INTR_IMPLDBG((CE_CONT, "apic_alloc_msix_vectors: "
1204a7639048Sjohnny "apic_allocate_vector failed\n"));
1205a7639048Sjohnny rcount = i;
1206a7639048Sjohnny goto out;
1207a7639048Sjohnny }
1208a7639048Sjohnny apic_max_device_irq = max(irqno, apic_max_device_irq);
1209a7639048Sjohnny apic_min_device_irq = min(irqno, apic_min_device_irq);
1210a7639048Sjohnny irqptr = apic_irq_table[irqno];
1211a7639048Sjohnny irqptr->airq_vector = (uchar_t)vector;
1212a7639048Sjohnny irqptr->airq_ipl = pri;
1213a7639048Sjohnny irqptr->airq_origirq = (uchar_t)(inum + i);
1214a7639048Sjohnny irqptr->airq_share_id = 0;
1215a7639048Sjohnny irqptr->airq_mps_intr_index = MSIX_INDEX;
1216a7639048Sjohnny irqptr->airq_dip = dip;
1217a7639048Sjohnny irqptr->airq_major = major;
1218a7639048Sjohnny irqptr->airq_cpu = apic_bind_intr(dip, irqno, 0xff, 0xff);
1219a7639048Sjohnny }
1220a7639048Sjohnny out:
1221a7639048Sjohnny mutex_exit(&airq_mutex);
1222a7639048Sjohnny return (rcount);
1223a7639048Sjohnny }
1224a7639048Sjohnny
1225a7639048Sjohnny /*
1226ae115bc7Smrj * Allocate a free vector for irq at ipl. Takes care of merging of multiple
1227ae115bc7Smrj * IPLs into a single APIC level as well as stretching some IPLs onto multiple
1228ae115bc7Smrj * levels. APIC_HI_PRI_VECTS interrupts are reserved for high priority
1229ae115bc7Smrj * requests and allocated only when pri is set.
1230ae115bc7Smrj */
1231ae115bc7Smrj uchar_t
apic_allocate_vector(int ipl,int irq,int pri)1232ae115bc7Smrj apic_allocate_vector(int ipl, int irq, int pri)
1233ae115bc7Smrj {
1234ae115bc7Smrj int lowest, highest, i;
1235ae115bc7Smrj
1236ae115bc7Smrj highest = apic_ipltopri[ipl] + APIC_VECTOR_MASK;
1237ae115bc7Smrj lowest = apic_ipltopri[ipl - 1] + APIC_VECTOR_PER_IPL;
1238ae115bc7Smrj
1239ae115bc7Smrj if (highest < lowest) /* Both ipl and ipl - 1 map to same pri */
1240ae115bc7Smrj lowest -= APIC_VECTOR_PER_IPL;
1241ae115bc7Smrj
1242ae115bc7Smrj #ifdef DEBUG
1243ae115bc7Smrj if (apic_restrict_vector) /* for testing shared interrupt logic */
1244ae115bc7Smrj highest = lowest + apic_restrict_vector + APIC_HI_PRI_VECTS;
1245ae115bc7Smrj #endif /* DEBUG */
1246ae115bc7Smrj if (pri == 0)
1247ae115bc7Smrj highest -= APIC_HI_PRI_VECTS;
1248ae115bc7Smrj
124926896e4cSGuoli Shu for (i = lowest; i <= highest; i++) {
1250ae115bc7Smrj if (APIC_CHECK_RESERVE_VECTORS(i))
1251ae115bc7Smrj continue;
1252ae115bc7Smrj if (apic_vector_to_irq[i] == APIC_RESV_IRQ) {
1253ae115bc7Smrj apic_vector_to_irq[i] = (uchar_t)irq;
1254ae115bc7Smrj return (i);
1255ae115bc7Smrj }
1256ae115bc7Smrj }
1257ae115bc7Smrj
1258ae115bc7Smrj return (0);
1259ae115bc7Smrj }
1260ae115bc7Smrj
1261ae115bc7Smrj /* Mark vector as not being used by any irq */
1262ae115bc7Smrj void
apic_free_vector(uchar_t vector)1263ae115bc7Smrj apic_free_vector(uchar_t vector)
1264ae115bc7Smrj {
1265ae115bc7Smrj apic_vector_to_irq[vector] = APIC_RESV_IRQ;
1266ae115bc7Smrj }
1267ae115bc7Smrj
1268e7c3cdaeSjiang.liu@intel.com /*
1269ae115bc7Smrj * Call rebind to do the actual programming.
1270ae115bc7Smrj * Must be called with interrupts disabled and apic_ioapic_lock held
1271ae115bc7Smrj * 'p' is polymorphic -- if this function is called to process a deferred
1272ae115bc7Smrj * reprogramming, p is of type 'struct ioapic_reprogram_data *', from which
1273ae115bc7Smrj * the irq pointer is retrieved. If not doing deferred reprogramming,
1274ae115bc7Smrj * p is of the type 'apic_irq_t *'.
1275ae115bc7Smrj *
1276ae115bc7Smrj * apic_ioapic_lock must be held across this call, as it protects apic_rebind
12777ff178cdSJimmy Vetayases * and it protects apic_get_next_bind_cpu() from a race in which a CPU can be
1278a3114836SGerry Liu * taken offline after a cpu is selected, but before apic_rebind is called to
1279ae115bc7Smrj * bind interrupts to it.
1280e250f1e2Ssethg */
1281ae115bc7Smrj int
apic_setup_io_intr(void * p,int irq,boolean_t deferred)1282ae115bc7Smrj apic_setup_io_intr(void *p, int irq, boolean_t deferred)
12837c478bd9Sstevel@tonic-gate {
1284ae115bc7Smrj apic_irq_t *irqptr;
1285ae115bc7Smrj struct ioapic_reprogram_data *drep = NULL;
1286ae115bc7Smrj int rv;
12877c478bd9Sstevel@tonic-gate
1288ae115bc7Smrj if (deferred) {
1289ae115bc7Smrj drep = (struct ioapic_reprogram_data *)p;
1290ae115bc7Smrj ASSERT(drep != NULL);
1291ae115bc7Smrj irqptr = drep->irqp;
1292ae115bc7Smrj } else
1293ae115bc7Smrj irqptr = (apic_irq_t *)p;
1294e250f1e2Ssethg
1295ae115bc7Smrj ASSERT(irqptr != NULL);
12967c478bd9Sstevel@tonic-gate
1297ae115bc7Smrj rv = apic_rebind(irqptr, apic_irq_table[irq]->airq_cpu, drep);
1298ae115bc7Smrj if (rv) {
12997c478bd9Sstevel@tonic-gate /*
1300ae115bc7Smrj * CPU is not up or interrupts are disabled. Fall back to
1301ae115bc7Smrj * the first available CPU
13027c478bd9Sstevel@tonic-gate */
1303e7c3cdaeSjiang.liu@intel.com rv = apic_rebind(irqptr, apic_find_cpu(APIC_CPU_INTR_ENABLE),
1304e7c3cdaeSjiang.liu@intel.com drep);
13057c478bd9Sstevel@tonic-gate }
13067c478bd9Sstevel@tonic-gate
1307ae115bc7Smrj return (rv);
13087c478bd9Sstevel@tonic-gate }
13097c478bd9Sstevel@tonic-gate
13107c478bd9Sstevel@tonic-gate
1311ae115bc7Smrj uchar_t
apic_modify_vector(uchar_t vector,int irq)1312ae115bc7Smrj apic_modify_vector(uchar_t vector, int irq)
1313ae115bc7Smrj {
1314ae115bc7Smrj apic_vector_to_irq[vector] = (uchar_t)irq;
1315ae115bc7Smrj return (vector);
13167c478bd9Sstevel@tonic-gate }
13172917a9c9Sschwartz
13182917a9c9Sschwartz char *
apic_get_apic_type(void)13197ff178cdSJimmy Vetayases apic_get_apic_type(void)
13202917a9c9Sschwartz {
13212917a9c9Sschwartz return (apic_psm_info.p_mach_idstring);
13222917a9c9Sschwartz }
1323b6917abeSmishra
1324b6917abeSmishra void
x2apic_update_psm(void)13257ff178cdSJimmy Vetayases x2apic_update_psm(void)
1326b6917abeSmishra {
1327b6917abeSmishra struct psm_ops *pops = &apic_ops;
1328b6917abeSmishra
1329b6917abeSmishra ASSERT(pops != NULL);
1330b6917abeSmishra
1331b6917abeSmishra pops->psm_intr_exit = x2apic_intr_exit;
1332b6917abeSmishra pops->psm_setspl = x2apic_setspl;
13337ff178cdSJimmy Vetayases
1334a3114836SGerry Liu pops->psm_send_ipi = x2apic_send_ipi;
1335b6917abeSmishra send_dirintf = pops->psm_send_ipi;
1336325e77f4SSaurabh Misra
1337325e77f4SSaurabh Misra apic_mode = LOCAL_X2APIC;
1338325e77f4SSaurabh Misra apic_change_ops();
1339b6917abeSmishra }
1340