The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/usb/usb_mem.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: usb_mem.c,v 1.26 2003/02/01 06:23:40 thorpej Exp $     */
    2 /*      $FreeBSD: releng/5.2/sys/dev/usb/usb_mem.c 120768 2003-10-04 22:13:21Z joe $    */
    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  * 3. All advertising materials mentioning features or use of this software
   21  *    must display the following acknowledgement:
   22  *        This product includes software developed by the NetBSD
   23  *        Foundation, Inc. and its contributors.
   24  * 4. Neither the name of The NetBSD Foundation nor the names of its
   25  *    contributors may be used to endorse or promote products derived
   26  *    from this software without specific prior written permission.
   27  *
   28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   38  * POSSIBILITY OF SUCH DAMAGE.
   39  */
   40 
   41 /*
   42  * USB DMA memory allocation.
   43  * We need to allocate a lot of small (many 8 byte, some larger)
   44  * memory blocks that can be used for DMA.  Using the bus_dma
   45  * routines directly would incur large overheads in space and time.
   46  */
   47 
   48 #include <sys/cdefs.h>
   49 __FBSDID("$FreeBSD: releng/5.2/sys/dev/usb/usb_mem.c 120768 2003-10-04 22:13:21Z joe $");
   50 
   51 #include <sys/param.h>
   52 #include <sys/systm.h>
   53 #include <sys/malloc.h>
   54 #include <sys/kernel.h>
   55 #if defined(__NetBSD__) || defined(__OpenBSD__)
   56 #include <sys/device.h>         /* for usbdivar.h */
   57 #include <machine/bus.h>
   58 #elif defined(__FreeBSD__)
   59 #include <sys/endian.h>
   60 #include <sys/module.h>
   61 #include <sys/bus.h>
   62 #endif
   63 #include <sys/queue.h>
   64 
   65 #include <machine/bus.h>
   66 #include <machine/endian.h>
   67 
   68 #ifdef DIAGNOSTIC
   69 #include <sys/proc.h>
   70 #endif
   71 
   72 #include <dev/usb/usb.h>
   73 #include <dev/usb/usbdi.h>
   74 #include <dev/usb/usbdivar.h>   /* just for usb_dma_t */
   75 #include <dev/usb/usb_mem.h>
   76 
   77 #ifdef USB_DEBUG
   78 #define DPRINTF(x)      if (usbdebug) logprintf x
   79 #define DPRINTFN(n,x)   if (usbdebug>(n)) logprintf x
   80 extern int usbdebug;
   81 #else
   82 #define DPRINTF(x)
   83 #define DPRINTFN(n,x)
   84 #endif
   85 
   86 #define USB_MEM_SMALL 64
   87 #define USB_MEM_CHUNKS (PAGE_SIZE / USB_MEM_SMALL)
   88 #define USB_MEM_BLOCK (USB_MEM_SMALL * USB_MEM_CHUNKS)
   89 
   90 /* This struct is overlayed on free fragments. */
   91 struct usb_frag_dma {
   92         usb_dma_block_t *block;
   93         u_int offs;
   94         LIST_ENTRY(usb_frag_dma) next;
   95 };
   96 
   97 Static bus_dmamap_callback_t usbmem_callback;
   98 Static usbd_status      usb_block_allocmem(bus_dma_tag_t, size_t, size_t,
   99                                            usb_dma_block_t **);
  100 Static void             usb_block_freemem(usb_dma_block_t *);
  101 
  102 Static LIST_HEAD(, usb_dma_block) usb_blk_freelist =
  103         LIST_HEAD_INITIALIZER(usb_blk_freelist);
  104 Static int usb_blk_nfree = 0;
  105 /* XXX should have different free list for different tags (for speed) */
  106 Static LIST_HEAD(, usb_frag_dma) usb_frag_freelist =
  107         LIST_HEAD_INITIALIZER(usb_frag_freelist);
  108 
  109 Static void
  110 usbmem_callback(void *arg, bus_dma_segment_t *segs, int nseg, int error)
  111 {
  112         int i;
  113         usb_dma_block_t *p = arg;
  114 
  115         if (error == EFBIG) {
  116                 printf("usb: mapping to large\n");
  117                 return;
  118         }
  119 
  120         p->nsegs = nseg;
  121         for (i = 0; i < nseg && i < sizeof p->segs / sizeof *p->segs; i++)
  122                 p->segs[i] = segs[i];
  123 }
  124 
  125 Static usbd_status
  126 usb_block_allocmem(bus_dma_tag_t tag, size_t size, size_t align,
  127                    usb_dma_block_t **dmap)
  128 {
  129         usb_dma_block_t *p;
  130         int s;
  131 
  132         DPRINTFN(5, ("usb_block_allocmem: size=%lu align=%lu\n",
  133                      (u_long)size, (u_long)align));
  134 
  135 #ifdef DIAGNOSTIC
  136         if (!curproc) {
  137                 printf("usb_block_allocmem: in interrupt context, size=%lu\n",
  138                     (unsigned long) size);
  139         }
  140 #endif
  141 
  142         s = splusb();
  143         /* First check the free list. */
  144         for (p = LIST_FIRST(&usb_blk_freelist); p; p = LIST_NEXT(p, next)) {
  145                 if (p->tag == tag && p->size >= size && p->align >= align) {
  146                         LIST_REMOVE(p, next);
  147                         usb_blk_nfree--;
  148                         splx(s);
  149                         *dmap = p;
  150                         DPRINTFN(6,("usb_block_allocmem: free list size=%lu\n",
  151                                     (u_long)p->size));
  152                         return (USBD_NORMAL_COMPLETION);
  153                 }
  154         }
  155         splx(s);
  156 
  157 #ifdef DIAGNOSTIC
  158         if (!curproc) {
  159                 printf("usb_block_allocmem: in interrupt context, failed\n");
  160                 return (USBD_NOMEM);
  161         }
  162 #endif
  163 
  164         DPRINTFN(6, ("usb_block_allocmem: no free\n"));
  165         p = malloc(sizeof *p, M_USB, M_NOWAIT);
  166         if (p == NULL)
  167                 return (USBD_NOMEM);
  168 
  169 #if __FreeBSD_version >= 500000
  170         if (bus_dma_tag_create(tag, align, 0,
  171             BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
  172             size, sizeof(p->segs) / sizeof(p->segs[0]), size,
  173             BUS_DMA_ALLOCNOW, NULL, NULL, &p->tag) == ENOMEM)
  174 #else
  175         if (bus_dma_tag_create(tag, align, 0,
  176             BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
  177             size, sizeof(p->segs) / sizeof(p->segs[0]), size,
  178             BUS_DMA_ALLOCNOW, &p->tag) == ENOMEM)
  179 #endif
  180         {
  181                 goto free;
  182         }
  183 
  184         p->size = size;
  185         p->align = align;
  186         if (bus_dmamem_alloc(p->tag, &p->kaddr,
  187             BUS_DMA_NOWAIT|BUS_DMA_COHERENT, &p->map))
  188                 goto tagfree;
  189 
  190         if (bus_dmamap_load(p->tag, p->map, p->kaddr, p->size,
  191             usbmem_callback, p, 0))
  192                 goto memfree;
  193 
  194         /* XXX - override the tag, ok since we never free it */
  195         p->tag = tag;
  196         *dmap = p;
  197         return (USBD_NORMAL_COMPLETION);
  198 
  199         /*
  200          * XXX - do we need to _unload? is the order of _free and _destroy
  201          * correct?
  202          */
  203 memfree:
  204         bus_dmamem_free(p->tag, p->kaddr, p->map);
  205 tagfree:
  206         bus_dma_tag_destroy(p->tag);
  207 free:
  208         free(p, M_USB);
  209         return (USBD_NOMEM);
  210 }
  211 
  212 /*
  213  * Do not free the memory unconditionally since we might be called
  214  * from an interrupt context and that is BAD.
  215  * XXX when should we really free?
  216  */
  217 Static void
  218 usb_block_freemem(usb_dma_block_t *p)
  219 {
  220         int s;
  221 
  222         DPRINTFN(6, ("usb_block_freemem: size=%lu\n", (u_long)p->size));
  223         s = splusb();
  224         LIST_INSERT_HEAD(&usb_blk_freelist, p, next);
  225         usb_blk_nfree++;
  226         splx(s);
  227 }
  228 
  229 usbd_status
  230 usb_allocmem(usbd_bus_handle bus, size_t size, size_t align, usb_dma_t *p)
  231 {
  232         bus_dma_tag_t tag = bus->dmatag;
  233         usbd_status err;
  234         struct usb_frag_dma *f;
  235         usb_dma_block_t *b;
  236         int i;
  237         int s;
  238 
  239         /* compat w/ Net/OpenBSD */
  240         if (align == 0)
  241                 align = 1;
  242 
  243         /* If the request is large then just use a full block. */
  244         if (size > USB_MEM_SMALL || align > USB_MEM_SMALL) {
  245                 DPRINTFN(1, ("usb_allocmem: large alloc %d\n", (int)size));
  246                 size = (size + USB_MEM_BLOCK - 1) & ~(USB_MEM_BLOCK - 1);
  247                 err = usb_block_allocmem(tag, size, align, &p->block);
  248                 if (!err) {
  249                         p->block->fullblock = 1;
  250                         p->offs = 0;
  251                         p->len = size;
  252                 }
  253                 return (err);
  254         }
  255 
  256         s = splusb();
  257         /* Check for free fragments. */
  258         for (f = LIST_FIRST(&usb_frag_freelist); f; f = LIST_NEXT(f, next))
  259                 if (f->block->tag == tag)
  260                         break;
  261         if (f == NULL) {
  262                 DPRINTFN(1, ("usb_allocmem: adding fragments\n"));
  263                 err = usb_block_allocmem(tag, USB_MEM_BLOCK, USB_MEM_SMALL,&b);
  264                 if (err) {
  265                         splx(s);
  266                         return (err);
  267                 }
  268                 b->fullblock = 0;
  269                 /* XXX - override the tag, ok since we never free it */
  270                 b->tag = tag;
  271                 KASSERT(sizeof *f <= USB_MEM_SMALL, ("USB_MEM_SMALL(%d) is too small for struct usb_frag_dma(%zd)\n",
  272                     USB_MEM_SMALL, sizeof *f));
  273                 for (i = 0; i < USB_MEM_BLOCK; i += USB_MEM_SMALL) {
  274                         f = (struct usb_frag_dma *)((char *)b->kaddr + i);
  275                         f->block = b;
  276                         f->offs = i;
  277                         LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
  278                 }
  279                 f = LIST_FIRST(&usb_frag_freelist);
  280         }
  281         p->block = f->block;
  282         p->offs = f->offs;
  283         p->len = USB_MEM_SMALL;
  284         LIST_REMOVE(f, next);
  285         splx(s);
  286         DPRINTFN(5, ("usb_allocmem: use frag=%p size=%d\n", f, (int)size));
  287         return (USBD_NORMAL_COMPLETION);
  288 }
  289 
  290 void
  291 usb_freemem(usbd_bus_handle bus, usb_dma_t *p)
  292 {
  293         struct usb_frag_dma *f;
  294         int s;
  295 
  296         if (p->block->fullblock) {
  297                 DPRINTFN(1, ("usb_freemem: large free\n"));
  298                 usb_block_freemem(p->block);
  299                 return;
  300         }
  301         f = KERNADDR(p, 0);
  302         f->block = p->block;
  303         f->offs = p->offs;
  304         s = splusb();
  305         LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
  306         splx(s);
  307         DPRINTFN(5, ("usb_freemem: frag=%p\n", f));
  308 }

Cache object: a09e08dc286373edf7923544b619b81a


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.