copy_32.S 9.6 KB

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