upgrade to linux 2.6.9-1.11_FC2
[linux-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-2004 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
23 #include <asm/processor.h>
24 #include <asm/irq.h>
25 #include <asm/sal.h>
26 #include <asm/system.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
41 void sn2_ptc_deadlock_recovery(unsigned long data0, unsigned long data1);
42
43 static spinlock_t sn2_global_ptc_lock __cacheline_aligned = SPIN_LOCK_UNLOCKED;
44
45 static unsigned long sn2_ptc_deadlock_count;
46
47 static inline unsigned long wait_piowc(void)
48 {
49         volatile unsigned long *piows;
50         unsigned long ws;
51
52         piows = pda->pio_write_status_addr;
53         do {
54                 ia64_mfa();
55         } while (((ws =
56                    *piows) & SH_PIO_WRITE_STATUS_0_PENDING_WRITE_COUNT_MASK) !=
57                  SH_PIO_WRITE_STATUS_0_PENDING_WRITE_COUNT_MASK);
58         return ws;
59 }
60
61 void sn_tlb_migrate_finish(struct mm_struct *mm)
62 {
63         if (mm == current->mm)
64                 flush_tlb_mm(mm);
65 }
66
67 /**
68  * sn2_global_tlb_purge - globally purge translation cache of virtual address range
69  * @start: start of virtual address range
70  * @end: end of virtual address range
71  * @nbits: specifies number of bytes to purge per instruction (num = 1<<(nbits & 0xfc))
72  *
73  * Purges the translation caches of all processors of the given virtual address
74  * range.
75  *
76  * Note:
77  *      - cpu_vm_mask is a bit mask that indicates which cpus have loaded the context.
78  *      - cpu_vm_mask is converted into a nodemask of the nodes containing the
79  *        cpus in cpu_vm_mask.
80  *      - if only one bit is set in cpu_vm_mask & it is the current cpu,
81  *        then only the local TLB needs to be flushed. This flushing can be done
82  *        using ptc.l. This is the common case & avoids the global spinlock.
83  *      - if multiple cpus have loaded the context, then flushing has to be
84  *        done with ptc.g/MMRs under protection of the global ptc_lock.
85  */
86
87 void
88 sn2_global_tlb_purge(unsigned long start, unsigned long end,
89                      unsigned long nbits)
90 {
91         int i, cnode, mynasid, cpu, lcpu = 0, nasid, flushed = 0;
92         volatile unsigned long *ptc0, *ptc1;
93         unsigned long flags = 0, data0, data1;
94         struct mm_struct *mm = current->active_mm;
95         short nasids[NR_NODES], nix;
96         DECLARE_BITMAP(nodes_flushed, NR_NODES);
97
98         bitmap_zero(nodes_flushed, NR_NODES);
99
100         i = 0;
101
102         for_each_cpu_mask(cpu, mm->cpu_vm_mask) {
103                 cnode = cpu_to_node(cpu);
104                 __set_bit(cnode, nodes_flushed);
105                 lcpu = cpu;
106                 i++;
107         }
108
109         preempt_disable();
110
111         if (likely(i == 1 && lcpu == smp_processor_id())) {
112                 do {
113                         ia64_ptcl(start, nbits << 2);
114                         start += (1UL << nbits);
115                 } while (start < end);
116                 ia64_srlz_i();
117                 preempt_enable();
118                 return;
119         }
120
121         if (atomic_read(&mm->mm_users) == 1) {
122                 flush_tlb_mm(mm);
123                 preempt_enable();
124                 return;
125         }
126
127         nix = 0;
128         for (cnode = find_first_bit(&nodes_flushed, NR_NODES); cnode < NR_NODES;
129              cnode = find_next_bit(&nodes_flushed, NR_NODES, ++cnode))
130                 nasids[nix++] = cnodeid_to_nasid(cnode);
131
132         data0 = (1UL << SH_PTC_0_A_SHFT) |
133             (nbits << SH_PTC_0_PS_SHFT) |
134             ((ia64_get_rr(start) >> 8) << SH_PTC_0_RID_SHFT) |
135             (1UL << SH_PTC_0_START_SHFT);
136
137         ptc0 = (long *)GLOBAL_MMR_PHYS_ADDR(0, SH_PTC_0);
138         ptc1 = (long *)GLOBAL_MMR_PHYS_ADDR(0, SH_PTC_1);
139
140         mynasid = get_nasid();
141
142         spin_lock_irqsave(&sn2_global_ptc_lock, flags);
143
144         do {
145                 data1 = start | (1UL << SH_PTC_1_START_SHFT);
146                 for (i = 0; i < nix; i++) {
147                         nasid = nasids[i];
148                         if (likely(nasid == mynasid)) {
149                                 ia64_ptcga(start, nbits << 2);
150                                 ia64_srlz_i();
151                         } else {
152                                 ptc0 = CHANGE_NASID(nasid, ptc0);
153                                 ptc1 = CHANGE_NASID(nasid, ptc1);
154                                 pio_atomic_phys_write_mmrs(ptc0, data0, ptc1,
155                                                            data1);
156                                 flushed = 1;
157                         }
158                 }
159
160                 if (flushed
161                     && (wait_piowc() &
162                         SH_PIO_WRITE_STATUS_0_WRITE_DEADLOCK_MASK)) {
163                         sn2_ptc_deadlock_recovery(data0, data1);
164                 }
165
166                 start += (1UL << nbits);
167
168         } while (start < end);
169
170         spin_unlock_irqrestore(&sn2_global_ptc_lock, flags);
171
172         preempt_enable();
173 }
174
175 /*
176  * sn2_ptc_deadlock_recovery
177  *
178  * Recover from PTC deadlocks conditions. Recovery requires stepping thru each 
179  * TLB flush transaction.  The recovery sequence is somewhat tricky & is
180  * coded in assembly language.
181  */
182 void sn2_ptc_deadlock_recovery(unsigned long data0, unsigned long data1)
183 {
184         extern void sn2_ptc_deadlock_recovery_core(long *, long, long *, long,
185                                                    long *);
186         int cnode, mycnode, nasid;
187         long *ptc0, *ptc1, *piows;
188
189         sn2_ptc_deadlock_count++;
190
191         ptc0 = (long *)GLOBAL_MMR_PHYS_ADDR(0, SH_PTC_0);
192         ptc1 = (long *)GLOBAL_MMR_PHYS_ADDR(0, SH_PTC_1);
193         piows = (long *)pda->pio_write_status_addr;
194
195         mycnode = numa_node_id();
196
197         for (cnode = 0; cnode < numnodes; cnode++) {
198                 if (is_headless_node(cnode) || cnode == mycnode)
199                         continue;
200                 nasid = cnodeid_to_nasid(cnode);
201                 ptc0 = CHANGE_NASID(nasid, ptc0);
202                 ptc1 = CHANGE_NASID(nasid, ptc1);
203                 sn2_ptc_deadlock_recovery_core(ptc0, data0, ptc1, data1, piows);
204         }
205 }
206
207 /**
208  * sn_send_IPI_phys - send an IPI to a Nasid and slice
209  * @nasid: nasid to receive the interrupt (may be outside partition)
210  * @physid: physical cpuid to receive the interrupt.
211  * @vector: command to send
212  * @delivery_mode: delivery mechanism
213  *
214  * Sends an IPI (interprocessor interrupt) to the processor specified by
215  * @physid
216  *
217  * @delivery_mode can be one of the following
218  *
219  * %IA64_IPI_DM_INT - pend an interrupt
220  * %IA64_IPI_DM_PMI - pend a PMI
221  * %IA64_IPI_DM_NMI - pend an NMI
222  * %IA64_IPI_DM_INIT - pend an INIT interrupt
223  */
224 void sn_send_IPI_phys(int nasid, long physid, int vector, int delivery_mode)
225 {
226         long val;
227         unsigned long flags = 0;
228         volatile long *p;
229
230         p = (long *)GLOBAL_MMR_PHYS_ADDR(nasid, SH_IPI_INT);
231         val = (1UL << SH_IPI_INT_SEND_SHFT) |
232             (physid << SH_IPI_INT_PID_SHFT) |
233             ((long)delivery_mode << SH_IPI_INT_TYPE_SHFT) |
234             ((long)vector << SH_IPI_INT_IDX_SHFT) |
235             (0x000feeUL << SH_IPI_INT_BASE_SHFT);
236
237         mb();
238         if (enable_shub_wars_1_1()) {
239                 spin_lock_irqsave(&sn2_global_ptc_lock, flags);
240         }
241         pio_phys_write_mmr(p, val);
242         if (enable_shub_wars_1_1()) {
243                 wait_piowc();
244                 spin_unlock_irqrestore(&sn2_global_ptc_lock, flags);
245         }
246
247 }
248
249 EXPORT_SYMBOL(sn_send_IPI_phys);
250
251 /**
252  * sn2_send_IPI - send an IPI to a processor
253  * @cpuid: target of the IPI
254  * @vector: command to send
255  * @delivery_mode: delivery mechanism
256  * @redirect: redirect the IPI?
257  *
258  * Sends an IPI (InterProcessor Interrupt) to the processor specified by
259  * @cpuid.  @vector specifies the command to send, while @delivery_mode can 
260  * be one of the following
261  *
262  * %IA64_IPI_DM_INT - pend an interrupt
263  * %IA64_IPI_DM_PMI - pend a PMI
264  * %IA64_IPI_DM_NMI - pend an NMI
265  * %IA64_IPI_DM_INIT - pend an INIT interrupt
266  */
267 void sn2_send_IPI(int cpuid, int vector, int delivery_mode, int redirect)
268 {
269         long physid;
270         int nasid;
271
272         physid = cpu_physical_id(cpuid);
273         nasid = cpuid_to_nasid(cpuid);
274
275         /* the following is used only when starting cpus at boot time */
276         if (unlikely(nasid == -1))
277                 ia64_sn_get_sapic_info(physid, &nasid, NULL, NULL);
278
279         sn_send_IPI_phys(nasid, physid, vector, delivery_mode);
280 }