tlb_uv.c 55.6 KB
Newer Older
1 2 3
/*
 *	SGI UltraViolet TLB flush routines.
 *
C
Cliff Wickman 已提交
4
 *	(c) 2008-2011 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
/* 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
};
C
Cliff Wickman 已提交
38

39
static int timeout_us;
40
static int nobau;
41 42 43
static int baudisabled;
static spinlock_t disable_lock;
static cycles_t congested_cycles;
44

45
/* tunables: */
C
Cliff Wickman 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
static int max_concurr		= MAX_BAU_CONCURRENT;
static int max_concurr_const	= 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_respns_us	= CONGESTED_RESPONSE_US;
static int congested_reps	= CONGESTED_REPS;
static int congested_period	= CONGESTED_PERIOD;

static struct tunables tunables[] = {
	{&max_concurr, MAX_BAU_CONCURRENT}, /* must be [0] */
	{&plugged_delay, PLUGGED_DELAY},
	{&plugsb4reset, PLUGSB4RESET},
	{&timeoutsb4reset, TIMEOUTSB4RESET},
	{&ipi_reset_limit, IPI_RESET_LIMIT},
	{&complete_threshold, COMPLETE_THRESHOLD},
	{&congested_respns_us, CONGESTED_RESPONSE_US},
	{&congested_reps, CONGESTED_REPS},
	{&congested_period, CONGESTED_PERIOD}
};

69 70
static struct dentry *tunables_dir;
static struct dentry *tunables_file;
71

C
Cliff Wickman 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
/* these correspond to the statistics printed by ptc_seq_show() */
static char *stat_description[] = {
	"sent:     number of shootdown messages sent",
	"stime:    time spent sending messages",
	"numuvhubs: number of hubs targeted with shootdown",
	"numuvhubs16: number times 16 or more hubs targeted",
	"numuvhubs8: number times 8 or more hubs targeted",
	"numuvhubs4: number times 4 or more hubs targeted",
	"numuvhubs2: number times 2 or more hubs targeted",
	"numuvhubs1: number times 1 hub targeted",
	"numcpus:  number of cpus targeted with shootdown",
	"dto:      number of destination timeouts",
	"retries:  destination timeout retries sent",
	"rok:   :  destination timeouts successfully retried",
	"resetp:   ipi-style resource resets for plugs",
	"resett:   ipi-style resource resets for timeouts",
	"giveup:   fall-backs to ipi-style shootdowns",
	"sto:      number of source timeouts",
	"bz:       number of stay-busy's",
	"throt:    number times spun in throttle",
	"swack:   image of UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE",
	"recv:     shootdown messages received",
	"rtime:    time spent processing messages",
	"all:      shootdown all-tlb messages",
	"one:      shootdown one-tlb messages",
	"mult:     interrupts that found multiple messages",
	"none:     interrupts that found no messages",
	"retry:    number of retry messages processed",
	"canc:     number messages canceled by retries",
	"nocan:    number retries that found nothing to cancel",
	"reset:    number of ipi-style reset requests processed",
	"rcan:     number messages canceled by reset requests",
	"disable:  number times use of the BAU was disabled",
	"enable:   number times use of the BAU was re-enabled"
};

static int __init
setup_nobau(char *arg)
110 111 112 113 114
{
	nobau = 1;
	return 0;
}
early_param("nobau", setup_nobau);
115

116
/* base pnode in this partition */
C
Cliff Wickman 已提交
117
static int uv_base_pnode __read_mostly;
118

119 120
static DEFINE_PER_CPU(struct ptc_stats, ptcstats);
static DEFINE_PER_CPU(struct bau_control, bau_control);
121 122
static DEFINE_PER_CPU(cpumask_var_t, uv_flush_tlb_mask);

123
/*
124 125
 * Determine the first node on a uvhub. 'Nodes' are used for kernel
 * memory allocation.
126
 */
127
static int __init uvhub_to_first_node(int uvhub)
128 129 130 131 132
{
	int node, b;

	for_each_online_node(node) {
		b = uv_node_to_blade_id(node);
133
		if (uvhub == b)
134 135
			return node;
	}
136
	return -1;
137 138 139
}

/*
140
 * Determine the apicid of the first cpu on a uvhub.
141
 */
142
static int __init uvhub_to_first_apicid(int uvhub)
143 144 145 146
{
	int cpu;

	for_each_present_cpu(cpu)
147
		if (uvhub == uv_cpu_to_blade_id(cpu))
148 149 150 151
			return per_cpu(x86_cpu_to_apicid, cpu);
	return -1;
}

152 153 154 155 156 157 158 159
/*
 * 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).
 */
C
Cliff Wickman 已提交
160 161
static void reply_to_message(struct msg_desc *mdp, struct bau_control *bcp,
						int do_acknowledge)
162
{
163
	unsigned long dw;
C
Cliff Wickman 已提交
164
	struct bau_pq_entry *msg;
165

166
	msg = mdp->msg;
C
Cliff Wickman 已提交
167
	if (!msg->canceled && do_acknowledge) {
C
Cliff Wickman 已提交
168 169
		dw = (msg->swack_vec << UV_SW_ACK_NPENDING) | msg->swack_vec;
		write_mmr_sw_ack(dw);
170
	}
171
	msg->replied_to = 1;
C
Cliff Wickman 已提交
172
	msg->swack_vec = 0;
173 174 175
}

/*
176
 * Process the receipt of a RETRY message
177
 */
C
Cliff Wickman 已提交
178 179
static void bau_process_retry_msg(struct msg_desc *mdp,
					struct bau_control *bcp)
180
{
181 182 183 184
	int i;
	int cancel_count = 0;
	unsigned long msg_res;
	unsigned long mmr = 0;
C
Cliff Wickman 已提交
185 186 187
	struct bau_pq_entry *msg = mdp->msg;
	struct bau_pq_entry *msg2;
	struct ptc_stats *stat = bcp->statp;
188

189 190 191 192 193
	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++) {
C
Cliff Wickman 已提交
194 195
		if (msg2 > mdp->queue_last)
			msg2 = mdp->queue_first;
196 197 198
		if (msg2 == msg)
			break;

C
Cliff Wickman 已提交
199
		/* same conditions for cancellation as do_reset */
200
		if ((msg2->replied_to == 0) && (msg2->canceled == 0) &&
C
Cliff Wickman 已提交
201 202
		    (msg2->swack_vec) && ((msg2->swack_vec &
			msg->swack_vec) == 0) &&
203 204
		    (msg2->sending_cpu == msg->sending_cpu) &&
		    (msg2->msg_type != MSG_NOOP)) {
C
Cliff Wickman 已提交
205 206
			mmr = read_mmr_sw_ack();
			msg_res = msg2->swack_vec;
207 208 209 210 211 212
			/*
			 * 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.
			 */
213
			if (mmr & (msg_res << UV_SW_ACK_NPENDING)) {
C
Cliff Wickman 已提交
214
				unsigned long mr;
215
				/*
C
Cliff Wickman 已提交
216 217
				 * Is the resource timed out?
				 * Make everyone ignore the cancelled message.
218 219 220 221
				 */
				msg2->canceled = 1;
				stat->d_canceled++;
				cancel_count++;
C
Cliff Wickman 已提交
222 223
				mr = (msg_res << UV_SW_ACK_NPENDING) | msg_res;
				write_mmr_sw_ack(mr);
224
			}
225 226 227 228 229
		}
	}
	if (!cancel_count)
		stat->d_nocanceled++;
}
230

231 232 233 234
/*
 * 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.
 */
C
Cliff Wickman 已提交
235 236
static void bau_process_message(struct msg_desc *mdp, struct bau_control *bcp,
						int do_acknowledge)
237 238
{
	short socket_ack_count = 0;
C
Cliff Wickman 已提交
239 240 241 242
	short *sp;
	struct atomic_short *asp;
	struct ptc_stats *stat = bcp->statp;
	struct bau_pq_entry *msg = mdp->msg;
243
	struct bau_control *smaster = bcp->socket_master;
244

245 246 247
	/*
	 * This must be a normal message, or retry of a normal message
	 */
248 249
	if (msg->address == TLB_FLUSH_ALL) {
		local_flush_tlb();
250
		stat->d_alltlb++;
251 252
	} else {
		__flush_tlb_one(msg->address);
253
		stat->d_onetlb++;
254
	}
255 256 257 258 259 260 261 262 263
	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)
C
Cliff Wickman 已提交
264
		bau_process_retry_msg(mdp, bcp);
265

266
	/*
C
Cliff Wickman 已提交
267
	 * This is a swack message, so we have to reply to it.
268 269 270 271
	 * Count each responding cpu on the socket. This avoids
	 * pinging the count's cache line back and forth between
	 * the sockets.
	 */
C
Cliff Wickman 已提交
272 273 274
	sp = &smaster->socket_acknowledge_count[mdp->msg_slot];
	asp = (struct atomic_short *)sp;
	socket_ack_count = atom_asr(1, asp);
275
	if (socket_ack_count == bcp->cpus_in_socket) {
C
Cliff Wickman 已提交
276
		int msg_ack_count;
277 278 279 280 281
		/*
		 * Both sockets dump their completed count total into
		 * the message's count.
		 */
		smaster->socket_acknowledge_count[mdp->msg_slot] = 0;
C
Cliff Wickman 已提交
282 283
		asp = (struct atomic_short *)&msg->acknowledge_count;
		msg_ack_count = atom_asr(socket_ack_count, asp);
284 285 286 287

		if (msg_ack_count == bcp->cpus_in_uvhub) {
			/*
			 * All cpus in uvhub saw it; reply
C
Cliff Wickman 已提交
288
			 * (unless we are in the UV2 workaround)
289
			 */
C
Cliff Wickman 已提交
290
			reply_to_message(mdp, bcp, do_acknowledge);
291 292
		}
	}
293

294
	return;
295 296 297
}

/*
C
cpw@sgi.com 已提交
298
 * Determine the first cpu on a pnode.
299
 */
