blob: 92a9b5f12f98adfe4e87e6a18ff381e528108946 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * arch/parisc/mm/ioremap.c
4 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * (C) Copyright 1995 1996 Linus Torvalds
Helge Dellerb2d6b9f2006-03-27 19:52:14 +00006 * (C) Copyright 2001-2006 Helge Deller <deller@gmx.de>
Kyle McMartine0565a12006-01-10 20:47:52 -05007 * (C) Copyright 2005 Kyle McMartin <kyle@parisc-linux.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
10#include <linux/vmalloc.h>
11#include <linux/errno.h>
12#include <linux/module.h>
Haavard Skinnemoene34067f2006-12-08 02:38:05 -080013#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <asm/pgalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Linus Torvalds1da177e2005-04-16 15:20:36 -070016/*
17 * Generic mapping function (not visible outside):
18 */
19
20/*
21 * Remap an arbitrary physical address space into the kernel virtual
Kyle McMartine0565a12006-01-10 20:47:52 -050022 * address space.
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 *
24 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
25 * have to convert them into an offset in a page-aligned mapping, but the
26 * caller shouldn't need to know that small detail.
27 */
28void __iomem * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
29{
Matthew Wilcoxe51ec242006-11-05 15:24:48 -070030 void __iomem *addr;
Helge Dellercb4ab592006-03-23 00:40:10 -070031 struct vm_struct *area;
32 unsigned long offset, last_addr;
Haavard Skinnemoene34067f2006-12-08 02:38:05 -080033 pgprot_t pgprot;
Helge Dellercb4ab592006-03-23 00:40:10 -070034
Helge Deller29ef82952006-03-23 00:32:46 -070035#ifdef CONFIG_EISA
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 unsigned long end = phys_addr + size - 1;
37 /* Support EISA addresses */
Helge Deller10267cd2006-03-26 01:54:16 -070038 if ((phys_addr >= 0x00080000 && end < 0x000fffff) ||
39 (phys_addr >= 0x00500000 && end < 0x03bfffff)) {
40 phys_addr |= F_EXTEND(0xfc000000);
Helge Dellerb2d6b9f2006-03-27 19:52:14 +000041 flags |= _PAGE_NO_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#endif
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 /* Don't allow wraparound or zero size */
46 last_addr = phys_addr + size - 1;
47 if (!size || last_addr < phys_addr)
48 return NULL;
49
50 /*
51 * Don't allow anybody to remap normal RAM that we're using..
52 */
53 if (phys_addr < virt_to_phys(high_memory)) {
54 char *t_addr, *t_end;
55 struct page *page;
56
57 t_addr = __va(phys_addr);
58 t_end = t_addr + (size - 1);
59
Kyle McMartine0565a12006-01-10 20:47:52 -050060 for (page = virt_to_page(t_addr);
61 page <= virt_to_page(t_end); page++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 if(!PageReserved(page))
63 return NULL;
Kyle McMartine0565a12006-01-10 20:47:52 -050064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 }
66
Haavard Skinnemoene34067f2006-12-08 02:38:05 -080067 pgprot = __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY |
68 _PAGE_ACCESSED | flags);
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 /*
71 * Mappings have to be page-aligned
72 */
73 offset = phys_addr & ~PAGE_MASK;
74 phys_addr &= PAGE_MASK;
Florian Zumbiehla292dfa2010-08-11 14:17:33 -070075 size = PAGE_ALIGN(last_addr + 1) - phys_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 /*
78 * Ok, go for it..
79 */
80 area = get_vm_area(size, VM_IOREMAP);
81 if (!area)
82 return NULL;
Kyle McMartine0565a12006-01-10 20:47:52 -050083
Matthew Wilcoxe51ec242006-11-05 15:24:48 -070084 addr = (void __iomem *) area->addr;
Haavard Skinnemoene34067f2006-12-08 02:38:05 -080085 if (ioremap_page_range((unsigned long)addr, (unsigned long)addr + size,
86 phys_addr, pgprot)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 vfree(addr);
88 return NULL;
89 }
Kyle McMartine0565a12006-01-10 20:47:52 -050090
Matthew Wilcoxe51ec242006-11-05 15:24:48 -070091 return (void __iomem *) (offset + (char __iomem *)addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
Kyle McMartind345fd32006-03-29 15:18:32 -070093EXPORT_SYMBOL(__ioremap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Matthew Wilcox01232e92006-09-19 16:37:01 -060095void iounmap(const volatile void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (addr > high_memory)
98 return vfree((void *) (PAGE_MASK & (unsigned long __force) addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
Kyle McMartind345fd32006-03-29 15:18:32 -0700100EXPORT_SYMBOL(iounmap);