page.c 19 KB

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