blob: ac3390f81900c64192c70a14e1a10258201c7090 [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>
32#include <asm/io.h>
33
34#include "mmu_decl.h"
35
36unsigned long ioremap_base;
37unsigned long ioremap_bot;
Olaf Hering920573b2006-03-14 21:21:11 +010038EXPORT_SYMBOL(ioremap_bot); /* aka VMALLOC_END */
Paul Mackerras14cf11a2005-09-26 16:04:21 +100039
40#if defined(CONFIG_6xx) || defined(CONFIG_POWER3)
41#define HAVE_BATS 1
42#endif
43
44#if defined(CONFIG_FSL_BOOKE)
45#define HAVE_TLBCAM 1
46#endif
47
48extern char etext[], _stext[];
49
50#ifdef CONFIG_SMP
51extern void hash_page_sync(void);
52#endif
53
54#ifdef HAVE_BATS
55extern unsigned long v_mapped_by_bats(unsigned long va);
56extern unsigned long p_mapped_by_bats(unsigned long pa);
57void setbat(int index, unsigned long virt, unsigned long phys,
58 unsigned int size, int flags);
59
60#else /* !HAVE_BATS */
61#define v_mapped_by_bats(x) (0UL)
62#define p_mapped_by_bats(x) (0UL)
63#endif /* HAVE_BATS */
64
65#ifdef HAVE_TLBCAM
66extern unsigned int tlbcam_index;
67extern unsigned long v_mapped_by_tlbcam(unsigned long va);
68extern unsigned long p_mapped_by_tlbcam(unsigned long pa);
69#else /* !HAVE_TLBCAM */
70#define v_mapped_by_tlbcam(x) (0UL)
71#define p_mapped_by_tlbcam(x) (0UL)
72#endif /* HAVE_TLBCAM */
73
74#ifdef CONFIG_PTE_64BIT
75/* 44x uses an 8kB pgdir because it has 8-byte Linux PTEs. */
76#define PGDIR_ORDER 1
77#else
78#define PGDIR_ORDER 0
79#endif
80
81pgd_t *pgd_alloc(struct mm_struct *mm)
82{
83 pgd_t *ret;
84
85 ret = (pgd_t *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, PGDIR_ORDER);
86 return ret;
87}
88
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -080089void pgd_free(struct mm_struct *mm, pgd_t *pgd)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100090{
91 free_pages((unsigned long)pgd, PGDIR_ORDER);
92}
93
Kumar Galaf1aed922007-05-23 07:49:37 -050094__init_refok pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100095{
96 pte_t *pte;
97 extern int mem_init_done;
98 extern void *early_get_page(void);
99
100 if (mem_init_done) {
101 pte = (pte_t *)__get_free_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO);
102 } else {
103 pte = (pte_t *)early_get_page();
104 if (pte)
105 clear_page(pte);
106 }
107 return pte;
108}
109
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800110pgtable_t pte_alloc_one(struct mm_struct *mm, unsigned long address)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000111{
112 struct page *ptepage;
113
114#ifdef CONFIG_HIGHPTE
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800115 gfp_t flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_REPEAT | __GFP_ZERO;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000116#else
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800117 gfp_t flags = GFP_KERNEL | __GFP_REPEAT | __GFP_ZERO;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000118#endif
119
120 ptepage = alloc_pages(flags, 0);
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800121 if (!ptepage)
122 return NULL;
123 pgtable_page_ctor(ptepage);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000124 return ptepage;
125}
126
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -0800127void pte_free_kernel(struct mm_struct *mm, pte_t *pte)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000128{
129#ifdef CONFIG_SMP
130 hash_page_sync();
131#endif
132 free_page((unsigned long)pte);
133}
134
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800135void pte_free(struct mm_struct *mm, pgtable_t ptepage)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000136{
137#ifdef CONFIG_SMP
138 hash_page_sync();
139#endif
Martin Schwidefsky2f569af2008-02-08 04:22:04 -0800140 pgtable_page_dtor(ptepage);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000141 __free_page(ptepage);
142}
143
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000144void __iomem *
145ioremap(phys_addr_t addr, unsigned long size)
146{
147 return __ioremap(addr, size, _PAGE_NO_CACHE);
148}
Olaf Hering920573b2006-03-14 21:21:11 +0100149EXPORT_SYMBOL(ioremap);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000150
151void __iomem *
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100152ioremap_flags(phys_addr_t addr, unsigned long size, unsigned long flags)
153{
154 return __ioremap(addr, size, flags);
155}
156EXPORT_SYMBOL(ioremap_flags);
157
158void __iomem *
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000159__ioremap(phys_addr_t addr, unsigned long size, unsigned long flags)
160{
161 unsigned long v, i;
162 phys_addr_t p;
163 int err;
164
165 /*
166 * Choose an address to map it to.
167 * Once the vmalloc system is running, we use it.
168 * Before then, we use space going down from ioremap_base
169 * (ioremap_bot records where we're up to).
170 */
171 p = addr & PAGE_MASK;
172 size = PAGE_ALIGN(addr + size) - p;
173
174 /*
175 * If the address lies within the first 16 MB, assume it's in ISA
176 * memory space
177 */
178 if (p < 16*1024*1024)
179 p += _ISA_MEM_BASE;
180
181 /*
182 * Don't allow anybody to remap normal RAM that we're using.
183 * mem_init() sets high_memory so only do the check after that.
184 */
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000185 if (mem_init_done && (p < virt_to_phys(high_memory))) {
David Gibson37f01d62007-04-24 15:05:18 +1000186 printk("__ioremap(): phys addr 0x%llx is RAM lr %p\n",
187 (unsigned long long)p, __builtin_return_address(0));
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000188 return NULL;
189 }
190
191 if (size == 0)
192 return NULL;
193
194 /*
195 * Is it already mapped? Perhaps overlapped by a previous
196 * BAT mapping. If the whole area is mapped then we're done,
197 * otherwise remap it since we want to keep the virt addrs for
198 * each request contiguous.
199 *
200 * We make the assumption here that if the bottom and top
201 * of the range we want are mapped then it's mapped to the
202 * same virt address (and this is contiguous).
203 * -- Cort
204 */
205 if ((v = p_mapped_by_bats(p)) /*&& p_mapped_by_bats(p+size-1)*/ )
206 goto out;
207
208 if ((v = p_mapped_by_tlbcam(p)))
209 goto out;
210
211 if (mem_init_done) {
212 struct vm_struct *area;
213 area = get_vm_area(size, VM_IOREMAP);
214 if (area == 0)
215 return NULL;
216 v = (unsigned long) area->addr;
217 } else {
218 v = (ioremap_bot -= size);
219 }
220
221 if ((flags & _PAGE_PRESENT) == 0)
222 flags |= _PAGE_KERNEL;
223 if (flags & _PAGE_NO_CACHE)
224 flags |= _PAGE_GUARDED;
225
226 /*
227 * Should check if it is a candidate for a BAT mapping
228 */
229
230 err = 0;
231 for (i = 0; i < size && err == 0; i += PAGE_SIZE)
232 err = map_page(v+i, p+i, flags);
233 if (err) {
234 if (mem_init_done)
235 vunmap((void *)v);
236 return NULL;
237 }
238
239out:
240 return (void __iomem *) (v + ((unsigned long)addr & ~PAGE_MASK));
241}
Olaf Hering920573b2006-03-14 21:21:11 +0100242EXPORT_SYMBOL(__ioremap);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000243
244void iounmap(volatile void __iomem *addr)
245{
246 /*
247 * If mapped by BATs then there is nothing to do.
248 * Calling vfree() generates a benign warning.
249 */
250 if (v_mapped_by_bats((unsigned long)addr)) return;
251
252 if (addr > high_memory && (unsigned long) addr < ioremap_bot)
253 vunmap((void *) (PAGE_MASK & (unsigned long)addr));
254}
Olaf Hering920573b2006-03-14 21:21:11 +0100255EXPORT_SYMBOL(iounmap);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000256
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100257int map_page(unsigned long va, phys_addr_t pa, int flags)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000258{
259 pmd_t *pd;
260 pte_t *pg;
261 int err = -ENOMEM;
262
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000263 /* Use upper 10 bits of VA to index the first level map */
David Gibsond1953c82007-05-08 12:46:49 +1000264 pd = pmd_offset(pud_offset(pgd_offset_k(va), va), va);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000265 /* Use middle 10 bits of VA to index the second-level map */
Paul Mackerrase2f2e582005-10-31 14:40:03 +1100266 pg = pte_alloc_kernel(pd, va);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000267 if (pg != 0) {
268 err = 0;
Benjamin Herrenschmidt3be4e692007-04-12 15:30:21 +1000269 /* The PTE should never be already set nor present in the
270 * hash table
271 */
272 BUG_ON(pte_val(*pg) & (_PAGE_PRESENT | _PAGE_HASHPTE));
273 set_pte_at(&init_mm, va, pg, pfn_pte(pa >> PAGE_SHIFT,
274 __pgprot(flags)));
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000275 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000276 return err;
277}
278
279/*
280 * Map in all of physical memory starting at KERNELBASE.
281 */
282void __init mapin_ram(void)
283{
284 unsigned long v, p, s, f;
Benjamin Herrenschmidtee4f2ea2007-04-12 15:30:22 +1000285 int ktext;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000286
287 s = mmu_mapin_ram();
288 v = KERNELBASE + s;
289 p = PPC_MEMSTART + s;
290 for (; s < total_lowmem; s += PAGE_SIZE) {
Benjamin Herrenschmidtee4f2ea2007-04-12 15:30:22 +1000291 ktext = ((char *) v >= _stext && (char *) v < etext);
292 f = ktext ?_PAGE_RAM_TEXT : _PAGE_RAM;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000293 map_page(v, p, f);
Benjamin Herrenschmidtee4f2ea2007-04-12 15:30:22 +1000294#ifdef CONFIG_PPC_STD_MMU_32
295 if (ktext)
296 hash_preload(&init_mm, v, 0, 0x300);
297#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000298 v += PAGE_SIZE;
299 p += PAGE_SIZE;
300 }
301}
302
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000303/* Scan the real Linux page tables and return a PTE pointer for
304 * a virtual address in a context.
305 * Returns true (1) if PTE was found, zero otherwise. The pointer to
306 * the PTE pointer is unmodified if PTE is not found.
307 */
308int
Eugene Suroveginbab70a42006-03-28 10:13:12 -0800309get_pteptr(struct mm_struct *mm, unsigned long addr, pte_t **ptep, pmd_t **pmdp)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000310{
311 pgd_t *pgd;
David Gibsond1953c82007-05-08 12:46:49 +1000312 pud_t *pud;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000313 pmd_t *pmd;
314 pte_t *pte;
315 int retval = 0;
316
317 pgd = pgd_offset(mm, addr & PAGE_MASK);
318 if (pgd) {
David Gibsond1953c82007-05-08 12:46:49 +1000319 pud = pud_offset(pgd, addr & PAGE_MASK);
320 if (pud && pud_present(*pud)) {
321 pmd = pmd_offset(pud, addr & PAGE_MASK);
322 if (pmd_present(*pmd)) {
323 pte = pte_offset_map(pmd, addr & PAGE_MASK);
324 if (pte) {
325 retval = 1;
326 *ptep = pte;
327 if (pmdp)
328 *pmdp = pmd;
329 /* XXX caller needs to do pte_unmap, yuck */
330 }
331 }
332 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000333 }
334 return(retval);
335}
336
Benjamin Herrenschmidt88df6e92007-04-12 15:30:22 +1000337#ifdef CONFIG_DEBUG_PAGEALLOC
338
339static int __change_page_attr(struct page *page, pgprot_t prot)
340{
341 pte_t *kpte;
342 pmd_t *kpmd;
343 unsigned long address;
344
345 BUG_ON(PageHighMem(page));
346 address = (unsigned long)page_address(page);
347
348 if (v_mapped_by_bats(address) || v_mapped_by_tlbcam(address))
349 return 0;
350 if (!get_pteptr(&init_mm, address, &kpte, &kpmd))
351 return -EINVAL;
352 set_pte_at(&init_mm, address, kpte, mk_pte(page, prot));
353 wmb();
354 flush_HPTE(0, address, pmd_val(*kpmd));
355 pte_unmap(kpte);
356
357 return 0;
358}
359
360/*
361 * Change the page attributes of an page in the linear mapping.
362 *
363 * THIS CONFLICTS WITH BAT MAPPINGS, DEBUG USE ONLY
364 */
365static int change_page_attr(struct page *page, int numpages, pgprot_t prot)
366{
367 int i, err = 0;
368 unsigned long flags;
369
370 local_irq_save(flags);
371 for (i = 0; i < numpages; i++, page++) {
372 err = __change_page_attr(page, prot);
373 if (err)
374 break;
375 }
376 local_irq_restore(flags);
377 return err;
378}
379
380
381void kernel_map_pages(struct page *page, int numpages, int enable)
382{
383 if (PageHighMem(page))
384 return;
385
386 change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
387}
388#endif /* CONFIG_DEBUG_PAGEALLOC */