]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/powerpc/powerpc/busdma_machdep.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / powerpc / powerpc / busdma_machdep.c
1 /*-
2  * Copyright (c) 2002 Peter Grehan
3  * Copyright (c) 1997, 1998 Justin T. Gibbs.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions, and the following disclaimer,
11  *    without modification, immediately at the beginning of the file.
12  * 2. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  *   From i386/busdma_machdep.c,v 1.26 2002/04/19 22:58:09 alfred
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 /*
34  * Bus dma support routines
35  */
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/bus.h>
41 #include <sys/interrupt.h>
42 #include <sys/lock.h>
43 #include <sys/proc.h>
44 #include <sys/mutex.h>
45 #include <sys/mbuf.h>
46 #include <sys/uio.h>
47
48 #include <vm/vm.h>
49 #include <vm/vm_page.h>
50 #include <vm/vm_map.h>
51
52 #include <machine/atomic.h>
53 #include <machine/bus.h>
54 #include <machine/cpufunc.h>
55
56 struct bus_dma_tag {
57         bus_dma_tag_t     parent;
58         bus_size_t        alignment;
59         bus_size_t        boundary;
60         bus_addr_t        lowaddr;
61         bus_addr_t        highaddr;
62         bus_dma_filter_t *filter;
63         void             *filterarg;
64         bus_size_t        maxsize;
65         u_int             nsegments;
66         bus_size_t        maxsegsz;
67         int               flags;
68         int               ref_count;
69         int               map_count;
70         bus_dma_lock_t   *lockfunc;
71         void             *lockfuncarg;
72 };
73
74 struct bus_dmamap {
75         bus_dma_tag_t          dmat;
76         void                  *buf;             /* unmapped buffer pointer */
77         bus_size_t             buflen;          /* unmapped buffer length */
78         bus_dmamap_callback_t *callback;
79         void                  *callback_arg;
80 };
81
82 /*
83  * Convenience function for manipulating driver locks from busdma (during
84  * busdma_swi, for example).  Drivers that don't provide their own locks
85  * should specify &Giant to dmat->lockfuncarg.  Drivers that use their own
86  * non-mutex locking scheme don't have to use this at all.
87  */
88 void
89 busdma_lock_mutex(void *arg, bus_dma_lock_op_t op)
90 {
91         struct mtx *dmtx;
92
93         dmtx = (struct mtx *)arg;
94         switch (op) {
95         case BUS_DMA_LOCK:
96                 mtx_lock(dmtx);
97                 break;
98         case BUS_DMA_UNLOCK:
99                 mtx_unlock(dmtx);
100                 break;
101         default:
102                 panic("Unknown operation 0x%x for busdma_lock_mutex!", op);
103         }
104 }
105
106 /*
107  * dflt_lock should never get called.  It gets put into the dma tag when
108  * lockfunc == NULL, which is only valid if the maps that are associated
109  * with the tag are meant to never be defered.
110  * XXX Should have a way to identify which driver is responsible here.
111  */
112 static void
113 dflt_lock(void *arg, bus_dma_lock_op_t op)
114 {
115 #ifdef INVARIANTS
116         panic("driver error: busdma dflt_lock called");
117 #else
118         printf("DRIVER_ERROR: busdma dflt_lock called\n");
119 #endif
120 }
121
122 /*
123  * Allocate a device specific dma_tag.
124  */
125 int
126 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
127     bus_size_t boundary, bus_addr_t lowaddr, bus_addr_t highaddr,
128     bus_dma_filter_t *filter, void *filterarg, bus_size_t maxsize,
129     int nsegments, bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
130     void *lockfuncarg, bus_dma_tag_t *dmat)
131 {
132         bus_dma_tag_t newtag;
133         int error = 0;
134
135         /* Return a NULL tag on failure */
136         *dmat = NULL;
137
138         newtag = (bus_dma_tag_t)malloc(sizeof(*newtag), M_DEVBUF, M_NOWAIT);
139         if (newtag == NULL)
140                 return (ENOMEM);
141
142         newtag->parent = parent;
143         newtag->alignment = alignment;
144         newtag->boundary = boundary;
145         newtag->lowaddr = trunc_page((vm_offset_t)lowaddr) + (PAGE_SIZE - 1);
146         newtag->highaddr = trunc_page((vm_offset_t)highaddr) + (PAGE_SIZE - 1);
147         newtag->filter = filter;
148         newtag->filterarg = filterarg;
149         newtag->maxsize = maxsize;
150         newtag->nsegments = nsegments;
151         newtag->maxsegsz = maxsegsz;
152         newtag->flags = flags;
153         newtag->ref_count = 1; /* Count ourself */
154         newtag->map_count = 0;
155         if (lockfunc != NULL) {
156                 newtag->lockfunc = lockfunc;
157                 newtag->lockfuncarg = lockfuncarg;
158         } else {
159                 newtag->lockfunc = dflt_lock;
160                 newtag->lockfuncarg = NULL;
161         }
162
163         /*
164          * Take into account any restrictions imposed by our parent tag
165          */
166         if (parent != NULL) {
167                 newtag->lowaddr = min(parent->lowaddr, newtag->lowaddr);
168                 newtag->highaddr = max(parent->highaddr, newtag->highaddr);
169                 if (newtag->boundary == 0)
170                         newtag->boundary = parent->boundary;
171                 else if (parent->boundary != 0)
172                         newtag->boundary = MIN(parent->boundary,
173                                                newtag->boundary);
174                 if (newtag->filter == NULL) {
175                         /*
176                          * Short circuit looking at our parent directly
177                          * since we have encapsulated all of its information
178                          */
179                         newtag->filter = parent->filter;
180                         newtag->filterarg = parent->filterarg;
181                         newtag->parent = parent->parent;
182                 }
183                 if (newtag->parent != NULL)
184                         atomic_add_int(&parent->ref_count, 1);
185         }
186
187         *dmat = newtag;
188         return (error);
189 }
190
191 int
192 bus_dma_tag_destroy(bus_dma_tag_t dmat)
193 {
194         if (dmat != NULL) {
195                 
196                 if (dmat->map_count != 0)
197                         return (EBUSY);
198                 
199                 while (dmat != NULL) {
200                         bus_dma_tag_t parent;
201                         
202                         parent = dmat->parent;
203                         atomic_subtract_int(&dmat->ref_count, 1);
204                         if (dmat->ref_count == 0) {
205                                 free(dmat, M_DEVBUF);
206                                 /*
207                                  * Last reference count, so
208                                  * release our reference
209                                  * count on our parent.
210                                  */
211                                 dmat = parent;
212                         } else
213                                 dmat = NULL;
214                 }
215         }
216         return (0);
217 }
218
219 /*
220  * Allocate a handle for mapping from kva/uva/physical
221  * address space into bus device space.
222  */
223 int
224 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
225 {
226         *mapp = NULL;
227         dmat->map_count++;
228
229         return (0);
230 }
231
232 /*
233  * Destroy a handle for mapping from kva/uva/physical
234  * address space into bus device space.
235  */
236 int
237 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
238 {
239         if (map != NULL) {
240                 panic("dmamap_destroy: NULL?\n");
241         }
242         dmat->map_count--;
243         return (0);
244 }
245
246 /*
247  * Allocate a piece of memory that can be efficiently mapped into
248  * bus device space based on the constraints lited in the dma tag.
249  * A dmamap to for use with dmamap_load is also allocated.
250  */
251 int
252 bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
253     bus_dmamap_t *mapp)
254 {
255         int mflags;
256
257         if (flags & BUS_DMA_NOWAIT)
258                 mflags = M_NOWAIT;
259         else
260                 mflags = M_WAITOK;
261         if (flags & BUS_DMA_ZERO)
262                 mflags |= M_ZERO;
263
264         *mapp = NULL;
265
266         /* 
267          * XXX:
268          * (dmat->alignment < dmat->maxsize) is just a quick hack; the exact
269          * alignment guarantees of malloc need to be nailed down, and the
270          * code below should be rewritten to take that into account.
271          *
272          * In the meantime, we'll return an error if malloc gets it wrong.
273          */
274         if (dmat->maxsize <= PAGE_SIZE &&
275             dmat->alignment < dmat->maxsize) {
276                 *vaddr = malloc(dmat->maxsize, M_DEVBUF, mflags);
277         } else {
278                 /*
279                  * XXX Use Contigmalloc until it is merged into this facility
280                  *     and handles multi-seg allocations.  Nobody is doing
281                  *     multi-seg allocations yet though.
282                  */
283                 *vaddr = contigmalloc(dmat->maxsize, M_DEVBUF, mflags,
284                     0ul, dmat->lowaddr, dmat->alignment? dmat->alignment : 1ul,
285                     dmat->boundary);
286         }
287
288         if (*vaddr == NULL)
289                 return (ENOMEM);
290
291         if ((uintptr_t)*vaddr % dmat->alignment)
292                 printf("XXX: %s: alignment not respected!\n", __func__);
293
294         return (0);
295 }
296
297 /*
298  * Free a piece of memory and it's allocated dmamap, that was allocated
299  * via bus_dmamem_alloc.  Make the same choice for free/contigfree.
300  */
301 void
302 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
303 {
304         if (map != NULL)
305                 panic("bus_dmamem_free: Invalid map freed\n");
306         if (dmat->maxsize <= PAGE_SIZE &&
307             dmat->alignment < dmat->maxsize)
308                 free(vaddr, M_DEVBUF);
309         else
310                 contigfree(vaddr, dmat->maxsize, M_DEVBUF);
311 }
312
313 /*
314  * Utility function to load a linear buffer.  lastaddrp holds state
315  * between invocations (for multiple-buffer loads).  segp contains
316  * the starting segment on entrance, and the ending segment on exit.
317  * first indicates if this is the first invocation of this function.
318  */
319 static int
320 bus_dmamap_load_buffer(bus_dma_tag_t dmat, bus_dma_segment_t segs[],
321     void *buf, bus_size_t buflen, struct thread *td, int flags,
322     vm_offset_t *lastaddrp, int *segp, int first)
323 {
324         bus_size_t sgsize;
325         bus_addr_t curaddr, lastaddr, baddr, bmask;
326         vm_offset_t vaddr = (vm_offset_t)buf;
327         int seg;
328         pmap_t pmap;
329
330         if (td != NULL)
331                 pmap = vmspace_pmap(td->td_proc->p_vmspace);
332         else
333                 pmap = NULL;
334
335         lastaddr = *lastaddrp;
336         bmask = ~(dmat->boundary - 1);
337
338         for (seg = *segp; buflen > 0 ; ) {
339                 /*
340                  * Get the physical address for this segment.
341                  */
342                 if (pmap)
343                         curaddr = pmap_extract(pmap, vaddr);
344                 else
345                         curaddr = pmap_kextract(vaddr);
346
347                 /*
348                  * Compute the segment size, and adjust counts.
349                  */
350                 sgsize = PAGE_SIZE - ((u_long)curaddr & PAGE_MASK);
351                 if (sgsize > dmat->maxsegsz)
352                         sgsize = dmat->maxsegsz;
353                 if (buflen < sgsize)
354                         sgsize = buflen;
355
356                 /*
357                  * Make sure we don't cross any boundaries.
358                  */
359                 if (dmat->boundary > 0) {
360                         baddr = (curaddr + dmat->boundary) & bmask;
361                         if (sgsize > (baddr - curaddr))
362                                 sgsize = (baddr - curaddr);
363                 }
364
365                 /*
366                  * Insert chunk into a segment, coalescing with
367                  * the previous segment if possible.
368                  */
369                 if (first) {
370                         segs[seg].ds_addr = curaddr;
371                         segs[seg].ds_len = sgsize;
372                         first = 0;
373                 } else {
374                         if (curaddr == lastaddr &&
375                             (segs[seg].ds_len + sgsize) <= dmat->maxsegsz &&
376                             (dmat->boundary == 0 ||
377                              (segs[seg].ds_addr & bmask) == (curaddr & bmask)))
378                                 segs[seg].ds_len += sgsize;
379                         else {
380                                 if (++seg >= dmat->nsegments)
381                                         break;
382                                 segs[seg].ds_addr = curaddr;
383                                 segs[seg].ds_len = sgsize;
384                         }
385                 }
386
387                 lastaddr = curaddr + sgsize;
388                 vaddr += sgsize;
389                 buflen -= sgsize;
390         }
391
392         *segp = seg;
393         *lastaddrp = lastaddr;
394
395         /*
396          * Did we fit?
397          */
398         return (buflen != 0 ? EFBIG : 0); /* XXX better return value here? */
399 }
400
401 /*
402  * Map the buffer buf into bus space using the dmamap map.
403  */
404 int
405 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
406     bus_size_t buflen, bus_dmamap_callback_t *callback,
407     void *callback_arg, int flags)
408 {
409 #ifdef __CC_SUPPORTS_DYNAMIC_ARRAY_INIT
410         bus_dma_segment_t       dm_segments[dmat->nsegments];
411 #else
412         bus_dma_segment_t       dm_segments[BUS_DMAMAP_NSEGS];
413 #endif
414         vm_offset_t             lastaddr;
415         int                     error, nsegs;
416
417         if (map != NULL)
418                 panic("bus_dmamap_load: Invalid map\n");
419
420         lastaddr = (vm_offset_t)0;
421         nsegs = 0;      
422         error = bus_dmamap_load_buffer(dmat, dm_segments, buf, buflen,
423             NULL, flags, &lastaddr, &nsegs, 1);
424
425         if (error == 0)
426                 (*callback)(callback_arg, dm_segments, nsegs + 1, 0);
427         else
428                 (*callback)(callback_arg, NULL, 0, error);
429
430         return (0);
431 }
432
433 /*
434  * Like bus_dmamap_load(), but for mbufs.
435  */
436 int
437 bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map, struct mbuf *m0,
438     bus_dmamap_callback2_t *callback, void *callback_arg, int flags)
439 {
440 #ifdef __CC_SUPPORTS_DYNAMIC_ARRAY_INIT
441         bus_dma_segment_t dm_segments[dmat->nsegments];
442 #else
443         bus_dma_segment_t dm_segments[BUS_DMAMAP_NSEGS];
444 #endif
445         int nsegs = 0, error = 0;
446
447         M_ASSERTPKTHDR(m0);
448
449         if (m0->m_pkthdr.len <= dmat->maxsize) {
450                 int first = 1;
451                 vm_offset_t lastaddr = 0;
452                 struct mbuf *m;
453
454                 for (m = m0; m != NULL && error == 0; m = m->m_next) {
455                         if (m->m_len > 0) {
456                                 error = bus_dmamap_load_buffer(dmat,
457                                     dm_segments, m->m_data, m->m_len, NULL,
458                                     flags, &lastaddr, &nsegs, first);
459                                 first = 0;
460                         }
461                 }
462         } else {
463                 error = EINVAL;
464         }
465
466         if (error) {
467                 /* 
468                  * force "no valid mappings" on error in callback.
469                  */
470                 (*callback)(callback_arg, dm_segments, 0, 0, error);
471         } else {
472                 (*callback)(callback_arg, dm_segments, nsegs+1,
473                     m0->m_pkthdr.len, error);
474         }
475         return (error);
476 }
477
478 int
479 bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map, struct mbuf *m0,
480     bus_dma_segment_t *segs, int *nsegs, int flags)
481 {
482         int error = 0;
483
484         M_ASSERTPKTHDR(m0);
485
486         *nsegs = 0;
487
488         if (m0->m_pkthdr.len <= dmat->maxsize) {
489                 int first = 1;
490                 vm_offset_t lastaddr = 0;
491                 struct mbuf *m;
492
493                 for (m = m0; m != NULL && error == 0; m = m->m_next) {
494                         if (m->m_len > 0) {
495                                 error = bus_dmamap_load_buffer(dmat,
496                                     segs, m->m_data, m->m_len, NULL,
497                                     flags, &lastaddr, nsegs, first);
498                                 first = 0;
499                         }
500                 }
501                 ++*nsegs;
502         } else {
503                 error = EINVAL;
504         }
505
506         return (error);
507 }
508
509 /*
510  * Like bus_dmamap_load(), but for uios.
511  */
512 int
513 bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map, struct uio *uio,
514     bus_dmamap_callback2_t *callback, void *callback_arg, int flags)
515 {
516         vm_offset_t lastaddr;
517 #ifdef __CC_SUPPORTS_DYNAMIC_ARRAY_INIT
518         bus_dma_segment_t dm_segments[dmat->nsegments];
519 #else
520         bus_dma_segment_t dm_segments[BUS_DMAMAP_NSEGS];
521 #endif
522         int nsegs, i, error, first;
523         bus_size_t resid;
524         struct iovec *iov;
525         struct thread *td = NULL;
526
527         resid = uio->uio_resid;
528         iov = uio->uio_iov;
529
530         if (uio->uio_segflg == UIO_USERSPACE) {
531                 td = uio->uio_td;
532                 KASSERT(td != NULL,
533                     ("bus_dmamap_load_uio: USERSPACE but no proc"));
534         }
535
536         first = 1;
537         nsegs = error = 0;
538         for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) {
539                 /*
540                  * Now at the first iovec to load.  Load each iovec
541                  * until we have exhausted the residual count.
542                  */
543                 bus_size_t minlen =
544                     resid < iov[i].iov_len ? resid : iov[i].iov_len;
545                 caddr_t addr = (caddr_t) iov[i].iov_base;
546
547                 if (minlen > 0) {
548                         error = bus_dmamap_load_buffer(dmat, dm_segments, addr,
549                             minlen, td, flags, &lastaddr, &nsegs, first);
550
551                         first = 0;
552
553                         resid -= minlen;
554                 }
555         }
556
557         if (error) {
558                 /* 
559                  * force "no valid mappings" on error in callback.
560                  */
561                 (*callback)(callback_arg, dm_segments, 0, 0, error);
562         } else {
563                 (*callback)(callback_arg, dm_segments, nsegs+1,
564                     uio->uio_resid, error);
565         }
566
567         return (error);
568 }
569
570 /*
571  * Release the mapping held by map. A no-op on PowerPC.
572  */
573 void
574 _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
575 {
576
577         return;
578 }
579
580 void
581 _bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
582 {
583
584         return;
585 }