pg-r4k.c 12.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.
 *
 * Copyright (C) 2003, 04, 05 Ralf Baechle (ralf@linux-mips.org)
7
 * Copyright (C) 2007  Maciej W. Rozycki
Linus Torvalds's avatar
Linus Torvalds committed
8 9 10 11 12 13 14 15
 */
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/proc_fs.h>

16
#include <asm/bugs.h>
Linus Torvalds's avatar
Linus Torvalds committed
17 18 19 20 21 22 23 24 25 26 27 28 29
#include <asm/cacheops.h>
#include <asm/inst.h>
#include <asm/io.h>
#include <asm/page.h>
#include <asm/pgtable.h>
#include <asm/prefetch.h>
#include <asm/system.h>
#include <asm/bootinfo.h>
#include <asm/mipsregs.h>
#include <asm/mmu_context.h>
#include <asm/cpu.h>
#include <asm/war.h>

30 31 32 33
#define half_scache_line_size()	(cpu_scache_line_size() >> 1)
#define cpu_is_r4600_v1_x()	((read_c0_prid() & 0xfffffff0) == 0x00002010)
#define cpu_is_r4600_v2_x()	((read_c0_prid() & 0xfffffff0) == 0x00002020)

Linus Torvalds's avatar
Linus Torvalds committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

/*
 * Maximum sizes:
 *
 * R4000 128 bytes S-cache:		0x58 bytes
 * R4600 v1.7:				0x5c bytes
 * R4600 v2.0:				0x60 bytes
 * With prefetching, 16 byte strides	0xa0 bytes
 */

static unsigned int clear_page_array[0x130 / 4];

void clear_page(void * page) __attribute__((alias("clear_page_array")));

EXPORT_SYMBOL(clear_page);

/*
 * Maximum sizes:
 *
 * R4000 128 bytes S-cache:		0x11c bytes
 * R4600 v1.7:				0x080 bytes
 * R4600 v2.0:				0x07c bytes
 * With prefetching, 16 byte strides	0x0b8 bytes
 */
static unsigned int copy_page_array[0x148 / 4];

void copy_page(void *to, void *from) __attribute__((alias("copy_page_array")));

EXPORT_SYMBOL(copy_page);

/*
 * This is suboptimal for 32-bit kernels; we assume that R10000 is only used
 * with 64-bit kernels.  The prefetch offsets have been experimentally tuned
 * an Origin 200.
 */
69 70
static int pref_offset_clear __cpuinitdata = 512;
static int pref_offset_copy  __cpuinitdata = 256;
Linus Torvalds's avatar
Linus Torvalds committed
71

72 73
static unsigned int pref_src_mode __cpuinitdata;
static unsigned int pref_dst_mode __cpuinitdata;
Linus Torvalds's avatar
Linus Torvalds committed
74

75 76
static int load_offset __cpuinitdata;
static int store_offset __cpuinitdata;
Linus Torvalds's avatar
Linus Torvalds committed
77

78
static unsigned int __cpuinitdata *dest, *epc;
Linus Torvalds's avatar
Linus Torvalds committed
79 80 81 82

static unsigned int instruction_pending;
static union mips_instruction delayed_mi;

83
static void __cpuinit emit_instruction(union mips_instruction mi)
Linus Torvalds's avatar
Linus Torvalds committed
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
{
	if (instruction_pending)
		*epc++ = delayed_mi.word;

	instruction_pending = 1;
	delayed_mi = mi;
}

static inline void flush_delay_slot_or_nop(void)
{
	if (instruction_pending) {
		*epc++ = delayed_mi.word;
		instruction_pending = 0;
		return;
	}

	*epc++ = 0;
}

static inline unsigned int *label(void)
{
	if (instruction_pending) {
		*epc++ = delayed_mi.word;
		instruction_pending = 0;
	}

	return epc;
}

