tlb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. BUG_ON(max_purges < nptcg);
  151. return;
  152. }
  153. if (nptcg_from == NPTCG_FROM_KERNEL_PARAMETER) {
  154. kp_override = 1;
  155. nptcg = max_purges;
  156. goto resetsema;
  157. }
  158. if (kp_override) {
  159. need_ptcg_sem = num_possible_cpus() > nptcg;
  160. return;
  161. }
  162. if (nptcg_from == NPTCG_FROM_PALO) {
  163. palo_override = 1;
  164. /* In PALO max_purges == 0 really means it! */
  165. if (max_purges == 0)
  166. panic("Whoa! Platform does not support global TLB purges.\n");
  167. nptcg = max_purges;
  168. if (nptcg == PALO_MAX_TLB_PURGES) {
  169. need_ptcg_sem = 0;
  170. return;
  171. }
  172. goto resetsema;
  173. }
  174. if (palo_override) {
  175. if (nptcg != PALO_MAX_TLB_PURGES)
  176. need_ptcg_sem = (num_possible_cpus() > nptcg);
  177. return;
  178. }
  179. /* In PAL_VM_SUMMARY max_purges == 0 actually means 1 */
  180. if (max_purges == 0) max_purges = 1;
  181. if (firstcpu) {
  182. nptcg = max_purges;
  183. firstcpu = 0;
  184. }
  185. if (max_purges < nptcg)
  186. nptcg = max_purges;
  187. if (nptcg == PAL_MAX_PURGES) {
  188. need_ptcg_sem = 0;
  189. return;
  190. } else
  191. need_ptcg_sem = (num_possible_cpus() > nptcg);
  192. resetsema:
  193. spinaphore_init(&ptcg_sem, max_purges);
  194. }
  195. void
  196. ia64_global_tlb_purge (struct mm_struct *mm, unsigned long start,
  197. unsigned long end, unsigned long nbits)
  198. {
  199. struct mm_struct *active_mm = current->active_mm;
  200. toolatetochangeptcgsem = 1;
  201. if (mm != active_mm) {
  202. /* Restore region IDs for mm */
  203. if (mm && active_mm) {
  204. activate_context(mm);
  205. } else {
  206. flush_tlb_all();
  207. return;
  208. }
  209. }
  210. if (need_ptcg_sem)
  211. down_spin(&ptcg_sem);
  212. do {
  213. /*
  214. * Flush ALAT entries also.
  215. */
  216. ia64_ptcga(start, (nbits << 2));
  217. ia64_srlz_i();
  218. start += (1UL << nbits);
  219. } while (start < end);
  220. if (need_ptcg_sem)
  221. up_spin(&ptcg_sem);
  222. if (mm != active_mm) {
  223. activate_context(active_mm);
  224. }
  225. }
  226. void
  227. local_flush_tlb_all (void)
  228. {
  229. unsigned long i, j, flags, count0, count1, stride0, stride1, addr;
  230. addr = local_cpu_data->ptce_base;
  231. count0 = local_cpu_data->ptce_count[0];
  232. count1 = local_cpu_data->ptce_count[1];
  233. stride0 = local_cpu_data->ptce_stride[0];
  234. stride1 = local_cpu_data->ptce_stride[1];
  235. local_irq_save(flags);
  236. for (i = 0; i < count0; ++i) {
  237. for (j = 0; j < count1; ++j) {
  238. ia64_ptce(addr);
  239. addr += stride1;
  240. }
  241. addr += stride0;
  242. }
  243. local_irq_restore(flags);
  244. ia64_srlz_i(); /* srlz.i implies srlz.d */
  245. }
  246. void
  247. flush_tlb_range (struct vm_area_struct *vma, unsigned long start,
  248. unsigned long end)
  249. {
  250. struct mm_struct *mm = vma->vm_mm;
  251. unsigned long size = end - start;
  252. unsigned long nbits;
  253. #ifndef CONFIG_SMP
  254. if (mm != current->active_mm) {
  255. mm->context = 0;
  256. return;
  257. }
  258. #endif
  259. nbits = ia64_fls(size + 0xfff);
  260. while (unlikely (((1UL << nbits) & purge.mask) == 0) &&
  261. (nbits < purge.max_bits))
  262. ++nbits;
  263. if (nbits > purge.max_bits)
  264. nbits = purge.max_bits;
  265. start &= ~((1UL << nbits) - 1);
  266. preempt_disable();
  267. #ifdef CONFIG_SMP
  268. if (mm != current->active_mm || cpus_weight(mm->cpu_vm_mask) != 1) {
  269. platform_global_tlb_purge(mm, start, end, nbits);
  270. preempt_enable();
  271. return;
  272. }
  273. #endif
  274. do {
  275. ia64_ptcl(start, (nbits<<2));
  276. start += (1UL << nbits);
  277. } while (start < end);
  278. preempt_enable();
  279. ia64_srlz_i(); /* srlz.i implies srlz.d */
  280. }
  281. EXPORT_SYMBOL(flush_tlb_range);
  282. void __devinit
  283. ia64_tlb_init (void)
  284. {
  285. ia64_ptce_info_t uninitialized_var(ptce_info); /* GCC be quiet */
  286. unsigned long tr_pgbits;
  287. long status;
  288. pal_vm_info_1_u_t vm_info_1;
  289. pal_vm_info_2_u_t vm_info_2;
  290. int cpu = smp_processor_id();
  291. if ((status = ia64_pal_vm_page_size(&tr_pgbits, &purge.mask)) != 0) {
  292. printk(KERN_ERR "PAL_VM_PAGE_SIZE failed with status=%ld; "
  293. "defaulting to architected purge page-sizes.\n", status);
  294. purge.mask = 0x115557000UL;
  295. }
  296. purge.max_bits = ia64_fls(purge.mask);
  297. ia64_get_ptce(&ptce_info);
  298. local_cpu_data->ptce_base = ptce_info.base;
  299. local_cpu_data->ptce_count[0] = ptce_info.count[0];
  300. local_cpu_data->ptce_count[1] = ptce_info.count[1];
  301. local_cpu_data->ptce_stride[0] = ptce_info.stride[0];
  302. local_cpu_data->ptce_stride[1] = ptce_info.stride[1];
  303. local_flush_tlb_all(); /* nuke left overs from bootstrapping... */
  304. status = ia64_pal_vm_summary(&vm_info_1, &vm_info_2);
  305. if (status) {
  306. printk(KERN_ERR "ia64_pal_vm_summary=%ld\n", status);
  307. per_cpu(ia64_tr_num, cpu) = 8;
  308. return;
  309. }
  310. per_cpu(ia64_tr_num, cpu) = vm_info_1.pal_vm_info_1_s.max_itr_entry+1;
  311. if (per_cpu(ia64_tr_num, cpu) >
  312. (vm_info_1.pal_vm_info_1_s.max_dtr_entry+1))
  313. per_cpu(ia64_tr_num, cpu) =
  314. vm_info_1.pal_vm_info_1_s.max_dtr_entry+1;
  315. if (per_cpu(ia64_tr_num, cpu) > IA64_TR_ALLOC_MAX) {
  316. per_cpu(ia64_tr_num, cpu) = IA64_TR_ALLOC_MAX;
  317. printk(KERN_DEBUG "TR register number exceeds IA64_TR_ALLOC_MAX!"
  318. "IA64_TR_ALLOC_MAX should be extended\n");
  319. }
  320. }
  321. /*
  322. * is_tr_overlap
  323. *
  324. * Check overlap with inserted TRs.
  325. */
  326. static int is_tr_overlap(struct ia64_tr_entry *p, u64 va, u64 log_size)
  327. {
  328. u64 tr_log_size;
  329. u64 tr_end;
  330. u64 va_rr = ia64_get_rr(va);
  331. u64 va_rid = RR_TO_RID(va_rr);
  332. u64 va_end = va + (1<<log_size) - 1;
  333. if (va_rid != RR_TO_RID(p->rr))
  334. return 0;
  335. tr_log_size = (p->itir & 0xff) >> 2;
  336. tr_end = p->ifa + (1<<tr_log_size) - 1;
  337. if (va > tr_end || p->ifa > va_end)
  338. return 0;
  339. return 1;
  340. }
  341. /*
  342. * ia64_insert_tr in virtual mode. Allocate a TR slot
  343. *
  344. * target_mask : 0x1 : itr, 0x2 : dtr, 0x3 : idtr
  345. *
  346. * va : virtual address.
  347. * pte : pte entries inserted.
  348. * log_size: range to be covered.
  349. *
  350. * Return value: <0 : error No.
  351. *
  352. * >=0 : slot number allocated for TR.
  353. * Must be called with preemption disabled.
  354. */
  355. int ia64_itr_entry(u64 target_mask, u64 va, u64 pte, u64 log_size)
  356. {
  357. int i, r;
  358. unsigned long psr;
  359. struct ia64_tr_entry *p;
  360. int cpu = smp_processor_id();
  361. r = -EINVAL;
  362. /*Check overlap with existing TR entries*/
  363. if (target_mask & 0x1) {
  364. p = &__per_cpu_idtrs[cpu][0][0];
  365. for (i = IA64_TR_ALLOC_BASE; i <= per_cpu(ia64_tr_used, cpu);
  366. i++, p++) {
  367. if (p->pte & 0x1)
  368. if (is_tr_overlap(p, va, log_size)) {
  369. printk(KERN_DEBUG "Overlapped Entry"
  370. "Inserted for TR Reigster!!\n");
  371. goto out;
  372. }
  373. }
  374. }
  375. if (target_mask & 0x2) {
  376. p = &__per_cpu_idtrs[cpu][1][0];
  377. for (i = IA64_TR_ALLOC_BASE; i <= per_cpu(ia64_tr_used, cpu);
  378. i++, p++) {
  379. if (p->pte & 0x1)
  380. if (is_tr_overlap(p, va, log_size)) {
  381. printk(KERN_DEBUG "Overlapped Entry"
  382. "Inserted for TR Reigster!!\n");
  383. goto out;
  384. }
  385. }
  386. }
  387. for (i = IA64_TR_ALLOC_BASE; i < per_cpu(ia64_tr_num, cpu); i++) {
  388. switch (target_mask & 0x3) {
  389. case 1:
  390. if (!(__per_cpu_idtrs[cpu][0][i].pte & 0x1))
  391. goto found;
  392. continue;
  393. case 2:
  394. if (!(__per_cpu_idtrs[cpu][1][i].pte & 0x1))
  395. goto found;
  396. continue;
  397. case 3:
  398. if (!(__per_cpu_idtrs[cpu][0][i].pte & 0x1) &&
  399. !(__per_cpu_idtrs[cpu][1][i].pte & 0x1))
  400. goto found;
  401. continue;
  402. default:
  403. r = -EINVAL;
  404. goto out;
  405. }
  406. }
  407. found:
  408. if (i >= per_cpu(ia64_tr_num, cpu))
  409. return -EBUSY;
  410. /*Record tr info for mca hander use!*/
  411. if (i > per_cpu(ia64_tr_used, cpu))
  412. per_cpu(ia64_tr_used, cpu) = i;
  413. psr = ia64_clear_ic();
  414. if (target_mask & 0x1) {
  415. ia64_itr(0x1, i, va, pte, log_size);
  416. ia64_srlz_i();
  417. p = &__per_cpu_idtrs[cpu][0][i];
  418. p->ifa = va;
  419. p->pte = pte;
  420. p->itir = log_size << 2;
  421. p->rr = ia64_get_rr(va);
  422. }
  423. if (target_mask & 0x2) {
  424. ia64_itr(0x2, i, va, pte, log_size);
  425. ia64_srlz_i();
  426. p = &__per_cpu_idtrs[cpu][1][i];
  427. p->ifa = va;
  428. p->pte = pte;
  429. p->itir = log_size << 2;
  430. p->rr = ia64_get_rr(va);
  431. }
  432. ia64_set_psr(psr);
  433. r = i;
  434. out:
  435. return r;
  436. }
  437. EXPORT_SYMBOL_GPL(ia64_itr_entry);
  438. /*
  439. * ia64_purge_tr
  440. *
  441. * target_mask: 0x1: purge itr, 0x2 : purge dtr, 0x3 purge idtr.
  442. * slot: slot number to be freed.
  443. *
  444. * Must be called with preemption disabled.
  445. */
  446. void ia64_ptr_entry(u64 target_mask, int slot)
  447. {
  448. int cpu = smp_processor_id();
  449. int i;
  450. struct ia64_tr_entry *p;
  451. if (slot < IA64_TR_ALLOC_BASE || slot >= per_cpu(ia64_tr_num, cpu))
  452. return;
  453. if (target_mask & 0x1) {
  454. p = &__per_cpu_idtrs[cpu][0][slot];
  455. if ((p->pte&0x1) && is_tr_overlap(p, p->ifa, p->itir>>2)) {
  456. p->pte = 0;
  457. ia64_ptr(0x1, p->ifa, p->itir>>2);
  458. ia64_srlz_i();
  459. }
  460. }
  461. if (target_mask & 0x2) {
  462. p = &__per_cpu_idtrs[cpu][1][slot];
  463. if ((p->pte & 0x1) && is_tr_overlap(p, p->ifa, p->itir>>2)) {
  464. p->pte = 0;
  465. ia64_ptr(0x2, p->ifa, p->itir>>2);
  466. ia64_srlz_i();
  467. }
  468. }
  469. for (i = per_cpu(ia64_tr_used, cpu); i >= IA64_TR_ALLOC_BASE; i--) {
  470. if ((__per_cpu_idtrs[cpu][0][i].pte & 0x1) ||
  471. (__per_cpu_idtrs[cpu][1][i].pte & 0x1))
  472. break;
  473. }
  474. per_cpu(ia64_tr_used, cpu) = i;
  475. }
  476. EXPORT_SYMBOL_GPL(ia64_ptr_entry);