ath79/mikrotik: use routerbootpart partitions
[oweals/openwrt.git] / target / linux / layerscape / patches-5.4 / 821-vfio-0002-vfio-fsl-mc-Scan-DPRC-objects-on-vfio-fsl-mc-driver-.patch
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
5
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.
9
10 Signed-off-by: Bharat Bhushan <Bharat.Bhushan@nxp.com>
11 Signed-off-by: Diana Craciun <diana.craciun@nxp.com>
12 ---
13  drivers/vfio/fsl-mc/vfio_fsl_mc.c | 99 +++++++++++++++++++++++++++++++++++++++
14  1 file changed, 99 insertions(+)
15
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
19  {
20         return -EINVAL;
21  }
22 +static int vfio_fsl_mc_init_device(struct vfio_fsl_mc_device *vdev)
23 +{
24 +       struct fsl_mc_device *mc_dev = vdev->mc_dev;
25 +       struct fsl_mc_bus *mc_bus;
26 +       size_t region_size;
27 +       int ret = 0;
28 +       unsigned int irq_count;
29 +
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);
33 +
34 +               mc_dev->mc_io = mc_cont->mc_io;
35 +               return 0;
36 +       }
37 +
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,
42 +                        region_size,
43 +                        NULL,
44 +                        FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
45 +                        &mc_dev->mc_io);
46 +       if (ret < 0) {
47 +               dev_err(&mc_dev->dev, "failed to create mc-io (error = %d)\n", ret);
48 +               return ret;
49 +       }
50 +       ret = dprc_open(mc_dev->mc_io, 0, mc_dev->obj_desc.id,
51 +                         &mc_dev->mc_handle);
52 +       if (ret < 0) {
53 +               dev_err(&mc_dev->dev, "dprc_open() failed: %d\n", ret);
54 +               goto clean_mc_io;
55 +       }
56  
57 +       mc_bus = to_fsl_mc_bus(mc_dev);
58 +       /* initialize resource pools */
59 +       fsl_mc_init_all_resource_pools(mc_dev);
60 +
61 +       mutex_init(&mc_bus->scan_mutex);
62 +
63 +       mutex_lock(&mc_bus->scan_mutex);
64 +       ret = dprc_scan_objects(mc_dev, mc_dev->driver_override, false,
65 +               &irq_count);
66 +       mutex_unlock(&mc_bus->scan_mutex);
67 +
68 +       if (ret < 0) {
69 +               dev_err(&mc_dev->dev, "dprc_scan_objects() failed: %d\n", ret);
70 +               goto clean_resource_pool;
71 +       }
72 +
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);
77 +       }
78 +
79 +return 0;
80 +
81 +clean_resource_pool:
82 +       fsl_mc_cleanup_all_resource_pools(mc_dev);
83 +       dprc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
84 +
85 +clean_mc_io:
86 +       fsl_destroy_mc_io(mc_dev->mc_io);
87 +       mc_dev->mc_io = NULL;
88 +
89 +       return ret;
90 +}
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_
95                 return ret;
96         }
97  
98 +       ret = vfio_fsl_mc_init_device(vdev);
99 +       if (ret) {
100 +               vfio_iommu_group_put(group, dev);
101 +               return ret;
102 +       }
103 +
104         return ret;
105  }
106 +static int vfio_fsl_mc_device_remove(struct device *dev, void *data)
107 +{
108 +       struct fsl_mc_device *mc_dev;
109 +
110 +       WARN_ON(dev == NULL);
111 +       mc_dev = to_fsl_mc_device(dev);
112 +       if (WARN_ON(mc_dev == NULL))
113 +               return -ENODEV;
114 +       fsl_mc_device_remove(mc_dev);
115 +
116 +       return 0;
117 +}
118 +
119 +static void vfio_fsl_mc_cleanup_dprc(struct fsl_mc_device *mc_dev)
120 +{
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);
125 +}
126  
127  static int vfio_fsl_mc_remove(struct fsl_mc_device *mc_dev)
128  {
129 @@ -125,6 +219,11 @@ static int vfio_fsl_mc_remove(struct fsl
130         if (!vdev)
131                 return -EINVAL;
132  
133 +       if (is_fsl_mc_bus_dprc(mc_dev))
134 +               vfio_fsl_mc_cleanup_dprc(vdev->mc_dev);
135 +
136 +       mc_dev->mc_io = NULL;
137 +
138         vfio_iommu_group_put(mc_dev->dev.iommu_group, dev);
139         devm_kfree(dev, vdev);
140