copy_32.S 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /*
  2. * Memory copy functions for 32-bit PowerPC.
  3. *
  4. * Copyright (C) 1996-2005 Paul Mackerras.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <asm/processor.h>
  12. #include <asm/cache.h>
  13. #include <asm/errno.h>
  14. #include <asm/ppc_asm.h>
  15. #include <asm/export.h>
  16. #define COPY_16_BYTES \
  17. lwz r7,4(r4); \
  18. lwz r8,8(r4); \
  19. lwz r9,12(r4); \
  20. lwzu r10,16(r4); \
  21. stw r7,4(r6); \
  22. stw r8,8(r6); \
  23. stw r9,12(r6); \
  24. stwu r10,16(r6)
  25. #define COPY_16_BYTES_WITHEX(n) \
  26. 8 ## n ## 0: \
  27. lwz r7,4(r4); \
  28. 8 ## n ## 1: \
  29. lwz r8,8(r4); \
  30. 8 ## n ## 2: \
  31. lwz r9,12(r4); \
  32. 8 ## n ## 3: \
  33. lwzu r10,16(r4); \
  34. 8 ## n ## 4: \
  35. stw r7,4(r6); \
  36. 8 ## n ## 5: \
  37. stw r8,8(r6); \
  38. 8 ## n ## 6: \
  39. stw r9,12(r6); \
  40. 8 ## n ## 7: \
  41. stwu r10,16(r6)
  42. #define COPY_16_BYTES_EXCODE(n) \
  43. 9 ## n ## 0: \
  44. addi r5,r5,-(16 * n); \
  45. b 104f; \
  46. 9 ## n ## 1: \
  47. addi r5,r5,-(16 * n); \
  48. b 105f; \
  49. .section __ex_table,"a"; \
  50. .align 2; \
  51. .long 8 ## n ## 0b,9 ## n ## 0b; \
  52. .long 8 ## n ## 1b,9 ## n ## 0b; \
  53. .long 8 ## n ## 2b,9 ## n ## 0b; \
  54. .long 8 ## n ## 3b,9 ## n ## 0b; \
  55. .long 8 ## n ## 4b,9 ## n ## 1b; \
  56. .long 8 ## n ## 5b,9 ## n ## 1b; \
  57. .long 8 ## n ## 6b,9 ## n ## 1b; \
  58. .long 8 ## n ## 7b,9 ## n ## 1b; \
  59. .text
  60. .text
  61. .stabs "arch/powerpc/lib/",N_SO,0,0,0f
  62. .stabs "copy_32.S",N_SO,0,0,0f
  63. 0:
  64. CACHELINE_BYTES = L1_CACHE_BYTES
  65. LG_CACHELINE_BYTES = L1_CACHE_SHIFT
  66. CACHELINE_MASK = (L1_CACHE_BYTES-1)
  67. /*
  68. * Use dcbz on the complete cache lines in the destination
  69. * to set them to zero. This requires that the destination
  70. * area is cacheable. -- paulus
  71. *
  72. * During early init, cache might not be active yet, so dcbz cannot be used.
  73. * We therefore skip the optimised bloc that uses dcbz. This jump is
  74. * replaced by a nop once cache is active. This is done in machine_init()
  75. */
  76. _GLOBAL(memset)
  77. rlwimi r4,r4,8,16,23
  78. rlwimi r4,r4,16,0,15
  79. addi r6,r3,-4
  80. cmplwi 0,r5,4
  81. blt 7f
  82. stwu r4,4(r6)
  83. beqlr
  84. andi. r0,r6,3
  85. add r5,r0,r5
  86. subf r6,r0,r6
  87. cmplwi 0,r4,0
  88. bne 2f /* Use normal procedure if r4 is not zero */
  89. EXPORT_SYMBOL(memset)
  90. _GLOBAL(memset_nocache_branch)
  91. b 2f /* Skip optimised bloc until cache is enabled */
  92. clrlwi r7,r6,32-LG_CACHELINE_BYTES
  93. add r8,r7,r5
  94. srwi r9,r8,LG_CACHELINE_BYTES
  95. addic. r9,r9,-1 /* total number of complete cachelines */
  96. ble 2f
  97. xori r0,r7,CACHELINE_MASK & ~3
  98. srwi. r0,r0,2
  99. beq 3f
  100. mtctr r0
  101. 4: stwu r4,4(r6)
  102. bdnz 4b
  103. 3: mtctr r9
  104. li r7,4
  105. 10: dcbz r7,r6
  106. addi r6,r6,CACHELINE_BYTES
  107. bdnz 10b
  108. clrlwi r5,r8,32-LG_CACHELINE_BYTES
  109. addi r5,r5,4
  110. 2: srwi r0,r5,2
  111. mtctr r0
  112. bdz 6f
  113. 1: stwu r4,4(r6)
  114. bdnz 1b
  115. 6: andi. r5,r5,3
  116. 7: cmpwi 0,r5,0
  117. beqlr
  118. mtctr r5
  119. addi r6,r6,3
  120. 8: stbu r4,1(r6)
  121. bdnz 8b
  122. blr
  123. /*
  124. * This version uses dcbz on the complete cache lines in the
  125. * destination area to reduce memory traffic. This requires that
  126. * the destination area is cacheable.
  127. * We only use this version if the source and dest don't overlap.
  128. * -- paulus.
  129. *
  130. * During early init, cache might not be active yet, so dcbz cannot be used.
  131. * We therefore jump to generic_memcpy which doesn't use dcbz. This jump is
  132. * replaced by a nop once cache is active. This is done in machine_init()
  133. */
  134. _GLOBAL(memmove)
  135. cmplw 0,r3,r4
  136. bgt backwards_memcpy
  137. /* fall through */
  138. _GLOBAL(memcpy)
  139. b generic_memcpy
  140. add r7,r3,r5 /* test if the src & dst overlap */
  141. add r8,r4,r5
  142. cmplw 0,r4,r7
  143. cmplw 1,r3,r8
  144. crand 0,0,4 /* cr0.lt &= cr1.lt */
  145. blt generic_memcpy /* if regions overlap */
  146. addi r4,r4,-4
  147. addi r6,r3,-4
  148. neg r0,r3
  149. andi. r0,r0,CACHELINE_MASK /* # bytes to start of cache line */
  150. beq 58f
  151. cmplw 0,r5,r0 /* is this more than total to do? */
  152. blt 63f /* if not much to do */
  153. andi. r8,r0,3 /* get it word-aligned first */
  154. subf r5,r0,r5
  155. mtctr r8
  156. beq+ 61f
  157. 70: lbz r9,4(r4) /* do some bytes */
  158. addi r4,r4,1
  159. addi r6,r6,1
  160. stb r9,3(r6)
  161. bdnz 70b
  162. 61: srwi. r0,r0,2
  163. mtctr r0
  164. beq 58f
  165. 72: lwzu r9,4(r4) /* do some words */
  166. stwu r9,4(r6)
  167. bdnz 72b
  168. 58: srwi. r0,r5,LG_CACHELINE_BYTES /* # complete cachelines */
  169. clrlwi r5,r5,32-LG_CACHELINE_BYTES
  170. li r11,4
  171. mtctr r0
  172. beq 63f
  173. 53:
  174. dcbz r11,r6
  175. COPY_16_BYTES
  176. #if L1_CACHE_BYTES >= 32
  177. COPY_16_BYTES
  178. #if L1_CACHE_BYTES >= 64
  179. COPY_16_BYTES
  180. COPY_16_BYTES
  181. #if L1_CACHE_BYTES >= 128
  182. COPY_16_BYTES
  183. COPY_16_BYTES
  184. COPY_16_BYTES
  185. COPY_16_BYTES
  186. #endif
  187. #endif
  188. #endif
  189. bdnz 53b
  190. 63: srwi. r0,r5,2
  191. mtctr r0
  192. beq 64f
  193. 30: lwzu r0,4(r4)
  194. stwu r0,4(r6)
  195. bdnz 30b
  196. 64: andi. r0,r5,3
  197. mtctr r0
  198. beq+ 65f
  199. addi r4,r4,3
  200. addi r6,r6,3
  201. 40: lbzu r0,1(r4)
  202. stbu r0,1(r6)
  203. bdnz 40b
  204. 65: blr
  205. EXPORT_SYMBOL(memcpy)
  206. EXPORT_SYMBOL(memmove)
  207. generic_memcpy:
  208. srwi. r7,r5,3
  209. addi r6,r3,-4
  210. addi r4,r4,-4
  211. beq 2f /* if less than 8 bytes to do */
  212. andi. r0,r6,3 /* get dest word aligned */
  213. mtctr r7
  214. bne 5f
  215. 1: lwz r7,4(r4)
  216. lwzu r8,8(r4)
  217. stw r7,4(r6)
  218. stwu r8,8(r6)
  219. bdnz 1b
  220. andi. r5,r5,7
  221. 2: cmplwi 0,r5,4
  222. blt 3f
  223. lwzu r0,4(r4)
  224. addi r5,r5,-4
  225. stwu r0,4(r6)
  226. 3: cmpwi 0,r5,0
  227. beqlr
  228. mtctr r5
  229. addi r4,r4,3
  230. addi r6,r6,3
  231. 4: lbzu r0,1(r4)
  232. stbu r0,1(r6)
  233. bdnz 4b
  234. blr
  235. 5: subfic r0,r0,4
  236. mtctr r0
  237. 6: lbz r7,4(r4)
  238. addi r4,r4,1
  239. stb r7,4(r6)
  240. addi r6,r6,1
  241. bdnz 6b
  242. subf r5,r0,r5
  243. rlwinm. r7,r5,32-3,3,31
  244. beq 2b
  245. mtctr r7
  246. b 1b
  247. _GLOBAL(backwards_memcpy)
  248. rlwinm. r7,r5,32-3,3,31 /* r0 = r5 >> 3 */
  249. add r6,r3,r5
  250. add r4,r4,r5
  251. beq 2f
  252. andi. r0,r6,3
  253. mtctr r7
  254. bne 5f
  255. 1: lwz r7,-4(r4)
  256. lwzu r8,-8(r4)
  257. stw r7,-4(r6)
  258. stwu r8,-8(r6)
  259. bdnz 1b
  260. andi. r5,r5,7
  261. 2: cmplwi 0,r5,4
  262. blt 3f
  263. lwzu r0,-4(r4)
  264. subi r5,r5,4
  265. stwu r0,-4(r6)
  266. 3: cmpwi 0,r5,0
  267. beqlr
  268. mtctr r5
  269. 4: lbzu r0,-1(r4)
  270. stbu r0,-1(r6)
  271. bdnz 4b
  272. blr
  273. 5: mtctr r0
  274. 6: lbzu r7,-1(r4)
  275. stbu r7,-1(r6)
  276. bdnz 6b
  277. subf r5,r0,r5
  278. rlwinm. r7,r5,32-3,3,31
  279. beq 2b
  280. mtctr r7
  281. b 1b
  282. _GLOBAL(__copy_tofrom_user)
  283. addi r4,r4,-4
  284. addi r6,r3,-4
  285. neg r0,r3
  286. andi. r0,r0,CACHELINE_MASK /* # bytes to start of cache line */
  287. beq 58f
  288. cmplw 0,r5,r0 /* is this more than total to do? */
  289. blt 63f /* if not much to do */
  290. andi. r8,r0,3 /* get it word-aligned first */
  291. mtctr r8
  292. beq+ 61f
  293. 70: lbz r9,4(r4) /* do some bytes */
  294. 71: stb r9,4(r6)
  295. addi r4,r4,1
  296. addi r6,r6,1
  297. bdnz 70b
  298. 61: subf r5,r0,r5
  299. srwi. r0,r0,2
  300. mtctr r0
  301. beq 58f
  302. 72: lwzu r9,4(r4) /* do some words */
  303. 73: stwu r9,4(r6)
  304. bdnz 72b
  305. .section __ex_table,"a"
  306. .align 2
  307. .long 70b,100f
  308. .long 71b,101f
  309. .long 72b,102f
  310. .long 73b,103f
  311. .text
  312. 58: srwi. r0,r5,LG_CACHELINE_BYTES /* # complete cachelines */
  313. clrlwi r5,r5,32-LG_CACHELINE_BYTES
  314. li r11,4
  315. beq 63f
  316. /* Here we decide how far ahead to prefetch the source */
  317. li r3,4
  318. cmpwi r0,1
  319. li r7,0
  320. ble 114f
  321. li r7,1
  322. #if MAX_COPY_PREFETCH > 1
  323. /* Heuristically, for large transfers we prefetch
  324. MAX_COPY_PREFETCH cachelines ahead. For small transfers
  325. we prefetch 1 cacheline ahead. */
  326. cmpwi r0,MAX_COPY_PREFETCH
  327. ble 112f
  328. li r7,MAX_COPY_PREFETCH
  329. 112: mtctr r7
  330. 111: dcbt r3,r4
  331. addi r3,r3,CACHELINE_BYTES
  332. bdnz 111b
  333. #else
  334. dcbt r3,r4
  335. addi r3,r3,CACHELINE_BYTES
  336. #endif /* MAX_COPY_PREFETCH > 1 */
  337. 114: subf r8,r7,r0
  338. mr r0,r7
  339. mtctr r8
  340. 53: dcbt r3,r4
  341. 54: dcbz r11,r6
  342. .section __ex_table,"a"
  343. .align 2
  344. .long 54b,105f
  345. .text
  346. /* the main body of the cacheline loop */
  347. COPY_16_BYTES_WITHEX(0)
  348. #if L1_CACHE_BYTES >= 32
  349. COPY_16_BYTES_WITHEX(1)
  350. #if L1_CACHE_BYTES >= 64
  351. COPY_16_BYTES_WITHEX(2)
  352. COPY_16_BYTES_WITHEX(3)
  353. #if L1_CACHE_BYTES >= 128
  354. COPY_16_BYTES_WITHEX(4)
  355. COPY_16_BYTES_WITHEX(5)
  356. COPY_16_BYTES_WITHEX(6)
  357. COPY_16_BYTES_WITHEX(7)
  358. #endif
  359. #endif
  360. #endif
  361. bdnz 53b
  362. cmpwi r0,0
  363. li r3,4
  364. li r7,0
  365. bne 114b
  366. 63: srwi. r0,r5,2
  367. mtctr r0
  368. beq 64f
  369. 30: lwzu r0,4(r4)
  370. 31: stwu r0,4(r6)
  371. bdnz 30b
  372. 64: andi. r0,r5,3
  373. mtctr r0
  374. beq+ 65f
  375. 40: lbz r0,4(r4)
  376. 41: stb r0,4(r6)
  377. addi r4,r4,1
  378. addi r6,r6,1
  379. bdnz 40b
  380. 65: li r3,0
  381. blr
  382. /* read fault, initial single-byte copy */
  383. 100: li r9,0
  384. b 90f
  385. /* write fault, initial single-byte copy */
  386. 101: li r9,1
  387. 90: subf r5,r8,r5
  388. li r3,0
  389. b 99f
  390. /* read fault, initial word copy */
  391. 102: li r9,0
  392. b 91f
  393. /* write fault, initial word copy */
  394. 103: li r9,1
  395. 91: li r3,2
  396. b 99f
  397. /*
  398. * this stuff handles faults in the cacheline loop and branches to either
  399. * 104f (if in read part) or 105f (if in write part), after updating r5
  400. */
  401. COPY_16_BYTES_EXCODE(0)
  402. #if L1_CACHE_BYTES >= 32
  403. COPY_16_BYTES_EXCODE(1)
  404. #if L1_CACHE_BYTES >= 64
  405. COPY_16_BYTES_EXCODE(2)
  406. COPY_16_BYTES_EXCODE(3)
  407. #if L1_CACHE_BYTES >= 128
  408. COPY_16_BYTES_EXCODE(4)
  409. COPY_16_BYTES_EXCODE(5)
  410. COPY_16_BYTES_EXCODE(6)
  411. COPY_16_BYTES_EXCODE(7)
  412. #endif
  413. #endif
  414. #endif
  415. /* read fault in cacheline loop */
  416. 104: li r9,0
  417. b 92f
  418. /* fault on dcbz (effectively a write fault) */
  419. /* or write fault in cacheline loop */
  420. 105: li r9,1
  421. 92: li r3,LG_CACHELINE_BYTES
  422. mfctr r8
  423. add r0,r0,r8
  424. b 106f
  425. /* read fault in final word loop */
  426. 108: li r9,0
  427. b 93f
  428. /* write fault in final word loop */
  429. 109: li r9,1
  430. 93: andi. r5,r5,3
  431. li r3,2
  432. b 99f
  433. /* read fault in final byte loop */
  434. 110: li r9,0
  435. b 94f
  436. /* write fault in final byte loop */
  437. 111: li r9,1
  438. 94: li r5,0
  439. li r3,0
  440. /*
  441. * At this stage the number of bytes not copied is
  442. * r5 + (ctr << r3), and r9 is 0 for read or 1 for write.
  443. */
  444. 99: mfctr r0
  445. 106: slw r3,r0,r3
  446. add. r3,r3,r5
  447. beq 120f /* shouldn't happen */
  448. cmpwi 0,r9,0
  449. bne 120f
  450. /* for a read fault, first try to continue the copy one byte at a time */
  451. mtctr r3
  452. 130: lbz r0,4(r4)
  453. 131: stb r0,4(r6)
  454. addi r4,r4,1
  455. addi r6,r6,1
  456. bdnz 130b
  457. /* then clear out the destination: r3 bytes starting at 4(r6) */
  458. 132: mfctr r3
  459. srwi. r0,r3,2
  460. li r9,0
  461. mtctr r0
  462. beq 113f
  463. 112: stwu r9,4(r6)
  464. bdnz 112b
  465. 113: andi. r0,r3,3
  466. mtctr r0
  467. beq 120f
  468. 114: stb r9,4(r6)
  469. addi r6,r6,1
  470. bdnz 114b
  471. 120: blr
  472. .section __ex_table,"a"
  473. .align 2
  474. .long 30b,108b
  475. .long 31b,109b
  476. .long 40b,110b
  477. .long 41b,111b
  478. .long 130b,132b
  479. .long 131b,120b
  480. .long 112b,120b
  481. .long 114b,120b
  482. .text
  483. EXPORT_SYMBOL(__copy_tofrom_user)