tlb-radix.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * TLB flush routines for radix kernels.
  3. *
  4. * Copyright 2015-2016, Aneesh Kumar K.V, IBM Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/mm.h>
  12. #include <linux/hugetlb.h>
  13. #include <linux/memblock.h>
  14. #include <asm/ppc-opcode.h>
  15. #include <asm/tlb.h>
  16. #include <asm/tlbflush.h>
  17. static DEFINE_RAW_SPINLOCK(native_tlbie_lock);
  18. #define RIC_FLUSH_TLB 0
  19. #define RIC_FLUSH_PWC 1
  20. #define RIC_FLUSH_ALL 2
  21. static inline void __tlbiel_pid(unsigned long pid, int set,
  22. unsigned long ric)
  23. {
  24. unsigned long rb,rs,prs,r;
  25. rb = PPC_BIT(53); /* IS = 1 */
  26. rb |= set << PPC_BITLSHIFT(51);
  27. rs = ((unsigned long)pid) << PPC_BITLSHIFT(31);
  28. prs = 1; /* process scoped */
  29. r = 1; /* raidx format */
  30. asm volatile("ptesync": : :"memory");
  31. asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
  32. : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
  33. asm volatile("ptesync": : :"memory");
  34. }
  35. /*
  36. * We use 128 set in radix mode and 256 set in hpt mode.
  37. */
  38. static inline void _tlbiel_pid(unsigned long pid, unsigned long ric)
  39. {
  40. int set;
  41. for (set = 0; set < POWER9_TLB_SETS_RADIX ; set++) {
  42. __tlbiel_pid(pid, set, ric);
  43. }
  44. if (cpu_has_feature(CPU_FTR_POWER9_DD1))
  45. asm volatile(PPC_INVALIDATE_ERAT : : :"memory");
  46. return;
  47. }
  48. static inline void _tlbie_pid(unsigned long pid, unsigned long ric)
  49. {
  50. unsigned long rb,rs,prs,r;
  51. rb = PPC_BIT(53); /* IS = 1 */
  52. rs = pid << PPC_BITLSHIFT(31);
  53. prs = 1; /* process scoped */
  54. r = 1; /* raidx format */
  55. asm volatile("ptesync": : :"memory");
  56. asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
  57. : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
  58. asm volatile("eieio; tlbsync; ptesync": : :"memory");
  59. }
  60. static inline void _tlbiel_va(unsigned long va, unsigned long pid,
  61. unsigned long ap, unsigned long ric)
  62. {
  63. unsigned long rb,rs,prs,r;
  64. rb = va & ~(PPC_BITMASK(52, 63));
  65. rb |= ap << PPC_BITLSHIFT(58);
  66. rs = pid << PPC_BITLSHIFT(31);
  67. prs = 1; /* process scoped */
  68. r = 1; /* raidx format */
  69. asm volatile("ptesync": : :"memory");
  70. asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
  71. : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
  72. asm volatile("ptesync": : :"memory");
  73. if (cpu_has_feature(CPU_FTR_POWER9_DD1))
  74. asm volatile(PPC_INVALIDATE_ERAT : : :"memory");
  75. }
  76. static inline void _tlbie_va(unsigned long va, unsigned long pid,
  77. unsigned long ap, unsigned long ric)
  78. {
  79. unsigned long rb,rs,prs,r;
  80. rb = va & ~(PPC_BITMASK(52, 63));
  81. rb |= ap << PPC_BITLSHIFT(58);
  82. rs = pid << PPC_BITLSHIFT(31);
  83. prs = 1; /* process scoped */
  84. r = 1; /* raidx format */
  85. asm volatile("ptesync": : :"memory");
  86. asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
  87. : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
  88. asm volatile("eieio; tlbsync; ptesync": : :"memory");
  89. }
  90. /*
  91. * Base TLB flushing operations:
  92. *
  93. * - flush_tlb_mm(mm) flushes the specified mm context TLB's
  94. * - flush_tlb_page(vma, vmaddr) flushes one page
  95. * - flush_tlb_range(vma, start, end) flushes a range of pages
  96. * - flush_tlb_kernel_range(start, end) flushes kernel pages
  97. *
  98. * - local_* variants of page and mm only apply to the current
  99. * processor
  100. */
  101. void radix__local_flush_tlb_mm(struct mm_struct *mm)
  102. {
  103. unsigned long pid;
  104. preempt_disable();
  105. pid = mm->context.id;
  106. if (pid != MMU_NO_CONTEXT)
  107. _tlbiel_pid(pid, RIC_FLUSH_ALL);
  108. preempt_enable();
  109. }
  110. EXPORT_SYMBOL(radix__local_flush_tlb_mm);
  111. void radix__local_flush_tlb_pwc(struct mmu_gather *tlb, unsigned long addr)
  112. {
  113. unsigned long pid;
  114. struct mm_struct *mm = tlb->mm;
  115. preempt_disable();
  116. pid = mm->context.id;
  117. if (pid != MMU_NO_CONTEXT)
  118. _tlbiel_pid(pid, RIC_FLUSH_PWC);
  119. preempt_enable();
  120. }
  121. EXPORT_SYMBOL(radix__local_flush_tlb_pwc);
  122. void radix__local_flush_tlb_page_psize(struct mm_struct *mm, unsigned long vmaddr,
  123. int psize)
  124. {
  125. unsigned long pid;
  126. unsigned long ap = mmu_get_ap(psize);
  127. preempt_disable();
  128. pid = mm ? mm->context.id : 0;
  129. if (pid != MMU_NO_CONTEXT)
  130. _tlbiel_va(vmaddr, pid, ap, RIC_FLUSH_TLB);
  131. preempt_enable();
  132. }
  133. void radix__local_flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
  134. {
  135. #ifdef CONFIG_HUGETLB_PAGE
  136. /* need the return fix for nohash.c */
  137. if (vma && is_vm_hugetlb_page(vma))
  138. return __local_flush_hugetlb_page(vma, vmaddr);
  139. #endif
  140. radix__local_flush_tlb_page_psize(vma ? vma->vm_mm : NULL, vmaddr,
  141. mmu_virtual_psize);
  142. }
  143. EXPORT_SYMBOL(radix__local_flush_tlb_page);
  144. #ifdef CONFIG_SMP
  145. void radix__flush_tlb_mm(struct mm_struct *mm)
  146. {
  147. unsigned long pid;
  148. preempt_disable();
  149. pid = mm->context.id;
  150. if (unlikely(pid == MMU_NO_CONTEXT))
  151. goto no_context;
  152. if (!mm_is_thread_local(mm)) {
  153. int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
  154. if (lock_tlbie)
  155. raw_spin_lock(&native_tlbie_lock);
  156. _tlbie_pid(pid, RIC_FLUSH_ALL);
  157. if (lock_tlbie)
  158. raw_spin_unlock(&native_tlbie_lock);
  159. } else
  160. _tlbiel_pid(pid, RIC_FLUSH_ALL);
  161. no_context:
  162. preempt_enable();
  163. }
  164. EXPORT_SYMBOL(radix__flush_tlb_mm);
  165. void radix__flush_tlb_pwc(struct mmu_gather *tlb, unsigned long addr)
  166. {
  167. unsigned long pid;
  168. struct mm_struct *mm = tlb->mm;
  169. preempt_disable();
  170. pid = mm->context.id;
  171. if (unlikely(pid == MMU_NO_CONTEXT))
  172. goto no_context;
  173. if (!mm_is_thread_local(mm)) {
  174. int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
  175. if (lock_tlbie)
  176. raw_spin_lock(&native_tlbie_lock);
  177. _tlbie_pid(pid, RIC_FLUSH_PWC);
  178. if (lock_tlbie)
  179. raw_spin_unlock(&native_tlbie_lock);
  180. } else
  181. _tlbiel_pid(pid, RIC_FLUSH_PWC);
  182. no_context:
  183. preempt_enable();
  184. }
  185. EXPORT_SYMBOL(radix__flush_tlb_pwc);
  186. void radix__flush_tlb_page_psize(struct mm_struct *mm, unsigned long vmaddr,
  187. int psize)
  188. {
  189. unsigned long pid;
  190. unsigned long ap = mmu_get_ap(psize);
  191. preempt_disable();
  192. pid = mm ? mm->context.id : 0;
  193. if (unlikely(pid == MMU_NO_CONTEXT))
  194. goto bail;
  195. if (!mm_is_thread_local(mm)) {
  196. int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
  197. if (lock_tlbie)
  198. raw_spin_lock(&native_tlbie_lock);
  199. _tlbie_va(vmaddr, pid, ap, RIC_FLUSH_TLB);
  200. if (lock_tlbie)
  201. raw_spin_unlock(&native_tlbie_lock);
  202. } else
  203. _tlbiel_va(vmaddr, pid, ap, RIC_FLUSH_TLB);
  204. bail:
  205. preempt_enable();
  206. }
  207. void radix__flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
  208. {
  209. #ifdef CONFIG_HUGETLB_PAGE
  210. if (vma && is_vm_hugetlb_page(vma))
  211. return flush_hugetlb_page(vma, vmaddr);
  212. #endif
  213. radix__flush_tlb_page_psize(vma ? vma->vm_mm : NULL, vmaddr,
  214. mmu_virtual_psize);
  215. }
  216. EXPORT_SYMBOL(radix__flush_tlb_page);
  217. #endif /* CONFIG_SMP */
  218. void radix__flush_tlb_kernel_range(unsigned long start, unsigned long end)
  219. {
  220. int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
  221. if (lock_tlbie)
  222. raw_spin_lock(&native_tlbie_lock);
  223. _tlbie_pid(0, RIC_FLUSH_ALL);
  224. if (lock_tlbie)
  225. raw_spin_unlock(&native_tlbie_lock);
  226. }
  227. EXPORT_SYMBOL(radix__flush_tlb_kernel_range);
  228. /*
  229. * Currently, for range flushing, we just do a full mm flush. Because
  230. * we use this in code path where we don' track the page size.
  231. */
  232. void radix__flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
  233. unsigned long end)
  234. {
  235. struct mm_struct *mm = vma->vm_mm;
  236. radix__flush_tlb_mm(mm);
  237. }
  238. EXPORT_SYMBOL(radix__flush_tlb_range);
  239. static int radix_get_mmu_psize(int page_size)
  240. {
  241. int psize;
  242. if (page_size == (1UL << mmu_psize_defs[mmu_virtual_psize].shift))
  243. psize = mmu_virtual_psize;
  244. else if (page_size == (1UL << mmu_psize_defs[MMU_PAGE_2M].shift))
  245. psize = MMU_PAGE_2M;
  246. else if (page_size == (1UL << mmu_psize_defs[MMU_PAGE_1G].shift))
  247. psize = MMU_PAGE_1G;
  248. else
  249. return -1;
  250. return psize;
  251. }
  252. void radix__tlb_flush(struct mmu_gather *tlb)
  253. {
  254. int psize = 0;
  255. struct mm_struct *mm = tlb->mm;
  256. int page_size = tlb->page_size;
  257. psize = radix_get_mmu_psize(page_size);
  258. /*
  259. * if page size is not something we understand, do a full mm flush
  260. */
  261. if (psize != -1 && !tlb->fullmm && !tlb->need_flush_all)
  262. radix__flush_tlb_range_psize(mm, tlb->start, tlb->end, psize);
  263. else
  264. radix__flush_tlb_mm(mm);
  265. }
  266. #define TLB_FLUSH_ALL -1UL
  267. /*
  268. * Number of pages above which we will do a bcast tlbie. Just a
  269. * number at this point copied from x86
  270. */
  271. static unsigned long tlb_single_page_flush_ceiling __read_mostly = 33;
  272. void radix__flush_tlb_range_psize(struct mm_struct *mm, unsigned long start,
  273. unsigned long end, int psize)
  274. {
  275. unsigned long pid;
  276. unsigned long addr;
  277. int local = mm_is_thread_local(mm);
  278. unsigned long ap = mmu_get_ap(psize);
  279. int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
  280. unsigned long page_size = 1UL << mmu_psize_defs[psize].shift;
  281. preempt_disable();
  282. pid = mm ? mm->context.id : 0;
  283. if (unlikely(pid == MMU_NO_CONTEXT))
  284. goto err_out;
  285. if (end == TLB_FLUSH_ALL ||
  286. (end - start) > tlb_single_page_flush_ceiling * page_size) {
  287. if (local)
  288. _tlbiel_pid(pid, RIC_FLUSH_TLB);
  289. else
  290. _tlbie_pid(pid, RIC_FLUSH_TLB);
  291. goto err_out;
  292. }
  293. for (addr = start; addr < end; addr += page_size) {
  294. if (local)
  295. _tlbiel_va(addr, pid, ap, RIC_FLUSH_TLB);
  296. else {
  297. if (lock_tlbie)
  298. raw_spin_lock(&native_tlbie_lock);
  299. _tlbie_va(addr, pid, ap, RIC_FLUSH_TLB);
  300. if (lock_tlbie)
  301. raw_spin_unlock(&native_tlbie_lock);
  302. }
  303. }
  304. err_out:
  305. preempt_enable();
  306. }
  307. void radix__flush_tlb_lpid_va(unsigned long lpid, unsigned long gpa,
  308. unsigned long page_size)
  309. {
  310. unsigned long rb,rs,prs,r;
  311. unsigned long ap;
  312. unsigned long ric = RIC_FLUSH_TLB;
  313. ap = mmu_get_ap(radix_get_mmu_psize(page_size));
  314. rb = gpa & ~(PPC_BITMASK(52, 63));
  315. rb |= ap << PPC_BITLSHIFT(58);
  316. rs = lpid & ((1UL << 32) - 1);
  317. prs = 0; /* process scoped */
  318. r = 1; /* raidx format */
  319. asm volatile("ptesync": : :"memory");
  320. asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
  321. : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
  322. asm volatile("eieio; tlbsync; ptesync": : :"memory");
  323. }
  324. EXPORT_SYMBOL(radix__flush_tlb_lpid_va);
  325. void radix__flush_tlb_lpid(unsigned long lpid)
  326. {
  327. unsigned long rb,rs,prs,r;
  328. unsigned long ric = RIC_FLUSH_ALL;
  329. rb = 0x2 << PPC_BITLSHIFT(53); /* IS = 2 */
  330. rs = lpid & ((1UL << 32) - 1);
  331. prs = 0; /* partition scoped */
  332. r = 1; /* raidx format */
  333. asm volatile("ptesync": : :"memory");
  334. asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
  335. : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
  336. asm volatile("eieio; tlbsync; ptesync": : :"memory");
  337. }
  338. EXPORT_SYMBOL(radix__flush_tlb_lpid);
  339. void radix__flush_pmd_tlb_range(struct vm_area_struct *vma,
  340. unsigned long start, unsigned long end)
  341. {
  342. radix__flush_tlb_range_psize(vma->vm_mm, start, end, MMU_PAGE_2M);
  343. }
  344. EXPORT_SYMBOL(radix__flush_pmd_tlb_range);
  345. void radix__flush_tlb_all(void)
  346. {
  347. unsigned long rb,prs,r,rs;
  348. unsigned long ric = RIC_FLUSH_ALL;
  349. rb = 0x3 << PPC_BITLSHIFT(53); /* IS = 3 */
  350. prs = 0; /* partition scoped */
  351. r = 1; /* raidx format */
  352. rs = 1 & ((1UL << 32) - 1); /* any LPID value to flush guest mappings */
  353. asm volatile("ptesync": : :"memory");
  354. /*
  355. * now flush guest entries by passing PRS = 1 and LPID != 0
  356. */
  357. asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
  358. : : "r"(rb), "i"(r), "i"(1), "i"(ric), "r"(rs) : "memory");
  359. /*
  360. * now flush host entires by passing PRS = 0 and LPID == 0
  361. */
  362. asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
  363. : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(0) : "memory");
  364. asm volatile("eieio; tlbsync; ptesync": : :"memory");
  365. }
  366. void radix__flush_tlb_pte_p9_dd1(unsigned long old_pte, struct mm_struct *mm,
  367. unsigned long address)
  368. {
  369. /*
  370. * We track page size in pte only for DD1, So we can
  371. * call this only on DD1.
  372. */
  373. if (!cpu_has_feature(CPU_FTR_POWER9_DD1)) {
  374. VM_WARN_ON(1);
  375. return;
  376. }
  377. if (old_pte & _PAGE_LARGE)
  378. radix__flush_tlb_page_psize(mm, address, MMU_PAGE_2M);
  379. else
  380. radix__flush_tlb_page_psize(mm, address, mmu_virtual_psize);
  381. }