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 /*
30*d828baa4SHans Rosenfeld * Copyright (c) 2017, 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);
251*d828baa4SHans Rosenfeld
252*d828baa4SHans Rosenfeld /*
253*d828baa4SHans Rosenfeld * Check whether x2APIC mode was activated by BIOS. We don't support
254*d828baa4SHans Rosenfeld * that in pcplusmp as apix normally handles that.
255*d828baa4SHans Rosenfeld */
256*d828baa4SHans Rosenfeld if (apic_local_mode() == LOCAL_X2APIC)
257*d828baa4SHans Rosenfeld return (PSM_FAILURE);
258*d828baa4SHans Rosenfeld
259*d828baa4SHans Rosenfeld /* continue using pcplusmp PSM */
260*d828baa4SHans Rosenfeld apix_enable = 0;
2617ff178cdSJimmy Vetayases
262ae115bc7Smrj return (apic_probe_common(apic_psm_info.p_mach_idstring));
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate
2657ff178cdSJimmy Vetayases static uchar_t
apic_xlate_vector_by_irq(uchar_t irq)2667ff178cdSJimmy Vetayases apic_xlate_vector_by_irq(uchar_t irq)
2677ff178cdSJimmy Vetayases {
2687ff178cdSJimmy Vetayases if (apic_irq_table[irq] == NULL)
2697ff178cdSJimmy Vetayases return (0);
2707ff178cdSJimmy Vetayases
2717ff178cdSJimmy Vetayases return (apic_irq_table[irq]->airq_vector);
2727ff178cdSJimmy Vetayases }
2737ff178cdSJimmy Vetayases
2747c478bd9Sstevel@tonic-gate void
apic_init(void)2757ff178cdSJimmy Vetayases apic_init(void)
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate int i;
2787c478bd9Sstevel@tonic-gate int j = 1;
279ae115bc7Smrj
2807ff178cdSJimmy Vetayases psm_get_ioapicid = apic_get_ioapicid;
2817ff178cdSJimmy Vetayases psm_get_localapicid = apic_get_localapicid;
2827ff178cdSJimmy Vetayases psm_xlate_vector_by_irq = apic_xlate_vector_by_irq;
2837ff178cdSJimmy Vetayases
2847c478bd9Sstevel@tonic-gate apic_ipltopri[0] = APIC_VECTOR_PER_IPL; /* leave 0 for idle */
2857c478bd9Sstevel@tonic-gate for (i = 0; i < (APIC_AVAIL_VECTOR / APIC_VECTOR_PER_IPL); i++) {
2867c478bd9Sstevel@tonic-gate if ((i < ((APIC_AVAIL_VECTOR / APIC_VECTOR_PER_IPL) - 1)) &&
2877c478bd9Sstevel@tonic-gate (apic_vectortoipl[i + 1] == apic_vectortoipl[i]))
2887c478bd9Sstevel@tonic-gate /* get to highest vector at the same ipl */
2897c478bd9Sstevel@tonic-gate continue;
2907c478bd9Sstevel@tonic-gate for (; j <= apic_vectortoipl[i]; j++) {
2917c478bd9Sstevel@tonic-gate apic_ipltopri[j] = (i << APIC_IPL_SHIFT) +
2927c478bd9Sstevel@tonic-gate APIC_BASE_VECT;
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate for (; j < MAXIPL + 1; j++)
2967c478bd9Sstevel@tonic-gate /* fill up any empty ipltopri slots */
2977c478bd9Sstevel@tonic-gate apic_ipltopri[j] = (i << APIC_IPL_SHIFT) + APIC_BASE_VECT;
298ae115bc7Smrj apic_init_common();
29932842aabSJosef 'Jeff' Sipek
30032842aabSJosef 'Jeff' Sipek #if !defined(__amd64)
3012ef50f01SJoe Bonasera if (cpuid_have_cr8access(CPU))
3022ef50f01SJoe Bonasera apic_have_32bit_cr8 = 1;
30332842aabSJosef 'Jeff' Sipek #endif
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate
3067c478bd9Sstevel@tonic-gate static void
apic_init_intr(void)3077ff178cdSJimmy Vetayases apic_init_intr(void)
3087c478bd9Sstevel@tonic-gate {
3097c478bd9Sstevel@tonic-gate processorid_t cpun = psm_get_cpu_id();
310da2743adSdmick uint_t nlvt;
311b6917abeSmishra uint32_t svr = AV_UNIT_ENABLE | APIC_SPUR_INTR;
3127c478bd9Sstevel@tonic-gate
313b6917abeSmishra apic_reg_ops->apic_write_task_reg(APIC_MASK_ALL);
314b6917abeSmishra
315b6917abeSmishra if (apic_mode == LOCAL_APIC) {
316b6917abeSmishra /*
317b6917abeSmishra * We are running APIC in MMIO mode.
318b6917abeSmishra */
319b6917abeSmishra if (apic_flat_model) {
320b6917abeSmishra apic_reg_ops->apic_write(APIC_FORMAT_REG,
321b6917abeSmishra APIC_FLAT_MODEL);
322b6917abeSmishra } else {
323b6917abeSmishra apic_reg_ops->apic_write(APIC_FORMAT_REG,
324b6917abeSmishra APIC_CLUSTER_MODEL);
325b6917abeSmishra }
326b6917abeSmishra
327b6917abeSmishra apic_reg_ops->apic_write(APIC_DEST_REG,
328b6917abeSmishra AV_HIGH_ORDER >> cpun);
329b6917abeSmishra }
330b6917abeSmishra
331e511d54dSSaurabh Misra if (apic_directed_EOI_supported()) {
332b6917abeSmishra /*
333e511d54dSSaurabh Misra * Setting the 12th bit in the Spurious Interrupt Vector
334e511d54dSSaurabh Misra * Register suppresses broadcast EOIs generated by the local
335e511d54dSSaurabh Misra * APIC. The suppression of broadcast EOIs happens only when
336e511d54dSSaurabh Misra * interrupts are level-triggered.
337b6917abeSmishra */
338e511d54dSSaurabh Misra svr |= APIC_SVR_SUPPRESS_BROADCAST_EOI;
339b6917abeSmishra }
3407c478bd9Sstevel@tonic-gate
3417c478bd9Sstevel@tonic-gate /* need to enable APIC before unmasking NMI */
342b6917abeSmishra apic_reg_ops->apic_write(APIC_SPUR_INT_REG, svr);
3437c478bd9Sstevel@tonic-gate
344da2743adSdmick /*
345da2743adSdmick * Presence of an invalid vector with delivery mode AV_FIXED can
346da2743adSdmick * cause an error interrupt, even if the entry is masked...so
347da2743adSdmick * write a valid vector to LVT entries along with the mask bit
348da2743adSdmick */
349da2743adSdmick
350da2743adSdmick /* All APICs have timer and LINT0/1 */
351b6917abeSmishra apic_reg_ops->apic_write(APIC_LOCAL_TIMER, AV_MASK|APIC_RESV_IRQ);
352b6917abeSmishra apic_reg_ops->apic_write(APIC_INT_VECT0, AV_MASK|APIC_RESV_IRQ);
353b6917abeSmishra apic_reg_ops->apic_write(APIC_INT_VECT1, AV_NMI); /* enable NMI */
3547c478bd9Sstevel@tonic-gate
355da2743adSdmick /*
356da2743adSdmick * On integrated APICs, the number of LVT entries is
357da2743adSdmick * 'Max LVT entry' + 1; on 82489DX's (non-integrated
358da2743adSdmick * APICs), nlvt is "3" (LINT0, LINT1, and timer)
359da2743adSdmick */
3607c478bd9Sstevel@tonic-gate
361da2743adSdmick if (apic_cpus[cpun].aci_local_ver < APIC_INTEGRATED_VERS) {
362da2743adSdmick nlvt = 3;
363da2743adSdmick } else {
364a898d95bSSaurabh Misra nlvt = ((apic_reg_ops->apic_read(APIC_VERS_REG) >> 16) &
365a898d95bSSaurabh Misra 0xFF) + 1;
366da2743adSdmick }
367da2743adSdmick
368da2743adSdmick if (nlvt >= 5) {
3697c478bd9Sstevel@tonic-gate /* Enable performance counter overflow interrupt */
3707c478bd9Sstevel@tonic-gate
3717417cfdeSKuriakose Kuruvilla if (!is_x86_feature(x86_featureset, X86FSET_MSR))
3727c478bd9Sstevel@tonic-gate apic_enable_cpcovf_intr = 0;
3737c478bd9Sstevel@tonic-gate if (apic_enable_cpcovf_intr) {
3747c478bd9Sstevel@tonic-gate if (apic_cpcovf_vect == 0) {
3757c478bd9Sstevel@tonic-gate int ipl = APIC_PCINT_IPL;
3767c478bd9Sstevel@tonic-gate int irq = apic_get_ipivect(ipl, -1);
3777c478bd9Sstevel@tonic-gate
3787c478bd9Sstevel@tonic-gate ASSERT(irq != -1);
379da2743adSdmick apic_cpcovf_vect =
380da2743adSdmick apic_irq_table[irq]->airq_vector;
3817c478bd9Sstevel@tonic-gate ASSERT(apic_cpcovf_vect);
3827c478bd9Sstevel@tonic-gate (void) add_avintr(NULL, ipl,
3837c478bd9Sstevel@tonic-gate (avfunc)kcpc_hw_overflow_intr,
3847a364d25Sschwartz "apic pcint", irq, NULL, NULL, NULL, NULL);
3857c478bd9Sstevel@tonic-gate kcpc_hw_overflow_intr_installed = 1;
386da2743adSdmick kcpc_hw_enable_cpc_intr =
387da2743adSdmick apic_cpcovf_mask_clear;
3887c478bd9Sstevel@tonic-gate }
389b6917abeSmishra apic_reg_ops->apic_write(APIC_PCINT_VECT,
390b6917abeSmishra apic_cpcovf_vect);
3917c478bd9Sstevel@tonic-gate }
392da2743adSdmick }
393da2743adSdmick
394da2743adSdmick if (nlvt >= 6) {
395da2743adSdmick /* Only mask TM intr if the BIOS apparently doesn't use it */
396da2743adSdmick
397da2743adSdmick uint32_t lvtval;
398da2743adSdmick
399b6917abeSmishra lvtval = apic_reg_ops->apic_read(APIC_THERM_VECT);
400da2743adSdmick if (((lvtval & AV_MASK) == AV_MASK) ||
401da2743adSdmick ((lvtval & AV_DELIV_MODE) != AV_SMI)) {
402b6917abeSmishra apic_reg_ops->apic_write(APIC_THERM_VECT,
403b6917abeSmishra AV_MASK|APIC_RESV_IRQ);
404da2743adSdmick }
405da2743adSdmick }
4067c478bd9Sstevel@tonic-gate
4077c478bd9Sstevel@tonic-gate /* Enable error interrupt */
4087c478bd9Sstevel@tonic-gate
409da2743adSdmick if (nlvt >= 4 && apic_enable_error_intr) {
4107c478bd9Sstevel@tonic-gate if (apic_errvect == 0) {
4117c478bd9Sstevel@tonic-gate int ipl = 0xf; /* get highest priority intr */
4127c478bd9Sstevel@tonic-gate int irq = apic_get_ipivect(ipl, -1);
4137c478bd9Sstevel@tonic-gate
4147c478bd9Sstevel@tonic-gate ASSERT(irq != -1);
4157c478bd9Sstevel@tonic-gate apic_errvect = apic_irq_table[irq]->airq_vector;
4167c478bd9Sstevel@tonic-gate ASSERT(apic_errvect);
4177c478bd9Sstevel@tonic-gate /*
4187c478bd9Sstevel@tonic-gate * Not PSMI compliant, but we are going to merge
4197c478bd9Sstevel@tonic-gate * with ON anyway
4207c478bd9Sstevel@tonic-gate */
4217c478bd9Sstevel@tonic-gate (void) add_avintr((void *)NULL, ipl,
4227c478bd9Sstevel@tonic-gate (avfunc)apic_error_intr, "apic error intr",
4237a364d25Sschwartz irq, NULL, NULL, NULL, NULL);
4247c478bd9Sstevel@tonic-gate }
425b6917abeSmishra apic_reg_ops->apic_write(APIC_ERR_VECT, apic_errvect);
426b6917abeSmishra apic_reg_ops->apic_write(APIC_ERROR_STATUS, 0);
427b6917abeSmishra apic_reg_ops->apic_write(APIC_ERROR_STATUS, 0);
4287c478bd9Sstevel@tonic-gate }
429da2743adSdmick
430e3d60c9bSAdrian Frost /* Enable CMCI interrupt */
431e3d60c9bSAdrian Frost if (cmi_enable_cmci) {
432e3d60c9bSAdrian Frost
433e3d60c9bSAdrian Frost mutex_enter(&cmci_cpu_setup_lock);
434e3d60c9bSAdrian Frost if (cmci_cpu_setup_registered == 0) {
435e3d60c9bSAdrian Frost mutex_enter(&cpu_lock);
436e3d60c9bSAdrian Frost register_cpu_setup_func(cmci_cpu_setup, NULL);
437e3d60c9bSAdrian Frost mutex_exit(&cpu_lock);
438e3d60c9bSAdrian Frost cmci_cpu_setup_registered = 1;
439e3d60c9bSAdrian Frost }
440e3d60c9bSAdrian Frost mutex_exit(&cmci_cpu_setup_lock);
441e3d60c9bSAdrian Frost
442e3d60c9bSAdrian Frost if (apic_cmci_vect == 0) {
443e3d60c9bSAdrian Frost int ipl = 0x2;
444e3d60c9bSAdrian Frost int irq = apic_get_ipivect(ipl, -1);
445e3d60c9bSAdrian Frost
446e3d60c9bSAdrian Frost ASSERT(irq != -1);
447e3d60c9bSAdrian Frost apic_cmci_vect = apic_irq_table[irq]->airq_vector;
448e3d60c9bSAdrian Frost ASSERT(apic_cmci_vect);
449e3d60c9bSAdrian Frost
450e3d60c9bSAdrian Frost (void) add_avintr(NULL, ipl,
451e3d60c9bSAdrian Frost (avfunc)cmi_cmci_trap,
452e3d60c9bSAdrian Frost "apic cmci intr", irq, NULL, NULL, NULL, NULL);
453e3d60c9bSAdrian Frost }
454e3d60c9bSAdrian Frost apic_reg_ops->apic_write(APIC_CMCI_VECT, apic_cmci_vect);
455e3d60c9bSAdrian Frost }
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate
4587c478bd9Sstevel@tonic-gate static void
apic_picinit(void)4597c478bd9Sstevel@tonic-gate apic_picinit(void)
4607c478bd9Sstevel@tonic-gate {
461ae115bc7Smrj int i, j;
4627c478bd9Sstevel@tonic-gate uint_t isr;
4637c478bd9Sstevel@tonic-gate
4647c478bd9Sstevel@tonic-gate /*
465f7a1836aSSaurabh Misra * Initialize and enable interrupt remapping before apic
4661de082f7SVikram Hegde * hardware initialization
4671de082f7SVikram Hegde */
4683a634bfcSVikram Hegde apic_intrmap_init(apic_mode);
4691de082f7SVikram Hegde
4701de082f7SVikram Hegde /*
4717c478bd9Sstevel@tonic-gate * On UniSys Model 6520, the BIOS leaves vector 0x20 isr
4727c478bd9Sstevel@tonic-gate * bit on without clearing it with EOI. Since softint
4737c478bd9Sstevel@tonic-gate * uses vector 0x20 to interrupt itself, so softint will
4747c478bd9Sstevel@tonic-gate * not work on this machine. In order to fix this problem
4757c478bd9Sstevel@tonic-gate * a check is made to verify all the isr bits are clear.
4767c478bd9Sstevel@tonic-gate * If not, EOIs are issued to clear the bits.
4777c478bd9Sstevel@tonic-gate */
4787c478bd9Sstevel@tonic-gate for (i = 7; i >= 1; i--) {
479b6917abeSmishra isr = apic_reg_ops->apic_read(APIC_ISR_REG + (i * 4));
480b6917abeSmishra if (isr != 0)
4817c478bd9Sstevel@tonic-gate for (j = 0; ((j < 32) && (isr != 0)); j++)
4827c478bd9Sstevel@tonic-gate if (isr & (1 << j)) {
483b6917abeSmishra apic_reg_ops->apic_write(
484b6917abeSmishra APIC_EOI_REG, 0);
4857c478bd9Sstevel@tonic-gate isr &= ~(1 << j);
4867c478bd9Sstevel@tonic-gate apic_error |= APIC_ERR_BOOT_EOI;
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate
4907c478bd9Sstevel@tonic-gate /* set a flag so we know we have run apic_picinit() */
491843e1988Sjohnlev apic_picinit_called = 1;
4927c478bd9Sstevel@tonic-gate LOCK_INIT_CLEAR(&apic_gethrtime_lock);
4937c478bd9Sstevel@tonic-gate LOCK_INIT_CLEAR(&apic_ioapic_lock);
4947c478bd9Sstevel@tonic-gate LOCK_INIT_CLEAR(&apic_error_lock);
495a3114836SGerry Liu LOCK_INIT_CLEAR(&apic_mode_switch_lock);
4967c478bd9Sstevel@tonic-gate
4977c478bd9Sstevel@tonic-gate picsetup(); /* initialise the 8259 */
4987c478bd9Sstevel@tonic-gate
4997c478bd9Sstevel@tonic-gate /* add nmi handler - least priority nmi handler */
5007c478bd9Sstevel@tonic-gate LOCK_INIT_CLEAR(&apic_nmi_lock);
5017c478bd9Sstevel@tonic-gate
5027c478bd9Sstevel@tonic-gate if (!psm_add_nmintr(0, (avfunc) apic_nmi_intr,
5037c478bd9Sstevel@tonic-gate "pcplusmp NMI handler", (caddr_t)NULL))
5047c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "pcplusmp: Unable to add nmi handler");
5057c478bd9Sstevel@tonic-gate
506f7a1836aSSaurabh Misra /*
507f7a1836aSSaurabh Misra * Check for directed-EOI capability in the local APIC.
508f7a1836aSSaurabh Misra */
509f7a1836aSSaurabh Misra if (apic_directed_EOI_supported() == 1) {
510f7a1836aSSaurabh Misra apic_set_directed_EOI_handler();
511f7a1836aSSaurabh Misra }
512f7a1836aSSaurabh Misra
5137c478bd9Sstevel@tonic-gate apic_init_intr();
5147c478bd9Sstevel@tonic-gate
5157c478bd9Sstevel@tonic-gate /* enable apic mode if imcr present */
5167c478bd9Sstevel@tonic-gate if (apic_imcrp) {
5177c478bd9Sstevel@tonic-gate outb(APIC_IMCR_P1, (uchar_t)APIC_IMCR_SELECT);
5187c478bd9Sstevel@tonic-gate outb(APIC_IMCR_P2, (uchar_t)APIC_IMCR_APIC);
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate
521ae115bc7Smrj ioapic_init_intr(IOAPIC_MASK);
5227c478bd9Sstevel@tonic-gate }
5237c478bd9Sstevel@tonic-gate
5245d8efbbcSSaurabh Misra #ifdef DEBUG
5257c478bd9Sstevel@tonic-gate void
apic_break(void)5267ff178cdSJimmy Vetayases apic_break(void)
5277c478bd9Sstevel@tonic-gate {
5287c478bd9Sstevel@tonic-gate }
5297c478bd9Sstevel@tonic-gate #endif /* DEBUG */
5307c478bd9Sstevel@tonic-gate
5317c478bd9Sstevel@tonic-gate /*
5327c478bd9Sstevel@tonic-gate * platform_intr_enter
5337c478bd9Sstevel@tonic-gate *
5347c478bd9Sstevel@tonic-gate * Called at the beginning of the interrupt service routine to
5357c478bd9Sstevel@tonic-gate * mask all level equal to and below the interrupt priority
5367c478bd9Sstevel@tonic-gate * of the interrupting vector. An EOI should be given to
5377c478bd9Sstevel@tonic-gate * the interrupt controller to enable other HW interrupts.
5387c478bd9Sstevel@tonic-gate *
5397c478bd9Sstevel@tonic-gate * Return -1 for spurious interrupts
5407c478bd9Sstevel@tonic-gate *
5417c478bd9Sstevel@tonic-gate */
5427c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5437c478bd9Sstevel@tonic-gate static int
apic_intr_enter(int ipl,int * vectorp)5447c478bd9Sstevel@tonic-gate apic_intr_enter(int ipl, int *vectorp)
5457c478bd9Sstevel@tonic-gate {
5467c478bd9Sstevel@tonic-gate uchar_t vector;
5477c478bd9Sstevel@tonic-gate int nipl;
548ae115bc7Smrj int irq;
549ae115bc7Smrj ulong_t iflag;
5507c478bd9Sstevel@tonic-gate apic_cpus_info_t *cpu_infop;
5517c478bd9Sstevel@tonic-gate
5527c478bd9Sstevel@tonic-gate /*
553c8589f13Ssethg * The real vector delivered is (*vectorp + 0x20), but our caller
554c8589f13Ssethg * subtracts 0x20 from the vector before passing it to us.
555c8589f13Ssethg * (That's why APIC_BASE_VECT is 0x20.)
5567c478bd9Sstevel@tonic-gate */
5577c478bd9Sstevel@tonic-gate vector = (uchar_t)*vectorp;
5587c478bd9Sstevel@tonic-gate
5597c478bd9Sstevel@tonic-gate /* if interrupted by the clock, increment apic_nsec_since_boot */
5607c478bd9Sstevel@tonic-gate if (vector == apic_clkvect) {
5617c478bd9Sstevel@tonic-gate if (!apic_oneshot) {
5627c478bd9Sstevel@tonic-gate /* NOTE: this is not MT aware */
5637c478bd9Sstevel@tonic-gate apic_hrtime_stamp++;
5647c478bd9Sstevel@tonic-gate apic_nsec_since_boot += apic_nsec_per_intr;
5657c478bd9Sstevel@tonic-gate apic_hrtime_stamp++;
5667c478bd9Sstevel@tonic-gate last_count_read = apic_hertz_count;
5677c478bd9Sstevel@tonic-gate apic_redistribute_compute();
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate
5707c478bd9Sstevel@tonic-gate /* We will avoid all the book keeping overhead for clock */
571c8589f13Ssethg nipl = apic_ipls[vector];
572c8589f13Ssethg
5737c478bd9Sstevel@tonic-gate *vectorp = apic_vector_to_irq[vector + APIC_BASE_VECT];
57429ddbbefSJosef 'Jeff' Sipek
57529ddbbefSJosef 'Jeff' Sipek apic_reg_ops->apic_write_task_reg(apic_ipltopri[nipl]);
57629ddbbefSJosef 'Jeff' Sipek apic_reg_ops->apic_send_eoi(0);
577b6917abeSmishra
5787c478bd9Sstevel@tonic-gate return (nipl);
5797c478bd9Sstevel@tonic-gate }
5807c478bd9Sstevel@tonic-gate
5817c478bd9Sstevel@tonic-gate cpu_infop = &apic_cpus[psm_get_cpu_id()];
5827c478bd9Sstevel@tonic-gate
5837c478bd9Sstevel@tonic-gate if (vector == (APIC_SPUR_INTR - APIC_BASE_VECT)) {
5847c478bd9Sstevel@tonic-gate cpu_infop->aci_spur_cnt++;
5857c478bd9Sstevel@tonic-gate return (APIC_INT_SPURIOUS);
5867c478bd9Sstevel@tonic-gate }
5877c478bd9Sstevel@tonic-gate
5887c478bd9Sstevel@tonic-gate /* Check if the vector we got is really what we need */
5897c478bd9Sstevel@tonic-gate if (apic_revector_pending) {
5907c478bd9Sstevel@tonic-gate /*
5917c478bd9Sstevel@tonic-gate * Disable interrupts for the duration of
5927c478bd9Sstevel@tonic-gate * the vector translation to prevent a self-race for
5937c478bd9Sstevel@tonic-gate * the apic_revector_lock. This cannot be done
5947c478bd9Sstevel@tonic-gate * in apic_xlate_vector because it is recursive and
5957c478bd9Sstevel@tonic-gate * we want the vector translation to be atomic with
5967c478bd9Sstevel@tonic-gate * respect to other (higher-priority) interrupts.
5977c478bd9Sstevel@tonic-gate */
5987c478bd9Sstevel@tonic-gate iflag = intr_clear();
5997c478bd9Sstevel@tonic-gate vector = apic_xlate_vector(vector + APIC_BASE_VECT) -
6007c478bd9Sstevel@tonic-gate APIC_BASE_VECT;
6017c478bd9Sstevel@tonic-gate intr_restore(iflag);
6027c478bd9Sstevel@tonic-gate }
6037c478bd9Sstevel@tonic-gate
604c8589f13Ssethg nipl = apic_ipls[vector];
6057c478bd9Sstevel@tonic-gate *vectorp = irq = apic_vector_to_irq[vector + APIC_BASE_VECT];
6067c478bd9Sstevel@tonic-gate
60729ddbbefSJosef 'Jeff' Sipek apic_reg_ops->apic_write_task_reg(apic_ipltopri[nipl]);
6087c478bd9Sstevel@tonic-gate
6097c478bd9Sstevel@tonic-gate cpu_infop->aci_current[nipl] = (uchar_t)irq;
6107c478bd9Sstevel@tonic-gate cpu_infop->aci_curipl = (uchar_t)nipl;
6117c478bd9Sstevel@tonic-gate cpu_infop->aci_ISR_in_progress |= 1 << nipl;
6127c478bd9Sstevel@tonic-gate
6137c478bd9Sstevel@tonic-gate /*
6147c478bd9Sstevel@tonic-gate * apic_level_intr could have been assimilated into the irq struct.
6157c478bd9Sstevel@tonic-gate * but, having it as a character array is more efficient in terms of
6167c478bd9Sstevel@tonic-gate * cache usage. So, we leave it as is.
6177c478bd9Sstevel@tonic-gate */
618b6917abeSmishra if (!apic_level_intr[irq]) {
61929ddbbefSJosef 'Jeff' Sipek apic_reg_ops->apic_send_eoi(0);
62087cc6269SSaurabh Misra }
6217c478bd9Sstevel@tonic-gate
6227c478bd9Sstevel@tonic-gate #ifdef DEBUG
6237c478bd9Sstevel@tonic-gate APIC_DEBUG_BUF_PUT(vector);
6247c478bd9Sstevel@tonic-gate APIC_DEBUG_BUF_PUT(irq);
6257c478bd9Sstevel@tonic-gate APIC_DEBUG_BUF_PUT(nipl);
6267c478bd9Sstevel@tonic-gate APIC_DEBUG_BUF_PUT(psm_get_cpu_id());
6277c478bd9Sstevel@tonic-gate if ((apic_stretch_interrupts) && (apic_stretch_ISR & (1 << nipl)))
6287c478bd9Sstevel@tonic-gate drv_usecwait(apic_stretch_interrupts);
6297c478bd9Sstevel@tonic-gate
6307c478bd9Sstevel@tonic-gate if (apic_break_on_cpu == psm_get_cpu_id())
6317c478bd9Sstevel@tonic-gate apic_break();
6327c478bd9Sstevel@tonic-gate #endif /* DEBUG */
6337c478bd9Sstevel@tonic-gate return (nipl);
6347c478bd9Sstevel@tonic-gate }
6357c478bd9Sstevel@tonic-gate
636b6917abeSmishra /*
637325e77f4SSaurabh Misra * This macro is a common code used by MMIO local apic and X2APIC
638b6917abeSmishra * local apic.
639b6917abeSmishra */
640b6917abeSmishra #define APIC_INTR_EXIT() \
641b6917abeSmishra { \
642b6917abeSmishra cpu_infop = &apic_cpus[psm_get_cpu_id()]; \
643b6917abeSmishra if (apic_level_intr[irq]) \
644b6917abeSmishra apic_reg_ops->apic_send_eoi(irq); \
645b6917abeSmishra cpu_infop->aci_curipl = (uchar_t)prev_ipl; \
646b6917abeSmishra /* ISR above current pri could not be in progress */ \
647b6917abeSmishra cpu_infop->aci_ISR_in_progress &= (2 << prev_ipl) - 1; \
648b6917abeSmishra }
649b6917abeSmishra
650b6917abeSmishra /*
651325e77f4SSaurabh Misra * Any changes made to this function must also change X2APIC
652b6917abeSmishra * version of intr_exit.
653b6917abeSmishra */
654ae115bc7Smrj void
apic_intr_exit(int prev_ipl,int irq)6557c478bd9Sstevel@tonic-gate apic_intr_exit(int prev_ipl, int irq)
6567c478bd9Sstevel@tonic-gate {
6577c478bd9Sstevel@tonic-gate apic_cpus_info_t *cpu_infop;
6587c478bd9Sstevel@tonic-gate
65929ddbbefSJosef 'Jeff' Sipek apic_reg_ops->apic_write_task_reg(apic_ipltopri[prev_ipl]);
6607c478bd9Sstevel@tonic-gate
661b6917abeSmishra APIC_INTR_EXIT();
662b6917abeSmishra }
6637c478bd9Sstevel@tonic-gate
664b6917abeSmishra /*
665b6917abeSmishra * Same as apic_intr_exit() except it uses MSR rather than MMIO
666b6917abeSmishra * to access local apic registers.
667b6917abeSmishra */
668b6917abeSmishra void
x2apic_intr_exit(int prev_ipl,int irq)669b6917abeSmishra x2apic_intr_exit(int prev_ipl, int irq)
670b6917abeSmishra {
671b6917abeSmishra apic_cpus_info_t *cpu_infop;
672b6917abeSmishra
673b6917abeSmishra X2APIC_WRITE(APIC_TASK_REG, apic_ipltopri[prev_ipl]);
674b6917abeSmishra APIC_INTR_EXIT();
6757c478bd9Sstevel@tonic-gate }
6767c478bd9Sstevel@tonic-gate
677843e1988Sjohnlev intr_exit_fn_t
psm_intr_exit_fn(void)678843e1988Sjohnlev psm_intr_exit_fn(void)
679843e1988Sjohnlev {
680b6917abeSmishra if (apic_mode == LOCAL_X2APIC)
681b6917abeSmishra return (x2apic_intr_exit);
682b6917abeSmishra
683843e1988Sjohnlev return (apic_intr_exit);
684843e1988Sjohnlev }
685843e1988Sjohnlev
6867c478bd9Sstevel@tonic-gate /*
687b6917abeSmishra * Mask all interrupts below or equal to the given IPL.
688325e77f4SSaurabh Misra * Any changes made to this function must also change X2APIC
689b6917abeSmishra * version of setspl.
6907c478bd9Sstevel@tonic-gate */
6917c478bd9Sstevel@tonic-gate static void
apic_setspl(int ipl)6927c478bd9Sstevel@tonic-gate apic_setspl(int ipl)
6937c478bd9Sstevel@tonic-gate {
69429ddbbefSJosef 'Jeff' Sipek apic_reg_ops->apic_write_task_reg(apic_ipltopri[ipl]);
6957c478bd9Sstevel@tonic-gate
6967c478bd9Sstevel@tonic-gate /* interrupts at ipl above this cannot be in progress */
6977c478bd9Sstevel@tonic-gate apic_cpus[psm_get_cpu_id()].aci_ISR_in_progress &= (2 << ipl) - 1;
6987c478bd9Sstevel@tonic-gate /*
6997c478bd9Sstevel@tonic-gate * this is a patch fix for the ALR QSMP P5 machine, so that interrupts
7007c478bd9Sstevel@tonic-gate * have enough time to come in before the priority is raised again
7017c478bd9Sstevel@tonic-gate * during the idle() loop.
7027c478bd9Sstevel@tonic-gate */
7037c478bd9Sstevel@tonic-gate if (apic_setspl_delay)
704b6917abeSmishra (void) apic_reg_ops->apic_get_pri();
705b6917abeSmishra }
706b6917abeSmishra
707b6917abeSmishra /*
708325e77f4SSaurabh Misra * X2APIC version of setspl.
709b6917abeSmishra * Mask all interrupts below or equal to the given IPL
710b6917abeSmishra */
711b6917abeSmishra static void
x2apic_setspl(int ipl)712b6917abeSmishra x2apic_setspl(int ipl)
713b6917abeSmishra {
714b6917abeSmishra X2APIC_WRITE(APIC_TASK_REG, apic_ipltopri[ipl]);
715b6917abeSmishra
716b6917abeSmishra /* interrupts at ipl above this cannot be in progress */
717b6917abeSmishra apic_cpus[psm_get_cpu_id()].aci_ISR_in_progress &= (2 << ipl) - 1;
7187c478bd9Sstevel@tonic-gate }
7197c478bd9Sstevel@tonic-gate
7207c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7217c478bd9Sstevel@tonic-gate static int
apic_addspl(int irqno,int ipl,int min_ipl,int max_ipl)7227c478bd9Sstevel@tonic-gate apic_addspl(int irqno, int ipl, int min_ipl, int max_ipl)
7237c478bd9Sstevel@tonic-gate {
724ae115bc7Smrj return (apic_addspl_common(irqno, ipl, min_ipl, max_ipl));
7257c478bd9Sstevel@tonic-gate }
7267c478bd9Sstevel@tonic-gate
7277c478bd9Sstevel@tonic-gate static int
apic_delspl(int irqno,int ipl,int min_ipl,int max_ipl)7287c478bd9Sstevel@tonic-gate apic_delspl(int irqno, int ipl, int min_ipl, int max_ipl)
7297c478bd9Sstevel@tonic-gate {
730ae115bc7Smrj return (apic_delspl_common(irqno, ipl, min_ipl, max_ipl));
7317c478bd9Sstevel@tonic-gate }
7327c478bd9Sstevel@tonic-gate
7337c478bd9Sstevel@tonic-gate static int
apic_post_cpu_start(void)7347ff178cdSJimmy Vetayases apic_post_cpu_start(void)
7357c478bd9Sstevel@tonic-gate {
736afbc4541Ssherrym int cpun;
737325e77f4SSaurabh Misra static int cpus_started = 1;
738325e77f4SSaurabh Misra
739325e77f4SSaurabh Misra /* We know this CPU + BSP started successfully. */
740325e77f4SSaurabh Misra cpus_started++;
7417c478bd9Sstevel@tonic-gate
7425d8efbbcSSaurabh Misra /*
7435d8efbbcSSaurabh Misra * On BSP we would have enabled X2APIC, if supported by processor,
7445d8efbbcSSaurabh Misra * in acpi_probe(), but on AP we do it here.
745325e77f4SSaurabh Misra *
746325e77f4SSaurabh Misra * We enable X2APIC mode only if BSP is running in X2APIC & the
747325e77f4SSaurabh Misra * local APIC mode of the current CPU is MMIO (xAPIC).
7485d8efbbcSSaurabh Misra */
749325e77f4SSaurabh Misra if (apic_mode == LOCAL_X2APIC && apic_detect_x2apic() &&
750325e77f4SSaurabh Misra apic_local_mode() == LOCAL_APIC) {
7515d8efbbcSSaurabh Misra apic_enable_x2apic();
7525d8efbbcSSaurabh Misra }
7535d8efbbcSSaurabh Misra
754325e77f4SSaurabh Misra /*
755a3114836SGerry Liu * Switch back to x2apic IPI sending method for performance when target
756a3114836SGerry Liu * CPU has entered x2apic mode.
757325e77f4SSaurabh Misra */
758a3114836SGerry Liu if (apic_mode == LOCAL_X2APIC) {
759a3114836SGerry Liu apic_switch_ipi_callback(B_FALSE);
760325e77f4SSaurabh Misra }
761325e77f4SSaurabh Misra
7625d8efbbcSSaurabh Misra splx(ipltospl(LOCK_LEVEL));
7637c478bd9Sstevel@tonic-gate apic_init_intr();
7647c478bd9Sstevel@tonic-gate
7657c478bd9Sstevel@tonic-gate /*
7667c478bd9Sstevel@tonic-gate * since some systems don't enable the internal cache on the non-boot
7677c478bd9Sstevel@tonic-gate * cpus, so we have to enable them here
7687c478bd9Sstevel@tonic-gate */
769ae115bc7Smrj setcr0(getcr0() & ~(CR0_CD | CR0_NW));
7707c478bd9Sstevel@tonic-gate
7715d8efbbcSSaurabh Misra #ifdef DEBUG
7725d8efbbcSSaurabh Misra APIC_AV_PENDING_SET();
7735d8efbbcSSaurabh Misra #else
7745d8efbbcSSaurabh Misra if (apic_mode == LOCAL_APIC)
7755d8efbbcSSaurabh Misra APIC_AV_PENDING_SET();
7765d8efbbcSSaurabh Misra #endif /* DEBUG */
7777c478bd9Sstevel@tonic-gate
778643e2e74Sbholler /*
779643e2e74Sbholler * We may be booting, or resuming from suspend; aci_status will
780643e2e74Sbholler * be APIC_CPU_INTR_ENABLE if coming from suspend, so we add the
781643e2e74Sbholler * APIC_CPU_ONLINE flag here rather than setting aci_status completely.
782643e2e74Sbholler */
7837c478bd9Sstevel@tonic-gate cpun = psm_get_cpu_id();
784643e2e74Sbholler apic_cpus[cpun].aci_status |= APIC_CPU_ONLINE;
7857c478bd9Sstevel@tonic-gate
786b6917abeSmishra apic_reg_ops->apic_write(APIC_DIVIDE_REG, apic_divide_reg_init);
7877c478bd9Sstevel@tonic-gate return (PSM_SUCCESS);
7887c478bd9Sstevel@tonic-gate }
7897c478bd9Sstevel@tonic-gate
7907c478bd9Sstevel@tonic-gate /*
7917c478bd9Sstevel@tonic-gate * type == -1 indicates it is an internal request. Do not change
7927c478bd9Sstevel@tonic-gate * resv_vector for these requests
7937c478bd9Sstevel@tonic-gate */
7947c478bd9Sstevel@tonic-gate static int
apic_get_ipivect(int ipl,int type)7957c478bd9Sstevel@tonic-gate apic_get_ipivect(int ipl, int type)
7967c478bd9Sstevel@tonic-gate {
7977c478bd9Sstevel@tonic-gate uchar_t vector;
7987c478bd9Sstevel@tonic-gate int irq;
7997c478bd9Sstevel@tonic-gate
80086a9c507SGuoli Shu if ((irq = apic_allocate_irq(APIC_VECTOR(ipl))) != -1) {
801a833a696SJosef 'Jeff' Sipek if ((vector = apic_allocate_vector(ipl, irq, 1))) {
8027c478bd9Sstevel@tonic-gate apic_irq_table[irq]->airq_mps_intr_index =
8037c478bd9Sstevel@tonic-gate RESERVE_INDEX;
8047c478bd9Sstevel@tonic-gate apic_irq_table[irq]->airq_vector = vector;
8057c478bd9Sstevel@tonic-gate if (type != -1) {
8067c478bd9Sstevel@tonic-gate apic_resv_vector[ipl] = vector;
8077c478bd9Sstevel@tonic-gate }
8087c478bd9Sstevel@tonic-gate return (irq);
8097c478bd9Sstevel@tonic-gate }
8107c478bd9Sstevel@tonic-gate }
8117c478bd9Sstevel@tonic-gate apic_error |= APIC_ERR_GET_IPIVECT_FAIL;
8127c478bd9Sstevel@tonic-gate return (-1); /* shouldn't happen */
8137c478bd9Sstevel@tonic-gate }
8147c478bd9Sstevel@tonic-gate
8157c478bd9Sstevel@tonic-gate static int
apic_getclkirq(int ipl)8167c478bd9Sstevel@tonic-gate apic_getclkirq(int ipl)
8177c478bd9Sstevel@tonic-gate {
8187c478bd9Sstevel@tonic-gate int irq;
8197c478bd9Sstevel@tonic-gate
8207c478bd9Sstevel@tonic-gate if ((irq = apic_get_ipivect(ipl, -1)) == -1)
8217c478bd9Sstevel@tonic-gate return (-1);
8227c478bd9Sstevel@tonic-gate /*
8237c478bd9Sstevel@tonic-gate * Note the vector in apic_clkvect for per clock handling.
8247c478bd9Sstevel@tonic-gate */
8257c478bd9Sstevel@tonic-gate apic_clkvect = apic_irq_table[irq]->airq_vector - APIC_BASE_VECT;
8267c478bd9Sstevel@tonic-gate APIC_VERBOSE_IOAPIC((CE_NOTE, "get_clkirq: vector = %x\n",
8277c478bd9Sstevel@tonic-gate apic_clkvect));
8287c478bd9Sstevel@tonic-gate return (irq);
8297c478bd9Sstevel@tonic-gate }
8307c478bd9Sstevel@tonic-gate
8317c478bd9Sstevel@tonic-gate /*
8327c478bd9Sstevel@tonic-gate * Try and disable all interrupts. We just assign interrupts to other
8337c478bd9Sstevel@tonic-gate * processors based on policy. If any were bound by user request, we
8347c478bd9Sstevel@tonic-gate * let them continue and return failure. We do not bother to check
8357c478bd9Sstevel@tonic-gate * for cache affinity while rebinding.
8367c478bd9Sstevel@tonic-gate */
8377c478bd9Sstevel@tonic-gate
8387c478bd9Sstevel@tonic-gate static int
apic_disable_intr(processorid_t cpun)8397c478bd9Sstevel@tonic-gate apic_disable_intr(processorid_t cpun)
8407c478bd9Sstevel@tonic-gate {
841ae115bc7Smrj int bind_cpu = 0, i, hardbound = 0;
8427c478bd9Sstevel@tonic-gate apic_irq_t *irq_ptr;
843ae115bc7Smrj ulong_t iflag;
8447c478bd9Sstevel@tonic-gate
8457c478bd9Sstevel@tonic-gate iflag = intr_clear();
8467c478bd9Sstevel@tonic-gate lock_set(&apic_ioapic_lock);
847e250f1e2Ssethg
848e250f1e2Ssethg for (i = 0; i <= APIC_MAX_VECTOR; i++) {
849e250f1e2Ssethg if (apic_reprogram_info[i].done == B_FALSE) {
850e250f1e2Ssethg if (apic_reprogram_info[i].bindcpu == cpun) {
851e250f1e2Ssethg /*
852e250f1e2Ssethg * CPU is busy -- it's the target of
853e250f1e2Ssethg * a pending reprogramming attempt
854e250f1e2Ssethg */
8557c478bd9Sstevel@tonic-gate lock_clear(&apic_ioapic_lock);
8567c478bd9Sstevel@tonic-gate intr_restore(iflag);
857e250f1e2Ssethg return (PSM_FAILURE);
858e250f1e2Ssethg }
859e250f1e2Ssethg }
860e250f1e2Ssethg }
861e250f1e2Ssethg
862e250f1e2Ssethg apic_cpus[cpun].aci_status &= ~APIC_CPU_INTR_ENABLE;
863e250f1e2Ssethg
8647c478bd9Sstevel@tonic-gate apic_cpus[cpun].aci_curipl = 0;
865e250f1e2Ssethg
8667c478bd9Sstevel@tonic-gate i = apic_min_device_irq;
8677c478bd9Sstevel@tonic-gate for (; i <= apic_max_device_irq; i++) {
8687c478bd9Sstevel@tonic-gate /*
8697c478bd9Sstevel@tonic-gate * If there are bound interrupts on this cpu, then
8707c478bd9Sstevel@tonic-gate * rebind them to other processors.
8717c478bd9Sstevel@tonic-gate */
8727c478bd9Sstevel@tonic-gate if ((irq_ptr = apic_irq_table[i]) != NULL) {
8737c478bd9Sstevel@tonic-gate ASSERT((irq_ptr->airq_temp_cpu == IRQ_UNBOUND) ||
8747c478bd9Sstevel@tonic-gate (irq_ptr->airq_temp_cpu == IRQ_UNINIT) ||
875a3114836SGerry Liu (apic_cpu_in_range(irq_ptr->airq_temp_cpu)));
8767c478bd9Sstevel@tonic-gate
8777c478bd9Sstevel@tonic-gate if (irq_ptr->airq_temp_cpu == (cpun | IRQ_USER_BOUND)) {
8787c478bd9Sstevel@tonic-gate hardbound = 1;
8797c478bd9Sstevel@tonic-gate continue;
8807c478bd9Sstevel@tonic-gate }
8817c478bd9Sstevel@tonic-gate
8827c478bd9Sstevel@tonic-gate if (irq_ptr->airq_temp_cpu == cpun) {
8837c478bd9Sstevel@tonic-gate do {
884e7c3cdaeSjiang.liu@intel.com bind_cpu =
885e7c3cdaeSjiang.liu@intel.com apic_find_cpu(APIC_CPU_INTR_ENABLE);
886e250f1e2Ssethg } while (apic_rebind_all(irq_ptr, bind_cpu));
8877c478bd9Sstevel@tonic-gate }
8887c478bd9Sstevel@tonic-gate }
8897c478bd9Sstevel@tonic-gate }
890e250f1e2Ssethg
891e250f1e2Ssethg lock_clear(&apic_ioapic_lock);
892e250f1e2Ssethg intr_restore(iflag);
893e250f1e2Ssethg
8947c478bd9Sstevel@tonic-gate if (hardbound) {
8957c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "Could not disable interrupts on %d"
8967c478bd9Sstevel@tonic-gate "due to user bound interrupts", cpun);
8977c478bd9Sstevel@tonic-gate return (PSM_FAILURE);
8987c478bd9Sstevel@tonic-gate }
8997c478bd9Sstevel@tonic-gate else
9007c478bd9Sstevel@tonic-gate return (PSM_SUCCESS);
9017c478bd9Sstevel@tonic-gate }
9027c478bd9Sstevel@tonic-gate
903643e2e74Sbholler /*
904643e2e74Sbholler * Bind interrupts to the CPU's local APIC.
905643e2e74Sbholler * Interrupts should not be bound to a CPU's local APIC until the CPU
906643e2e74Sbholler * is ready to receive interrupts.
907643e2e74Sbholler */
9087c478bd9Sstevel@tonic-gate static void
apic_enable_intr(processorid_t cpun)9097c478bd9Sstevel@tonic-gate apic_enable_intr(processorid_t cpun)
9107c478bd9Sstevel@tonic-gate {
911ae115bc7Smrj int i;
9127c478bd9Sstevel@tonic-gate apic_irq_t *irq_ptr;
913ae115bc7Smrj ulong_t iflag;
9147c478bd9Sstevel@tonic-gate
9157c478bd9Sstevel@tonic-gate iflag = intr_clear();
9167c478bd9Sstevel@tonic-gate lock_set(&apic_ioapic_lock);
917e250f1e2Ssethg
9187c478bd9Sstevel@tonic-gate apic_cpus[cpun].aci_status |= APIC_CPU_INTR_ENABLE;
9197c478bd9Sstevel@tonic-gate
9207c478bd9Sstevel@tonic-gate i = apic_min_device_irq;
9217c478bd9Sstevel@tonic-gate for (i = apic_min_device_irq; i <= apic_max_device_irq; i++) {
9227c478bd9Sstevel@tonic-gate if ((irq_ptr = apic_irq_table[i]) != NULL) {
9237c478bd9Sstevel@tonic-gate if ((irq_ptr->airq_cpu & ~IRQ_USER_BOUND) == cpun) {
9247c478bd9Sstevel@tonic-gate (void) apic_rebind_all(irq_ptr,
925e250f1e2Ssethg irq_ptr->airq_cpu);
9267c478bd9Sstevel@tonic-gate }
9277c478bd9Sstevel@tonic-gate }
9287c478bd9Sstevel@tonic-gate }
929e250f1e2Ssethg
9307ff178cdSJimmy Vetayases if (apic_cpus[cpun].aci_status & APIC_CPU_SUSPEND)
9317ff178cdSJimmy Vetayases apic_cpus[cpun].aci_status &= ~APIC_CPU_SUSPEND;
9327ff178cdSJimmy Vetayases
933e250f1e2Ssethg lock_clear(&apic_ioapic_lock);
934e250f1e2Ssethg intr_restore(iflag);
9357c478bd9Sstevel@tonic-gate }
9367c478bd9Sstevel@tonic-gate
9377c478bd9Sstevel@tonic-gate /*
938dd4eeefdSeota * If this module needs a periodic handler for the interrupt distribution, it
939dd4eeefdSeota * can be added here. The argument to the periodic handler is not currently
940dd4eeefdSeota * used, but is reserved for future.
9417c478bd9Sstevel@tonic-gate */
9427c478bd9Sstevel@tonic-gate static void
apic_post_cyclic_setup(void * arg)9437c478bd9Sstevel@tonic-gate apic_post_cyclic_setup(void *arg)
9447c478bd9Sstevel@tonic-gate {
9457c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(arg))
946a288e5a9SJoshua M. Clulow
947a288e5a9SJoshua M. Clulow cyc_handler_t cyh;
948a288e5a9SJoshua M. Clulow cyc_time_t cyt;
949a288e5a9SJoshua M. Clulow
9507c478bd9Sstevel@tonic-gate /* cpu_lock is held */
951dd4eeefdSeota /* set up a periodic handler for intr redistribution */
9527c478bd9Sstevel@tonic-gate
9537c478bd9Sstevel@tonic-gate /*
9547c478bd9Sstevel@tonic-gate * In peridoc mode intr redistribution processing is done in
9557c478bd9Sstevel@tonic-gate * apic_intr_enter during clk intr processing
9567c478bd9Sstevel@tonic-gate */
9577c478bd9Sstevel@tonic-gate if (!apic_oneshot)
9587c478bd9Sstevel@tonic-gate return;
959a288e5a9SJoshua M. Clulow
960dd4eeefdSeota /*
961dd4eeefdSeota * Register a periodical handler for the redistribution processing.
962a288e5a9SJoshua M. Clulow * Though we would generally prefer to use the DDI interface for
963a288e5a9SJoshua M. Clulow * periodic handler invocation, ddi_periodic_add(9F), we are
964a288e5a9SJoshua M. Clulow * unfortunately already holding cpu_lock, which ddi_periodic_add will
965a288e5a9SJoshua M. Clulow * attempt to take for us. Thus, we add our own cyclic directly:
966dd4eeefdSeota */
967a288e5a9SJoshua M. Clulow cyh.cyh_func = (void (*)(void *))apic_redistribute_compute;
968a288e5a9SJoshua M. Clulow cyh.cyh_arg = NULL;
969a288e5a9SJoshua M. Clulow cyh.cyh_level = CY_LOW_LEVEL;
970a288e5a9SJoshua M. Clulow
971a288e5a9SJoshua M. Clulow cyt.cyt_when = 0;
972a288e5a9SJoshua M. Clulow cyt.cyt_interval = apic_redistribute_sample_interval;
973a288e5a9SJoshua M. Clulow
974a288e5a9SJoshua M. Clulow apic_cyclic_id = cyclic_add(&cyh, &cyt);
9757c478bd9Sstevel@tonic-gate }
9767c478bd9Sstevel@tonic-gate
9777c478bd9Sstevel@tonic-gate static void
apic_redistribute_compute(void)9787c478bd9Sstevel@tonic-gate apic_redistribute_compute(void)
9797c478bd9Sstevel@tonic-gate {
9807c478bd9Sstevel@tonic-gate int i, j, max_busy;
9817c478bd9Sstevel@tonic-gate
9827c478bd9Sstevel@tonic-gate if (apic_enable_dynamic_migration) {
9837c478bd9Sstevel@tonic-gate if (++apic_nticks == apic_sample_factor_redistribution) {
9847c478bd9Sstevel@tonic-gate /*
9857c478bd9Sstevel@tonic-gate * Time to call apic_intr_redistribute().
9867c478bd9Sstevel@tonic-gate * reset apic_nticks. This will cause max_busy
9877c478bd9Sstevel@tonic-gate * to be calculated below and if it is more than
9887c478bd9Sstevel@tonic-gate * apic_int_busy, we will do the whole thing
9897c478bd9Sstevel@tonic-gate */
9907c478bd9Sstevel@tonic-gate apic_nticks = 0;
9917c478bd9Sstevel@tonic-gate }
9927c478bd9Sstevel@tonic-gate max_busy = 0;
9937c478bd9Sstevel@tonic-gate for (i = 0; i < apic_nproc; i++) {
994a3114836SGerry Liu if (!apic_cpu_in_range(i))
995a3114836SGerry Liu continue;
9967c478bd9Sstevel@tonic-gate
9977c478bd9Sstevel@tonic-gate /*
9987c478bd9Sstevel@tonic-gate * Check if curipl is non zero & if ISR is in
9997c478bd9Sstevel@tonic-gate * progress
10007c478bd9Sstevel@tonic-gate */
10017c478bd9Sstevel@tonic-gate if (((j = apic_cpus[i].aci_curipl) != 0) &&
10027c478bd9Sstevel@tonic-gate (apic_cpus[i].aci_ISR_in_progress & (1 << j))) {
10037c478bd9Sstevel@tonic-gate
10047c478bd9Sstevel@tonic-gate int irq;
10057c478bd9Sstevel@tonic-gate apic_cpus[i].aci_busy++;
10067c478bd9Sstevel@tonic-gate irq = apic_cpus[i].aci_current[j];
10077c478bd9Sstevel@tonic-gate apic_irq_table[irq]->airq_busy++;
10087c478bd9Sstevel@tonic-gate }
10097c478bd9Sstevel@tonic-gate
10107c478bd9Sstevel@tonic-gate if (!apic_nticks &&
10117c478bd9Sstevel@tonic-gate (apic_cpus[i].aci_busy > max_busy))
10127c478bd9Sstevel@tonic-gate max_busy = apic_cpus[i].aci_busy;
10137c478bd9Sstevel@tonic-gate }
10147c478bd9Sstevel@tonic-gate if (!apic_nticks) {
10157c478bd9Sstevel@tonic-gate if (max_busy > apic_int_busy_mark) {
10167c478bd9Sstevel@tonic-gate /*
10177c478bd9Sstevel@tonic-gate * We could make the following check be
10187c478bd9Sstevel@tonic-gate * skipped > 1 in which case, we get a
10197c478bd9Sstevel@tonic-gate * redistribution at half the busy mark (due to
10207c478bd9Sstevel@tonic-gate * double interval). Need to be able to collect
10217c478bd9Sstevel@tonic-gate * more empirical data to decide if that is a
10227c478bd9Sstevel@tonic-gate * good strategy. Punt for now.
10237c478bd9Sstevel@tonic-gate */
1024ae115bc7Smrj if (apic_skipped_redistribute) {
10257c478bd9Sstevel@tonic-gate apic_cleanup_busy();
1026ae115bc7Smrj apic_skipped_redistribute = 0;
1027ae115bc7Smrj } else {
10287c478bd9Sstevel@tonic-gate apic_intr_redistribute();
1029ae115bc7Smrj }
10307c478bd9Sstevel@tonic-gate } else
10317c478bd9Sstevel@tonic-gate apic_skipped_redistribute++;
10327c478bd9Sstevel@tonic-gate }
10337c478bd9Sstevel@tonic-gate }
10347c478bd9Sstevel@tonic-gate }
10357c478bd9Sstevel@tonic-gate
10367c478bd9Sstevel@tonic-gate
1037ae115bc7Smrj /*
1038ae115bc7Smrj * The following functions are in the platform specific file so that they
1039ae115bc7Smrj * can be different functions depending on whether we are running on
1040ae115bc7Smrj * bare metal or a hypervisor.
1041ae115bc7Smrj */
1042ae115bc7Smrj
1043ae115bc7Smrj /*
1044a7639048Sjohnny * Check to make sure there are enough irq slots
1045e250f1e2Ssethg */
1046ae115bc7Smrj int
apic_check_free_irqs(int count)1047a7639048Sjohnny apic_check_free_irqs(int count)
1048a7639048Sjohnny {
1049a7639048Sjohnny int i, avail;
1050a7639048Sjohnny
1051a7639048Sjohnny avail = 0;
1052a7639048Sjohnny for (i = APIC_FIRST_FREE_IRQ; i < APIC_RESV_IRQ; i++) {
1053a7639048Sjohnny if ((apic_irq_table[i] == NULL) ||
1054a7639048Sjohnny apic_irq_table[i]->airq_mps_intr_index == FREE_INDEX) {
1055a7639048Sjohnny if (++avail >= count)
1056a7639048Sjohnny return (PSM_SUCCESS);
1057a7639048Sjohnny }
1058a7639048Sjohnny }
1059a7639048Sjohnny return (PSM_FAILURE);
1060a7639048Sjohnny }
1061a7639048Sjohnny
1062a7639048Sjohnny /*
1063a7639048Sjohnny * This function allocates "count" MSI vector(s) for the given "dip/pri/type"
1064a7639048Sjohnny */
1065a7639048Sjohnny int
apic_alloc_msi_vectors(dev_info_t * dip,int inum,int count,int pri,int behavior)1066a7639048Sjohnny apic_alloc_msi_vectors(dev_info_t *dip, int inum, int count, int pri,
1067ae115bc7Smrj int behavior)
1068e250f1e2Ssethg {
1069ae115bc7Smrj int rcount, i;
1070b6917abeSmishra uchar_t start, irqno;
1071b6917abeSmishra uint32_t cpu;
1072ae115bc7Smrj major_t major;
1073ae115bc7Smrj apic_irq_t *irqptr;
1074ae115bc7Smrj
1075a7639048Sjohnny DDI_INTR_IMPLDBG((CE_CONT, "apic_alloc_msi_vectors: dip=0x%p "
1076ae115bc7Smrj "inum=0x%x pri=0x%x count=0x%x behavior=%d\n",
1077a7639048Sjohnny (void *)dip, inum, pri, count, behavior));
1078ae115bc7Smrj
1079ae115bc7Smrj if (count > 1) {
1080ae115bc7Smrj if (behavior == DDI_INTR_ALLOC_STRICT &&
108163ea9ad2SEvan Yan apic_multi_msi_enable == 0)
1082ae115bc7Smrj return (0);
1083ae115bc7Smrj if (apic_multi_msi_enable == 0)
1084ae115bc7Smrj count = 1;
1085ae115bc7Smrj }
1086ae115bc7Smrj
1087ae115bc7Smrj if ((rcount = apic_navail_vector(dip, pri)) > count)
1088ae115bc7Smrj rcount = count;
1089ae115bc7Smrj else if (rcount == 0 || (rcount < count &&
1090ae115bc7Smrj behavior == DDI_INTR_ALLOC_STRICT))
1091ae115bc7Smrj return (0);
1092ae115bc7Smrj
1093ae115bc7Smrj /* if not ISP2, then round it down */
1094ae115bc7Smrj if (!ISP2(rcount))
1095ae115bc7Smrj rcount = 1 << (highbit(rcount) - 1);
1096ae115bc7Smrj
1097ae115bc7Smrj mutex_enter(&airq_mutex);
1098ae115bc7Smrj
1099ae115bc7Smrj for (start = 0; rcount > 0; rcount >>= 1) {
1100ae115bc7Smrj if ((start = apic_find_multi_vectors(pri, rcount)) != 0 ||
1101ae115bc7Smrj behavior == DDI_INTR_ALLOC_STRICT)
1102ae115bc7Smrj break;
1103ae115bc7Smrj }
1104ae115bc7Smrj
1105ae115bc7Smrj if (start == 0) {
1106ae115bc7Smrj /* no vector available */
1107ae115bc7Smrj mutex_exit(&airq_mutex);
1108ae115bc7Smrj return (0);
1109ae115bc7Smrj }
1110ae115bc7Smrj
1111a7639048Sjohnny if (apic_check_free_irqs(rcount) == PSM_FAILURE) {
1112a7639048Sjohnny /* not enough free irq slots available */
1113a7639048Sjohnny mutex_exit(&airq_mutex);
1114a7639048Sjohnny return (0);
1115a7639048Sjohnny }
1116a7639048Sjohnny
11175c066ec2SJerry Gilliam major = (dip != NULL) ? ddi_driver_major(dip) : 0;
1118ae115bc7Smrj for (i = 0; i < rcount; i++) {
1119ae115bc7Smrj if ((irqno = apic_allocate_irq(apic_first_avail_irq)) ==
1120ae115bc7Smrj (uchar_t)-1) {
1121a7639048Sjohnny /*
1122a7639048Sjohnny * shouldn't happen because of the
1123a7639048Sjohnny * apic_check_free_irqs() check earlier
1124a7639048Sjohnny */
1125ae115bc7Smrj mutex_exit(&airq_mutex);
1126a7639048Sjohnny DDI_INTR_IMPLDBG((CE_CONT, "apic_alloc_msi_vectors: "
1127ae115bc7Smrj "apic_allocate_irq failed\n"));
1128ae115bc7Smrj return (i);
1129ae115bc7Smrj }
1130ae115bc7Smrj apic_max_device_irq = max(irqno, apic_max_device_irq);
1131ae115bc7Smrj apic_min_device_irq = min(irqno, apic_min_device_irq);
1132ae115bc7Smrj irqptr = apic_irq_table[irqno];
1133ae115bc7Smrj #ifdef DEBUG
1134ae115bc7Smrj if (apic_vector_to_irq[start + i] != APIC_RESV_IRQ)
1135a7639048Sjohnny DDI_INTR_IMPLDBG((CE_CONT, "apic_alloc_msi_vectors: "
1136ae115bc7Smrj "apic_vector_to_irq is not APIC_RESV_IRQ\n"));
1137ae115bc7Smrj #endif
1138ae115bc7Smrj apic_vector_to_irq[start + i] = (uchar_t)irqno;
1139ae115bc7Smrj
1140ae115bc7Smrj irqptr->airq_vector = (uchar_t)(start + i);
1141ae115bc7Smrj irqptr->airq_ioapicindex = (uchar_t)inum; /* start */
1142ae115bc7Smrj irqptr->airq_intin_no = (uchar_t)rcount;
1143ae115bc7Smrj irqptr->airq_ipl = pri;
1144ae115bc7Smrj irqptr->airq_vector = start + i;
1145ae115bc7Smrj irqptr->airq_origirq = (uchar_t)(inum + i);
1146ae115bc7Smrj irqptr->airq_share_id = 0;
1147ae115bc7Smrj irqptr->airq_mps_intr_index = MSI_INDEX;
1148ae115bc7Smrj irqptr->airq_dip = dip;
1149ae115bc7Smrj irqptr->airq_major = major;
1150ae115bc7Smrj if (i == 0) /* they all bound to the same cpu */
1151ae115bc7Smrj cpu = irqptr->airq_cpu = apic_bind_intr(dip, irqno,
1152ae115bc7Smrj 0xff, 0xff);
1153ae115bc7Smrj else
1154ae115bc7Smrj irqptr->airq_cpu = cpu;
1155a7639048Sjohnny DDI_INTR_IMPLDBG((CE_CONT, "apic_alloc_msi_vectors: irq=0x%x "
1156ae115bc7Smrj "dip=0x%p vector=0x%x origirq=0x%x pri=0x%x\n", irqno,
1157ae115bc7Smrj (void *)irqptr->airq_dip, irqptr->airq_vector,
1158ae115bc7Smrj irqptr->airq_origirq, pri));
1159ae115bc7Smrj }
1160ae115bc7Smrj mutex_exit(&airq_mutex);
1161ae115bc7Smrj return (rcount);
1162ae115bc7Smrj }
1163ae115bc7Smrj
1164ae115bc7Smrj /*
1165a7639048Sjohnny * This function allocates "count" MSI-X vector(s) for the given "dip/pri/type"
1166a7639048Sjohnny */
1167a7639048Sjohnny int
apic_alloc_msix_vectors(dev_info_t * dip,int inum,int count,int pri,int behavior)1168a7639048Sjohnny apic_alloc_msix_vectors(dev_info_t *dip, int inum, int count, int pri,
1169a7639048Sjohnny int behavior)
1170a7639048Sjohnny {
1171a7639048Sjohnny int rcount, i;
1172a7639048Sjohnny major_t major;
1173a7639048Sjohnny
1174a7639048Sjohnny mutex_enter(&airq_mutex);
1175a7639048Sjohnny
1176a7639048Sjohnny if ((rcount = apic_navail_vector(dip, pri)) > count)
1177a7639048Sjohnny rcount = count;
1178a7639048Sjohnny else if (rcount == 0 || (rcount < count &&
1179a7639048Sjohnny behavior == DDI_INTR_ALLOC_STRICT)) {
1180a7639048Sjohnny rcount = 0;
1181a7639048Sjohnny goto out;
1182a7639048Sjohnny }
1183a7639048Sjohnny
1184a7639048Sjohnny if (apic_check_free_irqs(rcount) == PSM_FAILURE) {
1185a7639048Sjohnny /* not enough free irq slots available */
1186a7639048Sjohnny rcount = 0;
1187a7639048Sjohnny goto out;
1188a7639048Sjohnny }
1189a7639048Sjohnny
11905c066ec2SJerry Gilliam major = (dip != NULL) ? ddi_driver_major(dip) : 0;
1191a7639048Sjohnny for (i = 0; i < rcount; i++) {
1192a7639048Sjohnny uchar_t vector, irqno;
1193a7639048Sjohnny apic_irq_t *irqptr;
1194a7639048Sjohnny
1195a7639048Sjohnny if ((irqno = apic_allocate_irq(apic_first_avail_irq)) ==
1196a7639048Sjohnny (uchar_t)-1) {
1197a7639048Sjohnny /*
1198a7639048Sjohnny * shouldn't happen because of the
1199a7639048Sjohnny * apic_check_free_irqs() check earlier
1200a7639048Sjohnny */
1201a7639048Sjohnny DDI_INTR_IMPLDBG((CE_CONT, "apic_alloc_msix_vectors: "
1202a7639048Sjohnny "apic_allocate_irq failed\n"));
1203a7639048Sjohnny rcount = i;
1204a7639048Sjohnny goto out;
1205a7639048Sjohnny }
1206a7639048Sjohnny if ((vector = apic_allocate_vector(pri, irqno, 1)) == 0) {
1207a7639048Sjohnny /*
1208a7639048Sjohnny * shouldn't happen because of the
1209a7639048Sjohnny * apic_navail_vector() call earlier
1210a7639048Sjohnny */
1211a7639048Sjohnny DDI_INTR_IMPLDBG((CE_CONT, "apic_alloc_msix_vectors: "
1212a7639048Sjohnny "apic_allocate_vector failed\n"));
1213a7639048Sjohnny rcount = i;
1214a7639048Sjohnny goto out;
1215a7639048Sjohnny }
1216a7639048Sjohnny apic_max_device_irq = max(irqno, apic_max_device_irq);
1217a7639048Sjohnny apic_min_device_irq = min(irqno, apic_min_device_irq);
1218a7639048Sjohnny irqptr = apic_irq_table[irqno];
1219a7639048Sjohnny irqptr->airq_vector = (uchar_t)vector;
1220a7639048Sjohnny irqptr->airq_ipl = pri;
1221a7639048Sjohnny irqptr->airq_origirq = (uchar_t)(inum + i);
1222a7639048Sjohnny irqptr->airq_share_id = 0;
1223a7639048Sjohnny irqptr->airq_mps_intr_index = MSIX_INDEX;
1224a7639048Sjohnny irqptr->airq_dip = dip;
1225a7639048Sjohnny irqptr->airq_major = major;
1226a7639048Sjohnny irqptr->airq_cpu = apic_bind_intr(dip, irqno, 0xff, 0xff);
1227a7639048Sjohnny }
1228a7639048Sjohnny out:
1229a7639048Sjohnny mutex_exit(&airq_mutex);
1230a7639048Sjohnny return (rcount);
1231a7639048Sjohnny }
1232a7639048Sjohnny
1233a7639048Sjohnny /*
1234ae115bc7Smrj * Allocate a free vector for irq at ipl. Takes care of merging of multiple
1235ae115bc7Smrj * IPLs into a single APIC level as well as stretching some IPLs onto multiple
1236ae115bc7Smrj * levels. APIC_HI_PRI_VECTS interrupts are reserved for high priority
1237ae115bc7Smrj * requests and allocated only when pri is set.
1238ae115bc7Smrj */
1239ae115bc7Smrj uchar_t
apic_allocate_vector(int ipl,int irq,int pri)1240ae115bc7Smrj apic_allocate_vector(int ipl, int irq, int pri)
1241ae115bc7Smrj {
1242ae115bc7Smrj int lowest, highest, i;
1243ae115bc7Smrj
1244ae115bc7Smrj highest = apic_ipltopri[ipl] + APIC_VECTOR_MASK;
1245ae115bc7Smrj lowest = apic_ipltopri[ipl - 1] + APIC_VECTOR_PER_IPL;
1246ae115bc7Smrj
1247ae115bc7Smrj if (highest < lowest) /* Both ipl and ipl - 1 map to same pri */
1248ae115bc7Smrj lowest -= APIC_VECTOR_PER_IPL;
1249ae115bc7Smrj
1250ae115bc7Smrj #ifdef DEBUG
1251ae115bc7Smrj if (apic_restrict_vector) /* for testing shared interrupt logic */
1252ae115bc7Smrj highest = lowest + apic_restrict_vector + APIC_HI_PRI_VECTS;
1253ae115bc7Smrj #endif /* DEBUG */
1254ae115bc7Smrj if (pri == 0)
1255ae115bc7Smrj highest -= APIC_HI_PRI_VECTS;
1256ae115bc7Smrj
125726896e4cSGuoli Shu for (i = lowest; i <= highest; i++) {
1258ae115bc7Smrj if (APIC_CHECK_RESERVE_VECTORS(i))
1259ae115bc7Smrj continue;
1260ae115bc7Smrj if (apic_vector_to_irq[i] == APIC_RESV_IRQ) {
1261ae115bc7Smrj apic_vector_to_irq[i] = (uchar_t)irq;
1262ae115bc7Smrj return (i);
1263ae115bc7Smrj }
1264ae115bc7Smrj }
1265ae115bc7Smrj
1266ae115bc7Smrj return (0);
1267ae115bc7Smrj }
1268ae115bc7Smrj
1269ae115bc7Smrj /* Mark vector as not being used by any irq */
1270ae115bc7Smrj void
apic_free_vector(uchar_t vector)1271ae115bc7Smrj apic_free_vector(uchar_t vector)
1272ae115bc7Smrj {
1273ae115bc7Smrj apic_vector_to_irq[vector] = APIC_RESV_IRQ;
1274ae115bc7Smrj }
1275ae115bc7Smrj
1276e7c3cdaeSjiang.liu@intel.com /*
1277ae115bc7Smrj * Call rebind to do the actual programming.
1278ae115bc7Smrj * Must be called with interrupts disabled and apic_ioapic_lock held
1279ae115bc7Smrj * 'p' is polymorphic -- if this function is called to process a deferred
1280ae115bc7Smrj * reprogramming, p is of type 'struct ioapic_reprogram_data *', from which
1281ae115bc7Smrj * the irq pointer is retrieved. If not doing deferred reprogramming,
1282ae115bc7Smrj * p is of the type 'apic_irq_t *'.
1283ae115bc7Smrj *
1284ae115bc7Smrj * apic_ioapic_lock must be held across this call, as it protects apic_rebind
12857ff178cdSJimmy Vetayases * and it protects apic_get_next_bind_cpu() from a race in which a CPU can be
1286a3114836SGerry Liu * taken offline after a cpu is selected, but before apic_rebind is called to
1287ae115bc7Smrj * bind interrupts to it.
1288e250f1e2Ssethg */
1289ae115bc7Smrj int
apic_setup_io_intr(void * p,int irq,boolean_t deferred)1290ae115bc7Smrj apic_setup_io_intr(void *p, int irq, boolean_t deferred)
12917c478bd9Sstevel@tonic-gate {
1292ae115bc7Smrj apic_irq_t *irqptr;
1293ae115bc7Smrj struct ioapic_reprogram_data *drep = NULL;
1294ae115bc7Smrj int rv;
12957c478bd9Sstevel@tonic-gate
1296ae115bc7Smrj if (deferred) {
1297ae115bc7Smrj drep = (struct ioapic_reprogram_data *)p;
1298ae115bc7Smrj ASSERT(drep != NULL);
1299ae115bc7Smrj irqptr = drep->irqp;
1300ae115bc7Smrj } else
1301ae115bc7Smrj irqptr = (apic_irq_t *)p;
1302e250f1e2Ssethg
1303ae115bc7Smrj ASSERT(irqptr != NULL);
13047c478bd9Sstevel@tonic-gate
1305ae115bc7Smrj rv = apic_rebind(irqptr, apic_irq_table[irq]->airq_cpu, drep);
1306ae115bc7Smrj if (rv) {
13077c478bd9Sstevel@tonic-gate /*
1308ae115bc7Smrj * CPU is not up or interrupts are disabled. Fall back to
1309ae115bc7Smrj * the first available CPU
13107c478bd9Sstevel@tonic-gate */
1311e7c3cdaeSjiang.liu@intel.com rv = apic_rebind(irqptr, apic_find_cpu(APIC_CPU_INTR_ENABLE),
1312e7c3cdaeSjiang.liu@intel.com drep);
13137c478bd9Sstevel@tonic-gate }
13147c478bd9Sstevel@tonic-gate
1315ae115bc7Smrj return (rv);
13167c478bd9Sstevel@tonic-gate }
13177c478bd9Sstevel@tonic-gate
13187c478bd9Sstevel@tonic-gate
1319ae115bc7Smrj uchar_t
apic_modify_vector(uchar_t vector,int irq)1320ae115bc7Smrj apic_modify_vector(uchar_t vector, int irq)
1321ae115bc7Smrj {
1322ae115bc7Smrj apic_vector_to_irq[vector] = (uchar_t)irq;
1323ae115bc7Smrj return (vector);
13247c478bd9Sstevel@tonic-gate }
13252917a9c9Sschwartz
13262917a9c9Sschwartz char *
apic_get_apic_type(void)13277ff178cdSJimmy Vetayases apic_get_apic_type(void)
13282917a9c9Sschwartz {
13292917a9c9Sschwartz return (apic_psm_info.p_mach_idstring);
13302917a9c9Sschwartz }
1331b6917abeSmishra
1332b6917abeSmishra void
x2apic_update_psm(void)13337ff178cdSJimmy Vetayases x2apic_update_psm(void)
1334b6917abeSmishra {
1335b6917abeSmishra struct psm_ops *pops = &apic_ops;
1336b6917abeSmishra
1337b6917abeSmishra ASSERT(pops != NULL);
1338b6917abeSmishra
1339b6917abeSmishra pops->psm_intr_exit = x2apic_intr_exit;
1340b6917abeSmishra pops->psm_setspl = x2apic_setspl;
13417ff178cdSJimmy Vetayases
1342a3114836SGerry Liu pops->psm_send_ipi = x2apic_send_ipi;
1343b6917abeSmishra send_dirintf = pops->psm_send_ipi;
1344325e77f4SSaurabh Misra
1345325e77f4SSaurabh Misra apic_mode = LOCAL_X2APIC;
1346325e77f4SSaurabh Misra apic_change_ops();
1347b6917abeSmishra }
1348