static inline void build_insn_word(unsigned int word)
{
	union mips_instruction mi;

	mi.word		 = word;

	emit_instruction(mi);
}

static inline void build_nop(void)
{
	build_insn_word(0);			/* nop */
}

static inline void build_src_pref(int advance)
{
Atsushi Nemoto's avatar
Atsushi Nemoto committed
129
	if (!(load_offset & (cpu_dcache_line_size() - 1)) && advance) {
Linus Torvalds's avatar
Linus Torvalds committed
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
		union mips_instruction mi;

		mi.i_format.opcode     = pref_op;
		mi.i_format.rs         = 5;		/* $a1 */
		mi.i_format.rt         = pref_src_mode;
		mi.i_format.simmediate = load_offset + advance;

		emit_instruction(mi);
	}
}

static inline void __build_load_reg(int reg)
{
	union mips_instruction mi;
	unsigned int width;

	if (cpu_has_64bit_gp_regs) {
		mi.i_format.opcode     = ld_op;
		width = 8;
	} else {
		mi.i_format.opcode     = lw_op;
		width = 4;
	}
	mi.i_format.rs         = 5;		/* $a1 */
	mi.i_format.rt         = reg;		/* $reg */
	mi.i_format.simmediate = load_offset;

	load_offset += width;
	emit_instruction(mi);
}

static inline void build_load_reg(int reg)
{
	if (cpu_has_prefetch)
		build_src_pref(pref_offset_copy);

	__build_load_reg(reg);
}

static inline void build_dst_pref(int advance)
{
Atsushi Nemoto's avatar
Atsushi Nemoto committed
171
	if (!(store_offset & (cpu_dcache_line_size() - 1)) && advance) {
Linus Torvalds's avatar
Linus Torvalds committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
		union mips_instruction mi;

		mi.i_format.opcode     = pref_op;
		mi.i_format.rs         = 4;		/* $a0 */
		mi.i_format.rt         = pref_dst_mode;
		mi.i_format.simmediate = store_offset + advance;

		emit_instruction(mi);
	}
}

static inline void build_cdex_s(void)
{
	union mips_instruction mi;

	if ((store_offset & (cpu_scache_line_size() - 1)))
		return;

	mi.c_format.opcode     = cache_op;
	mi.c_format.rs         = 4;		/* $a0 */
	mi.c_format.c_op       = 3;		/* Create Dirty Exclusive */
	mi.c_format.cache      = 3;		/* Secondary Data Cache */
	mi.c_format.simmediate = store_offset;

	emit_instruction(mi);
}

static inline void build_cdex_p(void)
{
	union mips_instruction mi;

	if (store_offset & (cpu_dcache_line_size() - 1))
		return;

206
	if (R4600_V1_HIT_CACHEOP_WAR && cpu_is_r4600_v1_x()) {
Linus Torvalds's avatar
Linus Torvalds committed
207 208 209 210 211 212
		build_nop();
		build_nop();
		build_nop();
		build_nop();
	}

213
	if (R4600_V2_HIT_CACHEOP_WAR && cpu_is_r4600_v2_x())
214
		build_insn_word(0x8c200000);	/* lw      $zero, ($at) */
Linus Torvalds's avatar
Linus Torvalds committed
215 216 217 218 219 220 221 222 223 224

	mi.c_format.opcode     = cache_op;
	mi.c_format.rs         = 4;		/* $a0 */
	mi.c_format.c_op       = 3;		/* Create Dirty Exclusive */
	mi.c_format.cache      = 1;		/* Data Cache */
	mi.c_format.simmediate = store_offset;

	emit_instruction(mi);
}

