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