1 /* $NetBSD: usb_mem.c,v 1.53 2012/06/10 06:15:54 mrg 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.53 2012/06/10 06:15:54 mrg 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 #include <sys/once.h> 56 57 #include <sys/extent.h> 58 59 #ifdef DIAGNOSTIC 60 #include <sys/proc.h> 61 #endif 62 63 #include <dev/usb/usb.h> 64 #include <dev/usb/usbdi.h> 65 #include <dev/usb/usbdivar.h> /* just for usb_dma_t */ 66 #include <dev/usb/usb_mem.h> 67 68 #ifdef USB_DEBUG 69 #define DPRINTF(x) if (usbdebug) printf x 70 #define DPRINTFN(n,x) if (usbdebug>(n)) printf x 71 extern int usbdebug; 72 #else 73 #define DPRINTF(x) 74 #define DPRINTFN(n,x) 75 #endif 76 77 #define USB_MEM_SMALL 64 78 #define USB_MEM_CHUNKS 64 79 #define USB_MEM_BLOCK (USB_MEM_SMALL * USB_MEM_CHUNKS) 80 81 /* This struct is overlayed on free fragments. */ 82 struct usb_frag_dma { 83 usb_dma_block_t *block; 84 u_int offs; 85 LIST_ENTRY(usb_frag_dma) next; 86 }; 87 88 Static usbd_status usb_block_allocmem(bus_dma_tag_t, size_t, size_t, 89 usb_dma_block_t **); 90 Static void usb_block_freemem(usb_dma_block_t *); 91 92 LIST_HEAD(usb_dma_block_qh, usb_dma_block); 93 Static struct usb_dma_block_qh usb_blk_freelist = 94 LIST_HEAD_INITIALIZER(usb_blk_freelist); 95 kmutex_t usb_blk_lock; 96 97 #ifdef DEBUG 98 Static struct usb_dma_block_qh usb_blk_fraglist = 99 LIST_HEAD_INITIALIZER(usb_blk_fraglist); 100 Static struct usb_dma_block_qh usb_blk_fulllist = 101 LIST_HEAD_INITIALIZER(usb_blk_fulllist); 102 #endif 103 Static u_int usb_blk_nfree = 0; 104 /* XXX should have different free list for different tags (for speed) */ 105 Static LIST_HEAD(, usb_frag_dma) usb_frag_freelist = 106 LIST_HEAD_INITIALIZER(usb_frag_freelist); 107 108 Static int usb_mem_init(void); 109 110 Static int 111 usb_mem_init(void) 112 { 113 114 mutex_init(&usb_blk_lock, MUTEX_DEFAULT, IPL_NONE); 115 return 0; 116 } 117 118 Static usbd_status 119 usb_block_allocmem(bus_dma_tag_t tag, size_t size, size_t align, 120 usb_dma_block_t **dmap) 121 { 122 usb_dma_block_t *b; 123 int error; 124 125 DPRINTFN(5, ("usb_block_allocmem: size=%zu align=%zu\n", size, align)); 126 127 #ifdef DIAGNOSTIC 128 if (cpu_intr_p()) { 129 printf("usb_block_allocmem: in interrupt context, size=%lu\n", 130 (unsigned long) size); 131 } 132 #endif 133 134 KASSERT(mutex_owned(&usb_blk_lock)); 135 136 /* First check the free list. */ 137 LIST_FOREACH(b, &usb_blk_freelist, next) { 138 if (b->tag == tag && b->size >= size && b->align >= align) { 139 LIST_REMOVE(b, next); 140 usb_blk_nfree--; 141 *dmap = b; 142 DPRINTFN(6,("usb_block_allocmem: free list size=%zu\n", 143 b->size)); 144 return (USBD_NORMAL_COMPLETION); 145 } 146 } 147 148 #ifdef DIAGNOSTIC 149 if (cpu_intr_p()) { 150 printf("usb_block_allocmem: in interrupt context, failed\n"); 151 return (USBD_NOMEM); 152 } 153 #endif 154 155 DPRINTFN(6, ("usb_block_allocmem: no free\n")); 156 b = malloc(sizeof *b, M_USB, M_NOWAIT | M_ZERO); 157 if (b == NULL) 158 return (USBD_NOMEM); 159 160 b->tag = tag; 161 b->size = size; 162 b->align = align; 163 error = bus_dmamem_alloc(tag, b->size, align, 0, 164 b->segs, __arraycount(b->segs), 165 &b->nsegs, BUS_DMA_NOWAIT); 166 if (error) 167 goto free0; 168 169 error = bus_dmamem_map(tag, b->segs, b->nsegs, b->size, 170 &b->kaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT); 171 if (error) 172 goto free1; 173 174 error = bus_dmamap_create(tag, b->size, 1, b->size, 175 0, BUS_DMA_NOWAIT, &b->map); 176 if (error) 177 goto unmap; 178 179 error = bus_dmamap_load(tag, b->map, b->kaddr, b->size, NULL, 180 BUS_DMA_NOWAIT); 181 if (error) 182 goto destroy; 183 184 *dmap = b; 185 #ifdef USB_FRAG_DMA_WORKAROUND 186 memset(b->kaddr, 0, b->size); 187 #endif 188 return (USBD_NORMAL_COMPLETION); 189 190 destroy: 191 bus_dmamap_destroy(tag, b->map); 192 unmap: 193 bus_dmamem_unmap(tag, b->kaddr, b->size); 194 free1: 195 bus_dmamem_free(tag, b->segs, b->nsegs); 196 free0: 197 free(b, M_USB); 198 return (USBD_NOMEM); 199 } 200 201 #if 0 202 void 203 usb_block_real_freemem(usb_dma_block_t *b) 204 { 205 #ifdef DIAGNOSTIC 206 if (cpu_intr_p()) { 207 printf("usb_block_real_freemem: in interrupt context\n"); 208 return; 209 } 210 #endif 211 bus_dmamap_unload(b->tag, b->map); 212 bus_dmamap_destroy(b->tag, b->map); 213 bus_dmamem_unmap(b->tag, b->kaddr, b->size); 214 bus_dmamem_free(b->tag, b->segs, b->nsegs); 215 free(p, M_USB); 216 } 217 #endif 218 219 #ifdef DEBUG 220 static bool 221 usb_valid_block_p(usb_dma_block_t *b, struct usb_dma_block_qh *qh) 222 { 223 usb_dma_block_t *xb; 224 LIST_FOREACH(xb, qh, next) { 225 if (xb == b) 226 return true; 227 } 228 return false; 229 } 230 #endif 231 232 /* 233 * Do not free the memory unconditionally since we might be called 234 * from an interrupt context and that is BAD. 235 * XXX when should we really free? 236 */ 237 Static void 238 usb_block_freemem(usb_dma_block_t *b) 239 { 240 241 KASSERT(mutex_owned(&usb_blk_lock)); 242 243 DPRINTFN(6, ("usb_block_freemem: size=%zu\n", b->size)); 244 #ifdef DEBUG 245 LIST_REMOVE(b, next); 246 #endif 247 LIST_INSERT_HEAD(&usb_blk_freelist, b, next); 248 usb_blk_nfree++; 249 } 250 251 usbd_status 252 usb_allocmem(usbd_bus_handle bus, size_t size, size_t align, usb_dma_t *p) 253 { 254 bus_dma_tag_t tag = bus->dmatag; 255 usbd_status err; 256 struct usb_frag_dma *f; 257 usb_dma_block_t *b; 258 int i; 259 static ONCE_DECL(init_control); 260 261 RUN_ONCE(&init_control, usb_mem_init); 262 263 /* If the request is large then just use a full block. */ 264 if (size > USB_MEM_SMALL || align > USB_MEM_SMALL) { 265 DPRINTFN(1, ("usb_allocmem: large alloc %d\n", (int)size)); 266 size = (size + USB_MEM_BLOCK - 1) & ~(USB_MEM_BLOCK - 1); 267 mutex_enter(&usb_blk_lock); 268 err = usb_block_allocmem(tag, size, align, &p->block); 269 if (!err) { 270 #ifdef DEBUG 271 LIST_INSERT_HEAD(&usb_blk_fulllist, p->block, next); 272 #endif 273 p->block->flags = USB_DMA_FULLBLOCK; 274 p->offs = 0; 275 } 276 mutex_exit(&usb_blk_lock); 277 return (err); 278 } 279 280 mutex_enter(&usb_blk_lock); 281 /* Check for free fragments. */ 282 LIST_FOREACH(f, &usb_frag_freelist, next) { 283 KDASSERTMSG(usb_valid_block_p(f->block, &usb_blk_fraglist), 284 "%s: usb frag %p: unknown block pointer %p", 285 __func__, f, f->block); 286 if (f->block->tag == tag) 287 break; 288 } 289 if (f == NULL) { 290 DPRINTFN(1, ("usb_allocmem: adding fragments\n")); 291 err = usb_block_allocmem(tag, USB_MEM_BLOCK, USB_MEM_SMALL,&b); 292 if (err) { 293 mutex_exit(&usb_blk_lock); 294 return (err); 295 } 296 #ifdef DEBUG 297 LIST_INSERT_HEAD(&usb_blk_fraglist, b, next); 298 #endif 299 b->flags = 0; 300 for (i = 0; i < USB_MEM_BLOCK; i += USB_MEM_SMALL) { 301 f = (struct usb_frag_dma *)((char *)b->kaddr + i); 302 f->block = b; 303 f->offs = i; 304 LIST_INSERT_HEAD(&usb_frag_freelist, f, next); 305 #ifdef USB_FRAG_DMA_WORKAROUND 306 i += 1 * USB_MEM_SMALL; 307 #endif 308 } 309 f = LIST_FIRST(&usb_frag_freelist); 310 } 311 p->block = f->block; 312 p->offs = f->offs; 313 #ifdef USB_FRAG_DMA_WORKAROUND 314 p->offs += USB_MEM_SMALL; 315 #endif 316 p->block->flags &= ~USB_DMA_RESERVE; 317 LIST_REMOVE(f, next); 318 mutex_exit(&usb_blk_lock); 319 DPRINTFN(5, ("usb_allocmem: use frag=%p size=%d\n", f, (int)size)); 320 return (USBD_NORMAL_COMPLETION); 321 } 322 323 void 324 usb_freemem(usbd_bus_handle bus, usb_dma_t *p) 325 { 326 struct usb_frag_dma *f; 327 328 mutex_enter(&usb_blk_lock); 329 if (p->block->flags & USB_DMA_FULLBLOCK) { 330 KDASSERTMSG(usb_valid_block_p(p->block, &usb_blk_fulllist), 331 "%s: dma %p: invalid block pointer %p", 332 __func__, p, p->block); 333 DPRINTFN(1, ("usb_freemem: large free\n")); 334 usb_block_freemem(p->block); 335 mutex_exit(&usb_blk_lock); 336 return; 337 } 338 KDASSERTMSG(usb_valid_block_p(p->block, &usb_blk_fraglist), 339 "%s: dma %p: invalid block pointer %p", 340 __func__, p, p->block); 341 //usb_syncmem(p, 0, USB_MEM_SMALL, BUS_DMASYNC_POSTREAD); 342 f = KERNADDR(p, 0); 343 #ifdef USB_FRAG_DMA_WORKAROUND 344 f = (void *)((uintptr_t)f - USB_MEM_SMALL); 345 #endif 346 f->block = p->block; 347 f->offs = p->offs; 348 #ifdef USB_FRAG_DMA_WORKAROUND 349 f->offs -= USB_MEM_SMALL; 350 #endif 351 LIST_INSERT_HEAD(&usb_frag_freelist, f, next); 352 mutex_exit(&usb_blk_lock); 353 DPRINTFN(5, ("usb_freemem: frag=%p\n", f)); 354 } 355 356 void 357 usb_syncmem(usb_dma_t *p, bus_addr_t offset, bus_size_t len, int ops) 358 { 359 bus_dmamap_sync(p->block->tag, p->block->map, p->offs + offset, 360 len, ops); 361 } 362 363 364 usbd_status 365 usb_reserve_allocm(struct usb_dma_reserve *rs, usb_dma_t *dma, u_int32_t size) 366 { 367 int error; 368 u_long start; 369 bus_addr_t baddr; 370 371 if (rs->vaddr == 0 || size > USB_MEM_RESERVE) 372 return USBD_NOMEM; 373 374 dma->block = malloc(sizeof *dma->block, M_USB, M_ZERO | M_NOWAIT); 375 if (dma->block == NULL) 376 return USBD_NOMEM; 377 378 error = extent_alloc(rs->extent, size, PAGE_SIZE, 0, 379 EX_NOWAIT, &start); 380 381 if (error != 0) { 382 aprint_error_dev(rs->dv, 383 "usb_reserve_allocm of size %u failed (error %d)\n", 384 size, error); 385 return USBD_NOMEM; 386 } 387 388 baddr = start; 389 dma->offs = baddr - rs->paddr; 390 dma->block->flags = USB_DMA_RESERVE; 391 dma->block->align = PAGE_SIZE; 392 dma->block->size = size; 393 dma->block->nsegs = 1; 394 /* XXX segs appears to be unused */ 395 dma->block->segs[0] = rs->map->dm_segs[0]; 396 dma->block->map = rs->map; 397 dma->block->kaddr = rs->vaddr; 398 dma->block->tag = rs->dtag; 399 400 return USBD_NORMAL_COMPLETION; 401 } 402 403 void 404 usb_reserve_freem(struct usb_dma_reserve *rs, usb_dma_t *dma) 405 { 406 int error; 407 408 error = extent_free(rs->extent, 409 (u_long)(rs->paddr + dma->offs), dma->block->size, 0); 410 free(dma->block, M_USB); 411 } 412 413 int 414 usb_setup_reserve(device_t dv, struct usb_dma_reserve *rs, bus_dma_tag_t dtag, 415 size_t size) 416 { 417 int error, nseg; 418 bus_dma_segment_t seg; 419 420 rs->dtag = dtag; 421 rs->size = size; 422 rs->dv = dv; 423 424 error = bus_dmamem_alloc(dtag, USB_MEM_RESERVE, PAGE_SIZE, 0, 425 &seg, 1, &nseg, BUS_DMA_NOWAIT); 426 if (error != 0) 427 return error; 428 429 error = bus_dmamem_map(dtag, &seg, nseg, USB_MEM_RESERVE, 430 &rs->vaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT); 431 if (error != 0) 432 goto freeit; 433 434 error = bus_dmamap_create(dtag, USB_MEM_RESERVE, 1, 435 USB_MEM_RESERVE, 0, BUS_DMA_NOWAIT, &rs->map); 436 if (error != 0) 437 goto unmap; 438 439 error = bus_dmamap_load(dtag, rs->map, rs->vaddr, USB_MEM_RESERVE, 440 NULL, BUS_DMA_NOWAIT); 441 if (error != 0) 442 goto destroy; 443 444 rs->paddr = rs->map->dm_segs[0].ds_addr; 445 rs->extent = extent_create(device_xname(dv), (u_long)rs->paddr, 446 (u_long)(rs->paddr + USB_MEM_RESERVE - 1), 0, 0, 0); 447 if (rs->extent == NULL) { 448 rs->vaddr = 0; 449 return ENOMEM; 450 } 451 452 return 0; 453 454 destroy: 455 bus_dmamap_destroy(dtag, rs->map); 456 unmap: 457 bus_dmamem_unmap(dtag, rs->vaddr, size); 458 freeit: 459 bus_dmamem_free(dtag, &seg, nseg); 460 461 rs->vaddr = 0; 462 463 return error; 464 } 465