xref: /onnv-gate/usr/src/uts/sun4/os/dvma.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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/types.h>
30*0Sstevel@tonic-gate #include <sys/param.h>
31*0Sstevel@tonic-gate #include <sys/systm.h>
32*0Sstevel@tonic-gate #include <sys/kmem.h>
33*0Sstevel@tonic-gate #include <sys/cpu.h>
34*0Sstevel@tonic-gate #include <sys/sunddi.h>
35*0Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
36*0Sstevel@tonic-gate #include <sys/pte.h>
37*0Sstevel@tonic-gate #include <sys/machsystm.h>
38*0Sstevel@tonic-gate #include <sys/mmu.h>
39*0Sstevel@tonic-gate #include <sys/dvma.h>
40*0Sstevel@tonic-gate #include <sys/debug.h>
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate #define	HD	((ddi_dma_impl_t *)h)->dmai_rdip
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate unsigned long
45*0Sstevel@tonic-gate dvma_pagesize(dev_info_t *dip)
46*0Sstevel@tonic-gate {
47*0Sstevel@tonic-gate 	auto unsigned long dvmapgsz;
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate 	(void) ddi_ctlops(dip, dip, DDI_CTLOPS_DVMAPAGESIZE,
50*0Sstevel@tonic-gate 	    NULL, (void *) &dvmapgsz);
51*0Sstevel@tonic-gate 	return (dvmapgsz);
52*0Sstevel@tonic-gate }
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate int
55*0Sstevel@tonic-gate dvma_reserve(dev_info_t *dip,  ddi_dma_lim_t *limp, uint_t pages,
56*0Sstevel@tonic-gate     ddi_dma_handle_t *handlep)
57*0Sstevel@tonic-gate {
58*0Sstevel@tonic-gate 	auto ddi_dma_lim_t dma_lim;
59*0Sstevel@tonic-gate 	auto ddi_dma_impl_t implhdl;
60*0Sstevel@tonic-gate 	struct ddi_dma_req dmareq;
61*0Sstevel@tonic-gate 	ddi_dma_handle_t reqhdl;
62*0Sstevel@tonic-gate 	ddi_dma_impl_t *mp;
63*0Sstevel@tonic-gate 	int ret;
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 	if (limp == (ddi_dma_lim_t *)0) {
66*0Sstevel@tonic-gate 		return (DDI_DMA_BADLIMITS);
67*0Sstevel@tonic-gate 	} else {
68*0Sstevel@tonic-gate 		dma_lim = *limp;
69*0Sstevel@tonic-gate 	}
70*0Sstevel@tonic-gate 	bzero(&dmareq, sizeof (dmareq));
71*0Sstevel@tonic-gate 	dmareq.dmar_fp = DDI_DMA_DONTWAIT;
72*0Sstevel@tonic-gate 	dmareq.dmar_flags = DDI_DMA_RDWR | DDI_DMA_STREAMING;
73*0Sstevel@tonic-gate 	dmareq.dmar_limits = &dma_lim;
74*0Sstevel@tonic-gate 	dmareq.dmar_object.dmao_size = pages;
75*0Sstevel@tonic-gate 	/*
76*0Sstevel@tonic-gate 	 * pass in a dummy handle. This avoids the problem when
77*0Sstevel@tonic-gate 	 * somebody is dereferencing the handle before checking
78*0Sstevel@tonic-gate 	 * the operation. This can be avoided once we separate
79*0Sstevel@tonic-gate 	 * handle allocation and actual operation.
80*0Sstevel@tonic-gate 	 */
81*0Sstevel@tonic-gate 	bzero((caddr_t)&implhdl, sizeof (ddi_dma_impl_t));
82*0Sstevel@tonic-gate 	reqhdl = (ddi_dma_handle_t)&implhdl;
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 	ret = ddi_dma_mctl(dip, dip, reqhdl, DDI_DMA_RESERVE, (off_t *)&dmareq,
85*0Sstevel@tonic-gate 	    0, (caddr_t *)handlep, 0);
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	if (ret == DDI_SUCCESS) {
88*0Sstevel@tonic-gate 		mp = (ddi_dma_impl_t *)(*handlep);
89*0Sstevel@tonic-gate 		if (!(mp->dmai_rflags & DMP_BYPASSNEXUS)) {
90*0Sstevel@tonic-gate 			uint_t np = mp->dmai_ndvmapages;
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 			mp->dmai_mapping = (ulong_t)kmem_alloc(
93*0Sstevel@tonic-gate 				sizeof (ddi_dma_lim_t), KM_SLEEP);
94*0Sstevel@tonic-gate 			bcopy((char *)&dma_lim, (char *)mp->dmai_mapping,
95*0Sstevel@tonic-gate 			    sizeof (ddi_dma_lim_t));
96*0Sstevel@tonic-gate 			mp->dmai_minfo = kmem_alloc(
97*0Sstevel@tonic-gate 				np * sizeof (ddi_dma_handle_t), KM_SLEEP);
98*0Sstevel@tonic-gate 		}
99*0Sstevel@tonic-gate 	}
100*0Sstevel@tonic-gate 	return (ret);
101*0Sstevel@tonic-gate }
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate void
104*0Sstevel@tonic-gate dvma_release(ddi_dma_handle_t h)
105*0Sstevel@tonic-gate {
106*0Sstevel@tonic-gate 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)h;
107*0Sstevel@tonic-gate 	uint_t np = mp->dmai_ndvmapages;
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	if (!(mp->dmai_rflags & DMP_BYPASSNEXUS)) {
110*0Sstevel@tonic-gate 		kmem_free((void *)mp->dmai_mapping, sizeof (ddi_dma_lim_t));
111*0Sstevel@tonic-gate 		kmem_free(mp->dmai_minfo, np * sizeof (ddi_dma_handle_t));
112*0Sstevel@tonic-gate 	}
113*0Sstevel@tonic-gate 	(void) ddi_dma_mctl(HD, HD, h, DDI_DMA_RELEASE, 0, 0, 0, 0);
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate }
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate void
118*0Sstevel@tonic-gate dvma_kaddr_load(ddi_dma_handle_t h, caddr_t a, uint_t len, uint_t index,
119*0Sstevel@tonic-gate 	ddi_dma_cookie_t *cp)
120*0Sstevel@tonic-gate {
121*0Sstevel@tonic-gate 	register ddi_dma_impl_t *mp = (ddi_dma_impl_t *)h;
122*0Sstevel@tonic-gate 	struct fast_dvma *nexus_private;
123*0Sstevel@tonic-gate 	struct dvma_ops *nexus_funcptr;
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 	if (mp->dmai_rflags & DMP_BYPASSNEXUS) {
126*0Sstevel@tonic-gate 		nexus_private = (struct fast_dvma *)mp->dmai_nexus_private;
127*0Sstevel@tonic-gate 		nexus_funcptr = (struct dvma_ops *)nexus_private->ops;
128*0Sstevel@tonic-gate 		(void) (*nexus_funcptr->dvma_kaddr_load)(h, a, len, index, cp);
129*0Sstevel@tonic-gate 	} else {
130*0Sstevel@tonic-gate 		ddi_dma_handle_t handle;
131*0Sstevel@tonic-gate 		ddi_dma_lim_t *limp;
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 		limp = (ddi_dma_lim_t *)mp->dmai_mapping;
134*0Sstevel@tonic-gate 		(void) ddi_dma_addr_setup(HD, NULL, a, len, DDI_DMA_RDWR,
135*0Sstevel@tonic-gate 					DDI_DMA_SLEEP, NULL, limp, &handle);
136*0Sstevel@tonic-gate 		((ddi_dma_handle_t *)mp->dmai_minfo)[index] = handle;
137*0Sstevel@tonic-gate 		(void) ddi_dma_htoc(handle, 0, cp);
138*0Sstevel@tonic-gate 	}
139*0Sstevel@tonic-gate }
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate /*ARGSUSED*/
142*0Sstevel@tonic-gate void
143*0Sstevel@tonic-gate dvma_unload(ddi_dma_handle_t h, uint_t objindex, uint_t type)
144*0Sstevel@tonic-gate {
145*0Sstevel@tonic-gate 	register ddi_dma_impl_t *mp = (ddi_dma_impl_t *)h;
146*0Sstevel@tonic-gate 	struct fast_dvma *nexus_private;
147*0Sstevel@tonic-gate 	struct dvma_ops *nexus_funcptr;
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 	if (mp->dmai_rflags & DMP_BYPASSNEXUS) {
150*0Sstevel@tonic-gate 		nexus_private = (struct fast_dvma *)mp->dmai_nexus_private;
151*0Sstevel@tonic-gate 		nexus_funcptr = (struct dvma_ops *)nexus_private->ops;
152*0Sstevel@tonic-gate 		(void) (*nexus_funcptr->dvma_unload)(h, objindex, type);
153*0Sstevel@tonic-gate 	} else {
154*0Sstevel@tonic-gate 		ddi_dma_handle_t handle;
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 		handle = ((ddi_dma_handle_t *)mp->dmai_minfo)[objindex];
157*0Sstevel@tonic-gate 		(void) ddi_dma_free(handle);
158*0Sstevel@tonic-gate 	}
159*0Sstevel@tonic-gate }
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate void
162*0Sstevel@tonic-gate dvma_sync(ddi_dma_handle_t h, uint_t objindex, uint_t type)
163*0Sstevel@tonic-gate {
164*0Sstevel@tonic-gate 	register ddi_dma_impl_t *mp = (ddi_dma_impl_t *)h;
165*0Sstevel@tonic-gate 	struct fast_dvma *nexus_private;
166*0Sstevel@tonic-gate 	struct dvma_ops *nexus_funcptr;
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 	if (mp->dmai_rflags & DMP_BYPASSNEXUS) {
169*0Sstevel@tonic-gate 		nexus_private = (struct fast_dvma *)mp->dmai_nexus_private;
170*0Sstevel@tonic-gate 		nexus_funcptr = (struct dvma_ops *)nexus_private->ops;
171*0Sstevel@tonic-gate 		(void) (*nexus_funcptr->dvma_sync)(h, objindex, type);
172*0Sstevel@tonic-gate 	} else {
173*0Sstevel@tonic-gate 		ddi_dma_handle_t handle;
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 		handle = ((ddi_dma_handle_t *)mp->dmai_minfo)[objindex];
176*0Sstevel@tonic-gate 		(void) ddi_dma_sync(handle, 0, 0, type);
177*0Sstevel@tonic-gate 	}
178*0Sstevel@tonic-gate }
179