Re: [Intel-gfx] [PATCH v2 11/15] vfio/platform: Use the new device life cycle helpers

2022-09-07 Thread Eric Auger
Hi kevin,

On 9/1/22 16:37, Kevin Tian wrote:
> Move vfio_device_ops from platform core to platform drivers so device
> specific init/cleanup can be added.
>
> Introduce two new helpers vfio_platform_init/release_common() for the
> use in driver @init/@release.
>
> vfio_platform_probe/remove_common() will be deprecated.
>
> Signed-off-by: Kevin Tian 
> Reviewed-by: Jason Gunthorpe 
> ---
>  drivers/vfio/platform/vfio_platform.c | 66 +++
>  drivers/vfio/platform/vfio_platform_common.c  | 53 ---
>  drivers/vfio/platform/vfio_platform_private.h | 15 +
>  3 files changed, 111 insertions(+), 23 deletions(-)
>
> diff --git a/drivers/vfio/platform/vfio_platform.c 
> b/drivers/vfio/platform/vfio_platform.c
> index 04f40c5acfd6..82cedcebfd90 100644
> --- a/drivers/vfio/platform/vfio_platform.c
> +++ b/drivers/vfio/platform/vfio_platform.c
> @@ -7,6 +7,7 @@
>  #include 
>  #include 
>  #include 
> +#include 
>  #include 
>  
>  #include "vfio_platform_private.h"
> @@ -36,14 +37,11 @@ static int get_platform_irq(struct vfio_platform_device 
> *vdev, int i)
>   return platform_get_irq_optional(pdev, i);
>  }
>  
> -static int vfio_platform_probe(struct platform_device *pdev)
> +static int vfio_platform_init_dev(struct vfio_device *core_vdev)
>  {
> - struct vfio_platform_device *vdev;
> - int ret;
> -
> - vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
> - if (!vdev)
> - return -ENOMEM;
> + struct vfio_platform_device *vdev =
> + container_of(core_vdev, struct vfio_platform_device, vdev);
> + struct platform_device *pdev = to_platform_device(core_vdev->dev);
>  
>   vdev->opaque = (void *) pdev;
>   vdev->name = pdev->name;
> @@ -52,24 +50,64 @@ static int vfio_platform_probe(struct platform_device 
> *pdev)
>   vdev->get_irq = get_platform_irq;
>   vdev->reset_required = reset_required;
>  
> - ret = vfio_platform_probe_common(vdev, >dev);
> - if (ret) {
> - kfree(vdev);
> - return ret;
> - }
> + return vfio_platform_init_common(vdev);
> +}
> +
> +static const struct vfio_device_ops vfio_platform_ops;
> +static int vfio_platform_probe(struct platform_device *pdev)
> +{
> + struct vfio_platform_device *vdev;
> + int ret;
> +
> + vdev = vfio_alloc_device(vfio_platform_device, vdev, >dev,
> +  _platform_ops);
> + if (IS_ERR(vdev))
> + return PTR_ERR(vdev);
> +
> + ret = vfio_register_group_dev(>vdev);
> + if (ret)
> + goto out_put_vdev;
> +
> + pm_runtime_enable(>dev);
>   dev_set_drvdata(>dev, vdev);
>   return 0;
> +
> +out_put_vdev:
> + vfio_put_device(>vdev);
> + return ret;
> +}
> +
> +static void vfio_platform_release_dev(struct vfio_device *core_vdev)
> +{
> + struct vfio_platform_device *vdev =
> + container_of(core_vdev, struct vfio_platform_device, vdev);
> +
> + vfio_platform_release_common(vdev);
> + vfio_free_device(core_vdev);
>  }
>  
>  static int vfio_platform_remove(struct platform_device *pdev)
>  {
>   struct vfio_platform_device *vdev = dev_get_drvdata(>dev);
>  
> - vfio_platform_remove_common(vdev);
> - kfree(vdev);
> + vfio_unregister_group_dev(>vdev);
> + pm_runtime_disable(vdev->device);
> + vfio_put_device(>vdev);
>   return 0;
>  }
>  
> +static const struct vfio_device_ops vfio_platform_ops = {
> + .name   = "vfio-platform",
> + .init   = vfio_platform_init_dev,
> + .release= vfio_platform_release_dev,
> + .open_device= vfio_platform_open_device,
> + .close_device   = vfio_platform_close_device,
> + .ioctl  = vfio_platform_ioctl,
> + .read   = vfio_platform_read,
> + .write  = vfio_platform_write,
> + .mmap   = vfio_platform_mmap,
> +};
> +
>  static struct platform_driver vfio_platform_driver = {
>   .probe  = vfio_platform_probe,
>   .remove = vfio_platform_remove,
> diff --git a/drivers/vfio/platform/vfio_platform_common.c 
> b/drivers/vfio/platform/vfio_platform_common.c
> index 256f55b84e70..4c01bf0adebb 100644
> --- a/drivers/vfio/platform/vfio_platform_common.c
> +++ b/drivers/vfio/platform/vfio_platform_common.c
> @@ -218,7 +218,7 @@ static int vfio_platform_call_reset(struct 
> vfio_platform_device *vdev,
>   return -EINVAL;
>  }
>  
> -static void vfio_platform_close_device(struct vfio_device *core_vdev)
> +void vfio_platform_close_device(struct vfio_device *core_vdev)
>  {
>   struct vfio_platform_device *vdev =
>   container_of(core_vdev, struct vfio_platform_device, vdev);
> @@ -236,8 +236,9 @@ static void vfio_platform_close_device(struct vfio_device 
> *core_vdev)
>   vfio_platform_regions_cleanup(vdev);
>   vfio_platform_irq_cleanup(vdev);
>  }
> +EXPORT_SYMBOL_GPL(vfio_platform_close_device);
>  
> -static int vfio_platform_open_device(struct 

[Intel-gfx] [PATCH v2 11/15] vfio/platform: Use the new device life cycle helpers

2022-09-01 Thread Kevin Tian
Move vfio_device_ops from platform core to platform drivers so device
specific init/cleanup can be added.

Introduce two new helpers vfio_platform_init/release_common() for the
use in driver @init/@release.

vfio_platform_probe/remove_common() will be deprecated.

Signed-off-by: Kevin Tian 
Reviewed-by: Jason Gunthorpe 
---
 drivers/vfio/platform/vfio_platform.c | 66 +++
 drivers/vfio/platform/vfio_platform_common.c  | 53 ---
 drivers/vfio/platform/vfio_platform_private.h | 15 +
 3 files changed, 111 insertions(+), 23 deletions(-)

diff --git a/drivers/vfio/platform/vfio_platform.c 
b/drivers/vfio/platform/vfio_platform.c
index 04f40c5acfd6..82cedcebfd90 100644
--- a/drivers/vfio/platform/vfio_platform.c
+++ b/drivers/vfio/platform/vfio_platform.c
@@ -7,6 +7,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 
 #include "vfio_platform_private.h"
@@ -36,14 +37,11 @@ static int get_platform_irq(struct vfio_platform_device 
*vdev, int i)
return platform_get_irq_optional(pdev, i);
 }
 
-static int vfio_platform_probe(struct platform_device *pdev)
+static int vfio_platform_init_dev(struct vfio_device *core_vdev)
 {
-   struct vfio_platform_device *vdev;
-   int ret;
-
-   vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
-   if (!vdev)
-   return -ENOMEM;
+   struct vfio_platform_device *vdev =
+   container_of(core_vdev, struct vfio_platform_device, vdev);
+   struct platform_device *pdev = to_platform_device(core_vdev->dev);
 
vdev->opaque = (void *) pdev;
vdev->name = pdev->name;
@@ -52,24 +50,64 @@ static int vfio_platform_probe(struct platform_device *pdev)
vdev->get_irq = get_platform_irq;
vdev->reset_required = reset_required;
 
-   ret = vfio_platform_probe_common(vdev, >dev);
-   if (ret) {
-   kfree(vdev);
-   return ret;
-   }
+   return vfio_platform_init_common(vdev);
+}
+
+static const struct vfio_device_ops vfio_platform_ops;
+static int vfio_platform_probe(struct platform_device *pdev)
+{
+   struct vfio_platform_device *vdev;
+   int ret;
+
+   vdev = vfio_alloc_device(vfio_platform_device, vdev, >dev,
+_platform_ops);
+   if (IS_ERR(vdev))
+   return PTR_ERR(vdev);
+
+   ret = vfio_register_group_dev(>vdev);
+   if (ret)
+   goto out_put_vdev;
+
+   pm_runtime_enable(>dev);
dev_set_drvdata(>dev, vdev);
return 0;
+
+out_put_vdev:
+   vfio_put_device(>vdev);
+   return ret;
+}
+
+static void vfio_platform_release_dev(struct vfio_device *core_vdev)
+{
+   struct vfio_platform_device *vdev =
+   container_of(core_vdev, struct vfio_platform_device, vdev);
+
+   vfio_platform_release_common(vdev);
+   vfio_free_device(core_vdev);
 }
 
 static int vfio_platform_remove(struct platform_device *pdev)
 {
struct vfio_platform_device *vdev = dev_get_drvdata(>dev);
 
-   vfio_platform_remove_common(vdev);
-   kfree(vdev);
+   vfio_unregister_group_dev(>vdev);
+   pm_runtime_disable(vdev->device);
+   vfio_put_device(>vdev);
return 0;
 }
 
+static const struct vfio_device_ops vfio_platform_ops = {
+   .name   = "vfio-platform",
+   .init   = vfio_platform_init_dev,
+   .release= vfio_platform_release_dev,
+   .open_device= vfio_platform_open_device,
+   .close_device   = vfio_platform_close_device,
+   .ioctl  = vfio_platform_ioctl,
+   .read   = vfio_platform_read,
+   .write  = vfio_platform_write,
+   .mmap   = vfio_platform_mmap,
+};
+
 static struct platform_driver vfio_platform_driver = {
.probe  = vfio_platform_probe,
.remove = vfio_platform_remove,
diff --git a/drivers/vfio/platform/vfio_platform_common.c 
b/drivers/vfio/platform/vfio_platform_common.c
index 256f55b84e70..4c01bf0adebb 100644
--- a/drivers/vfio/platform/vfio_platform_common.c
+++ b/drivers/vfio/platform/vfio_platform_common.c
@@ -218,7 +218,7 @@ static int vfio_platform_call_reset(struct 
vfio_platform_device *vdev,
return -EINVAL;
 }
 
-static void vfio_platform_close_device(struct vfio_device *core_vdev)
+void vfio_platform_close_device(struct vfio_device *core_vdev)
 {
struct vfio_platform_device *vdev =
container_of(core_vdev, struct vfio_platform_device, vdev);
@@ -236,8 +236,9 @@ static void vfio_platform_close_device(struct vfio_device 
*core_vdev)
vfio_platform_regions_cleanup(vdev);
vfio_platform_irq_cleanup(vdev);
 }
+EXPORT_SYMBOL_GPL(vfio_platform_close_device);
 
-static int vfio_platform_open_device(struct vfio_device *core_vdev)
+int vfio_platform_open_device(struct vfio_device *core_vdev)
 {
struct vfio_platform_device *vdev =
container_of(core_vdev, struct vfio_platform_device, vdev);
@@