sn2_smp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * SN2 Platform specific SMP Support
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * Copyright (C) 2000-2006 Silicon Graphics, Inc. All rights reserved.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/kernel.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/threads.h>
  14. #include <linux/sched.h>
  15. #include <linux/smp.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/irq.h>
  18. #include <linux/mmzone.h>
  19. #include <linux/module.h>
  20. #include <linux/bitops.h>
  21. #include <linux/nodemask.h>
  22. #include <linux/proc_fs.h>
  23. #include <linux/seq_file.h>
  24. #include <asm/processor.h>
  25. #include <asm/irq.h>
  26. #include <asm/sal.h>
  27. #include <asm/system.h>
  28. #include <asm/delay.h>
  29. #include <asm/io.h>
  30. #include <asm/smp.h>
  31. #include <asm/tlb.h>
  32. #include <asm/numa.h>
  33. #include <asm/hw_irq.h>
  34. #include <asm/current.h>
  35. #include <asm/sn/sn_cpuid.h>
  36. #include <asm/sn/sn_sal.h>
  37. #include <asm/sn/addrs.h>
  38. #include <asm/sn/shub_mmr.h>
  39. #include <asm/sn/nodepda.h>
  40. #include <asm/sn/rw_mmr.h>
  41. DEFINE_PER_CPU(struct ptc_stats, ptcstats);
  42. DECLARE_PER_CPU(struct ptc_stats, ptcstats);
  43. static __cacheline_aligned DEFINE_SPINLOCK(sn2_global_ptc_lock);
  44. void sn2_ptc_deadlock_recovery(short *, short, short, int, volatile unsigned long *, unsigned long,
  45. volatile unsigned long *, unsigned long);
  46. /*
  47. * Note: some is the following is captured here to make degugging easier
  48. * (the macros make more sense if you see the debug patch - not posted)
  49. */
  50. #define sn2_ptctest 0
  51. #define local_node_uses_ptc_ga(sh1) ((sh1) ? 1 : 0)
  52. #define max_active_pio(sh1) ((sh1) ? 32 : 7)
  53. #define reset_max_active_on_deadlock() 1
  54. #define PTC_LOCK(sh1) ((sh1) ? &sn2_global_ptc_lock : &sn_nodepda->ptc_lock)
  55. static inline void ptc_lock(int sh1, unsigned long *flagp)
  56. {
  57. spin_lock_irqsave(PTC_LOCK(sh1), *flagp);
  58. }
  59. static inline void ptc_unlock(int sh1, unsigned long flags)
  60. {
  61. spin_unlock_irqrestore(PTC_LOCK(sh1), flags);
  62. }
  63. struct ptc_stats {
  64. unsigned long ptc_l;
  65. unsigned long change_rid;
  66. unsigned long shub_ptc_flushes;
  67. unsigned long nodes_flushed;
  68. unsigned long deadlocks;
  69. unsigned long deadlocks2;
  70. unsigned long lock_itc_clocks;
  71. unsigned long shub_itc_clocks;
  72. unsigned long shub_itc_clocks_max;
  73. unsigned long shub_ptc_flushes_not_my_mm;
  74. };
  75. static inline unsigned long wait_piowc(void)
  76. {
  77. volatile unsigned long *piows;
  78. unsigned long zeroval, ws;
  79. piows = pda->pio_write_status_addr;
  80. zeroval = pda->pio_write_status_val;
  81. do {
  82. cpu_relax();
  83. } while (((ws = *piows) & SH_PIO_WRITE_STATUS_PENDING_WRITE_COUNT_MASK) != zeroval);
  84. return (ws & SH_PIO_WRITE_STATUS_WRITE_DEADLOCK_MASK) != 0;
  85. }
  86. void sn_tlb_migrate_finish(struct mm_struct *mm)
  87. {
  88. /* flush_tlb_mm is inefficient if more than 1 users of mm */
  89. if (mm == current->mm && mm && atomic_read(&mm->mm_users) == 1)
  90. flush_tlb_mm(mm);
  91. }
  92. /**
  93. * sn2_global_tlb_purge - globally purge translation cache of virtual address range
  94. * @mm: mm_struct containing virtual address range
  95. * @start: start of virtual address range
  96. * @end: end of virtual address range
  97. * @nbits: specifies number of bytes to purge per instruction (num = 1<<(nbits & 0xfc))
  98. *
  99. * Purges the translation caches of all processors of the given virtual address
  100. * range.
  101. *
  102. * Note:
  103. * - cpu_vm_mask is a bit mask that indicates which cpus have loaded the context.
  104. * - cpu_vm_mask is converted into a nodemask of the nodes containing the
  105. * cpus in cpu_vm_mask.
  106. * - if only one bit is set in cpu_vm_mask & it is the current cpu & the
  107. * process is purging its own virtual address range, then only the
  108. * local TLB needs to be flushed. This flushing can be done using
  109. * ptc.l. This is the common case & avoids the global spinlock.
  110. * - if multiple cpus have loaded the context, then flushing has to be
  111. * done with ptc.g/MMRs under protection of the global ptc_lock.
  112. */
  113. void
  114. sn2_global_tlb_purge(struct mm_struct *mm, unsigned long start,
  115. unsigned long end, unsigned long nbits)
  116. {
  117. int i, ibegin, shub1, cnode, mynasid, cpu, lcpu = 0, nasid;
  118. int mymm = (mm == current->active_mm && mm == current->mm);
  119. int use_cpu_ptcga;
  120. volatile unsigned long *ptc0, *ptc1;
  121. unsigned long itc, itc2, flags, data0 = 0, data1 = 0, rr_value, old_rr = 0;
  122. short nasids[MAX_NUMNODES], nix;
  123. nodemask_t nodes_flushed;
  124. int active, max_active, deadlock;
  125. nodes_clear(nodes_flushed);
  126. i = 0;
  127. for_each_cpu_mask(cpu, mm->cpu_vm_mask) {
  128. cnode = cpu_to_node(cpu);
  129. node_set(cnode, nodes_flushed);
  130. lcpu = cpu;
  131. i++;
  132. }
  133. if (i == 0)
  134. return;
  135. preempt_disable();
  136. if (likely(i == 1 && lcpu == smp_processor_id() && mymm)) {
  137. do {
  138. ia64_ptcl(start, nbits << 2);
  139. start += (1UL << nbits);
  140. } while (start < end);
  141. ia64_srlz_i();
  142. __get_cpu_var(ptcstats).ptc_l++;
  143. preempt_enable();
  144. return;
  145. }
  146. if (atomic_read(&mm->mm_users) == 1 && mymm) {
  147. flush_tlb_mm(mm);
  148. __get_cpu_var(ptcstats).change_rid++;
  149. preempt_enable();
  150. return;
  151. }
  152. itc = ia64_get_itc();
  153. nix = 0;
  154. for_each_node_mask(cnode, nodes_flushed)
  155. nasids[nix++] = cnodeid_to_nasid(cnode);
  156. rr_value = (mm->context << 3) | REGION_NUMBER(start);
  157. shub1 = is_shub1();
  158. if (shub1) {
  159. data0 = (1UL << SH1_PTC_0_A_SHFT) |
  160. (nbits << SH1_PTC_0_PS_SHFT) |
  161. (rr_value << SH1_PTC_0_RID_SHFT) |
  162. (1UL << SH1_PTC_0_START_SHFT);
  163. ptc0 = (long *)GLOBAL_MMR_PHYS_ADDR(0, SH1_PTC_0);
  164. ptc1 = (long *)GLOBAL_MMR_PHYS_ADDR(0, SH1_PTC_1);
  165. } else {
  166. data0 = (1UL << SH2_PTC_A_SHFT) |
  167. (nbits << SH2_PTC_PS_SHFT) |
  168. (1UL << SH2_PTC_START_SHFT);
  169. ptc0 = (long *)GLOBAL_MMR_PHYS_ADDR(0, SH2_PTC +
  170. (rr_value << SH2_PTC_RID_SHFT));
  171. ptc1 = NULL;
  172. }
  173. mynasid = get_nasid();
  174. use_cpu_ptcga = local_node_uses_ptc_ga(shub1);
  175. max_active = max_active_pio(shub1);
  176. itc = ia64_get_itc();
  177. ptc_lock(shub1, &flags);
  178. itc2 = ia64_get_itc();
  179. __get_cpu_var(ptcstats).lock_itc_clocks += itc2 - itc;
  180. __get_cpu_var(ptcstats).shub_ptc_flushes++;
  181. __get_cpu_var(ptcstats).nodes_flushed += nix;
  182. if (!mymm)
  183. __get_cpu_var(ptcstats).shub_ptc_flushes_not_my_mm++;
  184. if (use_cpu_ptcga && !mymm) {
  185. old_rr = ia64_get_rr(start);
  186. ia64_set_rr(start, (old_rr & 0xff) | (rr_value << 8));
  187. ia64_srlz_d();
  188. }
  189. wait_piowc();
  190. do {
  191. if (shub1)
  192. data1 = start | (1UL << SH1_PTC_1_START_SHFT);
  193. else
  194. data0 = (data0 & ~SH2_PTC_ADDR_MASK) | (start & SH2_PTC_ADDR_MASK);
  195. deadlock = 0;
  196. active = 0;
  197. for (ibegin = 0, i = 0; i < nix; i++) {
  198. nasid = nasids[i];
  199. if (use_cpu_ptcga && unlikely(nasid == mynasid)) {
  200. ia64_ptcga(start, nbits << 2);
  201. ia64_srlz_i();
  202. } else {
  203. ptc0 = CHANGE_NASID(nasid, ptc0);
  204. if (ptc1)
  205. ptc1 = CHANGE_NASID(nasid, ptc1);
  206. pio_atomic_phys_write_mmrs(ptc0, data0, ptc1, data1);
  207. active++;
  208. }
  209. if (active >= max_active || i == (nix - 1)) {
  210. if ((deadlock = wait_piowc())) {
  211. sn2_ptc_deadlock_recovery(nasids, ibegin, i, mynasid, ptc0, data0, ptc1, data1);
  212. if (reset_max_active_on_deadlock())
  213. max_active = 1;
  214. }
  215. active = 0;
  216. ibegin = i + 1;
  217. }
  218. }
  219. start += (1UL << nbits);
  220. } while (start < end);
  221. itc2 = ia64_get_itc() - itc2;
  222. __get_cpu_var(ptcstats).shub_itc_clocks += itc2;
  223. if (itc2 > __get_cpu_var(ptcstats).shub_itc_clocks_max)
  224. __get_cpu_var(ptcstats).shub_itc_clocks_max = itc2;
  225. if (old_rr) {
  226. ia64_set_rr(start, old_rr);
  227. ia64_srlz_d();
  228. }
  229. ptc_unlock(shub1, flags);
  230. preempt_enable();
  231. }
  232. /*
  233. * sn2_ptc_deadlock_recovery
  234. *
  235. * Recover from PTC deadlocks conditions. Recovery requires stepping thru each
  236. * TLB flush transaction. The recovery sequence is somewhat tricky & is
  237. * coded in assembly language.
  238. */
  239. void sn2_ptc_deadlock_recovery(short *nasids, short ib, short ie, int mynasid, volatile unsigned long *ptc0, unsigned long data0,
  240. volatile unsigned long *ptc1, unsigned long data1)
  241. {
  242. extern unsigned long sn2_ptc_deadlock_recovery_core(volatile unsigned long *, unsigned long,
  243. volatile unsigned long *, unsigned long, volatile unsigned long *, unsigned long);
  244. short nasid, i;
  245. unsigned long *piows, zeroval, n;
  246. __get_cpu_var(ptcstats).deadlocks++;
  247. piows = (unsigned long *) pda->pio_write_status_addr;
  248. zeroval = pda->pio_write_status_val;
  249. for (i=ib; i <= ie; i++) {
  250. nasid = nasids[i];
  251. if (local_node_uses_ptc_ga(is_shub1()) && nasid == mynasid)
  252. continue;
  253. ptc0 = CHANGE_NASID(nasid, ptc0);
  254. if (ptc1)
  255. ptc1 = CHANGE_NASID(nasid, ptc1);
  256. n = sn2_ptc_deadlock_recovery_core(ptc0, data0, ptc1, data1, piows, zeroval);
  257. __get_cpu_var(ptcstats).deadlocks2 += n;
  258. }
  259. }
  260. /**
  261. * sn_send_IPI_phys - send an IPI to a Nasid and slice
  262. * @nasid: nasid to receive the interrupt (may be outside partition)
  263. * @physid: physical cpuid to receive the interrupt.
  264. * @vector: command to send
  265. * @delivery_mode: delivery mechanism
  266. *
  267. * Sends an IPI (interprocessor interrupt) to the processor specified by
  268. * @physid
  269. *
  270. * @delivery_mode can be one of the following
  271. *
  272. * %IA64_IPI_DM_INT - pend an interrupt
  273. * %IA64_IPI_DM_PMI - pend a PMI
  274. * %IA64_IPI_DM_NMI - pend an NMI
  275. * %IA64_IPI_DM_INIT - pend an INIT interrupt
  276. */
  277. void sn_send_IPI_phys(int nasid, long physid, int vector, int delivery_mode)
  278. {
  279. long val;
  280. unsigned long flags = 0;
  281. volatile long *p;
  282. p = (long *)GLOBAL_MMR_PHYS_ADDR(nasid, SH_IPI_INT);
  283. val = (1UL << SH_IPI_INT_SEND_SHFT) |
  284. (physid << SH_IPI_INT_PID_SHFT) |
  285. ((long)delivery_mode << SH_IPI_INT_TYPE_SHFT) |
  286. ((long)vector << SH_IPI_INT_IDX_SHFT) |
  287. (0x000feeUL << SH_IPI_INT_BASE_SHFT);
  288. mb();
  289. if (enable_shub_wars_1_1()) {
  290. spin_lock_irqsave(&sn2_global_ptc_lock, flags);
  291. }
  292. pio_phys_write_mmr(p, val);
  293. if (enable_shub_wars_1_1()) {
  294. wait_piowc();
  295. spin_unlock_irqrestore(&sn2_global_ptc_lock, flags);
  296. }
  297. }
  298. EXPORT_SYMBOL(sn_send_IPI_phys);
  299. /**
  300. * sn2_send_IPI - send an IPI to a processor
  301. * @cpuid: target of the IPI
  302. * @vector: command to send
  303. * @delivery_mode: delivery mechanism
  304. * @redirect: redirect the IPI?
  305. *
  306. * Sends an IPI (InterProcessor Interrupt) to the processor specified by
  307. * @cpuid. @vector specifies the command to send, while @delivery_mode can
  308. * be one of the following
  309. *
  310. * %IA64_IPI_DM_INT - pend an interrupt
  311. * %IA64_IPI_DM_PMI - pend a PMI
  312. * %IA64_IPI_DM_NMI - pend an NMI
  313. * %IA64_IPI_DM_INIT - pend an INIT interrupt
  314. */
  315. void sn2_send_IPI(int cpuid, int vector, int delivery_mode, int redirect)
  316. {
  317. long physid;
  318. int nasid;
  319. physid = cpu_physical_id(cpuid);
  320. nasid = cpuid_to_nasid(cpuid);
  321. /* the following is used only when starting cpus at boot time */
  322. if (unlikely(nasid == -1))
  323. ia64_sn_get_sapic_info(physid, &nasid, NULL, NULL);
  324. sn_send_IPI_phys(nasid, physid, vector, delivery_mode);
  325. }
  326. #ifdef CONFIG_PROC_FS
  327. #define PTC_BASENAME "sgi_sn/ptc_statistics"
  328. static void *sn2_ptc_seq_start(struct seq_file *file, loff_t * offset)
  329. {
  330. if (*offset < NR_CPUS)
  331. return offset;
  332. return NULL;
  333. }
  334. static void *sn2_ptc_seq_next(struct seq_file *file, void *data, loff_t * offset)
  335. {
  336. (*offset)++;
  337. if (*offset < NR_CPUS)
  338. return offset;
  339. return NULL;
  340. }
  341. static void sn2_ptc_seq_stop(struct seq_file *file, void *data)
  342. {
  343. }
  344. static int sn2_ptc_seq_show(struct seq_file *file, void *data)
  345. {
  346. struct ptc_stats *stat;
  347. int cpu;
  348. cpu = *(loff_t *) data;
  349. if (!cpu) {
  350. seq_printf(file,
  351. "# cpu ptc_l newrid ptc_flushes nodes_flushed deadlocks lock_nsec shub_nsec shub_nsec_max not_my_mm deadlock2\n");
  352. seq_printf(file, "# ptctest %d\n", sn2_ptctest);
  353. }
  354. if (cpu < NR_CPUS && cpu_online(cpu)) {
  355. stat = &per_cpu(ptcstats, cpu);
  356. seq_printf(file, "cpu %d %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld\n", cpu, stat->ptc_l,
  357. stat->change_rid, stat->shub_ptc_flushes, stat->nodes_flushed,
  358. stat->deadlocks,
  359. 1000 * stat->lock_itc_clocks / per_cpu(cpu_info, cpu).cyc_per_usec,
  360. 1000 * stat->shub_itc_clocks / per_cpu(cpu_info, cpu).cyc_per_usec,
  361. 1000 * stat->shub_itc_clocks_max / per_cpu(cpu_info, cpu).cyc_per_usec,
  362. stat->shub_ptc_flushes_not_my_mm,
  363. stat->deadlocks2);
  364. }
  365. return 0;
  366. }
  367. static struct seq_operations sn2_ptc_seq_ops = {
  368. .start = sn2_ptc_seq_start,
  369. .next = sn2_ptc_seq_next,
  370. .stop = sn2_ptc_seq_stop,
  371. .show = sn2_ptc_seq_show
  372. };
  373. static int sn2_ptc_proc_open(struct inode *inode, struct file *file)
  374. {
  375. return seq_open(file, &sn2_ptc_seq_ops);
  376. }
  377. static struct file_operations proc_sn2_ptc_operations = {
  378. .open = sn2_ptc_proc_open,
  379. .read = seq_read,
  380. .llseek = seq_lseek,
  381. .release = seq_release,
  382. };
  383. static struct proc_dir_entry *proc_sn2_ptc;
  384. static int __init sn2_ptc_init(void)
  385. {
  386. if (!ia64_platform_is("sn2"))
  387. return -ENOSYS;
  388. if (!(proc_sn2_ptc = create_proc_entry(PTC_BASENAME, 0444, NULL))) {
  389. printk(KERN_ERR "unable to create %s proc entry", PTC_BASENAME);
  390. return -EINVAL;
  391. }
  392. proc_sn2_ptc->proc_fops = &proc_sn2_ptc_operations;
  393. spin_lock_init(&sn2_global_ptc_lock);
  394. return 0;
  395. }
  396. static void __exit sn2_ptc_exit(void)
  397. {
  398. remove_proc_entry(PTC_BASENAME, NULL);
  399. }
  400. module_init(sn2_ptc_init);
  401. module_exit(sn2_ptc_exit);
  402. #endif /* CONFIG_PROC_FS */