llsc4.c 24.4 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6
/* 
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
Jesse Barnes's avatar
Jesse Barnes committed
7
 * Copyright (C) 2000-2002 Silicon Graphics, Inc. All rights reserved.
Linus Torvalds's avatar
Linus Torvalds committed
8 9 10 11 12 13 14 15 16 17 18 19
 */

#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/smp.h>
#include <linux/kernel_stat.h>
#include <linux/mm.h>
#include <linux/delay.h>
#include <linux/string.h>
20
#include <linux/efi.h>
Linus Torvalds's avatar
Linus Torvalds committed
21 22
#include <asm/page.h>
#include <linux/threads.h>
David Mosberger's avatar
David Mosberger committed
23 24
#include <asm/sn/simulator.h>
#include <asm/sn/leds.h>
Linus Torvalds's avatar
Linus Torvalds committed
25 26 27 28 29 30 31 32 33 34 35 36

#include "llsc4.h"


#ifdef STANDALONE
#include "lock.h"
#endif

#ifdef INTTEST
static int	inttest=0;
#endif

David Mosberger's avatar
David Mosberger committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50
#ifdef IA64_SEMFIX_INSN
#undef IA64_SEMFIX_INSN
#endif
#ifdef IA64_SEMFIX
#undef IA64_SEMFIX
#endif
# define IA64_SEMFIX_INSN
# define IA64_SEMFIX    ""

#define NOLOCK		0xdead
#define BGUARD(linei)	(0xbbbb0000 | (linei));
#define EGUARD(linei)	(0xeeee0000 | (linei));
#define GUARDLINE(v)	((v)&0xffff)

Linus Torvalds's avatar
Linus Torvalds committed
51 52 53 54 55 56 57 58 59 60
/*
 * Test parameter table for AUTOTEST
 */
typedef struct {
	int	passes;
	int	linecount;
	int	linepad;
} autotest_table_t;

autotest_table_t autotest_table[] = {
David Mosberger's avatar
David Mosberger committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
	{50000000,	2,	0x2b4		},
	{50000000,	16,	0,		},
	{50000000,	16,	4,		},
	{50000000,	128,	0x44		},
	{50000000,	128,	0x84		},
	{50000000,	128,	0x200		},
	{50000000,	128,	0x204		},
	{50000000,	128,	0x2b4		},
	{50000000,	2,	8*MB+0x2b4	},
	{50000000,	16,	8*MB+0		},
	{50000000,	16,	8*MB+4		},
	{50000000,	128,	8*MB+0x44	},
	{50000000,	128,	8*MB+0x84	},
	{50000000,	128,	8*MB+0x200	},
	{50000000,	128,	8*MB+0x204	},
	{50000000,	128,	8*MB+0x2b4	},
Linus Torvalds's avatar
Linus Torvalds committed
77 78 79 80 81 82 83 84 85 86
	{0}};

/*
 * Array of virtual addresses available for test purposes.
 */

typedef struct {
	long	vstart;
	long	vend;
	long	nextaddr;
David Mosberger's avatar
David Mosberger committed
87
	long	nextinit;
Linus Torvalds's avatar
Linus Torvalds committed
88 89 90
	int	wrapcount;
} memmap_t;

David Mosberger's avatar
David Mosberger committed
91 92
#define MAPCHUNKS		128
memmap_t 	memmap[MAPCHUNKS];
Linus Torvalds's avatar
Linus Torvalds committed
93 94 95 96 97 98 99 100 101 102 103 104
int		memmapx=0;

typedef struct {
	void	*addr;
	long	data[16];
	long	data_fc[16];
} capture_line_t;

typedef struct {
	int	size;
	void	*blockaddr;
	void	*shadaddr;
David Mosberger's avatar
David Mosberger committed
105 106 107 108
	long	blockdata[48];
	long	shaddata[48];
	long	blockdata_fc[48];
	long	shaddata_fc[48];
Linus Torvalds's avatar
Linus Torvalds committed
109 110 111 112 113 114 115
	long	synerr;
} capture_t;

/*
 * PORTING NOTE: revisit this statement. On hardware we put mbase at 0 and
 * the rest of the tables have to start at 1MB to skip PROM tables.
 */
Jesse Barnes's avatar
Jesse Barnes committed
116 117
#define THREADPRIVATESZ()	((sizeof(threadprivate_t)+511)/512*512)
#define THREADPRIVATE(t)	((threadprivate_t*)(((long)mbase)+4096+t*THREADPRIVATESZ()))
Linus Torvalds's avatar
Linus Torvalds committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

#define k_capture		mbase->sk_capture
#define k_go			mbase->sk_go
#define k_linecount		mbase->sk_linecount
#define k_passes		mbase->sk_passes
#define k_napticks		mbase->sk_napticks
#define k_stop_on_error		mbase->sk_stop_on_error
#define k_verbose		mbase->sk_verbose
#define k_threadprivate		mbase->sk_threadprivate
#define k_blocks		mbase->sk_blocks
#define k_iter_msg		mbase->sk_iter_msg
#define k_vv			mbase->sk_vv
#define k_linepad		mbase->sk_linepad
#define k_options		mbase->sk_options
#define k_testnumber		mbase->sk_testnumber
#define k_currentpass		mbase->sk_currentpass

static long		blocks[MAX_LINECOUNT];		/* addresses of data blocks */
static control_t	*mbase;
static vint		initialized=0;

