tlb_uv.c 45.7 KB
Newer Older
1 2 3
/*
 *	SGI UltraViolet TLB flush routines.
 *
4
 *	(c) 2008-2010 Cliff Wickman <cpw@sgi.com>, SGI.
5 6 7 8
 *
 *	This code is released under the GNU General Public License version 2 or
 *	later.
 */
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>
15 16

#include <asm/mmu_context.h>
T
Tejun Heo 已提交
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>
I
Ingo Molnar 已提交
21
#include <asm/apic.h>
22
#include <asm/idle.h>
23
#include <asm/tsc.h>
24
#include <asm/irq_vectors.h>
25
#include <asm/timer.h>
26

27 28 29 30 31 32 33 34 35 36 37 38
/* timeouts in nanoseconds (indexed by UVH_AGING_PRESCALE_SEL urgency7 30:28) */
static int timeout_base_ns[] = {
		20,
		160,
		1280,
		10240,
		81920,
		655360,
		5242880,
		167772160
};
static int timeout_us;
39
static int nobau;
40 41 42
static int baudisabled;
static spinlock_t disable_lock;
static cycles_t congested_cycles;
43

44 45 46 47 48 49 50 51 52 53 54 55 56
/* tunables: */
static int max_bau_concurrent = MAX_BAU_CONCURRENT;
static int max_bau_concurrent_constant = MAX_BAU_CONCURRENT;
static int plugged_delay = PLUGGED_DELAY;
static int plugsb4reset = PLUGSB4RESET;
static int timeoutsb4reset = TIMEOUTSB4RESET;
static int ipi_reset_limit = IPI_RESET_LIMIT;
static int complete_threshold = COMPLETE_THRESHOLD;
static int congested_response_us = CONGESTED_RESPONSE_US;
static int congested_reps = CONGESTED_REPS;
static int congested_period = CONGESTED_PERIOD;
static struct dentry *tunables_dir;
static struct dentry *tunables_file;
57

58 59 60 61 62 63
static int __init setup_nobau(char *arg)
{
	nobau = 1;
	return 0;
}
early_param("nobau", setup_nobau);
64

65 66 67 68 69
/* base pnode in this partition */
static int uv_partition_base_pnode __read_mostly;
/* position of pnode (which is nasid>>1): */
static int uv_nshift __read_mostly;
static unsigned long uv_mmask __read_mostly;
70

71 72
static DEFINE_PER_CPU(struct ptc_stats, ptcstats);
static DEFINE_PER_CPU(struct bau_control, bau_control);
73 74
static DEFINE_PER_CPU(cpumask_var_t, uv_flush_tlb_mask);

75
/*
76 77
 * Determine the first node on a uvhub. 'Nodes' are used for kernel
 * memory allocation.
78
 */
79
static int __init uvhub_to_first_node(int uvhub)
80 81 82 83 84
{
	int node, b;

	for_each_online_node(node) {
		b = uv_node_to_blade_id(node);
85
		if (uvhub == b)
86 87
			return node;
	}
88
	return -1;
89 90 91
}

/*
92
 * Determine the apicid of the first cpu on a uvhub.
93
 */
94
static int __init uvhub_to_first_apicid(int uvhub)
95 96 97 98
{
	int cpu;

	for_each_present_cpu(cpu)
99
		if (uvhub == uv_cpu_to_blade_id(cpu))
100 101 102 103
			return per_cpu(x86_cpu_to_apicid, cpu);
	return -1;
}

104 105 106 107 108 109 110 111
/*
 * Free a software acknowledge hardware resource by clearing its Pending
 * bit. This will return a reply to the sender.
 * If the message has timed out, a reply has already been sent by the
 * hardware but the resource has not been released. In that case our
 * clear of the Timeout bit (as well) will free the resource. No reply will
 * be sent (the hardware will only do one reply per message).
 */
112 113
static inline void uv_reply_to_message(struct msg_desc *mdp,
				       struct bau_control *bcp)
114
{
115
	unsigned long dw;
116
	struct bau_payload_queue_entry *msg;
117

118 119 120 121 122 123 124
	msg = mdp->msg;
	if (!msg->canceled) {
		dw = (msg->sw_ack_vector << UV_SW_ACK_NPENDING) |
						msg->sw_ack_vector;
		uv_write_local_mmr(
				UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE_ALIAS, dw);
	}
125 126 127 128 129
	msg->replied_to = 1;
	msg->sw_ack_vector = 0;
}

/*
130
 * Process the receipt of a RETRY message
131
 */
132 133
static inline void uv_bau_process_retry_msg(struct msg_desc *mdp,
					    struct bau_control *bcp)
134
{
135 136 137 138 139 140 141 142
	int i;
	int cancel_count = 0;
	int slot2;
	unsigned long msg_res;
	unsigned long mmr = 0;
	struct bau_payload_queue_entry *msg;
	struct bau_payload_queue_entry *msg2;
	struct ptc_stats *stat;
143

144
	msg = mdp->msg;
145
	stat = bcp->statp;
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	stat->d_retries++;
	/*
	 * cancel any message from msg+1 to the retry itself
	 */
	for (msg2 = msg+1, i = 0; i < DEST_Q_SIZE; msg2++, i++) {
		if (msg2 > mdp->va_queue_last)
			msg2 = mdp->va_queue_first;
		if (msg2 == msg)
			break;

		/* same conditions for cancellation as uv_do_reset */
		if ((msg2->replied_to == 0) && (msg2->canceled == 0) &&
		    (msg2->sw_ack_vector) && ((msg2->sw_ack_vector &
			msg->sw_ack_vector) == 0) &&
		    (msg2->sending_cpu == msg->sending_cpu) &&
		    (msg2->msg_type != MSG_NOOP)) {
			slot2 = msg2 - mdp->va_queue_first;
			mmr = uv_read_local_mmr
				(UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE);
165
			msg_res = msg2->sw_ack_vector;
166 167 168 169 170 171
			/*
			 * This is a message retry; clear the resources held
			 * by the previous message only if they timed out.
			 * If it has not timed out we have an unexpected
			 * situation to report.
			 */
172
			if (mmr & (msg_res << UV_SW_ACK_NPENDING)) {
173 174 175 176 177 178 179 180 181
				/*
				 * is the resource timed out?
				 * make everyone ignore the cancelled message.
				 */
				msg2->canceled = 1;
				stat->d_canceled++;
				cancel_count++;
				uv_write_local_mmr(
				    UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE_ALIAS,
182 183 184
					(msg_res << UV_SW_ACK_NPENDING) |
					 msg_res);
			}
185 186 187 188 189
		}
	}
	if (!cancel_count)
		stat->d_nocanceled++;
}
190

191 192 193 194 195 196 197 198 199 200 201 202
/*
 * Do all the things a cpu should do for a TLB shootdown message.
 * Other cpu's may come here at the same time for this message.
 */
static void uv_bau_process_message(struct msg_desc *mdp,
				   struct bau_control *bcp)
{
	int msg_ack_count;
	short socket_ack_count = 0;
	struct ptc_stats *stat;
	struct bau_payload_queue_entry *msg;
	struct bau_control *smaster = bcp->socket_master;
203

204 205 206 207
	/*
	 * This must be a normal message, or retry of a normal message
	 */
	msg = mdp->msg;
208
	stat = bcp->statp;
209 210
	if (msg->address == TLB_FLUSH_ALL) {
		local_flush_tlb();
211
		stat->d_alltlb++;
212 213
	} else {
		__flush_tlb_one(msg->address);
214
		stat->d_onetlb++;
215
	}
216 217 218 219 220 221 222 223 224 225
	stat->d_requestee++;

	/*
	 * One cpu on each uvhub has the additional job on a RETRY
	 * of releasing the resource held by the message that is
	 * being retried.  That message is identified by sending
	 * cpu number.
	 */
	if (msg->msg_type == MSG_RETRY && bcp == bcp->uvhub_master)
		uv_bau_process_retry_msg(mdp, bcp);
226

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
	/*
	 * This is a sw_ack message, so we have to reply to it.
	 * Count each responding cpu on the socket. This avoids
	 * pinging the count's cache line back and forth between
	 * the sockets.
	 */
	socket_ack_count = atomic_add_short_return(1, (struct atomic_short *)
			&smaster->socket_acknowledge_count[mdp->msg_slot]);
	if (socket_ack_count == bcp->cpus_in_socket) {
		/*
		 * Both sockets dump their completed count total into
		 * the message's count.
		 */
		smaster->socket_acknowledge_count[mdp->msg_slot] = 0;
		msg_ack_count = atomic_add_short_return(socket_ack_count,
				(struct atomic_short *)&msg->acknowledge_count);

		if (msg_ack_count == bcp->cpus_in_uvhub) {
			/*
			 * All cpus in uvhub saw it; reply
			 */
			uv_reply_to_message(mdp, bcp);
		}
	}
251

252
	return;
253 254 255
}

