1bce5c2eaSStephen Hemminger // SPDX-License-Identifier: GPL-2.0
2ccb86a69SMichael S. Tsirkin /* uio_pci_generic - generic UIO driver for PCI 2.3 devices
3ccb86a69SMichael S. Tsirkin *
4ccb86a69SMichael S. Tsirkin * Copyright (C) 2009 Red Hat, Inc.
5ccb86a69SMichael S. Tsirkin * Author: Michael S. Tsirkin <mst@redhat.com>
6ccb86a69SMichael S. Tsirkin *
7ccb86a69SMichael S. Tsirkin * Since the driver does not declare any device ids, you must allocate
8ccb86a69SMichael S. Tsirkin * id and bind the device to the driver yourself. For example:
9ccb86a69SMichael S. Tsirkin *
10ccb86a69SMichael S. Tsirkin * # echo "8086 10f5" > /sys/bus/pci/drivers/uio_pci_generic/new_id
11ccb86a69SMichael S. Tsirkin * # echo -n 0000:00:19.0 > /sys/bus/pci/drivers/e1000e/unbind
12ccb86a69SMichael S. Tsirkin * # echo -n 0000:00:19.0 > /sys/bus/pci/drivers/uio_pci_generic/bind
13ccb86a69SMichael S. Tsirkin * # ls -l /sys/bus/pci/devices/0000:00:19.0/driver
14ccb86a69SMichael S. Tsirkin * .../0000:00:19.0/driver -> ../../../bus/pci/drivers/uio_pci_generic
15ccb86a69SMichael S. Tsirkin *
16ccb86a69SMichael S. Tsirkin * Driver won't bind to devices which do not support the Interrupt Disable Bit
17ccb86a69SMichael S. Tsirkin * in the command register. All devices compliant to PCI 2.3 (circa 2002) and
18ccb86a69SMichael S. Tsirkin * all compliant PCI Express devices should support this bit.
19ccb86a69SMichael S. Tsirkin */
20ccb86a69SMichael S. Tsirkin
21ccb86a69SMichael S. Tsirkin #include <linux/device.h>
22ccb86a69SMichael S. Tsirkin #include <linux/module.h>
23ccb86a69SMichael S. Tsirkin #include <linux/pci.h>
245a0e3ad6STejun Heo #include <linux/slab.h>
25ccb86a69SMichael S. Tsirkin #include <linux/uio_driver.h>
26ccb86a69SMichael S. Tsirkin
27ccb86a69SMichael S. Tsirkin #define DRIVER_VERSION "0.01.0"
28ccb86a69SMichael S. Tsirkin #define DRIVER_AUTHOR "Michael S. Tsirkin <mst@redhat.com>"
29ccb86a69SMichael S. Tsirkin #define DRIVER_DESC "Generic UIO driver for PCI 2.3 devices"
30ccb86a69SMichael S. Tsirkin
31ccb86a69SMichael S. Tsirkin struct uio_pci_generic_dev {
32ccb86a69SMichael S. Tsirkin struct uio_info info;
33ccb86a69SMichael S. Tsirkin struct pci_dev *pdev;
34ccb86a69SMichael S. Tsirkin };
35ccb86a69SMichael S. Tsirkin
36ccb86a69SMichael S. Tsirkin static inline struct uio_pci_generic_dev *
to_uio_pci_generic_dev(struct uio_info * info)37ccb86a69SMichael S. Tsirkin to_uio_pci_generic_dev(struct uio_info *info)
38ccb86a69SMichael S. Tsirkin {
39ccb86a69SMichael S. Tsirkin return container_of(info, struct uio_pci_generic_dev, info);
40ccb86a69SMichael S. Tsirkin }
41ccb86a69SMichael S. Tsirkin
release(struct uio_info * info,struct inode * inode)42865a11f9SVenkatesh Srinivas static int release(struct uio_info *info, struct inode *inode)
43865a11f9SVenkatesh Srinivas {
44865a11f9SVenkatesh Srinivas struct uio_pci_generic_dev *gdev = to_uio_pci_generic_dev(info);
45865a11f9SVenkatesh Srinivas
46865a11f9SVenkatesh Srinivas /*
47865a11f9SVenkatesh Srinivas * This driver is insecure when used with devices doing DMA, but some
48865a11f9SVenkatesh Srinivas * people (mis)use it with such devices.
49865a11f9SVenkatesh Srinivas * Let's at least make sure DMA isn't left enabled after the userspace
50865a11f9SVenkatesh Srinivas * driver closes the fd.
51865a11f9SVenkatesh Srinivas * Note that there's a non-zero chance doing this will wedge the device
52865a11f9SVenkatesh Srinivas * at least until reset.
53865a11f9SVenkatesh Srinivas */
54865a11f9SVenkatesh Srinivas pci_clear_master(gdev->pdev);
55865a11f9SVenkatesh Srinivas return 0;
56865a11f9SVenkatesh Srinivas }
57865a11f9SVenkatesh Srinivas
58ccb86a69SMichael S. Tsirkin /* Interrupt handler. Read/modify/write the command register to disable
59ccb86a69SMichael S. Tsirkin * the interrupt. */
irqhandler(int irq,struct uio_info * info)60ccb86a69SMichael S. Tsirkin static irqreturn_t irqhandler(int irq, struct uio_info *info)
61ccb86a69SMichael S. Tsirkin {
62ccb86a69SMichael S. Tsirkin struct uio_pci_generic_dev *gdev = to_uio_pci_generic_dev(info);
63ccb86a69SMichael S. Tsirkin
642502dbdfSJan Kiszka if (!pci_check_and_mask_intx(gdev->pdev))
652502dbdfSJan Kiszka return IRQ_NONE;
66ccb86a69SMichael S. Tsirkin
67ccb86a69SMichael S. Tsirkin /* UIO core will signal the user process. */
682502dbdfSJan Kiszka return IRQ_HANDLED;
69ccb86a69SMichael S. Tsirkin }
70ccb86a69SMichael S. Tsirkin
probe(struct pci_dev * pdev,const struct pci_device_id * id)71b17b75bbSBill Pemberton static int probe(struct pci_dev *pdev,
72ccb86a69SMichael S. Tsirkin const struct pci_device_id *id)
73ccb86a69SMichael S. Tsirkin {
74ccb86a69SMichael S. Tsirkin struct uio_pci_generic_dev *gdev;
75*e4e05016SFiras Ashkar struct uio_mem *uiomem;
76ccb86a69SMichael S. Tsirkin int err;
77*e4e05016SFiras Ashkar int i;
78ccb86a69SMichael S. Tsirkin
79ef84928cSAlexandru Ardelean err = pcim_enable_device(pdev);
80ccb86a69SMichael S. Tsirkin if (err) {
81ccb86a69SMichael S. Tsirkin dev_err(&pdev->dev, "%s: pci_enable_device failed: %d\n",
82ccb86a69SMichael S. Tsirkin __func__, err);
83ccb86a69SMichael S. Tsirkin return err;
84ccb86a69SMichael S. Tsirkin }
85ccb86a69SMichael S. Tsirkin
86ef84928cSAlexandru Ardelean if (pdev->irq && !pci_intx_mask_supported(pdev))
87156ed021SMartin Ågren return -ENODEV;
88ccb86a69SMichael S. Tsirkin
89ef84928cSAlexandru Ardelean gdev = devm_kzalloc(&pdev->dev, sizeof(struct uio_pci_generic_dev), GFP_KERNEL);
90ef84928cSAlexandru Ardelean if (!gdev)
91ef84928cSAlexandru Ardelean return -ENOMEM;
92ccb86a69SMichael S. Tsirkin
93ccb86a69SMichael S. Tsirkin gdev->info.name = "uio_pci_generic";
94ccb86a69SMichael S. Tsirkin gdev->info.version = DRIVER_VERSION;
95865a11f9SVenkatesh Srinivas gdev->info.release = release;
96acec09e6SJim Harris gdev->pdev = pdev;
9761de21a8SJie Li if (pdev->irq && (pdev->irq != IRQ_NOTCONNECTED)) {
98ccb86a69SMichael S. Tsirkin gdev->info.irq = pdev->irq;
99ccb86a69SMichael S. Tsirkin gdev->info.irq_flags = IRQF_SHARED;
100ccb86a69SMichael S. Tsirkin gdev->info.handler = irqhandler;
101acec09e6SJim Harris } else {
102acec09e6SJim Harris dev_warn(&pdev->dev, "No IRQ assigned to device: "
103acec09e6SJim Harris "no support for interrupts?\n");
104acec09e6SJim Harris }
105ccb86a69SMichael S. Tsirkin
106*e4e05016SFiras Ashkar uiomem = &gdev->info.mem[0];
107*e4e05016SFiras Ashkar for (i = 0; i < MAX_UIO_MAPS; ++i) {
108*e4e05016SFiras Ashkar struct resource *r = &pdev->resource[i];
109*e4e05016SFiras Ashkar
110*e4e05016SFiras Ashkar if (r->flags != (IORESOURCE_SIZEALIGN | IORESOURCE_MEM))
111*e4e05016SFiras Ashkar continue;
112*e4e05016SFiras Ashkar
113*e4e05016SFiras Ashkar if (uiomem >= &gdev->info.mem[MAX_UIO_MAPS]) {
114*e4e05016SFiras Ashkar dev_warn(
115*e4e05016SFiras Ashkar &pdev->dev,
116*e4e05016SFiras Ashkar "device has more than " __stringify(
117*e4e05016SFiras Ashkar MAX_UIO_MAPS) " I/O memory resources.\n");
118*e4e05016SFiras Ashkar break;
119*e4e05016SFiras Ashkar }
120*e4e05016SFiras Ashkar
121*e4e05016SFiras Ashkar uiomem->memtype = UIO_MEM_PHYS;
122*e4e05016SFiras Ashkar uiomem->addr = r->start & PAGE_MASK;
123*e4e05016SFiras Ashkar uiomem->offs = r->start & ~PAGE_MASK;
124*e4e05016SFiras Ashkar uiomem->size =
125*e4e05016SFiras Ashkar (uiomem->offs + resource_size(r) + PAGE_SIZE - 1) &
126*e4e05016SFiras Ashkar PAGE_MASK;
127*e4e05016SFiras Ashkar uiomem->name = r->name;
128*e4e05016SFiras Ashkar ++uiomem;
129*e4e05016SFiras Ashkar }
130*e4e05016SFiras Ashkar
131*e4e05016SFiras Ashkar while (uiomem < &gdev->info.mem[MAX_UIO_MAPS]) {
132*e4e05016SFiras Ashkar uiomem->size = 0;
133*e4e05016SFiras Ashkar ++uiomem;
134*e4e05016SFiras Ashkar }
135*e4e05016SFiras Ashkar
1364849e0edSAlexandru Ardelean return devm_uio_register_device(&pdev->dev, &gdev->info);
137ccb86a69SMichael S. Tsirkin }
138ccb86a69SMichael S. Tsirkin
139cd437398SPeter Huewe static struct pci_driver uio_pci_driver = {
140ccb86a69SMichael S. Tsirkin .name = "uio_pci_generic",
141ccb86a69SMichael S. Tsirkin .id_table = NULL, /* only dynamic id's */
142ccb86a69SMichael S. Tsirkin .probe = probe,
143ccb86a69SMichael S. Tsirkin };
144ccb86a69SMichael S. Tsirkin
145cd437398SPeter Huewe module_pci_driver(uio_pci_driver);
146ccb86a69SMichael S. Tsirkin MODULE_VERSION(DRIVER_VERSION);
147ccb86a69SMichael S. Tsirkin MODULE_LICENSE("GPL v2");
148ccb86a69SMichael S. Tsirkin MODULE_AUTHOR(DRIVER_AUTHOR);
149ccb86a69SMichael S. Tsirkin MODULE_DESCRIPTION(DRIVER_DESC);
150