tlb_nohash.c 20 KB

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