1 // SPDX-License-Identifier: GPL-2.0-only
3 * AMD Platform Security Processor (PSP) interface
5 * Copyright (C) 2016,2019 Advanced Micro Devices, Inc.
7 * Author: Brijesh Singh <brijesh.singh@amd.com>
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>
20 #include "platform-access.h"
24 struct psp_device
*psp_master
;
26 #define PSP_C2PMSG_17_CMDRESP_CMD GENMASK(19, 16)
28 static int psp_mailbox_poll(const void __iomem
*cmdresp_reg
, unsigned int *cmdresp
,
29 unsigned int timeout_msecs
)
32 *cmdresp
= ioread32(cmdresp_reg
);
33 if (FIELD_GET(PSP_CMDRESP_RESP
, *cmdresp
))
39 usleep_range(1000, 1100);
45 int psp_mailbox_command(struct psp_device
*psp
, enum psp_cmd cmd
, void *cmdbuff
,
46 unsigned int timeout_msecs
, unsigned int *cmdresp
)
48 void __iomem
*cmdresp_reg
, *cmdbuff_lo_reg
, *cmdbuff_hi_reg
;
51 if (!psp
|| !psp
->vdata
|| !psp
->vdata
->cmdresp_reg
||
52 !psp
->vdata
->cmdbuff_addr_lo_reg
|| !psp
->vdata
->cmdbuff_addr_hi_reg
)
55 cmdresp_reg
= psp
->io_regs
+ psp
->vdata
->cmdresp_reg
;
56 cmdbuff_lo_reg
= psp
->io_regs
+ psp
->vdata
->cmdbuff_addr_lo_reg
;
57 cmdbuff_hi_reg
= psp
->io_regs
+ psp
->vdata
->cmdbuff_addr_hi_reg
;
59 mutex_lock(&psp
->mailbox_mutex
);
61 /* Ensure mailbox is ready for a command */
63 if (psp_mailbox_poll(cmdresp_reg
, cmdresp
, 0))
67 iowrite32(lower_32_bits(__psp_pa(cmdbuff
)), cmdbuff_lo_reg
);
68 iowrite32(upper_32_bits(__psp_pa(cmdbuff
)), cmdbuff_hi_reg
);
71 *cmdresp
= FIELD_PREP(PSP_C2PMSG_17_CMDRESP_CMD
, cmd
);
72 iowrite32(*cmdresp
, cmdresp_reg
);
74 ret
= psp_mailbox_poll(cmdresp_reg
, cmdresp
, timeout_msecs
);
77 mutex_unlock(&psp
->mailbox_mutex
);
82 int psp_extended_mailbox_cmd(struct psp_device
*psp
, unsigned int timeout_msecs
,
83 struct psp_ext_request
*req
)
88 print_hex_dump_debug("->psp ", DUMP_PREFIX_OFFSET
, 16, 2, req
,
89 req
->header
.payload_size
, false);
91 ret
= psp_mailbox_command(psp
, PSP_CMD_TEE_EXTENDED_CMD
, (void *)req
,
95 } else if (FIELD_GET(PSP_CMDRESP_STS
, reg
)) {
96 req
->header
.status
= FIELD_GET(PSP_CMDRESP_STS
, reg
);
100 print_hex_dump_debug("<-psp ", DUMP_PREFIX_OFFSET
, 16, 2, req
,
101 req
->header
.payload_size
, false);
106 static struct psp_device
*psp_alloc_struct(struct sp_device
*sp
)
108 struct device
*dev
= sp
->dev
;
109 struct psp_device
*psp
;
111 psp
= devm_kzalloc(dev
, sizeof(*psp
), GFP_KERNEL
);
118 snprintf(psp
->name
, sizeof(psp
->name
), "psp-%u", sp
->ord
);
123 static irqreturn_t
psp_irq_handler(int irq
, void *data
)
125 struct psp_device
*psp
= data
;
128 /* Read the interrupt status: */
129 status
= ioread32(psp
->io_regs
+ psp
->vdata
->intsts_reg
);
131 /* Clear the interrupt status by writing the same value we read. */
132 iowrite32(status
, psp
->io_regs
+ psp
->vdata
->intsts_reg
);
134 /* invoke subdevice interrupt handlers */
136 if (psp
->sev_irq_handler
)
137 psp
->sev_irq_handler(irq
, psp
->sev_irq_data
, status
);
143 static unsigned int psp_get_capability(struct psp_device
*psp
)
145 unsigned int val
= ioread32(psp
->io_regs
+ psp
->vdata
->feature_reg
);
148 * Check for a access to the registers. If this read returns
149 * 0xffffffff, it's likely that the system is running a broken
150 * BIOS which disallows access to the device. Stop here and
151 * fail the PSP initialization (but not the load, as the CCP
152 * could get properly initialized).
154 if (val
== 0xffffffff) {
155 dev_notice(psp
->dev
, "psp: unable to access the device: you might be running a broken BIOS.\n");
158 psp
->capability
.raw
= val
;
163 static int psp_check_sev_support(struct psp_device
*psp
)
165 /* Check if device supports SEV feature */
166 if (!psp
->capability
.sev
) {
167 dev_dbg(psp
->dev
, "psp does not support SEV\n");
174 static int psp_check_tee_support(struct psp_device
*psp
)
176 /* Check if device supports TEE feature */
177 if (!psp
->capability
.tee
) {
178 dev_dbg(psp
->dev
, "psp does not support TEE\n");
185 static int psp_init(struct psp_device
*psp
)
189 if (!psp_check_sev_support(psp
)) {
190 ret
= sev_dev_init(psp
);
195 if (!psp_check_tee_support(psp
)) {
196 ret
= tee_dev_init(psp
);
201 if (psp
->vdata
->platform_access
) {
202 ret
= platform_access_dev_init(psp
);
207 /* dbc must come after platform access as it tests the feature */
208 if (PSP_FEATURE(psp
, DBC
) ||
209 psp
->capability
.dbc_thru_ext
) {
210 ret
= dbc_dev_init(psp
);
215 /* HSTI uses platform access on some systems. */
216 ret
= psp_init_hsti(psp
);
223 int psp_dev_init(struct sp_device
*sp
)
225 struct device
*dev
= sp
->dev
;
226 struct psp_device
*psp
;
230 psp
= psp_alloc_struct(sp
);
236 psp
->vdata
= (struct psp_vdata
*)sp
->dev_vdata
->psp_vdata
;
239 dev_err(dev
, "missing driver data\n");
243 psp
->io_regs
= sp
->io_map
;
244 mutex_init(&psp
->mailbox_mutex
);
246 ret
= psp_get_capability(psp
);
250 /* Disable and clear interrupts until ready */
251 iowrite32(0, psp
->io_regs
+ psp
->vdata
->inten_reg
);
252 iowrite32(-1, psp
->io_regs
+ psp
->vdata
->intsts_reg
);
255 ret
= sp_request_psp_irq(psp
->sp
, psp_irq_handler
, psp
->name
, psp
);
257 dev_err(dev
, "psp: unable to allocate an IRQ\n");
261 /* master device must be set for platform access */
262 if (psp
->sp
->set_psp_master_device
)
263 psp
->sp
->set_psp_master_device(psp
->sp
);
269 /* Enable interrupt */
270 iowrite32(-1, psp
->io_regs
+ psp
->vdata
->inten_reg
);
272 dev_notice(dev
, "psp enabled\n");
277 if (sp
->clear_psp_master_device
)
278 sp
->clear_psp_master_device(sp
);
280 sp_free_psp_irq(psp
->sp
, psp
);
284 dev_notice(dev
, "psp initialization failed\n");
294 void psp_dev_destroy(struct sp_device
*sp
)
296 struct psp_device
*psp
= sp
->psp_data
;
301 sev_dev_destroy(psp
);
303 tee_dev_destroy(psp
);
305 dbc_dev_destroy(psp
);
307 platform_access_dev_destroy(psp
);
309 sp_free_psp_irq(sp
, psp
);
311 if (sp
->clear_psp_master_device
)
312 sp
->clear_psp_master_device(sp
);
315 void psp_set_sev_irq_handler(struct psp_device
*psp
, psp_irq_handler_t handler
,
318 psp
->sev_irq_data
= data
;
319 psp
->sev_irq_handler
= handler
;
322 void psp_clear_sev_irq_handler(struct psp_device
*psp
)
324 psp_set_sev_irq_handler(psp
, NULL
, NULL
);
327 struct psp_device
*psp_get_master_device(void)
329 struct sp_device
*sp
= sp_get_psp_master_device();
331 return sp
? sp
->psp_data
: NULL
;
334 void psp_pci_init(void)
336 psp_master
= psp_get_master_device();
344 void psp_pci_exit(void)