tlb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * TLB support routines.
  4. *
  5. * Copyright (C) 1998-2001, 2003 Hewlett-Packard Co
  6. * David Mosberger-Tang <[email protected]>
  7. *
  8. * 08/02/00 A. Mallick <[email protected]>
  9. * Modified RID allocation for SMP
  10. * Goutham Rao <[email protected]>
  11. * IPI based ptc implementation and A-step IPI implementation.
  12. * Rohit Seth <[email protected]>
  13. * Ken Chen <[email protected]>
  14. * Christophe de Dinechin <[email protected]>: Avoid ptc.e on memory allocation
  15. * Copyright (C) 2007 Intel Corp
  16. * Fenghua Yu <[email protected]>
  17. * Add multiple ptc.g/ptc.ga instruction support in global tlb purge.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/kernel.h>
  22. #include <linux/sched.h>
  23. #include <linux/smp.h>
  24. #include <linux/mm.h>
  25. #include <linux/memblock.h>
  26. #include <linux/slab.h>
  27. #include <asm/delay.h>
  28. #include <asm/mmu_context.h>
  29. #include <asm/pal.h>
  30. #include <asm/tlbflush.h>
  31. #include <asm/dma.h>
  32. #include <asm/processor.h>
  33. #include <asm/sal.h>
  34. #include <asm/tlb.h>
  35. static struct {
  36. u64 mask; /* mask of supported purge page-sizes */
  37. unsigned long max_bits; /* log2 of largest supported purge page-size */
  38. } purge;
  39. struct ia64_ctx ia64_ctx = {
  40. .lock = __SPIN_LOCK_UNLOCKED(ia64_ctx.lock),
  41. .next = 1,
  42. .max_ctx = ~0U
  43. };
  44. DEFINE_PER_CPU(u8, ia64_need_tlb_flush);
  45. DEFINE_PER_CPU(u8, ia64_tr_num); /*Number of TR slots in current processor*/
  46. DEFINE_PER_CPU(u8, ia64_tr_used); /*Max Slot number used by kernel*/
  47. struct ia64_tr_entry *ia64_idtrs[NR_CPUS];
  48. /*
  49. * Initializes the ia64_ctx.bitmap array based on max_ctx+1.
  50. * Called after cpu_init() has setup ia64_ctx.max_ctx based on
  51. * maximum RID that is supported by boot CPU.
  52. */
  53. void __init
  54. mmu_context_init (void)
  55. {
  56. ia64_ctx.bitmap = memblock_alloc((ia64_ctx.max_ctx + 1) >> 3,
  57. SMP_CACHE_BYTES);
  58. if (!ia64_ctx.bitmap)
  59. panic("%s: Failed to allocate %u bytes\n", __func__,
  60. (ia64_ctx.max_ctx + 1) >> 3);
  61. ia64_ctx.flushmap = memblock_alloc((ia64_ctx.max_ctx + 1) >> 3,
  62. SMP_CACHE_BYTES);
  63. if (!ia64_ctx.flushmap)
  64. panic("%s: Failed to allocate %u bytes\n", __func__,
  65. (ia64_ctx.max_ctx + 1) >> 3);
  66. }
  67. /*
  68. * Acquire the ia64_ctx.lock before calling this function!
  69. */
  70. void
  71. wrap_mmu_context (struct mm_struct *mm)
  72. {
  73. int i, cpu;
  74. unsigned long flush_bit;
  75. for (i=0; i <= ia64_ctx.max_ctx / BITS_PER_LONG; i++) {
  76. flush_bit = xchg(&ia64_ctx.flushmap[i], 0);
  77. ia64_ctx.bitmap[i] ^= flush_bit;
  78. }
  79. /* use offset at 300 to skip daemons */
  80. ia64_ctx.next = find_next_zero_bit(ia64_ctx.bitmap,
  81. ia64_ctx.max_ctx, 300);
  82. ia64_ctx.limit = find_next_bit(ia64_ctx.bitmap,
  83. ia64_ctx.max_ctx, ia64_ctx.next);
  84. /*
  85. * can't call flush_tlb_all() here because of race condition
  86. * with O(1) scheduler [EF]
  87. */
  88. cpu = get_cpu(); /* prevent preemption/migration */
  89. for_each_online_cpu(i)
  90. if (i != cpu)
  91. per_cpu(ia64_need_tlb_flush, i) = 1;
  92. put_cpu();
  93. local_flush_tlb_all();
  94. }
  95. /*
  96. * Implement "spinaphores" ... like counting semaphores, but they
  97. * spin instead of sleeping. If there are ever any other users for
  98. * this primitive it can be moved up to a spinaphore.h header.
  99. */
  100. struct spinaphore {
  101. unsigned long ticket;
  102. unsigned long serve;
  103. };
  104. static inline void spinaphore_init(struct spinaphore *ss, int val)
  105. {
  106. ss->ticket = 0;
  107. ss->serve = val;
  108. }
  109. static inline void down_spin(struct spinaphore *ss)
  110. {
  111. unsigned long t = ia64_fetchadd(1, &ss->ticket, acq), serve;
  112. if (time_before(t, ss->serve))
  113. return;
  114. ia64_invala();
  115. for (;;) {
  116. asm volatile ("ld8.c.nc %0=[%1]" : "=r"(serve) : "r"(&ss->serve) : "memory");
  117. if (time_before(t, serve))
  118. return;
  119. cpu_relax();
  120. }
  121. }
  122. static inline void up_spin(struct spinaphore *ss)
  123. {
  124. ia64_fetchadd(1, &ss->serve, rel);
  125. }
  126. static struct spinaphore ptcg_sem;
  127. static u16 nptcg = 1;
  128. static int need_ptcg_sem = 1;
  129. static int toolatetochangeptcgsem = 0;
  130. /*
  131. * Kernel parameter "nptcg=" overrides max number of concurrent global TLB
  132. * purges which is reported from either PAL or SAL PALO.
  133. *
  134. * We don't have sanity checking for nptcg value. It's the user's responsibility
  135. * for valid nptcg value on the platform. Otherwise, kernel may hang in some
  136. * cases.
  137. */
  138. static int __init
  139. set_nptcg(char *str)
  140. {
  141. int value = 0;
  142. get_option(&str, &value);
  143. setup_ptcg_sem(value, NPTCG_FROM_KERNEL_PARAMETER);
  144. return 1;
  145. }
  146. __setup("nptcg=", set_nptcg);
  147. /*
  148. * Maximum number of simultaneous ptc.g purges in the system can
  149. * be defined by PAL_VM_SUMMARY (in which case we should take
  150. * the smallest value for any cpu in the system) or by the PAL
  151. * override table (in which case we should ignore the value from
  152. * PAL_VM_SUMMARY).
  153. *
  154. * Kernel parameter "nptcg=" overrides maximum number of simultaneous ptc.g
  155. * purges defined in either PAL_VM_SUMMARY or PAL override table. In this case,
  156. * we should ignore the value from either PAL_VM_SUMMARY or PAL override table.
  157. *
  158. * Complicating the logic here is the fact that num_possible_cpus()
  159. * isn't fully setup until we start bringing cpus online.
  160. */
  161. void
  162. setup_ptcg_sem(int max_purges, int nptcg_from)
  163. {
  164. static int kp_override;
  165. static int palo_override;
  166. static int firstcpu = 1;
  167. if (toolatetochangeptcgsem) {
  168. if (nptcg_from == NPTCG_FROM_PAL && max_purges == 0)
  169. BUG_ON(1 < nptcg);
  170. else
  171. BUG_ON(max_purges < nptcg);
  172. return;
  173. }
  174. if (nptcg_from == NPTCG_FROM_KERNEL_PARAMETER) {
  175. kp_override = 1;
  176. nptcg = max_purges;
  177. goto resetsema;
  178. }
  179. if (kp_override) {
  180. need_ptcg_sem = num_possible_cpus() > nptcg;
  181. return;
  182. }
  183. if (nptcg_from == NPTCG_FROM_PALO) {
  184. palo_override = 1;
  185. /* In PALO max_purges == 0 really means it! */
  186. if (max_purges == 0)
  187. panic("Whoa! Platform does not support global TLB purges.\n");
  188. nptcg = max_purges;
  189. if (nptcg == PALO_MAX_TLB_PURGES) {
  190. need_ptcg_sem = 0;
  191. return;
  192. }
  193. goto resetsema;
  194. }
  195. if (palo_override) {
  196. if (nptcg != PALO_MAX_TLB_PURGES)
  197. need_ptcg_sem = (num_possible_cpus() > nptcg);
  198. return;
  199. }
  200. /* In PAL_VM_SUMMARY max_purges == 0 actually means 1 */
  201. if (max_purges == 0) max_purges = 1;
  202. if (firstcpu) {
  203. nptcg = max_purges;
  204. firstcpu = 0;
  205. }
  206. if (max_purges < nptcg)
  207. nptcg = max_purges;
  208. if (nptcg == PAL_MAX_PURGES) {
  209. need_ptcg_sem = 0;
  210. return;
  211. } else
  212. need_ptcg_sem = (num_possible_cpus() > nptcg);
  213. resetsema:
  214. spinaphore_init(&ptcg_sem, max_purges);
  215. }
  216. #ifdef CONFIG_SMP
  217. static void
  218. ia64_global_tlb_purge (struct mm_struct *mm, unsigned long start,
  219. unsigned long end, unsigned long nbits)
  220. {
  221. struct mm_struct *active_mm = current->active_mm;
  222. toolatetochangeptcgsem = 1;
  223. if (mm != active_mm) {
  224. /* Restore region IDs for mm */
  225. if (mm && active_mm) {
  226. activate_context(mm);
  227. } else {
  228. flush_tlb_all();
  229. return;
  230. }
  231. }
  232. if (need_ptcg_sem)
  233. down_spin(&ptcg_sem);
  234. do {
  235. /*
  236. * Flush ALAT entries also.
  237. */
  238. ia64_ptcga(start, (nbits << 2));
  239. ia64_srlz_i();
  240. start += (1UL << nbits);
  241. } while (start < end);
  242. if (need_ptcg_sem)
  243. up_spin(&ptcg_sem);
  244. if (mm != active_mm) {
  245. activate_context(active_mm);
  246. }
  247. }
  248. #endif /* CONFIG_SMP */
  249. void
  250. local_flush_tlb_all (void)
  251. {
  252. unsigned long i, j, flags, count0, count1, stride0, stride1, addr;
  253. addr = local_cpu_data->ptce_base;
  254. count0 = local_cpu_data->ptce_count[0];
  255. count1 = local_cpu_data->ptce_count[1];
  256. stride0 = local_cpu_data->ptce_stride[0];
  257. stride1 = local_cpu_data->ptce_stride[1];
  258. local_irq_save(flags);
  259. for (i = 0; i < count0; ++i) {
  260. for (j = 0; j < count1; ++j) {
  261. ia64_ptce(addr);
  262. addr += stride1;
  263. }
  264. addr += stride0;
  265. }
  266. local_irq_restore(flags);
  267. ia64_srlz_i(); /* srlz.i implies srlz.d */
  268. }
  269. static void
  270. __flush_tlb_range (struct vm_area_struct *vma, unsigned long start,
  271. unsigned long end)
  272. {
  273. struct mm_struct *mm = vma->vm_mm;
  274. unsigned long size = end - start;
  275. unsigned long nbits;
  276. #ifndef CONFIG_SMP
  277. if (mm != current->active_mm) {
  278. mm->context = 0;
  279. return;
  280. }
  281. #endif
  282. nbits = ia64_fls(size + 0xfff);
  283. while (unlikely (((1UL << nbits) & purge.mask) == 0) &&
  284. (nbits < purge.max_bits))
  285. ++nbits;
  286. if (nbits > purge.max_bits)
  287. nbits = purge.max_bits;
  288. start &= ~((1UL << nbits) - 1);
  289. preempt_disable();
  290. #ifdef CONFIG_SMP
  291. if (mm != current->active_mm || cpumask_weight(mm_cpumask(mm)) != 1) {
  292. ia64_global_tlb_purge(mm, start, end, nbits);
  293. preempt_enable();
  294. return;
  295. }
  296. #endif
  297. do {
  298. ia64_ptcl(start, (nbits<<2));
  299. start += (1UL << nbits);
  300. } while (start < end);
  301. preempt_enable();
  302. ia64_srlz_i(); /* srlz.i implies srlz.d */
  303. }
  304. void flush_tlb_range(struct vm_area_struct *vma,
  305. unsigned long start, unsigned long end)
  306. {
  307. if (unlikely(end - start >= 1024*1024*1024*1024UL
  308. || REGION_NUMBER(start) != REGION_NUMBER(end - 1))) {
  309. /*
  310. * If we flush more than a tera-byte or across regions, we're
  311. * probably better off just flushing the entire TLB(s). This
  312. * should be very rare and is not worth optimizing for.
  313. */
  314. flush_tlb_all();
  315. } else {
  316. /* flush the address range from the tlb */
  317. __flush_tlb_range(vma, start, end);
  318. /* flush the virt. page-table area mapping the addr range */
  319. __flush_tlb_range(vma, ia64_thash(start), ia64_thash(end));
  320. }
  321. }
  322. EXPORT_SYMBOL(flush_tlb_range);
  323. void ia64_tlb_init(void)
  324. {
  325. ia64_ptce_info_t ptce_info;
  326. u64 tr_pgbits;
  327. long status;
  328. pal_vm_info_1_u_t vm_info_1;
  329. pal_vm_info_2_u_t vm_info_2;
  330. int cpu = smp_processor_id();
  331. if ((status = ia64_pal_vm_page_size(&tr_pgbits, &purge.mask)) != 0) {
  332. printk(KERN_ERR "PAL_VM_PAGE_SIZE failed with status=%ld; "
  333. "defaulting to architected purge page-sizes.\n", status);
  334. purge.mask = 0x115557000UL;
  335. }
  336. purge.max_bits = ia64_fls(purge.mask);
  337. ia64_get_ptce(&ptce_info);
  338. local_cpu_data->ptce_base = ptce_info.base;
  339. local_cpu_data->ptce_count[0] = ptce_info.count[0];
  340. local_cpu_data->ptce_count[1] = ptce_info.count[1];
  341. local_cpu_data->ptce_stride[0] = ptce_info.stride[0];
  342. local_cpu_data->ptce_stride[1] = ptce_info.stride[1];
  343. local_flush_tlb_all(); /* nuke left overs from bootstrapping... */
  344. status = ia64_pal_vm_summary(&vm_info_1, &vm_info_2);
  345. if (status) {
  346. printk(KERN_ERR "ia64_pal_vm_summary=%ld\n", status);
  347. per_cpu(ia64_tr_num, cpu) = 8;
  348. return;
  349. }
  350. per_cpu(ia64_tr_num, cpu) = vm_info_1.pal_vm_info_1_s.max_itr_entry+1;
  351. if (per_cpu(ia64_tr_num, cpu) >
  352. (vm_info_1.pal_vm_info_1_s.max_dtr_entry+1))
  353. per_cpu(ia64_tr_num, cpu) =
  354. vm_info_1.pal_vm_info_1_s.max_dtr_entry+1;
  355. if (per_cpu(ia64_tr_num, cpu) > IA64_TR_ALLOC_MAX) {
  356. static int justonce = 1;
  357. per_cpu(ia64_tr_num, cpu) = IA64_TR_ALLOC_MAX;
  358. if (justonce) {
  359. justonce = 0;
  360. printk(KERN_DEBUG "TR register number exceeds "
  361. "IA64_TR_ALLOC_MAX!\n");
  362. }
  363. }
  364. }
  365. /*
  366. * is_tr_overlap
  367. *
  368. * Check overlap with inserted TRs.
  369. */
  370. static int is_tr_overlap(struct ia64_tr_entry *p, u64 va, u64 log_size)
  371. {
  372. u64 tr_log_size;
  373. u64 tr_end;
  374. u64 va_rr = ia64_get_rr(va);
  375. u64 va_rid = RR_TO_RID(va_rr);
  376. u64 va_end = va + (1<<log_size) - 1;
  377. if (va_rid != RR_TO_RID(p->rr))
  378. return 0;
  379. tr_log_size = (p->itir & 0xff) >> 2;
  380. tr_end = p->ifa + (1<<tr_log_size) - 1;
  381. if (va > tr_end || p->ifa > va_end)
  382. return 0;
  383. return 1;
  384. }
  385. /*
  386. * ia64_insert_tr in virtual mode. Allocate a TR slot
  387. *
  388. * target_mask : 0x1 : itr, 0x2 : dtr, 0x3 : idtr
  389. *
  390. * va : virtual address.
  391. * pte : pte entries inserted.
  392. * log_size: range to be covered.
  393. *
  394. * Return value: <0 : error No.
  395. *
  396. * >=0 : slot number allocated for TR.
  397. * Must be called with preemption disabled.
  398. */
  399. int ia64_itr_entry(u64 target_mask, u64 va, u64 pte, u64 log_size)
  400. {
  401. int i, r;
  402. unsigned long psr;
  403. struct ia64_tr_entry *p;
  404. int cpu = smp_processor_id();
  405. if (!ia64_idtrs[cpu]) {
  406. ia64_idtrs[cpu] = kmalloc_array(2 * IA64_TR_ALLOC_MAX,
  407. sizeof(struct ia64_tr_entry),
  408. GFP_KERNEL);
  409. if (!ia64_idtrs[cpu])
  410. return -ENOMEM;
  411. }
  412. r = -EINVAL;
  413. /*Check overlap with existing TR entries*/
  414. if (target_mask & 0x1) {
  415. p = ia64_idtrs[cpu];
  416. for (i = IA64_TR_ALLOC_BASE; i <= per_cpu(ia64_tr_used, cpu);
  417. i++, p++) {
  418. if (p->pte & 0x1)
  419. if (is_tr_overlap(p, va, log_size)) {
  420. printk(KERN_DEBUG "Overlapped Entry"
  421. "Inserted for TR Register!!\n");
  422. goto out;
  423. }
  424. }
  425. }
  426. if (target_mask & 0x2) {
  427. p = ia64_idtrs[cpu] + IA64_TR_ALLOC_MAX;
  428. for (i = IA64_TR_ALLOC_BASE; i <= per_cpu(ia64_tr_used, cpu);
  429. i++, p++) {
  430. if (p->pte & 0x1)
  431. if (is_tr_overlap(p, va, log_size)) {
  432. printk(KERN_DEBUG "Overlapped Entry"
  433. "Inserted for TR Register!!\n");
  434. goto out;
  435. }
  436. }
  437. }
  438. for (i = IA64_TR_ALLOC_BASE; i < per_cpu(ia64_tr_num, cpu); i++) {
  439. switch (target_mask & 0x3) {
  440. case 1:
  441. if (!((ia64_idtrs[cpu] + i)->pte & 0x1))
  442. goto found;
  443. continue;
  444. case 2:
  445. if (!((ia64_idtrs[cpu] + IA64_TR_ALLOC_MAX + i)->pte & 0x1))
  446. goto found;
  447. continue;
  448. case 3:
  449. if (!((ia64_idtrs[cpu] + i)->pte & 0x1) &&
  450. !((ia64_idtrs[cpu] + IA64_TR_ALLOC_MAX + i)->pte & 0x1))
  451. goto found;
  452. continue;
  453. default:
  454. r = -EINVAL;
  455. goto out;
  456. }
  457. }
  458. found:
  459. if (i >= per_cpu(ia64_tr_num, cpu))
  460. return -EBUSY;
  461. /*Record tr info for mca handler use!*/
  462. if (i > per_cpu(ia64_tr_used, cpu))
  463. per_cpu(ia64_tr_used, cpu) = i;
  464. psr = ia64_clear_ic();
  465. if (target_mask & 0x1) {
  466. ia64_itr(0x1, i, va, pte, log_size);
  467. ia64_srlz_i();
  468. p = ia64_idtrs[cpu] + i;
  469. p->ifa = va;
  470. p->pte = pte;
  471. p->itir = log_size << 2;
  472. p->rr = ia64_get_rr(va);
  473. }
  474. if (target_mask & 0x2) {
  475. ia64_itr(0x2, i, va, pte, log_size);
  476. ia64_srlz_i();
  477. p = ia64_idtrs[cpu] + IA64_TR_ALLOC_MAX + i;
  478. p->ifa = va;
  479. p->pte = pte;
  480. p->itir = log_size << 2;
  481. p->rr = ia64_get_rr(va);
  482. }
  483. ia64_set_psr(psr);
  484. r = i;
  485. out:
  486. return r;
  487. }
  488. EXPORT_SYMBOL_GPL(ia64_itr_entry);
  489. /*
  490. * ia64_purge_tr
  491. *
  492. * target_mask: 0x1: purge itr, 0x2 : purge dtr, 0x3 purge idtr.
  493. * slot: slot number to be freed.
  494. *
  495. * Must be called with preemption disabled.
  496. */
  497. void ia64_ptr_entry(u64 target_mask, int slot)
  498. {
  499. int cpu = smp_processor_id();
  500. int i;
  501. struct ia64_tr_entry *p;
  502. if (slot < IA64_TR_ALLOC_BASE || slot >= per_cpu(ia64_tr_num, cpu))
  503. return;
  504. if (target_mask & 0x1) {
  505. p = ia64_idtrs[cpu] + slot;
  506. if ((p->pte&0x1) && is_tr_overlap(p, p->ifa, p->itir>>2)) {
  507. p->pte = 0;
  508. ia64_ptr(0x1, p->ifa, p->itir>>2);
  509. ia64_srlz_i();
  510. }
  511. }
  512. if (target_mask & 0x2) {
  513. p = ia64_idtrs[cpu] + IA64_TR_ALLOC_MAX + slot;
  514. if ((p->pte & 0x1) && is_tr_overlap(p, p->ifa, p->itir>>2)) {
  515. p->pte = 0;
  516. ia64_ptr(0x2, p->ifa, p->itir>>2);
  517. ia64_srlz_i();
  518. }
  519. }
  520. for (i = per_cpu(ia64_tr_used, cpu); i >= IA64_TR_ALLOC_BASE; i--) {
  521. if (((ia64_idtrs[cpu] + i)->pte & 0x1) ||
  522. ((ia64_idtrs[cpu] + IA64_TR_ALLOC_MAX + i)->pte & 0x1))
  523. break;
  524. }
  525. per_cpu(ia64_tr_used, cpu) = i;
  526. }
  527. EXPORT_SYMBOL_GPL(ia64_ptr_entry);