138fe3975SBrett Creeley // SPDX-License-Identifier: GPL-2.0
238fe3975SBrett Creeley /* Copyright(c) 2023 Advanced Micro Devices, Inc. */
338fe3975SBrett Creeley
438fe3975SBrett Creeley #include <linux/vfio.h>
538fe3975SBrett Creeley #include <linux/vfio_pci_core.h>
638fe3975SBrett Creeley
7bb500dbeSBrett Creeley #include "lm.h"
8f232836aSBrett Creeley #include "dirty.h"
938fe3975SBrett Creeley #include "vfio_dev.h"
1038fe3975SBrett Creeley
pds_vfio_to_pci_dev(struct pds_vfio_pci_device * pds_vfio)1163f77a71SBrett Creeley struct pci_dev *pds_vfio_to_pci_dev(struct pds_vfio_pci_device *pds_vfio)
1263f77a71SBrett Creeley {
1363f77a71SBrett Creeley return pds_vfio->vfio_coredev.pdev;
1463f77a71SBrett Creeley }
1563f77a71SBrett Creeley
pds_vfio_to_dev(struct pds_vfio_pci_device * pds_vfio)16bb500dbeSBrett Creeley struct device *pds_vfio_to_dev(struct pds_vfio_pci_device *pds_vfio)
17bb500dbeSBrett Creeley {
18bb500dbeSBrett Creeley return &pds_vfio_to_pci_dev(pds_vfio)->dev;
19bb500dbeSBrett Creeley }
20bb500dbeSBrett Creeley
pds_vfio_pci_drvdata(struct pci_dev * pdev)2138fe3975SBrett Creeley struct pds_vfio_pci_device *pds_vfio_pci_drvdata(struct pci_dev *pdev)
2238fe3975SBrett Creeley {
2338fe3975SBrett Creeley struct vfio_pci_core_device *core_device = dev_get_drvdata(&pdev->dev);
2438fe3975SBrett Creeley
2538fe3975SBrett Creeley return container_of(core_device, struct pds_vfio_pci_device,
2638fe3975SBrett Creeley vfio_coredev);
2738fe3975SBrett Creeley }
2838fe3975SBrett Creeley
pds_vfio_state_mutex_unlock(struct pds_vfio_pci_device * pds_vfio)29f232836aSBrett Creeley void pds_vfio_state_mutex_unlock(struct pds_vfio_pci_device *pds_vfio)
30bb500dbeSBrett Creeley {
31bb500dbeSBrett Creeley again:
3272654bc1SBrett Creeley mutex_lock(&pds_vfio->reset_mutex);
33bb500dbeSBrett Creeley if (pds_vfio->deferred_reset) {
34bb500dbeSBrett Creeley pds_vfio->deferred_reset = false;
35bb500dbeSBrett Creeley pds_vfio_put_restore_file(pds_vfio);
36bb500dbeSBrett Creeley pds_vfio_put_save_file(pds_vfio);
37*245c318cSBrett Creeley if (pds_vfio->state == VFIO_DEVICE_STATE_ERROR) {
38f232836aSBrett Creeley pds_vfio_dirty_disable(pds_vfio, false);
39bb500dbeSBrett Creeley }
407dabb1bcSBrett Creeley pds_vfio->state = pds_vfio->deferred_reset_state;
417dabb1bcSBrett Creeley pds_vfio->deferred_reset_state = VFIO_DEVICE_STATE_RUNNING;
4272654bc1SBrett Creeley mutex_unlock(&pds_vfio->reset_mutex);
43bb500dbeSBrett Creeley goto again;
44bb500dbeSBrett Creeley }
45bb500dbeSBrett Creeley mutex_unlock(&pds_vfio->state_mutex);
4672654bc1SBrett Creeley mutex_unlock(&pds_vfio->reset_mutex);
47bb500dbeSBrett Creeley }
48bb500dbeSBrett Creeley
pds_vfio_reset(struct pds_vfio_pci_device * pds_vfio)49bb500dbeSBrett Creeley void pds_vfio_reset(struct pds_vfio_pci_device *pds_vfio)
50bb500dbeSBrett Creeley {
5172654bc1SBrett Creeley mutex_lock(&pds_vfio->reset_mutex);
52bb500dbeSBrett Creeley pds_vfio->deferred_reset = true;
537dabb1bcSBrett Creeley pds_vfio->deferred_reset_state = VFIO_DEVICE_STATE_RUNNING;
54bb500dbeSBrett Creeley if (!mutex_trylock(&pds_vfio->state_mutex)) {
5572654bc1SBrett Creeley mutex_unlock(&pds_vfio->reset_mutex);
56bb500dbeSBrett Creeley return;
57bb500dbeSBrett Creeley }
5872654bc1SBrett Creeley mutex_unlock(&pds_vfio->reset_mutex);
59bb500dbeSBrett Creeley pds_vfio_state_mutex_unlock(pds_vfio);
60bb500dbeSBrett Creeley }
61bb500dbeSBrett Creeley
62bb500dbeSBrett Creeley static struct file *
pds_vfio_set_device_state(struct vfio_device * vdev,enum vfio_device_mig_state new_state)63bb500dbeSBrett Creeley pds_vfio_set_device_state(struct vfio_device *vdev,
64bb500dbeSBrett Creeley enum vfio_device_mig_state new_state)
65bb500dbeSBrett Creeley {
66bb500dbeSBrett Creeley struct pds_vfio_pci_device *pds_vfio =
67bb500dbeSBrett Creeley container_of(vdev, struct pds_vfio_pci_device,
68bb500dbeSBrett Creeley vfio_coredev.vdev);
69bb500dbeSBrett Creeley struct file *res = NULL;
70bb500dbeSBrett Creeley
71bb500dbeSBrett Creeley mutex_lock(&pds_vfio->state_mutex);
727dabb1bcSBrett Creeley /*
737dabb1bcSBrett Creeley * only way to transition out of VFIO_DEVICE_STATE_ERROR is via
747dabb1bcSBrett Creeley * VFIO_DEVICE_RESET, so prevent the state machine from running since
757dabb1bcSBrett Creeley * vfio_mig_get_next_state() will throw a WARN_ON() when transitioning
767dabb1bcSBrett Creeley * from VFIO_DEVICE_STATE_ERROR to any other state
777dabb1bcSBrett Creeley */
787dabb1bcSBrett Creeley while (pds_vfio->state != VFIO_DEVICE_STATE_ERROR &&
797dabb1bcSBrett Creeley new_state != pds_vfio->state) {
80bb500dbeSBrett Creeley enum vfio_device_mig_state next_state;
81bb500dbeSBrett Creeley
82bb500dbeSBrett Creeley int err = vfio_mig_get_next_state(vdev, pds_vfio->state,
83bb500dbeSBrett Creeley new_state, &next_state);
84bb500dbeSBrett Creeley if (err) {
85bb500dbeSBrett Creeley res = ERR_PTR(err);
86bb500dbeSBrett Creeley break;
87bb500dbeSBrett Creeley }
88bb500dbeSBrett Creeley
89bb500dbeSBrett Creeley res = pds_vfio_step_device_state_locked(pds_vfio, next_state);
90bb500dbeSBrett Creeley if (IS_ERR(res))
91bb500dbeSBrett Creeley break;
92bb500dbeSBrett Creeley
93bb500dbeSBrett Creeley pds_vfio->state = next_state;
94bb500dbeSBrett Creeley
95bb500dbeSBrett Creeley if (WARN_ON(res && new_state != pds_vfio->state)) {
96bb500dbeSBrett Creeley res = ERR_PTR(-EINVAL);
97bb500dbeSBrett Creeley break;
98bb500dbeSBrett Creeley }
99bb500dbeSBrett Creeley }
100bb500dbeSBrett Creeley pds_vfio_state_mutex_unlock(pds_vfio);
1017dabb1bcSBrett Creeley /* still waiting on a deferred_reset */
1027dabb1bcSBrett Creeley if (pds_vfio->state == VFIO_DEVICE_STATE_ERROR)
1037dabb1bcSBrett Creeley res = ERR_PTR(-EIO);
104bb500dbeSBrett Creeley
105bb500dbeSBrett Creeley return res;
106bb500dbeSBrett Creeley }
107bb500dbeSBrett Creeley
pds_vfio_get_device_state(struct vfio_device * vdev,enum vfio_device_mig_state * current_state)108bb500dbeSBrett Creeley static int pds_vfio_get_device_state(struct vfio_device *vdev,
109bb500dbeSBrett Creeley enum vfio_device_mig_state *current_state)
110bb500dbeSBrett Creeley {
111bb500dbeSBrett Creeley struct pds_vfio_pci_device *pds_vfio =
112bb500dbeSBrett Creeley container_of(vdev, struct pds_vfio_pci_device,
113bb500dbeSBrett Creeley vfio_coredev.vdev);
114bb500dbeSBrett Creeley
115bb500dbeSBrett Creeley mutex_lock(&pds_vfio->state_mutex);
116bb500dbeSBrett Creeley *current_state = pds_vfio->state;
117bb500dbeSBrett Creeley pds_vfio_state_mutex_unlock(pds_vfio);
118bb500dbeSBrett Creeley return 0;
119bb500dbeSBrett Creeley }
120bb500dbeSBrett Creeley
pds_vfio_get_device_state_size(struct vfio_device * vdev,unsigned long * stop_copy_length)121bb500dbeSBrett Creeley static int pds_vfio_get_device_state_size(struct vfio_device *vdev,
122bb500dbeSBrett Creeley unsigned long *stop_copy_length)
123bb500dbeSBrett Creeley {
124bb500dbeSBrett Creeley *stop_copy_length = PDS_LM_DEVICE_STATE_LENGTH;
125bb500dbeSBrett Creeley return 0;
126bb500dbeSBrett Creeley }
127bb500dbeSBrett Creeley
128bb500dbeSBrett Creeley static const struct vfio_migration_ops pds_vfio_lm_ops = {
129bb500dbeSBrett Creeley .migration_set_state = pds_vfio_set_device_state,
130bb500dbeSBrett Creeley .migration_get_state = pds_vfio_get_device_state,
131bb500dbeSBrett Creeley .migration_get_data_size = pds_vfio_get_device_state_size
132bb500dbeSBrett Creeley };
133bb500dbeSBrett Creeley
134f232836aSBrett Creeley static const struct vfio_log_ops pds_vfio_log_ops = {
135f232836aSBrett Creeley .log_start = pds_vfio_dma_logging_start,
136f232836aSBrett Creeley .log_stop = pds_vfio_dma_logging_stop,
137f232836aSBrett Creeley .log_read_and_clear = pds_vfio_dma_logging_report,
138f232836aSBrett Creeley };
139f232836aSBrett Creeley
pds_vfio_init_device(struct vfio_device * vdev)14038fe3975SBrett Creeley static int pds_vfio_init_device(struct vfio_device *vdev)
14138fe3975SBrett Creeley {
14238fe3975SBrett Creeley struct pds_vfio_pci_device *pds_vfio =
14338fe3975SBrett Creeley container_of(vdev, struct pds_vfio_pci_device,
14438fe3975SBrett Creeley vfio_coredev.vdev);
14538fe3975SBrett Creeley struct pci_dev *pdev = to_pci_dev(vdev->dev);
14663f77a71SBrett Creeley int err, vf_id, pci_id;
14738fe3975SBrett Creeley
14838fe3975SBrett Creeley vf_id = pci_iov_vf_id(pdev);
14938fe3975SBrett Creeley if (vf_id < 0)
15038fe3975SBrett Creeley return vf_id;
15138fe3975SBrett Creeley
15238fe3975SBrett Creeley err = vfio_pci_core_init_dev(vdev);
15338fe3975SBrett Creeley if (err)
15438fe3975SBrett Creeley return err;
15538fe3975SBrett Creeley
15638fe3975SBrett Creeley pds_vfio->vf_id = vf_id;
15738fe3975SBrett Creeley
158e9e45c19SBrett Creeley mutex_init(&pds_vfio->state_mutex);
15972654bc1SBrett Creeley mutex_init(&pds_vfio->reset_mutex);
160e9e45c19SBrett Creeley
161bb500dbeSBrett Creeley vdev->migration_flags = VFIO_MIGRATION_STOP_COPY | VFIO_MIGRATION_P2P;
162bb500dbeSBrett Creeley vdev->mig_ops = &pds_vfio_lm_ops;
163f232836aSBrett Creeley vdev->log_ops = &pds_vfio_log_ops;
164bb500dbeSBrett Creeley
16563f77a71SBrett Creeley pci_id = PCI_DEVID(pdev->bus->number, pdev->devfn);
16663f77a71SBrett Creeley dev_dbg(&pdev->dev,
16763f77a71SBrett Creeley "%s: PF %#04x VF %#04x vf_id %d domain %d pds_vfio %p\n",
16827004f89SShixiong Ou __func__, pci_dev_id(pci_physfn(pdev)), pci_id, vf_id,
16963f77a71SBrett Creeley pci_domain_nr(pdev->bus), pds_vfio);
17063f77a71SBrett Creeley
17138fe3975SBrett Creeley return 0;
17238fe3975SBrett Creeley }
17338fe3975SBrett Creeley
pds_vfio_release_device(struct vfio_device * vdev)174e9e45c19SBrett Creeley static void pds_vfio_release_device(struct vfio_device *vdev)
175e9e45c19SBrett Creeley {
176e9e45c19SBrett Creeley struct pds_vfio_pci_device *pds_vfio =
177e9e45c19SBrett Creeley container_of(vdev, struct pds_vfio_pci_device,
178e9e45c19SBrett Creeley vfio_coredev.vdev);
179e9e45c19SBrett Creeley
180e9e45c19SBrett Creeley mutex_destroy(&pds_vfio->state_mutex);
18172654bc1SBrett Creeley mutex_destroy(&pds_vfio->reset_mutex);
182e9e45c19SBrett Creeley vfio_pci_core_release_dev(vdev);
183e9e45c19SBrett Creeley }
184e9e45c19SBrett Creeley
pds_vfio_open_device(struct vfio_device * vdev)18538fe3975SBrett Creeley static int pds_vfio_open_device(struct vfio_device *vdev)
18638fe3975SBrett Creeley {
18738fe3975SBrett Creeley struct pds_vfio_pci_device *pds_vfio =
18838fe3975SBrett Creeley container_of(vdev, struct pds_vfio_pci_device,
18938fe3975SBrett Creeley vfio_coredev.vdev);
19038fe3975SBrett Creeley int err;
19138fe3975SBrett Creeley
19238fe3975SBrett Creeley err = vfio_pci_core_enable(&pds_vfio->vfio_coredev);
19338fe3975SBrett Creeley if (err)
19438fe3975SBrett Creeley return err;
19538fe3975SBrett Creeley
196bb500dbeSBrett Creeley pds_vfio->state = VFIO_DEVICE_STATE_RUNNING;
1977dabb1bcSBrett Creeley pds_vfio->deferred_reset_state = VFIO_DEVICE_STATE_RUNNING;
198bb500dbeSBrett Creeley
19938fe3975SBrett Creeley vfio_pci_core_finish_enable(&pds_vfio->vfio_coredev);
20038fe3975SBrett Creeley
20138fe3975SBrett Creeley return 0;
20238fe3975SBrett Creeley }
20338fe3975SBrett Creeley
pds_vfio_close_device(struct vfio_device * vdev)204bb500dbeSBrett Creeley static void pds_vfio_close_device(struct vfio_device *vdev)
205bb500dbeSBrett Creeley {
206bb500dbeSBrett Creeley struct pds_vfio_pci_device *pds_vfio =
207bb500dbeSBrett Creeley container_of(vdev, struct pds_vfio_pci_device,
208bb500dbeSBrett Creeley vfio_coredev.vdev);
209bb500dbeSBrett Creeley
210bb500dbeSBrett Creeley mutex_lock(&pds_vfio->state_mutex);
211bb500dbeSBrett Creeley pds_vfio_put_restore_file(pds_vfio);
212bb500dbeSBrett Creeley pds_vfio_put_save_file(pds_vfio);
213f232836aSBrett Creeley pds_vfio_dirty_disable(pds_vfio, true);
214bb500dbeSBrett Creeley mutex_unlock(&pds_vfio->state_mutex);
215bb500dbeSBrett Creeley vfio_pci_core_close_device(vdev);
216bb500dbeSBrett Creeley }
217bb500dbeSBrett Creeley
21838fe3975SBrett Creeley static const struct vfio_device_ops pds_vfio_ops = {
21938fe3975SBrett Creeley .name = "pds-vfio",
22038fe3975SBrett Creeley .init = pds_vfio_init_device,
221e9e45c19SBrett Creeley .release = pds_vfio_release_device,
22238fe3975SBrett Creeley .open_device = pds_vfio_open_device,
223bb500dbeSBrett Creeley .close_device = pds_vfio_close_device,
22438fe3975SBrett Creeley .ioctl = vfio_pci_core_ioctl,
22538fe3975SBrett Creeley .device_feature = vfio_pci_core_ioctl_feature,
22638fe3975SBrett Creeley .read = vfio_pci_core_read,
22738fe3975SBrett Creeley .write = vfio_pci_core_write,
22838fe3975SBrett Creeley .mmap = vfio_pci_core_mmap,
22938fe3975SBrett Creeley .request = vfio_pci_core_request,
23038fe3975SBrett Creeley .match = vfio_pci_core_match,
23138fe3975SBrett Creeley .bind_iommufd = vfio_iommufd_physical_bind,
23238fe3975SBrett Creeley .unbind_iommufd = vfio_iommufd_physical_unbind,
23338fe3975SBrett Creeley .attach_ioas = vfio_iommufd_physical_attach_ioas,
23438fe3975SBrett Creeley };
23538fe3975SBrett Creeley
pds_vfio_ops_info(void)23638fe3975SBrett Creeley const struct vfio_device_ops *pds_vfio_ops_info(void)
23738fe3975SBrett Creeley {
23838fe3975SBrett Creeley return &pds_vfio_ops;
23938fe3975SBrett Creeley }
240