tlb.c 18 KB

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