static unsigned int ran_conf_llsc(int);
static int  rerr(capture_t *, char *, void *, void *, int, int, int, int, int, int);
static void dumpline(void *, char *, char *, void *, void *, int);
static int  checkstop(int, int, uint);
static void spin(int);
static void capturedata(capture_t *, uint, void *, void *, int);
static int  randn(uint max, uint *seed);
static uint zrandom (uint *zranseed);
static int  set_lock(uint *, uint);
static int  clr_lock(uint *, uint);
static void Speedo(void);

int autotest_enabled=0;
static int llsctest_number=-1;
static int errstop_enabled=0;
static int fail_enabled=0;
David Mosberger's avatar
David Mosberger committed
155
static int l4_opt=0;
Linus Torvalds's avatar
Linus Torvalds committed
156
static int selective_trigger=0;
Linus Torvalds's avatar
Linus Torvalds committed
157
static int dump_block_addrs_opt=0;
David Mosberger's avatar
David Mosberger committed
158 159
static lock_t errlock=NOLOCK;
static private_t init_private[LLSC_MAXCPUS];
Linus Torvalds's avatar
Linus Torvalds committed
160 161 162 163 164 165

static int __init autotest_enable(char *str)
{
        autotest_enabled = 1;
	return 1;
}
Linus Torvalds's avatar
Linus Torvalds committed
166
static int __init set_llscblkadr(char *str)
Linus Torvalds's avatar
Linus Torvalds committed
167
{
Linus Torvalds's avatar
Linus Torvalds committed
168
	dump_block_addrs_opt = 1;
Linus Torvalds's avatar
Linus Torvalds committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
	return 1;
}
static int __init set_llscselt(char *str)
{
	selective_trigger = 1;
	return 1;
}
static int __init set_llsctest(char *str)
{
        llsctest_number = simple_strtol(str, &str, 10);
	if (llsctest_number < 0 || llsctest_number > 15)
		llsctest_number = -1;
	return 1;
}
static int __init set_llscerrstop(char *str)
{
        errstop_enabled = 1;
	return 1;
}
static int __init set_llscfail(char *str)
{
        fail_enabled = 8;
	return 1;
}
David Mosberger's avatar
David Mosberger committed
193 194 195 196 197
static int __init set_llscl4(char *str)
{
        l4_opt = 1;
	return 1;
}
Linus Torvalds's avatar
Linus Torvalds committed
198 199 200 201 202 203 204 205 206

static void print_params(void)
{
	printk ("********* Enter AUTOTEST facility on master cpu *************\n");
	printk ("  Test options:\n");
	printk ("     llsctest=<n>\t%d\tTest number to run (all = -1)\n", llsctest_number);
	printk ("     llscerrstop \t%s\tStop on error\n", errstop_enabled ? "on" : "off");
	printk ("     llscfail    \t%s\tForce a failure to test the trigger & error messages\n", fail_enabled ? "on" : "off");
	printk ("     llscselt    \t%s\tSelective triger on failures\n", selective_trigger ? "on" : "off");
Linus Torvalds's avatar
Linus Torvalds committed
207
	printk ("     llscblkadr  \t%s\tDump data block addresses\n", dump_block_addrs_opt ? "on" : "off");
David Mosberger's avatar
David Mosberger committed
208 209
	printk ("     llscl4      \t%s\tRun only tests that evict from L4\n", l4_opt ? "on" : "off");
	printk ("  SEMFIX: %s\n", IA64_SEMFIX);
Linus Torvalds's avatar
Linus Torvalds committed
210 211 212 213 214 215 216
	printk ("\n");
}
__setup("autotest", autotest_enable);
__setup("llsctest=", set_llsctest);
__setup("llscerrstop", set_llscerrstop);
__setup("llscfail", set_llscfail);
__setup("llscselt", set_llscselt);
Linus Torvalds's avatar
Linus Torvalds committed
217
__setup("llscblkadr", set_llscblkadr);
David Mosberger's avatar
David Mosberger committed
218 219
__setup("llscl4", set_llscl4);

Linus Torvalds's avatar
Linus Torvalds committed
220 221


David Mosberger's avatar
David Mosberger committed
222
static inline int
Linus Torvalds's avatar
Linus Torvalds committed
223 224 225
set_lock(uint *lock, uint id)
{
	uint	old;
David Mosberger's avatar
David Mosberger committed
226 227
	old = cmpxchg_acq(lock, NOLOCK, id);
	return (old == NOLOCK);
Linus Torvalds's avatar
Linus Torvalds committed
228 229
}

David Mosberger's avatar
David Mosberger committed
230
static inline int
Linus Torvalds's avatar
Linus Torvalds committed
231 232 233
clr_lock(uint *lock, uint id)
{
	uint	old;
David Mosberger's avatar
David Mosberger committed
234
	old = cmpxchg_rel(lock, id, NOLOCK);
Linus Torvalds's avatar
Linus Torvalds committed
235 236 237
	return (old == id);
}

David Mosberger's avatar
David Mosberger committed
238 239
static inline void
init_lock(uint *lock)
Linus Torvalds's avatar
Linus Torvalds committed
240
{
David Mosberger's avatar
David Mosberger committed
241
	*lock = NOLOCK;
Linus Torvalds's avatar
Linus Torvalds committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
}

