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