xref: /netbsd-src/sys/dev/usb/usb_mem.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: usb_mem.c,v 1.29 2005/12/11 12:24:01 christos 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@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/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: usb_mem.c,v 1.29 2005/12/11 12:24:01 christos Exp $");
49 
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/malloc.h>
54 #include <sys/queue.h>
55 #include <sys/device.h>		/* for usbdivar.h */
56 #include <machine/bus.h>
57 
58 #ifdef __NetBSD__
59 #include <sys/extent.h>
60 #include <uvm/uvm_extern.h>
61 #endif
62 
63 #ifdef DIAGNOSTIC
64 #include <sys/proc.h>
65 #endif
66 
67 #include <dev/usb/usb.h>
68 #include <dev/usb/usbdi.h>
69 #include <dev/usb/usbdivar.h>	/* just for usb_dma_t */
70 #include <dev/usb/usb_mem.h>
71 
72 #ifdef USB_DEBUG
73 #define DPRINTF(x)	if (usbdebug) logprintf x
74 #define DPRINTFN(n,x)	if (usbdebug>(n)) logprintf x
75 extern int usbdebug;
76 #else
77 #define DPRINTF(x)
78 #define DPRINTFN(n,x)
79 #endif
80 
81 MALLOC_DEFINE(M_USB, "USB", "USB misc. memory");
82 MALLOC_DEFINE(M_USBDEV, "USB device", "USB device driver");
83 MALLOC_DEFINE(M_USBHC, "USB HC", "USB host controller");
84 
85 #define USB_MEM_SMALL 64
86 #define USB_MEM_CHUNKS 64
87 #define USB_MEM_BLOCK (USB_MEM_SMALL * USB_MEM_CHUNKS)
88 
89 /* This struct is overlayed on free fragments. */
90 struct usb_frag_dma {
91 	usb_dma_block_t *block;
92 	u_int offs;
93 	LIST_ENTRY(usb_frag_dma) next;
94 };
95 
96 Static usbd_status	usb_block_allocmem(bus_dma_tag_t, size_t, size_t,
97 					   usb_dma_block_t **);
98 Static void		usb_block_freemem(usb_dma_block_t *);
99 
100 Static LIST_HEAD(, usb_dma_block) usb_blk_freelist =
101 	LIST_HEAD_INITIALIZER(usb_blk_freelist);
102 Static int usb_blk_nfree = 0;
103 /* XXX should have different free list for different tags (for speed) */
104 Static LIST_HEAD(, usb_frag_dma) usb_frag_freelist =
105 	LIST_HEAD_INITIALIZER(usb_frag_freelist);
106 
107 Static usbd_status
108 usb_block_allocmem(bus_dma_tag_t tag, size_t size, size_t align,
109 		   usb_dma_block_t **dmap)
110 {
111 	int error;
112         usb_dma_block_t *p;
113 	int s;
114 
115 	DPRINTFN(5, ("usb_block_allocmem: size=%lu align=%lu\n",
116 		     (u_long)size, (u_long)align));
117 
118 #ifdef DIAGNOSTIC
119 	if (!curproc) {
120 		printf("usb_block_allocmem: in interrupt context, size=%lu\n",
121 		    (unsigned long) size);
122 	}
123 #endif
124 
125 	s = splusb();
126 	/* First check the free list. */
127 	for (p = LIST_FIRST(&usb_blk_freelist); p; p = LIST_NEXT(p, next)) {
128 		if (p->tag == tag && p->size >= size && p->align >= align) {
129 			LIST_REMOVE(p, next);
130 			usb_blk_nfree--;
131 			splx(s);
132 			*dmap = p;
133 			DPRINTFN(6,("usb_block_allocmem: free list size=%lu\n",
134 				    (u_long)p->size));
135 			return (USBD_NORMAL_COMPLETION);
136 		}
137 	}
138 	splx(s);
139 
140 #ifdef DIAGNOSTIC
141 	if (!curproc) {
142 		printf("usb_block_allocmem: in interrupt context, failed\n");
143 		return (USBD_NOMEM);
144 	}
145 #endif
146 
147 	DPRINTFN(6, ("usb_block_allocmem: no free\n"));
148 	p = malloc(sizeof *p, M_USB, M_NOWAIT);
149 	if (p == NULL)
150 		return (USBD_NOMEM);
151 
152 	p->tag = tag;
153 	p->size = size;
154 	p->align = align;
155 	error = bus_dmamem_alloc(tag, p->size, align, 0,
156 				 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
157 				 &p->nsegs, BUS_DMA_NOWAIT);
158 	if (error)
159 		goto free0;
160 
161 	error = bus_dmamem_map(tag, p->segs, p->nsegs, p->size,
162 			       &p->kaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
163 	if (error)
164 		goto free1;
165 
166 	error = bus_dmamap_create(tag, p->size, 1, p->size,
167 				  0, BUS_DMA_NOWAIT, &p->map);
168 	if (error)
169 		goto unmap;
170 
171 	error = bus_dmamap_load(tag, p->map, p->kaddr, p->size, NULL,
172 				BUS_DMA_NOWAIT);
173 	if (error)
174 		goto destroy;
175 
176 	*dmap = p;
177 	return (USBD_NORMAL_COMPLETION);
178 
179  destroy:
180 	bus_dmamap_destroy(tag, p->map);
181  unmap:
182 	bus_dmamem_unmap(tag, p->kaddr, p->size);
183  free1:
184 	bus_dmamem_free(tag, p->segs, p->nsegs);
185  free0:
186 	free(p, M_USB);
187 	return (USBD_NOMEM);
188 }
189 
190 #if 0
191 void
192 usb_block_real_freemem(usb_dma_block_t *p)
193 {
194 #ifdef DIAGNOSTIC
195 	if (!curproc) {
196 		printf("usb_block_real_freemem: in interrupt context\n");
197 		return;
198 	}
199 #endif
200 	bus_dmamap_unload(p->tag, p->map);
201 	bus_dmamap_destroy(p->tag, p->map);
202 	bus_dmamem_unmap(p->tag, p->kaddr, p->size);
203 	bus_dmamem_free(p->tag, p->segs, p->nsegs);
204 	free(p, M_USB);
205 }
206 #endif
207 
208 /*
209  * Do not free the memory unconditionally since we might be called
210  * from an interrupt context and that is BAD.
211  * XXX when should we really free?
212  */
213 Static void
214 usb_block_freemem(usb_dma_block_t *p)
215 {
216 	int s;
217 
218 	DPRINTFN(6, ("usb_block_freemem: size=%lu\n", (u_long)p->size));
219 	s = splusb();
220 	LIST_INSERT_HEAD(&usb_blk_freelist, p, next);
221 	usb_blk_nfree++;
222 	splx(s);
223 }
224 
225 usbd_status
226 usb_allocmem(usbd_bus_handle bus, size_t size, size_t align, usb_dma_t *p)
227 {
228 	bus_dma_tag_t tag = bus->dmatag;
229 	usbd_status err;
230 	struct usb_frag_dma *f;
231 	usb_dma_block_t *b;
232 	int i;
233 	int s;
234 
235 	/* If the request is large then just use a full block. */
236 	if (size > USB_MEM_SMALL || align > USB_MEM_SMALL) {
237 		DPRINTFN(1, ("usb_allocmem: large alloc %d\n", (int)size));
238 		size = (size + USB_MEM_BLOCK - 1) & ~(USB_MEM_BLOCK - 1);
239 		err = usb_block_allocmem(tag, size, align, &p->block);
240 		if (!err) {
241 			p->block->flags = USB_DMA_FULLBLOCK;
242 			p->offs = 0;
243 		}
244 		return (err);
245 	}
246 
247 	s = splusb();
248 	/* Check for free fragments. */
249 	for (f = LIST_FIRST(&usb_frag_freelist); f; f = LIST_NEXT(f, next))
250 		if (f->block->tag == tag)
251 			break;
252 	if (f == NULL) {
253 		DPRINTFN(1, ("usb_allocmem: adding fragments\n"));
254 		err = usb_block_allocmem(tag, USB_MEM_BLOCK, USB_MEM_SMALL,&b);
255 		if (err) {
256 			splx(s);
257 			return (err);
258 		}
259 		b->flags = 0;
260 		for (i = 0; i < USB_MEM_BLOCK; i += USB_MEM_SMALL) {
261 			f = (struct usb_frag_dma *)(b->kaddr + i);
262 			f->block = b;
263 			f->offs = i;
264 			LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
265 		}
266 		f = LIST_FIRST(&usb_frag_freelist);
267 	}
268 	p->block = f->block;
269 	p->offs = f->offs;
270 	p->block->flags &= ~USB_DMA_RESERVE;
271 	LIST_REMOVE(f, next);
272 	splx(s);
273 	DPRINTFN(5, ("usb_allocmem: use frag=%p size=%d\n", f, (int)size));
274 	return (USBD_NORMAL_COMPLETION);
275 }
276 
277 void
278 usb_freemem(usbd_bus_handle bus, usb_dma_t *p)
279 {
280 	struct usb_frag_dma *f;
281 	int s;
282 
283 	if (p->block->flags & USB_DMA_FULLBLOCK) {
284 		DPRINTFN(1, ("usb_freemem: large free\n"));
285 		usb_block_freemem(p->block);
286 		return;
287 	}
288 	f = KERNADDR(p, 0);
289 	f->block = p->block;
290 	f->offs = p->offs;
291 	s = splusb();
292 	LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
293 	splx(s);
294 	DPRINTFN(5, ("usb_freemem: frag=%p\n", f));
295 }
296 
297 
298 #ifdef __NetBSD__
299 usbd_status
300 usb_reserve_allocm(struct usb_dma_reserve *rs, usb_dma_t *dma, u_int32_t size)
301 {
302 	int error;
303 	u_long start;
304 	bus_addr_t baddr;
305 
306 	if (rs->vaddr == 0)
307 		return USBD_NOMEM;
308 
309 	dma->block = malloc(sizeof *dma->block, M_USB, M_ZERO | M_NOWAIT);
310 	if (dma->block == NULL)
311 		return USBD_NOMEM;
312 
313 	error = extent_alloc(rs->extent, size, PAGE_SIZE, 0,
314 	    EX_NOWAIT, &start);
315 
316 	if (error != 0) {
317 		printf("%s: usb_reserve_allocm of size %u failed (error %d)\n",
318 		    ((struct device *)rs->softc)->dv_xname, size, error);
319 		return USBD_NOMEM;
320 	}
321 
322 	baddr = start;
323 	dma->offs = baddr - rs->paddr;
324 	dma->block->flags = USB_DMA_RESERVE;
325 	dma->block->align = PAGE_SIZE;
326 	dma->block->size = size;
327 	dma->block->nsegs = 1;
328 	/* XXX segs appears to be unused */
329 	dma->block->segs[0] = rs->map->dm_segs[0];
330 	dma->block->map = rs->map;
331 	dma->block->kaddr = rs->vaddr;
332 	dma->block->tag = rs->dtag;
333 
334 	return USBD_NORMAL_COMPLETION;
335 }
336 
337 void
338 usb_reserve_freem(struct usb_dma_reserve *rs, usb_dma_t *dma)
339 {
340 	int error;
341 
342 	error = extent_free(rs->extent,
343 	    (u_long)(rs->paddr + dma->offs), dma->block->size, 0);
344 	free(dma->block, M_USB);
345 }
346 
347 int
348 usb_setup_reserve(void *softc, struct usb_dma_reserve *rs, bus_dma_tag_t dtag,
349 		  size_t size)
350 {
351 	int error, nseg;
352 	bus_dma_segment_t seg;
353 	struct device *dv = softc;
354 
355 	rs->dtag = dtag;
356 	rs->size = size;
357 	rs->softc = softc;
358 
359 	error = bus_dmamem_alloc(dtag, USB_MEM_RESERVE, PAGE_SIZE, 0,
360 	    &seg, 1, &nseg, BUS_DMA_NOWAIT);
361 	if (error != 0)
362 		return error;
363 
364 	error = bus_dmamem_map(dtag, &seg, nseg, USB_MEM_RESERVE,
365 	    &rs->vaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
366 	if (error != 0)
367 		goto freeit;
368 
369 	error = bus_dmamap_create(dtag, USB_MEM_RESERVE, 1,
370 	    USB_MEM_RESERVE, 0, BUS_DMA_NOWAIT, &rs->map);
371 	if (error != 0)
372 		goto unmap;
373 
374 	error = bus_dmamap_load(dtag, rs->map, rs->vaddr, USB_MEM_RESERVE,
375 	    NULL, BUS_DMA_NOWAIT);
376 	if (error != 0)
377 		goto destroy;
378 
379 	rs->paddr = rs->map->dm_segs[0].ds_addr;
380 	rs->extent = extent_create(dv->dv_xname, (u_long)rs->paddr,
381 	    (u_long)(rs->paddr + USB_MEM_RESERVE),
382 	    M_USB, 0, 0, 0);
383 	if (rs->extent == NULL) {
384 		rs->vaddr = 0;
385 		return ENOMEM;
386 	}
387 
388 	return 0;
389 
390  destroy:
391 	bus_dmamap_destroy(dtag, rs->map);
392  unmap:
393 	bus_dmamem_unmap(dtag, rs->vaddr, size);
394  freeit:
395 	bus_dmamem_free(dtag, &seg, nseg);
396 
397 	rs->vaddr = 0;
398 
399 	return error;
400 }
401 #endif
402