xref: /openbmc/linux/drivers/crypto/ccp/sp-pci.c (revision 5b628549)
1 /*
2  * AMD Secure Processor device driver
3  *
4  * Copyright (C) 2013,2018 Advanced Micro Devices, Inc.
5  *
6  * Author: Tom Lendacky <thomas.lendacky@amd.com>
7  * Author: Gary R Hook <gary.hook@amd.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13 
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/device.h>
17 #include <linux/pci.h>
18 #include <linux/pci_ids.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/kthread.h>
21 #include <linux/sched.h>
22 #include <linux/interrupt.h>
23 #include <linux/spinlock.h>
24 #include <linux/delay.h>
25 #include <linux/ccp.h>
26 
27 #include "ccp-dev.h"
28 #include "psp-dev.h"
29 
30 #define MSIX_VECTORS			2
31 
32 struct sp_pci {
33 	int msix_count;
34 	struct msix_entry msix_entry[MSIX_VECTORS];
35 };
36 static struct sp_device *sp_dev_master;
37 
38 static int sp_get_msix_irqs(struct sp_device *sp)
39 {
40 	struct sp_pci *sp_pci = sp->dev_specific;
41 	struct device *dev = sp->dev;
42 	struct pci_dev *pdev = to_pci_dev(dev);
43 	int v, ret;
44 
45 	for (v = 0; v < ARRAY_SIZE(sp_pci->msix_entry); v++)
46 		sp_pci->msix_entry[v].entry = v;
47 
48 	ret = pci_enable_msix_range(pdev, sp_pci->msix_entry, 1, v);
49 	if (ret < 0)
50 		return ret;
51 
52 	sp_pci->msix_count = ret;
53 	sp->use_tasklet = true;
54 
55 	sp->psp_irq = sp_pci->msix_entry[0].vector;
56 	sp->ccp_irq = (sp_pci->msix_count > 1) ? sp_pci->msix_entry[1].vector
57 					       : sp_pci->msix_entry[0].vector;
58 	return 0;
59 }
60 
61 static int sp_get_msi_irq(struct sp_device *sp)
62 {
63 	struct device *dev = sp->dev;
64 	struct pci_dev *pdev = to_pci_dev(dev);
65 	int ret;
66 
67 	ret = pci_enable_msi(pdev);
68 	if (ret)
69 		return ret;
70 
71 	sp->ccp_irq = pdev->irq;
72 	sp->psp_irq = pdev->irq;
73 
74 	return 0;
75 }
76 
77 static int sp_get_irqs(struct sp_device *sp)
78 {
79 	struct device *dev = sp->dev;
80 	int ret;
81 
82 	ret = sp_get_msix_irqs(sp);
83 	if (!ret)
84 		return 0;
85 
86 	/* Couldn't get MSI-X vectors, try MSI */
87 	dev_notice(dev, "could not enable MSI-X (%d), trying MSI\n", ret);
88 	ret = sp_get_msi_irq(sp);
89 	if (!ret)
90 		return 0;
91 
92 	/* Couldn't get MSI interrupt */
93 	dev_notice(dev, "could not enable MSI (%d)\n", ret);
94 
95 	return ret;
96 }
97 
98 static void sp_free_irqs(struct sp_device *sp)
99 {
100 	struct sp_pci *sp_pci = sp->dev_specific;
101 	struct device *dev = sp->dev;
102 	struct pci_dev *pdev = to_pci_dev(dev);
103 
104 	if (sp_pci->msix_count)
105 		pci_disable_msix(pdev);
106 	else if (sp->psp_irq)
107 		pci_disable_msi(pdev);
108 
109 	sp->ccp_irq = 0;
110 	sp->psp_irq = 0;
111 }
112 
113 static bool sp_pci_is_master(struct sp_device *sp)
114 {
115 	struct device *dev_cur, *dev_new;
116 	struct pci_dev *pdev_cur, *pdev_new;
117 
118 	dev_new = sp->dev;
119 	dev_cur = sp_dev_master->dev;
120 
121 	pdev_new = to_pci_dev(dev_new);
122 	pdev_cur = to_pci_dev(dev_cur);
123 
124 	if (pdev_new->bus->number < pdev_cur->bus->number)
125 		return true;
126 
127 	if (PCI_SLOT(pdev_new->devfn) < PCI_SLOT(pdev_cur->devfn))
128 		return true;
129 
130 	if (PCI_FUNC(pdev_new->devfn) < PCI_FUNC(pdev_cur->devfn))
131 		return true;
132 
133 	return false;
134 }
135 
136 static void psp_set_master(struct sp_device *sp)
137 {
138 	if (!sp_dev_master) {
139 		sp_dev_master = sp;
140 		return;
141 	}
142 
143 	if (sp_pci_is_master(sp))
144 		sp_dev_master = sp;
145 }
146 
147 static struct sp_device *psp_get_master(void)
148 {
149 	return sp_dev_master;
150 }
151 
152 static int sp_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
153 {
154 	struct sp_device *sp;
155 	struct sp_pci *sp_pci;
156 	struct device *dev = &pdev->dev;
157 	void __iomem * const *iomap_table;
158 	int bar_mask;
159 	int ret;
160 
161 	ret = -ENOMEM;
162 	sp = sp_alloc_struct(dev);
163 	if (!sp)
164 		goto e_err;
165 
166 	sp_pci = devm_kzalloc(dev, sizeof(*sp_pci), GFP_KERNEL);
167 	if (!sp_pci)
168 		goto e_err;
169 
170 	sp->dev_specific = sp_pci;
171 	sp->dev_vdata = (struct sp_dev_vdata *)id->driver_data;
172 	if (!sp->dev_vdata) {
173 		ret = -ENODEV;
174 		dev_err(dev, "missing driver data\n");
175 		goto e_err;
176 	}
177 
178 	ret = pcim_enable_device(pdev);
179 	if (ret) {
180 		dev_err(dev, "pcim_enable_device failed (%d)\n", ret);
181 		goto e_err;
182 	}
183 
184 	bar_mask = pci_select_bars(pdev, IORESOURCE_MEM);
185 	ret = pcim_iomap_regions(pdev, bar_mask, "ccp");
186 	if (ret) {
187 		dev_err(dev, "pcim_iomap_regions failed (%d)\n", ret);
188 		goto e_err;
189 	}
190 
191 	iomap_table = pcim_iomap_table(pdev);
192 	if (!iomap_table) {
193 		dev_err(dev, "pcim_iomap_table failed\n");
194 		ret = -ENOMEM;
195 		goto e_err;
196 	}
197 
198 	sp->io_map = iomap_table[sp->dev_vdata->bar];
199 	if (!sp->io_map) {
200 		dev_err(dev, "ioremap failed\n");
201 		ret = -ENOMEM;
202 		goto e_err;
203 	}
204 
205 	ret = sp_get_irqs(sp);
206 	if (ret)
207 		goto e_err;
208 
209 	pci_set_master(pdev);
210 	sp->set_psp_master_device = psp_set_master;
211 	sp->get_psp_master_device = psp_get_master;
212 
213 	ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
214 	if (ret) {
215 		ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
216 		if (ret) {
217 			dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n",
218 				ret);
219 			goto e_err;
220 		}
221 	}
222 
223 	dev_set_drvdata(dev, sp);
224 
225 	ret = sp_init(sp);
226 	if (ret)
227 		goto e_err;
228 
229 	return 0;
230 
231 e_err:
232 	dev_notice(dev, "initialization failed\n");
233 	return ret;
234 }
235 
236 static void sp_pci_remove(struct pci_dev *pdev)
237 {
238 	struct device *dev = &pdev->dev;
239 	struct sp_device *sp = dev_get_drvdata(dev);
240 
241 	if (!sp)
242 		return;
243 
244 	sp_destroy(sp);
245 
246 	sp_free_irqs(sp);
247 }
248 
249 #ifdef CONFIG_PM
250 static int sp_pci_suspend(struct pci_dev *pdev, pm_message_t state)
251 {
252 	struct device *dev = &pdev->dev;
253 	struct sp_device *sp = dev_get_drvdata(dev);
254 
255 	return sp_suspend(sp, state);
256 }
257 
258 static int sp_pci_resume(struct pci_dev *pdev)
259 {
260 	struct device *dev = &pdev->dev;
261 	struct sp_device *sp = dev_get_drvdata(dev);
262 
263 	return sp_resume(sp);
264 }
265 #endif
266 
267 #ifdef CONFIG_CRYPTO_DEV_SP_PSP
268 static const struct psp_vdata pspv1 = {
269 	.cmdresp_reg		= 0x10580,
270 	.cmdbuff_addr_lo_reg	= 0x105e0,
271 	.cmdbuff_addr_hi_reg	= 0x105e4,
272 	.feature_reg		= 0x105fc,
273 	.inten_reg		= 0x10610,
274 	.intsts_reg		= 0x10614,
275 };
276 
277 static const struct psp_vdata pspv2 = {
278 	.cmdresp_reg		= 0x10980,
279 	.cmdbuff_addr_lo_reg	= 0x109e0,
280 	.cmdbuff_addr_hi_reg	= 0x109e4,
281 	.feature_reg		= 0x109fc,
282 	.inten_reg		= 0x10690,
283 	.intsts_reg		= 0x10694,
284 };
285 #endif
286 
287 static const struct sp_dev_vdata dev_vdata[] = {
288 	{	/* 0 */
289 		.bar = 2,
290 #ifdef CONFIG_CRYPTO_DEV_SP_CCP
291 		.ccp_vdata = &ccpv3,
292 #endif
293 	},
294 	{	/* 1 */
295 		.bar = 2,
296 #ifdef CONFIG_CRYPTO_DEV_SP_CCP
297 		.ccp_vdata = &ccpv5a,
298 #endif
299 #ifdef CONFIG_CRYPTO_DEV_SP_PSP
300 		.psp_vdata = &pspv1,
301 #endif
302 	},
303 	{	/* 2 */
304 		.bar = 2,
305 #ifdef CONFIG_CRYPTO_DEV_SP_CCP
306 		.ccp_vdata = &ccpv5b,
307 #endif
308 	},
309 	{	/* 3 */
310 		.bar = 2,
311 #ifdef CONFIG_CRYPTO_DEV_SP_CCP
312 		.ccp_vdata = &ccpv5a,
313 #endif
314 #ifdef CONFIG_CRYPTO_DEV_SP_PSP
315 		.psp_vdata = &pspv2,
316 #endif
317 	},
318 };
319 static const struct pci_device_id sp_pci_table[] = {
320 	{ PCI_VDEVICE(AMD, 0x1537), (kernel_ulong_t)&dev_vdata[0] },
321 	{ PCI_VDEVICE(AMD, 0x1456), (kernel_ulong_t)&dev_vdata[1] },
322 	{ PCI_VDEVICE(AMD, 0x1468), (kernel_ulong_t)&dev_vdata[2] },
323 	{ PCI_VDEVICE(AMD, 0x1486), (kernel_ulong_t)&dev_vdata[3] },
324 	/* Last entry must be zero */
325 	{ 0, }
326 };
327 MODULE_DEVICE_TABLE(pci, sp_pci_table);
328 
329 static struct pci_driver sp_pci_driver = {
330 	.name = "ccp",
331 	.id_table = sp_pci_table,
332 	.probe = sp_pci_probe,
333 	.remove = sp_pci_remove,
334 #ifdef CONFIG_PM
335 	.suspend = sp_pci_suspend,
336 	.resume = sp_pci_resume,
337 #endif
338 };
339 
340 int sp_pci_init(void)
341 {
342 	return pci_register_driver(&sp_pci_driver);
343 }
344 
345 void sp_pci_exit(void)
346 {
347 	pci_unregister_driver(&sp_pci_driver);
348 }
349