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)
8 #include <linux/init.h>
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
12 #include <linux/module.h>
13 #include <linux/proc_fs.h>
15 #include <asm/cacheops.h>
19 #include <asm/pgtable.h>
20 #include <asm/prefetch.h>
21 #include <asm/system.h>
22 #include <asm/bootinfo.h>
23 #include <asm/mipsregs.h>
24 #include <asm/mmu_context.h>
28 #define half_scache_line_size() (cpu_scache_line_size() >> 1)
33 * R4000 128 bytes S-cache: 0x58 bytes
34 * R4600 v1.7: 0x5c bytes
35 * R4600 v2.0: 0x60 bytes
36 * With prefetching, 16 byte strides 0xa0 bytes
39 static unsigned int clear_page_array[0x130 / 4];
41 void clear_page(void * page) __attribute__((alias("clear_page_array")));
43 EXPORT_SYMBOL(clear_page);
48 * R4000 128 bytes S-cache: 0x11c bytes
49 * R4600 v1.7: 0x080 bytes
50 * R4600 v2.0: 0x07c bytes
51 * With prefetching, 16 byte strides 0x0b8 bytes
53 static unsigned int copy_page_array[0x148 / 4];
55 void copy_page(void *to, void *from) __attribute__((alias("copy_page_array")));
57 EXPORT_SYMBOL(copy_page);
60 * This is suboptimal for 32-bit kernels; we assume that R10000 is only used
61 * with 64-bit kernels. The prefetch offsets have been experimentally tuned
64 static int pref_offset_clear __initdata = 512;
65 static int pref_offset_copy __initdata = 256;
67 static unsigned int pref_src_mode __initdata;
68 static unsigned int pref_dst_mode __initdata;
70 static int load_offset __initdata;
71 static int store_offset __initdata;
73 static unsigned int __initdata *dest, *epc;
75 static unsigned int instruction_pending;
76 static union mips_instruction delayed_mi;
78 static void __init emit_instruction(union mips_instruction mi)
80 if (instruction_pending)
81 *epc++ = delayed_mi.word;
83 instruction_pending = 1;
87 static inline void flush_delay_slot_or_nop(void)
89 if (instruction_pending) {
90 *epc++ = delayed_mi.word;
91 instruction_pending = 0;
98 static inline unsigned int *label(void)
100 if (instruction_pending) {
101 *epc++ = delayed_mi.word;
102 instruction_pending = 0;
108 static inline void build_insn_word(unsigned int word)
110 union mips_instruction mi;
114 emit_instruction(mi);
117 static inline void build_nop(void)
119 build_insn_word(0); /* nop */
122 static inline void build_src_pref(int advance)
124 if (!(load_offset & (cpu_dcache_line_size() - 1))) {
125 union mips_instruction mi;
127 mi.i_format.opcode = pref_op;
128 mi.i_format.rs = 5; /* $a1 */
129 mi.i_format.rt = pref_src_mode;
130 mi.i_format.simmediate = load_offset + advance;
132 emit_instruction(mi);
136 static inline void __build_load_reg(int reg)
138 union mips_instruction mi;
141 if (cpu_has_64bit_gp_regs) {
142 mi.i_format.opcode = ld_op;
145 mi.i_format.opcode = lw_op;
148 mi.i_format.rs = 5; /* $a1 */
149 mi.i_format.rt = reg; /* $reg */
150 mi.i_format.simmediate = load_offset;
152 load_offset += width;
153 emit_instruction(mi);
156 static inline void build_load_reg(int reg)
158 if (cpu_has_prefetch)
159 build_src_pref(pref_offset_copy);
161 __build_load_reg(reg);
164 static inline void build_dst_pref(int advance)
166 if (!(store_offset & (cpu_dcache_line_size() - 1))) {
167 union mips_instruction mi;
169 mi.i_format.opcode = pref_op;
170 mi.i_format.rs = 4; /* $a0 */
171 mi.i_format.rt = pref_dst_mode;
172 mi.i_format.simmediate = store_offset + advance;
174 emit_instruction(mi);
178 static inline void build_cdex_s(void)
180 union mips_instruction mi;
182 if ((store_offset & (cpu_scache_line_size() - 1)))
185 mi.c_format.opcode = cache_op;
186 mi.c_format.rs = 4; /* $a0 */
187 mi.c_format.c_op = 3; /* Create Dirty Exclusive */
188 mi.c_format.cache = 3; /* Secondary Data Cache */
189 mi.c_format.simmediate = store_offset;
191 emit_instruction(mi);
194 static inline void build_cdex_p(void)
196 union mips_instruction mi;
198 if (store_offset & (cpu_dcache_line_size() - 1))
201 if (R4600_V1_HIT_CACHEOP_WAR && ((read_c0_prid() & 0xfff0) == 0x2010)) {
208 if (R4600_V2_HIT_CACHEOP_WAR && ((read_c0_prid() & 0xfff0) == 0x2020))
209 build_insn_word(0x8c200000); /* lw $zero, ($at) */
211 mi.c_format.opcode = cache_op;
212 mi.c_format.rs = 4; /* $a0 */
213 mi.c_format.c_op = 3; /* Create Dirty Exclusive */
214 mi.c_format.cache = 1; /* Data Cache */
215 mi.c_format.simmediate = store_offset;
217 emit_instruction(mi);
220 static void __init __build_store_reg(int reg)
222 union mips_instruction mi;
225 if (cpu_has_64bit_gp_regs ||
226 (cpu_has_64bit_zero_reg && reg == 0)) {
227 mi.i_format.opcode = sd_op;
230 mi.i_format.opcode = sw_op;
233 mi.i_format.rs = 4; /* $a0 */
234 mi.i_format.rt = reg; /* $reg */
235 mi.i_format.simmediate = store_offset;
237 store_offset += width;
238 emit_instruction(mi);
241 static inline void build_store_reg(int reg)
243 if (cpu_has_prefetch)
245 build_dst_pref(pref_offset_copy);
247 build_dst_pref(pref_offset_clear);
248 else if (cpu_has_cache_cdex_s)
250 else if (cpu_has_cache_cdex_p)
253 __build_store_reg(reg);
256 static inline void build_addiu_a2_a0(unsigned long offset)
258 union mips_instruction mi;
260 BUG_ON(offset > 0x7fff);
262 mi.i_format.opcode = cpu_has_64bit_gp_regs ? daddiu_op : addiu_op;
263 mi.i_format.rs = 4; /* $a0 */
264 mi.i_format.rt = 6; /* $a2 */
265 mi.i_format.simmediate = offset;
267 emit_instruction(mi);
270 static inline void build_addiu_a1(unsigned long offset)
272 union mips_instruction mi;
274 BUG_ON(offset > 0x7fff);
276 mi.i_format.opcode = cpu_has_64bit_gp_regs ? daddiu_op : addiu_op;
277 mi.i_format.rs = 5; /* $a1 */
278 mi.i_format.rt = 5; /* $a1 */
279 mi.i_format.simmediate = offset;
281 load_offset -= offset;
283 emit_instruction(mi);
286 static inline void build_addiu_a0(unsigned long offset)
288 union mips_instruction mi;
290 BUG_ON(offset > 0x7fff);
292 mi.i_format.opcode = cpu_has_64bit_gp_regs ? daddiu_op : addiu_op;
293 mi.i_format.rs = 4; /* $a0 */
294 mi.i_format.rt = 4; /* $a0 */
295 mi.i_format.simmediate = offset;
297 store_offset -= offset;
299 emit_instruction(mi);
302 static inline void build_bne(unsigned int *dest)
304 union mips_instruction mi;
306 mi.i_format.opcode = bne_op;
307 mi.i_format.rs = 6; /* $a2 */
308 mi.i_format.rt = 4; /* $a0 */
309 mi.i_format.simmediate = dest - epc - 1;
312 flush_delay_slot_or_nop();
315 static inline void build_jr_ra(void)
317 union mips_instruction mi;
319 mi.r_format.opcode = spec_op;
324 mi.r_format.func = jr_op;
327 flush_delay_slot_or_nop();
330 void __init build_clear_page(void)
332 unsigned int loop_start;
334 epc = (unsigned int *) &clear_page_array;
335 instruction_pending = 0;
338 if (cpu_has_prefetch) {
339 switch (current_cpu_data.cputype) {
342 * As a workaround for erratum G105 which make the
343 * PrepareForStore hint unusable we fall back to
344 * StoreRetained on the RM9000. Once it is known which
345 * versions of the RM9000 we'll be able to condition-
351 pref_src_mode = Pref_LoadStreamed;
352 pref_dst_mode = Pref_StoreStreamed;
356 pref_src_mode = Pref_LoadStreamed;
357 pref_dst_mode = Pref_PrepareForStore;
362 build_addiu_a2_a0(PAGE_SIZE - (cpu_has_prefetch ? pref_offset_clear : 0));
364 if (R4600_V2_HIT_CACHEOP_WAR && ((read_c0_prid() & 0xfff0) == 0x2020))
365 build_insn_word(0x3c01a000); /* lui $at, 0xa000 */
373 } while (store_offset < half_scache_line_size());
374 build_addiu_a0(2 * store_offset);
375 loop_start = store_offset;
381 } while ((store_offset - loop_start) < half_scache_line_size());
384 if (cpu_has_prefetch && pref_offset_clear) {
385 build_addiu_a2_a0(pref_offset_clear);
387 loop_start = store_offset;
389 __build_store_reg(0);
390 __build_store_reg(0);
391 __build_store_reg(0);
392 __build_store_reg(0);
393 } while ((store_offset - loop_start) < half_scache_line_size());
394 build_addiu_a0(2 * store_offset);
395 loop_start = store_offset;
397 __build_store_reg(0);
398 __build_store_reg(0);
399 __build_store_reg(0);
400 __build_store_reg(0);
401 } while ((store_offset - loop_start) < half_scache_line_size());
407 flush_icache_range((unsigned long)&clear_page_array,
408 (unsigned long) epc);
410 BUG_ON(epc > clear_page_array + ARRAY_SIZE(clear_page_array));
413 void __init build_copy_page(void)
415 unsigned int loop_start;
417 epc = (unsigned int *) ©_page_array;
418 store_offset = load_offset = 0;
419 instruction_pending = 0;
421 build_addiu_a2_a0(PAGE_SIZE - (cpu_has_prefetch ? pref_offset_copy : 0));
423 if (R4600_V2_HIT_CACHEOP_WAR && ((read_c0_prid() & 0xfff0) == 0x2020))
424 build_insn_word(0x3c01a000); /* lui $at, 0xa000 */
427 loop_start = store_offset;
437 } while ((store_offset - loop_start) < half_scache_line_size());
438 build_addiu_a0(2 * store_offset);
439 build_addiu_a1(2 * load_offset);
440 loop_start = store_offset;
450 } while ((store_offset - loop_start) < half_scache_line_size());
453 if (cpu_has_prefetch && pref_offset_copy) {
454 build_addiu_a2_a0(pref_offset_copy);
456 loop_start = store_offset;
458 __build_load_reg( 8);
459 __build_load_reg( 9);
460 __build_load_reg(10);
461 __build_load_reg(11);
462 __build_store_reg( 8);
463 __build_store_reg( 9);
464 __build_store_reg(10);
465 __build_store_reg(11);
466 } while ((store_offset - loop_start) < half_scache_line_size());
467 build_addiu_a0(2 * store_offset);
468 build_addiu_a1(2 * load_offset);
469 loop_start = store_offset;
471 __build_load_reg( 8);
472 __build_load_reg( 9);
473 __build_load_reg(10);
474 __build_load_reg(11);
475 __build_store_reg( 8);
476 __build_store_reg( 9);
477 __build_store_reg(10);
478 __build_store_reg(11);
479 } while ((store_offset - loop_start) < half_scache_line_size());
485 flush_icache_range((unsigned long)©_page_array,
486 (unsigned long) epc);
488 BUG_ON(epc > copy_page_array + ARRAY_SIZE(copy_page_array));