xref: /illumos-gate/usr/src/uts/intel/os/ddi_arch.c (revision 2d6eb4a5)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*843e1988Sjohnlev  * Common Development and Distribution License (the "License").
6*843e1988Sjohnlev  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*843e1988Sjohnlev  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * This file contains ddi functions common to intel architectures
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/dditypes.h>
337c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
347c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
357c478bd9Sstevel@tonic-gate #include <sys/cpu.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /*
387c478bd9Sstevel@tonic-gate  * DDI Mapping
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * i_ddi_bus_map:
437c478bd9Sstevel@tonic-gate  * Generic bus_map entry point, for byte addressable devices
447c478bd9Sstevel@tonic-gate  * conforming to the reg/range addressing model with no HAT layer
457c478bd9Sstevel@tonic-gate  * to be programmed at this level.
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@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)497c478bd9Sstevel@tonic-gate i_ddi_bus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
507c478bd9Sstevel@tonic-gate 	off_t offset, off_t len, caddr_t *vaddrp)
517c478bd9Sstevel@tonic-gate {
527c478bd9Sstevel@tonic-gate 	struct regspec tmp_reg, *rp;
537c478bd9Sstevel@tonic-gate 	ddi_map_req_t mr = *mp;		/* Get private copy of request */
547c478bd9Sstevel@tonic-gate 	int error;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	mp = &mr;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	/*
597c478bd9Sstevel@tonic-gate 	 * First, if given an rnumber, convert it to a regspec...
607c478bd9Sstevel@tonic-gate 	 */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	if (mp->map_type == DDI_MT_RNUMBER)  {
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 		int rnumber = mp->map_obj.rnumber;
657c478bd9Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
667c478bd9Sstevel@tonic-gate 		static char *out_of_range =
677c478bd9Sstevel@tonic-gate 		    "i_ddi_bus_map: Out of range rnumber <%d>, device <%s>";
687c478bd9Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 		rp = i_ddi_rnumber_to_regspec(rdip, rnumber);
717c478bd9Sstevel@tonic-gate 		if (rp == (struct regspec *)0)  {
727c478bd9Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
737c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, out_of_range, rnumber,
747c478bd9Sstevel@tonic-gate 			    ddi_get_name(rdip));
757c478bd9Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
767c478bd9Sstevel@tonic-gate 			return (DDI_ME_RNUMBER_RANGE);
777c478bd9Sstevel@tonic-gate 		}
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 		/*
807c478bd9Sstevel@tonic-gate 		 * Convert the given ddi_map_req_t from rnumber to regspec...
817c478bd9Sstevel@tonic-gate 		 */
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 		mp->map_type = DDI_MT_REGSPEC;
847c478bd9Sstevel@tonic-gate 		mp->map_obj.rp = rp;
857c478bd9Sstevel@tonic-gate 	}
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	/*
887c478bd9Sstevel@tonic-gate 	 * Adjust offset and length correspnding to called values...
897c478bd9Sstevel@tonic-gate 	 * XXX: A non-zero length means override the one in the regspec.
907c478bd9Sstevel@tonic-gate 	 * XXX: (Regardless of what's in the parent's range)
917c478bd9Sstevel@tonic-gate 	 */
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	tmp_reg = *(mp->map_obj.rp);		/* Preserve underlying data */
947c478bd9Sstevel@tonic-gate 	rp = mp->map_obj.rp = &tmp_reg;		/* Use tmp_reg in request */
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
977c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT,
98*843e1988Sjohnlev 	    "i_ddi_bus_map: <%s,%s> <0x%x, 0x%x, 0x%d> "
99*843e1988Sjohnlev 	    "offset %d len %d handle 0x%x\n",
100*843e1988Sjohnlev 	    ddi_get_name(dip), ddi_get_name(rdip),
101*843e1988Sjohnlev 	    rp->regspec_bustype, rp->regspec_addr, rp->regspec_size,
102*843e1988Sjohnlev 	    offset, len, mp->map_handlep);
1037c478bd9Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	/*
1067c478bd9Sstevel@tonic-gate 	 * I/O or memory mapping
1077c478bd9Sstevel@tonic-gate 	 *
1087c478bd9Sstevel@tonic-gate 	 *	<bustype=0, addr=x, len=x>: memory
1097c478bd9Sstevel@tonic-gate 	 *	<bustype=1, addr=x, len=x>: i/o
1107c478bd9Sstevel@tonic-gate 	 *	<bustype>1, addr=0, len=x>: x86-compatibility i/o
1117c478bd9Sstevel@tonic-gate 	 */
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	if (rp->regspec_bustype > 1 && rp->regspec_addr != 0) {
1147c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "<%s,%s>: invalid register spec"
1157c478bd9Sstevel@tonic-gate 		    " <0x%x, 0x%x, 0x%x>\n", ddi_get_name(dip),
1167c478bd9Sstevel@tonic-gate 		    ddi_get_name(rdip), rp->regspec_bustype,
1177c478bd9Sstevel@tonic-gate 		    rp->regspec_addr, rp->regspec_size);
1187c478bd9Sstevel@tonic-gate 		return (DDI_ME_INVAL);
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	if (rp->regspec_bustype > 1 && rp->regspec_addr == 0) {
1227c478bd9Sstevel@tonic-gate 		/*
1237c478bd9Sstevel@tonic-gate 		 * compatibility i/o mapping
1247c478bd9Sstevel@tonic-gate 		 */
1257c478bd9Sstevel@tonic-gate 		rp->regspec_bustype += (uint_t)offset;
1267c478bd9Sstevel@tonic-gate 	} else {
1277c478bd9Sstevel@tonic-gate 		/*
1287c478bd9Sstevel@tonic-gate 		 * Normal memory or i/o mapping
1297c478bd9Sstevel@tonic-gate 		 */
1307c478bd9Sstevel@tonic-gate 		rp->regspec_addr += (uint_t)offset;
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	if (len != 0)
1347c478bd9Sstevel@tonic-gate 		rp->regspec_size = (uint_t)len;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
1377c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT,
138*843e1988Sjohnlev 	    "               <%s,%s> <0x%x, 0x%x, 0x%d> "
139*843e1988Sjohnlev 	    "offset %d len %d\n",
140*843e1988Sjohnlev 	    ddi_get_name(dip), ddi_get_name(rdip),
141*843e1988Sjohnlev 	    rp->regspec_bustype, rp->regspec_addr, rp->regspec_size,
142*843e1988Sjohnlev 	    offset, len);
1437c478bd9Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	/*
1467c478bd9Sstevel@tonic-gate 	 * If we had an MMU, this is where you'd program the MMU and hat layer.
1477c478bd9Sstevel@tonic-gate 	 * Since we're using the default function here, we do not have an MMU
1487c478bd9Sstevel@tonic-gate 	 * to program.
1497c478bd9Sstevel@tonic-gate 	 */
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	/*
1527c478bd9Sstevel@tonic-gate 	 * Apply any parent ranges at this level, if applicable.
1537c478bd9Sstevel@tonic-gate 	 * (This is where nexus specific regspec translation takes place.
1547c478bd9Sstevel@tonic-gate 	 * Use of this function is implicit agreement that translation is
1557c478bd9Sstevel@tonic-gate 	 * provided via ddi_apply_range.)  Note that we assume that
1567c478bd9Sstevel@tonic-gate 	 * the request is within the parents limits.
1577c478bd9Sstevel@tonic-gate 	 */
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
1607c478bd9Sstevel@tonic-gate 	ddi_map_debug("applying range of parent <%s> to child <%s>...\n",
1617c478bd9Sstevel@tonic-gate 	    ddi_get_name(dip), ddi_get_name(rdip));
1627c478bd9Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	if ((error = i_ddi_apply_range(dip, rdip, mp->map_obj.rp)) != 0)
1657c478bd9Sstevel@tonic-gate 		return (error);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	/*
1687c478bd9Sstevel@tonic-gate 	 * Call my parents bus_map function with modified values...
1697c478bd9Sstevel@tonic-gate 	 */
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	return (ddi_map(dip, mp, (off_t)0, (off_t)0, vaddrp));
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate /*
1757c478bd9Sstevel@tonic-gate  * Creating register mappings and handling interrupts:
1767c478bd9Sstevel@tonic-gate  */
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate struct regspec *
i_ddi_rnumber_to_regspec(dev_info_t * dip,int rnumber)1797c478bd9Sstevel@tonic-gate i_ddi_rnumber_to_regspec(dev_info_t *dip, int rnumber)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate 	if (rnumber >= sparc_pd_getnreg(DEVI(dip)))
1827c478bd9Sstevel@tonic-gate 		return ((struct regspec *)0);
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	return (sparc_pd_getreg(DEVI(dip), rnumber));
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate  * Static function to determine if a reg prop is enclosed within
1897c478bd9Sstevel@tonic-gate  * a given a range spec.  (For readability: only used by i_ddi_aply_range.).
1907c478bd9Sstevel@tonic-gate  */
1917c478bd9Sstevel@tonic-gate static int
reg_is_enclosed_in_range(struct regspec * rp,struct rangespec * rangep)1927c478bd9Sstevel@tonic-gate reg_is_enclosed_in_range(struct regspec *rp, struct rangespec *rangep)
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate 	if (rp->regspec_bustype != rangep->rng_cbustype)
1957c478bd9Sstevel@tonic-gate 		return (0);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	if (rp->regspec_addr < rangep->rng_coffset)
1987c478bd9Sstevel@tonic-gate 		return (0);
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	if (rangep->rng_size == 0)
2017c478bd9Sstevel@tonic-gate 		return (1);	/* size is really 2**(bits_per_word) */
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	if ((rp->regspec_addr + rp->regspec_size - 1) <=
2047c478bd9Sstevel@tonic-gate 	    (rangep->rng_coffset + rangep->rng_size - 1))
2057c478bd9Sstevel@tonic-gate 		return (1);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	return (0);
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate /*
2117c478bd9Sstevel@tonic-gate  * i_ddi_apply_range:
2127c478bd9Sstevel@tonic-gate  * Apply range of dp to struct regspec *rp, if applicable.
2137c478bd9Sstevel@tonic-gate  * If there's any range defined, it gets applied.
2147c478bd9Sstevel@tonic-gate  */
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate int
i_ddi_apply_range(dev_info_t * dp,dev_info_t * rdip,struct regspec * rp)2177c478bd9Sstevel@tonic-gate i_ddi_apply_range(dev_info_t *dp, dev_info_t *rdip, struct regspec *rp)
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate 	int nrange, b;
2207c478bd9Sstevel@tonic-gate 	struct rangespec *rangep;
2217c478bd9Sstevel@tonic-gate 	static char *out_of_range =
2227c478bd9Sstevel@tonic-gate 	    "Out of range register specification from device node <%s>\n";
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	nrange = sparc_pd_getnrng(dp);
2257c478bd9Sstevel@tonic-gate 	if (nrange == 0)  {
2267c478bd9Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
2277c478bd9Sstevel@tonic-gate 		ddi_map_debug("    No range.\n");
2287c478bd9Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
2297c478bd9Sstevel@tonic-gate 		return (0);
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	/*
2337c478bd9Sstevel@tonic-gate 	 * Find a match, making sure the regspec is within the range
2347c478bd9Sstevel@tonic-gate 	 * of the parent, noting that a size of zero in a range spec
2357c478bd9Sstevel@tonic-gate 	 * really means a size of 2**(bitsperword).
2367c478bd9Sstevel@tonic-gate 	 */
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	for (b = 0, rangep = sparc_pd_getrng(dp, 0); b < nrange; ++b, ++rangep)
2397c478bd9Sstevel@tonic-gate 		if (reg_is_enclosed_in_range(rp, rangep))
2407c478bd9Sstevel@tonic-gate 			break;		/* found a match */
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	if (b == nrange)  {
2437c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, out_of_range, ddi_get_name(rdip));
2447c478bd9Sstevel@tonic-gate 		return (DDI_ME_REGSPEC_RANGE);
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
2487c478bd9Sstevel@tonic-gate 	ddi_map_debug("    Input:  %x.%x.%x\n", rp->regspec_bustype,
2497c478bd9Sstevel@tonic-gate 	    rp->regspec_addr, rp->regspec_size);
2507c478bd9Sstevel@tonic-gate 	ddi_map_debug("    Range:  %x.%x %x.%x %x\n",
2517c478bd9Sstevel@tonic-gate 	    rangep->rng_cbustype, rangep->rng_coffset,
2527c478bd9Sstevel@tonic-gate 	    rangep->rng_bustype, rangep->rng_offset, rangep->rng_size);
2537c478bd9Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	rp->regspec_bustype = rangep->rng_bustype;
2567c478bd9Sstevel@tonic-gate 	rp->regspec_addr += rangep->rng_offset - rangep->rng_coffset;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate #ifdef	DDI_MAP_DEBUG
2597c478bd9Sstevel@tonic-gate 	ddi_map_debug("    Return: %x.%x.%x\n", rp->regspec_bustype,
2607c478bd9Sstevel@tonic-gate 	    rp->regspec_addr, rp->regspec_size);
2617c478bd9Sstevel@tonic-gate #endif	/* DDI_MAP_DEBUG */
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	return (0);
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate /*
2677c478bd9Sstevel@tonic-gate  * i_ddi_map_fault: wrapper for bus_map_fault.
2687c478bd9Sstevel@tonic-gate  */
2697c478bd9Sstevel@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)2707c478bd9Sstevel@tonic-gate i_ddi_map_fault(dev_info_t *dip, dev_info_t *rdip,
2717c478bd9Sstevel@tonic-gate 	struct hat *hat, struct seg *seg, caddr_t addr,
2727c478bd9Sstevel@tonic-gate 	struct devpage *dp, pfn_t pfn, uint_t prot, uint_t lock)
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate 	dev_info_t *pdip;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	if (dip == NULL)
2777c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	pdip = (dev_info_t *)DEVI(dip)->devi_bus_map_fault;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	/* request appropriate parent to map fault */
2827c478bd9Sstevel@tonic-gate 	return ((*(DEVI(pdip)->devi_ops->devo_bus_ops->bus_map_fault))(pdip,
2837c478bd9Sstevel@tonic-gate 	    rdip, hat, seg, addr, dp, pfn, prot, lock));
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate /*
2877c478bd9Sstevel@tonic-gate  * Return an integer in native machine format from an OBP 1275 integer
2887c478bd9Sstevel@tonic-gate  * representation, which is big-endian, with no particular alignment
2897c478bd9Sstevel@tonic-gate  * guarantees.  intp points to the OBP data, and n the number of bytes.
2907c478bd9Sstevel@tonic-gate  *
2917c478bd9Sstevel@tonic-gate  * Byte-swapping is needed on intel.
2927c478bd9Sstevel@tonic-gate  */
2937c478bd9Sstevel@tonic-gate int
impl_ddi_prop_int_from_prom(uchar_t * intp,int n)2947c478bd9Sstevel@tonic-gate impl_ddi_prop_int_from_prom(uchar_t *intp, int n)
2957c478bd9Sstevel@tonic-gate {
2967c478bd9Sstevel@tonic-gate 	int	i = 0;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	ASSERT(n > 0 && n <= 4);
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	intp += n;
3017c478bd9Sstevel@tonic-gate 	while (n-- > 0) {
3027c478bd9Sstevel@tonic-gate 		i = (i << 8) | *(--intp);
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	return (i);
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate int drv_usec_coarse_timing = 0;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate /*
3127c478bd9Sstevel@tonic-gate  * Time delay function called by drivers
3137c478bd9Sstevel@tonic-gate  */
3147c478bd9Sstevel@tonic-gate void
drv_usecwait(clock_t count)3157c478bd9Sstevel@tonic-gate drv_usecwait(clock_t count)
3167c478bd9Sstevel@tonic-gate {
3177c478bd9Sstevel@tonic-gate 	int tens = 0;
318*843e1988Sjohnlev 	extern int gethrtime_hires;
3197c478bd9Sstevel@tonic-gate 
320*843e1988Sjohnlev 	if (gethrtime_hires) {
3217c478bd9Sstevel@tonic-gate 		hrtime_t start, end;
3227c478bd9Sstevel@tonic-gate 		hrtime_t waittime;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 		if (drv_usec_coarse_timing) {
3257c478bd9Sstevel@tonic-gate 			/* revert to the wait time as before using tsc */
3267c478bd9Sstevel@tonic-gate 			/* in case there are callers depending on the */
3277c478bd9Sstevel@tonic-gate 			/* old behaviour */
3287c478bd9Sstevel@tonic-gate 			waittime = ((count > 10) ?
329*843e1988Sjohnlev 			    (((hrtime_t)count / 10) + 1) : 1) *
330*843e1988Sjohnlev 			    10 * (NANOSEC / MICROSEC);
3317c478bd9Sstevel@tonic-gate 		} else  {
3327c478bd9Sstevel@tonic-gate 			waittime = (hrtime_t)count * (NANOSEC / MICROSEC);
3337c478bd9Sstevel@tonic-gate 		}
3347c478bd9Sstevel@tonic-gate 		start = end =  gethrtime();
3357c478bd9Sstevel@tonic-gate 		while ((end - start) < waittime) {
3367c478bd9Sstevel@tonic-gate 			SMT_PAUSE();
3377c478bd9Sstevel@tonic-gate 			end = gethrtime();
3387c478bd9Sstevel@tonic-gate 		}
3397c478bd9Sstevel@tonic-gate 		return;
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	}
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	if (count > 10)
3447c478bd9Sstevel@tonic-gate 		tens = count/10;
3457c478bd9Sstevel@tonic-gate 	tens++;			/* roundup; wait at least 10 microseconds */
3467c478bd9Sstevel@tonic-gate 	while (tens > 0) {
3477c478bd9Sstevel@tonic-gate 		tenmicrosec();
3487c478bd9Sstevel@tonic-gate 		tens--;
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate }
351