ccp-dev.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * AMD Cryptographic Coprocessor (CCP) driver
  3. *
  4. * Copyright (C) 2013,2016 Advanced Micro Devices, Inc.
  5. *
  6. * Author: Tom Lendacky <thomas.lendacky@amd.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #ifndef __CCP_DEV_H__
  13. #define __CCP_DEV_H__
  14. #include <linux/device.h>
  15. #include <linux/pci.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/mutex.h>
  18. #include <linux/list.h>
  19. #include <linux/wait.h>
  20. #include <linux/dmapool.h>
  21. #include <linux/hw_random.h>
  22. #include <linux/bitops.h>
  23. #define MAX_CCP_NAME_LEN 16
  24. #define MAX_DMAPOOL_NAME_LEN 32
  25. #define MAX_HW_QUEUES 5
  26. #define MAX_CMD_QLEN 100
  27. #define TRNG_RETRIES 10
  28. #define CACHE_NONE 0x00
  29. #define CACHE_WB_NO_ALLOC 0xb7
  30. /****** Register Mappings ******/
  31. #define Q_MASK_REG 0x000
  32. #define TRNG_OUT_REG 0x00c
  33. #define IRQ_MASK_REG 0x040
  34. #define IRQ_STATUS_REG 0x200
  35. #define DEL_CMD_Q_JOB 0x124
  36. #define DEL_Q_ACTIVE 0x00000200
  37. #define DEL_Q_ID_SHIFT 6
  38. #define CMD_REQ0 0x180
  39. #define CMD_REQ_INCR 0x04
  40. #define CMD_Q_STATUS_BASE 0x210
  41. #define CMD_Q_INT_STATUS_BASE 0x214
  42. #define CMD_Q_STATUS_INCR 0x20
  43. #define CMD_Q_CACHE_BASE 0x228
  44. #define CMD_Q_CACHE_INC 0x20
  45. #define CMD_Q_ERROR(__qs) ((__qs) & 0x0000003f)
  46. #define CMD_Q_DEPTH(__qs) (((__qs) >> 12) & 0x0000000f)
  47. /****** REQ0 Related Values ******/
  48. #define REQ0_WAIT_FOR_WRITE 0x00000004
  49. #define REQ0_INT_ON_COMPLETE 0x00000002
  50. #define REQ0_STOP_ON_COMPLETE 0x00000001
  51. #define REQ0_CMD_Q_SHIFT 9
  52. #define REQ0_JOBID_SHIFT 3
  53. /****** REQ1 Related Values ******/
  54. #define REQ1_PROTECT_SHIFT 27
  55. #define REQ1_ENGINE_SHIFT 23
  56. #define REQ1_KEY_KSB_SHIFT 2
  57. #define REQ1_EOM 0x00000002
  58. #define REQ1_INIT 0x00000001
  59. /* AES Related Values */
  60. #define REQ1_AES_TYPE_SHIFT 21
  61. #define REQ1_AES_MODE_SHIFT 18
  62. #define REQ1_AES_ACTION_SHIFT 17
  63. #define REQ1_AES_CFB_SIZE_SHIFT 10
  64. /* XTS-AES Related Values */
  65. #define REQ1_XTS_AES_SIZE_SHIFT 10
  66. /* SHA Related Values */
  67. #define REQ1_SHA_TYPE_SHIFT 21
  68. /* RSA Related Values */
  69. #define REQ1_RSA_MOD_SIZE_SHIFT 10
  70. /* Pass-Through Related Values */
  71. #define REQ1_PT_BW_SHIFT 12
  72. #define REQ1_PT_BS_SHIFT 10
  73. /* ECC Related Values */
  74. #define REQ1_ECC_AFFINE_CONVERT 0x00200000
  75. #define REQ1_ECC_FUNCTION_SHIFT 18
  76. /****** REQ4 Related Values ******/
  77. #define REQ4_KSB_SHIFT 18
  78. #define REQ4_MEMTYPE_SHIFT 16
  79. /****** REQ6 Related Values ******/
  80. #define REQ6_MEMTYPE_SHIFT 16
  81. /****** Key Storage Block ******/
  82. #define KSB_START 77
  83. #define KSB_END 127
  84. #define KSB_COUNT (KSB_END - KSB_START + 1)
  85. #define CCP_KSB_BITS 256
  86. #define CCP_KSB_BYTES 32
  87. #define CCP_JOBID_MASK 0x0000003f
  88. #define CCP_DMAPOOL_MAX_SIZE 64
  89. #define CCP_DMAPOOL_ALIGN BIT(5)
  90. #define CCP_REVERSE_BUF_SIZE 64
  91. #define CCP_AES_KEY_KSB_COUNT 1
  92. #define CCP_AES_CTX_KSB_COUNT 1
  93. #define CCP_XTS_AES_KEY_KSB_COUNT 1
  94. #define CCP_XTS_AES_CTX_KSB_COUNT 1
  95. #define CCP_SHA_KSB_COUNT 1
  96. #define CCP_RSA_MAX_WIDTH 4096
  97. #define CCP_PASSTHRU_BLOCKSIZE 256
  98. #define CCP_PASSTHRU_MASKSIZE 32
  99. #define CCP_PASSTHRU_KSB_COUNT 1
  100. #define CCP_ECC_MODULUS_BYTES 48 /* 384-bits */
  101. #define CCP_ECC_MAX_OPERANDS 6
  102. #define CCP_ECC_MAX_OUTPUTS 3
  103. #define CCP_ECC_SRC_BUF_SIZE 448
  104. #define CCP_ECC_DST_BUF_SIZE 192
  105. #define CCP_ECC_OPERAND_SIZE 64
  106. #define CCP_ECC_OUTPUT_SIZE 64
  107. #define CCP_ECC_RESULT_OFFSET 60
  108. #define CCP_ECC_RESULT_SUCCESS 0x0001
  109. struct ccp_op;
  110. /* Structure for computation functions that are device-specific */
  111. struct ccp_actions {
  112. int (*perform_aes)(struct ccp_op *);
  113. int (*perform_xts_aes)(struct ccp_op *);
  114. int (*perform_sha)(struct ccp_op *);
  115. int (*perform_rsa)(struct ccp_op *);
  116. int (*perform_passthru)(struct ccp_op *);
  117. int (*perform_ecc)(struct ccp_op *);
  118. int (*init)(struct ccp_device *);
  119. void (*destroy)(struct ccp_device *);
  120. irqreturn_t (*irqhandler)(int, void *);
  121. };
  122. /* Structure to hold CCP version-specific values */
  123. struct ccp_vdata {
  124. unsigned int version;
  125. struct ccp_actions *perform;
  126. };
  127. extern struct ccp_vdata ccpv3;
  128. struct ccp_device;
  129. struct ccp_cmd;
  130. struct ccp_cmd_queue {
  131. struct ccp_device *ccp;
  132. /* Queue identifier */
  133. u32 id;
  134. /* Queue dma pool */
  135. struct dma_pool *dma_pool;
  136. /* Queue reserved KSB regions */
  137. u32 ksb_key;
  138. u32 ksb_ctx;
  139. /* Queue processing thread */
  140. struct task_struct *kthread;
  141. unsigned int active;
  142. unsigned int suspended;
  143. /* Number of free command slots available */
  144. unsigned int free_slots;
  145. /* Interrupt masks */
  146. u32 int_ok;
  147. u32 int_err;
  148. /* Register addresses for queue */
  149. void __iomem *reg_status;
  150. void __iomem *reg_int_status;
  151. /* Status values from job */
  152. u32 int_status;
  153. u32 q_status;
  154. u32 q_int_status;
  155. u32 cmd_error;
  156. /* Interrupt wait queue */
  157. wait_queue_head_t int_queue;
  158. unsigned int int_rcvd;
  159. } ____cacheline_aligned;
  160. struct ccp_device {
  161. struct list_head entry;
  162. struct ccp_vdata *vdata;
  163. unsigned int ord;
  164. char name[MAX_CCP_NAME_LEN];
  165. char rngname[MAX_CCP_NAME_LEN];
  166. struct device *dev;
  167. /*
  168. * Bus specific device information
  169. */
  170. void *dev_specific;
  171. int (*get_irq)(struct ccp_device *ccp);
  172. void (*free_irq)(struct ccp_device *ccp);
  173. unsigned int irq;
  174. /*
  175. * I/O area used for device communication. The register mapping
  176. * starts at an offset into the mapped bar.
  177. * The CMD_REQx registers and the Delete_Cmd_Queue_Job register
  178. * need to be protected while a command queue thread is accessing
  179. * them.
  180. */
  181. struct mutex req_mutex ____cacheline_aligned;
  182. void __iomem *io_map;
  183. void __iomem *io_regs;
  184. /*
  185. * Master lists that all cmds are queued on. Because there can be
  186. * more than one CCP command queue that can process a cmd a separate
  187. * backlog list is neeeded so that the backlog completion call
  188. * completes before the cmd is available for execution.
  189. */
  190. spinlock_t cmd_lock ____cacheline_aligned;
  191. unsigned int cmd_count;
  192. struct list_head cmd;
  193. struct list_head backlog;
  194. /*
  195. * The command queues. These represent the queues available on the
  196. * CCP that are available for processing cmds
  197. */
  198. struct ccp_cmd_queue cmd_q[MAX_HW_QUEUES];
  199. unsigned int cmd_q_count;
  200. /*
  201. * Support for the CCP True RNG
  202. */
  203. struct hwrng hwrng;
  204. unsigned int hwrng_retries;
  205. /*
  206. * A counter used to generate job-ids for cmds submitted to the CCP
  207. */
  208. atomic_t current_id ____cacheline_aligned;
  209. /*
  210. * The CCP uses key storage blocks (KSB) to maintain context for certain
  211. * operations. To prevent multiple cmds from using the same KSB range
  212. * a command queue reserves a KSB range for the duration of the cmd.
  213. * Each queue, will however, reserve 2 KSB blocks for operations that
  214. * only require single KSB entries (eg. AES context/iv and key) in order
  215. * to avoid allocation contention. This will reserve at most 10 KSB
  216. * entries, leaving 40 KSB entries available for dynamic allocation.
  217. */
  218. struct mutex ksb_mutex ____cacheline_aligned;
  219. DECLARE_BITMAP(ksb, KSB_COUNT);
  220. wait_queue_head_t ksb_queue;
  221. unsigned int ksb_avail;
  222. unsigned int ksb_count;
  223. u32 ksb_start;
  224. /* Suspend support */
  225. unsigned int suspending;
  226. wait_queue_head_t suspend_queue;
  227. /* DMA caching attribute support */
  228. unsigned int axcache;
  229. };
  230. enum ccp_memtype {
  231. CCP_MEMTYPE_SYSTEM = 0,
  232. CCP_MEMTYPE_KSB,
  233. CCP_MEMTYPE_LOCAL,
  234. CCP_MEMTYPE__LAST,
  235. };
  236. struct ccp_dma_info {
  237. dma_addr_t address;
  238. unsigned int offset;
  239. unsigned int length;
  240. enum dma_data_direction dir;
  241. };
  242. struct ccp_dm_workarea {
  243. struct device *dev;
  244. struct dma_pool *dma_pool;
  245. unsigned int length;
  246. u8 *address;
  247. struct ccp_dma_info dma;
  248. };
  249. struct ccp_sg_workarea {
  250. struct scatterlist *sg;
  251. int nents;
  252. struct scatterlist *dma_sg;
  253. struct device *dma_dev;
  254. unsigned int dma_count;
  255. enum dma_data_direction dma_dir;
  256. unsigned int sg_used;
  257. u64 bytes_left;
  258. };
  259. struct ccp_data {
  260. struct ccp_sg_workarea sg_wa;
  261. struct ccp_dm_workarea dm_wa;
  262. };
  263. struct ccp_mem {
  264. enum ccp_memtype type;
  265. union {
  266. struct ccp_dma_info dma;
  267. u32 ksb;
  268. } u;
  269. };
  270. struct ccp_aes_op {
  271. enum ccp_aes_type type;
  272. enum ccp_aes_mode mode;
  273. enum ccp_aes_action action;
  274. };
  275. struct ccp_xts_aes_op {
  276. enum ccp_aes_action action;
  277. enum ccp_xts_aes_unit_size unit_size;
  278. };
  279. struct ccp_sha_op {
  280. enum ccp_sha_type type;
  281. u64 msg_bits;
  282. };
  283. struct ccp_rsa_op {
  284. u32 mod_size;
  285. u32 input_len;
  286. };
  287. struct ccp_passthru_op {
  288. enum ccp_passthru_bitwise bit_mod;
  289. enum ccp_passthru_byteswap byte_swap;
  290. };
  291. struct ccp_ecc_op {
  292. enum ccp_ecc_function function;
  293. };
  294. struct ccp_op {
  295. struct ccp_cmd_queue *cmd_q;
  296. u32 jobid;
  297. u32 ioc;
  298. u32 soc;
  299. u32 ksb_key;
  300. u32 ksb_ctx;
  301. u32 init;
  302. u32 eom;
  303. struct ccp_mem src;
  304. struct ccp_mem dst;
  305. union {
  306. struct ccp_aes_op aes;
  307. struct ccp_xts_aes_op xts;
  308. struct ccp_sha_op sha;
  309. struct ccp_rsa_op rsa;
  310. struct ccp_passthru_op passthru;
  311. struct ccp_ecc_op ecc;
  312. } u;
  313. };
  314. static inline u32 ccp_addr_lo(struct ccp_dma_info *info)
  315. {
  316. return lower_32_bits(info->address + info->offset);
  317. }
  318. static inline u32 ccp_addr_hi(struct ccp_dma_info *info)
  319. {
  320. return upper_32_bits(info->address + info->offset) & 0x0000ffff;
  321. }
  322. int ccp_pci_init(void);
  323. void ccp_pci_exit(void);
  324. int ccp_platform_init(void);
  325. void ccp_platform_exit(void);
  326. void ccp_add_device(struct ccp_device *ccp);
  327. void ccp_del_device(struct ccp_device *ccp);
  328. struct ccp_device *ccp_alloc_struct(struct device *dev);
  329. bool ccp_queues_suspended(struct ccp_device *ccp);
  330. int ccp_cmd_queue_thread(void *data);
  331. int ccp_run_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd);
  332. #endif