2 * SGI UltraViolet TLB flush routines.
4 * (c) 2008 Cliff Wickman <cpw@sgi.com>, SGI.
6 * This code is released under the GNU General Public License version 2 or
9 #include <linux/mc146818rtc.h>
10 #include <linux/proc_fs.h>
11 #include <linux/kernel.h>
13 #include <asm/mach-bigsmp/mach_apic.h>
14 #include <asm/mmu_context.h>
16 #include <asm/genapic.h>
17 #include <asm/uv/uv_hub.h>
18 #include <asm/uv/uv_mmrs.h>
19 #include <asm/uv/uv_bau.h>
21 struct bau_control **uv_bau_table_bases;
22 static int uv_bau_retry_limit;
23 static int uv_nshift; /* position of pnode (which is nasid>>1) */
24 static unsigned long uv_mmask;
26 char *status_table[] = {
29 "DESTINATION TIMEOUT",
33 DEFINE_PER_CPU(struct ptc_stats, ptcstats);
34 DEFINE_PER_CPU(struct bau_control, bau_control);
37 * Free a software acknowledge hardware resource by clearing its Pending
38 * bit. This will return a reply to the sender.
39 * If the message has timed out, a reply has already been sent by the
40 * hardware but the resource has not been released. In that case our
41 * clear of the Timeout bit (as well) will free the resource. No reply will
42 * be sent (the hardware will only do one reply per message).
45 uv_reply_to_message(int resource,
46 struct bau_payload_queue_entry *msg,
47 struct bau_msg_status *msp)
51 fw = (1 << (resource + UV_SW_ACK_NPENDING)) | (1 << resource);
53 msg->sw_ack_vector = 0;
55 msp->seen_by.bits = 0;
56 uv_write_local_mmr(UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE_ALIAS, fw);
61 * Do all the things a cpu should do for a TLB shootdown message.
62 * Other cpu's may come here at the same time for this message.
65 uv_bau_process_message(struct bau_payload_queue_entry *msg,
66 int msg_slot, int sw_ack_slot)
69 unsigned long this_cpu_mask;
70 struct bau_msg_status *msp;
72 msp = __get_cpu_var(bau_control).msg_statuses + msg_slot;
73 cpu = uv_blade_processor_id();
75 uv_blade_nr_online_cpus(uv_node_to_blade_id(numa_node_id()));
76 this_cpu_mask = (unsigned long)1 << cpu;
77 if (msp->seen_by.bits & this_cpu_mask)
79 atomic_or_long(&msp->seen_by.bits, this_cpu_mask);
81 if (msg->replied_to == 1)
84 if (msg->address == TLB_FLUSH_ALL) {
86 __get_cpu_var(ptcstats).alltlb++;
88 __flush_tlb_one(msg->address);
89 __get_cpu_var(ptcstats).onetlb++;
92 __get_cpu_var(ptcstats).requestee++;
94 atomic_inc_short(&msg->acknowledge_count);
95 if (msg->number_of_cpus == msg->acknowledge_count)
96 uv_reply_to_message(sw_ack_slot, msg, msp);
101 * Examine the payload queue on all the distribution nodes to see
102 * which messages have not been seen, and which cpu(s) have not seen them.
104 * Returns the number of cpu's that have not responded.
107 uv_examine_destinations(struct bau_target_nodemask *distribution)
114 struct bau_control *bau_tablesp;
115 struct bau_payload_queue_entry *msg;
116 struct bau_msg_status *msp;
118 sender = smp_processor_id();
119 for (i = 0; i < (sizeof(struct bau_target_nodemask) * BITSPERBYTE);
121 if (bau_node_isset(i, distribution)) {
122 bau_tablesp = uv_bau_table_bases[i];
123 for (msg = bau_tablesp->va_queue_first, j = 0;
124 j < DESTINATION_PAYLOAD_QUEUE_SIZE; msg++, j++) {
125 if ((msg->sending_cpu == sender) &&
126 (!msg->replied_to)) {
127 msp = bau_tablesp->msg_statuses + j;
129 "blade %d: address:%#lx %d of %d, not cpu(s): ",
131 msg->acknowledge_count,
132 msg->number_of_cpus);
133 for (k = 0; k < msg->number_of_cpus;
135 if (!((long)1 << k & msp->
150 * uv_flush_tlb_others - globally purge translation cache of a virtual
151 * address or all TLB's
152 * @cpumaskp: mask of all cpu's in which the address is to be removed
153 * @mm: mm_struct containing virtual address range
154 * @va: virtual address to be removed (or TLB_FLUSH_ALL for all TLB's on cpu)
156 * This is the entry point for initiating any UV global TLB shootdown.
158 * Purges the translation caches of all specified processors of the given
159 * virtual address, or purges all TLB's on specified processors.
161 * The caller has derived the cpumaskp from the mm_struct and has subtracted
162 * the local cpu from the mask. This function is called only if there
163 * are bits set in the mask. (e.g. flush_tlb_page())
165 * The cpumaskp is converted into a nodemask of the nodes containing
169 uv_flush_tlb_others(cpumask_t *cpumaskp, struct mm_struct *mm, unsigned long va)
179 long source_timeouts = 0;
180 long destination_timeouts = 0;
182 unsigned long mmr_offset;
183 unsigned long descriptor_status;
184 struct bau_activation_descriptor *bau_desc;
185 ktime_t time1, time2;
187 cpu = uv_blade_processor_id();
188 this_blade = uv_numa_blade_id();
189 bau_desc = __get_cpu_var(bau_control).descriptor_base;
190 bau_desc += (UV_ITEMS_PER_DESCRIPTOR * cpu);
192 bau_nodes_clear(&bau_desc->distribution, UV_DISTRIBUTION_SIZE);
195 for_each_cpu_mask(bit, *cpumaskp) {
196 blade = uv_cpu_to_blade_id(bit);
197 if (blade > (UV_DISTRIBUTION_SIZE - 1))
199 if (blade == this_blade)
201 bau_node_set(blade, &bau_desc->distribution);
202 /* leave the bits for the remote cpu's in the mask until
203 success; on failure we fall back to the IPI method */
208 __get_cpu_var(ptcstats).requestor++;
209 __get_cpu_var(ptcstats).ntargeted += i;
211 bau_desc->payload.address = va;
212 bau_desc->payload.sending_cpu = smp_processor_id();
214 if (cpu < UV_CPUS_PER_ACT_STATUS) {
215 mmr_offset = UVH_LB_BAU_SB_ACTIVATION_STATUS_0;
216 right_shift = cpu * UV_ACT_STATUS_SIZE;
218 mmr_offset = UVH_LB_BAU_SB_ACTIVATION_STATUS_1;
220 ((cpu - UV_CPUS_PER_ACT_STATUS) * UV_ACT_STATUS_SIZE);
226 index = ((unsigned long)
227 1 << UVH_LB_BAU_SB_ACTIVATION_CONTROL_PUSH_SHFT) | cpu;
228 uv_write_local_mmr(UVH_LB_BAU_SB_ACTIVATION_CONTROL, index);
230 while ((descriptor_status = (((unsigned long)
231 uv_read_local_mmr(mmr_offset) >>
232 right_shift) & UV_ACT_STATUS_MASK)) !=
234 if (descriptor_status == DESC_STATUS_SOURCE_TIMEOUT) {
236 if (source_timeouts > SOURCE_TIMEOUT_LIMIT)
238 __get_cpu_var(ptcstats).s_retry++;
241 /* spin here looking for progress at the destinations */
242 if (descriptor_status == DESC_STATUS_DESTINATION_TIMEOUT) {
243 destination_timeouts++;
244 if (destination_timeouts > DESTINATION_TIMEOUT_LIMIT) {
245 /* returns # of cpus not responding */
246 if (uv_examine_destinations
247 (&bau_desc->distribution) == 0) {
248 __get_cpu_var(ptcstats).d_retry++;
252 if (exams >= uv_bau_retry_limit) {
254 "uv_flush_tlb_others");
255 printk("giving up on cpu %d\n",
259 /* delays can hang up the simulator
262 destination_timeouts = 0;
267 __get_cpu_var(ptcstats).retriesok++;
268 /* on success, clear the remote cpu's from the mask so we don't
269 use the IPI method of shootdown on them */
270 for_each_cpu_mask(bit, *cpumaskp) {
271 blade = uv_cpu_to_blade_id(bit);
272 if (blade == this_blade)
274 cpu_clear(bit, *cpumaskp);
279 __get_cpu_var(ptcstats).sflush_ns += (time2.tv64 - time1.tv64);
282 if (cpus_empty(*cpumaskp))
285 /* Cause the caller to do an IPI-style TLB shootdown on
286 the cpu's still in the mask */
287 __get_cpu_var(ptcstats).ptc_i++;
292 * The BAU message interrupt comes here. (registered by set_intr_gate)
295 * We received a broadcast assist message.
297 * Interrupts may have been disabled; this interrupt could represent
298 * the receipt of several messages.
300 * All cores/threads on this node get this interrupt.
301 * The last one to see it does the s/w ack.
302 * (the resource will not be freed until noninterruptable cpus see this
303 * interrupt; hardware will timeout the s/w ack and reply ERROR)
306 uv_bau_message_interrupt(struct pt_regs *regs)
308 struct bau_payload_queue_entry *pqp;
309 struct bau_payload_queue_entry *msg;
310 struct pt_regs *old_regs = set_irq_regs(regs);
311 ktime_t time1, time2;
316 unsigned long local_pnode;
324 local_pnode = uv_blade_to_pnode(uv_numa_blade_id());
326 pqp = __get_cpu_var(bau_control).va_queue_first;
327 msg = __get_cpu_var(bau_control).bau_msg_head;
328 while (msg->sw_ack_vector) {
330 fw = msg->sw_ack_vector;
331 msg_slot = msg - pqp;
332 sw_ack_slot = ffs(fw) - 1;
334 uv_bau_process_message(msg, msg_slot, sw_ack_slot);
337 if (msg > __get_cpu_var(bau_control).va_queue_last)
338 msg = __get_cpu_var(bau_control).va_queue_first;
339 __get_cpu_var(bau_control).bau_msg_head = msg;
342 __get_cpu_var(ptcstats).nomsg++;
344 __get_cpu_var(ptcstats).multmsg++;
347 __get_cpu_var(ptcstats).dflush_ns += (time2.tv64 - time1.tv64);
350 set_irq_regs(old_regs);
355 uv_enable_timeouts(void)
362 unsigned long apicid;
364 /* better if we had each_online_blade */
366 for_each_online_node(i) {
367 blade = uv_node_to_blade_id(i);
368 if (blade == last_blade)
371 apicid = per_cpu(x86_cpu_to_apicid, cur_cpu);
372 pnode = uv_blade_to_pnode(blade);
373 cur_cpu += uv_blade_nr_possible_cpus(i);
379 uv_ptc_seq_start(struct seq_file *file, loff_t *offset)
381 if (*offset < num_possible_cpus())
387 uv_ptc_seq_next(struct seq_file *file, void *data, loff_t *offset)
390 if (*offset < num_possible_cpus())
396 uv_ptc_seq_stop(struct seq_file *file, void *data)
401 * Display the statistics thru /proc
402 * data points to the cpu number
405 uv_ptc_seq_show(struct seq_file *file, void *data)
407 struct ptc_stats *stat;
410 cpu = *(loff_t *)data;
414 "# cpu requestor requestee one all sretry dretry ptc_i ");
416 "sw_ack sflush_us dflush_us sok dnomsg dmult starget\n");
418 if (cpu < num_possible_cpus() && cpu_online(cpu)) {
419 stat = &per_cpu(ptcstats, cpu);
420 seq_printf(file, "cpu %d %ld %ld %ld %ld %ld %ld %ld ",
421 cpu, stat->requestor,
422 stat->requestee, stat->onetlb, stat->alltlb,
423 stat->s_retry, stat->d_retry, stat->ptc_i);
424 seq_printf(file, "%lx %ld %ld %ld %ld %ld %ld\n",
425 uv_read_global_mmr64(uv_blade_to_pnode
426 (uv_cpu_to_blade_id(cpu)),
427 UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE),
428 stat->sflush_ns / 1000, stat->dflush_ns / 1000,
429 stat->retriesok, stat->nomsg,
430 stat->multmsg, stat->ntargeted);
437 * 0: display meaning of the statistics
441 uv_ptc_proc_write(struct file *file, const char __user *user,
442 size_t count, loff_t *data)
447 if (copy_from_user(optstr, user, count))
449 optstr[count - 1] = '\0';
450 if (strict_strtoul(optstr, 10, &newmode) < 0) {
451 printk(KERN_DEBUG "%s is invalid\n", optstr);
456 printk(KERN_DEBUG "# cpu: cpu number\n");
458 "requestor: times this cpu was the flush requestor\n");
460 "requestee: times this cpu was requested to flush its TLBs\n");
462 "one: times requested to flush a single address\n");
464 "all: times requested to flush all TLB's\n");
466 "sretry: number of retries of source-side timeouts\n");
468 "dretry: number of retries of destination-side timeouts\n");
470 "ptc_i: times UV fell through to IPI-style flushes\n");
472 "sw_ack: image of UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE\n");
474 "sflush_us: microseconds spent in uv_flush_tlb_others()\n");
476 "dflush_us: microseconds spent in handling flush requests\n");
477 printk(KERN_DEBUG "sok: successes on retry\n");
478 printk(KERN_DEBUG "dnomsg: interrupts with no message\n");
480 "dmult: interrupts with multiple messages\n");
481 printk(KERN_DEBUG "starget: nodes targeted\n");
483 uv_bau_retry_limit = newmode;
484 printk(KERN_DEBUG "timeout retry limit:%d\n",
491 static const struct seq_operations uv_ptc_seq_ops = {
492 .start = uv_ptc_seq_start,
493 .next = uv_ptc_seq_next,
494 .stop = uv_ptc_seq_stop,
495 .show = uv_ptc_seq_show
499 uv_ptc_proc_open(struct inode *inode, struct file *file)
501 return seq_open(file, &uv_ptc_seq_ops);
504 static const struct file_operations proc_uv_ptc_operations = {
505 .open = uv_ptc_proc_open,
507 .write = uv_ptc_proc_write,
509 .release = seq_release,
512 static struct proc_dir_entry *proc_uv_ptc;
517 static struct proc_dir_entry *sgi_proc_dir;
524 sgi_proc_dir = proc_mkdir("sgi_uv", NULL);
528 proc_uv_ptc = create_proc_entry(UV_PTC_BASENAME, 0444, NULL);
530 printk(KERN_ERR "unable to create %s proc entry\n",
534 proc_uv_ptc->proc_fops = &proc_uv_ptc_operations;
541 remove_proc_entry(UV_PTC_BASENAME, NULL);
544 module_init(uv_ptc_init);
545 module_exit(uv_ptc_exit);
548 * Initialization of BAU-related structures
564 unsigned long mmr_image;
565 unsigned long apicid;
567 struct bau_control *bau_tablesp;
568 struct bau_activation_descriptor *adp, *ad2;
569 struct bau_payload_queue_entry *pqp;
570 struct bau_msg_status *msp;
571 struct bau_control *bcp;
576 uv_bau_retry_limit = 1;
578 if ((sizeof(struct bau_local_cpumask) * BITSPERBYTE) <
581 "uv_bau_init: bau_local_cpumask.bits too small\n");
585 uv_nshift = uv_hub_info->n_val;
586 uv_mmask = ((unsigned long)1 << uv_hub_info->n_val) - 1;
589 for_each_online_node(i) {
590 blade = uv_node_to_blade_id(i);
591 if (blade == last_blade)
597 uv_bau_table_bases = (struct bau_control **)
598 kmalloc(nblades * sizeof(struct bau_control *), GFP_KERNEL);
599 if (!uv_bau_table_bases)
602 /* better if we had each_online_blade */
604 for_each_online_node(i) {
605 blade = uv_node_to_blade_id(i);
606 if (blade == last_blade)
611 kmalloc_node(sizeof(struct bau_control), GFP_KERNEL, i);
615 bau_tablesp->msg_statuses =
616 kmalloc_node(sizeof(struct bau_msg_status) *
617 DESTINATION_PAYLOAD_QUEUE_SIZE, GFP_KERNEL, i);
618 if (!bau_tablesp->msg_statuses)
620 for (j = 0, msp = bau_tablesp->msg_statuses;
621 j < DESTINATION_PAYLOAD_QUEUE_SIZE; j++, msp++) {
622 bau_cpubits_clear(&msp->seen_by, (int)
623 uv_blade_nr_possible_cpus(blade));
626 bau_tablesp->watching =
627 kmalloc_node(sizeof(int) * DESTINATION_NUM_RESOURCES,
629 if (!bau_tablesp->watching)
631 for (j = 0, ip = bau_tablesp->watching;
632 j < DESTINATION_PAYLOAD_QUEUE_SIZE; j++, ip++) {
636 uv_bau_table_bases[i] = bau_tablesp;
638 pnode = uv_blade_to_pnode(blade);
640 if (sizeof(struct bau_activation_descriptor) != 64)
643 adp = (struct bau_activation_descriptor *)
644 kmalloc_node(16384, GFP_KERNEL, i);
647 if ((unsigned long)adp & 0xfff)
649 pa = __pa((unsigned long)adp);
653 mmr_image = uv_read_global_mmr64(pnode,
654 UVH_LB_BAU_SB_DESCRIPTOR_BASE);
656 uv_write_global_mmr64(pnode, (unsigned long)
657 UVH_LB_BAU_SB_DESCRIPTOR_BASE,
658 (n << UV_DESC_BASE_PNODE_SHIFT |
660 for (j = 0, ad2 = adp; j < UV_ACTIVATION_DESCRIPTOR_SIZE;
663 sizeof(struct bau_activation_descriptor));
664 ad2->header.sw_ack_flag = 1;
665 ad2->header.base_dest_nodeid =
666 uv_blade_to_pnode(uv_cpu_to_blade_id(0));
667 ad2->header.command = UV_NET_ENDPOINT_INTD;
668 ad2->header.int_both = 1;
669 /* all others need to be set to zero:
670 fairness chaining multilevel count replied_to */
673 pqp = (struct bau_payload_queue_entry *)
674 kmalloc_node((DESTINATION_PAYLOAD_QUEUE_SIZE + 1) *
675 sizeof(struct bau_payload_queue_entry),
679 if (sizeof(struct bau_payload_queue_entry) != 32)
681 if ((unsigned long)(&((struct bau_payload_queue_entry *)0)->
682 sw_ack_vector) != 15)
685 cp = (char *)pqp + 31;
686 pqp = (struct bau_payload_queue_entry *)
687 (((unsigned long)cp >> 5) << 5);
688 bau_tablesp->va_queue_first = pqp;
689 uv_write_global_mmr64(pnode,
690 UVH_LB_BAU_INTD_PAYLOAD_QUEUE_FIRST,
691 ((unsigned long)pnode <<
692 UV_PAYLOADQ_PNODE_SHIFT) |
693 uv_physnodeaddr(pqp));
694 uv_write_global_mmr64(pnode, UVH_LB_BAU_INTD_PAYLOAD_QUEUE_TAIL,
695 uv_physnodeaddr(pqp));
696 bau_tablesp->va_queue_last =
697 pqp + (DESTINATION_PAYLOAD_QUEUE_SIZE - 1);
698 uv_write_global_mmr64(pnode, UVH_LB_BAU_INTD_PAYLOAD_QUEUE_LAST,
700 uv_physnodeaddr(bau_tablesp->
702 memset(pqp, 0, sizeof(struct bau_payload_queue_entry) *
703 DESTINATION_PAYLOAD_QUEUE_SIZE);
705 /* this initialization can't be in firmware because the
706 messaging IRQ will be determined by the OS */
707 apicid = per_cpu(x86_cpu_to_apicid, cur_cpu);
708 pa = uv_read_global_mmr64(pnode, UVH_BAU_DATA_CONFIG);
709 if ((pa & 0xff) != UV_BAU_MESSAGE) {
710 uv_write_global_mmr64(pnode, UVH_BAU_DATA_CONFIG,
715 for (j = cur_cpu; j < (cur_cpu + uv_blade_nr_possible_cpus(i));
717 bcp = (struct bau_control *)&per_cpu(bau_control, j);
718 bcp->bau_msg_head = bau_tablesp->va_queue_first;
719 bcp->va_queue_first = bau_tablesp->va_queue_first;
721 bcp->va_queue_last = bau_tablesp->va_queue_last;
722 bcp->watching = bau_tablesp->watching;
723 bcp->msg_statuses = bau_tablesp->msg_statuses;
724 bcp->descriptor_base = adp;
726 cur_cpu += uv_blade_nr_possible_cpus(i);
729 set_intr_gate(UV_BAU_MESSAGE, uv_bau_message_intr1);
731 uv_enable_timeouts();
736 __initcall(uv_bau_init);