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/arm/arm/devmap.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 /*-
    2  * Copyright (c) 2013 Ian Lepore <ian@freebsd.org>
    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.2/sys/arm/arm/devmap.c 266086 2014-05-14 20:17:31Z ian $");
   29 
   30 /*
   31  * Routines for mapping device memory.
   32  */
   33 
   34 #include "opt_ddb.h"
   35 
   36 #include <sys/param.h>
   37 #include <sys/systm.h>
   38 #include <vm/vm.h>
   39 #include <vm/vm_extern.h>
   40 #include <vm/pmap.h>
   41 #include <machine/armreg.h>
   42 #include <machine/devmap.h>
   43 
   44 static const struct arm_devmap_entry *devmap_table;
   45 static boolean_t devmap_bootstrap_done = false;
   46 
   47 /*
   48  * The allocated-kva (akva) devmap table and metadata.  Platforms can call
   49  * arm_devmap_add_entry() to add static device mappings to this table using
   50  * automatically allocated virtual addresses carved out of the top of kva space.
   51  * Allocation begins immediately below the ARM_VECTORS_HIGH address.
   52  */
   53 #define AKVA_DEVMAP_MAX_ENTRIES 32
   54 static struct arm_devmap_entry  akva_devmap_entries[AKVA_DEVMAP_MAX_ENTRIES];
   55 static u_int                    akva_devmap_idx;
   56 static vm_offset_t              akva_devmap_vaddr = ARM_VECTORS_HIGH;
   57 
   58 /*
   59  * Print the contents of the static mapping table using the provided printf-like
   60  * output function (which will be either printf or db_printf).
   61  */
   62 static void
   63 devmap_dump_table(int (*prfunc)(const char *, ...))
   64 {
   65         const struct arm_devmap_entry *pd;
   66 
   67         if (devmap_table == NULL || devmap_table[0].pd_size == 0) {
   68                 prfunc("No static device mappings.\n");
   69                 return;
   70         }
   71 
   72         prfunc("Static device mappings:\n");
   73         for (pd = devmap_table; pd->pd_size != 0; ++pd) {
   74                 prfunc("  0x%08x - 0x%08x mapped at VA 0x%08x\n",
   75                     pd->pd_pa, pd->pd_pa + pd->pd_size - 1, pd->pd_va);
   76         }
   77 }
   78 
   79 /*
   80  * Print the contents of the static mapping table.  Used for bootverbose.
   81  */
   82 void
   83 arm_devmap_print_table()
   84 {
   85         devmap_dump_table(printf);
   86 }
   87 
   88 /*
   89  * Return the "last" kva address used by the registered devmap table.  It's
   90  * actually the lowest address used by the static mappings, i.e., the address of
   91  * the first unusable byte of KVA.
   92  */
   93 vm_offset_t
   94 arm_devmap_lastaddr()
   95 {
   96         const struct arm_devmap_entry *pd;
   97         vm_offset_t lowaddr;
   98 
   99         if (akva_devmap_idx > 0)
  100                 return (akva_devmap_vaddr);
  101 
  102         lowaddr = ARM_VECTORS_HIGH;
  103         for (pd = devmap_table; pd != NULL && pd->pd_size != 0; ++pd) {
  104                 if (lowaddr > pd->pd_va)
  105                         lowaddr = pd->pd_va;
  106         }
  107 
  108         return (lowaddr);
  109 }
  110 
  111 /*
  112  * Add an entry to the internal "akva" static devmap table using the given
  113  * physical address and size and a virtual address allocated from the top of
  114  * kva.  This automatically registers the akva table on the first call, so all a
  115  * platform has to do is call this routine to install as many mappings as it
  116  * needs and when initarm() calls arm_devmap_bootstrap() it will pick up all the
  117  * entries in the akva table automatically.
  118  */
  119 void
  120 arm_devmap_add_entry(vm_paddr_t pa, vm_size_t sz)
  121 {
  122         struct arm_devmap_entry *m;
  123 
  124         if (devmap_bootstrap_done)
  125                 panic("arm_devmap_add_entry() after arm_devmap_bootstrap()");
  126 
  127         if (akva_devmap_idx == (AKVA_DEVMAP_MAX_ENTRIES - 1))
  128                 panic("AKVA_DEVMAP_MAX_ENTRIES is too small");
  129 
  130         if (akva_devmap_idx == 0)
  131                 arm_devmap_register_table(akva_devmap_entries);
  132 
  133         /*
  134          * Allocate virtual address space from the top of kva downwards.  If the
  135          * range being mapped is aligned and sized to 1MB boundaries then also
  136          * align the virtual address to the next-lower 1MB boundary so that we
  137          * end up with a nice efficient section mapping.
  138          */
  139         if ((pa & 0x000fffff) == 0 && (sz & 0x000fffff) == 0) {
  140                 akva_devmap_vaddr = trunc_1mpage(akva_devmap_vaddr - sz);
  141         } else {
  142                 akva_devmap_vaddr = trunc_page(akva_devmap_vaddr - sz);
  143         }
  144         m = &akva_devmap_entries[akva_devmap_idx++];
  145         m->pd_va    = akva_devmap_vaddr;
  146         m->pd_pa    = pa;
  147         m->pd_size  = sz;
  148         m->pd_prot  = VM_PROT_READ | VM_PROT_WRITE;
  149         m->pd_cache = PTE_DEVICE;
  150 }
  151 
  152 /*
  153  * Register the given table as the one to use in arm_devmap_bootstrap().
  154  */
  155 void
  156 arm_devmap_register_table(const struct arm_devmap_entry *table)
  157 {
  158 
  159         devmap_table = table;
  160 }
  161 
  162 /*
  163  * Map all of the static regions in the devmap table, and remember the devmap
  164  * table so the mapdev, ptov, and vtop functions can do lookups later.
  165  *
  166  * If a non-NULL table pointer is given it is used unconditionally, otherwise
  167  * the previously-registered table is used.  This smooths transition from legacy
  168  * code that fills in a local table then calls this function passing that table,
  169  * and newer code that uses arm_devmap_register_table() in platform-specific
  170  * code, then lets the common initarm() call this function with a NULL pointer.
  171  */
  172 void
  173 arm_devmap_bootstrap(vm_offset_t l1pt, const struct arm_devmap_entry *table)
  174 {
  175         const struct arm_devmap_entry *pd;
  176 
  177         devmap_bootstrap_done = true;
  178 
  179         /*
  180          * If given a table pointer, use it.  Otherwise, if a table was
  181          * previously registered, use it.  Otherwise, no work to do.
  182          */
  183         if (table != NULL)
  184                 devmap_table = table;
  185         else if (devmap_table == NULL)
  186                 return;
  187 
  188         for (pd = devmap_table; pd->pd_size != 0; ++pd) {
  189                 pmap_map_chunk(l1pt, pd->pd_va, pd->pd_pa, pd->pd_size,
  190                     pd->pd_prot,pd->pd_cache);
  191         }
  192 }
  193 
  194 /*
  195  * Look up the given physical address in the static mapping data and return the
  196  * corresponding virtual address, or NULL if not found.
  197  */
  198 void *
  199 arm_devmap_ptov(vm_paddr_t pa, vm_size_t size)
  200 {
  201         const struct arm_devmap_entry *pd;
  202 
  203         if (devmap_table == NULL)
  204                 return (NULL);
  205 
  206         for (pd = devmap_table; pd->pd_size != 0; ++pd) {
  207                 if (pa >= pd->pd_pa && pa + size <= pd->pd_pa + pd->pd_size)
  208                         return ((void *)(pd->pd_va + (pa - pd->pd_pa)));
  209         }
  210 
  211         return (NULL);
  212 }
  213 
  214 /*
  215  * Look up the given virtual address in the static mapping data and return the
  216  * corresponding physical address, or DEVMAP_PADDR_NOTFOUND if not found.
  217  */
  218 vm_paddr_t
  219 arm_devmap_vtop(void * vpva, vm_size_t size)
  220 {
  221         const struct arm_devmap_entry *pd;
  222         vm_offset_t va;
  223 
  224         if (devmap_table == NULL)
  225                 return (DEVMAP_PADDR_NOTFOUND);
  226 
  227         va = (vm_offset_t)vpva;
  228         for (pd = devmap_table; pd->pd_size != 0; ++pd) {
  229                 if (va >= pd->pd_va && va + size <= pd->pd_va + pd->pd_size)
  230                         return ((vm_paddr_t)(pd->pd_pa + (va - pd->pd_va)));
  231         }
  232 
  233         return (DEVMAP_PADDR_NOTFOUND);
  234 }
  235 
  236 /*
  237  * Map a set of physical memory pages into the kernel virtual address space.
  238  * Return a pointer to where it is mapped.
  239  *
  240  * This uses a pre-established static mapping if one exists for the requested
  241  * range, otherwise it allocates kva space and maps the physical pages into it.
  242  *
  243  * This routine is intended to be used for mapping device memory, NOT real
  244  * memory; the mapping type is inherently PTE_DEVICE in pmap_kenter_device().
  245  */
  246 void *
  247 pmap_mapdev(vm_offset_t pa, vm_size_t size)
  248 {
  249         vm_offset_t va, tmpva, offset;
  250         void * rva;
  251 
  252         /* First look in the static mapping table. */
  253         if ((rva = arm_devmap_ptov(pa, size)) != NULL)
  254                 return (rva);
  255         
  256         offset = pa & PAGE_MASK;
  257         pa = trunc_page(pa);
  258         size = round_page(size + offset);
  259         
  260         va = kva_alloc(size);
  261         if (!va)
  262                 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
  263 
  264         for (tmpva = va; size > 0;) {
  265                 pmap_kenter_device(tmpva, pa);
  266                 size -= PAGE_SIZE;
  267                 tmpva += PAGE_SIZE;
  268                 pa += PAGE_SIZE;
  269         }
  270         
  271         return ((void *)(va + offset));
  272 }
  273 
  274 /*
  275  * Unmap device memory and free the kva space.
  276  */
  277 void
  278 pmap_unmapdev(vm_offset_t va, vm_size_t size)
  279 {
  280         vm_offset_t tmpva, offset;
  281         vm_size_t origsize;
  282 
  283         /* Nothing to do if we find the mapping in the static table. */
  284         if (arm_devmap_vtop((void*)va, size) != DEVMAP_PADDR_NOTFOUND)
  285                 return;
  286 
  287         origsize = size;
  288         offset = va & PAGE_MASK;
  289         va = trunc_page(va);
  290         size = round_page(size + offset);
  291 
  292         for (tmpva = va; size > 0;) {
  293                 pmap_kremove(tmpva);
  294                 size -= PAGE_SIZE;
  295                 tmpva += PAGE_SIZE;
  296         }
  297 
  298         kva_free(va, origsize);
  299 }
  300 
  301 #ifdef DDB
  302 #include <ddb/ddb.h>
  303 
  304 DB_SHOW_COMMAND(devmap, db_show_devmap)
  305 {
  306         devmap_dump_table(db_printf);
  307 }
  308 
  309 #endif /* DDB */
  310 

Cache object: db726f0df7cfef83d5be9e3cfbd86f2d


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