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