xref: /titanic_44/usr/src/uts/sparc/os/ddi_arch.c (revision 4a6ec905b96eb96a398c346f59e034a90ce8ad37)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 /*
31  * This file contains ddi functions common to sparc implementations
32  */
33 
34 #include <sys/types.h>
35 #include <sys/dditypes.h>
36 #include <sys/ddi_impldefs.h>
37 #include <sys/sunddi.h>
38 
39 /*
40  * DDI Mapping
41  */
42 
43 /*
44  * Enable DDI_MAP_DEBUG for map debugging messages...
45  * (c.f. rootnex.c)
46  * #define	DDI_MAP_DEBUG
47  */
48 
49 #ifdef	DDI_MAP_DEBUG
50 int ddi_map_debug_flag = 1;
51 #define	ddi_map_debug	if (ddi_map_debug_flag) printf
52 #endif	/* DDI_MAP_DEBUG */
53 
54 /*
55  * i_ddi_bus_map:
56  * Generic bus_map entry point, for byte addressable devices
57  * conforming to the reg/range addressing model with no HAT layer
58  * to be programmed at this level.
59  */
60 int
61 i_ddi_bus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
62 	off_t offset, off_t len, caddr_t *vaddrp)
63 {
64 	struct regspec tmp_reg, *rp;
65 	ddi_map_req_t mr = *mp;	 /* Get private copy of request */
66 	int error;
67 
68 	mp = &mr;
69 
70 	/*
71 	 * First, if given an rnumber, convert it to a regspec...
72 	 */
73 
74 	if (mp->map_type == DDI_MT_RNUMBER) {
75 
76 		int rnumber = mp->map_obj.rnumber;
77 #ifdef	DDI_MAP_DEBUG
78 		static char *out_of_range =
79 		    "i_ddi_bus_map: Out of range rnumber <%d>, device <%s>";
80 #endif	/* DDI_MAP_DEBUG */
81 
82 		rp = i_ddi_rnumber_to_regspec(rdip, rnumber);
83 		if (rp == (struct regspec *)0)	{
84 #ifdef	DDI_MAP_DEBUG
85 			cmn_err(CE_WARN, out_of_range, rnumber,
86 			    ddi_get_name(rdip));
87 #endif	/* DDI_MAP_DEBUG */
88 			return (DDI_ME_RNUMBER_RANGE);
89 		}
90 
91 		/*
92 		 * Convert the given ddi_map_req_t from rnumber to regspec...
93 		 */
94 
95 		mp->map_type = DDI_MT_REGSPEC;
96 		mp->map_obj.rp = rp;
97 	}
98 
99 	/*
100 	 * Adjust offset and length correspnding to called values.
101 	 * A non-zero length means override the one in the regspec,
102 	 * regardless of what's in the parent's range.
103 	 */
104 
105 	tmp_reg = *(mp->map_obj.rp);	/* Preserve underlying data */
106 	rp = mp->map_obj.rp = &tmp_reg;	/* Use tmp_reg in request */
107 
108 	rp->regspec_addr += (uintptr_t)offset;
109 	if (len != 0)
110 		rp->regspec_size = (uint_t)len;
111 
112 	/*
113 	 * If we had an MMU, this is where you'd program the MMU and hat layer.
114 	 * Since we're using the default function here, we do not have an MMU
115 	 * to program.
116 	 */
117 
118 	/*
119 	 * Apply any parent ranges at this level, if applicable.
120 	 * (This is where nexus specific regspec translation takes place.
121 	 * Use of this function is implicit agreement that translation is
122 	 * provided via ddi_apply_range.)  Note that we assume that
123 	 * the request is within the parents limits.
124 	 */
125 
126 #ifdef	DDI_MAP_DEBUG
127 	ddi_map_debug("applying range of parent <%s> to child <%s>...\n",
128 	    ddi_get_name(dip), ddi_get_name(rdip));
129 #endif	/* DDI_MAP_DEBUG */
130 
131 	if ((error = i_ddi_apply_range(dip, rdip, mp->map_obj.rp)) != 0)
132 		return (error);
133 
134 	/*
135 	 * Call my parents bus_map function with modified values...
136 	 */
137 
138 	return (ddi_map(dip, mp, (off_t)0, (off_t)0, vaddrp));
139 }
140 
141 /*
142  * i_ddi_map_fault: wrapper for bus_map_fault.
143  */
144 int
145 i_ddi_map_fault(dev_info_t *dip, dev_info_t *rdip,
146 	struct hat *hat, struct seg *seg, caddr_t addr,
147 	struct devpage *dp, pfn_t pfn, uint_t prot, uint_t lock)
148 {
149 	dev_info_t *pdip;
150 
151 	if (dip == NULL)
152 		return (DDI_FAILURE);
153 
154 	pdip = (dev_info_t *)DEVI(dip)->devi_bus_map_fault;
155 
156 	/* request appropriate parent to map fault */
157 	return ((*(DEVI(pdip)->devi_ops->devo_bus_ops->bus_map_fault))(pdip,
158 	    rdip, hat, seg, addr, dp, pfn, prot, lock));
159 }
160 
161 /*
162  * Creating register mappings and handling interrupts:
163  */
164 struct regspec *
165 i_ddi_rnumber_to_regspec(dev_info_t *dip, int rnumber)
166 {
167 	if (rnumber >= sparc_pd_getnreg(DEVI(dip)))
168 		return ((struct regspec *)0);
169 
170 	return (sparc_pd_getreg(DEVI(dip), rnumber));
171 }
172 
173 /*
174  * Static function to determine if a reg prop is enclosed within
175  * a given a range spec.  (For readability: only used by i_ddi_aply_range.).
176  */
177 static int
178 reg_is_enclosed_in_range(struct regspec *rp, struct rangespec *rangep)
179 {
180 	if (rp->regspec_bustype != rangep->rng_cbustype)
181 		return (0);
182 
183 	if (rp->regspec_addr < rangep->rng_coffset)
184 		return (0);
185 
186 	if (rangep->rng_size == 0)
187 		return (1);	/* size is really 2**(bits_per_word) */
188 
189 	if ((rp->regspec_addr + rp->regspec_size - 1) <=
190 	    (rangep->rng_coffset + rangep->rng_size - 1))
191 		return (1);
192 
193 	return (0);
194 }
195 
196 /*
197  * i_ddi_apply_range:
198  * Apply range of dp to struct regspec *rp, if applicable.
199  * If there's any range defined, it gets applied.
200  */
201 int
202 i_ddi_apply_range(dev_info_t *dp, dev_info_t *rdip, struct regspec *rp)
203 {
204 	int nrange, b;
205 	struct rangespec *rangep;
206 	static char out_of_range[] =
207 	    "Out of range register specification from device node <%s>";
208 
209 	nrange = sparc_pd_getnrng(dp);
210 	if (nrange == 0) {
211 #ifdef	DDI_MAP_DEBUG
212 		ddi_map_debug("    No range.\n");
213 #endif	/* DDI_MAP_DEBUG */
214 		return (0);
215 	}
216 
217 	/*
218 	 * Find a match, making sure the regspec is within the range
219 	 * of the parent, noting that a size of zero in a range spec
220 	 * really means a size of 2**(bitsperword).
221 	 */
222 
223 	for (b = 0, rangep = sparc_pd_getrng(dp, 0); b < nrange; ++b, ++rangep)
224 		if (reg_is_enclosed_in_range(rp, rangep))
225 			break;		/* found a match */
226 
227 	if (b == nrange) {
228 		cmn_err(CE_WARN, out_of_range, ddi_get_name(rdip));
229 		return (DDI_ME_REGSPEC_RANGE);
230 	}
231 
232 #ifdef	DDI_MAP_DEBUG
233 	ddi_map_debug("    Input:  %x.%x.%x\n", rp->regspec_bustype,
234 	    rp->regspec_addr, rp->regspec_size);
235 	ddi_map_debug("    Range:  %x.%x %x.%x %x\n",
236 	    rangep->rng_cbustype, rangep->rng_coffset,
237 	    rangep->rng_bustype, rangep->rng_offset, rangep->rng_size);
238 #endif	/* DDI_MAP_DEBUG */
239 
240 	rp->regspec_bustype = rangep->rng_bustype;
241 	rp->regspec_addr += rangep->rng_offset - rangep->rng_coffset;
242 
243 #ifdef	DDI_MAP_DEBUG
244 	ddi_map_debug("    Return: %x.%x.%x\n", rp->regspec_bustype,
245 	    rp->regspec_addr, rp->regspec_size);
246 #endif	/* DDI_MAP_DEBUG */
247 
248 	return (0);
249 }
250 
251 /*
252  * Return an integer in native machine format from an OBP 1275 integer
253  * representation, which is big-endian, with no particular alignment
254  * guarantees.  intp points to the OBP data, and n the number of bytes.
255  */
256 int
257 impl_ddi_prop_int_from_prom(uchar_t *intp, int n)
258 {
259 	int	i = 0;
260 
261 	ASSERT(n > 0 && n <= 4);
262 
263 	while (n-- > 0) {
264 		i = (i << 8) | *intp++;
265 	}
266 
267 	return (i);
268 }
269