copy_32.S 9.5 KB

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