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