bus_dma.c revision 1.47.2.4 1 1.47.2.4 nathanw /* $NetBSD: bus_dma.c,v 1.47.2.4 2002/10/18 02:34:05 nathanw Exp $ */
2 1.47.2.2 nathanw
3 1.47.2.2 nathanw /*-
4 1.47.2.2 nathanw * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5 1.47.2.2 nathanw * All rights reserved.
6 1.47.2.2 nathanw *
7 1.47.2.2 nathanw * This code is derived from software contributed to The NetBSD Foundation
8 1.47.2.2 nathanw * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.47.2.2 nathanw * NASA Ames Research Center.
10 1.47.2.2 nathanw *
11 1.47.2.2 nathanw * Redistribution and use in source and binary forms, with or without
12 1.47.2.2 nathanw * modification, are permitted provided that the following conditions
13 1.47.2.2 nathanw * are met:
14 1.47.2.2 nathanw * 1. Redistributions of source code must retain the above copyright
15 1.47.2.2 nathanw * notice, this list of conditions and the following disclaimer.
16 1.47.2.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
17 1.47.2.2 nathanw * notice, this list of conditions and the following disclaimer in the
18 1.47.2.2 nathanw * documentation and/or other materials provided with the distribution.
19 1.47.2.2 nathanw * 3. All advertising materials mentioning features or use of this software
20 1.47.2.2 nathanw * must display the following acknowledgement:
21 1.47.2.2 nathanw * This product includes software developed by the NetBSD
22 1.47.2.2 nathanw * Foundation, Inc. and its contributors.
23 1.47.2.2 nathanw * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.47.2.2 nathanw * contributors may be used to endorse or promote products derived
25 1.47.2.2 nathanw * from this software without specific prior written permission.
26 1.47.2.2 nathanw *
27 1.47.2.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.47.2.2 nathanw * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.47.2.2 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.47.2.2 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.47.2.2 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.47.2.2 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.47.2.2 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.47.2.2 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.47.2.2 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.47.2.2 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.47.2.2 nathanw * POSSIBILITY OF SUCH DAMAGE.
38 1.47.2.2 nathanw */
39 1.47.2.2 nathanw
40 1.47.2.2 nathanw #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
41 1.47.2.2 nathanw
42 1.47.2.4 nathanw __KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.47.2.4 2002/10/18 02:34:05 nathanw Exp $");
43 1.47.2.2 nathanw
44 1.47.2.2 nathanw #include <sys/param.h>
45 1.47.2.2 nathanw #include <sys/systm.h>
46 1.47.2.2 nathanw #include <sys/kernel.h>
47 1.47.2.2 nathanw #include <sys/device.h>
48 1.47.2.2 nathanw #include <sys/malloc.h>
49 1.47.2.2 nathanw #include <sys/proc.h>
50 1.47.2.2 nathanw #include <sys/mbuf.h>
51 1.47.2.2 nathanw
52 1.47.2.2 nathanw #include <uvm/uvm_extern.h>
53 1.47.2.2 nathanw
54 1.47.2.2 nathanw #define _ALPHA_BUS_DMA_PRIVATE
55 1.47.2.2 nathanw #include <machine/bus.h>
56 1.47.2.2 nathanw #include <machine/intr.h>
57 1.47.2.2 nathanw
58 1.47.2.2 nathanw int _bus_dmamap_load_buffer_direct(bus_dma_tag_t,
59 1.47.2.2 nathanw bus_dmamap_t, void *, bus_size_t, struct proc *, int,
60 1.47.2.2 nathanw paddr_t *, int *, int);
61 1.47.2.2 nathanw
62 1.47.2.2 nathanw extern paddr_t avail_start, avail_end; /* from pmap.c */
63 1.47.2.2 nathanw
64 1.47.2.2 nathanw /*
65 1.47.2.2 nathanw * Common function for DMA map creation. May be called by bus-specific
66 1.47.2.2 nathanw * DMA map creation functions.
67 1.47.2.2 nathanw */
68 1.47.2.2 nathanw int
69 1.47.2.2 nathanw _bus_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
70 1.47.2.2 nathanw bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp)
71 1.47.2.2 nathanw {
72 1.47.2.2 nathanw struct alpha_bus_dmamap *map;
73 1.47.2.2 nathanw void *mapstore;
74 1.47.2.2 nathanw size_t mapsize;
75 1.47.2.2 nathanw
76 1.47.2.2 nathanw /*
77 1.47.2.2 nathanw * Allocate and initialize the DMA map. The end of the map
78 1.47.2.2 nathanw * is a variable-sized array of segments, so we allocate enough
79 1.47.2.2 nathanw * room for them in one shot.
80 1.47.2.2 nathanw *
81 1.47.2.2 nathanw * Note we don't preserve the WAITOK or NOWAIT flags. Preservation
82 1.47.2.2 nathanw * of ALLOCNOW notifes others that we've reserved these resources,
83 1.47.2.2 nathanw * and they are not to be freed.
84 1.47.2.2 nathanw *
85 1.47.2.2 nathanw * The bus_dmamap_t includes one bus_dma_segment_t, hence
86 1.47.2.2 nathanw * the (nsegments - 1).
87 1.47.2.2 nathanw */
88 1.47.2.2 nathanw mapsize = sizeof(struct alpha_bus_dmamap) +
89 1.47.2.2 nathanw (sizeof(bus_dma_segment_t) * (nsegments - 1));
90 1.47.2.2 nathanw if ((mapstore = malloc(mapsize, M_DMAMAP,
91 1.47.2.2 nathanw (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL)
92 1.47.2.2 nathanw return (ENOMEM);
93 1.47.2.2 nathanw
94 1.47.2.2 nathanw memset(mapstore, 0, mapsize);
95 1.47.2.2 nathanw map = (struct alpha_bus_dmamap *)mapstore;
96 1.47.2.2 nathanw map->_dm_size = size;
97 1.47.2.2 nathanw map->_dm_segcnt = nsegments;
98 1.47.2.2 nathanw map->_dm_maxsegsz = maxsegsz;
99 1.47.2.2 nathanw if (t->_boundary != 0 && t->_boundary < boundary)
100 1.47.2.2 nathanw map->_dm_boundary = t->_boundary;
101 1.47.2.2 nathanw else
102 1.47.2.2 nathanw map->_dm_boundary = boundary;
103 1.47.2.2 nathanw map->_dm_flags = flags & ~(BUS_DMA_WAITOK|BUS_DMA_NOWAIT);
104 1.47.2.2 nathanw map->dm_mapsize = 0; /* no valid mappings */
105 1.47.2.2 nathanw map->dm_nsegs = 0;
106 1.47.2.3 nathanw map->_dm_window = NULL;
107 1.47.2.2 nathanw
108 1.47.2.2 nathanw *dmamp = map;
109 1.47.2.2 nathanw return (0);
110 1.47.2.2 nathanw }
111 1.47.2.2 nathanw
112 1.47.2.2 nathanw /*
113 1.47.2.2 nathanw * Common function for DMA map destruction. May be called by bus-specific
114 1.47.2.2 nathanw * DMA map destruction functions.
115 1.47.2.2 nathanw */
116 1.47.2.2 nathanw void
117 1.47.2.2 nathanw _bus_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
118 1.47.2.2 nathanw {
119 1.47.2.2 nathanw
120 1.47.2.2 nathanw free(map, M_DMAMAP);
121 1.47.2.2 nathanw }
122 1.47.2.2 nathanw
123 1.47.2.2 nathanw /*
124 1.47.2.2 nathanw * Utility function to load a linear buffer. lastaddrp holds state
125 1.47.2.2 nathanw * between invocations (for multiple-buffer loads). segp contains
126 1.47.2.2 nathanw * the starting segment on entrance, and the ending segment on exit.
127 1.47.2.2 nathanw * first indicates if this is the first invocation of this function.
128 1.47.2.2 nathanw */
129 1.47.2.2 nathanw int
130 1.47.2.2 nathanw _bus_dmamap_load_buffer_direct(bus_dma_tag_t t, bus_dmamap_t map,
131 1.47.2.2 nathanw void *buf, size_t buflen, struct proc *p, int flags, paddr_t *lastaddrp,
132 1.47.2.2 nathanw int *segp, int first)
133 1.47.2.2 nathanw {
134 1.47.2.2 nathanw bus_size_t sgsize;
135 1.47.2.2 nathanw bus_addr_t curaddr, lastaddr, baddr, bmask;
136 1.47.2.2 nathanw vaddr_t vaddr = (vaddr_t)buf;
137 1.47.2.2 nathanw int seg;
138 1.47.2.2 nathanw
139 1.47.2.2 nathanw lastaddr = *lastaddrp;
140 1.47.2.2 nathanw bmask = ~(map->_dm_boundary - 1);
141 1.47.2.2 nathanw
142 1.47.2.2 nathanw for (seg = *segp; buflen > 0 ; ) {
143 1.47.2.2 nathanw /*
144 1.47.2.2 nathanw * Get the physical address for this segment.
145 1.47.2.2 nathanw */
146 1.47.2.2 nathanw if (p != NULL)
147 1.47.2.2 nathanw (void) pmap_extract(p->p_vmspace->vm_map.pmap,
148 1.47.2.2 nathanw vaddr, &curaddr);
149 1.47.2.2 nathanw else
150 1.47.2.2 nathanw curaddr = vtophys(vaddr);
151 1.47.2.2 nathanw
152 1.47.2.2 nathanw /*
153 1.47.2.2 nathanw * If we're beyond the current DMA window, indicate
154 1.47.2.2 nathanw * that and try to fall back into SGMAPs.
155 1.47.2.2 nathanw */
156 1.47.2.2 nathanw if (t->_wsize != 0 && curaddr >= t->_wsize)
157 1.47.2.2 nathanw return (EINVAL);
158 1.47.2.2 nathanw
159 1.47.2.2 nathanw curaddr |= t->_wbase;
160 1.47.2.2 nathanw
161 1.47.2.2 nathanw /*
162 1.47.2.2 nathanw * Compute the segment size, and adjust counts.
163 1.47.2.2 nathanw */
164 1.47.2.2 nathanw sgsize = NBPG - ((u_long)vaddr & PGOFSET);
165 1.47.2.2 nathanw if (buflen < sgsize)
166 1.47.2.2 nathanw sgsize = buflen;
167 1.47.2.2 nathanw if (map->_dm_maxsegsz < sgsize)
168 1.47.2.2 nathanw sgsize = map->_dm_maxsegsz;
169 1.47.2.2 nathanw
170 1.47.2.2 nathanw /*
171 1.47.2.2 nathanw * Make sure we don't cross any boundaries.
172 1.47.2.2 nathanw */
173 1.47.2.2 nathanw if (map->_dm_boundary > 0) {
174 1.47.2.2 nathanw baddr = (curaddr + map->_dm_boundary) & bmask;
175 1.47.2.2 nathanw if (sgsize > (baddr - curaddr))
176 1.47.2.2 nathanw sgsize = (baddr - curaddr);
177 1.47.2.2 nathanw }
178 1.47.2.2 nathanw
179 1.47.2.2 nathanw /*
180 1.47.2.2 nathanw * Insert chunk into a segment, coalescing with
181 1.47.2.2 nathanw * the previous segment if possible.
182 1.47.2.2 nathanw */
183 1.47.2.2 nathanw if (first) {
184 1.47.2.2 nathanw map->dm_segs[seg].ds_addr = curaddr;
185 1.47.2.2 nathanw map->dm_segs[seg].ds_len = sgsize;
186 1.47.2.2 nathanw first = 0;
187 1.47.2.2 nathanw } else {
188 1.47.2.2 nathanw if ((map->_dm_flags & DMAMAP_NO_COALESCE) == 0 &&
189 1.47.2.2 nathanw curaddr == lastaddr &&
190 1.47.2.2 nathanw (map->dm_segs[seg].ds_len + sgsize) <=
191 1.47.2.2 nathanw map->_dm_maxsegsz &&
192 1.47.2.2 nathanw (map->_dm_boundary == 0 ||
193 1.47.2.2 nathanw (map->dm_segs[seg].ds_addr & bmask) ==
194 1.47.2.2 nathanw (curaddr & bmask)))
195 1.47.2.2 nathanw map->dm_segs[seg].ds_len += sgsize;
196 1.47.2.2 nathanw else {
197 1.47.2.2 nathanw if (++seg >= map->_dm_segcnt)
198 1.47.2.2 nathanw break;
199 1.47.2.2 nathanw map->dm_segs[seg].ds_addr = curaddr;
200 1.47.2.2 nathanw map->dm_segs[seg].ds_len = sgsize;
201 1.47.2.2 nathanw }
202 1.47.2.2 nathanw }
203 1.47.2.2 nathanw
204 1.47.2.2 nathanw lastaddr = curaddr + sgsize;
205 1.47.2.2 nathanw vaddr += sgsize;
206 1.47.2.2 nathanw buflen -= sgsize;
207 1.47.2.2 nathanw }
208 1.47.2.2 nathanw
209 1.47.2.2 nathanw *segp = seg;
210 1.47.2.2 nathanw *lastaddrp = lastaddr;
211 1.47.2.2 nathanw
212 1.47.2.2 nathanw /*
213 1.47.2.2 nathanw * Did we fit?
214 1.47.2.2 nathanw */
215 1.47.2.2 nathanw if (buflen != 0) {
216 1.47.2.2 nathanw /*
217 1.47.2.2 nathanw * If there is a chained window, we will automatically
218 1.47.2.2 nathanw * fall back to it.
219 1.47.2.2 nathanw */
220 1.47.2.2 nathanw return (EFBIG); /* XXX better return value here? */
221 1.47.2.2 nathanw }
222 1.47.2.2 nathanw
223 1.47.2.2 nathanw return (0);
224 1.47.2.2 nathanw }
225 1.47.2.2 nathanw
226 1.47.2.2 nathanw /*
227 1.47.2.2 nathanw * Common function for loading a direct-mapped DMA map with a linear
228 1.47.2.2 nathanw * buffer. Called by bus-specific DMA map load functions with the
229 1.47.2.2 nathanw * OR value appropriate for indicating "direct-mapped" for that
230 1.47.2.2 nathanw * chipset.
231 1.47.2.2 nathanw */
232 1.47.2.2 nathanw int
233 1.47.2.2 nathanw _bus_dmamap_load_direct(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
234 1.47.2.2 nathanw bus_size_t buflen, struct proc *p, int flags)
235 1.47.2.2 nathanw {
236 1.47.2.2 nathanw paddr_t lastaddr;
237 1.47.2.2 nathanw int seg, error;
238 1.47.2.2 nathanw
239 1.47.2.2 nathanw /*
240 1.47.2.2 nathanw * Make sure that on error condition we return "no valid mappings".
241 1.47.2.2 nathanw */
242 1.47.2.2 nathanw map->dm_mapsize = 0;
243 1.47.2.2 nathanw map->dm_nsegs = 0;
244 1.47.2.2 nathanw
245 1.47.2.2 nathanw if (buflen > map->_dm_size)
246 1.47.2.2 nathanw return (EINVAL);
247 1.47.2.2 nathanw
248 1.47.2.2 nathanw seg = 0;
249 1.47.2.2 nathanw error = _bus_dmamap_load_buffer_direct(t, map, buf, buflen,
250 1.47.2.2 nathanw p, flags, &lastaddr, &seg, 1);
251 1.47.2.2 nathanw if (error == 0) {
252 1.47.2.2 nathanw map->dm_mapsize = buflen;
253 1.47.2.2 nathanw map->dm_nsegs = seg + 1;
254 1.47.2.3 nathanw map->_dm_window = t;
255 1.47.2.2 nathanw } else if (t->_next_window != NULL) {
256 1.47.2.2 nathanw /*
257 1.47.2.2 nathanw * Give the next window a chance.
258 1.47.2.2 nathanw */
259 1.47.2.2 nathanw error = bus_dmamap_load(t->_next_window, map, buf, buflen,
260 1.47.2.2 nathanw p, flags);
261 1.47.2.2 nathanw }
262 1.47.2.2 nathanw return (error);
263 1.47.2.2 nathanw }
264 1.47.2.2 nathanw
265 1.47.2.2 nathanw /*
266 1.47.2.2 nathanw * Like _bus_dmamap_load_direct(), but for mbufs.
267 1.47.2.2 nathanw */
268 1.47.2.2 nathanw int
269 1.47.2.2 nathanw _bus_dmamap_load_mbuf_direct(bus_dma_tag_t t, bus_dmamap_t map,
270 1.47.2.2 nathanw struct mbuf *m0, int flags)
271 1.47.2.2 nathanw {
272 1.47.2.2 nathanw paddr_t lastaddr;
273 1.47.2.2 nathanw int seg, error, first;
274 1.47.2.2 nathanw struct mbuf *m;
275 1.47.2.2 nathanw
276 1.47.2.2 nathanw /*
277 1.47.2.2 nathanw * Make sure that on error condition we return "no valid mappings."
278 1.47.2.2 nathanw */
279 1.47.2.2 nathanw map->dm_mapsize = 0;
280 1.47.2.2 nathanw map->dm_nsegs = 0;
281 1.47.2.2 nathanw
282 1.47.2.2 nathanw #ifdef DIAGNOSTIC
283 1.47.2.2 nathanw if ((m0->m_flags & M_PKTHDR) == 0)
284 1.47.2.2 nathanw panic("_bus_dmamap_load_mbuf_direct: no packet header");
285 1.47.2.2 nathanw #endif
286 1.47.2.2 nathanw
287 1.47.2.2 nathanw if (m0->m_pkthdr.len > map->_dm_size)
288 1.47.2.2 nathanw return (EINVAL);
289 1.47.2.2 nathanw
290 1.47.2.2 nathanw first = 1;
291 1.47.2.2 nathanw seg = 0;
292 1.47.2.2 nathanw error = 0;
293 1.47.2.2 nathanw for (m = m0; m != NULL && error == 0; m = m->m_next) {
294 1.47.2.2 nathanw error = _bus_dmamap_load_buffer_direct(t, map,
295 1.47.2.2 nathanw m->m_data, m->m_len, NULL, flags, &lastaddr, &seg, first);
296 1.47.2.2 nathanw first = 0;
297 1.47.2.2 nathanw }
298 1.47.2.2 nathanw if (error == 0) {
299 1.47.2.2 nathanw map->dm_mapsize = m0->m_pkthdr.len;
300 1.47.2.2 nathanw map->dm_nsegs = seg + 1;
301 1.47.2.3 nathanw map->_dm_window = t;
302 1.47.2.2 nathanw } else if (t->_next_window != NULL) {
303 1.47.2.2 nathanw /*
304 1.47.2.2 nathanw * Give the next window a chance.
305 1.47.2.2 nathanw */
306 1.47.2.2 nathanw error = bus_dmamap_load_mbuf(t->_next_window, map, m0, flags);
307 1.47.2.2 nathanw }
308 1.47.2.2 nathanw return (error);
309 1.47.2.2 nathanw }
310 1.47.2.2 nathanw
311 1.47.2.2 nathanw /*
312 1.47.2.2 nathanw * Like _bus_dmamap_load_direct(), but for uios.
313 1.47.2.2 nathanw */
314 1.47.2.2 nathanw int
315 1.47.2.2 nathanw _bus_dmamap_load_uio_direct(bus_dma_tag_t t, bus_dmamap_t map,
316 1.47.2.2 nathanw struct uio *uio, int flags)
317 1.47.2.2 nathanw {
318 1.47.2.2 nathanw paddr_t lastaddr;
319 1.47.2.2 nathanw int seg, i, error, first;
320 1.47.2.2 nathanw bus_size_t minlen, resid;
321 1.47.2.2 nathanw struct proc *p = NULL;
322 1.47.2.2 nathanw struct iovec *iov;
323 1.47.2.2 nathanw caddr_t addr;
324 1.47.2.2 nathanw
325 1.47.2.2 nathanw /*
326 1.47.2.2 nathanw * Make sure that on error condition we return "no valid mappings."
327 1.47.2.2 nathanw */
328 1.47.2.2 nathanw map->dm_mapsize = 0;
329 1.47.2.2 nathanw map->dm_nsegs = 0;
330 1.47.2.2 nathanw
331 1.47.2.2 nathanw resid = uio->uio_resid;
332 1.47.2.2 nathanw iov = uio->uio_iov;
333 1.47.2.2 nathanw
334 1.47.2.2 nathanw if (uio->uio_segflg == UIO_USERSPACE) {
335 1.47.2.2 nathanw p = uio->uio_procp;
336 1.47.2.2 nathanw #ifdef DIAGNOSTIC
337 1.47.2.2 nathanw if (p == NULL)
338 1.47.2.2 nathanw panic("_bus_dmamap_load_uio_direct: "
339 1.47.2.2 nathanw "USERSPACE but no proc");
340 1.47.2.2 nathanw #endif
341 1.47.2.2 nathanw }
342 1.47.2.2 nathanw
343 1.47.2.2 nathanw first = 1;
344 1.47.2.2 nathanw seg = 0;
345 1.47.2.2 nathanw error = 0;
346 1.47.2.2 nathanw for (i = 0; i < uio->uio_iovcnt && resid != 0 && error == 0; i++) {
347 1.47.2.2 nathanw /*
348 1.47.2.2 nathanw * Now at the first iovec to load. Load each iovec
349 1.47.2.2 nathanw * until we have exhausted the residual count.
350 1.47.2.2 nathanw */
351 1.47.2.2 nathanw minlen = resid < iov[i].iov_len ? resid : iov[i].iov_len;
352 1.47.2.2 nathanw addr = (caddr_t)iov[i].iov_base;
353 1.47.2.2 nathanw
354 1.47.2.2 nathanw error = _bus_dmamap_load_buffer_direct(t, map,
355 1.47.2.2 nathanw addr, minlen, p, flags, &lastaddr, &seg, first);
356 1.47.2.2 nathanw first = 0;
357 1.47.2.2 nathanw
358 1.47.2.2 nathanw resid -= minlen;
359 1.47.2.2 nathanw }
360 1.47.2.2 nathanw if (error == 0) {
361 1.47.2.2 nathanw map->dm_mapsize = uio->uio_resid;
362 1.47.2.2 nathanw map->dm_nsegs = seg + 1;
363 1.47.2.3 nathanw map->_dm_window = t;
364 1.47.2.2 nathanw } else if (t->_next_window != NULL) {
365 1.47.2.2 nathanw /*
366 1.47.2.2 nathanw * Give the next window a chance.
367 1.47.2.2 nathanw */
368 1.47.2.2 nathanw error = bus_dmamap_load_uio(t->_next_window, map, uio, flags);
369 1.47.2.2 nathanw }
370 1.47.2.2 nathanw return (error);
371 1.47.2.2 nathanw }
372 1.47.2.2 nathanw
373 1.47.2.2 nathanw /*
374 1.47.2.2 nathanw * Like _bus_dmamap_load_direct(), but for raw memory.
375 1.47.2.2 nathanw */
376 1.47.2.2 nathanw int
377 1.47.2.2 nathanw _bus_dmamap_load_raw_direct(bus_dma_tag_t t, bus_dmamap_t map,
378 1.47.2.2 nathanw bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
379 1.47.2.2 nathanw {
380 1.47.2.2 nathanw
381 1.47.2.2 nathanw panic("_bus_dmamap_load_raw_direct: not implemented");
382 1.47.2.2 nathanw }
383 1.47.2.2 nathanw
384 1.47.2.2 nathanw /*
385 1.47.2.2 nathanw * Common function for unloading a DMA map. May be called by
386 1.47.2.2 nathanw * chipset-specific DMA map unload functions.
387 1.47.2.2 nathanw */
388 1.47.2.2 nathanw void
389 1.47.2.2 nathanw _bus_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
390 1.47.2.2 nathanw {
391 1.47.2.2 nathanw
392 1.47.2.2 nathanw /*
393 1.47.2.2 nathanw * No resources to free; just mark the mappings as
394 1.47.2.2 nathanw * invalid.
395 1.47.2.2 nathanw */
396 1.47.2.2 nathanw map->dm_mapsize = 0;
397 1.47.2.2 nathanw map->dm_nsegs = 0;
398 1.47.2.3 nathanw map->_dm_window = NULL;
399 1.47.2.2 nathanw }
400 1.47.2.2 nathanw
401 1.47.2.2 nathanw /*
402 1.47.2.2 nathanw * Common function for DMA map synchronization. May be called
403 1.47.2.2 nathanw * by chipset-specific DMA map synchronization functions.
404 1.47.2.2 nathanw */
405 1.47.2.2 nathanw void
406 1.47.2.2 nathanw _bus_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
407 1.47.2.2 nathanw bus_size_t len, int ops)
408 1.47.2.2 nathanw {
409 1.47.2.2 nathanw
410 1.47.2.2 nathanw /*
411 1.47.2.2 nathanw * Flush the store buffer.
412 1.47.2.2 nathanw */
413 1.47.2.2 nathanw alpha_mb();
414 1.47.2.2 nathanw }
415 1.47.2.2 nathanw
416 1.47.2.2 nathanw /*
417 1.47.2.2 nathanw * Common function for DMA-safe memory allocation. May be called
418 1.47.2.2 nathanw * by bus-specific DMA memory allocation functions.
419 1.47.2.2 nathanw */
420 1.47.2.2 nathanw int
421 1.47.2.2 nathanw _bus_dmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
422 1.47.2.2 nathanw bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
423 1.47.2.2 nathanw int flags)
424 1.47.2.2 nathanw {
425 1.47.2.2 nathanw
426 1.47.2.2 nathanw return (_bus_dmamem_alloc_range(t, size, alignment, boundary,
427 1.47.2.2 nathanw segs, nsegs, rsegs, flags, 0, trunc_page(avail_end)));
428 1.47.2.2 nathanw }
429 1.47.2.2 nathanw
430 1.47.2.2 nathanw /*
431 1.47.2.2 nathanw * Allocate physical memory from the given physical address range.
432 1.47.2.2 nathanw * Called by DMA-safe memory allocation methods.
433 1.47.2.2 nathanw */
434 1.47.2.2 nathanw int
435 1.47.2.2 nathanw _bus_dmamem_alloc_range(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
436 1.47.2.2 nathanw bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
437 1.47.2.2 nathanw int flags, paddr_t low, paddr_t high)
438 1.47.2.2 nathanw {
439 1.47.2.2 nathanw paddr_t curaddr, lastaddr;
440 1.47.2.2 nathanw struct vm_page *m;
441 1.47.2.2 nathanw struct pglist mlist;
442 1.47.2.2 nathanw int curseg, error;
443 1.47.2.2 nathanw
444 1.47.2.2 nathanw /* Always round the size. */
445 1.47.2.2 nathanw size = round_page(size);
446 1.47.2.2 nathanw
447 1.47.2.2 nathanw /*
448 1.47.2.2 nathanw * Allocate pages from the VM system.
449 1.47.2.2 nathanw */
450 1.47.2.2 nathanw error = uvm_pglistalloc(size, low, high, alignment, boundary,
451 1.47.2.2 nathanw &mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0);
452 1.47.2.2 nathanw if (error)
453 1.47.2.2 nathanw return (error);
454 1.47.2.2 nathanw
455 1.47.2.2 nathanw /*
456 1.47.2.2 nathanw * Compute the location, size, and number of segments actually
457 1.47.2.2 nathanw * returned by the VM code.
458 1.47.2.2 nathanw */
459 1.47.2.2 nathanw m = mlist.tqh_first;
460 1.47.2.2 nathanw curseg = 0;
461 1.47.2.2 nathanw lastaddr = segs[curseg].ds_addr = VM_PAGE_TO_PHYS(m);
462 1.47.2.2 nathanw segs[curseg].ds_len = PAGE_SIZE;
463 1.47.2.2 nathanw m = m->pageq.tqe_next;
464 1.47.2.2 nathanw
465 1.47.2.2 nathanw for (; m != NULL; m = m->pageq.tqe_next) {
466 1.47.2.2 nathanw curaddr = VM_PAGE_TO_PHYS(m);
467 1.47.2.2 nathanw #ifdef DIAGNOSTIC
468 1.47.2.2 nathanw if (curaddr < avail_start || curaddr >= high) {
469 1.47.2.2 nathanw printf("uvm_pglistalloc returned non-sensical"
470 1.47.2.2 nathanw " address 0x%lx\n", curaddr);
471 1.47.2.2 nathanw panic("_bus_dmamem_alloc");
472 1.47.2.2 nathanw }
473 1.47.2.2 nathanw #endif
474 1.47.2.2 nathanw if (curaddr == (lastaddr + PAGE_SIZE))
475 1.47.2.2 nathanw segs[curseg].ds_len += PAGE_SIZE;
476 1.47.2.2 nathanw else {
477 1.47.2.2 nathanw curseg++;
478 1.47.2.2 nathanw segs[curseg].ds_addr = curaddr;
479 1.47.2.2 nathanw segs[curseg].ds_len = PAGE_SIZE;
480 1.47.2.2 nathanw }
481 1.47.2.2 nathanw lastaddr = curaddr;
482 1.47.2.2 nathanw }
483 1.47.2.2 nathanw
484 1.47.2.2 nathanw *rsegs = curseg + 1;
485 1.47.2.2 nathanw
486 1.47.2.2 nathanw return (0);
487 1.47.2.2 nathanw }
488 1.47.2.2 nathanw
489 1.47.2.2 nathanw /*
490 1.47.2.2 nathanw * Common function for freeing DMA-safe memory. May be called by
491 1.47.2.2 nathanw * bus-specific DMA memory free functions.
492 1.47.2.2 nathanw */
493 1.47.2.2 nathanw void
494 1.47.2.2 nathanw _bus_dmamem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs)
495 1.47.2.2 nathanw {
496 1.47.2.2 nathanw struct vm_page *m;
497 1.47.2.2 nathanw bus_addr_t addr;
498 1.47.2.2 nathanw struct pglist mlist;
499 1.47.2.2 nathanw int curseg;
500 1.47.2.2 nathanw
501 1.47.2.2 nathanw /*
502 1.47.2.2 nathanw * Build a list of pages to free back to the VM system.
503 1.47.2.2 nathanw */
504 1.47.2.2 nathanw TAILQ_INIT(&mlist);
505 1.47.2.2 nathanw for (curseg = 0; curseg < nsegs; curseg++) {
506 1.47.2.2 nathanw for (addr = segs[curseg].ds_addr;
507 1.47.2.2 nathanw addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
508 1.47.2.2 nathanw addr += PAGE_SIZE) {
509 1.47.2.2 nathanw m = PHYS_TO_VM_PAGE(addr);
510 1.47.2.2 nathanw TAILQ_INSERT_TAIL(&mlist, m, pageq);
511 1.47.2.2 nathanw }
512 1.47.2.2 nathanw }
513 1.47.2.2 nathanw
514 1.47.2.2 nathanw uvm_pglistfree(&mlist);
515 1.47.2.2 nathanw }
516 1.47.2.2 nathanw
517 1.47.2.2 nathanw /*
518 1.47.2.2 nathanw * Common function for mapping DMA-safe memory. May be called by
519 1.47.2.2 nathanw * bus-specific DMA memory map functions.
520 1.47.2.2 nathanw */
521 1.47.2.2 nathanw int
522 1.47.2.2 nathanw _bus_dmamem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
523 1.47.2.2 nathanw size_t size, caddr_t *kvap, int flags)
524 1.47.2.2 nathanw {
525 1.47.2.2 nathanw vaddr_t va;
526 1.47.2.2 nathanw bus_addr_t addr;
527 1.47.2.2 nathanw int curseg;
528 1.47.2.2 nathanw
529 1.47.2.2 nathanw /*
530 1.47.2.2 nathanw * If we're only mapping 1 segment, use K0SEG, to avoid
531 1.47.2.2 nathanw * TLB thrashing.
532 1.47.2.2 nathanw */
533 1.47.2.2 nathanw if (nsegs == 1) {
534 1.47.2.2 nathanw *kvap = (caddr_t)ALPHA_PHYS_TO_K0SEG(segs[0].ds_addr);
535 1.47.2.2 nathanw return (0);
536 1.47.2.2 nathanw }
537 1.47.2.2 nathanw
538 1.47.2.2 nathanw size = round_page(size);
539 1.47.2.2 nathanw
540 1.47.2.2 nathanw va = uvm_km_valloc(kernel_map, size);
541 1.47.2.2 nathanw
542 1.47.2.2 nathanw if (va == 0)
543 1.47.2.2 nathanw return (ENOMEM);
544 1.47.2.2 nathanw
545 1.47.2.2 nathanw *kvap = (caddr_t)va;
546 1.47.2.2 nathanw
547 1.47.2.2 nathanw for (curseg = 0; curseg < nsegs; curseg++) {
548 1.47.2.2 nathanw for (addr = segs[curseg].ds_addr;
549 1.47.2.2 nathanw addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
550 1.47.2.2 nathanw addr += NBPG, va += NBPG, size -= NBPG) {
551 1.47.2.2 nathanw if (size == 0)
552 1.47.2.2 nathanw panic("_bus_dmamem_map: size botch");
553 1.47.2.2 nathanw pmap_enter(pmap_kernel(), va, addr,
554 1.47.2.2 nathanw VM_PROT_READ | VM_PROT_WRITE,
555 1.47.2.2 nathanw PMAP_WIRED | VM_PROT_READ | VM_PROT_WRITE);
556 1.47.2.2 nathanw }
557 1.47.2.2 nathanw }
558 1.47.2.2 nathanw pmap_update(pmap_kernel());
559 1.47.2.2 nathanw
560 1.47.2.2 nathanw return (0);
561 1.47.2.2 nathanw }
562 1.47.2.2 nathanw
563 1.47.2.2 nathanw /*
564 1.47.2.2 nathanw * Common function for unmapping DMA-safe memory. May be called by
565 1.47.2.2 nathanw * bus-specific DMA memory unmapping functions.
566 1.47.2.2 nathanw */
567 1.47.2.2 nathanw void
568 1.47.2.2 nathanw _bus_dmamem_unmap(bus_dma_tag_t t, caddr_t kva, size_t size)
569 1.47.2.2 nathanw {
570 1.47.2.2 nathanw
571 1.47.2.2 nathanw #ifdef DIAGNOSTIC
572 1.47.2.2 nathanw if ((u_long)kva & PGOFSET)
573 1.47.2.2 nathanw panic("_bus_dmamem_unmap");
574 1.47.2.2 nathanw #endif
575 1.47.2.2 nathanw
576 1.47.2.2 nathanw /*
577 1.47.2.2 nathanw * Nothing to do if we mapped it with K0SEG.
578 1.47.2.2 nathanw */
579 1.47.2.2 nathanw if (kva >= (caddr_t)ALPHA_K0SEG_BASE &&
580 1.47.2.2 nathanw kva <= (caddr_t)ALPHA_K0SEG_END)
581 1.47.2.2 nathanw return;
582 1.47.2.2 nathanw
583 1.47.2.2 nathanw size = round_page(size);
584 1.47.2.2 nathanw uvm_km_free(kernel_map, (vaddr_t)kva, size);
585 1.47.2.2 nathanw }
586 1.47.2.2 nathanw
587 1.47.2.2 nathanw /*
588 1.47.2.2 nathanw * Common functin for mmap(2)'ing DMA-safe memory. May be called by
589 1.47.2.2 nathanw * bus-specific DMA mmap(2)'ing functions.
590 1.47.2.2 nathanw */
591 1.47.2.2 nathanw paddr_t
592 1.47.2.2 nathanw _bus_dmamem_mmap(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
593 1.47.2.2 nathanw off_t off, int prot, int flags)
594 1.47.2.2 nathanw {
595 1.47.2.2 nathanw int i;
596 1.47.2.2 nathanw
597 1.47.2.2 nathanw for (i = 0; i < nsegs; i++) {
598 1.47.2.2 nathanw #ifdef DIAGNOSTIC
599 1.47.2.2 nathanw if (off & PGOFSET)
600 1.47.2.2 nathanw panic("_bus_dmamem_mmap: offset unaligned");
601 1.47.2.2 nathanw if (segs[i].ds_addr & PGOFSET)
602 1.47.2.2 nathanw panic("_bus_dmamem_mmap: segment unaligned");
603 1.47.2.2 nathanw if (segs[i].ds_len & PGOFSET)
604 1.47.2.2 nathanw panic("_bus_dmamem_mmap: segment size not multiple"
605 1.47.2.2 nathanw " of page size");
606 1.47.2.2 nathanw #endif
607 1.47.2.2 nathanw if (off >= segs[i].ds_len) {
608 1.47.2.2 nathanw off -= segs[i].ds_len;
609 1.47.2.2 nathanw continue;
610 1.47.2.2 nathanw }
611 1.47.2.2 nathanw
612 1.47.2.2 nathanw return (alpha_btop((caddr_t)segs[i].ds_addr + off));
613 1.47.2.2 nathanw }
614 1.47.2.2 nathanw
615 1.47.2.2 nathanw /* Page not found. */
616 1.47.2.2 nathanw return (-1);
617 1.47.2.2 nathanw }
618