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