1 /* $NetBSD: usb_mem.c,v 1.45 2011/01/04 01:37:55 matt 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 * 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.45 2011/01/04 01:37:55 matt Exp $"); 42 43 #ifdef _KERNEL_OPT 44 #include "opt_usb.h" 45 #endif 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 <sys/bus.h> 54 #include <sys/cpu.h> 55 56 #ifdef __NetBSD__ 57 #include <sys/extent.h> 58 #endif 59 60 #ifdef DIAGNOSTIC 61 #include <sys/proc.h> 62 #endif 63 64 #include <dev/usb/usb.h> 65 #include <dev/usb/usbdi.h> 66 #include <dev/usb/usbdivar.h> /* just for usb_dma_t */ 67 #include <dev/usb/usb_mem.h> 68 69 #ifdef USB_DEBUG 70 #define DPRINTF(x) if (usbdebug) printf x 71 #define DPRINTFN(n,x) if (usbdebug>(n)) printf x 72 extern int usbdebug; 73 #else 74 #define DPRINTF(x) 75 #define DPRINTFN(n,x) 76 #endif 77 78 MALLOC_DEFINE(M_USB, "USB", "USB misc. memory"); 79 MALLOC_DEFINE(M_USBDEV, "USB device", "USB device driver"); 80 MALLOC_DEFINE(M_USBHC, "USB HC", "USB host controller"); 81 82 #define USB_MEM_SMALL 64 83 #define USB_MEM_CHUNKS 64 84 #define USB_MEM_BLOCK (USB_MEM_SMALL * USB_MEM_CHUNKS) 85 86 /* This struct is overlayed on free fragments. */ 87 struct usb_frag_dma { 88 usb_dma_block_t *block; 89 u_int offs; 90 LIST_ENTRY(usb_frag_dma) next; 91 }; 92 93 Static usbd_status usb_block_allocmem(bus_dma_tag_t, size_t, size_t, 94 usb_dma_block_t **); 95 Static void usb_block_freemem(usb_dma_block_t *); 96 97 Static LIST_HEAD(, usb_dma_block) usb_blk_freelist = 98 LIST_HEAD_INITIALIZER(usb_blk_freelist); 99 Static 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 usbd_status 105 usb_block_allocmem(bus_dma_tag_t tag, size_t size, size_t align, 106 usb_dma_block_t **dmap) 107 { 108 int error; 109 usb_dma_block_t *p; 110 int s; 111 112 DPRINTFN(5, ("usb_block_allocmem: size=%lu align=%lu\n", 113 (u_long)size, (u_long)align)); 114 115 #ifdef DIAGNOSTIC 116 if (cpu_intr_p()) { 117 printf("usb_block_allocmem: in interrupt context, size=%lu\n", 118 (unsigned long) size); 119 } 120 #endif 121 122 s = splusb(); 123 /* First check the free list. */ 124 LIST_FOREACH(p, &usb_blk_freelist, next) { 125 if (p->tag == tag && p->size >= size && p->align >= align) { 126 LIST_REMOVE(p, next); 127 usb_blk_nfree--; 128 splx(s); 129 *dmap = p; 130 DPRINTFN(6,("usb_block_allocmem: free list size=%lu\n", 131 (u_long)p->size)); 132 return (USBD_NORMAL_COMPLETION); 133 } 134 } 135 splx(s); 136 137 #ifdef DIAGNOSTIC 138 if (cpu_intr_p()) { 139 printf("usb_block_allocmem: in interrupt context, failed\n"); 140 return (USBD_NOMEM); 141 } 142 #endif 143 144 DPRINTFN(6, ("usb_block_allocmem: no free\n")); 145 p = malloc(sizeof *p, M_USB, M_NOWAIT); 146 if (p == NULL) 147 return (USBD_NOMEM); 148 149 p->tag = tag; 150 p->size = size; 151 p->align = align; 152 error = bus_dmamem_alloc(tag, p->size, align, 0, 153 p->segs, sizeof(p->segs)/sizeof(p->segs[0]), 154 &p->nsegs, BUS_DMA_NOWAIT); 155 if (error) 156 goto free0; 157 158 error = bus_dmamem_map(tag, p->segs, p->nsegs, p->size, 159 &p->kaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT); 160 if (error) 161 goto free1; 162 163 error = bus_dmamap_create(tag, p->size, 1, p->size, 164 0, BUS_DMA_NOWAIT, &p->map); 165 if (error) 166 goto unmap; 167 168 error = bus_dmamap_load(tag, p->map, p->kaddr, p->size, NULL, 169 BUS_DMA_NOWAIT); 170 if (error) 171 goto destroy; 172 173 *dmap = p; 174 #ifdef USB_FRAG_DMA_WORKAROUND 175 memset(p->kaddr, 0, p->size); 176 #endif 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 (cpu_intr_p()) { 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 LIST_FOREACH(f, &usb_frag_freelist, next) { 250 if (f->block->tag == tag) 251 break; 252 } 253 if (f == NULL) { 254 DPRINTFN(1, ("usb_allocmem: adding fragments\n")); 255 err = usb_block_allocmem(tag, USB_MEM_BLOCK, USB_MEM_SMALL,&b); 256 if (err) { 257 splx(s); 258 return (err); 259 } 260 b->flags = 0; 261 for (i = 0; i < USB_MEM_BLOCK; i += USB_MEM_SMALL) { 262 f = (struct usb_frag_dma *)((char *)b->kaddr + i); 263 f->block = b; 264 f->offs = i; 265 LIST_INSERT_HEAD(&usb_frag_freelist, f, next); 266 #ifdef USB_FRAG_DMA_WORKAROUND 267 i += 1 * USB_MEM_SMALL; 268 #endif 269 } 270 f = LIST_FIRST(&usb_frag_freelist); 271 } 272 p->block = f->block; 273 p->offs = f->offs; 274 #ifdef USB_FRAG_DMA_WORKAROUND 275 p->offs += USB_MEM_SMALL; 276 #endif 277 p->block->flags &= ~USB_DMA_RESERVE; 278 LIST_REMOVE(f, next); 279 splx(s); 280 DPRINTFN(5, ("usb_allocmem: use frag=%p size=%d\n", f, (int)size)); 281 return (USBD_NORMAL_COMPLETION); 282 } 283 284 void 285 usb_freemem(usbd_bus_handle bus, usb_dma_t *p) 286 { 287 struct usb_frag_dma *f; 288 int s; 289 290 if (p->block->flags & USB_DMA_FULLBLOCK) { 291 DPRINTFN(1, ("usb_freemem: large free\n")); 292 usb_block_freemem(p->block); 293 return; 294 } 295 //usb_syncmem(p, 0, USB_MEM_SMALL, BUS_DMASYNC_POSTREAD); 296 f = KERNADDR(p, 0); 297 #ifdef USB_FRAG_DMA_WORKAROUND 298 f = (void *)((uintptr_t)f - USB_MEM_SMALL); 299 #endif 300 f->block = p->block; 301 f->offs = p->offs; 302 #ifdef USB_FRAG_DMA_WORKAROUND 303 f->offs -= USB_MEM_SMALL; 304 #endif 305 s = splusb(); 306 LIST_INSERT_HEAD(&usb_frag_freelist, f, next); 307 splx(s); 308 DPRINTFN(5, ("usb_freemem: frag=%p\n", f)); 309 } 310 311 void 312 usb_syncmem(usb_dma_t *p, bus_addr_t offset, bus_size_t len, int ops) 313 { 314 bus_dmamap_sync(p->block->tag, p->block->map, p->offs + offset, 315 len, ops); 316 } 317 318 319 #ifdef __NetBSD__ 320 usbd_status 321 usb_reserve_allocm(struct usb_dma_reserve *rs, usb_dma_t *dma, u_int32_t size) 322 { 323 int error; 324 u_long start; 325 bus_addr_t baddr; 326 327 if (rs->vaddr == 0 || size > USB_MEM_RESERVE) 328 return USBD_NOMEM; 329 330 dma->block = malloc(sizeof *dma->block, M_USB, M_ZERO | M_NOWAIT); 331 if (dma->block == NULL) 332 return USBD_NOMEM; 333 334 error = extent_alloc(rs->extent, size, PAGE_SIZE, 0, 335 EX_NOWAIT, &start); 336 337 if (error != 0) { 338 aprint_error_dev(rs->dv, 339 "usb_reserve_allocm of size %u failed (error %d)\n", 340 size, error); 341 return USBD_NOMEM; 342 } 343 344 baddr = start; 345 dma->offs = baddr - rs->paddr; 346 dma->block->flags = USB_DMA_RESERVE; 347 dma->block->align = PAGE_SIZE; 348 dma->block->size = size; 349 dma->block->nsegs = 1; 350 /* XXX segs appears to be unused */ 351 dma->block->segs[0] = rs->map->dm_segs[0]; 352 dma->block->map = rs->map; 353 dma->block->kaddr = rs->vaddr; 354 dma->block->tag = rs->dtag; 355 356 return USBD_NORMAL_COMPLETION; 357 } 358 359 void 360 usb_reserve_freem(struct usb_dma_reserve *rs, usb_dma_t *dma) 361 { 362 int error; 363 364 error = extent_free(rs->extent, 365 (u_long)(rs->paddr + dma->offs), dma->block->size, 0); 366 free(dma->block, M_USB); 367 } 368 369 int 370 usb_setup_reserve(device_t dv, struct usb_dma_reserve *rs, bus_dma_tag_t dtag, 371 size_t size) 372 { 373 int error, nseg; 374 bus_dma_segment_t seg; 375 376 rs->dtag = dtag; 377 rs->size = size; 378 rs->dv = dv; 379 380 error = bus_dmamem_alloc(dtag, USB_MEM_RESERVE, PAGE_SIZE, 0, 381 &seg, 1, &nseg, BUS_DMA_NOWAIT); 382 if (error != 0) 383 return error; 384 385 error = bus_dmamem_map(dtag, &seg, nseg, USB_MEM_RESERVE, 386 &rs->vaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT); 387 if (error != 0) 388 goto freeit; 389 390 error = bus_dmamap_create(dtag, USB_MEM_RESERVE, 1, 391 USB_MEM_RESERVE, 0, BUS_DMA_NOWAIT, &rs->map); 392 if (error != 0) 393 goto unmap; 394 395 error = bus_dmamap_load(dtag, rs->map, rs->vaddr, USB_MEM_RESERVE, 396 NULL, BUS_DMA_NOWAIT); 397 if (error != 0) 398 goto destroy; 399 400 rs->paddr = rs->map->dm_segs[0].ds_addr; 401 rs->extent = extent_create(device_xname(dv), (u_long)rs->paddr, 402 (u_long)(rs->paddr + USB_MEM_RESERVE - 1), 403 M_USB, 0, 0, 0); 404 if (rs->extent == NULL) { 405 rs->vaddr = 0; 406 return ENOMEM; 407 } 408 409 return 0; 410 411 destroy: 412 bus_dmamap_destroy(dtag, rs->map); 413 unmap: 414 bus_dmamem_unmap(dtag, rs->vaddr, size); 415 freeit: 416 bus_dmamem_free(dtag, &seg, nseg); 417 418 rs->vaddr = 0; 419 420 return error; 421 } 422 #endif 423