/*------------------------------------------------------------------------+
| Routine  :  ran_conf_llsc - ll/sc shared data test                      |
| Description: This test checks the coherency of shared data              |
+------------------------------------------------------------------------*/
static unsigned int
ran_conf_llsc(int thread)
{
	private_t	pval;
	share_t		sval, sval2;
	uint		vv, linei, slinei, sharei, pass;
	long		t;
	lock_t		lockpat;
	share_t		*sharecopy;
	long		verbose, napticks, passes, linecount, lcount;
	dataline_t	*linep, *slinep;
	int		s, seed;
	threadprivate_t	*tp;
	uint		iter_msg, iter_msg_i=0;
	int		vv_mask;
	int		correct_errors;
	int		errs=0;
	int		stillbad;
	capture_t	capdata;
	private_t	*privp;
	share_t		*sharep;


	linecount = k_linecount;
	napticks = k_napticks;
	verbose = k_verbose;
	passes = k_passes;
	iter_msg = k_iter_msg;
	seed = (thread + 1) * 647;
	tp = THREADPRIVATE(thread);
	vv_mask = (k_vv>>((thread%16)*4)) & 0xf;
	correct_errors = k_options&0xff;

	memset (&capdata, 0, sizeof(capdata));
David Mosberger's avatar
David Mosberger committed
282 283
	for (linei=0; linei<linecount; linei++)
		tp->private[linei] = thread;	
Linus Torvalds's avatar
Linus Torvalds committed
284 285 286

	for (pass = 1; passes == 0 || pass < passes; pass++) {
		lockpat = (pass & 0x0fffffff) + (thread <<28);
David Mosberger's avatar
David Mosberger committed
287 288
		if (lockpat == NOLOCK)
			continue;
Linus Torvalds's avatar
Linus Torvalds committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
		tp->threadpasses = pass;
		if (checkstop(thread, pass, lockpat))
			return 0;
		iter_msg_i++;
		if (iter_msg && iter_msg_i > iter_msg) {
			printk("Thread %d, Pass %d\n", thread, pass);
			iter_msg_i = 0;
		}
		lcount = 0;

		/*
		 * Select line to perform operations on.
		 */
		linei = randn(linecount, &seed);
		sharei = randn(2, &seed);
		slinei = (linei + (linecount/2))%linecount;		/* I dont like this - fix later */

		linep = (dataline_t *)blocks[linei];
		slinep = (dataline_t *)blocks[slinei];
		if (sharei == 0)
			sharecopy = &slinep->share0;
		else
			sharecopy = &slinep->share1;


		vv = randn(4, &seed);
		if ((vv_mask & (1<<vv)) == 0)
			continue;

		if (napticks) {
			t = randn(napticks, &seed);
			udelay(t);
		}
		privp = &linep->private[thread];
		sharep = &linep->share[sharei];
		
		switch(vv) {
		case 0:
			/* Read and verify private count on line. */
			pval = *privp;
			if (verbose)
				printk("Line:%3d, Thread:%d:%d. Val: %x\n", linei, thread, vv, tp->private[linei]);
			if (pval != tp->private[linei]) {
				capturedata(&capdata, pass, privp, NULL, sizeof(*privp));
				stillbad = (*privp != tp->private[linei]);
				if (rerr(&capdata, "Private count", linep, slinep, thread, pass, linei, tp->private[linei], pval, stillbad)) {
					return 1;
				}
				if (correct_errors) {
					tp->private[linei] = *privp;
				}
				errs++;
			}
			break;

		case 1:
			/* Read, verify, and increment private count on line. */
			pval = *privp;
			if (verbose)
				printk("Line:%3d, Thread:%d:%d. Val: %x\n", linei, thread, vv, tp->private[linei]);
			if (pval != tp->private[linei]) {
				capturedata(&capdata, pass, privp, NULL, sizeof(*privp));
				stillbad = (*privp != tp->private[linei]);
				if (rerr(&capdata, "Private count & inc", linep, slinep, thread, pass, linei, tp->private[linei], pval, stillbad)) {
					return 1;
				}
				errs++;
			}
David Mosberger's avatar
David Mosberger committed
357
			pval = (pval==255) ? 0 : pval+1;
Linus Torvalds's avatar
Linus Torvalds committed
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
			*privp = pval;
			tp->private[linei] = pval;
			break;

		case 2:
			/* Lock line, read and verify shared data. */
			if (verbose)
				printk("Line:%3d, Thread:%d:%d. Val: %x\n", linei, thread, vv, *sharecopy);
			lcount = 0;
			while (LOCK(sharei) != 1) {
				if (checkstop(thread, pass, lockpat))
					return 0;
				if (lcount++>1000000) {
					capturedata(&capdata, pass, LOCKADDR(sharei), NULL, sizeof(lock_t));
					stillbad = (GETLOCK(sharei) != 0);
					rerr(&capdata, "Shared data lock", linep, slinep, thread, pass, linei, 0, GETLOCK(sharei), stillbad);
					return 1;
				}
				if ((lcount&0x3fff) == 0)
					udelay(1000);
			}

			sval = *sharep;
			sval2 = *sharecopy;
			if (pass > 12 && thread == 0 && fail_enabled == 1)
				sval++;
			if (sval != sval2) {
				capturedata(&capdata, pass, sharep, sharecopy, sizeof(*sharecopy));
				stillbad = (*sharep != *sharecopy);
				if (!stillbad && *sharep != sval && *sharecopy == sval2)
					stillbad = 2;
				if (rerr(&capdata, "Shared data", linep, slinep, thread, pass, linei, sval2, sval, stillbad)) {
					return 1;
				}
				if (correct_errors)
					*sharep = *sharecopy;
				errs++;
			}


			if ( (s=UNLOCK(sharei)) != 1) {
				capturedata(&capdata, pass, LOCKADDR(sharei), NULL, 4);
				stillbad = (GETLOCK(sharei) != lockpat);
				if (rerr(&capdata, "Shared data unlock", linep, slinep, thread, pass, linei, lockpat, GETLOCK(sharei), stillbad))
					return 1;
				if (correct_errors)
					ZEROLOCK(sharei);	
				errs++;
			}
			break;

		case 3:
			/* Lock line, read and verify shared data, modify shared data. */
			if (verbose)
				printk("Line:%3d, Thread:%d:%d. Val: %x\n", linei, thread, vv, *sharecopy);
			lcount = 0;
			while (LOCK(sharei) != 1) {
				if (checkstop(thread, pass, lockpat))
					return 0;
				if (lcount++>1000000) {
					capturedata(&capdata, pass, LOCKADDR(sharei), NULL, sizeof(lock_t));
					stillbad = (GETLOCK(sharei) != 0);
					rerr(&capdata, "Shared data lock & inc", linep, slinep, thread, pass, linei, 0, GETLOCK(sharei), stillbad);
					return 1;
				}
				if ((lcount&0x3fff) == 0)
					udelay(1000);
			}
			sval = *sharep;
			sval2 = *sharecopy;
			if (sval != sval2) {
				capturedata(&capdata, pass, sharep, sharecopy, sizeof(*sharecopy));
				stillbad = (*sharep != *sharecopy);
				if (!stillbad && *sharep != sval && *sharecopy == sval2)
					stillbad = 2;
				if (rerr(&capdata, "Shared data & inc", linep, slinep, thread, pass, linei, sval2, sval, stillbad)) {
					return 1;
				}
				errs++;
			}

			*sharep = lockpat;
			*sharecopy = lockpat;


			if ( (s=UNLOCK(sharei)) != 1) {
				capturedata(&capdata, pass, LOCKADDR(sharei), NULL, 4);
				stillbad = (GETLOCK(sharei) != lockpat);
				if (rerr(&capdata, "Shared data & inc unlock", linep, slinep, thread, pass, linei, thread, GETLOCK(sharei), stillbad))
					return 1;
				if (correct_errors)
					ZEROLOCK(sharei);	
				errs++;
			}
			break;
		}
	}

	return (errs > 0);
}

