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