linux/arch/cris/mm/ioremap.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * arch/cris/mm/ioremap.c
   4 *
   5 * Re-map IO memory to kernel address space so that we can access it.
   6 * Needed for memory-mapped I/O devices mapped outside our normal DRAM
   7 * window (that is, all memory-mapped I/O devices).
   8 *
   9 * (C) Copyright 1995 1996 Linus Torvalds
  10 * CRIS-port by Axis Communications AB
  11 */
  12
  13#include <linux/vmalloc.h>
  14#include <linux/io.h>
  15#include <asm/pgalloc.h>
  16#include <arch/memmap.h>
  17
  18/*
  19 * Generic mapping function (not visible outside):
  20 */
  21
  22/*
  23 * Remap an arbitrary physical address space into the kernel virtual
  24 * address space. Needed when the kernel wants to access high addresses
  25 * directly.
  26 *
  27 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
  28 * have to convert them into an offset in a page-aligned mapping, but the
  29 * caller shouldn't need to know that small detail.
  30 */
  31void __iomem * __ioremap_prot(unsigned long phys_addr, unsigned long size, pgprot_t prot)
  32{
  33        void __iomem * addr;
  34        struct vm_struct * area;
  35        unsigned long offset, last_addr;
  36
  37        /* Don't allow wraparound or zero size */
  38        last_addr = phys_addr + size - 1;
  39        if (!size || last_addr < phys_addr)
  40                return NULL;
  41
  42        /*
  43         * Mappings have to be page-aligned
  44         */
  45        offset = phys_addr & ~PAGE_MASK;
  46        phys_addr &= PAGE_MASK;
  47        size = PAGE_ALIGN(last_addr+1) - phys_addr;
  48
  49        /*
  50         * Ok, go for it..
  51         */
  52        area = get_vm_area(size, VM_IOREMAP);
  53        if (!area)
  54                return NULL;
  55        addr = (void __iomem *)area->addr;
  56        if (ioremap_page_range((unsigned long)addr, (unsigned long)addr + size,
  57                               phys_addr, prot)) {
  58                vfree((void __force *)addr);
  59                return NULL;
  60        }
  61        return (void __iomem *) (offset + (char __iomem *)addr);
  62}
  63
  64void __iomem * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
  65{
  66        return __ioremap_prot(phys_addr, size,
  67                              __pgprot(_PAGE_PRESENT | __READABLE |
  68                                       __WRITEABLE | _PAGE_GLOBAL |
  69                                       _PAGE_KERNEL | flags));
  70}
  71
  72/**
  73 * ioremap_nocache     -   map bus memory into CPU space
  74 * @offset:    bus address of the memory
  75 * @size:      size of the resource to map
  76 *
  77 * Must be freed with iounmap.
  78 */
  79
  80void __iomem *ioremap_nocache(unsigned long phys_addr, unsigned long size)
  81{
  82        return __ioremap(phys_addr | MEM_NON_CACHEABLE, size, 0);
  83}
  84EXPORT_SYMBOL(ioremap_nocache);
  85
  86void iounmap(volatile void __iomem *addr)
  87{
  88        if (addr > high_memory)
  89                return vfree((void *) (PAGE_MASK & (unsigned long) addr));
  90}
  91