xref: /illumos-gate/usr/src/uts/intel/io/pci/pci_boot.c (revision 8d483882)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
575bcd456Sjg  * Common Development and Distribution License (the "License").
675bcd456Sjg  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22c8589f13Ssethg  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/stat.h>
307c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
317c478bd9Sstevel@tonic-gate #include <sys/pci.h>
327c478bd9Sstevel@tonic-gate #include <sys/pci_impl.h>
337c478bd9Sstevel@tonic-gate #include <sys/pci_cfgspace.h>
347c478bd9Sstevel@tonic-gate #include <sys/memlist.h>
357c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
3670025d76Sjohnny #include <io/pci/mps_table.h>
37c88420b3Sdmick #include <sys/pci_cfgspace.h>
38c88420b3Sdmick #include <sys/pci_cfgspace_impl.h>
39c88420b3Sdmick #include <sys/psw.h>
4009f67678Sanish #include "../../../../common/pci/pci_strings.h"
41c8589f13Ssethg #include <sys/apic.h>
428a5a0d1eSanish #include <io/pciex/pcie_nvidia.h>
4325145214Smyers #include <sys/acpi/acpi.h>
4425145214Smyers #include <sys/acpica.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #define	pci_getb	(*pci_getb_func)
477c478bd9Sstevel@tonic-gate #define	pci_getw	(*pci_getw_func)
487c478bd9Sstevel@tonic-gate #define	pci_getl	(*pci_getl_func)
497c478bd9Sstevel@tonic-gate #define	pci_putb	(*pci_putb_func)
507c478bd9Sstevel@tonic-gate #define	pci_putw	(*pci_putw_func)
517c478bd9Sstevel@tonic-gate #define	pci_putl	(*pci_putl_func)
527c478bd9Sstevel@tonic-gate #define	dcmn_err	if (pci_boot_debug) cmn_err
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #define	CONFIG_INFO	0
557c478bd9Sstevel@tonic-gate #define	CONFIG_UPDATE	1
567c478bd9Sstevel@tonic-gate #define	CONFIG_NEW	2
57bd87be88Ssethg #define	CONFIG_FIX	3
5870025d76Sjohnny #define	COMPAT_BUFSIZE	512
597c478bd9Sstevel@tonic-gate 
60bd87be88Ssethg /* See AMD-8111 Datasheet Rev 3.03, Page 149: */
61bd87be88Ssethg #define	LPC_IO_CONTROL_REG_1	0x40
62bd87be88Ssethg #define	AMD8111_ENABLENMI	(uint8_t)0x80
63bd87be88Ssethg #define	DEVID_AMD8111_LPC	0x7468
64bd87be88Ssethg 
65bd87be88Ssethg struct pci_fixundo {
66bd87be88Ssethg 	uint8_t			bus;
67bd87be88Ssethg 	uint8_t			dev;
68bd87be88Ssethg 	uint8_t			fn;
69bd87be88Ssethg 	void			(*undofn)(uint8_t, uint8_t, uint8_t);
70bd87be88Ssethg 	struct pci_fixundo	*next;
71bd87be88Ssethg };
72bd87be88Ssethg 
737c478bd9Sstevel@tonic-gate extern int pci_bios_nbus;
747c478bd9Sstevel@tonic-gate static uchar_t max_dev_pci = 32;	/* PCI standard */
757c478bd9Sstevel@tonic-gate int pci_boot_debug = 0;
767c478bd9Sstevel@tonic-gate extern struct memlist *find_bus_res(int, int);
77bd87be88Ssethg static struct pci_fixundo *undolist = NULL;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate  * Module prototypes
817c478bd9Sstevel@tonic-gate  */
827c478bd9Sstevel@tonic-gate static void enumerate_bus_devs(uchar_t bus, int config_op);
837c478bd9Sstevel@tonic-gate static void create_root_bus_dip(uchar_t bus);
84bd87be88Ssethg static dev_info_t *process_devfunc(uchar_t, uchar_t, uchar_t, uchar_t,
857c478bd9Sstevel@tonic-gate     ushort_t, int);
867c478bd9Sstevel@tonic-gate static void add_compatible(dev_info_t *, ushort_t, ushort_t,
8770025d76Sjohnny     ushort_t, ushort_t, uchar_t, uint_t, int);
887c478bd9Sstevel@tonic-gate static int add_reg_props(dev_info_t *, uchar_t, uchar_t, uchar_t, int, int);
8970025d76Sjohnny static void add_ppb_props(dev_info_t *, uchar_t, uchar_t, uchar_t, int);
907c478bd9Sstevel@tonic-gate static void add_model_prop(dev_info_t *, uint_t);
917c478bd9Sstevel@tonic-gate static void add_bus_range_prop(int);
92b1f176e8Sjg static void add_bus_slot_names_prop(int);
937c478bd9Sstevel@tonic-gate static void add_ppb_ranges_prop(int);
947c478bd9Sstevel@tonic-gate static void add_bus_available_prop(int);
959896aa55Sjveta static void fix_ppb_res(uchar_t);
96f55ce205Sszhou static void alloc_res_array();
97c8589f13Ssethg static void create_ioapic_node(int bus, int dev, int fn, ushort_t vendorid,
98c8589f13Ssethg     ushort_t deviceid);
997c478bd9Sstevel@tonic-gate 
10075bcd456Sjg extern int pci_slot_names_prop(int, char *, int);
10175bcd456Sjg 
102ee8c1d4aSdm /* set non-zero to force PCI peer-bus renumbering */
10325145214Smyers int pci_bus_always_renumber = 0;
10425145214Smyers 
105fc396574Srw /* get the subordinate bus # for a root/peer bus */
106fc396574Srw static int
107fc396574Srw pci_root_subbus(int bus, uchar_t *subbus)
108fc396574Srw {
109fc396574Srw 	ACPI_HANDLE	hdl;
110fc396574Srw 	ACPI_BUFFER	rb;
111fc396574Srw 	ACPI_RESOURCE	*rp;
112fc396574Srw 	int	rv;
113fc396574Srw 
114fc396574Srw 	if (pci_bus_res[bus].dip == NULL) {
115fc396574Srw 		/* non-used bus # */
116fc396574Srw 		return (AE_ERROR);
117fc396574Srw 	}
1185cff7825Smh 	if (acpica_get_handle(pci_bus_res[bus].dip, &hdl) != AE_OK) {
119fc396574Srw 		cmn_err(CE_WARN, "!No ACPI obj for bus%d, ACPI OFF?\n", bus);
120fc396574Srw 		return (AE_ERROR);
121fc396574Srw 	}
122fc396574Srw 
123fc396574Srw 	rb.Length = ACPI_ALLOCATE_BUFFER;
124fc396574Srw 	if (AcpiGetCurrentResources(hdl, &rb) != AE_OK) {
125fc396574Srw 		cmn_err(CE_WARN, "!_CRS failed on pci%d\n", bus);
126fc396574Srw 		return (AE_ERROR);
127fc396574Srw 	}
128fc396574Srw 
129fc396574Srw 	rv = AE_ERROR;
130fc396574Srw 
131fc396574Srw 	for (rp = rb.Pointer; rp->Type != ACPI_RESOURCE_TYPE_END_TAG;
132fc396574Srw 	    rp = ACPI_NEXT_RESOURCE(rp)) {
133fc396574Srw 
134fc396574Srw 		switch (rp->Type) {
1359896aa55Sjveta 		case ACPI_RESOURCE_TYPE_ADDRESS16:
1369896aa55Sjveta 			if (rp->Data.Address.ResourceType !=
1379896aa55Sjveta 			    ACPI_BUS_NUMBER_RANGE)
1389896aa55Sjveta 				continue;
1399896aa55Sjveta 			*subbus = (uchar_t)rp->Data.Address16.Maximum;
1409896aa55Sjveta 			dcmn_err(CE_NOTE, "Address16,subbus=%d\n", *subbus);
1419896aa55Sjveta 			break;
1429896aa55Sjveta 		case ACPI_RESOURCE_TYPE_ADDRESS32:
1439896aa55Sjveta 			if (rp->Data.Address.ResourceType !=
1449896aa55Sjveta 			    ACPI_BUS_NUMBER_RANGE)
1459896aa55Sjveta 				continue;
1469896aa55Sjveta 			*subbus = (uchar_t)rp->Data.Address32.Maximum;
1479896aa55Sjveta 			dcmn_err(CE_NOTE, "Address32,subbus=%d\n", *subbus);
1489896aa55Sjveta 			break;
1499896aa55Sjveta 		case ACPI_RESOURCE_TYPE_ADDRESS64:
1509896aa55Sjveta 			if (rp->Data.Address.ResourceType !=
1519896aa55Sjveta 			    ACPI_BUS_NUMBER_RANGE)
1529896aa55Sjveta 				continue;
1539896aa55Sjveta 			*subbus = (uchar_t)rp->Data.Address64.Maximum;
1549896aa55Sjveta 			dcmn_err(CE_NOTE, "Address64,subbus=%d\n", *subbus);
1559896aa55Sjveta 			break;
1569896aa55Sjveta 		case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
1579896aa55Sjveta 			if (rp->Data.Address.ResourceType !=
1589896aa55Sjveta 			    ACPI_BUS_NUMBER_RANGE)
1599896aa55Sjveta 				continue;
1609896aa55Sjveta 			*subbus = (uchar_t)rp->Data.ExtAddress64.Maximum;
1619896aa55Sjveta 			dcmn_err(CE_NOTE, "ExtAdr64,subbus=%d\n", *subbus);
1629896aa55Sjveta 			break;
1639896aa55Sjveta 		default:
1649896aa55Sjveta 			dcmn_err(CE_NOTE, "rp->Type=%d\n", rp->Type);
1659896aa55Sjveta 			continue;
166fc396574Srw 		}
167fc396574Srw 
168fc396574Srw 		/* found the bus-range resource */
169fc396574Srw 		dcmn_err(CE_NOTE, "pci%d, subbus=%d\n", bus, *subbus);
170fc396574Srw 		rv = AE_OK;
171fc396574Srw 
172fc396574Srw 		/* This breaks out of the resource scanning loop */
173fc396574Srw 		break;
174fc396574Srw 	}
175fc396574Srw 
176fc396574Srw 	AcpiOsFree(rb.Pointer);
177fc396574Srw 	if (rv != AE_OK)
178fc396574Srw 		cmn_err(CE_NOTE, "!No bus-range resource for pci%d\n", bus);
179fc396574Srw 
180fc396574Srw 	return (rv);
181fc396574Srw 
182fc396574Srw }
183fc396574Srw 
1847c478bd9Sstevel@tonic-gate /*
1857c478bd9Sstevel@tonic-gate  * Enumerate all PCI devices
1867c478bd9Sstevel@tonic-gate  */
1877c478bd9Sstevel@tonic-gate void
1887c478bd9Sstevel@tonic-gate pci_setup_tree()
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	uchar_t i, root_bus_addr = 0;
1917c478bd9Sstevel@tonic-gate 
192f55ce205Sszhou 	alloc_res_array();
1937c478bd9Sstevel@tonic-gate 	for (i = 0; i <= pci_bios_nbus; i++) {
1947c478bd9Sstevel@tonic-gate 		pci_bus_res[i].par_bus = (uchar_t)-1;
1957c478bd9Sstevel@tonic-gate 		pci_bus_res[i].root_addr = (uchar_t)-1;
1967c478bd9Sstevel@tonic-gate 		pci_bus_res[i].sub_bus = i;
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	pci_bus_res[0].root_addr = root_bus_addr++;
2007c478bd9Sstevel@tonic-gate 	create_root_bus_dip(0);
2017c478bd9Sstevel@tonic-gate 	enumerate_bus_devs(0, CONFIG_INFO);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	/*
2047c478bd9Sstevel@tonic-gate 	 * Now enumerate peer busses
2057c478bd9Sstevel@tonic-gate 	 *
2067c478bd9Sstevel@tonic-gate 	 * We loop till pci_bios_nbus. On most systems, there is
2077c478bd9Sstevel@tonic-gate 	 * one more bus at the high end, which implements the ISA
2087c478bd9Sstevel@tonic-gate 	 * compatibility bus. We don't care about that.
2097c478bd9Sstevel@tonic-gate 	 *
2107c478bd9Sstevel@tonic-gate 	 * Note: In the old (bootconf) enumeration, the peer bus
2117c478bd9Sstevel@tonic-gate 	 *	address did not use the bus number, and there were
2127c478bd9Sstevel@tonic-gate 	 *	too many peer busses created. The root_bus_addr is
2137c478bd9Sstevel@tonic-gate 	 *	used to maintain the old peer bus address assignment.
2147c478bd9Sstevel@tonic-gate 	 *	However, we stop enumerating phantom peers with no
2157c478bd9Sstevel@tonic-gate 	 *	device below.
2167c478bd9Sstevel@tonic-gate 	 */
2177c478bd9Sstevel@tonic-gate 	for (i = 1; i <= pci_bios_nbus; i++) {
2187c478bd9Sstevel@tonic-gate 		if (pci_bus_res[i].dip == NULL) {
2197c478bd9Sstevel@tonic-gate 			pci_bus_res[i].root_addr = root_bus_addr++;
2207c478bd9Sstevel@tonic-gate 		}
2217c478bd9Sstevel@tonic-gate 		enumerate_bus_devs(i, CONFIG_INFO);
222b1f176e8Sjg 
223b1f176e8Sjg 		/* add slot-names property for named pci hot-plug slots */
224b1f176e8Sjg 		add_bus_slot_names_prop(i);
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate 
22925145214Smyers /*
23025145214Smyers  * >0 = present, 0 = not present, <0 = error
23125145214Smyers  */
23225145214Smyers static int
23325145214Smyers pci_bbn_present(int bus)
23425145214Smyers {
23525145214Smyers 	ACPI_HANDLE	hdl;
23625145214Smyers 	ACPI_BUFFER	rb;
23725145214Smyers 	int	rv;
23825145214Smyers 
23925145214Smyers 	/* no dip means no _BBN */
24025145214Smyers 	if (pci_bus_res[bus].dip == NULL)
24125145214Smyers 		return (0);
24225145214Smyers 
2435cff7825Smh 	rv = acpica_get_handle(pci_bus_res[bus].dip, &hdl);
24425145214Smyers 	if (rv != AE_OK)
24525145214Smyers 		return (-1);
24625145214Smyers 
24725145214Smyers 	rb.Length = ACPI_ALLOCATE_BUFFER;
24825145214Smyers 
24925145214Smyers 	rv = AcpiEvaluateObject(hdl, "_BBN", NULL, &rb);
25025145214Smyers 
25125145214Smyers 	if (rb.Length > 0)
25225145214Smyers 		AcpiOsFree(rb.Pointer);
25325145214Smyers 
25425145214Smyers 	if (rv == AE_OK)
25525145214Smyers 		return (1);
25625145214Smyers 	else if (rv == AE_NOT_FOUND)
25725145214Smyers 		return (0);
25825145214Smyers 	else
25925145214Smyers 		return (-1);
26025145214Smyers }
26125145214Smyers 
26225145214Smyers /*
26325145214Smyers  * Return non-zero if any PCI bus in the system has an associated
26425145214Smyers  * _BBN object, 0 otherwise.
26525145214Smyers  */
26625145214Smyers static int
26725145214Smyers pci_roots_have_bbn(void)
26825145214Smyers {
26925145214Smyers 	int	i;
27025145214Smyers 
27125145214Smyers 	/*
27225145214Smyers 	 * Scan the PCI busses and look for at least 1 _BBN
27325145214Smyers 	 */
27425145214Smyers 	for (i = 0; i <= pci_bios_nbus; i++) {
27525145214Smyers 		/* skip non-root (peer) PCI busses */
27625145214Smyers 		if (pci_bus_res[i].par_bus != (uchar_t)-1)
27725145214Smyers 			continue;
27825145214Smyers 
27925145214Smyers 		if (pci_bbn_present(i) > 0)
28025145214Smyers 			return (1);
28125145214Smyers 	}
28225145214Smyers 	return (0);
28325145214Smyers 
28425145214Smyers }
28525145214Smyers 
28625145214Smyers /*
28725145214Smyers  * return non-zero if the machine is one on which we renumber
28825145214Smyers  * the internal pci unit-addresses
28925145214Smyers  */
29025145214Smyers static int
29125145214Smyers pci_bus_renumber()
29225145214Smyers {
293ee8c1d4aSdm 	ACPI_TABLE_HEADER *fadt;
29425145214Smyers 
295ee8c1d4aSdm 	if (pci_bus_always_renumber)
29625145214Smyers 		return (1);
297ee8c1d4aSdm 
298ee8c1d4aSdm 	/* get the FADT */
299ee8c1d4aSdm 	if (AcpiGetFirmwareTable(FADT_SIG, 1, ACPI_LOGICAL_ADDRESSING,
300ee8c1d4aSdm 	    (ACPI_TABLE_HEADER **)&fadt) != AE_OK)
30125145214Smyers 		return (0);
30225145214Smyers 
303ee8c1d4aSdm 	/* compare OEM Table ID to "SUNm31" */
304ee8c1d4aSdm 	if (strncmp("SUNm31", fadt->OemId, 6))
305ee8c1d4aSdm 		return (0);
306ee8c1d4aSdm 	else
307ee8c1d4aSdm 		return (1);
30825145214Smyers }
30925145214Smyers 
31025145214Smyers /*
31125145214Smyers  * Initial enumeration of the physical PCI bus hierarchy can
31225145214Smyers  * leave 'gaps' in the order of peer PCI bus unit-addresses.
31325145214Smyers  * Systems with more than one peer PCI bus *must* have an ACPI
31425145214Smyers  * _BBN object associated with each peer bus; use the presence
31525145214Smyers  * of this object to remove gaps in the numbering of the peer
31625145214Smyers  * PCI bus unit-addresses - only peer busses with an associated
31725145214Smyers  * _BBN are counted.
31825145214Smyers  */
31925145214Smyers static void
32025145214Smyers pci_renumber_root_busses(void)
32125145214Smyers {
32225145214Smyers 	int pci_regs[] = {0, 0, 0};
32325145214Smyers 	int	i, root_addr = 0;
32425145214Smyers 
325ee8c1d4aSdm 	/*
326ee8c1d4aSdm 	 * Currently, we only enable the re-numbering on specific
327ee8c1d4aSdm 	 * Sun machines; this is a work-around for the more complicated
328ee8c1d4aSdm 	 * issue of upgrade changing physical device paths
329ee8c1d4aSdm 	 */
33025145214Smyers 	if (!pci_bus_renumber())
33125145214Smyers 		return;
33225145214Smyers 
33325145214Smyers 	/*
33425145214Smyers 	 * If we find no _BBN objects at all, we either don't need
33525145214Smyers 	 * to do anything or can't do anything anyway
33625145214Smyers 	 */
33725145214Smyers 	if (!pci_roots_have_bbn())
33825145214Smyers 		return;
33925145214Smyers 
34025145214Smyers 	for (i = 0; i <= pci_bios_nbus; i++) {
34125145214Smyers 		/* skip non-root (peer) PCI busses */
34225145214Smyers 		if (pci_bus_res[i].par_bus != (uchar_t)-1)
34325145214Smyers 			continue;
34425145214Smyers 
34525145214Smyers 		if (pci_bbn_present(i) < 1) {
34625145214Smyers 			pci_bus_res[i].root_addr = (uchar_t)-1;
34725145214Smyers 			continue;
34825145214Smyers 		}
34925145214Smyers 
35025145214Smyers 		ASSERT(pci_bus_res[i].dip != NULL);
35125145214Smyers 		if (pci_bus_res[i].root_addr != root_addr) {
35225145214Smyers 			/* update reg property for node */
35325145214Smyers 			pci_bus_res[i].root_addr = root_addr;
35425145214Smyers 			pci_regs[0] = pci_bus_res[i].root_addr;
35525145214Smyers 			(void) ndi_prop_update_int_array(DDI_DEV_T_NONE,
35625145214Smyers 			    pci_bus_res[i].dip, "reg", (int *)pci_regs, 3);
35725145214Smyers 		}
35825145214Smyers 		root_addr++;
35925145214Smyers 	}
36025145214Smyers }
36125145214Smyers 
362aaba6dfeSmyers static void
363aaba6dfeSmyers remove_resource_range(struct memlist **list, int *ranges, int range_count)
364aaba6dfeSmyers {
365aaba6dfeSmyers 	struct range {
366aaba6dfeSmyers 		uint32_t base;
367aaba6dfeSmyers 		uint32_t len;
368aaba6dfeSmyers 	};
369aaba6dfeSmyers 	int index;
370aaba6dfeSmyers 
371aaba6dfeSmyers 	for (index = 0; index < range_count; index++) {
372328364c0Smyers 		/* all done if list is or has become empty */
373328364c0Smyers 		if (*list == NULL)
374328364c0Smyers 			break;
375aaba6dfeSmyers 		(void) memlist_remove(list,
376aaba6dfeSmyers 		    (uint64_t)((struct range *)ranges)[index].base,
377aaba6dfeSmyers 		    (uint64_t)((struct range *)ranges)[index].len);
378aaba6dfeSmyers 	}
379aaba6dfeSmyers }
380aaba6dfeSmyers 
381aaba6dfeSmyers static void
382aaba6dfeSmyers remove_used_resources()
383aaba6dfeSmyers {
384aaba6dfeSmyers 	dev_info_t *used;
385aaba6dfeSmyers 	int	*narray;
386aaba6dfeSmyers 	uint_t	ncount;
387aaba6dfeSmyers 	int	status;
388aaba6dfeSmyers 	int	bus;
389aaba6dfeSmyers 
390aaba6dfeSmyers 	used = ddi_find_devinfo("used-resources", -1, 0);
391328364c0Smyers 	if (used == NULL)
392aaba6dfeSmyers 		return;
393aaba6dfeSmyers 
394aaba6dfeSmyers 	status = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, used,
395aaba6dfeSmyers 	    DDI_PROP_DONTPASS, "io-space", &narray, &ncount);
396aaba6dfeSmyers 	if (status == DDI_PROP_SUCCESS) {
397aaba6dfeSmyers 		for (bus = 0; bus <= pci_bios_nbus; bus++)
398328364c0Smyers 			remove_resource_range(&pci_bus_res[bus].io_ports,
399328364c0Smyers 			    narray, ncount / 2);
400aaba6dfeSmyers 		ddi_prop_free(narray);
401aaba6dfeSmyers 	}
402aaba6dfeSmyers 
403aaba6dfeSmyers 	status = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, used,
404aaba6dfeSmyers 	    DDI_PROP_DONTPASS, "device-memory", &narray, &ncount);
405aaba6dfeSmyers 	if (status == DDI_PROP_SUCCESS) {
406aaba6dfeSmyers 		for (bus = 0; bus <= pci_bios_nbus; bus++)
407328364c0Smyers 			remove_resource_range(&pci_bus_res[bus].mem_space,
4089896aa55Sjveta 			    narray, ncount / 2);
409aaba6dfeSmyers 		ddi_prop_free(narray);
410aaba6dfeSmyers 	}
411aaba6dfeSmyers }
412aaba6dfeSmyers 
4139896aa55Sjveta /*
4149896aa55Sjveta  * Assign i/o resources to unconfigured hotplug bridges after the first pass.
4159896aa55Sjveta  * It must be after the first pass in order to use the ports left over after
4169896aa55Sjveta  * accounting for i/o resources of bridges that have been configured by bios.
4179896aa55Sjveta  * We are expecting unconfigured bridges to be empty bridges otherwise
4189896aa55Sjveta  * this resource assignment needs to be done at an earlier stage.
4199896aa55Sjveta  */
4209896aa55Sjveta static void
4219896aa55Sjveta fix_ppb_res(uchar_t secbus)
4229896aa55Sjveta {
4239896aa55Sjveta 	uchar_t bus, dev, func;
4249896aa55Sjveta 	uint_t io_base, io_limit, io_size = 0x1000;
4259896aa55Sjveta 	uint64_t addr = 0;
4269896aa55Sjveta 	int *regp = NULL, rv;
4279896aa55Sjveta 	uint_t reglen;
4289896aa55Sjveta 	dev_info_t *dip;
4299896aa55Sjveta 
4309896aa55Sjveta 	dip = pci_bus_res[secbus].dip;
4319896aa55Sjveta 	/* some entries may be empty due to discontiguous bus numbering */
4329896aa55Sjveta 	if (dip == NULL)
4339896aa55Sjveta 		return;
4349896aa55Sjveta 
4359896aa55Sjveta 	if (ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
4369896aa55Sjveta 	    "pci-hotplug-type", INBAND_HPC_NONE) == INBAND_HPC_NONE)
4379896aa55Sjveta 		return;
4389896aa55Sjveta 
4399896aa55Sjveta 	rv = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
4409896aa55Sjveta 	    "reg", &regp, &reglen);
4419896aa55Sjveta 	if (rv != DDI_PROP_SUCCESS || reglen == 0) {
4429896aa55Sjveta 		/* panic to enforce proper calling order */
4439896aa55Sjveta 		cmn_err(CE_PANIC, "reg property unset for bus %d\n", secbus);
4449896aa55Sjveta 		return;
4459896aa55Sjveta 	}
4469896aa55Sjveta 
4479896aa55Sjveta 	func = (uchar_t)((regp[0] >> 8) & 0x7);
4489896aa55Sjveta 	dev = (uchar_t)((regp[0] >> 11) & 0x1f);
4499896aa55Sjveta 	bus = (uchar_t)((regp[0] >> 16) & 0xff);
4509896aa55Sjveta 	ASSERT(bus == pci_bus_res[secbus].par_bus);
4519896aa55Sjveta 
4529896aa55Sjveta 	/*
4539896aa55Sjveta 	 * io_base >= io_limit means that the bridge was not configured
4549896aa55Sjveta 	 * This may have been set by the bios or by add_ppb_props()
4559896aa55Sjveta 	 */
4569896aa55Sjveta 	io_base = pci_getb(bus, dev, func, PCI_BCNF_IO_BASE_LOW);
4579896aa55Sjveta 	io_limit = pci_getb(bus, dev, func, PCI_BCNF_IO_LIMIT_LOW);
4589896aa55Sjveta 	ASSERT(io_base != 0xff && io_limit != 0xff);
4599896aa55Sjveta 
4609896aa55Sjveta 	io_base = (io_base & 0xf0) << 8;
4619896aa55Sjveta 	io_limit = ((io_limit & 0xf0) << 8) | 0xfff;
4629896aa55Sjveta 	if (io_base < io_limit && io_base != 0)
4639896aa55Sjveta 		return;
4649896aa55Sjveta 
4659896aa55Sjveta 	if (ddi_get_child(dip) != NULL) {
4669896aa55Sjveta 		cmn_err(CE_WARN, "detected unsupported configuration: "
4679896aa55Sjveta 		    "non-empty bridge (bus 0x%x, dev 0x%x, func 0x%x) without "
4689896aa55Sjveta 		    "I/O resources assigned by bios for secondary bus 0x%x\n",
4699896aa55Sjveta 		    bus, dev, func, secbus);
4709896aa55Sjveta 		goto IOFAIL;
4719896aa55Sjveta 	}
4729896aa55Sjveta 
4739896aa55Sjveta 	if (pci_bus_res[bus].io_ports != NULL)
4749896aa55Sjveta 		addr = memlist_find(&pci_bus_res[bus].io_ports, io_size,
4759896aa55Sjveta 		    0x1000);
4769896aa55Sjveta 
4779896aa55Sjveta 	ASSERT(addr <= 0xf000);
4789896aa55Sjveta 	if (addr == 0) {
4799896aa55Sjveta 		cmn_err(CE_WARN, "out of I/O resources on bridge: bus 0x%x, "
4809896aa55Sjveta 		    "dev 0x%x, func 0x%x, for secondary bus 0x%x\n",
4819896aa55Sjveta 		    bus, dev, func, secbus);
4829896aa55Sjveta 		goto IOFAIL;
4839896aa55Sjveta 	}
4849896aa55Sjveta 
4859896aa55Sjveta 	memlist_insert(&pci_bus_res[secbus].io_ports, addr, io_size);
4869896aa55Sjveta 	io_base = addr;
4879896aa55Sjveta 	io_limit = addr + io_size - 1;
4889896aa55Sjveta 	pci_putb(bus, dev, func, PCI_BCNF_IO_BASE_LOW,
4899896aa55Sjveta 	    (uint8_t)((io_base >> 8) & 0xf0));
4909896aa55Sjveta 	pci_putb(bus, dev, func, PCI_BCNF_IO_LIMIT_LOW,
4919896aa55Sjveta 	    (uint8_t)((io_limit >> 8) & 0xf0));
4929896aa55Sjveta 
4939896aa55Sjveta 	add_ppb_ranges_prop(secbus);
4949896aa55Sjveta 	return;
4959896aa55Sjveta 
4969896aa55Sjveta 	/*NOTREACHED*/
4979896aa55Sjveta IOFAIL:
4989896aa55Sjveta 	cmn_err(CE_WARN, "devices under bridge bus 0x%x, dev 0x%x, func 0x%x "
4999896aa55Sjveta 	    "will not be assigned I/O ports\n", bus, dev, func);
5009896aa55Sjveta }
5019896aa55Sjveta 
5027c478bd9Sstevel@tonic-gate void
5037c478bd9Sstevel@tonic-gate pci_reprogram(void)
5047c478bd9Sstevel@tonic-gate {
5057c478bd9Sstevel@tonic-gate 	int i, pci_reconfig = 1;
5067c478bd9Sstevel@tonic-gate 	char *onoff;
5077c478bd9Sstevel@tonic-gate 
50825145214Smyers 	/*
50925145214Smyers 	 * Excise phantom roots if possible
51025145214Smyers 	 */
51125145214Smyers 	pci_renumber_root_busses();
51225145214Smyers 
513fc396574Srw 	/* add bus-range property for root/peer bus nodes */
514fc396574Srw 	for (i = 0; i <= pci_bios_nbus; i++) {
515fc396574Srw 		if (pci_bus_res[i].par_bus == (uchar_t)-1) {
516fc396574Srw 			uchar_t subbus;
517fc396574Srw 			if (pci_root_subbus(i, &subbus) == AE_OK)
5189896aa55Sjveta 				pci_bus_res[i].sub_bus = subbus;
519fc396574Srw 			add_bus_range_prop(i);
520fc396574Srw 		}
521fc396574Srw 	}
522fc396574Srw 
5237c478bd9Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
5247c478bd9Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "pci-reprog", &onoff) == DDI_SUCCESS) {
5257c478bd9Sstevel@tonic-gate 		if (strcmp(onoff, "off") == 0) {
5267c478bd9Sstevel@tonic-gate 			pci_reconfig = 0;
5277c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "pci device reprogramming disabled");
5287c478bd9Sstevel@tonic-gate 		}
5297c478bd9Sstevel@tonic-gate 		ddi_prop_free(onoff);
5307c478bd9Sstevel@tonic-gate 	}
5317c478bd9Sstevel@tonic-gate 
532aaba6dfeSmyers 	/* remove used-resources from PCI resource maps */
533aaba6dfeSmyers 	remove_used_resources();
534aaba6dfeSmyers 
5357c478bd9Sstevel@tonic-gate 	for (i = 0; i <= pci_bios_nbus; i++) {
5367c478bd9Sstevel@tonic-gate 		/* configure devices not configured by bios */
5379896aa55Sjveta 		if (pci_reconfig) {
5389896aa55Sjveta 			fix_ppb_res(i);
5397c478bd9Sstevel@tonic-gate 			enumerate_bus_devs(i, CONFIG_NEW);
5409896aa55Sjveta 		}
5417c478bd9Sstevel@tonic-gate 		/* All dev programmed, so we can create available prop */
5427c478bd9Sstevel@tonic-gate 		add_bus_available_prop(i);
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate }
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate /*
5477c478bd9Sstevel@tonic-gate  * Create top-level bus dips, i.e. /pci@0,0, /pci@1,0...
5487c478bd9Sstevel@tonic-gate  */
5497c478bd9Sstevel@tonic-gate static void
5507c478bd9Sstevel@tonic-gate create_root_bus_dip(uchar_t bus)
5517c478bd9Sstevel@tonic-gate {
5527c478bd9Sstevel@tonic-gate 	int pci_regs[] = {0, 0, 0};
5537c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	ASSERT(pci_bus_res[bus].par_bus == (uchar_t)-1);
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	ndi_devi_alloc_sleep(ddi_root_node(), "pci",
558fa9e4066Sahrens 	    (pnode_t)DEVI_SID_NODEID, &dip);
5597c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
5607c478bd9Sstevel@tonic-gate 	    "#address-cells", 3);
5617c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
5627c478bd9Sstevel@tonic-gate 	    "#size-cells", 2);
5637c478bd9Sstevel@tonic-gate 	pci_regs[0] = pci_bus_res[bus].root_addr;
5647c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
5657c478bd9Sstevel@tonic-gate 	    "reg", (int *)pci_regs, 3);
5667c478bd9Sstevel@tonic-gate 
56770025d76Sjohnny 	/*
56870025d76Sjohnny 	 * If system has PCIe bus, then create different properties
56970025d76Sjohnny 	 */
57070025d76Sjohnny 	if (create_pcie_root_bus(bus, dip) == B_FALSE)
57170025d76Sjohnny 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
57270025d76Sjohnny 		    "device_type", "pci");
57370025d76Sjohnny 
5747c478bd9Sstevel@tonic-gate 	(void) ndi_devi_bind_driver(dip, 0);
5757c478bd9Sstevel@tonic-gate 	pci_bus_res[bus].dip = dip;
5767c478bd9Sstevel@tonic-gate 	pci_bus_res[bus].pmem_space = find_bus_res(bus, PREFETCH_TYPE);
5777c478bd9Sstevel@tonic-gate 	pci_bus_res[bus].mem_space = find_bus_res(bus, MEM_TYPE);
5787c478bd9Sstevel@tonic-gate 	pci_bus_res[bus].io_ports = find_bus_res(bus, IO_TYPE);
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	if (bus != 0)
5817c478bd9Sstevel@tonic-gate 		return;
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	/*
5847c478bd9Sstevel@tonic-gate 	 * Special treatment of bus 0:
5857c478bd9Sstevel@tonic-gate 	 * If no resource from MPSPEC/HRT, copy pcimem from boot
586aaba6dfeSmyers 	 * and make I/O space the entire range starting at 0x100. There
587aaba6dfeSmyers 	 * is no difference between prefetchable memory or not.
5887c478bd9Sstevel@tonic-gate 	 */
5897c478bd9Sstevel@tonic-gate 	if (pci_bus_res[0].mem_space == NULL)
5907c478bd9Sstevel@tonic-gate 		pci_bus_res[0].mem_space =
5917c478bd9Sstevel@tonic-gate 		    memlist_dup(bootops->boot_mem->pcimem);
592aaba6dfeSmyers 	/* Exclude 0x00 to 0xff of the I/O space, used by all PCs */
5937c478bd9Sstevel@tonic-gate 	if (pci_bus_res[0].io_ports == NULL)
594aaba6dfeSmyers 		memlist_insert(&pci_bus_res[0].io_ports, 0x100, 0xff00);
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate /*
5987c478bd9Sstevel@tonic-gate  * For any fixed configuration (often compatability) pci devices
5997c478bd9Sstevel@tonic-gate  * and those with their own expansion rom, create device nodes
6007c478bd9Sstevel@tonic-gate  * to hold the already configured device details.
6017c478bd9Sstevel@tonic-gate  */
6027c478bd9Sstevel@tonic-gate void
6037c478bd9Sstevel@tonic-gate enumerate_bus_devs(uchar_t bus, int config_op)
6047c478bd9Sstevel@tonic-gate {
6057c478bd9Sstevel@tonic-gate 	uchar_t dev, func, nfunc, header;
6067c478bd9Sstevel@tonic-gate 	ushort_t venid;
6077c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
6087c478bd9Sstevel@tonic-gate 	struct pci_devfunc {
6097c478bd9Sstevel@tonic-gate 		struct pci_devfunc *next;
6107c478bd9Sstevel@tonic-gate 		dev_info_t *dip;
6117c478bd9Sstevel@tonic-gate 		uchar_t bus;
6127c478bd9Sstevel@tonic-gate 		uchar_t dev;
6137c478bd9Sstevel@tonic-gate 		uchar_t func;
6147c478bd9Sstevel@tonic-gate 	} *devlist = NULL, *entry;
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	if (config_op == CONFIG_NEW) {
6177c478bd9Sstevel@tonic-gate 		dcmn_err(CE_NOTE, "configuring pci bus 0x%x", bus);
618bd87be88Ssethg 	} else if (config_op == CONFIG_FIX) {
619bd87be88Ssethg 		dcmn_err(CE_NOTE, "fixing devices on pci bus 0x%x", bus);
6207c478bd9Sstevel@tonic-gate 	} else
6217c478bd9Sstevel@tonic-gate 		dcmn_err(CE_NOTE, "enumerating pci bus 0x%x", bus);
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	for (dev = 0; dev < max_dev_pci; dev++) {
6247c478bd9Sstevel@tonic-gate 		nfunc = 1;
6257c478bd9Sstevel@tonic-gate 		for (func = 0; func < nfunc; func++) {
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 			dcmn_err(CE_NOTE, "probing dev 0x%x, func 0x%x",
6287c478bd9Sstevel@tonic-gate 			    dev, func);
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 			venid = pci_getw(bus, dev, func, PCI_CONF_VENID);
631bd87be88Ssethg 
6327c478bd9Sstevel@tonic-gate 			if ((venid == 0xffff) || (venid == 0)) {
6337c478bd9Sstevel@tonic-gate 				/* no function at this address */
6347c478bd9Sstevel@tonic-gate 				continue;
6357c478bd9Sstevel@tonic-gate 			}
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 			header = pci_getb(bus, dev, func, PCI_CONF_HEADER);
6387c478bd9Sstevel@tonic-gate 			if (header == 0xff) {
6397c478bd9Sstevel@tonic-gate 				continue; /* illegal value */
6407c478bd9Sstevel@tonic-gate 			}
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 			/*
6437c478bd9Sstevel@tonic-gate 			 * according to some mail from Microsoft posted
6447c478bd9Sstevel@tonic-gate 			 * to the pci-drivers alias, their only requirement
6457c478bd9Sstevel@tonic-gate 			 * for a multifunction device is for the 1st
6467c478bd9Sstevel@tonic-gate 			 * function to have to PCI_HEADER_MULTI bit set.
6477c478bd9Sstevel@tonic-gate 			 */
6487c478bd9Sstevel@tonic-gate 			if ((func == 0) && (header & PCI_HEADER_MULTI)) {
6497c478bd9Sstevel@tonic-gate 				nfunc = 8;
6507c478bd9Sstevel@tonic-gate 			}
65146e9e839Smyers 
652bd87be88Ssethg 			if (config_op == CONFIG_FIX) {
653bd87be88Ssethg 				/*
654bd87be88Ssethg 				 * If we're processing PCI fixes, no dip
655bd87be88Ssethg 				 * will be returned.
656bd87be88Ssethg 				 */
657bd87be88Ssethg 				(void) process_devfunc(bus, dev, func, header,
658bd87be88Ssethg 				    venid, config_op);
659bd87be88Ssethg 
660bd87be88Ssethg 			} else if (config_op == CONFIG_INFO) {
661ebf3afa8Sdmick 				/*
662ebf3afa8Sdmick 				 * Create the node, unconditionally, on the
663ebf3afa8Sdmick 				 * first pass only.  It may still need
664ebf3afa8Sdmick 				 * resource assignment, which will be
665ebf3afa8Sdmick 				 * done on the second, CONFIG_NEW, pass.
666ebf3afa8Sdmick 				 */
667bd87be88Ssethg 				dip = process_devfunc(bus, dev, func, header,
668ebf3afa8Sdmick 				    venid, config_op);
669db063408Sdmick 				/*
670db063408Sdmick 				 * If dip isn't null, put on a list to
671db063408Sdmick 				 * save for reprogramming when config_op
672db063408Sdmick 				 * is CONFIG_NEW.
673db063408Sdmick 				 */
674db063408Sdmick 
675db063408Sdmick 				if (dip) {
676db063408Sdmick 					entry = kmem_alloc(sizeof (*entry),
677db063408Sdmick 					    KM_SLEEP);
678db063408Sdmick 					entry->dip = dip;
679db063408Sdmick 					entry->dev = dev;
680db063408Sdmick 					entry->func = func;
681db063408Sdmick 					entry->next = devlist;
682db063408Sdmick 					devlist = entry;
683db063408Sdmick 				}
6847c478bd9Sstevel@tonic-gate 			}
6857c478bd9Sstevel@tonic-gate 		}
6867c478bd9Sstevel@tonic-gate 	}
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	if (config_op == CONFIG_NEW) {
6897c478bd9Sstevel@tonic-gate 		devlist = (struct pci_devfunc *)pci_bus_res[bus].privdata;
6907c478bd9Sstevel@tonic-gate 		while (devlist) {
6917c478bd9Sstevel@tonic-gate 			entry = devlist;
6927c478bd9Sstevel@tonic-gate 			devlist = entry->next;
6937c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE,
6947c478bd9Sstevel@tonic-gate 			    "!reprogram pci device [%d/%d/%d] (%s)",
6957c478bd9Sstevel@tonic-gate 			    bus, entry->dev, entry->func,
6967c478bd9Sstevel@tonic-gate 			    ddi_driver_name(entry->dip));
6977c478bd9Sstevel@tonic-gate 			(void) add_reg_props(entry->dip, bus, entry->dev,
6987c478bd9Sstevel@tonic-gate 			    entry->func, CONFIG_UPDATE, 0);
6997c478bd9Sstevel@tonic-gate 			kmem_free(entry, sizeof (*entry));
7007c478bd9Sstevel@tonic-gate 		}
7017c478bd9Sstevel@tonic-gate 		pci_bus_res[bus].privdata = NULL;
702bd87be88Ssethg 	} else if (config_op != CONFIG_FIX) {
7037c478bd9Sstevel@tonic-gate 		pci_bus_res[bus].privdata = devlist;
7047c478bd9Sstevel@tonic-gate 	}
7057c478bd9Sstevel@tonic-gate }
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate static int
7087c478bd9Sstevel@tonic-gate check_pciide_prop(uchar_t revid, ushort_t venid, ushort_t devid,
7097c478bd9Sstevel@tonic-gate     ushort_t subvenid, ushort_t subdevid)
7107c478bd9Sstevel@tonic-gate {
7117c478bd9Sstevel@tonic-gate 	static int prop_exist = -1;
7127c478bd9Sstevel@tonic-gate 	static char *pciide_str;
7137c478bd9Sstevel@tonic-gate 	char compat[32];
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	if (prop_exist == -1) {
7167c478bd9Sstevel@tonic-gate 		prop_exist = (ddi_prop_lookup_string(DDI_DEV_T_ANY,
7177c478bd9Sstevel@tonic-gate 		    ddi_root_node(), DDI_PROP_DONTPASS, "pci-ide",
7187c478bd9Sstevel@tonic-gate 		    &pciide_str) == DDI_SUCCESS);
7197c478bd9Sstevel@tonic-gate 	}
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	if (!prop_exist)
7227c478bd9Sstevel@tonic-gate 		return (0);
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	/* compare property value against various forms of compatible */
7257c478bd9Sstevel@tonic-gate 	if (subvenid) {
7267c478bd9Sstevel@tonic-gate 		(void) snprintf(compat, sizeof (compat), "pci%x,%x.%x.%x.%x",
7277c478bd9Sstevel@tonic-gate 		    venid, devid, subvenid, subdevid, revid);
7287c478bd9Sstevel@tonic-gate 		if (strcmp(pciide_str, compat) == 0)
7297c478bd9Sstevel@tonic-gate 			return (1);
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 		(void) snprintf(compat, sizeof (compat), "pci%x,%x.%x.%x",
7327c478bd9Sstevel@tonic-gate 		    venid, devid, subvenid, subdevid);
7337c478bd9Sstevel@tonic-gate 		if (strcmp(pciide_str, compat) == 0)
7347c478bd9Sstevel@tonic-gate 			return (1);
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 		(void) snprintf(compat, sizeof (compat), "pci%x,%x",
7377c478bd9Sstevel@tonic-gate 		    subvenid, subdevid);
7387c478bd9Sstevel@tonic-gate 		if (strcmp(pciide_str, compat) == 0)
7397c478bd9Sstevel@tonic-gate 			return (1);
7407c478bd9Sstevel@tonic-gate 	}
7417c478bd9Sstevel@tonic-gate 	(void) snprintf(compat, sizeof (compat), "pci%x,%x.%x",
7427c478bd9Sstevel@tonic-gate 	    venid, devid, revid);
7437c478bd9Sstevel@tonic-gate 	if (strcmp(pciide_str, compat) == 0)
7447c478bd9Sstevel@tonic-gate 		return (1);
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	(void) snprintf(compat, sizeof (compat), "pci%x,%x", venid, devid);
7477c478bd9Sstevel@tonic-gate 	if (strcmp(pciide_str, compat) == 0)
7487c478bd9Sstevel@tonic-gate 		return (1);
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	return (0);
7517c478bd9Sstevel@tonic-gate }
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate static int
7547c478bd9Sstevel@tonic-gate is_pciide(uchar_t basecl, uchar_t subcl, uchar_t revid,
7557c478bd9Sstevel@tonic-gate     ushort_t venid, ushort_t devid, ushort_t subvenid, ushort_t subdevid)
7567c478bd9Sstevel@tonic-gate {
7577c478bd9Sstevel@tonic-gate 	struct ide_table {	/* table for PCI_MASS_OTHER */
7587c478bd9Sstevel@tonic-gate 		ushort_t venid;
7597c478bd9Sstevel@tonic-gate 		ushort_t devid;
7607c478bd9Sstevel@tonic-gate 	} *entry;
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	/* XXX SATA devices: need a way to add dynamically */
7637c478bd9Sstevel@tonic-gate 	static struct ide_table ide_other[] = {
7647c478bd9Sstevel@tonic-gate 		{0x1095, 0x3112},
7657c478bd9Sstevel@tonic-gate 		{0x1095, 0x3114},
7667c478bd9Sstevel@tonic-gate 		{0x1095, 0x3512},
7677c478bd9Sstevel@tonic-gate 		{0, 0}
7687c478bd9Sstevel@tonic-gate 	};
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	if (basecl != PCI_CLASS_MASS)
7717c478bd9Sstevel@tonic-gate 		return (0);
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	if (subcl == PCI_MASS_IDE) {
7747c478bd9Sstevel@tonic-gate 		return (1);
7757c478bd9Sstevel@tonic-gate 	}
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 	if (subcl != PCI_MASS_OTHER && subcl != PCI_MASS_SATA) {
7787c478bd9Sstevel@tonic-gate 		return (0);
7797c478bd9Sstevel@tonic-gate 	}
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	entry = &ide_other[0];
7827c478bd9Sstevel@tonic-gate 	while (entry->venid) {
7837c478bd9Sstevel@tonic-gate 		if (entry->venid == venid && entry->devid == devid)
7847c478bd9Sstevel@tonic-gate 			return (1);
7857c478bd9Sstevel@tonic-gate 		entry++;
7867c478bd9Sstevel@tonic-gate 	}
7877c478bd9Sstevel@tonic-gate 	return (check_pciide_prop(revid, venid, devid, subvenid, subdevid));
7887c478bd9Sstevel@tonic-gate }
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate static int
7917c478bd9Sstevel@tonic-gate is_display(uint_t classcode)
7927c478bd9Sstevel@tonic-gate {
7937c478bd9Sstevel@tonic-gate 	static uint_t disp_classes[] = {
7947c478bd9Sstevel@tonic-gate 		0x000100,
7957c478bd9Sstevel@tonic-gate 		0x030000,
7967c478bd9Sstevel@tonic-gate 		0x030001
7977c478bd9Sstevel@tonic-gate 	};
7987c478bd9Sstevel@tonic-gate 	int i, nclasses = sizeof (disp_classes) / sizeof (uint_t);
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	for (i = 0; i < nclasses; i++) {
8017c478bd9Sstevel@tonic-gate 		if (classcode == disp_classes[i])
8027c478bd9Sstevel@tonic-gate 			return (1);
8037c478bd9Sstevel@tonic-gate 	}
8047c478bd9Sstevel@tonic-gate 	return (0);
8057c478bd9Sstevel@tonic-gate }
8067c478bd9Sstevel@tonic-gate 
807bd87be88Ssethg static void
808bd87be88Ssethg add_undofix_entry(uint8_t bus, uint8_t dev, uint8_t fn,
809bd87be88Ssethg     void (*undofn)(uint8_t, uint8_t, uint8_t))
810bd87be88Ssethg {
811bd87be88Ssethg 	struct pci_fixundo *newundo;
812bd87be88Ssethg 
813bd87be88Ssethg 	newundo = kmem_alloc(sizeof (struct pci_fixundo), KM_SLEEP);
814bd87be88Ssethg 
815bd87be88Ssethg 	/*
816bd87be88Ssethg 	 * Adding an item to this list means that we must turn its NMIENABLE
817bd87be88Ssethg 	 * bit back on at a later time.
818bd87be88Ssethg 	 */
819bd87be88Ssethg 	newundo->bus = bus;
820bd87be88Ssethg 	newundo->dev = dev;
821bd87be88Ssethg 	newundo->fn = fn;
822bd87be88Ssethg 	newundo->undofn = undofn;
823bd87be88Ssethg 	newundo->next = undolist;
824bd87be88Ssethg 
825bd87be88Ssethg 	/* add to the undo list in LIFO order */
826bd87be88Ssethg 	undolist = newundo;
827bd87be88Ssethg }
828bd87be88Ssethg 
829bd87be88Ssethg void
830bd87be88Ssethg add_pci_fixes(void)
831bd87be88Ssethg {
832bd87be88Ssethg 	int i;
833bd87be88Ssethg 
834bd87be88Ssethg 	for (i = 0; i <= pci_bios_nbus; i++) {
835bd87be88Ssethg 		/*
836bd87be88Ssethg 		 * For each bus, apply needed fixes to the appropriate devices.
837bd87be88Ssethg 		 * This must be done before the main enumeration loop because
838bd87be88Ssethg 		 * some fixes must be applied to devices normally encountered
839bd87be88Ssethg 		 * later in the pci scan (e.g. if a fix to device 7 must be
840bd87be88Ssethg 		 * applied before scanning device 6, applying fixes in the
841bd87be88Ssethg 		 * normal enumeration loop would obviously be too late).
842bd87be88Ssethg 		 */
843bd87be88Ssethg 		enumerate_bus_devs(i, CONFIG_FIX);
844bd87be88Ssethg 	}
845bd87be88Ssethg }
846bd87be88Ssethg 
847bd87be88Ssethg void
848bd87be88Ssethg undo_pci_fixes(void)
849bd87be88Ssethg {
850bd87be88Ssethg 	struct pci_fixundo *nextundo;
851bd87be88Ssethg 	uint8_t bus, dev, fn;
852bd87be88Ssethg 
853bd87be88Ssethg 	/*
854bd87be88Ssethg 	 * All fixes in the undo list are performed unconditionally.  Future
855bd87be88Ssethg 	 * fixes may require selective undo.
856bd87be88Ssethg 	 */
857bd87be88Ssethg 	while (undolist != NULL) {
858bd87be88Ssethg 
859bd87be88Ssethg 		bus = undolist->bus;
860bd87be88Ssethg 		dev = undolist->dev;
861bd87be88Ssethg 		fn = undolist->fn;
862bd87be88Ssethg 
863bd87be88Ssethg 		(*(undolist->undofn))(bus, dev, fn);
864bd87be88Ssethg 
865bd87be88Ssethg 		nextundo = undolist->next;
866bd87be88Ssethg 		kmem_free(undolist, sizeof (struct pci_fixundo));
867bd87be88Ssethg 		undolist = nextundo;
868bd87be88Ssethg 	}
869bd87be88Ssethg }
870bd87be88Ssethg 
871bd87be88Ssethg static void
872bd87be88Ssethg undo_amd8111_pci_fix(uint8_t bus, uint8_t dev, uint8_t fn)
873bd87be88Ssethg {
874bd87be88Ssethg 	uint8_t val8;
875bd87be88Ssethg 
876bd87be88Ssethg 	val8 = pci_getb(bus, dev, fn, LPC_IO_CONTROL_REG_1);
877bd87be88Ssethg 	/*
878bd87be88Ssethg 	 * The NMIONERR bit is turned back on to allow the SMM BIOS
879bd87be88Ssethg 	 * to handle more critical PCI errors (e.g. PERR#).
880bd87be88Ssethg 	 */
881bd87be88Ssethg 	val8 |= AMD8111_ENABLENMI;
882bd87be88Ssethg 	pci_putb(bus, dev, fn, LPC_IO_CONTROL_REG_1, val8);
883bd87be88Ssethg }
884bd87be88Ssethg 
885bd87be88Ssethg static void
886bd87be88Ssethg pci_fix_amd8111(uint8_t bus, uint8_t dev, uint8_t fn)
887bd87be88Ssethg {
888bd87be88Ssethg 	uint8_t val8;
889bd87be88Ssethg 
890bd87be88Ssethg 	val8 = pci_getb(bus, dev, fn, LPC_IO_CONTROL_REG_1);
891bd87be88Ssethg 
892bd87be88Ssethg 	if ((val8 & AMD8111_ENABLENMI) == 0)
893bd87be88Ssethg 		return;
894bd87be88Ssethg 
895bd87be88Ssethg 	/*
896bd87be88Ssethg 	 * We reset NMIONERR in the LPC because master-abort on the PCI
897bd87be88Ssethg 	 * bridge side of the 8111 will cause NMI, which might cause SMI,
898bd87be88Ssethg 	 * which sometimes prevents all devices from being enumerated.
899bd87be88Ssethg 	 */
900bd87be88Ssethg 	val8 &= ~AMD8111_ENABLENMI;
901bd87be88Ssethg 
902bd87be88Ssethg 	pci_putb(bus, dev, fn, LPC_IO_CONTROL_REG_1, val8);
903bd87be88Ssethg 
904bd87be88Ssethg 	add_undofix_entry(bus, dev, fn, undo_amd8111_pci_fix);
905bd87be88Ssethg }
906bd87be88Ssethg 
9077c478bd9Sstevel@tonic-gate static dev_info_t *
908bd87be88Ssethg process_devfunc(uchar_t bus, uchar_t dev, uchar_t func, uchar_t header,
9097c478bd9Sstevel@tonic-gate     ushort_t vendorid, int config_op)
9107c478bd9Sstevel@tonic-gate {
9117c478bd9Sstevel@tonic-gate 	char nodename[32], unitaddr[5];
9127c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
913c8589f13Ssethg 	uchar_t basecl, subcl, progcl, intr, revid;
9147c478bd9Sstevel@tonic-gate 	ushort_t subvenid, subdevid, status;
91570025d76Sjohnny 	ushort_t slot_num;
9167c478bd9Sstevel@tonic-gate 	uint_t classcode, revclass;
917*8d483882Smlf 	int reprogram = 0, pciide = 0;
9187c478bd9Sstevel@tonic-gate 	int power[2] = {1, 1};
91970025d76Sjohnny 	int pciex = 0;
92070025d76Sjohnny 	ushort_t is_pci_bridge = 0;
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	ushort_t deviceid = pci_getw(bus, dev, func, PCI_CONF_DEVID);
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	switch (header & PCI_HEADER_TYPE_M) {
9257c478bd9Sstevel@tonic-gate 	case PCI_HEADER_ZERO:
9267c478bd9Sstevel@tonic-gate 		subvenid = pci_getw(bus, dev, func, PCI_CONF_SUBVENID);
9277c478bd9Sstevel@tonic-gate 		subdevid = pci_getw(bus, dev, func, PCI_CONF_SUBSYSID);
9287c478bd9Sstevel@tonic-gate 		break;
9297c478bd9Sstevel@tonic-gate 	case PCI_HEADER_CARDBUS:
9307c478bd9Sstevel@tonic-gate 		subvenid = pci_getw(bus, dev, func, PCI_CBUS_SUBVENID);
9317c478bd9Sstevel@tonic-gate 		subdevid = pci_getw(bus, dev, func, PCI_CBUS_SUBSYSID);
9327c478bd9Sstevel@tonic-gate 		break;
9337c478bd9Sstevel@tonic-gate 	default:
9347c478bd9Sstevel@tonic-gate 		subvenid = 0;
9357c478bd9Sstevel@tonic-gate 		subdevid = 0;
9367c478bd9Sstevel@tonic-gate 		break;
9377c478bd9Sstevel@tonic-gate 	}
9387c478bd9Sstevel@tonic-gate 
939bd87be88Ssethg 	if (config_op == CONFIG_FIX) {
940bd87be88Ssethg 		if (vendorid == VENID_AMD && deviceid == DEVID_AMD8111_LPC) {
941bd87be88Ssethg 			pci_fix_amd8111(bus, dev, func);
942bd87be88Ssethg 		}
943bd87be88Ssethg 		return (NULL);
944bd87be88Ssethg 	}
945bd87be88Ssethg 
9467c478bd9Sstevel@tonic-gate 	/* XXX should be use generic names? derive from class? */
9477c478bd9Sstevel@tonic-gate 	revclass = pci_getl(bus, dev, func, PCI_CONF_REVID);
9487c478bd9Sstevel@tonic-gate 	classcode = revclass >> 8;
9497c478bd9Sstevel@tonic-gate 	revid = revclass & 0xff;
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	/* figure out if this is pci-ide */
9527c478bd9Sstevel@tonic-gate 	basecl = classcode >> 16;
9537c478bd9Sstevel@tonic-gate 	subcl = (classcode >> 8) & 0xff;
954c8589f13Ssethg 	progcl = classcode & 0xff;
9557c478bd9Sstevel@tonic-gate 
956*8d483882Smlf 
957*8d483882Smlf 	if (is_display(classcode))
9587c478bd9Sstevel@tonic-gate 		(void) snprintf(nodename, sizeof (nodename), "display");
9597c478bd9Sstevel@tonic-gate 	else if (subvenid != 0)
9607c478bd9Sstevel@tonic-gate 		(void) snprintf(nodename, sizeof (nodename),
9617c478bd9Sstevel@tonic-gate 		    "pci%x,%x", subvenid, subdevid);
9627c478bd9Sstevel@tonic-gate 	else
9637c478bd9Sstevel@tonic-gate 		(void) snprintf(nodename, sizeof (nodename),
9647c478bd9Sstevel@tonic-gate 		    "pci%x,%x", vendorid, deviceid);
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 	/* make sure parent bus dip has been created */
9677c478bd9Sstevel@tonic-gate 	if (pci_bus_res[bus].dip == NULL) {
9687c478bd9Sstevel@tonic-gate 		create_root_bus_dip(bus);
9697c478bd9Sstevel@tonic-gate 	}
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	ndi_devi_alloc_sleep(pci_bus_res[bus].dip, nodename,
9727c478bd9Sstevel@tonic-gate 	    DEVI_SID_NODEID, &dip);
9737c478bd9Sstevel@tonic-gate 
97400d0963fSdilpreet 	if (check_if_device_is_pciex(dip, bus, dev, func, &slot_num,
97500d0963fSdilpreet 	    &is_pci_bridge) == B_TRUE)
97600d0963fSdilpreet 		pciex = 1;
97700d0963fSdilpreet 
9787c478bd9Sstevel@tonic-gate 	/* add properties */
9797c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, "device-id", deviceid);
9807c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, "vendor-id", vendorid);
9817c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, "revision-id", revid);
9827c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
9837c478bd9Sstevel@tonic-gate 	    "class-code", classcode);
9847c478bd9Sstevel@tonic-gate 	if (func == 0)
9857c478bd9Sstevel@tonic-gate 		(void) snprintf(unitaddr, sizeof (unitaddr), "%x", dev);
9867c478bd9Sstevel@tonic-gate 	else
9877c478bd9Sstevel@tonic-gate 		(void) snprintf(unitaddr, sizeof (unitaddr),
9887c478bd9Sstevel@tonic-gate 		    "%x,%x", dev, func);
9897c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
9907c478bd9Sstevel@tonic-gate 	    "unit-address", unitaddr);
9917c478bd9Sstevel@tonic-gate 
992ebf3afa8Sdmick 	/* add device_type for display nodes */
993ebf3afa8Sdmick 	if (is_display(classcode)) {
994ebf3afa8Sdmick 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
995ebf3afa8Sdmick 		    "device_type", "display");
996ebf3afa8Sdmick 	}
9977c478bd9Sstevel@tonic-gate 	/* add special stuff for header type */
9987c478bd9Sstevel@tonic-gate 	if ((header & PCI_HEADER_TYPE_M) == PCI_HEADER_ZERO) {
9997c478bd9Sstevel@tonic-gate 		uchar_t mingrant = pci_getb(bus, dev, func, PCI_CONF_MIN_G);
10007c478bd9Sstevel@tonic-gate 		uchar_t maxlatency = pci_getb(bus, dev, func, PCI_CONF_MAX_L);
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 		if (subvenid != 0) {
10037c478bd9Sstevel@tonic-gate 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
10047c478bd9Sstevel@tonic-gate 			    "subsystem-id", subdevid);
10057c478bd9Sstevel@tonic-gate 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
10067c478bd9Sstevel@tonic-gate 			    "subsystem-vendor-id", subvenid);
10077c478bd9Sstevel@tonic-gate 		}
100870025d76Sjohnny 		if (!pciex)
100970025d76Sjohnny 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
101070025d76Sjohnny 			    "min-grant", mingrant);
101170025d76Sjohnny 		if (!pciex)
101270025d76Sjohnny 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
101370025d76Sjohnny 			    "max-latency", maxlatency);
10147c478bd9Sstevel@tonic-gate 	}
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	/* interrupt, record if not 0 */
10177c478bd9Sstevel@tonic-gate 	intr = pci_getb(bus, dev, func, PCI_CONF_IPIN);
10187c478bd9Sstevel@tonic-gate 	if (intr != 0)
10197c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
10207c478bd9Sstevel@tonic-gate 		    "interrupts", intr);
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	/*
10237c478bd9Sstevel@tonic-gate 	 * Add support for 133 mhz pci eventually
10247c478bd9Sstevel@tonic-gate 	 */
10257c478bd9Sstevel@tonic-gate 	status = pci_getw(bus, dev, func, PCI_CONF_STAT);
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
10287c478bd9Sstevel@tonic-gate 	    "devsel-speed", (status & PCI_STAT_DEVSELT) >> 9);
102970025d76Sjohnny 	if (!pciex && (status & PCI_STAT_FBBC))
10307c478bd9Sstevel@tonic-gate 		(void) ndi_prop_create_boolean(DDI_DEV_T_NONE, dip,
10317c478bd9Sstevel@tonic-gate 		    "fast-back-to-back");
103270025d76Sjohnny 	if (!pciex && (status & PCI_STAT_66MHZ))
10337c478bd9Sstevel@tonic-gate 		(void) ndi_prop_create_boolean(DDI_DEV_T_NONE, dip,
10347c478bd9Sstevel@tonic-gate 		    "66mhz-capable");
10357c478bd9Sstevel@tonic-gate 	if (status & PCI_STAT_UDF)
10367c478bd9Sstevel@tonic-gate 		(void) ndi_prop_create_boolean(DDI_DEV_T_NONE, dip,
10377c478bd9Sstevel@tonic-gate 		    "udf-supported");
1038e14a96cdSanish 	if (pciex && slot_num)
103970025d76Sjohnny 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
104070025d76Sjohnny 		    "physical-slot#", slot_num);
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
10437c478bd9Sstevel@tonic-gate 	    "power-consumption", power, 2);
10447c478bd9Sstevel@tonic-gate 
104570025d76Sjohnny 	if ((basecl == PCI_CLASS_BRIDGE) && (subcl == PCI_BRIDGE_PCI))
104670025d76Sjohnny 		add_ppb_props(dip, bus, dev, func, pciex);
104770025d76Sjohnny 
1048c8589f13Ssethg 	if (config_op == CONFIG_INFO &&
1049c8589f13Ssethg 	    IS_CLASS_IOAPIC(basecl, subcl, progcl)) {
1050c8589f13Ssethg 		create_ioapic_node(bus, dev, func, vendorid, deviceid);
1051c8589f13Ssethg 	}
1052c8589f13Ssethg 
105370025d76Sjohnny 	/* check for ck8-04 based PCI ISA bridge only */
105470025d76Sjohnny 	if (NVIDIA_IS_LPC_BRIDGE(vendorid, deviceid) && (dev == 1) &&
105570025d76Sjohnny 	    (func == 0))
10568a5a0d1eSanish 		add_nvidia_isa_bridge_props(dip, bus, dev, func);
105770025d76Sjohnny 
105870025d76Sjohnny 	if (pciex && is_pci_bridge)
105970025d76Sjohnny 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, "model",
106070025d76Sjohnny 		    (char *)"PCIe-PCI bridge");
106170025d76Sjohnny 	else
106270025d76Sjohnny 		add_model_prop(dip, classcode);
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	add_compatible(dip, subvenid, subdevid, vendorid, deviceid,
106570025d76Sjohnny 	    revid, classcode, pciex);
1066*8d483882Smlf 
1067*8d483882Smlf 	/*
1068*8d483882Smlf 	 * See if this device is a controller that advertises
1069*8d483882Smlf 	 * itself to be a standard ATA task file controller, or one that
1070*8d483882Smlf 	 * has been hard coded.
1071*8d483882Smlf 	 *
1072*8d483882Smlf 	 * If it is, check if any other higher precedence driver listed in
1073*8d483882Smlf 	 * driver_aliases will claim the node by calling
1074*8d483882Smlf 	 * ddi_compatibile_driver_major.  If so, clear pciide and do not
1075*8d483882Smlf 	 * create a pci-ide node or any other special handling.
1076*8d483882Smlf 	 *
1077*8d483882Smlf 	 * If another driver does not bind, set the node name to pci-ide
1078*8d483882Smlf 	 * and then let the special pci-ide handling for registers and
1079*8d483882Smlf 	 * child pci-ide nodes proceed below.
1080*8d483882Smlf 	 */
1081*8d483882Smlf 	if (is_pciide(basecl, subcl, revid, vendorid, deviceid,
1082*8d483882Smlf 	    subvenid, subdevid) == 1) {
1083*8d483882Smlf 		if (ddi_compatible_driver_major(dip, NULL) == (major_t)-1) {
1084*8d483882Smlf 			(void) ndi_devi_set_nodename(dip, "pci-ide", 0);
1085*8d483882Smlf 			pciide = 1;
1086*8d483882Smlf 		}
1087*8d483882Smlf 	}
1088*8d483882Smlf 
10897c478bd9Sstevel@tonic-gate 	reprogram = add_reg_props(dip, bus, dev, func, config_op, pciide);
10907c478bd9Sstevel@tonic-gate 	(void) ndi_devi_bind_driver(dip, 0);
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	/* special handling for pci-ide */
10937c478bd9Sstevel@tonic-gate 	if (pciide) {
10947c478bd9Sstevel@tonic-gate 		dev_info_t *cdip;
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 		/*
10977c478bd9Sstevel@tonic-gate 		 * Create properties specified by P1275 Working Group
10987c478bd9Sstevel@tonic-gate 		 * Proposal #414 Version 1
10997c478bd9Sstevel@tonic-gate 		 */
11007c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
11017c478bd9Sstevel@tonic-gate 		    "device_type", "pci-ide");
11027c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
11037c478bd9Sstevel@tonic-gate 		    "#address-cells", 1);
11047c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
11057c478bd9Sstevel@tonic-gate 		    "#size-cells", 0);
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 		/* allocate two child nodes */
11087c478bd9Sstevel@tonic-gate 		ndi_devi_alloc_sleep(dip, "ide",
1109fa9e4066Sahrens 		    (pnode_t)DEVI_SID_NODEID, &cdip);
11107c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cdip,
11117c478bd9Sstevel@tonic-gate 		    "reg", 0);
11127c478bd9Sstevel@tonic-gate 		(void) ndi_devi_bind_driver(cdip, 0);
11137c478bd9Sstevel@tonic-gate 		ndi_devi_alloc_sleep(dip, "ide",
1114fa9e4066Sahrens 		    (pnode_t)DEVI_SID_NODEID, &cdip);
11157c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cdip,
11167c478bd9Sstevel@tonic-gate 		    "reg", 1);
11177c478bd9Sstevel@tonic-gate 		(void) ndi_devi_bind_driver(cdip, 0);
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 		reprogram = 0;	/* don't reprogram pci-ide bridge */
11207c478bd9Sstevel@tonic-gate 	}
11217c478bd9Sstevel@tonic-gate 
112270025d76Sjohnny 
11237c478bd9Sstevel@tonic-gate 	if (reprogram)
11247c478bd9Sstevel@tonic-gate 		return (dip);
11257c478bd9Sstevel@tonic-gate 	return (NULL);
11267c478bd9Sstevel@tonic-gate }
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate /*
11297c478bd9Sstevel@tonic-gate  * Set the compatible property to a value compliant with
11307c478bd9Sstevel@tonic-gate  * rev 2.1 of the IEEE1275 PCI binding.
113170025d76Sjohnny  * (Also used for PCI-Express devices).
11327c478bd9Sstevel@tonic-gate  *
11337c478bd9Sstevel@tonic-gate  *   pciVVVV,DDDD.SSSS.ssss.RR	(0)
11347c478bd9Sstevel@tonic-gate  *   pciVVVV,DDDD.SSSS.ssss	(1)
11357c478bd9Sstevel@tonic-gate  *   pciSSSS,ssss		(2)
11367c478bd9Sstevel@tonic-gate  *   pciVVVV,DDDD.RR		(3)
11377c478bd9Sstevel@tonic-gate  *   pciVVVV,DDDD		(4)
11387c478bd9Sstevel@tonic-gate  *   pciclass,CCSSPP		(5)
11397c478bd9Sstevel@tonic-gate  *   pciclass,CCSS		(6)
11407c478bd9Sstevel@tonic-gate  *
11417c478bd9Sstevel@tonic-gate  * The Subsystem (SSSS) forms are not inserted if
11427c478bd9Sstevel@tonic-gate  * subsystem-vendor-id is 0.
11437c478bd9Sstevel@tonic-gate  *
114470025d76Sjohnny  * NOTE: For PCI-Express devices "pci" is replaced with "pciex" in 0-6 above
114570025d76Sjohnny  * property 2 is not created as per "1275 bindings for PCI Express Interconnect"
114670025d76Sjohnny  *
11477c478bd9Sstevel@tonic-gate  * Set with setprop and \x00 between each
11487c478bd9Sstevel@tonic-gate  * to generate the encoded string array form.
11497c478bd9Sstevel@tonic-gate  */
11507c478bd9Sstevel@tonic-gate void
11517c478bd9Sstevel@tonic-gate add_compatible(dev_info_t *dip, ushort_t subvenid, ushort_t subdevid,
115270025d76Sjohnny     ushort_t vendorid, ushort_t deviceid, uchar_t revid, uint_t classcode,
115370025d76Sjohnny     int pciex)
11547c478bd9Sstevel@tonic-gate {
115570025d76Sjohnny 	int i = 0;
115670025d76Sjohnny 	int size = COMPAT_BUFSIZE;
115770025d76Sjohnny 	char *compat[13];
11587c478bd9Sstevel@tonic-gate 	char *buf, *curr;
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 	curr = buf = kmem_alloc(size, KM_SLEEP);
11617c478bd9Sstevel@tonic-gate 
116270025d76Sjohnny 	if (pciex) {
116370025d76Sjohnny 		if (subvenid) {
116470025d76Sjohnny 			compat[i++] = curr;	/* form 0 */
116570025d76Sjohnny 			(void) snprintf(curr, size, "pciex%x,%x.%x.%x.%x",
116670025d76Sjohnny 			    vendorid, deviceid, subvenid, subdevid, revid);
116770025d76Sjohnny 			size -= strlen(curr) + 1;
116870025d76Sjohnny 			curr += strlen(curr) + 1;
116970025d76Sjohnny 
117070025d76Sjohnny 			compat[i++] = curr;	/* form 1 */
117170025d76Sjohnny 			(void) snprintf(curr, size, "pciex%x,%x.%x.%x",
117270025d76Sjohnny 			    vendorid, deviceid, subvenid, subdevid);
117370025d76Sjohnny 			size -= strlen(curr) + 1;
117470025d76Sjohnny 			curr += strlen(curr) + 1;
117570025d76Sjohnny 
117670025d76Sjohnny 		}
117770025d76Sjohnny 		compat[i++] = curr;	/* form 3 */
117870025d76Sjohnny 		(void) snprintf(curr, size, "pciex%x,%x.%x",
117970025d76Sjohnny 		    vendorid, deviceid, revid);
118070025d76Sjohnny 		size -= strlen(curr) + 1;
118170025d76Sjohnny 		curr += strlen(curr) + 1;
118270025d76Sjohnny 
118370025d76Sjohnny 		compat[i++] = curr;	/* form 4 */
118470025d76Sjohnny 		(void) snprintf(curr, size, "pciex%x,%x", vendorid, deviceid);
118570025d76Sjohnny 		size -= strlen(curr) + 1;
118670025d76Sjohnny 		curr += strlen(curr) + 1;
118770025d76Sjohnny 
118870025d76Sjohnny 		compat[i++] = curr;	/* form 5 */
118970025d76Sjohnny 		(void) snprintf(curr, size, "pciexclass,%06x", classcode);
119070025d76Sjohnny 		size -= strlen(curr) + 1;
119170025d76Sjohnny 		curr += strlen(curr) + 1;
119270025d76Sjohnny 
119370025d76Sjohnny 		compat[i++] = curr;	/* form 6 */
119470025d76Sjohnny 		(void) snprintf(curr, size, "pciexclass,%04x",
119570025d76Sjohnny 		    (classcode >> 8));
119670025d76Sjohnny 		size -= strlen(curr) + 1;
119770025d76Sjohnny 		curr += strlen(curr) + 1;
119870025d76Sjohnny 	}
119970025d76Sjohnny 
12007c478bd9Sstevel@tonic-gate 	if (subvenid) {
12017c478bd9Sstevel@tonic-gate 		compat[i++] = curr;	/* form 0 */
12027c478bd9Sstevel@tonic-gate 		(void) snprintf(curr, size, "pci%x,%x.%x.%x.%x",
12037c478bd9Sstevel@tonic-gate 		    vendorid, deviceid, subvenid, subdevid, revid);
12047c478bd9Sstevel@tonic-gate 		size -= strlen(curr) + 1;
12057c478bd9Sstevel@tonic-gate 		curr += strlen(curr) + 1;
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 		compat[i++] = curr;	/* form 1 */
12087c478bd9Sstevel@tonic-gate 		(void) snprintf(curr, size, "pci%x,%x.%x.%x",
12097c478bd9Sstevel@tonic-gate 		    vendorid, deviceid, subvenid, subdevid);
12107c478bd9Sstevel@tonic-gate 		size -= strlen(curr) + 1;
12117c478bd9Sstevel@tonic-gate 		curr += strlen(curr) + 1;
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 		compat[i++] = curr;	/* form 2 */
121470025d76Sjohnny 		(void) snprintf(curr, size, "pci%x,%x", subvenid, subdevid);
12157c478bd9Sstevel@tonic-gate 		size -= strlen(curr) + 1;
12167c478bd9Sstevel@tonic-gate 		curr += strlen(curr) + 1;
12177c478bd9Sstevel@tonic-gate 	}
12187c478bd9Sstevel@tonic-gate 	compat[i++] = curr;	/* form 3 */
12197c478bd9Sstevel@tonic-gate 	(void) snprintf(curr, size, "pci%x,%x.%x", vendorid, deviceid, revid);
12207c478bd9Sstevel@tonic-gate 	size -= strlen(curr) + 1;
12217c478bd9Sstevel@tonic-gate 	curr += strlen(curr) + 1;
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	compat[i++] = curr;	/* form 4 */
12247c478bd9Sstevel@tonic-gate 	(void) snprintf(curr, size, "pci%x,%x", vendorid, deviceid);
12257c478bd9Sstevel@tonic-gate 	size -= strlen(curr) + 1;
12267c478bd9Sstevel@tonic-gate 	curr += strlen(curr) + 1;
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 	compat[i++] = curr;	/* form 5 */
12297c478bd9Sstevel@tonic-gate 	(void) snprintf(curr, size, "pciclass,%06x", classcode);
12307c478bd9Sstevel@tonic-gate 	size -= strlen(curr) + 1;
12317c478bd9Sstevel@tonic-gate 	curr += strlen(curr) + 1;
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 	compat[i++] = curr;	/* form 6 */
12347c478bd9Sstevel@tonic-gate 	(void) snprintf(curr, size, "pciclass,%04x", (classcode >> 8));
123570025d76Sjohnny 	size -= strlen(curr) + 1;
123670025d76Sjohnny 	curr += strlen(curr) + 1;
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_string_array(DDI_DEV_T_NONE, dip,
12397c478bd9Sstevel@tonic-gate 	    "compatible", compat, i);
12407c478bd9Sstevel@tonic-gate 	kmem_free(buf, COMPAT_BUFSIZE);
12417c478bd9Sstevel@tonic-gate }
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate /*
12447c478bd9Sstevel@tonic-gate  * Adjust the reg properties for a dual channel PCI-IDE device.
12457c478bd9Sstevel@tonic-gate  *
12467c478bd9Sstevel@tonic-gate  * NOTE: don't do anything that changes the order of the hard-decodes
12477c478bd9Sstevel@tonic-gate  * and programmed BARs. The kernel driver depends on these values
12487c478bd9Sstevel@tonic-gate  * being in this order regardless of whether they're for a 'native'
12497c478bd9Sstevel@tonic-gate  * mode BAR or not.
12507c478bd9Sstevel@tonic-gate  */
12517c478bd9Sstevel@tonic-gate /*
12527c478bd9Sstevel@tonic-gate  * config info for pci-ide devices
12537c478bd9Sstevel@tonic-gate  */
12547c478bd9Sstevel@tonic-gate static struct {
12557c478bd9Sstevel@tonic-gate 	uchar_t  native_mask;	/* 0 == 'compatibility' mode, 1 == native */
12567c478bd9Sstevel@tonic-gate 	uchar_t  bar_offset;	/* offset for alt status register */
12577c478bd9Sstevel@tonic-gate 	ushort_t addr;		/* compatibility mode base address */
12587c478bd9Sstevel@tonic-gate 	ushort_t length;	/* number of ports for this BAR */
12597c478bd9Sstevel@tonic-gate } pciide_bar[] = {
12607c478bd9Sstevel@tonic-gate 	{ 0x01, 0, 0x1f0, 8 },	/* primary lower BAR */
12617c478bd9Sstevel@tonic-gate 	{ 0x01, 2, 0x3f6, 1 },	/* primary upper BAR */
12627c478bd9Sstevel@tonic-gate 	{ 0x04, 0, 0x170, 8 },	/* secondary lower BAR */
12637c478bd9Sstevel@tonic-gate 	{ 0x04, 2, 0x376, 1 }	/* secondary upper BAR */
12647c478bd9Sstevel@tonic-gate };
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate static int
12677c478bd9Sstevel@tonic-gate pciIdeAdjustBAR(uchar_t progcl, int index, uint_t *basep, uint_t *lenp)
12687c478bd9Sstevel@tonic-gate {
12697c478bd9Sstevel@tonic-gate 	int hard_decode = 0;
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 	/*
12727c478bd9Sstevel@tonic-gate 	 * Adjust the base and len for the BARs of the PCI-IDE
12737c478bd9Sstevel@tonic-gate 	 * device's primary and secondary controllers. The first
12747c478bd9Sstevel@tonic-gate 	 * two BARs are for the primary controller and the next
12757c478bd9Sstevel@tonic-gate 	 * two BARs are for the secondary controller. The fifth
12767c478bd9Sstevel@tonic-gate 	 * and sixth bars are never adjusted.
12777c478bd9Sstevel@tonic-gate 	 */
12787c478bd9Sstevel@tonic-gate 	if (index >= 0 && index <= 3) {
12797c478bd9Sstevel@tonic-gate 		*lenp = pciide_bar[index].length;
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 		if (progcl & pciide_bar[index].native_mask) {
12827c478bd9Sstevel@tonic-gate 			*basep += pciide_bar[index].bar_offset;
12837c478bd9Sstevel@tonic-gate 		} else {
12847c478bd9Sstevel@tonic-gate 			*basep = pciide_bar[index].addr;
12857c478bd9Sstevel@tonic-gate 			hard_decode = 1;
12867c478bd9Sstevel@tonic-gate 		}
12877c478bd9Sstevel@tonic-gate 	}
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 	/*
12907c478bd9Sstevel@tonic-gate 	 * if either base or len is zero make certain both are zero
12917c478bd9Sstevel@tonic-gate 	 */
12927c478bd9Sstevel@tonic-gate 	if (*basep == 0 || *lenp == 0) {
12937c478bd9Sstevel@tonic-gate 		*basep = 0;
12947c478bd9Sstevel@tonic-gate 		*lenp = 0;
12957c478bd9Sstevel@tonic-gate 		hard_decode = 0;
12967c478bd9Sstevel@tonic-gate 	}
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate 	return (hard_decode);
12997c478bd9Sstevel@tonic-gate }
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate /*
13037c478bd9Sstevel@tonic-gate  * Add the "reg" and "assigned-addresses" property
13047c478bd9Sstevel@tonic-gate  */
13057c478bd9Sstevel@tonic-gate static int
13067c478bd9Sstevel@tonic-gate add_reg_props(dev_info_t *dip, uchar_t bus, uchar_t dev, uchar_t func,
13077c478bd9Sstevel@tonic-gate     int config_op, int pciide)
13087c478bd9Sstevel@tonic-gate {
13097c478bd9Sstevel@tonic-gate 	uchar_t baseclass, subclass, progclass, header;
13107c478bd9Sstevel@tonic-gate 	ushort_t bar_sz;
13117c478bd9Sstevel@tonic-gate 	uint_t value = 0, len, devloc;
13127c478bd9Sstevel@tonic-gate 	uint_t base, base_hi, type;
13137c478bd9Sstevel@tonic-gate 	ushort_t offset, end;
13147c478bd9Sstevel@tonic-gate 	int max_basereg, j, reprogram = 0;
13157c478bd9Sstevel@tonic-gate 	uint_t phys_hi;
13167c478bd9Sstevel@tonic-gate 	struct memlist **io_res, **mres, **mem_res, **pmem_res;
131746e9e839Smyers 	uint16_t cmd_reg;
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate 	pci_regspec_t regs[16] = {{0}};
13207c478bd9Sstevel@tonic-gate 	pci_regspec_t assigned[15] = {{0}};
1321ebf3afa8Sdmick 	int nreg, nasgn, enable = 0;
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate 	io_res = &pci_bus_res[bus].io_ports;
13247c478bd9Sstevel@tonic-gate 	mem_res = &pci_bus_res[bus].mem_space;
13257c478bd9Sstevel@tonic-gate 	if (bus == 0)	/* for bus 0, there is only mem_space */
13267c478bd9Sstevel@tonic-gate 		pmem_res = mem_res;
13277c478bd9Sstevel@tonic-gate 	else
13287c478bd9Sstevel@tonic-gate 		pmem_res = &pci_bus_res[bus].pmem_space;
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 	devloc = (uint_t)bus << 16 | (uint_t)dev << 11 | (uint_t)func << 8;
13317c478bd9Sstevel@tonic-gate 	regs[0].pci_phys_hi = devloc;
13327c478bd9Sstevel@tonic-gate 	nreg = 1;	/* rest of regs[0] is all zero */
13337c478bd9Sstevel@tonic-gate 	nasgn = 0;
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate 	baseclass = pci_getb(bus, dev, func, PCI_CONF_BASCLASS);
13367c478bd9Sstevel@tonic-gate 	subclass = pci_getb(bus, dev, func, PCI_CONF_SUBCLASS);
13377c478bd9Sstevel@tonic-gate 	progclass = pci_getb(bus, dev, func, PCI_CONF_PROGCLASS);
13387c478bd9Sstevel@tonic-gate 	header = pci_getb(bus, dev, func, PCI_CONF_HEADER) & PCI_HEADER_TYPE_M;
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate 	switch (header) {
13417c478bd9Sstevel@tonic-gate 	case PCI_HEADER_ZERO:
13427c478bd9Sstevel@tonic-gate 		max_basereg = PCI_BASE_NUM;
13437c478bd9Sstevel@tonic-gate 		break;
13447c478bd9Sstevel@tonic-gate 	case PCI_HEADER_PPB:
13457c478bd9Sstevel@tonic-gate 		max_basereg = PCI_BCNF_BASE_NUM;
13467c478bd9Sstevel@tonic-gate 		break;
13477c478bd9Sstevel@tonic-gate 	case PCI_HEADER_CARDBUS:
13487c478bd9Sstevel@tonic-gate 		max_basereg = PCI_CBUS_BASE_NUM;
13497c478bd9Sstevel@tonic-gate 		break;
13507c478bd9Sstevel@tonic-gate 	default:
13517c478bd9Sstevel@tonic-gate 		max_basereg = 0;
13527c478bd9Sstevel@tonic-gate 		break;
13537c478bd9Sstevel@tonic-gate 	}
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 	/*
13567c478bd9Sstevel@tonic-gate 	 * Create the register property by saving the current
13578d34f104Smyers 	 * value of the base register. Write 0xffffffff to the
13588d34f104Smyers 	 * base register.  Read the value back to determine the
13598d34f104Smyers 	 * required size of the address space.  Restore the base
13608d34f104Smyers 	 * register contents.
13618d34f104Smyers 	 *
13628d34f104Smyers 	 * Do not disable I/O and memory access; this isn't necessary
13638d34f104Smyers 	 * since no driver is yet attached to this device, and disabling
13648d34f104Smyers 	 * I/O and memory access has the side-effect of disabling PCI-PCI
13658d34f104Smyers 	 * bridge mappings, which makes the bridge transparent to secondary-
13668d34f104Smyers 	 * bus activity (see sections 4.1-4.3 of the PCI-PCI Bridge
13678d34f104Smyers 	 * Spec V1.2).
13687c478bd9Sstevel@tonic-gate 	 */
13697c478bd9Sstevel@tonic-gate 	end = PCI_CONF_BASE0 + max_basereg * sizeof (uint_t);
13707c478bd9Sstevel@tonic-gate 	for (j = 0, offset = PCI_CONF_BASE0; offset < end;
13717c478bd9Sstevel@tonic-gate 	    j++, offset += bar_sz) {
13727c478bd9Sstevel@tonic-gate 		int hard_decode = 0;
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 		/* determine the size of the address space */
13757c478bd9Sstevel@tonic-gate 		base = pci_getl(bus, dev, func, offset);
13767c478bd9Sstevel@tonic-gate 		pci_putl(bus, dev, func, offset, 0xffffffff);
13777c478bd9Sstevel@tonic-gate 		value = pci_getl(bus, dev, func, offset);
13787c478bd9Sstevel@tonic-gate 		pci_putl(bus, dev, func, offset, base);
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate 		/* construct phys hi,med.lo, size hi, lo */
13817c478bd9Sstevel@tonic-gate 		if ((pciide && j < 4) || (base & PCI_BASE_SPACE_IO)) {
13827c478bd9Sstevel@tonic-gate 			/* i/o space */
13837c478bd9Sstevel@tonic-gate 			bar_sz = PCI_BAR_SZ_32;
13847c478bd9Sstevel@tonic-gate 			value &= PCI_BASE_IO_ADDR_M;
13857c478bd9Sstevel@tonic-gate 			len = ((value ^ (value-1)) + 1) >> 1;
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate 			/* XXX Adjust first 4 IDE registers */
13887c478bd9Sstevel@tonic-gate 			if (pciide) {
1389f088817aSyt 				if (subclass != PCI_MASS_IDE)
13907c478bd9Sstevel@tonic-gate 					progclass = (PCI_IDE_IF_NATIVE_PRI |
13917c478bd9Sstevel@tonic-gate 					    PCI_IDE_IF_NATIVE_SEC);
13927c478bd9Sstevel@tonic-gate 				hard_decode = pciIdeAdjustBAR(progclass, j,
13937c478bd9Sstevel@tonic-gate 				    &base, &len);
13947c478bd9Sstevel@tonic-gate 			} else if (value == 0) {
13957c478bd9Sstevel@tonic-gate 				/* skip base regs with size of 0 */
13967c478bd9Sstevel@tonic-gate 				continue;
13977c478bd9Sstevel@tonic-gate 			}
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 			regs[nreg].pci_size_low =
14007c478bd9Sstevel@tonic-gate 			    assigned[nasgn].pci_size_low = len;
14017c478bd9Sstevel@tonic-gate 			if (!hard_decode) {
14027c478bd9Sstevel@tonic-gate 				regs[nreg].pci_phys_hi =
14037c478bd9Sstevel@tonic-gate 				    (PCI_ADDR_IO | devloc) + offset;
14047c478bd9Sstevel@tonic-gate 			} else {
14057c478bd9Sstevel@tonic-gate 				regs[nreg].pci_phys_hi =
14067c478bd9Sstevel@tonic-gate 				    (PCI_RELOCAT_B | PCI_ADDR_IO | devloc) +
14077c478bd9Sstevel@tonic-gate 				    offset;
14087c478bd9Sstevel@tonic-gate 				regs[nreg].pci_phys_low =
14097c478bd9Sstevel@tonic-gate 				    base & PCI_BASE_IO_ADDR_M;
14107c478bd9Sstevel@tonic-gate 			}
14117c478bd9Sstevel@tonic-gate 			assigned[nasgn].pci_phys_hi =
14127c478bd9Sstevel@tonic-gate 			    (PCI_RELOCAT_B | PCI_ADDR_IO | devloc) + offset;
14137c478bd9Sstevel@tonic-gate 			type = base & (~PCI_BASE_IO_ADDR_M);
14147c478bd9Sstevel@tonic-gate 			base &= PCI_BASE_IO_ADDR_M;
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 			/*
14177c478bd9Sstevel@tonic-gate 			 * first pass - gather what's there
14187c478bd9Sstevel@tonic-gate 			 * update/second pass - adjust/allocate regions
14197c478bd9Sstevel@tonic-gate 			 *	config - allocate regions
14207c478bd9Sstevel@tonic-gate 			 */
14217c478bd9Sstevel@tonic-gate 			if (config_op == CONFIG_INFO) {	/* first pass */
14227c478bd9Sstevel@tonic-gate 				/* take out of the resource map of the bus */
14237c478bd9Sstevel@tonic-gate 				if (*io_res && base != 0)
14247c478bd9Sstevel@tonic-gate 					(void) memlist_remove(io_res,
14257c478bd9Sstevel@tonic-gate 					    (uint64_t)base, (uint64_t)len);
14267c478bd9Sstevel@tonic-gate 				else if (*io_res)
14277c478bd9Sstevel@tonic-gate 					reprogram = 1;
14287c478bd9Sstevel@tonic-gate 			} else if (*io_res && base == 0) {
14297c478bd9Sstevel@tonic-gate 				base = (uint_t)memlist_find(io_res,
1430ebf3afa8Sdmick 				    (uint64_t)len, (uint64_t)0x4);
14317c478bd9Sstevel@tonic-gate 				if (base != 0) {
14327c478bd9Sstevel@tonic-gate 					/* XXX need to worry about 64-bit? */
14337c478bd9Sstevel@tonic-gate 					pci_putl(bus, dev, func, offset,
14347c478bd9Sstevel@tonic-gate 					    base | type);
14357c478bd9Sstevel@tonic-gate 					base = pci_getl(bus, dev, func, offset);
14367c478bd9Sstevel@tonic-gate 					base &= PCI_BASE_IO_ADDR_M;
14377c478bd9Sstevel@tonic-gate 				}
14387c478bd9Sstevel@tonic-gate 				if (base == 0) {
14397c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN, "failed to program"
1440db063408Sdmick 					    " IO space [%d/%d/%d] BAR@0x%x"
1441db063408Sdmick 					    " length 0x%x",
1442ebf3afa8Sdmick 					    bus, dev, func, offset, len);
14437c478bd9Sstevel@tonic-gate 				} else
144446e9e839Smyers 					enable |= PCI_COMM_IO;
14457c478bd9Sstevel@tonic-gate 			}
14467c478bd9Sstevel@tonic-gate 			assigned[nasgn].pci_phys_low = base;
14477c478bd9Sstevel@tonic-gate 			nreg++, nasgn++;
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 		} else {
14507c478bd9Sstevel@tonic-gate 			/* memory space */
14517c478bd9Sstevel@tonic-gate 			if ((base & PCI_BASE_TYPE_M) == PCI_BASE_TYPE_ALL) {
14527c478bd9Sstevel@tonic-gate 				bar_sz = PCI_BAR_SZ_64;
14537c478bd9Sstevel@tonic-gate 				base_hi = pci_getl(bus, dev, func, offset + 4);
14547c478bd9Sstevel@tonic-gate 				phys_hi = PCI_ADDR_MEM64;
14557c478bd9Sstevel@tonic-gate 			} else {
14567c478bd9Sstevel@tonic-gate 				bar_sz = PCI_BAR_SZ_32;
14577c478bd9Sstevel@tonic-gate 				base_hi = 0;
14587c478bd9Sstevel@tonic-gate 				phys_hi = PCI_ADDR_MEM32;
14597c478bd9Sstevel@tonic-gate 			}
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 			/* skip base regs with size of 0 */
14627c478bd9Sstevel@tonic-gate 			value &= PCI_BASE_M_ADDR_M;
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate 			if (value == 0) {
14657c478bd9Sstevel@tonic-gate 				continue;
14667c478bd9Sstevel@tonic-gate 			}
14677c478bd9Sstevel@tonic-gate 			len = ((value ^ (value-1)) + 1) >> 1;
14687c478bd9Sstevel@tonic-gate 			regs[nreg].pci_size_low =
14697c478bd9Sstevel@tonic-gate 			    assigned[nasgn].pci_size_low = len;
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate 			phys_hi |= (devloc | offset);
14727c478bd9Sstevel@tonic-gate 			if (base & PCI_BASE_PREF_M) {
14737c478bd9Sstevel@tonic-gate 				mres = pmem_res;
14747c478bd9Sstevel@tonic-gate 				phys_hi |= PCI_PREFETCH_B;
14757c478bd9Sstevel@tonic-gate 			} else {
14767c478bd9Sstevel@tonic-gate 				mres = mem_res;
14777c478bd9Sstevel@tonic-gate 			}
14787c478bd9Sstevel@tonic-gate 			regs[nreg].pci_phys_hi =
14797c478bd9Sstevel@tonic-gate 			    assigned[nasgn].pci_phys_hi = phys_hi;
14807c478bd9Sstevel@tonic-gate 			assigned[nasgn].pci_phys_hi |= PCI_RELOCAT_B;
14817c478bd9Sstevel@tonic-gate 			assigned[nasgn].pci_phys_mid = base_hi;
14827c478bd9Sstevel@tonic-gate 			type = base & ~PCI_BASE_M_ADDR_M;
14837c478bd9Sstevel@tonic-gate 			base &= PCI_BASE_M_ADDR_M;
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate 			if (config_op == CONFIG_INFO) {
14867c478bd9Sstevel@tonic-gate 				/* take out of the resource map of the bus */
14877c478bd9Sstevel@tonic-gate 				if (*mres && base != 0) {
14887c478bd9Sstevel@tonic-gate 					(void) memlist_remove(mres,
14897c478bd9Sstevel@tonic-gate 					    (uint64_t)base, (uint64_t)len);
14907c478bd9Sstevel@tonic-gate 				} else if (*mres)
14917c478bd9Sstevel@tonic-gate 					reprogram = 1;
14927c478bd9Sstevel@tonic-gate 			} else if (*mres && base == 0) {
14937c478bd9Sstevel@tonic-gate 				base = (uint_t)memlist_find(mres,
14947c478bd9Sstevel@tonic-gate 				    (uint64_t)len, (uint64_t)0x1000);
14957c478bd9Sstevel@tonic-gate 				if (base != NULL) {
14967c478bd9Sstevel@tonic-gate 					pci_putl(bus, dev, func, offset,
14977c478bd9Sstevel@tonic-gate 					    base | type);
14987c478bd9Sstevel@tonic-gate 					base = pci_getl(bus, dev, func, offset);
14997c478bd9Sstevel@tonic-gate 					base &= PCI_BASE_M_ADDR_M;
15007c478bd9Sstevel@tonic-gate 				}
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 				if (base == 0) {
15037c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN, "failed to program "
1504ebf3afa8Sdmick 					    "mem space [%d/%d/%d] BAR@0x%x"
1505db063408Sdmick 					    " length 0x%x",
1506ebf3afa8Sdmick 					    bus, dev, func, offset, len);
15077c478bd9Sstevel@tonic-gate 				} else
150846e9e839Smyers 					enable |= PCI_COMM_MAE;
15097c478bd9Sstevel@tonic-gate 			}
15107c478bd9Sstevel@tonic-gate 			assigned[nasgn].pci_phys_low = base;
15117c478bd9Sstevel@tonic-gate 			nreg++, nasgn++;
15127c478bd9Sstevel@tonic-gate 		}
15137c478bd9Sstevel@tonic-gate 	}
15147c478bd9Sstevel@tonic-gate 	switch (header) {
15157c478bd9Sstevel@tonic-gate 	case PCI_HEADER_ZERO:
15167c478bd9Sstevel@tonic-gate 		offset = PCI_CONF_ROM;
15177c478bd9Sstevel@tonic-gate 		break;
15187c478bd9Sstevel@tonic-gate 	case PCI_HEADER_PPB:
15197c478bd9Sstevel@tonic-gate 		offset = PCI_BCNF_ROM;
15207c478bd9Sstevel@tonic-gate 		break;
15217c478bd9Sstevel@tonic-gate 	default: /* including PCI_HEADER_CARDBUS */
15227c478bd9Sstevel@tonic-gate 		goto done;
15237c478bd9Sstevel@tonic-gate 	}
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 	/*
15267c478bd9Sstevel@tonic-gate 	 * Add the expansion rom memory space
15277c478bd9Sstevel@tonic-gate 	 * Determine the size of the ROM base reg; don't write reserved bits
15287c478bd9Sstevel@tonic-gate 	 * ROM isn't in the PCI memory space.
15297c478bd9Sstevel@tonic-gate 	 */
15307c478bd9Sstevel@tonic-gate 	base = pci_getl(bus, dev, func, offset);
15317c478bd9Sstevel@tonic-gate 	pci_putl(bus, dev, func, offset, PCI_BASE_ROM_ADDR_M);
15327c478bd9Sstevel@tonic-gate 	value = pci_getl(bus, dev, func, offset);
15337c478bd9Sstevel@tonic-gate 	pci_putl(bus, dev, func, offset, base);
153470025d76Sjohnny 	if (value & PCI_BASE_ROM_ENABLE)
153570025d76Sjohnny 		value &= PCI_BASE_ROM_ADDR_M;
153670025d76Sjohnny 	else
153770025d76Sjohnny 		value = 0;
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate 	if (value != 0) {
15407c478bd9Sstevel@tonic-gate 		regs[nreg].pci_phys_hi = (PCI_ADDR_MEM32 | devloc) + offset;
15417c478bd9Sstevel@tonic-gate 		assigned[nasgn].pci_phys_hi = (PCI_RELOCAT_B |
15427c478bd9Sstevel@tonic-gate 		    PCI_ADDR_MEM32 | devloc) + offset;
15437c478bd9Sstevel@tonic-gate 		base &= PCI_BASE_ROM_ADDR_M;
15447c478bd9Sstevel@tonic-gate 		assigned[nasgn].pci_phys_low = base;
15457c478bd9Sstevel@tonic-gate 		len = ((value ^ (value-1)) + 1) >> 1;
15467c478bd9Sstevel@tonic-gate 		regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = len;
15477c478bd9Sstevel@tonic-gate 		nreg++, nasgn++;
154899ed6083Sszhou 		/* take it out of the memory resource */
154999ed6083Sszhou 		if (*mem_res && base != 0)
155099ed6083Sszhou 			(void) memlist_remove(mem_res,
155199ed6083Sszhou 			    (uint64_t)base, (uint64_t)len);
15527c478bd9Sstevel@tonic-gate 	}
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate 	/*
15557c478bd9Sstevel@tonic-gate 	 * The following are ISA resources. There are not part
15567c478bd9Sstevel@tonic-gate 	 * of the PCI local bus resources. So don't attempt to
15577c478bd9Sstevel@tonic-gate 	 * do resource accounting against PCI.
15587c478bd9Sstevel@tonic-gate 	 */
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate 	/* add the three hard-decode, aliased address spaces for VGA */
15617c478bd9Sstevel@tonic-gate 	if ((baseclass == PCI_CLASS_DISPLAY && subclass == PCI_DISPLAY_VGA) ||
15627c478bd9Sstevel@tonic-gate 	    (baseclass == PCI_CLASS_NONE && subclass == PCI_NONE_VGA)) {
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 		/* VGA hard decode 0x3b0-0x3bb */
15657c478bd9Sstevel@tonic-gate 		regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi =
15667c478bd9Sstevel@tonic-gate 		    (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc);
15677c478bd9Sstevel@tonic-gate 		regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x3b0;
15687c478bd9Sstevel@tonic-gate 		regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0xc;
15697c478bd9Sstevel@tonic-gate 		nreg++, nasgn++;
15707c478bd9Sstevel@tonic-gate 
15717c478bd9Sstevel@tonic-gate 		/* VGA hard decode 0x3c0-0x3df */
15727c478bd9Sstevel@tonic-gate 		regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi =
15737c478bd9Sstevel@tonic-gate 		    (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc);
15747c478bd9Sstevel@tonic-gate 		regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x3c0;
15757c478bd9Sstevel@tonic-gate 		regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0x20;
15767c478bd9Sstevel@tonic-gate 		nreg++, nasgn++;
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 		/* Video memory */
15797c478bd9Sstevel@tonic-gate 		regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi =
15807c478bd9Sstevel@tonic-gate 		    (PCI_RELOCAT_B | PCI_ADDR_MEM32 | devloc);
15817c478bd9Sstevel@tonic-gate 		regs[nreg].pci_phys_low =
15827c478bd9Sstevel@tonic-gate 		    assigned[nasgn].pci_phys_low = 0xa0000;
15837c478bd9Sstevel@tonic-gate 		regs[nreg].pci_size_low =
15847c478bd9Sstevel@tonic-gate 		    assigned[nasgn].pci_size_low = 0x20000;
15857c478bd9Sstevel@tonic-gate 		nreg++, nasgn++;
15867c478bd9Sstevel@tonic-gate 	}
15877c478bd9Sstevel@tonic-gate 
15887c478bd9Sstevel@tonic-gate 	/* add the hard-decode, aliased address spaces for 8514 */
15897c478bd9Sstevel@tonic-gate 	if ((baseclass == PCI_CLASS_DISPLAY) &&
15909896aa55Sjveta 	    (subclass == PCI_DISPLAY_VGA) &&
15919896aa55Sjveta 	    (progclass & PCI_DISPLAY_IF_8514)) {
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate 		/* hard decode 0x2e8 */
15947c478bd9Sstevel@tonic-gate 		regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi =
15957c478bd9Sstevel@tonic-gate 		    (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc);
15967c478bd9Sstevel@tonic-gate 		regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x2e8;
15977c478bd9Sstevel@tonic-gate 		regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0x1;
15987c478bd9Sstevel@tonic-gate 		nreg++, nasgn++;
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate 		/* hard decode 0x2ea-0x2ef */
16017c478bd9Sstevel@tonic-gate 		regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi =
16027c478bd9Sstevel@tonic-gate 		    (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc);
16037c478bd9Sstevel@tonic-gate 		regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x2ea;
16047c478bd9Sstevel@tonic-gate 		regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0x6;
16057c478bd9Sstevel@tonic-gate 		nreg++, nasgn++;
16067c478bd9Sstevel@tonic-gate 	}
16077c478bd9Sstevel@tonic-gate 
16087c478bd9Sstevel@tonic-gate done:
16097c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "reg",
16107c478bd9Sstevel@tonic-gate 	    (int *)regs, nreg * sizeof (pci_regspec_t) / sizeof (int));
16117c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
16127c478bd9Sstevel@tonic-gate 	    "assigned-addresses",
16137c478bd9Sstevel@tonic-gate 	    (int *)assigned, nasgn * sizeof (pci_regspec_t) / sizeof (int));
16147c478bd9Sstevel@tonic-gate 	if (config_op == CONFIG_NEW && enable) {
16157c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE,
16167c478bd9Sstevel@tonic-gate 		    "!enable PCI device [%d/%d/%d]", bus, dev, func);
16178d34f104Smyers 		cmd_reg = pci_getw(bus, dev, func, PCI_CONF_COMM);
161846e9e839Smyers 		cmd_reg |= (enable | PCI_COMM_ME);
16198d34f104Smyers 		pci_putw(bus, dev, func, PCI_CONF_COMM, cmd_reg);
16207c478bd9Sstevel@tonic-gate 	}
16217c478bd9Sstevel@tonic-gate 	return (reprogram);
16227c478bd9Sstevel@tonic-gate }
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate static void
162570025d76Sjohnny add_ppb_props(dev_info_t *dip, uchar_t bus, uchar_t dev, uchar_t func,
162670025d76Sjohnny     int pciex)
16277c478bd9Sstevel@tonic-gate {
162870025d76Sjohnny 	char *dev_type;
16297c478bd9Sstevel@tonic-gate 	int i;
16307c478bd9Sstevel@tonic-gate 	uint_t val, io_range[2], mem_range[2], pmem_range[2];
16317c478bd9Sstevel@tonic-gate 	uchar_t secbus = pci_getb(bus, dev, func, PCI_BCNF_SECBUS);
16327c478bd9Sstevel@tonic-gate 	uchar_t subbus = pci_getb(bus, dev, func, PCI_BCNF_SUBBUS);
1633f55ce205Sszhou 	ASSERT(secbus <= subbus);
16347c478bd9Sstevel@tonic-gate 
1635f55ce205Sszhou 	/*
1636f55ce205Sszhou 	 * Some BIOSes lie about max pci busses, we allow for
1637f55ce205Sszhou 	 * such mistakes here
1638f55ce205Sszhou 	 */
1639f55ce205Sszhou 	if (subbus > pci_bios_nbus) {
1640f55ce205Sszhou 		pci_bios_nbus = subbus;
1641f55ce205Sszhou 		alloc_res_array();
1642f55ce205Sszhou 	}
1643f55ce205Sszhou 
1644f55ce205Sszhou 	ASSERT(pci_bus_res[secbus].dip == NULL);
16457c478bd9Sstevel@tonic-gate 	pci_bus_res[secbus].dip = dip;
16467c478bd9Sstevel@tonic-gate 	pci_bus_res[secbus].par_bus = bus;
16477c478bd9Sstevel@tonic-gate 
164870025d76Sjohnny 	dev_type = pciex ? "pciex" : "pci";
164970025d76Sjohnny 
16507c478bd9Sstevel@tonic-gate 	/* setup bus number hierarchy */
16517c478bd9Sstevel@tonic-gate 	pci_bus_res[secbus].sub_bus = subbus;
165253273e82Ssethg 	/*
165353273e82Ssethg 	 * Keep track of the largest subordinate bus number (this is essential
165453273e82Ssethg 	 * for peer busses because there is no other way of determining its
165553273e82Ssethg 	 * subordinate bus number).
165653273e82Ssethg 	 */
16577c478bd9Sstevel@tonic-gate 	if (subbus > pci_bus_res[bus].sub_bus)
16587c478bd9Sstevel@tonic-gate 		pci_bus_res[bus].sub_bus = subbus;
165953273e82Ssethg 	/*
166053273e82Ssethg 	 * Loop through subordinate busses, initializing their parent bus
166153273e82Ssethg 	 * field to this bridge's parent.  The subordinate busses' parent
166253273e82Ssethg 	 * fields may very well be further refined later, as child bridges
166353273e82Ssethg 	 * are enumerated.  (The value is to note that the subordinate busses
166453273e82Ssethg 	 * are not peer busses by changing their par_bus fields to anything
166553273e82Ssethg 	 * other than -1.)
166653273e82Ssethg 	 */
16677c478bd9Sstevel@tonic-gate 	for (i = secbus + 1; i <= subbus; i++)
16687c478bd9Sstevel@tonic-gate 		pci_bus_res[i].par_bus = bus;
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
167170025d76Sjohnny 	    "device_type", dev_type);
16727c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
16737c478bd9Sstevel@tonic-gate 	    "#address-cells", 3);
16747c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
16757c478bd9Sstevel@tonic-gate 	    "#size-cells", 2);
16767c478bd9Sstevel@tonic-gate 
16777c478bd9Sstevel@tonic-gate 	/*
16787c478bd9Sstevel@tonic-gate 	 * According to PPB spec, the base register should be programmed
16797c478bd9Sstevel@tonic-gate 	 * with a value bigger than the limit register when there are
16807c478bd9Sstevel@tonic-gate 	 * no resources available. This applies to io, memory, and
16817c478bd9Sstevel@tonic-gate 	 * prefetchable memory.
16827c478bd9Sstevel@tonic-gate 	 */
16839896aa55Sjveta 
16849896aa55Sjveta 	/*
16859896aa55Sjveta 	 * io range
16869896aa55Sjveta 	 * We determine i/o windows that are left unconfigured by bios
16879896aa55Sjveta 	 * through its i/o enable bit as Microsoft recommends OEMs to do.
16889896aa55Sjveta 	 * If it is unset, we disable i/o and mark it for reconfiguration in
16899896aa55Sjveta 	 * later passes by setting the base > limit
16909896aa55Sjveta 	 */
16919896aa55Sjveta 	val = (uint_t)pci_getw(bus, dev, func, PCI_CONF_COMM);
16929896aa55Sjveta 	if (val & PCI_COMM_IO) {
16939896aa55Sjveta 		val = (uint_t)pci_getb(bus, dev, func, PCI_BCNF_IO_BASE_LOW);
16949896aa55Sjveta 		io_range[0] = ((val & 0xf0) << 8);
16959896aa55Sjveta 		val = (uint_t)pci_getb(bus, dev, func, PCI_BCNF_IO_LIMIT_LOW);
16969896aa55Sjveta 		io_range[1]  = ((val & 0xf0) << 8) | 0xFFF;
16979896aa55Sjveta 	} else {
16989896aa55Sjveta 		io_range[0] = 0x9fff;
16999896aa55Sjveta 		io_range[1] = 0x1000;
17009896aa55Sjveta 		pci_putb(bus, dev, func, PCI_BCNF_IO_BASE_LOW,
17019896aa55Sjveta 		    (uint8_t)((io_range[0] >> 8) & 0xf0));
17029896aa55Sjveta 		pci_putb(bus, dev, func, PCI_BCNF_IO_LIMIT_LOW,
17039896aa55Sjveta 		    (uint8_t)((io_range[1] >> 8) & 0xf0));
17049896aa55Sjveta 		pci_putw(bus, dev, func, PCI_BCNF_IO_BASE_HI, 0);
17059896aa55Sjveta 		pci_putw(bus, dev, func, PCI_BCNF_IO_LIMIT_HI, 0);
17069896aa55Sjveta 	}
17079896aa55Sjveta 
17087c478bd9Sstevel@tonic-gate 	if (io_range[0] != 0 && io_range[0] < io_range[1]) {
17097c478bd9Sstevel@tonic-gate 		memlist_insert(&pci_bus_res[secbus].io_ports,
17107c478bd9Sstevel@tonic-gate 		    (uint64_t)io_range[0],
17117c478bd9Sstevel@tonic-gate 		    (uint64_t)(io_range[1] - io_range[0] + 1));
17127c478bd9Sstevel@tonic-gate 		if (pci_bus_res[bus].io_ports != NULL) {
17137c478bd9Sstevel@tonic-gate 			(void) memlist_remove(&pci_bus_res[bus].io_ports,
17147c478bd9Sstevel@tonic-gate 			    (uint64_t)io_range[0],
17157c478bd9Sstevel@tonic-gate 			    (uint64_t)(io_range[1] - io_range[0] + 1));
17167c478bd9Sstevel@tonic-gate 		}
17177c478bd9Sstevel@tonic-gate 		dcmn_err(CE_NOTE, "bus %d io-range: 0x%x-%x",
17187c478bd9Sstevel@tonic-gate 		    secbus, io_range[0], io_range[1]);
17192269adc8Sszhou 		/* if 32-bit supported, make sure upper bits are not set */
17202269adc8Sszhou 		if ((val & 0xf) == 1 &&
17212269adc8Sszhou 		    pci_getw(bus, dev, func, PCI_BCNF_IO_BASE_HI)) {
17222269adc8Sszhou 			cmn_err(CE_NOTE, "unsupported 32-bit IO address on"
17232269adc8Sszhou 			    " pci-pci bridge [%d/%d/%d]", bus, dev, func);
17242269adc8Sszhou 		}
17257c478bd9Sstevel@tonic-gate 	}
17267c478bd9Sstevel@tonic-gate 
17277c478bd9Sstevel@tonic-gate 	/* mem range */
17287c478bd9Sstevel@tonic-gate 	val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_MEM_BASE);
17297c478bd9Sstevel@tonic-gate 	mem_range[0] = ((val & 0xFFF0) << 16);
17307c478bd9Sstevel@tonic-gate 	val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_MEM_LIMIT);
17317c478bd9Sstevel@tonic-gate 	mem_range[1] = ((val & 0xFFF0) << 16) | 0xFFFFF;
17327c478bd9Sstevel@tonic-gate 	if (mem_range[0] != 0 && mem_range[0] < mem_range[1]) {
17337c478bd9Sstevel@tonic-gate 		memlist_insert(&pci_bus_res[secbus].mem_space,
17347c478bd9Sstevel@tonic-gate 		    (uint64_t)mem_range[0],
17357c478bd9Sstevel@tonic-gate 		    (uint64_t)(mem_range[1] - mem_range[0] + 1));
17367c478bd9Sstevel@tonic-gate 		/* remove from parent resouce list */
17377c478bd9Sstevel@tonic-gate 		if (pci_bus_res[bus].mem_space != NULL) {
17387c478bd9Sstevel@tonic-gate 			(void) memlist_remove(&pci_bus_res[bus].mem_space,
17397c478bd9Sstevel@tonic-gate 			    (uint64_t)mem_range[0],
17407c478bd9Sstevel@tonic-gate 			    (uint64_t)(mem_range[1] - mem_range[0] + 1));
17417c478bd9Sstevel@tonic-gate 		}
17427c478bd9Sstevel@tonic-gate 		dcmn_err(CE_NOTE, "bus %d mem-range: 0x%x-%x",
17437c478bd9Sstevel@tonic-gate 		    secbus, mem_range[0], mem_range[1]);
17447c478bd9Sstevel@tonic-gate 	}
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 	/* prefetchable memory range */
17477c478bd9Sstevel@tonic-gate 	val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_PF_BASE_LOW);
17487c478bd9Sstevel@tonic-gate 	pmem_range[0] = ((val & 0xFFF0) << 16);
17497c478bd9Sstevel@tonic-gate 	val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_PF_LIMIT_LOW);
17507c478bd9Sstevel@tonic-gate 	pmem_range[1] = ((val & 0xFFF0) << 16) | 0xFFFFF;
17517c478bd9Sstevel@tonic-gate 	if (pmem_range[0] != 0 && pmem_range[0] < pmem_range[1]) {
17527c478bd9Sstevel@tonic-gate 		memlist_insert(&pci_bus_res[secbus].pmem_space,
17537c478bd9Sstevel@tonic-gate 		    (uint64_t)pmem_range[0],
17547c478bd9Sstevel@tonic-gate 		    (uint64_t)(pmem_range[1] - pmem_range[0] + 1));
17557c478bd9Sstevel@tonic-gate 		if (pci_bus_res[bus].pmem_space != NULL) {
17567c478bd9Sstevel@tonic-gate 			(void) memlist_remove(&pci_bus_res[bus].pmem_space,
17577c478bd9Sstevel@tonic-gate 			    (uint64_t)pmem_range[0],
17587c478bd9Sstevel@tonic-gate 			    (uint64_t)(pmem_range[1] - pmem_range[0] + 1));
17597c478bd9Sstevel@tonic-gate 		}
17607c478bd9Sstevel@tonic-gate 		dcmn_err(CE_NOTE, "bus %d pmem-range: 0x%x-%x",
17617c478bd9Sstevel@tonic-gate 		    secbus, pmem_range[0], pmem_range[1]);
17622269adc8Sszhou 		/* if 64-bit supported, make sure upper bits are not set */
17632269adc8Sszhou 		if ((val & 0xf) == 1 &&
17642269adc8Sszhou 		    pci_getl(bus, dev, func, PCI_BCNF_PF_BASE_HIGH)) {
17652269adc8Sszhou 			cmn_err(CE_NOTE, "unsupported 64-bit prefetch memory on"
17662269adc8Sszhou 			    " pci-pci bridge [%d/%d/%d]", bus, dev, func);
17672269adc8Sszhou 		}
17687c478bd9Sstevel@tonic-gate 	}
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate 	add_bus_range_prop(secbus);
17717c478bd9Sstevel@tonic-gate 	add_ppb_ranges_prop(secbus);
17727c478bd9Sstevel@tonic-gate }
17737c478bd9Sstevel@tonic-gate 
177409f67678Sanish extern const struct pci_class_strings_s class_pci[];
177509f67678Sanish extern int class_pci_items;
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate static void
17787c478bd9Sstevel@tonic-gate add_model_prop(dev_info_t *dip, uint_t classcode)
17797c478bd9Sstevel@tonic-gate {
17807c478bd9Sstevel@tonic-gate 	const char *desc;
17817c478bd9Sstevel@tonic-gate 	int i;
17827c478bd9Sstevel@tonic-gate 	uchar_t baseclass = classcode >> 16;
17837c478bd9Sstevel@tonic-gate 	uchar_t subclass = (classcode >> 8) & 0xff;
17847c478bd9Sstevel@tonic-gate 	uchar_t progclass = classcode & 0xff;
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate 	if ((baseclass == PCI_CLASS_MASS) && (subclass == PCI_MASS_IDE)) {
17877c478bd9Sstevel@tonic-gate 		desc = "IDE controller";
17887c478bd9Sstevel@tonic-gate 	} else {
17897c478bd9Sstevel@tonic-gate 		for (desc = 0, i = 0; i < class_pci_items; i++) {
17907c478bd9Sstevel@tonic-gate 			if ((baseclass == class_pci[i].base_class) &&
17917c478bd9Sstevel@tonic-gate 			    (subclass == class_pci[i].sub_class) &&
17927c478bd9Sstevel@tonic-gate 			    (progclass == class_pci[i].prog_class)) {
179309f67678Sanish 				desc = class_pci[i].actual_desc;
17947c478bd9Sstevel@tonic-gate 				break;
17957c478bd9Sstevel@tonic-gate 			}
17967c478bd9Sstevel@tonic-gate 		}
179709f67678Sanish 		if (i == class_pci_items)
17987c478bd9Sstevel@tonic-gate 			desc = "Unknown class of pci/pnpbios device";
17997c478bd9Sstevel@tonic-gate 	}
18007c478bd9Sstevel@tonic-gate 
18017c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, "model",
18027c478bd9Sstevel@tonic-gate 	    (char *)desc);
18037c478bd9Sstevel@tonic-gate }
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate static void
18067c478bd9Sstevel@tonic-gate add_bus_range_prop(int bus)
18077c478bd9Sstevel@tonic-gate {
18087c478bd9Sstevel@tonic-gate 	int bus_range[2];
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate 	if (pci_bus_res[bus].dip == NULL)
18117c478bd9Sstevel@tonic-gate 		return;
18127c478bd9Sstevel@tonic-gate 	bus_range[0] = bus;
18137c478bd9Sstevel@tonic-gate 	bus_range[1] = pci_bus_res[bus].sub_bus;
18147c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, pci_bus_res[bus].dip,
18157c478bd9Sstevel@tonic-gate 	    "bus-range", (int *)bus_range, 2);
18167c478bd9Sstevel@tonic-gate }
18177c478bd9Sstevel@tonic-gate 
1818b1f176e8Sjg /*
1819b1f176e8Sjg  * Add slot-names property for any named pci hot-plug slots
1820b1f176e8Sjg  */
1821b1f176e8Sjg static void
1822b1f176e8Sjg add_bus_slot_names_prop(int bus)
1823b1f176e8Sjg {
1824b1f176e8Sjg 	char slotprop[256];
1825b1f176e8Sjg 	int len;
1826b1f176e8Sjg 
1827b1f176e8Sjg 	len = pci_slot_names_prop(bus, slotprop, sizeof (slotprop));
1828b1f176e8Sjg 	if (len > 0) {
182953273e82Ssethg 		/*
183053273e82Ssethg 		 * Only create a peer bus node if this bus may be a peer bus.
183153273e82Ssethg 		 * It may be a peer bus if the dip is NULL and if par_bus is
183253273e82Ssethg 		 * -1 (par_bus is -1 if this bus was not found to be
183353273e82Ssethg 		 * subordinate to any PCI-PCI bridge).
183453273e82Ssethg 		 * If it's not a peer bus, then the ACPI BBN-handling code
183553273e82Ssethg 		 * will remove it later.
183653273e82Ssethg 		 */
183753273e82Ssethg 		if (pci_bus_res[bus].par_bus == (uchar_t)-1 &&
183853273e82Ssethg 		    pci_bus_res[bus].dip == NULL) {
183953273e82Ssethg 
1840b1f176e8Sjg 			create_root_bus_dip(bus);
184153273e82Ssethg 		}
184253273e82Ssethg 		if (pci_bus_res[bus].dip != NULL) {
184353273e82Ssethg 			ASSERT((len % sizeof (int)) == 0);
184453273e82Ssethg 			(void) ndi_prop_update_int_array(DDI_DEV_T_NONE,
184553273e82Ssethg 			    pci_bus_res[bus].dip, "slot-names",
184653273e82Ssethg 			    (int *)slotprop, len / sizeof (int));
184753273e82Ssethg 		} else {
184853273e82Ssethg 			cmn_err(CE_NOTE, "!BIOS BUG: Invalid bus number in PCI "
184953273e82Ssethg 			    "IRQ routing table; Not adding slot-names "
185053273e82Ssethg 			    "property for incorrect bus %d", bus);
185153273e82Ssethg 		}
1852b1f176e8Sjg 	}
1853b1f176e8Sjg }
1854b1f176e8Sjg 
18557c478bd9Sstevel@tonic-gate static int
185600d0963fSdilpreet memlist_to_range(ppb_ranges_t *rp, struct memlist *entry, int type)
18577c478bd9Sstevel@tonic-gate {
18587c478bd9Sstevel@tonic-gate 	if (entry == NULL)
18597c478bd9Sstevel@tonic-gate 		return (0);
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate 	/* assume 32-bit addresses */
186200d0963fSdilpreet 	rp->child_high = rp->parent_high = type;
18637c478bd9Sstevel@tonic-gate 	rp->child_mid = rp->parent_mid = 0;
186400d0963fSdilpreet 	rp->child_low = rp->parent_low = (uint32_t)entry->address;
186500d0963fSdilpreet 	rp->size_high = 0;
186600d0963fSdilpreet 	rp->size_low = (uint32_t)entry->size;
18677c478bd9Sstevel@tonic-gate 	return (1);
18687c478bd9Sstevel@tonic-gate }
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate static void
18717c478bd9Sstevel@tonic-gate add_ppb_ranges_prop(int bus)
18727c478bd9Sstevel@tonic-gate {
18737c478bd9Sstevel@tonic-gate 	int i = 0;
187400d0963fSdilpreet 	ppb_ranges_t *rp;
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate 	rp = kmem_alloc(3 * sizeof (*rp), KM_SLEEP);
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate 	i = memlist_to_range(&rp[0], pci_bus_res[bus].io_ports,
18797c478bd9Sstevel@tonic-gate 	    PCI_ADDR_IO | PCI_REG_REL_M);
18807c478bd9Sstevel@tonic-gate 	i += memlist_to_range(&rp[i], pci_bus_res[bus].mem_space,
18817c478bd9Sstevel@tonic-gate 	    PCI_ADDR_MEM32 | PCI_REG_REL_M);
18827c478bd9Sstevel@tonic-gate 	i += memlist_to_range(&rp[i], pci_bus_res[bus].pmem_space,
18837c478bd9Sstevel@tonic-gate 	    PCI_ADDR_MEM32 | PCI_REG_REL_M | PCI_REG_PF_M);
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate 	if (i != 0)
18867c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_int_array(DDI_DEV_T_NONE,
18877c478bd9Sstevel@tonic-gate 		    pci_bus_res[bus].dip, "ranges", (int *)rp,
188800d0963fSdilpreet 		    i * sizeof (ppb_ranges_t) / sizeof (int));
18897c478bd9Sstevel@tonic-gate 	kmem_free(rp, 3 * sizeof (*rp));
18907c478bd9Sstevel@tonic-gate }
18917c478bd9Sstevel@tonic-gate 
18927c478bd9Sstevel@tonic-gate static int
18937c478bd9Sstevel@tonic-gate memlist_to_spec(struct pci_phys_spec *sp, struct memlist *list, int type)
18947c478bd9Sstevel@tonic-gate {
18957c478bd9Sstevel@tonic-gate 	int i = 0;
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate 	while (list) {
18987c478bd9Sstevel@tonic-gate 		/* assume 32-bit addresses */
18997c478bd9Sstevel@tonic-gate 		sp->pci_phys_hi = type;
19007c478bd9Sstevel@tonic-gate 		sp->pci_phys_mid = 0;
19017c478bd9Sstevel@tonic-gate 		sp->pci_phys_low = (uint32_t)list->address;
19027c478bd9Sstevel@tonic-gate 		sp->pci_size_hi = 0;
19037c478bd9Sstevel@tonic-gate 		sp->pci_size_low = (uint32_t)list->size;
19047c478bd9Sstevel@tonic-gate 
19057c478bd9Sstevel@tonic-gate 		list = list->next;
19067c478bd9Sstevel@tonic-gate 		sp++, i++;
19077c478bd9Sstevel@tonic-gate 	}
19087c478bd9Sstevel@tonic-gate 	return (i);
19097c478bd9Sstevel@tonic-gate }
19107c478bd9Sstevel@tonic-gate 
19117c478bd9Sstevel@tonic-gate static void
19127c478bd9Sstevel@tonic-gate add_bus_available_prop(int bus)
19137c478bd9Sstevel@tonic-gate {
19147c478bd9Sstevel@tonic-gate 	int i, count;
19157c478bd9Sstevel@tonic-gate 	struct pci_phys_spec *sp;
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate 	count = memlist_count(pci_bus_res[bus].io_ports) +
19187c478bd9Sstevel@tonic-gate 	    memlist_count(pci_bus_res[bus].mem_space) +
19197c478bd9Sstevel@tonic-gate 	    memlist_count(pci_bus_res[bus].pmem_space);
19207c478bd9Sstevel@tonic-gate 
19217c478bd9Sstevel@tonic-gate 	if (count == 0)		/* nothing available */
19227c478bd9Sstevel@tonic-gate 		return;
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate 	sp = kmem_alloc(count * sizeof (*sp), KM_SLEEP);
19257c478bd9Sstevel@tonic-gate 	i = memlist_to_spec(&sp[0], pci_bus_res[bus].io_ports,
19267c478bd9Sstevel@tonic-gate 	    PCI_ADDR_IO | PCI_REG_REL_M);
19277c478bd9Sstevel@tonic-gate 	i += memlist_to_spec(&sp[i], pci_bus_res[bus].mem_space,
19287c478bd9Sstevel@tonic-gate 	    PCI_ADDR_MEM32 | PCI_REG_REL_M);
19297c478bd9Sstevel@tonic-gate 	i += memlist_to_spec(&sp[i], pci_bus_res[bus].pmem_space,
19307c478bd9Sstevel@tonic-gate 	    PCI_ADDR_MEM32 | PCI_REG_REL_M | PCI_REG_PF_M);
19317c478bd9Sstevel@tonic-gate 	ASSERT(i == count);
19327c478bd9Sstevel@tonic-gate 
19337c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, pci_bus_res[bus].dip,
19347c478bd9Sstevel@tonic-gate 	    "available", (int *)sp,
19357c478bd9Sstevel@tonic-gate 	    i * sizeof (struct pci_phys_spec) / sizeof (int));
19367c478bd9Sstevel@tonic-gate 	kmem_free(sp, count * sizeof (*sp));
19377c478bd9Sstevel@tonic-gate }
1938f55ce205Sszhou 
1939f55ce205Sszhou static void
1940f55ce205Sszhou alloc_res_array(void)
1941f55ce205Sszhou {
1942f55ce205Sszhou 	static int array_max = 0;
1943f55ce205Sszhou 	int old_max;
1944f55ce205Sszhou 	void *old_res;
1945f55ce205Sszhou 
1946f55ce205Sszhou 	if (array_max > pci_bios_nbus + 1)
1947f55ce205Sszhou 		return;	/* array is big enough */
1948f55ce205Sszhou 
1949f55ce205Sszhou 	old_max = array_max;
1950f55ce205Sszhou 	old_res = pci_bus_res;
1951f55ce205Sszhou 
1952f55ce205Sszhou 	if (array_max == 0)
1953f55ce205Sszhou 		array_max = 16;	/* start with a reasonable number */
1954f55ce205Sszhou 
1955f55ce205Sszhou 	while (array_max < pci_bios_nbus + 1)
1956f55ce205Sszhou 		array_max <<= 1;
1957f55ce205Sszhou 	pci_bus_res = (struct pci_bus_resource *)kmem_zalloc(
1958f55ce205Sszhou 	    array_max * sizeof (struct pci_bus_resource), KM_SLEEP);
1959f55ce205Sszhou 
1960f55ce205Sszhou 	if (old_res) {	/* copy content and free old array */
1961f55ce205Sszhou 		bcopy(old_res, pci_bus_res,
1962f55ce205Sszhou 		    old_max * sizeof (struct pci_bus_resource));
1963f55ce205Sszhou 		kmem_free(old_res, old_max * sizeof (struct pci_bus_resource));
1964f55ce205Sszhou 	}
1965f55ce205Sszhou }
1966c8589f13Ssethg 
1967c8589f13Ssethg static void
1968c8589f13Ssethg create_ioapic_node(int bus, int dev, int fn, ushort_t vendorid,
1969c8589f13Ssethg     ushort_t deviceid)
1970c8589f13Ssethg {
1971c8589f13Ssethg 	static dev_info_t *ioapicsnode = NULL;
1972c8589f13Ssethg 	static int numioapics = 0;
1973c8589f13Ssethg 	dev_info_t *ioapic_node;
1974c8589f13Ssethg 	uint64_t physaddr;
1975c8589f13Ssethg 	uint32_t lobase, hibase = 0;
1976c8589f13Ssethg 
1977c8589f13Ssethg 	/* BAR 0 contains the IOAPIC's memory-mapped I/O address */
1978c8589f13Ssethg 	lobase = (*pci_getl_func)(bus, dev, fn, PCI_CONF_BASE0);
1979c8589f13Ssethg 
1980c8589f13Ssethg 	/* We (and the rest of the world) only support memory-mapped IOAPICs */
1981c8589f13Ssethg 	if ((lobase & PCI_BASE_SPACE_M) != PCI_BASE_SPACE_MEM)
1982c8589f13Ssethg 		return;
1983c8589f13Ssethg 
1984c8589f13Ssethg 	if ((lobase & PCI_BASE_TYPE_M) == PCI_BASE_TYPE_ALL)
1985c8589f13Ssethg 		hibase = (*pci_getl_func)(bus, dev, fn, PCI_CONF_BASE0 + 4);
1986c8589f13Ssethg 
1987c8589f13Ssethg 	lobase &= PCI_BASE_M_ADDR_M;
1988c8589f13Ssethg 
1989c8589f13Ssethg 	physaddr = (((uint64_t)hibase) << 32) | lobase;
1990c8589f13Ssethg 
1991c8589f13Ssethg 	/*
1992c8589f13Ssethg 	 * Create a nexus node for all IOAPICs under the root node.
1993c8589f13Ssethg 	 */
1994c8589f13Ssethg 	if (ioapicsnode == NULL) {
1995c8589f13Ssethg 		if (ndi_devi_alloc(ddi_root_node(), IOAPICS_NODE_NAME,
1996c8589f13Ssethg 		    (pnode_t)DEVI_SID_NODEID, &ioapicsnode) != NDI_SUCCESS) {
1997c8589f13Ssethg 			return;
1998c8589f13Ssethg 		}
1999c8589f13Ssethg 		(void) ndi_devi_online(ioapicsnode, 0);
2000c8589f13Ssethg 	}
2001c8589f13Ssethg 
2002c8589f13Ssethg 	/*
2003c8589f13Ssethg 	 * Create a child node for this IOAPIC
2004c8589f13Ssethg 	 */
2005c8589f13Ssethg 	ioapic_node = ddi_add_child(ioapicsnode, IOAPICS_CHILD_NAME,
2006c8589f13Ssethg 	    DEVI_SID_NODEID, numioapics++);
2007c8589f13Ssethg 	if (ioapic_node == NULL) {
2008c8589f13Ssethg 		return;
2009c8589f13Ssethg 	}
2010c8589f13Ssethg 
2011c8589f13Ssethg 	/* Vendor and Device ID */
2012c8589f13Ssethg 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, ioapic_node,
2013c8589f13Ssethg 	    IOAPICS_PROP_VENID, vendorid);
2014c8589f13Ssethg 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, ioapic_node,
2015c8589f13Ssethg 	    IOAPICS_PROP_DEVID, deviceid);
2016c8589f13Ssethg 
2017c8589f13Ssethg 	/* device_type */
2018c8589f13Ssethg 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, ioapic_node,
2019c8589f13Ssethg 	    "device_type", IOAPICS_DEV_TYPE);
2020c8589f13Ssethg 
2021c8589f13Ssethg 	/* reg */
2022c8589f13Ssethg 	(void) ndi_prop_update_int64(DDI_DEV_T_NONE, ioapic_node,
2023c8589f13Ssethg 	    "reg", physaddr);
2024c8589f13Ssethg }
2025