/*
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
 * Determine the first cpu on a uvhub.
 */
static int uvhub_to_first_cpu(int uvhub)
{
	int cpu;
	for_each_present_cpu(cpu)
		if (uvhub == uv_cpu_to_blade_id(cpu))
			return cpu;
	return -1;
}

/*
 * Last resort when we get a large number of destination timeouts is
 * to clear resources held by a given cpu.
 * Do this with IPI so that all messages in the BAU message queue
 * can be identified by their nonzero sw_ack_vector field.
272
 *
273 274 275
 * This is entered for a single cpu on the uvhub.
 * The sender want's this uvhub to free a specific message's
 * sw_ack resources.
276
 */
277 278
static void
uv_do_reset(void *ptr)
279
{
280
	int i;
281 282 283 284 285 286 287 288
	int slot;
	int count = 0;
	unsigned long mmr;
	unsigned long msg_res;
	struct bau_control *bcp;
	struct reset_args *rap;
	struct bau_payload_queue_entry *msg;
	struct ptc_stats *stat;
289

290 291
	bcp = &per_cpu(bau_control, smp_processor_id());
	rap = (struct reset_args *)ptr;
292
	stat = bcp->statp;
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
	stat->d_resets++;

	/*
	 * We're looking for the given sender, and
	 * will free its sw_ack resource.
	 * If all cpu's finally responded after the timeout, its
	 * message 'replied_to' was set.
	 */
	for (msg = bcp->va_queue_first, i = 0; i < DEST_Q_SIZE; msg++, i++) {
		/* uv_do_reset: same conditions for cancellation as
		   uv_bau_process_retry_msg() */
		if ((msg->replied_to == 0) &&
		    (msg->canceled == 0) &&
		    (msg->sending_cpu == rap->sender) &&
		    (msg->sw_ack_vector) &&
		    (msg->msg_type != MSG_NOOP)) {
			/*
			 * make everyone else ignore this message
			 */
			msg->canceled = 1;
			slot = msg - bcp->va_queue_first;
			count++;
			/*
			 * only reset the resource if it is still pending
			 */
			mmr = uv_read_local_mmr
					(UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE);
320
			msg_res = msg->sw_ack_vector;
321 322 323 324
			if (mmr & msg_res) {
				stat->d_rcanceled++;
				uv_write_local_mmr(
				    UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE_ALIAS,
325 326
					(msg_res << UV_SW_ACK_NPENDING) |
					 msg_res);
327 328 329
			}
		}
	}
330
	return;
331 332 333
}

/*
334 335
 * Use IPI to get all target uvhubs to release resources held by
 * a given sending cpu number.
336
 */
337 338
static void uv_reset_with_ipi(struct bau_target_uvhubmask *distribution,
			      int sender)
339
{
340 341 342 343
	int uvhub;
	int cpu;
	cpumask_t mask;
	struct reset_args reset_args;
344

345
	reset_args.sender = sender;
346

347 348 349 350 351 352
	cpus_clear(mask);
	/* find a single cpu for each uvhub in this distribution mask */
	for (uvhub = 0;
		    uvhub < sizeof(struct bau_target_uvhubmask) * BITSPERBYTE;
		    uvhub++) {
		if (!bau_uvhub_isset(uvhub, distribution))
353
			continue;
354 355 356
		/* find a cpu for this uvhub */
		cpu = uvhub_to_first_cpu(uvhub);
		cpu_set(cpu, mask);
357
	}
358 359 360 361 362 363 364 365 366 367 368 369 370 371
	/* IPI all cpus; Preemption is already disabled */
	smp_call_function_many(&mask, uv_do_reset, (void *)&reset_args, 1);
	return;
}

static inline unsigned long
cycles_2_us(unsigned long long cyc)
{
	unsigned long long ns;
	unsigned long us;
	ns =  (cyc * per_cpu(cyc2ns, smp_processor_id()))
						>> CYC2NS_SCALE_FACTOR;
	us = ns / 1000;
	return us;
372 373
}

374
/*
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
 * wait for all cpus on this hub to finish their sends and go quiet
 * leaves uvhub_quiesce set so that no new broadcasts are started by
 * bau_flush_send_and_wait()
 */
static inline void
quiesce_local_uvhub(struct bau_control *hmaster)
{
	atomic_add_short_return(1, (struct atomic_short *)
		 &hmaster->uvhub_quiesce);
}

/*
 * mark this quiet-requestor as done
 */
static inline void
end_uvhub_quiesce(struct bau_control *hmaster)
{
	atomic_add_short_return(-1, (struct atomic_short *)
		&hmaster->uvhub_quiesce);
}

/*
 * Wait for completion of a broadcast software ack message
 * return COMPLETE, RETRY(PLUGGED or TIMEOUT) or GIVEUP
399
 */
400
static int uv_wait_completion(struct bau_desc *bau_desc,
401 402
	unsigned long mmr_offset, int right_shift, int this_cpu,
	struct bau_control *bcp, struct bau_control *smaster, long try)
403 404
{
	unsigned long descriptor_status;
405
	cycles_t ttime;
406
	struct ptc_stats *stat = bcp->statp;
407
	struct bau_control *hmaster;
408

409
	hmaster = bcp->uvhub_master;
410

411
	/* spin on the status MMR, waiting for it to go idle */
412 413 414 415 416
	while ((descriptor_status = (((unsigned long)
		uv_read_local_mmr(mmr_offset) >>
			right_shift) & UV_ACT_STATUS_MASK)) !=
			DESC_STATUS_IDLE) {
		/*
417 418 419 420
		 * Our software ack messages may be blocked because there are
		 * no swack resources available.  As long as none of them
		 * has timed out hardware will NACK our message and its
		 * state will stay IDLE.
421
		 */
422 423 424 425 426 427 428 429 430 431 432 433 434 435
		if (descriptor_status == DESC_STATUS_SOURCE_TIMEOUT) {
			stat->s_stimeout++;
			return FLUSH_GIVEUP;
		} else if (descriptor_status ==
					DESC_STATUS_DESTINATION_TIMEOUT) {
			stat->s_dtimeout++;
			ttime = get_cycles();

			/*
			 * Our retries may be blocked by all destination
			 * swack resources being consumed, and a timeout
			 * pending.  In that case hardware returns the
			 * ERROR that looks like a destination timeout.
			 */
436 437
			if (cycles_2_us(ttime - bcp->send_message) <
							timeout_us) {
438 439 440 441 442 443 444 445 446 447 448
				bcp->conseccompletes = 0;
				return FLUSH_RETRY_PLUGGED;
			}

			bcp->conseccompletes = 0;
			return FLUSH_RETRY_TIMEOUT;
		} else {
			/*
			 * descriptor_status is still BUSY
			 */
			cpu_relax();
449 450
		}
	}
451
	bcp->conseccompletes++;
452 453 454
	return FLUSH_COMPLETE;
}

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
static inline cycles_t
sec_2_cycles(unsigned long sec)
{
	unsigned long ns;
	cycles_t cyc;

	ns = sec * 1000000000;
	cyc = (ns << CYC2NS_SCALE_FACTOR)/(per_cpu(cyc2ns, smp_processor_id()));
	return cyc;
}

/*
 * conditionally add 1 to *v, unless *v is >= u
 * return 0 if we cannot add 1 to *v because it is >= u
 * return 1 if we can add 1 to *v because it is < u
 * the add is atomic
 *
 * This is close to atomic_add_unless(), but this allows the 'u' value
 * to be lowered below the current 'v'.  atomic_add_unless can only stop
 * on equal.
 */
static inline int atomic_inc_unless_ge(spinlock_t *lock, atomic_t *v, int u)
{
	spin_lock(lock);
	if (atomic_read(v) >= u) {
		spin_unlock(lock);
		return 0;
	}
	atomic_inc(v);
	spin_unlock(lock);
	return 1;
}

488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
/*
 * Our retries are blocked by all destination swack resources being
 * in use, and a timeout is pending. In that case hardware immediately
 * returns the ERROR that looks like a destination timeout.
 */
