blob: d32ec643c231fa6b038d579b7112e590c03daf92 [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
Becky Bruce3160b092011-06-28 14:54:47 -0500106#ifdef CONFIG_PPC_FSL_BOOK3E
107/* next_tlbcam_idx is used to round-robin tlbcam entry assignment */
108DEFINE_PER_CPU(int, next_tlbcam_idx);
109EXPORT_PER_CPU_SYMBOL(next_tlbcam_idx);
110#endif
111
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000112/*
113 * Base TLB flushing operations:
114 *
115 * - flush_tlb_mm(mm) flushes the specified mm context TLB's
116 * - flush_tlb_page(vma, vmaddr) flushes one page
117 * - flush_tlb_range(vma, start, end) flushes a range of pages
118 * - flush_tlb_kernel_range(start, end) flushes kernel pages
119 *
120 * - local_* variants of page and mm only apply to the current
121 * processor
122 */
123
124/*
125 * These are the base non-SMP variants of page and mm flushing
126 */
127void local_flush_tlb_mm(struct mm_struct *mm)
128{
129 unsigned int pid;
130
131 preempt_disable();
132 pid = mm->context.id;
133 if (pid != MMU_NO_CONTEXT)
134 _tlbil_pid(pid);
135 preempt_enable();
136}
137EXPORT_SYMBOL(local_flush_tlb_mm);
138
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000139void __local_flush_tlb_page(struct mm_struct *mm, unsigned long vmaddr,
140 int tsize, int ind)
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000141{
142 unsigned int pid;
143
144 preempt_disable();
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000145 pid = mm ? mm->context.id : 0;
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000146 if (pid != MMU_NO_CONTEXT)
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000147 _tlbil_va(vmaddr, pid, tsize, ind);
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000148 preempt_enable();
149}
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000150
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000151void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
152{
153 __local_flush_tlb_page(vma ? vma->vm_mm : NULL, vmaddr,
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000154 mmu_get_tsize(mmu_virtual_psize), 0);
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000155}
156EXPORT_SYMBOL(local_flush_tlb_page);
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000157
158/*
159 * And here are the SMP non-local implementations
160 */
161#ifdef CONFIG_SMP
162
Thomas Gleixner3eb93c52010-02-18 02:22:44 +0000163static DEFINE_RAW_SPINLOCK(tlbivax_lock);
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000164
Benjamin Herrenschmidtfcce8102009-07-23 23:15:10 +0000165static int mm_is_core_local(struct mm_struct *mm)
166{
167 return cpumask_subset(mm_cpumask(mm),
168 topology_thread_cpumask(smp_processor_id()));
169}
170
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000171struct tlb_flush_param {
172 unsigned long addr;
173 unsigned int pid;
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000174 unsigned int tsize;
175 unsigned int ind;
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000176};
177
178static void do_flush_tlb_mm_ipi(void *param)
179{
180 struct tlb_flush_param *p = param;
181
182 _tlbil_pid(p ? p->pid : 0);
183}
184
185static void do_flush_tlb_page_ipi(void *param)
186{
187 struct tlb_flush_param *p = param;
188
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000189 _tlbil_va(p->addr, p->pid, p->tsize, p->ind);
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000190}
191
192
193/* Note on invalidations and PID:
194 *
195 * We snapshot the PID with preempt disabled. At this point, it can still
196 * change either because:
197 * - our context is being stolen (PID -> NO_CONTEXT) on another CPU
198 * - we are invaliating some target that isn't currently running here
199 * and is concurrently acquiring a new PID on another CPU
200 * - some other CPU is re-acquiring a lost PID for this mm
201 * etc...
202 *
203 * However, this shouldn't be a problem as we only guarantee
204 * invalidation of TLB entries present prior to this call, so we
205 * don't care about the PID changing, and invalidating a stale PID
206 * is generally harmless.
207 */
208
209void flush_tlb_mm(struct mm_struct *mm)
210{
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000211 unsigned int pid;
212
213 preempt_disable();
214 pid = mm->context.id;
215 if (unlikely(pid == MMU_NO_CONTEXT))
216 goto no_context;
Benjamin Herrenschmidtfcce8102009-07-23 23:15:10 +0000217 if (!mm_is_core_local(mm)) {
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000218 struct tlb_flush_param p = { .pid = pid };
Rusty Russell56aa4122009-03-15 18:16:43 +0000219 /* Ignores smp_processor_id() even if set. */
220 smp_call_function_many(mm_cpumask(mm),
221 do_flush_tlb_mm_ipi, &p, 1);
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000222 }
223 _tlbil_pid(pid);
224 no_context:
225 preempt_enable();
226}
227EXPORT_SYMBOL(flush_tlb_mm);
228
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000229void __flush_tlb_page(struct mm_struct *mm, unsigned long vmaddr,
230 int tsize, int ind)
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000231{
Rusty Russell56aa4122009-03-15 18:16:43 +0000232 struct cpumask *cpu_mask;
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000233 unsigned int pid;
234
235 preempt_disable();
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000236 pid = mm ? mm->context.id : 0;
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000237 if (unlikely(pid == MMU_NO_CONTEXT))
238 goto bail;
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000239 cpu_mask = mm_cpumask(mm);
Benjamin Herrenschmidtfcce8102009-07-23 23:15:10 +0000240 if (!mm_is_core_local(mm)) {
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000241 /* If broadcast tlbivax is supported, use it */
242 if (mmu_has_feature(MMU_FTR_USE_TLBIVAX_BCAST)) {
243 int lock = mmu_has_feature(MMU_FTR_LOCK_BCAST_INVAL);
244 if (lock)
Thomas Gleixner3eb93c52010-02-18 02:22:44 +0000245 raw_spin_lock(&tlbivax_lock);
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000246 _tlbivax_bcast(vmaddr, pid, tsize, ind);
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000247 if (lock)
Thomas Gleixner3eb93c52010-02-18 02:22:44 +0000248 raw_spin_unlock(&tlbivax_lock);
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000249 goto bail;
250 } else {
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000251 struct tlb_flush_param p = {
252 .pid = pid,
253 .addr = vmaddr,
254 .tsize = tsize,
255 .ind = ind,
256 };
Rusty Russell56aa4122009-03-15 18:16:43 +0000257 /* Ignores smp_processor_id() even if set in cpu_mask */
258 smp_call_function_many(cpu_mask,
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000259 do_flush_tlb_page_ipi, &p, 1);
260 }
261 }
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000262 _tlbil_va(vmaddr, pid, tsize, ind);
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000263 bail:
264 preempt_enable();
265}
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000266
267void flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
268{
269 __flush_tlb_page(vma ? vma->vm_mm : NULL, vmaddr,
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000270 mmu_get_tsize(mmu_virtual_psize), 0);
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000271}
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000272EXPORT_SYMBOL(flush_tlb_page);
273
274#endif /* CONFIG_SMP */
275
Dave Kleikamp91b191c2011-07-04 18:38:03 +0000276#ifdef CONFIG_PPC_47x
277void __init early_init_mmu_47x(void)
278{
279#ifdef CONFIG_SMP
280 unsigned long root = of_get_flat_dt_root();
281 if (of_get_flat_dt_prop(root, "cooperative-partition", NULL))
282 mmu_clear_feature(MMU_FTR_USE_TLBIVAX_BCAST);
283#endif /* CONFIG_SMP */
284}
285#endif /* CONFIG_PPC_47x */
286
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000287/*
288 * Flush kernel TLB entries in the given range
289 */
290void flush_tlb_kernel_range(unsigned long start, unsigned long end)
291{
292#ifdef CONFIG_SMP
293 preempt_disable();
294 smp_call_function(do_flush_tlb_mm_ipi, NULL, 1);
295 _tlbil_pid(0);
296 preempt_enable();
Dave Liud6a09e02008-12-30 23:42:55 +0000297#else
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000298 _tlbil_pid(0);
Dave Liud6a09e02008-12-30 23:42:55 +0000299#endif
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000300}
301EXPORT_SYMBOL(flush_tlb_kernel_range);
302
303/*
304 * Currently, for range flushing, we just do a full mm flush. This should
305 * be optimized based on a threshold on the size of the range, since
306 * some implementation can stack multiple tlbivax before a tlbsync but
307 * for now, we keep it that way
308 */
309void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
310 unsigned long end)
311
312{
313 flush_tlb_mm(vma->vm_mm);
314}
315EXPORT_SYMBOL(flush_tlb_range);
Benjamin Herrenschmidtc7cc58a12009-07-23 23:15:28 +0000316
317void tlb_flush(struct mmu_gather *tlb)
318{
319 flush_tlb_mm(tlb->mm);
Benjamin Herrenschmidtc7cc58a12009-07-23 23:15:28 +0000320}
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000321
322/*
323 * Below are functions specific to the 64-bit variant of Book3E though that
324 * may change in the future
325 */
326
327#ifdef CONFIG_PPC64
328
329/*
330 * Handling of virtual linear page tables or indirect TLB entries
331 * flushing when PTE pages are freed
332 */
333void tlb_flush_pgtable(struct mmu_gather *tlb, unsigned long address)
334{
335 int tsize = mmu_psize_defs[mmu_pte_psize].enc;
336
337 if (book3e_htw_enabled) {
338 unsigned long start = address & PMD_MASK;
339 unsigned long end = address + PMD_SIZE;
340 unsigned long size = 1UL << mmu_psize_defs[mmu_pte_psize].shift;
341
342 /* This isn't the most optimal, ideally we would factor out the
343 * while preempt & CPU mask mucking around, or even the IPI but
344 * it will do for now
345 */
346 while (start < end) {
347 __flush_tlb_page(tlb->mm, start, tsize, 1);
348 start += size;
349 }
350 } else {
351 unsigned long rmask = 0xf000000000000000ul;
352 unsigned long rid = (address & rmask) | 0x1000000000000000ul;
353 unsigned long vpte = address & ~rmask;
354
355#ifdef CONFIG_PPC_64K_PAGES
356 vpte = (vpte >> (PAGE_SHIFT - 4)) & ~0xfffful;
357#else
358 vpte = (vpte >> (PAGE_SHIFT - 3)) & ~0xffful;
359#endif
360 vpte |= rid;
361 __flush_tlb_page(tlb->mm, vpte, tsize, 0);
362 }
363}
364
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +1000365static void setup_page_sizes(void)
366{
Kumar Gala988cf862010-10-08 02:13:25 -0500367 unsigned int tlb0cfg;
368 unsigned int tlb0ps;
369 unsigned int eptcfg;
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +1000370 int i, psize;
371
Kumar Gala988cf862010-10-08 02:13:25 -0500372#ifdef CONFIG_PPC_FSL_BOOK3E
373 unsigned int mmucfg = mfspr(SPRN_MMUCFG);
374
375 if (((mmucfg & MMUCFG_MAVN) == MMUCFG_MAVN_V1) &&
376 (mmu_has_feature(MMU_FTR_TYPE_FSL_E))) {
377 unsigned int tlb1cfg = mfspr(SPRN_TLB1CFG);
378 unsigned int min_pg, max_pg;
379
380 min_pg = (tlb1cfg & TLBnCFG_MINSIZE) >> TLBnCFG_MINSIZE_SHIFT;
381 max_pg = (tlb1cfg & TLBnCFG_MAXSIZE) >> TLBnCFG_MAXSIZE_SHIFT;
382
383 for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
384 struct mmu_psize_def *def;
385 unsigned int shift;
386
387 def = &mmu_psize_defs[psize];
388 shift = def->shift;
389
390 if (shift == 0)
391 continue;
392
393 /* adjust to be in terms of 4^shift Kb */
394 shift = (shift - 10) >> 1;
395
396 if ((shift >= min_pg) && (shift <= max_pg))
397 def->flags |= MMU_PAGE_SIZE_DIRECT;
398 }
399
400 goto no_indirect;
401 }
402#endif
403
404 tlb0cfg = mfspr(SPRN_TLB0CFG);
405 tlb0ps = mfspr(SPRN_TLB0PS);
406 eptcfg = mfspr(SPRN_EPTCFG);
407
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +1000408 /* Look for supported direct sizes */
409 for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
410 struct mmu_psize_def *def = &mmu_psize_defs[psize];
411
412 if (tlb0ps & (1U << (def->shift - 10)))
413 def->flags |= MMU_PAGE_SIZE_DIRECT;
414 }
415
416 /* Indirect page sizes supported ? */
417 if ((tlb0cfg & TLBnCFG_IND) == 0)
418 goto no_indirect;
419
420 /* Now, we only deal with one IND page size for each
421 * direct size. Hopefully all implementations today are
422 * unambiguous, but we might want to be careful in the
423 * future.
424 */
425 for (i = 0; i < 3; i++) {
426 unsigned int ps, sps;
427
428 sps = eptcfg & 0x1f;
429 eptcfg >>= 5;
430 ps = eptcfg & 0x1f;
431 eptcfg >>= 5;
432 if (!ps || !sps)
433 continue;
434 for (psize = 0; psize < MMU_PAGE_COUNT; psize++) {
435 struct mmu_psize_def *def = &mmu_psize_defs[psize];
436
437 if (ps == (def->shift - 10))
438 def->flags |= MMU_PAGE_SIZE_INDIRECT;
439 if (sps == (def->shift - 10))
440 def->ind = ps + 10;
441 }
442 }
443 no_indirect:
444
445 /* Cleanup array and print summary */
446 pr_info("MMU: Supported page sizes\n");
447 for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
448 struct mmu_psize_def *def = &mmu_psize_defs[psize];
449 const char *__page_type_names[] = {
450 "unsupported",
451 "direct",
452 "indirect",
453 "direct & indirect"
454 };
455 if (def->flags == 0) {
456 def->shift = 0;
457 continue;
458 }
459 pr_info(" %8ld KB as %s\n", 1ul << (def->shift - 10),
460 __page_type_names[def->flags & 0x3]);
461 }
462}
463
Scott Woodf67f4ef2011-06-22 11:25:42 +0000464static void __patch_exception(int exc, unsigned long addr)
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000465{
466 extern unsigned int interrupt_base_book3e;
Scott Woodf67f4ef2011-06-22 11:25:42 +0000467 unsigned int *ibase = &interrupt_base_book3e;
468
469 /* Our exceptions vectors start with a NOP and -then- a branch
470 * to deal with single stepping from userspace which stops on
471 * the second instruction. Thus we need to patch the second
472 * instruction of the exception, not the first one
473 */
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000474
Scott Woodf67f4ef2011-06-22 11:25:42 +0000475 patch_branch(ibase + (exc / 4) + 1, addr, 0);
476}
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +1000477
Scott Woodf67f4ef2011-06-22 11:25:42 +0000478#define patch_exception(exc, name) do { \
479 extern unsigned int name; \
480 __patch_exception((exc), (unsigned long)&name); \
481} while (0)
482
483static void setup_mmu_htw(void)
484{
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +1000485 /* Check if HW tablewalk is present, and if yes, enable it by:
486 *
487 * - patching the TLB miss handlers to branch to the
488 * one dedicates to it
489 *
490 * - setting the global book3e_htw_enabled
491 */
492 unsigned int tlb0cfg = mfspr(SPRN_TLB0CFG);
493
494 if ((tlb0cfg & TLBnCFG_IND) &&
495 (tlb0cfg & TLBnCFG_PT)) {
Scott Woodf67f4ef2011-06-22 11:25:42 +0000496 patch_exception(0x1c0, exc_data_tlb_miss_htw_book3e);
497 patch_exception(0x1e0, exc_instruction_tlb_miss_htw_book3e);
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +1000498 book3e_htw_enabled = 1;
499 }
Kumar Gala32d206e2011-05-19 20:09:28 +0000500 pr_info("MMU: Book3E HW tablewalk %s\n",
501 book3e_htw_enabled ? "enabled" : "not supported");
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +1000502}
503
504/*
505 * Early initialization of the MMU TLB code
506 */
507static void __early_init_mmu(int boot_cpu)
508{
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000509 unsigned int mas4;
510
511 /* XXX This will have to be decided at runtime, but right
Benjamin Herrenschmidt32a74942009-07-23 23:15:58 +0000512 * now our boot and TLB miss code hard wires it. Ideally
513 * we should find out a suitable page size and patch the
514 * TLB miss code (either that or use the PACA to store
515 * the value we want)
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000516 */
517 mmu_linear_psize = MMU_PAGE_1G;
518
Benjamin Herrenschmidt32a74942009-07-23 23:15:58 +0000519 /* XXX This should be decided at runtime based on supported
520 * page sizes in the TLB, but for now let's assume 16M is
521 * always there and a good fit (which it probably is)
522 */
523 mmu_vmemmap_psize = MMU_PAGE_16M;
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000524
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000525 /* XXX This code only checks for TLB 0 capabilities and doesn't
526 * check what page size combos are supported by the HW. It
527 * also doesn't handle the case where a separate array holds
528 * the IND entries from the array loaded by the PT.
529 */
530 if (boot_cpu) {
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +1000531 /* Look for supported page sizes */
532 setup_page_sizes();
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000533
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +1000534 /* Look for HW tablewalk support */
535 setup_mmu_htw();
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000536 }
537
538 /* Set MAS4 based on page table setting */
539
540 mas4 = 0x4 << MAS4_WIMGED_SHIFT;
541 if (book3e_htw_enabled) {
542 mas4 |= mas4 | MAS4_INDD;
543#ifdef CONFIG_PPC_64K_PAGES
544 mas4 |= BOOK3E_PAGESZ_256M << MAS4_TSIZED_SHIFT;
545 mmu_pte_psize = MMU_PAGE_256M;
546#else
547 mas4 |= BOOK3E_PAGESZ_1M << MAS4_TSIZED_SHIFT;
548 mmu_pte_psize = MMU_PAGE_1M;
549#endif
550 } else {
551#ifdef CONFIG_PPC_64K_PAGES
552 mas4 |= BOOK3E_PAGESZ_64K << MAS4_TSIZED_SHIFT;
553#else
554 mas4 |= BOOK3E_PAGESZ_4K << MAS4_TSIZED_SHIFT;
555#endif
556 mmu_pte_psize = mmu_virtual_psize;
557 }
558 mtspr(SPRN_MAS4, mas4);
559
560 /* Set the global containing the top of the linear mapping
561 * for use by the TLB miss code
562 */
Yinghai Lu95f72d12010-07-12 14:36:09 +1000563 linear_map_top = memblock_end_of_DRAM();
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000564
Kumar Gala55fd7662009-10-16 18:48:40 -0500565#ifdef CONFIG_PPC_FSL_BOOK3E
566 if (mmu_has_feature(MMU_FTR_TYPE_FSL_E)) {
567 unsigned int num_cams;
568
569 /* use a quarter of the TLBCAM for bolted linear map */
570 num_cams = (mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY) / 4;
571 linear_map_top = map_mem_in_cams(linear_map_top, num_cams);
572
573 /* limit memory so we dont have linear faults */
574 memblock_enforce_memory_limit(linear_map_top);
575 memblock_analyze();
Scott Woodf67f4ef2011-06-22 11:25:42 +0000576
577 patch_exception(0x1c0, exc_data_tlb_miss_bolted_book3e);
578 patch_exception(0x1e0, exc_instruction_tlb_miss_bolted_book3e);
Kumar Gala55fd7662009-10-16 18:48:40 -0500579 }
580#endif
581
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000582 /* A sync won't hurt us after mucking around with
583 * the MMU configuration
584 */
585 mb();
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700586
587 memblock_set_current_limit(linear_map_top);
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000588}
589
590void __init early_init_mmu(void)
591{
592 __early_init_mmu(1);
593}
594
595void __cpuinit early_init_mmu_secondary(void)
596{
597 __early_init_mmu(0);
598}
599
Benjamin Herrenschmidtcd3db0c2010-07-06 15:39:02 -0700600void setup_initial_memory_limit(phys_addr_t first_memblock_base,
601 phys_addr_t first_memblock_size)
602{
603 /* On Embedded 64-bit, we adjust the RMA size to match
604 * the bolted TLB entry. We know for now that only 1G
605 * entries are supported though that may eventually
606 * change. We crop it to the size of the first MEMBLOCK to
607 * avoid going over total available memory just in case...
608 */
609 ppc64_rma_size = min_t(u64, first_memblock_size, 0x40000000);
610
611 /* Finally limit subsequent allocations */
Kumar Gala4a892612010-11-10 12:29:49 +0000612 memblock_set_current_limit(first_memblock_base + ppc64_rma_size);
Benjamin Herrenschmidtcd3db0c2010-07-06 15:39:02 -0700613}
Dave Kleikamp91b191c2011-07-04 18:38:03 +0000614#else /* ! CONFIG_PPC64 */
615void __init early_init_mmu(void)
616{
617#ifdef CONFIG_PPC_47x
618 early_init_mmu_47x();
619#endif
620}
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000621#endif /* CONFIG_PPC64 */