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