1 /* $OpenBSD: usb_mem.c,v 1.36 2024/10/08 19:42:31 kettenis 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/malloc.h>
44 #include <sys/queue.h>
45 #include <sys/device.h> /* for usbdivar.h */
46 #include <machine/bus.h>
47
48 #include <dev/usb/usb.h>
49 #include <dev/usb/usbdi.h>
50 #include <dev/usb/usbdivar.h> /* just for struct usb_dma */
51 #include <dev/usb/usb_mem.h>
52
53 #ifdef USB_DEBUG
54 #define DPRINTF(x) do { if (usbdebug) printf x; } while (0)
55 #define DPRINTFN(n,x) do { if (usbdebug>(n)) printf x; } while (0)
56 extern int usbdebug;
57 #else
58 #define DPRINTF(x)
59 #define DPRINTFN(n,x)
60 #endif
61
62 #define USB_MEM_SMALL 64
63 #define USB_MEM_CHUNKS 64
64 #define USB_MEM_BLOCK (USB_MEM_SMALL * USB_MEM_CHUNKS)
65
66 struct usb_frag_dma {
67 struct usb_dma_block *block;
68 u_int offs;
69 LIST_ENTRY(usb_frag_dma) next;
70 };
71
72 usbd_status usb_block_allocmem(bus_dma_tag_t, size_t, size_t,
73 struct usb_dma_block **, int);
74 void usb_block_freemem(struct usb_dma_block *);
75
76 LIST_HEAD(, usb_dma_block) usb_blk_freelist =
77 LIST_HEAD_INITIALIZER(usb_blk_freelist);
78 int usb_blk_nfree = 0;
79 /* XXX should have different free list for different tags (for speed) */
80 LIST_HEAD(, usb_frag_dma) usb_frag_freelist =
81 LIST_HEAD_INITIALIZER(usb_frag_freelist);
82
83 usbd_status
usb_block_allocmem(bus_dma_tag_t tag,size_t size,size_t align,struct usb_dma_block ** dmap,int flags)84 usb_block_allocmem(bus_dma_tag_t tag, size_t size, size_t align,
85 struct usb_dma_block **dmap, int flags)
86 {
87 int error;
88 struct usb_dma_block *p;
89 int s;
90
91 DPRINTFN(5, ("%s: size=%lu align=%lu\n", __func__,
92 (u_long)size, (u_long)align));
93
94 s = splusb();
95 /* First check the free list. */
96 for (p = LIST_FIRST(&usb_blk_freelist); p; p = LIST_NEXT(p, next)) {
97 if (p->tag == tag && p->size >= size && p->align >= align &&
98 p->flags == flags) {
99 LIST_REMOVE(p, next);
100 usb_blk_nfree--;
101 splx(s);
102 *dmap = p;
103 DPRINTFN(6,("%s: free list size=%lu\n", __func__,
104 (u_long)p->size));
105 return (USBD_NORMAL_COMPLETION);
106 }
107 }
108 splx(s);
109
110 DPRINTFN(6, ("usb_block_allocmem: no free\n"));
111 p = malloc(sizeof *p, M_USB, M_NOWAIT);
112 if (p == NULL)
113 return (USBD_NOMEM);
114
115 p->tag = tag;
116 p->size = size;
117 p->align = align;
118 p->flags = flags;
119 error = bus_dmamem_alloc(tag, p->size, align, 0,
120 p->segs, nitems(p->segs), &p->nsegs,
121 BUS_DMA_NOWAIT | (flags & BUS_DMA_64BIT));
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 | (flags & 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 | (flags & BUS_DMA_64BIT), &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
usb_block_freemem(struct usb_dma_block * p)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
usb_allocmem(struct usbd_bus * bus,size_t size,size_t align,int flags,struct usb_dma * p)184 usb_allocmem(struct usbd_bus *bus, size_t size, size_t align, int flags,
185 struct usb_dma *p)
186 {
187 bus_dma_tag_t tag = bus->dmatag;
188 usbd_status err;
189 struct usb_frag_dma *f;
190 struct usb_dma_block *b;
191 int i;
192 int s;
193
194 flags = (flags & USB_DMA_COHERENT) ? BUS_DMA_COHERENT : 0;
195 flags |= bus->dmaflags;
196
197 /* If the request is large then just use a full block. */
198 if (size > USB_MEM_SMALL || align > USB_MEM_SMALL) {
199 DPRINTFN(1, ("%s: large alloc %d\n", __func__, (int)size));
200 size = (size + USB_MEM_BLOCK - 1) & ~(USB_MEM_BLOCK - 1);
201 err = usb_block_allocmem(tag, size, align, &p->block, flags);
202 if (!err) {
203 p->block->frags = NULL;
204 p->offs = 0;
205 }
206 return (err);
207 }
208
209 s = splusb();
210 /* Check for free fragments. */
211 for (f = LIST_FIRST(&usb_frag_freelist); f; f = LIST_NEXT(f, next))
212 if (f->block->tag == tag && f->block->flags == flags)
213 break;
214 if (f == NULL) {
215 DPRINTFN(1, ("usb_allocmem: adding fragments\n"));
216 err = usb_block_allocmem(tag, USB_MEM_BLOCK, USB_MEM_SMALL, &b,
217 flags);
218 if (err) {
219 splx(s);
220 return (err);
221 }
222 b->frags = mallocarray(USB_MEM_CHUNKS, sizeof(*f), M_USB,
223 M_NOWAIT);
224 if (b->frags == NULL) {
225 splx(s);
226 usb_block_freemem(b);
227 return (USBD_NOMEM);
228 }
229 for (i = 0; i < USB_MEM_CHUNKS; i++) {
230 f = &b->frags[i];
231 f->block = b;
232 f->offs = USB_MEM_SMALL * i;
233 LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
234 }
235 f = LIST_FIRST(&usb_frag_freelist);
236 }
237 p->block = f->block;
238 p->offs = f->offs;
239 LIST_REMOVE(f, next);
240 splx(s);
241 DPRINTFN(5, ("%s: use frag=%p size=%d\n", __func__, f, (int)size));
242 return (USBD_NORMAL_COMPLETION);
243 }
244
245 void
usb_freemem(struct usbd_bus * bus,struct usb_dma * p)246 usb_freemem(struct usbd_bus *bus, struct usb_dma *p)
247 {
248 struct usb_frag_dma *f;
249 int s;
250
251 if (p->block->frags == NULL) {
252 DPRINTFN(1, ("usb_freemem: large free\n"));
253 usb_block_freemem(p->block);
254 return;
255 }
256 s = splusb();
257 f = &p->block->frags[p->offs / USB_MEM_SMALL];
258 LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
259 splx(s);
260 DPRINTFN(5, ("%s: frag=%p block=%p\n", __func__, f, f->block));
261 }
262
263 void
usb_syncmem(struct usb_dma * p,bus_addr_t offset,bus_size_t len,int ops)264 usb_syncmem(struct usb_dma *p, bus_addr_t offset, bus_size_t len, int ops)
265 {
266 bus_dmamap_sync(p->block->tag, p->block->map, p->offs + offset,
267 len, ops);
268 }
269