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
6 * Copyright (C) 2003, 04, 05 Ralf Baechle (ralf@linux-mips.org)
7 * Copyright (C) 2007 Maciej W. Rozycki
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
13 #include <linux/module.h>
14 #include <linux/proc_fs.h>
17 #include <asm/cacheops.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>
30 #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)
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
44 static unsigned int clear_page_array
[0x130 / 4];
46 void clear_page(void * page
) __attribute__((alias("clear_page_array")));
48 EXPORT_SYMBOL(clear_page
);
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
58 static unsigned int copy_page_array
[0x148 / 4];
60 void copy_page(void *to
, void *from
) __attribute__((alias("copy_page_array")));
62 EXPORT_SYMBOL(copy_page
);
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
69 <<<<<<< HEAD
:arch
/mips
/mm
/pg
-r4k
.c
70 static int pref_offset_clear __initdata
= 512;
71 static int pref_offset_copy __initdata
= 256;
73 static int pref_offset_clear __cpuinitdata
= 512;
74 static int pref_offset_copy __cpuinitdata
= 256;
75 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/mips
/mm
/pg
-r4k
.c
77 <<<<<<< HEAD
:arch
/mips
/mm
/pg
-r4k
.c
78 static unsigned int pref_src_mode __initdata
;
79 static unsigned int pref_dst_mode __initdata
;
81 static unsigned int pref_src_mode __cpuinitdata
;
82 static unsigned int pref_dst_mode __cpuinitdata
;
83 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/mips
/mm
/pg
-r4k
.c
85 <<<<<<< HEAD
:arch
/mips
/mm
/pg
-r4k
.c
86 static int load_offset __initdata
;
87 static int store_offset __initdata
;
89 static int load_offset __cpuinitdata
;
90 static int store_offset __cpuinitdata
;
91 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/mips
/mm
/pg
-r4k
.c
93 <<<<<<< HEAD
:arch
/mips
/mm
/pg
-r4k
.c
94 static unsigned int __initdata
*dest
, *epc
;
96 static unsigned int __cpuinitdata
*dest
, *epc
;
97 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/mips
/mm
/pg
-r4k
.c
99 static unsigned int instruction_pending
;
100 static union mips_instruction delayed_mi
;
102 <<<<<<< HEAD
:arch
/mips
/mm
/pg
-r4k
.c
103 static void __init
emit_instruction(union mips_instruction mi
)
105 static void __cpuinit
emit_instruction(union mips_instruction mi
)
106 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/mips
/mm
/pg
-r4k
.c
108 if (instruction_pending
)
109 *epc
++ = delayed_mi
.word
;
111 instruction_pending
= 1;
115 static inline void flush_delay_slot_or_nop(void)
117 if (instruction_pending
) {
118 *epc
++ = delayed_mi
.word
;
119 instruction_pending
= 0;
126 static inline unsigned int *label(void)
128 if (instruction_pending
) {
129 *epc
++ = delayed_mi
.word
;
130 instruction_pending
= 0;
136 static inline void build_insn_word(unsigned int word
)
138 union mips_instruction mi
;
142 emit_instruction(mi
);
145 static inline void build_nop(void)
147 build_insn_word(0); /* nop */
150 static inline void build_src_pref(int advance
)
152 if (!(load_offset
& (cpu_dcache_line_size() - 1)) && advance
) {
153 union mips_instruction mi
;
155 mi
.i_format
.opcode
= pref_op
;
156 mi
.i_format
.rs
= 5; /* $a1 */
157 mi
.i_format
.rt
= pref_src_mode
;
158 mi
.i_format
.simmediate
= load_offset
+ advance
;
160 emit_instruction(mi
);
164 static inline void __build_load_reg(int reg
)
166 union mips_instruction mi
;
169 if (cpu_has_64bit_gp_regs
) {
170 mi
.i_format
.opcode
= ld_op
;
173 mi
.i_format
.opcode
= lw_op
;
176 mi
.i_format
.rs
= 5; /* $a1 */
177 mi
.i_format
.rt
= reg
; /* $reg */
178 mi
.i_format
.simmediate
= load_offset
;
180 load_offset
+= width
;
181 emit_instruction(mi
);
184 static inline void build_load_reg(int reg
)
186 if (cpu_has_prefetch
)
187 build_src_pref(pref_offset_copy
);
189 __build_load_reg(reg
);
192 static inline void build_dst_pref(int advance
)
194 if (!(store_offset
& (cpu_dcache_line_size() - 1)) && advance
) {
195 union mips_instruction mi
;
197 mi
.i_format
.opcode
= pref_op
;
198 mi
.i_format
.rs
= 4; /* $a0 */
199 mi
.i_format
.rt
= pref_dst_mode
;
200 mi
.i_format
.simmediate
= store_offset
+ advance
;
202 emit_instruction(mi
);
206 static inline void build_cdex_s(void)
208 union mips_instruction mi
;
210 if ((store_offset
& (cpu_scache_line_size() - 1)))
213 mi
.c_format
.opcode
= cache_op
;
214 mi
.c_format
.rs
= 4; /* $a0 */
215 mi
.c_format
.c_op
= 3; /* Create Dirty Exclusive */
216 mi
.c_format
.cache
= 3; /* Secondary Data Cache */
217 mi
.c_format
.simmediate
= store_offset
;
219 emit_instruction(mi
);
222 static inline void build_cdex_p(void)
224 union mips_instruction mi
;
226 if (store_offset
& (cpu_dcache_line_size() - 1))
229 if (R4600_V1_HIT_CACHEOP_WAR
&& cpu_is_r4600_v1_x()) {
236 if (R4600_V2_HIT_CACHEOP_WAR
&& cpu_is_r4600_v2_x())
237 build_insn_word(0x8c200000); /* lw $zero, ($at) */
239 mi
.c_format
.opcode
= cache_op
;
240 mi
.c_format
.rs
= 4; /* $a0 */
241 mi
.c_format
.c_op
= 3; /* Create Dirty Exclusive */
242 mi
.c_format
.cache
= 1; /* Data Cache */
243 mi
.c_format
.simmediate
= store_offset
;
245 emit_instruction(mi
);
248 <<<<<<< HEAD
:arch
/mips
/mm
/pg
-r4k
.c
249 static void __init
__build_store_reg(int reg
)
251 static void __cpuinit
__build_store_reg(int reg
)
252 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/mips
/mm
/pg
-r4k
.c
254 union mips_instruction mi
;
257 if (cpu_has_64bit_gp_regs
||
258 (cpu_has_64bit_zero_reg
&& reg
== 0)) {
259 mi
.i_format
.opcode
= sd_op
;
262 mi
.i_format
.opcode
= sw_op
;
265 mi
.i_format
.rs
= 4; /* $a0 */
266 mi
.i_format
.rt
= reg
; /* $reg */
267 mi
.i_format
.simmediate
= store_offset
;
269 store_offset
+= width
;
270 emit_instruction(mi
);
273 static inline void build_store_reg(int reg
)
275 int pref_off
= cpu_has_prefetch
?
276 (reg
? pref_offset_copy
: pref_offset_clear
) : 0;
278 build_dst_pref(pref_off
);
279 else if (cpu_has_cache_cdex_s
)
281 else if (cpu_has_cache_cdex_p
)
284 __build_store_reg(reg
);
287 static inline void build_addiu_rt_rs(unsigned int rt
, unsigned int rs
,
288 unsigned long offset
)
290 union mips_instruction mi
;
292 BUG_ON(offset
> 0x7fff);
294 if (cpu_has_64bit_gp_regs
&& DADDI_WAR
&& r4k_daddiu_bug()) {
295 mi
.i_format
.opcode
= addiu_op
;
296 mi
.i_format
.rs
= 0; /* $zero */
297 mi
.i_format
.rt
= 25; /* $t9 */
298 mi
.i_format
.simmediate
= offset
;
299 emit_instruction(mi
);
301 mi
.r_format
.opcode
= spec_op
;
303 mi
.r_format
.rt
= 25; /* $t9 */
306 mi
.r_format
.func
= daddu_op
;
308 mi
.i_format
.opcode
= cpu_has_64bit_gp_regs
?
309 daddiu_op
: addiu_op
;
312 mi
.i_format
.simmediate
= offset
;
314 emit_instruction(mi
);
317 static inline void build_addiu_a2_a0(unsigned long offset
)
319 build_addiu_rt_rs(6, 4, offset
); /* $a2, $a0, offset */
322 static inline void build_addiu_a2(unsigned long offset
)
324 build_addiu_rt_rs(6, 6, offset
); /* $a2, $a2, offset */
327 static inline void build_addiu_a1(unsigned long offset
)
329 build_addiu_rt_rs(5, 5, offset
); /* $a1, $a1, offset */
331 load_offset
-= offset
;
334 static inline void build_addiu_a0(unsigned long offset
)
336 build_addiu_rt_rs(4, 4, offset
); /* $a0, $a0, offset */
338 store_offset
-= offset
;
341 static inline void build_bne(unsigned int *dest
)
343 union mips_instruction mi
;
345 mi
.i_format
.opcode
= bne_op
;
346 mi
.i_format
.rs
= 6; /* $a2 */
347 mi
.i_format
.rt
= 4; /* $a0 */
348 mi
.i_format
.simmediate
= dest
- epc
- 1;
351 flush_delay_slot_or_nop();
354 static inline void build_jr_ra(void)
356 union mips_instruction mi
;
358 mi
.r_format
.opcode
= spec_op
;
363 mi
.r_format
.func
= jr_op
;
366 flush_delay_slot_or_nop();
369 <<<<<<< HEAD
:arch
/mips
/mm
/pg
-r4k
.c
370 void __init
build_clear_page(void)
372 void __cpuinit
build_clear_page(void)
373 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/mips
/mm
/pg
-r4k
.c
375 unsigned int loop_start
;
379 epc
= (unsigned int *) &clear_page_array
;
380 instruction_pending
= 0;
383 if (cpu_has_prefetch
) {
384 switch (current_cpu_type()) {
386 /* TX49 supports only Pref_Load */
387 pref_offset_clear
= 0;
388 pref_offset_copy
= 0;
393 * As a workaround for erratum G105 which make the
394 * PrepareForStore hint unusable we fall back to
395 * StoreRetained on the RM9000. Once it is known which
396 * versions of the RM9000 we'll be able to condition-
403 pref_src_mode
= Pref_LoadStreamed
;
404 pref_dst_mode
= Pref_StoreStreamed
;
408 pref_src_mode
= Pref_LoadStreamed
;
409 pref_dst_mode
= Pref_PrepareForStore
;
414 off
= PAGE_SIZE
- (cpu_has_prefetch
? pref_offset_clear
: 0);
416 build_addiu_a2_a0(off
>> 1);
417 build_addiu_a2(off
>> 1);
419 build_addiu_a2_a0(off
);
421 if (R4600_V2_HIT_CACHEOP_WAR
&& cpu_is_r4600_v2_x())
422 build_insn_word(0x3c01a000); /* lui $at, 0xa000 */
430 } while (store_offset
< half_scache_line_size());
431 build_addiu_a0(2 * store_offset
);
432 loop_start
= store_offset
;
438 } while ((store_offset
- loop_start
) < half_scache_line_size());
441 if (cpu_has_prefetch
&& pref_offset_clear
) {
442 build_addiu_a2_a0(pref_offset_clear
);
444 loop_start
= store_offset
;
446 __build_store_reg(0);
447 __build_store_reg(0);
448 __build_store_reg(0);
449 __build_store_reg(0);
450 } while ((store_offset
- loop_start
) < half_scache_line_size());
451 build_addiu_a0(2 * store_offset
);
452 loop_start
= store_offset
;
454 __build_store_reg(0);
455 __build_store_reg(0);
456 __build_store_reg(0);
457 __build_store_reg(0);
458 } while ((store_offset
- loop_start
) < half_scache_line_size());
464 BUG_ON(epc
> clear_page_array
+ ARRAY_SIZE(clear_page_array
));
466 pr_info("Synthesized clear page handler (%u instructions).\n",
467 (unsigned int)(epc
- clear_page_array
));
469 pr_debug("\t.set push\n");
470 pr_debug("\t.set noreorder\n");
471 for (i
= 0; i
< (epc
- clear_page_array
); i
++)
472 pr_debug("\t.word 0x%08x\n", clear_page_array
[i
]);
473 pr_debug("\t.set pop\n");
476 <<<<<<< HEAD
:arch
/mips
/mm
/pg
-r4k
.c
477 void __init
build_copy_page(void)
479 void __cpuinit
build_copy_page(void)
480 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/mips
/mm
/pg
-r4k
.c
482 unsigned int loop_start
;
486 epc
= (unsigned int *) ©_page_array
;
487 store_offset
= load_offset
= 0;
488 instruction_pending
= 0;
490 off
= PAGE_SIZE
- (cpu_has_prefetch
? pref_offset_copy
: 0);
492 build_addiu_a2_a0(off
>> 1);
493 build_addiu_a2(off
>> 1);
495 build_addiu_a2_a0(off
);
497 if (R4600_V2_HIT_CACHEOP_WAR
&& cpu_is_r4600_v2_x())
498 build_insn_word(0x3c01a000); /* lui $at, 0xa000 */
501 loop_start
= store_offset
;
511 } while ((store_offset
- loop_start
) < half_scache_line_size());
512 build_addiu_a0(2 * store_offset
);
513 build_addiu_a1(2 * load_offset
);
514 loop_start
= store_offset
;
524 } while ((store_offset
- loop_start
) < half_scache_line_size());
527 if (cpu_has_prefetch
&& pref_offset_copy
) {
528 build_addiu_a2_a0(pref_offset_copy
);
530 loop_start
= store_offset
;
532 __build_load_reg( 8);
533 __build_load_reg( 9);
534 __build_load_reg(10);
535 __build_load_reg(11);
536 __build_store_reg( 8);
537 __build_store_reg( 9);
538 __build_store_reg(10);
539 __build_store_reg(11);
540 } while ((store_offset
- loop_start
) < half_scache_line_size());
541 build_addiu_a0(2 * store_offset
);
542 build_addiu_a1(2 * load_offset
);
543 loop_start
= store_offset
;
545 __build_load_reg( 8);
546 __build_load_reg( 9);
547 __build_load_reg(10);
548 __build_load_reg(11);
549 __build_store_reg( 8);
550 __build_store_reg( 9);
551 __build_store_reg(10);
552 __build_store_reg(11);
553 } while ((store_offset
- loop_start
) < half_scache_line_size());
559 BUG_ON(epc
> copy_page_array
+ ARRAY_SIZE(copy_page_array
));
561 pr_info("Synthesized copy page handler (%u instructions).\n",
562 (unsigned int)(epc
- copy_page_array
));
564 pr_debug("\t.set push\n");
565 pr_debug("\t.set noreorder\n");
566 for (i
= 0; i
< (epc
- copy_page_array
); i
++)
567 pr_debug("\t.word 0x%08x\n", copy_page_array
[i
]);
568 pr_debug("\t.set pop\n");