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