1 /* $OpenBSD: usb_mem.c,v 1.25 2013/04/15 09:23:02 mglocker 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 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 /* 35 * USB DMA memory allocation. 36 * We need to allocate a lot of small (many 8 byte, some larger) 37 * memory blocks that can be used for DMA. Using the bus_dma 38 * routines directly would incur large overheads in space and time. 39 */ 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/kernel.h> 44 #include <sys/malloc.h> 45 #include <sys/queue.h> 46 #include <sys/timeout.h> 47 #include <sys/device.h> /* for usbdivar.h */ 48 #include <machine/bus.h> 49 50 #include <dev/usb/usb.h> 51 #include <dev/usb/usbdi.h> 52 #include <dev/usb/usbdivar.h> /* just for struct usb_dma */ 53 #include <dev/usb/usb_mem.h> 54 55 #ifdef USB_DEBUG 56 #define DPRINTF(x) do { if (usbdebug) printf x; } while (0) 57 #define DPRINTFN(n,x) do { if (usbdebug>(n)) printf x; } while (0) 58 extern int usbdebug; 59 #else 60 #define DPRINTF(x) 61 #define DPRINTFN(n,x) 62 #endif 63 64 #define USB_MEM_SMALL 64 65 #define USB_MEM_CHUNKS 64 66 #define USB_MEM_BLOCK (USB_MEM_SMALL * USB_MEM_CHUNKS) 67 68 /* This struct is overlayed on free fragments. */ 69 struct usb_frag_dma { 70 struct usb_dma_block *block; 71 u_int offs; 72 LIST_ENTRY(usb_frag_dma) next; 73 }; 74 75 usbd_status usb_block_allocmem(bus_dma_tag_t, size_t, size_t, 76 struct usb_dma_block **); 77 void usb_block_freemem(struct usb_dma_block *); 78 79 LIST_HEAD(, usb_dma_block) usb_blk_freelist = 80 LIST_HEAD_INITIALIZER(usb_blk_freelist); 81 int usb_blk_nfree = 0; 82 /* XXX should have different free list for different tags (for speed) */ 83 LIST_HEAD(, usb_frag_dma) usb_frag_freelist = 84 LIST_HEAD_INITIALIZER(usb_frag_freelist); 85 86 usbd_status 87 usb_block_allocmem(bus_dma_tag_t tag, size_t size, size_t align, 88 struct usb_dma_block **dmap) 89 { 90 int error; 91 struct usb_dma_block *p; 92 int s; 93 94 DPRINTFN(5, ("usb_block_allocmem: size=%lu align=%lu\n", 95 (u_long)size, (u_long)align)); 96 97 #ifdef DIAGNOSTIC 98 if (!curproc) { 99 printf("usb_block_allocmem: in interrupt context, size=%lu\n", 100 (unsigned long) size); 101 } 102 #endif 103 104 s = splusb(); 105 /* First check the free list. */ 106 for (p = LIST_FIRST(&usb_blk_freelist); p; p = LIST_NEXT(p, next)) { 107 if (p->tag == tag && p->size >= size && p->align >= align) { 108 LIST_REMOVE(p, next); 109 usb_blk_nfree--; 110 splx(s); 111 *dmap = p; 112 DPRINTFN(6,("usb_block_allocmem: free list size=%lu\n", 113 (u_long)p->size)); 114 return (USBD_NORMAL_COMPLETION); 115 } 116 } 117 splx(s); 118 119 #ifdef DIAGNOSTIC 120 if (!curproc) { 121 printf("usb_block_allocmem: in interrupt context, failed\n"); 122 return (USBD_NOMEM); 123 } 124 #endif 125 126 DPRINTFN(6, ("usb_block_allocmem: no free\n")); 127 p = malloc(sizeof *p, M_USB, M_NOWAIT); 128 if (p == NULL) 129 return (USBD_NOMEM); 130 131 p->tag = tag; 132 p->size = size; 133 p->align = align; 134 error = bus_dmamem_alloc(tag, p->size, align, 0, 135 p->segs, nitems(p->segs), 136 &p->nsegs, BUS_DMA_NOWAIT); 137 if (error) 138 goto free0; 139 140 error = bus_dmamem_map(tag, p->segs, p->nsegs, p->size, 141 &p->kaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT); 142 if (error) 143 goto free1; 144 145 error = bus_dmamap_create(tag, p->size, 1, p->size, 146 0, BUS_DMA_NOWAIT, &p->map); 147 if (error) 148 goto unmap; 149 150 error = bus_dmamap_load(tag, p->map, p->kaddr, p->size, NULL, 151 BUS_DMA_NOWAIT); 152 if (error) 153 goto destroy; 154 155 *dmap = p; 156 return (USBD_NORMAL_COMPLETION); 157 158 destroy: 159 bus_dmamap_destroy(tag, p->map); 160 unmap: 161 bus_dmamem_unmap(tag, p->kaddr, p->size); 162 free1: 163 bus_dmamem_free(tag, p->segs, p->nsegs); 164 free0: 165 free(p, M_USB); 166 return (USBD_NOMEM); 167 } 168 169 #if 0 170 void 171 usb_block_real_freemem(struct usb_dma_block *p) 172 { 173 #ifdef DIAGNOSTIC 174 if (!curproc) { 175 printf("usb_block_real_freemem: in interrupt context\n"); 176 return; 177 } 178 #endif 179 bus_dmamap_unload(p->tag, p->map); 180 bus_dmamap_destroy(p->tag, p->map); 181 bus_dmamem_unmap(p->tag, p->kaddr, p->size); 182 bus_dmamem_free(p->tag, p->segs, p->nsegs); 183 free(p, M_USB); 184 } 185 #endif 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 should we really free? 191 */ 192 void 193 usb_block_freemem(struct usb_dma_block *p) 194 { 195 int s; 196 197 DPRINTFN(6, ("usb_block_freemem: size=%lu\n", (u_long)p->size)); 198 s = splusb(); 199 LIST_INSERT_HEAD(&usb_blk_freelist, p, next); 200 usb_blk_nfree++; 201 splx(s); 202 } 203 204 usbd_status 205 usb_allocmem(struct usbd_bus *bus, size_t size, size_t align, struct usb_dma *p) 206 { 207 bus_dma_tag_t tag = bus->dmatag; 208 usbd_status err; 209 struct usb_frag_dma *f; 210 struct usb_dma_block *b; 211 int i; 212 int s; 213 214 /* If the request is large then just use a full block. */ 215 if (size > USB_MEM_SMALL || align > USB_MEM_SMALL) { 216 DPRINTFN(1, ("usb_allocmem: large alloc %d\n", (int)size)); 217 size = (size + USB_MEM_BLOCK - 1) & ~(USB_MEM_BLOCK - 1); 218 err = usb_block_allocmem(tag, size, align, &p->block); 219 if (!err) { 220 p->block->fullblock = 1; 221 p->offs = 0; 222 } 223 return (err); 224 } 225 226 s = splusb(); 227 /* Check for free fragments. */ 228 for (f = LIST_FIRST(&usb_frag_freelist); f; f = LIST_NEXT(f, next)) 229 if (f->block->tag == tag) 230 break; 231 if (f == NULL) { 232 DPRINTFN(1, ("usb_allocmem: adding fragments\n")); 233 err = usb_block_allocmem(tag, USB_MEM_BLOCK, USB_MEM_SMALL,&b); 234 if (err) { 235 splx(s); 236 return (err); 237 } 238 b->fullblock = 0; 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(struct usbd_bus *bus, struct usb_dma *p) 257 { 258 struct usb_frag_dma *f; 259 int s; 260 261 if (p->block->fullblock) { 262 DPRINTFN(1, ("usb_freemem: large free\n")); 263 usb_block_freemem(p->block); 264 return; 265 } 266 f = KERNADDR(p, 0); 267 f->block = p->block; 268 f->offs = p->offs; 269 s = splusb(); 270 LIST_INSERT_HEAD(&usb_frag_freelist, f, next); 271 splx(s); 272 DPRINTFN(5, ("usb_freemem: frag=%p\n", f)); 273 } 274 275 void 276 usb_syncmem(struct usb_dma *p, bus_addr_t offset, bus_size_t len, int ops) 277 { 278 bus_dmamap_sync(p->block->tag, p->block->map, p->offs + offset, 279 len, ops); 280 } 281