xref: /linux/drivers/crypto/ccp/psp-dev.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1b93566f1SRijo Thomas // SPDX-License-Identifier: GPL-2.0-only
2b93566f1SRijo Thomas /*
3b93566f1SRijo Thomas  * AMD Platform Security Processor (PSP) interface
4b93566f1SRijo Thomas  *
5b93566f1SRijo Thomas  * Copyright (C) 2016,2019 Advanced Micro Devices, Inc.
6b93566f1SRijo Thomas  *
7b93566f1SRijo Thomas  * Author: Brijesh Singh <brijesh.singh@amd.com>
8b93566f1SRijo Thomas  */
9b93566f1SRijo Thomas 
10b93566f1SRijo Thomas #include <linux/kernel.h>
11b93566f1SRijo Thomas #include <linux/irqreturn.h>
12949a0c8dSTom Lendacky #include <linux/mutex.h>
13949a0c8dSTom Lendacky #include <linux/bitfield.h>
14949a0c8dSTom Lendacky #include <linux/delay.h>
15b93566f1SRijo Thomas 
16b93566f1SRijo Thomas #include "sp-dev.h"
17b93566f1SRijo Thomas #include "psp-dev.h"
18b93566f1SRijo Thomas #include "sev-dev.h"
1933960accSRijo Thomas #include "tee-dev.h"
207ccc4f4eSMario Limonciello #include "platform-access.h"
21c04cf9e1SMario Limonciello #include "dbc.h"
2256e0d883SMario Limonciello #include "hsti.h"
23b93566f1SRijo Thomas 
24b93566f1SRijo Thomas struct psp_device *psp_master;
25b93566f1SRijo Thomas 
26949a0c8dSTom Lendacky #define PSP_C2PMSG_17_CMDRESP_CMD	GENMASK(19, 16)
27949a0c8dSTom Lendacky 
psp_mailbox_poll(const void __iomem * cmdresp_reg,unsigned int * cmdresp,unsigned int timeout_msecs)28949a0c8dSTom Lendacky static int psp_mailbox_poll(const void __iomem *cmdresp_reg, unsigned int *cmdresp,
29949a0c8dSTom Lendacky 			    unsigned int timeout_msecs)
30949a0c8dSTom Lendacky {
31949a0c8dSTom Lendacky 	while (true) {
32949a0c8dSTom Lendacky 		*cmdresp = ioread32(cmdresp_reg);
33949a0c8dSTom Lendacky 		if (FIELD_GET(PSP_CMDRESP_RESP, *cmdresp))
34949a0c8dSTom Lendacky 			return 0;
35949a0c8dSTom Lendacky 
36949a0c8dSTom Lendacky 		if (!timeout_msecs--)
37949a0c8dSTom Lendacky 			break;
38949a0c8dSTom Lendacky 
39949a0c8dSTom Lendacky 		usleep_range(1000, 1100);
40949a0c8dSTom Lendacky 	}
41949a0c8dSTom Lendacky 
42949a0c8dSTom Lendacky 	return -ETIMEDOUT;
43949a0c8dSTom Lendacky }
44949a0c8dSTom Lendacky 
psp_mailbox_command(struct psp_device * psp,enum psp_cmd cmd,void * cmdbuff,unsigned int timeout_msecs,unsigned int * cmdresp)45949a0c8dSTom Lendacky int psp_mailbox_command(struct psp_device *psp, enum psp_cmd cmd, void *cmdbuff,
46949a0c8dSTom Lendacky 			unsigned int timeout_msecs, unsigned int *cmdresp)
47949a0c8dSTom Lendacky {
48949a0c8dSTom Lendacky 	void __iomem *cmdresp_reg, *cmdbuff_lo_reg, *cmdbuff_hi_reg;
49949a0c8dSTom Lendacky 	int ret;
50949a0c8dSTom Lendacky 
51949a0c8dSTom Lendacky 	if (!psp || !psp->vdata || !psp->vdata->cmdresp_reg ||
52949a0c8dSTom Lendacky 	    !psp->vdata->cmdbuff_addr_lo_reg || !psp->vdata->cmdbuff_addr_hi_reg)
53949a0c8dSTom Lendacky 		return -ENODEV;
54949a0c8dSTom Lendacky 
55949a0c8dSTom Lendacky 	cmdresp_reg    = psp->io_regs + psp->vdata->cmdresp_reg;
56949a0c8dSTom Lendacky 	cmdbuff_lo_reg = psp->io_regs + psp->vdata->cmdbuff_addr_lo_reg;
57949a0c8dSTom Lendacky 	cmdbuff_hi_reg = psp->io_regs + psp->vdata->cmdbuff_addr_hi_reg;
58949a0c8dSTom Lendacky 
59949a0c8dSTom Lendacky 	mutex_lock(&psp->mailbox_mutex);
60949a0c8dSTom Lendacky 
61949a0c8dSTom Lendacky 	/* Ensure mailbox is ready for a command */
62949a0c8dSTom Lendacky 	ret = -EBUSY;
63949a0c8dSTom Lendacky 	if (psp_mailbox_poll(cmdresp_reg, cmdresp, 0))
64949a0c8dSTom Lendacky 		goto unlock;
65949a0c8dSTom Lendacky 
66949a0c8dSTom Lendacky 	if (cmdbuff) {
67949a0c8dSTom Lendacky 		iowrite32(lower_32_bits(__psp_pa(cmdbuff)), cmdbuff_lo_reg);
68949a0c8dSTom Lendacky 		iowrite32(upper_32_bits(__psp_pa(cmdbuff)), cmdbuff_hi_reg);
69949a0c8dSTom Lendacky 	}
70949a0c8dSTom Lendacky 
71949a0c8dSTom Lendacky 	*cmdresp = FIELD_PREP(PSP_C2PMSG_17_CMDRESP_CMD, cmd);
72949a0c8dSTom Lendacky 	iowrite32(*cmdresp, cmdresp_reg);
73949a0c8dSTom Lendacky 
74949a0c8dSTom Lendacky 	ret = psp_mailbox_poll(cmdresp_reg, cmdresp, timeout_msecs);
75949a0c8dSTom Lendacky 
76949a0c8dSTom Lendacky unlock:
77949a0c8dSTom Lendacky 	mutex_unlock(&psp->mailbox_mutex);
78949a0c8dSTom Lendacky 
79949a0c8dSTom Lendacky 	return ret;
80949a0c8dSTom Lendacky }
81949a0c8dSTom Lendacky 
psp_extended_mailbox_cmd(struct psp_device * psp,unsigned int timeout_msecs,struct psp_ext_request * req)826e17375cSMario Limonciello int psp_extended_mailbox_cmd(struct psp_device *psp, unsigned int timeout_msecs,
836e17375cSMario Limonciello 			     struct psp_ext_request *req)
846e17375cSMario Limonciello {
856e17375cSMario Limonciello 	unsigned int reg;
866e17375cSMario Limonciello 	int ret;
876e17375cSMario Limonciello 
886e17375cSMario Limonciello 	print_hex_dump_debug("->psp ", DUMP_PREFIX_OFFSET, 16, 2, req,
896e17375cSMario Limonciello 			     req->header.payload_size, false);
906e17375cSMario Limonciello 
916e17375cSMario Limonciello 	ret = psp_mailbox_command(psp, PSP_CMD_TEE_EXTENDED_CMD, (void *)req,
926e17375cSMario Limonciello 				  timeout_msecs, &reg);
936e17375cSMario Limonciello 	if (ret) {
946e17375cSMario Limonciello 		return ret;
956e17375cSMario Limonciello 	} else if (FIELD_GET(PSP_CMDRESP_STS, reg)) {
966e17375cSMario Limonciello 		req->header.status = FIELD_GET(PSP_CMDRESP_STS, reg);
976e17375cSMario Limonciello 		return -EIO;
986e17375cSMario Limonciello 	}
996e17375cSMario Limonciello 
1006e17375cSMario Limonciello 	print_hex_dump_debug("<-psp ", DUMP_PREFIX_OFFSET, 16, 2, req,
1016e17375cSMario Limonciello 			     req->header.payload_size, false);
1026e17375cSMario Limonciello 
1036e17375cSMario Limonciello 	return 0;
1046e17375cSMario Limonciello }
1056e17375cSMario Limonciello 
psp_alloc_struct(struct sp_device * sp)106b93566f1SRijo Thomas static struct psp_device *psp_alloc_struct(struct sp_device *sp)
107b93566f1SRijo Thomas {
108b93566f1SRijo Thomas 	struct device *dev = sp->dev;
109b93566f1SRijo Thomas 	struct psp_device *psp;
110b93566f1SRijo Thomas 
111b93566f1SRijo Thomas 	psp = devm_kzalloc(dev, sizeof(*psp), GFP_KERNEL);
112b93566f1SRijo Thomas 	if (!psp)
113b93566f1SRijo Thomas 		return NULL;
114b93566f1SRijo Thomas 
115b93566f1SRijo Thomas 	psp->dev = dev;
116b93566f1SRijo Thomas 	psp->sp = sp;
117b93566f1SRijo Thomas 
118b93566f1SRijo Thomas 	snprintf(psp->name, sizeof(psp->name), "psp-%u", sp->ord);
119b93566f1SRijo Thomas 
120b93566f1SRijo Thomas 	return psp;
121b93566f1SRijo Thomas }
122b93566f1SRijo Thomas 
psp_irq_handler(int irq,void * data)123b93566f1SRijo Thomas static irqreturn_t psp_irq_handler(int irq, void *data)
124b93566f1SRijo Thomas {
125b93566f1SRijo Thomas 	struct psp_device *psp = data;
126b93566f1SRijo Thomas 	unsigned int status;
127b93566f1SRijo Thomas 
128b93566f1SRijo Thomas 	/* Read the interrupt status: */
129b93566f1SRijo Thomas 	status = ioread32(psp->io_regs + psp->vdata->intsts_reg);
130b93566f1SRijo Thomas 
13145121ad4SJeremi Piotrowski 	/* Clear the interrupt status by writing the same value we read. */
13245121ad4SJeremi Piotrowski 	iowrite32(status, psp->io_regs + psp->vdata->intsts_reg);
13345121ad4SJeremi Piotrowski 
134b93566f1SRijo Thomas 	/* invoke subdevice interrupt handlers */
135b93566f1SRijo Thomas 	if (status) {
136b93566f1SRijo Thomas 		if (psp->sev_irq_handler)
137b93566f1SRijo Thomas 			psp->sev_irq_handler(irq, psp->sev_irq_data, status);
138b93566f1SRijo Thomas 	}
139b93566f1SRijo Thomas 
140b93566f1SRijo Thomas 	return IRQ_HANDLED;
141b93566f1SRijo Thomas }
142b93566f1SRijo Thomas 
psp_get_capability(struct psp_device * psp)143f100ab62SRijo Thomas static unsigned int psp_get_capability(struct psp_device *psp)
144b93566f1SRijo Thomas {
145b93566f1SRijo Thomas 	unsigned int val = ioread32(psp->io_regs + psp->vdata->feature_reg);
146b93566f1SRijo Thomas 
147b93566f1SRijo Thomas 	/*
148b93566f1SRijo Thomas 	 * Check for a access to the registers.  If this read returns
149b93566f1SRijo Thomas 	 * 0xffffffff, it's likely that the system is running a broken
150b93566f1SRijo Thomas 	 * BIOS which disallows access to the device. Stop here and
151b93566f1SRijo Thomas 	 * fail the PSP initialization (but not the load, as the CCP
152b93566f1SRijo Thomas 	 * could get properly initialized).
153b93566f1SRijo Thomas 	 */
154b93566f1SRijo Thomas 	if (val == 0xffffffff) {
155b93566f1SRijo Thomas 		dev_notice(psp->dev, "psp: unable to access the device: you might be running a broken BIOS.\n");
156cac32cd4SMario Limonciello 		return -ENODEV;
157cac32cd4SMario Limonciello 	}
1588609dd25SMario Limonciello 	psp->capability.raw = val;
159cac32cd4SMario Limonciello 
160f100ab62SRijo Thomas 	return 0;
161b93566f1SRijo Thomas }
162b93566f1SRijo Thomas 
psp_check_sev_support(struct psp_device * psp)163cac32cd4SMario Limonciello static int psp_check_sev_support(struct psp_device *psp)
164f100ab62SRijo Thomas {
165f100ab62SRijo Thomas 	/* Check if device supports SEV feature */
1668609dd25SMario Limonciello 	if (!psp->capability.sev) {
167b93566f1SRijo Thomas 		dev_dbg(psp->dev, "psp does not support SEV\n");
168b93566f1SRijo Thomas 		return -ENODEV;
169b93566f1SRijo Thomas 	}
170b93566f1SRijo Thomas 
171b93566f1SRijo Thomas 	return 0;
172b93566f1SRijo Thomas }
173b93566f1SRijo Thomas 
psp_check_tee_support(struct psp_device * psp)174cac32cd4SMario Limonciello static int psp_check_tee_support(struct psp_device *psp)
175f100ab62SRijo Thomas {
176f100ab62SRijo Thomas 	/* Check if device supports TEE feature */
1778609dd25SMario Limonciello 	if (!psp->capability.tee) {
178f100ab62SRijo Thomas 		dev_dbg(psp->dev, "psp does not support TEE\n");
179f100ab62SRijo Thomas 		return -ENODEV;
180f100ab62SRijo Thomas 	}
181f100ab62SRijo Thomas 
182f100ab62SRijo Thomas 	return 0;
183f100ab62SRijo Thomas }
184f100ab62SRijo Thomas 
psp_init(struct psp_device * psp)185cac32cd4SMario Limonciello static int psp_init(struct psp_device *psp)
18633960accSRijo Thomas {
18733960accSRijo Thomas 	int ret;
18833960accSRijo Thomas 
189cac32cd4SMario Limonciello 	if (!psp_check_sev_support(psp)) {
19033960accSRijo Thomas 		ret = sev_dev_init(psp);
19133960accSRijo Thomas 		if (ret)
19233960accSRijo Thomas 			return ret;
19333960accSRijo Thomas 	}
19433960accSRijo Thomas 
195cac32cd4SMario Limonciello 	if (!psp_check_tee_support(psp)) {
19633960accSRijo Thomas 		ret = tee_dev_init(psp);
19733960accSRijo Thomas 		if (ret)
19833960accSRijo Thomas 			return ret;
19933960accSRijo Thomas 	}
20033960accSRijo Thomas 
2010470bb1bSMario Limonciello 	if (psp->vdata->platform_access) {
2020470bb1bSMario Limonciello 		ret = platform_access_dev_init(psp);
2030470bb1bSMario Limonciello 		if (ret)
2040470bb1bSMario Limonciello 			return ret;
2050470bb1bSMario Limonciello 	}
2060470bb1bSMario Limonciello 
2070470bb1bSMario Limonciello 	/* dbc must come after platform access as it tests the feature */
2080470bb1bSMario Limonciello 	if (PSP_FEATURE(psp, DBC) ||
2098609dd25SMario Limonciello 	    psp->capability.dbc_thru_ext) {
2100470bb1bSMario Limonciello 		ret = dbc_dev_init(psp);
2110470bb1bSMario Limonciello 		if (ret)
2120470bb1bSMario Limonciello 			return ret;
2130470bb1bSMario Limonciello 	}
2147ccc4f4eSMario Limonciello 
215*82f9327fSMario Limonciello 	/* HSTI uses platform access on some systems. */
216*82f9327fSMario Limonciello 	ret = psp_init_hsti(psp);
217*82f9327fSMario Limonciello 	if (ret)
218*82f9327fSMario Limonciello 		return ret;
219*82f9327fSMario Limonciello 
22033960accSRijo Thomas 	return 0;
22133960accSRijo Thomas }
22233960accSRijo Thomas 
psp_dev_init(struct sp_device * sp)223b93566f1SRijo Thomas int psp_dev_init(struct sp_device *sp)
224b93566f1SRijo Thomas {
225b93566f1SRijo Thomas 	struct device *dev = sp->dev;
226b93566f1SRijo Thomas 	struct psp_device *psp;
227b93566f1SRijo Thomas 	int ret;
228b93566f1SRijo Thomas 
229b93566f1SRijo Thomas 	ret = -ENOMEM;
230b93566f1SRijo Thomas 	psp = psp_alloc_struct(sp);
231b93566f1SRijo Thomas 	if (!psp)
232b93566f1SRijo Thomas 		goto e_err;
233b93566f1SRijo Thomas 
234b93566f1SRijo Thomas 	sp->psp_data = psp;
235b93566f1SRijo Thomas 
236b93566f1SRijo Thomas 	psp->vdata = (struct psp_vdata *)sp->dev_vdata->psp_vdata;
237b93566f1SRijo Thomas 	if (!psp->vdata) {
238b93566f1SRijo Thomas 		ret = -ENODEV;
239b93566f1SRijo Thomas 		dev_err(dev, "missing driver data\n");
240b93566f1SRijo Thomas 		goto e_err;
241b93566f1SRijo Thomas 	}
242b93566f1SRijo Thomas 
243b93566f1SRijo Thomas 	psp->io_regs = sp->io_map;
244949a0c8dSTom Lendacky 	mutex_init(&psp->mailbox_mutex);
245b93566f1SRijo Thomas 
246cac32cd4SMario Limonciello 	ret = psp_get_capability(psp);
247cac32cd4SMario Limonciello 	if (ret)
248f100ab62SRijo Thomas 		goto e_disable;
249f100ab62SRijo Thomas 
250b93566f1SRijo Thomas 	/* Disable and clear interrupts until ready */
251b93566f1SRijo Thomas 	iowrite32(0, psp->io_regs + psp->vdata->inten_reg);
252b93566f1SRijo Thomas 	iowrite32(-1, psp->io_regs + psp->vdata->intsts_reg);
253b93566f1SRijo Thomas 
254b93566f1SRijo Thomas 	/* Request an irq */
255b93566f1SRijo Thomas 	ret = sp_request_psp_irq(psp->sp, psp_irq_handler, psp->name, psp);
256b93566f1SRijo Thomas 	if (ret) {
257b93566f1SRijo Thomas 		dev_err(dev, "psp: unable to allocate an IRQ\n");
258b93566f1SRijo Thomas 		goto e_err;
259b93566f1SRijo Thomas 	}
260b93566f1SRijo Thomas 
261b8440d55SMario Limonciello 	/* master device must be set for platform access */
262b8440d55SMario Limonciello 	if (psp->sp->set_psp_master_device)
263b8440d55SMario Limonciello 		psp->sp->set_psp_master_device(psp->sp);
264b8440d55SMario Limonciello 
265cac32cd4SMario Limonciello 	ret = psp_init(psp);
266b93566f1SRijo Thomas 	if (ret)
267b93566f1SRijo Thomas 		goto e_irq;
268b93566f1SRijo Thomas 
269b93566f1SRijo Thomas 	/* Enable interrupt */
270b93566f1SRijo Thomas 	iowrite32(-1, psp->io_regs + psp->vdata->inten_reg);
271b93566f1SRijo Thomas 
272b93566f1SRijo Thomas 	dev_notice(dev, "psp enabled\n");
273b93566f1SRijo Thomas 
274b93566f1SRijo Thomas 	return 0;
275b93566f1SRijo Thomas 
276b93566f1SRijo Thomas e_irq:
277b8440d55SMario Limonciello 	if (sp->clear_psp_master_device)
278b8440d55SMario Limonciello 		sp->clear_psp_master_device(sp);
279b8440d55SMario Limonciello 
280b93566f1SRijo Thomas 	sp_free_psp_irq(psp->sp, psp);
281b93566f1SRijo Thomas e_err:
282b93566f1SRijo Thomas 	sp->psp_data = NULL;
283b93566f1SRijo Thomas 
284b93566f1SRijo Thomas 	dev_notice(dev, "psp initialization failed\n");
285b93566f1SRijo Thomas 
286b93566f1SRijo Thomas 	return ret;
287b93566f1SRijo Thomas 
288b93566f1SRijo Thomas e_disable:
289b93566f1SRijo Thomas 	sp->psp_data = NULL;
290b93566f1SRijo Thomas 
291b93566f1SRijo Thomas 	return ret;
292b93566f1SRijo Thomas }
293b93566f1SRijo Thomas 
psp_dev_destroy(struct sp_device * sp)294b93566f1SRijo Thomas void psp_dev_destroy(struct sp_device *sp)
295b93566f1SRijo Thomas {
296b93566f1SRijo Thomas 	struct psp_device *psp = sp->psp_data;
297b93566f1SRijo Thomas 
298b93566f1SRijo Thomas 	if (!psp)
299b93566f1SRijo Thomas 		return;
300b93566f1SRijo Thomas 
301b93566f1SRijo Thomas 	sev_dev_destroy(psp);
302b93566f1SRijo Thomas 
30333960accSRijo Thomas 	tee_dev_destroy(psp);
30433960accSRijo Thomas 
305c04cf9e1SMario Limonciello 	dbc_dev_destroy(psp);
306c04cf9e1SMario Limonciello 
3077ccc4f4eSMario Limonciello 	platform_access_dev_destroy(psp);
3087ccc4f4eSMario Limonciello 
309b93566f1SRijo Thomas 	sp_free_psp_irq(sp, psp);
31015f7a4c6SJohn Allen 
31115f7a4c6SJohn Allen 	if (sp->clear_psp_master_device)
31215f7a4c6SJohn Allen 		sp->clear_psp_master_device(sp);
313b93566f1SRijo Thomas }
314b93566f1SRijo Thomas 
psp_set_sev_irq_handler(struct psp_device * psp,psp_irq_handler_t handler,void * data)315b93566f1SRijo Thomas void psp_set_sev_irq_handler(struct psp_device *psp, psp_irq_handler_t handler,
316b93566f1SRijo Thomas 			     void *data)
317b93566f1SRijo Thomas {
318b93566f1SRijo Thomas 	psp->sev_irq_data = data;
319b93566f1SRijo Thomas 	psp->sev_irq_handler = handler;
320b93566f1SRijo Thomas }
321b93566f1SRijo Thomas 
psp_clear_sev_irq_handler(struct psp_device * psp)322b93566f1SRijo Thomas void psp_clear_sev_irq_handler(struct psp_device *psp)
323b93566f1SRijo Thomas {
324b93566f1SRijo Thomas 	psp_set_sev_irq_handler(psp, NULL, NULL);
325b93566f1SRijo Thomas }
326b93566f1SRijo Thomas 
psp_get_master_device(void)327b93566f1SRijo Thomas struct psp_device *psp_get_master_device(void)
328b93566f1SRijo Thomas {
329b93566f1SRijo Thomas 	struct sp_device *sp = sp_get_psp_master_device();
330b93566f1SRijo Thomas 
331b93566f1SRijo Thomas 	return sp ? sp->psp_data : NULL;
332b93566f1SRijo Thomas }
333b93566f1SRijo Thomas 
psp_pci_init(void)334b93566f1SRijo Thomas void psp_pci_init(void)
335b93566f1SRijo Thomas {
336b93566f1SRijo Thomas 	psp_master = psp_get_master_device();
337b93566f1SRijo Thomas 
338b93566f1SRijo Thomas 	if (!psp_master)
339b93566f1SRijo Thomas 		return;
340b93566f1SRijo Thomas 
341b93566f1SRijo Thomas 	sev_pci_init();
342b93566f1SRijo Thomas }
343b93566f1SRijo Thomas 
psp_pci_exit(void)344b93566f1SRijo Thomas void psp_pci_exit(void)
345b93566f1SRijo Thomas {
346b93566f1SRijo Thomas 	if (!psp_master)
347b93566f1SRijo Thomas 		return;
348b93566f1SRijo Thomas 
349b93566f1SRijo Thomas 	sev_pci_exit();
350b93566f1SRijo Thomas }
351