xref: /illumos-gate/usr/src/uts/intel/os/ddi_arch.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * This file contains ddi functions common to intel architectures
28  */
29 
30 #include <sys/archsystm.h>
31 #include <sys/types.h>
32 #include <sys/dditypes.h>
33 #include <sys/ddi_impldefs.h>
34 #include <sys/sunddi.h>
35 #include <sys/cpu.h>
36 
37 /*
38  * DDI Mapping
39  */
40 
41 /*
42  * i_ddi_bus_map:
43  * Generic bus_map entry point, for byte addressable devices
44  * conforming to the reg/range addressing model with no HAT layer
45  * to be programmed at this level.
46  */
47 
48 int
49 i_ddi_bus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
50 	off_t offset, off_t len, caddr_t *vaddrp)
51 {
52 	struct regspec tmp_reg, *rp;
53 	ddi_map_req_t mr = *mp;		/* Get private copy of request */
54 	int error;
55 
56 	mp = &mr;
57 
58 	/*
59 	 * First, if given an rnumber, convert it to a regspec...
60 	 */
61 
62 	if (mp->map_type == DDI_MT_RNUMBER)  {
63 
64 		int rnumber = mp->map_obj.rnumber;
65 #ifdef	DDI_MAP_DEBUG
66 		static char *out_of_range =
67 		    "i_ddi_bus_map: Out of range rnumber <%d>, device <%s>";
68 #endif	/* DDI_MAP_DEBUG */
69 
70 		rp = i_ddi_rnumber_to_regspec(rdip, rnumber);
71 		if (rp == (struct regspec *)0)  {
72 #ifdef	DDI_MAP_DEBUG
73 			cmn_err(CE_WARN, out_of_range, rnumber,
74 			    ddi_get_name(rdip));
75 #endif	/* DDI_MAP_DEBUG */
76 			return (DDI_ME_RNUMBER_RANGE);
77 		}
78 
79 		/*
80 		 * Convert the given ddi_map_req_t from rnumber to regspec...
81 		 */
82 
83 		mp->map_type = DDI_MT_REGSPEC;
84 		mp->map_obj.rp = rp;
85 	}
86 
87 	/*
88 	 * Adjust offset and length correspnding to called values...
89 	 * XXX: A non-zero length means override the one in the regspec.
90 	 * XXX: (Regardless of what's in the parent's range)
91 	 */
92 
93 	tmp_reg = *(mp->map_obj.rp);		/* Preserve underlying data */
94 	rp = mp->map_obj.rp = &tmp_reg;		/* Use tmp_reg in request */
95 
96 #ifdef	DDI_MAP_DEBUG
97 	cmn_err(CE_CONT,
98 	    "i_ddi_bus_map: <%s,%s> <0x%x, 0x%x, 0x%d> "
99 	    "offset %d len %d handle 0x%x\n",
100 	    ddi_get_name(dip), ddi_get_name(rdip),
101 	    rp->regspec_bustype, rp->regspec_addr, rp->regspec_size,
102 	    offset, len, mp->map_handlep);
103 #endif	/* DDI_MAP_DEBUG */
104 
105 	/*
106 	 * I/O or memory mapping
107 	 *
108 	 *	<bustype=0, addr=x, len=x>: memory
109 	 *	<bustype=1, addr=x, len=x>: i/o
110 	 *	<bustype>1, addr=0, len=x>: x86-compatibility i/o
111 	 */
112 
113 	if (rp->regspec_bustype > 1 && rp->regspec_addr != 0) {
114 		cmn_err(CE_WARN, "<%s,%s>: invalid register spec"
115 		    " <0x%x, 0x%x, 0x%x>\n", ddi_get_name(dip),
116 		    ddi_get_name(rdip), rp->regspec_bustype,
117 		    rp->regspec_addr, rp->regspec_size);
118 		return (DDI_ME_INVAL);
119 	}
120 
121 	if (rp->regspec_bustype > 1 && rp->regspec_addr == 0) {
122 		/*
123 		 * compatibility i/o mapping
124 		 */
125 		rp->regspec_bustype += (uint_t)offset;
126 	} else {
127 		/*
128 		 * Normal memory or i/o mapping
129 		 */
130 		rp->regspec_addr += (uint_t)offset;
131 	}
132 
133 	if (len != 0)
134 		rp->regspec_size = (uint_t)len;
135 
136 #ifdef	DDI_MAP_DEBUG
137 	cmn_err(CE_CONT,
138 	    "               <%s,%s> <0x%x, 0x%x, 0x%d> "
139 	    "offset %d len %d\n",
140 	    ddi_get_name(dip), ddi_get_name(rdip),
141 	    rp->regspec_bustype, rp->regspec_addr, rp->regspec_size,
142 	    offset, len);
143 #endif	/* DDI_MAP_DEBUG */
144 
145 	/*
146 	 * If we had an MMU, this is where you'd program the MMU and hat layer.
147 	 * Since we're using the default function here, we do not have an MMU
148 	 * to program.
149 	 */
150 
151 	/*
152 	 * Apply any parent ranges at this level, if applicable.
153 	 * (This is where nexus specific regspec translation takes place.
154 	 * Use of this function is implicit agreement that translation is
155 	 * provided via ddi_apply_range.)  Note that we assume that
156 	 * the request is within the parents limits.
157 	 */
158 
159 #ifdef	DDI_MAP_DEBUG
160 	ddi_map_debug("applying range of parent <%s> to child <%s>...\n",
161 	    ddi_get_name(dip), ddi_get_name(rdip));
162 #endif	/* DDI_MAP_DEBUG */
163 
164 	if ((error = i_ddi_apply_range(dip, rdip, mp->map_obj.rp)) != 0)
165 		return (error);
166 
167 	/*
168 	 * Call my parents bus_map function with modified values...
169 	 */
170 
171 	return (ddi_map(dip, mp, (off_t)0, (off_t)0, vaddrp));
172 }
173 
174 /*
175  * Creating register mappings and handling interrupts:
176  */
177 
178 struct regspec *
179 i_ddi_rnumber_to_regspec(dev_info_t *dip, int rnumber)
180 {
181 	if (rnumber >= sparc_pd_getnreg(DEVI(dip)))
182 		return ((struct regspec *)0);
183 
184 	return (sparc_pd_getreg(DEVI(dip), rnumber));
185 }
186 
187 /*
188  * Static function to determine if a reg prop is enclosed within
189  * a given a range spec.  (For readability: only used by i_ddi_aply_range.).
190  */
191 static int
192 reg_is_enclosed_in_range(struct regspec *rp, struct rangespec *rangep)
193 {
194 	if (rp->regspec_bustype != rangep->rng_cbustype)
195 		return (0);
196 
197 	if (rp->regspec_addr < rangep->rng_coffset)
198 		return (0);
199 
200 	if (rangep->rng_size == 0)
201 		return (1);	/* size is really 2**(bits_per_word) */
202 
203 	if ((rp->regspec_addr + rp->regspec_size - 1) <=
204 	    (rangep->rng_coffset + rangep->rng_size - 1))
205 		return (1);
206 
207 	return (0);
208 }
209 
210 /*
211  * i_ddi_apply_range:
212  * Apply range of dp to struct regspec *rp, if applicable.
213  * If there's any range defined, it gets applied.
214  */
215 
216 int
217 i_ddi_apply_range(dev_info_t *dp, dev_info_t *rdip, struct regspec *rp)
218 {
219 	int nrange, b;
220 	struct rangespec *rangep;
221 	static char *out_of_range =
222 	    "Out of range register specification from device node <%s>\n";
223 
224 	nrange = sparc_pd_getnrng(dp);
225 	if (nrange == 0)  {
226 #ifdef	DDI_MAP_DEBUG
227 		ddi_map_debug("    No range.\n");
228 #endif	/* DDI_MAP_DEBUG */
229 		return (0);
230 	}
231 
232 	/*
233 	 * Find a match, making sure the regspec is within the range
234 	 * of the parent, noting that a size of zero in a range spec
235 	 * really means a size of 2**(bitsperword).
236 	 */
237 
238 	for (b = 0, rangep = sparc_pd_getrng(dp, 0); b < nrange; ++b, ++rangep)
239 		if (reg_is_enclosed_in_range(rp, rangep))
240 			break;		/* found a match */
241 
242 	if (b == nrange)  {
243 		cmn_err(CE_WARN, out_of_range, ddi_get_name(rdip));
244 		return (DDI_ME_REGSPEC_RANGE);
245 	}
246 
247 #ifdef	DDI_MAP_DEBUG
248 	ddi_map_debug("    Input:  %x.%x.%x\n", rp->regspec_bustype,
249 	    rp->regspec_addr, rp->regspec_size);
250 	ddi_map_debug("    Range:  %x.%x %x.%x %x\n",
251 	    rangep->rng_cbustype, rangep->rng_coffset,
252 	    rangep->rng_bustype, rangep->rng_offset, rangep->rng_size);
253 #endif	/* DDI_MAP_DEBUG */
254 
255 	rp->regspec_bustype = rangep->rng_bustype;
256 	rp->regspec_addr += rangep->rng_offset - rangep->rng_coffset;
257 
258 #ifdef	DDI_MAP_DEBUG
259 	ddi_map_debug("    Return: %x.%x.%x\n", rp->regspec_bustype,
260 	    rp->regspec_addr, rp->regspec_size);
261 #endif	/* DDI_MAP_DEBUG */
262 
263 	return (0);
264 }
265 
266 /*
267  * i_ddi_map_fault: wrapper for bus_map_fault.
268  */
269 int
270 i_ddi_map_fault(dev_info_t *dip, dev_info_t *rdip,
271 	struct hat *hat, struct seg *seg, caddr_t addr,
272 	struct devpage *dp, pfn_t pfn, uint_t prot, uint_t lock)
273 {
274 	dev_info_t *pdip;
275 
276 	if (dip == NULL)
277 		return (DDI_FAILURE);
278 
279 	pdip = (dev_info_t *)DEVI(dip)->devi_bus_map_fault;
280 
281 	/* request appropriate parent to map fault */
282 	return ((*(DEVI(pdip)->devi_ops->devo_bus_ops->bus_map_fault))(pdip,
283 	    rdip, hat, seg, addr, dp, pfn, prot, lock));
284 }
285 
286 /*
287  * Return an integer in native machine format from an OBP 1275 integer
288  * representation, which is big-endian, with no particular alignment
289  * guarantees.  intp points to the OBP data, and n the number of bytes.
290  *
291  * Byte-swapping is needed on intel.
292  */
293 int
294 impl_ddi_prop_int_from_prom(uchar_t *intp, int n)
295 {
296 	int	i = 0;
297 
298 	ASSERT(n > 0 && n <= 4);
299 
300 	intp += n;
301 	while (n-- > 0) {
302 		i = (i << 8) | *(--intp);
303 	}
304 
305 	return (i);
306 }
307 
308 
309 int drv_usec_coarse_timing = 0;
310 
311 /*
312  * Time delay function called by drivers
313  */
314 void
315 drv_usecwait(clock_t count)
316 {
317 	int tens = 0;
318 	extern int gethrtime_hires;
319 
320 	if (gethrtime_hires) {
321 		hrtime_t start, end;
322 		hrtime_t waittime;
323 
324 		if (drv_usec_coarse_timing) {
325 			/* revert to the wait time as before using tsc */
326 			/* in case there are callers depending on the */
327 			/* old behaviour */
328 			waittime = ((count > 10) ?
329 			    (((hrtime_t)count / 10) + 1) : 1) *
330 			    10 * (NANOSEC / MICROSEC);
331 		} else  {
332 			waittime = (hrtime_t)count * (NANOSEC / MICROSEC);
333 		}
334 		start = end =  gethrtime();
335 		while ((end - start) < waittime) {
336 			SMT_PAUSE();
337 			end = gethrtime();
338 		}
339 		return;
340 
341 	}
342 
343 	if (count > 10)
344 		tens = count/10;
345 	tens++;			/* roundup; wait at least 10 microseconds */
346 	while (tens > 0) {
347 		tenmicrosec();
348 		tens--;
349 	}
350 }
351