Home | History | Annotate | Line # | Download | only in usb
usb_mem.c revision 1.1
      1 /*	$NetBSD: usb_mem.c,v 1.1 1998/07/24 21:09:08 augustss Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Author: Lennart Augustsson <augustss (at) carlstedt.se>
      8  *         Carlstedt Research & Technology
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * USB DMA memory allocation.
     41  * We need to allocate a lot of small (many 8 byte, some larger)
     42  * memory blocks that can be used for DMA.  Using the bus_dma
     43  * routines directly would uncur large overheads in space and time.
     44  */
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/kernel.h>
     49 #include <sys/malloc.h>
     50 #include <sys/queue.h>
     51 
     52 #include <machine/bus.h>
     53 
     54 #include <dev/usb/usb.h>
     55 #include <dev/usb/usbdi.h>
     56 #include <dev/usb/usb_mem.h>
     57 
     58 #ifdef USB_DEBUG
     59 #define DPRINTF(x)	if (usbdebug) printf x
     60 #define DPRINTFN(n,x)	if (usbdebug>(n)) printf x
     61 extern int usbdebug;
     62 #else
     63 #define DPRINTF(x)
     64 #define DPRINTFN(n,x)
     65 #endif
     66 
     67 #define USB_MEM_SMALL 64
     68 #define USB_MEM_CHUNKS 64
     69 #define USB_MEM_BLOCK (USB_MEM_SMALL * USB_MEM_CHUNKS)
     70 
     71 /* This struct is overlayed on free fragments. */
     72 struct usb_frag_dma {
     73 	usb_dma_block_t *block;
     74 	u_int offs;
     75 	LIST_ENTRY(usb_frag_dma) next;
     76 };
     77 
     78 usbd_status	usb_block_allocmem
     79 	__P((bus_dma_tag_t, size_t, size_t, usb_dma_block_t **));
     80 void		usb_block_real_freemem  __P((usb_dma_block_t *));
     81 void		usb_block_freemem  __P((usb_dma_block_t *));
     82 
     83 LIST_HEAD(, usb_block_dma) usb_blk_freelist =
     84 	LIST_HEAD_INITIALIZER(usb_blk_freelist);
     85 /* XXX should have different free list for different tags */
     86 LIST_HEAD(, usb_frag_dma) usb_frag_freelist =
     87 	LIST_HEAD_INITIALIZER(usb_frag_freelist);
     88 
     89 usbd_status
     90 usb_block_allocmem(tag, size, align, dmap)
     91 	bus_dma_tag_t tag;
     92 	size_t size;
     93 	size_t align;
     94         usb_dma_block_t **dmap;
     95 {
     96 	int error;
     97         usb_dma_block_t *p;
     98 
     99 	DPRINTFN(5, ("usb_block_allocmem: size=%d align=%d\n", size, align));
    100 
    101 	/* First check the free list. */
    102 	for (p = LIST_FIRST(&usb_blk_freelist); p; p = LIST_NEXT(p, next)) {
    103 		if (p->tag == tag && p->size >= size && p->align >= align) {
    104 			LIST_REMOVE(p, next);
    105 			*dmap = p;
    106 			DPRINTFN(6, ("usb_block_allocmem: free list size=%d\n",
    107 				     p->size));
    108 			return (USBD_NORMAL_COMPLETION);
    109 		}
    110 	}
    111 
    112 	DPRINTFN(6, ("usb_block_allocmem: no free\n"));
    113 	p = malloc(sizeof *p, M_USB, M_NOWAIT);
    114 	if (p == 0)
    115 		return (USBD_NOMEM);
    116 	*dmap = p;
    117 
    118 	p->tag = tag;
    119 	p->size = size;
    120 	p->align = align;
    121 	error = bus_dmamem_alloc(tag, p->size, align, 0,
    122 				 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
    123 				 &p->nsegs, BUS_DMA_NOWAIT);
    124 	if (error)
    125 		return (USBD_NOMEM);
    126 
    127 	error = bus_dmamem_map(tag, p->segs, p->nsegs, p->size,
    128 			       &p->kaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    129 	if (error)
    130 		goto free;
    131 
    132 	error = bus_dmamap_create(tag, p->size, 1, p->size,
    133 				  0, BUS_DMA_NOWAIT, &p->map);
    134 	if (error)
    135 		goto unmap;
    136 
    137 	error = bus_dmamap_load(tag, p->map, p->kaddr,p->size, NULL,
    138 				BUS_DMA_NOWAIT);
    139 	if (error)
    140 		goto destroy;
    141 	return 0;
    142 
    143 destroy:
    144 	bus_dmamap_destroy(tag, p->map);
    145 unmap:
    146 	bus_dmamem_unmap(tag, p->kaddr, p->size);
    147 free:
    148 	bus_dmamem_free(tag, p->segs, p->nsegs);
    149 	return (USBD_NOMEM);
    150 }
    151 
    152 void
    153 usb_block_real_freemem(p)
    154         usb_dma_block_t *p;
    155 {
    156 	bus_dmamap_unload(p->tag, p->map);
    157 	bus_dmamap_destroy(p->tag, p->map);
    158 	bus_dmamem_unmap(p->tag, p->kaddr, p->size);
    159 	bus_dmamem_free(p->tag, p->segs, p->nsegs);
    160 	free(p, M_USB);
    161 }
    162 
    163 /*
    164  * Do not free the memory unconditionally since we might be called
    165  * from an interrupt context and that is BAD.
    166  * XXX when you we really free?
    167  */
    168 void
    169 usb_block_freemem(p)
    170         usb_dma_block_t *p;
    171 {
    172 	DPRINTFN(6, ("usb_block_freemem: size=%d\n", p->size));
    173 	LIST_INSERT_HEAD(&usb_blk_freelist, p, next);
    174 }
    175 
    176 usbd_status
    177 usb_allocmem(tag, size, align, p)
    178 	bus_dma_tag_t tag;
    179 	size_t size;
    180 	size_t align;
    181         usb_dma_t *p;
    182 {
    183 	usbd_status r;
    184 	struct usb_frag_dma *f;
    185 	usb_dma_block_t *b;
    186 	int i;
    187 
    188 	/* If the request is large then just use a full block. */
    189 	if (size > USB_MEM_SMALL || align > USB_MEM_SMALL) {
    190 		DPRINTFN(1, ("usb_allocmem: large alloc %d\n", (int)size));
    191 		size = (size + USB_MEM_BLOCK - 1) & ~(USB_MEM_BLOCK - 1);
    192 		r = usb_block_allocmem(tag, size, align, &p->block);
    193 		if (r == USBD_NORMAL_COMPLETION) {
    194 			p->block->fullblock = 1;
    195 			p->offs = 0;
    196 		}
    197 		return (r);
    198 	}
    199 
    200 	/* Check for free fragments. */
    201 	for (f = LIST_FIRST(&usb_frag_freelist); f; f = LIST_NEXT(f, next))
    202 		if (f->block->tag == tag)
    203 			break;
    204 	if (!f) {
    205 		DPRINTFN(1, ("usb_allocmem: adding fragments\n"));
    206 		r = usb_block_allocmem(tag, USB_MEM_BLOCK, USB_MEM_SMALL, &b);
    207 		for (i = 0; i < USB_MEM_BLOCK; i += USB_MEM_SMALL) {
    208 			f = (struct usb_frag_dma *)(b->kaddr + i);
    209 			f->block = b;
    210 			f->offs = i;
    211 			LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
    212 		}
    213 		f = LIST_FIRST(&usb_frag_freelist);
    214 	}
    215 	p->block = f->block;
    216 	p->offs = f->offs;
    217 	LIST_REMOVE(f, next);
    218 	DPRINTFN(5, ("usb_allocmem: use frag=%p size=%d\n", f, (int)size));
    219 	return (USBD_NORMAL_COMPLETION);
    220 }
    221 
    222 void
    223 usb_freemem(tag, p)
    224 	bus_dma_tag_t tag;
    225         usb_dma_t *p;
    226 {
    227 	struct usb_frag_dma *f;
    228 
    229 	if (p->block->fullblock) {
    230 		usb_block_freemem(p->block);
    231 		return;
    232 	}
    233 	f = KERNADDR(p);
    234 	f->block = p->block;
    235 	f->offs = p->offs;
    236 	LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
    237 	DPRINTFN(5, ("usb_freemem: frag=%p\n", f));
    238 }
    239