Skip to content

Commit 7fa005c

Browse files
mgurtovoyAlex Williamson
authored andcommitted
vfio/pci: Introduce vfio_pci_core.ko
Now that vfio_pci has been split into two source modules, one focusing on the "struct pci_driver" (vfio_pci.c) and a toolbox library of code (vfio_pci_core.c), complete the split and move them into two different kernel modules. As before vfio_pci.ko continues to present the same interface under sysfs and this change will have no functional impact. Splitting into another module and adding exports allows creating new HW specific VFIO PCI drivers that can implement device specific functionality, such as VFIO migration interfaces or specialized device requirements. Signed-off-by: Max Gurtovoy <mgurtovoy@nvidia.com> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Yishai Hadas <yishaih@nvidia.com> Link: https://lore.kernel.org/r/20210826103912.128972-14-yishaih@nvidia.com Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
1 parent 85c94dc commit 7fa005c

11 files changed

Lines changed: 65 additions & 42 deletions

File tree

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19466,6 +19466,7 @@ T: git git://github.com/awilliam/linux-vfio.git
1946619466
F: Documentation/driver-api/vfio.rst
1946719467
F: drivers/vfio/
1946819468
F: include/linux/vfio.h
19469+
F: include/linux/vfio_pci_core.h
1946919470
F: include/uapi/linux/vfio.h
1947019471

1947119472
VFIO FSL-MC DRIVER

drivers/vfio/pci/Kconfig

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
# SPDX-License-Identifier: GPL-2.0-only
2-
config VFIO_PCI
3-
tristate "VFIO support for PCI devices"
4-
depends on PCI
5-
depends on MMU
2+
if PCI && MMU
3+
config VFIO_PCI_CORE
4+
tristate
65
select VFIO_VIRQFD
76
select IRQ_BYPASS_MANAGER
7+
8+
config VFIO_PCI_MMAP
9+
def_bool y if !S390
10+
11+
config VFIO_PCI_INTX
12+
def_bool y if !S390
13+
14+
config VFIO_PCI
15+
tristate "Generic VFIO support for any PCI device"
16+
select VFIO_PCI_CORE
817
help
9-
Support for the PCI VFIO bus driver. This is required to make
10-
use of PCI drivers using the VFIO framework.
18+
Support for the generic PCI VFIO bus driver which can connect any
19+
PCI device to the VFIO framework.
1120

1221
If you don't know what to do here, say N.
1322

1423
if VFIO_PCI
1524
config VFIO_PCI_VGA
16-
bool "VFIO PCI support for VGA devices"
25+
bool "Generic VFIO PCI support for VGA devices"
1726
depends on X86 && VGA_ARB
1827
help
1928
Support for VGA extension to VFIO PCI. This exposes an additional
@@ -22,14 +31,8 @@ config VFIO_PCI_VGA
2231

2332
If you don't know what to do here, say N.
2433

25-
config VFIO_PCI_MMAP
26-
def_bool y if !S390
27-
28-
config VFIO_PCI_INTX
29-
def_bool y if !S390
30-
3134
config VFIO_PCI_IGD
32-
bool "VFIO PCI extensions for Intel graphics (GVT-d)"
35+
bool "Generic VFIO PCI extensions for Intel graphics (GVT-d)"
3336
depends on X86
3437
default y
3538
help
@@ -39,5 +42,5 @@ config VFIO_PCI_IGD
3942
and LPC bridge config space.
4043

4144
To enable Intel IGD assignment through vfio-pci, say Y.
42-
45+
endif
4346
endif

drivers/vfio/pci/Makefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# SPDX-License-Identifier: GPL-2.0-only
22

3-
vfio-pci-y := vfio_pci.o vfio_pci_core.o vfio_pci_intrs.o vfio_pci_rdwr.o vfio_pci_config.o
4-
vfio-pci-$(CONFIG_VFIO_PCI_IGD) += vfio_pci_igd.o
5-
vfio-pci-$(CONFIG_S390) += vfio_pci_zdev.o
3+
vfio-pci-core-y := vfio_pci_core.o vfio_pci_intrs.o vfio_pci_rdwr.o vfio_pci_config.o
4+
vfio-pci-core-$(CONFIG_S390) += vfio_pci_zdev.o
5+
obj-$(CONFIG_VFIO_PCI_CORE) += vfio-pci-core.o
66

