copy_32.S 9.7 KB

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