tlb_nohash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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/lmb.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. .enc = BOOK3E_PAGESZ_4K,
  46. },
  47. [MMU_PAGE_16K] = {
  48. .shift = 14,
  49. .enc = BOOK3E_PAGESZ_16K,
  50. },
  51. [MMU_PAGE_64K] = {
  52. .shift = 16,
  53. .enc = BOOK3E_PAGESZ_64K,
  54. },
  55. [MMU_PAGE_1M] = {
  56. .shift = 20,
  57. .enc = BOOK3E_PAGESZ_1M,
  58. },
  59. [MMU_PAGE_16M] = {
  60. .shift = 24,
  61. .enc = BOOK3E_PAGESZ_16M,
  62. },
  63. [MMU_PAGE_256M] = {
  64. .shift = 28,
  65. .enc = BOOK3E_PAGESZ_256M,
  66. },
  67. [MMU_PAGE_1G] = {
  68. .shift = 30,
  69. .enc = BOOK3E_PAGESZ_1GB,
  70. },
  71. };
  72. static inline int mmu_get_tsize(int psize)
  73. {
  74. return mmu_psize_defs[psize].enc;
  75. }
  76. #else
  77. static inline int mmu_get_tsize(int psize)
  78. {
  79. /* This isn't used on !Book3E for now */
  80. return 0;
  81. }
  82. #endif
  83. /* The variables below are currently only used on 64-bit Book3E
  84. * though this will probably be made common with other nohash
  85. * implementations at some point
  86. */
  87. #ifdef CONFIG_PPC64
  88. int mmu_linear_psize; /* Page size used for the linear mapping */
  89. int mmu_pte_psize; /* Page size used for PTE pages */
  90. int mmu_vmemmap_psize; /* Page size used for the virtual mem map */
  91. int book3e_htw_enabled; /* Is HW tablewalk enabled ? */
  92. unsigned long linear_map_top; /* Top of linear mapping */
  93. #endif /* CONFIG_PPC64 */
  94. /*
  95. * Base TLB flushing operations:
  96. *
  97. * - flush_tlb_mm(mm) flushes the specified mm context TLB's
  98. * - flush_tlb_page(vma, vmaddr) flushes one page
  99. * - flush_tlb_range(vma, start, end) flushes a range of pages
  100. * - flush_tlb_kernel_range(start, end) flushes kernel pages
  101. *
  102. * - local_* variants of page and mm only apply to the current
  103. * processor
  104. */
  105. /*
  106. * These are the base non-SMP variants of page and mm flushing
  107. */
  108. void local_flush_tlb_mm(struct mm_struct *mm)
  109. {
  110. unsigned int pid;
  111. preempt_disable();
  112. pid = mm->context.id;
  113. if (pid != MMU_NO_CONTEXT)
  114. _tlbil_pid(pid);
  115. preempt_enable();
  116. }
  117. EXPORT_SYMBOL(local_flush_tlb_mm);
  118. void __local_flush_tlb_page(struct mm_struct *mm, unsigned long vmaddr,
  119. int tsize, int ind)
  120. {
  121. unsigned int pid;
  122. preempt_disable();
  123. pid = mm ? mm->context.id : 0;
  124. if (pid != MMU_NO_CONTEXT)
  125. _tlbil_va(vmaddr, pid, tsize, ind);
  126. preempt_enable();
  127. }
  128. void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
  129. {
  130. __local_flush_tlb_page(vma ? vma->vm_mm : NULL, vmaddr,
  131. mmu_get_tsize(mmu_virtual_psize), 0);
  132. }
  133. EXPORT_SYMBOL(local_flush_tlb_page);
  134. /*
  135. * And here are the SMP non-local implementations
  136. */
  137. #ifdef CONFIG_SMP
  138. static DEFINE_SPINLOCK(tlbivax_lock);
  139. static int mm_is_core_local(struct mm_struct *mm)
  140. {
  141. return cpumask_subset(mm_cpumask(mm),
  142. topology_thread_cpumask(smp_processor_id()));
  143. }
  144. struct tlb_flush_param {
  145. unsigned long addr;
  146. unsigned int pid;
  147. unsigned int tsize;
  148. unsigned int ind;
  149. };
  150. static void do_flush_tlb_mm_ipi(void *param)
  151. {
  152. struct tlb_flush_param *p = param;
  153. _tlbil_pid(p ? p->pid : 0);
  154. }
  155. static void do_flush_tlb_page_ipi(void *param)
  156. {
  157. struct tlb_flush_param *p = param;
  158. _tlbil_va(p->addr, p->pid, p->tsize, p->ind);
  159. }
  160. /* Note on invalidations and PID:
  161. *
  162. * We snapshot the PID with preempt disabled. At this point, it can still
  163. * change either because:
  164. * - our context is being stolen (PID -> NO_CONTEXT) on another CPU
  165. * - we are invaliating some target that isn't currently running here
  166. * and is concurrently acquiring a new PID on another CPU
  167. * - some other CPU is re-acquiring a lost PID for this mm
  168. * etc...
  169. *
  170. * However, this shouldn't be a problem as we only guarantee
  171. * invalidation of TLB entries present prior to this call, so we
  172. * don't care about the PID changing, and invalidating a stale PID
  173. * is generally harmless.
  174. */
  175. void flush_tlb_mm(struct mm_struct *mm)
  176. {
  177. unsigned int pid;
  178. preempt_disable();
  179. pid = mm->context.id;
  180. if (unlikely(pid == MMU_NO_CONTEXT))
  181. goto no_context;
  182. if (!mm_is_core_local(mm)) {
  183. struct tlb_flush_param p = { .pid = pid };
  184. /* Ignores smp_processor_id() even if set. */
  185. smp_call_function_many(mm_cpumask(mm),
  186. do_flush_tlb_mm_ipi, &p, 1);
  187. }
  188. _tlbil_pid(pid);
  189. no_context:
  190. preempt_enable();
  191. }
  192. EXPORT_SYMBOL(flush_tlb_mm);
  193. void __flush_tlb_page(struct mm_struct *mm, unsigned long vmaddr,
  194. int tsize, int ind)
  195. {
  196. struct cpumask *cpu_mask;
  197. unsigned int pid;
  198. preempt_disable();
  199. pid = mm ? mm->context.id : 0;
  200. if (unlikely(pid == MMU_NO_CONTEXT))
  201. goto bail;
  202. cpu_mask = mm_cpumask(mm);
  203. if (!mm_is_core_local(mm)) {
  204. /* If broadcast tlbivax is supported, use it */
  205. if (mmu_has_feature(MMU_FTR_USE_TLBIVAX_BCAST)) {
  206. int lock = mmu_has_feature(MMU_FTR_LOCK_BCAST_INVAL);
  207. if (lock)
  208. spin_lock(&tlbivax_lock);
  209. _tlbivax_bcast(vmaddr, pid, tsize, ind);
  210. if (lock)
  211. spin_unlock(&tlbivax_lock);
  212. goto bail;
  213. } else {
  214. struct tlb_flush_param p = {
  215. .pid = pid,
  216. .addr = vmaddr,
  217. .tsize = tsize,
  218. .ind = ind,
  219. };
  220. /* Ignores smp_processor_id() even if set in cpu_mask */
  221. smp_call_function_many(cpu_mask,
  222. do_flush_tlb_page_ipi, &p, 1);
  223. }
  224. }
  225. _tlbil_va(vmaddr, pid, tsize, ind);
  226. bail:
  227. preempt_enable();
  228. }
  229. void flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
  230. {
  231. __flush_tlb_page(vma ? vma->vm_mm : NULL, vmaddr,
  232. mmu_get_tsize(mmu_virtual_psize), 0);
  233. }
  234. EXPORT_SYMBOL(flush_tlb_page);
  235. #endif /* CONFIG_SMP */
  236. /*
  237. * Flush kernel TLB entries in the given range
  238. */
  239. void flush_tlb_kernel_range(unsigned long start, unsigned long end)
  240. {
  241. #ifdef CONFIG_SMP
  242. preempt_disable();
  243. smp_call_function(do_flush_tlb_mm_ipi, NULL, 1);
  244. _tlbil_pid(0);
  245. preempt_enable();
  246. #else
  247. _tlbil_pid(0);
  248. #endif
  249. }
  250. EXPORT_SYMBOL(flush_tlb_kernel_range);
  251. /*
  252. * Currently, for range flushing, we just do a full mm flush. This should
  253. * be optimized based on a threshold on the size of the range, since
  254. * some implementation can stack multiple tlbivax before a tlbsync but
  255. * for now, we keep it that way
  256. */
  257. void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
  258. unsigned long end)
  259. {
  260. flush_tlb_mm(vma->vm_mm);
  261. }
  262. EXPORT_SYMBOL(flush_tlb_range);
  263. void tlb_flush(struct mmu_gather *tlb)
  264. {
  265. flush_tlb_mm(tlb->mm);
  266. /* Push out batch of freed page tables */
  267. pte_free_finish();
  268. }
  269. /*
  270. * Below are functions specific to the 64-bit variant of Book3E though that
  271. * may change in the future
  272. */
  273. #ifdef CONFIG_PPC64
  274. /*
  275. * Handling of virtual linear page tables or indirect TLB entries
  276. * flushing when PTE pages are freed
  277. */
  278. void tlb_flush_pgtable(struct mmu_gather *tlb, unsigned long address)
  279. {
  280. int tsize = mmu_psize_defs[mmu_pte_psize].enc;
  281. if (book3e_htw_enabled) {
  282. unsigned long start = address & PMD_MASK;
  283. unsigned long end = address + PMD_SIZE;
  284. unsigned long size = 1UL << mmu_psize_defs[mmu_pte_psize].shift;
  285. /* This isn't the most optimal, ideally we would factor out the
  286. * while preempt & CPU mask mucking around, or even the IPI but
  287. * it will do for now
  288. */
  289. while (start < end) {
  290. __flush_tlb_page(tlb->mm, start, tsize, 1);
  291. start += size;
  292. }
  293. } else {
  294. unsigned long rmask = 0xf000000000000000ul;
  295. unsigned long rid = (address & rmask) | 0x1000000000000000ul;
  296. unsigned long vpte = address & ~rmask;
  297. #ifdef CONFIG_PPC_64K_PAGES
  298. vpte = (vpte >> (PAGE_SHIFT - 4)) & ~0xfffful;
  299. #else
  300. vpte = (vpte >> (PAGE_SHIFT - 3)) & ~0xffful;
  301. #endif
  302. vpte |= rid;
  303. __flush_tlb_page(tlb->mm, vpte, tsize, 0);
  304. }
  305. }
  306. /*
  307. * Early initialization of the MMU TLB code
  308. */
  309. static void __early_init_mmu(int boot_cpu)
  310. {
  311. extern unsigned int interrupt_base_book3e;
  312. extern unsigned int exc_data_tlb_miss_htw_book3e;
  313. extern unsigned int exc_instruction_tlb_miss_htw_book3e;
  314. unsigned int *ibase = &interrupt_base_book3e;
  315. unsigned int mas4;
  316. /* XXX This will have to be decided at runtime, but right
  317. * now our boot and TLB miss code hard wires it. Ideally
  318. * we should find out a suitable page size and patch the
  319. * TLB miss code (either that or use the PACA to store
  320. * the value we want)
  321. */
  322. mmu_linear_psize = MMU_PAGE_1G;
  323. /* XXX This should be decided at runtime based on supported
  324. * page sizes in the TLB, but for now let's assume 16M is
  325. * always there and a good fit (which it probably is)
  326. */
  327. mmu_vmemmap_psize = MMU_PAGE_16M;
  328. /* Check if HW tablewalk is present, and if yes, enable it by:
  329. *
  330. * - patching the TLB miss handlers to branch to the
  331. * one dedicates to it
  332. *
  333. * - setting the global book3e_htw_enabled
  334. *
  335. * - Set MAS4:INDD and default page size
  336. */
  337. /* XXX This code only checks for TLB 0 capabilities and doesn't
  338. * check what page size combos are supported by the HW. It
  339. * also doesn't handle the case where a separate array holds
  340. * the IND entries from the array loaded by the PT.
  341. */
  342. if (boot_cpu) {
  343. unsigned int tlb0cfg = mfspr(SPRN_TLB0CFG);
  344. /* Check if HW loader is supported */
  345. if ((tlb0cfg & TLBnCFG_IND) &&
  346. (tlb0cfg & TLBnCFG_PT)) {
  347. patch_branch(ibase + (0x1c0 / 4),
  348. (unsigned long)&exc_data_tlb_miss_htw_book3e, 0);
  349. patch_branch(ibase + (0x1e0 / 4),
  350. (unsigned long)&exc_instruction_tlb_miss_htw_book3e, 0);
  351. book3e_htw_enabled = 1;
  352. }
  353. pr_info("MMU: Book3E Page Tables %s\n",
  354. book3e_htw_enabled ? "Enabled" : "Disabled");
  355. }
  356. /* Set MAS4 based on page table setting */
  357. mas4 = 0x4 << MAS4_WIMGED_SHIFT;
  358. if (book3e_htw_enabled) {
  359. mas4 |= mas4 | MAS4_INDD;
  360. #ifdef CONFIG_PPC_64K_PAGES
  361. mas4 |= BOOK3E_PAGESZ_256M << MAS4_TSIZED_SHIFT;
  362. mmu_pte_psize = MMU_PAGE_256M;
  363. #else
  364. mas4 |= BOOK3E_PAGESZ_1M << MAS4_TSIZED_SHIFT;
  365. mmu_pte_psize = MMU_PAGE_1M;
  366. #endif
  367. } else {
  368. #ifdef CONFIG_PPC_64K_PAGES
  369. mas4 |= BOOK3E_PAGESZ_64K << MAS4_TSIZED_SHIFT;
  370. #else
  371. mas4 |= BOOK3E_PAGESZ_4K << MAS4_TSIZED_SHIFT;
  372. #endif
  373. mmu_pte_psize = mmu_virtual_psize;
  374. }
  375. mtspr(SPRN_MAS4, mas4);
  376. /* Set the global containing the top of the linear mapping
  377. * for use by the TLB miss code
  378. */
  379. linear_map_top = lmb_end_of_DRAM();
  380. /* A sync won't hurt us after mucking around with
  381. * the MMU configuration
  382. */
  383. mb();
  384. }
  385. void __init early_init_mmu(void)
  386. {
  387. __early_init_mmu(1);
  388. }
  389. void __cpuinit early_init_mmu_secondary(void)
  390. {
  391. __early_init_mmu(0);
  392. }
  393. #endif /* CONFIG_PPC64 */