blob: ebba11e270fa7db425aeafcfb27c78b4ee3f3947 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/cris/mm/ioremap.c
3 *
4 * Re-map IO memory to kernel address space so that we can access it.
5 * Needed for memory-mapped I/O devices mapped outside our normal DRAM
6 * window (that is, all memory-mapped I/O devices).
7 *
8 * (C) Copyright 1995 1996 Linus Torvalds
9 * CRIS-port by Axis Communications AB
10 */
11
12#include <linux/vmalloc.h>
13#include <asm/io.h>
14#include <asm/pgalloc.h>
15#include <asm/cacheflush.h>
16#include <asm/tlbflush.h>
Mikael Starvik4f18cfb2005-07-27 11:44:39 -070017#include <asm/arch/memmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19extern inline void remap_area_pte(pte_t * pte, unsigned long address, unsigned long size,
Mikael Starvik4f18cfb2005-07-27 11:44:39 -070020 unsigned long phys_addr, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
22 unsigned long end;
23
24 address &= ~PMD_MASK;
25 end = address + size;
26 if (end > PMD_SIZE)
27 end = PMD_SIZE;
28 if (address >= end)
29 BUG();
30 do {
31 if (!pte_none(*pte)) {
32 printk("remap_area_pte: page already exists\n");
33 BUG();
34 }
Mikael Starvik4f18cfb2005-07-27 11:44:39 -070035 set_pte(pte, mk_pte_phys(phys_addr, prot));
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 address += PAGE_SIZE;
37 phys_addr += PAGE_SIZE;
38 pte++;
39 } while (address && (address < end));
40}
41
42static inline int remap_area_pmd(pmd_t * pmd, unsigned long address, unsigned long size,
Mikael Starvik4f18cfb2005-07-27 11:44:39 -070043 unsigned long phys_addr, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45 unsigned long end;
46
47 address &= ~PGDIR_MASK;
48 end = address + size;
49 if (end > PGDIR_SIZE)
50 end = PGDIR_SIZE;
51 phys_addr -= address;
52 if (address >= end)
53 BUG();
54 do {
55 pte_t * pte = pte_alloc_kernel(&init_mm, pmd, address);
56 if (!pte)
57 return -ENOMEM;
Mikael Starvik4f18cfb2005-07-27 11:44:39 -070058 remap_area_pte(pte, address, end - address, address + phys_addr, prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 address = (address + PMD_SIZE) & PMD_MASK;
60 pmd++;
61 } while (address && (address < end));
62 return 0;
63}
64
65static int remap_area_pages(unsigned long address, unsigned long phys_addr,
Mikael Starvik4f18cfb2005-07-27 11:44:39 -070066 unsigned long size, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
68 int error;
69 pgd_t * dir;
70 unsigned long end = address + size;
71
72 phys_addr -= address;
73 dir = pgd_offset(&init_mm, address);
74 flush_cache_all();
75 if (address >= end)
76 BUG();
77 spin_lock(&init_mm.page_table_lock);
78 do {
Mikael Starvik4f18cfb2005-07-27 11:44:39 -070079 pud_t *pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 pmd_t *pmd;
Mikael Starvik4f18cfb2005-07-27 11:44:39 -070081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 error = -ENOMEM;
Mikael Starvik4f18cfb2005-07-27 11:44:39 -070083 pud = pud_alloc(&init_mm, dir, address);
84 if (!pud)
85 break;
86 pmd = pmd_alloc(&init_mm, pud, address);
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (!pmd)
89 break;
90 if (remap_area_pmd(pmd, address, end - address,
Mikael Starvik4f18cfb2005-07-27 11:44:39 -070091 phys_addr + address, prot))
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 break;
93 error = 0;
94 address = (address + PGDIR_SIZE) & PGDIR_MASK;
95 dir++;
96 } while (address && (address < end));
97 spin_unlock(&init_mm.page_table_lock);
98 flush_tlb_all();
99 return error;
100}
101
102/*
103 * Generic mapping function (not visible outside):
104 */
105
106/*
107 * Remap an arbitrary physical address space into the kernel virtual
108 * address space. Needed when the kernel wants to access high addresses
109 * directly.
110 *
111 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
112 * have to convert them into an offset in a page-aligned mapping, but the
113 * caller shouldn't need to know that small detail.
114 */
Mikael Starvik4f18cfb2005-07-27 11:44:39 -0700115void __iomem * __ioremap_prot(unsigned long phys_addr, unsigned long size, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Mikael Starvik4f18cfb2005-07-27 11:44:39 -0700117 void __iomem * addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 struct vm_struct * area;
119 unsigned long offset, last_addr;
120
121 /* Don't allow wraparound or zero size */
122 last_addr = phys_addr + size - 1;
123 if (!size || last_addr < phys_addr)
124 return NULL;
125
126 /*
127 * Mappings have to be page-aligned
128 */
129 offset = phys_addr & ~PAGE_MASK;
130 phys_addr &= PAGE_MASK;
131 size = PAGE_ALIGN(last_addr+1) - phys_addr;
132
133 /*
134 * Ok, go for it..
135 */
136 area = get_vm_area(size, VM_IOREMAP);
137 if (!area)
138 return NULL;
Mikael Starvik4f18cfb2005-07-27 11:44:39 -0700139 addr = (void __iomem *)area->addr;
140 if (remap_area_pages((unsigned long) addr, phys_addr, size, prot)) {
141 vfree((void __force *)addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return NULL;
143 }
Mikael Starvik4f18cfb2005-07-27 11:44:39 -0700144 return (void __iomem *) (offset + (char __iomem *)addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Mikael Starvik4f18cfb2005-07-27 11:44:39 -0700147void __iomem * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
148{
149 return __ioremap_prot(phys_addr, size,
150 __pgprot(_PAGE_PRESENT | __READABLE |
151 __WRITEABLE | _PAGE_GLOBAL |
152 _PAGE_KERNEL | flags));
153}
154
155/**
156 * ioremap_nocache - map bus memory into CPU space
157 * @offset: bus address of the memory
158 * @size: size of the resource to map
159 *
160 * Must be freed with iounmap.
161 */
162
163void __iomem *ioremap_nocache (unsigned long phys_addr, unsigned long size)
164{
165 return __ioremap(phys_addr | MEM_NON_CACHEABLE, size, 0);
166}
167
168void iounmap(volatile void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
170 if (addr > high_memory)
171 return vfree((void *) (PAGE_MASK & (unsigned long) addr));
172}