xref: /onnv-gate/usr/src/uts/i86pc/io/mp_platform_common.c (revision 13029:2ba2eec82e2a)
13446Smrj /*
23446Smrj  * CDDL HEADER START
33446Smrj  *
43446Smrj  * The contents of this file are subject to the terms of the
53446Smrj  * Common Development and Distribution License (the "License").
63446Smrj  * You may not use this file except in compliance with the License.
73446Smrj  *
83446Smrj  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93446Smrj  * or http://www.opensolaris.org/os/licensing.
103446Smrj  * See the License for the specific language governing permissions
113446Smrj  * and limitations under the License.
123446Smrj  *
133446Smrj  * When distributing Covered Code, include this CDDL HEADER in each
143446Smrj  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153446Smrj  * If applicable, add the following below this CDDL HEADER, with the
163446Smrj  * fields enclosed by brackets "[]" replaced with your own identifying
173446Smrj  * information: Portions Copyright [yyyy] [name of copyright owner]
183446Smrj  *
193446Smrj  * CDDL HEADER END
203446Smrj  */
213446Smrj /*
2212204Sjiang.liu@intel.com  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
233446Smrj  */
2412004Sjiang.liu@intel.com /*
2512004Sjiang.liu@intel.com  * Copyright (c) 2010, Intel Corporation.
2612004Sjiang.liu@intel.com  * All rights reserved.
2712004Sjiang.liu@intel.com  */
283446Smrj 
293446Smrj /*
303446Smrj  * PSMI 1.1 extensions are supported only in 2.6 and later versions.
313446Smrj  * PSMI 1.2 extensions are supported only in 2.7 and later versions.
323446Smrj  * PSMI 1.3 and 1.4 extensions are supported in Solaris 10.
333446Smrj  * PSMI 1.5 extensions are supported in Solaris Nevada.
345295Srandyf  * PSMI 1.6 extensions are supported in Solaris Nevada.
3512004Sjiang.liu@intel.com  * PSMI 1.7 extensions are supported in Solaris Nevada.
363446Smrj  */
3712004Sjiang.liu@intel.com #define	PSMI_1_7
383446Smrj 
393446Smrj #include <sys/processor.h>
403446Smrj #include <sys/time.h>
413446Smrj #include <sys/psm.h>
423446Smrj #include <sys/smp_impldefs.h>
433446Smrj #include <sys/cram.h>
443446Smrj #include <sys/acpi/acpi.h>
453446Smrj #include <sys/acpica.h>
463446Smrj #include <sys/psm_common.h>
473446Smrj #include <sys/apic.h>
48*13029SKrishnendu.Sadhukhan@Sun.COM #include <sys/apic_timer.h>
493446Smrj #include <sys/pit.h>
503446Smrj #include <sys/ddi.h>
513446Smrj #include <sys/sunddi.h>
523446Smrj #include <sys/ddi_impldefs.h>
533446Smrj #include <sys/pci.h>
543446Smrj #include <sys/promif.h>
553446Smrj #include <sys/x86_archext.h>
563446Smrj #include <sys/cpc_impl.h>
573446Smrj #include <sys/uadmin.h>
583446Smrj #include <sys/panic.h>
593446Smrj #include <sys/debug.h>
603446Smrj #include <sys/archsystm.h>
613446Smrj #include <sys/trap.h>
623446Smrj #include <sys/machsystm.h>
633446Smrj #include <sys/cpuvar.h>
643446Smrj #include <sys/rm_platter.h>
653446Smrj #include <sys/privregs.h>
663446Smrj #include <sys/cyclic.h>
673446Smrj #include <sys/note.h>
683446Smrj #include <sys/pci_intr_lib.h>
693446Smrj #include <sys/sunndi.h>
708906SEric.Saxe@Sun.COM #if !defined(__xpv)
718906SEric.Saxe@Sun.COM #include <sys/hpet.h>
728906SEric.Saxe@Sun.COM #include <sys/clock.h>
738906SEric.Saxe@Sun.COM #endif
743446Smrj 
753446Smrj /*
763446Smrj  *	Local Function Prototypes
773446Smrj  */
783446Smrj static int apic_handle_defconf();
793446Smrj static int apic_parse_mpct(caddr_t mpct, int bypass);
803446Smrj static struct apic_mpfps_hdr *apic_find_fps_sig(caddr_t fptr, int size);
813446Smrj static int apic_checksum(caddr_t bptr, int len);
823446Smrj static int apic_find_bus_type(char *bus);
833446Smrj static int apic_find_bus(int busid);
843446Smrj static struct apic_io_intr *apic_find_io_intr(int irqno);
853446Smrj static int apic_find_free_irq(int start, int end);
8612683SJimmy.Vetayases@oracle.com struct apic_io_intr *apic_find_io_intr_w_busid(int irqno, int busid);
873446Smrj static void apic_set_pwroff_method_from_mpcnfhdr(struct apic_mp_cnf_hdr *hdrp);
8812004Sjiang.liu@intel.com static void apic_free_apic_cpus(void);
893745Ssethg static boolean_t apic_is_ioapic_AMD_813x(uint32_t physaddr);
907515SSeth.Goldberg@Sun.COM static int apic_acpi_enter_apicmode(void);
913446Smrj 
9212683SJimmy.Vetayases@oracle.com int apic_handle_pci_pci_bridge(dev_info_t *idip, int child_devno,
9312683SJimmy.Vetayases@oracle.com     int child_ipin, struct apic_io_intr **intrp);
9412683SJimmy.Vetayases@oracle.com int apic_find_bus_id(int bustype);
9512683SJimmy.Vetayases@oracle.com int apic_find_intin(uchar_t ioapic, uchar_t intin);
9612683SJimmy.Vetayases@oracle.com void apic_record_rdt_entry(apic_irq_t *irqptr, int irq);
9712683SJimmy.Vetayases@oracle.com 
983446Smrj int apic_debug_mps_id = 0;	/* 1 - print MPS ID strings */
993446Smrj 
1003446Smrj /* ACPI SCI interrupt configuration; -1 if SCI not used */
1013446Smrj int apic_sci_vect = -1;
1023446Smrj iflag_t apic_sci_flags;
1033446Smrj 
1048906SEric.Saxe@Sun.COM #if !defined(__xpv)
1058906SEric.Saxe@Sun.COM /* ACPI HPET interrupt configuration; -1 if HPET not used */
1068906SEric.Saxe@Sun.COM int apic_hpet_vect = -1;
1078906SEric.Saxe@Sun.COM iflag_t apic_hpet_flags;
1088906SEric.Saxe@Sun.COM #endif
1098906SEric.Saxe@Sun.COM 
1103446Smrj /*
1113446Smrj  * psm name pointer
1123446Smrj  */
11312683SJimmy.Vetayases@oracle.com char *psm_name;
1143446Smrj 
1153446Smrj /* ACPI support routines */
1163446Smrj static int acpi_probe(char *);
1173446Smrj static int apic_acpi_irq_configure(acpi_psm_lnk_t *acpipsmlnkp, dev_info_t *dip,
1183446Smrj     int *pci_irqp, iflag_t *intr_flagp);
1193446Smrj 
12012683SJimmy.Vetayases@oracle.com int apic_acpi_translate_pci_irq(dev_info_t *dip, int busid, int devid,
1213446Smrj     int ipin, int *pci_irqp, iflag_t *intr_flagp);
12212683SJimmy.Vetayases@oracle.com uchar_t acpi_find_ioapic(int irq);
1233446Smrj static int acpi_intr_compatible(iflag_t iflag1, iflag_t iflag2);
1243446Smrj 
1253446Smrj /* Max wait time (in repetitions) for flags to clear in an RDT entry. */
1263446Smrj int apic_max_reps_clear_pending = 1000;
1273446Smrj 
1288925SEvan.Yan@Sun.COM int	apic_intr_policy = INTR_ROUND_ROBIN;
1293446Smrj 
1303446Smrj int	apic_next_bind_cpu = 1; /* For round robin assignment */
1313446Smrj 				/* start with cpu 1 */
1323446Smrj 
1333446Smrj /*
1343446Smrj  * If enabled, the distribution works as follows:
1353446Smrj  * On every interrupt entry, the current ipl for the CPU is set in cpu_info
1363446Smrj  * and the irq corresponding to the ipl is also set in the aci_current array.
1373446Smrj  * interrupt exit and setspl (due to soft interrupts) will cause the current
1383446Smrj  * ipl to be be changed. This is cache friendly as these frequently used
1393446Smrj  * paths write into a per cpu structure.
1403446Smrj  *
1413446Smrj  * Sampling is done by checking the structures for all CPUs and incrementing
1423446Smrj  * the busy field of the irq (if any) executing on each CPU and the busy field
1433446Smrj  * of the corresponding CPU.
1443446Smrj  * In periodic mode this is done on every clock interrupt.
1453446Smrj  * In one-shot mode, this is done thru a cyclic with an interval of
1463446Smrj  * apic_redistribute_sample_interval (default 10 milli sec).
1473446Smrj  *
1483446Smrj  * Every apic_sample_factor_redistribution times we sample, we do computations
1493446Smrj  * to decide which interrupt needs to be migrated (see comments
1503446Smrj  * before apic_intr_redistribute().
1513446Smrj  */
1523446Smrj 
1533446Smrj /*
1543446Smrj  * Following 3 variables start as % and can be patched or set using an
1553446Smrj  * API to be defined in future. They will be scaled to
1563446Smrj  * sample_factor_redistribution which is in turn set to hertz+1 (in periodic
1573446Smrj  * mode), or 101 in one-shot mode to stagger it away from one sec processing
1583446Smrj  */
1593446Smrj 
1603446Smrj int	apic_int_busy_mark = 60;
1613446Smrj int	apic_int_free_mark = 20;
1623446Smrj int	apic_diff_for_redistribution = 10;
1633446Smrj 
1643446Smrj /* sampling interval for interrupt redistribution for dynamic migration */
1653446Smrj int	apic_redistribute_sample_interval = NANOSEC / 100; /* 10 millisec */
1663446Smrj 
1673446Smrj /*
1683446Smrj  * number of times we sample before deciding to redistribute interrupts
1693446Smrj  * for dynamic migration
1703446Smrj  */
1713446Smrj int	apic_sample_factor_redistribution = 101;
1723446Smrj 
1733446Smrj int	apic_redist_cpu_skip = 0;
1743446Smrj int	apic_num_imbalance = 0;
1753446Smrj int	apic_num_rebind = 0;
1763446Smrj 
17712004Sjiang.liu@intel.com /*
17812004Sjiang.liu@intel.com  * Maximum number of APIC CPUs in the system, -1 indicates that dynamic
17912004Sjiang.liu@intel.com  * allocation of CPU ids is disabled.
18012004Sjiang.liu@intel.com  */
18112004Sjiang.liu@intel.com int 	apic_max_nproc = -1;
1823446Smrj int	apic_nproc = 0;
1833446Smrj size_t	apic_cpus_size = 0;
1843446Smrj int	apic_defconf = 0;
1853446Smrj int	apic_irq_translate = 0;
1863446Smrj int	apic_spec_rev = 0;
1873446Smrj int	apic_imcrp = 0;
1883446Smrj 
1893446Smrj int	apic_use_acpi = 1;	/* 1 = use ACPI, 0 = don't use ACPI */
1903446Smrj int	apic_use_acpi_madt_only = 0;	/* 1=ONLY use MADT from ACPI */
1913446Smrj 
1923446Smrj /*
1933446Smrj  * For interrupt link devices, if apic_unconditional_srs is set, an irq resource
1943446Smrj  * will be assigned (via _SRS). If it is not set, use the current
1953446Smrj  * irq setting (via _CRS), but only if that irq is in the set of possible
1963446Smrj  * irqs (returned by _PRS) for the device.
1973446Smrj  */
1983446Smrj int	apic_unconditional_srs = 1;
1993446Smrj 
2003446Smrj /*
2013446Smrj  * For interrupt link devices, if apic_prefer_crs is set when we are
2023446Smrj  * assigning an IRQ resource to a device, prefer the current IRQ setting
2033446Smrj  * over other possible irq settings under same conditions.
2043446Smrj  */
2053446Smrj 
2063446Smrj int	apic_prefer_crs = 1;
2073446Smrj 
20812683SJimmy.Vetayases@oracle.com uchar_t apic_io_id[MAX_IO_APIC];
2093446Smrj volatile uint32_t *apicioadr[MAX_IO_APIC];
21012683SJimmy.Vetayases@oracle.com uchar_t	apic_io_ver[MAX_IO_APIC];
21112683SJimmy.Vetayases@oracle.com uchar_t	apic_io_vectbase[MAX_IO_APIC];
21212683SJimmy.Vetayases@oracle.com uchar_t	apic_io_vectend[MAX_IO_APIC];
2133446Smrj uchar_t apic_reserved_irqlist[MAX_ISA_IRQ + 1];
2143446Smrj uint32_t apic_physaddr[MAX_IO_APIC];
2153446Smrj 
21612683SJimmy.Vetayases@oracle.com boolean_t ioapic_mask_workaround[MAX_IO_APIC];
2173745Ssethg 
2183446Smrj /*
2193446Smrj  * First available slot to be used as IRQ index into the apic_irq_table
2203446Smrj  * for those interrupts (like MSI/X) that don't have a physical IRQ.
2213446Smrj  */
2223446Smrj int apic_first_avail_irq  = APIC_FIRST_FREE_IRQ;
2233446Smrj 
2243446Smrj /*
2253446Smrj  * apic_ioapic_lock protects the ioapics (reg select), the status, temp_bound
2263446Smrj  * and bound elements of cpus_info and the temp_cpu element of irq_struct
2273446Smrj  */
2283446Smrj lock_t	apic_ioapic_lock;
2293446Smrj 
23012683SJimmy.Vetayases@oracle.com int	apic_io_max = 0;	/* no. of i/o apics enabled */
2313446Smrj 
23212683SJimmy.Vetayases@oracle.com struct apic_io_intr *apic_io_intrp = NULL;
2333446Smrj static	struct apic_bus	*apic_busp;
2343446Smrj 
2353446Smrj uchar_t	apic_resv_vector[MAXIPL+1];
2363446Smrj 
2373446Smrj char	apic_level_intr[APIC_MAX_VECTOR+1];
2383446Smrj 
23912683SJimmy.Vetayases@oracle.com uint32_t	eisa_level_intr_mask = 0;
2403446Smrj 	/* At least MSB will be set if EISA bus */
2413446Smrj 
24212683SJimmy.Vetayases@oracle.com int	apic_pci_bus_total = 0;
24312683SJimmy.Vetayases@oracle.com uchar_t	apic_single_pci_busid = 0;
2443446Smrj 
2453446Smrj /*
2463446Smrj  * airq_mutex protects additions to the apic_irq_table - the first
2473446Smrj  * pointer and any airq_nexts off of that one. It also protects
2483446Smrj  * apic_max_device_irq & apic_min_device_irq. It also guarantees
2493446Smrj  * that share_id is unique as new ids are generated only when new
2503446Smrj  * irq_t structs are linked in. Once linked in the structs are never
2513446Smrj  * deleted. temp_cpu & mps_intr_index field indicate if it is programmed
2523446Smrj  * or allocated. Note that there is a slight gap between allocating in
2533446Smrj  * apic_introp_xlate and programming in addspl.
2543446Smrj  */
2553446Smrj kmutex_t	airq_mutex;
2563446Smrj apic_irq_t	*apic_irq_table[APIC_MAX_VECTOR+1];
2573446Smrj int		apic_max_device_irq = 0;
2583446Smrj int		apic_min_device_irq = APIC_MAX_VECTOR;
2593446Smrj 
2603446Smrj typedef struct prs_irq_list_ent {
2613446Smrj 	int			list_prio;
2623446Smrj 	int32_t			irq;
2633446Smrj 	iflag_t			intrflags;
2643446Smrj 	acpi_prs_private_t	prsprv;
2653446Smrj 	struct prs_irq_list_ent	*next;
2663446Smrj } prs_irq_list_t;
2673446Smrj 
2683446Smrj 
2693446Smrj /*
2703446Smrj  * ACPI variables
2713446Smrj  */
2723446Smrj /* 1 = acpi is enabled & working, 0 = acpi is not enabled or not there */
2733446Smrj int apic_enable_acpi = 0;
2743446Smrj 
2753446Smrj /* ACPI Multiple APIC Description Table ptr */
2767851SDana.Myers@Sun.COM static	ACPI_TABLE_MADT *acpi_mapic_dtp = NULL;
2773446Smrj 
2783446Smrj /* ACPI Interrupt Source Override Structure ptr */
27912683SJimmy.Vetayases@oracle.com ACPI_MADT_INTERRUPT_OVERRIDE *acpi_isop = NULL;
28012683SJimmy.Vetayases@oracle.com int acpi_iso_cnt = 0;
2813446Smrj 
2823446Smrj /* ACPI Non-maskable Interrupt Sources ptr */
2837851SDana.Myers@Sun.COM static	ACPI_MADT_NMI_SOURCE *acpi_nmi_sp = NULL;
2843446Smrj static	int acpi_nmi_scnt = 0;
2857851SDana.Myers@Sun.COM static	ACPI_MADT_LOCAL_APIC_NMI *acpi_nmi_cp = NULL;
2863446Smrj static	int acpi_nmi_ccnt = 0;
2873446Smrj 
2883446Smrj /*
2893446Smrj  * The following added to identify a software poweroff method if available.
2903446Smrj  */
2913446Smrj 
2923446Smrj static struct {
2933446Smrj 	int	poweroff_method;
2943446Smrj 	char	oem_id[APIC_MPS_OEM_ID_LEN + 1];	/* MAX + 1 for NULL */
2953446Smrj 	char	prod_id[APIC_MPS_PROD_ID_LEN + 1];	/* MAX + 1 for NULL */
2963446Smrj } apic_mps_ids[] = {
2973446Smrj 	{ APIC_POWEROFF_VIA_RTC,	"INTEL",	"ALDER" },   /* 4300 */
2983446Smrj 	{ APIC_POWEROFF_VIA_RTC,	"NCR",		"AMC" },    /* 4300 */
2993446Smrj 	{ APIC_POWEROFF_VIA_ASPEN_BMC,	"INTEL",	"A450NX" },  /* 4400? */
3003446Smrj 	{ APIC_POWEROFF_VIA_ASPEN_BMC,	"INTEL",	"AD450NX" }, /* 4400 */
3013446Smrj 	{ APIC_POWEROFF_VIA_ASPEN_BMC,	"INTEL",	"AC450NX" }, /* 4400R */
3023446Smrj 	{ APIC_POWEROFF_VIA_SITKA_BMC,	"INTEL",	"S450NX" },  /* S50  */
3033446Smrj 	{ APIC_POWEROFF_VIA_SITKA_BMC,	"INTEL",	"SC450NX" }  /* S50? */
3043446Smrj };
3053446Smrj 
3063446Smrj int	apic_poweroff_method = APIC_POWEROFF_NONE;
3073446Smrj 
3083446Smrj /*
3093446Smrj  * Auto-configuration routines
3103446Smrj  */
3113446Smrj 
3123446Smrj /*
3133446Smrj  * Look at MPSpec 1.4 (Intel Order # 242016-005) for details of what we do here
3143446Smrj  * May work with 1.1 - but not guaranteed.
3153446Smrj  * According to the MP Spec, the MP floating pointer structure
3163446Smrj  * will be searched in the order described below:
3173446Smrj  * 1. In the first kilobyte of Extended BIOS Data Area (EBDA)
3183446Smrj  * 2. Within the last kilobyte of system base memory
3193446Smrj  * 3. In the BIOS ROM address space between 0F0000h and 0FFFFh
3203446Smrj  * Once we find the right signature with proper checksum, we call
3213446Smrj  * either handle_defconf or parse_mpct to get all info necessary for
3223446Smrj  * subsequent operations.
3233446Smrj  */
3243446Smrj int
apic_probe_common(char * modname)3253446Smrj apic_probe_common(char *modname)
3263446Smrj {
3273446Smrj 	uint32_t mpct_addr, ebda_start = 0, base_mem_end;
3283446Smrj 	caddr_t	biosdatap;
32912683SJimmy.Vetayases@oracle.com 	caddr_t	mpct = 0;
3303446Smrj 	caddr_t	fptr;
3313446Smrj 	int	i, mpct_size, mapsize, retval = PSM_FAILURE;
3323446Smrj 	ushort_t	ebda_seg, base_mem_size;
3333446Smrj 	struct	apic_mpfps_hdr	*fpsp;
3343446Smrj 	struct	apic_mp_cnf_hdr	*hdrp;
3353446Smrj 	int bypass_cpu_and_ioapics_in_mptables;
3363446Smrj 	int acpi_user_options;
3373446Smrj 
3383446Smrj 	if (apic_forceload < 0)
3393446Smrj 		return (retval);
3403446Smrj 
3413446Smrj 	/*
3423446Smrj 	 * Remember who we are
3433446Smrj 	 */
3443446Smrj 	psm_name = modname;
3453446Smrj 
3463446Smrj 	/* Allow override for MADT-only mode */
3473446Smrj 	acpi_user_options = ddi_prop_get_int(DDI_DEV_T_ANY, ddi_root_node(), 0,
3483446Smrj 	    "acpi-user-options", 0);
3493446Smrj 	apic_use_acpi_madt_only = ((acpi_user_options & ACPI_OUSER_MADT) != 0);
3503446Smrj 
3513446Smrj 	/* Allow apic_use_acpi to override MADT-only mode */
3523446Smrj 	if (!apic_use_acpi)
3533446Smrj 		apic_use_acpi_madt_only = 0;
3543446Smrj 
3553446Smrj 	retval = acpi_probe(modname);
3563446Smrj 
3573446Smrj 	/*
3583446Smrj 	 * mapin the bios data area 40:0
3593446Smrj 	 * 40:13h - two-byte location reports the base memory size
3603446Smrj 	 * 40:0Eh - two-byte location for the exact starting address of
3613446Smrj 	 *	    the EBDA segment for EISA
3623446Smrj 	 */
3633446Smrj 	biosdatap = psm_map_phys(0x400, 0x20, PROT_READ);
3643446Smrj 	if (!biosdatap)
36512683SJimmy.Vetayases@oracle.com 		goto apic_ret;
3663446Smrj 	fpsp = (struct apic_mpfps_hdr *)NULL;
3673446Smrj 	mapsize = MPFPS_RAM_WIN_LEN;
3683446Smrj 	/*LINTED: pointer cast may result in improper alignment */
3693446Smrj 	ebda_seg = *((ushort_t *)(biosdatap+0xe));
3703446Smrj 	/* check the 1k of EBDA */
3713446Smrj 	if (ebda_seg) {
3723446Smrj 		ebda_start = ((uint32_t)ebda_seg) << 4;
3733446Smrj 		fptr = psm_map_phys(ebda_start, MPFPS_RAM_WIN_LEN, PROT_READ);
3743446Smrj 		if (fptr) {
3753446Smrj 			if (!(fpsp =
3763446Smrj 			    apic_find_fps_sig(fptr, MPFPS_RAM_WIN_LEN)))
3773446Smrj 				psm_unmap_phys(fptr, MPFPS_RAM_WIN_LEN);
3783446Smrj 		}
3793446Smrj 	}
3803446Smrj 	/* If not in EBDA, check the last k of system base memory */
3813446Smrj 	if (!fpsp) {
3823446Smrj 		/*LINTED: pointer cast may result in improper alignment */
3833446Smrj 		base_mem_size = *((ushort_t *)(biosdatap + 0x13));
3843446Smrj 
3853446Smrj 		if (base_mem_size > 512)
3863446Smrj 			base_mem_end = 639 * 1024;
3873446Smrj 		else
3883446Smrj 			base_mem_end = 511 * 1024;
3893446Smrj 		/* if ebda == last k of base mem, skip to check BIOS ROM */
3903446Smrj 		if (base_mem_end != ebda_start) {
3913446Smrj 
3923446Smrj 			fptr = psm_map_phys(base_mem_end, MPFPS_RAM_WIN_LEN,
3933446Smrj 			    PROT_READ);
3943446Smrj 
3953446Smrj 			if (fptr) {
3963446Smrj 				if (!(fpsp = apic_find_fps_sig(fptr,
3973446Smrj 				    MPFPS_RAM_WIN_LEN)))
3983446Smrj 					psm_unmap_phys(fptr, MPFPS_RAM_WIN_LEN);
3993446Smrj 			}
4003446Smrj 		}
4013446Smrj 	}
4023446Smrj 	psm_unmap_phys(biosdatap, 0x20);
4033446Smrj 
4043446Smrj 	/* If still cannot find it, check the BIOS ROM space */
4053446Smrj 	if (!fpsp) {
4063446Smrj 		mapsize = MPFPS_ROM_WIN_LEN;
4073446Smrj 		fptr = psm_map_phys(MPFPS_ROM_WIN_START,
4083446Smrj 		    MPFPS_ROM_WIN_LEN, PROT_READ);
4093446Smrj 		if (fptr) {
4103446Smrj 			if (!(fpsp =
4113446Smrj 			    apic_find_fps_sig(fptr, MPFPS_ROM_WIN_LEN))) {
4123446Smrj 				psm_unmap_phys(fptr, MPFPS_ROM_WIN_LEN);
41312683SJimmy.Vetayases@oracle.com 				goto apic_ret;
4143446Smrj 			}
4153446Smrj 		}
4163446Smrj 	}
4173446Smrj 
4183446Smrj 	if (apic_checksum((caddr_t)fpsp, fpsp->mpfps_length * 16) != 0) {
4193446Smrj 		psm_unmap_phys(fptr, MPFPS_ROM_WIN_LEN);
42012683SJimmy.Vetayases@oracle.com 		goto apic_ret;
4213446Smrj 	}
4223446Smrj 
4233446Smrj 	apic_spec_rev = fpsp->mpfps_spec_rev;
4243446Smrj 	if ((apic_spec_rev != 04) && (apic_spec_rev != 01)) {
4253446Smrj 		psm_unmap_phys(fptr, MPFPS_ROM_WIN_LEN);
42612683SJimmy.Vetayases@oracle.com 		goto apic_ret;
4273446Smrj 	}
4283446Smrj 
4293446Smrj 	/* check IMCR is present or not */
4303446Smrj 	apic_imcrp = fpsp->mpfps_featinfo2 & MPFPS_FEATINFO2_IMCRP;
4313446Smrj 
4323446Smrj 	/* check default configuration (dual CPUs) */
4333446Smrj 	if ((apic_defconf = fpsp->mpfps_featinfo1) != 0) {
4343446Smrj 		psm_unmap_phys(fptr, mapsize);
43512683SJimmy.Vetayases@oracle.com 		if ((retval = apic_handle_defconf()) != PSM_SUCCESS)
43612683SJimmy.Vetayases@oracle.com 			return (retval);
43712683SJimmy.Vetayases@oracle.com 
43812683SJimmy.Vetayases@oracle.com 		goto apic_ret;
4393446Smrj 	}
4403446Smrj 
4413446Smrj 	/* MP Configuration Table */
4423446Smrj 	mpct_addr = (uint32_t)(fpsp->mpfps_mpct_paddr);
4433446Smrj 
4443446Smrj 	psm_unmap_phys(fptr, mapsize); /* unmap floating ptr struct */
4453446Smrj 
4463446Smrj 	/*
4473446Smrj 	 * Map in enough memory for the MP Configuration Table Header.
4483446Smrj 	 * Use this table to read the total length of the BIOS data and
4493446Smrj 	 * map in all the info
4503446Smrj 	 */
4513446Smrj 	/*LINTED: pointer cast may result in improper alignment */
4523446Smrj 	hdrp = (struct apic_mp_cnf_hdr *)psm_map_phys(mpct_addr,
4533446Smrj 	    sizeof (struct apic_mp_cnf_hdr), PROT_READ);
4543446Smrj 	if (!hdrp)
45512683SJimmy.Vetayases@oracle.com 		goto apic_ret;
4563446Smrj 
4573446Smrj 	/* check mp configuration table signature PCMP */
4583446Smrj 	if (hdrp->mpcnf_sig != 0x504d4350) {
4593446Smrj 		psm_unmap_phys((caddr_t)hdrp, sizeof (struct apic_mp_cnf_hdr));
46012683SJimmy.Vetayases@oracle.com 		goto apic_ret;
4613446Smrj 	}
4623446Smrj 	mpct_size = (int)hdrp->mpcnf_tbl_length;
4633446Smrj 
4643446Smrj 	apic_set_pwroff_method_from_mpcnfhdr(hdrp);
4653446Smrj 
4663446Smrj 	psm_unmap_phys((caddr_t)hdrp, sizeof (struct apic_mp_cnf_hdr));
4673446Smrj 
4683446Smrj 	if ((retval == PSM_SUCCESS) && !apic_use_acpi_madt_only) {
4693446Smrj 		/* This is an ACPI machine No need for further checks */
47012683SJimmy.Vetayases@oracle.com 		goto apic_ret;
4713446Smrj 	}
4723446Smrj 
4733446Smrj 	/*
4743446Smrj 	 * Map in the entries for this machine, ie. Processor
4753446Smrj 	 * Entry Tables, Bus Entry Tables, etc.
4763446Smrj 	 * They are in fixed order following one another
4773446Smrj 	 */
4783446Smrj 	mpct = psm_map_phys(mpct_addr, mpct_size, PROT_READ);
4793446Smrj 	if (!mpct)
48012683SJimmy.Vetayases@oracle.com 		goto apic_ret;
4813446Smrj 
4823446Smrj 	if (apic_checksum(mpct, mpct_size) != 0)
4833446Smrj 		goto apic_fail1;
4843446Smrj 
4853446Smrj 	/*LINTED: pointer cast may result in improper alignment */
4863446Smrj 	hdrp = (struct apic_mp_cnf_hdr *)mpct;
4873446Smrj 	apicadr = (uint32_t *)mapin_apic((uint32_t)hdrp->mpcnf_local_apic,
4883446Smrj 	    APIC_LOCAL_MEMLEN, PROT_READ | PROT_WRITE);
4893446Smrj 	if (!apicadr)
4903446Smrj 		goto apic_fail1;
4913446Smrj 
4923446Smrj 	/* Parse all information in the tables */
4933446Smrj 	bypass_cpu_and_ioapics_in_mptables = (retval == PSM_SUCCESS);
4943446Smrj 	if (apic_parse_mpct(mpct, bypass_cpu_and_ioapics_in_mptables) ==
49512683SJimmy.Vetayases@oracle.com 	    PSM_SUCCESS) {
49612683SJimmy.Vetayases@oracle.com 		retval = PSM_SUCCESS;
49712683SJimmy.Vetayases@oracle.com 		goto apic_ret;
49812683SJimmy.Vetayases@oracle.com 	}
49912683SJimmy.Vetayases@oracle.com 
50012683SJimmy.Vetayases@oracle.com apic_fail1:
50112683SJimmy.Vetayases@oracle.com 	psm_unmap_phys(mpct, mpct_size);
50212683SJimmy.Vetayases@oracle.com 	mpct = NULL;
50312683SJimmy.Vetayases@oracle.com 
50412683SJimmy.Vetayases@oracle.com apic_ret:
50512683SJimmy.Vetayases@oracle.com 	if (retval == PSM_SUCCESS) {
50612683SJimmy.Vetayases@oracle.com 		extern int apic_ioapic_method_probe();
50712683SJimmy.Vetayases@oracle.com 
50812683SJimmy.Vetayases@oracle.com 		if ((retval = apic_ioapic_method_probe()) == PSM_SUCCESS)
50912683SJimmy.Vetayases@oracle.com 			return (PSM_SUCCESS);
51012683SJimmy.Vetayases@oracle.com 	}
5113446Smrj 
5123446Smrj 	for (i = 0; i < apic_io_max; i++)
5133446Smrj 		mapout_ioapic((caddr_t)apicioadr[i], APIC_IO_MEMLEN);
51412683SJimmy.Vetayases@oracle.com 	if (apic_cpus) {
5153446Smrj 		kmem_free(apic_cpus, apic_cpus_size);
51612683SJimmy.Vetayases@oracle.com 		apic_cpus = NULL;
51712683SJimmy.Vetayases@oracle.com 	}
51812683SJimmy.Vetayases@oracle.com 	if (apicadr) {
5193446Smrj 		mapout_apic((caddr_t)apicadr, APIC_LOCAL_MEMLEN);
52012683SJimmy.Vetayases@oracle.com 		apicadr = NULL;
52112683SJimmy.Vetayases@oracle.com 	}
52212683SJimmy.Vetayases@oracle.com 	if (mpct)
52312683SJimmy.Vetayases@oracle.com 		psm_unmap_phys(mpct, mpct_size);
52412683SJimmy.Vetayases@oracle.com 
5253446Smrj 	return (retval);
5263446Smrj }
5273446Smrj 
5283446Smrj static void
apic_set_pwroff_method_from_mpcnfhdr(struct apic_mp_cnf_hdr * hdrp)5293446Smrj apic_set_pwroff_method_from_mpcnfhdr(struct apic_mp_cnf_hdr *hdrp)
5303446Smrj {
5313446Smrj 	int	i;
5323446Smrj 
5333446Smrj 	for (i = 0; i < (sizeof (apic_mps_ids) / sizeof (apic_mps_ids[0]));
5343446Smrj 	    i++) {
5353446Smrj 		if ((strncmp(hdrp->mpcnf_oem_str, apic_mps_ids[i].oem_id,
5363446Smrj 		    strlen(apic_mps_ids[i].oem_id)) == 0) &&
5373446Smrj 		    (strncmp(hdrp->mpcnf_prod_str, apic_mps_ids[i].prod_id,
5383446Smrj 		    strlen(apic_mps_ids[i].prod_id)) == 0)) {
5393446Smrj 
5403446Smrj 			apic_poweroff_method = apic_mps_ids[i].poweroff_method;
5413446Smrj 			break;
5423446Smrj 		}
5433446Smrj 	}
5443446Smrj 
5453446Smrj 	if (apic_debug_mps_id != 0) {
5463446Smrj 		cmn_err(CE_CONT, "%s: MPS OEM ID = '%c%c%c%c%c%c%c%c'"
5473446Smrj 		    "Product ID = '%c%c%c%c%c%c%c%c%c%c%c%c'\n",
5483446Smrj 		    psm_name,
5493446Smrj 		    hdrp->mpcnf_oem_str[0],
5503446Smrj 		    hdrp->mpcnf_oem_str[1],
5513446Smrj 		    hdrp->mpcnf_oem_str[2],
5523446Smrj 		    hdrp->mpcnf_oem_str[3],
5533446Smrj 		    hdrp->mpcnf_oem_str[4],
5543446Smrj 		    hdrp->mpcnf_oem_str[5],
5553446Smrj 		    hdrp->mpcnf_oem_str[6],
5563446Smrj 		    hdrp->mpcnf_oem_str[7],
5573446Smrj 		    hdrp->mpcnf_prod_str[0],
5583446Smrj 		    hdrp->mpcnf_prod_str[1],
5593446Smrj 		    hdrp->mpcnf_prod_str[2],
5603446Smrj 		    hdrp->mpcnf_prod_str[3],
5613446Smrj 		    hdrp->mpcnf_prod_str[4],
5623446Smrj 		    hdrp->mpcnf_prod_str[5],
5633446Smrj 		    hdrp->mpcnf_prod_str[6],
5643446Smrj 		    hdrp->mpcnf_prod_str[7],
5653446Smrj 		    hdrp->mpcnf_prod_str[8],
5663446Smrj 		    hdrp->mpcnf_prod_str[9],
5673446Smrj 		    hdrp->mpcnf_prod_str[10],
5683446Smrj 		    hdrp->mpcnf_prod_str[11]);
5693446Smrj 	}
5703446Smrj }
5713446Smrj 
57212004Sjiang.liu@intel.com static void
apic_free_apic_cpus(void)57312004Sjiang.liu@intel.com apic_free_apic_cpus(void)
57412004Sjiang.liu@intel.com {
57512004Sjiang.liu@intel.com 	if (apic_cpus != NULL) {
57612004Sjiang.liu@intel.com 		kmem_free(apic_cpus, apic_cpus_size);
57712004Sjiang.liu@intel.com 		apic_cpus = NULL;
57812004Sjiang.liu@intel.com 		apic_cpus_size = 0;
57912004Sjiang.liu@intel.com 	}
58012004Sjiang.liu@intel.com }
58112004Sjiang.liu@intel.com 
5823446Smrj static int
acpi_probe(char * modname)5833446Smrj acpi_probe(char *modname)
5843446Smrj {
5857515SSeth.Goldberg@Sun.COM 	int			i, intmax, index;
5863446Smrj 	uint32_t		id, ver;
5873446Smrj 	int			acpi_verboseflags = 0;
5883446Smrj 	int			madt_seen, madt_size;
5897851SDana.Myers@Sun.COM 	ACPI_SUBTABLE_HEADER		*ap;
5907851SDana.Myers@Sun.COM 	ACPI_MADT_LOCAL_APIC	*mpa;
5917851SDana.Myers@Sun.COM 	ACPI_MADT_LOCAL_X2APIC	*mpx2a;
5927851SDana.Myers@Sun.COM 	ACPI_MADT_IO_APIC		*mia;
5937851SDana.Myers@Sun.COM 	ACPI_MADT_IO_SAPIC		*misa;
5947851SDana.Myers@Sun.COM 	ACPI_MADT_INTERRUPT_OVERRIDE	*mio;
5957851SDana.Myers@Sun.COM 	ACPI_MADT_NMI_SOURCE		*mns;
5967851SDana.Myers@Sun.COM 	ACPI_MADT_INTERRUPT_SOURCE	*mis;
5977851SDana.Myers@Sun.COM 	ACPI_MADT_LOCAL_APIC_NMI	*mlan;
5987851SDana.Myers@Sun.COM 	ACPI_MADT_LOCAL_X2APIC_NMI	*mx2alan;
5997851SDana.Myers@Sun.COM 	ACPI_MADT_LOCAL_APIC_OVERRIDE	*mao;
6003446Smrj 	int			sci;
6013446Smrj 	iflag_t			sci_flags;
6023446Smrj 	volatile uint32_t	*ioapic;
6037515SSeth.Goldberg@Sun.COM 	int			ioapic_ix;
60412461SDana.Myers@Sun.COM 	uint32_t		*local_ids;
60512461SDana.Myers@Sun.COM 	uint32_t		*proc_ids;
6063446Smrj 	uchar_t			hid;
6079415SSeth.Goldberg@Sun.COM 	int			warned = 0;
6083446Smrj 
6093446Smrj 	if (!apic_use_acpi)
6103446Smrj 		return (PSM_FAILURE);
6113446Smrj 
6127851SDana.Myers@Sun.COM 	if (AcpiGetTable(ACPI_SIG_MADT, 1,
6133446Smrj 	    (ACPI_TABLE_HEADER **) &acpi_mapic_dtp) != AE_OK)
6143446Smrj 		return (PSM_FAILURE);
6153446Smrj 
6167851SDana.Myers@Sun.COM 	apicadr = mapin_apic((uint32_t)acpi_mapic_dtp->Address,
6173446Smrj 	    APIC_LOCAL_MEMLEN, PROT_READ | PROT_WRITE);
6183446Smrj 	if (!apicadr)
6193446Smrj 		return (PSM_FAILURE);
6203446Smrj 
62112461SDana.Myers@Sun.COM 	if ((local_ids = (uint32_t *)kmem_zalloc(NCPU * sizeof (uint32_t),
62212461SDana.Myers@Sun.COM 	    KM_NOSLEEP)) == NULL)
62312461SDana.Myers@Sun.COM 		return (PSM_FAILURE);
62412461SDana.Myers@Sun.COM 
62512461SDana.Myers@Sun.COM 	if ((proc_ids = (uint32_t *)kmem_zalloc(NCPU * sizeof (uint32_t),
62612461SDana.Myers@Sun.COM 	    KM_NOSLEEP)) == NULL) {
62712461SDana.Myers@Sun.COM 		kmem_free(local_ids, NCPU * sizeof (uint32_t));
62812461SDana.Myers@Sun.COM 		return (PSM_FAILURE);
62912461SDana.Myers@Sun.COM 	}
63012461SDana.Myers@Sun.COM 
6317282Smishra 	id = apic_reg_ops->apic_read(APIC_LID_REG);
6323446Smrj 	local_ids[0] = (uchar_t)(id >> 24);
6333446Smrj 	apic_nproc = index = 1;
6343446Smrj 	apic_io_max = 0;
6353446Smrj 
6367851SDana.Myers@Sun.COM 	ap = (ACPI_SUBTABLE_HEADER *) (acpi_mapic_dtp + 1);
6377851SDana.Myers@Sun.COM 	madt_size = acpi_mapic_dtp->Header.Length;
6383446Smrj 	madt_seen = sizeof (*acpi_mapic_dtp);
6393446Smrj 
6403446Smrj 	while (madt_seen < madt_size) {
6413446Smrj 		switch (ap->Type) {
6427851SDana.Myers@Sun.COM 		case ACPI_MADT_TYPE_LOCAL_APIC:
6437851SDana.Myers@Sun.COM 			mpa = (ACPI_MADT_LOCAL_APIC *) ap;
6447851SDana.Myers@Sun.COM 			if (mpa->LapicFlags & ACPI_MADT_ENABLED) {
6457851SDana.Myers@Sun.COM 				if (mpa->Id == local_ids[0]) {
64612004Sjiang.liu@intel.com 					ASSERT(index == 1);
6473446Smrj 					proc_ids[0] = mpa->ProcessorId;
6487515SSeth.Goldberg@Sun.COM 				} else if (apic_nproc < NCPU && use_mp &&
6497515SSeth.Goldberg@Sun.COM 				    apic_nproc < boot_ncpus) {
6507851SDana.Myers@Sun.COM 					local_ids[index] = mpa->Id;
6513446Smrj 					proc_ids[index] = mpa->ProcessorId;
6523446Smrj 					index++;
6533446Smrj 					apic_nproc++;
6549415SSeth.Goldberg@Sun.COM 				} else if (apic_nproc == NCPU && !warned) {
6559415SSeth.Goldberg@Sun.COM 					cmn_err(CE_WARN, "%s: CPU limit "
6569415SSeth.Goldberg@Sun.COM 					    "exceeded"
6579415SSeth.Goldberg@Sun.COM #if !defined(__amd64)
6589415SSeth.Goldberg@Sun.COM 					    " for 32-bit mode"
6599415SSeth.Goldberg@Sun.COM #endif
6609415SSeth.Goldberg@Sun.COM 					    "; Solaris will use %d CPUs.",
6613446Smrj 					    psm_name,  NCPU);
6629415SSeth.Goldberg@Sun.COM 					warned = 1;
6639415SSeth.Goldberg@Sun.COM 				}
6643446Smrj 			}
6653446Smrj 			break;
6663446Smrj 
6677851SDana.Myers@Sun.COM 		case ACPI_MADT_TYPE_IO_APIC:
6687851SDana.Myers@Sun.COM 			mia = (ACPI_MADT_IO_APIC *) ap;
6693446Smrj 			if (apic_io_max < MAX_IO_APIC) {
6707515SSeth.Goldberg@Sun.COM 				ioapic_ix = apic_io_max;
6717851SDana.Myers@Sun.COM 				apic_io_id[apic_io_max] = mia->Id;
6723446Smrj 				apic_io_vectbase[apic_io_max] =
6737851SDana.Myers@Sun.COM 				    mia->GlobalIrqBase;
6743446Smrj 				apic_physaddr[apic_io_max] =
6753446Smrj 				    (uint32_t)mia->Address;
6763446Smrj 				ioapic = apicioadr[apic_io_max] =
6773446Smrj 				    mapin_ioapic((uint32_t)mia->Address,
6783446Smrj 				    APIC_IO_MEMLEN, PROT_READ | PROT_WRITE);
6793446Smrj 				if (!ioapic)
6803446Smrj 					goto cleanup;
6813745Ssethg 				ioapic_mask_workaround[apic_io_max] =
6823745Ssethg 				    apic_is_ioapic_AMD_813x(mia->Address);
6833446Smrj 				apic_io_max++;
6843446Smrj 			}
6853446Smrj 			break;
6863446Smrj 
6877851SDana.Myers@Sun.COM 		case ACPI_MADT_TYPE_INTERRUPT_OVERRIDE:
6887851SDana.Myers@Sun.COM 			mio = (ACPI_MADT_INTERRUPT_OVERRIDE *) ap;
6893446Smrj 			if (acpi_isop == NULL)
6903446Smrj 				acpi_isop = mio;
6913446Smrj 			acpi_iso_cnt++;
6923446Smrj 			break;
6933446Smrj 
6947851SDana.Myers@Sun.COM 		case ACPI_MADT_TYPE_NMI_SOURCE:
6953446Smrj 			/* UNIMPLEMENTED */
6967851SDana.Myers@Sun.COM 			mns = (ACPI_MADT_NMI_SOURCE *) ap;
6973446Smrj 			if (acpi_nmi_sp == NULL)
6983446Smrj 				acpi_nmi_sp = mns;
6993446Smrj 			acpi_nmi_scnt++;
7003446Smrj 
7017851SDana.Myers@Sun.COM 			cmn_err(CE_NOTE, "!apic: nmi source: %d 0x%x\n",
7027851SDana.Myers@Sun.COM 			    mns->GlobalIrq, mns->IntiFlags);
7033446Smrj 			break;
7043446Smrj 
7057851SDana.Myers@Sun.COM 		case ACPI_MADT_TYPE_LOCAL_APIC_NMI:
7063446Smrj 			/* UNIMPLEMENTED */
7077851SDana.Myers@Sun.COM 			mlan = (ACPI_MADT_LOCAL_APIC_NMI *) ap;
7083446Smrj 			if (acpi_nmi_cp == NULL)
7093446Smrj 				acpi_nmi_cp = mlan;
7103446Smrj 			acpi_nmi_ccnt++;
7113446Smrj 
7127851SDana.Myers@Sun.COM 			cmn_err(CE_NOTE, "!apic: local nmi: %d 0x%x %d\n",
7137851SDana.Myers@Sun.COM 			    mlan->ProcessorId, mlan->IntiFlags,
7147851SDana.Myers@Sun.COM 			    mlan->Lint);
7153446Smrj 			break;
7163446Smrj 
7177851SDana.Myers@Sun.COM 		case ACPI_MADT_TYPE_LOCAL_APIC_OVERRIDE:
7183446Smrj 			/* UNIMPLEMENTED */
7197851SDana.Myers@Sun.COM 			mao = (ACPI_MADT_LOCAL_APIC_OVERRIDE *) ap;
7203446Smrj 			cmn_err(CE_NOTE, "!apic: address override: %lx\n",
7214355Smyers 			    (long)mao->Address);
7223446Smrj 			break;
7233446Smrj 
7247851SDana.Myers@Sun.COM 		case ACPI_MADT_TYPE_IO_SAPIC:
7253446Smrj 			/* UNIMPLEMENTED */
7267851SDana.Myers@Sun.COM 			misa = (ACPI_MADT_IO_SAPIC *) ap;
7273446Smrj 
7283446Smrj 			cmn_err(CE_NOTE, "!apic: io sapic: %d %d %lx\n",
7297851SDana.Myers@Sun.COM 			    misa->Id, misa->GlobalIrqBase,
7304355Smyers 			    (long)misa->Address);
7313446Smrj 			break;
7323446Smrj 
7337851SDana.Myers@Sun.COM 		case ACPI_MADT_TYPE_INTERRUPT_SOURCE:
7343446Smrj 			/* UNIMPLEMENTED */
7357851SDana.Myers@Sun.COM 			mis = (ACPI_MADT_INTERRUPT_SOURCE *) ap;
7363446Smrj 
7373446Smrj 			cmn_err(CE_NOTE,
7387851SDana.Myers@Sun.COM 			    "!apic: irq source: %d %d %d 0x%x %d %d\n",
7397851SDana.Myers@Sun.COM 			    mis->Id, mis->Eid, mis->GlobalIrq,
7407851SDana.Myers@Sun.COM 			    mis->IntiFlags, mis->Type,
7414355Smyers 			    mis->IoSapicVector);
7423446Smrj 			break;
7437282Smishra 
7447851SDana.Myers@Sun.COM 		case ACPI_MADT_TYPE_LOCAL_X2APIC:
7457851SDana.Myers@Sun.COM 			mpx2a = (ACPI_MADT_LOCAL_X2APIC *) ap;
7467282Smishra 
7477282Smishra 			/*
7487282Smishra 			 * All logical processors with APIC ID values
7497282Smishra 			 * of 255 and greater will have their APIC
7507282Smishra 			 * reported through Processor X2APIC structure.
7517282Smishra 			 * All logical processors with APIC ID less than
7527282Smishra 			 * 255 will have their APIC reported through
7537282Smishra 			 * Processor Local APIC.
7547282Smishra 			 */
7557851SDana.Myers@Sun.COM 			if ((mpx2a->LapicFlags & ACPI_MADT_ENABLED) &&
7567851SDana.Myers@Sun.COM 			    (mpx2a->LocalApicId >> 8)) {
7577515SSeth.Goldberg@Sun.COM 				if (apic_nproc < NCPU && use_mp &&
7587515SSeth.Goldberg@Sun.COM 				    apic_nproc < boot_ncpus) {
7597851SDana.Myers@Sun.COM 					local_ids[index] = mpx2a->LocalApicId;
76012004Sjiang.liu@intel.com 					proc_ids[index] = mpa->ProcessorId;
7617282Smishra 					index++;
7627282Smishra 					apic_nproc++;
7639415SSeth.Goldberg@Sun.COM 				} else if (apic_nproc == NCPU && !warned) {
7649415SSeth.Goldberg@Sun.COM 					cmn_err(CE_WARN, "%s: CPU limit "
7659415SSeth.Goldberg@Sun.COM 					    "exceeded"
7669415SSeth.Goldberg@Sun.COM #if !defined(__amd64)
7679415SSeth.Goldberg@Sun.COM 					    " for 32-bit mode"
7689415SSeth.Goldberg@Sun.COM #endif
7699415SSeth.Goldberg@Sun.COM 					    "; Solaris will use %d CPUs.",
7709415SSeth.Goldberg@Sun.COM 					    psm_name,  NCPU);
7719415SSeth.Goldberg@Sun.COM 					warned = 1;
7727282Smishra 				}
7737282Smishra 			}
7747282Smishra 
7757282Smishra 			break;
7767282Smishra 
7777851SDana.Myers@Sun.COM 		case ACPI_MADT_TYPE_LOCAL_X2APIC_NMI:
7787282Smishra 			/* UNIMPLEMENTED */
7797851SDana.Myers@Sun.COM 			mx2alan = (ACPI_MADT_LOCAL_X2APIC_NMI *) ap;
7807851SDana.Myers@Sun.COM 			if (mx2alan->Uid >> 8)
7817282Smishra 				acpi_nmi_ccnt++;
7827282Smishra 
7837282Smishra #ifdef	DEBUG
7847851SDana.Myers@Sun.COM 			cmn_err(CE_NOTE,
7857851SDana.Myers@Sun.COM 			    "!apic: local x2apic nmi: %d 0x%x %d\n",
7867851SDana.Myers@Sun.COM 			    mx2alan->Uid, mx2alan->IntiFlags, mx2alan->Lint);
7877282Smishra #endif
7887282Smishra 
7897282Smishra 			break;
7907282Smishra 
7917851SDana.Myers@Sun.COM 		case ACPI_MADT_TYPE_RESERVED:
7923446Smrj 		default:
7933446Smrj 			break;
7943446Smrj 		}
7953446Smrj 
7963446Smrj 		/* advance to next entry */
7973446Smrj 		madt_seen += ap->Length;
7987851SDana.Myers@Sun.COM 		ap = (ACPI_SUBTABLE_HEADER *)(((char *)ap) + ap->Length);
7993446Smrj 	}
8003446Smrj 
80112004Sjiang.liu@intel.com 	/*
80212004Sjiang.liu@intel.com 	 * allocate enough space for possible hot-adding of CPUs.
80312004Sjiang.liu@intel.com 	 * max_ncpus may be less than apic_nproc if it's set by user.
80412004Sjiang.liu@intel.com 	 */
80512004Sjiang.liu@intel.com 	if (plat_dr_support_cpu()) {
80612004Sjiang.liu@intel.com 		apic_max_nproc = max_ncpus;
80712004Sjiang.liu@intel.com 	}
80812004Sjiang.liu@intel.com 	apic_cpus_size = max(apic_nproc, max_ncpus) * sizeof (*apic_cpus);
8093446Smrj 	if ((apic_cpus = kmem_zalloc(apic_cpus_size, KM_NOSLEEP)) == NULL)
8103446Smrj 		goto cleanup;
8113446Smrj 
8123446Smrj 	/*
8133446Smrj 	 * ACPI doesn't provide the local apic ver, get it directly from the
8143446Smrj 	 * local apic
8153446Smrj 	 */
8167282Smishra 	ver = apic_reg_ops->apic_read(APIC_VERS_REG);
8173446Smrj 	for (i = 0; i < apic_nproc; i++) {
8183446Smrj 		apic_cpus[i].aci_local_id = local_ids[i];
8193446Smrj 		apic_cpus[i].aci_local_ver = (uchar_t)(ver & 0xFF);
82012004Sjiang.liu@intel.com 		apic_cpus[i].aci_processor_id = proc_ids[i];
82112004Sjiang.liu@intel.com 		/* Only build mapping info for CPUs present at boot. */
82212004Sjiang.liu@intel.com 		if (i < boot_ncpus)
82312004Sjiang.liu@intel.com 			(void) acpica_map_cpu(i, proc_ids[i]);
82412004Sjiang.liu@intel.com 	}
82512004Sjiang.liu@intel.com 
82612004Sjiang.liu@intel.com 	/*
82712004Sjiang.liu@intel.com 	 * To support CPU dynamic reconfiguration, the apic CPU info structure
82812004Sjiang.liu@intel.com 	 * for each possible CPU will be pre-allocated at boot time.
82912004Sjiang.liu@intel.com 	 * The state for each apic CPU info structure will be assigned according
83012004Sjiang.liu@intel.com 	 * to the following rules:
83112004Sjiang.liu@intel.com 	 * Rule 1:
83212004Sjiang.liu@intel.com 	 * 	Slot index range: [0, min(apic_nproc, boot_ncpus))
83312004Sjiang.liu@intel.com 	 *	State flags: 0
83412004Sjiang.liu@intel.com 	 *	Note: cpu exists and will be configured/enabled at boot time
83512004Sjiang.liu@intel.com 	 * Rule 2:
83612004Sjiang.liu@intel.com 	 * 	Slot index range: [boot_ncpus, apic_nproc)
83712004Sjiang.liu@intel.com 	 *	State flags: APIC_CPU_FREE | APIC_CPU_DIRTY
83812004Sjiang.liu@intel.com 	 *	Note: cpu exists but won't be configured/enabled at boot time
83912004Sjiang.liu@intel.com 	 * Rule 3:
84012004Sjiang.liu@intel.com 	 * 	Slot index range: [apic_nproc, boot_ncpus)
84112004Sjiang.liu@intel.com 	 *	State flags: APIC_CPU_FREE
84212004Sjiang.liu@intel.com 	 *	Note: cpu doesn't exist at boot time
84312004Sjiang.liu@intel.com 	 * Rule 4:
84412004Sjiang.liu@intel.com 	 * 	Slot index range: [max(apic_nproc, boot_ncpus), max_ncpus)
84512004Sjiang.liu@intel.com 	 *	State flags: APIC_CPU_FREE
84612004Sjiang.liu@intel.com 	 *	Note: cpu doesn't exist at boot time
84712004Sjiang.liu@intel.com 	 */
84812004Sjiang.liu@intel.com 	CPUSET_ZERO(apic_cpumask);
84912004Sjiang.liu@intel.com 	for (i = 0; i < min(boot_ncpus, apic_nproc); i++) {
85012004Sjiang.liu@intel.com 		CPUSET_ADD(apic_cpumask, i);
85112004Sjiang.liu@intel.com 		apic_cpus[i].aci_status = 0;
85212004Sjiang.liu@intel.com 	}
85312004Sjiang.liu@intel.com 	for (i = boot_ncpus; i < apic_nproc; i++) {
85412004Sjiang.liu@intel.com 		apic_cpus[i].aci_status = APIC_CPU_FREE | APIC_CPU_DIRTY;
85512004Sjiang.liu@intel.com 	}
85612004Sjiang.liu@intel.com 	for (i = apic_nproc; i < boot_ncpus; i++) {
85712004Sjiang.liu@intel.com 		apic_cpus[i].aci_status = APIC_CPU_FREE;
85812004Sjiang.liu@intel.com 	}
85912004Sjiang.liu@intel.com 	for (i = max(boot_ncpus, apic_nproc); i < max_ncpus; i++) {
86012004Sjiang.liu@intel.com 		apic_cpus[i].aci_status = APIC_CPU_FREE;
8613446Smrj 	}
8627282Smishra 
8633446Smrj 	for (i = 0; i < apic_io_max; i++) {
8647515SSeth.Goldberg@Sun.COM 		ioapic_ix = i;
8653446Smrj 
8663446Smrj 		/*
8673446Smrj 		 * need to check Sitka on the following acpi problem
8683446Smrj 		 * On the Sitka, the ioapic's apic_id field isn't reporting
8693446Smrj 		 * the actual io apic id. We have reported this problem
8703446Smrj 		 * to Intel. Until they fix the problem, we will get the
8713446Smrj 		 * actual id directly from the ioapic.
8723446Smrj 		 */
8737515SSeth.Goldberg@Sun.COM 		id = ioapic_read(ioapic_ix, APIC_ID_CMD);
8743446Smrj 		hid = (uchar_t)(id >> 24);
8753446Smrj 
8763446Smrj 		if (hid != apic_io_id[i]) {
8773446Smrj 			if (apic_io_id[i] == 0)
8783446Smrj 				apic_io_id[i] = hid;
8793446Smrj 			else { /* set ioapic id to whatever reported by ACPI */
8803446Smrj 				id = ((uint32_t)apic_io_id[i]) << 24;
8817515SSeth.Goldberg@Sun.COM 				ioapic_write(ioapic_ix, APIC_ID_CMD, id);
8823446Smrj 			}
8833446Smrj 		}
8847515SSeth.Goldberg@Sun.COM 		ver = ioapic_read(ioapic_ix, APIC_VERS_CMD);
8853446Smrj 		apic_io_ver[i] = (uchar_t)(ver & 0xff);
8863446Smrj 		intmax = (ver >> 16) & 0xff;
8873446Smrj 		apic_io_vectend[i] = apic_io_vectbase[i] + intmax;
8883446Smrj 		if (apic_first_avail_irq <= apic_io_vectend[i])
8893446Smrj 			apic_first_avail_irq = apic_io_vectend[i] + 1;
8903446Smrj 	}
8913446Smrj 
8923446Smrj 
8933446Smrj 	/*
8943446Smrj 	 * Process SCI configuration here
8953446Smrj 	 * An error may be returned here if
8963446Smrj 	 * acpi-user-options specifies legacy mode
8973446Smrj 	 * (no SCI, no ACPI mode)
8983446Smrj 	 */
8993446Smrj 	if (acpica_get_sci(&sci, &sci_flags) != AE_OK)
9003446Smrj 		sci = -1;
9013446Smrj 
9023446Smrj 	/*
9033446Smrj 	 * Now call acpi_init() to generate namespaces
9043446Smrj 	 * If this fails, we don't attempt to use ACPI
9053446Smrj 	 * even if we were able to get a MADT above
9063446Smrj 	 */
9073446Smrj 	if (acpica_init() != AE_OK)
9083446Smrj 		goto cleanup;
9093446Smrj 
9103446Smrj 	/*
9114667Smh27603 	 * Call acpica_build_processor_map() now that we have
9124667Smh27603 	 * ACPI namesspace access
9134667Smh27603 	 */
91411387SSurya.Prakki@Sun.COM 	(void) acpica_build_processor_map();
9154667Smh27603 
9164667Smh27603 	/*
9173446Smrj 	 * Squirrel away the SCI and flags for later on
9183446Smrj 	 * in apic_picinit() when we're ready
9193446Smrj 	 */
9203446Smrj 	apic_sci_vect = sci;
9213446Smrj 	apic_sci_flags = sci_flags;
9223446Smrj 
9233446Smrj 	if (apic_verbose & APIC_VERBOSE_IRQ_FLAG)
9243446Smrj 		acpi_verboseflags |= PSM_VERBOSE_IRQ_FLAG;
9253446Smrj 
9263446Smrj 	if (apic_verbose & APIC_VERBOSE_POWEROFF_FLAG)
9273446Smrj 		acpi_verboseflags |= PSM_VERBOSE_POWEROFF_FLAG;
9283446Smrj 
9293446Smrj 	if (apic_verbose & APIC_VERBOSE_POWEROFF_PAUSE_FLAG)
9303446Smrj 		acpi_verboseflags |= PSM_VERBOSE_POWEROFF_PAUSE_FLAG;
9313446Smrj 
9323446Smrj 	if (acpi_psm_init(modname, acpi_verboseflags) == ACPI_PSM_FAILURE)
9333446Smrj 		goto cleanup;
9343446Smrj 
9353446Smrj 	/* Enable ACPI APIC interrupt routing */
9367515SSeth.Goldberg@Sun.COM 	if (apic_acpi_enter_apicmode() != PSM_FAILURE) {
9373446Smrj 		build_reserved_irqlist((uchar_t *)apic_reserved_irqlist);
9383446Smrj 		apic_enable_acpi = 1;
9399652SMichael.Corcoran@Sun.COM 		if (apic_sci_vect > 0) {
9409652SMichael.Corcoran@Sun.COM 			acpica_set_core_feature(ACPI_FEATURE_SCI_EVENT);
9419652SMichael.Corcoran@Sun.COM 		}
9423446Smrj 		if (apic_use_acpi_madt_only) {
9433446Smrj 			cmn_err(CE_CONT,
9443446Smrj 			    "?Using ACPI for CPU/IOAPIC information ONLY\n");
9453446Smrj 		}
9468906SEric.Saxe@Sun.COM 
9478906SEric.Saxe@Sun.COM #if !defined(__xpv)
9488906SEric.Saxe@Sun.COM 		/*
9498906SEric.Saxe@Sun.COM 		 * probe ACPI for hpet information here which is used later
9508906SEric.Saxe@Sun.COM 		 * in apic_picinit().
9518906SEric.Saxe@Sun.COM 		 */
9528906SEric.Saxe@Sun.COM 		if (hpet_acpi_init(&apic_hpet_vect, &apic_hpet_flags) < 0) {
9538906SEric.Saxe@Sun.COM 			cmn_err(CE_NOTE, "!ACPI HPET table query failed\n");
9548906SEric.Saxe@Sun.COM 		}
9558906SEric.Saxe@Sun.COM #endif
9568906SEric.Saxe@Sun.COM 
95712461SDana.Myers@Sun.COM 		kmem_free(local_ids, NCPU * sizeof (uint32_t));
95812461SDana.Myers@Sun.COM 		kmem_free(proc_ids, NCPU * sizeof (uint32_t));
9593446Smrj 		return (PSM_SUCCESS);
9603446Smrj 	}
9613446Smrj 	/* if setting APIC mode failed above, we fall through to cleanup */
9623446Smrj 
9633446Smrj cleanup:
96412004Sjiang.liu@intel.com 	apic_free_apic_cpus();
9653446Smrj 	if (apicadr != NULL) {
9663446Smrj 		mapout_apic((caddr_t)apicadr, APIC_LOCAL_MEMLEN);
9673446Smrj 		apicadr = NULL;
9683446Smrj 	}
96912004Sjiang.liu@intel.com 	apic_max_nproc = -1;
9703446Smrj 	apic_nproc = 0;
9713446Smrj 	for (i = 0; i < apic_io_max; i++) {
9723446Smrj 		mapout_ioapic((caddr_t)apicioadr[i], APIC_IO_MEMLEN);
9733446Smrj 		apicioadr[i] = NULL;
9743446Smrj 	}
9753446Smrj 	apic_io_max = 0;
9763446Smrj 	acpi_isop = NULL;
9773446Smrj 	acpi_iso_cnt = 0;
9783446Smrj 	acpi_nmi_sp = NULL;
9793446Smrj 	acpi_nmi_scnt = 0;
9803446Smrj 	acpi_nmi_cp = NULL;
9813446Smrj 	acpi_nmi_ccnt = 0;
98212461SDana.Myers@Sun.COM 	kmem_free(local_ids, NCPU * sizeof (uint32_t));
98312461SDana.Myers@Sun.COM 	kmem_free(proc_ids, NCPU * sizeof (uint32_t));
9843446Smrj 	return (PSM_FAILURE);
9853446Smrj }
9863446Smrj 
9873446Smrj /*
9883446Smrj  * Handle default configuration. Fill in reqd global variables & tables
9893446Smrj  * Fill all details as MP table does not give any more info
9903446Smrj  */
9913446Smrj static int
apic_handle_defconf()9923446Smrj apic_handle_defconf()
9933446Smrj {
9943446Smrj 	uint_t	lid;
9953446Smrj 
99612004Sjiang.liu@intel.com 	/* Failed to probe ACPI MADT tables, disable CPU DR. */
99712004Sjiang.liu@intel.com 	apic_max_nproc = -1;
99812004Sjiang.liu@intel.com 	apic_free_apic_cpus();
99912004Sjiang.liu@intel.com 	plat_dr_disable_cpu();
100012004Sjiang.liu@intel.com 
100112683SJimmy.Vetayases@oracle.com 	apicioadr[0] = (void *)mapin_ioapic(APIC_IO_ADDR,
10023446Smrj 	    APIC_IO_MEMLEN, PROT_READ | PROT_WRITE);
100312683SJimmy.Vetayases@oracle.com 	apicadr = (void *)psm_map_phys(APIC_LOCAL_ADDR,
10043446Smrj 	    APIC_LOCAL_MEMLEN, PROT_READ);
10053446Smrj 	apic_cpus_size = 2 * sizeof (*apic_cpus);
10063446Smrj 	apic_cpus = (apic_cpus_info_t *)
10073446Smrj 	    kmem_zalloc(apic_cpus_size, KM_NOSLEEP);
10083446Smrj 	if ((!apicadr) || (!apicioadr[0]) || (!apic_cpus))
10093446Smrj 		goto apic_handle_defconf_fail;
10103446Smrj 	CPUSET_ONLY(apic_cpumask, 0);
10113446Smrj 	CPUSET_ADD(apic_cpumask, 1);
10123446Smrj 	apic_nproc = 2;
10137282Smishra 	lid = apic_reg_ops->apic_read(APIC_LID_REG);
10143446Smrj 	apic_cpus[0].aci_local_id = (uchar_t)(lid >> APIC_ID_BIT_OFFSET);
10153446Smrj 	/*
10163446Smrj 	 * According to the PC+MP spec 1.1, the local ids
10173446Smrj 	 * for the default configuration has to be 0 or 1
10183446Smrj 	 */
10193446Smrj 	if (apic_cpus[0].aci_local_id == 1)
10203446Smrj 		apic_cpus[1].aci_local_id = 0;
10213446Smrj 	else if (apic_cpus[0].aci_local_id == 0)
10223446Smrj 		apic_cpus[1].aci_local_id = 1;
10233446Smrj 	else
10243446Smrj 		goto apic_handle_defconf_fail;
10253446Smrj 
10263446Smrj 	apic_io_id[0] = 2;
10273446Smrj 	apic_io_max = 1;
10283446Smrj 	if (apic_defconf >= 5) {
10293446Smrj 		apic_cpus[0].aci_local_ver = APIC_INTEGRATED_VERS;
10303446Smrj 		apic_cpus[1].aci_local_ver = APIC_INTEGRATED_VERS;
10313446Smrj 		apic_io_ver[0] = APIC_INTEGRATED_VERS;
10323446Smrj 	} else {
10333446Smrj 		apic_cpus[0].aci_local_ver = 0;		/* 82489 DX */
10343446Smrj 		apic_cpus[1].aci_local_ver = 0;
10353446Smrj 		apic_io_ver[0] = 0;
10363446Smrj 	}
10373446Smrj 	if (apic_defconf == 2 || apic_defconf == 3 || apic_defconf == 6)
10383446Smrj 		eisa_level_intr_mask = (inb(EISA_LEVEL_CNTL + 1) << 8) |
10393446Smrj 		    inb(EISA_LEVEL_CNTL) | ((uint_t)INT32_MAX + 1);
10403446Smrj 	return (PSM_SUCCESS);
10413446Smrj 
10423446Smrj apic_handle_defconf_fail:
10433446Smrj 	if (apicadr)
10443446Smrj 		mapout_apic((caddr_t)apicadr, APIC_LOCAL_MEMLEN);
10453446Smrj 	if (apicioadr[0])
10463446Smrj 		mapout_ioapic((caddr_t)apicioadr[0], APIC_IO_MEMLEN);
10473446Smrj 	return (PSM_FAILURE);
10483446Smrj }
10493446Smrj 
10503446Smrj /* Parse the entries in MP configuration table and collect info that we need */
10513446Smrj static int
apic_parse_mpct(caddr_t mpct,int bypass_cpus_and_ioapics)10523446Smrj apic_parse_mpct(caddr_t mpct, int bypass_cpus_and_ioapics)
10533446Smrj {
10543446Smrj 	struct	apic_procent	*procp;
10553446Smrj 	struct	apic_bus	*busp;
10563446Smrj 	struct	apic_io_entry	*ioapicp;
10573446Smrj 	struct	apic_io_intr	*intrp;
10587515SSeth.Goldberg@Sun.COM 	int			ioapic_ix;
10593446Smrj 	uint_t	lid;
10603446Smrj 	uint32_t	id;
10613446Smrj 	uchar_t hid;
10627515SSeth.Goldberg@Sun.COM 	int	warned = 0;
10633446Smrj 
10643446Smrj 	/*LINTED: pointer cast may result in improper alignment */
10653446Smrj 	procp = (struct apic_procent *)(mpct + sizeof (struct apic_mp_cnf_hdr));
10663446Smrj 
10673446Smrj 	/* No need to count cpu entries if we won't use them */
10683446Smrj 	if (!bypass_cpus_and_ioapics) {
10693446Smrj 
10703446Smrj 		/* Find max # of CPUS and allocate structure accordingly */
10713446Smrj 		apic_nproc = 0;
10723446Smrj 		CPUSET_ZERO(apic_cpumask);
10733446Smrj 		while (procp->proc_entry == APIC_CPU_ENTRY) {
10743446Smrj 			if (procp->proc_cpuflags & CPUFLAGS_EN) {
10757515SSeth.Goldberg@Sun.COM 				if (apic_nproc < NCPU && use_mp &&
10767515SSeth.Goldberg@Sun.COM 				    apic_nproc < boot_ncpus) {
10773446Smrj 					CPUSET_ADD(apic_cpumask, apic_nproc);
10787515SSeth.Goldberg@Sun.COM 					apic_nproc++;
10797515SSeth.Goldberg@Sun.COM 				} else if (apic_nproc == NCPU && !warned) {
10809415SSeth.Goldberg@Sun.COM 					cmn_err(CE_WARN, "%s: CPU limit "
10819415SSeth.Goldberg@Sun.COM 					    "exceeded"
10829415SSeth.Goldberg@Sun.COM #if !defined(__amd64)
10839415SSeth.Goldberg@Sun.COM 					    " for 32-bit mode"
10849415SSeth.Goldberg@Sun.COM #endif
10859415SSeth.Goldberg@Sun.COM 					    "; Solaris will use %d CPUs.",
10869415SSeth.Goldberg@Sun.COM 					    psm_name,  NCPU);
10877515SSeth.Goldberg@Sun.COM 					warned = 1;
10887515SSeth.Goldberg@Sun.COM 				}
10897515SSeth.Goldberg@Sun.COM 
10903446Smrj 			}
10913446Smrj 			procp++;
10923446Smrj 		}
10933446Smrj 		apic_cpus_size = apic_nproc * sizeof (*apic_cpus);
10943446Smrj 		if (!apic_nproc || !(apic_cpus = (apic_cpus_info_t *)
10953446Smrj 		    kmem_zalloc(apic_cpus_size, KM_NOSLEEP)))
10963446Smrj 			return (PSM_FAILURE);
10973446Smrj 	}
10983446Smrj 
10993446Smrj 	/*LINTED: pointer cast may result in improper alignment */
11003446Smrj 	procp = (struct apic_procent *)(mpct + sizeof (struct apic_mp_cnf_hdr));
11013446Smrj 
11023446Smrj 	/*
11033446Smrj 	 * start with index 1 as 0 needs to be filled in with Boot CPU, but
11043446Smrj 	 * if we're bypassing this information, it has already been filled
11053446Smrj 	 * in by acpi_probe(), so don't overwrite it.
11063446Smrj 	 */
11073446Smrj 	if (!bypass_cpus_and_ioapics)
11083446Smrj 		apic_nproc = 1;
11093446Smrj 
11103446Smrj 	while (procp->proc_entry == APIC_CPU_ENTRY) {
11113446Smrj 		/* check whether the cpu exists or not */
11123446Smrj 		if (!bypass_cpus_and_ioapics &&
11133446Smrj 		    procp->proc_cpuflags & CPUFLAGS_EN) {
11143446Smrj 			if (procp->proc_cpuflags & CPUFLAGS_BP) { /* Boot CPU */
11157282Smishra 				lid = apic_reg_ops->apic_read(APIC_LID_REG);
11163446Smrj 				apic_cpus[0].aci_local_id = procp->proc_apicid;
11173446Smrj 				if (apic_cpus[0].aci_local_id !=
11183446Smrj 				    (uchar_t)(lid >> APIC_ID_BIT_OFFSET)) {
11193446Smrj 					return (PSM_FAILURE);
11203446Smrj 				}
11213446Smrj 				apic_cpus[0].aci_local_ver =
11223446Smrj 				    procp->proc_version;
11237515SSeth.Goldberg@Sun.COM 			} else if (apic_nproc < NCPU && use_mp &&
11247515SSeth.Goldberg@Sun.COM 			    apic_nproc < boot_ncpus) {
11253446Smrj 				apic_cpus[apic_nproc].aci_local_id =
11263446Smrj 				    procp->proc_apicid;
11277282Smishra 
11283446Smrj 				apic_cpus[apic_nproc].aci_local_ver =
11293446Smrj 				    procp->proc_version;
11303446Smrj 				apic_nproc++;
11313446Smrj 
11323446Smrj 			}
11333446Smrj 		}
11343446Smrj 		procp++;
11353446Smrj 	}
11363446Smrj 
11373446Smrj 	/*
11383446Smrj 	 * Save start of bus entries for later use.
11393446Smrj 	 * Get EISA level cntrl if EISA bus is present.
11403446Smrj 	 * Also get the CPI bus id for single CPI bus case
11413446Smrj 	 */
11423446Smrj 	apic_busp = busp = (struct apic_bus *)procp;
11433446Smrj 	while (busp->bus_entry == APIC_BUS_ENTRY) {
11443446Smrj 		lid = apic_find_bus_type((char *)&busp->bus_str1);
11453446Smrj 		if (lid	== BUS_EISA) {
11463446Smrj 			eisa_level_intr_mask = (inb(EISA_LEVEL_CNTL + 1) << 8) |
11473446Smrj 			    inb(EISA_LEVEL_CNTL) | ((uint_t)INT32_MAX + 1);
11483446Smrj 		} else if (lid == BUS_PCI) {
11493446Smrj 			/*
11503446Smrj 			 * apic_single_pci_busid will be used only if
11513446Smrj 			 * apic_pic_bus_total is equal to 1
11523446Smrj 			 */
11533446Smrj 			apic_pci_bus_total++;
11543446Smrj 			apic_single_pci_busid = busp->bus_id;
11553446Smrj 		}
11563446Smrj 		busp++;
11573446Smrj 	}
11583446Smrj 
11593446Smrj 	ioapicp = (struct apic_io_entry *)busp;
11603446Smrj 
11613446Smrj 	if (!bypass_cpus_and_ioapics)
11623446Smrj 		apic_io_max = 0;
11633446Smrj 	do {
11643446Smrj 		if (!bypass_cpus_and_ioapics && apic_io_max < MAX_IO_APIC) {
11653446Smrj 			if (ioapicp->io_flags & IOAPIC_FLAGS_EN) {
11663446Smrj 				apic_io_id[apic_io_max] = ioapicp->io_apicid;
11673446Smrj 				apic_io_ver[apic_io_max] = ioapicp->io_version;
11683446Smrj 				apicioadr[apic_io_max] =
116912683SJimmy.Vetayases@oracle.com 				    (void *)mapin_ioapic(
11703446Smrj 				    (uint32_t)ioapicp->io_apic_addr,
11713446Smrj 				    APIC_IO_MEMLEN, PROT_READ | PROT_WRITE);
11723446Smrj 
11733446Smrj 				if (!apicioadr[apic_io_max])
11743446Smrj 					return (PSM_FAILURE);
11753446Smrj 
11763745Ssethg 				ioapic_mask_workaround[apic_io_max] =
11773745Ssethg 				    apic_is_ioapic_AMD_813x(
11784355Smyers 				    ioapicp->io_apic_addr);
11793745Ssethg 
11807515SSeth.Goldberg@Sun.COM 				ioapic_ix = apic_io_max;
11817515SSeth.Goldberg@Sun.COM 				id = ioapic_read(ioapic_ix, APIC_ID_CMD);
11823446Smrj 				hid = (uchar_t)(id >> 24);
11833446Smrj 
11843446Smrj 				if (hid != apic_io_id[apic_io_max]) {
11853446Smrj 					if (apic_io_id[apic_io_max] == 0)
11863446Smrj 						apic_io_id[apic_io_max] = hid;
11873446Smrj 					else {
11883446Smrj 						/*
11893446Smrj 						 * set ioapic id to whatever
11903446Smrj 						 * reported by MPS
11913446Smrj 						 *
11923446Smrj 						 * may not need to set index
11933446Smrj 						 * again ???
11943446Smrj 						 * take it out and try
11953446Smrj 						 */
11963446Smrj 
11973446Smrj 						id = ((uint32_t)
11983446Smrj 						    apic_io_id[apic_io_max]) <<
11993446Smrj 						    24;
12003446Smrj 
12017515SSeth.Goldberg@Sun.COM 						ioapic_write(ioapic_ix,
12023446Smrj 						    APIC_ID_CMD, id);
12033446Smrj 					}
12043446Smrj 				}
12053446Smrj 				apic_io_max++;
12063446Smrj 			}
12073446Smrj 		}
12083446Smrj 		ioapicp++;
12093446Smrj 	} while (ioapicp->io_entry == APIC_IO_ENTRY);
12103446Smrj 
12113446Smrj 	apic_io_intrp = (struct apic_io_intr *)ioapicp;
12123446Smrj 
12133446Smrj 	intrp = apic_io_intrp;
12143446Smrj 	while (intrp->intr_entry == APIC_IO_INTR_ENTRY) {
12153446Smrj 		if ((intrp->intr_irq > APIC_MAX_ISA_IRQ) ||
12163446Smrj 		    (apic_find_bus(intrp->intr_busid) == BUS_PCI)) {
12173446Smrj 			apic_irq_translate = 1;
12183446Smrj 			break;
12193446Smrj 		}
12203446Smrj 		intrp++;
12213446Smrj 	}
12223446Smrj 
12233446Smrj 	return (PSM_SUCCESS);
12243446Smrj }
12253446Smrj 
12263446Smrj boolean_t
apic_cpu_in_range(int cpu)12273446Smrj apic_cpu_in_range(int cpu)
12283446Smrj {
122912004Sjiang.liu@intel.com 	cpu &= ~IRQ_USER_BOUND;
123012004Sjiang.liu@intel.com 	/* Check whether cpu id is in valid range. */
123112004Sjiang.liu@intel.com 	if (cpu < 0 || cpu >= apic_nproc) {
123212004Sjiang.liu@intel.com 		return (B_FALSE);
123312004Sjiang.liu@intel.com 	} else if (apic_max_nproc != -1 && cpu >= apic_max_nproc) {
123412004Sjiang.liu@intel.com 		/*
123512004Sjiang.liu@intel.com 		 * Check whether cpuid is in valid range if CPU DR is enabled.
123612004Sjiang.liu@intel.com 		 */
123712004Sjiang.liu@intel.com 		return (B_FALSE);
123812004Sjiang.liu@intel.com 	} else if (!CPU_IN_SET(apic_cpumask, cpu)) {
123912004Sjiang.liu@intel.com 		return (B_FALSE);
124012004Sjiang.liu@intel.com 	}
124112004Sjiang.liu@intel.com 
124212004Sjiang.liu@intel.com 	return (B_TRUE);
124312004Sjiang.liu@intel.com }
124412004Sjiang.liu@intel.com 
124512004Sjiang.liu@intel.com processorid_t
apic_get_next_bind_cpu(void)124612204Sjiang.liu@intel.com apic_get_next_bind_cpu(void)
124712004Sjiang.liu@intel.com {
124812004Sjiang.liu@intel.com 	int i, count;
124912004Sjiang.liu@intel.com 	processorid_t cpuid = 0;
125012004Sjiang.liu@intel.com 
125112004Sjiang.liu@intel.com 	for (count = 0; count < apic_nproc; count++) {
125212004Sjiang.liu@intel.com 		if (apic_next_bind_cpu >= apic_nproc) {
125312004Sjiang.liu@intel.com 			apic_next_bind_cpu = 0;
125412004Sjiang.liu@intel.com 		}
125512004Sjiang.liu@intel.com 		i = apic_next_bind_cpu++;
125612204Sjiang.liu@intel.com 		if (apic_cpu_in_range(i)) {
125712004Sjiang.liu@intel.com 			cpuid = i;
125812004Sjiang.liu@intel.com 			break;
125912004Sjiang.liu@intel.com 		}
126012004Sjiang.liu@intel.com 	}
126112004Sjiang.liu@intel.com 
126212004Sjiang.liu@intel.com 	return (cpuid);
12633446Smrj }
12643446Smrj 
12654397Sschwartz uint16_t
apic_get_apic_version()12664397Sschwartz apic_get_apic_version()
12674397Sschwartz {
12684397Sschwartz 	int i;
12694397Sschwartz 	uchar_t min_io_apic_ver = 0;
12704397Sschwartz 	static uint16_t version;		/* Cache as value is constant */
12714397Sschwartz 	static boolean_t found = B_FALSE;	/* Accomodate zero version */
12724397Sschwartz 
12734397Sschwartz 	if (found == B_FALSE) {
12744397Sschwartz 		found = B_TRUE;
12754397Sschwartz 
12764397Sschwartz 		/*
12774397Sschwartz 		 * Don't assume all IO APICs in the system are the same.
12784397Sschwartz 		 *
12794397Sschwartz 		 * Set to the minimum version.
12804397Sschwartz 		 */
12814397Sschwartz 		for (i = 0; i < apic_io_max; i++) {
12824397Sschwartz 			if ((apic_io_ver[i] != 0) &&
12834397Sschwartz 			    ((min_io_apic_ver == 0) ||
12844397Sschwartz 			    (min_io_apic_ver >= apic_io_ver[i])))
12854397Sschwartz 				min_io_apic_ver = apic_io_ver[i];
12864397Sschwartz 		}
12874397Sschwartz 
12884397Sschwartz 		/* Assume all local APICs are of the same version. */
12894397Sschwartz 		version = (min_io_apic_ver << 8) | apic_cpus[0].aci_local_ver;
12904397Sschwartz 	}
12914397Sschwartz 	return (version);
12924397Sschwartz }
12934397Sschwartz 
12943446Smrj static struct apic_mpfps_hdr *
apic_find_fps_sig(caddr_t cptr,int len)12953446Smrj apic_find_fps_sig(caddr_t cptr, int len)
12963446Smrj {
12973446Smrj 	int	i;
12983446Smrj 
12993446Smrj 	/* Look for the pattern "_MP_" */
13003446Smrj 	for (i = 0; i < len; i += 16) {
13013446Smrj 		if ((*(cptr+i) == '_') &&
13023446Smrj 		    (*(cptr+i+1) == 'M') &&
13033446Smrj 		    (*(cptr+i+2) == 'P') &&
13043446Smrj 		    (*(cptr+i+3) == '_'))
13053446Smrj 		    /*LINTED: pointer cast may result in improper alignment */
13063446Smrj 			return ((struct apic_mpfps_hdr *)(cptr + i));
13073446Smrj 	}
13083446Smrj 	return (NULL);
13093446Smrj }
13103446Smrj 
13113446Smrj static int
apic_checksum(caddr_t bptr,int len)13123446Smrj apic_checksum(caddr_t bptr, int len)
13133446Smrj {
13143446Smrj 	int	i;
13153446Smrj 	uchar_t	cksum;
13163446Smrj 
13173446Smrj 	cksum = 0;
13183446Smrj 	for (i = 0; i < len; i++)
13193446Smrj 		cksum += *bptr++;
13203446Smrj 	return ((int)cksum);
13213446Smrj }
13223446Smrj 
13233446Smrj /*
13243446Smrj  * On machines with PCI-PCI bridges, a device behind a PCI-PCI bridge
13253446Smrj  * needs special handling.  We may need to chase up the device tree,
13263446Smrj  * using the PCI-PCI Bridge specification's "rotating IPIN assumptions",
13273446Smrj  * to find the IPIN at the root bus that relates to the IPIN on the
13283446Smrj  * subsidiary bus (for ACPI or MP).  We may, however, have an entry
13293446Smrj  * in the MP table or the ACPI namespace for this device itself.
13303446Smrj  * We handle both cases in the search below.
13313446Smrj  */
13323446Smrj /* this is the non-acpi version */
133312683SJimmy.Vetayases@oracle.com int
apic_handle_pci_pci_bridge(dev_info_t * idip,int child_devno,int child_ipin,struct apic_io_intr ** intrp)13343446Smrj apic_handle_pci_pci_bridge(dev_info_t *idip, int child_devno, int child_ipin,
13353446Smrj 			struct apic_io_intr **intrp)
13363446Smrj {
13373446Smrj 	dev_info_t *dipp, *dip;
13383446Smrj 	int pci_irq;
13393446Smrj 	ddi_acc_handle_t cfg_handle;
13403446Smrj 	int bridge_devno, bridge_bus;
13413446Smrj 	int ipin;
13423446Smrj 
13433446Smrj 	dip = idip;
13443446Smrj 
13453446Smrj 	/*CONSTCOND*/
13463446Smrj 	while (1) {
13473472Smyers 		if (((dipp = ddi_get_parent(dip)) == (dev_info_t *)NULL) ||
13484355Smyers 		    (pci_config_setup(dipp, &cfg_handle) != DDI_SUCCESS))
13493446Smrj 			return (-1);
13503472Smyers 		if ((pci_config_get8(cfg_handle, PCI_CONF_BASCLASS) ==
13513446Smrj 		    PCI_CLASS_BRIDGE) && (pci_config_get8(cfg_handle,
13523446Smrj 		    PCI_CONF_SUBCLASS) == PCI_BRIDGE_PCI)) {
13533446Smrj 			pci_config_teardown(&cfg_handle);
13543446Smrj 			if (acpica_get_bdf(dipp, &bridge_bus, &bridge_devno,
13553446Smrj 			    NULL) != 0)
13563446Smrj 				return (-1);
13573446Smrj 			/*
13583472Smyers 			 * This is the rotating scheme documented in the
13593472Smyers 			 * PCI-to-PCI spec.  If the PCI-to-PCI bridge is
13603472Smyers 			 * behind another PCI-to-PCI bridge, then it needs
13613472Smyers 			 * to keep ascending until an interrupt entry is
13623472Smyers 			 * found or the root is reached.
13633446Smrj 			 */
13643446Smrj 			ipin = (child_devno + child_ipin) % PCI_INTD;
13653446Smrj 				if (bridge_bus == 0 && apic_pci_bus_total == 1)
13663446Smrj 					bridge_bus = (int)apic_single_pci_busid;
13673446Smrj 				pci_irq = ((bridge_devno & 0x1f) << 2) |
13683446Smrj 				    (ipin & 0x3);
13693446Smrj 				if ((*intrp = apic_find_io_intr_w_busid(pci_irq,
13703446Smrj 				    bridge_bus)) != NULL) {
13713446Smrj 					return (pci_irq);
13723446Smrj 				}
13733446Smrj 			dip = dipp;
13743446Smrj 			child_devno = bridge_devno;
13753446Smrj 			child_ipin = ipin;
13763472Smyers 		} else {
13773472Smyers 			pci_config_teardown(&cfg_handle);
13783446Smrj 			return (-1);
13793472Smyers 		}
13803446Smrj 	}
13813446Smrj 	/*LINTED: function will not fall off the bottom */
13823446Smrj }
13833446Smrj 
138412683SJimmy.Vetayases@oracle.com uchar_t
acpi_find_ioapic(int irq)13853446Smrj acpi_find_ioapic(int irq)
13863446Smrj {
13873446Smrj 	int i;
13883446Smrj 
13893446Smrj 	for (i = 0; i < apic_io_max; i++) {
13903446Smrj 		if (irq >= apic_io_vectbase[i] && irq <= apic_io_vectend[i])
139112683SJimmy.Vetayases@oracle.com 			return ((uchar_t)i);
13923446Smrj 	}
13933446Smrj 	return (0xFF);	/* shouldn't happen */
13943446Smrj }
13953446Smrj 
13963446Smrj /*
13973446Smrj  * See if two irqs are compatible for sharing a vector.
13983446Smrj  * Currently we only support sharing of PCI devices.
13993446Smrj  */
14003446Smrj static int
acpi_intr_compatible(iflag_t iflag1,iflag_t iflag2)14013446Smrj acpi_intr_compatible(iflag_t iflag1, iflag_t iflag2)
14023446Smrj {
14033446Smrj 	uint_t	level1, po1;
14043446Smrj 	uint_t	level2, po2;
14053446Smrj 
14063446Smrj 	/* Assume active high by default */
14073446Smrj 	po1 = 0;
14083446Smrj 	po2 = 0;
14093446Smrj 
14103446Smrj 	if (iflag1.bustype != iflag2.bustype || iflag1.bustype != BUS_PCI)
14113446Smrj 		return (0);
14123446Smrj 
14133446Smrj 	if (iflag1.intr_el == INTR_EL_CONFORM)
14143446Smrj 		level1 = AV_LEVEL;
14153446Smrj 	else
14163446Smrj 		level1 = (iflag1.intr_el == INTR_EL_LEVEL) ? AV_LEVEL : 0;
14173446Smrj 
14183446Smrj 	if (level1 && ((iflag1.intr_po == INTR_PO_ACTIVE_LOW) ||
14193446Smrj 	    (iflag1.intr_po == INTR_PO_CONFORM)))
14203446Smrj 		po1 = AV_ACTIVE_LOW;
14213446Smrj 
14223446Smrj 	if (iflag2.intr_el == INTR_EL_CONFORM)
14233446Smrj 		level2 = AV_LEVEL;
14243446Smrj 	else
14253446Smrj 		level2 = (iflag2.intr_el == INTR_EL_LEVEL) ? AV_LEVEL : 0;
14263446Smrj 
14273446Smrj 	if (level2 && ((iflag2.intr_po == INTR_PO_ACTIVE_LOW) ||
14283446Smrj 	    (iflag2.intr_po == INTR_PO_CONFORM)))
14293446Smrj 		po2 = AV_ACTIVE_LOW;
14303446Smrj 
14313446Smrj 	if ((level1 == level2) && (po1 == po2))
14323446Smrj 		return (1);
14333446Smrj 
14343446Smrj 	return (0);
14353446Smrj }
14363446Smrj 
143712683SJimmy.Vetayases@oracle.com struct apic_io_intr *
apic_find_io_intr_w_busid(int irqno,int busid)14383446Smrj apic_find_io_intr_w_busid(int irqno, int busid)
14393446Smrj {
14403446Smrj 	struct	apic_io_intr	*intrp;
14413446Smrj 
14423446Smrj 	/*
14433446Smrj 	 * It can have more than 1 entry with same source bus IRQ,
14443446Smrj 	 * but unique with the source bus id
14453446Smrj 	 */
14463446Smrj 	intrp = apic_io_intrp;
14473446Smrj 	if (intrp != NULL) {
14483446Smrj 		while (intrp->intr_entry == APIC_IO_INTR_ENTRY) {
14493446Smrj 			if (intrp->intr_irq == irqno &&
14503446Smrj 			    intrp->intr_busid == busid &&
14513446Smrj 			    intrp->intr_type == IO_INTR_INT)
14523446Smrj 				return (intrp);
14533446Smrj 			intrp++;
14543446Smrj 		}
14553446Smrj 	}
14563446Smrj 	APIC_VERBOSE_IOAPIC((CE_NOTE, "Did not find io intr for irqno:"
14573446Smrj 	    "busid %x:%x\n", irqno, busid));
14583446Smrj 	return ((struct apic_io_intr *)NULL);
14593446Smrj }
14603446Smrj 
14613446Smrj 
14623446Smrj struct mps_bus_info {
14633446Smrj 	char	*bus_name;
14643446Smrj 	int	bus_id;
14653446Smrj } bus_info_array[] = {
14663446Smrj 	"ISA ", BUS_ISA,
14673446Smrj 	"PCI ", BUS_PCI,
14683446Smrj 	"EISA ", BUS_EISA,
14693446Smrj 	"XPRESS", BUS_XPRESS,
14703446Smrj 	"PCMCIA", BUS_PCMCIA,
14713446Smrj 	"VL ", BUS_VL,
14723446Smrj 	"CBUS ", BUS_CBUS,
14733446Smrj 	"CBUSII", BUS_CBUSII,
14743446Smrj 	"FUTURE", BUS_FUTURE,
14753446Smrj 	"INTERN", BUS_INTERN,
14763446Smrj 	"MBI ", BUS_MBI,
14773446Smrj 	"MBII ", BUS_MBII,
14783446Smrj 	"MPI ", BUS_MPI,
14793446Smrj 	"MPSA ", BUS_MPSA,
14803446Smrj 	"NUBUS ", BUS_NUBUS,
14813446Smrj 	"TC ", BUS_TC,
14823446Smrj 	"VME ", BUS_VME,
14833446Smrj 	"PCI-E ", BUS_PCIE
14843446Smrj };
14853446Smrj 
14863446Smrj static int
apic_find_bus_type(char * bus)14873446Smrj apic_find_bus_type(char *bus)
14883446Smrj {
14893446Smrj 	int	i = 0;
14903446Smrj 
14913446Smrj 	for (; i < sizeof (bus_info_array)/sizeof (struct mps_bus_info); i++)
14923446Smrj 		if (strncmp(bus, bus_info_array[i].bus_name,
14933446Smrj 		    strlen(bus_info_array[i].bus_name)) == 0)
14943446Smrj 			return (bus_info_array[i].bus_id);
14953446Smrj 	APIC_VERBOSE_IOAPIC((CE_WARN, "Did not find bus type for bus %s", bus));
14963446Smrj 	return (0);
14973446Smrj }
14983446Smrj 
14993446Smrj static int
apic_find_bus(int busid)15003446Smrj apic_find_bus(int busid)
15013446Smrj {
15023446Smrj 	struct	apic_bus	*busp;
15033446Smrj 
15043446Smrj 	busp = apic_busp;
15053446Smrj 	while (busp->bus_entry == APIC_BUS_ENTRY) {
15063446Smrj 		if (busp->bus_id == busid)
15073446Smrj 			return (apic_find_bus_type((char *)&busp->bus_str1));
15083446Smrj 		busp++;
15093446Smrj 	}
15103446Smrj 	APIC_VERBOSE_IOAPIC((CE_WARN, "Did not find bus for bus id %x", busid));
15113446Smrj 	return (0);
15123446Smrj }
15133446Smrj 
151412683SJimmy.Vetayases@oracle.com int
apic_find_bus_id(int bustype)15153446Smrj apic_find_bus_id(int bustype)
15163446Smrj {
15173446Smrj 	struct	apic_bus	*busp;
15183446Smrj 
15193446Smrj 	busp = apic_busp;
15203446Smrj 	while (busp->bus_entry == APIC_BUS_ENTRY) {
15213446Smrj 		if (apic_find_bus_type((char *)&busp->bus_str1) == bustype)
15223446Smrj 			return (busp->bus_id);
15233446Smrj 		busp++;
15243446Smrj 	}
15253446Smrj 	APIC_VERBOSE_IOAPIC((CE_WARN, "Did not find bus id for bustype %x",
15263446Smrj 	    bustype));
15273446Smrj 	return (-1);
15283446Smrj }
15293446Smrj 
15303446Smrj /*
15313446Smrj  * Check if a particular irq need to be reserved for any io_intr
15323446Smrj  */
15333446Smrj static struct apic_io_intr *
apic_find_io_intr(int irqno)15343446Smrj apic_find_io_intr(int irqno)
15353446Smrj {
15363446Smrj 	struct	apic_io_intr	*intrp;
15373446Smrj 
15383446Smrj 	intrp = apic_io_intrp;
15393446Smrj 	if (intrp != NULL) {
15403446Smrj 		while (intrp->intr_entry == APIC_IO_INTR_ENTRY) {
15413446Smrj 			if (intrp->intr_irq == irqno &&
15423446Smrj 			    intrp->intr_type == IO_INTR_INT)
15433446Smrj 				return (intrp);
15443446Smrj 			intrp++;
15453446Smrj 		}
15463446Smrj 	}
15473446Smrj 	return ((struct apic_io_intr *)NULL);
15483446Smrj }
15493446Smrj 
15503446Smrj /*
15513446Smrj  * Check if the given ioapicindex intin combination has already been assigned
15523446Smrj  * an irq. If so return irqno. Else -1
15533446Smrj  */
155412683SJimmy.Vetayases@oracle.com int
apic_find_intin(uchar_t ioapic,uchar_t intin)15553446Smrj apic_find_intin(uchar_t ioapic, uchar_t intin)
15563446Smrj {
15573446Smrj 	apic_irq_t *irqptr;
15583446Smrj 	int	i;
15593446Smrj 
15603446Smrj 	/* find ioapic and intin in the apic_irq_table[] and return the index */
15613446Smrj 	for (i = apic_min_device_irq; i <= apic_max_device_irq; i++) {
15623446Smrj 		irqptr = apic_irq_table[i];
15633446Smrj 		while (irqptr) {
15643446Smrj 			if ((irqptr->airq_mps_intr_index >= 0) &&
15653446Smrj 			    (irqptr->airq_intin_no == intin) &&
15663446Smrj 			    (irqptr->airq_ioapicindex == ioapic)) {
15673446Smrj 				APIC_VERBOSE_IOAPIC((CE_NOTE, "!Found irq "
15683446Smrj 				    "entry for ioapic:intin %x:%x "
15693446Smrj 				    "shared interrupts ?", ioapic, intin));
15703446Smrj 				return (i);
15713446Smrj 			}
15723446Smrj 			irqptr = irqptr->airq_next;
15733446Smrj 		}
15743446Smrj 	}
15753446Smrj 	return (-1);
15763446Smrj }
15773446Smrj 
15783446Smrj int
apic_allocate_irq(int irq)15793446Smrj apic_allocate_irq(int irq)
15803446Smrj {
15813446Smrj 	int	freeirq, i;
15823446Smrj 
15833446Smrj 	if ((freeirq = apic_find_free_irq(irq, (APIC_RESV_IRQ - 1))) == -1)
15843446Smrj 		if ((freeirq = apic_find_free_irq(APIC_FIRST_FREE_IRQ,
15853446Smrj 		    (irq - 1))) == -1) {
15863446Smrj 			/*
15873446Smrj 			 * if BIOS really defines every single irq in the mps
15883446Smrj 			 * table, then don't worry about conflicting with
15893446Smrj 			 * them, just use any free slot in apic_irq_table
15903446Smrj 			 */
15913446Smrj 			for (i = APIC_FIRST_FREE_IRQ; i < APIC_RESV_IRQ; i++) {
15923446Smrj 				if ((apic_irq_table[i] == NULL) ||
15933446Smrj 				    apic_irq_table[i]->airq_mps_intr_index ==
15943446Smrj 				    FREE_INDEX) {
15953446Smrj 				freeirq = i;
15963446Smrj 				break;
15973446Smrj 			}
15983446Smrj 		}
15993446Smrj 		if (freeirq == -1) {
16003446Smrj 			/* This shouldn't happen, but just in case */
16013446Smrj 			cmn_err(CE_WARN, "%s: NO available IRQ", psm_name);
16023446Smrj 			return (-1);
16033446Smrj 		}
16043446Smrj 	}
16053446Smrj 	if (apic_irq_table[freeirq] == NULL) {
16063446Smrj 		apic_irq_table[freeirq] =
16073446Smrj 		    kmem_zalloc(sizeof (apic_irq_t), KM_NOSLEEP);
16083446Smrj 		if (apic_irq_table[freeirq] == NULL) {
16093446Smrj 			cmn_err(CE_WARN, "%s: NO memory to allocate IRQ",
16104355Smyers 			    psm_name);
16113446Smrj 			return (-1);
16123446Smrj 		}
161311465SKerry.Shu@Sun.COM 		apic_irq_table[freeirq]->airq_temp_cpu = IRQ_UNINIT;
16143446Smrj 		apic_irq_table[freeirq]->airq_mps_intr_index = FREE_INDEX;
16153446Smrj 	}
16163446Smrj 	return (freeirq);
16173446Smrj }
16183446Smrj 
16193446Smrj static int
apic_find_free_irq(int start,int end)16203446Smrj apic_find_free_irq(int start, int end)
16213446Smrj {
16223446Smrj 	int	i;
16233446Smrj 
16243446Smrj 	for (i = start; i <= end; i++)
16253446Smrj 		/* Check if any I/O entry needs this IRQ */
16263446Smrj 		if (apic_find_io_intr(i) == NULL) {
16273446Smrj 			/* Then see if it is free */
16283446Smrj 			if ((apic_irq_table[i] == NULL) ||
16293446Smrj 			    (apic_irq_table[i]->airq_mps_intr_index ==
16303446Smrj 			    FREE_INDEX)) {
16313446Smrj 				return (i);
16323446Smrj 			}
16333446Smrj 		}
16343446Smrj 	return (-1);
16353446Smrj }
16363446Smrj 
16373446Smrj /*
16383446Smrj  * compute the polarity, trigger mode and vector for programming into
16393446Smrj  * the I/O apic and record in airq_rdt_entry.
16403446Smrj  */
164112683SJimmy.Vetayases@oracle.com void
apic_record_rdt_entry(apic_irq_t * irqptr,int irq)16423446Smrj apic_record_rdt_entry(apic_irq_t *irqptr, int irq)
16433446Smrj {
16443446Smrj 	int	ioapicindex, bus_type, vector;
16453446Smrj 	short	intr_index;
16463446Smrj 	uint_t	level, po, io_po;
16473446Smrj 	struct apic_io_intr *iointrp;
16483446Smrj 
16493446Smrj 	intr_index = irqptr->airq_mps_intr_index;
16503446Smrj 	DDI_INTR_IMPLDBG((CE_CONT, "apic_record_rdt_entry: intr_index=%d "
16513446Smrj 	    "irq = 0x%x dip = 0x%p vector = 0x%x\n", intr_index, irq,
16523446Smrj 	    (void *)irqptr->airq_dip, irqptr->airq_vector));
16533446Smrj 
16543446Smrj 	if (intr_index == RESERVE_INDEX) {
16553446Smrj 		apic_error |= APIC_ERR_INVALID_INDEX;
16563446Smrj 		return;
16573446Smrj 	} else if (APIC_IS_MSI_OR_MSIX_INDEX(intr_index)) {
16583446Smrj 		return;
16593446Smrj 	}
16603446Smrj 
16613446Smrj 	vector = irqptr->airq_vector;
16623446Smrj 	ioapicindex = irqptr->airq_ioapicindex;
16633446Smrj 	/* Assume edge triggered by default */
16643446Smrj 	level = 0;
16653446Smrj 	/* Assume active high by default */
16663446Smrj 	po = 0;
16673446Smrj 
16683446Smrj 	if (intr_index == DEFAULT_INDEX || intr_index == FREE_INDEX) {
16693446Smrj 		ASSERT(irq < 16);
16703446Smrj 		if (eisa_level_intr_mask & (1 << irq))
16713446Smrj 			level = AV_LEVEL;
16723446Smrj 		if (intr_index == FREE_INDEX && apic_defconf == 0)
16733446Smrj 			apic_error |= APIC_ERR_INVALID_INDEX;
16743446Smrj 	} else if (intr_index == ACPI_INDEX) {
16753446Smrj 		bus_type = irqptr->airq_iflag.bustype;
16763446Smrj 		if (irqptr->airq_iflag.intr_el == INTR_EL_CONFORM) {
16773446Smrj 			if (bus_type == BUS_PCI)
16783446Smrj 				level = AV_LEVEL;
16793446Smrj 		} else
16803446Smrj 			level = (irqptr->airq_iflag.intr_el == INTR_EL_LEVEL) ?
16813446Smrj 			    AV_LEVEL : 0;
16823446Smrj 		if (level &&
16833446Smrj 		    ((irqptr->airq_iflag.intr_po == INTR_PO_ACTIVE_LOW) ||
16843446Smrj 		    (irqptr->airq_iflag.intr_po == INTR_PO_CONFORM &&
16853446Smrj 		    bus_type == BUS_PCI)))
16863446Smrj 			po = AV_ACTIVE_LOW;
16873446Smrj 	} else {
16883446Smrj 		iointrp = apic_io_intrp + intr_index;
16893446Smrj 		bus_type = apic_find_bus(iointrp->intr_busid);
16903446Smrj 		if (iointrp->intr_el == INTR_EL_CONFORM) {
16913446Smrj 			if ((irq < 16) && (eisa_level_intr_mask & (1 << irq)))
16923446Smrj 				level = AV_LEVEL;
16933446Smrj 			else if (bus_type == BUS_PCI)
16943446Smrj 				level = AV_LEVEL;
16953446Smrj 		} else
16963446Smrj 			level = (iointrp->intr_el == INTR_EL_LEVEL) ?
16973446Smrj 			    AV_LEVEL : 0;
16983446Smrj 		if (level && ((iointrp->intr_po == INTR_PO_ACTIVE_LOW) ||
16993446Smrj 		    (iointrp->intr_po == INTR_PO_CONFORM &&
17003446Smrj 		    bus_type == BUS_PCI)))
17013446Smrj 			po = AV_ACTIVE_LOW;
17023446Smrj 	}
17033446Smrj 	if (level)
17043446Smrj 		apic_level_intr[irq] = 1;
17053446Smrj 	/*
17063446Smrj 	 * The 82489DX External APIC cannot do active low polarity interrupts.
17073446Smrj 	 */
17083446Smrj 	if (po && (apic_io_ver[ioapicindex] != IOAPIC_VER_82489DX))
17093446Smrj 		io_po = po;
17103446Smrj 	else
17113446Smrj 		io_po = 0;
17123446Smrj 
17133446Smrj 	if (apic_verbose & APIC_VERBOSE_IOAPIC_FLAG)
171412683SJimmy.Vetayases@oracle.com 		prom_printf("setio: ioapic=0x%x intin=0x%x level=0x%x po=0x%x "
171512683SJimmy.Vetayases@oracle.com 		    "vector=0x%x cpu=0x%x\n\n", ioapicindex,
171612683SJimmy.Vetayases@oracle.com 		    irqptr->airq_intin_no, level, io_po, vector,
171712683SJimmy.Vetayases@oracle.com 		    irqptr->airq_cpu);
17183446Smrj 
17193446Smrj 	irqptr->airq_rdt_entry = level|io_po|vector;
17203446Smrj }
17213446Smrj 
17223446Smrj int
apic_acpi_translate_pci_irq(dev_info_t * dip,int busid,int devid,int ipin,int * pci_irqp,iflag_t * intr_flagp)17233446Smrj apic_acpi_translate_pci_irq(dev_info_t *dip, int busid, int devid,
17243446Smrj     int ipin, int *pci_irqp, iflag_t *intr_flagp)
17253446Smrj {
17263446Smrj 
17273446Smrj 	int status;
17283446Smrj 	acpi_psm_lnk_t acpipsmlnk;
17293446Smrj 
17303446Smrj 	if ((status = acpi_get_irq_cache_ent(busid, devid, ipin, pci_irqp,
17313446Smrj 	    intr_flagp)) == ACPI_PSM_SUCCESS) {
17323446Smrj 		APIC_VERBOSE_IRQ((CE_CONT, "!%s: Found irqno %d "
17333446Smrj 		    "from cache for device %s, instance #%d\n", psm_name,
17343446Smrj 		    *pci_irqp, ddi_get_name(dip), ddi_get_instance(dip)));
17353446Smrj 		return (status);
17363446Smrj 	}
17373446Smrj 
17383446Smrj 	bzero(&acpipsmlnk, sizeof (acpi_psm_lnk_t));
17393446Smrj 
17403446Smrj 	if ((status = acpi_translate_pci_irq(dip, ipin, pci_irqp, intr_flagp,
17413446Smrj 	    &acpipsmlnk)) == ACPI_PSM_FAILURE) {
17423446Smrj 		APIC_VERBOSE_IRQ((CE_WARN, "%s: "
17433446Smrj 		    " acpi_translate_pci_irq failed for device %s, instance"
17443446Smrj 		    " #%d", psm_name, ddi_get_name(dip),
17453446Smrj 		    ddi_get_instance(dip)));
17463446Smrj 		return (status);
17473446Smrj 	}
17483446Smrj 
17493446Smrj 	if (status == ACPI_PSM_PARTIAL && acpipsmlnk.lnkobj != NULL) {
17503446Smrj 		status = apic_acpi_irq_configure(&acpipsmlnk, dip, pci_irqp,
17513446Smrj 		    intr_flagp);
17523446Smrj 		if (status != ACPI_PSM_SUCCESS) {
17533446Smrj 			status = acpi_get_current_irq_resource(&acpipsmlnk,
17543446Smrj 			    pci_irqp, intr_flagp);
17553446Smrj 		}
17563446Smrj 	}
17573446Smrj 
17583446Smrj 	if (status == ACPI_PSM_SUCCESS) {
17593446Smrj 		acpi_new_irq_cache_ent(busid, devid, ipin, *pci_irqp,
17603446Smrj 		    intr_flagp, &acpipsmlnk);
17613446Smrj 
17623446Smrj 		APIC_VERBOSE_IRQ((CE_CONT, "%s: [ACPI] "
17633446Smrj 		    "new irq %d for device %s, instance #%d\n", psm_name,
17643446Smrj 		    *pci_irqp, ddi_get_name(dip), ddi_get_instance(dip)));
17653446Smrj 	}
17663446Smrj 
17673446Smrj 	return (status);
17683446Smrj }
17693446Smrj 
17703446Smrj /*
17713446Smrj  * Adds an entry to the irq list passed in, and returns the new list.
17723446Smrj  * Entries are added in priority order (lower numerical priorities are
17733446Smrj  * placed closer to the head of the list)
17743446Smrj  */
17753446Smrj static prs_irq_list_t *
acpi_insert_prs_irq_ent(prs_irq_list_t * listp,int priority,int irq,iflag_t * iflagp,acpi_prs_private_t * prsprvp)17763446Smrj acpi_insert_prs_irq_ent(prs_irq_list_t *listp, int priority, int irq,
17773446Smrj     iflag_t *iflagp, acpi_prs_private_t *prsprvp)
17783446Smrj {
17793446Smrj 	struct prs_irq_list_ent *newent, *prevp = NULL, *origlistp;
17803446Smrj 
17813446Smrj 	newent = kmem_zalloc(sizeof (struct prs_irq_list_ent), KM_SLEEP);
17823446Smrj 
17833446Smrj 	newent->list_prio = priority;
17843446Smrj 	newent->irq = irq;
17853446Smrj 	newent->intrflags = *iflagp;
17863446Smrj 	newent->prsprv = *prsprvp;
17873446Smrj 	/* ->next is NULL from kmem_zalloc */
17883446Smrj 
17893446Smrj 	/*
17903446Smrj 	 * New list -- return the new entry as the list.
17913446Smrj 	 */
17923446Smrj 	if (listp == NULL)
17933446Smrj 		return (newent);
17943446Smrj 
17953446Smrj 	/*
17963446Smrj 	 * Save original list pointer for return (since we're not modifying
17973446Smrj 	 * the head)
17983446Smrj 	 */
17993446Smrj 	origlistp = listp;
18003446Smrj 
18013446Smrj 	/*
18023446Smrj 	 * Insertion sort, with entries with identical keys stored AFTER
18033446Smrj 	 * existing entries (the less-than-or-equal test of priority does
18043446Smrj 	 * this for us).
18053446Smrj 	 */
18063446Smrj 	while (listp != NULL && listp->list_prio <= priority) {
18073446Smrj 		prevp = listp;
18083446Smrj 		listp = listp->next;
18093446Smrj 	}
18103446Smrj 
18113446Smrj 	newent->next = listp;
18123446Smrj 
18133446Smrj 	if (prevp == NULL) { /* Add at head of list (newent is the new head) */
18143446Smrj 		return (newent);
18153446Smrj 	} else {
18163446Smrj 		prevp->next = newent;
18173446Smrj 		return (origlistp);
18183446Smrj 	}
18193446Smrj }
18203446Smrj 
18213446Smrj /*
18223446Smrj  * Frees the list passed in, deallocating all memory and leaving *listpp
18233446Smrj  * set to NULL.
18243446Smrj  */
18253446Smrj static void
acpi_destroy_prs_irq_list(prs_irq_list_t ** listpp)18263446Smrj acpi_destroy_prs_irq_list(prs_irq_list_t **listpp)
18273446Smrj {
18283446Smrj 	struct prs_irq_list_ent *nextp;
18293446Smrj 
18303446Smrj 	ASSERT(listpp != NULL);
18313446Smrj 
18323446Smrj 	while (*listpp != NULL) {
18333446Smrj 		nextp = (*listpp)->next;
18343446Smrj 		kmem_free(*listpp, sizeof (struct prs_irq_list_ent));
18353446Smrj 		*listpp = nextp;
18363446Smrj 	}
18373446Smrj }
18383446Smrj 
18393446Smrj /*
18403446Smrj  * apic_choose_irqs_from_prs returns a list of irqs selected from the list of
18413446Smrj  * irqs returned by the link device's _PRS method.  The irqs are chosen
18423446Smrj  * to minimize contention in situations where the interrupt link device
18433446Smrj  * can be programmed to steer interrupts to different interrupt controller
18443446Smrj  * inputs (some of which may already be in use).  The list is sorted in order
18453446Smrj  * of irqs to use, with the highest priority given to interrupt controller
18463446Smrj  * inputs that are not shared.   When an interrupt controller input
18473446Smrj  * must be shared, apic_choose_irqs_from_prs adds the possible irqs to the
18483446Smrj  * returned list in the order that minimizes sharing (thereby ensuring lowest
18493446Smrj  * possible latency from interrupt trigger time to ISR execution time).
18503446Smrj  */
18513446Smrj static prs_irq_list_t *
apic_choose_irqs_from_prs(acpi_irqlist_t * irqlistent,dev_info_t * dip,int crs_irq)18523446Smrj apic_choose_irqs_from_prs(acpi_irqlist_t *irqlistent, dev_info_t *dip,
18533446Smrj     int crs_irq)
18543446Smrj {
18553446Smrj 	int32_t irq;
18563446Smrj 	int i;
18573446Smrj 	prs_irq_list_t *prsirqlistp = NULL;
18583446Smrj 	iflag_t iflags;
18593446Smrj 
18603446Smrj 	while (irqlistent != NULL) {
18613446Smrj 		irqlistent->intr_flags.bustype = BUS_PCI;
18623446Smrj 
18633446Smrj 		for (i = 0; i < irqlistent->num_irqs; i++) {
18643446Smrj 
18653446Smrj 			irq = irqlistent->irqs[i];
18663446Smrj 
18673446Smrj 			if (irq <= 0) {
18683446Smrj 				/* invalid irq number */
18693446Smrj 				continue;
18703446Smrj 			}
18713446Smrj 
18723446Smrj 			if ((irq < 16) && (apic_reserved_irqlist[irq]))
18733446Smrj 				continue;
18743446Smrj 
18753446Smrj 			if ((apic_irq_table[irq] == NULL) ||
18763446Smrj 			    (apic_irq_table[irq]->airq_dip == dip)) {
18773446Smrj 
18783446Smrj 				prsirqlistp = acpi_insert_prs_irq_ent(
18793446Smrj 				    prsirqlistp, 0 /* Highest priority */, irq,
18803446Smrj 				    &irqlistent->intr_flags,
18813446Smrj 				    &irqlistent->acpi_prs_prv);
18823446Smrj 
18833446Smrj 				/*
18843446Smrj 				 * If we do not prefer the current irq from _CRS
18853446Smrj 				 * or if we do and this irq is the same as the
18863446Smrj 				 * current irq from _CRS, this is the one
18873446Smrj 				 * to pick.
18883446Smrj 				 */
18893446Smrj 				if (!(apic_prefer_crs) || (irq == crs_irq)) {
18903446Smrj 					return (prsirqlistp);
18913446Smrj 				}
18923446Smrj 				continue;
18933446Smrj 			}
18943446Smrj 
18953446Smrj 			/*
18963446Smrj 			 * Edge-triggered interrupts cannot be shared
18973446Smrj 			 */
18983446Smrj 			if (irqlistent->intr_flags.intr_el == INTR_EL_EDGE)
18993446Smrj 				continue;
19003446Smrj 
19013446Smrj 			/*
19023446Smrj 			 * To work around BIOSes that contain incorrect
19033446Smrj 			 * interrupt polarity information in interrupt
19043446Smrj 			 * descriptors returned by _PRS, we assume that
19053446Smrj 			 * the polarity of the other device sharing this
19063446Smrj 			 * interrupt controller input is compatible.
19073446Smrj 			 * If it's not, the caller will catch it when
19083446Smrj 			 * the caller invokes the link device's _CRS method
19093446Smrj 			 * (after invoking its _SRS method).
19103446Smrj 			 */
19113446Smrj 			iflags = irqlistent->intr_flags;
19123446Smrj 			iflags.intr_po =
19133446Smrj 			    apic_irq_table[irq]->airq_iflag.intr_po;
19143446Smrj 
19153446Smrj 			if (!acpi_intr_compatible(iflags,
19163446Smrj 			    apic_irq_table[irq]->airq_iflag)) {
19173446Smrj 				APIC_VERBOSE_IRQ((CE_CONT, "!%s: irq %d "
19183446Smrj 				    "not compatible [%x:%x:%x !~ %x:%x:%x]",
19193446Smrj 				    psm_name, irq,
19203446Smrj 				    iflags.intr_po,
19213446Smrj 				    iflags.intr_el,
19223446Smrj 				    iflags.bustype,
19233446Smrj 				    apic_irq_table[irq]->airq_iflag.intr_po,
19243446Smrj 				    apic_irq_table[irq]->airq_iflag.intr_el,
19253446Smrj 				    apic_irq_table[irq]->airq_iflag.bustype));
19263446Smrj 				continue;
19273446Smrj 			}
19283446Smrj 
19293446Smrj 			/*
19303446Smrj 			 * If we prefer the irq from _CRS, no need
19313446Smrj 			 * to search any further (and make sure
19323446Smrj 			 * to add this irq with the highest priority
19333446Smrj 			 * so it's tried first).
19343446Smrj 			 */
19353446Smrj 			if (crs_irq == irq && apic_prefer_crs) {
19363446Smrj 
19373446Smrj 				return (acpi_insert_prs_irq_ent(
19383446Smrj 				    prsirqlistp,
19393446Smrj 				    0 /* Highest priority */,
19403446Smrj 				    irq, &iflags,
19413446Smrj 				    &irqlistent->acpi_prs_prv));
19423446Smrj 			}
19433446Smrj 
19443446Smrj 			/*
19453446Smrj 			 * Priority is equal to the share count (lower
19463446Smrj 			 * share count is higher priority). Note that
19473446Smrj 			 * the intr flags passed in here are the ones we
19483446Smrj 			 * changed above -- if incorrect, it will be
19493446Smrj 			 * caught by the caller's _CRS flags comparison.
19503446Smrj 			 */
19513446Smrj 			prsirqlistp = acpi_insert_prs_irq_ent(
19523446Smrj 			    prsirqlistp,
19533446Smrj 			    apic_irq_table[irq]->airq_share, irq,
19543446Smrj 			    &iflags, &irqlistent->acpi_prs_prv);
19553446Smrj 		}
19563446Smrj 
19573446Smrj 		/* Go to the next irqlist entry */
19583446Smrj 		irqlistent = irqlistent->next;
19593446Smrj 	}
19603446Smrj 
19613446Smrj 	return (prsirqlistp);
19623446Smrj }
19633446Smrj 
19643446Smrj /*
19653446Smrj  * Configures the irq for the interrupt link device identified by
19663446Smrj  * acpipsmlnkp.
19673446Smrj  *
19683446Smrj  * Gets the current and the list of possible irq settings for the
19693446Smrj  * device. If apic_unconditional_srs is not set, and the current
19703446Smrj  * resource setting is in the list of possible irq settings,
19713446Smrj  * current irq resource setting is passed to the caller.
19723446Smrj  *
19733446Smrj  * Otherwise, picks an irq number from the list of possible irq
19743446Smrj  * settings, and sets the irq of the device to this value.
19753446Smrj  * If prefer_crs is set, among a set of irq numbers in the list that have
19763446Smrj  * the least number of devices sharing the interrupt, we pick current irq
19773446Smrj  * resource setting if it is a member of this set.
19783446Smrj  *
19793446Smrj  * Passes the irq number in the value pointed to by pci_irqp, and
19803446Smrj  * polarity and sensitivity in the structure pointed to by dipintrflagp
19813446Smrj  * to the caller.
19823446Smrj  *
19833446Smrj  * Note that if setting the irq resource failed, but successfuly obtained
19843446Smrj  * the current irq resource settings, passes the current irq resources
19853446Smrj  * and considers it a success.
19863446Smrj  *
19873446Smrj  * Returns:
19883446Smrj  * ACPI_PSM_SUCCESS on success.
19893446Smrj  *
19903446Smrj  * ACPI_PSM_FAILURE if an error occured during the configuration or
19913446Smrj  * if a suitable irq was not found for this device, or if setting the
19923446Smrj  * irq resource and obtaining the current resource fails.
19933446Smrj  *
19943446Smrj  */
19953446Smrj static int
apic_acpi_irq_configure(acpi_psm_lnk_t * acpipsmlnkp,dev_info_t * dip,int * pci_irqp,iflag_t * dipintr_flagp)19963446Smrj apic_acpi_irq_configure(acpi_psm_lnk_t *acpipsmlnkp, dev_info_t *dip,
19973446Smrj     int *pci_irqp, iflag_t *dipintr_flagp)
19983446Smrj {
19993446Smrj 	int32_t irq;
20003446Smrj 	int cur_irq = -1;
20013446Smrj 	acpi_irqlist_t *irqlistp;
20023446Smrj 	prs_irq_list_t *prs_irq_listp, *prs_irq_entp;
20033446Smrj 	boolean_t found_irq = B_FALSE;
20043446Smrj 
20053446Smrj 	dipintr_flagp->bustype = BUS_PCI;
20063446Smrj 
20073446Smrj 	if ((acpi_get_possible_irq_resources(acpipsmlnkp, &irqlistp))
20083446Smrj 	    == ACPI_PSM_FAILURE) {
20093446Smrj 		APIC_VERBOSE_IRQ((CE_WARN, "!%s: Unable to determine "
20103446Smrj 		    "or assign IRQ for device %s, instance #%d: The system was "
20113446Smrj 		    "unable to get the list of potential IRQs from ACPI.",
20123446Smrj 		    psm_name, ddi_get_name(dip), ddi_get_instance(dip)));
20133446Smrj 
20143446Smrj 		return (ACPI_PSM_FAILURE);
20153446Smrj 	}
20163446Smrj 
20173446Smrj 	if ((acpi_get_current_irq_resource(acpipsmlnkp, &cur_irq,
20183446Smrj 	    dipintr_flagp) == ACPI_PSM_SUCCESS) && (!apic_unconditional_srs) &&
20193446Smrj 	    (cur_irq > 0)) {
20203446Smrj 		/*
20213446Smrj 		 * If an IRQ is set in CRS and that IRQ exists in the set
20223446Smrj 		 * returned from _PRS, return that IRQ, otherwise print
20233446Smrj 		 * a warning
20243446Smrj 		 */
20253446Smrj 
20263446Smrj 		if (acpi_irqlist_find_irq(irqlistp, cur_irq, NULL)
20273446Smrj 		    == ACPI_PSM_SUCCESS) {
20283446Smrj 
20293446Smrj 			ASSERT(pci_irqp != NULL);
20303446Smrj 			*pci_irqp = cur_irq;
20313446Smrj 			acpi_free_irqlist(irqlistp);
20323446Smrj 			return (ACPI_PSM_SUCCESS);
20333446Smrj 		}
20343446Smrj 
20353446Smrj 		APIC_VERBOSE_IRQ((CE_WARN, "!%s: Could not find the "
20363446Smrj 		    "current irq %d for device %s, instance #%d in ACPI's "
20373446Smrj 		    "list of possible irqs for this device. Picking one from "
20383446Smrj 		    " the latter list.", psm_name, cur_irq, ddi_get_name(dip),
20393446Smrj 		    ddi_get_instance(dip)));
20403446Smrj 	}
20413446Smrj 
20423446Smrj 	if ((prs_irq_listp = apic_choose_irqs_from_prs(irqlistp, dip,
20433446Smrj 	    cur_irq)) == NULL) {
20443446Smrj 
20453446Smrj 		APIC_VERBOSE_IRQ((CE_WARN, "!%s: Could not find a "
20463446Smrj 		    "suitable irq from the list of possible irqs for device "
20473446Smrj 		    "%s, instance #%d in ACPI's list of possible irqs",
20483446Smrj 		    psm_name, ddi_get_name(dip), ddi_get_instance(dip)));
20493446Smrj 
20503446Smrj 		acpi_free_irqlist(irqlistp);
20513446Smrj 		return (ACPI_PSM_FAILURE);
20523446Smrj 	}
20533446Smrj 
20543446Smrj 	acpi_free_irqlist(irqlistp);
20553446Smrj 
20563446Smrj 	for (prs_irq_entp = prs_irq_listp;
20573446Smrj 	    prs_irq_entp != NULL && found_irq == B_FALSE;
20583446Smrj 	    prs_irq_entp = prs_irq_entp->next) {
20593446Smrj 
20603446Smrj 		acpipsmlnkp->acpi_prs_prv = prs_irq_entp->prsprv;
20613446Smrj 		irq = prs_irq_entp->irq;
20623446Smrj 
20633446Smrj 		APIC_VERBOSE_IRQ((CE_CONT, "!%s: Setting irq %d for "
20643446Smrj 		    "device %s instance #%d\n", psm_name, irq,
20653446Smrj 		    ddi_get_name(dip), ddi_get_instance(dip)));
20663446Smrj 
20673446Smrj 		if ((acpi_set_irq_resource(acpipsmlnkp, irq))
20683446Smrj 		    == ACPI_PSM_SUCCESS) {
20693446Smrj 			/*
20703446Smrj 			 * setting irq was successful, check to make sure CRS
20713446Smrj 			 * reflects that. If CRS does not agree with what we
20723446Smrj 			 * set, return the irq that was set.
20733446Smrj 			 */
20743446Smrj 
20753446Smrj 			if (acpi_get_current_irq_resource(acpipsmlnkp, &cur_irq,
20763446Smrj 			    dipintr_flagp) == ACPI_PSM_SUCCESS) {
20773446Smrj 
20783446Smrj 				if (cur_irq != irq)
20793446Smrj 					APIC_VERBOSE_IRQ((CE_WARN,
20803446Smrj 					    "!%s: IRQ resource set "
20813446Smrj 					    "(irqno %d) for device %s "
20823446Smrj 					    "instance #%d, differs from "
20833446Smrj 					    "current setting irqno %d",
20843446Smrj 					    psm_name, irq, ddi_get_name(dip),
20853446Smrj 					    ddi_get_instance(dip), cur_irq));
20863446Smrj 			} else {
20873446Smrj 				/*
20883446Smrj 				 * On at least one system, there was a bug in
20893446Smrj 				 * a DSDT method called by _STA, causing _STA to
20903446Smrj 				 * indicate that the link device was disabled
20913446Smrj 				 * (when, in fact, it was enabled).  Since _SRS
20923446Smrj 				 * succeeded, assume that _CRS is lying and use
20933446Smrj 				 * the iflags from this _PRS interrupt choice.
20943446Smrj 				 * If we're wrong about the flags, the polarity
20953446Smrj 				 * will be incorrect and we may get an interrupt
20963446Smrj 				 * storm, but there's not much else we can do
20973446Smrj 				 * at this point.
20983446Smrj 				 */
20993446Smrj 				*dipintr_flagp = prs_irq_entp->intrflags;
21003446Smrj 			}
21013446Smrj 
21023446Smrj 			/*
21033446Smrj 			 * Return the irq that was set, and not what _CRS
21043446Smrj 			 * reports, since _CRS has been seen to return
21053446Smrj 			 * different IRQs than what was passed to _SRS on some
21063446Smrj 			 * systems (and just not return successfully on others).
21073446Smrj 			 */
21083446Smrj 			cur_irq = irq;
21093446Smrj 			found_irq = B_TRUE;
21103446Smrj 		} else {
21113446Smrj 			APIC_VERBOSE_IRQ((CE_WARN, "!%s: set resource "
21123446Smrj 			    "irq %d failed for device %s instance #%d",
21133446Smrj 			    psm_name, irq, ddi_get_name(dip),
21143446Smrj 			    ddi_get_instance(dip)));
21153446Smrj 
21163446Smrj 			if (cur_irq == -1) {
21173446Smrj 				acpi_destroy_prs_irq_list(&prs_irq_listp);
21183446Smrj 				return (ACPI_PSM_FAILURE);
21193446Smrj 			}
21203446Smrj 		}
21213446Smrj 	}
21223446Smrj 
21233446Smrj 	acpi_destroy_prs_irq_list(&prs_irq_listp);
21243446Smrj 
21253446Smrj 	if (!found_irq)
21263446Smrj 		return (ACPI_PSM_FAILURE);
21273446Smrj 
21283446Smrj 	ASSERT(pci_irqp != NULL);
21293446Smrj 	*pci_irqp = cur_irq;
21303446Smrj 	return (ACPI_PSM_SUCCESS);
21313446Smrj }
21323446Smrj 
21333446Smrj void
ioapic_disable_redirection()21343446Smrj ioapic_disable_redirection()
21353446Smrj {
21363446Smrj 	int ioapic_ix;
21373446Smrj 	int intin_max;
21383446Smrj 	int intin_ix;
21393446Smrj 
21403446Smrj 	/* Disable the I/O APIC redirection entries */
21413446Smrj 	for (ioapic_ix = 0; ioapic_ix < apic_io_max; ioapic_ix++) {
21423446Smrj 
21433446Smrj 		/* Bits 23-16 define the maximum redirection entries */
21443446Smrj 		intin_max = (ioapic_read(ioapic_ix, APIC_VERS_CMD) >> 16)
21453446Smrj 		    & 0xff;
21463446Smrj 
21477656SSherry.Moore@Sun.COM 		for (intin_ix = 0; intin_ix <= intin_max; intin_ix++) {
21483745Ssethg 			/*
21493745Ssethg 			 * The assumption here is that this is safe, even for
21503745Ssethg 			 * systems with IOAPICs that suffer from the hardware
21513745Ssethg 			 * erratum because all devices have been quiesced before
21523745Ssethg 			 * this function is called from apic_shutdown()
21533745Ssethg 			 * (or equivalent). If that assumption turns out to be
21543745Ssethg 			 * false, this mask operation can induce the same
21553745Ssethg 			 * erratum result we're trying to avoid.
21563745Ssethg 			 */
21573446Smrj 			ioapic_write(ioapic_ix, APIC_RDT_CMD + 2 * intin_ix,
21583446Smrj 			    AV_MASK);
21593745Ssethg 		}
21603446Smrj 	}
21613446Smrj }
21623745Ssethg 
21633745Ssethg /*
21643745Ssethg  * Looks for an IOAPIC with the specified physical address in the /ioapics
21653745Ssethg  * node in the device tree (created by the PCI enumerator).
21663745Ssethg  */
21673745Ssethg static boolean_t
apic_is_ioapic_AMD_813x(uint32_t physaddr)21683745Ssethg apic_is_ioapic_AMD_813x(uint32_t physaddr)
21693745Ssethg {
21703745Ssethg 	/*
21713745Ssethg 	 * Look in /ioapics, for the ioapic with
21723745Ssethg 	 * the physical address given
21733745Ssethg 	 */
21743745Ssethg 	dev_info_t *ioapicsnode = ddi_find_devinfo(IOAPICS_NODE_NAME, -1, 0);
21753745Ssethg 	dev_info_t *ioapic_child;
21763745Ssethg 	boolean_t rv = B_FALSE;
21773745Ssethg 	int vid, did;
21783745Ssethg 	uint64_t ioapic_paddr;
21793745Ssethg 	boolean_t done = B_FALSE;
21803745Ssethg 
21813745Ssethg 	if (ioapicsnode == NULL)
21823745Ssethg 		return (B_FALSE);
21833745Ssethg 
21843745Ssethg 	/* Load first child: */
21853745Ssethg 	ioapic_child = ddi_get_child(ioapicsnode);
21863745Ssethg 	while (!done && ioapic_child != 0) { /* Iterate over children */
21873745Ssethg 
21883745Ssethg 		if ((ioapic_paddr = (uint64_t)ddi_prop_get_int64(DDI_DEV_T_ANY,
21893745Ssethg 		    ioapic_child, DDI_PROP_DONTPASS, "reg", 0))
21903745Ssethg 		    != 0 && physaddr == ioapic_paddr) {
21913745Ssethg 
21923745Ssethg 			vid = ddi_prop_get_int(DDI_DEV_T_ANY, ioapic_child,
21933745Ssethg 			    DDI_PROP_DONTPASS, IOAPICS_PROP_VENID, 0);
21943745Ssethg 
21953745Ssethg 			if (vid == VENID_AMD) {
21963745Ssethg 
21973745Ssethg 				did = ddi_prop_get_int(DDI_DEV_T_ANY,
21983745Ssethg 				    ioapic_child, DDI_PROP_DONTPASS,
21993745Ssethg 				    IOAPICS_PROP_DEVID, 0);
22003745Ssethg 
22013745Ssethg 				if (did == DEVID_8131_IOAPIC ||
22023745Ssethg 				    did == DEVID_8132_IOAPIC) {
22033745Ssethg 					rv = B_TRUE;
22043745Ssethg 					done = B_TRUE;
22053745Ssethg 				}
22063745Ssethg 			}
22073745Ssethg 		}
22083745Ssethg 
22093745Ssethg 		if (!done)
22103745Ssethg 			ioapic_child = ddi_get_next_sibling(ioapic_child);
22113745Ssethg 	}
22123745Ssethg 
22133745Ssethg 	/* The ioapics node was held by ddi_find_devinfo, so release it */
22143745Ssethg 	ndi_rele_devi(ioapicsnode);
22153745Ssethg 	return (rv);
22163745Ssethg }
22175295Srandyf 
22185295Srandyf struct apic_state {
22195295Srandyf 	int32_t as_task_reg;
22205295Srandyf 	int32_t as_dest_reg;
22215295Srandyf 	int32_t as_format_reg;
22225295Srandyf 	int32_t as_local_timer;
22235295Srandyf 	int32_t as_pcint_vect;
22245295Srandyf 	int32_t as_int_vect0;
22255295Srandyf 	int32_t as_int_vect1;
22265295Srandyf 	int32_t as_err_vect;
22275295Srandyf 	int32_t as_init_count;
22285295Srandyf 	int32_t as_divide_reg;
22295295Srandyf 	int32_t as_spur_int_reg;
22307515SSeth.Goldberg@Sun.COM 	uint32_t as_ioapic_ids[MAX_IO_APIC];
22315295Srandyf };
22325295Srandyf 
22335295Srandyf 
22347515SSeth.Goldberg@Sun.COM static int
apic_acpi_enter_apicmode(void)22357515SSeth.Goldberg@Sun.COM apic_acpi_enter_apicmode(void)
22367515SSeth.Goldberg@Sun.COM {
22377515SSeth.Goldberg@Sun.COM 	ACPI_OBJECT_LIST	arglist;
22387515SSeth.Goldberg@Sun.COM 	ACPI_OBJECT		arg;
22397515SSeth.Goldberg@Sun.COM 	ACPI_STATUS		status;
22407515SSeth.Goldberg@Sun.COM 
22417515SSeth.Goldberg@Sun.COM 	/* Setup parameter object */
22427515SSeth.Goldberg@Sun.COM 	arglist.Count = 1;
22437515SSeth.Goldberg@Sun.COM 	arglist.Pointer = &arg;
22447515SSeth.Goldberg@Sun.COM 	arg.Type = ACPI_TYPE_INTEGER;
22457515SSeth.Goldberg@Sun.COM 	arg.Integer.Value = ACPI_APIC_MODE;
22467515SSeth.Goldberg@Sun.COM 
22477515SSeth.Goldberg@Sun.COM 	status = AcpiEvaluateObject(NULL, "\\_PIC", &arglist, NULL);
22487515SSeth.Goldberg@Sun.COM 	if (ACPI_FAILURE(status))
22497515SSeth.Goldberg@Sun.COM 		return (PSM_FAILURE);
22507515SSeth.Goldberg@Sun.COM 	else
22517515SSeth.Goldberg@Sun.COM 		return (PSM_SUCCESS);
22527515SSeth.Goldberg@Sun.COM }
22537515SSeth.Goldberg@Sun.COM 
22547515SSeth.Goldberg@Sun.COM 
22555295Srandyf static void
apic_save_state(struct apic_state * sp)22565295Srandyf apic_save_state(struct apic_state *sp)
22575295Srandyf {
225812683SJimmy.Vetayases@oracle.com 	int	i, cpuid;
22597515SSeth.Goldberg@Sun.COM 	ulong_t	iflag;
22605295Srandyf 
22615295Srandyf 	PMD(PMD_SX, ("apic_save_state %p\n", (void *)sp))
22625295Srandyf 	/*
22635295Srandyf 	 * First the local APIC.
22645295Srandyf 	 */
22657282Smishra 	sp->as_task_reg = apic_reg_ops->apic_get_pri();
22667282Smishra 	sp->as_dest_reg =  apic_reg_ops->apic_read(APIC_DEST_REG);
22677282Smishra 	if (apic_mode == LOCAL_APIC)
22687282Smishra 		sp->as_format_reg = apic_reg_ops->apic_read(APIC_FORMAT_REG);
22697282Smishra 	sp->as_local_timer = apic_reg_ops->apic_read(APIC_LOCAL_TIMER);
22707282Smishra 	sp->as_pcint_vect = apic_reg_ops->apic_read(APIC_PCINT_VECT);
22717282Smishra 	sp->as_int_vect0 = apic_reg_ops->apic_read(APIC_INT_VECT0);
22727282Smishra 	sp->as_int_vect1 = apic_reg_ops->apic_read(APIC_INT_VECT1);
22737282Smishra 	sp->as_err_vect = apic_reg_ops->apic_read(APIC_ERR_VECT);
22747282Smishra 	sp->as_init_count = apic_reg_ops->apic_read(APIC_INIT_COUNT);
22757282Smishra 	sp->as_divide_reg = apic_reg_ops->apic_read(APIC_DIVIDE_REG);
22767282Smishra 	sp->as_spur_int_reg = apic_reg_ops->apic_read(APIC_SPUR_INT_REG);
22775295Srandyf 
22785295Srandyf 	/*
22797515SSeth.Goldberg@Sun.COM 	 * If on the boot processor then save the IOAPICs' IDs
22805295Srandyf 	 */
228112683SJimmy.Vetayases@oracle.com 	if ((cpuid = psm_get_cpu_id()) == 0) {
22827515SSeth.Goldberg@Sun.COM 
22837515SSeth.Goldberg@Sun.COM 		iflag = intr_clear();
22847515SSeth.Goldberg@Sun.COM 		lock_set(&apic_ioapic_lock);
22857515SSeth.Goldberg@Sun.COM 
22867515SSeth.Goldberg@Sun.COM 		for (i = 0; i < apic_io_max; i++)
22877515SSeth.Goldberg@Sun.COM 			sp->as_ioapic_ids[i] = ioapic_read(i, APIC_ID_CMD);
22887515SSeth.Goldberg@Sun.COM 
22897515SSeth.Goldberg@Sun.COM 		lock_clear(&apic_ioapic_lock);
22907515SSeth.Goldberg@Sun.COM 		intr_restore(iflag);
22915295Srandyf 	}
229212683SJimmy.Vetayases@oracle.com 
229312683SJimmy.Vetayases@oracle.com 	/* apic_state() is currently invoked only in Suspend/Resume */
229412683SJimmy.Vetayases@oracle.com 	apic_cpus[cpuid].aci_status |= APIC_CPU_SUSPEND;
22955295Srandyf }
22965295Srandyf 
22975295Srandyf static void
apic_restore_state(struct apic_state * sp)22985295Srandyf apic_restore_state(struct apic_state *sp)
22995295Srandyf {
23005295Srandyf 	int	i;
23016336Sbholler 	ulong_t	iflag;
23025295Srandyf 
23035295Srandyf 	/*
23045295Srandyf 	 * First the local APIC.
23055295Srandyf 	 */
23067282Smishra 	apic_reg_ops->apic_write_task_reg(sp->as_task_reg);
23077282Smishra 	if (apic_mode == LOCAL_APIC) {
23087282Smishra 		apic_reg_ops->apic_write(APIC_DEST_REG, sp->as_dest_reg);
23097282Smishra 		apic_reg_ops->apic_write(APIC_FORMAT_REG, sp->as_format_reg);
23107282Smishra 	}
23117282Smishra 	apic_reg_ops->apic_write(APIC_LOCAL_TIMER, sp->as_local_timer);
23127282Smishra 	apic_reg_ops->apic_write(APIC_PCINT_VECT, sp->as_pcint_vect);
23137282Smishra 	apic_reg_ops->apic_write(APIC_INT_VECT0, sp->as_int_vect0);
23147282Smishra 	apic_reg_ops->apic_write(APIC_INT_VECT1, sp->as_int_vect1);
23157282Smishra 	apic_reg_ops->apic_write(APIC_ERR_VECT, sp->as_err_vect);
23167282Smishra 	apic_reg_ops->apic_write(APIC_INIT_COUNT, sp->as_init_count);
23177282Smishra 	apic_reg_ops->apic_write(APIC_DIVIDE_REG, sp->as_divide_reg);
23187282Smishra 	apic_reg_ops->apic_write(APIC_SPUR_INT_REG, sp->as_spur_int_reg);
23195295Srandyf 
23205295Srandyf 	/*
23215295Srandyf 	 * the following only needs to be done once, so we do it on the
23225295Srandyf 	 * boot processor, since we know that we only have one of those
23235295Srandyf 	 */
23245295Srandyf 	if (psm_get_cpu_id() == 0) {
23255295Srandyf 
23265295Srandyf 		iflag = intr_clear();
23275295Srandyf 		lock_set(&apic_ioapic_lock);
23285295Srandyf 
23297515SSeth.Goldberg@Sun.COM 		/* Restore IOAPICs' APIC IDs */
23307515SSeth.Goldberg@Sun.COM 		for (i = 0; i < apic_io_max; i++) {
23317515SSeth.Goldberg@Sun.COM 			ioapic_write(i, APIC_ID_CMD, sp->as_ioapic_ids[i]);
23325295Srandyf 		}
23335295Srandyf 
23345295Srandyf 		lock_clear(&apic_ioapic_lock);
23355295Srandyf 		intr_restore(iflag);
23365295Srandyf 
23377515SSeth.Goldberg@Sun.COM 		/*
23387515SSeth.Goldberg@Sun.COM 		 * Reenter APIC mode before restoring LNK devices
23397515SSeth.Goldberg@Sun.COM 		 */
23407515SSeth.Goldberg@Sun.COM 		(void) apic_acpi_enter_apicmode();
23415295Srandyf 
23425295Srandyf 		/*
23435295Srandyf 		 * restore acpi link device mappings
23445295Srandyf 		 */
23455295Srandyf 		acpi_restore_link_devices();
23465295Srandyf 	}
23475295Srandyf }
23485295Srandyf 
23495295Srandyf /*
23505295Srandyf  * Returns 0 on success
23515295Srandyf  */
23525295Srandyf int
apic_state(psm_state_request_t * rp)23535295Srandyf apic_state(psm_state_request_t *rp)
23545295Srandyf {
23555295Srandyf 	PMD(PMD_SX, ("apic_state "))
23565295Srandyf 	switch (rp->psr_cmd) {
23575295Srandyf 	case PSM_STATE_ALLOC:
23585295Srandyf 		rp->req.psm_state_req.psr_state =
23595295Srandyf 		    kmem_zalloc(sizeof (struct apic_state), KM_NOSLEEP);
23605295Srandyf 		if (rp->req.psm_state_req.psr_state == NULL)
23615295Srandyf 			return (ENOMEM);
23625295Srandyf 		rp->req.psm_state_req.psr_state_size =
23635295Srandyf 		    sizeof (struct apic_state);
23645295Srandyf 		PMD(PMD_SX, (":STATE_ALLOC: state %p, size %lx\n",
23655295Srandyf 		    rp->req.psm_state_req.psr_state,
23665295Srandyf 		    rp->req.psm_state_req.psr_state_size))
23675295Srandyf 		return (0);
23685295Srandyf 
23695295Srandyf 	case PSM_STATE_FREE:
23705295Srandyf 		kmem_free(rp->req.psm_state_req.psr_state,
23715295Srandyf 		    rp->req.psm_state_req.psr_state_size);
23725295Srandyf 		PMD(PMD_SX, (" STATE_FREE: state %p, size %lx\n",
23735295Srandyf 		    rp->req.psm_state_req.psr_state,
23745295Srandyf 		    rp->req.psm_state_req.psr_state_size))
23755295Srandyf 		return (0);
23765295Srandyf 
23775295Srandyf 	case PSM_STATE_SAVE:
23785295Srandyf 		PMD(PMD_SX, (" STATE_SAVE: state %p, size %lx\n",
23795295Srandyf 		    rp->req.psm_state_req.psr_state,
23805295Srandyf 		    rp->req.psm_state_req.psr_state_size))
23815295Srandyf 		apic_save_state(rp->req.psm_state_req.psr_state);
23825295Srandyf 		return (0);
23835295Srandyf 
23845295Srandyf 	case PSM_STATE_RESTORE:
23855295Srandyf 		apic_restore_state(rp->req.psm_state_req.psr_state);
23865295Srandyf 		PMD(PMD_SX, (" STATE_RESTORE: state %p, size %lx\n",
23875295Srandyf 		    rp->req.psm_state_req.psr_state,
23885295Srandyf 		    rp->req.psm_state_req.psr_state_size))
23895295Srandyf 		return (0);
23905295Srandyf 
23915295Srandyf 	default:
23925295Srandyf 		return (EINVAL);
23935295Srandyf 	}
23945295Srandyf }
2395