Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Support for Marvell's Cryptographic Engine and Security Accelerator (CESA) |
| 4 | * that can be found on the following platform: Orion, Kirkwood, Armada. This |
| 5 | * driver supports the TDMA engine on platforms on which it is available. |
| 6 | * |
| 7 | * Author: Boris Brezillon <boris.brezillon@free-electrons.com> |
| 8 | * Author: Arnaud Ebalard <arno@natisbad.org> |
| 9 | * |
| 10 | * This work is based on an initial version written by |
| 11 | * Sebastian Andrzej Siewior < sebastian at breakpoint dot cc > |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <linux/delay.h> |
Robin Murphy | 37d728f | 2018-01-10 15:15:43 +0000 | [diff] [blame] | 15 | #include <linux/dma-mapping.h> |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 16 | #include <linux/genalloc.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/kthread.h> |
| 20 | #include <linux/mbus.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/scatterlist.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/clk.h> |
| 26 | #include <linux/of.h> |
| 27 | #include <linux/of_platform.h> |
| 28 | #include <linux/of_irq.h> |
| 29 | |
| 30 | #include "cesa.h" |
| 31 | |
Romain Perier | e26df73 | 2016-06-21 10:08:31 +0200 | [diff] [blame] | 32 | /* Limit of the crypto queue before reaching the backlog */ |
Romain Perier | 47a1f0b | 2016-06-21 10:08:40 +0200 | [diff] [blame] | 33 | #define CESA_CRYPTO_DEFAULT_MAX_QLEN 128 |
Romain Perier | e26df73 | 2016-06-21 10:08:31 +0200 | [diff] [blame] | 34 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 35 | struct mv_cesa_dev *cesa_dev; |
| 36 | |
Romain Perier | 85030c5 | 2016-06-21 10:08:39 +0200 | [diff] [blame] | 37 | struct crypto_async_request * |
| 38 | mv_cesa_dequeue_req_locked(struct mv_cesa_engine *engine, |
| 39 | struct crypto_async_request **backlog) |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 40 | { |
Romain Perier | 85030c5 | 2016-06-21 10:08:39 +0200 | [diff] [blame] | 41 | struct crypto_async_request *req; |
| 42 | |
| 43 | *backlog = crypto_get_backlog(&engine->queue); |
| 44 | req = crypto_dequeue_request(&engine->queue); |
| 45 | |
| 46 | if (!req) |
| 47 | return NULL; |
| 48 | |
| 49 | return req; |
| 50 | } |
| 51 | |
| 52 | static void mv_cesa_rearm_engine(struct mv_cesa_engine *engine) |
| 53 | { |
| 54 | struct crypto_async_request *req = NULL, *backlog = NULL; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 55 | struct mv_cesa_ctx *ctx; |
| 56 | |
Romain Perier | 85030c5 | 2016-06-21 10:08:39 +0200 | [diff] [blame] | 57 | |
| 58 | spin_lock_bh(&engine->lock); |
| 59 | if (!engine->req) { |
| 60 | req = mv_cesa_dequeue_req_locked(engine, &backlog); |
| 61 | engine->req = req; |
| 62 | } |
| 63 | spin_unlock_bh(&engine->lock); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 64 | |
| 65 | if (!req) |
| 66 | return; |
| 67 | |
| 68 | if (backlog) |
| 69 | backlog->complete(backlog, -EINPROGRESS); |
| 70 | |
| 71 | ctx = crypto_tfm_ctx(req->tfm); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 72 | ctx->ops->step(req); |
Romain Perier | 85030c5 | 2016-06-21 10:08:39 +0200 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static int mv_cesa_std_process(struct mv_cesa_engine *engine, u32 status) |
| 76 | { |
| 77 | struct crypto_async_request *req; |
| 78 | struct mv_cesa_ctx *ctx; |
| 79 | int res; |
| 80 | |
| 81 | req = engine->req; |
| 82 | ctx = crypto_tfm_ctx(req->tfm); |
| 83 | res = ctx->ops->process(req, status); |
| 84 | |
| 85 | if (res == 0) { |
| 86 | ctx->ops->complete(req); |
| 87 | mv_cesa_engine_enqueue_complete_request(engine, req); |
| 88 | } else if (res == -EINPROGRESS) { |
| 89 | ctx->ops->step(req); |
| 90 | } |
| 91 | |
| 92 | return res; |
| 93 | } |
| 94 | |
| 95 | static int mv_cesa_int_process(struct mv_cesa_engine *engine, u32 status) |
| 96 | { |
| 97 | if (engine->chain.first && engine->chain.last) |
| 98 | return mv_cesa_tdma_process(engine, status); |
| 99 | |
| 100 | return mv_cesa_std_process(engine, status); |
| 101 | } |
| 102 | |
| 103 | static inline void |
| 104 | mv_cesa_complete_req(struct mv_cesa_ctx *ctx, struct crypto_async_request *req, |
| 105 | int res) |
| 106 | { |
| 107 | ctx->ops->cleanup(req); |
| 108 | local_bh_disable(); |
| 109 | req->complete(req, res); |
| 110 | local_bh_enable(); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static irqreturn_t mv_cesa_int(int irq, void *priv) |
| 114 | { |
| 115 | struct mv_cesa_engine *engine = priv; |
| 116 | struct crypto_async_request *req; |
| 117 | struct mv_cesa_ctx *ctx; |
| 118 | u32 status, mask; |
| 119 | irqreturn_t ret = IRQ_NONE; |
| 120 | |
| 121 | while (true) { |
| 122 | int res; |
| 123 | |
| 124 | mask = mv_cesa_get_int_mask(engine); |
| 125 | status = readl(engine->regs + CESA_SA_INT_STATUS); |
| 126 | |
| 127 | if (!(status & mask)) |
| 128 | break; |
| 129 | |
| 130 | /* |
| 131 | * TODO: avoid clearing the FPGA_INT_STATUS if this not |
| 132 | * relevant on some platforms. |
| 133 | */ |
| 134 | writel(~status, engine->regs + CESA_SA_FPGA_INT_STATUS); |
| 135 | writel(~status, engine->regs + CESA_SA_INT_STATUS); |
| 136 | |
Romain Perier | 85030c5 | 2016-06-21 10:08:39 +0200 | [diff] [blame] | 137 | /* Process fetched requests */ |
| 138 | res = mv_cesa_int_process(engine, status & mask); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 139 | ret = IRQ_HANDLED; |
Romain Perier | 85030c5 | 2016-06-21 10:08:39 +0200 | [diff] [blame] | 140 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 141 | spin_lock_bh(&engine->lock); |
| 142 | req = engine->req; |
Romain Perier | 85030c5 | 2016-06-21 10:08:39 +0200 | [diff] [blame] | 143 | if (res != -EINPROGRESS) |
| 144 | engine->req = NULL; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 145 | spin_unlock_bh(&engine->lock); |
Romain Perier | 85030c5 | 2016-06-21 10:08:39 +0200 | [diff] [blame] | 146 | |
| 147 | ctx = crypto_tfm_ctx(req->tfm); |
| 148 | |
| 149 | if (res && res != -EINPROGRESS) |
| 150 | mv_cesa_complete_req(ctx, req, res); |
| 151 | |
| 152 | /* Launch the next pending request */ |
| 153 | mv_cesa_rearm_engine(engine); |
| 154 | |
| 155 | /* Iterate over the complete queue */ |
| 156 | while (true) { |
| 157 | req = mv_cesa_engine_dequeue_complete_request(engine); |
| 158 | if (!req) |
| 159 | break; |
| 160 | |
Romain Perier | 09951d8 | 2016-08-09 11:03:18 +0200 | [diff] [blame] | 161 | ctx = crypto_tfm_ctx(req->tfm); |
Romain Perier | 85030c5 | 2016-06-21 10:08:39 +0200 | [diff] [blame] | 162 | mv_cesa_complete_req(ctx, req, 0); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | return ret; |
| 167 | } |
| 168 | |
Romain Perier | 53da740 | 2016-06-21 10:08:35 +0200 | [diff] [blame] | 169 | int mv_cesa_queue_req(struct crypto_async_request *req, |
| 170 | struct mv_cesa_req *creq) |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 171 | { |
| 172 | int ret; |
Romain Perier | bf8f91e | 2016-06-21 10:08:38 +0200 | [diff] [blame] | 173 | struct mv_cesa_engine *engine = creq->engine; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 174 | |
Romain Perier | bf8f91e | 2016-06-21 10:08:38 +0200 | [diff] [blame] | 175 | spin_lock_bh(&engine->lock); |
| 176 | ret = crypto_enqueue_request(&engine->queue, req); |
Romain Perier | 603e989e | 2016-07-22 14:40:55 +0200 | [diff] [blame] | 177 | if ((mv_cesa_req_get_type(creq) == CESA_DMA_REQ) && |
Gilad Ben-Yossef | 3d549e3 | 2017-10-18 08:00:37 +0100 | [diff] [blame] | 178 | (ret == -EINPROGRESS || ret == -EBUSY)) |
Romain Perier | 603e989e | 2016-07-22 14:40:55 +0200 | [diff] [blame] | 179 | mv_cesa_tdma_chain(engine, creq); |
Romain Perier | bf8f91e | 2016-06-21 10:08:38 +0200 | [diff] [blame] | 180 | spin_unlock_bh(&engine->lock); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 181 | |
| 182 | if (ret != -EINPROGRESS) |
| 183 | return ret; |
| 184 | |
Romain Perier | 85030c5 | 2016-06-21 10:08:39 +0200 | [diff] [blame] | 185 | mv_cesa_rearm_engine(engine); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 186 | |
| 187 | return -EINPROGRESS; |
| 188 | } |
| 189 | |
| 190 | static int mv_cesa_add_algs(struct mv_cesa_dev *cesa) |
| 191 | { |
| 192 | int ret; |
| 193 | int i, j; |
| 194 | |
| 195 | for (i = 0; i < cesa->caps->ncipher_algs; i++) { |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 196 | ret = crypto_register_skcipher(cesa->caps->cipher_algs[i]); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 197 | if (ret) |
| 198 | goto err_unregister_crypto; |
| 199 | } |
| 200 | |
| 201 | for (i = 0; i < cesa->caps->nahash_algs; i++) { |
| 202 | ret = crypto_register_ahash(cesa->caps->ahash_algs[i]); |
| 203 | if (ret) |
| 204 | goto err_unregister_ahash; |
| 205 | } |
| 206 | |
| 207 | return 0; |
| 208 | |
| 209 | err_unregister_ahash: |
| 210 | for (j = 0; j < i; j++) |
| 211 | crypto_unregister_ahash(cesa->caps->ahash_algs[j]); |
| 212 | i = cesa->caps->ncipher_algs; |
| 213 | |
| 214 | err_unregister_crypto: |
| 215 | for (j = 0; j < i; j++) |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 216 | crypto_unregister_skcipher(cesa->caps->cipher_algs[j]); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 217 | |
| 218 | return ret; |
| 219 | } |
| 220 | |
| 221 | static void mv_cesa_remove_algs(struct mv_cesa_dev *cesa) |
| 222 | { |
| 223 | int i; |
| 224 | |
| 225 | for (i = 0; i < cesa->caps->nahash_algs; i++) |
| 226 | crypto_unregister_ahash(cesa->caps->ahash_algs[i]); |
| 227 | |
| 228 | for (i = 0; i < cesa->caps->ncipher_algs; i++) |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 229 | crypto_unregister_skcipher(cesa->caps->cipher_algs[i]); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 230 | } |
| 231 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 232 | static struct skcipher_alg *orion_cipher_algs[] = { |
Boris BREZILLON | 0bf6948 | 2015-06-18 15:46:28 +0200 | [diff] [blame] | 233 | &mv_cesa_ecb_des_alg, |
| 234 | &mv_cesa_cbc_des_alg, |
| 235 | &mv_cesa_ecb_des3_ede_alg, |
| 236 | &mv_cesa_cbc_des3_ede_alg, |
| 237 | &mv_cesa_ecb_aes_alg, |
| 238 | &mv_cesa_cbc_aes_alg, |
| 239 | }; |
| 240 | |
| 241 | static struct ahash_alg *orion_ahash_algs[] = { |
| 242 | &mv_md5_alg, |
| 243 | &mv_sha1_alg, |
| 244 | &mv_ahmac_md5_alg, |
| 245 | &mv_ahmac_sha1_alg, |
| 246 | }; |
| 247 | |
Boris BREZILLON | e6cd5bf | 2017-10-13 15:30:32 +0200 | [diff] [blame] | 248 | static struct skcipher_alg *armada_370_cipher_algs[] = { |
Boris BREZILLON | 7b3aaaa | 2015-06-18 15:46:22 +0200 | [diff] [blame] | 249 | &mv_cesa_ecb_des_alg, |
| 250 | &mv_cesa_cbc_des_alg, |
Arnaud Ebalard | 4ada483 | 2015-06-18 15:46:23 +0200 | [diff] [blame] | 251 | &mv_cesa_ecb_des3_ede_alg, |
| 252 | &mv_cesa_cbc_des3_ede_alg, |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 253 | &mv_cesa_ecb_aes_alg, |
| 254 | &mv_cesa_cbc_aes_alg, |
| 255 | }; |
| 256 | |
| 257 | static struct ahash_alg *armada_370_ahash_algs[] = { |
Arnaud Ebalard | 7aeef69 | 2015-06-18 15:46:24 +0200 | [diff] [blame] | 258 | &mv_md5_alg, |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 259 | &mv_sha1_alg, |
Arnaud Ebalard | f85a762 | 2015-06-18 15:46:25 +0200 | [diff] [blame] | 260 | &mv_sha256_alg, |
Arnaud Ebalard | 7aeef69 | 2015-06-18 15:46:24 +0200 | [diff] [blame] | 261 | &mv_ahmac_md5_alg, |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 262 | &mv_ahmac_sha1_alg, |
Arnaud Ebalard | f85a762 | 2015-06-18 15:46:25 +0200 | [diff] [blame] | 263 | &mv_ahmac_sha256_alg, |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 264 | }; |
| 265 | |
Boris BREZILLON | 0bf6948 | 2015-06-18 15:46:28 +0200 | [diff] [blame] | 266 | static const struct mv_cesa_caps orion_caps = { |
| 267 | .nengines = 1, |
| 268 | .cipher_algs = orion_cipher_algs, |
| 269 | .ncipher_algs = ARRAY_SIZE(orion_cipher_algs), |
| 270 | .ahash_algs = orion_ahash_algs, |
| 271 | .nahash_algs = ARRAY_SIZE(orion_ahash_algs), |
| 272 | .has_tdma = false, |
| 273 | }; |
| 274 | |
Arnaud Ebalard | 7240425 | 2015-06-18 15:46:29 +0200 | [diff] [blame] | 275 | static const struct mv_cesa_caps kirkwood_caps = { |
| 276 | .nengines = 1, |
| 277 | .cipher_algs = orion_cipher_algs, |
| 278 | .ncipher_algs = ARRAY_SIZE(orion_cipher_algs), |
| 279 | .ahash_algs = orion_ahash_algs, |
| 280 | .nahash_algs = ARRAY_SIZE(orion_ahash_algs), |
| 281 | .has_tdma = true, |
| 282 | }; |
| 283 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 284 | static const struct mv_cesa_caps armada_370_caps = { |
| 285 | .nengines = 1, |
| 286 | .cipher_algs = armada_370_cipher_algs, |
| 287 | .ncipher_algs = ARRAY_SIZE(armada_370_cipher_algs), |
| 288 | .ahash_algs = armada_370_ahash_algs, |
| 289 | .nahash_algs = ARRAY_SIZE(armada_370_ahash_algs), |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 290 | .has_tdma = true, |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 291 | }; |
| 292 | |
Boris BREZILLON | 898c9d5 | 2015-06-18 15:46:26 +0200 | [diff] [blame] | 293 | static const struct mv_cesa_caps armada_xp_caps = { |
| 294 | .nengines = 2, |
| 295 | .cipher_algs = armada_370_cipher_algs, |
| 296 | .ncipher_algs = ARRAY_SIZE(armada_370_cipher_algs), |
| 297 | .ahash_algs = armada_370_ahash_algs, |
| 298 | .nahash_algs = ARRAY_SIZE(armada_370_ahash_algs), |
| 299 | .has_tdma = true, |
| 300 | }; |
| 301 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 302 | static const struct of_device_id mv_cesa_of_match_table[] = { |
Boris BREZILLON | 0bf6948 | 2015-06-18 15:46:28 +0200 | [diff] [blame] | 303 | { .compatible = "marvell,orion-crypto", .data = &orion_caps }, |
Arnaud Ebalard | 7240425 | 2015-06-18 15:46:29 +0200 | [diff] [blame] | 304 | { .compatible = "marvell,kirkwood-crypto", .data = &kirkwood_caps }, |
| 305 | { .compatible = "marvell,dove-crypto", .data = &kirkwood_caps }, |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 306 | { .compatible = "marvell,armada-370-crypto", .data = &armada_370_caps }, |
Boris BREZILLON | 898c9d5 | 2015-06-18 15:46:26 +0200 | [diff] [blame] | 307 | { .compatible = "marvell,armada-xp-crypto", .data = &armada_xp_caps }, |
| 308 | { .compatible = "marvell,armada-375-crypto", .data = &armada_xp_caps }, |
| 309 | { .compatible = "marvell,armada-38x-crypto", .data = &armada_xp_caps }, |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 310 | {} |
| 311 | }; |
| 312 | MODULE_DEVICE_TABLE(of, mv_cesa_of_match_table); |
| 313 | |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 314 | static void |
| 315 | mv_cesa_conf_mbus_windows(struct mv_cesa_engine *engine, |
| 316 | const struct mbus_dram_target_info *dram) |
| 317 | { |
| 318 | void __iomem *iobase = engine->regs; |
| 319 | int i; |
| 320 | |
| 321 | for (i = 0; i < 4; i++) { |
| 322 | writel(0, iobase + CESA_TDMA_WINDOW_CTRL(i)); |
| 323 | writel(0, iobase + CESA_TDMA_WINDOW_BASE(i)); |
| 324 | } |
| 325 | |
| 326 | for (i = 0; i < dram->num_cs; i++) { |
| 327 | const struct mbus_dram_window *cs = dram->cs + i; |
| 328 | |
| 329 | writel(((cs->size - 1) & 0xffff0000) | |
| 330 | (cs->mbus_attr << 8) | |
| 331 | (dram->mbus_dram_target_id << 4) | 1, |
| 332 | iobase + CESA_TDMA_WINDOW_CTRL(i)); |
| 333 | writel(cs->base, iobase + CESA_TDMA_WINDOW_BASE(i)); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | static int mv_cesa_dev_dma_init(struct mv_cesa_dev *cesa) |
| 338 | { |
| 339 | struct device *dev = cesa->dev; |
| 340 | struct mv_cesa_dev_dma *dma; |
| 341 | |
| 342 | if (!cesa->caps->has_tdma) |
| 343 | return 0; |
| 344 | |
| 345 | dma = devm_kzalloc(dev, sizeof(*dma), GFP_KERNEL); |
| 346 | if (!dma) |
| 347 | return -ENOMEM; |
| 348 | |
| 349 | dma->tdma_desc_pool = dmam_pool_create("tdma_desc", dev, |
| 350 | sizeof(struct mv_cesa_tdma_desc), |
| 351 | 16, 0); |
| 352 | if (!dma->tdma_desc_pool) |
| 353 | return -ENOMEM; |
| 354 | |
| 355 | dma->op_pool = dmam_pool_create("cesa_op", dev, |
| 356 | sizeof(struct mv_cesa_op_ctx), 16, 0); |
| 357 | if (!dma->op_pool) |
| 358 | return -ENOMEM; |
| 359 | |
| 360 | dma->cache_pool = dmam_pool_create("cesa_cache", dev, |
| 361 | CESA_MAX_HASH_BLOCK_SIZE, 1, 0); |
| 362 | if (!dma->cache_pool) |
| 363 | return -ENOMEM; |
| 364 | |
| 365 | dma->padding_pool = dmam_pool_create("cesa_padding", dev, 72, 1, 0); |
Boris BREZILLON | 8a3978a | 2016-02-05 17:45:48 +0100 | [diff] [blame] | 366 | if (!dma->padding_pool) |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 367 | return -ENOMEM; |
| 368 | |
| 369 | cesa->dma = dma; |
| 370 | |
| 371 | return 0; |
| 372 | } |
| 373 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 374 | static int mv_cesa_get_sram(struct platform_device *pdev, int idx) |
| 375 | { |
| 376 | struct mv_cesa_dev *cesa = platform_get_drvdata(pdev); |
| 377 | struct mv_cesa_engine *engine = &cesa->engines[idx]; |
| 378 | const char *res_name = "sram"; |
| 379 | struct resource *res; |
| 380 | |
Vladimir Zapolskiy | abdd4a7 | 2015-06-30 15:00:07 -0700 | [diff] [blame] | 381 | engine->pool = of_gen_pool_get(cesa->dev->of_node, |
| 382 | "marvell,crypto-srams", idx); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 383 | if (engine->pool) { |
Herbert Xu | c114cf7 | 2021-01-21 16:16:46 +1100 | [diff] [blame] | 384 | engine->sram_pool = gen_pool_dma_alloc(engine->pool, |
| 385 | cesa->sram_size, |
| 386 | &engine->sram_dma); |
| 387 | if (engine->sram_pool) |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 388 | return 0; |
| 389 | |
| 390 | engine->pool = NULL; |
| 391 | return -ENOMEM; |
| 392 | } |
| 393 | |
| 394 | if (cesa->caps->nengines > 1) { |
| 395 | if (!idx) |
| 396 | res_name = "sram0"; |
| 397 | else |
| 398 | res_name = "sram1"; |
| 399 | } |
| 400 | |
| 401 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, |
| 402 | res_name); |
| 403 | if (!res || resource_size(res) < cesa->sram_size) |
| 404 | return -EINVAL; |
| 405 | |
| 406 | engine->sram = devm_ioremap_resource(cesa->dev, res); |
| 407 | if (IS_ERR(engine->sram)) |
| 408 | return PTR_ERR(engine->sram); |
| 409 | |
Robin Murphy | 37d728f | 2018-01-10 15:15:43 +0000 | [diff] [blame] | 410 | engine->sram_dma = dma_map_resource(cesa->dev, res->start, |
| 411 | cesa->sram_size, |
| 412 | DMA_BIDIRECTIONAL, 0); |
| 413 | if (dma_mapping_error(cesa->dev, engine->sram_dma)) |
| 414 | return -ENOMEM; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 415 | |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | static void mv_cesa_put_sram(struct platform_device *pdev, int idx) |
| 420 | { |
| 421 | struct mv_cesa_dev *cesa = platform_get_drvdata(pdev); |
| 422 | struct mv_cesa_engine *engine = &cesa->engines[idx]; |
| 423 | |
Robin Murphy | 37d728f | 2018-01-10 15:15:43 +0000 | [diff] [blame] | 424 | if (engine->pool) |
Herbert Xu | c114cf7 | 2021-01-21 16:16:46 +1100 | [diff] [blame] | 425 | gen_pool_free(engine->pool, (unsigned long)engine->sram_pool, |
Robin Murphy | 37d728f | 2018-01-10 15:15:43 +0000 | [diff] [blame] | 426 | cesa->sram_size); |
| 427 | else |
| 428 | dma_unmap_resource(cesa->dev, engine->sram_dma, |
| 429 | cesa->sram_size, DMA_BIDIRECTIONAL, 0); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | static int mv_cesa_probe(struct platform_device *pdev) |
| 433 | { |
Boris BREZILLON | 0bf6948 | 2015-06-18 15:46:28 +0200 | [diff] [blame] | 434 | const struct mv_cesa_caps *caps = &orion_caps; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 435 | const struct mbus_dram_target_info *dram; |
| 436 | const struct of_device_id *match; |
| 437 | struct device *dev = &pdev->dev; |
| 438 | struct mv_cesa_dev *cesa; |
| 439 | struct mv_cesa_engine *engines; |
Sven Auhagen | 28ee8b0 | 2020-07-21 06:40:27 +0200 | [diff] [blame] | 440 | int irq, ret, i, cpu; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 441 | u32 sram_size; |
| 442 | |
| 443 | if (cesa_dev) { |
| 444 | dev_err(&pdev->dev, "Only one CESA device authorized\n"); |
| 445 | return -EEXIST; |
| 446 | } |
| 447 | |
Boris BREZILLON | 0bf6948 | 2015-06-18 15:46:28 +0200 | [diff] [blame] | 448 | if (dev->of_node) { |
| 449 | match = of_match_node(mv_cesa_of_match_table, dev->of_node); |
| 450 | if (!match || !match->data) |
| 451 | return -ENOTSUPP; |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 452 | |
Boris BREZILLON | 0bf6948 | 2015-06-18 15:46:28 +0200 | [diff] [blame] | 453 | caps = match->data; |
| 454 | } |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 455 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 456 | cesa = devm_kzalloc(dev, sizeof(*cesa), GFP_KERNEL); |
| 457 | if (!cesa) |
| 458 | return -ENOMEM; |
| 459 | |
| 460 | cesa->caps = caps; |
| 461 | cesa->dev = dev; |
| 462 | |
| 463 | sram_size = CESA_SA_DEFAULT_SRAM_SIZE; |
| 464 | of_property_read_u32(cesa->dev->of_node, "marvell,crypto-sram-size", |
| 465 | &sram_size); |
| 466 | if (sram_size < CESA_SA_MIN_SRAM_SIZE) |
| 467 | sram_size = CESA_SA_MIN_SRAM_SIZE; |
| 468 | |
| 469 | cesa->sram_size = sram_size; |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 470 | cesa->engines = devm_kcalloc(dev, caps->nengines, sizeof(*engines), |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 471 | GFP_KERNEL); |
| 472 | if (!cesa->engines) |
| 473 | return -ENOMEM; |
| 474 | |
| 475 | spin_lock_init(&cesa->lock); |
Romain Perier | bf8f91e | 2016-06-21 10:08:38 +0200 | [diff] [blame] | 476 | |
Zhang Qilong | 3cea6b3 | 2020-09-17 15:42:34 +0800 | [diff] [blame] | 477 | cesa->regs = devm_platform_ioremap_resource_byname(pdev, "regs"); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 478 | if (IS_ERR(cesa->regs)) |
Boris BREZILLON | dfe97ad | 2016-03-17 10:47:10 +0100 | [diff] [blame] | 479 | return PTR_ERR(cesa->regs); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 480 | |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 481 | ret = mv_cesa_dev_dma_init(cesa); |
| 482 | if (ret) |
| 483 | return ret; |
| 484 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 485 | dram = mv_mbus_dram_info_nooverlap(); |
| 486 | |
| 487 | platform_set_drvdata(pdev, cesa); |
| 488 | |
| 489 | for (i = 0; i < caps->nengines; i++) { |
| 490 | struct mv_cesa_engine *engine = &cesa->engines[i]; |
| 491 | char res_name[7]; |
| 492 | |
| 493 | engine->id = i; |
| 494 | spin_lock_init(&engine->lock); |
| 495 | |
| 496 | ret = mv_cesa_get_sram(pdev, i); |
| 497 | if (ret) |
| 498 | goto err_cleanup; |
| 499 | |
| 500 | irq = platform_get_irq(pdev, i); |
| 501 | if (irq < 0) { |
| 502 | ret = irq; |
| 503 | goto err_cleanup; |
| 504 | } |
| 505 | |
Sven Auhagen | 28ee8b0 | 2020-07-21 06:40:27 +0200 | [diff] [blame] | 506 | engine->irq = irq; |
| 507 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 508 | /* |
| 509 | * Not all platforms can gate the CESA clocks: do not complain |
| 510 | * if the clock does not exist. |
| 511 | */ |
| 512 | snprintf(res_name, sizeof(res_name), "cesa%d", i); |
| 513 | engine->clk = devm_clk_get(dev, res_name); |
| 514 | if (IS_ERR(engine->clk)) { |
| 515 | engine->clk = devm_clk_get(dev, NULL); |
| 516 | if (IS_ERR(engine->clk)) |
| 517 | engine->clk = NULL; |
| 518 | } |
| 519 | |
| 520 | snprintf(res_name, sizeof(res_name), "cesaz%d", i); |
| 521 | engine->zclk = devm_clk_get(dev, res_name); |
| 522 | if (IS_ERR(engine->zclk)) |
| 523 | engine->zclk = NULL; |
| 524 | |
| 525 | ret = clk_prepare_enable(engine->clk); |
| 526 | if (ret) |
| 527 | goto err_cleanup; |
| 528 | |
| 529 | ret = clk_prepare_enable(engine->zclk); |
| 530 | if (ret) |
| 531 | goto err_cleanup; |
| 532 | |
| 533 | engine->regs = cesa->regs + CESA_ENGINE_OFF(i); |
| 534 | |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 535 | if (dram && cesa->caps->has_tdma) |
Romain Perier | 21ec757 | 2016-04-19 17:09:20 +0200 | [diff] [blame] | 536 | mv_cesa_conf_mbus_windows(engine, dram); |
Boris BREZILLON | db509a4 | 2015-06-18 15:46:21 +0200 | [diff] [blame] | 537 | |
Romain Perier | 21ec757 | 2016-04-19 17:09:20 +0200 | [diff] [blame] | 538 | writel(0, engine->regs + CESA_SA_INT_STATUS); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 539 | writel(CESA_SA_CFG_STOP_DIG_ERR, |
Romain Perier | 21ec757 | 2016-04-19 17:09:20 +0200 | [diff] [blame] | 540 | engine->regs + CESA_SA_CFG); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 541 | writel(engine->sram_dma & CESA_SA_SRAM_MSK, |
Romain Perier | 21ec757 | 2016-04-19 17:09:20 +0200 | [diff] [blame] | 542 | engine->regs + CESA_SA_DESC_P0); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 543 | |
| 544 | ret = devm_request_threaded_irq(dev, irq, NULL, mv_cesa_int, |
| 545 | IRQF_ONESHOT, |
| 546 | dev_name(&pdev->dev), |
Romain Perier | 21ec757 | 2016-04-19 17:09:20 +0200 | [diff] [blame] | 547 | engine); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 548 | if (ret) |
| 549 | goto err_cleanup; |
Romain Perier | bf8f91e | 2016-06-21 10:08:38 +0200 | [diff] [blame] | 550 | |
Sven Auhagen | 28ee8b0 | 2020-07-21 06:40:27 +0200 | [diff] [blame] | 551 | /* Set affinity */ |
| 552 | cpu = cpumask_local_spread(engine->id, NUMA_NO_NODE); |
| 553 | irq_set_affinity_hint(irq, get_cpu_mask(cpu)); |
| 554 | |
Romain Perier | bf8f91e | 2016-06-21 10:08:38 +0200 | [diff] [blame] | 555 | crypto_init_queue(&engine->queue, CESA_CRYPTO_DEFAULT_MAX_QLEN); |
| 556 | atomic_set(&engine->load, 0); |
Romain Perier | 85030c5 | 2016-06-21 10:08:39 +0200 | [diff] [blame] | 557 | INIT_LIST_HEAD(&engine->complete_queue); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | cesa_dev = cesa; |
| 561 | |
| 562 | ret = mv_cesa_add_algs(cesa); |
| 563 | if (ret) { |
| 564 | cesa_dev = NULL; |
| 565 | goto err_cleanup; |
| 566 | } |
| 567 | |
| 568 | dev_info(dev, "CESA device successfully registered\n"); |
| 569 | |
| 570 | return 0; |
| 571 | |
| 572 | err_cleanup: |
| 573 | for (i = 0; i < caps->nengines; i++) { |
| 574 | clk_disable_unprepare(cesa->engines[i].zclk); |
| 575 | clk_disable_unprepare(cesa->engines[i].clk); |
| 576 | mv_cesa_put_sram(pdev, i); |
Sven Auhagen | 28ee8b0 | 2020-07-21 06:40:27 +0200 | [diff] [blame] | 577 | if (cesa->engines[i].irq > 0) |
| 578 | irq_set_affinity_hint(cesa->engines[i].irq, NULL); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | return ret; |
| 582 | } |
| 583 | |
| 584 | static int mv_cesa_remove(struct platform_device *pdev) |
| 585 | { |
| 586 | struct mv_cesa_dev *cesa = platform_get_drvdata(pdev); |
| 587 | int i; |
| 588 | |
| 589 | mv_cesa_remove_algs(cesa); |
| 590 | |
| 591 | for (i = 0; i < cesa->caps->nengines; i++) { |
| 592 | clk_disable_unprepare(cesa->engines[i].zclk); |
| 593 | clk_disable_unprepare(cesa->engines[i].clk); |
| 594 | mv_cesa_put_sram(pdev, i); |
Sven Auhagen | 28ee8b0 | 2020-07-21 06:40:27 +0200 | [diff] [blame] | 595 | irq_set_affinity_hint(cesa->engines[i].irq, NULL); |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | return 0; |
| 599 | } |
| 600 | |
Boris BREZILLON | 7b0c3d6 | 2017-10-11 15:16:17 +0200 | [diff] [blame] | 601 | static const struct platform_device_id mv_cesa_plat_id_table[] = { |
| 602 | { .name = "mv_crypto" }, |
Boris BREZILLON | 9c90344 | 2017-11-03 17:52:01 +0100 | [diff] [blame] | 603 | { /* sentinel */ }, |
Boris BREZILLON | 7b0c3d6 | 2017-10-11 15:16:17 +0200 | [diff] [blame] | 604 | }; |
| 605 | MODULE_DEVICE_TABLE(platform, mv_cesa_plat_id_table); |
| 606 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 607 | static struct platform_driver marvell_cesa = { |
| 608 | .probe = mv_cesa_probe, |
| 609 | .remove = mv_cesa_remove, |
Boris BREZILLON | 7b0c3d6 | 2017-10-11 15:16:17 +0200 | [diff] [blame] | 610 | .id_table = mv_cesa_plat_id_table, |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 611 | .driver = { |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 612 | .name = "marvell-cesa", |
| 613 | .of_match_table = mv_cesa_of_match_table, |
| 614 | }, |
| 615 | }; |
| 616 | module_platform_driver(marvell_cesa); |
| 617 | |
Boris BREZILLON | f63601f | 2015-06-18 15:46:20 +0200 | [diff] [blame] | 618 | MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>"); |
| 619 | MODULE_AUTHOR("Arnaud Ebalard <arno@natisbad.org>"); |
| 620 | MODULE_DESCRIPTION("Support for Marvell's cryptographic engine"); |
| 621 | MODULE_LICENSE("GPL v2"); |