tlb_uv.c 48.4 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
/* position of pnode (which is nasid>>1): */
static int uv_nshift __read_mostly;
static unsigned long uv_mmask __read_mostly;
121

122 123
static DEFINE_PER_CPU(struct ptc_stats, ptcstats);
static DEFINE_PER_CPU(struct bau_control, bau_control);
124 125
static DEFINE_PER_CPU(cpumask_var_t, uv_flush_tlb_mask);

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

	for_each_online_node(node) {
		b = uv_node_to_blade_id(node);
136
		if (uvhub == b)
137 138
			return node;
	}
139
	return -1;
140 141 142
}

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

	for_each_present_cpu(cpu)
150
		if (uvhub == uv_cpu_to_blade_id(cpu))
151 152 153 154
			return per_cpu(x86_cpu_to_apicid, cpu);
	return -1;
}

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

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

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

191 192 193 194 195
	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 已提交
196 197
		if (msg2 > mdp->queue_last)
			msg2 = mdp->queue_first;
198 199 200
		if (msg2 == msg)
			break;

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

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

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

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

		if (msg_ack_count == bcp->cpus_in_uvhub) {
			/*
			 * All cpus in uvhub saw it; reply
			 */
C
Cliff Wickman 已提交
291
			reply_to_message(mdp, bcp);
292 293
		}
	}
294

295
	return;
296 297 298
}

/*
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
 * 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
C
Cliff Wickman 已提交
314
 * can be identified by their nonzero swack_vec field.
315
 *
316 317
 * 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 已提交
318
 * swack resources.
319
 */
C
Cliff Wickman 已提交
320
static void do_reset(void *ptr)
321
{
322
	int i;
C
Cliff Wickman 已提交
323 324 325 326
	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;
327

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

/*
366 367
 * Use IPI to get all target uvhubs to release resources held by
 * a given sending cpu number.
368
 */
C
Cliff Wickman 已提交
369
static void reset_with_ipi(struct bau_targ_hubmask *distribution, int sender)
370
{
371
	int uvhub;
C
Cliff Wickman 已提交
372
	int maskbits;
373 374
	cpumask_t mask;
	struct reset_args reset_args;
375

376 377 378
	reset_args.sender = sender;
	cpus_clear(mask);
	/* find a single cpu for each uvhub in this distribution mask */
C
Cliff Wickman 已提交
379 380 381
	maskbits = sizeof(struct bau_targ_hubmask) * BITSPERBYTE;
	for (uvhub = 0; uvhub < maskbits; uvhub++) {
		int cpu;
382
		if (!bau_uvhub_isset(uvhub, distribution))
383
			continue;
384 385 386
		/* find a cpu for this uvhub */
		cpu = uvhub_to_first_cpu(uvhub);
		cpu_set(cpu, mask);
387
	}
C
Cliff Wickman 已提交
388 389 390

	/* IPI all cpus; preemption is already disabled */
	smp_call_function_many(&mask, do_reset, (void *)&reset_args, 1);
391 392 393
	return;
}

C
Cliff Wickman 已提交
394
static inline unsigned long cycles_2_us(unsigned long long cyc)
395 396 397
{
	unsigned long long ns;
	unsigned long us;
C
Cliff Wickman 已提交
398 399 400
	int cpu = smp_processor_id();

	ns =  (cyc * per_cpu(cyc2ns, cpu)) >> CYC2NS_SCALE_FACTOR;
401 402
	us = ns / 1000;
	return us;
403 404
}

405
/*
406 407 408 409
 * 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 已提交
410
static inline void quiesce_local_uvhub(struct bau_control *hmaster)
411
{
C
Cliff Wickman 已提交
412
	atom_asr(1, (struct atomic_short *)&hmaster->uvhub_quiesce);
413 414 415 416 417
}

/*
 * mark this quiet-requestor as done
 */
C
Cliff Wickman 已提交
418
static inline void end_uvhub_quiesce(struct bau_control *hmaster)
419
{
C
Cliff Wickman 已提交
420 421 422 423 424 425 426 427 428 429 430
	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;
431 432 433 434 435
}

/*
 * Wait for completion of a broadcast software ack message
 * return COMPLETE, RETRY(PLUGGED or TIMEOUT) or GIVEUP
436
 */
437
static int uv1_wait_completion(struct bau_desc *bau_desc,
C
Cliff Wickman 已提交
438 439
				unsigned long mmr_offset, int right_shift,
				struct bau_control *bcp, long try)
440 441
{
	unsigned long descriptor_status;
C
Cliff Wickman 已提交
442
	cycles_t ttm;
443
	struct ptc_stats *stat = bcp->statp;
444

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

			/*
			 * 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 已提交
467
			if (cycles_2_us(ttm - bcp->send_message) < timeout_us) {
468 469 470 471 472 473 474 475 476 477 478 479
				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 已提交
480
		descriptor_status = uv1_read_status(mmr_offset, right_shift);
481 482 483 484 485
	}
	bcp->conseccompletes++;
	return FLUSH_COMPLETE;
}

C
Cliff Wickman 已提交
486 487 488 489
/*
 * UV2 has an extra bit of status in the ACTIVATION_STATUS_2 register.
 */
static unsigned long uv2_read_status(unsigned long offset, int rshft, int cpu)
490 491 492
{
	unsigned long descriptor_status;
	unsigned long descriptor_status2;
C
Cliff Wickman 已提交
493 494 495 496 497 498 499 500 501 502 503 504 505 506

	descriptor_status = ((read_lmmr(offset) >> rshft) & UV_ACT_STATUS_MASK);
	descriptor_status2 = (read_mmr_uv2_status() >> cpu) & 0x1UL;
	descriptor_status = (descriptor_status << 1) | descriptor_status2;
	return descriptor_status;
}

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;
	int cpu = bcp->uvhub_cpu;
507 508
	struct ptc_stats *stat = bcp->statp;

C
Cliff Wickman 已提交
509 510
	descriptor_stat = uv2_read_status(mmr_offset, right_shift, cpu);

511
	/* spin on the status MMR, waiting for it to go idle */
C
Cliff Wickman 已提交
512
	while (descriptor_stat != UV2H_DESC_IDLE) {
513 514 515 516 517 518
		/*
		 * 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 已提交
519 520 521
		if ((descriptor_stat == UV2H_DESC_SOURCE_TIMEOUT) ||
		    (descriptor_stat == UV2H_DESC_DEST_STRONG_NACK) ||
		    (descriptor_stat == UV2H_DESC_DEST_PUT_ERR)) {
522 523
			stat->s_stimeout++;
			return FLUSH_GIVEUP;
C
Cliff Wickman 已提交
524
		} else if (descriptor_stat == UV2H_DESC_DEST_TIMEOUT) {
525
			stat->s_dtimeout++;
C
Cliff Wickman 已提交
526
			ttm = get_cycles();
527 528 529 530 531 532
			/*
			 * 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 已提交
533
			if (cycles_2_us(ttm - bcp->send_message) < timeout_us) {
534 535 536 537 538 539 540
				bcp->conseccompletes = 0;
				return FLUSH_RETRY_PLUGGED;
			}
			bcp->conseccompletes = 0;
			return FLUSH_RETRY_TIMEOUT;
		} else {
			/*
C
Cliff Wickman 已提交
541
			 * descriptor_stat is still BUSY
542 543
			 */
			cpu_relax();
544
		}
C
Cliff Wickman 已提交
545
		descriptor_stat = uv2_read_status(mmr_offset, right_shift, cpu);
546
	}
547
	bcp->conseccompletes++;
548 549 550
	return FLUSH_COMPLETE;
}

