tlb_nohash.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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/mm.h>
  31. #include <linux/init.h>
  32. #include <linux/highmem.h>
  33. #include <linux/pagemap.h>
  34. #include <linux/preempt.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/memblock.h>
  37. #include <asm/tlbflush.h>
  38. #include <asm/tlb.h>
  39. #include <asm/code-patching.h>
  40. #include "mmu_decl.h"
  41. #ifdef CONFIG_PPC_BOOK3E
  42. struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT] = {
  43. [MMU_PAGE_4K] = {
  44. .shift = 12,
  45. .ind = 20,
  46. .enc = BOOK3E_PAGESZ_4K,
  47. },
  48. [MMU_PAGE_16K] = {
  49. .shift = 14,
  50. .enc = BOOK3E_PAGESZ_16K,
  51. },
  52. [MMU_PAGE_64K] = {
  53. .shift = 16,
  54. .ind = 28,
  55. .enc = BOOK3E_PAGESZ_64K,
  56. },
  57. [MMU_PAGE_1M] = {
  58. .shift = 20,
  59. .enc = BOOK3E_PAGESZ_1M,
  60. },
  61. [MMU_PAGE_16M] = {
  62. .shift = 24,
  63. .ind = 36,
  64. .enc = BOOK3E_PAGESZ_16M,
  65. },
  66. [MMU_PAGE_256M] = {
  67. .shift = 28,
  68. .enc = BOOK3E_PAGESZ_256M,
  69. },
  70. [MMU_PAGE_1G] = {
  71. .shift = 30,
  72. .enc = BOOK3E_PAGESZ_1GB,
  73. },
  74. };
  75. static inline int mmu_get_tsize(int psize)
  76. {
  77. return mmu_psize_defs[psize].enc;
  78. }
  79. #else
  80. static inline int mmu_get_tsize(int psize)
  81. {
  82. /* This isn't used on !Book3E for now */
  83. return 0;
  84. }
  85. #endif
  86. /* The variables below are currently only used on 64-bit Book3E
  87. * though this will probably be made common with other nohash
  88. * implementations at some point
  89. */
  90. #ifdef CONFIG_PPC64
  91. int mmu_linear_psize; /* Page size used for the linear mapping */
  92. int mmu_pte_psize; /* Page size used for PTE pages */
  93. int mmu_vmemmap_psize; /* Page size used for the virtual mem map */
  94. int book3e_htw_enabled; /* Is HW tablewalk enabled ? */
  95. unsigned long linear_map_top; /* Top of linear mapping */
  96. #endif /* CONFIG_PPC64 */
  97. /*
  98. * Base TLB flushing operations:
  99. *
  100. * - flush_tlb_mm(mm) flushes the specified mm context TLB's
  101. * - flush_tlb_page(vma, vmaddr) flushes one page
  102. * - flush_tlb_range(vma, start, end) flushes a range of pages
  103. * - flush_tlb_kernel_range(start, end) flushes kernel pages
  104. *
  105. * - local_* variants of page and mm only apply to the current
  106. * processor
  107. */
  108. /*
  109. * These are the base non-SMP variants of page and mm flushing
  110. */
  111. void local_flush_tlb_mm(struct mm_struct *mm)
  112. {
  113. unsigned int pid;
  114. preempt_disable();
  115. pid = mm->context.id;
  116. if (pid != MMU_NO_CONTEXT)
  117. _tlbil_pid(pid);
  118. preempt_enable();
  119. }
  120. EXPORT_SYMBOL(local_flush_tlb_mm);
  121. void __local_flush_tlb_page(struct mm_struct *mm, unsigned long vmaddr,
  122. int tsize, int ind)
  123. {
  124. unsigned int pid;
  125. preempt_disable();
  126. pid = mm ? mm->context.id : 0;
  127. if (pid != MMU_NO_CONTEXT)
  128. _tlbil_va(vmaddr, pid, tsize, ind);
  129. preempt_enable();
  130. }
  131. void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
  132. {
  133. __local_flush_tlb_page(vma ? vma->vm_mm : NULL, vmaddr,
  134. mmu_get_tsize(mmu_virtual_psize), 0);
  135. }
  136. EXPORT_SYMBOL(local_flush_tlb_page);
  137. /*
  138. * And here are the SMP non-local implementations
  139. */
  140. #ifdef CONFIG_SMP
  141. static DEFINE_RAW_SPINLOCK(tlbivax_lock);
  142. static int mm_is_core_local(struct mm_struct *mm)
  143. {
  144. return cpumask_subset(mm_cpumask(mm),
  145. topology_thread_cpumask(smp_processor_id()));
  146. }
  147. struct tlb_flush_param {
  148. unsigned long addr;
  149. unsigned int pid;
  150. unsigned int tsize;
  151. unsigned int ind;
  152. };
  153. static void do_flush_tlb_mm_ipi(void *param)
  154. {
  155. struct tlb_flush_param *p = param;
  156. _tlbil_pid(p ? p->pid : 0);
  157. }
  158. static void do_flush_tlb_page_ipi(void *param)
  159. {
  160. struct tlb_flush_param *p = param;
  161. _tlbil_va(p->addr, p->pid, p->tsize, p->ind);
  162. }
  163. /* Note on invalidations and PID:
  164. *
  165. * We snapshot the PID with preempt disabled. At this point, it can still
  166. * change either because:
  167. * - our context is being stolen (PID -> NO_CONTEXT) on another CPU
  168. * - we are invaliating some target that isn't currently running here
  169. * and is concurrently acquiring a new PID on another CPU
  170. * - some other CPU is re-acquiring a lost PID for this mm
  171. * etc...
  172. *
  173. * However, this shouldn't be a problem as we only guarantee
  174. * invalidation of TLB entries present prior to this call, so we
  175. * don't care about the PID changing, and invalidating a stale PID
  176. * is generally harmless.
  177. */
  178. void flush_tlb_mm(struct mm_struct *mm)
  179. {
  180. unsigned int pid;
  181. preempt_disable();
  182. pid = mm->context.id;
  183. if (unlikely(pid == MMU_NO_CONTEXT))
  184. goto no_context;
  185. if (!mm_is_core_local(mm)) {
  186. struct tlb_flush_param p = { .pid = pid };
  187. /* Ignores smp_processor_id() even if set. */
  188. smp_call_function_many(mm_cpumask(mm),
  189. do_flush_tlb_mm_ipi, &p, 1);
  190. }
  191. _tlbil_pid(pid);
  192. no_context:
  193. preempt_enable();
  194. }
  195. EXPORT_SYMBOL(flush_tlb_mm);
  196. void __flush_tlb_page(struct mm_struct *mm, unsigned long vmaddr,
  197. int tsize, int ind)
  198. {
  199. struct cpumask *cpu_mask;
  200. unsigned int pid;
  201. preempt_disable();
  202. pid = mm ? mm->context.id : 0;
  203. if (unlikely(pid == MMU_NO_CONTEXT))
  204. goto bail;
  205. cpu_mask = mm_cpumask(mm);
  206. if (!mm_is_core_local(mm)) {
  207. /* If broadcast tlbivax is supported, use it */
  208. if (mmu_has_feature(MMU_FTR_USE_TLBIVAX_BCAST)) {
  209. int lock = mmu_has_feature(MMU_FTR_LOCK_BCAST_INVAL);
  210. if (lock)
  211. raw_spin_lock(&tlbivax_lock);
  212. _tlbivax_bcast(vmaddr, pid, tsize, ind);
  213. if (lock)
  214. raw_spin_unlock(&tlbivax_lock);
  215. goto bail;
  216. } else {
  217. struct tlb_flush_param p = {
  218. .pid = pid,
  219. .addr = vmaddr,
  220. .tsize = tsize,
  221. .ind = ind,
  222. };
  223. /* Ignores smp_processor_id() even if set in cpu_mask */
  224. smp_call_function_many(cpu_mask,
  225. do_flush_tlb_page_ipi, &p, 1);
  226. }
  227. }
  228. _tlbil_va(vmaddr, pid, tsize, ind);
  229. bail:
  230. preempt_enable();
  231. }
  232. void flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
  233. {
  234. __flush_tlb_page(vma ? vma->vm_mm : NULL, vmaddr,
  235. mmu_get_tsize(mmu_virtual_psize), 0);
  236. }
  237. EXPORT_SYMBOL(flush_tlb_page);
  238. #endif /* CONFIG_SMP */
  239. /*
  240. * Flush kernel TLB entries in the given range
  241. */
  242. void flush_tlb_kernel_range(unsigned long start, unsigned long end)
  243. {
  244. #ifdef CONFIG_SMP
  245. preempt_disable();
  246. smp_call_function(do_flush_tlb_mm_ipi, NULL, 1);
  247. _tlbil_pid(0);
  248. preempt_enable();
  249. #else
  250. _tlbil_pid(0);
  251. #endif
  252. }
  253. EXPORT_SYMBOL(flush_tlb_kernel_range);
  254. /*
  255. * Currently, for range flushing, we just do a full mm flush. This should
  256. * be optimized based on a threshold on the size of the range, since
  257. * some implementation can stack multiple tlbivax before a tlbsync but
  258. * for now, we keep it that way
  259. */
  260. void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
  261. unsigned long end)
  262. {
  263. flush_tlb_mm(vma->vm_mm);
  264. }
  265. EXPORT_SYMBOL(flush_tlb_range);
  266. void tlb_flush(struct mmu_gather *tlb)
  267. {
  268. flush_tlb_mm(tlb->mm);
  269. /* Push out batch of freed page tables */
  270. pte_free_finish();
  271. }
  272. /*
  273. * Below are functions specific to the 64-bit variant of Book3E though that
  274. * may change in the future
  275. */
  276. #ifdef CONFIG_PPC64
  277. /*
  278. * Handling of virtual linear page tables or indirect TLB entries
  279. * flushing when PTE pages are freed
  280. */
  281. void tlb_flush_pgtable(struct mmu_gather *tlb, unsigned long address)
  282. {
  283. int tsize = mmu_psize_defs[mmu_pte_psize].enc;
  284. if (book3e_htw_enabled) {
  285. unsigned long start = address & PMD_MASK;
  286. unsigned long end = address + PMD_SIZE;
  287. unsigned long size = 1UL << mmu_psize_defs[mmu_pte_psize].shift;
  288. /* This isn't the most optimal, ideally we would factor out the
  289. * while preempt & CPU mask mucking around, or even the IPI but
  290. * it will do for now
  291. */
  292. while (start < end) {
  293. __flush_tlb_page(tlb->mm, start, tsize, 1);
  294. start += size;
  295. }
  296. } else {
  297. unsigned long rmask = 0xf000000000000000ul;
  298. unsigned long rid = (address & rmask) | 0x1000000000000000ul;
  299. unsigned long vpte = address & ~rmask;
  300. #ifdef CONFIG_PPC_64K_PAGES
  301. vpte = (vpte >> (PAGE_SHIFT - 4)) & ~0xfffful;
  302. #else
  303. vpte = (vpte >> (PAGE_SHIFT - 3)) & ~0xffful;
  304. #endif
  305. vpte |= rid;
  306. __flush_tlb_page(tlb->mm, vpte, tsize, 0);
  307. }
  308. }
  309. static void setup_page_sizes(void)
  310. {
  311. unsigned int tlb0cfg;
  312. unsigned int tlb0ps;
  313. unsigned int eptcfg;
  314. int i, psize;
  315. #ifdef CONFIG_PPC_FSL_BOOK3E
  316. unsigned int mmucfg = mfspr(SPRN_MMUCFG);
  317. if (((mmucfg & MMUCFG_MAVN) == MMUCFG_MAVN_V1) &&
  318. (mmu_has_feature(MMU_FTR_TYPE_FSL_E))) {
  319. unsigned int tlb1cfg = mfspr(SPRN_TLB1CFG);
  320. unsigned int min_pg, max_pg;
  321. min_pg = (tlb1cfg & TLBnCFG_MINSIZE) >> TLBnCFG_MINSIZE_SHIFT;
  322. max_pg = (tlb1cfg & TLBnCFG_MAXSIZE) >> TLBnCFG_MAXSIZE_SHIFT;
  323. for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
  324. struct mmu_psize_def *def;
  325. unsigned int shift;
  326. def = &mmu_psize_defs[psize];
  327. shift = def->shift;
  328. if (shift == 0)
  329. continue;
  330. /* adjust to be in terms of 4^shift Kb */
  331. shift = (shift - 10) >> 1;
  332. if ((shift >= min_pg) && (shift <= max_pg))
  333. def->flags |= MMU_PAGE_SIZE_DIRECT;
  334. }
  335. goto no_indirect;
  336. }
  337. #endif
  338. tlb0cfg = mfspr(SPRN_TLB0CFG);
  339. tlb0ps = mfspr(SPRN_TLB0PS);
  340. eptcfg = mfspr(SPRN_EPTCFG);
  341. /* Look for supported direct sizes */
  342. for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
  343. struct mmu_psize_def *def = &mmu_psize_defs[psize];
  344. if (tlb0ps & (1U << (def->shift - 10)))
  345. def->flags |= MMU_PAGE_SIZE_DIRECT;
  346. }
  347. /* Indirect page sizes supported ? */
  348. if ((tlb0cfg & TLBnCFG_IND) == 0)
  349. goto no_indirect;
  350. /* Now, we only deal with one IND page size for each
  351. * direct size. Hopefully all implementations today are
  352. * unambiguous, but we might want to be careful in the
  353. * future.
  354. */
  355. for (i = 0; i < 3; i++) {
  356. unsigned int ps, sps;
  357. sps = eptcfg & 0x1f;
  358. eptcfg >>= 5;
  359. ps = eptcfg & 0x1f;
  360. eptcfg >>= 5;
  361. if (!ps || !sps)
  362. continue;
  363. for (psize = 0; psize < MMU_PAGE_COUNT; psize++) {
  364. struct mmu_psize_def *def = &mmu_psize_defs[psize];
  365. if (ps == (def->shift - 10))
  366. def->flags |= MMU_PAGE_SIZE_INDIRECT;
  367. if (sps == (def->shift - 10))
  368. def->ind = ps + 10;
  369. }
  370. }
  371. no_indirect:
  372. /* Cleanup array and print summary */
  373. pr_info("MMU: Supported page sizes\n");
  374. for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
  375. struct mmu_psize_def *def = &mmu_psize_defs[psize];
  376. const char *__page_type_names[] = {
  377. "unsupported",
  378. "direct",
  379. "indirect",
  380. "direct & indirect"
  381. };
  382. if (def->flags == 0) {
  383. def->shift = 0;
  384. continue;
  385. }
  386. pr_info(" %8ld KB as %s\n", 1ul << (def->shift - 10),
  387. __page_type_names[def->flags & 0x3]);
  388. }
  389. }
  390. static void setup_mmu_htw(void)
  391. {
  392. extern unsigned int interrupt_base_book3e;
  393. extern unsigned int exc_data_tlb_miss_htw_book3e;
  394. extern unsigned int exc_instruction_tlb_miss_htw_book3e;
  395. unsigned int *ibase = &interrupt_base_book3e;
  396. /* Check if HW tablewalk is present, and if yes, enable it by:
  397. *
  398. * - patching the TLB miss handlers to branch to the
  399. * one dedicates to it
  400. *
  401. * - setting the global book3e_htw_enabled
  402. */
  403. unsigned int tlb0cfg = mfspr(SPRN_TLB0CFG);
  404. if ((tlb0cfg & TLBnCFG_IND) &&
  405. (tlb0cfg & TLBnCFG_PT)) {
  406. /* Our exceptions vectors start with a NOP and -then- a branch
  407. * to deal with single stepping from userspace which stops on
  408. * the second instruction. Thus we need to patch the second
  409. * instruction of the exception, not the first one
  410. */
  411. patch_branch(ibase + (0x1c0 / 4) + 1,
  412. (unsigned long)&exc_data_tlb_miss_htw_book3e, 0);
  413. patch_branch(ibase + (0x1e0 / 4) + 1,
  414. (unsigned long)&exc_instruction_tlb_miss_htw_book3e, 0);
  415. book3e_htw_enabled = 1;
  416. }
  417. pr_info("MMU: Book3E Page Tables %s\n",
  418. book3e_htw_enabled ? "Enabled" : "Disabled");
  419. }
  420. /*
  421. * Early initialization of the MMU TLB code
  422. */
  423. static void __early_init_mmu(int boot_cpu)
  424. {
  425. unsigned int mas4;
  426. /* XXX This will have to be decided at runtime, but right
  427. * now our boot and TLB miss code hard wires it. Ideally
  428. * we should find out a suitable page size and patch the
  429. * TLB miss code (either that or use the PACA to store
  430. * the value we want)
  431. */
  432. mmu_linear_psize = MMU_PAGE_1G;
  433. /* XXX This should be decided at runtime based on supported
  434. * page sizes in the TLB, but for now let's assume 16M is
  435. * always there and a good fit (which it probably is)
  436. */
  437. mmu_vmemmap_psize = MMU_PAGE_16M;
  438. /* XXX This code only checks for TLB 0 capabilities and doesn't
  439. * check what page size combos are supported by the HW. It
  440. * also doesn't handle the case where a separate array holds
  441. * the IND entries from the array loaded by the PT.
  442. */
  443. if (boot_cpu) {
  444. /* Look for supported page sizes */
  445. setup_page_sizes();
  446. /* Look for HW tablewalk support */
  447. setup_mmu_htw();
  448. }
  449. /* Set MAS4 based on page table setting */
  450. mas4 = 0x4 << MAS4_WIMGED_SHIFT;
  451. if (book3e_htw_enabled) {
  452. mas4 |= mas4 | MAS4_INDD;
  453. #ifdef CONFIG_PPC_64K_PAGES
  454. mas4 |= BOOK3E_PAGESZ_256M << MAS4_TSIZED_SHIFT;
  455. mmu_pte_psize = MMU_PAGE_256M;
  456. #else
  457. mas4 |= BOOK3E_PAGESZ_1M << MAS4_TSIZED_SHIFT;
  458. mmu_pte_psize = MMU_PAGE_1M;
  459. #endif
  460. } else {
  461. #ifdef CONFIG_PPC_64K_PAGES
  462. mas4 |= BOOK3E_PAGESZ_64K << MAS4_TSIZED_SHIFT;
  463. #else
  464. mas4 |= BOOK3E_PAGESZ_4K << MAS4_TSIZED_SHIFT;
  465. #endif
  466. mmu_pte_psize = mmu_virtual_psize;
  467. }
  468. mtspr(SPRN_MAS4, mas4);
  469. /* Set the global containing the top of the linear mapping
  470. * for use by the TLB miss code
  471. */
  472. linear_map_top = memblock_end_of_DRAM();
  473. #ifdef CONFIG_PPC_FSL_BOOK3E
  474. if (mmu_has_feature(MMU_FTR_TYPE_FSL_E)) {
  475. unsigned int num_cams;
  476. /* use a quarter of the TLBCAM for bolted linear map */
  477. num_cams = (mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY) / 4;
  478. linear_map_top = map_mem_in_cams(linear_map_top, num_cams);
  479. /* limit memory so we dont have linear faults */
  480. memblock_enforce_memory_limit(linear_map_top);
  481. memblock_analyze();
  482. }
  483. #endif
  484. /* A sync won't hurt us after mucking around with
  485. * the MMU configuration
  486. */
  487. mb();
  488. memblock_set_current_limit(linear_map_top);
  489. }
  490. void __init early_init_mmu(void)
  491. {
  492. __early_init_mmu(1);
  493. }
  494. void __cpuinit early_init_mmu_secondary(void)
  495. {
  496. __early_init_mmu(0);
  497. }
  498. void setup_initial_memory_limit(phys_addr_t first_memblock_base,
  499. phys_addr_t first_memblock_size)
  500. {
  501. /* On Embedded 64-bit, we adjust the RMA size to match
  502. * the bolted TLB entry. We know for now that only 1G
  503. * entries are supported though that may eventually
  504. * change. We crop it to the size of the first MEMBLOCK to
  505. * avoid going over total available memory just in case...
  506. */
  507. ppc64_rma_size = min_t(u64, first_memblock_size, 0x40000000);
  508. /* Finally limit subsequent allocations */
  509. memblock_set_current_limit(first_memblock_base + ppc64_rma_size);
  510. }
  511. #endif /* CONFIG_PPC64 */