]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/usb/usb_mem.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / dev / usb / usb_mem.c
1 /*      $NetBSD: usb_mem.c,v 1.26 2003/02/01 06:23:40 thorpej Exp $     */
2 /*      $FreeBSD$       */
3
4 /*-
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (lennart@augustsson.net) at
10  * Carlstedt Research & Technology.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *        This product includes software developed by the NetBSD
23  *        Foundation, Inc. and its contributors.
24  * 4. Neither the name of The NetBSD Foundation nor the names of its
25  *    contributors may be used to endorse or promote products derived
26  *    from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40
41 /*
42  * USB DMA memory allocation.
43  * We need to allocate a lot of small (many 8 byte, some larger)
44  * memory blocks that can be used for DMA.  Using the bus_dma
45  * routines directly would incur large overheads in space and time.
46  */
47
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/malloc.h>
54 #include <sys/kernel.h>
55 #include <sys/endian.h>
56 #include <sys/module.h>
57 #include <sys/bus.h>
58 #include <sys/queue.h>
59
60 #include <machine/bus.h>
61 #include <machine/endian.h>
62
63 #ifdef DIAGNOSTIC
64 #include <sys/proc.h>
65 #endif
66
67 #include <dev/usb/usb.h>
68 #include <dev/usb/usbdi.h>
69 #include <dev/usb/usbdivar.h>   /* just for usb_dma_t */
70 #include <dev/usb/usb_mem.h>
71
72 #ifdef USB_DEBUG
73 #define DPRINTF(x)      if (usbdebug) printf x
74 #define DPRINTFN(n,x)   if (usbdebug>(n)) printf x
75 extern int usbdebug;
76 #else
77 #define DPRINTF(x)
78 #define DPRINTFN(n,x)
79 #endif
80
81 #define USB_MEM_SMALL 64
82 #define USB_MEM_CHUNKS (PAGE_SIZE / USB_MEM_SMALL)
83 #define USB_MEM_BLOCK (USB_MEM_SMALL * USB_MEM_CHUNKS)
84
85 /* This struct is overlayed on free fragments. */
86 struct usb_frag_dma {
87         usb_dma_block_t *block;
88         u_int offs;
89         LIST_ENTRY(usb_frag_dma) next;
90 };
91
92 static bus_dmamap_callback_t usbmem_callback;
93 static usbd_status      usb_block_allocmem(bus_dma_tag_t, size_t, size_t,
94                                            usb_dma_block_t **);
95 static void             usb_block_freemem(usb_dma_block_t *);
96
97 static LIST_HEAD(, usb_dma_block) usb_blk_freelist =
98         LIST_HEAD_INITIALIZER(usb_blk_freelist);
99 static int usb_blk_nfree = 0;
100 /* XXX should have different free list for different tags (for speed) */
101 static LIST_HEAD(, usb_frag_dma) usb_frag_freelist =
102         LIST_HEAD_INITIALIZER(usb_frag_freelist);
103
104 static void
105 usbmem_callback(void *arg, bus_dma_segment_t *segs, int nseg, int error)
106 {
107         int i;
108         usb_dma_block_t *p = arg;
109
110         if (error == EFBIG) {
111                 printf("usb: mapping to large\n");
112                 return;
113         }
114
115         p->nsegs = nseg;
116         for (i = 0; i < nseg && i < sizeof p->segs / sizeof *p->segs; i++)
117                 p->segs[i] = segs[i];
118 }
119
120 static usbd_status
121 usb_block_allocmem(bus_dma_tag_t tag, size_t size, size_t align,
122                    usb_dma_block_t **dmap)
123 {
124         usb_dma_block_t *p;
125         int s;
126
127         DPRINTFN(5, ("usb_block_allocmem: size=%lu align=%lu\n",
128                      (u_long)size, (u_long)align));
129
130 #ifdef DIAGNOSTIC
131         if (!curproc) {
132                 printf("usb_block_allocmem: in interrupt context, size=%lu\n",
133                     (unsigned long) size);
134         }
135 #endif
136
137         s = splusb();
138         /* First check the free list. */
139         for (p = LIST_FIRST(&usb_blk_freelist); p; p = LIST_NEXT(p, next)) {
140                 if (p->tag == tag && p->size >= size && p->size < size * 2 &&
141                     p->align >= align) {
142                         LIST_REMOVE(p, next);
143                         usb_blk_nfree--;
144                         splx(s);
145                         *dmap = p;
146                         DPRINTFN(6,("usb_block_allocmem: free list size=%lu\n",
147                                     (u_long)p->size));
148                         return (USBD_NORMAL_COMPLETION);
149                 }
150         }
151         splx(s);
152
153 #ifdef DIAGNOSTIC
154         if (!curproc) {
155                 printf("usb_block_allocmem: in interrupt context, failed\n");
156                 return (USBD_NOMEM);
157         }
158 #endif
159
160         DPRINTFN(6, ("usb_block_allocmem: no free\n"));
161         p = malloc(sizeof *p, M_USB, M_NOWAIT);
162         if (p == NULL)
163                 return (USBD_NOMEM);
164
165         if (bus_dma_tag_create(tag, align, 0,
166             BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
167             size, sizeof(p->segs) / sizeof(p->segs[0]), size,
168             0, NULL, NULL, &p->tag) == ENOMEM)
169         {
170                 goto free;
171         }
172
173         p->size = size;
174         p->align = align;
175         if (bus_dmamem_alloc(p->tag, &p->kaddr,
176             BUS_DMA_NOWAIT|BUS_DMA_COHERENT, &p->map))
177                 goto tagfree;
178
179         if (bus_dmamap_load(p->tag, p->map, p->kaddr, p->size,
180             usbmem_callback, p, 0))
181                 goto memfree;
182
183         /* XXX - override the tag, ok since we never free it */
184         p->tag = tag;
185         *dmap = p;
186         return (USBD_NORMAL_COMPLETION);
187
188         /*
189          * XXX - do we need to _unload? is the order of _free and _destroy
190          * correct?
191          */
192 memfree:
193         bus_dmamem_free(p->tag, p->kaddr, p->map);
194 tagfree:
195         bus_dma_tag_destroy(p->tag);
196 free:
197         free(p, M_USB);
198         return (USBD_NOMEM);
199 }
200
201 /*
202  * Do not free the memory unconditionally since we might be called
203  * from an interrupt context and that is BAD.
204  * XXX when should we really free?
205  */
206 static void
207 usb_block_freemem(usb_dma_block_t *p)
208 {
209         int s;
210
211         DPRINTFN(6, ("usb_block_freemem: size=%lu\n", (u_long)p->size));
212         s = splusb();
213         LIST_INSERT_HEAD(&usb_blk_freelist, p, next);
214         usb_blk_nfree++;
215         splx(s);
216 }
217
218 usbd_status
219 usb_allocmem(usbd_bus_handle bus, size_t size, size_t align, usb_dma_t *p)
220 {
221         bus_dma_tag_t tag = bus->parent_dmatag;
222         usbd_status err;
223         struct usb_frag_dma *f;
224         usb_dma_block_t *b;
225         int i;
226         int s;
227
228         /* compat w/ Net/OpenBSD */
229         if (align == 0)
230                 align = 1;
231
232         /* If the request is large then just use a full block. */
233         if (size > USB_MEM_SMALL || align > USB_MEM_SMALL) {
234                 DPRINTFN(1, ("usb_allocmem: large alloc %d\n", (int)size));
235                 size = (size + USB_MEM_BLOCK - 1) & ~(USB_MEM_BLOCK - 1);
236                 err = usb_block_allocmem(tag, size, align, &p->block);
237                 if (!err) {
238                         p->block->fullblock = 1;
239                         p->offs = 0;
240                         p->len = size;
241                 }
242                 return (err);
243         }
244
245         s = splusb();
246         /* Check for free fragments. */
247         for (f = LIST_FIRST(&usb_frag_freelist); f; f = LIST_NEXT(f, next))
248                 if (f->block->tag == tag)
249                         break;
250         if (f == NULL) {
251                 DPRINTFN(1, ("usb_allocmem: adding fragments\n"));
252                 err = usb_block_allocmem(tag, USB_MEM_BLOCK, USB_MEM_SMALL,&b);
253                 if (err) {
254                         splx(s);
255                         return (err);
256                 }
257                 b->fullblock = 0;
258                 /* XXX - override the tag, ok since we never free it */
259                 b->tag = tag;
260                 KASSERT(sizeof *f <= USB_MEM_SMALL, ("USB_MEM_SMALL(%d) is too small for struct usb_frag_dma(%zd)\n",
261                     USB_MEM_SMALL, sizeof *f));
262                 for (i = 0; i < USB_MEM_BLOCK; i += USB_MEM_SMALL) {
263                         f = (struct usb_frag_dma *)((char *)b->kaddr + i);
264                         f->block = b;
265                         f->offs = i;
266                         LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
267                 }
268                 f = LIST_FIRST(&usb_frag_freelist);
269         }
270         p->block = f->block;
271         p->offs = f->offs;
272         p->len = USB_MEM_SMALL;
273         LIST_REMOVE(f, next);
274         splx(s);
275         DPRINTFN(5, ("usb_allocmem: use frag=%p size=%d\n", f, (int)size));
276         return (USBD_NORMAL_COMPLETION);
277 }
278
279 void
280 usb_freemem(usbd_bus_handle bus, usb_dma_t *p)
281 {
282         struct usb_frag_dma *f;
283         int s;
284
285         if (p->block->fullblock) {
286                 DPRINTFN(1, ("usb_freemem: large free\n"));
287                 usb_block_freemem(p->block);
288                 return;
289         }
290         f = KERNADDR(p, 0);
291         f->block = p->block;
292         f->offs = p->offs;
293         s = splusb();
294         LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
295         splx(s);
296         DPRINTFN(5, ("usb_freemem: frag=%p\n", f));
297 }