Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * AMD Platform Security Processor (PSP) interface
0004  *
0005  * Copyright (C) 2016,2019 Advanced Micro Devices, Inc.
0006  *
0007  * Author: Brijesh Singh <brijesh.singh@amd.com>
0008  */
0009 
0010 #include <linux/kernel.h>
0011 #include <linux/irqreturn.h>
0012 
0013 #include "sp-dev.h"
0014 #include "psp-dev.h"
0015 #include "sev-dev.h"
0016 #include "tee-dev.h"
0017 
0018 struct psp_device *psp_master;
0019 
0020 static struct psp_device *psp_alloc_struct(struct sp_device *sp)
0021 {
0022     struct device *dev = sp->dev;
0023     struct psp_device *psp;
0024 
0025     psp = devm_kzalloc(dev, sizeof(*psp), GFP_KERNEL);
0026     if (!psp)
0027         return NULL;
0028 
0029     psp->dev = dev;
0030     psp->sp = sp;
0031 
0032     snprintf(psp->name, sizeof(psp->name), "psp-%u", sp->ord);
0033 
0034     return psp;
0035 }
0036 
0037 static irqreturn_t psp_irq_handler(int irq, void *data)
0038 {
0039     struct psp_device *psp = data;
0040     unsigned int status;
0041 
0042     /* Read the interrupt status: */
0043     status = ioread32(psp->io_regs + psp->vdata->intsts_reg);
0044 
0045     /* invoke subdevice interrupt handlers */
0046     if (status) {
0047         if (psp->sev_irq_handler)
0048             psp->sev_irq_handler(irq, psp->sev_irq_data, status);
0049 
0050         if (psp->tee_irq_handler)
0051             psp->tee_irq_handler(irq, psp->tee_irq_data, status);
0052     }
0053 
0054     /* Clear the interrupt status by writing the same value we read. */
0055     iowrite32(status, psp->io_regs + psp->vdata->intsts_reg);
0056 
0057     return IRQ_HANDLED;
0058 }
0059 
0060 static unsigned int psp_get_capability(struct psp_device *psp)
0061 {
0062     unsigned int val = ioread32(psp->io_regs + psp->vdata->feature_reg);
0063 
0064     /*
0065      * Check for a access to the registers.  If this read returns
0066      * 0xffffffff, it's likely that the system is running a broken
0067      * BIOS which disallows access to the device. Stop here and
0068      * fail the PSP initialization (but not the load, as the CCP
0069      * could get properly initialized).
0070      */
0071     if (val == 0xffffffff) {
0072         dev_notice(psp->dev, "psp: unable to access the device: you might be running a broken BIOS.\n");
0073         return -ENODEV;
0074     }
0075     psp->capability = val;
0076 
0077     /* Detect if TSME and SME are both enabled */
0078     if (psp->capability & PSP_CAPABILITY_PSP_SECURITY_REPORTING &&
0079         psp->capability & (PSP_SECURITY_TSME_STATUS << PSP_CAPABILITY_PSP_SECURITY_OFFSET) &&
0080         cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
0081         dev_notice(psp->dev, "psp: Both TSME and SME are active, SME is unnecessary when TSME is active.\n");
0082 
0083     return 0;
0084 }
0085 
0086 static int psp_check_sev_support(struct psp_device *psp)
0087 {
0088     /* Check if device supports SEV feature */
0089     if (!(psp->capability & PSP_CAPABILITY_SEV)) {
0090         dev_dbg(psp->dev, "psp does not support SEV\n");
0091         return -ENODEV;
0092     }
0093 
0094     return 0;
0095 }
0096 
0097 static int psp_check_tee_support(struct psp_device *psp)
0098 {
0099     /* Check if device supports TEE feature */
0100     if (!(psp->capability & PSP_CAPABILITY_TEE)) {
0101         dev_dbg(psp->dev, "psp does not support TEE\n");
0102         return -ENODEV;
0103     }
0104 
0105     return 0;
0106 }
0107 
0108 static int psp_init(struct psp_device *psp)
0109 {
0110     int ret;
0111 
0112     if (!psp_check_sev_support(psp)) {
0113         ret = sev_dev_init(psp);
0114         if (ret)
0115             return ret;
0116     }
0117 
0118     if (!psp_check_tee_support(psp)) {
0119         ret = tee_dev_init(psp);
0120         if (ret)
0121             return ret;
0122     }
0123 
0124     return 0;
0125 }
0126 
0127 int psp_dev_init(struct sp_device *sp)
0128 {
0129     struct device *dev = sp->dev;
0130     struct psp_device *psp;
0131     int ret;
0132 
0133     ret = -ENOMEM;
0134     psp = psp_alloc_struct(sp);
0135     if (!psp)
0136         goto e_err;
0137 
0138     sp->psp_data = psp;
0139 
0140     psp->vdata = (struct psp_vdata *)sp->dev_vdata->psp_vdata;
0141     if (!psp->vdata) {
0142         ret = -ENODEV;
0143         dev_err(dev, "missing driver data\n");
0144         goto e_err;
0145     }
0146 
0147     psp->io_regs = sp->io_map;
0148 
0149     ret = psp_get_capability(psp);
0150     if (ret)
0151         goto e_disable;
0152 
0153     /* Disable and clear interrupts until ready */
0154     iowrite32(0, psp->io_regs + psp->vdata->inten_reg);
0155     iowrite32(-1, psp->io_regs + psp->vdata->intsts_reg);
0156 
0157     /* Request an irq */
0158     ret = sp_request_psp_irq(psp->sp, psp_irq_handler, psp->name, psp);
0159     if (ret) {
0160         dev_err(dev, "psp: unable to allocate an IRQ\n");
0161         goto e_err;
0162     }
0163 
0164     ret = psp_init(psp);
0165     if (ret)
0166         goto e_irq;
0167 
0168     if (sp->set_psp_master_device)
0169         sp->set_psp_master_device(sp);
0170 
0171     /* Enable interrupt */
0172     iowrite32(-1, psp->io_regs + psp->vdata->inten_reg);
0173 
0174     dev_notice(dev, "psp enabled\n");
0175 
0176     return 0;
0177 
0178 e_irq:
0179     sp_free_psp_irq(psp->sp, psp);
0180 e_err:
0181     sp->psp_data = NULL;
0182 
0183     dev_notice(dev, "psp initialization failed\n");
0184 
0185     return ret;
0186 
0187 e_disable:
0188     sp->psp_data = NULL;
0189 
0190     return ret;
0191 }
0192 
0193 void psp_dev_destroy(struct sp_device *sp)
0194 {
0195     struct psp_device *psp = sp->psp_data;
0196 
0197     if (!psp)
0198         return;
0199 
0200     sev_dev_destroy(psp);
0201 
0202     tee_dev_destroy(psp);
0203 
0204     sp_free_psp_irq(sp, psp);
0205 
0206     if (sp->clear_psp_master_device)
0207         sp->clear_psp_master_device(sp);
0208 }
0209 
0210 void psp_set_sev_irq_handler(struct psp_device *psp, psp_irq_handler_t handler,
0211                  void *data)
0212 {
0213     psp->sev_irq_data = data;
0214     psp->sev_irq_handler = handler;
0215 }
0216 
0217 void psp_clear_sev_irq_handler(struct psp_device *psp)
0218 {
0219     psp_set_sev_irq_handler(psp, NULL, NULL);
0220 }
0221 
0222 void psp_set_tee_irq_handler(struct psp_device *psp, psp_irq_handler_t handler,
0223                  void *data)
0224 {
0225     psp->tee_irq_data = data;
0226     psp->tee_irq_handler = handler;
0227 }
0228 
0229 void psp_clear_tee_irq_handler(struct psp_device *psp)
0230 {
0231     psp_set_tee_irq_handler(psp, NULL, NULL);
0232 }
0233 
0234 struct psp_device *psp_get_master_device(void)
0235 {
0236     struct sp_device *sp = sp_get_psp_master_device();
0237 
0238     return sp ? sp->psp_data : NULL;
0239 }
0240 
0241 void psp_pci_init(void)
0242 {
0243     psp_master = psp_get_master_device();
0244 
0245     if (!psp_master)
0246         return;
0247 
0248     sev_pci_init();
0249 }
0250 
0251 void psp_pci_exit(void)
0252 {
0253     if (!psp_master)
0254         return;
0255 
0256     sev_pci_exit();
0257 }