safexcel.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. /*
  2. * Copyright (C) 2017 Marvell
  3. *
  4. * Antoine Tenart <antoine.tenart@free-electrons.com>
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/device.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/dmapool.h>
  14. #include <linux/firmware.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/module.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/workqueue.h>
  21. #include <crypto/internal/hash.h>
  22. #include <crypto/internal/skcipher.h>
  23. #include "safexcel.h"
  24. static u32 max_rings = EIP197_MAX_RINGS;
  25. module_param(max_rings, uint, 0644);
  26. MODULE_PARM_DESC(max_rings, "Maximum number of rings to use.");
  27. static void eip197_trc_cache_init(struct safexcel_crypto_priv *priv)
  28. {
  29. u32 val, htable_offset;
  30. int i;
  31. /* Enable the record cache memory access */
  32. val = readl(priv->base + EIP197_CS_RAM_CTRL);
  33. val &= ~EIP197_TRC_ENABLE_MASK;
  34. val |= EIP197_TRC_ENABLE_0;
  35. writel(val, priv->base + EIP197_CS_RAM_CTRL);
  36. /* Clear all ECC errors */
  37. writel(0, priv->base + EIP197_TRC_ECCCTRL);
  38. /*
  39. * Make sure the cache memory is accessible by taking record cache into
  40. * reset.
  41. */
  42. val = readl(priv->base + EIP197_TRC_PARAMS);
  43. val |= EIP197_TRC_PARAMS_SW_RESET;
  44. val &= ~EIP197_TRC_PARAMS_DATA_ACCESS;
  45. writel(val, priv->base + EIP197_TRC_PARAMS);
  46. /* Clear all records */
  47. for (i = 0; i < EIP197_CS_RC_MAX; i++) {
  48. u32 val, offset = EIP197_CLASSIFICATION_RAMS + i * EIP197_CS_RC_SIZE;
  49. writel(EIP197_CS_RC_NEXT(EIP197_RC_NULL) |
  50. EIP197_CS_RC_PREV(EIP197_RC_NULL),
  51. priv->base + offset);
  52. val = EIP197_CS_RC_NEXT(i+1) | EIP197_CS_RC_PREV(i-1);
  53. if (i == 0)
  54. val |= EIP197_CS_RC_PREV(EIP197_RC_NULL);
  55. else if (i == EIP197_CS_RC_MAX - 1)
  56. val |= EIP197_CS_RC_NEXT(EIP197_RC_NULL);
  57. writel(val, priv->base + offset + sizeof(u32));
  58. }
  59. /* Clear the hash table entries */
  60. htable_offset = EIP197_CS_RC_MAX * EIP197_CS_RC_SIZE;
  61. for (i = 0; i < 64; i++)
  62. writel(GENMASK(29, 0),
  63. priv->base + EIP197_CLASSIFICATION_RAMS + htable_offset + i * sizeof(u32));
  64. /* Disable the record cache memory access */
  65. val = readl(priv->base + EIP197_CS_RAM_CTRL);
  66. val &= ~EIP197_TRC_ENABLE_MASK;
  67. writel(val, priv->base + EIP197_CS_RAM_CTRL);
  68. /* Write head and tail pointers of the record free chain */
  69. val = EIP197_TRC_FREECHAIN_HEAD_PTR(0) |
  70. EIP197_TRC_FREECHAIN_TAIL_PTR(EIP197_CS_RC_MAX - 1);
  71. writel(val, priv->base + EIP197_TRC_FREECHAIN);
  72. /* Configure the record cache #1 */
  73. val = EIP197_TRC_PARAMS2_RC_SZ_SMALL(EIP197_CS_TRC_REC_WC) |
  74. EIP197_TRC_PARAMS2_HTABLE_PTR(EIP197_CS_RC_MAX);
  75. writel(val, priv->base + EIP197_TRC_PARAMS2);
  76. /* Configure the record cache #2 */
  77. val = EIP197_TRC_PARAMS_RC_SZ_LARGE(EIP197_CS_TRC_LG_REC_WC) |
  78. EIP197_TRC_PARAMS_BLK_TIMER_SPEED(1) |
  79. EIP197_TRC_PARAMS_HTABLE_SZ(2);
  80. writel(val, priv->base + EIP197_TRC_PARAMS);
  81. }
  82. static void eip197_write_firmware(struct safexcel_crypto_priv *priv,
  83. const struct firmware *fw, u32 ctrl,
  84. u32 prog_en)
  85. {
  86. const u32 *data = (const u32 *)fw->data;
  87. u32 val;
  88. int i;
  89. /* Reset the engine to make its program memory accessible */
  90. writel(EIP197_PE_ICE_x_CTRL_SW_RESET |
  91. EIP197_PE_ICE_x_CTRL_CLR_ECC_CORR |
  92. EIP197_PE_ICE_x_CTRL_CLR_ECC_NON_CORR,
  93. priv->base + ctrl);
  94. /* Enable access to the program memory */
  95. writel(prog_en, priv->base + EIP197_PE_ICE_RAM_CTRL);
  96. /* Write the firmware */
  97. for (i = 0; i < fw->size / sizeof(u32); i++)
  98. writel(be32_to_cpu(data[i]),
  99. priv->base + EIP197_CLASSIFICATION_RAMS + i * sizeof(u32));
  100. /* Disable access to the program memory */
  101. writel(0, priv->base + EIP197_PE_ICE_RAM_CTRL);
  102. /* Release engine from reset */
  103. val = readl(priv->base + ctrl);
  104. val &= ~EIP197_PE_ICE_x_CTRL_SW_RESET;
  105. writel(val, priv->base + ctrl);
  106. }
  107. static int eip197_load_firmwares(struct safexcel_crypto_priv *priv)
  108. {
  109. const char *fw_name[] = {"ifpp.bin", "ipue.bin"};
  110. const struct firmware *fw[FW_NB];
  111. int i, j, ret = 0;
  112. u32 val;
  113. for (i = 0; i < FW_NB; i++) {
  114. ret = request_firmware(&fw[i], fw_name[i], priv->dev);
  115. if (ret) {
  116. dev_err(priv->dev,
  117. "Failed to request firmware %s (%d)\n",
  118. fw_name[i], ret);
  119. goto release_fw;
  120. }
  121. }
  122. /* Clear the scratchpad memory */
  123. val = readl(priv->base + EIP197_PE_ICE_SCRATCH_CTRL);
  124. val |= EIP197_PE_ICE_SCRATCH_CTRL_CHANGE_TIMER |
  125. EIP197_PE_ICE_SCRATCH_CTRL_TIMER_EN |
  126. EIP197_PE_ICE_SCRATCH_CTRL_SCRATCH_ACCESS |
  127. EIP197_PE_ICE_SCRATCH_CTRL_CHANGE_ACCESS;
  128. writel(val, priv->base + EIP197_PE_ICE_SCRATCH_CTRL);
  129. memset(priv->base + EIP197_PE_ICE_SCRATCH_RAM, 0,
  130. EIP197_NUM_OF_SCRATCH_BLOCKS * sizeof(u32));
  131. eip197_write_firmware(priv, fw[FW_IFPP], EIP197_PE_ICE_FPP_CTRL,
  132. EIP197_PE_ICE_RAM_CTRL_FPP_PROG_EN);
  133. eip197_write_firmware(priv, fw[FW_IPUE], EIP197_PE_ICE_PUE_CTRL,
  134. EIP197_PE_ICE_RAM_CTRL_PUE_PROG_EN);
  135. release_fw:
  136. for (j = 0; j < i; j++)
  137. release_firmware(fw[j]);
  138. return ret;
  139. }
  140. static int safexcel_hw_setup_cdesc_rings(struct safexcel_crypto_priv *priv)
  141. {
  142. u32 hdw, cd_size_rnd, val;
  143. int i;
  144. hdw = readl(priv->base + EIP197_HIA_OPTIONS);
  145. hdw &= GENMASK(27, 25);
  146. hdw >>= 25;
  147. cd_size_rnd = (priv->config.cd_size + (BIT(hdw) - 1)) >> hdw;
  148. for (i = 0; i < priv->config.rings; i++) {
  149. /* ring base address */
  150. writel(lower_32_bits(priv->ring[i].cdr.base_dma),
  151. priv->base + EIP197_HIA_CDR(i) + EIP197_HIA_xDR_RING_BASE_ADDR_LO);
  152. writel(upper_32_bits(priv->ring[i].cdr.base_dma),
  153. priv->base + EIP197_HIA_CDR(i) + EIP197_HIA_xDR_RING_BASE_ADDR_HI);
  154. writel(EIP197_xDR_DESC_MODE_64BIT | (priv->config.cd_offset << 16) |
  155. priv->config.cd_size,
  156. priv->base + EIP197_HIA_CDR(i) + EIP197_HIA_xDR_DESC_SIZE);
  157. writel(((EIP197_FETCH_COUNT * (cd_size_rnd << hdw)) << 16) |
  158. (EIP197_FETCH_COUNT * priv->config.cd_offset),
  159. priv->base + EIP197_HIA_CDR(i) + EIP197_HIA_xDR_CFG);
  160. /* Configure DMA tx control */
  161. val = EIP197_HIA_xDR_CFG_WR_CACHE(WR_CACHE_3BITS);
  162. val |= EIP197_HIA_xDR_CFG_RD_CACHE(RD_CACHE_3BITS);
  163. writel(val,
  164. priv->base + EIP197_HIA_CDR(i) + EIP197_HIA_xDR_DMA_CFG);
  165. /* clear any pending interrupt */
  166. writel(GENMASK(5, 0),
  167. priv->base + EIP197_HIA_CDR(i) + EIP197_HIA_xDR_STAT);
  168. }
  169. return 0;
  170. }
  171. static int safexcel_hw_setup_rdesc_rings(struct safexcel_crypto_priv *priv)
  172. {
  173. u32 hdw, rd_size_rnd, val;
  174. int i;
  175. hdw = readl(priv->base + EIP197_HIA_OPTIONS);
  176. hdw &= GENMASK(27, 25);
  177. hdw >>= 25;
  178. rd_size_rnd = (priv->config.rd_size + (BIT(hdw) - 1)) >> hdw;
  179. for (i = 0; i < priv->config.rings; i++) {
  180. /* ring base address */
  181. writel(lower_32_bits(priv->ring[i].rdr.base_dma),
  182. priv->base + EIP197_HIA_RDR(i) + EIP197_HIA_xDR_RING_BASE_ADDR_LO);
  183. writel(upper_32_bits(priv->ring[i].rdr.base_dma),
  184. priv->base + EIP197_HIA_RDR(i) + EIP197_HIA_xDR_RING_BASE_ADDR_HI);
  185. writel(EIP197_xDR_DESC_MODE_64BIT | (priv->config.rd_offset << 16) |
  186. priv->config.rd_size,
  187. priv->base + EIP197_HIA_RDR(i) + EIP197_HIA_xDR_DESC_SIZE);
  188. writel(((EIP197_FETCH_COUNT * (rd_size_rnd << hdw)) << 16) |
  189. (EIP197_FETCH_COUNT * priv->config.rd_offset),
  190. priv->base + EIP197_HIA_RDR(i) + EIP197_HIA_xDR_CFG);
  191. /* Configure DMA tx control */
  192. val = EIP197_HIA_xDR_CFG_WR_CACHE(WR_CACHE_3BITS);
  193. val |= EIP197_HIA_xDR_CFG_RD_CACHE(RD_CACHE_3BITS);
  194. val |= EIP197_HIA_xDR_WR_RES_BUF | EIP197_HIA_xDR_WR_CTRL_BUG;
  195. writel(val,
  196. priv->base + EIP197_HIA_RDR(i) + EIP197_HIA_xDR_DMA_CFG);
  197. /* clear any pending interrupt */
  198. writel(GENMASK(7, 0),
  199. priv->base + EIP197_HIA_RDR(i) + EIP197_HIA_xDR_STAT);
  200. /* enable ring interrupt */
  201. val = readl(priv->base + EIP197_HIA_AIC_R_ENABLE_CTRL(i));
  202. val |= EIP197_RDR_IRQ(i);
  203. writel(val, priv->base + EIP197_HIA_AIC_R_ENABLE_CTRL(i));
  204. }
  205. return 0;
  206. }
  207. static int safexcel_hw_init(struct safexcel_crypto_priv *priv)
  208. {
  209. u32 version, val;
  210. int i, ret;
  211. /* Determine endianess and configure byte swap */
  212. version = readl(priv->base + EIP197_HIA_VERSION);
  213. val = readl(priv->base + EIP197_HIA_MST_CTRL);
  214. if ((version & 0xffff) == EIP197_HIA_VERSION_BE)
  215. val |= EIP197_MST_CTRL_BYTE_SWAP;
  216. else if (((version >> 16) & 0xffff) == EIP197_HIA_VERSION_LE)
  217. val |= (EIP197_MST_CTRL_NO_BYTE_SWAP >> 24);
  218. writel(val, priv->base + EIP197_HIA_MST_CTRL);
  219. /* Configure wr/rd cache values */
  220. writel(EIP197_MST_CTRL_RD_CACHE(RD_CACHE_4BITS) |
  221. EIP197_MST_CTRL_WD_CACHE(WR_CACHE_4BITS),
  222. priv->base + EIP197_MST_CTRL);
  223. /* Interrupts reset */
  224. /* Disable all global interrupts */
  225. writel(0, priv->base + EIP197_HIA_AIC_G_ENABLE_CTRL);
  226. /* Clear any pending interrupt */
  227. writel(GENMASK(31, 0), priv->base + EIP197_HIA_AIC_G_ACK);
  228. /* Data Fetch Engine configuration */
  229. /* Reset all DFE threads */
  230. writel(EIP197_DxE_THR_CTRL_RESET_PE,
  231. priv->base + EIP197_HIA_DFE_THR_CTRL);
  232. /* Reset HIA input interface arbiter */
  233. writel(EIP197_HIA_RA_PE_CTRL_RESET,
  234. priv->base + EIP197_HIA_RA_PE_CTRL);
  235. /* DMA transfer size to use */
  236. val = EIP197_HIA_DFE_CFG_DIS_DEBUG;
  237. val |= EIP197_HIA_DxE_CFG_MIN_DATA_SIZE(5) | EIP197_HIA_DxE_CFG_MAX_DATA_SIZE(9);
  238. val |= EIP197_HIA_DxE_CFG_MIN_CTRL_SIZE(5) | EIP197_HIA_DxE_CFG_MAX_CTRL_SIZE(7);
  239. val |= EIP197_HIA_DxE_CFG_DATA_CACHE_CTRL(RD_CACHE_3BITS);
  240. val |= EIP197_HIA_DxE_CFG_CTRL_CACHE_CTRL(RD_CACHE_3BITS);
  241. writel(val, priv->base + EIP197_HIA_DFE_CFG);
  242. /* Leave the DFE threads reset state */
  243. writel(0, priv->base + EIP197_HIA_DFE_THR_CTRL);
  244. /* Configure the procesing engine thresholds */
  245. writel(EIP197_PE_IN_xBUF_THRES_MIN(5) | EIP197_PE_IN_xBUF_THRES_MAX(9),
  246. priv->base + EIP197_PE_IN_DBUF_THRES);
  247. writel(EIP197_PE_IN_xBUF_THRES_MIN(5) | EIP197_PE_IN_xBUF_THRES_MAX(7),
  248. priv->base + EIP197_PE_IN_TBUF_THRES);
  249. /* enable HIA input interface arbiter and rings */
  250. writel(EIP197_HIA_RA_PE_CTRL_EN | GENMASK(priv->config.rings - 1, 0),
  251. priv->base + EIP197_HIA_RA_PE_CTRL);
  252. /* Data Store Engine configuration */
  253. /* Reset all DSE threads */
  254. writel(EIP197_DxE_THR_CTRL_RESET_PE,
  255. priv->base + EIP197_HIA_DSE_THR_CTRL);
  256. /* Wait for all DSE threads to complete */
  257. while ((readl(priv->base + EIP197_HIA_DSE_THR_STAT) &
  258. GENMASK(15, 12)) != GENMASK(15, 12))
  259. ;
  260. /* DMA transfer size to use */
  261. val = EIP197_HIA_DSE_CFG_DIS_DEBUG;
  262. val |= EIP197_HIA_DxE_CFG_MIN_DATA_SIZE(7) | EIP197_HIA_DxE_CFG_MAX_DATA_SIZE(8);
  263. val |= EIP197_HIA_DxE_CFG_DATA_CACHE_CTRL(WR_CACHE_3BITS);
  264. val |= EIP197_HIA_DSE_CFG_ALLWAYS_BUFFERABLE;
  265. val |= EIP197_HIA_DSE_CFG_EN_SINGLE_WR;
  266. writel(val, priv->base + EIP197_HIA_DSE_CFG);
  267. /* Leave the DSE threads reset state */
  268. writel(0, priv->base + EIP197_HIA_DSE_THR_CTRL);
  269. /* Configure the procesing engine thresholds */
  270. writel(EIP197_PE_OUT_DBUF_THRES_MIN(7) | EIP197_PE_OUT_DBUF_THRES_MAX(8),
  271. priv->base + EIP197_PE_OUT_DBUF_THRES);
  272. /* Processing Engine configuration */
  273. /* H/W capabilities selection */
  274. val = EIP197_FUNCTION_RSVD;
  275. val |= EIP197_PROTOCOL_ENCRYPT_ONLY | EIP197_PROTOCOL_HASH_ONLY;
  276. val |= EIP197_ALG_AES_ECB | EIP197_ALG_AES_CBC;
  277. val |= EIP197_ALG_SHA1 | EIP197_ALG_HMAC_SHA1;
  278. val |= EIP197_ALG_SHA2;
  279. writel(val, priv->base + EIP197_PE_EIP96_FUNCTION_EN);
  280. /* Command Descriptor Rings prepare */
  281. for (i = 0; i < priv->config.rings; i++) {
  282. /* Clear interrupts for this ring */
  283. writel(GENMASK(31, 0),
  284. priv->base + EIP197_HIA_AIC_R_ENABLE_CLR(i));
  285. /* Disable external triggering */
  286. writel(0, priv->base + EIP197_HIA_CDR(i) + EIP197_HIA_xDR_CFG);
  287. /* Clear the pending prepared counter */
  288. writel(EIP197_xDR_PREP_CLR_COUNT,
  289. priv->base + EIP197_HIA_CDR(i) + EIP197_HIA_xDR_PREP_COUNT);
  290. /* Clear the pending processed counter */
  291. writel(EIP197_xDR_PROC_CLR_COUNT,
  292. priv->base + EIP197_HIA_CDR(i) + EIP197_HIA_xDR_PROC_COUNT);
  293. writel(0,
  294. priv->base + EIP197_HIA_CDR(i) + EIP197_HIA_xDR_PREP_PNTR);
  295. writel(0,
  296. priv->base + EIP197_HIA_CDR(i) + EIP197_HIA_xDR_PROC_PNTR);
  297. writel((EIP197_DEFAULT_RING_SIZE * priv->config.cd_offset) << 2,
  298. priv->base + EIP197_HIA_CDR(i) + EIP197_HIA_xDR_RING_SIZE);
  299. }
  300. /* Result Descriptor Ring prepare */
  301. for (i = 0; i < priv->config.rings; i++) {
  302. /* Disable external triggering*/
  303. writel(0, priv->base + EIP197_HIA_RDR(i) + EIP197_HIA_xDR_CFG);
  304. /* Clear the pending prepared counter */
  305. writel(EIP197_xDR_PREP_CLR_COUNT,
  306. priv->base + EIP197_HIA_RDR(i) + EIP197_HIA_xDR_PREP_COUNT);
  307. /* Clear the pending processed counter */
  308. writel(EIP197_xDR_PROC_CLR_COUNT,
  309. priv->base + EIP197_HIA_RDR(i) + EIP197_HIA_xDR_PROC_COUNT);
  310. writel(0,
  311. priv->base + EIP197_HIA_RDR(i) + EIP197_HIA_xDR_PREP_PNTR);
  312. writel(0,
  313. priv->base + EIP197_HIA_RDR(i) + EIP197_HIA_xDR_PROC_PNTR);
  314. /* Ring size */
  315. writel((EIP197_DEFAULT_RING_SIZE * priv->config.rd_offset) << 2,
  316. priv->base + EIP197_HIA_RDR(i) + EIP197_HIA_xDR_RING_SIZE);
  317. }
  318. /* Enable command descriptor rings */
  319. writel(EIP197_DxE_THR_CTRL_EN | GENMASK(priv->config.rings - 1, 0),
  320. priv->base + EIP197_HIA_DFE_THR_CTRL);
  321. /* Enable result descriptor rings */
  322. writel(EIP197_DxE_THR_CTRL_EN | GENMASK(priv->config.rings - 1, 0),
  323. priv->base + EIP197_HIA_DSE_THR_CTRL);
  324. /* Clear any HIA interrupt */
  325. writel(GENMASK(30, 20), priv->base + EIP197_HIA_AIC_G_ACK);
  326. eip197_trc_cache_init(priv);
  327. ret = eip197_load_firmwares(priv);
  328. if (ret)
  329. return ret;
  330. safexcel_hw_setup_cdesc_rings(priv);
  331. safexcel_hw_setup_rdesc_rings(priv);
  332. return 0;
  333. }
  334. void safexcel_dequeue(struct safexcel_crypto_priv *priv, int ring)
  335. {
  336. struct crypto_async_request *req, *backlog;
  337. struct safexcel_context *ctx;
  338. struct safexcel_request *request;
  339. int ret, nreq = 0, cdesc = 0, rdesc = 0, commands, results;
  340. priv->ring[ring].need_dequeue = false;
  341. do {
  342. spin_lock_bh(&priv->ring[ring].queue_lock);
  343. backlog = crypto_get_backlog(&priv->ring[ring].queue);
  344. req = crypto_dequeue_request(&priv->ring[ring].queue);
  345. spin_unlock_bh(&priv->ring[ring].queue_lock);
  346. if (!req)
  347. goto finalize;
  348. request = kzalloc(sizeof(*request), EIP197_GFP_FLAGS(*req));
  349. if (!request) {
  350. spin_lock_bh(&priv->ring[ring].queue_lock);
  351. crypto_enqueue_request(&priv->ring[ring].queue, req);
  352. spin_unlock_bh(&priv->ring[ring].queue_lock);
  353. priv->ring[ring].need_dequeue = true;
  354. goto finalize;
  355. }
  356. ctx = crypto_tfm_ctx(req->tfm);
  357. ret = ctx->send(req, ring, request, &commands, &results);
  358. if (ret) {
  359. kfree(request);
  360. req->complete(req, ret);
  361. priv->ring[ring].need_dequeue = true;
  362. goto finalize;
  363. }
  364. if (backlog)
  365. backlog->complete(backlog, -EINPROGRESS);
  366. /* In case the send() helper did not issue any command to push
  367. * to the engine because the input data was cached, continue to
  368. * dequeue other requests as this is valid and not an error.
  369. */
  370. if (!commands && !results) {
  371. kfree(request);
  372. continue;
  373. }
  374. spin_lock_bh(&priv->ring[ring].egress_lock);
  375. list_add_tail(&request->list, &priv->ring[ring].list);
  376. spin_unlock_bh(&priv->ring[ring].egress_lock);
  377. cdesc += commands;
  378. rdesc += results;
  379. } while (nreq++ < EIP197_MAX_BATCH_SZ);
  380. finalize:
  381. if (nreq == EIP197_MAX_BATCH_SZ)
  382. priv->ring[ring].need_dequeue = true;
  383. else if (!nreq)
  384. return;
  385. spin_lock_bh(&priv->ring[ring].lock);
  386. /* Configure when we want an interrupt */
  387. writel(EIP197_HIA_RDR_THRESH_PKT_MODE |
  388. EIP197_HIA_RDR_THRESH_PROC_PKT(nreq),
  389. priv->base + EIP197_HIA_RDR(ring) + EIP197_HIA_xDR_THRESH);
  390. /* let the RDR know we have pending descriptors */
  391. writel((rdesc * priv->config.rd_offset) << 2,
  392. priv->base + EIP197_HIA_RDR(ring) + EIP197_HIA_xDR_PREP_COUNT);
  393. /* let the CDR know we have pending descriptors */
  394. writel((cdesc * priv->config.cd_offset) << 2,
  395. priv->base + EIP197_HIA_CDR(ring) + EIP197_HIA_xDR_PREP_COUNT);
  396. spin_unlock_bh(&priv->ring[ring].lock);
  397. }
  398. void safexcel_free_context(struct safexcel_crypto_priv *priv,
  399. struct crypto_async_request *req,
  400. int result_sz)
  401. {
  402. struct safexcel_context *ctx = crypto_tfm_ctx(req->tfm);
  403. if (ctx->result_dma)
  404. dma_unmap_single(priv->dev, ctx->result_dma, result_sz,
  405. DMA_FROM_DEVICE);
  406. if (ctx->cache) {
  407. dma_unmap_single(priv->dev, ctx->cache_dma, ctx->cache_sz,
  408. DMA_TO_DEVICE);
  409. kfree(ctx->cache);
  410. ctx->cache = NULL;
  411. ctx->cache_sz = 0;
  412. }
  413. }
  414. void safexcel_complete(struct safexcel_crypto_priv *priv, int ring)
  415. {
  416. struct safexcel_command_desc *cdesc;
  417. /* Acknowledge the command descriptors */
  418. do {
  419. cdesc = safexcel_ring_next_rptr(priv, &priv->ring[ring].cdr);
  420. if (IS_ERR(cdesc)) {
  421. dev_err(priv->dev,
  422. "Could not retrieve the command descriptor\n");
  423. return;
  424. }
  425. } while (!cdesc->last_seg);
  426. }
  427. void safexcel_inv_complete(struct crypto_async_request *req, int error)
  428. {
  429. struct safexcel_inv_result *result = req->data;
  430. if (error == -EINPROGRESS)
  431. return;
  432. result->error = error;
  433. complete(&result->completion);
  434. }
  435. int safexcel_invalidate_cache(struct crypto_async_request *async,
  436. struct safexcel_context *ctx,
  437. struct safexcel_crypto_priv *priv,
  438. dma_addr_t ctxr_dma, int ring,
  439. struct safexcel_request *request)
  440. {
  441. struct safexcel_command_desc *cdesc;
  442. struct safexcel_result_desc *rdesc;
  443. int ret = 0;
  444. spin_lock_bh(&priv->ring[ring].egress_lock);
  445. /* Prepare command descriptor */
  446. cdesc = safexcel_add_cdesc(priv, ring, true, true, 0, 0, 0, ctxr_dma);
  447. if (IS_ERR(cdesc)) {
  448. ret = PTR_ERR(cdesc);
  449. goto unlock;
  450. }
  451. cdesc->control_data.type = EIP197_TYPE_EXTENDED;
  452. cdesc->control_data.options = 0;
  453. cdesc->control_data.refresh = 0;
  454. cdesc->control_data.control0 = CONTEXT_CONTROL_INV_TR;
  455. /* Prepare result descriptor */
  456. rdesc = safexcel_add_rdesc(priv, ring, true, true, 0, 0);
  457. if (IS_ERR(rdesc)) {
  458. ret = PTR_ERR(rdesc);
  459. goto cdesc_rollback;
  460. }
  461. request->req = async;
  462. goto unlock;
  463. cdesc_rollback:
  464. safexcel_ring_rollback_wptr(priv, &priv->ring[ring].cdr);
  465. unlock:
  466. spin_unlock_bh(&priv->ring[ring].egress_lock);
  467. return ret;
  468. }
  469. static inline void safexcel_handle_result_descriptor(struct safexcel_crypto_priv *priv,
  470. int ring)
  471. {
  472. struct safexcel_request *sreq;
  473. struct safexcel_context *ctx;
  474. int ret, i, nreq, ndesc = 0;
  475. bool should_complete;
  476. nreq = readl(priv->base + EIP197_HIA_RDR(ring) + EIP197_HIA_xDR_PROC_COUNT);
  477. nreq >>= 24;
  478. nreq &= GENMASK(6, 0);
  479. if (!nreq)
  480. return;
  481. for (i = 0; i < nreq; i++) {
  482. spin_lock_bh(&priv->ring[ring].egress_lock);
  483. sreq = list_first_entry(&priv->ring[ring].list,
  484. struct safexcel_request, list);
  485. list_del(&sreq->list);
  486. spin_unlock_bh(&priv->ring[ring].egress_lock);
  487. ctx = crypto_tfm_ctx(sreq->req->tfm);
  488. ndesc = ctx->handle_result(priv, ring, sreq->req,
  489. &should_complete, &ret);
  490. if (ndesc < 0) {
  491. kfree(sreq);
  492. dev_err(priv->dev, "failed to handle result (%d)", ndesc);
  493. return;
  494. }
  495. writel(EIP197_xDR_PROC_xD_PKT(1) |
  496. EIP197_xDR_PROC_xD_COUNT(ndesc * priv->config.rd_offset),
  497. priv->base + EIP197_HIA_RDR(ring) + EIP197_HIA_xDR_PROC_COUNT);
  498. if (should_complete) {
  499. local_bh_disable();
  500. sreq->req->complete(sreq->req, ret);
  501. local_bh_enable();
  502. }
  503. kfree(sreq);
  504. }
  505. }
  506. static void safexcel_handle_result_work(struct work_struct *work)
  507. {
  508. struct safexcel_work_data *data =
  509. container_of(work, struct safexcel_work_data, work);
  510. struct safexcel_crypto_priv *priv = data->priv;
  511. safexcel_handle_result_descriptor(priv, data->ring);
  512. if (priv->ring[data->ring].need_dequeue)
  513. safexcel_dequeue(data->priv, data->ring);
  514. }
  515. struct safexcel_ring_irq_data {
  516. struct safexcel_crypto_priv *priv;
  517. int ring;
  518. };
  519. static irqreturn_t safexcel_irq_ring(int irq, void *data)
  520. {
  521. struct safexcel_ring_irq_data *irq_data = data;
  522. struct safexcel_crypto_priv *priv = irq_data->priv;
  523. int ring = irq_data->ring;
  524. u32 status, stat;
  525. status = readl(priv->base + EIP197_HIA_AIC_R_ENABLED_STAT(ring));
  526. if (!status)
  527. return IRQ_NONE;
  528. /* RDR interrupts */
  529. if (status & EIP197_RDR_IRQ(ring)) {
  530. stat = readl(priv->base + EIP197_HIA_RDR(ring) + EIP197_HIA_xDR_STAT);
  531. if (unlikely(stat & EIP197_xDR_ERR)) {
  532. /*
  533. * Fatal error, the RDR is unusable and must be
  534. * reinitialized. This should not happen under
  535. * normal circumstances.
  536. */
  537. dev_err(priv->dev, "RDR: fatal error.");
  538. } else if (likely(stat & EIP197_xDR_THRESH)) {
  539. queue_work(priv->ring[ring].workqueue, &priv->ring[ring].work_data.work);
  540. }
  541. /* ACK the interrupts */
  542. writel(stat & 0xff,
  543. priv->base + EIP197_HIA_RDR(ring) + EIP197_HIA_xDR_STAT);
  544. }
  545. /* ACK the interrupts */
  546. writel(status, priv->base + EIP197_HIA_AIC_R_ACK(ring));
  547. return IRQ_HANDLED;
  548. }
  549. static int safexcel_request_ring_irq(struct platform_device *pdev, const char *name,
  550. irq_handler_t handler,
  551. struct safexcel_ring_irq_data *ring_irq_priv)
  552. {
  553. int ret, irq = platform_get_irq_byname(pdev, name);
  554. if (irq < 0) {
  555. dev_err(&pdev->dev, "unable to get IRQ '%s'\n", name);
  556. return irq;
  557. }
  558. ret = devm_request_irq(&pdev->dev, irq, handler, 0,
  559. dev_name(&pdev->dev), ring_irq_priv);
  560. if (ret) {
  561. dev_err(&pdev->dev, "unable to request IRQ %d\n", irq);
  562. return ret;
  563. }
  564. return irq;
  565. }
  566. static struct safexcel_alg_template *safexcel_algs[] = {
  567. &safexcel_alg_ecb_aes,
  568. &safexcel_alg_cbc_aes,
  569. &safexcel_alg_sha1,
  570. &safexcel_alg_sha224,
  571. &safexcel_alg_sha256,
  572. &safexcel_alg_hmac_sha1,
  573. };
  574. static int safexcel_register_algorithms(struct safexcel_crypto_priv *priv)
  575. {
  576. int i, j, ret = 0;
  577. for (i = 0; i < ARRAY_SIZE(safexcel_algs); i++) {
  578. safexcel_algs[i]->priv = priv;
  579. if (safexcel_algs[i]->type == SAFEXCEL_ALG_TYPE_SKCIPHER)
  580. ret = crypto_register_skcipher(&safexcel_algs[i]->alg.skcipher);
  581. else
  582. ret = crypto_register_ahash(&safexcel_algs[i]->alg.ahash);
  583. if (ret)
  584. goto fail;
  585. }
  586. return 0;
  587. fail:
  588. for (j = 0; j < i; j++) {
  589. if (safexcel_algs[j]->type == SAFEXCEL_ALG_TYPE_SKCIPHER)
  590. crypto_unregister_skcipher(&safexcel_algs[j]->alg.skcipher);
  591. else
  592. crypto_unregister_ahash(&safexcel_algs[j]->alg.ahash);
  593. }
  594. return ret;
  595. }
  596. static void safexcel_unregister_algorithms(struct safexcel_crypto_priv *priv)
  597. {
  598. int i;
  599. for (i = 0; i < ARRAY_SIZE(safexcel_algs); i++) {
  600. if (safexcel_algs[i]->type == SAFEXCEL_ALG_TYPE_SKCIPHER)
  601. crypto_unregister_skcipher(&safexcel_algs[i]->alg.skcipher);
  602. else
  603. crypto_unregister_ahash(&safexcel_algs[i]->alg.ahash);
  604. }
  605. }
  606. static void safexcel_configure(struct safexcel_crypto_priv *priv)
  607. {
  608. u32 val, mask;
  609. val = readl(priv->base + EIP197_HIA_OPTIONS);
  610. val = (val & GENMASK(27, 25)) >> 25;
  611. mask = BIT(val) - 1;
  612. val = readl(priv->base + EIP197_HIA_OPTIONS);
  613. priv->config.rings = min_t(u32, val & GENMASK(3, 0), max_rings);
  614. priv->config.cd_size = (sizeof(struct safexcel_command_desc) / sizeof(u32));
  615. priv->config.cd_offset = (priv->config.cd_size + mask) & ~mask;
  616. priv->config.rd_size = (sizeof(struct safexcel_result_desc) / sizeof(u32));
  617. priv->config.rd_offset = (priv->config.rd_size + mask) & ~mask;
  618. }
  619. static int safexcel_probe(struct platform_device *pdev)
  620. {
  621. struct device *dev = &pdev->dev;
  622. struct resource *res;
  623. struct safexcel_crypto_priv *priv;
  624. int i, ret;
  625. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  626. if (!priv)
  627. return -ENOMEM;
  628. priv->dev = dev;
  629. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  630. priv->base = devm_ioremap_resource(dev, res);
  631. if (IS_ERR(priv->base)) {
  632. dev_err(dev, "failed to get resource\n");
  633. return PTR_ERR(priv->base);
  634. }
  635. priv->clk = devm_clk_get(&pdev->dev, NULL);
  636. if (!IS_ERR(priv->clk)) {
  637. ret = clk_prepare_enable(priv->clk);
  638. if (ret) {
  639. dev_err(dev, "unable to enable clk (%d)\n", ret);
  640. return ret;
  641. }
  642. } else {
  643. /* The clock isn't mandatory */
  644. if (PTR_ERR(priv->clk) == -EPROBE_DEFER)
  645. return -EPROBE_DEFER;
  646. }
  647. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
  648. if (ret)
  649. goto err_clk;
  650. priv->context_pool = dmam_pool_create("safexcel-context", dev,
  651. sizeof(struct safexcel_context_record),
  652. 1, 0);
  653. if (!priv->context_pool) {
  654. ret = -ENOMEM;
  655. goto err_clk;
  656. }
  657. safexcel_configure(priv);
  658. for (i = 0; i < priv->config.rings; i++) {
  659. char irq_name[6] = {0}; /* "ringX\0" */
  660. char wq_name[9] = {0}; /* "wq_ringX\0" */
  661. int irq;
  662. struct safexcel_ring_irq_data *ring_irq;
  663. ret = safexcel_init_ring_descriptors(priv,
  664. &priv->ring[i].cdr,
  665. &priv->ring[i].rdr);
  666. if (ret)
  667. goto err_clk;
  668. ring_irq = devm_kzalloc(dev, sizeof(*ring_irq), GFP_KERNEL);
  669. if (!ring_irq) {
  670. ret = -ENOMEM;
  671. goto err_clk;
  672. }
  673. ring_irq->priv = priv;
  674. ring_irq->ring = i;
  675. snprintf(irq_name, 6, "ring%d", i);
  676. irq = safexcel_request_ring_irq(pdev, irq_name, safexcel_irq_ring,
  677. ring_irq);
  678. if (irq < 0) {
  679. ret = irq;
  680. goto err_clk;
  681. }
  682. priv->ring[i].work_data.priv = priv;
  683. priv->ring[i].work_data.ring = i;
  684. INIT_WORK(&priv->ring[i].work_data.work, safexcel_handle_result_work);
  685. snprintf(wq_name, 9, "wq_ring%d", i);
  686. priv->ring[i].workqueue = create_singlethread_workqueue(wq_name);
  687. if (!priv->ring[i].workqueue) {
  688. ret = -ENOMEM;
  689. goto err_clk;
  690. }
  691. crypto_init_queue(&priv->ring[i].queue,
  692. EIP197_DEFAULT_RING_SIZE);
  693. INIT_LIST_HEAD(&priv->ring[i].list);
  694. spin_lock_init(&priv->ring[i].lock);
  695. spin_lock_init(&priv->ring[i].egress_lock);
  696. spin_lock_init(&priv->ring[i].queue_lock);
  697. }
  698. platform_set_drvdata(pdev, priv);
  699. atomic_set(&priv->ring_used, 0);
  700. ret = safexcel_hw_init(priv);
  701. if (ret) {
  702. dev_err(dev, "EIP h/w init failed (%d)\n", ret);
  703. goto err_clk;
  704. }
  705. ret = safexcel_register_algorithms(priv);
  706. if (ret) {
  707. dev_err(dev, "Failed to register algorithms (%d)\n", ret);
  708. goto err_clk;
  709. }
  710. return 0;
  711. err_clk:
  712. clk_disable_unprepare(priv->clk);
  713. return ret;
  714. }
  715. static int safexcel_remove(struct platform_device *pdev)
  716. {
  717. struct safexcel_crypto_priv *priv = platform_get_drvdata(pdev);
  718. int i;
  719. safexcel_unregister_algorithms(priv);
  720. clk_disable_unprepare(priv->clk);
  721. for (i = 0; i < priv->config.rings; i++)
  722. destroy_workqueue(priv->ring[i].workqueue);
  723. return 0;
  724. }
  725. static const struct of_device_id safexcel_of_match_table[] = {
  726. { .compatible = "inside-secure,safexcel-eip197" },
  727. {},
  728. };
  729. static struct platform_driver crypto_safexcel = {
  730. .probe = safexcel_probe,
  731. .remove = safexcel_remove,
  732. .driver = {
  733. .name = "crypto-safexcel",
  734. .of_match_table = safexcel_of_match_table,
  735. },
  736. };
  737. module_platform_driver(crypto_safexcel);
  738. MODULE_AUTHOR("Antoine Tenart <antoine.tenart@free-electrons.com>");
  739. MODULE_AUTHOR("Ofer Heifetz <oferh@marvell.com>");
  740. MODULE_AUTHOR("Igal Liberman <igall@marvell.com>");
  741. MODULE_DESCRIPTION("Support for SafeXcel cryptographic engine EIP197");
  742. MODULE_LICENSE("GPL v2");