pg-r4k.c 12 KB

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