ioremap.c 2.5 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
V
Vineet Gupta 已提交
2 3 4 5 6 7 8 9 10 11
/*
 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
 */

#include <linux/vmalloc.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/io.h>
#include <linux/mm.h>
#include <linux/slab.h>
12
#include <linux/cache.h>
V
Vineet Gupta 已提交
13

14 15 16 17 18
static inline bool arc_uncached_addr_space(phys_addr_t paddr)
{
	if (is_isa_arcompact()) {
		if (paddr >= ARC_UNCACHED_ADDR_SPACE)
			return true;
19
	} else if (paddr >= perip_base && paddr <= perip_end) {
20 21 22 23 24 25
		return true;
	}

	return false;
}

26
void __iomem *ioremap(phys_addr_t paddr, unsigned long size)
V
Vineet Gupta 已提交
27
{
28
	phys_addr_t end;
V
Vineet Gupta 已提交
29 30 31 32 33 34

	/* Don't allow wraparound or zero size */
	end = paddr + size - 1;
	if (!size || (end < paddr))
		return NULL;

35 36 37 38
	/*
	 * If the region is h/w uncached, MMU mapping can be elided as optim
	 * The cast to u32 is fine as this region can only be inside 4GB
	 */
39
	if (arc_uncached_addr_space(paddr))
40
		return (void __iomem *)(u32)paddr;
V
Vineet Gupta 已提交
41

42 43
	return ioremap_prot(paddr, size,
			    pgprot_val(pgprot_noncached(PAGE_KERNEL)));
44 45 46 47 48 49
}
EXPORT_SYMBOL(ioremap);

/*
 * ioremap with access flags
 * Cache semantics wise it is same as ioremap - "forced" uncached.
A
Alexey Brodkin 已提交
50
 * However unlike vanilla ioremap which bypasses ARC MMU for addresses in
51 52 53 54 55 56
 * ARC hardware uncached region, this one still goes thru the MMU as caller
 * might need finer access control (R/W/X)
 */
void __iomem *ioremap_prot(phys_addr_t paddr, unsigned long size,
			   unsigned long flags)
{
57
	unsigned int off;
58
	unsigned long vaddr;
59
	struct vm_struct *area;
60
	phys_addr_t end;
61 62 63 64 65 66 67
	pgprot_t prot = __pgprot(flags);

	/* Don't allow wraparound, zero size */
	end = paddr + size - 1;
	if ((!size) || (end < paddr))
		return NULL;

V
Vineet Gupta 已提交
68 69 70 71
	/* An early platform driver might end up here */
	if (!slab_is_available())
		return NULL;

72 73 74 75
	/* force uncached */
	prot = pgprot_noncached(prot);

	/* Mappings have to be page-aligned */
V
Vineet Gupta 已提交
76
	off = paddr & ~PAGE_MASK;
77
	paddr &= PAGE_MASK_PHYS;
V
Vineet Gupta 已提交
78 79 80 81 82 83 84 85 86
	size = PAGE_ALIGN(end + 1) - paddr;

	/*
	 * Ok, go for it..
	 */
	area = get_vm_area(size, VM_IOREMAP);
	if (!area)
		return NULL;
	area->phys_addr = paddr;
87 88
	vaddr = (unsigned long)area->addr;
	if (ioremap_page_range(vaddr, vaddr + size, paddr, prot)) {
89
		vunmap((void __force *)vaddr);
V
Vineet Gupta 已提交
90 91 92 93
		return NULL;
	}
	return (void __iomem *)(off + (char __iomem *)vaddr);
}
94 95
EXPORT_SYMBOL(ioremap_prot);

V
Vineet Gupta 已提交
96

R
Randy Dunlap 已提交
97
void iounmap(const volatile void __iomem *addr)
V
Vineet Gupta 已提交
98
{
99 100
	/* weird double cast to handle phys_addr_t > 32 bits */
	if (arc_uncached_addr_space((phys_addr_t)(u32)addr))
V
Vineet Gupta 已提交
101 102 103 104 105
		return;

	vfree((void *)(PAGE_MASK & (unsigned long __force)addr));
}
EXPORT_SYMBOL(iounmap);