radix_tlb.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * TLB flush routines for radix kernels.
  4. *
  5. * Copyright 2015-2016, Aneesh Kumar K.V, IBM Corporation.
  6. */
  7. #include <linux/mm.h>
  8. #include <linux/hugetlb.h>
  9. #include <linux/memblock.h>
  10. #include <linux/mmu_context.h>
  11. #include <linux/sched/mm.h>
  12. #include <linux/debugfs.h>
  13. #include <asm/ppc-opcode.h>
  14. #include <asm/tlb.h>
  15. #include <asm/tlbflush.h>
  16. #include <asm/trace.h>
  17. #include <asm/cputhreads.h>
  18. #include <asm/plpar_wrappers.h>
  19. #include "internal.h"
  20. /*
  21. * tlbiel instruction for radix, set invalidation
  22. * i.e., r=1 and is=01 or is=10 or is=11
  23. */
  24. static __always_inline void tlbiel_radix_set_isa300(unsigned int set, unsigned int is,
  25. unsigned int pid,
  26. unsigned int ric, unsigned int prs)
  27. {
  28. unsigned long rb;
  29. unsigned long rs;
  30. rb = (set << PPC_BITLSHIFT(51)) | (is << PPC_BITLSHIFT(53));
  31. rs = ((unsigned long)pid << PPC_BITLSHIFT(31));
  32. asm volatile(PPC_TLBIEL(%0, %1, %2, %3, 1)
  33. : : "r"(rb), "r"(rs), "i"(ric), "i"(prs)
  34. : "memory");
  35. }
  36. static void tlbiel_all_isa300(unsigned int num_sets, unsigned int is)
  37. {
  38. unsigned int set;
  39. asm volatile("ptesync": : :"memory");
  40. /*
  41. * Flush the first set of the TLB, and the entire Page Walk Cache
  42. * and partition table entries. Then flush the remaining sets of the
  43. * TLB.
  44. */
  45. if (early_cpu_has_feature(CPU_FTR_HVMODE)) {
  46. /* MSR[HV] should flush partition scope translations first. */
  47. tlbiel_radix_set_isa300(0, is, 0, RIC_FLUSH_ALL, 0);
  48. if (!early_cpu_has_feature(CPU_FTR_ARCH_31)) {
  49. for (set = 1; set < num_sets; set++)
  50. tlbiel_radix_set_isa300(set, is, 0,
  51. RIC_FLUSH_TLB, 0);
  52. }
  53. }
  54. /* Flush process scoped entries. */
  55. tlbiel_radix_set_isa300(0, is, 0, RIC_FLUSH_ALL, 1);
  56. if (!early_cpu_has_feature(CPU_FTR_ARCH_31)) {
  57. for (set = 1; set < num_sets; set++)
  58. tlbiel_radix_set_isa300(set, is, 0, RIC_FLUSH_TLB, 1);
  59. }
  60. ppc_after_tlbiel_barrier();
  61. }
  62. void radix__tlbiel_all(unsigned int action)
  63. {
  64. unsigned int is;
  65. switch (action) {
  66. case TLB_INVAL_SCOPE_GLOBAL:
  67. is = 3;
  68. break;
  69. case TLB_INVAL_SCOPE_LPID:
  70. is = 2;
  71. break;
  72. default:
  73. BUG();
  74. }
  75. if (early_cpu_has_feature(CPU_FTR_ARCH_300))
  76. tlbiel_all_isa300(POWER9_TLB_SETS_RADIX, is);
  77. else
  78. WARN(1, "%s called on pre-POWER9 CPU\n", __func__);
  79. asm volatile(PPC_ISA_3_0_INVALIDATE_ERAT "; isync" : : :"memory");
  80. }
  81. static __always_inline void __tlbiel_pid(unsigned long pid, int set,
  82. unsigned long ric)
  83. {
  84. unsigned long rb,rs,prs,r;
  85. rb = PPC_BIT(53); /* IS = 1 */
  86. rb |= set << PPC_BITLSHIFT(51);
  87. rs = ((unsigned long)pid) << PPC_BITLSHIFT(31);
  88. prs = 1; /* process scoped */
  89. r = 1; /* radix format */
  90. asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
  91. : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
  92. trace_tlbie(0, 1, rb, rs, ric, prs, r);
  93. }
  94. static __always_inline void __tlbie_pid(unsigned long pid, unsigned long ric)
  95. {
  96. unsigned long rb,rs,prs,r;
  97. rb = PPC_BIT(53); /* IS = 1 */
  98. rs = pid << PPC_BITLSHIFT(31);
  99. prs = 1; /* process scoped */
  100. r = 1; /* radix format */
  101. asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
  102. : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
  103. trace_tlbie(0, 0, rb, rs, ric, prs, r);
  104. }
  105. static __always_inline void __tlbie_lpid(unsigned long lpid, unsigned long ric)
  106. {
  107. unsigned long rb,rs,prs,r;
  108. rb = PPC_BIT(52); /* IS = 2 */
  109. rs = lpid;
  110. prs = 0; /* partition scoped */
  111. r = 1; /* radix format */
  112. asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
  113. : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
  114. trace_tlbie(lpid, 0, rb, rs, ric, prs, r);
  115. }
  116. static __always_inline void __tlbie_lpid_guest(unsigned long lpid, unsigned long ric)
  117. {
  118. unsigned long rb,rs,prs,r;
  119. rb = PPC_BIT(52); /* IS = 2 */
  120. rs = lpid;
  121. prs = 1; /* process scoped */
  122. r = 1; /* radix format */
  123. asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
  124. : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
  125. trace_tlbie(lpid, 0, rb, rs, ric, prs, r);
  126. }
  127. static __always_inline void __tlbiel_va(unsigned long va, unsigned long pid,
  128. unsigned long ap, unsigned long ric)
  129. {
  130. unsigned long rb,rs,prs,r;
  131. rb = va & ~(PPC_BITMASK(52, 63));
  132. rb |= ap << PPC_BITLSHIFT(58);
  133. rs = pid << PPC_BITLSHIFT(31);
  134. prs = 1; /* process scoped */
  135. r = 1; /* radix format */
  136. asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)
  137. : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
  138. trace_tlbie(0, 1, rb, rs, ric, prs, r);
  139. }
  140. static __always_inline void __tlbie_va(unsigned long va, unsigned long pid,
  141. unsigned long ap, unsigned long ric)
  142. {
  143. unsigned long rb,rs,prs,r;
  144. rb = va & ~(PPC_BITMASK(52, 63));
  145. rb |= ap << PPC_BITLSHIFT(58);
  146. rs = pid << PPC_BITLSHIFT(31);
  147. prs = 1; /* process scoped */
  148. r = 1; /* radix format */
  149. asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
  150. : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
  151. trace_tlbie(0, 0, rb, rs, ric, prs, r);
  152. }
  153. static __always_inline void __tlbie_lpid_va(unsigned long va, unsigned long lpid,
  154. unsigned long ap, unsigned long ric)
  155. {
  156. unsigned long rb,rs,prs,r;
  157. rb = va & ~(PPC_BITMASK(52, 63));
  158. rb |= ap << PPC_BITLSHIFT(58);
  159. rs = lpid;
  160. prs = 0; /* partition scoped */
  161. r = 1; /* radix format */
  162. asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
  163. : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
  164. trace_tlbie(lpid, 0, rb, rs, ric, prs, r);
  165. }
  166. static inline void fixup_tlbie_va(unsigned long va, unsigned long pid,
  167. unsigned long ap)
  168. {
  169. if (cpu_has_feature(CPU_FTR_P9_TLBIE_ERAT_BUG)) {
  170. asm volatile("ptesync": : :"memory");
  171. __tlbie_va(va, 0, ap, RIC_FLUSH_TLB);
  172. }
  173. if (cpu_has_feature(CPU_FTR_P9_TLBIE_STQ_BUG)) {
  174. asm volatile("ptesync": : :"memory");
  175. __tlbie_va(va, pid, ap, RIC_FLUSH_TLB);
  176. }
  177. }
  178. static inline void fixup_tlbie_va_range(unsigned long va, unsigned long pid,
  179. unsigned long ap)
  180. {
  181. if (cpu_has_feature(CPU_FTR_P9_TLBIE_ERAT_BUG)) {
  182. asm volatile("ptesync": : :"memory");
  183. __tlbie_pid(0, RIC_FLUSH_TLB);
  184. }
  185. if (cpu_has_feature(CPU_FTR_P9_TLBIE_STQ_BUG)) {
  186. asm volatile("ptesync": : :"memory");
  187. __tlbie_va(va, pid, ap, RIC_FLUSH_TLB);
  188. }
  189. }
  190. static inline void fixup_tlbie_pid(unsigned long pid)
  191. {
  192. /*
  193. * We can use any address for the invalidation, pick one which is
  194. * probably unused as an optimisation.
  195. */
  196. unsigned long va = ((1UL << 52) - 1);
  197. if (cpu_has_feature(CPU_FTR_P9_TLBIE_ERAT_BUG)) {
  198. asm volatile("ptesync": : :"memory");
  199. __tlbie_pid(0, RIC_FLUSH_TLB);
  200. }
  201. if (cpu_has_feature(CPU_FTR_P9_TLBIE_STQ_BUG)) {
  202. asm volatile("ptesync": : :"memory");
  203. __tlbie_va(va, pid, mmu_get_ap(MMU_PAGE_64K), RIC_FLUSH_TLB);
  204. }
  205. }
  206. static inline void fixup_tlbie_lpid_va(unsigned long va, unsigned long lpid,
  207. unsigned long ap)
  208. {
  209. if (cpu_has_feature(CPU_FTR_P9_TLBIE_ERAT_BUG)) {
  210. asm volatile("ptesync": : :"memory");
  211. __tlbie_lpid_va(va, 0, ap, RIC_FLUSH_TLB);
  212. }
  213. if (cpu_has_feature(CPU_FTR_P9_TLBIE_STQ_BUG)) {
  214. asm volatile("ptesync": : :"memory");
  215. __tlbie_lpid_va(va, lpid, ap, RIC_FLUSH_TLB);
  216. }
  217. }
  218. static inline void fixup_tlbie_lpid(unsigned long lpid)
  219. {
  220. /*
  221. * We can use any address for the invalidation, pick one which is
  222. * probably unused as an optimisation.
  223. */
  224. unsigned long va = ((1UL << 52) - 1);
  225. if (cpu_has_feature(CPU_FTR_P9_TLBIE_ERAT_BUG)) {
  226. asm volatile("ptesync": : :"memory");
  227. __tlbie_lpid(0, RIC_FLUSH_TLB);
  228. }
  229. if (cpu_has_feature(CPU_FTR_P9_TLBIE_STQ_BUG)) {
  230. asm volatile("ptesync": : :"memory");
  231. __tlbie_lpid_va(va, lpid, mmu_get_ap(MMU_PAGE_64K), RIC_FLUSH_TLB);
  232. }
  233. }
  234. /*
  235. * We use 128 set in radix mode and 256 set in hpt mode.
  236. */
  237. static inline void _tlbiel_pid(unsigned long pid, unsigned long ric)
  238. {
  239. int set;
  240. asm volatile("ptesync": : :"memory");
  241. switch (ric) {
  242. case RIC_FLUSH_PWC:
  243. /* For PWC, only one flush is needed */
  244. __tlbiel_pid(pid, 0, RIC_FLUSH_PWC);
  245. ppc_after_tlbiel_barrier();
  246. return;
  247. case RIC_FLUSH_TLB:
  248. __tlbiel_pid(pid, 0, RIC_FLUSH_TLB);
  249. break;
  250. case RIC_FLUSH_ALL:
  251. default:
  252. /*
  253. * Flush the first set of the TLB, and if
  254. * we're doing a RIC_FLUSH_ALL, also flush
  255. * the entire Page Walk Cache.
  256. */
  257. __tlbiel_pid(pid, 0, RIC_FLUSH_ALL);
  258. }
  259. if (!cpu_has_feature(CPU_FTR_ARCH_31)) {
  260. /* For the remaining sets, just flush the TLB */
  261. for (set = 1; set < POWER9_TLB_SETS_RADIX ; set++)
  262. __tlbiel_pid(pid, set, RIC_FLUSH_TLB);
  263. }
  264. ppc_after_tlbiel_barrier();
  265. asm volatile(PPC_RADIX_INVALIDATE_ERAT_USER "; isync" : : :"memory");
  266. }
  267. static inline void _tlbie_pid(unsigned long pid, unsigned long ric)
  268. {
  269. asm volatile("ptesync": : :"memory");
  270. /*
  271. * Workaround the fact that the "ric" argument to __tlbie_pid
  272. * must be a compile-time constraint to match the "i" constraint
  273. * in the asm statement.
  274. */
  275. switch (ric) {
  276. case RIC_FLUSH_TLB:
  277. __tlbie_pid(pid, RIC_FLUSH_TLB);
  278. fixup_tlbie_pid(pid);
  279. break;
  280. case RIC_FLUSH_PWC:
  281. __tlbie_pid(pid, RIC_FLUSH_PWC);
  282. break;
  283. case RIC_FLUSH_ALL:
  284. default:
  285. __tlbie_pid(pid, RIC_FLUSH_ALL);
  286. fixup_tlbie_pid(pid);
  287. }
  288. asm volatile("eieio; tlbsync; ptesync": : :"memory");
  289. }
  290. struct tlbiel_pid {
  291. unsigned long pid;
  292. unsigned long ric;
  293. };
  294. static void do_tlbiel_pid(void *info)
  295. {
  296. struct tlbiel_pid *t = info;
  297. if (t->ric == RIC_FLUSH_TLB)
  298. _tlbiel_pid(t->pid, RIC_FLUSH_TLB);
  299. else if (t->ric == RIC_FLUSH_PWC)
  300. _tlbiel_pid(t->pid, RIC_FLUSH_PWC);
  301. else
  302. _tlbiel_pid(t->pid, RIC_FLUSH_ALL);
  303. }
  304. static inline void _tlbiel_pid_multicast(struct mm_struct *mm,
  305. unsigned long pid, unsigned long ric)
  306. {
  307. struct cpumask *cpus = mm_cpumask(mm);
  308. struct tlbiel_pid t = { .pid = pid, .ric = ric };
  309. on_each_cpu_mask(cpus, do_tlbiel_pid, &t, 1);
  310. /*
  311. * Always want the CPU translations to be invalidated with tlbiel in
  312. * these paths, so while coprocessors must use tlbie, we can not
  313. * optimise away the tlbiel component.
  314. */
  315. if (atomic_read(&mm->context.copros) > 0)
  316. _tlbie_pid(pid, RIC_FLUSH_ALL);
  317. }
  318. static inline void _tlbie_lpid(unsigned long lpid, unsigned long ric)
  319. {
  320. asm volatile("ptesync": : :"memory");
  321. /*
  322. * Workaround the fact that the "ric" argument to __tlbie_pid
  323. * must be a compile-time contraint to match the "i" constraint
  324. * in the asm statement.
  325. */
  326. switch (ric) {
  327. case RIC_FLUSH_TLB:
  328. __tlbie_lpid(lpid, RIC_FLUSH_TLB);
  329. fixup_tlbie_lpid(lpid);
  330. break;
  331. case RIC_FLUSH_PWC:
  332. __tlbie_lpid(lpid, RIC_FLUSH_PWC);
  333. break;
  334. case RIC_FLUSH_ALL:
  335. default:
  336. __tlbie_lpid(lpid, RIC_FLUSH_ALL);
  337. fixup_tlbie_lpid(lpid);
  338. }
  339. asm volatile("eieio; tlbsync; ptesync": : :"memory");
  340. }
  341. static __always_inline void _tlbie_lpid_guest(unsigned long lpid, unsigned long ric)
  342. {
  343. /*
  344. * Workaround the fact that the "ric" argument to __tlbie_pid
  345. * must be a compile-time contraint to match the "i" constraint
  346. * in the asm statement.
  347. */
  348. switch (ric) {
  349. case RIC_FLUSH_TLB:
  350. __tlbie_lpid_guest(lpid, RIC_FLUSH_TLB);
  351. break;
  352. case RIC_FLUSH_PWC:
  353. __tlbie_lpid_guest(lpid, RIC_FLUSH_PWC);
  354. break;
  355. case RIC_FLUSH_ALL:
  356. default:
  357. __tlbie_lpid_guest(lpid, RIC_FLUSH_ALL);
  358. }
  359. fixup_tlbie_lpid(lpid);
  360. asm volatile("eieio; tlbsync; ptesync": : :"memory");
  361. }
  362. static inline void __tlbiel_va_range(unsigned long start, unsigned long end,
  363. unsigned long pid, unsigned long page_size,
  364. unsigned long psize)
  365. {
  366. unsigned long addr;
  367. unsigned long ap = mmu_get_ap(psize);
  368. for (addr = start; addr < end; addr += page_size)
  369. __tlbiel_va(addr, pid, ap, RIC_FLUSH_TLB);
  370. }
  371. static __always_inline void _tlbiel_va(unsigned long va, unsigned long pid,
  372. unsigned long psize, unsigned long ric)
  373. {
  374. unsigned long ap = mmu_get_ap(psize);
  375. asm volatile("ptesync": : :"memory");
  376. __tlbiel_va(va, pid, ap, ric);
  377. ppc_after_tlbiel_barrier();
  378. }
  379. static inline void _tlbiel_va_range(unsigned long start, unsigned long end,
  380. unsigned long pid, unsigned long page_size,
  381. unsigned long psize, bool also_pwc)
  382. {
  383. asm volatile("ptesync": : :"memory");
  384. if (also_pwc)
  385. __tlbiel_pid(pid, 0, RIC_FLUSH_PWC);
  386. __tlbiel_va_range(start, end, pid, page_size, psize);
  387. ppc_after_tlbiel_barrier();
  388. }
  389. static inline void __tlbie_va_range(unsigned long start, unsigned long end,
  390. unsigned long pid, unsigned long page_size,
  391. unsigned long psize)
  392. {
  393. unsigned long addr;
  394. unsigned long ap = mmu_get_ap(psize);
  395. for (addr = start; addr < end; addr += page_size)
  396. __tlbie_va(addr, pid, ap, RIC_FLUSH_TLB);
  397. fixup_tlbie_va_range(addr - page_size, pid, ap);
  398. }
  399. static __always_inline void _tlbie_va(unsigned long va, unsigned long pid,
  400. unsigned long psize, unsigned long ric)
  401. {
  402. unsigned long ap = mmu_get_ap(psize);
  403. asm volatile("ptesync": : :"memory");
  404. __tlbie_va(va, pid, ap, ric);
  405. fixup_tlbie_va(va, pid, ap);
  406. asm volatile("eieio; tlbsync; ptesync": : :"memory");
  407. }
  408. struct tlbiel_va {
  409. unsigned long pid;
  410. unsigned long va;
  411. unsigned long psize;
  412. unsigned long ric;
  413. };
  414. static void do_tlbiel_va(void *info)
  415. {
  416. struct tlbiel_va *t = info;
  417. if (t->ric == RIC_FLUSH_TLB)
  418. _tlbiel_va(t->va, t->pid, t->psize, RIC_FLUSH_TLB);
  419. else if (t->ric == RIC_FLUSH_PWC)
  420. _tlbiel_va(t->va, t->pid, t->psize, RIC_FLUSH_PWC);
  421. else
  422. _tlbiel_va(t->va, t->pid, t->psize, RIC_FLUSH_ALL);
  423. }
  424. static inline void _tlbiel_va_multicast(struct mm_struct *mm,
  425. unsigned long va, unsigned long pid,
  426. unsigned long psize, unsigned long ric)
  427. {
  428. struct cpumask *cpus = mm_cpumask(mm);
  429. struct tlbiel_va t = { .va = va, .pid = pid, .psize = psize, .ric = ric };
  430. on_each_cpu_mask(cpus, do_tlbiel_va, &t, 1);
  431. if (atomic_read(&mm->context.copros) > 0)
  432. _tlbie_va(va, pid, psize, RIC_FLUSH_TLB);
  433. }
  434. struct tlbiel_va_range {
  435. unsigned long pid;
  436. unsigned long start;
  437. unsigned long end;
  438. unsigned long page_size;
  439. unsigned long psize;
  440. bool also_pwc;
  441. };
  442. static void do_tlbiel_va_range(void *info)
  443. {
  444. struct tlbiel_va_range *t = info;
  445. _tlbiel_va_range(t->start, t->end, t->pid, t->page_size,
  446. t->psize, t->also_pwc);
  447. }
  448. static __always_inline void _tlbie_lpid_va(unsigned long va, unsigned long lpid,
  449. unsigned long psize, unsigned long ric)
  450. {
  451. unsigned long ap = mmu_get_ap(psize);
  452. asm volatile("ptesync": : :"memory");
  453. __tlbie_lpid_va(va, lpid, ap, ric);
  454. fixup_tlbie_lpid_va(va, lpid, ap);
  455. asm volatile("eieio; tlbsync; ptesync": : :"memory");
  456. }
  457. static inline void _tlbie_va_range(unsigned long start, unsigned long end,
  458. unsigned long pid, unsigned long page_size,
  459. unsigned long psize, bool also_pwc)
  460. {
  461. asm volatile("ptesync": : :"memory");
  462. if (also_pwc)
  463. __tlbie_pid(pid, RIC_FLUSH_PWC);
  464. __tlbie_va_range(start, end, pid, page_size, psize);
  465. asm volatile("eieio; tlbsync; ptesync": : :"memory");
  466. }
  467. static inline void _tlbiel_va_range_multicast(struct mm_struct *mm,
  468. unsigned long start, unsigned long end,
  469. unsigned long pid, unsigned long page_size,
  470. unsigned long psize, bool also_pwc)
  471. {
  472. struct cpumask *cpus = mm_cpumask(mm);
  473. struct tlbiel_va_range t = { .start = start, .end = end,
  474. .pid = pid, .page_size = page_size,
  475. .psize = psize, .also_pwc = also_pwc };
  476. on_each_cpu_mask(cpus, do_tlbiel_va_range, &t, 1);
  477. if (atomic_read(&mm->context.copros) > 0)
  478. _tlbie_va_range(start, end, pid, page_size, psize, also_pwc);
  479. }
  480. /*
  481. * Base TLB flushing operations:
  482. *
  483. * - flush_tlb_mm(mm) flushes the specified mm context TLB's
  484. * - flush_tlb_page(vma, vmaddr) flushes one page
  485. * - flush_tlb_range(vma, start, end) flushes a range of pages
  486. * - flush_tlb_kernel_range(start, end) flushes kernel pages
  487. *
  488. * - local_* variants of page and mm only apply to the current
  489. * processor
  490. */
  491. void radix__local_flush_tlb_mm(struct mm_struct *mm)
  492. {
  493. unsigned long pid;
  494. preempt_disable();
  495. pid = mm->context.id;
  496. if (pid != MMU_NO_CONTEXT)
  497. _tlbiel_pid(pid, RIC_FLUSH_TLB);
  498. preempt_enable();
  499. }
  500. EXPORT_SYMBOL(radix__local_flush_tlb_mm);
  501. #ifndef CONFIG_SMP
  502. void radix__local_flush_all_mm(struct mm_struct *mm)
  503. {
  504. unsigned long pid;
  505. preempt_disable();
  506. pid = mm->context.id;
  507. if (pid != MMU_NO_CONTEXT)
  508. _tlbiel_pid(pid, RIC_FLUSH_ALL);
  509. preempt_enable();
  510. }
  511. EXPORT_SYMBOL(radix__local_flush_all_mm);
  512. static void __flush_all_mm(struct mm_struct *mm, bool fullmm)
  513. {
  514. radix__local_flush_all_mm(mm);
  515. }
  516. #endif /* CONFIG_SMP */
  517. void radix__local_flush_tlb_page_psize(struct mm_struct *mm, unsigned long vmaddr,
  518. int psize)
  519. {
  520. unsigned long pid;
  521. preempt_disable();
  522. pid = mm->context.id;
  523. if (pid != MMU_NO_CONTEXT)
  524. _tlbiel_va(vmaddr, pid, psize, RIC_FLUSH_TLB);
  525. preempt_enable();
  526. }
  527. void radix__local_flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
  528. {
  529. #ifdef CONFIG_HUGETLB_PAGE
  530. /* need the return fix for nohash.c */
  531. if (is_vm_hugetlb_page(vma))
  532. return radix__local_flush_hugetlb_page(vma, vmaddr);
  533. #endif
  534. radix__local_flush_tlb_page_psize(vma->vm_mm, vmaddr, mmu_virtual_psize);
  535. }
  536. EXPORT_SYMBOL(radix__local_flush_tlb_page);
  537. static bool mm_needs_flush_escalation(struct mm_struct *mm)
  538. {
  539. /*
  540. * The P9 nest MMU has issues with the page walk cache caching PTEs
  541. * and not flushing them when RIC = 0 for a PID/LPID invalidate.
  542. *
  543. * This may have been fixed in shipping firmware (by disabling PWC
  544. * or preventing it from caching PTEs), but until that is confirmed,
  545. * this workaround is required - escalate all RIC=0 IS=1/2/3 flushes
  546. * to RIC=2.
  547. *
  548. * POWER10 (and P9P) does not have this problem.
  549. */
  550. if (cpu_has_feature(CPU_FTR_ARCH_31))
  551. return false;
  552. if (atomic_read(&mm->context.copros) > 0)
  553. return true;
  554. return false;
  555. }
  556. /*
  557. * If always_flush is true, then flush even if this CPU can't be removed
  558. * from mm_cpumask.
  559. */
  560. void exit_lazy_flush_tlb(struct mm_struct *mm, bool always_flush)
  561. {
  562. unsigned long pid = mm->context.id;
  563. int cpu = smp_processor_id();
  564. /*
  565. * A kthread could have done a mmget_not_zero() after the flushing CPU
  566. * checked mm_cpumask, and be in the process of kthread_use_mm when
  567. * interrupted here. In that case, current->mm will be set to mm,
  568. * because kthread_use_mm() setting ->mm and switching to the mm is
  569. * done with interrupts off.
  570. */
  571. if (current->mm == mm)
  572. goto out;
  573. if (current->active_mm == mm) {
  574. WARN_ON_ONCE(current->mm != NULL);
  575. /* Is a kernel thread and is using mm as the lazy tlb */
  576. mmgrab(&init_mm);
  577. current->active_mm = &init_mm;
  578. switch_mm_irqs_off(mm, &init_mm, current);
  579. mmdrop(mm);
  580. }
  581. /*
  582. * This IPI may be initiated from any source including those not
  583. * running the mm, so there may be a racing IPI that comes after
  584. * this one which finds the cpumask already clear. Check and avoid
  585. * underflowing the active_cpus count in that case. The race should
  586. * not otherwise be a problem, but the TLB must be flushed because
  587. * that's what the caller expects.
  588. */
  589. if (cpumask_test_cpu(cpu, mm_cpumask(mm))) {
  590. atomic_dec(&mm->context.active_cpus);
  591. cpumask_clear_cpu(cpu, mm_cpumask(mm));
  592. always_flush = true;
  593. }
  594. out:
  595. if (always_flush)
  596. _tlbiel_pid(pid, RIC_FLUSH_ALL);
  597. }
  598. #ifdef CONFIG_SMP
  599. static void do_exit_flush_lazy_tlb(void *arg)
  600. {
  601. struct mm_struct *mm = arg;
  602. exit_lazy_flush_tlb(mm, true);
  603. }
  604. static void exit_flush_lazy_tlbs(struct mm_struct *mm)
  605. {
  606. /*
  607. * Would be nice if this was async so it could be run in
  608. * parallel with our local flush, but generic code does not
  609. * give a good API for it. Could extend the generic code or
  610. * make a special powerpc IPI for flushing TLBs.
  611. * For now it's not too performance critical.
  612. */
  613. smp_call_function_many(mm_cpumask(mm), do_exit_flush_lazy_tlb,
  614. (void *)mm, 1);
  615. }
  616. #else /* CONFIG_SMP */
  617. static inline void exit_flush_lazy_tlbs(struct mm_struct *mm) { }
  618. #endif /* CONFIG_SMP */
  619. static DEFINE_PER_CPU(unsigned int, mm_cpumask_trim_clock);
  620. /*
  621. * Interval between flushes at which we send out IPIs to check whether the
  622. * mm_cpumask can be trimmed for the case where it's not a single-threaded
  623. * process flushing its own mm. The intent is to reduce the cost of later
  624. * flushes. Don't want this to be so low that it adds noticable cost to TLB
  625. * flushing, or so high that it doesn't help reduce global TLBIEs.
  626. */
  627. static unsigned long tlb_mm_cpumask_trim_timer = 1073;
  628. static bool tick_and_test_trim_clock(void)
  629. {
  630. if (__this_cpu_inc_return(mm_cpumask_trim_clock) ==
  631. tlb_mm_cpumask_trim_timer) {
  632. __this_cpu_write(mm_cpumask_trim_clock, 0);
  633. return true;
  634. }
  635. return false;
  636. }
  637. enum tlb_flush_type {
  638. FLUSH_TYPE_NONE,
  639. FLUSH_TYPE_LOCAL,
  640. FLUSH_TYPE_GLOBAL,
  641. };
  642. static enum tlb_flush_type flush_type_needed(struct mm_struct *mm, bool fullmm)
  643. {
  644. int active_cpus = atomic_read(&mm->context.active_cpus);
  645. int cpu = smp_processor_id();
  646. if (active_cpus == 0)
  647. return FLUSH_TYPE_NONE;
  648. if (active_cpus == 1 && cpumask_test_cpu(cpu, mm_cpumask(mm))) {
  649. if (current->mm != mm) {
  650. /*
  651. * Asynchronous flush sources may trim down to nothing
  652. * if the process is not running, so occasionally try
  653. * to trim.
  654. */
  655. if (tick_and_test_trim_clock()) {
  656. exit_lazy_flush_tlb(mm, true);
  657. return FLUSH_TYPE_NONE;
  658. }
  659. }
  660. return FLUSH_TYPE_LOCAL;
  661. }
  662. /* Coprocessors require TLBIE to invalidate nMMU. */
  663. if (atomic_read(&mm->context.copros) > 0)
  664. return FLUSH_TYPE_GLOBAL;
  665. /*
  666. * In the fullmm case there's no point doing the exit_flush_lazy_tlbs
  667. * because the mm is being taken down anyway, and a TLBIE tends to
  668. * be faster than an IPI+TLBIEL.
  669. */
  670. if (fullmm)
  671. return FLUSH_TYPE_GLOBAL;
  672. /*
  673. * If we are running the only thread of a single-threaded process,
  674. * then we should almost always be able to trim off the rest of the
  675. * CPU mask (except in the case of use_mm() races), so always try
  676. * trimming the mask.
  677. */
  678. if (atomic_read(&mm->mm_users) <= 1 && current->mm == mm) {
  679. exit_flush_lazy_tlbs(mm);
  680. /*
  681. * use_mm() race could prevent IPIs from being able to clear
  682. * the cpumask here, however those users are established
  683. * after our first check (and so after the PTEs are removed),
  684. * and the TLB still gets flushed by the IPI, so this CPU
  685. * will only require a local flush.
  686. */
  687. return FLUSH_TYPE_LOCAL;
  688. }
  689. /*
  690. * Occasionally try to trim down the cpumask. It's possible this can
  691. * bring the mask to zero, which results in no flush.
  692. */
  693. if (tick_and_test_trim_clock()) {
  694. exit_flush_lazy_tlbs(mm);
  695. if (current->mm == mm)
  696. return FLUSH_TYPE_LOCAL;
  697. if (cpumask_test_cpu(cpu, mm_cpumask(mm)))
  698. exit_lazy_flush_tlb(mm, true);
  699. return FLUSH_TYPE_NONE;
  700. }
  701. return FLUSH_TYPE_GLOBAL;
  702. }
  703. #ifdef CONFIG_SMP
  704. void radix__flush_tlb_mm(struct mm_struct *mm)
  705. {
  706. unsigned long pid;
  707. enum tlb_flush_type type;
  708. pid = mm->context.id;
  709. if (unlikely(pid == MMU_NO_CONTEXT))
  710. return;
  711. preempt_disable();
  712. /*
  713. * Order loads of mm_cpumask (in flush_type_needed) vs previous
  714. * stores to clear ptes before the invalidate. See barrier in
  715. * switch_mm_irqs_off
  716. */
  717. smp_mb();
  718. type = flush_type_needed(mm, false);
  719. if (type == FLUSH_TYPE_LOCAL) {
  720. _tlbiel_pid(pid, RIC_FLUSH_TLB);
  721. } else if (type == FLUSH_TYPE_GLOBAL) {
  722. if (!mmu_has_feature(MMU_FTR_GTSE)) {
  723. unsigned long tgt = H_RPTI_TARGET_CMMU;
  724. if (atomic_read(&mm->context.copros) > 0)
  725. tgt |= H_RPTI_TARGET_NMMU;
  726. pseries_rpt_invalidate(pid, tgt, H_RPTI_TYPE_TLB,
  727. H_RPTI_PAGE_ALL, 0, -1UL);
  728. } else if (cputlb_use_tlbie()) {
  729. if (mm_needs_flush_escalation(mm))
  730. _tlbie_pid(pid, RIC_FLUSH_ALL);
  731. else
  732. _tlbie_pid(pid, RIC_FLUSH_TLB);
  733. } else {
  734. _tlbiel_pid_multicast(mm, pid, RIC_FLUSH_TLB);
  735. }
  736. }
  737. preempt_enable();
  738. }
  739. EXPORT_SYMBOL(radix__flush_tlb_mm);
  740. static void __flush_all_mm(struct mm_struct *mm, bool fullmm)
  741. {
  742. unsigned long pid;
  743. enum tlb_flush_type type;
  744. pid = mm->context.id;
  745. if (unlikely(pid == MMU_NO_CONTEXT))
  746. return;
  747. preempt_disable();
  748. smp_mb(); /* see radix__flush_tlb_mm */
  749. type = flush_type_needed(mm, fullmm);
  750. if (type == FLUSH_TYPE_LOCAL) {
  751. _tlbiel_pid(pid, RIC_FLUSH_ALL);
  752. } else if (type == FLUSH_TYPE_GLOBAL) {
  753. if (!mmu_has_feature(MMU_FTR_GTSE)) {
  754. unsigned long tgt = H_RPTI_TARGET_CMMU;
  755. unsigned long type = H_RPTI_TYPE_TLB | H_RPTI_TYPE_PWC |
  756. H_RPTI_TYPE_PRT;
  757. if (atomic_read(&mm->context.copros) > 0)
  758. tgt |= H_RPTI_TARGET_NMMU;
  759. pseries_rpt_invalidate(pid, tgt, type,
  760. H_RPTI_PAGE_ALL, 0, -1UL);
  761. } else if (cputlb_use_tlbie())
  762. _tlbie_pid(pid, RIC_FLUSH_ALL);
  763. else
  764. _tlbiel_pid_multicast(mm, pid, RIC_FLUSH_ALL);
  765. }
  766. preempt_enable();
  767. }
  768. void radix__flush_all_mm(struct mm_struct *mm)
  769. {
  770. __flush_all_mm(mm, false);
  771. }
  772. EXPORT_SYMBOL(radix__flush_all_mm);
  773. void radix__flush_tlb_page_psize(struct mm_struct *mm, unsigned long vmaddr,
  774. int psize)
  775. {
  776. unsigned long pid;
  777. enum tlb_flush_type type;
  778. pid = mm->context.id;
  779. if (unlikely(pid == MMU_NO_CONTEXT))
  780. return;
  781. preempt_disable();
  782. smp_mb(); /* see radix__flush_tlb_mm */
  783. type = flush_type_needed(mm, false);
  784. if (type == FLUSH_TYPE_LOCAL) {
  785. _tlbiel_va(vmaddr, pid, psize, RIC_FLUSH_TLB);
  786. } else if (type == FLUSH_TYPE_GLOBAL) {
  787. if (!mmu_has_feature(MMU_FTR_GTSE)) {
  788. unsigned long tgt, pg_sizes, size;
  789. tgt = H_RPTI_TARGET_CMMU;
  790. pg_sizes = psize_to_rpti_pgsize(psize);
  791. size = 1UL << mmu_psize_to_shift(psize);
  792. if (atomic_read(&mm->context.copros) > 0)
  793. tgt |= H_RPTI_TARGET_NMMU;
  794. pseries_rpt_invalidate(pid, tgt, H_RPTI_TYPE_TLB,
  795. pg_sizes, vmaddr,
  796. vmaddr + size);
  797. } else if (cputlb_use_tlbie())
  798. _tlbie_va(vmaddr, pid, psize, RIC_FLUSH_TLB);
  799. else
  800. _tlbiel_va_multicast(mm, vmaddr, pid, psize, RIC_FLUSH_TLB);
  801. }
  802. preempt_enable();
  803. }
  804. void radix__flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
  805. {
  806. #ifdef CONFIG_HUGETLB_PAGE
  807. if (is_vm_hugetlb_page(vma))
  808. return radix__flush_hugetlb_page(vma, vmaddr);
  809. #endif
  810. radix__flush_tlb_page_psize(vma->vm_mm, vmaddr, mmu_virtual_psize);
  811. }
  812. EXPORT_SYMBOL(radix__flush_tlb_page);
  813. #endif /* CONFIG_SMP */
  814. static void do_tlbiel_kernel(void *info)
  815. {
  816. _tlbiel_pid(0, RIC_FLUSH_ALL);
  817. }
  818. static inline void _tlbiel_kernel_broadcast(void)
  819. {
  820. on_each_cpu(do_tlbiel_kernel, NULL, 1);
  821. if (tlbie_capable) {
  822. /*
  823. * Coherent accelerators don't refcount kernel memory mappings,
  824. * so have to always issue a tlbie for them. This is quite a
  825. * slow path anyway.
  826. */
  827. _tlbie_pid(0, RIC_FLUSH_ALL);
  828. }
  829. }
  830. /*
  831. * If kernel TLBIs ever become local rather than global, then
  832. * drivers/misc/ocxl/link.c:ocxl_link_add_pe will need some work, as it
  833. * assumes kernel TLBIs are global.
  834. */
  835. void radix__flush_tlb_kernel_range(unsigned long start, unsigned long end)
  836. {
  837. if (!mmu_has_feature(MMU_FTR_GTSE)) {
  838. unsigned long tgt = H_RPTI_TARGET_CMMU | H_RPTI_TARGET_NMMU;
  839. unsigned long type = H_RPTI_TYPE_TLB | H_RPTI_TYPE_PWC |
  840. H_RPTI_TYPE_PRT;
  841. pseries_rpt_invalidate(0, tgt, type, H_RPTI_PAGE_ALL,
  842. start, end);
  843. } else if (cputlb_use_tlbie())
  844. _tlbie_pid(0, RIC_FLUSH_ALL);
  845. else
  846. _tlbiel_kernel_broadcast();
  847. }
  848. EXPORT_SYMBOL(radix__flush_tlb_kernel_range);
  849. #define TLB_FLUSH_ALL -1UL
  850. /*
  851. * Number of pages above which we invalidate the entire PID rather than
  852. * flush individual pages, for local and global flushes respectively.
  853. *
  854. * tlbie goes out to the interconnect and individual ops are more costly.
  855. * It also does not iterate over sets like the local tlbiel variant when
  856. * invalidating a full PID, so it has a far lower threshold to change from
  857. * individual page flushes to full-pid flushes.
  858. */
  859. static u32 tlb_single_page_flush_ceiling __read_mostly = 33;
  860. static u32 tlb_local_single_page_flush_ceiling __read_mostly = POWER9_TLB_SETS_RADIX * 2;
  861. static inline void __radix__flush_tlb_range(struct mm_struct *mm,
  862. unsigned long start, unsigned long end)
  863. {
  864. unsigned long pid;
  865. unsigned int page_shift = mmu_psize_defs[mmu_virtual_psize].shift;
  866. unsigned long page_size = 1UL << page_shift;
  867. unsigned long nr_pages = (end - start) >> page_shift;
  868. bool fullmm = (end == TLB_FLUSH_ALL);
  869. bool flush_pid, flush_pwc = false;
  870. enum tlb_flush_type type;
  871. pid = mm->context.id;
  872. if (unlikely(pid == MMU_NO_CONTEXT))
  873. return;
  874. preempt_disable();
  875. smp_mb(); /* see radix__flush_tlb_mm */
  876. type = flush_type_needed(mm, fullmm);
  877. if (type == FLUSH_TYPE_NONE)
  878. goto out;
  879. if (fullmm)
  880. flush_pid = true;
  881. else if (type == FLUSH_TYPE_GLOBAL)
  882. flush_pid = nr_pages > tlb_single_page_flush_ceiling;
  883. else
  884. flush_pid = nr_pages > tlb_local_single_page_flush_ceiling;
  885. /*
  886. * full pid flush already does the PWC flush. if it is not full pid
  887. * flush check the range is more than PMD and force a pwc flush
  888. * mremap() depends on this behaviour.
  889. */
  890. if (!flush_pid && (end - start) >= PMD_SIZE)
  891. flush_pwc = true;
  892. if (!mmu_has_feature(MMU_FTR_GTSE) && type == FLUSH_TYPE_GLOBAL) {
  893. unsigned long type = H_RPTI_TYPE_TLB;
  894. unsigned long tgt = H_RPTI_TARGET_CMMU;
  895. unsigned long pg_sizes = psize_to_rpti_pgsize(mmu_virtual_psize);
  896. if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
  897. pg_sizes |= psize_to_rpti_pgsize(MMU_PAGE_2M);
  898. if (atomic_read(&mm->context.copros) > 0)
  899. tgt |= H_RPTI_TARGET_NMMU;
  900. if (flush_pwc)
  901. type |= H_RPTI_TYPE_PWC;
  902. pseries_rpt_invalidate(pid, tgt, type, pg_sizes, start, end);
  903. } else if (flush_pid) {
  904. /*
  905. * We are now flushing a range larger than PMD size force a RIC_FLUSH_ALL
  906. */
  907. if (type == FLUSH_TYPE_LOCAL) {
  908. _tlbiel_pid(pid, RIC_FLUSH_ALL);
  909. } else {
  910. if (cputlb_use_tlbie()) {
  911. _tlbie_pid(pid, RIC_FLUSH_ALL);
  912. } else {
  913. _tlbiel_pid_multicast(mm, pid, RIC_FLUSH_ALL);
  914. }
  915. }
  916. } else {
  917. bool hflush;
  918. unsigned long hstart, hend;
  919. hstart = (start + PMD_SIZE - 1) & PMD_MASK;
  920. hend = end & PMD_MASK;
  921. hflush = IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && hstart < hend;
  922. if (type == FLUSH_TYPE_LOCAL) {
  923. asm volatile("ptesync": : :"memory");
  924. if (flush_pwc)
  925. /* For PWC, only one flush is needed */
  926. __tlbiel_pid(pid, 0, RIC_FLUSH_PWC);
  927. __tlbiel_va_range(start, end, pid, page_size, mmu_virtual_psize);
  928. if (hflush)
  929. __tlbiel_va_range(hstart, hend, pid,
  930. PMD_SIZE, MMU_PAGE_2M);
  931. ppc_after_tlbiel_barrier();
  932. } else if (cputlb_use_tlbie()) {
  933. asm volatile("ptesync": : :"memory");
  934. if (flush_pwc)
  935. __tlbie_pid(pid, RIC_FLUSH_PWC);
  936. __tlbie_va_range(start, end, pid, page_size, mmu_virtual_psize);
  937. if (hflush)
  938. __tlbie_va_range(hstart, hend, pid,
  939. PMD_SIZE, MMU_PAGE_2M);
  940. asm volatile("eieio; tlbsync; ptesync": : :"memory");
  941. } else {
  942. _tlbiel_va_range_multicast(mm,
  943. start, end, pid, page_size, mmu_virtual_psize, flush_pwc);
  944. if (hflush)
  945. _tlbiel_va_range_multicast(mm,
  946. hstart, hend, pid, PMD_SIZE, MMU_PAGE_2M, flush_pwc);
  947. }
  948. }
  949. out:
  950. preempt_enable();
  951. }
  952. void radix__flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
  953. unsigned long end)
  954. {
  955. #ifdef CONFIG_HUGETLB_PAGE
  956. if (is_vm_hugetlb_page(vma))
  957. return radix__flush_hugetlb_tlb_range(vma, start, end);
  958. #endif
  959. __radix__flush_tlb_range(vma->vm_mm, start, end);
  960. }
  961. EXPORT_SYMBOL(radix__flush_tlb_range);
  962. static int radix_get_mmu_psize(int page_size)
  963. {
  964. int psize;
  965. if (page_size == (1UL << mmu_psize_defs[mmu_virtual_psize].shift))
  966. psize = mmu_virtual_psize;
  967. else if (page_size == (1UL << mmu_psize_defs[MMU_PAGE_2M].shift))
  968. psize = MMU_PAGE_2M;
  969. else if (page_size == (1UL << mmu_psize_defs[MMU_PAGE_1G].shift))
  970. psize = MMU_PAGE_1G;
  971. else
  972. return -1;
  973. return psize;
  974. }
  975. /*
  976. * Flush partition scoped LPID address translation for all CPUs.
  977. */
  978. void radix__flush_tlb_lpid_page(unsigned int lpid,
  979. unsigned long addr,
  980. unsigned long page_size)
  981. {
  982. int psize = radix_get_mmu_psize(page_size);
  983. _tlbie_lpid_va(addr, lpid, psize, RIC_FLUSH_TLB);
  984. }
  985. EXPORT_SYMBOL_GPL(radix__flush_tlb_lpid_page);
  986. /*
  987. * Flush partition scoped PWC from LPID for all CPUs.
  988. */
  989. void radix__flush_pwc_lpid(unsigned int lpid)
  990. {
  991. _tlbie_lpid(lpid, RIC_FLUSH_PWC);
  992. }
  993. EXPORT_SYMBOL_GPL(radix__flush_pwc_lpid);
  994. /*
  995. * Flush partition scoped translations from LPID (=LPIDR)
  996. */
  997. void radix__flush_all_lpid(unsigned int lpid)
  998. {
  999. _tlbie_lpid(lpid, RIC_FLUSH_ALL);
  1000. }
  1001. EXPORT_SYMBOL_GPL(radix__flush_all_lpid);
  1002. /*
  1003. * Flush process scoped translations from LPID (=LPIDR)
  1004. */
  1005. void radix__flush_all_lpid_guest(unsigned int lpid)
  1006. {
  1007. _tlbie_lpid_guest(lpid, RIC_FLUSH_ALL);
  1008. }
  1009. void radix__tlb_flush(struct mmu_gather *tlb)
  1010. {
  1011. int psize = 0;
  1012. struct mm_struct *mm = tlb->mm;
  1013. int page_size = tlb->page_size;
  1014. unsigned long start = tlb->start;
  1015. unsigned long end = tlb->end;
  1016. /*
  1017. * if page size is not something we understand, do a full mm flush
  1018. *
  1019. * A "fullmm" flush must always do a flush_all_mm (RIC=2) flush
  1020. * that flushes the process table entry cache upon process teardown.
  1021. * See the comment for radix in arch_exit_mmap().
  1022. */
  1023. if (tlb->fullmm || tlb->need_flush_all) {
  1024. __flush_all_mm(mm, true);
  1025. } else if ( (psize = radix_get_mmu_psize(page_size)) == -1) {
  1026. if (!tlb->freed_tables)
  1027. radix__flush_tlb_mm(mm);
  1028. else
  1029. radix__flush_all_mm(mm);
  1030. } else {
  1031. if (!tlb->freed_tables)
  1032. radix__flush_tlb_range_psize(mm, start, end, psize);
  1033. else
  1034. radix__flush_tlb_pwc_range_psize(mm, start, end, psize);
  1035. }
  1036. }
  1037. static void __radix__flush_tlb_range_psize(struct mm_struct *mm,
  1038. unsigned long start, unsigned long end,
  1039. int psize, bool also_pwc)
  1040. {
  1041. unsigned long pid;
  1042. unsigned int page_shift = mmu_psize_defs[psize].shift;
  1043. unsigned long page_size = 1UL << page_shift;
  1044. unsigned long nr_pages = (end - start) >> page_shift;
  1045. bool fullmm = (end == TLB_FLUSH_ALL);
  1046. bool flush_pid;
  1047. enum tlb_flush_type type;
  1048. pid = mm->context.id;
  1049. if (unlikely(pid == MMU_NO_CONTEXT))
  1050. return;
  1051. fullmm = (end == TLB_FLUSH_ALL);
  1052. preempt_disable();
  1053. smp_mb(); /* see radix__flush_tlb_mm */
  1054. type = flush_type_needed(mm, fullmm);
  1055. if (type == FLUSH_TYPE_NONE)
  1056. goto out;
  1057. if (fullmm)
  1058. flush_pid = true;
  1059. else if (type == FLUSH_TYPE_GLOBAL)
  1060. flush_pid = nr_pages > tlb_single_page_flush_ceiling;
  1061. else
  1062. flush_pid = nr_pages > tlb_local_single_page_flush_ceiling;
  1063. if (!mmu_has_feature(MMU_FTR_GTSE) && type == FLUSH_TYPE_GLOBAL) {
  1064. unsigned long tgt = H_RPTI_TARGET_CMMU;
  1065. unsigned long type = H_RPTI_TYPE_TLB;
  1066. unsigned long pg_sizes = psize_to_rpti_pgsize(psize);
  1067. if (also_pwc)
  1068. type |= H_RPTI_TYPE_PWC;
  1069. if (atomic_read(&mm->context.copros) > 0)
  1070. tgt |= H_RPTI_TARGET_NMMU;
  1071. pseries_rpt_invalidate(pid, tgt, type, pg_sizes, start, end);
  1072. } else if (flush_pid) {
  1073. if (type == FLUSH_TYPE_LOCAL) {
  1074. _tlbiel_pid(pid, also_pwc ? RIC_FLUSH_ALL : RIC_FLUSH_TLB);
  1075. } else {
  1076. if (cputlb_use_tlbie()) {
  1077. if (mm_needs_flush_escalation(mm))
  1078. also_pwc = true;
  1079. _tlbie_pid(pid,
  1080. also_pwc ? RIC_FLUSH_ALL : RIC_FLUSH_TLB);
  1081. } else {
  1082. _tlbiel_pid_multicast(mm, pid,
  1083. also_pwc ? RIC_FLUSH_ALL : RIC_FLUSH_TLB);
  1084. }
  1085. }
  1086. } else {
  1087. if (type == FLUSH_TYPE_LOCAL)
  1088. _tlbiel_va_range(start, end, pid, page_size, psize, also_pwc);
  1089. else if (cputlb_use_tlbie())
  1090. _tlbie_va_range(start, end, pid, page_size, psize, also_pwc);
  1091. else
  1092. _tlbiel_va_range_multicast(mm,
  1093. start, end, pid, page_size, psize, also_pwc);
  1094. }
  1095. out:
  1096. preempt_enable();
  1097. }
  1098. void radix__flush_tlb_range_psize(struct mm_struct *mm, unsigned long start,
  1099. unsigned long end, int psize)
  1100. {
  1101. return __radix__flush_tlb_range_psize(mm, start, end, psize, false);
  1102. }
  1103. void radix__flush_tlb_pwc_range_psize(struct mm_struct *mm, unsigned long start,
  1104. unsigned long end, int psize)
  1105. {
  1106. __radix__flush_tlb_range_psize(mm, start, end, psize, true);
  1107. }
  1108. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  1109. void radix__flush_tlb_collapsed_pmd(struct mm_struct *mm, unsigned long addr)
  1110. {
  1111. unsigned long pid, end;
  1112. enum tlb_flush_type type;
  1113. pid = mm->context.id;
  1114. if (unlikely(pid == MMU_NO_CONTEXT))
  1115. return;
  1116. /* 4k page size, just blow the world */
  1117. if (PAGE_SIZE == 0x1000) {
  1118. radix__flush_all_mm(mm);
  1119. return;
  1120. }
  1121. end = addr + HPAGE_PMD_SIZE;
  1122. /* Otherwise first do the PWC, then iterate the pages. */
  1123. preempt_disable();
  1124. smp_mb(); /* see radix__flush_tlb_mm */
  1125. type = flush_type_needed(mm, false);
  1126. if (type == FLUSH_TYPE_LOCAL) {
  1127. _tlbiel_va_range(addr, end, pid, PAGE_SIZE, mmu_virtual_psize, true);
  1128. } else if (type == FLUSH_TYPE_GLOBAL) {
  1129. if (!mmu_has_feature(MMU_FTR_GTSE)) {
  1130. unsigned long tgt, type, pg_sizes;
  1131. tgt = H_RPTI_TARGET_CMMU;
  1132. type = H_RPTI_TYPE_TLB | H_RPTI_TYPE_PWC |
  1133. H_RPTI_TYPE_PRT;
  1134. pg_sizes = psize_to_rpti_pgsize(mmu_virtual_psize);
  1135. if (atomic_read(&mm->context.copros) > 0)
  1136. tgt |= H_RPTI_TARGET_NMMU;
  1137. pseries_rpt_invalidate(pid, tgt, type, pg_sizes,
  1138. addr, end);
  1139. } else if (cputlb_use_tlbie())
  1140. _tlbie_va_range(addr, end, pid, PAGE_SIZE, mmu_virtual_psize, true);
  1141. else
  1142. _tlbiel_va_range_multicast(mm,
  1143. addr, end, pid, PAGE_SIZE, mmu_virtual_psize, true);
  1144. }
  1145. preempt_enable();
  1146. }
  1147. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  1148. void radix__flush_pmd_tlb_range(struct vm_area_struct *vma,
  1149. unsigned long start, unsigned long end)
  1150. {
  1151. radix__flush_tlb_range_psize(vma->vm_mm, start, end, MMU_PAGE_2M);
  1152. }
  1153. EXPORT_SYMBOL(radix__flush_pmd_tlb_range);
  1154. void radix__flush_tlb_all(void)
  1155. {
  1156. unsigned long rb,prs,r,rs;
  1157. unsigned long ric = RIC_FLUSH_ALL;
  1158. rb = 0x3 << PPC_BITLSHIFT(53); /* IS = 3 */
  1159. prs = 0; /* partition scoped */
  1160. r = 1; /* radix format */
  1161. rs = 1 & ((1UL << 32) - 1); /* any LPID value to flush guest mappings */
  1162. asm volatile("ptesync": : :"memory");
  1163. /*
  1164. * now flush guest entries by passing PRS = 1 and LPID != 0
  1165. */
  1166. asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
  1167. : : "r"(rb), "i"(r), "i"(1), "i"(ric), "r"(rs) : "memory");
  1168. /*
  1169. * now flush host entires by passing PRS = 0 and LPID == 0
  1170. */
  1171. asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
  1172. : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(0) : "memory");
  1173. asm volatile("eieio; tlbsync; ptesync": : :"memory");
  1174. }
  1175. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  1176. static __always_inline void __tlbie_pid_lpid(unsigned long pid,
  1177. unsigned long lpid,
  1178. unsigned long ric)
  1179. {
  1180. unsigned long rb, rs, prs, r;
  1181. rb = PPC_BIT(53); /* IS = 1 */
  1182. rs = (pid << PPC_BITLSHIFT(31)) | (lpid & ~(PPC_BITMASK(0, 31)));
  1183. prs = 1; /* process scoped */
  1184. r = 1; /* radix format */
  1185. asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
  1186. : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
  1187. trace_tlbie(0, 0, rb, rs, ric, prs, r);
  1188. }
  1189. static __always_inline void __tlbie_va_lpid(unsigned long va, unsigned long pid,
  1190. unsigned long lpid,
  1191. unsigned long ap, unsigned long ric)
  1192. {
  1193. unsigned long rb, rs, prs, r;
  1194. rb = va & ~(PPC_BITMASK(52, 63));
  1195. rb |= ap << PPC_BITLSHIFT(58);
  1196. rs = (pid << PPC_BITLSHIFT(31)) | (lpid & ~(PPC_BITMASK(0, 31)));
  1197. prs = 1; /* process scoped */
  1198. r = 1; /* radix format */
  1199. asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
  1200. : : "r"(rb), "i"(r), "i"(prs), "i"(ric), "r"(rs) : "memory");
  1201. trace_tlbie(0, 0, rb, rs, ric, prs, r);
  1202. }
  1203. static inline void fixup_tlbie_pid_lpid(unsigned long pid, unsigned long lpid)
  1204. {
  1205. /*
  1206. * We can use any address for the invalidation, pick one which is
  1207. * probably unused as an optimisation.
  1208. */
  1209. unsigned long va = ((1UL << 52) - 1);
  1210. if (cpu_has_feature(CPU_FTR_P9_TLBIE_ERAT_BUG)) {
  1211. asm volatile("ptesync" : : : "memory");
  1212. __tlbie_pid_lpid(0, lpid, RIC_FLUSH_TLB);
  1213. }
  1214. if (cpu_has_feature(CPU_FTR_P9_TLBIE_STQ_BUG)) {
  1215. asm volatile("ptesync" : : : "memory");
  1216. __tlbie_va_lpid(va, pid, lpid, mmu_get_ap(MMU_PAGE_64K),
  1217. RIC_FLUSH_TLB);
  1218. }
  1219. }
  1220. static inline void _tlbie_pid_lpid(unsigned long pid, unsigned long lpid,
  1221. unsigned long ric)
  1222. {
  1223. asm volatile("ptesync" : : : "memory");
  1224. /*
  1225. * Workaround the fact that the "ric" argument to __tlbie_pid
  1226. * must be a compile-time contraint to match the "i" constraint
  1227. * in the asm statement.
  1228. */
  1229. switch (ric) {
  1230. case RIC_FLUSH_TLB:
  1231. __tlbie_pid_lpid(pid, lpid, RIC_FLUSH_TLB);
  1232. fixup_tlbie_pid_lpid(pid, lpid);
  1233. break;
  1234. case RIC_FLUSH_PWC:
  1235. __tlbie_pid_lpid(pid, lpid, RIC_FLUSH_PWC);
  1236. break;
  1237. case RIC_FLUSH_ALL:
  1238. default:
  1239. __tlbie_pid_lpid(pid, lpid, RIC_FLUSH_ALL);
  1240. fixup_tlbie_pid_lpid(pid, lpid);
  1241. }
  1242. asm volatile("eieio; tlbsync; ptesync" : : : "memory");
  1243. }
  1244. static inline void fixup_tlbie_va_range_lpid(unsigned long va,
  1245. unsigned long pid,
  1246. unsigned long lpid,
  1247. unsigned long ap)
  1248. {
  1249. if (cpu_has_feature(CPU_FTR_P9_TLBIE_ERAT_BUG)) {
  1250. asm volatile("ptesync" : : : "memory");
  1251. __tlbie_pid_lpid(0, lpid, RIC_FLUSH_TLB);
  1252. }
  1253. if (cpu_has_feature(CPU_FTR_P9_TLBIE_STQ_BUG)) {
  1254. asm volatile("ptesync" : : : "memory");
  1255. __tlbie_va_lpid(va, pid, lpid, ap, RIC_FLUSH_TLB);
  1256. }
  1257. }
  1258. static inline void __tlbie_va_range_lpid(unsigned long start, unsigned long end,
  1259. unsigned long pid, unsigned long lpid,
  1260. unsigned long page_size,
  1261. unsigned long psize)
  1262. {
  1263. unsigned long addr;
  1264. unsigned long ap = mmu_get_ap(psize);
  1265. for (addr = start; addr < end; addr += page_size)
  1266. __tlbie_va_lpid(addr, pid, lpid, ap, RIC_FLUSH_TLB);
  1267. fixup_tlbie_va_range_lpid(addr - page_size, pid, lpid, ap);
  1268. }
  1269. static inline void _tlbie_va_range_lpid(unsigned long start, unsigned long end,
  1270. unsigned long pid, unsigned long lpid,
  1271. unsigned long page_size,
  1272. unsigned long psize, bool also_pwc)
  1273. {
  1274. asm volatile("ptesync" : : : "memory");
  1275. if (also_pwc)
  1276. __tlbie_pid_lpid(pid, lpid, RIC_FLUSH_PWC);
  1277. __tlbie_va_range_lpid(start, end, pid, lpid, page_size, psize);
  1278. asm volatile("eieio; tlbsync; ptesync" : : : "memory");
  1279. }
  1280. /*
  1281. * Performs process-scoped invalidations for a given LPID
  1282. * as part of H_RPT_INVALIDATE hcall.
  1283. */
  1284. void do_h_rpt_invalidate_prt(unsigned long pid, unsigned long lpid,
  1285. unsigned long type, unsigned long pg_sizes,
  1286. unsigned long start, unsigned long end)
  1287. {
  1288. unsigned long psize, nr_pages;
  1289. struct mmu_psize_def *def;
  1290. bool flush_pid;
  1291. /*
  1292. * A H_RPTI_TYPE_ALL request implies RIC=3, hence
  1293. * do a single IS=1 based flush.
  1294. */
  1295. if ((type & H_RPTI_TYPE_ALL) == H_RPTI_TYPE_ALL) {
  1296. _tlbie_pid_lpid(pid, lpid, RIC_FLUSH_ALL);
  1297. return;
  1298. }
  1299. if (type & H_RPTI_TYPE_PWC)
  1300. _tlbie_pid_lpid(pid, lpid, RIC_FLUSH_PWC);
  1301. /* Full PID flush */
  1302. if (start == 0 && end == -1)
  1303. return _tlbie_pid_lpid(pid, lpid, RIC_FLUSH_TLB);
  1304. /* Do range invalidation for all the valid page sizes */
  1305. for (psize = 0; psize < MMU_PAGE_COUNT; psize++) {
  1306. def = &mmu_psize_defs[psize];
  1307. if (!(pg_sizes & def->h_rpt_pgsize))
  1308. continue;
  1309. nr_pages = (end - start) >> def->shift;
  1310. flush_pid = nr_pages > tlb_single_page_flush_ceiling;
  1311. /*
  1312. * If the number of pages spanning the range is above
  1313. * the ceiling, convert the request into a full PID flush.
  1314. * And since PID flush takes out all the page sizes, there
  1315. * is no need to consider remaining page sizes.
  1316. */
  1317. if (flush_pid) {
  1318. _tlbie_pid_lpid(pid, lpid, RIC_FLUSH_TLB);
  1319. return;
  1320. }
  1321. _tlbie_va_range_lpid(start, end, pid, lpid,
  1322. (1UL << def->shift), psize, false);
  1323. }
  1324. }
  1325. EXPORT_SYMBOL_GPL(do_h_rpt_invalidate_prt);
  1326. #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
  1327. static int __init create_tlb_single_page_flush_ceiling(void)
  1328. {
  1329. debugfs_create_u32("tlb_single_page_flush_ceiling", 0600,
  1330. arch_debugfs_dir, &tlb_single_page_flush_ceiling);
  1331. debugfs_create_u32("tlb_local_single_page_flush_ceiling", 0600,
  1332. arch_debugfs_dir, &tlb_local_single_page_flush_ceiling);
  1333. return 0;
  1334. }
  1335. late_initcall(create_tlb_single_page_flush_ceiling);