blob: 79cf21be8f6e18276a4c6c1f86143f60987dc32f [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>
Paul Gortmaker93087942011-07-29 16:19:31 +100031#include <linux/export.h>
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +000032#include <linux/mm.h>
33#include <linux/init.h>
34#include <linux/highmem.h>
35#include <linux/pagemap.h>
36#include <linux/preempt.h>
37#include <linux/spinlock.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100038#include <linux/memblock.h>
Dave Kleikamp91b191c2011-07-04 18:38:03 +000039#include <linux/of_fdt.h>
Becky Bruce41151e72011-06-28 09:54:48 +000040#include <linux/hugetlb.h>
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +000041
42#include <asm/tlbflush.h>
43#include <asm/tlb.h>
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +000044#include <asm/code-patching.h>
Scott Woodd9e18312015-10-06 22:48:09 -050045#include <asm/cputhreads.h>
Becky Bruce41151e72011-06-28 09:54:48 +000046#include <asm/hugetlb.h>
Scott Wood28efc352013-10-11 19:22:38 -050047#include <asm/paca.h>
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +000048
49#include "mmu_decl.h"
50
Becky Bruce41151e72011-06-28 09:54:48 +000051/*
52 * This struct lists the sw-supported page sizes. The hardawre MMU may support
53 * other sizes not listed here. The .ind field is only used on MMUs that have
54 * indirect page table entries.
55 */
56#ifdef CONFIG_PPC_BOOK3E_MMU
Becky Bruce881fde12011-10-10 10:50:40 +000057#ifdef CONFIG_PPC_FSL_BOOK3E
Becky Bruce41151e72011-06-28 09:54:48 +000058struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT] = {
59 [MMU_PAGE_4K] = {
60 .shift = 12,
61 .enc = BOOK3E_PAGESZ_4K,
62 },
Scott Wood28efc352013-10-11 19:22:38 -050063 [MMU_PAGE_2M] = {
64 .shift = 21,
65 .enc = BOOK3E_PAGESZ_2M,
66 },
Becky Bruce41151e72011-06-28 09:54:48 +000067 [MMU_PAGE_4M] = {
68 .shift = 22,
69 .enc = BOOK3E_PAGESZ_4M,
70 },
71 [MMU_PAGE_16M] = {
72 .shift = 24,
73 .enc = BOOK3E_PAGESZ_16M,
74 },
75 [MMU_PAGE_64M] = {
76 .shift = 26,
77 .enc = BOOK3E_PAGESZ_64M,
78 },
79 [MMU_PAGE_256M] = {
80 .shift = 28,
81 .enc = BOOK3E_PAGESZ_256M,
82 },
83 [MMU_PAGE_1G] = {
84 .shift = 30,
85 .enc = BOOK3E_PAGESZ_1GB,
86 },
87};
88#else
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +000089struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT] = {
90 [MMU_PAGE_4K] = {
91 .shift = 12,
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +100092 .ind = 20,
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +000093 .enc = BOOK3E_PAGESZ_4K,
94 },
95 [MMU_PAGE_16K] = {
96 .shift = 14,
97 .enc = BOOK3E_PAGESZ_16K,
98 },
99 [MMU_PAGE_64K] = {
100 .shift = 16,
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +1000101 .ind = 28,
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000102 .enc = BOOK3E_PAGESZ_64K,
103 },
104 [MMU_PAGE_1M] = {
105 .shift = 20,
106 .enc = BOOK3E_PAGESZ_1M,
107 },
108 [MMU_PAGE_16M] = {
109 .shift = 24,
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +1000110 .ind = 36,
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000111 .enc = BOOK3E_PAGESZ_16M,
112 },
113 [MMU_PAGE_256M] = {
114 .shift = 28,
115 .enc = BOOK3E_PAGESZ_256M,
116 },
117 [MMU_PAGE_1G] = {
118 .shift = 30,
119 .enc = BOOK3E_PAGESZ_1GB,
120 },
121};
Becky Bruce41151e72011-06-28 09:54:48 +0000122#endif /* CONFIG_FSL_BOOKE */
123
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000124static inline int mmu_get_tsize(int psize)
125{
126 return mmu_psize_defs[psize].enc;
127}
128#else
129static inline int mmu_get_tsize(int psize)
130{
131 /* This isn't used on !Book3E for now */
132 return 0;
133}
Becky Bruce41151e72011-06-28 09:54:48 +0000134#endif /* CONFIG_PPC_BOOK3E_MMU */
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000135
136/* The variables below are currently only used on 64-bit Book3E
137 * though this will probably be made common with other nohash
138 * implementations at some point
139 */
140#ifdef CONFIG_PPC64
141
142int mmu_linear_psize; /* Page size used for the linear mapping */
143int mmu_pte_psize; /* Page size used for PTE pages */
Benjamin Herrenschmidt32a74942009-07-23 23:15:58 +0000144int mmu_vmemmap_psize; /* Page size used for the virtual mem map */
Scott Wood28efc352013-10-11 19:22:38 -0500145int book3e_htw_mode; /* HW tablewalk? Value is PPC_HTW_* */
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000146unsigned long linear_map_top; /* Top of linear mapping */
147
Scott Wood609af382014-03-10 17:29:38 -0500148
149/*
150 * Number of bytes to add to SPRN_SPRG_TLB_EXFRAME on crit/mcheck/debug
151 * exceptions. This is used for bolted and e6500 TLB miss handlers which
152 * do not modify this SPRG in the TLB miss code; for other TLB miss handlers,
153 * this is set to zero.
154 */
155int extlb_level_exc;
156
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000157#endif /* CONFIG_PPC64 */
158
Becky Bruce3160b092011-06-28 14:54:47 -0500159#ifdef CONFIG_PPC_FSL_BOOK3E
160/* next_tlbcam_idx is used to round-robin tlbcam entry assignment */
161DEFINE_PER_CPU(int, next_tlbcam_idx);
162EXPORT_PER_CPU_SYMBOL(next_tlbcam_idx);
163#endif
164
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000165/*
166 * Base TLB flushing operations:
167 *
168 * - flush_tlb_mm(mm) flushes the specified mm context TLB's
169 * - flush_tlb_page(vma, vmaddr) flushes one page
170 * - flush_tlb_range(vma, start, end) flushes a range of pages
171 * - flush_tlb_kernel_range(start, end) flushes kernel pages
172 *
173 * - local_* variants of page and mm only apply to the current
174 * processor
175 */
176
177/*
178 * These are the base non-SMP variants of page and mm flushing
179 */
180void local_flush_tlb_mm(struct mm_struct *mm)
181{
182 unsigned int pid;
183
184 preempt_disable();
185 pid = mm->context.id;
186 if (pid != MMU_NO_CONTEXT)
187 _tlbil_pid(pid);
188 preempt_enable();
189}
190EXPORT_SYMBOL(local_flush_tlb_mm);
191
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000192void __local_flush_tlb_page(struct mm_struct *mm, unsigned long vmaddr,
193 int tsize, int ind)
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000194{
195 unsigned int pid;
196
197 preempt_disable();
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000198 pid = mm ? mm->context.id : 0;
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000199 if (pid != MMU_NO_CONTEXT)
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000200 _tlbil_va(vmaddr, pid, tsize, ind);
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000201 preempt_enable();
202}
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000203
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000204void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
205{
206 __local_flush_tlb_page(vma ? vma->vm_mm : NULL, vmaddr,
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000207 mmu_get_tsize(mmu_virtual_psize), 0);
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000208}
209EXPORT_SYMBOL(local_flush_tlb_page);
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000210
211/*
212 * And here are the SMP non-local implementations
213 */
214#ifdef CONFIG_SMP
215
Thomas Gleixner3eb93c52010-02-18 02:22:44 +0000216static DEFINE_RAW_SPINLOCK(tlbivax_lock);
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000217
218struct tlb_flush_param {
219 unsigned long addr;
220 unsigned int pid;
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000221 unsigned int tsize;
222 unsigned int ind;
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000223};
224
225static void do_flush_tlb_mm_ipi(void *param)
226{
227 struct tlb_flush_param *p = param;
228
229 _tlbil_pid(p ? p->pid : 0);
230}
231
232static void do_flush_tlb_page_ipi(void *param)
233{
234 struct tlb_flush_param *p = param;
235
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000236 _tlbil_va(p->addr, p->pid, p->tsize, p->ind);
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000237}
238
239
240/* Note on invalidations and PID:
241 *
242 * We snapshot the PID with preempt disabled. At this point, it can still
243 * change either because:
244 * - our context is being stolen (PID -> NO_CONTEXT) on another CPU
245 * - we are invaliating some target that isn't currently running here
246 * and is concurrently acquiring a new PID on another CPU
247 * - some other CPU is re-acquiring a lost PID for this mm
248 * etc...
249 *
250 * However, this shouldn't be a problem as we only guarantee
251 * invalidation of TLB entries present prior to this call, so we
252 * don't care about the PID changing, and invalidating a stale PID
253 * is generally harmless.
254 */
255
256void flush_tlb_mm(struct mm_struct *mm)
257{
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000258 unsigned int pid;
259
260 preempt_disable();
261 pid = mm->context.id;
262 if (unlikely(pid == MMU_NO_CONTEXT))
263 goto no_context;
Benjamin Herrenschmidtfcce8102009-07-23 23:15:10 +0000264 if (!mm_is_core_local(mm)) {
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000265 struct tlb_flush_param p = { .pid = pid };
Rusty Russell56aa4122009-03-15 18:16:43 +0000266 /* Ignores smp_processor_id() even if set. */
267 smp_call_function_many(mm_cpumask(mm),
268 do_flush_tlb_mm_ipi, &p, 1);
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000269 }
270 _tlbil_pid(pid);
271 no_context:
272 preempt_enable();
273}
274EXPORT_SYMBOL(flush_tlb_mm);
275
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000276void __flush_tlb_page(struct mm_struct *mm, unsigned long vmaddr,
277 int tsize, int ind)
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000278{
Rusty Russell56aa4122009-03-15 18:16:43 +0000279 struct cpumask *cpu_mask;
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000280 unsigned int pid;
281
Arseny Solokhac2c896b2015-02-04 13:18:02 +1100282 /*
283 * This function as well as __local_flush_tlb_page() must only be called
284 * for user contexts.
285 */
286 if (unlikely(WARN_ON(!mm)))
Arseny Solokha0dc294f2015-01-30 19:08:27 +0700287 return;
288
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000289 preempt_disable();
Arseny Solokha0dc294f2015-01-30 19:08:27 +0700290 pid = mm->context.id;
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000291 if (unlikely(pid == MMU_NO_CONTEXT))
292 goto bail;
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000293 cpu_mask = mm_cpumask(mm);
Benjamin Herrenschmidtfcce8102009-07-23 23:15:10 +0000294 if (!mm_is_core_local(mm)) {
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000295 /* If broadcast tlbivax is supported, use it */
296 if (mmu_has_feature(MMU_FTR_USE_TLBIVAX_BCAST)) {
297 int lock = mmu_has_feature(MMU_FTR_LOCK_BCAST_INVAL);
298 if (lock)
Thomas Gleixner3eb93c52010-02-18 02:22:44 +0000299 raw_spin_lock(&tlbivax_lock);
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000300 _tlbivax_bcast(vmaddr, pid, tsize, ind);
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000301 if (lock)
Thomas Gleixner3eb93c52010-02-18 02:22:44 +0000302 raw_spin_unlock(&tlbivax_lock);
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000303 goto bail;
304 } else {
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000305 struct tlb_flush_param p = {
306 .pid = pid,
307 .addr = vmaddr,
308 .tsize = tsize,
309 .ind = ind,
310 };
Rusty Russell56aa4122009-03-15 18:16:43 +0000311 /* Ignores smp_processor_id() even if set in cpu_mask */
312 smp_call_function_many(cpu_mask,
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000313 do_flush_tlb_page_ipi, &p, 1);
314 }
315 }
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000316 _tlbil_va(vmaddr, pid, tsize, ind);
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000317 bail:
318 preempt_enable();
319}
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000320
321void flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
322{
Becky Bruce41151e72011-06-28 09:54:48 +0000323#ifdef CONFIG_HUGETLB_PAGE
Scott Woodd742aa12013-11-21 18:26:42 -0600324 if (vma && is_vm_hugetlb_page(vma))
Becky Bruce41151e72011-06-28 09:54:48 +0000325 flush_hugetlb_page(vma, vmaddr);
326#endif
327
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000328 __flush_tlb_page(vma ? vma->vm_mm : NULL, vmaddr,
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000329 mmu_get_tsize(mmu_virtual_psize), 0);
Benjamin Herrenschmidtd4e167d2009-07-23 23:15:24 +0000330}
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000331EXPORT_SYMBOL(flush_tlb_page);
332
333#endif /* CONFIG_SMP */
334
Dave Kleikamp91b191c2011-07-04 18:38:03 +0000335#ifdef CONFIG_PPC_47x
336void __init early_init_mmu_47x(void)
337{
338#ifdef CONFIG_SMP
339 unsigned long root = of_get_flat_dt_root();
340 if (of_get_flat_dt_prop(root, "cooperative-partition", NULL))
341 mmu_clear_feature(MMU_FTR_USE_TLBIVAX_BCAST);
342#endif /* CONFIG_SMP */
343}
344#endif /* CONFIG_PPC_47x */
345
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000346/*
347 * Flush kernel TLB entries in the given range
348 */
349void flush_tlb_kernel_range(unsigned long start, unsigned long end)
350{
351#ifdef CONFIG_SMP
352 preempt_disable();
353 smp_call_function(do_flush_tlb_mm_ipi, NULL, 1);
354 _tlbil_pid(0);
355 preempt_enable();
Dave Liud6a09e02008-12-30 23:42:55 +0000356#else
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000357 _tlbil_pid(0);
Dave Liud6a09e02008-12-30 23:42:55 +0000358#endif
Benjamin Herrenschmidtf048aac2008-12-18 19:13:38 +0000359}
360EXPORT_SYMBOL(flush_tlb_kernel_range);
361
362/*
363 * Currently, for range flushing, we just do a full mm flush. This should
364 * be optimized based on a threshold on the size of the range, since
365 * some implementation can stack multiple tlbivax before a tlbsync but
366 * for now, we keep it that way
367 */
368void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
369 unsigned long end)
370
371{
372 flush_tlb_mm(vma->vm_mm);
373}
374EXPORT_SYMBOL(flush_tlb_range);
Benjamin Herrenschmidtc7cc58a12009-07-23 23:15:28 +0000375
376void tlb_flush(struct mmu_gather *tlb)
377{
378 flush_tlb_mm(tlb->mm);
Benjamin Herrenschmidtc7cc58a12009-07-23 23:15:28 +0000379}
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000380
381/*
382 * Below are functions specific to the 64-bit variant of Book3E though that
383 * may change in the future
384 */
385
386#ifdef CONFIG_PPC64
387
388/*
389 * Handling of virtual linear page tables or indirect TLB entries
390 * flushing when PTE pages are freed
391 */
392void tlb_flush_pgtable(struct mmu_gather *tlb, unsigned long address)
393{
394 int tsize = mmu_psize_defs[mmu_pte_psize].enc;
395
Scott Wood28efc352013-10-11 19:22:38 -0500396 if (book3e_htw_mode != PPC_HTW_NONE) {
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000397 unsigned long start = address & PMD_MASK;
398 unsigned long end = address + PMD_SIZE;
399 unsigned long size = 1UL << mmu_psize_defs[mmu_pte_psize].shift;
400
401 /* This isn't the most optimal, ideally we would factor out the
402 * while preempt & CPU mask mucking around, or even the IPI but
403 * it will do for now
404 */
405 while (start < end) {
406 __flush_tlb_page(tlb->mm, start, tsize, 1);
407 start += size;
408 }
409 } else {
410 unsigned long rmask = 0xf000000000000000ul;
411 unsigned long rid = (address & rmask) | 0x1000000000000000ul;
412 unsigned long vpte = address & ~rmask;
413
414#ifdef CONFIG_PPC_64K_PAGES
415 vpte = (vpte >> (PAGE_SHIFT - 4)) & ~0xfffful;
416#else
417 vpte = (vpte >> (PAGE_SHIFT - 3)) & ~0xffful;
418#endif
419 vpte |= rid;
420 __flush_tlb_page(tlb->mm, vpte, tsize, 0);
421 }
422}
423
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +1000424static void setup_page_sizes(void)
425{
Kumar Gala988cf862010-10-08 02:13:25 -0500426 unsigned int tlb0cfg;
427 unsigned int tlb0ps;
428 unsigned int eptcfg;
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +1000429 int i, psize;
430
Kumar Gala988cf862010-10-08 02:13:25 -0500431#ifdef CONFIG_PPC_FSL_BOOK3E
432 unsigned int mmucfg = mfspr(SPRN_MMUCFG);
Kumar Gala1b291872013-03-05 12:08:32 -0600433 int fsl_mmu = mmu_has_feature(MMU_FTR_TYPE_FSL_E);
Kumar Gala988cf862010-10-08 02:13:25 -0500434
Kumar Gala1b291872013-03-05 12:08:32 -0600435 if (fsl_mmu && (mmucfg & MMUCFG_MAVN) == MMUCFG_MAVN_V1) {
Kumar Gala988cf862010-10-08 02:13:25 -0500436 unsigned int tlb1cfg = mfspr(SPRN_TLB1CFG);
437 unsigned int min_pg, max_pg;
438
439 min_pg = (tlb1cfg & TLBnCFG_MINSIZE) >> TLBnCFG_MINSIZE_SHIFT;
440 max_pg = (tlb1cfg & TLBnCFG_MAXSIZE) >> TLBnCFG_MAXSIZE_SHIFT;
441
442 for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
443 struct mmu_psize_def *def;
444 unsigned int shift;
445
446 def = &mmu_psize_defs[psize];
447 shift = def->shift;
448
Scott Wood28efc352013-10-11 19:22:38 -0500449 if (shift == 0 || shift & 1)
Kumar Gala988cf862010-10-08 02:13:25 -0500450 continue;
451
452 /* adjust to be in terms of 4^shift Kb */
453 shift = (shift - 10) >> 1;
454
455 if ((shift >= min_pg) && (shift <= max_pg))
456 def->flags |= MMU_PAGE_SIZE_DIRECT;
457 }
458
Scott Wood28efc352013-10-11 19:22:38 -0500459 goto out;
Kumar Gala988cf862010-10-08 02:13:25 -0500460 }
Kumar Gala1b291872013-03-05 12:08:32 -0600461
462 if (fsl_mmu && (mmucfg & MMUCFG_MAVN) == MMUCFG_MAVN_V2) {
Scott Wood28efc352013-10-11 19:22:38 -0500463 u32 tlb1cfg, tlb1ps;
464
465 tlb0cfg = mfspr(SPRN_TLB0CFG);
466 tlb1cfg = mfspr(SPRN_TLB1CFG);
467 tlb1ps = mfspr(SPRN_TLB1PS);
468 eptcfg = mfspr(SPRN_EPTCFG);
469
470 if ((tlb1cfg & TLBnCFG_IND) && (tlb0cfg & TLBnCFG_PT))
471 book3e_htw_mode = PPC_HTW_E6500;
472
473 /*
474 * We expect 4K subpage size and unrestricted indirect size.
475 * The lack of a restriction on indirect size is a Freescale
476 * extension, indicated by PSn = 0 but SPSn != 0.
477 */
478 if (eptcfg != 2)
479 book3e_htw_mode = PPC_HTW_NONE;
Kumar Gala1b291872013-03-05 12:08:32 -0600480
481 for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
482 struct mmu_psize_def *def = &mmu_psize_defs[psize];
483
Daniel Axtens0e4dde12018-10-01 16:21:51 +1000484 if (!def->shift)
485 continue;
486
Kumar Gala1b291872013-03-05 12:08:32 -0600487 if (tlb1ps & (1U << (def->shift - 10))) {
488 def->flags |= MMU_PAGE_SIZE_DIRECT;
Scott Wood28efc352013-10-11 19:22:38 -0500489
490 if (book3e_htw_mode && psize == MMU_PAGE_2M)
491 def->flags |= MMU_PAGE_SIZE_INDIRECT;
Kumar Gala1b291872013-03-05 12:08:32 -0600492 }
493 }
494
Scott Wood28efc352013-10-11 19:22:38 -0500495 goto out;
Kumar Gala1b291872013-03-05 12:08:32 -0600496 }
Kumar Gala988cf862010-10-08 02:13:25 -0500497#endif
498
499 tlb0cfg = mfspr(SPRN_TLB0CFG);
500 tlb0ps = mfspr(SPRN_TLB0PS);
501 eptcfg = mfspr(SPRN_EPTCFG);
502
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +1000503 /* Look for supported direct sizes */
504 for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
505 struct mmu_psize_def *def = &mmu_psize_defs[psize];
506
507 if (tlb0ps & (1U << (def->shift - 10)))
508 def->flags |= MMU_PAGE_SIZE_DIRECT;
509 }
510
511 /* Indirect page sizes supported ? */
Scott Wood28efc352013-10-11 19:22:38 -0500512 if ((tlb0cfg & TLBnCFG_IND) == 0 ||
513 (tlb0cfg & TLBnCFG_PT) == 0)
514 goto out;
515
516 book3e_htw_mode = PPC_HTW_IBM;
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +1000517
518 /* Now, we only deal with one IND page size for each
519 * direct size. Hopefully all implementations today are
520 * unambiguous, but we might want to be careful in the
521 * future.
522 */
523 for (i = 0; i < 3; i++) {
524 unsigned int ps, sps;
525
526 sps = eptcfg & 0x1f;
527 eptcfg >>= 5;
528 ps = eptcfg & 0x1f;
529 eptcfg >>= 5;
530 if (!ps || !sps)
531 continue;
532 for (psize = 0; psize < MMU_PAGE_COUNT; psize++) {
533 struct mmu_psize_def *def = &mmu_psize_defs[psize];
534
535 if (ps == (def->shift - 10))
536 def->flags |= MMU_PAGE_SIZE_INDIRECT;
537 if (sps == (def->shift - 10))
538 def->ind = ps + 10;
539 }
540 }
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +1000541
Scott Wood28efc352013-10-11 19:22:38 -0500542out:
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +1000543 /* Cleanup array and print summary */
544 pr_info("MMU: Supported page sizes\n");
545 for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
546 struct mmu_psize_def *def = &mmu_psize_defs[psize];
547 const char *__page_type_names[] = {
548 "unsupported",
549 "direct",
550 "indirect",
551 "direct & indirect"
552 };
553 if (def->flags == 0) {
554 def->shift = 0;
555 continue;
556 }
557 pr_info(" %8ld KB as %s\n", 1ul << (def->shift - 10),
558 __page_type_names[def->flags & 0x3]);
559 }
560}
561
Scott Woodf67f4ef2011-06-22 11:25:42 +0000562static void setup_mmu_htw(void)
563{
Scott Wood28efc352013-10-11 19:22:38 -0500564 /*
565 * If we want to use HW tablewalk, enable it by patching the TLB miss
566 * handlers to branch to the one dedicated to it.
567 */
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +1000568
Scott Wood28efc352013-10-11 19:22:38 -0500569 switch (book3e_htw_mode) {
570 case PPC_HTW_IBM:
Scott Woodf67f4ef2011-06-22 11:25:42 +0000571 patch_exception(0x1c0, exc_data_tlb_miss_htw_book3e);
572 patch_exception(0x1e0, exc_instruction_tlb_miss_htw_book3e);
Scott Wood28efc352013-10-11 19:22:38 -0500573 break;
Scott Wood9841c792014-01-17 18:36:38 -0600574#ifdef CONFIG_PPC_FSL_BOOK3E
Scott Wood28efc352013-10-11 19:22:38 -0500575 case PPC_HTW_E6500:
Scott Wood609af382014-03-10 17:29:38 -0500576 extlb_level_exc = EX_TLB_SIZE;
Scott Wood28efc352013-10-11 19:22:38 -0500577 patch_exception(0x1c0, exc_data_tlb_miss_e6500_book3e);
578 patch_exception(0x1e0, exc_instruction_tlb_miss_e6500_book3e);
579 break;
Scott Wood9841c792014-01-17 18:36:38 -0600580#endif
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +1000581 }
Kumar Gala32d206e2011-05-19 20:09:28 +0000582 pr_info("MMU: Book3E HW tablewalk %s\n",
Scott Wood28efc352013-10-11 19:22:38 -0500583 book3e_htw_mode != PPC_HTW_NONE ? "enabled" : "not supported");
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +1000584}
585
586/*
587 * Early initialization of the MMU TLB code
588 */
Scott Wood5d61a212014-08-08 18:44:01 -0500589static void early_init_this_mmu(void)
Benjamin Herrenschmidtf2b26c92010-07-09 14:57:43 +1000590{
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000591 unsigned int mas4;
592
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000593 /* Set MAS4 based on page table setting */
594
595 mas4 = 0x4 << MAS4_WIMGED_SHIFT;
Scott Wood28efc352013-10-11 19:22:38 -0500596 switch (book3e_htw_mode) {
597 case PPC_HTW_E6500:
598 mas4 |= MAS4_INDD;
599 mas4 |= BOOK3E_PAGESZ_2M << MAS4_TSIZED_SHIFT;
600 mas4 |= MAS4_TLBSELD(1);
601 mmu_pte_psize = MMU_PAGE_2M;
602 break;
603
604 case PPC_HTW_IBM:
605 mas4 |= MAS4_INDD;
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000606#ifdef CONFIG_PPC_64K_PAGES
607 mas4 |= BOOK3E_PAGESZ_256M << MAS4_TSIZED_SHIFT;
608 mmu_pte_psize = MMU_PAGE_256M;
609#else
610 mas4 |= BOOK3E_PAGESZ_1M << MAS4_TSIZED_SHIFT;
611 mmu_pte_psize = MMU_PAGE_1M;
612#endif
Scott Wood28efc352013-10-11 19:22:38 -0500613 break;
614
615 case PPC_HTW_NONE:
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000616#ifdef CONFIG_PPC_64K_PAGES
617 mas4 |= BOOK3E_PAGESZ_64K << MAS4_TSIZED_SHIFT;
618#else
619 mas4 |= BOOK3E_PAGESZ_4K << MAS4_TSIZED_SHIFT;
620#endif
621 mmu_pte_psize = mmu_virtual_psize;
Scott Wood28efc352013-10-11 19:22:38 -0500622 break;
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000623 }
624 mtspr(SPRN_MAS4, mas4);
625
Kumar Gala55fd7662009-10-16 18:48:40 -0500626#ifdef CONFIG_PPC_FSL_BOOK3E
627 if (mmu_has_feature(MMU_FTR_TYPE_FSL_E)) {
628 unsigned int num_cams;
Scott Woodd9e18312015-10-06 22:48:09 -0500629 int __maybe_unused cpu = smp_processor_id();
630 bool map = true;
Kumar Gala55fd7662009-10-16 18:48:40 -0500631
632 /* use a quarter of the TLBCAM for bolted linear map */
633 num_cams = (mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY) / 4;
Scott Woodd9e18312015-10-06 22:48:09 -0500634
635 /*
636 * Only do the mapping once per core, or else the
637 * transient mapping would cause problems.
638 */
639#ifdef CONFIG_SMP
chenhui zhaoebb9d302015-12-24 08:39:57 +0800640 if (hweight32(get_tensr()) > 1)
Scott Woodd9e18312015-10-06 22:48:09 -0500641 map = false;
642#endif
643
644 if (map)
645 linear_map_top = map_mem_in_cams(linear_map_top,
Scott Woodeba5de82015-10-06 22:48:10 -0500646 num_cams, false);
Scott Wood5d61a212014-08-08 18:44:01 -0500647 }
648#endif
Kumar Gala55fd7662009-10-16 18:48:40 -0500649
Scott Wood5d61a212014-08-08 18:44:01 -0500650 /* A sync won't hurt us after mucking around with
651 * the MMU configuration
652 */
653 mb();
654}
Scott Woodf67f4ef2011-06-22 11:25:42 +0000655
Scott Wood5d61a212014-08-08 18:44:01 -0500656static void __init early_init_mmu_global(void)
657{
658 /* XXX This will have to be decided at runtime, but right
659 * now our boot and TLB miss code hard wires it. Ideally
660 * we should find out a suitable page size and patch the
661 * TLB miss code (either that or use the PACA to store
662 * the value we want)
663 */
664 mmu_linear_psize = MMU_PAGE_1G;
665
666 /* XXX This should be decided at runtime based on supported
667 * page sizes in the TLB, but for now let's assume 16M is
668 * always there and a good fit (which it probably is)
669 *
670 * Freescale booke only supports 4K pages in TLB0, so use that.
671 */
672 if (mmu_has_feature(MMU_FTR_TYPE_FSL_E))
673 mmu_vmemmap_psize = MMU_PAGE_4K;
674 else
675 mmu_vmemmap_psize = MMU_PAGE_16M;
676
677 /* XXX This code only checks for TLB 0 capabilities and doesn't
678 * check what page size combos are supported by the HW. It
679 * also doesn't handle the case where a separate array holds
680 * the IND entries from the array loaded by the PT.
681 */
682 /* Look for supported page sizes */
683 setup_page_sizes();
684
685 /* Look for HW tablewalk support */
686 setup_mmu_htw();
687
688#ifdef CONFIG_PPC_FSL_BOOK3E
689 if (mmu_has_feature(MMU_FTR_TYPE_FSL_E)) {
Scott Wood28efc352013-10-11 19:22:38 -0500690 if (book3e_htw_mode == PPC_HTW_NONE) {
Scott Wood609af382014-03-10 17:29:38 -0500691 extlb_level_exc = EX_TLB_SIZE;
Scott Wood28efc352013-10-11 19:22:38 -0500692 patch_exception(0x1c0, exc_data_tlb_miss_bolted_book3e);
693 patch_exception(0x1e0,
694 exc_instruction_tlb_miss_bolted_book3e);
695 }
Kumar Gala55fd7662009-10-16 18:48:40 -0500696 }
697#endif
698
Scott Wood5d61a212014-08-08 18:44:01 -0500699 /* Set the global containing the top of the linear mapping
700 * for use by the TLB miss code
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000701 */
Scott Wood5d61a212014-08-08 18:44:01 -0500702 linear_map_top = memblock_end_of_DRAM();
703}
704
705static void __init early_mmu_set_memory_limit(void)
706{
707#ifdef CONFIG_PPC_FSL_BOOK3E
708 if (mmu_has_feature(MMU_FTR_TYPE_FSL_E)) {
709 /*
710 * Limit memory so we dont have linear faults.
711 * Unlike memblock_set_current_limit, which limits
712 * memory available during early boot, this permanently
713 * reduces the memory available to Linux. We need to
714 * do this because highmem is not supported on 64-bit.
715 */
716 memblock_enforce_memory_limit(linear_map_top);
717 }
718#endif
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700719
720 memblock_set_current_limit(linear_map_top);
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000721}
722
Scott Wood5d61a212014-08-08 18:44:01 -0500723/* boot cpu only */
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000724void __init early_init_mmu(void)
725{
Scott Wood5d61a212014-08-08 18:44:01 -0500726 early_init_mmu_global();
727 early_init_this_mmu();
728 early_mmu_set_memory_limit();
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000729}
730
Paul Gortmaker061d19f2013-06-24 15:30:09 -0400731void early_init_mmu_secondary(void)
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000732{
Scott Wood5d61a212014-08-08 18:44:01 -0500733 early_init_this_mmu();
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000734}
735
Benjamin Herrenschmidtcd3db0c2010-07-06 15:39:02 -0700736void setup_initial_memory_limit(phys_addr_t first_memblock_base,
737 phys_addr_t first_memblock_size)
738{
Kumar Gala1dc91c32011-09-16 10:39:59 -0500739 /* On non-FSL Embedded 64-bit, we adjust the RMA size to match
Benjamin Herrenschmidtcd3db0c2010-07-06 15:39:02 -0700740 * the bolted TLB entry. We know for now that only 1G
741 * entries are supported though that may eventually
Kumar Gala1dc91c32011-09-16 10:39:59 -0500742 * change.
743 *
Scott Woodeba5de82015-10-06 22:48:10 -0500744 * on FSL Embedded 64-bit, usually all RAM is bolted, but with
745 * unusual memory sizes it's possible for some RAM to not be mapped
746 * (such RAM is not used at all by Linux, since we don't support
747 * highmem on 64-bit). We limit ppc64_rma_size to what would be
748 * mappable if this memblock is the only one. Additional memblocks
749 * can only increase, not decrease, the amount that ends up getting
750 * mapped. We still limit max to 1G even if we'll eventually map
751 * more. This is due to what the early init code is set up to do.
Kumar Gala1dc91c32011-09-16 10:39:59 -0500752 *
753 * We crop it to the size of the first MEMBLOCK to
Benjamin Herrenschmidtcd3db0c2010-07-06 15:39:02 -0700754 * avoid going over total available memory just in case...
755 */
Kumar Gala1dc91c32011-09-16 10:39:59 -0500756#ifdef CONFIG_PPC_FSL_BOOK3E
Michael Ellerman708def52017-04-03 12:05:55 +1000757 if (early_mmu_has_feature(MMU_FTR_TYPE_FSL_E)) {
Kumar Gala1dc91c32011-09-16 10:39:59 -0500758 unsigned long linear_sz;
Scott Woodeba5de82015-10-06 22:48:10 -0500759 unsigned int num_cams;
760
761 /* use a quarter of the TLBCAM for bolted linear map */
762 num_cams = (mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY) / 4;
763
764 linear_sz = map_mem_in_cams(first_memblock_size, num_cams,
765 true);
766
Kumar Gala1dc91c32011-09-16 10:39:59 -0500767 ppc64_rma_size = min_t(u64, linear_sz, 0x40000000);
768 } else
769#endif
770 ppc64_rma_size = min_t(u64, first_memblock_size, 0x40000000);
Benjamin Herrenschmidtcd3db0c2010-07-06 15:39:02 -0700771
772 /* Finally limit subsequent allocations */
Kumar Gala4a892612010-11-10 12:29:49 +0000773 memblock_set_current_limit(first_memblock_base + ppc64_rma_size);
Benjamin Herrenschmidtcd3db0c2010-07-06 15:39:02 -0700774}
Dave Kleikamp91b191c2011-07-04 18:38:03 +0000775#else /* ! CONFIG_PPC64 */
776void __init early_init_mmu(void)
777{
778#ifdef CONFIG_PPC_47x
779 early_init_mmu_47x();
780#endif
781}
Benjamin Herrenschmidt25d21ad2009-07-23 23:15:47 +0000782#endif /* CONFIG_PPC64 */