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