blob: 7355211b93b39d00ccae72a6555a2e1877eb86bd [file] [log] [blame]
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +00001/*
2 * This file contains the routines for TLB flushing.
3 * On machines where the MMU does not use a hash table to store virtual to
4 * physical translations (ie, SW loaded TLBs or Book3E compilant processors,
5 * this does -not- include 603 however which shares the implementation with
6 * hash based processors)
7 *
8 * -- BenH
9 *
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +000010 * Copyright 2008,2009 Ben Herrenschmidt <benh@kernel.crashing.org>
11 * IBM Corp.
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +000012 *
13 * Derived from arch/ppc/mm/init.c:
14 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
15 *
16 * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
17 * and Cort Dougan (PReP) (cort@cs.nmt.edu)
18 * Copyright (C) 1996 Paul Mackerras
19 *
20 * Derived from "arch/i386/mm/init.c"
21 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
22 *
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License
25 * as published by the Free Software Foundation; either version
26 * 2 of the License, or (at your option) any later version.
27 *
28 */
29
30#include <linux/kernel.h>
31#include <linux/mm.h>
32#include <linux/init.h>
33#include <linux/highmem.h>
34#include <linux/pagemap.h>
35#include <linux/preempt.h>
36#include <linux/spinlock.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100037#include <linux/memblock.h>
Dave Kleikamp91b191c2011-07-04 18:38:03 +000038#include <linux/of_fdt.h>
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +000039
40#include <asm/tlbflush.h>
41#include <asm/tlb.h>
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +000042#include <asm/code-patching.h>
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +000043
44#include "mmu_decl.h"
45
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +000046#ifdef CONFIG_PPC_BOOK3E
47struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT] = {
48 [MMU_PAGE_4K] = {
49 .shift = 12,
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +100050 .ind = 20,
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +000051 .enc = BOOK3E_PAGESZ_4K,
52 },
53 [MMU_PAGE_16K] = {
54 .shift = 14,
55 .enc = BOOK3E_PAGESZ_16K,
56 },
57 [MMU_PAGE_64K] = {
58 .shift = 16,
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +100059 .ind = 28,
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +000060 .enc = BOOK3E_PAGESZ_64K,
61 },
62 [MMU_PAGE_1M] = {
63 .shift = 20,
64 .enc = BOOK3E_PAGESZ_1M,
65 },
66 [MMU_PAGE_16M] = {
67 .shift = 24,
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +100068 .ind = 36,
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +000069 .enc = BOOK3E_PAGESZ_16M,
70 },
71 [MMU_PAGE_256M] = {
72 .shift = 28,
73 .enc = BOOK3E_PAGESZ_256M,
74 },
75 [MMU_PAGE_1G] = {
76 .shift = 30,
77 .enc = BOOK3E_PAGESZ_1GB,
78 },
79};
80static inline int mmu_get_tsize(int psize)
81{
82 return mmu_psize_defs[psize].enc;
83}
84#else
85static inline int mmu_get_tsize(int psize)
86{
87 /* This isn't used on !Book3E for now */
88 return 0;
89}
90#endif
91
92/* The variables below are currently only used on 64-bit Book3E
93 * though this will probably be made common with other nohash
94 * implementations at some point
95 */
96#ifdef CONFIG_PPC64
97
98int mmu_linear_psize; /* Page size used for the linear mapping */
99int mmu_pte_psize; /* Page size used for PTE pages */
Benjamin Herrenschmidt32a74942009-07-23 23:15:58 +0000100int mmu_vmemmap_psize; /* Page size used for the virtual mem map */
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000101int book3e_htw_enabled; /* Is HW tablewalk enabled ? */
102unsigned long linear_map_top; /* Top of linear mapping */
103
104#endif /* CONFIG_PPC64 */
105
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000106/*
107 * Base TLB flushing operations:
108 *
109 * - flush_tlb_mm(mm) flushes the specified mm context TLB's
110 * - flush_tlb_page(vma, vmaddr) flushes one page
111 * - flush_tlb_range(vma, start, end) flushes a range of pages
112 * - flush_tlb_kernel_range(start, end) flushes kernel pages
113 *
114 * - local_* variants of page and mm only apply to the current
115 * processor
116 */
117
118/*
119 * These are the base non-SMP variants of page and mm flushing
120 */
121void local_flush_tlb_mm(struct mm_struct *mm)
122{
123 unsigned int pid;
124
125 preempt_disable();
126 pid = mm->context.id;
127 if (pid != MMU_NO_CONTEXT)
128 _tlbil_pid(pid);
129 preempt_enable();
130}
131EXPORT_SYMBOL(local_flush_tlb_mm);
132
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000133void __local_flush_tlb_page(struct mm_struct *mm, unsigned long vmaddr,
134 int tsize, int ind)
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000135{
136 unsigned int pid;
137
138 preempt_disable();
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000139 pid = mm ? mm->context.id : 0;
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000140 if (pid != MMU_NO_CONTEXT)
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000141 _tlbil_va(vmaddr, pid, tsize, ind);
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000142 preempt_enable();
143}
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000144
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000145void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
146{
147 __local_flush_tlb_page(vma ? vma->vm_mm : NULL, vmaddr,
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000148 mmu_get_tsize(mmu_virtual_psize), 0);
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000149}
150EXPORT_SYMBOL(local_flush_tlb_page);
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000151
152/*
153 * And here are the SMP non-local implementations
154 */
155#ifdef CONFIG_SMP
156
Thomas Gleixner3eb93c52010-02-18 02:22:44 +0000157static DEFINE_RAW_SPINLOCK(tlbivax_lock);
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000158
Benjamin Herrenschmidtfcce8102009-07-23 23:15:10 +0000159static int mm_is_core_local(struct mm_struct *mm)
160{
161 return cpumask_subset(mm_cpumask(mm),
162 topology_thread_cpumask(smp_processor_id()));
163}
164
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000165struct tlb_flush_param {
166 unsigned long addr;
167 unsigned int pid;
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000168 unsigned int tsize;
169 unsigned int ind;
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000170};
171
172static void do_flush_tlb_mm_ipi(void *param)
173{
174 struct tlb_flush_param *p = param;
175
176 _tlbil_pid(p ? p->pid : 0);
177}
178
179static void do_flush_tlb_page_ipi(void *param)
180{
181 struct tlb_flush_param *p = param;
182
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000183 _tlbil_va(p->addr, p->pid, p->tsize, p->ind);
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000184}
185
186
187/* Note on invalidations and PID:
188 *
189 * We snapshot the PID with preempt disabled. At this point, it can still
190 * change either because:
191 * - our context is being stolen (PID -> NO_CONTEXT) on another CPU
192 * - we are invaliating some target that isn't currently running here
193 * and is concurrently acquiring a new PID on another CPU
194 * - some other CPU is re-acquiring a lost PID for this mm
195 * etc...
196 *
197 * However, this shouldn't be a problem as we only guarantee
198 * invalidation of TLB entries present prior to this call, so we
199 * don't care about the PID changing, and invalidating a stale PID
200 * is generally harmless.
201 */
202
203void flush_tlb_mm(struct mm_struct *mm)
204{
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000205 unsigned int pid;
206
207 preempt_disable();
208 pid = mm->context.id;
209 if (unlikely(pid == MMU_NO_CONTEXT))
210 goto no_context;
Benjamin Herrenschmidtfcce8102009-07-23 23:15:10 +0000211 if (!mm_is_core_local(mm)) {
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000212 struct tlb_flush_param p = { .pid = pid };
Rusty Russell56aa4122009-03-15 18:16:43 +0000213 /* Ignores smp_processor_id() even if set. */
214 smp_call_function_many(mm_cpumask(mm),
215 do_flush_tlb_mm_ipi, &p, 1);
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000216 }
217 _tlbil_pid(pid);
218 no_context:
219 preempt_enable();
220}
221EXPORT_SYMBOL(flush_tlb_mm);
222
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000223void __flush_tlb_page(struct mm_struct *mm, unsigned long vmaddr,
224 int tsize, int ind)
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000225{
Rusty Russell56aa4122009-03-15 18:16:43 +0000226 struct cpumask *cpu_mask;
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000227 unsigned int pid;
228
229 preempt_disable();
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000230 pid = mm ? mm->context.id : 0;
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000231 if (unlikely(pid == MMU_NO_CONTEXT))
232 goto bail;
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000233 cpu_mask = mm_cpumask(mm);
Benjamin Herrenschmidtfcce8102009-07-23 23:15:10 +0000234 if (!mm_is_core_local(mm)) {
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000235 /* If broadcast tlbivax is supported, use it */
236 if (mmu_has_feature(MMU_FTR_USE_TLBIVAX_BCAST)) {
237 int lock = mmu_has_feature(MMU_FTR_LOCK_BCAST_INVAL);
238 if (lock)
Thomas Gleixner3eb93c52010-02-18 02:22:44 +0000239 raw_spin_lock(&tlbivax_lock);
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000240 _tlbivax_bcast(vmaddr, pid, tsize, ind);
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000241 if (lock)
Thomas Gleixner3eb93c52010-02-18 02:22:44 +0000242 raw_spin_unlock(&tlbivax_lock);
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000243 goto bail;
244 } else {
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000245 struct tlb_flush_param p = {
246 .pid = pid,
247 .addr = vmaddr,
248 .tsize = tsize,
249 .ind = ind,
250 };
Rusty Russell56aa4122009-03-15 18:16:43 +0000251 /* Ignores smp_processor_id() even if set in cpu_mask */
252 smp_call_function_many(cpu_mask,
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000253 do_flush_tlb_page_ipi, &p, 1);
254 }
255 }
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000256 _tlbil_va(vmaddr, pid, tsize, ind);
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000257 bail:
258 preempt_enable();
259}
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000260
261void flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
262{
263 __flush_tlb_page(vma ? vma->vm_mm : NULL, vmaddr,
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000264 mmu_get_tsize(mmu_virtual_psize), 0);
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000265}
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000266EXPORT_SYMBOL(flush_tlb_page);
267
268#endif /* CONFIG_SMP */
269
Dave Kleikamp91b191c2011-07-04 18:38:03 +0000270#ifdef CONFIG_PPC_47x
271void __init early_init_mmu_47x(void)
272{
273#ifdef CONFIG_SMP
274 unsigned long root = of_get_flat_dt_root();
275 if (of_get_flat_dt_prop(root, "cooperative-partition", NULL))
276 mmu_clear_feature(MMU_FTR_USE_TLBIVAX_BCAST);
277#endif /* CONFIG_SMP */
278}
279#endif /* CONFIG_PPC_47x */
280
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000281/*
282 * Flush kernel TLB entries in the given range
283 */
284void flush_tlb_kernel_range(unsigned long start, unsigned long end)
285{
286#ifdef CONFIG_SMP
287 preempt_disable();
288 smp_call_function(do_flush_tlb_mm_ipi, NULL, 1);
289 _tlbil_pid(0);
290 preempt_enable();
Dave Liud6a09e02008-12-30 23:42:55 +0000291#else
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000292 _tlbil_pid(0);
Dave Liud6a09e02008-12-30 23:42:55 +0000293#endif
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000294}
295EXPORT_SYMBOL(flush_tlb_kernel_range);
296
297/*
298 * Currently, for range flushing, we just do a full mm flush. This should
299 * be optimized based on a threshold on the size of the range, since
300 * some implementation can stack multiple tlbivax before a tlbsync but
301 * for now, we keep it that way
302 */
303void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
304 unsigned long end)
305
306{
307 flush_tlb_mm(vma->vm_mm);
308}
309EXPORT_SYMBOL(flush_tlb_range);
Benjamin Herrenschmidtc7cc58a2009-07-23 23:15:28 +0000310
311void tlb_flush(struct mmu_gather *tlb)
312{
313 flush_tlb_mm(tlb->mm);
Benjamin Herrenschmidtc7cc58a2009-07-23 23:15:28 +0000314}
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000315
316/*
317 * Below are functions specific to the 64-bit variant of Book3E though that
318 * may change in the future
319 */
320
321#ifdef CONFIG_PPC64
322
323/*
324 * Handling of virtual linear page tables or indirect TLB entries
325 * flushing when PTE pages are freed
326 */
327void tlb_flush_pgtable(struct mmu_gather *tlb, unsigned long address)
328{
329 int tsize = mmu_psize_defs[mmu_pte_psize].enc;
330
331 if (book3e_htw_enabled) {
332 unsigned long start = address & PMD_MASK;
333 unsigned long end = address + PMD_SIZE;
334 unsigned long size = 1UL << mmu_psize_defs[mmu_pte_psize].shift;
335
336 /* This isn't the most optimal, ideally we would factor out the
337 * while preempt & CPU mask mucking around, or even the IPI but
338 * it will do for now
339 */
340 while (start < end) {
341 __flush_tlb_page(tlb->mm, start, tsize, 1);
342 start += size;
343 }
344 } else {
345 unsigned long rmask = 0xf000000000000000ul;
346 unsigned long rid = (address & rmask) | 0x1000000000000000ul;
347 unsigned long vpte = address & ~rmask;
348
349#ifdef CONFIG_PPC_64K_PAGES
350 vpte = (vpte >> (PAGE_SHIFT - 4)) & ~0xfffful;
351#else
352 vpte = (vpte >> (PAGE_SHIFT - 3)) & ~0xffful;
353#endif
354 vpte |= rid;
355 __flush_tlb_page(tlb->mm, vpte, tsize, 0);
356 }
357}
358
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +1000359static void setup_page_sizes(void)
360{
Kumar Gala988cf862010-10-08 02:13:25 -0500361 unsigned int tlb0cfg;
362 unsigned int tlb0ps;
363 unsigned int eptcfg;
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +1000364 int i, psize;
365
Kumar Gala988cf862010-10-08 02:13:25 -0500366#ifdef CONFIG_PPC_FSL_BOOK3E
367 unsigned int mmucfg = mfspr(SPRN_MMUCFG);
368
369 if (((mmucfg & MMUCFG_MAVN) == MMUCFG_MAVN_V1) &&
370 (mmu_has_feature(MMU_FTR_TYPE_FSL_E))) {
371 unsigned int tlb1cfg = mfspr(SPRN_TLB1CFG);
372 unsigned int min_pg, max_pg;
373
374 min_pg = (tlb1cfg & TLBnCFG_MINSIZE) >> TLBnCFG_MINSIZE_SHIFT;
375 max_pg = (tlb1cfg & TLBnCFG_MAXSIZE) >> TLBnCFG_MAXSIZE_SHIFT;
376
377 for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
378 struct mmu_psize_def *def;
379 unsigned int shift;
380
381 def = &mmu_psize_defs[psize];
382 shift = def->shift;
383
384 if (shift == 0)
385 continue;
386
387 /* adjust to be in terms of 4^shift Kb */
388 shift = (shift - 10) >> 1;
389
390 if ((shift >= min_pg) && (shift <= max_pg))
391 def->flags |= MMU_PAGE_SIZE_DIRECT;
392 }
393
394 goto no_indirect;
395 }
396#endif
397
398 tlb0cfg = mfspr(SPRN_TLB0CFG);
399 tlb0ps = mfspr(SPRN_TLB0PS);
400 eptcfg = mfspr(SPRN_EPTCFG);
401
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +1000402 /* Look for supported direct sizes */
403 for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
404 struct mmu_psize_def *def = &mmu_psize_defs[psize];
405
406 if (tlb0ps & (1U << (def->shift - 10)))
407 def->flags |= MMU_PAGE_SIZE_DIRECT;
408 }
409
410 /* Indirect page sizes supported ? */
411 if ((tlb0cfg & TLBnCFG_IND) == 0)
412 goto no_indirect;
413
414 /* Now, we only deal with one IND page size for each
415 * direct size. Hopefully all implementations today are
416 * unambiguous, but we might want to be careful in the
417 * future.
418 */
419 for (i = 0; i < 3; i++) {
420 unsigned int ps, sps;
421
422 sps = eptcfg & 0x1f;
423 eptcfg >>= 5;
424 ps = eptcfg & 0x1f;
425 eptcfg >>= 5;
426 if (!ps || !sps)
427 continue;
428 for (psize = 0; psize < MMU_PAGE_COUNT; psize++) {
429 struct mmu_psize_def *def = &mmu_psize_defs[psize];
430
431 if (ps == (def->shift - 10))
432 def->flags |= MMU_PAGE_SIZE_INDIRECT;
433 if (sps == (def->shift - 10))
434 def->ind = ps + 10;
435 }
436 }
437 no_indirect:
438
439 /* Cleanup array and print summary */
440 pr_info("MMU: Supported page sizes\n");
441 for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
442 struct mmu_psize_def *def = &mmu_psize_defs[psize];
443 const char *__page_type_names[] = {
444 "unsupported",
445 "direct",
446 "indirect",
447 "direct & indirect"
448 };
449 if (def->flags == 0) {
450 def->shift = 0;
451 continue;
452 }
453 pr_info(" %8ld KB as %s\n", 1ul << (def->shift - 10),
454 __page_type_names[def->flags & 0x3]);
455 }
456}
457
Scott Woodf67f4ef2011-06-22 11:25:42 +0000458static void __patch_exception(int exc, unsigned long addr)
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000459{
460 extern unsigned int interrupt_base_book3e;
Scott Woodf67f4ef2011-06-22 11:25:42 +0000461 unsigned int *ibase = &interrupt_base_book3e;
462
463 /* Our exceptions vectors start with a NOP and -then- a branch
464 * to deal with single stepping from userspace which stops on
465 * the second instruction. Thus we need to patch the second
466 * instruction of the exception, not the first one
467 */
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000468
Scott Woodf67f4ef2011-06-22 11:25:42 +0000469 patch_branch(ibase + (exc / 4) + 1, addr, 0);
470}
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +1000471
Scott Woodf67f4ef2011-06-22 11:25:42 +0000472#define patch_exception(exc, name) do { \
473 extern unsigned int name; \
474 __patch_exception((exc), (unsigned long)&name); \
475} while (0)
476
477static void setup_mmu_htw(void)
478{
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +1000479 /* Check if HW tablewalk is present, and if yes, enable it by:
480 *
481 * - patching the TLB miss handlers to branch to the
482 * one dedicates to it
483 *
484 * - setting the global book3e_htw_enabled
485 */
486 unsigned int tlb0cfg = mfspr(SPRN_TLB0CFG);
487
488 if ((tlb0cfg & TLBnCFG_IND) &&
489 (tlb0cfg & TLBnCFG_PT)) {
Scott Woodf67f4ef2011-06-22 11:25:42 +0000490 patch_exception(0x1c0, exc_data_tlb_miss_htw_book3e);
491 patch_exception(0x1e0, exc_instruction_tlb_miss_htw_book3e);
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +1000492 book3e_htw_enabled = 1;
493 }
Kumar Gala32d206e2011-05-19 20:09:28 +0000494 pr_info("MMU: Book3E HW tablewalk %s\n",
495 book3e_htw_enabled ? "enabled" : "not supported");
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +1000496}
497
498/*
499 * Early initialization of the MMU TLB code
500 */
501static void __early_init_mmu(int boot_cpu)
502{
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000503 unsigned int mas4;
504
505 /* XXX This will have to be decided at runtime, but right
Benjamin Herrenschmidt32a74942009-07-23 23:15:58 +0000506 * now our boot and TLB miss code hard wires it. Ideally
507 * we should find out a suitable page size and patch the
508 * TLB miss code (either that or use the PACA to store
509 * the value we want)
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000510 */
511 mmu_linear_psize = MMU_PAGE_1G;
512
Benjamin Herrenschmidt32a74942009-07-23 23:15:58 +0000513 /* XXX This should be decided at runtime based on supported
514 * page sizes in the TLB, but for now let's assume 16M is
515 * always there and a good fit (which it probably is)
516 */
517 mmu_vmemmap_psize = MMU_PAGE_16M;
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000518
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000519 /* XXX This code only checks for TLB 0 capabilities and doesn't
520 * check what page size combos are supported by the HW. It
521 * also doesn't handle the case where a separate array holds
522 * the IND entries from the array loaded by the PT.
523 */
524 if (boot_cpu) {
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +1000525 /* Look for supported page sizes */
526 setup_page_sizes();
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000527
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +1000528 /* Look for HW tablewalk support */
529 setup_mmu_htw();
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000530 }
531
532 /* Set MAS4 based on page table setting */
533
534 mas4 = 0x4 << MAS4_WIMGED_SHIFT;
535 if (book3e_htw_enabled) {
536 mas4 |= mas4 | MAS4_INDD;
537#ifdef CONFIG_PPC_64K_PAGES
538 mas4 |= BOOK3E_PAGESZ_256M << MAS4_TSIZED_SHIFT;
539 mmu_pte_psize = MMU_PAGE_256M;
540#else
541 mas4 |= BOOK3E_PAGESZ_1M << MAS4_TSIZED_SHIFT;
542 mmu_pte_psize = MMU_PAGE_1M;
543#endif
544 } else {
545#ifdef CONFIG_PPC_64K_PAGES
546 mas4 |= BOOK3E_PAGESZ_64K << MAS4_TSIZED_SHIFT;
547#else
548 mas4 |= BOOK3E_PAGESZ_4K << MAS4_TSIZED_SHIFT;
549#endif
550 mmu_pte_psize = mmu_virtual_psize;
551 }
552 mtspr(SPRN_MAS4, mas4);
553
554 /* Set the global containing the top of the linear mapping
555 * for use by the TLB miss code
556 */
Yinghai Lu95f72d12010-07-12 14:36:09 +1000557 linear_map_top = memblock_end_of_DRAM();
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000558
Kumar Gala55fd7662009-10-16 18:48:40 -0500559#ifdef CONFIG_PPC_FSL_BOOK3E
560 if (mmu_has_feature(MMU_FTR_TYPE_FSL_E)) {
561 unsigned int num_cams;
562
563 /* use a quarter of the TLBCAM for bolted linear map */
564 num_cams = (mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY) / 4;
565 linear_map_top = map_mem_in_cams(linear_map_top, num_cams);
566
567 /* limit memory so we dont have linear faults */
568 memblock_enforce_memory_limit(linear_map_top);
569 memblock_analyze();
Scott Woodf67f4ef2011-06-22 11:25:42 +0000570
571 patch_exception(0x1c0, exc_data_tlb_miss_bolted_book3e);
572 patch_exception(0x1e0, exc_instruction_tlb_miss_bolted_book3e);
Kumar Gala55fd7662009-10-16 18:48:40 -0500573 }
574#endif
575
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000576 /* A sync won't hurt us after mucking around with
577 * the MMU configuration
578 */
579 mb();
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700580
581 memblock_set_current_limit(linear_map_top);
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000582}
583
584void __init early_init_mmu(void)
585{
586 __early_init_mmu(1);
587}
588
589void __cpuinit early_init_mmu_secondary(void)
590{
591 __early_init_mmu(0);
592}
593
Benjamin Herrenschmidtcd3db0c2010-07-06 15:39:02 -0700594void setup_initial_memory_limit(phys_addr_t first_memblock_base,
595 phys_addr_t first_memblock_size)
596{
597 /* On Embedded 64-bit, we adjust the RMA size to match
598 * the bolted TLB entry. We know for now that only 1G
599 * entries are supported though that may eventually
600 * change. We crop it to the size of the first MEMBLOCK to
601 * avoid going over total available memory just in case...
602 */
603 ppc64_rma_size = min_t(u64, first_memblock_size, 0x40000000);
604
605 /* Finally limit subsequent allocations */
Kumar Gala4a892612010-11-10 12:29:49 +0000606 memblock_set_current_limit(first_memblock_base + ppc64_rma_size);
Benjamin Herrenschmidtcd3db0c2010-07-06 15:39:02 -0700607}
Dave Kleikamp91b191c2011-07-04 18:38:03 +0000608#else /* ! CONFIG_PPC64 */
609void __init early_init_mmu(void)
610{
611#ifdef CONFIG_PPC_47x
612 early_init_mmu_47x();
613#endif
614}
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000615#endif /* CONFIG_PPC64 */