Re: [XEN][PATCH v11 11/20] xen/iommu: Introduce iommu_remove_dt_device()

2023-09-05 Thread Stefano Stabellini
On Mon, 4 Sep 2023, Michal Orzel wrote:
> On 01/09/2023 06:59, Vikram Garhwal wrote:
> > Remove master device from the IOMMU. This will be helpful when removing the
> > overlay nodes using dynamic programming during run time.
> > 
> > Signed-off-by: Vikram Garhwal 
> > 
> > ---
> > Changes from v10:
> > Add comment regarding return values of iommu_remove_dt_device().
> > Add ASSERT to check if is_protected is removed or not.
> > Changes from v7:
> > Add check if IOMMU is enabled.
> > Fix indentation of fail.
> > ---
> > ---
> >  xen/drivers/passthrough/device_tree.c | 43 +++
> >  xen/include/xen/iommu.h   | 10 +++
> >  2 files changed, 53 insertions(+)
> > 
> > diff --git a/xen/drivers/passthrough/device_tree.c 
> > b/xen/drivers/passthrough/device_tree.c
> > index 687c61e7da..80f6efc606 100644
> > --- a/xen/drivers/passthrough/device_tree.c
> > +++ b/xen/drivers/passthrough/device_tree.c
> > @@ -127,6 +127,49 @@ int iommu_release_dt_devices(struct domain *d)
> >  return 0;
> >  }
> >  
> > +int iommu_remove_dt_device(struct dt_device_node *np)
> > +{
> > +const struct iommu_ops *ops = iommu_get_ops();
> > +struct device *dev = dt_to_dev(np);
> > +int rc;
> > +
> > +if ( !iommu_enabled )
> > +return 1;
> > +
> > +if ( !ops )
> > +return -EOPNOTSUPP;
> > +
> > +spin_lock(_lock);
> > +
> > +if ( iommu_dt_device_is_assigned_locked(np) )
> > +{
> > +rc = -EBUSY;
> > +goto fail;
> > +}
> > +
> > +if ( !ops->remove_device )
> > +{
> > +rc = -EOPNOTSUPP;
> > +goto fail;
> > +}
> > +
> > +/*
> > + * De-register the device from the IOMMU driver.
> > + * The driver is responsible for removing is_protected flag.
> > + */
> > +rc = ops->remove_device(0, dev);
> > +
> > +if ( !rc )
> > +{
> > +ASSERT(!dt_device_is_protected(np));
> > +iommu_fwspec_free(dev);
> > +}
> > +
> > + fail:
> > +spin_unlock(_lock);
> > +return rc;
> > +}
> > +
> >  int iommu_add_dt_device(struct dt_device_node *np)
> >  {
> >  const struct iommu_ops *ops = iommu_get_ops();
> > diff --git a/xen/include/xen/iommu.h b/xen/include/xen/iommu.h
> > index a18b68e247..84bd77395e 100644
> > --- a/xen/include/xen/iommu.h
> > +++ b/xen/include/xen/iommu.h
> > @@ -235,6 +235,16 @@ int iommu_add_dt_device(struct dt_device_node *np);
> >  int iommu_do_dt_domctl(struct xen_domctl *domctl, struct domain *d,
> > XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl);
> >  
> > +/*
> > + * Helper to remove master device from the IOMMU.
> > + *
> > + * Return values:
> > + *  0 : device is de-registerd from IOMMU.
> s/registerd/registered/
> 
> > + * <0 : error while removing the device from IOMMU.
> > + * >0 : IOMMU is not enabled/present or device is not connected to it.
> The first part refers to "iommu_enabled" but I cannot see how you check for 
> the second part
> (and return >0).

Yes I would remove "or device is not connected to it". It can be done on
commit.


> Apart from that:
> Reviewed-by: Michal Orzel 

Acked-by: Stefano Stabellini 



Re: [XEN][PATCH v11 11/20] xen/iommu: Introduce iommu_remove_dt_device()

2023-09-04 Thread Michal Orzel



On 01/09/2023 06:59, Vikram Garhwal wrote:
> Remove master device from the IOMMU. This will be helpful when removing the
> overlay nodes using dynamic programming during run time.
> 
> Signed-off-by: Vikram Garhwal 
> 
> ---
> Changes from v10:
> Add comment regarding return values of iommu_remove_dt_device().
> Add ASSERT to check if is_protected is removed or not.
> Changes from v7:
> Add check if IOMMU is enabled.
> Fix indentation of fail.
> ---
> ---
>  xen/drivers/passthrough/device_tree.c | 43 +++
>  xen/include/xen/iommu.h   | 10 +++
>  2 files changed, 53 insertions(+)
> 
> diff --git a/xen/drivers/passthrough/device_tree.c 
> b/xen/drivers/passthrough/device_tree.c
> index 687c61e7da..80f6efc606 100644
> --- a/xen/drivers/passthrough/device_tree.c
> +++ b/xen/drivers/passthrough/device_tree.c
> @@ -127,6 +127,49 @@ int iommu_release_dt_devices(struct domain *d)
>  return 0;
>  }
>  
> +int iommu_remove_dt_device(struct dt_device_node *np)
> +{
> +const struct iommu_ops *ops = iommu_get_ops();
> +struct device *dev = dt_to_dev(np);
> +int rc;
> +
> +if ( !iommu_enabled )
> +return 1;
> +
> +if ( !ops )
> +return -EOPNOTSUPP;
> +
> +spin_lock(_lock);
> +
> +if ( iommu_dt_device_is_assigned_locked(np) )
> +{
> +rc = -EBUSY;
> +goto fail;
> +}
> +
> +if ( !ops->remove_device )
> +{
> +rc = -EOPNOTSUPP;
> +goto fail;
> +}
> +
> +/*
> + * De-register the device from the IOMMU driver.
> + * The driver is responsible for removing is_protected flag.
> + */
> +rc = ops->remove_device(0, dev);
> +
> +if ( !rc )
> +{
> +ASSERT(!dt_device_is_protected(np));
> +iommu_fwspec_free(dev);
> +}
> +
> + fail:
> +spin_unlock(_lock);
> +return rc;
> +}
> +
>  int iommu_add_dt_device(struct dt_device_node *np)
>  {
>  const struct iommu_ops *ops = iommu_get_ops();
> diff --git a/xen/include/xen/iommu.h b/xen/include/xen/iommu.h
> index a18b68e247..84bd77395e 100644
> --- a/xen/include/xen/iommu.h
> +++ b/xen/include/xen/iommu.h
> @@ -235,6 +235,16 @@ int iommu_add_dt_device(struct dt_device_node *np);
>  int iommu_do_dt_domctl(struct xen_domctl *domctl, struct domain *d,
> XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl);
>  
> +/*
> + * Helper to remove master device from the IOMMU.
> + *
> + * Return values:
> + *  0 : device is de-registerd from IOMMU.
s/registerd/registered/

> + * <0 : error while removing the device from IOMMU.
> + * >0 : IOMMU is not enabled/present or device is not connected to it.
The first part refers to "iommu_enabled" but I cannot see how you check for the 
second part
(and return >0).

Apart from that:
Reviewed-by: Michal Orzel 

~Michal



[XEN][PATCH v11 11/20] xen/iommu: Introduce iommu_remove_dt_device()

2023-08-31 Thread Vikram Garhwal
Remove master device from the IOMMU. This will be helpful when removing the
overlay nodes using dynamic programming during run time.

Signed-off-by: Vikram Garhwal 

---
Changes from v10:
Add comment regarding return values of iommu_remove_dt_device().
Add ASSERT to check if is_protected is removed or not.
Changes from v7:
Add check if IOMMU is enabled.
Fix indentation of fail.
---
---
 xen/drivers/passthrough/device_tree.c | 43 +++
 xen/include/xen/iommu.h   | 10 +++
 2 files changed, 53 insertions(+)

diff --git a/xen/drivers/passthrough/device_tree.c 
b/xen/drivers/passthrough/device_tree.c
index 687c61e7da..80f6efc606 100644
--- a/xen/drivers/passthrough/device_tree.c
+++ b/xen/drivers/passthrough/device_tree.c
@@ -127,6 +127,49 @@ int iommu_release_dt_devices(struct domain *d)
 return 0;
 }
 
+int iommu_remove_dt_device(struct dt_device_node *np)
+{
+const struct iommu_ops *ops = iommu_get_ops();
+struct device *dev = dt_to_dev(np);
+int rc;
+
+if ( !iommu_enabled )
+return 1;
+
+if ( !ops )
+return -EOPNOTSUPP;
+
+spin_lock(_lock);
+
+if ( iommu_dt_device_is_assigned_locked(np) )
+{
+rc = -EBUSY;
+goto fail;
+}
+
+if ( !ops->remove_device )
+{
+rc = -EOPNOTSUPP;
+goto fail;
+}
+
+/*
+ * De-register the device from the IOMMU driver.
+ * The driver is responsible for removing is_protected flag.
+ */
+rc = ops->remove_device(0, dev);
+
+if ( !rc )
+{
+ASSERT(!dt_device_is_protected(np));
+iommu_fwspec_free(dev);
+}
+
+ fail:
+spin_unlock(_lock);
+return rc;
+}
+
 int iommu_add_dt_device(struct dt_device_node *np)
 {
 const struct iommu_ops *ops = iommu_get_ops();
diff --git a/xen/include/xen/iommu.h b/xen/include/xen/iommu.h
index a18b68e247..84bd77395e 100644
--- a/xen/include/xen/iommu.h
+++ b/xen/include/xen/iommu.h
@@ -235,6 +235,16 @@ int iommu_add_dt_device(struct dt_device_node *np);
 int iommu_do_dt_domctl(struct xen_domctl *domctl, struct domain *d,
XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl);
 
+/*
+ * Helper to remove master device from the IOMMU.
+ *
+ * Return values:
+ *  0 : device is de-registerd from IOMMU.
+ * <0 : error while removing the device from IOMMU.
+ * >0 : IOMMU is not enabled/present or device is not connected to it.
+ */
+int iommu_remove_dt_device(struct dt_device_node *np);
+
 #endif /* HAS_DEVICE_TREE */
 
 struct page_info;
-- 
2.17.1