static void
destination_plugged(struct bau_desc *bau_desc, struct bau_control *bcp,
			struct bau_control *hmaster, struct ptc_stats *stat)
{
	udelay(bcp->plugged_delay);
	bcp->plugged_tries++;
	if (bcp->plugged_tries >= bcp->plugsb4reset) {
		bcp->plugged_tries = 0;
		quiesce_local_uvhub(hmaster);
		spin_lock(&hmaster->queue_lock);
		uv_reset_with_ipi(&bau_desc->distribution, bcp->cpu);
		spin_unlock(&hmaster->queue_lock);
		end_uvhub_quiesce(hmaster);
		bcp->ipi_attempts++;
		stat->s_resets_plug++;
	}
}

static void
destination_timeout(struct bau_desc *bau_desc, struct bau_control *bcp,
			struct bau_control *hmaster, struct ptc_stats *stat)
{
	hmaster->max_bau_concurrent = 1;
	bcp->timeout_tries++;
	if (bcp->timeout_tries >= bcp->timeoutsb4reset) {
		bcp->timeout_tries = 0;
		quiesce_local_uvhub(hmaster);
		spin_lock(&hmaster->queue_lock);
		uv_reset_with_ipi(&bau_desc->distribution, bcp->cpu);
		spin_unlock(&hmaster->queue_lock);
		end_uvhub_quiesce(hmaster);
		bcp->ipi_attempts++;
		stat->s_resets_timeout++;
	}
}

529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
/*
 * Completions are taking a very long time due to a congested numalink
 * network.
 */
static void
disable_for_congestion(struct bau_control *bcp, struct ptc_stats *stat)
{
	int tcpu;
	struct bau_control *tbcp;

	/* let only one cpu do this disabling */
	spin_lock(&disable_lock);
	if (!baudisabled && bcp->period_requests &&
	    ((bcp->period_time / bcp->period_requests) > congested_cycles)) {
		/* it becomes this cpu's job to turn on the use of the
		   BAU again */
		baudisabled = 1;
		bcp->set_bau_off = 1;
		bcp->set_bau_on_time = get_cycles() +
			sec_2_cycles(bcp->congested_period);
		stat->s_bau_disabled++;
		for_each_present_cpu(tcpu) {
			tbcp = &per_cpu(bau_control, tcpu);
				tbcp->baudisabled = 1;
		}
	}
	spin_unlock(&disable_lock);
}

558 559 560
/**
 * uv_flush_send_and_wait
 *
561
 * Send a broadcast and wait for it to complete.
562
 *
563
 * The flush_mask contains the cpus the broadcast is to be sent to including
564
 * cpus that are on the local uvhub.
565
 *
566 567 568
 * Returns 0 if all flushing represented in the mask was done.
 * Returns 1 if it gives up entirely and the original cpu mask is to be
 * returned to the kernel.
569
 */
570 571
int uv_flush_send_and_wait(struct bau_desc *bau_desc,
			   struct cpumask *flush_mask, struct bau_control *bcp)
572 573
{
	int right_shift;
574 575 576 577 578
	int completion_status = 0;
	int seq_number = 0;
	long try = 0;
	int cpu = bcp->uvhub_cpu;
	int this_cpu = bcp->cpu;
579
	unsigned long mmr_offset;
580
	unsigned long index;
581 582
	cycles_t time1;
	cycles_t time2;
583
	cycles_t elapsed;
584
	struct ptc_stats *stat = bcp->statp;
585 586 587 588 589
	struct bau_control *smaster = bcp->socket_master;
	struct bau_control *hmaster = bcp->uvhub_master;

	if (!atomic_inc_unless_ge(&hmaster->uvhub_lock,
			&hmaster->active_descriptor_count,
590
			hmaster->max_bau_concurrent)) {
591 592 593 594 595
		stat->s_throttles++;
		do {
			cpu_relax();
		} while (!atomic_inc_unless_ge(&hmaster->uvhub_lock,
			&hmaster->active_descriptor_count,
596
			hmaster->max_bau_concurrent));
597 598 599
	}
	while (hmaster->uvhub_quiesce)
		cpu_relax();
600 601 602 603 604 605 606 607 608 609 610

	if (cpu < UV_CPUS_PER_ACT_STATUS) {
		mmr_offset = UVH_LB_BAU_SB_ACTIVATION_STATUS_0;
		right_shift = cpu * UV_ACT_STATUS_SIZE;
	} else {
		mmr_offset = UVH_LB_BAU_SB_ACTIVATION_STATUS_1;
		right_shift =
		    ((cpu - UV_CPUS_PER_ACT_STATUS) * UV_ACT_STATUS_SIZE);
	}
	time1 = get_cycles();
	do {
611
		if (try == 0) {
612
			bau_desc->header.msg_type = MSG_REGULAR;
613 614 615 616 617 618
			seq_number = bcp->message_number++;
		} else {
			bau_desc->header.msg_type = MSG_RETRY;
			stat->s_retry_messages++;
		}
		bau_desc->header.sequence = seq_number;
619
		index = (1UL << UVH_LB_BAU_SB_ACTIVATION_CONTROL_PUSH_SHFT) |
620 621
			bcp->uvhub_cpu;
		bcp->send_message = get_cycles();
622
		uv_write_local_mmr(UVH_LB_BAU_SB_ACTIVATION_CONTROL, index);
623
		try++;
624
		completion_status = uv_wait_completion(bau_desc, mmr_offset,
625 626 627
			right_shift, this_cpu, bcp, smaster, try);

		if (completion_status == FLUSH_RETRY_PLUGGED) {
628
			destination_plugged(bau_desc, bcp, hmaster, stat);
629
		} else if (completion_status == FLUSH_RETRY_TIMEOUT) {
630
			destination_timeout(bau_desc, bcp, hmaster, stat);
631
		}
632
		if (bcp->ipi_attempts >= bcp->ipi_reset_limit) {
633 634 635 636 637 638 639
			bcp->ipi_attempts = 0;
			completion_status = FLUSH_GIVEUP;
			break;
		}
		cpu_relax();
	} while ((completion_status == FLUSH_RETRY_PLUGGED) ||
		 (completion_status == FLUSH_RETRY_TIMEOUT));
640
	time2 = get_cycles();
641 642 643 644 645 646 647
	bcp->plugged_tries = 0;
	bcp->timeout_tries = 0;
	if ((completion_status == FLUSH_COMPLETE) &&
	    (bcp->conseccompletes > bcp->complete_threshold) &&
	    (hmaster->max_bau_concurrent <
					hmaster->max_bau_concurrent_constant))
			hmaster->max_bau_concurrent++;
648 649 650
	while (hmaster->uvhub_quiesce)
		cpu_relax();
	atomic_dec(&hmaster->active_descriptor_count);
651 652 653
	if (time2 > time1) {
		elapsed = time2 - time1;
		stat->s_time += elapsed;
654 655 656 657 658 659 660 661
		if ((completion_status == FLUSH_COMPLETE) && (try == 1)) {
			bcp->period_requests++;
			bcp->period_time += elapsed;
			if ((elapsed > congested_cycles) &&
			    (bcp->period_requests > bcp->congested_reps)) {
				disable_for_congestion(bcp, stat);
			}
		}
662
	} else
663
		stat->s_requestor--;
664 665 666 667
	if (completion_status == FLUSH_COMPLETE && try > 1)
		stat->s_retriesok++;
	else if (completion_status == FLUSH_GIVEUP) {
		stat->s_giveup++;
668
		return 1;
669
	}
670
	return 0;
671 672
}

673 674 675
/**
 * uv_flush_tlb_others - globally purge translation cache of a virtual
 * address or all TLB's
T
Tejun Heo 已提交
676
 * @cpumask: mask of all cpu's in which the address is to be removed
677 678
 * @mm: mm_struct containing virtual address range
 * @va: virtual address to be removed (or TLB_FLUSH_ALL for all TLB's on cpu)
T
Tejun Heo 已提交
679
 * @cpu: the current cpu
680 681 682 683 684 685
 *
 * This is the entry point for initiating any UV global TLB shootdown.
 *
 * Purges the translation caches of all specified processors of the given
 * virtual address, or purges all TLB's on specified processors.
 *
T
Tejun Heo 已提交
686 687
 * The caller has derived the cpumask from the mm_struct.  This function
 * is called only if there are bits set in the mask. (e.g. flush_tlb_page())
688
 *
689 690
 * The cpumask is converted into a uvhubmask of the uvhubs containing
 * those cpus.
691
 *
T
Tejun Heo 已提交
692 693 694 695 696
 * Note that this function should be called with preemption disabled.
 *
 * Returns NULL if all remote flushing was done.
 * Returns pointer to cpumask if some remote flushing remains to be
 * done.  The returned pointer is valid till preemption is re-enabled.
697
 */
