sn2_smp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. * @mm: mm_struct containing virtual address range
  162. * @start: start of virtual address range
  163. * @end: end of virtual address range
  164. * @nbits: specifies number of bytes to purge per instruction (num = 1<<(nbits & 0xfc))
  165. *
  166. * Purges the translation caches of all processors of the given virtual address
  167. * range.
  168. *
  169. * Note:
  170. * - cpu_vm_mask is a bit mask that indicates which cpus have loaded the context.
  171. * - cpu_vm_mask is converted into a nodemask of the nodes containing the
  172. * cpus in cpu_vm_mask.
  173. * - if only one bit is set in cpu_vm_mask & it is the current cpu & the
  174. * process is purging its own virtual address range, then only the
  175. * local TLB needs to be flushed. This flushing can be done using
  176. * ptc.l. This is the common case & avoids the global spinlock.
  177. * - if multiple cpus have loaded the context, then flushing has to be
  178. * done with ptc.g/MMRs under protection of the global ptc_lock.
  179. */
  180. void
  181. sn2_global_tlb_purge(struct mm_struct *mm, unsigned long start,
  182. unsigned long end, unsigned long nbits)
  183. {
  184. int i, opt, shub1, cnode, mynasid, cpu, lcpu = 0, nasid, flushed = 0;
  185. int mymm = (mm == current->active_mm && current->mm);
  186. volatile unsigned long *ptc0, *ptc1;
  187. unsigned long itc, itc2, flags, data0 = 0, data1 = 0, rr_value;
  188. short nasids[MAX_NUMNODES], nix;
  189. nodemask_t nodes_flushed;
  190. nodes_clear(nodes_flushed);
  191. i = 0;
  192. for_each_cpu_mask(cpu, mm->cpu_vm_mask) {
  193. cnode = cpu_to_node(cpu);
  194. node_set(cnode, nodes_flushed);
  195. lcpu = cpu;
  196. i++;
  197. }
  198. if (i == 0)
  199. return;
  200. preempt_disable();
  201. if (likely(i == 1 && lcpu == smp_processor_id() && mymm)) {
  202. do {
  203. ia64_ptcl(start, nbits << 2);
  204. start += (1UL << nbits);
  205. } while (start < end);
  206. ia64_srlz_i();
  207. __get_cpu_var(ptcstats).ptc_l++;
  208. preempt_enable();
  209. return;
  210. }
  211. if (atomic_read(&mm->mm_users) == 1 && mymm) {
  212. flush_tlb_mm(mm);
  213. __get_cpu_var(ptcstats).change_rid++;
  214. preempt_enable();
  215. return;
  216. }
  217. itc = ia64_get_itc();
  218. nix = 0;
  219. for_each_node_mask(cnode, nodes_flushed)
  220. nasids[nix++] = cnodeid_to_nasid(cnode);
  221. rr_value = (mm->context << 3) | REGION_NUMBER(start);
  222. shub1 = is_shub1();
  223. if (shub1) {
  224. data0 = (1UL << SH1_PTC_0_A_SHFT) |
  225. (nbits << SH1_PTC_0_PS_SHFT) |
  226. (rr_value << SH1_PTC_0_RID_SHFT) |
  227. (1UL << SH1_PTC_0_START_SHFT);
  228. ptc0 = (long *)GLOBAL_MMR_PHYS_ADDR(0, SH1_PTC_0);
  229. ptc1 = (long *)GLOBAL_MMR_PHYS_ADDR(0, SH1_PTC_1);
  230. } else {
  231. data0 = (1UL << SH2_PTC_A_SHFT) |
  232. (nbits << SH2_PTC_PS_SHFT) |
  233. (1UL << SH2_PTC_START_SHFT);
  234. ptc0 = (long *)GLOBAL_MMR_PHYS_ADDR(0, SH2_PTC +
  235. (rr_value << SH2_PTC_RID_SHFT));
  236. ptc1 = NULL;
  237. }
  238. mynasid = get_nasid();
  239. itc = ia64_get_itc();
  240. opt = ptc_lock(&flags);
  241. itc2 = ia64_get_itc();
  242. __get_cpu_var(ptcstats).lock_itc_clocks += itc2 - itc;
  243. __get_cpu_var(ptcstats).shub_ptc_flushes++;
  244. __get_cpu_var(ptcstats).nodes_flushed += nix;
  245. do {
  246. if (shub1)
  247. data1 = start | (1UL << SH1_PTC_1_START_SHFT);
  248. else
  249. data0 = (data0 & ~SH2_PTC_ADDR_MASK) | (start & SH2_PTC_ADDR_MASK);
  250. for (i = 0; i < nix; i++) {
  251. nasid = nasids[i];
  252. if ((!(sn2_ptctest & 3)) && unlikely(nasid == mynasid && mymm)) {
  253. ia64_ptcga(start, nbits << 2);
  254. ia64_srlz_i();
  255. } else {
  256. ptc0 = CHANGE_NASID(nasid, ptc0);
  257. if (ptc1)
  258. ptc1 = CHANGE_NASID(nasid, ptc1);
  259. pio_atomic_phys_write_mmrs(ptc0, data0, ptc1,
  260. data1);
  261. flushed = 1;
  262. }
  263. }
  264. if (flushed
  265. && (wait_piowc() &
  266. (SH_PIO_WRITE_STATUS_WRITE_DEADLOCK_MASK))) {
  267. sn2_ptc_deadlock_recovery(nasids, nix, mynasid, ptc0, data0, ptc1, data1);
  268. }
  269. start += (1UL << nbits);
  270. } while (start < end);
  271. itc2 = ia64_get_itc() - itc2;
  272. __get_cpu_var(ptcstats).shub_itc_clocks += itc2;
  273. if (itc2 > __get_cpu_var(ptcstats).shub_itc_clocks_max)
  274. __get_cpu_var(ptcstats).shub_itc_clocks_max = itc2;
  275. ptc_unlock(flags, opt);
  276. preempt_enable();
  277. }
  278. /*
  279. * sn2_ptc_deadlock_recovery
  280. *
  281. * Recover from PTC deadlocks conditions. Recovery requires stepping thru each
  282. * TLB flush transaction. The recovery sequence is somewhat tricky & is
  283. * coded in assembly language.
  284. */
  285. void sn2_ptc_deadlock_recovery(short *nasids, short nix, int mynasid, volatile unsigned long *ptc0, unsigned long data0,
  286. volatile unsigned long *ptc1, unsigned long data1)
  287. {
  288. extern void sn2_ptc_deadlock_recovery_core(volatile unsigned long *, unsigned long,
  289. volatile unsigned long *, unsigned long, volatile unsigned long *, unsigned long);
  290. short nasid, i;
  291. unsigned long *piows, zeroval;
  292. __get_cpu_var(ptcstats).deadlocks++;
  293. piows = (unsigned long *) pda->pio_write_status_addr;
  294. zeroval = pda->pio_write_status_val;
  295. for (i=0; i < nix; i++) {
  296. nasid = nasids[i];
  297. if (!(sn2_ptctest & 3) && nasid == mynasid)
  298. continue;
  299. ptc0 = CHANGE_NASID(nasid, ptc0);
  300. if (ptc1)
  301. ptc1 = CHANGE_NASID(nasid, ptc1);
  302. sn2_ptc_deadlock_recovery_core(ptc0, data0, ptc1, data1, piows, zeroval);
  303. }
  304. }
  305. /**
  306. * sn_send_IPI_phys - send an IPI to a Nasid and slice
  307. * @nasid: nasid to receive the interrupt (may be outside partition)
  308. * @physid: physical cpuid to receive the interrupt.
  309. * @vector: command to send
  310. * @delivery_mode: delivery mechanism
  311. *
  312. * Sends an IPI (interprocessor interrupt) to the processor specified by
  313. * @physid
  314. *
  315. * @delivery_mode can be one of the following
  316. *
  317. * %IA64_IPI_DM_INT - pend an interrupt
  318. * %IA64_IPI_DM_PMI - pend a PMI
  319. * %IA64_IPI_DM_NMI - pend an NMI
  320. * %IA64_IPI_DM_INIT - pend an INIT interrupt
  321. */
  322. void sn_send_IPI_phys(int nasid, long physid, int vector, int delivery_mode)
  323. {
  324. long val;
  325. unsigned long flags = 0;
  326. volatile long *p;
  327. p = (long *)GLOBAL_MMR_PHYS_ADDR(nasid, SH_IPI_INT);
  328. val = (1UL << SH_IPI_INT_SEND_SHFT) |
  329. (physid << SH_IPI_INT_PID_SHFT) |
  330. ((long)delivery_mode << SH_IPI_INT_TYPE_SHFT) |
  331. ((long)vector << SH_IPI_INT_IDX_SHFT) |
  332. (0x000feeUL << SH_IPI_INT_BASE_SHFT);
  333. mb();
  334. if (enable_shub_wars_1_1()) {
  335. spin_lock_irqsave(&sn2_global_ptc_lock, flags);
  336. }
  337. pio_phys_write_mmr(p, val);
  338. if (enable_shub_wars_1_1()) {
  339. wait_piowc();
  340. spin_unlock_irqrestore(&sn2_global_ptc_lock, flags);
  341. }
  342. }
  343. EXPORT_SYMBOL(sn_send_IPI_phys);
  344. /**
  345. * sn2_send_IPI - send an IPI to a processor
  346. * @cpuid: target of the IPI
  347. * @vector: command to send
  348. * @delivery_mode: delivery mechanism
  349. * @redirect: redirect the IPI?
  350. *
  351. * Sends an IPI (InterProcessor Interrupt) to the processor specified by
  352. * @cpuid. @vector specifies the command to send, while @delivery_mode can
  353. * be one of the following
  354. *
  355. * %IA64_IPI_DM_INT - pend an interrupt
  356. * %IA64_IPI_DM_PMI - pend a PMI
  357. * %IA64_IPI_DM_NMI - pend an NMI
  358. * %IA64_IPI_DM_INIT - pend an INIT interrupt
  359. */
  360. void sn2_send_IPI(int cpuid, int vector, int delivery_mode, int redirect)
  361. {
  362. long physid;
  363. int nasid;
  364. physid = cpu_physical_id(cpuid);
  365. nasid = cpuid_to_nasid(cpuid);
  366. /* the following is used only when starting cpus at boot time */
  367. if (unlikely(nasid == -1))
  368. ia64_sn_get_sapic_info(physid, &nasid, NULL, NULL);
  369. sn_send_IPI_phys(nasid, physid, vector, delivery_mode);
  370. }
  371. #ifdef CONFIG_PROC_FS
  372. #define PTC_BASENAME "sgi_sn/ptc_statistics"
  373. static void *sn2_ptc_seq_start(struct seq_file *file, loff_t * offset)
  374. {
  375. if (*offset < NR_CPUS)
  376. return offset;
  377. return NULL;
  378. }
  379. static void *sn2_ptc_seq_next(struct seq_file *file, void *data, loff_t * offset)
  380. {
  381. (*offset)++;
  382. if (*offset < NR_CPUS)
  383. return offset;
  384. return NULL;
  385. }
  386. static void sn2_ptc_seq_stop(struct seq_file *file, void *data)
  387. {
  388. }
  389. static int sn2_ptc_seq_show(struct seq_file *file, void *data)
  390. {
  391. struct ptc_stats *stat;
  392. int cpu;
  393. cpu = *(loff_t *) data;
  394. if (!cpu) {
  395. seq_printf(file, "# ptc_l change_rid shub_ptc_flushes shub_nodes_flushed deadlocks lock_nsec shub_nsec shub_nsec_max\n");
  396. seq_printf(file, "# ptctest %d\n", sn2_ptctest);
  397. }
  398. if (cpu < NR_CPUS && cpu_online(cpu)) {
  399. stat = &per_cpu(ptcstats, cpu);
  400. seq_printf(file, "cpu %d %ld %ld %ld %ld %ld %ld %ld %ld\n", cpu, stat->ptc_l,
  401. stat->change_rid, stat->shub_ptc_flushes, stat->nodes_flushed,
  402. stat->deadlocks,
  403. 1000 * stat->lock_itc_clocks / per_cpu(cpu_info, cpu).cyc_per_usec,
  404. 1000 * stat->shub_itc_clocks / per_cpu(cpu_info, cpu).cyc_per_usec,
  405. 1000 * stat->shub_itc_clocks_max / per_cpu(cpu_info, cpu).cyc_per_usec);
  406. }
  407. return 0;
  408. }
  409. static struct seq_operations sn2_ptc_seq_ops = {
  410. .start = sn2_ptc_seq_start,
  411. .next = sn2_ptc_seq_next,
  412. .stop = sn2_ptc_seq_stop,
  413. .show = sn2_ptc_seq_show
  414. };
  415. int sn2_ptc_proc_open(struct inode *inode, struct file *file)
  416. {
  417. return seq_open(file, &sn2_ptc_seq_ops);
  418. }
  419. static struct file_operations proc_sn2_ptc_operations = {
  420. .open = sn2_ptc_proc_open,
  421. .read = seq_read,
  422. .llseek = seq_lseek,
  423. .release = seq_release,
  424. };
  425. static struct proc_dir_entry *proc_sn2_ptc;
  426. static int __init sn2_ptc_init(void)
  427. {
  428. if (!ia64_platform_is("sn2"))
  429. return -ENOSYS;
  430. if (!(proc_sn2_ptc = create_proc_entry(PTC_BASENAME, 0444, NULL))) {
  431. printk(KERN_ERR "unable to create %s proc entry", PTC_BASENAME);
  432. return -EINVAL;
  433. }
  434. proc_sn2_ptc->proc_fops = &proc_sn2_ptc_operations;
  435. spin_lock_init(&sn2_global_ptc_lock);
  436. return 0;
  437. }
  438. static void __exit sn2_ptc_exit(void)
  439. {
  440. remove_proc_entry(PTC_BASENAME, NULL);
  441. }
  442. module_init(sn2_ptc_init);
  443. module_exit(sn2_ptc_exit);
  444. #endif /* CONFIG_PROC_FS */