1 From a309870f6fa4a9f69ca7490b355d147b0caeffd0 Mon Sep 17 00:00:00 2001
2 From: Diana Craciun <diana.craciun@nxp.com>
3 Date: Fri, 13 Sep 2019 17:20:35 +0300
4 Subject: [PATCH] vfio/fsl-mc: Scan DPRC objects on vfio-fsl-mc driver bind
6 The DPRC(Data Path Resource Container) device is a bus device and has
7 child devices attached to it. When the vfio-fsl-mc driver is probed
8 the DPRC is scanned and the child devices discovered and initialized.
10 Signed-off-by: Bharat Bhushan <Bharat.Bhushan@nxp.com>
11 Signed-off-by: Diana Craciun <diana.craciun@nxp.com>
13 drivers/vfio/fsl-mc/vfio_fsl_mc.c | 99 +++++++++++++++++++++++++++++++++++++++
14 1 file changed, 99 insertions(+)
16 --- a/drivers/vfio/fsl-mc/vfio_fsl_mc.c
17 +++ b/drivers/vfio/fsl-mc/vfio_fsl_mc.c
18 @@ -74,7 +74,75 @@ static int vfio_fsl_mc_mmap(void *device
22 +static int vfio_fsl_mc_init_device(struct vfio_fsl_mc_device *vdev)
24 + struct fsl_mc_device *mc_dev = vdev->mc_dev;
25 + struct fsl_mc_bus *mc_bus;
28 + unsigned int irq_count;
30 + /* Non-dprc devices share mc_io from parent */
31 + if (!is_fsl_mc_bus_dprc(mc_dev)) {
32 + struct fsl_mc_device *mc_cont = to_fsl_mc_device(mc_dev->dev.parent);
34 + mc_dev->mc_io = mc_cont->mc_io;
38 + /* Use dprc mc-portal for interaction with MC f/w. */
39 + region_size = resource_size(mc_dev->regions);
40 + ret = fsl_create_mc_io(&mc_dev->dev,
41 + mc_dev->regions[0].start,
44 + FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
47 + dev_err(&mc_dev->dev, "failed to create mc-io (error = %d)\n", ret);
50 + ret = dprc_open(mc_dev->mc_io, 0, mc_dev->obj_desc.id,
51 + &mc_dev->mc_handle);
53 + dev_err(&mc_dev->dev, "dprc_open() failed: %d\n", ret);
57 + mc_bus = to_fsl_mc_bus(mc_dev);
58 + /* initialize resource pools */
59 + fsl_mc_init_all_resource_pools(mc_dev);
61 + mutex_init(&mc_bus->scan_mutex);
63 + mutex_lock(&mc_bus->scan_mutex);
64 + ret = dprc_scan_objects(mc_dev, mc_dev->driver_override, false,
66 + mutex_unlock(&mc_bus->scan_mutex);
69 + dev_err(&mc_dev->dev, "dprc_scan_objects() failed: %d\n", ret);
70 + goto clean_resource_pool;
73 + if (irq_count > FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS) {
74 + dev_warn(&mc_dev->dev,
75 + "IRQs needed (%u) exceed IRQs preallocated (%u)\n",
76 + irq_count, FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS);
82 + fsl_mc_cleanup_all_resource_pools(mc_dev);
83 + dprc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
86 + fsl_destroy_mc_io(mc_dev->mc_io);
87 + mc_dev->mc_io = NULL;
91 static const struct vfio_device_ops vfio_fsl_mc_ops = {
92 .name = "vfio-fsl-mc",
93 .open = vfio_fsl_mc_open,
94 @@ -113,8 +181,34 @@ static int vfio_fsl_mc_probe(struct fsl_
98 + ret = vfio_fsl_mc_init_device(vdev);
100 + vfio_iommu_group_put(group, dev);
106 +static int vfio_fsl_mc_device_remove(struct device *dev, void *data)
108 + struct fsl_mc_device *mc_dev;
110 + WARN_ON(dev == NULL);
111 + mc_dev = to_fsl_mc_device(dev);
112 + if (WARN_ON(mc_dev == NULL))
114 + fsl_mc_device_remove(mc_dev);
119 +static void vfio_fsl_mc_cleanup_dprc(struct fsl_mc_device *mc_dev)
121 + device_for_each_child(&mc_dev->dev, NULL, vfio_fsl_mc_device_remove);
122 + fsl_mc_cleanup_all_resource_pools(mc_dev);
123 + dprc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
124 + fsl_destroy_mc_io(mc_dev->mc_io);
127 static int vfio_fsl_mc_remove(struct fsl_mc_device *mc_dev)
129 @@ -125,6 +219,11 @@ static int vfio_fsl_mc_remove(struct fsl
133 + if (is_fsl_mc_bus_dprc(mc_dev))
134 + vfio_fsl_mc_cleanup_dprc(vdev->mc_dev);
136 + mc_dev->mc_io = NULL;
138 vfio_iommu_group_put(mc_dev->dev.iommu_group, dev);
139 devm_kfree(dev, vdev);