225
static void __cpuinit __build_store_reg(int reg)
Linus Torvalds's avatar
Linus Torvalds committed
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
{
	union mips_instruction mi;
	unsigned int width;

	if (cpu_has_64bit_gp_regs ||
	    (cpu_has_64bit_zero_reg && reg == 0)) {
		mi.i_format.opcode     = sd_op;
		width = 8;
	} else {
		mi.i_format.opcode     = sw_op;
		width = 4;
	}
	mi.i_format.rs         = 4;		/* $a0 */
	mi.i_format.rt         = reg;		/* $reg */
	mi.i_format.simmediate = store_offset;

	store_offset += width;
	emit_instruction(mi);
}

static inline void build_store_reg(int reg)
{
248 249 250 251
	int pref_off = cpu_has_prefetch ?
		(reg ? pref_offset_copy : pref_offset_clear) : 0;
	if (pref_off)
		build_dst_pref(pref_off);
Linus Torvalds's avatar
Linus Torvalds committed
252 253 254 255 256 257 258 259
	else if (cpu_has_cache_cdex_s)
		build_cdex_s();
	else if (cpu_has_cache_cdex_p)
		build_cdex_p();

	__build_store_reg(reg);
}

260 261
static inline void build_addiu_rt_rs(unsigned int rt, unsigned int rs,
				     unsigned long offset)
Linus Torvalds's avatar
Linus Torvalds committed
262 263 264 265 266
{
	union mips_instruction mi;

	BUG_ON(offset > 0x7fff);

267 268 269 270 271 272
	if (cpu_has_64bit_gp_regs && DADDI_WAR && r4k_daddiu_bug()) {
		mi.i_format.opcode     = addiu_op;
		mi.i_format.rs         = 0;	/* $zero */
		mi.i_format.rt         = 25;	/* $t9 */
		mi.i_format.simmediate = offset;
		emit_instruction(mi);
Linus Torvalds's avatar
Linus Torvalds committed
273

274 275 276 277 278 279 280 281 282 283 284 285 286
		mi.r_format.opcode     = spec_op;
		mi.r_format.rs         = rs;
		mi.r_format.rt         = 25;	/* $t9 */
		mi.r_format.rd         = rt;
		mi.r_format.re         = 0;
		mi.r_format.func       = daddu_op;
	} else {
		mi.i_format.opcode     = cpu_has_64bit_gp_regs ?
					 daddiu_op : addiu_op;
		mi.i_format.rs         = rs;
		mi.i_format.rt         = rt;
		mi.i_format.simmediate = offset;
	}
Linus Torvalds's avatar
Linus Torvalds committed
287 288 289
	emit_instruction(mi);
}

290
static inline void build_addiu_a2_a0(unsigned long offset)
291
{
292 293
	build_addiu_rt_rs(6, 4, offset);	/* $a2, $a0, offset */
}
294

295 296 297
static inline void build_addiu_a2(unsigned long offset)
{
	build_addiu_rt_rs(6, 6, offset);	/* $a2, $a2, offset */
298 299
}

Linus Torvalds's avatar
Linus Torvalds committed
300 301
static inline void build_addiu_a1(unsigned long offset)
{
302
	build_addiu_rt_rs(5, 5, offset);	/* $a1, $a1, offset */
Linus Torvalds's avatar
Linus Torvalds committed
303 304 305 306 307 308

	load_offset -= offset;
}

static inline void build_addiu_a0(unsigned long offset)
{
309
	build_addiu_rt_rs(4, 4, offset);	/* $a0, $a0, offset */
Linus Torvalds's avatar
Linus Torvalds committed
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

	store_offset -= offset;
}

static inline void build_bne(unsigned int *dest)
{
	union mips_instruction mi;

	mi.i_format.opcode = bne_op;
	mi.i_format.rs     = 6;			/* $a2 */
	mi.i_format.rt     = 4;			/* $a0 */
	mi.i_format.simmediate = dest - epc - 1;

	*epc++ = mi.word;
	flush_delay_slot_or_nop();
}

