xref: /openbsd-src/sys/dev/usb/usb_mem.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /*	$OpenBSD: usb_mem.c,v 1.32 2018/12/05 17:41:23 gerhard Exp $ */
2 /*	$NetBSD: usb_mem.c,v 1.26 2003/02/01 06:23:40 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (lennart@augustsson.net) at
10  * Carlstedt Research & Technology.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * USB DMA memory allocation.
36  * We need to allocate a lot of small (many 8 byte, some larger)
37  * memory blocks that can be used for DMA.  Using the bus_dma
38  * routines directly would incur large overheads in space and time.
39  */
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/queue.h>
46 #include <sys/timeout.h>
47 #include <sys/device.h>		/* for usbdivar.h */
48 #include <machine/bus.h>
49 
50 #include <dev/usb/usb.h>
51 #include <dev/usb/usbdi.h>
52 #include <dev/usb/usbdivar.h>	/* just for struct usb_dma */
53 #include <dev/usb/usb_mem.h>
54 
55 #ifdef USB_DEBUG
56 #define DPRINTF(x)	do { if (usbdebug) printf x; } while (0)
57 #define DPRINTFN(n,x)	do { if (usbdebug>(n)) printf x; } while (0)
58 extern int usbdebug;
59 #else
60 #define DPRINTF(x)
61 #define DPRINTFN(n,x)
62 #endif
63 
64 #define USB_MEM_SMALL 64
65 #define USB_MEM_CHUNKS 64
66 #define USB_MEM_BLOCK (USB_MEM_SMALL * USB_MEM_CHUNKS)
67 
68 struct usb_frag_dma {
69 	struct usb_dma_block *block;
70 	u_int offs;
71 	LIST_ENTRY(usb_frag_dma) next;
72 };
73 
74 usbd_status	usb_block_allocmem(bus_dma_tag_t, size_t, size_t,
75 		    struct usb_dma_block **);
76 void		usb_block_freemem(struct usb_dma_block *);
77 
78 LIST_HEAD(, usb_dma_block) usb_blk_freelist =
79 	LIST_HEAD_INITIALIZER(usb_blk_freelist);
80 int usb_blk_nfree = 0;
81 /* XXX should have different free list for different tags (for speed) */
82 LIST_HEAD(, usb_frag_dma) usb_frag_freelist =
83 	LIST_HEAD_INITIALIZER(usb_frag_freelist);
84 
85 usbd_status
86 usb_block_allocmem(bus_dma_tag_t tag, size_t size, size_t align,
87     struct usb_dma_block **dmap)
88 {
89 	int error;
90         struct usb_dma_block *p;
91 	int s;
92 
93 	DPRINTFN(5, ("%s: size=%lu align=%lu\n", __func__,
94 		     (u_long)size, (u_long)align));
95 
96 	s = splusb();
97 	/* First check the free list. */
98 	for (p = LIST_FIRST(&usb_blk_freelist); p; p = LIST_NEXT(p, next)) {
99 		if (p->tag == tag && p->size >= size && p->align >= align) {
100 			LIST_REMOVE(p, next);
101 			usb_blk_nfree--;
102 			splx(s);
103 			*dmap = p;
104 			DPRINTFN(6,("%s: free list size=%lu\n", __func__,
105 				    (u_long)p->size));
106 			return (USBD_NORMAL_COMPLETION);
107 		}
108 	}
109 	splx(s);
110 
111 	DPRINTFN(6, ("usb_block_allocmem: no free\n"));
112 	p = malloc(sizeof *p, M_USB, M_NOWAIT);
113 	if (p == NULL)
114 		return (USBD_NOMEM);
115 
116 	p->tag = tag;
117 	p->size = size;
118 	p->align = align;
119 	error = bus_dmamem_alloc(tag, p->size, align, 0,
120 				 p->segs, nitems(p->segs),
121 				 &p->nsegs, BUS_DMA_NOWAIT);
122 	if (error)
123 		goto free0;
124 
125 	error = bus_dmamem_map(tag, p->segs, p->nsegs, p->size,
126 			       &p->kaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
127 	if (error)
128 		goto free1;
129 
130 	error = bus_dmamap_create(tag, p->size, 1, p->size,
131 				  0, BUS_DMA_NOWAIT, &p->map);
132 	if (error)
133 		goto unmap;
134 
135 	error = bus_dmamap_load(tag, p->map, p->kaddr, p->size, NULL,
136 				BUS_DMA_NOWAIT);
137 	if (error)
138 		goto destroy;
139 
140 	*dmap = p;
141 	return (USBD_NORMAL_COMPLETION);
142 
143 destroy:
144 	bus_dmamap_destroy(tag, p->map);
145 unmap:
146 	bus_dmamem_unmap(tag, p->kaddr, p->size);
147 free1:
148 	bus_dmamem_free(tag, p->segs, p->nsegs);
149 free0:
150 	free(p, M_USB, sizeof *p);
151 	return (USBD_NOMEM);
152 }
153 
154 #if 0
155 void
156 usb_block_real_freemem(struct usb_dma_block *p)
157 {
158 	bus_dmamap_unload(p->tag, p->map);
159 	bus_dmamap_destroy(p->tag, p->map);
160 	bus_dmamem_unmap(p->tag, p->kaddr, p->size);
161 	bus_dmamem_free(p->tag, p->segs, p->nsegs);
162 	free(p, M_USB, sizeof *p);
163 }
164 #endif
165 
166 /*
167  * Do not free the memory unconditionally since we might be called
168  * from an interrupt context and that is BAD.
169  * XXX when should we really free?
170  */
171 void
172 usb_block_freemem(struct usb_dma_block *p)
173 {
174 	int s;
175 
176 	DPRINTFN(6, ("%s: size=%lu\n", __func__, (u_long)p->size));
177 	s = splusb();
178 	LIST_INSERT_HEAD(&usb_blk_freelist, p, next);
179 	usb_blk_nfree++;
180 	splx(s);
181 }
182 
183 usbd_status
184 usb_allocmem(struct usbd_bus *bus, size_t size, size_t align, struct usb_dma *p)
185 {
186 	bus_dma_tag_t tag = bus->dmatag;
187 	usbd_status err;
188 	struct usb_frag_dma *f;
189 	struct usb_dma_block *b;
190 	int i;
191 	int s;
192 
193 	/* If the request is large then just use a full block. */
194 	if (size > USB_MEM_SMALL || align > USB_MEM_SMALL) {
195 		DPRINTFN(1, ("%s: large alloc %d\n", __func__, (int)size));
196 		size = (size + USB_MEM_BLOCK - 1) & ~(USB_MEM_BLOCK - 1);
197 		err = usb_block_allocmem(tag, size, align, &p->block);
198 		if (!err) {
199 			p->block->frags = NULL;
200 			p->offs = 0;
201 		}
202 		return (err);
203 	}
204 
205 	s = splusb();
206 	/* Check for free fragments. */
207 	for (f = LIST_FIRST(&usb_frag_freelist); f; f = LIST_NEXT(f, next))
208 		if (f->block->tag == tag)
209 			break;
210 	if (f == NULL) {
211 		DPRINTFN(1, ("usb_allocmem: adding fragments\n"));
212 		err = usb_block_allocmem(tag, USB_MEM_BLOCK, USB_MEM_SMALL,&b);
213 		if (err) {
214 			splx(s);
215 			return (err);
216 		}
217 		b->frags = mallocarray(USB_MEM_CHUNKS, sizeof(*f), M_USB,
218 		    M_NOWAIT);
219 		if (b->frags == NULL) {
220 			splx(s);
221 			usb_block_freemem(b);
222 			return (USBD_NOMEM);
223 		}
224 		for (i = 0; i < USB_MEM_CHUNKS; i++) {
225 			f = &b->frags[i];
226 			f->block = b;
227 			f->offs = USB_MEM_SMALL * i;
228 			LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
229 		}
230 		f = LIST_FIRST(&usb_frag_freelist);
231 	}
232 	p->block = f->block;
233 	p->offs = f->offs;
234 	LIST_REMOVE(f, next);
235 	splx(s);
236 	DPRINTFN(5, ("%s: use frag=%p size=%d\n", __func__, f, (int)size));
237 	return (USBD_NORMAL_COMPLETION);
238 }
239 
240 void
241 usb_freemem(struct usbd_bus *bus, struct usb_dma *p)
242 {
243 	struct usb_frag_dma *f;
244 	int s;
245 
246 	if (p->block->frags == NULL) {
247 		DPRINTFN(1, ("usb_freemem: large free\n"));
248 		usb_block_freemem(p->block);
249 		return;
250 	}
251 	s = splusb();
252 	f = &p->block->frags[p->offs / USB_MEM_SMALL];
253 	LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
254 	splx(s);
255 	DPRINTFN(5, ("%s: frag=%p block=%p\n", __func__, f, f->block));
256 }
257 
258 void
259 usb_syncmem(struct usb_dma *p, bus_addr_t offset, bus_size_t len, int ops)
260 {
261 	bus_dmamap_sync(p->block->tag, p->block->map, p->offs + offset,
262 	    len, ops);
263 }
264