static void
trigger_la(long val)
{
	long	*p;

	p = (long*)0xc0000a0001000020L; /* PI_CPU_NUM */
	*p = val;
}

static long
getsynerr(void)
{
	long	err, *errp;

	errp = (long*)0xc0000e0000000340L;	/* SYN_ERR */
	err = *errp;
	if (err)
		*errp = -1L;
	return (err & ~0x60);
}

static int
David Mosberger's avatar
David Mosberger committed
481
rerr(capture_t *cap, char *msg, void *lp, void *slp, int thread, int pass, int badlinei, int exp, int found, int stillbad)
Linus Torvalds's avatar
Linus Torvalds committed
482
{
David Mosberger's avatar
David Mosberger committed
483
	int		cpu, i, linei;
Linus Torvalds's avatar
Linus Torvalds committed
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
	long 		synerr;
	int		selt;


	selt = selective_trigger && stillbad > 1 && 
			memcmp(cap->blockdata, cap->blockdata_fc, 128) != 0 &&
			memcmp(cap->shaddata, cap->shaddata_fc, 128) == 0;
	if (selt) {
		trigger_la(pass);
	} else if (selective_trigger) {
		k_go = ST_STOP;
		return k_stop_on_error;;
	}

	spin(1);
Linus Torvalds's avatar
Linus Torvalds committed
499 500 501 502 503
	i = 100;
	while (i && set_lock(&errlock, 1) != 1) {
		spin(1);
		i--;
	}
Linus Torvalds's avatar
Linus Torvalds committed
504
	printk ("\nDataError!: %-20s, test %ld, thread %d, line:%d, pass %d (0x%x), time %ld expected:%x, found:%x\n",
David Mosberger's avatar
David Mosberger committed
505
	    msg, k_testnumber, thread, badlinei, pass, pass, jiffies, exp, found);
Linus Torvalds's avatar
Linus Torvalds committed
506 507

	dumpline (lp, "Corrupted data", "D ", cap->blockaddr, cap->blockdata, cap->size);
David Mosberger's avatar
David Mosberger committed
508
#ifdef ZZZ
Linus Torvalds's avatar
Linus Torvalds committed
509 510
	if (memcmp(cap->blockdata, cap->blockdata_fc, 128))
		dumpline (lp, "Corrupted data", "DF", cap->blockaddr, cap->blockdata_fc, cap->size);
David Mosberger's avatar
David Mosberger committed
511
#endif
Linus Torvalds's avatar
Linus Torvalds committed
512 513 514

	if (cap->shadaddr) {
		dumpline (slp, "Shadow    data", "S ", cap->shadaddr, cap->shaddata, cap->size);
David Mosberger's avatar
David Mosberger committed
515
#ifdef ZZZ
Linus Torvalds's avatar
Linus Torvalds committed
516 517
		if (memcmp(cap->shaddata, cap->shaddata_fc, 128))
			dumpline (slp, "Shadow    data", "SF", cap->shadaddr, cap->shaddata_fc, cap->size);
David Mosberger's avatar
David Mosberger committed
518
#endif
Linus Torvalds's avatar
Linus Torvalds committed
519 520 521
	}
	
	printk("Threadpasses: ");
David Mosberger's avatar
David Mosberger committed
522 523 524 525
	for (cpu=0,i=0; cpu<LLSC_MAXCPUS; cpu++)
		if (k_threadprivate[cpu]->threadpasses) {
			if (i && (i%8) == 0)
				printk("\n            : ");
Linus Torvalds's avatar
Linus Torvalds committed
526
			printk("  %d:0x%x", cpu, k_threadprivate[cpu]->threadpasses);
David Mosberger's avatar
David Mosberger committed
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
			i++;
		}
	printk("\n");
	
	for (linei=0; linei<k_linecount; linei++) {
		int		slinei, g1linei, g2linei, g1err, g2err, sh0err, sh1err;
		dataline_t	*linep, *slinep;

		slinei = (linei + (k_linecount/2))%k_linecount;
		linep = (dataline_t *)blocks[linei];
		slinep = (dataline_t *)blocks[slinei];

		g1linei = GUARDLINE(linep->guard1);
		g2linei = GUARDLINE(linep->guard2);
		g1err = (g1linei != linei);
		g2err = (g2linei != linei);
		sh0err = (linep->share[0] != slinep->share0);
		sh1err = (linep->share[1] != slinep->share1);

		if (g1err || g2err || sh0err || sh1err) {
			printk("Line 0x%lx (%03d), %sG1 0x%lx (%03d), %sG2 0x%lx (%03d), %sSH0 %08x (%08x), %sSH1 %08x (%08x)\n",
				blocks[linei], linei, 
				g1err ? "*" : " ", blocks[g1linei], g1linei,
				g2err ? "*" : " ", blocks[g2linei], g2linei,
				sh0err ? "*" : " ", linep->share[0], slinep->share0,
				sh1err ? "*" : " ", linep->share[1], slinep->share1);

Linus Torvalds's avatar
Linus Torvalds committed
554

David Mosberger's avatar
David Mosberger committed
555 556
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
557 558 559 560 561 562 563

	printk("\nData was %sfixed by flushcache\n", (stillbad == 1 ? "**** NOT **** " : " "));
	synerr = getsynerr();
	if (synerr)
		printk("SYNERR: Thread %d, Synerr: 0x%lx\n", thread, synerr);
	spin(2);
	printk("\n\n");
Linus Torvalds's avatar
Linus Torvalds committed
564
	clr_lock(&errlock, 1);
Linus Torvalds's avatar
Linus Torvalds committed
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581

	if (errstop_enabled) {
		local_irq_disable();
		while(1);
	}
	return k_stop_on_error;
}


static void
dumpline(void *lp, char *str1, char *str2, void *addr, void *data, int size)
{
	long *p;
	int i, off;

	printk("%s at 0x%lx, size %d, block starts at 0x%lx\n", str1, (long)addr, size, (long)lp);
	p = (long*) data;
David Mosberger's avatar
David Mosberger committed
582 583
	for (i=0; i<48; i++, p++) {
		if (i%8 == 0) printk("%2s", i==16 ? str2 : "  ");
Linus Torvalds's avatar
Linus Torvalds committed
584 585 586 587 588 589 590 591 592 593 594 595
		printk(" %016lx", *p);
		if ((i&7)==7) printk("\n");
	}
	printk("   ");
	off = (((long)addr) ^ size) & 63L;
	for (i=0; i<off+size; i++) {
		printk("%s", (i>=off) ? "--" : "  ");
		if ((i%8) == 7)
			printk(" ");
	}

	off = ((long)addr) & 127;
David Mosberger's avatar
David Mosberger committed
596
	printk(" (line %d)\n", 2+off/64+1);
Linus Torvalds's avatar
Linus Torvalds committed
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
}


static int
randn(uint max, uint *seedp)
{
	if (max == 1)
		return(0);
	else
		return((int)(zrandom(seedp)>>10) % max);
}


static int
checkstop(int thread, int pass, uint lockpat)
{
	long	synerr;

	if (k_go == ST_RUN)
		return 0;
	if (k_go == ST_STOP)
		return 1;

	if (errstop_enabled) {
		local_irq_disable();
		while(1);
	}
	synerr = getsynerr();
	spin(2);
	if (k_go == ST_STOP)
		return 1;
	if (synerr)
		printk("SYNERR: Thread %d, Synerr: 0x%lx\n", thread, synerr);
	return 1;
}


static void
spin(int j)
{
	udelay(j * 500000);
}

static void
capturedata(capture_t *cap, uint pass, void *blockaddr, void *shadaddr, int size)
{

	if (!selective_trigger)
		trigger_la (pass);

David Mosberger's avatar
David Mosberger committed
647
	memcpy (cap->blockdata, CACHEALIGN(blockaddr)-128, 3*128);
Linus Torvalds's avatar
Linus Torvalds committed
648
	if (shadaddr) 
David Mosberger's avatar
David Mosberger committed
649
		memcpy (cap->shaddata, CACHEALIGN(shadaddr)-128, 3*128);
Linus Torvalds's avatar
Linus Torvalds committed
650 651 652 653 654 655 656 657 658 659 660 661

	if (k_stop_on_error) {
		k_go = ST_ERRSTOP;
	}

 	cap->size = size;
	cap->blockaddr = blockaddr;
	cap->shadaddr = shadaddr;

	asm volatile ("fc %0" :: "r"(blockaddr) : "memory");
	ia64_sync_i();
	ia64_srlz_d();
David Mosberger's avatar
David Mosberger committed
662
	memcpy (cap->blockdata_fc, CACHEALIGN(blockaddr)-128, 3*128);
Linus Torvalds's avatar
Linus Torvalds committed
663 664 665 666 667

	if (shadaddr) {
		asm volatile ("fc %0" :: "r"(shadaddr) : "memory");
		ia64_sync_i();
		ia64_srlz_d();
David Mosberger's avatar
David Mosberger committed
668
		memcpy (cap->shaddata_fc, CACHEALIGN(shadaddr)-128, 3*128);
Linus Torvalds's avatar
Linus Torvalds committed
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
	}
}

int             zranmult = 0x48c27395;

static uint  
zrandom (uint *seedp)
{
        *seedp = (*seedp * zranmult) & 0x7fffffff;
        return (*seedp);
}


void
set_autotest_params(void)
{
	static int	testnumber=-1;

	if (llsctest_number >= 0) {
		testnumber = llsctest_number;
	} else {
		testnumber++;
Linus Torvalds's avatar
Linus Torvalds committed
691
		if (autotest_table[testnumber].passes == 0) {
Linus Torvalds's avatar
Linus Torvalds committed
692
			testnumber = 0;
Linus Torvalds's avatar
Linus Torvalds committed
693 694
			dump_block_addrs_opt = 0;
		}
Linus Torvalds's avatar
Linus Torvalds committed
695
	}
David Mosberger's avatar
David Mosberger committed
696 697
	if (testnumber == 0 && l4_opt) testnumber = 9;

Linus Torvalds's avatar
Linus Torvalds committed
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
	k_passes = autotest_table[testnumber].passes;
	k_linepad = autotest_table[testnumber].linepad;
	k_linecount = autotest_table[testnumber].linecount;
	k_testnumber = testnumber;

	if (IS_RUNNING_ON_SIMULATOR()) {
		printk ("llsc start test %ld\n", k_testnumber);
		k_passes = 1000;
	}
}


static void
set_leds(int errs)
{
	unsigned char	leds=0;

	/*
	 * Leds are:
	 * 	ppppeee-  
	 *   where
	 *      pppp = test number
	 *       eee = error count but top bit is stick
	 */

	leds =  ((errs&7)<<1) | ((k_testnumber&15)<<4) | (errs ? 0x08 : 0);
David Mosberger's avatar
David Mosberger committed
724
	set_led_bits(leds, LED_MASK_AUTOTEST);
Linus Torvalds's avatar
Linus Torvalds committed
725 726 727 728 729
}

static void
setup_block_addresses(void)
{
David Mosberger's avatar
David Mosberger committed
730 731 732 733
	int			i, stride, memmapi;
	dataline_t		*dp;
	long			*ip, *ipe;

Linus Torvalds's avatar
Linus Torvalds committed
734

David Mosberger's avatar
David Mosberger committed
735
	stride = k_linepad + sizeof(dataline_t);
Linus Torvalds's avatar
Linus Torvalds committed
736 737 738
	memmapi = 0;
	for (i=0; i<memmapx; i++) {
		memmap[i].nextaddr = memmap[i].vstart;
David Mosberger's avatar
David Mosberger committed
739
		memmap[i].nextinit = memmap[i].vstart;
Linus Torvalds's avatar
Linus Torvalds committed
740 741 742 743 744
		memmap[i].wrapcount = 0;
	}

	for (i=0; i<k_linecount; i++) {
		blocks[i] = memmap[memmapi].nextaddr;
David Mosberger's avatar
David Mosberger committed
745 746
		dp = (dataline_t*)blocks[i];
		memmap[memmapi].nextaddr += (stride & 0xffff);
Linus Torvalds's avatar
Linus Torvalds committed
747 748 749 750 751 752
		if (memmap[memmapi].nextaddr + sizeof(dataline_t) >= memmap[memmapi].vend) {
			memmap[memmapi].wrapcount++;
			memmap[memmapi].nextaddr = memmap[memmapi].vstart + 
					memmap[memmapi].wrapcount * sizeof(dataline_t);
		}

David Mosberger's avatar
David Mosberger committed
753 754 755 756 757 758 759 760 761 762 763 764
		ip = (long*)((memmap[memmapi].nextinit+7)&~7);
		ipe = (long*)(memmap[memmapi].nextaddr+2*sizeof(dataline_t)+8);
		while(ip <= ipe && ip < ((long*)memmap[memmapi].vend-8))
			*ip++ = (long)ip;
		memmap[memmapi].nextinit = (long) ipe;
		dp->guard1 = BGUARD(i);
		dp->guard2 = EGUARD(i);
		dp->lock[0] = dp->lock[1] = NOLOCK;
		dp->share[0] = dp->share0 = 0x1111;
		dp->share[1] = dp->share1 = 0x2222;
		memcpy(dp->private, init_private, LLSC_MAXCPUS*sizeof(private_t));

Linus Torvalds's avatar
Linus Torvalds committed
765 766 767 768 769 770 771 772 773 774

		if (stride > 16384) {
			memmapi++;
			if (memmapi == memmapx)
				memmapi = 0;
		}
	}

}

Linus Torvalds's avatar
Linus Torvalds committed
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
static void
dump_block_addrs(void)
{
	int	i;

	printk("LLSC TestNumber %ld\n", k_testnumber);

	for (i=0; i<k_linecount; i++) {
		printk("  %lx", blocks[i]);
		if (i%4 == 3)
			printk("\n");
	}
	printk("\n");
}


Linus Torvalds's avatar
Linus Torvalds committed
791 792 793 794
static void
set_thread_state(int cpuid, int state)
{
	if (k_threadprivate[cpuid]->threadstate == TS_KILLED) {
David Mosberger's avatar
David Mosberger committed
795
		set_led_bits(LED_MASK_AUTOTEST, LED_MASK_AUTOTEST);
Linus Torvalds's avatar
Linus Torvalds committed
796 797 798 799 800
		while(1);
	}
	k_threadprivate[cpuid]->threadstate = state;
}

Jesse Barnes's avatar
Jesse Barnes committed
801
#define MINBLK	(16*1024*1024)
Linus Torvalds's avatar
Linus Torvalds committed
802 803 804
static int
build_mem_map(unsigned long start, unsigned long end, void *arg)
{
Jesse Barnes's avatar
Jesse Barnes committed
805
	long	lstart, lend;
Linus Torvalds's avatar
Linus Torvalds committed
806
	long	align = 8*MB;
David Mosberger's avatar
David Mosberger committed
807

Linus Torvalds's avatar
Linus Torvalds committed
808 809 810
	printk ("LLSC memmap: start 0x%lx, end 0x%lx, (0x%lx - 0x%lx)\n", 
		start, end, (long) virt_to_page(start), (long) virt_to_page(end-PAGE_SIZE));

Jesse Barnes's avatar
Jesse Barnes committed
811
	if (memmapx >= MAPCHUNKS || (end-start) < MINBLK)
David Mosberger's avatar
David Mosberger committed
812
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
813

Jesse Barnes's avatar
Jesse Barnes committed
814 815 816 817 818 819 820 821 822 823
	/*
	 * Start in the middle of the range & find the first non-free page in both directions
	 * from the midpoint. This is likely to be the bigest free block.
	 */
	lend = lstart = start + (end-start)/2;
	while (lend < end && !PageReserved(virt_to_page(lend)) && virt_to_page(lend)->count.counter == 0)
		lend += PAGE_SIZE;
	lend -= PAGE_SIZE;

	while (lstart >= start && !PageReserved(virt_to_page(lstart)) && virt_to_page(lstart)->count.counter == 0)
Linus Torvalds's avatar
Linus Torvalds committed
824
		lstart -= PAGE_SIZE;
Jesse Barnes's avatar
Jesse Barnes committed
825
	lstart += PAGE_SIZE;
Linus Torvalds's avatar
Linus Torvalds committed
826

Linus Torvalds's avatar
Linus Torvalds committed
827 828
	lstart = (lstart + align -1) /align * align;
	end = end / align * align;
Linus Torvalds's avatar
Linus Torvalds committed
829 830
	if (lstart >= end)
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
831
	printk ("     memmap: start 0x%lx, end 0x%lx\n", lstart, end);
Linus Torvalds's avatar
Linus Torvalds committed
832 833 834 835 836 837 838 839 840 841

	memmap[memmapx].vstart = lstart;
	memmap[memmapx].vend = end;
	memmapx++;
	return 0;
}

void int_test(void);

int
Jesse Barnes's avatar
Jesse Barnes committed
842
llsc_main (int cpuid)
Linus Torvalds's avatar
Linus Torvalds committed
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
{
	int		i, cpu, is_master, repeatcnt=0;
	unsigned int	preverr=0, errs=0, pass=0;
	int		automode=0;

#ifdef INTTEST
	if (inttest)
		int_test();
#endif

	if (!autotest_enabled)
		return 0;

#ifdef CONFIG_SMP
	is_master = !smp_processor_id();
#else
	is_master = 1;
#endif


	if (is_master) {
Jesse Barnes's avatar
Jesse Barnes committed
864 865
		mbase = (control_t*) __get_free_pages(GFP_KERNEL, get_order(4096+THREADPRIVATESZ()*LLSC_MAXCPUS));
		printk("LLSC: mbase 0x%lx\n", (long)mbase);
Linus Torvalds's avatar
Linus Torvalds committed
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
		print_params();
		if(!IS_RUNNING_ON_SIMULATOR())
			spin(10);
		k_currentpass = 0;
		k_go = ST_IDLE;
		k_passes = DEF_PASSES;
		k_napticks = DEF_NAPTICKS;
		k_stop_on_error = DEF_STOP_ON_ERROR;
		k_verbose = DEF_VERBOSE;
		k_linecount = DEF_LINECOUNT;
		k_iter_msg = DEF_ITER_MSG;
		k_vv = DEF_VV;
		k_linepad = DEF_LINEPAD;
		k_blocks = (void*)blocks;
		efi_memmap_walk(build_mem_map, 0);
	
#ifdef CONFIG_IA64_SGI_AUTOTEST
		automode = 1;
#endif

David Mosberger's avatar
David Mosberger committed
886
		for (i=0; i<LLSC_MAXCPUS; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
887 888
			k_threadprivate[i] = THREADPRIVATE(i);
			memset(k_threadprivate[i], 0, sizeof(*k_threadprivate[i]));
David Mosberger's avatar
David Mosberger committed
889
			init_private[i] = i;
Linus Torvalds's avatar
Linus Torvalds committed
890
		}
Jesse Barnes's avatar
Jesse Barnes committed
891
		mb();
Linus Torvalds's avatar
Linus Torvalds committed
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
		initialized = 1;
	} else {
		while (initialized == 0)
			udelay(100);
	}

loop:
	if (is_master) {
		if (automode) {
			if (!preverr || repeatcnt++ > 5) {
				set_autotest_params();
				repeatcnt = 0;
			}
		} else {
			while (k_go == ST_IDLE);
		}

		k_go = ST_INIT;
		if (k_linecount > MAX_LINECOUNT) k_linecount = MAX_LINECOUNT;
		k_linecount = k_linecount & ~1;
		setup_block_addresses();
David Mosberger's avatar
David Mosberger committed
913
		if (!preverr && dump_block_addrs_opt)
Linus Torvalds's avatar
Linus Torvalds committed
914
			dump_block_addrs();
Linus Torvalds's avatar
Linus Torvalds committed
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937

		k_currentpass = pass++;
		k_go = ST_RUN;
		if (fail_enabled)
			fail_enabled--;

	} else {
		while (k_go != ST_RUN || k_currentpass != pass);
		pass++;
	}


	set_leds(errs);
	set_thread_state(cpuid, TS_RUNNING);

	errs += ran_conf_llsc(cpuid);
	preverr = (k_go == ST_ERRSTOP);

	set_leds(errs);
	set_thread_state(cpuid, TS_STOPPED);

	if (is_master) {
		Speedo();
David Mosberger's avatar
David Mosberger committed
938
		for (i=0, cpu=0; cpu<LLSC_MAXCPUS; cpu++) {
Linus Torvalds's avatar
Linus Torvalds committed
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
			while (k_threadprivate[cpu]->threadstate == TS_RUNNING) {
				i++;
				if (i == 10000) { 
					k_go = ST_STOP;
					printk ("  llsc master stopping test number %ld\n", k_testnumber);
				}
				if (i > 100000) {
					k_threadprivate[cpu]->threadstate = TS_KILLED;
					printk ("  llsc: master killing cpuid %d, running test number %ld\n", 
							cpu, k_testnumber);
				}
				udelay(1000);
			}
		}
	}

	goto loop;
}


static void
Speedo(void)
{
	static int i = 0;

	switch (++i%4) {
	case 0:
		printk("|\b");
		break;
	case 1:
		printk("\\\b");
		break;
	case 2:
		printk("-\b");
		break;
	case 3:
		printk("/\b");
		break;
	}
}

#ifdef INTTEST

/* ======================================================================================================== 
 *
 * Some test code to verify that interrupts work
 *
 * Add the following to the arch/ia64/kernel/smp.c after the comment "Reschedule callback"
 * 		if (zzzprint_resched) printk("  cpu %d got interrupt\n", smp_processor_id());
 *
 * Enable the code in arch/ia64/sn/sn1/smp.c to print sending IPIs.
 *
 */

static int __init set_inttest(char *str)
{
        inttest = 1;
	autotest_enabled = 1;

	return 1;
}	

__setup("inttest=", set_inttest);

int	zzzprint_resched=0;

void
int_test() {
	int			mycpu, cpu;
	static volatile int	control_cpu=0;

	mycpu = smp_processor_id();
	zzzprint_resched = 2;

	printk("Testing cross interrupts\n");
	
Jesse Barnes's avatar
Jesse Barnes committed
1015 1016 1017 1018
	while (control_cpu != smp_num_cpus) {
		if (mycpu == cpu_logical_map(control_cpu)) {
			for (cpu=0; cpu<smp_num_cpus; cpu++) {
				printk("Sending interrupt from %d to %d\n", mycpu, cpu_logical_map(cpu));
Linus Torvalds's avatar
Linus Torvalds committed
1019
				udelay(IS_RUNNING_ON_SIMULATOR ? 10000 : 400000);
Jesse Barnes's avatar
Jesse Barnes committed
1020
				smp_send_reschedule(cpu_logical_map(cpu));
Linus Torvalds's avatar
Linus Torvalds committed
1021
				udelay(IS_RUNNING_ON_SIMULATOR ? 10000 : 400000);
Jesse Barnes's avatar
Jesse Barnes committed
1022
				smp_send_reschedule(cpu_logical_map(cpu));
Linus Torvalds's avatar
Linus Torvalds committed
1023 1024 1025 1026 1027 1028 1029 1030
				udelay(IS_RUNNING_ON_SIMULATOR ? 10000 : 400000);
			}
			control_cpu++;
		}
	}

	zzzprint_resched = 1;

Jesse Barnes's avatar
Jesse Barnes committed
1031
	if (mycpu == cpu_logical_map(smp_num_cpus-1)) {
Linus Torvalds's avatar
Linus Torvalds committed
1032 1033
		printk("\nTight loop of cpu %d sending ints to cpu 0 (every 100 us)\n", mycpu);
		udelay(IS_RUNNING_ON_SIMULATOR ? 1000 : 1000000);
Jesse Barnes's avatar
Jesse Barnes committed
1034
		__cli();
Linus Torvalds's avatar
Linus Torvalds committed
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
		while (1) {
			smp_send_reschedule(0);
			udelay(100);
		}

	}

	while(1);
}
#endif