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