mtk-platform.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Driver for EIP97 cryptographic accelerator.
  3. *
  4. * Copyright (c) 2016 Ryder Lee <ryder.lee@mediatek.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #ifndef __MTK_PLATFORM_H_
  12. #define __MTK_PLATFORM_H_
  13. #include <crypto/algapi.h>
  14. #include <crypto/internal/aead.h>
  15. #include <crypto/internal/hash.h>
  16. #include <crypto/scatterwalk.h>
  17. #include <crypto/skcipher.h>
  18. #include <linux/crypto.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/scatterlist.h>
  22. #include "mtk-regs.h"
  23. #define MTK_RDR_PROC_THRESH BIT(0)
  24. #define MTK_RDR_PROC_MODE BIT(23)
  25. #define MTK_CNT_RST BIT(31)
  26. #define MTK_IRQ_RDR0 BIT(1)
  27. #define MTK_IRQ_RDR1 BIT(3)
  28. #define MTK_IRQ_RDR2 BIT(5)
  29. #define MTK_IRQ_RDR3 BIT(7)
  30. #define SIZE_IN_WORDS(x) ((x) >> 2)
  31. /**
  32. * Ring 0/1 are used by AES encrypt and decrypt.
  33. * Ring 2/3 are used by SHA.
  34. */
  35. enum {
  36. MTK_RING0,
  37. MTK_RING1,
  38. MTK_RING2,
  39. MTK_RING3,
  40. MTK_RING_MAX
  41. };
  42. #define MTK_REC_NUM (MTK_RING_MAX / 2)
  43. #define MTK_IRQ_NUM 5
  44. /**
  45. * struct mtk_desc - DMA descriptor
  46. * @hdr: the descriptor control header
  47. * @buf: DMA address of input buffer segment
  48. * @ct: DMA address of command token that control operation flow
  49. * @ct_hdr: the command token control header
  50. * @tag: the user-defined field
  51. * @tfm: DMA address of transform state
  52. * @bound: align descriptors offset boundary
  53. *
  54. * Structure passed to the crypto engine to describe where source
  55. * data needs to be fetched and how it needs to be processed.
  56. */
  57. struct mtk_desc {
  58. __le32 hdr;
  59. __le32 buf;
  60. __le32 ct;
  61. __le32 ct_hdr;
  62. __le32 tag;
  63. __le32 tfm;
  64. __le32 bound[2];
  65. };
  66. #define MTK_DESC_NUM 512
  67. #define MTK_DESC_OFF SIZE_IN_WORDS(sizeof(struct mtk_desc))
  68. #define MTK_DESC_SZ (MTK_DESC_OFF - 2)
  69. #define MTK_DESC_RING_SZ ((sizeof(struct mtk_desc) * MTK_DESC_NUM))
  70. #define MTK_DESC_CNT(x) ((MTK_DESC_OFF * (x)) << 2)
  71. #define MTK_DESC_LAST cpu_to_le32(BIT(22))
  72. #define MTK_DESC_FIRST cpu_to_le32(BIT(23))
  73. #define MTK_DESC_BUF_LEN(x) cpu_to_le32(x)
  74. #define MTK_DESC_CT_LEN(x) cpu_to_le32((x) << 24)
  75. /**
  76. * struct mtk_ring - Descriptor ring
  77. * @cmd_base: pointer to command descriptor ring base
  78. * @cmd_next: pointer to the next command descriptor
  79. * @cmd_dma: DMA address of command descriptor ring
  80. * @res_base: pointer to result descriptor ring base
  81. * @res_next: pointer to the next result descriptor
  82. * @res_prev: pointer to the previous result descriptor
  83. * @res_dma: DMA address of result descriptor ring
  84. *
  85. * A descriptor ring is a circular buffer that is used to manage
  86. * one or more descriptors. There are two type of descriptor rings;
  87. * the command descriptor ring and result descriptor ring.
  88. */
  89. struct mtk_ring {
  90. struct mtk_desc *cmd_base;
  91. struct mtk_desc *cmd_next;
  92. dma_addr_t cmd_dma;
  93. struct mtk_desc *res_base;
  94. struct mtk_desc *res_next;
  95. struct mtk_desc *res_prev;
  96. dma_addr_t res_dma;
  97. };
  98. /**
  99. * struct mtk_aes_dma - Structure that holds sg list info
  100. * @sg: pointer to scatter-gather list
  101. * @nents: number of entries in the sg list
  102. * @remainder: remainder of sg list
  103. * @sg_len: number of entries in the sg mapped list
  104. */
  105. struct mtk_aes_dma {
  106. struct scatterlist *sg;
  107. int nents;
  108. u32 remainder;
  109. u32 sg_len;
  110. };
  111. struct mtk_aes_base_ctx;
  112. struct mtk_aes_rec;
  113. struct mtk_cryp;
  114. typedef int (*mtk_aes_fn)(struct mtk_cryp *cryp, struct mtk_aes_rec *aes);
  115. /**
  116. * struct mtk_aes_rec - AES operation record
  117. * @cryp: pointer to Cryptographic device
  118. * @queue: crypto request queue
  119. * @areq: pointer to async request
  120. * @done_task: the tasklet is use in AES interrupt
  121. * @queue_task: the tasklet is used to dequeue request
  122. * @ctx: pointer to current context
  123. * @src: the structure that holds source sg list info
  124. * @dst: the structure that holds destination sg list info
  125. * @aligned_sg: the scatter list is use to alignment
  126. * @real_dst: pointer to the destination sg list
  127. * @resume: pointer to resume function
  128. * @total: request buffer length
  129. * @buf: pointer to page buffer
  130. * @id: the current use of ring
  131. * @flags: it's describing AES operation state
  132. * @lock: the async queue lock
  133. *
  134. * Structure used to record AES execution state.
  135. */
  136. struct mtk_aes_rec {
  137. struct mtk_cryp *cryp;
  138. struct crypto_queue queue;
  139. struct crypto_async_request *areq;
  140. struct tasklet_struct done_task;
  141. struct tasklet_struct queue_task;
  142. struct mtk_aes_base_ctx *ctx;
  143. struct mtk_aes_dma src;
  144. struct mtk_aes_dma dst;
  145. struct scatterlist aligned_sg;
  146. struct scatterlist *real_dst;
  147. mtk_aes_fn resume;
  148. size_t total;
  149. void *buf;
  150. u8 id;
  151. unsigned long flags;
  152. /* queue lock */
  153. spinlock_t lock;
  154. };
  155. /**
  156. * struct mtk_sha_rec - SHA operation record
  157. * @cryp: pointer to Cryptographic device
  158. * @queue: crypto request queue
  159. * @req: pointer to ahash request
  160. * @done_task: the tasklet is use in SHA interrupt
  161. * @queue_task: the tasklet is used to dequeue request
  162. * @id: the current use of ring
  163. * @flags: it's describing SHA operation state
  164. * @lock: the async queue lock
  165. *
  166. * Structure used to record SHA execution state.
  167. */
  168. struct mtk_sha_rec {
  169. struct mtk_cryp *cryp;
  170. struct crypto_queue queue;
  171. struct ahash_request *req;
  172. struct tasklet_struct done_task;
  173. struct tasklet_struct queue_task;
  174. u8 id;
  175. unsigned long flags;
  176. /* queue lock */
  177. spinlock_t lock;
  178. };
  179. /**
  180. * struct mtk_cryp - Cryptographic device
  181. * @base: pointer to mapped register I/O base
  182. * @dev: pointer to device
  183. * @clk_cryp: pointer to crypto clock
  184. * @irq: global system and rings IRQ
  185. * @ring: pointer to descriptor rings
  186. * @aes: pointer to operation record of AES
  187. * @sha: pointer to operation record of SHA
  188. * @aes_list: device list of AES
  189. * @sha_list: device list of SHA
  190. * @rec: it's used to select SHA record for tfm
  191. *
  192. * Structure storing cryptographic device information.
  193. */
  194. struct mtk_cryp {
  195. void __iomem *base;
  196. struct device *dev;
  197. struct clk *clk_cryp;
  198. int irq[MTK_IRQ_NUM];
  199. struct mtk_ring *ring[MTK_RING_MAX];
  200. struct mtk_aes_rec *aes[MTK_REC_NUM];
  201. struct mtk_sha_rec *sha[MTK_REC_NUM];
  202. struct list_head aes_list;
  203. struct list_head sha_list;
  204. bool rec;
  205. };
  206. int mtk_cipher_alg_register(struct mtk_cryp *cryp);
  207. void mtk_cipher_alg_release(struct mtk_cryp *cryp);
  208. int mtk_hash_alg_register(struct mtk_cryp *cryp);
  209. void mtk_hash_alg_release(struct mtk_cryp *cryp);
  210. #endif /* __MTK_PLATFORM_H_ */