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