xref: /linux-6.15/drivers/crypto/ccp/psp-dev.c (revision 949a0c8d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * AMD Platform Security Processor (PSP) interface
4  *
5  * Copyright (C) 2016,2019 Advanced Micro Devices, Inc.
6  *
7  * Author: Brijesh Singh <[email protected]>
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/irqreturn.h>
12 #include <linux/mutex.h>
13 #include <linux/bitfield.h>
14 #include <linux/delay.h>
15 
16 #include "sp-dev.h"
17 #include "psp-dev.h"
18 #include "sev-dev.h"
19 #include "tee-dev.h"
20 #include "platform-access.h"
21 #include "dbc.h"
22 
23 struct psp_device *psp_master;
24 
25 #define PSP_C2PMSG_17_CMDRESP_CMD	GENMASK(19, 16)
26 
27 static int psp_mailbox_poll(const void __iomem *cmdresp_reg, unsigned int *cmdresp,
28 			    unsigned int timeout_msecs)
29 {
30 	while (true) {
31 		*cmdresp = ioread32(cmdresp_reg);
32 		if (FIELD_GET(PSP_CMDRESP_RESP, *cmdresp))
33 			return 0;
34 
35 		if (!timeout_msecs--)
36 			break;
37 
38 		usleep_range(1000, 1100);
39 	}
40 
41 	return -ETIMEDOUT;
42 }
43 
44 int psp_mailbox_command(struct psp_device *psp, enum psp_cmd cmd, void *cmdbuff,
45 			unsigned int timeout_msecs, unsigned int *cmdresp)
46 {
47 	void __iomem *cmdresp_reg, *cmdbuff_lo_reg, *cmdbuff_hi_reg;
48 	int ret;
49 
50 	if (!psp || !psp->vdata || !psp->vdata->cmdresp_reg ||
51 	    !psp->vdata->cmdbuff_addr_lo_reg || !psp->vdata->cmdbuff_addr_hi_reg)
52 		return -ENODEV;
53 
54 	cmdresp_reg    = psp->io_regs + psp->vdata->cmdresp_reg;
55 	cmdbuff_lo_reg = psp->io_regs + psp->vdata->cmdbuff_addr_lo_reg;
56 	cmdbuff_hi_reg = psp->io_regs + psp->vdata->cmdbuff_addr_hi_reg;
57 
58 	mutex_lock(&psp->mailbox_mutex);
59 
60 	/* Ensure mailbox is ready for a command */
61 	ret = -EBUSY;
62 	if (psp_mailbox_poll(cmdresp_reg, cmdresp, 0))
63 		goto unlock;
64 
65 	if (cmdbuff) {
66 		iowrite32(lower_32_bits(__psp_pa(cmdbuff)), cmdbuff_lo_reg);
67 		iowrite32(upper_32_bits(__psp_pa(cmdbuff)), cmdbuff_hi_reg);
68 	}
69 
70 	*cmdresp = FIELD_PREP(PSP_C2PMSG_17_CMDRESP_CMD, cmd);
71 	iowrite32(*cmdresp, cmdresp_reg);
72 
73 	ret = psp_mailbox_poll(cmdresp_reg, cmdresp, timeout_msecs);
74 
75 unlock:
76 	mutex_unlock(&psp->mailbox_mutex);
77 
78 	return ret;
79 }
80 
81 static struct psp_device *psp_alloc_struct(struct sp_device *sp)
82 {
83 	struct device *dev = sp->dev;
84 	struct psp_device *psp;
85 
86 	psp = devm_kzalloc(dev, sizeof(*psp), GFP_KERNEL);
87 	if (!psp)
88 		return NULL;
89 
90 	psp->dev = dev;
91 	psp->sp = sp;
92 
93 	snprintf(psp->name, sizeof(psp->name), "psp-%u", sp->ord);
94 
95 	return psp;
96 }
97 
98 static irqreturn_t psp_irq_handler(int irq, void *data)
99 {
100 	struct psp_device *psp = data;
101 	unsigned int status;
102 
103 	/* Read the interrupt status: */
104 	status = ioread32(psp->io_regs + psp->vdata->intsts_reg);
105 
106 	/* Clear the interrupt status by writing the same value we read. */
107 	iowrite32(status, psp->io_regs + psp->vdata->intsts_reg);
108 
109 	/* invoke subdevice interrupt handlers */
110 	if (status) {
111 		if (psp->sev_irq_handler)
112 			psp->sev_irq_handler(irq, psp->sev_irq_data, status);
113 	}
114 
115 	return IRQ_HANDLED;
116 }
117 
118 static unsigned int psp_get_capability(struct psp_device *psp)
119 {
120 	unsigned int val = ioread32(psp->io_regs + psp->vdata->feature_reg);
121 
122 	/*
123 	 * Check for a access to the registers.  If this read returns
124 	 * 0xffffffff, it's likely that the system is running a broken
125 	 * BIOS which disallows access to the device. Stop here and
126 	 * fail the PSP initialization (but not the load, as the CCP
127 	 * could get properly initialized).
128 	 */
129 	if (val == 0xffffffff) {
130 		dev_notice(psp->dev, "psp: unable to access the device: you might be running a broken BIOS.\n");
131 		return -ENODEV;
132 	}
133 	psp->capability = val;
134 
135 	/* Detect if TSME and SME are both enabled */
136 	if (psp->capability & PSP_CAPABILITY_PSP_SECURITY_REPORTING &&
137 	    psp->capability & (PSP_SECURITY_TSME_STATUS << PSP_CAPABILITY_PSP_SECURITY_OFFSET) &&
138 	    cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
139 		dev_notice(psp->dev, "psp: Both TSME and SME are active, SME is unnecessary when TSME is active.\n");
140 
141 	return 0;
142 }
143 
144 static int psp_check_sev_support(struct psp_device *psp)
145 {
146 	/* Check if device supports SEV feature */
147 	if (!(psp->capability & PSP_CAPABILITY_SEV)) {
148 		dev_dbg(psp->dev, "psp does not support SEV\n");
149 		return -ENODEV;
150 	}
151 
152 	return 0;
153 }
154 
155 static int psp_check_tee_support(struct psp_device *psp)
156 {
157 	/* Check if device supports TEE feature */
158 	if (!(psp->capability & PSP_CAPABILITY_TEE)) {
159 		dev_dbg(psp->dev, "psp does not support TEE\n");
160 		return -ENODEV;
161 	}
162 
163 	return 0;
164 }
165 
166 static void psp_init_platform_access(struct psp_device *psp)
167 {
168 	int ret;
169 
170 	ret = platform_access_dev_init(psp);
171 	if (ret) {
172 		dev_warn(psp->dev, "platform access init failed: %d\n", ret);
173 		return;
174 	}
175 
176 	/* dbc must come after platform access as it tests the feature */
177 	ret = dbc_dev_init(psp);
178 	if (ret)
179 		dev_warn(psp->dev, "failed to init dynamic boost control: %d\n",
180 			 ret);
181 }
182 
183 static int psp_init(struct psp_device *psp)
184 {
185 	int ret;
186 
187 	if (!psp_check_sev_support(psp)) {
188 		ret = sev_dev_init(psp);
189 		if (ret)
190 			return ret;
191 	}
192 
193 	if (!psp_check_tee_support(psp)) {
194 		ret = tee_dev_init(psp);
195 		if (ret)
196 			return ret;
197 	}
198 
199 	if (psp->vdata->platform_access)
200 		psp_init_platform_access(psp);
201 
202 	return 0;
203 }
204 
205 int psp_dev_init(struct sp_device *sp)
206 {
207 	struct device *dev = sp->dev;
208 	struct psp_device *psp;
209 	int ret;
210 
211 	ret = -ENOMEM;
212 	psp = psp_alloc_struct(sp);
213 	if (!psp)
214 		goto e_err;
215 
216 	sp->psp_data = psp;
217 
218 	psp->vdata = (struct psp_vdata *)sp->dev_vdata->psp_vdata;
219 	if (!psp->vdata) {
220 		ret = -ENODEV;
221 		dev_err(dev, "missing driver data\n");
222 		goto e_err;
223 	}
224 
225 	psp->io_regs = sp->io_map;
226 	mutex_init(&psp->mailbox_mutex);
227 
228 	ret = psp_get_capability(psp);
229 	if (ret)
230 		goto e_disable;
231 
232 	/* Disable and clear interrupts until ready */
233 	iowrite32(0, psp->io_regs + psp->vdata->inten_reg);
234 	iowrite32(-1, psp->io_regs + psp->vdata->intsts_reg);
235 
236 	/* Request an irq */
237 	ret = sp_request_psp_irq(psp->sp, psp_irq_handler, psp->name, psp);
238 	if (ret) {
239 		dev_err(dev, "psp: unable to allocate an IRQ\n");
240 		goto e_err;
241 	}
242 
243 	/* master device must be set for platform access */
244 	if (psp->sp->set_psp_master_device)
245 		psp->sp->set_psp_master_device(psp->sp);
246 
247 	ret = psp_init(psp);
248 	if (ret)
249 		goto e_irq;
250 
251 	/* Enable interrupt */
252 	iowrite32(-1, psp->io_regs + psp->vdata->inten_reg);
253 
254 	dev_notice(dev, "psp enabled\n");
255 
256 	return 0;
257 
258 e_irq:
259 	if (sp->clear_psp_master_device)
260 		sp->clear_psp_master_device(sp);
261 
262 	sp_free_psp_irq(psp->sp, psp);
263 e_err:
264 	sp->psp_data = NULL;
265 
266 	dev_notice(dev, "psp initialization failed\n");
267 
268 	return ret;
269 
270 e_disable:
271 	sp->psp_data = NULL;
272 
273 	return ret;
274 }
275 
276 void psp_dev_destroy(struct sp_device *sp)
277 {
278 	struct psp_device *psp = sp->psp_data;
279 
280 	if (!psp)
281 		return;
282 
283 	sev_dev_destroy(psp);
284 
285 	tee_dev_destroy(psp);
286 
287 	dbc_dev_destroy(psp);
288 
289 	platform_access_dev_destroy(psp);
290 
291 	sp_free_psp_irq(sp, psp);
292 
293 	if (sp->clear_psp_master_device)
294 		sp->clear_psp_master_device(sp);
295 }
296 
297 void psp_set_sev_irq_handler(struct psp_device *psp, psp_irq_handler_t handler,
298 			     void *data)
299 {
300 	psp->sev_irq_data = data;
301 	psp->sev_irq_handler = handler;
302 }
303 
304 void psp_clear_sev_irq_handler(struct psp_device *psp)
305 {
306 	psp_set_sev_irq_handler(psp, NULL, NULL);
307 }
308 
309 struct psp_device *psp_get_master_device(void)
310 {
311 	struct sp_device *sp = sp_get_psp_master_device();
312 
313 	return sp ? sp->psp_data : NULL;
314 }
315 
316 void psp_pci_init(void)
317 {
318 	psp_master = psp_get_master_device();
319 
320 	if (!psp_master)
321 		return;
322 
323 	sev_pci_init();
324 }
325 
326 void psp_pci_exit(void)
327 {
328 	if (!psp_master)
329 		return;
330 
331 	sev_pci_exit();
332 }
333