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