blob: c227718ba9923472442a2f9bef1049bd49974b29 [file] [log] [blame]
Gilad Ben-Yossef4c3f9722018-01-22 09:27:00 +00001/* SPDX-License-Identifier: GPL-2.0 */
Gilad Ben-Yossef03963ca2019-04-18 16:38:53 +03002/* Copyright (C) 2012-2019 ARM Limited (or its affiliates). */
Gilad Ben-Yossef4c3f9722018-01-22 09:27:00 +00003
4/* \file cc_driver.h
5 * ARM CryptoCell Linux Crypto Driver
6 */
7
8#ifndef __CC_DRIVER_H__
9#define __CC_DRIVER_H__
10
11#ifdef COMP_IN_WQ
12#include <linux/workqueue.h>
13#else
14#include <linux/interrupt.h>
15#endif
16#include <linux/dma-mapping.h>
17#include <crypto/algapi.h>
Gilad Ben-Yossef63ee04c2018-01-22 09:27:01 +000018#include <crypto/internal/skcipher.h>
Gilad Ben-Yossef4c3f9722018-01-22 09:27:00 +000019#include <crypto/aes.h>
20#include <crypto/sha.h>
21#include <crypto/aead.h>
22#include <crypto/authenc.h>
23#include <crypto/hash.h>
24#include <crypto/skcipher.h>
25#include <linux/version.h>
26#include <linux/clk.h>
27#include <linux/platform_device.h>
28
29/* Registers definitions from shared/hw/ree_include */
30#include "cc_host_regs.h"
Gilad Ben-Yossef4c3f9722018-01-22 09:27:00 +000031#include "cc_crypto_ctx.h"
32#include "cc_hw_queue_defs.h"
33#include "cc_sram_mgr.h"
34
35extern bool cc_dump_desc;
36extern bool cc_dump_bytes;
37
Gilad Ben-Yossefe40fdb502018-10-29 09:50:12 +000038#define DRV_MODULE_VERSION "5.0"
Gilad Ben-Yossef4c3f9722018-01-22 09:27:00 +000039
Gilad Ben-Yossef27b3b222018-02-19 14:51:23 +000040enum cc_hw_rev {
41 CC_HW_REV_630 = 630,
42 CC_HW_REV_710 = 710,
Gilad Ben-Yossefe40fdb502018-10-29 09:50:12 +000043 CC_HW_REV_712 = 712,
44 CC_HW_REV_713 = 713
Gilad Ben-Yossef27b3b222018-02-19 14:51:23 +000045};
46
Gilad Ben-Yossef1c876a92018-11-13 09:40:35 +000047enum cc_std_body {
48 CC_STD_NIST = 0x1,
49 CC_STD_OSCCA = 0x2,
50 CC_STD_ALL = 0x3
51};
52
Gilad Ben-Yossef4c3f9722018-01-22 09:27:00 +000053#define CC_COHERENT_CACHE_PARAMS 0xEEE
54
Gilad Ben-Yossef303f99a2019-06-17 11:46:30 +030055#define CC_PINS_FULL 0x0
56#define CC_PINS_SLIM 0x9F
57
Gilad Ben-Yossef4c3f9722018-01-22 09:27:00 +000058/* Maximum DMA mask supported by IP */
59#define DMA_BIT_MASK_LEN 48
60
Gilad Ben-Yossef4c3f9722018-01-22 09:27:00 +000061#define CC_AXI_IRQ_MASK ((1 << CC_AXIM_CFG_BRESPMASK_BIT_SHIFT) | \
62 (1 << CC_AXIM_CFG_RRESPMASK_BIT_SHIFT) | \
63 (1 << CC_AXIM_CFG_INFLTMASK_BIT_SHIFT) | \
64 (1 << CC_AXIM_CFG_COMPMASK_BIT_SHIFT))
65
66#define CC_AXI_ERR_IRQ_MASK BIT(CC_HOST_IRR_AXI_ERR_INT_BIT_SHIFT)
67
68#define CC_COMP_IRQ_MASK BIT(CC_HOST_IRR_AXIM_COMP_INT_BIT_SHIFT)
69
Gilad Ben-Yosseff98f6e22019-04-18 16:38:39 +030070#define CC_SECURITY_DISABLED_MASK BIT(CC_SECURITY_DISABLED_VALUE_BIT_SHIFT)
71
Ofir Drangd84f6262019-06-17 11:46:28 +030072#define CC_NVM_IS_IDLE_MASK BIT(CC_NVM_IS_IDLE_VALUE_BIT_SHIFT)
73
Gilad Ben-Yossef4c3f9722018-01-22 09:27:00 +000074#define AXIM_MON_COMP_VALUE GENMASK(CC_AXIM_MON_COMP_VALUE_BIT_SIZE + \
75 CC_AXIM_MON_COMP_VALUE_BIT_SHIFT, \
76 CC_AXIM_MON_COMP_VALUE_BIT_SHIFT)
77
Gilad Ben-Yossefcadfd892019-04-18 16:38:40 +030078#define CC_CPP_AES_ABORT_MASK ( \
79 BIT(CC_HOST_IMR_REE_OP_ABORTED_AES_0_MASK_BIT_SHIFT) | \
80 BIT(CC_HOST_IMR_REE_OP_ABORTED_AES_1_MASK_BIT_SHIFT) | \
81 BIT(CC_HOST_IMR_REE_OP_ABORTED_AES_2_MASK_BIT_SHIFT) | \
82 BIT(CC_HOST_IMR_REE_OP_ABORTED_AES_3_MASK_BIT_SHIFT) | \
83 BIT(CC_HOST_IMR_REE_OP_ABORTED_AES_4_MASK_BIT_SHIFT) | \
84 BIT(CC_HOST_IMR_REE_OP_ABORTED_AES_5_MASK_BIT_SHIFT) | \
85 BIT(CC_HOST_IMR_REE_OP_ABORTED_AES_6_MASK_BIT_SHIFT) | \
86 BIT(CC_HOST_IMR_REE_OP_ABORTED_AES_7_MASK_BIT_SHIFT))
87
88#define CC_CPP_SM4_ABORT_MASK ( \
89 BIT(CC_HOST_IMR_REE_OP_ABORTED_SM_0_MASK_BIT_SHIFT) | \
90 BIT(CC_HOST_IMR_REE_OP_ABORTED_SM_1_MASK_BIT_SHIFT) | \
91 BIT(CC_HOST_IMR_REE_OP_ABORTED_SM_2_MASK_BIT_SHIFT) | \
92 BIT(CC_HOST_IMR_REE_OP_ABORTED_SM_3_MASK_BIT_SHIFT) | \
93 BIT(CC_HOST_IMR_REE_OP_ABORTED_SM_4_MASK_BIT_SHIFT) | \
94 BIT(CC_HOST_IMR_REE_OP_ABORTED_SM_5_MASK_BIT_SHIFT) | \
95 BIT(CC_HOST_IMR_REE_OP_ABORTED_SM_6_MASK_BIT_SHIFT) | \
96 BIT(CC_HOST_IMR_REE_OP_ABORTED_SM_7_MASK_BIT_SHIFT))
97
Gilad Ben-Yossef4c3f9722018-01-22 09:27:00 +000098/* Register name mangling macro */
99#define CC_REG(reg_name) CC_ ## reg_name ## _REG_OFFSET
100
101/* TEE FIPS status interrupt */
102#define CC_GPR0_IRQ_MASK BIT(CC_HOST_IRR_GPR0_BIT_SHIFT)
103
104#define CC_CRA_PRIO 400
105
106#define MIN_HW_QUEUE_SIZE 50 /* Minimum size required for proper function */
107
108#define MAX_REQUEST_QUEUE_SIZE 4096
109#define MAX_MLLI_BUFF_SIZE 2080
Gilad Ben-Yossef4c3f9722018-01-22 09:27:00 +0000110
111/* Definitions for HW descriptors DIN/DOUT fields */
112#define NS_BIT 1
113#define AXI_ID 0
114/* AXI_ID is not actually the AXI ID of the transaction but the value of AXI_ID
115 * field in the HW descriptor. The DMA engine +8 that value.
116 */
117
Gilad Ben-Yossefcadfd892019-04-18 16:38:40 +0300118struct cc_cpp_req {
119 bool is_cpp;
120 enum cc_cpp_alg alg;
121 u8 slot;
122};
123
Gilad Ben-Yossef4c3f9722018-01-22 09:27:00 +0000124#define CC_MAX_IVGEN_DMA_ADDRESSES 3
125struct cc_crypto_req {
126 void (*user_cb)(struct device *dev, void *req, int err);
127 void *user_arg;
Gilad Ben-Yossef4c3f9722018-01-22 09:27:00 +0000128 struct completion seq_compl; /* request completion */
Gilad Ben-Yossefcadfd892019-04-18 16:38:40 +0300129 struct cc_cpp_req cpp;
Gilad Ben-Yossef4c3f9722018-01-22 09:27:00 +0000130};
131
132/**
133 * struct cc_drvdata - driver private data context
134 * @cc_base: virt address of the CC registers
Gilad Ben-Yossef33c4b312020-01-16 12:14:44 +0200135 * @irq: bitmap indicating source of last interrupt
Gilad Ben-Yossef4c3f9722018-01-22 09:27:00 +0000136 */
137struct cc_drvdata {
138 void __iomem *cc_base;
139 int irq;
Gilad Ben-Yossef4c3f9722018-01-22 09:27:00 +0000140 struct completion hw_queue_avail; /* wait for HW queue availability */
141 struct platform_device *plat_dev;
142 cc_sram_addr_t mlli_sram_addr;
143 void *buff_mgr_handle;
Gilad Ben-Yossef63ee04c2018-01-22 09:27:01 +0000144 void *cipher_handle;
Gilad Ben-Yossef63893812018-01-22 09:27:02 +0000145 void *hash_handle;
Gilad Ben-Yossefff27e852018-01-22 09:27:03 +0000146 void *aead_handle;
Gilad Ben-Yossef4c3f9722018-01-22 09:27:00 +0000147 void *request_mgr_handle;
Gilad Ben-Yossefab8ec962018-01-22 09:27:04 +0000148 void *fips_handle;
Gilad Ben-Yossef4c3f9722018-01-22 09:27:00 +0000149 void *sram_mgr_handle;
150 void *debugfs;
151 struct clk *clk;
152 bool coherent;
Gilad Ben-Yossef27b3b222018-02-19 14:51:23 +0000153 char *hw_rev_name;
154 enum cc_hw_rev hw_rev;
Gilad Ben-Yossef27b3b222018-02-19 14:51:23 +0000155 u32 axim_mon_offset;
Gilad Ben-Yossef281a58c2018-05-24 15:19:06 +0100156 u32 sig_offset;
157 u32 ver_offset;
Gilad Ben-Yossef1c876a92018-11-13 09:40:35 +0000158 int std_bodies;
Gilad Ben-Yosseff98f6e22019-04-18 16:38:39 +0300159 bool sec_disabled;
Gilad Ben-Yossefcadfd892019-04-18 16:38:40 +0300160 u32 comp_mask;
Gilad Ben-Yossef15fd2562020-01-16 12:14:43 +0200161 bool pm_on;
Gilad Ben-Yossef4c3f9722018-01-22 09:27:00 +0000162};
163
164struct cc_crypto_alg {
165 struct list_head entry;
166 int cipher_mode;
167 int flow_mode; /* Note: currently, refers to the cipher mode only. */
168 int auth_mode;
Gilad Ben-Yossef63ee04c2018-01-22 09:27:01 +0000169 unsigned int data_unit;
Gilad Ben-Yossef4c3f9722018-01-22 09:27:00 +0000170 struct cc_drvdata *drvdata;
Gilad Ben-Yossef63ee04c2018-01-22 09:27:01 +0000171 struct skcipher_alg skcipher_alg;
Gilad Ben-Yossefff27e852018-01-22 09:27:03 +0000172 struct aead_alg aead_alg;
Gilad Ben-Yossef4c3f9722018-01-22 09:27:00 +0000173};
174
175struct cc_alg_template {
176 char name[CRYPTO_MAX_ALG_NAME];
177 char driver_name[CRYPTO_MAX_ALG_NAME];
178 unsigned int blocksize;
Gilad Ben-Yossef4c3f9722018-01-22 09:27:00 +0000179 union {
180 struct skcipher_alg skcipher;
181 struct aead_alg aead;
182 } template_u;
183 int cipher_mode;
184 int flow_mode; /* Note: currently, refers to the cipher mode only. */
185 int auth_mode;
Gilad Ben-Yossef27b3b222018-02-19 14:51:23 +0000186 u32 min_hw_rev;
Gilad Ben-Yossef1c876a92018-11-13 09:40:35 +0000187 enum cc_std_body std_body;
Gilad Ben-Yosseff98f6e22019-04-18 16:38:39 +0300188 bool sec_func;
Gilad Ben-Yossef63ee04c2018-01-22 09:27:01 +0000189 unsigned int data_unit;
Gilad Ben-Yossef4c3f9722018-01-22 09:27:00 +0000190 struct cc_drvdata *drvdata;
191};
192
193struct async_gen_req_ctx {
194 dma_addr_t iv_dma_addr;
Gilad Ben-Yossefe8662a62019-04-18 16:39:05 +0300195 u8 *iv;
Gilad Ben-Yossef4c3f9722018-01-22 09:27:00 +0000196 enum drv_crypto_direction op_type;
197};
198
199static inline struct device *drvdata_to_dev(struct cc_drvdata *drvdata)
200{
201 return &drvdata->plat_dev->dev;
202}
203
204void __dump_byte_array(const char *name, const u8 *buf, size_t len);
205static inline void dump_byte_array(const char *name, const u8 *the_array,
206 size_t size)
207{
208 if (cc_dump_bytes)
209 __dump_byte_array(name, the_array, size);
210}
211
Ofir Drangd84f6262019-06-17 11:46:28 +0300212bool cc_wait_for_reset_completion(struct cc_drvdata *drvdata);
Gilad Ben-Yossef4c3f9722018-01-22 09:27:00 +0000213int init_cc_regs(struct cc_drvdata *drvdata, bool is_probe);
214void fini_cc_regs(struct cc_drvdata *drvdata);
215int cc_clk_on(struct cc_drvdata *drvdata);
216void cc_clk_off(struct cc_drvdata *drvdata);
Yael Chemlaf1e52fd2018-10-18 13:59:57 +0100217unsigned int cc_get_default_hash_len(struct cc_drvdata *drvdata);
Gilad Ben-Yossef4c3f9722018-01-22 09:27:00 +0000218
219static inline void cc_iowrite(struct cc_drvdata *drvdata, u32 reg, u32 val)
220{
221 iowrite32(val, (drvdata->cc_base + reg));
222}
223
224static inline u32 cc_ioread(struct cc_drvdata *drvdata, u32 reg)
225{
226 return ioread32(drvdata->cc_base + reg);
227}
228
229static inline gfp_t cc_gfp_flags(struct crypto_async_request *req)
230{
231 return (req->flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
232 GFP_KERNEL : GFP_ATOMIC;
233}
234
Gilad Ben-Yossef27b3b222018-02-19 14:51:23 +0000235static inline void set_queue_last_ind(struct cc_drvdata *drvdata,
236 struct cc_hw_desc *pdesc)
237{
238 if (drvdata->hw_rev >= CC_HW_REV_712)
239 set_queue_last_ind_bit(pdesc);
240}
241
Gilad Ben-Yossef4c3f9722018-01-22 09:27:00 +0000242#endif /*__CC_DRIVER_H__*/