cc_driver.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright (C) 2012-2019 ARM Limited (or its affiliates). */
  3. /* \file cc_driver.h
  4. * ARM CryptoCell Linux Crypto Driver
  5. */
  6. #ifndef __CC_DRIVER_H__
  7. #define __CC_DRIVER_H__
  8. #ifdef COMP_IN_WQ
  9. #include <linux/workqueue.h>
  10. #else
  11. #include <linux/interrupt.h>
  12. #endif
  13. #include <linux/dma-mapping.h>
  14. #include <crypto/algapi.h>
  15. #include <crypto/internal/skcipher.h>
  16. #include <crypto/aes.h>
  17. #include <crypto/sha.h>
  18. #include <crypto/aead.h>
  19. #include <crypto/authenc.h>
  20. #include <crypto/hash.h>
  21. #include <crypto/skcipher.h>
  22. #include <linux/version.h>
  23. #include <linux/clk.h>
  24. #include <linux/platform_device.h>
  25. /* Registers definitions from shared/hw/ree_include */
  26. #include "cc_host_regs.h"
  27. #define CC_DEV_SHA_MAX 512
  28. #include "cc_crypto_ctx.h"
  29. #include "cc_hw_queue_defs.h"
  30. #include "cc_sram_mgr.h"
  31. extern bool cc_dump_desc;
  32. extern bool cc_dump_bytes;
  33. #define DRV_MODULE_VERSION "5.0"
  34. enum cc_hw_rev {
  35. CC_HW_REV_630 = 630,
  36. CC_HW_REV_710 = 710,
  37. CC_HW_REV_712 = 712,
  38. CC_HW_REV_713 = 713
  39. };
  40. enum cc_std_body {
  41. CC_STD_NIST = 0x1,
  42. CC_STD_OSCCA = 0x2,
  43. CC_STD_ALL = 0x3
  44. };
  45. #define CC_COHERENT_CACHE_PARAMS 0xEEE
  46. #define CC_PINS_FULL 0x0
  47. #define CC_PINS_SLIM 0x9F
  48. /* Maximum DMA mask supported by IP */
  49. #define DMA_BIT_MASK_LEN 48
  50. #define CC_AXI_IRQ_MASK ((1 << CC_AXIM_CFG_BRESPMASK_BIT_SHIFT) | \
  51. (1 << CC_AXIM_CFG_RRESPMASK_BIT_SHIFT) | \
  52. (1 << CC_AXIM_CFG_INFLTMASK_BIT_SHIFT) | \
  53. (1 << CC_AXIM_CFG_COMPMASK_BIT_SHIFT))
  54. #define CC_AXI_ERR_IRQ_MASK BIT(CC_HOST_IRR_AXI_ERR_INT_BIT_SHIFT)
  55. #define CC_COMP_IRQ_MASK BIT(CC_HOST_IRR_AXIM_COMP_INT_BIT_SHIFT)
  56. #define CC_SECURITY_DISABLED_MASK BIT(CC_SECURITY_DISABLED_VALUE_BIT_SHIFT)
  57. #define CC_NVM_IS_IDLE_MASK BIT(CC_NVM_IS_IDLE_VALUE_BIT_SHIFT)
  58. #define AXIM_MON_COMP_VALUE GENMASK(CC_AXIM_MON_COMP_VALUE_BIT_SIZE + \
  59. CC_AXIM_MON_COMP_VALUE_BIT_SHIFT, \
  60. CC_AXIM_MON_COMP_VALUE_BIT_SHIFT)
  61. #define CC_CPP_AES_ABORT_MASK ( \
  62. BIT(CC_HOST_IMR_REE_OP_ABORTED_AES_0_MASK_BIT_SHIFT) | \
  63. BIT(CC_HOST_IMR_REE_OP_ABORTED_AES_1_MASK_BIT_SHIFT) | \
  64. BIT(CC_HOST_IMR_REE_OP_ABORTED_AES_2_MASK_BIT_SHIFT) | \
  65. BIT(CC_HOST_IMR_REE_OP_ABORTED_AES_3_MASK_BIT_SHIFT) | \
  66. BIT(CC_HOST_IMR_REE_OP_ABORTED_AES_4_MASK_BIT_SHIFT) | \
  67. BIT(CC_HOST_IMR_REE_OP_ABORTED_AES_5_MASK_BIT_SHIFT) | \
  68. BIT(CC_HOST_IMR_REE_OP_ABORTED_AES_6_MASK_BIT_SHIFT) | \
  69. BIT(CC_HOST_IMR_REE_OP_ABORTED_AES_7_MASK_BIT_SHIFT))
  70. #define CC_CPP_SM4_ABORT_MASK ( \
  71. BIT(CC_HOST_IMR_REE_OP_ABORTED_SM_0_MASK_BIT_SHIFT) | \
  72. BIT(CC_HOST_IMR_REE_OP_ABORTED_SM_1_MASK_BIT_SHIFT) | \
  73. BIT(CC_HOST_IMR_REE_OP_ABORTED_SM_2_MASK_BIT_SHIFT) | \
  74. BIT(CC_HOST_IMR_REE_OP_ABORTED_SM_3_MASK_BIT_SHIFT) | \
  75. BIT(CC_HOST_IMR_REE_OP_ABORTED_SM_4_MASK_BIT_SHIFT) | \
  76. BIT(CC_HOST_IMR_REE_OP_ABORTED_SM_5_MASK_BIT_SHIFT) | \
  77. BIT(CC_HOST_IMR_REE_OP_ABORTED_SM_6_MASK_BIT_SHIFT) | \
  78. BIT(CC_HOST_IMR_REE_OP_ABORTED_SM_7_MASK_BIT_SHIFT))
  79. /* Register name mangling macro */
  80. #define CC_REG(reg_name) CC_ ## reg_name ## _REG_OFFSET
  81. /* TEE FIPS status interrupt */
  82. #define CC_GPR0_IRQ_MASK BIT(CC_HOST_IRR_GPR0_BIT_SHIFT)
  83. #define CC_CRA_PRIO 400
  84. #define MIN_HW_QUEUE_SIZE 50 /* Minimum size required for proper function */
  85. #define MAX_REQUEST_QUEUE_SIZE 4096
  86. #define MAX_MLLI_BUFF_SIZE 2080
  87. /* Definitions for HW descriptors DIN/DOUT fields */
  88. #define NS_BIT 1
  89. #define AXI_ID 0
  90. /* AXI_ID is not actually the AXI ID of the transaction but the value of AXI_ID
  91. * field in the HW descriptor. The DMA engine +8 that value.
  92. */
  93. struct cc_cpp_req {
  94. bool is_cpp;
  95. enum cc_cpp_alg alg;
  96. u8 slot;
  97. };
  98. #define CC_MAX_IVGEN_DMA_ADDRESSES 3
  99. struct cc_crypto_req {
  100. void (*user_cb)(struct device *dev, void *req, int err);
  101. void *user_arg;
  102. struct completion seq_compl; /* request completion */
  103. struct cc_cpp_req cpp;
  104. };
  105. /**
  106. * struct cc_drvdata - driver private data context
  107. * @cc_base: virt address of the CC registers
  108. * @irq: device IRQ number
  109. * @irq_mask: Interrupt mask shadow (1 for masked interrupts)
  110. */
  111. struct cc_drvdata {
  112. void __iomem *cc_base;
  113. int irq;
  114. u32 irq_mask;
  115. struct completion hw_queue_avail; /* wait for HW queue availability */
  116. struct platform_device *plat_dev;
  117. cc_sram_addr_t mlli_sram_addr;
  118. void *buff_mgr_handle;
  119. void *cipher_handle;
  120. void *hash_handle;
  121. void *aead_handle;
  122. void *request_mgr_handle;
  123. void *fips_handle;
  124. void *sram_mgr_handle;
  125. void *debugfs;
  126. struct clk *clk;
  127. bool coherent;
  128. char *hw_rev_name;
  129. enum cc_hw_rev hw_rev;
  130. u32 axim_mon_offset;
  131. u32 sig_offset;
  132. u32 ver_offset;
  133. int std_bodies;
  134. bool sec_disabled;
  135. u32 comp_mask;
  136. bool pm_on;
  137. };
  138. struct cc_crypto_alg {
  139. struct list_head entry;
  140. int cipher_mode;
  141. int flow_mode; /* Note: currently, refers to the cipher mode only. */
  142. int auth_mode;
  143. unsigned int data_unit;
  144. struct cc_drvdata *drvdata;
  145. struct skcipher_alg skcipher_alg;
  146. struct aead_alg aead_alg;
  147. };
  148. struct cc_alg_template {
  149. char name[CRYPTO_MAX_ALG_NAME];
  150. char driver_name[CRYPTO_MAX_ALG_NAME];
  151. unsigned int blocksize;
  152. union {
  153. struct skcipher_alg skcipher;
  154. struct aead_alg aead;
  155. } template_u;
  156. int cipher_mode;
  157. int flow_mode; /* Note: currently, refers to the cipher mode only. */
  158. int auth_mode;
  159. u32 min_hw_rev;
  160. enum cc_std_body std_body;
  161. bool sec_func;
  162. unsigned int data_unit;
  163. struct cc_drvdata *drvdata;
  164. };
  165. struct async_gen_req_ctx {
  166. dma_addr_t iv_dma_addr;
  167. u8 *iv;
  168. enum drv_crypto_direction op_type;
  169. };
  170. static inline struct device *drvdata_to_dev(struct cc_drvdata *drvdata)
  171. {
  172. return &drvdata->plat_dev->dev;
  173. }
  174. void __dump_byte_array(const char *name, const u8 *buf, size_t len);
  175. static inline void dump_byte_array(const char *name, const u8 *the_array,
  176. size_t size)
  177. {
  178. if (cc_dump_bytes)
  179. __dump_byte_array(name, the_array, size);
  180. }
  181. bool cc_wait_for_reset_completion(struct cc_drvdata *drvdata);
  182. int init_cc_regs(struct cc_drvdata *drvdata, bool is_probe);
  183. void fini_cc_regs(struct cc_drvdata *drvdata);
  184. int cc_clk_on(struct cc_drvdata *drvdata);
  185. void cc_clk_off(struct cc_drvdata *drvdata);
  186. unsigned int cc_get_default_hash_len(struct cc_drvdata *drvdata);
  187. static inline void cc_iowrite(struct cc_drvdata *drvdata, u32 reg, u32 val)
  188. {
  189. iowrite32(val, (drvdata->cc_base + reg));
  190. }
  191. static inline u32 cc_ioread(struct cc_drvdata *drvdata, u32 reg)
  192. {
  193. return ioread32(drvdata->cc_base + reg);
  194. }
  195. static inline gfp_t cc_gfp_flags(struct crypto_async_request *req)
  196. {
  197. return (req->flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
  198. GFP_KERNEL : GFP_ATOMIC;
  199. }
  200. static inline void set_queue_last_ind(struct cc_drvdata *drvdata,
  201. struct cc_hw_desc *pdesc)
  202. {
  203. if (drvdata->hw_rev >= CC_HW_REV_712)
  204. set_queue_last_ind_bit(pdesc);
  205. }
  206. #endif /*__CC_DRIVER_H__*/