xref: /netbsd-src/sys/dev/usb/usb_mem.c (revision 46f5119e40af2e51998f686b2fdcc76b5488f7f3)
1 /*	$NetBSD: usb_mem.c,v 1.46 2011/03/20 17:38:11 tsutsui 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.46 2011/03/20 17:38:11 tsutsui Exp $");
42 
43 #ifdef _KERNEL_OPT
44 #include "opt_usb.h"
45 #endif
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/queue.h>
52 #include <sys/device.h>		/* for usbdivar.h */
53 #include <sys/bus.h>
54 #include <sys/cpu.h>
55 
56 #ifdef __NetBSD__
57 #include <sys/extent.h>
58 #endif
59 
60 #ifdef DIAGNOSTIC
61 #include <sys/proc.h>
62 #endif
63 
64 #include <dev/usb/usb.h>
65 #include <dev/usb/usbdi.h>
66 #include <dev/usb/usbdivar.h>	/* just for usb_dma_t */
67 #include <dev/usb/usb_mem.h>
68 
69 #ifdef USB_DEBUG
70 #define DPRINTF(x)	if (usbdebug) printf x
71 #define DPRINTFN(n,x)	if (usbdebug>(n)) printf x
72 extern int usbdebug;
73 #else
74 #define DPRINTF(x)
75 #define DPRINTFN(n,x)
76 #endif
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 (cpu_intr_p()) {
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 	LIST_FOREACH(p, &usb_blk_freelist, 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 (cpu_intr_p()) {
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 #ifdef USB_FRAG_DMA_WORKAROUND
171 	memset(p->kaddr, 0, p->size);
172 #endif
173 	return (USBD_NORMAL_COMPLETION);
174 
175  destroy:
176 	bus_dmamap_destroy(tag, p->map);
177  unmap:
178 	bus_dmamem_unmap(tag, p->kaddr, p->size);
179  free1:
180 	bus_dmamem_free(tag, p->segs, p->nsegs);
181  free0:
182 	free(p, M_USB);
183 	return (USBD_NOMEM);
184 }
185 
186 #if 0
187 void
188 usb_block_real_freemem(usb_dma_block_t *p)
189 {
190 #ifdef DIAGNOSTIC
191 	if (cpu_intr_p()) {
192 		printf("usb_block_real_freemem: in interrupt context\n");
193 		return;
194 	}
195 #endif
196 	bus_dmamap_unload(p->tag, p->map);
197 	bus_dmamap_destroy(p->tag, p->map);
198 	bus_dmamem_unmap(p->tag, p->kaddr, p->size);
199 	bus_dmamem_free(p->tag, p->segs, p->nsegs);
200 	free(p, M_USB);
201 }
202 #endif
203 
204 /*
205  * Do not free the memory unconditionally since we might be called
206  * from an interrupt context and that is BAD.
207  * XXX when should we really free?
208  */
209 Static void
210 usb_block_freemem(usb_dma_block_t *p)
211 {
212 	int s;
213 
214 	DPRINTFN(6, ("usb_block_freemem: size=%lu\n", (u_long)p->size));
215 	s = splusb();
216 	LIST_INSERT_HEAD(&usb_blk_freelist, p, next);
217 	usb_blk_nfree++;
218 	splx(s);
219 }
220 
221 usbd_status
222 usb_allocmem(usbd_bus_handle bus, size_t size, size_t align, usb_dma_t *p)
223 {
224 	bus_dma_tag_t tag = bus->dmatag;
225 	usbd_status err;
226 	struct usb_frag_dma *f;
227 	usb_dma_block_t *b;
228 	int i;
229 	int s;
230 
231 	/* If the request is large then just use a full block. */
232 	if (size > USB_MEM_SMALL || align > USB_MEM_SMALL) {
233 		DPRINTFN(1, ("usb_allocmem: large alloc %d\n", (int)size));
234 		size = (size + USB_MEM_BLOCK - 1) & ~(USB_MEM_BLOCK - 1);
235 		err = usb_block_allocmem(tag, size, align, &p->block);
236 		if (!err) {
237 			p->block->flags = USB_DMA_FULLBLOCK;
238 			p->offs = 0;
239 		}
240 		return (err);
241 	}
242 
243 	s = splusb();
244 	/* Check for free fragments. */
245 	LIST_FOREACH(f, &usb_frag_freelist, next) {
246 		if (f->block->tag == tag)
247 			break;
248 	}
249 	if (f == NULL) {
250 		DPRINTFN(1, ("usb_allocmem: adding fragments\n"));
251 		err = usb_block_allocmem(tag, USB_MEM_BLOCK, USB_MEM_SMALL,&b);
252 		if (err) {
253 			splx(s);
254 			return (err);
255 		}
256 		b->flags = 0;
257 		for (i = 0; i < USB_MEM_BLOCK; i += USB_MEM_SMALL) {
258 			f = (struct usb_frag_dma *)((char *)b->kaddr + i);
259 			f->block = b;
260 			f->offs = i;
261 			LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
262 #ifdef USB_FRAG_DMA_WORKAROUND
263 			i += 1 * USB_MEM_SMALL;
264 #endif
265 		}
266 		f = LIST_FIRST(&usb_frag_freelist);
267 	}
268 	p->block = f->block;
269 	p->offs = f->offs;
270 #ifdef USB_FRAG_DMA_WORKAROUND
271 	p->offs += USB_MEM_SMALL;
272 #endif
273 	p->block->flags &= ~USB_DMA_RESERVE;
274 	LIST_REMOVE(f, next);
275 	splx(s);
276 	DPRINTFN(5, ("usb_allocmem: use frag=%p size=%d\n", f, (int)size));
277 	return (USBD_NORMAL_COMPLETION);
278 }
279 
280 void
281 usb_freemem(usbd_bus_handle bus, usb_dma_t *p)
282 {
283 	struct usb_frag_dma *f;
284 	int s;
285 
286 	if (p->block->flags & USB_DMA_FULLBLOCK) {
287 		DPRINTFN(1, ("usb_freemem: large free\n"));
288 		usb_block_freemem(p->block);
289 		return;
290 	}
291 	//usb_syncmem(p, 0, USB_MEM_SMALL, BUS_DMASYNC_POSTREAD);
292 	f = KERNADDR(p, 0);
293 #ifdef USB_FRAG_DMA_WORKAROUND
294 	f = (void *)((uintptr_t)f - USB_MEM_SMALL);
295 #endif
296 	f->block = p->block;
297 	f->offs = p->offs;
298 #ifdef USB_FRAG_DMA_WORKAROUND
299 	f->offs -= USB_MEM_SMALL;
300 #endif
301 	s = splusb();
302 	LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
303 	splx(s);
304 	DPRINTFN(5, ("usb_freemem: frag=%p\n", f));
305 }
306 
307 void
308 usb_syncmem(usb_dma_t *p, bus_addr_t offset, bus_size_t len, int ops)
309 {
310 	bus_dmamap_sync(p->block->tag, p->block->map, p->offs + offset,
311 	    len, ops);
312 }
313 
314 
315 #ifdef __NetBSD__
316 usbd_status
317 usb_reserve_allocm(struct usb_dma_reserve *rs, usb_dma_t *dma, u_int32_t size)
318 {
319 	int error;
320 	u_long start;
321 	bus_addr_t baddr;
322 
323 	if (rs->vaddr == 0 || size > USB_MEM_RESERVE)
324 		return USBD_NOMEM;
325 
326 	dma->block = malloc(sizeof *dma->block, M_USB, M_ZERO | M_NOWAIT);
327 	if (dma->block == NULL)
328 		return USBD_NOMEM;
329 
330 	error = extent_alloc(rs->extent, size, PAGE_SIZE, 0,
331 	    EX_NOWAIT, &start);
332 
333 	if (error != 0) {
334 		aprint_error_dev(rs->dv,
335 		    "usb_reserve_allocm of size %u failed (error %d)\n",
336 		    size, error);
337 		return USBD_NOMEM;
338 	}
339 
340 	baddr = start;
341 	dma->offs = baddr - rs->paddr;
342 	dma->block->flags = USB_DMA_RESERVE;
343 	dma->block->align = PAGE_SIZE;
344 	dma->block->size = size;
345 	dma->block->nsegs = 1;
346 	/* XXX segs appears to be unused */
347 	dma->block->segs[0] = rs->map->dm_segs[0];
348 	dma->block->map = rs->map;
349 	dma->block->kaddr = rs->vaddr;
350 	dma->block->tag = rs->dtag;
351 
352 	return USBD_NORMAL_COMPLETION;
353 }
354 
355 void
356 usb_reserve_freem(struct usb_dma_reserve *rs, usb_dma_t *dma)
357 {
358 	int error;
359 
360 	error = extent_free(rs->extent,
361 	    (u_long)(rs->paddr + dma->offs), dma->block->size, 0);
362 	free(dma->block, M_USB);
363 }
364 
365 int
366 usb_setup_reserve(device_t dv, struct usb_dma_reserve *rs, bus_dma_tag_t dtag,
367 		  size_t size)
368 {
369 	int error, nseg;
370 	bus_dma_segment_t seg;
371 
372 	rs->dtag = dtag;
373 	rs->size = size;
374 	rs->dv = dv;
375 
376 	error = bus_dmamem_alloc(dtag, USB_MEM_RESERVE, PAGE_SIZE, 0,
377 	    &seg, 1, &nseg, BUS_DMA_NOWAIT);
378 	if (error != 0)
379 		return error;
380 
381 	error = bus_dmamem_map(dtag, &seg, nseg, USB_MEM_RESERVE,
382 	    &rs->vaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
383 	if (error != 0)
384 		goto freeit;
385 
386 	error = bus_dmamap_create(dtag, USB_MEM_RESERVE, 1,
387 	    USB_MEM_RESERVE, 0, BUS_DMA_NOWAIT, &rs->map);
388 	if (error != 0)
389 		goto unmap;
390 
391 	error = bus_dmamap_load(dtag, rs->map, rs->vaddr, USB_MEM_RESERVE,
392 	    NULL, BUS_DMA_NOWAIT);
393 	if (error != 0)
394 		goto destroy;
395 
396 	rs->paddr = rs->map->dm_segs[0].ds_addr;
397 	rs->extent = extent_create(device_xname(dv), (u_long)rs->paddr,
398 	    (u_long)(rs->paddr + USB_MEM_RESERVE - 1),
399 	    M_USB, 0, 0, 0);
400 	if (rs->extent == NULL) {
401 		rs->vaddr = 0;
402 		return ENOMEM;
403 	}
404 
405 	return 0;
406 
407  destroy:
408 	bus_dmamap_destroy(dtag, rs->map);
409  unmap:
410 	bus_dmamem_unmap(dtag, rs->vaddr, size);
411  freeit:
412 	bus_dmamem_free(dtag, &seg, nseg);
413 
414 	rs->vaddr = 0;
415 
416 	return error;
417 }
418 #endif
419