static inline void build_jr_ra(void)
{
	union mips_instruction mi;

	mi.r_format.opcode = spec_op;
	mi.r_format.rs     = 31;
	mi.r_format.rt     = 0;
	mi.r_format.rd     = 0;
	mi.r_format.re     = 0;
	mi.r_format.func   = jr_op;

	*epc++ = mi.word;
	flush_delay_slot_or_nop();
}

342
void __cpuinit build_clear_page(void)
Linus Torvalds's avatar
Linus Torvalds committed
343 344
{
	unsigned int loop_start;
345
	unsigned long off;
346
	int i;
Linus Torvalds's avatar
Linus Torvalds committed
347 348 349 350 351 352

	epc = (unsigned int *) &clear_page_array;
	instruction_pending = 0;
	store_offset = 0;

	if (cpu_has_prefetch) {
353
		switch (current_cpu_type()) {
Atsushi Nemoto's avatar
Atsushi Nemoto committed
354 355 356 357 358 359
		case CPU_TX49XX:
			/* TX49 supports only Pref_Load */
			pref_offset_clear = 0;
			pref_offset_copy = 0;
			break;

Linus Torvalds's avatar
Linus Torvalds committed
360 361 362 363 364 365 366 367 368 369 370
		case CPU_RM9000:
			/*
			 * As a workaround for erratum G105 which make the
			 * PrepareForStore hint unusable we fall back to
			 * StoreRetained on the RM9000.  Once it is known which
			 * versions of the RM9000 we'll be able to condition-
			 * alize this.
			 */

		case CPU_R10000:
		case CPU_R12000:
Kumba's avatar
Kumba committed
371
		case CPU_R14000:
Linus Torvalds's avatar
Linus Torvalds committed
372 373 374 375 376 377 378 379 380 381 382
			pref_src_mode = Pref_LoadStreamed;
			pref_dst_mode = Pref_StoreStreamed;
			break;

		default:
			pref_src_mode = Pref_LoadStreamed;
			pref_dst_mode = Pref_PrepareForStore;
			break;
		}
	}

383 384 385 386 387 388
        off = PAGE_SIZE - (cpu_has_prefetch ? pref_offset_clear : 0);
	if (off > 0x7fff) {
		build_addiu_a2_a0(off >> 1);
		build_addiu_a2(off >> 1);
	} else
		build_addiu_a2_a0(off);
Linus Torvalds's avatar
Linus Torvalds committed
389

390
	if (R4600_V2_HIT_CACHEOP_WAR && cpu_is_r4600_v2_x())
Linus Torvalds's avatar
Linus Torvalds committed
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
		build_insn_word(0x3c01a000);	/* lui     $at, 0xa000  */

dest = label();
	do {
		build_store_reg(0);
		build_store_reg(0);
		build_store_reg(0);
		build_store_reg(0);
	} while (store_offset < half_scache_line_size());
	build_addiu_a0(2 * store_offset);
	loop_start = store_offset;
	do {
		build_store_reg(0);
		build_store_reg(0);
		build_store_reg(0);
		build_store_reg(0);
	} while ((store_offset - loop_start) < half_scache_line_size());
	build_bne(dest);

	if (cpu_has_prefetch && pref_offset_clear) {
		build_addiu_a2_a0(pref_offset_clear);
	dest = label();
		loop_start = store_offset;
		do {
			__build_store_reg(0);
			__build_store_reg(0);
			__build_store_reg(0);
			__build_store_reg(0);
		} while ((store_offset - loop_start) < half_scache_line_size());
		build_addiu_a0(2 * store_offset);
		loop_start = store_offset;
		do {
			__build_store_reg(0);
			__build_store_reg(0);
			__build_store_reg(0);
			__build_store_reg(0);
		} while ((store_offset - loop_start) < half_scache_line_size());
		build_bne(dest);
	}

	build_jr_ra();

	BUG_ON(epc > clear_page_array + ARRAY_SIZE(clear_page_array));
434 435 436 437 438 439 440 441 442

	pr_info("Synthesized clear page handler (%u instructions).\n",
		(unsigned int)(epc - clear_page_array));

	pr_debug("\t.set push\n");
	pr_debug("\t.set noreorder\n");
	for (i = 0; i < (epc - clear_page_array); i++)
		pr_debug("\t.word 0x%08x\n", clear_page_array[i]);
	pr_debug("\t.set pop\n");
Linus Torvalds's avatar
Linus Torvalds committed
443 444
}

