usb_mem.c revision 1.13 1 /* $NetBSD: usb_mem.c,v 1.13 1999/09/11 08:19:27 augustss 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 (augustss (at) carlstedt.se) 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/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/queue.h>
52 #include <sys/device.h> /* for usbdivar.h */
53
54 #ifdef DIAGNOSTIC
55 #include <sys/proc.h>
56 #endif
57
58 #include <machine/bus.h>
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) if (usbdebug) logprintf x
67 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
68 int usbdebug;
69 #else
70 #define DPRINTF(x)
71 #define DPRINTFN(n,x)
72 #endif
73
74 #define USB_MEM_SMALL 64
75 #define USB_MEM_CHUNKS 64
76 #define USB_MEM_BLOCK (USB_MEM_SMALL * USB_MEM_CHUNKS)
77
78 /* This struct is overlayed on free fragments. */
79 struct usb_frag_dma {
80 usb_dma_block_t *block;
81 u_int offs;
82 LIST_ENTRY(usb_frag_dma) next;
83 };
84
85 usbd_status usb_block_allocmem __P((bus_dma_tag_t, size_t, size_t,
86 usb_dma_block_t **));
87 void usb_block_freemem __P((usb_dma_block_t *));
88
89 LIST_HEAD(, usb_dma_block) usb_blk_freelist =
90 LIST_HEAD_INITIALIZER(usb_blk_freelist);
91 /* XXX should have different free list for different tags (for speed) */
92 LIST_HEAD(, usb_frag_dma) usb_frag_freelist =
93 LIST_HEAD_INITIALIZER(usb_frag_freelist);
94
95 usbd_status
96 usb_block_allocmem(tag, size, align, dmap)
97 bus_dma_tag_t tag;
98 size_t size;
99 size_t align;
100 usb_dma_block_t **dmap;
101 {
102 int error;
103 usb_dma_block_t *p;
104 int s;
105
106 DPRINTFN(5, ("usb_block_allocmem: size=%d align=%d\n", size, align));
107
108 #ifdef DIAGNOSTIC
109 if (!curproc) {
110 printf("usb_block_allocmem: in interrupt context, size=%lu\n",
111 (unsigned long) size);
112 }
113 #endif
114
115 s = splusb();
116 /* First check the free list. */
117 for (p = LIST_FIRST(&usb_blk_freelist); p; p = LIST_NEXT(p, next)) {
118 if (p->tag == tag && p->size >= size && p->align >= align) {
119 LIST_REMOVE(p, next);
120 splx(s);
121 *dmap = p;
122 DPRINTFN(6,("usb_block_allocmem: free list size=%d\n",
123 p->size));
124 return (USBD_NORMAL_COMPLETION);
125 }
126 }
127 splx(s);
128
129 #ifdef DIAGNOSTIC
130 if (!curproc) {
131 printf("usb_block_allocmem: in interrupt context, failed\n");
132 return (USBD_NOMEM);
133 }
134 #endif
135
136 DPRINTFN(6, ("usb_block_allocmem: no free\n"));
137 p = malloc(sizeof *p, M_USB, M_NOWAIT);
138 if (p == 0)
139 return (USBD_NOMEM);
140 *dmap = p;
141
142 p->tag = tag;
143 p->size = size;
144 p->align = align;
145 error = bus_dmamem_alloc(tag, p->size, align, 0,
146 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
147 &p->nsegs, BUS_DMA_NOWAIT);
148 if (error)
149 return (USBD_NOMEM);
150
151 error = bus_dmamem_map(tag, p->segs, p->nsegs, p->size,
152 &p->kaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
153 if (error)
154 goto free;
155
156 error = bus_dmamap_create(tag, p->size, 1, p->size,
157 0, BUS_DMA_NOWAIT, &p->map);
158 if (error)
159 goto unmap;
160
161 error = bus_dmamap_load(tag, p->map, p->kaddr, p->size, NULL,
162 BUS_DMA_NOWAIT);
163 if (error)
164 goto destroy;
165 return 0;
166
167 destroy:
168 bus_dmamap_destroy(tag, p->map);
169 unmap:
170 bus_dmamem_unmap(tag, p->kaddr, p->size);
171 free:
172 bus_dmamem_free(tag, p->segs, p->nsegs);
173 return (USBD_NOMEM);
174 }
175
176 #if 0
177 void
178 usb_block_real_freemem(p)
179 usb_dma_block_t *p;
180 {
181 #ifdef DIAGNOSTIC
182 if (!curproc) {
183 printf("usb_block_real_freemem: in interrupt context\n");
184 return;
185 }
186 #endif
187 bus_dmamap_unload(p->tag, p->map);
188 bus_dmamap_destroy(p->tag, p->map);
189 bus_dmamem_unmap(p->tag, p->kaddr, p->size);
190 bus_dmamem_free(p->tag, p->segs, p->nsegs);
191 free(p, M_USB);
192 }
193 #endif
194
195 /*
196 * Do not free the memory unconditionally since we might be called
197 * from an interrupt context and that is BAD.
198 * XXX when should we really free?
199 */
200 void
201 usb_block_freemem(p)
202 usb_dma_block_t *p;
203 {
204 int s;
205
206 DPRINTFN(6, ("usb_block_freemem: size=%d\n", p->size));
207 s = splusb();
208 LIST_INSERT_HEAD(&usb_blk_freelist, p, next);
209 splx(s);
210 }
211
212 usbd_status
213 usb_allocmem(tag, size, align, p)
214 bus_dma_tag_t tag;
215 size_t size;
216 size_t align;
217 usb_dma_t *p;
218 {
219 usbd_status r;
220 struct usb_frag_dma *f;
221 usb_dma_block_t *b;
222 int i;
223 int s;
224
225 /* If the request is large then just use a full block. */
226 if (size > USB_MEM_SMALL || align > USB_MEM_SMALL) {
227 DPRINTFN(1, ("usb_allocmem: large alloc %d\n", (int)size));
228 size = (size + USB_MEM_BLOCK - 1) & ~(USB_MEM_BLOCK - 1);
229 r = usb_block_allocmem(tag, size, align, &p->block);
230 if (r == USBD_NORMAL_COMPLETION) {
231 p->block->fullblock = 1;
232 p->offs = 0;
233 }
234 return (r);
235 }
236
237 s = splusb();
238 /* Check for free fragments. */
239 for (f = LIST_FIRST(&usb_frag_freelist); f; f = LIST_NEXT(f, next))
240 if (f->block->tag == tag)
241 break;
242 if (!f) {
243 DPRINTFN(1, ("usb_allocmem: adding fragments\n"));
244 r = usb_block_allocmem(tag, USB_MEM_BLOCK, USB_MEM_SMALL, &b);
245 if (r != USBD_NORMAL_COMPLETION) {
246 splx(s);
247 return (r);
248 }
249 b->fullblock = 0;
250 for (i = 0; i < USB_MEM_BLOCK; i += USB_MEM_SMALL) {
251 f = (struct usb_frag_dma *)(b->kaddr + i);
252 f->block = b;
253 f->offs = i;
254 LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
255 }
256 f = LIST_FIRST(&usb_frag_freelist);
257 }
258 p->block = f->block;
259 p->offs = f->offs;
260 LIST_REMOVE(f, next);
261 splx(s);
262 DPRINTFN(5, ("usb_allocmem: use frag=%p size=%d\n", f, (int)size));
263 return (USBD_NORMAL_COMPLETION);
264 }
265
266 void
267 usb_freemem(tag, p)
268 bus_dma_tag_t tag;
269 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);
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