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>
23 #include <asm/irq_vectors.h>
24 #include <asm/timer.h>
26 static struct bau_operations ops __ro_after_init;
28 /* timeouts in nanoseconds (indexed by UVH_AGING_PRESCALE_SEL urgency7 30:28) */
29 static int timeout_base_ns[] = {
40 static int timeout_us;
41 static bool nobau = true;
42 static int nobau_perm;
43 static cycles_t congested_cycles;
46 static int max_concurr = MAX_BAU_CONCURRENT;
47 static int max_concurr_const = MAX_BAU_CONCURRENT;
48 static int plugged_delay = PLUGGED_DELAY;
49 static int plugsb4reset = PLUGSB4RESET;
50 static int giveup_limit = GIVEUP_LIMIT;
51 static int timeoutsb4reset = TIMEOUTSB4RESET;
52 static int ipi_reset_limit = IPI_RESET_LIMIT;
53 static int complete_threshold = COMPLETE_THRESHOLD;
54 static int congested_respns_us = CONGESTED_RESPONSE_US;
55 static int congested_reps = CONGESTED_REPS;
56 static int disabled_period = DISABLED_PERIOD;
58 static struct tunables tunables[] = {
59 {&max_concurr, MAX_BAU_CONCURRENT}, /* must be [0] */
60 {&plugged_delay, PLUGGED_DELAY},
61 {&plugsb4reset, PLUGSB4RESET},
62 {&timeoutsb4reset, TIMEOUTSB4RESET},
63 {&ipi_reset_limit, IPI_RESET_LIMIT},
64 {&complete_threshold, COMPLETE_THRESHOLD},
65 {&congested_respns_us, CONGESTED_RESPONSE_US},
66 {&congested_reps, CONGESTED_REPS},
67 {&disabled_period, DISABLED_PERIOD},
68 {&giveup_limit, GIVEUP_LIMIT}
71 static struct dentry *tunables_dir;
72 static struct dentry *tunables_file;
74 /* these correspond to the statistics printed by ptc_seq_show() */
75 static char *stat_description[] = {
76 "sent: number of shootdown messages sent",
77 "stime: time spent sending messages",
78 "numuvhubs: number of hubs targeted with shootdown",
79 "numuvhubs16: number times 16 or more hubs targeted",
80 "numuvhubs8: number times 8 or more hubs targeted",
81 "numuvhubs4: number times 4 or more hubs targeted",
82 "numuvhubs2: number times 2 or more hubs targeted",
83 "numuvhubs1: number times 1 hub targeted",
84 "numcpus: number of cpus targeted with shootdown",
85 "dto: number of destination timeouts",
86 "retries: destination timeout retries sent",
87 "rok: : destination timeouts successfully retried",
88 "resetp: ipi-style resource resets for plugs",
89 "resett: ipi-style resource resets for timeouts",
90 "giveup: fall-backs to ipi-style shootdowns",
91 "sto: number of source timeouts",
92 "bz: number of stay-busy's",
93 "throt: number times spun in throttle",
94 "swack: image of UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE",
95 "recv: shootdown messages received",
96 "rtime: time spent processing messages",
97 "all: shootdown all-tlb messages",
98 "one: shootdown one-tlb messages",
99 "mult: interrupts that found multiple messages",
100 "none: interrupts that found no messages",
101 "retry: number of retry messages processed",
102 "canc: number messages canceled by retries",
103 "nocan: number retries that found nothing to cancel",
104 "reset: number of ipi-style reset requests processed",
105 "rcan: number messages canceled by reset requests",
106 "disable: number times use of the BAU was disabled",
107 "enable: number times use of the BAU was re-enabled"
110 static int __init setup_bau(char *arg)
117 result = strtobool(arg, &nobau);
121 /* we need to flip the logic here, so that bau=y sets nobau to false */
125 pr_info("UV BAU Enabled\n");
127 pr_info("UV BAU Disabled\n");
131 early_param("bau", setup_bau);
133 /* base pnode in this partition */
134 static int uv_base_pnode __read_mostly;
136 static DEFINE_PER_CPU(struct ptc_stats, ptcstats);
137 static DEFINE_PER_CPU(struct bau_control, bau_control);
138 static DEFINE_PER_CPU(cpumask_var_t, uv_flush_tlb_mask);
144 struct bau_control *bcp;
147 pr_info("BAU not initialized; cannot be turned on\n");
151 for_each_present_cpu(cpu) {
152 bcp = &per_cpu(bau_control, cpu);
155 pr_info("BAU turned on\n");
163 struct bau_control *bcp;
166 for_each_present_cpu(cpu) {
167 bcp = &per_cpu(bau_control, cpu);
170 pr_info("BAU turned off\n");
175 * Determine the first node on a uvhub. 'Nodes' are used for kernel
178 static int __init uvhub_to_first_node(int uvhub)
182 for_each_online_node(node) {
183 b = uv_node_to_blade_id(node);
191 * Determine the apicid of the first cpu on a uvhub.
193 static int __init uvhub_to_first_apicid(int uvhub)
197 for_each_present_cpu(cpu)
198 if (uvhub == uv_cpu_to_blade_id(cpu))
199 return per_cpu(x86_cpu_to_apicid, cpu);
204 * Free a software acknowledge hardware resource by clearing its Pending
205 * bit. This will return a reply to the sender.
206 * If the message has timed out, a reply has already been sent by the
207 * hardware but the resource has not been released. In that case our
208 * clear of the Timeout bit (as well) will free the resource. No reply will
209 * be sent (the hardware will only do one reply per message).
211 static void reply_to_message(struct msg_desc *mdp, struct bau_control *bcp,
215 struct bau_pq_entry *msg;
218 if (!msg->canceled && do_acknowledge) {
219 dw = (msg->swack_vec << UV_SW_ACK_NPENDING) | msg->swack_vec;
220 ops.write_l_sw_ack(dw);
227 * Process the receipt of a RETRY message
229 static void bau_process_retry_msg(struct msg_desc *mdp,
230 struct bau_control *bcp)
233 int cancel_count = 0;
234 unsigned long msg_res;
235 unsigned long mmr = 0;
236 struct bau_pq_entry *msg = mdp->msg;
237 struct bau_pq_entry *msg2;
238 struct ptc_stats *stat = bcp->statp;
242 * cancel any message from msg+1 to the retry itself
244 for (msg2 = msg+1, i = 0; i < DEST_Q_SIZE; msg2++, i++) {
245 if (msg2 > mdp->queue_last)
246 msg2 = mdp->queue_first;
250 /* same conditions for cancellation as do_reset */
251 if ((msg2->replied_to == 0) && (msg2->canceled == 0) &&
252 (msg2->swack_vec) && ((msg2->swack_vec &
253 msg->swack_vec) == 0) &&
254 (msg2->sending_cpu == msg->sending_cpu) &&
255 (msg2->msg_type != MSG_NOOP)) {
256 mmr = ops.read_l_sw_ack();
257 msg_res = msg2->swack_vec;
259 * This is a message retry; clear the resources held
260 * by the previous message only if they timed out.
261 * If it has not timed out we have an unexpected
262 * situation to report.
264 if (mmr & (msg_res << UV_SW_ACK_NPENDING)) {
267 * Is the resource timed out?
268 * Make everyone ignore the cancelled message.
273 mr = (msg_res << UV_SW_ACK_NPENDING) | msg_res;
274 ops.write_l_sw_ack(mr);
279 stat->d_nocanceled++;
283 * Do all the things a cpu should do for a TLB shootdown message.
284 * Other cpu's may come here at the same time for this message.
286 static void bau_process_message(struct msg_desc *mdp, struct bau_control *bcp,
289 short socket_ack_count = 0;
291 struct atomic_short *asp;
292 struct ptc_stats *stat = bcp->statp;
293 struct bau_pq_entry *msg = mdp->msg;
294 struct bau_control *smaster = bcp->socket_master;
297 * This must be a normal message, or retry of a normal message
299 if (msg->address == TLB_FLUSH_ALL) {
303 __flush_tlb_one(msg->address);
309 * One cpu on each uvhub has the additional job on a RETRY
310 * of releasing the resource held by the message that is
311 * being retried. That message is identified by sending
314 if (msg->msg_type == MSG_RETRY && bcp == bcp->uvhub_master)
315 bau_process_retry_msg(mdp, bcp);
318 * This is a swack message, so we have to reply to it.
319 * Count each responding cpu on the socket. This avoids
320 * pinging the count's cache line back and forth between
323 sp = &smaster->socket_acknowledge_count[mdp->msg_slot];
324 asp = (struct atomic_short *)sp;
325 socket_ack_count = atom_asr(1, asp);
326 if (socket_ack_count == bcp->cpus_in_socket) {
329 * Both sockets dump their completed count total into
330 * the message's count.
333 asp = (struct atomic_short *)&msg->acknowledge_count;
334 msg_ack_count = atom_asr(socket_ack_count, asp);
336 if (msg_ack_count == bcp->cpus_in_uvhub) {
338 * All cpus in uvhub saw it; reply
339 * (unless we are in the UV2 workaround)
341 reply_to_message(mdp, bcp, do_acknowledge);
349 * Determine the first cpu on a pnode.
351 static int pnode_to_first_cpu(int pnode, struct bau_control *smaster)
354 struct hub_and_pnode *hpp;
356 for_each_present_cpu(cpu) {
357 hpp = &smaster->thp[cpu];
358 if (pnode == hpp->pnode)
365 * Last resort when we get a large number of destination timeouts is
366 * to clear resources held by a given cpu.
367 * Do this with IPI so that all messages in the BAU message queue
368 * can be identified by their nonzero swack_vec field.
370 * This is entered for a single cpu on the uvhub.
371 * The sender want's this uvhub to free a specific message's
374 static void do_reset(void *ptr)
377 struct bau_control *bcp = &per_cpu(bau_control, smp_processor_id());
378 struct reset_args *rap = (struct reset_args *)ptr;
379 struct bau_pq_entry *msg;
380 struct ptc_stats *stat = bcp->statp;
384 * We're looking for the given sender, and
385 * will free its swack resource.
386 * If all cpu's finally responded after the timeout, its
387 * message 'replied_to' was set.
389 for (msg = bcp->queue_first, i = 0; i < DEST_Q_SIZE; msg++, i++) {
390 unsigned long msg_res;
391 /* do_reset: same conditions for cancellation as
392 bau_process_retry_msg() */
393 if ((msg->replied_to == 0) &&
394 (msg->canceled == 0) &&
395 (msg->sending_cpu == rap->sender) &&
397 (msg->msg_type != MSG_NOOP)) {
401 * make everyone else ignore this message
405 * only reset the resource if it is still pending
407 mmr = ops.read_l_sw_ack();
408 msg_res = msg->swack_vec;
409 mr = (msg_res << UV_SW_ACK_NPENDING) | msg_res;
412 ops.write_l_sw_ack(mr);
420 * Use IPI to get all target uvhubs to release resources held by
421 * a given sending cpu number.
423 static void reset_with_ipi(struct pnmask *distribution, struct bau_control *bcp)
428 int sender = bcp->cpu;
429 cpumask_t *mask = bcp->uvhub_master->cpumask;
430 struct bau_control *smaster = bcp->socket_master;
431 struct reset_args reset_args;
433 reset_args.sender = sender;
435 /* find a single cpu for each uvhub in this distribution mask */
436 maskbits = sizeof(struct pnmask) * BITSPERBYTE;
437 /* each bit is a pnode relative to the partition base pnode */
438 for (pnode = 0; pnode < maskbits; pnode++) {
440 if (!bau_uvhub_isset(pnode, distribution))
442 apnode = pnode + bcp->partition_base_pnode;
443 cpu = pnode_to_first_cpu(apnode, smaster);
444 cpumask_set_cpu(cpu, mask);
447 /* IPI all cpus; preemption is already disabled */
448 smp_call_function_many(mask, do_reset, (void *)&reset_args, 1);
453 * Not to be confused with cycles_2_ns() from tsc.c; this gives a relative
454 * number, not an absolute. It converts a duration in cycles to a duration in
457 static inline unsigned long long cycles_2_ns(unsigned long long cyc)
459 struct cyc2ns_data data;
460 unsigned long long ns;
462 cyc2ns_read_begin(&data);
463 ns = mul_u64_u32_shr(cyc, data.cyc2ns_mul, data.cyc2ns_shift);
470 * The reverse of the above; converts a duration in ns to a duration in cycles.
472 static inline unsigned long long ns_2_cycles(unsigned long long ns)
474 struct cyc2ns_data data;
475 unsigned long long cyc;
477 cyc2ns_read_begin(&data);
478 cyc = (ns << data.cyc2ns_shift) / data.cyc2ns_mul;
484 static inline unsigned long cycles_2_us(unsigned long long cyc)
486 return cycles_2_ns(cyc) / NSEC_PER_USEC;
489 static inline cycles_t sec_2_cycles(unsigned long sec)
491 return ns_2_cycles(sec * NSEC_PER_SEC);
494 static inline unsigned long long usec_2_cycles(unsigned long usec)
496 return ns_2_cycles(usec * NSEC_PER_USEC);
500 * wait for all cpus on this hub to finish their sends and go quiet
501 * leaves uvhub_quiesce set so that no new broadcasts are started by
502 * bau_flush_send_and_wait()
504 static inline void quiesce_local_uvhub(struct bau_control *hmaster)
506 atom_asr(1, (struct atomic_short *)&hmaster->uvhub_quiesce);
510 * mark this quiet-requestor as done
512 static inline void end_uvhub_quiesce(struct bau_control *hmaster)
514 atom_asr(-1, (struct atomic_short *)&hmaster->uvhub_quiesce);
517 static unsigned long uv1_read_status(unsigned long mmr_offset, int right_shift)
519 unsigned long descriptor_status;
521 descriptor_status = uv_read_local_mmr(mmr_offset);
522 descriptor_status >>= right_shift;
523 descriptor_status &= UV_ACT_STATUS_MASK;
524 return descriptor_status;
528 * Wait for completion of a broadcast software ack message
529 * return COMPLETE, RETRY(PLUGGED or TIMEOUT) or GIVEUP
531 static int uv1_wait_completion(struct bau_desc *bau_desc,
532 struct bau_control *bcp, long try)
534 unsigned long descriptor_status;
536 u64 mmr_offset = bcp->status_mmr;
537 int right_shift = bcp->status_index;
538 struct ptc_stats *stat = bcp->statp;
540 descriptor_status = uv1_read_status(mmr_offset, right_shift);
541 /* spin on the status MMR, waiting for it to go idle */
542 while ((descriptor_status != DS_IDLE)) {
544 * Our software ack messages may be blocked because
545 * there are no swack resources available. As long
546 * as none of them has timed out hardware will NACK
547 * our message and its state will stay IDLE.
549 if (descriptor_status == DS_SOURCE_TIMEOUT) {
552 } else if (descriptor_status == DS_DESTINATION_TIMEOUT) {
557 * Our retries may be blocked by all destination
558 * swack resources being consumed, and a timeout
559 * pending. In that case hardware returns the
560 * ERROR that looks like a destination timeout.
562 if (cycles_2_us(ttm - bcp->send_message) < timeout_us) {
563 bcp->conseccompletes = 0;
564 return FLUSH_RETRY_PLUGGED;
567 bcp->conseccompletes = 0;
568 return FLUSH_RETRY_TIMEOUT;
571 * descriptor_status is still BUSY
575 descriptor_status = uv1_read_status(mmr_offset, right_shift);
577 bcp->conseccompletes++;
578 return FLUSH_COMPLETE;
582 * UV2 could have an extra bit of status in the ACTIVATION_STATUS_2 register.
583 * But not currently used.
585 static unsigned long uv2_3_read_status(unsigned long offset, int rshft, int desc)
587 return ((read_lmmr(offset) >> rshft) & UV_ACT_STATUS_MASK) << 1;
591 * Entered when a bau descriptor has gone into a permanent busy wait because
593 * Workaround the bug.
595 static int handle_uv2_busy(struct bau_control *bcp)
597 struct ptc_stats *stat = bcp->statp;
604 static int uv2_3_wait_completion(struct bau_desc *bau_desc,
605 struct bau_control *bcp, long try)
607 unsigned long descriptor_stat;
609 u64 mmr_offset = bcp->status_mmr;
610 int right_shift = bcp->status_index;
611 int desc = bcp->uvhub_cpu;
613 struct ptc_stats *stat = bcp->statp;
615 descriptor_stat = uv2_3_read_status(mmr_offset, right_shift, desc);
617 /* spin on the status MMR, waiting for it to go idle */
618 while (descriptor_stat != UV2H_DESC_IDLE) {
619 if ((descriptor_stat == UV2H_DESC_SOURCE_TIMEOUT)) {
621 * A h/w bug on the destination side may
622 * have prevented the message being marked
623 * pending, thus it doesn't get replied to
624 * and gets continually nacked until it times
625 * out with a SOURCE_TIMEOUT.
629 } else if (descriptor_stat == UV2H_DESC_DEST_TIMEOUT) {
633 * Our retries may be blocked by all destination
634 * swack resources being consumed, and a timeout
635 * pending. In that case hardware returns the
636 * ERROR that looks like a destination timeout.
637 * Without using the extended status we have to
638 * deduce from the short time that this was a
641 if (cycles_2_us(ttm - bcp->send_message) < timeout_us) {
642 bcp->conseccompletes = 0;
644 /* FLUSH_RETRY_PLUGGED causes hang on boot */
648 bcp->conseccompletes = 0;
649 /* FLUSH_RETRY_TIMEOUT causes hang on boot */
653 if (busy_reps > 1000000) {
654 /* not to hammer on the clock */
657 if ((ttm - bcp->send_message) > bcp->timeout_interval)
658 return handle_uv2_busy(bcp);
661 * descriptor_stat is still BUSY
665 descriptor_stat = uv2_3_read_status(mmr_offset, right_shift, desc);
667 bcp->conseccompletes++;
668 return FLUSH_COMPLETE;
672 * Returns the status of current BAU message for cpu desc as a bit field
675 static u64 read_status(u64 status_mmr, int index, int desc)
679 stat = ((read_lmmr(status_mmr) >> index) & UV_ACT_STATUS_MASK) << 1;
680 stat |= (read_lmmr(UVH_LB_BAU_SB_ACTIVATION_STATUS_2) >> desc) & 0x1;
685 static int uv4_wait_completion(struct bau_desc *bau_desc,
686 struct bau_control *bcp, long try)
688 struct ptc_stats *stat = bcp->statp;
690 u64 mmr = bcp->status_mmr;
691 int index = bcp->status_index;
692 int desc = bcp->uvhub_cpu;
694 descriptor_stat = read_status(mmr, index, desc);
696 /* spin on the status MMR, waiting for it to go idle */
697 while (descriptor_stat != UV2H_DESC_IDLE) {
698 switch (descriptor_stat) {
699 case UV2H_DESC_SOURCE_TIMEOUT:
703 case UV2H_DESC_DEST_TIMEOUT:
705 bcp->conseccompletes = 0;
706 return FLUSH_RETRY_TIMEOUT;
708 case UV2H_DESC_DEST_STRONG_NACK:
710 bcp->conseccompletes = 0;
711 return FLUSH_RETRY_PLUGGED;
713 case UV2H_DESC_DEST_PUT_ERR:
714 bcp->conseccompletes = 0;
718 /* descriptor_stat is still BUSY */
721 descriptor_stat = read_status(mmr, index, desc);
723 bcp->conseccompletes++;
724 return FLUSH_COMPLETE;
728 * Our retries are blocked by all destination sw ack resources being
729 * in use, and a timeout is pending. In that case hardware immediately
730 * returns the ERROR that looks like a destination timeout.
732 static void destination_plugged(struct bau_desc *bau_desc,
733 struct bau_control *bcp,
734 struct bau_control *hmaster, struct ptc_stats *stat)
736 udelay(bcp->plugged_delay);
737 bcp->plugged_tries++;
739 if (bcp->plugged_tries >= bcp->plugsb4reset) {
740 bcp->plugged_tries = 0;
742 quiesce_local_uvhub(hmaster);
744 spin_lock(&hmaster->queue_lock);
745 reset_with_ipi(&bau_desc->distribution, bcp);
746 spin_unlock(&hmaster->queue_lock);
748 end_uvhub_quiesce(hmaster);
751 stat->s_resets_plug++;
755 static void destination_timeout(struct bau_desc *bau_desc,
756 struct bau_control *bcp, struct bau_control *hmaster,
757 struct ptc_stats *stat)
759 hmaster->max_concurr = 1;
760 bcp->timeout_tries++;
761 if (bcp->timeout_tries >= bcp->timeoutsb4reset) {
762 bcp->timeout_tries = 0;
764 quiesce_local_uvhub(hmaster);
766 spin_lock(&hmaster->queue_lock);
767 reset_with_ipi(&bau_desc->distribution, bcp);
768 spin_unlock(&hmaster->queue_lock);
770 end_uvhub_quiesce(hmaster);
773 stat->s_resets_timeout++;
778 * Stop all cpus on a uvhub from using the BAU for a period of time.
779 * This is reversed by check_enable.
781 static void disable_for_period(struct bau_control *bcp, struct ptc_stats *stat)
784 struct bau_control *tbcp;
785 struct bau_control *hmaster;
788 hmaster = bcp->uvhub_master;
789 spin_lock(&hmaster->disable_lock);
790 if (!bcp->baudisabled) {
791 stat->s_bau_disabled++;
793 for_each_present_cpu(tcpu) {
794 tbcp = &per_cpu(bau_control, tcpu);
795 if (tbcp->uvhub_master == hmaster) {
796 tbcp->baudisabled = 1;
797 tbcp->set_bau_on_time =
798 tm1 + bcp->disabled_period;
802 spin_unlock(&hmaster->disable_lock);
805 static void count_max_concurr(int stat, struct bau_control *bcp,
806 struct bau_control *hmaster)
808 bcp->plugged_tries = 0;
809 bcp->timeout_tries = 0;
810 if (stat != FLUSH_COMPLETE)
812 if (bcp->conseccompletes <= bcp->complete_threshold)
814 if (hmaster->max_concurr >= hmaster->max_concurr_const)
816 hmaster->max_concurr++;
819 static void record_send_stats(cycles_t time1, cycles_t time2,
820 struct bau_control *bcp, struct ptc_stats *stat,
821 int completion_status, int try)
826 elapsed = time2 - time1;
827 stat->s_time += elapsed;
829 if ((completion_status == FLUSH_COMPLETE) && (try == 1)) {
830 bcp->period_requests++;
831 bcp->period_time += elapsed;
832 if ((elapsed > congested_cycles) &&
833 (bcp->period_requests > bcp->cong_reps) &&
834 ((bcp->period_time / bcp->period_requests) >
837 disable_for_period(bcp, stat);
843 if (completion_status == FLUSH_COMPLETE && try > 1)
845 else if (completion_status == FLUSH_GIVEUP) {
847 if (get_cycles() > bcp->period_end)
848 bcp->period_giveups = 0;
849 bcp->period_giveups++;
850 if (bcp->period_giveups == 1)
851 bcp->period_end = get_cycles() + bcp->disabled_period;
852 if (bcp->period_giveups > bcp->giveup_limit) {
853 disable_for_period(bcp, stat);
854 stat->s_giveuplimit++;
860 * Because of a uv1 hardware bug only a limited number of concurrent
861 * requests can be made.
863 static void uv1_throttle(struct bau_control *hmaster, struct ptc_stats *stat)
865 spinlock_t *lock = &hmaster->uvhub_lock;
868 v = &hmaster->active_descriptor_count;
869 if (!atomic_inc_unless_ge(lock, v, hmaster->max_concurr)) {
873 } while (!atomic_inc_unless_ge(lock, v, hmaster->max_concurr));
878 * Handle the completion status of a message send.
880 static void handle_cmplt(int completion_status, struct bau_desc *bau_desc,
881 struct bau_control *bcp, struct bau_control *hmaster,
882 struct ptc_stats *stat)
884 if (completion_status == FLUSH_RETRY_PLUGGED)
885 destination_plugged(bau_desc, bcp, hmaster, stat);
886 else if (completion_status == FLUSH_RETRY_TIMEOUT)
887 destination_timeout(bau_desc, bcp, hmaster, stat);
891 * Send a broadcast and wait for it to complete.
893 * The flush_mask contains the cpus the broadcast is to be sent to including
894 * cpus that are on the local uvhub.
896 * Returns 0 if all flushing represented in the mask was done.
897 * Returns 1 if it gives up entirely and the original cpu mask is to be
898 * returned to the kernel.
900 static int uv_flush_send_and_wait(struct cpumask *flush_mask,
901 struct bau_control *bcp,
902 struct bau_desc *bau_desc)
905 int completion_stat = 0;
911 struct ptc_stats *stat = bcp->statp;
912 struct bau_control *hmaster = bcp->uvhub_master;
913 struct uv1_bau_msg_header *uv1_hdr = NULL;
914 struct uv2_3_bau_msg_header *uv2_3_hdr = NULL;
916 if (bcp->uvhub_version == UV_BAU_V1) {
918 uv1_throttle(hmaster, stat);
921 while (hmaster->uvhub_quiesce)
924 time1 = get_cycles();
926 uv1_hdr = &bau_desc->header.uv1_hdr;
929 uv2_3_hdr = &bau_desc->header.uv2_3_hdr;
934 uv1_hdr->msg_type = MSG_REGULAR;
936 uv2_3_hdr->msg_type = MSG_REGULAR;
937 seq_number = bcp->message_number++;
940 uv1_hdr->msg_type = MSG_RETRY;
942 uv2_3_hdr->msg_type = MSG_RETRY;
943 stat->s_retry_messages++;
947 uv1_hdr->sequence = seq_number;
949 uv2_3_hdr->sequence = seq_number;
950 index = (1UL << AS_PUSH_SHIFT) | bcp->uvhub_cpu;
951 bcp->send_message = get_cycles();
953 write_mmr_activation(index);
956 completion_stat = ops.wait_completion(bau_desc, bcp, try);
958 handle_cmplt(completion_stat, bau_desc, bcp, hmaster, stat);
960 if (bcp->ipi_attempts >= bcp->ipi_reset_limit) {
961 bcp->ipi_attempts = 0;
962 stat->s_overipilimit++;
963 completion_stat = FLUSH_GIVEUP;
967 } while ((completion_stat == FLUSH_RETRY_PLUGGED) ||
968 (completion_stat == FLUSH_RETRY_TIMEOUT));
970 time2 = get_cycles();
972 count_max_concurr(completion_stat, bcp, hmaster);
974 while (hmaster->uvhub_quiesce)
977 atomic_dec(&hmaster->active_descriptor_count);
979 record_send_stats(time1, time2, bcp, stat, completion_stat, try);
981 if (completion_stat == FLUSH_GIVEUP)
982 /* FLUSH_GIVEUP will fall back to using IPI's for tlb flush */
988 * The BAU is disabled for this uvhub. When the disabled time period has
989 * expired re-enable it.
990 * Return 0 if it is re-enabled for all cpus on this uvhub.
992 static int check_enable(struct bau_control *bcp, struct ptc_stats *stat)
995 struct bau_control *tbcp;
996 struct bau_control *hmaster;
998 hmaster = bcp->uvhub_master;
999 spin_lock(&hmaster->disable_lock);
1000 if (bcp->baudisabled && (get_cycles() >= bcp->set_bau_on_time)) {
1001 stat->s_bau_reenabled++;
1002 for_each_present_cpu(tcpu) {
1003 tbcp = &per_cpu(bau_control, tcpu);
1004 if (tbcp->uvhub_master == hmaster) {
1005 tbcp->baudisabled = 0;
1006 tbcp->period_requests = 0;
1007 tbcp->period_time = 0;
1008 tbcp->period_giveups = 0;
1011 spin_unlock(&hmaster->disable_lock);
1014 spin_unlock(&hmaster->disable_lock);
1018 static void record_send_statistics(struct ptc_stats *stat, int locals, int hubs,
1019 int remotes, struct bau_desc *bau_desc)
1021 stat->s_requestor++;
1022 stat->s_ntargcpu += remotes + locals;
1023 stat->s_ntargremotes += remotes;
1024 stat->s_ntarglocals += locals;
1026 /* uvhub statistics */
1027 hubs = bau_uvhub_weight(&bau_desc->distribution);
1029 stat->s_ntarglocaluvhub++;
1030 stat->s_ntargremoteuvhub += (hubs - 1);
1032 stat->s_ntargremoteuvhub += hubs;
1034 stat->s_ntarguvhub += hubs;
1037 stat->s_ntarguvhub16++;
1039 stat->s_ntarguvhub8++;
1041 stat->s_ntarguvhub4++;
1043 stat->s_ntarguvhub2++;
1045 stat->s_ntarguvhub1++;
1049 * Translate a cpu mask to the uvhub distribution mask in the BAU
1050 * activation descriptor.
1052 static int set_distrib_bits(struct cpumask *flush_mask, struct bau_control *bcp,
1053 struct bau_desc *bau_desc, int *localsp, int *remotesp)
1058 struct hub_and_pnode *hpp;
1060 for_each_cpu(cpu, flush_mask) {
1062 * The distribution vector is a bit map of pnodes, relative
1063 * to the partition base pnode (and the partition base nasid
1065 * Translate cpu to pnode and hub using a local memory array.
1067 hpp = &bcp->socket_master->thp[cpu];
1068 pnode = hpp->pnode - bcp->partition_base_pnode;
1069 bau_uvhub_set(pnode, &bau_desc->distribution);
1071 if (hpp->uvhub == bcp->uvhub)
1082 * globally purge translation cache of a virtual address or all TLB's
1083 * @cpumask: mask of all cpu's in which the address is to be removed
1084 * @mm: mm_struct containing virtual address range
1085 * @start: start virtual address to be removed from TLB
1086 * @end: end virtual address to be remove from TLB
1087 * @cpu: the current cpu
1089 * This is the entry point for initiating any UV global TLB shootdown.
1091 * Purges the translation caches of all specified processors of the given
1092 * virtual address, or purges all TLB's on specified processors.
1094 * The caller has derived the cpumask from the mm_struct. This function
1095 * is called only if there are bits set in the mask. (e.g. flush_tlb_page())
1097 * The cpumask is converted into a uvhubmask of the uvhubs containing
1100 * Note that this function should be called with preemption disabled.
1102 * Returns NULL if all remote flushing was done.
1103 * Returns pointer to cpumask if some remote flushing remains to be
1104 * done. The returned pointer is valid till preemption is re-enabled.
1106 const struct cpumask *uv_flush_tlb_others(const struct cpumask *cpumask,
1107 const struct flush_tlb_info *info)
1109 unsigned int cpu = smp_processor_id();
1110 int locals = 0, remotes = 0, hubs = 0;
1111 struct bau_desc *bau_desc;
1112 struct cpumask *flush_mask;
1113 struct ptc_stats *stat;
1114 struct bau_control *bcp;
1115 unsigned long descriptor_status, status, address;
1117 bcp = &per_cpu(bau_control, cpu);
1127 read_lmmr(UVH_LB_BAU_SB_ACTIVATION_STATUS_0);
1128 status = ((descriptor_status >> (bcp->uvhub_cpu *
1129 UV_ACT_STATUS_SIZE)) & UV_ACT_STATUS_MASK) << 1;
1130 if (status == UV2H_DESC_BUSY)
1135 /* bau was disabled due to slow response */
1136 if (bcp->baudisabled) {
1137 if (check_enable(bcp, stat)) {
1138 stat->s_ipifordisabled++;
1144 * Each sending cpu has a per-cpu mask which it fills from the caller's
1145 * cpu mask. All cpus are converted to uvhubs and copied to the
1146 * activation descriptor.
1148 flush_mask = (struct cpumask *)per_cpu(uv_flush_tlb_mask, cpu);
1149 /* don't actually do a shootdown of the local cpu */
1150 cpumask_andnot(flush_mask, cpumask, cpumask_of(cpu));
1152 if (cpumask_test_cpu(cpu, cpumask))
1153 stat->s_ntargself++;
1155 bau_desc = bcp->descriptor_base;
1156 bau_desc += (ITEMS_PER_DESC * bcp->uvhub_cpu);
1157 bau_uvhubs_clear(&bau_desc->distribution, UV_DISTRIBUTION_SIZE);
1158 if (set_distrib_bits(flush_mask, bcp, bau_desc, &locals, &remotes))
1161 record_send_statistics(stat, locals, hubs, remotes, bau_desc);
1163 if (!info->end || (info->end - info->start) <= PAGE_SIZE)
1164 address = info->start;
1166 address = TLB_FLUSH_ALL;
1168 switch (bcp->uvhub_version) {
1172 bau_desc->payload.uv1_2_3.address = address;
1173 bau_desc->payload.uv1_2_3.sending_cpu = cpu;
1176 bau_desc->payload.uv4.address = address;
1177 bau_desc->payload.uv4.sending_cpu = cpu;
1178 bau_desc->payload.uv4.qualifier = BAU_DESC_QUALIFIER;
1183 * uv_flush_send_and_wait returns 0 if all cpu's were messaged,
1184 * or 1 if it gave up and the original cpumask should be returned.
1186 if (!uv_flush_send_and_wait(flush_mask, bcp, bau_desc))
1193 * Search the message queue for any 'other' unprocessed message with the
1194 * same software acknowledge resource bit vector as the 'msg' message.
1196 static struct bau_pq_entry *find_another_by_swack(struct bau_pq_entry *msg,
1197 struct bau_control *bcp)
1199 struct bau_pq_entry *msg_next = msg + 1;
1200 unsigned char swack_vec = msg->swack_vec;
1202 if (msg_next > bcp->queue_last)
1203 msg_next = bcp->queue_first;
1204 while (msg_next != msg) {
1205 if ((msg_next->canceled == 0) && (msg_next->replied_to == 0) &&
1206 (msg_next->swack_vec == swack_vec))
1209 if (msg_next > bcp->queue_last)
1210 msg_next = bcp->queue_first;
1216 * UV2 needs to work around a bug in which an arriving message has not
1217 * set a bit in the UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE register.
1218 * Such a message must be ignored.
1220 void process_uv2_message(struct msg_desc *mdp, struct bau_control *bcp)
1222 unsigned long mmr_image;
1223 unsigned char swack_vec;
1224 struct bau_pq_entry *msg = mdp->msg;
1225 struct bau_pq_entry *other_msg;
1227 mmr_image = ops.read_l_sw_ack();
1228 swack_vec = msg->swack_vec;
1230 if ((swack_vec & mmr_image) == 0) {
1232 * This message was assigned a swack resource, but no
1233 * reserved acknowlegment is pending.
1234 * The bug has prevented this message from setting the MMR.
1237 * Some message has set the MMR 'pending' bit; it might have
1238 * been another message. Look for that message.
1240 other_msg = find_another_by_swack(msg, bcp);
1243 * There is another. Process this one but do not
1246 bau_process_message(mdp, bcp, 0);
1248 * Let the natural processing of that other message
1249 * acknowledge it. Don't get the processing of sw_ack's
1257 * Either the MMR shows this one pending a reply or there is no
1258 * other message using this sw_ack, so it is safe to acknowledge it.
1260 bau_process_message(mdp, bcp, 1);
1266 * The BAU message interrupt comes here. (registered by set_intr_gate)
1269 * We received a broadcast assist message.
1271 * Interrupts are disabled; this interrupt could represent
1272 * the receipt of several messages.
1274 * All cores/threads on this hub get this interrupt.
1275 * The last one to see it does the software ack.
1276 * (the resource will not be freed until noninterruptable cpus see this
1277 * interrupt; hardware may timeout the s/w ack and reply ERROR)
1279 void uv_bau_message_interrupt(struct pt_regs *regs)
1282 cycles_t time_start;
1283 struct bau_pq_entry *msg;
1284 struct bau_control *bcp;
1285 struct ptc_stats *stat;
1286 struct msg_desc msgdesc;
1289 time_start = get_cycles();
1291 bcp = &per_cpu(bau_control, smp_processor_id());
1294 msgdesc.queue_first = bcp->queue_first;
1295 msgdesc.queue_last = bcp->queue_last;
1297 msg = bcp->bau_msg_head;
1298 while (msg->swack_vec) {
1301 msgdesc.msg_slot = msg - msgdesc.queue_first;
1303 if (bcp->uvhub_version == UV_BAU_V2)
1304 process_uv2_message(&msgdesc, bcp);
1306 /* no error workaround for uv1 or uv3 */
1307 bau_process_message(&msgdesc, bcp, 1);
1310 if (msg > msgdesc.queue_last)
1311 msg = msgdesc.queue_first;
1312 bcp->bau_msg_head = msg;
1314 stat->d_time += (get_cycles() - time_start);
1322 * Each target uvhub (i.e. a uvhub that has cpu's) needs to have
1323 * shootdown message timeouts enabled. The timeout does not cause
1324 * an interrupt, but causes an error message to be returned to
1327 static void __init enable_timeouts(void)
1332 unsigned long mmr_image;
1334 nuvhubs = uv_num_possible_blades();
1336 for (uvhub = 0; uvhub < nuvhubs; uvhub++) {
1337 if (!uv_blade_nr_possible_cpus(uvhub))
1340 pnode = uv_blade_to_pnode(uvhub);
1341 mmr_image = read_mmr_misc_control(pnode);
1343 * Set the timeout period and then lock it in, in three
1344 * steps; captures and locks in the period.
1346 * To program the period, the SOFT_ACK_MODE must be off.
1348 mmr_image &= ~(1L << SOFTACK_MSHIFT);
1349 write_mmr_misc_control(pnode, mmr_image);
1351 * Set the 4-bit period.
1353 mmr_image &= ~((unsigned long)0xf << SOFTACK_PSHIFT);
1354 mmr_image |= (SOFTACK_TIMEOUT_PERIOD << SOFTACK_PSHIFT);
1355 write_mmr_misc_control(pnode, mmr_image);
1358 * Subsequent reversals of the timebase bit (3) cause an
1359 * immediate timeout of one or all INTD resources as
1360 * indicated in bits 2:0 (7 causes all of them to timeout).
1362 mmr_image |= (1L << SOFTACK_MSHIFT);
1364 /* do not touch the legacy mode bit */
1365 /* hw bug workaround; do not use extended status */
1366 mmr_image &= ~(1L << UV2_EXT_SHFT);
1367 } else if (is_uv3_hub()) {
1368 mmr_image &= ~(1L << PREFETCH_HINT_SHFT);
1369 mmr_image |= (1L << SB_STATUS_SHFT);
1371 write_mmr_misc_control(pnode, mmr_image);
1375 static void *ptc_seq_start(struct seq_file *file, loff_t *offset)
1377 if (*offset < num_possible_cpus())
1382 static void *ptc_seq_next(struct seq_file *file, void *data, loff_t *offset)
1385 if (*offset < num_possible_cpus())
1390 static void ptc_seq_stop(struct seq_file *file, void *data)
1395 * Display the statistics thru /proc/sgi_uv/ptc_statistics
1396 * 'data' points to the cpu number
1397 * Note: see the descriptions in stat_description[].
1399 static int ptc_seq_show(struct seq_file *file, void *data)
1401 struct ptc_stats *stat;
1402 struct bau_control *bcp;
1405 cpu = *(loff_t *)data;
1408 "# cpu bauoff sent stime self locals remotes ncpus localhub ");
1409 seq_puts(file, "remotehub numuvhubs numuvhubs16 numuvhubs8 ");
1411 "numuvhubs4 numuvhubs2 numuvhubs1 dto snacks retries ");
1413 "rok resetp resett giveup sto bz throt disable ");
1415 "enable wars warshw warwaits enters ipidis plugged ");
1417 "ipiover glim cong swack recv rtime all one mult ");
1418 seq_puts(file, "none retry canc nocan reset rcan\n");
1420 if (cpu < num_possible_cpus() && cpu_online(cpu)) {
1421 bcp = &per_cpu(bau_control, cpu);
1423 seq_printf(file, "cpu %d bau disabled\n", cpu);
1427 /* source side statistics */
1429 "cpu %d %d %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld ",
1430 cpu, bcp->nobau, stat->s_requestor,
1431 cycles_2_us(stat->s_time),
1432 stat->s_ntargself, stat->s_ntarglocals,
1433 stat->s_ntargremotes, stat->s_ntargcpu,
1434 stat->s_ntarglocaluvhub, stat->s_ntargremoteuvhub,
1435 stat->s_ntarguvhub, stat->s_ntarguvhub16);
1436 seq_printf(file, "%ld %ld %ld %ld %ld %ld ",
1437 stat->s_ntarguvhub8, stat->s_ntarguvhub4,
1438 stat->s_ntarguvhub2, stat->s_ntarguvhub1,
1439 stat->s_dtimeout, stat->s_strongnacks);
1440 seq_printf(file, "%ld %ld %ld %ld %ld %ld %ld %ld ",
1441 stat->s_retry_messages, stat->s_retriesok,
1442 stat->s_resets_plug, stat->s_resets_timeout,
1443 stat->s_giveup, stat->s_stimeout,
1444 stat->s_busy, stat->s_throttles);
1445 seq_printf(file, "%ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld ",
1446 stat->s_bau_disabled, stat->s_bau_reenabled,
1447 stat->s_uv2_wars, stat->s_uv2_wars_hw,
1448 stat->s_uv2_war_waits, stat->s_enters,
1449 stat->s_ipifordisabled, stat->s_plugged,
1450 stat->s_overipilimit, stat->s_giveuplimit,
1453 /* destination side statistics */
1455 "%lx %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld\n",
1456 ops.read_g_sw_ack(uv_cpu_to_pnode(cpu)),
1457 stat->d_requestee, cycles_2_us(stat->d_time),
1458 stat->d_alltlb, stat->d_onetlb, stat->d_multmsg,
1459 stat->d_nomsg, stat->d_retries, stat->d_canceled,
1460 stat->d_nocanceled, stat->d_resets,
1467 * Display the tunables thru debugfs
1469 static ssize_t tunables_read(struct file *file, char __user *userbuf,
1470 size_t count, loff_t *ppos)
1475 buf = kasprintf(GFP_KERNEL, "%s %s %s\n%d %d %d %d %d %d %d %d %d %d\n",
1476 "max_concur plugged_delay plugsb4reset timeoutsb4reset",
1477 "ipi_reset_limit complete_threshold congested_response_us",
1478 "congested_reps disabled_period giveup_limit",
1479 max_concurr, plugged_delay, plugsb4reset,
1480 timeoutsb4reset, ipi_reset_limit, complete_threshold,
1481 congested_respns_us, congested_reps, disabled_period,
1487 ret = simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
1493 * handle a write to /proc/sgi_uv/ptc_statistics
1494 * -1: reset the statistics
1495 * 0: display meaning of the statistics
1497 static ssize_t ptc_proc_write(struct file *file, const char __user *user,
1498 size_t count, loff_t *data)
1505 struct ptc_stats *stat;
1507 if (count == 0 || count > sizeof(optstr))
1509 if (copy_from_user(optstr, user, count))
1511 optstr[count - 1] = '\0';
1513 if (!strcmp(optstr, "on")) {
1516 } else if (!strcmp(optstr, "off")) {
1521 if (kstrtol(optstr, 10, &input_arg) < 0) {
1522 pr_debug("%s is invalid\n", optstr);
1526 if (input_arg == 0) {
1527 elements = ARRAY_SIZE(stat_description);
1528 pr_debug("# cpu: cpu number\n");
1529 pr_debug("Sender statistics:\n");
1530 for (i = 0; i < elements; i++)
1531 pr_debug("%s\n", stat_description[i]);
1532 } else if (input_arg == -1) {
1533 for_each_present_cpu(cpu) {
1534 stat = &per_cpu(ptcstats, cpu);
1535 memset(stat, 0, sizeof(struct ptc_stats));
1542 static int local_atoi(const char *name)
1549 val = 10*val+(*name-'0');
1558 * Parse the values written to /sys/kernel/debug/sgi_uv/bau_tunables.
1559 * Zero values reset them to defaults.
1561 static int parse_tunables_write(struct bau_control *bcp, char *instr,
1568 int e = ARRAY_SIZE(tunables);
1570 p = instr + strspn(instr, WHITESPACE);
1572 for (; *p; p = q + strspn(q, WHITESPACE)) {
1573 q = p + strcspn(p, WHITESPACE);
1579 pr_info("bau tunable error: should be %d values\n", e);
1583 p = instr + strspn(instr, WHITESPACE);
1585 for (cnt = 0; *p; p = q + strspn(q, WHITESPACE), cnt++) {
1586 q = p + strcspn(p, WHITESPACE);
1587 val = local_atoi(p);
1591 max_concurr = MAX_BAU_CONCURRENT;
1592 max_concurr_const = MAX_BAU_CONCURRENT;
1595 if (val < 1 || val > bcp->cpus_in_uvhub) {
1597 "Error: BAU max concurrent %d is invalid\n",
1602 max_concurr_const = val;
1606 *tunables[cnt].tunp = tunables[cnt].deflt;
1608 *tunables[cnt].tunp = val;
1618 * Handle a write to debugfs. (/sys/kernel/debug/sgi_uv/bau_tunables)
1620 static ssize_t tunables_write(struct file *file, const char __user *user,
1621 size_t count, loff_t *data)
1626 struct bau_control *bcp;
1628 if (count == 0 || count > sizeof(instr)-1)
1630 if (copy_from_user(instr, user, count))
1633 instr[count] = '\0';
1636 bcp = &per_cpu(bau_control, cpu);
1637 ret = parse_tunables_write(bcp, instr, count);
1642 for_each_present_cpu(cpu) {
1643 bcp = &per_cpu(bau_control, cpu);
1644 bcp->max_concurr = max_concurr;
1645 bcp->max_concurr_const = max_concurr;
1646 bcp->plugged_delay = plugged_delay;
1647 bcp->plugsb4reset = plugsb4reset;
1648 bcp->timeoutsb4reset = timeoutsb4reset;
1649 bcp->ipi_reset_limit = ipi_reset_limit;
1650 bcp->complete_threshold = complete_threshold;
1651 bcp->cong_response_us = congested_respns_us;
1652 bcp->cong_reps = congested_reps;
1653 bcp->disabled_period = sec_2_cycles(disabled_period);
1654 bcp->giveup_limit = giveup_limit;
1659 static const struct seq_operations uv_ptc_seq_ops = {
1660 .start = ptc_seq_start,
1661 .next = ptc_seq_next,
1662 .stop = ptc_seq_stop,
1663 .show = ptc_seq_show
1666 static int ptc_proc_open(struct inode *inode, struct file *file)
1668 return seq_open(file, &uv_ptc_seq_ops);
1671 static int tunables_open(struct inode *inode, struct file *file)
1676 static const struct file_operations proc_uv_ptc_operations = {
1677 .open = ptc_proc_open,
1679 .write = ptc_proc_write,
1680 .llseek = seq_lseek,
1681 .release = seq_release,
1684 static const struct file_operations tunables_fops = {
1685 .open = tunables_open,
1686 .read = tunables_read,
1687 .write = tunables_write,
1688 .llseek = default_llseek,
1691 static int __init uv_ptc_init(void)
1693 struct proc_dir_entry *proc_uv_ptc;
1695 if (!is_uv_system())
1698 proc_uv_ptc = proc_create(UV_PTC_BASENAME, 0444, NULL,
1699 &proc_uv_ptc_operations);
1701 pr_err("unable to create %s proc entry\n",
1706 tunables_dir = debugfs_create_dir(UV_BAU_TUNABLES_DIR, NULL);
1707 if (!tunables_dir) {
1708 pr_err("unable to create debugfs directory %s\n",
1709 UV_BAU_TUNABLES_DIR);
1712 tunables_file = debugfs_create_file(UV_BAU_TUNABLES_FILE, 0600,
1713 tunables_dir, NULL, &tunables_fops);
1714 if (!tunables_file) {
1715 pr_err("unable to create debugfs file %s\n",
1716 UV_BAU_TUNABLES_FILE);
1723 * Initialize the sending side's sending buffers.
1725 static void activation_descriptor_init(int node, int pnode, int base_pnode)
1734 struct bau_desc *bau_desc;
1735 struct bau_desc *bd2;
1736 struct uv1_bau_msg_header *uv1_hdr;
1737 struct uv2_3_bau_msg_header *uv2_3_hdr;
1738 struct bau_control *bcp;
1741 * each bau_desc is 64 bytes; there are 8 (ITEMS_PER_DESC)
1742 * per cpu; and one per cpu on the uvhub (ADP_SZ)
1744 dsize = sizeof(struct bau_desc) * ADP_SZ * ITEMS_PER_DESC;
1745 bau_desc = kmalloc_node(dsize, GFP_KERNEL, node);
1748 gpa = uv_gpa(bau_desc);
1749 n = uv_gpa_to_gnode(gpa);
1750 m = ops.bau_gpa_to_offset(gpa);
1754 /* the 14-bit pnode */
1755 write_mmr_descriptor_base(pnode, (n << UV_DESC_PSHIFT | m));
1757 * Initializing all 8 (ITEMS_PER_DESC) descriptors for each
1758 * cpu even though we only use the first one; one descriptor can
1759 * describe a broadcast to 256 uv hubs.
1761 for (i = 0, bd2 = bau_desc; i < (ADP_SZ * ITEMS_PER_DESC); i++, bd2++) {
1762 memset(bd2, 0, sizeof(struct bau_desc));
1764 uv1_hdr = &bd2->header.uv1_hdr;
1765 uv1_hdr->swack_flag = 1;
1767 * The base_dest_nasid set in the message header
1768 * is the nasid of the first uvhub in the partition.
1769 * The bit map will indicate destination pnode numbers
1770 * relative to that base. They may not be consecutive
1771 * if nasid striding is being used.
1773 uv1_hdr->base_dest_nasid =
1774 UV_PNODE_TO_NASID(base_pnode);
1775 uv1_hdr->dest_subnodeid = UV_LB_SUBNODEID;
1776 uv1_hdr->command = UV_NET_ENDPOINT_INTD;
1777 uv1_hdr->int_both = 1;
1779 * all others need to be set to zero:
1780 * fairness chaining multilevel count replied_to
1784 * BIOS uses legacy mode, but uv2 and uv3 hardware always
1785 * uses native mode for selective broadcasts.
1787 uv2_3_hdr = &bd2->header.uv2_3_hdr;
1788 uv2_3_hdr->swack_flag = 1;
1789 uv2_3_hdr->base_dest_nasid =
1790 UV_PNODE_TO_NASID(base_pnode);
1791 uv2_3_hdr->dest_subnodeid = UV_LB_SUBNODEID;
1792 uv2_3_hdr->command = UV_NET_ENDPOINT_INTD;
1795 for_each_present_cpu(cpu) {
1796 if (pnode != uv_blade_to_pnode(uv_cpu_to_blade_id(cpu)))
1798 bcp = &per_cpu(bau_control, cpu);
1799 bcp->descriptor_base = bau_desc;
1804 * initialize the destination side's receiving buffers
1805 * entered for each uvhub in the partition
1806 * - node is first node (kernel memory notion) on the uvhub
1807 * - pnode is the uvhub's physical identifier
1809 static void pq_init(int node, int pnode)
1815 unsigned long gnode, first, last, tail;
1816 struct bau_pq_entry *pqp;
1817 struct bau_control *bcp;
1819 plsize = (DEST_Q_SIZE + 1) * sizeof(struct bau_pq_entry);
1820 vp = kmalloc_node(plsize, GFP_KERNEL, node);
1821 pqp = (struct bau_pq_entry *)vp;
1824 cp = (char *)pqp + 31;
1825 pqp = (struct bau_pq_entry *)(((unsigned long)cp >> 5) << 5);
1827 for_each_present_cpu(cpu) {
1828 if (pnode != uv_cpu_to_pnode(cpu))
1830 /* for every cpu on this pnode: */
1831 bcp = &per_cpu(bau_control, cpu);
1832 bcp->queue_first = pqp;
1833 bcp->bau_msg_head = pqp;
1834 bcp->queue_last = pqp + (DEST_Q_SIZE - 1);
1837 first = ops.bau_gpa_to_offset(uv_gpa(pqp));
1838 last = ops.bau_gpa_to_offset(uv_gpa(pqp + (DEST_Q_SIZE - 1)));
1841 * Pre UV4, the gnode is required to locate the payload queue
1842 * and the payload queue tail must be maintained by the kernel.
1844 bcp = &per_cpu(bau_control, smp_processor_id());
1845 if (bcp->uvhub_version <= UV_BAU_V3) {
1847 gnode = uv_gpa_to_gnode(uv_gpa(pqp));
1848 first = (gnode << UV_PAYLOADQ_GNODE_SHIFT) | tail;
1849 write_mmr_payload_tail(pnode, tail);
1852 ops.write_payload_first(pnode, first);
1853 ops.write_payload_last(pnode, last);
1855 /* in effect, all msg_type's are set to MSG_NOOP */
1856 memset(pqp, 0, sizeof(struct bau_pq_entry) * DEST_Q_SIZE);
1860 * Initialization of each UV hub's structures
1862 static void __init init_uvhub(int uvhub, int vector, int base_pnode)
1866 unsigned long apicid;
1868 node = uvhub_to_first_node(uvhub);
1869 pnode = uv_blade_to_pnode(uvhub);
1871 activation_descriptor_init(node, pnode, base_pnode);
1873 pq_init(node, pnode);
1875 * The below initialization can't be in firmware because the
1876 * messaging IRQ will be determined by the OS.
1878 apicid = uvhub_to_first_apicid(uvhub) | uv_apicid_hibits;
1879 write_mmr_data_config(pnode, ((apicid << 32) | vector));
1883 * We will set BAU_MISC_CONTROL with a timeout period.
1884 * But the BIOS has set UVH_AGING_PRESCALE_SEL and UVH_TRANSACTION_TIMEOUT.
1885 * So the destination timeout period has to be calculated from them.
1887 static int calculate_destination_timeout(void)
1889 unsigned long mmr_image;
1895 unsigned long ts_ns;
1898 mult1 = SOFTACK_TIMEOUT_PERIOD & BAU_MISC_CONTROL_MULT_MASK;
1899 mmr_image = uv_read_local_mmr(UVH_AGING_PRESCALE_SEL);
1900 index = (mmr_image >> BAU_URGENCY_7_SHIFT) & BAU_URGENCY_7_MASK;
1901 mmr_image = uv_read_local_mmr(UVH_TRANSACTION_TIMEOUT);
1902 mult2 = (mmr_image >> BAU_TRANS_SHIFT) & BAU_TRANS_MASK;
1903 ts_ns = timeout_base_ns[index];
1904 ts_ns *= (mult1 * mult2);
1907 /* same destination timeout for uv2 and uv3 */
1908 /* 4 bits 0/1 for 10/80us base, 3 bits of multiplier */
1909 mmr_image = uv_read_local_mmr(UVH_LB_BAU_MISC_CONTROL);
1910 mmr_image = (mmr_image & UV_SA_MASK) >> UV_SA_SHFT;
1911 if (mmr_image & (1L << UV2_ACK_UNITS_SHFT))
1915 mult1 = mmr_image & UV2_ACK_MASK;
1921 static void __init init_per_cpu_tunables(void)
1924 struct bau_control *bcp;
1926 for_each_present_cpu(cpu) {
1927 bcp = &per_cpu(bau_control, cpu);
1928 bcp->baudisabled = 0;
1931 bcp->statp = &per_cpu(ptcstats, cpu);
1932 /* time interval to catch a hardware stay-busy bug */
1933 bcp->timeout_interval = usec_2_cycles(2*timeout_us);
1934 bcp->max_concurr = max_concurr;
1935 bcp->max_concurr_const = max_concurr;
1936 bcp->plugged_delay = plugged_delay;
1937 bcp->plugsb4reset = plugsb4reset;
1938 bcp->timeoutsb4reset = timeoutsb4reset;
1939 bcp->ipi_reset_limit = ipi_reset_limit;
1940 bcp->complete_threshold = complete_threshold;
1941 bcp->cong_response_us = congested_respns_us;
1942 bcp->cong_reps = congested_reps;
1943 bcp->disabled_period = sec_2_cycles(disabled_period);
1944 bcp->giveup_limit = giveup_limit;
1945 spin_lock_init(&bcp->queue_lock);
1946 spin_lock_init(&bcp->uvhub_lock);
1947 spin_lock_init(&bcp->disable_lock);
1952 * Scan all cpus to collect blade and socket summaries.
1954 static int __init get_cpu_topology(int base_pnode,
1955 struct uvhub_desc *uvhub_descs,
1956 unsigned char *uvhub_mask)
1962 struct bau_control *bcp;
1963 struct uvhub_desc *bdp;
1964 struct socket_desc *sdp;
1966 for_each_present_cpu(cpu) {
1967 bcp = &per_cpu(bau_control, cpu);
1969 memset(bcp, 0, sizeof(struct bau_control));
1971 pnode = uv_cpu_hub_info(cpu)->pnode;
1972 if ((pnode - base_pnode) >= UV_DISTRIBUTION_SIZE) {
1974 "cpu %d pnode %d-%d beyond %d; BAU disabled\n",
1975 cpu, pnode, base_pnode, UV_DISTRIBUTION_SIZE);
1979 bcp->osnode = cpu_to_node(cpu);
1980 bcp->partition_base_pnode = base_pnode;
1982 uvhub = uv_cpu_hub_info(cpu)->numa_blade_id;
1983 *(uvhub_mask + (uvhub/8)) |= (1 << (uvhub%8));
1984 bdp = &uvhub_descs[uvhub];
1990 /* kludge: 'assuming' one node per socket, and assuming that
1991 disabling a socket just leaves a gap in node numbers */
1992 socket = bcp->osnode & 1;
1993 bdp->socket_mask |= (1 << socket);
1994 sdp = &bdp->socket[socket];
1995 sdp->cpu_number[sdp->num_cpus] = cpu;
1997 if (sdp->num_cpus > MAX_CPUS_PER_SOCKET) {
1998 pr_emerg("%d cpus per socket invalid\n",
2007 * Each socket is to get a local array of pnodes/hubs.
2009 static void make_per_cpu_thp(struct bau_control *smaster)
2012 size_t hpsz = sizeof(struct hub_and_pnode) * num_possible_cpus();
2014 smaster->thp = kmalloc_node(hpsz, GFP_KERNEL, smaster->osnode);
2015 memset(smaster->thp, 0, hpsz);
2016 for_each_present_cpu(cpu) {
2017 smaster->thp[cpu].pnode = uv_cpu_hub_info(cpu)->pnode;
2018 smaster->thp[cpu].uvhub = uv_cpu_hub_info(cpu)->numa_blade_id;
2023 * Each uvhub is to get a local cpumask.
2025 static void make_per_hub_cpumask(struct bau_control *hmaster)
2027 int sz = sizeof(cpumask_t);
2029 hmaster->cpumask = kzalloc_node(sz, GFP_KERNEL, hmaster->osnode);
2033 * Initialize all the per_cpu information for the cpu's on a given socket,
2034 * given what has been gathered into the socket_desc struct.
2035 * And reports the chosen hub and socket masters back to the caller.
2037 static int scan_sock(struct socket_desc *sdp, struct uvhub_desc *bdp,
2038 struct bau_control **smasterp,
2039 struct bau_control **hmasterp)
2041 int i, cpu, uvhub_cpu;
2042 struct bau_control *bcp;
2044 for (i = 0; i < sdp->num_cpus; i++) {
2045 cpu = sdp->cpu_number[i];
2046 bcp = &per_cpu(bau_control, cpu);
2053 bcp->cpus_in_uvhub = bdp->num_cpus;
2054 bcp->cpus_in_socket = sdp->num_cpus;
2055 bcp->socket_master = *smasterp;
2056 bcp->uvhub = bdp->uvhub;
2058 bcp->uvhub_version = UV_BAU_V1;
2059 else if (is_uv2_hub())
2060 bcp->uvhub_version = UV_BAU_V2;
2061 else if (is_uv3_hub())
2062 bcp->uvhub_version = UV_BAU_V3;
2063 else if (is_uv4_hub())
2064 bcp->uvhub_version = UV_BAU_V4;
2066 pr_emerg("uvhub version not 1, 2, 3, or 4\n");
2069 bcp->uvhub_master = *hmasterp;
2070 uvhub_cpu = uv_cpu_blade_processor_id(cpu);
2071 bcp->uvhub_cpu = uvhub_cpu;
2074 * The ERROR and BUSY status registers are located pairwise over
2075 * the STATUS_0 and STATUS_1 mmrs; each an array[32] of 2 bits.
2077 if (uvhub_cpu < UV_CPUS_PER_AS) {
2078 bcp->status_mmr = UVH_LB_BAU_SB_ACTIVATION_STATUS_0;
2079 bcp->status_index = uvhub_cpu * UV_ACT_STATUS_SIZE;
2081 bcp->status_mmr = UVH_LB_BAU_SB_ACTIVATION_STATUS_1;
2082 bcp->status_index = (uvhub_cpu - UV_CPUS_PER_AS)
2083 * UV_ACT_STATUS_SIZE;
2086 if (bcp->uvhub_cpu >= MAX_CPUS_PER_UVHUB) {
2087 pr_emerg("%d cpus per uvhub invalid\n",
2096 * Summarize the blade and socket topology into the per_cpu structures.
2098 static int __init summarize_uvhub_sockets(int nuvhubs,
2099 struct uvhub_desc *uvhub_descs,
2100 unsigned char *uvhub_mask)
2104 unsigned short socket_mask;
2106 for (uvhub = 0; uvhub < nuvhubs; uvhub++) {
2107 struct uvhub_desc *bdp;
2108 struct bau_control *smaster = NULL;
2109 struct bau_control *hmaster = NULL;
2111 if (!(*(uvhub_mask + (uvhub/8)) & (1 << (uvhub%8))))
2114 bdp = &uvhub_descs[uvhub];
2115 socket_mask = bdp->socket_mask;
2117 while (socket_mask) {
2118 struct socket_desc *sdp;
2119 if ((socket_mask & 1)) {
2120 sdp = &bdp->socket[socket];
2121 if (scan_sock(sdp, bdp, &smaster, &hmaster))
2123 make_per_cpu_thp(smaster);
2126 socket_mask = (socket_mask >> 1);
2128 make_per_hub_cpumask(hmaster);
2134 * initialize the bau_control structure for each cpu
2136 static int __init init_per_cpu(int nuvhubs, int base_part_pnode)
2138 unsigned char *uvhub_mask;
2140 struct uvhub_desc *uvhub_descs;
2142 if (is_uv3_hub() || is_uv2_hub() || is_uv1_hub())
2143 timeout_us = calculate_destination_timeout();
2145 vp = kmalloc(nuvhubs * sizeof(struct uvhub_desc), GFP_KERNEL);
2146 uvhub_descs = (struct uvhub_desc *)vp;
2147 memset(uvhub_descs, 0, nuvhubs * sizeof(struct uvhub_desc));
2148 uvhub_mask = kzalloc((nuvhubs+7)/8, GFP_KERNEL);
2150 if (get_cpu_topology(base_part_pnode, uvhub_descs, uvhub_mask))
2153 if (summarize_uvhub_sockets(nuvhubs, uvhub_descs, uvhub_mask))
2158 init_per_cpu_tunables();
2167 static const struct bau_operations uv1_bau_ops __initconst = {
2168 .bau_gpa_to_offset = uv_gpa_to_offset,
2169 .read_l_sw_ack = read_mmr_sw_ack,
2170 .read_g_sw_ack = read_gmmr_sw_ack,
2171 .write_l_sw_ack = write_mmr_sw_ack,
2172 .write_g_sw_ack = write_gmmr_sw_ack,
2173 .write_payload_first = write_mmr_payload_first,
2174 .write_payload_last = write_mmr_payload_last,
2175 .wait_completion = uv1_wait_completion,
2178 static const struct bau_operations uv2_3_bau_ops __initconst = {
2179 .bau_gpa_to_offset = uv_gpa_to_offset,
2180 .read_l_sw_ack = read_mmr_sw_ack,
2181 .read_g_sw_ack = read_gmmr_sw_ack,
2182 .write_l_sw_ack = write_mmr_sw_ack,
2183 .write_g_sw_ack = write_gmmr_sw_ack,
2184 .write_payload_first = write_mmr_payload_first,
2185 .write_payload_last = write_mmr_payload_last,
2186 .wait_completion = uv2_3_wait_completion,
2189 static const struct bau_operations uv4_bau_ops __initconst = {
2190 .bau_gpa_to_offset = uv_gpa_to_soc_phys_ram,
2191 .read_l_sw_ack = read_mmr_proc_sw_ack,
2192 .read_g_sw_ack = read_gmmr_proc_sw_ack,
2193 .write_l_sw_ack = write_mmr_proc_sw_ack,
2194 .write_g_sw_ack = write_gmmr_proc_sw_ack,
2195 .write_payload_first = write_mmr_proc_payload_first,
2196 .write_payload_last = write_mmr_proc_payload_last,
2197 .wait_completion = uv4_wait_completion,
2201 * Initialization of BAU-related structures
2203 static int __init uv_bau_init(void)
2211 cpumask_var_t *mask;
2213 if (!is_uv_system())
2218 else if (is_uv3_hub())
2219 ops = uv2_3_bau_ops;
2220 else if (is_uv2_hub())
2221 ops = uv2_3_bau_ops;
2222 else if (is_uv1_hub())
2225 for_each_possible_cpu(cur_cpu) {
2226 mask = &per_cpu(uv_flush_tlb_mask, cur_cpu);
2227 zalloc_cpumask_var_node(mask, GFP_KERNEL, cpu_to_node(cur_cpu));
2230 nuvhubs = uv_num_possible_blades();
2231 congested_cycles = usec_2_cycles(congested_respns_us);
2233 uv_base_pnode = 0x7fffffff;
2234 for (uvhub = 0; uvhub < nuvhubs; uvhub++) {
2235 cpus = uv_blade_nr_possible_cpus(uvhub);
2236 if (cpus && (uv_blade_to_pnode(uvhub) < uv_base_pnode))
2237 uv_base_pnode = uv_blade_to_pnode(uvhub);
2240 /* software timeouts are not supported on UV4 */
2241 if (is_uv3_hub() || is_uv2_hub() || is_uv1_hub())
2244 if (init_per_cpu(nuvhubs, uv_base_pnode)) {
2250 vector = UV_BAU_MESSAGE;
2251 for_each_possible_blade(uvhub) {
2252 if (uv_blade_nr_possible_cpus(uvhub))
2253 init_uvhub(uvhub, vector, uv_base_pnode);
2256 alloc_intr_gate(vector, uv_bau_message_intr1);
2258 for_each_possible_blade(uvhub) {
2259 if (uv_blade_nr_possible_cpus(uvhub)) {
2262 pnode = uv_blade_to_pnode(uvhub);
2265 write_gmmr_activation(pnode, val);
2266 mmr = 1; /* should be 1 to broadcast to both sockets */
2268 write_mmr_data_broadcast(pnode, mmr);
2274 core_initcall(uv_bau_init);
2275 fs_initcall(uv_ptc_init);