FreeBSD/Linux Kernel Cross Reference
sys/dev/usb/usb_mem.c
1 /* $NetBSD: usb_mem.c,v 1.31.2.1 2008/08/31 16:48:29 jdc 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@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.31.2.1 2008/08/31 16:48:29 jdc 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 __NetBSD__
59 #include <sys/extent.h>
60 #include <uvm/uvm_extern.h>
61 #endif
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) logprintf x
74 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
75 extern int usbdebug;
76 #else
77 #define DPRINTF(x)
78 #define DPRINTFN(n,x)
79 #endif
80
81 MALLOC_DEFINE(M_USB, "USB", "USB misc. memory");
82 MALLOC_DEFINE(M_USBDEV, "USB device", "USB device driver");
83 MALLOC_DEFINE(M_USBHC, "USB HC", "USB host controller");
84
85 #define USB_MEM_SMALL 64
86 #define USB_MEM_CHUNKS 64
87 #define USB_MEM_BLOCK (USB_MEM_SMALL * USB_MEM_CHUNKS)
88
89 /* This struct is overlayed on free fragments. */
90 struct usb_frag_dma {
91 usb_dma_block_t *block;
92 u_int offs;
93 LIST_ENTRY(usb_frag_dma) next;
94 };
95
96 Static usbd_status usb_block_allocmem(bus_dma_tag_t, size_t, size_t,
97 usb_dma_block_t **);
98 Static void usb_block_freemem(usb_dma_block_t *);
99
100 Static LIST_HEAD(, usb_dma_block) usb_blk_freelist =
101 LIST_HEAD_INITIALIZER(usb_blk_freelist);
102 Static int usb_blk_nfree = 0;
103 /* XXX should have different free list for different tags (for speed) */
104 Static LIST_HEAD(, usb_frag_dma) usb_frag_freelist =
105 LIST_HEAD_INITIALIZER(usb_frag_freelist);
106
107 Static usbd_status
108 usb_block_allocmem(bus_dma_tag_t tag, size_t size, size_t align,
109 usb_dma_block_t **dmap)
110 {
111 int error;
112 usb_dma_block_t *p;
113 int s;
114
115 DPRINTFN(5, ("usb_block_allocmem: size=%lu align=%lu\n",
116 (u_long)size, (u_long)align));
117
118 #ifdef DIAGNOSTIC
119 if (!curproc) {
120 printf("usb_block_allocmem: in interrupt context, size=%lu\n",
121 (unsigned long) size);
122 }
123 #endif
124
125 s = splusb();
126 /* First check the free list. */
127 for (p = LIST_FIRST(&usb_blk_freelist); p; p = LIST_NEXT(p, next)) {
128 if (p->tag == tag && p->size >= size && p->align >= align) {
129 LIST_REMOVE(p, next);
130 usb_blk_nfree--;
131 splx(s);
132 *dmap = p;
133 DPRINTFN(6,("usb_block_allocmem: free list size=%lu\n",
134 (u_long)p->size));
135 return (USBD_NORMAL_COMPLETION);
136 }
137 }
138 splx(s);
139
140 #ifdef DIAGNOSTIC
141 if (!curproc) {
142 printf("usb_block_allocmem: in interrupt context, failed\n");
143 return (USBD_NOMEM);
144 }
145 #endif
146
147 DPRINTFN(6, ("usb_block_allocmem: no free\n"));
148 p = malloc(sizeof *p, M_USB, M_NOWAIT);
149 if (p == NULL)
150 return (USBD_NOMEM);
151
152 p->tag = tag;
153 p->size = size;
154 p->align = align;
155 error = bus_dmamem_alloc(tag, p->size, align, 0,
156 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
157 &p->nsegs, BUS_DMA_NOWAIT);
158 if (error)
159 goto free0;
160
161 error = bus_dmamem_map(tag, p->segs, p->nsegs, p->size,
162 &p->kaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
163 if (error)
164 goto free1;
165
166 error = bus_dmamap_create(tag, p->size, 1, p->size,
167 0, BUS_DMA_NOWAIT, &p->map);
168 if (error)
169 goto unmap;
170
171 error = bus_dmamap_load(tag, p->map, p->kaddr, p->size, NULL,
172 BUS_DMA_NOWAIT);
173 if (error)
174 goto destroy;
175
176 *dmap = p;
177 return (USBD_NORMAL_COMPLETION);
178
179 destroy:
180 bus_dmamap_destroy(tag, p->map);
181 unmap:
182 bus_dmamem_unmap(tag, p->kaddr, p->size);
183 free1:
184 bus_dmamem_free(tag, p->segs, p->nsegs);
185 free0:
186 free(p, M_USB);
187 return (USBD_NOMEM);
188 }
189
190 #if 0
191 void
192 usb_block_real_freemem(usb_dma_block_t *p)
193 {
194 #ifdef DIAGNOSTIC
195 if (!curproc) {
196 printf("usb_block_real_freemem: in interrupt context\n");
197 return;
198 }
199 #endif
200 bus_dmamap_unload(p->tag, p->map);
201 bus_dmamap_destroy(p->tag, p->map);
202 bus_dmamem_unmap(p->tag, p->kaddr, p->size);
203 bus_dmamem_free(p->tag, p->segs, p->nsegs);
204 free(p, M_USB);
205 }
206 #endif
207
208 /*
209 * Do not free the memory unconditionally since we might be called
210 * from an interrupt context and that is BAD.
211 * XXX when should we really free?
212 */
213 Static void
214 usb_block_freemem(usb_dma_block_t *p)
215 {
216 int s;
217
218 DPRINTFN(6, ("usb_block_freemem: size=%lu\n", (u_long)p->size));
219 s = splusb();
220 LIST_INSERT_HEAD(&usb_blk_freelist, p, next);
221 usb_blk_nfree++;
222 splx(s);
223 }
224
225 usbd_status
226 usb_allocmem(usbd_bus_handle bus, size_t size, size_t align, usb_dma_t *p)
227 {
228 bus_dma_tag_t tag = bus->dmatag;
229 usbd_status err;
230 struct usb_frag_dma *f;
231 usb_dma_block_t *b;
232 int i;
233 int s;
234
235 /* If the request is large then just use a full block. */
236 if (size > USB_MEM_SMALL || align > USB_MEM_SMALL) {
237 DPRINTFN(1, ("usb_allocmem: large alloc %d\n", (int)size));
238 size = (size + USB_MEM_BLOCK - 1) & ~(USB_MEM_BLOCK - 1);
239 err = usb_block_allocmem(tag, size, align, &p->block);
240 if (!err) {
241 p->block->flags = USB_DMA_FULLBLOCK;
242 p->offs = 0;
243 }
244 return (err);
245 }
246
247 s = splusb();
248 /* Check for free fragments. */
249 for (f = LIST_FIRST(&usb_frag_freelist); f; f = LIST_NEXT(f, next))
250 if (f->block->tag == tag)
251 break;
252 if (f == NULL) {
253 DPRINTFN(1, ("usb_allocmem: adding fragments\n"));
254 err = usb_block_allocmem(tag, USB_MEM_BLOCK, USB_MEM_SMALL,&b);
255 if (err) {
256 splx(s);
257 return (err);
258 }
259 b->flags = 0;
260 for (i = 0; i < USB_MEM_BLOCK; i += USB_MEM_SMALL) {
261 f = (struct usb_frag_dma *)(b->kaddr + i);
262 f->block = b;
263 f->offs = i;
264 LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
265 }
266 f = LIST_FIRST(&usb_frag_freelist);
267 }
268 p->block = f->block;
269 p->offs = f->offs;
270 p->block->flags &= ~USB_DMA_RESERVE;
271 LIST_REMOVE(f, next);
272 splx(s);
273 DPRINTFN(5, ("usb_allocmem: use frag=%p size=%d\n", f, (int)size));
274 return (USBD_NORMAL_COMPLETION);
275 }
276
277 void
278 usb_freemem(usbd_bus_handle bus, usb_dma_t *p)
279 {
280 struct usb_frag_dma *f;
281 int s;
282
283 if (p->block->flags & USB_DMA_FULLBLOCK) {
284 DPRINTFN(1, ("usb_freemem: large free\n"));
285 usb_block_freemem(p->block);
286 return;
287 }
288 f = KERNADDR(p, 0);
289 f->block = p->block;
290 f->offs = p->offs;
291 s = splusb();
292 LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
293 splx(s);
294 DPRINTFN(5, ("usb_freemem: frag=%p\n", f));
295 }
296
297 void
298 usb_syncmem(usb_dma_t *p, bus_addr_t offset, bus_size_t len, int ops)
299 {
300 bus_dmamap_sync(p->block->tag, p->block->map, p->offs + offset,
301 len, ops);
302 }
303
304
305 #ifdef __NetBSD__
306 usbd_status
307 usb_reserve_allocm(struct usb_dma_reserve *rs, usb_dma_t *dma, u_int32_t size)
308 {
309 int error;
310 u_long start;
311 bus_addr_t baddr;
312
313 if (rs->vaddr == 0)
314 return USBD_NOMEM;
315
316 dma->block = malloc(sizeof *dma->block, M_USB, M_ZERO | M_NOWAIT);
317 if (dma->block == NULL)
318 return USBD_NOMEM;
319
320 error = extent_alloc(rs->extent, size, PAGE_SIZE, 0,
321 EX_NOWAIT, &start);
322
323 if (error != 0) {
324 printf("%s: usb_reserve_allocm of size %u failed (error %d)\n",
325 ((struct device *)rs->softc)->dv_xname, size, error);
326 return USBD_NOMEM;
327 }
328
329 baddr = start;
330 dma->offs = baddr - rs->paddr;
331 dma->block->flags = USB_DMA_RESERVE;
332 dma->block->align = PAGE_SIZE;
333 dma->block->size = size;
334 dma->block->nsegs = 1;
335 /* XXX segs appears to be unused */
336 dma->block->segs[0] = rs->map->dm_segs[0];
337 dma->block->map = rs->map;
338 dma->block->kaddr = rs->vaddr;
339 dma->block->tag = rs->dtag;
340
341 return USBD_NORMAL_COMPLETION;
342 }
343
344 void
345 usb_reserve_freem(struct usb_dma_reserve *rs, usb_dma_t *dma)
346 {
347 int error;
348
349 error = extent_free(rs->extent,
350 (u_long)(rs->paddr + dma->offs), dma->block->size, 0);
351 free(dma->block, M_USB);
352 }
353
354 int
355 usb_setup_reserve(void *softc, struct usb_dma_reserve *rs, bus_dma_tag_t dtag,
356 size_t size)
357 {
358 int error, nseg;
359 bus_dma_segment_t seg;
360 struct device *dv = softc;
361
362 rs->dtag = dtag;
363 rs->size = size;
364 rs->softc = softc;
365
366 error = bus_dmamem_alloc(dtag, USB_MEM_RESERVE, PAGE_SIZE, 0,
367 &seg, 1, &nseg, BUS_DMA_NOWAIT);
368 if (error != 0)
369 return error;
370
371 error = bus_dmamem_map(dtag, &seg, nseg, USB_MEM_RESERVE,
372 &rs->vaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
373 if (error != 0)
374 goto freeit;
375
376 error = bus_dmamap_create(dtag, USB_MEM_RESERVE, 1,
377 USB_MEM_RESERVE, 0, BUS_DMA_NOWAIT, &rs->map);
378 if (error != 0)
379 goto unmap;
380
381 error = bus_dmamap_load(dtag, rs->map, rs->vaddr, USB_MEM_RESERVE,
382 NULL, BUS_DMA_NOWAIT);
383 if (error != 0)
384 goto destroy;
385
386 rs->paddr = rs->map->dm_segs[0].ds_addr;
387 rs->extent = extent_create(dv->dv_xname, (u_long)rs->paddr,
388 (u_long)(rs->paddr + USB_MEM_RESERVE),
389 M_USB, 0, 0, 0);
390 if (rs->extent == NULL) {
391 rs->vaddr = 0;
392 return ENOMEM;
393 }
394
395 return 0;
396
397 destroy:
398 bus_dmamap_destroy(dtag, rs->map);
399 unmap:
400 bus_dmamem_unmap(dtag, rs->vaddr, size);
401 freeit:
402 bus_dmamem_free(dtag, &seg, nseg);
403
404 rs->vaddr = 0;
405
406 return error;
407 }
408 #endif
Cache object: 871fd3680410f7b0b2f29210e95b8138
|