pg-r4k.c 11 KB

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