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