tlb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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 <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 = alloc_bootmem((ia64_ctx.max_ctx+1)>>3);
  57. ia64_ctx.flushmap = alloc_bootmem((ia64_ctx.max_ctx+1)>>3);
  58. }
  59. /*
  60. * Acquire the ia64_ctx.lock before calling this function!
  61. */
  62. void
  63. wrap_mmu_context (struct mm_struct *mm)
  64. {
  65. int i, cpu;
  66. unsigned long flush_bit;
  67. for (i=0; i <= ia64_ctx.max_ctx / BITS_PER_LONG; i++) {
  68. flush_bit = xchg(&ia64_ctx.flushmap[i], 0);
  69. ia64_ctx.bitmap[i] ^= flush_bit;
  70. }
  71. /* use offset at 300 to skip daemons */
  72. ia64_ctx.next = find_next_zero_bit(ia64_ctx.bitmap,
  73. ia64_ctx.max_ctx, 300);
  74. ia64_ctx.limit = find_next_bit(ia64_ctx.bitmap,
  75. ia64_ctx.max_ctx, ia64_ctx.next);
  76. /*
  77. * can't call flush_tlb_all() here because of race condition
  78. * with O(1) scheduler [EF]
  79. */
  80. cpu = get_cpu(); /* prevent preemption/migration */
  81. for_each_online_cpu(i)
  82. if (i != cpu)
  83. per_cpu(ia64_need_tlb_flush, i) = 1;
  84. put_cpu();
  85. local_flush_tlb_all();
  86. }
  87. /*
  88. * Implement "spinaphores" ... like counting semaphores, but they
  89. * spin instead of sleeping. If there are ever any other users for
  90. * this primitive it can be moved up to a spinaphore.h header.
  91. */
  92. struct spinaphore {
  93. unsigned long ticket;
  94. unsigned long serve;
  95. };
  96. static inline void spinaphore_init(struct spinaphore *ss, int val)
  97. {
  98. ss->ticket = 0;
  99. ss->serve = val;
  100. }
  101. static inline void down_spin(struct spinaphore *ss)
  102. {
  103. unsigned long t = ia64_fetchadd(1, &ss->ticket, acq), serve;
  104. if (time_before(t, ss->serve))
  105. return;
  106. ia64_invala();
  107. for (;;) {
  108. asm volatile ("ld8.c.nc %0=[%1]" : "=r"(serve) : "r"(&ss->serve) : "memory");
  109. if (time_before(t, serve))
  110. return;
  111. cpu_relax();
  112. }
  113. }
  114. static inline void up_spin(struct spinaphore *ss)
  115. {
  116. ia64_fetchadd(1, &ss->serve, rel);
  117. }
  118. static struct spinaphore ptcg_sem;
  119. static u16 nptcg = 1;
  120. static int need_ptcg_sem = 1;
  121. static int toolatetochangeptcgsem = 0;
  122. /*
  123. * Kernel parameter "nptcg=" overrides max number of concurrent global TLB
  124. * purges which is reported from either PAL or SAL PALO.
  125. *
  126. * We don't have sanity checking for nptcg value. It's the user's responsibility
  127. * for valid nptcg value on the platform. Otherwise, kernel may hang in some
  128. * cases.
  129. */
  130. static int __init
  131. set_nptcg(char *str)
  132. {
  133. int value = 0;
  134. get_option(&str, &value);
  135. setup_ptcg_sem(value, NPTCG_FROM_KERNEL_PARAMETER);
  136. return 1;
  137. }
  138. __setup("nptcg=", set_nptcg);
  139. /*
  140. * Maximum number of simultaneous ptc.g purges in the system can
  141. * be defined by PAL_VM_SUMMARY (in which case we should take
  142. * the smallest value for any cpu in the system) or by the PAL
  143. * override table (in which case we should ignore the value from
  144. * PAL_VM_SUMMARY).
  145. *
  146. * Kernel parameter "nptcg=" overrides maximum number of simultanesous ptc.g
  147. * purges defined in either PAL_VM_SUMMARY or PAL override table. In this case,
  148. * we should ignore the value from either PAL_VM_SUMMARY or PAL override table.
  149. *
  150. * Complicating the logic here is the fact that num_possible_cpus()
  151. * isn't fully setup until we start bringing cpus online.
  152. */
  153. void
  154. setup_ptcg_sem(int max_purges, int nptcg_from)
  155. {
  156. static int kp_override;
  157. static int palo_override;
  158. static int firstcpu = 1;
  159. if (toolatetochangeptcgsem) {
  160. if (nptcg_from == NPTCG_FROM_PAL && max_purges == 0)
  161. BUG_ON(1 < nptcg);
  162. else
  163. BUG_ON(max_purges < nptcg);
  164. return;
  165. }
  166. if (nptcg_from == NPTCG_FROM_KERNEL_PARAMETER) {
  167. kp_override = 1;
  168. nptcg = max_purges;
  169. goto resetsema;
  170. }
  171. if (kp_override) {
  172. need_ptcg_sem = num_possible_cpus() > nptcg;
  173. return;
  174. }
  175. if (nptcg_from == NPTCG_FROM_PALO) {
  176. palo_override = 1;
  177. /* In PALO max_purges == 0 really means it! */
  178. if (max_purges == 0)
  179. panic("Whoa! Platform does not support global TLB purges.\n");
  180. nptcg = max_purges;
  181. if (nptcg == PALO_MAX_TLB_PURGES) {
  182. need_ptcg_sem = 0;
  183. return;
  184. }
  185. goto resetsema;
  186. }
  187. if (palo_override) {
  188. if (nptcg != PALO_MAX_TLB_PURGES)
  189. need_ptcg_sem = (num_possible_cpus() > nptcg);
  190. return;
  191. }
  192. /* In PAL_VM_SUMMARY max_purges == 0 actually means 1 */
  193. if (max_purges == 0) max_purges = 1;
  194. if (firstcpu) {
  195. nptcg = max_purges;
  196. firstcpu = 0;
  197. }
  198. if (max_purges < nptcg)
  199. nptcg = max_purges;
  200. if (nptcg == PAL_MAX_PURGES) {
  201. need_ptcg_sem = 0;
  202. return;
  203. } else
  204. need_ptcg_sem = (num_possible_cpus() > nptcg);
  205. resetsema:
  206. spinaphore_init(&ptcg_sem, max_purges);
  207. }
  208. void
  209. ia64_global_tlb_purge (struct mm_struct *mm, unsigned long start,
  210. unsigned long end, unsigned long nbits)
  211. {
  212. struct mm_struct *active_mm = current->active_mm;
  213. toolatetochangeptcgsem = 1;
  214. if (mm != active_mm) {
  215. /* Restore region IDs for mm */
  216. if (mm && active_mm) {
  217. activate_context(mm);
  218. } else {
  219. flush_tlb_all();
  220. return;
  221. }
  222. }
  223. if (need_ptcg_sem)
  224. down_spin(&ptcg_sem);
  225. do {
  226. /*
  227. * Flush ALAT entries also.
  228. */
  229. ia64_ptcga(start, (nbits << 2));
  230. ia64_srlz_i();
  231. start += (1UL << nbits);
  232. } while (start < end);
  233. if (need_ptcg_sem)
  234. up_spin(&ptcg_sem);
  235. if (mm != active_mm) {
  236. activate_context(active_mm);
  237. }
  238. }
  239. void
  240. local_flush_tlb_all (void)
  241. {
  242. unsigned long i, j, flags, count0, count1, stride0, stride1, addr;
  243. addr = local_cpu_data->ptce_base;
  244. count0 = local_cpu_data->ptce_count[0];
  245. count1 = local_cpu_data->ptce_count[1];
  246. stride0 = local_cpu_data->ptce_stride[0];
  247. stride1 = local_cpu_data->ptce_stride[1];
  248. local_irq_save(flags);
  249. for (i = 0; i < count0; ++i) {
  250. for (j = 0; j < count1; ++j) {
  251. ia64_ptce(addr);
  252. addr += stride1;
  253. }
  254. addr += stride0;
  255. }
  256. local_irq_restore(flags);
  257. ia64_srlz_i(); /* srlz.i implies srlz.d */
  258. }
  259. void
  260. flush_tlb_range (struct vm_area_struct *vma, unsigned long start,
  261. unsigned long end)
  262. {
  263. struct mm_struct *mm = vma->vm_mm;
  264. unsigned long size = end - start;
  265. unsigned long nbits;
  266. #ifndef CONFIG_SMP
  267. if (mm != current->active_mm) {
  268. mm->context = 0;
  269. return;
  270. }
  271. #endif
  272. nbits = ia64_fls(size + 0xfff);
  273. while (unlikely (((1UL << nbits) & purge.mask) == 0) &&
  274. (nbits < purge.max_bits))
  275. ++nbits;
  276. if (nbits > purge.max_bits)
  277. nbits = purge.max_bits;
  278. start &= ~((1UL << nbits) - 1);
  279. preempt_disable();
  280. #ifdef CONFIG_SMP
  281. if (mm != current->active_mm || cpumask_weight(mm_cpumask(mm)) != 1) {
  282. platform_global_tlb_purge(mm, start, end, nbits);
  283. preempt_enable();
  284. return;
  285. }
  286. #endif
  287. do {
  288. ia64_ptcl(start, (nbits<<2));
  289. start += (1UL << nbits);
  290. } while (start < end);
  291. preempt_enable();
  292. ia64_srlz_i(); /* srlz.i implies srlz.d */
  293. }
  294. EXPORT_SYMBOL(flush_tlb_range);
  295. void 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. if (!ia64_idtrs[cpu]) {
  378. ia64_idtrs[cpu] = kmalloc(2 * IA64_TR_ALLOC_MAX *
  379. sizeof (struct ia64_tr_entry), GFP_KERNEL);
  380. if (!ia64_idtrs[cpu])
  381. return -ENOMEM;
  382. }
  383. r = -EINVAL;
  384. /*Check overlap with existing TR entries*/
  385. if (target_mask & 0x1) {
  386. p = ia64_idtrs[cpu];
  387. for (i = IA64_TR_ALLOC_BASE; i <= per_cpu(ia64_tr_used, cpu);
  388. i++, p++) {
  389. if (p->pte & 0x1)
  390. if (is_tr_overlap(p, va, log_size)) {
  391. printk(KERN_DEBUG "Overlapped Entry"
  392. "Inserted for TR Register!!\n");
  393. goto out;
  394. }
  395. }
  396. }
  397. if (target_mask & 0x2) {
  398. p = ia64_idtrs[cpu] + IA64_TR_ALLOC_MAX;
  399. for (i = IA64_TR_ALLOC_BASE; i <= per_cpu(ia64_tr_used, cpu);
  400. i++, p++) {
  401. if (p->pte & 0x1)
  402. if (is_tr_overlap(p, va, log_size)) {
  403. printk(KERN_DEBUG "Overlapped Entry"
  404. "Inserted for TR Register!!\n");
  405. goto out;
  406. }
  407. }
  408. }
  409. for (i = IA64_TR_ALLOC_BASE; i < per_cpu(ia64_tr_num, cpu); i++) {
  410. switch (target_mask & 0x3) {
  411. case 1:
  412. if (!((ia64_idtrs[cpu] + i)->pte & 0x1))
  413. goto found;
  414. continue;
  415. case 2:
  416. if (!((ia64_idtrs[cpu] + IA64_TR_ALLOC_MAX + i)->pte & 0x1))
  417. goto found;
  418. continue;
  419. case 3:
  420. if (!((ia64_idtrs[cpu] + i)->pte & 0x1) &&
  421. !((ia64_idtrs[cpu] + IA64_TR_ALLOC_MAX + i)->pte & 0x1))
  422. goto found;
  423. continue;
  424. default:
  425. r = -EINVAL;
  426. goto out;
  427. }
  428. }
  429. found:
  430. if (i >= per_cpu(ia64_tr_num, cpu))
  431. return -EBUSY;
  432. /*Record tr info for mca hander use!*/
  433. if (i > per_cpu(ia64_tr_used, cpu))
  434. per_cpu(ia64_tr_used, cpu) = i;
  435. psr = ia64_clear_ic();
  436. if (target_mask & 0x1) {
  437. ia64_itr(0x1, i, va, pte, log_size);
  438. ia64_srlz_i();
  439. p = ia64_idtrs[cpu] + i;
  440. p->ifa = va;
  441. p->pte = pte;
  442. p->itir = log_size << 2;
  443. p->rr = ia64_get_rr(va);
  444. }
  445. if (target_mask & 0x2) {
  446. ia64_itr(0x2, i, va, pte, log_size);
  447. ia64_srlz_i();
  448. p = ia64_idtrs[cpu] + IA64_TR_ALLOC_MAX + i;
  449. p->ifa = va;
  450. p->pte = pte;
  451. p->itir = log_size << 2;
  452. p->rr = ia64_get_rr(va);
  453. }
  454. ia64_set_psr(psr);
  455. r = i;
  456. out:
  457. return r;
  458. }
  459. EXPORT_SYMBOL_GPL(ia64_itr_entry);
  460. /*
  461. * ia64_purge_tr
  462. *
  463. * target_mask: 0x1: purge itr, 0x2 : purge dtr, 0x3 purge idtr.
  464. * slot: slot number to be freed.
  465. *
  466. * Must be called with preemption disabled.
  467. */
  468. void ia64_ptr_entry(u64 target_mask, int slot)
  469. {
  470. int cpu = smp_processor_id();
  471. int i;
  472. struct ia64_tr_entry *p;
  473. if (slot < IA64_TR_ALLOC_BASE || slot >= per_cpu(ia64_tr_num, cpu))
  474. return;
  475. if (target_mask & 0x1) {
  476. p = ia64_idtrs[cpu] + slot;
  477. if ((p->pte&0x1) && is_tr_overlap(p, p->ifa, p->itir>>2)) {
  478. p->pte = 0;
  479. ia64_ptr(0x1, p->ifa, p->itir>>2);
  480. ia64_srlz_i();
  481. }
  482. }
  483. if (target_mask & 0x2) {
  484. p = ia64_idtrs[cpu] + IA64_TR_ALLOC_MAX + slot;
  485. if ((p->pte & 0x1) && is_tr_overlap(p, p->ifa, p->itir>>2)) {
  486. p->pte = 0;
  487. ia64_ptr(0x2, p->ifa, p->itir>>2);
  488. ia64_srlz_i();
  489. }
  490. }
  491. for (i = per_cpu(ia64_tr_used, cpu); i >= IA64_TR_ALLOC_BASE; i--) {
  492. if (((ia64_idtrs[cpu] + i)->pte & 0x1) ||
  493. ((ia64_idtrs[cpu] + IA64_TR_ALLOC_MAX + i)->pte & 0x1))
  494. break;
  495. }
  496. per_cpu(ia64_tr_used, cpu) = i;
  497. }
  498. EXPORT_SYMBOL_GPL(ia64_ptr_entry);