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