1 /* $OpenBSD: usb_mem.c,v 1.16 2004/07/08 22:18:44 deraadt Exp $ */
2 /* $NetBSD: usb_mem.c,v 1.26 2003/02/01 06:23:40 thorpej Exp $ */
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/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/queue.h>
53 #include <sys/device.h> /* for usbdivar.h */
54 #include <machine/bus.h>
55
56 #ifdef DIAGNOSTIC
57 #include <sys/proc.h>
58 #endif
59
60 #include <dev/usb/usb.h>
61 #include <dev/usb/usbdi.h>
62 #include <dev/usb/usbdivar.h> /* just for usb_dma_t */
63 #include <dev/usb/usb_mem.h>
64
65 #ifdef USB_DEBUG
66 #define DPRINTF(x) do { if (usbdebug) logprintf x; } while (0)
67 #define DPRINTFN(n,x) do { if (usbdebug>(n)) logprintf x; } while (0)
68 extern int usbdebug;
69 #else
70 #define DPRINTF(x)
71 #define DPRINTFN(n,x)
72 #endif
73
74 #if defined(__NetBSD__)
75 MALLOC_DEFINE(M_USB, "USB", "USB misc. memory");
76 MALLOC_DEFINE(M_USBDEV, "USB device", "USB device driver");
77 MALLOC_DEFINE(M_USBHC, "USB HC", "USB host controller");
78 #endif
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
usb_block_allocmem(bus_dma_tag_t tag,size_t size,size_t align,usb_dma_block_t ** dmap)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
147 p->tag = tag;
148 p->size = size;
149 p->align = align;
150 error = bus_dmamem_alloc(tag, p->size, align, 0,
151 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
152 &p->nsegs, BUS_DMA_NOWAIT);
153 if (error)
154 goto free0;
155
156 error = bus_dmamem_map(tag, p->segs, p->nsegs, p->size,
157 &p->kaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
158 if (error)
159 goto free1;
160
161 error = bus_dmamap_create(tag, p->size, 1, p->size,
162 0, BUS_DMA_NOWAIT, &p->map);
163 if (error)
164 goto unmap;
165
166 error = bus_dmamap_load(tag, p->map, p->kaddr, p->size, NULL,
167 BUS_DMA_NOWAIT);
168 if (error)
169 goto destroy;
170
171 *dmap = p;
172 return (USBD_NORMAL_COMPLETION);
173
174 destroy:
175 bus_dmamap_destroy(tag, p->map);
176 unmap:
177 bus_dmamem_unmap(tag, p->kaddr, p->size);
178 free1:
179 bus_dmamem_free(tag, p->segs, p->nsegs);
180 free0:
181 free(p, M_USB);
182 return (USBD_NOMEM);
183 }
184
185 #if 0
186 void
187 usb_block_real_freemem(usb_dma_block_t *p)
188 {
189 #ifdef DIAGNOSTIC
190 if (!curproc) {
191 printf("usb_block_real_freemem: in interrupt context\n");
192 return;
193 }
194 #endif
195 bus_dmamap_unload(p->tag, p->map);
196 bus_dmamap_destroy(p->tag, p->map);
197 bus_dmamem_unmap(p->tag, p->kaddr, p->size);
198 bus_dmamem_free(p->tag, p->segs, p->nsegs);
199 free(p, M_USB);
200 }
201 #endif
202
203 /*
204 * Do not free the memory unconditionally since we might be called
205 * from an interrupt context and that is BAD.
206 * XXX when should we really free?
207 */
208 Static void
usb_block_freemem(usb_dma_block_t * p)209 usb_block_freemem(usb_dma_block_t *p)
210 {
211 int s;
212
213 DPRINTFN(6, ("usb_block_freemem: size=%lu\n", (u_long)p->size));
214 s = splusb();
215 LIST_INSERT_HEAD(&usb_blk_freelist, p, next);
216 usb_blk_nfree++;
217 splx(s);
218 }
219
220 usbd_status
usb_allocmem(usbd_bus_handle bus,size_t size,size_t align,usb_dma_t * p)221 usb_allocmem(usbd_bus_handle bus, size_t size, size_t align, usb_dma_t *p)
222 {
223 bus_dma_tag_t tag = bus->dmatag;
224 usbd_status err;
225 struct usb_frag_dma *f;
226 usb_dma_block_t *b;
227 int i;
228 int s;
229
230 /* If the request is large then just use a full block. */
231 if (size > USB_MEM_SMALL || align > USB_MEM_SMALL) {
232 DPRINTFN(1, ("usb_allocmem: large alloc %d\n", (int)size));
233 size = (size + USB_MEM_BLOCK - 1) & ~(USB_MEM_BLOCK - 1);
234 err = usb_block_allocmem(tag, size, align, &p->block);
235 if (!err) {
236 p->block->fullblock = 1;
237 p->offs = 0;
238 }
239 return (err);
240 }
241
242 s = splusb();
243 /* Check for free fragments. */
244 for (f = LIST_FIRST(&usb_frag_freelist); f; f = LIST_NEXT(f, next))
245 if (f->block->tag == tag)
246 break;
247 if (f == NULL) {
248 DPRINTFN(1, ("usb_allocmem: adding fragments\n"));
249 err = usb_block_allocmem(tag, USB_MEM_BLOCK, USB_MEM_SMALL,&b);
250 if (err) {
251 splx(s);
252 return (err);
253 }
254 b->fullblock = 0;
255 for (i = 0; i < USB_MEM_BLOCK; i += USB_MEM_SMALL) {
256 f = (struct usb_frag_dma *)(b->kaddr + i);
257 f->block = b;
258 f->offs = i;
259 LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
260 }
261 f = LIST_FIRST(&usb_frag_freelist);
262 }
263 p->block = f->block;
264 p->offs = f->offs;
265 LIST_REMOVE(f, next);
266 splx(s);
267 DPRINTFN(5, ("usb_allocmem: use frag=%p size=%d\n", f, (int)size));
268 return (USBD_NORMAL_COMPLETION);
269 }
270
271 void
usb_freemem(usbd_bus_handle bus,usb_dma_t * p)272 usb_freemem(usbd_bus_handle bus, usb_dma_t *p)
273 {
274 struct usb_frag_dma *f;
275 int s;
276
277 if (p->block->fullblock) {
278 DPRINTFN(1, ("usb_freemem: large free\n"));
279 usb_block_freemem(p->block);
280 return;
281 }
282 f = KERNADDR(p, 0);
283 f->block = p->block;
284 f->offs = p->offs;
285 s = splusb();
286 LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
287 splx(s);
288 DPRINTFN(5, ("usb_freemem: frag=%p\n", f));
289 }
290