xref: /openbmc/linux/drivers/crypto/ccp/ccp-dev.h (revision 3d77565ba5e5b9075a4f6d7d6d15996f5e582659)
163b94509STom Lendacky /*
263b94509STom Lendacky  * AMD Cryptographic Coprocessor (CCP) driver
363b94509STom Lendacky  *
463b94509STom Lendacky  * Copyright (C) 2013 Advanced Micro Devices, Inc.
563b94509STom Lendacky  *
663b94509STom Lendacky  * Author: Tom Lendacky <thomas.lendacky@amd.com>
763b94509STom Lendacky  *
863b94509STom Lendacky  * This program is free software; you can redistribute it and/or modify
963b94509STom Lendacky  * it under the terms of the GNU General Public License version 2 as
1063b94509STom Lendacky  * published by the Free Software Foundation.
1163b94509STom Lendacky  */
1263b94509STom Lendacky 
1363b94509STom Lendacky #ifndef __CCP_DEV_H__
1463b94509STom Lendacky #define __CCP_DEV_H__
1563b94509STom Lendacky 
1663b94509STom Lendacky #include <linux/device.h>
1763b94509STom Lendacky #include <linux/pci.h>
1863b94509STom Lendacky #include <linux/spinlock.h>
1963b94509STom Lendacky #include <linux/mutex.h>
2063b94509STom Lendacky #include <linux/list.h>
2163b94509STom Lendacky #include <linux/wait.h>
2263b94509STom Lendacky #include <linux/dmapool.h>
2363b94509STom Lendacky #include <linux/hw_random.h>
2463b94509STom Lendacky 
2563b94509STom Lendacky 
2663b94509STom Lendacky #define MAX_DMAPOOL_NAME_LEN		32
2763b94509STom Lendacky 
2863b94509STom Lendacky #define MAX_HW_QUEUES			5
2963b94509STom Lendacky #define MAX_CMD_QLEN			100
3063b94509STom Lendacky 
3163b94509STom Lendacky #define TRNG_RETRIES			10
3263b94509STom Lendacky 
3363b94509STom Lendacky 
3463b94509STom Lendacky /****** Register Mappings ******/
3563b94509STom Lendacky #define Q_MASK_REG			0x000
3663b94509STom Lendacky #define TRNG_OUT_REG			0x00c
3763b94509STom Lendacky #define IRQ_MASK_REG			0x040
3863b94509STom Lendacky #define IRQ_STATUS_REG			0x200
3963b94509STom Lendacky 
4063b94509STom Lendacky #define DEL_CMD_Q_JOB			0x124
4163b94509STom Lendacky #define DEL_Q_ACTIVE			0x00000200
4263b94509STom Lendacky #define DEL_Q_ID_SHIFT			6
4363b94509STom Lendacky 
4463b94509STom Lendacky #define CMD_REQ0			0x180
4563b94509STom Lendacky #define CMD_REQ_INCR			0x04
4663b94509STom Lendacky 
4763b94509STom Lendacky #define CMD_Q_STATUS_BASE		0x210
4863b94509STom Lendacky #define CMD_Q_INT_STATUS_BASE		0x214
4963b94509STom Lendacky #define CMD_Q_STATUS_INCR		0x20
5063b94509STom Lendacky 
5163b94509STom Lendacky #define CMD_Q_CACHE			0x228
5263b94509STom Lendacky #define CMD_Q_CACHE_INC			0x20
5363b94509STom Lendacky 
5463b94509STom Lendacky #define CMD_Q_ERROR(__qs)		((__qs) & 0x0000003f);
5563b94509STom Lendacky #define CMD_Q_DEPTH(__qs)		(((__qs) >> 12) & 0x0000000f);
5663b94509STom Lendacky 
5763b94509STom Lendacky /****** REQ0 Related Values ******/
5863b94509STom Lendacky #define REQ0_WAIT_FOR_WRITE		0x00000004
5963b94509STom Lendacky #define REQ0_INT_ON_COMPLETE		0x00000002
6063b94509STom Lendacky #define REQ0_STOP_ON_COMPLETE		0x00000001
6163b94509STom Lendacky 
6263b94509STom Lendacky #define REQ0_CMD_Q_SHIFT		9
6363b94509STom Lendacky #define REQ0_JOBID_SHIFT		3
6463b94509STom Lendacky 
6563b94509STom Lendacky /****** REQ1 Related Values ******/
6663b94509STom Lendacky #define REQ1_PROTECT_SHIFT		27
6763b94509STom Lendacky #define REQ1_ENGINE_SHIFT		23
6863b94509STom Lendacky #define REQ1_KEY_KSB_SHIFT		2
6963b94509STom Lendacky 
7063b94509STom Lendacky #define REQ1_EOM			0x00000002
7163b94509STom Lendacky #define REQ1_INIT			0x00000001
7263b94509STom Lendacky 
7363b94509STom Lendacky /* AES Related Values */
7463b94509STom Lendacky #define REQ1_AES_TYPE_SHIFT		21
7563b94509STom Lendacky #define REQ1_AES_MODE_SHIFT		18
7663b94509STom Lendacky #define REQ1_AES_ACTION_SHIFT		17
7763b94509STom Lendacky #define REQ1_AES_CFB_SIZE_SHIFT		10
7863b94509STom Lendacky 
7963b94509STom Lendacky /* XTS-AES Related Values */
8063b94509STom Lendacky #define REQ1_XTS_AES_SIZE_SHIFT		10
8163b94509STom Lendacky 
8263b94509STom Lendacky /* SHA Related Values */
8363b94509STom Lendacky #define REQ1_SHA_TYPE_SHIFT		21
8463b94509STom Lendacky 
8563b94509STom Lendacky /* RSA Related Values */
8663b94509STom Lendacky #define REQ1_RSA_MOD_SIZE_SHIFT		10
8763b94509STom Lendacky 
8863b94509STom Lendacky /* Pass-Through Related Values */
8963b94509STom Lendacky #define REQ1_PT_BW_SHIFT		12
9063b94509STom Lendacky #define REQ1_PT_BS_SHIFT		10
9163b94509STom Lendacky 
9263b94509STom Lendacky /* ECC Related Values */
9363b94509STom Lendacky #define REQ1_ECC_AFFINE_CONVERT		0x00200000
9463b94509STom Lendacky #define REQ1_ECC_FUNCTION_SHIFT		18
9563b94509STom Lendacky 
9663b94509STom Lendacky /****** REQ4 Related Values ******/
9763b94509STom Lendacky #define REQ4_KSB_SHIFT			18
9863b94509STom Lendacky #define REQ4_MEMTYPE_SHIFT		16
9963b94509STom Lendacky 
10063b94509STom Lendacky /****** REQ6 Related Values ******/
10163b94509STom Lendacky #define REQ6_MEMTYPE_SHIFT		16
10263b94509STom Lendacky 
10363b94509STom Lendacky 
10463b94509STom Lendacky /****** Key Storage Block ******/
10563b94509STom Lendacky #define KSB_START			77
10663b94509STom Lendacky #define KSB_END				127
10763b94509STom Lendacky #define KSB_COUNT			(KSB_END - KSB_START + 1)
10863b94509STom Lendacky #define CCP_KSB_BITS			256
10963b94509STom Lendacky #define CCP_KSB_BYTES			32
11063b94509STom Lendacky 
11163b94509STom Lendacky #define CCP_JOBID_MASK			0x0000003f
11263b94509STom Lendacky 
11363b94509STom Lendacky #define CCP_DMAPOOL_MAX_SIZE		64
11463b94509STom Lendacky #define CCP_DMAPOOL_ALIGN		(1 << 5)
11563b94509STom Lendacky 
11663b94509STom Lendacky #define CCP_REVERSE_BUF_SIZE		64
11763b94509STom Lendacky 
11863b94509STom Lendacky #define CCP_AES_KEY_KSB_COUNT		1
11963b94509STom Lendacky #define CCP_AES_CTX_KSB_COUNT		1
12063b94509STom Lendacky 
12163b94509STom Lendacky #define CCP_XTS_AES_KEY_KSB_COUNT	1
12263b94509STom Lendacky #define CCP_XTS_AES_CTX_KSB_COUNT	1
12363b94509STom Lendacky 
12463b94509STom Lendacky #define CCP_SHA_KSB_COUNT		1
12563b94509STom Lendacky 
12663b94509STom Lendacky #define CCP_RSA_MAX_WIDTH		4096
12763b94509STom Lendacky 
12863b94509STom Lendacky #define CCP_PASSTHRU_BLOCKSIZE		256
12963b94509STom Lendacky #define CCP_PASSTHRU_MASKSIZE		32
13063b94509STom Lendacky #define CCP_PASSTHRU_KSB_COUNT		1
13163b94509STom Lendacky 
13263b94509STom Lendacky #define CCP_ECC_MODULUS_BYTES		48      /* 384-bits */
13363b94509STom Lendacky #define CCP_ECC_MAX_OPERANDS		6
13463b94509STom Lendacky #define CCP_ECC_MAX_OUTPUTS		3
13563b94509STom Lendacky #define CCP_ECC_SRC_BUF_SIZE		448
13663b94509STom Lendacky #define CCP_ECC_DST_BUF_SIZE		192
13763b94509STom Lendacky #define CCP_ECC_OPERAND_SIZE		64
13863b94509STom Lendacky #define CCP_ECC_OUTPUT_SIZE		64
13963b94509STom Lendacky #define CCP_ECC_RESULT_OFFSET		60
14063b94509STom Lendacky #define CCP_ECC_RESULT_SUCCESS		0x0001
14163b94509STom Lendacky 
14263b94509STom Lendacky 
14363b94509STom Lendacky struct ccp_device;
14463b94509STom Lendacky struct ccp_cmd;
14563b94509STom Lendacky 
14663b94509STom Lendacky struct ccp_cmd_queue {
14763b94509STom Lendacky 	struct ccp_device *ccp;
14863b94509STom Lendacky 
14963b94509STom Lendacky 	/* Queue identifier */
15063b94509STom Lendacky 	u32 id;
15163b94509STom Lendacky 
15263b94509STom Lendacky 	/* Queue dma pool */
15363b94509STom Lendacky 	struct dma_pool *dma_pool;
15463b94509STom Lendacky 
15563b94509STom Lendacky 	/* Queue reserved KSB regions */
15663b94509STom Lendacky 	u32 ksb_key;
15763b94509STom Lendacky 	u32 ksb_ctx;
15863b94509STom Lendacky 
15963b94509STom Lendacky 	/* Queue processing thread */
16063b94509STom Lendacky 	struct task_struct *kthread;
16163b94509STom Lendacky 	unsigned int active;
16263b94509STom Lendacky 	unsigned int suspended;
16363b94509STom Lendacky 
16463b94509STom Lendacky 	/* Number of free command slots available */
16563b94509STom Lendacky 	unsigned int free_slots;
16663b94509STom Lendacky 
16763b94509STom Lendacky 	/* Interrupt masks */
16863b94509STom Lendacky 	u32 int_ok;
16963b94509STom Lendacky 	u32 int_err;
17063b94509STom Lendacky 
17163b94509STom Lendacky 	/* Register addresses for queue */
17263b94509STom Lendacky 	void __iomem *reg_status;
17363b94509STom Lendacky 	void __iomem *reg_int_status;
17463b94509STom Lendacky 
17563b94509STom Lendacky 	/* Status values from job */
17663b94509STom Lendacky 	u32 int_status;
17763b94509STom Lendacky 	u32 q_status;
17863b94509STom Lendacky 	u32 q_int_status;
17963b94509STom Lendacky 	u32 cmd_error;
18063b94509STom Lendacky 
18163b94509STom Lendacky 	/* Interrupt wait queue */
18263b94509STom Lendacky 	wait_queue_head_t int_queue;
18363b94509STom Lendacky 	unsigned int int_rcvd;
18463b94509STom Lendacky } ____cacheline_aligned;
18563b94509STom Lendacky 
18663b94509STom Lendacky struct ccp_device {
18763b94509STom Lendacky 	struct device *dev;
18863b94509STom Lendacky 
18963b94509STom Lendacky 	/*
19063b94509STom Lendacky 	 * Bus specific device information
19163b94509STom Lendacky 	 */
19263b94509STom Lendacky 	void *dev_specific;
19363b94509STom Lendacky 	int (*get_irq)(struct ccp_device *ccp);
19463b94509STom Lendacky 	void (*free_irq)(struct ccp_device *ccp);
195*3d77565bSTom Lendacky 	unsigned int irq;
19663b94509STom Lendacky 
19763b94509STom Lendacky 	/*
19863b94509STom Lendacky 	 * I/O area used for device communication. The register mapping
19963b94509STom Lendacky 	 * starts at an offset into the mapped bar.
20063b94509STom Lendacky 	 *   The CMD_REQx registers and the Delete_Cmd_Queue_Job register
20163b94509STom Lendacky 	 *   need to be protected while a command queue thread is accessing
20263b94509STom Lendacky 	 *   them.
20363b94509STom Lendacky 	 */
20463b94509STom Lendacky 	struct mutex req_mutex ____cacheline_aligned;
20563b94509STom Lendacky 	void __iomem *io_map;
20663b94509STom Lendacky 	void __iomem *io_regs;
20763b94509STom Lendacky 
20863b94509STom Lendacky 	/*
20963b94509STom Lendacky 	 * Master lists that all cmds are queued on. Because there can be
21063b94509STom Lendacky 	 * more than one CCP command queue that can process a cmd a separate
21163b94509STom Lendacky 	 * backlog list is neeeded so that the backlog completion call
21263b94509STom Lendacky 	 * completes before the cmd is available for execution.
21363b94509STom Lendacky 	 */
21463b94509STom Lendacky 	spinlock_t cmd_lock ____cacheline_aligned;
21563b94509STom Lendacky 	unsigned int cmd_count;
21663b94509STom Lendacky 	struct list_head cmd;
21763b94509STom Lendacky 	struct list_head backlog;
21863b94509STom Lendacky 
21963b94509STom Lendacky 	/*
22063b94509STom Lendacky 	 * The command queues. These represent the queues available on the
22163b94509STom Lendacky 	 * CCP that are available for processing cmds
22263b94509STom Lendacky 	 */
22363b94509STom Lendacky 	struct ccp_cmd_queue cmd_q[MAX_HW_QUEUES];
22463b94509STom Lendacky 	unsigned int cmd_q_count;
22563b94509STom Lendacky 
22663b94509STom Lendacky 	/*
22763b94509STom Lendacky 	 * Support for the CCP True RNG
22863b94509STom Lendacky 	 */
22963b94509STom Lendacky 	struct hwrng hwrng;
23063b94509STom Lendacky 	unsigned int hwrng_retries;
23163b94509STom Lendacky 
23263b94509STom Lendacky 	/*
23363b94509STom Lendacky 	 * A counter used to generate job-ids for cmds submitted to the CCP
23463b94509STom Lendacky 	 */
23563b94509STom Lendacky 	atomic_t current_id ____cacheline_aligned;
23663b94509STom Lendacky 
23763b94509STom Lendacky 	/*
23863b94509STom Lendacky 	 * The CCP uses key storage blocks (KSB) to maintain context for certain
23963b94509STom Lendacky 	 * operations. To prevent multiple cmds from using the same KSB range
24063b94509STom Lendacky 	 * a command queue reserves a KSB range for the duration of the cmd.
24163b94509STom Lendacky 	 * Each queue, will however, reserve 2 KSB blocks for operations that
24263b94509STom Lendacky 	 * only require single KSB entries (eg. AES context/iv and key) in order
24363b94509STom Lendacky 	 * to avoid allocation contention.  This will reserve at most 10 KSB
24463b94509STom Lendacky 	 * entries, leaving 40 KSB entries available for dynamic allocation.
24563b94509STom Lendacky 	 */
24663b94509STom Lendacky 	struct mutex ksb_mutex ____cacheline_aligned;
24763b94509STom Lendacky 	DECLARE_BITMAP(ksb, KSB_COUNT);
24863b94509STom Lendacky 	wait_queue_head_t ksb_queue;
24963b94509STom Lendacky 	unsigned int ksb_avail;
25063b94509STom Lendacky 	unsigned int ksb_count;
25163b94509STom Lendacky 	u32 ksb_start;
25263b94509STom Lendacky 
25363b94509STom Lendacky 	/* Suspend support */
25463b94509STom Lendacky 	unsigned int suspending;
25563b94509STom Lendacky 	wait_queue_head_t suspend_queue;
25663b94509STom Lendacky };
25763b94509STom Lendacky 
25863b94509STom Lendacky 
25963b94509STom Lendacky int ccp_pci_init(void);
26063b94509STom Lendacky void ccp_pci_exit(void);
26163b94509STom Lendacky 
26263b94509STom Lendacky struct ccp_device *ccp_alloc_struct(struct device *dev);
26363b94509STom Lendacky int ccp_init(struct ccp_device *ccp);
26463b94509STom Lendacky void ccp_destroy(struct ccp_device *ccp);
26563b94509STom Lendacky bool ccp_queues_suspended(struct ccp_device *ccp);
26663b94509STom Lendacky 
26763b94509STom Lendacky irqreturn_t ccp_irq_handler(int irq, void *data);
26863b94509STom Lendacky 
26963b94509STom Lendacky int ccp_run_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd);
27063b94509STom Lendacky 
27163b94509STom Lendacky #endif
272