T
Tejun Heo 已提交
698 699 700
const struct cpumask *uv_flush_tlb_others(const struct cpumask *cpumask,
					  struct mm_struct *mm,
					  unsigned long va, unsigned int cpu)
701
{
702
	int locals = 0;
703 704
	int remotes = 0;
	int hubs = 0;
705 706
	int tcpu;
	int tpnode;
707
	struct bau_desc *bau_desc;
708 709 710
	struct cpumask *flush_mask;
	struct ptc_stats *stat;
	struct bau_control *bcp;
711
	struct bau_control *tbcp;
712
	struct hub_and_pnode *hpp;
T
Tejun Heo 已提交
713

714
	/* kernel was booted 'nobau' */
715 716
	if (nobau)
		return cpumask;
T
Tejun Heo 已提交
717

718
	bcp = &per_cpu(bau_control, cpu);
719
	stat = bcp->statp;
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737

	/* bau was disabled due to slow response */
	if (bcp->baudisabled) {
		/* the cpu that disabled it must re-enable it */
		if (bcp->set_bau_off) {
			if (get_cycles() >= bcp->set_bau_on_time) {
				stat->s_bau_reenabled++;
				baudisabled = 0;
				for_each_present_cpu(tcpu) {
					tbcp = &per_cpu(bau_control, tcpu);
					tbcp->baudisabled = 0;
					tbcp->period_requests = 0;
					tbcp->period_time = 0;
				}
			}
		}
		return cpumask;
	}
738

739 740
	/*
	 * Each sending cpu has a per-cpu mask which it fills from the caller's
741 742
	 * cpu mask.  All cpus are converted to uvhubs and copied to the
	 * activation descriptor.
743 744
	 */
	flush_mask = (struct cpumask *)per_cpu(uv_flush_tlb_mask, cpu);
745
	/* don't actually do a shootdown of the local cpu */
746 747
	cpumask_andnot(flush_mask, cpumask, cpumask_of(cpu));
	if (cpu_isset(cpu, *cpumask))
748
		stat->s_ntargself++;
749

750 751 752
	bau_desc = bcp->descriptor_base;
	bau_desc += UV_ITEMS_PER_DESCRIPTOR * bcp->uvhub_cpu;
	bau_uvhubs_clear(&bau_desc->distribution, UV_DISTRIBUTION_SIZE);
753

754
	for_each_cpu(tcpu, flush_mask) {
755 756 757 758 759 760 761 762 763 764 765
		/*
		 * The distribution vector is a bit map of pnodes, relative
		 * to the partition base pnode (and the partition base nasid
		 * in the header).
		 * Translate cpu to pnode and hub using an array stored
		 * in local memory.
		 */
		hpp = &bcp->socket_master->target_hub_and_pnode[tcpu];
		tpnode = hpp->pnode - bcp->partition_base_pnode;
		bau_uvhub_set(tpnode, &bau_desc->distribution);
		if (hpp->uvhub == bcp->uvhub)
766
			locals++;
767
		else
768
			remotes++;
769
	}
770 771
	if ((locals + remotes) == 0)
		return NULL;
772
	stat->s_requestor++;
773 774 775
	stat->s_ntargcpu += remotes + locals;
	stat->s_ntargremotes += remotes;
	stat->s_ntarglocals += locals;
776
	remotes = bau_uvhub_weight(&bau_desc->distribution);
777 778 779 780 781 782 783 784 785 786

	/* uvhub statistics */
	hubs = bau_uvhub_weight(&bau_desc->distribution);
	if (locals) {
		stat->s_ntarglocaluvhub++;
		stat->s_ntargremoteuvhub += (hubs - 1);
	} else
		stat->s_ntargremoteuvhub += hubs;
	stat->s_ntarguvhub += hubs;
	if (hubs >= 16)
787
		stat->s_ntarguvhub16++;
788
	else if (hubs >= 8)
789
		stat->s_ntarguvhub8++;
790
	else if (hubs >= 4)
791
		stat->s_ntarguvhub4++;
792
	else if (hubs >= 2)
793 794 795
		stat->s_ntarguvhub2++;
	else
		stat->s_ntarguvhub1++;
796 797

	bau_desc->payload.address = va;
T
Tejun Heo 已提交
798
	bau_desc->payload.sending_cpu = cpu;
799

800
	/*
801 802
	 * uv_flush_send_and_wait returns 0 if all cpu's were messaged,
	 * or 1 if it gave up and the original cpumask should be returned.
803
	 */
804 805 806 807
	if (!uv_flush_send_and_wait(bau_desc, flush_mask, bcp))
		return NULL;
	else
		return cpumask;
808 809 810 811 812 813 814 815
}

/*
 * The BAU message interrupt comes here. (registered by set_intr_gate)
 * See entry_64.S
 *
 * We received a broadcast assist message.
 *
816
 * Interrupts are disabled; this interrupt could represent
817 818
 * the receipt of several messages.
 *
819 820
 * All cores/threads on this hub get this interrupt.
 * The last one to see it does the software ack.
821
 * (the resource will not be freed until noninterruptable cpus see this
822
 *  interrupt; hardware may timeout the s/w ack and reply ERROR)
823
 */
824
void uv_bau_message_interrupt(struct pt_regs *regs)
825 826
{
	int count = 0;
827 828 829 830 831 832 833 834
	cycles_t time_start;
	struct bau_payload_queue_entry *msg;
	struct bau_control *bcp;
	struct ptc_stats *stat;
	struct msg_desc msgdesc;

	time_start = get_cycles();
	bcp = &per_cpu(bau_control, smp_processor_id());
835
	stat = bcp->statp;
836 837 838
	msgdesc.va_queue_first = bcp->va_queue_first;
	msgdesc.va_queue_last = bcp->va_queue_last;
	msg = bcp->bau_msg_head;
839 840
	while (msg->sw_ack_vector) {
		count++;
841 842 843 844
		msgdesc.msg_slot = msg - msgdesc.va_queue_first;
		msgdesc.sw_ack_slot = ffs(msg->sw_ack_vector) - 1;
		msgdesc.msg = msg;
		uv_bau_process_message(&msgdesc, bcp);
845
		msg++;
846 847 848
		if (msg > msgdesc.va_queue_last)
			msg = msgdesc.va_queue_first;
		bcp->bau_msg_head = msg;
849
	}
850
	stat->d_time += (get_cycles() - time_start);
851
	if (!count)
852
		stat->d_nomsg++;
853
	else if (count > 1)
854 855
		stat->d_multmsg++;
	ack_APIC_irq();
856 857
}

C
Cliff Wickman 已提交
858 859 860
/*
 * uv_enable_timeouts
 *
861
 * Each target uvhub (i.e. a uvhub that has no cpu's) needs to have
C
Cliff Wickman 已提交
862 863 864 865
 * shootdown message timeouts enabled.  The timeout does not cause
 * an interrupt, but causes an error message to be returned to
 * the sender.
 */
866
static void __init uv_enable_timeouts(void)
867
{
868 869
	int uvhub;
	int nuvhubs;
870
	int pnode;
C
Cliff Wickman 已提交
871
	unsigned long mmr_image;
872

873
	nuvhubs = uv_num_possible_blades();
874

875 876
	for (uvhub = 0; uvhub < nuvhubs; uvhub++) {
		if (!uv_blade_nr_possible_cpus(uvhub))
877
			continue;
C
Cliff Wickman 已提交
878

879
		pnode = uv_blade_to_pnode(uvhub);
C
Cliff Wickman 已提交
880 881 882 883 884 885 886 887 888
		mmr_image =
		    uv_read_global_mmr64(pnode, UVH_LB_BAU_MISC_CONTROL);
		/*
		 * Set the timeout period and then lock it in, in three
		 * steps; captures and locks in the period.
		 *
		 * To program the period, the SOFT_ACK_MODE must be off.
		 */
		mmr_image &= ~((unsigned long)1 <<
889
		    UVH_LB_BAU_MISC_CONTROL_ENABLE_INTD_SOFT_ACK_MODE_SHFT);
C
Cliff Wickman 已提交
890 891 892 893 894 895
		uv_write_global_mmr64
		    (pnode, UVH_LB_BAU_MISC_CONTROL, mmr_image);
		/*
		 * Set the 4-bit period.
		 */
		mmr_image &= ~((unsigned long)0xf <<
896
		     UVH_LB_BAU_MISC_CONTROL_INTD_SOFT_ACK_TIMEOUT_PERIOD_SHFT);
C
Cliff Wickman 已提交
897
		mmr_image |= (UV_INTD_SOFT_ACK_TIMEOUT_PERIOD <<
898
		     UVH_LB_BAU_MISC_CONTROL_INTD_SOFT_ACK_TIMEOUT_PERIOD_SHFT);
C
Cliff Wickman 已提交
899 900 901 902 903 904 905 906
		uv_write_global_mmr64
		    (pnode, UVH_LB_BAU_MISC_CONTROL, mmr_image);
		/*
		 * Subsequent reversals of the timebase bit (3) cause an
		 * immediate timeout of one or all INTD resources as
		 * indicated in bits 2:0 (7 causes all of them to timeout).
		 */
		mmr_image |= ((unsigned long)1 <<
907
		    UVH_LB_BAU_MISC_CONTROL_ENABLE_INTD_SOFT_ACK_MODE_SHFT);
C
Cliff Wickman 已提交
908 909
		uv_write_global_mmr64
		    (pnode, UVH_LB_BAU_MISC_CONTROL, mmr_image);
910 911 912
	}
}