C
Cliff Wickman 已提交
551 552 553 554 555 556 557
/*
 * 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)
558
{
C
Cliff Wickman 已提交
559 560 561 562 563 564 565 566 567 568 569 570
	int right_shift;
	unsigned long mmr_offset;
	int cpu = bcp->uvhub_cpu;

	if (cpu < UV_CPUS_PER_AS) {
		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_AS) * UV_ACT_STATUS_SIZE);
	}

571 572
	if (is_uv1_hub())
		return uv1_wait_completion(bau_desc, mmr_offset, right_shift,
C
Cliff Wickman 已提交
573
								bcp, try);
574 575
	else
		return uv2_wait_completion(bau_desc, mmr_offset, right_shift,
C
Cliff Wickman 已提交
576
								bcp, try);
577 578
}

C
Cliff Wickman 已提交
579
static inline cycles_t sec_2_cycles(unsigned long sec)
580 581 582 583 584 585 586 587 588 589
{
	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 已提交
590
 * Our retries are blocked by all destination sw ack resources being
591 592 593
 * in use, and a timeout is pending. In that case hardware immediately
 * returns the ERROR that looks like a destination timeout.
 */
C
Cliff Wickman 已提交
594 595
static void destination_plugged(struct bau_desc *bau_desc,
			struct bau_control *bcp,
596 597 598 599
			struct bau_control *hmaster, struct ptc_stats *stat)
{
	udelay(bcp->plugged_delay);
	bcp->plugged_tries++;
C
Cliff Wickman 已提交
600

601 602
	if (bcp->plugged_tries >= bcp->plugsb4reset) {
		bcp->plugged_tries = 0;
C
Cliff Wickman 已提交
603

604
		quiesce_local_uvhub(hmaster);
C
Cliff Wickman 已提交
605

606
		spin_lock(&hmaster->queue_lock);
C
Cliff Wickman 已提交
607
		reset_with_ipi(&bau_desc->distribution, bcp->cpu);
608
		spin_unlock(&hmaster->queue_lock);
C
Cliff Wickman 已提交
609

610
		end_uvhub_quiesce(hmaster);
C
Cliff Wickman 已提交
611

612 613 614 615 616
		bcp->ipi_attempts++;
		stat->s_resets_plug++;
	}
}

C
Cliff Wickman 已提交
617 618 619
static void destination_timeout(struct bau_desc *bau_desc,
			struct bau_control *bcp, struct bau_control *hmaster,
			struct ptc_stats *stat)
620
{
C
Cliff Wickman 已提交
621
	hmaster->max_concurr = 1;
622 623 624
	bcp->timeout_tries++;
	if (bcp->timeout_tries >= bcp->timeoutsb4reset) {
		bcp->timeout_tries = 0;
C
Cliff Wickman 已提交
625

626
		quiesce_local_uvhub(hmaster);
C
Cliff Wickman 已提交
627

628
		spin_lock(&hmaster->queue_lock);
C
Cliff Wickman 已提交
629
		reset_with_ipi(&bau_desc->distribution, bcp->cpu);
630
		spin_unlock(&hmaster->queue_lock);
C
Cliff Wickman 已提交
631

632
		end_uvhub_quiesce(hmaster);
C
Cliff Wickman 已提交
633

634 635 636 637 638
		bcp->ipi_attempts++;
		stat->s_resets_timeout++;
	}
}

639 640 641 642
/*
 * Completions are taking a very long time due to a congested numalink
 * network.
 */
C
Cliff Wickman 已提交
643 644
static void disable_for_congestion(struct bau_control *bcp,
					struct ptc_stats *stat)
