tlb.c 13 KB

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