913
static void *uv_ptc_seq_start(struct seq_file *file, loff_t *offset)
914 915 916 917 918 919
{
	if (*offset < num_possible_cpus())
		return offset;
	return NULL;
}

920
static void *uv_ptc_seq_next(struct seq_file *file, void *data, loff_t *offset)
921 922 923 924 925 926 927
{
	(*offset)++;
	if (*offset < num_possible_cpus())
		return offset;
	return NULL;
}

928
static void uv_ptc_seq_stop(struct seq_file *file, void *data)
929 930 931
{
}

932
static inline unsigned long long
933
microsec_2_cycles(unsigned long microsec)
934 935 936 937
{
	unsigned long ns;
	unsigned long long cyc;

938
	ns = microsec * 1000;
939 940 941 942
	cyc = (ns << CYC2NS_SCALE_FACTOR)/(per_cpu(cyc2ns, smp_processor_id()));
	return cyc;
}

943
/*
944 945
 * Display the statistics thru /proc.
 * 'data' points to the cpu number
946
 */
947
static int uv_ptc_seq_show(struct seq_file *file, void *data)
948 949 950 951 952 953 954 955
{
	struct ptc_stats *stat;
	int cpu;

	cpu = *(loff_t *)data;

	if (!cpu) {
		seq_printf(file,
956 957 958
			"# cpu sent stime self locals remotes ncpus localhub ");
		seq_printf(file,
			"remotehub numuvhubs numuvhubs16 numuvhubs8 ");
959
		seq_printf(file,
960
			"numuvhubs4 numuvhubs2 numuvhubs1 dto ");
961 962 963 964 965
		seq_printf(file,
			"retries rok resetp resett giveup sto bz throt ");
		seq_printf(file,
			"sw_ack recv rtime all ");
		seq_printf(file,
966 967 968
			"one mult none retry canc nocan reset rcan ");
		seq_printf(file,
			"disable enable\n");
969 970 971
	}
	if (cpu < num_possible_cpus() && cpu_online(cpu)) {
		stat = &per_cpu(ptcstats, cpu);
972 973 974 975
		/* source side statistics */
		seq_printf(file,
			"cpu %d %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld ",
			   cpu, stat->s_requestor, cycles_2_us(stat->s_time),
976 977 978 979 980
			   stat->s_ntargself, stat->s_ntarglocals,
			   stat->s_ntargremotes, stat->s_ntargcpu,
			   stat->s_ntarglocaluvhub, stat->s_ntargremoteuvhub,
			   stat->s_ntarguvhub, stat->s_ntarguvhub16);
		seq_printf(file, "%ld %ld %ld %ld %ld ",
981 982
			   stat->s_ntarguvhub8, stat->s_ntarguvhub4,
			   stat->s_ntarguvhub2, stat->s_ntarguvhub1,
983
			   stat->s_dtimeout);
984 985 986 987 988
		seq_printf(file, "%ld %ld %ld %ld %ld %ld %ld %ld ",
			   stat->s_retry_messages, stat->s_retriesok,
			   stat->s_resets_plug, stat->s_resets_timeout,
			   stat->s_giveup, stat->s_stimeout,
			   stat->s_busy, stat->s_throttles);
989

990 991
		/* destination side statistics */
		seq_printf(file,
992
			   "%lx %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld ",
993
			   uv_read_global_mmr64(uv_cpu_to_pnode(cpu),
994
					UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE),
995 996 997 998 999
			   stat->d_requestee, cycles_2_us(stat->d_time),
			   stat->d_alltlb, stat->d_onetlb, stat->d_multmsg,
			   stat->d_nomsg, stat->d_retries, stat->d_canceled,
			   stat->d_nocanceled, stat->d_resets,
			   stat->d_rcanceled);
1000 1001
		seq_printf(file, "%ld %ld\n",
			stat->s_bau_disabled, stat->s_bau_reenabled);
1002 1003 1004 1005 1006
	}

	return 0;
}

1007 1008 1009 1010 1011 1012
/*
 * Display the tunables thru debugfs
 */
static ssize_t tunables_read(struct file *file, char __user *userbuf,
						size_t count, loff_t *ppos)
{
1013
	char *buf;
1014 1015
	int ret;

1016
	buf = kasprintf(GFP_KERNEL, "%s %s %s\n%d %d %d %d %d %d %d %d %d\n",
1017 1018 1019 1020 1021 1022 1023
		"max_bau_concurrent plugged_delay plugsb4reset",
		"timeoutsb4reset ipi_reset_limit complete_threshold",
		"congested_response_us congested_reps congested_period",
		max_bau_concurrent, plugged_delay, plugsb4reset,
		timeoutsb4reset, ipi_reset_limit, complete_threshold,
		congested_response_us, congested_reps, congested_period);

1024 1025 1026 1027 1028 1029
	if (!buf)
		return -ENOMEM;

	ret = simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
	kfree(buf);
	return ret;
1030 1031
}

1032
/*
1033
 * -1: resetf the statistics
1034 1035
 *  0: display meaning of the statistics
 */
1036
static ssize_t uv_ptc_proc_write(struct file *file, const char __user *user,
1037
				 size_t count, loff_t *data)
1038
{
1039 1040
	int cpu;
	long input_arg;
1041
	char optstr[64];
1042
	struct ptc_stats *stat;
1043

1044
	if (count == 0 || count > sizeof(optstr))
1045
		return -EINVAL;
1046 1047 1048
	if (copy_from_user(optstr, user, count))
		return -EFAULT;
	optstr[count - 1] = '\0';
1049
	if (strict_strtol(optstr, 10, &input_arg) < 0) {
1050 1051 1052 1053
		printk(KERN_DEBUG "%s is invalid\n", optstr);
		return -EINVAL;
	}

1054
	if (input_arg == 0) {
1055
		printk(KERN_DEBUG "# cpu:      cpu number\n");
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
		printk(KERN_DEBUG "Sender statistics:\n");
		printk(KERN_DEBUG
		"sent:     number of shootdown messages sent\n");
		printk(KERN_DEBUG
		"stime:    time spent sending messages\n");
		printk(KERN_DEBUG
		"numuvhubs: number of hubs targeted with shootdown\n");
		printk(KERN_DEBUG
		"numuvhubs16: number times 16 or more hubs targeted\n");
		printk(KERN_DEBUG
		"numuvhubs8: number times 8 or more hubs targeted\n");
		printk(KERN_DEBUG
		"numuvhubs4: number times 4 or more hubs targeted\n");
		printk(KERN_DEBUG
		"numuvhubs2: number times 2 or more hubs targeted\n");
		printk(KERN_DEBUG
		"numuvhubs1: number times 1 hub targeted\n");
		printk(KERN_DEBUG
		"numcpus:  number of cpus targeted with shootdown\n");
		printk(KERN_DEBUG
		"dto:      number of destination timeouts\n");
		printk(KERN_DEBUG
		"retries:  destination timeout retries sent\n");
		printk(KERN_DEBUG
		"rok:   :  destination timeouts successfully retried\n");
		printk(KERN_DEBUG
		"resetp:   ipi-style resource resets for plugs\n");
		printk(KERN_DEBUG
		"resett:   ipi-style resource resets for timeouts\n");
		printk(KERN_DEBUG
		"giveup:   fall-backs to ipi-style shootdowns\n");
		printk(KERN_DEBUG
		"sto:      number of source timeouts\n");
		printk(KERN_DEBUG
		"bz:       number of stay-busy's\n");
		printk(KERN_DEBUG
		"throt:    number times spun in throttle\n");
		printk(KERN_DEBUG "Destination side statistics:\n");
1094
		printk(KERN_DEBUG
1095
		"sw_ack:   image of UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE\n");
1096
		printk(KERN_DEBUG
1097
		"recv:     shootdown messages received\n");
1098
		printk(KERN_DEBUG
1099
		"rtime:    time spent processing messages\n");
1100
		printk(KERN_DEBUG
1101
		"all:      shootdown all-tlb messages\n");
1102
		printk(KERN_DEBUG
1103
		"one:      shootdown one-tlb messages\n");
1104
		printk(KERN_DEBUG
1105
		"mult:     interrupts that found multiple messages\n");
1106
		printk(KERN_DEBUG
1107
		"none:     interrupts that found no messages\n");
1108
		printk(KERN_DEBUG
1109
		"retry:    number of retry messages processed\n");
1110
		printk(KERN_DEBUG
1111
		"canc:     number messages canceled by retries\n");
1112
		printk(KERN_DEBUG
1113
		"nocan:    number retries that found nothing to cancel\n");
1114
		printk(KERN_DEBUG
1115 1116 1117
		"reset:    number of ipi-style reset requests processed\n");
		printk(KERN_DEBUG
		"rcan:     number messages canceled by reset requests\n");
1118 1119 1120 1121
		printk(KERN_DEBUG
		"disable:  number times use of the BAU was disabled\n");
		printk(KERN_DEBUG
		"enable:   number times use of the BAU was re-enabled\n");
1122 1123 1124 1125 1126
	} else if (input_arg == -1) {
		for_each_present_cpu(cpu) {
			stat = &per_cpu(ptcstats, cpu);
			memset(stat, 0, sizeof(struct ptc_stats));
		}
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
	}

	return count;
}

