xref: /titanic_52/usr/src/uts/intel/io/pci/pci_boot.c (revision 34f9b3eef6fdadbda0a846aa4d68691ac40eace5)
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 		 * 3. Exclude <1M address range here in case below reserved
1339 		 * ranges for BIOS data area, ROM area etc are wrongly reported
1340 		 * in ACPI resource producer entries for PCI root bus.
1341 		 * 	00000000 - 000003FF	RAM
1342 		 * 	00000400 - 000004FF	BIOS data area
1343 		 * 	00000500 - 0009FFFF	RAM
1344 		 * 	000A0000 - 000BFFFF	VGA RAM
1345 		 * 	000C0000 - 000FFFFF	ROM area
1346 		 */
1347 		memlist_remove(&pci_bus_res[bus].mem_avail, 0, 0x100000);
1348 		memlist_remove(&pci_bus_res[bus].pmem_avail, 0, 0x100000);
1349 	}
1350 
1351 	memlist_free_all(&isa_res.io_used);
1352 	memlist_free_all(&isa_res.mem_used);
1353 
1354 	/* add bus-range property for root/peer bus nodes */
1355 	for (i = 0; i <= pci_bios_maxbus; i++) {
1356 		/* create bus-range property on root/peer buses */
1357 		if (pci_bus_res[i].par_bus == (uchar_t)-1)
1358 			add_bus_range_prop(i);
1359 
1360 		/* setup bus range resource on each bus */
1361 		setup_bus_res(i);
1362 	}
1363 
1364 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
1365 	    DDI_PROP_DONTPASS, "pci-reprog", &onoff) == DDI_SUCCESS) {
1366 		if (strcmp(onoff, "off") == 0) {
1367 			pci_reconfig = 0;
1368 			cmn_err(CE_NOTE, "pci device reprogramming disabled");
1369 		}
1370 		ddi_prop_free(onoff);
1371 	}
1372 
1373 	remove_subtractive_res();
1374 
1375 	/* reprogram the non-subtractive PPB */
1376 	if (pci_reconfig)
1377 		for (i = 0; i <= pci_bios_maxbus; i++)
1378 			fix_ppb_res(i, B_FALSE);
1379 
1380 	for (i = 0; i <= pci_bios_maxbus; i++) {
1381 		/* configure devices not configured by BIOS */
1382 		if (pci_reconfig) {
1383 			/*
1384 			 * Reprogram the subtractive PPB. At this time, all its
1385 			 * siblings should have got their resources already.
1386 			 */
1387 			if (pci_bus_res[i].subtractive)
1388 				fix_ppb_res(i, B_TRUE);
1389 			enumerate_bus_devs(i, CONFIG_NEW);
1390 		}
1391 	}
1392 
1393 	/* All dev programmed, so we can create available prop */
1394 	for (i = 0; i <= pci_bios_maxbus; i++)
1395 		add_bus_available_prop(i);
1396 }
1397 
1398 /*
1399  * populate bus resources
1400  */
1401 static void
1402 populate_bus_res(uchar_t bus)
1403 {
1404 
1405 	/* scan BIOS structures */
1406 	pci_bus_res[bus].pmem_avail = find_bus_res(bus, PREFETCH_TYPE);
1407 	pci_bus_res[bus].mem_avail = find_bus_res(bus, MEM_TYPE);
1408 	pci_bus_res[bus].io_avail = find_bus_res(bus, IO_TYPE);
1409 	pci_bus_res[bus].bus_avail = find_bus_res(bus, BUSRANGE_TYPE);
1410 
1411 	/*
1412 	 * attempt to initialize sub_bus from the largest range-end
1413 	 * in the bus_avail list
1414 	 */
1415 	if (pci_bus_res[bus].bus_avail != NULL) {
1416 		struct memlist *entry;
1417 		int current;
1418 
1419 		entry = pci_bus_res[bus].bus_avail;
1420 		while (entry != NULL) {
1421 			current = entry->address + entry->size - 1;
1422 			if (current > pci_bus_res[bus].sub_bus)
1423 				pci_bus_res[bus].sub_bus = current;
1424 			entry = entry->next;
1425 		}
1426 	}
1427 
1428 	if (bus == 0) {
1429 		/*
1430 		 * Special treatment of bus 0:
1431 		 * If no IO/MEM resource from ACPI/MPSPEC/HRT, copy
1432 		 * pcimem from boot and make I/O space the entire range
1433 		 * starting at 0x100.
1434 		 */
1435 		if (pci_bus_res[0].mem_avail == NULL)
1436 			pci_bus_res[0].mem_avail =
1437 			    memlist_dup(bootops->boot_mem->pcimem);
1438 		/* Exclude 0x00 to 0xff of the I/O space, used by all PCs */
1439 		if (pci_bus_res[0].io_avail == NULL)
1440 			memlist_insert(&pci_bus_res[0].io_avail, 0x100, 0xffff);
1441 	}
1442 
1443 	/*
1444 	 * Create 'ranges' property here before any resources are
1445 	 * removed from the resource lists
1446 	 */
1447 	add_ranges_prop(bus, 0);
1448 }
1449 
1450 
1451 /*
1452  * Create top-level bus dips, i.e. /pci@0,0, /pci@1,0...
1453  */
1454 static void
1455 create_root_bus_dip(uchar_t bus)
1456 {
1457 	int pci_regs[] = {0, 0, 0};
1458 	dev_info_t *dip;
1459 
1460 	ASSERT(pci_bus_res[bus].par_bus == (uchar_t)-1);
1461 
1462 	num_root_bus++;
1463 	ndi_devi_alloc_sleep(ddi_root_node(), "pci",
1464 	    (pnode_t)DEVI_SID_NODEID, &dip);
1465 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1466 	    "#address-cells", 3);
1467 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1468 	    "#size-cells", 2);
1469 	pci_regs[0] = pci_bus_res[bus].root_addr;
1470 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
1471 	    "reg", (int *)pci_regs, 3);
1472 
1473 	/*
1474 	 * If system has PCIe bus, then create different properties
1475 	 */
1476 	if (create_pcie_root_bus(bus, dip) == B_FALSE)
1477 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1478 		    "device_type", "pci");
1479 
1480 	(void) ndi_devi_bind_driver(dip, 0);
1481 	pci_bus_res[bus].dip = dip;
1482 }
1483 
1484 /*
1485  * For any fixed configuration (often compatability) pci devices
1486  * and those with their own expansion rom, create device nodes
1487  * to hold the already configured device details.
1488  */
1489 void
1490 enumerate_bus_devs(uchar_t bus, int config_op)
1491 {
1492 	uchar_t dev, func, nfunc, header;
1493 	ushort_t venid;
1494 	struct pci_devfunc *devlist = NULL, *entry;
1495 
1496 	if (config_op == CONFIG_NEW) {
1497 		dcmn_err(CE_NOTE, "configuring pci bus 0x%x", bus);
1498 	} else if (config_op == CONFIG_FIX) {
1499 		dcmn_err(CE_NOTE, "fixing devices on pci bus 0x%x", bus);
1500 	} else
1501 		dcmn_err(CE_NOTE, "enumerating pci bus 0x%x", bus);
1502 
1503 	if (config_op == CONFIG_NEW) {
1504 		devlist = (struct pci_devfunc *)pci_bus_res[bus].privdata;
1505 		while (devlist) {
1506 			entry = devlist;
1507 			devlist = entry->next;
1508 			if (entry->reprogram ||
1509 			    pci_bus_res[bus].io_reprogram ||
1510 			    pci_bus_res[bus].mem_reprogram) {
1511 				/* reprogram device(s) */
1512 				(void) add_reg_props(entry->dip, bus,
1513 				    entry->dev, entry->func, CONFIG_NEW, 0);
1514 			}
1515 			kmem_free(entry, sizeof (*entry));
1516 		}
1517 		pci_bus_res[bus].privdata = NULL;
1518 		return;
1519 	}
1520 
1521 	for (dev = 0; dev < max_dev_pci; dev++) {
1522 		nfunc = 1;
1523 		for (func = 0; func < nfunc; func++) {
1524 
1525 			dcmn_err(CE_NOTE, "probing dev 0x%x, func 0x%x",
1526 			    dev, func);
1527 
1528 			venid = pci_getw(bus, dev, func, PCI_CONF_VENID);
1529 
1530 			if ((venid == 0xffff) || (venid == 0)) {
1531 				/* no function at this address */
1532 				continue;
1533 			}
1534 
1535 			header = pci_getb(bus, dev, func, PCI_CONF_HEADER);
1536 			if (header == 0xff) {
1537 				continue; /* illegal value */
1538 			}
1539 
1540 			/*
1541 			 * according to some mail from Microsoft posted
1542 			 * to the pci-drivers alias, their only requirement
1543 			 * for a multifunction device is for the 1st
1544 			 * function to have to PCI_HEADER_MULTI bit set.
1545 			 */
1546 			if ((func == 0) && (header & PCI_HEADER_MULTI)) {
1547 				nfunc = 8;
1548 			}
1549 
1550 			if (config_op == CONFIG_FIX ||
1551 			    config_op == CONFIG_INFO) {
1552 				/*
1553 				 * Create the node, unconditionally, on the
1554 				 * first pass only.  It may still need
1555 				 * resource assignment, which will be
1556 				 * done on the second, CONFIG_NEW, pass.
1557 				 */
1558 				process_devfunc(bus, dev, func, header,
1559 				    venid, config_op);
1560 
1561 			}
1562 		}
1563 	}
1564 
1565 	/* percolate bus used resources up through parents to root */
1566 	if (config_op == CONFIG_INFO) {
1567 		int	par_bus;
1568 
1569 		par_bus = pci_bus_res[bus].par_bus;
1570 		while (par_bus != (uchar_t)-1) {
1571 			pci_bus_res[par_bus].io_size +=
1572 			    pci_bus_res[bus].io_size;
1573 			pci_bus_res[par_bus].mem_size +=
1574 			    pci_bus_res[bus].mem_size;
1575 
1576 			if (pci_bus_res[bus].io_used)
1577 				memlist_merge(&pci_bus_res[bus].io_used,
1578 				    &pci_bus_res[par_bus].io_used);
1579 
1580 			if (pci_bus_res[bus].mem_used)
1581 				memlist_merge(&pci_bus_res[bus].mem_used,
1582 				    &pci_bus_res[par_bus].mem_used);
1583 
1584 			if (pci_bus_res[bus].pmem_used)
1585 				memlist_merge(&pci_bus_res[bus].pmem_used,
1586 				    &pci_bus_res[par_bus].pmem_used);
1587 
1588 			bus = par_bus;
1589 			par_bus = pci_bus_res[par_bus].par_bus;
1590 		}
1591 	}
1592 }
1593 
1594 static int
1595 check_pciide_prop(uchar_t revid, ushort_t venid, ushort_t devid,
1596     ushort_t subvenid, ushort_t subdevid)
1597 {
1598 	static int prop_exist = -1;
1599 	static char *pciide_str;
1600 	char compat[32];
1601 
1602 	if (prop_exist == -1) {
1603 		prop_exist = (ddi_prop_lookup_string(DDI_DEV_T_ANY,
1604 		    ddi_root_node(), DDI_PROP_DONTPASS, "pci-ide",
1605 		    &pciide_str) == DDI_SUCCESS);
1606 	}
1607 
1608 	if (!prop_exist)
1609 		return (0);
1610 
1611 	/* compare property value against various forms of compatible */
1612 	if (subvenid) {
1613 		(void) snprintf(compat, sizeof (compat), "pci%x,%x.%x.%x.%x",
1614 		    venid, devid, subvenid, subdevid, revid);
1615 		if (strcmp(pciide_str, compat) == 0)
1616 			return (1);
1617 
1618 		(void) snprintf(compat, sizeof (compat), "pci%x,%x.%x.%x",
1619 		    venid, devid, subvenid, subdevid);
1620 		if (strcmp(pciide_str, compat) == 0)
1621 			return (1);
1622 
1623 		(void) snprintf(compat, sizeof (compat), "pci%x,%x",
1624 		    subvenid, subdevid);
1625 		if (strcmp(pciide_str, compat) == 0)
1626 			return (1);
1627 	}
1628 	(void) snprintf(compat, sizeof (compat), "pci%x,%x.%x",
1629 	    venid, devid, revid);
1630 	if (strcmp(pciide_str, compat) == 0)
1631 		return (1);
1632 
1633 	(void) snprintf(compat, sizeof (compat), "pci%x,%x", venid, devid);
1634 	if (strcmp(pciide_str, compat) == 0)
1635 		return (1);
1636 
1637 	return (0);
1638 }
1639 
1640 static int
1641 is_pciide(uchar_t basecl, uchar_t subcl, uchar_t revid,
1642     ushort_t venid, ushort_t devid, ushort_t subvenid, ushort_t subdevid)
1643 {
1644 	struct ide_table {	/* table for PCI_MASS_OTHER */
1645 		ushort_t venid;
1646 		ushort_t devid;
1647 	} *entry;
1648 
1649 	/* XXX SATA and other devices: need a way to add dynamically */
1650 	static struct ide_table ide_other[] = {
1651 		{0x1095, 0x3112},
1652 		{0x1095, 0x3114},
1653 		{0x1095, 0x3512},
1654 		{0x1095, 0x680},	/* Sil0680 */
1655 		{0x1283, 0x8211},	/* ITE 8211F is subcl PCI_MASS_OTHER */
1656 		{0, 0}
1657 	};
1658 
1659 	if (basecl != PCI_CLASS_MASS)
1660 		return (0);
1661 
1662 	if (subcl == PCI_MASS_IDE) {
1663 		return (1);
1664 	}
1665 
1666 	if (check_pciide_prop(revid, venid, devid, subvenid, subdevid))
1667 		return (1);
1668 
1669 	if (subcl != PCI_MASS_OTHER && subcl != PCI_MASS_SATA) {
1670 		return (0);
1671 	}
1672 
1673 	entry = &ide_other[0];
1674 	while (entry->venid) {
1675 		if (entry->venid == venid && entry->devid == devid)
1676 			return (1);
1677 		entry++;
1678 	}
1679 	return (0);
1680 }
1681 
1682 static int
1683 is_display(uint_t classcode)
1684 {
1685 	static uint_t disp_classes[] = {
1686 		0x000100,
1687 		0x030000,
1688 		0x030001
1689 	};
1690 	int i, nclasses = sizeof (disp_classes) / sizeof (uint_t);
1691 
1692 	for (i = 0; i < nclasses; i++) {
1693 		if (classcode == disp_classes[i])
1694 			return (1);
1695 	}
1696 	return (0);
1697 }
1698 
1699 static void
1700 add_undofix_entry(uint8_t bus, uint8_t dev, uint8_t fn,
1701     void (*undofn)(uint8_t, uint8_t, uint8_t))
1702 {
1703 	struct pci_fixundo *newundo;
1704 
1705 	newundo = kmem_alloc(sizeof (struct pci_fixundo), KM_SLEEP);
1706 
1707 	/*
1708 	 * Adding an item to this list means that we must turn its NMIENABLE
1709 	 * bit back on at a later time.
1710 	 */
1711 	newundo->bus = bus;
1712 	newundo->dev = dev;
1713 	newundo->fn = fn;
1714 	newundo->undofn = undofn;
1715 	newundo->next = undolist;
1716 
1717 	/* add to the undo list in LIFO order */
1718 	undolist = newundo;
1719 }
1720 
1721 void
1722 add_pci_fixes(void)
1723 {
1724 	int i;
1725 
1726 	for (i = 0; i <= pci_bios_maxbus; i++) {
1727 		/*
1728 		 * For each bus, apply needed fixes to the appropriate devices.
1729 		 * This must be done before the main enumeration loop because
1730 		 * some fixes must be applied to devices normally encountered
1731 		 * later in the pci scan (e.g. if a fix to device 7 must be
1732 		 * applied before scanning device 6, applying fixes in the
1733 		 * normal enumeration loop would obviously be too late).
1734 		 */
1735 		enumerate_bus_devs(i, CONFIG_FIX);
1736 	}
1737 }
1738 
1739 void
1740 undo_pci_fixes(void)
1741 {
1742 	struct pci_fixundo *nextundo;
1743 	uint8_t bus, dev, fn;
1744 
1745 	/*
1746 	 * All fixes in the undo list are performed unconditionally.  Future
1747 	 * fixes may require selective undo.
1748 	 */
1749 	while (undolist != NULL) {
1750 
1751 		bus = undolist->bus;
1752 		dev = undolist->dev;
1753 		fn = undolist->fn;
1754 
1755 		(*(undolist->undofn))(bus, dev, fn);
1756 
1757 		nextundo = undolist->next;
1758 		kmem_free(undolist, sizeof (struct pci_fixundo));
1759 		undolist = nextundo;
1760 	}
1761 }
1762 
1763 static void
1764 undo_amd8111_pci_fix(uint8_t bus, uint8_t dev, uint8_t fn)
1765 {
1766 	uint8_t val8;
1767 
1768 	val8 = pci_getb(bus, dev, fn, LPC_IO_CONTROL_REG_1);
1769 	/*
1770 	 * The NMIONERR bit is turned back on to allow the SMM BIOS
1771 	 * to handle more critical PCI errors (e.g. PERR#).
1772 	 */
1773 	val8 |= AMD8111_ENABLENMI;
1774 	pci_putb(bus, dev, fn, LPC_IO_CONTROL_REG_1, val8);
1775 }
1776 
1777 static void
1778 pci_fix_amd8111(uint8_t bus, uint8_t dev, uint8_t fn)
1779 {
1780 	uint8_t val8;
1781 
1782 	val8 = pci_getb(bus, dev, fn, LPC_IO_CONTROL_REG_1);
1783 
1784 	if ((val8 & AMD8111_ENABLENMI) == 0)
1785 		return;
1786 
1787 	/*
1788 	 * We reset NMIONERR in the LPC because master-abort on the PCI
1789 	 * bridge side of the 8111 will cause NMI, which might cause SMI,
1790 	 * which sometimes prevents all devices from being enumerated.
1791 	 */
1792 	val8 &= ~AMD8111_ENABLENMI;
1793 
1794 	pci_putb(bus, dev, fn, LPC_IO_CONTROL_REG_1, val8);
1795 
1796 	add_undofix_entry(bus, dev, fn, undo_amd8111_pci_fix);
1797 }
1798 
1799 static void
1800 set_devpm_d0(uchar_t bus, uchar_t dev, uchar_t func)
1801 {
1802 	uint16_t status;
1803 	uint8_t header;
1804 	uint8_t cap_ptr;
1805 	uint8_t cap_id;
1806 	uint16_t pmcsr;
1807 
1808 	status = pci_getw(bus, dev, func, PCI_CONF_STAT);
1809 	if (!(status & PCI_STAT_CAP))
1810 		return;	/* No capabilities list */
1811 
1812 	header = pci_getb(bus, dev, func, PCI_CONF_HEADER) & PCI_HEADER_TYPE_M;
1813 	if (header == PCI_HEADER_CARDBUS)
1814 		cap_ptr = pci_getb(bus, dev, func, PCI_CBUS_CAP_PTR);
1815 	else
1816 		cap_ptr = pci_getb(bus, dev, func, PCI_CONF_CAP_PTR);
1817 	/*
1818 	 * Walk the capabilities list searching for a PM entry.
1819 	 */
1820 	while (cap_ptr != PCI_CAP_NEXT_PTR_NULL && cap_ptr >= PCI_CAP_PTR_OFF) {
1821 		cap_ptr &= PCI_CAP_PTR_MASK;
1822 		cap_id = pci_getb(bus, dev, func, cap_ptr + PCI_CAP_ID);
1823 		if (cap_id == PCI_CAP_ID_PM) {
1824 			pmcsr = pci_getw(bus, dev, func, cap_ptr + PCI_PMCSR);
1825 			pmcsr &= ~(PCI_PMCSR_STATE_MASK);
1826 			pmcsr |= PCI_PMCSR_D0; /* D0 state */
1827 			pci_putw(bus, dev, func, cap_ptr + PCI_PMCSR, pmcsr);
1828 			break;
1829 		}
1830 		cap_ptr = pci_getb(bus, dev, func, cap_ptr + PCI_CAP_NEXT_PTR);
1831 	}
1832 
1833 }
1834 
1835 #define	is_isa(bc, sc)	\
1836 	(((bc) == PCI_CLASS_BRIDGE) && ((sc) == PCI_BRIDGE_ISA))
1837 
1838 static void
1839 process_devfunc(uchar_t bus, uchar_t dev, uchar_t func, uchar_t header,
1840     ushort_t vendorid, int config_op)
1841 {
1842 	char nodename[32], unitaddr[5];
1843 	dev_info_t *dip;
1844 	uchar_t basecl, subcl, progcl, intr, revid;
1845 	ushort_t subvenid, subdevid, status;
1846 	ushort_t slot_num;
1847 	uint_t classcode, revclass;
1848 	int reprogram = 0, pciide = 0;
1849 	int power[2] = {1, 1};
1850 	int pciex = 0;
1851 	ushort_t is_pci_bridge = 0;
1852 	struct pci_devfunc *devlist = NULL, *entry = NULL;
1853 	gfx_entry_t *gfxp;
1854 
1855 	ushort_t deviceid = pci_getw(bus, dev, func, PCI_CONF_DEVID);
1856 
1857 	switch (header & PCI_HEADER_TYPE_M) {
1858 	case PCI_HEADER_ZERO:
1859 		subvenid = pci_getw(bus, dev, func, PCI_CONF_SUBVENID);
1860 		subdevid = pci_getw(bus, dev, func, PCI_CONF_SUBSYSID);
1861 		break;
1862 	case PCI_HEADER_CARDBUS:
1863 		subvenid = pci_getw(bus, dev, func, PCI_CBUS_SUBVENID);
1864 		subdevid = pci_getw(bus, dev, func, PCI_CBUS_SUBSYSID);
1865 		/* Record the # of cardbus bridges found on the bus */
1866 		if (config_op == CONFIG_INFO)
1867 			pci_bus_res[bus].num_cbb++;
1868 		break;
1869 	default:
1870 		subvenid = 0;
1871 		subdevid = 0;
1872 		break;
1873 	}
1874 
1875 	if (config_op == CONFIG_FIX) {
1876 		if (vendorid == VENID_AMD && deviceid == DEVID_AMD8111_LPC) {
1877 			pci_fix_amd8111(bus, dev, func);
1878 		}
1879 		return;
1880 	}
1881 
1882 	/* XXX should be use generic names? derive from class? */
1883 	revclass = pci_getl(bus, dev, func, PCI_CONF_REVID);
1884 	classcode = revclass >> 8;
1885 	revid = revclass & 0xff;
1886 
1887 	/* figure out if this is pci-ide */
1888 	basecl = classcode >> 16;
1889 	subcl = (classcode >> 8) & 0xff;
1890 	progcl = classcode & 0xff;
1891 
1892 
1893 	if (is_display(classcode))
1894 		(void) snprintf(nodename, sizeof (nodename), "display");
1895 	else if (!pseudo_isa && is_isa(basecl, subcl))
1896 		(void) snprintf(nodename, sizeof (nodename), "isa");
1897 	else if (subvenid != 0)
1898 		(void) snprintf(nodename, sizeof (nodename),
1899 		    "pci%x,%x", subvenid, subdevid);
1900 	else
1901 		(void) snprintf(nodename, sizeof (nodename),
1902 		    "pci%x,%x", vendorid, deviceid);
1903 
1904 	/* make sure parent bus dip has been created */
1905 	if (pci_bus_res[bus].dip == NULL)
1906 		create_root_bus_dip(bus);
1907 
1908 	ndi_devi_alloc_sleep(pci_bus_res[bus].dip, nodename,
1909 	    DEVI_SID_NODEID, &dip);
1910 
1911 	if (check_if_device_is_pciex(dip, bus, dev, func, &slot_num,
1912 	    &is_pci_bridge) == B_TRUE)
1913 		pciex = 1;
1914 
1915 	/* add properties */
1916 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, "device-id", deviceid);
1917 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, "vendor-id", vendorid);
1918 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, "revision-id", revid);
1919 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1920 	    "class-code", classcode);
1921 	if (func == 0)
1922 		(void) snprintf(unitaddr, sizeof (unitaddr), "%x", dev);
1923 	else
1924 		(void) snprintf(unitaddr, sizeof (unitaddr),
1925 		    "%x,%x", dev, func);
1926 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1927 	    "unit-address", unitaddr);
1928 
1929 	/* add device_type for display nodes */
1930 	if (is_display(classcode)) {
1931 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1932 		    "device_type", "display");
1933 	}
1934 	/* add special stuff for header type */
1935 	if ((header & PCI_HEADER_TYPE_M) == PCI_HEADER_ZERO) {
1936 		uchar_t mingrant = pci_getb(bus, dev, func, PCI_CONF_MIN_G);
1937 		uchar_t maxlatency = pci_getb(bus, dev, func, PCI_CONF_MAX_L);
1938 
1939 		if (subvenid != 0) {
1940 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1941 			    "subsystem-id", subdevid);
1942 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1943 			    "subsystem-vendor-id", subvenid);
1944 		}
1945 		if (!pciex)
1946 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1947 			    "min-grant", mingrant);
1948 		if (!pciex)
1949 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1950 			    "max-latency", maxlatency);
1951 	}
1952 
1953 	/* interrupt, record if not 0 */
1954 	intr = pci_getb(bus, dev, func, PCI_CONF_IPIN);
1955 	if (intr != 0)
1956 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1957 		    "interrupts", intr);
1958 
1959 	/*
1960 	 * Add support for 133 mhz pci eventually
1961 	 */
1962 	status = pci_getw(bus, dev, func, PCI_CONF_STAT);
1963 
1964 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1965 	    "devsel-speed", (status & PCI_STAT_DEVSELT) >> 9);
1966 	if (!pciex && (status & PCI_STAT_FBBC))
1967 		(void) ndi_prop_create_boolean(DDI_DEV_T_NONE, dip,
1968 		    "fast-back-to-back");
1969 	if (!pciex && (status & PCI_STAT_66MHZ))
1970 		(void) ndi_prop_create_boolean(DDI_DEV_T_NONE, dip,
1971 		    "66mhz-capable");
1972 	if (status & PCI_STAT_UDF)
1973 		(void) ndi_prop_create_boolean(DDI_DEV_T_NONE, dip,
1974 		    "udf-supported");
1975 	if (pciex && slot_num) {
1976 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1977 		    "physical-slot#", slot_num);
1978 		if (!is_pci_bridge)
1979 			pciex_slot_names_prop(dip, slot_num);
1980 	}
1981 
1982 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
1983 	    "power-consumption", power, 2);
1984 
1985 	/* Set the device PM state to D0 */
1986 	set_devpm_d0(bus, dev, func);
1987 
1988 	if ((basecl == PCI_CLASS_BRIDGE) && (subcl == PCI_BRIDGE_PCI))
1989 		add_ppb_props(dip, bus, dev, func, pciex, is_pci_bridge);
1990 	else {
1991 		/*
1992 		 * Record the non-PPB devices on the bus for possible
1993 		 * reprogramming at 2nd bus enumeration.
1994 		 * Note: PPB reprogramming is done in fix_ppb_res()
1995 		 */
1996 		devlist = (struct pci_devfunc *)pci_bus_res[bus].privdata;
1997 		entry = kmem_zalloc(sizeof (*entry), KM_SLEEP);
1998 		entry->dip = dip;
1999 		entry->dev = dev;
2000 		entry->func = func;
2001 		entry->next = devlist;
2002 		pci_bus_res[bus].privdata = entry;
2003 	}
2004 
2005 	if (config_op == CONFIG_INFO &&
2006 	    IS_CLASS_IOAPIC(basecl, subcl, progcl)) {
2007 		create_ioapic_node(bus, dev, func, vendorid, deviceid);
2008 	}
2009 
2010 	/* check for ck8-04 based PCI ISA bridge only */
2011 	if (NVIDIA_IS_LPC_BRIDGE(vendorid, deviceid) && (dev == 1) &&
2012 	    (func == 0))
2013 		add_nvidia_isa_bridge_props(dip, bus, dev, func);
2014 
2015 	if (pciex && is_pci_bridge)
2016 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, "model",
2017 		    (char *)"PCIe-PCI bridge");
2018 	else
2019 		add_model_prop(dip, classcode);
2020 
2021 	add_compatible(dip, subvenid, subdevid, vendorid, deviceid,
2022 	    revid, classcode, pciex);
2023 
2024 	/*
2025 	 * See if this device is a controller that advertises
2026 	 * itself to be a standard ATA task file controller, or one that
2027 	 * has been hard coded.
2028 	 *
2029 	 * If it is, check if any other higher precedence driver listed in
2030 	 * driver_aliases will claim the node by calling
2031 	 * ddi_compatibile_driver_major.  If so, clear pciide and do not
2032 	 * create a pci-ide node or any other special handling.
2033 	 *
2034 	 * If another driver does not bind, set the node name to pci-ide
2035 	 * and then let the special pci-ide handling for registers and
2036 	 * child pci-ide nodes proceed below.
2037 	 */
2038 	if (is_pciide(basecl, subcl, revid, vendorid, deviceid,
2039 	    subvenid, subdevid) == 1) {
2040 		if (ddi_compatible_driver_major(dip, NULL) == (major_t)-1) {
2041 			(void) ndi_devi_set_nodename(dip, "pci-ide", 0);
2042 			pciide = 1;
2043 		}
2044 	}
2045 
2046 	reprogram = add_reg_props(dip, bus, dev, func, config_op, pciide);
2047 	(void) ndi_devi_bind_driver(dip, 0);
2048 
2049 	/* special handling for pci-ide */
2050 	if (pciide) {
2051 		dev_info_t *cdip;
2052 
2053 		/*
2054 		 * Create properties specified by P1275 Working Group
2055 		 * Proposal #414 Version 1
2056 		 */
2057 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
2058 		    "device_type", "pci-ide");
2059 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
2060 		    "#address-cells", 1);
2061 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
2062 		    "#size-cells", 0);
2063 
2064 		/* allocate two child nodes */
2065 		ndi_devi_alloc_sleep(dip, "ide",
2066 		    (pnode_t)DEVI_SID_NODEID, &cdip);
2067 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cdip,
2068 		    "reg", 0);
2069 		(void) ndi_devi_bind_driver(cdip, 0);
2070 		ndi_devi_alloc_sleep(dip, "ide",
2071 		    (pnode_t)DEVI_SID_NODEID, &cdip);
2072 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cdip,
2073 		    "reg", 1);
2074 		(void) ndi_devi_bind_driver(cdip, 0);
2075 
2076 		reprogram = 0;	/* don't reprogram pci-ide bridge */
2077 	}
2078 
2079 	if (is_display(classcode)) {
2080 		gfxp = kmem_zalloc(sizeof (*gfxp), KM_SLEEP);
2081 		gfxp->g_dip = dip;
2082 		gfxp->g_prev = NULL;
2083 		gfxp->g_next = gfx_devinfo_list;
2084 		gfx_devinfo_list = gfxp;
2085 		if (gfxp->g_next)
2086 			gfxp->g_next->g_prev = gfxp;
2087 	}
2088 
2089 	/* special handling for isa */
2090 	if (!pseudo_isa && is_isa(basecl, subcl)) {
2091 		/* add device_type */
2092 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
2093 		    "device_type", "isa");
2094 	}
2095 
2096 	if (reprogram && (entry != NULL))
2097 		entry->reprogram = B_TRUE;
2098 
2099 }
2100 
2101 /*
2102  * Some vendors do not use unique subsystem IDs in their products, which
2103  * makes the use of form 2 compatible names (pciSSSS,ssss) inappropriate.
2104  * Allow for these compatible forms to be excluded on a per-device basis.
2105  */
2106 /*ARGSUSED*/
2107 static boolean_t
2108 subsys_compat_exclude(ushort_t venid, ushort_t devid, ushort_t subvenid,
2109     ushort_t subdevid, uchar_t revid, uint_t classcode)
2110 {
2111 	/* Nvidia display adapters */
2112 	if ((venid == 0x10de) && (is_display(classcode)))
2113 		return (B_TRUE);
2114 
2115 	return (B_FALSE);
2116 }
2117 
2118 /*
2119  * Set the compatible property to a value compliant with
2120  * rev 2.1 of the IEEE1275 PCI binding.
2121  * (Also used for PCI-Express devices).
2122  *
2123  *   pciVVVV,DDDD.SSSS.ssss.RR	(0)
2124  *   pciVVVV,DDDD.SSSS.ssss	(1)
2125  *   pciSSSS,ssss		(2)
2126  *   pciVVVV,DDDD.RR		(3)
2127  *   pciVVVV,DDDD		(4)
2128  *   pciclass,CCSSPP		(5)
2129  *   pciclass,CCSS		(6)
2130  *
2131  * The Subsystem (SSSS) forms are not inserted if
2132  * subsystem-vendor-id is 0.
2133  *
2134  * NOTE: For PCI-Express devices "pci" is replaced with "pciex" in 0-6 above
2135  * property 2 is not created as per "1275 bindings for PCI Express Interconnect"
2136  *
2137  * Set with setprop and \x00 between each
2138  * to generate the encoded string array form.
2139  */
2140 void
2141 add_compatible(dev_info_t *dip, ushort_t subvenid, ushort_t subdevid,
2142     ushort_t vendorid, ushort_t deviceid, uchar_t revid, uint_t classcode,
2143     int pciex)
2144 {
2145 	int i = 0;
2146 	int size = COMPAT_BUFSIZE;
2147 	char *compat[13];
2148 	char *buf, *curr;
2149 
2150 	curr = buf = kmem_alloc(size, KM_SLEEP);
2151 
2152 	if (pciex) {
2153 		if (subvenid) {
2154 			compat[i++] = curr;	/* form 0 */
2155 			(void) snprintf(curr, size, "pciex%x,%x.%x.%x.%x",
2156 			    vendorid, deviceid, subvenid, subdevid, revid);
2157 			size -= strlen(curr) + 1;
2158 			curr += strlen(curr) + 1;
2159 
2160 			compat[i++] = curr;	/* form 1 */
2161 			(void) snprintf(curr, size, "pciex%x,%x.%x.%x",
2162 			    vendorid, deviceid, subvenid, subdevid);
2163 			size -= strlen(curr) + 1;
2164 			curr += strlen(curr) + 1;
2165 
2166 		}
2167 		compat[i++] = curr;	/* form 3 */
2168 		(void) snprintf(curr, size, "pciex%x,%x.%x",
2169 		    vendorid, deviceid, revid);
2170 		size -= strlen(curr) + 1;
2171 		curr += strlen(curr) + 1;
2172 
2173 		compat[i++] = curr;	/* form 4 */
2174 		(void) snprintf(curr, size, "pciex%x,%x", vendorid, deviceid);
2175 		size -= strlen(curr) + 1;
2176 		curr += strlen(curr) + 1;
2177 
2178 		compat[i++] = curr;	/* form 5 */
2179 		(void) snprintf(curr, size, "pciexclass,%06x", classcode);
2180 		size -= strlen(curr) + 1;
2181 		curr += strlen(curr) + 1;
2182 
2183 		compat[i++] = curr;	/* form 6 */
2184 		(void) snprintf(curr, size, "pciexclass,%04x",
2185 		    (classcode >> 8));
2186 		size -= strlen(curr) + 1;
2187 		curr += strlen(curr) + 1;
2188 	}
2189 
2190 	if (subvenid) {
2191 		compat[i++] = curr;	/* form 0 */
2192 		(void) snprintf(curr, size, "pci%x,%x.%x.%x.%x",
2193 		    vendorid, deviceid, subvenid, subdevid, revid);
2194 		size -= strlen(curr) + 1;
2195 		curr += strlen(curr) + 1;
2196 
2197 		compat[i++] = curr;	/* form 1 */
2198 		(void) snprintf(curr, size, "pci%x,%x.%x.%x",
2199 		    vendorid, deviceid, subvenid, subdevid);
2200 		size -= strlen(curr) + 1;
2201 		curr += strlen(curr) + 1;
2202 
2203 		if (subsys_compat_exclude(vendorid, deviceid, subvenid,
2204 		    subdevid, revid, classcode) == B_FALSE) {
2205 			compat[i++] = curr;	/* form 2 */
2206 			(void) snprintf(curr, size, "pci%x,%x", subvenid,
2207 			    subdevid);
2208 			size -= strlen(curr) + 1;
2209 			curr += strlen(curr) + 1;
2210 		}
2211 	}
2212 	compat[i++] = curr;	/* form 3 */
2213 	(void) snprintf(curr, size, "pci%x,%x.%x", vendorid, deviceid, revid);
2214 	size -= strlen(curr) + 1;
2215 	curr += strlen(curr) + 1;
2216 
2217 	compat[i++] = curr;	/* form 4 */
2218 	(void) snprintf(curr, size, "pci%x,%x", vendorid, deviceid);
2219 	size -= strlen(curr) + 1;
2220 	curr += strlen(curr) + 1;
2221 
2222 	compat[i++] = curr;	/* form 5 */
2223 	(void) snprintf(curr, size, "pciclass,%06x", classcode);
2224 	size -= strlen(curr) + 1;
2225 	curr += strlen(curr) + 1;
2226 
2227 	compat[i++] = curr;	/* form 6 */
2228 	(void) snprintf(curr, size, "pciclass,%04x", (classcode >> 8));
2229 	size -= strlen(curr) + 1;
2230 	curr += strlen(curr) + 1;
2231 
2232 	(void) ndi_prop_update_string_array(DDI_DEV_T_NONE, dip,
2233 	    "compatible", compat, i);
2234 	kmem_free(buf, COMPAT_BUFSIZE);
2235 }
2236 
2237 /*
2238  * Adjust the reg properties for a dual channel PCI-IDE device.
2239  *
2240  * NOTE: don't do anything that changes the order of the hard-decodes
2241  * and programmed BARs. The kernel driver depends on these values
2242  * being in this order regardless of whether they're for a 'native'
2243  * mode BAR or not.
2244  */
2245 /*
2246  * config info for pci-ide devices
2247  */
2248 static struct {
2249 	uchar_t  native_mask;	/* 0 == 'compatibility' mode, 1 == native */
2250 	uchar_t  bar_offset;	/* offset for alt status register */
2251 	ushort_t addr;		/* compatibility mode base address */
2252 	ushort_t length;	/* number of ports for this BAR */
2253 } pciide_bar[] = {
2254 	{ 0x01, 0, 0x1f0, 8 },	/* primary lower BAR */
2255 	{ 0x01, 2, 0x3f6, 1 },	/* primary upper BAR */
2256 	{ 0x04, 0, 0x170, 8 },	/* secondary lower BAR */
2257 	{ 0x04, 2, 0x376, 1 }	/* secondary upper BAR */
2258 };
2259 
2260 static int
2261 pciIdeAdjustBAR(uchar_t progcl, int index, uint_t *basep, uint_t *lenp)
2262 {
2263 	int hard_decode = 0;
2264 
2265 	/*
2266 	 * Adjust the base and len for the BARs of the PCI-IDE
2267 	 * device's primary and secondary controllers. The first
2268 	 * two BARs are for the primary controller and the next
2269 	 * two BARs are for the secondary controller. The fifth
2270 	 * and sixth bars are never adjusted.
2271 	 */
2272 	if (index >= 0 && index <= 3) {
2273 		*lenp = pciide_bar[index].length;
2274 
2275 		if (progcl & pciide_bar[index].native_mask) {
2276 			*basep += pciide_bar[index].bar_offset;
2277 		} else {
2278 			*basep = pciide_bar[index].addr;
2279 			hard_decode = 1;
2280 		}
2281 	}
2282 
2283 	/*
2284 	 * if either base or len is zero make certain both are zero
2285 	 */
2286 	if (*basep == 0 || *lenp == 0) {
2287 		*basep = 0;
2288 		*lenp = 0;
2289 		hard_decode = 0;
2290 	}
2291 
2292 	return (hard_decode);
2293 }
2294 
2295 
2296 /*
2297  * Add the "reg" and "assigned-addresses" property
2298  */
2299 static int
2300 add_reg_props(dev_info_t *dip, uchar_t bus, uchar_t dev, uchar_t func,
2301     int config_op, int pciide)
2302 {
2303 	uchar_t baseclass, subclass, progclass, header;
2304 	ushort_t bar_sz;
2305 	uint_t value = 0, len, devloc;
2306 	uint_t base, base_hi, type;
2307 	ushort_t offset, end;
2308 	int max_basereg, j, reprogram = 0;
2309 	uint_t phys_hi;
2310 	struct memlist **io_avail, **io_used;
2311 	struct memlist **mem_avail, **mem_used;
2312 	struct memlist **pmem_avail, **pmem_used;
2313 	uchar_t res_bus;
2314 
2315 	pci_regspec_t regs[16] = {{0}};
2316 	pci_regspec_t assigned[15] = {{0}};
2317 	int nreg, nasgn;
2318 
2319 	io_avail = &pci_bus_res[bus].io_avail;
2320 	io_used = &pci_bus_res[bus].io_used;
2321 	mem_avail = &pci_bus_res[bus].mem_avail;
2322 	mem_used = &pci_bus_res[bus].mem_used;
2323 	pmem_avail = &pci_bus_res[bus].pmem_avail;
2324 	pmem_used = &pci_bus_res[bus].pmem_used;
2325 
2326 	devloc = (uint_t)bus << 16 | (uint_t)dev << 11 | (uint_t)func << 8;
2327 	regs[0].pci_phys_hi = devloc;
2328 	nreg = 1;	/* rest of regs[0] is all zero */
2329 	nasgn = 0;
2330 
2331 	baseclass = pci_getb(bus, dev, func, PCI_CONF_BASCLASS);
2332 	subclass = pci_getb(bus, dev, func, PCI_CONF_SUBCLASS);
2333 	progclass = pci_getb(bus, dev, func, PCI_CONF_PROGCLASS);
2334 	header = pci_getb(bus, dev, func, PCI_CONF_HEADER) & PCI_HEADER_TYPE_M;
2335 
2336 	switch (header) {
2337 	case PCI_HEADER_ZERO:
2338 		max_basereg = PCI_BASE_NUM;
2339 		break;
2340 	case PCI_HEADER_PPB:
2341 		max_basereg = PCI_BCNF_BASE_NUM;
2342 		break;
2343 	case PCI_HEADER_CARDBUS:
2344 		max_basereg = PCI_CBUS_BASE_NUM;
2345 		reprogram = 1;
2346 		break;
2347 	default:
2348 		max_basereg = 0;
2349 		break;
2350 	}
2351 
2352 	/*
2353 	 * Create the register property by saving the current
2354 	 * value of the base register. Write 0xffffffff to the
2355 	 * base register.  Read the value back to determine the
2356 	 * required size of the address space.  Restore the base
2357 	 * register contents.
2358 	 *
2359 	 * Do not disable I/O and memory access; this isn't necessary
2360 	 * since no driver is yet attached to this device, and disabling
2361 	 * I/O and memory access has the side-effect of disabling PCI-PCI
2362 	 * bridge mappings, which makes the bridge transparent to secondary-
2363 	 * bus activity (see sections 4.1-4.3 of the PCI-PCI Bridge
2364 	 * Spec V1.2).
2365 	 */
2366 	end = PCI_CONF_BASE0 + max_basereg * sizeof (uint_t);
2367 	for (j = 0, offset = PCI_CONF_BASE0; offset < end;
2368 	    j++, offset += bar_sz) {
2369 		/* determine the size of the address space */
2370 		base = pci_getl(bus, dev, func, offset);
2371 		pci_putl(bus, dev, func, offset, 0xffffffff);
2372 		value = pci_getl(bus, dev, func, offset);
2373 		pci_putl(bus, dev, func, offset, base);
2374 
2375 		/* construct phys hi,med.lo, size hi, lo */
2376 		if ((pciide && j < 4) || (base & PCI_BASE_SPACE_IO)) {
2377 			int hard_decode = 0;
2378 
2379 			/* i/o space */
2380 			bar_sz = PCI_BAR_SZ_32;
2381 			value &= PCI_BASE_IO_ADDR_M;
2382 			len = ((value ^ (value-1)) + 1) >> 1;
2383 
2384 			/* XXX Adjust first 4 IDE registers */
2385 			if (pciide) {
2386 				if (subclass != PCI_MASS_IDE)
2387 					progclass = (PCI_IDE_IF_NATIVE_PRI |
2388 					    PCI_IDE_IF_NATIVE_SEC);
2389 				hard_decode = pciIdeAdjustBAR(progclass, j,
2390 				    &base, &len);
2391 			} else if (value == 0) {
2392 				/* skip base regs with size of 0 */
2393 				continue;
2394 			}
2395 
2396 			regs[nreg].pci_phys_hi = PCI_ADDR_IO | devloc |
2397 			    (hard_decode ? PCI_RELOCAT_B : offset);
2398 			regs[nreg].pci_phys_low = hard_decode ?
2399 			    base & PCI_BASE_IO_ADDR_M : 0;
2400 			assigned[nasgn].pci_phys_hi =
2401 			    PCI_RELOCAT_B | regs[nreg].pci_phys_hi;
2402 			regs[nreg].pci_size_low =
2403 			    assigned[nasgn].pci_size_low = len;
2404 			type = base & (~PCI_BASE_IO_ADDR_M);
2405 			base &= PCI_BASE_IO_ADDR_M;
2406 			/*
2407 			 * A device under a subtractive PPB can allocate
2408 			 * resources from its parent bus if there is no resource
2409 			 * available on its own bus.
2410 			 */
2411 			if ((config_op == CONFIG_NEW) && (*io_avail == NULL)) {
2412 				res_bus = bus;
2413 				while (pci_bus_res[res_bus].subtractive) {
2414 					res_bus = pci_bus_res[res_bus].par_bus;
2415 					if (res_bus == (uchar_t)-1)
2416 						break; /* root bus already */
2417 					if (pci_bus_res[res_bus].io_avail) {
2418 						io_avail = &pci_bus_res
2419 						    [res_bus].io_avail;
2420 						break;
2421 					}
2422 				}
2423 			}
2424 
2425 			/*
2426 			 * first pass - gather what's there
2427 			 * update/second pass - adjust/allocate regions
2428 			 *	config - allocate regions
2429 			 */
2430 			if (config_op == CONFIG_INFO) {	/* first pass */
2431 				/* take out of the resource map of the bus */
2432 				if (base != 0) {
2433 					(void) memlist_remove(io_avail, base,
2434 					    len);
2435 					memlist_insert(io_used, base, len);
2436 				} else {
2437 					reprogram = 1;
2438 				}
2439 				pci_bus_res[bus].io_size += len;
2440 			} else if ((*io_avail && base == 0) ||
2441 			    pci_bus_res[bus].io_reprogram) {
2442 				base = (uint_t)memlist_find(io_avail, len, len);
2443 				if (base != 0) {
2444 					memlist_insert(io_used, base, len);
2445 					/* XXX need to worry about 64-bit? */
2446 					pci_putl(bus, dev, func, offset,
2447 					    base | type);
2448 					base = pci_getl(bus, dev, func, offset);
2449 					base &= PCI_BASE_IO_ADDR_M;
2450 				}
2451 				if (base == 0) {
2452 					cmn_err(CE_WARN, "failed to program"
2453 					    " IO space [%d/%d/%d] BAR@0x%x"
2454 					    " length 0x%x",
2455 					    bus, dev, func, offset, len);
2456 				}
2457 			}
2458 			assigned[nasgn].pci_phys_low = base;
2459 			nreg++, nasgn++;
2460 
2461 		} else {
2462 			/* memory space */
2463 			if ((base & PCI_BASE_TYPE_M) == PCI_BASE_TYPE_ALL) {
2464 				bar_sz = PCI_BAR_SZ_64;
2465 				base_hi = pci_getl(bus, dev, func, offset + 4);
2466 				phys_hi = PCI_ADDR_MEM64;
2467 			} else {
2468 				bar_sz = PCI_BAR_SZ_32;
2469 				base_hi = 0;
2470 				phys_hi = PCI_ADDR_MEM32;
2471 			}
2472 
2473 			/* skip base regs with size of 0 */
2474 			value &= PCI_BASE_M_ADDR_M;
2475 
2476 			if (value == 0)
2477 				continue;
2478 
2479 			len = ((value ^ (value-1)) + 1) >> 1;
2480 			regs[nreg].pci_size_low =
2481 			    assigned[nasgn].pci_size_low = len;
2482 
2483 			phys_hi |= (devloc | offset);
2484 			if (base & PCI_BASE_PREF_M)
2485 				phys_hi |= PCI_PREFETCH_B;
2486 
2487 			/*
2488 			 * A device under a subtractive PPB can allocate
2489 			 * resources from its parent bus if there is no resource
2490 			 * available on its own bus.
2491 			 */
2492 			if ((config_op == CONFIG_NEW) && (*mem_avail == NULL)) {
2493 				res_bus = bus;
2494 				while (pci_bus_res[res_bus].subtractive) {
2495 					res_bus = pci_bus_res[res_bus].par_bus;
2496 					if (res_bus == (uchar_t)-1)
2497 						break; /* root bus already */
2498 					mem_avail =
2499 					    &pci_bus_res[res_bus].mem_avail;
2500 					pmem_avail =
2501 					    &pci_bus_res [res_bus].pmem_avail;
2502 					/*
2503 					 * Break out as long as at least
2504 					 * mem_avail is available
2505 					 */
2506 					if ((*pmem_avail &&
2507 					    (phys_hi & PCI_PREFETCH_B)) ||
2508 					    *mem_avail)
2509 						break;
2510 				}
2511 			}
2512 
2513 			regs[nreg].pci_phys_hi =
2514 			    assigned[nasgn].pci_phys_hi = phys_hi;
2515 			assigned[nasgn].pci_phys_hi |= PCI_RELOCAT_B;
2516 			assigned[nasgn].pci_phys_mid = base_hi;
2517 			type = base & ~PCI_BASE_M_ADDR_M;
2518 			base &= PCI_BASE_M_ADDR_M;
2519 
2520 			if (config_op == CONFIG_INFO) {
2521 				/* take out of the resource map of the bus */
2522 				if (base != NULL) {
2523 					/* remove from PMEM and MEM space */
2524 					(void) memlist_remove(mem_avail,
2525 					    base, len);
2526 					(void) memlist_remove(pmem_avail,
2527 					    base, len);
2528 					/* only note as used in correct map */
2529 					if (phys_hi & PCI_PREFETCH_B)
2530 						memlist_insert(pmem_used,
2531 						    base, len);
2532 					else
2533 						memlist_insert(mem_used,
2534 						    base, len);
2535 				} else {
2536 					reprogram = 1;
2537 				}
2538 				pci_bus_res[bus].mem_size += len;
2539 			} else if ((*mem_avail && base == NULL) ||
2540 			    pci_bus_res[bus].mem_reprogram) {
2541 				/*
2542 				 * When desired, attempt a prefetchable
2543 				 * allocation first
2544 				 */
2545 				if (phys_hi & PCI_PREFETCH_B) {
2546 					base = (uint_t)memlist_find(pmem_avail,
2547 					    len, len);
2548 					if (base != NULL) {
2549 						memlist_insert(pmem_used,
2550 						    base, len);
2551 						(void) memlist_remove(mem_avail,
2552 						    base, len);
2553 					}
2554 				}
2555 				/*
2556 				 * If prefetchable allocation was not
2557 				 * desired, or failed, attempt ordinary
2558 				 * memory allocation
2559 				 */
2560 				if (base == NULL) {
2561 					base = (uint_t)memlist_find(mem_avail,
2562 					    len, len);
2563 					if (base != NULL) {
2564 						memlist_insert(mem_used,
2565 						    base, len);
2566 						(void) memlist_remove(
2567 						    pmem_avail, base, len);
2568 					}
2569 				}
2570 				if (base != NULL) {
2571 					pci_putl(bus, dev, func, offset,
2572 					    base | type);
2573 					base = pci_getl(bus, dev, func, offset);
2574 					base &= PCI_BASE_M_ADDR_M;
2575 				} else
2576 					cmn_err(CE_WARN, "failed to program "
2577 					    "mem space [%d/%d/%d] BAR@0x%x"
2578 					    " length 0x%x",
2579 					    bus, dev, func, offset, len);
2580 			}
2581 			assigned[nasgn].pci_phys_low = base;
2582 			nreg++, nasgn++;
2583 		}
2584 	}
2585 	switch (header) {
2586 	case PCI_HEADER_ZERO:
2587 		offset = PCI_CONF_ROM;
2588 		break;
2589 	case PCI_HEADER_PPB:
2590 		offset = PCI_BCNF_ROM;
2591 		break;
2592 	default: /* including PCI_HEADER_CARDBUS */
2593 		goto done;
2594 	}
2595 
2596 	/*
2597 	 * Add the expansion rom memory space
2598 	 * Determine the size of the ROM base reg; don't write reserved bits
2599 	 * ROM isn't in the PCI memory space.
2600 	 */
2601 	base = pci_getl(bus, dev, func, offset);
2602 	pci_putl(bus, dev, func, offset, PCI_BASE_ROM_ADDR_M);
2603 	value = pci_getl(bus, dev, func, offset);
2604 	pci_putl(bus, dev, func, offset, base);
2605 	if (value & PCI_BASE_ROM_ENABLE)
2606 		value &= PCI_BASE_ROM_ADDR_M;
2607 	else
2608 		value = 0;
2609 
2610 	if (value != 0) {
2611 		regs[nreg].pci_phys_hi = (PCI_ADDR_MEM32 | devloc) + offset;
2612 		assigned[nasgn].pci_phys_hi = (PCI_RELOCAT_B |
2613 		    PCI_ADDR_MEM32 | devloc) + offset;
2614 		base &= PCI_BASE_ROM_ADDR_M;
2615 		assigned[nasgn].pci_phys_low = base;
2616 		len = ((value ^ (value-1)) + 1) >> 1;
2617 		regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = len;
2618 		nreg++, nasgn++;
2619 		/* take it out of the memory resource */
2620 		if (base != NULL) {
2621 			(void) memlist_remove(mem_avail, base, len);
2622 			memlist_insert(mem_used, base, len);
2623 			pci_bus_res[bus].mem_size += len;
2624 		}
2625 	}
2626 
2627 	/*
2628 	 * Account for "legacy" (alias) video adapter resources
2629 	 */
2630 
2631 	/* add the three hard-decode, aliased address spaces for VGA */
2632 	if ((baseclass == PCI_CLASS_DISPLAY && subclass == PCI_DISPLAY_VGA) ||
2633 	    (baseclass == PCI_CLASS_NONE && subclass == PCI_NONE_VGA)) {
2634 
2635 		/* VGA hard decode 0x3b0-0x3bb */
2636 		regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi =
2637 		    (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc);
2638 		regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x3b0;
2639 		regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0xc;
2640 		nreg++, nasgn++;
2641 		(void) memlist_remove(io_avail, 0x3b0, 0xc);
2642 		memlist_insert(io_used, 0x3b0, 0xc);
2643 		pci_bus_res[bus].io_size += 0xc;
2644 
2645 		/* VGA hard decode 0x3c0-0x3df */
2646 		regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi =
2647 		    (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc);
2648 		regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x3c0;
2649 		regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0x20;
2650 		nreg++, nasgn++;
2651 		(void) memlist_remove(io_avail, 0x3c0, 0x20);
2652 		memlist_insert(io_used, 0x3c0, 0x20);
2653 		pci_bus_res[bus].io_size += 0x20;
2654 
2655 		/* Video memory */
2656 		regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi =
2657 		    (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_MEM32 | devloc);
2658 		regs[nreg].pci_phys_low =
2659 		    assigned[nasgn].pci_phys_low = 0xa0000;
2660 		regs[nreg].pci_size_low =
2661 		    assigned[nasgn].pci_size_low = 0x20000;
2662 		nreg++, nasgn++;
2663 		/* remove from MEM and PMEM space */
2664 		(void) memlist_remove(mem_avail, 0xa0000, 0x20000);
2665 		(void) memlist_remove(pmem_avail, 0xa0000, 0x20000);
2666 		memlist_insert(mem_used, 0xa0000, 0x20000);
2667 		pci_bus_res[bus].mem_size += 0x20000;
2668 	}
2669 
2670 	/* add the hard-decode, aliased address spaces for 8514 */
2671 	if ((baseclass == PCI_CLASS_DISPLAY) &&
2672 	    (subclass == PCI_DISPLAY_VGA) &&
2673 	    (progclass & PCI_DISPLAY_IF_8514)) {
2674 
2675 		/* hard decode 0x2e8 */
2676 		regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi =
2677 		    (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc);
2678 		regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x2e8;
2679 		regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0x1;
2680 		nreg++, nasgn++;
2681 		(void) memlist_remove(io_avail, 0x2e8, 0x1);
2682 		memlist_insert(io_used, 0x2e8, 0x1);
2683 		pci_bus_res[bus].io_size += 0x1;
2684 
2685 		/* hard decode 0x2ea-0x2ef */
2686 		regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi =
2687 		    (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc);
2688 		regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x2ea;
2689 		regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0x6;
2690 		nreg++, nasgn++;
2691 		(void) memlist_remove(io_avail, 0x2ea, 0x6);
2692 		memlist_insert(io_used, 0x2ea, 0x6);
2693 		pci_bus_res[bus].io_size += 0x6;
2694 	}
2695 
2696 done:
2697 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "reg",
2698 	    (int *)regs, nreg * sizeof (pci_regspec_t) / sizeof (int));
2699 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
2700 	    "assigned-addresses",
2701 	    (int *)assigned, nasgn * sizeof (pci_regspec_t) / sizeof (int));
2702 
2703 	return (reprogram);
2704 }
2705 
2706 static void
2707 add_ppb_props(dev_info_t *dip, uchar_t bus, uchar_t dev, uchar_t func,
2708     int pciex, ushort_t is_pci_bridge)
2709 {
2710 	char *dev_type;
2711 	int i;
2712 	uint_t val, io_range[2], mem_range[2], pmem_range[2];
2713 	uchar_t secbus = pci_getb(bus, dev, func, PCI_BCNF_SECBUS);
2714 	uchar_t subbus = pci_getb(bus, dev, func, PCI_BCNF_SUBBUS);
2715 	uchar_t progclass;
2716 
2717 	ASSERT(secbus <= subbus);
2718 
2719 	/*
2720 	 * Check if it's a subtractive PPB.
2721 	 */
2722 	progclass = pci_getb(bus, dev, func, PCI_CONF_PROGCLASS);
2723 	if (progclass == PCI_BRIDGE_PCI_IF_SUBDECODE)
2724 		pci_bus_res[secbus].subtractive = B_TRUE;
2725 
2726 	/*
2727 	 * Some BIOSes lie about max pci busses, we allow for
2728 	 * such mistakes here
2729 	 */
2730 	if (subbus > pci_bios_maxbus) {
2731 		pci_bios_maxbus = subbus;
2732 		alloc_res_array();
2733 	}
2734 
2735 	ASSERT(pci_bus_res[secbus].dip == NULL);
2736 	pci_bus_res[secbus].dip = dip;
2737 	pci_bus_res[secbus].par_bus = bus;
2738 
2739 	dev_type = (pciex && !is_pci_bridge) ? "pciex" : "pci";
2740 
2741 	/* setup bus number hierarchy */
2742 	pci_bus_res[secbus].sub_bus = subbus;
2743 	/*
2744 	 * Keep track of the largest subordinate bus number (this is essential
2745 	 * for peer busses because there is no other way of determining its
2746 	 * subordinate bus number).
2747 	 */
2748 	if (subbus > pci_bus_res[bus].sub_bus)
2749 		pci_bus_res[bus].sub_bus = subbus;
2750 	/*
2751 	 * Loop through subordinate busses, initializing their parent bus
2752 	 * field to this bridge's parent.  The subordinate busses' parent
2753 	 * fields may very well be further refined later, as child bridges
2754 	 * are enumerated.  (The value is to note that the subordinate busses
2755 	 * are not peer busses by changing their par_bus fields to anything
2756 	 * other than -1.)
2757 	 */
2758 	for (i = secbus + 1; i <= subbus; i++)
2759 		pci_bus_res[i].par_bus = bus;
2760 
2761 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
2762 	    "device_type", dev_type);
2763 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
2764 	    "#address-cells", 3);
2765 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
2766 	    "#size-cells", 2);
2767 
2768 	/*
2769 	 * Collect bridge window specifications, and use them to populate
2770 	 * the "avail" resources for the bus.  Not all of those resources will
2771 	 * end up being available; this is done top-down, and so the initial
2772 	 * collection of windows populates the 'ranges' property for the
2773 	 * bus node.  Later, as children are found, resources are removed from
2774 	 * the 'avail' list, so that it becomes the freelist for
2775 	 * this point in the tree.  ranges may be set again after bridge
2776 	 * reprogramming in fix_ppb_res(), in which case it's set from
2777 	 * used + avail.
2778 	 *
2779 	 * According to PPB spec, the base register should be programmed
2780 	 * with a value bigger than the limit register when there are
2781 	 * no resources available. This applies to io, memory, and
2782 	 * prefetchable memory.
2783 	 */
2784 
2785 	/*
2786 	 * io range
2787 	 * We determine i/o windows that are left unconfigured by BIOS
2788 	 * through its i/o enable bit as Microsoft recommends OEMs to do.
2789 	 * If it is unset, we disable i/o and mark it for reconfiguration in
2790 	 * later passes by setting the base > limit
2791 	 */
2792 	val = (uint_t)pci_getw(bus, dev, func, PCI_CONF_COMM);
2793 	if (val & PCI_COMM_IO) {
2794 		val = (uint_t)pci_getb(bus, dev, func, PCI_BCNF_IO_BASE_LOW);
2795 		io_range[0] = ((val & 0xf0) << 8);
2796 		val = (uint_t)pci_getb(bus, dev, func, PCI_BCNF_IO_LIMIT_LOW);
2797 		io_range[1]  = ((val & 0xf0) << 8) | 0xFFF;
2798 	} else {
2799 		io_range[0] = 0x9fff;
2800 		io_range[1] = 0x1000;
2801 		pci_putb(bus, dev, func, PCI_BCNF_IO_BASE_LOW,
2802 		    (uint8_t)((io_range[0] >> 8) & 0xf0));
2803 		pci_putb(bus, dev, func, PCI_BCNF_IO_LIMIT_LOW,
2804 		    (uint8_t)((io_range[1] >> 8) & 0xf0));
2805 		pci_putw(bus, dev, func, PCI_BCNF_IO_BASE_HI, 0);
2806 		pci_putw(bus, dev, func, PCI_BCNF_IO_LIMIT_HI, 0);
2807 	}
2808 
2809 	if (io_range[0] != 0 && io_range[0] < io_range[1]) {
2810 		memlist_insert(&pci_bus_res[secbus].io_avail,
2811 		    (uint64_t)io_range[0],
2812 		    (uint64_t)(io_range[1] - io_range[0] + 1));
2813 		memlist_insert(&pci_bus_res[bus].io_used,
2814 		    (uint64_t)io_range[0],
2815 		    (uint64_t)(io_range[1] - io_range[0] + 1));
2816 		if (pci_bus_res[bus].io_avail != NULL) {
2817 			(void) memlist_remove(&pci_bus_res[bus].io_avail,
2818 			    (uint64_t)io_range[0],
2819 			    (uint64_t)(io_range[1] - io_range[0] + 1));
2820 		}
2821 		dcmn_err(CE_NOTE, "bus %d io-range: 0x%x-%x",
2822 		    secbus, io_range[0], io_range[1]);
2823 		/* if 32-bit supported, make sure upper bits are not set */
2824 		if ((val & 0xf) == 1 &&
2825 		    pci_getw(bus, dev, func, PCI_BCNF_IO_BASE_HI)) {
2826 			cmn_err(CE_NOTE, "unsupported 32-bit IO address on"
2827 			    " pci-pci bridge [%d/%d/%d]", bus, dev, func);
2828 		}
2829 	}
2830 
2831 	/* mem range */
2832 	val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_MEM_BASE);
2833 	mem_range[0] = ((val & 0xFFF0) << 16);
2834 	val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_MEM_LIMIT);
2835 	mem_range[1] = ((val & 0xFFF0) << 16) | 0xFFFFF;
2836 	if (mem_range[0] != 0 && mem_range[0] < mem_range[1]) {
2837 		memlist_insert(&pci_bus_res[secbus].mem_avail,
2838 		    (uint64_t)mem_range[0],
2839 		    (uint64_t)(mem_range[1] - mem_range[0] + 1));
2840 		memlist_insert(&pci_bus_res[bus].mem_used,
2841 		    (uint64_t)mem_range[0],
2842 		    (uint64_t)(mem_range[1] - mem_range[0] + 1));
2843 		/* remove from parent resource list */
2844 		(void) memlist_remove(&pci_bus_res[bus].mem_avail,
2845 		    (uint64_t)mem_range[0],
2846 		    (uint64_t)(mem_range[1] - mem_range[0] + 1));
2847 		(void) memlist_remove(&pci_bus_res[bus].pmem_avail,
2848 		    (uint64_t)mem_range[0],
2849 		    (uint64_t)(mem_range[1] - mem_range[0] + 1));
2850 		dcmn_err(CE_NOTE, "bus %d mem-range: 0x%x-%x",
2851 		    secbus, mem_range[0], mem_range[1]);
2852 	}
2853 
2854 	/* prefetchable memory range */
2855 	val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_PF_BASE_LOW);
2856 	pmem_range[0] = ((val & 0xFFF0) << 16);
2857 	val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_PF_LIMIT_LOW);
2858 	pmem_range[1] = ((val & 0xFFF0) << 16) | 0xFFFFF;
2859 	if (pmem_range[0] != 0 && pmem_range[0] < pmem_range[1]) {
2860 		memlist_insert(&pci_bus_res[secbus].pmem_avail,
2861 		    (uint64_t)pmem_range[0],
2862 		    (uint64_t)(pmem_range[1] - pmem_range[0] + 1));
2863 		memlist_insert(&pci_bus_res[bus].pmem_used,
2864 		    (uint64_t)pmem_range[0],
2865 		    (uint64_t)(pmem_range[1] - pmem_range[0] + 1));
2866 		/* remove from parent resource list */
2867 		(void) memlist_remove(&pci_bus_res[bus].pmem_avail,
2868 		    (uint64_t)pmem_range[0],
2869 		    (uint64_t)(pmem_range[1] - pmem_range[0] + 1));
2870 		(void) memlist_remove(&pci_bus_res[bus].mem_avail,
2871 		    (uint64_t)pmem_range[0],
2872 		    (uint64_t)(pmem_range[1] - pmem_range[0] + 1));
2873 		dcmn_err(CE_NOTE, "bus %d pmem-range: 0x%x-%x",
2874 		    secbus, pmem_range[0], pmem_range[1]);
2875 		/* if 64-bit supported, make sure upper bits are not set */
2876 		if ((val & 0xf) == 1 &&
2877 		    pci_getl(bus, dev, func, PCI_BCNF_PF_BASE_HIGH)) {
2878 			cmn_err(CE_NOTE, "unsupported 64-bit prefetch memory on"
2879 			    " pci-pci bridge [%d/%d/%d]", bus, dev, func);
2880 		}
2881 	}
2882 
2883 	/*
2884 	 * Add VGA legacy resources to the bridge's pci_bus_res if it
2885 	 * has VGA_ENABLE set.  Note that we put them in 'avail',
2886 	 * because that's used to populate the ranges prop; they'll be
2887 	 * removed from there by the VGA device once it's found.  Also,
2888 	 * remove them from the parent's available list and note them as
2889 	 * used in the parent.
2890 	 */
2891 
2892 	if (pci_getw(bus, dev, func, PCI_BCNF_BCNTRL) &
2893 	    PCI_BCNF_BCNTRL_VGA_ENABLE) {
2894 
2895 		memlist_insert(&pci_bus_res[secbus].io_avail, 0x3b0, 0xc);
2896 
2897 		memlist_insert(&pci_bus_res[bus].io_used, 0x3b0, 0xc);
2898 		if (pci_bus_res[bus].io_avail != NULL) {
2899 			(void) memlist_remove(&pci_bus_res[bus].io_avail,
2900 			    0x3b0, 0xc);
2901 		}
2902 
2903 		memlist_insert(&pci_bus_res[secbus].io_avail, 0x3c0, 0x20);
2904 
2905 		memlist_insert(&pci_bus_res[bus].io_used, 0x3c0, 0x20);
2906 		if (pci_bus_res[bus].io_avail != NULL) {
2907 			(void) memlist_remove(&pci_bus_res[bus].io_avail,
2908 			    0x3c0, 0x20);
2909 		}
2910 
2911 		memlist_insert(&pci_bus_res[secbus].mem_avail, 0xa0000,
2912 		    0x20000);
2913 
2914 		memlist_insert(&pci_bus_res[bus].mem_used, 0xa0000, 0x20000);
2915 		if (pci_bus_res[bus].mem_avail != NULL) {
2916 			(void) memlist_remove(&pci_bus_res[bus].mem_avail,
2917 			    0xa0000, 0x20000);
2918 		}
2919 	}
2920 	add_bus_range_prop(secbus);
2921 	add_ranges_prop(secbus, 1);
2922 }
2923 
2924 extern const struct pci_class_strings_s class_pci[];
2925 extern int class_pci_items;
2926 
2927 static void
2928 add_model_prop(dev_info_t *dip, uint_t classcode)
2929 {
2930 	const char *desc;
2931 	int i;
2932 	uchar_t baseclass = classcode >> 16;
2933 	uchar_t subclass = (classcode >> 8) & 0xff;
2934 	uchar_t progclass = classcode & 0xff;
2935 
2936 	if ((baseclass == PCI_CLASS_MASS) && (subclass == PCI_MASS_IDE)) {
2937 		desc = "IDE controller";
2938 	} else {
2939 		for (desc = 0, i = 0; i < class_pci_items; i++) {
2940 			if ((baseclass == class_pci[i].base_class) &&
2941 			    (subclass == class_pci[i].sub_class) &&
2942 			    (progclass == class_pci[i].prog_class)) {
2943 				desc = class_pci[i].actual_desc;
2944 				break;
2945 			}
2946 		}
2947 		if (i == class_pci_items)
2948 			desc = "Unknown class of pci/pnpbios device";
2949 	}
2950 
2951 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, "model",
2952 	    (char *)desc);
2953 }
2954 
2955 static void
2956 add_bus_range_prop(int bus)
2957 {
2958 	int bus_range[2];
2959 
2960 	if (pci_bus_res[bus].dip == NULL)
2961 		return;
2962 	bus_range[0] = bus;
2963 	bus_range[1] = pci_bus_res[bus].sub_bus;
2964 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, pci_bus_res[bus].dip,
2965 	    "bus-range", (int *)bus_range, 2);
2966 }
2967 
2968 /*
2969  * Add slot-names property for any named pci hot-plug slots
2970  */
2971 static void
2972 add_bus_slot_names_prop(int bus)
2973 {
2974 	char slotprop[256];
2975 	int len;
2976 
2977 	if (pci_bus_res[bus].dip != NULL) {
2978 		/* simply return if the property is already defined */
2979 		if (ddi_prop_exists(DDI_DEV_T_ANY, pci_bus_res[bus].dip,
2980 		    DDI_PROP_DONTPASS, "slot-names"))
2981 			return;
2982 	}
2983 
2984 	len = pci_slot_names_prop(bus, slotprop, sizeof (slotprop));
2985 	if (len > 0) {
2986 		/*
2987 		 * Only create a peer bus node if this bus may be a peer bus.
2988 		 * It may be a peer bus if the dip is NULL and if par_bus is
2989 		 * -1 (par_bus is -1 if this bus was not found to be
2990 		 * subordinate to any PCI-PCI bridge).
2991 		 * If it's not a peer bus, then the ACPI BBN-handling code
2992 		 * will remove it later.
2993 		 */
2994 		if (pci_bus_res[bus].par_bus == (uchar_t)-1 &&
2995 		    pci_bus_res[bus].dip == NULL) {
2996 
2997 			create_root_bus_dip(bus);
2998 		}
2999 		if (pci_bus_res[bus].dip != NULL) {
3000 			ASSERT((len % sizeof (int)) == 0);
3001 			(void) ndi_prop_update_int_array(DDI_DEV_T_NONE,
3002 			    pci_bus_res[bus].dip, "slot-names",
3003 			    (int *)slotprop, len / sizeof (int));
3004 		} else {
3005 			cmn_err(CE_NOTE, "!BIOS BUG: Invalid bus number in PCI "
3006 			    "IRQ routing table; Not adding slot-names "
3007 			    "property for incorrect bus %d", bus);
3008 		}
3009 	}
3010 }
3011 
3012 /*
3013  * Handle both PCI root and PCI-PCI bridge range properties;
3014  * non-zero 'ppb' argument select PCI-PCI bridges versus root.
3015  */
3016 static void
3017 memlist_to_ranges(void **rp, struct memlist *entry, int type, int ppb)
3018 {
3019 	ppb_ranges_t *ppb_rp = *rp;
3020 	pci_ranges_t *pci_rp = *rp;
3021 
3022 	while (entry != NULL) {
3023 		if (ppb) {
3024 			ppb_rp->child_high = ppb_rp->parent_high = type;
3025 			ppb_rp->child_mid = ppb_rp->parent_mid =
3026 			    (uint32_t)(entry->address >> 32); /* XXX */
3027 			ppb_rp->child_low = ppb_rp->parent_low =
3028 			    (uint32_t)entry->address;
3029 			ppb_rp->size_high =
3030 			    (uint32_t)(entry->size >> 32); /* XXX */
3031 			ppb_rp->size_low = (uint32_t)entry->size;
3032 			*rp = ++ppb_rp;
3033 		} else {
3034 			pci_rp->child_high = type;
3035 			pci_rp->child_mid = pci_rp->parent_high =
3036 			    (uint32_t)(entry->address >> 32); /* XXX */
3037 			pci_rp->child_low = pci_rp->parent_low =
3038 			    (uint32_t)entry->address;
3039 			pci_rp->size_high =
3040 			    (uint32_t)(entry->size >> 32); /* XXX */
3041 			pci_rp->size_low = (uint32_t)entry->size;
3042 			*rp = ++pci_rp;
3043 		}
3044 		entry = entry->next;
3045 	}
3046 }
3047 
3048 static void
3049 add_ranges_prop(int bus, int ppb)
3050 {
3051 	int total, alloc_size;
3052 	void	*rp, *next_rp;
3053 	struct memlist *iolist, *memlist, *pmemlist;
3054 
3055 	/* no devinfo node - unused bus, return */
3056 	if (pci_bus_res[bus].dip == NULL)
3057 		return;
3058 
3059 	iolist = memlist = pmemlist = (struct memlist *)NULL;
3060 
3061 	memlist_merge(&pci_bus_res[bus].io_avail, &iolist);
3062 	memlist_merge(&pci_bus_res[bus].io_used, &iolist);
3063 	memlist_merge(&pci_bus_res[bus].mem_avail, &memlist);
3064 	memlist_merge(&pci_bus_res[bus].mem_used, &memlist);
3065 	memlist_merge(&pci_bus_res[bus].pmem_avail, &pmemlist);
3066 	memlist_merge(&pci_bus_res[bus].pmem_used, &pmemlist);
3067 
3068 	total = memlist_count(iolist);
3069 	total += memlist_count(memlist);
3070 	total += memlist_count(pmemlist);
3071 
3072 	/* no property is created if no ranges are present */
3073 	if (total == 0)
3074 		return;
3075 
3076 	alloc_size = total *
3077 	    (ppb ? sizeof (ppb_ranges_t) : sizeof (pci_ranges_t));
3078 
3079 	next_rp = rp = kmem_alloc(alloc_size, KM_SLEEP);
3080 
3081 	memlist_to_ranges(&next_rp, iolist, PCI_ADDR_IO | PCI_REG_REL_M, ppb);
3082 	memlist_to_ranges(&next_rp, memlist,
3083 	    PCI_ADDR_MEM32 | PCI_REG_REL_M, ppb);
3084 	memlist_to_ranges(&next_rp, pmemlist,
3085 	    PCI_ADDR_MEM32 | PCI_REG_REL_M | PCI_REG_PF_M, ppb);
3086 
3087 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, pci_bus_res[bus].dip,
3088 	    "ranges", (int *)rp, alloc_size / sizeof (int));
3089 
3090 	kmem_free(rp, alloc_size);
3091 	memlist_free_all(&iolist);
3092 	memlist_free_all(&memlist);
3093 	memlist_free_all(&pmemlist);
3094 }
3095 
3096 static void
3097 memlist_remove_list(struct memlist **list, struct memlist *remove_list)
3098 {
3099 	while (list && *list && remove_list) {
3100 		(void) memlist_remove(list, remove_list->address,
3101 		    remove_list->size);
3102 		remove_list = remove_list->next;
3103 	}
3104 }
3105 
3106 static int
3107 memlist_to_spec(struct pci_phys_spec *sp, struct memlist *list, int type)
3108 {
3109 	int i = 0;
3110 
3111 	while (list) {
3112 		/* assume 32-bit addresses */
3113 		sp->pci_phys_hi = type;
3114 		sp->pci_phys_mid = 0;
3115 		sp->pci_phys_low = (uint32_t)list->address;
3116 		sp->pci_size_hi = 0;
3117 		sp->pci_size_low = (uint32_t)list->size;
3118 
3119 		list = list->next;
3120 		sp++, i++;
3121 	}
3122 	return (i);
3123 }
3124 
3125 static void
3126 add_bus_available_prop(int bus)
3127 {
3128 	int i, count;
3129 	struct pci_phys_spec *sp;
3130 
3131 	/* no devinfo node - unused bus, return */
3132 	if (pci_bus_res[bus].dip == NULL)
3133 		return;
3134 
3135 	count = memlist_count(pci_bus_res[bus].io_avail) +
3136 	    memlist_count(pci_bus_res[bus].mem_avail) +
3137 	    memlist_count(pci_bus_res[bus].pmem_avail);
3138 
3139 	if (count == 0)		/* nothing available */
3140 		return;
3141 
3142 	sp = kmem_alloc(count * sizeof (*sp), KM_SLEEP);
3143 	i = memlist_to_spec(&sp[0], pci_bus_res[bus].io_avail,
3144 	    PCI_ADDR_IO | PCI_REG_REL_M);
3145 	i += memlist_to_spec(&sp[i], pci_bus_res[bus].mem_avail,
3146 	    PCI_ADDR_MEM32 | PCI_REG_REL_M);
3147 	i += memlist_to_spec(&sp[i], pci_bus_res[bus].pmem_avail,
3148 	    PCI_ADDR_MEM32 | PCI_REG_REL_M | PCI_REG_PF_M);
3149 	ASSERT(i == count);
3150 
3151 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, pci_bus_res[bus].dip,
3152 	    "available", (int *)sp,
3153 	    i * sizeof (struct pci_phys_spec) / sizeof (int));
3154 	kmem_free(sp, count * sizeof (*sp));
3155 }
3156 
3157 static void
3158 alloc_res_array(void)
3159 {
3160 	static int array_max = 0;
3161 	int old_max;
3162 	void *old_res;
3163 
3164 	if (array_max > pci_bios_maxbus + 1)
3165 		return;	/* array is big enough */
3166 
3167 	old_max = array_max;
3168 	old_res = pci_bus_res;
3169 
3170 	if (array_max == 0)
3171 		array_max = 16;	/* start with a reasonable number */
3172 
3173 	while (array_max < pci_bios_maxbus + 1)
3174 		array_max <<= 1;
3175 	pci_bus_res = (struct pci_bus_resource *)kmem_zalloc(
3176 	    array_max * sizeof (struct pci_bus_resource), KM_SLEEP);
3177 
3178 	if (old_res) {	/* copy content and free old array */
3179 		bcopy(old_res, pci_bus_res,
3180 		    old_max * sizeof (struct pci_bus_resource));
3181 		kmem_free(old_res, old_max * sizeof (struct pci_bus_resource));
3182 	}
3183 }
3184 
3185 static void
3186 create_ioapic_node(int bus, int dev, int fn, ushort_t vendorid,
3187     ushort_t deviceid)
3188 {
3189 	static dev_info_t *ioapicsnode = NULL;
3190 	static int numioapics = 0;
3191 	dev_info_t *ioapic_node;
3192 	uint64_t physaddr;
3193 	uint32_t lobase, hibase = 0;
3194 
3195 	/* BAR 0 contains the IOAPIC's memory-mapped I/O address */
3196 	lobase = (*pci_getl_func)(bus, dev, fn, PCI_CONF_BASE0);
3197 
3198 	/* We (and the rest of the world) only support memory-mapped IOAPICs */
3199 	if ((lobase & PCI_BASE_SPACE_M) != PCI_BASE_SPACE_MEM)
3200 		return;
3201 
3202 	if ((lobase & PCI_BASE_TYPE_M) == PCI_BASE_TYPE_ALL)
3203 		hibase = (*pci_getl_func)(bus, dev, fn, PCI_CONF_BASE0 + 4);
3204 
3205 	lobase &= PCI_BASE_M_ADDR_M;
3206 
3207 	physaddr = (((uint64_t)hibase) << 32) | lobase;
3208 
3209 	/*
3210 	 * Create a nexus node for all IOAPICs under the root node.
3211 	 */
3212 	if (ioapicsnode == NULL) {
3213 		if (ndi_devi_alloc(ddi_root_node(), IOAPICS_NODE_NAME,
3214 		    (pnode_t)DEVI_SID_NODEID, &ioapicsnode) != NDI_SUCCESS) {
3215 			return;
3216 		}
3217 		(void) ndi_devi_online(ioapicsnode, 0);
3218 	}
3219 
3220 	/*
3221 	 * Create a child node for this IOAPIC
3222 	 */
3223 	ioapic_node = ddi_add_child(ioapicsnode, IOAPICS_CHILD_NAME,
3224 	    DEVI_SID_NODEID, numioapics++);
3225 	if (ioapic_node == NULL) {
3226 		return;
3227 	}
3228 
3229 	/* Vendor and Device ID */
3230 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, ioapic_node,
3231 	    IOAPICS_PROP_VENID, vendorid);
3232 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, ioapic_node,
3233 	    IOAPICS_PROP_DEVID, deviceid);
3234 
3235 	/* device_type */
3236 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, ioapic_node,
3237 	    "device_type", IOAPICS_DEV_TYPE);
3238 
3239 	/* reg */
3240 	(void) ndi_prop_update_int64(DDI_DEV_T_NONE, ioapic_node,
3241 	    "reg", physaddr);
3242 }
3243 
3244 /*
3245  * NOTE: For PCIe slots, the name is generated from the slot number
3246  * information obtained from Slot Capabilities register.
3247  * For non-PCIe slots, it is generated based on the slot number
3248  * information in the PCI IRQ table.
3249  */
3250 static void
3251 pciex_slot_names_prop(dev_info_t *dip, ushort_t slot_num)
3252 {
3253 	char slotprop[256];
3254 	int len;
3255 
3256 	bzero(slotprop, sizeof (slotprop));
3257 
3258 	/* set mask to 1 as there is only one slot (i.e dev 0) */
3259 	*(uint32_t *)slotprop = 1;
3260 	len = 4;
3261 	(void) snprintf(slotprop + len, sizeof (slotprop) - len, "pcie%d",
3262 	    slot_num);
3263 	len += strlen(slotprop + len) + 1;
3264 	len += len % 4;
3265 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "slot-names",
3266 	    (int *)slotprop, len / sizeof (int));
3267 }
3268