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