xref: /titanic_44/usr/src/uts/intel/io/pci/pci_boot.c (revision 321502cd0930b1eb6d4805e17f16234f3e3ff4b2)
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 devices: need a way to add dynamically */
1174 	static struct ide_table ide_other[] = {
1175 		{0x1095, 0x3112},
1176 		{0x1095, 0x3114},
1177 		{0x1095, 0x3512},
1178 		{0, 0}
1179 	};
1180 
1181 	if (basecl != PCI_CLASS_MASS)
1182 		return (0);
1183 
1184 	if (subcl == PCI_MASS_IDE) {
1185 		return (1);
1186 	}
1187 
1188 	if (subcl != PCI_MASS_OTHER && subcl != PCI_MASS_SATA) {
1189 		return (0);
1190 	}
1191 
1192 	entry = &ide_other[0];
1193 	while (entry->venid) {
1194 		if (entry->venid == venid && entry->devid == devid)
1195 			return (1);
1196 		entry++;
1197 	}
1198 	return (check_pciide_prop(revid, venid, devid, subvenid, subdevid));
1199 }
1200 
1201 static int
1202 is_display(uint_t classcode)
1203 {
1204 	static uint_t disp_classes[] = {
1205 		0x000100,
1206 		0x030000,
1207 		0x030001
1208 	};
1209 	int i, nclasses = sizeof (disp_classes) / sizeof (uint_t);
1210 
1211 	for (i = 0; i < nclasses; i++) {
1212 		if (classcode == disp_classes[i])
1213 			return (1);
1214 	}
1215 	return (0);
1216 }
1217 
1218 static void
1219 add_undofix_entry(uint8_t bus, uint8_t dev, uint8_t fn,
1220     void (*undofn)(uint8_t, uint8_t, uint8_t))
1221 {
1222 	struct pci_fixundo *newundo;
1223 
1224 	newundo = kmem_alloc(sizeof (struct pci_fixundo), KM_SLEEP);
1225 
1226 	/*
1227 	 * Adding an item to this list means that we must turn its NMIENABLE
1228 	 * bit back on at a later time.
1229 	 */
1230 	newundo->bus = bus;
1231 	newundo->dev = dev;
1232 	newundo->fn = fn;
1233 	newundo->undofn = undofn;
1234 	newundo->next = undolist;
1235 
1236 	/* add to the undo list in LIFO order */
1237 	undolist = newundo;
1238 }
1239 
1240 void
1241 add_pci_fixes(void)
1242 {
1243 	int i;
1244 
1245 	for (i = 0; i <= pci_bios_nbus; i++) {
1246 		/*
1247 		 * For each bus, apply needed fixes to the appropriate devices.
1248 		 * This must be done before the main enumeration loop because
1249 		 * some fixes must be applied to devices normally encountered
1250 		 * later in the pci scan (e.g. if a fix to device 7 must be
1251 		 * applied before scanning device 6, applying fixes in the
1252 		 * normal enumeration loop would obviously be too late).
1253 		 */
1254 		enumerate_bus_devs(i, CONFIG_FIX);
1255 	}
1256 }
1257 
1258 void
1259 undo_pci_fixes(void)
1260 {
1261 	struct pci_fixundo *nextundo;
1262 	uint8_t bus, dev, fn;
1263 
1264 	/*
1265 	 * All fixes in the undo list are performed unconditionally.  Future
1266 	 * fixes may require selective undo.
1267 	 */
1268 	while (undolist != NULL) {
1269 
1270 		bus = undolist->bus;
1271 		dev = undolist->dev;
1272 		fn = undolist->fn;
1273 
1274 		(*(undolist->undofn))(bus, dev, fn);
1275 
1276 		nextundo = undolist->next;
1277 		kmem_free(undolist, sizeof (struct pci_fixundo));
1278 		undolist = nextundo;
1279 	}
1280 }
1281 
1282 static void
1283 undo_amd8111_pci_fix(uint8_t bus, uint8_t dev, uint8_t fn)
1284 {
1285 	uint8_t val8;
1286 
1287 	val8 = pci_getb(bus, dev, fn, LPC_IO_CONTROL_REG_1);
1288 	/*
1289 	 * The NMIONERR bit is turned back on to allow the SMM BIOS
1290 	 * to handle more critical PCI errors (e.g. PERR#).
1291 	 */
1292 	val8 |= AMD8111_ENABLENMI;
1293 	pci_putb(bus, dev, fn, LPC_IO_CONTROL_REG_1, val8);
1294 }
1295 
1296 static void
1297 pci_fix_amd8111(uint8_t bus, uint8_t dev, uint8_t fn)
1298 {
1299 	uint8_t val8;
1300 
1301 	val8 = pci_getb(bus, dev, fn, LPC_IO_CONTROL_REG_1);
1302 
1303 	if ((val8 & AMD8111_ENABLENMI) == 0)
1304 		return;
1305 
1306 	/*
1307 	 * We reset NMIONERR in the LPC because master-abort on the PCI
1308 	 * bridge side of the 8111 will cause NMI, which might cause SMI,
1309 	 * which sometimes prevents all devices from being enumerated.
1310 	 */
1311 	val8 &= ~AMD8111_ENABLENMI;
1312 
1313 	pci_putb(bus, dev, fn, LPC_IO_CONTROL_REG_1, val8);
1314 
1315 	add_undofix_entry(bus, dev, fn, undo_amd8111_pci_fix);
1316 }
1317 
1318 static void
1319 process_devfunc(uchar_t bus, uchar_t dev, uchar_t func, uchar_t header,
1320     ushort_t vendorid, int config_op)
1321 {
1322 	char nodename[32], unitaddr[5];
1323 	dev_info_t *dip;
1324 	uchar_t basecl, subcl, progcl, intr, revid;
1325 	ushort_t subvenid, subdevid, status;
1326 	ushort_t slot_num;
1327 	uint_t classcode, revclass;
1328 	int reprogram = 0, pciide = 0;
1329 	int power[2] = {1, 1};
1330 	int pciex = 0;
1331 	ushort_t is_pci_bridge = 0;
1332 	struct pci_devfunc *devlist = NULL, *entry = NULL;
1333 
1334 	ushort_t deviceid = pci_getw(bus, dev, func, PCI_CONF_DEVID);
1335 
1336 	switch (header & PCI_HEADER_TYPE_M) {
1337 	case PCI_HEADER_ZERO:
1338 		subvenid = pci_getw(bus, dev, func, PCI_CONF_SUBVENID);
1339 		subdevid = pci_getw(bus, dev, func, PCI_CONF_SUBSYSID);
1340 		break;
1341 	case PCI_HEADER_CARDBUS:
1342 		subvenid = pci_getw(bus, dev, func, PCI_CBUS_SUBVENID);
1343 		subdevid = pci_getw(bus, dev, func, PCI_CBUS_SUBSYSID);
1344 		/* Record the # of cardbus bridges found on the bus */
1345 		if (config_op == CONFIG_INFO)
1346 			pci_bus_res[bus].num_cbb++;
1347 		break;
1348 	default:
1349 		subvenid = 0;
1350 		subdevid = 0;
1351 		break;
1352 	}
1353 
1354 	if (config_op == CONFIG_FIX) {
1355 		if (vendorid == VENID_AMD && deviceid == DEVID_AMD8111_LPC) {
1356 			pci_fix_amd8111(bus, dev, func);
1357 		}
1358 		return;
1359 	}
1360 
1361 	/* XXX should be use generic names? derive from class? */
1362 	revclass = pci_getl(bus, dev, func, PCI_CONF_REVID);
1363 	classcode = revclass >> 8;
1364 	revid = revclass & 0xff;
1365 
1366 	/* figure out if this is pci-ide */
1367 	basecl = classcode >> 16;
1368 	subcl = (classcode >> 8) & 0xff;
1369 	progcl = classcode & 0xff;
1370 
1371 
1372 	if (is_display(classcode))
1373 		(void) snprintf(nodename, sizeof (nodename), "display");
1374 	else if (subvenid != 0)
1375 		(void) snprintf(nodename, sizeof (nodename),
1376 		    "pci%x,%x", subvenid, subdevid);
1377 	else
1378 		(void) snprintf(nodename, sizeof (nodename),
1379 		    "pci%x,%x", vendorid, deviceid);
1380 
1381 	/* make sure parent bus dip has been created */
1382 	if (pci_bus_res[bus].dip == NULL) {
1383 		create_root_bus_dip(bus);
1384 	}
1385 
1386 	ndi_devi_alloc_sleep(pci_bus_res[bus].dip, nodename,
1387 	    DEVI_SID_NODEID, &dip);
1388 
1389 	if (check_if_device_is_pciex(dip, bus, dev, func, &slot_num,
1390 	    &is_pci_bridge) == B_TRUE)
1391 		pciex = 1;
1392 
1393 	/* add properties */
1394 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, "device-id", deviceid);
1395 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, "vendor-id", vendorid);
1396 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, "revision-id", revid);
1397 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1398 	    "class-code", classcode);
1399 	if (func == 0)
1400 		(void) snprintf(unitaddr, sizeof (unitaddr), "%x", dev);
1401 	else
1402 		(void) snprintf(unitaddr, sizeof (unitaddr),
1403 		    "%x,%x", dev, func);
1404 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1405 	    "unit-address", unitaddr);
1406 
1407 	/* add device_type for display nodes */
1408 	if (is_display(classcode)) {
1409 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1410 		    "device_type", "display");
1411 	}
1412 	/* add special stuff for header type */
1413 	if ((header & PCI_HEADER_TYPE_M) == PCI_HEADER_ZERO) {
1414 		uchar_t mingrant = pci_getb(bus, dev, func, PCI_CONF_MIN_G);
1415 		uchar_t maxlatency = pci_getb(bus, dev, func, PCI_CONF_MAX_L);
1416 
1417 		if (subvenid != 0) {
1418 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1419 			    "subsystem-id", subdevid);
1420 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1421 			    "subsystem-vendor-id", subvenid);
1422 		}
1423 		if (!pciex)
1424 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1425 			    "min-grant", mingrant);
1426 		if (!pciex)
1427 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1428 			    "max-latency", maxlatency);
1429 	}
1430 
1431 	/* interrupt, record if not 0 */
1432 	intr = pci_getb(bus, dev, func, PCI_CONF_IPIN);
1433 	if (intr != 0)
1434 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1435 		    "interrupts", intr);
1436 
1437 	/*
1438 	 * Add support for 133 mhz pci eventually
1439 	 */
1440 	status = pci_getw(bus, dev, func, PCI_CONF_STAT);
1441 
1442 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1443 	    "devsel-speed", (status & PCI_STAT_DEVSELT) >> 9);
1444 	if (!pciex && (status & PCI_STAT_FBBC))
1445 		(void) ndi_prop_create_boolean(DDI_DEV_T_NONE, dip,
1446 		    "fast-back-to-back");
1447 	if (!pciex && (status & PCI_STAT_66MHZ))
1448 		(void) ndi_prop_create_boolean(DDI_DEV_T_NONE, dip,
1449 		    "66mhz-capable");
1450 	if (status & PCI_STAT_UDF)
1451 		(void) ndi_prop_create_boolean(DDI_DEV_T_NONE, dip,
1452 		    "udf-supported");
1453 	if (pciex && slot_num) {
1454 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1455 		    "physical-slot#", slot_num);
1456 		if (!is_pci_bridge)
1457 			pciex_slot_names_prop(dip, slot_num);
1458 	}
1459 
1460 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
1461 	    "power-consumption", power, 2);
1462 
1463 	if ((basecl == PCI_CLASS_BRIDGE) && (subcl == PCI_BRIDGE_PCI))
1464 		add_ppb_props(dip, bus, dev, func, pciex);
1465 	else {
1466 		/*
1467 		 * Record the non-PPB devices on the bus for possible
1468 		 * reprogramming at 2nd bus enumeration.
1469 		 * Note: PPB reprogramming is done in fix_ppb_res()
1470 		 */
1471 		devlist = (struct pci_devfunc *)pci_bus_res[bus].privdata;
1472 		entry = kmem_zalloc(sizeof (*entry), KM_SLEEP);
1473 		entry->dip = dip;
1474 		entry->dev = dev;
1475 		entry->func = func;
1476 		entry->next = devlist;
1477 		pci_bus_res[bus].privdata = entry;
1478 	}
1479 
1480 	if (config_op == CONFIG_INFO &&
1481 	    IS_CLASS_IOAPIC(basecl, subcl, progcl)) {
1482 		create_ioapic_node(bus, dev, func, vendorid, deviceid);
1483 	}
1484 
1485 	/* check for ck8-04 based PCI ISA bridge only */
1486 	if (NVIDIA_IS_LPC_BRIDGE(vendorid, deviceid) && (dev == 1) &&
1487 	    (func == 0))
1488 		add_nvidia_isa_bridge_props(dip, bus, dev, func);
1489 
1490 	if (pciex && is_pci_bridge)
1491 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, "model",
1492 		    (char *)"PCIe-PCI bridge");
1493 	else
1494 		add_model_prop(dip, classcode);
1495 
1496 	add_compatible(dip, subvenid, subdevid, vendorid, deviceid,
1497 	    revid, classcode, pciex);
1498 
1499 	/*
1500 	 * See if this device is a controller that advertises
1501 	 * itself to be a standard ATA task file controller, or one that
1502 	 * has been hard coded.
1503 	 *
1504 	 * If it is, check if any other higher precedence driver listed in
1505 	 * driver_aliases will claim the node by calling
1506 	 * ddi_compatibile_driver_major.  If so, clear pciide and do not
1507 	 * create a pci-ide node or any other special handling.
1508 	 *
1509 	 * If another driver does not bind, set the node name to pci-ide
1510 	 * and then let the special pci-ide handling for registers and
1511 	 * child pci-ide nodes proceed below.
1512 	 */
1513 	if (is_pciide(basecl, subcl, revid, vendorid, deviceid,
1514 	    subvenid, subdevid) == 1) {
1515 		if (ddi_compatible_driver_major(dip, NULL) == (major_t)-1) {
1516 			(void) ndi_devi_set_nodename(dip, "pci-ide", 0);
1517 			pciide = 1;
1518 		}
1519 	}
1520 
1521 	reprogram = add_reg_props(dip, bus, dev, func, config_op, pciide);
1522 	(void) ndi_devi_bind_driver(dip, 0);
1523 
1524 	/* special handling for pci-ide */
1525 	if (pciide) {
1526 		dev_info_t *cdip;
1527 
1528 		/*
1529 		 * Create properties specified by P1275 Working Group
1530 		 * Proposal #414 Version 1
1531 		 */
1532 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1533 		    "device_type", "pci-ide");
1534 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1535 		    "#address-cells", 1);
1536 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1537 		    "#size-cells", 0);
1538 
1539 		/* allocate two child nodes */
1540 		ndi_devi_alloc_sleep(dip, "ide",
1541 		    (pnode_t)DEVI_SID_NODEID, &cdip);
1542 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cdip,
1543 		    "reg", 0);
1544 		(void) ndi_devi_bind_driver(cdip, 0);
1545 		ndi_devi_alloc_sleep(dip, "ide",
1546 		    (pnode_t)DEVI_SID_NODEID, &cdip);
1547 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cdip,
1548 		    "reg", 1);
1549 		(void) ndi_devi_bind_driver(cdip, 0);
1550 
1551 		reprogram = 0;	/* don't reprogram pci-ide bridge */
1552 	}
1553 
1554 	if (reprogram && (entry != NULL))
1555 		entry->reprogram = B_TRUE;
1556 }
1557 
1558 /*
1559  * Set the compatible property to a value compliant with
1560  * rev 2.1 of the IEEE1275 PCI binding.
1561  * (Also used for PCI-Express devices).
1562  *
1563  *   pciVVVV,DDDD.SSSS.ssss.RR	(0)
1564  *   pciVVVV,DDDD.SSSS.ssss	(1)
1565  *   pciSSSS,ssss		(2)
1566  *   pciVVVV,DDDD.RR		(3)
1567  *   pciVVVV,DDDD		(4)
1568  *   pciclass,CCSSPP		(5)
1569  *   pciclass,CCSS		(6)
1570  *
1571  * The Subsystem (SSSS) forms are not inserted if
1572  * subsystem-vendor-id is 0.
1573  *
1574  * NOTE: For PCI-Express devices "pci" is replaced with "pciex" in 0-6 above
1575  * property 2 is not created as per "1275 bindings for PCI Express Interconnect"
1576  *
1577  * Set with setprop and \x00 between each
1578  * to generate the encoded string array form.
1579  */
1580 void
1581 add_compatible(dev_info_t *dip, ushort_t subvenid, ushort_t subdevid,
1582     ushort_t vendorid, ushort_t deviceid, uchar_t revid, uint_t classcode,
1583     int pciex)
1584 {
1585 	int i = 0;
1586 	int size = COMPAT_BUFSIZE;
1587 	char *compat[13];
1588 	char *buf, *curr;
1589 
1590 	curr = buf = kmem_alloc(size, KM_SLEEP);
1591 
1592 	if (pciex) {
1593 		if (subvenid) {
1594 			compat[i++] = curr;	/* form 0 */
1595 			(void) snprintf(curr, size, "pciex%x,%x.%x.%x.%x",
1596 			    vendorid, deviceid, subvenid, subdevid, revid);
1597 			size -= strlen(curr) + 1;
1598 			curr += strlen(curr) + 1;
1599 
1600 			compat[i++] = curr;	/* form 1 */
1601 			(void) snprintf(curr, size, "pciex%x,%x.%x.%x",
1602 			    vendorid, deviceid, subvenid, subdevid);
1603 			size -= strlen(curr) + 1;
1604 			curr += strlen(curr) + 1;
1605 
1606 		}
1607 		compat[i++] = curr;	/* form 3 */
1608 		(void) snprintf(curr, size, "pciex%x,%x.%x",
1609 		    vendorid, deviceid, revid);
1610 		size -= strlen(curr) + 1;
1611 		curr += strlen(curr) + 1;
1612 
1613 		compat[i++] = curr;	/* form 4 */
1614 		(void) snprintf(curr, size, "pciex%x,%x", vendorid, deviceid);
1615 		size -= strlen(curr) + 1;
1616 		curr += strlen(curr) + 1;
1617 
1618 		compat[i++] = curr;	/* form 5 */
1619 		(void) snprintf(curr, size, "pciexclass,%06x", classcode);
1620 		size -= strlen(curr) + 1;
1621 		curr += strlen(curr) + 1;
1622 
1623 		compat[i++] = curr;	/* form 6 */
1624 		(void) snprintf(curr, size, "pciexclass,%04x",
1625 		    (classcode >> 8));
1626 		size -= strlen(curr) + 1;
1627 		curr += strlen(curr) + 1;
1628 	}
1629 
1630 	if (subvenid) {
1631 		compat[i++] = curr;	/* form 0 */
1632 		(void) snprintf(curr, size, "pci%x,%x.%x.%x.%x",
1633 		    vendorid, deviceid, subvenid, subdevid, revid);
1634 		size -= strlen(curr) + 1;
1635 		curr += strlen(curr) + 1;
1636 
1637 		compat[i++] = curr;	/* form 1 */
1638 		(void) snprintf(curr, size, "pci%x,%x.%x.%x",
1639 		    vendorid, deviceid, subvenid, subdevid);
1640 		size -= strlen(curr) + 1;
1641 		curr += strlen(curr) + 1;
1642 
1643 		compat[i++] = curr;	/* form 2 */
1644 		(void) snprintf(curr, size, "pci%x,%x", subvenid, subdevid);
1645 		size -= strlen(curr) + 1;
1646 		curr += strlen(curr) + 1;
1647 	}
1648 	compat[i++] = curr;	/* form 3 */
1649 	(void) snprintf(curr, size, "pci%x,%x.%x", vendorid, deviceid, revid);
1650 	size -= strlen(curr) + 1;
1651 	curr += strlen(curr) + 1;
1652 
1653 	compat[i++] = curr;	/* form 4 */
1654 	(void) snprintf(curr, size, "pci%x,%x", vendorid, deviceid);
1655 	size -= strlen(curr) + 1;
1656 	curr += strlen(curr) + 1;
1657 
1658 	compat[i++] = curr;	/* form 5 */
1659 	(void) snprintf(curr, size, "pciclass,%06x", classcode);
1660 	size -= strlen(curr) + 1;
1661 	curr += strlen(curr) + 1;
1662 
1663 	compat[i++] = curr;	/* form 6 */
1664 	(void) snprintf(curr, size, "pciclass,%04x", (classcode >> 8));
1665 	size -= strlen(curr) + 1;
1666 	curr += strlen(curr) + 1;
1667 
1668 	(void) ndi_prop_update_string_array(DDI_DEV_T_NONE, dip,
1669 	    "compatible", compat, i);
1670 	kmem_free(buf, COMPAT_BUFSIZE);
1671 }
1672 
1673 /*
1674  * Adjust the reg properties for a dual channel PCI-IDE device.
1675  *
1676  * NOTE: don't do anything that changes the order of the hard-decodes
1677  * and programmed BARs. The kernel driver depends on these values
1678  * being in this order regardless of whether they're for a 'native'
1679  * mode BAR or not.
1680  */
1681 /*
1682  * config info for pci-ide devices
1683  */
1684 static struct {
1685 	uchar_t  native_mask;	/* 0 == 'compatibility' mode, 1 == native */
1686 	uchar_t  bar_offset;	/* offset for alt status register */
1687 	ushort_t addr;		/* compatibility mode base address */
1688 	ushort_t length;	/* number of ports for this BAR */
1689 } pciide_bar[] = {
1690 	{ 0x01, 0, 0x1f0, 8 },	/* primary lower BAR */
1691 	{ 0x01, 2, 0x3f6, 1 },	/* primary upper BAR */
1692 	{ 0x04, 0, 0x170, 8 },	/* secondary lower BAR */
1693 	{ 0x04, 2, 0x376, 1 }	/* secondary upper BAR */
1694 };
1695 
1696 static int
1697 pciIdeAdjustBAR(uchar_t progcl, int index, uint_t *basep, uint_t *lenp)
1698 {
1699 	int hard_decode = 0;
1700 
1701 	/*
1702 	 * Adjust the base and len for the BARs of the PCI-IDE
1703 	 * device's primary and secondary controllers. The first
1704 	 * two BARs are for the primary controller and the next
1705 	 * two BARs are for the secondary controller. The fifth
1706 	 * and sixth bars are never adjusted.
1707 	 */
1708 	if (index >= 0 && index <= 3) {
1709 		*lenp = pciide_bar[index].length;
1710 
1711 		if (progcl & pciide_bar[index].native_mask) {
1712 			*basep += pciide_bar[index].bar_offset;
1713 		} else {
1714 			*basep = pciide_bar[index].addr;
1715 			hard_decode = 1;
1716 		}
1717 	}
1718 
1719 	/*
1720 	 * if either base or len is zero make certain both are zero
1721 	 */
1722 	if (*basep == 0 || *lenp == 0) {
1723 		*basep = 0;
1724 		*lenp = 0;
1725 		hard_decode = 0;
1726 	}
1727 
1728 	return (hard_decode);
1729 }
1730 
1731 
1732 /*
1733  * Add the "reg" and "assigned-addresses" property
1734  */
1735 static int
1736 add_reg_props(dev_info_t *dip, uchar_t bus, uchar_t dev, uchar_t func,
1737     int config_op, int pciide)
1738 {
1739 	uchar_t baseclass, subclass, progclass, header;
1740 	ushort_t bar_sz;
1741 	uint_t value = 0, len, devloc;
1742 	uint_t base, base_hi, type;
1743 	ushort_t offset, end;
1744 	int max_basereg, j, reprogram = 0;
1745 	uint_t phys_hi;
1746 	struct memlist **io_res, **mres, **mem_res, **pmem_res;
1747 	struct memlist **io_res_used, **mres_used;
1748 	struct memlist **mem_res_used, **pmem_res_used;
1749 	uchar_t res_bus;
1750 	uint16_t cmd_reg;
1751 
1752 	pci_regspec_t regs[16] = {{0}};
1753 	pci_regspec_t assigned[15] = {{0}};
1754 	int nreg, nasgn, enable = 0;
1755 
1756 	io_res = &pci_bus_res[bus].io_ports;
1757 	io_res_used = &pci_bus_res[bus].io_ports_used;
1758 	mem_res = &pci_bus_res[bus].mem_space;
1759 	mem_res_used = &pci_bus_res[bus].mem_space_used;
1760 	if (bus == 0) {	/* for bus 0, there is only mem_space */
1761 		pmem_res = mem_res;
1762 		pmem_res_used = mem_res_used;
1763 	} else {
1764 		pmem_res = &pci_bus_res[bus].pmem_space;
1765 		pmem_res_used = &pci_bus_res[bus].pmem_space_used;
1766 	}
1767 
1768 	devloc = (uint_t)bus << 16 | (uint_t)dev << 11 | (uint_t)func << 8;
1769 	regs[0].pci_phys_hi = devloc;
1770 	nreg = 1;	/* rest of regs[0] is all zero */
1771 	nasgn = 0;
1772 
1773 	baseclass = pci_getb(bus, dev, func, PCI_CONF_BASCLASS);
1774 	subclass = pci_getb(bus, dev, func, PCI_CONF_SUBCLASS);
1775 	progclass = pci_getb(bus, dev, func, PCI_CONF_PROGCLASS);
1776 	header = pci_getb(bus, dev, func, PCI_CONF_HEADER) & PCI_HEADER_TYPE_M;
1777 
1778 	switch (header) {
1779 	case PCI_HEADER_ZERO:
1780 		max_basereg = PCI_BASE_NUM;
1781 		break;
1782 	case PCI_HEADER_PPB:
1783 		max_basereg = PCI_BCNF_BASE_NUM;
1784 		break;
1785 	case PCI_HEADER_CARDBUS:
1786 		max_basereg = PCI_CBUS_BASE_NUM;
1787 		break;
1788 	default:
1789 		max_basereg = 0;
1790 		break;
1791 	}
1792 
1793 	/*
1794 	 * Create the register property by saving the current
1795 	 * value of the base register. Write 0xffffffff to the
1796 	 * base register.  Read the value back to determine the
1797 	 * required size of the address space.  Restore the base
1798 	 * register contents.
1799 	 *
1800 	 * Do not disable I/O and memory access; this isn't necessary
1801 	 * since no driver is yet attached to this device, and disabling
1802 	 * I/O and memory access has the side-effect of disabling PCI-PCI
1803 	 * bridge mappings, which makes the bridge transparent to secondary-
1804 	 * bus activity (see sections 4.1-4.3 of the PCI-PCI Bridge
1805 	 * Spec V1.2).
1806 	 */
1807 	end = PCI_CONF_BASE0 + max_basereg * sizeof (uint_t);
1808 	for (j = 0, offset = PCI_CONF_BASE0; offset < end;
1809 	    j++, offset += bar_sz) {
1810 		int hard_decode = 0;
1811 
1812 		/* determine the size of the address space */
1813 		base = pci_getl(bus, dev, func, offset);
1814 		pci_putl(bus, dev, func, offset, 0xffffffff);
1815 		value = pci_getl(bus, dev, func, offset);
1816 		pci_putl(bus, dev, func, offset, base);
1817 
1818 		/* construct phys hi,med.lo, size hi, lo */
1819 		if ((pciide && j < 4) || (base & PCI_BASE_SPACE_IO)) {
1820 			/* i/o space */
1821 			bar_sz = PCI_BAR_SZ_32;
1822 			value &= PCI_BASE_IO_ADDR_M;
1823 			len = ((value ^ (value-1)) + 1) >> 1;
1824 
1825 			/* XXX Adjust first 4 IDE registers */
1826 			if (pciide) {
1827 				if (subclass != PCI_MASS_IDE)
1828 					progclass = (PCI_IDE_IF_NATIVE_PRI |
1829 					    PCI_IDE_IF_NATIVE_SEC);
1830 				hard_decode = pciIdeAdjustBAR(progclass, j,
1831 				    &base, &len);
1832 			} else if (value == 0) {
1833 				/* skip base regs with size of 0 */
1834 				continue;
1835 			}
1836 
1837 			regs[nreg].pci_size_low =
1838 			    assigned[nasgn].pci_size_low = len;
1839 			if (!hard_decode) {
1840 				regs[nreg].pci_phys_hi =
1841 				    (PCI_ADDR_IO | devloc) + offset;
1842 			} else {
1843 				regs[nreg].pci_phys_hi =
1844 				    (PCI_RELOCAT_B | PCI_ADDR_IO | devloc) +
1845 				    offset;
1846 				regs[nreg].pci_phys_low =
1847 				    base & PCI_BASE_IO_ADDR_M;
1848 			}
1849 			assigned[nasgn].pci_phys_hi =
1850 			    (PCI_RELOCAT_B | PCI_ADDR_IO | devloc) + offset;
1851 			type = base & (~PCI_BASE_IO_ADDR_M);
1852 			base &= PCI_BASE_IO_ADDR_M;
1853 			/*
1854 			 * A device under a subtractive PPB can allocate
1855 			 * resources from its parent bus if there is no resource
1856 			 * available on its own bus.
1857 			 */
1858 			if ((config_op == CONFIG_NEW) && (*io_res == NULL)) {
1859 				res_bus = bus;
1860 				while (pci_bus_res[res_bus].subtractive) {
1861 					res_bus = pci_bus_res[res_bus].par_bus;
1862 					if (res_bus == (uchar_t)-1)
1863 						break; /* root bus already */
1864 					if (pci_bus_res[res_bus].io_ports) {
1865 						io_res = &pci_bus_res
1866 						    [res_bus].io_ports;
1867 						break;
1868 					}
1869 				}
1870 			}
1871 
1872 			/*
1873 			 * first pass - gather what's there
1874 			 * update/second pass - adjust/allocate regions
1875 			 *	config - allocate regions
1876 			 */
1877 			if (config_op == CONFIG_INFO) {	/* first pass */
1878 				/* take out of the resource map of the bus */
1879 				if (base != 0) {
1880 					if (*io_res)
1881 						(void) memlist_remove(io_res,
1882 						    base, len);
1883 					memlist_insert(io_res_used, base, len);
1884 				} else
1885 					reprogram = 1;
1886 			} else if ((*io_res && base == 0) ||
1887 			    pci_bus_res[bus].io_reprogram) {
1888 				base = (uint_t)memlist_find(io_res, len, 0x4);
1889 				if (base != 0) {
1890 					memlist_insert(io_res_used, base, len);
1891 					/* XXX need to worry about 64-bit? */
1892 					pci_putl(bus, dev, func, offset,
1893 					    base | type);
1894 					base = pci_getl(bus, dev, func, offset);
1895 					base &= PCI_BASE_IO_ADDR_M;
1896 				}
1897 				if (base == 0) {
1898 					cmn_err(CE_WARN, "failed to program"
1899 					    " IO space [%d/%d/%d] BAR@0x%x"
1900 					    " length 0x%x",
1901 					    bus, dev, func, offset, len);
1902 				} else
1903 					enable |= PCI_COMM_IO;
1904 			}
1905 			assigned[nasgn].pci_phys_low = base;
1906 			nreg++, nasgn++;
1907 
1908 		} else {
1909 			/* memory space */
1910 			if ((base & PCI_BASE_TYPE_M) == PCI_BASE_TYPE_ALL) {
1911 				bar_sz = PCI_BAR_SZ_64;
1912 				base_hi = pci_getl(bus, dev, func, offset + 4);
1913 				phys_hi = PCI_ADDR_MEM64;
1914 			} else {
1915 				bar_sz = PCI_BAR_SZ_32;
1916 				base_hi = 0;
1917 				phys_hi = PCI_ADDR_MEM32;
1918 			}
1919 
1920 			/* skip base regs with size of 0 */
1921 			value &= PCI_BASE_M_ADDR_M;
1922 
1923 			if (value == 0) {
1924 				continue;
1925 			}
1926 			len = ((value ^ (value-1)) + 1) >> 1;
1927 			regs[nreg].pci_size_low =
1928 			    assigned[nasgn].pci_size_low = len;
1929 
1930 			phys_hi |= (devloc | offset);
1931 			if (base & PCI_BASE_PREF_M) {
1932 				mres = pmem_res;
1933 				mres_used = pmem_res_used;
1934 				phys_hi |= PCI_PREFETCH_B;
1935 			} else {
1936 				mres = mem_res;
1937 				mres_used = mem_res_used;
1938 			}
1939 			/*
1940 			 * A device under a subtractive PPB can allocate
1941 			 * resources from its parent bus if there is no resource
1942 			 * available on its own bus.
1943 			 */
1944 			if ((config_op == CONFIG_NEW) && (*mres == NULL)) {
1945 				res_bus = bus;
1946 				while (pci_bus_res[res_bus].subtractive) {
1947 					res_bus = pci_bus_res[res_bus].par_bus;
1948 					if (res_bus == (uchar_t)-1)
1949 						break; /* root bus already */
1950 					if ((phys_hi & PCI_PREFETCH_B) &&
1951 					    (res_bus != 0))
1952 						mres = &pci_bus_res
1953 						    [res_bus].pmem_space;
1954 					else
1955 						mres = &pci_bus_res
1956 						    [res_bus].mem_space;
1957 					if (*mres)
1958 						break;
1959 				}
1960 			}
1961 
1962 			regs[nreg].pci_phys_hi =
1963 			    assigned[nasgn].pci_phys_hi = phys_hi;
1964 			assigned[nasgn].pci_phys_hi |= PCI_RELOCAT_B;
1965 			assigned[nasgn].pci_phys_mid = base_hi;
1966 			type = base & ~PCI_BASE_M_ADDR_M;
1967 			base &= PCI_BASE_M_ADDR_M;
1968 
1969 			if (config_op == CONFIG_INFO) {
1970 				/* take out of the resource map of the bus */
1971 				if (base != 0) {
1972 					if (*mres)
1973 						(void) memlist_remove(mres,
1974 						    base, len);
1975 					memlist_insert(mres_used, base, len);
1976 				} else
1977 					reprogram = 1;
1978 			} else if ((*mres && base == 0) ||
1979 			    pci_bus_res[bus].mem_reprogram) {
1980 				base = (uint_t)memlist_find(mres, len, 0x1000);
1981 				if (base != NULL) {
1982 					memlist_insert(mres_used, base, len);
1983 					pci_putl(bus, dev, func, offset,
1984 					    base | type);
1985 					base = pci_getl(bus, dev, func, offset);
1986 					base &= PCI_BASE_M_ADDR_M;
1987 				}
1988 
1989 				if (base == 0) {
1990 					cmn_err(CE_WARN, "failed to program "
1991 					    "mem space [%d/%d/%d] BAR@0x%x"
1992 					    " length 0x%x",
1993 					    bus, dev, func, offset, len);
1994 				} else
1995 					enable |= PCI_COMM_MAE;
1996 			}
1997 			assigned[nasgn].pci_phys_low = base;
1998 			nreg++, nasgn++;
1999 		}
2000 	}
2001 	switch (header) {
2002 	case PCI_HEADER_ZERO:
2003 		offset = PCI_CONF_ROM;
2004 		break;
2005 	case PCI_HEADER_PPB:
2006 		offset = PCI_BCNF_ROM;
2007 		break;
2008 	default: /* including PCI_HEADER_CARDBUS */
2009 		goto done;
2010 	}
2011 
2012 	/*
2013 	 * Add the expansion rom memory space
2014 	 * Determine the size of the ROM base reg; don't write reserved bits
2015 	 * ROM isn't in the PCI memory space.
2016 	 */
2017 	base = pci_getl(bus, dev, func, offset);
2018 	pci_putl(bus, dev, func, offset, PCI_BASE_ROM_ADDR_M);
2019 	value = pci_getl(bus, dev, func, offset);
2020 	pci_putl(bus, dev, func, offset, base);
2021 	if (value & PCI_BASE_ROM_ENABLE)
2022 		value &= PCI_BASE_ROM_ADDR_M;
2023 	else
2024 		value = 0;
2025 
2026 	if (value != 0) {
2027 		regs[nreg].pci_phys_hi = (PCI_ADDR_MEM32 | devloc) + offset;
2028 		assigned[nasgn].pci_phys_hi = (PCI_RELOCAT_B |
2029 		    PCI_ADDR_MEM32 | devloc) + offset;
2030 		base &= PCI_BASE_ROM_ADDR_M;
2031 		assigned[nasgn].pci_phys_low = base;
2032 		len = ((value ^ (value-1)) + 1) >> 1;
2033 		regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = len;
2034 		nreg++, nasgn++;
2035 		/* take it out of the memory resource */
2036 		if (*mem_res && base != 0)
2037 			(void) memlist_remove(mem_res, base, len);
2038 		if (base != 0)
2039 			memlist_insert(mem_res, base, len);
2040 	}
2041 
2042 	/*
2043 	 * The following are ISA resources. There are not part
2044 	 * of the PCI local bus resources. So don't attempt to
2045 	 * do resource accounting against PCI.
2046 	 */
2047 
2048 	/* add the three hard-decode, aliased address spaces for VGA */
2049 	if ((baseclass == PCI_CLASS_DISPLAY && subclass == PCI_DISPLAY_VGA) ||
2050 	    (baseclass == PCI_CLASS_NONE && subclass == PCI_NONE_VGA)) {
2051 
2052 		/* VGA hard decode 0x3b0-0x3bb */
2053 		regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi =
2054 		    (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc);
2055 		regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x3b0;
2056 		regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0xc;
2057 		nreg++, nasgn++;
2058 
2059 		/* VGA hard decode 0x3c0-0x3df */
2060 		regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi =
2061 		    (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc);
2062 		regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x3c0;
2063 		regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0x20;
2064 		nreg++, nasgn++;
2065 
2066 		/* Video memory */
2067 		regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi =
2068 		    (PCI_RELOCAT_B | PCI_ADDR_MEM32 | devloc);
2069 		regs[nreg].pci_phys_low =
2070 		    assigned[nasgn].pci_phys_low = 0xa0000;
2071 		regs[nreg].pci_size_low =
2072 		    assigned[nasgn].pci_size_low = 0x20000;
2073 		nreg++, nasgn++;
2074 	}
2075 
2076 	/* add the hard-decode, aliased address spaces for 8514 */
2077 	if ((baseclass == PCI_CLASS_DISPLAY) &&
2078 	    (subclass == PCI_DISPLAY_VGA) &&
2079 	    (progclass & PCI_DISPLAY_IF_8514)) {
2080 
2081 		/* hard decode 0x2e8 */
2082 		regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi =
2083 		    (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc);
2084 		regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x2e8;
2085 		regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0x1;
2086 		nreg++, nasgn++;
2087 
2088 		/* hard decode 0x2ea-0x2ef */
2089 		regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi =
2090 		    (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc);
2091 		regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x2ea;
2092 		regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0x6;
2093 		nreg++, nasgn++;
2094 	}
2095 
2096 done:
2097 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "reg",
2098 	    (int *)regs, nreg * sizeof (pci_regspec_t) / sizeof (int));
2099 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
2100 	    "assigned-addresses",
2101 	    (int *)assigned, nasgn * sizeof (pci_regspec_t) / sizeof (int));
2102 	if ((config_op == CONFIG_NEW) && enable) {
2103 		cmn_err(CE_NOTE,
2104 		    "!reprogram PCI device [%x/%x/%x](%s): command = %x.\n",
2105 		    bus, dev, func, ddi_driver_name(dip), enable);
2106 		cmd_reg = pci_getw(bus, dev, func, PCI_CONF_COMM);
2107 		cmd_reg |= (enable | PCI_COMM_ME);
2108 		pci_putw(bus, dev, func, PCI_CONF_COMM, cmd_reg);
2109 	}
2110 	return (reprogram);
2111 }
2112 
2113 static void
2114 add_ppb_props(dev_info_t *dip, uchar_t bus, uchar_t dev, uchar_t func,
2115     int pciex)
2116 {
2117 	char *dev_type;
2118 	int i;
2119 	uint_t val, io_range[2], mem_range[2], pmem_range[2];
2120 	uchar_t secbus = pci_getb(bus, dev, func, PCI_BCNF_SECBUS);
2121 	uchar_t subbus = pci_getb(bus, dev, func, PCI_BCNF_SUBBUS);
2122 	uchar_t progclass;
2123 
2124 	ASSERT(secbus <= subbus);
2125 
2126 	/*
2127 	 * Check if it's a subtractive PPB.
2128 	 */
2129 	progclass = pci_getb(bus, dev, func, PCI_CONF_PROGCLASS);
2130 	if (progclass == PCI_BRIDGE_PCI_IF_SUBDECODE)
2131 		pci_bus_res[secbus].subtractive = B_TRUE;
2132 
2133 	/*
2134 	 * Some BIOSes lie about max pci busses, we allow for
2135 	 * such mistakes here
2136 	 */
2137 	if (subbus > pci_bios_nbus) {
2138 		pci_bios_nbus = subbus;
2139 		alloc_res_array();
2140 	}
2141 
2142 	ASSERT(pci_bus_res[secbus].dip == NULL);
2143 	pci_bus_res[secbus].dip = dip;
2144 	pci_bus_res[secbus].par_bus = bus;
2145 
2146 	dev_type = pciex ? "pciex" : "pci";
2147 
2148 	/* setup bus number hierarchy */
2149 	pci_bus_res[secbus].sub_bus = subbus;
2150 	/*
2151 	 * Keep track of the largest subordinate bus number (this is essential
2152 	 * for peer busses because there is no other way of determining its
2153 	 * subordinate bus number).
2154 	 */
2155 	if (subbus > pci_bus_res[bus].sub_bus)
2156 		pci_bus_res[bus].sub_bus = subbus;
2157 	/*
2158 	 * Loop through subordinate busses, initializing their parent bus
2159 	 * field to this bridge's parent.  The subordinate busses' parent
2160 	 * fields may very well be further refined later, as child bridges
2161 	 * are enumerated.  (The value is to note that the subordinate busses
2162 	 * are not peer busses by changing their par_bus fields to anything
2163 	 * other than -1.)
2164 	 */
2165 	for (i = secbus + 1; i <= subbus; i++)
2166 		pci_bus_res[i].par_bus = bus;
2167 
2168 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
2169 	    "device_type", dev_type);
2170 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
2171 	    "#address-cells", 3);
2172 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
2173 	    "#size-cells", 2);
2174 
2175 	/*
2176 	 * According to PPB spec, the base register should be programmed
2177 	 * with a value bigger than the limit register when there are
2178 	 * no resources available. This applies to io, memory, and
2179 	 * prefetchable memory.
2180 	 */
2181 
2182 	/*
2183 	 * io range
2184 	 * We determine i/o windows that are left unconfigured by BIOS
2185 	 * through its i/o enable bit as Microsoft recommends OEMs to do.
2186 	 * If it is unset, we disable i/o and mark it for reconfiguration in
2187 	 * later passes by setting the base > limit
2188 	 */
2189 	val = (uint_t)pci_getw(bus, dev, func, PCI_CONF_COMM);
2190 	if (val & PCI_COMM_IO) {
2191 		val = (uint_t)pci_getb(bus, dev, func, PCI_BCNF_IO_BASE_LOW);
2192 		io_range[0] = ((val & 0xf0) << 8);
2193 		val = (uint_t)pci_getb(bus, dev, func, PCI_BCNF_IO_LIMIT_LOW);
2194 		io_range[1]  = ((val & 0xf0) << 8) | 0xFFF;
2195 	} else {
2196 		io_range[0] = 0x9fff;
2197 		io_range[1] = 0x1000;
2198 		pci_putb(bus, dev, func, PCI_BCNF_IO_BASE_LOW,
2199 		    (uint8_t)((io_range[0] >> 8) & 0xf0));
2200 		pci_putb(bus, dev, func, PCI_BCNF_IO_LIMIT_LOW,
2201 		    (uint8_t)((io_range[1] >> 8) & 0xf0));
2202 		pci_putw(bus, dev, func, PCI_BCNF_IO_BASE_HI, 0);
2203 		pci_putw(bus, dev, func, PCI_BCNF_IO_LIMIT_HI, 0);
2204 	}
2205 
2206 	if (io_range[0] != 0 && io_range[0] < io_range[1]) {
2207 		memlist_insert(&pci_bus_res[secbus].io_ports,
2208 		    (uint64_t)io_range[0],
2209 		    (uint64_t)(io_range[1] - io_range[0] + 1));
2210 		memlist_insert(&pci_bus_res[bus].io_ports_used,
2211 		    (uint64_t)io_range[0],
2212 		    (uint64_t)(io_range[1] - io_range[0] + 1));
2213 		if (pci_bus_res[bus].io_ports != NULL) {
2214 			(void) memlist_remove(&pci_bus_res[bus].io_ports,
2215 			    (uint64_t)io_range[0],
2216 			    (uint64_t)(io_range[1] - io_range[0] + 1));
2217 		}
2218 		dcmn_err(CE_NOTE, "bus %d io-range: 0x%x-%x",
2219 		    secbus, io_range[0], io_range[1]);
2220 		/* if 32-bit supported, make sure upper bits are not set */
2221 		if ((val & 0xf) == 1 &&
2222 		    pci_getw(bus, dev, func, PCI_BCNF_IO_BASE_HI)) {
2223 			cmn_err(CE_NOTE, "unsupported 32-bit IO address on"
2224 			    " pci-pci bridge [%d/%d/%d]", bus, dev, func);
2225 		}
2226 	}
2227 
2228 	/* mem range */
2229 	val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_MEM_BASE);
2230 	mem_range[0] = ((val & 0xFFF0) << 16);
2231 	val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_MEM_LIMIT);
2232 	mem_range[1] = ((val & 0xFFF0) << 16) | 0xFFFFF;
2233 	if (mem_range[0] != 0 && mem_range[0] < mem_range[1]) {
2234 		memlist_insert(&pci_bus_res[secbus].mem_space,
2235 		    (uint64_t)mem_range[0],
2236 		    (uint64_t)(mem_range[1] - mem_range[0] + 1));
2237 		memlist_insert(&pci_bus_res[bus].mem_space_used,
2238 		    (uint64_t)mem_range[0],
2239 		    (uint64_t)(mem_range[1] - mem_range[0] + 1));
2240 		/* remove from parent resouce list */
2241 		if (pci_bus_res[bus].mem_space != NULL) {
2242 			(void) memlist_remove(&pci_bus_res[bus].mem_space,
2243 			    (uint64_t)mem_range[0],
2244 			    (uint64_t)(mem_range[1] - mem_range[0] + 1));
2245 		}
2246 		dcmn_err(CE_NOTE, "bus %d mem-range: 0x%x-%x",
2247 		    secbus, mem_range[0], mem_range[1]);
2248 	}
2249 
2250 	/* prefetchable memory range */
2251 	val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_PF_BASE_LOW);
2252 	pmem_range[0] = ((val & 0xFFF0) << 16);
2253 	val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_PF_LIMIT_LOW);
2254 	pmem_range[1] = ((val & 0xFFF0) << 16) | 0xFFFFF;
2255 	if (pmem_range[0] != 0 && pmem_range[0] < pmem_range[1]) {
2256 		memlist_insert(&pci_bus_res[secbus].pmem_space,
2257 		    (uint64_t)pmem_range[0],
2258 		    (uint64_t)(pmem_range[1] - pmem_range[0] + 1));
2259 		memlist_insert(&pci_bus_res[bus].pmem_space_used,
2260 		    (uint64_t)pmem_range[0],
2261 		    (uint64_t)(pmem_range[1] - pmem_range[0] + 1));
2262 		if (pci_bus_res[bus].pmem_space != NULL) {
2263 			(void) memlist_remove(&pci_bus_res[bus].pmem_space,
2264 			    (uint64_t)pmem_range[0],
2265 			    (uint64_t)(pmem_range[1] - pmem_range[0] + 1));
2266 		}
2267 		dcmn_err(CE_NOTE, "bus %d pmem-range: 0x%x-%x",
2268 		    secbus, pmem_range[0], pmem_range[1]);
2269 		/* if 64-bit supported, make sure upper bits are not set */
2270 		if ((val & 0xf) == 1 &&
2271 		    pci_getl(bus, dev, func, PCI_BCNF_PF_BASE_HIGH)) {
2272 			cmn_err(CE_NOTE, "unsupported 64-bit prefetch memory on"
2273 			    " pci-pci bridge [%d/%d/%d]", bus, dev, func);
2274 		}
2275 	}
2276 
2277 	add_bus_range_prop(secbus);
2278 	add_ppb_ranges_prop(secbus);
2279 }
2280 
2281 extern const struct pci_class_strings_s class_pci[];
2282 extern int class_pci_items;
2283 
2284 static void
2285 add_model_prop(dev_info_t *dip, uint_t classcode)
2286 {
2287 	const char *desc;
2288 	int i;
2289 	uchar_t baseclass = classcode >> 16;
2290 	uchar_t subclass = (classcode >> 8) & 0xff;
2291 	uchar_t progclass = classcode & 0xff;
2292 
2293 	if ((baseclass == PCI_CLASS_MASS) && (subclass == PCI_MASS_IDE)) {
2294 		desc = "IDE controller";
2295 	} else {
2296 		for (desc = 0, i = 0; i < class_pci_items; i++) {
2297 			if ((baseclass == class_pci[i].base_class) &&
2298 			    (subclass == class_pci[i].sub_class) &&
2299 			    (progclass == class_pci[i].prog_class)) {
2300 				desc = class_pci[i].actual_desc;
2301 				break;
2302 			}
2303 		}
2304 		if (i == class_pci_items)
2305 			desc = "Unknown class of pci/pnpbios device";
2306 	}
2307 
2308 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, "model",
2309 	    (char *)desc);
2310 }
2311 
2312 static void
2313 add_bus_range_prop(int bus)
2314 {
2315 	int bus_range[2];
2316 
2317 	if (pci_bus_res[bus].dip == NULL)
2318 		return;
2319 	bus_range[0] = bus;
2320 	bus_range[1] = pci_bus_res[bus].sub_bus;
2321 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, pci_bus_res[bus].dip,
2322 	    "bus-range", (int *)bus_range, 2);
2323 }
2324 
2325 /*
2326  * Add slot-names property for any named pci hot-plug slots
2327  */
2328 static void
2329 add_bus_slot_names_prop(int bus)
2330 {
2331 	char slotprop[256];
2332 	int len;
2333 
2334 	if (pci_bus_res[bus].dip != NULL) {
2335 		/* simply return if the property is already defined */
2336 		if (ddi_prop_exists(DDI_DEV_T_ANY, pci_bus_res[bus].dip,
2337 		    DDI_PROP_DONTPASS, "slot-names"))
2338 			return;
2339 	}
2340 
2341 	len = pci_slot_names_prop(bus, slotprop, sizeof (slotprop));
2342 	if (len > 0) {
2343 		/*
2344 		 * Only create a peer bus node if this bus may be a peer bus.
2345 		 * It may be a peer bus if the dip is NULL and if par_bus is
2346 		 * -1 (par_bus is -1 if this bus was not found to be
2347 		 * subordinate to any PCI-PCI bridge).
2348 		 * If it's not a peer bus, then the ACPI BBN-handling code
2349 		 * will remove it later.
2350 		 */
2351 		if (pci_bus_res[bus].par_bus == (uchar_t)-1 &&
2352 		    pci_bus_res[bus].dip == NULL) {
2353 
2354 			create_root_bus_dip(bus);
2355 		}
2356 		if (pci_bus_res[bus].dip != NULL) {
2357 			ASSERT((len % sizeof (int)) == 0);
2358 			(void) ndi_prop_update_int_array(DDI_DEV_T_NONE,
2359 			    pci_bus_res[bus].dip, "slot-names",
2360 			    (int *)slotprop, len / sizeof (int));
2361 		} else {
2362 			cmn_err(CE_NOTE, "!BIOS BUG: Invalid bus number in PCI "
2363 			    "IRQ routing table; Not adding slot-names "
2364 			    "property for incorrect bus %d", bus);
2365 		}
2366 	}
2367 }
2368 
2369 static int
2370 memlist_to_range(ppb_ranges_t *rp, struct memlist *entry, int type)
2371 {
2372 	if (entry == NULL)
2373 		return (0);
2374 
2375 	/* assume 32-bit addresses */
2376 	rp->child_high = rp->parent_high = type;
2377 	rp->child_mid = rp->parent_mid = 0;
2378 	rp->child_low = rp->parent_low = (uint32_t)entry->address;
2379 	rp->size_high = 0;
2380 	rp->size_low = (uint32_t)entry->size;
2381 	return (1);
2382 }
2383 
2384 static void
2385 add_ppb_ranges_prop(int bus)
2386 {
2387 	int i = 0;
2388 	ppb_ranges_t *rp;
2389 
2390 	rp = kmem_alloc(3 * sizeof (*rp), KM_SLEEP);
2391 
2392 	i = memlist_to_range(&rp[0], pci_bus_res[bus].io_ports,
2393 	    PCI_ADDR_IO | PCI_REG_REL_M);
2394 	i += memlist_to_range(&rp[i], pci_bus_res[bus].mem_space,
2395 	    PCI_ADDR_MEM32 | PCI_REG_REL_M);
2396 	i += memlist_to_range(&rp[i], pci_bus_res[bus].pmem_space,
2397 	    PCI_ADDR_MEM32 | PCI_REG_REL_M | PCI_REG_PF_M);
2398 
2399 	if (i != 0)
2400 		(void) ndi_prop_update_int_array(DDI_DEV_T_NONE,
2401 		    pci_bus_res[bus].dip, "ranges", (int *)rp,
2402 		    i * sizeof (ppb_ranges_t) / sizeof (int));
2403 	kmem_free(rp, 3 * sizeof (*rp));
2404 }
2405 
2406 static int
2407 memlist_to_spec(struct pci_phys_spec *sp, struct memlist *list, int type)
2408 {
2409 	int i = 0;
2410 
2411 	while (list) {
2412 		/* assume 32-bit addresses */
2413 		sp->pci_phys_hi = type;
2414 		sp->pci_phys_mid = 0;
2415 		sp->pci_phys_low = (uint32_t)list->address;
2416 		sp->pci_size_hi = 0;
2417 		sp->pci_size_low = (uint32_t)list->size;
2418 
2419 		list = list->next;
2420 		sp++, i++;
2421 	}
2422 	return (i);
2423 }
2424 
2425 static void
2426 add_bus_available_prop(int bus)
2427 {
2428 	int i, count;
2429 	struct pci_phys_spec *sp;
2430 
2431 	count = memlist_count(pci_bus_res[bus].io_ports) +
2432 	    memlist_count(pci_bus_res[bus].mem_space) +
2433 	    memlist_count(pci_bus_res[bus].pmem_space);
2434 
2435 	if (count == 0)		/* nothing available */
2436 		return;
2437 
2438 	sp = kmem_alloc(count * sizeof (*sp), KM_SLEEP);
2439 	i = memlist_to_spec(&sp[0], pci_bus_res[bus].io_ports,
2440 	    PCI_ADDR_IO | PCI_REG_REL_M);
2441 	i += memlist_to_spec(&sp[i], pci_bus_res[bus].mem_space,
2442 	    PCI_ADDR_MEM32 | PCI_REG_REL_M);
2443 	i += memlist_to_spec(&sp[i], pci_bus_res[bus].pmem_space,
2444 	    PCI_ADDR_MEM32 | PCI_REG_REL_M | PCI_REG_PF_M);
2445 	ASSERT(i == count);
2446 
2447 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, pci_bus_res[bus].dip,
2448 	    "available", (int *)sp,
2449 	    i * sizeof (struct pci_phys_spec) / sizeof (int));
2450 	kmem_free(sp, count * sizeof (*sp));
2451 }
2452 
2453 static void
2454 alloc_res_array(void)
2455 {
2456 	static int array_max = 0;
2457 	int old_max;
2458 	void *old_res;
2459 
2460 	if (array_max > pci_bios_nbus + 1)
2461 		return;	/* array is big enough */
2462 
2463 	old_max = array_max;
2464 	old_res = pci_bus_res;
2465 
2466 	if (array_max == 0)
2467 		array_max = 16;	/* start with a reasonable number */
2468 
2469 	while (array_max < pci_bios_nbus + 1)
2470 		array_max <<= 1;
2471 	pci_bus_res = (struct pci_bus_resource *)kmem_zalloc(
2472 	    array_max * sizeof (struct pci_bus_resource), KM_SLEEP);
2473 
2474 	if (old_res) {	/* copy content and free old array */
2475 		bcopy(old_res, pci_bus_res,
2476 		    old_max * sizeof (struct pci_bus_resource));
2477 		kmem_free(old_res, old_max * sizeof (struct pci_bus_resource));
2478 	}
2479 }
2480 
2481 static void
2482 create_ioapic_node(int bus, int dev, int fn, ushort_t vendorid,
2483     ushort_t deviceid)
2484 {
2485 	static dev_info_t *ioapicsnode = NULL;
2486 	static int numioapics = 0;
2487 	dev_info_t *ioapic_node;
2488 	uint64_t physaddr;
2489 	uint32_t lobase, hibase = 0;
2490 
2491 	/* BAR 0 contains the IOAPIC's memory-mapped I/O address */
2492 	lobase = (*pci_getl_func)(bus, dev, fn, PCI_CONF_BASE0);
2493 
2494 	/* We (and the rest of the world) only support memory-mapped IOAPICs */
2495 	if ((lobase & PCI_BASE_SPACE_M) != PCI_BASE_SPACE_MEM)
2496 		return;
2497 
2498 	if ((lobase & PCI_BASE_TYPE_M) == PCI_BASE_TYPE_ALL)
2499 		hibase = (*pci_getl_func)(bus, dev, fn, PCI_CONF_BASE0 + 4);
2500 
2501 	lobase &= PCI_BASE_M_ADDR_M;
2502 
2503 	physaddr = (((uint64_t)hibase) << 32) | lobase;
2504 
2505 	/*
2506 	 * Create a nexus node for all IOAPICs under the root node.
2507 	 */
2508 	if (ioapicsnode == NULL) {
2509 		if (ndi_devi_alloc(ddi_root_node(), IOAPICS_NODE_NAME,
2510 		    (pnode_t)DEVI_SID_NODEID, &ioapicsnode) != NDI_SUCCESS) {
2511 			return;
2512 		}
2513 		(void) ndi_devi_online(ioapicsnode, 0);
2514 	}
2515 
2516 	/*
2517 	 * Create a child node for this IOAPIC
2518 	 */
2519 	ioapic_node = ddi_add_child(ioapicsnode, IOAPICS_CHILD_NAME,
2520 	    DEVI_SID_NODEID, numioapics++);
2521 	if (ioapic_node == NULL) {
2522 		return;
2523 	}
2524 
2525 	/* Vendor and Device ID */
2526 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, ioapic_node,
2527 	    IOAPICS_PROP_VENID, vendorid);
2528 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, ioapic_node,
2529 	    IOAPICS_PROP_DEVID, deviceid);
2530 
2531 	/* device_type */
2532 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, ioapic_node,
2533 	    "device_type", IOAPICS_DEV_TYPE);
2534 
2535 	/* reg */
2536 	(void) ndi_prop_update_int64(DDI_DEV_T_NONE, ioapic_node,
2537 	    "reg", physaddr);
2538 }
2539 
2540 /*
2541  * NOTE: For PCIe slots, the name is generated from the slot number
2542  * information obtained from Slot Capabilities register.
2543  * For non-PCIe slots, it is generated based on the slot number
2544  * information in the PCI IRQ table.
2545  */
2546 static void
2547 pciex_slot_names_prop(dev_info_t *dip, ushort_t slot_num)
2548 {
2549 	char slotprop[256];
2550 	int len;
2551 
2552 	bzero(slotprop, sizeof (slotprop));
2553 
2554 	/* set mask to 1 as there is only one slot (i.e dev 0) */
2555 	*(uint32_t *)slotprop = 1;
2556 	len = 4;
2557 	(void) snprintf(slotprop + len, sizeof (slotprop) - len, "pcie%d",
2558 	    slot_num);
2559 	len += strlen(slotprop + len) + 1;
2560 	len += len % 4;
2561 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "slot-names",
2562 	    (int *)slotprop, len / sizeof (int));
2563 }
2564