xref: /illumos-gate/usr/src/uts/intel/io/pci/pci_boot.c (revision c559157643fef9f9afb0414e00a3579407ba3052)
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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright 2019 Joyent, Inc.
24  * Copyright 2019 Western Digital Corporation
25  */
26 
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/sysmacros.h>
30 #include <sys/sunndi.h>
31 #include <sys/pci.h>
32 #include <sys/pci_impl.h>
33 #include <sys/pcie_impl.h>
34 #include <sys/memlist.h>
35 #include <sys/bootconf.h>
36 #include <io/pci/mps_table.h>
37 #include <sys/pci_cfgacc.h>
38 #include <sys/pci_cfgspace.h>
39 #include <sys/pci_cfgspace_impl.h>
40 #include <sys/psw.h>
41 #include "../../../../common/pci/pci_strings.h"
42 #include <sys/apic.h>
43 #include <io/pciex/pcie_nvidia.h>
44 #include <sys/hotplug/pci/pciehpc_acpi.h>
45 #include <sys/acpi/acpi.h>
46 #include <sys/acpica.h>
47 #include <sys/iommulib.h>
48 #include <sys/devcache.h>
49 #include <sys/pci_cfgacc_x86.h>
50 
51 #define	pci_getb	(*pci_getb_func)
52 #define	pci_getw	(*pci_getw_func)
53 #define	pci_getl	(*pci_getl_func)
54 #define	pci_putb	(*pci_putb_func)
55 #define	pci_putw	(*pci_putw_func)
56 #define	pci_putl	(*pci_putl_func)
57 #define	dcmn_err	if (pci_boot_debug) cmn_err
58 
59 #define	CONFIG_INFO	0
60 #define	CONFIG_UPDATE	1
61 #define	CONFIG_NEW	2
62 #define	CONFIG_FIX	3
63 #define	COMPAT_BUFSIZE	512
64 
65 #define	PPB_IO_ALIGNMENT	0x1000		/* 4K aligned */
66 #define	PPB_MEM_ALIGNMENT	0x100000	/* 1M aligned */
67 /* round down to nearest power of two */
68 #define	P2LE(align)					\
69 	{						\
70 		int i = 0;				\
71 		while (align >>= 1)			\
72 			i ++;				\
73 		align = 1 << i;				\
74 	}						\
75 
76 /* for is_vga and list_is_vga_only */
77 
78 enum io_mem {
79 	IO,
80 	MEM
81 };
82 
83 /* See AMD-8111 Datasheet Rev 3.03, Page 149: */
84 #define	LPC_IO_CONTROL_REG_1	0x40
85 #define	AMD8111_ENABLENMI	(uint8_t)0x80
86 #define	DEVID_AMD8111_LPC	0x7468
87 
88 struct pci_fixundo {
89 	uint8_t			bus;
90 	uint8_t			dev;
91 	uint8_t			fn;
92 	void			(*undofn)(uint8_t, uint8_t, uint8_t);
93 	struct pci_fixundo	*next;
94 };
95 
96 struct pci_devfunc {
97 	struct pci_devfunc *next;
98 	dev_info_t *dip;
99 	uchar_t dev;
100 	uchar_t func;
101 	boolean_t reprogram;	/* this device needs to be reprogrammed */
102 };
103 
104 extern int apic_nvidia_io_max;
105 extern int pseudo_isa;
106 extern int pci_bios_maxbus;
107 static uchar_t max_dev_pci = 32;	/* PCI standard */
108 int pci_boot_debug = 0;
109 extern struct memlist *find_bus_res(int, int);
110 static struct pci_fixundo *undolist = NULL;
111 static int num_root_bus = 0;	/* count of root buses */
112 extern volatile int acpi_resource_discovery;
113 extern uint64_t mcfg_mem_base;
114 extern void pci_cfgacc_add_workaround(uint16_t, uchar_t, uchar_t);
115 extern dev_info_t *pcie_get_rc_dip(dev_info_t *);
116 
117 /*
118  * Module prototypes
119  */
120 static void enumerate_bus_devs(uchar_t bus, int config_op);
121 static void create_root_bus_dip(uchar_t bus);
122 static void process_devfunc(uchar_t, uchar_t, uchar_t, uchar_t,
123     ushort_t, int);
124 static void add_compatible(dev_info_t *, ushort_t, ushort_t,
125     ushort_t, ushort_t, uchar_t, uint_t, int);
126 static int add_reg_props(dev_info_t *, uchar_t, uchar_t, uchar_t, int, int);
127 static void add_ppb_props(dev_info_t *, uchar_t, uchar_t, uchar_t, int,
128     ushort_t);
129 static void add_model_prop(dev_info_t *, uint_t);
130 static void add_bus_range_prop(int);
131 static void add_bus_slot_names_prop(int);
132 static void add_ranges_prop(int, int);
133 static void add_bus_available_prop(int);
134 static int get_pci_cap(uchar_t bus, uchar_t dev, uchar_t func, uint8_t cap_id);
135 static void fix_ppb_res(uchar_t, boolean_t);
136 static void alloc_res_array();
137 static void create_ioapic_node(int bus, int dev, int fn, ushort_t vendorid,
138     ushort_t deviceid);
139 static void pciex_slot_names_prop(dev_info_t *, ushort_t);
140 static void populate_bus_res(uchar_t bus);
141 static void memlist_remove_list(struct memlist **list,
142     struct memlist *remove_list);
143 static void ck804_fix_aer_ptr(dev_info_t *, pcie_req_id_t);
144 
145 static void pci_scan_bbn(void);
146 static int pci_unitaddr_cache_valid(void);
147 static int pci_bus_unitaddr(int);
148 static void pci_unitaddr_cache_create(void);
149 
150 static int pci_cache_unpack_nvlist(nvf_handle_t, nvlist_t *, char *);
151 static int pci_cache_pack_nvlist(nvf_handle_t, nvlist_t **);
152 static void pci_cache_free_list(nvf_handle_t);
153 
154 extern int pci_slot_names_prop(int, char *, int);
155 
156 /* set non-zero to force PCI peer-bus renumbering */
157 int pci_bus_always_renumber = 0;
158 
159 /*
160  * used to register ISA resource usage which must not be made
161  * "available" from other PCI node' resource maps
162  */
163 static struct {
164 	struct memlist *io_used;
165 	struct memlist *mem_used;
166 } isa_res;
167 
168 /*
169  * PCI unit-address cache management
170  */
171 static nvf_ops_t pci_unitaddr_cache_ops = {
172 	"/etc/devices/pci_unitaddr_persistent",	/* path to cache */
173 	pci_cache_unpack_nvlist,		/* read in nvlist form */
174 	pci_cache_pack_nvlist,			/* convert to nvlist form */
175 	pci_cache_free_list,			/* free data list */
176 	NULL					/* write complete callback */
177 };
178 
179 typedef struct {
180 	list_node_t	pua_nodes;
181 	int		pua_index;
182 	int		pua_addr;
183 } pua_node_t;
184 
185 nvf_handle_t	puafd_handle;
186 int		pua_cache_valid = 0;
187 
188 
189 /*ARGSUSED*/
190 static ACPI_STATUS
191 pci_process_acpi_device(ACPI_HANDLE hdl, UINT32 level, void *ctx, void **rv)
192 {
193 	ACPI_DEVICE_INFO *adi;
194 	int		busnum;
195 
196 	/*
197 	 * Use AcpiGetObjectInfo() to find the device _HID
198 	 * If not a PCI root-bus, ignore this device and continue
199 	 * the walk
200 	 */
201 	if (ACPI_FAILURE(AcpiGetObjectInfo(hdl, &adi)))
202 		return (AE_OK);
203 
204 	if (!(adi->Valid & ACPI_VALID_HID)) {
205 		AcpiOsFree(adi);
206 		return (AE_OK);
207 	}
208 
209 	if (strncmp(adi->HardwareId.String, PCI_ROOT_HID_STRING,
210 	    sizeof (PCI_ROOT_HID_STRING)) &&
211 	    strncmp(adi->HardwareId.String, PCI_EXPRESS_ROOT_HID_STRING,
212 	    sizeof (PCI_EXPRESS_ROOT_HID_STRING))) {
213 		AcpiOsFree(adi);
214 		return (AE_OK);
215 	}
216 
217 	AcpiOsFree(adi);
218 
219 	/*
220 	 * acpica_get_busno() will check the presence of _BBN and
221 	 * fail if not present. It will then use the _CRS method to
222 	 * retrieve the actual bus number assigned, it will fall back
223 	 * to _BBN should the _CRS method fail.
224 	 */
225 	if (ACPI_SUCCESS(acpica_get_busno(hdl, &busnum))) {
226 		/*
227 		 * Ignore invalid _BBN return values here (rather
228 		 * than panic) and emit a warning; something else
229 		 * may suffer failure as a result of the broken BIOS.
230 		 */
231 		if ((busnum < 0) || (busnum > pci_bios_maxbus)) {
232 			dcmn_err(CE_NOTE,
233 			    "pci_process_acpi_device: invalid _BBN 0x%x\n",
234 			    busnum);
235 			return (AE_CTRL_DEPTH);
236 		}
237 
238 		/* PCI with valid _BBN */
239 		if (pci_bus_res[busnum].par_bus == (uchar_t)-1 &&
240 		    pci_bus_res[busnum].dip == NULL)
241 			create_root_bus_dip((uchar_t)busnum);
242 		return (AE_CTRL_DEPTH);
243 	}
244 
245 	/* PCI and no _BBN, continue walk */
246 	return (AE_OK);
247 }
248 
249 /*
250  * Scan the ACPI namespace for all top-level instances of _BBN
251  * in order to discover childless root-bridges (which enumeration
252  * may not find; root-bridges are inferred by the existence of
253  * children).  This scan should find all root-bridges that have
254  * been enumerated, and any childless root-bridges not enumerated.
255  * Root-bridge for bus 0 may not have a _BBN object.
256  */
257 static void
258 pci_scan_bbn()
259 {
260 	void *rv;
261 
262 	(void) AcpiGetDevices(NULL, pci_process_acpi_device, NULL, &rv);
263 }
264 
265 static void
266 pci_unitaddr_cache_init(void)
267 {
268 
269 	puafd_handle = nvf_register_file(&pci_unitaddr_cache_ops);
270 	ASSERT(puafd_handle);
271 
272 	list_create(nvf_list(puafd_handle), sizeof (pua_node_t),
273 	    offsetof(pua_node_t, pua_nodes));
274 
275 	rw_enter(nvf_lock(puafd_handle), RW_WRITER);
276 	(void) nvf_read_file(puafd_handle);
277 	rw_exit(nvf_lock(puafd_handle));
278 }
279 
280 /*
281  * Format of /etc/devices/pci_unitaddr_persistent:
282  *
283  * The persistent record of unit-address assignments contains
284  * a list of name/value pairs, where name is a string representation
285  * of the "index value" of the PCI root-bus and the value is
286  * the assigned unit-address.
287  *
288  * The "index value" is simply the zero-based index of the PCI
289  * root-buses ordered by physical bus number; first PCI bus is 0,
290  * second is 1, and so on.
291  */
292 
293 /*ARGSUSED*/
294 static int
295 pci_cache_unpack_nvlist(nvf_handle_t hdl, nvlist_t *nvl, char *name)
296 {
297 	long		index;
298 	int32_t		value;
299 	nvpair_t	*np;
300 	pua_node_t	*node;
301 
302 	np = NULL;
303 	while ((np = nvlist_next_nvpair(nvl, np)) != NULL) {
304 		/* name of nvpair is index value */
305 		if (ddi_strtol(nvpair_name(np), NULL, 10, &index) != 0)
306 			continue;
307 
308 		if (nvpair_value_int32(np, &value) != 0)
309 			continue;
310 
311 		node = kmem_zalloc(sizeof (pua_node_t), KM_SLEEP);
312 		node->pua_index = index;
313 		node->pua_addr = value;
314 		list_insert_tail(nvf_list(hdl), node);
315 	}
316 
317 	pua_cache_valid = 1;
318 	return (DDI_SUCCESS);
319 }
320 
321 static int
322 pci_cache_pack_nvlist(nvf_handle_t hdl, nvlist_t **ret_nvl)
323 {
324 	int		rval;
325 	nvlist_t	*nvl, *sub_nvl;
326 	list_t		*listp;
327 	pua_node_t	*pua;
328 	char		buf[13];
329 
330 	ASSERT(RW_WRITE_HELD(nvf_lock(hdl)));
331 
332 	rval = nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP);
333 	if (rval != DDI_SUCCESS) {
334 		nvf_error("%s: nvlist alloc error %d\n",
335 		    nvf_cache_name(hdl), rval);
336 		return (DDI_FAILURE);
337 	}
338 
339 	sub_nvl = NULL;
340 	rval = nvlist_alloc(&sub_nvl, NV_UNIQUE_NAME, KM_SLEEP);
341 	if (rval != DDI_SUCCESS)
342 		goto error;
343 
344 	listp = nvf_list(hdl);
345 	for (pua = list_head(listp); pua != NULL;
346 	    pua = list_next(listp, pua)) {
347 		(void) snprintf(buf, sizeof (buf), "%d", pua->pua_index);
348 		rval = nvlist_add_int32(sub_nvl, buf, pua->pua_addr);
349 		if (rval != DDI_SUCCESS)
350 			goto error;
351 	}
352 
353 	rval = nvlist_add_nvlist(nvl, "table", sub_nvl);
354 	if (rval != DDI_SUCCESS)
355 		goto error;
356 	nvlist_free(sub_nvl);
357 
358 	*ret_nvl = nvl;
359 	return (DDI_SUCCESS);
360 
361 error:
362 	nvlist_free(sub_nvl);
363 	ASSERT(nvl);
364 	nvlist_free(nvl);
365 	*ret_nvl = NULL;
366 	return (DDI_FAILURE);
367 }
368 
369 static void
370 pci_cache_free_list(nvf_handle_t hdl)
371 {
372 	list_t		*listp;
373 	pua_node_t	*pua;
374 
375 	ASSERT(RW_WRITE_HELD(nvf_lock(hdl)));
376 
377 	listp = nvf_list(hdl);
378 	for (pua = list_head(listp); pua != NULL;
379 	    pua = list_next(listp, pua)) {
380 		list_remove(listp, pua);
381 		kmem_free(pua, sizeof (pua_node_t));
382 	}
383 }
384 
385 
386 static int
387 pci_unitaddr_cache_valid(void)
388 {
389 
390 	/* read only, no need for rw lock */
391 	return (pua_cache_valid);
392 }
393 
394 
395 static int
396 pci_bus_unitaddr(int index)
397 {
398 	pua_node_t	*pua;
399 	list_t		*listp;
400 	int		addr;
401 
402 	rw_enter(nvf_lock(puafd_handle), RW_READER);
403 
404 	addr = -1;	/* default return if no match */
405 	listp = nvf_list(puafd_handle);
406 	for (pua = list_head(listp); pua != NULL;
407 	    pua = list_next(listp, pua)) {
408 		if (pua->pua_index == index) {
409 			addr = pua->pua_addr;
410 			break;
411 		}
412 	}
413 
414 	rw_exit(nvf_lock(puafd_handle));
415 	return (addr);
416 }
417 
418 static void
419 pci_unitaddr_cache_create(void)
420 {
421 	int		i, index;
422 	pua_node_t	*node;
423 	list_t		*listp;
424 
425 	rw_enter(nvf_lock(puafd_handle), RW_WRITER);
426 
427 	index = 0;
428 	listp = nvf_list(puafd_handle);
429 	for (i = 0; i <= pci_bios_maxbus; i++) {
430 		/* skip non-root (peer) PCI busses */
431 		if ((pci_bus_res[i].par_bus != (uchar_t)-1) ||
432 		    (pci_bus_res[i].dip == NULL))
433 			continue;
434 		node = kmem_zalloc(sizeof (pua_node_t), KM_SLEEP);
435 		node->pua_index = index++;
436 		node->pua_addr = pci_bus_res[i].root_addr;
437 		list_insert_tail(listp, node);
438 	}
439 
440 	(void) nvf_mark_dirty(puafd_handle);
441 	rw_exit(nvf_lock(puafd_handle));
442 	nvf_wake_daemon();
443 }
444 
445 
446 /*
447  * Enumerate all PCI devices
448  */
449 void
450 pci_setup_tree(void)
451 {
452 	uint_t i, root_bus_addr = 0;
453 
454 	alloc_res_array();
455 	for (i = 0; i <= pci_bios_maxbus; i++) {
456 		pci_bus_res[i].par_bus = (uchar_t)-1;
457 		pci_bus_res[i].root_addr = (uchar_t)-1;
458 		pci_bus_res[i].sub_bus = i;
459 	}
460 
461 	pci_bus_res[0].root_addr = root_bus_addr++;
462 	create_root_bus_dip(0);
463 	enumerate_bus_devs(0, CONFIG_INFO);
464 
465 	/*
466 	 * Now enumerate peer busses
467 	 *
468 	 * We loop till pci_bios_maxbus. On most systems, there is
469 	 * one more bus at the high end, which implements the ISA
470 	 * compatibility bus. We don't care about that.
471 	 *
472 	 * Note: In the old (bootconf) enumeration, the peer bus
473 	 *	address did not use the bus number, and there were
474 	 *	too many peer busses created. The root_bus_addr is
475 	 *	used to maintain the old peer bus address assignment.
476 	 *	However, we stop enumerating phantom peers with no
477 	 *	device below.
478 	 */
479 	for (i = 1; i <= pci_bios_maxbus; i++) {
480 		if (pci_bus_res[i].dip == NULL) {
481 			pci_bus_res[i].root_addr = root_bus_addr++;
482 		}
483 		enumerate_bus_devs(i, CONFIG_INFO);
484 
485 		/* add slot-names property for named pci hot-plug slots */
486 		add_bus_slot_names_prop(i);
487 	}
488 }
489 
490 /*
491  * >0 = present, 0 = not present, <0 = error
492  */
493 static int
494 pci_bbn_present(int bus)
495 {
496 	ACPI_HANDLE	hdl;
497 	int	rv;
498 
499 	/* no dip means no _BBN */
500 	if (pci_bus_res[bus].dip == NULL)
501 		return (0);
502 
503 	rv = -1;	/* default return value in case of error below */
504 	if (ACPI_SUCCESS(acpica_get_handle(pci_bus_res[bus].dip, &hdl))) {
505 		switch (AcpiEvaluateObject(hdl, "_BBN", NULL, NULL)) {
506 		case AE_OK:
507 			rv = 1;
508 			break;
509 		case AE_NOT_FOUND:
510 			rv = 0;
511 			break;
512 		default:
513 			break;
514 		}
515 	}
516 
517 	return (rv);
518 }
519 
520 /*
521  * Return non-zero if any PCI bus in the system has an associated
522  * _BBN object, 0 otherwise.
523  */
524 static int
525 pci_roots_have_bbn(void)
526 {
527 	int	i;
528 
529 	/*
530 	 * Scan the PCI busses and look for at least 1 _BBN
531 	 */
532 	for (i = 0; i <= pci_bios_maxbus; i++) {
533 		/* skip non-root (peer) PCI busses */
534 		if (pci_bus_res[i].par_bus != (uchar_t)-1)
535 			continue;
536 
537 		if (pci_bbn_present(i) > 0)
538 			return (1);
539 	}
540 	return (0);
541 
542 }
543 
544 /*
545  * return non-zero if the machine is one on which we renumber
546  * the internal pci unit-addresses
547  */
548 static int
549 pci_bus_renumber()
550 {
551 	ACPI_TABLE_HEADER *fadt;
552 
553 	if (pci_bus_always_renumber)
554 		return (1);
555 
556 	/* get the FADT */
557 	if (AcpiGetTable(ACPI_SIG_FADT, 1, (ACPI_TABLE_HEADER **)&fadt) !=
558 	    AE_OK)
559 		return (0);
560 
561 	/* compare OEM Table ID to "SUNm31" */
562 	if (strncmp("SUNm31", fadt->OemId, 6))
563 		return (0);
564 	else
565 		return (1);
566 }
567 
568 /*
569  * Initial enumeration of the physical PCI bus hierarchy can
570  * leave 'gaps' in the order of peer PCI bus unit-addresses.
571  * Systems with more than one peer PCI bus *must* have an ACPI
572  * _BBN object associated with each peer bus; use the presence
573  * of this object to remove gaps in the numbering of the peer
574  * PCI bus unit-addresses - only peer busses with an associated
575  * _BBN are counted.
576  */
577 static void
578 pci_renumber_root_busses(void)
579 {
580 	int pci_regs[] = {0, 0, 0};
581 	int	i, root_addr = 0;
582 
583 	/*
584 	 * Currently, we only enable the re-numbering on specific
585 	 * Sun machines; this is a work-around for the more complicated
586 	 * issue of upgrade changing physical device paths
587 	 */
588 	if (!pci_bus_renumber())
589 		return;
590 
591 	/*
592 	 * If we find no _BBN objects at all, we either don't need
593 	 * to do anything or can't do anything anyway
594 	 */
595 	if (!pci_roots_have_bbn())
596 		return;
597 
598 	for (i = 0; i <= pci_bios_maxbus; i++) {
599 		/* skip non-root (peer) PCI busses */
600 		if (pci_bus_res[i].par_bus != (uchar_t)-1)
601 			continue;
602 
603 		if (pci_bbn_present(i) < 1) {
604 			pci_bus_res[i].root_addr = (uchar_t)-1;
605 			continue;
606 		}
607 
608 		ASSERT(pci_bus_res[i].dip != NULL);
609 		if (pci_bus_res[i].root_addr != root_addr) {
610 			/* update reg property for node */
611 			pci_bus_res[i].root_addr = root_addr;
612 			pci_regs[0] = pci_bus_res[i].root_addr;
613 			(void) ndi_prop_update_int_array(DDI_DEV_T_NONE,
614 			    pci_bus_res[i].dip, "reg", (int *)pci_regs, 3);
615 		}
616 		root_addr++;
617 	}
618 }
619 
620 void
621 pci_register_isa_resources(int type, uint32_t base, uint32_t size)
622 {
623 	(void) memlist_insert(
624 	    (type == 1) ?  &isa_res.io_used : &isa_res.mem_used,
625 	    base, size);
626 }
627 
628 /*
629  * Remove the resources which are already used by devices under a subtractive
630  * bridge from the bus's resources lists, because they're not available, and
631  * shouldn't be allocated to other buses.  This is necessary because tracking
632  * resources for subtractive bridges is not complete.  (Subtractive bridges only
633  * track some of their claimed resources, not "the rest of the address space" as
634  * they should, so that allocation to peer non-subtractive PPBs is easier.  We
635  * need a fully-capable global resource allocator).
636  */
637 static void
638 remove_subtractive_res()
639 {
640 	int i, j;
641 	struct memlist *list;
642 
643 	for (i = 0; i <= pci_bios_maxbus; i++) {
644 		if (pci_bus_res[i].subtractive) {
645 			/* remove used io ports */
646 			list = pci_bus_res[i].io_used;
647 			while (list) {
648 				for (j = 0; j <= pci_bios_maxbus; j++)
649 					(void) memlist_remove(
650 					    &pci_bus_res[j].io_avail,
651 					    list->ml_address, list->ml_size);
652 				list = list->ml_next;
653 			}
654 			/* remove used mem resource */
655 			list = pci_bus_res[i].mem_used;
656 			while (list) {
657 				for (j = 0; j <= pci_bios_maxbus; j++) {
658 					(void) memlist_remove(
659 					    &pci_bus_res[j].mem_avail,
660 					    list->ml_address, list->ml_size);
661 					(void) memlist_remove(
662 					    &pci_bus_res[j].pmem_avail,
663 					    list->ml_address, list->ml_size);
664 				}
665 				list = list->ml_next;
666 			}
667 			/* remove used prefetchable mem resource */
668 			list = pci_bus_res[i].pmem_used;
669 			while (list) {
670 				for (j = 0; j <= pci_bios_maxbus; j++) {
671 					(void) memlist_remove(
672 					    &pci_bus_res[j].pmem_avail,
673 					    list->ml_address, list->ml_size);
674 					(void) memlist_remove(
675 					    &pci_bus_res[j].mem_avail,
676 					    list->ml_address, list->ml_size);
677 				}
678 				list = list->ml_next;
679 			}
680 		}
681 	}
682 }
683 
684 /*
685  * Set up (or complete the setup of) the bus_avail resource list
686  */
687 static void
688 setup_bus_res(int bus)
689 {
690 	uchar_t par_bus;
691 
692 	if (pci_bus_res[bus].dip == NULL)	/* unused bus */
693 		return;
694 
695 	/*
696 	 * Set up bus_avail if not already filled in by populate_bus_res()
697 	 */
698 	if (pci_bus_res[bus].bus_avail == NULL) {
699 		ASSERT(pci_bus_res[bus].sub_bus >= bus);
700 		memlist_insert(&pci_bus_res[bus].bus_avail, bus,
701 		    pci_bus_res[bus].sub_bus - bus + 1);
702 	}
703 
704 	ASSERT(pci_bus_res[bus].bus_avail != NULL);
705 
706 	/*
707 	 * Remove resources from parent bus node if this is not a
708 	 * root bus.
709 	 */
710 	par_bus = pci_bus_res[bus].par_bus;
711 	if (par_bus != (uchar_t)-1) {
712 		ASSERT(pci_bus_res[par_bus].bus_avail != NULL);
713 		memlist_remove_list(&pci_bus_res[par_bus].bus_avail,
714 		    pci_bus_res[bus].bus_avail);
715 	}
716 
717 	/* remove self from bus_avail */;
718 	(void) memlist_remove(&pci_bus_res[bus].bus_avail, bus, 1);
719 }
720 
721 static uint64_t
722 get_parbus_io_res(uchar_t parbus, uchar_t bus, uint64_t size, uint64_t align)
723 {
724 	uint64_t addr = 0;
725 	uchar_t res_bus;
726 
727 	/*
728 	 * Skip root(peer) buses in multiple-root-bus systems when
729 	 * ACPI resource discovery was not successfully done.
730 	 */
731 	if ((pci_bus_res[parbus].par_bus == (uchar_t)-1) &&
732 	    (num_root_bus > 1) && (acpi_resource_discovery <= 0))
733 		return (0);
734 
735 	res_bus = parbus;
736 	while (pci_bus_res[res_bus].subtractive) {
737 		if (pci_bus_res[res_bus].io_avail)
738 			break;
739 		res_bus = pci_bus_res[res_bus].par_bus;
740 		if (res_bus == (uchar_t)-1)
741 			break; /* root bus already */
742 	}
743 
744 	if (pci_bus_res[res_bus].io_avail) {
745 		addr = memlist_find(&pci_bus_res[res_bus].io_avail,
746 		    size, align);
747 		if (addr) {
748 			memlist_insert(&pci_bus_res[res_bus].io_used,
749 			    addr, size);
750 
751 			/* free the old resource */
752 			memlist_free_all(&pci_bus_res[bus].io_avail);
753 			memlist_free_all(&pci_bus_res[bus].io_used);
754 
755 			/* add the new resource */
756 			memlist_insert(&pci_bus_res[bus].io_avail, addr, size);
757 		}
758 	}
759 
760 	return (addr);
761 }
762 
763 static uint64_t
764 get_parbus_mem_res(uchar_t parbus, uchar_t bus, uint64_t size, uint64_t align)
765 {
766 	uint64_t addr = 0;
767 	uchar_t res_bus;
768 
769 	/*
770 	 * Skip root(peer) buses in multiple-root-bus systems when
771 	 * ACPI resource discovery was not successfully done.
772 	 */
773 	if ((pci_bus_res[parbus].par_bus == (uchar_t)-1) &&
774 	    (num_root_bus > 1) && (acpi_resource_discovery <= 0))
775 		return (0);
776 
777 	res_bus = parbus;
778 	while (pci_bus_res[res_bus].subtractive) {
779 		if (pci_bus_res[res_bus].mem_avail)
780 			break;
781 		res_bus = pci_bus_res[res_bus].par_bus;
782 		if (res_bus == (uchar_t)-1)
783 			break; /* root bus already */
784 	}
785 
786 	if (pci_bus_res[res_bus].mem_avail) {
787 		addr = memlist_find(&pci_bus_res[res_bus].mem_avail,
788 		    size, align);
789 		if (addr) {
790 			memlist_insert(&pci_bus_res[res_bus].mem_used,
791 			    addr, size);
792 			(void) memlist_remove(&pci_bus_res[res_bus].pmem_avail,
793 			    addr, size);
794 
795 			/* free the old resource */
796 			memlist_free_all(&pci_bus_res[bus].mem_avail);
797 			memlist_free_all(&pci_bus_res[bus].mem_used);
798 
799 			/* add the new resource */
800 			memlist_insert(&pci_bus_res[bus].mem_avail, addr, size);
801 		}
802 	}
803 
804 	return (addr);
805 }
806 
807 /*
808  * given a cap_id, return its cap_id location in config space
809  */
810 static int
811 get_pci_cap(uchar_t bus, uchar_t dev, uchar_t func, uint8_t cap_id)
812 {
813 	uint8_t curcap, cap_id_loc;
814 	uint16_t status;
815 	int location = -1;
816 
817 	/*
818 	 * Need to check the Status register for ECP support first.
819 	 * Also please note that for type 1 devices, the
820 	 * offset could change. Should support type 1 next.
821 	 */
822 	status = pci_getw(bus, dev, func, PCI_CONF_STAT);
823 	if (!(status & PCI_STAT_CAP)) {
824 		return (-1);
825 	}
826 	cap_id_loc = pci_getb(bus, dev, func, PCI_CONF_CAP_PTR);
827 
828 	/* Walk the list of capabilities */
829 	while (cap_id_loc && cap_id_loc != (uint8_t)-1) {
830 		curcap = pci_getb(bus, dev, func, cap_id_loc);
831 
832 		if (curcap == cap_id) {
833 			location = cap_id_loc;
834 			break;
835 		}
836 		cap_id_loc = pci_getb(bus, dev, func, cap_id_loc + 1);
837 	}
838 	return (location);
839 }
840 
841 /*
842  * Does this resource element live in the legacy VGA range?
843  */
844 
845 int
846 is_vga(struct memlist *elem, enum io_mem io)
847 {
848 
849 	if (io == IO) {
850 		if ((elem->ml_address == 0x3b0 && elem->ml_size == 0xc) ||
851 		    (elem->ml_address == 0x3c0 && elem->ml_size == 0x20))
852 			return (1);
853 	} else {
854 		if (elem->ml_address == 0xa0000 && elem->ml_size == 0x20000)
855 			return (1);
856 	}
857 	return (0);
858 }
859 
860 /*
861  * Does this entire resource list consist only of legacy VGA resources?
862  */
863 
864 int
865 list_is_vga_only(struct memlist *l, enum io_mem io)
866 {
867 	do {
868 		if (!is_vga(l, io))
869 			return (0);
870 	} while ((l = l->ml_next) != NULL);
871 	return (1);
872 }
873 
874 /*
875  * Assign valid resources to unconfigured pci(e) bridges. We are trying
876  * to reprogram the bridge when its
877  *		i)   SECBUS == SUBBUS	||
878  *		ii)  IOBASE > IOLIM	||
879  *		iii) MEMBASE > MEMLIM
880  * This must be done after one full pass through the PCI tree to collect
881  * all BIOS-configured resources, so that we know what resources are
882  * free and available to assign to the unconfigured PPBs.
883  */
884 static void
885 fix_ppb_res(uchar_t secbus, boolean_t prog_sub)
886 {
887 	uchar_t bus, dev, func;
888 	uchar_t parbus, subbus;
889 	uint_t io_base, io_limit, mem_base;
890 	uint_t io_size, io_align;
891 	uint64_t mem_size, mem_align, mem_limit;
892 	uint64_t addr = 0;
893 	int *regp = NULL;
894 	uint_t reglen;
895 	int rv, cap_ptr, physhi;
896 	dev_info_t *dip;
897 	uint16_t cmd_reg;
898 	struct memlist *list, *scratch_list;
899 
900 	/* skip root (peer) PCI busses */
901 	if (pci_bus_res[secbus].par_bus == (uchar_t)-1)
902 		return;
903 
904 	/* skip subtractive PPB when prog_sub is not TRUE */
905 	if (pci_bus_res[secbus].subtractive && !prog_sub)
906 		return;
907 
908 	/* some entries may be empty due to discontiguous bus numbering */
909 	dip = pci_bus_res[secbus].dip;
910 	if (dip == NULL)
911 		return;
912 
913 	rv = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
914 	    "reg", &regp, &reglen);
915 	if (rv != DDI_PROP_SUCCESS || reglen == 0)
916 		return;
917 	physhi = regp[0];
918 	ddi_prop_free(regp);
919 
920 	func = (uchar_t)PCI_REG_FUNC_G(physhi);
921 	dev = (uchar_t)PCI_REG_DEV_G(physhi);
922 	bus = (uchar_t)PCI_REG_BUS_G(physhi);
923 
924 	/*
925 	 * If pcie bridge, check to see if link is enabled
926 	 */
927 	cap_ptr = get_pci_cap(bus, dev, func, PCI_CAP_ID_PCI_E);
928 	if (cap_ptr != -1) {
929 		cmd_reg = pci_getw(bus, dev, func,
930 		    (uint16_t)cap_ptr + PCIE_LINKCTL);
931 		if (cmd_reg & PCIE_LINKCTL_LINK_DISABLE) {
932 			dcmn_err(CE_NOTE,
933 			    "!fix_ppb_res: ppb[%x/%x/%x] link is disabled.\n",
934 			    bus, dev, func);
935 			return;
936 		}
937 	}
938 
939 	subbus = pci_getb(bus, dev, func, PCI_BCNF_SUBBUS);
940 	parbus = pci_bus_res[secbus].par_bus;
941 	ASSERT(parbus == bus);
942 	cmd_reg = pci_getw(bus, dev, func, PCI_CONF_COMM);
943 
944 	/*
945 	 * If we have a Cardbus bridge, but no bus space
946 	 */
947 	if (pci_bus_res[secbus].num_cbb != 0 &&
948 	    pci_bus_res[secbus].bus_avail == NULL) {
949 		uchar_t range;
950 
951 		/* normally there are 2 buses under a cardbus bridge */
952 		range = pci_bus_res[secbus].num_cbb * 2;
953 
954 		/*
955 		 * Try to find and allocate a bus-range starting at subbus+1
956 		 * from the parent of the PPB.
957 		 */
958 		for (; range != 0; range--) {
959 			if (memlist_find_with_startaddr(
960 			    &pci_bus_res[parbus].bus_avail,
961 			    subbus + 1, range, 1) != 0)
962 				break; /* find bus range resource at parent */
963 		}
964 		if (range != 0) {
965 			memlist_insert(&pci_bus_res[secbus].bus_avail,
966 			    subbus + 1, range);
967 			subbus = subbus + range;
968 			pci_bus_res[secbus].sub_bus = subbus;
969 			pci_putb(bus, dev, func, PCI_BCNF_SUBBUS, subbus);
970 			add_bus_range_prop(secbus);
971 
972 			cmn_err(CE_NOTE, "!reprogram bus-range on ppb"
973 			    "[%x/%x/%x]: %x ~ %x\n", bus, dev, func,
974 			    secbus, subbus);
975 		}
976 	}
977 
978 	/*
979 	 * Calculate required IO size and alignment
980 	 * If bus io_size is zero, we are going to assign 512 bytes per bus,
981 	 * otherwise, we'll choose the maximum value of such calculation and
982 	 * bus io_size. The size needs to be 4K aligned.
983 	 *
984 	 * We calculate alignment as the largest power of two less than the
985 	 * the sum of all children's IO size requirements, because this will
986 	 * align to the size of the largest child request within that size
987 	 * (which is always a power of two).
988 	 */
989 	io_size = (subbus - secbus + 1) * 0x200;
990 	if (io_size <  pci_bus_res[secbus].io_size)
991 		io_size = pci_bus_res[secbus].io_size;
992 	io_size = P2ROUNDUP(io_size, PPB_IO_ALIGNMENT);
993 	io_align = io_size;
994 	P2LE(io_align);
995 
996 	/*
997 	 * Calculate required MEM size and alignment
998 	 * If bus mem_size is zero, we are going to assign 1M bytes per bus,
999 	 * otherwise, we'll choose the maximum value of such calculation and
1000 	 * bus mem_size. The size needs to be 1M aligned.
1001 	 *
1002 	 * For the alignment, refer to the I/O comment above.
1003 	 */
1004 	mem_size = (subbus - secbus + 1) * PPB_MEM_ALIGNMENT;
1005 	if (mem_size < pci_bus_res[secbus].mem_size) {
1006 		mem_size = pci_bus_res[secbus].mem_size;
1007 		mem_size = P2ROUNDUP(mem_size, PPB_MEM_ALIGNMENT);
1008 	}
1009 	mem_align = mem_size;
1010 	P2LE(mem_align);
1011 
1012 	/* Subtractive bridge */
1013 	if (pci_bus_res[secbus].subtractive && prog_sub) {
1014 		/*
1015 		 * We program an arbitrary amount of I/O and memory resource
1016 		 * for the subtractive bridge so that child dynamic-resource-
1017 		 * allocating devices (such as Cardbus bridges) have a chance
1018 		 * of success.  Until we have full-tree resource rebalancing,
1019 		 * dynamic resource allocation (thru busra) only looks at the
1020 		 * parent bridge, so all PPBs must have some allocatable
1021 		 * resource.  For non-subtractive bridges, the resources come
1022 		 * from the base/limit register "windows", but subtractive
1023 		 * bridges often don't program those (since they don't need to).
1024 		 * If we put all the remaining resources on the subtractive
1025 		 * bridge, then peer non-subtractive bridges can't allocate
1026 		 * more space (even though this is probably most correct).
1027 		 * If we put the resources only on the parent, then allocations
1028 		 * from children of subtractive bridges will fail without
1029 		 * special-case code for bypassing the subtractive bridge.
1030 		 * This solution is the middle-ground temporary solution until
1031 		 * we have fully-capable resource allocation.
1032 		 */
1033 
1034 		/*
1035 		 * Add an arbitrary I/O resource to the subtractive PPB
1036 		 */
1037 		if (pci_bus_res[secbus].io_avail == NULL) {
1038 			addr = get_parbus_io_res(parbus, secbus, io_size,
1039 			    io_align);
1040 			if (addr) {
1041 				add_ranges_prop(secbus, 1);
1042 				pci_bus_res[secbus].io_reprogram =
1043 				    pci_bus_res[parbus].io_reprogram;
1044 
1045 				cmn_err(CE_NOTE, "!add io-range on subtractive"
1046 				    " ppb[%x/%x/%x]: "
1047 				    "0x%"PRIx64" ~ 0x%"PRIx64"\n",
1048 				    bus, dev, func, addr, addr + io_size - 1);
1049 			}
1050 		}
1051 		/*
1052 		 * Add an arbitrary memory resource to the subtractive PPB
1053 		 */
1054 		if (pci_bus_res[secbus].mem_avail == NULL) {
1055 			addr = get_parbus_mem_res(parbus, secbus, mem_size,
1056 			    mem_align);
1057 			if (addr) {
1058 				add_ranges_prop(secbus, 1);
1059 				pci_bus_res[secbus].mem_reprogram =
1060 				    pci_bus_res[parbus].mem_reprogram;
1061 
1062 				cmn_err(CE_NOTE, "!add mem-range on "
1063 				    "subtractive ppb[%x/%x/%x]: "
1064 				    "0x%"PRIx64" ~ 0x%"PRIx64"\n",
1065 				    bus, dev, func,
1066 				    addr, addr + mem_size - 1);
1067 			}
1068 		}
1069 
1070 		goto cmd_enable;
1071 	}
1072 
1073 	/*
1074 	 * Check to see if we need to reprogram I/O space, either because the
1075 	 * parent bus needed reprogramming and so do we, or because I/O space is
1076 	 * disabled in base/limit or command register.
1077 	 */
1078 	io_base = pci_getb(bus, dev, func, PCI_BCNF_IO_BASE_LOW);
1079 	io_limit = pci_getb(bus, dev, func, PCI_BCNF_IO_LIMIT_LOW);
1080 	io_base = (io_base & PCI_BCNF_IO_MASK) << PCI_BCNF_IO_SHIFT;
1081 	io_limit = ((io_limit & PCI_BCNF_IO_MASK) << PCI_BCNF_IO_SHIFT) | 0xfff;
1082 	if ((io_base & PCI_BCNF_ADDR_MASK) == PCI_BCNF_IO_32BIT) {
1083 		uint16_t io_base_hi, io_limit_hi;
1084 		io_base_hi = pci_getw(bus, dev, func, PCI_BCNF_IO_BASE_HI);
1085 		io_limit_hi = pci_getw(bus, dev, func, PCI_BCNF_IO_LIMIT_HI);
1086 
1087 		io_base |= (uint_t)io_base_hi << 16;
1088 		io_limit |= (uint_t)io_limit_hi << 16;
1089 	}
1090 
1091 	/* Form list of all resources passed (avail + used) */
1092 	scratch_list = memlist_dup(pci_bus_res[secbus].io_avail);
1093 	memlist_merge(&pci_bus_res[secbus].io_used, &scratch_list);
1094 
1095 	if ((pci_bus_res[parbus].io_reprogram ||
1096 	    (io_base > io_limit) ||
1097 	    (!(cmd_reg & PCI_COMM_IO))) &&
1098 	    !list_is_vga_only(scratch_list, IO)) {
1099 		if (pci_bus_res[secbus].io_used) {
1100 			memlist_subsume(&pci_bus_res[secbus].io_used,
1101 			    &pci_bus_res[secbus].io_avail);
1102 		}
1103 		if (pci_bus_res[secbus].io_avail &&
1104 		    (!pci_bus_res[parbus].io_reprogram) &&
1105 		    (!pci_bus_res[parbus].subtractive)) {
1106 			/* rechoose old io ports info */
1107 			list = pci_bus_res[secbus].io_avail;
1108 			io_base = 0;
1109 			do {
1110 				if (is_vga(list, IO))
1111 					continue;
1112 				if (!io_base) {
1113 					io_base = (uint_t)list->ml_address;
1114 					io_limit = (uint_t)list->ml_address +
1115 					    list->ml_size - 1;
1116 					io_base =
1117 					    P2ALIGN(io_base, PPB_IO_ALIGNMENT);
1118 				} else {
1119 					if (list->ml_address + list->ml_size >
1120 					    io_limit) {
1121 						io_limit = (uint_t)
1122 						    (list->ml_address +
1123 						    list->ml_size - 1);
1124 					}
1125 				}
1126 			} while ((list = list->ml_next) != NULL);
1127 			/* 4K aligned */
1128 			io_limit = P2ROUNDUP(io_limit, PPB_IO_ALIGNMENT) - 1;
1129 			io_size = io_limit - io_base + 1;
1130 			ASSERT(io_base <= io_limit);
1131 			memlist_free_all(&pci_bus_res[secbus].io_avail);
1132 			memlist_insert(&pci_bus_res[secbus].io_avail,
1133 			    io_base, io_size);
1134 			memlist_insert(&pci_bus_res[parbus].io_used,
1135 			    io_base, io_size);
1136 			(void) memlist_remove(&pci_bus_res[parbus].io_avail,
1137 			    io_base, io_size);
1138 			pci_bus_res[secbus].io_reprogram = B_TRUE;
1139 		} else {
1140 			/* get new io ports from parent bus */
1141 			addr = get_parbus_io_res(parbus, secbus, io_size,
1142 			    io_align);
1143 			if (addr) {
1144 				io_base = addr;
1145 				io_limit = addr + io_size - 1;
1146 				pci_bus_res[secbus].io_reprogram = B_TRUE;
1147 			}
1148 		}
1149 		if (pci_bus_res[secbus].io_reprogram) {
1150 			/* reprogram PPB regs */
1151 			pci_putb(bus, dev, func, PCI_BCNF_IO_BASE_LOW,
1152 			    (uchar_t)((io_base>>8) & 0xf0));
1153 			pci_putb(bus, dev, func, PCI_BCNF_IO_LIMIT_LOW,
1154 			    (uchar_t)((io_limit>>8) & 0xf0));
1155 			pci_putb(bus, dev, func, PCI_BCNF_IO_BASE_HI, 0);
1156 			pci_putb(bus, dev, func, PCI_BCNF_IO_LIMIT_HI, 0);
1157 			add_ranges_prop(secbus, 1);
1158 
1159 			cmn_err(CE_NOTE, "!reprogram io-range on"
1160 			    " ppb[%x/%x/%x]: 0x%x ~ 0x%x\n",
1161 			    bus, dev, func, io_base, io_limit);
1162 		}
1163 	}
1164 	memlist_free_all(&scratch_list);
1165 
1166 	/*
1167 	 * Check memory space as we did I/O space.
1168 	 */
1169 	mem_base = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_MEM_BASE);
1170 	mem_base = (mem_base & 0xfff0) << 16;
1171 	mem_limit = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_MEM_LIMIT);
1172 	mem_limit = ((mem_limit & 0xfff0) << 16) | 0xfffff;
1173 
1174 	scratch_list = memlist_dup(pci_bus_res[secbus].mem_avail);
1175 	memlist_merge(&pci_bus_res[secbus].mem_used, &scratch_list);
1176 
1177 	if ((pci_bus_res[parbus].mem_reprogram ||
1178 	    (mem_base > mem_limit) ||
1179 	    (!(cmd_reg & PCI_COMM_MAE))) &&
1180 	    !list_is_vga_only(scratch_list, MEM)) {
1181 		if (pci_bus_res[secbus].mem_used) {
1182 			memlist_subsume(&pci_bus_res[secbus].mem_used,
1183 			    &pci_bus_res[secbus].mem_avail);
1184 		}
1185 		if (pci_bus_res[secbus].mem_avail &&
1186 		    (!pci_bus_res[parbus].mem_reprogram) &&
1187 		    (!pci_bus_res[parbus].subtractive)) {
1188 			/* rechoose old mem resource */
1189 			list = pci_bus_res[secbus].mem_avail;
1190 			mem_base = 0;
1191 			do {
1192 				if (is_vga(list, MEM))
1193 					continue;
1194 				if (mem_base == 0) {
1195 					mem_base = list->ml_address;
1196 					mem_base = P2ALIGN(mem_base,
1197 					    PPB_MEM_ALIGNMENT);
1198 					mem_limit = (list->ml_address +
1199 					    list->ml_size - 1);
1200 				} else {
1201 					if ((list->ml_address + list->ml_size) >
1202 					    mem_limit) {
1203 						mem_limit =
1204 						    (list->ml_address +
1205 						    list->ml_size - 1);
1206 					}
1207 				}
1208 			} while ((list = list->ml_next) != NULL);
1209 			mem_limit = P2ROUNDUP(mem_limit, PPB_MEM_ALIGNMENT) - 1;
1210 			mem_size = mem_limit + 1 - mem_base;
1211 			ASSERT(mem_base <= mem_limit);
1212 			memlist_free_all(&pci_bus_res[secbus].mem_avail);
1213 			memlist_insert(&pci_bus_res[secbus].mem_avail,
1214 			    mem_base, mem_size);
1215 			memlist_insert(&pci_bus_res[parbus].mem_used,
1216 			    mem_base, mem_size);
1217 			(void) memlist_remove(&pci_bus_res[parbus].mem_avail,
1218 			    mem_base, mem_size);
1219 			pci_bus_res[secbus].mem_reprogram = B_TRUE;
1220 		} else {
1221 			/* get new mem resource from parent bus */
1222 			addr = get_parbus_mem_res(parbus, secbus, mem_size,
1223 			    mem_align);
1224 			if (addr) {
1225 				mem_base = addr;
1226 				mem_limit = addr + mem_size - 1;
1227 				pci_bus_res[secbus].mem_reprogram = B_TRUE;
1228 			}
1229 		}
1230 
1231 		if (pci_bus_res[secbus].mem_reprogram) {
1232 			/* reprogram PPB MEM regs */
1233 			pci_putw(bus, dev, func, PCI_BCNF_MEM_BASE,
1234 			    (uint16_t)((mem_base>>16) & 0xfff0));
1235 			pci_putw(bus, dev, func, PCI_BCNF_MEM_LIMIT,
1236 			    (uint16_t)((mem_limit>>16) & 0xfff0));
1237 			/*
1238 			 * Disable PMEM window by setting base > limit.
1239 			 * We currently don't reprogram the PMEM like we've
1240 			 * done for I/O and MEM. (Devices that support prefetch
1241 			 * can use non-prefetch MEM.) Anyway, if the MEM access
1242 			 * bit is initially disabled by BIOS, we disable the
1243 			 * PMEM window manually by setting PMEM base > PMEM
1244 			 * limit here, in case there are incorrect values in
1245 			 * them from BIOS, so that we won't get in trouble once
1246 			 * the MEM access bit is enabled at the end of this
1247 			 * function.
1248 			 */
1249 			if (!(cmd_reg & PCI_COMM_MAE)) {
1250 				pci_putw(bus, dev, func, PCI_BCNF_PF_BASE_LOW,
1251 				    0xfff0);
1252 				pci_putw(bus, dev, func, PCI_BCNF_PF_LIMIT_LOW,
1253 				    0x0);
1254 				pci_putl(bus, dev, func, PCI_BCNF_PF_BASE_HIGH,
1255 				    0xffffffff);
1256 				pci_putl(bus, dev, func, PCI_BCNF_PF_LIMIT_HIGH,
1257 				    0x0);
1258 			}
1259 
1260 			add_ranges_prop(secbus, 1);
1261 
1262 			cmn_err(CE_NOTE, "!reprogram mem-range on"
1263 			    " ppb[%x/%x/%x]: 0x%x ~ 0x%"PRIx64"\n",
1264 			    bus, dev, func, mem_base, mem_limit);
1265 		}
1266 	}
1267 	memlist_free_all(&scratch_list);
1268 
1269 cmd_enable:
1270 	if (pci_bus_res[secbus].io_avail)
1271 		cmd_reg |= PCI_COMM_IO | PCI_COMM_ME;
1272 	if (pci_bus_res[secbus].mem_avail)
1273 		cmd_reg |= PCI_COMM_MAE | PCI_COMM_ME;
1274 	pci_putw(bus, dev, func, PCI_CONF_COMM, cmd_reg);
1275 }
1276 
1277 void
1278 pci_reprogram(void)
1279 {
1280 	int i, pci_reconfig = 1;
1281 	char *onoff;
1282 	int bus;
1283 
1284 	/*
1285 	 * Scan ACPI namespace for _BBN objects, make sure that
1286 	 * childless root-bridges appear in devinfo tree
1287 	 */
1288 	pci_scan_bbn();
1289 	pci_unitaddr_cache_init();
1290 
1291 	/*
1292 	 * Fix-up unit-address assignments if cache is available
1293 	 */
1294 	if (pci_unitaddr_cache_valid()) {
1295 		int pci_regs[] = {0, 0, 0};
1296 		int	new_addr;
1297 		int	index = 0;
1298 
1299 		for (bus = 0; bus <= pci_bios_maxbus; bus++) {
1300 			/* skip non-root (peer) PCI busses */
1301 			if ((pci_bus_res[bus].par_bus != (uchar_t)-1) ||
1302 			    (pci_bus_res[bus].dip == NULL))
1303 				continue;
1304 
1305 			new_addr = pci_bus_unitaddr(index);
1306 			if (pci_bus_res[bus].root_addr != new_addr) {
1307 				/* update reg property for node */
1308 				pci_regs[0] = pci_bus_res[bus].root_addr =
1309 				    new_addr;
1310 				(void) ndi_prop_update_int_array(
1311 				    DDI_DEV_T_NONE, pci_bus_res[bus].dip,
1312 				    "reg", (int *)pci_regs, 3);
1313 			}
1314 			index++;
1315 		}
1316 	} else {
1317 		/* perform legacy processing */
1318 		pci_renumber_root_busses();
1319 		pci_unitaddr_cache_create();
1320 	}
1321 
1322 	/*
1323 	 * Do root-bus resource discovery
1324 	 */
1325 	for (bus = 0; bus <= pci_bios_maxbus; bus++) {
1326 		/* skip non-root (peer) PCI busses */
1327 		if (pci_bus_res[bus].par_bus != (uchar_t)-1)
1328 			continue;
1329 
1330 		/*
1331 		 * 1. find resources associated with this root bus
1332 		 */
1333 		populate_bus_res(bus);
1334 
1335 
1336 		/*
1337 		 * 2. Remove used PCI and ISA resources from bus resource map
1338 		 */
1339 
1340 		memlist_remove_list(&pci_bus_res[bus].io_avail,
1341 		    pci_bus_res[bus].io_used);
1342 		memlist_remove_list(&pci_bus_res[bus].mem_avail,
1343 		    pci_bus_res[bus].mem_used);
1344 		memlist_remove_list(&pci_bus_res[bus].pmem_avail,
1345 		    pci_bus_res[bus].pmem_used);
1346 		memlist_remove_list(&pci_bus_res[bus].mem_avail,
1347 		    pci_bus_res[bus].pmem_used);
1348 		memlist_remove_list(&pci_bus_res[bus].pmem_avail,
1349 		    pci_bus_res[bus].mem_used);
1350 
1351 		memlist_remove_list(&pci_bus_res[bus].io_avail,
1352 		    isa_res.io_used);
1353 		memlist_remove_list(&pci_bus_res[bus].mem_avail,
1354 		    isa_res.mem_used);
1355 
1356 		/*
1357 		 * 3. Exclude <1M address range here in case below reserved
1358 		 * ranges for BIOS data area, ROM area etc are wrongly reported
1359 		 * in ACPI resource producer entries for PCI root bus.
1360 		 *	00000000 - 000003FF	RAM
1361 		 *	00000400 - 000004FF	BIOS data area
1362 		 *	00000500 - 0009FFFF	RAM
1363 		 *	000A0000 - 000BFFFF	VGA RAM
1364 		 *	000C0000 - 000FFFFF	ROM area
1365 		 */
1366 		(void) memlist_remove(&pci_bus_res[bus].mem_avail, 0, 0x100000);
1367 		(void) memlist_remove(&pci_bus_res[bus].pmem_avail,
1368 		    0, 0x100000);
1369 	}
1370 
1371 	memlist_free_all(&isa_res.io_used);
1372 	memlist_free_all(&isa_res.mem_used);
1373 
1374 	/* add bus-range property for root/peer bus nodes */
1375 	for (i = 0; i <= pci_bios_maxbus; i++) {
1376 		/* create bus-range property on root/peer buses */
1377 		if (pci_bus_res[i].par_bus == (uchar_t)-1)
1378 			add_bus_range_prop(i);
1379 
1380 		/* setup bus range resource on each bus */
1381 		setup_bus_res(i);
1382 	}
1383 
1384 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
1385 	    DDI_PROP_DONTPASS, "pci-reprog", &onoff) == DDI_SUCCESS) {
1386 		if (strcmp(onoff, "off") == 0) {
1387 			pci_reconfig = 0;
1388 			cmn_err(CE_NOTE, "pci device reprogramming disabled");
1389 		}
1390 		ddi_prop_free(onoff);
1391 	}
1392 
1393 	remove_subtractive_res();
1394 
1395 	/* reprogram the non-subtractive PPB */
1396 	if (pci_reconfig)
1397 		for (i = 0; i <= pci_bios_maxbus; i++)
1398 			fix_ppb_res(i, B_FALSE);
1399 
1400 	for (i = 0; i <= pci_bios_maxbus; i++) {
1401 		/* configure devices not configured by BIOS */
1402 		if (pci_reconfig) {
1403 			/*
1404 			 * Reprogram the subtractive PPB. At this time, all its
1405 			 * siblings should have got their resources already.
1406 			 */
1407 			if (pci_bus_res[i].subtractive)
1408 				fix_ppb_res(i, B_TRUE);
1409 			enumerate_bus_devs(i, CONFIG_NEW);
1410 		}
1411 	}
1412 
1413 	/* All dev programmed, so we can create available prop */
1414 	for (i = 0; i <= pci_bios_maxbus; i++)
1415 		add_bus_available_prop(i);
1416 }
1417 
1418 /*
1419  * populate bus resources
1420  */
1421 static void
1422 populate_bus_res(uchar_t bus)
1423 {
1424 
1425 	/* scan BIOS structures */
1426 	pci_bus_res[bus].pmem_avail = find_bus_res(bus, PREFETCH_TYPE);
1427 	pci_bus_res[bus].mem_avail = find_bus_res(bus, MEM_TYPE);
1428 	pci_bus_res[bus].io_avail = find_bus_res(bus, IO_TYPE);
1429 	pci_bus_res[bus].bus_avail = find_bus_res(bus, BUSRANGE_TYPE);
1430 
1431 	/*
1432 	 * attempt to initialize sub_bus from the largest range-end
1433 	 * in the bus_avail list
1434 	 */
1435 	if (pci_bus_res[bus].bus_avail != NULL) {
1436 		struct memlist *entry;
1437 		int current;
1438 
1439 		entry = pci_bus_res[bus].bus_avail;
1440 		while (entry != NULL) {
1441 			current = entry->ml_address + entry->ml_size - 1;
1442 			if (current > pci_bus_res[bus].sub_bus)
1443 				pci_bus_res[bus].sub_bus = current;
1444 			entry = entry->ml_next;
1445 		}
1446 	}
1447 
1448 	if (bus == 0) {
1449 		/*
1450 		 * Special treatment of bus 0:
1451 		 * If no IO/MEM resource from ACPI/MPSPEC/HRT, copy
1452 		 * pcimem from boot and make I/O space the entire range
1453 		 * starting at 0x100.
1454 		 */
1455 		if (pci_bus_res[0].mem_avail == NULL)
1456 			pci_bus_res[0].mem_avail =
1457 			    memlist_dup(bootops->boot_mem->pcimem);
1458 		/* Exclude 0x00 to 0xff of the I/O space, used by all PCs */
1459 		if (pci_bus_res[0].io_avail == NULL)
1460 			memlist_insert(&pci_bus_res[0].io_avail, 0x100, 0xffff);
1461 	}
1462 
1463 	/*
1464 	 * Create 'ranges' property here before any resources are
1465 	 * removed from the resource lists
1466 	 */
1467 	add_ranges_prop(bus, 0);
1468 }
1469 
1470 
1471 /*
1472  * Create top-level bus dips, i.e. /pci@0,0, /pci@1,0...
1473  */
1474 static void
1475 create_root_bus_dip(uchar_t bus)
1476 {
1477 	int pci_regs[] = {0, 0, 0};
1478 	dev_info_t *dip;
1479 
1480 	ASSERT(pci_bus_res[bus].par_bus == (uchar_t)-1);
1481 
1482 	num_root_bus++;
1483 	ndi_devi_alloc_sleep(ddi_root_node(), "pci",
1484 	    (pnode_t)DEVI_SID_NODEID, &dip);
1485 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1486 	    "#address-cells", 3);
1487 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1488 	    "#size-cells", 2);
1489 	pci_regs[0] = pci_bus_res[bus].root_addr;
1490 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
1491 	    "reg", (int *)pci_regs, 3);
1492 
1493 	/*
1494 	 * If system has PCIe bus, then create different properties
1495 	 */
1496 	if (create_pcie_root_bus(bus, dip) == B_FALSE)
1497 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1498 		    "device_type", "pci");
1499 
1500 	(void) ndi_devi_bind_driver(dip, 0);
1501 	pci_bus_res[bus].dip = dip;
1502 }
1503 
1504 /*
1505  * For any fixed configuration (often compatability) pci devices
1506  * and those with their own expansion rom, create device nodes
1507  * to hold the already configured device details.
1508  */
1509 void
1510 enumerate_bus_devs(uchar_t bus, int config_op)
1511 {
1512 	uchar_t dev, func, nfunc, header;
1513 	ushort_t venid;
1514 	struct pci_devfunc *devlist = NULL, *entry;
1515 
1516 	if (config_op == CONFIG_NEW) {
1517 		dcmn_err(CE_NOTE, "configuring pci bus 0x%x", bus);
1518 	} else if (config_op == CONFIG_FIX) {
1519 		dcmn_err(CE_NOTE, "fixing devices on pci bus 0x%x", bus);
1520 	} else
1521 		dcmn_err(CE_NOTE, "enumerating pci bus 0x%x", bus);
1522 
1523 	if (config_op == CONFIG_NEW) {
1524 		devlist = (struct pci_devfunc *)pci_bus_res[bus].privdata;
1525 		while (devlist) {
1526 			entry = devlist;
1527 			devlist = entry->next;
1528 			if (entry->reprogram ||
1529 			    pci_bus_res[bus].io_reprogram ||
1530 			    pci_bus_res[bus].mem_reprogram) {
1531 				/* reprogram device(s) */
1532 				(void) add_reg_props(entry->dip, bus,
1533 				    entry->dev, entry->func, CONFIG_NEW, 0);
1534 			}
1535 			kmem_free(entry, sizeof (*entry));
1536 		}
1537 		pci_bus_res[bus].privdata = NULL;
1538 		return;
1539 	}
1540 
1541 	for (dev = 0; dev < max_dev_pci; dev++) {
1542 		nfunc = 1;
1543 		for (func = 0; func < nfunc; func++) {
1544 
1545 			dcmn_err(CE_NOTE, "probing dev 0x%x, func 0x%x",
1546 			    dev, func);
1547 
1548 			venid = pci_getw(bus, dev, func, PCI_CONF_VENID);
1549 
1550 			if ((venid == 0xffff) || (venid == 0)) {
1551 				/* no function at this address */
1552 				continue;
1553 			}
1554 
1555 			header = pci_getb(bus, dev, func, PCI_CONF_HEADER);
1556 			if (header == 0xff) {
1557 				continue; /* illegal value */
1558 			}
1559 
1560 			/*
1561 			 * according to some mail from Microsoft posted
1562 			 * to the pci-drivers alias, their only requirement
1563 			 * for a multifunction device is for the 1st
1564 			 * function to have to PCI_HEADER_MULTI bit set.
1565 			 */
1566 			if ((func == 0) && (header & PCI_HEADER_MULTI)) {
1567 				nfunc = 8;
1568 			}
1569 
1570 			if (config_op == CONFIG_FIX ||
1571 			    config_op == CONFIG_INFO) {
1572 				/*
1573 				 * Create the node, unconditionally, on the
1574 				 * first pass only.  It may still need
1575 				 * resource assignment, which will be
1576 				 * done on the second, CONFIG_NEW, pass.
1577 				 */
1578 				process_devfunc(bus, dev, func, header,
1579 				    venid, config_op);
1580 
1581 			}
1582 		}
1583 	}
1584 
1585 	/* percolate bus used resources up through parents to root */
1586 	if (config_op == CONFIG_INFO) {
1587 		int	par_bus;
1588 
1589 		par_bus = pci_bus_res[bus].par_bus;
1590 		while (par_bus != (uchar_t)-1) {
1591 			pci_bus_res[par_bus].io_size +=
1592 			    pci_bus_res[bus].io_size;
1593 			pci_bus_res[par_bus].mem_size +=
1594 			    pci_bus_res[bus].mem_size;
1595 
1596 			if (pci_bus_res[bus].io_used)
1597 				memlist_merge(&pci_bus_res[bus].io_used,
1598 				    &pci_bus_res[par_bus].io_used);
1599 
1600 			if (pci_bus_res[bus].mem_used)
1601 				memlist_merge(&pci_bus_res[bus].mem_used,
1602 				    &pci_bus_res[par_bus].mem_used);
1603 
1604 			if (pci_bus_res[bus].pmem_used)
1605 				memlist_merge(&pci_bus_res[bus].pmem_used,
1606 				    &pci_bus_res[par_bus].pmem_used);
1607 
1608 			bus = par_bus;
1609 			par_bus = pci_bus_res[par_bus].par_bus;
1610 		}
1611 	}
1612 }
1613 
1614 static int
1615 check_pciide_prop(uchar_t revid, ushort_t venid, ushort_t devid,
1616     ushort_t subvenid, ushort_t subdevid)
1617 {
1618 	static int prop_exist = -1;
1619 	static char *pciide_str;
1620 	char compat[32];
1621 
1622 	if (prop_exist == -1) {
1623 		prop_exist = (ddi_prop_lookup_string(DDI_DEV_T_ANY,
1624 		    ddi_root_node(), DDI_PROP_DONTPASS, "pci-ide",
1625 		    &pciide_str) == DDI_SUCCESS);
1626 	}
1627 
1628 	if (!prop_exist)
1629 		return (0);
1630 
1631 	/* compare property value against various forms of compatible */
1632 	if (subvenid) {
1633 		(void) snprintf(compat, sizeof (compat), "pci%x,%x.%x.%x.%x",
1634 		    venid, devid, subvenid, subdevid, revid);
1635 		if (strcmp(pciide_str, compat) == 0)
1636 			return (1);
1637 
1638 		(void) snprintf(compat, sizeof (compat), "pci%x,%x.%x.%x",
1639 		    venid, devid, subvenid, subdevid);
1640 		if (strcmp(pciide_str, compat) == 0)
1641 			return (1);
1642 
1643 		(void) snprintf(compat, sizeof (compat), "pci%x,%x",
1644 		    subvenid, subdevid);
1645 		if (strcmp(pciide_str, compat) == 0)
1646 			return (1);
1647 	}
1648 	(void) snprintf(compat, sizeof (compat), "pci%x,%x.%x",
1649 	    venid, devid, revid);
1650 	if (strcmp(pciide_str, compat) == 0)
1651 		return (1);
1652 
1653 	(void) snprintf(compat, sizeof (compat), "pci%x,%x", venid, devid);
1654 	if (strcmp(pciide_str, compat) == 0)
1655 		return (1);
1656 
1657 	return (0);
1658 }
1659 
1660 static int
1661 is_pciide(uchar_t basecl, uchar_t subcl, uchar_t revid,
1662     ushort_t venid, ushort_t devid, ushort_t subvenid, ushort_t subdevid)
1663 {
1664 	struct ide_table {	/* table for PCI_MASS_OTHER */
1665 		ushort_t venid;
1666 		ushort_t devid;
1667 	} *entry;
1668 
1669 	/* XXX SATA and other devices: need a way to add dynamically */
1670 	static struct ide_table ide_other[] = {
1671 		{0x1095, 0x3112},
1672 		{0x1095, 0x3114},
1673 		{0x1095, 0x3512},
1674 		{0x1095, 0x680},	/* Sil0680 */
1675 		{0x1283, 0x8211},	/* ITE 8211F is subcl PCI_MASS_OTHER */
1676 		{0, 0}
1677 	};
1678 
1679 	if (basecl != PCI_CLASS_MASS)
1680 		return (0);
1681 
1682 	if (subcl == PCI_MASS_IDE) {
1683 		return (1);
1684 	}
1685 
1686 	if (check_pciide_prop(revid, venid, devid, subvenid, subdevid))
1687 		return (1);
1688 
1689 	if (subcl != PCI_MASS_OTHER && subcl != PCI_MASS_SATA) {
1690 		return (0);
1691 	}
1692 
1693 	entry = &ide_other[0];
1694 	while (entry->venid) {
1695 		if (entry->venid == venid && entry->devid == devid)
1696 			return (1);
1697 		entry++;
1698 	}
1699 	return (0);
1700 }
1701 
1702 static int
1703 is_display(uint_t classcode)
1704 {
1705 	static uint_t disp_classes[] = {
1706 		0x000100,
1707 		0x030000,
1708 		0x030001
1709 	};
1710 	int i, nclasses = sizeof (disp_classes) / sizeof (uint_t);
1711 
1712 	for (i = 0; i < nclasses; i++) {
1713 		if (classcode == disp_classes[i])
1714 			return (1);
1715 	}
1716 	return (0);
1717 }
1718 
1719 static void
1720 add_undofix_entry(uint8_t bus, uint8_t dev, uint8_t fn,
1721     void (*undofn)(uint8_t, uint8_t, uint8_t))
1722 {
1723 	struct pci_fixundo *newundo;
1724 
1725 	newundo = kmem_alloc(sizeof (struct pci_fixundo), KM_SLEEP);
1726 
1727 	/*
1728 	 * Adding an item to this list means that we must turn its NMIENABLE
1729 	 * bit back on at a later time.
1730 	 */
1731 	newundo->bus = bus;
1732 	newundo->dev = dev;
1733 	newundo->fn = fn;
1734 	newundo->undofn = undofn;
1735 	newundo->next = undolist;
1736 
1737 	/* add to the undo list in LIFO order */
1738 	undolist = newundo;
1739 }
1740 
1741 void
1742 add_pci_fixes(void)
1743 {
1744 	int i;
1745 
1746 	for (i = 0; i <= pci_bios_maxbus; i++) {
1747 		/*
1748 		 * For each bus, apply needed fixes to the appropriate devices.
1749 		 * This must be done before the main enumeration loop because
1750 		 * some fixes must be applied to devices normally encountered
1751 		 * later in the pci scan (e.g. if a fix to device 7 must be
1752 		 * applied before scanning device 6, applying fixes in the
1753 		 * normal enumeration loop would obviously be too late).
1754 		 */
1755 		enumerate_bus_devs(i, CONFIG_FIX);
1756 	}
1757 }
1758 
1759 void
1760 undo_pci_fixes(void)
1761 {
1762 	struct pci_fixundo *nextundo;
1763 	uint8_t bus, dev, fn;
1764 
1765 	/*
1766 	 * All fixes in the undo list are performed unconditionally.  Future
1767 	 * fixes may require selective undo.
1768 	 */
1769 	while (undolist != NULL) {
1770 
1771 		bus = undolist->bus;
1772 		dev = undolist->dev;
1773 		fn = undolist->fn;
1774 
1775 		(*(undolist->undofn))(bus, dev, fn);
1776 
1777 		nextundo = undolist->next;
1778 		kmem_free(undolist, sizeof (struct pci_fixundo));
1779 		undolist = nextundo;
1780 	}
1781 }
1782 
1783 static void
1784 undo_amd8111_pci_fix(uint8_t bus, uint8_t dev, uint8_t fn)
1785 {
1786 	uint8_t val8;
1787 
1788 	val8 = pci_getb(bus, dev, fn, LPC_IO_CONTROL_REG_1);
1789 	/*
1790 	 * The NMIONERR bit is turned back on to allow the SMM BIOS
1791 	 * to handle more critical PCI errors (e.g. PERR#).
1792 	 */
1793 	val8 |= AMD8111_ENABLENMI;
1794 	pci_putb(bus, dev, fn, LPC_IO_CONTROL_REG_1, val8);
1795 }
1796 
1797 static void
1798 pci_fix_amd8111(uint8_t bus, uint8_t dev, uint8_t fn)
1799 {
1800 	uint8_t val8;
1801 
1802 	val8 = pci_getb(bus, dev, fn, LPC_IO_CONTROL_REG_1);
1803 
1804 	if ((val8 & AMD8111_ENABLENMI) == 0)
1805 		return;
1806 
1807 	/*
1808 	 * We reset NMIONERR in the LPC because master-abort on the PCI
1809 	 * bridge side of the 8111 will cause NMI, which might cause SMI,
1810 	 * which sometimes prevents all devices from being enumerated.
1811 	 */
1812 	val8 &= ~AMD8111_ENABLENMI;
1813 
1814 	pci_putb(bus, dev, fn, LPC_IO_CONTROL_REG_1, val8);
1815 
1816 	add_undofix_entry(bus, dev, fn, undo_amd8111_pci_fix);
1817 }
1818 
1819 static void
1820 set_devpm_d0(uchar_t bus, uchar_t dev, uchar_t func)
1821 {
1822 	uint16_t status;
1823 	uint8_t header;
1824 	uint8_t cap_ptr;
1825 	uint8_t cap_id;
1826 	uint16_t pmcsr;
1827 
1828 	status = pci_getw(bus, dev, func, PCI_CONF_STAT);
1829 	if (!(status & PCI_STAT_CAP))
1830 		return;	/* No capabilities list */
1831 
1832 	header = pci_getb(bus, dev, func, PCI_CONF_HEADER) & PCI_HEADER_TYPE_M;
1833 	if (header == PCI_HEADER_CARDBUS)
1834 		cap_ptr = pci_getb(bus, dev, func, PCI_CBUS_CAP_PTR);
1835 	else
1836 		cap_ptr = pci_getb(bus, dev, func, PCI_CONF_CAP_PTR);
1837 	/*
1838 	 * Walk the capabilities list searching for a PM entry.
1839 	 */
1840 	while (cap_ptr != PCI_CAP_NEXT_PTR_NULL && cap_ptr >= PCI_CAP_PTR_OFF) {
1841 		cap_ptr &= PCI_CAP_PTR_MASK;
1842 		cap_id = pci_getb(bus, dev, func, cap_ptr + PCI_CAP_ID);
1843 		if (cap_id == PCI_CAP_ID_PM) {
1844 			pmcsr = pci_getw(bus, dev, func, cap_ptr + PCI_PMCSR);
1845 			pmcsr &= ~(PCI_PMCSR_STATE_MASK);
1846 			pmcsr |= PCI_PMCSR_D0; /* D0 state */
1847 			pci_putw(bus, dev, func, cap_ptr + PCI_PMCSR, pmcsr);
1848 			break;
1849 		}
1850 		cap_ptr = pci_getb(bus, dev, func, cap_ptr + PCI_CAP_NEXT_PTR);
1851 	}
1852 
1853 }
1854 
1855 #define	is_isa(bc, sc)	\
1856 	(((bc) == PCI_CLASS_BRIDGE) && ((sc) == PCI_BRIDGE_ISA))
1857 
1858 static void
1859 process_devfunc(uchar_t bus, uchar_t dev, uchar_t func, uchar_t header,
1860     ushort_t vendorid, int config_op)
1861 {
1862 	char nodename[32], unitaddr[5];
1863 	dev_info_t *dip;
1864 	uchar_t basecl, subcl, progcl, intr, revid;
1865 	ushort_t subvenid, subdevid, status;
1866 	ushort_t slot_num;
1867 	uint_t classcode, revclass;
1868 	int reprogram = 0, pciide = 0;
1869 	int power[2] = {1, 1};
1870 	int pciex = 0;
1871 	ushort_t is_pci_bridge = 0;
1872 	struct pci_devfunc *devlist = NULL, *entry = NULL;
1873 	boolean_t slot_valid;
1874 	gfx_entry_t *gfxp;
1875 	pcie_req_id_t bdf;
1876 
1877 	ushort_t deviceid = pci_getw(bus, dev, func, PCI_CONF_DEVID);
1878 
1879 	switch (header & PCI_HEADER_TYPE_M) {
1880 	case PCI_HEADER_ZERO:
1881 		subvenid = pci_getw(bus, dev, func, PCI_CONF_SUBVENID);
1882 		subdevid = pci_getw(bus, dev, func, PCI_CONF_SUBSYSID);
1883 		break;
1884 	case PCI_HEADER_CARDBUS:
1885 		subvenid = pci_getw(bus, dev, func, PCI_CBUS_SUBVENID);
1886 		subdevid = pci_getw(bus, dev, func, PCI_CBUS_SUBSYSID);
1887 		/* Record the # of cardbus bridges found on the bus */
1888 		if (config_op == CONFIG_INFO)
1889 			pci_bus_res[bus].num_cbb++;
1890 		break;
1891 	default:
1892 		subvenid = 0;
1893 		subdevid = 0;
1894 		break;
1895 	}
1896 
1897 	if (config_op == CONFIG_FIX) {
1898 		if (vendorid == VENID_AMD && deviceid == DEVID_AMD8111_LPC) {
1899 			pci_fix_amd8111(bus, dev, func);
1900 		}
1901 		return;
1902 	}
1903 
1904 	/* XXX should be use generic names? derive from class? */
1905 	revclass = pci_getl(bus, dev, func, PCI_CONF_REVID);
1906 	classcode = revclass >> 8;
1907 	revid = revclass & 0xff;
1908 
1909 	/* figure out if this is pci-ide */
1910 	basecl = classcode >> 16;
1911 	subcl = (classcode >> 8) & 0xff;
1912 	progcl = classcode & 0xff;
1913 
1914 
1915 	if (is_display(classcode))
1916 		(void) snprintf(nodename, sizeof (nodename), "display");
1917 	else if (!pseudo_isa && is_isa(basecl, subcl))
1918 		(void) snprintf(nodename, sizeof (nodename), "isa");
1919 	else if (subvenid != 0)
1920 		(void) snprintf(nodename, sizeof (nodename),
1921 		    "pci%x,%x", subvenid, subdevid);
1922 	else
1923 		(void) snprintf(nodename, sizeof (nodename),
1924 		    "pci%x,%x", vendorid, deviceid);
1925 
1926 	/* make sure parent bus dip has been created */
1927 	if (pci_bus_res[bus].dip == NULL)
1928 		create_root_bus_dip(bus);
1929 
1930 	ndi_devi_alloc_sleep(pci_bus_res[bus].dip, nodename,
1931 	    DEVI_SID_NODEID, &dip);
1932 
1933 	if (check_if_device_is_pciex(dip, bus, dev, func, &slot_valid,
1934 	    &slot_num, &is_pci_bridge) == B_TRUE)
1935 		pciex = 1;
1936 
1937 	bdf = PCI_GETBDF(bus, dev, func);
1938 	/*
1939 	 * Record BAD AMD bridges which don't support MMIO config access.
1940 	 */
1941 	if (IS_BAD_AMD_NTBRIDGE(vendorid, deviceid) ||
1942 	    IS_AMD_8132_CHIP(vendorid, deviceid)) {
1943 		uchar_t secbus = 0;
1944 		uchar_t subbus = 0;
1945 
1946 		if ((basecl == PCI_CLASS_BRIDGE) &&
1947 		    (subcl == PCI_BRIDGE_PCI)) {
1948 			secbus = pci_getb(bus, dev, func, PCI_BCNF_SECBUS);
1949 			subbus = pci_getb(bus, dev, func, PCI_BCNF_SUBBUS);
1950 		}
1951 		pci_cfgacc_add_workaround(bdf, secbus, subbus);
1952 	}
1953 
1954 	/*
1955 	 * Only populate bus_t if this device is sitting under a PCIE root
1956 	 * complex.  Some particular machines have both a PCIE root complex and
1957 	 * a PCI hostbridge, in which case only devices under the PCIE root
1958 	 * complex will have their bus_t populated.
1959 	 */
1960 	if (pcie_get_rc_dip(dip) != NULL) {
1961 		ck804_fix_aer_ptr(dip, bdf);
1962 		(void) pcie_init_bus(dip, bdf, PCIE_BUS_INITIAL);
1963 	}
1964 
1965 	/* add properties */
1966 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, "device-id", deviceid);
1967 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, "vendor-id", vendorid);
1968 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, "revision-id", revid);
1969 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1970 	    "class-code", classcode);
1971 	if (func == 0)
1972 		(void) snprintf(unitaddr, sizeof (unitaddr), "%x", dev);
1973 	else
1974 		(void) snprintf(unitaddr, sizeof (unitaddr),
1975 		    "%x,%x", dev, func);
1976 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1977 	    "unit-address", unitaddr);
1978 
1979 	/* add device_type for display nodes */
1980 	if (is_display(classcode)) {
1981 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1982 		    "device_type", "display");
1983 	}
1984 	/* add special stuff for header type */
1985 	if ((header & PCI_HEADER_TYPE_M) == PCI_HEADER_ZERO) {
1986 		uchar_t mingrant = pci_getb(bus, dev, func, PCI_CONF_MIN_G);
1987 		uchar_t maxlatency = pci_getb(bus, dev, func, PCI_CONF_MAX_L);
1988 
1989 		if (subvenid != 0) {
1990 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1991 			    "subsystem-id", subdevid);
1992 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1993 			    "subsystem-vendor-id", subvenid);
1994 		}
1995 		if (!pciex)
1996 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1997 			    "min-grant", mingrant);
1998 		if (!pciex)
1999 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
2000 			    "max-latency", maxlatency);
2001 	}
2002 
2003 	/* interrupt, record if not 0 */
2004 	intr = pci_getb(bus, dev, func, PCI_CONF_IPIN);
2005 	if (intr != 0)
2006 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
2007 		    "interrupts", intr);
2008 
2009 	/*
2010 	 * Add support for 133 mhz pci eventually
2011 	 */
2012 	status = pci_getw(bus, dev, func, PCI_CONF_STAT);
2013 
2014 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
2015 	    "devsel-speed", (status & PCI_STAT_DEVSELT) >> 9);
2016 	if (!pciex && (status & PCI_STAT_FBBC))
2017 		(void) ndi_prop_create_boolean(DDI_DEV_T_NONE, dip,
2018 		    "fast-back-to-back");
2019 	if (!pciex && (status & PCI_STAT_66MHZ))
2020 		(void) ndi_prop_create_boolean(DDI_DEV_T_NONE, dip,
2021 		    "66mhz-capable");
2022 	if (status & PCI_STAT_UDF)
2023 		(void) ndi_prop_create_boolean(DDI_DEV_T_NONE, dip,
2024 		    "udf-supported");
2025 	if (pciex && slot_valid) {
2026 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
2027 		    "physical-slot#", slot_num);
2028 		if (!is_pci_bridge)
2029 			pciex_slot_names_prop(dip, slot_num);
2030 	}
2031 
2032 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
2033 	    "power-consumption", power, 2);
2034 
2035 	/* Set the device PM state to D0 */
2036 	set_devpm_d0(bus, dev, func);
2037 
2038 	if ((basecl == PCI_CLASS_BRIDGE) && (subcl == PCI_BRIDGE_PCI))
2039 		add_ppb_props(dip, bus, dev, func, pciex, is_pci_bridge);
2040 	else {
2041 		/*
2042 		 * Record the non-PPB devices on the bus for possible
2043 		 * reprogramming at 2nd bus enumeration.
2044 		 * Note: PPB reprogramming is done in fix_ppb_res()
2045 		 */
2046 		devlist = (struct pci_devfunc *)pci_bus_res[bus].privdata;
2047 		entry = kmem_zalloc(sizeof (*entry), KM_SLEEP);
2048 		entry->dip = dip;
2049 		entry->dev = dev;
2050 		entry->func = func;
2051 		entry->next = devlist;
2052 		pci_bus_res[bus].privdata = entry;
2053 	}
2054 
2055 	if (IS_CLASS_IOAPIC(basecl, subcl, progcl)) {
2056 		create_ioapic_node(bus, dev, func, vendorid, deviceid);
2057 	}
2058 
2059 	/* check for NVIDIA CK8-04/MCP55 based LPC bridge */
2060 	if (NVIDIA_IS_LPC_BRIDGE(vendorid, deviceid) && (dev == 1) &&
2061 	    (func == 0)) {
2062 		add_nvidia_isa_bridge_props(dip, bus, dev, func);
2063 		/* each LPC bridge has an integrated IOAPIC */
2064 		apic_nvidia_io_max++;
2065 	}
2066 
2067 	if (pciex && is_pci_bridge)
2068 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, "model",
2069 		    (char *)"PCIe-PCI bridge");
2070 	else
2071 		add_model_prop(dip, classcode);
2072 
2073 	add_compatible(dip, subvenid, subdevid, vendorid, deviceid,
2074 	    revid, classcode, pciex);
2075 
2076 	/*
2077 	 * See if this device is a controller that advertises
2078 	 * itself to be a standard ATA task file controller, or one that
2079 	 * has been hard coded.
2080 	 *
2081 	 * If it is, check if any other higher precedence driver listed in
2082 	 * driver_aliases will claim the node by calling
2083 	 * ddi_compatibile_driver_major.  If so, clear pciide and do not
2084 	 * create a pci-ide node or any other special handling.
2085 	 *
2086 	 * If another driver does not bind, set the node name to pci-ide
2087 	 * and then let the special pci-ide handling for registers and
2088 	 * child pci-ide nodes proceed below.
2089 	 */
2090 	if (is_pciide(basecl, subcl, revid, vendorid, deviceid,
2091 	    subvenid, subdevid) == 1) {
2092 		if (ddi_compatible_driver_major(dip, NULL) == (major_t)-1) {
2093 			(void) ndi_devi_set_nodename(dip, "pci-ide", 0);
2094 			pciide = 1;
2095 		}
2096 	}
2097 
2098 	DEVI_SET_PCI(dip);
2099 	reprogram = add_reg_props(dip, bus, dev, func, config_op, pciide);
2100 	(void) ndi_devi_bind_driver(dip, 0);
2101 
2102 	/* special handling for pci-ide */
2103 	if (pciide) {
2104 		dev_info_t *cdip;
2105 
2106 		/*
2107 		 * Create properties specified by P1275 Working Group
2108 		 * Proposal #414 Version 1
2109 		 */
2110 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
2111 		    "device_type", "pci-ide");
2112 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
2113 		    "#address-cells", 1);
2114 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
2115 		    "#size-cells", 0);
2116 
2117 		/* allocate two child nodes */
2118 		ndi_devi_alloc_sleep(dip, "ide",
2119 		    (pnode_t)DEVI_SID_NODEID, &cdip);
2120 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cdip,
2121 		    "reg", 0);
2122 		(void) ndi_devi_bind_driver(cdip, 0);
2123 		ndi_devi_alloc_sleep(dip, "ide",
2124 		    (pnode_t)DEVI_SID_NODEID, &cdip);
2125 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, cdip,
2126 		    "reg", 1);
2127 		(void) ndi_devi_bind_driver(cdip, 0);
2128 
2129 		reprogram = 0;	/* don't reprogram pci-ide bridge */
2130 	}
2131 
2132 	if (is_display(classcode)) {
2133 		gfxp = kmem_zalloc(sizeof (*gfxp), KM_SLEEP);
2134 		gfxp->g_dip = dip;
2135 		gfxp->g_prev = NULL;
2136 		gfxp->g_next = gfx_devinfo_list;
2137 		gfx_devinfo_list = gfxp;
2138 		if (gfxp->g_next)
2139 			gfxp->g_next->g_prev = gfxp;
2140 	}
2141 
2142 	/* special handling for isa */
2143 	if (!pseudo_isa && is_isa(basecl, subcl)) {
2144 		/* add device_type */
2145 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
2146 		    "device_type", "isa");
2147 	}
2148 
2149 	if (reprogram && (entry != NULL))
2150 		entry->reprogram = B_TRUE;
2151 
2152 }
2153 
2154 /*
2155  * Some vendors do not use unique subsystem IDs in their products, which
2156  * makes the use of form 2 compatible names (pciSSSS,ssss) inappropriate.
2157  * Allow for these compatible forms to be excluded on a per-device basis.
2158  */
2159 /*ARGSUSED*/
2160 static boolean_t
2161 subsys_compat_exclude(ushort_t venid, ushort_t devid, ushort_t subvenid,
2162     ushort_t subdevid, uchar_t revid, uint_t classcode)
2163 {
2164 	/* Nvidia display adapters */
2165 	if ((venid == 0x10de) && (is_display(classcode)))
2166 		return (B_TRUE);
2167 
2168 	/*
2169 	 * 8086,166 is the Ivy Bridge built-in graphics controller on some
2170 	 * models. Unfortunately 8086,2044 is the Skylake Server processor
2171 	 * memory channel device. The Ivy Bridge device uses the Skylake
2172 	 * ID as its sub-device ID. The GPU is not a memory controller DIMM
2173 	 * channel.
2174 	 */
2175 	if (venid == 0x8086 && devid == 0x166 && subvenid == 0x8086 &&
2176 	    subdevid == 0x2044) {
2177 		return (B_TRUE);
2178 	}
2179 
2180 	return (B_FALSE);
2181 }
2182 
2183 /*
2184  * Set the compatible property to a value compliant with rev 2.1 of the IEEE1275
2185  * PCI binding. This is also used for PCI express devices and we have our own
2186  * minor additions.
2187  *
2188  *   pciVVVV,DDDD.SSSS.ssss.RR	(0)
2189  *   pciVVVV,DDDD.SSSS.ssss	(1)
2190  *   pciSSSS,ssss,s		(2+)
2191  *   pciSSSS,ssss		(2)
2192  *   pciVVVV,DDDD.RR		(3)
2193  *   pciVVVV,DDDD,p		(4+)
2194  *   pciVVVV,DDDD		(4)
2195  *   pciclass,CCSSPP		(5)
2196  *   pciclass,CCSS		(6)
2197  *
2198  * The Subsystem (SSSS) forms are not inserted if subsystem-vendor-id is 0 or if
2199  * it is a case where we know that the IDs overlap.
2200  *
2201  * NOTE: For PCI-Express devices "pci" is replaced with "pciex" in 0-6 above and
2202  * property 2 is not created as per "1275 bindings for PCI Express
2203  * Interconnect".
2204  *
2205  * Unlike on SPARC, we generate both the "pciex" and "pci" versions of the
2206  * above. The problem with property 2 is that it has an ambiguity with
2207  * property 4. To make sure that drivers can specify either form of 2 or 4
2208  * without ambiguity we add a suffix. The 'p' suffix represents the primary ID,
2209  * meaning that it is guaranteed to be form 4. The 's' suffix means that it is
2210  * sub-vendor and sub-device form, meaning it is guaranteed to be form 2.
2211  *
2212  * Set with setprop and \x00 between each to generate the encoded string array
2213  * form.
2214  */
2215 void
2216 add_compatible(dev_info_t *dip, ushort_t subvenid, ushort_t subdevid,
2217     ushort_t vendorid, ushort_t deviceid, uchar_t revid, uint_t classcode,
2218     int pciex)
2219 {
2220 	int i = 0;
2221 	int size = COMPAT_BUFSIZE;
2222 	char *compat[15];
2223 	char *buf, *curr;
2224 
2225 	curr = buf = kmem_alloc(size, KM_SLEEP);
2226 
2227 	if (pciex) {
2228 		if (subvenid) {
2229 			compat[i++] = curr;	/* form 0 */
2230 			(void) snprintf(curr, size, "pciex%x,%x.%x.%x.%x",
2231 			    vendorid, deviceid, subvenid, subdevid, revid);
2232 			size -= strlen(curr) + 1;
2233 			curr += strlen(curr) + 1;
2234 
2235 			compat[i++] = curr;	/* form 1 */
2236 			(void) snprintf(curr, size, "pciex%x,%x.%x.%x",
2237 			    vendorid, deviceid, subvenid, subdevid);
2238 			size -= strlen(curr) + 1;
2239 			curr += strlen(curr) + 1;
2240 
2241 		}
2242 		compat[i++] = curr;	/* form 3 */
2243 		(void) snprintf(curr, size, "pciex%x,%x.%x",
2244 		    vendorid, deviceid, revid);
2245 		size -= strlen(curr) + 1;
2246 		curr += strlen(curr) + 1;
2247 
2248 		compat[i++] = curr;	/* form 4 */
2249 		(void) snprintf(curr, size, "pciex%x,%x", vendorid, deviceid);
2250 		size -= strlen(curr) + 1;
2251 		curr += strlen(curr) + 1;
2252 
2253 		compat[i++] = curr;	/* form 5 */
2254 		(void) snprintf(curr, size, "pciexclass,%06x", classcode);
2255 		size -= strlen(curr) + 1;
2256 		curr += strlen(curr) + 1;
2257 
2258 		compat[i++] = curr;	/* form 6 */
2259 		(void) snprintf(curr, size, "pciexclass,%04x",
2260 		    (classcode >> 8));
2261 		size -= strlen(curr) + 1;
2262 		curr += strlen(curr) + 1;
2263 	}
2264 
2265 	if (subvenid) {
2266 		compat[i++] = curr;	/* form 0 */
2267 		(void) snprintf(curr, size, "pci%x,%x.%x.%x.%x",
2268 		    vendorid, deviceid, subvenid, subdevid, revid);
2269 		size -= strlen(curr) + 1;
2270 		curr += strlen(curr) + 1;
2271 
2272 		compat[i++] = curr;	/* form 1 */
2273 		(void) snprintf(curr, size, "pci%x,%x.%x.%x",
2274 		    vendorid, deviceid, subvenid, subdevid);
2275 		size -= strlen(curr) + 1;
2276 		curr += strlen(curr) + 1;
2277 
2278 		if (subsys_compat_exclude(vendorid, deviceid, subvenid,
2279 		    subdevid, revid, classcode) == B_FALSE) {
2280 			compat[i++] = curr;	/* form 2+ */
2281 			(void) snprintf(curr, size, "pci%x,%x,s", subvenid,
2282 			    subdevid);
2283 			size -= strlen(curr) + 1;
2284 			curr += strlen(curr) + 1;
2285 
2286 			compat[i++] = curr;	/* form 2 */
2287 			(void) snprintf(curr, size, "pci%x,%x", subvenid,
2288 			    subdevid);
2289 			size -= strlen(curr) + 1;
2290 			curr += strlen(curr) + 1;
2291 		}
2292 	}
2293 	compat[i++] = curr;	/* form 3 */
2294 	(void) snprintf(curr, size, "pci%x,%x.%x", vendorid, deviceid, revid);
2295 	size -= strlen(curr) + 1;
2296 	curr += strlen(curr) + 1;
2297 
2298 	compat[i++] = curr;	/* form 4+ */
2299 	(void) snprintf(curr, size, "pci%x,%x,p", vendorid, deviceid);
2300 	size -= strlen(curr) + 1;
2301 	curr += strlen(curr) + 1;
2302 
2303 	compat[i++] = curr;	/* form 4 */
2304 	(void) snprintf(curr, size, "pci%x,%x", vendorid, deviceid);
2305 	size -= strlen(curr) + 1;
2306 	curr += strlen(curr) + 1;
2307 
2308 	compat[i++] = curr;	/* form 5 */
2309 	(void) snprintf(curr, size, "pciclass,%06x", classcode);
2310 	size -= strlen(curr) + 1;
2311 	curr += strlen(curr) + 1;
2312 
2313 	compat[i++] = curr;	/* form 6 */
2314 	(void) snprintf(curr, size, "pciclass,%04x", (classcode >> 8));
2315 	size -= strlen(curr) + 1;
2316 	curr += strlen(curr) + 1;
2317 
2318 	(void) ndi_prop_update_string_array(DDI_DEV_T_NONE, dip,
2319 	    "compatible", compat, i);
2320 	kmem_free(buf, COMPAT_BUFSIZE);
2321 }
2322 
2323 /*
2324  * Adjust the reg properties for a dual channel PCI-IDE device.
2325  *
2326  * NOTE: don't do anything that changes the order of the hard-decodes
2327  * and programmed BARs. The kernel driver depends on these values
2328  * being in this order regardless of whether they're for a 'native'
2329  * mode BAR or not.
2330  */
2331 /*
2332  * config info for pci-ide devices
2333  */
2334 static struct {
2335 	uchar_t  native_mask;	/* 0 == 'compatibility' mode, 1 == native */
2336 	uchar_t  bar_offset;	/* offset for alt status register */
2337 	ushort_t addr;		/* compatibility mode base address */
2338 	ushort_t length;	/* number of ports for this BAR */
2339 } pciide_bar[] = {
2340 	{ 0x01, 0, 0x1f0, 8 },	/* primary lower BAR */
2341 	{ 0x01, 2, 0x3f6, 1 },	/* primary upper BAR */
2342 	{ 0x04, 0, 0x170, 8 },	/* secondary lower BAR */
2343 	{ 0x04, 2, 0x376, 1 }	/* secondary upper BAR */
2344 };
2345 
2346 static int
2347 pciIdeAdjustBAR(uchar_t progcl, int index, uint_t *basep, uint_t *lenp)
2348 {
2349 	int hard_decode = 0;
2350 
2351 	/*
2352 	 * Adjust the base and len for the BARs of the PCI-IDE
2353 	 * device's primary and secondary controllers. The first
2354 	 * two BARs are for the primary controller and the next
2355 	 * two BARs are for the secondary controller. The fifth
2356 	 * and sixth bars are never adjusted.
2357 	 */
2358 	if (index >= 0 && index <= 3) {
2359 		*lenp = pciide_bar[index].length;
2360 
2361 		if (progcl & pciide_bar[index].native_mask) {
2362 			*basep += pciide_bar[index].bar_offset;
2363 		} else {
2364 			*basep = pciide_bar[index].addr;
2365 			hard_decode = 1;
2366 		}
2367 	}
2368 
2369 	/*
2370 	 * if either base or len is zero make certain both are zero
2371 	 */
2372 	if (*basep == 0 || *lenp == 0) {
2373 		*basep = 0;
2374 		*lenp = 0;
2375 		hard_decode = 0;
2376 	}
2377 
2378 	return (hard_decode);
2379 }
2380 
2381 
2382 /*
2383  * Add the "reg" and "assigned-addresses" property
2384  */
2385 static int
2386 add_reg_props(dev_info_t *dip, uchar_t bus, uchar_t dev, uchar_t func,
2387     int config_op, int pciide)
2388 {
2389 	uchar_t baseclass, subclass, progclass, header;
2390 	ushort_t bar_sz;
2391 	uint64_t value = 0;
2392 	uint_t devloc;
2393 	uint_t base, base_hi, type;
2394 	ushort_t offset, end;
2395 	int max_basereg, j, reprogram = 0;
2396 	uint_t phys_hi;
2397 	struct memlist **io_avail, **io_used;
2398 	struct memlist **mem_avail, **mem_used;
2399 	struct memlist **pmem_avail, **pmem_used;
2400 	uchar_t res_bus;
2401 
2402 	pci_regspec_t regs[16] = {{0}};
2403 	pci_regspec_t assigned[15] = {{0}};
2404 	int nreg, nasgn;
2405 
2406 	io_avail = &pci_bus_res[bus].io_avail;
2407 	io_used = &pci_bus_res[bus].io_used;
2408 	mem_avail = &pci_bus_res[bus].mem_avail;
2409 	mem_used = &pci_bus_res[bus].mem_used;
2410 	pmem_avail = &pci_bus_res[bus].pmem_avail;
2411 	pmem_used = &pci_bus_res[bus].pmem_used;
2412 
2413 	devloc = (uint_t)bus << 16 | (uint_t)dev << 11 | (uint_t)func << 8;
2414 	regs[0].pci_phys_hi = devloc;
2415 	nreg = 1;	/* rest of regs[0] is all zero */
2416 	nasgn = 0;
2417 
2418 	baseclass = pci_getb(bus, dev, func, PCI_CONF_BASCLASS);
2419 	subclass = pci_getb(bus, dev, func, PCI_CONF_SUBCLASS);
2420 	progclass = pci_getb(bus, dev, func, PCI_CONF_PROGCLASS);
2421 	header = pci_getb(bus, dev, func, PCI_CONF_HEADER) & PCI_HEADER_TYPE_M;
2422 
2423 	switch (header) {
2424 	case PCI_HEADER_ZERO:
2425 		max_basereg = PCI_BASE_NUM;
2426 		break;
2427 	case PCI_HEADER_PPB:
2428 		max_basereg = PCI_BCNF_BASE_NUM;
2429 		break;
2430 	case PCI_HEADER_CARDBUS:
2431 		max_basereg = PCI_CBUS_BASE_NUM;
2432 		reprogram = 1;
2433 		break;
2434 	default:
2435 		max_basereg = 0;
2436 		break;
2437 	}
2438 
2439 	/*
2440 	 * Create the register property by saving the current
2441 	 * value of the base register. Write 0xffffffff to the
2442 	 * base register.  Read the value back to determine the
2443 	 * required size of the address space.  Restore the base
2444 	 * register contents.
2445 	 *
2446 	 * Do not disable I/O and memory access for bridges; this
2447 	 * has the side-effect of making the bridge transparent to
2448 	 * secondary-bus activity (see sections 4.1-4.3 of the
2449 	 * PCI-PCI Bridge Spec V1.2).  For non-bridges, disable
2450 	 * I/O and memory access to avoid difficulty with USB
2451 	 * emulation (see OHCI spec1.0a appendix B
2452 	 * "Host Controller Mapping")
2453 	 */
2454 	end = PCI_CONF_BASE0 + max_basereg * sizeof (uint_t);
2455 	for (j = 0, offset = PCI_CONF_BASE0; offset < end;
2456 	    j++, offset += bar_sz) {
2457 		uint_t	command;
2458 
2459 		/* determine the size of the address space */
2460 		base = pci_getl(bus, dev, func, offset);
2461 		if (baseclass != PCI_CLASS_BRIDGE) {
2462 			command = (uint_t)pci_getw(bus, dev, func,
2463 			    PCI_CONF_COMM);
2464 			pci_putw(bus, dev, func, PCI_CONF_COMM,
2465 			    command & ~(PCI_COMM_MAE | PCI_COMM_IO));
2466 		}
2467 		pci_putl(bus, dev, func, offset, 0xffffffff);
2468 		value = pci_getl(bus, dev, func, offset);
2469 		pci_putl(bus, dev, func, offset, base);
2470 		if (baseclass != PCI_CLASS_BRIDGE)
2471 			pci_putw(bus, dev, func, PCI_CONF_COMM, command);
2472 
2473 		/* construct phys hi,med.lo, size hi, lo */
2474 		if ((pciide && j < 4) || (base & PCI_BASE_SPACE_IO)) {
2475 			int hard_decode = 0;
2476 			uint_t len;
2477 
2478 			/* i/o space */
2479 			bar_sz = PCI_BAR_SZ_32;
2480 			value &= PCI_BASE_IO_ADDR_M;
2481 			len = ((value ^ (value-1)) + 1) >> 1;
2482 
2483 			/* XXX Adjust first 4 IDE registers */
2484 			if (pciide) {
2485 				if (subclass != PCI_MASS_IDE)
2486 					progclass = (PCI_IDE_IF_NATIVE_PRI |
2487 					    PCI_IDE_IF_NATIVE_SEC);
2488 				hard_decode = pciIdeAdjustBAR(progclass, j,
2489 				    &base, &len);
2490 			} else if (value == 0) {
2491 				/* skip base regs with size of 0 */
2492 				continue;
2493 			}
2494 
2495 			regs[nreg].pci_phys_hi = PCI_ADDR_IO | devloc |
2496 			    (hard_decode ? PCI_RELOCAT_B : offset);
2497 			regs[nreg].pci_phys_low = hard_decode ?
2498 			    base & PCI_BASE_IO_ADDR_M : 0;
2499 			assigned[nasgn].pci_phys_hi =
2500 			    PCI_RELOCAT_B | regs[nreg].pci_phys_hi;
2501 			regs[nreg].pci_size_low =
2502 			    assigned[nasgn].pci_size_low = len;
2503 			type = base & (~PCI_BASE_IO_ADDR_M);
2504 			base &= PCI_BASE_IO_ADDR_M;
2505 			/*
2506 			 * A device under a subtractive PPB can allocate
2507 			 * resources from its parent bus if there is no resource
2508 			 * available on its own bus.
2509 			 */
2510 			if ((config_op == CONFIG_NEW) && (*io_avail == NULL)) {
2511 				res_bus = bus;
2512 				while (pci_bus_res[res_bus].subtractive) {
2513 					res_bus = pci_bus_res[res_bus].par_bus;
2514 					if (res_bus == (uchar_t)-1)
2515 						break; /* root bus already */
2516 					if (pci_bus_res[res_bus].io_avail) {
2517 						io_avail = &pci_bus_res
2518 						    [res_bus].io_avail;
2519 						break;
2520 					}
2521 				}
2522 			}
2523 
2524 			/*
2525 			 * first pass - gather what's there
2526 			 * update/second pass - adjust/allocate regions
2527 			 *	config - allocate regions
2528 			 */
2529 			if (config_op == CONFIG_INFO) {	/* first pass */
2530 				/* take out of the resource map of the bus */
2531 				if (base != 0) {
2532 					(void) memlist_remove(io_avail, base,
2533 					    len);
2534 					memlist_insert(io_used, base, len);
2535 				} else {
2536 					reprogram = 1;
2537 				}
2538 				pci_bus_res[bus].io_size += len;
2539 			} else if ((*io_avail && base == 0) ||
2540 			    pci_bus_res[bus].io_reprogram) {
2541 				base = (uint_t)memlist_find(io_avail, len, len);
2542 				if (base != 0) {
2543 					memlist_insert(io_used, base, len);
2544 					/* XXX need to worry about 64-bit? */
2545 					pci_putl(bus, dev, func, offset,
2546 					    base | type);
2547 					base = pci_getl(bus, dev, func, offset);
2548 					base &= PCI_BASE_IO_ADDR_M;
2549 				}
2550 				if (base == 0) {
2551 					cmn_err(CE_WARN, "failed to program"
2552 					    " IO space [%d/%d/%d] BAR@0x%x"
2553 					    " length 0x%x",
2554 					    bus, dev, func, offset, len);
2555 				}
2556 			}
2557 			assigned[nasgn].pci_phys_low = base;
2558 			nreg++, nasgn++;
2559 
2560 		} else {
2561 			uint64_t len;
2562 			/* memory space */
2563 			if ((base & PCI_BASE_TYPE_M) == PCI_BASE_TYPE_ALL) {
2564 				bar_sz = PCI_BAR_SZ_64;
2565 				base_hi = pci_getl(bus, dev, func, offset + 4);
2566 				pci_putl(bus, dev, func, offset + 4,
2567 				    0xffffffff);
2568 				value |= (uint64_t)pci_getl(bus, dev, func,
2569 				    offset + 4) << 32;
2570 				pci_putl(bus, dev, func, offset + 4, base_hi);
2571 				phys_hi = PCI_ADDR_MEM64;
2572 				value &= PCI_BASE_M_ADDR64_M;
2573 			} else {
2574 				bar_sz = PCI_BAR_SZ_32;
2575 				base_hi = 0;
2576 				phys_hi = PCI_ADDR_MEM32;
2577 				value &= PCI_BASE_M_ADDR_M;
2578 			}
2579 
2580 			/* skip base regs with size of 0 */
2581 			if (value == 0)
2582 				continue;
2583 
2584 			len = ((value ^ (value-1)) + 1) >> 1;
2585 			regs[nreg].pci_size_low =
2586 			    assigned[nasgn].pci_size_low = len & 0xffffffff;
2587 			regs[nreg].pci_size_hi =
2588 			    assigned[nasgn].pci_size_hi = len >> 32;
2589 
2590 			phys_hi |= (devloc | offset);
2591 			if (base & PCI_BASE_PREF_M)
2592 				phys_hi |= PCI_PREFETCH_B;
2593 
2594 			/*
2595 			 * A device under a subtractive PPB can allocate
2596 			 * resources from its parent bus if there is no resource
2597 			 * available on its own bus.
2598 			 */
2599 			if ((config_op == CONFIG_NEW) && (*mem_avail == NULL)) {
2600 				res_bus = bus;
2601 				while (pci_bus_res[res_bus].subtractive) {
2602 					res_bus = pci_bus_res[res_bus].par_bus;
2603 					if (res_bus == (uchar_t)-1)
2604 						break; /* root bus already */
2605 					mem_avail =
2606 					    &pci_bus_res[res_bus].mem_avail;
2607 					pmem_avail =
2608 					    &pci_bus_res [res_bus].pmem_avail;
2609 					/*
2610 					 * Break out as long as at least
2611 					 * mem_avail is available
2612 					 */
2613 					if ((*pmem_avail &&
2614 					    (phys_hi & PCI_PREFETCH_B)) ||
2615 					    *mem_avail)
2616 						break;
2617 				}
2618 			}
2619 
2620 			regs[nreg].pci_phys_hi =
2621 			    assigned[nasgn].pci_phys_hi = phys_hi;
2622 			assigned[nasgn].pci_phys_hi |= PCI_RELOCAT_B;
2623 			assigned[nasgn].pci_phys_mid = base_hi;
2624 			type = base & ~PCI_BASE_M_ADDR_M;
2625 			base &= PCI_BASE_M_ADDR_M;
2626 
2627 			if (config_op == CONFIG_INFO) {
2628 				/* take out of the resource map of the bus */
2629 				if (base != 0) {
2630 					/* remove from PMEM and MEM space */
2631 					(void) memlist_remove(mem_avail,
2632 					    base, len);
2633 					(void) memlist_remove(pmem_avail,
2634 					    base, len);
2635 					/* only note as used in correct map */
2636 					if (phys_hi & PCI_PREFETCH_B)
2637 						memlist_insert(pmem_used,
2638 						    base, len);
2639 					else
2640 						memlist_insert(mem_used,
2641 						    base, len);
2642 				} else {
2643 					reprogram = 1;
2644 				}
2645 				pci_bus_res[bus].mem_size += len;
2646 			} else if ((*mem_avail && base == 0) ||
2647 			    pci_bus_res[bus].mem_reprogram) {
2648 				/*
2649 				 * When desired, attempt a prefetchable
2650 				 * allocation first
2651 				 */
2652 				if (phys_hi & PCI_PREFETCH_B) {
2653 					base = (uint_t)memlist_find(pmem_avail,
2654 					    len, len);
2655 					if (base != 0) {
2656 						memlist_insert(pmem_used,
2657 						    base, len);
2658 						(void) memlist_remove(mem_avail,
2659 						    base, len);
2660 					}
2661 				}
2662 				/*
2663 				 * If prefetchable allocation was not
2664 				 * desired, or failed, attempt ordinary
2665 				 * memory allocation
2666 				 */
2667 				if (base == 0) {
2668 					base = (uint_t)memlist_find(mem_avail,
2669 					    len, len);
2670 					if (base != 0) {
2671 						memlist_insert(mem_used,
2672 						    base, len);
2673 						(void) memlist_remove(
2674 						    pmem_avail, base, len);
2675 					}
2676 				}
2677 				if (base != 0) {
2678 					pci_putl(bus, dev, func, offset,
2679 					    base | type);
2680 					base = pci_getl(bus, dev, func, offset);
2681 					base &= PCI_BASE_M_ADDR_M;
2682 				} else
2683 					cmn_err(CE_WARN, "failed to program "
2684 					    "mem space [%d/%d/%d] BAR@0x%x"
2685 					    " length 0x%"PRIx64,
2686 					    bus, dev, func, offset, len);
2687 			}
2688 			assigned[nasgn].pci_phys_low = base;
2689 			nreg++, nasgn++;
2690 		}
2691 	}
2692 	switch (header) {
2693 	case PCI_HEADER_ZERO:
2694 		offset = PCI_CONF_ROM;
2695 		break;
2696 	case PCI_HEADER_PPB:
2697 		offset = PCI_BCNF_ROM;
2698 		break;
2699 	default: /* including PCI_HEADER_CARDBUS */
2700 		goto done;
2701 	}
2702 
2703 	/*
2704 	 * Add the expansion rom memory space
2705 	 * Determine the size of the ROM base reg; don't write reserved bits
2706 	 * ROM isn't in the PCI memory space.
2707 	 */
2708 	base = pci_getl(bus, dev, func, offset);
2709 	pci_putl(bus, dev, func, offset, PCI_BASE_ROM_ADDR_M);
2710 	value = pci_getl(bus, dev, func, offset);
2711 	pci_putl(bus, dev, func, offset, base);
2712 	if (value & PCI_BASE_ROM_ENABLE)
2713 		value &= PCI_BASE_ROM_ADDR_M;
2714 	else
2715 		value = 0;
2716 
2717 	if (value != 0) {
2718 		uint_t len;
2719 
2720 		regs[nreg].pci_phys_hi = (PCI_ADDR_MEM32 | devloc) + offset;
2721 		assigned[nasgn].pci_phys_hi = (PCI_RELOCAT_B |
2722 		    PCI_ADDR_MEM32 | devloc) + offset;
2723 		base &= PCI_BASE_ROM_ADDR_M;
2724 		assigned[nasgn].pci_phys_low = base;
2725 		len = ((value ^ (value-1)) + 1) >> 1;
2726 		regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = len;
2727 		nreg++, nasgn++;
2728 		/* take it out of the memory resource */
2729 		if (base != 0) {
2730 			(void) memlist_remove(mem_avail, base, len);
2731 			memlist_insert(mem_used, base, len);
2732 			pci_bus_res[bus].mem_size += len;
2733 		}
2734 	}
2735 
2736 	/*
2737 	 * Account for "legacy" (alias) video adapter resources
2738 	 */
2739 
2740 	/* add the three hard-decode, aliased address spaces for VGA */
2741 	if ((baseclass == PCI_CLASS_DISPLAY && subclass == PCI_DISPLAY_VGA) ||
2742 	    (baseclass == PCI_CLASS_NONE && subclass == PCI_NONE_VGA)) {
2743 
2744 		/* VGA hard decode 0x3b0-0x3bb */
2745 		regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi =
2746 		    (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc);
2747 		regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x3b0;
2748 		regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0xc;
2749 		nreg++, nasgn++;
2750 		(void) memlist_remove(io_avail, 0x3b0, 0xc);
2751 		memlist_insert(io_used, 0x3b0, 0xc);
2752 		pci_bus_res[bus].io_size += 0xc;
2753 
2754 		/* VGA hard decode 0x3c0-0x3df */
2755 		regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi =
2756 		    (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc);
2757 		regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x3c0;
2758 		regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0x20;
2759 		nreg++, nasgn++;
2760 		(void) memlist_remove(io_avail, 0x3c0, 0x20);
2761 		memlist_insert(io_used, 0x3c0, 0x20);
2762 		pci_bus_res[bus].io_size += 0x20;
2763 
2764 		/* Video memory */
2765 		regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi =
2766 		    (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_MEM32 | devloc);
2767 		regs[nreg].pci_phys_low =
2768 		    assigned[nasgn].pci_phys_low = 0xa0000;
2769 		regs[nreg].pci_size_low =
2770 		    assigned[nasgn].pci_size_low = 0x20000;
2771 		nreg++, nasgn++;
2772 		/* remove from MEM and PMEM space */
2773 		(void) memlist_remove(mem_avail, 0xa0000, 0x20000);
2774 		(void) memlist_remove(pmem_avail, 0xa0000, 0x20000);
2775 		memlist_insert(mem_used, 0xa0000, 0x20000);
2776 		pci_bus_res[bus].mem_size += 0x20000;
2777 	}
2778 
2779 	/* add the hard-decode, aliased address spaces for 8514 */
2780 	if ((baseclass == PCI_CLASS_DISPLAY) &&
2781 	    (subclass == PCI_DISPLAY_VGA) &&
2782 	    (progclass & PCI_DISPLAY_IF_8514)) {
2783 
2784 		/* hard decode 0x2e8 */
2785 		regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi =
2786 		    (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc);
2787 		regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x2e8;
2788 		regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0x1;
2789 		nreg++, nasgn++;
2790 		(void) memlist_remove(io_avail, 0x2e8, 0x1);
2791 		memlist_insert(io_used, 0x2e8, 0x1);
2792 		pci_bus_res[bus].io_size += 0x1;
2793 
2794 		/* hard decode 0x2ea-0x2ef */
2795 		regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi =
2796 		    (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc);
2797 		regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x2ea;
2798 		regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0x6;
2799 		nreg++, nasgn++;
2800 		(void) memlist_remove(io_avail, 0x2ea, 0x6);
2801 		memlist_insert(io_used, 0x2ea, 0x6);
2802 		pci_bus_res[bus].io_size += 0x6;
2803 	}
2804 
2805 done:
2806 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "reg",
2807 	    (int *)regs, nreg * sizeof (pci_regspec_t) / sizeof (int));
2808 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
2809 	    "assigned-addresses",
2810 	    (int *)assigned, nasgn * sizeof (pci_regspec_t) / sizeof (int));
2811 
2812 	return (reprogram);
2813 }
2814 
2815 static void
2816 add_ppb_props(dev_info_t *dip, uchar_t bus, uchar_t dev, uchar_t func,
2817     int pciex, ushort_t is_pci_bridge)
2818 {
2819 	char *dev_type;
2820 	int i;
2821 	uint_t val;
2822 	uint64_t io_range[2], mem_range[2], pmem_range[2];
2823 	uchar_t secbus = pci_getb(bus, dev, func, PCI_BCNF_SECBUS);
2824 	uchar_t subbus = pci_getb(bus, dev, func, PCI_BCNF_SUBBUS);
2825 	uchar_t progclass;
2826 
2827 	ASSERT(secbus <= subbus);
2828 
2829 	/*
2830 	 * Check if it's a subtractive PPB.
2831 	 */
2832 	progclass = pci_getb(bus, dev, func, PCI_CONF_PROGCLASS);
2833 	if (progclass == PCI_BRIDGE_PCI_IF_SUBDECODE)
2834 		pci_bus_res[secbus].subtractive = B_TRUE;
2835 
2836 	/*
2837 	 * Some BIOSes lie about max pci busses, we allow for
2838 	 * such mistakes here
2839 	 */
2840 	if (subbus > pci_bios_maxbus) {
2841 		pci_bios_maxbus = subbus;
2842 		alloc_res_array();
2843 	}
2844 
2845 	ASSERT(pci_bus_res[secbus].dip == NULL);
2846 	pci_bus_res[secbus].dip = dip;
2847 	pci_bus_res[secbus].par_bus = bus;
2848 
2849 	dev_type = (pciex && !is_pci_bridge) ? "pciex" : "pci";
2850 
2851 	/* setup bus number hierarchy */
2852 	pci_bus_res[secbus].sub_bus = subbus;
2853 	/*
2854 	 * Keep track of the largest subordinate bus number (this is essential
2855 	 * for peer busses because there is no other way of determining its
2856 	 * subordinate bus number).
2857 	 */
2858 	if (subbus > pci_bus_res[bus].sub_bus)
2859 		pci_bus_res[bus].sub_bus = subbus;
2860 	/*
2861 	 * Loop through subordinate busses, initializing their parent bus
2862 	 * field to this bridge's parent.  The subordinate busses' parent
2863 	 * fields may very well be further refined later, as child bridges
2864 	 * are enumerated.  (The value is to note that the subordinate busses
2865 	 * are not peer busses by changing their par_bus fields to anything
2866 	 * other than -1.)
2867 	 */
2868 	for (i = secbus + 1; i <= subbus; i++)
2869 		pci_bus_res[i].par_bus = bus;
2870 
2871 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
2872 	    "device_type", dev_type);
2873 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
2874 	    "#address-cells", 3);
2875 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
2876 	    "#size-cells", 2);
2877 
2878 	/*
2879 	 * Collect bridge window specifications, and use them to populate
2880 	 * the "avail" resources for the bus.  Not all of those resources will
2881 	 * end up being available; this is done top-down, and so the initial
2882 	 * collection of windows populates the 'ranges' property for the
2883 	 * bus node.  Later, as children are found, resources are removed from
2884 	 * the 'avail' list, so that it becomes the freelist for
2885 	 * this point in the tree.  ranges may be set again after bridge
2886 	 * reprogramming in fix_ppb_res(), in which case it's set from
2887 	 * used + avail.
2888 	 *
2889 	 * According to PPB spec, the base register should be programmed
2890 	 * with a value bigger than the limit register when there are
2891 	 * no resources available. This applies to io, memory, and
2892 	 * prefetchable memory.
2893 	 */
2894 
2895 	/*
2896 	 * io range
2897 	 * We determine i/o windows that are left unconfigured by BIOS
2898 	 * through its i/o enable bit as Microsoft recommends OEMs to do.
2899 	 * If it is unset, we disable i/o and mark it for reconfiguration in
2900 	 * later passes by setting the base > limit
2901 	 */
2902 	val = (uint_t)pci_getw(bus, dev, func, PCI_CONF_COMM);
2903 	if (val & PCI_COMM_IO) {
2904 		val = (uint_t)pci_getb(bus, dev, func, PCI_BCNF_IO_BASE_LOW);
2905 		io_range[0] = ((val & PCI_BCNF_IO_MASK) << PCI_BCNF_IO_SHIFT);
2906 		val = (uint_t)pci_getb(bus, dev, func, PCI_BCNF_IO_LIMIT_LOW);
2907 		io_range[1]  = ((val & PCI_BCNF_IO_MASK) << PCI_BCNF_IO_SHIFT) |
2908 		    0xfff;
2909 		if ((io_range[0] & PCI_BCNF_ADDR_MASK) == PCI_BCNF_IO_32BIT) {
2910 			uint16_t io_base_hi, io_limit_hi;
2911 			io_base_hi = pci_getw(bus, dev, func,
2912 			    PCI_BCNF_IO_BASE_HI);
2913 			io_limit_hi = pci_getw(bus, dev, func,
2914 			    PCI_BCNF_IO_LIMIT_HI);
2915 
2916 			io_range[0] |= (uint32_t)io_base_hi << 16;
2917 			io_range[1] |= (uint32_t)io_limit_hi << 16;
2918 		}
2919 	} else {
2920 		io_range[0] = 0x9fff;
2921 		io_range[1] = 0x1000;
2922 		pci_putb(bus, dev, func, PCI_BCNF_IO_BASE_LOW,
2923 		    (uint8_t)((io_range[0] >> 8) & 0xf0));
2924 		pci_putb(bus, dev, func, PCI_BCNF_IO_LIMIT_LOW,
2925 		    (uint8_t)((io_range[1] >> 8) & 0xf0));
2926 		pci_putw(bus, dev, func, PCI_BCNF_IO_BASE_HI, 0);
2927 		pci_putw(bus, dev, func, PCI_BCNF_IO_LIMIT_HI, 0);
2928 	}
2929 
2930 	if (io_range[0] != 0 && io_range[0] < io_range[1]) {
2931 		memlist_insert(&pci_bus_res[secbus].io_avail,
2932 		    io_range[0], (io_range[1] - io_range[0] + 1));
2933 		memlist_insert(&pci_bus_res[bus].io_used,
2934 		    io_range[0], (io_range[1] - io_range[0] + 1));
2935 		if (pci_bus_res[bus].io_avail != NULL) {
2936 			(void) memlist_remove(&pci_bus_res[bus].io_avail,
2937 			    io_range[0], (io_range[1] - io_range[0] + 1));
2938 		}
2939 		dcmn_err(CE_NOTE, "bus %d io-range: 0x%" PRIx64 "-%" PRIx64,
2940 		    secbus, io_range[0], io_range[1]);
2941 	}
2942 
2943 	/* mem range */
2944 	val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_MEM_BASE);
2945 	mem_range[0] = ((val & PCI_BCNF_MEM_MASK) << PCI_BCNF_MEM_SHIFT);
2946 	val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_MEM_LIMIT);
2947 	mem_range[1] = ((val & PCI_BCNF_MEM_MASK) << PCI_BCNF_MEM_SHIFT) |
2948 	    0xfffff;
2949 	if (mem_range[0] != 0 && mem_range[0] < mem_range[1]) {
2950 		memlist_insert(&pci_bus_res[secbus].mem_avail,
2951 		    (uint64_t)mem_range[0],
2952 		    (uint64_t)(mem_range[1] - mem_range[0] + 1));
2953 		memlist_insert(&pci_bus_res[bus].mem_used,
2954 		    (uint64_t)mem_range[0],
2955 		    (uint64_t)(mem_range[1] - mem_range[0] + 1));
2956 		/* remove from parent resource list */
2957 		(void) memlist_remove(&pci_bus_res[bus].mem_avail,
2958 		    (uint64_t)mem_range[0],
2959 		    (uint64_t)(mem_range[1] - mem_range[0] + 1));
2960 		(void) memlist_remove(&pci_bus_res[bus].pmem_avail,
2961 		    (uint64_t)mem_range[0],
2962 		    (uint64_t)(mem_range[1] - mem_range[0] + 1));
2963 		dcmn_err(CE_NOTE, "bus %d mem-range: 0x%" PRIx64 "-%" PRIx64,
2964 		    secbus, mem_range[0], mem_range[1]);
2965 	}
2966 
2967 	/* prefetchable memory range */
2968 	val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_PF_BASE_LOW);
2969 	pmem_range[0] = ((val & PCI_BCNF_MEM_MASK) << PCI_BCNF_MEM_SHIFT);
2970 	val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_PF_LIMIT_LOW);
2971 	pmem_range[1] = ((val & PCI_BCNF_MEM_MASK) << PCI_BCNF_MEM_SHIFT) |
2972 	    0xfffff;
2973 	if ((pmem_range[0] & PCI_BCNF_ADDR_MASK) == PCI_BCNF_PF_MEM_64BIT) {
2974 		uint32_t pf_addr_hi, pf_limit_hi;
2975 		pf_addr_hi = pci_getl(bus, dev, func, PCI_BCNF_PF_BASE_HIGH);
2976 		pf_limit_hi = pci_getl(bus, dev, func, PCI_BCNF_PF_LIMIT_HIGH);
2977 		pmem_range[0] |= (uint64_t)pf_addr_hi << 32;
2978 		pmem_range[1] |= (uint64_t)pf_limit_hi << 32;
2979 	}
2980 	if (pmem_range[0] != 0 && pmem_range[0] < pmem_range[1]) {
2981 		memlist_insert(&pci_bus_res[secbus].pmem_avail,
2982 		    (uint64_t)pmem_range[0],
2983 		    (uint64_t)(pmem_range[1] - pmem_range[0] + 1));
2984 		memlist_insert(&pci_bus_res[bus].pmem_used,
2985 		    (uint64_t)pmem_range[0],
2986 		    (uint64_t)(pmem_range[1] - pmem_range[0] + 1));
2987 		/* remove from parent resource list */
2988 		(void) memlist_remove(&pci_bus_res[bus].pmem_avail,
2989 		    (uint64_t)pmem_range[0],
2990 		    (uint64_t)(pmem_range[1] - pmem_range[0] + 1));
2991 		(void) memlist_remove(&pci_bus_res[bus].mem_avail,
2992 		    (uint64_t)pmem_range[0],
2993 		    (uint64_t)(pmem_range[1] - pmem_range[0] + 1));
2994 		dcmn_err(CE_NOTE, "bus %d pmem-range: 0x%" PRIx64 "-%" PRIx64,
2995 		    secbus, pmem_range[0], pmem_range[1]);
2996 	}
2997 
2998 	/*
2999 	 * Add VGA legacy resources to the bridge's pci_bus_res if it
3000 	 * has VGA_ENABLE set.  Note that we put them in 'avail',
3001 	 * because that's used to populate the ranges prop; they'll be
3002 	 * removed from there by the VGA device once it's found.  Also,
3003 	 * remove them from the parent's available list and note them as
3004 	 * used in the parent.
3005 	 */
3006 
3007 	if (pci_getw(bus, dev, func, PCI_BCNF_BCNTRL) &
3008 	    PCI_BCNF_BCNTRL_VGA_ENABLE) {
3009 
3010 		memlist_insert(&pci_bus_res[secbus].io_avail, 0x3b0, 0xc);
3011 
3012 		memlist_insert(&pci_bus_res[bus].io_used, 0x3b0, 0xc);
3013 		if (pci_bus_res[bus].io_avail != NULL) {
3014 			(void) memlist_remove(&pci_bus_res[bus].io_avail,
3015 			    0x3b0, 0xc);
3016 		}
3017 
3018 		memlist_insert(&pci_bus_res[secbus].io_avail, 0x3c0, 0x20);
3019 
3020 		memlist_insert(&pci_bus_res[bus].io_used, 0x3c0, 0x20);
3021 		if (pci_bus_res[bus].io_avail != NULL) {
3022 			(void) memlist_remove(&pci_bus_res[bus].io_avail,
3023 			    0x3c0, 0x20);
3024 		}
3025 
3026 		memlist_insert(&pci_bus_res[secbus].mem_avail, 0xa0000,
3027 		    0x20000);
3028 
3029 		memlist_insert(&pci_bus_res[bus].mem_used, 0xa0000, 0x20000);
3030 		if (pci_bus_res[bus].mem_avail != NULL) {
3031 			(void) memlist_remove(&pci_bus_res[bus].mem_avail,
3032 			    0xa0000, 0x20000);
3033 		}
3034 	}
3035 	add_bus_range_prop(secbus);
3036 	add_ranges_prop(secbus, 1);
3037 }
3038 
3039 extern const struct pci_class_strings_s class_pci[];
3040 extern int class_pci_items;
3041 
3042 static void
3043 add_model_prop(dev_info_t *dip, uint_t classcode)
3044 {
3045 	const char *desc;
3046 	int i;
3047 	uchar_t baseclass = classcode >> 16;
3048 	uchar_t subclass = (classcode >> 8) & 0xff;
3049 	uchar_t progclass = classcode & 0xff;
3050 
3051 	if ((baseclass == PCI_CLASS_MASS) && (subclass == PCI_MASS_IDE)) {
3052 		desc = "IDE controller";
3053 	} else {
3054 		for (desc = 0, i = 0; i < class_pci_items; i++) {
3055 			if ((baseclass == class_pci[i].base_class) &&
3056 			    (subclass == class_pci[i].sub_class) &&
3057 			    (progclass == class_pci[i].prog_class)) {
3058 				desc = class_pci[i].actual_desc;
3059 				break;
3060 			}
3061 		}
3062 		if (i == class_pci_items)
3063 			desc = "Unknown class of pci/pnpbios device";
3064 	}
3065 
3066 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, "model",
3067 	    (char *)desc);
3068 }
3069 
3070 static void
3071 add_bus_range_prop(int bus)
3072 {
3073 	int bus_range[2];
3074 
3075 	if (pci_bus_res[bus].dip == NULL)
3076 		return;
3077 	bus_range[0] = bus;
3078 	bus_range[1] = pci_bus_res[bus].sub_bus;
3079 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, pci_bus_res[bus].dip,
3080 	    "bus-range", (int *)bus_range, 2);
3081 }
3082 
3083 /*
3084  * Add slot-names property for any named pci hot-plug slots
3085  */
3086 static void
3087 add_bus_slot_names_prop(int bus)
3088 {
3089 	char slotprop[256];
3090 	int len;
3091 	extern int pci_irq_nroutes;
3092 	char *slotcap_name;
3093 
3094 	/*
3095 	 * If no irq routing table, then go with the slot-names as set up
3096 	 * in pciex_slot_names_prop() from slot capability register (if any).
3097 	 */
3098 	if (pci_irq_nroutes == 0)
3099 		return;
3100 
3101 	/*
3102 	 * Otherise delete the slot-names we already have and use the irq
3103 	 * routing table values as returned by pci_slot_names_prop() instead,
3104 	 * but keep any property of value "pcie0" as that can't be represented
3105 	 * in the irq routing table.
3106 	 */
3107 	if (pci_bus_res[bus].dip != NULL) {
3108 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, pci_bus_res[bus].dip,
3109 		    DDI_PROP_DONTPASS, "slot-names", &slotcap_name) !=
3110 		    DDI_SUCCESS || strcmp(slotcap_name, "pcie0") != 0)
3111 			(void) ndi_prop_remove(DDI_DEV_T_NONE,
3112 			    pci_bus_res[bus].dip, "slot-names");
3113 	}
3114 
3115 	len = pci_slot_names_prop(bus, slotprop, sizeof (slotprop));
3116 	if (len > 0) {
3117 		/*
3118 		 * Only create a peer bus node if this bus may be a peer bus.
3119 		 * It may be a peer bus if the dip is NULL and if par_bus is
3120 		 * -1 (par_bus is -1 if this bus was not found to be
3121 		 * subordinate to any PCI-PCI bridge).
3122 		 * If it's not a peer bus, then the ACPI BBN-handling code
3123 		 * will remove it later.
3124 		 */
3125 		if (pci_bus_res[bus].par_bus == (uchar_t)-1 &&
3126 		    pci_bus_res[bus].dip == NULL) {
3127 
3128 			create_root_bus_dip(bus);
3129 		}
3130 		if (pci_bus_res[bus].dip != NULL) {
3131 			ASSERT((len % sizeof (int)) == 0);
3132 			(void) ndi_prop_update_int_array(DDI_DEV_T_NONE,
3133 			    pci_bus_res[bus].dip, "slot-names",
3134 			    (int *)slotprop, len / sizeof (int));
3135 		} else {
3136 			cmn_err(CE_NOTE, "!BIOS BUG: Invalid bus number in PCI "
3137 			    "IRQ routing table; Not adding slot-names "
3138 			    "property for incorrect bus %d", bus);
3139 		}
3140 	}
3141 }
3142 
3143 /*
3144  * Handle both PCI root and PCI-PCI bridge range properties;
3145  * non-zero 'ppb' argument select PCI-PCI bridges versus root.
3146  */
3147 static void
3148 memlist_to_ranges(void **rp, struct memlist *entry, uint_t type, int ppb)
3149 {
3150 	ppb_ranges_t *ppb_rp = *rp;
3151 	pci_ranges_t *pci_rp = *rp;
3152 
3153 	while (entry != NULL) {
3154 		uint_t atype = type;
3155 		if ((type & PCI_REG_ADDR_M) == PCI_ADDR_MEM32 &&
3156 		    (entry->ml_address >= UINT32_MAX ||
3157 		    entry->ml_size >= UINT32_MAX)) {
3158 			atype &= ~PCI_ADDR_MEM32;
3159 			atype |= PCI_ADDR_MEM64;
3160 		}
3161 		if (ppb) {
3162 			ppb_rp->child_high = ppb_rp->parent_high = atype;
3163 			ppb_rp->child_mid = ppb_rp->parent_mid =
3164 			    (uint32_t)(entry->ml_address >> 32);
3165 			ppb_rp->child_low = ppb_rp->parent_low =
3166 			    (uint32_t)entry->ml_address;
3167 			ppb_rp->size_high =
3168 			    (uint32_t)(entry->ml_size >> 32);
3169 			ppb_rp->size_low = (uint32_t)entry->ml_size;
3170 			*rp = ++ppb_rp;
3171 		} else {
3172 			pci_rp->child_high = atype;
3173 			pci_rp->child_mid = pci_rp->parent_high =
3174 			    (uint32_t)(entry->ml_address >> 32);
3175 			pci_rp->child_low = pci_rp->parent_low =
3176 			    (uint32_t)entry->ml_address;
3177 			pci_rp->size_high =
3178 			    (uint32_t)(entry->ml_size >> 32);
3179 			pci_rp->size_low = (uint32_t)entry->ml_size;
3180 			*rp = ++pci_rp;
3181 		}
3182 		entry = entry->ml_next;
3183 	}
3184 }
3185 
3186 static void
3187 add_ranges_prop(int bus, int ppb)
3188 {
3189 	int total, alloc_size;
3190 	void	*rp, *next_rp;
3191 	struct memlist *iolist, *memlist, *pmemlist;
3192 
3193 	/* no devinfo node - unused bus, return */
3194 	if (pci_bus_res[bus].dip == NULL)
3195 		return;
3196 
3197 	iolist = memlist = pmemlist = (struct memlist *)NULL;
3198 
3199 	memlist_merge(&pci_bus_res[bus].io_avail, &iolist);
3200 	memlist_merge(&pci_bus_res[bus].io_used, &iolist);
3201 	memlist_merge(&pci_bus_res[bus].mem_avail, &memlist);
3202 	memlist_merge(&pci_bus_res[bus].mem_used, &memlist);
3203 	memlist_merge(&pci_bus_res[bus].pmem_avail, &pmemlist);
3204 	memlist_merge(&pci_bus_res[bus].pmem_used, &pmemlist);
3205 
3206 	total = memlist_count(iolist);
3207 	total += memlist_count(memlist);
3208 	total += memlist_count(pmemlist);
3209 
3210 	/* no property is created if no ranges are present */
3211 	if (total == 0)
3212 		return;
3213 
3214 	alloc_size = total *
3215 	    (ppb ? sizeof (ppb_ranges_t) : sizeof (pci_ranges_t));
3216 
3217 	next_rp = rp = kmem_alloc(alloc_size, KM_SLEEP);
3218 
3219 	memlist_to_ranges(&next_rp, iolist, PCI_ADDR_IO | PCI_REG_REL_M, ppb);
3220 	memlist_to_ranges(&next_rp, memlist,
3221 	    PCI_ADDR_MEM32 | PCI_REG_REL_M, ppb);
3222 	memlist_to_ranges(&next_rp, pmemlist,
3223 	    PCI_ADDR_MEM32 | PCI_REG_REL_M | PCI_REG_PF_M, ppb);
3224 
3225 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, pci_bus_res[bus].dip,
3226 	    "ranges", (int *)rp, alloc_size / sizeof (int));
3227 
3228 	kmem_free(rp, alloc_size);
3229 	memlist_free_all(&iolist);
3230 	memlist_free_all(&memlist);
3231 	memlist_free_all(&pmemlist);
3232 }
3233 
3234 static void
3235 memlist_remove_list(struct memlist **list, struct memlist *remove_list)
3236 {
3237 	while (list && *list && remove_list) {
3238 		(void) memlist_remove(list, remove_list->ml_address,
3239 		    remove_list->ml_size);
3240 		remove_list = remove_list->ml_next;
3241 	}
3242 }
3243 
3244 static int
3245 memlist_to_spec(struct pci_phys_spec *sp, struct memlist *list, int type)
3246 {
3247 	int i = 0;
3248 
3249 	while (list) {
3250 		/* assume 32-bit addresses */
3251 		sp->pci_phys_hi = type;
3252 		sp->pci_phys_mid = 0;
3253 		sp->pci_phys_low = (uint32_t)list->ml_address;
3254 		sp->pci_size_hi = 0;
3255 		sp->pci_size_low = (uint32_t)list->ml_size;
3256 
3257 		list = list->ml_next;
3258 		sp++, i++;
3259 	}
3260 	return (i);
3261 }
3262 
3263 static void
3264 add_bus_available_prop(int bus)
3265 {
3266 	int i, count;
3267 	struct pci_phys_spec *sp;
3268 
3269 	/* no devinfo node - unused bus, return */
3270 	if (pci_bus_res[bus].dip == NULL)
3271 		return;
3272 
3273 	count = memlist_count(pci_bus_res[bus].io_avail) +
3274 	    memlist_count(pci_bus_res[bus].mem_avail) +
3275 	    memlist_count(pci_bus_res[bus].pmem_avail);
3276 
3277 	if (count == 0)		/* nothing available */
3278 		return;
3279 
3280 	sp = kmem_alloc(count * sizeof (*sp), KM_SLEEP);
3281 	i = memlist_to_spec(&sp[0], pci_bus_res[bus].io_avail,
3282 	    PCI_ADDR_IO | PCI_REG_REL_M);
3283 	i += memlist_to_spec(&sp[i], pci_bus_res[bus].mem_avail,
3284 	    PCI_ADDR_MEM32 | PCI_REG_REL_M);
3285 	i += memlist_to_spec(&sp[i], pci_bus_res[bus].pmem_avail,
3286 	    PCI_ADDR_MEM32 | PCI_REG_REL_M | PCI_REG_PF_M);
3287 	ASSERT(i == count);
3288 
3289 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, pci_bus_res[bus].dip,
3290 	    "available", (int *)sp,
3291 	    i * sizeof (struct pci_phys_spec) / sizeof (int));
3292 	kmem_free(sp, count * sizeof (*sp));
3293 }
3294 
3295 static void
3296 alloc_res_array(void)
3297 {
3298 	static int array_size = 0;
3299 	int old_size;
3300 	void *old_res;
3301 
3302 	if (array_size > pci_bios_maxbus + 1)
3303 		return;	/* array is big enough */
3304 
3305 	old_size = array_size;
3306 	old_res = pci_bus_res;
3307 
3308 	if (array_size == 0)
3309 		array_size = 16;	/* start with a reasonable number */
3310 
3311 	while (array_size <= pci_bios_maxbus + 1)
3312 		array_size <<= 1;
3313 	pci_bus_res = (struct pci_bus_resource *)kmem_zalloc(
3314 	    array_size * sizeof (struct pci_bus_resource), KM_SLEEP);
3315 
3316 	if (old_res) {	/* copy content and free old array */
3317 		bcopy(old_res, pci_bus_res,
3318 		    old_size * sizeof (struct pci_bus_resource));
3319 		kmem_free(old_res, old_size * sizeof (struct pci_bus_resource));
3320 	}
3321 }
3322 
3323 static void
3324 create_ioapic_node(int bus, int dev, int fn, ushort_t vendorid,
3325     ushort_t deviceid)
3326 {
3327 	static dev_info_t *ioapicsnode = NULL;
3328 	static int numioapics = 0;
3329 	dev_info_t *ioapic_node;
3330 	uint64_t physaddr;
3331 	uint32_t lobase, hibase = 0;
3332 
3333 	/* BAR 0 contains the IOAPIC's memory-mapped I/O address */
3334 	lobase = (*pci_getl_func)(bus, dev, fn, PCI_CONF_BASE0);
3335 
3336 	/* We (and the rest of the world) only support memory-mapped IOAPICs */
3337 	if ((lobase & PCI_BASE_SPACE_M) != PCI_BASE_SPACE_MEM)
3338 		return;
3339 
3340 	if ((lobase & PCI_BASE_TYPE_M) == PCI_BASE_TYPE_ALL)
3341 		hibase = (*pci_getl_func)(bus, dev, fn, PCI_CONF_BASE0 + 4);
3342 
3343 	lobase &= PCI_BASE_M_ADDR_M;
3344 
3345 	physaddr = (((uint64_t)hibase) << 32) | lobase;
3346 
3347 	/*
3348 	 * Create a nexus node for all IOAPICs under the root node.
3349 	 */
3350 	if (ioapicsnode == NULL) {
3351 		if (ndi_devi_alloc(ddi_root_node(), IOAPICS_NODE_NAME,
3352 		    (pnode_t)DEVI_SID_NODEID, &ioapicsnode) != NDI_SUCCESS) {
3353 			return;
3354 		}
3355 		(void) ndi_devi_online(ioapicsnode, 0);
3356 	}
3357 
3358 	/*
3359 	 * Create a child node for this IOAPIC
3360 	 */
3361 	ioapic_node = ddi_add_child(ioapicsnode, IOAPICS_CHILD_NAME,
3362 	    DEVI_SID_NODEID, numioapics++);
3363 	if (ioapic_node == NULL) {
3364 		return;
3365 	}
3366 
3367 	/* Vendor and Device ID */
3368 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, ioapic_node,
3369 	    IOAPICS_PROP_VENID, vendorid);
3370 	(void) ndi_prop_update_int(DDI_DEV_T_NONE, ioapic_node,
3371 	    IOAPICS_PROP_DEVID, deviceid);
3372 
3373 	/* device_type */
3374 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, ioapic_node,
3375 	    "device_type", IOAPICS_DEV_TYPE);
3376 
3377 	/* reg */
3378 	(void) ndi_prop_update_int64(DDI_DEV_T_NONE, ioapic_node,
3379 	    "reg", physaddr);
3380 }
3381 
3382 /*
3383  * NOTE: For PCIe slots, the name is generated from the slot number
3384  * information obtained from Slot Capabilities register.
3385  * For non-PCIe slots, it is generated based on the slot number
3386  * information in the PCI IRQ table.
3387  */
3388 static void
3389 pciex_slot_names_prop(dev_info_t *dip, ushort_t slot_num)
3390 {
3391 	char slotprop[256];
3392 	int len;
3393 
3394 	bzero(slotprop, sizeof (slotprop));
3395 
3396 	/* set mask to 1 as there is only one slot (i.e dev 0) */
3397 	*(uint32_t *)slotprop = 1;
3398 	len = 4;
3399 	(void) snprintf(slotprop + len, sizeof (slotprop) - len, "pcie%d",
3400 	    slot_num);
3401 	len += strlen(slotprop + len) + 1;
3402 	len += len % 4;
3403 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "slot-names",
3404 	    (int *)slotprop, len / sizeof (int));
3405 }
3406 
3407 /*
3408  * Enable reporting of AER capability next pointer.
3409  * This needs to be done only for CK8-04 devices
3410  * by setting NV_XVR_VEND_CYA1 (offset 0xf40) bit 13
3411  * NOTE: BIOS is disabling this, it needs to be enabled temporarily
3412  *
3413  * This function is adapted from npe_ck804_fix_aer_ptr(), and is
3414  * called from pci_boot.c.
3415  */
3416 static void
3417 ck804_fix_aer_ptr(dev_info_t *dip, pcie_req_id_t bdf)
3418 {
3419 	dev_info_t *rcdip;
3420 	ushort_t cya1;
3421 
3422 	rcdip = pcie_get_rc_dip(dip);
3423 	ASSERT(rcdip != NULL);
3424 
3425 	if ((pci_cfgacc_get16(rcdip, bdf, PCI_CONF_VENID) ==
3426 	    NVIDIA_VENDOR_ID) &&
3427 	    (pci_cfgacc_get16(rcdip, bdf, PCI_CONF_DEVID) ==
3428 	    NVIDIA_CK804_DEVICE_ID) &&
3429 	    (pci_cfgacc_get8(rcdip, bdf, PCI_CONF_REVID) >=
3430 	    NVIDIA_CK804_AER_VALID_REVID)) {
3431 		cya1 = pci_cfgacc_get16(rcdip, bdf, NVIDIA_CK804_VEND_CYA1_OFF);
3432 		if (!(cya1 & ~NVIDIA_CK804_VEND_CYA1_ERPT_MASK))
3433 			(void) pci_cfgacc_put16(rcdip, bdf,
3434 			    NVIDIA_CK804_VEND_CYA1_OFF,
3435 			    cya1 | NVIDIA_CK804_VEND_CYA1_ERPT_VAL);
3436 	}
3437 }
3438