xref: /linux-6.15/drivers/crypto/ccp/psp-dev.c (revision cac32cd4)
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 #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 		if (psp->tee_irq_handler)
51 			psp->tee_irq_handler(irq, psp->tee_irq_data, status);
52 	}
53 
54 	/* Clear the interrupt status by writing the same value we read. */
55 	iowrite32(status, psp->io_regs + psp->vdata->intsts_reg);
56 
57 	return IRQ_HANDLED;
58 }
59 
60 static unsigned int psp_get_capability(struct psp_device *psp)
61 {
62 	unsigned int val = ioread32(psp->io_regs + psp->vdata->feature_reg);
63 
64 	/*
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).
70 	 */
71 	if (val == 0xffffffff) {
72 		dev_notice(psp->dev, "psp: unable to access the device: you might be running a broken BIOS.\n");
73 		return -ENODEV;
74 	}
75 	psp->capability = val;
76 
77 	return 0;
78 }
79 
80 static int psp_check_sev_support(struct psp_device *psp)
81 {
82 	/* Check if device supports SEV feature */
83 	if (!(psp->capability & PSP_CAPABILITY_SEV)) {
84 		dev_dbg(psp->dev, "psp does not support SEV\n");
85 		return -ENODEV;
86 	}
87 
88 	return 0;
89 }
90 
91 static int psp_check_tee_support(struct psp_device *psp)
92 {
93 	/* Check if device supports TEE feature */
94 	if (!(psp->capability & PSP_CAPABILITY_TEE)) {
95 		dev_dbg(psp->dev, "psp does not support TEE\n");
96 		return -ENODEV;
97 	}
98 
99 	return 0;
100 }
101 
102 static int psp_check_support(struct psp_device *psp)
103 {
104 	int sev_support = psp_check_sev_support(psp);
105 	int tee_support = psp_check_tee_support(psp);
106 
107 	/* Return error if device neither supports SEV nor TEE */
108 	if (sev_support && tee_support)
109 		return -ENODEV;
110 
111 	return 0;
112 }
113 
114 static int psp_init(struct psp_device *psp)
115 {
116 	int ret;
117 
118 	if (!psp_check_sev_support(psp)) {
119 		ret = sev_dev_init(psp);
120 		if (ret)
121 			return ret;
122 	}
123 
124 	if (!psp_check_tee_support(psp)) {
125 		ret = tee_dev_init(psp);
126 		if (ret)
127 			return ret;
128 	}
129 
130 	return 0;
131 }
132 
133 int psp_dev_init(struct sp_device *sp)
134 {
135 	struct device *dev = sp->dev;
136 	struct psp_device *psp;
137 	int ret;
138 
139 	ret = -ENOMEM;
140 	psp = psp_alloc_struct(sp);
141 	if (!psp)
142 		goto e_err;
143 
144 	sp->psp_data = psp;
145 
146 	psp->vdata = (struct psp_vdata *)sp->dev_vdata->psp_vdata;
147 	if (!psp->vdata) {
148 		ret = -ENODEV;
149 		dev_err(dev, "missing driver data\n");
150 		goto e_err;
151 	}
152 
153 	psp->io_regs = sp->io_map;
154 
155 	ret = psp_get_capability(psp);
156 	if (ret)
157 		goto e_disable;
158 
159 	ret = psp_check_support(psp);
160 	if (ret)
161 		goto e_disable;
162 
163 	/* Disable and clear interrupts until ready */
164 	iowrite32(0, psp->io_regs + psp->vdata->inten_reg);
165 	iowrite32(-1, psp->io_regs + psp->vdata->intsts_reg);
166 
167 	/* Request an irq */
168 	ret = sp_request_psp_irq(psp->sp, psp_irq_handler, psp->name, psp);
169 	if (ret) {
170 		dev_err(dev, "psp: unable to allocate an IRQ\n");
171 		goto e_err;
172 	}
173 
174 	ret = psp_init(psp);
175 	if (ret)
176 		goto e_irq;
177 
178 	if (sp->set_psp_master_device)
179 		sp->set_psp_master_device(sp);
180 
181 	/* Enable interrupt */
182 	iowrite32(-1, psp->io_regs + psp->vdata->inten_reg);
183 
184 	dev_notice(dev, "psp enabled\n");
185 
186 	return 0;
187 
188 e_irq:
189 	sp_free_psp_irq(psp->sp, psp);
190 e_err:
191 	sp->psp_data = NULL;
192 
193 	dev_notice(dev, "psp initialization failed\n");
194 
195 	return ret;
196 
197 e_disable:
198 	sp->psp_data = NULL;
199 
200 	return ret;
201 }
202 
203 void psp_dev_destroy(struct sp_device *sp)
204 {
205 	struct psp_device *psp = sp->psp_data;
206 
207 	if (!psp)
208 		return;
209 
210 	sev_dev_destroy(psp);
211 
212 	tee_dev_destroy(psp);
213 
214 	sp_free_psp_irq(sp, psp);
215 
216 	if (sp->clear_psp_master_device)
217 		sp->clear_psp_master_device(sp);
218 }
219 
220 void psp_set_sev_irq_handler(struct psp_device *psp, psp_irq_handler_t handler,
221 			     void *data)
222 {
223 	psp->sev_irq_data = data;
224 	psp->sev_irq_handler = handler;
225 }
226 
227 void psp_clear_sev_irq_handler(struct psp_device *psp)
228 {
229 	psp_set_sev_irq_handler(psp, NULL, NULL);
230 }
231 
232 void psp_set_tee_irq_handler(struct psp_device *psp, psp_irq_handler_t handler,
233 			     void *data)
234 {
235 	psp->tee_irq_data = data;
236 	psp->tee_irq_handler = handler;
237 }
238 
239 void psp_clear_tee_irq_handler(struct psp_device *psp)
240 {
241 	psp_set_tee_irq_handler(psp, NULL, NULL);
242 }
243 
244 struct psp_device *psp_get_master_device(void)
245 {
246 	struct sp_device *sp = sp_get_psp_master_device();
247 
248 	return sp ? sp->psp_data : NULL;
249 }
250 
251 void psp_pci_init(void)
252 {
253 	psp_master = psp_get_master_device();
254 
255 	if (!psp_master)
256 		return;
257 
258 	sev_pci_init();
259 }
260 
261 void psp_pci_exit(void)
262 {
263 	if (!psp_master)
264 		return;
265 
266 	sev_pci_exit();
267 }
268