sn2_smp.c 14 KB

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