blob: edd9a9559cba92298704e3428499e8995be22c00 [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
5 * (C) Copyright 2001 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 -050075#if USE_HPPA_IOREMAP
76static int
77remap_area_pages(unsigned long address, unsigned long phys_addr,
78 unsigned long size, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Kyle McMartine0565a12006-01-10 20:47:52 -050080 pgd_t *dir;
81 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 unsigned long end = address + size;
83
Kyle McMartine0565a12006-01-10 20:47:52 -050084 BUG_ON(address >= end);
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 phys_addr -= address;
Kyle McMartine0565a12006-01-10 20:47:52 -050087 dir = pgd_offset_k(address);
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 flush_cache_all();
Kyle McMartine0565a12006-01-10 20:47:52 -050090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 do {
Kyle McMartine0565a12006-01-10 20:47:52 -050092 pud_t *pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 pmd_t *pmd;
Kyle McMartine0565a12006-01-10 20:47:52 -050094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 error = -ENOMEM;
Kyle McMartine0565a12006-01-10 20:47:52 -050096 pud = pud_alloc(&init_mm, dir, address);
97 if (!pud)
98 break;
99
100 pmd = pmd_alloc(&init_mm, pud, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (!pmd)
102 break;
Kyle McMartine0565a12006-01-10 20:47:52 -0500103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 if (remap_area_pmd(pmd, address, end - address,
Kyle McMartine0565a12006-01-10 20:47:52 -0500105 phys_addr + address, flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 break;
Kyle McMartine0565a12006-01-10 20:47:52 -0500107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 error = 0;
109 address = (address + PGDIR_SIZE) & PGDIR_MASK;
110 dir++;
111 } while (address && (address < end));
Kyle McMartine0565a12006-01-10 20:47:52 -0500112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 flush_tlb_all();
Kyle McMartine0565a12006-01-10 20:47:52 -0500114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return error;
116}
117#endif /* USE_HPPA_IOREMAP */
118
119#ifdef CONFIG_DEBUG_IOREMAP
120static unsigned long last = 0;
121
122void gsc_bad_addr(unsigned long addr)
123{
124 if (time_after(jiffies, last + HZ*10)) {
125 printk("gsc_foo() called with bad address 0x%lx\n", addr);
126 dump_stack();
127 last = jiffies;
128 }
129}
130EXPORT_SYMBOL(gsc_bad_addr);
131
132void __raw_bad_addr(const volatile void __iomem *addr)
133{
134 if (time_after(jiffies, last + HZ*10)) {
135 printk("__raw_foo() called with bad address 0x%p\n", addr);
136 dump_stack();
137 last = jiffies;
138 }
139}
140EXPORT_SYMBOL(__raw_bad_addr);
141#endif
142
143/*
144 * Generic mapping function (not visible outside):
145 */
146
147/*
148 * Remap an arbitrary physical address space into the kernel virtual
Kyle McMartine0565a12006-01-10 20:47:52 -0500149 * address space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 *
151 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
152 * have to convert them into an offset in a page-aligned mapping, but the
153 * caller shouldn't need to know that small detail.
154 */
155void __iomem * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
156{
157#if !(USE_HPPA_IOREMAP)
158
159 unsigned long end = phys_addr + size - 1;
160 /* Support EISA addresses */
161 if ((phys_addr >= 0x00080000 && end < 0x000fffff)
162 || (phys_addr >= 0x00500000 && end < 0x03bfffff)) {
163 phys_addr |= 0xfc000000;
164 }
165
166#ifdef CONFIG_DEBUG_IOREMAP
167 return (void __iomem *)(phys_addr - (0x1UL << NYBBLE_SHIFT));
168#else
169 return (void __iomem *)phys_addr;
170#endif
171
172#else
Kyle McMartine0565a12006-01-10 20:47:52 -0500173 void *addr;
174 struct vm_struct *area;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 unsigned long offset, last_addr;
176
177 /* Don't allow wraparound or zero size */
178 last_addr = phys_addr + size - 1;
179 if (!size || last_addr < phys_addr)
180 return NULL;
181
182 /*
183 * Don't allow anybody to remap normal RAM that we're using..
184 */
185 if (phys_addr < virt_to_phys(high_memory)) {
186 char *t_addr, *t_end;
187 struct page *page;
188
189 t_addr = __va(phys_addr);
190 t_end = t_addr + (size - 1);
191
Kyle McMartine0565a12006-01-10 20:47:52 -0500192 for (page = virt_to_page(t_addr);
193 page <= virt_to_page(t_end); page++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if(!PageReserved(page))
195 return NULL;
Kyle McMartine0565a12006-01-10 20:47:52 -0500196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
198
199 /*
200 * Mappings have to be page-aligned
201 */
202 offset = phys_addr & ~PAGE_MASK;
203 phys_addr &= PAGE_MASK;
204 size = PAGE_ALIGN(last_addr) - phys_addr;
205
206 /*
207 * Ok, go for it..
208 */
209 area = get_vm_area(size, VM_IOREMAP);
210 if (!area)
211 return NULL;
Kyle McMartine0565a12006-01-10 20:47:52 -0500212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 addr = area->addr;
214 if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) {
215 vfree(addr);
216 return NULL;
217 }
Kyle McMartine0565a12006-01-10 20:47:52 -0500218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return (void __iomem *) (offset + (char *)addr);
220#endif
221}
222
223void iounmap(void __iomem *addr)
224{
225#if !(USE_HPPA_IOREMAP)
226 return;
227#else
228 if (addr > high_memory)
229 return vfree((void *) (PAGE_MASK & (unsigned long __force) addr));
230#endif
231}