xref: /netbsd-src/sys/dev/usb/usb_mem.c (revision cd22f25e6f6d1cc1f197fe8c5468a80f51d1c4e1)
1 /*	$NetBSD: usb_mem.c,v 1.35 2008/04/28 20:24:00 martin 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  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * USB DMA memory allocation.
35  * We need to allocate a lot of small (many 8 byte, some larger)
36  * memory blocks that can be used for DMA.  Using the bus_dma
37  * routines directly would incur large overheads in space and time.
38  */
39 
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: usb_mem.c,v 1.35 2008/04/28 20:24:00 martin Exp $");
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/queue.h>
48 #include <sys/device.h>		/* for usbdivar.h */
49 #include <sys/bus.h>
50 
51 #ifdef __NetBSD__
52 #include <sys/extent.h>
53 #include <uvm/uvm_extern.h>
54 #endif
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 extern int usbdebug;
69 #else
70 #define DPRINTF(x)
71 #define DPRINTFN(n,x)
72 #endif
73 
74 MALLOC_DEFINE(M_USB, "USB", "USB misc. memory");
75 MALLOC_DEFINE(M_USBDEV, "USB device", "USB device driver");
76 MALLOC_DEFINE(M_USBHC, "USB HC", "USB host controller");
77 
78 #define USB_MEM_SMALL 64
79 #define USB_MEM_CHUNKS 64
80 #define USB_MEM_BLOCK (USB_MEM_SMALL * USB_MEM_CHUNKS)
81 
82 /* This struct is overlayed on free fragments. */
83 struct usb_frag_dma {
84 	usb_dma_block_t *block;
85 	u_int offs;
86 	LIST_ENTRY(usb_frag_dma) next;
87 };
88 
89 Static usbd_status	usb_block_allocmem(bus_dma_tag_t, size_t, size_t,
90 					   usb_dma_block_t **);
91 Static void		usb_block_freemem(usb_dma_block_t *);
92 
93 Static LIST_HEAD(, usb_dma_block) usb_blk_freelist =
94 	LIST_HEAD_INITIALIZER(usb_blk_freelist);
95 Static int usb_blk_nfree = 0;
96 /* XXX should have different free list for different tags (for speed) */
97 Static LIST_HEAD(, usb_frag_dma) usb_frag_freelist =
98 	LIST_HEAD_INITIALIZER(usb_frag_freelist);
99 
100 Static usbd_status
101 usb_block_allocmem(bus_dma_tag_t tag, size_t size, size_t align,
102 		   usb_dma_block_t **dmap)
103 {
104 	int error;
105         usb_dma_block_t *p;
106 	int s;
107 
108 	DPRINTFN(5, ("usb_block_allocmem: size=%lu align=%lu\n",
109 		     (u_long)size, (u_long)align));
110 
111 #ifdef DIAGNOSTIC
112 	if (!curproc) {
113 		printf("usb_block_allocmem: in interrupt context, size=%lu\n",
114 		    (unsigned long) size);
115 	}
116 #endif
117 
118 	s = splusb();
119 	/* First check the free list. */
120 	for (p = LIST_FIRST(&usb_blk_freelist); p; p = LIST_NEXT(p, next)) {
121 		if (p->tag == tag && p->size >= size && p->align >= align) {
122 			LIST_REMOVE(p, next);
123 			usb_blk_nfree--;
124 			splx(s);
125 			*dmap = p;
126 			DPRINTFN(6,("usb_block_allocmem: free list size=%lu\n",
127 				    (u_long)p->size));
128 			return (USBD_NORMAL_COMPLETION);
129 		}
130 	}
131 	splx(s);
132 
133 #ifdef DIAGNOSTIC
134 	if (!curproc) {
135 		printf("usb_block_allocmem: in interrupt context, failed\n");
136 		return (USBD_NOMEM);
137 	}
138 #endif
139 
140 	DPRINTFN(6, ("usb_block_allocmem: no free\n"));
141 	p = malloc(sizeof *p, M_USB, M_NOWAIT);
142 	if (p == NULL)
143 		return (USBD_NOMEM);
144 
145 	p->tag = tag;
146 	p->size = size;
147 	p->align = align;
148 	error = bus_dmamem_alloc(tag, p->size, align, 0,
149 				 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
150 				 &p->nsegs, BUS_DMA_NOWAIT);
151 	if (error)
152 		goto free0;
153 
154 	error = bus_dmamem_map(tag, p->segs, p->nsegs, p->size,
155 			       &p->kaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
156 	if (error)
157 		goto free1;
158 
159 	error = bus_dmamap_create(tag, p->size, 1, p->size,
160 				  0, BUS_DMA_NOWAIT, &p->map);
161 	if (error)
162 		goto unmap;
163 
164 	error = bus_dmamap_load(tag, p->map, p->kaddr, p->size, NULL,
165 				BUS_DMA_NOWAIT);
166 	if (error)
167 		goto destroy;
168 
169 	*dmap = p;
170 	return (USBD_NORMAL_COMPLETION);
171 
172  destroy:
173 	bus_dmamap_destroy(tag, p->map);
174  unmap:
175 	bus_dmamem_unmap(tag, p->kaddr, p->size);
176  free1:
177 	bus_dmamem_free(tag, p->segs, p->nsegs);
178  free0:
179 	free(p, M_USB);
180 	return (USBD_NOMEM);
181 }
182 
183 #if 0
184 void
185 usb_block_real_freemem(usb_dma_block_t *p)
186 {
187 #ifdef DIAGNOSTIC
188 	if (!curproc) {
189 		printf("usb_block_real_freemem: in interrupt context\n");
190 		return;
191 	}
192 #endif
193 	bus_dmamap_unload(p->tag, p->map);
194 	bus_dmamap_destroy(p->tag, p->map);
195 	bus_dmamem_unmap(p->tag, p->kaddr, p->size);
196 	bus_dmamem_free(p->tag, p->segs, p->nsegs);
197 	free(p, M_USB);
198 }
199 #endif
200 
201 /*
202  * Do not free the memory unconditionally since we might be called
203  * from an interrupt context and that is BAD.
204  * XXX when should we really free?
205  */
206 Static void
207 usb_block_freemem(usb_dma_block_t *p)
208 {
209 	int s;
210 
211 	DPRINTFN(6, ("usb_block_freemem: size=%lu\n", (u_long)p->size));
212 	s = splusb();
213 	LIST_INSERT_HEAD(&usb_blk_freelist, p, next);
214 	usb_blk_nfree++;
215 	splx(s);
216 }
217 
218 usbd_status
219 usb_allocmem(usbd_bus_handle bus, size_t size, size_t align, usb_dma_t *p)
220 {
221 	bus_dma_tag_t tag = bus->dmatag;
222 	usbd_status err;
223 	struct usb_frag_dma *f;
224 	usb_dma_block_t *b;
225 	int i;
226 	int s;
227 
228 	/* If the request is large then just use a full block. */
229 	if (size > USB_MEM_SMALL || align > USB_MEM_SMALL) {
230 		DPRINTFN(1, ("usb_allocmem: large alloc %d\n", (int)size));
231 		size = (size + USB_MEM_BLOCK - 1) & ~(USB_MEM_BLOCK - 1);
232 		err = usb_block_allocmem(tag, size, align, &p->block);
233 		if (!err) {
234 			p->block->flags = USB_DMA_FULLBLOCK;
235 			p->offs = 0;
236 		}
237 		return (err);
238 	}
239 
240 	s = splusb();
241 	/* Check for free fragments. */
242 	for (f = LIST_FIRST(&usb_frag_freelist); f; f = LIST_NEXT(f, next))
243 		if (f->block->tag == tag)
244 			break;
245 	if (f == NULL) {
246 		DPRINTFN(1, ("usb_allocmem: adding fragments\n"));
247 		err = usb_block_allocmem(tag, USB_MEM_BLOCK, USB_MEM_SMALL,&b);
248 		if (err) {
249 			splx(s);
250 			return (err);
251 		}
252 		b->flags = 0;
253 		for (i = 0; i < USB_MEM_BLOCK; i += USB_MEM_SMALL) {
254 			f = (struct usb_frag_dma *)((char *)b->kaddr + i);
255 			f->block = b;
256 			f->offs = i;
257 			LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
258 		}
259 		f = LIST_FIRST(&usb_frag_freelist);
260 	}
261 	p->block = f->block;
262 	p->offs = f->offs;
263 	p->block->flags &= ~USB_DMA_RESERVE;
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(usbd_bus_handle bus, usb_dma_t *p)
272 {
273 	struct usb_frag_dma *f;
274 	int s;
275 
276 	if (p->block->flags & USB_DMA_FULLBLOCK) {
277 		DPRINTFN(1, ("usb_freemem: large free\n"));
278 		usb_block_freemem(p->block);
279 		return;
280 	}
281 	f = KERNADDR(p, 0);
282 	f->block = p->block;
283 	f->offs = p->offs;
284 	s = splusb();
285 	LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
286 	splx(s);
287 	DPRINTFN(5, ("usb_freemem: frag=%p\n", f));
288 }
289 
290 
291 #ifdef __NetBSD__
292 usbd_status
293 usb_reserve_allocm(struct usb_dma_reserve *rs, usb_dma_t *dma, u_int32_t size)
294 {
295 	int error;
296 	u_long start;
297 	bus_addr_t baddr;
298 
299 	if (rs->vaddr == 0)
300 		return USBD_NOMEM;
301 
302 	dma->block = malloc(sizeof *dma->block, M_USB, M_ZERO | M_NOWAIT);
303 	if (dma->block == NULL)
304 		return USBD_NOMEM;
305 
306 	error = extent_alloc(rs->extent, size, PAGE_SIZE, 0,
307 	    EX_NOWAIT, &start);
308 
309 	if (error != 0) {
310 		aprint_error_dev((struct device *)rs->softc, "usb_reserve_allocm of size %u failed (error %d)\n",
311 		    size, error);
312 		return USBD_NOMEM;
313 	}
314 
315 	baddr = start;
316 	dma->offs = baddr - rs->paddr;
317 	dma->block->flags = USB_DMA_RESERVE;
318 	dma->block->align = PAGE_SIZE;
319 	dma->block->size = size;
320 	dma->block->nsegs = 1;
321 	/* XXX segs appears to be unused */
322 	dma->block->segs[0] = rs->map->dm_segs[0];
323 	dma->block->map = rs->map;
324 	dma->block->kaddr = rs->vaddr;
325 	dma->block->tag = rs->dtag;
326 
327 	return USBD_NORMAL_COMPLETION;
328 }
329 
330 void
331 usb_reserve_freem(struct usb_dma_reserve *rs, usb_dma_t *dma)
332 {
333 	int error;
334 
335 	error = extent_free(rs->extent,
336 	    (u_long)(rs->paddr + dma->offs), dma->block->size, 0);
337 	free(dma->block, M_USB);
338 }
339 
340 int
341 usb_setup_reserve(void *softc, struct usb_dma_reserve *rs, bus_dma_tag_t dtag,
342 		  size_t size)
343 {
344 	int error, nseg;
345 	bus_dma_segment_t seg;
346 	struct device *dv = softc;
347 
348 	rs->dtag = dtag;
349 	rs->size = size;
350 	rs->softc = softc;
351 
352 	error = bus_dmamem_alloc(dtag, USB_MEM_RESERVE, PAGE_SIZE, 0,
353 	    &seg, 1, &nseg, BUS_DMA_NOWAIT);
354 	if (error != 0)
355 		return error;
356 
357 	error = bus_dmamem_map(dtag, &seg, nseg, USB_MEM_RESERVE,
358 	    &rs->vaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
359 	if (error != 0)
360 		goto freeit;
361 
362 	error = bus_dmamap_create(dtag, USB_MEM_RESERVE, 1,
363 	    USB_MEM_RESERVE, 0, BUS_DMA_NOWAIT, &rs->map);
364 	if (error != 0)
365 		goto unmap;
366 
367 	error = bus_dmamap_load(dtag, rs->map, rs->vaddr, USB_MEM_RESERVE,
368 	    NULL, BUS_DMA_NOWAIT);
369 	if (error != 0)
370 		goto destroy;
371 
372 	rs->paddr = rs->map->dm_segs[0].ds_addr;
373 	rs->extent = extent_create(device_xname(dv), (u_long)rs->paddr,
374 	    (u_long)(rs->paddr + USB_MEM_RESERVE),
375 	    M_USB, 0, 0, 0);
376 	if (rs->extent == NULL) {
377 		rs->vaddr = 0;
378 		return ENOMEM;
379 	}
380 
381 	return 0;
382 
383  destroy:
384 	bus_dmamap_destroy(dtag, rs->map);
385  unmap:
386 	bus_dmamem_unmap(dtag, rs->vaddr, size);
387  freeit:
388 	bus_dmamem_free(dtag, &seg, nseg);
389 
390 	rs->vaddr = 0;
391 
392 	return error;
393 }
394 #endif
395