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