xref: /openbmc/linux/arch/powerpc/mm/nohash/tlb.c (revision fc772314)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * This file contains the routines for TLB flushing.
4  * On machines where the MMU does not use a hash table to store virtual to
5  * physical translations (ie, SW loaded TLBs or Book3E compilant processors,
6  * this does -not- include 603 however which shares the implementation with
7  * hash based processors)
8  *
9  *  -- BenH
10  *
11  * Copyright 2008,2009 Ben Herrenschmidt <benh@kernel.crashing.org>
12  *                     IBM Corp.
13  *
14  *  Derived from arch/ppc/mm/init.c:
15  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
16  *
17  *  Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
18  *  and Cort Dougan (PReP) (cort@cs.nmt.edu)
19  *    Copyright (C) 1996 Paul Mackerras
20  *
21  *  Derived from "arch/i386/mm/init.c"
22  *    Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
23  */
24 
25 #include <linux/kernel.h>
26 #include <linux/export.h>
27 #include <linux/mm.h>
28 #include <linux/init.h>
29 #include <linux/highmem.h>
30 #include <linux/pagemap.h>
31 #include <linux/preempt.h>
32 #include <linux/spinlock.h>
33 #include <linux/memblock.h>
34 #include <linux/of_fdt.h>
35 #include <linux/hugetlb.h>
36 
37 #include <asm/pgalloc.h>
38 #include <asm/tlbflush.h>
39 #include <asm/tlb.h>
40 #include <asm/code-patching.h>
41 #include <asm/cputhreads.h>
42 #include <asm/hugetlb.h>
43 #include <asm/paca.h>
44 
45 #include <mm/mmu_decl.h>
46 
47 /*
48  * This struct lists the sw-supported page sizes.  The hardawre MMU may support
49  * other sizes not listed here.   The .ind field is only used on MMUs that have
50  * indirect page table entries.
51  */
52 #if defined(CONFIG_PPC_BOOK3E_MMU) || defined(CONFIG_PPC_8xx)
53 #ifdef CONFIG_PPC_FSL_BOOK3E
54 struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT] = {
55 	[MMU_PAGE_4K] = {
56 		.shift	= 12,
57 		.enc	= BOOK3E_PAGESZ_4K,
58 	},
59 	[MMU_PAGE_2M] = {
60 		.shift	= 21,
61 		.enc	= BOOK3E_PAGESZ_2M,
62 	},
63 	[MMU_PAGE_4M] = {
64 		.shift	= 22,
65 		.enc	= BOOK3E_PAGESZ_4M,
66 	},
67 	[MMU_PAGE_16M] = {
68 		.shift	= 24,
69 		.enc	= BOOK3E_PAGESZ_16M,
70 	},
71 	[MMU_PAGE_64M] = {
72 		.shift	= 26,
73 		.enc	= BOOK3E_PAGESZ_64M,
74 	},
75 	[MMU_PAGE_256M] = {
76 		.shift	= 28,
77 		.enc	= BOOK3E_PAGESZ_256M,
78 	},
79 	[MMU_PAGE_1G] = {
80 		.shift	= 30,
81 		.enc	= BOOK3E_PAGESZ_1GB,
82 	},
83 };
84 #elif defined(CONFIG_PPC_8xx)
85 struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT] = {
86 	/* we only manage 4k and 16k pages as normal pages */
87 #ifdef CONFIG_PPC_4K_PAGES
88 	[MMU_PAGE_4K] = {
89 		.shift	= 12,
90 	},
91 #else
92 	[MMU_PAGE_16K] = {
93 		.shift	= 14,
94 	},
95 #endif
96 	[MMU_PAGE_512K] = {
97 		.shift	= 19,
98 	},
99 	[MMU_PAGE_8M] = {
100 		.shift	= 23,
101 	},
102 };
103 #else
104 struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT] = {
105 	[MMU_PAGE_4K] = {
106 		.shift	= 12,
107 		.ind	= 20,
108 		.enc	= BOOK3E_PAGESZ_4K,
109 	},
110 	[MMU_PAGE_16K] = {
111 		.shift	= 14,
112 		.enc	= BOOK3E_PAGESZ_16K,
113 	},
114 	[MMU_PAGE_64K] = {
115 		.shift	= 16,
116 		.ind	= 28,
117 		.enc	= BOOK3E_PAGESZ_64K,
118 	},
119 	[MMU_PAGE_1M] = {
120 		.shift	= 20,
121 		.enc	= BOOK3E_PAGESZ_1M,
122 	},
123 	[MMU_PAGE_16M] = {
124 		.shift	= 24,
125 		.ind	= 36,
126 		.enc	= BOOK3E_PAGESZ_16M,
127 	},
128 	[MMU_PAGE_256M] = {
129 		.shift	= 28,
130 		.enc	= BOOK3E_PAGESZ_256M,
131 	},
132 	[MMU_PAGE_1G] = {
133 		.shift	= 30,
134 		.enc	= BOOK3E_PAGESZ_1GB,
135 	},
136 };
137 #endif /* CONFIG_FSL_BOOKE */
138 
139 static inline int mmu_get_tsize(int psize)
140 {
141 	return mmu_psize_defs[psize].enc;
142 }
143 #else
144 static inline int mmu_get_tsize(int psize)
145 {
146 	/* This isn't used on !Book3E for now */
147 	return 0;
148 }
149 #endif /* CONFIG_PPC_BOOK3E_MMU */
150 
151 /* The variables below are currently only used on 64-bit Book3E
152  * though this will probably be made common with other nohash
153  * implementations at some point
154  */
155 #ifdef CONFIG_PPC64
156 
157 int mmu_linear_psize;		/* Page size used for the linear mapping */
158 int mmu_pte_psize;		/* Page size used for PTE pages */
159 int mmu_vmemmap_psize;		/* Page size used for the virtual mem map */
160 int book3e_htw_mode;		/* HW tablewalk?  Value is PPC_HTW_* */
161 unsigned long linear_map_top;	/* Top of linear mapping */
162 
163 
164 /*
165  * Number of bytes to add to SPRN_SPRG_TLB_EXFRAME on crit/mcheck/debug
166  * exceptions.  This is used for bolted and e6500 TLB miss handlers which
167  * do not modify this SPRG in the TLB miss code; for other TLB miss handlers,
168  * this is set to zero.
169  */
170 int extlb_level_exc;
171 
172 #endif /* CONFIG_PPC64 */
173 
174 #ifdef CONFIG_PPC_FSL_BOOK3E
175 /* next_tlbcam_idx is used to round-robin tlbcam entry assignment */
176 DEFINE_PER_CPU(int, next_tlbcam_idx);
177 EXPORT_PER_CPU_SYMBOL(next_tlbcam_idx);
178 #endif
179 
180 /*
181  * Base TLB flushing operations:
182  *
183  *  - flush_tlb_mm(mm) flushes the specified mm context TLB's
184  *  - flush_tlb_page(vma, vmaddr) flushes one page
185  *  - flush_tlb_range(vma, start, end) flushes a range of pages
186  *  - flush_tlb_kernel_range(start, end) flushes kernel pages
187  *
188  *  - local_* variants of page and mm only apply to the current
189  *    processor
190  */
191 
192 /*
193  * These are the base non-SMP variants of page and mm flushing
194  */
195 void local_flush_tlb_mm(struct mm_struct *mm)
196 {
197 	unsigned int pid;
198 
199 	preempt_disable();
200 	pid = mm->context.id;
201 	if (pid != MMU_NO_CONTEXT)
202 		_tlbil_pid(pid);
203 	preempt_enable();
204 }
205 EXPORT_SYMBOL(local_flush_tlb_mm);
206 
207 void __local_flush_tlb_page(struct mm_struct *mm, unsigned long vmaddr,
208 			    int tsize, int ind)
209 {
210 	unsigned int pid;
211 
212 	preempt_disable();
213 	pid = mm ? mm->context.id : 0;
214 	if (pid != MMU_NO_CONTEXT)
215 		_tlbil_va(vmaddr, pid, tsize, ind);
216 	preempt_enable();
217 }
218 
219 void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
220 {
221 	__local_flush_tlb_page(vma ? vma->vm_mm : NULL, vmaddr,
222 			       mmu_get_tsize(mmu_virtual_psize), 0);
223 }
224 EXPORT_SYMBOL(local_flush_tlb_page);
225 
226 /*
227  * And here are the SMP non-local implementations
228  */
229 #ifdef CONFIG_SMP
230 
231 static DEFINE_RAW_SPINLOCK(tlbivax_lock);
232 
233 struct tlb_flush_param {
234 	unsigned long addr;
235 	unsigned int pid;
236 	unsigned int tsize;
237 	unsigned int ind;
238 };
239 
240 static void do_flush_tlb_mm_ipi(void *param)
241 {
242 	struct tlb_flush_param *p = param;
243 
244 	_tlbil_pid(p ? p->pid : 0);
245 }
246 
247 static void do_flush_tlb_page_ipi(void *param)
248 {
249 	struct tlb_flush_param *p = param;
250 
251 	_tlbil_va(p->addr, p->pid, p->tsize, p->ind);
252 }
253 
254 
255 /* Note on invalidations and PID:
256  *
257  * We snapshot the PID with preempt disabled. At this point, it can still
258  * change either because:
259  * - our context is being stolen (PID -> NO_CONTEXT) on another CPU
260  * - we are invaliating some target that isn't currently running here
261  *   and is concurrently acquiring a new PID on another CPU
262  * - some other CPU is re-acquiring a lost PID for this mm
263  * etc...
264  *
265  * However, this shouldn't be a problem as we only guarantee
266  * invalidation of TLB entries present prior to this call, so we
267  * don't care about the PID changing, and invalidating a stale PID
268  * is generally harmless.
269  */
270 
271 void flush_tlb_mm(struct mm_struct *mm)
272 {
273 	unsigned int pid;
274 
275 	preempt_disable();
276 	pid = mm->context.id;
277 	if (unlikely(pid == MMU_NO_CONTEXT))
278 		goto no_context;
279 	if (!mm_is_core_local(mm)) {
280 		struct tlb_flush_param p = { .pid = pid };
281 		/* Ignores smp_processor_id() even if set. */
282 		smp_call_function_many(mm_cpumask(mm),
283 				       do_flush_tlb_mm_ipi, &p, 1);
284 	}
285 	_tlbil_pid(pid);
286  no_context:
287 	preempt_enable();
288 }
289 EXPORT_SYMBOL(flush_tlb_mm);
290 
291 void __flush_tlb_page(struct mm_struct *mm, unsigned long vmaddr,
292 		      int tsize, int ind)
293 {
294 	struct cpumask *cpu_mask;
295 	unsigned int pid;
296 
297 	/*
298 	 * This function as well as __local_flush_tlb_page() must only be called
299 	 * for user contexts.
300 	 */
301 	if (WARN_ON(!mm))
302 		return;
303 
304 	preempt_disable();
305 	pid = mm->context.id;
306 	if (unlikely(pid == MMU_NO_CONTEXT))
307 		goto bail;
308 	cpu_mask = mm_cpumask(mm);
309 	if (!mm_is_core_local(mm)) {
310 		/* If broadcast tlbivax is supported, use it */
311 		if (mmu_has_feature(MMU_FTR_USE_TLBIVAX_BCAST)) {
312 			int lock = mmu_has_feature(MMU_FTR_LOCK_BCAST_INVAL);
313 			if (lock)
314 				raw_spin_lock(&tlbivax_lock);
315 			_tlbivax_bcast(vmaddr, pid, tsize, ind);
316 			if (lock)
317 				raw_spin_unlock(&tlbivax_lock);
318 			goto bail;
319 		} else {
320 			struct tlb_flush_param p = {
321 				.pid = pid,
322 				.addr = vmaddr,
323 				.tsize = tsize,
324 				.ind = ind,
325 			};
326 			/* Ignores smp_processor_id() even if set in cpu_mask */
327 			smp_call_function_many(cpu_mask,
328 					       do_flush_tlb_page_ipi, &p, 1);
329 		}
330 	}
331 	_tlbil_va(vmaddr, pid, tsize, ind);
332  bail:
333 	preempt_enable();
334 }
335 
336 void flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
337 {
338 #ifdef CONFIG_HUGETLB_PAGE
339 	if (vma && is_vm_hugetlb_page(vma))
340 		flush_hugetlb_page(vma, vmaddr);
341 #endif
342 
343 	__flush_tlb_page(vma ? vma->vm_mm : NULL, vmaddr,
344 			 mmu_get_tsize(mmu_virtual_psize), 0);
345 }
346 EXPORT_SYMBOL(flush_tlb_page);
347 
348 #endif /* CONFIG_SMP */
349 
350 #ifdef CONFIG_PPC_47x
351 void __init early_init_mmu_47x(void)
352 {
353 #ifdef CONFIG_SMP
354 	unsigned long root = of_get_flat_dt_root();
355 	if (of_get_flat_dt_prop(root, "cooperative-partition", NULL))
356 		mmu_clear_feature(MMU_FTR_USE_TLBIVAX_BCAST);
357 #endif /* CONFIG_SMP */
358 }
359 #endif /* CONFIG_PPC_47x */
360 
361 /*
362  * Flush kernel TLB entries in the given range
363  */
364 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
365 {
366 #ifdef CONFIG_SMP
367 	preempt_disable();
368 	smp_call_function(do_flush_tlb_mm_ipi, NULL, 1);
369 	_tlbil_pid(0);
370 	preempt_enable();
371 #else
372 	_tlbil_pid(0);
373 #endif
374 }
375 EXPORT_SYMBOL(flush_tlb_kernel_range);
376 
377 /*
378  * Currently, for range flushing, we just do a full mm flush. This should
379  * be optimized based on a threshold on the size of the range, since
380  * some implementation can stack multiple tlbivax before a tlbsync but
381  * for now, we keep it that way
382  */
383 void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
384 		     unsigned long end)
385 
386 {
387 	if (end - start == PAGE_SIZE && !(start & ~PAGE_MASK))
388 		flush_tlb_page(vma, start);
389 	else
390 		flush_tlb_mm(vma->vm_mm);
391 }
392 EXPORT_SYMBOL(flush_tlb_range);
393 
394 void tlb_flush(struct mmu_gather *tlb)
395 {
396 	flush_tlb_mm(tlb->mm);
397 }
398 
399 /*
400  * Below are functions specific to the 64-bit variant of Book3E though that
401  * may change in the future
402  */
403 
404 #ifdef CONFIG_PPC64
405 
406 /*
407  * Handling of virtual linear page tables or indirect TLB entries
408  * flushing when PTE pages are freed
409  */
410 void tlb_flush_pgtable(struct mmu_gather *tlb, unsigned long address)
411 {
412 	int tsize = mmu_psize_defs[mmu_pte_psize].enc;
413 
414 	if (book3e_htw_mode != PPC_HTW_NONE) {
415 		unsigned long start = address & PMD_MASK;
416 		unsigned long end = address + PMD_SIZE;
417 		unsigned long size = 1UL << mmu_psize_defs[mmu_pte_psize].shift;
418 
419 		/* This isn't the most optimal, ideally we would factor out the
420 		 * while preempt & CPU mask mucking around, or even the IPI but
421 		 * it will do for now
422 		 */
423 		while (start < end) {
424 			__flush_tlb_page(tlb->mm, start, tsize, 1);
425 			start += size;
426 		}
427 	} else {
428 		unsigned long rmask = 0xf000000000000000ul;
429 		unsigned long rid = (address & rmask) | 0x1000000000000000ul;
430 		unsigned long vpte = address & ~rmask;
431 
432 		vpte = (vpte >> (PAGE_SHIFT - 3)) & ~0xffful;
433 		vpte |= rid;
434 		__flush_tlb_page(tlb->mm, vpte, tsize, 0);
435 	}
436 }
437 
438 static void setup_page_sizes(void)
439 {
440 	unsigned int tlb0cfg;
441 	unsigned int tlb0ps;
442 	unsigned int eptcfg;
443 	int i, psize;
444 
445 #ifdef CONFIG_PPC_FSL_BOOK3E
446 	unsigned int mmucfg = mfspr(SPRN_MMUCFG);
447 	int fsl_mmu = mmu_has_feature(MMU_FTR_TYPE_FSL_E);
448 
449 	if (fsl_mmu && (mmucfg & MMUCFG_MAVN) == MMUCFG_MAVN_V1) {
450 		unsigned int tlb1cfg = mfspr(SPRN_TLB1CFG);
451 		unsigned int min_pg, max_pg;
452 
453 		min_pg = (tlb1cfg & TLBnCFG_MINSIZE) >> TLBnCFG_MINSIZE_SHIFT;
454 		max_pg = (tlb1cfg & TLBnCFG_MAXSIZE) >> TLBnCFG_MAXSIZE_SHIFT;
455 
456 		for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
457 			struct mmu_psize_def *def;
458 			unsigned int shift;
459 
460 			def = &mmu_psize_defs[psize];
461 			shift = def->shift;
462 
463 			if (shift == 0 || shift & 1)
464 				continue;
465 
466 			/* adjust to be in terms of 4^shift Kb */
467 			shift = (shift - 10) >> 1;
468 
469 			if ((shift >= min_pg) && (shift <= max_pg))
470 				def->flags |= MMU_PAGE_SIZE_DIRECT;
471 		}
472 
473 		goto out;
474 	}
475 
476 	if (fsl_mmu && (mmucfg & MMUCFG_MAVN) == MMUCFG_MAVN_V2) {
477 		u32 tlb1cfg, tlb1ps;
478 
479 		tlb0cfg = mfspr(SPRN_TLB0CFG);
480 		tlb1cfg = mfspr(SPRN_TLB1CFG);
481 		tlb1ps = mfspr(SPRN_TLB1PS);
482 		eptcfg = mfspr(SPRN_EPTCFG);
483 
484 		if ((tlb1cfg & TLBnCFG_IND) && (tlb0cfg & TLBnCFG_PT))
485 			book3e_htw_mode = PPC_HTW_E6500;
486 
487 		/*
488 		 * We expect 4K subpage size and unrestricted indirect size.
489 		 * The lack of a restriction on indirect size is a Freescale
490 		 * extension, indicated by PSn = 0 but SPSn != 0.
491 		 */
492 		if (eptcfg != 2)
493 			book3e_htw_mode = PPC_HTW_NONE;
494 
495 		for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
496 			struct mmu_psize_def *def = &mmu_psize_defs[psize];
497 
498 			if (!def->shift)
499 				continue;
500 
501 			if (tlb1ps & (1U << (def->shift - 10))) {
502 				def->flags |= MMU_PAGE_SIZE_DIRECT;
503 
504 				if (book3e_htw_mode && psize == MMU_PAGE_2M)
505 					def->flags |= MMU_PAGE_SIZE_INDIRECT;
506 			}
507 		}
508 
509 		goto out;
510 	}
511 #endif
512 
513 	tlb0cfg = mfspr(SPRN_TLB0CFG);
514 	tlb0ps = mfspr(SPRN_TLB0PS);
515 	eptcfg = mfspr(SPRN_EPTCFG);
516 
517 	/* Look for supported direct sizes */
518 	for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
519 		struct mmu_psize_def *def = &mmu_psize_defs[psize];
520 
521 		if (tlb0ps & (1U << (def->shift - 10)))
522 			def->flags |= MMU_PAGE_SIZE_DIRECT;
523 	}
524 
525 	/* Indirect page sizes supported ? */
526 	if ((tlb0cfg & TLBnCFG_IND) == 0 ||
527 	    (tlb0cfg & TLBnCFG_PT) == 0)
528 		goto out;
529 
530 	book3e_htw_mode = PPC_HTW_IBM;
531 
532 	/* Now, we only deal with one IND page size for each
533 	 * direct size. Hopefully all implementations today are
534 	 * unambiguous, but we might want to be careful in the
535 	 * future.
536 	 */
537 	for (i = 0; i < 3; i++) {
538 		unsigned int ps, sps;
539 
540 		sps = eptcfg & 0x1f;
541 		eptcfg >>= 5;
542 		ps = eptcfg & 0x1f;
543 		eptcfg >>= 5;
544 		if (!ps || !sps)
545 			continue;
546 		for (psize = 0; psize < MMU_PAGE_COUNT; psize++) {
547 			struct mmu_psize_def *def = &mmu_psize_defs[psize];
548 
549 			if (ps == (def->shift - 10))
550 				def->flags |= MMU_PAGE_SIZE_INDIRECT;
551 			if (sps == (def->shift - 10))
552 				def->ind = ps + 10;
553 		}
554 	}
555 
556 out:
557 	/* Cleanup array and print summary */
558 	pr_info("MMU: Supported page sizes\n");
559 	for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
560 		struct mmu_psize_def *def = &mmu_psize_defs[psize];
561 		const char *__page_type_names[] = {
562 			"unsupported",
563 			"direct",
564 			"indirect",
565 			"direct & indirect"
566 		};
567 		if (def->flags == 0) {
568 			def->shift = 0;
569 			continue;
570 		}
571 		pr_info("  %8ld KB as %s\n", 1ul << (def->shift - 10),
572 			__page_type_names[def->flags & 0x3]);
573 	}
574 }
575 
576 static void setup_mmu_htw(void)
577 {
578 	/*
579 	 * If we want to use HW tablewalk, enable it by patching the TLB miss
580 	 * handlers to branch to the one dedicated to it.
581 	 */
582 
583 	switch (book3e_htw_mode) {
584 	case PPC_HTW_IBM:
585 		patch_exception(0x1c0, exc_data_tlb_miss_htw_book3e);
586 		patch_exception(0x1e0, exc_instruction_tlb_miss_htw_book3e);
587 		break;
588 #ifdef CONFIG_PPC_FSL_BOOK3E
589 	case PPC_HTW_E6500:
590 		extlb_level_exc = EX_TLB_SIZE;
591 		patch_exception(0x1c0, exc_data_tlb_miss_e6500_book3e);
592 		patch_exception(0x1e0, exc_instruction_tlb_miss_e6500_book3e);
593 		break;
594 #endif
595 	}
596 	pr_info("MMU: Book3E HW tablewalk %s\n",
597 		book3e_htw_mode != PPC_HTW_NONE ? "enabled" : "not supported");
598 }
599 
600 /*
601  * Early initialization of the MMU TLB code
602  */
603 static void early_init_this_mmu(void)
604 {
605 	unsigned int mas4;
606 
607 	/* Set MAS4 based on page table setting */
608 
609 	mas4 = 0x4 << MAS4_WIMGED_SHIFT;
610 	switch (book3e_htw_mode) {
611 	case PPC_HTW_E6500:
612 		mas4 |= MAS4_INDD;
613 		mas4 |= BOOK3E_PAGESZ_2M << MAS4_TSIZED_SHIFT;
614 		mas4 |= MAS4_TLBSELD(1);
615 		mmu_pte_psize = MMU_PAGE_2M;
616 		break;
617 
618 	case PPC_HTW_IBM:
619 		mas4 |= MAS4_INDD;
620 		mas4 |=	BOOK3E_PAGESZ_1M << MAS4_TSIZED_SHIFT;
621 		mmu_pte_psize = MMU_PAGE_1M;
622 		break;
623 
624 	case PPC_HTW_NONE:
625 		mas4 |=	BOOK3E_PAGESZ_4K << MAS4_TSIZED_SHIFT;
626 		mmu_pte_psize = mmu_virtual_psize;
627 		break;
628 	}
629 	mtspr(SPRN_MAS4, mas4);
630 
631 #ifdef CONFIG_PPC_FSL_BOOK3E
632 	if (mmu_has_feature(MMU_FTR_TYPE_FSL_E)) {
633 		unsigned int num_cams;
634 		bool map = true;
635 
636 		/* use a quarter of the TLBCAM for bolted linear map */
637 		num_cams = (mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY) / 4;
638 
639 		/*
640 		 * Only do the mapping once per core, or else the
641 		 * transient mapping would cause problems.
642 		 */
643 #ifdef CONFIG_SMP
644 		if (hweight32(get_tensr()) > 1)
645 			map = false;
646 #endif
647 
648 		if (map)
649 			linear_map_top = map_mem_in_cams(linear_map_top,
650 							 num_cams, false);
651 	}
652 #endif
653 
654 	/* A sync won't hurt us after mucking around with
655 	 * the MMU configuration
656 	 */
657 	mb();
658 }
659 
660 static void __init early_init_mmu_global(void)
661 {
662 	/* XXX This will have to be decided at runtime, but right
663 	 * now our boot and TLB miss code hard wires it. Ideally
664 	 * we should find out a suitable page size and patch the
665 	 * TLB miss code (either that or use the PACA to store
666 	 * the value we want)
667 	 */
668 	mmu_linear_psize = MMU_PAGE_1G;
669 
670 	/* XXX This should be decided at runtime based on supported
671 	 * page sizes in the TLB, but for now let's assume 16M is
672 	 * always there and a good fit (which it probably is)
673 	 *
674 	 * Freescale booke only supports 4K pages in TLB0, so use that.
675 	 */
676 	if (mmu_has_feature(MMU_FTR_TYPE_FSL_E))
677 		mmu_vmemmap_psize = MMU_PAGE_4K;
678 	else
679 		mmu_vmemmap_psize = MMU_PAGE_16M;
680 
681 	/* XXX This code only checks for TLB 0 capabilities and doesn't
682 	 *     check what page size combos are supported by the HW. It
683 	 *     also doesn't handle the case where a separate array holds
684 	 *     the IND entries from the array loaded by the PT.
685 	 */
686 	/* Look for supported page sizes */
687 	setup_page_sizes();
688 
689 	/* Look for HW tablewalk support */
690 	setup_mmu_htw();
691 
692 #ifdef CONFIG_PPC_FSL_BOOK3E
693 	if (mmu_has_feature(MMU_FTR_TYPE_FSL_E)) {
694 		if (book3e_htw_mode == PPC_HTW_NONE) {
695 			extlb_level_exc = EX_TLB_SIZE;
696 			patch_exception(0x1c0, exc_data_tlb_miss_bolted_book3e);
697 			patch_exception(0x1e0,
698 				exc_instruction_tlb_miss_bolted_book3e);
699 		}
700 	}
701 #endif
702 
703 	/* Set the global containing the top of the linear mapping
704 	 * for use by the TLB miss code
705 	 */
706 	linear_map_top = memblock_end_of_DRAM();
707 
708 	ioremap_bot = IOREMAP_BASE;
709 }
710 
711 static void __init early_mmu_set_memory_limit(void)
712 {
713 #ifdef CONFIG_PPC_FSL_BOOK3E
714 	if (mmu_has_feature(MMU_FTR_TYPE_FSL_E)) {
715 		/*
716 		 * Limit memory so we dont have linear faults.
717 		 * Unlike memblock_set_current_limit, which limits
718 		 * memory available during early boot, this permanently
719 		 * reduces the memory available to Linux.  We need to
720 		 * do this because highmem is not supported on 64-bit.
721 		 */
722 		memblock_enforce_memory_limit(linear_map_top);
723 	}
724 #endif
725 
726 	memblock_set_current_limit(linear_map_top);
727 }
728 
729 /* boot cpu only */
730 void __init early_init_mmu(void)
731 {
732 	early_init_mmu_global();
733 	early_init_this_mmu();
734 	early_mmu_set_memory_limit();
735 }
736 
737 void early_init_mmu_secondary(void)
738 {
739 	early_init_this_mmu();
740 }
741 
742 void setup_initial_memory_limit(phys_addr_t first_memblock_base,
743 				phys_addr_t first_memblock_size)
744 {
745 	/* On non-FSL Embedded 64-bit, we adjust the RMA size to match
746 	 * the bolted TLB entry. We know for now that only 1G
747 	 * entries are supported though that may eventually
748 	 * change.
749 	 *
750 	 * on FSL Embedded 64-bit, usually all RAM is bolted, but with
751 	 * unusual memory sizes it's possible for some RAM to not be mapped
752 	 * (such RAM is not used at all by Linux, since we don't support
753 	 * highmem on 64-bit).  We limit ppc64_rma_size to what would be
754 	 * mappable if this memblock is the only one.  Additional memblocks
755 	 * can only increase, not decrease, the amount that ends up getting
756 	 * mapped.  We still limit max to 1G even if we'll eventually map
757 	 * more.  This is due to what the early init code is set up to do.
758 	 *
759 	 * We crop it to the size of the first MEMBLOCK to
760 	 * avoid going over total available memory just in case...
761 	 */
762 #ifdef CONFIG_PPC_FSL_BOOK3E
763 	if (early_mmu_has_feature(MMU_FTR_TYPE_FSL_E)) {
764 		unsigned long linear_sz;
765 		unsigned int num_cams;
766 
767 		/* use a quarter of the TLBCAM for bolted linear map */
768 		num_cams = (mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY) / 4;
769 
770 		linear_sz = map_mem_in_cams(first_memblock_size, num_cams,
771 					    true);
772 
773 		ppc64_rma_size = min_t(u64, linear_sz, 0x40000000);
774 	} else
775 #endif
776 		ppc64_rma_size = min_t(u64, first_memblock_size, 0x40000000);
777 
778 	/* Finally limit subsequent allocations */
779 	memblock_set_current_limit(first_memblock_base + ppc64_rma_size);
780 }
781 #else /* ! CONFIG_PPC64 */
782 void __init early_init_mmu(void)
783 {
784 #ifdef CONFIG_PPC_47x
785 	early_init_mmu_47x();
786 #endif
787 
788 #ifdef CONFIG_PPC_MM_SLICES
789 	mm_ctx_set_slb_addr_limit(&init_mm.context, SLB_ADDR_LIMIT_DEFAULT);
790 #endif
791 }
792 #endif /* CONFIG_PPC64 */
793