2 * SN2 Platform specific SMP Support
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
8 * Copyright (C) 2000-2006 Silicon Graphics, Inc. All rights reserved.
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/spinlock.h>
14 #include <linux/threads.h>
15 #include <linux/sched.h>
16 #include <linux/smp.h>
17 #include <linux/interrupt.h>
18 #include <linux/irq.h>
19 #include <linux/mmzone.h>
20 #include <linux/module.h>
21 #include <linux/bitops.h>
22 #include <linux/nodemask.h>
23 #include <linux/proc_fs.h>
24 #include <linux/seq_file.h>
26 #include <asm/processor.h>
29 #include <asm/system.h>
30 #include <asm/delay.h>
35 #include <asm/hw_irq.h>
36 #include <asm/current.h>
37 #include <asm/sn/sn_cpuid.h>
38 #include <asm/sn/sn_sal.h>
39 #include <asm/sn/addrs.h>
40 #include <asm/sn/shub_mmr.h>
41 #include <asm/sn/nodepda.h>
42 #include <asm/sn/rw_mmr.h>
44 DEFINE_PER_CPU(struct ptc_stats, ptcstats);
45 DECLARE_PER_CPU(struct ptc_stats, ptcstats);
47 static __cacheline_aligned DEFINE_SPINLOCK(sn2_global_ptc_lock);
50 sn2_ptc_deadlock_recovery_core(volatile unsigned long *, unsigned long,
51 volatile unsigned long *, unsigned long,
52 volatile unsigned long *, unsigned long);
54 sn2_ptc_deadlock_recovery(short *, short, short, int,
55 volatile unsigned long *, unsigned long,
56 volatile unsigned long *, unsigned long);
59 * Note: some is the following is captured here to make degugging easier
60 * (the macros make more sense if you see the debug patch - not posted)
63 #define local_node_uses_ptc_ga(sh1) ((sh1) ? 1 : 0)
64 #define max_active_pio(sh1) ((sh1) ? 32 : 7)
65 #define reset_max_active_on_deadlock() 1
66 #define PTC_LOCK(sh1) ((sh1) ? &sn2_global_ptc_lock : &sn_nodepda->ptc_lock)
70 unsigned long change_rid;
71 unsigned long shub_ptc_flushes;
72 unsigned long nodes_flushed;
73 unsigned long deadlocks;
74 unsigned long deadlocks2;
75 unsigned long lock_itc_clocks;
76 unsigned long shub_itc_clocks;
77 unsigned long shub_itc_clocks_max;
78 unsigned long shub_ptc_flushes_not_my_mm;
83 static inline unsigned long wait_piowc(void)
85 volatile unsigned long *piows;
86 unsigned long zeroval, ws;
88 piows = pda->pio_write_status_addr;
89 zeroval = pda->pio_write_status_val;
92 } while (((ws = *piows) & SH_PIO_WRITE_STATUS_PENDING_WRITE_COUNT_MASK) != zeroval);
93 return (ws & SH_PIO_WRITE_STATUS_WRITE_DEADLOCK_MASK) != 0;
97 * sn_migrate - SN-specific task migration actions
98 * @task: Task being migrated to new CPU
100 * SN2 PIO writes from separate CPUs are not guaranteed to arrive in order.
101 * Context switching user threads which have memory-mapped MMIO may cause
102 * PIOs to issue from seperate CPUs, thus the PIO writes must be drained
103 * from the previous CPU's Shub before execution resumes on the new CPU.
105 void sn_migrate(struct task_struct *task)
107 pda_t *last_pda = pdacpu(task_thread_info(task)->last_cpu);
108 volatile unsigned long *adr = last_pda->pio_write_status_addr;
109 unsigned long val = last_pda->pio_write_status_val;
111 /* Drain PIO writes from old CPU's Shub */
112 while (unlikely((*adr & SH_PIO_WRITE_STATUS_PENDING_WRITE_COUNT_MASK)
117 void sn_tlb_migrate_finish(struct mm_struct *mm)
119 /* flush_tlb_mm is inefficient if more than 1 users of mm */
120 if (mm == current->mm && mm && atomic_read(&mm->mm_users) == 1)
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))
131 * Purges the translation caches of all processors of the given virtual address
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.
147 sn2_global_tlb_purge(struct mm_struct *mm, unsigned long start,
148 unsigned long end, unsigned long nbits)
150 int i, ibegin, shub1, cnode, mynasid, cpu, lcpu = 0, nasid;
151 int mymm = (mm == current->active_mm && mm == current->mm);
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;
159 nodes_clear(nodes_flushed);
162 for_each_cpu_mask(cpu, mm->cpu_vm_mask) {
163 cnode = cpu_to_node(cpu);
164 node_set(cnode, nodes_flushed);
174 if (likely(i == 1 && lcpu == smp_processor_id() && mymm)) {
176 ia64_ptcl(start, nbits << 2);
177 start += (1UL << nbits);
178 } while (start < end);
180 __get_cpu_var(ptcstats).ptc_l++;
185 if (atomic_read(&mm->mm_users) == 1 && mymm) {
187 __get_cpu_var(ptcstats).change_rid++;
192 itc = ia64_get_itc();
194 for_each_node_mask(cnode, nodes_flushed)
195 nasids[nix++] = cnodeid_to_nasid(cnode);
197 rr_value = (mm->context << 3) | REGION_NUMBER(start);
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);
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));
217 mynasid = get_nasid();
218 use_cpu_ptcga = local_node_uses_ptc_ga(shub1);
219 max_active = max_active_pio(shub1);
221 itc = ia64_get_itc();
222 spin_lock_irqsave(PTC_LOCK(shub1), flags);
223 itc2 = ia64_get_itc();
225 __get_cpu_var(ptcstats).lock_itc_clocks += itc2 - itc;
226 __get_cpu_var(ptcstats).shub_ptc_flushes++;
227 __get_cpu_var(ptcstats).nodes_flushed += nix;
229 __get_cpu_var(ptcstats).shub_ptc_flushes_not_my_mm++;
231 if (use_cpu_ptcga && !mymm) {
232 old_rr = ia64_get_rr(start);
233 ia64_set_rr(start, (old_rr & 0xff) | (rr_value << 8));
240 data1 = start | (1UL << SH1_PTC_1_START_SHFT);
242 data0 = (data0 & ~SH2_PTC_ADDR_MASK) | (start & SH2_PTC_ADDR_MASK);
245 for (ibegin = 0, i = 0; i < nix; i++) {
247 if (use_cpu_ptcga && unlikely(nasid == mynasid)) {
248 ia64_ptcga(start, nbits << 2);
251 ptc0 = CHANGE_NASID(nasid, ptc0);
253 ptc1 = CHANGE_NASID(nasid, ptc1);
254 pio_atomic_phys_write_mmrs(ptc0, data0, ptc1, data1);
257 if (active >= max_active || i == (nix - 1)) {
258 if ((deadlock = wait_piowc())) {
259 sn2_ptc_deadlock_recovery(nasids, ibegin, i, mynasid, ptc0, data0, ptc1, data1);
260 if (reset_max_active_on_deadlock())
267 start += (1UL << nbits);
268 } while (start < end);
270 itc2 = ia64_get_itc() - itc2;
271 __get_cpu_var(ptcstats).shub_itc_clocks += itc2;
272 if (itc2 > __get_cpu_var(ptcstats).shub_itc_clocks_max)
273 __get_cpu_var(ptcstats).shub_itc_clocks_max = itc2;
276 ia64_set_rr(start, old_rr);
280 spin_unlock_irqrestore(PTC_LOCK(shub1), flags);
286 * sn2_ptc_deadlock_recovery
288 * Recover from PTC deadlocks conditions. Recovery requires stepping thru each
289 * TLB flush transaction. The recovery sequence is somewhat tricky & is
290 * coded in assembly language.
294 sn2_ptc_deadlock_recovery(short *nasids, short ib, short ie, int mynasid,
295 volatile unsigned long *ptc0, unsigned long data0,
296 volatile unsigned long *ptc1, unsigned long data1)
299 unsigned long *piows, zeroval, n;
301 __get_cpu_var(ptcstats).deadlocks++;
303 piows = (unsigned long *) pda->pio_write_status_addr;
304 zeroval = pda->pio_write_status_val;
307 for (i=ib; i <= ie; i++) {
309 if (local_node_uses_ptc_ga(is_shub1()) && nasid == mynasid)
311 ptc0 = CHANGE_NASID(nasid, ptc0);
313 ptc1 = CHANGE_NASID(nasid, ptc1);
315 n = sn2_ptc_deadlock_recovery_core(ptc0, data0, ptc1, data1, piows, zeroval);
316 __get_cpu_var(ptcstats).deadlocks2 += n;
322 * sn_send_IPI_phys - send an IPI to a Nasid and slice
323 * @nasid: nasid to receive the interrupt (may be outside partition)
324 * @physid: physical cpuid to receive the interrupt.
325 * @vector: command to send
326 * @delivery_mode: delivery mechanism
328 * Sends an IPI (interprocessor interrupt) to the processor specified by
331 * @delivery_mode can be one of the following
333 * %IA64_IPI_DM_INT - pend an interrupt
334 * %IA64_IPI_DM_PMI - pend a PMI
335 * %IA64_IPI_DM_NMI - pend an NMI
336 * %IA64_IPI_DM_INIT - pend an INIT interrupt
338 void sn_send_IPI_phys(int nasid, long physid, int vector, int delivery_mode)
341 unsigned long flags = 0;
344 p = (long *)GLOBAL_MMR_PHYS_ADDR(nasid, SH_IPI_INT);
345 val = (1UL << SH_IPI_INT_SEND_SHFT) |
346 (physid << SH_IPI_INT_PID_SHFT) |
347 ((long)delivery_mode << SH_IPI_INT_TYPE_SHFT) |
348 ((long)vector << SH_IPI_INT_IDX_SHFT) |
349 (0x000feeUL << SH_IPI_INT_BASE_SHFT);
352 if (enable_shub_wars_1_1()) {
353 spin_lock_irqsave(&sn2_global_ptc_lock, flags);
355 pio_phys_write_mmr(p, val);
356 if (enable_shub_wars_1_1()) {
358 spin_unlock_irqrestore(&sn2_global_ptc_lock, flags);
363 EXPORT_SYMBOL(sn_send_IPI_phys);
366 * sn2_send_IPI - send an IPI to a processor
367 * @cpuid: target of the IPI
368 * @vector: command to send
369 * @delivery_mode: delivery mechanism
370 * @redirect: redirect the IPI?
372 * Sends an IPI (InterProcessor Interrupt) to the processor specified by
373 * @cpuid. @vector specifies the command to send, while @delivery_mode can
374 * be one of the following
376 * %IA64_IPI_DM_INT - pend an interrupt
377 * %IA64_IPI_DM_PMI - pend a PMI
378 * %IA64_IPI_DM_NMI - pend an NMI
379 * %IA64_IPI_DM_INIT - pend an INIT interrupt
381 void sn2_send_IPI(int cpuid, int vector, int delivery_mode, int redirect)
386 physid = cpu_physical_id(cpuid);
387 nasid = cpuid_to_nasid(cpuid);
389 /* the following is used only when starting cpus at boot time */
390 if (unlikely(nasid == -1))
391 ia64_sn_get_sapic_info(physid, &nasid, NULL, NULL);
393 sn_send_IPI_phys(nasid, physid, vector, delivery_mode);
396 #ifdef CONFIG_PROC_FS
398 #define PTC_BASENAME "sgi_sn/ptc_statistics"
400 static void *sn2_ptc_seq_start(struct seq_file *file, loff_t * offset)
402 if (*offset < NR_CPUS)
407 static void *sn2_ptc_seq_next(struct seq_file *file, void *data, loff_t * offset)
410 if (*offset < NR_CPUS)
415 static void sn2_ptc_seq_stop(struct seq_file *file, void *data)
419 static int sn2_ptc_seq_show(struct seq_file *file, void *data)
421 struct ptc_stats *stat;
424 cpu = *(loff_t *) data;
428 "# cpu ptc_l newrid ptc_flushes nodes_flushed deadlocks lock_nsec shub_nsec shub_nsec_max not_my_mm deadlock2\n");
429 seq_printf(file, "# ptctest %d\n", sn2_ptctest);
432 if (cpu < NR_CPUS && cpu_online(cpu)) {
433 stat = &per_cpu(ptcstats, cpu);
434 seq_printf(file, "cpu %d %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld\n", cpu, stat->ptc_l,
435 stat->change_rid, stat->shub_ptc_flushes, stat->nodes_flushed,
437 1000 * stat->lock_itc_clocks / per_cpu(cpu_info, cpu).cyc_per_usec,
438 1000 * stat->shub_itc_clocks / per_cpu(cpu_info, cpu).cyc_per_usec,
439 1000 * stat->shub_itc_clocks_max / per_cpu(cpu_info, cpu).cyc_per_usec,
440 stat->shub_ptc_flushes_not_my_mm,
446 static struct seq_operations sn2_ptc_seq_ops = {
447 .start = sn2_ptc_seq_start,
448 .next = sn2_ptc_seq_next,
449 .stop = sn2_ptc_seq_stop,
450 .show = sn2_ptc_seq_show
453 static int sn2_ptc_proc_open(struct inode *inode, struct file *file)
455 return seq_open(file, &sn2_ptc_seq_ops);
458 static const struct file_operations proc_sn2_ptc_operations = {
459 .open = sn2_ptc_proc_open,
462 .release = seq_release,
465 static struct proc_dir_entry *proc_sn2_ptc;
467 static int __init sn2_ptc_init(void)
469 if (!ia64_platform_is("sn2"))
472 if (!(proc_sn2_ptc = create_proc_entry(PTC_BASENAME, 0444, NULL))) {
473 printk(KERN_ERR "unable to create %s proc entry", PTC_BASENAME);
476 proc_sn2_ptc->proc_fops = &proc_sn2_ptc_operations;
477 spin_lock_init(&sn2_global_ptc_lock);
481 static void __exit sn2_ptc_exit(void)
483 remove_proc_entry(PTC_BASENAME, NULL);
486 module_init(sn2_ptc_init);
487 module_exit(sn2_ptc_exit);
488 #endif /* CONFIG_PROC_FS */