Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / dma / ptdma / ptdma-pci.c
blob22739ff0c3c56cca9da29b9d9e8c9ee14f885f82
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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>
24 #include "ptdma.h"
26 struct pt_msix {
27 int msix_count;
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)
38 struct pt_device *pt;
40 pt = devm_kzalloc(dev, sizeof(*pt), GFP_KERNEL);
42 if (!pt)
43 return NULL;
44 pt->dev = dev;
46 INIT_LIST_HEAD(&pt->cmd);
48 return pt;
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);
56 int ret;
58 pt_msix->msix_entry.entry = 0;
60 ret = pci_enable_msix_range(pdev, &pt_msix->msix_entry, 1, 1);
61 if (ret < 0)
62 return ret;
64 pt_msix->msix_count = ret;
66 pt->pt_irq = pt_msix->msix_entry.vector;
68 return 0;
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);
75 int ret;
77 ret = pci_enable_msi(pdev);
78 if (ret)
79 return ret;
81 pt->pt_irq = pdev->irq;
83 return 0;
86 static int pt_get_irqs(struct pt_device *pt)
88 struct device *dev = pt->dev;
89 int ret;
91 ret = pt_get_msix_irqs(pt);
92 if (!ret)
93 return 0;
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);
98 if (!ret)
99 return 0;
101 /* Couldn't get MSI interrupt */
102 dev_err(dev, "could not enable MSI (%d)\n", ret);
104 return 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);
115 else if (pt->pt_irq)
116 pci_disable_msi(pdev);
118 pt->pt_irq = 0;
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;
127 int bar_mask;
128 int ret = -ENOMEM;
130 pt = pt_alloc_struct(dev);
131 if (!pt)
132 goto e_err;
134 pt_msix = devm_kzalloc(dev, sizeof(*pt_msix), GFP_KERNEL);
135 if (!pt_msix)
136 goto e_err;
138 pt->pt_msix = pt_msix;
139 pt->dev_vdata = (struct pt_dev_vdata *)id->driver_data;
140 if (!pt->dev_vdata) {
141 ret = -ENODEV;
142 dev_err(dev, "missing driver data\n");
143 goto e_err;
146 ret = pcim_enable_device(pdev);
147 if (ret) {
148 dev_err(dev, "pcim_enable_device failed (%d)\n", ret);
149 goto e_err;
152 bar_mask = pci_select_bars(pdev, IORESOURCE_MEM);
153 ret = pcim_iomap_regions(pdev, bar_mask, "ptdma");
154 if (ret) {
155 dev_err(dev, "pcim_iomap_regions failed (%d)\n", ret);
156 goto e_err;
159 iomap_table = pcim_iomap_table(pdev);
160 if (!iomap_table) {
161 dev_err(dev, "pcim_iomap_table failed\n");
162 ret = -ENOMEM;
163 goto e_err;
166 pt->io_regs = iomap_table[pt->dev_vdata->bar];
167 if (!pt->io_regs) {
168 dev_err(dev, "ioremap failed\n");
169 ret = -ENOMEM;
170 goto e_err;
173 ret = pt_get_irqs(pt);
174 if (ret)
175 goto e_err;
177 pci_set_master(pdev);
179 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
180 if (ret) {
181 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
182 if (ret) {
183 dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n",
184 ret);
185 goto e_err;
189 dev_set_drvdata(dev, pt);
191 if (pt->dev_vdata)
192 ret = pt_core_init(pt);
194 if (ret)
195 goto e_err;
197 return 0;
199 e_err:
200 dev_err(dev, "initialization failed ret = %d\n", ret);
202 return 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);
210 if (!pt)
211 return;
213 if (pt->dev_vdata)
214 pt_core_destroy(pt);
216 pt_free_irqs(pt);
219 static const struct pt_dev_vdata dev_vdata[] = {
221 .bar = 2,
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 */
228 { 0, }
230 MODULE_DEVICE_TABLE(pci, pt_pci_table);
232 static struct pci_driver pt_pci_driver = {
233 .name = "ptdma",
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");