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>
18 struct psp_device *psp_master;
20 static struct psp_device *psp_alloc_struct(struct sp_device *sp)
22 struct device *dev = sp->dev;
23 struct psp_device *psp;
25 psp = devm_kzalloc(dev, sizeof(*psp), GFP_KERNEL);
32 snprintf(psp->name, sizeof(psp->name), "psp-%u", sp->ord);
37 static irqreturn_t psp_irq_handler(int irq, void *data)
39 struct psp_device *psp = data;
42 /* Read the interrupt status: */
43 status = ioread32(psp->io_regs + psp->vdata->intsts_reg);
45 /* invoke subdevice interrupt handlers */
47 if (psp->sev_irq_handler)
48 psp->sev_irq_handler(irq, psp->sev_irq_data, status);
50 if (psp->tee_irq_handler)
51 psp->tee_irq_handler(irq, psp->tee_irq_data, status);
54 /* Clear the interrupt status by writing the same value we read. */
55 iowrite32(status, psp->io_regs + psp->vdata->intsts_reg);
60 static unsigned int psp_get_capability(struct psp_device *psp)
62 unsigned int val = ioread32(psp->io_regs + psp->vdata->feature_reg);
65 * Check for a access to the registers. If this read returns
66 * 0xffffffff, it's likely that the system is running a broken
67 * BIOS which disallows access to the device. Stop here and
68 * fail the PSP initialization (but not the load, as the CCP
69 * could get properly initialized).
71 if (val == 0xffffffff) {
72 dev_notice(psp->dev, "psp: unable to access the device: you might be running a broken BIOS.\n");
75 psp->capability = val;
77 /* Detect if TSME and SME are both enabled */
78 if (psp->capability & PSP_CAPABILITY_PSP_SECURITY_REPORTING &&
79 psp->capability & (PSP_SECURITY_TSME_STATUS << PSP_CAPABILITY_PSP_SECURITY_OFFSET) &&
80 cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
81 dev_notice(psp->dev, "psp: Both TSME and SME are active, SME is unnecessary when TSME is active.\n");
86 static int psp_check_sev_support(struct psp_device *psp)
88 /* Check if device supports SEV feature */
89 if (!(psp->capability & PSP_CAPABILITY_SEV)) {
90 dev_dbg(psp->dev, "psp does not support SEV\n");
97 static int psp_check_tee_support(struct psp_device *psp)
99 /* Check if device supports TEE feature */
100 if (!(psp->capability & PSP_CAPABILITY_TEE)) {
101 dev_dbg(psp->dev, "psp does not support TEE\n");
108 static int psp_init(struct psp_device *psp)
112 if (!psp_check_sev_support(psp)) {
113 ret = sev_dev_init(psp);
118 if (!psp_check_tee_support(psp)) {
119 ret = tee_dev_init(psp);
127 int psp_dev_init(struct sp_device *sp)
129 struct device *dev = sp->dev;
130 struct psp_device *psp;
134 psp = psp_alloc_struct(sp);
140 psp->vdata = (struct psp_vdata *)sp->dev_vdata->psp_vdata;
143 dev_err(dev, "missing driver data\n");
147 psp->io_regs = sp->io_map;
149 ret = psp_get_capability(psp);
153 /* Disable and clear interrupts until ready */
154 iowrite32(0, psp->io_regs + psp->vdata->inten_reg);
155 iowrite32(-1, psp->io_regs + psp->vdata->intsts_reg);
158 ret = sp_request_psp_irq(psp->sp, psp_irq_handler, psp->name, psp);
160 dev_err(dev, "psp: unable to allocate an IRQ\n");
168 if (sp->set_psp_master_device)
169 sp->set_psp_master_device(sp);
171 /* Enable interrupt */
172 iowrite32(-1, psp->io_regs + psp->vdata->inten_reg);
174 dev_notice(dev, "psp enabled\n");
179 sp_free_psp_irq(psp->sp, psp);
183 dev_notice(dev, "psp initialization failed\n");
193 void psp_dev_destroy(struct sp_device *sp)
195 struct psp_device *psp = sp->psp_data;
200 sev_dev_destroy(psp);
202 tee_dev_destroy(psp);
204 sp_free_psp_irq(sp, psp);
206 if (sp->clear_psp_master_device)
207 sp->clear_psp_master_device(sp);
210 void psp_set_sev_irq_handler(struct psp_device *psp, psp_irq_handler_t handler,
213 psp->sev_irq_data = data;
214 psp->sev_irq_handler = handler;
217 void psp_clear_sev_irq_handler(struct psp_device *psp)
219 psp_set_sev_irq_handler(psp, NULL, NULL);
222 void psp_set_tee_irq_handler(struct psp_device *psp, psp_irq_handler_t handler,
225 psp->tee_irq_data = data;
226 psp->tee_irq_handler = handler;
229 void psp_clear_tee_irq_handler(struct psp_device *psp)
231 psp_set_tee_irq_handler(psp, NULL, NULL);
234 struct psp_device *psp_get_master_device(void)
236 struct sp_device *sp = sp_get_psp_master_device();
238 return sp ? sp->psp_data : NULL;
241 void psp_pci_init(void)
243 psp_master = psp_get_master_device();
251 void psp_pci_exit(void)