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