crc32-ce-core.S 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Accelerated CRC32(C) using ARM CRC, NEON and Crypto Extensions instructions
  3. *
  4. * Copyright (C) 2016 Linaro Ltd <ard.biesheuvel@linaro.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. /* GPL HEADER START
  11. *
  12. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 only,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful, but
  19. * WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * General Public License version 2 for more details (a copy is included
  22. * in the LICENSE file that accompanied this code).
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * version 2 along with this program; If not, see http://www.gnu.org/licenses
  26. *
  27. * Please visit http://www.xyratex.com/contact if you need additional
  28. * information or have any questions.
  29. *
  30. * GPL HEADER END
  31. */
  32. /*
  33. * Copyright 2012 Xyratex Technology Limited
  34. *
  35. * Using hardware provided PCLMULQDQ instruction to accelerate the CRC32
  36. * calculation.
  37. * CRC32 polynomial:0x04c11db7(BE)/0xEDB88320(LE)
  38. * PCLMULQDQ is a new instruction in Intel SSE4.2, the reference can be found
  39. * at:
  40. * http://www.intel.com/products/processor/manuals/
  41. * Intel(R) 64 and IA-32 Architectures Software Developer's Manual
  42. * Volume 2B: Instruction Set Reference, N-Z
  43. *
  44. * Authors: Gregory Prestas <Gregory_Prestas@us.xyratex.com>
  45. * Alexander Boyko <Alexander_Boyko@xyratex.com>
  46. */
  47. #include <linux/linkage.h>
  48. #include <asm/assembler.h>
  49. .text
  50. .align 6
  51. .arch armv8-a
  52. .arch_extension crc
  53. .fpu crypto-neon-fp-armv8
  54. .Lcrc32_constants:
  55. /*
  56. * [x4*128+32 mod P(x) << 32)]' << 1 = 0x154442bd4
  57. * #define CONSTANT_R1 0x154442bd4LL
  58. *
  59. * [(x4*128-32 mod P(x) << 32)]' << 1 = 0x1c6e41596
  60. * #define CONSTANT_R2 0x1c6e41596LL
  61. */
  62. .quad 0x0000000154442bd4
  63. .quad 0x00000001c6e41596
  64. /*
  65. * [(x128+32 mod P(x) << 32)]' << 1 = 0x1751997d0
  66. * #define CONSTANT_R3 0x1751997d0LL
  67. *
  68. * [(x128-32 mod P(x) << 32)]' << 1 = 0x0ccaa009e
  69. * #define CONSTANT_R4 0x0ccaa009eLL
  70. */
  71. .quad 0x00000001751997d0
  72. .quad 0x00000000ccaa009e
  73. /*
  74. * [(x64 mod P(x) << 32)]' << 1 = 0x163cd6124
  75. * #define CONSTANT_R5 0x163cd6124LL
  76. */
  77. .quad 0x0000000163cd6124
  78. .quad 0x00000000FFFFFFFF
  79. /*
  80. * #define CRCPOLY_TRUE_LE_FULL 0x1DB710641LL
  81. *
  82. * Barrett Reduction constant (u64`) = u` = (x**64 / P(x))`
  83. * = 0x1F7011641LL
  84. * #define CONSTANT_RU 0x1F7011641LL
  85. */
  86. .quad 0x00000001DB710641
  87. .quad 0x00000001F7011641
  88. .Lcrc32c_constants:
  89. .quad 0x00000000740eef02
  90. .quad 0x000000009e4addf8
  91. .quad 0x00000000f20c0dfe
  92. .quad 0x000000014cd00bd6
  93. .quad 0x00000000dd45aab8
  94. .quad 0x00000000FFFFFFFF
  95. .quad 0x0000000105ec76f0
  96. .quad 0x00000000dea713f1
  97. dCONSTANTl .req d0
  98. dCONSTANTh .req d1
  99. qCONSTANT .req q0
  100. BUF .req r0
  101. LEN .req r1
  102. CRC .req r2
  103. qzr .req q9
  104. /**
  105. * Calculate crc32
  106. * BUF - buffer
  107. * LEN - sizeof buffer (multiple of 16 bytes), LEN should be > 63
  108. * CRC - initial crc32
  109. * return %eax crc32
  110. * uint crc32_pmull_le(unsigned char const *buffer,
  111. * size_t len, uint crc32)
  112. */
  113. ENTRY(crc32_pmull_le)
  114. adr r3, .Lcrc32_constants
  115. b 0f
  116. ENTRY(crc32c_pmull_le)
  117. adr r3, .Lcrc32c_constants
  118. 0: bic LEN, LEN, #15
  119. vld1.8 {q1-q2}, [BUF, :128]!
  120. vld1.8 {q3-q4}, [BUF, :128]!
  121. vmov.i8 qzr, #0
  122. vmov.i8 qCONSTANT, #0
  123. vmov dCONSTANTl[0], CRC
  124. veor.8 d2, d2, dCONSTANTl
  125. sub LEN, LEN, #0x40
  126. cmp LEN, #0x40
  127. blt less_64
  128. vld1.64 {qCONSTANT}, [r3]
  129. loop_64: /* 64 bytes Full cache line folding */
  130. sub LEN, LEN, #0x40
  131. vmull.p64 q5, d3, dCONSTANTh
  132. vmull.p64 q6, d5, dCONSTANTh
  133. vmull.p64 q7, d7, dCONSTANTh
  134. vmull.p64 q8, d9, dCONSTANTh
  135. vmull.p64 q1, d2, dCONSTANTl
  136. vmull.p64 q2, d4, dCONSTANTl
  137. vmull.p64 q3, d6, dCONSTANTl
  138. vmull.p64 q4, d8, dCONSTANTl
  139. veor.8 q1, q1, q5
  140. vld1.8 {q5}, [BUF, :128]!
  141. veor.8 q2, q2, q6
  142. vld1.8 {q6}, [BUF, :128]!
  143. veor.8 q3, q3, q7
  144. vld1.8 {q7}, [BUF, :128]!
  145. veor.8 q4, q4, q8
  146. vld1.8 {q8}, [BUF, :128]!
  147. veor.8 q1, q1, q5
  148. veor.8 q2, q2, q6
  149. veor.8 q3, q3, q7
  150. veor.8 q4, q4, q8
  151. cmp LEN, #0x40
  152. bge loop_64
  153. less_64: /* Folding cache line into 128bit */
  154. vldr dCONSTANTl, [r3, #16]
  155. vldr dCONSTANTh, [r3, #24]
  156. vmull.p64 q5, d3, dCONSTANTh
  157. vmull.p64 q1, d2, dCONSTANTl
  158. veor.8 q1, q1, q5
  159. veor.8 q1, q1, q2
  160. vmull.p64 q5, d3, dCONSTANTh
  161. vmull.p64 q1, d2, dCONSTANTl
  162. veor.8 q1, q1, q5
  163. veor.8 q1, q1, q3
  164. vmull.p64 q5, d3, dCONSTANTh
  165. vmull.p64 q1, d2, dCONSTANTl
  166. veor.8 q1, q1, q5
  167. veor.8 q1, q1, q4
  168. teq LEN, #0
  169. beq fold_64
  170. loop_16: /* Folding rest buffer into 128bit */
  171. subs LEN, LEN, #0x10
  172. vld1.8 {q2}, [BUF, :128]!
  173. vmull.p64 q5, d3, dCONSTANTh
  174. vmull.p64 q1, d2, dCONSTANTl
  175. veor.8 q1, q1, q5
  176. veor.8 q1, q1, q2
  177. bne loop_16
  178. fold_64:
  179. /* perform the last 64 bit fold, also adds 32 zeroes
  180. * to the input stream */
  181. vmull.p64 q2, d2, dCONSTANTh
  182. vext.8 q1, q1, qzr, #8
  183. veor.8 q1, q1, q2
  184. /* final 32-bit fold */
  185. vldr dCONSTANTl, [r3, #32]
  186. vldr d6, [r3, #40]
  187. vmov.i8 d7, #0
  188. vext.8 q2, q1, qzr, #4
  189. vand.8 d2, d2, d6
  190. vmull.p64 q1, d2, dCONSTANTl
  191. veor.8 q1, q1, q2
  192. /* Finish up with the bit-reversed barrett reduction 64 ==> 32 bits */
  193. vldr dCONSTANTl, [r3, #48]
  194. vldr dCONSTANTh, [r3, #56]
  195. vand.8 q2, q1, q3
  196. vext.8 q2, qzr, q2, #8
  197. vmull.p64 q2, d5, dCONSTANTh
  198. vand.8 q2, q2, q3
  199. vmull.p64 q2, d4, dCONSTANTl
  200. veor.8 q1, q1, q2
  201. vmov r0, s5
  202. bx lr
  203. ENDPROC(crc32_pmull_le)
  204. ENDPROC(crc32c_pmull_le)
  205. .macro __crc32, c
  206. subs ip, r2, #8
  207. bmi .Ltail\c
  208. tst r1, #3
  209. bne .Lunaligned\c
  210. teq ip, #0
  211. .Laligned8\c:
  212. ldrd r2, r3, [r1], #8
  213. ARM_BE8(rev r2, r2 )
  214. ARM_BE8(rev r3, r3 )
  215. crc32\c\()w r0, r0, r2
  216. crc32\c\()w r0, r0, r3
  217. bxeq lr
  218. subs ip, ip, #8
  219. bpl .Laligned8\c
  220. .Ltail\c:
  221. tst ip, #4
  222. beq 2f
  223. ldr r3, [r1], #4
  224. ARM_BE8(rev r3, r3 )
  225. crc32\c\()w r0, r0, r3
  226. 2: tst ip, #2
  227. beq 1f
  228. ldrh r3, [r1], #2
  229. ARM_BE8(rev16 r3, r3 )
  230. crc32\c\()h r0, r0, r3
  231. 1: tst ip, #1
  232. bxeq lr
  233. ldrb r3, [r1]
  234. crc32\c\()b r0, r0, r3
  235. bx lr
  236. .Lunaligned\c:
  237. tst r1, #1
  238. beq 2f
  239. ldrb r3, [r1], #1
  240. subs r2, r2, #1
  241. crc32\c\()b r0, r0, r3
  242. tst r1, #2
  243. beq 0f
  244. 2: ldrh r3, [r1], #2
  245. subs r2, r2, #2
  246. ARM_BE8(rev16 r3, r3 )
  247. crc32\c\()h r0, r0, r3
  248. 0: subs ip, r2, #8
  249. bpl .Laligned8\c
  250. b .Ltail\c
  251. .endm
  252. .align 5
  253. ENTRY(crc32_armv8_le)
  254. __crc32
  255. ENDPROC(crc32_armv8_le)
  256. .align 5
  257. ENTRY(crc32c_armv8_le)
  258. __crc32 c
  259. ENDPROC(crc32c_armv8_le)