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