powerpc/mm: pmd_devmap implies pmd_large().
[linux-2.6-block.git] / arch / powerpc / mm / pgtable_64.c
1 /*
2  *  This file contains ioremap and related functions for 64-bit machines.
3  *
4  *  Derived from arch/ppc64/mm/init.c
5  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
6  *
7  *  Modifications by Paul Mackerras (PowerMac) (paulus@samba.org)
8  *  and Cort Dougan (PReP) (cort@cs.nmt.edu)
9  *    Copyright (C) 1996 Paul Mackerras
10  *
11  *  Derived from "arch/i386/mm/init.c"
12  *    Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
13  *
14  *  Dave Engebretsen <engebret@us.ibm.com>
15  *      Rework for PPC64 port.
16  *
17  *  This program is free software; you can redistribute it and/or
18  *  modify it under the terms of the GNU General Public License
19  *  as published by the Free Software Foundation; either version
20  *  2 of the License, or (at your option) any later version.
21  *
22  */
23
24 #include <linux/signal.h>
25 #include <linux/sched.h>
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/string.h>
29 #include <linux/export.h>
30 #include <linux/types.h>
31 #include <linux/mman.h>
32 #include <linux/mm.h>
33 #include <linux/swap.h>
34 #include <linux/stddef.h>
35 #include <linux/vmalloc.h>
36 #include <linux/slab.h>
37 #include <linux/hugetlb.h>
38
39 #include <asm/pgalloc.h>
40 #include <asm/page.h>
41 #include <asm/prom.h>
42 #include <asm/io.h>
43 #include <asm/mmu_context.h>
44 #include <asm/pgtable.h>
45 #include <asm/mmu.h>
46 #include <asm/smp.h>
47 #include <asm/machdep.h>
48 #include <asm/tlb.h>
49 #include <asm/processor.h>
50 #include <asm/cputable.h>
51 #include <asm/sections.h>
52 #include <asm/firmware.h>
53 #include <asm/dma.h>
54
55 #include <mm/mmu_decl.h>
56
57
58 #ifdef CONFIG_PPC_BOOK3S_64
59 /*
60  * partition table and process table for ISA 3.0
61  */
62 struct prtb_entry *process_tb;
63 struct patb_entry *partition_tb;
64 /*
65  * page table size
66  */
67 unsigned long __pte_index_size;
68 EXPORT_SYMBOL(__pte_index_size);
69 unsigned long __pmd_index_size;
70 EXPORT_SYMBOL(__pmd_index_size);
71 unsigned long __pud_index_size;
72 EXPORT_SYMBOL(__pud_index_size);
73 unsigned long __pgd_index_size;
74 EXPORT_SYMBOL(__pgd_index_size);
75 unsigned long __pud_cache_index;
76 EXPORT_SYMBOL(__pud_cache_index);
77 unsigned long __pte_table_size;
78 EXPORT_SYMBOL(__pte_table_size);
79 unsigned long __pmd_table_size;
80 EXPORT_SYMBOL(__pmd_table_size);
81 unsigned long __pud_table_size;
82 EXPORT_SYMBOL(__pud_table_size);
83 unsigned long __pgd_table_size;
84 EXPORT_SYMBOL(__pgd_table_size);
85 unsigned long __pmd_val_bits;
86 EXPORT_SYMBOL(__pmd_val_bits);
87 unsigned long __pud_val_bits;
88 EXPORT_SYMBOL(__pud_val_bits);
89 unsigned long __pgd_val_bits;
90 EXPORT_SYMBOL(__pgd_val_bits);
91 unsigned long __kernel_virt_start;
92 EXPORT_SYMBOL(__kernel_virt_start);
93 unsigned long __vmalloc_start;
94 EXPORT_SYMBOL(__vmalloc_start);
95 unsigned long __vmalloc_end;
96 EXPORT_SYMBOL(__vmalloc_end);
97 unsigned long __kernel_io_start;
98 EXPORT_SYMBOL(__kernel_io_start);
99 unsigned long __kernel_io_end;
100 struct page *vmemmap;
101 EXPORT_SYMBOL(vmemmap);
102 unsigned long __pte_frag_nr;
103 EXPORT_SYMBOL(__pte_frag_nr);
104 unsigned long __pte_frag_size_shift;
105 EXPORT_SYMBOL(__pte_frag_size_shift);
106 unsigned long ioremap_bot;
107 #else /* !CONFIG_PPC_BOOK3S_64 */
108 unsigned long ioremap_bot = IOREMAP_BASE;
109 #endif
110
111 int __weak ioremap_range(unsigned long ea, phys_addr_t pa, unsigned long size, pgprot_t prot, int nid)
112 {
113         unsigned long i;
114
115         for (i = 0; i < size; i += PAGE_SIZE) {
116                 int err = map_kernel_page(ea + i, pa + i, prot);
117                 if (err) {
118                         if (slab_is_available())
119                                 unmap_kernel_range(ea, size);
120                         else
121                                 WARN_ON_ONCE(1); /* Should clean up */
122                         return err;
123                 }
124         }
125
126         return 0;
127 }
128
129 /**
130  * __ioremap_at - Low level function to establish the page tables
131  *                for an IO mapping
132  */
133 void __iomem *__ioremap_at(phys_addr_t pa, void *ea, unsigned long size, pgprot_t prot)
134 {
135         /* We don't support the 4K PFN hack with ioremap */
136         if (pgprot_val(prot) & H_PAGE_4K_PFN)
137                 return NULL;
138
139         if ((ea + size) >= (void *)IOREMAP_END) {
140                 pr_warn("Outside the supported range\n");
141                 return NULL;
142         }
143
144         WARN_ON(pa & ~PAGE_MASK);
145         WARN_ON(((unsigned long)ea) & ~PAGE_MASK);
146         WARN_ON(size & ~PAGE_MASK);
147
148         if (ioremap_range((unsigned long)ea, pa, size, prot, NUMA_NO_NODE))
149                 return NULL;
150
151         return (void __iomem *)ea;
152 }
153
154 /**
155  * __iounmap_from - Low level function to tear down the page tables
156  *                  for an IO mapping. This is used for mappings that
157  *                  are manipulated manually, like partial unmapping of
158  *                  PCI IOs or ISA space.
159  */
160 void __iounmap_at(void *ea, unsigned long size)
161 {
162         WARN_ON(((unsigned long)ea) & ~PAGE_MASK);
163         WARN_ON(size & ~PAGE_MASK);
164
165         unmap_kernel_range((unsigned long)ea, size);
166 }
167
168 void __iomem * __ioremap_caller(phys_addr_t addr, unsigned long size,
169                                 pgprot_t prot, void *caller)
170 {
171         phys_addr_t paligned;
172         void __iomem *ret;
173
174         /*
175          * Choose an address to map it to.
176          * Once the imalloc system is running, we use it.
177          * Before that, we map using addresses going
178          * up from ioremap_bot.  imalloc will use
179          * the addresses from ioremap_bot through
180          * IMALLOC_END
181          * 
182          */
183         paligned = addr & PAGE_MASK;
184         size = PAGE_ALIGN(addr + size) - paligned;
185
186         if ((size == 0) || (paligned == 0))
187                 return NULL;
188
189         if (slab_is_available()) {
190                 struct vm_struct *area;
191
192                 area = __get_vm_area_caller(size, VM_IOREMAP,
193                                             ioremap_bot, IOREMAP_END,
194                                             caller);
195                 if (area == NULL)
196                         return NULL;
197
198                 area->phys_addr = paligned;
199                 ret = __ioremap_at(paligned, area->addr, size, prot);
200         } else {
201                 ret = __ioremap_at(paligned, (void *)ioremap_bot, size, prot);
202                 if (ret)
203                         ioremap_bot += size;
204         }
205
206         if (ret)
207                 ret += addr & ~PAGE_MASK;
208         return ret;
209 }
210
211 void __iomem * __ioremap(phys_addr_t addr, unsigned long size,
212                          unsigned long flags)
213 {
214         return __ioremap_caller(addr, size, __pgprot(flags), __builtin_return_address(0));
215 }
216
217 void __iomem * ioremap(phys_addr_t addr, unsigned long size)
218 {
219         pgprot_t prot = pgprot_noncached(PAGE_KERNEL);
220         void *caller = __builtin_return_address(0);
221
222         if (ppc_md.ioremap)
223                 return ppc_md.ioremap(addr, size, prot, caller);
224         return __ioremap_caller(addr, size, prot, caller);
225 }
226
227 void __iomem * ioremap_wc(phys_addr_t addr, unsigned long size)
228 {
229         pgprot_t prot = pgprot_noncached_wc(PAGE_KERNEL);
230         void *caller = __builtin_return_address(0);
231
232         if (ppc_md.ioremap)
233                 return ppc_md.ioremap(addr, size, prot, caller);
234         return __ioremap_caller(addr, size, prot, caller);
235 }
236
237 void __iomem *ioremap_coherent(phys_addr_t addr, unsigned long size)
238 {
239         pgprot_t prot = pgprot_cached(PAGE_KERNEL);
240         void *caller = __builtin_return_address(0);
241
242         if (ppc_md.ioremap)
243                 return ppc_md.ioremap(addr, size, prot, caller);
244         return __ioremap_caller(addr, size, prot, caller);
245 }
246
247 void __iomem * ioremap_prot(phys_addr_t addr, unsigned long size,
248                              unsigned long flags)
249 {
250         pte_t pte = __pte(flags);
251         void *caller = __builtin_return_address(0);
252
253         /* writeable implies dirty for kernel addresses */
254         if (pte_write(pte))
255                 pte = pte_mkdirty(pte);
256
257         /* we don't want to let _PAGE_EXEC leak out */
258         pte = pte_exprotect(pte);
259         /*
260          * Force kernel mapping.
261          */
262         pte = pte_mkprivileged(pte);
263
264         if (ppc_md.ioremap)
265                 return ppc_md.ioremap(addr, size, pte_pgprot(pte), caller);
266         return __ioremap_caller(addr, size, pte_pgprot(pte), caller);
267 }
268
269
270 /*  
271  * Unmap an IO region and remove it from imalloc'd list.
272  * Access to IO memory should be serialized by driver.
273  */
274 void __iounmap(volatile void __iomem *token)
275 {
276         void *addr;
277
278         if (!slab_is_available())
279                 return;
280         
281         addr = (void *) ((unsigned long __force)
282                          PCI_FIX_ADDR(token) & PAGE_MASK);
283         if ((unsigned long)addr < ioremap_bot) {
284                 printk(KERN_WARNING "Attempt to iounmap early bolted mapping"
285                        " at 0x%p\n", addr);
286                 return;
287         }
288         vunmap(addr);
289 }
290
291 void iounmap(volatile void __iomem *token)
292 {
293         if (ppc_md.iounmap)
294                 ppc_md.iounmap(token);
295         else
296                 __iounmap(token);
297 }
298
299 EXPORT_SYMBOL(ioremap);
300 EXPORT_SYMBOL(ioremap_wc);
301 EXPORT_SYMBOL(ioremap_prot);
302 EXPORT_SYMBOL(__ioremap);
303 EXPORT_SYMBOL(__ioremap_at);
304 EXPORT_SYMBOL(iounmap);
305 EXPORT_SYMBOL(__iounmap);
306 EXPORT_SYMBOL(__iounmap_at);
307
308 #ifndef __PAGETABLE_PUD_FOLDED
309 /* 4 level page table */
310 struct page *pgd_page(pgd_t pgd)
311 {
312         if (pgd_is_leaf(pgd)) {
313                 VM_WARN_ON(!pgd_huge(pgd));
314                 return pte_page(pgd_pte(pgd));
315         }
316         return virt_to_page(pgd_page_vaddr(pgd));
317 }
318 #endif
319
320 struct page *pud_page(pud_t pud)
321 {
322         if (pud_is_leaf(pud)) {
323                 VM_WARN_ON(!pud_huge(pud));
324                 return pte_page(pud_pte(pud));
325         }
326         return virt_to_page(pud_page_vaddr(pud));
327 }
328
329 /*
330  * For hugepage we have pfn in the pmd, we use PTE_RPN_SHIFT bits for flags
331  * For PTE page, we have a PTE_FRAG_SIZE (4K) aligned virtual address.
332  */
333 struct page *pmd_page(pmd_t pmd)
334 {
335         if (pmd_is_leaf(pmd)) {
336                 VM_WARN_ON(!(pmd_large(pmd) || pmd_huge(pmd)));
337                 return pte_page(pmd_pte(pmd));
338         }
339         return virt_to_page(pmd_page_vaddr(pmd));
340 }
341
342 #ifdef CONFIG_STRICT_KERNEL_RWX
343 void mark_rodata_ro(void)
344 {
345         if (!mmu_has_feature(MMU_FTR_KERNEL_RO)) {
346                 pr_warn("Warning: Unable to mark rodata read only on this CPU.\n");
347                 return;
348         }
349
350         if (radix_enabled())
351                 radix__mark_rodata_ro();
352         else
353                 hash__mark_rodata_ro();
354
355         // mark_initmem_nx() should have already run by now
356         ptdump_check_wx();
357 }
358
359 void mark_initmem_nx(void)
360 {
361         if (radix_enabled())
362                 radix__mark_initmem_nx();
363         else
364                 hash__mark_initmem_nx();
365 }
366 #endif