powerpc/mm: Rename tlb_32.c and tlb_64.c to tlb_hash32.c and tlb_hash64.c
[deliverable/linux.git] / arch / powerpc / mm / pgtable_32.c
CommitLineData
14cf11af
PM
1/*
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
14cf11af
PM
11 *
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
14cf11af
PM
22#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>
2c419bde 32#include <asm/fixmap.h>
14cf11af
PM
33#include <asm/io.h>
34
35#include "mmu_decl.h"
36
37unsigned long ioremap_base;
38unsigned long ioremap_bot;
920573bd 39EXPORT_SYMBOL(ioremap_bot); /* aka VMALLOC_END */
14cf11af
PM
40
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
14cf11af 51#ifdef HAVE_BATS
7c5c4325
BB
52extern 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,
14cf11af
PM
55 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
4ee7084e 72/* Some processors use an 8kB pgdir because they have 8-byte Linux PTEs. */
14cf11af
PM
73#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
5e541973 86void pgd_free(struct mm_struct *mm, pgd_t *pgd)
14cf11af
PM
87{
88 free_pages((unsigned long)pgd, PGDIR_ORDER);
89}
90
f1aed924 91__init_refok pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
14cf11af
PM
92{
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
2f569afd 107pgtable_t pte_alloc_one(struct mm_struct *mm, unsigned long address)
14cf11af
PM
108{
109 struct page *ptepage;
110
111#ifdef CONFIG_HIGHPTE
2f569afd 112 gfp_t flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_REPEAT | __GFP_ZERO;
14cf11af 113#else
2f569afd 114 gfp_t flags = GFP_KERNEL | __GFP_REPEAT | __GFP_ZERO;
14cf11af
PM
115#endif
116
117 ptepage = alloc_pages(flags, 0);
2f569afd
MS
118 if (!ptepage)
119 return NULL;
120 pgtable_page_ctor(ptepage);
14cf11af
PM
121 return ptepage;
122}
123
14cf11af
PM
124void __iomem *
125ioremap(phys_addr_t addr, unsigned long size)
126{
a1f242ff 127 return __ioremap(addr, size, _PAGE_NO_CACHE | _PAGE_GUARDED);
14cf11af 128}
920573bd 129EXPORT_SYMBOL(ioremap);
14cf11af 130
68a64357
BH
131void __iomem *
132ioremap_flags(phys_addr_t addr, unsigned long size, unsigned long flags)
133{
a1f242ff
BH
134 /* 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
68a64357
BH
141 return __ioremap(addr, size, flags);
142}
143EXPORT_SYMBOL(ioremap_flags);
144
14cf11af
PM
145void __iomem *
146__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
a1f242ff
BH
152 /* 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
14cf11af
PM
160 /*
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
176 /*
177 * Don't allow anybody to remap normal RAM that we're using.
178 * mem_init() sets high_memory so only do the check after that.
179 */
7c8c6b97 180 if (mem_init_done && (p < virt_to_phys(high_memory))) {
37f01d64
DG
181 printk("__ioremap(): phys addr 0x%llx is RAM lr %p\n",
182 (unsigned long long)p, __builtin_return_address(0));
14cf11af
PM
183 return NULL;
184 }
185
186 if (size == 0)
187 return NULL;
188
189 /*
190 * Is it already mapped? Perhaps overlapped by a previous
191 * BAT mapping. If the whole area is mapped then we're done,
192 * otherwise remap it since we want to keep the virt addrs for
193 * each request contiguous.
194 *
195 * We make the assumption here that if the bottom and top
196 * of the range we want are mapped then it's mapped to the
197 * same virt address (and this is contiguous).
198 * -- Cort
199 */
200 if ((v = p_mapped_by_bats(p)) /*&& p_mapped_by_bats(p+size-1)*/ )
201 goto out;
202
203 if ((v = p_mapped_by_tlbcam(p)))
204 goto out;
205
206 if (mem_init_done) {
207 struct vm_struct *area;
208 area = get_vm_area(size, VM_IOREMAP);
209 if (area == 0)
210 return NULL;
211 v = (unsigned long) area->addr;
212 } else {
213 v = (ioremap_bot -= size);
214 }
215
14cf11af
PM
216 /*
217 * Should check if it is a candidate for a BAT mapping
218 */
219
220 err = 0;
221 for (i = 0; i < size && err == 0; i += PAGE_SIZE)
222 err = map_page(v+i, p+i, flags);
223 if (err) {
224 if (mem_init_done)
225 vunmap((void *)v);
226 return NULL;
227 }
228
229out:
230 return (void __iomem *) (v + ((unsigned long)addr & ~PAGE_MASK));
231}
920573bd 232EXPORT_SYMBOL(__ioremap);
14cf11af
PM
233
234void iounmap(volatile void __iomem *addr)
235{
236 /*
237 * If mapped by BATs then there is nothing to do.
238 * Calling vfree() generates a benign warning.
239 */
240 if (v_mapped_by_bats((unsigned long)addr)) return;
241
242 if (addr > high_memory && (unsigned long) addr < ioremap_bot)
243 vunmap((void *) (PAGE_MASK & (unsigned long)addr));
244}
920573bd 245EXPORT_SYMBOL(iounmap);
14cf11af 246
68a64357 247int map_page(unsigned long va, phys_addr_t pa, int flags)
14cf11af
PM
248{
249 pmd_t *pd;
250 pte_t *pg;
251 int err = -ENOMEM;
252
14cf11af 253 /* Use upper 10 bits of VA to index the first level map */
d1953c88 254 pd = pmd_offset(pud_offset(pgd_offset_k(va), va), va);
14cf11af 255 /* Use middle 10 bits of VA to index the second-level map */
e2f2e58e 256 pg = pte_alloc_kernel(pd, va);
14cf11af
PM
257 if (pg != 0) {
258 err = 0;
3be4e699
BH
259 /* The PTE should never be already set nor present in the
260 * hash table
261 */
262 BUG_ON(pte_val(*pg) & (_PAGE_PRESENT | _PAGE_HASHPTE));
263 set_pte_at(&init_mm, va, pg, pfn_pte(pa >> PAGE_SHIFT,
264 __pgprot(flags)));
14cf11af 265 }
14cf11af
PM
266 return err;
267}
268
269/*
4ee7084e 270 * Map in a big chunk of physical memory starting at KERNELBASE.
14cf11af
PM
271 */
272void __init mapin_ram(void)
273{
99c62dd7
KG
274 unsigned long v, s, f;
275 phys_addr_t p;
ee4f2ea4 276 int ktext;
14cf11af
PM
277
278 s = mmu_mapin_ram();
279 v = KERNELBASE + s;
99c62dd7 280 p = memstart_addr + s;
14cf11af 281 for (; s < total_lowmem; s += PAGE_SIZE) {
ee4f2ea4
BH
282 ktext = ((char *) v >= _stext && (char *) v < etext);
283 f = ktext ?_PAGE_RAM_TEXT : _PAGE_RAM;
14cf11af 284 map_page(v, p, f);
ee4f2ea4
BH
285#ifdef CONFIG_PPC_STD_MMU_32
286 if (ktext)
287 hash_preload(&init_mm, v, 0, 0x300);
288#endif
14cf11af
PM
289 v += PAGE_SIZE;
290 p += PAGE_SIZE;
291 }
292}
293
14cf11af
PM
294/* Scan the real Linux page tables and return a PTE pointer for
295 * a virtual address in a context.
296 * Returns true (1) if PTE was found, zero otherwise. The pointer to
297 * the PTE pointer is unmodified if PTE is not found.
298 */
299int
bab70a4a 300get_pteptr(struct mm_struct *mm, unsigned long addr, pte_t **ptep, pmd_t **pmdp)
14cf11af
PM
301{
302 pgd_t *pgd;
d1953c88 303 pud_t *pud;
14cf11af
PM
304 pmd_t *pmd;
305 pte_t *pte;
306 int retval = 0;
307
308 pgd = pgd_offset(mm, addr & PAGE_MASK);
309 if (pgd) {
d1953c88
DG
310 pud = pud_offset(pgd, addr & PAGE_MASK);
311 if (pud && pud_present(*pud)) {
312 pmd = pmd_offset(pud, addr & PAGE_MASK);
313 if (pmd_present(*pmd)) {
314 pte = pte_offset_map(pmd, addr & PAGE_MASK);
315 if (pte) {
316 retval = 1;
317 *ptep = pte;
318 if (pmdp)
319 *pmdp = pmd;
320 /* XXX caller needs to do pte_unmap, yuck */
321 }
322 }
323 }
14cf11af
PM
324 }
325 return(retval);
326}
327
88df6e90
BH
328#ifdef CONFIG_DEBUG_PAGEALLOC
329
330static int __change_page_attr(struct page *page, pgprot_t prot)
331{
332 pte_t *kpte;
333 pmd_t *kpmd;
334 unsigned long address;
335
336 BUG_ON(PageHighMem(page));
337 address = (unsigned long)page_address(page);
338
339 if (v_mapped_by_bats(address) || v_mapped_by_tlbcam(address))
340 return 0;
341 if (!get_pteptr(&init_mm, address, &kpte, &kpmd))
342 return -EINVAL;
343 set_pte_at(&init_mm, address, kpte, mk_pte(page, prot));
344 wmb();
345 flush_HPTE(0, address, pmd_val(*kpmd));
346 pte_unmap(kpte);
347
348 return 0;
349}
350
351/*
352 * Change the page attributes of an page in the linear mapping.
353 *
354 * THIS CONFLICTS WITH BAT MAPPINGS, DEBUG USE ONLY
355 */
356static int change_page_attr(struct page *page, int numpages, pgprot_t prot)
357{
358 int i, err = 0;
359 unsigned long flags;
360
361 local_irq_save(flags);
362 for (i = 0; i < numpages; i++, page++) {
363 err = __change_page_attr(page, prot);
364 if (err)
365 break;
366 }
367 local_irq_restore(flags);
368 return err;
369}
370
371
372void kernel_map_pages(struct page *page, int numpages, int enable)
373{
374 if (PageHighMem(page))
375 return;
376
377 change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
378}
379#endif /* CONFIG_DEBUG_PAGEALLOC */
2c419bde
KG
380
381static int fixmaps;
382unsigned long FIXADDR_TOP = 0xfffff000;
383EXPORT_SYMBOL(FIXADDR_TOP);
384
385void __set_fixmap (enum fixed_addresses idx, phys_addr_t phys, pgprot_t flags)
386{
387 unsigned long address = __fix_to_virt(idx);
388
389 if (idx >= __end_of_fixed_addresses) {
390 BUG();
391 return;
392 }
393
46a74179 394 map_page(address, phys, pgprot_val(flags));
2c419bde
KG
395 fixmaps++;
396}
397
398void __this_fixmap_does_not_exist(void)
399{
400 WARN_ON(1);
401}
This page took 0.320695 seconds and 5 git commands to generate.