xref: /titanic_51/usr/src/uts/intel/io/pci/pci_boot.c (revision 37714ae43602c675f9dc59b070bfdf9fa702872c)
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, *scratch_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 	scratch_list = memlist_dup(pci_bus_res[secbus].io_avail);
789 	memlist_merge(&pci_bus_res[secbus].io_used, &scratch_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(scratch_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 	memlist_free_all(&scratch_list);
861 
862 	/*
863 	 * Check memory space as we did I/O space.
864 	 */
865 	mem_base = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_MEM_BASE);
866 	mem_base = (mem_base & 0xfff0) << 16;
867 	mem_limit = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_MEM_LIMIT);
868 	mem_limit = ((mem_limit & 0xfff0) << 16) | 0xfffff;
869 
870 	scratch_list = memlist_dup(pci_bus_res[secbus].mem_avail);
871 	memlist_merge(&pci_bus_res[secbus].mem_used, &scratch_list);
872 
873 	if ((pci_bus_res[parbus].mem_reprogram ||
874 	    (mem_base > mem_limit) ||
875 	    (!(cmd_reg & PCI_COMM_MAE))) &&
876 	    !list_is_vga_only(scratch_list, MEM)) {
877 		if (pci_bus_res[secbus].mem_used) {
878 			memlist_subsume(&pci_bus_res[secbus].mem_used,
879 			    &pci_bus_res[secbus].mem_avail);
880 		}
881 		if (pci_bus_res[secbus].mem_avail &&
882 		    (!pci_bus_res[parbus].mem_reprogram) &&
883 		    (!pci_bus_res[parbus].subtractive)) {
884 			/* rechoose old mem resource */
885 			list = pci_bus_res[secbus].mem_avail;
886 			mem_base = 0;
887 			do {
888 				if (is_vga(list, MEM))
889 					continue;
890 				if (mem_base == 0) {
891 					mem_base = (uint_t)list->address;
892 					mem_base = P2ALIGN(mem_base,
893 					    PPB_MEM_ALIGNMENT);
894 					mem_limit = (uint_t)
895 					    (list->address + list->size - 1);
896 				} else {
897 					if ((list->address + list->size) >
898 					    mem_limit) {
899 						mem_limit = (uint_t)
900 						    (list->address +
901 						    list->size - 1);
902 					}
903 				}
904 			} while ((list = list->next) != NULL);
905 			mem_limit = P2ROUNDUP(mem_limit, PPB_MEM_ALIGNMENT) - 1;
906 			mem_size = mem_limit + 1 - mem_base;
907 			ASSERT(mem_base <= mem_limit);
908 			memlist_free_all(&pci_bus_res[secbus].mem_avail);
909 			memlist_insert(&pci_bus_res[secbus].mem_avail,
910 			    mem_base, mem_size);
911 			memlist_insert(&pci_bus_res[parbus].mem_used,
912 			    mem_base, mem_size);
913 			(void) memlist_remove(&pci_bus_res[parbus].mem_avail,
914 			    mem_base, mem_size);
915 			pci_bus_res[secbus].mem_reprogram = B_TRUE;
916 		} else {
917 			/* get new mem resource from parent bus */
918 			addr = get_parbus_mem_res(parbus, secbus, mem_size,
919 			    mem_align);
920 			if (addr) {
921 				mem_base = addr;
922 				mem_limit = addr + mem_size - 1;
923 				pci_bus_res[secbus].mem_reprogram = B_TRUE;
924 			}
925 		}
926 
927 		if (pci_bus_res[secbus].mem_reprogram) {
928 			/* reprogram PPB MEM regs */
929 			pci_putw(bus, dev, func, PCI_BCNF_MEM_BASE,
930 			    (uint16_t)((mem_base>>16) & 0xfff0));
931 			pci_putw(bus, dev, func, PCI_BCNF_MEM_LIMIT,
932 			    (uint16_t)((mem_limit>>16) & 0xfff0));
933 			/*
934 			 * Disable PMEM window by setting base > limit.
935 			 * We currently don't reprogram the PMEM like we've
936 			 * done for I/O and MEM. (Devices that support prefetch
937 			 * can use non-prefetch MEM.) Anyway, if the MEM access
938 			 * bit is initially disabled by BIOS, we disable the
939 			 * PMEM window manually by setting PMEM base > PMEM
940 			 * limit here, in case there are incorrect values in
941 			 * them from BIOS, so that we won't get in trouble once
942 			 * the MEM access bit is enabled at the end of this
943 			 * function.
944 			 */
945 			if (!(cmd_reg & PCI_COMM_MAE)) {
946 				pci_putw(bus, dev, func, PCI_BCNF_PF_BASE_LOW,
947 				    0xfff0);
948 				pci_putw(bus, dev, func, PCI_BCNF_PF_LIMIT_LOW,
949 				    0x0);
950 				pci_putl(bus, dev, func, PCI_BCNF_PF_BASE_HIGH,
951 				    0xffffffff);
952 				pci_putl(bus, dev, func, PCI_BCNF_PF_LIMIT_HIGH,
953 				    0x0);
954 			}
955 
956 			add_ranges_prop(secbus, 1);
957 
958 			cmn_err(CE_NOTE, "!reprogram mem-range on"
959 			    " ppb[%x/%x/%x]: 0x%x ~ 0x%x\n",
960 			    bus, dev, func, mem_base, mem_limit);
961 		}
962 	}
963 	memlist_free_all(&scratch_list);
964 
965 cmd_enable:
966 	if (pci_bus_res[secbus].io_avail)
967 		cmd_reg |= PCI_COMM_IO | PCI_COMM_ME;
968 	if (pci_bus_res[secbus].mem_avail)
969 		cmd_reg |= PCI_COMM_MAE | PCI_COMM_ME;
970 	pci_putw(bus, dev, func, PCI_CONF_COMM, cmd_reg);
971 }
972 
973 void
974 pci_reprogram(void)
975 {
976 	int i, pci_reconfig = 1;
977 	char *onoff;
978 	int bus;
979 
980 	/*
981 	 * Excise phantom roots if possible
982 	 */
983 	pci_renumber_root_busses();
984 
985 	/*
986 	 * Do root-bus resource discovery
987 	 */
988 	for (bus = 0; bus <= pci_bios_nbus; bus++) {
989 		/* skip non-root (peer) PCI busses */
990 		if (pci_bus_res[bus].par_bus != (uchar_t)-1)
991 			continue;
992 
993 		/*
994 		 * 1. find resources associated with this root bus
995 		 */
996 		populate_bus_res(bus);
997 
998 
999 		/*
1000 		 * 2. Remove used PCI and ISA resources from bus resource map
1001 		 */
1002 
1003 		memlist_remove_list(&pci_bus_res[bus].io_avail,
1004 		    pci_bus_res[bus].io_used);
1005 		memlist_remove_list(&pci_bus_res[bus].mem_avail,
1006 		    pci_bus_res[bus].mem_used);
1007 		memlist_remove_list(&pci_bus_res[bus].pmem_avail,
1008 		    pci_bus_res[bus].pmem_used);
1009 		memlist_remove_list(&pci_bus_res[bus].mem_avail,
1010 		    pci_bus_res[bus].pmem_used);
1011 		memlist_remove_list(&pci_bus_res[bus].pmem_avail,
1012 		    pci_bus_res[bus].mem_used);
1013 
1014 		memlist_remove_list(&pci_bus_res[bus].io_avail,
1015 		    isa_res.io_used);
1016 		memlist_remove_list(&pci_bus_res[bus].mem_avail,
1017 		    isa_res.mem_used);
1018 	}
1019 
1020 	memlist_free_all(&isa_res.io_used);
1021 	memlist_free_all(&isa_res.mem_used);
1022 
1023 	/* add bus-range property for root/peer bus nodes */
1024 	for (i = 0; i <= pci_bios_nbus; i++) {
1025 		/* create bus-range property on root/peer buses */
1026 		if (pci_bus_res[i].par_bus == (uchar_t)-1)
1027 			add_bus_range_prop(i);
1028 
1029 		/* setup bus range resource on each bus */
1030 		setup_bus_res(i);
1031 	}
1032 
1033 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
1034 	    DDI_PROP_DONTPASS, "pci-reprog", &onoff) == DDI_SUCCESS) {
1035 		if (strcmp(onoff, "off") == 0) {
1036 			pci_reconfig = 0;
1037 			cmn_err(CE_NOTE, "pci device reprogramming disabled");
1038 		}
1039 		ddi_prop_free(onoff);
1040 	}
1041 
1042 	remove_subtractive_res();
1043 
1044 	/* reprogram the non-subtractive PPB */
1045 	if (pci_reconfig)
1046 		for (i = 0; i <= pci_bios_nbus; i++)
1047 			fix_ppb_res(i, B_FALSE);
1048 
1049 	for (i = 0; i <= pci_bios_nbus; i++) {
1050 		/* configure devices not configured by BIOS */
1051 		if (pci_reconfig) {
1052 			/*
1053 			 * Reprogram the subtractive PPB. At this time, all its
1054 			 * siblings should have got their resources already.
1055 			 */
1056 			if (pci_bus_res[i].subtractive)
1057 				fix_ppb_res(i, B_TRUE);
1058 			enumerate_bus_devs(i, CONFIG_NEW);
1059 		}
1060 	}
1061 
1062 	/* All dev programmed, so we can create available prop */
1063 	for (i = 0; i <= pci_bios_nbus; i++)
1064 		add_bus_available_prop(i);
1065 }
1066 
1067 /*
1068  * populate bus resources
1069  */
1070 static void
1071 populate_bus_res(uchar_t bus)
1072 {
1073 
1074 	/* scan BIOS structures */
1075 	pci_bus_res[bus].pmem_avail = find_bus_res(bus, PREFETCH_TYPE);
1076 	pci_bus_res[bus].mem_avail = find_bus_res(bus, MEM_TYPE);
1077 	pci_bus_res[bus].io_avail = find_bus_res(bus, IO_TYPE);
1078 	pci_bus_res[bus].bus_avail = find_bus_res(bus, BUSRANGE_TYPE);
1079 
1080 	/*
1081 	 * attempt to initialize sub_bus from the largest range-end
1082 	 * in the bus_avail list
1083 	 */
1084 	if (pci_bus_res[bus].bus_avail != NULL) {
1085 		struct memlist *entry;
1086 		int current;
1087 
1088 		entry = pci_bus_res[bus].bus_avail;
1089 		while (entry != NULL) {
1090 			current = entry->address + entry->size - 1;
1091 			if (current > pci_bus_res[bus].sub_bus)
1092 				pci_bus_res[bus].sub_bus = current;
1093 			entry = entry->next;
1094 		}
1095 	}
1096 
1097 	if (bus == 0) {
1098 		/*
1099 		 * Special treatment of bus 0:
1100 		 * If no IO/MEM resource from ACPI/MPSPEC/HRT, copy
1101 		 * pcimem from boot and make I/O space the entire range
1102 		 * starting at 0x100.
1103 		 */
1104 		if (pci_bus_res[0].mem_avail == NULL)
1105 			pci_bus_res[0].mem_avail =
1106 			    memlist_dup(bootops->boot_mem->pcimem);
1107 		/* Exclude 0x00 to 0xff of the I/O space, used by all PCs */
1108 		if (pci_bus_res[0].io_avail == NULL)
1109 			memlist_insert(&pci_bus_res[0].io_avail, 0x100, 0xffff);
1110 	}
1111 
1112 	/*
1113 	 * Create 'ranges' property here before any resources are
1114 	 * removed from the resource lists
1115 	 */
1116 	add_ranges_prop(bus, 0);
1117 }
1118 
1119 
1120 /*
1121  * Create top-level bus dips, i.e. /pci@0,0, /pci@1,0...
1122  */
1123 static void
1124 create_root_bus_dip(uchar_t bus)
1125 {
1126 	int pci_regs[] = {0, 0, 0};
1127 	dev_info_t *dip;
1128 
1129 	ASSERT(pci_bus_res[bus].par_bus == (uchar_t)-1);
1130 
1131 	num_root_bus++;
1132 	ndi_devi_alloc_sleep(ddi_root_node(), "pci",
1133 	    (pnode_t)DEVI_SID_NODEID, &dip);
1134 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1135 	    "#address-cells", 3);
1136 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1137 	    "#size-cells", 2);
1138 	pci_regs[0] = pci_bus_res[bus].root_addr;
1139 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
1140 	    "reg", (int *)pci_regs, 3);
1141 
1142 	/*
1143 	 * If system has PCIe bus, then create different properties
1144 	 */
1145 	if (create_pcie_root_bus(bus, dip) == B_FALSE)
1146 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1147 		    "device_type", "pci");
1148 
1149 	(void) ndi_devi_bind_driver(dip, 0);
1150 	pci_bus_res[bus].dip = dip;
1151 }
1152 
1153 /*
1154  * For any fixed configuration (often compatability) pci devices
1155  * and those with their own expansion rom, create device nodes
1156  * to hold the already configured device details.
1157  */
1158 void
1159 enumerate_bus_devs(uchar_t bus, int config_op)
1160 {
1161 	uchar_t dev, func, nfunc, header;
1162 	ushort_t venid;
1163 	struct pci_devfunc *devlist = NULL, *entry;
1164 
1165 	if (config_op == CONFIG_NEW) {
1166 		dcmn_err(CE_NOTE, "configuring pci bus 0x%x", bus);
1167 	} else if (config_op == CONFIG_FIX) {
1168 		dcmn_err(CE_NOTE, "fixing devices on pci bus 0x%x", bus);
1169 	} else
1170 		dcmn_err(CE_NOTE, "enumerating pci bus 0x%x", bus);
1171 
1172 	if (config_op == CONFIG_NEW) {
1173 		devlist = (struct pci_devfunc *)pci_bus_res[bus].privdata;
1174 		while (devlist) {
1175 			entry = devlist;
1176 			devlist = entry->next;
1177 			if (entry->reprogram ||
1178 			    pci_bus_res[bus].io_reprogram ||
1179 			    pci_bus_res[bus].mem_reprogram) {
1180 				/* reprogram device(s) */
1181 				(void) add_reg_props(entry->dip, bus,
1182 				    entry->dev, entry->func, CONFIG_NEW, 0);
1183 			}
1184 			kmem_free(entry, sizeof (*entry));
1185 		}
1186 		pci_bus_res[bus].privdata = NULL;
1187 		return;
1188 	}
1189 
1190 	for (dev = 0; dev < max_dev_pci; dev++) {
1191 		nfunc = 1;
1192 		for (func = 0; func < nfunc; func++) {
1193 
1194 			dcmn_err(CE_NOTE, "probing dev 0x%x, func 0x%x",
1195 			    dev, func);
1196 
1197 			venid = pci_getw(bus, dev, func, PCI_CONF_VENID);
1198 
1199 			if ((venid == 0xffff) || (venid == 0)) {
1200 				/* no function at this address */
1201 				continue;
1202 			}
1203 
1204 			header = pci_getb(bus, dev, func, PCI_CONF_HEADER);
1205 			if (header == 0xff) {
1206 				continue; /* illegal value */
1207 			}
1208 
1209 			/*
1210 			 * according to some mail from Microsoft posted
1211 			 * to the pci-drivers alias, their only requirement
1212 			 * for a multifunction device is for the 1st
1213 			 * function to have to PCI_HEADER_MULTI bit set.
1214 			 */
1215 			if ((func == 0) && (header & PCI_HEADER_MULTI)) {
1216 				nfunc = 8;
1217 			}
1218 
1219 			if (config_op == CONFIG_FIX ||
1220 			    config_op == CONFIG_INFO) {
1221 				/*
1222 				 * Create the node, unconditionally, on the
1223 				 * first pass only.  It may still need
1224 				 * resource assignment, which will be
1225 				 * done on the second, CONFIG_NEW, pass.
1226 				 */
1227 				process_devfunc(bus, dev, func, header,
1228 				    venid, config_op);
1229 
1230 			}
1231 		}
1232 	}
1233 
1234 	/* percolate bus used resources up through parents to root */
1235 	if (config_op == CONFIG_INFO) {
1236 		int	par_bus;
1237 
1238 		par_bus = pci_bus_res[bus].par_bus;
1239 		while (par_bus != (uchar_t)-1) {
1240 			pci_bus_res[par_bus].io_size +=
1241 			    pci_bus_res[bus].io_size;
1242 			pci_bus_res[par_bus].mem_size +=
1243 			    pci_bus_res[bus].mem_size;
1244 
1245 			if (pci_bus_res[bus].io_used)
1246 				memlist_merge(&pci_bus_res[bus].io_used,
1247 				    &pci_bus_res[par_bus].io_used);
1248 
1249 			if (pci_bus_res[bus].mem_used)
1250 				memlist_merge(&pci_bus_res[bus].mem_used,
1251 				    &pci_bus_res[par_bus].mem_used);
1252 
1253 			if (pci_bus_res[bus].pmem_used)
1254 				memlist_merge(&pci_bus_res[bus].pmem_used,
1255 				    &pci_bus_res[par_bus].pmem_used);
1256 
1257 			bus = par_bus;
1258 			par_bus = pci_bus_res[par_bus].par_bus;
1259 		}
1260 	}
1261 }
1262 
1263 static int
1264 check_pciide_prop(uchar_t revid, ushort_t venid, ushort_t devid,
1265     ushort_t subvenid, ushort_t subdevid)
1266 {
1267 	static int prop_exist = -1;
1268 	static char *pciide_str;
1269 	char compat[32];
1270 
1271 	if (prop_exist == -1) {
1272 		prop_exist = (ddi_prop_lookup_string(DDI_DEV_T_ANY,
1273 		    ddi_root_node(), DDI_PROP_DONTPASS, "pci-ide",
1274 		    &pciide_str) == DDI_SUCCESS);
1275 	}
1276 
1277 	if (!prop_exist)
1278 		return (0);
1279 
1280 	/* compare property value against various forms of compatible */
1281 	if (subvenid) {
1282 		(void) snprintf(compat, sizeof (compat), "pci%x,%x.%x.%x.%x",
1283 		    venid, devid, subvenid, subdevid, revid);
1284 		if (strcmp(pciide_str, compat) == 0)
1285 			return (1);
1286 
1287 		(void) snprintf(compat, sizeof (compat), "pci%x,%x.%x.%x",
1288 		    venid, devid, subvenid, subdevid);
1289 		if (strcmp(pciide_str, compat) == 0)
1290 			return (1);
1291 
1292 		(void) snprintf(compat, sizeof (compat), "pci%x,%x",
1293 		    subvenid, subdevid);
1294 		if (strcmp(pciide_str, compat) == 0)
1295 			return (1);
1296 	}
1297 	(void) snprintf(compat, sizeof (compat), "pci%x,%x.%x",
1298 	    venid, devid, revid);
1299 	if (strcmp(pciide_str, compat) == 0)
1300 		return (1);
1301 
1302 	(void) snprintf(compat, sizeof (compat), "pci%x,%x", venid, devid);
1303 	if (strcmp(pciide_str, compat) == 0)
1304 		return (1);
1305 
1306 	return (0);
1307 }
1308 
1309 static int
1310 is_pciide(uchar_t basecl, uchar_t subcl, uchar_t revid,
1311     ushort_t venid, ushort_t devid, ushort_t subvenid, ushort_t subdevid)
1312 {
1313 	struct ide_table {	/* table for PCI_MASS_OTHER */
1314 		ushort_t venid;
1315 		ushort_t devid;
1316 	} *entry;
1317 
1318 	/* XXX SATA and other devices: need a way to add dynamically */
1319 	static struct ide_table ide_other[] = {
1320 		{0x1095, 0x3112},
1321 		{0x1095, 0x3114},
1322 		{0x1095, 0x3512},
1323 		{0x1095, 0x680},	/* Sil0680 */
1324 		{0x1283, 0x8211},	/* ITE 8211F is subcl PCI_MASS_OTHER */
1325 		{0, 0}
1326 	};
1327 
1328 	if (basecl != PCI_CLASS_MASS)
1329 		return (0);
1330 
1331 	if (subcl == PCI_MASS_IDE) {
1332 		return (1);
1333 	}
1334 
1335 	if (check_pciide_prop(revid, venid, devid, subvenid, subdevid))
1336 		return (1);
1337 
1338 	if (subcl != PCI_MASS_OTHER && subcl != PCI_MASS_SATA) {
1339 		return (0);
1340 	}
1341 
1342 	entry = &ide_other[0];
1343 	while (entry->venid) {
1344 		if (entry->venid == venid && entry->devid == devid)
1345 			return (1);
1346 		entry++;
1347 	}
1348 	return (0);
1349 }
1350 
1351 static int
1352 is_display(uint_t classcode)
1353 {
1354 	static uint_t disp_classes[] = {
1355 		0x000100,
1356 		0x030000,
1357 		0x030001
1358 	};
1359 	int i, nclasses = sizeof (disp_classes) / sizeof (uint_t);
1360 
1361 	for (i = 0; i < nclasses; i++) {
1362 		if (classcode == disp_classes[i])
1363 			return (1);
1364 	}
1365 	return (0);
1366 }
1367 
1368 static void
1369 add_undofix_entry(uint8_t bus, uint8_t dev, uint8_t fn,
1370     void (*undofn)(uint8_t, uint8_t, uint8_t))
1371 {
1372 	struct pci_fixundo *newundo;
1373 
1374 	newundo = kmem_alloc(sizeof (struct pci_fixundo), KM_SLEEP);
1375 
1376 	/*
1377 	 * Adding an item to this list means that we must turn its NMIENABLE
1378 	 * bit back on at a later time.
1379 	 */
1380 	newundo->bus = bus;
1381 	newundo->dev = dev;
1382 	newundo->fn = fn;
1383 	newundo->undofn = undofn;
1384 	newundo->next = undolist;
1385 
1386 	/* add to the undo list in LIFO order */
1387 	undolist = newundo;
1388 }
1389 
1390 void
1391 add_pci_fixes(void)
1392 {
1393 	int i;
1394 
1395 	for (i = 0; i <= pci_bios_nbus; i++) {
1396 		/*
1397 		 * For each bus, apply needed fixes to the appropriate devices.
1398 		 * This must be done before the main enumeration loop because
1399 		 * some fixes must be applied to devices normally encountered
1400 		 * later in the pci scan (e.g. if a fix to device 7 must be
1401 		 * applied before scanning device 6, applying fixes in the
1402 		 * normal enumeration loop would obviously be too late).
1403 		 */
1404 		enumerate_bus_devs(i, CONFIG_FIX);
1405 	}
1406 }
1407 
1408 void
1409 undo_pci_fixes(void)
1410 {
1411 	struct pci_fixundo *nextundo;
1412 	uint8_t bus, dev, fn;
1413 
1414 	/*
1415 	 * All fixes in the undo list are performed unconditionally.  Future
1416 	 * fixes may require selective undo.
1417 	 */
1418 	while (undolist != NULL) {
1419 
1420 		bus = undolist->bus;
1421 		dev = undolist->dev;
1422 		fn = undolist->fn;
1423 
1424 		(*(undolist->undofn))(bus, dev, fn);
1425 
1426 		nextundo = undolist->next;
1427 		kmem_free(undolist, sizeof (struct pci_fixundo));
1428 		undolist = nextundo;
1429 	}
1430 }
1431 
1432 static void
1433 undo_amd8111_pci_fix(uint8_t bus, uint8_t dev, uint8_t fn)
1434 {
1435 	uint8_t val8;
1436 
1437 	val8 = pci_getb(bus, dev, fn, LPC_IO_CONTROL_REG_1);
1438 	/*
1439 	 * The NMIONERR bit is turned back on to allow the SMM BIOS
1440 	 * to handle more critical PCI errors (e.g. PERR#).
1441 	 */
1442 	val8 |= AMD8111_ENABLENMI;
1443 	pci_putb(bus, dev, fn, LPC_IO_CONTROL_REG_1, val8);
1444 }
1445 
1446 static void
1447 pci_fix_amd8111(uint8_t bus, uint8_t dev, uint8_t fn)
1448 {
1449 	uint8_t val8;
1450 
1451 	val8 = pci_getb(bus, dev, fn, LPC_IO_CONTROL_REG_1);
1452 
1453 	if ((val8 & AMD8111_ENABLENMI) == 0)
1454 		return;
1455 
1456 	/*
1457 	 * We reset NMIONERR in the LPC because master-abort on the PCI
1458 	 * bridge side of the 8111 will cause NMI, which might cause SMI,
1459 	 * which sometimes prevents all devices from being enumerated.
1460 	 */
1461 	val8 &= ~AMD8111_ENABLENMI;
1462 
1463 	pci_putb(bus, dev, fn, LPC_IO_CONTROL_REG_1, val8);
1464 
1465 	add_undofix_entry(bus, dev, fn, undo_amd8111_pci_fix);
1466 }
1467 
1468 static void
1469 set_devpm_d0(uchar_t bus, uchar_t dev, uchar_t func)
1470 {
1471 	uint16_t status;
1472 	uint8_t header;
1473 	uint8_t cap_ptr;
1474 	uint8_t cap_id;
1475 	uint16_t pmcsr;
1476 
1477 	status = pci_getw(bus, dev, func, PCI_CONF_STAT);
1478 	if (!(status & PCI_STAT_CAP))
1479 		return;	/* No capabilities list */
1480 
1481 	header = pci_getb(bus, dev, func, PCI_CONF_HEADER) & PCI_HEADER_TYPE_M;
1482 	if (header == PCI_HEADER_CARDBUS)
1483 		cap_ptr = pci_getb(bus, dev, func, PCI_CBUS_CAP_PTR);
1484 	else
1485 		cap_ptr = pci_getb(bus, dev, func, PCI_CONF_CAP_PTR);
1486 	/*
1487 	 * Walk the capabilities list searching for a PM entry.
1488 	 */
1489 	while (cap_ptr != PCI_CAP_NEXT_PTR_NULL && cap_ptr >= PCI_CAP_PTR_OFF) {
1490 		cap_ptr &= PCI_CAP_PTR_MASK;
1491 		cap_id = pci_getb(bus, dev, func, cap_ptr + PCI_CAP_ID);
1492 		if (cap_id == PCI_CAP_ID_PM) {
1493 			pmcsr = pci_getw(bus, dev, func, cap_ptr + PCI_PMCSR);
1494 			pmcsr &= ~(PCI_PMCSR_STATE_MASK);
1495 			pmcsr |= PCI_PMCSR_D0; /* D0 state */
1496 			pci_putw(bus, dev, func, cap_ptr + PCI_PMCSR, pmcsr);
1497 			break;
1498 		}
1499 		cap_ptr = pci_getb(bus, dev, func, cap_ptr + PCI_CAP_NEXT_PTR);
1500 	}
1501 
1502 }
1503 
1504 #define	is_isa(bc, sc)	\
1505 	(((bc) == PCI_CLASS_BRIDGE) && ((sc) == PCI_BRIDGE_ISA))
1506 
1507 static void
1508 process_devfunc(uchar_t bus, uchar_t dev, uchar_t func, uchar_t header,
1509     ushort_t vendorid, int config_op)
1510 {
1511 	char nodename[32], unitaddr[5];
1512 	dev_info_t *dip;
1513 	uchar_t basecl, subcl, progcl, intr, revid;
1514 	ushort_t subvenid, subdevid, status;
1515 	ushort_t slot_num;
1516 	uint_t classcode, revclass;
1517 	int reprogram = 0, pciide = 0;
1518 	int power[2] = {1, 1};
1519 	int pciex = 0;
1520 	ushort_t is_pci_bridge = 0;
1521 	struct pci_devfunc *devlist = NULL, *entry = NULL;
1522 	iommu_private_t *private;
1523 	gfx_entry_t *gfxp;
1524 
1525 	ushort_t deviceid = pci_getw(bus, dev, func, PCI_CONF_DEVID);
1526 
1527 	switch (header & PCI_HEADER_TYPE_M) {
1528 	case PCI_HEADER_ZERO:
1529 		subvenid = pci_getw(bus, dev, func, PCI_CONF_SUBVENID);
1530 		subdevid = pci_getw(bus, dev, func, PCI_CONF_SUBSYSID);
1531 		break;
1532 	case PCI_HEADER_CARDBUS:
1533 		subvenid = pci_getw(bus, dev, func, PCI_CBUS_SUBVENID);
1534 		subdevid = pci_getw(bus, dev, func, PCI_CBUS_SUBSYSID);
1535 		/* Record the # of cardbus bridges found on the bus */
1536 		if (config_op == CONFIG_INFO)
1537 			pci_bus_res[bus].num_cbb++;
1538 		break;
1539 	default:
1540 		subvenid = 0;
1541 		subdevid = 0;
1542 		break;
1543 	}
1544 
1545 	if (config_op == CONFIG_FIX) {
1546 		if (vendorid == VENID_AMD && deviceid == DEVID_AMD8111_LPC) {
1547 			pci_fix_amd8111(bus, dev, func);
1548 		}
1549 		return;
1550 	}
1551 
1552 	/* XXX should be use generic names? derive from class? */
1553 	revclass = pci_getl(bus, dev, func, PCI_CONF_REVID);
1554 	classcode = revclass >> 8;
1555 	revid = revclass & 0xff;
1556 
1557 	/* figure out if this is pci-ide */
1558 	basecl = classcode >> 16;
1559 	subcl = (classcode >> 8) & 0xff;
1560 	progcl = classcode & 0xff;
1561 
1562 
1563 	if (is_display(classcode))
1564 		(void) snprintf(nodename, sizeof (nodename), "display");
1565 	else if (!pseudo_isa && is_isa(basecl, subcl))
1566 		(void) snprintf(nodename, sizeof (nodename), "isa");
1567 	else if (subvenid != 0)
1568 		(void) snprintf(nodename, sizeof (nodename),
1569 		    "pci%x,%x", subvenid, subdevid);
1570 	else
1571 		(void) snprintf(nodename, sizeof (nodename),
1572 		    "pci%x,%x", vendorid, deviceid);
1573 
1574 	/* make sure parent bus dip has been created */
1575 	if (pci_bus_res[bus].dip == NULL)
1576 		create_root_bus_dip(bus);
1577 
1578 	ndi_devi_alloc_sleep(pci_bus_res[bus].dip, nodename,
1579 	    DEVI_SID_NODEID, &dip);
1580 
1581 	if (check_if_device_is_pciex(dip, bus, dev, func, &slot_num,
1582 	    &is_pci_bridge) == B_TRUE)
1583 		pciex = 1;
1584 
1585 	/* add properties */
1586 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, "device-id", deviceid);
1587 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, "vendor-id", vendorid);
1588 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, "revision-id", revid);
1589 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1590 	    "class-code", classcode);
1591 	if (func == 0)
1592 		(void) snprintf(unitaddr, sizeof (unitaddr), "%x", dev);
1593 	else
1594 		(void) snprintf(unitaddr, sizeof (unitaddr),
1595 		    "%x,%x", dev, func);
1596 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1597 	    "unit-address", unitaddr);
1598 
1599 	/* add device_type for display nodes */
1600 	if (is_display(classcode)) {
1601 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1602 		    "device_type", "display");
1603 	}
1604 	/* add special stuff for header type */
1605 	if ((header & PCI_HEADER_TYPE_M) == PCI_HEADER_ZERO) {
1606 		uchar_t mingrant = pci_getb(bus, dev, func, PCI_CONF_MIN_G);
1607 		uchar_t maxlatency = pci_getb(bus, dev, func, PCI_CONF_MAX_L);
1608 
1609 		if (subvenid != 0) {
1610 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1611 			    "subsystem-id", subdevid);
1612 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1613 			    "subsystem-vendor-id", subvenid);
1614 		}
1615 		if (!pciex)
1616 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1617 			    "min-grant", mingrant);
1618 		if (!pciex)
1619 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1620 			    "max-latency", maxlatency);
1621 	}
1622 
1623 	/* interrupt, record if not 0 */
1624 	intr = pci_getb(bus, dev, func, PCI_CONF_IPIN);
1625 	if (intr != 0)
1626 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1627 		    "interrupts", intr);
1628 
1629 	/*
1630 	 * Add support for 133 mhz pci eventually
1631 	 */
1632 	status = pci_getw(bus, dev, func, PCI_CONF_STAT);
1633 
1634 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1635 	    "devsel-speed", (status & PCI_STAT_DEVSELT) >> 9);
1636 	if (!pciex && (status & PCI_STAT_FBBC))
1637 		(void) ndi_prop_create_boolean(DDI_DEV_T_NONE, dip,
1638 		    "fast-back-to-back");
1639 	if (!pciex && (status & PCI_STAT_66MHZ))
1640 		(void) ndi_prop_create_boolean(DDI_DEV_T_NONE, dip,
1641 		    "66mhz-capable");
1642 	if (status & PCI_STAT_UDF)
1643 		(void) ndi_prop_create_boolean(DDI_DEV_T_NONE, dip,
1644 		    "udf-supported");
1645 	if (pciex && slot_num) {
1646 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1647 		    "physical-slot#", slot_num);
1648 		if (!is_pci_bridge)
1649 			pciex_slot_names_prop(dip, slot_num);
1650 	}
1651 
1652 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
1653 	    "power-consumption", power, 2);
1654 
1655 	/* Set the device PM state to D0 */
1656 	set_devpm_d0(bus, dev, func);
1657 
1658 	if ((basecl == PCI_CLASS_BRIDGE) && (subcl == PCI_BRIDGE_PCI))
1659 		add_ppb_props(dip, bus, dev, func, pciex, is_pci_bridge);
1660 	else {
1661 		/*
1662 		 * Record the non-PPB devices on the bus for possible
1663 		 * reprogramming at 2nd bus enumeration.
1664 		 * Note: PPB reprogramming is done in fix_ppb_res()
1665 		 */
1666 		devlist = (struct pci_devfunc *)pci_bus_res[bus].privdata;
1667 		entry = kmem_zalloc(sizeof (*entry), KM_SLEEP);
1668 		entry->dip = dip;
1669 		entry->dev = dev;
1670 		entry->func = func;
1671 		entry->next = devlist;
1672 		pci_bus_res[bus].privdata = entry;
1673 	}
1674 
1675 	if (config_op == CONFIG_INFO &&
1676 	    IS_CLASS_IOAPIC(basecl, subcl, progcl)) {
1677 		create_ioapic_node(bus, dev, func, vendorid, deviceid);
1678 	}
1679 
1680 	/* check for ck8-04 based PCI ISA bridge only */
1681 	if (NVIDIA_IS_LPC_BRIDGE(vendorid, deviceid) && (dev == 1) &&
1682 	    (func == 0))
1683 		add_nvidia_isa_bridge_props(dip, bus, dev, func);
1684 
1685 	if (pciex && is_pci_bridge)
1686 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, "model",
1687 		    (char *)"PCIe-PCI bridge");
1688 	else
1689 		add_model_prop(dip, classcode);
1690 
1691 	add_compatible(dip, subvenid, subdevid, vendorid, deviceid,
1692 	    revid, classcode, pciex);
1693 
1694 	/*
1695 	 * See if this device is a controller that advertises
1696 	 * itself to be a standard ATA task file controller, or one that
1697 	 * has been hard coded.
1698 	 *
1699 	 * If it is, check if any other higher precedence driver listed in
1700 	 * driver_aliases will claim the node by calling
1701 	 * ddi_compatibile_driver_major.  If so, clear pciide and do not
1702 	 * create a pci-ide node or any other special handling.
1703 	 *
1704 	 * If another driver does not bind, set the node name to pci-ide
1705 	 * and then let the special pci-ide handling for registers and
1706 	 * child pci-ide nodes proceed below.
1707 	 */
1708 	if (is_pciide(basecl, subcl, revid, vendorid, deviceid,
1709 	    subvenid, subdevid) == 1) {
1710 		if (ddi_compatible_driver_major(dip, NULL) == (major_t)-1) {
1711 			(void) ndi_devi_set_nodename(dip, "pci-ide", 0);
1712 			pciide = 1;
1713 		}
1714 	}
1715 
1716 	reprogram = add_reg_props(dip, bus, dev, func, config_op, pciide);
1717 	(void) ndi_devi_bind_driver(dip, 0);
1718 
1719 	/* special handling for pci-ide */
1720 	if (pciide) {
1721 		dev_info_t *cdip;
1722 
1723 		/*
1724 		 * Create properties specified by P1275 Working Group
1725 		 * Proposal #414 Version 1
1726 		 */
1727 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1728 		    "device_type", "pci-ide");
1729 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1730 		    "#address-cells", 1);
1731 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1732 		    "#size-cells", 0);
1733 
1734 		/* allocate two child nodes */
1735 		ndi_devi_alloc_sleep(dip, "ide",
1736 		    (pnode_t)DEVI_SID_NODEID, &cdip);
1737 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cdip,
1738 		    "reg", 0);
1739 		(void) ndi_devi_bind_driver(cdip, 0);
1740 		ndi_devi_alloc_sleep(dip, "ide",
1741 		    (pnode_t)DEVI_SID_NODEID, &cdip);
1742 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cdip,
1743 		    "reg", 1);
1744 		(void) ndi_devi_bind_driver(cdip, 0);
1745 
1746 		reprogram = 0;	/* don't reprogram pci-ide bridge */
1747 	}
1748 
1749 	/* allocate and set up iommu private */
1750 	private = kmem_alloc(sizeof (iommu_private_t), KM_SLEEP);
1751 	private->idp_seg = 0;
1752 	private->idp_bus = bus;
1753 	private->idp_devfn = (dev << 3) | func;
1754 	private->idp_sec = 0;
1755 	private->idp_sub = 0;
1756 	private->idp_bbp_type = IOMMU_PPB_NONE;
1757 	/* record the bridge */
1758 	private->idp_is_bridge = ((basecl == PCI_CLASS_BRIDGE) &&
1759 	    (subcl == PCI_BRIDGE_PCI));
1760 	if (private->idp_is_bridge) {
1761 		private->idp_sec = pci_getb(bus, dev, func, PCI_BCNF_SECBUS);
1762 		private->idp_sub = pci_getb(bus, dev, func, PCI_BCNF_SUBBUS);
1763 		if (pciex && is_pci_bridge)
1764 			private->idp_bbp_type = IOMMU_PPB_PCIE_PCI;
1765 		else if (pciex)
1766 			private->idp_bbp_type = IOMMU_PPB_PCIE_PCIE;
1767 		else
1768 			private->idp_bbp_type = IOMMU_PPB_PCI_PCI;
1769 	}
1770 	/* record the special devices */
1771 	private->idp_is_display = (is_display(classcode) ? B_TRUE : B_FALSE);
1772 	private->idp_is_lpc = ((basecl == PCI_CLASS_BRIDGE) &&
1773 	    (subcl == PCI_BRIDGE_ISA));
1774 	private->idp_intel_domain = NULL;
1775 	/* hook the private to dip */
1776 	DEVI(dip)->devi_iommu_private = private;
1777 
1778 	if (private->idp_is_display == B_TRUE) {
1779 		gfxp = kmem_zalloc(sizeof (*gfxp), KM_SLEEP);
1780 		gfxp->g_dip = dip;
1781 		gfxp->g_prev = NULL;
1782 		gfxp->g_next = gfx_devinfo_list;
1783 		gfx_devinfo_list = gfxp;
1784 		if (gfxp->g_next)
1785 			gfxp->g_next->g_prev = gfxp;
1786 	}
1787 
1788 	/* special handling for isa */
1789 	if (!pseudo_isa && is_isa(basecl, subcl)) {
1790 		/* add device_type */
1791 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1792 		    "device_type", "isa");
1793 	}
1794 
1795 	if (reprogram && (entry != NULL))
1796 		entry->reprogram = B_TRUE;
1797 }
1798 
1799 /*
1800  * Some vendors do not use unique subsystem IDs in their products, which
1801  * makes the use of form 2 compatible names (pciSSSS,ssss) inappropriate.
1802  * Allow for these compatible forms to be excluded on a per-device basis.
1803  */
1804 /*ARGSUSED*/
1805 static boolean_t
1806 subsys_compat_exclude(ushort_t venid, ushort_t devid, ushort_t subvenid,
1807     ushort_t subdevid, uchar_t revid, uint_t classcode)
1808 {
1809 	/* Nvidia display adapters */
1810 	if ((venid == 0x10de) && (is_display(classcode)))
1811 		return (B_TRUE);
1812 
1813 	return (B_FALSE);
1814 }
1815 
1816 /*
1817  * Set the compatible property to a value compliant with
1818  * rev 2.1 of the IEEE1275 PCI binding.
1819  * (Also used for PCI-Express devices).
1820  *
1821  *   pciVVVV,DDDD.SSSS.ssss.RR	(0)
1822  *   pciVVVV,DDDD.SSSS.ssss	(1)
1823  *   pciSSSS,ssss		(2)
1824  *   pciVVVV,DDDD.RR		(3)
1825  *   pciVVVV,DDDD		(4)
1826  *   pciclass,CCSSPP		(5)
1827  *   pciclass,CCSS		(6)
1828  *
1829  * The Subsystem (SSSS) forms are not inserted if
1830  * subsystem-vendor-id is 0.
1831  *
1832  * NOTE: For PCI-Express devices "pci" is replaced with "pciex" in 0-6 above
1833  * property 2 is not created as per "1275 bindings for PCI Express Interconnect"
1834  *
1835  * Set with setprop and \x00 between each
1836  * to generate the encoded string array form.
1837  */
1838 void
1839 add_compatible(dev_info_t *dip, ushort_t subvenid, ushort_t subdevid,
1840     ushort_t vendorid, ushort_t deviceid, uchar_t revid, uint_t classcode,
1841     int pciex)
1842 {
1843 	int i = 0;
1844 	int size = COMPAT_BUFSIZE;
1845 	char *compat[13];
1846 	char *buf, *curr;
1847 
1848 	curr = buf = kmem_alloc(size, KM_SLEEP);
1849 
1850 	if (pciex) {
1851 		if (subvenid) {
1852 			compat[i++] = curr;	/* form 0 */
1853 			(void) snprintf(curr, size, "pciex%x,%x.%x.%x.%x",
1854 			    vendorid, deviceid, subvenid, subdevid, revid);
1855 			size -= strlen(curr) + 1;
1856 			curr += strlen(curr) + 1;
1857 
1858 			compat[i++] = curr;	/* form 1 */
1859 			(void) snprintf(curr, size, "pciex%x,%x.%x.%x",
1860 			    vendorid, deviceid, subvenid, subdevid);
1861 			size -= strlen(curr) + 1;
1862 			curr += strlen(curr) + 1;
1863 
1864 		}
1865 		compat[i++] = curr;	/* form 3 */
1866 		(void) snprintf(curr, size, "pciex%x,%x.%x",
1867 		    vendorid, deviceid, revid);
1868 		size -= strlen(curr) + 1;
1869 		curr += strlen(curr) + 1;
1870 
1871 		compat[i++] = curr;	/* form 4 */
1872 		(void) snprintf(curr, size, "pciex%x,%x", vendorid, deviceid);
1873 		size -= strlen(curr) + 1;
1874 		curr += strlen(curr) + 1;
1875 
1876 		compat[i++] = curr;	/* form 5 */
1877 		(void) snprintf(curr, size, "pciexclass,%06x", classcode);
1878 		size -= strlen(curr) + 1;
1879 		curr += strlen(curr) + 1;
1880 
1881 		compat[i++] = curr;	/* form 6 */
1882 		(void) snprintf(curr, size, "pciexclass,%04x",
1883 		    (classcode >> 8));
1884 		size -= strlen(curr) + 1;
1885 		curr += strlen(curr) + 1;
1886 	}
1887 
1888 	if (subvenid) {
1889 		compat[i++] = curr;	/* form 0 */
1890 		(void) snprintf(curr, size, "pci%x,%x.%x.%x.%x",
1891 		    vendorid, deviceid, subvenid, subdevid, revid);
1892 		size -= strlen(curr) + 1;
1893 		curr += strlen(curr) + 1;
1894 
1895 		compat[i++] = curr;	/* form 1 */
1896 		(void) snprintf(curr, size, "pci%x,%x.%x.%x",
1897 		    vendorid, deviceid, subvenid, subdevid);
1898 		size -= strlen(curr) + 1;
1899 		curr += strlen(curr) + 1;
1900 
1901 		if (subsys_compat_exclude(vendorid, deviceid, subvenid,
1902 		    subdevid, revid, classcode) == B_FALSE) {
1903 			compat[i++] = curr;	/* form 2 */
1904 			(void) snprintf(curr, size, "pci%x,%x", subvenid,
1905 			    subdevid);
1906 			size -= strlen(curr) + 1;
1907 			curr += strlen(curr) + 1;
1908 		}
1909 	}
1910 	compat[i++] = curr;	/* form 3 */
1911 	(void) snprintf(curr, size, "pci%x,%x.%x", vendorid, deviceid, revid);
1912 	size -= strlen(curr) + 1;
1913 	curr += strlen(curr) + 1;
1914 
1915 	compat[i++] = curr;	/* form 4 */
1916 	(void) snprintf(curr, size, "pci%x,%x", vendorid, deviceid);
1917 	size -= strlen(curr) + 1;
1918 	curr += strlen(curr) + 1;
1919 
1920 	compat[i++] = curr;	/* form 5 */
1921 	(void) snprintf(curr, size, "pciclass,%06x", classcode);
1922 	size -= strlen(curr) + 1;
1923 	curr += strlen(curr) + 1;
1924 
1925 	compat[i++] = curr;	/* form 6 */
1926 	(void) snprintf(curr, size, "pciclass,%04x", (classcode >> 8));
1927 	size -= strlen(curr) + 1;
1928 	curr += strlen(curr) + 1;
1929 
1930 	(void) ndi_prop_update_string_array(DDI_DEV_T_NONE, dip,
1931 	    "compatible", compat, i);
1932 	kmem_free(buf, COMPAT_BUFSIZE);
1933 }
1934 
1935 /*
1936  * Adjust the reg properties for a dual channel PCI-IDE device.
1937  *
1938  * NOTE: don't do anything that changes the order of the hard-decodes
1939  * and programmed BARs. The kernel driver depends on these values
1940  * being in this order regardless of whether they're for a 'native'
1941  * mode BAR or not.
1942  */
1943 /*
1944  * config info for pci-ide devices
1945  */
1946 static struct {
1947 	uchar_t  native_mask;	/* 0 == 'compatibility' mode, 1 == native */
1948 	uchar_t  bar_offset;	/* offset for alt status register */
1949 	ushort_t addr;		/* compatibility mode base address */
1950 	ushort_t length;	/* number of ports for this BAR */
1951 } pciide_bar[] = {
1952 	{ 0x01, 0, 0x1f0, 8 },	/* primary lower BAR */
1953 	{ 0x01, 2, 0x3f6, 1 },	/* primary upper BAR */
1954 	{ 0x04, 0, 0x170, 8 },	/* secondary lower BAR */
1955 	{ 0x04, 2, 0x376, 1 }	/* secondary upper BAR */
1956 };
1957 
1958 static int
1959 pciIdeAdjustBAR(uchar_t progcl, int index, uint_t *basep, uint_t *lenp)
1960 {
1961 	int hard_decode = 0;
1962 
1963 	/*
1964 	 * Adjust the base and len for the BARs of the PCI-IDE
1965 	 * device's primary and secondary controllers. The first
1966 	 * two BARs are for the primary controller and the next
1967 	 * two BARs are for the secondary controller. The fifth
1968 	 * and sixth bars are never adjusted.
1969 	 */
1970 	if (index >= 0 && index <= 3) {
1971 		*lenp = pciide_bar[index].length;
1972 
1973 		if (progcl & pciide_bar[index].native_mask) {
1974 			*basep += pciide_bar[index].bar_offset;
1975 		} else {
1976 			*basep = pciide_bar[index].addr;
1977 			hard_decode = 1;
1978 		}
1979 	}
1980 
1981 	/*
1982 	 * if either base or len is zero make certain both are zero
1983 	 */
1984 	if (*basep == 0 || *lenp == 0) {
1985 		*basep = 0;
1986 		*lenp = 0;
1987 		hard_decode = 0;
1988 	}
1989 
1990 	return (hard_decode);
1991 }
1992 
1993 
1994 /*
1995  * Add the "reg" and "assigned-addresses" property
1996  */
1997 static int
1998 add_reg_props(dev_info_t *dip, uchar_t bus, uchar_t dev, uchar_t func,
1999     int config_op, int pciide)
2000 {
2001 	uchar_t baseclass, subclass, progclass, header;
2002 	ushort_t bar_sz;
2003 	uint_t value = 0, len, devloc;
2004 	uint_t base, base_hi, type;
2005 	ushort_t offset, end;
2006 	int max_basereg, j, reprogram = 0;
2007 	uint_t phys_hi;
2008 	struct memlist **io_avail, **io_used;
2009 	struct memlist **mem_avail, **mem_used;
2010 	struct memlist **pmem_avail, **pmem_used;
2011 	uchar_t res_bus;
2012 
2013 	pci_regspec_t regs[16] = {{0}};
2014 	pci_regspec_t assigned[15] = {{0}};
2015 	int nreg, nasgn;
2016 
2017 	io_avail = &pci_bus_res[bus].io_avail;
2018 	io_used = &pci_bus_res[bus].io_used;
2019 	mem_avail = &pci_bus_res[bus].mem_avail;
2020 	mem_used = &pci_bus_res[bus].mem_used;
2021 	pmem_avail = &pci_bus_res[bus].pmem_avail;
2022 	pmem_used = &pci_bus_res[bus].pmem_used;
2023 
2024 	devloc = (uint_t)bus << 16 | (uint_t)dev << 11 | (uint_t)func << 8;
2025 	regs[0].pci_phys_hi = devloc;
2026 	nreg = 1;	/* rest of regs[0] is all zero */
2027 	nasgn = 0;
2028 
2029 	baseclass = pci_getb(bus, dev, func, PCI_CONF_BASCLASS);
2030 	subclass = pci_getb(bus, dev, func, PCI_CONF_SUBCLASS);
2031 	progclass = pci_getb(bus, dev, func, PCI_CONF_PROGCLASS);
2032 	header = pci_getb(bus, dev, func, PCI_CONF_HEADER) & PCI_HEADER_TYPE_M;
2033 
2034 	switch (header) {
2035 	case PCI_HEADER_ZERO:
2036 		max_basereg = PCI_BASE_NUM;
2037 		break;
2038 	case PCI_HEADER_PPB:
2039 		max_basereg = PCI_BCNF_BASE_NUM;
2040 		break;
2041 	case PCI_HEADER_CARDBUS:
2042 		max_basereg = PCI_CBUS_BASE_NUM;
2043 		reprogram = 1;
2044 		break;
2045 	default:
2046 		max_basereg = 0;
2047 		break;
2048 	}
2049 
2050 	/*
2051 	 * Create the register property by saving the current
2052 	 * value of the base register. Write 0xffffffff to the
2053 	 * base register.  Read the value back to determine the
2054 	 * required size of the address space.  Restore the base
2055 	 * register contents.
2056 	 *
2057 	 * Do not disable I/O and memory access; this isn't necessary
2058 	 * since no driver is yet attached to this device, and disabling
2059 	 * I/O and memory access has the side-effect of disabling PCI-PCI
2060 	 * bridge mappings, which makes the bridge transparent to secondary-
2061 	 * bus activity (see sections 4.1-4.3 of the PCI-PCI Bridge
2062 	 * Spec V1.2).
2063 	 */
2064 	end = PCI_CONF_BASE0 + max_basereg * sizeof (uint_t);
2065 	for (j = 0, offset = PCI_CONF_BASE0; offset < end;
2066 	    j++, offset += bar_sz) {
2067 		/* determine the size of the address space */
2068 		base = pci_getl(bus, dev, func, offset);
2069 		pci_putl(bus, dev, func, offset, 0xffffffff);
2070 		value = pci_getl(bus, dev, func, offset);
2071 		pci_putl(bus, dev, func, offset, base);
2072 
2073 		/* construct phys hi,med.lo, size hi, lo */
2074 		if ((pciide && j < 4) || (base & PCI_BASE_SPACE_IO)) {
2075 			int hard_decode = 0;
2076 
2077 			/* i/o space */
2078 			bar_sz = PCI_BAR_SZ_32;
2079 			value &= PCI_BASE_IO_ADDR_M;
2080 			len = ((value ^ (value-1)) + 1) >> 1;
2081 
2082 			/* XXX Adjust first 4 IDE registers */
2083 			if (pciide) {
2084 				if (subclass != PCI_MASS_IDE)
2085 					progclass = (PCI_IDE_IF_NATIVE_PRI |
2086 					    PCI_IDE_IF_NATIVE_SEC);
2087 				hard_decode = pciIdeAdjustBAR(progclass, j,
2088 				    &base, &len);
2089 			} else if (value == 0) {
2090 				/* skip base regs with size of 0 */
2091 				continue;
2092 			}
2093 
2094 			regs[nreg].pci_phys_hi = PCI_ADDR_IO | devloc |
2095 			    (hard_decode ? PCI_RELOCAT_B : offset);
2096 			regs[nreg].pci_phys_low = hard_decode ?
2097 			    base & PCI_BASE_IO_ADDR_M : 0;
2098 			assigned[nasgn].pci_phys_hi =
2099 			    PCI_RELOCAT_B | regs[nreg].pci_phys_hi;
2100 			regs[nreg].pci_size_low =
2101 			    assigned[nasgn].pci_size_low = len;
2102 			type = base & (~PCI_BASE_IO_ADDR_M);
2103 			base &= PCI_BASE_IO_ADDR_M;
2104 			/*
2105 			 * A device under a subtractive PPB can allocate
2106 			 * resources from its parent bus if there is no resource
2107 			 * available on its own bus.
2108 			 */
2109 			if ((config_op == CONFIG_NEW) && (*io_avail == NULL)) {
2110 				res_bus = bus;
2111 				while (pci_bus_res[res_bus].subtractive) {
2112 					res_bus = pci_bus_res[res_bus].par_bus;
2113 					if (res_bus == (uchar_t)-1)
2114 						break; /* root bus already */
2115 					if (pci_bus_res[res_bus].io_avail) {
2116 						io_avail = &pci_bus_res
2117 						    [res_bus].io_avail;
2118 						break;
2119 					}
2120 				}
2121 			}
2122 
2123 			/*
2124 			 * first pass - gather what's there
2125 			 * update/second pass - adjust/allocate regions
2126 			 *	config - allocate regions
2127 			 */
2128 			if (config_op == CONFIG_INFO) {	/* first pass */
2129 				/* take out of the resource map of the bus */
2130 				if (base != 0) {
2131 					(void) memlist_remove(io_avail, base,
2132 					    len);
2133 					memlist_insert(io_used, base, len);
2134 				} else {
2135 					reprogram = 1;
2136 				}
2137 				pci_bus_res[bus].io_size += len;
2138 			} else if ((*io_avail && base == 0) ||
2139 			    pci_bus_res[bus].io_reprogram) {
2140 				base = (uint_t)memlist_find(io_avail, len, len);
2141 				if (base != 0) {
2142 					memlist_insert(io_used, base, len);
2143 					/* XXX need to worry about 64-bit? */
2144 					pci_putl(bus, dev, func, offset,
2145 					    base | type);
2146 					base = pci_getl(bus, dev, func, offset);
2147 					base &= PCI_BASE_IO_ADDR_M;
2148 				}
2149 				if (base == 0) {
2150 					cmn_err(CE_WARN, "failed to program"
2151 					    " IO space [%d/%d/%d] BAR@0x%x"
2152 					    " length 0x%x",
2153 					    bus, dev, func, offset, len);
2154 				}
2155 			}
2156 			assigned[nasgn].pci_phys_low = base;
2157 			nreg++, nasgn++;
2158 
2159 		} else {
2160 			/* memory space */
2161 			if ((base & PCI_BASE_TYPE_M) == PCI_BASE_TYPE_ALL) {
2162 				bar_sz = PCI_BAR_SZ_64;
2163 				base_hi = pci_getl(bus, dev, func, offset + 4);
2164 				phys_hi = PCI_ADDR_MEM64;
2165 			} else {
2166 				bar_sz = PCI_BAR_SZ_32;
2167 				base_hi = 0;
2168 				phys_hi = PCI_ADDR_MEM32;
2169 			}
2170 
2171 			/* skip base regs with size of 0 */
2172 			value &= PCI_BASE_M_ADDR_M;
2173 
2174 			if (value == 0)
2175 				continue;
2176 
2177 			len = ((value ^ (value-1)) + 1) >> 1;
2178 			regs[nreg].pci_size_low =
2179 			    assigned[nasgn].pci_size_low = len;
2180 
2181 			phys_hi |= (devloc | offset);
2182 			if (base & PCI_BASE_PREF_M)
2183 				phys_hi |= PCI_PREFETCH_B;
2184 
2185 			/*
2186 			 * A device under a subtractive PPB can allocate
2187 			 * resources from its parent bus if there is no resource
2188 			 * available on its own bus.
2189 			 */
2190 			if ((config_op == CONFIG_NEW) && (*mem_avail == NULL)) {
2191 				res_bus = bus;
2192 				while (pci_bus_res[res_bus].subtractive) {
2193 					res_bus = pci_bus_res[res_bus].par_bus;
2194 					if (res_bus == (uchar_t)-1)
2195 						break; /* root bus already */
2196 					mem_avail =
2197 					    &pci_bus_res[res_bus].mem_avail;
2198 					pmem_avail =
2199 					    &pci_bus_res [res_bus].pmem_avail;
2200 					/*
2201 					 * Break out as long as at least
2202 					 * mem_avail is available
2203 					 */
2204 					if ((*pmem_avail &&
2205 					    (phys_hi & PCI_PREFETCH_B)) ||
2206 					    *mem_avail)
2207 						break;
2208 				}
2209 			}
2210 
2211 			regs[nreg].pci_phys_hi =
2212 			    assigned[nasgn].pci_phys_hi = phys_hi;
2213 			assigned[nasgn].pci_phys_hi |= PCI_RELOCAT_B;
2214 			assigned[nasgn].pci_phys_mid = base_hi;
2215 			type = base & ~PCI_BASE_M_ADDR_M;
2216 			base &= PCI_BASE_M_ADDR_M;
2217 
2218 			if (config_op == CONFIG_INFO) {
2219 				/* take out of the resource map of the bus */
2220 				if (base != NULL) {
2221 					/* remove from PMEM and MEM space */
2222 					(void) memlist_remove(mem_avail,
2223 					    base, len);
2224 					(void) memlist_remove(pmem_avail,
2225 					    base, len);
2226 					/* only note as used in correct map */
2227 					if (phys_hi & PCI_PREFETCH_B)
2228 						memlist_insert(pmem_used,
2229 						    base, len);
2230 					else
2231 						memlist_insert(mem_used,
2232 						    base, len);
2233 				} else {
2234 					reprogram = 1;
2235 				}
2236 				pci_bus_res[bus].mem_size += len;
2237 			} else if ((*mem_avail && base == NULL) ||
2238 			    pci_bus_res[bus].mem_reprogram) {
2239 				/*
2240 				 * When desired, attempt a prefetchable
2241 				 * allocation first
2242 				 */
2243 				if (phys_hi & PCI_PREFETCH_B) {
2244 					base = (uint_t)memlist_find(pmem_avail,
2245 					    len, len);
2246 					if (base != NULL) {
2247 						memlist_insert(pmem_used,
2248 						    base, len);
2249 						(void) memlist_remove(mem_avail,
2250 						    base, len);
2251 					}
2252 				}
2253 				/*
2254 				 * If prefetchable allocation was not
2255 				 * desired, or failed, attempt ordinary
2256 				 * memory allocation
2257 				 */
2258 				if (base == NULL) {
2259 					base = (uint_t)memlist_find(mem_avail,
2260 					    len, len);
2261 					if (base != NULL) {
2262 						memlist_insert(mem_used,
2263 						    base, len);
2264 						(void) memlist_remove(
2265 						    pmem_avail, base, len);
2266 					}
2267 				}
2268 				if (base != NULL) {
2269 					pci_putl(bus, dev, func, offset,
2270 					    base | type);
2271 					base = pci_getl(bus, dev, func, offset);
2272 					base &= PCI_BASE_M_ADDR_M;
2273 				} else
2274 					cmn_err(CE_WARN, "failed to program "
2275 					    "mem space [%d/%d/%d] BAR@0x%x"
2276 					    " length 0x%x",
2277 					    bus, dev, func, offset, len);
2278 			}
2279 			assigned[nasgn].pci_phys_low = base;
2280 			nreg++, nasgn++;
2281 		}
2282 	}
2283 	switch (header) {
2284 	case PCI_HEADER_ZERO:
2285 		offset = PCI_CONF_ROM;
2286 		break;
2287 	case PCI_HEADER_PPB:
2288 		offset = PCI_BCNF_ROM;
2289 		break;
2290 	default: /* including PCI_HEADER_CARDBUS */
2291 		goto done;
2292 	}
2293 
2294 	/*
2295 	 * Add the expansion rom memory space
2296 	 * Determine the size of the ROM base reg; don't write reserved bits
2297 	 * ROM isn't in the PCI memory space.
2298 	 */
2299 	base = pci_getl(bus, dev, func, offset);
2300 	pci_putl(bus, dev, func, offset, PCI_BASE_ROM_ADDR_M);
2301 	value = pci_getl(bus, dev, func, offset);
2302 	pci_putl(bus, dev, func, offset, base);
2303 	if (value & PCI_BASE_ROM_ENABLE)
2304 		value &= PCI_BASE_ROM_ADDR_M;
2305 	else
2306 		value = 0;
2307 
2308 	if (value != 0) {
2309 		regs[nreg].pci_phys_hi = (PCI_ADDR_MEM32 | devloc) + offset;
2310 		assigned[nasgn].pci_phys_hi = (PCI_RELOCAT_B |
2311 		    PCI_ADDR_MEM32 | devloc) + offset;
2312 		base &= PCI_BASE_ROM_ADDR_M;
2313 		assigned[nasgn].pci_phys_low = base;
2314 		len = ((value ^ (value-1)) + 1) >> 1;
2315 		regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = len;
2316 		nreg++, nasgn++;
2317 		/* take it out of the memory resource */
2318 		if (base != NULL) {
2319 			(void) memlist_remove(mem_avail, base, len);
2320 			memlist_insert(mem_used, base, len);
2321 			pci_bus_res[bus].mem_size += len;
2322 		}
2323 	}
2324 
2325 	/*
2326 	 * Account for "legacy" (alias) video adapter resources
2327 	 */
2328 
2329 	/* add the three hard-decode, aliased address spaces for VGA */
2330 	if ((baseclass == PCI_CLASS_DISPLAY && subclass == PCI_DISPLAY_VGA) ||
2331 	    (baseclass == PCI_CLASS_NONE && subclass == PCI_NONE_VGA)) {
2332 
2333 		/* VGA hard decode 0x3b0-0x3bb */
2334 		regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi =
2335 		    (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc);
2336 		regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x3b0;
2337 		regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0xc;
2338 		nreg++, nasgn++;
2339 		(void) memlist_remove(io_avail, 0x3b0, 0xc);
2340 		memlist_insert(io_used, 0x3b0, 0xc);
2341 		pci_bus_res[bus].io_size += 0xc;
2342 
2343 		/* VGA hard decode 0x3c0-0x3df */
2344 		regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi =
2345 		    (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc);
2346 		regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x3c0;
2347 		regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0x20;
2348 		nreg++, nasgn++;
2349 		(void) memlist_remove(io_avail, 0x3c0, 0x20);
2350 		memlist_insert(io_used, 0x3c0, 0x20);
2351 		pci_bus_res[bus].io_size += 0x20;
2352 
2353 		/* Video memory */
2354 		regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi =
2355 		    (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_MEM32 | devloc);
2356 		regs[nreg].pci_phys_low =
2357 		    assigned[nasgn].pci_phys_low = 0xa0000;
2358 		regs[nreg].pci_size_low =
2359 		    assigned[nasgn].pci_size_low = 0x20000;
2360 		nreg++, nasgn++;
2361 		/* remove from MEM and PMEM space */
2362 		(void) memlist_remove(mem_avail, 0xa0000, 0x20000);
2363 		(void) memlist_remove(pmem_avail, 0xa0000, 0x20000);
2364 		memlist_insert(mem_used, 0xa0000, 0x20000);
2365 		pci_bus_res[bus].mem_size += 0x20000;
2366 	}
2367 
2368 	/* add the hard-decode, aliased address spaces for 8514 */
2369 	if ((baseclass == PCI_CLASS_DISPLAY) &&
2370 	    (subclass == PCI_DISPLAY_VGA) &&
2371 	    (progclass & PCI_DISPLAY_IF_8514)) {
2372 
2373 		/* hard decode 0x2e8 */
2374 		regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi =
2375 		    (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc);
2376 		regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x2e8;
2377 		regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0x1;
2378 		nreg++, nasgn++;
2379 		(void) memlist_remove(io_avail, 0x2e8, 0x1);
2380 		memlist_insert(io_used, 0x2e8, 0x1);
2381 		pci_bus_res[bus].io_size += 0x1;
2382 
2383 		/* hard decode 0x2ea-0x2ef */
2384 		regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi =
2385 		    (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc);
2386 		regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x2ea;
2387 		regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0x6;
2388 		nreg++, nasgn++;
2389 		(void) memlist_remove(io_avail, 0x2ea, 0x6);
2390 		memlist_insert(io_used, 0x2ea, 0x6);
2391 		pci_bus_res[bus].io_size += 0x6;
2392 	}
2393 
2394 done:
2395 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "reg",
2396 	    (int *)regs, nreg * sizeof (pci_regspec_t) / sizeof (int));
2397 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
2398 	    "assigned-addresses",
2399 	    (int *)assigned, nasgn * sizeof (pci_regspec_t) / sizeof (int));
2400 
2401 	return (reprogram);
2402 }
2403 
2404 static void
2405 add_ppb_props(dev_info_t *dip, uchar_t bus, uchar_t dev, uchar_t func,
2406     int pciex, ushort_t is_pci_bridge)
2407 {
2408 	char *dev_type;
2409 	int i;
2410 	uint_t val, io_range[2], mem_range[2], pmem_range[2];
2411 	uchar_t secbus = pci_getb(bus, dev, func, PCI_BCNF_SECBUS);
2412 	uchar_t subbus = pci_getb(bus, dev, func, PCI_BCNF_SUBBUS);
2413 	uchar_t progclass;
2414 
2415 	ASSERT(secbus <= subbus);
2416 
2417 	/*
2418 	 * Check if it's a subtractive PPB.
2419 	 */
2420 	progclass = pci_getb(bus, dev, func, PCI_CONF_PROGCLASS);
2421 	if (progclass == PCI_BRIDGE_PCI_IF_SUBDECODE)
2422 		pci_bus_res[secbus].subtractive = B_TRUE;
2423 
2424 	/*
2425 	 * Some BIOSes lie about max pci busses, we allow for
2426 	 * such mistakes here
2427 	 */
2428 	if (subbus > pci_bios_nbus) {
2429 		pci_bios_nbus = subbus;
2430 		alloc_res_array();
2431 	}
2432 
2433 	ASSERT(pci_bus_res[secbus].dip == NULL);
2434 	pci_bus_res[secbus].dip = dip;
2435 	pci_bus_res[secbus].par_bus = bus;
2436 
2437 	dev_type = (pciex && !is_pci_bridge) ? "pciex" : "pci";
2438 
2439 	/* setup bus number hierarchy */
2440 	pci_bus_res[secbus].sub_bus = subbus;
2441 	/*
2442 	 * Keep track of the largest subordinate bus number (this is essential
2443 	 * for peer busses because there is no other way of determining its
2444 	 * subordinate bus number).
2445 	 */
2446 	if (subbus > pci_bus_res[bus].sub_bus)
2447 		pci_bus_res[bus].sub_bus = subbus;
2448 	/*
2449 	 * Loop through subordinate busses, initializing their parent bus
2450 	 * field to this bridge's parent.  The subordinate busses' parent
2451 	 * fields may very well be further refined later, as child bridges
2452 	 * are enumerated.  (The value is to note that the subordinate busses
2453 	 * are not peer busses by changing their par_bus fields to anything
2454 	 * other than -1.)
2455 	 */
2456 	for (i = secbus + 1; i <= subbus; i++)
2457 		pci_bus_res[i].par_bus = bus;
2458 
2459 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
2460 	    "device_type", dev_type);
2461 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
2462 	    "#address-cells", 3);
2463 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
2464 	    "#size-cells", 2);
2465 
2466 	/*
2467 	 * Collect bridge window specifications, and use them to populate
2468 	 * the "avail" resources for the bus.  Not all of those resources will
2469 	 * end up being available; this is done top-down, and so the initial
2470 	 * collection of windows populates the 'ranges' property for the
2471 	 * bus node.  Later, as children are found, resources are removed from
2472 	 * the 'avail' list, so that it becomes the freelist for
2473 	 * this point in the tree.  ranges may be set again after bridge
2474 	 * reprogramming in fix_ppb_res(), in which case it's set from
2475 	 * used + avail.
2476 	 *
2477 	 * According to PPB spec, the base register should be programmed
2478 	 * with a value bigger than the limit register when there are
2479 	 * no resources available. This applies to io, memory, and
2480 	 * prefetchable memory.
2481 	 */
2482 
2483 	/*
2484 	 * io range
2485 	 * We determine i/o windows that are left unconfigured by BIOS
2486 	 * through its i/o enable bit as Microsoft recommends OEMs to do.
2487 	 * If it is unset, we disable i/o and mark it for reconfiguration in
2488 	 * later passes by setting the base > limit
2489 	 */
2490 	val = (uint_t)pci_getw(bus, dev, func, PCI_CONF_COMM);
2491 	if (val & PCI_COMM_IO) {
2492 		val = (uint_t)pci_getb(bus, dev, func, PCI_BCNF_IO_BASE_LOW);
2493 		io_range[0] = ((val & 0xf0) << 8);
2494 		val = (uint_t)pci_getb(bus, dev, func, PCI_BCNF_IO_LIMIT_LOW);
2495 		io_range[1]  = ((val & 0xf0) << 8) | 0xFFF;
2496 	} else {
2497 		io_range[0] = 0x9fff;
2498 		io_range[1] = 0x1000;
2499 		pci_putb(bus, dev, func, PCI_BCNF_IO_BASE_LOW,
2500 		    (uint8_t)((io_range[0] >> 8) & 0xf0));
2501 		pci_putb(bus, dev, func, PCI_BCNF_IO_LIMIT_LOW,
2502 		    (uint8_t)((io_range[1] >> 8) & 0xf0));
2503 		pci_putw(bus, dev, func, PCI_BCNF_IO_BASE_HI, 0);
2504 		pci_putw(bus, dev, func, PCI_BCNF_IO_LIMIT_HI, 0);
2505 	}
2506 
2507 	if (io_range[0] != 0 && io_range[0] < io_range[1]) {
2508 		memlist_insert(&pci_bus_res[secbus].io_avail,
2509 		    (uint64_t)io_range[0],
2510 		    (uint64_t)(io_range[1] - io_range[0] + 1));
2511 		memlist_insert(&pci_bus_res[bus].io_used,
2512 		    (uint64_t)io_range[0],
2513 		    (uint64_t)(io_range[1] - io_range[0] + 1));
2514 		if (pci_bus_res[bus].io_avail != NULL) {
2515 			(void) memlist_remove(&pci_bus_res[bus].io_avail,
2516 			    (uint64_t)io_range[0],
2517 			    (uint64_t)(io_range[1] - io_range[0] + 1));
2518 		}
2519 		dcmn_err(CE_NOTE, "bus %d io-range: 0x%x-%x",
2520 		    secbus, io_range[0], io_range[1]);
2521 		/* if 32-bit supported, make sure upper bits are not set */
2522 		if ((val & 0xf) == 1 &&
2523 		    pci_getw(bus, dev, func, PCI_BCNF_IO_BASE_HI)) {
2524 			cmn_err(CE_NOTE, "unsupported 32-bit IO address on"
2525 			    " pci-pci bridge [%d/%d/%d]", bus, dev, func);
2526 		}
2527 	}
2528 
2529 	/* mem range */
2530 	val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_MEM_BASE);
2531 	mem_range[0] = ((val & 0xFFF0) << 16);
2532 	val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_MEM_LIMIT);
2533 	mem_range[1] = ((val & 0xFFF0) << 16) | 0xFFFFF;
2534 	if (mem_range[0] != 0 && mem_range[0] < mem_range[1]) {
2535 		memlist_insert(&pci_bus_res[secbus].mem_avail,
2536 		    (uint64_t)mem_range[0],
2537 		    (uint64_t)(mem_range[1] - mem_range[0] + 1));
2538 		memlist_insert(&pci_bus_res[bus].mem_used,
2539 		    (uint64_t)mem_range[0],
2540 		    (uint64_t)(mem_range[1] - mem_range[0] + 1));
2541 		/* remove from parent resource list */
2542 		(void) memlist_remove(&pci_bus_res[bus].mem_avail,
2543 		    (uint64_t)mem_range[0],
2544 		    (uint64_t)(mem_range[1] - mem_range[0] + 1));
2545 		(void) memlist_remove(&pci_bus_res[bus].pmem_avail,
2546 		    (uint64_t)mem_range[0],
2547 		    (uint64_t)(mem_range[1] - mem_range[0] + 1));
2548 		dcmn_err(CE_NOTE, "bus %d mem-range: 0x%x-%x",
2549 		    secbus, mem_range[0], mem_range[1]);
2550 	}
2551 
2552 	/* prefetchable memory range */
2553 	val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_PF_BASE_LOW);
2554 	pmem_range[0] = ((val & 0xFFF0) << 16);
2555 	val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_PF_LIMIT_LOW);
2556 	pmem_range[1] = ((val & 0xFFF0) << 16) | 0xFFFFF;
2557 	if (pmem_range[0] != 0 && pmem_range[0] < pmem_range[1]) {
2558 		memlist_insert(&pci_bus_res[secbus].pmem_avail,
2559 		    (uint64_t)pmem_range[0],
2560 		    (uint64_t)(pmem_range[1] - pmem_range[0] + 1));
2561 		memlist_insert(&pci_bus_res[bus].pmem_used,
2562 		    (uint64_t)pmem_range[0],
2563 		    (uint64_t)(pmem_range[1] - pmem_range[0] + 1));
2564 		/* remove from parent resource list */
2565 		(void) memlist_remove(&pci_bus_res[bus].pmem_avail,
2566 		    (uint64_t)pmem_range[0],
2567 		    (uint64_t)(pmem_range[1] - pmem_range[0] + 1));
2568 		(void) memlist_remove(&pci_bus_res[bus].mem_avail,
2569 		    (uint64_t)pmem_range[0],
2570 		    (uint64_t)(pmem_range[1] - pmem_range[0] + 1));
2571 		dcmn_err(CE_NOTE, "bus %d pmem-range: 0x%x-%x",
2572 		    secbus, pmem_range[0], pmem_range[1]);
2573 		/* if 64-bit supported, make sure upper bits are not set */
2574 		if ((val & 0xf) == 1 &&
2575 		    pci_getl(bus, dev, func, PCI_BCNF_PF_BASE_HIGH)) {
2576 			cmn_err(CE_NOTE, "unsupported 64-bit prefetch memory on"
2577 			    " pci-pci bridge [%d/%d/%d]", bus, dev, func);
2578 		}
2579 	}
2580 
2581 	/*
2582 	 * Add VGA legacy resources to the bridge's pci_bus_res if it
2583 	 * has VGA_ENABLE set.  Note that we put them in 'avail',
2584 	 * because that's used to populate the ranges prop; they'll be
2585 	 * removed from there by the VGA device once it's found.  Also,
2586 	 * remove them from the parent's available list and note them as
2587 	 * used in the parent.
2588 	 */
2589 
2590 	if (pci_getw(bus, dev, func, PCI_BCNF_BCNTRL) &
2591 	    PCI_BCNF_BCNTRL_VGA_ENABLE) {
2592 
2593 		memlist_insert(&pci_bus_res[secbus].io_avail, 0x3b0, 0xc);
2594 
2595 		memlist_insert(&pci_bus_res[bus].io_used, 0x3b0, 0xc);
2596 		if (pci_bus_res[bus].io_avail != NULL) {
2597 			(void) memlist_remove(&pci_bus_res[bus].io_avail,
2598 			    0x3b0, 0xc);
2599 		}
2600 
2601 		memlist_insert(&pci_bus_res[secbus].io_avail, 0x3c0, 0x20);
2602 
2603 		memlist_insert(&pci_bus_res[bus].io_used, 0x3c0, 0x20);
2604 		if (pci_bus_res[bus].io_avail != NULL) {
2605 			(void) memlist_remove(&pci_bus_res[bus].io_avail,
2606 			    0x3c0, 0x20);
2607 		}
2608 
2609 		memlist_insert(&pci_bus_res[secbus].mem_avail, 0xa0000,
2610 		    0x20000);
2611 
2612 		memlist_insert(&pci_bus_res[bus].mem_used, 0xa0000, 0x20000);
2613 		if (pci_bus_res[bus].mem_avail != NULL) {
2614 			(void) memlist_remove(&pci_bus_res[bus].mem_avail,
2615 			    0xa0000, 0x20000);
2616 		}
2617 	}
2618 	add_bus_range_prop(secbus);
2619 	add_ranges_prop(secbus, 1);
2620 }
2621 
2622 extern const struct pci_class_strings_s class_pci[];
2623 extern int class_pci_items;
2624 
2625 static void
2626 add_model_prop(dev_info_t *dip, uint_t classcode)
2627 {
2628 	const char *desc;
2629 	int i;
2630 	uchar_t baseclass = classcode >> 16;
2631 	uchar_t subclass = (classcode >> 8) & 0xff;
2632 	uchar_t progclass = classcode & 0xff;
2633 
2634 	if ((baseclass == PCI_CLASS_MASS) && (subclass == PCI_MASS_IDE)) {
2635 		desc = "IDE controller";
2636 	} else {
2637 		for (desc = 0, i = 0; i < class_pci_items; i++) {
2638 			if ((baseclass == class_pci[i].base_class) &&
2639 			    (subclass == class_pci[i].sub_class) &&
2640 			    (progclass == class_pci[i].prog_class)) {
2641 				desc = class_pci[i].actual_desc;
2642 				break;
2643 			}
2644 		}
2645 		if (i == class_pci_items)
2646 			desc = "Unknown class of pci/pnpbios device";
2647 	}
2648 
2649 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, "model",
2650 	    (char *)desc);
2651 }
2652 
2653 static void
2654 add_bus_range_prop(int bus)
2655 {
2656 	int bus_range[2];
2657 
2658 	if (pci_bus_res[bus].dip == NULL)
2659 		return;
2660 	bus_range[0] = bus;
2661 	bus_range[1] = pci_bus_res[bus].sub_bus;
2662 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, pci_bus_res[bus].dip,
2663 	    "bus-range", (int *)bus_range, 2);
2664 }
2665 
2666 /*
2667  * Add slot-names property for any named pci hot-plug slots
2668  */
2669 static void
2670 add_bus_slot_names_prop(int bus)
2671 {
2672 	char slotprop[256];
2673 	int len;
2674 
2675 	if (pci_bus_res[bus].dip != NULL) {
2676 		/* simply return if the property is already defined */
2677 		if (ddi_prop_exists(DDI_DEV_T_ANY, pci_bus_res[bus].dip,
2678 		    DDI_PROP_DONTPASS, "slot-names"))
2679 			return;
2680 	}
2681 
2682 	len = pci_slot_names_prop(bus, slotprop, sizeof (slotprop));
2683 	if (len > 0) {
2684 		/*
2685 		 * Only create a peer bus node if this bus may be a peer bus.
2686 		 * It may be a peer bus if the dip is NULL and if par_bus is
2687 		 * -1 (par_bus is -1 if this bus was not found to be
2688 		 * subordinate to any PCI-PCI bridge).
2689 		 * If it's not a peer bus, then the ACPI BBN-handling code
2690 		 * will remove it later.
2691 		 */
2692 		if (pci_bus_res[bus].par_bus == (uchar_t)-1 &&
2693 		    pci_bus_res[bus].dip == NULL) {
2694 
2695 			create_root_bus_dip(bus);
2696 		}
2697 		if (pci_bus_res[bus].dip != NULL) {
2698 			ASSERT((len % sizeof (int)) == 0);
2699 			(void) ndi_prop_update_int_array(DDI_DEV_T_NONE,
2700 			    pci_bus_res[bus].dip, "slot-names",
2701 			    (int *)slotprop, len / sizeof (int));
2702 		} else {
2703 			cmn_err(CE_NOTE, "!BIOS BUG: Invalid bus number in PCI "
2704 			    "IRQ routing table; Not adding slot-names "
2705 			    "property for incorrect bus %d", bus);
2706 		}
2707 	}
2708 }
2709 
2710 /*
2711  * Handle both PCI root and PCI-PCI bridge range properties;
2712  * non-zero 'ppb' argument select PCI-PCI bridges versus root.
2713  */
2714 static void
2715 memlist_to_ranges(void **rp, struct memlist *entry, int type, int ppb)
2716 {
2717 	ppb_ranges_t *ppb_rp = *rp;
2718 	pci_ranges_t *pci_rp = *rp;
2719 
2720 	while (entry != NULL) {
2721 		if (ppb) {
2722 			ppb_rp->child_high = ppb_rp->parent_high = type;
2723 			ppb_rp->child_mid = ppb_rp->parent_mid =
2724 			    (uint32_t)(entry->address >> 32); /* XXX */
2725 			ppb_rp->child_low = ppb_rp->parent_low =
2726 			    (uint32_t)entry->address;
2727 			ppb_rp->size_high =
2728 			    (uint32_t)(entry->size >> 32); /* XXX */
2729 			ppb_rp->size_low = (uint32_t)entry->size;
2730 			*rp = ++ppb_rp;
2731 		} else {
2732 			pci_rp->child_high = type;
2733 			pci_rp->child_mid = pci_rp->parent_high =
2734 			    (uint32_t)(entry->address >> 32); /* XXX */
2735 			pci_rp->child_low = pci_rp->parent_low =
2736 			    (uint32_t)entry->address;
2737 			pci_rp->size_high =
2738 			    (uint32_t)(entry->size >> 32); /* XXX */
2739 			pci_rp->size_low = (uint32_t)entry->size;
2740 			*rp = ++pci_rp;
2741 		}
2742 		entry = entry->next;
2743 	}
2744 }
2745 
2746 static void
2747 add_ranges_prop(int bus, int ppb)
2748 {
2749 	int total, alloc_size;
2750 	void	*rp, *next_rp;
2751 	struct memlist *iolist, *memlist, *pmemlist;
2752 
2753 	/* no devinfo node - unused bus, return */
2754 	if (pci_bus_res[bus].dip == NULL)
2755 		return;
2756 
2757 	iolist = memlist = pmemlist = (struct memlist *)NULL;
2758 
2759 	memlist_merge(&pci_bus_res[bus].io_avail, &iolist);
2760 	memlist_merge(&pci_bus_res[bus].io_used, &iolist);
2761 	memlist_merge(&pci_bus_res[bus].mem_avail, &memlist);
2762 	memlist_merge(&pci_bus_res[bus].mem_used, &memlist);
2763 	memlist_merge(&pci_bus_res[bus].pmem_avail, &pmemlist);
2764 	memlist_merge(&pci_bus_res[bus].pmem_used, &pmemlist);
2765 
2766 	total = memlist_count(iolist);
2767 	total += memlist_count(memlist);
2768 	total += memlist_count(pmemlist);
2769 
2770 	/* no property is created if no ranges are present */
2771 	if (total == 0)
2772 		return;
2773 
2774 	alloc_size = total *
2775 	    (ppb ? sizeof (ppb_ranges_t) : sizeof (pci_ranges_t));
2776 
2777 	next_rp = rp = kmem_alloc(alloc_size, KM_SLEEP);
2778 
2779 	memlist_to_ranges(&next_rp, iolist, PCI_ADDR_IO | PCI_REG_REL_M, ppb);
2780 	memlist_to_ranges(&next_rp, memlist,
2781 	    PCI_ADDR_MEM32 | PCI_REG_REL_M, ppb);
2782 	memlist_to_ranges(&next_rp, pmemlist,
2783 	    PCI_ADDR_MEM32 | PCI_REG_REL_M | PCI_REG_PF_M, ppb);
2784 
2785 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, pci_bus_res[bus].dip,
2786 	    "ranges", (int *)rp, alloc_size / sizeof (int));
2787 
2788 	kmem_free(rp, alloc_size);
2789 	memlist_free_all(&iolist);
2790 	memlist_free_all(&memlist);
2791 	memlist_free_all(&pmemlist);
2792 }
2793 
2794 static void
2795 memlist_remove_list(struct memlist **list, struct memlist *remove_list)
2796 {
2797 	while (list && *list && remove_list) {
2798 		(void) memlist_remove(list, remove_list->address,
2799 		    remove_list->size);
2800 		remove_list = remove_list->next;
2801 	}
2802 }
2803 
2804 static int
2805 memlist_to_spec(struct pci_phys_spec *sp, struct memlist *list, int type)
2806 {
2807 	int i = 0;
2808 
2809 	while (list) {
2810 		/* assume 32-bit addresses */
2811 		sp->pci_phys_hi = type;
2812 		sp->pci_phys_mid = 0;
2813 		sp->pci_phys_low = (uint32_t)list->address;
2814 		sp->pci_size_hi = 0;
2815 		sp->pci_size_low = (uint32_t)list->size;
2816 
2817 		list = list->next;
2818 		sp++, i++;
2819 	}
2820 	return (i);
2821 }
2822 
2823 static void
2824 add_bus_available_prop(int bus)
2825 {
2826 	int i, count;
2827 	struct pci_phys_spec *sp;
2828 
2829 	/* no devinfo node - unused bus, return */
2830 	if (pci_bus_res[bus].dip == NULL)
2831 		return;
2832 
2833 	count = memlist_count(pci_bus_res[bus].io_avail) +
2834 	    memlist_count(pci_bus_res[bus].mem_avail) +
2835 	    memlist_count(pci_bus_res[bus].pmem_avail);
2836 
2837 	if (count == 0)		/* nothing available */
2838 		return;
2839 
2840 	sp = kmem_alloc(count * sizeof (*sp), KM_SLEEP);
2841 	i = memlist_to_spec(&sp[0], pci_bus_res[bus].io_avail,
2842 	    PCI_ADDR_IO | PCI_REG_REL_M);
2843 	i += memlist_to_spec(&sp[i], pci_bus_res[bus].mem_avail,
2844 	    PCI_ADDR_MEM32 | PCI_REG_REL_M);
2845 	i += memlist_to_spec(&sp[i], pci_bus_res[bus].pmem_avail,
2846 	    PCI_ADDR_MEM32 | PCI_REG_REL_M | PCI_REG_PF_M);
2847 	ASSERT(i == count);
2848 
2849 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, pci_bus_res[bus].dip,
2850 	    "available", (int *)sp,
2851 	    i * sizeof (struct pci_phys_spec) / sizeof (int));
2852 	kmem_free(sp, count * sizeof (*sp));
2853 }
2854 
2855 static void
2856 alloc_res_array(void)
2857 {
2858 	static int array_max = 0;
2859 	int old_max;
2860 	void *old_res;
2861 
2862 	if (array_max > pci_bios_nbus + 1)
2863 		return;	/* array is big enough */
2864 
2865 	old_max = array_max;
2866 	old_res = pci_bus_res;
2867 
2868 	if (array_max == 0)
2869 		array_max = 16;	/* start with a reasonable number */
2870 
2871 	while (array_max < pci_bios_nbus + 1)
2872 		array_max <<= 1;
2873 	pci_bus_res = (struct pci_bus_resource *)kmem_zalloc(
2874 	    array_max * sizeof (struct pci_bus_resource), KM_SLEEP);
2875 
2876 	if (old_res) {	/* copy content and free old array */
2877 		bcopy(old_res, pci_bus_res,
2878 		    old_max * sizeof (struct pci_bus_resource));
2879 		kmem_free(old_res, old_max * sizeof (struct pci_bus_resource));
2880 	}
2881 }
2882 
2883 static void
2884 create_ioapic_node(int bus, int dev, int fn, ushort_t vendorid,
2885     ushort_t deviceid)
2886 {
2887 	static dev_info_t *ioapicsnode = NULL;
2888 	static int numioapics = 0;
2889 	dev_info_t *ioapic_node;
2890 	uint64_t physaddr;
2891 	uint32_t lobase, hibase = 0;
2892 
2893 	/* BAR 0 contains the IOAPIC's memory-mapped I/O address */
2894 	lobase = (*pci_getl_func)(bus, dev, fn, PCI_CONF_BASE0);
2895 
2896 	/* We (and the rest of the world) only support memory-mapped IOAPICs */
2897 	if ((lobase & PCI_BASE_SPACE_M) != PCI_BASE_SPACE_MEM)
2898 		return;
2899 
2900 	if ((lobase & PCI_BASE_TYPE_M) == PCI_BASE_TYPE_ALL)
2901 		hibase = (*pci_getl_func)(bus, dev, fn, PCI_CONF_BASE0 + 4);
2902 
2903 	lobase &= PCI_BASE_M_ADDR_M;
2904 
2905 	physaddr = (((uint64_t)hibase) << 32) | lobase;
2906 
2907 	/*
2908 	 * Create a nexus node for all IOAPICs under the root node.
2909 	 */
2910 	if (ioapicsnode == NULL) {
2911 		if (ndi_devi_alloc(ddi_root_node(), IOAPICS_NODE_NAME,
2912 		    (pnode_t)DEVI_SID_NODEID, &ioapicsnode) != NDI_SUCCESS) {
2913 			return;
2914 		}
2915 		(void) ndi_devi_online(ioapicsnode, 0);
2916 	}
2917 
2918 	/*
2919 	 * Create a child node for this IOAPIC
2920 	 */
2921 	ioapic_node = ddi_add_child(ioapicsnode, IOAPICS_CHILD_NAME,
2922 	    DEVI_SID_NODEID, numioapics++);
2923 	if (ioapic_node == NULL) {
2924 		return;
2925 	}
2926 
2927 	/* Vendor and Device ID */
2928 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, ioapic_node,
2929 	    IOAPICS_PROP_VENID, vendorid);
2930 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, ioapic_node,
2931 	    IOAPICS_PROP_DEVID, deviceid);
2932 
2933 	/* device_type */
2934 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, ioapic_node,
2935 	    "device_type", IOAPICS_DEV_TYPE);
2936 
2937 	/* reg */
2938 	(void) ndi_prop_update_int64(DDI_DEV_T_NONE, ioapic_node,
2939 	    "reg", physaddr);
2940 }
2941 
2942 /*
2943  * NOTE: For PCIe slots, the name is generated from the slot number
2944  * information obtained from Slot Capabilities register.
2945  * For non-PCIe slots, it is generated based on the slot number
2946  * information in the PCI IRQ table.
2947  */
2948 static void
2949 pciex_slot_names_prop(dev_info_t *dip, ushort_t slot_num)
2950 {
2951 	char slotprop[256];
2952 	int len;
2953 
2954 	bzero(slotprop, sizeof (slotprop));
2955 
2956 	/* set mask to 1 as there is only one slot (i.e dev 0) */
2957 	*(uint32_t *)slotprop = 1;
2958 	len = 4;
2959 	(void) snprintf(slotprop + len, sizeof (slotprop) - len, "pcie%d",
2960 	    slot_num);
2961 	len += strlen(slotprop + len) + 1;
2962 	len += len % 4;
2963 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "slot-names",
2964 	    (int *)slotprop, len / sizeof (int));
2965 }
2966