645 646 647
{
	/* let only one cpu do this disabling */
	spin_lock(&disable_lock);
C
Cliff Wickman 已提交
648

649 650
	if (!baudisabled && bcp->period_requests &&
	    ((bcp->period_time / bcp->period_requests) > congested_cycles)) {
C
Cliff Wickman 已提交
651 652
		int tcpu;
		struct bau_control *tbcp;
653 654 655 656
		/* 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 已提交
657 658
		bcp->set_bau_on_time = get_cycles();
		bcp->set_bau_on_time += sec_2_cycles(bcp->cong_period);
659 660 661
		stat->s_bau_disabled++;
		for_each_present_cpu(tcpu) {
			tbcp = &per_cpu(bau_control, tcpu);
C
Cliff Wickman 已提交
662
			tbcp->baudisabled = 1;
663 664
		}
	}
C
Cliff Wickman 已提交
665

666 667 668
	spin_unlock(&disable_lock);
}

C
Cliff Wickman 已提交
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
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);
}

/*
741
 * Send a broadcast and wait for it to complete.
742
 *
743
 * The flush_mask contains the cpus the broadcast is to be sent to including
744
 * cpus that are on the local uvhub.
745
 *
746 747 748
 * 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.
749
 */
750
int uv_flush_send_and_wait(struct bau_desc *bau_desc,
C
Cliff Wickman 已提交
751
			struct cpumask *flush_mask, struct bau_control *bcp)
752
{
753
	int seq_number = 0;
C
Cliff Wickman 已提交
754
	int completion_stat = 0;
755
	long try = 0;
756
	unsigned long index;
757 758
	cycles_t time1;
	cycles_t time2;
759
	struct ptc_stats *stat = bcp->statp;
760 761
	struct bau_control *hmaster = bcp->uvhub_master;

C
Cliff Wickman 已提交
762 763 764
	if (is_uv1_hub())
		uv1_throttle(hmaster, stat);

765 766
	while (hmaster->uvhub_quiesce)
		cpu_relax();
767 768 769

	time1 = get_cycles();
	do {
770
		if (try == 0) {
771
			bau_desc->header.msg_type = MSG_REGULAR;
772 773 774 775 776
			seq_number = bcp->message_number++;
		} else {
			bau_desc->header.msg_type = MSG_RETRY;
			stat->s_retry_messages++;
		}
C
Cliff Wickman 已提交
777

778
		bau_desc->header.sequence = seq_number;
C
Cliff Wickman 已提交
779
		index = (1UL << AS_PUSH_SHIFT) | bcp->uvhub_cpu;
780
		bcp->send_message = get_cycles();
C
Cliff Wickman 已提交
781 782 783

		write_mmr_activation(index);

784
		try++;
C
Cliff Wickman 已提交
785 786 787
		completion_stat = wait_completion(bau_desc, bcp, try);

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

789
		if (bcp->ipi_attempts >= bcp->ipi_reset_limit) {
790
			bcp->ipi_attempts = 0;
C
Cliff Wickman 已提交
791
			completion_stat = FLUSH_GIVEUP;
792 793 794
			break;
		}
		cpu_relax();
C
Cliff Wickman 已提交
795 796 797
	} while ((completion_stat == FLUSH_RETRY_PLUGGED) ||
		 (completion_stat == FLUSH_RETRY_TIMEOUT));

798
	time2 = get_cycles();
C
Cliff Wickman 已提交
799 800 801

	count_max_concurr(completion_stat, bcp, hmaster);

802 803
	while (hmaster->uvhub_quiesce)
		cpu_relax();
C
Cliff Wickman 已提交
804

805
	atomic_dec(&hmaster->active_descriptor_count);
C
Cliff Wickman 已提交
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

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

	if (completion_stat == FLUSH_GIVEUP)
		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;
833
			}
C
Cliff Wickman 已提交
834
			return 0;
835
		}
C
Cliff Wickman 已提交
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
	}
	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);
853
	} else
C
Cliff Wickman 已提交
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
		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)++;
897
	}
C
Cliff Wickman 已提交
898 899
	if (!cnt)
		return 1;
900
	return 0;
901 902
}

C
Cliff Wickman 已提交
903 904
/*
 * globally purge translation cache of a virtual address or all TLB's
T
Tejun Heo 已提交
905
 * @cpumask: mask of all cpu's in which the address is to be removed
906 907
 * @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 已提交
908
 * @cpu: the current cpu
909 910 911 912 913 914
 *
 * 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 已提交
915 916
 * 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())
917
 *
918 919
 * The cpumask is converted into a uvhubmask of the uvhubs containing
 * those cpus.
920
 *
T
Tejun Heo 已提交
921 922 923 924 925
 * 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.
926
 */
T
Tejun Heo 已提交
927
const struct cpumask *uv_flush_tlb_others(const struct cpumask *cpumask,
C
Cliff Wickman 已提交
928 929
				struct mm_struct *mm, unsigned long va,
				unsigned int cpu)
930
{
931
	int locals = 0;
932 933
	int remotes = 0;
	int hubs = 0;
934
	struct bau_desc *bau_desc;
935 936 937
	struct cpumask *flush_mask;
	struct ptc_stats *stat;
	struct bau_control *bcp;
T
Tejun Heo 已提交
938

939
	/* kernel was booted 'nobau' */
940 941
	if (nobau)
		return cpumask;
T
Tejun Heo 已提交
942

943
	bcp = &per_cpu(bau_control, cpu);
944
	stat = bcp->statp;
945 946 947

	/* bau was disabled due to slow response */
	if (bcp->baudisabled) {
C
Cliff Wickman 已提交
948 949
		if (check_enable(bcp, stat))
			return cpumask;
950
	}
951

952 953
	/*
	 * Each sending cpu has a per-cpu mask which it fills from the caller's
954 955
	 * cpu mask.  All cpus are converted to uvhubs and copied to the
	 * activation descriptor.
956 957
	 */
	flush_mask = (struct cpumask *)per_cpu(uv_flush_tlb_mask, cpu);
958
	/* don't actually do a shootdown of the local cpu */
959
	cpumask_andnot(flush_mask, cpumask, cpumask_of(cpu));
C
Cliff Wickman 已提交
960

961
	if (cpu_isset(cpu, *cpumask))
962
		stat->s_ntargself++;
963

964
	bau_desc = bcp->descriptor_base;
C
Cliff Wickman 已提交
965
	bau_desc += ITEMS_PER_DESC * bcp->uvhub_cpu;
966
	bau_uvhubs_clear(&bau_desc->distribution, UV_DISTRIBUTION_SIZE);
C
Cliff Wickman 已提交
967
	if (set_distrib_bits(flush_mask, bcp, bau_desc, &locals, &remotes))
968 969
		return NULL;

C
Cliff Wickman 已提交
970
	record_send_statistics(stat, locals, hubs, remotes, bau_desc);
971 972

	bau_desc->payload.address = va;
T
Tejun Heo 已提交
973
	bau_desc->payload.sending_cpu = cpu;
974
	/*
975 976
	 * 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.
977
	 */
978 979 980 981
	if (!uv_flush_send_and_wait(bau_desc, flush_mask, bcp))
		return NULL;
	else
		return cpumask;
982 983 984 985 986 987 988 989
}