C
cpw@sgi.com 已提交
300
static int pnode_to_first_cpu(int pnode, struct bau_control *smaster)
301 302
{
	int cpu;
C
cpw@sgi.com 已提交
303 304 305 306 307
	struct hub_and_pnode *hpp;

	for_each_present_cpu(cpu) {
		hpp = &smaster->thp[cpu];
		if (pnode == hpp->pnode)
308
			return cpu;
C
cpw@sgi.com 已提交
309
	}
310 311 312 313 314 315 316
	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
C
Cliff Wickman 已提交
317
 * can be identified by their nonzero swack_vec field.
318
 *
319 320
 * This is entered for a single cpu on the uvhub.
 * The sender want's this uvhub to free a specific message's
C
Cliff Wickman 已提交
321
 * swack resources.
322
 */
C
Cliff Wickman 已提交
323
static void do_reset(void *ptr)
324
{
325
	int i;
C
Cliff Wickman 已提交
326 327 328 329
	struct bau_control *bcp = &per_cpu(bau_control, smp_processor_id());
	struct reset_args *rap = (struct reset_args *)ptr;
	struct bau_pq_entry *msg;
	struct ptc_stats *stat = bcp->statp;
330

331 332 333
	stat->d_resets++;
	/*
	 * We're looking for the given sender, and
C
Cliff Wickman 已提交
334
	 * will free its swack resource.
335 336 337
	 * If all cpu's finally responded after the timeout, its
	 * message 'replied_to' was set.
	 */
C
Cliff Wickman 已提交
338 339 340 341
	for (msg = bcp->queue_first, i = 0; i < DEST_Q_SIZE; msg++, i++) {
		unsigned long msg_res;
		/* do_reset: same conditions for cancellation as
		   bau_process_retry_msg() */
342 343 344
		if ((msg->replied_to == 0) &&
		    (msg->canceled == 0) &&
		    (msg->sending_cpu == rap->sender) &&
C
Cliff Wickman 已提交
345
		    (msg->swack_vec) &&
346
		    (msg->msg_type != MSG_NOOP)) {
C
Cliff Wickman 已提交
347 348
			unsigned long mmr;
			unsigned long mr;
349 350 351 352 353 354 355
			/*
			 * make everyone else ignore this message
			 */
			msg->canceled = 1;
			/*
			 * only reset the resource if it is still pending
			 */
C
Cliff Wickman 已提交
356 357 358
			mmr = read_mmr_sw_ack();
			msg_res = msg->swack_vec;
			mr = (msg_res << UV_SW_ACK_NPENDING) | msg_res;
359 360
			if (mmr & msg_res) {
				stat->d_rcanceled++;
C
Cliff Wickman 已提交
361
				write_mmr_sw_ack(mr);
362 363 364
			}
		}
	}
365
	return;
366 367 368
}

/*
369 370
 * Use IPI to get all target uvhubs to release resources held by
 * a given sending cpu number.
371
 */
C
cpw@sgi.com 已提交
372
static void reset_with_ipi(struct pnmask *distribution, struct bau_control *bcp)
373
{
C
cpw@sgi.com 已提交
374 375
	int pnode;
	int apnode;
C
Cliff Wickman 已提交
376
	int maskbits;
C
cpw@sgi.com 已提交
377
	int sender = bcp->cpu;
378
	cpumask_t *mask = bcp->uvhub_master->cpumask;
C
cpw@sgi.com 已提交
379
	struct bau_control *smaster = bcp->socket_master;
380
	struct reset_args reset_args;
381

382
	reset_args.sender = sender;
383
	cpus_clear(*mask);
384
	/* find a single cpu for each uvhub in this distribution mask */
C
cpw@sgi.com 已提交
385
	maskbits = sizeof(struct pnmask) * BITSPERBYTE;
C
cpw@sgi.com 已提交
386 387
	/* each bit is a pnode relative to the partition base pnode */
	for (pnode = 0; pnode < maskbits; pnode++) {
C
Cliff Wickman 已提交
388
		int cpu;
C
cpw@sgi.com 已提交
389
		if (!bau_uvhub_isset(pnode, distribution))
390
			continue;
C
cpw@sgi.com 已提交
391 392
		apnode = pnode + bcp->partition_base_pnode;
		cpu = pnode_to_first_cpu(apnode, smaster);
393
		cpu_set(cpu, *mask);
394
	}
C
Cliff Wickman 已提交
395 396

	/* IPI all cpus; preemption is already disabled */
397
	smp_call_function_many(mask, do_reset, (void *)&reset_args, 1);
398 399 400
	return;
}

C
Cliff Wickman 已提交
401
static inline unsigned long cycles_2_us(unsigned long long cyc)
402 403 404
{
	unsigned long long ns;
	unsigned long us;
C
Cliff Wickman 已提交
405 406 407
	int cpu = smp_processor_id();

	ns =  (cyc * per_cpu(cyc2ns, cpu)) >> CYC2NS_SCALE_FACTOR;
408 409
	us = ns / 1000;
	return us;
410 411
}

412
/*
413 414 415 416
 * 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()
 */
C
Cliff Wickman 已提交
417
static inline void quiesce_local_uvhub(struct bau_control *hmaster)
418
{
C
Cliff Wickman 已提交
419
	atom_asr(1, (struct atomic_short *)&hmaster->uvhub_quiesce);
420 421 422 423 424
}

/*
 * mark this quiet-requestor as done
 */
C
Cliff Wickman 已提交
425
static inline void end_uvhub_quiesce(struct bau_control *hmaster)
426
{
C
Cliff Wickman 已提交
427 428 429 430 431 432 433 434 435 436 437
	atom_asr(-1, (struct atomic_short *)&hmaster->uvhub_quiesce);
}

static unsigned long uv1_read_status(unsigned long mmr_offset, int right_shift)
{
	unsigned long descriptor_status;

	descriptor_status = uv_read_local_mmr(mmr_offset);
	descriptor_status >>= right_shift;
	descriptor_status &= UV_ACT_STATUS_MASK;
	return descriptor_status;
438 439 440 441 442
}

/*
 * Wait for completion of a broadcast software ack message
 * return COMPLETE, RETRY(PLUGGED or TIMEOUT) or GIVEUP
443
 */
444
static int uv1_wait_completion(struct bau_desc *bau_desc,
C
Cliff Wickman 已提交
445 446
				unsigned long mmr_offset, int right_shift,
				struct bau_control *bcp, long try)
447 448
{
	unsigned long descriptor_status;
C
Cliff Wickman 已提交
449
	cycles_t ttm;
450
	struct ptc_stats *stat = bcp->statp;
451

C
Cliff Wickman 已提交
452
	descriptor_status = uv1_read_status(mmr_offset, right_shift);
453
	/* spin on the status MMR, waiting for it to go idle */
C
Cliff Wickman 已提交
454
	while ((descriptor_status != DS_IDLE)) {
455
		/*
456 457 458 459
		 * 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.
460
		 */
C
Cliff Wickman 已提交
461
		if (descriptor_status == DS_SOURCE_TIMEOUT) {
462 463
			stat->s_stimeout++;
			return FLUSH_GIVEUP;
C
Cliff Wickman 已提交
464
		} else if (descriptor_status == DS_DESTINATION_TIMEOUT) {
465
			stat->s_dtimeout++;
C
Cliff Wickman 已提交
466
			ttm = get_cycles();
467 468 469 470 471 472 473

			/*
			 * 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.
			 */
C
Cliff Wickman 已提交
474
			if (cycles_2_us(ttm - bcp->send_message) < timeout_us) {
475 476 477 478 479 480 481 482 483 484 485 486
				bcp->conseccompletes = 0;
				return FLUSH_RETRY_PLUGGED;
			}

			bcp->conseccompletes = 0;
			return FLUSH_RETRY_TIMEOUT;
		} else {
			/*
			 * descriptor_status is still BUSY
			 */
			cpu_relax();
		}
C
Cliff Wickman 已提交
487
		descriptor_status = uv1_read_status(mmr_offset, right_shift);
488 489 490 491 492
	}
	bcp->conseccompletes++;
	return FLUSH_COMPLETE;
}

C
Cliff Wickman 已提交
493 494 495
/*
 * UV2 has an extra bit of status in the ACTIVATION_STATUS_2 register.
 */
C
Cliff Wickman 已提交
496
static unsigned long uv2_read_status(unsigned long offset, int rshft, int desc)
497 498 499
{
	unsigned long descriptor_status;
	unsigned long descriptor_status2;
C
Cliff Wickman 已提交
500 501

	descriptor_status = ((read_lmmr(offset) >> rshft) & UV_ACT_STATUS_MASK);
C
Cliff Wickman 已提交
502
	descriptor_status2 = (read_mmr_uv2_status() >> desc) & 0x1UL;
C
Cliff Wickman 已提交
503 504 505 506
	descriptor_status = (descriptor_status << 1) | descriptor_status2;
	return descriptor_status;
}

C
Cliff Wickman 已提交
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 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 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
/*
 * Return whether the status of the descriptor that is normally used for this
 * cpu (the one indexed by its hub-relative cpu number) is busy.
 * The status of the original 32 descriptors is always reflected in the 64
 * bits of UVH_LB_BAU_SB_ACTIVATION_STATUS_0.
 * The bit provided by the activation_status_2 register is irrelevant to
 * the status if it is only being tested for busy or not busy.
 */
int normal_busy(struct bau_control *bcp)
{
	int cpu = bcp->uvhub_cpu;
	int mmr_offset;
	int right_shift;

	mmr_offset = UVH_LB_BAU_SB_ACTIVATION_STATUS_0;
	right_shift = cpu * UV_ACT_STATUS_SIZE;
	return (((((read_lmmr(mmr_offset) >> right_shift) &
				UV_ACT_STATUS_MASK)) << 1) == UV2H_DESC_BUSY);
}

/*
 * Entered when a bau descriptor has gone into a permanent busy wait because
 * of a hardware bug.
 * Workaround the bug.
 */
int handle_uv2_busy(struct bau_control *bcp)
{
	int busy_one = bcp->using_desc;
	int normal = bcp->uvhub_cpu;
	int selected = -1;
	int i;
	unsigned long descriptor_status;
	unsigned long status;
	int mmr_offset;
	struct bau_desc *bau_desc_old;
	struct bau_desc *bau_desc_new;
	struct bau_control *hmaster = bcp->uvhub_master;
	struct ptc_stats *stat = bcp->statp;
	cycles_t ttm;

	stat->s_uv2_wars++;
	spin_lock(&hmaster->uvhub_lock);
	/* try for the original first */
	if (busy_one != normal) {
		if (!normal_busy(bcp))
			selected = normal;
	}
	if (selected < 0) {
		/* can't use the normal, select an alternate */
		mmr_offset = UVH_LB_BAU_SB_ACTIVATION_STATUS_1;
		descriptor_status = read_lmmr(mmr_offset);

		/* scan available descriptors 32-63 */
		for (i = 0; i < UV_CPUS_PER_AS; i++) {
			if ((hmaster->inuse_map & (1 << i)) == 0) {
				status = ((descriptor_status >>
						(i * UV_ACT_STATUS_SIZE)) &
						UV_ACT_STATUS_MASK) << 1;
				if (status != UV2H_DESC_BUSY) {
					selected = i + UV_CPUS_PER_AS;
					break;
				}
			}
		}
	}

	if (busy_one != normal)
		/* mark the busy alternate as not in-use */
		hmaster->inuse_map &= ~(1 << (busy_one - UV_CPUS_PER_AS));

	if (selected >= 0) {
		/* switch to the selected descriptor */
		if (selected != normal) {
			/* set the selected alternate as in-use */
			hmaster->inuse_map |=
					(1 << (selected - UV_CPUS_PER_AS));
			if (selected > stat->s_uv2_wars_hw)
				stat->s_uv2_wars_hw = selected;
		}
		bau_desc_old = bcp->descriptor_base;
		bau_desc_old += (ITEMS_PER_DESC * busy_one);
		bcp->using_desc = selected;
		bau_desc_new = bcp->descriptor_base;
		bau_desc_new += (ITEMS_PER_DESC * selected);
		*bau_desc_new = *bau_desc_old;
	} else {
		/*
		 * All are busy. Wait for the normal one for this cpu to
		 * free up.
		 */
		stat->s_uv2_war_waits++;
		spin_unlock(&hmaster->uvhub_lock);
		ttm = get_cycles();
		do {
			cpu_relax();
		} while (normal_busy(bcp));
		spin_lock(&hmaster->uvhub_lock);
		/* switch to the original descriptor */
		bcp->using_desc = normal;
		bau_desc_old = bcp->descriptor_base;
		bau_desc_old += (ITEMS_PER_DESC * bcp->using_desc);
		bcp->using_desc = (ITEMS_PER_DESC * normal);
		bau_desc_new = bcp->descriptor_base;
		bau_desc_new += (ITEMS_PER_DESC * normal);
		*bau_desc_new = *bau_desc_old; /* copy the entire descriptor */
	}
	spin_unlock(&hmaster->uvhub_lock);
	return FLUSH_RETRY_BUSYBUG;
}

C
Cliff Wickman 已提交
617 618 619 620 621 622
static int uv2_wait_completion(struct bau_desc *bau_desc,
				unsigned long mmr_offset, int right_shift,
				struct bau_control *bcp, long try)
{
	unsigned long descriptor_stat;
	cycles_t ttm;
C
Cliff Wickman 已提交
623 624
	int desc = bcp->using_desc;
	long busy_reps = 0;
625 626
	struct ptc_stats *stat = bcp->statp;

C
Cliff Wickman 已提交
627
	descriptor_stat = uv2_read_status(mmr_offset, right_shift, desc);
C
Cliff Wickman 已提交
628

629
	/* spin on the status MMR, waiting for it to go idle */
C
Cliff Wickman 已提交
630
	while (descriptor_stat != UV2H_DESC_IDLE) {
631 632 633 634 635 636
		/*
		 * 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.
		 */
C
Cliff Wickman 已提交
637 638
		if ((descriptor_stat == UV2H_DESC_SOURCE_TIMEOUT) ||
		    (descriptor_stat == UV2H_DESC_DEST_PUT_ERR)) {
639 640
			stat->s_stimeout++;
			return FLUSH_GIVEUP;
641 642 643 644
		} else if (descriptor_stat == UV2H_DESC_DEST_STRONG_NACK) {
			stat->s_strongnacks++;
			bcp->conseccompletes = 0;
			return FLUSH_GIVEUP;
C
Cliff Wickman 已提交
645
		} else if (descriptor_stat == UV2H_DESC_DEST_TIMEOUT) {
646 647 648 649
			stat->s_dtimeout++;
			bcp->conseccompletes = 0;
			return FLUSH_RETRY_TIMEOUT;
		} else {
C
Cliff Wickman 已提交
650 651 652 653 654 655 656 657 658 659
			busy_reps++;
			if (busy_reps > 1000000) {
				/* not to hammer on the clock */
				busy_reps = 0;
				ttm = get_cycles();
				if ((ttm - bcp->send_message) >
					(bcp->clocks_per_100_usec)) {
					return handle_uv2_busy(bcp);
				}
			}
660
			/*
C
Cliff Wickman 已提交
661
			 * descriptor_stat is still BUSY
662 663
			 */
			cpu_relax();
664
		}
C
Cliff Wickman 已提交
665 666
		descriptor_stat = uv2_read_status(mmr_offset, right_shift,
									desc);
667
	}
668
	bcp->conseccompletes++;
669 670 671
	return FLUSH_COMPLETE;
}

C
Cliff Wickman 已提交
672 673 674 675 676 677 678
/*
 * There are 2 status registers; each and array[32] of 2 bits. Set up for
 * which register to read and position in that register based on cpu in
 * current hub.
 */
static int wait_completion(struct bau_desc *bau_desc,
				struct bau_control *bcp, long try)
679
{
C
Cliff Wickman 已提交
680 681
	int right_shift;
	unsigned long mmr_offset;
C
Cliff Wickman 已提交
682
	int desc = bcp->using_desc;
C
Cliff Wickman 已提交
683

C
Cliff Wickman 已提交
684
	if (desc < UV_CPUS_PER_AS) {
C
Cliff Wickman 已提交
685
		mmr_offset = UVH_LB_BAU_SB_ACTIVATION_STATUS_0;
C
Cliff Wickman 已提交
686
		right_shift = desc * UV_ACT_STATUS_SIZE;
C
Cliff Wickman 已提交
687 688
	} else {
		mmr_offset = UVH_LB_BAU_SB_ACTIVATION_STATUS_1;
C
Cliff Wickman 已提交
689
		right_shift = ((desc - UV_CPUS_PER_AS) * UV_ACT_STATUS_SIZE);
C
Cliff Wickman 已提交
690 691
	}

692
	if (bcp->uvhub_version == 1)
693
		return uv1_wait_completion(bau_desc, mmr_offset, right_shift,
C
Cliff Wickman 已提交
694
								bcp, try);
695 696
	else
		return uv2_wait_completion(bau_desc, mmr_offset, right_shift,
C
Cliff Wickman 已提交
697
								bcp, try);
698 699
}

C
Cliff Wickman 已提交
700
static inline cycles_t sec_2_cycles(unsigned long sec)
701 702 703 704 705 706 707 708 709 710
{
	unsigned long ns;
	cycles_t cyc;

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

/*
C
Cliff Wickman 已提交
711
 * Our retries are blocked by all destination sw ack resources being
712 713 714
 * in use, and a timeout is pending. In that case hardware immediately
 * returns the ERROR that looks like a destination timeout.
 */
C
Cliff Wickman 已提交
715 716
static void destination_plugged(struct bau_desc *bau_desc,
			struct bau_control *bcp,
717 718 719 720
			struct bau_control *hmaster, struct ptc_stats *stat)
{
	udelay(bcp->plugged_delay);
	bcp->plugged_tries++;
C
Cliff Wickman 已提交
721

722 723
	if (bcp->plugged_tries >= bcp->plugsb4reset) {
		bcp->plugged_tries = 0;
C
Cliff Wickman 已提交
724

725
		quiesce_local_uvhub(hmaster);
C
Cliff Wickman 已提交
726

727
		spin_lock(&hmaster->queue_lock);
C
cpw@sgi.com 已提交
728
		reset_with_ipi(&bau_desc->distribution, bcp);
729
		spin_unlock(&hmaster->queue_lock);
C
Cliff Wickman 已提交
730

731
		end_uvhub_quiesce(hmaster);
C
Cliff Wickman 已提交
732

733 734 735 736 737
		bcp->ipi_attempts++;
		stat->s_resets_plug++;
	}
}

C
Cliff Wickman 已提交
738 739 740
static void destination_timeout(struct bau_desc *bau_desc,
			struct bau_control *bcp, struct bau_control *hmaster,
			struct ptc_stats *stat)
741
{
C
Cliff Wickman 已提交
742
	hmaster->max_concurr = 1;
743 744 745
	bcp->timeout_tries++;
	if (bcp->timeout_tries >= bcp->timeoutsb4reset) {
		bcp->timeout_tries = 0;
C
Cliff Wickman 已提交
746

747
		quiesce_local_uvhub(hmaster);
C
Cliff Wickman 已提交
748

749
		spin_lock(&hmaster->queue_lock);
C
cpw@sgi.com 已提交
750
		reset_with_ipi(&bau_desc->distribution, bcp);
751
		spin_unlock(&hmaster->queue_lock);
C
Cliff Wickman 已提交
752

753
		end_uvhub_quiesce(hmaster);
C
Cliff Wickman 已提交
754

755 756 757 758 759
		bcp->ipi_attempts++;
		stat->s_resets_timeout++;
	}
}

760 761 762 763
/*
 * Completions are taking a very long time due to a congested numalink
 * network.
 */
C
Cliff Wickman 已提交
764 765
static void disable_for_congestion(struct bau_control *bcp,
					struct ptc_stats *stat)
766 767 768
{
	/* let only one cpu do this disabling */
	spin_lock(&disable_lock);
C
Cliff Wickman 已提交
769

770 771
	if (!baudisabled && bcp->period_requests &&
	    ((bcp->period_time / bcp->period_requests) > congested_cycles)) {
C
Cliff Wickman 已提交
772 773
		int tcpu;
		struct bau_control *tbcp;
774 775 776 777
		/* it becomes this cpu's job to turn on the use of the
		   BAU again */
		baudisabled = 1;
		bcp->set_bau_off = 1;
C
Cliff Wickman 已提交
778 779
		bcp->set_bau_on_time = get_cycles();
		bcp->set_bau_on_time += sec_2_cycles(bcp->cong_period);
780 781 782
		stat->s_bau_disabled++;
		for_each_present_cpu(tcpu) {
			tbcp = &per_cpu(bau_control, tcpu);
C
Cliff Wickman 已提交
783
			tbcp->baudisabled = 1;
784 785
		}
	}
C
Cliff Wickman 已提交
786

787 788 789
	spin_unlock(&disable_lock);
}

C
Cliff Wickman 已提交
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
static void count_max_concurr(int stat, struct bau_control *bcp,
				struct bau_control *hmaster)
{
	bcp->plugged_tries = 0;
	bcp->timeout_tries = 0;
	if (stat != FLUSH_COMPLETE)
		return;
	if (bcp->conseccompletes <= bcp->complete_threshold)
		return;
	if (hmaster->max_concurr >= hmaster->max_concurr_const)
		return;
	hmaster->max_concurr++;
}

static void record_send_stats(cycles_t time1, cycles_t time2,
		struct bau_control *bcp, struct ptc_stats *stat,
		int completion_status, int try)
{
	cycles_t elapsed;

	if (time2 > time1) {
		elapsed = time2 - time1;
		stat->s_time += elapsed;

		if ((completion_status == FLUSH_COMPLETE) && (try == 1)) {
			bcp->period_requests++;
			bcp->period_time += elapsed;
			if ((elapsed > congested_cycles) &&
			    (bcp->period_requests > bcp->cong_reps))
				disable_for_congestion(bcp, stat);
		}
	} else
		stat->s_requestor--;

	if (completion_status == FLUSH_COMPLETE && try > 1)
		stat->s_retriesok++;
	else if (completion_status == FLUSH_GIVEUP)
		stat->s_giveup++;
}

/*
 * Because of a uv1 hardware bug only a limited number of concurrent
 * requests can be made.
 */
static void uv1_throttle(struct bau_control *hmaster, struct ptc_stats *stat)
{
	spinlock_t *lock = &hmaster->uvhub_lock;
	atomic_t *v;

	v = &hmaster->active_descriptor_count;
	if (!atomic_inc_unless_ge(lock, v, hmaster->max_concurr)) {
		stat->s_throttles++;
		do {
			cpu_relax();
		} while (!atomic_inc_unless_ge(lock, v, hmaster->max_concurr));
	}
}

/*
 * Handle the completion status of a message send.
 */
static void handle_cmplt(int completion_status, struct bau_desc *bau_desc,
			struct bau_control *bcp, struct bau_control *hmaster,
			struct ptc_stats *stat)
{
	if (completion_status == FLUSH_RETRY_PLUGGED)
		destination_plugged(bau_desc, bcp, hmaster, stat);
	else if (completion_status == FLUSH_RETRY_TIMEOUT)
		destination_timeout(bau_desc, bcp, hmaster, stat);
}

/*
862
 * Send a broadcast and wait for it to complete.
863
 *
864
 * The flush_mask contains the cpus the broadcast is to be sent to including
865
 * cpus that are on the local uvhub.
866
 *
867 868 869
 * 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.
870
 */
C
Cliff Wickman 已提交
871
int uv_flush_send_and_wait(struct cpumask *flush_mask, struct bau_control *bcp)
872
{
873
	int seq_number = 0;
C
Cliff Wickman 已提交
874
	int completion_stat = 0;
875
	int uv1 = 0;
876
	long try = 0;
877
	unsigned long index;
878 879
	cycles_t time1;
	cycles_t time2;
880
	struct ptc_stats *stat = bcp->statp;
881
	struct bau_control *hmaster = bcp->uvhub_master;
882 883
	struct uv1_bau_msg_header *uv1_hdr = NULL;
	struct uv2_bau_msg_header *uv2_hdr = NULL;
C
Cliff Wickman 已提交
884
	struct bau_desc *bau_desc;
885

C
Cliff Wickman 已提交
886
	if (bcp->uvhub_version == 1)
C
Cliff Wickman 已提交
887 888
		uv1_throttle(hmaster, stat);

889 890
	while (hmaster->uvhub_quiesce)
		cpu_relax();
891 892 893

	time1 = get_cycles();
	do {
C
Cliff Wickman 已提交
894 895 896 897 898 899 900 901
		bau_desc = bcp->descriptor_base;
		bau_desc += (ITEMS_PER_DESC * bcp->using_desc);
		if (bcp->uvhub_version == 1) {
			uv1 = 1;
			uv1_hdr = &bau_desc->header.uv1_hdr;
		} else
			uv2_hdr = &bau_desc->header.uv2_hdr;
		if ((try == 0) || (completion_stat == FLUSH_RETRY_BUSYBUG)) {
902 903 904 905
			if (uv1)
				uv1_hdr->msg_type = MSG_REGULAR;
			else
				uv2_hdr->msg_type = MSG_REGULAR;
906 907
			seq_number = bcp->message_number++;
		} else {
908 909 910 911
			if (uv1)
				uv1_hdr->msg_type = MSG_RETRY;
			else
				uv2_hdr->msg_type = MSG_RETRY;
912 913
			stat->s_retry_messages++;
		}
C
Cliff Wickman 已提交
914

915 916 917 918
		if (uv1)
			uv1_hdr->sequence = seq_number;
		else
			uv2_hdr->sequence = seq_number;
C
Cliff Wickman 已提交
919
		index = (1UL << AS_PUSH_SHIFT) | bcp->using_desc;
920
		bcp->send_message = get_cycles();
C
Cliff Wickman 已提交
921 922 923

		write_mmr_activation(index);

924
		try++;
C
Cliff Wickman 已提交
925
		completion_stat = wait_completion(bau_desc, bcp, try);
C
Cliff Wickman 已提交
926
		/* UV2: wait_completion() may change the bcp->using_desc */
C
Cliff Wickman 已提交
927 928

		handle_cmplt(completion_stat, bau_desc, bcp, hmaster, stat);
929

930
		if (bcp->ipi_attempts >= bcp->ipi_reset_limit) {
931
			bcp->ipi_attempts = 0;
C
Cliff Wickman 已提交
932
			completion_stat = FLUSH_GIVEUP;
933 934 935
			break;
		}
		cpu_relax();
C
Cliff Wickman 已提交
936
	} while ((completion_stat == FLUSH_RETRY_PLUGGED) ||
C
Cliff Wickman 已提交
937
		 (completion_stat == FLUSH_RETRY_BUSYBUG) ||
C
Cliff Wickman 已提交
938 939
		 (completion_stat == FLUSH_RETRY_TIMEOUT));

940
	time2 = get_cycles();
C
Cliff Wickman 已提交
941 942 943

	count_max_concurr(completion_stat, bcp, hmaster);

944 945
	while (hmaster->uvhub_quiesce)
		cpu_relax();
C
Cliff Wickman 已提交
946

947
	atomic_dec(&hmaster->active_descriptor_count);
C
Cliff Wickman 已提交
948 949 950 951

	record_send_stats(time1, time2, bcp, stat, completion_stat, try);

	if (completion_stat == FLUSH_GIVEUP)
C
Cliff Wickman 已提交
952
		/* FLUSH_GIVEUP will fall back to using IPI's for tlb flush */
C
Cliff Wickman 已提交
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
		return 1;
	return 0;
}

/*
 * The BAU is disabled. When the disabled time period has expired, the cpu
 * that disabled it must re-enable it.
 * Return 0 if it is re-enabled for all cpus.
 */
static int check_enable(struct bau_control *bcp, struct ptc_stats *stat)
{
	int tcpu;
	struct bau_control *tbcp;

	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;
976
			}
C
Cliff Wickman 已提交
977
			return 0;
978
		}
C
Cliff Wickman 已提交
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
	}
	return -1;
}

