Hi,

Please find attached a kernel patch. This is based from a very old patch that never made it into the kernel in 2014. https://lkml.org/lkml/2014/10/20/295. I am not sure who else I should be adding to the Signed-off-by section.

I have modified it and tested it, so that it works against kernel 5.1.10.

Summary below:


   PCI: Introduce new device binding path using pci_dev.driver_override

    In order to bind PCI slots to specific drivers use:
pci=driver[xxxx:xx:xx.x]=foo,driver[xxxx:xx:xx.x]=bar,...

    The main use case for this is in relation to qemu passthrough
    of a pci device using IOMMU and vfio-pci.
    Example:
    The host has two identical devices. E.g. 2 AMD Vega GPUs,
    The user wishes to passthrough only one GPU.
    This new feature allows the user to select which GPU to passthrough.

    Signed-off-by: James Courtier-Dutton <ja...@superbug.co.uk>




commit bcb3011bb2e9906c669b249ab65312a03ecb041b
Author: James Courtier-Dutton <ja...@superbug.co.uk>
Date:   Sat Jun 15 13:29:41 2019 +0100

    PCI: Introduce new device binding path using pci_dev.driver_override
    
    In order to bind PCI slots to specific drivers use:
            pci=driver[xxxx:xx:xx.x]=foo,driver[xxxx:xx:xx.x]=bar,...
    
    The main use case for this is in relation to qemu passthrough
    of a pci device using IOMMU and vfio-pci.
    Example:
    The host has two identical devices. E.g. 2 AMD Vega GPUs,
    The user wishes to passthrough only one GPU.
    This new feature allows the user to select which GPU to passthrough.
    
    Signed-off-by: James Courtier-Dutton <ja...@superbug.co.uk>

diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index c7937f379d22..71df0fd19cc6 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -3439,6 +3439,10 @@
 		pcie_scan_all	Scan all possible PCIe devices.  Otherwise we
 				only look for one device below a PCIe downstream
 				port.
+		driver		Provide an override to the devid<->driver mapping
+				for a specific slot.
+				Bind PCI slot 0001:02:03.4 to vfio-pci by:
+				driver[0001:02:03.4]=vfio-pci
 		big_root_window	Try to add a big 64bit memory window to the PCIe
 				root complex on AMD CPUs. Some GFX hardware
 				can resize a BAR to allow access to all VRAM.
diff --git a/drivers/pci/bus.c b/drivers/pci/bus.c
index 5cb40b2518f9..983f1d524a26 100644
--- a/drivers/pci/bus.c
+++ b/drivers/pci/bus.c
@@ -14,6 +14,8 @@
 #include <linux/proc_fs.h>
 #include <linux/slab.h>
 
+#include <asm/setup.h>
+
 #include "pci.h"
 
 void pci_add_resource_offset(struct list_head *resources, struct resource *res,
@@ -206,6 +208,116 @@ static int pci_bus_alloc_from_region(struct pci_bus *bus, struct resource *res,
 	return -ENOMEM;
 }
 
+struct driver_override_entry {
+	u16 domain;
+	u8 bus;
+	u8 devfn;
+	char *driver_name;
+	struct list_head list;
+};
+
+static LIST_HEAD(driver_override_entries);
+
+static int pci_device_parse_driver_override(char *param, char *val,
+					    const char *unused, void *unused2)
+{
+	unsigned int domain, bus, dev, fn;
+	char  *buf;
+	struct driver_override_entry *entry;
+	int ret;
+
+	buf = kmalloc(COMMAND_LINE_SIZE, GFP_KERNEL);
+	if (!buf)
+		goto err_buf;
+
+	while (val) {
+		char *k = strchr(val, ',');
+
+		if (k)
+			*k++ = 0;
+
+		if (strncmp(val, "driver", 6)) {
+			val = k;
+			continue;
+		}
+
+		memset(buf, 0, COMMAND_LINE_SIZE);
+		ret = sscanf(val + 6, "[%4x:%2x:%2x.%2x]=%s",
+			     &domain, &bus, &dev, &fn, buf);
+		if (ret != 5) {
+			pr_warn("PCI: Invalid command line: %s\n", val);
+			val = k;
+			continue;
+		}
+
+		entry = kzalloc(sizeof(*entry), GFP_KERNEL);
+		if (!entry)
+			goto err_entry;
+
+		INIT_LIST_HEAD(&entry->list);
+		entry->domain = domain;
+		entry->bus = bus;
+		entry->devfn = PCI_DEVFN(dev, fn);
+		entry->driver_name = kstrdup(buf, GFP_KERNEL);
+		if (!entry->driver_name)
+			goto err_driver_name;
+
+		pr_info("PCI: driver_override added for driver_name: %s to [%04x:%02x:%02x.%x]\n",
+				entry->driver_name, domain, bus, dev, fn);
+		list_add_tail(&entry->list, &driver_override_entries);
+		val = k;
+	}
+
+	kfree(buf);
+	return 0;
+
+err_driver_name:
+	kfree(entry);
+
+err_entry:
+	kfree(buf);
+
+err_buf:
+	pr_err("PCI: Out of memory while parsing command line: %s\n", val);
+	return -ENOMEM;
+}
+
+static void pci_device_setup_driver_override(struct pci_dev *dev)
+{
+	static int parse_done;
+	struct driver_override_entry *entry;
+
+	if (!parse_done) {
+		char *cmdline = kstrdup(saved_command_line, GFP_KERNEL);
+
+		if (!cmdline)
+			goto err_out_of_mem;
+
+		parse_args("pci", cmdline, NULL,
+			   0, 0, 0, NULL, &pci_device_parse_driver_override);
+		kfree(cmdline);
+		parse_done = 1;
+	}
+
+	list_for_each_entry(entry, &driver_override_entries, list) {
+		if (pci_domain_nr(dev->bus) != entry->domain ||
+		    dev->bus->number != entry->bus ||
+		    dev->devfn != entry->devfn)
+			continue;
+
+		dev->driver_override = kstrdup(entry->driver_name, GFP_KERNEL);
+		if (!dev->driver_override)
+			goto err_out_of_mem;
+
+		break;
+	}
+
+	return;
+
+err_out_of_mem:
+	pr_err("PCI: Out of memory while setting up driver override\n");
+}
+
 /**
  * pci_bus_alloc_resource - allocate a resource from a parent bus
  * @bus: PCI bus
@@ -317,6 +429,7 @@ void pci_bus_add_device(struct pci_dev *dev)
 	 */
 	pcibios_bus_add_device(dev);
 	pci_fixup_device(pci_fixup_final, dev);
+	pci_device_setup_driver_override(dev);
 	pci_create_sysfs_dev_files(dev);
 	pci_proc_attach_device(dev);
 	pci_bridge_d3_update(dev);
diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index 766f5779db92..7dd5d2dfeee1 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -6261,6 +6261,8 @@ static int __init pci_setup(char *str)
 				pcie_bus_config = PCIE_BUS_PEER2PEER;
 			} else if (!strncmp(str, "pcie_scan_all", 13)) {
 				pci_add_flags(PCI_SCAN_ALL_PCIE_DEVS);
+			} else if (!strncmp(str, "driver", 6)) {
+				/* lazy evaluation by the pci subsystem */
 			} else if (!strncmp(str, "disable_acs_redir=", 18)) {
 				disable_acs_redir_param = str + 18;
 			} else {

_______________________________________________
vfio-users mailing list
vfio-users@redhat.com
https://www.redhat.com/mailman/listinfo/vfio-users

Reply via email to