tlb_nohash.c 20 KB

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