static void record_send_statistics(struct ptc_stats *stat, int locals, int hubs,
				int remotes, struct bau_desc *bau_desc)
{
	stat->s_requestor++;
	stat->s_ntargcpu += remotes + locals;
	stat->s_ntargremotes += remotes;
	stat->s_ntarglocals += locals;

	/* uvhub statistics */
	hubs = bau_uvhub_weight(&bau_desc->distribution);
	if (locals) {
		stat->s_ntarglocaluvhub++;
		stat->s_ntargremoteuvhub += (hubs - 1);
996
	} else
C
Cliff Wickman 已提交
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
		stat->s_ntargremoteuvhub += hubs;

	stat->s_ntarguvhub += hubs;

	if (hubs >= 16)
		stat->s_ntarguvhub16++;
	else if (hubs >= 8)
		stat->s_ntarguvhub8++;
	else if (hubs >= 4)
		stat->s_ntarguvhub4++;
	else if (hubs >= 2)
		stat->s_ntarguvhub2++;
	else
		stat->s_ntarguvhub1++;
}

/*
 * Translate a cpu mask to the uvhub distribution mask in the BAU
 * activation descriptor.
 */
static int set_distrib_bits(struct cpumask *flush_mask, struct bau_control *bcp,
			struct bau_desc *bau_desc, int *localsp, int *remotesp)
{
	int cpu;
	int pnode;
	int cnt = 0;
	struct hub_and_pnode *hpp;

