]> asedeno.scripts.mit.edu Git - linux.git/blob - arch/powerpc/mm/pgtable_32.c
powerpc/mm: move common 32/64 bits ioremap functions into ioremap.c
[linux.git] / arch / powerpc / mm / pgtable_32.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * This file contains the routines setting up the linux page tables.
4  *  -- paulus
5  *
6  *  Derived from arch/ppc/mm/init.c:
7  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
8  *
9  *  Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
10  *  and Cort Dougan (PReP) (cort@cs.nmt.edu)
11  *    Copyright (C) 1996 Paul Mackerras
12  *
13  *  Derived from "arch/i386/mm/init.c"
14  *    Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
15  */
16
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/mm.h>
21 #include <linux/vmalloc.h>
22 #include <linux/init.h>
23 #include <linux/highmem.h>
24 #include <linux/memblock.h>
25 #include <linux/slab.h>
26
27 #include <asm/pgtable.h>
28 #include <asm/pgalloc.h>
29 #include <asm/fixmap.h>
30 #include <asm/io.h>
31 #include <asm/setup.h>
32 #include <asm/sections.h>
33
34 #include <mm/mmu_decl.h>
35
36 unsigned long ioremap_bot;
37 EXPORT_SYMBOL(ioremap_bot);     /* aka VMALLOC_END */
38
39 extern char etext[], _stext[], _sinittext[], _einittext[];
40
41 void __iomem *
42 ioremap_wt(phys_addr_t addr, unsigned long size)
43 {
44         pgprot_t prot = pgprot_cached_wthru(PAGE_KERNEL);
45
46         return __ioremap_caller(addr, size, prot, __builtin_return_address(0));
47 }
48 EXPORT_SYMBOL(ioremap_wt);
49
50 void __iomem *
51 ioremap_prot(phys_addr_t addr, unsigned long size, unsigned long flags)
52 {
53         pte_t pte = __pte(flags);
54
55         /* writeable implies dirty for kernel addresses */
56         if (pte_write(pte))
57                 pte = pte_mkdirty(pte);
58
59         /* we don't want to let _PAGE_USER and _PAGE_EXEC leak out */
60         pte = pte_exprotect(pte);
61         pte = pte_mkprivileged(pte);
62
63         return __ioremap_caller(addr, size, pte_pgprot(pte), __builtin_return_address(0));
64 }
65 EXPORT_SYMBOL(ioremap_prot);
66
67 void __iomem *
68 __ioremap_caller(phys_addr_t addr, unsigned long size, pgprot_t prot, void *caller)
69 {
70         unsigned long v, i;
71         phys_addr_t p;
72         int err;
73
74         /*
75          * Choose an address to map it to.
76          * Once the vmalloc system is running, we use it.
77          * Before then, we use space going down from IOREMAP_TOP
78          * (ioremap_bot records where we're up to).
79          */
80         p = addr & PAGE_MASK;
81         size = PAGE_ALIGN(addr + size) - p;
82
83         /*
84          * If the address lies within the first 16 MB, assume it's in ISA
85          * memory space
86          */
87         if (p < 16*1024*1024)
88                 p += _ISA_MEM_BASE;
89
90 #ifndef CONFIG_CRASH_DUMP
91         /*
92          * Don't allow anybody to remap normal RAM that we're using.
93          * mem_init() sets high_memory so only do the check after that.
94          */
95         if (slab_is_available() && p <= virt_to_phys(high_memory - 1) &&
96             page_is_ram(__phys_to_pfn(p))) {
97                 pr_warn("%s(): phys addr 0x%llx is RAM lr %ps\n", __func__,
98                         (unsigned long long)p, __builtin_return_address(0));
99                 return NULL;
100         }
101 #endif
102
103         if (size == 0)
104                 return NULL;
105
106         /*
107          * Is it already mapped?  Perhaps overlapped by a previous
108          * mapping.
109          */
110         v = p_block_mapped(p);
111         if (v)
112                 goto out;
113
114         if (slab_is_available()) {
115                 struct vm_struct *area;
116                 area = get_vm_area_caller(size, VM_IOREMAP, caller);
117                 if (area == 0)
118                         return NULL;
119                 area->phys_addr = p;
120                 v = (unsigned long) area->addr;
121         } else {
122                 v = (ioremap_bot -= size);
123         }
124
125         /*
126          * Should check if it is a candidate for a BAT mapping
127          */
128
129         err = 0;
130         for (i = 0; i < size && err == 0; i += PAGE_SIZE)
131                 err = map_kernel_page(v + i, p + i, prot);
132         if (err) {
133                 if (slab_is_available())
134                         vunmap((void *)v);
135                 return NULL;
136         }
137
138 out:
139         return (void __iomem *) (v + ((unsigned long)addr & ~PAGE_MASK));
140 }
141
142 void iounmap(volatile void __iomem *addr)
143 {
144         /*
145          * If mapped by BATs then there is nothing to do.
146          * Calling vfree() generates a benign warning.
147          */
148         if (v_block_mapped((unsigned long)addr))
149                 return;
150
151         if (addr > high_memory && (unsigned long) addr < ioremap_bot)
152                 vunmap((void *) (PAGE_MASK & (unsigned long)addr));
153 }
154 EXPORT_SYMBOL(iounmap);
155
156 static void __init *early_alloc_pgtable(unsigned long size)
157 {
158         void *ptr = memblock_alloc(size, size);
159
160         if (!ptr)
161                 panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
162                       __func__, size, size);
163
164         return ptr;
165 }
166
167 static pte_t __init *early_pte_alloc_kernel(pmd_t *pmdp, unsigned long va)
168 {
169         if (pmd_none(*pmdp)) {
170                 pte_t *ptep = early_alloc_pgtable(PTE_FRAG_SIZE);
171
172                 pmd_populate_kernel(&init_mm, pmdp, ptep);
173         }
174         return pte_offset_kernel(pmdp, va);
175 }
176
177
178 int __ref map_kernel_page(unsigned long va, phys_addr_t pa, pgprot_t prot)
179 {
180         pmd_t *pd;
181         pte_t *pg;
182         int err = -ENOMEM;
183
184         /* Use upper 10 bits of VA to index the first level map */
185         pd = pmd_offset(pud_offset(pgd_offset_k(va), va), va);
186         /* Use middle 10 bits of VA to index the second-level map */
187         if (likely(slab_is_available()))
188                 pg = pte_alloc_kernel(pd, va);
189         else
190                 pg = early_pte_alloc_kernel(pd, va);
191         if (pg != 0) {
192                 err = 0;
193                 /* The PTE should never be already set nor present in the
194                  * hash table
195                  */
196                 BUG_ON((pte_present(*pg) | pte_hashpte(*pg)) && pgprot_val(prot));
197                 set_pte_at(&init_mm, va, pg, pfn_pte(pa >> PAGE_SHIFT, prot));
198         }
199         smp_wmb();
200         return err;
201 }
202
203 /*
204  * Map in a chunk of physical memory starting at start.
205  */
206 static void __init __mapin_ram_chunk(unsigned long offset, unsigned long top)
207 {
208         unsigned long v, s;
209         phys_addr_t p;
210         int ktext;
211
212         s = offset;
213         v = PAGE_OFFSET + s;
214         p = memstart_addr + s;
215         for (; s < top; s += PAGE_SIZE) {
216                 ktext = ((char *)v >= _stext && (char *)v < etext) ||
217                         ((char *)v >= _sinittext && (char *)v < _einittext);
218                 map_kernel_page(v, p, ktext ? PAGE_KERNEL_TEXT : PAGE_KERNEL);
219 #ifdef CONFIG_PPC_BOOK3S_32
220                 if (ktext)
221                         hash_preload(&init_mm, v);
222 #endif
223                 v += PAGE_SIZE;
224                 p += PAGE_SIZE;
225         }
226 }
227
228 void __init mapin_ram(void)
229 {
230         struct memblock_region *reg;
231
232         for_each_memblock(memory, reg) {
233                 phys_addr_t base = reg->base;
234                 phys_addr_t top = min(base + reg->size, total_lowmem);
235
236                 if (base >= top)
237                         continue;
238                 base = mmu_mapin_ram(base, top);
239                 if (IS_ENABLED(CONFIG_BDI_SWITCH))
240                         __mapin_ram_chunk(reg->base, top);
241                 else
242                         __mapin_ram_chunk(base, top);
243         }
244 }
245
246 /* Scan the real Linux page tables and return a PTE pointer for
247  * a virtual address in a context.
248  * Returns true (1) if PTE was found, zero otherwise.  The pointer to
249  * the PTE pointer is unmodified if PTE is not found.
250  */
251 static int
252 get_pteptr(struct mm_struct *mm, unsigned long addr, pte_t **ptep, pmd_t **pmdp)
253 {
254         pgd_t   *pgd;
255         pud_t   *pud;
256         pmd_t   *pmd;
257         pte_t   *pte;
258         int     retval = 0;
259
260         pgd = pgd_offset(mm, addr & PAGE_MASK);
261         if (pgd) {
262                 pud = pud_offset(pgd, addr & PAGE_MASK);
263                 if (pud && pud_present(*pud)) {
264                         pmd = pmd_offset(pud, addr & PAGE_MASK);
265                         if (pmd_present(*pmd)) {
266                                 pte = pte_offset_map(pmd, addr & PAGE_MASK);
267                                 if (pte) {
268                                         retval = 1;
269                                         *ptep = pte;
270                                         if (pmdp)
271                                                 *pmdp = pmd;
272                                         /* XXX caller needs to do pte_unmap, yuck */
273                                 }
274                         }
275                 }
276         }
277         return(retval);
278 }
279
280 static int __change_page_attr_noflush(struct page *page, pgprot_t prot)
281 {
282         pte_t *kpte;
283         pmd_t *kpmd;
284         unsigned long address;
285
286         BUG_ON(PageHighMem(page));
287         address = (unsigned long)page_address(page);
288
289         if (v_block_mapped(address))
290                 return 0;
291         if (!get_pteptr(&init_mm, address, &kpte, &kpmd))
292                 return -EINVAL;
293         __set_pte_at(&init_mm, address, kpte, mk_pte(page, prot), 0);
294         pte_unmap(kpte);
295
296         return 0;
297 }
298
299 /*
300  * Change the page attributes of an page in the linear mapping.
301  *
302  * THIS DOES NOTHING WITH BAT MAPPINGS, DEBUG USE ONLY
303  */
304 static int change_page_attr(struct page *page, int numpages, pgprot_t prot)
305 {
306         int i, err = 0;
307         unsigned long flags;
308         struct page *start = page;
309
310         local_irq_save(flags);
311         for (i = 0; i < numpages; i++, page++) {
312                 err = __change_page_attr_noflush(page, prot);
313                 if (err)
314                         break;
315         }
316         wmb();
317         local_irq_restore(flags);
318         flush_tlb_kernel_range((unsigned long)page_address(start),
319                                (unsigned long)page_address(page));
320         return err;
321 }
322
323 void mark_initmem_nx(void)
324 {
325         struct page *page = virt_to_page(_sinittext);
326         unsigned long numpages = PFN_UP((unsigned long)_einittext) -
327                                  PFN_DOWN((unsigned long)_sinittext);
328
329         if (v_block_mapped((unsigned long)_stext + 1))
330                 mmu_mark_initmem_nx();
331         else
332                 change_page_attr(page, numpages, PAGE_KERNEL);
333 }
334
335 #ifdef CONFIG_STRICT_KERNEL_RWX
336 void mark_rodata_ro(void)
337 {
338         struct page *page;
339         unsigned long numpages;
340
341         if (v_block_mapped((unsigned long)_sinittext)) {
342                 mmu_mark_rodata_ro();
343                 return;
344         }
345
346         page = virt_to_page(_stext);
347         numpages = PFN_UP((unsigned long)_etext) -
348                    PFN_DOWN((unsigned long)_stext);
349
350         change_page_attr(page, numpages, PAGE_KERNEL_ROX);
351         /*
352          * mark .rodata as read only. Use __init_begin rather than __end_rodata
353          * to cover NOTES and EXCEPTION_TABLE.
354          */
355         page = virt_to_page(__start_rodata);
356         numpages = PFN_UP((unsigned long)__init_begin) -
357                    PFN_DOWN((unsigned long)__start_rodata);
358
359         change_page_attr(page, numpages, PAGE_KERNEL_RO);
360
361         // mark_initmem_nx() should have already run by now
362         ptdump_check_wx();
363 }
364 #endif
365
366 #ifdef CONFIG_DEBUG_PAGEALLOC
367 void __kernel_map_pages(struct page *page, int numpages, int enable)
368 {
369         if (PageHighMem(page))
370                 return;
371
372         change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
373 }
374 #endif /* CONFIG_DEBUG_PAGEALLOC */