tlb.c 13 KB

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