static int local_atoi(const char *name)
{
	int val = 0;

	for (;; name++) {
		switch (*name) {
		case '0' ... '9':
			val = 10*val+(*name-'0');
			break;
		default:
			return val;
1143
		}
1144
	}
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
}

/*
 * set the tunables
 * 0 values reset them to defaults
 */
static ssize_t tunables_write(struct file *file, const char __user *user,
				 size_t count, loff_t *data)
{
	int cpu;
	int cnt = 0;
	int val;
	char *p;
	char *q;
	char instr[64];
	struct bau_control *bcp;
1161

1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
	if (count == 0 || count > sizeof(instr)-1)
		return -EINVAL;
	if (copy_from_user(instr, user, count))
		return -EFAULT;

	instr[count] = '\0';
	/* count the fields */
	p = instr + strspn(instr, WHITESPACE);
	q = p;
	for (; *p; p = q + strspn(q, WHITESPACE)) {
		q = p + strcspn(p, WHITESPACE);
		cnt++;
		if (q == p)
			break;
	}
	if (cnt != 9) {
		printk(KERN_INFO "bau tunable error: should be 9 numbers\n");
		return -EINVAL;
	}

	p = instr + strspn(instr, WHITESPACE);
	q = p;
	for (cnt = 0; *p; p = q + strspn(q, WHITESPACE), cnt++) {
		q = p + strcspn(p, WHITESPACE);
		val = local_atoi(p);
		switch (cnt) {
		case 0:
			if (val == 0) {
				max_bau_concurrent = MAX_BAU_CONCURRENT;
				max_bau_concurrent_constant =
							MAX_BAU_CONCURRENT;
				continue;
			}
			bcp = &per_cpu(bau_control, smp_processor_id());
			if (val < 1 || val > bcp->cpus_in_uvhub) {
				printk(KERN_DEBUG
				"Error: BAU max concurrent %d is invalid\n",
				val);
				return -EINVAL;
			}
			max_bau_concurrent = val;
			max_bau_concurrent_constant = val;
			continue;
		case 1:
			if (val == 0)
				plugged_delay = PLUGGED_DELAY;
			else
				plugged_delay = val;
			continue;
		case 2:
			if (val == 0)
				plugsb4reset = PLUGSB4RESET;
			else
				plugsb4reset = val;
			continue;
		case 3:
			if (val == 0)
				timeoutsb4reset = TIMEOUTSB4RESET;
			else
				timeoutsb4reset = val;
			continue;
		case 4:
			if (val == 0)
				ipi_reset_limit = IPI_RESET_LIMIT;
			else
				ipi_reset_limit = val;
			continue;
		case 5:
			if (val == 0)
				complete_threshold = COMPLETE_THRESHOLD;
			else
				complete_threshold = val;
			continue;
		case 6:
			if (val == 0)
				congested_response_us = CONGESTED_RESPONSE_US;
			else
				congested_response_us = val;
			continue;
		case 7:
			if (val == 0)
				congested_reps = CONGESTED_REPS;
			else
				congested_reps = val;
			continue;
		case 8:
			if (val == 0)
				congested_period = CONGESTED_PERIOD;
			else
				congested_period = val;
			continue;
		}
		if (q == p)
			break;
	}
	for_each_present_cpu(cpu) {
		bcp = &per_cpu(bau_control, cpu);
		bcp->max_bau_concurrent = max_bau_concurrent;
		bcp->max_bau_concurrent_constant = max_bau_concurrent;
		bcp->plugged_delay = plugged_delay;
		bcp->plugsb4reset = plugsb4reset;
		bcp->timeoutsb4reset = timeoutsb4reset;
		bcp->ipi_reset_limit = ipi_reset_limit;
		bcp->complete_threshold = complete_threshold;
		bcp->congested_response_us = congested_response_us;
		bcp->congested_reps = congested_reps;
		bcp->congested_period = congested_period;
	}
1270 1271 1272 1273
	return count;
}

static const struct seq_operations uv_ptc_seq_ops = {
1274 1275 1276 1277
	.start		= uv_ptc_seq_start,
	.next		= uv_ptc_seq_next,
	.stop		= uv_ptc_seq_stop,
	.show		= uv_ptc_seq_show
1278 1279
};

1280
static int uv_ptc_proc_open(struct inode *inode, struct file *file)
1281 1282 1283 1284
{
	return seq_open(file, &uv_ptc_seq_ops);
}

1285 1286 1287 1288 1289
static int tunables_open(struct inode *inode, struct file *file)
{
	return 0;
}

1290
static const struct file_operations proc_uv_ptc_operations = {
1291 1292 1293 1294 1295
	.open		= uv_ptc_proc_open,
	.read		= seq_read,
	.write		= uv_ptc_proc_write,
	.llseek		= seq_lseek,
	.release	= seq_release,
1296 1297
};

1298 1299 1300 1301
static const struct file_operations tunables_fops = {
	.open		= tunables_open,
	.read		= tunables_read,
	.write		= tunables_write,
1302
	.llseek		= default_llseek,
1303 1304
};

1305
static int __init uv_ptc_init(void)
1306
{
1307
	struct proc_dir_entry *proc_uv_ptc;
1308 1309 1310 1311

	if (!is_uv_system())
		return 0;

1312 1313
	proc_uv_ptc = proc_create(UV_PTC_BASENAME, 0444, NULL,
				  &proc_uv_ptc_operations);
1314 1315 1316 1317 1318
	if (!proc_uv_ptc) {
		printk(KERN_ERR "unable to create %s proc entry\n",
		       UV_PTC_BASENAME);
		return -EINVAL;
	}
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332

	tunables_dir = debugfs_create_dir(UV_BAU_TUNABLES_DIR, NULL);
	if (!tunables_dir) {
		printk(KERN_ERR "unable to create debugfs directory %s\n",
		       UV_BAU_TUNABLES_DIR);
		return -EINVAL;
	}
	tunables_file = debugfs_create_file(UV_BAU_TUNABLES_FILE, 0600,
			tunables_dir, NULL, &tunables_fops);
	if (!tunables_file) {
		printk(KERN_ERR "unable to create debugfs file %s\n",
		       UV_BAU_TUNABLES_FILE);
		return -EINVAL;
	}
1333 1334 1335 1336
	return 0;
}

/*
1337
 * Initialize the sending side's sending buffers.
1338
 */
