ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / mips / mm / pg-r4k.c
1 /*
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, 2004 Ralf Baechle (ralf@linux-mips.org)
7  */
8 #include <linux/config.h>
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
16 #include <asm/cacheops.h>
17 #include <asm/inst.h>
18 #include <asm/io.h>
19 #include <asm/page.h>
20 #include <asm/pgtable.h>
21 #include <asm/prefetch.h>
22 #include <asm/system.h>
23 #include <asm/bootinfo.h>
24 #include <asm/mipsregs.h>
25 #include <asm/mmu_context.h>
26 #include <asm/cpu.h>
27 #include <asm/war.h>
28
29 #define half_scache_line_size()         (cpu_scache_line_size() >> 1)
30
31 /*
32  * Maximum sizes:
33  *
34  * R4000 128 bytes S-cache:             0x58 bytes
35  * R4600 v1.7:                          0x5c bytes
36  * R4600 v2.0:                          0x60 bytes
37  * With prefetching, 16 byte strides    0xa0 bytes
38  */
39
40 static unsigned int clear_page_array[0x130 / 4];
41
42 void clear_page(void * page) __attribute__((alias("clear_page_array")));
43
44 EXPORT_SYMBOL(clear_page);
45
46 /*
47  * Maximum sizes:
48  *
49  * R4000 128 bytes S-cache:             0x11c bytes
50  * R4600 v1.7:                          0x080 bytes
51  * R4600 v2.0:                          0x07c bytes
52  * With prefetching, 16 byte strides    0x0b8 bytes
53  */
54 static unsigned int copy_page_array[0x148 / 4];
55
56 void copy_page(void *to, void *from) __attribute__((alias("copy_page_array")));
57
58 EXPORT_SYMBOL(copy_page);
59
60 /*
61  * An address fits into a single register so it's safe to use 64-bit registers
62  * if we have 64-bit adresses.
63  */
64 #define cpu_has_64bit_registers cpu_has_64bit_addresses
65
66 /*
67  * This is suboptimal for 32-bit kernels; we assume that R10000 is only used
68  * with 64-bit kernels.  The prefetch offsets have been experimentally tuned
69  * an Origin 200.
70  */
71 static int pref_offset_clear __initdata = 512;
72 static int pref_offset_copy  __initdata = 256;
73
74 static unsigned int pref_src_mode __initdata;
75 static unsigned int pref_dst_mode __initdata;
76
77 static int load_offset __initdata;
78 static int store_offset __initdata;
79
80 static unsigned int __initdata *dest, *epc;
81
82 static unsigned int instruction_pending;
83 static union mips_instruction delayed_mi;
84
85 static void __init emit_instruction(union mips_instruction mi)
86 {
87         if (instruction_pending)
88                 *epc++ = delayed_mi.word;
89
90         instruction_pending = 1;
91         delayed_mi = mi;
92 }
93
94 static inline void flush_delay_slot_or_nop(void)
95 {
96         if (instruction_pending) {
97                 *epc++ = delayed_mi.word;
98                 instruction_pending = 0;
99                 return;
100         }
101
102         *epc++ = 0;
103 }
104
105 static inline unsigned int *label(void)
106 {
107         if (instruction_pending) {
108                 *epc++ = delayed_mi.word;
109                 instruction_pending = 0;
110         }
111
112         return epc;
113 }
114
115 static inline void build_insn_word(unsigned int word)
116 {
117         union mips_instruction mi;
118
119         mi.word          = word;
120
121         emit_instruction(mi);
122 }
123
124 static inline void build_nop(void)
125 {
126         build_insn_word(0);                     /* nop */
127 }
128
129 static inline void build_src_pref(int advance)
130 {
131         if (!(load_offset & (cpu_dcache_line_size() - 1))) {
132                 union mips_instruction mi;
133
134                 mi.i_format.opcode     = pref_op;
135                 mi.i_format.rs         = 5;             /* $a1 */
136                 mi.i_format.rt         = pref_src_mode;
137                 mi.i_format.simmediate = load_offset + advance;
138
139                 emit_instruction(mi);
140         }
141 }
142
143 static inline void __build_load_reg(int reg)
144 {
145         union mips_instruction mi;
146         unsigned int width;
147
148         if (cpu_has_64bit_registers) {
149                 mi.i_format.opcode     = ld_op;
150                 width = 8;
151         } else {
152                 mi.i_format.opcode     = lw_op;
153                 width = 4;
154         }
155         mi.i_format.rs         = 5;             /* $a1 */
156         mi.i_format.rt         = reg;           /* $reg */
157         mi.i_format.simmediate = load_offset;
158
159         load_offset += width;
160         emit_instruction(mi);
161 }
162
163 static inline void build_load_reg(int reg)
164 {
165         if (cpu_has_prefetch)
166                 build_src_pref(pref_offset_copy);
167
168         __build_load_reg(reg);
169 }
170
171 static inline void build_dst_pref(int advance)
172 {
173         if (!(store_offset & (cpu_dcache_line_size() - 1))) {
174                 union mips_instruction mi;
175
176                 mi.i_format.opcode     = pref_op;
177                 mi.i_format.rs         = 4;             /* $a0 */
178                 mi.i_format.rt         = pref_dst_mode;
179                 mi.i_format.simmediate = store_offset + advance;
180
181                 emit_instruction(mi);
182         }
183 }
184
185 static inline void build_cdex_s(void)
186 {
187         union mips_instruction mi;
188
189         if ((store_offset & (cpu_scache_line_size() - 1)))
190                 return;
191
192         mi.c_format.opcode     = cache_op;
193         mi.c_format.rs         = 4;             /* $a0 */
194         mi.c_format.c_op       = 3;             /* Create Dirty Exclusive */
195         mi.c_format.cache      = 3;             /* Secondary Data Cache */
196         mi.c_format.simmediate = store_offset;
197
198         emit_instruction(mi);
199 }
200
201 static inline void build_cdex_p(void)
202 {
203         union mips_instruction mi;
204
205         if (store_offset & (cpu_dcache_line_size() - 1))
206                 return;
207
208         if (R4600_V1_HIT_CACHEOP_WAR && ((read_c0_prid() & 0xfff0) == 0x2010)) {
209                 build_nop();
210                 build_nop();
211                 build_nop();
212                 build_nop();
213         }
214
215         if (R4600_V2_HIT_CACHEOP_WAR && ((read_c0_prid() & 0xfff0) == 0x2020))
216                 build_insn_word(0x8c200000);    /* lw      $zero, ($at) */
217
218         mi.c_format.opcode     = cache_op;
219         mi.c_format.rs         = 4;             /* $a0 */
220         mi.c_format.c_op       = 3;             /* Create Dirty Exclusive */
221         mi.c_format.cache      = 1;             /* Data Cache */
222         mi.c_format.simmediate = store_offset;
223
224         emit_instruction(mi);
225 }
226
227 static void __build_store_reg(int reg)
228 {
229         union mips_instruction mi;
230         unsigned int width;
231
232         if (cpu_has_64bit_gp_regs ||
233             (cpu_has_64bit_zero_reg && reg == 0)) {
234                 mi.i_format.opcode     = sd_op;
235                 width = 8;
236         } else {
237                 mi.i_format.opcode     = sw_op;
238                 width = 4;
239         }
240         mi.i_format.rs         = 4;             /* $a0 */
241         mi.i_format.rt         = reg;           /* $reg */
242         mi.i_format.simmediate = store_offset;
243
244         store_offset += width;
245         emit_instruction(mi);
246 }
247
248 static inline void build_store_reg(int reg)
249 {
250         if (cpu_has_prefetch)
251                 if (reg)
252                         build_dst_pref(pref_offset_copy);
253                 else
254                         build_dst_pref(pref_offset_clear);
255         else if (cpu_has_cache_cdex_s)
256                 build_cdex_s();
257         else if (cpu_has_cache_cdex_p)
258                 build_cdex_p();
259
260         __build_store_reg(reg);
261 }
262
263 static inline void build_addiu_a2_a0(unsigned long offset)
264 {
265         union mips_instruction mi;
266
267         BUG_ON(offset > 0x7fff);
268
269         mi.i_format.opcode     = cpu_has_64bit_addresses ? daddiu_op : addiu_op;
270         mi.i_format.rs         = 4;             /* $a0 */
271         mi.i_format.rt         = 6;             /* $a2 */
272         mi.i_format.simmediate = offset;
273
274         emit_instruction(mi);
275 }
276
277 static inline void build_addiu_a1(unsigned long offset)
278 {
279         union mips_instruction mi;
280
281         BUG_ON(offset > 0x7fff);
282
283         mi.i_format.opcode     = cpu_has_64bit_addresses ? daddiu_op : addiu_op;
284         mi.i_format.rs         = 5;             /* $a1 */
285         mi.i_format.rt         = 5;             /* $a1 */
286         mi.i_format.simmediate = offset;
287
288         load_offset -= offset;
289
290         emit_instruction(mi);
291 }
292
293 static inline void build_addiu_a0(unsigned long offset)
294 {
295         union mips_instruction mi;
296
297         BUG_ON(offset > 0x7fff);
298
299         mi.i_format.opcode     = cpu_has_64bit_addresses ? daddiu_op : addiu_op;
300         mi.i_format.rs         = 4;             /* $a0 */
301         mi.i_format.rt         = 4;             /* $a0 */
302         mi.i_format.simmediate = offset;
303
304         store_offset -= offset;
305
306         emit_instruction(mi);
307 }
308
309 static inline void build_bne(unsigned int *dest)
310 {
311         union mips_instruction mi;
312
313         mi.i_format.opcode = bne_op;
314         mi.i_format.rs     = 6;                 /* $a2 */
315         mi.i_format.rt     = 4;                 /* $a0 */
316         mi.i_format.simmediate = dest - epc - 1;
317
318         *epc++ = mi.word;
319         flush_delay_slot_or_nop();
320 }
321
322 static inline void build_jr_ra(void)
323 {
324         union mips_instruction mi;
325
326         mi.r_format.opcode = spec_op;
327         mi.r_format.rs     = 31;
328         mi.r_format.rt     = 0;
329         mi.r_format.rd     = 0;
330         mi.r_format.re     = 0;
331         mi.r_format.func   = jr_op;
332
333         *epc++ = mi.word;
334         flush_delay_slot_or_nop();
335 }
336
337 void __init build_clear_page(void)
338 {
339         unsigned int loop_start;
340
341         epc = (unsigned int *) &clear_page_array;
342         instruction_pending = 0;
343         store_offset = 0;
344
345         if (cpu_has_prefetch) {
346                 switch (current_cpu_data.cputype) {
347                 case CPU_RM9000:
348                         /*
349                          * As a workaround for erratum G105 which make the
350                          * PrepareForStore hint unusable we fall back to
351                          * StoreRetained on the RM9000.  Once it is known which
352                          * versions of the RM9000 we'll be able to condition-
353                          * alize this.
354                          */
355
356                 case CPU_R10000:
357                 case CPU_R12000:
358                         pref_src_mode = Pref_LoadStreamed;
359                         pref_dst_mode = Pref_StoreRetained;
360                         break;
361
362                 default:
363                         pref_src_mode = Pref_LoadStreamed;
364                         pref_dst_mode = Pref_PrepareForStore;
365                         break;
366                 }
367         }
368
369         build_addiu_a2_a0(PAGE_SIZE - (cpu_has_prefetch ? pref_offset_clear : 0));
370
371         if (R4600_V2_HIT_CACHEOP_WAR && ((read_c0_prid() & 0xfff0) == 0x2020))
372                 build_insn_word(0x3c01a000);    /* lui     $at, 0xa000  */
373
374 dest = label();
375         do {
376                 build_store_reg(0);
377                 build_store_reg(0);
378                 build_store_reg(0);
379                 build_store_reg(0);
380         } while (store_offset < half_scache_line_size());
381         build_addiu_a0(2 * store_offset);
382         loop_start = store_offset;
383         do {
384                 build_store_reg(0);
385                 build_store_reg(0);
386                 build_store_reg(0);
387                 build_store_reg(0);
388         } while ((store_offset - loop_start) < half_scache_line_size());
389         build_bne(dest);
390
391         if (cpu_has_prefetch && pref_offset_clear) {
392                 build_addiu_a2_a0(pref_offset_clear);
393         dest = label();
394                 loop_start = store_offset;
395                 do {
396                         __build_store_reg(0);
397                         __build_store_reg(0);
398                         __build_store_reg(0);
399                         __build_store_reg(0);
400                 } while ((store_offset - loop_start) < half_scache_line_size());
401                 build_addiu_a0(2 * store_offset);
402                 loop_start = store_offset;
403                 do {
404                         __build_store_reg(0);
405                         __build_store_reg(0);
406                         __build_store_reg(0);
407                         __build_store_reg(0);
408                 } while ((store_offset - loop_start) < half_scache_line_size());
409                 build_bne(dest);
410         }
411
412         build_jr_ra();
413
414         flush_icache_range((unsigned long)&clear_page_array,
415                            (unsigned long) epc);
416
417         BUG_ON(epc > clear_page_array + ARRAY_SIZE(clear_page_array));
418 }
419
420 void __init build_copy_page(void)
421 {
422         unsigned int loop_start;
423
424         epc = (unsigned int *) &copy_page_array;
425         store_offset = load_offset = 0;
426         instruction_pending = 0;
427
428         build_addiu_a2_a0(PAGE_SIZE - (cpu_has_prefetch ? pref_offset_copy : 0));
429
430         if (R4600_V2_HIT_CACHEOP_WAR && ((read_c0_prid() & 0xfff0) == 0x2020))
431                 build_insn_word(0x3c01a000);    /* lui     $at, 0xa000  */
432
433 dest = label();
434         loop_start = store_offset;
435         do {
436                 build_load_reg( 8);
437                 build_load_reg( 9);
438                 build_load_reg(10);
439                 build_load_reg(11);
440                 build_store_reg( 8);
441                 build_store_reg( 9);
442                 build_store_reg(10);
443                 build_store_reg(11);
444         } while ((store_offset - loop_start) < half_scache_line_size());
445         build_addiu_a0(2 * store_offset);
446         build_addiu_a1(2 * load_offset);
447         loop_start = store_offset;
448         do {
449                 build_load_reg( 8);
450                 build_load_reg( 9);
451                 build_load_reg(10);
452                 build_load_reg(11);
453                 build_store_reg( 8);
454                 build_store_reg( 9);
455                 build_store_reg(10);
456                 build_store_reg(11);
457         } while ((store_offset - loop_start) < half_scache_line_size());
458         build_bne(dest);
459
460         if (cpu_has_prefetch && pref_offset_copy) {
461                 build_addiu_a2_a0(pref_offset_copy);
462         dest = label();
463                 loop_start = store_offset;
464                 do {
465                         __build_load_reg( 8);
466                         __build_load_reg( 9);
467                         __build_load_reg(10);
468                         __build_load_reg(11);
469                         __build_store_reg( 8);
470                         __build_store_reg( 9);
471                         __build_store_reg(10);
472                         __build_store_reg(11);
473                 } while ((store_offset - loop_start) < half_scache_line_size());
474                 build_addiu_a0(2 * store_offset);
475                 build_addiu_a1(2 * load_offset);
476                 loop_start = store_offset;
477                 do {
478                         __build_load_reg( 8);
479                         __build_load_reg( 9);
480                         __build_load_reg(10);
481                         __build_load_reg(11);
482                         __build_store_reg( 8);
483                         __build_store_reg( 9);
484                         __build_store_reg(10);
485                         __build_store_reg(11);
486                 } while ((store_offset - loop_start) < half_scache_line_size());
487                 build_bne(dest);
488         }
489
490         build_jr_ra();
491
492         flush_icache_range((unsigned long)&copy_page_array,
493                            (unsigned long) epc);
494
495         BUG_ON(epc > copy_page_array + ARRAY_SIZE(copy_page_array));
496 }