/*
 * The BAU message interrupt comes here. (registered by set_intr_gate)
 * See entry_64.S
 *
 * We received a broadcast assist message.
 *
990
 * Interrupts are disabled; this interrupt could represent
991 992
 * the receipt of several messages.
 *
993 994
 * All cores/threads on this hub get this interrupt.
 * The last one to see it does the software ack.
995
 * (the resource will not be freed until noninterruptable cpus see this
996
 *  interrupt; hardware may timeout the s/w ack and reply ERROR)
997
 */
998
void uv_bau_message_interrupt(struct pt_regs *regs)
999 1000
{
	int count = 0;
1001
	cycles_t time_start;
C
Cliff Wickman 已提交
1002
	struct bau_pq_entry *msg;
1003 1004 1005 1006 1007
	struct bau_control *bcp;
	struct ptc_stats *stat;
	struct msg_desc msgdesc;

	time_start = get_cycles();
C
Cliff Wickman 已提交
1008

1009
	bcp = &per_cpu(bau_control, smp_processor_id());
1010
	stat = bcp->statp;
C
Cliff Wickman 已提交
1011 1012 1013 1014

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

1015
	msg = bcp->bau_msg_head;
C
Cliff Wickman 已提交
1016
	while (msg->swack_vec) {
1017
		count++;
C
Cliff Wickman 已提交
1018 1019 1020

		msgdesc.msg_slot = msg - msgdesc.queue_first;
		msgdesc.swack_slot = ffs(msg->swack_vec) - 1;
1021
		msgdesc.msg = msg;
C
Cliff Wickman 已提交
1022 1023
		bau_process_message(&msgdesc, bcp);

1024
		msg++;
C
Cliff Wickman 已提交
1025 1026
		if (msg > msgdesc.queue_last)
			msg = msgdesc.queue_first;
1027
		bcp->bau_msg_head = msg;
1028
	}
1029
	stat->d_time += (get_cycles() - time_start);
1030
	if (!count)
1031
		stat->d_nomsg++;
1032
	else if (count > 1)
1033
		stat->d_multmsg++;
C
Cliff Wickman 已提交
1034

1035
	ack_APIC_irq();
1036 1037
}

C
Cliff Wickman 已提交
1038
/*
C
Cliff Wickman 已提交
1039
 * Each target uvhub (i.e. a uvhub that has cpu's) needs to have
C
Cliff Wickman 已提交
1040 1041 1042 1043
 * 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 已提交
1044
static void __init enable_timeouts(void)
1045
{
1046 1047
	int uvhub;
	int nuvhubs;
1048
	int pnode;
C
Cliff Wickman 已提交
1049
	unsigned long mmr_image;
1050

1051
	nuvhubs = uv_num_possible_blades();
1052

1053 1054
	for (uvhub = 0; uvhub < nuvhubs; uvhub++) {
		if (!uv_blade_nr_possible_cpus(uvhub))
1055
			continue;
C
Cliff Wickman 已提交
1056

1057
		pnode = uv_blade_to_pnode(uvhub);
C
Cliff Wickman 已提交
1058
		mmr_image = read_mmr_misc_control(pnode);
C
Cliff Wickman 已提交
1059 1060 1061 1062 1063 1064
		/*
		 * 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 已提交
1065 1066
		mmr_image &= ~(1L << SOFTACK_MSHIFT);
		write_mmr_misc_control(pnode, mmr_image);
C
Cliff Wickman 已提交
1067 1068 1069
		/*
		 * Set the 4-bit period.
		 */
C
Cliff Wickman 已提交
1070 1071 1072
		mmr_image &= ~((unsigned long)0xf << SOFTACK_PSHIFT);
		mmr_image |= (SOFTACK_TIMEOUT_PERIOD << SOFTACK_PSHIFT);
		write_mmr_misc_control(pnode, mmr_image);
C
Cliff Wickman 已提交
1073
		/*
1074
		 * UV1:
C
Cliff Wickman 已提交
1075 1076 1077 1078
		 * 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 已提交
1079
		mmr_image |= (1L << SOFTACK_MSHIFT);
1080
		if (is_uv2_hub()) {
C
Cliff Wickman 已提交
1081 1082
			mmr_image |= (1L << UV2_LEG_SHFT);
			mmr_image |= (1L << UV2_EXT_SHFT);
1083
		}
C
Cliff Wickman 已提交
1084
		write_mmr_misc_control(pnode, mmr_image);
1085 1086 1087
	}
}

C
Cliff Wickman 已提交
1088
static void *ptc_seq_start(struct seq_file *file, loff_t *offset)
1089 1090 1091 1092 1093 1094
{
	if (*offset < num_possible_cpus())
		return offset;
	return NULL;
}

C
Cliff Wickman 已提交
1095
static void *ptc_seq_next(struct seq_file *file, void *data, loff_t *offset)
1096 1097 1098 1099 1100 1101 1102
{
	(*offset)++;
	if (*offset < num_possible_cpus())
		return offset;
	return NULL;
}

C
Cliff Wickman 已提交
1103
static void ptc_seq_stop(struct seq_file *file, void *data)
1104 1105 1106
{
}

C
Cliff Wickman 已提交
1107
static inline unsigned long long usec_2_cycles(unsigned long microsec)
1108 1109 1110 1111
{
	unsigned long ns;
	unsigned long long cyc;

1112
	ns = microsec * 1000;
1113 1114 1115 1116
	cyc = (ns << CYC2NS_SCALE_FACTOR)/(per_cpu(cyc2ns, smp_processor_id()));
	return cyc;
}

1117
/*
C
Cliff Wickman 已提交
1118
 * Display the statistics thru /proc/sgi_uv/ptc_statistics
1119
 * 'data' points to the cpu number
C
Cliff Wickman 已提交
1120
 * Note: see the descriptions in stat_description[].
1121
 */
