xref: /openbsd/sys/dev/usb/usb_mem.c (revision d415bd75)
1 /*	$OpenBSD: usb_mem.c,v 1.34 2020/03/21 12:08:31 patrick 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 **, int);
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, int coherent)
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 		    p->coherent == coherent) {
101 			LIST_REMOVE(p, next);
102 			usb_blk_nfree--;
103 			splx(s);
104 			*dmap = p;
105 			DPRINTFN(6,("%s: free list size=%lu\n", __func__,
106 				    (u_long)p->size));
107 			return (USBD_NORMAL_COMPLETION);
108 		}
109 	}
110 	splx(s);
111 
112 	DPRINTFN(6, ("usb_block_allocmem: no free\n"));
113 	p = malloc(sizeof *p, M_USB, M_NOWAIT);
114 	if (p == NULL)
115 		return (USBD_NOMEM);
116 
117 	p->tag = tag;
118 	p->size = size;
119 	p->align = align;
120 	p->coherent = coherent;
121 	error = bus_dmamem_alloc(tag, p->size, align, 0,
122 				 p->segs, nitems(p->segs),
123 				 &p->nsegs, BUS_DMA_NOWAIT);
124 	if (error)
125 		goto free0;
126 
127 	error = bus_dmamem_map(tag, p->segs, p->nsegs, p->size,
128 			       &p->kaddr, BUS_DMA_NOWAIT | (coherent ?
129 			       BUS_DMA_COHERENT : 0));
130 	if (error)
131 		goto free1;
132 
133 	error = bus_dmamap_create(tag, p->size, 1, p->size,
134 				  0, BUS_DMA_NOWAIT, &p->map);
135 	if (error)
136 		goto unmap;
137 
138 	error = bus_dmamap_load(tag, p->map, p->kaddr, p->size, NULL,
139 				BUS_DMA_NOWAIT);
140 	if (error)
141 		goto destroy;
142 
143 	*dmap = p;
144 	return (USBD_NORMAL_COMPLETION);
145 
146 destroy:
147 	bus_dmamap_destroy(tag, p->map);
148 unmap:
149 	bus_dmamem_unmap(tag, p->kaddr, p->size);
150 free1:
151 	bus_dmamem_free(tag, p->segs, p->nsegs);
152 free0:
153 	free(p, M_USB, sizeof *p);
154 	return (USBD_NOMEM);
155 }
156 
157 #if 0
158 void
159 usb_block_real_freemem(struct usb_dma_block *p)
160 {
161 	bus_dmamap_unload(p->tag, p->map);
162 	bus_dmamap_destroy(p->tag, p->map);
163 	bus_dmamem_unmap(p->tag, p->kaddr, p->size);
164 	bus_dmamem_free(p->tag, p->segs, p->nsegs);
165 	free(p, M_USB, sizeof *p);
166 }
167 #endif
168 
169 /*
170  * Do not free the memory unconditionally since we might be called
171  * from an interrupt context and that is BAD.
172  * XXX when should we really free?
173  */
174 void
175 usb_block_freemem(struct usb_dma_block *p)
176 {
177 	int s;
178 
179 	DPRINTFN(6, ("%s: size=%lu\n", __func__, (u_long)p->size));
180 	s = splusb();
181 	LIST_INSERT_HEAD(&usb_blk_freelist, p, next);
182 	usb_blk_nfree++;
183 	splx(s);
184 }
185 
186 usbd_status
187 usb_allocmem(struct usbd_bus *bus, size_t size, size_t align, int flags,
188     struct usb_dma *p)
189 {
190 	bus_dma_tag_t tag = bus->dmatag;
191 	usbd_status err;
192 	struct usb_frag_dma *f;
193 	struct usb_dma_block *b;
194 	int coherent;
195 	int i;
196 	int s;
197 
198 	coherent = !!(flags & USB_DMA_COHERENT);
199 
200 	/* If the request is large then just use a full block. */
201 	if (size > USB_MEM_SMALL || align > USB_MEM_SMALL) {
202 		DPRINTFN(1, ("%s: large alloc %d\n", __func__, (int)size));
203 		size = (size + USB_MEM_BLOCK - 1) & ~(USB_MEM_BLOCK - 1);
204 		err = usb_block_allocmem(tag, size, align, &p->block,
205 		    coherent);
206 		if (!err) {
207 			p->block->frags = NULL;
208 			p->offs = 0;
209 		}
210 		return (err);
211 	}
212 
213 	s = splusb();
214 	/* Check for free fragments. */
215 	for (f = LIST_FIRST(&usb_frag_freelist); f; f = LIST_NEXT(f, next))
216 		if (f->block->tag == tag && f->block->coherent == coherent)
217 			break;
218 	if (f == NULL) {
219 		DPRINTFN(1, ("usb_allocmem: adding fragments\n"));
220 		err = usb_block_allocmem(tag, USB_MEM_BLOCK, USB_MEM_SMALL, &b,
221 		    coherent);
222 		if (err) {
223 			splx(s);
224 			return (err);
225 		}
226 		b->frags = mallocarray(USB_MEM_CHUNKS, sizeof(*f), M_USB,
227 		    M_NOWAIT);
228 		if (b->frags == NULL) {
229 			splx(s);
230 			usb_block_freemem(b);
231 			return (USBD_NOMEM);
232 		}
233 		for (i = 0; i < USB_MEM_CHUNKS; i++) {
234 			f = &b->frags[i];
235 			f->block = b;
236 			f->offs = USB_MEM_SMALL * i;
237 			LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
238 		}
239 		f = LIST_FIRST(&usb_frag_freelist);
240 	}
241 	p->block = f->block;
242 	p->offs = f->offs;
243 	LIST_REMOVE(f, next);
244 	splx(s);
245 	DPRINTFN(5, ("%s: use frag=%p size=%d\n", __func__, f, (int)size));
246 	return (USBD_NORMAL_COMPLETION);
247 }
248 
249 void
250 usb_freemem(struct usbd_bus *bus, struct usb_dma *p)
251 {
252 	struct usb_frag_dma *f;
253 	int s;
254 
255 	if (p->block->frags == NULL) {
256 		DPRINTFN(1, ("usb_freemem: large free\n"));
257 		usb_block_freemem(p->block);
258 		return;
259 	}
260 	s = splusb();
261 	f = &p->block->frags[p->offs / USB_MEM_SMALL];
262 	LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
263 	splx(s);
264 	DPRINTFN(5, ("%s: frag=%p block=%p\n", __func__, f, f->block));
265 }
266 
267 void
268 usb_syncmem(struct usb_dma *p, bus_addr_t offset, bus_size_t len, int ops)
269 {
270 	bus_dmamap_sync(p->block->tag, p->block->map, p->offs + offset,
271 	    len, ops);
272 }
273