/*1* arch/sh/mm/ioremap.c2*3* (C) Copyright 1995 1996 Linus Torvalds4* (C) Copyright 2005 - 2010 Paul Mundt5*6* Re-map IO memory to kernel address space so that we can access it.7* This is needed for high PCI addresses that aren't mapped in the8* 640k-1MB IO memory area on PC's9*10* This file is subject to the terms and conditions of the GNU General11* Public License. See the file "COPYING" in the main directory of this12* archive for more details.13*/14#include <linux/vmalloc.h>15#include <linux/module.h>16#include <linux/slab.h>17#include <linux/mm.h>18#include <linux/pci.h>19#include <linux/io.h>20#include <asm/page.h>21#include <asm/pgalloc.h>22#include <asm/addrspace.h>23#include <asm/cacheflush.h>24#include <asm/tlbflush.h>25#include <asm/mmu.h>2627/*28* Remap an arbitrary physical address space into the kernel virtual29* address space. Needed when the kernel wants to access high addresses30* directly.31*32* NOTE! We need to allow non-page-aligned mappings too: we will obviously33* have to convert them into an offset in a page-aligned mapping, but the34* caller shouldn't need to know that small detail.35*/36void __iomem * __init_refok37__ioremap_caller(phys_addr_t phys_addr, unsigned long size,38pgprot_t pgprot, void *caller)39{40struct vm_struct *area;41unsigned long offset, last_addr, addr, orig_addr;42void __iomem *mapped;4344/* Don't allow wraparound or zero size */45last_addr = phys_addr + size - 1;46if (!size || last_addr < phys_addr)47return NULL;4849/*50* If we can't yet use the regular approach, go the fixmap route.51*/52if (!mem_init_done)53return ioremap_fixed(phys_addr, size, pgprot);5455/*56* First try to remap through the PMB.57* PMB entries are all pre-faulted.58*/59mapped = pmb_remap_caller(phys_addr, size, pgprot, caller);60if (mapped && !IS_ERR(mapped))61return mapped;6263/*64* Mappings have to be page-aligned65*/66offset = phys_addr & ~PAGE_MASK;67phys_addr &= PAGE_MASK;68size = PAGE_ALIGN(last_addr+1) - phys_addr;6970/*71* Ok, go for it..72*/73area = get_vm_area_caller(size, VM_IOREMAP, caller);74if (!area)75return NULL;76area->phys_addr = phys_addr;77orig_addr = addr = (unsigned long)area->addr;7879if (ioremap_page_range(addr, addr + size, phys_addr, pgprot)) {80vunmap((void *)orig_addr);81return NULL;82}8384return (void __iomem *)(offset + (char *)orig_addr);85}86EXPORT_SYMBOL(__ioremap_caller);8788/*89* Simple checks for non-translatable mappings.90*/91static inline int iomapping_nontranslatable(unsigned long offset)92{93#ifdef CONFIG_29BIT94/*95* In 29-bit mode this includes the fixed P1/P2 areas, as well as96* parts of P3.97*/98if (PXSEG(offset) < P3SEG || offset >= P3_ADDR_MAX)99return 1;100#endif101102return 0;103}104105void __iounmap(void __iomem *addr)106{107unsigned long vaddr = (unsigned long __force)addr;108struct vm_struct *p;109110/*111* Nothing to do if there is no translatable mapping.112*/113if (iomapping_nontranslatable(vaddr))114return;115116/*117* There's no VMA if it's from an early fixed mapping.118*/119if (iounmap_fixed(addr) == 0)120return;121122/*123* If the PMB handled it, there's nothing else to do.124*/125if (pmb_unmap(addr) == 0)126return;127128p = remove_vm_area((void *)(vaddr & PAGE_MASK));129if (!p) {130printk(KERN_ERR "%s: bad address %p\n", __func__, addr);131return;132}133134kfree(p);135}136EXPORT_SYMBOL(__iounmap);137138139