C
Cliff Wickman 已提交
1122
static int ptc_seq_show(struct seq_file *file, void *data)
1123 1124 1125 1126 1127 1128 1129
{
	struct ptc_stats *stat;
	int cpu;

	cpu = *(loff_t *)data;
	if (!cpu) {
		seq_printf(file,
1130 1131 1132
			"# cpu sent stime self locals remotes ncpus localhub ");
		seq_printf(file,
			"remotehub numuvhubs numuvhubs16 numuvhubs8 ");
1133
		seq_printf(file,
C
Cliff Wickman 已提交
1134
			"numuvhubs4 numuvhubs2 numuvhubs1 dto retries rok ");
1135
		seq_printf(file,
C
Cliff Wickman 已提交
1136
			"resetp resett giveup sto bz throt swack recv rtime ");
1137
		seq_printf(file,
C
Cliff Wickman 已提交
1138
			"all one mult none retry canc nocan reset rcan ");
1139 1140
		seq_printf(file,
			"disable enable\n");
1141 1142 1143
	}
	if (cpu < num_possible_cpus() && cpu_online(cpu)) {
		stat = &per_cpu(ptcstats, cpu);
1144 1145 1146 1147
		/* 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),
1148 1149 1150 1151 1152
			   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 ",
1153 1154
			   stat->s_ntarguvhub8, stat->s_ntarguvhub4,
			   stat->s_ntarguvhub2, stat->s_ntarguvhub1,
1155
			   stat->s_dtimeout);
1156 1157 1158 1159 1160
		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);
1161

1162 1163
		/* destination side statistics */
		seq_printf(file,
1164
			   "%lx %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld ",
C
Cliff Wickman 已提交
1165
			   read_gmmr_sw_ack(uv_cpu_to_pnode(cpu)),
1166 1167 1168 1169 1170
			   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);
1171 1172
		seq_printf(file, "%ld %ld\n",
			stat->s_bau_disabled, stat->s_bau_reenabled);
1173 1174 1175 1176
	}
	return 0;
}

1177 1178 1179 1180
/*
 * Display the tunables thru debugfs
 */
static ssize_t tunables_read(struct file *file, char __user *userbuf,
C
Cliff Wickman 已提交
1181
				size_t count, loff_t *ppos)
1182
{
1183
	char *buf;
1184 1185
	int ret;

1186
	buf = kasprintf(GFP_KERNEL, "%s %s %s\n%d %d %d %d %d %d %d %d %d\n",
C
Cliff Wickman 已提交
1187
		"max_concur plugged_delay plugsb4reset",
1188 1189
		"timeoutsb4reset ipi_reset_limit complete_threshold",
		"congested_response_us congested_reps congested_period",
C
Cliff Wickman 已提交
1190
		max_concurr, plugged_delay, plugsb4reset,
1191
		timeoutsb4reset, ipi_reset_limit, complete_threshold,
C
Cliff Wickman 已提交
1192
		congested_respns_us, congested_reps, congested_period);
1193

1194 1195 1196 1197 1198 1199
	if (!buf)
		return -ENOMEM;

	ret = simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
	kfree(buf);
	return ret;
1200 1201
}

1202
/*
C
Cliff Wickman 已提交
1203 1204
 * handle a write to /proc/sgi_uv/ptc_statistics
 * -1: reset the statistics
1205 1206
 *  0: display meaning of the statistics
 */
C
Cliff Wickman 已提交
1207 1208
static ssize_t ptc_proc_write(struct file *file, const char __user *user,
				size_t count, loff_t *data)
1209
{
1210
	int cpu;
C
Cliff Wickman 已提交
1211 1212
	int i;
	int elements;
1213
	long input_arg;
1214
	char optstr[64];
1215
	struct ptc_stats *stat;
1216

1217
	if (count == 0 || count > sizeof(optstr))
1218
		return -EINVAL;
1219 1220 1221
	if (copy_from_user(optstr, user, count))
		return -EFAULT;
	optstr[count - 1] = '\0';
C
Cliff Wickman 已提交
1222

1223
	if (strict_strtol(optstr, 10, &input_arg) < 0) {
1224 1225 1226 1227
		printk(KERN_DEBUG "%s is invalid\n", optstr);
		return -EINVAL;
	}

1228
	if (input_arg == 0) {
C
Cliff Wickman 已提交
1229
		elements = sizeof(stat_description)/sizeof(*stat_description);
1230
		printk(KERN_DEBUG "# cpu:      cpu number\n");
1231
		printk(KERN_DEBUG "Sender statistics:\n");
C
Cliff Wickman 已提交
1232 1233
		for (i = 0; i < elements; i++)
			printk(KERN_DEBUG "%s\n", stat_description[i]);
1234 1235 1236 1237 1238
	} else if (input_arg == -1) {
		for_each_present_cpu(cpu) {
			stat = &per_cpu(ptcstats, cpu);
			memset(stat, 0, sizeof(struct ptc_stats));
		}
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
	}

	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;
1255
		}
1256
	}
1257 1258 1259
}

/*
C
Cliff Wickman 已提交
1260 1261
 * Parse the values written to /sys/kernel/debug/sgi_uv/bau_tunables.
 * Zero values reset them to defaults.
1262
 */
C
Cliff Wickman 已提交
1263 1264
static int parse_tunables_write(struct bau_control *bcp, char *instr,
				int count)
