ccp-dev.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * AMD Cryptographic Coprocessor (CCP) driver
  3. *
  4. * Copyright (C) 2013 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_DMAPOOL_NAME_LEN 32
  24. #define MAX_HW_QUEUES 5
  25. #define MAX_CMD_QLEN 100
  26. #define TRNG_RETRIES 10
  27. #define CACHE_NONE 0x00
  28. #define CACHE_WB_NO_ALLOC 0xb7
  29. /****** Register Mappings ******/
  30. #define Q_MASK_REG 0x000
  31. #define TRNG_OUT_REG 0x00c
  32. #define IRQ_MASK_REG 0x040
  33. #define IRQ_STATUS_REG 0x200
  34. #define DEL_CMD_Q_JOB 0x124
  35. #define DEL_Q_ACTIVE 0x00000200
  36. #define DEL_Q_ID_SHIFT 6
  37. #define CMD_REQ0 0x180
  38. #define CMD_REQ_INCR 0x04
  39. #define CMD_Q_STATUS_BASE 0x210
  40. #define CMD_Q_INT_STATUS_BASE 0x214
  41. #define CMD_Q_STATUS_INCR 0x20
  42. #define CMD_Q_CACHE_BASE 0x228
  43. #define CMD_Q_CACHE_INC 0x20
  44. #define CMD_Q_ERROR(__qs) ((__qs) & 0x0000003f)
  45. #define CMD_Q_DEPTH(__qs) (((__qs) >> 12) & 0x0000000f)
  46. /****** REQ0 Related Values ******/
  47. #define REQ0_WAIT_FOR_WRITE 0x00000004
  48. #define REQ0_INT_ON_COMPLETE 0x00000002
  49. #define REQ0_STOP_ON_COMPLETE 0x00000001
  50. #define REQ0_CMD_Q_SHIFT 9
  51. #define REQ0_JOBID_SHIFT 3
  52. /****** REQ1 Related Values ******/
  53. #define REQ1_PROTECT_SHIFT 27
  54. #define REQ1_ENGINE_SHIFT 23
  55. #define REQ1_KEY_KSB_SHIFT 2
  56. #define REQ1_EOM 0x00000002
  57. #define REQ1_INIT 0x00000001
  58. /* AES Related Values */
  59. #define REQ1_AES_TYPE_SHIFT 21
  60. #define REQ1_AES_MODE_SHIFT 18
  61. #define REQ1_AES_ACTION_SHIFT 17
  62. #define REQ1_AES_CFB_SIZE_SHIFT 10
  63. /* XTS-AES Related Values */
  64. #define REQ1_XTS_AES_SIZE_SHIFT 10
  65. /* SHA Related Values */
  66. #define REQ1_SHA_TYPE_SHIFT 21
  67. /* RSA Related Values */
  68. #define REQ1_RSA_MOD_SIZE_SHIFT 10
  69. /* Pass-Through Related Values */
  70. #define REQ1_PT_BW_SHIFT 12
  71. #define REQ1_PT_BS_SHIFT 10
  72. /* ECC Related Values */
  73. #define REQ1_ECC_AFFINE_CONVERT 0x00200000
  74. #define REQ1_ECC_FUNCTION_SHIFT 18
  75. /****** REQ4 Related Values ******/
  76. #define REQ4_KSB_SHIFT 18
  77. #define REQ4_MEMTYPE_SHIFT 16
  78. /****** REQ6 Related Values ******/
  79. #define REQ6_MEMTYPE_SHIFT 16
  80. /****** Key Storage Block ******/
  81. #define KSB_START 77
  82. #define KSB_END 127
  83. #define KSB_COUNT (KSB_END - KSB_START + 1)
  84. #define CCP_KSB_BITS 256
  85. #define CCP_KSB_BYTES 32
  86. #define CCP_JOBID_MASK 0x0000003f
  87. #define CCP_DMAPOOL_MAX_SIZE 64
  88. #define CCP_DMAPOOL_ALIGN BIT(5)
  89. #define CCP_REVERSE_BUF_SIZE 64
  90. #define CCP_AES_KEY_KSB_COUNT 1
  91. #define CCP_AES_CTX_KSB_COUNT 1
  92. #define CCP_XTS_AES_KEY_KSB_COUNT 1
  93. #define CCP_XTS_AES_CTX_KSB_COUNT 1
  94. #define CCP_SHA_KSB_COUNT 1
  95. #define CCP_RSA_MAX_WIDTH 4096
  96. #define CCP_PASSTHRU_BLOCKSIZE 256
  97. #define CCP_PASSTHRU_MASKSIZE 32
  98. #define CCP_PASSTHRU_KSB_COUNT 1
  99. #define CCP_ECC_MODULUS_BYTES 48 /* 384-bits */
  100. #define CCP_ECC_MAX_OPERANDS 6
  101. #define CCP_ECC_MAX_OUTPUTS 3
  102. #define CCP_ECC_SRC_BUF_SIZE 448
  103. #define CCP_ECC_DST_BUF_SIZE 192
  104. #define CCP_ECC_OPERAND_SIZE 64
  105. #define CCP_ECC_OUTPUT_SIZE 64
  106. #define CCP_ECC_RESULT_OFFSET 60
  107. #define CCP_ECC_RESULT_SUCCESS 0x0001
  108. struct ccp_device;
  109. struct ccp_cmd;
  110. struct ccp_cmd_queue {
  111. struct ccp_device *ccp;
  112. /* Queue identifier */
  113. u32 id;
  114. /* Queue dma pool */
  115. struct dma_pool *dma_pool;
  116. /* Queue reserved KSB regions */
  117. u32 ksb_key;
  118. u32 ksb_ctx;
  119. /* Queue processing thread */
  120. struct task_struct *kthread;
  121. unsigned int active;
  122. unsigned int suspended;
  123. /* Number of free command slots available */
  124. unsigned int free_slots;
  125. /* Interrupt masks */
  126. u32 int_ok;
  127. u32 int_err;
  128. /* Register addresses for queue */
  129. void __iomem *reg_status;
  130. void __iomem *reg_int_status;
  131. /* Status values from job */
  132. u32 int_status;
  133. u32 q_status;
  134. u32 q_int_status;
  135. u32 cmd_error;
  136. /* Interrupt wait queue */
  137. wait_queue_head_t int_queue;
  138. unsigned int int_rcvd;
  139. } ____cacheline_aligned;
  140. struct ccp_device {
  141. struct device *dev;
  142. /*
  143. * Bus specific device information
  144. */
  145. void *dev_specific;
  146. int (*get_irq)(struct ccp_device *ccp);
  147. void (*free_irq)(struct ccp_device *ccp);
  148. unsigned int irq;
  149. /*
  150. * I/O area used for device communication. The register mapping
  151. * starts at an offset into the mapped bar.
  152. * The CMD_REQx registers and the Delete_Cmd_Queue_Job register
  153. * need to be protected while a command queue thread is accessing
  154. * them.
  155. */
  156. struct mutex req_mutex ____cacheline_aligned;
  157. void __iomem *io_map;
  158. void __iomem *io_regs;
  159. /*
  160. * Master lists that all cmds are queued on. Because there can be
  161. * more than one CCP command queue that can process a cmd a separate
  162. * backlog list is neeeded so that the backlog completion call
  163. * completes before the cmd is available for execution.
  164. */
  165. spinlock_t cmd_lock ____cacheline_aligned;
  166. unsigned int cmd_count;
  167. struct list_head cmd;
  168. struct list_head backlog;
  169. /*
  170. * The command queues. These represent the queues available on the
  171. * CCP that are available for processing cmds
  172. */
  173. struct ccp_cmd_queue cmd_q[MAX_HW_QUEUES];
  174. unsigned int cmd_q_count;
  175. /*
  176. * Support for the CCP True RNG
  177. */
  178. struct hwrng hwrng;
  179. unsigned int hwrng_retries;
  180. /*
  181. * A counter used to generate job-ids for cmds submitted to the CCP
  182. */
  183. atomic_t current_id ____cacheline_aligned;
  184. /*
  185. * The CCP uses key storage blocks (KSB) to maintain context for certain
  186. * operations. To prevent multiple cmds from using the same KSB range
  187. * a command queue reserves a KSB range for the duration of the cmd.
  188. * Each queue, will however, reserve 2 KSB blocks for operations that
  189. * only require single KSB entries (eg. AES context/iv and key) in order
  190. * to avoid allocation contention. This will reserve at most 10 KSB
  191. * entries, leaving 40 KSB entries available for dynamic allocation.
  192. */
  193. struct mutex ksb_mutex ____cacheline_aligned;
  194. DECLARE_BITMAP(ksb, KSB_COUNT);
  195. wait_queue_head_t ksb_queue;
  196. unsigned int ksb_avail;
  197. unsigned int ksb_count;
  198. u32 ksb_start;
  199. /* Suspend support */
  200. unsigned int suspending;
  201. wait_queue_head_t suspend_queue;
  202. /* DMA caching attribute support */
  203. unsigned int axcache;
  204. };
  205. int ccp_pci_init(void);
  206. void ccp_pci_exit(void);
  207. int ccp_platform_init(void);
  208. void ccp_platform_exit(void);
  209. struct ccp_device *ccp_alloc_struct(struct device *dev);
  210. int ccp_init(struct ccp_device *ccp);
  211. void ccp_destroy(struct ccp_device *ccp);
  212. bool ccp_queues_suspended(struct ccp_device *ccp);
  213. irqreturn_t ccp_irq_handler(int irq, void *data);
  214. int ccp_run_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd);
  215. #endif