2 * SGI UltraViolet TLB flush routines.
4 * (c) 2008-2014 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>
14 #include <linux/delay.h>
16 #include <asm/mmu_context.h>
17 #include <asm/uv/uv.h>
18 #include <asm/uv/uv_mmrs.h>
19 #include <asm/uv/uv_hub.h>
20 #include <asm/uv/uv_bau.h>
24 #include <asm/irq_vectors.h>
25 #include <asm/timer.h>
27 /* timeouts in nanoseconds (indexed by UVH_AGING_PRESCALE_SEL urgency7 30:28) */
28 static int timeout_base_ns[] = {
39 static int timeout_us;
40 static bool nobau = true;
41 static int nobau_perm;
42 static cycles_t congested_cycles;
45 static int max_concurr = MAX_BAU_CONCURRENT;
46 static int max_concurr_const = MAX_BAU_CONCURRENT;
47 static int plugged_delay = PLUGGED_DELAY;
48 static int plugsb4reset = PLUGSB4RESET;
49 static int giveup_limit = GIVEUP_LIMIT;
50 static int timeoutsb4reset = TIMEOUTSB4RESET;
51 static int ipi_reset_limit = IPI_RESET_LIMIT;
52 static int complete_threshold = COMPLETE_THRESHOLD;
53 static int congested_respns_us = CONGESTED_RESPONSE_US;
54 static int congested_reps = CONGESTED_REPS;
55 static int disabled_period = DISABLED_PERIOD;
57 static struct tunables tunables[] = {
58 {&max_concurr, MAX_BAU_CONCURRENT}, /* must be [0] */
59 {&plugged_delay, PLUGGED_DELAY},
60 {&plugsb4reset, PLUGSB4RESET},
61 {&timeoutsb4reset, TIMEOUTSB4RESET},
62 {&ipi_reset_limit, IPI_RESET_LIMIT},
63 {&complete_threshold, COMPLETE_THRESHOLD},
64 {&congested_respns_us, CONGESTED_RESPONSE_US},
65 {&congested_reps, CONGESTED_REPS},
66 {&disabled_period, DISABLED_PERIOD},
67 {&giveup_limit, GIVEUP_LIMIT}
70 static struct dentry *tunables_dir;
71 static struct dentry *tunables_file;
73 /* these correspond to the statistics printed by ptc_seq_show() */
74 static char *stat_description[] = {
75 "sent: number of shootdown messages sent",
76 "stime: time spent sending messages",
77 "numuvhubs: number of hubs targeted with shootdown",
78 "numuvhubs16: number times 16 or more hubs targeted",
79 "numuvhubs8: number times 8 or more hubs targeted",
80 "numuvhubs4: number times 4 or more hubs targeted",
81 "numuvhubs2: number times 2 or more hubs targeted",
82 "numuvhubs1: number times 1 hub targeted",
83 "numcpus: number of cpus targeted with shootdown",
84 "dto: number of destination timeouts",
85 "retries: destination timeout retries sent",
86 "rok: : destination timeouts successfully retried",
87 "resetp: ipi-style resource resets for plugs",
88 "resett: ipi-style resource resets for timeouts",
89 "giveup: fall-backs to ipi-style shootdowns",
90 "sto: number of source timeouts",
91 "bz: number of stay-busy's",
92 "throt: number times spun in throttle",
93 "swack: image of UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE",
94 "recv: shootdown messages received",
95 "rtime: time spent processing messages",
96 "all: shootdown all-tlb messages",
97 "one: shootdown one-tlb messages",
98 "mult: interrupts that found multiple messages",
99 "none: interrupts that found no messages",
100 "retry: number of retry messages processed",
101 "canc: number messages canceled by retries",
102 "nocan: number retries that found nothing to cancel",
103 "reset: number of ipi-style reset requests processed",
104 "rcan: number messages canceled by reset requests",
105 "disable: number times use of the BAU was disabled",
106 "enable: number times use of the BAU was re-enabled"
109 static int __init setup_bau(char *arg)
116 result = strtobool(arg, &nobau);
120 /* we need to flip the logic here, so that bau=y sets nobau to false */
124 pr_info("UV BAU Enabled\n");
126 pr_info("UV BAU Disabled\n");
130 early_param("bau", setup_bau);
132 /* base pnode in this partition */
133 static int uv_base_pnode __read_mostly;
135 static DEFINE_PER_CPU(struct ptc_stats, ptcstats);
136 static DEFINE_PER_CPU(struct bau_control, bau_control);
137 static DEFINE_PER_CPU(cpumask_var_t, uv_flush_tlb_mask);
143 struct bau_control *bcp;
146 pr_info("BAU not initialized; cannot be turned on\n");
150 for_each_present_cpu(cpu) {
151 bcp = &per_cpu(bau_control, cpu);
154 pr_info("BAU turned on\n");
162 struct bau_control *bcp;
165 for_each_present_cpu(cpu) {
166 bcp = &per_cpu(bau_control, cpu);
169 pr_info("BAU turned off\n");
174 * Determine the first node on a uvhub. 'Nodes' are used for kernel
177 static int __init uvhub_to_first_node(int uvhub)
181 for_each_online_node(node) {
182 b = uv_node_to_blade_id(node);
190 * Determine the apicid of the first cpu on a uvhub.
192 static int __init uvhub_to_first_apicid(int uvhub)
196 for_each_present_cpu(cpu)
197 if (uvhub == uv_cpu_to_blade_id(cpu))
198 return per_cpu(x86_cpu_to_apicid, cpu);
203 * Free a software acknowledge hardware resource by clearing its Pending
204 * bit. This will return a reply to the sender.
205 * If the message has timed out, a reply has already been sent by the
206 * hardware but the resource has not been released. In that case our
207 * clear of the Timeout bit (as well) will free the resource. No reply will
208 * be sent (the hardware will only do one reply per message).
210 static void reply_to_message(struct msg_desc *mdp, struct bau_control *bcp,
214 struct bau_pq_entry *msg;
217 if (!msg->canceled && do_acknowledge) {
218 dw = (msg->swack_vec << UV_SW_ACK_NPENDING) | msg->swack_vec;
219 write_mmr_sw_ack(dw);
226 * Process the receipt of a RETRY message
228 static void bau_process_retry_msg(struct msg_desc *mdp,
229 struct bau_control *bcp)
232 int cancel_count = 0;
233 unsigned long msg_res;
234 unsigned long mmr = 0;
235 struct bau_pq_entry *msg = mdp->msg;
236 struct bau_pq_entry *msg2;
237 struct ptc_stats *stat = bcp->statp;
241 * cancel any message from msg+1 to the retry itself
243 for (msg2 = msg+1, i = 0; i < DEST_Q_SIZE; msg2++, i++) {
244 if (msg2 > mdp->queue_last)
245 msg2 = mdp->queue_first;
249 /* same conditions for cancellation as do_reset */
250 if ((msg2->replied_to == 0) && (msg2->canceled == 0) &&
251 (msg2->swack_vec) && ((msg2->swack_vec &
252 msg->swack_vec) == 0) &&
253 (msg2->sending_cpu == msg->sending_cpu) &&
254 (msg2->msg_type != MSG_NOOP)) {
255 mmr = read_mmr_sw_ack();
256 msg_res = msg2->swack_vec;
258 * This is a message retry; clear the resources held
259 * by the previous message only if they timed out.
260 * If it has not timed out we have an unexpected
261 * situation to report.
263 if (mmr & (msg_res << UV_SW_ACK_NPENDING)) {
266 * Is the resource timed out?
267 * Make everyone ignore the cancelled message.
272 mr = (msg_res << UV_SW_ACK_NPENDING) | msg_res;
273 write_mmr_sw_ack(mr);
278 stat->d_nocanceled++;
282 * Do all the things a cpu should do for a TLB shootdown message.
283 * Other cpu's may come here at the same time for this message.
285 static void bau_process_message(struct msg_desc *mdp, struct bau_control *bcp,
288 short socket_ack_count = 0;
290 struct atomic_short *asp;
291 struct ptc_stats *stat = bcp->statp;
292 struct bau_pq_entry *msg = mdp->msg;
293 struct bau_control *smaster = bcp->socket_master;
296 * This must be a normal message, or retry of a normal message
298 if (msg->address == TLB_FLUSH_ALL) {
302 __flush_tlb_one(msg->address);
308 * One cpu on each uvhub has the additional job on a RETRY
309 * of releasing the resource held by the message that is
310 * being retried. That message is identified by sending
313 if (msg->msg_type == MSG_RETRY && bcp == bcp->uvhub_master)
314 bau_process_retry_msg(mdp, bcp);
317 * This is a swack message, so we have to reply to it.
318 * Count each responding cpu on the socket. This avoids
319 * pinging the count's cache line back and forth between
322 sp = &smaster->socket_acknowledge_count[mdp->msg_slot];
323 asp = (struct atomic_short *)sp;
324 socket_ack_count = atom_asr(1, asp);
325 if (socket_ack_count == bcp->cpus_in_socket) {
328 * Both sockets dump their completed count total into
329 * the message's count.
332 asp = (struct atomic_short *)&msg->acknowledge_count;
333 msg_ack_count = atom_asr(socket_ack_count, asp);
335 if (msg_ack_count == bcp->cpus_in_uvhub) {
337 * All cpus in uvhub saw it; reply
338 * (unless we are in the UV2 workaround)
340 reply_to_message(mdp, bcp, do_acknowledge);
348 * Determine the first cpu on a pnode.
350 static int pnode_to_first_cpu(int pnode, struct bau_control *smaster)
353 struct hub_and_pnode *hpp;
355 for_each_present_cpu(cpu) {
356 hpp = &smaster->thp[cpu];
357 if (pnode == hpp->pnode)
364 * Last resort when we get a large number of destination timeouts is
365 * to clear resources held by a given cpu.
366 * Do this with IPI so that all messages in the BAU message queue
367 * can be identified by their nonzero swack_vec field.
369 * This is entered for a single cpu on the uvhub.
370 * The sender want's this uvhub to free a specific message's
373 static void do_reset(void *ptr)
376 struct bau_control *bcp = &per_cpu(bau_control, smp_processor_id());
377 struct reset_args *rap = (struct reset_args *)ptr;
378 struct bau_pq_entry *msg;
379 struct ptc_stats *stat = bcp->statp;
383 * We're looking for the given sender, and
384 * will free its swack resource.
385 * If all cpu's finally responded after the timeout, its
386 * message 'replied_to' was set.
388 for (msg = bcp->queue_first, i = 0; i < DEST_Q_SIZE; msg++, i++) {
389 unsigned long msg_res;
390 /* do_reset: same conditions for cancellation as
391 bau_process_retry_msg() */
392 if ((msg->replied_to == 0) &&
393 (msg->canceled == 0) &&
394 (msg->sending_cpu == rap->sender) &&
396 (msg->msg_type != MSG_NOOP)) {
400 * make everyone else ignore this message
404 * only reset the resource if it is still pending
406 mmr = read_mmr_sw_ack();
407 msg_res = msg->swack_vec;
408 mr = (msg_res << UV_SW_ACK_NPENDING) | msg_res;
411 write_mmr_sw_ack(mr);
419 * Use IPI to get all target uvhubs to release resources held by
420 * a given sending cpu number.
422 static void reset_with_ipi(struct pnmask *distribution, struct bau_control *bcp)
427 int sender = bcp->cpu;
428 cpumask_t *mask = bcp->uvhub_master->cpumask;
429 struct bau_control *smaster = bcp->socket_master;
430 struct reset_args reset_args;
432 reset_args.sender = sender;
434 /* find a single cpu for each uvhub in this distribution mask */
435 maskbits = sizeof(struct pnmask) * BITSPERBYTE;
436 /* each bit is a pnode relative to the partition base pnode */
437 for (pnode = 0; pnode < maskbits; pnode++) {
439 if (!bau_uvhub_isset(pnode, distribution))
441 apnode = pnode + bcp->partition_base_pnode;
442 cpu = pnode_to_first_cpu(apnode, smaster);
443 cpumask_set_cpu(cpu, mask);
446 /* IPI all cpus; preemption is already disabled */
447 smp_call_function_many(mask, do_reset, (void *)&reset_args, 1);
452 * Not to be confused with cycles_2_ns() from tsc.c; this gives a relative
453 * number, not an absolute. It converts a duration in cycles to a duration in
456 static inline unsigned long long cycles_2_ns(unsigned long long cyc)
458 struct cyc2ns_data *data = cyc2ns_read_begin();
459 unsigned long long ns;
461 ns = mul_u64_u32_shr(cyc, data->cyc2ns_mul, data->cyc2ns_shift);
463 cyc2ns_read_end(data);
468 * The reverse of the above; converts a duration in ns to a duration in cycles.
470 static inline unsigned long long ns_2_cycles(unsigned long long ns)
472 struct cyc2ns_data *data = cyc2ns_read_begin();
473 unsigned long long cyc;
475 cyc = (ns << data->cyc2ns_shift) / data->cyc2ns_mul;
477 cyc2ns_read_end(data);
481 static inline unsigned long cycles_2_us(unsigned long long cyc)
483 return cycles_2_ns(cyc) / NSEC_PER_USEC;
486 static inline cycles_t sec_2_cycles(unsigned long sec)
488 return ns_2_cycles(sec * NSEC_PER_SEC);
491 static inline unsigned long long usec_2_cycles(unsigned long usec)
493 return ns_2_cycles(usec * NSEC_PER_USEC);
497 * wait for all cpus on this hub to finish their sends and go quiet
498 * leaves uvhub_quiesce set so that no new broadcasts are started by
499 * bau_flush_send_and_wait()
501 static inline void quiesce_local_uvhub(struct bau_control *hmaster)
503 atom_asr(1, (struct atomic_short *)&hmaster->uvhub_quiesce);
507 * mark this quiet-requestor as done
509 static inline void end_uvhub_quiesce(struct bau_control *hmaster)
511 atom_asr(-1, (struct atomic_short *)&hmaster->uvhub_quiesce);
514 static unsigned long uv1_read_status(unsigned long mmr_offset, int right_shift)
516 unsigned long descriptor_status;
518 descriptor_status = uv_read_local_mmr(mmr_offset);
519 descriptor_status >>= right_shift;
520 descriptor_status &= UV_ACT_STATUS_MASK;
521 return descriptor_status;
525 * Wait for completion of a broadcast software ack message
526 * return COMPLETE, RETRY(PLUGGED or TIMEOUT) or GIVEUP
528 static int uv1_wait_completion(struct bau_desc *bau_desc,
529 unsigned long mmr_offset, int right_shift,
530 struct bau_control *bcp, long try)
532 unsigned long descriptor_status;
534 struct ptc_stats *stat = bcp->statp;
536 descriptor_status = uv1_read_status(mmr_offset, right_shift);
537 /* spin on the status MMR, waiting for it to go idle */
538 while ((descriptor_status != DS_IDLE)) {
540 * Our software ack messages may be blocked because
541 * there are no swack resources available. As long
542 * as none of them has timed out hardware will NACK
543 * our message and its state will stay IDLE.
545 if (descriptor_status == DS_SOURCE_TIMEOUT) {
548 } else if (descriptor_status == DS_DESTINATION_TIMEOUT) {
553 * Our retries may be blocked by all destination
554 * swack resources being consumed, and a timeout
555 * pending. In that case hardware returns the
556 * ERROR that looks like a destination timeout.
558 if (cycles_2_us(ttm - bcp->send_message) < timeout_us) {
559 bcp->conseccompletes = 0;
560 return FLUSH_RETRY_PLUGGED;
563 bcp->conseccompletes = 0;
564 return FLUSH_RETRY_TIMEOUT;
567 * descriptor_status is still BUSY
571 descriptor_status = uv1_read_status(mmr_offset, right_shift);
573 bcp->conseccompletes++;
574 return FLUSH_COMPLETE;
578 * UV2 could have an extra bit of status in the ACTIVATION_STATUS_2 register.
579 * But not currently used.
581 static unsigned long uv2_3_read_status(unsigned long offset, int rshft, int desc)
583 unsigned long descriptor_status;
586 ((read_lmmr(offset) >> rshft) & UV_ACT_STATUS_MASK) << 1;
587 return descriptor_status;
591 * Return whether the status of the descriptor that is normally used for this
592 * cpu (the one indexed by its hub-relative cpu number) is busy.
593 * The status of the original 32 descriptors is always reflected in the 64
594 * bits of UVH_LB_BAU_SB_ACTIVATION_STATUS_0.
595 * The bit provided by the activation_status_2 register is irrelevant to
596 * the status if it is only being tested for busy or not busy.
598 int normal_busy(struct bau_control *bcp)
600 int cpu = bcp->uvhub_cpu;
604 mmr_offset = UVH_LB_BAU_SB_ACTIVATION_STATUS_0;
605 right_shift = cpu * UV_ACT_STATUS_SIZE;
606 return (((((read_lmmr(mmr_offset) >> right_shift) &
607 UV_ACT_STATUS_MASK)) << 1) == UV2H_DESC_BUSY);
611 * Entered when a bau descriptor has gone into a permanent busy wait because
613 * Workaround the bug.
615 int handle_uv2_busy(struct bau_control *bcp)
617 struct ptc_stats *stat = bcp->statp;
624 static int uv2_3_wait_completion(struct bau_desc *bau_desc,
625 unsigned long mmr_offset, int right_shift,
626 struct bau_control *bcp, long try)
628 unsigned long descriptor_stat;
630 int desc = bcp->uvhub_cpu;
632 struct ptc_stats *stat = bcp->statp;
634 descriptor_stat = uv2_3_read_status(mmr_offset, right_shift, desc);
636 /* spin on the status MMR, waiting for it to go idle */
637 while (descriptor_stat != UV2H_DESC_IDLE) {
638 if ((descriptor_stat == UV2H_DESC_SOURCE_TIMEOUT)) {
640 * A h/w bug on the destination side may
641 * have prevented the message being marked
642 * pending, thus it doesn't get replied to
643 * and gets continually nacked until it times
644 * out with a SOURCE_TIMEOUT.
648 } else if (descriptor_stat == UV2H_DESC_DEST_TIMEOUT) {
652 * Our retries may be blocked by all destination
653 * swack resources being consumed, and a timeout
654 * pending. In that case hardware returns the
655 * ERROR that looks like a destination timeout.
656 * Without using the extended status we have to
657 * deduce from the short time that this was a
660 if (cycles_2_us(ttm - bcp->send_message) < timeout_us) {
661 bcp->conseccompletes = 0;
663 /* FLUSH_RETRY_PLUGGED causes hang on boot */
667 bcp->conseccompletes = 0;
668 /* FLUSH_RETRY_TIMEOUT causes hang on boot */
672 if (busy_reps > 1000000) {
673 /* not to hammer on the clock */
676 if ((ttm - bcp->send_message) > bcp->timeout_interval)
677 return handle_uv2_busy(bcp);
680 * descriptor_stat is still BUSY
684 descriptor_stat = uv2_3_read_status(mmr_offset, right_shift, desc);
686 bcp->conseccompletes++;
687 return FLUSH_COMPLETE;
691 * There are 2 status registers; each and array[32] of 2 bits. Set up for
692 * which register to read and position in that register based on cpu in
695 static int wait_completion(struct bau_desc *bau_desc, struct bau_control *bcp, long try)
698 unsigned long mmr_offset;
699 int desc = bcp->uvhub_cpu;
701 if (desc < UV_CPUS_PER_AS) {
702 mmr_offset = UVH_LB_BAU_SB_ACTIVATION_STATUS_0;
703 right_shift = desc * UV_ACT_STATUS_SIZE;
705 mmr_offset = UVH_LB_BAU_SB_ACTIVATION_STATUS_1;
706 right_shift = ((desc - UV_CPUS_PER_AS) * UV_ACT_STATUS_SIZE);
709 if (bcp->uvhub_version == 1)
710 return uv1_wait_completion(bau_desc, mmr_offset, right_shift, bcp, try);
712 return uv2_3_wait_completion(bau_desc, mmr_offset, right_shift, bcp, try);
716 * Our retries are blocked by all destination sw ack resources being
717 * in use, and a timeout is pending. In that case hardware immediately
718 * returns the ERROR that looks like a destination timeout.
720 static void destination_plugged(struct bau_desc *bau_desc,
721 struct bau_control *bcp,
722 struct bau_control *hmaster, struct ptc_stats *stat)
724 udelay(bcp->plugged_delay);
725 bcp->plugged_tries++;
727 if (bcp->plugged_tries >= bcp->plugsb4reset) {
728 bcp->plugged_tries = 0;
730 quiesce_local_uvhub(hmaster);
732 spin_lock(&hmaster->queue_lock);
733 reset_with_ipi(&bau_desc->distribution, bcp);
734 spin_unlock(&hmaster->queue_lock);
736 end_uvhub_quiesce(hmaster);
739 stat->s_resets_plug++;
743 static void destination_timeout(struct bau_desc *bau_desc,
744 struct bau_control *bcp, struct bau_control *hmaster,
745 struct ptc_stats *stat)
747 hmaster->max_concurr = 1;
748 bcp->timeout_tries++;
749 if (bcp->timeout_tries >= bcp->timeoutsb4reset) {
750 bcp->timeout_tries = 0;
752 quiesce_local_uvhub(hmaster);
754 spin_lock(&hmaster->queue_lock);
755 reset_with_ipi(&bau_desc->distribution, bcp);
756 spin_unlock(&hmaster->queue_lock);
758 end_uvhub_quiesce(hmaster);
761 stat->s_resets_timeout++;
766 * Stop all cpus on a uvhub from using the BAU for a period of time.
767 * This is reversed by check_enable.
769 static void disable_for_period(struct bau_control *bcp, struct ptc_stats *stat)
772 struct bau_control *tbcp;
773 struct bau_control *hmaster;
776 hmaster = bcp->uvhub_master;
777 spin_lock(&hmaster->disable_lock);
778 if (!bcp->baudisabled) {
779 stat->s_bau_disabled++;
781 for_each_present_cpu(tcpu) {
782 tbcp = &per_cpu(bau_control, tcpu);
783 if (tbcp->uvhub_master == hmaster) {
784 tbcp->baudisabled = 1;
785 tbcp->set_bau_on_time =
786 tm1 + bcp->disabled_period;
790 spin_unlock(&hmaster->disable_lock);
793 static void count_max_concurr(int stat, struct bau_control *bcp,
794 struct bau_control *hmaster)
796 bcp->plugged_tries = 0;
797 bcp->timeout_tries = 0;
798 if (stat != FLUSH_COMPLETE)
800 if (bcp->conseccompletes <= bcp->complete_threshold)
802 if (hmaster->max_concurr >= hmaster->max_concurr_const)
804 hmaster->max_concurr++;
807 static void record_send_stats(cycles_t time1, cycles_t time2,
808 struct bau_control *bcp, struct ptc_stats *stat,
809 int completion_status, int try)
814 elapsed = time2 - time1;
815 stat->s_time += elapsed;
817 if ((completion_status == FLUSH_COMPLETE) && (try == 1)) {
818 bcp->period_requests++;
819 bcp->period_time += elapsed;
820 if ((elapsed > congested_cycles) &&
821 (bcp->period_requests > bcp->cong_reps) &&
822 ((bcp->period_time / bcp->period_requests) >
825 disable_for_period(bcp, stat);
831 if (completion_status == FLUSH_COMPLETE && try > 1)
833 else if (completion_status == FLUSH_GIVEUP) {
835 if (get_cycles() > bcp->period_end)
836 bcp->period_giveups = 0;
837 bcp->period_giveups++;
838 if (bcp->period_giveups == 1)
839 bcp->period_end = get_cycles() + bcp->disabled_period;
840 if (bcp->period_giveups > bcp->giveup_limit) {
841 disable_for_period(bcp, stat);
842 stat->s_giveuplimit++;
848 * Because of a uv1 hardware bug only a limited number of concurrent
849 * requests can be made.
851 static void uv1_throttle(struct bau_control *hmaster, struct ptc_stats *stat)
853 spinlock_t *lock = &hmaster->uvhub_lock;
856 v = &hmaster->active_descriptor_count;
857 if (!atomic_inc_unless_ge(lock, v, hmaster->max_concurr)) {
861 } while (!atomic_inc_unless_ge(lock, v, hmaster->max_concurr));
866 * Handle the completion status of a message send.
868 static void handle_cmplt(int completion_status, struct bau_desc *bau_desc,
869 struct bau_control *bcp, struct bau_control *hmaster,
870 struct ptc_stats *stat)
872 if (completion_status == FLUSH_RETRY_PLUGGED)
873 destination_plugged(bau_desc, bcp, hmaster, stat);
874 else if (completion_status == FLUSH_RETRY_TIMEOUT)
875 destination_timeout(bau_desc, bcp, hmaster, stat);
879 * Send a broadcast and wait for it to complete.
881 * The flush_mask contains the cpus the broadcast is to be sent to including
882 * cpus that are on the local uvhub.
884 * Returns 0 if all flushing represented in the mask was done.
885 * Returns 1 if it gives up entirely and the original cpu mask is to be
886 * returned to the kernel.
888 int uv_flush_send_and_wait(struct cpumask *flush_mask, struct bau_control *bcp,
889 struct bau_desc *bau_desc)
892 int completion_stat = 0;
898 struct ptc_stats *stat = bcp->statp;
899 struct bau_control *hmaster = bcp->uvhub_master;
900 struct uv1_bau_msg_header *uv1_hdr = NULL;
901 struct uv2_3_bau_msg_header *uv2_3_hdr = NULL;
903 if (bcp->uvhub_version == 1) {
905 uv1_throttle(hmaster, stat);
908 while (hmaster->uvhub_quiesce)
911 time1 = get_cycles();
913 uv1_hdr = &bau_desc->header.uv1_hdr;
916 uv2_3_hdr = &bau_desc->header.uv2_3_hdr;
921 uv1_hdr->msg_type = MSG_REGULAR;
923 uv2_3_hdr->msg_type = MSG_REGULAR;
924 seq_number = bcp->message_number++;
927 uv1_hdr->msg_type = MSG_RETRY;
929 uv2_3_hdr->msg_type = MSG_RETRY;
930 stat->s_retry_messages++;
934 uv1_hdr->sequence = seq_number;
936 uv2_3_hdr->sequence = seq_number;
937 index = (1UL << AS_PUSH_SHIFT) | bcp->uvhub_cpu;
938 bcp->send_message = get_cycles();
940 write_mmr_activation(index);
943 completion_stat = wait_completion(bau_desc, bcp, try);
945 handle_cmplt(completion_stat, bau_desc, bcp, hmaster, stat);
947 if (bcp->ipi_attempts >= bcp->ipi_reset_limit) {
948 bcp->ipi_attempts = 0;
949 stat->s_overipilimit++;
950 completion_stat = FLUSH_GIVEUP;
954 } while ((completion_stat == FLUSH_RETRY_PLUGGED) ||
955 (completion_stat == FLUSH_RETRY_TIMEOUT));
957 time2 = get_cycles();
959 count_max_concurr(completion_stat, bcp, hmaster);
961 while (hmaster->uvhub_quiesce)
964 atomic_dec(&hmaster->active_descriptor_count);
966 record_send_stats(time1, time2, bcp, stat, completion_stat, try);
968 if (completion_stat == FLUSH_GIVEUP)
969 /* FLUSH_GIVEUP will fall back to using IPI's for tlb flush */
975 * The BAU is disabled for this uvhub. When the disabled time period has
976 * expired re-enable it.
977 * Return 0 if it is re-enabled for all cpus on this uvhub.
979 static int check_enable(struct bau_control *bcp, struct ptc_stats *stat)
982 struct bau_control *tbcp;
983 struct bau_control *hmaster;
985 hmaster = bcp->uvhub_master;
986 spin_lock(&hmaster->disable_lock);
987 if (bcp->baudisabled && (get_cycles() >= bcp->set_bau_on_time)) {
988 stat->s_bau_reenabled++;
989 for_each_present_cpu(tcpu) {
990 tbcp = &per_cpu(bau_control, tcpu);
991 if (tbcp->uvhub_master == hmaster) {
992 tbcp->baudisabled = 0;
993 tbcp->period_requests = 0;
994 tbcp->period_time = 0;
995 tbcp->period_giveups = 0;
998 spin_unlock(&hmaster->disable_lock);
1001 spin_unlock(&hmaster->disable_lock);
1005 static void record_send_statistics(struct ptc_stats *stat, int locals, int hubs,
1006 int remotes, struct bau_desc *bau_desc)
1008 stat->s_requestor++;
1009 stat->s_ntargcpu += remotes + locals;
1010 stat->s_ntargremotes += remotes;
1011 stat->s_ntarglocals += locals;
1013 /* uvhub statistics */
1014 hubs = bau_uvhub_weight(&bau_desc->distribution);
1016 stat->s_ntarglocaluvhub++;
1017 stat->s_ntargremoteuvhub += (hubs - 1);
1019 stat->s_ntargremoteuvhub += hubs;
1021 stat->s_ntarguvhub += hubs;
1024 stat->s_ntarguvhub16++;
1026 stat->s_ntarguvhub8++;
1028 stat->s_ntarguvhub4++;
1030 stat->s_ntarguvhub2++;
1032 stat->s_ntarguvhub1++;
1036 * Translate a cpu mask to the uvhub distribution mask in the BAU
1037 * activation descriptor.
1039 static int set_distrib_bits(struct cpumask *flush_mask, struct bau_control *bcp,
1040 struct bau_desc *bau_desc, int *localsp, int *remotesp)
1045 struct hub_and_pnode *hpp;
1047 for_each_cpu(cpu, flush_mask) {
1049 * The distribution vector is a bit map of pnodes, relative
1050 * to the partition base pnode (and the partition base nasid
1052 * Translate cpu to pnode and hub using a local memory array.
1054 hpp = &bcp->socket_master->thp[cpu];
1055 pnode = hpp->pnode - bcp->partition_base_pnode;
1056 bau_uvhub_set(pnode, &bau_desc->distribution);
1058 if (hpp->uvhub == bcp->uvhub)
1069 * globally purge translation cache of a virtual address or all TLB's
1070 * @cpumask: mask of all cpu's in which the address is to be removed
1071 * @mm: mm_struct containing virtual address range
1072 * @start: start virtual address to be removed from TLB
1073 * @end: end virtual address to be remove from TLB
1074 * @cpu: the current cpu
1076 * This is the entry point for initiating any UV global TLB shootdown.
1078 * Purges the translation caches of all specified processors of the given
1079 * virtual address, or purges all TLB's on specified processors.
1081 * The caller has derived the cpumask from the mm_struct. This function
1082 * is called only if there are bits set in the mask. (e.g. flush_tlb_page())
1084 * The cpumask is converted into a uvhubmask of the uvhubs containing
1087 * Note that this function should be called with preemption disabled.
1089 * Returns NULL if all remote flushing was done.
1090 * Returns pointer to cpumask if some remote flushing remains to be
1091 * done. The returned pointer is valid till preemption is re-enabled.
1093 const struct cpumask *uv_flush_tlb_others(const struct cpumask *cpumask,
1094 struct mm_struct *mm,
1095 unsigned long start,
1102 struct bau_desc *bau_desc;
1103 struct cpumask *flush_mask;
1104 struct ptc_stats *stat;
1105 struct bau_control *bcp;
1106 unsigned long descriptor_status;
1107 unsigned long status;
1109 bcp = &per_cpu(bau_control, cpu);
1119 read_lmmr(UVH_LB_BAU_SB_ACTIVATION_STATUS_0);
1120 status = ((descriptor_status >> (bcp->uvhub_cpu *
1121 UV_ACT_STATUS_SIZE)) & UV_ACT_STATUS_MASK) << 1;
1122 if (status == UV2H_DESC_BUSY)
1127 /* bau was disabled due to slow response */
1128 if (bcp->baudisabled) {
1129 if (check_enable(bcp, stat)) {
1130 stat->s_ipifordisabled++;
1136 * Each sending cpu has a per-cpu mask which it fills from the caller's
1137 * cpu mask. All cpus are converted to uvhubs and copied to the
1138 * activation descriptor.
1140 flush_mask = (struct cpumask *)per_cpu(uv_flush_tlb_mask, cpu);
1141 /* don't actually do a shootdown of the local cpu */
1142 cpumask_andnot(flush_mask, cpumask, cpumask_of(cpu));
1144 if (cpumask_test_cpu(cpu, cpumask))
1145 stat->s_ntargself++;
1147 bau_desc = bcp->descriptor_base;
1148 bau_desc += (ITEMS_PER_DESC * bcp->uvhub_cpu);
1149 bau_uvhubs_clear(&bau_desc->distribution, UV_DISTRIBUTION_SIZE);
1150 if (set_distrib_bits(flush_mask, bcp, bau_desc, &locals, &remotes))
1153 record_send_statistics(stat, locals, hubs, remotes, bau_desc);
1155 if (!end || (end - start) <= PAGE_SIZE)
1156 bau_desc->payload.address = start;
1158 bau_desc->payload.address = TLB_FLUSH_ALL;
1159 bau_desc->payload.sending_cpu = cpu;
1161 * uv_flush_send_and_wait returns 0 if all cpu's were messaged,
1162 * or 1 if it gave up and the original cpumask should be returned.
1164 if (!uv_flush_send_and_wait(flush_mask, bcp, bau_desc))
1171 * Search the message queue for any 'other' unprocessed message with the
1172 * same software acknowledge resource bit vector as the 'msg' message.
1174 struct bau_pq_entry *find_another_by_swack(struct bau_pq_entry *msg,
1175 struct bau_control *bcp)
1177 struct bau_pq_entry *msg_next = msg + 1;
1178 unsigned char swack_vec = msg->swack_vec;
1180 if (msg_next > bcp->queue_last)
1181 msg_next = bcp->queue_first;
1182 while (msg_next != msg) {
1183 if ((msg_next->canceled == 0) && (msg_next->replied_to == 0) &&
1184 (msg_next->swack_vec == swack_vec))
1187 if (msg_next > bcp->queue_last)
1188 msg_next = bcp->queue_first;
1194 * UV2 needs to work around a bug in which an arriving message has not
1195 * set a bit in the UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE register.
1196 * Such a message must be ignored.
1198 void process_uv2_message(struct msg_desc *mdp, struct bau_control *bcp)
1200 unsigned long mmr_image;
1201 unsigned char swack_vec;
1202 struct bau_pq_entry *msg = mdp->msg;
1203 struct bau_pq_entry *other_msg;
1205 mmr_image = read_mmr_sw_ack();
1206 swack_vec = msg->swack_vec;
1208 if ((swack_vec & mmr_image) == 0) {
1210 * This message was assigned a swack resource, but no
1211 * reserved acknowlegment is pending.
1212 * The bug has prevented this message from setting the MMR.
1215 * Some message has set the MMR 'pending' bit; it might have
1216 * been another message. Look for that message.
1218 other_msg = find_another_by_swack(msg, bcp);
1221 * There is another. Process this one but do not
1224 bau_process_message(mdp, bcp, 0);
1226 * Let the natural processing of that other message
1227 * acknowledge it. Don't get the processing of sw_ack's
1235 * Either the MMR shows this one pending a reply or there is no
1236 * other message using this sw_ack, so it is safe to acknowledge it.
1238 bau_process_message(mdp, bcp, 1);
1244 * The BAU message interrupt comes here. (registered by set_intr_gate)
1247 * We received a broadcast assist message.
1249 * Interrupts are disabled; this interrupt could represent
1250 * the receipt of several messages.
1252 * All cores/threads on this hub get this interrupt.
1253 * The last one to see it does the software ack.
1254 * (the resource will not be freed until noninterruptable cpus see this
1255 * interrupt; hardware may timeout the s/w ack and reply ERROR)
1257 void uv_bau_message_interrupt(struct pt_regs *regs)
1260 cycles_t time_start;
1261 struct bau_pq_entry *msg;
1262 struct bau_control *bcp;
1263 struct ptc_stats *stat;
1264 struct msg_desc msgdesc;
1267 time_start = get_cycles();
1269 bcp = &per_cpu(bau_control, smp_processor_id());
1272 msgdesc.queue_first = bcp->queue_first;
1273 msgdesc.queue_last = bcp->queue_last;
1275 msg = bcp->bau_msg_head;
1276 while (msg->swack_vec) {
1279 msgdesc.msg_slot = msg - msgdesc.queue_first;
1281 if (bcp->uvhub_version == 2)
1282 process_uv2_message(&msgdesc, bcp);
1284 /* no error workaround for uv1 or uv3 */
1285 bau_process_message(&msgdesc, bcp, 1);
1288 if (msg > msgdesc.queue_last)
1289 msg = msgdesc.queue_first;
1290 bcp->bau_msg_head = msg;
1292 stat->d_time += (get_cycles() - time_start);
1300 * Each target uvhub (i.e. a uvhub that has cpu's) needs to have
1301 * shootdown message timeouts enabled. The timeout does not cause
1302 * an interrupt, but causes an error message to be returned to
1305 static void __init enable_timeouts(void)
1310 unsigned long mmr_image;
1312 nuvhubs = uv_num_possible_blades();
1314 for (uvhub = 0; uvhub < nuvhubs; uvhub++) {
1315 if (!uv_blade_nr_possible_cpus(uvhub))
1318 pnode = uv_blade_to_pnode(uvhub);
1319 mmr_image = read_mmr_misc_control(pnode);
1321 * Set the timeout period and then lock it in, in three
1322 * steps; captures and locks in the period.
1324 * To program the period, the SOFT_ACK_MODE must be off.
1326 mmr_image &= ~(1L << SOFTACK_MSHIFT);
1327 write_mmr_misc_control(pnode, mmr_image);
1329 * Set the 4-bit period.
1331 mmr_image &= ~((unsigned long)0xf << SOFTACK_PSHIFT);
1332 mmr_image |= (SOFTACK_TIMEOUT_PERIOD << SOFTACK_PSHIFT);
1333 write_mmr_misc_control(pnode, mmr_image);
1336 * Subsequent reversals of the timebase bit (3) cause an
1337 * immediate timeout of one or all INTD resources as
1338 * indicated in bits 2:0 (7 causes all of them to timeout).
1340 mmr_image |= (1L << SOFTACK_MSHIFT);
1342 /* do not touch the legacy mode bit */
1343 /* hw bug workaround; do not use extended status */
1344 mmr_image &= ~(1L << UV2_EXT_SHFT);
1345 } else if (is_uv3_hub()) {
1346 mmr_image &= ~(1L << PREFETCH_HINT_SHFT);
1347 mmr_image |= (1L << SB_STATUS_SHFT);
1349 write_mmr_misc_control(pnode, mmr_image);
1353 static void *ptc_seq_start(struct seq_file *file, loff_t *offset)
1355 if (*offset < num_possible_cpus())
1360 static void *ptc_seq_next(struct seq_file *file, void *data, loff_t *offset)
1363 if (*offset < num_possible_cpus())
1368 static void ptc_seq_stop(struct seq_file *file, void *data)
1373 * Display the statistics thru /proc/sgi_uv/ptc_statistics
1374 * 'data' points to the cpu number
1375 * Note: see the descriptions in stat_description[].
1377 static int ptc_seq_show(struct seq_file *file, void *data)
1379 struct ptc_stats *stat;
1380 struct bau_control *bcp;
1383 cpu = *(loff_t *)data;
1386 "# cpu bauoff sent stime self locals remotes ncpus localhub ");
1387 seq_puts(file, "remotehub numuvhubs numuvhubs16 numuvhubs8 ");
1389 "numuvhubs4 numuvhubs2 numuvhubs1 dto snacks retries ");
1391 "rok resetp resett giveup sto bz throt disable ");
1393 "enable wars warshw warwaits enters ipidis plugged ");
1395 "ipiover glim cong swack recv rtime all one mult ");
1396 seq_puts(file, "none retry canc nocan reset rcan\n");
1398 if (cpu < num_possible_cpus() && cpu_online(cpu)) {
1399 bcp = &per_cpu(bau_control, cpu);
1401 seq_printf(file, "cpu %d bau disabled\n", cpu);
1405 /* source side statistics */
1407 "cpu %d %d %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld ",
1408 cpu, bcp->nobau, stat->s_requestor,
1409 cycles_2_us(stat->s_time),
1410 stat->s_ntargself, stat->s_ntarglocals,
1411 stat->s_ntargremotes, stat->s_ntargcpu,
1412 stat->s_ntarglocaluvhub, stat->s_ntargremoteuvhub,
1413 stat->s_ntarguvhub, stat->s_ntarguvhub16);
1414 seq_printf(file, "%ld %ld %ld %ld %ld %ld ",
1415 stat->s_ntarguvhub8, stat->s_ntarguvhub4,
1416 stat->s_ntarguvhub2, stat->s_ntarguvhub1,
1417 stat->s_dtimeout, stat->s_strongnacks);
1418 seq_printf(file, "%ld %ld %ld %ld %ld %ld %ld %ld ",
1419 stat->s_retry_messages, stat->s_retriesok,
1420 stat->s_resets_plug, stat->s_resets_timeout,
1421 stat->s_giveup, stat->s_stimeout,
1422 stat->s_busy, stat->s_throttles);
1423 seq_printf(file, "%ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld ",
1424 stat->s_bau_disabled, stat->s_bau_reenabled,
1425 stat->s_uv2_wars, stat->s_uv2_wars_hw,
1426 stat->s_uv2_war_waits, stat->s_enters,
1427 stat->s_ipifordisabled, stat->s_plugged,
1428 stat->s_overipilimit, stat->s_giveuplimit,
1431 /* destination side statistics */
1433 "%lx %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld\n",
1434 read_gmmr_sw_ack(uv_cpu_to_pnode(cpu)),
1435 stat->d_requestee, cycles_2_us(stat->d_time),
1436 stat->d_alltlb, stat->d_onetlb, stat->d_multmsg,
1437 stat->d_nomsg, stat->d_retries, stat->d_canceled,
1438 stat->d_nocanceled, stat->d_resets,
1445 * Display the tunables thru debugfs
1447 static ssize_t tunables_read(struct file *file, char __user *userbuf,
1448 size_t count, loff_t *ppos)
1453 buf = kasprintf(GFP_KERNEL, "%s %s %s\n%d %d %d %d %d %d %d %d %d %d\n",
1454 "max_concur plugged_delay plugsb4reset timeoutsb4reset",
1455 "ipi_reset_limit complete_threshold congested_response_us",
1456 "congested_reps disabled_period giveup_limit",
1457 max_concurr, plugged_delay, plugsb4reset,
1458 timeoutsb4reset, ipi_reset_limit, complete_threshold,
1459 congested_respns_us, congested_reps, disabled_period,
1465 ret = simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
1471 * handle a write to /proc/sgi_uv/ptc_statistics
1472 * -1: reset the statistics
1473 * 0: display meaning of the statistics
1475 static ssize_t ptc_proc_write(struct file *file, const char __user *user,
1476 size_t count, loff_t *data)
1483 struct ptc_stats *stat;
1485 if (count == 0 || count > sizeof(optstr))
1487 if (copy_from_user(optstr, user, count))
1489 optstr[count - 1] = '\0';
1491 if (!strcmp(optstr, "on")) {
1494 } else if (!strcmp(optstr, "off")) {
1499 if (kstrtol(optstr, 10, &input_arg) < 0) {
1500 pr_debug("%s is invalid\n", optstr);
1504 if (input_arg == 0) {
1505 elements = ARRAY_SIZE(stat_description);
1506 pr_debug("# cpu: cpu number\n");
1507 pr_debug("Sender statistics:\n");
1508 for (i = 0; i < elements; i++)
1509 pr_debug("%s\n", stat_description[i]);
1510 } else if (input_arg == -1) {
1511 for_each_present_cpu(cpu) {
1512 stat = &per_cpu(ptcstats, cpu);
1513 memset(stat, 0, sizeof(struct ptc_stats));
1520 static int local_atoi(const char *name)
1527 val = 10*val+(*name-'0');
1536 * Parse the values written to /sys/kernel/debug/sgi_uv/bau_tunables.
1537 * Zero values reset them to defaults.
1539 static int parse_tunables_write(struct bau_control *bcp, char *instr,
1546 int e = ARRAY_SIZE(tunables);
1548 p = instr + strspn(instr, WHITESPACE);
1550 for (; *p; p = q + strspn(q, WHITESPACE)) {
1551 q = p + strcspn(p, WHITESPACE);
1557 pr_info("bau tunable error: should be %d values\n", e);
1561 p = instr + strspn(instr, WHITESPACE);
1563 for (cnt = 0; *p; p = q + strspn(q, WHITESPACE), cnt++) {
1564 q = p + strcspn(p, WHITESPACE);
1565 val = local_atoi(p);
1569 max_concurr = MAX_BAU_CONCURRENT;
1570 max_concurr_const = MAX_BAU_CONCURRENT;
1573 if (val < 1 || val > bcp->cpus_in_uvhub) {
1575 "Error: BAU max concurrent %d is invalid\n",
1580 max_concurr_const = val;
1584 *tunables[cnt].tunp = tunables[cnt].deflt;
1586 *tunables[cnt].tunp = val;
1596 * Handle a write to debugfs. (/sys/kernel/debug/sgi_uv/bau_tunables)
1598 static ssize_t tunables_write(struct file *file, const char __user *user,
1599 size_t count, loff_t *data)
1604 struct bau_control *bcp;
1606 if (count == 0 || count > sizeof(instr)-1)
1608 if (copy_from_user(instr, user, count))
1611 instr[count] = '\0';
1614 bcp = &per_cpu(bau_control, cpu);
1615 ret = parse_tunables_write(bcp, instr, count);
1620 for_each_present_cpu(cpu) {
1621 bcp = &per_cpu(bau_control, cpu);
1622 bcp->max_concurr = max_concurr;
1623 bcp->max_concurr_const = max_concurr;
1624 bcp->plugged_delay = plugged_delay;
1625 bcp->plugsb4reset = plugsb4reset;
1626 bcp->timeoutsb4reset = timeoutsb4reset;
1627 bcp->ipi_reset_limit = ipi_reset_limit;
1628 bcp->complete_threshold = complete_threshold;
1629 bcp->cong_response_us = congested_respns_us;
1630 bcp->cong_reps = congested_reps;
1631 bcp->disabled_period = sec_2_cycles(disabled_period);
1632 bcp->giveup_limit = giveup_limit;
1637 static const struct seq_operations uv_ptc_seq_ops = {
1638 .start = ptc_seq_start,
1639 .next = ptc_seq_next,
1640 .stop = ptc_seq_stop,
1641 .show = ptc_seq_show
1644 static int ptc_proc_open(struct inode *inode, struct file *file)
1646 return seq_open(file, &uv_ptc_seq_ops);
1649 static int tunables_open(struct inode *inode, struct file *file)
1654 static const struct file_operations proc_uv_ptc_operations = {
1655 .open = ptc_proc_open,
1657 .write = ptc_proc_write,
1658 .llseek = seq_lseek,
1659 .release = seq_release,
1662 static const struct file_operations tunables_fops = {
1663 .open = tunables_open,
1664 .read = tunables_read,
1665 .write = tunables_write,
1666 .llseek = default_llseek,
1669 static int __init uv_ptc_init(void)
1671 struct proc_dir_entry *proc_uv_ptc;
1673 if (!is_uv_system())
1676 proc_uv_ptc = proc_create(UV_PTC_BASENAME, 0444, NULL,
1677 &proc_uv_ptc_operations);
1679 pr_err("unable to create %s proc entry\n",
1684 tunables_dir = debugfs_create_dir(UV_BAU_TUNABLES_DIR, NULL);
1685 if (!tunables_dir) {
1686 pr_err("unable to create debugfs directory %s\n",
1687 UV_BAU_TUNABLES_DIR);
1690 tunables_file = debugfs_create_file(UV_BAU_TUNABLES_FILE, 0600,
1691 tunables_dir, NULL, &tunables_fops);
1692 if (!tunables_file) {
1693 pr_err("unable to create debugfs file %s\n",
1694 UV_BAU_TUNABLES_FILE);
1701 * Initialize the sending side's sending buffers.
1703 static void activation_descriptor_init(int node, int pnode, int base_pnode)
1712 struct bau_desc *bau_desc;
1713 struct bau_desc *bd2;
1714 struct uv1_bau_msg_header *uv1_hdr;
1715 struct uv2_3_bau_msg_header *uv2_3_hdr;
1716 struct bau_control *bcp;
1719 * each bau_desc is 64 bytes; there are 8 (ITEMS_PER_DESC)
1720 * per cpu; and one per cpu on the uvhub (ADP_SZ)
1722 dsize = sizeof(struct bau_desc) * ADP_SZ * ITEMS_PER_DESC;
1723 bau_desc = kmalloc_node(dsize, GFP_KERNEL, node);
1726 gpa = uv_gpa(bau_desc);
1727 n = uv_gpa_to_gnode(gpa);
1728 m = uv_gpa_to_offset(gpa);
1732 /* the 14-bit pnode */
1733 write_mmr_descriptor_base(pnode, (n << UV_DESC_PSHIFT | m));
1735 * Initializing all 8 (ITEMS_PER_DESC) descriptors for each
1736 * cpu even though we only use the first one; one descriptor can
1737 * describe a broadcast to 256 uv hubs.
1739 for (i = 0, bd2 = bau_desc; i < (ADP_SZ * ITEMS_PER_DESC); i++, bd2++) {
1740 memset(bd2, 0, sizeof(struct bau_desc));
1742 uv1_hdr = &bd2->header.uv1_hdr;
1743 uv1_hdr->swack_flag = 1;
1745 * The base_dest_nasid set in the message header
1746 * is the nasid of the first uvhub in the partition.
1747 * The bit map will indicate destination pnode numbers
1748 * relative to that base. They may not be consecutive
1749 * if nasid striding is being used.
1751 uv1_hdr->base_dest_nasid =
1752 UV_PNODE_TO_NASID(base_pnode);
1753 uv1_hdr->dest_subnodeid = UV_LB_SUBNODEID;
1754 uv1_hdr->command = UV_NET_ENDPOINT_INTD;
1755 uv1_hdr->int_both = 1;
1757 * all others need to be set to zero:
1758 * fairness chaining multilevel count replied_to
1762 * BIOS uses legacy mode, but uv2 and uv3 hardware always
1763 * uses native mode for selective broadcasts.
1765 uv2_3_hdr = &bd2->header.uv2_3_hdr;
1766 uv2_3_hdr->swack_flag = 1;
1767 uv2_3_hdr->base_dest_nasid =
1768 UV_PNODE_TO_NASID(base_pnode);
1769 uv2_3_hdr->dest_subnodeid = UV_LB_SUBNODEID;
1770 uv2_3_hdr->command = UV_NET_ENDPOINT_INTD;
1773 for_each_present_cpu(cpu) {
1774 if (pnode != uv_blade_to_pnode(uv_cpu_to_blade_id(cpu)))
1776 bcp = &per_cpu(bau_control, cpu);
1777 bcp->descriptor_base = bau_desc;
1782 * initialize the destination side's receiving buffers
1783 * entered for each uvhub in the partition
1784 * - node is first node (kernel memory notion) on the uvhub
1785 * - pnode is the uvhub's physical identifier
1787 static void pq_init(int node, int pnode)
1793 unsigned long gnode, first, last, tail;
1794 struct bau_pq_entry *pqp;
1795 struct bau_control *bcp;
1797 plsize = (DEST_Q_SIZE + 1) * sizeof(struct bau_pq_entry);
1798 vp = kmalloc_node(plsize, GFP_KERNEL, node);
1799 pqp = (struct bau_pq_entry *)vp;
1802 cp = (char *)pqp + 31;
1803 pqp = (struct bau_pq_entry *)(((unsigned long)cp >> 5) << 5);
1805 for_each_present_cpu(cpu) {
1806 if (pnode != uv_cpu_to_pnode(cpu))
1808 /* for every cpu on this pnode: */
1809 bcp = &per_cpu(bau_control, cpu);
1810 bcp->queue_first = pqp;
1811 bcp->bau_msg_head = pqp;
1812 bcp->queue_last = pqp + (DEST_Q_SIZE - 1);
1815 first = uv_gpa_to_offset(uv_gpa(pqp));
1816 last = uv_gpa_to_offset(uv_gpa(pqp + (DEST_Q_SIZE - 1)));
1818 gnode = uv_gpa_to_gnode(uv_gpa(pqp));
1819 first = (gnode << UV_PAYLOADQ_GNODE_SHIFT) | tail;
1821 write_mmr_payload_first(pnode, first);
1822 write_mmr_payload_last(pnode, last);
1823 write_mmr_payload_tail(pnode, tail);
1824 write_gmmr_sw_ack(pnode, 0xffffUL);
1826 /* in effect, all msg_type's are set to MSG_NOOP */
1827 memset(pqp, 0, sizeof(struct bau_pq_entry) * DEST_Q_SIZE);
1831 * Initialization of each UV hub's structures
1833 static void __init init_uvhub(int uvhub, int vector, int base_pnode)
1837 unsigned long apicid;
1839 node = uvhub_to_first_node(uvhub);
1840 pnode = uv_blade_to_pnode(uvhub);
1842 activation_descriptor_init(node, pnode, base_pnode);
1844 pq_init(node, pnode);
1846 * The below initialization can't be in firmware because the
1847 * messaging IRQ will be determined by the OS.
1849 apicid = uvhub_to_first_apicid(uvhub) | uv_apicid_hibits;
1850 write_mmr_data_config(pnode, ((apicid << 32) | vector));
1854 * We will set BAU_MISC_CONTROL with a timeout period.
1855 * But the BIOS has set UVH_AGING_PRESCALE_SEL and UVH_TRANSACTION_TIMEOUT.
1856 * So the destination timeout period has to be calculated from them.
1858 static int calculate_destination_timeout(void)
1860 unsigned long mmr_image;
1866 unsigned long ts_ns;
1869 mult1 = SOFTACK_TIMEOUT_PERIOD & BAU_MISC_CONTROL_MULT_MASK;
1870 mmr_image = uv_read_local_mmr(UVH_AGING_PRESCALE_SEL);
1871 index = (mmr_image >> BAU_URGENCY_7_SHIFT) & BAU_URGENCY_7_MASK;
1872 mmr_image = uv_read_local_mmr(UVH_TRANSACTION_TIMEOUT);
1873 mult2 = (mmr_image >> BAU_TRANS_SHIFT) & BAU_TRANS_MASK;
1874 ts_ns = timeout_base_ns[index];
1875 ts_ns *= (mult1 * mult2);
1878 /* same destination timeout for uv2 and uv3 */
1879 /* 4 bits 0/1 for 10/80us base, 3 bits of multiplier */
1880 mmr_image = uv_read_local_mmr(UVH_LB_BAU_MISC_CONTROL);
1881 mmr_image = (mmr_image & UV_SA_MASK) >> UV_SA_SHFT;
1882 if (mmr_image & (1L << UV2_ACK_UNITS_SHFT))
1886 mult1 = mmr_image & UV2_ACK_MASK;
1892 static void __init init_per_cpu_tunables(void)
1895 struct bau_control *bcp;
1897 for_each_present_cpu(cpu) {
1898 bcp = &per_cpu(bau_control, cpu);
1899 bcp->baudisabled = 0;
1902 bcp->statp = &per_cpu(ptcstats, cpu);
1903 /* time interval to catch a hardware stay-busy bug */
1904 bcp->timeout_interval = usec_2_cycles(2*timeout_us);
1905 bcp->max_concurr = max_concurr;
1906 bcp->max_concurr_const = max_concurr;
1907 bcp->plugged_delay = plugged_delay;
1908 bcp->plugsb4reset = plugsb4reset;
1909 bcp->timeoutsb4reset = timeoutsb4reset;
1910 bcp->ipi_reset_limit = ipi_reset_limit;
1911 bcp->complete_threshold = complete_threshold;
1912 bcp->cong_response_us = congested_respns_us;
1913 bcp->cong_reps = congested_reps;
1914 bcp->disabled_period = sec_2_cycles(disabled_period);
1915 bcp->giveup_limit = giveup_limit;
1916 spin_lock_init(&bcp->queue_lock);
1917 spin_lock_init(&bcp->uvhub_lock);
1918 spin_lock_init(&bcp->disable_lock);
1923 * Scan all cpus to collect blade and socket summaries.
1925 static int __init get_cpu_topology(int base_pnode,
1926 struct uvhub_desc *uvhub_descs,
1927 unsigned char *uvhub_mask)
1933 struct bau_control *bcp;
1934 struct uvhub_desc *bdp;
1935 struct socket_desc *sdp;
1937 for_each_present_cpu(cpu) {
1938 bcp = &per_cpu(bau_control, cpu);
1940 memset(bcp, 0, sizeof(struct bau_control));
1942 pnode = uv_cpu_hub_info(cpu)->pnode;
1943 if ((pnode - base_pnode) >= UV_DISTRIBUTION_SIZE) {
1945 "cpu %d pnode %d-%d beyond %d; BAU disabled\n",
1946 cpu, pnode, base_pnode, UV_DISTRIBUTION_SIZE);
1950 bcp->osnode = cpu_to_node(cpu);
1951 bcp->partition_base_pnode = base_pnode;
1953 uvhub = uv_cpu_hub_info(cpu)->numa_blade_id;
1954 *(uvhub_mask + (uvhub/8)) |= (1 << (uvhub%8));
1955 bdp = &uvhub_descs[uvhub];
1961 /* kludge: 'assuming' one node per socket, and assuming that
1962 disabling a socket just leaves a gap in node numbers */
1963 socket = bcp->osnode & 1;
1964 bdp->socket_mask |= (1 << socket);
1965 sdp = &bdp->socket[socket];
1966 sdp->cpu_number[sdp->num_cpus] = cpu;
1968 if (sdp->num_cpus > MAX_CPUS_PER_SOCKET) {
1969 pr_emerg("%d cpus per socket invalid\n",
1978 * Each socket is to get a local array of pnodes/hubs.
1980 static void make_per_cpu_thp(struct bau_control *smaster)
1983 size_t hpsz = sizeof(struct hub_and_pnode) * num_possible_cpus();
1985 smaster->thp = kmalloc_node(hpsz, GFP_KERNEL, smaster->osnode);
1986 memset(smaster->thp, 0, hpsz);
1987 for_each_present_cpu(cpu) {
1988 smaster->thp[cpu].pnode = uv_cpu_hub_info(cpu)->pnode;
1989 smaster->thp[cpu].uvhub = uv_cpu_hub_info(cpu)->numa_blade_id;
1994 * Each uvhub is to get a local cpumask.
1996 static void make_per_hub_cpumask(struct bau_control *hmaster)
1998 int sz = sizeof(cpumask_t);
2000 hmaster->cpumask = kzalloc_node(sz, GFP_KERNEL, hmaster->osnode);
2004 * Initialize all the per_cpu information for the cpu's on a given socket,
2005 * given what has been gathered into the socket_desc struct.
2006 * And reports the chosen hub and socket masters back to the caller.
2008 static int scan_sock(struct socket_desc *sdp, struct uvhub_desc *bdp,
2009 struct bau_control **smasterp,
2010 struct bau_control **hmasterp)
2014 struct bau_control *bcp;
2016 for (i = 0; i < sdp->num_cpus; i++) {
2017 cpu = sdp->cpu_number[i];
2018 bcp = &per_cpu(bau_control, cpu);
2025 bcp->cpus_in_uvhub = bdp->num_cpus;
2026 bcp->cpus_in_socket = sdp->num_cpus;
2027 bcp->socket_master = *smasterp;
2028 bcp->uvhub = bdp->uvhub;
2030 bcp->uvhub_version = 1;
2031 else if (is_uv2_hub())
2032 bcp->uvhub_version = 2;
2033 else if (is_uv3_hub())
2034 bcp->uvhub_version = 3;
2036 pr_emerg("uvhub version not 1, 2, or 3\n");
2039 bcp->uvhub_master = *hmasterp;
2040 bcp->uvhub_cpu = uv_cpu_blade_processor_id(cpu);
2042 if (bcp->uvhub_cpu >= MAX_CPUS_PER_UVHUB) {
2043 pr_emerg("%d cpus per uvhub invalid\n",
2052 * Summarize the blade and socket topology into the per_cpu structures.
2054 static int __init summarize_uvhub_sockets(int nuvhubs,
2055 struct uvhub_desc *uvhub_descs,
2056 unsigned char *uvhub_mask)
2060 unsigned short socket_mask;
2062 for (uvhub = 0; uvhub < nuvhubs; uvhub++) {
2063 struct uvhub_desc *bdp;
2064 struct bau_control *smaster = NULL;
2065 struct bau_control *hmaster = NULL;
2067 if (!(*(uvhub_mask + (uvhub/8)) & (1 << (uvhub%8))))
2070 bdp = &uvhub_descs[uvhub];
2071 socket_mask = bdp->socket_mask;
2073 while (socket_mask) {
2074 struct socket_desc *sdp;
2075 if ((socket_mask & 1)) {
2076 sdp = &bdp->socket[socket];
2077 if (scan_sock(sdp, bdp, &smaster, &hmaster))
2079 make_per_cpu_thp(smaster);
2082 socket_mask = (socket_mask >> 1);
2084 make_per_hub_cpumask(hmaster);
2090 * initialize the bau_control structure for each cpu
2092 static int __init init_per_cpu(int nuvhubs, int base_part_pnode)
2094 unsigned char *uvhub_mask;
2096 struct uvhub_desc *uvhub_descs;
2098 timeout_us = calculate_destination_timeout();
2100 vp = kmalloc(nuvhubs * sizeof(struct uvhub_desc), GFP_KERNEL);
2101 uvhub_descs = (struct uvhub_desc *)vp;
2102 memset(uvhub_descs, 0, nuvhubs * sizeof(struct uvhub_desc));
2103 uvhub_mask = kzalloc((nuvhubs+7)/8, GFP_KERNEL);
2105 if (get_cpu_topology(base_part_pnode, uvhub_descs, uvhub_mask))
2108 if (summarize_uvhub_sockets(nuvhubs, uvhub_descs, uvhub_mask))
2113 init_per_cpu_tunables();
2123 * Initialization of BAU-related structures
2125 static int __init uv_bau_init(void)
2133 cpumask_var_t *mask;
2135 if (!is_uv_system())
2138 for_each_possible_cpu(cur_cpu) {
2139 mask = &per_cpu(uv_flush_tlb_mask, cur_cpu);
2140 zalloc_cpumask_var_node(mask, GFP_KERNEL, cpu_to_node(cur_cpu));
2143 nuvhubs = uv_num_possible_blades();
2144 congested_cycles = usec_2_cycles(congested_respns_us);
2146 uv_base_pnode = 0x7fffffff;
2147 for (uvhub = 0; uvhub < nuvhubs; uvhub++) {
2148 cpus = uv_blade_nr_possible_cpus(uvhub);
2149 if (cpus && (uv_blade_to_pnode(uvhub) < uv_base_pnode))
2150 uv_base_pnode = uv_blade_to_pnode(uvhub);
2155 if (init_per_cpu(nuvhubs, uv_base_pnode)) {
2161 vector = UV_BAU_MESSAGE;
2162 for_each_possible_blade(uvhub) {
2163 if (uv_blade_nr_possible_cpus(uvhub))
2164 init_uvhub(uvhub, vector, uv_base_pnode);
2167 alloc_intr_gate(vector, uv_bau_message_intr1);
2169 for_each_possible_blade(uvhub) {
2170 if (uv_blade_nr_possible_cpus(uvhub)) {
2173 pnode = uv_blade_to_pnode(uvhub);
2176 write_gmmr_activation(pnode, val);
2177 mmr = 1; /* should be 1 to broadcast to both sockets */
2179 write_mmr_data_broadcast(pnode, mmr);
2185 core_initcall(uv_bau_init);
2186 fs_initcall(uv_ptc_init);