blob: 75b0ca6e387eff4648ff07a44ad9d869cce13a70 [file] [log] [blame]
Vineet Gupta1162b072013-01-18 15:12:20 +05301/*
2 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9#include <linux/vmalloc.h>
10#include <linux/init.h>
11#include <linux/module.h>
12#include <linux/io.h>
13#include <linux/mm.h>
14#include <linux/slab.h>
Sachin Kamat1ec9db12013-03-06 16:53:44 +053015#include <linux/cache.h>
Vineet Gupta1162b072013-01-18 15:12:20 +053016
Vineet Guptaf5db19e2016-03-16 15:04:39 +053017void __iomem *ioremap(phys_addr_t paddr, unsigned long size)
Vineet Gupta1162b072013-01-18 15:12:20 +053018{
Vineet Guptaf5db19e2016-03-16 15:04:39 +053019 phys_addr_t end;
Vineet Gupta1162b072013-01-18 15:12:20 +053020
21 /* Don't allow wraparound or zero size */
22 end = paddr + size - 1;
23 if (!size || (end < paddr))
24 return NULL;
25
Vineet Guptaf5db19e2016-03-16 15:04:39 +053026 /*
27 * If the region is h/w uncached, MMU mapping can be elided as optim
28 * The cast to u32 is fine as this region can only be inside 4GB
29 */
Vineet Gupta1162b072013-01-18 15:12:20 +053030 if (paddr >= ARC_UNCACHED_ADDR_SPACE)
Vineet Guptaf5db19e2016-03-16 15:04:39 +053031 return (void __iomem *)(u32)paddr;
Vineet Gupta1162b072013-01-18 15:12:20 +053032
Gilad Ben-Yossef43689022013-01-22 16:48:45 +053033 return ioremap_prot(paddr, size, PAGE_KERNEL_NO_CACHE);
34}
35EXPORT_SYMBOL(ioremap);
36
37/*
38 * ioremap with access flags
39 * Cache semantics wise it is same as ioremap - "forced" uncached.
40 * However unline vanilla ioremap which bypasses ARC MMU for addresses in
41 * ARC hardware uncached region, this one still goes thru the MMU as caller
42 * might need finer access control (R/W/X)
43 */
44void __iomem *ioremap_prot(phys_addr_t paddr, unsigned long size,
45 unsigned long flags)
46{
Vineet Guptaf5db19e2016-03-16 15:04:39 +053047 unsigned long vaddr;
Gilad Ben-Yossef43689022013-01-22 16:48:45 +053048 struct vm_struct *area;
Vineet Guptaf5db19e2016-03-16 15:04:39 +053049 phys_addr_t off, end;
Gilad Ben-Yossef43689022013-01-22 16:48:45 +053050 pgprot_t prot = __pgprot(flags);
51
52 /* Don't allow wraparound, zero size */
53 end = paddr + size - 1;
54 if ((!size) || (end < paddr))
55 return NULL;
56
Vineet Gupta1162b072013-01-18 15:12:20 +053057 /* An early platform driver might end up here */
58 if (!slab_is_available())
59 return NULL;
60
Gilad Ben-Yossef43689022013-01-22 16:48:45 +053061 /* force uncached */
62 prot = pgprot_noncached(prot);
63
64 /* Mappings have to be page-aligned */
Vineet Gupta1162b072013-01-18 15:12:20 +053065 off = paddr & ~PAGE_MASK;
66 paddr &= PAGE_MASK;
67 size = PAGE_ALIGN(end + 1) - paddr;
68
69 /*
70 * Ok, go for it..
71 */
72 area = get_vm_area(size, VM_IOREMAP);
73 if (!area)
74 return NULL;
Vineet Gupta1162b072013-01-18 15:12:20 +053075 area->phys_addr = paddr;
Vineet Guptaf5db19e2016-03-16 15:04:39 +053076 vaddr = (unsigned long)area->addr;
77 if (ioremap_page_range(vaddr, vaddr + size, paddr, prot)) {
Gilad Ben-Yossef43689022013-01-22 16:48:45 +053078 vunmap((void __force *)vaddr);
Vineet Gupta1162b072013-01-18 15:12:20 +053079 return NULL;
80 }
Vineet Gupta1162b072013-01-18 15:12:20 +053081 return (void __iomem *)(off + (char __iomem *)vaddr);
82}
Gilad Ben-Yossef43689022013-01-22 16:48:45 +053083EXPORT_SYMBOL(ioremap_prot);
84
Vineet Gupta1162b072013-01-18 15:12:20 +053085
86void iounmap(const void __iomem *addr)
87{
88 if (addr >= (void __force __iomem *)ARC_UNCACHED_ADDR_SPACE)
89 return;
90
91 vfree((void *)(PAGE_MASK & (unsigned long __force)addr));
92}
93EXPORT_SYMBOL(iounmap);