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/kern/subr_busdma_bufalloc.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2012 Ian Lepore
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  */
   26 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD: releng/10.1/sys/kern/subr_busdma_bufalloc.c 254025 2013-08-07 06:21:20Z jeff $");
   29 
   30 /*
   31  * Buffer allocation support routines for bus_dmamem_alloc implementations.
   32  */
   33 
   34 #include <sys/param.h>
   35 #include <sys/systm.h>
   36 #include <sys/bus.h>
   37 #include <sys/busdma_bufalloc.h>
   38 #include <sys/malloc.h>
   39 
   40 #include <vm/vm.h>
   41 #include <vm/vm_extern.h>
   42 #include <vm/vm_kern.h>
   43 #include <vm/uma.h>
   44 
   45 /*
   46  * We manage buffer zones up to a page in size.  Buffers larger than a page can
   47  * be managed by one of the kernel's page-oriented memory allocation routines as
   48  * efficiently as what we can do here.  Also, a page is the largest size for
   49  * which we can g'tee contiguity when using uma, and contiguity is one of the
   50  * requirements we have to fulfill.
   51  */
   52 #define MIN_ZONE_BUFSIZE        32
   53 #define MAX_ZONE_BUFSIZE        PAGE_SIZE
   54 
   55 /*
   56  * The static array of 12 bufzones is big enough to handle all the zones for the
   57  * smallest supported allocation size of 32 through the largest supported page
   58  * size of 64K.  If you up the biggest page size number, up the array size too.
   59  * Basically the size of the array needs to be log2(maxsize)-log2(minsize)+1,
   60  * but I don't know of an easy way to express that as a compile-time constant.
   61  */
   62 #if PAGE_SIZE > 65536
   63 #error Unsupported page size
   64 #endif
   65 
   66 struct busdma_bufalloc {
   67         bus_size_t              min_size;
   68         size_t                  num_zones;
   69         struct busdma_bufzone   buf_zones[12];
   70 };
   71 
   72 busdma_bufalloc_t 
   73 busdma_bufalloc_create(const char *name, bus_size_t minimum_alignment,
   74     uma_alloc alloc_func, uma_free free_func, u_int32_t zcreate_flags)
   75 {
   76         struct busdma_bufalloc *ba;
   77         struct busdma_bufzone *bz;
   78         int i;
   79         bus_size_t cursize;
   80 
   81         ba = malloc(sizeof(struct busdma_bufalloc), M_DEVBUF, 
   82             M_ZERO | M_WAITOK);
   83 
   84         ba->min_size = MAX(MIN_ZONE_BUFSIZE, minimum_alignment);
   85 
   86         /*
   87          * Each uma zone is created with an alignment of size-1, meaning that
   88          * the alignment is equal to the size (I.E., 64 byte buffers are aligned
   89          * to 64 byte boundaries, etc).  This allows for a fast efficient test
   90          * when deciding whether a pool buffer meets the constraints of a given
   91          * tag used for allocation: the buffer is usable if tag->alignment <=
   92          * bufzone->size.
   93          */
   94         for (i = 0, bz = ba->buf_zones, cursize = ba->min_size;
   95             i < nitems(ba->buf_zones) && cursize <= MAX_ZONE_BUFSIZE;
   96             ++i, ++bz, cursize <<= 1) {
   97                 snprintf(bz->name, sizeof(bz->name), "dma %.10s %lu",
   98                     name, cursize);
   99                 bz->size = cursize;
  100                 bz->umazone = uma_zcreate(bz->name, bz->size,
  101                     NULL, NULL, NULL, NULL, bz->size - 1, zcreate_flags);
  102                 if (bz->umazone == NULL) {
  103                         busdma_bufalloc_destroy(ba);
  104                         return (NULL);
  105                 }
  106                 if (alloc_func != NULL)
  107                         uma_zone_set_allocf(bz->umazone, alloc_func);
  108                 if (free_func != NULL)
  109                         uma_zone_set_freef(bz->umazone, free_func);
  110                 ++ba->num_zones;
  111         }
  112 
  113         return (ba);
  114 }
  115 
  116 void 
  117 busdma_bufalloc_destroy(busdma_bufalloc_t ba)
  118 {
  119         struct busdma_bufzone *bz;
  120         int i;
  121 
  122         if (ba == NULL)
  123                 return;
  124 
  125         for (i = 0, bz = ba->buf_zones; i < ba->num_zones; ++i, ++bz) {
  126                 uma_zdestroy(bz->umazone);
  127         }
  128 
  129         free(ba, M_DEVBUF);
  130 }
  131 
  132 struct busdma_bufzone * 
  133 busdma_bufalloc_findzone(busdma_bufalloc_t ba, bus_size_t size)
  134 {
  135         struct busdma_bufzone *bz;
  136         int i;
  137 
  138         if (size > MAX_ZONE_BUFSIZE)
  139                 return (NULL);
  140 
  141         for (i = 0, bz = ba->buf_zones; i < ba->num_zones; ++i, ++bz) {
  142                 if (bz->size >= size)
  143                         return (bz);
  144         }
  145 
  146         panic("Didn't find a buffer zone of the right size");
  147 }
  148 
  149 void *
  150 busdma_bufalloc_alloc_uncacheable(uma_zone_t zone, int size, u_int8_t *pflag,
  151     int wait)
  152 {
  153 #ifdef VM_MEMATTR_UNCACHEABLE
  154 
  155         /* Inform UMA that this allocator uses kernel_arena/object. */
  156         *pflag = UMA_SLAB_KERNEL;
  157 
  158         return ((void *)kmem_alloc_attr(kernel_arena, size, wait, 0,
  159             BUS_SPACE_MAXADDR, VM_MEMATTR_UNCACHEABLE));
  160 
  161 #else
  162 
  163         panic("VM_MEMATTR_UNCACHEABLE unavailable");
  164 
  165 #endif  /* VM_MEMATTR_UNCACHEABLE */
  166 }
  167 
  168 void 
  169 busdma_bufalloc_free_uncacheable(void *item, int size, u_int8_t pflag)
  170 {
  171 
  172         kmem_free(kernel_arena, (vm_offset_t)item, size);
  173 }
  174 

Cache object: 2447e0c8b1e005bcdebf1c608b83179c


[ 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.