1339
static void
1340
uv_activation_descriptor_init(int node, int pnode, int base_pnode)
1341 1342
{
	int i;
1343
	int cpu;
1344 1345
	unsigned long pa;
	unsigned long m;
1346
	unsigned long n;
1347 1348 1349
	struct bau_desc *bau_desc;
	struct bau_desc *bd2;
	struct bau_control *bcp;
1350

1351 1352
	/*
	 * each bau_desc is 64 bytes; there are 8 (UV_ITEMS_PER_DESCRIPTOR)
1353
	 * per cpu; and one per cpu on the uvhub (UV_ADP_SIZE)
1354
	 */
1355 1356
	bau_desc = kmalloc_node(sizeof(struct bau_desc) * UV_ADP_SIZE
				* UV_ITEMS_PER_DESCRIPTOR, GFP_KERNEL, node);
1357
	BUG_ON(!bau_desc);
1358

1359 1360
	pa = uv_gpa(bau_desc); /* need the real nasid*/
	n = pa >> uv_nshift;
1361
	m = pa & uv_mmask;
1362

1363
	/* the 14-bit pnode */
1364 1365
	uv_write_global_mmr64(pnode, UVH_LB_BAU_SB_DESCRIPTOR_BASE,
			      (n << UV_DESC_BASE_PNODE_SHIFT | m));
1366
	/*
1367
	 * Initializing all 8 (UV_ITEMS_PER_DESCRIPTOR) descriptors for each
1368
	 * cpu even though we only use the first one; one descriptor can
1369
	 * describe a broadcast to 256 uv hubs.
1370
	 */
1371 1372 1373 1374
	for (i = 0, bd2 = bau_desc; i < (UV_ADP_SIZE*UV_ITEMS_PER_DESCRIPTOR);
		i++, bd2++) {
		memset(bd2, 0, sizeof(struct bau_desc));
		bd2->header.sw_ack_flag = 1;
1375
		/*
1376 1377 1378 1379
		 * The base_dest_nasid set in the message header is the nasid
		 * of the first uvhub in the partition. The bit map will
		 * indicate destination pnode numbers relative to that base.
		 * They may not be consecutive if nasid striding is being used.
1380
		 */
1381 1382
		bd2->header.base_dest_nasid = UV_PNODE_TO_NASID(base_pnode);
		bd2->header.dest_subnodeid = UV_LB_SUBNODEID;
1383 1384
		bd2->header.command = UV_NET_ENDPOINT_INTD;
		bd2->header.int_both = 1;
1385 1386 1387 1388 1389
		/*
		 * all others need to be set to zero:
		 *   fairness chaining multilevel count replied_to
		 */
	}
1390 1391 1392 1393 1394 1395
	for_each_present_cpu(cpu) {
		if (pnode != uv_blade_to_pnode(uv_cpu_to_blade_id(cpu)))
			continue;
		bcp = &per_cpu(bau_control, cpu);
		bcp->descriptor_base = bau_desc;
	}
1396 1397 1398 1399
}

/*
 * initialize the destination side's receiving buffers
1400 1401 1402
 * entered for each uvhub in the partition
 * - node is first node (kernel memory notion) on the uvhub
 * - pnode is the uvhub's physical identifier
1403
 */
1404 1405
static void
uv_payload_queue_init(int node, int pnode)
1406
{
1407
	int pn;
1408
	int cpu;
1409
	char *cp;
1410 1411 1412 1413
	unsigned long pa;
	struct bau_payload_queue_entry *pqp;
	struct bau_payload_queue_entry *pqp_malloc;
	struct bau_control *bcp;
1414

1415 1416 1417
	pqp = kmalloc_node((DEST_Q_SIZE + 1)
			   * sizeof(struct bau_payload_queue_entry),
			   GFP_KERNEL, node);
1418
	BUG_ON(!pqp);
1419
	pqp_malloc = pqp;
1420

1421 1422
	cp = (char *)pqp + 31;
	pqp = (struct bau_payload_queue_entry *)(((unsigned long)cp >> 5) << 5);
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432

	for_each_present_cpu(cpu) {
		if (pnode != uv_cpu_to_pnode(cpu))
			continue;
		/* for every cpu on this pnode: */
		bcp = &per_cpu(bau_control, cpu);
		bcp->va_queue_first = pqp;
		bcp->bau_msg_head = pqp;
		bcp->va_queue_last = pqp + (DEST_Q_SIZE - 1);
	}
1433 1434 1435 1436
	/*
	 * need the pnode of where the memory was really allocated
	 */
	pa = uv_gpa(pqp);
1437
	pn = pa >> uv_nshift;
1438 1439
	uv_write_global_mmr64(pnode,
			      UVH_LB_BAU_INTD_PAYLOAD_QUEUE_FIRST,
1440
			      ((unsigned long)pn << UV_PAYLOADQ_PNODE_SHIFT) |
1441 1442 1443 1444 1445
			      uv_physnodeaddr(pqp));
	uv_write_global_mmr64(pnode, UVH_LB_BAU_INTD_PAYLOAD_QUEUE_TAIL,
			      uv_physnodeaddr(pqp));
	uv_write_global_mmr64(pnode, UVH_LB_BAU_INTD_PAYLOAD_QUEUE_LAST,
			      (unsigned long)
1446 1447
			      uv_physnodeaddr(pqp + (DEST_Q_SIZE - 1)));
	/* in effect, all msg_type's are set to MSG_NOOP */
1448
	memset(pqp, 0, sizeof(struct bau_payload_queue_entry) * DEST_Q_SIZE);
1449
}
1450

1451
/*
1452
 * Initialization of each UV hub's structures
1453
 */
1454
static void __init uv_init_uvhub(int uvhub, int vector, int base_pnode)
1455
{
1456
	int node;
1457 1458
	int pnode;
	unsigned long apicid;
1459 1460 1461

	node = uvhub_to_first_node(uvhub);
	pnode = uv_blade_to_pnode(uvhub);
1462
	uv_activation_descriptor_init(node, pnode, base_pnode);
1463
	uv_payload_queue_init(node, pnode);
1464
	/*
1465 1466
	 * The below initialization can't be in firmware because the
	 * messaging IRQ will be determined by the OS.
1467
	 */
1468
	apicid = uvhub_to_first_apicid(uvhub) | uv_apicid_hibits;
1469
	uv_write_global_mmr64(pnode, UVH_BAU_DATA_CONFIG,
1470 1471 1472
				      ((apicid << 32) | vector));
}

1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
/*
 * We will set BAU_MISC_CONTROL with a timeout period.
 * But the BIOS has set UVH_AGING_PRESCALE_SEL and UVH_TRANSACTION_TIMEOUT.
 * So the destination timeout period has be be calculated from them.
 */
static int
calculate_destination_timeout(void)
{
	unsigned long mmr_image;
	int mult1;
	int mult2;
	int index;
	int base;
	int ret;
	unsigned long ts_ns;

	mult1 = UV_INTD_SOFT_ACK_TIMEOUT_PERIOD & BAU_MISC_CONTROL_MULT_MASK;
	mmr_image = uv_read_local_mmr(UVH_AGING_PRESCALE_SEL);
	index = (mmr_image >> BAU_URGENCY_7_SHIFT) & BAU_URGENCY_7_MASK;
	mmr_image = uv_read_local_mmr(UVH_TRANSACTION_TIMEOUT);
	mult2 = (mmr_image >> BAU_TRANS_SHIFT) & BAU_TRANS_MASK;
	base = timeout_base_ns[index];
	ts_ns = base * mult1 * mult2;
	ret = ts_ns / 1000;
	return ret;
}

1500 1501 1502
/*
 * initialize the bau_control structure for each cpu
 */