445
void __cpuinit build_copy_page(void)
Linus Torvalds's avatar
Linus Torvalds committed
446 447
{
	unsigned int loop_start;
448
	unsigned long off;
449
	int i;
Linus Torvalds's avatar
Linus Torvalds committed
450 451 452 453 454

	epc = (unsigned int *) &copy_page_array;
	store_offset = load_offset = 0;
	instruction_pending = 0;

455 456 457 458 459 460
	off = PAGE_SIZE - (cpu_has_prefetch ? pref_offset_copy : 0);
	if (off > 0x7fff) {
		build_addiu_a2_a0(off >> 1);
		build_addiu_a2(off >> 1);
	} else
		build_addiu_a2_a0(off);
Linus Torvalds's avatar
Linus Torvalds committed
461

462
	if (R4600_V2_HIT_CACHEOP_WAR && cpu_is_r4600_v2_x())
Linus Torvalds's avatar
Linus Torvalds committed
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
		build_insn_word(0x3c01a000);	/* lui     $at, 0xa000  */

dest = label();
	loop_start = store_offset;
	do {
		build_load_reg( 8);
		build_load_reg( 9);
		build_load_reg(10);
		build_load_reg(11);
		build_store_reg( 8);
		build_store_reg( 9);
		build_store_reg(10);
		build_store_reg(11);
	} while ((store_offset - loop_start) < half_scache_line_size());
	build_addiu_a0(2 * store_offset);
	build_addiu_a1(2 * load_offset);
	loop_start = store_offset;
	do {
		build_load_reg( 8);
		build_load_reg( 9);
		build_load_reg(10);
		build_load_reg(11);
		build_store_reg( 8);
		build_store_reg( 9);
		build_store_reg(10);
		build_store_reg(11);
	} while ((store_offset - loop_start) < half_scache_line_size());
	build_bne(dest);

	if (cpu_has_prefetch && pref_offset_copy) {
		build_addiu_a2_a0(pref_offset_copy);
	dest = label();
		loop_start = store_offset;
		do {
			__build_load_reg( 8);
			__build_load_reg( 9);
			__build_load_reg(10);
			__build_load_reg(11);
			__build_store_reg( 8);
			__build_store_reg( 9);
			__build_store_reg(10);
			__build_store_reg(11);
		} while ((store_offset - loop_start) < half_scache_line_size());
		build_addiu_a0(2 * store_offset);
		build_addiu_a1(2 * load_offset);
		loop_start = store_offset;
		do {
			__build_load_reg( 8);
			__build_load_reg( 9);
			__build_load_reg(10);
			__build_load_reg(11);
			__build_store_reg( 8);
			__build_store_reg( 9);
			__build_store_reg(10);
			__build_store_reg(11);
		} while ((store_offset - loop_start) < half_scache_line_size());
		build_bne(dest);
	}

	build_jr_ra();

	BUG_ON(epc > copy_page_array + ARRAY_SIZE(copy_page_array));
525 526 527 528 529 530 531 532 533

	pr_info("Synthesized copy page handler (%u instructions).\n",
		(unsigned int)(epc - copy_page_array));

	pr_debug("\t.set push\n");
	pr_debug("\t.set noreorder\n");
	for (i = 0; i < (epc - copy_page_array); i++)
		pr_debug("\t.word 0x%08x\n", copy_page_array[i]);
	pr_debug("\t.set pop\n");
Linus Torvalds's avatar
Linus Torvalds committed
534
}