usb_mem.c revision 1.58 1 /* $NetBSD: usb_mem.c,v 1.58 2013/01/19 20:49:33 christos 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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * USB DMA memory allocation.
35 * We need to allocate a lot of small (many 8 byte, some larger)
36 * memory blocks that can be used for DMA. Using the bus_dma
37 * routines directly would incur large overheads in space and time.
38 */
39
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: usb_mem.c,v 1.58 2013/01/19 20:49:33 christos Exp $");
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/kmem.h>
47 #include <sys/queue.h>
48 #include <sys/device.h> /* for usbdivar.h */
49 #include <sys/bus.h>
50 #include <sys/cpu.h>
51 #include <sys/once.h>
52
53 #include <sys/extent.h>
54
55 #ifdef DIAGNOSTIC
56 #include <sys/proc.h>
57 #endif
58
59 #include <dev/usb/usb.h>
60 #include <dev/usb/usbdi.h>
61 #include <dev/usb/usbdivar.h> /* just for usb_dma_t */
62 #include <dev/usb/usb_mem.h>
63
64 #ifdef USB_DEBUG
65 #define DPRINTF(x) if (usbdebug) printf x
66 #define DPRINTFN(n,x) if (usbdebug>(n)) printf x
67 extern int usbdebug;
68 #else
69 #define DPRINTF(x)
70 #define DPRINTFN(n,x)
71 #endif
72
73 #define USB_MEM_SMALL roundup(64, CACHE_LINE_SIZE)
74 #define USB_MEM_CHUNKS 64
75 #define USB_MEM_BLOCK (USB_MEM_SMALL * USB_MEM_CHUNKS)
76
77 /* This struct is overlayed on free fragments. */
78 struct usb_frag_dma {
79 usb_dma_block_t *block;
80 u_int offs;
81 LIST_ENTRY(usb_frag_dma) next;
82 };
83
84 Static usbd_status usb_block_allocmem(bus_dma_tag_t, size_t, size_t,
85 usb_dma_block_t **, bool);
86 Static void usb_block_freemem(usb_dma_block_t *);
87
88 LIST_HEAD(usb_dma_block_qh, usb_dma_block);
89 Static struct usb_dma_block_qh usb_blk_freelist =
90 LIST_HEAD_INITIALIZER(usb_blk_freelist);
91 kmutex_t usb_blk_lock;
92
93 #ifdef DEBUG
94 Static struct usb_dma_block_qh usb_blk_fraglist =
95 LIST_HEAD_INITIALIZER(usb_blk_fraglist);
96 Static struct usb_dma_block_qh usb_blk_fulllist =
97 LIST_HEAD_INITIALIZER(usb_blk_fulllist);
98 #endif
99 Static u_int usb_blk_nfree = 0;
100 /* XXX should have different free list for different tags (for speed) */
101 Static LIST_HEAD(, usb_frag_dma) usb_frag_freelist =
102 LIST_HEAD_INITIALIZER(usb_frag_freelist);
103
104 Static int usb_mem_init(void);
105
106 Static int
107 usb_mem_init(void)
108 {
109
110 mutex_init(&usb_blk_lock, MUTEX_DEFAULT, IPL_NONE);
111 return 0;
112 }
113
114 Static usbd_status
115 usb_block_allocmem(bus_dma_tag_t tag, size_t size, size_t align,
116 usb_dma_block_t **dmap, bool multiseg)
117 {
118 usb_dma_block_t *b;
119 int error;
120
121 DPRINTFN(5, ("usb_block_allocmem: size=%zu align=%zu\n", size, align));
122
123 if (size == 0) {
124 #ifdef DIAGNOSTIC
125 printf("usb_block_allocmem: called with size==0\n");
126 #endif
127 return USBD_INVAL;
128 }
129
130 #ifdef DIAGNOSTIC
131 if (cpu_intr_p()) {
132 printf("usb_block_allocmem: in interrupt context, size=%lu\n",
133 (unsigned long) size);
134 }
135 #endif
136
137 KASSERT(mutex_owned(&usb_blk_lock));
138
139 /* First check the free list. */
140 LIST_FOREACH(b, &usb_blk_freelist, next) {
141 /* Don't allocate multiple segments to unwilling callers */
142 if (b->nsegs != 1 && !multiseg)
143 continue;
144 if (b->tag == tag && b->size >= size && b->align >= align) {
145 LIST_REMOVE(b, next);
146 usb_blk_nfree--;
147 *dmap = b;
148 DPRINTFN(6,("usb_block_allocmem: free list size=%zu\n",
149 b->size));
150 return (USBD_NORMAL_COMPLETION);
151 }
152 }
153
154 #ifdef DIAGNOSTIC
155 if (cpu_intr_p()) {
156 printf("usb_block_allocmem: in interrupt context, failed\n");
157 return (USBD_NOMEM);
158 }
159 #endif
160
161 DPRINTFN(6, ("usb_block_allocmem: no free\n"));
162 b = kmem_zalloc(sizeof *b, KM_SLEEP);
163 if (b == NULL)
164 return (USBD_NOMEM);
165
166 b->tag = tag;
167 b->size = size;
168 b->align = align;
169
170 if (!multiseg)
171 /* Caller wants one segment */
172 b->nsegs = 1;
173 else
174 b->nsegs = (size + (PAGE_SIZE-1)) / PAGE_SIZE;
175
176 b->segs = kmem_alloc(b->nsegs * sizeof(*b->segs), KM_SLEEP);
177 if (b->segs == NULL) {
178 kmem_free(b, sizeof *b);
179 return USBD_NOMEM;
180 }
181
182 error = bus_dmamem_alloc(tag, b->size, align, 0,
183 b->segs, b->nsegs,
184 &b->nsegs, BUS_DMA_NOWAIT);
185 if (error)
186 goto free0;
187
188 error = bus_dmamem_map(tag, b->segs, b->nsegs, b->size,
189 &b->kaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
190 if (error)
191 goto free1;
192
193 error = bus_dmamap_create(tag, b->size, b->nsegs, b->size,
194 0, BUS_DMA_NOWAIT, &b->map);
195 if (error)
196 goto unmap;
197
198 error = bus_dmamap_load(tag, b->map, b->kaddr, b->size, NULL,
199 BUS_DMA_NOWAIT);
200 if (error)
201 goto destroy;
202
203 *dmap = b;
204 #ifdef USB_FRAG_DMA_WORKAROUND
205 memset(b->kaddr, 0, b->size);
206 #endif
207
208 return (USBD_NORMAL_COMPLETION);
209
210 destroy:
211 bus_dmamap_destroy(tag, b->map);
212 unmap:
213 bus_dmamem_unmap(tag, b->kaddr, b->size);
214 free1:
215 bus_dmamem_free(tag, b->segs, b->nsegs);
216 free0:
217 kmem_free(b->segs, b->nsegs * sizeof(*b->segs));
218 kmem_free(b, sizeof *b);
219 return (USBD_NOMEM);
220 }
221
222 #if 0
223 void
224 usb_block_real_freemem(usb_dma_block_t *b)
225 {
226 #ifdef DIAGNOSTIC
227 if (cpu_intr_p()) {
228 printf("usb_block_real_freemem: in interrupt context\n");
229 return;
230 }
231 #endif
232 bus_dmamap_unload(b->tag, b->map);
233 bus_dmamap_destroy(b->tag, b->map);
234 bus_dmamem_unmap(b->tag, b->kaddr, b->size);
235 bus_dmamem_free(b->tag, b->segs, b->nsegs);
236 kmem_free(b->segs, b->nsegs * sizeof(*b->segs));
237 kmem_free(b, sizeof *b);
238 }
239 #endif
240
241 #ifdef DEBUG
242 static bool
243 usb_valid_block_p(usb_dma_block_t *b, struct usb_dma_block_qh *qh)
244 {
245 usb_dma_block_t *xb;
246 LIST_FOREACH(xb, qh, next) {
247 if (xb == b)
248 return true;
249 }
250 return false;
251 }
252 #endif
253
254 /*
255 * Do not free the memory unconditionally since we might be called
256 * from an interrupt context and that is BAD.
257 * XXX when should we really free?
258 */
259 Static void
260 usb_block_freemem(usb_dma_block_t *b)
261 {
262
263 KASSERT(mutex_owned(&usb_blk_lock));
264
265 DPRINTFN(6, ("usb_block_freemem: size=%zu\n", b->size));
266 #ifdef DEBUG
267 LIST_REMOVE(b, next);
268 #endif
269 LIST_INSERT_HEAD(&usb_blk_freelist, b, next);
270 usb_blk_nfree++;
271 }
272
273 usbd_status
274 usb_allocmem(usbd_bus_handle bus, size_t size, size_t align, usb_dma_t *p)
275 {
276 return usb_allocmem_flags(bus, size, align, p, 0);
277 }
278
279 usbd_status
280 usb_allocmem_flags(usbd_bus_handle bus, size_t size, size_t align, usb_dma_t *p,
281 int flags)
282 {
283 bus_dma_tag_t tag = bus->dmatag;
284 usbd_status err;
285 struct usb_frag_dma *f;
286 usb_dma_block_t *b;
287 int i;
288 static ONCE_DECL(init_control);
289 bool frag;
290
291 RUN_ONCE(&init_control, usb_mem_init);
292
293 frag = (flags & USBMALLOC_MULTISEG);
294
295 /* If the request is large then just use a full block. */
296 if (size > USB_MEM_SMALL || align > USB_MEM_SMALL) {
297 DPRINTFN(1, ("usb_allocmem: large alloc %d\n", (int)size));
298 size = (size + USB_MEM_BLOCK - 1) & ~(USB_MEM_BLOCK - 1);
299 mutex_enter(&usb_blk_lock);
300 err = usb_block_allocmem(tag, size, align, &p->block, frag);
301 if (!err) {
302 #ifdef DEBUG
303 LIST_INSERT_HEAD(&usb_blk_fulllist, p->block, next);
304 #endif
305 p->block->flags = USB_DMA_FULLBLOCK;
306 p->offs = 0;
307 }
308 mutex_exit(&usb_blk_lock);
309 return (err);
310 }
311
312 mutex_enter(&usb_blk_lock);
313 /* Check for free fragments. */
314 LIST_FOREACH(f, &usb_frag_freelist, next) {
315 KDASSERTMSG(usb_valid_block_p(f->block, &usb_blk_fraglist),
316 "%s: usb frag %p: unknown block pointer %p",
317 __func__, f, f->block);
318 if (f->block->tag == tag)
319 break;
320 }
321 if (f == NULL) {
322 DPRINTFN(1, ("usb_allocmem: adding fragments\n"));
323 err = usb_block_allocmem(tag, USB_MEM_BLOCK, USB_MEM_SMALL, &b,
324 false);
325 if (err) {
326 mutex_exit(&usb_blk_lock);
327 return (err);
328 }
329 #ifdef DEBUG
330 LIST_INSERT_HEAD(&usb_blk_fraglist, b, next);
331 #endif
332 b->flags = 0;
333 for (i = 0; i < USB_MEM_BLOCK; i += USB_MEM_SMALL) {
334 f = (struct usb_frag_dma *)((char *)b->kaddr + i);
335 f->block = b;
336 f->offs = i;
337 LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
338 #ifdef USB_FRAG_DMA_WORKAROUND
339 i += 1 * USB_MEM_SMALL;
340 #endif
341 }
342 f = LIST_FIRST(&usb_frag_freelist);
343 }
344 p->block = f->block;
345 p->offs = f->offs;
346 #ifdef USB_FRAG_DMA_WORKAROUND
347 p->offs += USB_MEM_SMALL;
348 #endif
349 p->block->flags &= ~USB_DMA_RESERVE;
350 LIST_REMOVE(f, next);
351 mutex_exit(&usb_blk_lock);
352 DPRINTFN(5, ("usb_allocmem: use frag=%p size=%d\n", f, (int)size));
353
354 return (USBD_NORMAL_COMPLETION);
355 }
356
357 void
358 usb_freemem(usbd_bus_handle bus, usb_dma_t *p)
359 {
360 struct usb_frag_dma *f;
361
362 mutex_enter(&usb_blk_lock);
363 if (p->block->flags & USB_DMA_FULLBLOCK) {
364 KDASSERTMSG(usb_valid_block_p(p->block, &usb_blk_fulllist),
365 "%s: dma %p: invalid block pointer %p",
366 __func__, p, p->block);
367 DPRINTFN(1, ("usb_freemem: large free\n"));
368 usb_block_freemem(p->block);
369 mutex_exit(&usb_blk_lock);
370 return;
371 }
372 KDASSERTMSG(usb_valid_block_p(p->block, &usb_blk_fraglist),
373 "%s: dma %p: invalid block pointer %p",
374 __func__, p, p->block);
375 //usb_syncmem(p, 0, USB_MEM_SMALL, BUS_DMASYNC_POSTREAD);
376 f = KERNADDR(p, 0);
377 #ifdef USB_FRAG_DMA_WORKAROUND
378 f = (void *)((uintptr_t)f - USB_MEM_SMALL);
379 #endif
380 f->block = p->block;
381 f->offs = p->offs;
382 #ifdef USB_FRAG_DMA_WORKAROUND
383 f->offs -= USB_MEM_SMALL;
384 #endif
385 LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
386 mutex_exit(&usb_blk_lock);
387 DPRINTFN(5, ("usb_freemem: frag=%p\n", f));
388 }
389
390 bus_addr_t
391 usb_dmaaddr(usb_dma_t *dma, unsigned int offset)
392 {
393 unsigned int i;
394 bus_size_t seg_offs;
395
396 offset += dma->offs;
397
398 KASSERT(offset < dma->block->size);
399
400 if (dma->block->nsegs == 1) {
401 KASSERT(dma->block->map->dm_segs[0].ds_len > offset);
402 return dma->block->map->dm_segs[0].ds_addr + offset;
403 }
404
405 /* Search for a bus_segment_t corresponding to this offset. With no
406 * record of the offset in the map to a particular dma_segment_t, we
407 * have to iterate from the start of the list each time. Could be
408 * improved */
409 seg_offs = 0;
410 for (i = 0; i < dma->block->nsegs; i++) {
411 if (seg_offs + dma->block->map->dm_segs[i].ds_len > offset)
412 break;
413
414 seg_offs += dma->block->map->dm_segs[i].ds_len;
415 }
416
417 KASSERT(i != dma->block->nsegs);
418 offset -= seg_offs;
419 return dma->block->map->dm_segs[i].ds_addr + offset;
420 }
421
422 void
423 usb_syncmem(usb_dma_t *p, bus_addr_t offset, bus_size_t len, int ops)
424 {
425 bus_dmamap_sync(p->block->tag, p->block->map, p->offs + offset,
426 len, ops);
427 }
428
429
430 usbd_status
431 usb_reserve_allocm(struct usb_dma_reserve *rs, usb_dma_t *dma, u_int32_t size)
432 {
433 int error;
434 u_long start;
435 bus_addr_t baddr;
436
437 if (rs->vaddr == 0 || size > USB_MEM_RESERVE)
438 return USBD_NOMEM;
439
440 dma->block = kmem_zalloc(sizeof *dma->block, KM_SLEEP);
441 if (dma->block == NULL)
442 return USBD_NOMEM;
443
444 error = extent_alloc(rs->extent, size, PAGE_SIZE, 0,
445 EX_NOWAIT, &start);
446
447 if (error != 0) {
448 aprint_error_dev(rs->dv,
449 "usb_reserve_allocm of size %u failed (error %d)\n",
450 size, error);
451 return USBD_NOMEM;
452 }
453
454 baddr = start;
455 dma->offs = baddr - rs->paddr;
456 dma->block->flags = USB_DMA_RESERVE;
457 dma->block->align = PAGE_SIZE;
458 dma->block->size = size;
459 dma->block->nsegs = 1;
460 /* XXX segs appears to be unused */
461 dma->block->segs[0] = rs->map->dm_segs[0];
462 dma->block->map = rs->map;
463 dma->block->kaddr = rs->vaddr;
464 dma->block->tag = rs->dtag;
465
466 return USBD_NORMAL_COMPLETION;
467 }
468
469 void
470 usb_reserve_freem(struct usb_dma_reserve *rs, usb_dma_t *dma)
471 {
472 int error;
473
474 error = extent_free(rs->extent,
475 (u_long)(rs->paddr + dma->offs), dma->block->size, 0);
476 /* XXXPW correct that segs[0] is not used? */
477 kmem_free(dma->block, sizeof *dma->block);
478 }
479
480 int
481 usb_setup_reserve(device_t dv, struct usb_dma_reserve *rs, bus_dma_tag_t dtag,
482 size_t size)
483 {
484 int error, nseg;
485 bus_dma_segment_t seg;
486
487 rs->dtag = dtag;
488 rs->size = size;
489 rs->dv = dv;
490
491 error = bus_dmamem_alloc(dtag, USB_MEM_RESERVE, PAGE_SIZE, 0,
492 &seg, 1, &nseg, BUS_DMA_NOWAIT);
493 if (error != 0)
494 return error;
495
496 error = bus_dmamem_map(dtag, &seg, nseg, USB_MEM_RESERVE,
497 &rs->vaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
498 if (error != 0)
499 goto freeit;
500
501 error = bus_dmamap_create(dtag, USB_MEM_RESERVE, 1,
502 USB_MEM_RESERVE, 0, BUS_DMA_NOWAIT, &rs->map);
503 if (error != 0)
504 goto unmap;
505
506 error = bus_dmamap_load(dtag, rs->map, rs->vaddr, USB_MEM_RESERVE,
507 NULL, BUS_DMA_NOWAIT);
508 if (error != 0)
509 goto destroy;
510
511 rs->paddr = rs->map->dm_segs[0].ds_addr;
512 rs->extent = extent_create(device_xname(dv), (u_long)rs->paddr,
513 (u_long)(rs->paddr + USB_MEM_RESERVE - 1), 0, 0, 0);
514 if (rs->extent == NULL) {
515 rs->vaddr = 0;
516 return ENOMEM;
517 }
518
519 return 0;
520
521 destroy:
522 bus_dmamap_destroy(dtag, rs->map);
523 unmap:
524 bus_dmamem_unmap(dtag, rs->vaddr, size);
525 freeit:
526 bus_dmamem_free(dtag, &seg, nseg);
527
528 rs->vaddr = 0;
529
530 return error;
531 }
532