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;
96 void sn_tlb_migrate_finish(struct mm_struct *mm)
98 /* flush_tlb_mm is inefficient if more than 1 users of mm */
99 if (mm == current->mm && mm && atomic_read(&mm->mm_users) == 1)
104 * sn2_global_tlb_purge - globally purge translation cache of virtual address range
105 * @mm: mm_struct containing virtual address range
106 * @start: start of virtual address range
107 * @end: end of virtual address range
108 * @nbits: specifies number of bytes to purge per instruction (num = 1<<(nbits & 0xfc))
110 * Purges the translation caches of all processors of the given virtual address
114 * - cpu_vm_mask is a bit mask that indicates which cpus have loaded the context.
115 * - cpu_vm_mask is converted into a nodemask of the nodes containing the
116 * cpus in cpu_vm_mask.
117 * - if only one bit is set in cpu_vm_mask & it is the current cpu & the
118 * process is purging its own virtual address range, then only the
119 * local TLB needs to be flushed. This flushing can be done using
120 * ptc.l. This is the common case & avoids the global spinlock.
121 * - if multiple cpus have loaded the context, then flushing has to be
122 * done with ptc.g/MMRs under protection of the global ptc_lock.
126 sn2_global_tlb_purge(struct mm_struct *mm, unsigned long start,
127 unsigned long end, unsigned long nbits)
129 int i, ibegin, shub1, cnode, mynasid, cpu, lcpu = 0, nasid;
130 int mymm = (mm == current->active_mm && mm == current->mm);
132 volatile unsigned long *ptc0, *ptc1;
133 unsigned long itc, itc2, flags, data0 = 0, data1 = 0, rr_value, old_rr = 0;
134 short nasids[MAX_NUMNODES], nix;
135 nodemask_t nodes_flushed;
136 int active, max_active, deadlock;
138 nodes_clear(nodes_flushed);
141 for_each_cpu_mask(cpu, mm->cpu_vm_mask) {
142 cnode = cpu_to_node(cpu);
143 node_set(cnode, nodes_flushed);
153 if (likely(i == 1 && lcpu == smp_processor_id() && mymm)) {
155 ia64_ptcl(start, nbits << 2);
156 start += (1UL << nbits);
157 } while (start < end);
159 __get_cpu_var(ptcstats).ptc_l++;
164 if (atomic_read(&mm->mm_users) == 1 && mymm) {
166 __get_cpu_var(ptcstats).change_rid++;
171 itc = ia64_get_itc();
173 for_each_node_mask(cnode, nodes_flushed)
174 nasids[nix++] = cnodeid_to_nasid(cnode);
176 rr_value = (mm->context << 3) | REGION_NUMBER(start);
180 data0 = (1UL << SH1_PTC_0_A_SHFT) |
181 (nbits << SH1_PTC_0_PS_SHFT) |
182 (rr_value << SH1_PTC_0_RID_SHFT) |
183 (1UL << SH1_PTC_0_START_SHFT);
184 ptc0 = (long *)GLOBAL_MMR_PHYS_ADDR(0, SH1_PTC_0);
185 ptc1 = (long *)GLOBAL_MMR_PHYS_ADDR(0, SH1_PTC_1);
187 data0 = (1UL << SH2_PTC_A_SHFT) |
188 (nbits << SH2_PTC_PS_SHFT) |
189 (1UL << SH2_PTC_START_SHFT);
190 ptc0 = (long *)GLOBAL_MMR_PHYS_ADDR(0, SH2_PTC +
191 (rr_value << SH2_PTC_RID_SHFT));
196 mynasid = get_nasid();
197 use_cpu_ptcga = local_node_uses_ptc_ga(shub1);
198 max_active = max_active_pio(shub1);
200 itc = ia64_get_itc();
201 spin_lock_irqsave(PTC_LOCK(shub1), flags);
202 itc2 = ia64_get_itc();
204 __get_cpu_var(ptcstats).lock_itc_clocks += itc2 - itc;
205 __get_cpu_var(ptcstats).shub_ptc_flushes++;
206 __get_cpu_var(ptcstats).nodes_flushed += nix;
208 __get_cpu_var(ptcstats).shub_ptc_flushes_not_my_mm++;
210 if (use_cpu_ptcga && !mymm) {
211 old_rr = ia64_get_rr(start);
212 ia64_set_rr(start, (old_rr & 0xff) | (rr_value << 8));
219 data1 = start | (1UL << SH1_PTC_1_START_SHFT);
221 data0 = (data0 & ~SH2_PTC_ADDR_MASK) | (start & SH2_PTC_ADDR_MASK);
224 for (ibegin = 0, i = 0; i < nix; i++) {
226 if (use_cpu_ptcga && unlikely(nasid == mynasid)) {
227 ia64_ptcga(start, nbits << 2);
230 ptc0 = CHANGE_NASID(nasid, ptc0);
232 ptc1 = CHANGE_NASID(nasid, ptc1);
233 pio_atomic_phys_write_mmrs(ptc0, data0, ptc1, data1);
236 if (active >= max_active || i == (nix - 1)) {
237 if ((deadlock = wait_piowc())) {
238 sn2_ptc_deadlock_recovery(nasids, ibegin, i, mynasid, ptc0, data0, ptc1, data1);
239 if (reset_max_active_on_deadlock())
246 start += (1UL << nbits);
247 } while (start < end);
249 itc2 = ia64_get_itc() - itc2;
250 __get_cpu_var(ptcstats).shub_itc_clocks += itc2;
251 if (itc2 > __get_cpu_var(ptcstats).shub_itc_clocks_max)
252 __get_cpu_var(ptcstats).shub_itc_clocks_max = itc2;
255 ia64_set_rr(start, old_rr);
259 spin_unlock_irqrestore(PTC_LOCK(shub1), flags);
265 * sn2_ptc_deadlock_recovery
267 * Recover from PTC deadlocks conditions. Recovery requires stepping thru each
268 * TLB flush transaction. The recovery sequence is somewhat tricky & is
269 * coded in assembly language.
273 sn2_ptc_deadlock_recovery(short *nasids, short ib, short ie, int mynasid,
274 volatile unsigned long *ptc0, unsigned long data0,
275 volatile unsigned long *ptc1, unsigned long data1)
278 unsigned long *piows, zeroval, n;
280 __get_cpu_var(ptcstats).deadlocks++;
282 piows = (unsigned long *) pda->pio_write_status_addr;
283 zeroval = pda->pio_write_status_val;
286 for (i=ib; i <= ie; i++) {
288 if (local_node_uses_ptc_ga(is_shub1()) && nasid == mynasid)
290 ptc0 = CHANGE_NASID(nasid, ptc0);
292 ptc1 = CHANGE_NASID(nasid, ptc1);
294 n = sn2_ptc_deadlock_recovery_core(ptc0, data0, ptc1, data1, piows, zeroval);
295 __get_cpu_var(ptcstats).deadlocks2 += n;
301 * sn_send_IPI_phys - send an IPI to a Nasid and slice
302 * @nasid: nasid to receive the interrupt (may be outside partition)
303 * @physid: physical cpuid to receive the interrupt.
304 * @vector: command to send
305 * @delivery_mode: delivery mechanism
307 * Sends an IPI (interprocessor interrupt) to the processor specified by
310 * @delivery_mode can be one of the following
312 * %IA64_IPI_DM_INT - pend an interrupt
313 * %IA64_IPI_DM_PMI - pend a PMI
314 * %IA64_IPI_DM_NMI - pend an NMI
315 * %IA64_IPI_DM_INIT - pend an INIT interrupt
317 void sn_send_IPI_phys(int nasid, long physid, int vector, int delivery_mode)
320 unsigned long flags = 0;
323 p = (long *)GLOBAL_MMR_PHYS_ADDR(nasid, SH_IPI_INT);
324 val = (1UL << SH_IPI_INT_SEND_SHFT) |
325 (physid << SH_IPI_INT_PID_SHFT) |
326 ((long)delivery_mode << SH_IPI_INT_TYPE_SHFT) |
327 ((long)vector << SH_IPI_INT_IDX_SHFT) |
328 (0x000feeUL << SH_IPI_INT_BASE_SHFT);
331 if (enable_shub_wars_1_1()) {
332 spin_lock_irqsave(&sn2_global_ptc_lock, flags);
334 pio_phys_write_mmr(p, val);
335 if (enable_shub_wars_1_1()) {
337 spin_unlock_irqrestore(&sn2_global_ptc_lock, flags);
342 EXPORT_SYMBOL(sn_send_IPI_phys);
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?
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
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
360 void sn2_send_IPI(int cpuid, int vector, int delivery_mode, int redirect)
365 physid = cpu_physical_id(cpuid);
366 nasid = cpuid_to_nasid(cpuid);
368 /* the following is used only when starting cpus at boot time */
369 if (unlikely(nasid == -1))
370 ia64_sn_get_sapic_info(physid, &nasid, NULL, NULL);
372 sn_send_IPI_phys(nasid, physid, vector, delivery_mode);
375 #ifdef CONFIG_PROC_FS
377 #define PTC_BASENAME "sgi_sn/ptc_statistics"
379 static void *sn2_ptc_seq_start(struct seq_file *file, loff_t * offset)
381 if (*offset < NR_CPUS)
386 static void *sn2_ptc_seq_next(struct seq_file *file, void *data, loff_t * offset)
389 if (*offset < NR_CPUS)
394 static void sn2_ptc_seq_stop(struct seq_file *file, void *data)
398 static int sn2_ptc_seq_show(struct seq_file *file, void *data)
400 struct ptc_stats *stat;
403 cpu = *(loff_t *) data;
407 "# cpu ptc_l newrid ptc_flushes nodes_flushed deadlocks lock_nsec shub_nsec shub_nsec_max not_my_mm deadlock2\n");
408 seq_printf(file, "# ptctest %d\n", sn2_ptctest);
411 if (cpu < NR_CPUS && cpu_online(cpu)) {
412 stat = &per_cpu(ptcstats, cpu);
413 seq_printf(file, "cpu %d %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld\n", cpu, stat->ptc_l,
414 stat->change_rid, stat->shub_ptc_flushes, stat->nodes_flushed,
416 1000 * stat->lock_itc_clocks / per_cpu(cpu_info, cpu).cyc_per_usec,
417 1000 * stat->shub_itc_clocks / per_cpu(cpu_info, cpu).cyc_per_usec,
418 1000 * stat->shub_itc_clocks_max / per_cpu(cpu_info, cpu).cyc_per_usec,
419 stat->shub_ptc_flushes_not_my_mm,
425 static struct seq_operations sn2_ptc_seq_ops = {
426 .start = sn2_ptc_seq_start,
427 .next = sn2_ptc_seq_next,
428 .stop = sn2_ptc_seq_stop,
429 .show = sn2_ptc_seq_show
432 static int sn2_ptc_proc_open(struct inode *inode, struct file *file)
434 return seq_open(file, &sn2_ptc_seq_ops);
437 static struct file_operations proc_sn2_ptc_operations = {
438 .open = sn2_ptc_proc_open,
441 .release = seq_release,
444 static struct proc_dir_entry *proc_sn2_ptc;
446 static int __init sn2_ptc_init(void)
448 if (!ia64_platform_is("sn2"))
451 if (!(proc_sn2_ptc = create_proc_entry(PTC_BASENAME, 0444, NULL))) {
452 printk(KERN_ERR "unable to create %s proc entry", PTC_BASENAME);
455 proc_sn2_ptc->proc_fops = &proc_sn2_ptc_operations;
456 spin_lock_init(&sn2_global_ptc_lock);
460 static void __exit sn2_ptc_exit(void)
462 remove_proc_entry(PTC_BASENAME, NULL);
465 module_init(sn2_ptc_init);
466 module_exit(sn2_ptc_exit);
467 #endif /* CONFIG_PROC_FS */