xref: /onnv-gate/usr/src/uts/sun4/io/px/px_util.c (revision 2053:c81a99e1e596)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51501Sgovinda  * Common Development and Distribution License (the "License").
61501Sgovinda  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
221501Sgovinda  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * PCI nexus utility routines:
300Sstevel@tonic-gate  *	property and config routines for attach()
310Sstevel@tonic-gate  *	reg/intr/range/assigned-address property routines for bus_map()
320Sstevel@tonic-gate  *	init_child()
330Sstevel@tonic-gate  *	fault handling
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include <sys/types.h>
370Sstevel@tonic-gate #include <sys/kmem.h>
380Sstevel@tonic-gate #include <sys/async.h>
390Sstevel@tonic-gate #include <sys/sysmacros.h>
400Sstevel@tonic-gate #include <sys/sunddi.h>
410Sstevel@tonic-gate #include <sys/sunndi.h>
420Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
430Sstevel@tonic-gate #include "px_obj.h"
440Sstevel@tonic-gate #include "pcie_pwr.h"
450Sstevel@tonic-gate 
460Sstevel@tonic-gate /*LINTLIBRARY*/
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /*
490Sstevel@tonic-gate  * px_get_props
500Sstevel@tonic-gate  *
510Sstevel@tonic-gate  * This function is called from the attach routine to get the key
520Sstevel@tonic-gate  * properties of the pci nodes.
530Sstevel@tonic-gate  *
540Sstevel@tonic-gate  * used by: px_attach()
550Sstevel@tonic-gate  *
560Sstevel@tonic-gate  * return value: DDI_FAILURE on failure
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate int
590Sstevel@tonic-gate px_get_props(px_t *px_p, dev_info_t *dip)
600Sstevel@tonic-gate {
610Sstevel@tonic-gate 	int i, no_of_intrs;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	/*
640Sstevel@tonic-gate 	 * Get the bus-ranges property.
650Sstevel@tonic-gate 	 */
660Sstevel@tonic-gate 	i = sizeof (px_p->px_bus_range);
670Sstevel@tonic-gate 	if (ddi_getlongprop_buf(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
680Sstevel@tonic-gate 	    "bus-range", (caddr_t)&px_p->px_bus_range, &i) != DDI_SUCCESS) {
690Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: no bus-range property\n",
700Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
710Sstevel@tonic-gate 		return (DDI_FAILURE);
720Sstevel@tonic-gate 	}
730Sstevel@tonic-gate 	DBG(DBG_ATTACH, dip, "get_px_properties: bus-range (%x,%x)\n",
740Sstevel@tonic-gate 		px_p->px_bus_range.lo, px_p->px_bus_range.hi);
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	/*
770Sstevel@tonic-gate 	 * Get the interrupts property.
780Sstevel@tonic-gate 	 */
790Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
800Sstevel@tonic-gate 		"interrupts", (caddr_t)&px_p->px_inos,
810Sstevel@tonic-gate 		&px_p->px_inos_len) != DDI_SUCCESS) {
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: no interrupts property\n",
840Sstevel@tonic-gate 			ddi_driver_name(dip), ddi_get_instance(dip));
850Sstevel@tonic-gate 		return (DDI_FAILURE);
860Sstevel@tonic-gate 	}
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	/*
890Sstevel@tonic-gate 	 * figure out number of interrupts in the "interrupts" property
900Sstevel@tonic-gate 	 * and convert them all into ino.
910Sstevel@tonic-gate 	 */
920Sstevel@tonic-gate 	i = ddi_getprop(DDI_DEV_T_ANY, dip, 0, "#interrupt-cells", 1);
930Sstevel@tonic-gate 	i = CELLS_1275_TO_BYTES(i);
940Sstevel@tonic-gate 	no_of_intrs = px_p->px_inos_len / i;
950Sstevel@tonic-gate 	for (i = 0; i < no_of_intrs; i++)
960Sstevel@tonic-gate 		px_p->px_inos[i] = px_p->px_inos[i] & 0x3F;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	/*
990Sstevel@tonic-gate 	 * Get the ranges property.
1000Sstevel@tonic-gate 	 */
1010Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "ranges",
1020Sstevel@tonic-gate 		(caddr_t)&px_p->px_ranges_p, &px_p->px_ranges_length) !=
1030Sstevel@tonic-gate 		DDI_SUCCESS) {
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: no ranges property\n",
1060Sstevel@tonic-gate 			ddi_driver_name(dip), ddi_get_instance(dip));
1070Sstevel@tonic-gate 		kmem_free(px_p->px_inos, px_p->px_inos_len);
1080Sstevel@tonic-gate 		return (DDI_FAILURE);
1090Sstevel@tonic-gate 	}
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	return (DDI_SUCCESS);
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate /*
1150Sstevel@tonic-gate  * px_free_props:
1160Sstevel@tonic-gate  *
1170Sstevel@tonic-gate  * This routine frees the memory used to cache the "interrupts"
1180Sstevel@tonic-gate  * and "ranges" properties of the pci bus device node.
1190Sstevel@tonic-gate  *
1200Sstevel@tonic-gate  * used by: px_detach()
1210Sstevel@tonic-gate  *
1220Sstevel@tonic-gate  * return value: none
1230Sstevel@tonic-gate  */
1240Sstevel@tonic-gate void
1250Sstevel@tonic-gate px_free_props(px_t *px_p)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate 	kmem_free(px_p->px_inos, px_p->px_inos_len);
1280Sstevel@tonic-gate 	kmem_free(px_p->px_ranges_p, px_p->px_ranges_length);
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate /*
1320Sstevel@tonic-gate  * px_reloc_reg
1330Sstevel@tonic-gate  *
1340Sstevel@tonic-gate  * If the "reg" entry (*px_rp) is relocatable, lookup "assigned-addresses"
1350Sstevel@tonic-gate  * property to fetch corresponding relocated address.
1360Sstevel@tonic-gate  *
1370Sstevel@tonic-gate  * used by: px_map()
1380Sstevel@tonic-gate  *
1390Sstevel@tonic-gate  * return value:
1400Sstevel@tonic-gate  *
1410Sstevel@tonic-gate  *	DDI_SUCCESS		- on success
1420Sstevel@tonic-gate  *	DDI_ME_INVAL		- regspec is invalid
1430Sstevel@tonic-gate  */
1440Sstevel@tonic-gate int
1450Sstevel@tonic-gate px_reloc_reg(dev_info_t *dip, dev_info_t *rdip, px_t *px_p,
1460Sstevel@tonic-gate 	pci_regspec_t *rp)
1470Sstevel@tonic-gate {
1480Sstevel@tonic-gate 	int assign_len, assign_entries, i;
1490Sstevel@tonic-gate 	pci_regspec_t *assign_p;
1500Sstevel@tonic-gate 	uint32_t phys_hi = rp->pci_phys_hi;
1510Sstevel@tonic-gate 	uint32_t space_type = phys_hi & PCI_REG_ADDR_M;	/* 28-bit */
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	DBG(DBG_MAP | DBG_CONT, dip, "\tpx_reloc_reg fr: %x.%x.%x %x.%x\n",
1540Sstevel@tonic-gate 		rp->pci_phys_hi, rp->pci_phys_mid, rp->pci_phys_low,
1550Sstevel@tonic-gate 		rp->pci_size_hi, rp->pci_size_low);
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	if (space_type == PCI_ADDR_CONFIG || phys_hi & PCI_RELOCAT_B)
1580Sstevel@tonic-gate 		return (DDI_SUCCESS);
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	/*
1610Sstevel@tonic-gate 	 * Hot plug will be taken care of later
1620Sstevel@tonic-gate 	 * if (px_p->hotplug_capable == B_FALSE)
1630Sstevel@tonic-gate 	 */
1640Sstevel@tonic-gate 	{
1650Sstevel@tonic-gate 		uint32_t bus = PCI_REG_BUS_G(phys_hi);
1660Sstevel@tonic-gate 		if (bus < px_p->px_bus_range.lo ||
1670Sstevel@tonic-gate 		    bus > px_p->px_bus_range.hi) {
1680Sstevel@tonic-gate 			DBG(DBG_MAP | DBG_CONT, dip, "bad bus# (%x)\n", bus);
1690Sstevel@tonic-gate 			return (DDI_ME_INVAL);
1700Sstevel@tonic-gate 		}
1710Sstevel@tonic-gate 	}
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	i = ddi_getlongprop(DDI_DEV_T_ANY, rdip, DDI_PROP_DONTPASS,
1740Sstevel@tonic-gate 		"assigned-addresses", (caddr_t)&assign_p, &assign_len);
1750Sstevel@tonic-gate 	if (i) {
1760Sstevel@tonic-gate 		DBG(DBG_MAP | DBG_CONT, dip, "%s%d: assigned-addresses %d\n",
1770Sstevel@tonic-gate 			ddi_driver_name(rdip), ddi_get_instance(rdip), i);
1780Sstevel@tonic-gate 		return (DDI_ME_INVAL);
1790Sstevel@tonic-gate 	}
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	assign_entries = assign_len / sizeof (pci_regspec_t);
1820Sstevel@tonic-gate 	for (i = 0; i < assign_entries; i++, assign_p++) {
1830Sstevel@tonic-gate 		uint32_t assign_type = assign_p->pci_phys_hi & PCI_REG_ADDR_M;
1840Sstevel@tonic-gate 		uint32_t assign_addr = PCI_REG_BDFR_G(assign_p->pci_phys_hi);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 		if (PCI_REG_BDFR_G(phys_hi) != assign_addr)
1870Sstevel@tonic-gate 			continue;
1880Sstevel@tonic-gate 		if (space_type == assign_type) { /* exact match */
1890Sstevel@tonic-gate 			rp->pci_phys_low += assign_p->pci_phys_low;
1900Sstevel@tonic-gate 			break;
1910Sstevel@tonic-gate 		}
1920Sstevel@tonic-gate 		if (space_type == PCI_ADDR_MEM64 &&
1930Sstevel@tonic-gate 			assign_type == PCI_ADDR_MEM32) {
1940Sstevel@tonic-gate 			rp->pci_phys_low += assign_p->pci_phys_low;
1950Sstevel@tonic-gate 			rp->pci_phys_hi ^= PCI_ADDR_MEM64 ^ PCI_ADDR_MEM32;
1960Sstevel@tonic-gate 			break;
1970Sstevel@tonic-gate 		}
1980Sstevel@tonic-gate 	}
1990Sstevel@tonic-gate 	kmem_free(assign_p - i, assign_len);
2000Sstevel@tonic-gate 	DBG(DBG_MAP | DBG_CONT, dip, "\tpx_reloc_reg to: %x.%x.%x %x.%x <%d>\n",
2010Sstevel@tonic-gate 		rp->pci_phys_hi, rp->pci_phys_mid, rp->pci_phys_low,
2020Sstevel@tonic-gate 		rp->pci_size_hi, rp->pci_size_low, i);
2030Sstevel@tonic-gate 	return (i < assign_entries ? DDI_SUCCESS : DDI_ME_INVAL);
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate 
206*2053Sschwartz /*
207*2053Sschwartz  * use "ranges" to translate relocated pci regspec into parent space
208*2053Sschwartz  */
2090Sstevel@tonic-gate int
210*2053Sschwartz px_xlate_reg(px_t *px_p, pci_regspec_t *px_rp, struct regspec *new_rp)
2110Sstevel@tonic-gate {
212*2053Sschwartz 	int n;
2130Sstevel@tonic-gate 	px_ranges_t *rng_p = px_p->px_ranges_p;
2140Sstevel@tonic-gate 	int rng_n = px_p->px_ranges_length / sizeof (px_ranges_t);
215*2053Sschwartz 
216*2053Sschwartz 	uint32_t space_type = PCI_REG_ADDR_G(px_rp->pci_phys_hi);
217*2053Sschwartz 	uint32_t reg_end, reg_begin = px_rp->pci_phys_low;
218*2053Sschwartz 	uint32_t sz = px_rp->pci_size_low;
219*2053Sschwartz 
2200Sstevel@tonic-gate 	uint32_t rng_begin, rng_end;
2210Sstevel@tonic-gate 
222*2053Sschwartz 	if (space_type == PCI_REG_ADDR_G(PCI_ADDR_CONFIG)) {
223*2053Sschwartz 		if (reg_begin > PCI_CONF_HDR_SIZE)
224*2053Sschwartz 			return (DDI_ME_INVAL);
225*2053Sschwartz 		sz = sz ? MIN(sz, PCI_CONF_HDR_SIZE) : PCI_CONF_HDR_SIZE;
226*2053Sschwartz 		reg_begin += px_rp->pci_phys_hi << 4;
227*2053Sschwartz 	}
228*2053Sschwartz 	reg_end = reg_begin + sz - 1;
229*2053Sschwartz 
2300Sstevel@tonic-gate 	for (n = 0; n < rng_n; n++, rng_p++) {
2310Sstevel@tonic-gate 		if (space_type != PCI_REG_ADDR_G(rng_p->child_high))
2320Sstevel@tonic-gate 			continue;	/* not the same space type */
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 		rng_begin = rng_p->child_low;
2350Sstevel@tonic-gate 		if (space_type == PCI_REG_ADDR_G(PCI_ADDR_CONFIG))
2360Sstevel@tonic-gate 			rng_begin += rng_p->child_high;
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 		rng_end = rng_begin + rng_p->size_low - 1;
2390Sstevel@tonic-gate 		if (reg_begin >= rng_begin && reg_end <= rng_end)
2400Sstevel@tonic-gate 			break;
2410Sstevel@tonic-gate 	}
2420Sstevel@tonic-gate 	if (n >= rng_n)
2430Sstevel@tonic-gate 		return (DDI_ME_REGSPEC_RANGE);
2440Sstevel@tonic-gate 
245*2053Sschwartz 	new_rp->regspec_addr = reg_begin - rng_begin + rng_p->parent_low;
246*2053Sschwartz 	new_rp->regspec_bustype = rng_p->parent_high;
247*2053Sschwartz 	new_rp->regspec_size = sz;
248*2053Sschwartz 	DBG(DBG_MAP | DBG_CONT, px_p->px_dip,
249*2053Sschwartz 		"\tpx_xlate_reg: entry %d new_rp %x.%x %x\n",
250*2053Sschwartz 		n, new_rp->regspec_bustype, new_rp->regspec_addr, sz);
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	return (DDI_SUCCESS);
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate /*
2560Sstevel@tonic-gate  * px_report_dev
2570Sstevel@tonic-gate  *
2580Sstevel@tonic-gate  * This function is called from our control ops routine on a
2590Sstevel@tonic-gate  * DDI_CTLOPS_REPORTDEV request.
2600Sstevel@tonic-gate  *
2610Sstevel@tonic-gate  * The display format is
2620Sstevel@tonic-gate  *
2630Sstevel@tonic-gate  *	<name><inst> at <pname><pinst> device <dev> function <func>
2640Sstevel@tonic-gate  *
2650Sstevel@tonic-gate  * where
2660Sstevel@tonic-gate  *
2670Sstevel@tonic-gate  *	<name>		this device's name property
2680Sstevel@tonic-gate  *	<inst>		this device's instance number
2690Sstevel@tonic-gate  *	<name>		parent device's name property
2700Sstevel@tonic-gate  *	<inst>		parent device's instance number
2710Sstevel@tonic-gate  *	<dev>		this device's device number
2720Sstevel@tonic-gate  *	<func>		this device's function number
2730Sstevel@tonic-gate  */
2740Sstevel@tonic-gate int
2750Sstevel@tonic-gate px_report_dev(dev_info_t *dip)
2760Sstevel@tonic-gate {
2770Sstevel@tonic-gate 	if (dip == (dev_info_t *)0)
2780Sstevel@tonic-gate 		return (DDI_FAILURE);
2790Sstevel@tonic-gate 	cmn_err(CE_CONT, "?PCI Express-device: %s@%s, %s%d\n",
2800Sstevel@tonic-gate 	    ddi_node_name(dip), ddi_get_name_addr(dip),
2810Sstevel@tonic-gate 	    ddi_driver_name(dip),
2820Sstevel@tonic-gate 	    ddi_get_instance(dip));
2830Sstevel@tonic-gate 	return (DDI_SUCCESS);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate /*
2880Sstevel@tonic-gate  * reg property for pcimem nodes that covers the entire address
2890Sstevel@tonic-gate  * space for the node:  config, io, or memory.
2900Sstevel@tonic-gate  */
2910Sstevel@tonic-gate pci_regspec_t pci_pcimem_reg[3] =
2920Sstevel@tonic-gate {
2930Sstevel@tonic-gate 	{PCI_ADDR_CONFIG,			0, 0, 0, 0x800000	},
2940Sstevel@tonic-gate 	{(uint_t)(PCI_ADDR_IO|PCI_RELOCAT_B),	0, 0, 0, PX_IO_SIZE	},
2950Sstevel@tonic-gate 	{(uint_t)(PCI_ADDR_MEM32|PCI_RELOCAT_B), 0, 0, 0, PX_MEM_SIZE	}
2960Sstevel@tonic-gate };
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate /*
2990Sstevel@tonic-gate  * px_name_child
3000Sstevel@tonic-gate  *
3010Sstevel@tonic-gate  * This function is called from init_child to name a node. It is
3020Sstevel@tonic-gate  * also passed as a callback for node merging functions.
3030Sstevel@tonic-gate  *
3040Sstevel@tonic-gate  * return value: DDI_SUCCESS, DDI_FAILURE
3050Sstevel@tonic-gate  */
3060Sstevel@tonic-gate static int
3070Sstevel@tonic-gate px_name_child(dev_info_t *child, char *name, int namelen)
3080Sstevel@tonic-gate {
3090Sstevel@tonic-gate 	pci_regspec_t *pci_rp;
3100Sstevel@tonic-gate 	int reglen;
3110Sstevel@tonic-gate 	uint_t func;
3120Sstevel@tonic-gate 	char **unit_addr;
3130Sstevel@tonic-gate 	uint_t n;
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	/*
3160Sstevel@tonic-gate 	 * Set the address portion of the node name based on
3170Sstevel@tonic-gate 	 * unit-address property, if it exists.
3180Sstevel@tonic-gate 	 * The interpretation of the unit-address is DD[,F]
3190Sstevel@tonic-gate 	 * where DD is the device id and F is the function.
3200Sstevel@tonic-gate 	 */
3210Sstevel@tonic-gate 	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, child,
3220Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "unit-address", &unit_addr, &n) ==
3230Sstevel@tonic-gate 	    DDI_PROP_SUCCESS) {
3240Sstevel@tonic-gate 		if (n != 1 || *unit_addr == NULL || **unit_addr == 0) {
3250Sstevel@tonic-gate 			cmn_err(CE_WARN, "unit-address property in %s.conf"
3260Sstevel@tonic-gate 			    " not well-formed", ddi_driver_name(child));
3270Sstevel@tonic-gate 			ddi_prop_free(unit_addr);
3280Sstevel@tonic-gate 			return (DDI_FAILURE);
3290Sstevel@tonic-gate 		}
3300Sstevel@tonic-gate 		(void) snprintf(name, namelen, "%s", *unit_addr);
3310Sstevel@tonic-gate 		ddi_prop_free(unit_addr);
3320Sstevel@tonic-gate 		return (DDI_SUCCESS);
3330Sstevel@tonic-gate 	}
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	/*
3360Sstevel@tonic-gate 	 * The unit-address property is does not exist. Set the address
3370Sstevel@tonic-gate 	 * portion of the node name based on the function and device number.
3380Sstevel@tonic-gate 	 */
3390Sstevel@tonic-gate 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
3400Sstevel@tonic-gate 	    "reg", (int **)&pci_rp, (uint_t *)&reglen) == DDI_SUCCESS) {
3410Sstevel@tonic-gate 		if (((reglen * sizeof (int)) % sizeof (pci_regspec_t)) != 0) {
3420Sstevel@tonic-gate 			cmn_err(CE_WARN, "reg property not well-formed");
3430Sstevel@tonic-gate 			return (DDI_FAILURE);
3440Sstevel@tonic-gate 		}
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 		func = PCI_REG_FUNC_G(pci_rp[0].pci_phys_hi);
3470Sstevel@tonic-gate 		if (func != 0)
3480Sstevel@tonic-gate 			(void) snprintf(name, namelen, "%x,%x",
3490Sstevel@tonic-gate 				PCI_REG_DEV_G(pci_rp[0].pci_phys_hi), func);
3500Sstevel@tonic-gate 		else
3510Sstevel@tonic-gate 			(void) snprintf(name, namelen, "%x",
3520Sstevel@tonic-gate 				PCI_REG_DEV_G(pci_rp[0].pci_phys_hi));
3530Sstevel@tonic-gate 		ddi_prop_free(pci_rp);
3540Sstevel@tonic-gate 		return (DDI_SUCCESS);
3550Sstevel@tonic-gate 	}
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	cmn_err(CE_WARN, "cannot name pci child '%s'", ddi_node_name(child));
3580Sstevel@tonic-gate 	return (DDI_FAILURE);
3590Sstevel@tonic-gate }
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate int
3620Sstevel@tonic-gate px_uninit_child(px_t *px_p, dev_info_t *child)
3630Sstevel@tonic-gate {
3640Sstevel@tonic-gate 	DBG(DBG_INIT_CLD, px_p->px_dip,
3650Sstevel@tonic-gate 	    "DDI_CTLOPS_UNINITCHILD: arg=%s%d\n",
3660Sstevel@tonic-gate 	    ddi_driver_name(child), ddi_get_instance(child));
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	ddi_set_name_addr(child, NULL);
3690Sstevel@tonic-gate 	ddi_remove_minor_node(child, NULL);
3700Sstevel@tonic-gate 	impl_rem_dev_props(child);
37127Sjchu 
37227Sjchu 	DBG(DBG_PWR, ddi_get_parent(child), "\n\n");
3730Sstevel@tonic-gate 
37427Sjchu 	pcie_uninitchild(child);
37527Sjchu 
37627Sjchu 	return (DDI_SUCCESS);
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate /*
3800Sstevel@tonic-gate  * px_init_child
3810Sstevel@tonic-gate  *
3820Sstevel@tonic-gate  * This function is called from our control ops routine on a
3830Sstevel@tonic-gate  * DDI_CTLOPS_INITCHILD request.  It builds and sets the device's
3840Sstevel@tonic-gate  * parent private data area.
3850Sstevel@tonic-gate  *
3860Sstevel@tonic-gate  * used by: pci_ctlops()
3870Sstevel@tonic-gate  *
3880Sstevel@tonic-gate  * return value: none
3890Sstevel@tonic-gate  */
3900Sstevel@tonic-gate int
3910Sstevel@tonic-gate px_init_child(px_t *px_p, dev_info_t *child)
3920Sstevel@tonic-gate {
39327Sjchu 	dev_info_t	*parent_dip = px_p->px_dip;
39427Sjchu 	pci_regspec_t	*pci_rp;
39527Sjchu 	char		name[10];
39627Sjchu 	int		i, no_config;
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	/*
3990Sstevel@tonic-gate 	 * The following is a special case for pcimem nodes.
4000Sstevel@tonic-gate 	 * For these nodes we create a reg property with a
4010Sstevel@tonic-gate 	 * single entry that covers the entire address space
4020Sstevel@tonic-gate 	 * for the node (config, io or memory).
4030Sstevel@tonic-gate 	 */
4040Sstevel@tonic-gate 	if (strcmp(ddi_driver_name(child), "pcimem") == 0) {
4050Sstevel@tonic-gate 		(void) ddi_prop_create(DDI_DEV_T_NONE, child,
4060Sstevel@tonic-gate 		    DDI_PROP_CANSLEEP, "reg", (caddr_t)pci_pcimem_reg,
4070Sstevel@tonic-gate 		    sizeof (pci_pcimem_reg));
4080Sstevel@tonic-gate 		ddi_set_name_addr(child, "0");
4090Sstevel@tonic-gate 		ddi_set_parent_data(child, NULL);
4100Sstevel@tonic-gate 		return (DDI_SUCCESS);
4110Sstevel@tonic-gate 	}
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	/*
4140Sstevel@tonic-gate 	 * Check whether the node has config space or is a hard decode
4150Sstevel@tonic-gate 	 * node (possibly created by a driver.conf file).
4160Sstevel@tonic-gate 	 */
4170Sstevel@tonic-gate 	no_config = ddi_prop_get_int(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
4180Sstevel@tonic-gate 	    "no-config", 0);
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	/*
4210Sstevel@tonic-gate 	 * Pseudo nodes indicate a prototype node with per-instance
4220Sstevel@tonic-gate 	 * properties to be merged into the real h/w device node.
4230Sstevel@tonic-gate 	 * However, do not merge if the no-config property is set
4240Sstevel@tonic-gate 	 * (see PSARC 2000/088).
4250Sstevel@tonic-gate 	 */
4260Sstevel@tonic-gate 	if ((ndi_dev_is_persistent_node(child) == 0) && (no_config == 0)) {
4270Sstevel@tonic-gate 		extern int pci_allow_pseudo_children;
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 		if (ddi_getlongprop(DDI_DEV_T_ANY, child,
4300Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "reg", (caddr_t)&pci_rp, &i) ==
4310Sstevel@tonic-gate 		    DDI_SUCCESS) {
4320Sstevel@tonic-gate 			cmn_err(CE_WARN, "cannot merge prototype from %s.conf",
4330Sstevel@tonic-gate 			    ddi_driver_name(child));
4340Sstevel@tonic-gate 			kmem_free(pci_rp, i);
4350Sstevel@tonic-gate 			return (DDI_NOT_WELL_FORMED);
4360Sstevel@tonic-gate 		}
4370Sstevel@tonic-gate 		/*
4380Sstevel@tonic-gate 		 * Name the child
4390Sstevel@tonic-gate 		 */
4400Sstevel@tonic-gate 		if (px_name_child(child, name, 10) != DDI_SUCCESS)
4410Sstevel@tonic-gate 			return (DDI_FAILURE);
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 		ddi_set_name_addr(child, name);
4440Sstevel@tonic-gate 		ddi_set_parent_data(child, NULL);
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 		/*
4470Sstevel@tonic-gate 		 * Try to merge the properties from this prototype
4480Sstevel@tonic-gate 		 * node into real h/w nodes.
4490Sstevel@tonic-gate 		 */
4500Sstevel@tonic-gate 		if (ndi_merge_node(child, px_name_child) == DDI_SUCCESS) {
4510Sstevel@tonic-gate 			/*
4520Sstevel@tonic-gate 			 * Merged ok - return failure to remove the node.
4530Sstevel@tonic-gate 			 */
4540Sstevel@tonic-gate 			ddi_set_name_addr(child, NULL);
4550Sstevel@tonic-gate 			return (DDI_FAILURE);
4560Sstevel@tonic-gate 		}
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 		/* workaround for ddivs to run under PCI */
4590Sstevel@tonic-gate 		if (pci_allow_pseudo_children)
4600Sstevel@tonic-gate 			return (DDI_SUCCESS);
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 		cmn_err(CE_WARN, "!%s@%s: %s.conf properties not merged",
4630Sstevel@tonic-gate 		    ddi_driver_name(child), ddi_get_name_addr(child),
4640Sstevel@tonic-gate 		    ddi_driver_name(child));
4650Sstevel@tonic-gate 		ddi_set_name_addr(child, NULL);
4660Sstevel@tonic-gate 		return (DDI_NOT_WELL_FORMED);
4670Sstevel@tonic-gate 	}
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	if (px_name_child(child, name, 10) != DDI_SUCCESS)
4700Sstevel@tonic-gate 		return (DDI_FAILURE);
4710Sstevel@tonic-gate 	ddi_set_name_addr(child, name);
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	if (no_config != 0) {
4740Sstevel@tonic-gate 		/*
4750Sstevel@tonic-gate 		 * There is no config space so there's nothing more to do.
4760Sstevel@tonic-gate 		 */
4770Sstevel@tonic-gate 		return (DDI_SUCCESS);
4780Sstevel@tonic-gate 	}
4790Sstevel@tonic-gate 
48027Sjchu 	if (pcie_pm_hold(parent_dip) != DDI_SUCCESS) {
48127Sjchu 		DBG(DBG_PWR, parent_dip,
4820Sstevel@tonic-gate 		    "INITCHILD: px_pm_hold failed\n");
4830Sstevel@tonic-gate 		return (DDI_FAILURE);
4840Sstevel@tonic-gate 	}
4850Sstevel@tonic-gate 	/* Any return of DDI_FAILURE after this must call px_pm_release */
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	/*
4880Sstevel@tonic-gate 	 * If configuration registers were previously saved by
4890Sstevel@tonic-gate 	 * child (before it went to D3), then let the child do the
4900Sstevel@tonic-gate 	 * restore to set up the config regs as it'll first need to
4910Sstevel@tonic-gate 	 * power the device out of D3.
4920Sstevel@tonic-gate 	 */
4930Sstevel@tonic-gate 	if (ddi_prop_exists(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
4940Sstevel@tonic-gate 	    "config-regs-saved-by-child") == 1) {
4950Sstevel@tonic-gate 		DBG(DBG_PWR, child,
4960Sstevel@tonic-gate 		    "INITCHILD: config regs to be restored by child\n");
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 		return (DDI_SUCCESS);
4990Sstevel@tonic-gate 	}
5000Sstevel@tonic-gate 
50127Sjchu 	DBG(DBG_PWR, parent_dip,
5020Sstevel@tonic-gate 	    "INITCHILD: config regs setup for %s@%s\n",
5030Sstevel@tonic-gate 	    ddi_node_name(child), ddi_get_name_addr(child));
5040Sstevel@tonic-gate 
50527Sjchu 	pcie_initchild(child);
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	/*
5080Sstevel@tonic-gate 	 * Handle chip specific init-child tasks.
5090Sstevel@tonic-gate 	 */
51027Sjchu 	pcie_pm_release(parent_dip);
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	return (DDI_SUCCESS);
5130Sstevel@tonic-gate }
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate /*
5160Sstevel@tonic-gate  * px_get_reg_set_size
5170Sstevel@tonic-gate  *
5180Sstevel@tonic-gate  * Given a dev info pointer to a pci child and a register number, this
5190Sstevel@tonic-gate  * routine returns the size element of that reg set property.
5200Sstevel@tonic-gate  *
5210Sstevel@tonic-gate  * used by: pci_ctlops() - DDI_CTLOPS_REGSIZE
5220Sstevel@tonic-gate  *
52327Sjchu  * return value: size of reg set on success, 0 on error
5240Sstevel@tonic-gate  */
5250Sstevel@tonic-gate off_t
5260Sstevel@tonic-gate px_get_reg_set_size(dev_info_t *child, int rnumber)
5270Sstevel@tonic-gate {
5280Sstevel@tonic-gate 	pci_regspec_t *pci_rp;
52927Sjchu 	off_t size = 0;
5300Sstevel@tonic-gate 	int i;
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	if (rnumber < 0)
53327Sjchu 		return (0);
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	/*
5360Sstevel@tonic-gate 	 * Get the reg property for the device.
5370Sstevel@tonic-gate 	 */
5380Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS, "reg",
5390Sstevel@tonic-gate 	    (caddr_t)&pci_rp, &i) != DDI_SUCCESS)
54027Sjchu 		return (0);
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	if (rnumber >= (i / (int)sizeof (pci_regspec_t)))
5430Sstevel@tonic-gate 		goto done;
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	size = pci_rp[rnumber].pci_size_low |
5460Sstevel@tonic-gate 		((uint64_t)pci_rp[rnumber].pci_size_hi << 32);
5470Sstevel@tonic-gate done:
5480Sstevel@tonic-gate 	kmem_free(pci_rp, i);
5490Sstevel@tonic-gate 	return (size);
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate /*
5540Sstevel@tonic-gate  * px_get_nreg_set
5550Sstevel@tonic-gate  *
5560Sstevel@tonic-gate  * Given a dev info pointer to a pci child, this routine returns the
5570Sstevel@tonic-gate  * number of sets in its "reg" property.
5580Sstevel@tonic-gate  *
5590Sstevel@tonic-gate  * used by: pci_ctlops() - DDI_CTLOPS_NREGS
5600Sstevel@tonic-gate  *
5610Sstevel@tonic-gate  * return value: # of reg sets on success, zero on error
5620Sstevel@tonic-gate  */
5630Sstevel@tonic-gate uint_t
5640Sstevel@tonic-gate px_get_nreg_set(dev_info_t *child)
5650Sstevel@tonic-gate {
5660Sstevel@tonic-gate 	pci_regspec_t *pci_rp;
5670Sstevel@tonic-gate 	int i, n;
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	/*
5700Sstevel@tonic-gate 	 * Get the reg property for the device.
5710Sstevel@tonic-gate 	 */
5720Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS, "reg",
5730Sstevel@tonic-gate 	    (caddr_t)&pci_rp, &i) != DDI_SUCCESS)
5740Sstevel@tonic-gate 		return (0);
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	n = i / (int)sizeof (pci_regspec_t);
5770Sstevel@tonic-gate 	kmem_free(pci_rp, i);
5780Sstevel@tonic-gate 	return (n);
5790Sstevel@tonic-gate }
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate /*
5830Sstevel@tonic-gate  * px_get_nintr
5840Sstevel@tonic-gate  *
5850Sstevel@tonic-gate  * Given a dev info pointer to a pci child, this routine returns the
5860Sstevel@tonic-gate  * number of items in its "interrupts" property.
5870Sstevel@tonic-gate  *
5880Sstevel@tonic-gate  * used by: pci_ctlops() - DDI_CTLOPS_NREGS
5890Sstevel@tonic-gate  *
5900Sstevel@tonic-gate  * return value: # of interrupts on success, zero on error
5910Sstevel@tonic-gate  */
5920Sstevel@tonic-gate uint_t
5930Sstevel@tonic-gate px_get_nintr(dev_info_t *child)
5940Sstevel@tonic-gate {
5950Sstevel@tonic-gate 	int *pci_ip;
5960Sstevel@tonic-gate 	int i, n;
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
5990Sstevel@tonic-gate 	    "interrupts", (caddr_t)&pci_ip, &i) != DDI_SUCCESS)
6000Sstevel@tonic-gate 		return (0);
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	n = i / (int)sizeof (uint_t);
6030Sstevel@tonic-gate 	kmem_free(pci_ip, i);
6040Sstevel@tonic-gate 	return (n);
6050Sstevel@tonic-gate }
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate uint64_t
6080Sstevel@tonic-gate px_get_cfg_pabase(px_t *px_p)
6090Sstevel@tonic-gate {
6100Sstevel@tonic-gate 	int i;
6110Sstevel@tonic-gate 	px_ranges_t *rangep = px_p->px_ranges_p;
6120Sstevel@tonic-gate 	int nrange = px_p->px_ranges_length / sizeof (px_ranges_t);
6130Sstevel@tonic-gate 	uint32_t cfg_space_type = PCI_REG_ADDR_G(PCI_ADDR_CONFIG);
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	ASSERT(cfg_space_type == 0);
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	for (i = 0; i < nrange; i++, rangep++) {
6180Sstevel@tonic-gate 		if (PCI_REG_ADDR_G(rangep->child_high) == cfg_space_type)
6190Sstevel@tonic-gate 			break;
6200Sstevel@tonic-gate 	}
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	if (i >= nrange)
623671Skrishnae 		cmn_err(CE_PANIC, "no cfg space in px(%p) ranges prop.\n",
624671Skrishnae 			px_p);
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	return (((uint64_t)rangep->parent_high << 32) | rangep->parent_low);
6270Sstevel@tonic-gate }
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate /*
6300Sstevel@tonic-gate  * decodes standard PCI config space 16bit error status reg
6310Sstevel@tonic-gate  */
6320Sstevel@tonic-gate int
6330Sstevel@tonic-gate px_log_cfg_err(dev_info_t *dip, ushort_t status_reg, char *err_msg)
6340Sstevel@tonic-gate {
6350Sstevel@tonic-gate 	int nerr = ddi_get_instance(dip); /* temp for instance */
6360Sstevel@tonic-gate 	uint64_t perr_fatal = px_perr_fatal & (1 << nerr);
6370Sstevel@tonic-gate 	uint64_t serr_fatal = px_serr_fatal & (1 << nerr);
6380Sstevel@tonic-gate 	nerr = 0;
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	if ((status_reg & PCI_STAT_PERROR) && perr_fatal)
6410Sstevel@tonic-gate 		nerr++;
6420Sstevel@tonic-gate 	if ((status_reg & PCI_STAT_S_SYSERR) && serr_fatal)
6430Sstevel@tonic-gate 		nerr++;
6440Sstevel@tonic-gate 	if (status_reg & PCI_STAT_R_MAST_AB)
6450Sstevel@tonic-gate 		nerr++;
6460Sstevel@tonic-gate 	if ((status_reg & PCI_STAT_S_PERROR) && perr_fatal)
6470Sstevel@tonic-gate 		nerr++;
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	cmn_err(CE_WARN, "%s%d: %sPCI Express config space CSR=0x%b",
6500Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), err_msg,
6510Sstevel@tonic-gate 	    (uint32_t)status_reg, PX_STATUS_BITS);
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	return (nerr);
6540Sstevel@tonic-gate }
6551501Sgovinda 
6561501Sgovinda /*
6571501Sgovinda  * This is a software workaround to fix the Broadcom PCIe-PCI bridge
6581501Sgovinda  * prefetch bug. Existence of a "cross-page-prefetch" property in the
6591501Sgovinda  * child or parent node means the px nexus driver has to allocate an
6601501Sgovinda  * extra page and make it valid one, for any DVMA request that comes
6611501Sgovinda  * from any of the Broadcom child device.
6621501Sgovinda  */
6631501Sgovinda boolean_t
6641501Sgovinda px_child_prefetch(dev_info_t *child)
6651501Sgovinda {
6661501Sgovinda 	if (ddi_prop_exists(DDI_DEV_T_ANY, child, DDI_PROP_NOTPROM,
6671501Sgovinda 	    "cross-page-prefetch"))
6681501Sgovinda 		return (B_TRUE);
6691501Sgovinda 
6701501Sgovinda 	return (B_FALSE);
6711501Sgovinda }
672