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