xref: /openbmc/linux/drivers/crypto/ccp/sp-dev.h (revision 3ddc8b84)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * AMD Secure Processor driver
4  *
5  * Copyright (C) 2017-2019 Advanced Micro Devices, Inc.
6  *
7  * Author: Tom Lendacky <thomas.lendacky@amd.com>
8  * Author: Gary R Hook <gary.hook@amd.com>
9  * Author: Brijesh Singh <brijesh.singh@amd.com>
10  */
11 
12 #ifndef __SP_DEV_H__
13 #define __SP_DEV_H__
14 
15 #include <linux/device.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 #include <linux/interrupt.h>
24 #include <linux/irqreturn.h>
25 
26 #define SP_MAX_NAME_LEN		32
27 
28 #define CACHE_NONE			0x00
29 #define CACHE_WB_NO_ALLOC		0xb7
30 
31 #define PLATFORM_FEATURE_DBC		0x1
32 
33 #define PSP_FEATURE(psp, feat)	(psp->vdata && psp->vdata->platform_features & PLATFORM_FEATURE_##feat)
34 
35 /* Structure to hold CCP device data */
36 struct ccp_device;
37 struct ccp_vdata {
38 	const unsigned int version;
39 	const unsigned int dma_chan_attr;
40 	void (*setup)(struct ccp_device *);
41 	const struct ccp_actions *perform;
42 	const unsigned int offset;
43 	const unsigned int rsamax;
44 };
45 
46 struct sev_vdata {
47 	const unsigned int cmdresp_reg;
48 	const unsigned int cmdbuff_addr_lo_reg;
49 	const unsigned int cmdbuff_addr_hi_reg;
50 };
51 
52 struct tee_vdata {
53 	const unsigned int cmdresp_reg;
54 	const unsigned int cmdbuff_addr_lo_reg;
55 	const unsigned int cmdbuff_addr_hi_reg;
56 	const unsigned int ring_wptr_reg;
57 	const unsigned int ring_rptr_reg;
58 	const unsigned int info_reg;
59 };
60 
61 struct platform_access_vdata {
62 	const unsigned int cmdresp_reg;
63 	const unsigned int cmdbuff_addr_lo_reg;
64 	const unsigned int cmdbuff_addr_hi_reg;
65 	const unsigned int doorbell_button_reg;
66 	const unsigned int doorbell_cmd_reg;
67 
68 };
69 
70 struct psp_vdata {
71 	const struct sev_vdata *sev;
72 	const struct tee_vdata *tee;
73 	const struct platform_access_vdata *platform_access;
74 	const unsigned int feature_reg;
75 	const unsigned int inten_reg;
76 	const unsigned int intsts_reg;
77 	const unsigned int bootloader_info_reg;
78 	const unsigned int platform_features;
79 };
80 
81 /* Structure to hold SP device data */
82 struct sp_dev_vdata {
83 	const unsigned int bar;
84 
85 	const struct ccp_vdata *ccp_vdata;
86 	const struct psp_vdata *psp_vdata;
87 };
88 
89 struct sp_device {
90 	struct list_head entry;
91 
92 	struct device *dev;
93 
94 	struct sp_dev_vdata *dev_vdata;
95 	unsigned int ord;
96 	char name[SP_MAX_NAME_LEN];
97 
98 	/* Bus specific device information */
99 	void *dev_specific;
100 
101 	/* I/O area used for device communication. */
102 	void __iomem *io_map;
103 
104 	/* DMA caching attribute support */
105 	unsigned int axcache;
106 
107 	/* get and set master device */
108 	struct sp_device*(*get_psp_master_device)(void);
109 	void (*set_psp_master_device)(struct sp_device *);
110 	void (*clear_psp_master_device)(struct sp_device *);
111 
112 	bool irq_registered;
113 	bool use_tasklet;
114 
115 	unsigned int ccp_irq;
116 	irq_handler_t ccp_irq_handler;
117 	void *ccp_irq_data;
118 
119 	unsigned int psp_irq;
120 	irq_handler_t psp_irq_handler;
121 	void *psp_irq_data;
122 
123 	void *ccp_data;
124 	void *psp_data;
125 };
126 
127 int sp_pci_init(void);
128 void sp_pci_exit(void);
129 
130 int sp_platform_init(void);
131 void sp_platform_exit(void);
132 
133 struct sp_device *sp_alloc_struct(struct device *dev);
134 
135 int sp_init(struct sp_device *sp);
136 void sp_destroy(struct sp_device *sp);
137 struct sp_device *sp_get_master(void);
138 
139 int sp_suspend(struct sp_device *sp);
140 int sp_resume(struct sp_device *sp);
141 int sp_request_ccp_irq(struct sp_device *sp, irq_handler_t handler,
142 		       const char *name, void *data);
143 void sp_free_ccp_irq(struct sp_device *sp, void *data);
144 int sp_request_psp_irq(struct sp_device *sp, irq_handler_t handler,
145 		       const char *name, void *data);
146 void sp_free_psp_irq(struct sp_device *sp, void *data);
147 struct sp_device *sp_get_psp_master_device(void);
148 
149 #ifdef CONFIG_CRYPTO_DEV_SP_CCP
150 
151 int ccp_dev_init(struct sp_device *sp);
152 void ccp_dev_destroy(struct sp_device *sp);
153 
154 void ccp_dev_suspend(struct sp_device *sp);
155 void ccp_dev_resume(struct sp_device *sp);
156 
157 #else	/* !CONFIG_CRYPTO_DEV_SP_CCP */
158 
159 static inline int ccp_dev_init(struct sp_device *sp)
160 {
161 	return 0;
162 }
163 static inline void ccp_dev_destroy(struct sp_device *sp) { }
164 static inline void ccp_dev_suspend(struct sp_device *sp) { }
165 static inline void ccp_dev_resume(struct sp_device *sp) { }
166 #endif	/* CONFIG_CRYPTO_DEV_SP_CCP */
167 
168 #ifdef CONFIG_CRYPTO_DEV_SP_PSP
169 
170 int psp_dev_init(struct sp_device *sp);
171 void psp_pci_init(void);
172 void psp_dev_destroy(struct sp_device *sp);
173 void psp_pci_exit(void);
174 
175 #else /* !CONFIG_CRYPTO_DEV_SP_PSP */
176 
177 static inline int psp_dev_init(struct sp_device *sp) { return 0; }
178 static inline void psp_pci_init(void) { }
179 static inline void psp_dev_destroy(struct sp_device *sp) { }
180 static inline void psp_pci_exit(void) { }
181 
182 #endif /* CONFIG_CRYPTO_DEV_SP_PSP */
183 
184 #endif
185