File: /usr/src/linux/arch/mips/mm/ioremap.c
1 /*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * (C) Copyright 1995 1996 Linus Torvalds
7 * (C) Copyright 2001 Ralf Baechle
8 */
9 #include <linux/module.h>
10 #include <asm/addrspace.h>
11 #include <asm/byteorder.h>
12
13 #include <linux/vmalloc.h>
14 #include <asm/io.h>
15 #include <asm/pgalloc.h>
16
17 static inline void remap_area_pte(pte_t * pte, unsigned long address, unsigned long size,
18 unsigned long phys_addr, unsigned long flags)
19 {
20 unsigned long end;
21 pgprot_t pgprot = __pgprot(_PAGE_GLOBAL | _PAGE_PRESENT | __READABLE
22 | __WRITEABLE | flags);
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 }
35 set_pte(pte, mk_pte_phys(phys_addr, pgprot));
36 address += PAGE_SIZE;
37 phys_addr += PAGE_SIZE;
38 pte++;
39 } while (address && (address < end));
40 }
41
42 static inline int remap_area_pmd(pmd_t * pmd, unsigned long address, unsigned long size,
43 unsigned long phys_addr, unsigned long flags)
44 {
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(&init_mm, pmd, address);
56 if (!pte)
57 return -ENOMEM;
58 remap_area_pte(pte, address, end - address, address + phys_addr, flags);
59 address = (address + PMD_SIZE) & PMD_MASK;
60 pmd++;
61 } while (address && (address < end));
62 return 0;
63 }
64
65 static int remap_area_pages(unsigned long address, unsigned long phys_addr,
66 unsigned long size, unsigned long flags)
67 {
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 {
79 pmd_t *pmd;
80 pmd = pmd_alloc(&init_mm, dir, address);
81 error = -ENOMEM;
82 if (!pmd)
83 break;
84 if (remap_area_pmd(pmd, address, end - address,
85 phys_addr + address, flags))
86 break;
87 error = 0;
88 address = (address + PGDIR_SIZE) & PGDIR_MASK;
89 dir++;
90 } while (address && (address < end));
91 spin_unlock(&init_mm.page_table_lock);
92 flush_tlb_all();
93 return error;
94 }
95
96 /*
97 * Generic mapping function (not visible outside):
98 */
99
100 /*
101 * Remap an arbitrary physical address space into the kernel virtual
102 * address space. Needed when the kernel wants to access high addresses
103 * directly.
104 *
105 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
106 * have to convert them into an offset in a page-aligned mapping, but the
107 * caller shouldn't need to know that small detail.
108 */
109
110 #define IS_LOW512(addr) (!((unsigned long)(addr) & ~0x1fffffffUL))
111
112 void * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
113 {
114 void * addr;
115 struct vm_struct * area;
116 unsigned long offset, last_addr;
117
118 /* Don't allow wraparound or zero size */
119 last_addr = phys_addr + size - 1;
120 if (!size || last_addr < phys_addr)
121 return NULL;
122
123 /*
124 * Map objects in the low 512mb of address space using KSEG1, otherwise
125 * map using page tables.
126 */
127 if (IS_LOW512(phys_addr) && IS_LOW512(phys_addr + size - 1))
128 return (void *) KSEG1ADDR(phys_addr);
129
130 /*
131 * Don't allow anybody to remap normal RAM that we're using..
132 */
133 if (phys_addr < virt_to_phys(high_memory)) {
134 char *t_addr, *t_end;
135 struct page *page;
136
137 t_addr = __va(phys_addr);
138 t_end = t_addr + (size - 1);
139
140 for(page = virt_to_page(t_addr); page <= virt_to_page(t_end); page++)
141 if(!PageReserved(page))
142 return NULL;
143 }
144
145 /*
146 * Mappings have to be page-aligned
147 */
148 offset = phys_addr & ~PAGE_MASK;
149 phys_addr &= PAGE_MASK;
150 size = PAGE_ALIGN(last_addr) - phys_addr;
151
152 /*
153 * Ok, go for it..
154 */
155 area = get_vm_area(size, VM_IOREMAP);
156 if (!area)
157 return NULL;
158 addr = area->addr;
159 if (remap_area_pages(VMALLOC_VMADDR(addr), phys_addr, size, flags)) {
160 vfree(addr);
161 return NULL;
162 }
163
164 return (void *) (offset + (char *)addr);
165 }
166
167 #define IS_KSEG1(addr) (((unsigned long)(addr) & ~0x1fffffffUL) == KSEG1)
168
169 void iounmap(void *addr)
170 {
171 if (!IS_KSEG1(addr))
172 return vfree((void *) (PAGE_MASK & (unsigned long) addr));
173 }
174
175 EXPORT_SYMBOL(__ioremap);
176 EXPORT_SYMBOL(iounmap);
177