2 * SGI UltraViolet TLB flush routines.
4 * (c) 2008-2010 Cliff Wickman <cpw@sgi.com>, SGI.
6 * This code is released under the GNU General Public License version 2 or
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>
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>
23 #include <asm/irq_vectors.h>
24 #include <asm/timer.h>
26 /* timeouts in nanoseconds (indexed by UVH_AGING_PRESCALE_SEL urgency7 30:28) */
27 static int timeout_base_ns[] = {
37 static int timeout_us;
39 static int baudisabled;
40 static spinlock_t disable_lock;
41 static cycles_t congested_cycles;
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;
57 static int __init setup_nobau(char *arg)
62 early_param("nobau", setup_nobau);
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;
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);
75 * Determine the first node on a uvhub. 'Nodes' are used for kernel
78 static int __init uvhub_to_first_node(int uvhub)
82 for_each_online_node(node) {
83 b = uv_node_to_blade_id(node);
91 * Determine the apicid of the first cpu on a uvhub.
93 static int __init uvhub_to_first_apicid(int uvhub)
97 for_each_present_cpu(cpu)
98 if (uvhub == uv_cpu_to_blade_id(cpu))
99 return per_cpu(x86_cpu_to_apicid, cpu);
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).
111 static inline void uv_reply_to_message(struct msg_desc *mdp,
112 struct bau_control *bcp)
115 struct bau_payload_queue_entry *msg;
118 if (!msg->canceled) {
119 dw = (msg->sw_ack_vector << UV_SW_ACK_NPENDING) |
122 UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE_ALIAS, dw);
125 msg->sw_ack_vector = 0;
129 * Process the receipt of a RETRY message
131 static inline void uv_bau_process_retry_msg(struct msg_desc *mdp,
132 struct bau_control *bcp)
135 int cancel_count = 0;
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;
147 * cancel any message from msg+1 to the retry itself
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;
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;
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.
171 if (mmr & (msg_res << UV_SW_ACK_NPENDING)) {
173 * is the resource timed out?
174 * make everyone ignore the cancelled message.
180 UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE_ALIAS,
181 (msg_res << UV_SW_ACK_NPENDING) |
187 stat->d_nocanceled++;
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.
194 static void uv_bau_process_message(struct msg_desc *mdp,
195 struct bau_control *bcp)
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;
204 * This must be a normal message, or retry of a normal message
208 if (msg->address == TLB_FLUSH_ALL) {
212 __flush_tlb_one(msg->address);
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
223 if (msg->msg_type == MSG_RETRY && bcp == bcp->uvhub_master)
224 uv_bau_process_retry_msg(mdp, bcp);
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
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) {
236 * Both sockets dump their completed count total into
237 * the message's count.
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);
243 if (msg_ack_count == bcp->cpus_in_uvhub) {
245 * All cpus in uvhub saw it; reply
247 uv_reply_to_message(mdp, bcp);
255 * Determine the first cpu on a uvhub.
257 static int uvhub_to_first_cpu(int uvhub)
260 for_each_present_cpu(cpu)
261 if (uvhub == uv_cpu_to_blade_id(cpu))
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.
272 * This is entered for a single cpu on the uvhub.
273 * The sender want's this uvhub to free a specific message's
277 uv_do_reset(void *ptr)
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;
289 bcp = &per_cpu(bau_control, smp_processor_id());
290 rap = (struct reset_args *)ptr;
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.
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)) {
309 * make everyone else ignore this message
312 slot = msg - bcp->va_queue_first;
315 * only reset the resource if it is still pending
317 mmr = uv_read_local_mmr
318 (UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE);
319 msg_res = msg->sw_ack_vector;
323 UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE_ALIAS,
324 (msg_res << UV_SW_ACK_NPENDING) |
333 * Use IPI to get all target uvhubs to release resources held by
334 * a given sending cpu number.
336 static void uv_reset_with_ipi(struct bau_target_uvhubmask *distribution,
342 struct reset_args reset_args;
344 reset_args.sender = sender;
347 /* find a single cpu for each uvhub in this distribution mask */
349 uvhub < sizeof(struct bau_target_uvhubmask) * BITSPERBYTE;
351 if (!bau_uvhub_isset(uvhub, distribution))
353 /* find a cpu for this uvhub */
354 cpu = uvhub_to_first_cpu(uvhub);
357 /* IPI all cpus; Preemption is already disabled */
358 smp_call_function_many(&mask, uv_do_reset, (void *)&reset_args, 1);
362 static inline unsigned long
363 cycles_2_us(unsigned long long cyc)
365 unsigned long long ns;
367 ns = (cyc * per_cpu(cyc2ns, smp_processor_id()))
368 >> CYC2NS_SCALE_FACTOR;
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()
379 quiesce_local_uvhub(struct bau_control *hmaster)
381 atomic_add_short_return(1, (struct atomic_short *)
382 &hmaster->uvhub_quiesce);
386 * mark this quiet-requestor as done
389 end_uvhub_quiesce(struct bau_control *hmaster)
391 atomic_add_short_return(-1, (struct atomic_short *)
392 &hmaster->uvhub_quiesce);
396 * Wait for completion of a broadcast software ack message
397 * return COMPLETE, RETRY(PLUGGED or TIMEOUT) or GIVEUP
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)
403 unsigned long descriptor_status;
405 struct ptc_stats *stat = bcp->statp;
406 struct bau_control *hmaster;
408 hmaster = bcp->uvhub_master;
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)) !=
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.
421 if (descriptor_status == DESC_STATUS_SOURCE_TIMEOUT) {
424 } else if (descriptor_status ==
425 DESC_STATUS_DESTINATION_TIMEOUT) {
427 ttime = get_cycles();
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.
435 if (cycles_2_us(ttime - bcp->send_message) <
437 bcp->conseccompletes = 0;
438 return FLUSH_RETRY_PLUGGED;
441 bcp->conseccompletes = 0;
442 return FLUSH_RETRY_TIMEOUT;
445 * descriptor_status is still BUSY
450 bcp->conseccompletes++;
451 return FLUSH_COMPLETE;
454 static inline cycles_t
455 sec_2_cycles(unsigned long sec)
460 ns = sec * 1000000000;
461 cyc = (ns << CYC2NS_SCALE_FACTOR)/(per_cpu(cyc2ns, smp_processor_id()));
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
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
475 static inline int atomic_inc_unless_ge(spinlock_t *lock, atomic_t *v, int u)
478 if (atomic_read(v) >= u) {
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.
493 destination_plugged(struct bau_desc *bau_desc, struct bau_control *bcp,
494 struct bau_control *hmaster, struct ptc_stats *stat)
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);
506 stat->s_resets_plug++;
511 destination_timeout(struct bau_desc *bau_desc, struct bau_control *bcp,
512 struct bau_control *hmaster, struct ptc_stats *stat)
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);
524 stat->s_resets_timeout++;
529 * Completions are taking a very long time due to a congested numalink
533 disable_for_congestion(struct bau_control *bcp, struct ptc_stats *stat)
536 struct bau_control *tbcp;
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
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;
554 spin_unlock(&disable_lock);
558 * uv_flush_send_and_wait
560 * Send a broadcast and wait for it to complete.
562 * The flush_mask contains the cpus the broadcast is to be sent to including
563 * cpus that are on the local uvhub.
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.
569 int uv_flush_send_and_wait(struct bau_desc *bau_desc,
570 struct cpumask *flush_mask, struct bau_control *bcp)
573 int completion_status = 0;
576 int cpu = bcp->uvhub_cpu;
577 int this_cpu = bcp->cpu;
578 unsigned long mmr_offset;
583 struct ptc_stats *stat = bcp->statp;
584 struct bau_control *smaster = bcp->socket_master;
585 struct bau_control *hmaster = bcp->uvhub_master;
587 if (!atomic_inc_unless_ge(&hmaster->uvhub_lock,
588 &hmaster->active_descriptor_count,
589 hmaster->max_bau_concurrent)) {
593 } while (!atomic_inc_unless_ge(&hmaster->uvhub_lock,
594 &hmaster->active_descriptor_count,
595 hmaster->max_bau_concurrent));
597 while (hmaster->uvhub_quiesce)
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;
604 mmr_offset = UVH_LB_BAU_SB_ACTIVATION_STATUS_1;
606 ((cpu - UV_CPUS_PER_ACT_STATUS) * UV_ACT_STATUS_SIZE);
608 time1 = get_cycles();
611 bau_desc->header.msg_type = MSG_REGULAR;
612 seq_number = bcp->message_number++;
614 bau_desc->header.msg_type = MSG_RETRY;
615 stat->s_retry_messages++;
617 bau_desc->header.sequence = seq_number;
618 index = (1UL << UVH_LB_BAU_SB_ACTIVATION_CONTROL_PUSH_SHFT) |
620 bcp->send_message = get_cycles();
621 uv_write_local_mmr(UVH_LB_BAU_SB_ACTIVATION_CONTROL, index);
623 completion_status = uv_wait_completion(bau_desc, mmr_offset,
624 right_shift, this_cpu, bcp, smaster, try);
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);
631 if (bcp->ipi_attempts >= bcp->ipi_reset_limit) {
632 bcp->ipi_attempts = 0;
633 completion_status = FLUSH_GIVEUP;
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)
649 atomic_dec(&hmaster->active_descriptor_count);
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);
663 if (completion_status == FLUSH_COMPLETE && try > 1)
665 else if (completion_status == FLUSH_GIVEUP) {
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
680 * This is the entry point for initiating any UV global TLB shootdown.
682 * Purges the translation caches of all specified processors of the given
683 * virtual address, or purges all TLB's on specified processors.
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())
688 * The cpumask is converted into a uvhubmask of the uvhubs containing
691 * Note that this function should be called with preemption disabled.
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.
697 const struct cpumask *uv_flush_tlb_others(const struct cpumask *cpumask,
698 struct mm_struct *mm,
699 unsigned long va, unsigned int cpu)
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;
712 /* kernel was booted 'nobau' */
716 bcp = &per_cpu(bau_control, cpu);
719 /* bau was disabled due to slow response */
720 if (bcp->baudisabled) {
721 /* the cpu that disabled it must re-enable it */
722 if (bcp->set_bau_off) {
723 if (get_cycles() >= bcp->set_bau_on_time) {
724 stat->s_bau_reenabled++;
726 for_each_present_cpu(tcpu) {
727 tbcp = &per_cpu(bau_control, tcpu);
728 tbcp->baudisabled = 0;
729 tbcp->period_requests = 0;
730 tbcp->period_time = 0;
738 * Each sending cpu has a per-cpu mask which it fills from the caller's
739 * cpu mask. All cpus are converted to uvhubs and copied to the
740 * activation descriptor.
742 flush_mask = (struct cpumask *)per_cpu(uv_flush_tlb_mask, cpu);
743 /* don't actually do a shootdown of the local cpu */
744 cpumask_andnot(flush_mask, cpumask, cpumask_of(cpu));
745 if (cpu_isset(cpu, *cpumask))
748 bau_desc = bcp->descriptor_base;
749 bau_desc += UV_ITEMS_PER_DESCRIPTOR * bcp->uvhub_cpu;
750 bau_uvhubs_clear(&bau_desc->distribution, UV_DISTRIBUTION_SIZE);
753 for_each_cpu(tcpu, flush_mask) {
754 uvhub = uv_cpu_to_blade_id(tcpu);
755 bau_uvhub_set(uvhub, &bau_desc->distribution);
756 if (uvhub == bcp->uvhub)
761 if ((locals + remotes) == 0)
764 stat->s_ntargcpu += remotes + locals;
765 stat->s_ntargremotes += remotes;
766 stat->s_ntarglocals += locals;
767 remotes = bau_uvhub_weight(&bau_desc->distribution);
769 /* uvhub statistics */
770 hubs = bau_uvhub_weight(&bau_desc->distribution);
772 stat->s_ntarglocaluvhub++;
773 stat->s_ntargremoteuvhub += (hubs - 1);
775 stat->s_ntargremoteuvhub += hubs;
776 stat->s_ntarguvhub += hubs;
778 stat->s_ntarguvhub16++;
780 stat->s_ntarguvhub8++;
782 stat->s_ntarguvhub4++;
784 stat->s_ntarguvhub2++;
786 stat->s_ntarguvhub1++;
788 bau_desc->payload.address = va;
789 bau_desc->payload.sending_cpu = cpu;
792 * uv_flush_send_and_wait returns 0 if all cpu's were messaged,
793 * or 1 if it gave up and the original cpumask should be returned.
795 if (!uv_flush_send_and_wait(bau_desc, flush_mask, bcp))
802 * The BAU message interrupt comes here. (registered by set_intr_gate)
805 * We received a broadcast assist message.
807 * Interrupts are disabled; this interrupt could represent
808 * the receipt of several messages.
810 * All cores/threads on this hub get this interrupt.
811 * The last one to see it does the software ack.
812 * (the resource will not be freed until noninterruptable cpus see this
813 * interrupt; hardware may timeout the s/w ack and reply ERROR)
815 void uv_bau_message_interrupt(struct pt_regs *regs)
819 struct bau_payload_queue_entry *msg;
820 struct bau_control *bcp;
821 struct ptc_stats *stat;
822 struct msg_desc msgdesc;
824 time_start = get_cycles();
825 bcp = &per_cpu(bau_control, smp_processor_id());
827 msgdesc.va_queue_first = bcp->va_queue_first;
828 msgdesc.va_queue_last = bcp->va_queue_last;
829 msg = bcp->bau_msg_head;
830 while (msg->sw_ack_vector) {
832 msgdesc.msg_slot = msg - msgdesc.va_queue_first;
833 msgdesc.sw_ack_slot = ffs(msg->sw_ack_vector) - 1;
835 uv_bau_process_message(&msgdesc, bcp);
837 if (msg > msgdesc.va_queue_last)
838 msg = msgdesc.va_queue_first;
839 bcp->bau_msg_head = msg;
841 stat->d_time += (get_cycles() - time_start);
852 * Each target uvhub (i.e. a uvhub that has no cpu's) needs to have
853 * shootdown message timeouts enabled. The timeout does not cause
854 * an interrupt, but causes an error message to be returned to
857 static void uv_enable_timeouts(void)
862 unsigned long mmr_image;
864 nuvhubs = uv_num_possible_blades();
866 for (uvhub = 0; uvhub < nuvhubs; uvhub++) {
867 if (!uv_blade_nr_possible_cpus(uvhub))
870 pnode = uv_blade_to_pnode(uvhub);
872 uv_read_global_mmr64(pnode, UVH_LB_BAU_MISC_CONTROL);
874 * Set the timeout period and then lock it in, in three
875 * steps; captures and locks in the period.
877 * To program the period, the SOFT_ACK_MODE must be off.
879 mmr_image &= ~((unsigned long)1 <<
880 UVH_LB_BAU_MISC_CONTROL_ENABLE_INTD_SOFT_ACK_MODE_SHFT);
881 uv_write_global_mmr64
882 (pnode, UVH_LB_BAU_MISC_CONTROL, mmr_image);
884 * Set the 4-bit period.
886 mmr_image &= ~((unsigned long)0xf <<
887 UVH_LB_BAU_MISC_CONTROL_INTD_SOFT_ACK_TIMEOUT_PERIOD_SHFT);
888 mmr_image |= (UV_INTD_SOFT_ACK_TIMEOUT_PERIOD <<
889 UVH_LB_BAU_MISC_CONTROL_INTD_SOFT_ACK_TIMEOUT_PERIOD_SHFT);
890 uv_write_global_mmr64
891 (pnode, UVH_LB_BAU_MISC_CONTROL, mmr_image);
893 * Subsequent reversals of the timebase bit (3) cause an
894 * immediate timeout of one or all INTD resources as
895 * indicated in bits 2:0 (7 causes all of them to timeout).
897 mmr_image |= ((unsigned long)1 <<
898 UVH_LB_BAU_MISC_CONTROL_ENABLE_INTD_SOFT_ACK_MODE_SHFT);
899 uv_write_global_mmr64
900 (pnode, UVH_LB_BAU_MISC_CONTROL, mmr_image);
904 static void *uv_ptc_seq_start(struct seq_file *file, loff_t *offset)
906 if (*offset < num_possible_cpus())
911 static void *uv_ptc_seq_next(struct seq_file *file, void *data, loff_t *offset)
914 if (*offset < num_possible_cpus())
919 static void uv_ptc_seq_stop(struct seq_file *file, void *data)
923 static inline unsigned long long
924 microsec_2_cycles(unsigned long microsec)
927 unsigned long long cyc;
929 ns = microsec * 1000;
930 cyc = (ns << CYC2NS_SCALE_FACTOR)/(per_cpu(cyc2ns, smp_processor_id()));
935 * Display the statistics thru /proc.
936 * 'data' points to the cpu number
938 static int uv_ptc_seq_show(struct seq_file *file, void *data)
940 struct ptc_stats *stat;
943 cpu = *(loff_t *)data;
947 "# cpu sent stime self locals remotes ncpus localhub ");
949 "remotehub numuvhubs numuvhubs16 numuvhubs8 ");
951 "numuvhubs4 numuvhubs2 numuvhubs1 dto ");
953 "retries rok resetp resett giveup sto bz throt ");
955 "sw_ack recv rtime all ");
957 "one mult none retry canc nocan reset rcan ");
961 if (cpu < num_possible_cpus() && cpu_online(cpu)) {
962 stat = &per_cpu(ptcstats, cpu);
963 /* source side statistics */
965 "cpu %d %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld ",
966 cpu, stat->s_requestor, cycles_2_us(stat->s_time),
967 stat->s_ntargself, stat->s_ntarglocals,
968 stat->s_ntargremotes, stat->s_ntargcpu,
969 stat->s_ntarglocaluvhub, stat->s_ntargremoteuvhub,
970 stat->s_ntarguvhub, stat->s_ntarguvhub16);
971 seq_printf(file, "%ld %ld %ld %ld %ld ",
972 stat->s_ntarguvhub8, stat->s_ntarguvhub4,
973 stat->s_ntarguvhub2, stat->s_ntarguvhub1,
975 seq_printf(file, "%ld %ld %ld %ld %ld %ld %ld %ld ",
976 stat->s_retry_messages, stat->s_retriesok,
977 stat->s_resets_plug, stat->s_resets_timeout,
978 stat->s_giveup, stat->s_stimeout,
979 stat->s_busy, stat->s_throttles);
981 /* destination side statistics */
983 "%lx %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld ",
984 uv_read_global_mmr64(uv_cpu_to_pnode(cpu),
985 UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE),
986 stat->d_requestee, cycles_2_us(stat->d_time),
987 stat->d_alltlb, stat->d_onetlb, stat->d_multmsg,
988 stat->d_nomsg, stat->d_retries, stat->d_canceled,
989 stat->d_nocanceled, stat->d_resets,
991 seq_printf(file, "%ld %ld\n",
992 stat->s_bau_disabled, stat->s_bau_reenabled);
999 * Display the tunables thru debugfs
1001 static ssize_t tunables_read(struct file *file, char __user *userbuf,
1002 size_t count, loff_t *ppos)
1007 buf = kasprintf(GFP_KERNEL, "%s %s %s\n%d %d %d %d %d %d %d %d %d\n",
1008 "max_bau_concurrent plugged_delay plugsb4reset",
1009 "timeoutsb4reset ipi_reset_limit complete_threshold",
1010 "congested_response_us congested_reps congested_period",
1011 max_bau_concurrent, plugged_delay, plugsb4reset,
1012 timeoutsb4reset, ipi_reset_limit, complete_threshold,
1013 congested_response_us, congested_reps, congested_period);
1018 ret = simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
1024 * -1: resetf the statistics
1025 * 0: display meaning of the statistics
1027 static ssize_t uv_ptc_proc_write(struct file *file, const char __user *user,
1028 size_t count, loff_t *data)
1033 struct ptc_stats *stat;
1035 if (count == 0 || count > sizeof(optstr))
1037 if (copy_from_user(optstr, user, count))
1039 optstr[count - 1] = '\0';
1040 if (strict_strtol(optstr, 10, &input_arg) < 0) {
1041 printk(KERN_DEBUG "%s is invalid\n", optstr);
1045 if (input_arg == 0) {
1046 printk(KERN_DEBUG "# cpu: cpu number\n");
1047 printk(KERN_DEBUG "Sender statistics:\n");
1049 "sent: number of shootdown messages sent\n");
1051 "stime: time spent sending messages\n");
1053 "numuvhubs: number of hubs targeted with shootdown\n");
1055 "numuvhubs16: number times 16 or more hubs targeted\n");
1057 "numuvhubs8: number times 8 or more hubs targeted\n");
1059 "numuvhubs4: number times 4 or more hubs targeted\n");
1061 "numuvhubs2: number times 2 or more hubs targeted\n");
1063 "numuvhubs1: number times 1 hub targeted\n");
1065 "numcpus: number of cpus targeted with shootdown\n");
1067 "dto: number of destination timeouts\n");
1069 "retries: destination timeout retries sent\n");
1071 "rok: : destination timeouts successfully retried\n");
1073 "resetp: ipi-style resource resets for plugs\n");
1075 "resett: ipi-style resource resets for timeouts\n");
1077 "giveup: fall-backs to ipi-style shootdowns\n");
1079 "sto: number of source timeouts\n");
1081 "bz: number of stay-busy's\n");
1083 "throt: number times spun in throttle\n");
1084 printk(KERN_DEBUG "Destination side statistics:\n");
1086 "sw_ack: image of UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE\n");
1088 "recv: shootdown messages received\n");
1090 "rtime: time spent processing messages\n");
1092 "all: shootdown all-tlb messages\n");
1094 "one: shootdown one-tlb messages\n");
1096 "mult: interrupts that found multiple messages\n");
1098 "none: interrupts that found no messages\n");
1100 "retry: number of retry messages processed\n");
1102 "canc: number messages canceled by retries\n");
1104 "nocan: number retries that found nothing to cancel\n");
1106 "reset: number of ipi-style reset requests processed\n");
1108 "rcan: number messages canceled by reset requests\n");
1110 "disable: number times use of the BAU was disabled\n");
1112 "enable: number times use of the BAU was re-enabled\n");
1113 } else if (input_arg == -1) {
1114 for_each_present_cpu(cpu) {
1115 stat = &per_cpu(ptcstats, cpu);
1116 memset(stat, 0, sizeof(struct ptc_stats));
1123 static int local_atoi(const char *name)
1130 val = 10*val+(*name-'0');
1140 * 0 values reset them to defaults
1142 static ssize_t tunables_write(struct file *file, const char __user *user,
1143 size_t count, loff_t *data)
1151 struct bau_control *bcp;
1153 if (count == 0 || count > sizeof(instr)-1)
1155 if (copy_from_user(instr, user, count))
1158 instr[count] = '\0';
1159 /* count the fields */
1160 p = instr + strspn(instr, WHITESPACE);
1162 for (; *p; p = q + strspn(q, WHITESPACE)) {
1163 q = p + strcspn(p, WHITESPACE);
1169 printk(KERN_INFO "bau tunable error: should be 9 numbers\n");
1173 p = instr + strspn(instr, WHITESPACE);
1175 for (cnt = 0; *p; p = q + strspn(q, WHITESPACE), cnt++) {
1176 q = p + strcspn(p, WHITESPACE);
1177 val = local_atoi(p);
1181 max_bau_concurrent = MAX_BAU_CONCURRENT;
1182 max_bau_concurrent_constant =
1186 bcp = &per_cpu(bau_control, smp_processor_id());
1187 if (val < 1 || val > bcp->cpus_in_uvhub) {
1189 "Error: BAU max concurrent %d is invalid\n",
1193 max_bau_concurrent = val;
1194 max_bau_concurrent_constant = val;
1198 plugged_delay = PLUGGED_DELAY;
1200 plugged_delay = val;
1204 plugsb4reset = PLUGSB4RESET;
1210 timeoutsb4reset = TIMEOUTSB4RESET;
1212 timeoutsb4reset = val;
1216 ipi_reset_limit = IPI_RESET_LIMIT;
1218 ipi_reset_limit = val;
1222 complete_threshold = COMPLETE_THRESHOLD;
1224 complete_threshold = val;
1228 congested_response_us = CONGESTED_RESPONSE_US;
1230 congested_response_us = val;
1234 congested_reps = CONGESTED_REPS;
1236 congested_reps = val;
1240 congested_period = CONGESTED_PERIOD;
1242 congested_period = val;
1248 for_each_present_cpu(cpu) {
1249 bcp = &per_cpu(bau_control, cpu);
1250 bcp->max_bau_concurrent = max_bau_concurrent;
1251 bcp->max_bau_concurrent_constant = max_bau_concurrent;
1252 bcp->plugged_delay = plugged_delay;
1253 bcp->plugsb4reset = plugsb4reset;
1254 bcp->timeoutsb4reset = timeoutsb4reset;
1255 bcp->ipi_reset_limit = ipi_reset_limit;
1256 bcp->complete_threshold = complete_threshold;
1257 bcp->congested_response_us = congested_response_us;
1258 bcp->congested_reps = congested_reps;
1259 bcp->congested_period = congested_period;
1264 static const struct seq_operations uv_ptc_seq_ops = {
1265 .start = uv_ptc_seq_start,
1266 .next = uv_ptc_seq_next,
1267 .stop = uv_ptc_seq_stop,
1268 .show = uv_ptc_seq_show
1271 static int uv_ptc_proc_open(struct inode *inode, struct file *file)
1273 return seq_open(file, &uv_ptc_seq_ops);
1276 static int tunables_open(struct inode *inode, struct file *file)
1281 static const struct file_operations proc_uv_ptc_operations = {
1282 .open = uv_ptc_proc_open,
1284 .write = uv_ptc_proc_write,
1285 .llseek = seq_lseek,
1286 .release = seq_release,
1289 static const struct file_operations tunables_fops = {
1290 .open = tunables_open,
1291 .read = tunables_read,
1292 .write = tunables_write,
1293 .llseek = default_llseek,
1296 static int __init uv_ptc_init(void)
1298 struct proc_dir_entry *proc_uv_ptc;
1300 if (!is_uv_system())
1303 proc_uv_ptc = proc_create(UV_PTC_BASENAME, 0444, NULL,
1304 &proc_uv_ptc_operations);
1306 printk(KERN_ERR "unable to create %s proc entry\n",
1311 tunables_dir = debugfs_create_dir(UV_BAU_TUNABLES_DIR, NULL);
1312 if (!tunables_dir) {
1313 printk(KERN_ERR "unable to create debugfs directory %s\n",
1314 UV_BAU_TUNABLES_DIR);
1317 tunables_file = debugfs_create_file(UV_BAU_TUNABLES_FILE, 0600,
1318 tunables_dir, NULL, &tunables_fops);
1319 if (!tunables_file) {
1320 printk(KERN_ERR "unable to create debugfs file %s\n",
1321 UV_BAU_TUNABLES_FILE);
1328 * initialize the sending side's sending buffers
1331 uv_activation_descriptor_init(int node, int pnode)
1338 struct bau_desc *bau_desc;
1339 struct bau_desc *bd2;
1340 struct bau_control *bcp;
1343 * each bau_desc is 64 bytes; there are 8 (UV_ITEMS_PER_DESCRIPTOR)
1344 * per cpu; and up to 32 (UV_ADP_SIZE) cpu's per uvhub
1346 bau_desc = (struct bau_desc *)kmalloc_node(sizeof(struct bau_desc)*
1347 UV_ADP_SIZE*UV_ITEMS_PER_DESCRIPTOR, GFP_KERNEL, node);
1350 pa = uv_gpa(bau_desc); /* need the real nasid*/
1351 n = pa >> uv_nshift;
1354 uv_write_global_mmr64(pnode, UVH_LB_BAU_SB_DESCRIPTOR_BASE,
1355 (n << UV_DESC_BASE_PNODE_SHIFT | m));
1358 * initializing all 8 (UV_ITEMS_PER_DESCRIPTOR) descriptors for each
1359 * cpu even though we only use the first one; one descriptor can
1360 * describe a broadcast to 256 uv hubs.
1362 for (i = 0, bd2 = bau_desc; i < (UV_ADP_SIZE*UV_ITEMS_PER_DESCRIPTOR);
1364 memset(bd2, 0, sizeof(struct bau_desc));
1365 bd2->header.sw_ack_flag = 1;
1367 * base_dest_nodeid is the nasid (pnode<<1) of the first uvhub
1368 * in the partition. The bit map will indicate uvhub numbers,
1369 * which are 0-N in a partition. Pnodes are unique system-wide.
1371 bd2->header.base_dest_nodeid = uv_partition_base_pnode << 1;
1372 bd2->header.dest_subnodeid = 0x10; /* the LB */
1373 bd2->header.command = UV_NET_ENDPOINT_INTD;
1374 bd2->header.int_both = 1;
1376 * all others need to be set to zero:
1377 * fairness chaining multilevel count replied_to
1380 for_each_present_cpu(cpu) {
1381 if (pnode != uv_blade_to_pnode(uv_cpu_to_blade_id(cpu)))
1383 bcp = &per_cpu(bau_control, cpu);
1384 bcp->descriptor_base = bau_desc;
1389 * initialize the destination side's receiving buffers
1390 * entered for each uvhub in the partition
1391 * - node is first node (kernel memory notion) on the uvhub
1392 * - pnode is the uvhub's physical identifier
1395 uv_payload_queue_init(int node, int pnode)
1401 struct bau_payload_queue_entry *pqp;
1402 struct bau_payload_queue_entry *pqp_malloc;
1403 struct bau_control *bcp;
1405 pqp = (struct bau_payload_queue_entry *) kmalloc_node(
1406 (DEST_Q_SIZE + 1) * sizeof(struct bau_payload_queue_entry),
1411 cp = (char *)pqp + 31;
1412 pqp = (struct bau_payload_queue_entry *)(((unsigned long)cp >> 5) << 5);
1414 for_each_present_cpu(cpu) {
1415 if (pnode != uv_cpu_to_pnode(cpu))
1417 /* for every cpu on this pnode: */
1418 bcp = &per_cpu(bau_control, cpu);
1419 bcp->va_queue_first = pqp;
1420 bcp->bau_msg_head = pqp;
1421 bcp->va_queue_last = pqp + (DEST_Q_SIZE - 1);
1424 * need the pnode of where the memory was really allocated
1427 pn = pa >> uv_nshift;
1428 uv_write_global_mmr64(pnode,
1429 UVH_LB_BAU_INTD_PAYLOAD_QUEUE_FIRST,
1430 ((unsigned long)pn << UV_PAYLOADQ_PNODE_SHIFT) |
1431 uv_physnodeaddr(pqp));
1432 uv_write_global_mmr64(pnode, UVH_LB_BAU_INTD_PAYLOAD_QUEUE_TAIL,
1433 uv_physnodeaddr(pqp));
1434 uv_write_global_mmr64(pnode, UVH_LB_BAU_INTD_PAYLOAD_QUEUE_LAST,
1436 uv_physnodeaddr(pqp + (DEST_Q_SIZE - 1)));
1437 /* in effect, all msg_type's are set to MSG_NOOP */
1438 memset(pqp, 0, sizeof(struct bau_payload_queue_entry) * DEST_Q_SIZE);
1442 * Initialization of each UV hub's structures
1444 static void __init uv_init_uvhub(int uvhub, int vector)
1448 unsigned long apicid;
1450 node = uvhub_to_first_node(uvhub);
1451 pnode = uv_blade_to_pnode(uvhub);
1452 uv_activation_descriptor_init(node, pnode);
1453 uv_payload_queue_init(node, pnode);
1455 * the below initialization can't be in firmware because the
1456 * messaging IRQ will be determined by the OS
1458 apicid = uvhub_to_first_apicid(uvhub);
1459 uv_write_global_mmr64(pnode, UVH_BAU_DATA_CONFIG,
1460 ((apicid << 32) | vector));
1464 * We will set BAU_MISC_CONTROL with a timeout period.
1465 * But the BIOS has set UVH_AGING_PRESCALE_SEL and UVH_TRANSACTION_TIMEOUT.
1466 * So the destination timeout period has be be calculated from them.
1469 calculate_destination_timeout(void)
1471 unsigned long mmr_image;
1477 unsigned long ts_ns;
1479 mult1 = UV_INTD_SOFT_ACK_TIMEOUT_PERIOD & BAU_MISC_CONTROL_MULT_MASK;
1480 mmr_image = uv_read_local_mmr(UVH_AGING_PRESCALE_SEL);
1481 index = (mmr_image >> BAU_URGENCY_7_SHIFT) & BAU_URGENCY_7_MASK;
1482 mmr_image = uv_read_local_mmr(UVH_TRANSACTION_TIMEOUT);
1483 mult2 = (mmr_image >> BAU_TRANS_SHIFT) & BAU_TRANS_MASK;
1484 base = timeout_base_ns[index];
1485 ts_ns = base * mult1 * mult2;
1491 * initialize the bau_control structure for each cpu
1493 static void __init uv_init_per_cpu(int nuvhubs)
1501 unsigned short socket_mask;
1502 unsigned char *uvhub_mask;
1503 struct bau_control *bcp;
1504 struct uvhub_desc *bdp;
1505 struct socket_desc *sdp;
1506 struct bau_control *hmaster = NULL;
1507 struct bau_control *smaster = NULL;
1508 struct socket_desc {
1510 short cpu_number[16];
1513 unsigned short socket_mask;
1517 struct socket_desc socket[2];
1519 struct uvhub_desc *uvhub_descs;
1521 timeout_us = calculate_destination_timeout();
1523 uvhub_descs = (struct uvhub_desc *)
1524 kmalloc(nuvhubs * sizeof(struct uvhub_desc), GFP_KERNEL);
1525 memset(uvhub_descs, 0, nuvhubs * sizeof(struct uvhub_desc));
1526 uvhub_mask = kzalloc((nuvhubs+7)/8, GFP_KERNEL);
1527 for_each_present_cpu(cpu) {
1528 bcp = &per_cpu(bau_control, cpu);
1529 memset(bcp, 0, sizeof(struct bau_control));
1530 pnode = uv_cpu_hub_info(cpu)->pnode;
1531 uvhub = uv_cpu_hub_info(cpu)->numa_blade_id;
1532 *(uvhub_mask + (uvhub/8)) |= (1 << (uvhub%8));
1533 bdp = &uvhub_descs[uvhub];
1537 /* kludge: 'assuming' one node per socket, and assuming that
1538 disabling a socket just leaves a gap in node numbers */
1539 socket = (cpu_to_node(cpu) & 1);
1540 bdp->socket_mask |= (1 << socket);
1541 sdp = &bdp->socket[socket];
1542 sdp->cpu_number[sdp->num_cpus] = cpu;
1545 for (uvhub = 0; uvhub < nuvhubs; uvhub++) {
1546 if (!(*(uvhub_mask + (uvhub/8)) & (1 << (uvhub%8))))
1549 bdp = &uvhub_descs[uvhub];
1550 socket_mask = bdp->socket_mask;
1552 while (socket_mask) {
1553 if (!(socket_mask & 1))
1555 sdp = &bdp->socket[socket];
1556 for (i = 0; i < sdp->num_cpus; i++) {
1557 cpu = sdp->cpu_number[i];
1558 bcp = &per_cpu(bau_control, cpu);
1562 if (!have_hmaster) {
1567 bcp->cpus_in_uvhub = bdp->num_cpus;
1568 bcp->cpus_in_socket = sdp->num_cpus;
1569 bcp->socket_master = smaster;
1570 bcp->uvhub = bdp->uvhub;
1571 bcp->uvhub_master = hmaster;
1572 bcp->uvhub_cpu = uv_cpu_hub_info(cpu)->
1577 socket_mask = (socket_mask >> 1);
1582 for_each_present_cpu(cpu) {
1583 bcp = &per_cpu(bau_control, cpu);
1584 bcp->baudisabled = 0;
1585 bcp->statp = &per_cpu(ptcstats, cpu);
1586 /* time interval to catch a hardware stay-busy bug */
1587 bcp->timeout_interval = microsec_2_cycles(2*timeout_us);
1588 bcp->max_bau_concurrent = max_bau_concurrent;
1589 bcp->max_bau_concurrent_constant = max_bau_concurrent;
1590 bcp->plugged_delay = plugged_delay;
1591 bcp->plugsb4reset = plugsb4reset;
1592 bcp->timeoutsb4reset = timeoutsb4reset;
1593 bcp->ipi_reset_limit = ipi_reset_limit;
1594 bcp->complete_threshold = complete_threshold;
1595 bcp->congested_response_us = congested_response_us;
1596 bcp->congested_reps = congested_reps;
1597 bcp->congested_period = congested_period;
1602 * Initialization of BAU-related structures
1604 static int __init uv_bau_init(void)
1613 if (!is_uv_system())
1619 for_each_possible_cpu(cur_cpu)
1620 zalloc_cpumask_var_node(&per_cpu(uv_flush_tlb_mask, cur_cpu),
1621 GFP_KERNEL, cpu_to_node(cur_cpu));
1623 uv_nshift = uv_hub_info->m_val;
1624 uv_mmask = (1UL << uv_hub_info->m_val) - 1;
1625 nuvhubs = uv_num_possible_blades();
1626 spin_lock_init(&disable_lock);
1627 congested_cycles = microsec_2_cycles(congested_response_us);
1629 uv_init_per_cpu(nuvhubs);
1631 uv_partition_base_pnode = 0x7fffffff;
1632 for (uvhub = 0; uvhub < nuvhubs; uvhub++)
1633 if (uv_blade_nr_possible_cpus(uvhub) &&
1634 (uv_blade_to_pnode(uvhub) < uv_partition_base_pnode))
1635 uv_partition_base_pnode = uv_blade_to_pnode(uvhub);
1637 vector = UV_BAU_MESSAGE;
1638 for_each_possible_blade(uvhub)
1639 if (uv_blade_nr_possible_cpus(uvhub))
1640 uv_init_uvhub(uvhub, vector);
1642 uv_enable_timeouts();
1643 alloc_intr_gate(vector, uv_bau_message_intr1);
1645 for_each_possible_blade(uvhub) {
1646 if (uv_blade_nr_possible_cpus(uvhub)) {
1647 pnode = uv_blade_to_pnode(uvhub);
1649 uv_write_global_mmr64(pnode,
1650 UVH_LB_BAU_SB_ACTIVATION_CONTROL,
1651 ((unsigned long)1 << 63));
1652 mmr = 1; /* should be 1 to broadcast to both sockets */
1653 uv_write_global_mmr64(pnode, UVH_BAU_DATA_BROADCAST,
1660 core_initcall(uv_bau_init);
1661 fs_initcall(uv_ptc_init);