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