page.c 19 KB

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