	for_each_cpu(cpu, flush_mask) {
		/*
		 * 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 a local memory array.
		 */
		hpp = &bcp->socket_master->thp[cpu];
		pnode = hpp->pnode - bcp->partition_base_pnode;
		bau_uvhub_set(pnode, &bau_desc->distribution);
		cnt++;
		if (hpp->uvhub == bcp->uvhub)
			(*localsp)++;
		else
			(*remotesp)++;
1040
	}
C
Cliff Wickman 已提交
1041 1042
	if (!cnt)
		return 1;
1043
	return 0;
1044 1045
}

C
Cliff Wickman 已提交
1046 1047
/*
 * globally purge translation cache of a virtual address or all TLB's
T
Tejun Heo 已提交
1048
 * @cpumask: mask of all cpu's in which the address is to be removed
1049 1050
 * @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 已提交
1051
 * @cpu: the current cpu
1052 1053 1054 1055 1056 1057
 *
 * 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 已提交
1058 1059
 * 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())
1060
 *
1061 1062
 * The cpumask is converted into a uvhubmask of the uvhubs containing
 * those cpus.
1063
 *
T
Tejun Heo 已提交
1064 1065 1066 1067 1068
 * 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.
1069
 */
T
Tejun Heo 已提交
1070
const struct cpumask *uv_flush_tlb_others(const struct cpumask *cpumask,
C
Cliff Wickman 已提交
1071 1072
				struct mm_struct *mm, unsigned long va,
				unsigned int cpu)
