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