sn2_smp.c 15 KB

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