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