1265 1266 1267
{
	char *p;
	char *q;
C
Cliff Wickman 已提交
1268 1269 1270
	int cnt = 0;
	int val;
	int e = sizeof(tunables) / sizeof(*tunables);
1271 1272 1273 1274 1275 1276 1277 1278 1279

	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 已提交
1280 1281
	if (cnt != e) {
		printk(KERN_INFO "bau tunable error: should be %d values\n", e);
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
		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 已提交
1293 1294
				max_concurr = MAX_BAU_CONCURRENT;
				max_concurr_const = MAX_BAU_CONCURRENT;
1295 1296 1297 1298 1299 1300 1301 1302
				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 已提交
1303 1304
			max_concurr = val;
			max_concurr_const = val;
1305
			continue;
C
Cliff Wickman 已提交
1306
		default:
1307
			if (val == 0)
C
Cliff Wickman 已提交
1308
				*tunables[cnt].tunp = tunables[cnt].deflt;
1309
			else
C
Cliff Wickman 已提交
1310
				*tunables[cnt].tunp = val;
1311 1312 1313 1314 1315
			continue;
		}
		if (q == p)
			break;
	}
C
Cliff Wickman 已提交
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
	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';

1337 1338
	cpu = get_cpu();
	bcp = &per_cpu(bau_control, cpu);
C
Cliff Wickman 已提交
1339
	ret = parse_tunables_write(bcp, instr, count);
1340
	put_cpu();
C
Cliff Wickman 已提交
1341 1342 1343
	if (ret)
		return ret;

1344 1345
	for_each_present_cpu(cpu) {
		bcp = &per_cpu(bau_control, cpu);
C
Cliff Wickman 已提交
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
		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;
1356
	}
1357 1358 1359 1360
	return count;
}

static const struct seq_operations uv_ptc_seq_ops = {
C
Cliff Wickman 已提交
1361 1362 1363 1364
	.start		= ptc_seq_start,
	.next		= ptc_seq_next,
	.stop		= ptc_seq_stop,
	.show		= ptc_seq_show
1365 1366
};

C
Cliff Wickman 已提交
1367
static int ptc_proc_open(struct inode *inode, struct file *file)
1368 1369 1370 1371
{
	return seq_open(file, &uv_ptc_seq_ops);
}

1372 1373 1374 1375 1376
static int tunables_open(struct inode *inode, struct file *file)
{
	return 0;
}

1377
static const struct file_operations proc_uv_ptc_operations = {
C
Cliff Wickman 已提交
1378
	.open		= ptc_proc_open,
1379
	.read		= seq_read,
C
Cliff Wickman 已提交
1380
	.write		= ptc_proc_write,
1381 1382
	.llseek		= seq_lseek,
	.release	= seq_release,
1383 1384
};

1385 1386 1387 1388
static const struct file_operations tunables_fops = {
	.open		= tunables_open,
	.read		= tunables_read,
	.write		= tunables_write,
1389
	.llseek		= default_llseek,
1390 1391
};

1392
static int __init uv_ptc_init(void)
1393
{
1394
	struct proc_dir_entry *proc_uv_ptc;
1395 1396 1397 1398

	if (!is_uv_system())
		return 0;

1399 1400
	proc_uv_ptc = proc_create(UV_PTC_BASENAME, 0444, NULL,
				  &proc_uv_ptc_operations);
1401 1402 1403 1404 1405
	if (!proc_uv_ptc) {
		printk(KERN_ERR "unable to create %s proc entry\n",
		       UV_PTC_BASENAME);
		return -EINVAL;
	}
1406 1407 1408 1409 1410 1411 1412 1413

	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 已提交
1414
					tunables_dir, NULL, &tunables_fops);
1415 1416 1417 1418 1419
	if (!tunables_file) {
		printk(KERN_ERR "unable to create debugfs file %s\n",
		       UV_BAU_TUNABLES_FILE);
		return -EINVAL;
	}
1420 1421 1422 1423
	return 0;
}

/*
1424
 * Initialize the sending side's sending buffers.
1425
 */
C
Cliff Wickman 已提交
1426
static void activation_descriptor_init(int node, int pnode, int base_pnode)
1427 1428
{
	int i;
1429
	int cpu;
1430 1431
	unsigned long pa;
	unsigned long m;
1432
	unsigned long n;
C
Cliff Wickman 已提交
1433
	size_t dsize;
1434 1435 1436
	struct bau_desc *bau_desc;
	struct bau_desc *bd2;
	struct bau_control *bcp;
1437

1438
	/*
C
Cliff Wickman 已提交
1439 1440
	 * each bau_desc is 64 bytes; there are 8 (ITEMS_PER_DESC)
	 * per cpu; and one per cpu on the uvhub (ADP_SZ)
1441
	 */
C
Cliff Wickman 已提交
1442 1443
	dsize = sizeof(struct bau_desc) * ADP_SZ * ITEMS_PER_DESC;
	bau_desc = kmalloc_node(dsize, GFP_KERNEL, node);
1444
	BUG_ON(!bau_desc);
1445

1446 1447
	pa = uv_gpa(bau_desc); /* need the real nasid*/
	n = pa >> uv_nshift;
1448
	m = pa & uv_mmask;
1449

1450
	/* the 14-bit pnode */
C
Cliff Wickman 已提交
1451
	write_mmr_descriptor_base(pnode, (n << UV_DESC_PSHIFT | m));
1452
	/*
C
Cliff Wickman 已提交
1453
	 * Initializing all 8 (ITEMS_PER_DESC) descriptors for each
1454
	 * cpu even though we only use the first one; one descriptor can
1455
	 * describe a broadcast to 256 uv hubs.
1456
	 */
C
Cliff Wickman 已提交
1457
	for (i = 0, bd2 = bau_desc; i < (ADP_SZ * ITEMS_PER_DESC); i++, bd2++) {
1458
		memset(bd2, 0, sizeof(struct bau_desc));
C
Cliff Wickman 已提交
1459
		bd2->header.swack_flag =	1;
1460
		/*
1461 1462 1463 1464
		 * 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.
1465
		 */
C
Cliff Wickman 已提交
1466 1467 1468 1469
		bd2->header.base_dest_nasid =	UV_PNODE_TO_NASID(base_pnode);
		bd2->header.dest_subnodeid =	UV_LB_SUBNODEID;
		bd2->header.command =		UV_NET_ENDPOINT_INTD;
		bd2->header.int_both =		1;
1470 1471 1472 1473 1474
		/*
		 * all others need to be set to zero:
		 *   fairness chaining multilevel count replied_to
		 */
	}
1475 1476 1477 1478 1479 1480
	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;
	}
