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
5*5084Sjohnlev * Common Development and Distribution License (the "License").
6*5084Sjohnlev * 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 /*
22*5084Sjohnlev * Copyright 2007 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 * This file contains ddi functions common to intel architectures
300Sstevel@tonic-gate */
310Sstevel@tonic-gate
320Sstevel@tonic-gate #include <sys/archsystm.h>
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/dditypes.h>
350Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
360Sstevel@tonic-gate #include <sys/sunddi.h>
370Sstevel@tonic-gate #include <sys/cpu.h>
380Sstevel@tonic-gate
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate * DDI Mapping
410Sstevel@tonic-gate */
420Sstevel@tonic-gate
430Sstevel@tonic-gate /*
440Sstevel@tonic-gate * i_ddi_bus_map:
450Sstevel@tonic-gate * Generic bus_map entry point, for byte addressable devices
460Sstevel@tonic-gate * conforming to the reg/range addressing model with no HAT layer
470Sstevel@tonic-gate * to be programmed at this level.
480Sstevel@tonic-gate */
490Sstevel@tonic-gate
500Sstevel@tonic-gate int
i_ddi_bus_map(dev_info_t * dip,dev_info_t * rdip,ddi_map_req_t * mp,off_t offset,off_t len,caddr_t * vaddrp)510Sstevel@tonic-gate i_ddi_bus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
520Sstevel@tonic-gate off_t offset, off_t len, caddr_t *vaddrp)
530Sstevel@tonic-gate {
540Sstevel@tonic-gate struct regspec tmp_reg, *rp;
550Sstevel@tonic-gate ddi_map_req_t mr = *mp; /* Get private copy of request */
560Sstevel@tonic-gate int error;
570Sstevel@tonic-gate
580Sstevel@tonic-gate mp = &mr;
590Sstevel@tonic-gate
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate * First, if given an rnumber, convert it to a regspec...
620Sstevel@tonic-gate */
630Sstevel@tonic-gate
640Sstevel@tonic-gate if (mp->map_type == DDI_MT_RNUMBER) {
650Sstevel@tonic-gate
660Sstevel@tonic-gate int rnumber = mp->map_obj.rnumber;
670Sstevel@tonic-gate #ifdef DDI_MAP_DEBUG
680Sstevel@tonic-gate static char *out_of_range =
690Sstevel@tonic-gate "i_ddi_bus_map: Out of range rnumber <%d>, device <%s>";
700Sstevel@tonic-gate #endif /* DDI_MAP_DEBUG */
710Sstevel@tonic-gate
720Sstevel@tonic-gate rp = i_ddi_rnumber_to_regspec(rdip, rnumber);
730Sstevel@tonic-gate if (rp == (struct regspec *)0) {
740Sstevel@tonic-gate #ifdef DDI_MAP_DEBUG
750Sstevel@tonic-gate cmn_err(CE_WARN, out_of_range, rnumber,
760Sstevel@tonic-gate ddi_get_name(rdip));
770Sstevel@tonic-gate #endif /* DDI_MAP_DEBUG */
780Sstevel@tonic-gate return (DDI_ME_RNUMBER_RANGE);
790Sstevel@tonic-gate }
800Sstevel@tonic-gate
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate * Convert the given ddi_map_req_t from rnumber to regspec...
830Sstevel@tonic-gate */
840Sstevel@tonic-gate
850Sstevel@tonic-gate mp->map_type = DDI_MT_REGSPEC;
860Sstevel@tonic-gate mp->map_obj.rp = rp;
870Sstevel@tonic-gate }
880Sstevel@tonic-gate
890Sstevel@tonic-gate /*
900Sstevel@tonic-gate * Adjust offset and length correspnding to called values...
910Sstevel@tonic-gate * XXX: A non-zero length means override the one in the regspec.
920Sstevel@tonic-gate * XXX: (Regardless of what's in the parent's range)
930Sstevel@tonic-gate */
940Sstevel@tonic-gate
950Sstevel@tonic-gate tmp_reg = *(mp->map_obj.rp); /* Preserve underlying data */
960Sstevel@tonic-gate rp = mp->map_obj.rp = &tmp_reg; /* Use tmp_reg in request */
970Sstevel@tonic-gate
980Sstevel@tonic-gate #ifdef DDI_MAP_DEBUG
990Sstevel@tonic-gate cmn_err(CE_CONT,
100*5084Sjohnlev "i_ddi_bus_map: <%s,%s> <0x%x, 0x%x, 0x%d> "
101*5084Sjohnlev "offset %d len %d handle 0x%x\n",
102*5084Sjohnlev ddi_get_name(dip), ddi_get_name(rdip),
103*5084Sjohnlev rp->regspec_bustype, rp->regspec_addr, rp->regspec_size,
104*5084Sjohnlev offset, len, mp->map_handlep);
1050Sstevel@tonic-gate #endif /* DDI_MAP_DEBUG */
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate /*
1080Sstevel@tonic-gate * I/O or memory mapping
1090Sstevel@tonic-gate *
1100Sstevel@tonic-gate * <bustype=0, addr=x, len=x>: memory
1110Sstevel@tonic-gate * <bustype=1, addr=x, len=x>: i/o
1120Sstevel@tonic-gate * <bustype>1, addr=0, len=x>: x86-compatibility i/o
1130Sstevel@tonic-gate */
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate if (rp->regspec_bustype > 1 && rp->regspec_addr != 0) {
1160Sstevel@tonic-gate cmn_err(CE_WARN, "<%s,%s>: invalid register spec"
1170Sstevel@tonic-gate " <0x%x, 0x%x, 0x%x>\n", ddi_get_name(dip),
1180Sstevel@tonic-gate ddi_get_name(rdip), rp->regspec_bustype,
1190Sstevel@tonic-gate rp->regspec_addr, rp->regspec_size);
1200Sstevel@tonic-gate return (DDI_ME_INVAL);
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate if (rp->regspec_bustype > 1 && rp->regspec_addr == 0) {
1240Sstevel@tonic-gate /*
1250Sstevel@tonic-gate * compatibility i/o mapping
1260Sstevel@tonic-gate */
1270Sstevel@tonic-gate rp->regspec_bustype += (uint_t)offset;
1280Sstevel@tonic-gate } else {
1290Sstevel@tonic-gate /*
1300Sstevel@tonic-gate * Normal memory or i/o mapping
1310Sstevel@tonic-gate */
1320Sstevel@tonic-gate rp->regspec_addr += (uint_t)offset;
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate if (len != 0)
1360Sstevel@tonic-gate rp->regspec_size = (uint_t)len;
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate #ifdef DDI_MAP_DEBUG
1390Sstevel@tonic-gate cmn_err(CE_CONT,
140*5084Sjohnlev " <%s,%s> <0x%x, 0x%x, 0x%d> "
141*5084Sjohnlev "offset %d len %d\n",
142*5084Sjohnlev ddi_get_name(dip), ddi_get_name(rdip),
143*5084Sjohnlev rp->regspec_bustype, rp->regspec_addr, rp->regspec_size,
144*5084Sjohnlev offset, len);
1450Sstevel@tonic-gate #endif /* DDI_MAP_DEBUG */
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate /*
1480Sstevel@tonic-gate * If we had an MMU, this is where you'd program the MMU and hat layer.
1490Sstevel@tonic-gate * Since we're using the default function here, we do not have an MMU
1500Sstevel@tonic-gate * to program.
1510Sstevel@tonic-gate */
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate /*
1540Sstevel@tonic-gate * Apply any parent ranges at this level, if applicable.
1550Sstevel@tonic-gate * (This is where nexus specific regspec translation takes place.
1560Sstevel@tonic-gate * Use of this function is implicit agreement that translation is
1570Sstevel@tonic-gate * provided via ddi_apply_range.) Note that we assume that
1580Sstevel@tonic-gate * the request is within the parents limits.
1590Sstevel@tonic-gate */
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate #ifdef DDI_MAP_DEBUG
1620Sstevel@tonic-gate ddi_map_debug("applying range of parent <%s> to child <%s>...\n",
1630Sstevel@tonic-gate ddi_get_name(dip), ddi_get_name(rdip));
1640Sstevel@tonic-gate #endif /* DDI_MAP_DEBUG */
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate if ((error = i_ddi_apply_range(dip, rdip, mp->map_obj.rp)) != 0)
1670Sstevel@tonic-gate return (error);
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate /*
1700Sstevel@tonic-gate * Call my parents bus_map function with modified values...
1710Sstevel@tonic-gate */
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate return (ddi_map(dip, mp, (off_t)0, (off_t)0, vaddrp));
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate /*
1770Sstevel@tonic-gate * Creating register mappings and handling interrupts:
1780Sstevel@tonic-gate */
1790Sstevel@tonic-gate
1800Sstevel@tonic-gate struct regspec *
i_ddi_rnumber_to_regspec(dev_info_t * dip,int rnumber)1810Sstevel@tonic-gate i_ddi_rnumber_to_regspec(dev_info_t *dip, int rnumber)
1820Sstevel@tonic-gate {
1830Sstevel@tonic-gate if (rnumber >= sparc_pd_getnreg(DEVI(dip)))
1840Sstevel@tonic-gate return ((struct regspec *)0);
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate return (sparc_pd_getreg(DEVI(dip), rnumber));
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate /*
1900Sstevel@tonic-gate * Static function to determine if a reg prop is enclosed within
1910Sstevel@tonic-gate * a given a range spec. (For readability: only used by i_ddi_aply_range.).
1920Sstevel@tonic-gate */
1930Sstevel@tonic-gate static int
reg_is_enclosed_in_range(struct regspec * rp,struct rangespec * rangep)1940Sstevel@tonic-gate reg_is_enclosed_in_range(struct regspec *rp, struct rangespec *rangep)
1950Sstevel@tonic-gate {
1960Sstevel@tonic-gate if (rp->regspec_bustype != rangep->rng_cbustype)
1970Sstevel@tonic-gate return (0);
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate if (rp->regspec_addr < rangep->rng_coffset)
2000Sstevel@tonic-gate return (0);
2010Sstevel@tonic-gate
2020Sstevel@tonic-gate if (rangep->rng_size == 0)
2030Sstevel@tonic-gate return (1); /* size is really 2**(bits_per_word) */
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate if ((rp->regspec_addr + rp->regspec_size - 1) <=
2060Sstevel@tonic-gate (rangep->rng_coffset + rangep->rng_size - 1))
2070Sstevel@tonic-gate return (1);
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate return (0);
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate /*
2130Sstevel@tonic-gate * i_ddi_apply_range:
2140Sstevel@tonic-gate * Apply range of dp to struct regspec *rp, if applicable.
2150Sstevel@tonic-gate * If there's any range defined, it gets applied.
2160Sstevel@tonic-gate */
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate int
i_ddi_apply_range(dev_info_t * dp,dev_info_t * rdip,struct regspec * rp)2190Sstevel@tonic-gate i_ddi_apply_range(dev_info_t *dp, dev_info_t *rdip, struct regspec *rp)
2200Sstevel@tonic-gate {
2210Sstevel@tonic-gate int nrange, b;
2220Sstevel@tonic-gate struct rangespec *rangep;
2230Sstevel@tonic-gate static char *out_of_range =
2240Sstevel@tonic-gate "Out of range register specification from device node <%s>\n";
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate nrange = sparc_pd_getnrng(dp);
2270Sstevel@tonic-gate if (nrange == 0) {
2280Sstevel@tonic-gate #ifdef DDI_MAP_DEBUG
2290Sstevel@tonic-gate ddi_map_debug(" No range.\n");
2300Sstevel@tonic-gate #endif /* DDI_MAP_DEBUG */
2310Sstevel@tonic-gate return (0);
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate
2340Sstevel@tonic-gate /*
2350Sstevel@tonic-gate * Find a match, making sure the regspec is within the range
2360Sstevel@tonic-gate * of the parent, noting that a size of zero in a range spec
2370Sstevel@tonic-gate * really means a size of 2**(bitsperword).
2380Sstevel@tonic-gate */
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate for (b = 0, rangep = sparc_pd_getrng(dp, 0); b < nrange; ++b, ++rangep)
2410Sstevel@tonic-gate if (reg_is_enclosed_in_range(rp, rangep))
2420Sstevel@tonic-gate break; /* found a match */
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate if (b == nrange) {
2450Sstevel@tonic-gate cmn_err(CE_WARN, out_of_range, ddi_get_name(rdip));
2460Sstevel@tonic-gate return (DDI_ME_REGSPEC_RANGE);
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate #ifdef DDI_MAP_DEBUG
2500Sstevel@tonic-gate ddi_map_debug(" Input: %x.%x.%x\n", rp->regspec_bustype,
2510Sstevel@tonic-gate rp->regspec_addr, rp->regspec_size);
2520Sstevel@tonic-gate ddi_map_debug(" Range: %x.%x %x.%x %x\n",
2530Sstevel@tonic-gate rangep->rng_cbustype, rangep->rng_coffset,
2540Sstevel@tonic-gate rangep->rng_bustype, rangep->rng_offset, rangep->rng_size);
2550Sstevel@tonic-gate #endif /* DDI_MAP_DEBUG */
2560Sstevel@tonic-gate
2570Sstevel@tonic-gate rp->regspec_bustype = rangep->rng_bustype;
2580Sstevel@tonic-gate rp->regspec_addr += rangep->rng_offset - rangep->rng_coffset;
2590Sstevel@tonic-gate
2600Sstevel@tonic-gate #ifdef DDI_MAP_DEBUG
2610Sstevel@tonic-gate ddi_map_debug(" Return: %x.%x.%x\n", rp->regspec_bustype,
2620Sstevel@tonic-gate rp->regspec_addr, rp->regspec_size);
2630Sstevel@tonic-gate #endif /* DDI_MAP_DEBUG */
2640Sstevel@tonic-gate
2650Sstevel@tonic-gate return (0);
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate
2680Sstevel@tonic-gate /*
2690Sstevel@tonic-gate * i_ddi_map_fault: wrapper for bus_map_fault.
2700Sstevel@tonic-gate */
2710Sstevel@tonic-gate int
i_ddi_map_fault(dev_info_t * dip,dev_info_t * rdip,struct hat * hat,struct seg * seg,caddr_t addr,struct devpage * dp,pfn_t pfn,uint_t prot,uint_t lock)2720Sstevel@tonic-gate i_ddi_map_fault(dev_info_t *dip, dev_info_t *rdip,
2730Sstevel@tonic-gate struct hat *hat, struct seg *seg, caddr_t addr,
2740Sstevel@tonic-gate struct devpage *dp, pfn_t pfn, uint_t prot, uint_t lock)
2750Sstevel@tonic-gate {
2760Sstevel@tonic-gate dev_info_t *pdip;
2770Sstevel@tonic-gate
2780Sstevel@tonic-gate if (dip == NULL)
2790Sstevel@tonic-gate return (DDI_FAILURE);
2800Sstevel@tonic-gate
2810Sstevel@tonic-gate pdip = (dev_info_t *)DEVI(dip)->devi_bus_map_fault;
2820Sstevel@tonic-gate
2830Sstevel@tonic-gate /* request appropriate parent to map fault */
2840Sstevel@tonic-gate return ((*(DEVI(pdip)->devi_ops->devo_bus_ops->bus_map_fault))(pdip,
2850Sstevel@tonic-gate rdip, hat, seg, addr, dp, pfn, prot, lock));
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate
2880Sstevel@tonic-gate /*
2890Sstevel@tonic-gate * Return an integer in native machine format from an OBP 1275 integer
2900Sstevel@tonic-gate * representation, which is big-endian, with no particular alignment
2910Sstevel@tonic-gate * guarantees. intp points to the OBP data, and n the number of bytes.
2920Sstevel@tonic-gate *
2930Sstevel@tonic-gate * Byte-swapping is needed on intel.
2940Sstevel@tonic-gate */
2950Sstevel@tonic-gate int
impl_ddi_prop_int_from_prom(uchar_t * intp,int n)2960Sstevel@tonic-gate impl_ddi_prop_int_from_prom(uchar_t *intp, int n)
2970Sstevel@tonic-gate {
2980Sstevel@tonic-gate int i = 0;
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate ASSERT(n > 0 && n <= 4);
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate intp += n;
3030Sstevel@tonic-gate while (n-- > 0) {
3040Sstevel@tonic-gate i = (i << 8) | *(--intp);
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate
3070Sstevel@tonic-gate return (i);
3080Sstevel@tonic-gate }
3090Sstevel@tonic-gate
3100Sstevel@tonic-gate
3110Sstevel@tonic-gate int drv_usec_coarse_timing = 0;
3120Sstevel@tonic-gate
3130Sstevel@tonic-gate /*
3140Sstevel@tonic-gate * Time delay function called by drivers
3150Sstevel@tonic-gate */
3160Sstevel@tonic-gate void
drv_usecwait(clock_t count)3170Sstevel@tonic-gate drv_usecwait(clock_t count)
3180Sstevel@tonic-gate {
3190Sstevel@tonic-gate int tens = 0;
320*5084Sjohnlev extern int gethrtime_hires;
3210Sstevel@tonic-gate
322*5084Sjohnlev if (gethrtime_hires) {
3230Sstevel@tonic-gate hrtime_t start, end;
3240Sstevel@tonic-gate hrtime_t waittime;
3250Sstevel@tonic-gate
3260Sstevel@tonic-gate if (drv_usec_coarse_timing) {
3270Sstevel@tonic-gate /* revert to the wait time as before using tsc */
3280Sstevel@tonic-gate /* in case there are callers depending on the */
3290Sstevel@tonic-gate /* old behaviour */
3300Sstevel@tonic-gate waittime = ((count > 10) ?
331*5084Sjohnlev (((hrtime_t)count / 10) + 1) : 1) *
332*5084Sjohnlev 10 * (NANOSEC / MICROSEC);
3330Sstevel@tonic-gate } else {
3340Sstevel@tonic-gate waittime = (hrtime_t)count * (NANOSEC / MICROSEC);
3350Sstevel@tonic-gate }
3360Sstevel@tonic-gate start = end = gethrtime();
3370Sstevel@tonic-gate while ((end - start) < waittime) {
3380Sstevel@tonic-gate SMT_PAUSE();
3390Sstevel@tonic-gate end = gethrtime();
3400Sstevel@tonic-gate }
3410Sstevel@tonic-gate return;
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate }
3440Sstevel@tonic-gate
3450Sstevel@tonic-gate if (count > 10)
3460Sstevel@tonic-gate tens = count/10;
3470Sstevel@tonic-gate tens++; /* roundup; wait at least 10 microseconds */
3480Sstevel@tonic-gate while (tens > 0) {
3490Sstevel@tonic-gate tenmicrosec();
3500Sstevel@tonic-gate tens--;
3510Sstevel@tonic-gate }
3520Sstevel@tonic-gate }
353