xref: /openbmc/linux/drivers/crypto/ccp/ccp-dev.h (revision 4b394a232df78414442778b02ca4a388d947d059)
163b94509STom Lendacky /*
263b94509STom Lendacky  * AMD Cryptographic Coprocessor (CCP) driver
363b94509STom Lendacky  *
4553d2374SGary R Hook  * Copyright (C) 2013,2016 Advanced Micro Devices, Inc.
563b94509STom Lendacky  *
663b94509STom Lendacky  * Author: Tom Lendacky <thomas.lendacky@amd.com>
7fba8855cSGary R Hook  * Author: Gary R Hook <gary.hook@amd.com>
863b94509STom Lendacky  *
963b94509STom Lendacky  * This program is free software; you can redistribute it and/or modify
1063b94509STom Lendacky  * it under the terms of the GNU General Public License version 2 as
1163b94509STom Lendacky  * published by the Free Software Foundation.
1263b94509STom Lendacky  */
1363b94509STom Lendacky 
1463b94509STom Lendacky #ifndef __CCP_DEV_H__
1563b94509STom Lendacky #define __CCP_DEV_H__
1663b94509STom Lendacky 
1763b94509STom Lendacky #include <linux/device.h>
1863b94509STom Lendacky #include <linux/pci.h>
1963b94509STom Lendacky #include <linux/spinlock.h>
2063b94509STom Lendacky #include <linux/mutex.h>
2163b94509STom Lendacky #include <linux/list.h>
2263b94509STom Lendacky #include <linux/wait.h>
2363b94509STom Lendacky #include <linux/dmapool.h>
2463b94509STom Lendacky #include <linux/hw_random.h>
258db88467STom Lendacky #include <linux/bitops.h>
2658ea8abfSGary R Hook #include <linux/interrupt.h>
2758ea8abfSGary R Hook #include <linux/irqreturn.h>
2858ea8abfSGary R Hook #include <linux/dmaengine.h>
2963b94509STom Lendacky 
30553d2374SGary R Hook #define MAX_CCP_NAME_LEN		16
3163b94509STom Lendacky #define MAX_DMAPOOL_NAME_LEN		32
3263b94509STom Lendacky 
3363b94509STom Lendacky #define MAX_HW_QUEUES			5
3463b94509STom Lendacky #define MAX_CMD_QLEN			100
3563b94509STom Lendacky 
3663b94509STom Lendacky #define TRNG_RETRIES			10
3763b94509STom Lendacky 
38126ae9adSTom Lendacky #define CACHE_NONE			0x00
39c4f4b325STom Lendacky #define CACHE_WB_NO_ALLOC		0xb7
40c4f4b325STom Lendacky 
4163b94509STom Lendacky /****** Register Mappings ******/
4263b94509STom Lendacky #define Q_MASK_REG			0x000
4363b94509STom Lendacky #define TRNG_OUT_REG			0x00c
4463b94509STom Lendacky #define IRQ_MASK_REG			0x040
4563b94509STom Lendacky #define IRQ_STATUS_REG			0x200
4663b94509STom Lendacky 
4763b94509STom Lendacky #define DEL_CMD_Q_JOB			0x124
4863b94509STom Lendacky #define DEL_Q_ACTIVE			0x00000200
4963b94509STom Lendacky #define DEL_Q_ID_SHIFT			6
5063b94509STom Lendacky 
5163b94509STom Lendacky #define CMD_REQ0			0x180
5263b94509STom Lendacky #define CMD_REQ_INCR			0x04
5363b94509STom Lendacky 
5463b94509STom Lendacky #define CMD_Q_STATUS_BASE		0x210
5563b94509STom Lendacky #define CMD_Q_INT_STATUS_BASE		0x214
5663b94509STom Lendacky #define CMD_Q_STATUS_INCR		0x20
5763b94509STom Lendacky 
58c4f4b325STom Lendacky #define CMD_Q_CACHE_BASE		0x228
5963b94509STom Lendacky #define CMD_Q_CACHE_INC			0x20
6063b94509STom Lendacky 
618db88467STom Lendacky #define CMD_Q_ERROR(__qs)		((__qs) & 0x0000003f)
628db88467STom Lendacky #define CMD_Q_DEPTH(__qs)		(((__qs) >> 12) & 0x0000000f)
6363b94509STom Lendacky 
64*4b394a23SGary R Hook /* ------------------------ CCP Version 5 Specifics ------------------------ */
65*4b394a23SGary R Hook #define CMD5_QUEUE_MASK_OFFSET		0x00
66*4b394a23SGary R Hook #define CMD5_REQID_CONFIG_OFFSET	0x08
67*4b394a23SGary R Hook #define LSB_PUBLIC_MASK_LO_OFFSET	0x18
68*4b394a23SGary R Hook #define LSB_PUBLIC_MASK_HI_OFFSET	0x1C
69*4b394a23SGary R Hook #define LSB_PRIVATE_MASK_LO_OFFSET	0x20
70*4b394a23SGary R Hook #define LSB_PRIVATE_MASK_HI_OFFSET	0x24
71*4b394a23SGary R Hook 
72*4b394a23SGary R Hook #define CMD5_Q_CONTROL_BASE		0x0000
73*4b394a23SGary R Hook #define CMD5_Q_TAIL_LO_BASE		0x0004
74*4b394a23SGary R Hook #define CMD5_Q_HEAD_LO_BASE		0x0008
75*4b394a23SGary R Hook #define CMD5_Q_INT_ENABLE_BASE		0x000C
76*4b394a23SGary R Hook #define CMD5_Q_INTERRUPT_STATUS_BASE	0x0010
77*4b394a23SGary R Hook 
78*4b394a23SGary R Hook #define CMD5_Q_STATUS_BASE		0x0100
79*4b394a23SGary R Hook #define CMD5_Q_INT_STATUS_BASE		0x0104
80*4b394a23SGary R Hook #define CMD5_Q_DMA_STATUS_BASE		0x0108
81*4b394a23SGary R Hook #define CMD5_Q_DMA_READ_STATUS_BASE	0x010C
82*4b394a23SGary R Hook #define CMD5_Q_DMA_WRITE_STATUS_BASE	0x0110
83*4b394a23SGary R Hook #define CMD5_Q_ABORT_BASE		0x0114
84*4b394a23SGary R Hook #define CMD5_Q_AX_CACHE_BASE		0x0118
85*4b394a23SGary R Hook 
86*4b394a23SGary R Hook /* Address offset between two virtual queue registers */
87*4b394a23SGary R Hook #define CMD5_Q_STATUS_INCR		0x1000
88*4b394a23SGary R Hook 
89*4b394a23SGary R Hook /* Bit masks */
90*4b394a23SGary R Hook #define CMD5_Q_RUN			0x1
91*4b394a23SGary R Hook #define CMD5_Q_HALT			0x2
92*4b394a23SGary R Hook #define CMD5_Q_MEM_LOCATION		0x4
93*4b394a23SGary R Hook #define CMD5_Q_SIZE			0x1F
94*4b394a23SGary R Hook #define CMD5_Q_SHIFT			3
95*4b394a23SGary R Hook #define COMMANDS_PER_QUEUE		16
96*4b394a23SGary R Hook #define QUEUE_SIZE_VAL			((ffs(COMMANDS_PER_QUEUE) - 2) & \
97*4b394a23SGary R Hook 					  CMD5_Q_SIZE)
98*4b394a23SGary R Hook #define Q_PTR_MASK			(2 << (QUEUE_SIZE_VAL + 5) - 1)
99*4b394a23SGary R Hook #define Q_DESC_SIZE			sizeof(struct ccp5_desc)
100*4b394a23SGary R Hook #define Q_SIZE(n)			(COMMANDS_PER_QUEUE*(n))
101*4b394a23SGary R Hook 
102*4b394a23SGary R Hook #define INT_COMPLETION			0x1
103*4b394a23SGary R Hook #define INT_ERROR			0x2
104*4b394a23SGary R Hook #define INT_QUEUE_STOPPED		0x4
105*4b394a23SGary R Hook #define ALL_INTERRUPTS			(INT_COMPLETION| \
106*4b394a23SGary R Hook 					 INT_ERROR| \
107*4b394a23SGary R Hook 					 INT_QUEUE_STOPPED)
108*4b394a23SGary R Hook 
109*4b394a23SGary R Hook #define LSB_REGION_WIDTH		5
110*4b394a23SGary R Hook #define MAX_LSB_CNT			8
111*4b394a23SGary R Hook 
112*4b394a23SGary R Hook #define LSB_SIZE			16
113*4b394a23SGary R Hook #define LSB_ITEM_SIZE			32
114*4b394a23SGary R Hook #define PLSB_MAP_SIZE			(LSB_SIZE)
115*4b394a23SGary R Hook #define SLSB_MAP_SIZE			(MAX_LSB_CNT * LSB_SIZE)
116*4b394a23SGary R Hook 
117*4b394a23SGary R Hook #define LSB_ENTRY_NUMBER(LSB_ADDR)	(LSB_ADDR / LSB_ITEM_SIZE)
118*4b394a23SGary R Hook 
119*4b394a23SGary R Hook /* ------------------------ CCP Version 3 Specifics ------------------------ */
12063b94509STom Lendacky #define REQ0_WAIT_FOR_WRITE		0x00000004
12163b94509STom Lendacky #define REQ0_INT_ON_COMPLETE		0x00000002
12263b94509STom Lendacky #define REQ0_STOP_ON_COMPLETE		0x00000001
12363b94509STom Lendacky 
12463b94509STom Lendacky #define REQ0_CMD_Q_SHIFT		9
12563b94509STom Lendacky #define REQ0_JOBID_SHIFT		3
12663b94509STom Lendacky 
12763b94509STom Lendacky /****** REQ1 Related Values ******/
12863b94509STom Lendacky #define REQ1_PROTECT_SHIFT		27
12963b94509STom Lendacky #define REQ1_ENGINE_SHIFT		23
13063b94509STom Lendacky #define REQ1_KEY_KSB_SHIFT		2
13163b94509STom Lendacky 
13263b94509STom Lendacky #define REQ1_EOM			0x00000002
13363b94509STom Lendacky #define REQ1_INIT			0x00000001
13463b94509STom Lendacky 
13563b94509STom Lendacky /* AES Related Values */
13663b94509STom Lendacky #define REQ1_AES_TYPE_SHIFT		21
13763b94509STom Lendacky #define REQ1_AES_MODE_SHIFT		18
13863b94509STom Lendacky #define REQ1_AES_ACTION_SHIFT		17
13963b94509STom Lendacky #define REQ1_AES_CFB_SIZE_SHIFT		10
14063b94509STom Lendacky 
14163b94509STom Lendacky /* XTS-AES Related Values */
14263b94509STom Lendacky #define REQ1_XTS_AES_SIZE_SHIFT		10
14363b94509STom Lendacky 
14463b94509STom Lendacky /* SHA Related Values */
14563b94509STom Lendacky #define REQ1_SHA_TYPE_SHIFT		21
14663b94509STom Lendacky 
14763b94509STom Lendacky /* RSA Related Values */
14863b94509STom Lendacky #define REQ1_RSA_MOD_SIZE_SHIFT		10
14963b94509STom Lendacky 
15063b94509STom Lendacky /* Pass-Through Related Values */
15163b94509STom Lendacky #define REQ1_PT_BW_SHIFT		12
15263b94509STom Lendacky #define REQ1_PT_BS_SHIFT		10
15363b94509STom Lendacky 
15463b94509STom Lendacky /* ECC Related Values */
15563b94509STom Lendacky #define REQ1_ECC_AFFINE_CONVERT		0x00200000
15663b94509STom Lendacky #define REQ1_ECC_FUNCTION_SHIFT		18
15763b94509STom Lendacky 
15863b94509STom Lendacky /****** REQ4 Related Values ******/
15963b94509STom Lendacky #define REQ4_KSB_SHIFT			18
16063b94509STom Lendacky #define REQ4_MEMTYPE_SHIFT		16
16163b94509STom Lendacky 
16263b94509STom Lendacky /****** REQ6 Related Values ******/
16363b94509STom Lendacky #define REQ6_MEMTYPE_SHIFT		16
16463b94509STom Lendacky 
16563b94509STom Lendacky /****** Key Storage Block ******/
16663b94509STom Lendacky #define KSB_START			77
16763b94509STom Lendacky #define KSB_END				127
16863b94509STom Lendacky #define KSB_COUNT			(KSB_END - KSB_START + 1)
169956ee21aSGary R Hook #define CCP_SB_BITS			256
17063b94509STom Lendacky 
17163b94509STom Lendacky #define CCP_JOBID_MASK			0x0000003f
17263b94509STom Lendacky 
173*4b394a23SGary R Hook /* ------------------------ General CCP Defines ------------------------ */
174*4b394a23SGary R Hook 
17563b94509STom Lendacky #define CCP_DMAPOOL_MAX_SIZE		64
1768db88467STom Lendacky #define CCP_DMAPOOL_ALIGN		BIT(5)
17763b94509STom Lendacky 
17863b94509STom Lendacky #define CCP_REVERSE_BUF_SIZE		64
17963b94509STom Lendacky 
180956ee21aSGary R Hook #define CCP_AES_KEY_SB_COUNT		1
181956ee21aSGary R Hook #define CCP_AES_CTX_SB_COUNT		1
18263b94509STom Lendacky 
183956ee21aSGary R Hook #define CCP_XTS_AES_KEY_SB_COUNT	1
184956ee21aSGary R Hook #define CCP_XTS_AES_CTX_SB_COUNT	1
18563b94509STom Lendacky 
186956ee21aSGary R Hook #define CCP_SHA_SB_COUNT		1
18763b94509STom Lendacky 
18863b94509STom Lendacky #define CCP_RSA_MAX_WIDTH		4096
18963b94509STom Lendacky 
19063b94509STom Lendacky #define CCP_PASSTHRU_BLOCKSIZE		256
19163b94509STom Lendacky #define CCP_PASSTHRU_MASKSIZE		32
192956ee21aSGary R Hook #define CCP_PASSTHRU_SB_COUNT		1
19363b94509STom Lendacky 
19463b94509STom Lendacky #define CCP_ECC_MODULUS_BYTES		48      /* 384-bits */
19563b94509STom Lendacky #define CCP_ECC_MAX_OPERANDS		6
19663b94509STom Lendacky #define CCP_ECC_MAX_OUTPUTS		3
19763b94509STom Lendacky #define CCP_ECC_SRC_BUF_SIZE		448
19863b94509STom Lendacky #define CCP_ECC_DST_BUF_SIZE		192
19963b94509STom Lendacky #define CCP_ECC_OPERAND_SIZE		64
20063b94509STom Lendacky #define CCP_ECC_OUTPUT_SIZE		64
20163b94509STom Lendacky #define CCP_ECC_RESULT_OFFSET		60
20263b94509STom Lendacky #define CCP_ECC_RESULT_SUCCESS		0x0001
20363b94509STom Lendacky 
204956ee21aSGary R Hook #define CCP_SB_BYTES			32
205956ee21aSGary R Hook 
206ea0375afSGary R Hook struct ccp_op;
20763b94509STom Lendacky struct ccp_device;
20863b94509STom Lendacky struct ccp_cmd;
209*4b394a23SGary R Hook struct ccp_fns;
21063b94509STom Lendacky 
21158ea8abfSGary R Hook struct ccp_dma_cmd {
21258ea8abfSGary R Hook 	struct list_head entry;
21358ea8abfSGary R Hook 
21458ea8abfSGary R Hook 	struct ccp_cmd ccp_cmd;
21558ea8abfSGary R Hook };
21658ea8abfSGary R Hook 
21758ea8abfSGary R Hook struct ccp_dma_desc {
21858ea8abfSGary R Hook 	struct list_head entry;
21958ea8abfSGary R Hook 
22058ea8abfSGary R Hook 	struct ccp_device *ccp;
22158ea8abfSGary R Hook 
22258ea8abfSGary R Hook 	struct list_head pending;
22358ea8abfSGary R Hook 	struct list_head active;
22458ea8abfSGary R Hook 
22558ea8abfSGary R Hook 	enum dma_status status;
22658ea8abfSGary R Hook 	struct dma_async_tx_descriptor tx_desc;
22758ea8abfSGary R Hook 	size_t len;
22858ea8abfSGary R Hook };
22958ea8abfSGary R Hook 
23058ea8abfSGary R Hook struct ccp_dma_chan {
23158ea8abfSGary R Hook 	struct ccp_device *ccp;
23258ea8abfSGary R Hook 
23358ea8abfSGary R Hook 	spinlock_t lock;
23458ea8abfSGary R Hook 	struct list_head pending;
23558ea8abfSGary R Hook 	struct list_head active;
23658ea8abfSGary R Hook 	struct list_head complete;
23758ea8abfSGary R Hook 
23858ea8abfSGary R Hook 	struct tasklet_struct cleanup_tasklet;
23958ea8abfSGary R Hook 
24058ea8abfSGary R Hook 	enum dma_status status;
24158ea8abfSGary R Hook 	struct dma_chan dma_chan;
24258ea8abfSGary R Hook };
24358ea8abfSGary R Hook 
24463b94509STom Lendacky struct ccp_cmd_queue {
24563b94509STom Lendacky 	struct ccp_device *ccp;
24663b94509STom Lendacky 
24763b94509STom Lendacky 	/* Queue identifier */
24863b94509STom Lendacky 	u32 id;
24963b94509STom Lendacky 
25063b94509STom Lendacky 	/* Queue dma pool */
25163b94509STom Lendacky 	struct dma_pool *dma_pool;
25263b94509STom Lendacky 
253*4b394a23SGary R Hook 	/* Queue base address (not neccessarily aligned)*/
254*4b394a23SGary R Hook 	struct ccp5_desc *qbase;
255*4b394a23SGary R Hook 
256*4b394a23SGary R Hook 	/* Aligned queue start address (per requirement) */
257*4b394a23SGary R Hook 	struct mutex q_mutex ____cacheline_aligned;
258*4b394a23SGary R Hook 	unsigned int qidx;
259*4b394a23SGary R Hook 
260*4b394a23SGary R Hook 	/* Version 5 has different requirements for queue memory */
261*4b394a23SGary R Hook 	unsigned int qsize;
262*4b394a23SGary R Hook 	dma_addr_t qbase_dma;
263*4b394a23SGary R Hook 	dma_addr_t qdma_tail;
264*4b394a23SGary R Hook 
265956ee21aSGary R Hook 	/* Per-queue reserved storage block(s) */
266956ee21aSGary R Hook 	u32 sb_key;
267956ee21aSGary R Hook 	u32 sb_ctx;
26863b94509STom Lendacky 
269*4b394a23SGary R Hook 	/* Bitmap of LSBs that can be accessed by this queue */
270*4b394a23SGary R Hook 	DECLARE_BITMAP(lsbmask, MAX_LSB_CNT);
271*4b394a23SGary R Hook 	/* Private LSB that is assigned to this queue, or -1 if none.
272*4b394a23SGary R Hook 	 * Bitmap for my private LSB, unused otherwise
273*4b394a23SGary R Hook 	 */
274*4b394a23SGary R Hook 	unsigned int lsb;
275*4b394a23SGary R Hook 	DECLARE_BITMAP(lsbmap, PLSB_MAP_SIZE);
276*4b394a23SGary R Hook 
27763b94509STom Lendacky 	/* Queue processing thread */
27863b94509STom Lendacky 	struct task_struct *kthread;
27963b94509STom Lendacky 	unsigned int active;
28063b94509STom Lendacky 	unsigned int suspended;
28163b94509STom Lendacky 
28263b94509STom Lendacky 	/* Number of free command slots available */
28363b94509STom Lendacky 	unsigned int free_slots;
28463b94509STom Lendacky 
28563b94509STom Lendacky 	/* Interrupt masks */
28663b94509STom Lendacky 	u32 int_ok;
28763b94509STom Lendacky 	u32 int_err;
28863b94509STom Lendacky 
28963b94509STom Lendacky 	/* Register addresses for queue */
290*4b394a23SGary R Hook 	void __iomem *reg_control;
291*4b394a23SGary R Hook 	void __iomem *reg_tail_lo;
292*4b394a23SGary R Hook 	void __iomem *reg_head_lo;
293*4b394a23SGary R Hook 	void __iomem *reg_int_enable;
294*4b394a23SGary R Hook 	void __iomem *reg_interrupt_status;
29563b94509STom Lendacky 	void __iomem *reg_status;
29663b94509STom Lendacky 	void __iomem *reg_int_status;
297*4b394a23SGary R Hook 	void __iomem *reg_dma_status;
298*4b394a23SGary R Hook 	void __iomem *reg_dma_read_status;
299*4b394a23SGary R Hook 	void __iomem *reg_dma_write_status;
300*4b394a23SGary R Hook 	u32 qcontrol; /* Cached control register */
30163b94509STom Lendacky 
30263b94509STom Lendacky 	/* Status values from job */
30363b94509STom Lendacky 	u32 int_status;
30463b94509STom Lendacky 	u32 q_status;
30563b94509STom Lendacky 	u32 q_int_status;
30663b94509STom Lendacky 	u32 cmd_error;
30763b94509STom Lendacky 
30863b94509STom Lendacky 	/* Interrupt wait queue */
30963b94509STom Lendacky 	wait_queue_head_t int_queue;
31063b94509STom Lendacky 	unsigned int int_rcvd;
31163b94509STom Lendacky } ____cacheline_aligned;
31263b94509STom Lendacky 
31363b94509STom Lendacky struct ccp_device {
314553d2374SGary R Hook 	struct list_head entry;
315553d2374SGary R Hook 
316c7019c4dSGary R Hook 	struct ccp_vdata *vdata;
317553d2374SGary R Hook 	unsigned int ord;
318553d2374SGary R Hook 	char name[MAX_CCP_NAME_LEN];
319553d2374SGary R Hook 	char rngname[MAX_CCP_NAME_LEN];
320553d2374SGary R Hook 
32163b94509STom Lendacky 	struct device *dev;
32263b94509STom Lendacky 
323fa242e80SGary R Hook 	/* Bus specific device information
32463b94509STom Lendacky 	 */
32563b94509STom Lendacky 	void *dev_specific;
32663b94509STom Lendacky 	int (*get_irq)(struct ccp_device *ccp);
32763b94509STom Lendacky 	void (*free_irq)(struct ccp_device *ccp);
3283d77565bSTom Lendacky 	unsigned int irq;
32963b94509STom Lendacky 
330fa242e80SGary R Hook 	/* I/O area used for device communication. The register mapping
33163b94509STom Lendacky 	 * starts at an offset into the mapped bar.
33263b94509STom Lendacky 	 *   The CMD_REQx registers and the Delete_Cmd_Queue_Job register
33363b94509STom Lendacky 	 *   need to be protected while a command queue thread is accessing
33463b94509STom Lendacky 	 *   them.
33563b94509STom Lendacky 	 */
33663b94509STom Lendacky 	struct mutex req_mutex ____cacheline_aligned;
33763b94509STom Lendacky 	void __iomem *io_map;
33863b94509STom Lendacky 	void __iomem *io_regs;
33963b94509STom Lendacky 
340fa242e80SGary R Hook 	/* Master lists that all cmds are queued on. Because there can be
34163b94509STom Lendacky 	 * more than one CCP command queue that can process a cmd a separate
34263b94509STom Lendacky 	 * backlog list is neeeded so that the backlog completion call
34363b94509STom Lendacky 	 * completes before the cmd is available for execution.
34463b94509STom Lendacky 	 */
34563b94509STom Lendacky 	spinlock_t cmd_lock ____cacheline_aligned;
34663b94509STom Lendacky 	unsigned int cmd_count;
34763b94509STom Lendacky 	struct list_head cmd;
34863b94509STom Lendacky 	struct list_head backlog;
34963b94509STom Lendacky 
350fa242e80SGary R Hook 	/* The command queues. These represent the queues available on the
35163b94509STom Lendacky 	 * CCP that are available for processing cmds
35263b94509STom Lendacky 	 */
35363b94509STom Lendacky 	struct ccp_cmd_queue cmd_q[MAX_HW_QUEUES];
35463b94509STom Lendacky 	unsigned int cmd_q_count;
35563b94509STom Lendacky 
356fa242e80SGary R Hook 	/* Support for the CCP True RNG
35763b94509STom Lendacky 	 */
35863b94509STom Lendacky 	struct hwrng hwrng;
35963b94509STom Lendacky 	unsigned int hwrng_retries;
36063b94509STom Lendacky 
361fa242e80SGary R Hook 	/* Support for the CCP DMA capabilities
36258ea8abfSGary R Hook 	 */
36358ea8abfSGary R Hook 	struct dma_device dma_dev;
36458ea8abfSGary R Hook 	struct ccp_dma_chan *ccp_dma_chan;
36558ea8abfSGary R Hook 	struct kmem_cache *dma_cmd_cache;
36658ea8abfSGary R Hook 	struct kmem_cache *dma_desc_cache;
36758ea8abfSGary R Hook 
368fa242e80SGary R Hook 	/* A counter used to generate job-ids for cmds submitted to the CCP
36963b94509STom Lendacky 	 */
37063b94509STom Lendacky 	atomic_t current_id ____cacheline_aligned;
37163b94509STom Lendacky 
37258a690b7SGary R Hook 	/* The v3 CCP uses key storage blocks (SB) to maintain context for
37358a690b7SGary R Hook 	 * certain operations. To prevent multiple cmds from using the same
37458a690b7SGary R Hook 	 * SB range a command queue reserves an SB range for the duration of
37558a690b7SGary R Hook 	 * the cmd. Each queue, will however, reserve 2 SB blocks for
37658a690b7SGary R Hook 	 * operations that only require single SB entries (eg. AES context/iv
37758a690b7SGary R Hook 	 * and key) in order to avoid allocation contention.  This will reserve
37858a690b7SGary R Hook 	 * at most 10 SB entries, leaving 40 SB entries available for dynamic
37958a690b7SGary R Hook 	 * allocation.
38058a690b7SGary R Hook 	 *
38158a690b7SGary R Hook 	 * The v5 CCP Local Storage Block (LSB) is broken up into 8
38258a690b7SGary R Hook 	 * memrory ranges, each of which can be enabled for access by one
38358a690b7SGary R Hook 	 * or more queues. Device initialization takes this into account,
38458a690b7SGary R Hook 	 * and attempts to assign one region for exclusive use by each
38558a690b7SGary R Hook 	 * available queue; the rest are then aggregated as "public" use.
38658a690b7SGary R Hook 	 * If there are fewer regions than queues, all regions are shared
38758a690b7SGary R Hook 	 * amongst all queues.
38863b94509STom Lendacky 	 */
389956ee21aSGary R Hook 	struct mutex sb_mutex ____cacheline_aligned;
390956ee21aSGary R Hook 	DECLARE_BITMAP(sb, KSB_COUNT);
391956ee21aSGary R Hook 	wait_queue_head_t sb_queue;
392956ee21aSGary R Hook 	unsigned int sb_avail;
393956ee21aSGary R Hook 	unsigned int sb_count;
394956ee21aSGary R Hook 	u32 sb_start;
39563b94509STom Lendacky 
396*4b394a23SGary R Hook 	/* Bitmap of shared LSBs, if any */
397*4b394a23SGary R Hook 	DECLARE_BITMAP(lsbmap, SLSB_MAP_SIZE);
398*4b394a23SGary R Hook 
39963b94509STom Lendacky 	/* Suspend support */
40063b94509STom Lendacky 	unsigned int suspending;
40163b94509STom Lendacky 	wait_queue_head_t suspend_queue;
402126ae9adSTom Lendacky 
403126ae9adSTom Lendacky 	/* DMA caching attribute support */
404126ae9adSTom Lendacky 	unsigned int axcache;
40563b94509STom Lendacky };
40663b94509STom Lendacky 
407ea0375afSGary R Hook enum ccp_memtype {
408ea0375afSGary R Hook 	CCP_MEMTYPE_SYSTEM = 0,
409956ee21aSGary R Hook 	CCP_MEMTYPE_SB,
410ea0375afSGary R Hook 	CCP_MEMTYPE_LOCAL,
411ea0375afSGary R Hook 	CCP_MEMTYPE__LAST,
412ea0375afSGary R Hook };
413*4b394a23SGary R Hook #define	CCP_MEMTYPE_LSB	CCP_MEMTYPE_KSB
414ea0375afSGary R Hook 
415ea0375afSGary R Hook struct ccp_dma_info {
416ea0375afSGary R Hook 	dma_addr_t address;
417ea0375afSGary R Hook 	unsigned int offset;
418ea0375afSGary R Hook 	unsigned int length;
419ea0375afSGary R Hook 	enum dma_data_direction dir;
420ea0375afSGary R Hook };
421ea0375afSGary R Hook 
422ea0375afSGary R Hook struct ccp_dm_workarea {
423ea0375afSGary R Hook 	struct device *dev;
424ea0375afSGary R Hook 	struct dma_pool *dma_pool;
425ea0375afSGary R Hook 	unsigned int length;
426ea0375afSGary R Hook 
427ea0375afSGary R Hook 	u8 *address;
428ea0375afSGary R Hook 	struct ccp_dma_info dma;
429ea0375afSGary R Hook };
430ea0375afSGary R Hook 
431ea0375afSGary R Hook struct ccp_sg_workarea {
432ea0375afSGary R Hook 	struct scatterlist *sg;
433ea0375afSGary R Hook 	int nents;
434ea0375afSGary R Hook 
435ea0375afSGary R Hook 	struct scatterlist *dma_sg;
436ea0375afSGary R Hook 	struct device *dma_dev;
437ea0375afSGary R Hook 	unsigned int dma_count;
438ea0375afSGary R Hook 	enum dma_data_direction dma_dir;
439ea0375afSGary R Hook 
440ea0375afSGary R Hook 	unsigned int sg_used;
441ea0375afSGary R Hook 
442ea0375afSGary R Hook 	u64 bytes_left;
443ea0375afSGary R Hook };
444ea0375afSGary R Hook 
445ea0375afSGary R Hook struct ccp_data {
446ea0375afSGary R Hook 	struct ccp_sg_workarea sg_wa;
447ea0375afSGary R Hook 	struct ccp_dm_workarea dm_wa;
448ea0375afSGary R Hook };
449ea0375afSGary R Hook 
450ea0375afSGary R Hook struct ccp_mem {
451ea0375afSGary R Hook 	enum ccp_memtype type;
452ea0375afSGary R Hook 	union {
453ea0375afSGary R Hook 		struct ccp_dma_info dma;
454956ee21aSGary R Hook 		u32 sb;
455ea0375afSGary R Hook 	} u;
456ea0375afSGary R Hook };
457ea0375afSGary R Hook 
458ea0375afSGary R Hook struct ccp_aes_op {
459ea0375afSGary R Hook 	enum ccp_aes_type type;
460ea0375afSGary R Hook 	enum ccp_aes_mode mode;
461ea0375afSGary R Hook 	enum ccp_aes_action action;
462ea0375afSGary R Hook };
463ea0375afSGary R Hook 
464ea0375afSGary R Hook struct ccp_xts_aes_op {
465ea0375afSGary R Hook 	enum ccp_aes_action action;
466ea0375afSGary R Hook 	enum ccp_xts_aes_unit_size unit_size;
467ea0375afSGary R Hook };
468ea0375afSGary R Hook 
469ea0375afSGary R Hook struct ccp_sha_op {
470ea0375afSGary R Hook 	enum ccp_sha_type type;
471ea0375afSGary R Hook 	u64 msg_bits;
472ea0375afSGary R Hook };
473ea0375afSGary R Hook 
474ea0375afSGary R Hook struct ccp_rsa_op {
475ea0375afSGary R Hook 	u32 mod_size;
476ea0375afSGary R Hook 	u32 input_len;
477ea0375afSGary R Hook };
478ea0375afSGary R Hook 
479ea0375afSGary R Hook struct ccp_passthru_op {
480ea0375afSGary R Hook 	enum ccp_passthru_bitwise bit_mod;
481ea0375afSGary R Hook 	enum ccp_passthru_byteswap byte_swap;
482ea0375afSGary R Hook };
483ea0375afSGary R Hook 
484ea0375afSGary R Hook struct ccp_ecc_op {
485ea0375afSGary R Hook 	enum ccp_ecc_function function;
486ea0375afSGary R Hook };
487ea0375afSGary R Hook 
488ea0375afSGary R Hook struct ccp_op {
489ea0375afSGary R Hook 	struct ccp_cmd_queue *cmd_q;
490ea0375afSGary R Hook 
491ea0375afSGary R Hook 	u32 jobid;
492ea0375afSGary R Hook 	u32 ioc;
493ea0375afSGary R Hook 	u32 soc;
494956ee21aSGary R Hook 	u32 sb_key;
495956ee21aSGary R Hook 	u32 sb_ctx;
496ea0375afSGary R Hook 	u32 init;
497ea0375afSGary R Hook 	u32 eom;
498ea0375afSGary R Hook 
499ea0375afSGary R Hook 	struct ccp_mem src;
500ea0375afSGary R Hook 	struct ccp_mem dst;
501*4b394a23SGary R Hook 	struct ccp_mem exp;
502ea0375afSGary R Hook 
503ea0375afSGary R Hook 	union {
504ea0375afSGary R Hook 		struct ccp_aes_op aes;
505ea0375afSGary R Hook 		struct ccp_xts_aes_op xts;
506ea0375afSGary R Hook 		struct ccp_sha_op sha;
507ea0375afSGary R Hook 		struct ccp_rsa_op rsa;
508ea0375afSGary R Hook 		struct ccp_passthru_op passthru;
509ea0375afSGary R Hook 		struct ccp_ecc_op ecc;
510ea0375afSGary R Hook 	} u;
511*4b394a23SGary R Hook 	struct ccp_mem key;
512ea0375afSGary R Hook };
513ea0375afSGary R Hook 
514ea0375afSGary R Hook static inline u32 ccp_addr_lo(struct ccp_dma_info *info)
515ea0375afSGary R Hook {
516ea0375afSGary R Hook 	return lower_32_bits(info->address + info->offset);
517ea0375afSGary R Hook }
518ea0375afSGary R Hook 
519ea0375afSGary R Hook static inline u32 ccp_addr_hi(struct ccp_dma_info *info)
520ea0375afSGary R Hook {
521ea0375afSGary R Hook 	return upper_32_bits(info->address + info->offset) & 0x0000ffff;
522ea0375afSGary R Hook }
523ea0375afSGary R Hook 
524*4b394a23SGary R Hook /**
525*4b394a23SGary R Hook  * descriptor for version 5 CPP commands
526*4b394a23SGary R Hook  * 8 32-bit words:
527*4b394a23SGary R Hook  * word 0: function; engine; control bits
528*4b394a23SGary R Hook  * word 1: length of source data
529*4b394a23SGary R Hook  * word 2: low 32 bits of source pointer
530*4b394a23SGary R Hook  * word 3: upper 16 bits of source pointer; source memory type
531*4b394a23SGary R Hook  * word 4: low 32 bits of destination pointer
532*4b394a23SGary R Hook  * word 5: upper 16 bits of destination pointer; destination memory type
533*4b394a23SGary R Hook  * word 6: low 32 bits of key pointer
534*4b394a23SGary R Hook  * word 7: upper 16 bits of key pointer; key memory type
535*4b394a23SGary R Hook  */
536*4b394a23SGary R Hook struct dword0 {
537*4b394a23SGary R Hook 	__le32 soc:1;
538*4b394a23SGary R Hook 	__le32 ioc:1;
539*4b394a23SGary R Hook 	__le32 rsvd1:1;
540*4b394a23SGary R Hook 	__le32 init:1;
541*4b394a23SGary R Hook 	__le32 eom:1;		/* AES/SHA only */
542*4b394a23SGary R Hook 	__le32 function:15;
543*4b394a23SGary R Hook 	__le32 engine:4;
544*4b394a23SGary R Hook 	__le32 prot:1;
545*4b394a23SGary R Hook 	__le32 rsvd2:7;
546*4b394a23SGary R Hook };
547*4b394a23SGary R Hook 
548*4b394a23SGary R Hook struct dword3 {
549*4b394a23SGary R Hook 	__le32 src_hi:16;
550*4b394a23SGary R Hook 	__le32 src_mem:2;
551*4b394a23SGary R Hook 	__le32 lsb_cxt_id:8;
552*4b394a23SGary R Hook 	__le32 rsvd1:5;
553*4b394a23SGary R Hook 	__le32 fixed:1;
554*4b394a23SGary R Hook };
555*4b394a23SGary R Hook 
556*4b394a23SGary R Hook union dword4 {
557*4b394a23SGary R Hook 	__le32 dst_lo;		/* NON-SHA	*/
558*4b394a23SGary R Hook 	__le32 sha_len_lo;	/* SHA		*/
559*4b394a23SGary R Hook };
560*4b394a23SGary R Hook 
561*4b394a23SGary R Hook union dword5 {
562*4b394a23SGary R Hook 	struct {
563*4b394a23SGary R Hook 		__le32 dst_hi:16;
564*4b394a23SGary R Hook 		__le32 dst_mem:2;
565*4b394a23SGary R Hook 		__le32 rsvd1:13;
566*4b394a23SGary R Hook 		__le32 fixed:1;
567*4b394a23SGary R Hook 	} fields;
568*4b394a23SGary R Hook 	__le32 sha_len_hi;
569*4b394a23SGary R Hook };
570*4b394a23SGary R Hook 
571*4b394a23SGary R Hook struct dword7 {
572*4b394a23SGary R Hook 	__le32 key_hi:16;
573*4b394a23SGary R Hook 	__le32 key_mem:2;
574*4b394a23SGary R Hook 	__le32 rsvd1:14;
575*4b394a23SGary R Hook };
576*4b394a23SGary R Hook 
577*4b394a23SGary R Hook struct ccp5_desc {
578*4b394a23SGary R Hook 	struct dword0 dw0;
579*4b394a23SGary R Hook 	__le32 length;
580*4b394a23SGary R Hook 	__le32 src_lo;
581*4b394a23SGary R Hook 	struct dword3 dw3;
582*4b394a23SGary R Hook 	union dword4 dw4;
583*4b394a23SGary R Hook 	union dword5 dw5;
584*4b394a23SGary R Hook 	__le32 key_lo;
585*4b394a23SGary R Hook 	struct dword7 dw7;
586*4b394a23SGary R Hook };
587*4b394a23SGary R Hook 
58863b94509STom Lendacky int ccp_pci_init(void);
58963b94509STom Lendacky void ccp_pci_exit(void);
59063b94509STom Lendacky 
591c4f4b325STom Lendacky int ccp_platform_init(void);
592c4f4b325STom Lendacky void ccp_platform_exit(void);
593c4f4b325STom Lendacky 
594ea0375afSGary R Hook void ccp_add_device(struct ccp_device *ccp);
595ea0375afSGary R Hook void ccp_del_device(struct ccp_device *ccp);
59663b94509STom Lendacky 
597ea0375afSGary R Hook struct ccp_device *ccp_alloc_struct(struct device *dev);
598ea0375afSGary R Hook bool ccp_queues_suspended(struct ccp_device *ccp);
599ea0375afSGary R Hook int ccp_cmd_queue_thread(void *data);
6008256e683SGary R Hook int ccp_trng_read(struct hwrng *rng, void *data, size_t max, bool wait);
60163b94509STom Lendacky 
60263b94509STom Lendacky int ccp_run_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd);
60363b94509STom Lendacky 
60458ea8abfSGary R Hook int ccp_dmaengine_register(struct ccp_device *ccp);
60558ea8abfSGary R Hook void ccp_dmaengine_unregister(struct ccp_device *ccp);
60658ea8abfSGary R Hook 
60758a690b7SGary R Hook /* Structure for computation functions that are device-specific */
60858a690b7SGary R Hook struct ccp_actions {
60958a690b7SGary R Hook 	int (*aes)(struct ccp_op *);
61058a690b7SGary R Hook 	int (*xts_aes)(struct ccp_op *);
61158a690b7SGary R Hook 	int (*sha)(struct ccp_op *);
61258a690b7SGary R Hook 	int (*rsa)(struct ccp_op *);
61358a690b7SGary R Hook 	int (*passthru)(struct ccp_op *);
61458a690b7SGary R Hook 	int (*ecc)(struct ccp_op *);
61558a690b7SGary R Hook 	u32 (*sballoc)(struct ccp_cmd_queue *, unsigned int);
61658a690b7SGary R Hook 	void (*sbfree)(struct ccp_cmd_queue *, unsigned int,
61758a690b7SGary R Hook 			       unsigned int);
618bb4e89b3SGary R Hook 	unsigned int (*get_free_slots)(struct ccp_cmd_queue *);
61958a690b7SGary R Hook 	int (*init)(struct ccp_device *);
62058a690b7SGary R Hook 	void (*destroy)(struct ccp_device *);
62158a690b7SGary R Hook 	irqreturn_t (*irqhandler)(int, void *);
62258a690b7SGary R Hook };
62358a690b7SGary R Hook 
62458a690b7SGary R Hook /* Structure to hold CCP version-specific values */
62558a690b7SGary R Hook struct ccp_vdata {
626*4b394a23SGary R Hook 	const unsigned int version;
627*4b394a23SGary R Hook 	void (*setup)(struct ccp_device *);
62858a690b7SGary R Hook 	const struct ccp_actions *perform;
62958a690b7SGary R Hook 	const unsigned int bar;
63058a690b7SGary R Hook 	const unsigned int offset;
63158a690b7SGary R Hook };
63258a690b7SGary R Hook 
63358a690b7SGary R Hook extern	struct ccp_vdata ccpv3;
634*4b394a23SGary R Hook extern	struct ccp_vdata ccpv5;
63558a690b7SGary R Hook 
63663b94509STom Lendacky #endif
637