1481 1482 1483 1484
}

/*
 * initialize the destination side's receiving buffers
1485 1486 1487
 * entered for each uvhub in the partition
 * - node is first node (kernel memory notion) on the uvhub
 * - pnode is the uvhub's physical identifier
1488
 */
C
Cliff Wickman 已提交
1489
static void pq_init(int node, int pnode)
1490
{
1491
	int cpu;
C
Cliff Wickman 已提交
1492
	size_t plsize;
1493
	char *cp;
C
Cliff Wickman 已提交
1494 1495 1496 1497 1498 1499
	void *vp;
	unsigned long pn;
	unsigned long first;
	unsigned long pn_first;
	unsigned long last;
	struct bau_pq_entry *pqp;
1500
	struct bau_control *bcp;
1501

C
Cliff Wickman 已提交
1502 1503 1504
	plsize = (DEST_Q_SIZE + 1) * sizeof(struct bau_pq_entry);
	vp = kmalloc_node(plsize, GFP_KERNEL, node);
	pqp = (struct bau_pq_entry *)vp;
1505
	BUG_ON(!pqp);
1506

1507
	cp = (char *)pqp + 31;
C
Cliff Wickman 已提交
1508
	pqp = (struct bau_pq_entry *)(((unsigned long)cp >> 5) << 5);
1509 1510 1511 1512 1513 1514

	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 已提交
1515 1516 1517
		bcp->queue_first	= pqp;
		bcp->bau_msg_head	= pqp;
		bcp->queue_last		= pqp + (DEST_Q_SIZE - 1);
1518
	}
1519 1520 1521
	/*
	 * need the pnode of where the memory was really allocated
	 */
C
Cliff Wickman 已提交
1522 1523 1524 1525 1526 1527 1528 1529
	pn = uv_gpa(pqp) >> uv_nshift;
	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);

1530
	/* in effect, all msg_type's are set to MSG_NOOP */
C
Cliff Wickman 已提交
1531
	memset(pqp, 0, sizeof(struct bau_pq_entry) * DEST_Q_SIZE);
1532
}
1533

1534
/*
1535
 * Initialization of each UV hub's structures
1536
 */
C
Cliff Wickman 已提交
1537
static void __init init_uvhub(int uvhub, int vector, int base_pnode)
1538
{
1539
	int node;
1540 1541
	int pnode;
	unsigned long apicid;
1542 1543 1544

	node = uvhub_to_first_node(uvhub);
	pnode = uv_blade_to_pnode(uvhub);
C
Cliff Wickman 已提交
1545 1546 1547 1548

	activation_descriptor_init(node, pnode, base_pnode);

	pq_init(node, pnode);
1549
	/*
1550 1551
	 * The below initialization can't be in firmware because the
	 * messaging IRQ will be determined by the OS.
1552
	 */
1553
	apicid = uvhub_to_first_apicid(uvhub) | uv_apicid_hibits;
C
Cliff Wickman 已提交
1554
	write_mmr_data_config(pnode, ((apicid << 32) | vector));
1555 1556
}

1557 1558 1559
/*
 * 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 已提交
1560
 * So the destination timeout period has to be calculated from them.
1561
 */
C
Cliff Wickman 已提交
1562
static int calculate_destination_timeout(void)
1563 1564 1565 1566 1567 1568 1569 1570 1571
{
	unsigned long mmr_image;
	int mult1;
	int mult2;
	int index;
	int base;
	int ret;
	unsigned long ts_ns;

1572
	if (is_uv1_hub()) {
C
Cliff Wickman 已提交
1573
		mult1 = SOFTACK_TIMEOUT_PERIOD & BAU_MISC_CONTROL_MULT_MASK;
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
		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 {
		/* 4 bits  0/1 for 10/80us, 3 bits of multiplier */
		mmr_image = uv_read_local_mmr(UVH_AGING_PRESCALE_SEL);
		mmr_image = (mmr_image & UV_SA_MASK) >> UV_SA_SHFT;
C
Cliff Wickman 已提交
1585
		if (mmr_image & (1L << UV2_ACK_UNITS_SHFT))
1586 1587 1588 1589 1590 1591
			mult1 = 80;
		else
			mult1 = 10;
		base = mmr_image & UV2_ACK_MASK;
		ret = mult1 * base;
	}
1592 1593 1594
	return ret;
}

C
Cliff Wickman 已提交
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618
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;
	}
}

1619
/*
C
Cliff Wickman 已提交
1620
 * Scan all cpus to collect blade and socket summaries.
1621
 */
C
Cliff Wickman 已提交
1622 1623 1624
static int __init get_cpu_topology(int base_pnode,
					struct uvhub_desc *uvhub_descs,
					unsigned char *uvhub_mask)
