pg-r4k.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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)) && advance) {
  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)) && advance) {
  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_a2(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 = 6; /* $a2 */
  223. mi.i_format.rt = 6; /* $a2 */
  224. mi.i_format.simmediate = offset;
  225. emit_instruction(mi);
  226. }
  227. static inline void build_addiu_a1(unsigned long offset)
  228. {
  229. union mips_instruction mi;
  230. BUG_ON(offset > 0x7fff);
  231. mi.i_format.opcode = cpu_has_64bit_gp_regs ? daddiu_op : addiu_op;
  232. mi.i_format.rs = 5; /* $a1 */
  233. mi.i_format.rt = 5; /* $a1 */
  234. mi.i_format.simmediate = offset;
  235. load_offset -= offset;
  236. emit_instruction(mi);
  237. }
  238. static inline void build_addiu_a0(unsigned long offset)
  239. {
  240. union mips_instruction mi;
  241. BUG_ON(offset > 0x7fff);
  242. mi.i_format.opcode = cpu_has_64bit_gp_regs ? daddiu_op : addiu_op;
  243. mi.i_format.rs = 4; /* $a0 */
  244. mi.i_format.rt = 4; /* $a0 */
  245. mi.i_format.simmediate = offset;
  246. store_offset -= offset;
  247. emit_instruction(mi);
  248. }
  249. static inline void build_bne(unsigned int *dest)
  250. {
  251. union mips_instruction mi;
  252. mi.i_format.opcode = bne_op;
  253. mi.i_format.rs = 6; /* $a2 */
  254. mi.i_format.rt = 4; /* $a0 */
  255. mi.i_format.simmediate = dest - epc - 1;
  256. *epc++ = mi.word;
  257. flush_delay_slot_or_nop();
  258. }
  259. static inline void build_jr_ra(void)
  260. {
  261. union mips_instruction mi;
  262. mi.r_format.opcode = spec_op;
  263. mi.r_format.rs = 31;
  264. mi.r_format.rt = 0;
  265. mi.r_format.rd = 0;
  266. mi.r_format.re = 0;
  267. mi.r_format.func = jr_op;
  268. *epc++ = mi.word;
  269. flush_delay_slot_or_nop();
  270. }
  271. void __init build_clear_page(void)
  272. {
  273. unsigned int loop_start;
  274. unsigned long off;
  275. epc = (unsigned int *) &clear_page_array;
  276. instruction_pending = 0;
  277. store_offset = 0;
  278. if (cpu_has_prefetch) {
  279. switch (current_cpu_data.cputype) {
  280. case CPU_TX49XX:
  281. /* TX49 supports only Pref_Load */
  282. pref_offset_clear = 0;
  283. pref_offset_copy = 0;
  284. break;
  285. case CPU_RM9000:
  286. /*
  287. * As a workaround for erratum G105 which make the
  288. * PrepareForStore hint unusable we fall back to
  289. * StoreRetained on the RM9000. Once it is known which
  290. * versions of the RM9000 we'll be able to condition-
  291. * alize this.
  292. */
  293. case CPU_R10000:
  294. case CPU_R12000:
  295. case CPU_R14000:
  296. pref_src_mode = Pref_LoadStreamed;
  297. pref_dst_mode = Pref_StoreStreamed;
  298. break;
  299. default:
  300. pref_src_mode = Pref_LoadStreamed;
  301. pref_dst_mode = Pref_PrepareForStore;
  302. break;
  303. }
  304. }
  305. off = PAGE_SIZE - (cpu_has_prefetch ? pref_offset_clear : 0);
  306. if (off > 0x7fff) {
  307. build_addiu_a2_a0(off >> 1);
  308. build_addiu_a2(off >> 1);
  309. } else
  310. build_addiu_a2_a0(off);
  311. if (R4600_V2_HIT_CACHEOP_WAR && cpu_is_r4600_v2_x())
  312. build_insn_word(0x3c01a000); /* lui $at, 0xa000 */
  313. dest = label();
  314. do {
  315. build_store_reg(0);
  316. build_store_reg(0);
  317. build_store_reg(0);
  318. build_store_reg(0);
  319. } while (store_offset < half_scache_line_size());
  320. build_addiu_a0(2 * store_offset);
  321. loop_start = store_offset;
  322. do {
  323. build_store_reg(0);
  324. build_store_reg(0);
  325. build_store_reg(0);
  326. build_store_reg(0);
  327. } while ((store_offset - loop_start) < half_scache_line_size());
  328. build_bne(dest);
  329. if (cpu_has_prefetch && pref_offset_clear) {
  330. build_addiu_a2_a0(pref_offset_clear);
  331. dest = label();
  332. loop_start = store_offset;
  333. do {
  334. __build_store_reg(0);
  335. __build_store_reg(0);
  336. __build_store_reg(0);
  337. __build_store_reg(0);
  338. } while ((store_offset - loop_start) < half_scache_line_size());
  339. build_addiu_a0(2 * store_offset);
  340. loop_start = store_offset;
  341. do {
  342. __build_store_reg(0);
  343. __build_store_reg(0);
  344. __build_store_reg(0);
  345. __build_store_reg(0);
  346. } while ((store_offset - loop_start) < half_scache_line_size());
  347. build_bne(dest);
  348. }
  349. build_jr_ra();
  350. BUG_ON(epc > clear_page_array + ARRAY_SIZE(clear_page_array));
  351. }
  352. void __init build_copy_page(void)
  353. {
  354. unsigned int loop_start;
  355. unsigned long off;
  356. epc = (unsigned int *) &copy_page_array;
  357. store_offset = load_offset = 0;
  358. instruction_pending = 0;
  359. off = PAGE_SIZE - (cpu_has_prefetch ? pref_offset_copy : 0);
  360. if (off > 0x7fff) {
  361. build_addiu_a2_a0(off >> 1);
  362. build_addiu_a2(off >> 1);
  363. } else
  364. build_addiu_a2_a0(off);
  365. if (R4600_V2_HIT_CACHEOP_WAR && cpu_is_r4600_v2_x())
  366. build_insn_word(0x3c01a000); /* lui $at, 0xa000 */
  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. if (cpu_has_prefetch && pref_offset_copy) {
  394. build_addiu_a2_a0(pref_offset_copy);
  395. dest = label();
  396. loop_start = store_offset;
  397. do {
  398. __build_load_reg( 8);
  399. __build_load_reg( 9);
  400. __build_load_reg(10);
  401. __build_load_reg(11);
  402. __build_store_reg( 8);
  403. __build_store_reg( 9);
  404. __build_store_reg(10);
  405. __build_store_reg(11);
  406. } while ((store_offset - loop_start) < half_scache_line_size());
  407. build_addiu_a0(2 * store_offset);
  408. build_addiu_a1(2 * load_offset);
  409. loop_start = store_offset;
  410. do {
  411. __build_load_reg( 8);
  412. __build_load_reg( 9);
  413. __build_load_reg(10);
  414. __build_load_reg(11);
  415. __build_store_reg( 8);
  416. __build_store_reg( 9);
  417. __build_store_reg(10);
  418. __build_store_reg(11);
  419. } while ((store_offset - loop_start) < half_scache_line_size());
  420. build_bne(dest);
  421. }
  422. build_jr_ra();
  423. BUG_ON(epc > copy_page_array + ARRAY_SIZE(copy_page_array));
  424. }