xref: /netbsd-src/sys/dev/usb/usb_mem.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: usb_mem.c,v 1.38 2009/02/10 17:00:06 drochner 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.38 2009/02/10 17:00:06 drochner 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 void
291 usb_syncmem(usb_dma_t *p, bus_addr_t offset, bus_size_t len, int ops)
292 {
293 	bus_dmamap_sync(p->block->tag, p->block->map, p->offs + offset,
294 	    len, ops);
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 || size > USB_MEM_RESERVE)
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 		aprint_error_dev(rs->dv,
318 		    "usb_reserve_allocm of size %u failed (error %d)\n",
319 		    size, error);
320 		return USBD_NOMEM;
321 	}
322 
323 	baddr = start;
324 	dma->offs = baddr - rs->paddr;
325 	dma->block->flags = USB_DMA_RESERVE;
326 	dma->block->align = PAGE_SIZE;
327 	dma->block->size = size;
328 	dma->block->nsegs = 1;
329 	/* XXX segs appears to be unused */
330 	dma->block->segs[0] = rs->map->dm_segs[0];
331 	dma->block->map = rs->map;
332 	dma->block->kaddr = rs->vaddr;
333 	dma->block->tag = rs->dtag;
334 
335 	return USBD_NORMAL_COMPLETION;
336 }
337 
338 void
339 usb_reserve_freem(struct usb_dma_reserve *rs, usb_dma_t *dma)
340 {
341 	int error;
342 
343 	error = extent_free(rs->extent,
344 	    (u_long)(rs->paddr + dma->offs), dma->block->size, 0);
345 	free(dma->block, M_USB);
346 }
347 
348 int
349 usb_setup_reserve(device_t dv, struct usb_dma_reserve *rs, bus_dma_tag_t dtag,
350 		  size_t size)
351 {
352 	int error, nseg;
353 	bus_dma_segment_t seg;
354 
355 	rs->dtag = dtag;
356 	rs->size = size;
357 	rs->dv = dv;
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(device_xname(dv), (u_long)rs->paddr,
381 	    (u_long)(rs->paddr + USB_MEM_RESERVE - 1),
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