page.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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 ([email protected])
  7. * Copyright (C) 2007 Maciej W. Rozycki
  8. * Copyright (C) 2008 Thiemo Seufer
  9. * Copyright (C) 2012 MIPS Technologies, Inc.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/sched.h>
  13. #include <linux/smp.h>
  14. #include <linux/mm.h>
  15. #include <linux/proc_fs.h>
  16. #include <asm/bugs.h>
  17. #include <asm/cacheops.h>
  18. #include <asm/cpu-type.h>
  19. #include <asm/inst.h>
  20. #include <asm/io.h>
  21. #include <asm/page.h>
  22. #include <asm/prefetch.h>
  23. #include <asm/bootinfo.h>
  24. #include <asm/mipsregs.h>
  25. #include <asm/mmu_context.h>
  26. #include <asm/cpu.h>
  27. #ifdef CONFIG_SIBYTE_DMA_PAGEOPS
  28. #include <asm/sibyte/sb1250.h>
  29. #include <asm/sibyte/sb1250_regs.h>
  30. #include <asm/sibyte/sb1250_dma.h>
  31. #endif
  32. #include <asm/uasm.h>
  33. /* Registers used in the assembled routines. */
  34. #define ZERO 0
  35. #define AT 2
  36. #define A0 4
  37. #define A1 5
  38. #define A2 6
  39. #define T0 8
  40. #define T1 9
  41. #define T2 10
  42. #define T3 11
  43. #define T9 25
  44. #define RA 31
  45. /* Handle labels (which must be positive integers). */
  46. enum label_id {
  47. label_clear_nopref = 1,
  48. label_clear_pref,
  49. label_copy_nopref,
  50. label_copy_pref_both,
  51. label_copy_pref_store,
  52. };
  53. UASM_L_LA(_clear_nopref)
  54. UASM_L_LA(_clear_pref)
  55. UASM_L_LA(_copy_nopref)
  56. UASM_L_LA(_copy_pref_both)
  57. UASM_L_LA(_copy_pref_store)
  58. /* We need one branch and therefore one relocation per target label. */
  59. static struct uasm_label labels[5];
  60. static struct uasm_reloc relocs[5];
  61. #define cpu_is_r4600_v1_x() ((read_c0_prid() & 0xfffffff0) == 0x00002010)
  62. #define cpu_is_r4600_v2_x() ((read_c0_prid() & 0xfffffff0) == 0x00002020)
  63. /*
  64. * R6 has a limited offset of the pref instruction.
  65. * Skip it if the offset is more than 9 bits.
  66. */
  67. #define _uasm_i_pref(a, b, c, d) \
  68. do { \
  69. if (cpu_has_mips_r6) { \
  70. if (c <= 0xff && c >= -0x100) \
  71. uasm_i_pref(a, b, c, d);\
  72. } else { \
  73. uasm_i_pref(a, b, c, d); \
  74. } \
  75. } while(0)
  76. static int pref_bias_clear_store;
  77. static int pref_bias_copy_load;
  78. static int pref_bias_copy_store;
  79. static u32 pref_src_mode;
  80. static u32 pref_dst_mode;
  81. static int clear_word_size;
  82. static int copy_word_size;
  83. static int half_clear_loop_size;
  84. static int half_copy_loop_size;
  85. static int cache_line_size;
  86. #define cache_line_mask() (cache_line_size - 1)
  87. static inline void
  88. pg_addiu(u32 **buf, unsigned int reg1, unsigned int reg2, unsigned int off)
  89. {
  90. if (cpu_has_64bit_gp_regs &&
  91. IS_ENABLED(CONFIG_CPU_DADDI_WORKAROUNDS) &&
  92. r4k_daddiu_bug()) {
  93. if (off > 0x7fff) {
  94. uasm_i_lui(buf, T9, uasm_rel_hi(off));
  95. uasm_i_addiu(buf, T9, T9, uasm_rel_lo(off));
  96. } else
  97. uasm_i_addiu(buf, T9, ZERO, off);
  98. uasm_i_daddu(buf, reg1, reg2, T9);
  99. } else {
  100. if (off > 0x7fff) {
  101. uasm_i_lui(buf, T9, uasm_rel_hi(off));
  102. uasm_i_addiu(buf, T9, T9, uasm_rel_lo(off));
  103. UASM_i_ADDU(buf, reg1, reg2, T9);
  104. } else
  105. UASM_i_ADDIU(buf, reg1, reg2, off);
  106. }
  107. }
  108. static void set_prefetch_parameters(void)
  109. {
  110. if (cpu_has_64bit_gp_regs || cpu_has_64bit_zero_reg)
  111. clear_word_size = 8;
  112. else
  113. clear_word_size = 4;
  114. if (cpu_has_64bit_gp_regs)
  115. copy_word_size = 8;
  116. else
  117. copy_word_size = 4;
  118. /*
  119. * The pref's used here are using "streaming" hints, which cause the
  120. * copied data to be kicked out of the cache sooner. A page copy often
  121. * ends up copying a lot more data than is commonly used, so this seems
  122. * to make sense in terms of reducing cache pollution, but I've no real
  123. * performance data to back this up.
  124. */
  125. if (cpu_has_prefetch) {
  126. /*
  127. * XXX: Most prefetch bias values in here are based on
  128. * guesswork.
  129. */
  130. cache_line_size = cpu_dcache_line_size();
  131. switch (current_cpu_type()) {
  132. case CPU_R5500:
  133. case CPU_TX49XX:
  134. /* These processors only support the Pref_Load. */
  135. pref_bias_copy_load = 256;
  136. break;
  137. case CPU_R10000:
  138. case CPU_R12000:
  139. case CPU_R14000:
  140. case CPU_R16000:
  141. /*
  142. * Those values have been experimentally tuned for an
  143. * Origin 200.
  144. */
  145. pref_bias_clear_store = 512;
  146. pref_bias_copy_load = 256;
  147. pref_bias_copy_store = 256;
  148. pref_src_mode = Pref_LoadStreamed;
  149. pref_dst_mode = Pref_StoreStreamed;
  150. break;
  151. case CPU_SB1:
  152. case CPU_SB1A:
  153. pref_bias_clear_store = 128;
  154. pref_bias_copy_load = 128;
  155. pref_bias_copy_store = 128;
  156. /*
  157. * SB1 pass1 Pref_LoadStreamed/Pref_StoreStreamed
  158. * hints are broken.
  159. */
  160. if (current_cpu_type() == CPU_SB1 &&
  161. (current_cpu_data.processor_id & 0xff) < 0x02) {
  162. pref_src_mode = Pref_Load;
  163. pref_dst_mode = Pref_Store;
  164. } else {
  165. pref_src_mode = Pref_LoadStreamed;
  166. pref_dst_mode = Pref_StoreStreamed;
  167. }
  168. break;
  169. case CPU_LOONGSON64:
  170. /* Loongson-3 only support the Pref_Load/Pref_Store. */
  171. pref_bias_clear_store = 128;
  172. pref_bias_copy_load = 128;
  173. pref_bias_copy_store = 128;
  174. pref_src_mode = Pref_Load;
  175. pref_dst_mode = Pref_Store;
  176. break;
  177. default:
  178. pref_bias_clear_store = 128;
  179. pref_bias_copy_load = 256;
  180. pref_bias_copy_store = 128;
  181. pref_src_mode = Pref_LoadStreamed;
  182. if (cpu_has_mips_r6)
  183. /*
  184. * Bit 30 (Pref_PrepareForStore) has been
  185. * removed from MIPS R6. Use bit 5
  186. * (Pref_StoreStreamed).
  187. */
  188. pref_dst_mode = Pref_StoreStreamed;
  189. else
  190. pref_dst_mode = Pref_PrepareForStore;
  191. break;
  192. }
  193. } else {
  194. if (cpu_has_cache_cdex_s)
  195. cache_line_size = cpu_scache_line_size();
  196. else if (cpu_has_cache_cdex_p)
  197. cache_line_size = cpu_dcache_line_size();
  198. }
  199. /*
  200. * Too much unrolling will overflow the available space in
  201. * clear_space_array / copy_page_array.
  202. */
  203. half_clear_loop_size = min(16 * clear_word_size,
  204. max(cache_line_size >> 1,
  205. 4 * clear_word_size));
  206. half_copy_loop_size = min(16 * copy_word_size,
  207. max(cache_line_size >> 1,
  208. 4 * copy_word_size));
  209. }
  210. static void build_clear_store(u32 **buf, int off)
  211. {
  212. if (cpu_has_64bit_gp_regs || cpu_has_64bit_zero_reg) {
  213. uasm_i_sd(buf, ZERO, off, A0);
  214. } else {
  215. uasm_i_sw(buf, ZERO, off, A0);
  216. }
  217. }
  218. static inline void build_clear_pref(u32 **buf, int off)
  219. {
  220. if (off & cache_line_mask())
  221. return;
  222. if (pref_bias_clear_store) {
  223. _uasm_i_pref(buf, pref_dst_mode, pref_bias_clear_store + off,
  224. A0);
  225. } else if (cache_line_size == (half_clear_loop_size << 1)) {
  226. if (cpu_has_cache_cdex_s) {
  227. uasm_i_cache(buf, Create_Dirty_Excl_SD, off, A0);
  228. } else if (cpu_has_cache_cdex_p) {
  229. if (IS_ENABLED(CONFIG_WAR_R4600_V1_HIT_CACHEOP) &&
  230. cpu_is_r4600_v1_x()) {
  231. uasm_i_nop(buf);
  232. uasm_i_nop(buf);
  233. uasm_i_nop(buf);
  234. uasm_i_nop(buf);
  235. }
  236. if (IS_ENABLED(CONFIG_WAR_R4600_V2_HIT_CACHEOP) &&
  237. cpu_is_r4600_v2_x())
  238. uasm_i_lw(buf, ZERO, ZERO, AT);
  239. uasm_i_cache(buf, Create_Dirty_Excl_D, off, A0);
  240. }
  241. }
  242. }
  243. extern u32 __clear_page_start;
  244. extern u32 __clear_page_end;
  245. extern u32 __copy_page_start;
  246. extern u32 __copy_page_end;
  247. void build_clear_page(void)
  248. {
  249. int off;
  250. u32 *buf = &__clear_page_start;
  251. struct uasm_label *l = labels;
  252. struct uasm_reloc *r = relocs;
  253. int i;
  254. static atomic_t run_once = ATOMIC_INIT(0);
  255. if (atomic_xchg(&run_once, 1)) {
  256. return;
  257. }
  258. memset(labels, 0, sizeof(labels));
  259. memset(relocs, 0, sizeof(relocs));
  260. set_prefetch_parameters();
  261. /*
  262. * This algorithm makes the following assumptions:
  263. * - The prefetch bias is a multiple of 2 words.
  264. * - The prefetch bias is less than one page.
  265. */
  266. BUG_ON(pref_bias_clear_store % (2 * clear_word_size));
  267. BUG_ON(PAGE_SIZE < pref_bias_clear_store);
  268. off = PAGE_SIZE - pref_bias_clear_store;
  269. if (off > 0xffff || !pref_bias_clear_store)
  270. pg_addiu(&buf, A2, A0, off);
  271. else
  272. uasm_i_ori(&buf, A2, A0, off);
  273. if (IS_ENABLED(CONFIG_WAR_R4600_V2_HIT_CACHEOP) && cpu_is_r4600_v2_x())
  274. uasm_i_lui(&buf, AT, uasm_rel_hi(0xa0000000));
  275. off = cache_line_size ? min(8, pref_bias_clear_store / cache_line_size)
  276. * cache_line_size : 0;
  277. while (off) {
  278. build_clear_pref(&buf, -off);
  279. off -= cache_line_size;
  280. }
  281. uasm_l_clear_pref(&l, buf);
  282. do {
  283. build_clear_pref(&buf, off);
  284. build_clear_store(&buf, off);
  285. off += clear_word_size;
  286. } while (off < half_clear_loop_size);
  287. pg_addiu(&buf, A0, A0, 2 * off);
  288. off = -off;
  289. do {
  290. build_clear_pref(&buf, off);
  291. if (off == -clear_word_size)
  292. uasm_il_bne(&buf, &r, A0, A2, label_clear_pref);
  293. build_clear_store(&buf, off);
  294. off += clear_word_size;
  295. } while (off < 0);
  296. if (pref_bias_clear_store) {
  297. pg_addiu(&buf, A2, A0, pref_bias_clear_store);
  298. uasm_l_clear_nopref(&l, buf);
  299. off = 0;
  300. do {
  301. build_clear_store(&buf, off);
  302. off += clear_word_size;
  303. } while (off < half_clear_loop_size);
  304. pg_addiu(&buf, A0, A0, 2 * off);
  305. off = -off;
  306. do {
  307. if (off == -clear_word_size)
  308. uasm_il_bne(&buf, &r, A0, A2,
  309. label_clear_nopref);
  310. build_clear_store(&buf, off);
  311. off += clear_word_size;
  312. } while (off < 0);
  313. }
  314. uasm_i_jr(&buf, RA);
  315. uasm_i_nop(&buf);
  316. BUG_ON(buf > &__clear_page_end);
  317. uasm_resolve_relocs(relocs, labels);
  318. pr_debug("Synthesized clear page handler (%u instructions).\n",
  319. (u32)(buf - &__clear_page_start));
  320. pr_debug("\t.set push\n");
  321. pr_debug("\t.set noreorder\n");
  322. for (i = 0; i < (buf - &__clear_page_start); i++)
  323. pr_debug("\t.word 0x%08x\n", (&__clear_page_start)[i]);
  324. pr_debug("\t.set pop\n");
  325. }
  326. static void build_copy_load(u32 **buf, int reg, int off)
  327. {
  328. if (cpu_has_64bit_gp_regs) {
  329. uasm_i_ld(buf, reg, off, A1);
  330. } else {
  331. uasm_i_lw(buf, reg, off, A1);
  332. }
  333. }
  334. static void build_copy_store(u32 **buf, int reg, int off)
  335. {
  336. if (cpu_has_64bit_gp_regs) {
  337. uasm_i_sd(buf, reg, off, A0);
  338. } else {
  339. uasm_i_sw(buf, reg, off, A0);
  340. }
  341. }
  342. static inline void build_copy_load_pref(u32 **buf, int off)
  343. {
  344. if (off & cache_line_mask())
  345. return;
  346. if (pref_bias_copy_load)
  347. _uasm_i_pref(buf, pref_src_mode, pref_bias_copy_load + off, A1);
  348. }
  349. static inline void build_copy_store_pref(u32 **buf, int off)
  350. {
  351. if (off & cache_line_mask())
  352. return;
  353. if (pref_bias_copy_store) {
  354. _uasm_i_pref(buf, pref_dst_mode, pref_bias_copy_store + off,
  355. A0);
  356. } else if (cache_line_size == (half_copy_loop_size << 1)) {
  357. if (cpu_has_cache_cdex_s) {
  358. uasm_i_cache(buf, Create_Dirty_Excl_SD, off, A0);
  359. } else if (cpu_has_cache_cdex_p) {
  360. if (IS_ENABLED(CONFIG_WAR_R4600_V1_HIT_CACHEOP) &&
  361. cpu_is_r4600_v1_x()) {
  362. uasm_i_nop(buf);
  363. uasm_i_nop(buf);
  364. uasm_i_nop(buf);
  365. uasm_i_nop(buf);
  366. }
  367. if (IS_ENABLED(CONFIG_WAR_R4600_V2_HIT_CACHEOP) &&
  368. cpu_is_r4600_v2_x())
  369. uasm_i_lw(buf, ZERO, ZERO, AT);
  370. uasm_i_cache(buf, Create_Dirty_Excl_D, off, A0);
  371. }
  372. }
  373. }
  374. void build_copy_page(void)
  375. {
  376. int off;
  377. u32 *buf = &__copy_page_start;
  378. struct uasm_label *l = labels;
  379. struct uasm_reloc *r = relocs;
  380. int i;
  381. static atomic_t run_once = ATOMIC_INIT(0);
  382. if (atomic_xchg(&run_once, 1)) {
  383. return;
  384. }
  385. memset(labels, 0, sizeof(labels));
  386. memset(relocs, 0, sizeof(relocs));
  387. set_prefetch_parameters();
  388. /*
  389. * This algorithm makes the following assumptions:
  390. * - All prefetch biases are multiples of 8 words.
  391. * - The prefetch biases are less than one page.
  392. * - The store prefetch bias isn't greater than the load
  393. * prefetch bias.
  394. */
  395. BUG_ON(pref_bias_copy_load % (8 * copy_word_size));
  396. BUG_ON(pref_bias_copy_store % (8 * copy_word_size));
  397. BUG_ON(PAGE_SIZE < pref_bias_copy_load);
  398. BUG_ON(pref_bias_copy_store > pref_bias_copy_load);
  399. off = PAGE_SIZE - pref_bias_copy_load;
  400. if (off > 0xffff || !pref_bias_copy_load)
  401. pg_addiu(&buf, A2, A0, off);
  402. else
  403. uasm_i_ori(&buf, A2, A0, off);
  404. if (IS_ENABLED(CONFIG_WAR_R4600_V2_HIT_CACHEOP) && cpu_is_r4600_v2_x())
  405. uasm_i_lui(&buf, AT, uasm_rel_hi(0xa0000000));
  406. off = cache_line_size ? min(8, pref_bias_copy_load / cache_line_size) *
  407. cache_line_size : 0;
  408. while (off) {
  409. build_copy_load_pref(&buf, -off);
  410. off -= cache_line_size;
  411. }
  412. off = cache_line_size ? min(8, pref_bias_copy_store / cache_line_size) *
  413. cache_line_size : 0;
  414. while (off) {
  415. build_copy_store_pref(&buf, -off);
  416. off -= cache_line_size;
  417. }
  418. uasm_l_copy_pref_both(&l, buf);
  419. do {
  420. build_copy_load_pref(&buf, off);
  421. build_copy_load(&buf, T0, off);
  422. build_copy_load_pref(&buf, off + copy_word_size);
  423. build_copy_load(&buf, T1, off + copy_word_size);
  424. build_copy_load_pref(&buf, off + 2 * copy_word_size);
  425. build_copy_load(&buf, T2, off + 2 * copy_word_size);
  426. build_copy_load_pref(&buf, off + 3 * copy_word_size);
  427. build_copy_load(&buf, T3, off + 3 * copy_word_size);
  428. build_copy_store_pref(&buf, off);
  429. build_copy_store(&buf, T0, off);
  430. build_copy_store_pref(&buf, off + copy_word_size);
  431. build_copy_store(&buf, T1, off + copy_word_size);
  432. build_copy_store_pref(&buf, off + 2 * copy_word_size);
  433. build_copy_store(&buf, T2, off + 2 * copy_word_size);
  434. build_copy_store_pref(&buf, off + 3 * copy_word_size);
  435. build_copy_store(&buf, T3, off + 3 * copy_word_size);
  436. off += 4 * copy_word_size;
  437. } while (off < half_copy_loop_size);
  438. pg_addiu(&buf, A1, A1, 2 * off);
  439. pg_addiu(&buf, A0, A0, 2 * off);
  440. off = -off;
  441. do {
  442. build_copy_load_pref(&buf, off);
  443. build_copy_load(&buf, T0, off);
  444. build_copy_load_pref(&buf, off + copy_word_size);
  445. build_copy_load(&buf, T1, off + copy_word_size);
  446. build_copy_load_pref(&buf, off + 2 * copy_word_size);
  447. build_copy_load(&buf, T2, off + 2 * copy_word_size);
  448. build_copy_load_pref(&buf, off + 3 * copy_word_size);
  449. build_copy_load(&buf, T3, off + 3 * copy_word_size);
  450. build_copy_store_pref(&buf, off);
  451. build_copy_store(&buf, T0, off);
  452. build_copy_store_pref(&buf, off + copy_word_size);
  453. build_copy_store(&buf, T1, off + copy_word_size);
  454. build_copy_store_pref(&buf, off + 2 * copy_word_size);
  455. build_copy_store(&buf, T2, off + 2 * copy_word_size);
  456. build_copy_store_pref(&buf, off + 3 * copy_word_size);
  457. if (off == -(4 * copy_word_size))
  458. uasm_il_bne(&buf, &r, A2, A0, label_copy_pref_both);
  459. build_copy_store(&buf, T3, off + 3 * copy_word_size);
  460. off += 4 * copy_word_size;
  461. } while (off < 0);
  462. if (pref_bias_copy_load - pref_bias_copy_store) {
  463. pg_addiu(&buf, A2, A0,
  464. pref_bias_copy_load - pref_bias_copy_store);
  465. uasm_l_copy_pref_store(&l, buf);
  466. off = 0;
  467. do {
  468. build_copy_load(&buf, T0, off);
  469. build_copy_load(&buf, T1, off + copy_word_size);
  470. build_copy_load(&buf, T2, off + 2 * copy_word_size);
  471. build_copy_load(&buf, T3, off + 3 * copy_word_size);
  472. build_copy_store_pref(&buf, off);
  473. build_copy_store(&buf, T0, off);
  474. build_copy_store_pref(&buf, off + copy_word_size);
  475. build_copy_store(&buf, T1, off + copy_word_size);
  476. build_copy_store_pref(&buf, off + 2 * copy_word_size);
  477. build_copy_store(&buf, T2, off + 2 * copy_word_size);
  478. build_copy_store_pref(&buf, off + 3 * copy_word_size);
  479. build_copy_store(&buf, T3, off + 3 * copy_word_size);
  480. off += 4 * copy_word_size;
  481. } while (off < half_copy_loop_size);
  482. pg_addiu(&buf, A1, A1, 2 * off);
  483. pg_addiu(&buf, A0, A0, 2 * off);
  484. off = -off;
  485. do {
  486. build_copy_load(&buf, T0, off);
  487. build_copy_load(&buf, T1, off + copy_word_size);
  488. build_copy_load(&buf, T2, off + 2 * copy_word_size);
  489. build_copy_load(&buf, T3, off + 3 * copy_word_size);
  490. build_copy_store_pref(&buf, off);
  491. build_copy_store(&buf, T0, off);
  492. build_copy_store_pref(&buf, off + copy_word_size);
  493. build_copy_store(&buf, T1, off + copy_word_size);
  494. build_copy_store_pref(&buf, off + 2 * copy_word_size);
  495. build_copy_store(&buf, T2, off + 2 * copy_word_size);
  496. build_copy_store_pref(&buf, off + 3 * copy_word_size);
  497. if (off == -(4 * copy_word_size))
  498. uasm_il_bne(&buf, &r, A2, A0,
  499. label_copy_pref_store);
  500. build_copy_store(&buf, T3, off + 3 * copy_word_size);
  501. off += 4 * copy_word_size;
  502. } while (off < 0);
  503. }
  504. if (pref_bias_copy_store) {
  505. pg_addiu(&buf, A2, A0, pref_bias_copy_store);
  506. uasm_l_copy_nopref(&l, buf);
  507. off = 0;
  508. do {
  509. build_copy_load(&buf, T0, off);
  510. build_copy_load(&buf, T1, off + copy_word_size);
  511. build_copy_load(&buf, T2, off + 2 * copy_word_size);
  512. build_copy_load(&buf, T3, off + 3 * copy_word_size);
  513. build_copy_store(&buf, T0, off);
  514. build_copy_store(&buf, T1, off + copy_word_size);
  515. build_copy_store(&buf, T2, off + 2 * copy_word_size);
  516. build_copy_store(&buf, T3, off + 3 * copy_word_size);
  517. off += 4 * copy_word_size;
  518. } while (off < half_copy_loop_size);
  519. pg_addiu(&buf, A1, A1, 2 * off);
  520. pg_addiu(&buf, A0, A0, 2 * off);
  521. off = -off;
  522. do {
  523. build_copy_load(&buf, T0, off);
  524. build_copy_load(&buf, T1, off + copy_word_size);
  525. build_copy_load(&buf, T2, off + 2 * copy_word_size);
  526. build_copy_load(&buf, T3, off + 3 * copy_word_size);
  527. build_copy_store(&buf, T0, off);
  528. build_copy_store(&buf, T1, off + copy_word_size);
  529. build_copy_store(&buf, T2, off + 2 * copy_word_size);
  530. if (off == -(4 * copy_word_size))
  531. uasm_il_bne(&buf, &r, A2, A0,
  532. label_copy_nopref);
  533. build_copy_store(&buf, T3, off + 3 * copy_word_size);
  534. off += 4 * copy_word_size;
  535. } while (off < 0);
  536. }
  537. uasm_i_jr(&buf, RA);
  538. uasm_i_nop(&buf);
  539. BUG_ON(buf > &__copy_page_end);
  540. uasm_resolve_relocs(relocs, labels);
  541. pr_debug("Synthesized copy page handler (%u instructions).\n",
  542. (u32)(buf - &__copy_page_start));
  543. pr_debug("\t.set push\n");
  544. pr_debug("\t.set noreorder\n");
  545. for (i = 0; i < (buf - &__copy_page_start); i++)
  546. pr_debug("\t.word 0x%08x\n", (&__copy_page_start)[i]);
  547. pr_debug("\t.set pop\n");
  548. }
  549. #ifdef CONFIG_SIBYTE_DMA_PAGEOPS
  550. extern void clear_page_cpu(void *page);
  551. extern void copy_page_cpu(void *to, void *from);
  552. /*
  553. * Pad descriptors to cacheline, since each is exclusively owned by a
  554. * particular CPU.
  555. */
  556. struct dmadscr {
  557. u64 dscr_a;
  558. u64 dscr_b;
  559. u64 pad_a;
  560. u64 pad_b;
  561. } ____cacheline_aligned_in_smp page_descr[DM_NUM_CHANNELS];
  562. void clear_page(void *page)
  563. {
  564. u64 to_phys = CPHYSADDR((unsigned long)page);
  565. unsigned int cpu = smp_processor_id();
  566. /* if the page is not in KSEG0, use old way */
  567. if ((long)KSEGX((unsigned long)page) != (long)CKSEG0)
  568. return clear_page_cpu(page);
  569. page_descr[cpu].dscr_a = to_phys | M_DM_DSCRA_ZERO_MEM |
  570. M_DM_DSCRA_L2C_DEST | M_DM_DSCRA_INTERRUPT;
  571. page_descr[cpu].dscr_b = V_DM_DSCRB_SRC_LENGTH(PAGE_SIZE);
  572. __raw_writeq(1, IOADDR(A_DM_REGISTER(cpu, R_DM_DSCR_COUNT)));
  573. /*
  574. * Don't really want to do it this way, but there's no
  575. * reliable way to delay completion detection.
  576. */
  577. while (!(__raw_readq(IOADDR(A_DM_REGISTER(cpu, R_DM_DSCR_BASE_DEBUG)))
  578. & M_DM_DSCR_BASE_INTERRUPT))
  579. ;
  580. __raw_readq(IOADDR(A_DM_REGISTER(cpu, R_DM_DSCR_BASE)));
  581. }
  582. EXPORT_SYMBOL(clear_page);
  583. void copy_page(void *to, void *from)
  584. {
  585. u64 from_phys = CPHYSADDR((unsigned long)from);
  586. u64 to_phys = CPHYSADDR((unsigned long)to);
  587. unsigned int cpu = smp_processor_id();
  588. /* if any page is not in KSEG0, use old way */
  589. if ((long)KSEGX((unsigned long)to) != (long)CKSEG0
  590. || (long)KSEGX((unsigned long)from) != (long)CKSEG0)
  591. return copy_page_cpu(to, from);
  592. page_descr[cpu].dscr_a = to_phys | M_DM_DSCRA_L2C_DEST |
  593. M_DM_DSCRA_INTERRUPT;
  594. page_descr[cpu].dscr_b = from_phys | V_DM_DSCRB_SRC_LENGTH(PAGE_SIZE);
  595. __raw_writeq(1, IOADDR(A_DM_REGISTER(cpu, R_DM_DSCR_COUNT)));
  596. /*
  597. * Don't really want to do it this way, but there's no
  598. * reliable way to delay completion detection.
  599. */
  600. while (!(__raw_readq(IOADDR(A_DM_REGISTER(cpu, R_DM_DSCR_BASE_DEBUG)))
  601. & M_DM_DSCR_BASE_INTERRUPT))
  602. ;
  603. __raw_readq(IOADDR(A_DM_REGISTER(cpu, R_DM_DSCR_BASE)));
  604. }
  605. EXPORT_SYMBOL(copy_page);
  606. #endif /* CONFIG_SIBYTE_DMA_PAGEOPS */