1625 1626 1627 1628
{
	int cpu;
	int pnode;
	int uvhub;
C
Cliff Wickman 已提交
1629
	int socket;
1630 1631 1632 1633 1634 1635
	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 已提交
1636

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

1639
		pnode = uv_cpu_hub_info(cpu)->pnode;
C
Cliff Wickman 已提交
1640
		if ((pnode - base_pnode) >= UV_DISTRIBUTION_SIZE) {
1641 1642
			printk(KERN_EMERG
				"cpu %d pnode %d-%d beyond %d; BAU disabled\n",
C
Cliff Wickman 已提交
1643
				cpu, pnode, base_pnode, UV_DISTRIBUTION_SIZE);
1644 1645
			return 1;
		}
C
Cliff Wickman 已提交
1646

1647
		bcp->osnode = cpu_to_node(cpu);
C
Cliff Wickman 已提交
1648 1649
		bcp->partition_base_pnode = base_pnode;

1650
		uvhub = uv_cpu_hub_info(cpu)->numa_blade_id;
C
Cliff Wickman 已提交
1651
		*(uvhub_mask + (uvhub/8)) |= (1 << (uvhub%8));
1652
		bdp = &uvhub_descs[uvhub];
C
Cliff Wickman 已提交
1653

1654 1655 1656
		bdp->num_cpus++;
		bdp->uvhub = uvhub;
		bdp->pnode = pnode;
C
Cliff Wickman 已提交
1657

1658 1659
		/* kludge: 'assuming' one node per socket, and assuming that
		   disabling a socket just leaves a gap in node numbers */
1660
		socket = bcp->osnode & 1;
1661
		bdp->socket_mask |= (1 << socket);
1662 1663 1664
		sdp = &bdp->socket[socket];
		sdp->cpu_number[sdp->num_cpus] = cpu;
		sdp->num_cpus++;
1665
		if (sdp->num_cpus > MAX_CPUS_PER_SOCKET) {
C
Cliff Wickman 已提交
1666 1667
			printk(KERN_EMERG "%d cpus per socket invalid\n",
				sdp->num_cpus);
1668 1669
			return 1;
		}
1670
	}
C
Cliff Wickman 已提交
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 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 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737
	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;
	}
}

/*
 * 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;
		bcp->uvhub_master = *hmasterp;
		bcp->uvhub_cpu = uv_cpu_hub_info(cpu)->blade_processor_id;
		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 已提交
1738
	for (uvhub = 0; uvhub < nuvhubs; uvhub++) {
C
Cliff Wickman 已提交
1739 1740 1741 1742
		struct uvhub_desc *bdp;
		struct bau_control *smaster = NULL;
		struct bau_control *hmaster = NULL;

C
Cliff Wickman 已提交
1743 1744
		if (!(*(uvhub_mask + (uvhub/8)) & (1 << (uvhub%8))))
			continue;
C
Cliff Wickman 已提交
1745

1746
		bdp = &uvhub_descs[uvhub];
1747 1748 1749
		socket_mask = bdp->socket_mask;
		socket = 0;
		while (socket_mask) {
C
Cliff Wickman 已提交
1750 1751 1752 1753
			struct socket_desc *sdp;
			if ((socket_mask & 1)) {
				sdp = &bdp->socket[socket];
				if (scan_sock(sdp, bdp, &smaster, &hmaster))
1754
					return 1;
1755
				make_per_cpu_thp(smaster);
1756 1757
			}
			socket++;
1758
			socket_mask = (socket_mask >> 1);
1759 1760
		}
	}
C
Cliff Wickman 已提交
1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785
	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))
		return 1;

	if (summarize_uvhub_sockets(nuvhubs, uvhub_descs, uvhub_mask))
		return 1;

1786
	kfree(uvhub_descs);
C
Cliff Wickman 已提交
1787
	kfree(uvhub_mask);
C
Cliff Wickman 已提交
1788
	init_per_cpu_tunables();
1789
	return 0;
1790 1791 1792 1793 1794 1795 1796
}

/*
 * Initialization of BAU-related structures
 */
static int __init uv_bau_init(void)
{
1797 1798 1799
	int uvhub;
	int pnode;
	int nuvhubs;
1800
	int cur_cpu;
C
Cliff Wickman 已提交
1801
	int cpus;
1802
	int vector;
C
Cliff Wickman 已提交
1803
	cpumask_var_t *mask;
1804 1805 1806

	if (!is_uv_system())
		return 0;
1807

1808 1809 1810
	if (nobau)
		return 0;

C
Cliff Wickman 已提交
1811 1812 1813 1814
	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));
	}
1815

1816
	uv_nshift = uv_hub_info->m_val;
1817
	uv_mmask = (1UL << uv_hub_info->m_val) - 1;
1818
	nuvhubs = uv_num_possible_blades();
1819
	spin_lock_init(&disable_lock);
C
Cliff Wickman 已提交
1820
	congested_cycles = usec_2_cycles(congested_respns_us);
1821

C
Cliff Wickman 已提交
1822
	uv_base_pnode = 0x7fffffff;
1823
	for (uvhub = 0; uvhub < nuvhubs; uvhub++) {
C
Cliff Wickman 已提交
1824 1825 1826
		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);
1827 1828
	}

C
Cliff Wickman 已提交
1829
	if (init_per_cpu(nuvhubs, uv_base_pnode)) {
1830 1831 1832
		nobau = 1;
		return 0;
	}
1833 1834 1835 1836

	vector = UV_BAU_MESSAGE;
	for_each_possible_blade(uvhub)
		if (uv_blade_nr_possible_cpus(uvhub))
C
Cliff Wickman 已提交
1837
			init_uvhub(uvhub, vector, uv_base_pnode);
1838

C
Cliff Wickman 已提交
1839
	enable_timeouts();
1840 1841 1842
	alloc_intr_gate(vector, uv_bau_message_intr1);

	for_each_possible_blade(uvhub) {
1843
		if (uv_blade_nr_possible_cpus(uvhub)) {
C
Cliff Wickman 已提交
1844 1845
			unsigned long val;
			unsigned long mmr;
1846 1847
			pnode = uv_blade_to_pnode(uvhub);
			/* INIT the bau */
C
Cliff Wickman 已提交
1848 1849
			val = 1L << 63;
			write_gmmr_activation(pnode, val);
1850
			mmr = 1; /* should be 1 to broadcast to both sockets */
C
Cliff Wickman 已提交
1851
			write_mmr_data_broadcast(pnode, mmr);
1852
		}
1853
	}
1854

1855 1856
	return 0;
}
1857
core_initcall(uv_bau_init);
1858
fs_initcall(uv_ptc_init);