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