1 /* $OpenBSD: usb_mem.c,v 1.27 2014/10/31 15:20:01 mpi 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 s = splusb(); 98 /* First check the free list. */ 99 for (p = LIST_FIRST(&usb_blk_freelist); p; p = LIST_NEXT(p, next)) { 100 if (p->tag == tag && p->size >= size && p->align >= align) { 101 LIST_REMOVE(p, next); 102 usb_blk_nfree--; 103 splx(s); 104 *dmap = p; 105 DPRINTFN(6,("usb_block_allocmem: free list size=%lu\n", 106 (u_long)p->size)); 107 return (USBD_NORMAL_COMPLETION); 108 } 109 } 110 splx(s); 111 112 #ifdef DIAGNOSTIC 113 if (!curproc) { 114 printf("usb_block_allocmem: in interrupt context, failed\n"); 115 return (USBD_NOMEM); 116 } 117 #endif 118 119 DPRINTFN(6, ("usb_block_allocmem: no free\n")); 120 p = malloc(sizeof *p, M_USB, M_NOWAIT); 121 if (p == NULL) 122 return (USBD_NOMEM); 123 124 p->tag = tag; 125 p->size = size; 126 p->align = align; 127 error = bus_dmamem_alloc(tag, p->size, align, 0, 128 p->segs, nitems(p->segs), 129 &p->nsegs, BUS_DMA_NOWAIT); 130 if (error) 131 goto free0; 132 133 error = bus_dmamem_map(tag, p->segs, p->nsegs, p->size, 134 &p->kaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT); 135 if (error) 136 goto free1; 137 138 error = bus_dmamap_create(tag, p->size, 1, p->size, 139 0, BUS_DMA_NOWAIT, &p->map); 140 if (error) 141 goto unmap; 142 143 error = bus_dmamap_load(tag, p->map, p->kaddr, p->size, NULL, 144 BUS_DMA_NOWAIT); 145 if (error) 146 goto destroy; 147 148 *dmap = p; 149 return (USBD_NORMAL_COMPLETION); 150 151 destroy: 152 bus_dmamap_destroy(tag, p->map); 153 unmap: 154 bus_dmamem_unmap(tag, p->kaddr, p->size); 155 free1: 156 bus_dmamem_free(tag, p->segs, p->nsegs); 157 free0: 158 free(p, M_USB, 0); 159 return (USBD_NOMEM); 160 } 161 162 #if 0 163 void 164 usb_block_real_freemem(struct usb_dma_block *p) 165 { 166 #ifdef DIAGNOSTIC 167 if (!curproc) { 168 printf("usb_block_real_freemem: in interrupt context\n"); 169 return; 170 } 171 #endif 172 bus_dmamap_unload(p->tag, p->map); 173 bus_dmamap_destroy(p->tag, p->map); 174 bus_dmamem_unmap(p->tag, p->kaddr, p->size); 175 bus_dmamem_free(p->tag, p->segs, p->nsegs); 176 free(p, M_USB, 0); 177 } 178 #endif 179 180 /* 181 * Do not free the memory unconditionally since we might be called 182 * from an interrupt context and that is BAD. 183 * XXX when should we really free? 184 */ 185 void 186 usb_block_freemem(struct usb_dma_block *p) 187 { 188 int s; 189 190 DPRINTFN(6, ("usb_block_freemem: size=%lu\n", (u_long)p->size)); 191 s = splusb(); 192 LIST_INSERT_HEAD(&usb_blk_freelist, p, next); 193 usb_blk_nfree++; 194 splx(s); 195 } 196 197 usbd_status 198 usb_allocmem(struct usbd_bus *bus, size_t size, size_t align, struct usb_dma *p) 199 { 200 bus_dma_tag_t tag = bus->dmatag; 201 usbd_status err; 202 struct usb_frag_dma *f; 203 struct usb_dma_block *b; 204 int i; 205 int s; 206 207 /* If the request is large then just use a full block. */ 208 if (size > USB_MEM_SMALL || align > USB_MEM_SMALL) { 209 DPRINTFN(1, ("usb_allocmem: large alloc %d\n", (int)size)); 210 size = (size + USB_MEM_BLOCK - 1) & ~(USB_MEM_BLOCK - 1); 211 err = usb_block_allocmem(tag, size, align, &p->block); 212 if (!err) { 213 p->block->fullblock = 1; 214 p->offs = 0; 215 } 216 return (err); 217 } 218 219 s = splusb(); 220 /* Check for free fragments. */ 221 for (f = LIST_FIRST(&usb_frag_freelist); f; f = LIST_NEXT(f, next)) 222 if (f->block->tag == tag) 223 break; 224 if (f == NULL) { 225 DPRINTFN(1, ("usb_allocmem: adding fragments\n")); 226 err = usb_block_allocmem(tag, USB_MEM_BLOCK, USB_MEM_SMALL,&b); 227 if (err) { 228 splx(s); 229 return (err); 230 } 231 b->fullblock = 0; 232 for (i = 0; i < USB_MEM_BLOCK; i += USB_MEM_SMALL) { 233 f = (struct usb_frag_dma *)(b->kaddr + i); 234 f->block = b; 235 f->offs = i; 236 LIST_INSERT_HEAD(&usb_frag_freelist, f, next); 237 } 238 f = LIST_FIRST(&usb_frag_freelist); 239 } 240 p->block = f->block; 241 p->offs = f->offs; 242 LIST_REMOVE(f, next); 243 splx(s); 244 DPRINTFN(5, ("usb_allocmem: use frag=%p size=%d\n", f, (int)size)); 245 return (USBD_NORMAL_COMPLETION); 246 } 247 248 void 249 usb_freemem(struct usbd_bus *bus, struct usb_dma *p) 250 { 251 struct usb_frag_dma *f; 252 int s; 253 254 if (p->block->fullblock) { 255 DPRINTFN(1, ("usb_freemem: large free\n")); 256 usb_block_freemem(p->block); 257 return; 258 } 259 f = KERNADDR(p, 0); 260 f->block = p->block; 261 f->offs = p->offs; 262 s = splusb(); 263 LIST_INSERT_HEAD(&usb_frag_freelist, f, next); 264 splx(s); 265 DPRINTFN(5, ("usb_freemem: frag=%p block=%p\n", f, f->block)); 266 } 267 268 void 269 usb_syncmem(struct usb_dma *p, bus_addr_t offset, bus_size_t len, int ops) 270 { 271 bus_dmamap_sync(p->block->tag, p->block->map, p->offs + offset, 272 len, ops); 273 } 274