1073
{
1074
	int locals = 0;
1075 1076
	int remotes = 0;
	int hubs = 0;
1077
	struct bau_desc *bau_desc;
1078 1079 1080
	struct cpumask *flush_mask;
	struct ptc_stats *stat;
	struct bau_control *bcp;
T
Tejun Heo 已提交
1081

1082
	/* kernel was booted 'nobau' */
1083 1084
	if (nobau)
		return cpumask;
T
Tejun Heo 已提交
1085

1086
	bcp = &per_cpu(bau_control, cpu);
1087
	stat = bcp->statp;
1088 1089 1090

	/* bau was disabled due to slow response */
	if (bcp->baudisabled) {
C
Cliff Wickman 已提交
1091 1092
		if (check_enable(bcp, stat))
			return cpumask;
1093
	}
1094

1095 1096
	/*
	 * Each sending cpu has a per-cpu mask which it fills from the caller's
1097 1098
	 * cpu mask.  All cpus are converted to uvhubs and copied to the
	 * activation descriptor.
1099 1100
	 */
	flush_mask = (struct cpumask *)per_cpu(uv_flush_tlb_mask, cpu);
1101
	/* don't actually do a shootdown of the local cpu */
1102
	cpumask_andnot(flush_mask, cpumask, cpumask_of(cpu));
C
Cliff Wickman 已提交
1103

1104
	if (cpu_isset(cpu, *cpumask))
1105
		stat->s_ntargself++;
1106

1107
	bau_desc = bcp->descriptor_base;
C
Cliff Wickman 已提交
1108
	bau_desc += (ITEMS_PER_DESC * bcp->using_desc);
1109
	bau_uvhubs_clear(&bau_desc->distribution, UV_DISTRIBUTION_SIZE);
C
Cliff Wickman 已提交
1110
	if (set_distrib_bits(flush_mask, bcp, bau_desc, &locals, &remotes))
1111 1112
		return NULL;

C
Cliff Wickman 已提交
1113
	record_send_statistics(stat, locals, hubs, remotes, bau_desc);
1114 1115

	bau_desc->payload.address = va;
T
Tejun Heo 已提交
1116
	bau_desc->payload.sending_cpu = cpu;
1117
	/*
1118 1119
	 * 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.
1120
	 */
C
Cliff Wickman 已提交
1121
	if (!uv_flush_send_and_wait(flush_mask, bcp))
1122 1123 1124
		return NULL;
	else
		return cpumask;
1125 1126
}

C
Cliff Wickman 已提交
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 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
/*
 * Search the message queue for any 'other' message with the same software
 * acknowledge resource bit vector.
 */
struct bau_pq_entry *find_another_by_swack(struct bau_pq_entry *msg,
			struct bau_control *bcp, unsigned char swack_vec)
{
	struct bau_pq_entry *msg_next = msg + 1;

	if (msg_next > bcp->queue_last)
		msg_next = bcp->queue_first;
	while ((msg_next->swack_vec != 0) && (msg_next != msg)) {
		if (msg_next->swack_vec == swack_vec)
			return msg_next;
		msg_next++;
		if (msg_next > bcp->queue_last)
			msg_next = bcp->queue_first;
	}
	return NULL;
}

/*
 * UV2 needs to work around a bug in which an arriving message has not
 * set a bit in the UVH_LB_BAU_INTD_SOFTWARE_ACKNOWLEDGE register.
 * Such a message must be ignored.
 */
void process_uv2_message(struct msg_desc *mdp, struct bau_control *bcp)
{
	unsigned long mmr_image;
	unsigned char swack_vec;
	struct bau_pq_entry *msg = mdp->msg;
	struct bau_pq_entry *other_msg;

	mmr_image = read_mmr_sw_ack();
	swack_vec = msg->swack_vec;

	if ((swack_vec & mmr_image) == 0) {
		/*
		 * This message was assigned a swack resource, but no
		 * reserved acknowlegment is pending.
		 * The bug has prevented this message from setting the MMR.
		 * And no other message has used the same sw_ack resource.
		 * Do the requested shootdown but do not reply to the msg.
		 * (the 0 means make no acknowledge)
		 */
		bau_process_message(mdp, bcp, 0);
		return;
	}

	/*
	 * Some message has set the MMR 'pending' bit; it might have been
	 * another message.  Look for that message.
	 */
	other_msg = find_another_by_swack(msg, bcp, msg->swack_vec);
	if (other_msg) {
		/* There is another.  Do not ack the current one. */
		bau_process_message(mdp, bcp, 0);
		/*
		 * Let the natural processing of that message acknowledge
		 * it. Don't get the processing of sw_ack's out of order.
		 */
		return;
	}

	/*
	 * There is no other message using this sw_ack, so it is safe to
	 * acknowledge it.
	 */
	bau_process_message(mdp, bcp, 1);

	return;
}

1200 1201 1202 1203 1204 1205
/*
 * The BAU message interrupt comes here. (registered by set_intr_gate)
 * See entry_64.S
 *
 * We received a broadcast assist message.
 *
1206
 * Interrupts are disabled; this interrupt could represent
1207 1208
 * the receipt of several messages.
 *
1209 1210
 * All cores/threads on this hub get this interrupt.
 * The last one to see it does the software ack.
1211
 * (the resource will not be freed until noninterruptable cpus see this
1212
 *  interrupt; hardware may timeout the s/w ack and reply ERROR)
1213
 */
1214
void uv_bau_message_interrupt(struct pt_regs *regs)
1215 1216
{
	int count = 0;
1217
	cycles_t time_start;
C
Cliff Wickman 已提交
1218
	struct bau_pq_entry *msg;
1219 1220 1221 1222
	struct bau_control *bcp;
	struct ptc_stats *stat;
	struct msg_desc msgdesc;

1223
	ack_APIC_irq();
1224
	time_start = get_cycles();
C
Cliff Wickman 已提交
1225

1226
	bcp = &per_cpu(bau_control, smp_processor_id());
1227
	stat = bcp->statp;
C
Cliff Wickman 已提交
1228 1229 1230 1231

	msgdesc.queue_first = bcp->queue_first;
	msgdesc.queue_last = bcp->queue_last;

1232
	msg = bcp->bau_msg_head;
C
Cliff Wickman 已提交
1233
	while (msg->swack_vec) {
1234
		count++;
C
Cliff Wickman 已提交
1235 1236

		msgdesc.msg_slot = msg - msgdesc.queue_first;
1237
		msgdesc.msg = msg;
C
Cliff Wickman 已提交
1238 1239 1240 1241
		if (bcp->uvhub_version == 2)
			process_uv2_message(&msgdesc, bcp);
		else
			bau_process_message(&msgdesc, bcp, 1);
C
Cliff Wickman 已提交
1242

1243
		msg++;
C
Cliff Wickman 已提交
1244 1245
		if (msg > msgdesc.queue_last)
			msg = msgdesc.queue_first;
1246
		bcp->bau_msg_head = msg;
1247
	}
1248
	stat->d_time += (get_cycles() - time_start);
1249
	if (!count)
1250
		stat->d_nomsg++;
1251
	else if (count > 1)
1252
		stat->d_multmsg++;
1253 1254
}

C
Cliff Wickman 已提交
1255
/*
C
Cliff Wickman 已提交
1256
 * Each target uvhub (i.e. a uvhub that has cpu's) needs to have
C
Cliff Wickman 已提交
1257 1258 1259 1260
 * shootdown message timeouts enabled.  The timeout does not cause
 * an interrupt, but causes an error message to be returned to
 * the sender.
 */
C
Cliff Wickman 已提交
1261
static void __init enable_timeouts(void)
1262
{
1263 1264
	int uvhub;
	int nuvhubs;
1265
	int pnode;
C
Cliff Wickman 已提交
1266
	unsigned long mmr_image;
1267

1268
	nuvhubs = uv_num_possible_blades();
1269

1270 1271
	for (uvhub = 0; uvhub < nuvhubs; uvhub++) {
		if (!uv_blade_nr_possible_cpus(uvhub))
1272
			continue;
C
Cliff Wickman 已提交
1273

1274
		pnode = uv_blade_to_pnode(uvhub);
C
Cliff Wickman 已提交
1275
		mmr_image = read_mmr_misc_control(pnode);
C
Cliff Wickman 已提交
1276 1277 1278 1279 1280 1281
		/*
		 * 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.
		 */
C
Cliff Wickman 已提交
1282 1283
		mmr_image &= ~(1L << SOFTACK_MSHIFT);
		write_mmr_misc_control(pnode, mmr_image);
C
Cliff Wickman 已提交
1284 1285 1286
		/*
		 * Set the 4-bit period.
		 */
C
Cliff Wickman 已提交
1287 1288 1289
		mmr_image &= ~((unsigned long)0xf << SOFTACK_PSHIFT);
		mmr_image |= (SOFTACK_TIMEOUT_PERIOD << SOFTACK_PSHIFT);
		write_mmr_misc_control(pnode, mmr_image);
C
Cliff Wickman 已提交
1290
		/*
1291
		 * UV1:
C
Cliff Wickman 已提交
1292 1293 1294 1295
		 * 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).
		 */
C
Cliff Wickman 已提交
1296
		mmr_image |= (1L << SOFTACK_MSHIFT);
1297
		if (is_uv2_hub()) {
1298
			mmr_image &= ~(1L << UV2_LEG_SHFT);
C
Cliff Wickman 已提交
1299
			mmr_image |= (1L << UV2_EXT_SHFT);
1300
		}
C
Cliff Wickman 已提交
1301
		write_mmr_misc_control(pnode, mmr_image);
1302 1303 1304
	}
}

C
Cliff Wickman 已提交
1305
static void *ptc_seq_start(struct seq_file *file, loff_t *offset)
1306 1307 1308 1309 1310 1311
{
	if (*offset < num_possible_cpus())
		return offset;
	return NULL;
}

C
Cliff Wickman 已提交
1312
static void *ptc_seq_next(struct seq_file *file, void *data, loff_t *offset)
1313 1314 1315 1316 1317 1318 1319
{
	(*offset)++;
	if (*offset < num_possible_cpus())
		return offset;
	return NULL;
}

C
Cliff Wickman 已提交
1320
static void ptc_seq_stop(struct seq_file *file, void *data)
1321 1322 1323
{
}

C
Cliff Wickman 已提交
1324
static inline unsigned long long usec_2_cycles(unsigned long microsec)
1325 1326 1327 1328
{
	unsigned long ns;
	unsigned long long cyc;

1329
	ns = microsec * 1000;
1330 1331 1332 1333
	cyc = (ns << CYC2NS_SCALE_FACTOR)/(per_cpu(cyc2ns, smp_processor_id()));
	return cyc;
}

1334
/*
C
Cliff Wickman 已提交
1335
 * Display the statistics thru /proc/sgi_uv/ptc_statistics
1336
 * 'data' points to the cpu number
C
Cliff Wickman 已提交
1337
 * Note: see the descriptions in stat_description[].
1338
 */
