2003-02-01 09:23:35 +03:00
|
|
|
/* $NetBSD: usb_mem.c,v 1.26 2003/02/01 06:23:40 thorpej Exp $ */
|
1998-07-25 01:09:07 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1998 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
1998-11-26 01:32:04 +03:00
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
2000-04-27 19:26:44 +04:00
|
|
|
* by Lennart Augustsson (lennart@augustsson.net) at
|
1998-11-26 01:32:04 +03:00
|
|
|
* Carlstedt Research & Technology.
|
1998-07-25 01:09:07 +04:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* USB DMA memory allocation.
|
|
|
|
* We need to allocate a lot of small (many 8 byte, some larger)
|
|
|
|
* memory blocks that can be used for DMA. Using the bus_dma
|
1998-12-09 04:02:29 +03:00
|
|
|
* routines directly would incur large overheads in space and time.
|
1998-07-25 01:09:07 +04:00
|
|
|
*/
|
|
|
|
|
2001-11-13 09:24:53 +03:00
|
|
|
#include <sys/cdefs.h>
|
2003-02-01 09:23:35 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: usb_mem.c,v 1.26 2003/02/01 06:23:40 thorpej Exp $");
|
2001-11-13 09:24:53 +03:00
|
|
|
|
1998-07-25 01:09:07 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/queue.h>
|
1999-09-09 16:26:43 +04:00
|
|
|
#include <sys/device.h> /* for usbdivar.h */
|
1999-09-13 23:18:17 +04:00
|
|
|
#include <machine/bus.h>
|
1998-07-25 01:09:07 +04:00
|
|
|
|
1998-12-09 04:02:29 +03:00
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#endif
|
|
|
|
|
1998-07-25 01:09:07 +04:00
|
|
|
#include <dev/usb/usb.h>
|
|
|
|
#include <dev/usb/usbdi.h>
|
1999-09-09 16:26:43 +04:00
|
|
|
#include <dev/usb/usbdivar.h> /* just for usb_dma_t */
|
1998-07-25 01:09:07 +04:00
|
|
|
#include <dev/usb/usb_mem.h>
|
|
|
|
|
|
|
|
#ifdef USB_DEBUG
|
1999-08-14 18:49:31 +04:00
|
|
|
#define DPRINTF(x) if (usbdebug) logprintf x
|
|
|
|
#define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
|
2002-12-30 05:44:32 +03:00
|
|
|
extern int usbdebug;
|
1998-07-25 01:09:07 +04:00
|
|
|
#else
|
|
|
|
#define DPRINTF(x)
|
|
|
|
#define DPRINTFN(n,x)
|
|
|
|
#endif
|
|
|
|
|
2003-02-01 09:23:35 +03:00
|
|
|
MALLOC_DEFINE(M_USB, "USB", "USB misc. memory");
|
|
|
|
MALLOC_DEFINE(M_USBDEV, "USB device", "USB device driver");
|
|
|
|
MALLOC_DEFINE(M_USBHC, "USB HC", "USB host controller");
|
|
|
|
|
1998-07-25 01:09:07 +04:00
|
|
|
#define USB_MEM_SMALL 64
|
|
|
|
#define USB_MEM_CHUNKS 64
|
|
|
|
#define USB_MEM_BLOCK (USB_MEM_SMALL * USB_MEM_CHUNKS)
|
|
|
|
|
|
|
|
/* This struct is overlayed on free fragments. */
|
|
|
|
struct usb_frag_dma {
|
|
|
|
usb_dma_block_t *block;
|
|
|
|
u_int offs;
|
|
|
|
LIST_ENTRY(usb_frag_dma) next;
|
|
|
|
};
|
|
|
|
|
2000-06-01 18:28:57 +04:00
|
|
|
Static usbd_status usb_block_allocmem(bus_dma_tag_t, size_t, size_t,
|
|
|
|
usb_dma_block_t **);
|
|
|
|
Static void usb_block_freemem(usb_dma_block_t *);
|
1998-07-25 01:09:07 +04:00
|
|
|
|
2002-07-12 01:14:24 +04:00
|
|
|
Static LIST_HEAD(, usb_dma_block) usb_blk_freelist =
|
1998-07-25 01:09:07 +04:00
|
|
|
LIST_HEAD_INITIALIZER(usb_blk_freelist);
|
2000-03-27 16:33:53 +04:00
|
|
|
Static int usb_blk_nfree = 0;
|
1999-08-23 00:12:39 +04:00
|
|
|
/* XXX should have different free list for different tags (for speed) */
|
2000-03-27 16:33:53 +04:00
|
|
|
Static LIST_HEAD(, usb_frag_dma) usb_frag_freelist =
|
1998-07-25 01:09:07 +04:00
|
|
|
LIST_HEAD_INITIALIZER(usb_frag_freelist);
|
|
|
|
|
2000-03-27 16:33:53 +04:00
|
|
|
Static usbd_status
|
2000-06-01 18:28:57 +04:00
|
|
|
usb_block_allocmem(bus_dma_tag_t tag, size_t size, size_t align,
|
|
|
|
usb_dma_block_t **dmap)
|
1998-07-25 01:09:07 +04:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
usb_dma_block_t *p;
|
1998-12-11 03:05:07 +03:00
|
|
|
int s;
|
1998-07-25 01:09:07 +04:00
|
|
|
|
2002-07-12 01:14:24 +04:00
|
|
|
DPRINTFN(5, ("usb_block_allocmem: size=%lu align=%lu\n",
|
1999-12-19 01:47:11 +03:00
|
|
|
(u_long)size, (u_long)align));
|
1998-07-25 01:09:07 +04:00
|
|
|
|
1998-12-09 04:02:29 +03:00
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
if (!curproc) {
|
1999-08-20 01:29:04 +04:00
|
|
|
printf("usb_block_allocmem: in interrupt context, size=%lu\n",
|
|
|
|
(unsigned long) size);
|
1998-12-09 04:02:29 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1998-12-11 03:05:07 +03:00
|
|
|
s = splusb();
|
1998-07-25 01:09:07 +04:00
|
|
|
/* First check the free list. */
|
|
|
|
for (p = LIST_FIRST(&usb_blk_freelist); p; p = LIST_NEXT(p, next)) {
|
|
|
|
if (p->tag == tag && p->size >= size && p->align >= align) {
|
|
|
|
LIST_REMOVE(p, next);
|
2000-03-27 12:27:03 +04:00
|
|
|
usb_blk_nfree--;
|
1998-12-11 03:05:07 +03:00
|
|
|
splx(s);
|
1998-07-25 01:09:07 +04:00
|
|
|
*dmap = p;
|
1999-12-19 01:47:11 +03:00
|
|
|
DPRINTFN(6,("usb_block_allocmem: free list size=%lu\n",
|
|
|
|
(u_long)p->size));
|
1998-07-25 01:09:07 +04:00
|
|
|
return (USBD_NORMAL_COMPLETION);
|
|
|
|
}
|
|
|
|
}
|
1998-12-11 03:05:07 +03:00
|
|
|
splx(s);
|
1998-07-25 01:09:07 +04:00
|
|
|
|
1999-08-17 00:19:55 +04:00
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
if (!curproc) {
|
|
|
|
printf("usb_block_allocmem: in interrupt context, failed\n");
|
|
|
|
return (USBD_NOMEM);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1998-07-25 01:09:07 +04:00
|
|
|
DPRINTFN(6, ("usb_block_allocmem: no free\n"));
|
|
|
|
p = malloc(sizeof *p, M_USB, M_NOWAIT);
|
1999-11-12 03:34:57 +03:00
|
|
|
if (p == NULL)
|
1998-07-25 01:09:07 +04:00
|
|
|
return (USBD_NOMEM);
|
|
|
|
*dmap = p;
|
|
|
|
|
|
|
|
p->tag = tag;
|
|
|
|
p->size = size;
|
|
|
|
p->align = align;
|
|
|
|
error = bus_dmamem_alloc(tag, p->size, align, 0,
|
|
|
|
p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
|
|
|
|
&p->nsegs, BUS_DMA_NOWAIT);
|
|
|
|
if (error)
|
|
|
|
return (USBD_NOMEM);
|
|
|
|
|
2002-07-12 01:14:24 +04:00
|
|
|
error = bus_dmamem_map(tag, p->segs, p->nsegs, p->size,
|
1998-07-25 01:09:07 +04:00
|
|
|
&p->kaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
|
|
|
|
if (error)
|
|
|
|
goto free;
|
|
|
|
|
|
|
|
error = bus_dmamap_create(tag, p->size, 1, p->size,
|
|
|
|
0, BUS_DMA_NOWAIT, &p->map);
|
|
|
|
if (error)
|
|
|
|
goto unmap;
|
|
|
|
|
2002-07-12 01:14:24 +04:00
|
|
|
error = bus_dmamap_load(tag, p->map, p->kaddr, p->size, NULL,
|
1998-07-25 01:09:07 +04:00
|
|
|
BUS_DMA_NOWAIT);
|
|
|
|
if (error)
|
|
|
|
goto destroy;
|
1999-11-12 03:34:57 +03:00
|
|
|
return (USBD_NORMAL_COMPLETION);
|
1998-07-25 01:09:07 +04:00
|
|
|
|
|
|
|
destroy:
|
|
|
|
bus_dmamap_destroy(tag, p->map);
|
|
|
|
unmap:
|
|
|
|
bus_dmamem_unmap(tag, p->kaddr, p->size);
|
|
|
|
free:
|
|
|
|
bus_dmamem_free(tag, p->segs, p->nsegs);
|
|
|
|
return (USBD_NOMEM);
|
|
|
|
}
|
|
|
|
|
1999-08-23 00:12:39 +04:00
|
|
|
#if 0
|
1998-07-25 01:09:07 +04:00
|
|
|
void
|
2000-06-01 18:28:57 +04:00
|
|
|
usb_block_real_freemem(usb_dma_block_t *p)
|
1998-07-25 01:09:07 +04:00
|
|
|
{
|
1998-12-09 04:02:29 +03:00
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
if (!curproc) {
|
|
|
|
printf("usb_block_real_freemem: in interrupt context\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
1998-07-25 01:09:07 +04:00
|
|
|
bus_dmamap_unload(p->tag, p->map);
|
|
|
|
bus_dmamap_destroy(p->tag, p->map);
|
|
|
|
bus_dmamem_unmap(p->tag, p->kaddr, p->size);
|
|
|
|
bus_dmamem_free(p->tag, p->segs, p->nsegs);
|
|
|
|
free(p, M_USB);
|
|
|
|
}
|
1999-08-23 00:12:39 +04:00
|
|
|
#endif
|
1998-07-25 01:09:07 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Do not free the memory unconditionally since we might be called
|
|
|
|
* from an interrupt context and that is BAD.
|
1998-12-11 03:05:07 +03:00
|
|
|
* XXX when should we really free?
|
1998-07-25 01:09:07 +04:00
|
|
|
*/
|
2000-03-27 16:33:53 +04:00
|
|
|
Static void
|
2000-06-01 18:28:57 +04:00
|
|
|
usb_block_freemem(usb_dma_block_t *p)
|
1998-07-25 01:09:07 +04:00
|
|
|
{
|
1998-12-11 03:05:07 +03:00
|
|
|
int s;
|
|
|
|
|
1999-12-19 01:47:11 +03:00
|
|
|
DPRINTFN(6, ("usb_block_freemem: size=%lu\n", (u_long)p->size));
|
1998-12-11 03:05:07 +03:00
|
|
|
s = splusb();
|
1998-07-25 01:09:07 +04:00
|
|
|
LIST_INSERT_HEAD(&usb_blk_freelist, p, next);
|
2000-03-27 12:27:03 +04:00
|
|
|
usb_blk_nfree++;
|
1998-12-11 03:05:07 +03:00
|
|
|
splx(s);
|
1998-07-25 01:09:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
usbd_status
|
2000-06-01 18:28:57 +04:00
|
|
|
usb_allocmem(usbd_bus_handle bus, size_t size, size_t align, usb_dma_t *p)
|
1998-07-25 01:09:07 +04:00
|
|
|
{
|
1999-09-13 23:18:17 +04:00
|
|
|
bus_dma_tag_t tag = bus->dmatag;
|
1999-11-12 03:34:57 +03:00
|
|
|
usbd_status err;
|
1998-07-25 01:09:07 +04:00
|
|
|
struct usb_frag_dma *f;
|
|
|
|
usb_dma_block_t *b;
|
|
|
|
int i;
|
1998-12-09 04:02:29 +03:00
|
|
|
int s;
|
1998-07-25 01:09:07 +04:00
|
|
|
|
|
|
|
/* If the request is large then just use a full block. */
|
|
|
|
if (size > USB_MEM_SMALL || align > USB_MEM_SMALL) {
|
|
|
|
DPRINTFN(1, ("usb_allocmem: large alloc %d\n", (int)size));
|
|
|
|
size = (size + USB_MEM_BLOCK - 1) & ~(USB_MEM_BLOCK - 1);
|
1999-11-12 03:34:57 +03:00
|
|
|
err = usb_block_allocmem(tag, size, align, &p->block);
|
|
|
|
if (!err) {
|
1998-07-25 01:09:07 +04:00
|
|
|
p->block->fullblock = 1;
|
|
|
|
p->offs = 0;
|
|
|
|
}
|
1999-11-12 03:34:57 +03:00
|
|
|
return (err);
|
1998-07-25 01:09:07 +04:00
|
|
|
}
|
2002-07-12 01:14:24 +04:00
|
|
|
|
1998-12-09 04:02:29 +03:00
|
|
|
s = splusb();
|
1998-07-25 01:09:07 +04:00
|
|
|
/* Check for free fragments. */
|
|
|
|
for (f = LIST_FIRST(&usb_frag_freelist); f; f = LIST_NEXT(f, next))
|
|
|
|
if (f->block->tag == tag)
|
|
|
|
break;
|
1999-11-12 03:34:57 +03:00
|
|
|
if (f == NULL) {
|
1998-07-25 01:09:07 +04:00
|
|
|
DPRINTFN(1, ("usb_allocmem: adding fragments\n"));
|
1999-11-12 03:34:57 +03:00
|
|
|
err = usb_block_allocmem(tag, USB_MEM_BLOCK, USB_MEM_SMALL,&b);
|
|
|
|
if (err) {
|
1998-12-15 02:44:04 +03:00
|
|
|
splx(s);
|
1999-11-12 03:34:57 +03:00
|
|
|
return (err);
|
1998-12-15 02:44:04 +03:00
|
|
|
}
|
|
|
|
b->fullblock = 0;
|
1998-07-25 01:09:07 +04:00
|
|
|
for (i = 0; i < USB_MEM_BLOCK; i += USB_MEM_SMALL) {
|
|
|
|
f = (struct usb_frag_dma *)(b->kaddr + i);
|
|
|
|
f->block = b;
|
|
|
|
f->offs = i;
|
|
|
|
LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
|
|
|
|
}
|
|
|
|
f = LIST_FIRST(&usb_frag_freelist);
|
|
|
|
}
|
|
|
|
p->block = f->block;
|
|
|
|
p->offs = f->offs;
|
|
|
|
LIST_REMOVE(f, next);
|
1998-12-09 04:02:29 +03:00
|
|
|
splx(s);
|
1998-07-25 01:09:07 +04:00
|
|
|
DPRINTFN(5, ("usb_allocmem: use frag=%p size=%d\n", f, (int)size));
|
|
|
|
return (USBD_NORMAL_COMPLETION);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-06-01 18:28:57 +04:00
|
|
|
usb_freemem(usbd_bus_handle bus, usb_dma_t *p)
|
1998-07-25 01:09:07 +04:00
|
|
|
{
|
|
|
|
struct usb_frag_dma *f;
|
1998-12-09 04:02:29 +03:00
|
|
|
int s;
|
1998-07-25 01:09:07 +04:00
|
|
|
|
|
|
|
if (p->block->fullblock) {
|
1999-08-02 23:49:50 +04:00
|
|
|
DPRINTFN(1, ("usb_freemem: large free\n"));
|
1998-07-25 01:09:07 +04:00
|
|
|
usb_block_freemem(p->block);
|
|
|
|
return;
|
|
|
|
}
|
2002-05-19 10:24:30 +04:00
|
|
|
f = KERNADDR(p, 0);
|
1998-07-25 01:09:07 +04:00
|
|
|
f->block = p->block;
|
|
|
|
f->offs = p->offs;
|
1998-12-09 04:02:29 +03:00
|
|
|
s = splusb();
|
1998-07-25 01:09:07 +04:00
|
|
|
LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
|
1998-12-09 04:02:29 +03:00
|
|
|
splx(s);
|
1998-07-25 01:09:07 +04:00
|
|
|
DPRINTFN(5, ("usb_freemem: frag=%p\n", f));
|
|
|
|
}
|