cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

tlb.c (19541B)


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