blob: 8cba46fc9e3bfb647914c55cd7de8e4601b718fa [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>
29
30#include <asm/pgtable.h>
31#include <asm/pgalloc.h>
Kumar Gala2c419bd2008-04-23 23:05:20 +100032#include <asm/fixmap.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100033#include <asm/io.h>
34
35#include "mmu_decl.h"
36
37unsigned long ioremap_base;
38unsigned long ioremap_bot;
Olaf Hering920573b2006-03-14 21:21:11 +010039EXPORT_SYMBOL(ioremap_bot); /* aka VMALLOC_END */
Paul Mackerras14cf11a2005-09-26 16:04:21 +100040
41#if defined(CONFIG_6xx) || defined(CONFIG_POWER3)
42#define HAVE_BATS 1
43#endif
44
45#if defined(CONFIG_FSL_BOOKE)
46#define HAVE_TLBCAM 1
47#endif
48
49extern char etext[], _stext[];
50
Paul Mackerras14cf11a2005-09-26 16:04:21 +100051#ifdef HAVE_BATS
Becky Bruce7c5c4322008-06-14 09:41:42 +100052extern phys_addr_t v_mapped_by_bats(unsigned long va);
53extern unsigned long p_mapped_by_bats(phys_addr_t pa);
54void setbat(int index, unsigned long virt, phys_addr_t phys,
Paul Mackerras14cf11a2005-09-26 16:04:21 +100055 unsigned int size, int flags);
56
57#else /* !HAVE_BATS */
58#define v_mapped_by_bats(x) (0UL)
59#define p_mapped_by_bats(x) (0UL)
60#endif /* HAVE_BATS */
61
62#ifdef HAVE_TLBCAM
63extern unsigned int tlbcam_index;
64extern unsigned long v_mapped_by_tlbcam(unsigned long va);
65extern unsigned long p_mapped_by_tlbcam(unsigned long pa);
66#else /* !HAVE_TLBCAM */
67#define v_mapped_by_tlbcam(x) (0UL)
68#define p_mapped_by_tlbcam(x) (0UL)
69#endif /* HAVE_TLBCAM */
70
71#ifdef CONFIG_PTE_64BIT
Becky Bruce4ee70842008-09-24 11:01:24 -050072/* Some processors use an 8kB pgdir because they have 8-byte Linux PTEs. */
Paul Mackerras14cf11a2005-09-26 16:04:21 +100073#define PGDIR_ORDER 1
74#else
75#define PGDIR_ORDER 0
76#endif
77
78pgd_t *pgd_alloc(struct mm_struct *mm)
79{
80 pgd_t *ret;
81
82 ret = (pgd_t *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, PGDIR_ORDER);
83 return ret;
84}
85
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -080086void pgd_free(struct mm_struct *mm, pgd_t *pgd)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100087{
88 free_pages((unsigned long)pgd, PGDIR_ORDER);
89}
90
Kumar Galaf1aed922007-05-23 07:49:37 -050091__init_refok pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100092{
93 pte_t *pte;
94 extern int mem_init_done;
95 extern void *early_get_page(void);
96
97 if (mem_init_done) {
98 pte = (pte_t *)__get_free_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO);
99 } else {
100 pte = (pte_t *)early_get_page();
101 if (pte)
102 clear_page(pte);
103 }
104 return pte;
105}
106
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800107pgtable_t pte_alloc_one(struct mm_struct *mm, unsigned long address)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000108{
109 struct page *ptepage;
110
111#ifdef CONFIG_HIGHPTE
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800112 gfp_t flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_REPEAT | __GFP_ZERO;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000113#else
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800114 gfp_t flags = GFP_KERNEL | __GFP_REPEAT | __GFP_ZERO;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000115#endif
116
117 ptepage = alloc_pages(flags, 0);
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800118 if (!ptepage)
119 return NULL;
120 pgtable_page_ctor(ptepage);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000121 return ptepage;
122}
123
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000124void __iomem *
125ioremap(phys_addr_t addr, unsigned long size)
126{
Benjamin Herrenschmidta1f242f2008-07-23 21:27:08 -0700127 return __ioremap(addr, size, _PAGE_NO_CACHE | _PAGE_GUARDED);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000128}
Olaf Hering920573b2006-03-14 21:21:11 +0100129EXPORT_SYMBOL(ioremap);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000130
131void __iomem *
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100132ioremap_flags(phys_addr_t addr, unsigned long size, unsigned long flags)
133{
Benjamin Herrenschmidta1f242f2008-07-23 21:27:08 -0700134 /* writeable implies dirty for kernel addresses */
135 if (flags & _PAGE_RW)
136 flags |= _PAGE_DIRTY | _PAGE_HWWRITE;
137
138 /* we don't want to let _PAGE_USER and _PAGE_EXEC leak out */
139 flags &= ~(_PAGE_USER | _PAGE_EXEC | _PAGE_HWEXEC);
140
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100141 return __ioremap(addr, size, flags);
142}
143EXPORT_SYMBOL(ioremap_flags);
144
145void __iomem *
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000146__ioremap(phys_addr_t addr, unsigned long size, unsigned long flags)
147{
148 unsigned long v, i;
149 phys_addr_t p;
150 int err;
151
Benjamin Herrenschmidta1f242f2008-07-23 21:27:08 -0700152 /* Make sure we have the base flags */
153 if ((flags & _PAGE_PRESENT) == 0)
154 flags |= _PAGE_KERNEL;
155
156 /* Non-cacheable page cannot be coherent */
157 if (flags & _PAGE_NO_CACHE)
158 flags &= ~_PAGE_COHERENT;
159
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000160 /*
161 * Choose an address to map it to.
162 * Once the vmalloc system is running, we use it.
163 * Before then, we use space going down from ioremap_base
164 * (ioremap_bot records where we're up to).
165 */
166 p = addr & PAGE_MASK;
167 size = PAGE_ALIGN(addr + size) - p;
168
169 /*
170 * If the address lies within the first 16 MB, assume it's in ISA
171 * memory space
172 */
173 if (p < 16*1024*1024)
174 p += _ISA_MEM_BASE;
175
Anton Vorontsov01695a92008-12-17 10:09:10 +0000176#ifndef CONFIG_CRASH_DUMP
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000177 /*
178 * Don't allow anybody to remap normal RAM that we're using.
179 * mem_init() sets high_memory so only do the check after that.
180 */
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000181 if (mem_init_done && (p < virt_to_phys(high_memory))) {
David Gibson37f01d62007-04-24 15:05:18 +1000182 printk("__ioremap(): phys addr 0x%llx is RAM lr %p\n",
183 (unsigned long long)p, __builtin_return_address(0));
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000184 return NULL;
185 }
Anton Vorontsov01695a92008-12-17 10:09:10 +0000186#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000187
188 if (size == 0)
189 return NULL;
190
191 /*
192 * Is it already mapped? Perhaps overlapped by a previous
193 * BAT mapping. If the whole area is mapped then we're done,
194 * otherwise remap it since we want to keep the virt addrs for
195 * each request contiguous.
196 *
197 * We make the assumption here that if the bottom and top
198 * of the range we want are mapped then it's mapped to the
199 * same virt address (and this is contiguous).
200 * -- Cort
201 */
202 if ((v = p_mapped_by_bats(p)) /*&& p_mapped_by_bats(p+size-1)*/ )
203 goto out;
204
205 if ((v = p_mapped_by_tlbcam(p)))
206 goto out;
207
208 if (mem_init_done) {
209 struct vm_struct *area;
210 area = get_vm_area(size, VM_IOREMAP);
211 if (area == 0)
212 return NULL;
213 v = (unsigned long) area->addr;
214 } else {
215 v = (ioremap_bot -= size);
216 }
217
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000218 /*
219 * Should check if it is a candidate for a BAT mapping
220 */
221
222 err = 0;
223 for (i = 0; i < size && err == 0; i += PAGE_SIZE)
224 err = map_page(v+i, p+i, flags);
225 if (err) {
226 if (mem_init_done)
227 vunmap((void *)v);
228 return NULL;
229 }
230
231out:
232 return (void __iomem *) (v + ((unsigned long)addr & ~PAGE_MASK));
233}
Olaf Hering920573b2006-03-14 21:21:11 +0100234EXPORT_SYMBOL(__ioremap);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000235
236void iounmap(volatile void __iomem *addr)
237{
238 /*
239 * If mapped by BATs then there is nothing to do.
240 * Calling vfree() generates a benign warning.
241 */
242 if (v_mapped_by_bats((unsigned long)addr)) return;
243
244 if (addr > high_memory && (unsigned long) addr < ioremap_bot)
245 vunmap((void *) (PAGE_MASK & (unsigned long)addr));
246}
Olaf Hering920573b2006-03-14 21:21:11 +0100247EXPORT_SYMBOL(iounmap);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000248
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100249int map_page(unsigned long va, phys_addr_t pa, int flags)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000250{
251 pmd_t *pd;
252 pte_t *pg;
253 int err = -ENOMEM;
254
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000255 /* Use upper 10 bits of VA to index the first level map */
David Gibsond1953c82007-05-08 12:46:49 +1000256 pd = pmd_offset(pud_offset(pgd_offset_k(va), va), va);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000257 /* Use middle 10 bits of VA to index the second-level map */
Paul Mackerrase2f2e582005-10-31 14:40:03 +1100258 pg = pte_alloc_kernel(pd, va);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000259 if (pg != 0) {
260 err = 0;
Benjamin Herrenschmidt3be4e692007-04-12 15:30:21 +1000261 /* The PTE should never be already set nor present in the
262 * hash table
263 */
264 BUG_ON(pte_val(*pg) & (_PAGE_PRESENT | _PAGE_HASHPTE));
265 set_pte_at(&init_mm, va, pg, pfn_pte(pa >> PAGE_SHIFT,
266 __pgprot(flags)));
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000267 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000268 return err;
269}
270
271/*
Dale Farnsworthccdcef72008-12-17 10:09:13 +0000272 * Map in a big chunk of physical memory starting at PAGE_OFFSET.
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000273 */
274void __init mapin_ram(void)
275{
Kumar Gala99c62dd72008-04-16 05:52:21 +1000276 unsigned long v, s, f;
277 phys_addr_t p;
Benjamin Herrenschmidtee4f2ea2007-04-12 15:30:22 +1000278 int ktext;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000279
280 s = mmu_mapin_ram();
Dale Farnsworthccdcef72008-12-17 10:09:13 +0000281 v = PAGE_OFFSET + s;
Kumar Gala99c62dd72008-04-16 05:52:21 +1000282 p = memstart_addr + s;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000283 for (; s < total_lowmem; s += PAGE_SIZE) {
Benjamin Herrenschmidtee4f2ea2007-04-12 15:30:22 +1000284 ktext = ((char *) v >= _stext && (char *) v < etext);
285 f = ktext ?_PAGE_RAM_TEXT : _PAGE_RAM;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000286 map_page(v, p, f);
Benjamin Herrenschmidtee4f2ea2007-04-12 15:30:22 +1000287#ifdef CONFIG_PPC_STD_MMU_32
288 if (ktext)
289 hash_preload(&init_mm, v, 0, 0x300);
290#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000291 v += PAGE_SIZE;
292 p += PAGE_SIZE;
293 }
294}
295
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000296/* Scan the real Linux page tables and return a PTE pointer for
297 * a virtual address in a context.
298 * Returns true (1) if PTE was found, zero otherwise. The pointer to
299 * the PTE pointer is unmodified if PTE is not found.
300 */
301int
Eugene Suroveginbab70a42006-03-28 10:13:12 -0800302get_pteptr(struct mm_struct *mm, unsigned long addr, pte_t **ptep, pmd_t **pmdp)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000303{
304 pgd_t *pgd;
David Gibsond1953c82007-05-08 12:46:49 +1000305 pud_t *pud;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000306 pmd_t *pmd;
307 pte_t *pte;
308 int retval = 0;
309
310 pgd = pgd_offset(mm, addr & PAGE_MASK);
311 if (pgd) {
David Gibsond1953c82007-05-08 12:46:49 +1000312 pud = pud_offset(pgd, addr & PAGE_MASK);
313 if (pud && pud_present(*pud)) {
314 pmd = pmd_offset(pud, addr & PAGE_MASK);
315 if (pmd_present(*pmd)) {
316 pte = pte_offset_map(pmd, addr & PAGE_MASK);
317 if (pte) {
318 retval = 1;
319 *ptep = pte;
320 if (pmdp)
321 *pmdp = pmd;
322 /* XXX caller needs to do pte_unmap, yuck */
323 }
324 }
325 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000326 }
327 return(retval);
328}
329
Benjamin Herrenschmidt88df6e92007-04-12 15:30:22 +1000330#ifdef CONFIG_DEBUG_PAGEALLOC
331
332static int __change_page_attr(struct page *page, pgprot_t prot)
333{
334 pte_t *kpte;
335 pmd_t *kpmd;
336 unsigned long address;
337
338 BUG_ON(PageHighMem(page));
339 address = (unsigned long)page_address(page);
340
341 if (v_mapped_by_bats(address) || v_mapped_by_tlbcam(address))
342 return 0;
343 if (!get_pteptr(&init_mm, address, &kpte, &kpmd))
344 return -EINVAL;
345 set_pte_at(&init_mm, address, kpte, mk_pte(page, prot));
346 wmb();
Benjamin Herrenschmidtf63837f2008-12-14 19:44:51 +0000347#ifdef CONFIG_PPC_STD_MMU
348 flush_hash_pages(0, address, pmd_val(*kpmd), 1);
349#else
350 flush_tlb_page(NULL, address);
351#endif
Benjamin Herrenschmidt88df6e92007-04-12 15:30:22 +1000352 pte_unmap(kpte);
353
354 return 0;
355}
356
357/*
358 * Change the page attributes of an page in the linear mapping.
359 *
360 * THIS CONFLICTS WITH BAT MAPPINGS, DEBUG USE ONLY
361 */
362static int change_page_attr(struct page *page, int numpages, pgprot_t prot)
363{
364 int i, err = 0;
365 unsigned long flags;
366
367 local_irq_save(flags);
368 for (i = 0; i < numpages; i++, page++) {
369 err = __change_page_attr(page, prot);
370 if (err)
371 break;
372 }
373 local_irq_restore(flags);
374 return err;
375}
376
377
378void kernel_map_pages(struct page *page, int numpages, int enable)
379{
380 if (PageHighMem(page))
381 return;
382
383 change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
384}
385#endif /* CONFIG_DEBUG_PAGEALLOC */
Kumar Gala2c419bd2008-04-23 23:05:20 +1000386
387static int fixmaps;
388unsigned long FIXADDR_TOP = 0xfffff000;
389EXPORT_SYMBOL(FIXADDR_TOP);
390
391void __set_fixmap (enum fixed_addresses idx, phys_addr_t phys, pgprot_t flags)
392{
393 unsigned long address = __fix_to_virt(idx);
394
395 if (idx >= __end_of_fixed_addresses) {
396 BUG();
397 return;
398 }
399
David Gibson46a74172008-05-19 16:16:00 +1000400 map_page(address, phys, pgprot_val(flags));
Kumar Gala2c419bd2008-04-23 23:05:20 +1000401 fixmaps++;
402}
403
404void __this_fixmap_does_not_exist(void)
405{
406 WARN_ON(1);
407}