blob: 8de9db2b4d96a1a476569115eb645d747727d849 [file] [log] [blame]
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001/*
2 * This file contains the routines setting up the linux page tables.
3 * -- paulus
4 *
5 * Derived from arch/ppc/mm/init.c:
6 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
7 *
8 * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
9 * and Cort Dougan (PReP) (cort@cs.nmt.edu)
10 * Copyright (C) 1996 Paul Mackerras
Paul Mackerras14cf11a2005-09-26 16:04:21 +100011 *
12 * Derived from "arch/i386/mm/init.c"
13 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
19 *
20 */
21
Paul Mackerras14cf11a2005-09-26 16:04:21 +100022#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/types.h>
25#include <linux/mm.h>
26#include <linux/vmalloc.h>
27#include <linux/init.h>
28#include <linux/highmem.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100029#include <linux/memblock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100031
32#include <asm/pgtable.h>
33#include <asm/pgalloc.h>
Kumar Gala2c419bd2008-04-23 23:05:20 +100034#include <asm/fixmap.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100035#include <asm/io.h>
David Howellsae3a1972012-03-28 18:30:02 +010036#include <asm/setup.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100037
38#include "mmu_decl.h"
39
Paul Mackerras14cf11a2005-09-26 16:04:21 +100040unsigned long ioremap_bot;
Olaf Hering920573b2006-03-14 21:21:11 +010041EXPORT_SYMBOL(ioremap_bot); /* aka VMALLOC_END */
Paul Mackerras14cf11a2005-09-26 16:04:21 +100042
Christophe Leroy060ef9d2016-02-10 08:17:08 +010043extern char etext[], _stext[], _sinittext[], _einittext[];
Paul Mackerras14cf11a2005-09-26 16:04:21 +100044
Fabian Frederickbd721ea2016-08-02 14:03:33 -070045__ref pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100046{
47 pte_t *pte;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100048
Michael Ellermanf691fa12015-03-30 14:10:37 +110049 if (slab_is_available()) {
Michal Hocko32d6bd92016-06-24 14:48:47 -070050 pte = (pte_t *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100051 } else {
Anton Blanchard10239732014-09-17 22:15:33 +100052 pte = __va(memblock_alloc(PAGE_SIZE, PAGE_SIZE));
Paul Mackerras14cf11a2005-09-26 16:04:21 +100053 if (pte)
54 clear_page(pte);
55 }
56 return pte;
57}
58
Martin Schwidefsky2f569af2008-02-08 04:22:04 -080059pgtable_t pte_alloc_one(struct mm_struct *mm, unsigned long address)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100060{
61 struct page *ptepage;
62
Balbir Singhabd667b2017-05-02 15:17:05 +100063 gfp_t flags = GFP_KERNEL | __GFP_ZERO | __GFP_ACCOUNT;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100064
65 ptepage = alloc_pages(flags, 0);
Martin Schwidefsky2f569af2008-02-08 04:22:04 -080066 if (!ptepage)
67 return NULL;
Kirill A. Shutemov4f8049432013-11-14 14:31:38 -080068 if (!pgtable_page_ctor(ptepage)) {
69 __free_page(ptepage);
70 return NULL;
71 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +100072 return ptepage;
73}
74
Paul Mackerras14cf11a2005-09-26 16:04:21 +100075void __iomem *
76ioremap(phys_addr_t addr, unsigned long size)
77{
Benjamin Herrenschmidt1cdab552009-02-22 16:19:14 +000078 return __ioremap_caller(addr, size, _PAGE_NO_CACHE | _PAGE_GUARDED,
79 __builtin_return_address(0));
Paul Mackerras14cf11a2005-09-26 16:04:21 +100080}
Olaf Hering920573b2006-03-14 21:21:11 +010081EXPORT_SYMBOL(ioremap);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100082
83void __iomem *
Anton Blanchardbe135f42011-05-08 21:41:59 +000084ioremap_wc(phys_addr_t addr, unsigned long size)
85{
86 return __ioremap_caller(addr, size, _PAGE_NO_CACHE,
87 __builtin_return_address(0));
88}
89EXPORT_SYMBOL(ioremap_wc);
90
91void __iomem *
Anton Blanchard40f1ce72011-05-08 21:43:47 +000092ioremap_prot(phys_addr_t addr, unsigned long size, unsigned long flags)
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +110093{
Benjamin Herrenschmidta1f242f2008-07-23 21:27:08 -070094 /* writeable implies dirty for kernel addresses */
LEROY Christophea7b9f672015-01-19 17:04:38 +010095 if ((flags & (_PAGE_RW | _PAGE_RO)) != _PAGE_RO)
Benjamin Herrenschmidta1f242f2008-07-23 21:27:08 -070096 flags |= _PAGE_DIRTY | _PAGE_HWWRITE;
97
98 /* we don't want to let _PAGE_USER and _PAGE_EXEC leak out */
Benjamin Herrenschmidtea3cc332009-08-18 19:00:34 +000099 flags &= ~(_PAGE_USER | _PAGE_EXEC);
Benjamin Herrenschmidta1f242f2008-07-23 21:27:08 -0700100
Benjamin Herrenschmidt55052ee2010-04-07 14:39:36 +1000101#ifdef _PAGE_BAP_SR
102 /* _PAGE_USER contains _PAGE_BAP_SR on BookE using the new PTE format
103 * which means that we just cleared supervisor access... oops ;-) This
104 * restores it
105 */
106 flags |= _PAGE_BAP_SR;
107#endif
108
Benjamin Herrenschmidt1cdab552009-02-22 16:19:14 +0000109 return __ioremap_caller(addr, size, flags, __builtin_return_address(0));
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100110}
Anton Blanchard40f1ce72011-05-08 21:43:47 +0000111EXPORT_SYMBOL(ioremap_prot);
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100112
113void __iomem *
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000114__ioremap(phys_addr_t addr, unsigned long size, unsigned long flags)
115{
Benjamin Herrenschmidt1cdab552009-02-22 16:19:14 +0000116 return __ioremap_caller(addr, size, flags, __builtin_return_address(0));
117}
118
119void __iomem *
120__ioremap_caller(phys_addr_t addr, unsigned long size, unsigned long flags,
121 void *caller)
122{
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000123 unsigned long v, i;
124 phys_addr_t p;
125 int err;
126
Benjamin Herrenschmidta1f242f2008-07-23 21:27:08 -0700127 /* Make sure we have the base flags */
128 if ((flags & _PAGE_PRESENT) == 0)
Michael Ellerman4f9c53c2015-03-25 20:11:57 +1100129 flags |= pgprot_val(PAGE_KERNEL);
Benjamin Herrenschmidta1f242f2008-07-23 21:27:08 -0700130
131 /* Non-cacheable page cannot be coherent */
132 if (flags & _PAGE_NO_CACHE)
133 flags &= ~_PAGE_COHERENT;
134
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000135 /*
136 * Choose an address to map it to.
137 * Once the vmalloc system is running, we use it.
Christophe Leroye974cd42016-02-09 17:08:10 +0100138 * Before then, we use space going down from IOREMAP_TOP
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000139 * (ioremap_bot records where we're up to).
140 */
141 p = addr & PAGE_MASK;
142 size = PAGE_ALIGN(addr + size) - p;
143
144 /*
145 * If the address lies within the first 16 MB, assume it's in ISA
146 * memory space
147 */
148 if (p < 16*1024*1024)
149 p += _ISA_MEM_BASE;
150
Anton Vorontsov01695a92008-12-17 10:09:10 +0000151#ifndef CONFIG_CRASH_DUMP
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000152 /*
153 * Don't allow anybody to remap normal RAM that we're using.
154 * mem_init() sets high_memory so only do the check after that.
155 */
Michael Ellermanf691fa12015-03-30 14:10:37 +1100156 if (slab_is_available() && (p < virt_to_phys(high_memory)) &&
Yinghai Lu95f72d12010-07-12 14:36:09 +1000157 !(__allow_ioremap_reserved && memblock_is_region_reserved(p, size))) {
Scott Woodcc834582015-03-11 22:13:46 -0500158 printk("__ioremap(): phys addr 0x%llx is RAM lr %ps\n",
David Gibson37f01d62007-04-24 15:05:18 +1000159 (unsigned long long)p, __builtin_return_address(0));
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000160 return NULL;
161 }
Anton Vorontsov01695a92008-12-17 10:09:10 +0000162#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000163
164 if (size == 0)
165 return NULL;
166
167 /*
168 * Is it already mapped? Perhaps overlapped by a previous
Christophe Leroy3084cdb2016-02-09 17:07:58 +0100169 * mapping.
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000170 */
Christophe Leroy3084cdb2016-02-09 17:07:58 +0100171 v = p_block_mapped(p);
172 if (v)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000173 goto out;
174
Michael Ellermanf691fa12015-03-30 14:10:37 +1100175 if (slab_is_available()) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000176 struct vm_struct *area;
Benjamin Herrenschmidt1cdab552009-02-22 16:19:14 +0000177 area = get_vm_area_caller(size, VM_IOREMAP, caller);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000178 if (area == 0)
179 return NULL;
Michael Ellerman7a9d1252010-11-28 18:26:36 +0000180 area->phys_addr = p;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000181 v = (unsigned long) area->addr;
182 } else {
183 v = (ioremap_bot -= size);
184 }
185
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000186 /*
187 * Should check if it is a candidate for a BAT mapping
188 */
189
190 err = 0;
191 for (i = 0; i < size && err == 0; i += PAGE_SIZE)
192 err = map_page(v+i, p+i, flags);
193 if (err) {
Michael Ellermanf691fa12015-03-30 14:10:37 +1100194 if (slab_is_available())
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000195 vunmap((void *)v);
196 return NULL;
197 }
198
199out:
200 return (void __iomem *) (v + ((unsigned long)addr & ~PAGE_MASK));
201}
Olaf Hering920573b2006-03-14 21:21:11 +0100202EXPORT_SYMBOL(__ioremap);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000203
204void iounmap(volatile void __iomem *addr)
205{
206 /*
207 * If mapped by BATs then there is nothing to do.
208 * Calling vfree() generates a benign warning.
209 */
Christophe Leroy3084cdb2016-02-09 17:07:58 +0100210 if (v_block_mapped((unsigned long)addr))
211 return;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000212
213 if (addr > high_memory && (unsigned long) addr < ioremap_bot)
214 vunmap((void *) (PAGE_MASK & (unsigned long)addr));
215}
Olaf Hering920573b2006-03-14 21:21:11 +0100216EXPORT_SYMBOL(iounmap);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000217
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100218int map_page(unsigned long va, phys_addr_t pa, int flags)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000219{
220 pmd_t *pd;
221 pte_t *pg;
222 int err = -ENOMEM;
223
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000224 /* Use upper 10 bits of VA to index the first level map */
David Gibsond1953c82007-05-08 12:46:49 +1000225 pd = pmd_offset(pud_offset(pgd_offset_k(va), va), va);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000226 /* Use middle 10 bits of VA to index the second-level map */
Paul Mackerrase2f2e582005-10-31 14:40:03 +1100227 pg = pte_alloc_kernel(pd, va);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000228 if (pg != 0) {
229 err = 0;
Benjamin Herrenschmidt3be4e692007-04-12 15:30:21 +1000230 /* The PTE should never be already set nor present in the
231 * hash table
232 */
Anton Vorontsov7021d862008-12-29 06:40:35 +0000233 BUG_ON((pte_val(*pg) & (_PAGE_PRESENT | _PAGE_HASHPTE)) &&
234 flags);
Benjamin Herrenschmidt3be4e692007-04-12 15:30:21 +1000235 set_pte_at(&init_mm, va, pg, pfn_pte(pa >> PAGE_SHIFT,
236 __pgprot(flags)));
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000237 }
Scott Wood47ce8af2013-10-11 19:22:37 -0500238 smp_wmb();
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000239 return err;
240}
241
242/*
Albert Herranzde324002009-12-12 06:31:53 +0000243 * Map in a chunk of physical memory starting at start.
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000244 */
Albert Herranzde324002009-12-12 06:31:53 +0000245void __init __mapin_ram_chunk(unsigned long offset, unsigned long top)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000246{
Kumar Gala99c62dd72008-04-16 05:52:21 +1000247 unsigned long v, s, f;
248 phys_addr_t p;
Benjamin Herrenschmidtee4f2ea2007-04-12 15:30:22 +1000249 int ktext;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000250
Albert Herranzde324002009-12-12 06:31:53 +0000251 s = offset;
Dale Farnsworthccdcef72008-12-17 10:09:13 +0000252 v = PAGE_OFFSET + s;
Kumar Gala99c62dd72008-04-16 05:52:21 +1000253 p = memstart_addr + s;
Albert Herranzde324002009-12-12 06:31:53 +0000254 for (; s < top; s += PAGE_SIZE) {
Christophe Leroy060ef9d2016-02-10 08:17:08 +0100255 ktext = ((char *)v >= _stext && (char *)v < etext) ||
256 ((char *)v >= _sinittext && (char *)v < _einittext);
Michael Ellerman4f9c53c2015-03-25 20:11:57 +1100257 f = ktext ? pgprot_val(PAGE_KERNEL_TEXT) : pgprot_val(PAGE_KERNEL);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000258 map_page(v, p, f);
Benjamin Herrenschmidtee4f2ea2007-04-12 15:30:22 +1000259#ifdef CONFIG_PPC_STD_MMU_32
260 if (ktext)
261 hash_preload(&init_mm, v, 0, 0x300);
262#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000263 v += PAGE_SIZE;
264 p += PAGE_SIZE;
265 }
266}
267
Albert Herranzde324002009-12-12 06:31:53 +0000268void __init mapin_ram(void)
269{
270 unsigned long s, top;
271
272#ifndef CONFIG_WII
273 top = total_lowmem;
274 s = mmu_mapin_ram(top);
275 __mapin_ram_chunk(s, top);
276#else
277 if (!wii_hole_size) {
278 s = mmu_mapin_ram(total_lowmem);
279 __mapin_ram_chunk(s, total_lowmem);
280 } else {
281 top = wii_hole_start;
282 s = mmu_mapin_ram(top);
283 __mapin_ram_chunk(s, top);
284
Yinghai Lu95f72d12010-07-12 14:36:09 +1000285 top = memblock_end_of_DRAM();
Albert Herranzde324002009-12-12 06:31:53 +0000286 s = wii_mmu_mapin_mem2(top);
287 __mapin_ram_chunk(s, top);
288 }
289#endif
290}
291
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000292/* Scan the real Linux page tables and return a PTE pointer for
293 * a virtual address in a context.
294 * Returns true (1) if PTE was found, zero otherwise. The pointer to
295 * the PTE pointer is unmodified if PTE is not found.
296 */
297int
Eugene Suroveginbab70a42006-03-28 10:13:12 -0800298get_pteptr(struct mm_struct *mm, unsigned long addr, pte_t **ptep, pmd_t **pmdp)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000299{
300 pgd_t *pgd;
David Gibsond1953c82007-05-08 12:46:49 +1000301 pud_t *pud;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000302 pmd_t *pmd;
303 pte_t *pte;
304 int retval = 0;
305
306 pgd = pgd_offset(mm, addr & PAGE_MASK);
307 if (pgd) {
David Gibsond1953c82007-05-08 12:46:49 +1000308 pud = pud_offset(pgd, addr & PAGE_MASK);
309 if (pud && pud_present(*pud)) {
310 pmd = pmd_offset(pud, addr & PAGE_MASK);
311 if (pmd_present(*pmd)) {
312 pte = pte_offset_map(pmd, addr & PAGE_MASK);
313 if (pte) {
314 retval = 1;
315 *ptep = pte;
316 if (pmdp)
317 *pmdp = pmd;
318 /* XXX caller needs to do pte_unmap, yuck */
319 }
320 }
321 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000322 }
323 return(retval);
324}
325
Benjamin Herrenschmidt88df6e92007-04-12 15:30:22 +1000326#ifdef CONFIG_DEBUG_PAGEALLOC
327
328static int __change_page_attr(struct page *page, pgprot_t prot)
329{
330 pte_t *kpte;
331 pmd_t *kpmd;
332 unsigned long address;
333
334 BUG_ON(PageHighMem(page));
335 address = (unsigned long)page_address(page);
336
Christophe Leroy3084cdb2016-02-09 17:07:58 +0100337 if (v_block_mapped(address))
Benjamin Herrenschmidt88df6e92007-04-12 15:30:22 +1000338 return 0;
339 if (!get_pteptr(&init_mm, address, &kpte, &kpmd))
340 return -EINVAL;
Benjamin Herrenschmidt50891452009-12-08 21:08:44 +0000341 __set_pte_at(&init_mm, address, kpte, mk_pte(page, prot), 0);
Benjamin Herrenschmidt88df6e92007-04-12 15:30:22 +1000342 wmb();
Benjamin Herrenschmidtf63837f2008-12-14 19:44:51 +0000343 flush_tlb_page(NULL, address);
Benjamin Herrenschmidt88df6e92007-04-12 15:30:22 +1000344 pte_unmap(kpte);
345
346 return 0;
347}
348
349/*
350 * Change the page attributes of an page in the linear mapping.
351 *
352 * THIS CONFLICTS WITH BAT MAPPINGS, DEBUG USE ONLY
353 */
354static int change_page_attr(struct page *page, int numpages, pgprot_t prot)
355{
356 int i, err = 0;
357 unsigned long flags;
358
359 local_irq_save(flags);
360 for (i = 0; i < numpages; i++, page++) {
361 err = __change_page_attr(page, prot);
362 if (err)
363 break;
364 }
365 local_irq_restore(flags);
366 return err;
367}
368
369
Joonsoo Kim031bc572014-12-12 16:55:52 -0800370void __kernel_map_pages(struct page *page, int numpages, int enable)
Benjamin Herrenschmidt88df6e92007-04-12 15:30:22 +1000371{
372 if (PageHighMem(page))
373 return;
374
375 change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
376}
377#endif /* CONFIG_DEBUG_PAGEALLOC */
Kumar Gala2c419bd2008-04-23 23:05:20 +1000378
379static int fixmaps;
Kumar Gala2c419bd2008-04-23 23:05:20 +1000380
381void __set_fixmap (enum fixed_addresses idx, phys_addr_t phys, pgprot_t flags)
382{
383 unsigned long address = __fix_to_virt(idx);
384
385 if (idx >= __end_of_fixed_addresses) {
386 BUG();
387 return;
388 }
389
David Gibson46a74172008-05-19 16:16:00 +1000390 map_page(address, phys, pgprot_val(flags));
Kumar Gala2c419bd2008-04-23 23:05:20 +1000391 fixmaps++;
392}