7+
vfio-pci-y := vfio_pci.o
8+
vfio-pci-$(CONFIG_VFIO_PCI_IGD) += vfio_pci_igd.o
79
obj-$(CONFIG_VFIO_PCI) += vfio-pci.o

drivers/vfio/pci/vfio_pci.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include <linux/types.h>
2626
#include <linux/uaccess.h>
2727

28-
#include "vfio_pci_core.h"
28+
#include <linux/vfio_pci_core.h>
2929

3030
#define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
3131
#define DRIVER_DESC "VFIO PCI - User Level meta-driver"
@@ -153,6 +153,7 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
153153
ret = vfio_pci_core_register_device(vdev);
154154
if (ret)
155155
goto out_free;
156+
dev_set_drvdata(&pdev->dev, vdev);
156157
return 0;
157158

158159
out_free:
@@ -246,32 +247,23 @@ static int __init vfio_pci_init(void)
246247

247248
vfio_pci_core_set_params(nointxmask, is_disable_vga, disable_idle_d3);
248249

249-
ret = vfio_pci_core_init();
250-
if (ret)
251-
return ret;
252-
253250
/* Register and scan for devices */
254251
ret = pci_register_driver(&vfio_pci_driver);
255252
if (ret)
256-
goto out;
253+
return ret;
257254

258255
vfio_pci_fill_ids();
259256

260257
if (disable_denylist)
261258
pr_warn("device denylist disabled.\n");
262259

263260
return 0;
264-
265-
out:
266-
vfio_pci_core_cleanup();
267-
return ret;
268261
}
269262
module_init(vfio_pci_init);
270263

271264
static void __exit vfio_pci_cleanup(void)
272265
{
273266
pci_unregister_driver(&vfio_pci_driver);
274-
vfio_pci_core_cleanup();
275267
}
276268
module_exit(vfio_pci_cleanup);
277269

drivers/vfio/pci/vfio_pci_config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include <linux/vfio.h>
2727
#include <linux/slab.h>
2828

29-
#include "vfio_pci_core.h"
29+
#include <linux/vfio_pci_core.h>
3030

3131
/* Fake capability ID for standard config space */
3232
#define PCI_CAP_ID_BASIC 0

drivers/vfio/pci/vfio_pci_core.c

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* Author: Tom Lyon, pugs@cisco.com
99
*/
1010

11+
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12+
1113
#include <linux/device.h>
1214
#include <linux/eventfd.h>
1315
#include <linux/file.h>
@@ -25,7 +27,10 @@
2527
#include <linux/nospec.h>
2628
#include <linux/sched/mm.h>
2729

28-
#include "vfio_pci_core.h"
30+
#include <linux/vfio_pci_core.h>
31+
32+
#define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
33+
#define DRIVER_DESC "core driver for VFIO based PCI devices"
2934

3035
static bool nointxmask;
3136
static bool disable_vga;
@@ -306,6 +311,7 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)
306311

307312
return 0;
308313
}
314+
EXPORT_SYMBOL_GPL(vfio_pci_core_enable);
309315

310316
void vfio_pci_core_disable(struct vfio_pci_core_device *vdev)
311317
{
@@ -403,6 +409,7 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev)
403409
if (!vfio_pci_dev_set_try_reset(vdev->vdev.dev_set) && !disable_idle_d3)
404410
vfio_pci_set_power_state(vdev, PCI_D3hot);
405411
}
412+
EXPORT_SYMBOL_GPL(vfio_pci_core_disable);
406413

407414
static struct vfio_pci_core_device *get_pf_vdev(struct vfio_pci_core_device *vdev)
408415
{
@@ -459,13 +466,15 @@ void vfio_pci_core_close_device(struct vfio_device *core_vdev)
459466
}
460467
mutex_unlock(&vdev->igate);
461468
}
469+
EXPORT_SYMBOL_GPL(vfio_pci_core_close_device);
462470

463471
void vfio_pci_core_finish_enable(struct vfio_pci_core_device *vdev)
464472
{
465473
vfio_pci_probe_mmaps(vdev);
466474
vfio_spapr_pci_eeh_open(vdev->pdev);
467475
vfio_pci_vf_token_user_add(vdev, 1);
468476
}
477+
EXPORT_SYMBOL_GPL(vfio_pci_core_finish_enable);
469478

