xref: /linux/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_driver.h (revision 53e5fe5f28df72dd5917b74665465d42b2f0e90e)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 #ifndef SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_VFIO_PCI_DRIVER_H
3 #define SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_VFIO_PCI_DRIVER_H
4 
5 #include <libvfio/iommu.h>
6 
7 struct vfio_pci_device;
8 
9 struct vfio_pci_driver_ops {
10 	const char *name;
11 
12 	/*
13 	 * Size of the driver's state structure overlaid on
14 	 * device->driver.region.vaddr
15 	 */
16 	u64 region_size;
17 
18 	/**
19 	 * @probe() - Check if the driver supports the given device.
20 	 *
21 	 * Return: 0 on success, non-0 on failure.
22 	 */
23 	int (*probe)(struct vfio_pci_device *device);
24 
25 	/**
26 	 * @init() - Initialize the driver for @device.
27 	 *
28 	 * Must be called after device->driver.region has been initialized.
29 	 */
30 	void (*init)(struct vfio_pci_device *device);
31 
32 	/**
33 	 * remove() - Deinitialize the driver for @device.
34 	 */
35 	void (*remove)(struct vfio_pci_device *device);
36 
37 	/**
38 	 * memcpy_start() - Kick off @count repeated memcpy operations from
39 	 * [@src, @src + @size) to [@dst, @dst + @size).
40 	 *
41 	 * Guarantees:
42 	 *  - The device will attempt DMA reads on [src, src + size).
43 	 *  - The device will attempt DMA writes on [dst, dst + size).
44 	 *  - The device will not generate any interrupts.
45 	 *
46 	 * memcpy_start() returns immediately, it does not wait for the
47 	 * copies to complete.
48 	 */
49 	void (*memcpy_start)(struct vfio_pci_device *device,
50 			     iova_t src, iova_t dst, u64 size, u64 count);
51 
52 	/**
53 	 * memcpy_wait() - Wait until the memcpy operations started by
54 	 * memcpy_start() have finished.
55 	 *
56 	 * Guarantees:
57 	 *  - All in-flight DMAs initiated by memcpy_start() are fully complete
58 	 *    before memcpy_wait() returns.
59 	 *
60 	 * Returns non-0 if the driver detects that an error occurred during the
61 	 * memcpy, 0 otherwise.
62 	 */
63 	int (*memcpy_wait)(struct vfio_pci_device *device);
64 
65 	/**
66 	 * send_msi() - Make the device send the MSI device->driver.msi.
67 	 *
68 	 * Guarantees:
69 	 *  - The device will send the MSI once.
70 	 */
71 	void (*send_msi)(struct vfio_pci_device *device);
72 };
73 
74 struct vfio_pci_driver {
75 	const struct vfio_pci_driver_ops *ops;
76 	bool initialized;
77 	bool memcpy_in_progress;
78 
79 	/* Region to be used by the driver (e.g. for in-memory descriptors) */
80 	struct dma_region region;
81 
82 	/* The maximum size that can be passed to memcpy_start(). */
83 	u64 max_memcpy_size;
84 
85 	/* The maximum count that can be passed to memcpy_start(). */
86 	u64 max_memcpy_count;
87 
88 	/* The MSI vector the device will signal in ops->send_msi(). */
89 	int msi;
90 };
91 
92 void vfio_pci_driver_probe(struct vfio_pci_device *device);
93 void vfio_pci_driver_init(struct vfio_pci_device *device);
94 void vfio_pci_driver_remove(struct vfio_pci_device *device);
95 int vfio_pci_driver_memcpy(struct vfio_pci_device *device,
96 			   iova_t src, iova_t dst, u64 size);
97 void vfio_pci_driver_memcpy_start(struct vfio_pci_device *device,
98 				  iova_t src, iova_t dst, u64 size,
99 				  u64 count);
100 int vfio_pci_driver_memcpy_wait(struct vfio_pci_device *device);
101 void vfio_pci_driver_send_msi(struct vfio_pci_device *device);
102 
103 #endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_VFIO_PCI_DRIVER_H */
104