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 13 #include "sp-dev.h" 14 #include "psp-dev.h" 15 #include "sev-dev.h" 16 17 struct psp_device *psp_master; 18 19 static struct psp_device *psp_alloc_struct(struct sp_device *sp) 20 { 21 struct device *dev = sp->dev; 22 struct psp_device *psp; 23 24 psp = devm_kzalloc(dev, sizeof(*psp), GFP_KERNEL); 25 if (!psp) 26 return NULL; 27 28 psp->dev = dev; 29 psp->sp = sp; 30 31 snprintf(psp->name, sizeof(psp->name), "psp-%u", sp->ord); 32 33 return psp; 34 } 35 36 static irqreturn_t psp_irq_handler(int irq, void *data) 37 { 38 struct psp_device *psp = data; 39 unsigned int status; 40 41 /* Read the interrupt status: */ 42 status = ioread32(psp->io_regs + psp->vdata->intsts_reg); 43 44 /* invoke subdevice interrupt handlers */ 45 if (status) { 46 if (psp->sev_irq_handler) 47 psp->sev_irq_handler(irq, psp->sev_irq_data, status); 48 } 49 50 /* Clear the interrupt status by writing the same value we read. */ 51 iowrite32(status, psp->io_regs + psp->vdata->intsts_reg); 52 53 return IRQ_HANDLED; 54 } 55 56 static unsigned int psp_get_capability(struct psp_device *psp) 57 { 58 unsigned int val = ioread32(psp->io_regs + psp->vdata->feature_reg); 59 60 /* 61 * Check for a access to the registers. If this read returns 62 * 0xffffffff, it's likely that the system is running a broken 63 * BIOS which disallows access to the device. Stop here and 64 * fail the PSP initialization (but not the load, as the CCP 65 * could get properly initialized). 66 */ 67 if (val == 0xffffffff) { 68 dev_notice(psp->dev, "psp: unable to access the device: you might be running a broken BIOS.\n"); 69 return 0; 70 } 71 72 return val; 73 } 74 75 static int psp_check_sev_support(struct psp_device *psp, 76 unsigned int capability) 77 { 78 /* Check if device supports SEV feature */ 79 if (!(capability & 1)) { 80 dev_dbg(psp->dev, "psp does not support SEV\n"); 81 return -ENODEV; 82 } 83 84 return 0; 85 } 86 87 static int psp_check_tee_support(struct psp_device *psp, 88 unsigned int capability) 89 { 90 /* Check if device supports TEE feature */ 91 if (!(capability & 2)) { 92 dev_dbg(psp->dev, "psp does not support TEE\n"); 93 return -ENODEV; 94 } 95 96 return 0; 97 } 98 99 static int psp_check_support(struct psp_device *psp, 100 unsigned int capability) 101 { 102 int sev_support = psp_check_sev_support(psp, capability); 103 int tee_support = psp_check_tee_support(psp, capability); 104 105 /* Return error if device neither supports SEV nor TEE */ 106 if (sev_support && tee_support) 107 return -ENODEV; 108 109 return 0; 110 } 111 112 int psp_dev_init(struct sp_device *sp) 113 { 114 struct device *dev = sp->dev; 115 struct psp_device *psp; 116 unsigned int capability; 117 int ret; 118 119 ret = -ENOMEM; 120 psp = psp_alloc_struct(sp); 121 if (!psp) 122 goto e_err; 123 124 sp->psp_data = psp; 125 126 psp->vdata = (struct psp_vdata *)sp->dev_vdata->psp_vdata; 127 if (!psp->vdata) { 128 ret = -ENODEV; 129 dev_err(dev, "missing driver data\n"); 130 goto e_err; 131 } 132 133 psp->io_regs = sp->io_map; 134 135 capability = psp_get_capability(psp); 136 if (!capability) 137 goto e_disable; 138 139 ret = psp_check_support(psp, capability); 140 if (ret) 141 goto e_disable; 142 143 /* Disable and clear interrupts until ready */ 144 iowrite32(0, psp->io_regs + psp->vdata->inten_reg); 145 iowrite32(-1, psp->io_regs + psp->vdata->intsts_reg); 146 147 /* Request an irq */ 148 ret = sp_request_psp_irq(psp->sp, psp_irq_handler, psp->name, psp); 149 if (ret) { 150 dev_err(dev, "psp: unable to allocate an IRQ\n"); 151 goto e_err; 152 } 153 154 ret = sev_dev_init(psp); 155 if (ret) 156 goto e_irq; 157 158 if (sp->set_psp_master_device) 159 sp->set_psp_master_device(sp); 160 161 /* Enable interrupt */ 162 iowrite32(-1, psp->io_regs + psp->vdata->inten_reg); 163 164 dev_notice(dev, "psp enabled\n"); 165 166 return 0; 167 168 e_irq: 169 sp_free_psp_irq(psp->sp, psp); 170 e_err: 171 sp->psp_data = NULL; 172 173 dev_notice(dev, "psp initialization failed\n"); 174 175 return ret; 176 177 e_disable: 178 sp->psp_data = NULL; 179 180 return ret; 181 } 182 183 void psp_dev_destroy(struct sp_device *sp) 184 { 185 struct psp_device *psp = sp->psp_data; 186 187 if (!psp) 188 return; 189 190 sev_dev_destroy(psp); 191 192 sp_free_psp_irq(sp, psp); 193 } 194 195 void psp_set_sev_irq_handler(struct psp_device *psp, psp_irq_handler_t handler, 196 void *data) 197 { 198 psp->sev_irq_data = data; 199 psp->sev_irq_handler = handler; 200 } 201 202 void psp_clear_sev_irq_handler(struct psp_device *psp) 203 { 204 psp_set_sev_irq_handler(psp, NULL, NULL); 205 } 206 207 struct psp_device *psp_get_master_device(void) 208 { 209 struct sp_device *sp = sp_get_psp_master_device(); 210 211 return sp ? sp->psp_data : NULL; 212 } 213 214 void psp_pci_init(void) 215 { 216 psp_master = psp_get_master_device(); 217 218 if (!psp_master) 219 return; 220 221 sev_pci_init(); 222 } 223 224 void psp_pci_exit(void) 225 { 226 if (!psp_master) 227 return; 228 229 sev_pci_exit(); 230 } 231