470479
static int vfio_pci_get_irq_count(struct vfio_pci_core_device *vdev, int irq_type)
471480
{
@@ -624,6 +633,7 @@ int vfio_pci_register_dev_region(struct vfio_pci_core_device *vdev,
624633

625634
return 0;
626635
}
636+
EXPORT_SYMBOL_GPL(vfio_pci_register_dev_region);
627637

628638
long vfio_pci_core_ioctl(struct vfio_device *core_vdev, unsigned int cmd,
629639
unsigned long arg)
@@ -1168,6 +1178,7 @@ long vfio_pci_core_ioctl(struct vfio_device *core_vdev, unsigned int cmd,
11681178

11691179
return -ENOTTY;
11701180
}
1181+
EXPORT_SYMBOL_GPL(vfio_pci_core_ioctl);
11711182

11721183
static ssize_t vfio_pci_rw(struct vfio_pci_core_device *vdev, char __user *buf,
11731184
size_t count, loff_t *ppos, bool iswrite)
@@ -1211,6 +1222,7 @@ ssize_t vfio_pci_core_read(struct vfio_device *core_vdev, char __user *buf,
12111222

12121223
return vfio_pci_rw(vdev, buf, count, ppos, false);
12131224
}
1225+
EXPORT_SYMBOL_GPL(vfio_pci_core_read);
12141226

12151227
ssize_t vfio_pci_core_write(struct vfio_device *core_vdev, const char __user *buf,
12161228
size_t count, loff_t *ppos)
@@ -1223,6 +1235,7 @@ ssize_t vfio_pci_core_write(struct vfio_device *core_vdev, const char __user *bu
12231235

12241236
return vfio_pci_rw(vdev, (char __user *)buf, count, ppos, true);
12251237
}
1238+
EXPORT_SYMBOL_GPL(vfio_pci_core_write);
12261239

12271240
/* Return 1 on zap and vma_lock acquired, 0 on contention (only with @try) */
12281241
static int vfio_pci_zap_and_vma_lock(struct vfio_pci_core_device *vdev, bool try)
@@ -1501,6 +1514,7 @@ int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma
15011514

15021515
return 0;
15031516
}
1517+
EXPORT_SYMBOL_GPL(vfio_pci_core_mmap);
15041518

15051519
void vfio_pci_core_request(struct vfio_device *core_vdev, unsigned int count)
15061520
{
@@ -1523,6 +1537,7 @@ void vfio_pci_core_request(struct vfio_device *core_vdev, unsigned int count)
15231537

15241538
mutex_unlock(&vdev->igate);
15251539
}
1540+
EXPORT_SYMBOL_GPL(vfio_pci_core_request);
15261541

15271542
static int vfio_pci_validate_vf_token(struct vfio_pci_core_device *vdev,
15281543
bool vf_token, uuid_t *uuid)
@@ -1667,6 +1682,7 @@ int vfio_pci_core_match(struct vfio_device *core_vdev, char *buf)
16671682

16681683
return 1; /* Match */
16691684
}
1685+
EXPORT_SYMBOL_GPL(vfio_pci_core_match);
16701686

16711687
static int vfio_pci_bus_notifier(struct notifier_block *nb,
16721688
unsigned long action, void *data)
@@ -1775,6 +1791,7 @@ void vfio_pci_core_init_device(struct vfio_pci_core_device *vdev,
17751791
INIT_LIST_HEAD(&vdev->vma_list);
17761792
init_rwsem(&vdev->memory_lock);
17771793
}
1794+
EXPORT_SYMBOL_GPL(vfio_pci_core_init_device);
17781795

17791796
void vfio_pci_core_uninit_device(struct vfio_pci_core_device *vdev)
17801797
{
@@ -1785,6 +1802,7 @@ void vfio_pci_core_uninit_device(struct vfio_pci_core_device *vdev)
17851802
kfree(vdev->region);
17861803
kfree(vdev->pm_save);
17871804
}
1805+
EXPORT_SYMBOL_GPL(vfio_pci_core_uninit_device);
17881806

17891807
int vfio_pci_core_register_device(struct vfio_pci_core_device *vdev)
17901808
{
@@ -1852,7 +1870,6 @@ int vfio_pci_core_register_device(struct vfio_pci_core_device *vdev)
18521870
ret = vfio_register_group_dev(&vdev->vdev);
18531871
if (ret)
18541872
goto out_power;
1855-
dev_set_drvdata(&pdev->dev, vdev);
18561873
return 0;
18571874

18581875
out_power:
@@ -1864,6 +1881,7 @@ int vfio_pci_core_register_device(struct vfio_pci_core_device *vdev)
18641881
vfio_iommu_group_put(group, &pdev->dev);
18651882
return ret;
18661883
}
1884+
EXPORT_SYMBOL_GPL(vfio_pci_core_register_device);
18671885

