pg-r4k.c 11 KB

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