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