^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * amlogic.h - hardware cryptographic offloader for Amlogic SoC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2018-2019 Corentin LABBE <clabbe@baylibre.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <crypto/aes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <crypto/engine.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <crypto/skcipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/scatterlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define MODE_KEY 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define MODE_AES_128 0x8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define MODE_AES_192 0x9
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define MODE_AES_256 0xa
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define MESON_DECRYPT 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define MESON_ENCRYPT 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define MESON_OPMODE_ECB 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define MESON_OPMODE_CBC 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define MAXFLOW 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define MAXDESC 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define DESC_LAST BIT(18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define DESC_ENCRYPTION BIT(28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define DESC_OWN BIT(31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * struct meson_desc - Descriptor for DMA operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * Note that without datasheet, some are unknown
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * @t_status: Descriptor of the cipher operation (see description below)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * @t_src: Physical address of data to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * @t_dst: Physical address of data to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * t_status is segmented like this:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * @len: 0-16 length of data to operate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * @irq: 17 Ignored by hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * @eoc: 18 End means the descriptor is the last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * @loop: 19 Unknown
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * @mode: 20-23 Type of algorithm (AES, SHA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * @begin: 24 Unknown
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * @end: 25 Unknown
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * @op_mode: 26-27 Blockmode (CBC, ECB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * @enc: 28 0 means decryption, 1 is for encryption
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * @block: 29 Unknown
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * @error: 30 Unknown
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * @owner: 31 owner of the descriptor, 1 own by HW
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct meson_desc {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) __le32 t_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) __le32 t_src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) __le32 t_dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * struct meson_flow - Information used by each flow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * @engine: ptr to the crypto_engine for this flow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * @keylen: keylen for this flow operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * @complete: completion for the current task on this flow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * @status: set to 1 by interrupt if task is done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * @t_phy: Physical address of task
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * @tl: pointer to the current ce_task for this flow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * @stat_req: number of request done by this flow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct meson_flow {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct crypto_engine *engine;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct completion complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) unsigned int keylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) dma_addr_t t_phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct meson_desc *tl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #ifdef CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) unsigned long stat_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * struct meson_dev - main container for all this driver information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * @base: base address of amlogic-crypto
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * @busclk: bus clock for amlogic-crypto
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * @dev: the platform device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * @chanlist: array of all flow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * @flow: flow to use in next request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * @irqs: IRQ numbers for amlogic-crypto
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * @dbgfs_dir: Debugfs dentry for statistic directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * @dbgfs_stats: Debugfs dentry for statistic counters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct meson_dev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) void __iomem *base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct clk *busclk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct meson_flow *chanlist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) atomic_t flow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) int *irqs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) #ifdef CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct dentry *dbgfs_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * struct meson_cipher_req_ctx - context for a skcipher request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * @op_dir: direction (encrypt vs decrypt) for this request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * @flow: the flow to use for this request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct meson_cipher_req_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) u32 op_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) int flow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) struct skcipher_request fallback_req; // keep at the end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * struct meson_cipher_tfm_ctx - context for a skcipher TFM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * @enginectx: crypto_engine used by this TFM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * @key: pointer to key data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * @keylen: len of the key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * @keymode: The keymode(type and size of key) associated with this TFM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * @mc: pointer to the private data of driver handling this TFM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * @fallback_tfm: pointer to the fallback TFM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct meson_cipher_tfm_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct crypto_engine_ctx enginectx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) u32 *key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) u32 keylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) u32 keymode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct meson_dev *mc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct crypto_skcipher *fallback_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * struct meson_alg_template - crypto_alg template
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * @type: the CRYPTO_ALG_TYPE for this template
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * @blockmode: the type of block operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * @mc: pointer to the meson_dev structure associated with this template
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * @alg: one of sub struct must be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * @stat_req: number of request done on this template
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * @stat_fb: total of all data len done on this template
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) struct meson_alg_template {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) u32 type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) u32 blockmode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) struct skcipher_alg skcipher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) } alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct meson_dev *mc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) #ifdef CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) unsigned long stat_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) unsigned long stat_fb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) int meson_enqueue(struct crypto_async_request *areq, u32 type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) int meson_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) unsigned int keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) int meson_cipher_init(struct crypto_tfm *tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) void meson_cipher_exit(struct crypto_tfm *tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) int meson_skdecrypt(struct skcipher_request *areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) int meson_skencrypt(struct skcipher_request *areq);