2 * AMD Secure Processor device driver
4 * Copyright (C) 2013,2016 Advanced Micro Devices, Inc.
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.
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>
29 #define MSIX_VECTORS 2
33 struct msix_entry msix_entry[MSIX_VECTORS];
36 static int sp_get_msix_irqs(struct sp_device *sp)
38 struct sp_pci *sp_pci = sp->dev_specific;
39 struct device *dev = sp->dev;
40 struct pci_dev *pdev = to_pci_dev(dev);
43 for (v = 0; v < ARRAY_SIZE(sp_pci->msix_entry); v++)
44 sp_pci->msix_entry[v].entry = v;
46 ret = pci_enable_msix_range(pdev, sp_pci->msix_entry, 1, v);
50 sp_pci->msix_count = ret;
51 sp->use_tasklet = true;
53 sp->psp_irq = sp_pci->msix_entry[0].vector;
54 sp->ccp_irq = (sp_pci->msix_count > 1) ? sp_pci->msix_entry[1].vector
55 : sp_pci->msix_entry[0].vector;
59 static int sp_get_msi_irq(struct sp_device *sp)
61 struct device *dev = sp->dev;
62 struct pci_dev *pdev = to_pci_dev(dev);
65 ret = pci_enable_msi(pdev);
69 sp->ccp_irq = pdev->irq;
70 sp->psp_irq = pdev->irq;
75 static int sp_get_irqs(struct sp_device *sp)
77 struct device *dev = sp->dev;
80 ret = sp_get_msix_irqs(sp);
84 /* Couldn't get MSI-X vectors, try MSI */
85 dev_notice(dev, "could not enable MSI-X (%d), trying MSI\n", ret);
86 ret = sp_get_msi_irq(sp);
90 /* Couldn't get MSI interrupt */
91 dev_notice(dev, "could not enable MSI (%d)\n", ret);
96 static void sp_free_irqs(struct sp_device *sp)
98 struct sp_pci *sp_pci = sp->dev_specific;
99 struct device *dev = sp->dev;
100 struct pci_dev *pdev = to_pci_dev(dev);
102 if (sp_pci->msix_count)
103 pci_disable_msix(pdev);
104 else if (sp->psp_irq)
105 pci_disable_msi(pdev);
111 static int sp_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
113 struct sp_device *sp;
114 struct sp_pci *sp_pci;
115 struct device *dev = &pdev->dev;
116 void __iomem * const *iomap_table;
121 sp = sp_alloc_struct(dev);
125 sp_pci = devm_kzalloc(dev, sizeof(*sp_pci), GFP_KERNEL);
129 sp->dev_specific = sp_pci;
130 sp->dev_vdata = (struct sp_dev_vdata *)id->driver_data;
131 if (!sp->dev_vdata) {
133 dev_err(dev, "missing driver data\n");
137 ret = pcim_enable_device(pdev);
139 dev_err(dev, "pcim_enable_device failed (%d)\n", ret);
143 bar_mask = pci_select_bars(pdev, IORESOURCE_MEM);
144 ret = pcim_iomap_regions(pdev, bar_mask, "ccp");
146 dev_err(dev, "pcim_iomap_regions failed (%d)\n", ret);
150 iomap_table = pcim_iomap_table(pdev);
152 dev_err(dev, "pcim_iomap_table failed\n");
157 sp->io_map = iomap_table[sp->dev_vdata->bar];
159 dev_err(dev, "ioremap failed\n");
164 ret = sp_get_irqs(sp);
168 pci_set_master(pdev);
170 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
172 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
174 dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n",
180 dev_set_drvdata(dev, sp);
186 dev_notice(dev, "enabled\n");
191 dev_notice(dev, "initialization failed\n");
195 static void sp_pci_remove(struct pci_dev *pdev)
197 struct device *dev = &pdev->dev;
198 struct sp_device *sp = dev_get_drvdata(dev);
207 dev_notice(dev, "disabled\n");
211 static int sp_pci_suspend(struct pci_dev *pdev, pm_message_t state)
213 struct device *dev = &pdev->dev;
214 struct sp_device *sp = dev_get_drvdata(dev);
216 return sp_suspend(sp, state);
219 static int sp_pci_resume(struct pci_dev *pdev)
221 struct device *dev = &pdev->dev;
222 struct sp_device *sp = dev_get_drvdata(dev);
224 return sp_resume(sp);
228 static const struct sp_dev_vdata dev_vdata[] = {
231 #ifdef CONFIG_CRYPTO_DEV_SP_CCP
237 #ifdef CONFIG_CRYPTO_DEV_SP_CCP
238 .ccp_vdata = &ccpv5a,
243 #ifdef CONFIG_CRYPTO_DEV_SP_CCP
244 .ccp_vdata = &ccpv5b,
248 static const struct pci_device_id sp_pci_table[] = {
249 { PCI_VDEVICE(AMD, 0x1537), (kernel_ulong_t)&dev_vdata[0] },
250 { PCI_VDEVICE(AMD, 0x1456), (kernel_ulong_t)&dev_vdata[1] },
251 { PCI_VDEVICE(AMD, 0x1468), (kernel_ulong_t)&dev_vdata[2] },
252 /* Last entry must be zero */
255 MODULE_DEVICE_TABLE(pci, sp_pci_table);
257 static struct pci_driver sp_pci_driver = {
259 .id_table = sp_pci_table,
260 .probe = sp_pci_probe,
261 .remove = sp_pci_remove,
263 .suspend = sp_pci_suspend,
264 .resume = sp_pci_resume,
268 int sp_pci_init(void)
270 return pci_register_driver(&sp_pci_driver);
273 void sp_pci_exit(void)
275 pci_unregister_driver(&sp_pci_driver);