18681886
void vfio_pci_core_unregister_device(struct vfio_pci_core_device *vdev)
18691887
{
@@ -1881,6 +1899,7 @@ void vfio_pci_core_unregister_device(struct vfio_pci_core_device *vdev)
18811899
if (!disable_idle_d3)
18821900
vfio_pci_set_power_state(vdev, PCI_D0);
18831901
}
1902+
EXPORT_SYMBOL_GPL(vfio_pci_core_unregister_device);
18841903

18851904
static pci_ers_result_t vfio_pci_aer_err_detected(struct pci_dev *pdev,
18861905
pci_channel_state_t state)
@@ -1924,10 +1943,12 @@ int vfio_pci_core_sriov_configure(struct pci_dev *pdev, int nr_virtfn)
19241943

19251944
return ret < 0 ? ret : nr_virtfn;
19261945
}
1946+
EXPORT_SYMBOL_GPL(vfio_pci_core_sriov_configure);
19271947

19281948
const struct pci_error_handlers vfio_pci_core_err_handlers = {
19291949
.error_detected = vfio_pci_aer_err_detected,
19301950
};
1951+
EXPORT_SYMBOL_GPL(vfio_pci_core_err_handlers);
19311952

19321953
static bool vfio_dev_in_groups(struct vfio_pci_core_device *vdev,
19331954
struct vfio_pci_group_info *groups)
@@ -2116,16 +2137,22 @@ void vfio_pci_core_set_params(bool is_nointxmask, bool is_disable_vga,
21162137
disable_vga = is_disable_vga;
21172138
disable_idle_d3 = is_disable_idle_d3;
21182139
}
2140+
EXPORT_SYMBOL_GPL(vfio_pci_core_set_params);
21192141

2120-
/* This will become the __exit function of vfio_pci_core.ko */
2121-
void vfio_pci_core_cleanup(void)
2142+
static void vfio_pci_core_cleanup(void)
21222143
{
21232144
vfio_pci_uninit_perm_bits();
21242145
}
21252146

2126-
/* This will become the __init function of vfio_pci_core.ko */
2127-
int __init vfio_pci_core_init(void)
2147+
static int __init vfio_pci_core_init(void)
21282148
{
21292149
/* Allocate shared config space permission data used by all devices */
21302150
return vfio_pci_init_perm_bits();
21312151
}
2152+
2153+
module_init(vfio_pci_core_init);
2154+
module_exit(vfio_pci_core_cleanup);
2155+
2156+
MODULE_LICENSE("GPL v2");
2157+
MODULE_AUTHOR(DRIVER_AUTHOR);
2158+
MODULE_DESCRIPTION(DRIVER_DESC);

drivers/vfio/pci/vfio_pci_igd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include <linux/uaccess.h>
1616
#include <linux/vfio.h>
1717

18-
#include "vfio_pci_core.h"
18+
#include <linux/vfio_pci_core.h>
1919

2020
#define OPREGION_SIGNATURE "IntelGraphicsMem"
2121
#define OPREGION_SIZE (8 * 1024)

drivers/vfio/pci/vfio_pci_intrs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include <linux/wait.h>
2121
#include <linux/slab.h>
2222

23-
#include "vfio_pci_core.h"
23+
#include <linux/vfio_pci_core.h>
2424

2525
/*
2626
* INTx

drivers/vfio/pci/vfio_pci_rdwr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <linux/vfio.h>
1818
#include <linux/vgaarb.h>
1919

20-
#include "vfio_pci_core.h"
20+
#include <linux/vfio_pci_core.h>
2121

2222
#ifdef __LITTLE_ENDIAN
2323
#define vfio_ioread64 ioread64

drivers/vfio/pci/vfio_pci_zdev.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include <asm/pci_clp.h>
2020
#include <asm/pci_io.h>
2121

22-
#include "vfio_pci_core.h"
22+
#include <linux/vfio_pci_core.h>
2323

2424
/*
2525
* Add the Base PCI Function information to the device info region.

0 commit comments

Comments
 (0)