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