1503
static int __init uv_init_per_cpu(int nuvhubs, int base_part_pnode)
1504
{
1505
	int i;
1506
	int cpu;
1507
	int tcpu;
1508 1509
	int pnode;
	int uvhub;
C
Cliff Wickman 已提交
1510
	int have_hmaster;
1511
	short socket = 0;
1512
	unsigned short socket_mask;
C
Cliff Wickman 已提交
1513
	unsigned char *uvhub_mask;
1514 1515 1516 1517 1518 1519 1520
	struct bau_control *bcp;
	struct uvhub_desc *bdp;
	struct socket_desc *sdp;
	struct bau_control *hmaster = NULL;
	struct bau_control *smaster = NULL;
	struct socket_desc {
		short num_cpus;
1521
		short cpu_number[MAX_CPUS_PER_SOCKET];
1522 1523
	};
	struct uvhub_desc {
1524
		unsigned short socket_mask;
1525 1526 1527 1528 1529 1530 1531
		short num_cpus;
		short uvhub;
		short pnode;
		struct socket_desc socket[2];
	};
	struct uvhub_desc *uvhub_descs;

1532 1533
	timeout_us = calculate_destination_timeout();

1534
	uvhub_descs = kmalloc(nuvhubs * sizeof(struct uvhub_desc), GFP_KERNEL);
1535
	memset(uvhub_descs, 0, nuvhubs * sizeof(struct uvhub_desc));
C
Cliff Wickman 已提交
1536
	uvhub_mask = kzalloc((nuvhubs+7)/8, GFP_KERNEL);
1537 1538 1539 1540
	for_each_present_cpu(cpu) {
		bcp = &per_cpu(bau_control, cpu);
		memset(bcp, 0, sizeof(struct bau_control));
		pnode = uv_cpu_hub_info(cpu)->pnode;
1541 1542 1543 1544 1545 1546 1547 1548 1549
		if ((pnode - base_part_pnode) >= UV_DISTRIBUTION_SIZE) {
			printk(KERN_EMERG
				"cpu %d pnode %d-%d beyond %d; BAU disabled\n",
				cpu, pnode, base_part_pnode,
				UV_DISTRIBUTION_SIZE);
			return 1;
		}
		bcp->osnode = cpu_to_node(cpu);
		bcp->partition_base_pnode = uv_partition_base_pnode;
1550
		uvhub = uv_cpu_hub_info(cpu)->numa_blade_id;
C
Cliff Wickman 已提交
1551
		*(uvhub_mask + (uvhub/8)) |= (1 << (uvhub%8));
1552 1553 1554 1555
		bdp = &uvhub_descs[uvhub];
		bdp->num_cpus++;
		bdp->uvhub = uvhub;
		bdp->pnode = pnode;
1556 1557
		/* kludge: 'assuming' one node per socket, and assuming that
		   disabling a socket just leaves a gap in node numbers */
1558
		socket = bcp->osnode & 1;
1559
		bdp->socket_mask |= (1 << socket);
1560 1561 1562
		sdp = &bdp->socket[socket];
		sdp->cpu_number[sdp->num_cpus] = cpu;
		sdp->num_cpus++;
1563 1564 1565 1566
		if (sdp->num_cpus > MAX_CPUS_PER_SOCKET) {
			printk(KERN_EMERG "%d cpus per socket invalid\n", sdp->num_cpus);
			return 1;
		}
1567
	}
C
Cliff Wickman 已提交
1568 1569 1570 1571
	for (uvhub = 0; uvhub < nuvhubs; uvhub++) {
		if (!(*(uvhub_mask + (uvhub/8)) & (1 << (uvhub%8))))
			continue;
		have_hmaster = 0;
1572
		bdp = &uvhub_descs[uvhub];
1573 1574 1575 1576 1577 1578 1579 1580
		socket_mask = bdp->socket_mask;
		socket = 0;
		while (socket_mask) {
			if (!(socket_mask & 1))
				goto nextsocket;
			sdp = &bdp->socket[socket];
			for (i = 0; i < sdp->num_cpus; i++) {
				cpu = sdp->cpu_number[i];
1581 1582
				bcp = &per_cpu(bau_control, cpu);
				bcp->cpu = cpu;
1583
				if (i == 0) {
1584
					smaster = bcp;
C
Cliff Wickman 已提交
1585 1586
					if (!have_hmaster) {
						have_hmaster++;
1587
						hmaster = bcp;
C
Cliff Wickman 已提交
1588
					}
1589 1590 1591 1592
				}
				bcp->cpus_in_uvhub = bdp->num_cpus;
				bcp->cpus_in_socket = sdp->num_cpus;
				bcp->socket_master = smaster;
1593
				bcp->uvhub = bdp->uvhub;
1594
				bcp->uvhub_master = hmaster;
1595 1596
				bcp->uvhub_cpu = uv_cpu_hub_info(cpu)->
						blade_processor_id;
1597 1598 1599 1600 1601 1602
				if (bcp->uvhub_cpu >= MAX_CPUS_PER_UVHUB) {
					printk(KERN_EMERG
						"%d cpus per uvhub invalid\n",
						bcp->uvhub_cpu);
					return 1;
				}
1603
			}
1604
nextsocket:
1605
			socket++;
1606
			socket_mask = (socket_mask >> 1);
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620
			/* each socket gets a local array of pnodes/hubs */
			bcp = smaster;
			bcp->target_hub_and_pnode = kmalloc_node(
				sizeof(struct hub_and_pnode) *
				num_possible_cpus(), GFP_KERNEL, bcp->osnode);
			memset(bcp->target_hub_and_pnode, 0,
				sizeof(struct hub_and_pnode) *
				num_possible_cpus());
			for_each_present_cpu(tcpu) {
				bcp->target_hub_and_pnode[tcpu].pnode =
					uv_cpu_hub_info(tcpu)->pnode;
				bcp->target_hub_and_pnode[tcpu].uvhub =
					uv_cpu_hub_info(tcpu)->numa_blade_id;
			}
1621 1622 1623
		}
	}
	kfree(uvhub_descs);
C
Cliff Wickman 已提交
1624
	kfree(uvhub_mask);
1625 1626
	for_each_present_cpu(cpu) {
		bcp = &per_cpu(bau_control, cpu);
1627
		bcp->baudisabled = 0;
1628
		bcp->statp = &per_cpu(ptcstats, cpu);
1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
		/* time interval to catch a hardware stay-busy bug */
		bcp->timeout_interval = microsec_2_cycles(2*timeout_us);
		bcp->max_bau_concurrent = max_bau_concurrent;
		bcp->max_bau_concurrent_constant = max_bau_concurrent;
		bcp->plugged_delay = plugged_delay;
		bcp->plugsb4reset = plugsb4reset;
		bcp->timeoutsb4reset = timeoutsb4reset;
		bcp->ipi_reset_limit = ipi_reset_limit;
		bcp->complete_threshold = complete_threshold;
		bcp->congested_response_us = congested_response_us;
		bcp->congested_reps = congested_reps;
		bcp->congested_period = congested_period;
	}
1642
	return 0;
1643 1644 1645 1646 1647 1648 1649
}

/*
 * Initialization of BAU-related structures
 */
static int __init uv_bau_init(void)
{
1650 1651 1652
	int uvhub;
	int pnode;
	int nuvhubs;
1653
	int cur_cpu;
1654 1655
	int vector;
	unsigned long mmr;
1656 1657 1658

	if (!is_uv_system())
		return 0;
1659

1660 1661 1662
	if (nobau)
		return 0;

1663
	for_each_possible_cpu(cur_cpu)
1664
		zalloc_cpumask_var_node(&per_cpu(uv_flush_tlb_mask, cur_cpu),
1665 1666
				       GFP_KERNEL, cpu_to_node(cur_cpu));

1667
	uv_nshift = uv_hub_info->m_val;
1668
	uv_mmask = (1UL << uv_hub_info->m_val) - 1;
1669
	nuvhubs = uv_num_possible_blades();
1670 1671
	spin_lock_init(&disable_lock);
	congested_cycles = microsec_2_cycles(congested_response_us);
1672

1673
	uv_partition_base_pnode = 0x7fffffff;
1674
	for (uvhub = 0; uvhub < nuvhubs; uvhub++) {
1675 1676 1677
		if (uv_blade_nr_possible_cpus(uvhub) &&
			(uv_blade_to_pnode(uvhub) < uv_partition_base_pnode))
			uv_partition_base_pnode = uv_blade_to_pnode(uvhub);
1678 1679 1680 1681 1682 1683
	}

	if (uv_init_per_cpu(nuvhubs, uv_partition_base_pnode)) {
		nobau = 1;
		return 0;
	}
1684 1685 1686 1687

	vector = UV_BAU_MESSAGE;
	for_each_possible_blade(uvhub)
		if (uv_blade_nr_possible_cpus(uvhub))
1688
			uv_init_uvhub(uvhub, vector, uv_partition_base_pnode);
1689

1690
	uv_enable_timeouts();
1691 1692 1693
	alloc_intr_gate(vector, uv_bau_message_intr1);

	for_each_possible_blade(uvhub) {
1694 1695 1696 1697 1698 1699 1700 1701 1702 1703
		if (uv_blade_nr_possible_cpus(uvhub)) {
			pnode = uv_blade_to_pnode(uvhub);
			/* INIT the bau */
			uv_write_global_mmr64(pnode,
					UVH_LB_BAU_SB_ACTIVATION_CONTROL,
					((unsigned long)1 << 63));
			mmr = 1; /* should be 1 to broadcast to both sockets */
			uv_write_global_mmr64(pnode, UVH_BAU_DATA_BROADCAST,
						mmr);
		}
1704
	}
1705

1706 1707
	return 0;
}
1708
core_initcall(uv_bau_init);
1709
fs_initcall(uv_ptc_init);