Home | History | Annotate | Line # | Download | only in usb
usb_mem.c revision 1.55
      1 /*	$NetBSD: usb_mem.c,v 1.55 2013/01/07 15:07:41 prlw1 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 (at) 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.55 2013/01/07 15:07:41 prlw1 Exp $");
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/kernel.h>
     46 #include <sys/kmem.h>
     47 #include <sys/queue.h>
     48 #include <sys/device.h>		/* for usbdivar.h */
     49 #include <sys/bus.h>
     50 #include <sys/cpu.h>
     51 #include <sys/once.h>
     52 
     53 #include <sys/extent.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) printf x
     66 #define DPRINTFN(n,x)	if (usbdebug>(n)) printf x
     67 extern 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(bus_dma_tag_t, size_t, size_t,
     85 					   usb_dma_block_t **, bool);
     86 Static void		usb_block_freemem(usb_dma_block_t *);
     87 
     88 LIST_HEAD(usb_dma_block_qh, usb_dma_block);
     89 Static struct usb_dma_block_qh usb_blk_freelist =
     90 	LIST_HEAD_INITIALIZER(usb_blk_freelist);
     91 kmutex_t usb_blk_lock;
     92 
     93 #ifdef DEBUG
     94 Static struct usb_dma_block_qh usb_blk_fraglist =
     95 	LIST_HEAD_INITIALIZER(usb_blk_fraglist);
     96 Static struct usb_dma_block_qh usb_blk_fulllist =
     97 	LIST_HEAD_INITIALIZER(usb_blk_fulllist);
     98 #endif
     99 Static u_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 int usb_mem_init(void);
    105 
    106 Static int
    107 usb_mem_init(void)
    108 {
    109 
    110 	mutex_init(&usb_blk_lock, MUTEX_DEFAULT, IPL_NONE);
    111 	return 0;
    112 }
    113 
    114 Static usbd_status
    115 usb_block_allocmem(bus_dma_tag_t tag, size_t size, size_t align,
    116 		   usb_dma_block_t **dmap, bool multiseg)
    117 {
    118 	usb_dma_block_t *b;
    119 	int error;
    120 
    121 	DPRINTFN(5, ("usb_block_allocmem: size=%zu align=%zu\n", size, align));
    122 
    123 	if (size == 0) {
    124 #ifdef DIAGNOSTIC
    125 		printf("usb_block_allocmem: called with size==0\n");
    126 #endif
    127 		return USBD_INVAL;
    128 	}
    129 
    130 #ifdef DIAGNOSTIC
    131 	if (cpu_intr_p()) {
    132 		printf("usb_block_allocmem: in interrupt context, size=%lu\n",
    133 		    (unsigned long) size);
    134 	}
    135 #endif
    136 
    137 	KASSERT(mutex_owned(&usb_blk_lock));
    138 
    139 	/* First check the free list. */
    140 	LIST_FOREACH(b, &usb_blk_freelist, next) {
    141 		/* Don't allocate multiple segments to unwilling callers */
    142 		if (b->nsegs != 1 && !multiseg)
    143 			continue;
    144 		if (b->tag == tag && b->size >= size && b->align >= align) {
    145 			LIST_REMOVE(b, next);
    146 			usb_blk_nfree--;
    147 			*dmap = b;
    148 			DPRINTFN(6,("usb_block_allocmem: free list size=%zu\n",
    149 			    b->size));
    150 			return (USBD_NORMAL_COMPLETION);
    151 		}
    152 	}
    153 
    154 #ifdef DIAGNOSTIC
    155 	if (cpu_intr_p()) {
    156 		printf("usb_block_allocmem: in interrupt context, failed\n");
    157 		return (USBD_NOMEM);
    158 	}
    159 #endif
    160 
    161 	DPRINTFN(6, ("usb_block_allocmem: no free\n"));
    162 	b = kmem_zalloc(sizeof *b, KM_SLEEP);
    163 	if (b == NULL)
    164 		return (USBD_NOMEM);
    165 
    166 	b->tag = tag;
    167 	b->size = size;
    168 	b->align = align;
    169 
    170 	b->nsegs = (size + (PAGE_SIZE-1)) / PAGE_SIZE;
    171 	if (!multiseg)
    172 		/* Caller wants one segment */
    173 		b->nsegs = 1;
    174 
    175 	b->segs = kmem_alloc(b->nsegs * sizeof(*b->segs), KM_SLEEP);
    176 	if (b->segs == NULL) {
    177 		kmem_free(b, sizeof *b);
    178 		return USBD_NOMEM;
    179 	}
    180 
    181 	error = bus_dmamem_alloc(tag, b->size, align, 0,
    182 				 b->segs, b->nsegs,
    183 				 &b->nsegs, BUS_DMA_NOWAIT);
    184 	if (error)
    185 		goto free0;
    186 
    187 	error = bus_dmamem_map(tag, b->segs, b->nsegs, b->size,
    188 			       &b->kaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    189 	if (error)
    190 		goto free1;
    191 
    192 	error = bus_dmamap_create(tag, b->size, b->nsegs, b->size,
    193 				  0, BUS_DMA_NOWAIT, &b->map);
    194 	if (error)
    195 		goto unmap;
    196 
    197 	error = bus_dmamap_load(tag, b->map, b->kaddr, b->size, NULL,
    198 				BUS_DMA_NOWAIT);
    199 	if (error)
    200 		goto destroy;
    201 
    202 	*dmap = b;
    203 #ifdef USB_FRAG_DMA_WORKAROUND
    204 	memset(b->kaddr, 0, b->size);
    205 #endif
    206 
    207 	return (USBD_NORMAL_COMPLETION);
    208 
    209  destroy:
    210 	bus_dmamap_destroy(tag, b->map);
    211  unmap:
    212 	bus_dmamem_unmap(tag, b->kaddr, b->size);
    213  free1:
    214 	bus_dmamem_free(tag, b->segs, b->nsegs);
    215  free0:
    216 	kmem_free(b->segs, b->nsegs * sizeof(*b->segs));
    217 	kmem_free(b, sizeof *b);
    218 	return (USBD_NOMEM);
    219 }
    220 
    221 #if 0
    222 void
    223 usb_block_real_freemem(usb_dma_block_t *b)
    224 {
    225 #ifdef DIAGNOSTIC
    226 	if (cpu_intr_p()) {
    227 		printf("usb_block_real_freemem: in interrupt context\n");
    228 		return;
    229 	}
    230 #endif
    231 	bus_dmamap_unload(b->tag, b->map);
    232 	bus_dmamap_destroy(b->tag, b->map);
    233 	bus_dmamem_unmap(b->tag, b->kaddr, b->size);
    234 	bus_dmamem_free(b->tag, b->segs, b->nsegs);
    235 	kmem_free(b->segs, b->nsegs * sizeof(*b->segs));
    236 	kmem_free(b, sizeof *b);
    237 }
    238 #endif
    239 
    240 #ifdef DEBUG
    241 static bool
    242 usb_valid_block_p(usb_dma_block_t *b, struct usb_dma_block_qh *qh)
    243 {
    244 	usb_dma_block_t *xb;
    245 	LIST_FOREACH(xb, qh, next) {
    246 		if (xb == b)
    247 			return true;
    248 	}
    249 	return false;
    250 }
    251 #endif
    252 
    253 /*
    254  * Do not free the memory unconditionally since we might be called
    255  * from an interrupt context and that is BAD.
    256  * XXX when should we really free?
    257  */
    258 Static void
    259 usb_block_freemem(usb_dma_block_t *b)
    260 {
    261 
    262 	KASSERT(mutex_owned(&usb_blk_lock));
    263 
    264 	DPRINTFN(6, ("usb_block_freemem: size=%zu\n", b->size));
    265 #ifdef DEBUG
    266 	LIST_REMOVE(b, next);
    267 #endif
    268 	LIST_INSERT_HEAD(&usb_blk_freelist, b, next);
    269 	usb_blk_nfree++;
    270 }
    271 
    272 usbd_status
    273 usb_allocmem(usbd_bus_handle bus, size_t size, size_t align, usb_dma_t *p)
    274 {
    275 	return usb_allocmem_flags(bus, size, align, p, 0);
    276 }
    277 
    278 usbd_status
    279 usb_allocmem_flags(usbd_bus_handle bus, size_t size, size_t align, usb_dma_t *p,
    280 		   int flags)
    281 {
    282 	bus_dma_tag_t tag = bus->dmatag;
    283 	usbd_status err;
    284 	struct usb_frag_dma *f;
    285 	usb_dma_block_t *b;
    286 	int i;
    287 	static ONCE_DECL(init_control);
    288 	bool frag;
    289 
    290 	RUN_ONCE(&init_control, usb_mem_init);
    291 
    292 	frag = (flags & USBMALLOC_MULTISEG);
    293 
    294 	/* If the request is large then just use a full block. */
    295 	if (size > USB_MEM_SMALL || align > USB_MEM_SMALL) {
    296 		DPRINTFN(1, ("usb_allocmem: large alloc %d\n", (int)size));
    297 		size = (size + USB_MEM_BLOCK - 1) & ~(USB_MEM_BLOCK - 1);
    298 		mutex_enter(&usb_blk_lock);
    299 		err = usb_block_allocmem(tag, size, align, &p->block, frag);
    300 		if (!err) {
    301 #ifdef DEBUG
    302 			LIST_INSERT_HEAD(&usb_blk_fulllist, p->block, next);
    303 #endif
    304 			p->block->flags = USB_DMA_FULLBLOCK;
    305 			p->offs = 0;
    306 		}
    307 		mutex_exit(&usb_blk_lock);
    308 		return (err);
    309 	}
    310 
    311 	mutex_enter(&usb_blk_lock);
    312 	/* Check for free fragments. */
    313 	LIST_FOREACH(f, &usb_frag_freelist, next) {
    314 		KDASSERTMSG(usb_valid_block_p(f->block, &usb_blk_fraglist),
    315 		    "%s: usb frag %p: unknown block pointer %p",
    316 		     __func__, f, f->block);
    317 		if (f->block->tag == tag)
    318 			break;
    319 	}
    320 	if (f == NULL) {
    321 		DPRINTFN(1, ("usb_allocmem: adding fragments\n"));
    322 		err = usb_block_allocmem(tag, USB_MEM_BLOCK, USB_MEM_SMALL, &b,
    323 					 false);
    324 		if (err) {
    325 			mutex_exit(&usb_blk_lock);
    326 			return (err);
    327 		}
    328 #ifdef DEBUG
    329 		LIST_INSERT_HEAD(&usb_blk_fraglist, b, next);
    330 #endif
    331 		b->flags = 0;
    332 		for (i = 0; i < USB_MEM_BLOCK; i += USB_MEM_SMALL) {
    333 			f = (struct usb_frag_dma *)((char *)b->kaddr + i);
    334 			f->block = b;
    335 			f->offs = i;
    336 			LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
    337 #ifdef USB_FRAG_DMA_WORKAROUND
    338 			i += 1 * USB_MEM_SMALL;
    339 #endif
    340 		}
    341 		f = LIST_FIRST(&usb_frag_freelist);
    342 	}
    343 	p->block = f->block;
    344 	p->offs = f->offs;
    345 #ifdef USB_FRAG_DMA_WORKAROUND
    346 	p->offs += USB_MEM_SMALL;
    347 #endif
    348 	p->block->flags &= ~USB_DMA_RESERVE;
    349 	LIST_REMOVE(f, next);
    350 	mutex_exit(&usb_blk_lock);
    351 	DPRINTFN(5, ("usb_allocmem: use frag=%p size=%d\n", f, (int)size));
    352 
    353 	return (USBD_NORMAL_COMPLETION);
    354 }
    355 
    356 void
    357 usb_freemem(usbd_bus_handle bus, usb_dma_t *p)
    358 {
    359 	struct usb_frag_dma *f;
    360 
    361 	mutex_enter(&usb_blk_lock);
    362 	if (p->block->flags & USB_DMA_FULLBLOCK) {
    363 		KDASSERTMSG(usb_valid_block_p(p->block, &usb_blk_fulllist),
    364 		    "%s: dma %p: invalid block pointer %p",
    365 		     __func__, p, p->block);
    366 		DPRINTFN(1, ("usb_freemem: large free\n"));
    367 		usb_block_freemem(p->block);
    368 		mutex_exit(&usb_blk_lock);
    369 		return;
    370 	}
    371 	KDASSERTMSG(usb_valid_block_p(p->block, &usb_blk_fraglist),
    372 	    "%s: dma %p: invalid block pointer %p",
    373 	     __func__, p, p->block);
    374 	//usb_syncmem(p, 0, USB_MEM_SMALL, BUS_DMASYNC_POSTREAD);
    375 	f = KERNADDR(p, 0);
    376 #ifdef USB_FRAG_DMA_WORKAROUND
    377 	f = (void *)((uintptr_t)f - USB_MEM_SMALL);
    378 #endif
    379 	f->block = p->block;
    380 	f->offs = p->offs;
    381 #ifdef USB_FRAG_DMA_WORKAROUND
    382 	f->offs -= USB_MEM_SMALL;
    383 #endif
    384 	LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
    385 	mutex_exit(&usb_blk_lock);
    386 	DPRINTFN(5, ("usb_freemem: frag=%p\n", f));
    387 }
    388 
    389 bus_addr_t
    390 usb_dmaaddr(usb_dma_t *dma, unsigned int offset)
    391 {
    392 	unsigned int i;
    393 	bus_size_t seg_offs;
    394 
    395 	offset += dma->offs;
    396 
    397 	KASSERT(offset < dma->block->size);
    398 
    399 	if (dma->block->nsegs == 1) {
    400 		KASSERT(dma->block->map->dm_segs[0].ds_len > offset);
    401 		return dma->block->map->dm_segs[0].ds_addr + offset;
    402 	}
    403 
    404 	/* Search for a bus_segment_t corresponding to this offset. With no
    405 	 * record of the offset in the map to a particular dma_segment_t, we
    406 	 * have to iterate from the start of the list each time. Could be
    407 	 * improved */
    408 	seg_offs = 0;
    409 	for (i = 0; i < dma->block->nsegs; i++) {
    410 		if (seg_offs + dma->block->map->dm_segs[i].ds_len > offset)
    411 			break;
    412 
    413 		seg_offs += dma->block->map->dm_segs[i].ds_len;
    414 	}
    415 
    416 	KASSERT(i != dma->block->nsegs);
    417 	offset -= seg_offs;
    418 	return dma->block->map->dm_segs[i].ds_addr + offset;
    419 }
    420 
    421 void
    422 usb_syncmem(usb_dma_t *p, bus_addr_t offset, bus_size_t len, int ops)
    423 {
    424 	bus_dmamap_sync(p->block->tag, p->block->map, p->offs + offset,
    425 	    len, ops);
    426 }
    427 
    428 
    429 usbd_status
    430 usb_reserve_allocm(struct usb_dma_reserve *rs, usb_dma_t *dma, u_int32_t size)
    431 {
    432 	int error;
    433 	u_long start;
    434 	bus_addr_t baddr;
    435 
    436 	if (rs->vaddr == 0 || size > USB_MEM_RESERVE)
    437 		return USBD_NOMEM;
    438 
    439 	dma->block = kmem_zalloc(sizeof *dma->block, KM_SLEEP);
    440 	if (dma->block == NULL)
    441 		return USBD_NOMEM;
    442 
    443 	error = extent_alloc(rs->extent, size, PAGE_SIZE, 0,
    444 	    EX_NOWAIT, &start);
    445 
    446 	if (error != 0) {
    447 		aprint_error_dev(rs->dv,
    448 		    "usb_reserve_allocm of size %u failed (error %d)\n",
    449 		    size, error);
    450 		return USBD_NOMEM;
    451 	}
    452 
    453 	baddr = start;
    454 	dma->offs = baddr - rs->paddr;
    455 	dma->block->flags = USB_DMA_RESERVE;
    456 	dma->block->align = PAGE_SIZE;
    457 	dma->block->size = size;
    458 	dma->block->nsegs = 1;
    459 	/* XXX segs appears to be unused */
    460 	dma->block->segs[0] = rs->map->dm_segs[0];
    461 	dma->block->map = rs->map;
    462 	dma->block->kaddr = rs->vaddr;
    463 	dma->block->tag = rs->dtag;
    464 
    465 	return USBD_NORMAL_COMPLETION;
    466 }
    467 
    468 void
    469 usb_reserve_freem(struct usb_dma_reserve *rs, usb_dma_t *dma)
    470 {
    471 	int error;
    472 
    473 	error = extent_free(rs->extent,
    474 	    (u_long)(rs->paddr + dma->offs), dma->block->size, 0);
    475 	/* XXXPW correct that segs[0] is not used? */
    476 	kmem_free(dma->block, dma->block->size);
    477 }
    478 
    479 int
    480 usb_setup_reserve(device_t dv, struct usb_dma_reserve *rs, bus_dma_tag_t dtag,
    481 		  size_t size)
    482 {
    483 	int error, nseg;
    484 	bus_dma_segment_t seg;
    485 
    486 	rs->dtag = dtag;
    487 	rs->size = size;
    488 	rs->dv = dv;
    489 
    490 	error = bus_dmamem_alloc(dtag, USB_MEM_RESERVE, PAGE_SIZE, 0,
    491 	    &seg, 1, &nseg, BUS_DMA_NOWAIT);
    492 	if (error != 0)
    493 		return error;
    494 
    495 	error = bus_dmamem_map(dtag, &seg, nseg, USB_MEM_RESERVE,
    496 	    &rs->vaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    497 	if (error != 0)
    498 		goto freeit;
    499 
    500 	error = bus_dmamap_create(dtag, USB_MEM_RESERVE, 1,
    501 	    USB_MEM_RESERVE, 0, BUS_DMA_NOWAIT, &rs->map);
    502 	if (error != 0)
    503 		goto unmap;
    504 
    505 	error = bus_dmamap_load(dtag, rs->map, rs->vaddr, USB_MEM_RESERVE,
    506 	    NULL, BUS_DMA_NOWAIT);
    507 	if (error != 0)
    508 		goto destroy;
    509 
    510 	rs->paddr = rs->map->dm_segs[0].ds_addr;
    511 	rs->extent = extent_create(device_xname(dv), (u_long)rs->paddr,
    512 	    (u_long)(rs->paddr + USB_MEM_RESERVE - 1), 0, 0, 0);
    513 	if (rs->extent == NULL) {
    514 		rs->vaddr = 0;
    515 		return ENOMEM;
    516 	}
    517 
    518 	return 0;
    519 
    520  destroy:
    521 	bus_dmamap_destroy(dtag, rs->map);
    522  unmap:
    523 	bus_dmamem_unmap(dtag, rs->vaddr, size);
    524  freeit:
    525 	bus_dmamem_free(dtag, &seg, nseg);
    526 
    527 	rs->vaddr = 0;
    528 
    529 	return error;
    530 }
    531