18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/* Copyright (C) 2012-2019 ARM Limited (or its affiliates). */
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci/* \file cc_driver.h
58c2ecf20Sopenharmony_ci * ARM CryptoCell Linux Crypto Driver
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#ifndef __CC_DRIVER_H__
98c2ecf20Sopenharmony_ci#define __CC_DRIVER_H__
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#ifdef COMP_IN_WQ
128c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
138c2ecf20Sopenharmony_ci#else
148c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
158c2ecf20Sopenharmony_ci#endif
168c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
178c2ecf20Sopenharmony_ci#include <crypto/algapi.h>
188c2ecf20Sopenharmony_ci#include <crypto/internal/skcipher.h>
198c2ecf20Sopenharmony_ci#include <crypto/aes.h>
208c2ecf20Sopenharmony_ci#include <crypto/sha.h>
218c2ecf20Sopenharmony_ci#include <crypto/aead.h>
228c2ecf20Sopenharmony_ci#include <crypto/authenc.h>
238c2ecf20Sopenharmony_ci#include <crypto/hash.h>
248c2ecf20Sopenharmony_ci#include <crypto/skcipher.h>
258c2ecf20Sopenharmony_ci#include <linux/version.h>
268c2ecf20Sopenharmony_ci#include <linux/clk.h>
278c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#include "cc_host_regs.h"
308c2ecf20Sopenharmony_ci#include "cc_crypto_ctx.h"
318c2ecf20Sopenharmony_ci#include "cc_hw_queue_defs.h"
328c2ecf20Sopenharmony_ci#include "cc_sram_mgr.h"
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ciextern bool cc_dump_desc;
358c2ecf20Sopenharmony_ciextern bool cc_dump_bytes;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#define DRV_MODULE_VERSION "5.0"
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cienum cc_hw_rev {
408c2ecf20Sopenharmony_ci	CC_HW_REV_630 = 630,
418c2ecf20Sopenharmony_ci	CC_HW_REV_710 = 710,
428c2ecf20Sopenharmony_ci	CC_HW_REV_712 = 712,
438c2ecf20Sopenharmony_ci	CC_HW_REV_713 = 713
448c2ecf20Sopenharmony_ci};
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cienum cc_std_body {
478c2ecf20Sopenharmony_ci	CC_STD_NIST = 0x1,
488c2ecf20Sopenharmony_ci	CC_STD_OSCCA = 0x2,
498c2ecf20Sopenharmony_ci	CC_STD_ALL = 0x3
508c2ecf20Sopenharmony_ci};
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci#define CC_COHERENT_CACHE_PARAMS 0xEEE
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define CC_PINS_FULL	0x0
558c2ecf20Sopenharmony_ci#define CC_PINS_SLIM	0x9F
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci/* Maximum DMA mask supported by IP */
588c2ecf20Sopenharmony_ci#define DMA_BIT_MASK_LEN 48
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci#define CC_AXI_IRQ_MASK ((1 << CC_AXIM_CFG_BRESPMASK_BIT_SHIFT) | \
618c2ecf20Sopenharmony_ci			  (1 << CC_AXIM_CFG_RRESPMASK_BIT_SHIFT) | \
628c2ecf20Sopenharmony_ci			  (1 << CC_AXIM_CFG_INFLTMASK_BIT_SHIFT) | \
638c2ecf20Sopenharmony_ci			  (1 << CC_AXIM_CFG_COMPMASK_BIT_SHIFT))
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci#define CC_AXI_ERR_IRQ_MASK BIT(CC_HOST_IRR_AXI_ERR_INT_BIT_SHIFT)
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci#define CC_COMP_IRQ_MASK BIT(CC_HOST_IRR_AXIM_COMP_INT_BIT_SHIFT)
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci#define CC_SECURITY_DISABLED_MASK BIT(CC_SECURITY_DISABLED_VALUE_BIT_SHIFT)
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci#define CC_NVM_IS_IDLE_MASK BIT(CC_NVM_IS_IDLE_VALUE_BIT_SHIFT)
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci#define AXIM_MON_COMP_VALUE CC_GENMASK(CC_AXIM_MON_COMP_VALUE)
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci#define CC_CPP_AES_ABORT_MASK ( \
768c2ecf20Sopenharmony_ci	BIT(CC_HOST_IMR_REE_OP_ABORTED_AES_0_MASK_BIT_SHIFT) | \
778c2ecf20Sopenharmony_ci	BIT(CC_HOST_IMR_REE_OP_ABORTED_AES_1_MASK_BIT_SHIFT) | \
788c2ecf20Sopenharmony_ci	BIT(CC_HOST_IMR_REE_OP_ABORTED_AES_2_MASK_BIT_SHIFT) | \
798c2ecf20Sopenharmony_ci	BIT(CC_HOST_IMR_REE_OP_ABORTED_AES_3_MASK_BIT_SHIFT) | \
808c2ecf20Sopenharmony_ci	BIT(CC_HOST_IMR_REE_OP_ABORTED_AES_4_MASK_BIT_SHIFT) | \
818c2ecf20Sopenharmony_ci	BIT(CC_HOST_IMR_REE_OP_ABORTED_AES_5_MASK_BIT_SHIFT) | \
828c2ecf20Sopenharmony_ci	BIT(CC_HOST_IMR_REE_OP_ABORTED_AES_6_MASK_BIT_SHIFT) | \
838c2ecf20Sopenharmony_ci	BIT(CC_HOST_IMR_REE_OP_ABORTED_AES_7_MASK_BIT_SHIFT))
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci#define CC_CPP_SM4_ABORT_MASK ( \
868c2ecf20Sopenharmony_ci	BIT(CC_HOST_IMR_REE_OP_ABORTED_SM_0_MASK_BIT_SHIFT) | \
878c2ecf20Sopenharmony_ci	BIT(CC_HOST_IMR_REE_OP_ABORTED_SM_1_MASK_BIT_SHIFT) | \
888c2ecf20Sopenharmony_ci	BIT(CC_HOST_IMR_REE_OP_ABORTED_SM_2_MASK_BIT_SHIFT) | \
898c2ecf20Sopenharmony_ci	BIT(CC_HOST_IMR_REE_OP_ABORTED_SM_3_MASK_BIT_SHIFT) | \
908c2ecf20Sopenharmony_ci	BIT(CC_HOST_IMR_REE_OP_ABORTED_SM_4_MASK_BIT_SHIFT) | \
918c2ecf20Sopenharmony_ci	BIT(CC_HOST_IMR_REE_OP_ABORTED_SM_5_MASK_BIT_SHIFT) | \
928c2ecf20Sopenharmony_ci	BIT(CC_HOST_IMR_REE_OP_ABORTED_SM_6_MASK_BIT_SHIFT) | \
938c2ecf20Sopenharmony_ci	BIT(CC_HOST_IMR_REE_OP_ABORTED_SM_7_MASK_BIT_SHIFT))
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci/* Register name mangling macro */
968c2ecf20Sopenharmony_ci#define CC_REG(reg_name) CC_ ## reg_name ## _REG_OFFSET
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci/* TEE FIPS status interrupt */
998c2ecf20Sopenharmony_ci#define CC_GPR0_IRQ_MASK BIT(CC_HOST_IRR_GPR0_BIT_SHIFT)
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci#define CC_CRA_PRIO 400
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci#define MIN_HW_QUEUE_SIZE 50 /* Minimum size required for proper function */
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci#define MAX_REQUEST_QUEUE_SIZE 4096
1068c2ecf20Sopenharmony_ci#define MAX_MLLI_BUFF_SIZE 2080
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci/* Definitions for HW descriptors DIN/DOUT fields */
1098c2ecf20Sopenharmony_ci#define NS_BIT 1
1108c2ecf20Sopenharmony_ci#define AXI_ID 0
1118c2ecf20Sopenharmony_ci/* AXI_ID is not actually the AXI ID of the transaction but the value of AXI_ID
1128c2ecf20Sopenharmony_ci * field in the HW descriptor. The DMA engine +8 that value.
1138c2ecf20Sopenharmony_ci */
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cistruct cc_cpp_req {
1168c2ecf20Sopenharmony_ci	bool is_cpp;
1178c2ecf20Sopenharmony_ci	enum cc_cpp_alg alg;
1188c2ecf20Sopenharmony_ci	u8 slot;
1198c2ecf20Sopenharmony_ci};
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci#define CC_MAX_IVGEN_DMA_ADDRESSES	3
1228c2ecf20Sopenharmony_cistruct cc_crypto_req {
1238c2ecf20Sopenharmony_ci	void (*user_cb)(struct device *dev, void *req, int err);
1248c2ecf20Sopenharmony_ci	void *user_arg;
1258c2ecf20Sopenharmony_ci	struct completion seq_compl; /* request completion */
1268c2ecf20Sopenharmony_ci	struct cc_cpp_req cpp;
1278c2ecf20Sopenharmony_ci};
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci/**
1308c2ecf20Sopenharmony_ci * struct cc_drvdata - driver private data context
1318c2ecf20Sopenharmony_ci * @cc_base:	virt address of the CC registers
1328c2ecf20Sopenharmony_ci * @irq:	bitmap indicating source of last interrupt
1338c2ecf20Sopenharmony_ci */
1348c2ecf20Sopenharmony_cistruct cc_drvdata {
1358c2ecf20Sopenharmony_ci	void __iomem *cc_base;
1368c2ecf20Sopenharmony_ci	int irq;
1378c2ecf20Sopenharmony_ci	struct completion hw_queue_avail; /* wait for HW queue availability */
1388c2ecf20Sopenharmony_ci	struct platform_device *plat_dev;
1398c2ecf20Sopenharmony_ci	u32 mlli_sram_addr;
1408c2ecf20Sopenharmony_ci	struct dma_pool *mlli_buffs_pool;
1418c2ecf20Sopenharmony_ci	struct list_head alg_list;
1428c2ecf20Sopenharmony_ci	void *hash_handle;
1438c2ecf20Sopenharmony_ci	void *aead_handle;
1448c2ecf20Sopenharmony_ci	void *request_mgr_handle;
1458c2ecf20Sopenharmony_ci	void *fips_handle;
1468c2ecf20Sopenharmony_ci	u32 sram_free_offset;	/* offset to non-allocated area in SRAM */
1478c2ecf20Sopenharmony_ci	struct dentry *dir;	/* for debugfs */
1488c2ecf20Sopenharmony_ci	struct clk *clk;
1498c2ecf20Sopenharmony_ci	bool coherent;
1508c2ecf20Sopenharmony_ci	char *hw_rev_name;
1518c2ecf20Sopenharmony_ci	enum cc_hw_rev hw_rev;
1528c2ecf20Sopenharmony_ci	u32 axim_mon_offset;
1538c2ecf20Sopenharmony_ci	u32 sig_offset;
1548c2ecf20Sopenharmony_ci	u32 ver_offset;
1558c2ecf20Sopenharmony_ci	int std_bodies;
1568c2ecf20Sopenharmony_ci	bool sec_disabled;
1578c2ecf20Sopenharmony_ci	u32 comp_mask;
1588c2ecf20Sopenharmony_ci};
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistruct cc_crypto_alg {
1618c2ecf20Sopenharmony_ci	struct list_head entry;
1628c2ecf20Sopenharmony_ci	int cipher_mode;
1638c2ecf20Sopenharmony_ci	int flow_mode; /* Note: currently, refers to the cipher mode only. */
1648c2ecf20Sopenharmony_ci	int auth_mode;
1658c2ecf20Sopenharmony_ci	struct cc_drvdata *drvdata;
1668c2ecf20Sopenharmony_ci	struct skcipher_alg skcipher_alg;
1678c2ecf20Sopenharmony_ci	struct aead_alg aead_alg;
1688c2ecf20Sopenharmony_ci};
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_cistruct cc_alg_template {
1718c2ecf20Sopenharmony_ci	char name[CRYPTO_MAX_ALG_NAME];
1728c2ecf20Sopenharmony_ci	char driver_name[CRYPTO_MAX_ALG_NAME];
1738c2ecf20Sopenharmony_ci	unsigned int blocksize;
1748c2ecf20Sopenharmony_ci	union {
1758c2ecf20Sopenharmony_ci		struct skcipher_alg skcipher;
1768c2ecf20Sopenharmony_ci		struct aead_alg aead;
1778c2ecf20Sopenharmony_ci	} template_u;
1788c2ecf20Sopenharmony_ci	int cipher_mode;
1798c2ecf20Sopenharmony_ci	int flow_mode; /* Note: currently, refers to the cipher mode only. */
1808c2ecf20Sopenharmony_ci	int auth_mode;
1818c2ecf20Sopenharmony_ci	u32 min_hw_rev;
1828c2ecf20Sopenharmony_ci	enum cc_std_body std_body;
1838c2ecf20Sopenharmony_ci	bool sec_func;
1848c2ecf20Sopenharmony_ci	unsigned int data_unit;
1858c2ecf20Sopenharmony_ci	struct cc_drvdata *drvdata;
1868c2ecf20Sopenharmony_ci};
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_cistruct async_gen_req_ctx {
1898c2ecf20Sopenharmony_ci	dma_addr_t iv_dma_addr;
1908c2ecf20Sopenharmony_ci	u8 *iv;
1918c2ecf20Sopenharmony_ci	enum drv_crypto_direction op_type;
1928c2ecf20Sopenharmony_ci};
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_cistatic inline struct device *drvdata_to_dev(struct cc_drvdata *drvdata)
1958c2ecf20Sopenharmony_ci{
1968c2ecf20Sopenharmony_ci	return &drvdata->plat_dev->dev;
1978c2ecf20Sopenharmony_ci}
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_civoid __dump_byte_array(const char *name, const u8 *buf, size_t len);
2008c2ecf20Sopenharmony_cistatic inline void dump_byte_array(const char *name, const u8 *the_array,
2018c2ecf20Sopenharmony_ci				   size_t size)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	if (cc_dump_bytes)
2048c2ecf20Sopenharmony_ci		__dump_byte_array(name, the_array, size);
2058c2ecf20Sopenharmony_ci}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cibool cc_wait_for_reset_completion(struct cc_drvdata *drvdata);
2088c2ecf20Sopenharmony_ciint init_cc_regs(struct cc_drvdata *drvdata, bool is_probe);
2098c2ecf20Sopenharmony_civoid fini_cc_regs(struct cc_drvdata *drvdata);
2108c2ecf20Sopenharmony_ciunsigned int cc_get_default_hash_len(struct cc_drvdata *drvdata);
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_cistatic inline void cc_iowrite(struct cc_drvdata *drvdata, u32 reg, u32 val)
2138c2ecf20Sopenharmony_ci{
2148c2ecf20Sopenharmony_ci	iowrite32(val, (drvdata->cc_base + reg));
2158c2ecf20Sopenharmony_ci}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_cistatic inline u32 cc_ioread(struct cc_drvdata *drvdata, u32 reg)
2188c2ecf20Sopenharmony_ci{
2198c2ecf20Sopenharmony_ci	return ioread32(drvdata->cc_base + reg);
2208c2ecf20Sopenharmony_ci}
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_cistatic inline gfp_t cc_gfp_flags(struct crypto_async_request *req)
2238c2ecf20Sopenharmony_ci{
2248c2ecf20Sopenharmony_ci	return (req->flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
2258c2ecf20Sopenharmony_ci			GFP_KERNEL : GFP_ATOMIC;
2268c2ecf20Sopenharmony_ci}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_cistatic inline void set_queue_last_ind(struct cc_drvdata *drvdata,
2298c2ecf20Sopenharmony_ci				      struct cc_hw_desc *pdesc)
2308c2ecf20Sopenharmony_ci{
2318c2ecf20Sopenharmony_ci	if (drvdata->hw_rev >= CC_HW_REV_712)
2328c2ecf20Sopenharmony_ci		set_queue_last_ind_bit(pdesc);
2338c2ecf20Sopenharmony_ci}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci#endif /*__CC_DRIVER_H__*/
236