blob: 455dedb5b39e8379703246bc3a158fdafb0ca83e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (C) 2003, 04, 05 Ralf Baechle (ralf@linux-mips.org)
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +01007 * Copyright (C) 2007 Maciej W. Rozycki
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9#include <linux/init.h>
10#include <linux/kernel.h>
11#include <linux/sched.h>
12#include <linux/mm.h>
13#include <linux/module.h>
14#include <linux/proc_fs.h>
15
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +010016#include <asm/bugs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/cacheops.h>
18#include <asm/inst.h>
19#include <asm/io.h>
20#include <asm/page.h>
21#include <asm/pgtable.h>
22#include <asm/prefetch.h>
23#include <asm/system.h>
24#include <asm/bootinfo.h>
25#include <asm/mipsregs.h>
26#include <asm/mmu_context.h>
27#include <asm/cpu.h>
28#include <asm/war.h>
29
Thiemo Seufer330cfe02005-09-01 18:33:58 +000030#define half_scache_line_size() (cpu_scache_line_size() >> 1)
31#define cpu_is_r4600_v1_x() ((read_c0_prid() & 0xfffffff0) == 0x00002010)
32#define cpu_is_r4600_v2_x() ((read_c0_prid() & 0xfffffff0) == 0x00002020)
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35/*
36 * Maximum sizes:
37 *
38 * R4000 128 bytes S-cache: 0x58 bytes
39 * R4600 v1.7: 0x5c bytes
40 * R4600 v2.0: 0x60 bytes
41 * With prefetching, 16 byte strides 0xa0 bytes
42 */
43
44static unsigned int clear_page_array[0x130 / 4];
45
46void clear_page(void * page) __attribute__((alias("clear_page_array")));
47
48EXPORT_SYMBOL(clear_page);
49
50/*
51 * Maximum sizes:
52 *
53 * R4000 128 bytes S-cache: 0x11c bytes
54 * R4600 v1.7: 0x080 bytes
55 * R4600 v2.0: 0x07c bytes
56 * With prefetching, 16 byte strides 0x0b8 bytes
57 */
58static unsigned int copy_page_array[0x148 / 4];
59
60void copy_page(void *to, void *from) __attribute__((alias("copy_page_array")));
61
62EXPORT_SYMBOL(copy_page);
63
64/*
65 * This is suboptimal for 32-bit kernels; we assume that R10000 is only used
66 * with 64-bit kernels. The prefetch offsets have been experimentally tuned
67 * an Origin 200.
68 */
Ralf Baechle234fcd12008-03-08 09:56:28 +000069static int pref_offset_clear __cpuinitdata = 512;
70static int pref_offset_copy __cpuinitdata = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Ralf Baechle234fcd12008-03-08 09:56:28 +000072static unsigned int pref_src_mode __cpuinitdata;
73static unsigned int pref_dst_mode __cpuinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Ralf Baechle234fcd12008-03-08 09:56:28 +000075static int load_offset __cpuinitdata;
76static int store_offset __cpuinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Ralf Baechle234fcd12008-03-08 09:56:28 +000078static unsigned int __cpuinitdata *dest, *epc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80static unsigned int instruction_pending;
81static union mips_instruction delayed_mi;
82
Ralf Baechle234fcd12008-03-08 09:56:28 +000083static void __cpuinit emit_instruction(union mips_instruction mi)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
85 if (instruction_pending)
86 *epc++ = delayed_mi.word;
87
88 instruction_pending = 1;
89 delayed_mi = mi;
90}
91
92static inline void flush_delay_slot_or_nop(void)
93{
94 if (instruction_pending) {
95 *epc++ = delayed_mi.word;
96 instruction_pending = 0;
97 return;
98 }
99
100 *epc++ = 0;
101}
102
103static inline unsigned int *label(void)
104{
105 if (instruction_pending) {
106 *epc++ = delayed_mi.word;
107 instruction_pending = 0;
108 }
109
110 return epc;
111}
112
113static inline void build_insn_word(unsigned int word)
114{
115 union mips_instruction mi;
116
117 mi.word = word;
118
119 emit_instruction(mi);
120}
121
122static inline void build_nop(void)
123{
124 build_insn_word(0); /* nop */
125}
126
127static inline void build_src_pref(int advance)
128{
Atsushi Nemotode862b42006-03-17 12:59:22 +0900129 if (!(load_offset & (cpu_dcache_line_size() - 1)) && advance) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 union mips_instruction mi;
131
132 mi.i_format.opcode = pref_op;
133 mi.i_format.rs = 5; /* $a1 */
134 mi.i_format.rt = pref_src_mode;
135 mi.i_format.simmediate = load_offset + advance;
136
137 emit_instruction(mi);
138 }
139}
140
141static inline void __build_load_reg(int reg)
142{
143 union mips_instruction mi;
144 unsigned int width;
145
146 if (cpu_has_64bit_gp_regs) {
147 mi.i_format.opcode = ld_op;
148 width = 8;
149 } else {
150 mi.i_format.opcode = lw_op;
151 width = 4;
152 }
153 mi.i_format.rs = 5; /* $a1 */
154 mi.i_format.rt = reg; /* $reg */
155 mi.i_format.simmediate = load_offset;
156
157 load_offset += width;
158 emit_instruction(mi);
159}
160
161static inline void build_load_reg(int reg)
162{
163 if (cpu_has_prefetch)
164 build_src_pref(pref_offset_copy);
165
166 __build_load_reg(reg);
167}
168
169static inline void build_dst_pref(int advance)
170{
Atsushi Nemotode862b42006-03-17 12:59:22 +0900171 if (!(store_offset & (cpu_dcache_line_size() - 1)) && advance) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 union mips_instruction mi;
173
174 mi.i_format.opcode = pref_op;
175 mi.i_format.rs = 4; /* $a0 */
176 mi.i_format.rt = pref_dst_mode;
177 mi.i_format.simmediate = store_offset + advance;
178
179 emit_instruction(mi);
180 }
181}
182
183static inline void build_cdex_s(void)
184{
185 union mips_instruction mi;
186
187 if ((store_offset & (cpu_scache_line_size() - 1)))
188 return;
189
190 mi.c_format.opcode = cache_op;
191 mi.c_format.rs = 4; /* $a0 */
192 mi.c_format.c_op = 3; /* Create Dirty Exclusive */
193 mi.c_format.cache = 3; /* Secondary Data Cache */
194 mi.c_format.simmediate = store_offset;
195
196 emit_instruction(mi);
197}
198
199static inline void build_cdex_p(void)
200{
201 union mips_instruction mi;
202
203 if (store_offset & (cpu_dcache_line_size() - 1))
204 return;
205
Thiemo Seufer330cfe02005-09-01 18:33:58 +0000206 if (R4600_V1_HIT_CACHEOP_WAR && cpu_is_r4600_v1_x()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 build_nop();
208 build_nop();
209 build_nop();
210 build_nop();
211 }
212
Thiemo Seufer330cfe02005-09-01 18:33:58 +0000213 if (R4600_V2_HIT_CACHEOP_WAR && cpu_is_r4600_v2_x())
Maciej W. Rozyckif6a9e6d2007-10-02 14:47:22 +0100214 build_insn_word(0x8c200000); /* lw $zero, ($at) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 mi.c_format.opcode = cache_op;
217 mi.c_format.rs = 4; /* $a0 */
218 mi.c_format.c_op = 3; /* Create Dirty Exclusive */
219 mi.c_format.cache = 1; /* Data Cache */
220 mi.c_format.simmediate = store_offset;
221
222 emit_instruction(mi);
223}
224
Ralf Baechle234fcd12008-03-08 09:56:28 +0000225static void __cpuinit __build_store_reg(int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 union mips_instruction mi;
228 unsigned int width;
229
230 if (cpu_has_64bit_gp_regs ||
231 (cpu_has_64bit_zero_reg && reg == 0)) {
232 mi.i_format.opcode = sd_op;
233 width = 8;
234 } else {
235 mi.i_format.opcode = sw_op;
236 width = 4;
237 }
238 mi.i_format.rs = 4; /* $a0 */
239 mi.i_format.rt = reg; /* $reg */
240 mi.i_format.simmediate = store_offset;
241
242 store_offset += width;
243 emit_instruction(mi);
244}
245
246static inline void build_store_reg(int reg)
247{
Atsushi Nemoto33b06b52006-12-18 00:38:21 +0900248 int pref_off = cpu_has_prefetch ?
249 (reg ? pref_offset_copy : pref_offset_clear) : 0;
250 if (pref_off)
251 build_dst_pref(pref_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 else if (cpu_has_cache_cdex_s)
253 build_cdex_s();
254 else if (cpu_has_cache_cdex_p)
255 build_cdex_p();
256
257 __build_store_reg(reg);
258}
259
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100260static inline void build_addiu_rt_rs(unsigned int rt, unsigned int rs,
261 unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 union mips_instruction mi;
264
265 BUG_ON(offset > 0x7fff);
266
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100267 if (cpu_has_64bit_gp_regs && DADDI_WAR && r4k_daddiu_bug()) {
268 mi.i_format.opcode = addiu_op;
269 mi.i_format.rs = 0; /* $zero */
270 mi.i_format.rt = 25; /* $t9 */
271 mi.i_format.simmediate = offset;
272 emit_instruction(mi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100274 mi.r_format.opcode = spec_op;
275 mi.r_format.rs = rs;
276 mi.r_format.rt = 25; /* $t9 */
277 mi.r_format.rd = rt;
278 mi.r_format.re = 0;
279 mi.r_format.func = daddu_op;
280 } else {
281 mi.i_format.opcode = cpu_has_64bit_gp_regs ?
282 daddiu_op : addiu_op;
283 mi.i_format.rs = rs;
284 mi.i_format.rt = rt;
285 mi.i_format.simmediate = offset;
286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 emit_instruction(mi);
288}
289
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100290static inline void build_addiu_a2_a0(unsigned long offset)
291{
292 build_addiu_rt_rs(6, 4, offset); /* $a2, $a0, offset */
293}
294
Ralf Baechle242954b2006-10-24 02:29:01 +0100295static inline void build_addiu_a2(unsigned long offset)
296{
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100297 build_addiu_rt_rs(6, 6, offset); /* $a2, $a2, offset */
Ralf Baechle242954b2006-10-24 02:29:01 +0100298}
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300static inline void build_addiu_a1(unsigned long offset)
301{
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100302 build_addiu_rt_rs(5, 5, offset); /* $a1, $a1, offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 load_offset -= offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
307static inline void build_addiu_a0(unsigned long offset)
308{
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100309 build_addiu_rt_rs(4, 4, offset); /* $a0, $a0, offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 store_offset -= offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
314static inline void build_bne(unsigned int *dest)
315{
316 union mips_instruction mi;
317
318 mi.i_format.opcode = bne_op;
319 mi.i_format.rs = 6; /* $a2 */
320 mi.i_format.rt = 4; /* $a0 */
321 mi.i_format.simmediate = dest - epc - 1;
322
323 *epc++ = mi.word;
324 flush_delay_slot_or_nop();
325}
326
327static inline void build_jr_ra(void)
328{
329 union mips_instruction mi;
330
331 mi.r_format.opcode = spec_op;
332 mi.r_format.rs = 31;
333 mi.r_format.rt = 0;
334 mi.r_format.rd = 0;
335 mi.r_format.re = 0;
336 mi.r_format.func = jr_op;
337
338 *epc++ = mi.word;
339 flush_delay_slot_or_nop();
340}
341
Ralf Baechle234fcd12008-03-08 09:56:28 +0000342void __cpuinit build_clear_page(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 unsigned int loop_start;
Ralf Baechle242954b2006-10-24 02:29:01 +0100345 unsigned long off;
Maciej W. Rozycki1ac74d52007-10-02 14:54:15 +0100346 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 epc = (unsigned int *) &clear_page_array;
349 instruction_pending = 0;
350 store_offset = 0;
351
352 if (cpu_has_prefetch) {
Ralf Baechle10cc3522007-10-11 23:46:15 +0100353 switch (current_cpu_type()) {
Atsushi Nemotode862b42006-03-17 12:59:22 +0900354 case CPU_TX49XX:
355 /* TX49 supports only Pref_Load */
356 pref_offset_clear = 0;
357 pref_offset_copy = 0;
358 break;
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 case CPU_RM9000:
361 /*
362 * As a workaround for erratum G105 which make the
363 * PrepareForStore hint unusable we fall back to
364 * StoreRetained on the RM9000. Once it is known which
365 * versions of the RM9000 we'll be able to condition-
366 * alize this.
367 */
368
369 case CPU_R10000:
370 case CPU_R12000:
Kumba44d921b2006-05-16 22:23:59 -0400371 case CPU_R14000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 pref_src_mode = Pref_LoadStreamed;
373 pref_dst_mode = Pref_StoreStreamed;
374 break;
375
376 default:
377 pref_src_mode = Pref_LoadStreamed;
378 pref_dst_mode = Pref_PrepareForStore;
379 break;
380 }
381 }
382
Ralf Baechle242954b2006-10-24 02:29:01 +0100383 off = PAGE_SIZE - (cpu_has_prefetch ? pref_offset_clear : 0);
384 if (off > 0x7fff) {
385 build_addiu_a2_a0(off >> 1);
386 build_addiu_a2(off >> 1);
387 } else
388 build_addiu_a2_a0(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Thiemo Seufer330cfe02005-09-01 18:33:58 +0000390 if (R4600_V2_HIT_CACHEOP_WAR && cpu_is_r4600_v2_x())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 build_insn_word(0x3c01a000); /* lui $at, 0xa000 */
392
393dest = label();
394 do {
395 build_store_reg(0);
396 build_store_reg(0);
397 build_store_reg(0);
398 build_store_reg(0);
399 } while (store_offset < half_scache_line_size());
400 build_addiu_a0(2 * store_offset);
401 loop_start = store_offset;
402 do {
403 build_store_reg(0);
404 build_store_reg(0);
405 build_store_reg(0);
406 build_store_reg(0);
407 } while ((store_offset - loop_start) < half_scache_line_size());
408 build_bne(dest);
409
410 if (cpu_has_prefetch && pref_offset_clear) {
411 build_addiu_a2_a0(pref_offset_clear);
412 dest = label();
413 loop_start = store_offset;
414 do {
415 __build_store_reg(0);
416 __build_store_reg(0);
417 __build_store_reg(0);
418 __build_store_reg(0);
419 } while ((store_offset - loop_start) < half_scache_line_size());
420 build_addiu_a0(2 * store_offset);
421 loop_start = store_offset;
422 do {
423 __build_store_reg(0);
424 __build_store_reg(0);
425 __build_store_reg(0);
426 __build_store_reg(0);
427 } while ((store_offset - loop_start) < half_scache_line_size());
428 build_bne(dest);
429 }
430
431 build_jr_ra();
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 BUG_ON(epc > clear_page_array + ARRAY_SIZE(clear_page_array));
Maciej W. Rozycki1ac74d52007-10-02 14:54:15 +0100434
435 pr_info("Synthesized clear page handler (%u instructions).\n",
436 (unsigned int)(epc - clear_page_array));
437
438 pr_debug("\t.set push\n");
439 pr_debug("\t.set noreorder\n");
440 for (i = 0; i < (epc - clear_page_array); i++)
441 pr_debug("\t.word 0x%08x\n", clear_page_array[i]);
442 pr_debug("\t.set pop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
Ralf Baechle234fcd12008-03-08 09:56:28 +0000445void __cpuinit build_copy_page(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 unsigned int loop_start;
Ralf Baechle242954b2006-10-24 02:29:01 +0100448 unsigned long off;
Maciej W. Rozycki1ac74d52007-10-02 14:54:15 +0100449 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 epc = (unsigned int *) &copy_page_array;
452 store_offset = load_offset = 0;
453 instruction_pending = 0;
454
Ralf Baechle242954b2006-10-24 02:29:01 +0100455 off = PAGE_SIZE - (cpu_has_prefetch ? pref_offset_copy : 0);
456 if (off > 0x7fff) {
457 build_addiu_a2_a0(off >> 1);
458 build_addiu_a2(off >> 1);
459 } else
460 build_addiu_a2_a0(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Thiemo Seufer330cfe02005-09-01 18:33:58 +0000462 if (R4600_V2_HIT_CACHEOP_WAR && cpu_is_r4600_v2_x())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 build_insn_word(0x3c01a000); /* lui $at, 0xa000 */
464
465dest = label();
466 loop_start = store_offset;
467 do {
468 build_load_reg( 8);
469 build_load_reg( 9);
470 build_load_reg(10);
471 build_load_reg(11);
472 build_store_reg( 8);
473 build_store_reg( 9);
474 build_store_reg(10);
475 build_store_reg(11);
476 } while ((store_offset - loop_start) < half_scache_line_size());
477 build_addiu_a0(2 * store_offset);
478 build_addiu_a1(2 * load_offset);
479 loop_start = store_offset;
480 do {
481 build_load_reg( 8);
482 build_load_reg( 9);
483 build_load_reg(10);
484 build_load_reg(11);
485 build_store_reg( 8);
486 build_store_reg( 9);
487 build_store_reg(10);
488 build_store_reg(11);
489 } while ((store_offset - loop_start) < half_scache_line_size());
490 build_bne(dest);
491
492 if (cpu_has_prefetch && pref_offset_copy) {
493 build_addiu_a2_a0(pref_offset_copy);
494 dest = label();
495 loop_start = store_offset;
496 do {
497 __build_load_reg( 8);
498 __build_load_reg( 9);
499 __build_load_reg(10);
500 __build_load_reg(11);
501 __build_store_reg( 8);
502 __build_store_reg( 9);
503 __build_store_reg(10);
504 __build_store_reg(11);
505 } while ((store_offset - loop_start) < half_scache_line_size());
506 build_addiu_a0(2 * store_offset);
507 build_addiu_a1(2 * load_offset);
508 loop_start = store_offset;
509 do {
510 __build_load_reg( 8);
511 __build_load_reg( 9);
512 __build_load_reg(10);
513 __build_load_reg(11);
514 __build_store_reg( 8);
515 __build_store_reg( 9);
516 __build_store_reg(10);
517 __build_store_reg(11);
518 } while ((store_offset - loop_start) < half_scache_line_size());
519 build_bne(dest);
520 }
521
522 build_jr_ra();
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 BUG_ON(epc > copy_page_array + ARRAY_SIZE(copy_page_array));
Maciej W. Rozycki1ac74d52007-10-02 14:54:15 +0100525
526 pr_info("Synthesized copy page handler (%u instructions).\n",
527 (unsigned int)(epc - copy_page_array));
528
529 pr_debug("\t.set push\n");
530 pr_debug("\t.set noreorder\n");
531 for (i = 0; i < (epc - copy_page_array); i++)
532 pr_debug("\t.word 0x%08x\n", copy_page_array[i]);
533 pr_debug("\t.set pop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}