Nuke device_ptr_t, USBBASEDEVICE, USBDEVNAME(), USBDEVUNIT(), USBGETSOFTC(),
[dragonfly/vkernel-mp.git] / sys / bus / usb / usb_mem.c
bloba6aba0fc50290b7a083a79695905b6e7338b7134
1 /*
2 * $NetBSD: usb_mem.c,v 1.26 2003/02/01 06:23:40 thorpej Exp $
3 * $FreeBSD: src/sys/dev/usb/usb_mem.c,v 1.5 2003/10/04 22:13:21 joe Exp $
4 * $DragonFly: src/sys/bus/usb/usb_mem.c,v 1.11 2007/06/28 06:32:31 hasso Exp $
5 */
6 /*
7 * Copyright (c) 1998 The NetBSD Foundation, Inc.
8 * All rights reserved.
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Lennart Augustsson (lennart@augustsson.net) at
12 * Carlstedt Research & Technology.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the NetBSD
25 * Foundation, Inc. and its contributors.
26 * 4. Neither the name of The NetBSD Foundation nor the names of its
27 * contributors may be used to endorse or promote products derived
28 * from this software without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
44 * USB DMA memory allocation.
45 * We need to allocate a lot of small (many 8 byte, some larger)
46 * memory blocks that can be used for DMA. Using the bus_dma
47 * routines directly would incur large overheads in space and time.
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/malloc.h>
52 #include <sys/kernel.h>
53 #include <sys/endian.h>
54 #include <sys/module.h>
55 #include <sys/bus.h>
56 #include <sys/queue.h>
57 #include <sys/sysctl.h>
58 #include <sys/ktr.h>
60 #include <machine/endian.h>
62 #ifdef DIAGNOSTIC
63 #include <sys/proc.h>
64 #endif
65 #include <sys/thread2.h>
67 #include <bus/usb/usb.h>
68 #include <bus/usb/usbdi.h>
69 #include <bus/usb/usbdivar.h> /* just for usb_dma_t */
70 #include <bus/usb/usb_mem.h>
72 #define USBKTR_STRING "ptr=%p bus=%p size=%d align=%d"
73 #define USBKTR_ARG_SIZE (sizeof(void *) * 2 + sizeof(int) * 2)
75 #if !defined(KTR_USB_MEMORY)
76 #define KTR_USB_MEMORY KTR_ALL
77 #endif
78 KTR_INFO_MASTER(usbmem);
79 KTR_INFO(KTR_USB_MEMORY, usbmem, alloc_full, 0, USBKTR_STRING, USBKTR_ARG_SIZE);
80 KTR_INFO(KTR_USB_MEMORY, usbmem, alloc_frag, 0, USBKTR_STRING, USBKTR_ARG_SIZE);
81 KTR_INFO(KTR_USB_MEMORY, usbmem, free_full, 0, USBKTR_STRING, USBKTR_ARG_SIZE);
82 KTR_INFO(KTR_USB_MEMORY, usbmem, free_frag, 0, USBKTR_STRING, USBKTR_ARG_SIZE);
83 KTR_INFO(KTR_USB_MEMORY, usbmem, blkalloc, 0, USBKTR_STRING, USBKTR_ARG_SIZE);
84 KTR_INFO(KTR_USB_MEMORY, usbmem, blkalloc2, 0, USBKTR_STRING, USBKTR_ARG_SIZE);
85 KTR_INFO(KTR_USB_MEMORY, usbmem, blkfree, 0, USBKTR_STRING, USBKTR_ARG_SIZE);
87 #define logmemory(name, ptr, bus, size, align) \
88 KTR_LOG(usbmem_ ## name, ptr, bus, (int)size, (int)align);
90 #ifdef USB_DEBUG
91 #define DPRINTF(x) if (usbdebug) logprintf x
92 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
93 extern int usbdebug;
94 #else
95 #define DPRINTF(x)
96 #define DPRINTFN(n,x)
97 #endif
99 #define USB_MEM_SMALL 64
100 #define USB_MEM_CHUNKS (PAGE_SIZE / USB_MEM_SMALL)
101 #define USB_MEM_BLOCK (USB_MEM_SMALL * USB_MEM_CHUNKS)
103 /* This struct is overlayed on free fragments. */
104 struct usb_frag_dma {
105 usb_dma_block_t *block;
106 u_int offs;
107 LIST_ENTRY(usb_frag_dma) next;
110 static bus_dmamap_callback_t usbmem_callback;
111 static usbd_status usb_block_allocmem(bus_dma_tag_t, size_t, size_t,
112 usb_dma_block_t **);
113 static void usb_block_freemem(usb_dma_block_t *);
115 static LIST_HEAD(, usb_dma_block) usb_blk_freelist =
116 LIST_HEAD_INITIALIZER(usb_blk_freelist);
117 static int usb_blk_nfree = 0;
118 /* XXX should have different free list for different tags (for speed) */
119 static LIST_HEAD(, usb_frag_dma) usb_frag_freelist =
120 LIST_HEAD_INITIALIZER(usb_frag_freelist);
122 static void
123 usbmem_callback(void *arg, bus_dma_segment_t *segs, int nseg, int error)
125 int i;
126 usb_dma_block_t *p = arg;
128 if (error == EFBIG) {
129 kprintf("usb: mapping to large\n");
130 return;
133 p->nsegs = nseg;
134 for (i = 0; i < nseg && i < sizeof p->segs / sizeof *p->segs; i++)
135 p->segs[i] = segs[i];
138 static usbd_status
139 usb_block_allocmem(bus_dma_tag_t tag, size_t size, size_t align,
140 usb_dma_block_t **dmap)
142 usb_dma_block_t *p;
144 DPRINTFN(5, ("usb_block_allocmem: size=%lu align=%lu\n",
145 (u_long)size, (u_long)align));
147 crit_enter();
148 /* First check the free list. */
149 for (p = LIST_FIRST(&usb_blk_freelist); p; p = LIST_NEXT(p, next)) {
150 if (p->tag == tag && p->size >= size && p->align >= align) {
151 LIST_REMOVE(p, next);
152 usb_blk_nfree--;
153 crit_exit();
154 *dmap = p;
155 DPRINTFN(6,("usb_block_allocmem: free list size=%lu\n",
156 (u_long)p->size));
157 logmemory(blkalloc2, p, NULL, size, align);
158 return (USBD_NORMAL_COMPLETION);
161 crit_exit();
163 DPRINTFN(6, ("usb_block_allocmem: no free\n"));
164 p = kmalloc(sizeof *p, M_USB, M_INTWAIT);
165 logmemory(blkalloc, p, NULL, size, align);
167 if (bus_dma_tag_create(tag, align, 0,
168 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
169 size, sizeof(p->segs) / sizeof(p->segs[0]), size,
170 BUS_DMA_ALLOCNOW, &p->tag) == ENOMEM)
172 goto free;
175 p->size = size;
176 p->align = align;
177 if (bus_dmamem_alloc(p->tag, &p->kaddr,
178 BUS_DMA_NOWAIT|BUS_DMA_COHERENT, &p->map))
179 goto tagfree;
181 if (bus_dmamap_load(p->tag, p->map, p->kaddr, p->size,
182 usbmem_callback, p, 0))
183 goto memfree;
185 /* XXX - override the tag, ok since we never free it */
186 p->tag = tag;
187 *dmap = p;
188 return (USBD_NORMAL_COMPLETION);
191 * XXX - do we need to _unload? is the order of _free and _destroy
192 * correct?
194 memfree:
195 bus_dmamem_free(p->tag, p->kaddr, p->map);
196 tagfree:
197 bus_dma_tag_destroy(p->tag);
198 free:
199 kfree(p, M_USB);
200 return (USBD_NOMEM);
204 * Do not free the memory unconditionally since we might be called
205 * from an interrupt context and that is BAD.
206 * XXX when should we really free?
208 static void
209 usb_block_freemem(usb_dma_block_t *p)
211 DPRINTFN(6, ("usb_block_freemem: size=%lu\n", (u_long)p->size));
212 logmemory(blkfree, p, NULL, p->size, p->align);
213 crit_enter();
214 LIST_INSERT_HEAD(&usb_blk_freelist, p, next);
215 usb_blk_nfree++;
216 crit_exit();
219 usbd_status
220 usb_allocmem(usbd_bus_handle bus, size_t size, size_t align, usb_dma_t *p)
222 bus_dma_tag_t tag = bus->dmatag;
223 usbd_status err;
224 struct usb_frag_dma *f;
225 usb_dma_block_t *b;
226 int i;
228 /* compat w/ Net/OpenBSD */
229 if (align == 0)
230 align = 1;
232 /* If the request is large then just use a full block. */
233 if (size > USB_MEM_SMALL || align > USB_MEM_SMALL) {
234 DPRINTFN(1, ("usb_allocmem: large alloc %d\n", (int)size));
235 size = (size + USB_MEM_BLOCK - 1) & ~(USB_MEM_BLOCK - 1);
236 err = usb_block_allocmem(tag, size, align, &p->block);
237 if (!err) {
238 p->block->fullblock = 1;
239 p->offs = 0;
240 p->len = size;
241 logmemory(alloc_full, p, NULL, size, align);
243 return (err);
246 crit_enter();
247 /* Check for free fragments. */
248 for (f = LIST_FIRST(&usb_frag_freelist); f; f = LIST_NEXT(f, next))
249 if (f->block->tag == tag)
250 break;
251 if (f == NULL) {
252 DPRINTFN(1, ("usb_allocmem: adding fragments\n"));
253 err = usb_block_allocmem(tag, USB_MEM_BLOCK, USB_MEM_SMALL,&b);
254 if (err) {
255 crit_exit();
256 return (err);
258 b->fullblock = 0;
259 /* XXX - override the tag, ok since we never free it */
260 b->tag = tag;
261 KASSERT(sizeof *f <= USB_MEM_SMALL, ("USB_MEM_SMALL(%d) is too small for struct usb_frag_dma(%zd)\n",
262 USB_MEM_SMALL, sizeof *f));
263 for (i = 0; i < USB_MEM_BLOCK; i += USB_MEM_SMALL) {
264 f = (struct usb_frag_dma *)((char *)b->kaddr + i);
265 f->block = b;
266 f->offs = i;
267 LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
269 f = LIST_FIRST(&usb_frag_freelist);
271 p->block = f->block;
272 p->offs = f->offs;
273 p->len = USB_MEM_SMALL;
274 LIST_REMOVE(f, next);
275 crit_exit();
276 logmemory(alloc_frag, p, NULL, size, align);
277 DPRINTFN(5, ("usb_allocmem: use frag=%p size=%d\n", f, (int)size));
278 return (USBD_NORMAL_COMPLETION);
281 void
282 usb_freemem(usbd_bus_handle bus, usb_dma_t *p)
284 struct usb_frag_dma *f;
286 if (p->block->fullblock) {
287 DPRINTFN(1, ("usb_freemem: large free\n"));
288 usb_block_freemem(p->block);
289 logmemory(free_full, p, bus, 0, 0);
290 return;
292 logmemory(free_frag, p, bus, 0, 0);
293 f = KERNADDR(p, 0);
294 f->block = p->block;
295 f->offs = p->offs;
296 crit_enter();
297 LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
298 crit_exit();
299 DPRINTFN(5, ("usb_freemem: frag=%p\n", f));