ioremap.c 2.6 KB
Newer Older
V
Vineet Gupta 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

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

17 18 19 20 21 22 23 24 25 26 27 28
static inline bool arc_uncached_addr_space(phys_addr_t paddr)
{
	if (is_isa_arcompact()) {
		if (paddr >= ARC_UNCACHED_ADDR_SPACE)
			return true;
	} else if (paddr >= perip_base && paddr <= 0xFFFFFFFF) {
		return true;
	}

	return false;
}

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

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

38 39 40 41
	/*
	 * 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
	 */
42
	if (arc_uncached_addr_space(paddr))
43
		return (void __iomem *)(u32)paddr;
V
Vineet Gupta 已提交
44

45 46 47 48 49 50 51 52 53 54 55 56 57 58
	return ioremap_prot(paddr, size, PAGE_KERNEL_NO_CACHE);
}
EXPORT_SYMBOL(ioremap);

/*
 * ioremap with access flags
 * Cache semantics wise it is same as ioremap - "forced" uncached.
 * However unline vanilla ioremap which bypasses ARC MMU for addresses in
 * 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)
{
59
	unsigned long vaddr;
60
	struct vm_struct *area;
61
	phys_addr_t off, end;
62 63 64 65 66 67 68
	pgprot_t prot = __pgprot(flags);

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

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

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

	/* Mappings have to be page-aligned */
V
Vineet Gupta 已提交
77 78 79 80 81 82 83 84 85 86 87
	off = paddr & ~PAGE_MASK;
	paddr &= PAGE_MASK;
	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;
88 89
	vaddr = (unsigned long)area->addr;
	if (ioremap_page_range(vaddr, vaddr + size, paddr, prot)) {
90
		vunmap((void __force *)vaddr);
V
Vineet Gupta 已提交
91 92 93 94
		return NULL;
	}
	return (void __iomem *)(off + (char __iomem *)vaddr);
}
95 96
EXPORT_SYMBOL(ioremap_prot);

V
Vineet Gupta 已提交
97 98 99

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

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