C
Cliff Wickman 已提交
1339
static int ptc_seq_show(struct seq_file *file, void *data)
1340 1341 1342 1343 1344 1345 1346
{
	struct ptc_stats *stat;
	int cpu;

	cpu = *(loff_t *)data;
	if (!cpu) {
		seq_printf(file,
1347 1348 1349
			"# cpu sent stime self locals remotes ncpus localhub ");
		seq_printf(file,
			"remotehub numuvhubs numuvhubs16 numuvhubs8 ");
1350
		seq_printf(file,
1351
		    "numuvhubs4 numuvhubs2 numuvhubs1 dto snacks retries rok ");
1352
		seq_printf(file,
C
Cliff Wickman 已提交
1353
			"resetp resett giveup sto bz throt swack recv rtime ");
1354
		seq_printf(file,
C
Cliff Wickman 已提交
1355
			"all one mult none retry canc nocan reset rcan ");
1356
		seq_printf(file,
C
Cliff Wickman 已提交
1357
			"disable enable wars warshw warwaits\n");
1358 1359 1360
	}
	if (cpu < num_possible_cpus() && cpu_online(cpu)) {
		stat = &per_cpu(ptcstats, cpu);
1361 1362 1363 1364
		/* 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),
1365 1366 1367 1368
			   stat->s_ntargself, stat->s_ntarglocals,
			   stat->s_ntargremotes, stat->s_ntargcpu,
			   stat->s_ntarglocaluvhub, stat->s_ntargremoteuvhub,
			   stat->s_ntarguvhub, stat->s_ntarguvhub16);
1369
		seq_printf(file, "%ld %ld %ld %ld %ld %ld ",
1370 1371
			   stat->s_ntarguvhub8, stat->s_ntarguvhub4,
			   stat->s_ntarguvhub2, stat->s_ntarguvhub1,
1372
			   stat->s_dtimeout, stat->s_strongnacks);
1373 1374 1375 1376 1377
		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);
1378

1379 1380
		/* destination side statistics */
		seq_printf(file,
1381
			   "%lx %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld ",
C
Cliff Wickman 已提交
1382
			   read_gmmr_sw_ack(uv_cpu_to_pnode(cpu)),
1383 1384 1385 1386 1387
			   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);
C
Cliff Wickman 已提交
1388 1389 1390 1391
		seq_printf(file, "%ld %ld %ld %ld %ld\n",
			stat->s_bau_disabled, stat->s_bau_reenabled,
			stat->s_uv2_wars, stat->s_uv2_wars_hw,
			stat->s_uv2_war_waits);
1392 1393 1394 1395
	}
	return 0;
}

1396 1397 1398 1399
/*
 * Display the tunables thru debugfs
 */
static ssize_t tunables_read(struct file *file, char __user *userbuf,
C
Cliff Wickman 已提交
1400
				size_t count, loff_t *ppos)
1401
{
1402
	char *buf;
1403 1404
	int ret;

1405
	buf = kasprintf(GFP_KERNEL, "%s %s %s\n%d %d %d %d %d %d %d %d %d\n",
C
Cliff Wickman 已提交
1406
		"max_concur plugged_delay plugsb4reset",
1407 1408
		"timeoutsb4reset ipi_reset_limit complete_threshold",
		"congested_response_us congested_reps congested_period",
C
Cliff Wickman 已提交
1409
		max_concurr, plugged_delay, plugsb4reset,
1410
		timeoutsb4reset, ipi_reset_limit, complete_threshold,
C
Cliff Wickman 已提交
1411
		congested_respns_us, congested_reps, congested_period);
1412

1413 1414 1415 1416 1417 1418
	if (!buf)
		return -ENOMEM;

	ret = simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
	kfree(buf);
	return ret;
1419 1420
}

1421
/*
C
Cliff Wickman 已提交
1422 1423
 * handle a write to /proc/sgi_uv/ptc_statistics
 * -1: reset the statistics
1424 1425
 *  0: display meaning of the statistics
 */
C
Cliff Wickman 已提交
1426 1427
static ssize_t ptc_proc_write(struct file *file, const char __user *user,
				size_t count, loff_t *data)
1428
{
1429
	int cpu;
C
Cliff Wickman 已提交
1430 1431
	int i;
	int elements;
1432
	long input_arg;
1433
	char optstr[64];
1434
	struct ptc_stats *stat;
1435

1436
	if (count == 0 || count > sizeof(optstr))
1437
		return -EINVAL;
1438 1439 1440
	if (copy_from_user(optstr, user, count))
		return -EFAULT;
	optstr[count - 1] = '\0';
C
Cliff Wickman 已提交
1441

1442
	if (strict_strtol(optstr, 10, &input_arg) < 0) {
1443 1444 1445 1446
		printk(KERN_DEBUG "%s is invalid\n", optstr);
		return -EINVAL;
	}

1447
	if (input_arg == 0) {
C
Cliff Wickman 已提交
1448
		elements = sizeof(stat_description)/sizeof(*stat_description);
1449
		printk(KERN_DEBUG "# cpu:      cpu number\n");
1450
		printk(KERN_DEBUG "Sender statistics:\n");
C
Cliff Wickman 已提交
1451 1452
		for (i = 0; i < elements; i++)
			printk(KERN_DEBUG "%s\n", stat_description[i]);
1453 1454 1455 1456 1457
	} else if (input_arg == -1) {
		for_each_present_cpu(cpu) {
			stat = &per_cpu(ptcstats, cpu);
			memset(stat, 0, sizeof(struct ptc_stats));
		}
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
	}

	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;
1474
		}
1475
	}
1476 1477 1478
}

/*
C
Cliff Wickman 已提交
1479 1480
 * Parse the values written to /sys/kernel/debug/sgi_uv/bau_tunables.
 * Zero values reset them to defaults.
1481
 */
C
Cliff Wickman 已提交
1482 1483
static int parse_tunables_write(struct bau_control *bcp, char *instr,
				int count)
1484 1485 1486
{
	char *p;
	char *q;
C
Cliff Wickman 已提交
1487 1488 1489
	int cnt = 0;
	int val;
	int e = sizeof(tunables) / sizeof(*tunables);
1490 1491 1492 1493 1494 1495 1496 1497 1498

	p = instr + strspn(instr, WHITESPACE);
	q = p;
	for (; *p; p = q + strspn(q, WHITESPACE)) {
		q = p + strcspn(p, WHITESPACE);
		cnt++;
		if (q == p)
			break;
	}
C
Cliff Wickman 已提交
1499 1500
	if (cnt != e) {
		printk(KERN_INFO "bau tunable error: should be %d values\n", e);
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
		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) {
C
Cliff Wickman 已提交
1512 1513
				max_concurr = MAX_BAU_CONCURRENT;
				max_concurr_const = MAX_BAU_CONCURRENT;
1514 1515 1516 1517 1518 1519 1520 1521
				continue;
			}
			if (val < 1 || val > bcp->cpus_in_uvhub) {
				printk(KERN_DEBUG
				"Error: BAU max concurrent %d is invalid\n",
				val);
				return -EINVAL;
			}
C
Cliff Wickman 已提交
1522 1523
			max_concurr = val;
			max_concurr_const = val;
1524
			continue;
C
Cliff Wickman 已提交
1525
		default:
1526
			if (val == 0)
C
Cliff Wickman 已提交
1527
				*tunables[cnt].tunp = tunables[cnt].deflt;
1528
			else
C
Cliff Wickman 已提交
1529
				*tunables[cnt].tunp = val;
1530 1531 1532 1533 1534
			continue;
		}
		if (q == p)
			break;
	}
C
Cliff Wickman 已提交
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
	return 0;
}

/*
 * Handle a write to debugfs. (/sys/kernel/debug/sgi_uv/bau_tunables)
 */
static ssize_t tunables_write(struct file *file, const char __user *user,
				size_t count, loff_t *data)
{
	int cpu;
	int ret;
	char instr[100];
	struct bau_control *bcp;

	if (count == 0 || count > sizeof(instr)-1)
		return -EINVAL;
	if (copy_from_user(instr, user, count))
		return -EFAULT;

	instr[count] = '\0';

1556 1557
	cpu = get_cpu();
	bcp = &per_cpu(bau_control, cpu);
C
Cliff Wickman 已提交
1558
	ret = parse_tunables_write(bcp, instr, count);
1559
	put_cpu();
C
Cliff Wickman 已提交
1560 1561 1562
	if (ret)
		return ret;

1563 1564
	for_each_present_cpu(cpu) {
		bcp = &per_cpu(bau_control, cpu);
C
Cliff Wickman 已提交
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
		bcp->max_concurr =		max_concurr;
		bcp->max_concurr_const =	max_concurr;
		bcp->plugged_delay =		plugged_delay;
		bcp->plugsb4reset =		plugsb4reset;
		bcp->timeoutsb4reset =		timeoutsb4reset;
		bcp->ipi_reset_limit =		ipi_reset_limit;
		bcp->complete_threshold =	complete_threshold;
		bcp->cong_response_us =		congested_respns_us;
		bcp->cong_reps =		congested_reps;
		bcp->cong_period =		congested_period;
1575
	}
1576 1577 1578 1579
	return count;
}

static const struct seq_operations uv_ptc_seq_ops = {
C
Cliff Wickman 已提交
1580 1581 1582 1583
	.start		= ptc_seq_start,
	.next		= ptc_seq_next,
	.stop		= ptc_seq_stop,
	.show		= ptc_seq_show
1584 1585
};

C
Cliff Wickman 已提交
1586
static int ptc_proc_open(struct inode *inode, struct file *file)
1587 1588 1589 1590
{
	return seq_open(file, &uv_ptc_seq_ops);
}

1591 1592 1593 1594 1595
static int tunables_open(struct inode *inode, struct file *file)
{
	return 0;
}

1596
static const struct file_operations proc_uv_ptc_operations = {
C
Cliff Wickman 已提交
1597
	.open		= ptc_proc_open,
1598
	.read		= seq_read,
C
Cliff Wickman 已提交
1599
	.write		= ptc_proc_write,
1600 1601
	.llseek		= seq_lseek,
	.release	= seq_release,
1602 1603
};

1604 1605 1606 1607
static const struct file_operations tunables_fops = {
	.open		= tunables_open,
	.read		= tunables_read,
	.write		= tunables_write,
1608
	.llseek		= default_llseek,
1609 1610
};

1611
static int __init uv_ptc_init(void)
1612
{
1613
	struct proc_dir_entry *proc_uv_ptc;
1614 1615 1616 1617

	if (!is_uv_system())
		return 0;

1618 1619
	proc_uv_ptc = proc_create(UV_PTC_BASENAME, 0444, NULL,
				  &proc_uv_ptc_operations);
1620 1621 1622 1623 1624
	if (!proc_uv_ptc) {
		printk(KERN_ERR "unable to create %s proc entry\n",
		       UV_PTC_BASENAME);
		return -EINVAL;
	}
1625 1626 1627 1628 1629 1630 1631 1632

	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,
C
Cliff Wickman 已提交
1633
					tunables_dir, NULL, &tunables_fops);
1634 1635 1636 1637 1638
	if (!tunables_file) {
		printk(KERN_ERR "unable to create debugfs file %s\n",
		       UV_BAU_TUNABLES_FILE);
		return -EINVAL;
	}
1639 1640 1641 1642
	return 0;
}

/*
1643
 * Initialize the sending side's sending buffers.
1644
 */
