xref: /titanic_41/usr/src/uts/intel/io/pci/pci_boot.c (revision 7a4f122cf63a91c2888629e433b5f5ee7536a802)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/sysmacros.h>
29 #include <sys/sunndi.h>
30 #include <sys/pci.h>
31 #include <sys/pci_impl.h>
32 #include <sys/pci_cfgspace.h>
33 #include <sys/memlist.h>
34 #include <sys/bootconf.h>
35 #include <io/pci/mps_table.h>
36 #include <sys/pci_cfgspace.h>
37 #include <sys/pci_cfgspace_impl.h>
38 #include <sys/psw.h>
39 #include "../../../../common/pci/pci_strings.h"
40 #include <sys/apic.h>
41 #include <io/pciex/pcie_nvidia.h>
42 #include <io/hotplug/pciehpc/pciehpc_acpi.h>
43 #include <sys/acpi/acpi.h>
44 #include <sys/acpica.h>
45 #include <sys/intel_iommu.h>
46 #include <sys/iommulib.h>
47 #include <sys/devcache.h>
48 
49 #define	pci_getb	(*pci_getb_func)
50 #define	pci_getw	(*pci_getw_func)
51 #define	pci_getl	(*pci_getl_func)
52 #define	pci_putb	(*pci_putb_func)
53 #define	pci_putw	(*pci_putw_func)
54 #define	pci_putl	(*pci_putl_func)
55 #define	dcmn_err	if (pci_boot_debug) cmn_err
56 
57 #define	CONFIG_INFO	0
58 #define	CONFIG_UPDATE	1
59 #define	CONFIG_NEW	2
60 #define	CONFIG_FIX	3
61 #define	COMPAT_BUFSIZE	512
62 
63 #define	PPB_IO_ALIGNMENT	0x1000		/* 4K aligned */
64 #define	PPB_MEM_ALIGNMENT	0x100000	/* 1M aligned */
65 /* round down to nearest power of two */
66 #define	P2LE(align)					\
67 	{						\
68 		int i = 0;				\
69 		while (align >>= 1)			\
70 			i ++;				\
71 		align = 1 << i;				\
72 	}						\
73 
74 /* for is_vga and list_is_vga_only */
75 
76 enum io_mem {
77 	IO,
78 	MEM
79 };
80 
81 /* See AMD-8111 Datasheet Rev 3.03, Page 149: */
82 #define	LPC_IO_CONTROL_REG_1	0x40
83 #define	AMD8111_ENABLENMI	(uint8_t)0x80
84 #define	DEVID_AMD8111_LPC	0x7468
85 
86 struct pci_fixundo {
87 	uint8_t			bus;
88 	uint8_t			dev;
89 	uint8_t			fn;
90 	void			(*undofn)(uint8_t, uint8_t, uint8_t);
91 	struct pci_fixundo	*next;
92 };
93 
94 struct pci_devfunc {
95 	struct pci_devfunc *next;
96 	dev_info_t *dip;
97 	uchar_t dev;
98 	uchar_t func;
99 	boolean_t reprogram;	/* this device needs to be reprogrammed */
100 };
101 
102 extern int pseudo_isa;
103 extern int pci_bios_nbus;
104 static uchar_t max_dev_pci = 32;	/* PCI standard */
105 int pci_boot_debug = 0;
106 extern struct memlist *find_bus_res(int, int);
107 static struct pci_fixundo *undolist = NULL;
108 static int num_root_bus = 0;	/* count of root buses */
109 extern volatile int acpi_resource_discovery;
110 
111 /*
112  * Module prototypes
113  */
114 static void enumerate_bus_devs(uchar_t bus, int config_op);
115 static void create_root_bus_dip(uchar_t bus);
116 static void process_devfunc(uchar_t, uchar_t, uchar_t, uchar_t,
117     ushort_t, int);
118 static void add_compatible(dev_info_t *, ushort_t, ushort_t,
119     ushort_t, ushort_t, uchar_t, uint_t, int);
120 static int add_reg_props(dev_info_t *, uchar_t, uchar_t, uchar_t, int, int);
121 static void add_ppb_props(dev_info_t *, uchar_t, uchar_t, uchar_t, int,
122     ushort_t);
123 static void add_model_prop(dev_info_t *, uint_t);
124 static void add_bus_range_prop(int);
125 static void add_bus_slot_names_prop(int);
126 static void add_ranges_prop(int, int);
127 static void add_bus_available_prop(int);
128 static int get_pci_cap(uchar_t bus, uchar_t dev, uchar_t func, uint8_t cap_id);
129 static void fix_ppb_res(uchar_t, boolean_t);
130 static void alloc_res_array();
131 static void create_ioapic_node(int bus, int dev, int fn, ushort_t vendorid,
132     ushort_t deviceid);
133 static void pciex_slot_names_prop(dev_info_t *, ushort_t);
134 static void populate_bus_res(uchar_t bus);
135 static void memlist_remove_list(struct memlist **list,
136     struct memlist *remove_list);
137 
138 static void pci_scan_bbn(void);
139 static int pci_unitaddr_cache_valid(void);
140 static int pci_bus_unitaddr(int);
141 static void pci_unitaddr_cache_create(void);
142 
143 static int pci_cache_unpack_nvlist(nvf_handle_t, nvlist_t *, char *);
144 static int pci_cache_pack_nvlist(nvf_handle_t, nvlist_t **);
145 static void pci_cache_free_list(nvf_handle_t);
146 
147 extern int pci_slot_names_prop(int, char *, int);
148 
149 /* set non-zero to force PCI peer-bus renumbering */
150 int pci_bus_always_renumber = 0;
151 
152 /*
153  * used to register ISA resource usage which must not be made
154  * "available" from other PCI node' resource maps
155  */
156 static struct {
157 	struct memlist *io_used;
158 	struct memlist *mem_used;
159 } isa_res;
160 
161 /*
162  * PCI unit-address cache management
163  */
164 static nvf_ops_t pci_unitaddr_cache_ops = {
165 	"/etc/devices/pci_unitaddr_persistent",	/* path to cache */
166 	pci_cache_unpack_nvlist,		/* read in nvlist form */
167 	pci_cache_pack_nvlist,			/* convert to nvlist form */
168 	pci_cache_free_list,			/* free data list */
169 	NULL					/* write complete callback */
170 };
171 
172 typedef struct {
173 	list_node_t	pua_nodes;
174 	int		pua_index;
175 	int		pua_addr;
176 } pua_node_t;
177 
178 nvf_handle_t	puafd_handle;
179 int		pua_cache_valid = 0;
180 
181 
182 /*ARGSUSED*/
183 static ACPI_STATUS
184 pci_process_acpi_device(ACPI_HANDLE hdl, UINT32 level, void *ctx, void **rv)
185 {
186 	ACPI_BUFFER	rb;
187 	ACPI_OBJECT	ro;
188 	ACPI_DEVICE_INFO *adi;
189 
190 	/*
191 	 * Use AcpiGetObjectInfo() to find the device _HID
192 	 * If not a PCI root-bus, ignore this device and continue
193 	 * the walk
194 	 */
195 
196 	rb.Length = ACPI_ALLOCATE_BUFFER;
197 	if (ACPI_FAILURE(AcpiGetObjectInfo(hdl, &rb)))
198 		return (AE_OK);
199 
200 	adi = rb.Pointer;
201 	if (!(adi->Valid & ACPI_VALID_HID)) {
202 		AcpiOsFree(adi);
203 		return (AE_OK);
204 	}
205 
206 	if (strncmp(adi->HardwareId.Value, PCI_ROOT_HID_STRING,
207 	    sizeof (PCI_ROOT_HID_STRING)) &&
208 	    strncmp(adi->HardwareId.Value, PCI_EXPRESS_ROOT_HID_STRING,
209 	    sizeof (PCI_EXPRESS_ROOT_HID_STRING))) {
210 		AcpiOsFree(adi);
211 		return (AE_OK);
212 	}
213 
214 	AcpiOsFree(adi);
215 
216 	/*
217 	 * XXX: ancient Big Bear broken _BBN will result in two
218 	 * bus 0 _BBNs being found, so we need to handle duplicate
219 	 * bus 0 gracefully.  However, broken _BBN does not
220 	 * hide a childless root-bridge so no need to work-around it
221 	 * here
222 	 */
223 	rb.Pointer = &ro;
224 	rb.Length = sizeof (ro);
225 	if (ACPI_SUCCESS(AcpiEvaluateObjectTyped(hdl, "_BBN",
226 	    NULL, &rb, ACPI_TYPE_INTEGER))) {
227 		/* PCI with _BBN, process it, go no deeper */
228 		if (pci_bus_res[ro.Integer.Value].par_bus == (uchar_t)-1 &&
229 		    pci_bus_res[ro.Integer.Value].dip == NULL)
230 			create_root_bus_dip((uchar_t)ro.Integer.Value);
231 		return (AE_CTRL_DEPTH);
232 	}
233 
234 	/* PCI and no _BBN, continue walk */
235 	return (AE_OK);
236 }
237 
238 /*
239  * Scan the ACPI namespace for all top-level instances of _BBN
240  * in order to discover childless root-bridges (which enumeration
241  * may not find; root-bridges are inferred by the existence of
242  * children).  This scan should find all root-bridges that have
243  * been enumerated, and any childless root-bridges not enumerated.
244  * Root-bridge for bus 0 may not have a _BBN object.
245  */
246 static void
247 pci_scan_bbn()
248 {
249 	void *rv;
250 
251 	(void) AcpiGetDevices(NULL, pci_process_acpi_device, NULL, &rv);
252 }
253 
254 static void
255 pci_unitaddr_cache_init(void)
256 {
257 
258 	puafd_handle = nvf_register_file(&pci_unitaddr_cache_ops);
259 	ASSERT(puafd_handle);
260 
261 	list_create(nvf_list(puafd_handle), sizeof (pua_node_t),
262 	    offsetof(pua_node_t, pua_nodes));
263 
264 	rw_enter(nvf_lock(puafd_handle), RW_WRITER);
265 	(void) nvf_read_file(puafd_handle);
266 	rw_exit(nvf_lock(puafd_handle));
267 }
268 
269 /*
270  * Format of /etc/devices/pci_unitaddr_persistent:
271  *
272  * The persistent record of unit-address assignments contains
273  * a list of name/value pairs, where name is a string representation
274  * of the "index value" of the PCI root-bus and the value is
275  * the assigned unit-address.
276  *
277  * The "index value" is simply the zero-based index of the PCI
278  * root-buses ordered by physical bus number; first PCI bus is 0,
279  * second is 1, and so on.
280  */
281 
282 static int
283 pci_cache_unpack_nvlist(nvf_handle_t hdl, nvlist_t *nvl, char *name)
284 {
285 	long		index;
286 	int32_t		value;
287 	nvpair_t	*np;
288 	pua_node_t	*node;
289 
290 	np = NULL;
291 	while ((np = nvlist_next_nvpair(nvl, np)) != NULL) {
292 		/* name of nvpair is index value */
293 		if (ddi_strtol(nvpair_name(np), NULL, 10, &index) != 0)
294 			continue;
295 
296 		if (nvpair_value_int32(np, &value) != 0)
297 			continue;
298 
299 		node = kmem_zalloc(sizeof (pua_node_t), KM_SLEEP);
300 		node->pua_index = index;
301 		node->pua_addr = value;
302 		list_insert_tail(nvf_list(hdl), node);
303 	}
304 
305 	pua_cache_valid = 1;
306 	return (DDI_SUCCESS);
307 }
308 
309 static int
310 pci_cache_pack_nvlist(nvf_handle_t hdl, nvlist_t **ret_nvl)
311 {
312 	int		rval;
313 	nvlist_t	*nvl, *sub_nvl;
314 	list_t		*listp;
315 	pua_node_t	*pua;
316 	char		buf[13];
317 
318 	ASSERT(RW_WRITE_HELD(nvf_lock(hdl)));
319 
320 	rval = nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP);
321 	if (rval != DDI_SUCCESS) {
322 		nvf_error("%s: nvlist alloc error %d\n",
323 		    nvf_cache_name(hdl), rval);
324 		return (DDI_FAILURE);
325 	}
326 
327 	sub_nvl = NULL;
328 	rval = nvlist_alloc(&sub_nvl, NV_UNIQUE_NAME, KM_SLEEP);
329 	if (rval != DDI_SUCCESS)
330 		goto error;
331 
332 	listp = nvf_list(hdl);
333 	for (pua = list_head(listp); pua != NULL;
334 	    pua = list_next(listp, pua)) {
335 		snprintf(buf, sizeof (buf), "%d", pua->pua_index);
336 		rval = nvlist_add_int32(sub_nvl, buf, pua->pua_addr);
337 		if (rval != DDI_SUCCESS)
338 			goto error;
339 	}
340 
341 	rval = nvlist_add_nvlist(nvl, "table", sub_nvl);
342 	if (rval != DDI_SUCCESS)
343 		goto error;
344 	nvlist_free(sub_nvl);
345 
346 	*ret_nvl = nvl;
347 	return (DDI_SUCCESS);
348 
349 error:
350 	if (sub_nvl)
351 		nvlist_free(sub_nvl);
352 	ASSERT(nvl);
353 	nvlist_free(nvl);
354 	*ret_nvl = NULL;
355 	return (DDI_FAILURE);
356 }
357 
358 static void
359 pci_cache_free_list(nvf_handle_t hdl)
360 {
361 	list_t		*listp;
362 	pua_node_t	*pua;
363 
364 	ASSERT(RW_WRITE_HELD(nvf_lock(hdl)));
365 
366 	listp = nvf_list(hdl);
367 	for (pua = list_head(listp); pua != NULL;
368 	    pua = list_next(listp, pua)) {
369 		list_remove(listp, pua);
370 		kmem_free(pua, sizeof (pua_node_t));
371 	}
372 }
373 
374 
375 static int
376 pci_unitaddr_cache_valid(void)
377 {
378 
379 	/* read only, no need for rw lock */
380 	return (pua_cache_valid);
381 }
382 
383 
384 static int
385 pci_bus_unitaddr(int index)
386 {
387 	pua_node_t	*pua;
388 	list_t		*listp;
389 	int		addr;
390 
391 	rw_enter(nvf_lock(puafd_handle), RW_READER);
392 
393 	addr = -1;	/* default return if no match */
394 	listp = nvf_list(puafd_handle);
395 	for (pua = list_head(listp); pua != NULL;
396 	    pua = list_next(listp, pua)) {
397 		if (pua->pua_index == index) {
398 			addr = pua->pua_addr;
399 			break;
400 		}
401 	}
402 
403 	rw_exit(nvf_lock(puafd_handle));
404 	return (addr);
405 }
406 
407 static void
408 pci_unitaddr_cache_create(void)
409 {
410 	int		i, index;
411 	pua_node_t	*node;
412 	list_t		*listp;
413 
414 	rw_enter(nvf_lock(puafd_handle), RW_WRITER);
415 
416 	index = 0;
417 	listp = nvf_list(puafd_handle);
418 	for (i = 0; i <= pci_bios_nbus; i++) {
419 		/* skip non-root (peer) PCI busses */
420 		if ((pci_bus_res[i].par_bus != (uchar_t)-1) ||
421 		    (pci_bus_res[i].dip == NULL))
422 			continue;
423 		node = kmem_zalloc(sizeof (pua_node_t), KM_SLEEP);
424 		node->pua_index = index++;
425 		node->pua_addr = pci_bus_res[i].root_addr;
426 		list_insert_tail(listp, node);
427 	}
428 
429 	(void) nvf_mark_dirty(puafd_handle);
430 	rw_exit(nvf_lock(puafd_handle));
431 	nvf_wake_daemon();
432 }
433 
434 
435 /*
436  * Enumerate all PCI devices
437  */
438 void
439 pci_setup_tree(void)
440 {
441 	uint_t i, root_bus_addr = 0;
442 
443 	alloc_res_array();
444 	for (i = 0; i <= pci_bios_nbus; i++) {
445 		pci_bus_res[i].par_bus = (uchar_t)-1;
446 		pci_bus_res[i].root_addr = (uchar_t)-1;
447 		pci_bus_res[i].sub_bus = i;
448 	}
449 
450 	pci_bus_res[0].root_addr = root_bus_addr++;
451 	create_root_bus_dip(0);
452 	enumerate_bus_devs(0, CONFIG_INFO);
453 
454 	/*
455 	 * Now enumerate peer busses
456 	 *
457 	 * We loop till pci_bios_nbus. On most systems, there is
458 	 * one more bus at the high end, which implements the ISA
459 	 * compatibility bus. We don't care about that.
460 	 *
461 	 * Note: In the old (bootconf) enumeration, the peer bus
462 	 *	address did not use the bus number, and there were
463 	 *	too many peer busses created. The root_bus_addr is
464 	 *	used to maintain the old peer bus address assignment.
465 	 *	However, we stop enumerating phantom peers with no
466 	 *	device below.
467 	 */
468 	for (i = 1; i <= pci_bios_nbus; i++) {
469 		if (pci_bus_res[i].dip == NULL) {
470 			pci_bus_res[i].root_addr = root_bus_addr++;
471 		}
472 		enumerate_bus_devs(i, CONFIG_INFO);
473 
474 		/* add slot-names property for named pci hot-plug slots */
475 		add_bus_slot_names_prop(i);
476 	}
477 
478 }
479 
480 /*
481  * >0 = present, 0 = not present, <0 = error
482  */
483 static int
484 pci_bbn_present(int bus)
485 {
486 	ACPI_HANDLE	hdl;
487 	int	rv;
488 
489 	/* no dip means no _BBN */
490 	if (pci_bus_res[bus].dip == NULL)
491 		return (0);
492 
493 	rv = -1;	/* default return value in case of error below */
494 	if (ACPI_SUCCESS(acpica_get_handle(pci_bus_res[bus].dip, &hdl))) {
495 		switch (AcpiEvaluateObject(hdl, "_BBN", NULL, NULL)) {
496 		case AE_OK:
497 			rv = 1;
498 			break;
499 		case AE_NOT_FOUND:
500 			rv = 0;
501 			break;
502 		default:
503 			break;
504 		}
505 	}
506 
507 	return (rv);
508 }
509 
510 /*
511  * Return non-zero if any PCI bus in the system has an associated
512  * _BBN object, 0 otherwise.
513  */
514 static int
515 pci_roots_have_bbn(void)
516 {
517 	int	i;
518 
519 	/*
520 	 * Scan the PCI busses and look for at least 1 _BBN
521 	 */
522 	for (i = 0; i <= pci_bios_nbus; i++) {
523 		/* skip non-root (peer) PCI busses */
524 		if (pci_bus_res[i].par_bus != (uchar_t)-1)
525 			continue;
526 
527 		if (pci_bbn_present(i) > 0)
528 			return (1);
529 	}
530 	return (0);
531 
532 }
533 
534 /*
535  * return non-zero if the machine is one on which we renumber
536  * the internal pci unit-addresses
537  */
538 static int
539 pci_bus_renumber()
540 {
541 	ACPI_TABLE_HEADER *fadt;
542 
543 	if (pci_bus_always_renumber)
544 		return (1);
545 
546 	/* get the FADT */
547 	if (AcpiGetTable(ACPI_SIG_FADT, 1, (ACPI_TABLE_HEADER **)&fadt) !=
548 	    AE_OK)
549 		return (0);
550 
551 	/* compare OEM Table ID to "SUNm31" */
552 	if (strncmp("SUNm31", fadt->OemId, 6))
553 		return (0);
554 	else
555 		return (1);
556 }
557 
558 /*
559  * Initial enumeration of the physical PCI bus hierarchy can
560  * leave 'gaps' in the order of peer PCI bus unit-addresses.
561  * Systems with more than one peer PCI bus *must* have an ACPI
562  * _BBN object associated with each peer bus; use the presence
563  * of this object to remove gaps in the numbering of the peer
564  * PCI bus unit-addresses - only peer busses with an associated
565  * _BBN are counted.
566  */
567 static void
568 pci_renumber_root_busses(void)
569 {
570 	int pci_regs[] = {0, 0, 0};
571 	int	i, root_addr = 0;
572 
573 	/*
574 	 * Currently, we only enable the re-numbering on specific
575 	 * Sun machines; this is a work-around for the more complicated
576 	 * issue of upgrade changing physical device paths
577 	 */
578 	if (!pci_bus_renumber())
579 		return;
580 
581 	/*
582 	 * If we find no _BBN objects at all, we either don't need
583 	 * to do anything or can't do anything anyway
584 	 */
585 	if (!pci_roots_have_bbn())
586 		return;
587 
588 	for (i = 0; i <= pci_bios_nbus; i++) {
589 		/* skip non-root (peer) PCI busses */
590 		if (pci_bus_res[i].par_bus != (uchar_t)-1)
591 			continue;
592 
593 		if (pci_bbn_present(i) < 1) {
594 			pci_bus_res[i].root_addr = (uchar_t)-1;
595 			continue;
596 		}
597 
598 		ASSERT(pci_bus_res[i].dip != NULL);
599 		if (pci_bus_res[i].root_addr != root_addr) {
600 			/* update reg property for node */
601 			pci_bus_res[i].root_addr = root_addr;
602 			pci_regs[0] = pci_bus_res[i].root_addr;
603 			(void) ndi_prop_update_int_array(DDI_DEV_T_NONE,
604 			    pci_bus_res[i].dip, "reg", (int *)pci_regs, 3);
605 		}
606 		root_addr++;
607 	}
608 }
609 
610 void
611 pci_register_isa_resources(int type, uint32_t base, uint32_t size)
612 {
613 	(void) memlist_insert(
614 	    (type == 1) ?  &isa_res.io_used : &isa_res.mem_used,
615 	    base, size);
616 }
617 
618 /*
619  * Remove the resources which are already used by devices under a subtractive
620  * bridge from the bus's resources lists, because they're not available, and
621  * shouldn't be allocated to other buses.  This is necessary because tracking
622  * resources for subtractive bridges is not complete.  (Subtractive bridges only
623  * track some of their claimed resources, not "the rest of the address space" as
624  * they should, so that allocation to peer non-subtractive PPBs is easier.  We
625  * need a fully-capable global resource allocator).
626  */
627 static void
628 remove_subtractive_res()
629 {
630 	int i, j;
631 	struct memlist *list;
632 
633 	for (i = 0; i <= pci_bios_nbus; i++) {
634 		if (pci_bus_res[i].subtractive) {
635 			/* remove used io ports */
636 			list = pci_bus_res[i].io_used;
637 			while (list) {
638 				for (j = 0; j <= pci_bios_nbus; j++)
639 					(void) memlist_remove(
640 					    &pci_bus_res[j].io_avail,
641 					    list->address, list->size);
642 				list = list->next;
643 			}
644 			/* remove used mem resource */
645 			list = pci_bus_res[i].mem_used;
646 			while (list) {
647 				for (j = 0; j <= pci_bios_nbus; j++) {
648 					(void) memlist_remove(
649 					    &pci_bus_res[j].mem_avail,
650 					    list->address, list->size);
651 					(void) memlist_remove(
652 					    &pci_bus_res[j].pmem_avail,
653 					    list->address, list->size);
654 				}
655 				list = list->next;
656 			}
657 			/* remove used prefetchable mem resource */
658 			list = pci_bus_res[i].pmem_used;
659 			while (list) {
660 				for (j = 0; j <= pci_bios_nbus; j++) {
661 					(void) memlist_remove(
662 					    &pci_bus_res[j].pmem_avail,
663 					    list->address, list->size);
664 					(void) memlist_remove(
665 					    &pci_bus_res[j].mem_avail,
666 					    list->address, list->size);
667 				}
668 				list = list->next;
669 			}
670 		}
671 	}
672 }
673 
674 /*
675  * Set up (or complete the setup of) the bus_avail resource list
676  */
677 static void
678 setup_bus_res(int bus)
679 {
680 	uchar_t par_bus;
681 
682 	if (pci_bus_res[bus].dip == NULL)	/* unused bus */
683 		return;
684 
685 	/*
686 	 * Set up bus_avail if not already filled in by populate_bus_res()
687 	 */
688 	if (pci_bus_res[bus].bus_avail == NULL) {
689 		ASSERT(pci_bus_res[bus].sub_bus >= bus);
690 		memlist_insert(&pci_bus_res[bus].bus_avail, bus,
691 		    pci_bus_res[bus].sub_bus - bus + 1);
692 	}
693 
694 	ASSERT(pci_bus_res[bus].bus_avail != NULL);
695 
696 	/*
697 	 * Remove resources from parent bus node if this is not a
698 	 * root bus.
699 	 */
700 	par_bus = pci_bus_res[bus].par_bus;
701 	if (par_bus != (uchar_t)-1) {
702 		ASSERT(pci_bus_res[par_bus].bus_avail != NULL);
703 		memlist_remove_list(&pci_bus_res[par_bus].bus_avail,
704 		    pci_bus_res[bus].bus_avail);
705 	}
706 
707 	/* remove self from bus_avail */;
708 	(void) memlist_remove(&pci_bus_res[bus].bus_avail, bus, 1);
709 }
710 
711 static uint64_t
712 get_parbus_io_res(uchar_t parbus, uchar_t bus, uint64_t size, uint64_t align)
713 {
714 	uint64_t addr = 0;
715 	uchar_t res_bus;
716 
717 	/*
718 	 * Skip root(peer) buses in multiple-root-bus systems when
719 	 * ACPI resource discovery was not successfully done.
720 	 */
721 	if ((pci_bus_res[parbus].par_bus == (uchar_t)-1) &&
722 	    (num_root_bus > 1) && (acpi_resource_discovery <= 0))
723 		return (0);
724 
725 	res_bus = parbus;
726 	while (pci_bus_res[res_bus].subtractive) {
727 		if (pci_bus_res[res_bus].io_avail)
728 			break;
729 		res_bus = pci_bus_res[res_bus].par_bus;
730 		if (res_bus == (uchar_t)-1)
731 			break; /* root bus already */
732 	}
733 
734 	if (pci_bus_res[res_bus].io_avail) {
735 		addr = memlist_find(&pci_bus_res[res_bus].io_avail,
736 		    size, align);
737 		if (addr) {
738 			memlist_insert(&pci_bus_res[res_bus].io_used,
739 			    addr, size);
740 
741 			/* free the old resource */
742 			memlist_free_all(&pci_bus_res[bus].io_avail);
743 			memlist_free_all(&pci_bus_res[bus].io_used);
744 
745 			/* add the new resource */
746 			memlist_insert(&pci_bus_res[bus].io_avail, addr, size);
747 		}
748 	}
749 
750 	return (addr);
751 }
752 
753 static uint64_t
754 get_parbus_mem_res(uchar_t parbus, uchar_t bus, uint64_t size, uint64_t align)
755 {
756 	uint64_t addr = 0;
757 	uchar_t res_bus;
758 
759 	/*
760 	 * Skip root(peer) buses in multiple-root-bus systems when
761 	 * ACPI resource discovery was not successfully done.
762 	 */
763 	if ((pci_bus_res[parbus].par_bus == (uchar_t)-1) &&
764 	    (num_root_bus > 1) && (acpi_resource_discovery <= 0))
765 		return (0);
766 
767 	res_bus = parbus;
768 	while (pci_bus_res[res_bus].subtractive) {
769 		if (pci_bus_res[res_bus].mem_avail)
770 			break;
771 		res_bus = pci_bus_res[res_bus].par_bus;
772 		if (res_bus == (uchar_t)-1)
773 			break; /* root bus already */
774 	}
775 
776 	if (pci_bus_res[res_bus].mem_avail) {
777 		addr = memlist_find(&pci_bus_res[res_bus].mem_avail,
778 		    size, align);
779 		if (addr) {
780 			memlist_insert(&pci_bus_res[res_bus].mem_used,
781 			    addr, size);
782 			(void) memlist_remove(&pci_bus_res[res_bus].pmem_avail,
783 			    addr, size);
784 
785 			/* free the old resource */
786 			memlist_free_all(&pci_bus_res[bus].mem_avail);
787 			memlist_free_all(&pci_bus_res[bus].mem_used);
788 
789 			/* add the new resource */
790 			memlist_insert(&pci_bus_res[bus].mem_avail, addr, size);
791 		}
792 	}
793 
794 	return (addr);
795 }
796 
797 /*
798  * given a cap_id, return its cap_id location in config space
799  */
800 static int
801 get_pci_cap(uchar_t bus, uchar_t dev, uchar_t func, uint8_t cap_id)
802 {
803 	uint8_t curcap, cap_id_loc;
804 	uint16_t status;
805 	int location = -1;
806 
807 	/*
808 	 * Need to check the Status register for ECP support first.
809 	 * Also please note that for type 1 devices, the
810 	 * offset could change. Should support type 1 next.
811 	 */
812 	status = pci_getw(bus, dev, func, PCI_CONF_STAT);
813 	if (!(status & PCI_STAT_CAP)) {
814 		return (-1);
815 	}
816 	cap_id_loc = pci_getb(bus, dev, func, PCI_CONF_CAP_PTR);
817 
818 	/* Walk the list of capabilities */
819 	while (cap_id_loc && cap_id_loc != (uint8_t)-1) {
820 		curcap = pci_getb(bus, dev, func, cap_id_loc);
821 
822 		if (curcap == cap_id) {
823 			location = cap_id_loc;
824 			break;
825 		}
826 		cap_id_loc = pci_getb(bus, dev, func, cap_id_loc + 1);
827 	}
828 	return (location);
829 }
830 
831 /*
832  * Does this resource element live in the legacy VGA range?
833  */
834 
835 int
836 is_vga(struct memlist *elem, enum io_mem io)
837 {
838 
839 	if (io == IO) {
840 		if ((elem->address == 0x3b0 && elem->size == 0xc) ||
841 		    (elem->address == 0x3c0 && elem->size == 0x20))
842 			return (1);
843 	} else {
844 		if (elem->address == 0xa0000 && elem->size == 0x20000)
845 			return (1);
846 	}
847 	return (0);
848 }
849 
850 /*
851  * Does this entire resource list consist only of legacy VGA resources?
852  */
853 
854 int
855 list_is_vga_only(struct memlist *l, enum io_mem io)
856 {
857 	do {
858 		if (!is_vga(l, io))
859 			return (0);
860 	} while ((l = l->next) != NULL);
861 	return (1);
862 }
863 
864 /*
865  * Assign valid resources to unconfigured pci(e) bridges. We are trying
866  * to reprogram the bridge when its
867  * 		i)   SECBUS == SUBBUS	||
868  * 		ii)  IOBASE > IOLIM	||
869  * 		iii) MEMBASE > MEMLIM
870  * This must be done after one full pass through the PCI tree to collect
871  * all BIOS-configured resources, so that we know what resources are
872  * free and available to assign to the unconfigured PPBs.
873  */
874 static void
875 fix_ppb_res(uchar_t secbus, boolean_t prog_sub)
876 {
877 	uchar_t bus, dev, func;
878 	uchar_t parbus, subbus;
879 	uint_t io_base, io_limit, mem_base, mem_limit;
880 	uint_t io_size, mem_size, io_align, mem_align;
881 	uint64_t addr = 0;
882 	int *regp = NULL;
883 	uint_t reglen;
884 	int rv, cap_ptr, physhi;
885 	dev_info_t *dip;
886 	uint16_t cmd_reg;
887 	struct memlist *list, *scratch_list;
888 
889 	/* skip root (peer) PCI busses */
890 	if (pci_bus_res[secbus].par_bus == (uchar_t)-1)
891 		return;
892 
893 	/* skip subtractive PPB when prog_sub is not TRUE */
894 	if (pci_bus_res[secbus].subtractive && !prog_sub)
895 		return;
896 
897 	/* some entries may be empty due to discontiguous bus numbering */
898 	dip = pci_bus_res[secbus].dip;
899 	if (dip == NULL)
900 		return;
901 
902 	rv = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
903 	    "reg", &regp, &reglen);
904 	if (rv != DDI_PROP_SUCCESS || reglen == 0)
905 		return;
906 	physhi = regp[0];
907 	ddi_prop_free(regp);
908 
909 	func = (uchar_t)PCI_REG_FUNC_G(physhi);
910 	dev = (uchar_t)PCI_REG_DEV_G(physhi);
911 	bus = (uchar_t)PCI_REG_BUS_G(physhi);
912 
913 	/*
914 	 * If pcie bridge, check to see if link is enabled
915 	 */
916 	cap_ptr = get_pci_cap(bus, dev, func, PCI_CAP_ID_PCI_E);
917 	if (cap_ptr != -1) {
918 		cmd_reg = pci_getw(bus, dev, func,
919 		    (uint16_t)cap_ptr + PCIE_LINKCTL);
920 		if (cmd_reg & PCIE_LINKCTL_LINK_DISABLE) {
921 			dcmn_err(CE_NOTE,
922 			    "!fix_ppb_res: ppb[%x/%x/%x] link is disabled.\n",
923 			    bus, dev, func);
924 			return;
925 		}
926 	}
927 
928 	subbus = pci_getb(bus, dev, func, PCI_BCNF_SUBBUS);
929 	parbus = pci_bus_res[secbus].par_bus;
930 	ASSERT(parbus == bus);
931 	cmd_reg = pci_getw(bus, dev, func, PCI_CONF_COMM);
932 
933 	/*
934 	 * If we have a Cardbus bridge, but no bus space
935 	 */
936 	if (pci_bus_res[secbus].num_cbb != 0 &&
937 	    pci_bus_res[secbus].bus_avail == NULL) {
938 		uchar_t range;
939 
940 		/* normally there are 2 buses under a cardbus bridge */
941 		range = pci_bus_res[secbus].num_cbb * 2;
942 
943 		/*
944 		 * Try to find and allocate a bus-range starting at subbus+1
945 		 * from the parent of the PPB.
946 		 */
947 		for (; range != 0; range--) {
948 			if (memlist_find_with_startaddr(
949 			    &pci_bus_res[parbus].bus_avail,
950 			    subbus + 1, range, 1) != NULL)
951 				break; /* find bus range resource at parent */
952 		}
953 		if (range != 0) {
954 			memlist_insert(&pci_bus_res[secbus].bus_avail,
955 			    subbus + 1, range);
956 			subbus = subbus + range;
957 			pci_bus_res[secbus].sub_bus = subbus;
958 			pci_putb(bus, dev, func, PCI_BCNF_SUBBUS, subbus);
959 			add_bus_range_prop(secbus);
960 
961 			cmn_err(CE_NOTE, "!reprogram bus-range on ppb"
962 			    "[%x/%x/%x]: %x ~ %x\n", bus, dev, func,
963 			    secbus, subbus);
964 		}
965 	}
966 
967 	/*
968 	 * Calculate required IO size and alignment
969 	 * If bus io_size is zero, we are going to assign 512 bytes per bus,
970 	 * otherwise, we'll choose the maximum value of such calculation and
971 	 * bus io_size. The size needs to be 4K aligned.
972 	 *
973 	 * We calculate alignment as the largest power of two less than the
974 	 * the sum of all children's IO size requirements, because this will
975 	 * align to the size of the largest child request within that size
976 	 * (which is always a power of two).
977 	 */
978 	io_size = (subbus - secbus + 1) * 0x200;
979 	if (io_size <  pci_bus_res[secbus].io_size)
980 		io_size = pci_bus_res[secbus].io_size;
981 	io_size = P2ROUNDUP(io_size, PPB_IO_ALIGNMENT);
982 	io_align = io_size;
983 	P2LE(io_align);
984 
985 	/*
986 	 * Calculate required MEM size and alignment
987 	 * If bus mem_size is zero, we are going to assign 1M bytes per bus,
988 	 * otherwise, we'll choose the maximum value of such calculation and
989 	 * bus mem_size. The size needs to be 1M aligned.
990 	 *
991 	 * For the alignment, refer to the I/O comment above.
992 	 */
993 	mem_size = (subbus - secbus + 1) * PPB_MEM_ALIGNMENT;
994 	if (mem_size < pci_bus_res[secbus].mem_size) {
995 		mem_size = pci_bus_res[secbus].mem_size;
996 		mem_size = P2ROUNDUP(mem_size, PPB_MEM_ALIGNMENT);
997 	}
998 	mem_align = mem_size;
999 	P2LE(mem_align);
1000 
1001 	/* Subtractive bridge */
1002 	if (pci_bus_res[secbus].subtractive && prog_sub) {
1003 		/*
1004 		 * We program an arbitrary amount of I/O and memory resource
1005 		 * for the subtractive bridge so that child dynamic-resource-
1006 		 * allocating devices (such as Cardbus bridges) have a chance
1007 		 * of success.  Until we have full-tree resource rebalancing,
1008 		 * dynamic resource allocation (thru busra) only looks at the
1009 		 * parent bridge, so all PPBs must have some allocatable
1010 		 * resource.  For non-subtractive bridges, the resources come
1011 		 * from the base/limit register "windows", but subtractive
1012 		 * bridges often don't program those (since they don't need to).
1013 		 * If we put all the remaining resources on the subtractive
1014 		 * bridge, then peer non-subtractive bridges can't allocate
1015 		 * more space (even though this is probably most correct).
1016 		 * If we put the resources only on the parent, then allocations
1017 		 * from children of subtractive bridges will fail without
1018 		 * special-case code for bypassing the subtractive bridge.
1019 		 * This solution is the middle-ground temporary solution until
1020 		 * we have fully-capable resource allocation.
1021 		 */
1022 
1023 		/*
1024 		 * Add an arbitrary I/O resource to the subtractive PPB
1025 		 */
1026 		if (pci_bus_res[secbus].io_avail == NULL) {
1027 			addr = get_parbus_io_res(parbus, secbus, io_size,
1028 			    io_align);
1029 			if (addr) {
1030 				add_ranges_prop(secbus, 1);
1031 				pci_bus_res[secbus].io_reprogram =
1032 				    pci_bus_res[parbus].io_reprogram;
1033 
1034 				cmn_err(CE_NOTE, "!add io-range on subtractive"
1035 				    " ppb[%x/%x/%x]: 0x%x ~ 0x%x\n",
1036 				    bus, dev, func, (uint32_t)addr,
1037 				    (uint32_t)addr + io_size - 1);
1038 			}
1039 		}
1040 		/*
1041 		 * Add an arbitrary memory resource to the subtractive PPB
1042 		 */
1043 		if (pci_bus_res[secbus].mem_avail == NULL) {
1044 			addr = get_parbus_mem_res(parbus, secbus, mem_size,
1045 			    mem_align);
1046 			if (addr) {
1047 				add_ranges_prop(secbus, 1);
1048 				pci_bus_res[secbus].mem_reprogram =
1049 				    pci_bus_res[parbus].mem_reprogram;
1050 
1051 				cmn_err(CE_NOTE, "!add mem-range on "
1052 				    "subtractive ppb[%x/%x/%x]: 0x%x ~ 0x%x\n",
1053 				    bus, dev, func, (uint32_t)addr,
1054 				    (uint32_t)addr + mem_size - 1);
1055 			}
1056 		}
1057 
1058 		goto cmd_enable;
1059 	}
1060 
1061 	/*
1062 	 * Check to see if we need to reprogram I/O space, either because the
1063 	 * parent bus needed reprogramming and so do we, or because I/O space is
1064 	 * disabled in base/limit or command register.
1065 	 */
1066 	io_base = pci_getb(bus, dev, func, PCI_BCNF_IO_BASE_LOW);
1067 	io_limit = pci_getb(bus, dev, func, PCI_BCNF_IO_LIMIT_LOW);
1068 	io_base = (io_base & 0xf0) << 8;
1069 	io_limit = ((io_limit & 0xf0) << 8) | 0xfff;
1070 
1071 	/* Form list of all resources passed (avail + used) */
1072 	scratch_list = memlist_dup(pci_bus_res[secbus].io_avail);
1073 	memlist_merge(&pci_bus_res[secbus].io_used, &scratch_list);
1074 
1075 	if ((pci_bus_res[parbus].io_reprogram ||
1076 	    (io_base > io_limit) ||
1077 	    (!(cmd_reg & PCI_COMM_IO))) &&
1078 	    !list_is_vga_only(scratch_list, IO)) {
1079 		if (pci_bus_res[secbus].io_used) {
1080 			memlist_subsume(&pci_bus_res[secbus].io_used,
1081 			    &pci_bus_res[secbus].io_avail);
1082 		}
1083 		if (pci_bus_res[secbus].io_avail &&
1084 		    (!pci_bus_res[parbus].io_reprogram) &&
1085 		    (!pci_bus_res[parbus].subtractive)) {
1086 			/* rechoose old io ports info */
1087 			list = pci_bus_res[secbus].io_avail;
1088 			io_base = 0;
1089 			do {
1090 				if (is_vga(list, IO))
1091 					continue;
1092 				if (!io_base) {
1093 					io_base = (uint_t)list->address;
1094 					io_limit = (uint_t)
1095 					    list->address + list->size - 1;
1096 					io_base =
1097 					    P2ALIGN(io_base, PPB_IO_ALIGNMENT);
1098 				} else {
1099 					if (list->address + list->size >
1100 					    io_limit) {
1101 						io_limit = (uint_t)
1102 						    (list->address +
1103 						    list->size - 1);
1104 					}
1105 				}
1106 			} while ((list = list->next) != NULL);
1107 			/* 4K aligned */
1108 			io_limit = P2ROUNDUP(io_limit, PPB_IO_ALIGNMENT) - 1;
1109 			io_size = io_limit - io_base + 1;
1110 			ASSERT(io_base <= io_limit);
1111 			memlist_free_all(&pci_bus_res[secbus].io_avail);
1112 			memlist_insert(&pci_bus_res[secbus].io_avail,
1113 			    io_base, io_size);
1114 			memlist_insert(&pci_bus_res[parbus].io_used,
1115 			    io_base, io_size);
1116 			(void) memlist_remove(&pci_bus_res[parbus].io_avail,
1117 			    io_base, io_size);
1118 			pci_bus_res[secbus].io_reprogram = B_TRUE;
1119 		} else {
1120 			/* get new io ports from parent bus */
1121 			addr = get_parbus_io_res(parbus, secbus, io_size,
1122 			    io_align);
1123 			if (addr) {
1124 				io_base = addr;
1125 				io_limit = addr + io_size - 1;
1126 				pci_bus_res[secbus].io_reprogram = B_TRUE;
1127 			}
1128 		}
1129 		if (pci_bus_res[secbus].io_reprogram) {
1130 			/* reprogram PPB regs */
1131 			pci_putb(bus, dev, func, PCI_BCNF_IO_BASE_LOW,
1132 			    (uchar_t)((io_base>>8) & 0xf0));
1133 			pci_putb(bus, dev, func, PCI_BCNF_IO_LIMIT_LOW,
1134 			    (uchar_t)((io_limit>>8) & 0xf0));
1135 			pci_putb(bus, dev, func, PCI_BCNF_IO_BASE_HI, 0);
1136 			pci_putb(bus, dev, func, PCI_BCNF_IO_LIMIT_HI, 0);
1137 			add_ranges_prop(secbus, 1);
1138 
1139 			cmn_err(CE_NOTE, "!reprogram io-range on"
1140 			    " ppb[%x/%x/%x]: 0x%x ~ 0x%x\n",
1141 			    bus, dev, func, io_base, io_limit);
1142 		}
1143 	}
1144 	memlist_free_all(&scratch_list);
1145 
1146 	/*
1147 	 * Check memory space as we did I/O space.
1148 	 */
1149 	mem_base = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_MEM_BASE);
1150 	mem_base = (mem_base & 0xfff0) << 16;
1151 	mem_limit = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_MEM_LIMIT);
1152 	mem_limit = ((mem_limit & 0xfff0) << 16) | 0xfffff;
1153 
1154 	scratch_list = memlist_dup(pci_bus_res[secbus].mem_avail);
1155 	memlist_merge(&pci_bus_res[secbus].mem_used, &scratch_list);
1156 
1157 	if ((pci_bus_res[parbus].mem_reprogram ||
1158 	    (mem_base > mem_limit) ||
1159 	    (!(cmd_reg & PCI_COMM_MAE))) &&
1160 	    !list_is_vga_only(scratch_list, MEM)) {
1161 		if (pci_bus_res[secbus].mem_used) {
1162 			memlist_subsume(&pci_bus_res[secbus].mem_used,
1163 			    &pci_bus_res[secbus].mem_avail);
1164 		}
1165 		if (pci_bus_res[secbus].mem_avail &&
1166 		    (!pci_bus_res[parbus].mem_reprogram) &&
1167 		    (!pci_bus_res[parbus].subtractive)) {
1168 			/* rechoose old mem resource */
1169 			list = pci_bus_res[secbus].mem_avail;
1170 			mem_base = 0;
1171 			do {
1172 				if (is_vga(list, MEM))
1173 					continue;
1174 				if (mem_base == 0) {
1175 					mem_base = (uint_t)list->address;
1176 					mem_base = P2ALIGN(mem_base,
1177 					    PPB_MEM_ALIGNMENT);
1178 					mem_limit = (uint_t)
1179 					    (list->address + list->size - 1);
1180 				} else {
1181 					if ((list->address + list->size) >
1182 					    mem_limit) {
1183 						mem_limit = (uint_t)
1184 						    (list->address +
1185 						    list->size - 1);
1186 					}
1187 				}
1188 			} while ((list = list->next) != NULL);
1189 			mem_limit = P2ROUNDUP(mem_limit, PPB_MEM_ALIGNMENT) - 1;
1190 			mem_size = mem_limit + 1 - mem_base;
1191 			ASSERT(mem_base <= mem_limit);
1192 			memlist_free_all(&pci_bus_res[secbus].mem_avail);
1193 			memlist_insert(&pci_bus_res[secbus].mem_avail,
1194 			    mem_base, mem_size);
1195 			memlist_insert(&pci_bus_res[parbus].mem_used,
1196 			    mem_base, mem_size);
1197 			(void) memlist_remove(&pci_bus_res[parbus].mem_avail,
1198 			    mem_base, mem_size);
1199 			pci_bus_res[secbus].mem_reprogram = B_TRUE;
1200 		} else {
1201 			/* get new mem resource from parent bus */
1202 			addr = get_parbus_mem_res(parbus, secbus, mem_size,
1203 			    mem_align);
1204 			if (addr) {
1205 				mem_base = addr;
1206 				mem_limit = addr + mem_size - 1;
1207 				pci_bus_res[secbus].mem_reprogram = B_TRUE;
1208 			}
1209 		}
1210 
1211 		if (pci_bus_res[secbus].mem_reprogram) {
1212 			/* reprogram PPB MEM regs */
1213 			pci_putw(bus, dev, func, PCI_BCNF_MEM_BASE,
1214 			    (uint16_t)((mem_base>>16) & 0xfff0));
1215 			pci_putw(bus, dev, func, PCI_BCNF_MEM_LIMIT,
1216 			    (uint16_t)((mem_limit>>16) & 0xfff0));
1217 			/*
1218 			 * Disable PMEM window by setting base > limit.
1219 			 * We currently don't reprogram the PMEM like we've
1220 			 * done for I/O and MEM. (Devices that support prefetch
1221 			 * can use non-prefetch MEM.) Anyway, if the MEM access
1222 			 * bit is initially disabled by BIOS, we disable the
1223 			 * PMEM window manually by setting PMEM base > PMEM
1224 			 * limit here, in case there are incorrect values in
1225 			 * them from BIOS, so that we won't get in trouble once
1226 			 * the MEM access bit is enabled at the end of this
1227 			 * function.
1228 			 */
1229 			if (!(cmd_reg & PCI_COMM_MAE)) {
1230 				pci_putw(bus, dev, func, PCI_BCNF_PF_BASE_LOW,
1231 				    0xfff0);
1232 				pci_putw(bus, dev, func, PCI_BCNF_PF_LIMIT_LOW,
1233 				    0x0);
1234 				pci_putl(bus, dev, func, PCI_BCNF_PF_BASE_HIGH,
1235 				    0xffffffff);
1236 				pci_putl(bus, dev, func, PCI_BCNF_PF_LIMIT_HIGH,
1237 				    0x0);
1238 			}
1239 
1240 			add_ranges_prop(secbus, 1);
1241 
1242 			cmn_err(CE_NOTE, "!reprogram mem-range on"
1243 			    " ppb[%x/%x/%x]: 0x%x ~ 0x%x\n",
1244 			    bus, dev, func, mem_base, mem_limit);
1245 		}
1246 	}
1247 	memlist_free_all(&scratch_list);
1248 
1249 cmd_enable:
1250 	if (pci_bus_res[secbus].io_avail)
1251 		cmd_reg |= PCI_COMM_IO | PCI_COMM_ME;
1252 	if (pci_bus_res[secbus].mem_avail)
1253 		cmd_reg |= PCI_COMM_MAE | PCI_COMM_ME;
1254 	pci_putw(bus, dev, func, PCI_CONF_COMM, cmd_reg);
1255 }
1256 
1257 void
1258 pci_reprogram(void)
1259 {
1260 	int i, pci_reconfig = 1;
1261 	char *onoff;
1262 	int bus;
1263 
1264 	/*
1265 	 * Scan ACPI namespace for _BBN objects, make sure that
1266 	 * childless root-bridges appear in devinfo tree
1267 	 */
1268 	pci_scan_bbn();
1269 	pci_unitaddr_cache_init();
1270 
1271 	/*
1272 	 * Fix-up unit-address assignments if cache is available
1273 	 */
1274 	if (pci_unitaddr_cache_valid()) {
1275 		int pci_regs[] = {0, 0, 0};
1276 		int	new_addr;
1277 		int	index = 0;
1278 
1279 		for (bus = 0; bus <= pci_bios_nbus; bus++) {
1280 			/* skip non-root (peer) PCI busses */
1281 			if ((pci_bus_res[bus].par_bus != (uchar_t)-1) ||
1282 			    (pci_bus_res[bus].dip == NULL))
1283 				continue;
1284 
1285 			new_addr = pci_bus_unitaddr(index);
1286 			if (pci_bus_res[bus].root_addr != new_addr) {
1287 				/* update reg property for node */
1288 				pci_regs[0] = pci_bus_res[bus].root_addr =
1289 				    new_addr;
1290 				(void) ndi_prop_update_int_array(
1291 				    DDI_DEV_T_NONE, pci_bus_res[bus].dip,
1292 				    "reg", (int *)pci_regs, 3);
1293 			}
1294 			index++;
1295 		}
1296 	} else {
1297 		/* perform legacy processing */
1298 		pci_renumber_root_busses();
1299 		pci_unitaddr_cache_create();
1300 	}
1301 
1302 	/*
1303 	 * Do root-bus resource discovery
1304 	 */
1305 	for (bus = 0; bus <= pci_bios_nbus; bus++) {
1306 		/* skip non-root (peer) PCI busses */
1307 		if (pci_bus_res[bus].par_bus != (uchar_t)-1)
1308 			continue;
1309 
1310 		/*
1311 		 * 1. find resources associated with this root bus
1312 		 */
1313 		populate_bus_res(bus);
1314 
1315 
1316 		/*
1317 		 * 2. Remove used PCI and ISA resources from bus resource map
1318 		 */
1319 
1320 		memlist_remove_list(&pci_bus_res[bus].io_avail,
1321 		    pci_bus_res[bus].io_used);
1322 		memlist_remove_list(&pci_bus_res[bus].mem_avail,
1323 		    pci_bus_res[bus].mem_used);
1324 		memlist_remove_list(&pci_bus_res[bus].pmem_avail,
1325 		    pci_bus_res[bus].pmem_used);
1326 		memlist_remove_list(&pci_bus_res[bus].mem_avail,
1327 		    pci_bus_res[bus].pmem_used);
1328 		memlist_remove_list(&pci_bus_res[bus].pmem_avail,
1329 		    pci_bus_res[bus].mem_used);
1330 
1331 		memlist_remove_list(&pci_bus_res[bus].io_avail,
1332 		    isa_res.io_used);
1333 		memlist_remove_list(&pci_bus_res[bus].mem_avail,
1334 		    isa_res.mem_used);
1335 	}
1336 
1337 	memlist_free_all(&isa_res.io_used);
1338 	memlist_free_all(&isa_res.mem_used);
1339 
1340 	/* add bus-range property for root/peer bus nodes */
1341 	for (i = 0; i <= pci_bios_nbus; i++) {
1342 		/* create bus-range property on root/peer buses */
1343 		if (pci_bus_res[i].par_bus == (uchar_t)-1)
1344 			add_bus_range_prop(i);
1345 
1346 		/* setup bus range resource on each bus */
1347 		setup_bus_res(i);
1348 	}
1349 
1350 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
1351 	    DDI_PROP_DONTPASS, "pci-reprog", &onoff) == DDI_SUCCESS) {
1352 		if (strcmp(onoff, "off") == 0) {
1353 			pci_reconfig = 0;
1354 			cmn_err(CE_NOTE, "pci device reprogramming disabled");
1355 		}
1356 		ddi_prop_free(onoff);
1357 	}
1358 
1359 	remove_subtractive_res();
1360 
1361 	/* reprogram the non-subtractive PPB */
1362 	if (pci_reconfig)
1363 		for (i = 0; i <= pci_bios_nbus; i++)
1364 			fix_ppb_res(i, B_FALSE);
1365 
1366 	for (i = 0; i <= pci_bios_nbus; i++) {
1367 		/* configure devices not configured by BIOS */
1368 		if (pci_reconfig) {
1369 			/*
1370 			 * Reprogram the subtractive PPB. At this time, all its
1371 			 * siblings should have got their resources already.
1372 			 */
1373 			if (pci_bus_res[i].subtractive)
1374 				fix_ppb_res(i, B_TRUE);
1375 			enumerate_bus_devs(i, CONFIG_NEW);
1376 		}
1377 	}
1378 
1379 	/* All dev programmed, so we can create available prop */
1380 	for (i = 0; i <= pci_bios_nbus; i++)
1381 		add_bus_available_prop(i);
1382 }
1383 
1384 /*
1385  * populate bus resources
1386  */
1387 static void
1388 populate_bus_res(uchar_t bus)
1389 {
1390 
1391 	/* scan BIOS structures */
1392 	pci_bus_res[bus].pmem_avail = find_bus_res(bus, PREFETCH_TYPE);
1393 	pci_bus_res[bus].mem_avail = find_bus_res(bus, MEM_TYPE);
1394 	pci_bus_res[bus].io_avail = find_bus_res(bus, IO_TYPE);
1395 	pci_bus_res[bus].bus_avail = find_bus_res(bus, BUSRANGE_TYPE);
1396 
1397 	/*
1398 	 * attempt to initialize sub_bus from the largest range-end
1399 	 * in the bus_avail list
1400 	 */
1401 	if (pci_bus_res[bus].bus_avail != NULL) {
1402 		struct memlist *entry;
1403 		int current;
1404 
1405 		entry = pci_bus_res[bus].bus_avail;
1406 		while (entry != NULL) {
1407 			current = entry->address + entry->size - 1;
1408 			if (current > pci_bus_res[bus].sub_bus)
1409 				pci_bus_res[bus].sub_bus = current;
1410 			entry = entry->next;
1411 		}
1412 	}
1413 
1414 	if (bus == 0) {
1415 		/*
1416 		 * Special treatment of bus 0:
1417 		 * If no IO/MEM resource from ACPI/MPSPEC/HRT, copy
1418 		 * pcimem from boot and make I/O space the entire range
1419 		 * starting at 0x100.
1420 		 */
1421 		if (pci_bus_res[0].mem_avail == NULL)
1422 			pci_bus_res[0].mem_avail =
1423 			    memlist_dup(bootops->boot_mem->pcimem);
1424 		/* Exclude 0x00 to 0xff of the I/O space, used by all PCs */
1425 		if (pci_bus_res[0].io_avail == NULL)
1426 			memlist_insert(&pci_bus_res[0].io_avail, 0x100, 0xffff);
1427 	}
1428 
1429 	/*
1430 	 * Create 'ranges' property here before any resources are
1431 	 * removed from the resource lists
1432 	 */
1433 	add_ranges_prop(bus, 0);
1434 }
1435 
1436 
1437 /*
1438  * Create top-level bus dips, i.e. /pci@0,0, /pci@1,0...
1439  */
1440 static void
1441 create_root_bus_dip(uchar_t bus)
1442 {
1443 	int pci_regs[] = {0, 0, 0};
1444 	dev_info_t *dip;
1445 
1446 	ASSERT(pci_bus_res[bus].par_bus == (uchar_t)-1);
1447 
1448 	num_root_bus++;
1449 	ndi_devi_alloc_sleep(ddi_root_node(), "pci",
1450 	    (pnode_t)DEVI_SID_NODEID, &dip);
1451 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1452 	    "#address-cells", 3);
1453 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1454 	    "#size-cells", 2);
1455 	pci_regs[0] = pci_bus_res[bus].root_addr;
1456 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
1457 	    "reg", (int *)pci_regs, 3);
1458 
1459 	/*
1460 	 * If system has PCIe bus, then create different properties
1461 	 */
1462 	if (create_pcie_root_bus(bus, dip) == B_FALSE)
1463 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1464 		    "device_type", "pci");
1465 
1466 	(void) ndi_devi_bind_driver(dip, 0);
1467 	pci_bus_res[bus].dip = dip;
1468 }
1469 
1470 /*
1471  * For any fixed configuration (often compatability) pci devices
1472  * and those with their own expansion rom, create device nodes
1473  * to hold the already configured device details.
1474  */
1475 void
1476 enumerate_bus_devs(uchar_t bus, int config_op)
1477 {
1478 	uchar_t dev, func, nfunc, header;
1479 	ushort_t venid;
1480 	struct pci_devfunc *devlist = NULL, *entry;
1481 
1482 	if (config_op == CONFIG_NEW) {
1483 		dcmn_err(CE_NOTE, "configuring pci bus 0x%x", bus);
1484 	} else if (config_op == CONFIG_FIX) {
1485 		dcmn_err(CE_NOTE, "fixing devices on pci bus 0x%x", bus);
1486 	} else
1487 		dcmn_err(CE_NOTE, "enumerating pci bus 0x%x", bus);
1488 
1489 	if (config_op == CONFIG_NEW) {
1490 		devlist = (struct pci_devfunc *)pci_bus_res[bus].privdata;
1491 		while (devlist) {
1492 			entry = devlist;
1493 			devlist = entry->next;
1494 			if (entry->reprogram ||
1495 			    pci_bus_res[bus].io_reprogram ||
1496 			    pci_bus_res[bus].mem_reprogram) {
1497 				/* reprogram device(s) */
1498 				(void) add_reg_props(entry->dip, bus,
1499 				    entry->dev, entry->func, CONFIG_NEW, 0);
1500 			}
1501 			kmem_free(entry, sizeof (*entry));
1502 		}
1503 		pci_bus_res[bus].privdata = NULL;
1504 		return;
1505 	}
1506 
1507 	for (dev = 0; dev < max_dev_pci; dev++) {
1508 		nfunc = 1;
1509 		for (func = 0; func < nfunc; func++) {
1510 
1511 			dcmn_err(CE_NOTE, "probing dev 0x%x, func 0x%x",
1512 			    dev, func);
1513 
1514 			venid = pci_getw(bus, dev, func, PCI_CONF_VENID);
1515 
1516 			if ((venid == 0xffff) || (venid == 0)) {
1517 				/* no function at this address */
1518 				continue;
1519 			}
1520 
1521 			header = pci_getb(bus, dev, func, PCI_CONF_HEADER);
1522 			if (header == 0xff) {
1523 				continue; /* illegal value */
1524 			}
1525 
1526 			/*
1527 			 * according to some mail from Microsoft posted
1528 			 * to the pci-drivers alias, their only requirement
1529 			 * for a multifunction device is for the 1st
1530 			 * function to have to PCI_HEADER_MULTI bit set.
1531 			 */
1532 			if ((func == 0) && (header & PCI_HEADER_MULTI)) {
1533 				nfunc = 8;
1534 			}
1535 
1536 			if (config_op == CONFIG_FIX ||
1537 			    config_op == CONFIG_INFO) {
1538 				/*
1539 				 * Create the node, unconditionally, on the
1540 				 * first pass only.  It may still need
1541 				 * resource assignment, which will be
1542 				 * done on the second, CONFIG_NEW, pass.
1543 				 */
1544 				process_devfunc(bus, dev, func, header,
1545 				    venid, config_op);
1546 
1547 			}
1548 		}
1549 	}
1550 
1551 	/* percolate bus used resources up through parents to root */
1552 	if (config_op == CONFIG_INFO) {
1553 		int	par_bus;
1554 
1555 		par_bus = pci_bus_res[bus].par_bus;
1556 		while (par_bus != (uchar_t)-1) {
1557 			pci_bus_res[par_bus].io_size +=
1558 			    pci_bus_res[bus].io_size;
1559 			pci_bus_res[par_bus].mem_size +=
1560 			    pci_bus_res[bus].mem_size;
1561 
1562 			if (pci_bus_res[bus].io_used)
1563 				memlist_merge(&pci_bus_res[bus].io_used,
1564 				    &pci_bus_res[par_bus].io_used);
1565 
1566 			if (pci_bus_res[bus].mem_used)
1567 				memlist_merge(&pci_bus_res[bus].mem_used,
1568 				    &pci_bus_res[par_bus].mem_used);
1569 
1570 			if (pci_bus_res[bus].pmem_used)
1571 				memlist_merge(&pci_bus_res[bus].pmem_used,
1572 				    &pci_bus_res[par_bus].pmem_used);
1573 
1574 			bus = par_bus;
1575 			par_bus = pci_bus_res[par_bus].par_bus;
1576 		}
1577 	}
1578 }
1579 
1580 static int
1581 check_pciide_prop(uchar_t revid, ushort_t venid, ushort_t devid,
1582     ushort_t subvenid, ushort_t subdevid)
1583 {
1584 	static int prop_exist = -1;
1585 	static char *pciide_str;
1586 	char compat[32];
1587 
1588 	if (prop_exist == -1) {
1589 		prop_exist = (ddi_prop_lookup_string(DDI_DEV_T_ANY,
1590 		    ddi_root_node(), DDI_PROP_DONTPASS, "pci-ide",
1591 		    &pciide_str) == DDI_SUCCESS);
1592 	}
1593 
1594 	if (!prop_exist)
1595 		return (0);
1596 
1597 	/* compare property value against various forms of compatible */
1598 	if (subvenid) {
1599 		(void) snprintf(compat, sizeof (compat), "pci%x,%x.%x.%x.%x",
1600 		    venid, devid, subvenid, subdevid, revid);
1601 		if (strcmp(pciide_str, compat) == 0)
1602 			return (1);
1603 
1604 		(void) snprintf(compat, sizeof (compat), "pci%x,%x.%x.%x",
1605 		    venid, devid, subvenid, subdevid);
1606 		if (strcmp(pciide_str, compat) == 0)
1607 			return (1);
1608 
1609 		(void) snprintf(compat, sizeof (compat), "pci%x,%x",
1610 		    subvenid, subdevid);
1611 		if (strcmp(pciide_str, compat) == 0)
1612 			return (1);
1613 	}
1614 	(void) snprintf(compat, sizeof (compat), "pci%x,%x.%x",
1615 	    venid, devid, revid);
1616 	if (strcmp(pciide_str, compat) == 0)
1617 		return (1);
1618 
1619 	(void) snprintf(compat, sizeof (compat), "pci%x,%x", venid, devid);
1620 	if (strcmp(pciide_str, compat) == 0)
1621 		return (1);
1622 
1623 	return (0);
1624 }
1625 
1626 static int
1627 is_pciide(uchar_t basecl, uchar_t subcl, uchar_t revid,
1628     ushort_t venid, ushort_t devid, ushort_t subvenid, ushort_t subdevid)
1629 {
1630 	struct ide_table {	/* table for PCI_MASS_OTHER */
1631 		ushort_t venid;
1632 		ushort_t devid;
1633 	} *entry;
1634 
1635 	/* XXX SATA and other devices: need a way to add dynamically */
1636 	static struct ide_table ide_other[] = {
1637 		{0x1095, 0x3112},
1638 		{0x1095, 0x3114},
1639 		{0x1095, 0x3512},
1640 		{0x1095, 0x680},	/* Sil0680 */
1641 		{0x1283, 0x8211},	/* ITE 8211F is subcl PCI_MASS_OTHER */
1642 		{0, 0}
1643 	};
1644 
1645 	if (basecl != PCI_CLASS_MASS)
1646 		return (0);
1647 
1648 	if (subcl == PCI_MASS_IDE) {
1649 		return (1);
1650 	}
1651 
1652 	if (check_pciide_prop(revid, venid, devid, subvenid, subdevid))
1653 		return (1);
1654 
1655 	if (subcl != PCI_MASS_OTHER && subcl != PCI_MASS_SATA) {
1656 		return (0);
1657 	}
1658 
1659 	entry = &ide_other[0];
1660 	while (entry->venid) {
1661 		if (entry->venid == venid && entry->devid == devid)
1662 			return (1);
1663 		entry++;
1664 	}
1665 	return (0);
1666 }
1667 
1668 static int
1669 is_display(uint_t classcode)
1670 {
1671 	static uint_t disp_classes[] = {
1672 		0x000100,
1673 		0x030000,
1674 		0x030001
1675 	};
1676 	int i, nclasses = sizeof (disp_classes) / sizeof (uint_t);
1677 
1678 	for (i = 0; i < nclasses; i++) {
1679 		if (classcode == disp_classes[i])
1680 			return (1);
1681 	}
1682 	return (0);
1683 }
1684 
1685 static void
1686 add_undofix_entry(uint8_t bus, uint8_t dev, uint8_t fn,
1687     void (*undofn)(uint8_t, uint8_t, uint8_t))
1688 {
1689 	struct pci_fixundo *newundo;
1690 
1691 	newundo = kmem_alloc(sizeof (struct pci_fixundo), KM_SLEEP);
1692 
1693 	/*
1694 	 * Adding an item to this list means that we must turn its NMIENABLE
1695 	 * bit back on at a later time.
1696 	 */
1697 	newundo->bus = bus;
1698 	newundo->dev = dev;
1699 	newundo->fn = fn;
1700 	newundo->undofn = undofn;
1701 	newundo->next = undolist;
1702 
1703 	/* add to the undo list in LIFO order */
1704 	undolist = newundo;
1705 }
1706 
1707 void
1708 add_pci_fixes(void)
1709 {
1710 	int i;
1711 
1712 	for (i = 0; i <= pci_bios_nbus; i++) {
1713 		/*
1714 		 * For each bus, apply needed fixes to the appropriate devices.
1715 		 * This must be done before the main enumeration loop because
1716 		 * some fixes must be applied to devices normally encountered
1717 		 * later in the pci scan (e.g. if a fix to device 7 must be
1718 		 * applied before scanning device 6, applying fixes in the
1719 		 * normal enumeration loop would obviously be too late).
1720 		 */
1721 		enumerate_bus_devs(i, CONFIG_FIX);
1722 	}
1723 }
1724 
1725 void
1726 undo_pci_fixes(void)
1727 {
1728 	struct pci_fixundo *nextundo;
1729 	uint8_t bus, dev, fn;
1730 
1731 	/*
1732 	 * All fixes in the undo list are performed unconditionally.  Future
1733 	 * fixes may require selective undo.
1734 	 */
1735 	while (undolist != NULL) {
1736 
1737 		bus = undolist->bus;
1738 		dev = undolist->dev;
1739 		fn = undolist->fn;
1740 
1741 		(*(undolist->undofn))(bus, dev, fn);
1742 
1743 		nextundo = undolist->next;
1744 		kmem_free(undolist, sizeof (struct pci_fixundo));
1745 		undolist = nextundo;
1746 	}
1747 }
1748 
1749 static void
1750 undo_amd8111_pci_fix(uint8_t bus, uint8_t dev, uint8_t fn)
1751 {
1752 	uint8_t val8;
1753 
1754 	val8 = pci_getb(bus, dev, fn, LPC_IO_CONTROL_REG_1);
1755 	/*
1756 	 * The NMIONERR bit is turned back on to allow the SMM BIOS
1757 	 * to handle more critical PCI errors (e.g. PERR#).
1758 	 */
1759 	val8 |= AMD8111_ENABLENMI;
1760 	pci_putb(bus, dev, fn, LPC_IO_CONTROL_REG_1, val8);
1761 }
1762 
1763 static void
1764 pci_fix_amd8111(uint8_t bus, uint8_t dev, uint8_t fn)
1765 {
1766 	uint8_t val8;
1767 
1768 	val8 = pci_getb(bus, dev, fn, LPC_IO_CONTROL_REG_1);
1769 
1770 	if ((val8 & AMD8111_ENABLENMI) == 0)
1771 		return;
1772 
1773 	/*
1774 	 * We reset NMIONERR in the LPC because master-abort on the PCI
1775 	 * bridge side of the 8111 will cause NMI, which might cause SMI,
1776 	 * which sometimes prevents all devices from being enumerated.
1777 	 */
1778 	val8 &= ~AMD8111_ENABLENMI;
1779 
1780 	pci_putb(bus, dev, fn, LPC_IO_CONTROL_REG_1, val8);
1781 
1782 	add_undofix_entry(bus, dev, fn, undo_amd8111_pci_fix);
1783 }
1784 
1785 static void
1786 set_devpm_d0(uchar_t bus, uchar_t dev, uchar_t func)
1787 {
1788 	uint16_t status;
1789 	uint8_t header;
1790 	uint8_t cap_ptr;
1791 	uint8_t cap_id;
1792 	uint16_t pmcsr;
1793 
1794 	status = pci_getw(bus, dev, func, PCI_CONF_STAT);
1795 	if (!(status & PCI_STAT_CAP))
1796 		return;	/* No capabilities list */
1797 
1798 	header = pci_getb(bus, dev, func, PCI_CONF_HEADER) & PCI_HEADER_TYPE_M;
1799 	if (header == PCI_HEADER_CARDBUS)
1800 		cap_ptr = pci_getb(bus, dev, func, PCI_CBUS_CAP_PTR);
1801 	else
1802 		cap_ptr = pci_getb(bus, dev, func, PCI_CONF_CAP_PTR);
1803 	/*
1804 	 * Walk the capabilities list searching for a PM entry.
1805 	 */
1806 	while (cap_ptr != PCI_CAP_NEXT_PTR_NULL && cap_ptr >= PCI_CAP_PTR_OFF) {
1807 		cap_ptr &= PCI_CAP_PTR_MASK;
1808 		cap_id = pci_getb(bus, dev, func, cap_ptr + PCI_CAP_ID);
1809 		if (cap_id == PCI_CAP_ID_PM) {
1810 			pmcsr = pci_getw(bus, dev, func, cap_ptr + PCI_PMCSR);
1811 			pmcsr &= ~(PCI_PMCSR_STATE_MASK);
1812 			pmcsr |= PCI_PMCSR_D0; /* D0 state */
1813 			pci_putw(bus, dev, func, cap_ptr + PCI_PMCSR, pmcsr);
1814 			break;
1815 		}
1816 		cap_ptr = pci_getb(bus, dev, func, cap_ptr + PCI_CAP_NEXT_PTR);
1817 	}
1818 
1819 }
1820 
1821 #define	is_isa(bc, sc)	\
1822 	(((bc) == PCI_CLASS_BRIDGE) && ((sc) == PCI_BRIDGE_ISA))
1823 
1824 static void
1825 process_devfunc(uchar_t bus, uchar_t dev, uchar_t func, uchar_t header,
1826     ushort_t vendorid, int config_op)
1827 {
1828 	char nodename[32], unitaddr[5];
1829 	dev_info_t *dip;
1830 	uchar_t basecl, subcl, progcl, intr, revid;
1831 	ushort_t subvenid, subdevid, status;
1832 	ushort_t slot_num;
1833 	uint_t classcode, revclass;
1834 	int reprogram = 0, pciide = 0;
1835 	int power[2] = {1, 1};
1836 	int pciex = 0;
1837 	ushort_t is_pci_bridge = 0;
1838 	struct pci_devfunc *devlist = NULL, *entry = NULL;
1839 	gfx_entry_t *gfxp;
1840 
1841 	ushort_t deviceid = pci_getw(bus, dev, func, PCI_CONF_DEVID);
1842 
1843 	switch (header & PCI_HEADER_TYPE_M) {
1844 	case PCI_HEADER_ZERO:
1845 		subvenid = pci_getw(bus, dev, func, PCI_CONF_SUBVENID);
1846 		subdevid = pci_getw(bus, dev, func, PCI_CONF_SUBSYSID);
1847 		break;
1848 	case PCI_HEADER_CARDBUS:
1849 		subvenid = pci_getw(bus, dev, func, PCI_CBUS_SUBVENID);
1850 		subdevid = pci_getw(bus, dev, func, PCI_CBUS_SUBSYSID);
1851 		/* Record the # of cardbus bridges found on the bus */
1852 		if (config_op == CONFIG_INFO)
1853 			pci_bus_res[bus].num_cbb++;
1854 		break;
1855 	default:
1856 		subvenid = 0;
1857 		subdevid = 0;
1858 		break;
1859 	}
1860 
1861 	if (config_op == CONFIG_FIX) {
1862 		if (vendorid == VENID_AMD && deviceid == DEVID_AMD8111_LPC) {
1863 			pci_fix_amd8111(bus, dev, func);
1864 		}
1865 		return;
1866 	}
1867 
1868 	/* XXX should be use generic names? derive from class? */
1869 	revclass = pci_getl(bus, dev, func, PCI_CONF_REVID);
1870 	classcode = revclass >> 8;
1871 	revid = revclass & 0xff;
1872 
1873 	/* figure out if this is pci-ide */
1874 	basecl = classcode >> 16;
1875 	subcl = (classcode >> 8) & 0xff;
1876 	progcl = classcode & 0xff;
1877 
1878 
1879 	if (is_display(classcode))
1880 		(void) snprintf(nodename, sizeof (nodename), "display");
1881 	else if (!pseudo_isa && is_isa(basecl, subcl))
1882 		(void) snprintf(nodename, sizeof (nodename), "isa");
1883 	else if (subvenid != 0)
1884 		(void) snprintf(nodename, sizeof (nodename),
1885 		    "pci%x,%x", subvenid, subdevid);
1886 	else
1887 		(void) snprintf(nodename, sizeof (nodename),
1888 		    "pci%x,%x", vendorid, deviceid);
1889 
1890 	/* make sure parent bus dip has been created */
1891 	if (pci_bus_res[bus].dip == NULL)
1892 		create_root_bus_dip(bus);
1893 
1894 	ndi_devi_alloc_sleep(pci_bus_res[bus].dip, nodename,
1895 	    DEVI_SID_NODEID, &dip);
1896 
1897 	if (check_if_device_is_pciex(dip, bus, dev, func, &slot_num,
1898 	    &is_pci_bridge) == B_TRUE)
1899 		pciex = 1;
1900 
1901 	/* add properties */
1902 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, "device-id", deviceid);
1903 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, "vendor-id", vendorid);
1904 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, "revision-id", revid);
1905 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1906 	    "class-code", classcode);
1907 	if (func == 0)
1908 		(void) snprintf(unitaddr, sizeof (unitaddr), "%x", dev);
1909 	else
1910 		(void) snprintf(unitaddr, sizeof (unitaddr),
1911 		    "%x,%x", dev, func);
1912 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1913 	    "unit-address", unitaddr);
1914 
1915 	/* add device_type for display nodes */
1916 	if (is_display(classcode)) {
1917 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1918 		    "device_type", "display");
1919 	}
1920 	/* add special stuff for header type */
1921 	if ((header & PCI_HEADER_TYPE_M) == PCI_HEADER_ZERO) {
1922 		uchar_t mingrant = pci_getb(bus, dev, func, PCI_CONF_MIN_G);
1923 		uchar_t maxlatency = pci_getb(bus, dev, func, PCI_CONF_MAX_L);
1924 
1925 		if (subvenid != 0) {
1926 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1927 			    "subsystem-id", subdevid);
1928 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1929 			    "subsystem-vendor-id", subvenid);
1930 		}
1931 		if (!pciex)
1932 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1933 			    "min-grant", mingrant);
1934 		if (!pciex)
1935 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1936 			    "max-latency", maxlatency);
1937 	}
1938 
1939 	/* interrupt, record if not 0 */
1940 	intr = pci_getb(bus, dev, func, PCI_CONF_IPIN);
1941 	if (intr != 0)
1942 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1943 		    "interrupts", intr);
1944 
1945 	/*
1946 	 * Add support for 133 mhz pci eventually
1947 	 */
1948 	status = pci_getw(bus, dev, func, PCI_CONF_STAT);
1949 
1950 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1951 	    "devsel-speed", (status & PCI_STAT_DEVSELT) >> 9);
1952 	if (!pciex && (status & PCI_STAT_FBBC))
1953 		(void) ndi_prop_create_boolean(DDI_DEV_T_NONE, dip,
1954 		    "fast-back-to-back");
1955 	if (!pciex && (status & PCI_STAT_66MHZ))
1956 		(void) ndi_prop_create_boolean(DDI_DEV_T_NONE, dip,
1957 		    "66mhz-capable");
1958 	if (status & PCI_STAT_UDF)
1959 		(void) ndi_prop_create_boolean(DDI_DEV_T_NONE, dip,
1960 		    "udf-supported");
1961 	if (pciex && slot_num) {
1962 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1963 		    "physical-slot#", slot_num);
1964 		if (!is_pci_bridge)
1965 			pciex_slot_names_prop(dip, slot_num);
1966 	}
1967 
1968 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
1969 	    "power-consumption", power, 2);
1970 
1971 	/* Set the device PM state to D0 */
1972 	set_devpm_d0(bus, dev, func);
1973 
1974 	if ((basecl == PCI_CLASS_BRIDGE) && (subcl == PCI_BRIDGE_PCI))
1975 		add_ppb_props(dip, bus, dev, func, pciex, is_pci_bridge);
1976 	else {
1977 		/*
1978 		 * Record the non-PPB devices on the bus for possible
1979 		 * reprogramming at 2nd bus enumeration.
1980 		 * Note: PPB reprogramming is done in fix_ppb_res()
1981 		 */
1982 		devlist = (struct pci_devfunc *)pci_bus_res[bus].privdata;
1983 		entry = kmem_zalloc(sizeof (*entry), KM_SLEEP);
1984 		entry->dip = dip;
1985 		entry->dev = dev;
1986 		entry->func = func;
1987 		entry->next = devlist;
1988 		pci_bus_res[bus].privdata = entry;
1989 	}
1990 
1991 	if (config_op == CONFIG_INFO &&
1992 	    IS_CLASS_IOAPIC(basecl, subcl, progcl)) {
1993 		create_ioapic_node(bus, dev, func, vendorid, deviceid);
1994 	}
1995 
1996 	/* check for ck8-04 based PCI ISA bridge only */
1997 	if (NVIDIA_IS_LPC_BRIDGE(vendorid, deviceid) && (dev == 1) &&
1998 	    (func == 0))
1999 		add_nvidia_isa_bridge_props(dip, bus, dev, func);
2000 
2001 	if (pciex && is_pci_bridge)
2002 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, "model",
2003 		    (char *)"PCIe-PCI bridge");
2004 	else
2005 		add_model_prop(dip, classcode);
2006 
2007 	add_compatible(dip, subvenid, subdevid, vendorid, deviceid,
2008 	    revid, classcode, pciex);
2009 
2010 	/*
2011 	 * See if this device is a controller that advertises
2012 	 * itself to be a standard ATA task file controller, or one that
2013 	 * has been hard coded.
2014 	 *
2015 	 * If it is, check if any other higher precedence driver listed in
2016 	 * driver_aliases will claim the node by calling
2017 	 * ddi_compatibile_driver_major.  If so, clear pciide and do not
2018 	 * create a pci-ide node or any other special handling.
2019 	 *
2020 	 * If another driver does not bind, set the node name to pci-ide
2021 	 * and then let the special pci-ide handling for registers and
2022 	 * child pci-ide nodes proceed below.
2023 	 */
2024 	if (is_pciide(basecl, subcl, revid, vendorid, deviceid,
2025 	    subvenid, subdevid) == 1) {
2026 		if (ddi_compatible_driver_major(dip, NULL) == (major_t)-1) {
2027 			(void) ndi_devi_set_nodename(dip, "pci-ide", 0);
2028 			pciide = 1;
2029 		}
2030 	}
2031 
2032 	reprogram = add_reg_props(dip, bus, dev, func, config_op, pciide);
2033 	(void) ndi_devi_bind_driver(dip, 0);
2034 
2035 	/* special handling for pci-ide */
2036 	if (pciide) {
2037 		dev_info_t *cdip;
2038 
2039 		/*
2040 		 * Create properties specified by P1275 Working Group
2041 		 * Proposal #414 Version 1
2042 		 */
2043 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
2044 		    "device_type", "pci-ide");
2045 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
2046 		    "#address-cells", 1);
2047 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
2048 		    "#size-cells", 0);
2049 
2050 		/* allocate two child nodes */
2051 		ndi_devi_alloc_sleep(dip, "ide",
2052 		    (pnode_t)DEVI_SID_NODEID, &cdip);
2053 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cdip,
2054 		    "reg", 0);
2055 		(void) ndi_devi_bind_driver(cdip, 0);
2056 		ndi_devi_alloc_sleep(dip, "ide",
2057 		    (pnode_t)DEVI_SID_NODEID, &cdip);
2058 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cdip,
2059 		    "reg", 1);
2060 		(void) ndi_devi_bind_driver(cdip, 0);
2061 
2062 		reprogram = 0;	/* don't reprogram pci-ide bridge */
2063 	}
2064 
2065 	if (is_display(classcode)) {
2066 		gfxp = kmem_zalloc(sizeof (*gfxp), KM_SLEEP);
2067 		gfxp->g_dip = dip;
2068 		gfxp->g_prev = NULL;
2069 		gfxp->g_next = gfx_devinfo_list;
2070 		gfx_devinfo_list = gfxp;
2071 		if (gfxp->g_next)
2072 			gfxp->g_next->g_prev = gfxp;
2073 	}
2074 
2075 	/* special handling for isa */
2076 	if (!pseudo_isa && is_isa(basecl, subcl)) {
2077 		/* add device_type */
2078 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
2079 		    "device_type", "isa");
2080 	}
2081 
2082 	if (reprogram && (entry != NULL))
2083 		entry->reprogram = B_TRUE;
2084 
2085 }
2086 
2087 /*
2088  * Some vendors do not use unique subsystem IDs in their products, which
2089  * makes the use of form 2 compatible names (pciSSSS,ssss) inappropriate.
2090  * Allow for these compatible forms to be excluded on a per-device basis.
2091  */
2092 /*ARGSUSED*/
2093 static boolean_t
2094 subsys_compat_exclude(ushort_t venid, ushort_t devid, ushort_t subvenid,
2095     ushort_t subdevid, uchar_t revid, uint_t classcode)
2096 {
2097 	/* Nvidia display adapters */
2098 	if ((venid == 0x10de) && (is_display(classcode)))
2099 		return (B_TRUE);
2100 
2101 	return (B_FALSE);
2102 }
2103 
2104 /*
2105  * Set the compatible property to a value compliant with
2106  * rev 2.1 of the IEEE1275 PCI binding.
2107  * (Also used for PCI-Express devices).
2108  *
2109  *   pciVVVV,DDDD.SSSS.ssss.RR	(0)
2110  *   pciVVVV,DDDD.SSSS.ssss	(1)
2111  *   pciSSSS,ssss		(2)
2112  *   pciVVVV,DDDD.RR		(3)
2113  *   pciVVVV,DDDD		(4)
2114  *   pciclass,CCSSPP		(5)
2115  *   pciclass,CCSS		(6)
2116  *
2117  * The Subsystem (SSSS) forms are not inserted if
2118  * subsystem-vendor-id is 0.
2119  *
2120  * NOTE: For PCI-Express devices "pci" is replaced with "pciex" in 0-6 above
2121  * property 2 is not created as per "1275 bindings for PCI Express Interconnect"
2122  *
2123  * Set with setprop and \x00 between each
2124  * to generate the encoded string array form.
2125  */
2126 void
2127 add_compatible(dev_info_t *dip, ushort_t subvenid, ushort_t subdevid,
2128     ushort_t vendorid, ushort_t deviceid, uchar_t revid, uint_t classcode,
2129     int pciex)
2130 {
2131 	int i = 0;
2132 	int size = COMPAT_BUFSIZE;
2133 	char *compat[13];
2134 	char *buf, *curr;
2135 
2136 	curr = buf = kmem_alloc(size, KM_SLEEP);
2137 
2138 	if (pciex) {
2139 		if (subvenid) {
2140 			compat[i++] = curr;	/* form 0 */
2141 			(void) snprintf(curr, size, "pciex%x,%x.%x.%x.%x",
2142 			    vendorid, deviceid, subvenid, subdevid, revid);
2143 			size -= strlen(curr) + 1;
2144 			curr += strlen(curr) + 1;
2145 
2146 			compat[i++] = curr;	/* form 1 */
2147 			(void) snprintf(curr, size, "pciex%x,%x.%x.%x",
2148 			    vendorid, deviceid, subvenid, subdevid);
2149 			size -= strlen(curr) + 1;
2150 			curr += strlen(curr) + 1;
2151 
2152 		}
2153 		compat[i++] = curr;	/* form 3 */
2154 		(void) snprintf(curr, size, "pciex%x,%x.%x",
2155 		    vendorid, deviceid, revid);
2156 		size -= strlen(curr) + 1;
2157 		curr += strlen(curr) + 1;
2158 
2159 		compat[i++] = curr;	/* form 4 */
2160 		(void) snprintf(curr, size, "pciex%x,%x", vendorid, deviceid);
2161 		size -= strlen(curr) + 1;
2162 		curr += strlen(curr) + 1;
2163 
2164 		compat[i++] = curr;	/* form 5 */
2165 		(void) snprintf(curr, size, "pciexclass,%06x", classcode);
2166 		size -= strlen(curr) + 1;
2167 		curr += strlen(curr) + 1;
2168 
2169 		compat[i++] = curr;	/* form 6 */
2170 		(void) snprintf(curr, size, "pciexclass,%04x",
2171 		    (classcode >> 8));
2172 		size -= strlen(curr) + 1;
2173 		curr += strlen(curr) + 1;
2174 	}
2175 
2176 	if (subvenid) {
2177 		compat[i++] = curr;	/* form 0 */
2178 		(void) snprintf(curr, size, "pci%x,%x.%x.%x.%x",
2179 		    vendorid, deviceid, subvenid, subdevid, revid);
2180 		size -= strlen(curr) + 1;
2181 		curr += strlen(curr) + 1;
2182 
2183 		compat[i++] = curr;	/* form 1 */
2184 		(void) snprintf(curr, size, "pci%x,%x.%x.%x",
2185 		    vendorid, deviceid, subvenid, subdevid);
2186 		size -= strlen(curr) + 1;
2187 		curr += strlen(curr) + 1;
2188 
2189 		if (subsys_compat_exclude(vendorid, deviceid, subvenid,
2190 		    subdevid, revid, classcode) == B_FALSE) {
2191 			compat[i++] = curr;	/* form 2 */
2192 			(void) snprintf(curr, size, "pci%x,%x", subvenid,
2193 			    subdevid);
2194 			size -= strlen(curr) + 1;
2195 			curr += strlen(curr) + 1;
2196 		}
2197 	}
2198 	compat[i++] = curr;	/* form 3 */
2199 	(void) snprintf(curr, size, "pci%x,%x.%x", vendorid, deviceid, revid);
2200 	size -= strlen(curr) + 1;
2201 	curr += strlen(curr) + 1;
2202 
2203 	compat[i++] = curr;	/* form 4 */
2204 	(void) snprintf(curr, size, "pci%x,%x", vendorid, deviceid);
2205 	size -= strlen(curr) + 1;
2206 	curr += strlen(curr) + 1;
2207 
2208 	compat[i++] = curr;	/* form 5 */
2209 	(void) snprintf(curr, size, "pciclass,%06x", classcode);
2210 	size -= strlen(curr) + 1;
2211 	curr += strlen(curr) + 1;
2212 
2213 	compat[i++] = curr;	/* form 6 */
2214 	(void) snprintf(curr, size, "pciclass,%04x", (classcode >> 8));
2215 	size -= strlen(curr) + 1;
2216 	curr += strlen(curr) + 1;
2217 
2218 	(void) ndi_prop_update_string_array(DDI_DEV_T_NONE, dip,
2219 	    "compatible", compat, i);
2220 	kmem_free(buf, COMPAT_BUFSIZE);
2221 }
2222 
2223 /*
2224  * Adjust the reg properties for a dual channel PCI-IDE device.
2225  *
2226  * NOTE: don't do anything that changes the order of the hard-decodes
2227  * and programmed BARs. The kernel driver depends on these values
2228  * being in this order regardless of whether they're for a 'native'
2229  * mode BAR or not.
2230  */
2231 /*
2232  * config info for pci-ide devices
2233  */
2234 static struct {
2235 	uchar_t  native_mask;	/* 0 == 'compatibility' mode, 1 == native */
2236 	uchar_t  bar_offset;	/* offset for alt status register */
2237 	ushort_t addr;		/* compatibility mode base address */
2238 	ushort_t length;	/* number of ports for this BAR */
2239 } pciide_bar[] = {
2240 	{ 0x01, 0, 0x1f0, 8 },	/* primary lower BAR */
2241 	{ 0x01, 2, 0x3f6, 1 },	/* primary upper BAR */
2242 	{ 0x04, 0, 0x170, 8 },	/* secondary lower BAR */
2243 	{ 0x04, 2, 0x376, 1 }	/* secondary upper BAR */
2244 };
2245 
2246 static int
2247 pciIdeAdjustBAR(uchar_t progcl, int index, uint_t *basep, uint_t *lenp)
2248 {
2249 	int hard_decode = 0;
2250 
2251 	/*
2252 	 * Adjust the base and len for the BARs of the PCI-IDE
2253 	 * device's primary and secondary controllers. The first
2254 	 * two BARs are for the primary controller and the next
2255 	 * two BARs are for the secondary controller. The fifth
2256 	 * and sixth bars are never adjusted.
2257 	 */
2258 	if (index >= 0 && index <= 3) {
2259 		*lenp = pciide_bar[index].length;
2260 
2261 		if (progcl & pciide_bar[index].native_mask) {
2262 			*basep += pciide_bar[index].bar_offset;
2263 		} else {
2264 			*basep = pciide_bar[index].addr;
2265 			hard_decode = 1;
2266 		}
2267 	}
2268 
2269 	/*
2270 	 * if either base or len is zero make certain both are zero
2271 	 */
2272 	if (*basep == 0 || *lenp == 0) {
2273 		*basep = 0;
2274 		*lenp = 0;
2275 		hard_decode = 0;
2276 	}
2277 
2278 	return (hard_decode);
2279 }
2280 
2281 
2282 /*
2283  * Add the "reg" and "assigned-addresses" property
2284  */
2285 static int
2286 add_reg_props(dev_info_t *dip, uchar_t bus, uchar_t dev, uchar_t func,
2287     int config_op, int pciide)
2288 {
2289 	uchar_t baseclass, subclass, progclass, header;
2290 	ushort_t bar_sz;
2291 	uint_t value = 0, len, devloc;
2292 	uint_t base, base_hi, type;
2293 	ushort_t offset, end;
2294 	int max_basereg, j, reprogram = 0;
2295 	uint_t phys_hi;
2296 	struct memlist **io_avail, **io_used;
2297 	struct memlist **mem_avail, **mem_used;
2298 	struct memlist **pmem_avail, **pmem_used;
2299 	uchar_t res_bus;
2300 
2301 	pci_regspec_t regs[16] = {{0}};
2302 	pci_regspec_t assigned[15] = {{0}};
2303 	int nreg, nasgn;
2304 
2305 	io_avail = &pci_bus_res[bus].io_avail;
2306 	io_used = &pci_bus_res[bus].io_used;
2307 	mem_avail = &pci_bus_res[bus].mem_avail;
2308 	mem_used = &pci_bus_res[bus].mem_used;
2309 	pmem_avail = &pci_bus_res[bus].pmem_avail;
2310 	pmem_used = &pci_bus_res[bus].pmem_used;
2311 
2312 	devloc = (uint_t)bus << 16 | (uint_t)dev << 11 | (uint_t)func << 8;
2313 	regs[0].pci_phys_hi = devloc;
2314 	nreg = 1;	/* rest of regs[0] is all zero */
2315 	nasgn = 0;
2316 
2317 	baseclass = pci_getb(bus, dev, func, PCI_CONF_BASCLASS);
2318 	subclass = pci_getb(bus, dev, func, PCI_CONF_SUBCLASS);
2319 	progclass = pci_getb(bus, dev, func, PCI_CONF_PROGCLASS);
2320 	header = pci_getb(bus, dev, func, PCI_CONF_HEADER) & PCI_HEADER_TYPE_M;
2321 
2322 	switch (header) {
2323 	case PCI_HEADER_ZERO:
2324 		max_basereg = PCI_BASE_NUM;
2325 		break;
2326 	case PCI_HEADER_PPB:
2327 		max_basereg = PCI_BCNF_BASE_NUM;
2328 		break;
2329 	case PCI_HEADER_CARDBUS:
2330 		max_basereg = PCI_CBUS_BASE_NUM;
2331 		reprogram = 1;
2332 		break;
2333 	default:
2334 		max_basereg = 0;
2335 		break;
2336 	}
2337 
2338 	/*
2339 	 * Create the register property by saving the current
2340 	 * value of the base register. Write 0xffffffff to the
2341 	 * base register.  Read the value back to determine the
2342 	 * required size of the address space.  Restore the base
2343 	 * register contents.
2344 	 *
2345 	 * Do not disable I/O and memory access; this isn't necessary
2346 	 * since no driver is yet attached to this device, and disabling
2347 	 * I/O and memory access has the side-effect of disabling PCI-PCI
2348 	 * bridge mappings, which makes the bridge transparent to secondary-
2349 	 * bus activity (see sections 4.1-4.3 of the PCI-PCI Bridge
2350 	 * Spec V1.2).
2351 	 */
2352 	end = PCI_CONF_BASE0 + max_basereg * sizeof (uint_t);
2353 	for (j = 0, offset = PCI_CONF_BASE0; offset < end;
2354 	    j++, offset += bar_sz) {
2355 		/* determine the size of the address space */
2356 		base = pci_getl(bus, dev, func, offset);
2357 		pci_putl(bus, dev, func, offset, 0xffffffff);
2358 		value = pci_getl(bus, dev, func, offset);
2359 		pci_putl(bus, dev, func, offset, base);
2360 
2361 		/* construct phys hi,med.lo, size hi, lo */
2362 		if ((pciide && j < 4) || (base & PCI_BASE_SPACE_IO)) {
2363 			int hard_decode = 0;
2364 
2365 			/* i/o space */
2366 			bar_sz = PCI_BAR_SZ_32;
2367 			value &= PCI_BASE_IO_ADDR_M;
2368 			len = ((value ^ (value-1)) + 1) >> 1;
2369 
2370 			/* XXX Adjust first 4 IDE registers */
2371 			if (pciide) {
2372 				if (subclass != PCI_MASS_IDE)
2373 					progclass = (PCI_IDE_IF_NATIVE_PRI |
2374 					    PCI_IDE_IF_NATIVE_SEC);
2375 				hard_decode = pciIdeAdjustBAR(progclass, j,
2376 				    &base, &len);
2377 			} else if (value == 0) {
2378 				/* skip base regs with size of 0 */
2379 				continue;
2380 			}
2381 
2382 			regs[nreg].pci_phys_hi = PCI_ADDR_IO | devloc |
2383 			    (hard_decode ? PCI_RELOCAT_B : offset);
2384 			regs[nreg].pci_phys_low = hard_decode ?
2385 			    base & PCI_BASE_IO_ADDR_M : 0;
2386 			assigned[nasgn].pci_phys_hi =
2387 			    PCI_RELOCAT_B | regs[nreg].pci_phys_hi;
2388 			regs[nreg].pci_size_low =
2389 			    assigned[nasgn].pci_size_low = len;
2390 			type = base & (~PCI_BASE_IO_ADDR_M);
2391 			base &= PCI_BASE_IO_ADDR_M;
2392 			/*
2393 			 * A device under a subtractive PPB can allocate
2394 			 * resources from its parent bus if there is no resource
2395 			 * available on its own bus.
2396 			 */
2397 			if ((config_op == CONFIG_NEW) && (*io_avail == NULL)) {
2398 				res_bus = bus;
2399 				while (pci_bus_res[res_bus].subtractive) {
2400 					res_bus = pci_bus_res[res_bus].par_bus;
2401 					if (res_bus == (uchar_t)-1)
2402 						break; /* root bus already */
2403 					if (pci_bus_res[res_bus].io_avail) {
2404 						io_avail = &pci_bus_res
2405 						    [res_bus].io_avail;
2406 						break;
2407 					}
2408 				}
2409 			}
2410 
2411 			/*
2412 			 * first pass - gather what's there
2413 			 * update/second pass - adjust/allocate regions
2414 			 *	config - allocate regions
2415 			 */
2416 			if (config_op == CONFIG_INFO) {	/* first pass */
2417 				/* take out of the resource map of the bus */
2418 				if (base != 0) {
2419 					(void) memlist_remove(io_avail, base,
2420 					    len);
2421 					memlist_insert(io_used, base, len);
2422 				} else {
2423 					reprogram = 1;
2424 				}
2425 				pci_bus_res[bus].io_size += len;
2426 			} else if ((*io_avail && base == 0) ||
2427 			    pci_bus_res[bus].io_reprogram) {
2428 				base = (uint_t)memlist_find(io_avail, len, len);
2429 				if (base != 0) {
2430 					memlist_insert(io_used, base, len);
2431 					/* XXX need to worry about 64-bit? */
2432 					pci_putl(bus, dev, func, offset,
2433 					    base | type);
2434 					base = pci_getl(bus, dev, func, offset);
2435 					base &= PCI_BASE_IO_ADDR_M;
2436 				}
2437 				if (base == 0) {
2438 					cmn_err(CE_WARN, "failed to program"
2439 					    " IO space [%d/%d/%d] BAR@0x%x"
2440 					    " length 0x%x",
2441 					    bus, dev, func, offset, len);
2442 				}
2443 			}
2444 			assigned[nasgn].pci_phys_low = base;
2445 			nreg++, nasgn++;
2446 
2447 		} else {
2448 			/* memory space */
2449 			if ((base & PCI_BASE_TYPE_M) == PCI_BASE_TYPE_ALL) {
2450 				bar_sz = PCI_BAR_SZ_64;
2451 				base_hi = pci_getl(bus, dev, func, offset + 4);
2452 				phys_hi = PCI_ADDR_MEM64;
2453 			} else {
2454 				bar_sz = PCI_BAR_SZ_32;
2455 				base_hi = 0;
2456 				phys_hi = PCI_ADDR_MEM32;
2457 			}
2458 
2459 			/* skip base regs with size of 0 */
2460 			value &= PCI_BASE_M_ADDR_M;
2461 
2462 			if (value == 0)
2463 				continue;
2464 
2465 			len = ((value ^ (value-1)) + 1) >> 1;
2466 			regs[nreg].pci_size_low =
2467 			    assigned[nasgn].pci_size_low = len;
2468 
2469 			phys_hi |= (devloc | offset);
2470 			if (base & PCI_BASE_PREF_M)
2471 				phys_hi |= PCI_PREFETCH_B;
2472 
2473 			/*
2474 			 * A device under a subtractive PPB can allocate
2475 			 * resources from its parent bus if there is no resource
2476 			 * available on its own bus.
2477 			 */
2478 			if ((config_op == CONFIG_NEW) && (*mem_avail == NULL)) {
2479 				res_bus = bus;
2480 				while (pci_bus_res[res_bus].subtractive) {
2481 					res_bus = pci_bus_res[res_bus].par_bus;
2482 					if (res_bus == (uchar_t)-1)
2483 						break; /* root bus already */
2484 					mem_avail =
2485 					    &pci_bus_res[res_bus].mem_avail;
2486 					pmem_avail =
2487 					    &pci_bus_res [res_bus].pmem_avail;
2488 					/*
2489 					 * Break out as long as at least
2490 					 * mem_avail is available
2491 					 */
2492 					if ((*pmem_avail &&
2493 					    (phys_hi & PCI_PREFETCH_B)) ||
2494 					    *mem_avail)
2495 						break;
2496 				}
2497 			}
2498 
2499 			regs[nreg].pci_phys_hi =
2500 			    assigned[nasgn].pci_phys_hi = phys_hi;
2501 			assigned[nasgn].pci_phys_hi |= PCI_RELOCAT_B;
2502 			assigned[nasgn].pci_phys_mid = base_hi;
2503 			type = base & ~PCI_BASE_M_ADDR_M;
2504 			base &= PCI_BASE_M_ADDR_M;
2505 
2506 			if (config_op == CONFIG_INFO) {
2507 				/* take out of the resource map of the bus */
2508 				if (base != NULL) {
2509 					/* remove from PMEM and MEM space */
2510 					(void) memlist_remove(mem_avail,
2511 					    base, len);
2512 					(void) memlist_remove(pmem_avail,
2513 					    base, len);
2514 					/* only note as used in correct map */
2515 					if (phys_hi & PCI_PREFETCH_B)
2516 						memlist_insert(pmem_used,
2517 						    base, len);
2518 					else
2519 						memlist_insert(mem_used,
2520 						    base, len);
2521 				} else {
2522 					reprogram = 1;
2523 				}
2524 				pci_bus_res[bus].mem_size += len;
2525 			} else if ((*mem_avail && base == NULL) ||
2526 			    pci_bus_res[bus].mem_reprogram) {
2527 				/*
2528 				 * When desired, attempt a prefetchable
2529 				 * allocation first
2530 				 */
2531 				if (phys_hi & PCI_PREFETCH_B) {
2532 					base = (uint_t)memlist_find(pmem_avail,
2533 					    len, len);
2534 					if (base != NULL) {
2535 						memlist_insert(pmem_used,
2536 						    base, len);
2537 						(void) memlist_remove(mem_avail,
2538 						    base, len);
2539 					}
2540 				}
2541 				/*
2542 				 * If prefetchable allocation was not
2543 				 * desired, or failed, attempt ordinary
2544 				 * memory allocation
2545 				 */
2546 				if (base == NULL) {
2547 					base = (uint_t)memlist_find(mem_avail,
2548 					    len, len);
2549 					if (base != NULL) {
2550 						memlist_insert(mem_used,
2551 						    base, len);
2552 						(void) memlist_remove(
2553 						    pmem_avail, base, len);
2554 					}
2555 				}
2556 				if (base != NULL) {
2557 					pci_putl(bus, dev, func, offset,
2558 					    base | type);
2559 					base = pci_getl(bus, dev, func, offset);
2560 					base &= PCI_BASE_M_ADDR_M;
2561 				} else
2562 					cmn_err(CE_WARN, "failed to program "
2563 					    "mem space [%d/%d/%d] BAR@0x%x"
2564 					    " length 0x%x",
2565 					    bus, dev, func, offset, len);
2566 			}
2567 			assigned[nasgn].pci_phys_low = base;
2568 			nreg++, nasgn++;
2569 		}
2570 	}
2571 	switch (header) {
2572 	case PCI_HEADER_ZERO:
2573 		offset = PCI_CONF_ROM;
2574 		break;
2575 	case PCI_HEADER_PPB:
2576 		offset = PCI_BCNF_ROM;
2577 		break;
2578 	default: /* including PCI_HEADER_CARDBUS */
2579 		goto done;
2580 	}
2581 
2582 	/*
2583 	 * Add the expansion rom memory space
2584 	 * Determine the size of the ROM base reg; don't write reserved bits
2585 	 * ROM isn't in the PCI memory space.
2586 	 */
2587 	base = pci_getl(bus, dev, func, offset);
2588 	pci_putl(bus, dev, func, offset, PCI_BASE_ROM_ADDR_M);
2589 	value = pci_getl(bus, dev, func, offset);
2590 	pci_putl(bus, dev, func, offset, base);
2591 	if (value & PCI_BASE_ROM_ENABLE)
2592 		value &= PCI_BASE_ROM_ADDR_M;
2593 	else
2594 		value = 0;
2595 
2596 	if (value != 0) {
2597 		regs[nreg].pci_phys_hi = (PCI_ADDR_MEM32 | devloc) + offset;
2598 		assigned[nasgn].pci_phys_hi = (PCI_RELOCAT_B |
2599 		    PCI_ADDR_MEM32 | devloc) + offset;
2600 		base &= PCI_BASE_ROM_ADDR_M;
2601 		assigned[nasgn].pci_phys_low = base;
2602 		len = ((value ^ (value-1)) + 1) >> 1;
2603 		regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = len;
2604 		nreg++, nasgn++;
2605 		/* take it out of the memory resource */
2606 		if (base != NULL) {
2607 			(void) memlist_remove(mem_avail, base, len);
2608 			memlist_insert(mem_used, base, len);
2609 			pci_bus_res[bus].mem_size += len;
2610 		}
2611 	}
2612 
2613 	/*
2614 	 * Account for "legacy" (alias) video adapter resources
2615 	 */
2616 
2617 	/* add the three hard-decode, aliased address spaces for VGA */
2618 	if ((baseclass == PCI_CLASS_DISPLAY && subclass == PCI_DISPLAY_VGA) ||
2619 	    (baseclass == PCI_CLASS_NONE && subclass == PCI_NONE_VGA)) {
2620 
2621 		/* VGA hard decode 0x3b0-0x3bb */
2622 		regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi =
2623 		    (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc);
2624 		regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x3b0;
2625 		regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0xc;
2626 		nreg++, nasgn++;
2627 		(void) memlist_remove(io_avail, 0x3b0, 0xc);
2628 		memlist_insert(io_used, 0x3b0, 0xc);
2629 		pci_bus_res[bus].io_size += 0xc;
2630 
2631 		/* VGA hard decode 0x3c0-0x3df */
2632 		regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi =
2633 		    (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc);
2634 		regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x3c0;
2635 		regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0x20;
2636 		nreg++, nasgn++;
2637 		(void) memlist_remove(io_avail, 0x3c0, 0x20);
2638 		memlist_insert(io_used, 0x3c0, 0x20);
2639 		pci_bus_res[bus].io_size += 0x20;
2640 
2641 		/* Video memory */
2642 		regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi =
2643 		    (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_MEM32 | devloc);
2644 		regs[nreg].pci_phys_low =
2645 		    assigned[nasgn].pci_phys_low = 0xa0000;
2646 		regs[nreg].pci_size_low =
2647 		    assigned[nasgn].pci_size_low = 0x20000;
2648 		nreg++, nasgn++;
2649 		/* remove from MEM and PMEM space */
2650 		(void) memlist_remove(mem_avail, 0xa0000, 0x20000);
2651 		(void) memlist_remove(pmem_avail, 0xa0000, 0x20000);
2652 		memlist_insert(mem_used, 0xa0000, 0x20000);
2653 		pci_bus_res[bus].mem_size += 0x20000;
2654 	}
2655 
2656 	/* add the hard-decode, aliased address spaces for 8514 */
2657 	if ((baseclass == PCI_CLASS_DISPLAY) &&
2658 	    (subclass == PCI_DISPLAY_VGA) &&
2659 	    (progclass & PCI_DISPLAY_IF_8514)) {
2660 
2661 		/* hard decode 0x2e8 */
2662 		regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi =
2663 		    (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc);
2664 		regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x2e8;
2665 		regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0x1;
2666 		nreg++, nasgn++;
2667 		(void) memlist_remove(io_avail, 0x2e8, 0x1);
2668 		memlist_insert(io_used, 0x2e8, 0x1);
2669 		pci_bus_res[bus].io_size += 0x1;
2670 
2671 		/* hard decode 0x2ea-0x2ef */
2672 		regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi =
2673 		    (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc);
2674 		regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x2ea;
2675 		regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0x6;
2676 		nreg++, nasgn++;
2677 		(void) memlist_remove(io_avail, 0x2ea, 0x6);
2678 		memlist_insert(io_used, 0x2ea, 0x6);
2679 		pci_bus_res[bus].io_size += 0x6;
2680 	}
2681 
2682 done:
2683 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "reg",
2684 	    (int *)regs, nreg * sizeof (pci_regspec_t) / sizeof (int));
2685 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
2686 	    "assigned-addresses",
2687 	    (int *)assigned, nasgn * sizeof (pci_regspec_t) / sizeof (int));
2688 
2689 	return (reprogram);
2690 }
2691 
2692 static void
2693 add_ppb_props(dev_info_t *dip, uchar_t bus, uchar_t dev, uchar_t func,
2694     int pciex, ushort_t is_pci_bridge)
2695 {
2696 	char *dev_type;
2697 	int i;
2698 	uint_t val, io_range[2], mem_range[2], pmem_range[2];
2699 	uchar_t secbus = pci_getb(bus, dev, func, PCI_BCNF_SECBUS);
2700 	uchar_t subbus = pci_getb(bus, dev, func, PCI_BCNF_SUBBUS);
2701 	uchar_t progclass;
2702 
2703 	ASSERT(secbus <= subbus);
2704 
2705 	/*
2706 	 * Check if it's a subtractive PPB.
2707 	 */
2708 	progclass = pci_getb(bus, dev, func, PCI_CONF_PROGCLASS);
2709 	if (progclass == PCI_BRIDGE_PCI_IF_SUBDECODE)
2710 		pci_bus_res[secbus].subtractive = B_TRUE;
2711 
2712 	/*
2713 	 * Some BIOSes lie about max pci busses, we allow for
2714 	 * such mistakes here
2715 	 */
2716 	if (subbus > pci_bios_nbus) {
2717 		pci_bios_nbus = subbus;
2718 		alloc_res_array();
2719 	}
2720 
2721 	ASSERT(pci_bus_res[secbus].dip == NULL);
2722 	pci_bus_res[secbus].dip = dip;
2723 	pci_bus_res[secbus].par_bus = bus;
2724 
2725 	dev_type = (pciex && !is_pci_bridge) ? "pciex" : "pci";
2726 
2727 	/* setup bus number hierarchy */
2728 	pci_bus_res[secbus].sub_bus = subbus;
2729 	/*
2730 	 * Keep track of the largest subordinate bus number (this is essential
2731 	 * for peer busses because there is no other way of determining its
2732 	 * subordinate bus number).
2733 	 */
2734 	if (subbus > pci_bus_res[bus].sub_bus)
2735 		pci_bus_res[bus].sub_bus = subbus;
2736 	/*
2737 	 * Loop through subordinate busses, initializing their parent bus
2738 	 * field to this bridge's parent.  The subordinate busses' parent
2739 	 * fields may very well be further refined later, as child bridges
2740 	 * are enumerated.  (The value is to note that the subordinate busses
2741 	 * are not peer busses by changing their par_bus fields to anything
2742 	 * other than -1.)
2743 	 */
2744 	for (i = secbus + 1; i <= subbus; i++)
2745 		pci_bus_res[i].par_bus = bus;
2746 
2747 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
2748 	    "device_type", dev_type);
2749 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
2750 	    "#address-cells", 3);
2751 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
2752 	    "#size-cells", 2);
2753 
2754 	/*
2755 	 * Collect bridge window specifications, and use them to populate
2756 	 * the "avail" resources for the bus.  Not all of those resources will
2757 	 * end up being available; this is done top-down, and so the initial
2758 	 * collection of windows populates the 'ranges' property for the
2759 	 * bus node.  Later, as children are found, resources are removed from
2760 	 * the 'avail' list, so that it becomes the freelist for
2761 	 * this point in the tree.  ranges may be set again after bridge
2762 	 * reprogramming in fix_ppb_res(), in which case it's set from
2763 	 * used + avail.
2764 	 *
2765 	 * According to PPB spec, the base register should be programmed
2766 	 * with a value bigger than the limit register when there are
2767 	 * no resources available. This applies to io, memory, and
2768 	 * prefetchable memory.
2769 	 */
2770 
2771 	/*
2772 	 * io range
2773 	 * We determine i/o windows that are left unconfigured by BIOS
2774 	 * through its i/o enable bit as Microsoft recommends OEMs to do.
2775 	 * If it is unset, we disable i/o and mark it for reconfiguration in
2776 	 * later passes by setting the base > limit
2777 	 */
2778 	val = (uint_t)pci_getw(bus, dev, func, PCI_CONF_COMM);
2779 	if (val & PCI_COMM_IO) {
2780 		val = (uint_t)pci_getb(bus, dev, func, PCI_BCNF_IO_BASE_LOW);
2781 		io_range[0] = ((val & 0xf0) << 8);
2782 		val = (uint_t)pci_getb(bus, dev, func, PCI_BCNF_IO_LIMIT_LOW);
2783 		io_range[1]  = ((val & 0xf0) << 8) | 0xFFF;
2784 	} else {
2785 		io_range[0] = 0x9fff;
2786 		io_range[1] = 0x1000;
2787 		pci_putb(bus, dev, func, PCI_BCNF_IO_BASE_LOW,
2788 		    (uint8_t)((io_range[0] >> 8) & 0xf0));
2789 		pci_putb(bus, dev, func, PCI_BCNF_IO_LIMIT_LOW,
2790 		    (uint8_t)((io_range[1] >> 8) & 0xf0));
2791 		pci_putw(bus, dev, func, PCI_BCNF_IO_BASE_HI, 0);
2792 		pci_putw(bus, dev, func, PCI_BCNF_IO_LIMIT_HI, 0);
2793 	}
2794 
2795 	if (io_range[0] != 0 && io_range[0] < io_range[1]) {
2796 		memlist_insert(&pci_bus_res[secbus].io_avail,
2797 		    (uint64_t)io_range[0],
2798 		    (uint64_t)(io_range[1] - io_range[0] + 1));
2799 		memlist_insert(&pci_bus_res[bus].io_used,
2800 		    (uint64_t)io_range[0],
2801 		    (uint64_t)(io_range[1] - io_range[0] + 1));
2802 		if (pci_bus_res[bus].io_avail != NULL) {
2803 			(void) memlist_remove(&pci_bus_res[bus].io_avail,
2804 			    (uint64_t)io_range[0],
2805 			    (uint64_t)(io_range[1] - io_range[0] + 1));
2806 		}
2807 		dcmn_err(CE_NOTE, "bus %d io-range: 0x%x-%x",
2808 		    secbus, io_range[0], io_range[1]);
2809 		/* if 32-bit supported, make sure upper bits are not set */
2810 		if ((val & 0xf) == 1 &&
2811 		    pci_getw(bus, dev, func, PCI_BCNF_IO_BASE_HI)) {
2812 			cmn_err(CE_NOTE, "unsupported 32-bit IO address on"
2813 			    " pci-pci bridge [%d/%d/%d]", bus, dev, func);
2814 		}
2815 	}
2816 
2817 	/* mem range */
2818 	val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_MEM_BASE);
2819 	mem_range[0] = ((val & 0xFFF0) << 16);
2820 	val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_MEM_LIMIT);
2821 	mem_range[1] = ((val & 0xFFF0) << 16) | 0xFFFFF;
2822 	if (mem_range[0] != 0 && mem_range[0] < mem_range[1]) {
2823 		memlist_insert(&pci_bus_res[secbus].mem_avail,
2824 		    (uint64_t)mem_range[0],
2825 		    (uint64_t)(mem_range[1] - mem_range[0] + 1));
2826 		memlist_insert(&pci_bus_res[bus].mem_used,
2827 		    (uint64_t)mem_range[0],
2828 		    (uint64_t)(mem_range[1] - mem_range[0] + 1));
2829 		/* remove from parent resource list */
2830 		(void) memlist_remove(&pci_bus_res[bus].mem_avail,
2831 		    (uint64_t)mem_range[0],
2832 		    (uint64_t)(mem_range[1] - mem_range[0] + 1));
2833 		(void) memlist_remove(&pci_bus_res[bus].pmem_avail,
2834 		    (uint64_t)mem_range[0],
2835 		    (uint64_t)(mem_range[1] - mem_range[0] + 1));
2836 		dcmn_err(CE_NOTE, "bus %d mem-range: 0x%x-%x",
2837 		    secbus, mem_range[0], mem_range[1]);
2838 	}
2839 
2840 	/* prefetchable memory range */
2841 	val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_PF_BASE_LOW);
2842 	pmem_range[0] = ((val & 0xFFF0) << 16);
2843 	val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_PF_LIMIT_LOW);
2844 	pmem_range[1] = ((val & 0xFFF0) << 16) | 0xFFFFF;
2845 	if (pmem_range[0] != 0 && pmem_range[0] < pmem_range[1]) {
2846 		memlist_insert(&pci_bus_res[secbus].pmem_avail,
2847 		    (uint64_t)pmem_range[0],
2848 		    (uint64_t)(pmem_range[1] - pmem_range[0] + 1));
2849 		memlist_insert(&pci_bus_res[bus].pmem_used,
2850 		    (uint64_t)pmem_range[0],
2851 		    (uint64_t)(pmem_range[1] - pmem_range[0] + 1));
2852 		/* remove from parent resource list */
2853 		(void) memlist_remove(&pci_bus_res[bus].pmem_avail,
2854 		    (uint64_t)pmem_range[0],
2855 		    (uint64_t)(pmem_range[1] - pmem_range[0] + 1));
2856 		(void) memlist_remove(&pci_bus_res[bus].mem_avail,
2857 		    (uint64_t)pmem_range[0],
2858 		    (uint64_t)(pmem_range[1] - pmem_range[0] + 1));
2859 		dcmn_err(CE_NOTE, "bus %d pmem-range: 0x%x-%x",
2860 		    secbus, pmem_range[0], pmem_range[1]);
2861 		/* if 64-bit supported, make sure upper bits are not set */
2862 		if ((val & 0xf) == 1 &&
2863 		    pci_getl(bus, dev, func, PCI_BCNF_PF_BASE_HIGH)) {
2864 			cmn_err(CE_NOTE, "unsupported 64-bit prefetch memory on"
2865 			    " pci-pci bridge [%d/%d/%d]", bus, dev, func);
2866 		}
2867 	}
2868 
2869 	/*
2870 	 * Add VGA legacy resources to the bridge's pci_bus_res if it
2871 	 * has VGA_ENABLE set.  Note that we put them in 'avail',
2872 	 * because that's used to populate the ranges prop; they'll be
2873 	 * removed from there by the VGA device once it's found.  Also,
2874 	 * remove them from the parent's available list and note them as
2875 	 * used in the parent.
2876 	 */
2877 
2878 	if (pci_getw(bus, dev, func, PCI_BCNF_BCNTRL) &
2879 	    PCI_BCNF_BCNTRL_VGA_ENABLE) {
2880 
2881 		memlist_insert(&pci_bus_res[secbus].io_avail, 0x3b0, 0xc);
2882 
2883 		memlist_insert(&pci_bus_res[bus].io_used, 0x3b0, 0xc);
2884 		if (pci_bus_res[bus].io_avail != NULL) {
2885 			(void) memlist_remove(&pci_bus_res[bus].io_avail,
2886 			    0x3b0, 0xc);
2887 		}
2888 
2889 		memlist_insert(&pci_bus_res[secbus].io_avail, 0x3c0, 0x20);
2890 
2891 		memlist_insert(&pci_bus_res[bus].io_used, 0x3c0, 0x20);
2892 		if (pci_bus_res[bus].io_avail != NULL) {
2893 			(void) memlist_remove(&pci_bus_res[bus].io_avail,
2894 			    0x3c0, 0x20);
2895 		}
2896 
2897 		memlist_insert(&pci_bus_res[secbus].mem_avail, 0xa0000,
2898 		    0x20000);
2899 
2900 		memlist_insert(&pci_bus_res[bus].mem_used, 0xa0000, 0x20000);
2901 		if (pci_bus_res[bus].mem_avail != NULL) {
2902 			(void) memlist_remove(&pci_bus_res[bus].mem_avail,
2903 			    0xa0000, 0x20000);
2904 		}
2905 	}
2906 	add_bus_range_prop(secbus);
2907 	add_ranges_prop(secbus, 1);
2908 }
2909 
2910 extern const struct pci_class_strings_s class_pci[];
2911 extern int class_pci_items;
2912 
2913 static void
2914 add_model_prop(dev_info_t *dip, uint_t classcode)
2915 {
2916 	const char *desc;
2917 	int i;
2918 	uchar_t baseclass = classcode >> 16;
2919 	uchar_t subclass = (classcode >> 8) & 0xff;
2920 	uchar_t progclass = classcode & 0xff;
2921 
2922 	if ((baseclass == PCI_CLASS_MASS) && (subclass == PCI_MASS_IDE)) {
2923 		desc = "IDE controller";
2924 	} else {
2925 		for (desc = 0, i = 0; i < class_pci_items; i++) {
2926 			if ((baseclass == class_pci[i].base_class) &&
2927 			    (subclass == class_pci[i].sub_class) &&
2928 			    (progclass == class_pci[i].prog_class)) {
2929 				desc = class_pci[i].actual_desc;
2930 				break;
2931 			}
2932 		}
2933 		if (i == class_pci_items)
2934 			desc = "Unknown class of pci/pnpbios device";
2935 	}
2936 
2937 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, "model",
2938 	    (char *)desc);
2939 }
2940 
2941 static void
2942 add_bus_range_prop(int bus)
2943 {
2944 	int bus_range[2];
2945 
2946 	if (pci_bus_res[bus].dip == NULL)
2947 		return;
2948 	bus_range[0] = bus;
2949 	bus_range[1] = pci_bus_res[bus].sub_bus;
2950 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, pci_bus_res[bus].dip,
2951 	    "bus-range", (int *)bus_range, 2);
2952 }
2953 
2954 /*
2955  * Add slot-names property for any named pci hot-plug slots
2956  */
2957 static void
2958 add_bus_slot_names_prop(int bus)
2959 {
2960 	char slotprop[256];
2961 	int len;
2962 
2963 	if (pci_bus_res[bus].dip != NULL) {
2964 		/* simply return if the property is already defined */
2965 		if (ddi_prop_exists(DDI_DEV_T_ANY, pci_bus_res[bus].dip,
2966 		    DDI_PROP_DONTPASS, "slot-names"))
2967 			return;
2968 	}
2969 
2970 	len = pci_slot_names_prop(bus, slotprop, sizeof (slotprop));
2971 	if (len > 0) {
2972 		/*
2973 		 * Only create a peer bus node if this bus may be a peer bus.
2974 		 * It may be a peer bus if the dip is NULL and if par_bus is
2975 		 * -1 (par_bus is -1 if this bus was not found to be
2976 		 * subordinate to any PCI-PCI bridge).
2977 		 * If it's not a peer bus, then the ACPI BBN-handling code
2978 		 * will remove it later.
2979 		 */
2980 		if (pci_bus_res[bus].par_bus == (uchar_t)-1 &&
2981 		    pci_bus_res[bus].dip == NULL) {
2982 
2983 			create_root_bus_dip(bus);
2984 		}
2985 		if (pci_bus_res[bus].dip != NULL) {
2986 			ASSERT((len % sizeof (int)) == 0);
2987 			(void) ndi_prop_update_int_array(DDI_DEV_T_NONE,
2988 			    pci_bus_res[bus].dip, "slot-names",
2989 			    (int *)slotprop, len / sizeof (int));
2990 		} else {
2991 			cmn_err(CE_NOTE, "!BIOS BUG: Invalid bus number in PCI "
2992 			    "IRQ routing table; Not adding slot-names "
2993 			    "property for incorrect bus %d", bus);
2994 		}
2995 	}
2996 }
2997 
2998 /*
2999  * Handle both PCI root and PCI-PCI bridge range properties;
3000  * non-zero 'ppb' argument select PCI-PCI bridges versus root.
3001  */
3002 static void
3003 memlist_to_ranges(void **rp, struct memlist *entry, int type, int ppb)
3004 {
3005 	ppb_ranges_t *ppb_rp = *rp;
3006 	pci_ranges_t *pci_rp = *rp;
3007 
3008 	while (entry != NULL) {
3009 		if (ppb) {
3010 			ppb_rp->child_high = ppb_rp->parent_high = type;
3011 			ppb_rp->child_mid = ppb_rp->parent_mid =
3012 			    (uint32_t)(entry->address >> 32); /* XXX */
3013 			ppb_rp->child_low = ppb_rp->parent_low =
3014 			    (uint32_t)entry->address;
3015 			ppb_rp->size_high =
3016 			    (uint32_t)(entry->size >> 32); /* XXX */
3017 			ppb_rp->size_low = (uint32_t)entry->size;
3018 			*rp = ++ppb_rp;
3019 		} else {
3020 			pci_rp->child_high = type;
3021 			pci_rp->child_mid = pci_rp->parent_high =
3022 			    (uint32_t)(entry->address >> 32); /* XXX */
3023 			pci_rp->child_low = pci_rp->parent_low =
3024 			    (uint32_t)entry->address;
3025 			pci_rp->size_high =
3026 			    (uint32_t)(entry->size >> 32); /* XXX */
3027 			pci_rp->size_low = (uint32_t)entry->size;
3028 			*rp = ++pci_rp;
3029 		}
3030 		entry = entry->next;
3031 	}
3032 }
3033 
3034 static void
3035 add_ranges_prop(int bus, int ppb)
3036 {
3037 	int total, alloc_size;
3038 	void	*rp, *next_rp;
3039 	struct memlist *iolist, *memlist, *pmemlist;
3040 
3041 	/* no devinfo node - unused bus, return */
3042 	if (pci_bus_res[bus].dip == NULL)
3043 		return;
3044 
3045 	iolist = memlist = pmemlist = (struct memlist *)NULL;
3046 
3047 	memlist_merge(&pci_bus_res[bus].io_avail, &iolist);
3048 	memlist_merge(&pci_bus_res[bus].io_used, &iolist);
3049 	memlist_merge(&pci_bus_res[bus].mem_avail, &memlist);
3050 	memlist_merge(&pci_bus_res[bus].mem_used, &memlist);
3051 	memlist_merge(&pci_bus_res[bus].pmem_avail, &pmemlist);
3052 	memlist_merge(&pci_bus_res[bus].pmem_used, &pmemlist);
3053 
3054 	total = memlist_count(iolist);
3055 	total += memlist_count(memlist);
3056 	total += memlist_count(pmemlist);
3057 
3058 	/* no property is created if no ranges are present */
3059 	if (total == 0)
3060 		return;
3061 
3062 	alloc_size = total *
3063 	    (ppb ? sizeof (ppb_ranges_t) : sizeof (pci_ranges_t));
3064 
3065 	next_rp = rp = kmem_alloc(alloc_size, KM_SLEEP);
3066 
3067 	memlist_to_ranges(&next_rp, iolist, PCI_ADDR_IO | PCI_REG_REL_M, ppb);
3068 	memlist_to_ranges(&next_rp, memlist,
3069 	    PCI_ADDR_MEM32 | PCI_REG_REL_M, ppb);
3070 	memlist_to_ranges(&next_rp, pmemlist,
3071 	    PCI_ADDR_MEM32 | PCI_REG_REL_M | PCI_REG_PF_M, ppb);
3072 
3073 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, pci_bus_res[bus].dip,
3074 	    "ranges", (int *)rp, alloc_size / sizeof (int));
3075 
3076 	kmem_free(rp, alloc_size);
3077 	memlist_free_all(&iolist);
3078 	memlist_free_all(&memlist);
3079 	memlist_free_all(&pmemlist);
3080 }
3081 
3082 static void
3083 memlist_remove_list(struct memlist **list, struct memlist *remove_list)
3084 {
3085 	while (list && *list && remove_list) {
3086 		(void) memlist_remove(list, remove_list->address,
3087 		    remove_list->size);
3088 		remove_list = remove_list->next;
3089 	}
3090 }
3091 
3092 static int
3093 memlist_to_spec(struct pci_phys_spec *sp, struct memlist *list, int type)
3094 {
3095 	int i = 0;
3096 
3097 	while (list) {
3098 		/* assume 32-bit addresses */
3099 		sp->pci_phys_hi = type;
3100 		sp->pci_phys_mid = 0;
3101 		sp->pci_phys_low = (uint32_t)list->address;
3102 		sp->pci_size_hi = 0;
3103 		sp->pci_size_low = (uint32_t)list->size;
3104 
3105 		list = list->next;
3106 		sp++, i++;
3107 	}
3108 	return (i);
3109 }
3110 
3111 static void
3112 add_bus_available_prop(int bus)
3113 {
3114 	int i, count;
3115 	struct pci_phys_spec *sp;
3116 
3117 	/* no devinfo node - unused bus, return */
3118 	if (pci_bus_res[bus].dip == NULL)
3119 		return;
3120 
3121 	count = memlist_count(pci_bus_res[bus].io_avail) +
3122 	    memlist_count(pci_bus_res[bus].mem_avail) +
3123 	    memlist_count(pci_bus_res[bus].pmem_avail);
3124 
3125 	if (count == 0)		/* nothing available */
3126 		return;
3127 
3128 	sp = kmem_alloc(count * sizeof (*sp), KM_SLEEP);
3129 	i = memlist_to_spec(&sp[0], pci_bus_res[bus].io_avail,
3130 	    PCI_ADDR_IO | PCI_REG_REL_M);
3131 	i += memlist_to_spec(&sp[i], pci_bus_res[bus].mem_avail,
3132 	    PCI_ADDR_MEM32 | PCI_REG_REL_M);
3133 	i += memlist_to_spec(&sp[i], pci_bus_res[bus].pmem_avail,
3134 	    PCI_ADDR_MEM32 | PCI_REG_REL_M | PCI_REG_PF_M);
3135 	ASSERT(i == count);
3136 
3137 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, pci_bus_res[bus].dip,
3138 	    "available", (int *)sp,
3139 	    i * sizeof (struct pci_phys_spec) / sizeof (int));
3140 	kmem_free(sp, count * sizeof (*sp));
3141 }
3142 
3143 static void
3144 alloc_res_array(void)
3145 {
3146 	static int array_max = 0;
3147 	int old_max;
3148 	void *old_res;
3149 
3150 	if (array_max > pci_bios_nbus + 1)
3151 		return;	/* array is big enough */
3152 
3153 	old_max = array_max;
3154 	old_res = pci_bus_res;
3155 
3156 	if (array_max == 0)
3157 		array_max = 16;	/* start with a reasonable number */
3158 
3159 	while (array_max < pci_bios_nbus + 1)
3160 		array_max <<= 1;
3161 	pci_bus_res = (struct pci_bus_resource *)kmem_zalloc(
3162 	    array_max * sizeof (struct pci_bus_resource), KM_SLEEP);
3163 
3164 	if (old_res) {	/* copy content and free old array */
3165 		bcopy(old_res, pci_bus_res,
3166 		    old_max * sizeof (struct pci_bus_resource));
3167 		kmem_free(old_res, old_max * sizeof (struct pci_bus_resource));
3168 	}
3169 }
3170 
3171 static void
3172 create_ioapic_node(int bus, int dev, int fn, ushort_t vendorid,
3173     ushort_t deviceid)
3174 {
3175 	static dev_info_t *ioapicsnode = NULL;
3176 	static int numioapics = 0;
3177 	dev_info_t *ioapic_node;
3178 	uint64_t physaddr;
3179 	uint32_t lobase, hibase = 0;
3180 
3181 	/* BAR 0 contains the IOAPIC's memory-mapped I/O address */
3182 	lobase = (*pci_getl_func)(bus, dev, fn, PCI_CONF_BASE0);
3183 
3184 	/* We (and the rest of the world) only support memory-mapped IOAPICs */
3185 	if ((lobase & PCI_BASE_SPACE_M) != PCI_BASE_SPACE_MEM)
3186 		return;
3187 
3188 	if ((lobase & PCI_BASE_TYPE_M) == PCI_BASE_TYPE_ALL)
3189 		hibase = (*pci_getl_func)(bus, dev, fn, PCI_CONF_BASE0 + 4);
3190 
3191 	lobase &= PCI_BASE_M_ADDR_M;
3192 
3193 	physaddr = (((uint64_t)hibase) << 32) | lobase;
3194 
3195 	/*
3196 	 * Create a nexus node for all IOAPICs under the root node.
3197 	 */
3198 	if (ioapicsnode == NULL) {
3199 		if (ndi_devi_alloc(ddi_root_node(), IOAPICS_NODE_NAME,
3200 		    (pnode_t)DEVI_SID_NODEID, &ioapicsnode) != NDI_SUCCESS) {
3201 			return;
3202 		}
3203 		(void) ndi_devi_online(ioapicsnode, 0);
3204 	}
3205 
3206 	/*
3207 	 * Create a child node for this IOAPIC
3208 	 */
3209 	ioapic_node = ddi_add_child(ioapicsnode, IOAPICS_CHILD_NAME,
3210 	    DEVI_SID_NODEID, numioapics++);
3211 	if (ioapic_node == NULL) {
3212 		return;
3213 	}
3214 
3215 	/* Vendor and Device ID */
3216 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, ioapic_node,
3217 	    IOAPICS_PROP_VENID, vendorid);
3218 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, ioapic_node,
3219 	    IOAPICS_PROP_DEVID, deviceid);
3220 
3221 	/* device_type */
3222 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, ioapic_node,
3223 	    "device_type", IOAPICS_DEV_TYPE);
3224 
3225 	/* reg */
3226 	(void) ndi_prop_update_int64(DDI_DEV_T_NONE, ioapic_node,
3227 	    "reg", physaddr);
3228 }
3229 
3230 /*
3231  * NOTE: For PCIe slots, the name is generated from the slot number
3232  * information obtained from Slot Capabilities register.
3233  * For non-PCIe slots, it is generated based on the slot number
3234  * information in the PCI IRQ table.
3235  */
3236 static void
3237 pciex_slot_names_prop(dev_info_t *dip, ushort_t slot_num)
3238 {
3239 	char slotprop[256];
3240 	int len;
3241 
3242 	bzero(slotprop, sizeof (slotprop));
3243 
3244 	/* set mask to 1 as there is only one slot (i.e dev 0) */
3245 	*(uint32_t *)slotprop = 1;
3246 	len = 4;
3247 	(void) snprintf(slotprop + len, sizeof (slotprop) - len, "pcie%d",
3248 	    slot_num);
3249 	len += strlen(slotprop + len) + 1;
3250 	len += len % 4;
3251 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "slot-names",
3252 	    (int *)slotprop, len / sizeof (int));
3253 }
3254