tlb-radix.c 11 KB

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