usb_mem.c revision 1.26 1 /* $NetBSD: usb_mem.c,v 1.26 2003/02/01 06:23:40 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart (at) augustsson.net) at
9 * Carlstedt Research & Technology.
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 /*
41 * USB DMA memory allocation.
42 * We need to allocate a lot of small (many 8 byte, some larger)
43 * memory blocks that can be used for DMA. Using the bus_dma
44 * routines directly would incur large overheads in space and time.
45 */
46
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: usb_mem.c,v 1.26 2003/02/01 06:23:40 thorpej Exp $");
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/malloc.h>
54 #include <sys/queue.h>
55 #include <sys/device.h> /* for usbdivar.h */
56 #include <machine/bus.h>
57
58 #ifdef DIAGNOSTIC
59 #include <sys/proc.h>
60 #endif
61
62 #include <dev/usb/usb.h>
63 #include <dev/usb/usbdi.h>
64 #include <dev/usb/usbdivar.h> /* just for usb_dma_t */
65 #include <dev/usb/usb_mem.h>
66
67 #ifdef USB_DEBUG
68 #define DPRINTF(x) if (usbdebug) logprintf x
69 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
70 extern int usbdebug;
71 #else
72 #define DPRINTF(x)
73 #define DPRINTFN(n,x)
74 #endif
75
76 MALLOC_DEFINE(M_USB, "USB", "USB misc. memory");
77 MALLOC_DEFINE(M_USBDEV, "USB device", "USB device driver");
78 MALLOC_DEFINE(M_USBHC, "USB HC", "USB host controller");
79
80 #define USB_MEM_SMALL 64
81 #define USB_MEM_CHUNKS 64
82 #define USB_MEM_BLOCK (USB_MEM_SMALL * USB_MEM_CHUNKS)
83
84 /* This struct is overlayed on free fragments. */
85 struct usb_frag_dma {
86 usb_dma_block_t *block;
87 u_int offs;
88 LIST_ENTRY(usb_frag_dma) next;
89 };
90
91 Static usbd_status usb_block_allocmem(bus_dma_tag_t, size_t, size_t,
92 usb_dma_block_t **);
93 Static void usb_block_freemem(usb_dma_block_t *);
94
95 Static LIST_HEAD(, usb_dma_block) usb_blk_freelist =
96 LIST_HEAD_INITIALIZER(usb_blk_freelist);
97 Static int usb_blk_nfree = 0;
98 /* XXX should have different free list for different tags (for speed) */
99 Static LIST_HEAD(, usb_frag_dma) usb_frag_freelist =
100 LIST_HEAD_INITIALIZER(usb_frag_freelist);
101
102 Static usbd_status
103 usb_block_allocmem(bus_dma_tag_t tag, size_t size, size_t align,
104 usb_dma_block_t **dmap)
105 {
106 int error;
107 usb_dma_block_t *p;
108 int s;
109
110 DPRINTFN(5, ("usb_block_allocmem: size=%lu align=%lu\n",
111 (u_long)size, (u_long)align));
112
113 #ifdef DIAGNOSTIC
114 if (!curproc) {
115 printf("usb_block_allocmem: in interrupt context, size=%lu\n",
116 (unsigned long) size);
117 }
118 #endif
119
120 s = splusb();
121 /* First check the free list. */
122 for (p = LIST_FIRST(&usb_blk_freelist); p; p = LIST_NEXT(p, next)) {
123 if (p->tag == tag && p->size >= size && p->align >= align) {
124 LIST_REMOVE(p, next);
125 usb_blk_nfree--;
126 splx(s);
127 *dmap = p;
128 DPRINTFN(6,("usb_block_allocmem: free list size=%lu\n",
129 (u_long)p->size));
130 return (USBD_NORMAL_COMPLETION);
131 }
132 }
133 splx(s);
134
135 #ifdef DIAGNOSTIC
136 if (!curproc) {
137 printf("usb_block_allocmem: in interrupt context, failed\n");
138 return (USBD_NOMEM);
139 }
140 #endif
141
142 DPRINTFN(6, ("usb_block_allocmem: no free\n"));
143 p = malloc(sizeof *p, M_USB, M_NOWAIT);
144 if (p == NULL)
145 return (USBD_NOMEM);
146 *dmap = p;
147
148 p->tag = tag;
149 p->size = size;
150 p->align = align;
151 error = bus_dmamem_alloc(tag, p->size, align, 0,
152 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
153 &p->nsegs, BUS_DMA_NOWAIT);
154 if (error)
155 return (USBD_NOMEM);
156
157 error = bus_dmamem_map(tag, p->segs, p->nsegs, p->size,
158 &p->kaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
159 if (error)
160 goto free;
161
162 error = bus_dmamap_create(tag, p->size, 1, p->size,
163 0, BUS_DMA_NOWAIT, &p->map);
164 if (error)
165 goto unmap;
166
167 error = bus_dmamap_load(tag, p->map, p->kaddr, p->size, NULL,
168 BUS_DMA_NOWAIT);
169 if (error)
170 goto destroy;
171 return (USBD_NORMAL_COMPLETION);
172
173 destroy:
174 bus_dmamap_destroy(tag, p->map);
175 unmap:
176 bus_dmamem_unmap(tag, p->kaddr, p->size);
177 free:
178 bus_dmamem_free(tag, p->segs, p->nsegs);
179 return (USBD_NOMEM);
180 }
181
182 #if 0
183 void
184 usb_block_real_freemem(usb_dma_block_t *p)
185 {
186 #ifdef DIAGNOSTIC
187 if (!curproc) {
188 printf("usb_block_real_freemem: in interrupt context\n");
189 return;
190 }
191 #endif
192 bus_dmamap_unload(p->tag, p->map);
193 bus_dmamap_destroy(p->tag, p->map);
194 bus_dmamem_unmap(p->tag, p->kaddr, p->size);
195 bus_dmamem_free(p->tag, p->segs, p->nsegs);
196 free(p, M_USB);
197 }
198 #endif
199
200 /*
201 * Do not free the memory unconditionally since we might be called
202 * from an interrupt context and that is BAD.
203 * XXX when should we really free?
204 */
205 Static void
206 usb_block_freemem(usb_dma_block_t *p)
207 {
208 int s;
209
210 DPRINTFN(6, ("usb_block_freemem: size=%lu\n", (u_long)p->size));
211 s = splusb();
212 LIST_INSERT_HEAD(&usb_blk_freelist, p, next);
213 usb_blk_nfree++;
214 splx(s);
215 }
216
217 usbd_status
218 usb_allocmem(usbd_bus_handle bus, size_t size, size_t align, usb_dma_t *p)
219 {
220 bus_dma_tag_t tag = bus->dmatag;
221 usbd_status err;
222 struct usb_frag_dma *f;
223 usb_dma_block_t *b;
224 int i;
225 int s;
226
227 /* If the request is large then just use a full block. */
228 if (size > USB_MEM_SMALL || align > USB_MEM_SMALL) {
229 DPRINTFN(1, ("usb_allocmem: large alloc %d\n", (int)size));
230 size = (size + USB_MEM_BLOCK - 1) & ~(USB_MEM_BLOCK - 1);
231 err = usb_block_allocmem(tag, size, align, &p->block);
232 if (!err) {
233 p->block->fullblock = 1;
234 p->offs = 0;
235 }
236 return (err);
237 }
238
239 s = splusb();
240 /* Check for free fragments. */
241 for (f = LIST_FIRST(&usb_frag_freelist); f; f = LIST_NEXT(f, next))
242 if (f->block->tag == tag)
243 break;
244 if (f == NULL) {
245 DPRINTFN(1, ("usb_allocmem: adding fragments\n"));
246 err = usb_block_allocmem(tag, USB_MEM_BLOCK, USB_MEM_SMALL,&b);
247 if (err) {
248 splx(s);
249 return (err);
250 }
251 b->fullblock = 0;
252 for (i = 0; i < USB_MEM_BLOCK; i += USB_MEM_SMALL) {
253 f = (struct usb_frag_dma *)(b->kaddr + i);
254 f->block = b;
255 f->offs = i;
256 LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
257 }
258 f = LIST_FIRST(&usb_frag_freelist);
259 }
260 p->block = f->block;
261 p->offs = f->offs;
262 LIST_REMOVE(f, next);
263 splx(s);
264 DPRINTFN(5, ("usb_allocmem: use frag=%p size=%d\n", f, (int)size));
265 return (USBD_NORMAL_COMPLETION);
266 }
267
268 void
269 usb_freemem(usbd_bus_handle bus, usb_dma_t *p)
270 {
271 struct usb_frag_dma *f;
272 int s;
273
274 if (p->block->fullblock) {
275 DPRINTFN(1, ("usb_freemem: large free\n"));
276 usb_block_freemem(p->block);
277 return;
278 }
279 f = KERNADDR(p, 0);
280 f->block = p->block;
281 f->offs = p->offs;
282 s = splusb();
283 LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
284 splx(s);
285 DPRINTFN(5, ("usb_freemem: frag=%p\n", f));
286 }
287