1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 /*
25  * Copyright (c) 2010, Intel Corporation.
26  * All rights reserved.
27  */
28 
29 /*
30  * PSMI 1.1 extensions are supported only in 2.6 and later versions.
31  * PSMI 1.2 extensions are supported only in 2.7 and later versions.
32  * PSMI 1.3 and 1.4 extensions are supported in Solaris 10.
33  * PSMI 1.5 extensions are supported in Solaris Nevada.
34  * PSMI 1.6 extensions are supported in Solaris Nevada.
35  * PSMI 1.7 extensions are supported in Solaris Nevada.
36  */
37 #define	PSMI_1_7
38 
39 #include <sys/processor.h>
40 #include <sys/time.h>
41 #include <sys/psm.h>
42 #include <sys/smp_impldefs.h>
43 #include <sys/cram.h>
44 #include <sys/acpi/acpi.h>
45 #include <sys/acpica.h>
46 #include <sys/psm_common.h>
47 #include <sys/apic.h>
48 #include <sys/pit.h>
49 #include <sys/ddi.h>
50 #include <sys/sunddi.h>
51 #include <sys/ddi_impldefs.h>
52 #include <sys/pci.h>
53 #include <sys/promif.h>
54 #include <sys/x86_archext.h>
55 #include <sys/cpc_impl.h>
56 #include <sys/uadmin.h>
57 #include <sys/panic.h>
58 #include <sys/debug.h>
59 #include <sys/archsystm.h>
60 #include <sys/trap.h>
61 #include <sys/machsystm.h>
62 #include <sys/cpuvar.h>
63 #include <sys/rm_platter.h>
64 #include <sys/privregs.h>
65 #include <sys/cyclic.h>
66 #include <sys/note.h>
67 #include <sys/pci_intr_lib.h>
68 #include <sys/sunndi.h>
69 #if !defined(__xpv)
70 #include <sys/hpet.h>
71 #include <sys/clock.h>
72 #endif
73 
74 /*
75  *	Local Function Prototypes
76  */
77 static int apic_handle_defconf();
78 static int apic_parse_mpct(caddr_t mpct, int bypass);
79 static struct apic_mpfps_hdr *apic_find_fps_sig(caddr_t fptr, int size);
80 static int apic_checksum(caddr_t bptr, int len);
81 static int apic_find_bus_type(char *bus);
82 static int apic_find_bus(int busid);
83 static struct apic_io_intr *apic_find_io_intr(int irqno);
84 static int apic_find_free_irq(int start, int end);
85 struct apic_io_intr *apic_find_io_intr_w_busid(int irqno, int busid);
86 static void apic_set_pwroff_method_from_mpcnfhdr(struct apic_mp_cnf_hdr *hdrp);
87 static void apic_free_apic_cpus(void);
88 static boolean_t apic_is_ioapic_AMD_813x(uint32_t physaddr);
89 static int apic_acpi_enter_apicmode(void);
90 
91 int apic_handle_pci_pci_bridge(dev_info_t *idip, int child_devno,
92     int child_ipin, struct apic_io_intr **intrp);
93 int apic_find_bus_id(int bustype);
94 int apic_find_intin(uchar_t ioapic, uchar_t intin);
95 void apic_record_rdt_entry(apic_irq_t *irqptr, int irq);
96 
97 int apic_debug_mps_id = 0;	/* 1 - print MPS ID strings */
98 
99 /* ACPI SCI interrupt configuration; -1 if SCI not used */
100 int apic_sci_vect = -1;
101 iflag_t apic_sci_flags;
102 
103 #if !defined(__xpv)
104 /* ACPI HPET interrupt configuration; -1 if HPET not used */
105 int apic_hpet_vect = -1;
106 iflag_t apic_hpet_flags;
107 #endif
108 
109 /*
110  * psm name pointer
111  */
112 char *psm_name;
113 
114 /* ACPI support routines */
115 static int acpi_probe(char *);
116 static int apic_acpi_irq_configure(acpi_psm_lnk_t *acpipsmlnkp, dev_info_t *dip,
117     int *pci_irqp, iflag_t *intr_flagp);
118 
119 int apic_acpi_translate_pci_irq(dev_info_t *dip, int busid, int devid,
120     int ipin, int *pci_irqp, iflag_t *intr_flagp);
121 uchar_t acpi_find_ioapic(int irq);
122 static int acpi_intr_compatible(iflag_t iflag1, iflag_t iflag2);
123 
124 /* Max wait time (in repetitions) for flags to clear in an RDT entry. */
125 int apic_max_reps_clear_pending = 1000;
126 
127 int	apic_intr_policy = INTR_ROUND_ROBIN;
128 
129 int	apic_next_bind_cpu = 1; /* For round robin assignment */
130 				/* start with cpu 1 */
131 
132 /*
133  * If enabled, the distribution works as follows:
134  * On every interrupt entry, the current ipl for the CPU is set in cpu_info
135  * and the irq corresponding to the ipl is also set in the aci_current array.
136  * interrupt exit and setspl (due to soft interrupts) will cause the current
137  * ipl to be be changed. This is cache friendly as these frequently used
138  * paths write into a per cpu structure.
139  *
140  * Sampling is done by checking the structures for all CPUs and incrementing
141  * the busy field of the irq (if any) executing on each CPU and the busy field
142  * of the corresponding CPU.
143  * In periodic mode this is done on every clock interrupt.
144  * In one-shot mode, this is done thru a cyclic with an interval of
145  * apic_redistribute_sample_interval (default 10 milli sec).
146  *
147  * Every apic_sample_factor_redistribution times we sample, we do computations
148  * to decide which interrupt needs to be migrated (see comments
149  * before apic_intr_redistribute().
150  */
151 
152 /*
153  * Following 3 variables start as % and can be patched or set using an
154  * API to be defined in future. They will be scaled to
155  * sample_factor_redistribution which is in turn set to hertz+1 (in periodic
156  * mode), or 101 in one-shot mode to stagger it away from one sec processing
157  */
158 
159 int	apic_int_busy_mark = 60;
160 int	apic_int_free_mark = 20;
161 int	apic_diff_for_redistribution = 10;
162 
163 /* sampling interval for interrupt redistribution for dynamic migration */
164 int	apic_redistribute_sample_interval = NANOSEC / 100; /* 10 millisec */
165 
166 /*
167  * number of times we sample before deciding to redistribute interrupts
168  * for dynamic migration
169  */
170 int	apic_sample_factor_redistribution = 101;
171 
172 int	apic_redist_cpu_skip = 0;
173 int	apic_num_imbalance = 0;
174 int	apic_num_rebind = 0;
175 
176 /*
177  * Maximum number of APIC CPUs in the system, -1 indicates that dynamic
178  * allocation of CPU ids is disabled.
179  */
180 int 	apic_max_nproc = -1;
181 int	apic_nproc = 0;
182 size_t	apic_cpus_size = 0;
183 int	apic_defconf = 0;
184 int	apic_irq_translate = 0;
185 int	apic_spec_rev = 0;
186 int	apic_imcrp = 0;
187 
188 int	apic_use_acpi = 1;	/* 1 = use ACPI, 0 = don't use ACPI */
189 int	apic_use_acpi_madt_only = 0;	/* 1=ONLY use MADT from ACPI */
190 
191 /*
192  * For interrupt link devices, if apic_unconditional_srs is set, an irq resource
193  * will be assigned (via _SRS). If it is not set, use the current
194  * irq setting (via _CRS), but only if that irq is in the set of possible
195  * irqs (returned by _PRS) for the device.
196  */
197 int	apic_unconditional_srs = 1;
198 
199 /*
200  * For interrupt link devices, if apic_prefer_crs is set when we are
201  * assigning an IRQ resource to a device, prefer the current IRQ setting
202  * over other possible irq settings under same conditions.
203  */
204 
205 int	apic_prefer_crs = 1;
206 
207 uchar_t apic_io_id[MAX_IO_APIC];
208 volatile uint32_t *apicioadr[MAX_IO_APIC];
209 uchar_t	apic_io_ver[MAX_IO_APIC];
210 uchar_t	apic_io_vectbase[MAX_IO_APIC];
211 uchar_t	apic_io_vectend[MAX_IO_APIC];
212 uchar_t apic_reserved_irqlist[MAX_ISA_IRQ + 1];
213 uint32_t apic_physaddr[MAX_IO_APIC];
214 
215 boolean_t ioapic_mask_workaround[MAX_IO_APIC];
216 
217 /*
218  * First available slot to be used as IRQ index into the apic_irq_table
219  * for those interrupts (like MSI/X) that don't have a physical IRQ.
220  */
221 int apic_first_avail_irq  = APIC_FIRST_FREE_IRQ;
222 
223 /*
224  * apic_ioapic_lock protects the ioapics (reg select), the status, temp_bound
225  * and bound elements of cpus_info and the temp_cpu element of irq_struct
226  */
227 lock_t	apic_ioapic_lock;
228 
229 int	apic_io_max = 0;	/* no. of i/o apics enabled */
230 
231 struct apic_io_intr *apic_io_intrp = NULL;
232 static	struct apic_bus	*apic_busp;
233 
234 uchar_t	apic_resv_vector[MAXIPL+1];
235 
236 char	apic_level_intr[APIC_MAX_VECTOR+1];
237 
238 uint32_t	eisa_level_intr_mask = 0;
239 	/* At least MSB will be set if EISA bus */
240 
241 int	apic_pci_bus_total = 0;
242 uchar_t	apic_single_pci_busid = 0;
243 
244 /*
245  * airq_mutex protects additions to the apic_irq_table - the first
246  * pointer and any airq_nexts off of that one. It also protects
247  * apic_max_device_irq & apic_min_device_irq. It also guarantees
248  * that share_id is unique as new ids are generated only when new
249  * irq_t structs are linked in. Once linked in the structs are never
250  * deleted. temp_cpu & mps_intr_index field indicate if it is programmed
251  * or allocated. Note that there is a slight gap between allocating in
252  * apic_introp_xlate and programming in addspl.
253  */
254 kmutex_t	airq_mutex;
255 apic_irq_t	*apic_irq_table[APIC_MAX_VECTOR+1];
256 int		apic_max_device_irq = 0;
257 int		apic_min_device_irq = APIC_MAX_VECTOR;
258 
259 typedef struct prs_irq_list_ent {
260 	int			list_prio;
261 	int32_t			irq;
262 	iflag_t			intrflags;
263 	acpi_prs_private_t	prsprv;
264 	struct prs_irq_list_ent	*next;
265 } prs_irq_list_t;
266 
267 
268 /*
269  * ACPI variables
270  */
271 /* 1 = acpi is enabled & working, 0 = acpi is not enabled or not there */
272 int apic_enable_acpi = 0;
273 
274 /* ACPI Multiple APIC Description Table ptr */
275 static	ACPI_TABLE_MADT *acpi_mapic_dtp = NULL;
276 
277 /* ACPI Interrupt Source Override Structure ptr */
278 ACPI_MADT_INTERRUPT_OVERRIDE *acpi_isop = NULL;
279 int acpi_iso_cnt = 0;
280 
281 /* ACPI Non-maskable Interrupt Sources ptr */
282 static	ACPI_MADT_NMI_SOURCE *acpi_nmi_sp = NULL;
283 static	int acpi_nmi_scnt = 0;
284 static	ACPI_MADT_LOCAL_APIC_NMI *acpi_nmi_cp = NULL;
285 static	int acpi_nmi_ccnt = 0;
286 
287 /*
288  * The following added to identify a software poweroff method if available.
289  */
290 
291 static struct {
292 	int	poweroff_method;
293 	char	oem_id[APIC_MPS_OEM_ID_LEN + 1];	/* MAX + 1 for NULL */
294 	char	prod_id[APIC_MPS_PROD_ID_LEN + 1];	/* MAX + 1 for NULL */
295 } apic_mps_ids[] = {
296 	{ APIC_POWEROFF_VIA_RTC,	"INTEL",	"ALDER" },   /* 4300 */
297 	{ APIC_POWEROFF_VIA_RTC,	"NCR",		"AMC" },    /* 4300 */
298 	{ APIC_POWEROFF_VIA_ASPEN_BMC,	"INTEL",	"A450NX" },  /* 4400? */
299 	{ APIC_POWEROFF_VIA_ASPEN_BMC,	"INTEL",	"AD450NX" }, /* 4400 */
300 	{ APIC_POWEROFF_VIA_ASPEN_BMC,	"INTEL",	"AC450NX" }, /* 4400R */
301 	{ APIC_POWEROFF_VIA_SITKA_BMC,	"INTEL",	"S450NX" },  /* S50  */
302 	{ APIC_POWEROFF_VIA_SITKA_BMC,	"INTEL",	"SC450NX" }  /* S50? */
303 };
304 
305 int	apic_poweroff_method = APIC_POWEROFF_NONE;
306 
307 /*
308  * Auto-configuration routines
309  */
310 
311 /*
312  * Look at MPSpec 1.4 (Intel Order # 242016-005) for details of what we do here
313  * May work with 1.1 - but not guaranteed.
314  * According to the MP Spec, the MP floating pointer structure
315  * will be searched in the order described below:
316  * 1. In the first kilobyte of Extended BIOS Data Area (EBDA)
317  * 2. Within the last kilobyte of system base memory
318  * 3. In the BIOS ROM address space between 0F0000h and 0FFFFh
319  * Once we find the right signature with proper checksum, we call
320  * either handle_defconf or parse_mpct to get all info necessary for
321  * subsequent operations.
322  */
323 int
324 apic_probe_common(char *modname)
325 {
326 	uint32_t mpct_addr, ebda_start = 0, base_mem_end;
327 	caddr_t	biosdatap;
328 	caddr_t	mpct = 0;
329 	caddr_t	fptr;
330 	int	i, mpct_size, mapsize, retval = PSM_FAILURE;
331 	ushort_t	ebda_seg, base_mem_size;
332 	struct	apic_mpfps_hdr	*fpsp;
333 	struct	apic_mp_cnf_hdr	*hdrp;
334 	int bypass_cpu_and_ioapics_in_mptables;
335 	int acpi_user_options;
336 
337 	if (apic_forceload < 0)
338 		return (retval);
339 
340 	/*
341 	 * Remember who we are
342 	 */
343 	psm_name = modname;
344 
345 	/* Allow override for MADT-only mode */
346 	acpi_user_options = ddi_prop_get_int(DDI_DEV_T_ANY, ddi_root_node(), 0,
347 	    "acpi-user-options", 0);
348 	apic_use_acpi_madt_only = ((acpi_user_options & ACPI_OUSER_MADT) != 0);
349 
350 	/* Allow apic_use_acpi to override MADT-only mode */
351 	if (!apic_use_acpi)
352 		apic_use_acpi_madt_only = 0;
353 
354 	retval = acpi_probe(modname);
355 
356 	/*
357 	 * mapin the bios data area 40:0
358 	 * 40:13h - two-byte location reports the base memory size
359 	 * 40:0Eh - two-byte location for the exact starting address of
360 	 *	    the EBDA segment for EISA
361 	 */
362 	biosdatap = psm_map_phys(0x400, 0x20, PROT_READ);
363 	if (!biosdatap)
364 		goto apic_ret;
365 	fpsp = (struct apic_mpfps_hdr *)NULL;
366 	mapsize = MPFPS_RAM_WIN_LEN;
367 	/*LINTED: pointer cast may result in improper alignment */
368 	ebda_seg = *((ushort_t *)(biosdatap+0xe));
369 	/* check the 1k of EBDA */
370 	if (ebda_seg) {
371 		ebda_start = ((uint32_t)ebda_seg) << 4;
372 		fptr = psm_map_phys(ebda_start, MPFPS_RAM_WIN_LEN, PROT_READ);
373 		if (fptr) {
374 			if (!(fpsp =
375 			    apic_find_fps_sig(fptr, MPFPS_RAM_WIN_LEN)))
376 				psm_unmap_phys(fptr, MPFPS_RAM_WIN_LEN);
377 		}
378 	}
379 	/* If not in EBDA, check the last k of system base memory */
380 	if (!fpsp) {
381 		/*LINTED: pointer cast may result in improper alignment */
382 		base_mem_size = *((ushort_t *)(biosdatap + 0x13));
383 
384 		if (base_mem_size > 512)
385 			base_mem_end = 639 * 1024;
386 		else
387 			base_mem_end = 511 * 1024;
388 		/* if ebda == last k of base mem, skip to check BIOS ROM */
389 		if (base_mem_end != ebda_start) {
390 
391 			fptr = psm_map_phys(base_mem_end, MPFPS_RAM_WIN_LEN,
392 			    PROT_READ);
393 
394 			if (fptr) {
395 				if (!(fpsp = apic_find_fps_sig(fptr,
396 				    MPFPS_RAM_WIN_LEN)))
397 					psm_unmap_phys(fptr, MPFPS_RAM_WIN_LEN);
398 			}
399 		}
400 	}
401 	psm_unmap_phys(biosdatap, 0x20);
402 
403 	/* If still cannot find it, check the BIOS ROM space */
404 	if (!fpsp) {
405 		mapsize = MPFPS_ROM_WIN_LEN;
406 		fptr = psm_map_phys(MPFPS_ROM_WIN_START,
407 		    MPFPS_ROM_WIN_LEN, PROT_READ);
408 		if (fptr) {
409 			if (!(fpsp =
410 			    apic_find_fps_sig(fptr, MPFPS_ROM_WIN_LEN))) {
411 				psm_unmap_phys(fptr, MPFPS_ROM_WIN_LEN);
412 				goto apic_ret;
413 			}
414 		}
415 	}
416 
417 	if (apic_checksum((caddr_t)fpsp, fpsp->mpfps_length * 16) != 0) {
418 		psm_unmap_phys(fptr, MPFPS_ROM_WIN_LEN);
419 		goto apic_ret;
420 	}
421 
422 	apic_spec_rev = fpsp->mpfps_spec_rev;
423 	if ((apic_spec_rev != 04) && (apic_spec_rev != 01)) {
424 		psm_unmap_phys(fptr, MPFPS_ROM_WIN_LEN);
425 		goto apic_ret;
426 	}
427 
428 	/* check IMCR is present or not */
429 	apic_imcrp = fpsp->mpfps_featinfo2 & MPFPS_FEATINFO2_IMCRP;
430 
431 	/* check default configuration (dual CPUs) */
432 	if ((apic_defconf = fpsp->mpfps_featinfo1) != 0) {
433 		psm_unmap_phys(fptr, mapsize);
434 		if ((retval = apic_handle_defconf()) != PSM_SUCCESS)
435 			return (retval);
436 
437 		goto apic_ret;
438 	}
439 
440 	/* MP Configuration Table */
441 	mpct_addr = (uint32_t)(fpsp->mpfps_mpct_paddr);
442 
443 	psm_unmap_phys(fptr, mapsize); /* unmap floating ptr struct */
444 
445 	/*
446 	 * Map in enough memory for the MP Configuration Table Header.
447 	 * Use this table to read the total length of the BIOS data and
448 	 * map in all the info
449 	 */
450 	/*LINTED: pointer cast may result in improper alignment */
451 	hdrp = (struct apic_mp_cnf_hdr *)psm_map_phys(mpct_addr,
452 	    sizeof (struct apic_mp_cnf_hdr), PROT_READ);
453 	if (!hdrp)
454 		goto apic_ret;
455 
456 	/* check mp configuration table signature PCMP */
457 	if (hdrp->mpcnf_sig != 0x504d4350) {
458 		psm_unmap_phys((caddr_t)hdrp, sizeof (struct apic_mp_cnf_hdr));
459 		goto apic_ret;
460 	}
461 	mpct_size = (int)hdrp->mpcnf_tbl_length;
462 
463 	apic_set_pwroff_method_from_mpcnfhdr(hdrp);
464 
465 	psm_unmap_phys((caddr_t)hdrp, sizeof (struct apic_mp_cnf_hdr));
466 
467 	if ((retval == PSM_SUCCESS) && !apic_use_acpi_madt_only) {
468 		/* This is an ACPI machine No need for further checks */
469 		goto apic_ret;
470 	}
471 
472 	/*
473 	 * Map in the entries for this machine, ie. Processor
474 	 * Entry Tables, Bus Entry Tables, etc.
475 	 * They are in fixed order following one another
476 	 */
477 	mpct = psm_map_phys(mpct_addr, mpct_size, PROT_READ);
478 	if (!mpct)
479 		goto apic_ret;
480 
481 	if (apic_checksum(mpct, mpct_size) != 0)
482 		goto apic_fail1;
483 
484 	/*LINTED: pointer cast may result in improper alignment */
485 	hdrp = (struct apic_mp_cnf_hdr *)mpct;
486 	apicadr = (uint32_t *)mapin_apic((uint32_t)hdrp->mpcnf_local_apic,
487 	    APIC_LOCAL_MEMLEN, PROT_READ | PROT_WRITE);
488 	if (!apicadr)
489 		goto apic_fail1;
490 
491 	/* Parse all information in the tables */
492 	bypass_cpu_and_ioapics_in_mptables = (retval == PSM_SUCCESS);
493 	if (apic_parse_mpct(mpct, bypass_cpu_and_ioapics_in_mptables) ==
494 	    PSM_SUCCESS) {
495 		retval = PSM_SUCCESS;
496 		goto apic_ret;
497 	}
498 
499 apic_fail1:
500 	psm_unmap_phys(mpct, mpct_size);
501 	mpct = NULL;
502 
503 apic_ret:
504 	if (retval == PSM_SUCCESS) {
505 		extern int apic_ioapic_method_probe();
506 
507 		if ((retval = apic_ioapic_method_probe()) == PSM_SUCCESS)
508 			return (PSM_SUCCESS);
509 	}
510 
511 	for (i = 0; i < apic_io_max; i++)
512 		mapout_ioapic((caddr_t)apicioadr[i], APIC_IO_MEMLEN);
513 	if (apic_cpus) {
514 		kmem_free(apic_cpus, apic_cpus_size);
515 		apic_cpus = NULL;
516 	}
517 	if (apicadr) {
518 		mapout_apic((caddr_t)apicadr, APIC_LOCAL_MEMLEN);
519 		apicadr = NULL;
520 	}
521 	if (mpct)
522 		psm_unmap_phys(mpct, mpct_size);
523 
524 	return (retval);
525 }
526 
527 static void
528 apic_set_pwroff_method_from_mpcnfhdr(struct apic_mp_cnf_hdr *hdrp)
529 {
530 	int	i;
531 
532 	for (i = 0; i < (sizeof (apic_mps_ids) / sizeof (apic_mps_ids[0]));
533 	    i++) {
534 		if ((strncmp(hdrp->mpcnf_oem_str, apic_mps_ids[i].oem_id,
535 		    strlen(apic_mps_ids[i].oem_id)) == 0) &&
536 		    (strncmp(hdrp->mpcnf_prod_str, apic_mps_ids[i].prod_id,
537 		    strlen(apic_mps_ids[i].prod_id)) == 0)) {
538 
539 			apic_poweroff_method = apic_mps_ids[i].poweroff_method;
540 			break;
541 		}
542 	}
543 
544 	if (apic_debug_mps_id != 0) {
545 		cmn_err(CE_CONT, "%s: MPS OEM ID = '%c%c%c%c%c%c%c%c'"
546 		    "Product ID = '%c%c%c%c%c%c%c%c%c%c%c%c'\n",
547 		    psm_name,
548 		    hdrp->mpcnf_oem_str[0],
549 		    hdrp->mpcnf_oem_str[1],
550 		    hdrp->mpcnf_oem_str[2],
551 		    hdrp->mpcnf_oem_str[3],
552 		    hdrp->mpcnf_oem_str[4],
553 		    hdrp->mpcnf_oem_str[5],
554 		    hdrp->mpcnf_oem_str[6],
555 		    hdrp->mpcnf_oem_str[7],
556 		    hdrp->mpcnf_prod_str[0],
557 		    hdrp->mpcnf_prod_str[1],
558 		    hdrp->mpcnf_prod_str[2],
559 		    hdrp->mpcnf_prod_str[3],
560 		    hdrp->mpcnf_prod_str[4],
561 		    hdrp->mpcnf_prod_str[5],
562 		    hdrp->mpcnf_prod_str[6],
563 		    hdrp->mpcnf_prod_str[7],
564 		    hdrp->mpcnf_prod_str[8],
565 		    hdrp->mpcnf_prod_str[9],
566 		    hdrp->mpcnf_prod_str[10],
567 		    hdrp->mpcnf_prod_str[11]);
568 	}
569 }
570 
571 static void
572 apic_free_apic_cpus(void)
573 {
574 	if (apic_cpus != NULL) {
575 		kmem_free(apic_cpus, apic_cpus_size);
576 		apic_cpus = NULL;
577 		apic_cpus_size = 0;
578 	}
579 }
580 
581 static int
582 acpi_probe(char *modname)
583 {
584 	int			i, intmax, index;
585 	uint32_t		id, ver;
586 	int			acpi_verboseflags = 0;
587 	int			madt_seen, madt_size;
588 	ACPI_SUBTABLE_HEADER		*ap;
589 	ACPI_MADT_LOCAL_APIC	*mpa;
590 	ACPI_MADT_LOCAL_X2APIC	*mpx2a;
591 	ACPI_MADT_IO_APIC		*mia;
592 	ACPI_MADT_IO_SAPIC		*misa;
593 	ACPI_MADT_INTERRUPT_OVERRIDE	*mio;
594 	ACPI_MADT_NMI_SOURCE		*mns;
595 	ACPI_MADT_INTERRUPT_SOURCE	*mis;
596 	ACPI_MADT_LOCAL_APIC_NMI	*mlan;
597 	ACPI_MADT_LOCAL_X2APIC_NMI	*mx2alan;
598 	ACPI_MADT_LOCAL_APIC_OVERRIDE	*mao;
599 	int			sci;
600 	iflag_t			sci_flags;
601 	volatile uint32_t	*ioapic;
602 	int			ioapic_ix;
603 	uint32_t		*local_ids;
604 	uint32_t		*proc_ids;
605 	uchar_t			hid;
606 	int			warned = 0;
607 
608 	if (!apic_use_acpi)
609 		return (PSM_FAILURE);
610 
611 	if (AcpiGetTable(ACPI_SIG_MADT, 1,
612 	    (ACPI_TABLE_HEADER **) &acpi_mapic_dtp) != AE_OK)
613 		return (PSM_FAILURE);
614 
615 	apicadr = mapin_apic((uint32_t)acpi_mapic_dtp->Address,
616 	    APIC_LOCAL_MEMLEN, PROT_READ | PROT_WRITE);
617 	if (!apicadr)
618 		return (PSM_FAILURE);
619 
620 	if ((local_ids = (uint32_t *)kmem_zalloc(NCPU * sizeof (uint32_t),
621 	    KM_NOSLEEP)) == NULL)
622 		return (PSM_FAILURE);
623 
624 	if ((proc_ids = (uint32_t *)kmem_zalloc(NCPU * sizeof (uint32_t),
625 	    KM_NOSLEEP)) == NULL) {
626 		kmem_free(local_ids, NCPU * sizeof (uint32_t));
627 		return (PSM_FAILURE);
628 	}
629 
630 	id = apic_reg_ops->apic_read(APIC_LID_REG);
631 	local_ids[0] = (uchar_t)(id >> 24);
632 	apic_nproc = index = 1;
633 	apic_io_max = 0;
634 
635 	ap = (ACPI_SUBTABLE_HEADER *) (acpi_mapic_dtp + 1);
636 	madt_size = acpi_mapic_dtp->Header.Length;
637 	madt_seen = sizeof (*acpi_mapic_dtp);
638 
639 	while (madt_seen < madt_size) {
640 		switch (ap->Type) {
641 		case ACPI_MADT_TYPE_LOCAL_APIC:
642 			mpa = (ACPI_MADT_LOCAL_APIC *) ap;
643 			if (mpa->LapicFlags & ACPI_MADT_ENABLED) {
644 				if (mpa->Id == local_ids[0]) {
645 					ASSERT(index == 1);
646 					proc_ids[0] = mpa->ProcessorId;
647 				} else if (apic_nproc < NCPU && use_mp &&
648 				    apic_nproc < boot_ncpus) {
649 					local_ids[index] = mpa->Id;
650 					proc_ids[index] = mpa->ProcessorId;
651 					index++;
652 					apic_nproc++;
653 				} else if (apic_nproc == NCPU && !warned) {
654 					cmn_err(CE_WARN, "%s: CPU limit "
655 					    "exceeded"
656 #if !defined(__amd64)
657 					    " for 32-bit mode"
658 #endif
659 					    "; Solaris will use %d CPUs.",
660 					    psm_name,  NCPU);
661 					warned = 1;
662 				}
663 			}
664 			break;
665 
666 		case ACPI_MADT_TYPE_IO_APIC:
667 			mia = (ACPI_MADT_IO_APIC *) ap;
668 			if (apic_io_max < MAX_IO_APIC) {
669 				ioapic_ix = apic_io_max;
670 				apic_io_id[apic_io_max] = mia->Id;
671 				apic_io_vectbase[apic_io_max] =
672 				    mia->GlobalIrqBase;
673 				apic_physaddr[apic_io_max] =
674 				    (uint32_t)mia->Address;
675 				ioapic = apicioadr[apic_io_max] =
676 				    mapin_ioapic((uint32_t)mia->Address,
677 				    APIC_IO_MEMLEN, PROT_READ | PROT_WRITE);
678 				if (!ioapic)
679 					goto cleanup;
680 				ioapic_mask_workaround[apic_io_max] =
681 				    apic_is_ioapic_AMD_813x(mia->Address);
682 				apic_io_max++;
683 			}
684 			break;
685 
686 		case ACPI_MADT_TYPE_INTERRUPT_OVERRIDE:
687 			mio = (ACPI_MADT_INTERRUPT_OVERRIDE *) ap;
688 			if (acpi_isop == NULL)
689 				acpi_isop = mio;
690 			acpi_iso_cnt++;
691 			break;
692 
693 		case ACPI_MADT_TYPE_NMI_SOURCE:
694 			/* UNIMPLEMENTED */
695 			mns = (ACPI_MADT_NMI_SOURCE *) ap;
696 			if (acpi_nmi_sp == NULL)
697 				acpi_nmi_sp = mns;
698 			acpi_nmi_scnt++;
699 
700 			cmn_err(CE_NOTE, "!apic: nmi source: %d 0x%x\n",
701 			    mns->GlobalIrq, mns->IntiFlags);
702 			break;
703 
704 		case ACPI_MADT_TYPE_LOCAL_APIC_NMI:
705 			/* UNIMPLEMENTED */
706 			mlan = (ACPI_MADT_LOCAL_APIC_NMI *) ap;
707 			if (acpi_nmi_cp == NULL)
708 				acpi_nmi_cp = mlan;
709 			acpi_nmi_ccnt++;
710 
711 			cmn_err(CE_NOTE, "!apic: local nmi: %d 0x%x %d\n",
712 			    mlan->ProcessorId, mlan->IntiFlags,
713 			    mlan->Lint);
714 			break;
715 
716 		case ACPI_MADT_TYPE_LOCAL_APIC_OVERRIDE:
717 			/* UNIMPLEMENTED */
718 			mao = (ACPI_MADT_LOCAL_APIC_OVERRIDE *) ap;
719 			cmn_err(CE_NOTE, "!apic: address override: %lx\n",
720 			    (long)mao->Address);
721 			break;
722 
723 		case ACPI_MADT_TYPE_IO_SAPIC:
724 			/* UNIMPLEMENTED */
725 			misa = (ACPI_MADT_IO_SAPIC *) ap;
726 
727 			cmn_err(CE_NOTE, "!apic: io sapic: %d %d %lx\n",
728 			    misa->Id, misa->GlobalIrqBase,
729 			    (long)misa->Address);
730 			break;
731 
732 		case ACPI_MADT_TYPE_INTERRUPT_SOURCE:
733 			/* UNIMPLEMENTED */
734 			mis = (ACPI_MADT_INTERRUPT_SOURCE *) ap;
735 
736 			cmn_err(CE_NOTE,
737 			    "!apic: irq source: %d %d %d 0x%x %d %d\n",
738 			    mis->Id, mis->Eid, mis->GlobalIrq,
739 			    mis->IntiFlags, mis->Type,
740 			    mis->IoSapicVector);
741 			break;
742 
743 		case ACPI_MADT_TYPE_LOCAL_X2APIC:
744 			mpx2a = (ACPI_MADT_LOCAL_X2APIC *) ap;
745 
746 			/*
747 			 * All logical processors with APIC ID values
748 			 * of 255 and greater will have their APIC
749 			 * reported through Processor X2APIC structure.
750 			 * All logical processors with APIC ID less than
751 			 * 255 will have their APIC reported through
752 			 * Processor Local APIC.
753 			 */
754 			if ((mpx2a->LapicFlags & ACPI_MADT_ENABLED) &&
755 			    (mpx2a->LocalApicId >> 8)) {
756 				if (apic_nproc < NCPU && use_mp &&
757 				    apic_nproc < boot_ncpus) {
758 					local_ids[index] = mpx2a->LocalApicId;
759 					proc_ids[index] = mpa->ProcessorId;
760 					index++;
761 					apic_nproc++;
762 				} else if (apic_nproc == NCPU && !warned) {
763 					cmn_err(CE_WARN, "%s: CPU limit "
764 					    "exceeded"
765 #if !defined(__amd64)
766 					    " for 32-bit mode"
767 #endif
768 					    "; Solaris will use %d CPUs.",
769 					    psm_name,  NCPU);
770 					warned = 1;
771 				}
772 			}
773 
774 			break;
775 
776 		case ACPI_MADT_TYPE_LOCAL_X2APIC_NMI:
777 			/* UNIMPLEMENTED */
778 			mx2alan = (ACPI_MADT_LOCAL_X2APIC_NMI *) ap;
779 			if (mx2alan->Uid >> 8)
780 				acpi_nmi_ccnt++;
781 
782 #ifdef	DEBUG
783 			cmn_err(CE_NOTE,
784 			    "!apic: local x2apic nmi: %d 0x%x %d\n",
785 			    mx2alan->Uid, mx2alan->IntiFlags, mx2alan->Lint);
786 #endif
787 
788 			break;
789 
790 		case ACPI_MADT_TYPE_RESERVED:
791 		default:
792 			break;
793 		}
794 
795 		/* advance to next entry */
796 		madt_seen += ap->Length;
797 		ap = (ACPI_SUBTABLE_HEADER *)(((char *)ap) + ap->Length);
798 	}
799 
800 	/*
801 	 * allocate enough space for possible hot-adding of CPUs.
802 	 * max_ncpus may be less than apic_nproc if it's set by user.
803 	 */
804 	if (plat_dr_support_cpu()) {
805 		apic_max_nproc = max_ncpus;
806 	}
807 	apic_cpus_size = max(apic_nproc, max_ncpus) * sizeof (*apic_cpus);
808 	if ((apic_cpus = kmem_zalloc(apic_cpus_size, KM_NOSLEEP)) == NULL)
809 		goto cleanup;
810 
811 	/*
812 	 * ACPI doesn't provide the local apic ver, get it directly from the
813 	 * local apic
814 	 */
815 	ver = apic_reg_ops->apic_read(APIC_VERS_REG);
816 	for (i = 0; i < apic_nproc; i++) {
817 		apic_cpus[i].aci_local_id = local_ids[i];
818 		apic_cpus[i].aci_local_ver = (uchar_t)(ver & 0xFF);
819 		apic_cpus[i].aci_processor_id = proc_ids[i];
820 		/* Only build mapping info for CPUs present at boot. */
821 		if (i < boot_ncpus)
822 			(void) acpica_map_cpu(i, proc_ids[i]);
823 	}
824 
825 	/*
826 	 * To support CPU dynamic reconfiguration, the apic CPU info structure
827 	 * for each possible CPU will be pre-allocated at boot time.
828 	 * The state for each apic CPU info structure will be assigned according
829 	 * to the following rules:
830 	 * Rule 1:
831 	 * 	Slot index range: [0, min(apic_nproc, boot_ncpus))
832 	 *	State flags: 0
833 	 *	Note: cpu exists and will be configured/enabled at boot time
834 	 * Rule 2:
835 	 * 	Slot index range: [boot_ncpus, apic_nproc)
836 	 *	State flags: APIC_CPU_FREE | APIC_CPU_DIRTY
837 	 *	Note: cpu exists but won't be configured/enabled at boot time
838 	 * Rule 3:
839 	 * 	Slot index range: [apic_nproc, boot_ncpus)
840 	 *	State flags: APIC_CPU_FREE
841 	 *	Note: cpu doesn't exist at boot time
842 	 * Rule 4:
843 	 * 	Slot index range: [max(apic_nproc, boot_ncpus), max_ncpus)
844 	 *	State flags: APIC_CPU_FREE
845 	 *	Note: cpu doesn't exist at boot time
846 	 */
847 	CPUSET_ZERO(apic_cpumask);
848 	for (i = 0; i < min(boot_ncpus, apic_nproc); i++) {
849 		CPUSET_ADD(apic_cpumask, i);
850 		apic_cpus[i].aci_status = 0;
851 	}
852 	for (i = boot_ncpus; i < apic_nproc; i++) {
853 		apic_cpus[i].aci_status = APIC_CPU_FREE | APIC_CPU_DIRTY;
854 	}
855 	for (i = apic_nproc; i < boot_ncpus; i++) {
856 		apic_cpus[i].aci_status = APIC_CPU_FREE;
857 	}
858 	for (i = max(boot_ncpus, apic_nproc); i < max_ncpus; i++) {
859 		apic_cpus[i].aci_status = APIC_CPU_FREE;
860 	}
861 
862 	for (i = 0; i < apic_io_max; i++) {
863 		ioapic_ix = i;
864 
865 		/*
866 		 * need to check Sitka on the following acpi problem
867 		 * On the Sitka, the ioapic's apic_id field isn't reporting
868 		 * the actual io apic id. We have reported this problem
869 		 * to Intel. Until they fix the problem, we will get the
870 		 * actual id directly from the ioapic.
871 		 */
872 		id = ioapic_read(ioapic_ix, APIC_ID_CMD);
873 		hid = (uchar_t)(id >> 24);
874 
875 		if (hid != apic_io_id[i]) {
876 			if (apic_io_id[i] == 0)
877 				apic_io_id[i] = hid;
878 			else { /* set ioapic id to whatever reported by ACPI */
879 				id = ((uint32_t)apic_io_id[i]) << 24;
880 				ioapic_write(ioapic_ix, APIC_ID_CMD, id);
881 			}
882 		}
883 		ver = ioapic_read(ioapic_ix, APIC_VERS_CMD);
884 		apic_io_ver[i] = (uchar_t)(ver & 0xff);
885 		intmax = (ver >> 16) & 0xff;
886 		apic_io_vectend[i] = apic_io_vectbase[i] + intmax;
887 		if (apic_first_avail_irq <= apic_io_vectend[i])
888 			apic_first_avail_irq = apic_io_vectend[i] + 1;
889 	}
890 
891 
892 	/*
893 	 * Process SCI configuration here
894 	 * An error may be returned here if
895 	 * acpi-user-options specifies legacy mode
896 	 * (no SCI, no ACPI mode)
897 	 */
898 	if (acpica_get_sci(&sci, &sci_flags) != AE_OK)
899 		sci = -1;
900 
901 	/*
902 	 * Now call acpi_init() to generate namespaces
903 	 * If this fails, we don't attempt to use ACPI
904 	 * even if we were able to get a MADT above
905 	 */
906 	if (acpica_init() != AE_OK)
907 		goto cleanup;
908 
909 	/*
910 	 * Call acpica_build_processor_map() now that we have
911 	 * ACPI namesspace access
912 	 */
913 	(void) acpica_build_processor_map();
914 
915 	/*
916 	 * Squirrel away the SCI and flags for later on
917 	 * in apic_picinit() when we're ready
918 	 */
919 	apic_sci_vect = sci;
920 	apic_sci_flags = sci_flags;
921 
922 	if (apic_verbose & APIC_VERBOSE_IRQ_FLAG)
923 		acpi_verboseflags |= PSM_VERBOSE_IRQ_FLAG;
924 
925 	if (apic_verbose & APIC_VERBOSE_POWEROFF_FLAG)
926 		acpi_verboseflags |= PSM_VERBOSE_POWEROFF_FLAG;
927 
928 	if (apic_verbose & APIC_VERBOSE_POWEROFF_PAUSE_FLAG)
929 		acpi_verboseflags |= PSM_VERBOSE_POWEROFF_PAUSE_FLAG;
930 
931 	if (acpi_psm_init(modname, acpi_verboseflags) == ACPI_PSM_FAILURE)
932 		goto cleanup;
933 
934 	/* Enable ACPI APIC interrupt routing */
935 	if (apic_acpi_enter_apicmode() != PSM_FAILURE) {
936 		build_reserved_irqlist((uchar_t *)apic_reserved_irqlist);
937 		apic_enable_acpi = 1;
938 		if (apic_sci_vect > 0) {
939 			acpica_set_core_feature(ACPI_FEATURE_SCI_EVENT);
940 		}
941 		if (apic_use_acpi_madt_only) {
942 			cmn_err(CE_CONT,
943 			    "?Using ACPI for CPU/IOAPIC information ONLY\n");
944 		}
945 
946 #if !defined(__xpv)
947 		/*
948 		 * probe ACPI for hpet information here which is used later
949 		 * in apic_picinit().
950 		 */
951 		if (hpet_acpi_init(&apic_hpet_vect, &apic_hpet_flags) < 0) {
952 			cmn_err(CE_NOTE, "!ACPI HPET table query failed\n");
953 		}
954 #endif
955 
956 		kmem_free(local_ids, NCPU * sizeof (uint32_t));
957 		kmem_free(proc_ids, NCPU * sizeof (uint32_t));
958 		return (PSM_SUCCESS);
959 	}
960 	/* if setting APIC mode failed above, we fall through to cleanup */
961 
962 cleanup:
963 	apic_free_apic_cpus();
964 	if (apicadr != NULL) {
965 		mapout_apic((caddr_t)apicadr, APIC_LOCAL_MEMLEN);
966 		apicadr = NULL;
967 	}
968 	apic_max_nproc = -1;
969 	apic_nproc = 0;
970 	for (i = 0; i < apic_io_max; i++) {
971 		mapout_ioapic((caddr_t)apicioadr[i], APIC_IO_MEMLEN);
972 		apicioadr[i] = NULL;
973 	}
974 	apic_io_max = 0;
975 	acpi_isop = NULL;
976 	acpi_iso_cnt = 0;
977 	acpi_nmi_sp = NULL;
978 	acpi_nmi_scnt = 0;
979 	acpi_nmi_cp = NULL;
980 	acpi_nmi_ccnt = 0;
981 	kmem_free(local_ids, NCPU * sizeof (uint32_t));
982 	kmem_free(proc_ids, NCPU * sizeof (uint32_t));
983 	return (PSM_FAILURE);
984 }
985 
986 /*
987  * Handle default configuration. Fill in reqd global variables & tables
988  * Fill all details as MP table does not give any more info
989  */
990 static int
991 apic_handle_defconf()
992 {
993 	uint_t	lid;
994 
995 	/* Failed to probe ACPI MADT tables, disable CPU DR. */
996 	apic_max_nproc = -1;
997 	apic_free_apic_cpus();
998 	plat_dr_disable_cpu();
999 
1000 	apicioadr[0] = (void *)mapin_ioapic(APIC_IO_ADDR,
1001 	    APIC_IO_MEMLEN, PROT_READ | PROT_WRITE);
1002 	apicadr = (void *)psm_map_phys(APIC_LOCAL_ADDR,
1003 	    APIC_LOCAL_MEMLEN, PROT_READ);
1004 	apic_cpus_size = 2 * sizeof (*apic_cpus);
1005 	apic_cpus = (apic_cpus_info_t *)
1006 	    kmem_zalloc(apic_cpus_size, KM_NOSLEEP);
1007 	if ((!apicadr) || (!apicioadr[0]) || (!apic_cpus))
1008 		goto apic_handle_defconf_fail;
1009 	CPUSET_ONLY(apic_cpumask, 0);
1010 	CPUSET_ADD(apic_cpumask, 1);
1011 	apic_nproc = 2;
1012 	lid = apic_reg_ops->apic_read(APIC_LID_REG);
1013 	apic_cpus[0].aci_local_id = (uchar_t)(lid >> APIC_ID_BIT_OFFSET);
1014 	/*
1015 	 * According to the PC+MP spec 1.1, the local ids
1016 	 * for the default configuration has to be 0 or 1
1017 	 */
1018 	if (apic_cpus[0].aci_local_id == 1)
1019 		apic_cpus[1].aci_local_id = 0;
1020 	else if (apic_cpus[0].aci_local_id == 0)
1021 		apic_cpus[1].aci_local_id = 1;
1022 	else
1023 		goto apic_handle_defconf_fail;
1024 
1025 	apic_io_id[0] = 2;
1026 	apic_io_max = 1;
1027 	if (apic_defconf >= 5) {
1028 		apic_cpus[0].aci_local_ver = APIC_INTEGRATED_VERS;
1029 		apic_cpus[1].aci_local_ver = APIC_INTEGRATED_VERS;
1030 		apic_io_ver[0] = APIC_INTEGRATED_VERS;
1031 	} else {
1032 		apic_cpus[0].aci_local_ver = 0;		/* 82489 DX */
1033 		apic_cpus[1].aci_local_ver = 0;
1034 		apic_io_ver[0] = 0;
1035 	}
1036 	if (apic_defconf == 2 || apic_defconf == 3 || apic_defconf == 6)
1037 		eisa_level_intr_mask = (inb(EISA_LEVEL_CNTL + 1) << 8) |
1038 		    inb(EISA_LEVEL_CNTL) | ((uint_t)INT32_MAX + 1);
1039 	return (PSM_SUCCESS);
1040 
1041 apic_handle_defconf_fail:
1042 	if (apicadr)
1043 		mapout_apic((caddr_t)apicadr, APIC_LOCAL_MEMLEN);
1044 	if (apicioadr[0])
1045 		mapout_ioapic((caddr_t)apicioadr[0], APIC_IO_MEMLEN);
1046 	return (PSM_FAILURE);
1047 }
1048 
1049 /* Parse the entries in MP configuration table and collect info that we need */
1050 static int
1051 apic_parse_mpct(caddr_t mpct, int bypass_cpus_and_ioapics)
1052 {
1053 	struct	apic_procent	*procp;
1054 	struct	apic_bus	*busp;
1055 	struct	apic_io_entry	*ioapicp;
1056 	struct	apic_io_intr	*intrp;
1057 	int			ioapic_ix;
1058 	uint_t	lid;
1059 	uint32_t	id;
1060 	uchar_t hid;
1061 	int	warned = 0;
1062 
1063 	/*LINTED: pointer cast may result in improper alignment */
1064 	procp = (struct apic_procent *)(mpct + sizeof (struct apic_mp_cnf_hdr));
1065 
1066 	/* No need to count cpu entries if we won't use them */
1067 	if (!bypass_cpus_and_ioapics) {
1068 
1069 		/* Find max # of CPUS and allocate structure accordingly */
1070 		apic_nproc = 0;
1071 		CPUSET_ZERO(apic_cpumask);
1072 		while (procp->proc_entry == APIC_CPU_ENTRY) {
1073 			if (procp->proc_cpuflags & CPUFLAGS_EN) {
1074 				if (apic_nproc < NCPU && use_mp &&
1075 				    apic_nproc < boot_ncpus) {
1076 					CPUSET_ADD(apic_cpumask, apic_nproc);
1077 					apic_nproc++;
1078 				} else if (apic_nproc == NCPU && !warned) {
1079 					cmn_err(CE_WARN, "%s: CPU limit "
1080 					    "exceeded"
1081 #if !defined(__amd64)
1082 					    " for 32-bit mode"
1083 #endif
1084 					    "; Solaris will use %d CPUs.",
1085 					    psm_name,  NCPU);
1086 					warned = 1;
1087 				}
1088 
1089 			}
1090 			procp++;
1091 		}
1092 		apic_cpus_size = apic_nproc * sizeof (*apic_cpus);
1093 		if (!apic_nproc || !(apic_cpus = (apic_cpus_info_t *)
1094 		    kmem_zalloc(apic_cpus_size, KM_NOSLEEP)))
1095 			return (PSM_FAILURE);
1096 	}
1097 
1098 	/*LINTED: pointer cast may result in improper alignment */
1099 	procp = (struct apic_procent *)(mpct + sizeof (struct apic_mp_cnf_hdr));
1100 
1101 	/*
1102 	 * start with index 1 as 0 needs to be filled in with Boot CPU, but
1103 	 * if we're bypassing this information, it has already been filled
1104 	 * in by acpi_probe(), so don't overwrite it.
1105 	 */
1106 	if (!bypass_cpus_and_ioapics)
1107 		apic_nproc = 1;
1108 
1109 	while (procp->proc_entry == APIC_CPU_ENTRY) {
1110 		/* check whether the cpu exists or not */
1111 		if (!bypass_cpus_and_ioapics &&
1112 		    procp->proc_cpuflags & CPUFLAGS_EN) {
1113 			if (procp->proc_cpuflags & CPUFLAGS_BP) { /* Boot CPU */
1114 				lid = apic_reg_ops->apic_read(APIC_LID_REG);
1115 				apic_cpus[0].aci_local_id = procp->proc_apicid;
1116 				if (apic_cpus[0].aci_local_id !=
1117 				    (uchar_t)(lid >> APIC_ID_BIT_OFFSET)) {
1118 					return (PSM_FAILURE);
1119 				}
1120 				apic_cpus[0].aci_local_ver =
1121 				    procp->proc_version;
1122 			} else if (apic_nproc < NCPU && use_mp &&
1123 			    apic_nproc < boot_ncpus) {
1124 				apic_cpus[apic_nproc].aci_local_id =
1125 				    procp->proc_apicid;
1126 
1127 				apic_cpus[apic_nproc].aci_local_ver =
1128 				    procp->proc_version;
1129 				apic_nproc++;
1130 
1131 			}
1132 		}
1133 		procp++;
1134 	}
1135 
1136 	/*
1137 	 * Save start of bus entries for later use.
1138 	 * Get EISA level cntrl if EISA bus is present.
1139 	 * Also get the CPI bus id for single CPI bus case
1140 	 */
1141 	apic_busp = busp = (struct apic_bus *)procp;
1142 	while (busp->bus_entry == APIC_BUS_ENTRY) {
1143 		lid = apic_find_bus_type((char *)&busp->bus_str1);
1144 		if (lid	== BUS_EISA) {
1145 			eisa_level_intr_mask = (inb(EISA_LEVEL_CNTL + 1) << 8) |
1146 			    inb(EISA_LEVEL_CNTL) | ((uint_t)INT32_MAX + 1);
1147 		} else if (lid == BUS_PCI) {
1148 			/*
1149 			 * apic_single_pci_busid will be used only if
1150 			 * apic_pic_bus_total is equal to 1
1151 			 */
1152 			apic_pci_bus_total++;
1153 			apic_single_pci_busid = busp->bus_id;
1154 		}
1155 		busp++;
1156 	}
1157 
1158 	ioapicp = (struct apic_io_entry *)busp;
1159 
1160 	if (!bypass_cpus_and_ioapics)
1161 		apic_io_max = 0;
1162 	do {
1163 		if (!bypass_cpus_and_ioapics && apic_io_max < MAX_IO_APIC) {
1164 			if (ioapicp->io_flags & IOAPIC_FLAGS_EN) {
1165 				apic_io_id[apic_io_max] = ioapicp->io_apicid;
1166 				apic_io_ver[apic_io_max] = ioapicp->io_version;
1167 				apicioadr[apic_io_max] =
1168 				    (void *)mapin_ioapic(
1169 				    (uint32_t)ioapicp->io_apic_addr,
1170 				    APIC_IO_MEMLEN, PROT_READ | PROT_WRITE);
1171 
1172 				if (!apicioadr[apic_io_max])
1173 					return (PSM_FAILURE);
1174 
1175 				ioapic_mask_workaround[apic_io_max] =
1176 				    apic_is_ioapic_AMD_813x(
1177 				    ioapicp->io_apic_addr);
1178 
1179 				ioapic_ix = apic_io_max;
1180 				id = ioapic_read(ioapic_ix, APIC_ID_CMD);
1181 				hid = (uchar_t)(id >> 24);
1182 
1183 				if (hid != apic_io_id[apic_io_max]) {
1184 					if (apic_io_id[apic_io_max] == 0)
1185 						apic_io_id[apic_io_max] = hid;
1186 					else {
1187 						/*
1188 						 * set ioapic id to whatever
1189 						 * reported by MPS
1190 						 *
1191 						 * may not need to set index
1192 						 * again ???
1193 						 * take it out and try
1194 						 */
1195 
1196 						id = ((uint32_t)
1197 						    apic_io_id[apic_io_max]) <<
1198 						    24;
1199 
1200 						ioapic_write(ioapic_ix,
1201 						    APIC_ID_CMD, id);
1202 					}
1203 				}
1204 				apic_io_max++;
1205 			}
1206 		}
1207 		ioapicp++;
1208 	} while (ioapicp->io_entry == APIC_IO_ENTRY);
1209 
1210 	apic_io_intrp = (struct apic_io_intr *)ioapicp;
1211 
1212 	intrp = apic_io_intrp;
1213 	while (intrp->intr_entry == APIC_IO_INTR_ENTRY) {
1214 		if ((intrp->intr_irq > APIC_MAX_ISA_IRQ) ||
1215 		    (apic_find_bus(intrp->intr_busid) == BUS_PCI)) {
1216 			apic_irq_translate = 1;
1217 			break;
1218 		}
1219 		intrp++;
1220 	}
1221 
1222 	return (PSM_SUCCESS);
1223 }
1224 
1225 boolean_t
1226 apic_cpu_in_range(int cpu)
1227 {
1228 	cpu &= ~IRQ_USER_BOUND;
1229 	/* Check whether cpu id is in valid range. */
1230 	if (cpu < 0 || cpu >= apic_nproc) {
1231 		return (B_FALSE);
1232 	} else if (apic_max_nproc != -1 && cpu >= apic_max_nproc) {
1233 		/*
1234 		 * Check whether cpuid is in valid range if CPU DR is enabled.
1235 		 */
1236 		return (B_FALSE);
1237 	} else if (!CPU_IN_SET(apic_cpumask, cpu)) {
1238 		return (B_FALSE);
1239 	}
1240 
1241 	return (B_TRUE);
1242 }
1243 
1244 processorid_t
1245 apic_get_next_bind_cpu(void)
1246 {
1247 	int i, count;
1248 	processorid_t cpuid = 0;
1249 
1250 	for (count = 0; count < apic_nproc; count++) {
1251 		if (apic_next_bind_cpu >= apic_nproc) {
1252 			apic_next_bind_cpu = 0;
1253 		}
1254 		i = apic_next_bind_cpu++;
1255 		if (apic_cpu_in_range(i)) {
1256 			cpuid = i;
1257 			break;
1258 		}
1259 	}
1260 
1261 	return (cpuid);
1262 }
1263 
1264 uint16_t
1265 apic_get_apic_version()
1266 {
1267 	int i;
1268 	uchar_t min_io_apic_ver = 0;
1269 	static uint16_t version;		/* Cache as value is constant */
1270 	static boolean_t found = B_FALSE;	/* Accomodate zero version */
1271 
1272 	if (found == B_FALSE) {
1273 		found = B_TRUE;
1274 
1275 		/*
1276 		 * Don't assume all IO APICs in the system are the same.
1277 		 *
1278 		 * Set to the minimum version.
1279 		 */
1280 		for (i = 0; i < apic_io_max; i++) {
1281 			if ((apic_io_ver[i] != 0) &&
1282 			    ((min_io_apic_ver == 0) ||
1283 			    (min_io_apic_ver >= apic_io_ver[i])))
1284 				min_io_apic_ver = apic_io_ver[i];
1285 		}
1286 
1287 		/* Assume all local APICs are of the same version. */
1288 		version = (min_io_apic_ver << 8) | apic_cpus[0].aci_local_ver;
1289 	}
1290 	return (version);
1291 }
1292 
1293 static struct apic_mpfps_hdr *
1294 apic_find_fps_sig(caddr_t cptr, int len)
1295 {
1296 	int	i;
1297 
1298 	/* Look for the pattern "_MP_" */
1299 	for (i = 0; i < len; i += 16) {
1300 		if ((*(cptr+i) == '_') &&
1301 		    (*(cptr+i+1) == 'M') &&
1302 		    (*(cptr+i+2) == 'P') &&
1303 		    (*(cptr+i+3) == '_'))
1304 		    /*LINTED: pointer cast may result in improper alignment */
1305 			return ((struct apic_mpfps_hdr *)(cptr + i));
1306 	}
1307 	return (NULL);
1308 }
1309 
1310 static int
1311 apic_checksum(caddr_t bptr, int len)
1312 {
1313 	int	i;
1314 	uchar_t	cksum;
1315 
1316 	cksum = 0;
1317 	for (i = 0; i < len; i++)
1318 		cksum += *bptr++;
1319 	return ((int)cksum);
1320 }
1321 
1322 /*
1323  * On machines with PCI-PCI bridges, a device behind a PCI-PCI bridge
1324  * needs special handling.  We may need to chase up the device tree,
1325  * using the PCI-PCI Bridge specification's "rotating IPIN assumptions",
1326  * to find the IPIN at the root bus that relates to the IPIN on the
1327  * subsidiary bus (for ACPI or MP).  We may, however, have an entry
1328  * in the MP table or the ACPI namespace for this device itself.
1329  * We handle both cases in the search below.
1330  */
1331 /* this is the non-acpi version */
1332 int
1333 apic_handle_pci_pci_bridge(dev_info_t *idip, int child_devno, int child_ipin,
1334 			struct apic_io_intr **intrp)
1335 {
1336 	dev_info_t *dipp, *dip;
1337 	int pci_irq;
1338 	ddi_acc_handle_t cfg_handle;
1339 	int bridge_devno, bridge_bus;
1340 	int ipin;
1341 
1342 	dip = idip;
1343 
1344 	/*CONSTCOND*/
1345 	while (1) {
1346 		if (((dipp = ddi_get_parent(dip)) == (dev_info_t *)NULL) ||
1347 		    (pci_config_setup(dipp, &cfg_handle) != DDI_SUCCESS))
1348 			return (-1);
1349 		if ((pci_config_get8(cfg_handle, PCI_CONF_BASCLASS) ==
1350 		    PCI_CLASS_BRIDGE) && (pci_config_get8(cfg_handle,
1351 		    PCI_CONF_SUBCLASS) == PCI_BRIDGE_PCI)) {
1352 			pci_config_teardown(&cfg_handle);
1353 			if (acpica_get_bdf(dipp, &bridge_bus, &bridge_devno,
1354 			    NULL) != 0)
1355 				return (-1);
1356 			/*
1357 			 * This is the rotating scheme documented in the
1358 			 * PCI-to-PCI spec.  If the PCI-to-PCI bridge is
1359 			 * behind another PCI-to-PCI bridge, then it needs
1360 			 * to keep ascending until an interrupt entry is
1361 			 * found or the root is reached.
1362 			 */
1363 			ipin = (child_devno + child_ipin) % PCI_INTD;
1364 				if (bridge_bus == 0 && apic_pci_bus_total == 1)
1365 					bridge_bus = (int)apic_single_pci_busid;
1366 				pci_irq = ((bridge_devno & 0x1f) << 2) |
1367 				    (ipin & 0x3);
1368 				if ((*intrp = apic_find_io_intr_w_busid(pci_irq,
1369 				    bridge_bus)) != NULL) {
1370 					return (pci_irq);
1371 				}
1372 			dip = dipp;
1373 			child_devno = bridge_devno;
1374 			child_ipin = ipin;
1375 		} else {
1376 			pci_config_teardown(&cfg_handle);
1377 			return (-1);
1378 		}
1379 	}
1380 	/*LINTED: function will not fall off the bottom */
1381 }
1382 
1383 uchar_t
1384 acpi_find_ioapic(int irq)
1385 {
1386 	int i;
1387 
1388 	for (i = 0; i < apic_io_max; i++) {
1389 		if (irq >= apic_io_vectbase[i] && irq <= apic_io_vectend[i])
1390 			return ((uchar_t)i);
1391 	}
1392 	return (0xFF);	/* shouldn't happen */
1393 }
1394 
1395 /*
1396  * See if two irqs are compatible for sharing a vector.
1397  * Currently we only support sharing of PCI devices.
1398  */
1399 static int
1400 acpi_intr_compatible(iflag_t iflag1, iflag_t iflag2)
1401 {
1402 	uint_t	level1, po1;
1403 	uint_t	level2, po2;
1404 
1405 	/* Assume active high by default */
1406 	po1 = 0;
1407 	po2 = 0;
1408 
1409 	if (iflag1.bustype != iflag2.bustype || iflag1.bustype != BUS_PCI)
1410 		return (0);
1411 
1412 	if (iflag1.intr_el == INTR_EL_CONFORM)
1413 		level1 = AV_LEVEL;
1414 	else
1415 		level1 = (iflag1.intr_el == INTR_EL_LEVEL) ? AV_LEVEL : 0;
1416 
1417 	if (level1 && ((iflag1.intr_po == INTR_PO_ACTIVE_LOW) ||
1418 	    (iflag1.intr_po == INTR_PO_CONFORM)))
1419 		po1 = AV_ACTIVE_LOW;
1420 
1421 	if (iflag2.intr_el == INTR_EL_CONFORM)
1422 		level2 = AV_LEVEL;
1423 	else
1424 		level2 = (iflag2.intr_el == INTR_EL_LEVEL) ? AV_LEVEL : 0;
1425 
1426 	if (level2 && ((iflag2.intr_po == INTR_PO_ACTIVE_LOW) ||
1427 	    (iflag2.intr_po == INTR_PO_CONFORM)))
1428 		po2 = AV_ACTIVE_LOW;
1429 
1430 	if ((level1 == level2) && (po1 == po2))
1431 		return (1);
1432 
1433 	return (0);
1434 }
1435 
1436 struct apic_io_intr *
1437 apic_find_io_intr_w_busid(int irqno, int busid)
1438 {
1439 	struct	apic_io_intr	*intrp;
1440 
1441 	/*
1442 	 * It can have more than 1 entry with same source bus IRQ,
1443 	 * but unique with the source bus id
1444 	 */
1445 	intrp = apic_io_intrp;
1446 	if (intrp != NULL) {
1447 		while (intrp->intr_entry == APIC_IO_INTR_ENTRY) {
1448 			if (intrp->intr_irq == irqno &&
1449 			    intrp->intr_busid == busid &&
1450 			    intrp->intr_type == IO_INTR_INT)
1451 				return (intrp);
1452 			intrp++;
1453 		}
1454 	}
1455 	APIC_VERBOSE_IOAPIC((CE_NOTE, "Did not find io intr for irqno:"
1456 	    "busid %x:%x\n", irqno, busid));
1457 	return ((struct apic_io_intr *)NULL);
1458 }
1459 
1460 
1461 struct mps_bus_info {
1462 	char	*bus_name;
1463 	int	bus_id;
1464 } bus_info_array[] = {
1465 	"ISA ", BUS_ISA,
1466 	"PCI ", BUS_PCI,
1467 	"EISA ", BUS_EISA,
1468 	"XPRESS", BUS_XPRESS,
1469 	"PCMCIA", BUS_PCMCIA,
1470 	"VL ", BUS_VL,
1471 	"CBUS ", BUS_CBUS,
1472 	"CBUSII", BUS_CBUSII,
1473 	"FUTURE", BUS_FUTURE,
1474 	"INTERN", BUS_INTERN,
1475 	"MBI ", BUS_MBI,
1476 	"MBII ", BUS_MBII,
1477 	"MPI ", BUS_MPI,
1478 	"MPSA ", BUS_MPSA,
1479 	"NUBUS ", BUS_NUBUS,
1480 	"TC ", BUS_TC,
1481 	"VME ", BUS_VME,
1482 	"PCI-E ", BUS_PCIE
1483 };
1484 
1485 static int
1486 apic_find_bus_type(char *bus)
1487 {
1488 	int	i = 0;
1489 
1490 	for (; i < sizeof (bus_info_array)/sizeof (struct mps_bus_info); i++)
1491 		if (strncmp(bus, bus_info_array[i].bus_name,
1492 		    strlen(bus_info_array[i].bus_name)) == 0)
1493 			return (bus_info_array[i].bus_id);
1494 	APIC_VERBOSE_IOAPIC((CE_WARN, "Did not find bus type for bus %s", bus));
1495 	return (0);
1496 }
1497 
1498 static int
1499 apic_find_bus(int busid)
1500 {
1501 	struct	apic_bus	*busp;
1502 
1503 	busp = apic_busp;
1504 	while (busp->bus_entry == APIC_BUS_ENTRY) {
1505 		if (busp->bus_id == busid)
1506 			return (apic_find_bus_type((char *)&busp->bus_str1));
1507 		busp++;
1508 	}
1509 	APIC_VERBOSE_IOAPIC((CE_WARN, "Did not find bus for bus id %x", busid));
1510 	return (0);
1511 }
1512 
1513 int
1514 apic_find_bus_id(int bustype)
1515 {
1516 	struct	apic_bus	*busp;
1517 
1518 	busp = apic_busp;
1519 	while (busp->bus_entry == APIC_BUS_ENTRY) {
1520 		if (apic_find_bus_type((char *)&busp->bus_str1) == bustype)
1521 			return (busp->bus_id);
1522 		busp++;
1523 	}
1524 	APIC_VERBOSE_IOAPIC((CE_WARN, "Did not find bus id for bustype %x",
1525 	    bustype));
1526 	return (-1);
1527 }
1528 
1529 /*
1530  * Check if a particular irq need to be reserved for any io_intr
1531  */
1532 static struct apic_io_intr *
1533 apic_find_io_intr(int irqno)
1534 {
1535 	struct	apic_io_intr	*intrp;
1536 
1537 	intrp = apic_io_intrp;
1538 	if (intrp != NULL) {
1539 		while (intrp->intr_entry == APIC_IO_INTR_ENTRY) {
1540 			if (intrp->intr_irq == irqno &&
1541 			    intrp->intr_type == IO_INTR_INT)
1542 				return (intrp);
1543 			intrp++;
1544 		}
1545 	}
1546 	return ((struct apic_io_intr *)NULL);
1547 }
1548 
1549 /*
1550  * Check if the given ioapicindex intin combination has already been assigned
1551  * an irq. If so return irqno. Else -1
1552  */
1553 int
1554 apic_find_intin(uchar_t ioapic, uchar_t intin)
1555 {
1556 	apic_irq_t *irqptr;
1557 	int	i;
1558 
1559 	/* find ioapic and intin in the apic_irq_table[] and return the index */
1560 	for (i = apic_min_device_irq; i <= apic_max_device_irq; i++) {
1561 		irqptr = apic_irq_table[i];
1562 		while (irqptr) {
1563 			if ((irqptr->airq_mps_intr_index >= 0) &&
1564 			    (irqptr->airq_intin_no == intin) &&
1565 			    (irqptr->airq_ioapicindex == ioapic)) {
1566 				APIC_VERBOSE_IOAPIC((CE_NOTE, "!Found irq "
1567 				    "entry for ioapic:intin %x:%x "
1568 				    "shared interrupts ?", ioapic, intin));
1569 				return (i);
1570 			}
1571 			irqptr = irqptr->airq_next;
1572 		}
1573 	}
1574 	return (-1);
1575 }
1576 
1577 int
1578 apic_allocate_irq(int irq)
1579 {
1580 	int	freeirq, i;
1581 
1582 	if ((freeirq = apic_find_free_irq(irq, (APIC_RESV_IRQ - 1))) == -1)
1583 		if ((freeirq = apic_find_free_irq(APIC_FIRST_FREE_IRQ,
1584 		    (irq - 1))) == -1) {
1585 			/*
1586 			 * if BIOS really defines every single irq in the mps
1587 			 * table, then don't worry about conflicting with
1588 			 * them, just use any free slot in apic_irq_table
1589 			 */
1590 			for (i = APIC_FIRST_FREE_IRQ; i < APIC_RESV_IRQ; i++) {
1591 				if ((apic_irq_table[i] == NULL) ||
1592 				    apic_irq_table[i]->airq_mps_intr_index ==
1593 				    FREE_INDEX) {
1594 				freeirq = i;
1595 				break;
1596 			}
1597 		}
1598 		if (freeirq == -1) {
1599 			/* This shouldn't happen, but just in case */
1600 			cmn_err(CE_WARN, "%s: NO available IRQ", psm_name);
1601 			return (-1);
1602 		}
1603 	}
1604 	if (apic_irq_table[freeirq] == NULL) {
1605 		apic_irq_table[freeirq] =
1606 		    kmem_zalloc(sizeof (apic_irq_t), KM_NOSLEEP);
1607 		if (apic_irq_table[freeirq] == NULL) {
1608 			cmn_err(CE_WARN, "%s: NO memory to allocate IRQ",
1609 			    psm_name);
1610 			return (-1);
1611 		}
1612 		apic_irq_table[freeirq]->airq_temp_cpu = IRQ_UNINIT;
1613 		apic_irq_table[freeirq]->airq_mps_intr_index = FREE_INDEX;
1614 	}
1615 	return (freeirq);
1616 }
1617 
1618 static int
1619 apic_find_free_irq(int start, int end)
1620 {
1621 	int	i;
1622 
1623 	for (i = start; i <= end; i++)
1624 		/* Check if any I/O entry needs this IRQ */
1625 		if (apic_find_io_intr(i) == NULL) {
1626 			/* Then see if it is free */
1627 			if ((apic_irq_table[i] == NULL) ||
1628 			    (apic_irq_table[i]->airq_mps_intr_index ==
1629 			    FREE_INDEX)) {
1630 				return (i);
1631 			}
1632 		}
1633 	return (-1);
1634 }
1635 
1636 /*
1637  * compute the polarity, trigger mode and vector for programming into
1638  * the I/O apic and record in airq_rdt_entry.
1639  */
1640 void
1641 apic_record_rdt_entry(apic_irq_t *irqptr, int irq)
1642 {
1643 	int	ioapicindex, bus_type, vector;
1644 	short	intr_index;
1645 	uint_t	level, po, io_po;
1646 	struct apic_io_intr *iointrp;
1647 
1648 	intr_index = irqptr->airq_mps_intr_index;
1649 	DDI_INTR_IMPLDBG((CE_CONT, "apic_record_rdt_entry: intr_index=%d "
1650 	    "irq = 0x%x dip = 0x%p vector = 0x%x\n", intr_index, irq,
1651 	    (void *)irqptr->airq_dip, irqptr->airq_vector));
1652 
1653 	if (intr_index == RESERVE_INDEX) {
1654 		apic_error |= APIC_ERR_INVALID_INDEX;
1655 		return;
1656 	} else if (APIC_IS_MSI_OR_MSIX_INDEX(intr_index)) {
1657 		return;
1658 	}
1659 
1660 	vector = irqptr->airq_vector;
1661 	ioapicindex = irqptr->airq_ioapicindex;
1662 	/* Assume edge triggered by default */
1663 	level = 0;
1664 	/* Assume active high by default */
1665 	po = 0;
1666 
1667 	if (intr_index == DEFAULT_INDEX || intr_index == FREE_INDEX) {
1668 		ASSERT(irq < 16);
1669 		if (eisa_level_intr_mask & (1 << irq))
1670 			level = AV_LEVEL;
1671 		if (intr_index == FREE_INDEX && apic_defconf == 0)
1672 			apic_error |= APIC_ERR_INVALID_INDEX;
1673 	} else if (intr_index == ACPI_INDEX) {
1674 		bus_type = irqptr->airq_iflag.bustype;
1675 		if (irqptr->airq_iflag.intr_el == INTR_EL_CONFORM) {
1676 			if (bus_type == BUS_PCI)
1677 				level = AV_LEVEL;
1678 		} else
1679 			level = (irqptr->airq_iflag.intr_el == INTR_EL_LEVEL) ?
1680 			    AV_LEVEL : 0;
1681 		if (level &&
1682 		    ((irqptr->airq_iflag.intr_po == INTR_PO_ACTIVE_LOW) ||
1683 		    (irqptr->airq_iflag.intr_po == INTR_PO_CONFORM &&
1684 		    bus_type == BUS_PCI)))
1685 			po = AV_ACTIVE_LOW;
1686 	} else {
1687 		iointrp = apic_io_intrp + intr_index;
1688 		bus_type = apic_find_bus(iointrp->intr_busid);
1689 		if (iointrp->intr_el == INTR_EL_CONFORM) {
1690 			if ((irq < 16) && (eisa_level_intr_mask & (1 << irq)))
1691 				level = AV_LEVEL;
1692 			else if (bus_type == BUS_PCI)
1693 				level = AV_LEVEL;
1694 		} else
1695 			level = (iointrp->intr_el == INTR_EL_LEVEL) ?
1696 			    AV_LEVEL : 0;
1697 		if (level && ((iointrp->intr_po == INTR_PO_ACTIVE_LOW) ||
1698 		    (iointrp->intr_po == INTR_PO_CONFORM &&
1699 		    bus_type == BUS_PCI)))
1700 			po = AV_ACTIVE_LOW;
1701 	}
1702 	if (level)
1703 		apic_level_intr[irq] = 1;
1704 	/*
1705 	 * The 82489DX External APIC cannot do active low polarity interrupts.
1706 	 */
1707 	if (po && (apic_io_ver[ioapicindex] != IOAPIC_VER_82489DX))
1708 		io_po = po;
1709 	else
1710 		io_po = 0;
1711 
1712 	if (apic_verbose & APIC_VERBOSE_IOAPIC_FLAG)
1713 		prom_printf("setio: ioapic=0x%x intin=0x%x level=0x%x po=0x%x "
1714 		    "vector=0x%x cpu=0x%x\n\n", ioapicindex,
1715 		    irqptr->airq_intin_no, level, io_po, vector,
1716 		    irqptr->airq_cpu);
1717 
1718 	irqptr->airq_rdt_entry = level|io_po|vector;
1719 }
1720 
1721 int
1722 apic_acpi_translate_pci_irq(dev_info_t *dip, int busid, int devid,
1723     int ipin, int *pci_irqp, iflag_t *intr_flagp)
1724 {
1725 
1726 	int status;
1727 	acpi_psm_lnk_t acpipsmlnk;
1728 
1729 	if ((status = acpi_get_irq_cache_ent(busid, devid, ipin, pci_irqp,
1730 	    intr_flagp)) == ACPI_PSM_SUCCESS) {
1731 		APIC_VERBOSE_IRQ((CE_CONT, "!%s: Found irqno %d "
1732 		    "from cache for device %s, instance #%d\n", psm_name,
1733 		    *pci_irqp, ddi_get_name(dip), ddi_get_instance(dip)));
1734 		return (status);
1735 	}
1736 
1737 	bzero(&acpipsmlnk, sizeof (acpi_psm_lnk_t));
1738 
1739 	if ((status = acpi_translate_pci_irq(dip, ipin, pci_irqp, intr_flagp,
1740 	    &acpipsmlnk)) == ACPI_PSM_FAILURE) {
1741 		APIC_VERBOSE_IRQ((CE_WARN, "%s: "
1742 		    " acpi_translate_pci_irq failed for device %s, instance"
1743 		    " #%d", psm_name, ddi_get_name(dip),
1744 		    ddi_get_instance(dip)));
1745 		return (status);
1746 	}
1747 
1748 	if (status == ACPI_PSM_PARTIAL && acpipsmlnk.lnkobj != NULL) {
1749 		status = apic_acpi_irq_configure(&acpipsmlnk, dip, pci_irqp,
1750 		    intr_flagp);
1751 		if (status != ACPI_PSM_SUCCESS) {
1752 			status = acpi_get_current_irq_resource(&acpipsmlnk,
1753 			    pci_irqp, intr_flagp);
1754 		}
1755 	}
1756 
1757 	if (status == ACPI_PSM_SUCCESS) {
1758 		acpi_new_irq_cache_ent(busid, devid, ipin, *pci_irqp,
1759 		    intr_flagp, &acpipsmlnk);
1760 
1761 		APIC_VERBOSE_IRQ((CE_CONT, "%s: [ACPI] "
1762 		    "new irq %d for device %s, instance #%d\n", psm_name,
1763 		    *pci_irqp, ddi_get_name(dip), ddi_get_instance(dip)));
1764 	}
1765 
1766 	return (status);
1767 }
1768 
1769 /*
1770  * Adds an entry to the irq list passed in, and returns the new list.
1771  * Entries are added in priority order (lower numerical priorities are
1772  * placed closer to the head of the list)
1773  */
1774 static prs_irq_list_t *
1775 acpi_insert_prs_irq_ent(prs_irq_list_t *listp, int priority, int irq,
1776     iflag_t *iflagp, acpi_prs_private_t *prsprvp)
1777 {
1778 	struct prs_irq_list_ent *newent, *prevp = NULL, *origlistp;
1779 
1780 	newent = kmem_zalloc(sizeof (struct prs_irq_list_ent), KM_SLEEP);
1781 
1782 	newent->list_prio = priority;
1783 	newent->irq = irq;
1784 	newent->intrflags = *iflagp;
1785 	newent->prsprv = *prsprvp;
1786 	/* ->next is NULL from kmem_zalloc */
1787 
1788 	/*
1789 	 * New list -- return the new entry as the list.
1790 	 */
1791 	if (listp == NULL)
1792 		return (newent);
1793 
1794 	/*
1795 	 * Save original list pointer for return (since we're not modifying
1796 	 * the head)
1797 	 */
1798 	origlistp = listp;
1799 
1800 	/*
1801 	 * Insertion sort, with entries with identical keys stored AFTER
1802 	 * existing entries (the less-than-or-equal test of priority does
1803 	 * this for us).
1804 	 */
1805 	while (listp != NULL && listp->list_prio <= priority) {
1806 		prevp = listp;
1807 		listp = listp->next;
1808 	}
1809 
1810 	newent->next = listp;
1811 
1812 	if (prevp == NULL) { /* Add at head of list (newent is the new head) */
1813 		return (newent);
1814 	} else {
1815 		prevp->next = newent;
1816 		return (origlistp);
1817 	}
1818 }
1819 
1820 /*
1821  * Frees the list passed in, deallocating all memory and leaving *listpp
1822  * set to NULL.
1823  */
1824 static void
1825 acpi_destroy_prs_irq_list(prs_irq_list_t **listpp)
1826 {
1827 	struct prs_irq_list_ent *nextp;
1828 
1829 	ASSERT(listpp != NULL);
1830 
1831 	while (*listpp != NULL) {
1832 		nextp = (*listpp)->next;
1833 		kmem_free(*listpp, sizeof (struct prs_irq_list_ent));
1834 		*listpp = nextp;
1835 	}
1836 }
1837 
1838 /*
1839  * apic_choose_irqs_from_prs returns a list of irqs selected from the list of
1840  * irqs returned by the link device's _PRS method.  The irqs are chosen
1841  * to minimize contention in situations where the interrupt link device
1842  * can be programmed to steer interrupts to different interrupt controller
1843  * inputs (some of which may already be in use).  The list is sorted in order
1844  * of irqs to use, with the highest priority given to interrupt controller
1845  * inputs that are not shared.   When an interrupt controller input
1846  * must be shared, apic_choose_irqs_from_prs adds the possible irqs to the
1847  * returned list in the order that minimizes sharing (thereby ensuring lowest
1848  * possible latency from interrupt trigger time to ISR execution time).
1849  */
1850 static prs_irq_list_t *
1851 apic_choose_irqs_from_prs(acpi_irqlist_t *irqlistent, dev_info_t *dip,
1852     int crs_irq)
1853 {
1854 	int32_t irq;
1855 	int i;
1856 	prs_irq_list_t *prsirqlistp = NULL;
1857 	iflag_t iflags;
1858 
1859 	while (irqlistent != NULL) {
1860 		irqlistent->intr_flags.bustype = BUS_PCI;
1861 
1862 		for (i = 0; i < irqlistent->num_irqs; i++) {
1863 
1864 			irq = irqlistent->irqs[i];
1865 
1866 			if (irq <= 0) {
1867 				/* invalid irq number */
1868 				continue;
1869 			}
1870 
1871 			if ((irq < 16) && (apic_reserved_irqlist[irq]))
1872 				continue;
1873 
1874 			if ((apic_irq_table[irq] == NULL) ||
1875 			    (apic_irq_table[irq]->airq_dip == dip)) {
1876 
1877 				prsirqlistp = acpi_insert_prs_irq_ent(
1878 				    prsirqlistp, 0 /* Highest priority */, irq,
1879 				    &irqlistent->intr_flags,
1880 				    &irqlistent->acpi_prs_prv);
1881 
1882 				/*
1883 				 * If we do not prefer the current irq from _CRS
1884 				 * or if we do and this irq is the same as the
1885 				 * current irq from _CRS, this is the one
1886 				 * to pick.
1887 				 */
1888 				if (!(apic_prefer_crs) || (irq == crs_irq)) {
1889 					return (prsirqlistp);
1890 				}
1891 				continue;
1892 			}
1893 
1894 			/*
1895 			 * Edge-triggered interrupts cannot be shared
1896 			 */
1897 			if (irqlistent->intr_flags.intr_el == INTR_EL_EDGE)
1898 				continue;
1899 
1900 			/*
1901 			 * To work around BIOSes that contain incorrect
1902 			 * interrupt polarity information in interrupt
1903 			 * descriptors returned by _PRS, we assume that
1904 			 * the polarity of the other device sharing this
1905 			 * interrupt controller input is compatible.
1906 			 * If it's not, the caller will catch it when
1907 			 * the caller invokes the link device's _CRS method
1908 			 * (after invoking its _SRS method).
1909 			 */
1910 			iflags = irqlistent->intr_flags;
1911 			iflags.intr_po =
1912 			    apic_irq_table[irq]->airq_iflag.intr_po;
1913 
1914 			if (!acpi_intr_compatible(iflags,
1915 			    apic_irq_table[irq]->airq_iflag)) {
1916 				APIC_VERBOSE_IRQ((CE_CONT, "!%s: irq %d "
1917 				    "not compatible [%x:%x:%x !~ %x:%x:%x]",
1918 				    psm_name, irq,
1919 				    iflags.intr_po,
1920 				    iflags.intr_el,
1921 				    iflags.bustype,
1922 				    apic_irq_table[irq]->airq_iflag.intr_po,
1923 				    apic_irq_table[irq]->airq_iflag.intr_el,
1924 				    apic_irq_table[irq]->airq_iflag.bustype));
1925 				continue;
1926 			}
1927 
1928 			/*
1929 			 * If we prefer the irq from _CRS, no need
1930 			 * to search any further (and make sure
1931 			 * to add this irq with the highest priority
1932 			 * so it's tried first).
1933 			 */
1934 			if (crs_irq == irq && apic_prefer_crs) {
1935 
1936 				return (acpi_insert_prs_irq_ent(
1937 				    prsirqlistp,
1938 				    0 /* Highest priority */,
1939 				    irq, &iflags,
1940 				    &irqlistent->acpi_prs_prv));
1941 			}
1942 
1943 			/*
1944 			 * Priority is equal to the share count (lower
1945 			 * share count is higher priority). Note that
1946 			 * the intr flags passed in here are the ones we
1947 			 * changed above -- if incorrect, it will be
1948 			 * caught by the caller's _CRS flags comparison.
1949 			 */
1950 			prsirqlistp = acpi_insert_prs_irq_ent(
1951 			    prsirqlistp,
1952 			    apic_irq_table[irq]->airq_share, irq,
1953 			    &iflags, &irqlistent->acpi_prs_prv);
1954 		}
1955 
1956 		/* Go to the next irqlist entry */
1957 		irqlistent = irqlistent->next;
1958 	}
1959 
1960 	return (prsirqlistp);
1961 }
1962 
1963 /*
1964  * Configures the irq for the interrupt link device identified by
1965  * acpipsmlnkp.
1966  *
1967  * Gets the current and the list of possible irq settings for the
1968  * device. If apic_unconditional_srs is not set, and the current
1969  * resource setting is in the list of possible irq settings,
1970  * current irq resource setting is passed to the caller.
1971  *
1972  * Otherwise, picks an irq number from the list of possible irq
1973  * settings, and sets the irq of the device to this value.
1974  * If prefer_crs is set, among a set of irq numbers in the list that have
1975  * the least number of devices sharing the interrupt, we pick current irq
1976  * resource setting if it is a member of this set.
1977  *
1978  * Passes the irq number in the value pointed to by pci_irqp, and
1979  * polarity and sensitivity in the structure pointed to by dipintrflagp
1980  * to the caller.
1981  *
1982  * Note that if setting the irq resource failed, but successfuly obtained
1983  * the current irq resource settings, passes the current irq resources
1984  * and considers it a success.
1985  *
1986  * Returns:
1987  * ACPI_PSM_SUCCESS on success.
1988  *
1989  * ACPI_PSM_FAILURE if an error occured during the configuration or
1990  * if a suitable irq was not found for this device, or if setting the
1991  * irq resource and obtaining the current resource fails.
1992  *
1993  */
1994 static int
1995 apic_acpi_irq_configure(acpi_psm_lnk_t *acpipsmlnkp, dev_info_t *dip,
1996     int *pci_irqp, iflag_t *dipintr_flagp)
1997 {
1998 	int32_t irq;
1999 	int cur_irq = -1;
2000 	acpi_irqlist_t *irqlistp;
2001 	prs_irq_list_t *prs_irq_listp, *prs_irq_entp;
2002 	boolean_t found_irq = B_FALSE;
2003 
2004 	dipintr_flagp->bustype = BUS_PCI;
2005 
2006 	if ((acpi_get_possible_irq_resources(acpipsmlnkp, &irqlistp))
2007 	    == ACPI_PSM_FAILURE) {
2008 		APIC_VERBOSE_IRQ((CE_WARN, "!%s: Unable to determine "
2009 		    "or assign IRQ for device %s, instance #%d: The system was "
2010 		    "unable to get the list of potential IRQs from ACPI.",
2011 		    psm_name, ddi_get_name(dip), ddi_get_instance(dip)));
2012 
2013 		return (ACPI_PSM_FAILURE);
2014 	}
2015 
2016 	if ((acpi_get_current_irq_resource(acpipsmlnkp, &cur_irq,
2017 	    dipintr_flagp) == ACPI_PSM_SUCCESS) && (!apic_unconditional_srs) &&
2018 	    (cur_irq > 0)) {
2019 		/*
2020 		 * If an IRQ is set in CRS and that IRQ exists in the set
2021 		 * returned from _PRS, return that IRQ, otherwise print
2022 		 * a warning
2023 		 */
2024 
2025 		if (acpi_irqlist_find_irq(irqlistp, cur_irq, NULL)
2026 		    == ACPI_PSM_SUCCESS) {
2027 
2028 			ASSERT(pci_irqp != NULL);
2029 			*pci_irqp = cur_irq;
2030 			acpi_free_irqlist(irqlistp);
2031 			return (ACPI_PSM_SUCCESS);
2032 		}
2033 
2034 		APIC_VERBOSE_IRQ((CE_WARN, "!%s: Could not find the "
2035 		    "current irq %d for device %s, instance #%d in ACPI's "
2036 		    "list of possible irqs for this device. Picking one from "
2037 		    " the latter list.", psm_name, cur_irq, ddi_get_name(dip),
2038 		    ddi_get_instance(dip)));
2039 	}
2040 
2041 	if ((prs_irq_listp = apic_choose_irqs_from_prs(irqlistp, dip,
2042 	    cur_irq)) == NULL) {
2043 
2044 		APIC_VERBOSE_IRQ((CE_WARN, "!%s: Could not find a "
2045 		    "suitable irq from the list of possible irqs for device "
2046 		    "%s, instance #%d in ACPI's list of possible irqs",
2047 		    psm_name, ddi_get_name(dip), ddi_get_instance(dip)));
2048 
2049 		acpi_free_irqlist(irqlistp);
2050 		return (ACPI_PSM_FAILURE);
2051 	}
2052 
2053 	acpi_free_irqlist(irqlistp);
2054 
2055 	for (prs_irq_entp = prs_irq_listp;
2056 	    prs_irq_entp != NULL && found_irq == B_FALSE;
2057 	    prs_irq_entp = prs_irq_entp->next) {
2058 
2059 		acpipsmlnkp->acpi_prs_prv = prs_irq_entp->prsprv;
2060 		irq = prs_irq_entp->irq;
2061 
2062 		APIC_VERBOSE_IRQ((CE_CONT, "!%s: Setting irq %d for "
2063 		    "device %s instance #%d\n", psm_name, irq,
2064 		    ddi_get_name(dip), ddi_get_instance(dip)));
2065 
2066 		if ((acpi_set_irq_resource(acpipsmlnkp, irq))
2067 		    == ACPI_PSM_SUCCESS) {
2068 			/*
2069 			 * setting irq was successful, check to make sure CRS
2070 			 * reflects that. If CRS does not agree with what we
2071 			 * set, return the irq that was set.
2072 			 */
2073 
2074 			if (acpi_get_current_irq_resource(acpipsmlnkp, &cur_irq,
2075 			    dipintr_flagp) == ACPI_PSM_SUCCESS) {
2076 
2077 				if (cur_irq != irq)
2078 					APIC_VERBOSE_IRQ((CE_WARN,
2079 					    "!%s: IRQ resource set "
2080 					    "(irqno %d) for device %s "
2081 					    "instance #%d, differs from "
2082 					    "current setting irqno %d",
2083 					    psm_name, irq, ddi_get_name(dip),
2084 					    ddi_get_instance(dip), cur_irq));
2085 			} else {
2086 				/*
2087 				 * On at least one system, there was a bug in
2088 				 * a DSDT method called by _STA, causing _STA to
2089 				 * indicate that the link device was disabled
2090 				 * (when, in fact, it was enabled).  Since _SRS
2091 				 * succeeded, assume that _CRS is lying and use
2092 				 * the iflags from this _PRS interrupt choice.
2093 				 * If we're wrong about the flags, the polarity
2094 				 * will be incorrect and we may get an interrupt
2095 				 * storm, but there's not much else we can do
2096 				 * at this point.
2097 				 */
2098 				*dipintr_flagp = prs_irq_entp->intrflags;
2099 			}
2100 
2101 			/*
2102 			 * Return the irq that was set, and not what _CRS
2103 			 * reports, since _CRS has been seen to return
2104 			 * different IRQs than what was passed to _SRS on some
2105 			 * systems (and just not return successfully on others).
2106 			 */
2107 			cur_irq = irq;
2108 			found_irq = B_TRUE;
2109 		} else {
2110 			APIC_VERBOSE_IRQ((CE_WARN, "!%s: set resource "
2111 			    "irq %d failed for device %s instance #%d",
2112 			    psm_name, irq, ddi_get_name(dip),
2113 			    ddi_get_instance(dip)));
2114 
2115 			if (cur_irq == -1) {
2116 				acpi_destroy_prs_irq_list(&prs_irq_listp);
2117 				return (ACPI_PSM_FAILURE);
2118 			}
2119 		}
2120 	}
2121 
2122 	acpi_destroy_prs_irq_list(&prs_irq_listp);
2123 
2124 	if (!found_irq)
2125 		return (ACPI_PSM_FAILURE);
2126 
2127 	ASSERT(pci_irqp != NULL);
2128 	*pci_irqp = cur_irq;
2129 	return (ACPI_PSM_SUCCESS);
2130 }
2131 
2132 void
2133 ioapic_disable_redirection()
2134 {
2135 	int ioapic_ix;
2136 	int intin_max;
2137 	int intin_ix;
2138 
2139 	/* Disable the I/O APIC redirection entries */
2140 	for (ioapic_ix = 0; ioapic_ix < apic_io_max; ioapic_ix++) {
2141 
2142 		/* Bits 23-16 define the maximum redirection entries */
2143 		intin_max = (ioapic_read(ioapic_ix, APIC_VERS_CMD) >> 16)
2144 		    & 0xff;
2145 
2146 		for (intin_ix = 0; intin_ix <= intin_max; intin_ix++) {
2147 			/*
2148 			 * The assumption here is that this is safe, even for
2149 			 * systems with IOAPICs that suffer from the hardware
2150 			 * erratum because all devices have been quiesced before
2151 			 * this function is called from apic_shutdown()
2152 			 * (or equivalent). If that assumption turns out to be
2153 			 * false, this mask operation can induce the same
2154 			 * erratum result we're trying to avoid.
2155 			 */
2156 			ioapic_write(ioapic_ix, APIC_RDT_CMD + 2 * intin_ix,
2157 			    AV_MASK);
2158 		}
2159 	}
2160 }
2161 
2162 /*
2163  * Looks for an IOAPIC with the specified physical address in the /ioapics
2164  * node in the device tree (created by the PCI enumerator).
2165  */
2166 static boolean_t
2167 apic_is_ioapic_AMD_813x(uint32_t physaddr)
2168 {
2169 	/*
2170 	 * Look in /ioapics, for the ioapic with
2171 	 * the physical address given
2172 	 */
2173 	dev_info_t *ioapicsnode = ddi_find_devinfo(IOAPICS_NODE_NAME, -1, 0);
2174 	dev_info_t *ioapic_child;
2175 	boolean_t rv = B_FALSE;
2176 	int vid, did;
2177 	uint64_t ioapic_paddr;
2178 	boolean_t done = B_FALSE;
2179 
2180 	if (ioapicsnode == NULL)
2181 		return (B_FALSE);
2182 
2183 	/* Load first child: */
2184 	ioapic_child = ddi_get_child(ioapicsnode);
2185 	while (!done && ioapic_child != 0) { /* Iterate over children */
2186 
2187 		if ((ioapic_paddr = (uint64_t)ddi_prop_get_int64(DDI_DEV_T_ANY,
2188 		    ioapic_child, DDI_PROP_DONTPASS, "reg", 0))
2189 		    != 0 && physaddr == ioapic_paddr) {
2190 
2191 			vid = ddi_prop_get_int(DDI_DEV_T_ANY, ioapic_child,
2192 			    DDI_PROP_DONTPASS, IOAPICS_PROP_VENID, 0);
2193 
2194 			if (vid == VENID_AMD) {
2195 
2196 				did = ddi_prop_get_int(DDI_DEV_T_ANY,
2197 				    ioapic_child, DDI_PROP_DONTPASS,
2198 				    IOAPICS_PROP_DEVID, 0);
2199 
2200 				if (did == DEVID_8131_IOAPIC ||
2201 				    did == DEVID_8132_IOAPIC) {
2202 					rv = B_TRUE;
2203 					done = B_TRUE;
2204 				}
2205 			}
2206 		}
2207 
2208 		if (!done)
2209 			ioapic_child = ddi_get_next_sibling(ioapic_child);
2210 	}
2211 
2212 	/* The ioapics node was held by ddi_find_devinfo, so release it */
2213 	ndi_rele_devi(ioapicsnode);
2214 	return (rv);
2215 }
2216 
2217 struct apic_state {
2218 	int32_t as_task_reg;
2219 	int32_t as_dest_reg;
2220 	int32_t as_format_reg;
2221 	int32_t as_local_timer;
2222 	int32_t as_pcint_vect;
2223 	int32_t as_int_vect0;
2224 	int32_t as_int_vect1;
2225 	int32_t as_err_vect;
2226 	int32_t as_init_count;
2227 	int32_t as_divide_reg;
2228 	int32_t as_spur_int_reg;
2229 	uint32_t as_ioapic_ids[MAX_IO_APIC];
2230 };
2231 
2232 
2233 static int
2234 apic_acpi_enter_apicmode(void)
2235 {
2236 	ACPI_OBJECT_LIST	arglist;
2237 	ACPI_OBJECT		arg;
2238 	ACPI_STATUS		status;
2239 
2240 	/* Setup parameter object */
2241 	arglist.Count = 1;
2242 	arglist.Pointer = &arg;
2243 	arg.Type = ACPI_TYPE_INTEGER;
2244 	arg.Integer.Value = ACPI_APIC_MODE;
2245 
2246 	status = AcpiEvaluateObject(NULL, "\\_PIC", &arglist, NULL);
2247 	if (ACPI_FAILURE(status))
2248 		return (PSM_FAILURE);
2249 	else
2250 		return (PSM_SUCCESS);
2251 }
2252 
2253 
2254 static void
2255 apic_save_state(struct apic_state *sp)
2256 {
2257 	int	i, cpuid;
2258 	ulong_t	iflag;
2259 
2260 	PMD(PMD_SX, ("apic_save_state %p\n", (void *)sp))
2261 	/*
2262 	 * First the local APIC.
2263 	 */
2264 	sp->as_task_reg = apic_reg_ops->apic_get_pri();
2265 	sp->as_dest_reg =  apic_reg_ops->apic_read(APIC_DEST_REG);
2266 	if (apic_mode == LOCAL_APIC)
2267 		sp->as_format_reg = apic_reg_ops->apic_read(APIC_FORMAT_REG);
2268 	sp->as_local_timer = apic_reg_ops->apic_read(APIC_LOCAL_TIMER);
2269 	sp->as_pcint_vect = apic_reg_ops->apic_read(APIC_PCINT_VECT);
2270 	sp->as_int_vect0 = apic_reg_ops->apic_read(APIC_INT_VECT0);
2271 	sp->as_int_vect1 = apic_reg_ops->apic_read(APIC_INT_VECT1);
2272 	sp->as_err_vect = apic_reg_ops->apic_read(APIC_ERR_VECT);
2273 	sp->as_init_count = apic_reg_ops->apic_read(APIC_INIT_COUNT);
2274 	sp->as_divide_reg = apic_reg_ops->apic_read(APIC_DIVIDE_REG);
2275 	sp->as_spur_int_reg = apic_reg_ops->apic_read(APIC_SPUR_INT_REG);
2276 
2277 	/*
2278 	 * If on the boot processor then save the IOAPICs' IDs
2279 	 */
2280 	if ((cpuid = psm_get_cpu_id()) == 0) {
2281 
2282 		iflag = intr_clear();
2283 		lock_set(&apic_ioapic_lock);
2284 
2285 		for (i = 0; i < apic_io_max; i++)
2286 			sp->as_ioapic_ids[i] = ioapic_read(i, APIC_ID_CMD);
2287 
2288 		lock_clear(&apic_ioapic_lock);
2289 		intr_restore(iflag);
2290 	}
2291 
2292 	/* apic_state() is currently invoked only in Suspend/Resume */
2293 	apic_cpus[cpuid].aci_status |= APIC_CPU_SUSPEND;
2294 }
2295 
2296 static void
2297 apic_restore_state(struct apic_state *sp)
2298 {
2299 	int	i;
2300 	ulong_t	iflag;
2301 
2302 	/*
2303 	 * First the local APIC.
2304 	 */
2305 	apic_reg_ops->apic_write_task_reg(sp->as_task_reg);
2306 	if (apic_mode == LOCAL_APIC) {
2307 		apic_reg_ops->apic_write(APIC_DEST_REG, sp->as_dest_reg);
2308 		apic_reg_ops->apic_write(APIC_FORMAT_REG, sp->as_format_reg);
2309 	}
2310 	apic_reg_ops->apic_write(APIC_LOCAL_TIMER, sp->as_local_timer);
2311 	apic_reg_ops->apic_write(APIC_PCINT_VECT, sp->as_pcint_vect);
2312 	apic_reg_ops->apic_write(APIC_INT_VECT0, sp->as_int_vect0);
2313 	apic_reg_ops->apic_write(APIC_INT_VECT1, sp->as_int_vect1);
2314 	apic_reg_ops->apic_write(APIC_ERR_VECT, sp->as_err_vect);
2315 	apic_reg_ops->apic_write(APIC_INIT_COUNT, sp->as_init_count);
2316 	apic_reg_ops->apic_write(APIC_DIVIDE_REG, sp->as_divide_reg);
2317 	apic_reg_ops->apic_write(APIC_SPUR_INT_REG, sp->as_spur_int_reg);
2318 
2319 	/*
2320 	 * the following only needs to be done once, so we do it on the
2321 	 * boot processor, since we know that we only have one of those
2322 	 */
2323 	if (psm_get_cpu_id() == 0) {
2324 
2325 		iflag = intr_clear();
2326 		lock_set(&apic_ioapic_lock);
2327 
2328 		/* Restore IOAPICs' APIC IDs */
2329 		for (i = 0; i < apic_io_max; i++) {
2330 			ioapic_write(i, APIC_ID_CMD, sp->as_ioapic_ids[i]);
2331 		}
2332 
2333 		lock_clear(&apic_ioapic_lock);
2334 		intr_restore(iflag);
2335 
2336 		/*
2337 		 * Reenter APIC mode before restoring LNK devices
2338 		 */
2339 		(void) apic_acpi_enter_apicmode();
2340 
2341 		/*
2342 		 * restore acpi link device mappings
2343 		 */
2344 		acpi_restore_link_devices();
2345 	}
2346 }
2347 
2348 /*
2349  * Returns 0 on success
2350  */
2351 int
2352 apic_state(psm_state_request_t *rp)
2353 {
2354 	PMD(PMD_SX, ("apic_state "))
2355 	switch (rp->psr_cmd) {
2356 	case PSM_STATE_ALLOC:
2357 		rp->req.psm_state_req.psr_state =
2358 		    kmem_zalloc(sizeof (struct apic_state), KM_NOSLEEP);
2359 		if (rp->req.psm_state_req.psr_state == NULL)
2360 			return (ENOMEM);
2361 		rp->req.psm_state_req.psr_state_size =
2362 		    sizeof (struct apic_state);
2363 		PMD(PMD_SX, (":STATE_ALLOC: state %p, size %lx\n",
2364 		    rp->req.psm_state_req.psr_state,
2365 		    rp->req.psm_state_req.psr_state_size))
2366 		return (0);
2367 
2368 	case PSM_STATE_FREE:
2369 		kmem_free(rp->req.psm_state_req.psr_state,
2370 		    rp->req.psm_state_req.psr_state_size);
2371 		PMD(PMD_SX, (" STATE_FREE: state %p, size %lx\n",
2372 		    rp->req.psm_state_req.psr_state,
2373 		    rp->req.psm_state_req.psr_state_size))
2374 		return (0);
2375 
2376 	case PSM_STATE_SAVE:
2377 		PMD(PMD_SX, (" STATE_SAVE: state %p, size %lx\n",
2378 		    rp->req.psm_state_req.psr_state,
2379 		    rp->req.psm_state_req.psr_state_size))
2380 		apic_save_state(rp->req.psm_state_req.psr_state);
2381 		return (0);
2382 
2383 	case PSM_STATE_RESTORE:
2384 		apic_restore_state(rp->req.psm_state_req.psr_state);
2385 		PMD(PMD_SX, (" STATE_RESTORE: state %p, size %lx\n",
2386 		    rp->req.psm_state_req.psr_state,
2387 		    rp->req.psm_state_req.psr_state_size))
2388 		return (0);
2389 
2390 	default:
2391 		return (EINVAL);
2392 	}
2393 }
2394