]> git.karo-electronics.de Git - mv-sheeva.git/blob - arch/x86/kernel/tlb_uv.c
x86, UV: Remove BAU check for stay-busy
[mv-sheeva.git] / arch / x86 / kernel / 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         int relaxes = 0;
404         unsigned long descriptor_status;
405         unsigned long mmr;
406         unsigned long mask;
407         cycles_t ttime;
408         struct ptc_stats *stat = bcp->statp;
409         struct bau_control *hmaster;
410
411         hmaster = bcp->uvhub_master;
412
413         /* spin on the status MMR, waiting for it to go idle */
414         while ((descriptor_status = (((unsigned long)
415                 uv_read_local_mmr(mmr_offset) >>
416                         right_shift) & UV_ACT_STATUS_MASK)) !=
417                         DESC_STATUS_IDLE) {
418                 /*
419                  * Our software ack messages may be blocked because there are
420                  * no swack resources available.  As long as none of them
421                  * has timed out hardware will NACK our message and its
422                  * state will stay IDLE.
423                  */
424                 if (descriptor_status == DESC_STATUS_SOURCE_TIMEOUT) {
425                         stat->s_stimeout++;
426                         return FLUSH_GIVEUP;
427                 } else if (descriptor_status ==
428                                         DESC_STATUS_DESTINATION_TIMEOUT) {
429                         stat->s_dtimeout++;
430                         ttime = get_cycles();
431
432                         /*
433                          * Our retries may be blocked by all destination
434                          * swack resources being consumed, and a timeout
435                          * pending.  In that case hardware returns the
436                          * ERROR that looks like a destination timeout.
437                          */
438                         if (cycles_2_us(ttime - bcp->send_message) <
439                                                         timeout_us) {
440                                 bcp->conseccompletes = 0;
441                                 return FLUSH_RETRY_PLUGGED;
442                         }
443
444                         bcp->conseccompletes = 0;
445                         return FLUSH_RETRY_TIMEOUT;
446                 } else {
447                         /*
448                          * descriptor_status is still BUSY
449                          */
450                         cpu_relax();
451                 }
452         }
453         bcp->conseccompletes++;
454         return FLUSH_COMPLETE;
455 }
456
457 static inline cycles_t
458 sec_2_cycles(unsigned long sec)
459 {
460         unsigned long ns;
461         cycles_t cyc;
462
463         ns = sec * 1000000000;
464         cyc = (ns << CYC2NS_SCALE_FACTOR)/(per_cpu(cyc2ns, smp_processor_id()));
465         return cyc;
466 }
467
468 /*
469  * conditionally add 1 to *v, unless *v is >= u
470  * return 0 if we cannot add 1 to *v because it is >= u
471  * return 1 if we can add 1 to *v because it is < u
472  * the add is atomic
473  *
474  * This is close to atomic_add_unless(), but this allows the 'u' value
475  * to be lowered below the current 'v'.  atomic_add_unless can only stop
476  * on equal.
477  */
478 static inline int atomic_inc_unless_ge(spinlock_t *lock, atomic_t *v, int u)
479 {
480         spin_lock(lock);
481         if (atomic_read(v) >= u) {
482                 spin_unlock(lock);
483                 return 0;
484         }
485         atomic_inc(v);
486         spin_unlock(lock);
487         return 1;
488 }
489
490 /*
491  * Completions are taking a very long time due to a congested numalink
492  * network.
493  */
494 static void
495 disable_for_congestion(struct bau_control *bcp, struct ptc_stats *stat)
496 {
497         int tcpu;
498         struct bau_control *tbcp;
499
500         /* let only one cpu do this disabling */
501         spin_lock(&disable_lock);
502         if (!baudisabled && bcp->period_requests &&
503             ((bcp->period_time / bcp->period_requests) > congested_cycles)) {
504                 /* it becomes this cpu's job to turn on the use of the
505                    BAU again */
506                 baudisabled = 1;
507                 bcp->set_bau_off = 1;
508                 bcp->set_bau_on_time = get_cycles() +
509                         sec_2_cycles(bcp->congested_period);
510                 stat->s_bau_disabled++;
511                 for_each_present_cpu(tcpu) {
512                         tbcp = &per_cpu(bau_control, tcpu);
513                                 tbcp->baudisabled = 1;
514                 }
515         }
516         spin_unlock(&disable_lock);
517 }
518
519 /**
520  * uv_flush_send_and_wait
521  *
522  * Send a broadcast and wait for it to complete.
523  *
524  * The flush_mask contains the cpus the broadcast is to be sent to, plus
525  * cpus that are on the local uvhub.
526  *
527  * Returns NULL if all flushing represented in the mask was done. The mask
528  * is zeroed.
529  * Returns @flush_mask if some remote flushing remains to be done. The
530  * mask will have some bits still set, representing any cpus on the local
531  * uvhub (not current cpu) and any on remote uvhubs if the broadcast failed.
532  */
533 const struct cpumask *uv_flush_send_and_wait(struct bau_desc *bau_desc,
534                                              struct cpumask *flush_mask,
535                                              struct bau_control *bcp)
536 {
537         int right_shift;
538         int uvhub;
539         int bit;
540         int completion_status = 0;
541         int seq_number = 0;
542         long try = 0;
543         int cpu = bcp->uvhub_cpu;
544         int this_cpu = bcp->cpu;
545         int this_uvhub = bcp->uvhub;
546         unsigned long mmr_offset;
547         unsigned long index;
548         cycles_t time1;
549         cycles_t time2;
550         cycles_t elapsed;
551         struct ptc_stats *stat = bcp->statp;
552         struct bau_control *smaster = bcp->socket_master;
553         struct bau_control *hmaster = bcp->uvhub_master;
554
555         /*
556          * Spin here while there are hmaster->max_bau_concurrent or more active
557          * descriptors. This is the per-uvhub 'throttle'.
558          */
559         if (!atomic_inc_unless_ge(&hmaster->uvhub_lock,
560                         &hmaster->active_descriptor_count,
561                         hmaster->max_bau_concurrent)) {
562                 stat->s_throttles++;
563                 do {
564                         cpu_relax();
565                 } while (!atomic_inc_unless_ge(&hmaster->uvhub_lock,
566                         &hmaster->active_descriptor_count,
567                         hmaster->max_bau_concurrent));
568         }
569
570         while (hmaster->uvhub_quiesce)
571                 cpu_relax();
572
573         if (cpu < UV_CPUS_PER_ACT_STATUS) {
574                 mmr_offset = UVH_LB_BAU_SB_ACTIVATION_STATUS_0;
575                 right_shift = cpu * UV_ACT_STATUS_SIZE;
576         } else {
577                 mmr_offset = UVH_LB_BAU_SB_ACTIVATION_STATUS_1;
578                 right_shift =
579                     ((cpu - UV_CPUS_PER_ACT_STATUS) * UV_ACT_STATUS_SIZE);
580         }
581         time1 = get_cycles();
582         do {
583                 /*
584                  * Every message from any given cpu gets a unique message
585                  * sequence number. But retries use that same number.
586                  * Our message may have timed out at the destination because
587                  * all sw-ack resources are in use and there is a timeout
588                  * pending there.  In that case, our last send never got
589                  * placed into the queue and we need to persist until it
590                  * does.
591                  *
592                  * Make any retry a type MSG_RETRY so that the destination will
593                  * free any resource held by a previous message from this cpu.
594                  */
595                 if (try == 0) {
596                         /* use message type set by the caller the first time */
597                         seq_number = bcp->message_number++;
598                 } else {
599                         /* use RETRY type on all the rest; same sequence */
600                         bau_desc->header.msg_type = MSG_RETRY;
601                         stat->s_retry_messages++;
602                 }
603                 bau_desc->header.sequence = seq_number;
604                 index = (1UL << UVH_LB_BAU_SB_ACTIVATION_CONTROL_PUSH_SHFT) |
605                         bcp->uvhub_cpu;
606                 bcp->send_message = get_cycles();
607
608                 uv_write_local_mmr(UVH_LB_BAU_SB_ACTIVATION_CONTROL, index);
609
610                 try++;
611                 completion_status = uv_wait_completion(bau_desc, mmr_offset,
612                         right_shift, this_cpu, bcp, smaster, try);
613
614                 if (completion_status == FLUSH_RETRY_PLUGGED) {
615                         /*
616                          * Our retries may be blocked by all destination swack
617                          * resources being consumed, and a timeout pending. In
618                          * that case hardware immediately returns the ERROR
619                          * that looks like a destination timeout.
620                          */
621                         udelay(bcp->plugged_delay);
622                         bcp->plugged_tries++;
623                         if (bcp->plugged_tries >= bcp->plugsb4reset) {
624                                 bcp->plugged_tries = 0;
625                                 quiesce_local_uvhub(hmaster);
626                                 spin_lock(&hmaster->queue_lock);
627                                 uv_reset_with_ipi(&bau_desc->distribution,
628                                                         this_cpu);
629                                 spin_unlock(&hmaster->queue_lock);
630                                 end_uvhub_quiesce(hmaster);
631                                 bcp->ipi_attempts++;
632                                 stat->s_resets_plug++;
633                         }
634                 } else if (completion_status == FLUSH_RETRY_TIMEOUT) {
635                         hmaster->max_bau_concurrent = 1;
636                         bcp->timeout_tries++;
637                         udelay(TIMEOUT_DELAY);
638                         if (bcp->timeout_tries >= bcp->timeoutsb4reset) {
639                                 bcp->timeout_tries = 0;
640                                 quiesce_local_uvhub(hmaster);
641                                 spin_lock(&hmaster->queue_lock);
642                                 uv_reset_with_ipi(&bau_desc->distribution,
643                                                                 this_cpu);
644                                 spin_unlock(&hmaster->queue_lock);
645                                 end_uvhub_quiesce(hmaster);
646                                 bcp->ipi_attempts++;
647                                 stat->s_resets_timeout++;
648                         }
649                 }
650                 if (bcp->ipi_attempts >= bcp->ipi_reset_limit) {
651                         bcp->ipi_attempts = 0;
652                         completion_status = FLUSH_GIVEUP;
653                         break;
654                 }
655                 cpu_relax();
656         } while ((completion_status == FLUSH_RETRY_PLUGGED) ||
657                  (completion_status == FLUSH_RETRY_TIMEOUT));
658         time2 = get_cycles();
659
660         bcp->plugged_tries = 0;
661         bcp->timeout_tries = 0;
662
663         if ((completion_status == FLUSH_COMPLETE) &&
664             (bcp->conseccompletes > bcp->complete_threshold) &&
665             (hmaster->max_bau_concurrent <
666                                         hmaster->max_bau_concurrent_constant))
667                         hmaster->max_bau_concurrent++;
668
669         /*
670          * hold any cpu not timing out here; no other cpu currently held by
671          * the 'throttle' should enter the activation code
672          */
673         while (hmaster->uvhub_quiesce)
674                 cpu_relax();
675         atomic_dec(&hmaster->active_descriptor_count);
676
677         /* guard against cycles wrap */
678         if (time2 > time1) {
679                 elapsed = time2 - time1;
680                 stat->s_time += elapsed;
681                 if ((completion_status == FLUSH_COMPLETE) && (try == 1)) {
682                         bcp->period_requests++;
683                         bcp->period_time += elapsed;
684                         if ((elapsed > congested_cycles) &&
685                             (bcp->period_requests > bcp->congested_reps)) {
686                                 disable_for_congestion(bcp, stat);
687                         }
688                 }
689         } else
690                 stat->s_requestor--; /* don't count this one */
691         if (completion_status == FLUSH_COMPLETE && try > 1)
692                 stat->s_retriesok++;
693         else if (completion_status == FLUSH_GIVEUP) {
694                 /*
695                  * Cause the caller to do an IPI-style TLB shootdown on
696                  * the target cpu's, all of which are still in the mask.
697                  */
698                 stat->s_giveup++;
699                 return flush_mask;
700         }
701
702         /*
703          * Success, so clear the remote cpu's from the mask so we don't
704          * use the IPI method of shootdown on them.
705          */
706         for_each_cpu(bit, flush_mask) {
707                 uvhub = uv_cpu_to_blade_id(bit);
708                 if (uvhub == this_uvhub)
709                         continue;
710                 cpumask_clear_cpu(bit, flush_mask);
711         }
712         if (!cpumask_empty(flush_mask))
713                 return flush_mask;
714
715         return NULL;
716 }
717
718 /**
719  * uv_flush_tlb_others - globally purge translation cache of a virtual
720  * address or all TLB's
721  * @cpumask: mask of all cpu's in which the address is to be removed
722  * @mm: mm_struct containing virtual address range
723  * @va: virtual address to be removed (or TLB_FLUSH_ALL for all TLB's on cpu)
724  * @cpu: the current cpu
725  *
726  * This is the entry point for initiating any UV global TLB shootdown.
727  *
728  * Purges the translation caches of all specified processors of the given
729  * virtual address, or purges all TLB's on specified processors.
730  *
731  * The caller has derived the cpumask from the mm_struct.  This function
732  * is called only if there are bits set in the mask. (e.g. flush_tlb_page())
733  *
734  * The cpumask is converted into a uvhubmask of the uvhubs containing
735  * those cpus.
736  *
737  * Note that this function should be called with preemption disabled.
738  *
739  * Returns NULL if all remote flushing was done.
740  * Returns pointer to cpumask if some remote flushing remains to be
741  * done.  The returned pointer is valid till preemption is re-enabled.
742  */
743 const struct cpumask *uv_flush_tlb_others(const struct cpumask *cpumask,
744                                           struct mm_struct *mm,
745                                           unsigned long va, unsigned int cpu)
746 {
747         int remotes;
748         int tcpu;
749         int uvhub;
750         int locals = 0;
751         struct bau_desc *bau_desc;
752         struct cpumask *flush_mask;
753         struct ptc_stats *stat;
754         struct bau_control *bcp;
755         struct bau_control *tbcp;
756
757         /* kernel was booted 'nobau' */
758         if (nobau)
759                 return cpumask;
760
761         bcp = &per_cpu(bau_control, cpu);
762         stat = bcp->statp;
763
764         /* bau was disabled due to slow response */
765         if (bcp->baudisabled) {
766                 /* the cpu that disabled it must re-enable it */
767                 if (bcp->set_bau_off) {
768                         if (get_cycles() >= bcp->set_bau_on_time) {
769                                 stat->s_bau_reenabled++;
770                                 baudisabled = 0;
771                                 for_each_present_cpu(tcpu) {
772                                         tbcp = &per_cpu(bau_control, tcpu);
773                                         tbcp->baudisabled = 0;
774                                         tbcp->period_requests = 0;
775                                         tbcp->period_time = 0;
776                                 }
777                         }
778                 }
779                 return cpumask;
780         }
781
782         /*
783          * Each sending cpu has a per-cpu mask which it fills from the caller's
784          * cpu mask.  Only remote cpus are converted to uvhubs and copied.
785          */
786         flush_mask = (struct cpumask *)per_cpu(uv_flush_tlb_mask, cpu);
787         /*
788          * copy cpumask to flush_mask, removing current cpu
789          * (current cpu should already have been flushed by the caller and
790          *  should never be returned if we return flush_mask)
791          */
792         cpumask_andnot(flush_mask, cpumask, cpumask_of(cpu));
793         if (cpu_isset(cpu, *cpumask))
794                 locals++;  /* current cpu was targeted */
795
796         bau_desc = bcp->descriptor_base;
797         bau_desc += UV_ITEMS_PER_DESCRIPTOR * bcp->uvhub_cpu;
798
799         bau_uvhubs_clear(&bau_desc->distribution, UV_DISTRIBUTION_SIZE);
800         remotes = 0;
801         for_each_cpu(tcpu, flush_mask) {
802                 uvhub = uv_cpu_to_blade_id(tcpu);
803                 if (uvhub == bcp->uvhub) {
804                         locals++;
805                         continue;
806                 }
807                 bau_uvhub_set(uvhub, &bau_desc->distribution);
808                 remotes++;
809         }
810         if (remotes == 0) {
811                 /*
812                  * No off_hub flushing; return status for local hub.
813                  * Return the caller's mask if all were local (the current
814                  * cpu may be in that mask).
815                  */
816                 if (locals)
817                         return cpumask;
818                 else
819                         return NULL;
820         }
821         stat->s_requestor++;
822         stat->s_ntargcpu += remotes;
823         remotes = bau_uvhub_weight(&bau_desc->distribution);
824         stat->s_ntarguvhub += remotes;
825         if (remotes >= 16)
826                 stat->s_ntarguvhub16++;
827         else if (remotes >= 8)
828                 stat->s_ntarguvhub8++;
829         else if (remotes >= 4)
830                 stat->s_ntarguvhub4++;
831         else if (remotes >= 2)
832                 stat->s_ntarguvhub2++;
833         else
834                 stat->s_ntarguvhub1++;
835
836         bau_desc->payload.address = va;
837         bau_desc->payload.sending_cpu = cpu;
838
839         /*
840          * uv_flush_send_and_wait returns null if all cpu's were messaged, or
841          * the adjusted flush_mask if any cpu's were not messaged.
842          */
843         return uv_flush_send_and_wait(bau_desc, flush_mask, bcp);
844 }
845
846 /*
847  * The BAU message interrupt comes here. (registered by set_intr_gate)
848  * See entry_64.S
849  *
850  * We received a broadcast assist message.
851  *
852  * Interrupts are disabled; this interrupt could represent
853  * the receipt of several messages.
854  *
855  * All cores/threads on this hub get this interrupt.
856  * The last one to see it does the software ack.
857  * (the resource will not be freed until noninterruptable cpus see this
858  *  interrupt; hardware may timeout the s/w ack and reply ERROR)
859  */
860 void uv_bau_message_interrupt(struct pt_regs *regs)
861 {
862         int count = 0;
863         cycles_t time_start;
864         struct bau_payload_queue_entry *msg;
865         struct bau_control *bcp;
866         struct ptc_stats *stat;
867         struct msg_desc msgdesc;
868
869         time_start = get_cycles();
870         bcp = &per_cpu(bau_control, smp_processor_id());
871         stat = bcp->statp;
872         msgdesc.va_queue_first = bcp->va_queue_first;
873         msgdesc.va_queue_last = bcp->va_queue_last;
874         msg = bcp->bau_msg_head;
875         while (msg->sw_ack_vector) {
876                 count++;
877                 msgdesc.msg_slot = msg - msgdesc.va_queue_first;
878                 msgdesc.sw_ack_slot = ffs(msg->sw_ack_vector) - 1;
879                 msgdesc.msg = msg;
880                 uv_bau_process_message(&msgdesc, bcp);
881                 msg++;
882                 if (msg > msgdesc.va_queue_last)
883                         msg = msgdesc.va_queue_first;
884                 bcp->bau_msg_head = msg;
885         }
886         stat->d_time += (get_cycles() - time_start);
887         if (!count)
888                 stat->d_nomsg++;
889         else if (count > 1)
890                 stat->d_multmsg++;
891         ack_APIC_irq();
892 }
893
894 /*
895  * uv_enable_timeouts
896  *
897  * Each target uvhub (i.e. a uvhub that has no cpu's) needs to have
898  * shootdown message timeouts enabled.  The timeout does not cause
899  * an interrupt, but causes an error message to be returned to
900  * the sender.
901  */
902 static void uv_enable_timeouts(void)
903 {
904         int uvhub;
905         int nuvhubs;
906         int pnode;
907         unsigned long mmr_image;
908
909         nuvhubs = uv_num_possible_blades();
910
911         for (uvhub = 0; uvhub < nuvhubs; uvhub++) {
912                 if (!uv_blade_nr_possible_cpus(uvhub))
913                         continue;
914
915                 pnode = uv_blade_to_pnode(uvhub);
916                 mmr_image =
917                     uv_read_global_mmr64(pnode, UVH_LB_BAU_MISC_CONTROL);
918                 /*
919                  * Set the timeout period and then lock it in, in three
920                  * steps; captures and locks in the period.
921                  *
922                  * To program the period, the SOFT_ACK_MODE must be off.
923                  */
924                 mmr_image &= ~((unsigned long)1 <<
925                     UVH_LB_BAU_MISC_CONTROL_ENABLE_INTD_SOFT_ACK_MODE_SHFT);
926                 uv_write_global_mmr64
927                     (pnode, UVH_LB_BAU_MISC_CONTROL, mmr_image);
928                 /*
929                  * Set the 4-bit period.
930                  */
931                 mmr_image &= ~((unsigned long)0xf <<
932                      UVH_LB_BAU_MISC_CONTROL_INTD_SOFT_ACK_TIMEOUT_PERIOD_SHFT);
933                 mmr_image |= (UV_INTD_SOFT_ACK_TIMEOUT_PERIOD <<
934                      UVH_LB_BAU_MISC_CONTROL_INTD_SOFT_ACK_TIMEOUT_PERIOD_SHFT);
935                 uv_write_global_mmr64
936                     (pnode, UVH_LB_BAU_MISC_CONTROL, mmr_image);
937                 /*
938                  * Subsequent reversals of the timebase bit (3) cause an
939                  * immediate timeout of one or all INTD resources as
940                  * indicated in bits 2:0 (7 causes all of them to timeout).
941                  */
942                 mmr_image |= ((unsigned long)1 <<
943                     UVH_LB_BAU_MISC_CONTROL_ENABLE_INTD_SOFT_ACK_MODE_SHFT);
944                 uv_write_global_mmr64
945                     (pnode, UVH_LB_BAU_MISC_CONTROL, mmr_image);
946         }
947 }
948
949 static void *uv_ptc_seq_start(struct seq_file *file, loff_t *offset)
950 {
951         if (*offset < num_possible_cpus())
952                 return offset;
953         return NULL;
954 }
955
956 static void *uv_ptc_seq_next(struct seq_file *file, void *data, loff_t *offset)
957 {
958         (*offset)++;
959         if (*offset < num_possible_cpus())
960                 return offset;
961         return NULL;
962 }
963
964 static void uv_ptc_seq_stop(struct seq_file *file, void *data)
965 {
966 }
967
968 static inline unsigned long long
969 microsec_2_cycles(unsigned long microsec)
970 {
971         unsigned long ns;
972         unsigned long long cyc;
973
974         ns = microsec * 1000;
975         cyc = (ns << CYC2NS_SCALE_FACTOR)/(per_cpu(cyc2ns, smp_processor_id()));
976         return cyc;
977 }
978
979 /*
980  * Display the statistics thru /proc.
981  * 'data' points to the cpu number
982  */
983 static int uv_ptc_seq_show(struct seq_file *file, void *data)
984 {
985         struct ptc_stats *stat;
986         int cpu;
987
988         cpu = *(loff_t *)data;
989
990         if (!cpu) {
991                 seq_printf(file,
992                         "# cpu sent stime numuvhubs numuvhubs16 numuvhubs8 ");
993                 seq_printf(file,
994                         "numuvhubs4 numuvhubs2 numuvhubs1 numcpus dto ");
995                 seq_printf(file,
996                         "retries rok resetp resett giveup sto bz throt ");
997                 seq_printf(file,
998                         "sw_ack recv rtime all ");
999                 seq_printf(file,
1000                         "one mult none retry canc nocan reset rcan ");
1001                 seq_printf(file,
1002                         "disable enable\n");
1003         }
1004         if (cpu < num_possible_cpus() && cpu_online(cpu)) {
1005                 stat = &per_cpu(ptcstats, cpu);
1006                 /* source side statistics */
1007                 seq_printf(file,
1008                         "cpu %d %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld ",
1009                            cpu, stat->s_requestor, cycles_2_us(stat->s_time),
1010                            stat->s_ntarguvhub, stat->s_ntarguvhub16,
1011                            stat->s_ntarguvhub8, stat->s_ntarguvhub4,
1012                            stat->s_ntarguvhub2, stat->s_ntarguvhub1,
1013                            stat->s_ntargcpu, stat->s_dtimeout);
1014                 seq_printf(file, "%ld %ld %ld %ld %ld %ld %ld %ld ",
1015                            stat->s_retry_messages, stat->s_retriesok,
1016                            stat->s_resets_plug, stat->s_resets_timeout,
1017                            stat->s_giveup, stat->s_stimeout,
1018                            stat->s_busy, stat->s_throttles);
1019
1020                 /* destination side statistics */
1021                 seq_printf(file,
1022                            "%lx %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld ",
1023                            uv_read_global_mmr64(uv_cpu_to_pnode(cpu),
1024                                         UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE),
1025                            stat->d_requestee, cycles_2_us(stat->d_time),
1026                            stat->d_alltlb, stat->d_onetlb, stat->d_multmsg,
1027                            stat->d_nomsg, stat->d_retries, stat->d_canceled,
1028                            stat->d_nocanceled, stat->d_resets,
1029                            stat->d_rcanceled);
1030                 seq_printf(file, "%ld %ld\n",
1031                         stat->s_bau_disabled, stat->s_bau_reenabled);
1032         }
1033
1034         return 0;
1035 }
1036
1037 /*
1038  * Display the tunables thru debugfs
1039  */
1040 static ssize_t tunables_read(struct file *file, char __user *userbuf,
1041                                                 size_t count, loff_t *ppos)
1042 {
1043         char buf[300];
1044         int ret;
1045
1046         ret = snprintf(buf, 300, "%s %s %s\n%d %d %d %d %d %d %d %d %d\n",
1047                 "max_bau_concurrent plugged_delay plugsb4reset",
1048                 "timeoutsb4reset ipi_reset_limit complete_threshold",
1049                 "congested_response_us congested_reps congested_period",
1050                 max_bau_concurrent, plugged_delay, plugsb4reset,
1051                 timeoutsb4reset, ipi_reset_limit, complete_threshold,
1052                 congested_response_us, congested_reps, congested_period);
1053
1054         return simple_read_from_buffer(userbuf, count, ppos, buf, ret);
1055 }
1056
1057 /*
1058  * -1: resetf the statistics
1059  *  0: display meaning of the statistics
1060  */
1061 static ssize_t uv_ptc_proc_write(struct file *file, const char __user *user,
1062                                  size_t count, loff_t *data)
1063 {
1064         int cpu;
1065         long input_arg;
1066         char optstr[64];
1067         struct ptc_stats *stat;
1068
1069         if (count == 0 || count > sizeof(optstr))
1070                 return -EINVAL;
1071         if (copy_from_user(optstr, user, count))
1072                 return -EFAULT;
1073         optstr[count - 1] = '\0';
1074         if (strict_strtol(optstr, 10, &input_arg) < 0) {
1075                 printk(KERN_DEBUG "%s is invalid\n", optstr);
1076                 return -EINVAL;
1077         }
1078
1079         if (input_arg == 0) {
1080                 printk(KERN_DEBUG "# cpu:      cpu number\n");
1081                 printk(KERN_DEBUG "Sender statistics:\n");
1082                 printk(KERN_DEBUG
1083                 "sent:     number of shootdown messages sent\n");
1084                 printk(KERN_DEBUG
1085                 "stime:    time spent sending messages\n");
1086                 printk(KERN_DEBUG
1087                 "numuvhubs: number of hubs targeted with shootdown\n");
1088                 printk(KERN_DEBUG
1089                 "numuvhubs16: number times 16 or more hubs targeted\n");
1090                 printk(KERN_DEBUG
1091                 "numuvhubs8: number times 8 or more hubs targeted\n");
1092                 printk(KERN_DEBUG
1093                 "numuvhubs4: number times 4 or more hubs targeted\n");
1094                 printk(KERN_DEBUG
1095                 "numuvhubs2: number times 2 or more hubs targeted\n");
1096                 printk(KERN_DEBUG
1097                 "numuvhubs1: number times 1 hub targeted\n");
1098                 printk(KERN_DEBUG
1099                 "numcpus:  number of cpus targeted with shootdown\n");
1100                 printk(KERN_DEBUG
1101                 "dto:      number of destination timeouts\n");
1102                 printk(KERN_DEBUG
1103                 "retries:  destination timeout retries sent\n");
1104                 printk(KERN_DEBUG
1105                 "rok:   :  destination timeouts successfully retried\n");
1106                 printk(KERN_DEBUG
1107                 "resetp:   ipi-style resource resets for plugs\n");
1108                 printk(KERN_DEBUG
1109                 "resett:   ipi-style resource resets for timeouts\n");
1110                 printk(KERN_DEBUG
1111                 "giveup:   fall-backs to ipi-style shootdowns\n");
1112                 printk(KERN_DEBUG
1113                 "sto:      number of source timeouts\n");
1114                 printk(KERN_DEBUG
1115                 "bz:       number of stay-busy's\n");
1116                 printk(KERN_DEBUG
1117                 "throt:    number times spun in throttle\n");
1118                 printk(KERN_DEBUG "Destination side statistics:\n");
1119                 printk(KERN_DEBUG
1120                 "sw_ack:   image of UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE\n");
1121                 printk(KERN_DEBUG
1122                 "recv:     shootdown messages received\n");
1123                 printk(KERN_DEBUG
1124                 "rtime:    time spent processing messages\n");
1125                 printk(KERN_DEBUG
1126                 "all:      shootdown all-tlb messages\n");
1127                 printk(KERN_DEBUG
1128                 "one:      shootdown one-tlb messages\n");
1129                 printk(KERN_DEBUG
1130                 "mult:     interrupts that found multiple messages\n");
1131                 printk(KERN_DEBUG
1132                 "none:     interrupts that found no messages\n");
1133                 printk(KERN_DEBUG
1134                 "retry:    number of retry messages processed\n");
1135                 printk(KERN_DEBUG
1136                 "canc:     number messages canceled by retries\n");
1137                 printk(KERN_DEBUG
1138                 "nocan:    number retries that found nothing to cancel\n");
1139                 printk(KERN_DEBUG
1140                 "reset:    number of ipi-style reset requests processed\n");
1141                 printk(KERN_DEBUG
1142                 "rcan:     number messages canceled by reset requests\n");
1143                 printk(KERN_DEBUG
1144                 "disable:  number times use of the BAU was disabled\n");
1145                 printk(KERN_DEBUG
1146                 "enable:   number times use of the BAU was re-enabled\n");
1147         } else if (input_arg == -1) {
1148                 for_each_present_cpu(cpu) {
1149                         stat = &per_cpu(ptcstats, cpu);
1150                         memset(stat, 0, sizeof(struct ptc_stats));
1151                 }
1152         }
1153
1154         return count;
1155 }
1156
1157 static int local_atoi(const char *name)
1158 {
1159         int val = 0;
1160
1161         for (;; name++) {
1162                 switch (*name) {
1163                 case '0' ... '9':
1164                         val = 10*val+(*name-'0');
1165                         break;
1166                 default:
1167                         return val;
1168                 }
1169         }
1170 }
1171
1172 /*
1173  * set the tunables
1174  * 0 values reset them to defaults
1175  */
1176 static ssize_t tunables_write(struct file *file, const char __user *user,
1177                                  size_t count, loff_t *data)
1178 {
1179         int cpu;
1180         int cnt = 0;
1181         int val;
1182         char *p;
1183         char *q;
1184         char instr[64];
1185         struct bau_control *bcp;
1186
1187         if (count == 0 || count > sizeof(instr)-1)
1188                 return -EINVAL;
1189         if (copy_from_user(instr, user, count))
1190                 return -EFAULT;
1191
1192         instr[count] = '\0';
1193         /* count the fields */
1194         p = instr + strspn(instr, WHITESPACE);
1195         q = p;
1196         for (; *p; p = q + strspn(q, WHITESPACE)) {
1197                 q = p + strcspn(p, WHITESPACE);
1198                 cnt++;
1199                 if (q == p)
1200                         break;
1201         }
1202         if (cnt != 9) {
1203                 printk(KERN_INFO "bau tunable error: should be 9 numbers\n");
1204                 return -EINVAL;
1205         }
1206
1207         p = instr + strspn(instr, WHITESPACE);
1208         q = p;
1209         for (cnt = 0; *p; p = q + strspn(q, WHITESPACE), cnt++) {
1210                 q = p + strcspn(p, WHITESPACE);
1211                 val = local_atoi(p);
1212                 switch (cnt) {
1213                 case 0:
1214                         if (val == 0) {
1215                                 max_bau_concurrent = MAX_BAU_CONCURRENT;
1216                                 max_bau_concurrent_constant =
1217                                                         MAX_BAU_CONCURRENT;
1218                                 continue;
1219                         }
1220                         bcp = &per_cpu(bau_control, smp_processor_id());
1221                         if (val < 1 || val > bcp->cpus_in_uvhub) {
1222                                 printk(KERN_DEBUG
1223                                 "Error: BAU max concurrent %d is invalid\n",
1224                                 val);
1225                                 return -EINVAL;
1226                         }
1227                         max_bau_concurrent = val;
1228                         max_bau_concurrent_constant = val;
1229                         continue;
1230                 case 1:
1231                         if (val == 0)
1232                                 plugged_delay = PLUGGED_DELAY;
1233                         else
1234                                 plugged_delay = val;
1235                         continue;
1236                 case 2:
1237                         if (val == 0)
1238                                 plugsb4reset = PLUGSB4RESET;
1239                         else
1240                                 plugsb4reset = val;
1241                         continue;
1242                 case 3:
1243                         if (val == 0)
1244                                 timeoutsb4reset = TIMEOUTSB4RESET;
1245                         else
1246                                 timeoutsb4reset = val;
1247                         continue;
1248                 case 4:
1249                         if (val == 0)
1250                                 ipi_reset_limit = IPI_RESET_LIMIT;
1251                         else
1252                                 ipi_reset_limit = val;
1253                         continue;
1254                 case 5:
1255                         if (val == 0)
1256                                 complete_threshold = COMPLETE_THRESHOLD;
1257                         else
1258                                 complete_threshold = val;
1259                         continue;
1260                 case 6:
1261                         if (val == 0)
1262                                 congested_response_us = CONGESTED_RESPONSE_US;
1263                         else
1264                                 congested_response_us = val;
1265                         continue;
1266                 case 7:
1267                         if (val == 0)
1268                                 congested_reps = CONGESTED_REPS;
1269                         else
1270                                 congested_reps = val;
1271                         continue;
1272                 case 8:
1273                         if (val == 0)
1274                                 congested_period = CONGESTED_PERIOD;
1275                         else
1276                                 congested_period = val;
1277                         continue;
1278                 }
1279                 if (q == p)
1280                         break;
1281         }
1282         for_each_present_cpu(cpu) {
1283                 bcp = &per_cpu(bau_control, cpu);
1284                 bcp->max_bau_concurrent = max_bau_concurrent;
1285                 bcp->max_bau_concurrent_constant = max_bau_concurrent;
1286                 bcp->plugged_delay = plugged_delay;
1287                 bcp->plugsb4reset = plugsb4reset;
1288                 bcp->timeoutsb4reset = timeoutsb4reset;
1289                 bcp->ipi_reset_limit = ipi_reset_limit;
1290                 bcp->complete_threshold = complete_threshold;
1291                 bcp->congested_response_us = congested_response_us;
1292                 bcp->congested_reps = congested_reps;
1293                 bcp->congested_period = congested_period;
1294         }
1295         return count;
1296 }
1297
1298 static const struct seq_operations uv_ptc_seq_ops = {
1299         .start          = uv_ptc_seq_start,
1300         .next           = uv_ptc_seq_next,
1301         .stop           = uv_ptc_seq_stop,
1302         .show           = uv_ptc_seq_show
1303 };
1304
1305 static int uv_ptc_proc_open(struct inode *inode, struct file *file)
1306 {
1307         return seq_open(file, &uv_ptc_seq_ops);
1308 }
1309
1310 static int tunables_open(struct inode *inode, struct file *file)
1311 {
1312         return 0;
1313 }
1314
1315 static const struct file_operations proc_uv_ptc_operations = {
1316         .open           = uv_ptc_proc_open,
1317         .read           = seq_read,
1318         .write          = uv_ptc_proc_write,
1319         .llseek         = seq_lseek,
1320         .release        = seq_release,
1321 };
1322
1323 static const struct file_operations tunables_fops = {
1324         .open           = tunables_open,
1325         .read           = tunables_read,
1326         .write          = tunables_write,
1327 };
1328
1329 static int __init uv_ptc_init(void)
1330 {
1331         struct proc_dir_entry *proc_uv_ptc;
1332
1333         if (!is_uv_system())
1334                 return 0;
1335
1336         proc_uv_ptc = proc_create(UV_PTC_BASENAME, 0444, NULL,
1337                                   &proc_uv_ptc_operations);
1338         if (!proc_uv_ptc) {
1339                 printk(KERN_ERR "unable to create %s proc entry\n",
1340                        UV_PTC_BASENAME);
1341                 return -EINVAL;
1342         }
1343
1344         tunables_dir = debugfs_create_dir(UV_BAU_TUNABLES_DIR, NULL);
1345         if (!tunables_dir) {
1346                 printk(KERN_ERR "unable to create debugfs directory %s\n",
1347                        UV_BAU_TUNABLES_DIR);
1348                 return -EINVAL;
1349         }
1350         tunables_file = debugfs_create_file(UV_BAU_TUNABLES_FILE, 0600,
1351                         tunables_dir, NULL, &tunables_fops);
1352         if (!tunables_file) {
1353                 printk(KERN_ERR "unable to create debugfs file %s\n",
1354                        UV_BAU_TUNABLES_FILE);
1355                 return -EINVAL;
1356         }
1357         return 0;
1358 }
1359
1360 /*
1361  * initialize the sending side's sending buffers
1362  */
1363 static void
1364 uv_activation_descriptor_init(int node, int pnode)
1365 {
1366         int i;
1367         int cpu;
1368         unsigned long pa;
1369         unsigned long m;
1370         unsigned long n;
1371         struct bau_desc *bau_desc;
1372         struct bau_desc *bd2;
1373         struct bau_control *bcp;
1374
1375         /*
1376          * each bau_desc is 64 bytes; there are 8 (UV_ITEMS_PER_DESCRIPTOR)
1377          * per cpu; and up to 32 (UV_ADP_SIZE) cpu's per uvhub
1378          */
1379         bau_desc = (struct bau_desc *)kmalloc_node(sizeof(struct bau_desc)*
1380                 UV_ADP_SIZE*UV_ITEMS_PER_DESCRIPTOR, GFP_KERNEL, node);
1381         BUG_ON(!bau_desc);
1382
1383         pa = uv_gpa(bau_desc); /* need the real nasid*/
1384         n = pa >> uv_nshift;
1385         m = pa & uv_mmask;
1386
1387         uv_write_global_mmr64(pnode, UVH_LB_BAU_SB_DESCRIPTOR_BASE,
1388                               (n << UV_DESC_BASE_PNODE_SHIFT | m));
1389
1390         /*
1391          * initializing all 8 (UV_ITEMS_PER_DESCRIPTOR) descriptors for each
1392          * cpu even though we only use the first one; one descriptor can
1393          * describe a broadcast to 256 uv hubs.
1394          */
1395         for (i = 0, bd2 = bau_desc; i < (UV_ADP_SIZE*UV_ITEMS_PER_DESCRIPTOR);
1396                 i++, bd2++) {
1397                 memset(bd2, 0, sizeof(struct bau_desc));
1398                 bd2->header.sw_ack_flag = 1;
1399                 /*
1400                  * base_dest_nodeid is the nasid (pnode<<1) of the first uvhub
1401                  * in the partition. The bit map will indicate uvhub numbers,
1402                  * which are 0-N in a partition. Pnodes are unique system-wide.
1403                  */
1404                 bd2->header.base_dest_nodeid = uv_partition_base_pnode << 1;
1405                 bd2->header.dest_subnodeid = 0x10; /* the LB */
1406                 bd2->header.command = UV_NET_ENDPOINT_INTD;
1407                 bd2->header.int_both = 1;
1408                 /*
1409                  * all others need to be set to zero:
1410                  *   fairness chaining multilevel count replied_to
1411                  */
1412         }
1413         for_each_present_cpu(cpu) {
1414                 if (pnode != uv_blade_to_pnode(uv_cpu_to_blade_id(cpu)))
1415                         continue;
1416                 bcp = &per_cpu(bau_control, cpu);
1417                 bcp->descriptor_base = bau_desc;
1418         }
1419 }
1420
1421 /*
1422  * initialize the destination side's receiving buffers
1423  * entered for each uvhub in the partition
1424  * - node is first node (kernel memory notion) on the uvhub
1425  * - pnode is the uvhub's physical identifier
1426  */
1427 static void
1428 uv_payload_queue_init(int node, int pnode)
1429 {
1430         int pn;
1431         int cpu;
1432         char *cp;
1433         unsigned long pa;
1434         struct bau_payload_queue_entry *pqp;
1435         struct bau_payload_queue_entry *pqp_malloc;
1436         struct bau_control *bcp;
1437
1438         pqp = (struct bau_payload_queue_entry *) kmalloc_node(
1439                 (DEST_Q_SIZE + 1) * sizeof(struct bau_payload_queue_entry),
1440                 GFP_KERNEL, node);
1441         BUG_ON(!pqp);
1442         pqp_malloc = pqp;
1443
1444         cp = (char *)pqp + 31;
1445         pqp = (struct bau_payload_queue_entry *)(((unsigned long)cp >> 5) << 5);
1446
1447         for_each_present_cpu(cpu) {
1448                 if (pnode != uv_cpu_to_pnode(cpu))
1449                         continue;
1450                 /* for every cpu on this pnode: */
1451                 bcp = &per_cpu(bau_control, cpu);
1452                 bcp->va_queue_first = pqp;
1453                 bcp->bau_msg_head = pqp;
1454                 bcp->va_queue_last = pqp + (DEST_Q_SIZE - 1);
1455         }
1456         /*
1457          * need the pnode of where the memory was really allocated
1458          */
1459         pa = uv_gpa(pqp);
1460         pn = pa >> uv_nshift;
1461         uv_write_global_mmr64(pnode,
1462                               UVH_LB_BAU_INTD_PAYLOAD_QUEUE_FIRST,
1463                               ((unsigned long)pn << UV_PAYLOADQ_PNODE_SHIFT) |
1464                               uv_physnodeaddr(pqp));
1465         uv_write_global_mmr64(pnode, UVH_LB_BAU_INTD_PAYLOAD_QUEUE_TAIL,
1466                               uv_physnodeaddr(pqp));
1467         uv_write_global_mmr64(pnode, UVH_LB_BAU_INTD_PAYLOAD_QUEUE_LAST,
1468                               (unsigned long)
1469                               uv_physnodeaddr(pqp + (DEST_Q_SIZE - 1)));
1470         /* in effect, all msg_type's are set to MSG_NOOP */
1471         memset(pqp, 0, sizeof(struct bau_payload_queue_entry) * DEST_Q_SIZE);
1472 }
1473
1474 /*
1475  * Initialization of each UV hub's structures
1476  */
1477 static void __init uv_init_uvhub(int uvhub, int vector)
1478 {
1479         int node;
1480         int pnode;
1481         unsigned long apicid;
1482
1483         node = uvhub_to_first_node(uvhub);
1484         pnode = uv_blade_to_pnode(uvhub);
1485         uv_activation_descriptor_init(node, pnode);
1486         uv_payload_queue_init(node, pnode);
1487         /*
1488          * the below initialization can't be in firmware because the
1489          * messaging IRQ will be determined by the OS
1490          */
1491         apicid = uvhub_to_first_apicid(uvhub);
1492         uv_write_global_mmr64(pnode, UVH_BAU_DATA_CONFIG,
1493                                       ((apicid << 32) | vector));
1494 }
1495
1496 /*
1497  * We will set BAU_MISC_CONTROL with a timeout period.
1498  * But the BIOS has set UVH_AGING_PRESCALE_SEL and UVH_TRANSACTION_TIMEOUT.
1499  * So the destination timeout period has be be calculated from them.
1500  */
1501 static int
1502 calculate_destination_timeout(void)
1503 {
1504         unsigned long mmr_image;
1505         int mult1;
1506         int mult2;
1507         int index;
1508         int base;
1509         int ret;
1510         unsigned long ts_ns;
1511
1512         mult1 = UV_INTD_SOFT_ACK_TIMEOUT_PERIOD & BAU_MISC_CONTROL_MULT_MASK;
1513         mmr_image = uv_read_local_mmr(UVH_AGING_PRESCALE_SEL);
1514         index = (mmr_image >> BAU_URGENCY_7_SHIFT) & BAU_URGENCY_7_MASK;
1515         mmr_image = uv_read_local_mmr(UVH_TRANSACTION_TIMEOUT);
1516         mult2 = (mmr_image >> BAU_TRANS_SHIFT) & BAU_TRANS_MASK;
1517         base = timeout_base_ns[index];
1518         ts_ns = base * mult1 * mult2;
1519         ret = ts_ns / 1000;
1520         return ret;
1521 }
1522
1523 /*
1524  * initialize the bau_control structure for each cpu
1525  */
1526 static void uv_init_per_cpu(int nuvhubs)
1527 {
1528         int i;
1529         int cpu;
1530         int pnode;
1531         int uvhub;
1532         short socket = 0;
1533         unsigned short socket_mask;
1534         unsigned int uvhub_mask;
1535         struct bau_control *bcp;
1536         struct uvhub_desc *bdp;
1537         struct socket_desc *sdp;
1538         struct bau_control *hmaster = NULL;
1539         struct bau_control *smaster = NULL;
1540         struct socket_desc {
1541                 short num_cpus;
1542                 short cpu_number[16];
1543         };
1544         struct uvhub_desc {
1545                 unsigned short socket_mask;
1546                 short num_cpus;
1547                 short uvhub;
1548                 short pnode;
1549                 struct socket_desc socket[2];
1550         };
1551         struct uvhub_desc *uvhub_descs;
1552
1553         timeout_us = calculate_destination_timeout();
1554
1555         uvhub_descs = (struct uvhub_desc *)
1556                 kmalloc(nuvhubs * sizeof(struct uvhub_desc), GFP_KERNEL);
1557         memset(uvhub_descs, 0, nuvhubs * sizeof(struct uvhub_desc));
1558         for_each_present_cpu(cpu) {
1559                 bcp = &per_cpu(bau_control, cpu);
1560                 memset(bcp, 0, sizeof(struct bau_control));
1561                 pnode = uv_cpu_hub_info(cpu)->pnode;
1562                 uvhub = uv_cpu_hub_info(cpu)->numa_blade_id;
1563                 uvhub_mask |= (1 << uvhub);
1564                 bdp = &uvhub_descs[uvhub];
1565                 bdp->num_cpus++;
1566                 bdp->uvhub = uvhub;
1567                 bdp->pnode = pnode;
1568                 /* kludge: 'assuming' one node per socket, and assuming that
1569                    disabling a socket just leaves a gap in node numbers */
1570                 socket = (cpu_to_node(cpu) & 1);;
1571                 bdp->socket_mask |= (1 << socket);
1572                 sdp = &bdp->socket[socket];
1573                 sdp->cpu_number[sdp->num_cpus] = cpu;
1574                 sdp->num_cpus++;
1575         }
1576         uvhub = 0;
1577         while (uvhub_mask) {
1578                 if (!(uvhub_mask & 1))
1579                         goto nexthub;
1580                 bdp = &uvhub_descs[uvhub];
1581                 socket_mask = bdp->socket_mask;
1582                 socket = 0;
1583                 while (socket_mask) {
1584                         if (!(socket_mask & 1))
1585                                 goto nextsocket;
1586                         sdp = &bdp->socket[socket];
1587                         for (i = 0; i < sdp->num_cpus; i++) {
1588                                 cpu = sdp->cpu_number[i];
1589                                 bcp = &per_cpu(bau_control, cpu);
1590                                 bcp->cpu = cpu;
1591                                 if (i == 0) {
1592                                         smaster = bcp;
1593                                         if (socket == 0)
1594                                                 hmaster = bcp;
1595                                 }
1596                                 bcp->cpus_in_uvhub = bdp->num_cpus;
1597                                 bcp->cpus_in_socket = sdp->num_cpus;
1598                                 bcp->socket_master = smaster;
1599                                 bcp->uvhub = bdp->uvhub;
1600                                 bcp->uvhub_master = hmaster;
1601                                 bcp->uvhub_cpu = uv_cpu_hub_info(cpu)->
1602                                                 blade_processor_id;
1603                         }
1604 nextsocket:
1605                         socket++;
1606                         socket_mask = (socket_mask >> 1);
1607                 }
1608 nexthub:
1609                 uvhub++;
1610                 uvhub_mask = (uvhub_mask >> 1);
1611         }
1612         kfree(uvhub_descs);
1613         for_each_present_cpu(cpu) {
1614                 bcp = &per_cpu(bau_control, cpu);
1615                 bcp->baudisabled = 0;
1616                 bcp->statp = &per_cpu(ptcstats, cpu);
1617                 /* time interval to catch a hardware stay-busy bug */
1618                 bcp->timeout_interval = microsec_2_cycles(2*timeout_us);
1619                 bcp->max_bau_concurrent = max_bau_concurrent;
1620                 bcp->max_bau_concurrent_constant = max_bau_concurrent;
1621                 bcp->plugged_delay = plugged_delay;
1622                 bcp->plugsb4reset = plugsb4reset;
1623                 bcp->timeoutsb4reset = timeoutsb4reset;
1624                 bcp->ipi_reset_limit = ipi_reset_limit;
1625                 bcp->complete_threshold = complete_threshold;
1626                 bcp->congested_response_us = congested_response_us;
1627                 bcp->congested_reps = congested_reps;
1628                 bcp->congested_period = congested_period;
1629         }
1630 }
1631
1632 /*
1633  * Initialization of BAU-related structures
1634  */
1635 static int __init uv_bau_init(void)
1636 {
1637         int uvhub;
1638         int pnode;
1639         int nuvhubs;
1640         int cur_cpu;
1641         int vector;
1642         unsigned long mmr;
1643
1644         if (!is_uv_system())
1645                 return 0;
1646
1647         if (nobau)
1648                 return 0;
1649
1650         for_each_possible_cpu(cur_cpu)
1651                 zalloc_cpumask_var_node(&per_cpu(uv_flush_tlb_mask, cur_cpu),
1652                                        GFP_KERNEL, cpu_to_node(cur_cpu));
1653
1654         uv_nshift = uv_hub_info->m_val;
1655         uv_mmask = (1UL << uv_hub_info->m_val) - 1;
1656         nuvhubs = uv_num_possible_blades();
1657         spin_lock_init(&disable_lock);
1658         congested_cycles = microsec_2_cycles(congested_response_us);
1659
1660         uv_init_per_cpu(nuvhubs);
1661
1662         uv_partition_base_pnode = 0x7fffffff;
1663         for (uvhub = 0; uvhub < nuvhubs; uvhub++)
1664                 if (uv_blade_nr_possible_cpus(uvhub) &&
1665                         (uv_blade_to_pnode(uvhub) < uv_partition_base_pnode))
1666                         uv_partition_base_pnode = uv_blade_to_pnode(uvhub);
1667
1668         vector = UV_BAU_MESSAGE;
1669         for_each_possible_blade(uvhub)
1670                 if (uv_blade_nr_possible_cpus(uvhub))
1671                         uv_init_uvhub(uvhub, vector);
1672
1673         uv_enable_timeouts();
1674         alloc_intr_gate(vector, uv_bau_message_intr1);
1675
1676         for_each_possible_blade(uvhub) {
1677                 pnode = uv_blade_to_pnode(uvhub);
1678                 /* INIT the bau */
1679                 uv_write_global_mmr64(pnode, UVH_LB_BAU_SB_ACTIVATION_CONTROL,
1680                                       ((unsigned long)1 << 63));
1681                 mmr = 1; /* should be 1 to broadcast to both sockets */
1682                 uv_write_global_mmr64(pnode, UVH_BAU_DATA_BROADCAST, mmr);
1683         }
1684
1685         return 0;
1686 }
1687 core_initcall(uv_bau_init);
1688 fs_initcall(uv_ptc_init);