1 // SPDX-License-Identifier: GPL-2.0-only
3 * AMD Passthru DMA device driver
4 * -- Based on the CCP driver
6 * Copyright (C) 2016,2021 Advanced Micro Devices, Inc.
8 * Author: Sanjay R Mehta <sanju.mehta@amd.com>
9 * Author: Tom Lendacky <thomas.lendacky@amd.com>
10 * Author: Gary R Hook <gary.hook@amd.com>
13 #include <linux/device.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/delay.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
18 #include <linux/kthread.h>
19 #include <linux/module.h>
20 #include <linux/pci_ids.h>
21 #include <linux/pci.h>
22 #include <linux/spinlock.h>
28 struct msix_entry msix_entry
;
32 * pt_alloc_struct - allocate and initialize the pt_device struct
34 * @dev: device struct of the PTDMA
36 static struct pt_device
*pt_alloc_struct(struct device
*dev
)
40 pt
= devm_kzalloc(dev
, sizeof(*pt
), GFP_KERNEL
);
46 INIT_LIST_HEAD(&pt
->cmd
);
51 static int pt_get_msix_irqs(struct pt_device
*pt
)
53 struct pt_msix
*pt_msix
= pt
->pt_msix
;
54 struct device
*dev
= pt
->dev
;
55 struct pci_dev
*pdev
= to_pci_dev(dev
);
58 pt_msix
->msix_entry
.entry
= 0;
60 ret
= pci_enable_msix_range(pdev
, &pt_msix
->msix_entry
, 1, 1);
64 pt_msix
->msix_count
= ret
;
66 pt
->pt_irq
= pt_msix
->msix_entry
.vector
;
71 static int pt_get_msi_irq(struct pt_device
*pt
)
73 struct device
*dev
= pt
->dev
;
74 struct pci_dev
*pdev
= to_pci_dev(dev
);
77 ret
= pci_enable_msi(pdev
);
81 pt
->pt_irq
= pdev
->irq
;
86 static int pt_get_irqs(struct pt_device
*pt
)
88 struct device
*dev
= pt
->dev
;
91 ret
= pt_get_msix_irqs(pt
);
95 /* Couldn't get MSI-X vectors, try MSI */
96 dev_err(dev
, "could not enable MSI-X (%d), trying MSI\n", ret
);
97 ret
= pt_get_msi_irq(pt
);
101 /* Couldn't get MSI interrupt */
102 dev_err(dev
, "could not enable MSI (%d)\n", ret
);
107 static void pt_free_irqs(struct pt_device
*pt
)
109 struct pt_msix
*pt_msix
= pt
->pt_msix
;
110 struct device
*dev
= pt
->dev
;
111 struct pci_dev
*pdev
= to_pci_dev(dev
);
113 if (pt_msix
->msix_count
)
114 pci_disable_msix(pdev
);
116 pci_disable_msi(pdev
);
121 static int pt_pci_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
123 struct pt_device
*pt
;
124 struct pt_msix
*pt_msix
;
125 struct device
*dev
= &pdev
->dev
;
126 void __iomem
* const *iomap_table
;
130 pt
= pt_alloc_struct(dev
);
134 pt_msix
= devm_kzalloc(dev
, sizeof(*pt_msix
), GFP_KERNEL
);
138 pt
->pt_msix
= pt_msix
;
139 pt
->dev_vdata
= (struct pt_dev_vdata
*)id
->driver_data
;
140 if (!pt
->dev_vdata
) {
142 dev_err(dev
, "missing driver data\n");
146 ret
= pcim_enable_device(pdev
);
148 dev_err(dev
, "pcim_enable_device failed (%d)\n", ret
);
152 bar_mask
= pci_select_bars(pdev
, IORESOURCE_MEM
);
153 ret
= pcim_iomap_regions(pdev
, bar_mask
, "ptdma");
155 dev_err(dev
, "pcim_iomap_regions failed (%d)\n", ret
);
159 iomap_table
= pcim_iomap_table(pdev
);
161 dev_err(dev
, "pcim_iomap_table failed\n");
166 pt
->io_regs
= iomap_table
[pt
->dev_vdata
->bar
];
168 dev_err(dev
, "ioremap failed\n");
173 ret
= pt_get_irqs(pt
);
177 pci_set_master(pdev
);
179 ret
= dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(48));
181 ret
= dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(32));
183 dev_err(dev
, "dma_set_mask_and_coherent failed (%d)\n",
189 dev_set_drvdata(dev
, pt
);
192 ret
= pt_core_init(pt
);
200 dev_err(dev
, "initialization failed ret = %d\n", ret
);
205 static void pt_pci_remove(struct pci_dev
*pdev
)
207 struct device
*dev
= &pdev
->dev
;
208 struct pt_device
*pt
= dev_get_drvdata(dev
);
219 static const struct pt_dev_vdata dev_vdata
[] = {
225 static const struct pci_device_id pt_pci_table
[] = {
226 { PCI_VDEVICE(AMD
, 0x1498), (kernel_ulong_t
)&dev_vdata
[0] },
227 /* Last entry must be zero */
230 MODULE_DEVICE_TABLE(pci
, pt_pci_table
);
232 static struct pci_driver pt_pci_driver
= {
234 .id_table
= pt_pci_table
,
235 .probe
= pt_pci_probe
,
236 .remove
= pt_pci_remove
,
239 module_pci_driver(pt_pci_driver
);
241 MODULE_AUTHOR("Sanjay R Mehta <sanju.mehta@amd.com>");
242 MODULE_LICENSE("GPL");
243 MODULE_DESCRIPTION("AMD PassThru DMA driver");