blob: 5fbee638c9baadcbf7c143339f5ce7c395c1c251 [file] [log] [blame]
Tom Lendacky63b94502013-11-12 11:46:16 -06001/*
2 * AMD Cryptographic Coprocessor (CCP) driver
3 *
Gary R Hook553d2372016-03-01 13:49:04 -06004 * Copyright (C) 2013,2016 Advanced Micro Devices, Inc.
Tom Lendacky63b94502013-11-12 11:46:16 -06005 *
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
13#ifndef __CCP_DEV_H__
14#define __CCP_DEV_H__
15
16#include <linux/device.h>
17#include <linux/pci.h>
18#include <linux/spinlock.h>
19#include <linux/mutex.h>
20#include <linux/list.h>
21#include <linux/wait.h>
22#include <linux/dmapool.h>
23#include <linux/hw_random.h>
Tom Lendacky8db88462015-02-03 13:07:05 -060024#include <linux/bitops.h>
Gary R Hook58ea8ab2016-04-18 09:21:44 -050025#include <linux/interrupt.h>
26#include <linux/irqreturn.h>
27#include <linux/dmaengine.h>
Tom Lendacky63b94502013-11-12 11:46:16 -060028
Gary R Hook553d2372016-03-01 13:49:04 -060029#define MAX_CCP_NAME_LEN 16
Tom Lendacky63b94502013-11-12 11:46:16 -060030#define MAX_DMAPOOL_NAME_LEN 32
31
32#define MAX_HW_QUEUES 5
33#define MAX_CMD_QLEN 100
34
35#define TRNG_RETRIES 10
36
Tom Lendacky126ae9a2014-07-10 10:58:35 -050037#define CACHE_NONE 0x00
Tom Lendackyc4f4b322014-06-05 10:17:57 -050038#define CACHE_WB_NO_ALLOC 0xb7
39
Tom Lendacky63b94502013-11-12 11:46:16 -060040/****** Register Mappings ******/
41#define Q_MASK_REG 0x000
42#define TRNG_OUT_REG 0x00c
43#define IRQ_MASK_REG 0x040
44#define IRQ_STATUS_REG 0x200
45
46#define DEL_CMD_Q_JOB 0x124
47#define DEL_Q_ACTIVE 0x00000200
48#define DEL_Q_ID_SHIFT 6
49
50#define CMD_REQ0 0x180
51#define CMD_REQ_INCR 0x04
52
53#define CMD_Q_STATUS_BASE 0x210
54#define CMD_Q_INT_STATUS_BASE 0x214
55#define CMD_Q_STATUS_INCR 0x20
56
Tom Lendackyc4f4b322014-06-05 10:17:57 -050057#define CMD_Q_CACHE_BASE 0x228
Tom Lendacky63b94502013-11-12 11:46:16 -060058#define CMD_Q_CACHE_INC 0x20
59
Tom Lendacky8db88462015-02-03 13:07:05 -060060#define CMD_Q_ERROR(__qs) ((__qs) & 0x0000003f)
61#define CMD_Q_DEPTH(__qs) (((__qs) >> 12) & 0x0000000f)
Tom Lendacky63b94502013-11-12 11:46:16 -060062
63/****** REQ0 Related Values ******/
64#define REQ0_WAIT_FOR_WRITE 0x00000004
65#define REQ0_INT_ON_COMPLETE 0x00000002
66#define REQ0_STOP_ON_COMPLETE 0x00000001
67
68#define REQ0_CMD_Q_SHIFT 9
69#define REQ0_JOBID_SHIFT 3
70
71/****** REQ1 Related Values ******/
72#define REQ1_PROTECT_SHIFT 27
73#define REQ1_ENGINE_SHIFT 23
74#define REQ1_KEY_KSB_SHIFT 2
75
76#define REQ1_EOM 0x00000002
77#define REQ1_INIT 0x00000001
78
79/* AES Related Values */
80#define REQ1_AES_TYPE_SHIFT 21
81#define REQ1_AES_MODE_SHIFT 18
82#define REQ1_AES_ACTION_SHIFT 17
83#define REQ1_AES_CFB_SIZE_SHIFT 10
84
85/* XTS-AES Related Values */
86#define REQ1_XTS_AES_SIZE_SHIFT 10
87
88/* SHA Related Values */
89#define REQ1_SHA_TYPE_SHIFT 21
90
91/* RSA Related Values */
92#define REQ1_RSA_MOD_SIZE_SHIFT 10
93
94/* Pass-Through Related Values */
95#define REQ1_PT_BW_SHIFT 12
96#define REQ1_PT_BS_SHIFT 10
97
98/* ECC Related Values */
99#define REQ1_ECC_AFFINE_CONVERT 0x00200000
100#define REQ1_ECC_FUNCTION_SHIFT 18
101
102/****** REQ4 Related Values ******/
103#define REQ4_KSB_SHIFT 18
104#define REQ4_MEMTYPE_SHIFT 16
105
106/****** REQ6 Related Values ******/
107#define REQ6_MEMTYPE_SHIFT 16
108
Tom Lendacky63b94502013-11-12 11:46:16 -0600109/****** Key Storage Block ******/
110#define KSB_START 77
111#define KSB_END 127
112#define KSB_COUNT (KSB_END - KSB_START + 1)
113#define CCP_KSB_BITS 256
114#define CCP_KSB_BYTES 32
115
116#define CCP_JOBID_MASK 0x0000003f
117
118#define CCP_DMAPOOL_MAX_SIZE 64
Tom Lendacky8db88462015-02-03 13:07:05 -0600119#define CCP_DMAPOOL_ALIGN BIT(5)
Tom Lendacky63b94502013-11-12 11:46:16 -0600120
121#define CCP_REVERSE_BUF_SIZE 64
122
123#define CCP_AES_KEY_KSB_COUNT 1
124#define CCP_AES_CTX_KSB_COUNT 1
125
126#define CCP_XTS_AES_KEY_KSB_COUNT 1
127#define CCP_XTS_AES_CTX_KSB_COUNT 1
128
129#define CCP_SHA_KSB_COUNT 1
130
131#define CCP_RSA_MAX_WIDTH 4096
132
133#define CCP_PASSTHRU_BLOCKSIZE 256
134#define CCP_PASSTHRU_MASKSIZE 32
135#define CCP_PASSTHRU_KSB_COUNT 1
136
137#define CCP_ECC_MODULUS_BYTES 48 /* 384-bits */
138#define CCP_ECC_MAX_OPERANDS 6
139#define CCP_ECC_MAX_OUTPUTS 3
140#define CCP_ECC_SRC_BUF_SIZE 448
141#define CCP_ECC_DST_BUF_SIZE 192
142#define CCP_ECC_OPERAND_SIZE 64
143#define CCP_ECC_OUTPUT_SIZE 64
144#define CCP_ECC_RESULT_OFFSET 60
145#define CCP_ECC_RESULT_SUCCESS 0x0001
146
Gary R Hookea0375a2016-03-01 13:49:25 -0600147struct ccp_op;
148
149/* Structure for computation functions that are device-specific */
150struct ccp_actions {
151 int (*perform_aes)(struct ccp_op *);
152 int (*perform_xts_aes)(struct ccp_op *);
153 int (*perform_sha)(struct ccp_op *);
154 int (*perform_rsa)(struct ccp_op *);
155 int (*perform_passthru)(struct ccp_op *);
156 int (*perform_ecc)(struct ccp_op *);
157 int (*init)(struct ccp_device *);
158 void (*destroy)(struct ccp_device *);
159 irqreturn_t (*irqhandler)(int, void *);
160};
161
Gary R Hookc7019c42016-03-01 13:49:15 -0600162/* Structure to hold CCP version-specific values */
163struct ccp_vdata {
164 unsigned int version;
Julia Lawallbc197b2a2016-05-01 13:52:55 +0200165 const struct ccp_actions *perform;
Gary R Hookc7019c42016-03-01 13:49:15 -0600166};
167
168extern struct ccp_vdata ccpv3;
169
Tom Lendacky63b94502013-11-12 11:46:16 -0600170struct ccp_device;
171struct ccp_cmd;
172
Gary R Hook58ea8ab2016-04-18 09:21:44 -0500173struct ccp_dma_cmd {
174 struct list_head entry;
175
176 struct ccp_cmd ccp_cmd;
177};
178
179struct ccp_dma_desc {
180 struct list_head entry;
181
182 struct ccp_device *ccp;
183
184 struct list_head pending;
185 struct list_head active;
186
187 enum dma_status status;
188 struct dma_async_tx_descriptor tx_desc;
189 size_t len;
190};
191
192struct ccp_dma_chan {
193 struct ccp_device *ccp;
194
195 spinlock_t lock;
196 struct list_head pending;
197 struct list_head active;
198 struct list_head complete;
199
200 struct tasklet_struct cleanup_tasklet;
201
202 enum dma_status status;
203 struct dma_chan dma_chan;
204};
205
Tom Lendacky63b94502013-11-12 11:46:16 -0600206struct ccp_cmd_queue {
207 struct ccp_device *ccp;
208
209 /* Queue identifier */
210 u32 id;
211
212 /* Queue dma pool */
213 struct dma_pool *dma_pool;
214
215 /* Queue reserved KSB regions */
216 u32 ksb_key;
217 u32 ksb_ctx;
218
219 /* Queue processing thread */
220 struct task_struct *kthread;
221 unsigned int active;
222 unsigned int suspended;
223
224 /* Number of free command slots available */
225 unsigned int free_slots;
226
227 /* Interrupt masks */
228 u32 int_ok;
229 u32 int_err;
230
231 /* Register addresses for queue */
232 void __iomem *reg_status;
233 void __iomem *reg_int_status;
234
235 /* Status values from job */
236 u32 int_status;
237 u32 q_status;
238 u32 q_int_status;
239 u32 cmd_error;
240
241 /* Interrupt wait queue */
242 wait_queue_head_t int_queue;
243 unsigned int int_rcvd;
244} ____cacheline_aligned;
245
246struct ccp_device {
Gary R Hook553d2372016-03-01 13:49:04 -0600247 struct list_head entry;
248
Gary R Hookc7019c42016-03-01 13:49:15 -0600249 struct ccp_vdata *vdata;
Gary R Hook553d2372016-03-01 13:49:04 -0600250 unsigned int ord;
251 char name[MAX_CCP_NAME_LEN];
252 char rngname[MAX_CCP_NAME_LEN];
253
Tom Lendacky63b94502013-11-12 11:46:16 -0600254 struct device *dev;
255
Gary R Hookfa242e82016-07-26 18:09:46 -0500256 /* Bus specific device information
Tom Lendacky63b94502013-11-12 11:46:16 -0600257 */
258 void *dev_specific;
259 int (*get_irq)(struct ccp_device *ccp);
260 void (*free_irq)(struct ccp_device *ccp);
Tom Lendacky3d775652014-06-05 10:17:45 -0500261 unsigned int irq;
Tom Lendacky63b94502013-11-12 11:46:16 -0600262
Gary R Hookfa242e82016-07-26 18:09:46 -0500263 /* I/O area used for device communication. The register mapping
Tom Lendacky63b94502013-11-12 11:46:16 -0600264 * starts at an offset into the mapped bar.
265 * The CMD_REQx registers and the Delete_Cmd_Queue_Job register
266 * need to be protected while a command queue thread is accessing
267 * them.
268 */
269 struct mutex req_mutex ____cacheline_aligned;
270 void __iomem *io_map;
271 void __iomem *io_regs;
272
Gary R Hookfa242e82016-07-26 18:09:46 -0500273 /* Master lists that all cmds are queued on. Because there can be
Tom Lendacky63b94502013-11-12 11:46:16 -0600274 * more than one CCP command queue that can process a cmd a separate
275 * backlog list is neeeded so that the backlog completion call
276 * completes before the cmd is available for execution.
277 */
278 spinlock_t cmd_lock ____cacheline_aligned;
279 unsigned int cmd_count;
280 struct list_head cmd;
281 struct list_head backlog;
282
Gary R Hookfa242e82016-07-26 18:09:46 -0500283 /* The command queues. These represent the queues available on the
Tom Lendacky63b94502013-11-12 11:46:16 -0600284 * CCP that are available for processing cmds
285 */
286 struct ccp_cmd_queue cmd_q[MAX_HW_QUEUES];
287 unsigned int cmd_q_count;
288
Gary R Hookfa242e82016-07-26 18:09:46 -0500289 /* Support for the CCP True RNG
Tom Lendacky63b94502013-11-12 11:46:16 -0600290 */
291 struct hwrng hwrng;
292 unsigned int hwrng_retries;
293
Gary R Hookfa242e82016-07-26 18:09:46 -0500294 /* Support for the CCP DMA capabilities
Gary R Hook58ea8ab2016-04-18 09:21:44 -0500295 */
296 struct dma_device dma_dev;
297 struct ccp_dma_chan *ccp_dma_chan;
298 struct kmem_cache *dma_cmd_cache;
299 struct kmem_cache *dma_desc_cache;
300
Gary R Hookfa242e82016-07-26 18:09:46 -0500301 /* A counter used to generate job-ids for cmds submitted to the CCP
Tom Lendacky63b94502013-11-12 11:46:16 -0600302 */
303 atomic_t current_id ____cacheline_aligned;
304
Gary R Hookfa242e82016-07-26 18:09:46 -0500305 /* The CCP uses key storage blocks (KSB) to maintain context for certain
Tom Lendacky63b94502013-11-12 11:46:16 -0600306 * operations. To prevent multiple cmds from using the same KSB range
307 * a command queue reserves a KSB range for the duration of the cmd.
308 * Each queue, will however, reserve 2 KSB blocks for operations that
309 * only require single KSB entries (eg. AES context/iv and key) in order
310 * to avoid allocation contention. This will reserve at most 10 KSB
311 * entries, leaving 40 KSB entries available for dynamic allocation.
312 */
313 struct mutex ksb_mutex ____cacheline_aligned;
314 DECLARE_BITMAP(ksb, KSB_COUNT);
315 wait_queue_head_t ksb_queue;
316 unsigned int ksb_avail;
317 unsigned int ksb_count;
318 u32 ksb_start;
319
320 /* Suspend support */
321 unsigned int suspending;
322 wait_queue_head_t suspend_queue;
Tom Lendacky126ae9a2014-07-10 10:58:35 -0500323
324 /* DMA caching attribute support */
325 unsigned int axcache;
Tom Lendacky63b94502013-11-12 11:46:16 -0600326};
327
Gary R Hookea0375a2016-03-01 13:49:25 -0600328enum ccp_memtype {
329 CCP_MEMTYPE_SYSTEM = 0,
330 CCP_MEMTYPE_KSB,
331 CCP_MEMTYPE_LOCAL,
332 CCP_MEMTYPE__LAST,
333};
334
335struct ccp_dma_info {
336 dma_addr_t address;
337 unsigned int offset;
338 unsigned int length;
339 enum dma_data_direction dir;
340};
341
342struct ccp_dm_workarea {
343 struct device *dev;
344 struct dma_pool *dma_pool;
345 unsigned int length;
346
347 u8 *address;
348 struct ccp_dma_info dma;
349};
350
351struct ccp_sg_workarea {
352 struct scatterlist *sg;
353 int nents;
354
355 struct scatterlist *dma_sg;
356 struct device *dma_dev;
357 unsigned int dma_count;
358 enum dma_data_direction dma_dir;
359
360 unsigned int sg_used;
361
362 u64 bytes_left;
363};
364
365struct ccp_data {
366 struct ccp_sg_workarea sg_wa;
367 struct ccp_dm_workarea dm_wa;
368};
369
370struct ccp_mem {
371 enum ccp_memtype type;
372 union {
373 struct ccp_dma_info dma;
374 u32 ksb;
375 } u;
376};
377
378struct ccp_aes_op {
379 enum ccp_aes_type type;
380 enum ccp_aes_mode mode;
381 enum ccp_aes_action action;
382};
383
384struct ccp_xts_aes_op {
385 enum ccp_aes_action action;
386 enum ccp_xts_aes_unit_size unit_size;
387};
388
389struct ccp_sha_op {
390 enum ccp_sha_type type;
391 u64 msg_bits;
392};
393
394struct ccp_rsa_op {
395 u32 mod_size;
396 u32 input_len;
397};
398
399struct ccp_passthru_op {
400 enum ccp_passthru_bitwise bit_mod;
401 enum ccp_passthru_byteswap byte_swap;
402};
403
404struct ccp_ecc_op {
405 enum ccp_ecc_function function;
406};
407
408struct ccp_op {
409 struct ccp_cmd_queue *cmd_q;
410
411 u32 jobid;
412 u32 ioc;
413 u32 soc;
414 u32 ksb_key;
415 u32 ksb_ctx;
416 u32 init;
417 u32 eom;
418
419 struct ccp_mem src;
420 struct ccp_mem dst;
421
422 union {
423 struct ccp_aes_op aes;
424 struct ccp_xts_aes_op xts;
425 struct ccp_sha_op sha;
426 struct ccp_rsa_op rsa;
427 struct ccp_passthru_op passthru;
428 struct ccp_ecc_op ecc;
429 } u;
430};
431
432static inline u32 ccp_addr_lo(struct ccp_dma_info *info)
433{
434 return lower_32_bits(info->address + info->offset);
435}
436
437static inline u32 ccp_addr_hi(struct ccp_dma_info *info)
438{
439 return upper_32_bits(info->address + info->offset) & 0x0000ffff;
440}
441
Tom Lendacky63b94502013-11-12 11:46:16 -0600442int ccp_pci_init(void);
443void ccp_pci_exit(void);
444
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500445int ccp_platform_init(void);
446void ccp_platform_exit(void);
447
Gary R Hookea0375a2016-03-01 13:49:25 -0600448void ccp_add_device(struct ccp_device *ccp);
449void ccp_del_device(struct ccp_device *ccp);
Tom Lendacky63b94502013-11-12 11:46:16 -0600450
Gary R Hookea0375a2016-03-01 13:49:25 -0600451struct ccp_device *ccp_alloc_struct(struct device *dev);
452bool ccp_queues_suspended(struct ccp_device *ccp);
453int ccp_cmd_queue_thread(void *data);
Tom Lendacky63b94502013-11-12 11:46:16 -0600454
455int ccp_run_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd);
456
Gary R Hook58ea8ab2016-04-18 09:21:44 -0500457int ccp_dmaengine_register(struct ccp_device *ccp);
458void ccp_dmaengine_unregister(struct ccp_device *ccp);
459
Tom Lendacky63b94502013-11-12 11:46:16 -0600460#endif