sn2_smp.c 13 KB

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