xref: /titanic_41/usr/src/uts/intel/io/pci/pci_boot.c (revision d65a28a2f5c3e1fc40e2c7384739f05e22018bfe)
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  * used to register ISA resource usage which must not be made
128  * "available" from other PCI node' resource maps
129  */
130 static struct {
131 	struct memlist *io_ports_used;
132 	struct memlist *mem_space_used;
133 } isa_res;
134 
135 /*
136  * Enumerate all PCI devices
137  */
138 void
139 pci_setup_tree()
140 {
141 	uint_t i, root_bus_addr = 0;
142 
143 	alloc_res_array();
144 	for (i = 0; i <= pci_bios_nbus; i++) {
145 		pci_bus_res[i].par_bus = (uchar_t)-1;
146 		pci_bus_res[i].root_addr = (uchar_t)-1;
147 		pci_bus_res[i].sub_bus = i;
148 	}
149 
150 	pci_bus_res[0].root_addr = root_bus_addr++;
151 	create_root_bus_dip(0);
152 	enumerate_bus_devs(0, CONFIG_INFO);
153 
154 	/*
155 	 * Now enumerate peer busses
156 	 *
157 	 * We loop till pci_bios_nbus. On most systems, there is
158 	 * one more bus at the high end, which implements the ISA
159 	 * compatibility bus. We don't care about that.
160 	 *
161 	 * Note: In the old (bootconf) enumeration, the peer bus
162 	 *	address did not use the bus number, and there were
163 	 *	too many peer busses created. The root_bus_addr is
164 	 *	used to maintain the old peer bus address assignment.
165 	 *	However, we stop enumerating phantom peers with no
166 	 *	device below.
167 	 */
168 	for (i = 1; i <= pci_bios_nbus; i++) {
169 		if (pci_bus_res[i].dip == NULL) {
170 			pci_bus_res[i].root_addr = root_bus_addr++;
171 		}
172 		enumerate_bus_devs(i, CONFIG_INFO);
173 
174 		/* add slot-names property for named pci hot-plug slots */
175 		add_bus_slot_names_prop(i);
176 	}
177 
178 }
179 
180 /*
181  * >0 = present, 0 = not present, <0 = error
182  */
183 static int
184 pci_bbn_present(int bus)
185 {
186 	ACPI_HANDLE	hdl;
187 	int	rv;
188 
189 	/* no dip means no _BBN */
190 	if (pci_bus_res[bus].dip == NULL)
191 		return (0);
192 
193 	rv = -1;	/* default return value in case of error below */
194 	if (ACPI_SUCCESS(acpica_get_handle(pci_bus_res[bus].dip, &hdl))) {
195 		switch (AcpiEvaluateObject(hdl, "_BBN", NULL, NULL)) {
196 		case AE_OK:
197 			rv = 1;
198 			break;
199 		case AE_NOT_FOUND:
200 			rv = 0;
201 			break;
202 		default:
203 			break;
204 		}
205 	}
206 
207 	return (rv);
208 }
209 
210 /*
211  * Return non-zero if any PCI bus in the system has an associated
212  * _BBN object, 0 otherwise.
213  */
214 static int
215 pci_roots_have_bbn(void)
216 {
217 	int	i;
218 
219 	/*
220 	 * Scan the PCI busses and look for at least 1 _BBN
221 	 */
222 	for (i = 0; i <= pci_bios_nbus; i++) {
223 		/* skip non-root (peer) PCI busses */
224 		if (pci_bus_res[i].par_bus != (uchar_t)-1)
225 			continue;
226 
227 		if (pci_bbn_present(i) > 0)
228 			return (1);
229 	}
230 	return (0);
231 
232 }
233 
234 /*
235  * return non-zero if the machine is one on which we renumber
236  * the internal pci unit-addresses
237  */
238 static int
239 pci_bus_renumber()
240 {
241 	ACPI_TABLE_HEADER *fadt;
242 
243 	if (pci_bus_always_renumber)
244 		return (1);
245 
246 	/* get the FADT */
247 	if (AcpiGetTable(ACPI_SIG_FADT, 1, (ACPI_TABLE_HEADER **)&fadt) !=
248 	    AE_OK)
249 		return (0);
250 
251 	/* compare OEM Table ID to "SUNm31" */
252 	if (strncmp("SUNm31", fadt->OemId, 6))
253 		return (0);
254 	else
255 		return (1);
256 }
257 
258 /*
259  * Initial enumeration of the physical PCI bus hierarchy can
260  * leave 'gaps' in the order of peer PCI bus unit-addresses.
261  * Systems with more than one peer PCI bus *must* have an ACPI
262  * _BBN object associated with each peer bus; use the presence
263  * of this object to remove gaps in the numbering of the peer
264  * PCI bus unit-addresses - only peer busses with an associated
265  * _BBN are counted.
266  */
267 static void
268 pci_renumber_root_busses(void)
269 {
270 	int pci_regs[] = {0, 0, 0};
271 	int	i, root_addr = 0;
272 
273 	/*
274 	 * Currently, we only enable the re-numbering on specific
275 	 * Sun machines; this is a work-around for the more complicated
276 	 * issue of upgrade changing physical device paths
277 	 */
278 	if (!pci_bus_renumber())
279 		return;
280 
281 	/*
282 	 * If we find no _BBN objects at all, we either don't need
283 	 * to do anything or can't do anything anyway
284 	 */
285 	if (!pci_roots_have_bbn())
286 		return;
287 
288 	for (i = 0; i <= pci_bios_nbus; i++) {
289 		/* skip non-root (peer) PCI busses */
290 		if (pci_bus_res[i].par_bus != (uchar_t)-1)
291 			continue;
292 
293 		if (pci_bbn_present(i) < 1) {
294 			pci_bus_res[i].root_addr = (uchar_t)-1;
295 			continue;
296 		}
297 
298 		ASSERT(pci_bus_res[i].dip != NULL);
299 		if (pci_bus_res[i].root_addr != root_addr) {
300 			/* update reg property for node */
301 			pci_bus_res[i].root_addr = root_addr;
302 			pci_regs[0] = pci_bus_res[i].root_addr;
303 			(void) ndi_prop_update_int_array(DDI_DEV_T_NONE,
304 			    pci_bus_res[i].dip, "reg", (int *)pci_regs, 3);
305 		}
306 		root_addr++;
307 	}
308 }
309 
310 void
311 pci_register_isa_resources(int type, uint32_t base, uint32_t size)
312 {
313 	(void) memlist_insert(
314 	    (type == 1) ?  &isa_res.io_ports_used : &isa_res.mem_space_used,
315 	    base, size);
316 }
317 
318 /*
319  * Remove the resources which are already used by devices under a subtractive
320  * bridge from the bus's resources lists, because they're not available, and
321  * shouldn't be allocated to other buses.  This is necessary because tracking
322  * resources for subtractive bridges is not complete.  (Subtractive bridges only
323  * track some of their claimed resources, not "the rest of the address space" as
324  * they should, so that allocation to peer non-subtractive PPBs is easier.  We
325  * need a fully-capable global resource allocator).
326  */
327 static void
328 remove_subtractive_res()
329 {
330 	int i, j;
331 	struct memlist *list;
332 
333 	for (i = 0; i <= pci_bios_nbus; i++) {
334 		if (pci_bus_res[i].subtractive) {
335 			/* remove used io ports */
336 			list = pci_bus_res[i].io_ports_used;
337 			while (list) {
338 				for (j = 0; j <= pci_bios_nbus; j++)
339 					(void) memlist_remove(
340 					    &pci_bus_res[j].io_ports,
341 					    list->address, list->size);
342 				list = list->next;
343 			}
344 			/* remove used mem resource */
345 			list = pci_bus_res[i].mem_space_used;
346 			while (list) {
347 				for (j = 0; j <= pci_bios_nbus; j++) {
348 					(void) memlist_remove(
349 					    &pci_bus_res[j].mem_space,
350 					    list->address, list->size);
351 					(void) memlist_remove(
352 					    &pci_bus_res[j].pmem_space,
353 					    list->address, list->size);
354 				}
355 				list = list->next;
356 			}
357 			/* remove used prefetchable mem resource */
358 			list = pci_bus_res[i].pmem_space_used;
359 			while (list) {
360 				for (j = 0; j <= pci_bios_nbus; j++) {
361 					(void) memlist_remove(
362 					    &pci_bus_res[j].pmem_space,
363 					    list->address, list->size);
364 					(void) memlist_remove(
365 					    &pci_bus_res[j].mem_space,
366 					    list->address, list->size);
367 				}
368 				list = list->next;
369 			}
370 		}
371 	}
372 }
373 
374 /*
375  * Set-up (or complete the set-up) of the bus_space resource list
376  */
377 static void
378 setup_bus_res(int bus)
379 {
380 	uchar_t par_bus;
381 
382 	if (pci_bus_res[bus].dip == NULL)	/* unused bus */
383 		return;
384 
385 	/*
386 	 * Setup bus_space if not already filled-in by populate_bus_res();
387 	 */
388 	if (pci_bus_res[bus].bus_space == NULL) {
389 		ASSERT(pci_bus_res[bus].sub_bus >= bus);
390 		memlist_insert(&pci_bus_res[bus].bus_space, bus,
391 		    pci_bus_res[bus].sub_bus - bus + 1);
392 	}
393 
394 	ASSERT(pci_bus_res[bus].bus_space != NULL);
395 
396 	/*
397 	 * Remove resources from parent bus node if this is not a
398 	 * root bus.
399 	 */
400 	par_bus = pci_bus_res[bus].par_bus;
401 	if (par_bus != (uchar_t)-1) {
402 		ASSERT(pci_bus_res[par_bus].bus_space != NULL);
403 		memlist_remove_list(&pci_bus_res[par_bus].bus_space,
404 		    pci_bus_res[bus].bus_space);
405 	}
406 
407 	/* remove self from bus_space */;
408 	(void) memlist_remove(&pci_bus_res[bus].bus_space, bus, 1);
409 }
410 
411 static uint64_t
412 get_parbus_io_res(uchar_t parbus, uchar_t bus, uint64_t size, uint64_t align)
413 {
414 	uint64_t addr = 0;
415 	uchar_t res_bus;
416 
417 	/*
418 	 * Skip root(peer) buses in multiple-root-bus systems when
419 	 * ACPI resource discovery was not successfully done.
420 	 */
421 	if ((pci_bus_res[parbus].par_bus == (uchar_t)-1) &&
422 	    (num_root_bus > 1) && (acpi_resource_discovery <= 0))
423 		return (0);
424 
425 	res_bus = parbus;
426 	while (pci_bus_res[res_bus].subtractive) {
427 		if (pci_bus_res[res_bus].io_ports)
428 			break;
429 		res_bus = pci_bus_res[res_bus].par_bus;
430 		if (res_bus == (uchar_t)-1)
431 			break; /* root bus already */
432 	}
433 
434 	if (pci_bus_res[res_bus].io_ports) {
435 		addr = memlist_find(&pci_bus_res[res_bus].io_ports,
436 		    size, align);
437 		if (addr) {
438 			memlist_insert(&pci_bus_res[res_bus].io_ports_used,
439 			    addr, size);
440 
441 			/* free the old resource */
442 			memlist_free_all(&pci_bus_res[bus].io_ports);
443 			memlist_free_all(&pci_bus_res[bus].io_ports_used);
444 
445 			/* add the new resource */
446 			memlist_insert(&pci_bus_res[bus].io_ports, addr, size);
447 		}
448 	}
449 
450 	return (addr);
451 }
452 
453 static uint64_t
454 get_parbus_mem_res(uchar_t parbus, uchar_t bus, uint64_t size, uint64_t align)
455 {
456 	uint64_t addr = 0;
457 	uchar_t res_bus;
458 
459 	/*
460 	 * Skip root(peer) buses in multiple-root-bus systems when
461 	 * ACPI resource discovery was not successfully done.
462 	 */
463 	if ((pci_bus_res[parbus].par_bus == (uchar_t)-1) &&
464 	    (num_root_bus > 1) && (acpi_resource_discovery <= 0))
465 		return (0);
466 
467 	res_bus = parbus;
468 	while (pci_bus_res[res_bus].subtractive) {
469 		if (pci_bus_res[res_bus].mem_space)
470 			break;
471 		res_bus = pci_bus_res[res_bus].par_bus;
472 		if (res_bus == (uchar_t)-1)
473 			break; /* root bus already */
474 	}
475 
476 	if (pci_bus_res[res_bus].mem_space) {
477 		addr = memlist_find(&pci_bus_res[res_bus].mem_space,
478 		    size, align);
479 		if (addr) {
480 			memlist_insert(&pci_bus_res[res_bus].mem_space_used,
481 			    addr, size);
482 			(void) memlist_remove(&pci_bus_res[res_bus].pmem_space,
483 			    addr, size);
484 
485 			/* free the old resource */
486 			memlist_free_all(&pci_bus_res[bus].mem_space);
487 			memlist_free_all(&pci_bus_res[bus].mem_space_used);
488 
489 			/* add the new resource */
490 			memlist_insert(&pci_bus_res[bus].mem_space, addr, size);
491 		}
492 	}
493 
494 	return (addr);
495 }
496 
497 /*
498  * given a cap_id, return its cap_id location in config space
499  */
500 static int
501 get_pci_cap(uchar_t bus, uchar_t dev, uchar_t func, uint8_t cap_id)
502 {
503 	uint8_t curcap, cap_id_loc;
504 	uint16_t status;
505 	int location = -1;
506 
507 	/*
508 	 * Need to check the Status register for ECP support first.
509 	 * Also please note that for type 1 devices, the
510 	 * offset could change. Should support type 1 next.
511 	 */
512 	status = pci_getw(bus, dev, func, PCI_CONF_STAT);
513 	if (!(status & PCI_STAT_CAP)) {
514 		return (-1);
515 	}
516 	cap_id_loc = pci_getb(bus, dev, func, PCI_CONF_CAP_PTR);
517 
518 	/* Walk the list of capabilities */
519 	while (cap_id_loc && cap_id_loc != (uint8_t)-1) {
520 		curcap = pci_getb(bus, dev, func, cap_id_loc);
521 
522 		if (curcap == cap_id) {
523 			location = cap_id_loc;
524 			break;
525 		}
526 		cap_id_loc = pci_getb(bus, dev, func, cap_id_loc + 1);
527 	}
528 	return (location);
529 }
530 
531 /*
532  * Assign valid resources to unconfigured pci(e) bridges. We are trying
533  * to reprogram the bridge when its
534  * 		i)   SECBUS == SUBBUS	||
535  * 		ii)  IOBASE > IOLIM	||
536  * 		iii) MEMBASE > MEMLIM
537  * This must be done after one full pass through the PCI tree to collect
538  * all BIOS-configured resources, so that we know what resources are
539  * free and available to assign to the unconfigured PPBs.
540  */
541 static void
542 fix_ppb_res(uchar_t secbus, boolean_t prog_sub)
543 {
544 	uchar_t bus, dev, func;
545 	uchar_t parbus, subbus;
546 	uint_t io_base, io_limit, mem_base, mem_limit;
547 	uint_t io_size, mem_size;
548 	uint64_t addr = 0;
549 	int *regp = NULL;
550 	uint_t reglen;
551 	int rv, cap_ptr, physhi;
552 	dev_info_t *dip;
553 	uint16_t cmd_reg;
554 	struct memlist *list;
555 
556 	/* skip root (peer) PCI busses */
557 	if (pci_bus_res[secbus].par_bus == (uchar_t)-1)
558 		return;
559 
560 	/* skip subtractive PPB when prog_sub is not TRUE */
561 	if (pci_bus_res[secbus].subtractive && !prog_sub)
562 		return;
563 
564 	/* some entries may be empty due to discontiguous bus numbering */
565 	dip = pci_bus_res[secbus].dip;
566 	if (dip == NULL)
567 		return;
568 
569 	rv = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
570 	    "reg", &regp, &reglen);
571 	ASSERT(rv == DDI_PROP_SUCCESS && reglen > 0);
572 	physhi = regp[0];
573 	ddi_prop_free(regp);
574 
575 	func = (uchar_t)PCI_REG_FUNC_G(physhi);
576 	dev = (uchar_t)PCI_REG_DEV_G(physhi);
577 	bus = (uchar_t)PCI_REG_BUS_G(physhi);
578 
579 	/*
580 	 * If pcie bridge, check to see if link is enabled
581 	 */
582 	cap_ptr = get_pci_cap(bus, dev, func, PCI_CAP_ID_PCI_E);
583 	if (cap_ptr != -1) {
584 		cmd_reg = pci_getw(bus, dev, func,
585 		    (uint16_t)cap_ptr + PCIE_LINKCTL);
586 		if (cmd_reg & PCIE_LINKCTL_LINK_DISABLE) {
587 			dcmn_err(CE_NOTE,
588 			    "!fix_ppb_res: ppb[%x/%x/%x] link is disabled.\n",
589 			    bus, dev, func);
590 			return;
591 		}
592 	}
593 
594 	subbus = pci_getb(bus, dev, func, PCI_BCNF_SUBBUS);
595 	parbus = pci_bus_res[secbus].par_bus;
596 	ASSERT(parbus == bus);
597 	cmd_reg = pci_getw(bus, dev, func, PCI_CONF_COMM);
598 
599 	/*
600 	 * If we have a Cardbus bridge, but no bus space
601 	 */
602 	if (pci_bus_res[secbus].num_cbb != 0 &&
603 	    pci_bus_res[secbus].bus_space == NULL) {
604 		uchar_t range;
605 
606 		/* normally there are 2 buses under a cardbus bridge */
607 		range = pci_bus_res[secbus].num_cbb * 2;
608 
609 		/*
610 		 * Try to find and allocate a bus-range starting at subbus+1
611 		 * from the parent of the PPB.
612 		 */
613 		for (; range != 0; range--) {
614 			if (memlist_find_with_startaddr(
615 			    &pci_bus_res[parbus].bus_space,
616 			    subbus + 1, range, 1) != NULL)
617 				break; /* find bus range resource at parent */
618 		}
619 		if (range != 0) {
620 			memlist_insert(&pci_bus_res[secbus].bus_space,
621 			    subbus + 1, range);
622 			subbus = subbus + range;
623 			pci_bus_res[secbus].sub_bus = subbus;
624 			pci_putb(bus, dev, func, PCI_BCNF_SUBBUS, subbus);
625 			add_bus_range_prop(secbus);
626 
627 			cmn_err(CE_NOTE, "!reprogram bus-range on ppb"
628 			    "[%x/%x/%x]: %x ~ %x\n", bus, dev, func,
629 			    secbus, subbus);
630 		}
631 	}
632 
633 	/*
634 	 * Calculate required IO size
635 	 * We are going to assign 512 bytes per bus. The size needs to be
636 	 * 4K aligned and the maximum size is 16K.
637 	 */
638 	io_size = (subbus - secbus + 1) * 0x200;
639 	io_size = (io_size + PPB_IO_ALIGNMENT) & (~(PPB_IO_ALIGNMENT - 1));
640 	if (io_size > 0x4 * PPB_IO_ALIGNMENT)
641 		io_size = 0x4 * PPB_IO_ALIGNMENT;
642 	/*
643 	 * Calculate required MEM size
644 	 * We are going to assign 1M bytes per bus. The size needs to be
645 	 * 1M aligned and the maximum size is 8M.
646 	 */
647 	mem_size = (subbus - secbus + 1) * PPB_MEM_ALIGNMENT;
648 	if (mem_size > 0x8 * PPB_MEM_ALIGNMENT)
649 		mem_size = 0x8 * PPB_MEM_ALIGNMENT;
650 
651 	/* Subtractive bridge */
652 	if (pci_bus_res[secbus].subtractive && prog_sub) {
653 		/*
654 		 * We program an arbitrary amount of I/O and memory resource
655 		 * for the subtractive bridge so that child dynamic-resource-
656 		 * allocating devices (such as Cardbus bridges) have a chance
657 		 * of success.  Until we have full-tree resource rebalancing,
658 		 * dynamic resource allocation (thru busra) only looks at the
659 		 * parent bridge, so all PPBs must have some allocatable
660 		 * resource.  For non-subtractive bridges, the resources come
661 		 * from the base/limit register "windows", but subtractive
662 		 * bridges often don't program those (since they don't need to).
663 		 * If we put all the remaining resources on the subtractive
664 		 * bridge, then peer non-subtractive bridges can't allocate
665 		 * more space (even though this is probably most correct).
666 		 * If we put the resources only on the parent, then allocations
667 		 * from children of subtractive bridges will fail without
668 		 * special-case code for bypassing the subtractive bridge.
669 		 * This solution is the middle-ground temporary solution until
670 		 * we have fully-capable resource allocation.
671 		 */
672 
673 		/*
674 		 * Add an arbitrary I/O resource to the subtractive PPB
675 		 */
676 		if (pci_bus_res[secbus].io_ports == NULL) {
677 			addr = get_parbus_io_res(parbus, secbus, io_size,
678 			    PPB_IO_ALIGNMENT);
679 			if (addr) {
680 				add_ranges_prop(secbus, 1);
681 				pci_bus_res[secbus].io_reprogram =
682 				    pci_bus_res[parbus].io_reprogram;
683 
684 				cmn_err(CE_NOTE, "!add io-range on subtractive"
685 				    " ppb[%x/%x/%x]: 0x%x ~ 0x%x\n",
686 				    bus, dev, func, (uint32_t)addr,
687 				    (uint32_t)addr + io_size - 1);
688 			}
689 		}
690 		/*
691 		 * Add an arbitrary memory resource to the subtractive PPB
692 		 */
693 		if (pci_bus_res[secbus].mem_space == NULL) {
694 			addr = get_parbus_mem_res(parbus, secbus, mem_size,
695 			    PPB_MEM_ALIGNMENT);
696 			if (addr) {
697 				add_ranges_prop(secbus, 1);
698 				pci_bus_res[secbus].mem_reprogram =
699 				    pci_bus_res[parbus].mem_reprogram;
700 
701 				cmn_err(CE_NOTE, "!add mem-range on "
702 				    "subtractive ppb[%x/%x/%x]: 0x%x ~ 0x%x\n",
703 				    bus, dev, func, (uint32_t)addr,
704 				    (uint32_t)addr + mem_size - 1);
705 			}
706 		}
707 
708 		goto cmd_enable;
709 	}
710 
711 	/*
712 	 * Check to see if we need to reprogram I/O space, either because the
713 	 * parent bus needed reprogramming and so do we, or because I/O space is
714 	 * disabled in base/limit or command register.
715 	 */
716 	io_base = pci_getb(bus, dev, func, PCI_BCNF_IO_BASE_LOW);
717 	io_limit = pci_getb(bus, dev, func, PCI_BCNF_IO_LIMIT_LOW);
718 	io_base = (io_base & 0xf0) << 8;
719 	io_limit = ((io_limit & 0xf0) << 8) | 0xfff;
720 
721 	if (pci_bus_res[parbus].io_reprogram || (io_base > io_limit) ||
722 	    (!(cmd_reg & PCI_COMM_IO))) {
723 		if (pci_bus_res[secbus].io_ports_used) {
724 			memlist_merge(&pci_bus_res[secbus].io_ports_used,
725 			    &pci_bus_res[secbus].io_ports);
726 		}
727 		if (pci_bus_res[secbus].io_ports &&
728 		    (!pci_bus_res[parbus].io_reprogram) &&
729 		    (!pci_bus_res[parbus].subtractive)) {
730 			/* rechoose old io ports info */
731 			list = pci_bus_res[secbus].io_ports;
732 			io_base = (uint_t)list->address;
733 			/* 4K aligned */
734 			io_base = io_base & (~(PPB_IO_ALIGNMENT - 1));
735 			io_limit = (uint_t)(list->address + list->size);
736 			while (list->next) {
737 				list = list->next;
738 				if ((list->address + list->size) > io_limit)
739 					io_limit = (uint_t)
740 					    (list->address + list->size);
741 			}
742 			io_limit = io_limit - 1;
743 			/* 4K aligned */
744 			io_limit = (io_limit + PPB_IO_ALIGNMENT) &
745 			    (~(PPB_IO_ALIGNMENT - 1));
746 			io_size = io_limit - io_base;
747 			io_limit = io_limit - 1;
748 			ASSERT(io_base <= io_limit);
749 			memlist_free_all(&pci_bus_res[secbus].io_ports);
750 			memlist_insert(&pci_bus_res[secbus].io_ports,
751 			    io_base, io_size);
752 			memlist_insert(&pci_bus_res[parbus].io_ports_used,
753 			    io_base, io_size);
754 			(void) memlist_remove(&pci_bus_res[parbus].io_ports,
755 			    io_base, io_size);
756 			pci_bus_res[secbus].io_reprogram = B_TRUE;
757 		} else {
758 			/* get new io ports from parent bus */
759 			addr = get_parbus_io_res(parbus, secbus, io_size,
760 			    PPB_IO_ALIGNMENT);
761 			if (addr) {
762 				io_base = addr;
763 				io_limit = addr + io_size - 1;
764 				pci_bus_res[secbus].io_reprogram = B_TRUE;
765 			}
766 		}
767 		if (pci_bus_res[secbus].io_reprogram) {
768 			/* reprogram PPB regs */
769 			pci_putb(bus, dev, func, PCI_BCNF_IO_BASE_LOW,
770 			    (uchar_t)((io_base>>8) & 0xf0));
771 			pci_putb(bus, dev, func, PCI_BCNF_IO_LIMIT_LOW,
772 			    (uchar_t)((io_limit>>8) & 0xf0));
773 			pci_putb(bus, dev, func, PCI_BCNF_IO_BASE_HI, 0);
774 			pci_putb(bus, dev, func, PCI_BCNF_IO_LIMIT_HI, 0);
775 			add_ranges_prop(secbus, 1);
776 
777 			cmn_err(CE_NOTE, "!reprogram io-range on"
778 			    " ppb[%x/%x/%x]: 0x%x ~ 0x%x\n",
779 			    bus, dev, func, io_base, io_limit);
780 		}
781 	}
782 
783 	/*
784 	 * Check memory space as we did I/O space.
785 	 */
786 	mem_base = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_MEM_BASE);
787 	mem_base = (mem_base & 0xfff0) << 16;
788 	mem_limit = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_MEM_LIMIT);
789 	mem_limit = ((mem_limit & 0xfff0) << 16) | 0xfffff;
790 
791 	if (pci_bus_res[parbus].mem_reprogram || (mem_base > mem_limit) ||
792 	    (!(cmd_reg & PCI_COMM_MAE))) {
793 		if (pci_bus_res[secbus].mem_space_used) {
794 			memlist_merge(&pci_bus_res[secbus].mem_space_used,
795 			    &pci_bus_res[secbus].mem_space);
796 		}
797 		if (pci_bus_res[secbus].mem_space &&
798 		    (!pci_bus_res[parbus].mem_reprogram) &&
799 		    (!pci_bus_res[parbus].subtractive)) {
800 			/* rechoose old mem resource */
801 			list = pci_bus_res[secbus].mem_space;
802 			mem_base = (uint_t)list->address;
803 			/* 1M aligned */
804 			mem_base = mem_base & (~0xfffff);
805 			mem_limit = (uint_t)(list->address + list->size);
806 			while (list->next) {
807 				list = list->next;
808 				if ((list->address + list->size) > mem_limit)
809 					mem_limit = (uint_t)
810 					    (list->address + list->size);
811 			}
812 			mem_limit = mem_limit - 1;
813 			/* 1M aligned */
814 			mem_limit = (mem_limit + PPB_MEM_ALIGNMENT) &
815 			    (~(PPB_MEM_ALIGNMENT - 1));
816 			mem_size = mem_limit - mem_base;
817 			mem_limit = mem_limit - 1;
818 			ASSERT(mem_base <= mem_limit);
819 			memlist_free_all(&pci_bus_res[secbus].mem_space);
820 			memlist_insert(&pci_bus_res[secbus].mem_space,
821 			    mem_base, mem_size);
822 			memlist_insert(&pci_bus_res[parbus].mem_space_used,
823 			    mem_base, mem_size);
824 			(void) memlist_remove(&pci_bus_res[parbus].mem_space,
825 			    mem_base, mem_size);
826 			pci_bus_res[secbus].mem_reprogram = B_TRUE;
827 		} else {
828 			/* get new mem resource from parent bus */
829 			addr = get_parbus_mem_res(parbus, secbus, mem_size,
830 			    PPB_MEM_ALIGNMENT);
831 			if (addr) {
832 				mem_base = addr;
833 				mem_limit = addr + mem_size - 1;
834 				pci_bus_res[secbus].mem_reprogram = B_TRUE;
835 			}
836 		}
837 
838 		if (pci_bus_res[secbus].mem_reprogram) {
839 			/* reprogram PPB MEM regs */
840 			pci_putw(bus, dev, func, PCI_BCNF_MEM_BASE,
841 			    (uint16_t)((mem_base>>16) & 0xfff0));
842 			pci_putw(bus, dev, func, PCI_BCNF_MEM_LIMIT,
843 			    (uint16_t)((mem_limit>>16) & 0xfff0));
844 			/*
845 			 * Disable PMEM window by setting base > limit.
846 			 * We currently don't reprogram the PMEM like we've
847 			 * done for I/O and MEM. (Devices that support prefetch
848 			 * can use non-prefetch MEM.) Anyway, if the MEM access
849 			 * bit is initially disabled by BIOS, we disable the
850 			 * PMEM window manually by setting PMEM base > PMEM
851 			 * limit here, in case there are incorrect values in
852 			 * them from BIOS, so that we won't get in trouble once
853 			 * the MEM access bit is enabled at the end of this
854 			 * function.
855 			 */
856 			if (!(cmd_reg & PCI_COMM_MAE)) {
857 				pci_putw(bus, dev, func, PCI_BCNF_PF_BASE_LOW,
858 				    0xfff0);
859 				pci_putw(bus, dev, func, PCI_BCNF_PF_LIMIT_LOW,
860 				    0x0);
861 				pci_putl(bus, dev, func, PCI_BCNF_PF_BASE_HIGH,
862 				    0xffffffff);
863 				pci_putl(bus, dev, func, PCI_BCNF_PF_LIMIT_HIGH,
864 				    0x0);
865 			}
866 
867 			add_ranges_prop(secbus, 1);
868 
869 			cmn_err(CE_NOTE, "!reprogram mem-range on"
870 			    " ppb[%x/%x/%x]: 0x%x ~ 0x%x\n",
871 			    bus, dev, func, mem_base, mem_limit);
872 		}
873 	}
874 
875 cmd_enable:
876 	if (pci_bus_res[secbus].io_ports)
877 		cmd_reg |= PCI_COMM_IO | PCI_COMM_ME;
878 	if (pci_bus_res[secbus].mem_space)
879 		cmd_reg |= PCI_COMM_MAE | PCI_COMM_ME;
880 	pci_putw(bus, dev, func, PCI_CONF_COMM, cmd_reg);
881 }
882 
883 void
884 pci_reprogram(void)
885 {
886 	int i, pci_reconfig = 1;
887 	char *onoff;
888 	int bus;
889 
890 	/*
891 	 * Excise phantom roots if possible
892 	 */
893 	pci_renumber_root_busses();
894 
895 	/*
896 	 * Do root-bus resource discovery
897 	 */
898 	for (bus = 0; bus <= pci_bios_nbus; bus++) {
899 		/* skip non-root (peer) PCI busses */
900 		if (pci_bus_res[bus].par_bus != (uchar_t)-1)
901 			continue;
902 
903 		/*
904 		 * 1. find resources associated with this root bus
905 		 */
906 		populate_bus_res(bus);
907 
908 
909 		/*
910 		 * 2. Remove used PCI and ISA resources from bus resource map
911 		 */
912 
913 		memlist_remove_list(&pci_bus_res[bus].io_ports,
914 		    pci_bus_res[bus].io_ports_used);
915 		memlist_remove_list(&pci_bus_res[bus].mem_space,
916 		    pci_bus_res[bus].mem_space_used);
917 		memlist_remove_list(&pci_bus_res[bus].pmem_space,
918 		    pci_bus_res[bus].pmem_space_used);
919 		memlist_remove_list(&pci_bus_res[bus].mem_space,
920 		    pci_bus_res[bus].pmem_space_used);
921 		memlist_remove_list(&pci_bus_res[bus].pmem_space,
922 		    pci_bus_res[bus].mem_space_used);
923 
924 		memlist_remove_list(&pci_bus_res[bus].io_ports,
925 		    isa_res.io_ports_used);
926 		memlist_remove_list(&pci_bus_res[bus].mem_space,
927 		    isa_res.mem_space_used);
928 	}
929 
930 	memlist_free_all(&isa_res.io_ports_used);
931 	memlist_free_all(&isa_res.mem_space_used);
932 
933 	/* add bus-range property for root/peer bus nodes */
934 	for (i = 0; i <= pci_bios_nbus; i++) {
935 		/* create bus-range property on root/peer buses */
936 		if (pci_bus_res[i].par_bus == (uchar_t)-1)
937 			add_bus_range_prop(i);
938 
939 		/* setup bus range resource on each bus */
940 		setup_bus_res(i);
941 	}
942 
943 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
944 	    DDI_PROP_DONTPASS, "pci-reprog", &onoff) == DDI_SUCCESS) {
945 		if (strcmp(onoff, "off") == 0) {
946 			pci_reconfig = 0;
947 			cmn_err(CE_NOTE, "pci device reprogramming disabled");
948 		}
949 		ddi_prop_free(onoff);
950 	}
951 
952 	remove_subtractive_res();
953 
954 	/* reprogram the non-subtractive PPB */
955 	if (pci_reconfig)
956 		for (i = 0; i <= pci_bios_nbus; i++)
957 			fix_ppb_res(i, B_FALSE);
958 
959 	for (i = 0; i <= pci_bios_nbus; i++) {
960 		/* configure devices not configured by BIOS */
961 		if (pci_reconfig) {
962 			/*
963 			 * Reprogram the subtractive PPB. At this time, all its
964 			 * siblings should have got their resources already.
965 			 */
966 			if (pci_bus_res[i].subtractive)
967 				fix_ppb_res(i, B_TRUE);
968 			enumerate_bus_devs(i, CONFIG_NEW);
969 		}
970 	}
971 
972 	/* All dev programmed, so we can create available prop */
973 	for (i = 0; i <= pci_bios_nbus; i++)
974 		add_bus_available_prop(i);
975 }
976 
977 /*
978  * populate bus resources
979  */
980 static void
981 populate_bus_res(uchar_t bus)
982 {
983 
984 	/* scan BIOS structures */
985 	pci_bus_res[bus].pmem_space = find_bus_res(bus, PREFETCH_TYPE);
986 	pci_bus_res[bus].mem_space = find_bus_res(bus, MEM_TYPE);
987 	pci_bus_res[bus].io_ports = find_bus_res(bus, IO_TYPE);
988 	pci_bus_res[bus].bus_space = find_bus_res(bus, BUSRANGE_TYPE);
989 
990 	/*
991 	 * attempt to initialize sub_bus from the largest range-end
992 	 * in the bus_space list
993 	 */
994 	if (pci_bus_res[bus].bus_space != NULL) {
995 		struct memlist *entry;
996 		int current;
997 
998 		entry = pci_bus_res[bus].bus_space;
999 		while (entry != NULL) {
1000 			current = entry->address + entry->size - 1;
1001 			if (current > pci_bus_res[bus].sub_bus)
1002 				pci_bus_res[bus].sub_bus = current;
1003 			entry = entry->next;
1004 		}
1005 	}
1006 
1007 	if (bus == 0) {
1008 		/*
1009 		 * Special treatment of bus 0:
1010 		 * If no IO/MEM resource from ACPI/MPSPEC/HRT, copy
1011 		 * pcimem from boot and make I/O space the entire range
1012 		 * starting at 0x100.
1013 		 */
1014 		if (pci_bus_res[0].mem_space == NULL)
1015 			pci_bus_res[0].mem_space =
1016 			    memlist_dup(bootops->boot_mem->pcimem);
1017 		/* Exclude 0x00 to 0xff of the I/O space, used by all PCs */
1018 		if (pci_bus_res[0].io_ports == NULL)
1019 			memlist_insert(&pci_bus_res[0].io_ports, 0x100, 0xffff);
1020 	}
1021 
1022 	/*
1023 	 * Create 'ranges' property here before any resources are
1024 	 * removed from the resource lists
1025 	 */
1026 	add_ranges_prop(bus, 0);
1027 }
1028 
1029 
1030 /*
1031  * Create top-level bus dips, i.e. /pci@0,0, /pci@1,0...
1032  */
1033 static void
1034 create_root_bus_dip(uchar_t bus)
1035 {
1036 	int pci_regs[] = {0, 0, 0};
1037 	dev_info_t *dip;
1038 
1039 	ASSERT(pci_bus_res[bus].par_bus == (uchar_t)-1);
1040 
1041 	num_root_bus++;
1042 	ndi_devi_alloc_sleep(ddi_root_node(), "pci",
1043 	    (pnode_t)DEVI_SID_NODEID, &dip);
1044 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1045 	    "#address-cells", 3);
1046 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1047 	    "#size-cells", 2);
1048 	pci_regs[0] = pci_bus_res[bus].root_addr;
1049 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
1050 	    "reg", (int *)pci_regs, 3);
1051 
1052 	/*
1053 	 * If system has PCIe bus, then create different properties
1054 	 */
1055 	if (create_pcie_root_bus(bus, dip) == B_FALSE)
1056 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1057 		    "device_type", "pci");
1058 
1059 	(void) ndi_devi_bind_driver(dip, 0);
1060 	pci_bus_res[bus].dip = dip;
1061 }
1062 
1063 /*
1064  * For any fixed configuration (often compatability) pci devices
1065  * and those with their own expansion rom, create device nodes
1066  * to hold the already configured device details.
1067  */
1068 void
1069 enumerate_bus_devs(uchar_t bus, int config_op)
1070 {
1071 	uchar_t dev, func, nfunc, header;
1072 	ushort_t venid;
1073 	struct pci_devfunc *devlist = NULL, *entry;
1074 
1075 	if (config_op == CONFIG_NEW) {
1076 		dcmn_err(CE_NOTE, "configuring pci bus 0x%x", bus);
1077 	} else if (config_op == CONFIG_FIX) {
1078 		dcmn_err(CE_NOTE, "fixing devices on pci bus 0x%x", bus);
1079 	} else
1080 		dcmn_err(CE_NOTE, "enumerating pci bus 0x%x", bus);
1081 
1082 	if (config_op == CONFIG_NEW) {
1083 		devlist = (struct pci_devfunc *)pci_bus_res[bus].privdata;
1084 		while (devlist) {
1085 			entry = devlist;
1086 			devlist = entry->next;
1087 			if (entry->reprogram ||
1088 			    pci_bus_res[bus].io_reprogram ||
1089 			    pci_bus_res[bus].mem_reprogram) {
1090 				/* reprogram device(s) */
1091 				(void) add_reg_props(entry->dip, bus,
1092 				    entry->dev, entry->func, CONFIG_NEW, 0);
1093 			}
1094 			kmem_free(entry, sizeof (*entry));
1095 		}
1096 		pci_bus_res[bus].privdata = NULL;
1097 		return;
1098 	}
1099 
1100 	for (dev = 0; dev < max_dev_pci; dev++) {
1101 		nfunc = 1;
1102 		for (func = 0; func < nfunc; func++) {
1103 
1104 			dcmn_err(CE_NOTE, "probing dev 0x%x, func 0x%x",
1105 			    dev, func);
1106 
1107 			venid = pci_getw(bus, dev, func, PCI_CONF_VENID);
1108 
1109 			if ((venid == 0xffff) || (venid == 0)) {
1110 				/* no function at this address */
1111 				continue;
1112 			}
1113 
1114 			header = pci_getb(bus, dev, func, PCI_CONF_HEADER);
1115 			if (header == 0xff) {
1116 				continue; /* illegal value */
1117 			}
1118 
1119 			/*
1120 			 * according to some mail from Microsoft posted
1121 			 * to the pci-drivers alias, their only requirement
1122 			 * for a multifunction device is for the 1st
1123 			 * function to have to PCI_HEADER_MULTI bit set.
1124 			 */
1125 			if ((func == 0) && (header & PCI_HEADER_MULTI)) {
1126 				nfunc = 8;
1127 			}
1128 
1129 			if (config_op == CONFIG_FIX ||
1130 			    config_op == CONFIG_INFO) {
1131 				/*
1132 				 * Create the node, unconditionally, on the
1133 				 * first pass only.  It may still need
1134 				 * resource assignment, which will be
1135 				 * done on the second, CONFIG_NEW, pass.
1136 				 */
1137 				process_devfunc(bus, dev, func, header,
1138 				    venid, config_op);
1139 
1140 			}
1141 		}
1142 	}
1143 
1144 	/* percolate bus used resources up through parents to root */
1145 	if (config_op == CONFIG_INFO) {
1146 		int	par_bus;
1147 
1148 		par_bus = pci_bus_res[bus].par_bus;
1149 		while (par_bus != (uchar_t)-1) {
1150 
1151 			if (pci_bus_res[bus].io_ports_used)
1152 				memlist_merge(&pci_bus_res[bus].io_ports_used,
1153 				    &pci_bus_res[par_bus].io_ports_used);
1154 
1155 			if (pci_bus_res[bus].mem_space_used)
1156 				memlist_merge(&pci_bus_res[bus].mem_space_used,
1157 				    &pci_bus_res[par_bus].mem_space_used);
1158 
1159 			if (pci_bus_res[bus].pmem_space_used)
1160 				memlist_merge(&pci_bus_res[bus].pmem_space_used,
1161 				    &pci_bus_res[par_bus].pmem_space_used);
1162 
1163 			par_bus = pci_bus_res[par_bus].par_bus;
1164 		}
1165 	}
1166 }
1167 
1168 static int
1169 check_pciide_prop(uchar_t revid, ushort_t venid, ushort_t devid,
1170     ushort_t subvenid, ushort_t subdevid)
1171 {
1172 	static int prop_exist = -1;
1173 	static char *pciide_str;
1174 	char compat[32];
1175 
1176 	if (prop_exist == -1) {
1177 		prop_exist = (ddi_prop_lookup_string(DDI_DEV_T_ANY,
1178 		    ddi_root_node(), DDI_PROP_DONTPASS, "pci-ide",
1179 		    &pciide_str) == DDI_SUCCESS);
1180 	}
1181 
1182 	if (!prop_exist)
1183 		return (0);
1184 
1185 	/* compare property value against various forms of compatible */
1186 	if (subvenid) {
1187 		(void) snprintf(compat, sizeof (compat), "pci%x,%x.%x.%x.%x",
1188 		    venid, devid, subvenid, subdevid, revid);
1189 		if (strcmp(pciide_str, compat) == 0)
1190 			return (1);
1191 
1192 		(void) snprintf(compat, sizeof (compat), "pci%x,%x.%x.%x",
1193 		    venid, devid, subvenid, subdevid);
1194 		if (strcmp(pciide_str, compat) == 0)
1195 			return (1);
1196 
1197 		(void) snprintf(compat, sizeof (compat), "pci%x,%x",
1198 		    subvenid, subdevid);
1199 		if (strcmp(pciide_str, compat) == 0)
1200 			return (1);
1201 	}
1202 	(void) snprintf(compat, sizeof (compat), "pci%x,%x.%x",
1203 	    venid, devid, revid);
1204 	if (strcmp(pciide_str, compat) == 0)
1205 		return (1);
1206 
1207 	(void) snprintf(compat, sizeof (compat), "pci%x,%x", venid, devid);
1208 	if (strcmp(pciide_str, compat) == 0)
1209 		return (1);
1210 
1211 	return (0);
1212 }
1213 
1214 static int
1215 is_pciide(uchar_t basecl, uchar_t subcl, uchar_t revid,
1216     ushort_t venid, ushort_t devid, ushort_t subvenid, ushort_t subdevid)
1217 {
1218 	struct ide_table {	/* table for PCI_MASS_OTHER */
1219 		ushort_t venid;
1220 		ushort_t devid;
1221 	} *entry;
1222 
1223 	/* XXX SATA and other devices: need a way to add dynamically */
1224 	static struct ide_table ide_other[] = {
1225 		{0x1095, 0x3112},
1226 		{0x1095, 0x3114},
1227 		{0x1095, 0x3512},
1228 		{0x1095, 0x680},	/* Sil0680 */
1229 		{0x1283, 0x8211},	/* ITE 8211F is subcl PCI_MASS_OTHER */
1230 		{0, 0}
1231 	};
1232 
1233 	if (basecl != PCI_CLASS_MASS)
1234 		return (0);
1235 
1236 	if (subcl == PCI_MASS_IDE) {
1237 		return (1);
1238 	}
1239 
1240 	if (check_pciide_prop(revid, venid, devid, subvenid, subdevid))
1241 		return (1);
1242 
1243 	if (subcl != PCI_MASS_OTHER && subcl != PCI_MASS_SATA) {
1244 		return (0);
1245 	}
1246 
1247 	entry = &ide_other[0];
1248 	while (entry->venid) {
1249 		if (entry->venid == venid && entry->devid == devid)
1250 			return (1);
1251 		entry++;
1252 	}
1253 	return (0);
1254 }
1255 
1256 static int
1257 is_display(uint_t classcode)
1258 {
1259 	static uint_t disp_classes[] = {
1260 		0x000100,
1261 		0x030000,
1262 		0x030001
1263 	};
1264 	int i, nclasses = sizeof (disp_classes) / sizeof (uint_t);
1265 
1266 	for (i = 0; i < nclasses; i++) {
1267 		if (classcode == disp_classes[i])
1268 			return (1);
1269 	}
1270 	return (0);
1271 }
1272 
1273 static void
1274 add_undofix_entry(uint8_t bus, uint8_t dev, uint8_t fn,
1275     void (*undofn)(uint8_t, uint8_t, uint8_t))
1276 {
1277 	struct pci_fixundo *newundo;
1278 
1279 	newundo = kmem_alloc(sizeof (struct pci_fixundo), KM_SLEEP);
1280 
1281 	/*
1282 	 * Adding an item to this list means that we must turn its NMIENABLE
1283 	 * bit back on at a later time.
1284 	 */
1285 	newundo->bus = bus;
1286 	newundo->dev = dev;
1287 	newundo->fn = fn;
1288 	newundo->undofn = undofn;
1289 	newundo->next = undolist;
1290 
1291 	/* add to the undo list in LIFO order */
1292 	undolist = newundo;
1293 }
1294 
1295 void
1296 add_pci_fixes(void)
1297 {
1298 	int i;
1299 
1300 	for (i = 0; i <= pci_bios_nbus; i++) {
1301 		/*
1302 		 * For each bus, apply needed fixes to the appropriate devices.
1303 		 * This must be done before the main enumeration loop because
1304 		 * some fixes must be applied to devices normally encountered
1305 		 * later in the pci scan (e.g. if a fix to device 7 must be
1306 		 * applied before scanning device 6, applying fixes in the
1307 		 * normal enumeration loop would obviously be too late).
1308 		 */
1309 		enumerate_bus_devs(i, CONFIG_FIX);
1310 	}
1311 }
1312 
1313 void
1314 undo_pci_fixes(void)
1315 {
1316 	struct pci_fixundo *nextundo;
1317 	uint8_t bus, dev, fn;
1318 
1319 	/*
1320 	 * All fixes in the undo list are performed unconditionally.  Future
1321 	 * fixes may require selective undo.
1322 	 */
1323 	while (undolist != NULL) {
1324 
1325 		bus = undolist->bus;
1326 		dev = undolist->dev;
1327 		fn = undolist->fn;
1328 
1329 		(*(undolist->undofn))(bus, dev, fn);
1330 
1331 		nextundo = undolist->next;
1332 		kmem_free(undolist, sizeof (struct pci_fixundo));
1333 		undolist = nextundo;
1334 	}
1335 }
1336 
1337 static void
1338 undo_amd8111_pci_fix(uint8_t bus, uint8_t dev, uint8_t fn)
1339 {
1340 	uint8_t val8;
1341 
1342 	val8 = pci_getb(bus, dev, fn, LPC_IO_CONTROL_REG_1);
1343 	/*
1344 	 * The NMIONERR bit is turned back on to allow the SMM BIOS
1345 	 * to handle more critical PCI errors (e.g. PERR#).
1346 	 */
1347 	val8 |= AMD8111_ENABLENMI;
1348 	pci_putb(bus, dev, fn, LPC_IO_CONTROL_REG_1, val8);
1349 }
1350 
1351 static void
1352 pci_fix_amd8111(uint8_t bus, uint8_t dev, uint8_t fn)
1353 {
1354 	uint8_t val8;
1355 
1356 	val8 = pci_getb(bus, dev, fn, LPC_IO_CONTROL_REG_1);
1357 
1358 	if ((val8 & AMD8111_ENABLENMI) == 0)
1359 		return;
1360 
1361 	/*
1362 	 * We reset NMIONERR in the LPC because master-abort on the PCI
1363 	 * bridge side of the 8111 will cause NMI, which might cause SMI,
1364 	 * which sometimes prevents all devices from being enumerated.
1365 	 */
1366 	val8 &= ~AMD8111_ENABLENMI;
1367 
1368 	pci_putb(bus, dev, fn, LPC_IO_CONTROL_REG_1, val8);
1369 
1370 	add_undofix_entry(bus, dev, fn, undo_amd8111_pci_fix);
1371 }
1372 
1373 static void
1374 set_devpm_d0(uchar_t bus, uchar_t dev, uchar_t func)
1375 {
1376 	uint16_t status;
1377 	uint8_t header;
1378 	uint8_t cap_ptr;
1379 	uint8_t cap_id;
1380 	uint16_t pmcsr;
1381 
1382 	status = pci_getw(bus, dev, func, PCI_CONF_STAT);
1383 	if (!(status & PCI_STAT_CAP))
1384 		return;	/* No capabilities list */
1385 
1386 	header = pci_getb(bus, dev, func, PCI_CONF_HEADER) & PCI_HEADER_TYPE_M;
1387 	if (header == PCI_HEADER_CARDBUS)
1388 		cap_ptr = pci_getb(bus, dev, func, PCI_CBUS_CAP_PTR);
1389 	else
1390 		cap_ptr = pci_getb(bus, dev, func, PCI_CONF_CAP_PTR);
1391 	/*
1392 	 * Walk the capabilities list searching for a PM entry.
1393 	 */
1394 	while (cap_ptr != PCI_CAP_NEXT_PTR_NULL && cap_ptr >= PCI_CAP_PTR_OFF) {
1395 		cap_ptr &= PCI_CAP_PTR_MASK;
1396 		cap_id = pci_getb(bus, dev, func, cap_ptr + PCI_CAP_ID);
1397 		if (cap_id == PCI_CAP_ID_PM) {
1398 			pmcsr = pci_getw(bus, dev, func, cap_ptr + PCI_PMCSR);
1399 			pmcsr &= ~(PCI_PMCSR_STATE_MASK);
1400 			pmcsr |= PCI_PMCSR_D0; /* D0 state */
1401 			pci_putw(bus, dev, func, cap_ptr + PCI_PMCSR, pmcsr);
1402 			break;
1403 		}
1404 		cap_ptr = pci_getb(bus, dev, func, cap_ptr + PCI_CAP_NEXT_PTR);
1405 	}
1406 
1407 }
1408 
1409 #define	is_isa(bc, sc)	\
1410 	(((bc) == PCI_CLASS_BRIDGE) && ((sc) == PCI_BRIDGE_ISA))
1411 
1412 static void
1413 process_devfunc(uchar_t bus, uchar_t dev, uchar_t func, uchar_t header,
1414     ushort_t vendorid, int config_op)
1415 {
1416 	char nodename[32], unitaddr[5];
1417 	dev_info_t *dip;
1418 	uchar_t basecl, subcl, progcl, intr, revid;
1419 	ushort_t subvenid, subdevid, status;
1420 	ushort_t slot_num;
1421 	uint_t classcode, revclass;
1422 	int reprogram = 0, pciide = 0;
1423 	int power[2] = {1, 1};
1424 	int pciex = 0;
1425 	ushort_t is_pci_bridge = 0;
1426 	struct pci_devfunc *devlist = NULL, *entry = NULL;
1427 	iommu_private_t *private;
1428 	gfx_entry_t *gfxp;
1429 
1430 	ushort_t deviceid = pci_getw(bus, dev, func, PCI_CONF_DEVID);
1431 
1432 	switch (header & PCI_HEADER_TYPE_M) {
1433 	case PCI_HEADER_ZERO:
1434 		subvenid = pci_getw(bus, dev, func, PCI_CONF_SUBVENID);
1435 		subdevid = pci_getw(bus, dev, func, PCI_CONF_SUBSYSID);
1436 		break;
1437 	case PCI_HEADER_CARDBUS:
1438 		subvenid = pci_getw(bus, dev, func, PCI_CBUS_SUBVENID);
1439 		subdevid = pci_getw(bus, dev, func, PCI_CBUS_SUBSYSID);
1440 		/* Record the # of cardbus bridges found on the bus */
1441 		if (config_op == CONFIG_INFO)
1442 			pci_bus_res[bus].num_cbb++;
1443 		break;
1444 	default:
1445 		subvenid = 0;
1446 		subdevid = 0;
1447 		break;
1448 	}
1449 
1450 	if (config_op == CONFIG_FIX) {
1451 		if (vendorid == VENID_AMD && deviceid == DEVID_AMD8111_LPC) {
1452 			pci_fix_amd8111(bus, dev, func);
1453 		}
1454 		return;
1455 	}
1456 
1457 	/* XXX should be use generic names? derive from class? */
1458 	revclass = pci_getl(bus, dev, func, PCI_CONF_REVID);
1459 	classcode = revclass >> 8;
1460 	revid = revclass & 0xff;
1461 
1462 	/* figure out if this is pci-ide */
1463 	basecl = classcode >> 16;
1464 	subcl = (classcode >> 8) & 0xff;
1465 	progcl = classcode & 0xff;
1466 
1467 
1468 	if (is_display(classcode))
1469 		(void) snprintf(nodename, sizeof (nodename), "display");
1470 	else if (!pseudo_isa && is_isa(basecl, subcl))
1471 		(void) snprintf(nodename, sizeof (nodename), "isa");
1472 	else if (subvenid != 0)
1473 		(void) snprintf(nodename, sizeof (nodename),
1474 		    "pci%x,%x", subvenid, subdevid);
1475 	else
1476 		(void) snprintf(nodename, sizeof (nodename),
1477 		    "pci%x,%x", vendorid, deviceid);
1478 
1479 	/* make sure parent bus dip has been created */
1480 	if (pci_bus_res[bus].dip == NULL)
1481 		create_root_bus_dip(bus);
1482 
1483 	ndi_devi_alloc_sleep(pci_bus_res[bus].dip, nodename,
1484 	    DEVI_SID_NODEID, &dip);
1485 
1486 	if (check_if_device_is_pciex(dip, bus, dev, func, &slot_num,
1487 	    &is_pci_bridge) == B_TRUE)
1488 		pciex = 1;
1489 
1490 	/* add properties */
1491 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, "device-id", deviceid);
1492 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, "vendor-id", vendorid);
1493 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, "revision-id", revid);
1494 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1495 	    "class-code", classcode);
1496 	if (func == 0)
1497 		(void) snprintf(unitaddr, sizeof (unitaddr), "%x", dev);
1498 	else
1499 		(void) snprintf(unitaddr, sizeof (unitaddr),
1500 		    "%x,%x", dev, func);
1501 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1502 	    "unit-address", unitaddr);
1503 
1504 	/* add device_type for display nodes */
1505 	if (is_display(classcode)) {
1506 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1507 		    "device_type", "display");
1508 	}
1509 	/* add special stuff for header type */
1510 	if ((header & PCI_HEADER_TYPE_M) == PCI_HEADER_ZERO) {
1511 		uchar_t mingrant = pci_getb(bus, dev, func, PCI_CONF_MIN_G);
1512 		uchar_t maxlatency = pci_getb(bus, dev, func, PCI_CONF_MAX_L);
1513 
1514 		if (subvenid != 0) {
1515 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1516 			    "subsystem-id", subdevid);
1517 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1518 			    "subsystem-vendor-id", subvenid);
1519 		}
1520 		if (!pciex)
1521 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1522 			    "min-grant", mingrant);
1523 		if (!pciex)
1524 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1525 			    "max-latency", maxlatency);
1526 	}
1527 
1528 	/* interrupt, record if not 0 */
1529 	intr = pci_getb(bus, dev, func, PCI_CONF_IPIN);
1530 	if (intr != 0)
1531 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1532 		    "interrupts", intr);
1533 
1534 	/*
1535 	 * Add support for 133 mhz pci eventually
1536 	 */
1537 	status = pci_getw(bus, dev, func, PCI_CONF_STAT);
1538 
1539 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1540 	    "devsel-speed", (status & PCI_STAT_DEVSELT) >> 9);
1541 	if (!pciex && (status & PCI_STAT_FBBC))
1542 		(void) ndi_prop_create_boolean(DDI_DEV_T_NONE, dip,
1543 		    "fast-back-to-back");
1544 	if (!pciex && (status & PCI_STAT_66MHZ))
1545 		(void) ndi_prop_create_boolean(DDI_DEV_T_NONE, dip,
1546 		    "66mhz-capable");
1547 	if (status & PCI_STAT_UDF)
1548 		(void) ndi_prop_create_boolean(DDI_DEV_T_NONE, dip,
1549 		    "udf-supported");
1550 	if (pciex && slot_num) {
1551 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1552 		    "physical-slot#", slot_num);
1553 		if (!is_pci_bridge)
1554 			pciex_slot_names_prop(dip, slot_num);
1555 	}
1556 
1557 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
1558 	    "power-consumption", power, 2);
1559 
1560 	/* Set the device PM state to D0 */
1561 	set_devpm_d0(bus, dev, func);
1562 
1563 	if ((basecl == PCI_CLASS_BRIDGE) && (subcl == PCI_BRIDGE_PCI))
1564 		add_ppb_props(dip, bus, dev, func, pciex, is_pci_bridge);
1565 	else {
1566 		/*
1567 		 * Record the non-PPB devices on the bus for possible
1568 		 * reprogramming at 2nd bus enumeration.
1569 		 * Note: PPB reprogramming is done in fix_ppb_res()
1570 		 */
1571 		devlist = (struct pci_devfunc *)pci_bus_res[bus].privdata;
1572 		entry = kmem_zalloc(sizeof (*entry), KM_SLEEP);
1573 		entry->dip = dip;
1574 		entry->dev = dev;
1575 		entry->func = func;
1576 		entry->next = devlist;
1577 		pci_bus_res[bus].privdata = entry;
1578 	}
1579 
1580 	if (config_op == CONFIG_INFO &&
1581 	    IS_CLASS_IOAPIC(basecl, subcl, progcl)) {
1582 		create_ioapic_node(bus, dev, func, vendorid, deviceid);
1583 	}
1584 
1585 	/* check for ck8-04 based PCI ISA bridge only */
1586 	if (NVIDIA_IS_LPC_BRIDGE(vendorid, deviceid) && (dev == 1) &&
1587 	    (func == 0))
1588 		add_nvidia_isa_bridge_props(dip, bus, dev, func);
1589 
1590 	if (pciex && is_pci_bridge)
1591 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, "model",
1592 		    (char *)"PCIe-PCI bridge");
1593 	else
1594 		add_model_prop(dip, classcode);
1595 
1596 	add_compatible(dip, subvenid, subdevid, vendorid, deviceid,
1597 	    revid, classcode, pciex);
1598 
1599 	/*
1600 	 * See if this device is a controller that advertises
1601 	 * itself to be a standard ATA task file controller, or one that
1602 	 * has been hard coded.
1603 	 *
1604 	 * If it is, check if any other higher precedence driver listed in
1605 	 * driver_aliases will claim the node by calling
1606 	 * ddi_compatibile_driver_major.  If so, clear pciide and do not
1607 	 * create a pci-ide node or any other special handling.
1608 	 *
1609 	 * If another driver does not bind, set the node name to pci-ide
1610 	 * and then let the special pci-ide handling for registers and
1611 	 * child pci-ide nodes proceed below.
1612 	 */
1613 	if (is_pciide(basecl, subcl, revid, vendorid, deviceid,
1614 	    subvenid, subdevid) == 1) {
1615 		if (ddi_compatible_driver_major(dip, NULL) == (major_t)-1) {
1616 			(void) ndi_devi_set_nodename(dip, "pci-ide", 0);
1617 			pciide = 1;
1618 		}
1619 	}
1620 
1621 	reprogram = add_reg_props(dip, bus, dev, func, config_op, pciide);
1622 	(void) ndi_devi_bind_driver(dip, 0);
1623 
1624 	/* special handling for pci-ide */
1625 	if (pciide) {
1626 		dev_info_t *cdip;
1627 
1628 		/*
1629 		 * Create properties specified by P1275 Working Group
1630 		 * Proposal #414 Version 1
1631 		 */
1632 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1633 		    "device_type", "pci-ide");
1634 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1635 		    "#address-cells", 1);
1636 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1637 		    "#size-cells", 0);
1638 
1639 		/* allocate two child nodes */
1640 		ndi_devi_alloc_sleep(dip, "ide",
1641 		    (pnode_t)DEVI_SID_NODEID, &cdip);
1642 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cdip,
1643 		    "reg", 0);
1644 		(void) ndi_devi_bind_driver(cdip, 0);
1645 		ndi_devi_alloc_sleep(dip, "ide",
1646 		    (pnode_t)DEVI_SID_NODEID, &cdip);
1647 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cdip,
1648 		    "reg", 1);
1649 		(void) ndi_devi_bind_driver(cdip, 0);
1650 
1651 		reprogram = 0;	/* don't reprogram pci-ide bridge */
1652 	}
1653 
1654 	/* allocate and set up iommu private */
1655 	private = kmem_alloc(sizeof (iommu_private_t), KM_SLEEP);
1656 	private->idp_seg = 0;
1657 	private->idp_bus = bus;
1658 	private->idp_devfn = (dev << 3) | func;
1659 	private->idp_sec = 0;
1660 	private->idp_sub = 0;
1661 	private->idp_bbp_type = IOMMU_PPB_NONE;
1662 	/* record the bridge */
1663 	private->idp_is_bridge = ((basecl == PCI_CLASS_BRIDGE) &&
1664 	    (subcl == PCI_BRIDGE_PCI));
1665 	if (private->idp_is_bridge) {
1666 		private->idp_sec = pci_getb(bus, dev, func, PCI_BCNF_SECBUS);
1667 		private->idp_sub = pci_getb(bus, dev, func, PCI_BCNF_SUBBUS);
1668 		if (pciex && is_pci_bridge)
1669 			private->idp_bbp_type = IOMMU_PPB_PCIE_PCI;
1670 		else if (pciex)
1671 			private->idp_bbp_type = IOMMU_PPB_PCIE_PCIE;
1672 		else
1673 			private->idp_bbp_type = IOMMU_PPB_PCI_PCI;
1674 	}
1675 	/* record the special devices */
1676 	private->idp_is_display = (is_display(classcode) ? B_TRUE : B_FALSE);
1677 	private->idp_is_lpc = ((basecl == PCI_CLASS_BRIDGE) &&
1678 	    (subcl == PCI_BRIDGE_ISA));
1679 	private->idp_intel_domain = NULL;
1680 	/* hook the private to dip */
1681 	DEVI(dip)->devi_iommu_private = private;
1682 
1683 	if (private->idp_is_display == B_TRUE) {
1684 		gfxp = kmem_zalloc(sizeof (*gfxp), KM_SLEEP);
1685 		gfxp->g_dip = dip;
1686 		gfxp->g_prev = NULL;
1687 		gfxp->g_next = gfx_devinfo_list;
1688 		gfx_devinfo_list = gfxp;
1689 		if (gfxp->g_next)
1690 			gfxp->g_next->g_prev = gfxp;
1691 	}
1692 
1693 	/* special handling for isa */
1694 	if (!pseudo_isa && is_isa(basecl, subcl)) {
1695 		/* add device_type */
1696 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1697 		    "device_type", "isa");
1698 	}
1699 
1700 	if (reprogram && (entry != NULL))
1701 		entry->reprogram = B_TRUE;
1702 }
1703 
1704 /*
1705  * Set the compatible property to a value compliant with
1706  * rev 2.1 of the IEEE1275 PCI binding.
1707  * (Also used for PCI-Express devices).
1708  *
1709  *   pciVVVV,DDDD.SSSS.ssss.RR	(0)
1710  *   pciVVVV,DDDD.SSSS.ssss	(1)
1711  *   pciSSSS,ssss		(2)
1712  *   pciVVVV,DDDD.RR		(3)
1713  *   pciVVVV,DDDD		(4)
1714  *   pciclass,CCSSPP		(5)
1715  *   pciclass,CCSS		(6)
1716  *
1717  * The Subsystem (SSSS) forms are not inserted if
1718  * subsystem-vendor-id is 0.
1719  *
1720  * NOTE: For PCI-Express devices "pci" is replaced with "pciex" in 0-6 above
1721  * property 2 is not created as per "1275 bindings for PCI Express Interconnect"
1722  *
1723  * Set with setprop and \x00 between each
1724  * to generate the encoded string array form.
1725  */
1726 void
1727 add_compatible(dev_info_t *dip, ushort_t subvenid, ushort_t subdevid,
1728     ushort_t vendorid, ushort_t deviceid, uchar_t revid, uint_t classcode,
1729     int pciex)
1730 {
1731 	int i = 0;
1732 	int size = COMPAT_BUFSIZE;
1733 	char *compat[13];
1734 	char *buf, *curr;
1735 
1736 	curr = buf = kmem_alloc(size, KM_SLEEP);
1737 
1738 	if (pciex) {
1739 		if (subvenid) {
1740 			compat[i++] = curr;	/* form 0 */
1741 			(void) snprintf(curr, size, "pciex%x,%x.%x.%x.%x",
1742 			    vendorid, deviceid, subvenid, subdevid, revid);
1743 			size -= strlen(curr) + 1;
1744 			curr += strlen(curr) + 1;
1745 
1746 			compat[i++] = curr;	/* form 1 */
1747 			(void) snprintf(curr, size, "pciex%x,%x.%x.%x",
1748 			    vendorid, deviceid, subvenid, subdevid);
1749 			size -= strlen(curr) + 1;
1750 			curr += strlen(curr) + 1;
1751 
1752 		}
1753 		compat[i++] = curr;	/* form 3 */
1754 		(void) snprintf(curr, size, "pciex%x,%x.%x",
1755 		    vendorid, deviceid, revid);
1756 		size -= strlen(curr) + 1;
1757 		curr += strlen(curr) + 1;
1758 
1759 		compat[i++] = curr;	/* form 4 */
1760 		(void) snprintf(curr, size, "pciex%x,%x", vendorid, deviceid);
1761 		size -= strlen(curr) + 1;
1762 		curr += strlen(curr) + 1;
1763 
1764 		compat[i++] = curr;	/* form 5 */
1765 		(void) snprintf(curr, size, "pciexclass,%06x", classcode);
1766 		size -= strlen(curr) + 1;
1767 		curr += strlen(curr) + 1;
1768 
1769 		compat[i++] = curr;	/* form 6 */
1770 		(void) snprintf(curr, size, "pciexclass,%04x",
1771 		    (classcode >> 8));
1772 		size -= strlen(curr) + 1;
1773 		curr += strlen(curr) + 1;
1774 	}
1775 
1776 	if (subvenid) {
1777 		compat[i++] = curr;	/* form 0 */
1778 		(void) snprintf(curr, size, "pci%x,%x.%x.%x.%x",
1779 		    vendorid, deviceid, subvenid, subdevid, revid);
1780 		size -= strlen(curr) + 1;
1781 		curr += strlen(curr) + 1;
1782 
1783 		compat[i++] = curr;	/* form 1 */
1784 		(void) snprintf(curr, size, "pci%x,%x.%x.%x",
1785 		    vendorid, deviceid, subvenid, subdevid);
1786 		size -= strlen(curr) + 1;
1787 		curr += strlen(curr) + 1;
1788 
1789 		compat[i++] = curr;	/* form 2 */
1790 		(void) snprintf(curr, size, "pci%x,%x", subvenid, subdevid);
1791 		size -= strlen(curr) + 1;
1792 		curr += strlen(curr) + 1;
1793 	}
1794 	compat[i++] = curr;	/* form 3 */
1795 	(void) snprintf(curr, size, "pci%x,%x.%x", vendorid, deviceid, revid);
1796 	size -= strlen(curr) + 1;
1797 	curr += strlen(curr) + 1;
1798 
1799 	compat[i++] = curr;	/* form 4 */
1800 	(void) snprintf(curr, size, "pci%x,%x", vendorid, deviceid);
1801 	size -= strlen(curr) + 1;
1802 	curr += strlen(curr) + 1;
1803 
1804 	compat[i++] = curr;	/* form 5 */
1805 	(void) snprintf(curr, size, "pciclass,%06x", classcode);
1806 	size -= strlen(curr) + 1;
1807 	curr += strlen(curr) + 1;
1808 
1809 	compat[i++] = curr;	/* form 6 */
1810 	(void) snprintf(curr, size, "pciclass,%04x", (classcode >> 8));
1811 	size -= strlen(curr) + 1;
1812 	curr += strlen(curr) + 1;
1813 
1814 	(void) ndi_prop_update_string_array(DDI_DEV_T_NONE, dip,
1815 	    "compatible", compat, i);
1816 	kmem_free(buf, COMPAT_BUFSIZE);
1817 }
1818 
1819 /*
1820  * Adjust the reg properties for a dual channel PCI-IDE device.
1821  *
1822  * NOTE: don't do anything that changes the order of the hard-decodes
1823  * and programmed BARs. The kernel driver depends on these values
1824  * being in this order regardless of whether they're for a 'native'
1825  * mode BAR or not.
1826  */
1827 /*
1828  * config info for pci-ide devices
1829  */
1830 static struct {
1831 	uchar_t  native_mask;	/* 0 == 'compatibility' mode, 1 == native */
1832 	uchar_t  bar_offset;	/* offset for alt status register */
1833 	ushort_t addr;		/* compatibility mode base address */
1834 	ushort_t length;	/* number of ports for this BAR */
1835 } pciide_bar[] = {
1836 	{ 0x01, 0, 0x1f0, 8 },	/* primary lower BAR */
1837 	{ 0x01, 2, 0x3f6, 1 },	/* primary upper BAR */
1838 	{ 0x04, 0, 0x170, 8 },	/* secondary lower BAR */
1839 	{ 0x04, 2, 0x376, 1 }	/* secondary upper BAR */
1840 };
1841 
1842 static int
1843 pciIdeAdjustBAR(uchar_t progcl, int index, uint_t *basep, uint_t *lenp)
1844 {
1845 	int hard_decode = 0;
1846 
1847 	/*
1848 	 * Adjust the base and len for the BARs of the PCI-IDE
1849 	 * device's primary and secondary controllers. The first
1850 	 * two BARs are for the primary controller and the next
1851 	 * two BARs are for the secondary controller. The fifth
1852 	 * and sixth bars are never adjusted.
1853 	 */
1854 	if (index >= 0 && index <= 3) {
1855 		*lenp = pciide_bar[index].length;
1856 
1857 		if (progcl & pciide_bar[index].native_mask) {
1858 			*basep += pciide_bar[index].bar_offset;
1859 		} else {
1860 			*basep = pciide_bar[index].addr;
1861 			hard_decode = 1;
1862 		}
1863 	}
1864 
1865 	/*
1866 	 * if either base or len is zero make certain both are zero
1867 	 */
1868 	if (*basep == 0 || *lenp == 0) {
1869 		*basep = 0;
1870 		*lenp = 0;
1871 		hard_decode = 0;
1872 	}
1873 
1874 	return (hard_decode);
1875 }
1876 
1877 
1878 /*
1879  * Add the "reg" and "assigned-addresses" property
1880  */
1881 static int
1882 add_reg_props(dev_info_t *dip, uchar_t bus, uchar_t dev, uchar_t func,
1883     int config_op, int pciide)
1884 {
1885 	uchar_t baseclass, subclass, progclass, header;
1886 	ushort_t bar_sz;
1887 	uint_t value = 0, len, devloc;
1888 	uint_t base, base_hi, type;
1889 	ushort_t offset, end;
1890 	int max_basereg, j, reprogram = 0;
1891 	uint_t phys_hi;
1892 	struct memlist **io_res, **io_res_used;
1893 	struct memlist **mem_res, **mem_res_used;
1894 	struct memlist **pmem_res, **pmem_res_used;
1895 	uchar_t res_bus;
1896 
1897 	pci_regspec_t regs[16] = {{0}};
1898 	pci_regspec_t assigned[15] = {{0}};
1899 	int nreg, nasgn;
1900 
1901 	io_res = &pci_bus_res[bus].io_ports;
1902 	io_res_used = &pci_bus_res[bus].io_ports_used;
1903 	mem_res = &pci_bus_res[bus].mem_space;
1904 	mem_res_used = &pci_bus_res[bus].mem_space_used;
1905 	pmem_res = &pci_bus_res[bus].pmem_space;
1906 	pmem_res_used = &pci_bus_res[bus].pmem_space_used;
1907 
1908 	devloc = (uint_t)bus << 16 | (uint_t)dev << 11 | (uint_t)func << 8;
1909 	regs[0].pci_phys_hi = devloc;
1910 	nreg = 1;	/* rest of regs[0] is all zero */
1911 	nasgn = 0;
1912 
1913 	baseclass = pci_getb(bus, dev, func, PCI_CONF_BASCLASS);
1914 	subclass = pci_getb(bus, dev, func, PCI_CONF_SUBCLASS);
1915 	progclass = pci_getb(bus, dev, func, PCI_CONF_PROGCLASS);
1916 	header = pci_getb(bus, dev, func, PCI_CONF_HEADER) & PCI_HEADER_TYPE_M;
1917 
1918 	switch (header) {
1919 	case PCI_HEADER_ZERO:
1920 		max_basereg = PCI_BASE_NUM;
1921 		break;
1922 	case PCI_HEADER_PPB:
1923 		max_basereg = PCI_BCNF_BASE_NUM;
1924 		break;
1925 	case PCI_HEADER_CARDBUS:
1926 		max_basereg = PCI_CBUS_BASE_NUM;
1927 		break;
1928 	default:
1929 		max_basereg = 0;
1930 		break;
1931 	}
1932 
1933 	/*
1934 	 * Create the register property by saving the current
1935 	 * value of the base register. Write 0xffffffff to the
1936 	 * base register.  Read the value back to determine the
1937 	 * required size of the address space.  Restore the base
1938 	 * register contents.
1939 	 *
1940 	 * Do not disable I/O and memory access; this isn't necessary
1941 	 * since no driver is yet attached to this device, and disabling
1942 	 * I/O and memory access has the side-effect of disabling PCI-PCI
1943 	 * bridge mappings, which makes the bridge transparent to secondary-
1944 	 * bus activity (see sections 4.1-4.3 of the PCI-PCI Bridge
1945 	 * Spec V1.2).
1946 	 */
1947 	end = PCI_CONF_BASE0 + max_basereg * sizeof (uint_t);
1948 	for (j = 0, offset = PCI_CONF_BASE0; offset < end;
1949 	    j++, offset += bar_sz) {
1950 		/* determine the size of the address space */
1951 		base = pci_getl(bus, dev, func, offset);
1952 		pci_putl(bus, dev, func, offset, 0xffffffff);
1953 		value = pci_getl(bus, dev, func, offset);
1954 		pci_putl(bus, dev, func, offset, base);
1955 
1956 		/* construct phys hi,med.lo, size hi, lo */
1957 		if ((pciide && j < 4) || (base & PCI_BASE_SPACE_IO)) {
1958 			int hard_decode = 0;
1959 
1960 			/* i/o space */
1961 			bar_sz = PCI_BAR_SZ_32;
1962 			value &= PCI_BASE_IO_ADDR_M;
1963 			len = ((value ^ (value-1)) + 1) >> 1;
1964 
1965 			/* XXX Adjust first 4 IDE registers */
1966 			if (pciide) {
1967 				if (subclass != PCI_MASS_IDE)
1968 					progclass = (PCI_IDE_IF_NATIVE_PRI |
1969 					    PCI_IDE_IF_NATIVE_SEC);
1970 				hard_decode = pciIdeAdjustBAR(progclass, j,
1971 				    &base, &len);
1972 			} else if (value == 0) {
1973 				/* skip base regs with size of 0 */
1974 				continue;
1975 			}
1976 
1977 			regs[nreg].pci_phys_hi = PCI_ADDR_IO | devloc |
1978 			    (hard_decode ? PCI_RELOCAT_B : offset);
1979 			regs[nreg].pci_phys_low = hard_decode ?
1980 			    base & PCI_BASE_IO_ADDR_M : 0;
1981 			assigned[nasgn].pci_phys_hi =
1982 			    PCI_RELOCAT_B | regs[nreg].pci_phys_hi;
1983 			regs[nreg].pci_size_low =
1984 			    assigned[nasgn].pci_size_low = len;
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_ALIAS_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