blob: 90a8cc8c7d46d0591db562f691ae9fecb231b5e4 [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>
Tom Lendacky63b94502013-11-12 11:46:16 -060025
Gary R Hook553d2372016-03-01 13:49:04 -060026#define MAX_CCP_NAME_LEN 16
Tom Lendacky63b94502013-11-12 11:46:16 -060027#define MAX_DMAPOOL_NAME_LEN 32
28
29#define MAX_HW_QUEUES 5
30#define MAX_CMD_QLEN 100
31
32#define TRNG_RETRIES 10
33
Tom Lendacky126ae9a2014-07-10 10:58:35 -050034#define CACHE_NONE 0x00
Tom Lendackyc4f4b322014-06-05 10:17:57 -050035#define CACHE_WB_NO_ALLOC 0xb7
36
Tom Lendacky63b94502013-11-12 11:46:16 -060037/****** Register Mappings ******/
38#define Q_MASK_REG 0x000
39#define TRNG_OUT_REG 0x00c
40#define IRQ_MASK_REG 0x040
41#define IRQ_STATUS_REG 0x200
42
43#define DEL_CMD_Q_JOB 0x124
44#define DEL_Q_ACTIVE 0x00000200
45#define DEL_Q_ID_SHIFT 6
46
47#define CMD_REQ0 0x180
48#define CMD_REQ_INCR 0x04
49
50#define CMD_Q_STATUS_BASE 0x210
51#define CMD_Q_INT_STATUS_BASE 0x214
52#define CMD_Q_STATUS_INCR 0x20
53
Tom Lendackyc4f4b322014-06-05 10:17:57 -050054#define CMD_Q_CACHE_BASE 0x228
Tom Lendacky63b94502013-11-12 11:46:16 -060055#define CMD_Q_CACHE_INC 0x20
56
Tom Lendacky8db88462015-02-03 13:07:05 -060057#define CMD_Q_ERROR(__qs) ((__qs) & 0x0000003f)
58#define CMD_Q_DEPTH(__qs) (((__qs) >> 12) & 0x0000000f)
Tom Lendacky63b94502013-11-12 11:46:16 -060059
60/****** REQ0 Related Values ******/
61#define REQ0_WAIT_FOR_WRITE 0x00000004
62#define REQ0_INT_ON_COMPLETE 0x00000002
63#define REQ0_STOP_ON_COMPLETE 0x00000001
64
65#define REQ0_CMD_Q_SHIFT 9
66#define REQ0_JOBID_SHIFT 3
67
68/****** REQ1 Related Values ******/
69#define REQ1_PROTECT_SHIFT 27
70#define REQ1_ENGINE_SHIFT 23
71#define REQ1_KEY_KSB_SHIFT 2
72
73#define REQ1_EOM 0x00000002
74#define REQ1_INIT 0x00000001
75
76/* AES Related Values */
77#define REQ1_AES_TYPE_SHIFT 21
78#define REQ1_AES_MODE_SHIFT 18
79#define REQ1_AES_ACTION_SHIFT 17
80#define REQ1_AES_CFB_SIZE_SHIFT 10
81
82/* XTS-AES Related Values */
83#define REQ1_XTS_AES_SIZE_SHIFT 10
84
85/* SHA Related Values */
86#define REQ1_SHA_TYPE_SHIFT 21
87
88/* RSA Related Values */
89#define REQ1_RSA_MOD_SIZE_SHIFT 10
90
91/* Pass-Through Related Values */
92#define REQ1_PT_BW_SHIFT 12
93#define REQ1_PT_BS_SHIFT 10
94
95/* ECC Related Values */
96#define REQ1_ECC_AFFINE_CONVERT 0x00200000
97#define REQ1_ECC_FUNCTION_SHIFT 18
98
99/****** REQ4 Related Values ******/
100#define REQ4_KSB_SHIFT 18
101#define REQ4_MEMTYPE_SHIFT 16
102
103/****** REQ6 Related Values ******/
104#define REQ6_MEMTYPE_SHIFT 16
105
Tom Lendacky63b94502013-11-12 11:46:16 -0600106/****** Key Storage Block ******/
107#define KSB_START 77
108#define KSB_END 127
109#define KSB_COUNT (KSB_END - KSB_START + 1)
110#define CCP_KSB_BITS 256
111#define CCP_KSB_BYTES 32
112
113#define CCP_JOBID_MASK 0x0000003f
114
115#define CCP_DMAPOOL_MAX_SIZE 64
Tom Lendacky8db88462015-02-03 13:07:05 -0600116#define CCP_DMAPOOL_ALIGN BIT(5)
Tom Lendacky63b94502013-11-12 11:46:16 -0600117
118#define CCP_REVERSE_BUF_SIZE 64
119
120#define CCP_AES_KEY_KSB_COUNT 1
121#define CCP_AES_CTX_KSB_COUNT 1
122
123#define CCP_XTS_AES_KEY_KSB_COUNT 1
124#define CCP_XTS_AES_CTX_KSB_COUNT 1
125
126#define CCP_SHA_KSB_COUNT 1
127
128#define CCP_RSA_MAX_WIDTH 4096
129
130#define CCP_PASSTHRU_BLOCKSIZE 256
131#define CCP_PASSTHRU_MASKSIZE 32
132#define CCP_PASSTHRU_KSB_COUNT 1
133
134#define CCP_ECC_MODULUS_BYTES 48 /* 384-bits */
135#define CCP_ECC_MAX_OPERANDS 6
136#define CCP_ECC_MAX_OUTPUTS 3
137#define CCP_ECC_SRC_BUF_SIZE 448
138#define CCP_ECC_DST_BUF_SIZE 192
139#define CCP_ECC_OPERAND_SIZE 64
140#define CCP_ECC_OUTPUT_SIZE 64
141#define CCP_ECC_RESULT_OFFSET 60
142#define CCP_ECC_RESULT_SUCCESS 0x0001
143
Gary R Hookc7019c42016-03-01 13:49:15 -0600144/* Structure to hold CCP version-specific values */
145struct ccp_vdata {
146 unsigned int version;
147};
148
149extern struct ccp_vdata ccpv3;
150
Tom Lendacky63b94502013-11-12 11:46:16 -0600151struct ccp_device;
152struct ccp_cmd;
153
154struct ccp_cmd_queue {
155 struct ccp_device *ccp;
156
157 /* Queue identifier */
158 u32 id;
159
160 /* Queue dma pool */
161 struct dma_pool *dma_pool;
162
163 /* Queue reserved KSB regions */
164 u32 ksb_key;
165 u32 ksb_ctx;
166
167 /* Queue processing thread */
168 struct task_struct *kthread;
169 unsigned int active;
170 unsigned int suspended;
171
172 /* Number of free command slots available */
173 unsigned int free_slots;
174
175 /* Interrupt masks */
176 u32 int_ok;
177 u32 int_err;
178
179 /* Register addresses for queue */
180 void __iomem *reg_status;
181 void __iomem *reg_int_status;
182
183 /* Status values from job */
184 u32 int_status;
185 u32 q_status;
186 u32 q_int_status;
187 u32 cmd_error;
188
189 /* Interrupt wait queue */
190 wait_queue_head_t int_queue;
191 unsigned int int_rcvd;
192} ____cacheline_aligned;
193
194struct ccp_device {
Gary R Hook553d2372016-03-01 13:49:04 -0600195 struct list_head entry;
196
Gary R Hookc7019c42016-03-01 13:49:15 -0600197 struct ccp_vdata *vdata;
Gary R Hook553d2372016-03-01 13:49:04 -0600198 unsigned int ord;
199 char name[MAX_CCP_NAME_LEN];
200 char rngname[MAX_CCP_NAME_LEN];
201
Tom Lendacky63b94502013-11-12 11:46:16 -0600202 struct device *dev;
203
204 /*
205 * Bus specific device information
206 */
207 void *dev_specific;
208 int (*get_irq)(struct ccp_device *ccp);
209 void (*free_irq)(struct ccp_device *ccp);
Tom Lendacky3d775652014-06-05 10:17:45 -0500210 unsigned int irq;
Tom Lendacky63b94502013-11-12 11:46:16 -0600211
212 /*
213 * I/O area used for device communication. The register mapping
214 * starts at an offset into the mapped bar.
215 * The CMD_REQx registers and the Delete_Cmd_Queue_Job register
216 * need to be protected while a command queue thread is accessing
217 * them.
218 */
219 struct mutex req_mutex ____cacheline_aligned;
220 void __iomem *io_map;
221 void __iomem *io_regs;
222
223 /*
224 * Master lists that all cmds are queued on. Because there can be
225 * more than one CCP command queue that can process a cmd a separate
226 * backlog list is neeeded so that the backlog completion call
227 * completes before the cmd is available for execution.
228 */
229 spinlock_t cmd_lock ____cacheline_aligned;
230 unsigned int cmd_count;
231 struct list_head cmd;
232 struct list_head backlog;
233
234 /*
235 * The command queues. These represent the queues available on the
236 * CCP that are available for processing cmds
237 */
238 struct ccp_cmd_queue cmd_q[MAX_HW_QUEUES];
239 unsigned int cmd_q_count;
240
241 /*
242 * Support for the CCP True RNG
243 */
244 struct hwrng hwrng;
245 unsigned int hwrng_retries;
246
247 /*
248 * A counter used to generate job-ids for cmds submitted to the CCP
249 */
250 atomic_t current_id ____cacheline_aligned;
251
252 /*
253 * The CCP uses key storage blocks (KSB) to maintain context for certain
254 * operations. To prevent multiple cmds from using the same KSB range
255 * a command queue reserves a KSB range for the duration of the cmd.
256 * Each queue, will however, reserve 2 KSB blocks for operations that
257 * only require single KSB entries (eg. AES context/iv and key) in order
258 * to avoid allocation contention. This will reserve at most 10 KSB
259 * entries, leaving 40 KSB entries available for dynamic allocation.
260 */
261 struct mutex ksb_mutex ____cacheline_aligned;
262 DECLARE_BITMAP(ksb, KSB_COUNT);
263 wait_queue_head_t ksb_queue;
264 unsigned int ksb_avail;
265 unsigned int ksb_count;
266 u32 ksb_start;
267
268 /* Suspend support */
269 unsigned int suspending;
270 wait_queue_head_t suspend_queue;
Tom Lendacky126ae9a2014-07-10 10:58:35 -0500271
272 /* DMA caching attribute support */
273 unsigned int axcache;
Tom Lendacky63b94502013-11-12 11:46:16 -0600274};
275
Tom Lendacky63b94502013-11-12 11:46:16 -0600276int ccp_pci_init(void);
277void ccp_pci_exit(void);
278
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500279int ccp_platform_init(void);
280void ccp_platform_exit(void);
281
Tom Lendacky63b94502013-11-12 11:46:16 -0600282struct ccp_device *ccp_alloc_struct(struct device *dev);
283int ccp_init(struct ccp_device *ccp);
284void ccp_destroy(struct ccp_device *ccp);
285bool ccp_queues_suspended(struct ccp_device *ccp);
286
287irqreturn_t ccp_irq_handler(int irq, void *data);
288
289int ccp_run_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd);
290
291#endif