C
Cliff Wickman 已提交
1645
static void activation_descriptor_init(int node, int pnode, int base_pnode)
1646 1647
{
	int i;
1648
	int cpu;
1649
	int uv1 = 0;
1650
	unsigned long gpa;
1651
	unsigned long m;
1652
	unsigned long n;
C
Cliff Wickman 已提交
1653
	size_t dsize;
1654 1655
	struct bau_desc *bau_desc;
	struct bau_desc *bd2;
1656 1657
	struct uv1_bau_msg_header *uv1_hdr;
	struct uv2_bau_msg_header *uv2_hdr;
1658
	struct bau_control *bcp;
1659

1660
	/*
C
Cliff Wickman 已提交
1661 1662
	 * each bau_desc is 64 bytes; there are 8 (ITEMS_PER_DESC)
	 * per cpu; and one per cpu on the uvhub (ADP_SZ)
1663
	 */
C
Cliff Wickman 已提交
1664 1665
	dsize = sizeof(struct bau_desc) * ADP_SZ * ITEMS_PER_DESC;
	bau_desc = kmalloc_node(dsize, GFP_KERNEL, node);
1666
	BUG_ON(!bau_desc);
1667

1668 1669 1670
	gpa = uv_gpa(bau_desc);
	n = uv_gpa_to_gnode(gpa);
	m = uv_gpa_to_offset(gpa);
1671 1672
	if (is_uv1_hub())
		uv1 = 1;
1673

1674
	/* the 14-bit pnode */
C
Cliff Wickman 已提交
1675
	write_mmr_descriptor_base(pnode, (n << UV_DESC_PSHIFT | m));
1676
	/*
C
Cliff Wickman 已提交
1677
	 * Initializing all 8 (ITEMS_PER_DESC) descriptors for each
1678
	 * cpu even though we only use the first one; one descriptor can
1679
	 * describe a broadcast to 256 uv hubs.
1680
	 */
C
Cliff Wickman 已提交
1681
	for (i = 0, bd2 = bau_desc; i < (ADP_SZ * ITEMS_PER_DESC); i++, bd2++) {
1682
		memset(bd2, 0, sizeof(struct bau_desc));
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
		if (uv1) {
			uv1_hdr = &bd2->header.uv1_hdr;
			uv1_hdr->swack_flag =	1;
			/*
			 * 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.
			 */
			uv1_hdr->base_dest_nasid =
						UV_PNODE_TO_NASID(base_pnode);
			uv1_hdr->dest_subnodeid =	UV_LB_SUBNODEID;
			uv1_hdr->command =		UV_NET_ENDPOINT_INTD;
			uv1_hdr->int_both =		1;
			/*
			 * all others need to be set to zero:
			 *   fairness chaining multilevel count replied_to
			 */
		} else {
			uv2_hdr = &bd2->header.uv2_hdr;
			uv2_hdr->swack_flag =	1;
			uv2_hdr->base_dest_nasid =
						UV_PNODE_TO_NASID(base_pnode);
			uv2_hdr->dest_subnodeid =	UV_LB_SUBNODEID;
			uv2_hdr->command =		UV_NET_ENDPOINT_INTD;
		}
1710
	}
1711 1712 1713 1714 1715 1716
	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;
	}
1717 1718 1719 1720
}

/*
 * initialize the destination side's receiving buffers
1721 1722 1723
 * entered for each uvhub in the partition
 * - node is first node (kernel memory notion) on the uvhub
 * - pnode is the uvhub's physical identifier
1724
 */
C
Cliff Wickman 已提交
1725
static void pq_init(int node, int pnode)
1726
{
1727
	int cpu;
C
Cliff Wickman 已提交
1728
	size_t plsize;
1729
	char *cp;
C
Cliff Wickman 已提交
1730 1731 1732 1733 1734 1735
	void *vp;
	unsigned long pn;
	unsigned long first;
	unsigned long pn_first;
	unsigned long last;
	struct bau_pq_entry *pqp;
1736
	struct bau_control *bcp;
1737

C
Cliff Wickman 已提交
1738 1739 1740
	plsize = (DEST_Q_SIZE + 1) * sizeof(struct bau_pq_entry);
	vp = kmalloc_node(plsize, GFP_KERNEL, node);
	pqp = (struct bau_pq_entry *)vp;
1741
	BUG_ON(!pqp);
1742

1743
	cp = (char *)pqp + 31;
C
Cliff Wickman 已提交
1744
	pqp = (struct bau_pq_entry *)(((unsigned long)cp >> 5) << 5);
1745 1746 1747 1748 1749 1750

	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);
C
Cliff Wickman 已提交
1751 1752 1753
		bcp->queue_first	= pqp;
		bcp->bau_msg_head	= pqp;
		bcp->queue_last		= pqp + (DEST_Q_SIZE - 1);
1754
	}
1755
	/*
1756
	 * need the gnode of where the memory was really allocated
1757
	 */
1758
	pn = uv_gpa_to_gnode(uv_gpa(pqp));
C
Cliff Wickman 已提交
1759 1760 1761 1762 1763 1764
	first = uv_physnodeaddr(pqp);
	pn_first = ((unsigned long)pn << UV_PAYLOADQ_PNODE_SHIFT) | first;
	last = uv_physnodeaddr(pqp + (DEST_Q_SIZE - 1));
	write_mmr_payload_first(pnode, pn_first);
	write_mmr_payload_tail(pnode, first);
	write_mmr_payload_last(pnode, last);
C
Cliff Wickman 已提交
1765
	write_gmmr_sw_ack(pnode, 0xffffUL);
C
Cliff Wickman 已提交
1766

1767
	/* in effect, all msg_type's are set to MSG_NOOP */
C
Cliff Wickman 已提交
1768
	memset(pqp, 0, sizeof(struct bau_pq_entry) * DEST_Q_SIZE);
1769
}
1770

1771
/*
1772
 * Initialization of each UV hub's structures
1773
 */
C
Cliff Wickman 已提交
1774
static void __init init_uvhub(int uvhub, int vector, int base_pnode)
1775
{
1776
	int node;
1777 1778
	int pnode;
	unsigned long apicid;
1779 1780 1781

	node = uvhub_to_first_node(uvhub);
	pnode = uv_blade_to_pnode(uvhub);
C
Cliff Wickman 已提交
1782 1783 1784 1785

	activation_descriptor_init(node, pnode, base_pnode);

	pq_init(node, pnode);
1786
	/*
1787 1788
	 * The below initialization can't be in firmware because the
	 * messaging IRQ will be determined by the OS.
1789
	 */
1790
	apicid = uvhub_to_first_apicid(uvhub) | uv_apicid_hibits;
C
Cliff Wickman 已提交
1791
	write_mmr_data_config(pnode, ((apicid << 32) | vector));
1792 1793
}

1794 1795 1796
/*
 * We will set BAU_MISC_CONTROL with a timeout period.
 * But the BIOS has set UVH_AGING_PRESCALE_SEL and UVH_TRANSACTION_TIMEOUT.
C
Cliff Wickman 已提交
1797
 * So the destination timeout period has to be calculated from them.
1798
 */
C
Cliff Wickman 已提交
1799
static int calculate_destination_timeout(void)
1800 1801 1802 1803 1804 1805 1806 1807 1808
{
	unsigned long mmr_image;
	int mult1;
	int mult2;
	int index;
	int base;
	int ret;
	unsigned long ts_ns;

1809
	if (is_uv1_hub()) {
C
Cliff Wickman 已提交
1810
		mult1 = SOFTACK_TIMEOUT_PERIOD & BAU_MISC_CONTROL_MULT_MASK;
1811 1812 1813 1814 1815 1816 1817 1818
		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;
	} else {
1819 1820
		/* 4 bits  0/1 for 10/80us base, 3 bits of multiplier */
		mmr_image = uv_read_local_mmr(UVH_LB_BAU_MISC_CONTROL);
1821
		mmr_image = (mmr_image & UV_SA_MASK) >> UV_SA_SHFT;
C
Cliff Wickman 已提交
1822
		if (mmr_image & (1L << UV2_ACK_UNITS_SHFT))
1823
			base = 80;
1824
		else
1825 1826
			base = 10;
		mult1 = mmr_image & UV2_ACK_MASK;
1827 1828
		ret = mult1 * base;
	}
1829 1830 1831
	return ret;
}

C
Cliff Wickman 已提交
1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852
static void __init init_per_cpu_tunables(void)
{
	int cpu;
	struct bau_control *bcp;

	for_each_present_cpu(cpu) {
		bcp = &per_cpu(bau_control, cpu);
		bcp->baudisabled		= 0;
		bcp->statp			= &per_cpu(ptcstats, cpu);
		/* time interval to catch a hardware stay-busy bug */
		bcp->timeout_interval		= usec_2_cycles(2*timeout_us);
		bcp->max_concurr		= max_concurr;
		bcp->max_concurr_const		= max_concurr;
		bcp->plugged_delay		= plugged_delay;
		bcp->plugsb4reset		= plugsb4reset;
		bcp->timeoutsb4reset		= timeoutsb4reset;
		bcp->ipi_reset_limit		= ipi_reset_limit;
		bcp->complete_threshold		= complete_threshold;
		bcp->cong_response_us		= congested_respns_us;
		bcp->cong_reps			= congested_reps;
		bcp->cong_period		= congested_period;
C
Cliff Wickman 已提交
1853
		bcp->clocks_per_100_usec =	usec_2_cycles(100);
1854 1855
		spin_lock_init(&bcp->queue_lock);
		spin_lock_init(&bcp->uvhub_lock);
C
Cliff Wickman 已提交
1856 1857 1858
	}
}

1859
/*
C
Cliff Wickman 已提交
1860
 * Scan all cpus to collect blade and socket summaries.
1861
 */
C
Cliff Wickman 已提交
1862 1863 1864
static int __init get_cpu_topology(int base_pnode,
					struct uvhub_desc *uvhub_descs,
					unsigned char *uvhub_mask)
