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