2 * This file contains ioremap and related functions for 64-bit machines.
4 * Derived from arch/ppc64/mm/init.c
9 * Copyright (C) 1996 Paul Mackerras
11 * Derived from "arch/i386/mm/init.c"
12 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
15 * Rework for PPC64 port.
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.
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>
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>
39 #include <asm/pgalloc.h>
43 #include <asm/mmu_context.h>
44 #include <asm/pgtable.h>
47 #include <asm/machdep.h>
49 #include <asm/processor.h>
50 #include <asm/cputable.h>
51 #include <asm/sections.h>
52 #include <asm/firmware.h>
58 #ifdef CONFIG_PPC_BOOK3S_64
60 * partition table and process table for ISA 3.0
62 struct prtb_entry *process_tb;
63 struct patb_entry *partition_tb;
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 __kernel_virt_size;
94 EXPORT_SYMBOL(__kernel_virt_size);
95 unsigned long __vmalloc_start;
96 EXPORT_SYMBOL(__vmalloc_start);
97 unsigned long __vmalloc_end;
98 EXPORT_SYMBOL(__vmalloc_end);
99 unsigned long __kernel_io_start;
100 EXPORT_SYMBOL(__kernel_io_start);
101 struct page *vmemmap;
102 EXPORT_SYMBOL(vmemmap);
103 unsigned long __pte_frag_nr;
104 EXPORT_SYMBOL(__pte_frag_nr);
105 unsigned long __pte_frag_size_shift;
106 EXPORT_SYMBOL(__pte_frag_size_shift);
107 unsigned long ioremap_bot;
108 #else /* !CONFIG_PPC_BOOK3S_64 */
109 unsigned long ioremap_bot = IOREMAP_BASE;
113 * __ioremap_at - Low level function to establish the page tables
116 void __iomem * __ioremap_at(phys_addr_t pa, void *ea, unsigned long size,
121 /* Make sure we have the base flags */
122 if ((flags & _PAGE_PRESENT) == 0)
123 flags |= pgprot_val(PAGE_KERNEL);
125 /* We don't support the 4K PFN hack with ioremap */
126 if (flags & H_PAGE_4K_PFN)
129 WARN_ON(pa & ~PAGE_MASK);
130 WARN_ON(((unsigned long)ea) & ~PAGE_MASK);
131 WARN_ON(size & ~PAGE_MASK);
133 for (i = 0; i < size; i += PAGE_SIZE)
134 if (map_kernel_page((unsigned long)ea+i, pa+i, flags))
137 return (void __iomem *)ea;
141 * __iounmap_from - Low level function to tear down the page tables
142 * for an IO mapping. This is used for mappings that
143 * are manipulated manually, like partial unmapping of
144 * PCI IOs or ISA space.
146 void __iounmap_at(void *ea, unsigned long size)
148 WARN_ON(((unsigned long)ea) & ~PAGE_MASK);
149 WARN_ON(size & ~PAGE_MASK);
151 unmap_kernel_range((unsigned long)ea, size);
154 void __iomem * __ioremap_caller(phys_addr_t addr, unsigned long size,
155 unsigned long flags, void *caller)
157 phys_addr_t paligned;
161 * Choose an address to map it to.
162 * Once the imalloc system is running, we use it.
163 * Before that, we map using addresses going
164 * up from ioremap_bot. imalloc will use
165 * the addresses from ioremap_bot through
169 paligned = addr & PAGE_MASK;
170 size = PAGE_ALIGN(addr + size) - paligned;
172 if ((size == 0) || (paligned == 0))
175 if (slab_is_available()) {
176 struct vm_struct *area;
178 area = __get_vm_area_caller(size, VM_IOREMAP,
179 ioremap_bot, IOREMAP_END,
184 area->phys_addr = paligned;
185 ret = __ioremap_at(paligned, area->addr, size, flags);
189 ret = __ioremap_at(paligned, (void *)ioremap_bot, size, flags);
195 ret += addr & ~PAGE_MASK;
199 void __iomem * __ioremap(phys_addr_t addr, unsigned long size,
202 return __ioremap_caller(addr, size, flags, __builtin_return_address(0));
205 void __iomem * ioremap(phys_addr_t addr, unsigned long size)
207 unsigned long flags = pgprot_val(pgprot_noncached(__pgprot(0)));
208 void *caller = __builtin_return_address(0);
211 return ppc_md.ioremap(addr, size, flags, caller);
212 return __ioremap_caller(addr, size, flags, caller);
215 void __iomem * ioremap_wc(phys_addr_t addr, unsigned long size)
217 unsigned long flags = pgprot_val(pgprot_noncached_wc(__pgprot(0)));
218 void *caller = __builtin_return_address(0);
221 return ppc_md.ioremap(addr, size, flags, caller);
222 return __ioremap_caller(addr, size, flags, caller);
225 void __iomem * ioremap_prot(phys_addr_t addr, unsigned long size,
228 void *caller = __builtin_return_address(0);
230 /* writeable implies dirty for kernel addresses */
231 if (flags & _PAGE_WRITE)
232 flags |= _PAGE_DIRTY;
234 /* we don't want to let _PAGE_EXEC leak out */
235 flags &= ~_PAGE_EXEC;
237 * Force kernel mapping.
239 flags &= ~_PAGE_USER;
240 flags |= _PAGE_PRIVILEGED;
243 return ppc_md.ioremap(addr, size, flags, caller);
244 return __ioremap_caller(addr, size, flags, caller);
249 * Unmap an IO region and remove it from imalloc'd list.
250 * Access to IO memory should be serialized by driver.
252 void __iounmap(volatile void __iomem *token)
256 if (!slab_is_available())
259 addr = (void *) ((unsigned long __force)
260 PCI_FIX_ADDR(token) & PAGE_MASK);
261 if ((unsigned long)addr < ioremap_bot) {
262 printk(KERN_WARNING "Attempt to iounmap early bolted mapping"
269 void iounmap(volatile void __iomem *token)
272 ppc_md.iounmap(token);
277 EXPORT_SYMBOL(ioremap);
278 EXPORT_SYMBOL(ioremap_wc);
279 EXPORT_SYMBOL(ioremap_prot);
280 EXPORT_SYMBOL(__ioremap);
281 EXPORT_SYMBOL(__ioremap_at);
282 EXPORT_SYMBOL(iounmap);
283 EXPORT_SYMBOL(__iounmap);
284 EXPORT_SYMBOL(__iounmap_at);
286 #ifndef __PAGETABLE_PUD_FOLDED
287 /* 4 level page table */
288 struct page *pgd_page(pgd_t pgd)
291 return pte_page(pgd_pte(pgd));
292 return virt_to_page(pgd_page_vaddr(pgd));
296 struct page *pud_page(pud_t pud)
299 return pte_page(pud_pte(pud));
300 return virt_to_page(pud_page_vaddr(pud));
304 * For hugepage we have pfn in the pmd, we use PTE_RPN_SHIFT bits for flags
305 * For PTE page, we have a PTE_FRAG_SIZE (4K) aligned virtual address.
307 struct page *pmd_page(pmd_t pmd)
309 if (pmd_trans_huge(pmd) || pmd_huge(pmd) || pmd_devmap(pmd))
310 return pte_page(pmd_pte(pmd));
311 return virt_to_page(pmd_page_vaddr(pmd));
314 #ifdef CONFIG_STRICT_KERNEL_RWX
315 void mark_rodata_ro(void)
317 if (!mmu_has_feature(MMU_FTR_KERNEL_RO)) {
318 pr_warn("Warning: Unable to mark rodata read only on this CPU.\n");
323 radix__mark_rodata_ro();
325 hash__mark_rodata_ro();
328 void mark_initmem_nx(void)
331 radix__mark_initmem_nx();
333 hash__mark_initmem_nx();