1865 1866 1867 1868
{
	int cpu;
	int pnode;
	int uvhub;
C
Cliff Wickman 已提交
1869
	int socket;
1870 1871 1872 1873 1874 1875
	struct bau_control *bcp;
	struct uvhub_desc *bdp;
	struct socket_desc *sdp;

	for_each_present_cpu(cpu) {
		bcp = &per_cpu(bau_control, cpu);
C
Cliff Wickman 已提交
1876

1877
		memset(bcp, 0, sizeof(struct bau_control));
C
Cliff Wickman 已提交
1878

1879
		pnode = uv_cpu_hub_info(cpu)->pnode;
C
Cliff Wickman 已提交
1880
		if ((pnode - base_pnode) >= UV_DISTRIBUTION_SIZE) {
1881 1882
			printk(KERN_EMERG
				"cpu %d pnode %d-%d beyond %d; BAU disabled\n",
C
Cliff Wickman 已提交
1883
				cpu, pnode, base_pnode, UV_DISTRIBUTION_SIZE);
1884 1885
			return 1;
		}
C
Cliff Wickman 已提交
1886

1887
		bcp->osnode = cpu_to_node(cpu);
C
Cliff Wickman 已提交
1888 1889
		bcp->partition_base_pnode = base_pnode;

1890
		uvhub = uv_cpu_hub_info(cpu)->numa_blade_id;
C
Cliff Wickman 已提交
1891
		*(uvhub_mask + (uvhub/8)) |= (1 << (uvhub%8));
1892
		bdp = &uvhub_descs[uvhub];
C
Cliff Wickman 已提交
1893

1894 1895 1896
		bdp->num_cpus++;
		bdp->uvhub = uvhub;
		bdp->pnode = pnode;
C
Cliff Wickman 已提交
1897

1898 1899
		/* kludge: 'assuming' one node per socket, and assuming that
		   disabling a socket just leaves a gap in node numbers */
1900
		socket = bcp->osnode & 1;
1901
		bdp->socket_mask |= (1 << socket);
1902 1903 1904
		sdp = &bdp->socket[socket];
		sdp->cpu_number[sdp->num_cpus] = cpu;
		sdp->num_cpus++;
1905
		if (sdp->num_cpus > MAX_CPUS_PER_SOCKET) {
C
Cliff Wickman 已提交
1906 1907
			printk(KERN_EMERG "%d cpus per socket invalid\n",
				sdp->num_cpus);
1908 1909
			return 1;
		}
1910
	}
C
Cliff Wickman 已提交
1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929
	return 0;
}

/*
 * Each socket is to get a local array of pnodes/hubs.
 */
static void make_per_cpu_thp(struct bau_control *smaster)
{
	int cpu;
	size_t hpsz = sizeof(struct hub_and_pnode) * num_possible_cpus();

	smaster->thp = kmalloc_node(hpsz, GFP_KERNEL, smaster->osnode);
	memset(smaster->thp, 0, hpsz);
	for_each_present_cpu(cpu) {
		smaster->thp[cpu].pnode = uv_cpu_hub_info(cpu)->pnode;
		smaster->thp[cpu].uvhub = uv_cpu_hub_info(cpu)->numa_blade_id;
	}
}

1930 1931 1932 1933 1934 1935 1936 1937 1938 1939
/*
 * Each uvhub is to get a local cpumask.
 */
static void make_per_hub_cpumask(struct bau_control *hmaster)
{
	int sz = sizeof(cpumask_t);

	hmaster->cpumask = kzalloc_node(sz, GFP_KERNEL, hmaster->osnode);
}

C
Cliff Wickman 已提交
1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965
/*
 * Initialize all the per_cpu information for the cpu's on a given socket,
 * given what has been gathered into the socket_desc struct.
 * And reports the chosen hub and socket masters back to the caller.
 */
static int scan_sock(struct socket_desc *sdp, struct uvhub_desc *bdp,
			struct bau_control **smasterp,
			struct bau_control **hmasterp)
{
	int i;
	int cpu;
	struct bau_control *bcp;

	for (i = 0; i < sdp->num_cpus; i++) {
		cpu = sdp->cpu_number[i];
		bcp = &per_cpu(bau_control, cpu);
		bcp->cpu = cpu;
		if (i == 0) {
			*smasterp = bcp;
			if (!(*hmasterp))
				*hmasterp = bcp;
		}
		bcp->cpus_in_uvhub = bdp->num_cpus;
		bcp->cpus_in_socket = sdp->num_cpus;
		bcp->socket_master = *smasterp;
		bcp->uvhub = bdp->uvhub;
1966 1967 1968 1969 1970 1971 1972 1973
		if (is_uv1_hub())
			bcp->uvhub_version = 1;
		else if (is_uv2_hub())
			bcp->uvhub_version = 2;
		else {
			printk(KERN_EMERG "uvhub version not 1 or 2\n");
			return 1;
		}
C
Cliff Wickman 已提交
1974 1975
		bcp->uvhub_master = *hmasterp;
		bcp->uvhub_cpu = uv_cpu_hub_info(cpu)->blade_processor_id;
C
Cliff Wickman 已提交
1976
		bcp->using_desc = bcp->uvhub_cpu;
C
Cliff Wickman 已提交
1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996
		if (bcp->uvhub_cpu >= MAX_CPUS_PER_UVHUB) {
			printk(KERN_EMERG "%d cpus per uvhub invalid\n",
				bcp->uvhub_cpu);
			return 1;
		}
	}
	return 0;
}

/*
 * Summarize the blade and socket topology into the per_cpu structures.
 */
static int __init summarize_uvhub_sockets(int nuvhubs,
			struct uvhub_desc *uvhub_descs,
			unsigned char *uvhub_mask)
{
	int socket;
	int uvhub;
	unsigned short socket_mask;

C
Cliff Wickman 已提交
1997
	for (uvhub = 0; uvhub < nuvhubs; uvhub++) {
C
Cliff Wickman 已提交
1998 1999 2000 2001
		struct uvhub_desc *bdp;
		struct bau_control *smaster = NULL;
		struct bau_control *hmaster = NULL;

C
Cliff Wickman 已提交
2002 2003
		if (!(*(uvhub_mask + (uvhub/8)) & (1 << (uvhub%8))))
			continue;
C
Cliff Wickman 已提交
2004

2005
		bdp = &uvhub_descs[uvhub];
2006 2007 2008
		socket_mask = bdp->socket_mask;
		socket = 0;
		while (socket_mask) {
C
Cliff Wickman 已提交
2009 2010 2011 2012
			struct socket_desc *sdp;
			if ((socket_mask & 1)) {
				sdp = &bdp->socket[socket];
				if (scan_sock(sdp, bdp, &smaster, &hmaster))
2013
					return 1;
2014
				make_per_cpu_thp(smaster);
2015 2016
			}
			socket++;
2017
			socket_mask = (socket_mask >> 1);
2018
		}
2019
		make_per_hub_cpumask(hmaster);
2020
	}
C
Cliff Wickman 已提交
2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040
	return 0;
}

/*
 * initialize the bau_control structure for each cpu
 */
static int __init init_per_cpu(int nuvhubs, int base_part_pnode)
{
	unsigned char *uvhub_mask;
	void *vp;
	struct uvhub_desc *uvhub_descs;

	timeout_us = calculate_destination_timeout();

	vp = kmalloc(nuvhubs * sizeof(struct uvhub_desc), GFP_KERNEL);
	uvhub_descs = (struct uvhub_desc *)vp;
	memset(uvhub_descs, 0, nuvhubs * sizeof(struct uvhub_desc));
	uvhub_mask = kzalloc((nuvhubs+7)/8, GFP_KERNEL);

	if (get_cpu_topology(base_part_pnode, uvhub_descs, uvhub_mask))
2041
		goto fail;
C
Cliff Wickman 已提交
2042 2043

	if (summarize_uvhub_sockets(nuvhubs, uvhub_descs, uvhub_mask))
2044
		goto fail;
C
Cliff Wickman 已提交
2045

2046
	kfree(uvhub_descs);
C
Cliff Wickman 已提交
2047
	kfree(uvhub_mask);
C
Cliff Wickman 已提交
2048
	init_per_cpu_tunables();
2049
	return 0;
2050 2051 2052 2053 2054

fail:
	kfree(uvhub_descs);
	kfree(uvhub_mask);
	return 1;
2055 2056 2057 2058 2059 2060 2061
}

/*
 * Initialization of BAU-related structures
 */
static int __init uv_bau_init(void)
{
2062 2063 2064
	int uvhub;
	int pnode;
	int nuvhubs;
2065
	int cur_cpu;
C
Cliff Wickman 已提交
2066
	int cpus;
2067
	int vector;
C
Cliff Wickman 已提交
2068
	cpumask_var_t *mask;
2069 2070 2071

	if (!is_uv_system())
		return 0;
2072

2073 2074 2075
	if (nobau)
		return 0;

C
Cliff Wickman 已提交
2076 2077 2078 2079
	for_each_possible_cpu(cur_cpu) {
		mask = &per_cpu(uv_flush_tlb_mask, cur_cpu);
		zalloc_cpumask_var_node(mask, GFP_KERNEL, cpu_to_node(cur_cpu));
	}
2080

2081
	nuvhubs = uv_num_possible_blades();
2082
	spin_lock_init(&disable_lock);
C
Cliff Wickman 已提交
2083
	congested_cycles = usec_2_cycles(congested_respns_us);
2084

C
Cliff Wickman 已提交
2085
	uv_base_pnode = 0x7fffffff;
2086
	for (uvhub = 0; uvhub < nuvhubs; uvhub++) {
C
Cliff Wickman 已提交
2087 2088 2089
		cpus = uv_blade_nr_possible_cpus(uvhub);
		if (cpus && (uv_blade_to_pnode(uvhub) < uv_base_pnode))
			uv_base_pnode = uv_blade_to_pnode(uvhub);
2090 2091
	}

2092 2093
	enable_timeouts();

C
Cliff Wickman 已提交
2094
	if (init_per_cpu(nuvhubs, uv_base_pnode)) {
2095 2096 2097
		nobau = 1;
		return 0;
	}
2098 2099 2100 2101

	vector = UV_BAU_MESSAGE;
	for_each_possible_blade(uvhub)
		if (uv_blade_nr_possible_cpus(uvhub))
C
Cliff Wickman 已提交
2102
			init_uvhub(uvhub, vector, uv_base_pnode);
2103 2104 2105 2106

	alloc_intr_gate(vector, uv_bau_message_intr1);

	for_each_possible_blade(uvhub) {
2107
		if (uv_blade_nr_possible_cpus(uvhub)) {
C
Cliff Wickman 已提交
2108 2109
			unsigned long val;
			unsigned long mmr;
2110 2111
			pnode = uv_blade_to_pnode(uvhub);
			/* INIT the bau */
C
Cliff Wickman 已提交
2112 2113
			val = 1L << 63;
			write_gmmr_activation(pnode, val);
2114
			mmr = 1; /* should be 1 to broadcast to both sockets */
2115 2116
			if (!is_uv1_hub())
				write_mmr_data_broadcast(pnode, mmr);
2117
		}
2118
	}
2119

2120 2121
	return 0;
}
2122
core_initcall(uv_bau_init);
2123
fs_initcall(uv_ptc_init);