1/*	$NetBSD: usb_mem.c,v 1.50 2011/09/27 01:02:38 jym 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.50 2011/09/27 01:02:38 jym 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
72extern 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. */
83struct usb_frag_dma {
84	usb_dma_block_t *block;
85	u_int offs;
86	LIST_ENTRY(usb_frag_dma) next;
87};
88
89Static usbd_status	usb_block_allocmem(bus_dma_tag_t, size_t, size_t,
90					   usb_dma_block_t **);
91Static void		usb_block_freemem(usb_dma_block_t *);
92
93LIST_HEAD(usb_dma_block_qh, usb_dma_block);
94Static struct usb_dma_block_qh usb_blk_freelist =
95	LIST_HEAD_INITIALIZER(usb_blk_freelist);
96#ifdef DEBUG
97Static struct usb_dma_block_qh usb_blk_fraglist =
98	LIST_HEAD_INITIALIZER(usb_blk_fraglist);
99Static struct usb_dma_block_qh usb_blk_fulllist =
100	LIST_HEAD_INITIALIZER(usb_blk_fulllist);
101#endif
102Static u_int usb_blk_nfree = 0;
103/* XXX should have different free list for different tags (for speed) */
104Static LIST_HEAD(, usb_frag_dma) usb_frag_freelist =
105	LIST_HEAD_INITIALIZER(usb_frag_freelist);
106
107Static usbd_status
108usb_block_allocmem(bus_dma_tag_t tag, size_t size, size_t align,
109		   usb_dma_block_t **dmap)
110{
111	usb_dma_block_t *b;
112	int error;
113	int s;
114
115	DPRINTFN(5, ("usb_block_allocmem: size=%zu align=%zu\n", size, align));
116
117#ifdef DIAGNOSTIC
118	if (cpu_intr_p()) {
119		printf("usb_block_allocmem: in interrupt context, size=%lu\n",
120		    (unsigned long) size);
121	}
122#endif
123
124	s = splusb();
125	/* First check the free list. */
126	LIST_FOREACH(b, &usb_blk_freelist, next) {
127		if (b->tag == tag && b->size >= size && b->align >= align) {
128			LIST_REMOVE(b, next);
129			usb_blk_nfree--;
130			splx(s);
131			*dmap = b;
132			DPRINTFN(6,("usb_block_allocmem: free list size=%zu\n",
133			    b->size));
134			return (USBD_NORMAL_COMPLETION);
135		}
136	}
137	splx(s);
138
139#ifdef DIAGNOSTIC
140	if (cpu_intr_p()) {
141		printf("usb_block_allocmem: in interrupt context, failed\n");
142		return (USBD_NOMEM);
143	}
144#endif
145
146	DPRINTFN(6, ("usb_block_allocmem: no free\n"));
147	b = malloc(sizeof *b, M_USB, M_NOWAIT | M_ZERO);
148	if (b == NULL)
149		return (USBD_NOMEM);
150
151	b->tag = tag;
152	b->size = size;
153	b->align = align;
154	error = bus_dmamem_alloc(tag, b->size, align, 0,
155				 b->segs, __arraycount(b->segs),
156				 &b->nsegs, BUS_DMA_NOWAIT);
157	if (error)
158		goto free0;
159
160	error = bus_dmamem_map(tag, b->segs, b->nsegs, b->size,
161			       &b->kaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
162	if (error)
163		goto free1;
164
165	error = bus_dmamap_create(tag, b->size, 1, b->size,
166				  0, BUS_DMA_NOWAIT, &b->map);
167	if (error)
168		goto unmap;
169
170	error = bus_dmamap_load(tag, b->map, b->kaddr, b->size, NULL,
171				BUS_DMA_NOWAIT);
172	if (error)
173		goto destroy;
174
175	*dmap = b;
176#ifdef USB_FRAG_DMA_WORKAROUND
177	memset(b->kaddr, 0, b->size);
178#endif
179	return (USBD_NORMAL_COMPLETION);
180
181 destroy:
182	bus_dmamap_destroy(tag, b->map);
183 unmap:
184	bus_dmamem_unmap(tag, b->kaddr, b->size);
185 free1:
186	bus_dmamem_free(tag, b->segs, b->nsegs);
187 free0:
188	free(b, M_USB);
189	return (USBD_NOMEM);
190}
191
192#if 0
193void
194usb_block_real_freemem(usb_dma_block_t *b)
195{
196#ifdef DIAGNOSTIC
197	if (cpu_intr_p()) {
198		printf("usb_block_real_freemem: in interrupt context\n");
199		return;
200	}
201#endif
202	bus_dmamap_unload(b->tag, b->map);
203	bus_dmamap_destroy(b->tag, b->map);
204	bus_dmamem_unmap(b->tag, b->kaddr, b->size);
205	bus_dmamem_free(b->tag, b->segs, b->nsegs);
206	free(p, M_USB);
207}
208#endif
209
210#ifdef DEBUG
211static bool
212usb_valid_block_p(usb_dma_block_t *b, struct usb_dma_block_qh *qh)
213{
214	usb_dma_block_t *xb;
215	LIST_FOREACH(xb, qh, next) {
216		if (xb == b)
217			return true;
218	}
219	return false;
220}
221#endif
222
223/*
224 * Do not free the memory unconditionally since we might be called
225 * from an interrupt context and that is BAD.
226 * XXX when should we really free?
227 */
228Static void
229usb_block_freemem(usb_dma_block_t *b)
230{
231	int s;
232
233	DPRINTFN(6, ("usb_block_freemem: size=%zu\n", b->size));
234	s = splusb();
235#ifdef DEBUG
236	LIST_REMOVE(b, next);
237#endif
238	LIST_INSERT_HEAD(&usb_blk_freelist, b, next);
239	usb_blk_nfree++;
240	splx(s);
241}
242
243usbd_status
244usb_allocmem(usbd_bus_handle bus, size_t size, size_t align, usb_dma_t *p)
245{
246	bus_dma_tag_t tag = bus->dmatag;
247	usbd_status err;
248	struct usb_frag_dma *f;
249	usb_dma_block_t *b;
250	int i;
251	int s;
252
253	/* If the request is large then just use a full block. */
254	if (size > USB_MEM_SMALL || align > USB_MEM_SMALL) {
255		DPRINTFN(1, ("usb_allocmem: large alloc %d\n", (int)size));
256		size = (size + USB_MEM_BLOCK - 1) & ~(USB_MEM_BLOCK - 1);
257		err = usb_block_allocmem(tag, size, align, &p->block);
258		if (!err) {
259#ifdef DEBUG
260			LIST_INSERT_HEAD(&usb_blk_fulllist, p->block, next);
261#endif
262			p->block->flags = USB_DMA_FULLBLOCK;
263			p->offs = 0;
264		}
265		return (err);
266	}
267
268	s = splusb();
269	/* Check for free fragments. */
270	LIST_FOREACH(f, &usb_frag_freelist, next) {
271		KDASSERTMSG(usb_valid_block_p(f->block, &usb_blk_fraglist),
272		    "%s: usb frag %p: unknown block pointer %p",
273		     __func__, f, f->block);
274		if (f->block->tag == tag)
275			break;
276	}
277	if (f == NULL) {
278		DPRINTFN(1, ("usb_allocmem: adding fragments\n"));
279		err = usb_block_allocmem(tag, USB_MEM_BLOCK, USB_MEM_SMALL,&b);
280		if (err) {
281			splx(s);
282			return (err);
283		}
284#ifdef DEBUG
285		LIST_INSERT_HEAD(&usb_blk_fraglist, b, next);
286#endif
287		b->flags = 0;
288		for (i = 0; i < USB_MEM_BLOCK; i += USB_MEM_SMALL) {
289			f = (struct usb_frag_dma *)((char *)b->kaddr + i);
290			f->block = b;
291			f->offs = i;
292			LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
293#ifdef USB_FRAG_DMA_WORKAROUND
294			i += 1 * USB_MEM_SMALL;
295#endif
296		}
297		f = LIST_FIRST(&usb_frag_freelist);
298	}
299	p->block = f->block;
300	p->offs = f->offs;
301#ifdef USB_FRAG_DMA_WORKAROUND
302	p->offs += USB_MEM_SMALL;
303#endif
304	p->block->flags &= ~USB_DMA_RESERVE;
305	LIST_REMOVE(f, next);
306	splx(s);
307	DPRINTFN(5, ("usb_allocmem: use frag=%p size=%d\n", f, (int)size));
308	return (USBD_NORMAL_COMPLETION);
309}
310
311void
312usb_freemem(usbd_bus_handle bus, usb_dma_t *p)
313{
314	struct usb_frag_dma *f;
315	int s;
316
317	if (p->block->flags & USB_DMA_FULLBLOCK) {
318		KDASSERTMSG(usb_valid_block_p(p->block, &usb_blk_fulllist),
319		    "%s: dma %p: invalid block pointer %p",
320		     __func__, p, p->block);
321		DPRINTFN(1, ("usb_freemem: large free\n"));
322		usb_block_freemem(p->block);
323		return;
324	}
325	KDASSERTMSG(usb_valid_block_p(p->block, &usb_blk_fraglist),
326	    "%s: dma %p: invalid block pointer %p",
327	     __func__, p, p->block);
328	//usb_syncmem(p, 0, USB_MEM_SMALL, BUS_DMASYNC_POSTREAD);
329	f = KERNADDR(p, 0);
330#ifdef USB_FRAG_DMA_WORKAROUND
331	f = (void *)((uintptr_t)f - USB_MEM_SMALL);
332#endif
333	f->block = p->block;
334	f->offs = p->offs;
335#ifdef USB_FRAG_DMA_WORKAROUND
336	f->offs -= USB_MEM_SMALL;
337#endif
338	s = splusb();
339	LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
340	splx(s);
341	DPRINTFN(5, ("usb_freemem: frag=%p\n", f));
342}
343
344void
345usb_syncmem(usb_dma_t *p, bus_addr_t offset, bus_size_t len, int ops)
346{
347	bus_dmamap_sync(p->block->tag, p->block->map, p->offs + offset,
348	    len, ops);
349}
350
351
352#ifdef __NetBSD__
353usbd_status
354usb_reserve_allocm(struct usb_dma_reserve *rs, usb_dma_t *dma, u_int32_t size)
355{
356	int error;
357	u_long start;
358	bus_addr_t baddr;
359
360	if (rs->vaddr == 0 || size > USB_MEM_RESERVE)
361		return USBD_NOMEM;
362
363	dma->block = malloc(sizeof *dma->block, M_USB, M_ZERO | M_NOWAIT);
364	if (dma->block == NULL)
365		return USBD_NOMEM;
366
367	error = extent_alloc(rs->extent, size, PAGE_SIZE, 0,
368	    EX_NOWAIT, &start);
369
370	if (error != 0) {
371		aprint_error_dev(rs->dv,
372		    "usb_reserve_allocm of size %u failed (error %d)\n",
373		    size, error);
374		return USBD_NOMEM;
375	}
376
377	baddr = start;
378	dma->offs = baddr - rs->paddr;
379	dma->block->flags = USB_DMA_RESERVE;
380	dma->block->align = PAGE_SIZE;
381	dma->block->size = size;
382	dma->block->nsegs = 1;
383	/* XXX segs appears to be unused */
384	dma->block->segs[0] = rs->map->dm_segs[0];
385	dma->block->map = rs->map;
386	dma->block->kaddr = rs->vaddr;
387	dma->block->tag = rs->dtag;
388
389	return USBD_NORMAL_COMPLETION;
390}
391
392void
393usb_reserve_freem(struct usb_dma_reserve *rs, usb_dma_t *dma)
394{
395	int error;
396
397	error = extent_free(rs->extent,
398	    (u_long)(rs->paddr + dma->offs), dma->block->size, 0);
399	free(dma->block, M_USB);
400}
401
402int
403usb_setup_reserve(device_t dv, struct usb_dma_reserve *rs, bus_dma_tag_t dtag,
404		  size_t size)
405{
406	int error, nseg;
407	bus_dma_segment_t seg;
408
409	rs->dtag = dtag;
410	rs->size = size;
411	rs->dv = dv;
412
413	error = bus_dmamem_alloc(dtag, USB_MEM_RESERVE, PAGE_SIZE, 0,
414	    &seg, 1, &nseg, BUS_DMA_NOWAIT);
415	if (error != 0)
416		return error;
417
418	error = bus_dmamem_map(dtag, &seg, nseg, USB_MEM_RESERVE,
419	    &rs->vaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
420	if (error != 0)
421		goto freeit;
422
423	error = bus_dmamap_create(dtag, USB_MEM_RESERVE, 1,
424	    USB_MEM_RESERVE, 0, BUS_DMA_NOWAIT, &rs->map);
425	if (error != 0)
426		goto unmap;
427
428	error = bus_dmamap_load(dtag, rs->map, rs->vaddr, USB_MEM_RESERVE,
429	    NULL, BUS_DMA_NOWAIT);
430	if (error != 0)
431		goto destroy;
432
433	rs->paddr = rs->map->dm_segs[0].ds_addr;
434	rs->extent = extent_create(device_xname(dv), (u_long)rs->paddr,
435	    (u_long)(rs->paddr + USB_MEM_RESERVE - 1), 0, 0, 0);
436	if (rs->extent == NULL) {
437		rs->vaddr = 0;
438		return ENOMEM;
439	}
440
441	return 0;
442
443 destroy:
444	bus_dmamap_destroy(dtag, rs->map);
445 unmap:
446	bus_dmamem_unmap(dtag, rs->vaddr, size);
447 freeit:
448	bus_dmamem_free(dtag, &seg, nseg);
449
450	rs->vaddr = 0;
451
452	return error;
453}
454#endif
455