sn2_smp.c 13 KB

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