tlb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. u64 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. unsigned long ticket;
  93. unsigned long serve;
  94. };
  95. static inline void spinaphore_init(struct spinaphore *ss, int val)
  96. {
  97. ss->ticket = 0;
  98. ss->serve = val;
  99. }
  100. static inline void down_spin(struct spinaphore *ss)
  101. {
  102. unsigned long t = ia64_fetchadd(1, &ss->ticket, acq), serve;
  103. if (time_before(t, ss->serve))
  104. return;
  105. ia64_invala();
  106. for (;;) {
  107. asm volatile ("ld4.c.nc %0=[%1]" : "=r"(serve) : "r"(&ss->serve) : "memory");
  108. if (time_before(t, serve))
  109. return;
  110. cpu_relax();
  111. }
  112. }
  113. static inline void up_spin(struct spinaphore *ss)
  114. {
  115. ia64_fetchadd(1, &ss->serve, rel);
  116. }
  117. static struct spinaphore ptcg_sem;
  118. static u16 nptcg = 1;
  119. static int need_ptcg_sem = 1;
  120. static int toolatetochangeptcgsem = 0;
  121. /*
  122. * Kernel parameter "nptcg=" overrides max number of concurrent global TLB
  123. * purges which is reported from either PAL or SAL PALO.
  124. *
  125. * We don't have sanity checking for nptcg value. It's the user's responsibility
  126. * for valid nptcg value on the platform. Otherwise, kernel may hang in some
  127. * cases.
  128. */
  129. static int __init
  130. set_nptcg(char *str)
  131. {
  132. int value = 0;
  133. get_option(&str, &value);
  134. setup_ptcg_sem(value, NPTCG_FROM_KERNEL_PARAMETER);
  135. return 1;
  136. }
  137. __setup("nptcg=", set_nptcg);
  138. /*
  139. * Maximum number of simultaneous ptc.g purges in the system can
  140. * be defined by PAL_VM_SUMMARY (in which case we should take
  141. * the smallest value for any cpu in the system) or by the PAL
  142. * override table (in which case we should ignore the value from
  143. * PAL_VM_SUMMARY).
  144. *
  145. * Kernel parameter "nptcg=" overrides maximum number of simultanesous ptc.g
  146. * purges defined in either PAL_VM_SUMMARY or PAL override table. In this case,
  147. * we should ignore the value from either PAL_VM_SUMMARY or PAL override table.
  148. *
  149. * Complicating the logic here is the fact that num_possible_cpus()
  150. * isn't fully setup until we start bringing cpus online.
  151. */
  152. void
  153. setup_ptcg_sem(int max_purges, int nptcg_from)
  154. {
  155. static int kp_override;
  156. static int palo_override;
  157. static int firstcpu = 1;
  158. if (toolatetochangeptcgsem) {
  159. if (nptcg_from == NPTCG_FROM_PAL && max_purges == 0)
  160. BUG_ON(1 < nptcg);
  161. else
  162. BUG_ON(max_purges < nptcg);
  163. return;
  164. }
  165. if (nptcg_from == NPTCG_FROM_KERNEL_PARAMETER) {
  166. kp_override = 1;
  167. nptcg = max_purges;
  168. goto resetsema;
  169. }
  170. if (kp_override) {
  171. need_ptcg_sem = num_possible_cpus() > nptcg;
  172. return;
  173. }
  174. if (nptcg_from == NPTCG_FROM_PALO) {
  175. palo_override = 1;
  176. /* In PALO max_purges == 0 really means it! */
  177. if (max_purges == 0)
  178. panic("Whoa! Platform does not support global TLB purges.\n");
  179. nptcg = max_purges;
  180. if (nptcg == PALO_MAX_TLB_PURGES) {
  181. need_ptcg_sem = 0;
  182. return;
  183. }
  184. goto resetsema;
  185. }
  186. if (palo_override) {
  187. if (nptcg != PALO_MAX_TLB_PURGES)
  188. need_ptcg_sem = (num_possible_cpus() > nptcg);
  189. return;
  190. }
  191. /* In PAL_VM_SUMMARY max_purges == 0 actually means 1 */
  192. if (max_purges == 0) max_purges = 1;
  193. if (firstcpu) {
  194. nptcg = max_purges;
  195. firstcpu = 0;
  196. }
  197. if (max_purges < nptcg)
  198. nptcg = max_purges;
  199. if (nptcg == PAL_MAX_PURGES) {
  200. need_ptcg_sem = 0;
  201. return;
  202. } else
  203. need_ptcg_sem = (num_possible_cpus() > nptcg);
  204. resetsema:
  205. spinaphore_init(&ptcg_sem, max_purges);
  206. }
  207. void
  208. ia64_global_tlb_purge (struct mm_struct *mm, unsigned long start,
  209. unsigned long end, unsigned long nbits)
  210. {
  211. struct mm_struct *active_mm = current->active_mm;
  212. toolatetochangeptcgsem = 1;
  213. if (mm != active_mm) {
  214. /* Restore region IDs for mm */
  215. if (mm && active_mm) {
  216. activate_context(mm);
  217. } else {
  218. flush_tlb_all();
  219. return;
  220. }
  221. }
  222. if (need_ptcg_sem)
  223. down_spin(&ptcg_sem);
  224. do {
  225. /*
  226. * Flush ALAT entries also.
  227. */
  228. ia64_ptcga(start, (nbits << 2));
  229. ia64_srlz_i();
  230. start += (1UL << nbits);
  231. } while (start < end);
  232. if (need_ptcg_sem)
  233. up_spin(&ptcg_sem);
  234. if (mm != active_mm) {
  235. activate_context(active_mm);
  236. }
  237. }
  238. void
  239. local_flush_tlb_all (void)
  240. {
  241. unsigned long i, j, flags, count0, count1, stride0, stride1, addr;
  242. addr = local_cpu_data->ptce_base;
  243. count0 = local_cpu_data->ptce_count[0];
  244. count1 = local_cpu_data->ptce_count[1];
  245. stride0 = local_cpu_data->ptce_stride[0];
  246. stride1 = local_cpu_data->ptce_stride[1];
  247. local_irq_save(flags);
  248. for (i = 0; i < count0; ++i) {
  249. for (j = 0; j < count1; ++j) {
  250. ia64_ptce(addr);
  251. addr += stride1;
  252. }
  253. addr += stride0;
  254. }
  255. local_irq_restore(flags);
  256. ia64_srlz_i(); /* srlz.i implies srlz.d */
  257. }
  258. void
  259. flush_tlb_range (struct vm_area_struct *vma, unsigned long start,
  260. unsigned long end)
  261. {
  262. struct mm_struct *mm = vma->vm_mm;
  263. unsigned long size = end - start;
  264. unsigned long nbits;
  265. #ifndef CONFIG_SMP
  266. if (mm != current->active_mm) {
  267. mm->context = 0;
  268. return;
  269. }
  270. #endif
  271. nbits = ia64_fls(size + 0xfff);
  272. while (unlikely (((1UL << nbits) & purge.mask) == 0) &&
  273. (nbits < purge.max_bits))
  274. ++nbits;
  275. if (nbits > purge.max_bits)
  276. nbits = purge.max_bits;
  277. start &= ~((1UL << nbits) - 1);
  278. preempt_disable();
  279. #ifdef CONFIG_SMP
  280. if (mm != current->active_mm || cpumask_weight(mm_cpumask(mm)) != 1) {
  281. platform_global_tlb_purge(mm, start, end, nbits);
  282. preempt_enable();
  283. return;
  284. }
  285. #endif
  286. do {
  287. ia64_ptcl(start, (nbits<<2));
  288. start += (1UL << nbits);
  289. } while (start < end);
  290. preempt_enable();
  291. ia64_srlz_i(); /* srlz.i implies srlz.d */
  292. }
  293. EXPORT_SYMBOL(flush_tlb_range);
  294. void __devinit
  295. ia64_tlb_init (void)
  296. {
  297. ia64_ptce_info_t uninitialized_var(ptce_info); /* GCC be quiet */
  298. u64 tr_pgbits;
  299. long status;
  300. pal_vm_info_1_u_t vm_info_1;
  301. pal_vm_info_2_u_t vm_info_2;
  302. int cpu = smp_processor_id();
  303. if ((status = ia64_pal_vm_page_size(&tr_pgbits, &purge.mask)) != 0) {
  304. printk(KERN_ERR "PAL_VM_PAGE_SIZE failed with status=%ld; "
  305. "defaulting to architected purge page-sizes.\n", status);
  306. purge.mask = 0x115557000UL;
  307. }
  308. purge.max_bits = ia64_fls(purge.mask);
  309. ia64_get_ptce(&ptce_info);
  310. local_cpu_data->ptce_base = ptce_info.base;
  311. local_cpu_data->ptce_count[0] = ptce_info.count[0];
  312. local_cpu_data->ptce_count[1] = ptce_info.count[1];
  313. local_cpu_data->ptce_stride[0] = ptce_info.stride[0];
  314. local_cpu_data->ptce_stride[1] = ptce_info.stride[1];
  315. local_flush_tlb_all(); /* nuke left overs from bootstrapping... */
  316. status = ia64_pal_vm_summary(&vm_info_1, &vm_info_2);
  317. if (status) {
  318. printk(KERN_ERR "ia64_pal_vm_summary=%ld\n", status);
  319. per_cpu(ia64_tr_num, cpu) = 8;
  320. return;
  321. }
  322. per_cpu(ia64_tr_num, cpu) = vm_info_1.pal_vm_info_1_s.max_itr_entry+1;
  323. if (per_cpu(ia64_tr_num, cpu) >
  324. (vm_info_1.pal_vm_info_1_s.max_dtr_entry+1))
  325. per_cpu(ia64_tr_num, cpu) =
  326. vm_info_1.pal_vm_info_1_s.max_dtr_entry+1;
  327. if (per_cpu(ia64_tr_num, cpu) > IA64_TR_ALLOC_MAX) {
  328. static int justonce = 1;
  329. per_cpu(ia64_tr_num, cpu) = IA64_TR_ALLOC_MAX;
  330. if (justonce) {
  331. justonce = 0;
  332. printk(KERN_DEBUG "TR register number exceeds "
  333. "IA64_TR_ALLOC_MAX!\n");
  334. }
  335. }
  336. }
  337. /*
  338. * is_tr_overlap
  339. *
  340. * Check overlap with inserted TRs.
  341. */
  342. static int is_tr_overlap(struct ia64_tr_entry *p, u64 va, u64 log_size)
  343. {
  344. u64 tr_log_size;
  345. u64 tr_end;
  346. u64 va_rr = ia64_get_rr(va);
  347. u64 va_rid = RR_TO_RID(va_rr);
  348. u64 va_end = va + (1<<log_size) - 1;
  349. if (va_rid != RR_TO_RID(p->rr))
  350. return 0;
  351. tr_log_size = (p->itir & 0xff) >> 2;
  352. tr_end = p->ifa + (1<<tr_log_size) - 1;
  353. if (va > tr_end || p->ifa > va_end)
  354. return 0;
  355. return 1;
  356. }
  357. /*
  358. * ia64_insert_tr in virtual mode. Allocate a TR slot
  359. *
  360. * target_mask : 0x1 : itr, 0x2 : dtr, 0x3 : idtr
  361. *
  362. * va : virtual address.
  363. * pte : pte entries inserted.
  364. * log_size: range to be covered.
  365. *
  366. * Return value: <0 : error No.
  367. *
  368. * >=0 : slot number allocated for TR.
  369. * Must be called with preemption disabled.
  370. */
  371. int ia64_itr_entry(u64 target_mask, u64 va, u64 pte, u64 log_size)
  372. {
  373. int i, r;
  374. unsigned long psr;
  375. struct ia64_tr_entry *p;
  376. int cpu = smp_processor_id();
  377. r = -EINVAL;
  378. /*Check overlap with existing TR entries*/
  379. if (target_mask & 0x1) {
  380. p = &__per_cpu_idtrs[cpu][0][0];
  381. for (i = IA64_TR_ALLOC_BASE; i <= per_cpu(ia64_tr_used, cpu);
  382. i++, p++) {
  383. if (p->pte & 0x1)
  384. if (is_tr_overlap(p, va, log_size)) {
  385. printk(KERN_DEBUG "Overlapped Entry"
  386. "Inserted for TR Reigster!!\n");
  387. goto out;
  388. }
  389. }
  390. }
  391. if (target_mask & 0x2) {
  392. p = &__per_cpu_idtrs[cpu][1][0];
  393. for (i = IA64_TR_ALLOC_BASE; i <= per_cpu(ia64_tr_used, cpu);
  394. i++, p++) {
  395. if (p->pte & 0x1)
  396. if (is_tr_overlap(p, va, log_size)) {
  397. printk(KERN_DEBUG "Overlapped Entry"
  398. "Inserted for TR Reigster!!\n");
  399. goto out;
  400. }
  401. }
  402. }
  403. for (i = IA64_TR_ALLOC_BASE; i < per_cpu(ia64_tr_num, cpu); i++) {
  404. switch (target_mask & 0x3) {
  405. case 1:
  406. if (!(__per_cpu_idtrs[cpu][0][i].pte & 0x1))
  407. goto found;
  408. continue;
  409. case 2:
  410. if (!(__per_cpu_idtrs[cpu][1][i].pte & 0x1))
  411. goto found;
  412. continue;
  413. case 3:
  414. if (!(__per_cpu_idtrs[cpu][0][i].pte & 0x1) &&
  415. !(__per_cpu_idtrs[cpu][1][i].pte & 0x1))
  416. goto found;
  417. continue;
  418. default:
  419. r = -EINVAL;
  420. goto out;
  421. }
  422. }
  423. found:
  424. if (i >= per_cpu(ia64_tr_num, cpu))
  425. return -EBUSY;
  426. /*Record tr info for mca hander use!*/
  427. if (i > per_cpu(ia64_tr_used, cpu))
  428. per_cpu(ia64_tr_used, cpu) = i;
  429. psr = ia64_clear_ic();
  430. if (target_mask & 0x1) {
  431. ia64_itr(0x1, i, va, pte, log_size);
  432. ia64_srlz_i();
  433. p = &__per_cpu_idtrs[cpu][0][i];
  434. p->ifa = va;
  435. p->pte = pte;
  436. p->itir = log_size << 2;
  437. p->rr = ia64_get_rr(va);
  438. }
  439. if (target_mask & 0x2) {
  440. ia64_itr(0x2, i, va, pte, log_size);
  441. ia64_srlz_i();
  442. p = &__per_cpu_idtrs[cpu][1][i];
  443. p->ifa = va;
  444. p->pte = pte;
  445. p->itir = log_size << 2;
  446. p->rr = ia64_get_rr(va);
  447. }
  448. ia64_set_psr(psr);
  449. r = i;
  450. out:
  451. return r;
  452. }
  453. EXPORT_SYMBOL_GPL(ia64_itr_entry);
  454. /*
  455. * ia64_purge_tr
  456. *
  457. * target_mask: 0x1: purge itr, 0x2 : purge dtr, 0x3 purge idtr.
  458. * slot: slot number to be freed.
  459. *
  460. * Must be called with preemption disabled.
  461. */
  462. void ia64_ptr_entry(u64 target_mask, int slot)
  463. {
  464. int cpu = smp_processor_id();
  465. int i;
  466. struct ia64_tr_entry *p;
  467. if (slot < IA64_TR_ALLOC_BASE || slot >= per_cpu(ia64_tr_num, cpu))
  468. return;
  469. if (target_mask & 0x1) {
  470. p = &__per_cpu_idtrs[cpu][0][slot];
  471. if ((p->pte&0x1) && is_tr_overlap(p, p->ifa, p->itir>>2)) {
  472. p->pte = 0;
  473. ia64_ptr(0x1, p->ifa, p->itir>>2);
  474. ia64_srlz_i();
  475. }
  476. }
  477. if (target_mask & 0x2) {
  478. p = &__per_cpu_idtrs[cpu][1][slot];
  479. if ((p->pte & 0x1) && is_tr_overlap(p, p->ifa, p->itir>>2)) {
  480. p->pte = 0;
  481. ia64_ptr(0x2, p->ifa, p->itir>>2);
  482. ia64_srlz_i();
  483. }
  484. }
  485. for (i = per_cpu(ia64_tr_used, cpu); i >= IA64_TR_ALLOC_BASE; i--) {
  486. if ((__per_cpu_idtrs[cpu][0][i].pte & 0x1) ||
  487. (__per_cpu_idtrs[cpu][1][i].pte & 0x1))
  488. break;
  489. }
  490. per_cpu(ia64_tr_used, cpu) = i;
  491. }
  492. EXPORT_SYMBOL_GPL(ia64_ptr_entry);