[PATCH] ieee80211: fix sparse warning about missing "static"
[sfrench/cifs-2.6.git] / arch / ia64 / sn / kernel / sn2 / sn2_smp.c
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
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>
25
26 #include <asm/processor.h>
27 #include <asm/irq.h>
28 #include <asm/sal.h>
29 #include <asm/system.h>
30 #include <asm/delay.h>
31 #include <asm/io.h>
32 #include <asm/smp.h>
33 #include <asm/tlb.h>
34 #include <asm/numa.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>
43
44 DEFINE_PER_CPU(struct ptc_stats, ptcstats);
45 DECLARE_PER_CPU(struct ptc_stats, ptcstats);
46
47 static  __cacheline_aligned DEFINE_SPINLOCK(sn2_global_ptc_lock);
48
49 void sn2_ptc_deadlock_recovery(short *, short, short, int, volatile unsigned long *, unsigned long,
50         volatile unsigned long *, unsigned long);
51
52 /*
53  * Note: some is the following is captured here to make degugging easier
54  * (the macros make more sense if you see the debug patch - not posted)
55  */
56 #define sn2_ptctest     0
57 #define local_node_uses_ptc_ga(sh1)     ((sh1) ? 1 : 0)
58 #define max_active_pio(sh1)             ((sh1) ? 32 : 7)
59 #define reset_max_active_on_deadlock()  1
60 #define PTC_LOCK(sh1)                   ((sh1) ? &sn2_global_ptc_lock : &sn_nodepda->ptc_lock)
61
62 static inline void ptc_lock(int sh1, unsigned long *flagp)
63 {
64         spin_lock_irqsave(PTC_LOCK(sh1), *flagp);
65 }
66
67 static inline void ptc_unlock(int sh1, unsigned long flags)
68 {
69         spin_unlock_irqrestore(PTC_LOCK(sh1), flags);
70 }
71
72 struct ptc_stats {
73         unsigned long ptc_l;
74         unsigned long change_rid;
75         unsigned long shub_ptc_flushes;
76         unsigned long nodes_flushed;
77         unsigned long deadlocks;
78         unsigned long deadlocks2;
79         unsigned long lock_itc_clocks;
80         unsigned long shub_itc_clocks;
81         unsigned long shub_itc_clocks_max;
82         unsigned long shub_ptc_flushes_not_my_mm;
83 };
84
85 static inline unsigned long wait_piowc(void)
86 {
87         volatile unsigned long *piows;
88         unsigned long zeroval, ws;
89
90         piows = pda->pio_write_status_addr;
91         zeroval = pda->pio_write_status_val;
92         do {
93                 cpu_relax();
94         } while (((ws = *piows) & SH_PIO_WRITE_STATUS_PENDING_WRITE_COUNT_MASK) != zeroval);
95         return (ws & SH_PIO_WRITE_STATUS_WRITE_DEADLOCK_MASK) != 0;
96 }
97
98 void sn_tlb_migrate_finish(struct mm_struct *mm)
99 {
100         /* flush_tlb_mm is inefficient if more than 1 users of mm */
101         if (mm == current->mm && mm && atomic_read(&mm->mm_users) == 1)
102                 flush_tlb_mm(mm);
103 }
104
105 /**
106  * sn2_global_tlb_purge - globally purge translation cache of virtual address range
107  * @mm: mm_struct containing virtual address range
108  * @start: start of virtual address range
109  * @end: end of virtual address range
110  * @nbits: specifies number of bytes to purge per instruction (num = 1<<(nbits & 0xfc))
111  *
112  * Purges the translation caches of all processors of the given virtual address
113  * range.
114  *
115  * Note:
116  *      - cpu_vm_mask is a bit mask that indicates which cpus have loaded the context.
117  *      - cpu_vm_mask is converted into a nodemask of the nodes containing the
118  *        cpus in cpu_vm_mask.
119  *      - if only one bit is set in cpu_vm_mask & it is the current cpu & the
120  *        process is purging its own virtual address range, then only the
121  *        local TLB needs to be flushed. This flushing can be done using
122  *        ptc.l. This is the common case & avoids the global spinlock.
123  *      - if multiple cpus have loaded the context, then flushing has to be
124  *        done with ptc.g/MMRs under protection of the global ptc_lock.
125  */
126
127 void
128 sn2_global_tlb_purge(struct mm_struct *mm, unsigned long start,
129                      unsigned long end, unsigned long nbits)
130 {
131         int i, ibegin, shub1, cnode, mynasid, cpu, lcpu = 0, nasid;
132         int mymm = (mm == current->active_mm && mm == current->mm);
133         int use_cpu_ptcga;
134         volatile unsigned long *ptc0, *ptc1;
135         unsigned long itc, itc2, flags, data0 = 0, data1 = 0, rr_value, old_rr = 0;
136         short nasids[MAX_NUMNODES], nix;
137         nodemask_t nodes_flushed;
138         int active, max_active, deadlock;
139
140         nodes_clear(nodes_flushed);
141         i = 0;
142
143         for_each_cpu_mask(cpu, mm->cpu_vm_mask) {
144                 cnode = cpu_to_node(cpu);
145                 node_set(cnode, nodes_flushed);
146                 lcpu = cpu;
147                 i++;
148         }
149
150         if (i == 0)
151                 return;
152
153         preempt_disable();
154
155         if (likely(i == 1 && lcpu == smp_processor_id() && mymm)) {
156                 do {
157                         ia64_ptcl(start, nbits << 2);
158                         start += (1UL << nbits);
159                 } while (start < end);
160                 ia64_srlz_i();
161                 __get_cpu_var(ptcstats).ptc_l++;
162                 preempt_enable();
163                 return;
164         }
165
166         if (atomic_read(&mm->mm_users) == 1 && mymm) {
167                 flush_tlb_mm(mm);
168                 __get_cpu_var(ptcstats).change_rid++;
169                 preempt_enable();
170                 return;
171         }
172
173         itc = ia64_get_itc();
174         nix = 0;
175         for_each_node_mask(cnode, nodes_flushed)
176                 nasids[nix++] = cnodeid_to_nasid(cnode);
177
178         rr_value = (mm->context << 3) | REGION_NUMBER(start);
179
180         shub1 = is_shub1();
181         if (shub1) {
182                 data0 = (1UL << SH1_PTC_0_A_SHFT) |
183                         (nbits << SH1_PTC_0_PS_SHFT) |
184                         (rr_value << SH1_PTC_0_RID_SHFT) |
185                         (1UL << SH1_PTC_0_START_SHFT);
186                 ptc0 = (long *)GLOBAL_MMR_PHYS_ADDR(0, SH1_PTC_0);
187                 ptc1 = (long *)GLOBAL_MMR_PHYS_ADDR(0, SH1_PTC_1);
188         } else {
189                 data0 = (1UL << SH2_PTC_A_SHFT) |
190                         (nbits << SH2_PTC_PS_SHFT) |
191                         (1UL << SH2_PTC_START_SHFT);
192                 ptc0 = (long *)GLOBAL_MMR_PHYS_ADDR(0, SH2_PTC + 
193                         (rr_value << SH2_PTC_RID_SHFT));
194                 ptc1 = NULL;
195         }
196         
197
198         mynasid = get_nasid();
199         use_cpu_ptcga = local_node_uses_ptc_ga(shub1);
200         max_active = max_active_pio(shub1);
201
202         itc = ia64_get_itc();
203         ptc_lock(shub1, &flags);
204         itc2 = ia64_get_itc();
205
206         __get_cpu_var(ptcstats).lock_itc_clocks += itc2 - itc;
207         __get_cpu_var(ptcstats).shub_ptc_flushes++;
208         __get_cpu_var(ptcstats).nodes_flushed += nix;
209         if (!mymm)
210                  __get_cpu_var(ptcstats).shub_ptc_flushes_not_my_mm++;
211
212         if (use_cpu_ptcga && !mymm) {
213                 old_rr = ia64_get_rr(start);
214                 ia64_set_rr(start, (old_rr & 0xff) | (rr_value << 8));
215                 ia64_srlz_d();
216         }
217
218         wait_piowc();
219         do {
220                 if (shub1)
221                         data1 = start | (1UL << SH1_PTC_1_START_SHFT);
222                 else
223                         data0 = (data0 & ~SH2_PTC_ADDR_MASK) | (start & SH2_PTC_ADDR_MASK);
224                 deadlock = 0;
225                 active = 0;
226                 for (ibegin = 0, i = 0; i < nix; i++) {
227                         nasid = nasids[i];
228                         if (use_cpu_ptcga && unlikely(nasid == mynasid)) {
229                                 ia64_ptcga(start, nbits << 2);
230                                 ia64_srlz_i();
231                         } else {
232                                 ptc0 = CHANGE_NASID(nasid, ptc0);
233                                 if (ptc1)
234                                         ptc1 = CHANGE_NASID(nasid, ptc1);
235                                 pio_atomic_phys_write_mmrs(ptc0, data0, ptc1, data1);
236                                 active++;
237                         }
238                         if (active >= max_active || i == (nix - 1)) {
239                                 if ((deadlock = wait_piowc())) {
240                                         sn2_ptc_deadlock_recovery(nasids, ibegin, i, mynasid, ptc0, data0, ptc1, data1);
241                                         if (reset_max_active_on_deadlock())
242                                                 max_active = 1;
243                                 }
244                                 active = 0;
245                                 ibegin = i + 1;
246                         }
247                 }
248                 start += (1UL << nbits);
249         } while (start < end);
250
251         itc2 = ia64_get_itc() - itc2;
252         __get_cpu_var(ptcstats).shub_itc_clocks += itc2;
253         if (itc2 > __get_cpu_var(ptcstats).shub_itc_clocks_max)
254                 __get_cpu_var(ptcstats).shub_itc_clocks_max = itc2;
255
256         if (old_rr) {
257                 ia64_set_rr(start, old_rr);
258                 ia64_srlz_d();
259         }
260
261         ptc_unlock(shub1, flags);
262
263         preempt_enable();
264 }
265
266 /*
267  * sn2_ptc_deadlock_recovery
268  *
269  * Recover from PTC deadlocks conditions. Recovery requires stepping thru each 
270  * TLB flush transaction.  The recovery sequence is somewhat tricky & is
271  * coded in assembly language.
272  */
273 void sn2_ptc_deadlock_recovery(short *nasids, short ib, short ie, int mynasid, volatile unsigned long *ptc0, unsigned long data0,
274         volatile unsigned long *ptc1, unsigned long data1)
275 {
276         extern unsigned long sn2_ptc_deadlock_recovery_core(volatile unsigned long *, unsigned long,
277                 volatile unsigned long *, unsigned long, volatile unsigned long *, unsigned long);
278         short nasid, i;
279         unsigned long *piows, zeroval, n;
280
281         __get_cpu_var(ptcstats).deadlocks++;
282
283         piows = (unsigned long *) pda->pio_write_status_addr;
284         zeroval = pda->pio_write_status_val;
285
286
287         for (i=ib; i <= ie; i++) {
288                 nasid = nasids[i];
289                 if (local_node_uses_ptc_ga(is_shub1()) && nasid == mynasid)
290                         continue;
291                 ptc0 = CHANGE_NASID(nasid, ptc0);
292                 if (ptc1)
293                         ptc1 = CHANGE_NASID(nasid, ptc1);
294
295                 n = sn2_ptc_deadlock_recovery_core(ptc0, data0, ptc1, data1, piows, zeroval);
296                 __get_cpu_var(ptcstats).deadlocks2 += n;
297         }
298
299 }
300
301 /**
302  * sn_send_IPI_phys - send an IPI to a Nasid and slice
303  * @nasid: nasid to receive the interrupt (may be outside partition)
304  * @physid: physical cpuid to receive the interrupt.
305  * @vector: command to send
306  * @delivery_mode: delivery mechanism
307  *
308  * Sends an IPI (interprocessor interrupt) to the processor specified by
309  * @physid
310  *
311  * @delivery_mode can be one of the following
312  *
313  * %IA64_IPI_DM_INT - pend an interrupt
314  * %IA64_IPI_DM_PMI - pend a PMI
315  * %IA64_IPI_DM_NMI - pend an NMI
316  * %IA64_IPI_DM_INIT - pend an INIT interrupt
317  */
318 void sn_send_IPI_phys(int nasid, long physid, int vector, int delivery_mode)
319 {
320         long val;
321         unsigned long flags = 0;
322         volatile long *p;
323
324         p = (long *)GLOBAL_MMR_PHYS_ADDR(nasid, SH_IPI_INT);
325         val = (1UL << SH_IPI_INT_SEND_SHFT) |
326             (physid << SH_IPI_INT_PID_SHFT) |
327             ((long)delivery_mode << SH_IPI_INT_TYPE_SHFT) |
328             ((long)vector << SH_IPI_INT_IDX_SHFT) |
329             (0x000feeUL << SH_IPI_INT_BASE_SHFT);
330
331         mb();
332         if (enable_shub_wars_1_1()) {
333                 spin_lock_irqsave(&sn2_global_ptc_lock, flags);
334         }
335         pio_phys_write_mmr(p, val);
336         if (enable_shub_wars_1_1()) {
337                 wait_piowc();
338                 spin_unlock_irqrestore(&sn2_global_ptc_lock, flags);
339         }
340
341 }
342
343 EXPORT_SYMBOL(sn_send_IPI_phys);
344
345 /**
346  * sn2_send_IPI - send an IPI to a processor
347  * @cpuid: target of the IPI
348  * @vector: command to send
349  * @delivery_mode: delivery mechanism
350  * @redirect: redirect the IPI?
351  *
352  * Sends an IPI (InterProcessor Interrupt) to the processor specified by
353  * @cpuid.  @vector specifies the command to send, while @delivery_mode can 
354  * be one of the following
355  *
356  * %IA64_IPI_DM_INT - pend an interrupt
357  * %IA64_IPI_DM_PMI - pend a PMI
358  * %IA64_IPI_DM_NMI - pend an NMI
359  * %IA64_IPI_DM_INIT - pend an INIT interrupt
360  */
361 void sn2_send_IPI(int cpuid, int vector, int delivery_mode, int redirect)
362 {
363         long physid;
364         int nasid;
365
366         physid = cpu_physical_id(cpuid);
367         nasid = cpuid_to_nasid(cpuid);
368
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
373         sn_send_IPI_phys(nasid, physid, vector, delivery_mode);
374 }
375
376 #ifdef CONFIG_PROC_FS
377
378 #define PTC_BASENAME    "sgi_sn/ptc_statistics"
379
380 static void *sn2_ptc_seq_start(struct seq_file *file, loff_t * offset)
381 {
382         if (*offset < NR_CPUS)
383                 return offset;
384         return NULL;
385 }
386
387 static void *sn2_ptc_seq_next(struct seq_file *file, void *data, loff_t * offset)
388 {
389         (*offset)++;
390         if (*offset < NR_CPUS)
391                 return offset;
392         return NULL;
393 }
394
395 static void sn2_ptc_seq_stop(struct seq_file *file, void *data)
396 {
397 }
398
399 static int sn2_ptc_seq_show(struct seq_file *file, void *data)
400 {
401         struct ptc_stats *stat;
402         int cpu;
403
404         cpu = *(loff_t *) data;
405
406         if (!cpu) {
407                 seq_printf(file,
408                            "# cpu ptc_l newrid ptc_flushes nodes_flushed deadlocks lock_nsec shub_nsec shub_nsec_max not_my_mm deadlock2\n");
409                 seq_printf(file, "# ptctest %d\n", sn2_ptctest);
410         }
411
412         if (cpu < NR_CPUS && cpu_online(cpu)) {
413                 stat = &per_cpu(ptcstats, cpu);
414                 seq_printf(file, "cpu %d %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld\n", cpu, stat->ptc_l,
415                                 stat->change_rid, stat->shub_ptc_flushes, stat->nodes_flushed,
416                                 stat->deadlocks,
417                                 1000 * stat->lock_itc_clocks / per_cpu(cpu_info, cpu).cyc_per_usec,
418                                 1000 * stat->shub_itc_clocks / per_cpu(cpu_info, cpu).cyc_per_usec,
419                                 1000 * stat->shub_itc_clocks_max / per_cpu(cpu_info, cpu).cyc_per_usec,
420                                 stat->shub_ptc_flushes_not_my_mm,
421                                 stat->deadlocks2);
422         }
423         return 0;
424 }
425
426 static struct seq_operations sn2_ptc_seq_ops = {
427         .start = sn2_ptc_seq_start,
428         .next = sn2_ptc_seq_next,
429         .stop = sn2_ptc_seq_stop,
430         .show = sn2_ptc_seq_show
431 };
432
433 static int sn2_ptc_proc_open(struct inode *inode, struct file *file)
434 {
435         return seq_open(file, &sn2_ptc_seq_ops);
436 }
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
445 static struct proc_dir_entry *proc_sn2_ptc;
446
447 static int __init sn2_ptc_init(void)
448 {
449         if (!ia64_platform_is("sn2"))
450                 return -ENOSYS;
451
452         if (!(proc_sn2_ptc = create_proc_entry(PTC_BASENAME, 0444, NULL))) {
453                 printk(KERN_ERR "unable to create %s proc entry", PTC_BASENAME);
454                 return -EINVAL;
455         }
456         proc_sn2_ptc->proc_fops = &proc_sn2_ptc_operations;
457         spin_lock_init(&sn2_global_ptc_lock);
458         return 0;
459 }
460
461 static void __exit sn2_ptc_exit(void)
462 {
463         remove_proc_entry(PTC_BASENAME, NULL);
464 }
465
466 module_init(sn2_ptc_init);
467 module_exit(sn2_ptc_exit);
468 #endif /* CONFIG_PROC_FS */
469