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