x86: Fix UV BAU for non-consecutive nasids
[linux-flexiantxendom0-natty.git] / arch / x86 / platform / uv / tlb_uv.c
1 /*
2  *      SGI UltraViolet TLB flush routines.
3  *
4  *      (c) 2008-2010 Cliff Wickman <cpw@sgi.com>, SGI.
5  *
6  *      This code is released under the GNU General Public License version 2 or
7  *      later.
8  */
9 #include <linux/seq_file.h>
10 #include <linux/proc_fs.h>
11 #include <linux/debugfs.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14
15 #include <asm/mmu_context.h>
16 #include <asm/uv/uv.h>
17 #include <asm/uv/uv_mmrs.h>
18 #include <asm/uv/uv_hub.h>
19 #include <asm/uv/uv_bau.h>
20 #include <asm/apic.h>
21 #include <asm/idle.h>
22 #include <asm/tsc.h>
23 #include <asm/irq_vectors.h>
24 #include <asm/timer.h>
25
26 /* timeouts in nanoseconds (indexed by UVH_AGING_PRESCALE_SEL urgency7 30:28) */
27 static int timeout_base_ns[] = {
28                 20,
29                 160,
30                 1280,
31                 10240,
32                 81920,
33                 655360,
34                 5242880,
35                 167772160
36 };
37 static int timeout_us;
38 static int nobau;
39 static int baudisabled;
40 static spinlock_t disable_lock;
41 static cycles_t congested_cycles;
42
43 /* tunables: */
44 static int max_bau_concurrent = MAX_BAU_CONCURRENT;
45 static int max_bau_concurrent_constant = MAX_BAU_CONCURRENT;
46 static int plugged_delay = PLUGGED_DELAY;
47 static int plugsb4reset = PLUGSB4RESET;
48 static int timeoutsb4reset = TIMEOUTSB4RESET;
49 static int ipi_reset_limit = IPI_RESET_LIMIT;
50 static int complete_threshold = COMPLETE_THRESHOLD;
51 static int congested_response_us = CONGESTED_RESPONSE_US;
52 static int congested_reps = CONGESTED_REPS;
53 static int congested_period = CONGESTED_PERIOD;
54 static struct dentry *tunables_dir;
55 static struct dentry *tunables_file;
56
57 static int __init setup_nobau(char *arg)
58 {
59         nobau = 1;
60         return 0;
61 }
62 early_param("nobau", setup_nobau);
63
64 /* base pnode in this partition */
65 static int uv_partition_base_pnode __read_mostly;
66 /* position of pnode (which is nasid>>1): */
67 static int uv_nshift __read_mostly;
68 static unsigned long uv_mmask __read_mostly;
69
70 static DEFINE_PER_CPU(struct ptc_stats, ptcstats);
71 static DEFINE_PER_CPU(struct bau_control, bau_control);
72 static DEFINE_PER_CPU(cpumask_var_t, uv_flush_tlb_mask);
73
74 /*
75  * Determine the first node on a uvhub. 'Nodes' are used for kernel
76  * memory allocation.
77  */
78 static int __init uvhub_to_first_node(int uvhub)
79 {
80         int node, b;
81
82         for_each_online_node(node) {
83                 b = uv_node_to_blade_id(node);
84                 if (uvhub == b)
85                         return node;
86         }
87         return -1;
88 }
89
90 /*
91  * Determine the apicid of the first cpu on a uvhub.
92  */
93 static int __init uvhub_to_first_apicid(int uvhub)
94 {
95         int cpu;
96
97         for_each_present_cpu(cpu)
98                 if (uvhub == uv_cpu_to_blade_id(cpu))
99                         return per_cpu(x86_cpu_to_apicid, cpu);
100         return -1;
101 }
102
103 /*
104  * Free a software acknowledge hardware resource by clearing its Pending
105  * bit. This will return a reply to the sender.
106  * If the message has timed out, a reply has already been sent by the
107  * hardware but the resource has not been released. In that case our
108  * clear of the Timeout bit (as well) will free the resource. No reply will
109  * be sent (the hardware will only do one reply per message).
110  */
111 static inline void uv_reply_to_message(struct msg_desc *mdp,
112                                        struct bau_control *bcp)
113 {
114         unsigned long dw;
115         struct bau_payload_queue_entry *msg;
116
117         msg = mdp->msg;
118         if (!msg->canceled) {
119                 dw = (msg->sw_ack_vector << UV_SW_ACK_NPENDING) |
120                                                 msg->sw_ack_vector;
121                 uv_write_local_mmr(
122                                 UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE_ALIAS, dw);
123         }
124         msg->replied_to = 1;
125         msg->sw_ack_vector = 0;
126 }
127
128 /*
129  * Process the receipt of a RETRY message
130  */
131 static inline void uv_bau_process_retry_msg(struct msg_desc *mdp,
132                                             struct bau_control *bcp)
133 {
134         int i;
135         int cancel_count = 0;
136         int slot2;
137         unsigned long msg_res;
138         unsigned long mmr = 0;
139         struct bau_payload_queue_entry *msg;
140         struct bau_payload_queue_entry *msg2;
141         struct ptc_stats *stat;
142
143         msg = mdp->msg;
144         stat = bcp->statp;
145         stat->d_retries++;
146         /*
147          * cancel any message from msg+1 to the retry itself
148          */
149         for (msg2 = msg+1, i = 0; i < DEST_Q_SIZE; msg2++, i++) {
150                 if (msg2 > mdp->va_queue_last)
151                         msg2 = mdp->va_queue_first;
152                 if (msg2 == msg)
153                         break;
154
155                 /* same conditions for cancellation as uv_do_reset */
156                 if ((msg2->replied_to == 0) && (msg2->canceled == 0) &&
157                     (msg2->sw_ack_vector) && ((msg2->sw_ack_vector &
158                         msg->sw_ack_vector) == 0) &&
159                     (msg2->sending_cpu == msg->sending_cpu) &&
160                     (msg2->msg_type != MSG_NOOP)) {
161                         slot2 = msg2 - mdp->va_queue_first;
162                         mmr = uv_read_local_mmr
163                                 (UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE);
164                         msg_res = msg2->sw_ack_vector;
165                         /*
166                          * This is a message retry; clear the resources held
167                          * by the previous message only if they timed out.
168                          * If it has not timed out we have an unexpected
169                          * situation to report.
170                          */
171                         if (mmr & (msg_res << UV_SW_ACK_NPENDING)) {
172                                 /*
173                                  * is the resource timed out?
174                                  * make everyone ignore the cancelled message.
175                                  */
176                                 msg2->canceled = 1;
177                                 stat->d_canceled++;
178                                 cancel_count++;
179                                 uv_write_local_mmr(
180                                     UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE_ALIAS,
181                                         (msg_res << UV_SW_ACK_NPENDING) |
182                                          msg_res);
183                         }
184                 }
185         }
186         if (!cancel_count)
187                 stat->d_nocanceled++;
188 }
189
190 /*
191  * Do all the things a cpu should do for a TLB shootdown message.
192  * Other cpu's may come here at the same time for this message.
193  */
194 static void uv_bau_process_message(struct msg_desc *mdp,
195                                    struct bau_control *bcp)
196 {
197         int msg_ack_count;
198         short socket_ack_count = 0;
199         struct ptc_stats *stat;
200         struct bau_payload_queue_entry *msg;
201         struct bau_control *smaster = bcp->socket_master;
202
203         /*
204          * This must be a normal message, or retry of a normal message
205          */
206         msg = mdp->msg;
207         stat = bcp->statp;
208         if (msg->address == TLB_FLUSH_ALL) {
209                 local_flush_tlb();
210                 stat->d_alltlb++;
211         } else {
212                 __flush_tlb_one(msg->address);
213                 stat->d_onetlb++;
214         }
215         stat->d_requestee++;
216
217         /*
218          * One cpu on each uvhub has the additional job on a RETRY
219          * of releasing the resource held by the message that is
220          * being retried.  That message is identified by sending
221          * cpu number.
222          */
223         if (msg->msg_type == MSG_RETRY && bcp == bcp->uvhub_master)
224                 uv_bau_process_retry_msg(mdp, bcp);
225
226         /*
227          * This is a sw_ack message, so we have to reply to it.
228          * Count each responding cpu on the socket. This avoids
229          * pinging the count's cache line back and forth between
230          * the sockets.
231          */
232         socket_ack_count = atomic_add_short_return(1, (struct atomic_short *)
233                         &smaster->socket_acknowledge_count[mdp->msg_slot]);
234         if (socket_ack_count == bcp->cpus_in_socket) {
235                 /*
236                  * Both sockets dump their completed count total into
237                  * the message's count.
238                  */
239                 smaster->socket_acknowledge_count[mdp->msg_slot] = 0;
240                 msg_ack_count = atomic_add_short_return(socket_ack_count,
241                                 (struct atomic_short *)&msg->acknowledge_count);
242
243                 if (msg_ack_count == bcp->cpus_in_uvhub) {
244                         /*
245                          * All cpus in uvhub saw it; reply
246                          */
247                         uv_reply_to_message(mdp, bcp);
248                 }
249         }
250
251         return;
252 }
253
254 /*
255  * Determine the first cpu on a uvhub.
256  */
257 static int uvhub_to_first_cpu(int uvhub)
258 {
259         int cpu;
260         for_each_present_cpu(cpu)
261                 if (uvhub == uv_cpu_to_blade_id(cpu))
262                         return cpu;
263         return -1;
264 }
265
266 /*
267  * Last resort when we get a large number of destination timeouts is
268  * to clear resources held by a given cpu.
269  * Do this with IPI so that all messages in the BAU message queue
270  * can be identified by their nonzero sw_ack_vector field.
271  *
272  * This is entered for a single cpu on the uvhub.
273  * The sender want's this uvhub to free a specific message's
274  * sw_ack resources.
275  */
276 static void
277 uv_do_reset(void *ptr)
278 {
279         int i;
280         int slot;
281         int count = 0;
282         unsigned long mmr;
283         unsigned long msg_res;
284         struct bau_control *bcp;
285         struct reset_args *rap;
286         struct bau_payload_queue_entry *msg;
287         struct ptc_stats *stat;
288
289         bcp = &per_cpu(bau_control, smp_processor_id());
290         rap = (struct reset_args *)ptr;
291         stat = bcp->statp;
292         stat->d_resets++;
293
294         /*
295          * We're looking for the given sender, and
296          * will free its sw_ack resource.
297          * If all cpu's finally responded after the timeout, its
298          * message 'replied_to' was set.
299          */
300         for (msg = bcp->va_queue_first, i = 0; i < DEST_Q_SIZE; msg++, i++) {
301                 /* uv_do_reset: same conditions for cancellation as
302                    uv_bau_process_retry_msg() */
303                 if ((msg->replied_to == 0) &&
304                     (msg->canceled == 0) &&
305                     (msg->sending_cpu == rap->sender) &&
306                     (msg->sw_ack_vector) &&
307                     (msg->msg_type != MSG_NOOP)) {
308                         /*
309                          * make everyone else ignore this message
310                          */
311                         msg->canceled = 1;
312                         slot = msg - bcp->va_queue_first;
313                         count++;
314                         /*
315                          * only reset the resource if it is still pending
316                          */
317                         mmr = uv_read_local_mmr
318                                         (UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE);
319                         msg_res = msg->sw_ack_vector;
320                         if (mmr & msg_res) {
321                                 stat->d_rcanceled++;
322                                 uv_write_local_mmr(
323                                     UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE_ALIAS,
324                                         (msg_res << UV_SW_ACK_NPENDING) |
325                                          msg_res);
326                         }
327                 }
328         }
329         return;
330 }
331
332 /*
333  * Use IPI to get all target uvhubs to release resources held by
334  * a given sending cpu number.
335  */
336 static void uv_reset_with_ipi(struct bau_target_uvhubmask *distribution,
337                               int sender)
338 {
339         int uvhub;
340         int cpu;
341         cpumask_t mask;
342         struct reset_args reset_args;
343
344         reset_args.sender = sender;
345
346         cpus_clear(mask);
347         /* find a single cpu for each uvhub in this distribution mask */
348         for (uvhub = 0;
349                     uvhub < sizeof(struct bau_target_uvhubmask) * BITSPERBYTE;
350                     uvhub++) {
351                 if (!bau_uvhub_isset(uvhub, distribution))
352                         continue;
353                 /* find a cpu for this uvhub */
354                 cpu = uvhub_to_first_cpu(uvhub);
355                 cpu_set(cpu, mask);
356         }
357         /* IPI all cpus; Preemption is already disabled */
358         smp_call_function_many(&mask, uv_do_reset, (void *)&reset_args, 1);
359         return;
360 }
361
362 static inline unsigned long
363 cycles_2_us(unsigned long long cyc)
364 {
365         unsigned long long ns;
366         unsigned long us;
367         ns =  (cyc * per_cpu(cyc2ns, smp_processor_id()))
368                                                 >> CYC2NS_SCALE_FACTOR;
369         us = ns / 1000;
370         return us;
371 }
372
373 /*
374  * wait for all cpus on this hub to finish their sends and go quiet
375  * leaves uvhub_quiesce set so that no new broadcasts are started by
376  * bau_flush_send_and_wait()
377  */
378 static inline void
379 quiesce_local_uvhub(struct bau_control *hmaster)
380 {
381         atomic_add_short_return(1, (struct atomic_short *)
382                  &hmaster->uvhub_quiesce);
383 }
384
385 /*
386  * mark this quiet-requestor as done
387  */
388 static inline void
389 end_uvhub_quiesce(struct bau_control *hmaster)
390 {
391         atomic_add_short_return(-1, (struct atomic_short *)
392                 &hmaster->uvhub_quiesce);
393 }
394
395 /*
396  * Wait for completion of a broadcast software ack message
397  * return COMPLETE, RETRY(PLUGGED or TIMEOUT) or GIVEUP
398  */
399 static int uv_wait_completion(struct bau_desc *bau_desc,
400         unsigned long mmr_offset, int right_shift, int this_cpu,
401         struct bau_control *bcp, struct bau_control *smaster, long try)
402 {
403         unsigned long descriptor_status;
404         cycles_t ttime;
405         struct ptc_stats *stat = bcp->statp;
406         struct bau_control *hmaster;
407
408         hmaster = bcp->uvhub_master;
409
410         /* spin on the status MMR, waiting for it to go idle */
411         while ((descriptor_status = (((unsigned long)
412                 uv_read_local_mmr(mmr_offset) >>
413                         right_shift) & UV_ACT_STATUS_MASK)) !=
414                         DESC_STATUS_IDLE) {
415                 /*
416                  * Our software ack messages may be blocked because there are
417                  * no swack resources available.  As long as none of them
418                  * has timed out hardware will NACK our message and its
419                  * state will stay IDLE.
420                  */
421                 if (descriptor_status == DESC_STATUS_SOURCE_TIMEOUT) {
422                         stat->s_stimeout++;
423                         return FLUSH_GIVEUP;
424                 } else if (descriptor_status ==
425                                         DESC_STATUS_DESTINATION_TIMEOUT) {
426                         stat->s_dtimeout++;
427                         ttime = get_cycles();
428
429                         /*
430                          * Our retries may be blocked by all destination
431                          * swack resources being consumed, and a timeout
432                          * pending.  In that case hardware returns the
433                          * ERROR that looks like a destination timeout.
434                          */
435                         if (cycles_2_us(ttime - bcp->send_message) <
436                                                         timeout_us) {
437                                 bcp->conseccompletes = 0;
438                                 return FLUSH_RETRY_PLUGGED;
439                         }
440
441                         bcp->conseccompletes = 0;
442                         return FLUSH_RETRY_TIMEOUT;
443                 } else {
444                         /*
445                          * descriptor_status is still BUSY
446                          */
447                         cpu_relax();
448                 }
449         }
450         bcp->conseccompletes++;
451         return FLUSH_COMPLETE;
452 }
453
454 static inline cycles_t
455 sec_2_cycles(unsigned long sec)
456 {
457         unsigned long ns;
458         cycles_t cyc;
459
460         ns = sec * 1000000000;
461         cyc = (ns << CYC2NS_SCALE_FACTOR)/(per_cpu(cyc2ns, smp_processor_id()));
462         return cyc;
463 }
464
465 /*
466  * conditionally add 1 to *v, unless *v is >= u
467  * return 0 if we cannot add 1 to *v because it is >= u
468  * return 1 if we can add 1 to *v because it is < u
469  * the add is atomic
470  *
471  * This is close to atomic_add_unless(), but this allows the 'u' value
472  * to be lowered below the current 'v'.  atomic_add_unless can only stop
473  * on equal.
474  */
475 static inline int atomic_inc_unless_ge(spinlock_t *lock, atomic_t *v, int u)
476 {
477         spin_lock(lock);
478         if (atomic_read(v) >= u) {
479                 spin_unlock(lock);
480                 return 0;
481         }
482         atomic_inc(v);
483         spin_unlock(lock);
484         return 1;
485 }
486
487 /*
488  * Our retries are blocked by all destination swack resources being
489  * in use, and a timeout is pending. In that case hardware immediately
490  * returns the ERROR that looks like a destination timeout.
491  */
492 static void
493 destination_plugged(struct bau_desc *bau_desc, struct bau_control *bcp,
494                         struct bau_control *hmaster, struct ptc_stats *stat)
495 {
496         udelay(bcp->plugged_delay);
497         bcp->plugged_tries++;
498         if (bcp->plugged_tries >= bcp->plugsb4reset) {
499                 bcp->plugged_tries = 0;
500                 quiesce_local_uvhub(hmaster);
501                 spin_lock(&hmaster->queue_lock);
502                 uv_reset_with_ipi(&bau_desc->distribution, bcp->cpu);
503                 spin_unlock(&hmaster->queue_lock);
504                 end_uvhub_quiesce(hmaster);
505                 bcp->ipi_attempts++;
506                 stat->s_resets_plug++;
507         }
508 }
509
510 static void
511 destination_timeout(struct bau_desc *bau_desc, struct bau_control *bcp,
512                         struct bau_control *hmaster, struct ptc_stats *stat)
513 {
514         hmaster->max_bau_concurrent = 1;
515         bcp->timeout_tries++;
516         if (bcp->timeout_tries >= bcp->timeoutsb4reset) {
517                 bcp->timeout_tries = 0;
518                 quiesce_local_uvhub(hmaster);
519                 spin_lock(&hmaster->queue_lock);
520                 uv_reset_with_ipi(&bau_desc->distribution, bcp->cpu);
521                 spin_unlock(&hmaster->queue_lock);
522                 end_uvhub_quiesce(hmaster);
523                 bcp->ipi_attempts++;
524                 stat->s_resets_timeout++;
525         }
526 }
527
528 /*
529  * Completions are taking a very long time due to a congested numalink
530  * network.
531  */
532 static void
533 disable_for_congestion(struct bau_control *bcp, struct ptc_stats *stat)
534 {
535         int tcpu;
536         struct bau_control *tbcp;
537
538         /* let only one cpu do this disabling */
539         spin_lock(&disable_lock);
540         if (!baudisabled && bcp->period_requests &&
541             ((bcp->period_time / bcp->period_requests) > congested_cycles)) {
542                 /* it becomes this cpu's job to turn on the use of the
543                    BAU again */
544                 baudisabled = 1;
545                 bcp->set_bau_off = 1;
546                 bcp->set_bau_on_time = get_cycles() +
547                         sec_2_cycles(bcp->congested_period);
548                 stat->s_bau_disabled++;
549                 for_each_present_cpu(tcpu) {
550                         tbcp = &per_cpu(bau_control, tcpu);
551                                 tbcp->baudisabled = 1;
552                 }
553         }
554         spin_unlock(&disable_lock);
555 }
556
557 /**
558  * uv_flush_send_and_wait
559  *
560  * Send a broadcast and wait for it to complete.
561  *
562  * The flush_mask contains the cpus the broadcast is to be sent to including
563  * cpus that are on the local uvhub.
564  *
565  * Returns 0 if all flushing represented in the mask was done.
566  * Returns 1 if it gives up entirely and the original cpu mask is to be
567  * returned to the kernel.
568  */
569 int uv_flush_send_and_wait(struct bau_desc *bau_desc,
570                            struct cpumask *flush_mask, struct bau_control *bcp)
571 {
572         int right_shift;
573         int completion_status = 0;
574         int seq_number = 0;
575         long try = 0;
576         int cpu = bcp->uvhub_cpu;
577         int this_cpu = bcp->cpu;
578         unsigned long mmr_offset;
579         unsigned long index;
580         cycles_t time1;
581         cycles_t time2;
582         cycles_t elapsed;
583         struct ptc_stats *stat = bcp->statp;
584         struct bau_control *smaster = bcp->socket_master;
585         struct bau_control *hmaster = bcp->uvhub_master;
586
587         if (!atomic_inc_unless_ge(&hmaster->uvhub_lock,
588                         &hmaster->active_descriptor_count,
589                         hmaster->max_bau_concurrent)) {
590                 stat->s_throttles++;
591                 do {
592                         cpu_relax();
593                 } while (!atomic_inc_unless_ge(&hmaster->uvhub_lock,
594                         &hmaster->active_descriptor_count,
595                         hmaster->max_bau_concurrent));
596         }
597         while (hmaster->uvhub_quiesce)
598                 cpu_relax();
599
600         if (cpu < UV_CPUS_PER_ACT_STATUS) {
601                 mmr_offset = UVH_LB_BAU_SB_ACTIVATION_STATUS_0;
602                 right_shift = cpu * UV_ACT_STATUS_SIZE;
603         } else {
604                 mmr_offset = UVH_LB_BAU_SB_ACTIVATION_STATUS_1;
605                 right_shift =
606                     ((cpu - UV_CPUS_PER_ACT_STATUS) * UV_ACT_STATUS_SIZE);
607         }
608         time1 = get_cycles();
609         do {
610                 if (try == 0) {
611                         bau_desc->header.msg_type = MSG_REGULAR;
612                         seq_number = bcp->message_number++;
613                 } else {
614                         bau_desc->header.msg_type = MSG_RETRY;
615                         stat->s_retry_messages++;
616                 }
617                 bau_desc->header.sequence = seq_number;
618                 index = (1UL << UVH_LB_BAU_SB_ACTIVATION_CONTROL_PUSH_SHFT) |
619                         bcp->uvhub_cpu;
620                 bcp->send_message = get_cycles();
621                 uv_write_local_mmr(UVH_LB_BAU_SB_ACTIVATION_CONTROL, index);
622                 try++;
623                 completion_status = uv_wait_completion(bau_desc, mmr_offset,
624                         right_shift, this_cpu, bcp, smaster, try);
625
626                 if (completion_status == FLUSH_RETRY_PLUGGED) {
627                         destination_plugged(bau_desc, bcp, hmaster, stat);
628                 } else if (completion_status == FLUSH_RETRY_TIMEOUT) {
629                         destination_timeout(bau_desc, bcp, hmaster, stat);
630                 }
631                 if (bcp->ipi_attempts >= bcp->ipi_reset_limit) {
632                         bcp->ipi_attempts = 0;
633                         completion_status = FLUSH_GIVEUP;
634                         break;
635                 }
636                 cpu_relax();
637         } while ((completion_status == FLUSH_RETRY_PLUGGED) ||
638                  (completion_status == FLUSH_RETRY_TIMEOUT));
639         time2 = get_cycles();
640         bcp->plugged_tries = 0;
641         bcp->timeout_tries = 0;
642         if ((completion_status == FLUSH_COMPLETE) &&
643             (bcp->conseccompletes > bcp->complete_threshold) &&
644             (hmaster->max_bau_concurrent <
645                                         hmaster->max_bau_concurrent_constant))
646                         hmaster->max_bau_concurrent++;
647         while (hmaster->uvhub_quiesce)
648                 cpu_relax();
649         atomic_dec(&hmaster->active_descriptor_count);
650         if (time2 > time1) {
651                 elapsed = time2 - time1;
652                 stat->s_time += elapsed;
653                 if ((completion_status == FLUSH_COMPLETE) && (try == 1)) {
654                         bcp->period_requests++;
655                         bcp->period_time += elapsed;
656                         if ((elapsed > congested_cycles) &&
657                             (bcp->period_requests > bcp->congested_reps)) {
658                                 disable_for_congestion(bcp, stat);
659                         }
660                 }
661         } else
662                 stat->s_requestor--;
663         if (completion_status == FLUSH_COMPLETE && try > 1)
664                 stat->s_retriesok++;
665         else if (completion_status == FLUSH_GIVEUP) {
666                 stat->s_giveup++;
667                 return 1;
668         }
669         return 0;
670 }
671
672 /**
673  * uv_flush_tlb_others - globally purge translation cache of a virtual
674  * address or all TLB's
675  * @cpumask: mask of all cpu's in which the address is to be removed
676  * @mm: mm_struct containing virtual address range
677  * @va: virtual address to be removed (or TLB_FLUSH_ALL for all TLB's on cpu)
678  * @cpu: the current cpu
679  *
680  * This is the entry point for initiating any UV global TLB shootdown.
681  *
682  * Purges the translation caches of all specified processors of the given
683  * virtual address, or purges all TLB's on specified processors.
684  *
685  * The caller has derived the cpumask from the mm_struct.  This function
686  * is called only if there are bits set in the mask. (e.g. flush_tlb_page())
687  *
688  * The cpumask is converted into a uvhubmask of the uvhubs containing
689  * those cpus.
690  *
691  * Note that this function should be called with preemption disabled.
692  *
693  * Returns NULL if all remote flushing was done.
694  * Returns pointer to cpumask if some remote flushing remains to be
695  * done.  The returned pointer is valid till preemption is re-enabled.
696  */
697 const struct cpumask *uv_flush_tlb_others(const struct cpumask *cpumask,
698                                           struct mm_struct *mm,
699                                           unsigned long va, unsigned int cpu)
700 {
701         int locals = 0;
702         int remotes = 0;
703         int hubs = 0;
704         int tcpu;
705         int tpnode;
706         struct bau_desc *bau_desc;
707         struct cpumask *flush_mask;
708         struct ptc_stats *stat;
709         struct bau_control *bcp;
710         struct bau_control *tbcp;
711         struct hub_and_pnode *hpp;
712
713         /* kernel was booted 'nobau' */
714         if (nobau)
715                 return cpumask;
716
717         bcp = &per_cpu(bau_control, cpu);
718         stat = bcp->statp;
719
720         /* bau was disabled due to slow response */
721         if (bcp->baudisabled) {
722                 /* the cpu that disabled it must re-enable it */
723                 if (bcp->set_bau_off) {
724                         if (get_cycles() >= bcp->set_bau_on_time) {
725                                 stat->s_bau_reenabled++;
726                                 baudisabled = 0;
727                                 for_each_present_cpu(tcpu) {
728                                         tbcp = &per_cpu(bau_control, tcpu);
729                                         tbcp->baudisabled = 0;
730                                         tbcp->period_requests = 0;
731                                         tbcp->period_time = 0;
732                                 }
733                         }
734                 }
735                 return cpumask;
736         }
737
738         /*
739          * Each sending cpu has a per-cpu mask which it fills from the caller's
740          * cpu mask.  All cpus are converted to uvhubs and copied to the
741          * activation descriptor.
742          */
743         flush_mask = (struct cpumask *)per_cpu(uv_flush_tlb_mask, cpu);
744         /* don't actually do a shootdown of the local cpu */
745         cpumask_andnot(flush_mask, cpumask, cpumask_of(cpu));
746         if (cpu_isset(cpu, *cpumask))
747                 stat->s_ntargself++;
748
749         bau_desc = bcp->descriptor_base;
750         bau_desc += UV_ITEMS_PER_DESCRIPTOR * bcp->uvhub_cpu;
751         bau_uvhubs_clear(&bau_desc->distribution, UV_DISTRIBUTION_SIZE);
752
753         for_each_cpu(tcpu, flush_mask) {
754                 /*
755                  * The distribution vector is a bit map of pnodes, relative
756                  * to the partition base pnode (and the partition base nasid
757                  * in the header).
758                  * Translate cpu to pnode and hub using an array stored
759                  * in local memory.
760                  */
761                 hpp = &bcp->socket_master->target_hub_and_pnode[tcpu];
762                 tpnode = hpp->pnode - bcp->partition_base_pnode;
763                 bau_uvhub_set(tpnode, &bau_desc->distribution);
764                 if (hpp->uvhub == bcp->uvhub)
765                         locals++;
766                 else
767                         remotes++;
768         }
769         if ((locals + remotes) == 0)
770                 return NULL;
771         stat->s_requestor++;
772         stat->s_ntargcpu += remotes + locals;
773         stat->s_ntargremotes += remotes;
774         stat->s_ntarglocals += locals;
775         remotes = bau_uvhub_weight(&bau_desc->distribution);
776
777         /* uvhub statistics */
778         hubs = bau_uvhub_weight(&bau_desc->distribution);
779         if (locals) {
780                 stat->s_ntarglocaluvhub++;
781                 stat->s_ntargremoteuvhub += (hubs - 1);
782         } else
783                 stat->s_ntargremoteuvhub += hubs;
784         stat->s_ntarguvhub += hubs;
785         if (hubs >= 16)
786                 stat->s_ntarguvhub16++;
787         else if (hubs >= 8)
788                 stat->s_ntarguvhub8++;
789         else if (hubs >= 4)
790                 stat->s_ntarguvhub4++;
791         else if (hubs >= 2)
792                 stat->s_ntarguvhub2++;
793         else
794                 stat->s_ntarguvhub1++;
795
796         bau_desc->payload.address = va;
797         bau_desc->payload.sending_cpu = cpu;
798
799         /*
800          * uv_flush_send_and_wait returns 0 if all cpu's were messaged,
801          * or 1 if it gave up and the original cpumask should be returned.
802          */
803         if (!uv_flush_send_and_wait(bau_desc, flush_mask, bcp))
804                 return NULL;
805         else
806                 return cpumask;
807 }
808
809 /*
810  * The BAU message interrupt comes here. (registered by set_intr_gate)
811  * See entry_64.S
812  *
813  * We received a broadcast assist message.
814  *
815  * Interrupts are disabled; this interrupt could represent
816  * the receipt of several messages.
817  *
818  * All cores/threads on this hub get this interrupt.
819  * The last one to see it does the software ack.
820  * (the resource will not be freed until noninterruptable cpus see this
821  *  interrupt; hardware may timeout the s/w ack and reply ERROR)
822  */
823 void uv_bau_message_interrupt(struct pt_regs *regs)
824 {
825         int count = 0;
826         cycles_t time_start;
827         struct bau_payload_queue_entry *msg;
828         struct bau_control *bcp;
829         struct ptc_stats *stat;
830         struct msg_desc msgdesc;
831
832         time_start = get_cycles();
833         bcp = &per_cpu(bau_control, smp_processor_id());
834         stat = bcp->statp;
835         msgdesc.va_queue_first = bcp->va_queue_first;
836         msgdesc.va_queue_last = bcp->va_queue_last;
837         msg = bcp->bau_msg_head;
838         while (msg->sw_ack_vector) {
839                 count++;
840                 msgdesc.msg_slot = msg - msgdesc.va_queue_first;
841                 msgdesc.sw_ack_slot = ffs(msg->sw_ack_vector) - 1;
842                 msgdesc.msg = msg;
843                 uv_bau_process_message(&msgdesc, bcp);
844                 msg++;
845                 if (msg > msgdesc.va_queue_last)
846                         msg = msgdesc.va_queue_first;
847                 bcp->bau_msg_head = msg;
848         }
849         stat->d_time += (get_cycles() - time_start);
850         if (!count)
851                 stat->d_nomsg++;
852         else if (count > 1)
853                 stat->d_multmsg++;
854         ack_APIC_irq();
855 }
856
857 /*
858  * uv_enable_timeouts
859  *
860  * Each target uvhub (i.e. a uvhub that has no cpu's) needs to have
861  * shootdown message timeouts enabled.  The timeout does not cause
862  * an interrupt, but causes an error message to be returned to
863  * the sender.
864  */
865 static void __init uv_enable_timeouts(void)
866 {
867         int uvhub;
868         int nuvhubs;
869         int pnode;
870         unsigned long mmr_image;
871
872         nuvhubs = uv_num_possible_blades();
873
874         for (uvhub = 0; uvhub < nuvhubs; uvhub++) {
875                 if (!uv_blade_nr_possible_cpus(uvhub))
876                         continue;
877
878                 pnode = uv_blade_to_pnode(uvhub);
879                 mmr_image =
880                     uv_read_global_mmr64(pnode, UVH_LB_BAU_MISC_CONTROL);
881                 /*
882                  * Set the timeout period and then lock it in, in three
883                  * steps; captures and locks in the period.
884                  *
885                  * To program the period, the SOFT_ACK_MODE must be off.
886                  */
887                 mmr_image &= ~((unsigned long)1 <<
888                     UVH_LB_BAU_MISC_CONTROL_ENABLE_INTD_SOFT_ACK_MODE_SHFT);
889                 uv_write_global_mmr64
890                     (pnode, UVH_LB_BAU_MISC_CONTROL, mmr_image);
891                 /*
892                  * Set the 4-bit period.
893                  */
894                 mmr_image &= ~((unsigned long)0xf <<
895                      UVH_LB_BAU_MISC_CONTROL_INTD_SOFT_ACK_TIMEOUT_PERIOD_SHFT);
896                 mmr_image |= (UV_INTD_SOFT_ACK_TIMEOUT_PERIOD <<
897                      UVH_LB_BAU_MISC_CONTROL_INTD_SOFT_ACK_TIMEOUT_PERIOD_SHFT);
898                 uv_write_global_mmr64
899                     (pnode, UVH_LB_BAU_MISC_CONTROL, mmr_image);
900                 /*
901                  * Subsequent reversals of the timebase bit (3) cause an
902                  * immediate timeout of one or all INTD resources as
903                  * indicated in bits 2:0 (7 causes all of them to timeout).
904                  */
905                 mmr_image |= ((unsigned long)1 <<
906                     UVH_LB_BAU_MISC_CONTROL_ENABLE_INTD_SOFT_ACK_MODE_SHFT);
907                 uv_write_global_mmr64
908                     (pnode, UVH_LB_BAU_MISC_CONTROL, mmr_image);
909         }
910 }
911
912 static void *uv_ptc_seq_start(struct seq_file *file, loff_t *offset)
913 {
914         if (*offset < num_possible_cpus())
915                 return offset;
916         return NULL;
917 }
918
919 static void *uv_ptc_seq_next(struct seq_file *file, void *data, loff_t *offset)
920 {
921         (*offset)++;
922         if (*offset < num_possible_cpus())
923                 return offset;
924         return NULL;
925 }
926
927 static void uv_ptc_seq_stop(struct seq_file *file, void *data)
928 {
929 }
930
931 static inline unsigned long long
932 microsec_2_cycles(unsigned long microsec)
933 {
934         unsigned long ns;
935         unsigned long long cyc;
936
937         ns = microsec * 1000;
938         cyc = (ns << CYC2NS_SCALE_FACTOR)/(per_cpu(cyc2ns, smp_processor_id()));
939         return cyc;
940 }
941
942 /*
943  * Display the statistics thru /proc.
944  * 'data' points to the cpu number
945  */
946 static int uv_ptc_seq_show(struct seq_file *file, void *data)
947 {
948         struct ptc_stats *stat;
949         int cpu;
950
951         cpu = *(loff_t *)data;
952
953         if (!cpu) {
954                 seq_printf(file,
955                         "# cpu sent stime self locals remotes ncpus localhub ");
956                 seq_printf(file,
957                         "remotehub numuvhubs numuvhubs16 numuvhubs8 ");
958                 seq_printf(file,
959                         "numuvhubs4 numuvhubs2 numuvhubs1 dto ");
960                 seq_printf(file,
961                         "retries rok resetp resett giveup sto bz throt ");
962                 seq_printf(file,
963                         "sw_ack recv rtime all ");
964                 seq_printf(file,
965                         "one mult none retry canc nocan reset rcan ");
966                 seq_printf(file,
967                         "disable enable\n");
968         }
969         if (cpu < num_possible_cpus() && cpu_online(cpu)) {
970                 stat = &per_cpu(ptcstats, cpu);
971                 /* source side statistics */
972                 seq_printf(file,
973                         "cpu %d %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld ",
974                            cpu, stat->s_requestor, cycles_2_us(stat->s_time),
975                            stat->s_ntargself, stat->s_ntarglocals,
976                            stat->s_ntargremotes, stat->s_ntargcpu,
977                            stat->s_ntarglocaluvhub, stat->s_ntargremoteuvhub,
978                            stat->s_ntarguvhub, stat->s_ntarguvhub16);
979                 seq_printf(file, "%ld %ld %ld %ld %ld ",
980                            stat->s_ntarguvhub8, stat->s_ntarguvhub4,
981                            stat->s_ntarguvhub2, stat->s_ntarguvhub1,
982                            stat->s_dtimeout);
983                 seq_printf(file, "%ld %ld %ld %ld %ld %ld %ld %ld ",
984                            stat->s_retry_messages, stat->s_retriesok,
985                            stat->s_resets_plug, stat->s_resets_timeout,
986                            stat->s_giveup, stat->s_stimeout,
987                            stat->s_busy, stat->s_throttles);
988
989                 /* destination side statistics */
990                 seq_printf(file,
991                            "%lx %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld ",
992                            uv_read_global_mmr64(uv_cpu_to_pnode(cpu),
993                                         UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE),
994                            stat->d_requestee, cycles_2_us(stat->d_time),
995                            stat->d_alltlb, stat->d_onetlb, stat->d_multmsg,
996                            stat->d_nomsg, stat->d_retries, stat->d_canceled,
997                            stat->d_nocanceled, stat->d_resets,
998                            stat->d_rcanceled);
999                 seq_printf(file, "%ld %ld\n",
1000                         stat->s_bau_disabled, stat->s_bau_reenabled);
1001         }
1002
1003         return 0;
1004 }
1005
1006 /*
1007  * Display the tunables thru debugfs
1008  */
1009 static ssize_t tunables_read(struct file *file, char __user *userbuf,
1010                                                 size_t count, loff_t *ppos)
1011 {
1012         char *buf;
1013         int ret;
1014
1015         buf = kasprintf(GFP_KERNEL, "%s %s %s\n%d %d %d %d %d %d %d %d %d\n",
1016                 "max_bau_concurrent plugged_delay plugsb4reset",
1017                 "timeoutsb4reset ipi_reset_limit complete_threshold",
1018                 "congested_response_us congested_reps congested_period",
1019                 max_bau_concurrent, plugged_delay, plugsb4reset,
1020                 timeoutsb4reset, ipi_reset_limit, complete_threshold,
1021                 congested_response_us, congested_reps, congested_period);
1022
1023         if (!buf)
1024                 return -ENOMEM;
1025
1026         ret = simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
1027         kfree(buf);
1028         return ret;
1029 }
1030
1031 /*
1032  * -1: resetf the statistics
1033  *  0: display meaning of the statistics
1034  */
1035 static ssize_t uv_ptc_proc_write(struct file *file, const char __user *user,
1036                                  size_t count, loff_t *data)
1037 {
1038         int cpu;
1039         long input_arg;
1040         char optstr[64];
1041         struct ptc_stats *stat;
1042
1043         if (count == 0 || count > sizeof(optstr))
1044                 return -EINVAL;
1045         if (copy_from_user(optstr, user, count))
1046                 return -EFAULT;
1047         optstr[count - 1] = '\0';
1048         if (strict_strtol(optstr, 10, &input_arg) < 0) {
1049                 printk(KERN_DEBUG "%s is invalid\n", optstr);
1050                 return -EINVAL;
1051         }
1052
1053         if (input_arg == 0) {
1054                 printk(KERN_DEBUG "# cpu:      cpu number\n");
1055                 printk(KERN_DEBUG "Sender statistics:\n");
1056                 printk(KERN_DEBUG
1057                 "sent:     number of shootdown messages sent\n");
1058                 printk(KERN_DEBUG
1059                 "stime:    time spent sending messages\n");
1060                 printk(KERN_DEBUG
1061                 "numuvhubs: number of hubs targeted with shootdown\n");
1062                 printk(KERN_DEBUG
1063                 "numuvhubs16: number times 16 or more hubs targeted\n");
1064                 printk(KERN_DEBUG
1065                 "numuvhubs8: number times 8 or more hubs targeted\n");
1066                 printk(KERN_DEBUG
1067                 "numuvhubs4: number times 4 or more hubs targeted\n");
1068                 printk(KERN_DEBUG
1069                 "numuvhubs2: number times 2 or more hubs targeted\n");
1070                 printk(KERN_DEBUG
1071                 "numuvhubs1: number times 1 hub targeted\n");
1072                 printk(KERN_DEBUG
1073                 "numcpus:  number of cpus targeted with shootdown\n");
1074                 printk(KERN_DEBUG
1075                 "dto:      number of destination timeouts\n");
1076                 printk(KERN_DEBUG
1077                 "retries:  destination timeout retries sent\n");
1078                 printk(KERN_DEBUG
1079                 "rok:   :  destination timeouts successfully retried\n");
1080                 printk(KERN_DEBUG
1081                 "resetp:   ipi-style resource resets for plugs\n");
1082                 printk(KERN_DEBUG
1083                 "resett:   ipi-style resource resets for timeouts\n");
1084                 printk(KERN_DEBUG
1085                 "giveup:   fall-backs to ipi-style shootdowns\n");
1086                 printk(KERN_DEBUG
1087                 "sto:      number of source timeouts\n");
1088                 printk(KERN_DEBUG
1089                 "bz:       number of stay-busy's\n");
1090                 printk(KERN_DEBUG
1091                 "throt:    number times spun in throttle\n");
1092                 printk(KERN_DEBUG "Destination side statistics:\n");
1093                 printk(KERN_DEBUG
1094                 "sw_ack:   image of UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE\n");
1095                 printk(KERN_DEBUG
1096                 "recv:     shootdown messages received\n");
1097                 printk(KERN_DEBUG
1098                 "rtime:    time spent processing messages\n");
1099                 printk(KERN_DEBUG
1100                 "all:      shootdown all-tlb messages\n");
1101                 printk(KERN_DEBUG
1102                 "one:      shootdown one-tlb messages\n");
1103                 printk(KERN_DEBUG
1104                 "mult:     interrupts that found multiple messages\n");
1105                 printk(KERN_DEBUG
1106                 "none:     interrupts that found no messages\n");
1107                 printk(KERN_DEBUG
1108                 "retry:    number of retry messages processed\n");
1109                 printk(KERN_DEBUG
1110                 "canc:     number messages canceled by retries\n");
1111                 printk(KERN_DEBUG
1112                 "nocan:    number retries that found nothing to cancel\n");
1113                 printk(KERN_DEBUG
1114                 "reset:    number of ipi-style reset requests processed\n");
1115                 printk(KERN_DEBUG
1116                 "rcan:     number messages canceled by reset requests\n");
1117                 printk(KERN_DEBUG
1118                 "disable:  number times use of the BAU was disabled\n");
1119                 printk(KERN_DEBUG
1120                 "enable:   number times use of the BAU was re-enabled\n");
1121         } else if (input_arg == -1) {
1122                 for_each_present_cpu(cpu) {
1123                         stat = &per_cpu(ptcstats, cpu);
1124                         memset(stat, 0, sizeof(struct ptc_stats));
1125                 }
1126         }
1127
1128         return count;
1129 }
1130
1131 static int local_atoi(const char *name)
1132 {
1133         int val = 0;
1134
1135         for (;; name++) {
1136                 switch (*name) {
1137                 case '0' ... '9':
1138                         val = 10*val+(*name-'0');
1139                         break;
1140                 default:
1141                         return val;
1142                 }
1143         }
1144 }
1145
1146 /*
1147  * set the tunables
1148  * 0 values reset them to defaults
1149  */
1150 static ssize_t tunables_write(struct file *file, const char __user *user,
1151                                  size_t count, loff_t *data)
1152 {
1153         int cpu;
1154         int cnt = 0;
1155         int val;
1156         char *p;
1157         char *q;
1158         char instr[64];
1159         struct bau_control *bcp;
1160
1161         if (count == 0 || count > sizeof(instr)-1)
1162                 return -EINVAL;
1163         if (copy_from_user(instr, user, count))
1164                 return -EFAULT;
1165
1166         instr[count] = '\0';
1167         /* count the fields */
1168         p = instr + strspn(instr, WHITESPACE);
1169         q = p;
1170         for (; *p; p = q + strspn(q, WHITESPACE)) {
1171                 q = p + strcspn(p, WHITESPACE);
1172                 cnt++;
1173                 if (q == p)
1174                         break;
1175         }
1176         if (cnt != 9) {
1177                 printk(KERN_INFO "bau tunable error: should be 9 numbers\n");
1178                 return -EINVAL;
1179         }
1180
1181         p = instr + strspn(instr, WHITESPACE);
1182         q = p;
1183         for (cnt = 0; *p; p = q + strspn(q, WHITESPACE), cnt++) {
1184                 q = p + strcspn(p, WHITESPACE);
1185                 val = local_atoi(p);
1186                 switch (cnt) {
1187                 case 0:
1188                         if (val == 0) {
1189                                 max_bau_concurrent = MAX_BAU_CONCURRENT;
1190                                 max_bau_concurrent_constant =
1191                                                         MAX_BAU_CONCURRENT;
1192                                 continue;
1193                         }
1194                         bcp = &per_cpu(bau_control, smp_processor_id());
1195                         if (val < 1 || val > bcp->cpus_in_uvhub) {
1196                                 printk(KERN_DEBUG
1197                                 "Error: BAU max concurrent %d is invalid\n",
1198                                 val);
1199                                 return -EINVAL;
1200                         }
1201                         max_bau_concurrent = val;
1202                         max_bau_concurrent_constant = val;
1203                         continue;
1204                 case 1:
1205                         if (val == 0)
1206                                 plugged_delay = PLUGGED_DELAY;
1207                         else
1208                                 plugged_delay = val;
1209                         continue;
1210                 case 2:
1211                         if (val == 0)
1212                                 plugsb4reset = PLUGSB4RESET;
1213                         else
1214                                 plugsb4reset = val;
1215                         continue;
1216                 case 3:
1217                         if (val == 0)
1218                                 timeoutsb4reset = TIMEOUTSB4RESET;
1219                         else
1220                                 timeoutsb4reset = val;
1221                         continue;
1222                 case 4:
1223                         if (val == 0)
1224                                 ipi_reset_limit = IPI_RESET_LIMIT;
1225                         else
1226                                 ipi_reset_limit = val;
1227                         continue;
1228                 case 5:
1229                         if (val == 0)
1230                                 complete_threshold = COMPLETE_THRESHOLD;
1231                         else
1232                                 complete_threshold = val;
1233                         continue;
1234                 case 6:
1235                         if (val == 0)
1236                                 congested_response_us = CONGESTED_RESPONSE_US;
1237                         else
1238                                 congested_response_us = val;
1239                         continue;
1240                 case 7:
1241                         if (val == 0)
1242                                 congested_reps = CONGESTED_REPS;
1243                         else
1244                                 congested_reps = val;
1245                         continue;
1246                 case 8:
1247                         if (val == 0)
1248                                 congested_period = CONGESTED_PERIOD;
1249                         else
1250                                 congested_period = val;
1251                         continue;
1252                 }
1253                 if (q == p)
1254                         break;
1255         }
1256         for_each_present_cpu(cpu) {
1257                 bcp = &per_cpu(bau_control, cpu);
1258                 bcp->max_bau_concurrent = max_bau_concurrent;
1259                 bcp->max_bau_concurrent_constant = max_bau_concurrent;
1260                 bcp->plugged_delay = plugged_delay;
1261                 bcp->plugsb4reset = plugsb4reset;
1262                 bcp->timeoutsb4reset = timeoutsb4reset;
1263                 bcp->ipi_reset_limit = ipi_reset_limit;
1264                 bcp->complete_threshold = complete_threshold;
1265                 bcp->congested_response_us = congested_response_us;
1266                 bcp->congested_reps = congested_reps;
1267                 bcp->congested_period = congested_period;
1268         }
1269         return count;
1270 }
1271
1272 static const struct seq_operations uv_ptc_seq_ops = {
1273         .start          = uv_ptc_seq_start,
1274         .next           = uv_ptc_seq_next,
1275         .stop           = uv_ptc_seq_stop,
1276         .show           = uv_ptc_seq_show
1277 };
1278
1279 static int uv_ptc_proc_open(struct inode *inode, struct file *file)
1280 {
1281         return seq_open(file, &uv_ptc_seq_ops);
1282 }
1283
1284 static int tunables_open(struct inode *inode, struct file *file)
1285 {
1286         return 0;
1287 }
1288
1289 static const struct file_operations proc_uv_ptc_operations = {
1290         .open           = uv_ptc_proc_open,
1291         .read           = seq_read,
1292         .write          = uv_ptc_proc_write,
1293         .llseek         = seq_lseek,
1294         .release        = seq_release,
1295 };
1296
1297 static const struct file_operations tunables_fops = {
1298         .open           = tunables_open,
1299         .read           = tunables_read,
1300         .write          = tunables_write,
1301         .llseek         = default_llseek,
1302 };
1303
1304 static int __init uv_ptc_init(void)
1305 {
1306         struct proc_dir_entry *proc_uv_ptc;
1307
1308         if (!is_uv_system())
1309                 return 0;
1310
1311         proc_uv_ptc = proc_create(UV_PTC_BASENAME, 0444, NULL,
1312                                   &proc_uv_ptc_operations);
1313         if (!proc_uv_ptc) {
1314                 printk(KERN_ERR "unable to create %s proc entry\n",
1315                        UV_PTC_BASENAME);
1316                 return -EINVAL;
1317         }
1318
1319         tunables_dir = debugfs_create_dir(UV_BAU_TUNABLES_DIR, NULL);
1320         if (!tunables_dir) {
1321                 printk(KERN_ERR "unable to create debugfs directory %s\n",
1322                        UV_BAU_TUNABLES_DIR);
1323                 return -EINVAL;
1324         }
1325         tunables_file = debugfs_create_file(UV_BAU_TUNABLES_FILE, 0600,
1326                         tunables_dir, NULL, &tunables_fops);
1327         if (!tunables_file) {
1328                 printk(KERN_ERR "unable to create debugfs file %s\n",
1329                        UV_BAU_TUNABLES_FILE);
1330                 return -EINVAL;
1331         }
1332         return 0;
1333 }
1334
1335 /*
1336  * Initialize the sending side's sending buffers.
1337  */
1338 static void
1339 uv_activation_descriptor_init(int node, int pnode, int base_pnode)
1340 {
1341         int i;
1342         int cpu;
1343         unsigned long pa;
1344         unsigned long m;
1345         unsigned long n;
1346         struct bau_desc *bau_desc;
1347         struct bau_desc *bd2;
1348         struct bau_control *bcp;
1349
1350         /*
1351          * each bau_desc is 64 bytes; there are 8 (UV_ITEMS_PER_DESCRIPTOR)
1352          * per cpu; and one per cpu on the uvhub (UV_ADP_SIZE)
1353          */
1354         bau_desc = kmalloc_node(sizeof(struct bau_desc) * UV_ADP_SIZE
1355                                 * UV_ITEMS_PER_DESCRIPTOR, GFP_KERNEL, node);
1356         BUG_ON(!bau_desc);
1357
1358         pa = uv_gpa(bau_desc); /* need the real nasid*/
1359         n = pa >> uv_nshift;
1360         m = pa & uv_mmask;
1361
1362         /* the 14-bit pnode */
1363         uv_write_global_mmr64(pnode, UVH_LB_BAU_SB_DESCRIPTOR_BASE,
1364                               (n << UV_DESC_BASE_PNODE_SHIFT | m));
1365         /*
1366          * Initializing all 8 (UV_ITEMS_PER_DESCRIPTOR) descriptors for each
1367          * cpu even though we only use the first one; one descriptor can
1368          * describe a broadcast to 256 uv hubs.
1369          */
1370         for (i = 0, bd2 = bau_desc; i < (UV_ADP_SIZE*UV_ITEMS_PER_DESCRIPTOR);
1371                 i++, bd2++) {
1372                 memset(bd2, 0, sizeof(struct bau_desc));
1373                 bd2->header.sw_ack_flag = 1;
1374                 /*
1375                  * The base_dest_nasid set in the message header is the nasid
1376                  * of the first uvhub in the partition. The bit map will
1377                  * indicate destination pnode numbers relative to that base.
1378                  * They may not be consecutive if nasid striding is being used.
1379                  */
1380                 bd2->header.base_dest_nasid = UV_PNODE_TO_NASID(base_pnode);
1381                 bd2->header.dest_subnodeid = UV_LB_SUBNODEID;
1382                 bd2->header.command = UV_NET_ENDPOINT_INTD;
1383                 bd2->header.int_both = 1;
1384                 /*
1385                  * all others need to be set to zero:
1386                  *   fairness chaining multilevel count replied_to
1387                  */
1388         }
1389         for_each_present_cpu(cpu) {
1390                 if (pnode != uv_blade_to_pnode(uv_cpu_to_blade_id(cpu)))
1391                         continue;
1392                 bcp = &per_cpu(bau_control, cpu);
1393                 bcp->descriptor_base = bau_desc;
1394         }
1395 }
1396
1397 /*
1398  * initialize the destination side's receiving buffers
1399  * entered for each uvhub in the partition
1400  * - node is first node (kernel memory notion) on the uvhub
1401  * - pnode is the uvhub's physical identifier
1402  */
1403 static void
1404 uv_payload_queue_init(int node, int pnode)
1405 {
1406         int pn;
1407         int cpu;
1408         char *cp;
1409         unsigned long pa;
1410         struct bau_payload_queue_entry *pqp;
1411         struct bau_payload_queue_entry *pqp_malloc;
1412         struct bau_control *bcp;
1413
1414         pqp = kmalloc_node((DEST_Q_SIZE + 1)
1415                            * sizeof(struct bau_payload_queue_entry),
1416                            GFP_KERNEL, node);
1417         BUG_ON(!pqp);
1418         pqp_malloc = pqp;
1419
1420         cp = (char *)pqp + 31;
1421         pqp = (struct bau_payload_queue_entry *)(((unsigned long)cp >> 5) << 5);
1422
1423         for_each_present_cpu(cpu) {
1424                 if (pnode != uv_cpu_to_pnode(cpu))
1425                         continue;
1426                 /* for every cpu on this pnode: */
1427                 bcp = &per_cpu(bau_control, cpu);
1428                 bcp->va_queue_first = pqp;
1429                 bcp->bau_msg_head = pqp;
1430                 bcp->va_queue_last = pqp + (DEST_Q_SIZE - 1);
1431         }
1432         /*
1433          * need the pnode of where the memory was really allocated
1434          */
1435         pa = uv_gpa(pqp);
1436         pn = pa >> uv_nshift;
1437         uv_write_global_mmr64(pnode,
1438                               UVH_LB_BAU_INTD_PAYLOAD_QUEUE_FIRST,
1439                               ((unsigned long)pn << UV_PAYLOADQ_PNODE_SHIFT) |
1440                               uv_physnodeaddr(pqp));
1441         uv_write_global_mmr64(pnode, UVH_LB_BAU_INTD_PAYLOAD_QUEUE_TAIL,
1442                               uv_physnodeaddr(pqp));
1443         uv_write_global_mmr64(pnode, UVH_LB_BAU_INTD_PAYLOAD_QUEUE_LAST,
1444                               (unsigned long)
1445                               uv_physnodeaddr(pqp + (DEST_Q_SIZE - 1)));
1446         /* in effect, all msg_type's are set to MSG_NOOP */
1447         memset(pqp, 0, sizeof(struct bau_payload_queue_entry) * DEST_Q_SIZE);
1448 }
1449
1450 /*
1451  * Initialization of each UV hub's structures
1452  */
1453 static void __init uv_init_uvhub(int uvhub, int vector, int base_pnode)
1454 {
1455         int node;
1456         int pnode;
1457         unsigned long apicid;
1458
1459         node = uvhub_to_first_node(uvhub);
1460         pnode = uv_blade_to_pnode(uvhub);
1461         uv_activation_descriptor_init(node, pnode, base_pnode);
1462         uv_payload_queue_init(node, pnode);
1463         /*
1464          * The below initialization can't be in firmware because the
1465          * messaging IRQ will be determined by the OS.
1466          */
1467         apicid = uvhub_to_first_apicid(uvhub) | uv_apicid_hibits;
1468         uv_write_global_mmr64(pnode, UVH_BAU_DATA_CONFIG,
1469                                       ((apicid << 32) | vector));
1470 }
1471
1472 /*
1473  * We will set BAU_MISC_CONTROL with a timeout period.
1474  * But the BIOS has set UVH_AGING_PRESCALE_SEL and UVH_TRANSACTION_TIMEOUT.
1475  * So the destination timeout period has be be calculated from them.
1476  */
1477 static int
1478 calculate_destination_timeout(void)
1479 {
1480         unsigned long mmr_image;
1481         int mult1;
1482         int mult2;
1483         int index;
1484         int base;
1485         int ret;
1486         unsigned long ts_ns;
1487
1488         mult1 = UV_INTD_SOFT_ACK_TIMEOUT_PERIOD & BAU_MISC_CONTROL_MULT_MASK;
1489         mmr_image = uv_read_local_mmr(UVH_AGING_PRESCALE_SEL);
1490         index = (mmr_image >> BAU_URGENCY_7_SHIFT) & BAU_URGENCY_7_MASK;
1491         mmr_image = uv_read_local_mmr(UVH_TRANSACTION_TIMEOUT);
1492         mult2 = (mmr_image >> BAU_TRANS_SHIFT) & BAU_TRANS_MASK;
1493         base = timeout_base_ns[index];
1494         ts_ns = base * mult1 * mult2;
1495         ret = ts_ns / 1000;
1496         return ret;
1497 }
1498
1499 /*
1500  * initialize the bau_control structure for each cpu
1501  */
1502 static int __init uv_init_per_cpu(int nuvhubs, int base_part_pnode)
1503 {
1504         int i;
1505         int cpu;
1506         int tcpu;
1507         int pnode;
1508         int uvhub;
1509         int have_hmaster;
1510         short socket = 0;
1511         unsigned short socket_mask;
1512         unsigned char *uvhub_mask;
1513         struct bau_control *bcp;
1514         struct uvhub_desc *bdp;
1515         struct socket_desc *sdp;
1516         struct bau_control *hmaster = NULL;
1517         struct bau_control *smaster = NULL;
1518         struct socket_desc {
1519                 short num_cpus;
1520                 short cpu_number[MAX_CPUS_PER_SOCKET];
1521         };
1522         struct uvhub_desc {
1523                 unsigned short socket_mask;
1524                 short num_cpus;
1525                 short uvhub;
1526                 short pnode;
1527                 struct socket_desc socket[2];
1528         };
1529         struct uvhub_desc *uvhub_descs;
1530
1531         timeout_us = calculate_destination_timeout();
1532
1533         uvhub_descs = kmalloc(nuvhubs * sizeof(struct uvhub_desc), GFP_KERNEL);
1534         memset(uvhub_descs, 0, nuvhubs * sizeof(struct uvhub_desc));
1535         uvhub_mask = kzalloc((nuvhubs+7)/8, GFP_KERNEL);
1536         for_each_present_cpu(cpu) {
1537                 bcp = &per_cpu(bau_control, cpu);
1538                 memset(bcp, 0, sizeof(struct bau_control));
1539                 pnode = uv_cpu_hub_info(cpu)->pnode;
1540                 if ((pnode - base_part_pnode) >= UV_DISTRIBUTION_SIZE) {
1541                         printk(KERN_EMERG
1542                                 "cpu %d pnode %d-%d beyond %d; BAU disabled\n",
1543                                 cpu, pnode, base_part_pnode,
1544                                 UV_DISTRIBUTION_SIZE);
1545                         return 1;
1546                 }
1547                 bcp->osnode = cpu_to_node(cpu);
1548                 bcp->partition_base_pnode = uv_partition_base_pnode;
1549                 uvhub = uv_cpu_hub_info(cpu)->numa_blade_id;
1550                 *(uvhub_mask + (uvhub/8)) |= (1 << (uvhub%8));
1551                 bdp = &uvhub_descs[uvhub];
1552                 bdp->num_cpus++;
1553                 bdp->uvhub = uvhub;
1554                 bdp->pnode = pnode;
1555                 /* kludge: 'assuming' one node per socket, and assuming that
1556                    disabling a socket just leaves a gap in node numbers */
1557                 socket = bcp->osnode & 1;
1558                 bdp->socket_mask |= (1 << socket);
1559                 sdp = &bdp->socket[socket];
1560                 sdp->cpu_number[sdp->num_cpus] = cpu;
1561                 sdp->num_cpus++;
1562                 if (sdp->num_cpus > MAX_CPUS_PER_SOCKET) {
1563                         printk(KERN_EMERG "%d cpus per socket invalid\n", sdp->num_cpus);
1564                         return 1;
1565                 }
1566         }
1567         for (uvhub = 0; uvhub < nuvhubs; uvhub++) {
1568                 if (!(*(uvhub_mask + (uvhub/8)) & (1 << (uvhub%8))))
1569                         continue;
1570                 have_hmaster = 0;
1571                 bdp = &uvhub_descs[uvhub];
1572                 socket_mask = bdp->socket_mask;
1573                 socket = 0;
1574                 while (socket_mask) {
1575                         if (!(socket_mask & 1))
1576                                 goto nextsocket;
1577                         sdp = &bdp->socket[socket];
1578                         for (i = 0; i < sdp->num_cpus; i++) {
1579                                 cpu = sdp->cpu_number[i];
1580                                 bcp = &per_cpu(bau_control, cpu);
1581                                 bcp->cpu = cpu;
1582                                 if (i == 0) {
1583                                         smaster = bcp;
1584                                         if (!have_hmaster) {
1585                                                 have_hmaster++;
1586                                                 hmaster = bcp;
1587                                         }
1588                                 }
1589                                 bcp->cpus_in_uvhub = bdp->num_cpus;
1590                                 bcp->cpus_in_socket = sdp->num_cpus;
1591                                 bcp->socket_master = smaster;
1592                                 bcp->uvhub = bdp->uvhub;
1593                                 bcp->uvhub_master = hmaster;
1594                                 bcp->uvhub_cpu = uv_cpu_hub_info(cpu)->
1595                                                 blade_processor_id;
1596                                 if (bcp->uvhub_cpu >= MAX_CPUS_PER_UVHUB) {
1597                                         printk(KERN_EMERG
1598                                                 "%d cpus per uvhub invalid\n",
1599                                                 bcp->uvhub_cpu);
1600                                         return 1;
1601                                 }
1602                         }
1603 nextsocket:
1604                         socket++;
1605                         socket_mask = (socket_mask >> 1);
1606                         /* each socket gets a local array of pnodes/hubs */
1607                         bcp = smaster;
1608                         bcp->target_hub_and_pnode = kmalloc_node(
1609                                 sizeof(struct hub_and_pnode) *
1610                                 num_possible_cpus(), GFP_KERNEL, bcp->osnode);
1611                         memset(bcp->target_hub_and_pnode, 0,
1612                                 sizeof(struct hub_and_pnode) *
1613                                 num_possible_cpus());
1614                         for_each_present_cpu(tcpu) {
1615                                 bcp->target_hub_and_pnode[tcpu].pnode =
1616                                         uv_cpu_hub_info(tcpu)->pnode;
1617                                 bcp->target_hub_and_pnode[tcpu].uvhub =
1618                                         uv_cpu_hub_info(tcpu)->numa_blade_id;
1619                         }
1620                 }
1621         }
1622         kfree(uvhub_descs);
1623         kfree(uvhub_mask);
1624         for_each_present_cpu(cpu) {
1625                 bcp = &per_cpu(bau_control, cpu);
1626                 bcp->baudisabled = 0;
1627                 bcp->statp = &per_cpu(ptcstats, cpu);
1628                 /* time interval to catch a hardware stay-busy bug */
1629                 bcp->timeout_interval = microsec_2_cycles(2*timeout_us);
1630                 bcp->max_bau_concurrent = max_bau_concurrent;
1631                 bcp->max_bau_concurrent_constant = max_bau_concurrent;
1632                 bcp->plugged_delay = plugged_delay;
1633                 bcp->plugsb4reset = plugsb4reset;
1634                 bcp->timeoutsb4reset = timeoutsb4reset;
1635                 bcp->ipi_reset_limit = ipi_reset_limit;
1636                 bcp->complete_threshold = complete_threshold;
1637                 bcp->congested_response_us = congested_response_us;
1638                 bcp->congested_reps = congested_reps;
1639                 bcp->congested_period = congested_period;
1640         }
1641         return 0;
1642 }
1643
1644 /*
1645  * Initialization of BAU-related structures
1646  */
1647 static int __init uv_bau_init(void)
1648 {
1649         int uvhub;
1650         int pnode;
1651         int nuvhubs;
1652         int cur_cpu;
1653         int vector;
1654         unsigned long mmr;
1655
1656         if (!is_uv_system())
1657                 return 0;
1658
1659         if (nobau)
1660                 return 0;
1661
1662         for_each_possible_cpu(cur_cpu)
1663                 zalloc_cpumask_var_node(&per_cpu(uv_flush_tlb_mask, cur_cpu),
1664                                        GFP_KERNEL, cpu_to_node(cur_cpu));
1665
1666         uv_nshift = uv_hub_info->m_val;
1667         uv_mmask = (1UL << uv_hub_info->m_val) - 1;
1668         nuvhubs = uv_num_possible_blades();
1669         spin_lock_init(&disable_lock);
1670         congested_cycles = microsec_2_cycles(congested_response_us);
1671
1672         uv_partition_base_pnode = 0x7fffffff;
1673         for (uvhub = 0; uvhub < nuvhubs; uvhub++) {
1674                 if (uv_blade_nr_possible_cpus(uvhub) &&
1675                         (uv_blade_to_pnode(uvhub) < uv_partition_base_pnode))
1676                         uv_partition_base_pnode = uv_blade_to_pnode(uvhub);
1677         }
1678
1679         if (uv_init_per_cpu(nuvhubs, uv_partition_base_pnode)) {
1680                 nobau = 1;
1681                 return 0;
1682         }
1683
1684         vector = UV_BAU_MESSAGE;
1685         for_each_possible_blade(uvhub)
1686                 if (uv_blade_nr_possible_cpus(uvhub))
1687                         uv_init_uvhub(uvhub, vector, uv_partition_base_pnode);
1688
1689         uv_enable_timeouts();
1690         alloc_intr_gate(vector, uv_bau_message_intr1);
1691
1692         for_each_possible_blade(uvhub) {
1693                 if (uv_blade_nr_possible_cpus(uvhub)) {
1694                         pnode = uv_blade_to_pnode(uvhub);
1695                         /* INIT the bau */
1696                         uv_write_global_mmr64(pnode,
1697                                         UVH_LB_BAU_SB_ACTIVATION_CONTROL,
1698                                         ((unsigned long)1 << 63));
1699                         mmr = 1; /* should be 1 to broadcast to both sockets */
1700                         uv_write_global_mmr64(pnode, UVH_BAU_DATA_BROADCAST,
1701                                                 mmr);
1702                 }
1703         }
1704
1705         return 0;
1706 }
1707 core_initcall(uv_bau_init);
1708 fs_initcall(uv_ptc_init);