pg-r4k.c 12 KB

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