blob: 6c6102307c0653d7319b3ab2af7cc96b81e74da9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/parisc/mm/ioremap.c
3 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * (C) Copyright 1995 1996 Linus Torvalds
Helge Dellerb2d6b9f2006-03-27 19:52:14 +00005 * (C) Copyright 2001-2006 Helge Deller <deller@gmx.de>
Kyle McMartine0565a12006-01-10 20:47:52 -05006 * (C) Copyright 2005 Kyle McMartin <kyle@parisc-linux.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
9#include <linux/vmalloc.h>
10#include <linux/errno.h>
11#include <linux/module.h>
12#include <asm/io.h>
13#include <asm/pgalloc.h>
Kyle McMartine0565a12006-01-10 20:47:52 -050014#include <asm/tlbflush.h>
15#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Kyle McMartine0565a12006-01-10 20:47:52 -050017static inline void
18remap_area_pte(pte_t *pte, unsigned long address, unsigned long size,
19 unsigned long phys_addr, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070020{
Kyle McMartine0565a12006-01-10 20:47:52 -050021 unsigned long end, pfn;
22 pgprot_t pgprot = __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY |
23 _PAGE_ACCESSED | flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25 address &= ~PMD_MASK;
Kyle McMartine0565a12006-01-10 20:47:52 -050026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 end = address + size;
28 if (end > PMD_SIZE)
29 end = PMD_SIZE;
Kyle McMartine0565a12006-01-10 20:47:52 -050030
31 BUG_ON(address >= end);
32
33 pfn = phys_addr >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 do {
Kyle McMartine0565a12006-01-10 20:47:52 -050035 BUG_ON(!pte_none(*pte));
36
37 set_pte(pte, pfn_pte(pfn, pgprot));
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 address += PAGE_SIZE;
Kyle McMartine0565a12006-01-10 20:47:52 -050040 pfn++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 pte++;
42 } while (address && (address < end));
43}
44
Kyle McMartine0565a12006-01-10 20:47:52 -050045static inline int
46remap_area_pmd(pmd_t *pmd, unsigned long address, unsigned long size,
47 unsigned long phys_addr, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 unsigned long end;
50
51 address &= ~PGDIR_MASK;
Kyle McMartine0565a12006-01-10 20:47:52 -050052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 end = address + size;
54 if (end > PGDIR_SIZE)
55 end = PGDIR_SIZE;
Kyle McMartine0565a12006-01-10 20:47:52 -050056
57 BUG_ON(address >= end);
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 phys_addr -= address;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 do {
Kyle McMartine0565a12006-01-10 20:47:52 -050061 pte_t *pte = pte_alloc_kernel(pmd, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 if (!pte)
63 return -ENOMEM;
Kyle McMartine0565a12006-01-10 20:47:52 -050064
65 remap_area_pte(pte, address, end - address,
66 address + phys_addr, flags);
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 address = (address + PMD_SIZE) & PMD_MASK;
69 pmd++;
70 } while (address && (address < end));
Kyle McMartine0565a12006-01-10 20:47:52 -050071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return 0;
73}
74
Kyle McMartine0565a12006-01-10 20:47:52 -050075static int
76remap_area_pages(unsigned long address, unsigned long phys_addr,
77 unsigned long size, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Kyle McMartine0565a12006-01-10 20:47:52 -050079 pgd_t *dir;
80 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 unsigned long end = address + size;
82
Kyle McMartine0565a12006-01-10 20:47:52 -050083 BUG_ON(address >= end);
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 phys_addr -= address;
Kyle McMartine0565a12006-01-10 20:47:52 -050086 dir = pgd_offset_k(address);
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 flush_cache_all();
Kyle McMartine0565a12006-01-10 20:47:52 -050089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 do {
Kyle McMartine0565a12006-01-10 20:47:52 -050091 pud_t *pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 pmd_t *pmd;
Kyle McMartine0565a12006-01-10 20:47:52 -050093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 error = -ENOMEM;
Kyle McMartine0565a12006-01-10 20:47:52 -050095 pud = pud_alloc(&init_mm, dir, address);
96 if (!pud)
97 break;
98
99 pmd = pmd_alloc(&init_mm, pud, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (!pmd)
101 break;
Kyle McMartine0565a12006-01-10 20:47:52 -0500102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (remap_area_pmd(pmd, address, end - address,
Kyle McMartine0565a12006-01-10 20:47:52 -0500104 phys_addr + address, flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 break;
Kyle McMartine0565a12006-01-10 20:47:52 -0500106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 error = 0;
108 address = (address + PGDIR_SIZE) & PGDIR_MASK;
109 dir++;
110 } while (address && (address < end));
Kyle McMartine0565a12006-01-10 20:47:52 -0500111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 flush_tlb_all();
Kyle McMartine0565a12006-01-10 20:47:52 -0500113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return error;
115}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117/*
118 * Generic mapping function (not visible outside):
119 */
120
121/*
122 * Remap an arbitrary physical address space into the kernel virtual
Kyle McMartine0565a12006-01-10 20:47:52 -0500123 * address space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 *
125 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
126 * have to convert them into an offset in a page-aligned mapping, but the
127 * caller shouldn't need to know that small detail.
128 */
129void __iomem * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
130{
Matthew Wilcoxe51ec242006-11-05 15:24:48 -0700131 void __iomem *addr;
Helge Dellercb4ab592006-03-23 00:40:10 -0700132 struct vm_struct *area;
133 unsigned long offset, last_addr;
134
Helge Deller29ef82952006-03-23 00:32:46 -0700135#ifdef CONFIG_EISA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 unsigned long end = phys_addr + size - 1;
137 /* Support EISA addresses */
Helge Deller10267cd2006-03-26 01:54:16 -0700138 if ((phys_addr >= 0x00080000 && end < 0x000fffff) ||
139 (phys_addr >= 0x00500000 && end < 0x03bfffff)) {
140 phys_addr |= F_EXTEND(0xfc000000);
Helge Dellerb2d6b9f2006-03-27 19:52:14 +0000141 flags |= _PAGE_NO_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143#endif
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 /* Don't allow wraparound or zero size */
146 last_addr = phys_addr + size - 1;
147 if (!size || last_addr < phys_addr)
148 return NULL;
149
150 /*
151 * Don't allow anybody to remap normal RAM that we're using..
152 */
153 if (phys_addr < virt_to_phys(high_memory)) {
154 char *t_addr, *t_end;
155 struct page *page;
156
157 t_addr = __va(phys_addr);
158 t_end = t_addr + (size - 1);
159
Kyle McMartine0565a12006-01-10 20:47:52 -0500160 for (page = virt_to_page(t_addr);
161 page <= virt_to_page(t_end); page++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if(!PageReserved(page))
163 return NULL;
Kyle McMartine0565a12006-01-10 20:47:52 -0500164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166
167 /*
168 * Mappings have to be page-aligned
169 */
170 offset = phys_addr & ~PAGE_MASK;
171 phys_addr &= PAGE_MASK;
172 size = PAGE_ALIGN(last_addr) - phys_addr;
173
174 /*
175 * Ok, go for it..
176 */
177 area = get_vm_area(size, VM_IOREMAP);
178 if (!area)
179 return NULL;
Kyle McMartine0565a12006-01-10 20:47:52 -0500180
Matthew Wilcoxe51ec242006-11-05 15:24:48 -0700181 addr = (void __iomem *) area->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) {
183 vfree(addr);
184 return NULL;
185 }
Kyle McMartine0565a12006-01-10 20:47:52 -0500186
Matthew Wilcoxe51ec242006-11-05 15:24:48 -0700187 return (void __iomem *) (offset + (char __iomem *)addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
Kyle McMartind345fd32006-03-29 15:18:32 -0700189EXPORT_SYMBOL(__ioremap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Matthew Wilcox01232e92006-09-19 16:37:01 -0600191void iounmap(const volatile void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (addr > high_memory)
194 return vfree((void *) (PAGE_MASK & (unsigned long __force) addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
Kyle McMartind345fd32006-03-29 15:18:32 -0700196EXPORT_SYMBOL(iounmap);