tlb-radix.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  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 <linux/mmu_context.h>
  15. #include <linux/sched/mm.h>
  16. #include <asm/ppc-opcode.h>
  17. #include <asm/tlb.h>
  18. #include <asm/tlbflush.h>
  19. #include <asm/trace.h>
  20. #include <asm/cputhreads.h>
  21. #define RIC_FLUSH_TLB 0
  22. #define RIC_FLUSH_PWC 1
  23. #define RIC_FLUSH_ALL 2
  24. /*
  25. * tlbiel instruction for radix, set invalidation
  26. * i.e., r=1 and is=01 or is=10 or is=11
  27. */
  28. static inline void tlbiel_radix_set_isa300(unsigned int set, unsigned int is,
  29. unsigned int pid,
  30. unsigned int ric, unsigned int prs)
  31. {
  32. unsigned long rb;
  33. unsigned long rs;
  34. rb = (set << PPC_BITLSHIFT(51)) | (is << PPC_BITLSHIFT(53));
  35. rs = ((unsigned long)pid << PPC_BITLSHIFT(31));
  36. asm volatile(PPC_TLBIEL(%0, %1, %2, %3, 1)
  37. : : "r"(rb), "r"(rs), "i"(ric), "i"(prs)
  38. : "memory");
  39. }
  40. static void tlbiel_all_isa300(unsigned int num_sets, unsigned int is)
  41. {
  42. unsigned int set;
  43. asm volatile("ptesync": : :"memory");
  44. /*
  45. * Flush the first set of the TLB, and the entire Page Walk Cache
  46. * and partition table entries. Then flush the remaining sets of the
  47. * TLB.
  48. */
  49. tlbiel_radix_set_isa300(0, is, 0, RIC_FLUSH_ALL, 0);
  50. for (set = 1; set < num_sets; set++)
  51. tlbiel_radix_set_isa300(set, is, 0, RIC_FLUSH_TLB, 0);
  52. /* Do the same for process scoped entries. */
  53. tlbiel_radix_set_isa300(0, is, 0, RIC_FLUSH_ALL, 1);
  54. for (set = 1; set < num_sets; set++)
  55. tlbiel_radix_set_isa300(set, is, 0, RIC_FLUSH_TLB, 1);
  56. asm volatile("ptesync": : :"memory");
  57. }
  58. void radix__tlbiel_all(unsigned int action)
  59. {
  60. unsigned int is;
  61. switch (action) {
  62. case TLB_INVAL_SCOPE_GLOBAL:
  63. is = 3;
  64. break;
  65. case TLB_INVAL_SCOPE_LPID:
  66. is = 2;
  67. break;
  68. default:
  69. BUG();
  70. }
  71. if (early_cpu_has_feature(CPU_FTR_ARCH_300))
  72. tlbiel_all_isa300(POWER9_TLB_SETS_RADIX, is);
  73. else
  74. WARN(1, "%s called on pre-POWER9 CPU\n", __func__);
  75. asm volatile(PPC_INVALIDATE_ERAT "; isync" : : :"memory");
  76. }
  77. static inline void __tlbiel_pid(unsigned long pid, int set,
  78. unsigned long ric)
  79. {
  80. unsigned long rb,rs,prs,r;
  81. rb = PPC_BIT(53); /* IS = 1 */
  82. rb |= set << PPC_BITLSHIFT(51);
  83. rs = ((unsigned long)pid) << PPC_BITLSHIFT(31);
  84. prs = 1; /* process scoped */
  85. r = 1; /* radix format */
  86. asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
  87. : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
  88. trace_tlbie(0, 1, rb, rs, ric, prs, r);
  89. }
  90. static inline void __tlbie_pid(unsigned long pid, unsigned long ric)
  91. {
  92. unsigned long rb,rs,prs,r;
  93. rb = PPC_BIT(53); /* IS = 1 */
  94. rs = pid << PPC_BITLSHIFT(31);
  95. prs = 1; /* process scoped */
  96. r = 1; /* radix format */
  97. asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
  98. : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
  99. trace_tlbie(0, 0, rb, rs, ric, prs, r);
  100. }
  101. static inline void __tlbiel_lpid(unsigned long lpid, int set,
  102. unsigned long ric)
  103. {
  104. unsigned long rb,rs,prs,r;
  105. rb = PPC_BIT(52); /* IS = 2 */
  106. rb |= set << PPC_BITLSHIFT(51);
  107. rs = 0; /* LPID comes from LPIDR */
  108. prs = 0; /* partition scoped */
  109. r = 1; /* radix format */
  110. asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
  111. : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
  112. trace_tlbie(lpid, 1, rb, rs, ric, prs, r);
  113. }
  114. static inline void __tlbie_lpid(unsigned long lpid, unsigned long ric)
  115. {
  116. unsigned long rb,rs,prs,r;
  117. rb = PPC_BIT(52); /* IS = 2 */
  118. rs = lpid;
  119. prs = 0; /* partition scoped */
  120. r = 1; /* radix format */
  121. asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
  122. : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
  123. trace_tlbie(lpid, 0, rb, rs, ric, prs, r);
  124. }
  125. static inline void __tlbiel_lpid_guest(unsigned long lpid, int set,
  126. unsigned long ric)
  127. {
  128. unsigned long rb,rs,prs,r;
  129. rb = PPC_BIT(52); /* IS = 2 */
  130. rb |= set << PPC_BITLSHIFT(51);
  131. rs = 0; /* LPID comes from LPIDR */
  132. prs = 1; /* process scoped */
  133. r = 1; /* radix format */
  134. asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
  135. : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
  136. trace_tlbie(lpid, 1, rb, rs, ric, prs, r);
  137. }
  138. static inline void __tlbiel_va(unsigned long va, unsigned long pid,
  139. unsigned long ap, unsigned long ric)
  140. {
  141. unsigned long rb,rs,prs,r;
  142. rb = va & ~(PPC_BITMASK(52, 63));
  143. rb |= ap << PPC_BITLSHIFT(58);
  144. rs = pid << PPC_BITLSHIFT(31);
  145. prs = 1; /* process scoped */
  146. r = 1; /* radix format */
  147. asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
  148. : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
  149. trace_tlbie(0, 1, rb, rs, ric, prs, r);
  150. }
  151. static inline void __tlbie_va(unsigned long va, unsigned long pid,
  152. unsigned long ap, unsigned long ric)
  153. {
  154. unsigned long rb,rs,prs,r;
  155. rb = va & ~(PPC_BITMASK(52, 63));
  156. rb |= ap << PPC_BITLSHIFT(58);
  157. rs = pid << PPC_BITLSHIFT(31);
  158. prs = 1; /* process scoped */
  159. r = 1; /* radix format */
  160. asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
  161. : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
  162. trace_tlbie(0, 0, rb, rs, ric, prs, r);
  163. }
  164. static inline void __tlbie_lpid_va(unsigned long va, unsigned long lpid,
  165. unsigned long ap, unsigned long ric)
  166. {
  167. unsigned long rb,rs,prs,r;
  168. rb = va & ~(PPC_BITMASK(52, 63));
  169. rb |= ap << PPC_BITLSHIFT(58);
  170. rs = lpid;
  171. prs = 0; /* partition scoped */
  172. r = 1; /* radix format */
  173. asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
  174. : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
  175. trace_tlbie(lpid, 0, rb, rs, ric, prs, r);
  176. }
  177. static inline void fixup_tlbie(void)
  178. {
  179. unsigned long pid = 0;
  180. unsigned long va = ((1UL << 52) - 1);
  181. if (cpu_has_feature(CPU_FTR_P9_TLBIE_BUG)) {
  182. asm volatile("ptesync": : :"memory");
  183. __tlbie_va(va, pid, mmu_get_ap(MMU_PAGE_64K), RIC_FLUSH_TLB);
  184. }
  185. }
  186. static inline void fixup_tlbie_lpid(unsigned long lpid)
  187. {
  188. unsigned long va = ((1UL << 52) - 1);
  189. if (cpu_has_feature(CPU_FTR_P9_TLBIE_BUG)) {
  190. asm volatile("ptesync": : :"memory");
  191. __tlbie_lpid_va(va, lpid, mmu_get_ap(MMU_PAGE_64K), RIC_FLUSH_TLB);
  192. }
  193. }
  194. /*
  195. * We use 128 set in radix mode and 256 set in hpt mode.
  196. */
  197. static inline void _tlbiel_pid(unsigned long pid, unsigned long ric)
  198. {
  199. int set;
  200. asm volatile("ptesync": : :"memory");
  201. /*
  202. * Flush the first set of the TLB, and if we're doing a RIC_FLUSH_ALL,
  203. * also flush the entire Page Walk Cache.
  204. */
  205. __tlbiel_pid(pid, 0, ric);
  206. /* For PWC, only one flush is needed */
  207. if (ric == RIC_FLUSH_PWC) {
  208. asm volatile("ptesync": : :"memory");
  209. return;
  210. }
  211. /* For the remaining sets, just flush the TLB */
  212. for (set = 1; set < POWER9_TLB_SETS_RADIX ; set++)
  213. __tlbiel_pid(pid, set, RIC_FLUSH_TLB);
  214. asm volatile("ptesync": : :"memory");
  215. asm volatile(PPC_INVALIDATE_ERAT "; isync" : : :"memory");
  216. }
  217. static inline void _tlbie_pid(unsigned long pid, unsigned long ric)
  218. {
  219. asm volatile("ptesync": : :"memory");
  220. /*
  221. * Workaround the fact that the "ric" argument to __tlbie_pid
  222. * must be a compile-time contraint to match the "i" constraint
  223. * in the asm statement.
  224. */
  225. switch (ric) {
  226. case RIC_FLUSH_TLB:
  227. __tlbie_pid(pid, RIC_FLUSH_TLB);
  228. break;
  229. case RIC_FLUSH_PWC:
  230. __tlbie_pid(pid, RIC_FLUSH_PWC);
  231. break;
  232. case RIC_FLUSH_ALL:
  233. default:
  234. __tlbie_pid(pid, RIC_FLUSH_ALL);
  235. }
  236. fixup_tlbie();
  237. asm volatile("eieio; tlbsync; ptesync": : :"memory");
  238. }
  239. static inline void _tlbiel_lpid(unsigned long lpid, unsigned long ric)
  240. {
  241. int set;
  242. VM_BUG_ON(mfspr(SPRN_LPID) != lpid);
  243. asm volatile("ptesync": : :"memory");
  244. /*
  245. * Flush the first set of the TLB, and if we're doing a RIC_FLUSH_ALL,
  246. * also flush the entire Page Walk Cache.
  247. */
  248. __tlbiel_lpid(lpid, 0, ric);
  249. /* For PWC, only one flush is needed */
  250. if (ric == RIC_FLUSH_PWC) {
  251. asm volatile("ptesync": : :"memory");
  252. return;
  253. }
  254. /* For the remaining sets, just flush the TLB */
  255. for (set = 1; set < POWER9_TLB_SETS_RADIX ; set++)
  256. __tlbiel_lpid(lpid, set, RIC_FLUSH_TLB);
  257. asm volatile("ptesync": : :"memory");
  258. asm volatile(PPC_INVALIDATE_ERAT "; isync" : : :"memory");
  259. }
  260. static inline void _tlbie_lpid(unsigned long lpid, unsigned long ric)
  261. {
  262. asm volatile("ptesync": : :"memory");
  263. /*
  264. * Workaround the fact that the "ric" argument to __tlbie_pid
  265. * must be a compile-time contraint to match the "i" constraint
  266. * in the asm statement.
  267. */
  268. switch (ric) {
  269. case RIC_FLUSH_TLB:
  270. __tlbie_lpid(lpid, RIC_FLUSH_TLB);
  271. break;
  272. case RIC_FLUSH_PWC:
  273. __tlbie_lpid(lpid, RIC_FLUSH_PWC);
  274. break;
  275. case RIC_FLUSH_ALL:
  276. default:
  277. __tlbie_lpid(lpid, RIC_FLUSH_ALL);
  278. }
  279. fixup_tlbie_lpid(lpid);
  280. asm volatile("eieio; tlbsync; ptesync": : :"memory");
  281. }
  282. static inline void _tlbiel_lpid_guest(unsigned long lpid, unsigned long ric)
  283. {
  284. int set;
  285. VM_BUG_ON(mfspr(SPRN_LPID) != lpid);
  286. asm volatile("ptesync": : :"memory");
  287. /*
  288. * Flush the first set of the TLB, and if we're doing a RIC_FLUSH_ALL,
  289. * also flush the entire Page Walk Cache.
  290. */
  291. __tlbiel_lpid_guest(lpid, 0, ric);
  292. /* For PWC, only one flush is needed */
  293. if (ric == RIC_FLUSH_PWC) {
  294. asm volatile("ptesync": : :"memory");
  295. return;
  296. }
  297. /* For the remaining sets, just flush the TLB */
  298. for (set = 1; set < POWER9_TLB_SETS_RADIX ; set++)
  299. __tlbiel_lpid_guest(lpid, set, RIC_FLUSH_TLB);
  300. asm volatile("ptesync": : :"memory");
  301. }
  302. static inline void __tlbiel_va_range(unsigned long start, unsigned long end,
  303. unsigned long pid, unsigned long page_size,
  304. unsigned long psize)
  305. {
  306. unsigned long addr;
  307. unsigned long ap = mmu_get_ap(psize);
  308. for (addr = start; addr < end; addr += page_size)
  309. __tlbiel_va(addr, pid, ap, RIC_FLUSH_TLB);
  310. }
  311. static inline void _tlbiel_va(unsigned long va, unsigned long pid,
  312. unsigned long psize, unsigned long ric)
  313. {
  314. unsigned long ap = mmu_get_ap(psize);
  315. asm volatile("ptesync": : :"memory");
  316. __tlbiel_va(va, pid, ap, ric);
  317. asm volatile("ptesync": : :"memory");
  318. }
  319. static inline void _tlbiel_va_range(unsigned long start, unsigned long end,
  320. unsigned long pid, unsigned long page_size,
  321. unsigned long psize, bool also_pwc)
  322. {
  323. asm volatile("ptesync": : :"memory");
  324. if (also_pwc)
  325. __tlbiel_pid(pid, 0, RIC_FLUSH_PWC);
  326. __tlbiel_va_range(start, end, pid, page_size, psize);
  327. asm volatile("ptesync": : :"memory");
  328. }
  329. static inline void __tlbie_va_range(unsigned long start, unsigned long end,
  330. unsigned long pid, unsigned long page_size,
  331. unsigned long psize)
  332. {
  333. unsigned long addr;
  334. unsigned long ap = mmu_get_ap(psize);
  335. for (addr = start; addr < end; addr += page_size)
  336. __tlbie_va(addr, pid, ap, RIC_FLUSH_TLB);
  337. }
  338. static inline void _tlbie_va(unsigned long va, unsigned long pid,
  339. unsigned long psize, unsigned long ric)
  340. {
  341. unsigned long ap = mmu_get_ap(psize);
  342. asm volatile("ptesync": : :"memory");
  343. __tlbie_va(va, pid, ap, ric);
  344. fixup_tlbie();
  345. asm volatile("eieio; tlbsync; ptesync": : :"memory");
  346. }
  347. static inline void _tlbie_lpid_va(unsigned long va, unsigned long lpid,
  348. unsigned long psize, unsigned long ric)
  349. {
  350. unsigned long ap = mmu_get_ap(psize);
  351. asm volatile("ptesync": : :"memory");
  352. __tlbie_lpid_va(va, lpid, ap, ric);
  353. fixup_tlbie_lpid(lpid);
  354. asm volatile("eieio; tlbsync; ptesync": : :"memory");
  355. }
  356. static inline void _tlbie_va_range(unsigned long start, unsigned long end,
  357. unsigned long pid, unsigned long page_size,
  358. unsigned long psize, bool also_pwc)
  359. {
  360. asm volatile("ptesync": : :"memory");
  361. if (also_pwc)
  362. __tlbie_pid(pid, RIC_FLUSH_PWC);
  363. __tlbie_va_range(start, end, pid, page_size, psize);
  364. fixup_tlbie();
  365. asm volatile("eieio; tlbsync; ptesync": : :"memory");
  366. }
  367. /*
  368. * Base TLB flushing operations:
  369. *
  370. * - flush_tlb_mm(mm) flushes the specified mm context TLB's
  371. * - flush_tlb_page(vma, vmaddr) flushes one page
  372. * - flush_tlb_range(vma, start, end) flushes a range of pages
  373. * - flush_tlb_kernel_range(start, end) flushes kernel pages
  374. *
  375. * - local_* variants of page and mm only apply to the current
  376. * processor
  377. */
  378. void radix__local_flush_tlb_mm(struct mm_struct *mm)
  379. {
  380. unsigned long pid;
  381. preempt_disable();
  382. pid = mm->context.id;
  383. if (pid != MMU_NO_CONTEXT)
  384. _tlbiel_pid(pid, RIC_FLUSH_TLB);
  385. preempt_enable();
  386. }
  387. EXPORT_SYMBOL(radix__local_flush_tlb_mm);
  388. #ifndef CONFIG_SMP
  389. void radix__local_flush_all_mm(struct mm_struct *mm)
  390. {
  391. unsigned long pid;
  392. preempt_disable();
  393. pid = mm->context.id;
  394. if (pid != MMU_NO_CONTEXT)
  395. _tlbiel_pid(pid, RIC_FLUSH_ALL);
  396. preempt_enable();
  397. }
  398. EXPORT_SYMBOL(radix__local_flush_all_mm);
  399. #endif /* CONFIG_SMP */
  400. void radix__local_flush_tlb_page_psize(struct mm_struct *mm, unsigned long vmaddr,
  401. int psize)
  402. {
  403. unsigned long pid;
  404. preempt_disable();
  405. pid = mm->context.id;
  406. if (pid != MMU_NO_CONTEXT)
  407. _tlbiel_va(vmaddr, pid, psize, RIC_FLUSH_TLB);
  408. preempt_enable();
  409. }
  410. void radix__local_flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
  411. {
  412. #ifdef CONFIG_HUGETLB_PAGE
  413. /* need the return fix for nohash.c */
  414. if (is_vm_hugetlb_page(vma))
  415. return radix__local_flush_hugetlb_page(vma, vmaddr);
  416. #endif
  417. radix__local_flush_tlb_page_psize(vma->vm_mm, vmaddr, mmu_virtual_psize);
  418. }
  419. EXPORT_SYMBOL(radix__local_flush_tlb_page);
  420. static bool mm_is_singlethreaded(struct mm_struct *mm)
  421. {
  422. if (atomic_read(&mm->context.copros) > 0)
  423. return false;
  424. if (atomic_read(&mm->mm_users) <= 1 && current->mm == mm)
  425. return true;
  426. return false;
  427. }
  428. static bool mm_needs_flush_escalation(struct mm_struct *mm)
  429. {
  430. /*
  431. * P9 nest MMU has issues with the page walk cache
  432. * caching PTEs and not flushing them properly when
  433. * RIC = 0 for a PID/LPID invalidate
  434. */
  435. if (atomic_read(&mm->context.copros) > 0)
  436. return true;
  437. return false;
  438. }
  439. #ifdef CONFIG_SMP
  440. static void do_exit_flush_lazy_tlb(void *arg)
  441. {
  442. struct mm_struct *mm = arg;
  443. unsigned long pid = mm->context.id;
  444. if (current->mm == mm)
  445. return; /* Local CPU */
  446. if (current->active_mm == mm) {
  447. /*
  448. * Must be a kernel thread because sender is single-threaded.
  449. */
  450. BUG_ON(current->mm);
  451. mmgrab(&init_mm);
  452. switch_mm(mm, &init_mm, current);
  453. current->active_mm = &init_mm;
  454. mmdrop(mm);
  455. }
  456. _tlbiel_pid(pid, RIC_FLUSH_ALL);
  457. }
  458. static void exit_flush_lazy_tlbs(struct mm_struct *mm)
  459. {
  460. /*
  461. * Would be nice if this was async so it could be run in
  462. * parallel with our local flush, but generic code does not
  463. * give a good API for it. Could extend the generic code or
  464. * make a special powerpc IPI for flushing TLBs.
  465. * For now it's not too performance critical.
  466. */
  467. smp_call_function_many(mm_cpumask(mm), do_exit_flush_lazy_tlb,
  468. (void *)mm, 1);
  469. mm_reset_thread_local(mm);
  470. }
  471. void radix__flush_tlb_mm(struct mm_struct *mm)
  472. {
  473. unsigned long pid;
  474. pid = mm->context.id;
  475. if (unlikely(pid == MMU_NO_CONTEXT))
  476. return;
  477. preempt_disable();
  478. /*
  479. * Order loads of mm_cpumask vs previous stores to clear ptes before
  480. * the invalidate. See barrier in switch_mm_irqs_off
  481. */
  482. smp_mb();
  483. if (!mm_is_thread_local(mm)) {
  484. if (unlikely(mm_is_singlethreaded(mm))) {
  485. exit_flush_lazy_tlbs(mm);
  486. goto local;
  487. }
  488. if (mm_needs_flush_escalation(mm))
  489. _tlbie_pid(pid, RIC_FLUSH_ALL);
  490. else
  491. _tlbie_pid(pid, RIC_FLUSH_TLB);
  492. } else {
  493. local:
  494. _tlbiel_pid(pid, RIC_FLUSH_TLB);
  495. }
  496. preempt_enable();
  497. }
  498. EXPORT_SYMBOL(radix__flush_tlb_mm);
  499. static void __flush_all_mm(struct mm_struct *mm, bool fullmm)
  500. {
  501. unsigned long pid;
  502. pid = mm->context.id;
  503. if (unlikely(pid == MMU_NO_CONTEXT))
  504. return;
  505. preempt_disable();
  506. smp_mb(); /* see radix__flush_tlb_mm */
  507. if (!mm_is_thread_local(mm)) {
  508. if (unlikely(mm_is_singlethreaded(mm))) {
  509. if (!fullmm) {
  510. exit_flush_lazy_tlbs(mm);
  511. goto local;
  512. }
  513. }
  514. _tlbie_pid(pid, RIC_FLUSH_ALL);
  515. } else {
  516. local:
  517. _tlbiel_pid(pid, RIC_FLUSH_ALL);
  518. }
  519. preempt_enable();
  520. }
  521. void radix__flush_all_mm(struct mm_struct *mm)
  522. {
  523. __flush_all_mm(mm, false);
  524. }
  525. EXPORT_SYMBOL(radix__flush_all_mm);
  526. void radix__flush_tlb_pwc(struct mmu_gather *tlb, unsigned long addr)
  527. {
  528. tlb->need_flush_all = 1;
  529. }
  530. EXPORT_SYMBOL(radix__flush_tlb_pwc);
  531. void radix__flush_tlb_page_psize(struct mm_struct *mm, unsigned long vmaddr,
  532. int psize)
  533. {
  534. unsigned long pid;
  535. pid = mm->context.id;
  536. if (unlikely(pid == MMU_NO_CONTEXT))
  537. return;
  538. preempt_disable();
  539. smp_mb(); /* see radix__flush_tlb_mm */
  540. if (!mm_is_thread_local(mm)) {
  541. if (unlikely(mm_is_singlethreaded(mm))) {
  542. exit_flush_lazy_tlbs(mm);
  543. goto local;
  544. }
  545. _tlbie_va(vmaddr, pid, psize, RIC_FLUSH_TLB);
  546. } else {
  547. local:
  548. _tlbiel_va(vmaddr, pid, psize, RIC_FLUSH_TLB);
  549. }
  550. preempt_enable();
  551. }
  552. void radix__flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
  553. {
  554. #ifdef CONFIG_HUGETLB_PAGE
  555. if (is_vm_hugetlb_page(vma))
  556. return radix__flush_hugetlb_page(vma, vmaddr);
  557. #endif
  558. radix__flush_tlb_page_psize(vma->vm_mm, vmaddr, mmu_virtual_psize);
  559. }
  560. EXPORT_SYMBOL(radix__flush_tlb_page);
  561. #else /* CONFIG_SMP */
  562. #define radix__flush_all_mm radix__local_flush_all_mm
  563. #endif /* CONFIG_SMP */
  564. void radix__flush_tlb_kernel_range(unsigned long start, unsigned long end)
  565. {
  566. _tlbie_pid(0, RIC_FLUSH_ALL);
  567. }
  568. EXPORT_SYMBOL(radix__flush_tlb_kernel_range);
  569. #define TLB_FLUSH_ALL -1UL
  570. /*
  571. * Number of pages above which we invalidate the entire PID rather than
  572. * flush individual pages, for local and global flushes respectively.
  573. *
  574. * tlbie goes out to the interconnect and individual ops are more costly.
  575. * It also does not iterate over sets like the local tlbiel variant when
  576. * invalidating a full PID, so it has a far lower threshold to change from
  577. * individual page flushes to full-pid flushes.
  578. */
  579. static unsigned long tlb_single_page_flush_ceiling __read_mostly = 33;
  580. static unsigned long tlb_local_single_page_flush_ceiling __read_mostly = POWER9_TLB_SETS_RADIX * 2;
  581. void radix__flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
  582. unsigned long end)
  583. {
  584. struct mm_struct *mm = vma->vm_mm;
  585. unsigned long pid;
  586. unsigned int page_shift = mmu_psize_defs[mmu_virtual_psize].shift;
  587. unsigned long page_size = 1UL << page_shift;
  588. unsigned long nr_pages = (end - start) >> page_shift;
  589. bool local, full;
  590. #ifdef CONFIG_HUGETLB_PAGE
  591. if (is_vm_hugetlb_page(vma))
  592. return radix__flush_hugetlb_tlb_range(vma, start, end);
  593. #endif
  594. pid = mm->context.id;
  595. if (unlikely(pid == MMU_NO_CONTEXT))
  596. return;
  597. preempt_disable();
  598. smp_mb(); /* see radix__flush_tlb_mm */
  599. if (!mm_is_thread_local(mm)) {
  600. if (unlikely(mm_is_singlethreaded(mm))) {
  601. if (end != TLB_FLUSH_ALL) {
  602. exit_flush_lazy_tlbs(mm);
  603. goto is_local;
  604. }
  605. }
  606. local = false;
  607. full = (end == TLB_FLUSH_ALL ||
  608. nr_pages > tlb_single_page_flush_ceiling);
  609. } else {
  610. is_local:
  611. local = true;
  612. full = (end == TLB_FLUSH_ALL ||
  613. nr_pages > tlb_local_single_page_flush_ceiling);
  614. }
  615. if (full) {
  616. if (local) {
  617. _tlbiel_pid(pid, RIC_FLUSH_TLB);
  618. } else {
  619. if (mm_needs_flush_escalation(mm))
  620. _tlbie_pid(pid, RIC_FLUSH_ALL);
  621. else
  622. _tlbie_pid(pid, RIC_FLUSH_TLB);
  623. }
  624. } else {
  625. bool hflush = false;
  626. unsigned long hstart, hend;
  627. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  628. hstart = (start + HPAGE_PMD_SIZE - 1) >> HPAGE_PMD_SHIFT;
  629. hend = end >> HPAGE_PMD_SHIFT;
  630. if (hstart < hend) {
  631. hstart <<= HPAGE_PMD_SHIFT;
  632. hend <<= HPAGE_PMD_SHIFT;
  633. hflush = true;
  634. }
  635. #endif
  636. asm volatile("ptesync": : :"memory");
  637. if (local) {
  638. __tlbiel_va_range(start, end, pid, page_size, mmu_virtual_psize);
  639. if (hflush)
  640. __tlbiel_va_range(hstart, hend, pid,
  641. HPAGE_PMD_SIZE, MMU_PAGE_2M);
  642. asm volatile("ptesync": : :"memory");
  643. } else {
  644. __tlbie_va_range(start, end, pid, page_size, mmu_virtual_psize);
  645. if (hflush)
  646. __tlbie_va_range(hstart, hend, pid,
  647. HPAGE_PMD_SIZE, MMU_PAGE_2M);
  648. fixup_tlbie();
  649. asm volatile("eieio; tlbsync; ptesync": : :"memory");
  650. }
  651. }
  652. preempt_enable();
  653. }
  654. EXPORT_SYMBOL(radix__flush_tlb_range);
  655. static int radix_get_mmu_psize(int page_size)
  656. {
  657. int psize;
  658. if (page_size == (1UL << mmu_psize_defs[mmu_virtual_psize].shift))
  659. psize = mmu_virtual_psize;
  660. else if (page_size == (1UL << mmu_psize_defs[MMU_PAGE_2M].shift))
  661. psize = MMU_PAGE_2M;
  662. else if (page_size == (1UL << mmu_psize_defs[MMU_PAGE_1G].shift))
  663. psize = MMU_PAGE_1G;
  664. else
  665. return -1;
  666. return psize;
  667. }
  668. /*
  669. * Flush partition scoped LPID address translation for all CPUs.
  670. */
  671. void radix__flush_tlb_lpid_page(unsigned int lpid,
  672. unsigned long addr,
  673. unsigned long page_size)
  674. {
  675. int psize = radix_get_mmu_psize(page_size);
  676. _tlbie_lpid_va(addr, lpid, psize, RIC_FLUSH_TLB);
  677. }
  678. EXPORT_SYMBOL_GPL(radix__flush_tlb_lpid_page);
  679. /*
  680. * Flush partition scoped PWC from LPID for all CPUs.
  681. */
  682. void radix__flush_pwc_lpid(unsigned int lpid)
  683. {
  684. _tlbie_lpid(lpid, RIC_FLUSH_PWC);
  685. }
  686. EXPORT_SYMBOL_GPL(radix__flush_pwc_lpid);
  687. /*
  688. * Flush partition scoped translations from LPID (=LPIDR)
  689. */
  690. void radix__local_flush_tlb_lpid(unsigned int lpid)
  691. {
  692. _tlbiel_lpid(lpid, RIC_FLUSH_ALL);
  693. }
  694. EXPORT_SYMBOL_GPL(radix__local_flush_tlb_lpid);
  695. /*
  696. * Flush process scoped translations from LPID (=LPIDR).
  697. * Important difference, the guest normally manages its own translations,
  698. * but some cases e.g., vCPU CPU migration require KVM to flush.
  699. */
  700. void radix__local_flush_tlb_lpid_guest(unsigned int lpid)
  701. {
  702. _tlbiel_lpid_guest(lpid, RIC_FLUSH_ALL);
  703. }
  704. EXPORT_SYMBOL_GPL(radix__local_flush_tlb_lpid_guest);
  705. static void radix__flush_tlb_pwc_range_psize(struct mm_struct *mm, unsigned long start,
  706. unsigned long end, int psize);
  707. void radix__tlb_flush(struct mmu_gather *tlb)
  708. {
  709. int psize = 0;
  710. struct mm_struct *mm = tlb->mm;
  711. int page_size = tlb->page_size;
  712. /*
  713. * if page size is not something we understand, do a full mm flush
  714. *
  715. * A "fullmm" flush must always do a flush_all_mm (RIC=2) flush
  716. * that flushes the process table entry cache upon process teardown.
  717. * See the comment for radix in arch_exit_mmap().
  718. */
  719. if (tlb->fullmm) {
  720. __flush_all_mm(mm, true);
  721. } else if ( (psize = radix_get_mmu_psize(page_size)) == -1) {
  722. if (!tlb->need_flush_all)
  723. radix__flush_tlb_mm(mm);
  724. else
  725. radix__flush_all_mm(mm);
  726. } else {
  727. unsigned long start = tlb->start;
  728. unsigned long end = tlb->end;
  729. if (!tlb->need_flush_all)
  730. radix__flush_tlb_range_psize(mm, start, end, psize);
  731. else
  732. radix__flush_tlb_pwc_range_psize(mm, start, end, psize);
  733. }
  734. tlb->need_flush_all = 0;
  735. }
  736. static inline void __radix__flush_tlb_range_psize(struct mm_struct *mm,
  737. unsigned long start, unsigned long end,
  738. int psize, bool also_pwc)
  739. {
  740. unsigned long pid;
  741. unsigned int page_shift = mmu_psize_defs[psize].shift;
  742. unsigned long page_size = 1UL << page_shift;
  743. unsigned long nr_pages = (end - start) >> page_shift;
  744. bool local, full;
  745. pid = mm->context.id;
  746. if (unlikely(pid == MMU_NO_CONTEXT))
  747. return;
  748. preempt_disable();
  749. smp_mb(); /* see radix__flush_tlb_mm */
  750. if (!mm_is_thread_local(mm)) {
  751. if (unlikely(mm_is_singlethreaded(mm))) {
  752. if (end != TLB_FLUSH_ALL) {
  753. exit_flush_lazy_tlbs(mm);
  754. goto is_local;
  755. }
  756. }
  757. local = false;
  758. full = (end == TLB_FLUSH_ALL ||
  759. nr_pages > tlb_single_page_flush_ceiling);
  760. } else {
  761. is_local:
  762. local = true;
  763. full = (end == TLB_FLUSH_ALL ||
  764. nr_pages > tlb_local_single_page_flush_ceiling);
  765. }
  766. if (full) {
  767. if (local) {
  768. _tlbiel_pid(pid, also_pwc ? RIC_FLUSH_ALL : RIC_FLUSH_TLB);
  769. } else {
  770. if (mm_needs_flush_escalation(mm))
  771. also_pwc = true;
  772. _tlbie_pid(pid, also_pwc ? RIC_FLUSH_ALL : RIC_FLUSH_TLB);
  773. }
  774. } else {
  775. if (local)
  776. _tlbiel_va_range(start, end, pid, page_size, psize, also_pwc);
  777. else
  778. _tlbie_va_range(start, end, pid, page_size, psize, also_pwc);
  779. }
  780. preempt_enable();
  781. }
  782. void radix__flush_tlb_range_psize(struct mm_struct *mm, unsigned long start,
  783. unsigned long end, int psize)
  784. {
  785. return __radix__flush_tlb_range_psize(mm, start, end, psize, false);
  786. }
  787. static void radix__flush_tlb_pwc_range_psize(struct mm_struct *mm, unsigned long start,
  788. unsigned long end, int psize)
  789. {
  790. __radix__flush_tlb_range_psize(mm, start, end, psize, true);
  791. }
  792. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  793. void radix__flush_tlb_collapsed_pmd(struct mm_struct *mm, unsigned long addr)
  794. {
  795. unsigned long pid, end;
  796. pid = mm->context.id;
  797. if (unlikely(pid == MMU_NO_CONTEXT))
  798. return;
  799. /* 4k page size, just blow the world */
  800. if (PAGE_SIZE == 0x1000) {
  801. radix__flush_all_mm(mm);
  802. return;
  803. }
  804. end = addr + HPAGE_PMD_SIZE;
  805. /* Otherwise first do the PWC, then iterate the pages. */
  806. preempt_disable();
  807. smp_mb(); /* see radix__flush_tlb_mm */
  808. if (!mm_is_thread_local(mm)) {
  809. if (unlikely(mm_is_singlethreaded(mm))) {
  810. exit_flush_lazy_tlbs(mm);
  811. goto local;
  812. }
  813. _tlbie_va_range(addr, end, pid, PAGE_SIZE, mmu_virtual_psize, true);
  814. goto local;
  815. } else {
  816. local:
  817. _tlbiel_va_range(addr, end, pid, PAGE_SIZE, mmu_virtual_psize, true);
  818. }
  819. preempt_enable();
  820. }
  821. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  822. void radix__flush_pmd_tlb_range(struct vm_area_struct *vma,
  823. unsigned long start, unsigned long end)
  824. {
  825. radix__flush_tlb_range_psize(vma->vm_mm, start, end, MMU_PAGE_2M);
  826. }
  827. EXPORT_SYMBOL(radix__flush_pmd_tlb_range);
  828. void radix__flush_tlb_all(void)
  829. {
  830. unsigned long rb,prs,r,rs;
  831. unsigned long ric = RIC_FLUSH_ALL;
  832. rb = 0x3 << PPC_BITLSHIFT(53); /* IS = 3 */
  833. prs = 0; /* partition scoped */
  834. r = 1; /* radix format */
  835. rs = 1 & ((1UL << 32) - 1); /* any LPID value to flush guest mappings */
  836. asm volatile("ptesync": : :"memory");
  837. /*
  838. * now flush guest entries by passing PRS = 1 and LPID != 0
  839. */
  840. asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
  841. : : "r"(rb), "i"(r), "i"(1), "i"(ric), "r"(rs) : "memory");
  842. /*
  843. * now flush host entires by passing PRS = 0 and LPID == 0
  844. */
  845. asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
  846. : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(0) : "memory");
  847. asm volatile("eieio; tlbsync; ptesync": : :"memory");
  848. }
  849. void radix__flush_tlb_pte_p9_dd1(unsigned long old_pte, struct mm_struct *mm,
  850. unsigned long address)
  851. {
  852. /*
  853. * We track page size in pte only for DD1, So we can
  854. * call this only on DD1.
  855. */
  856. if (!cpu_has_feature(CPU_FTR_POWER9_DD1)) {
  857. VM_WARN_ON(1);
  858. return;
  859. }
  860. if (old_pte & R_PAGE_LARGE)
  861. radix__flush_tlb_page_psize(mm, address, MMU_PAGE_2M);
  862. else
  863. radix__flush_tlb_page_psize(mm, address, mmu_virtual_psize);
  864. }
  865. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  866. extern void radix_kvm_prefetch_workaround(struct mm_struct *mm)
  867. {
  868. unsigned long pid = mm->context.id;
  869. if (unlikely(pid == MMU_NO_CONTEXT))
  870. return;
  871. /*
  872. * If this context hasn't run on that CPU before and KVM is
  873. * around, there's a slim chance that the guest on another
  874. * CPU just brought in obsolete translation into the TLB of
  875. * this CPU due to a bad prefetch using the guest PID on
  876. * the way into the hypervisor.
  877. *
  878. * We work around this here. If KVM is possible, we check if
  879. * any sibling thread is in KVM. If it is, the window may exist
  880. * and thus we flush that PID from the core.
  881. *
  882. * A potential future improvement would be to mark which PIDs
  883. * have never been used on the system and avoid it if the PID
  884. * is new and the process has no other cpumask bit set.
  885. */
  886. if (cpu_has_feature(CPU_FTR_HVMODE) && radix_enabled()) {
  887. int cpu = smp_processor_id();
  888. int sib = cpu_first_thread_sibling(cpu);
  889. bool flush = false;
  890. for (; sib <= cpu_last_thread_sibling(cpu) && !flush; sib++) {
  891. if (sib == cpu)
  892. continue;
  893. if (paca_ptrs[sib]->kvm_hstate.kvm_vcpu)
  894. flush = true;
  895. }
  896. if (flush)
  897. _tlbiel_pid(pid, RIC_FLUSH_ALL);
  898. }
  899. }
  900. EXPORT_SYMBOL_GPL(radix_kvm_prefetch_workaround);
  901. #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */