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