All of lore.kernel.org
 help / color / mirror / Atom feed
From: Julien Grall <julien.grall@linaro.org>
To: xen-devel@lists.xenproject.org
Cc: ian.campbell@citrix.com, Julien Grall <julien.grall@linaro.org>,
	tim@xen.org, stefano.stabellini@citrix.com,
	Jan Beulich <jbeulich@suse.com>,
	Xiantao Zhang <xiantao.zhang@intel.com>
Subject: [PATCH v4 14/21] xen/passthrough: iommu: Basic support of device tree assignment
Date: Tue, 22 Apr 2014 14:14:28 +0100	[thread overview]
Message-ID: <1398172475-27873-15-git-send-email-julien.grall@linaro.org> (raw)
In-Reply-To: <1398172475-27873-1-git-send-email-julien.grall@linaro.org>

Add IOMMU helpers to support device tree assignment/deassignment. This patch
introduces 2 new fields in the dt_device_node:
    - is_protected: Does the device is protected by an IOMMU
    - domain_list: Pointer to the next device assigned to the same
    domain

This commit contains only support to protected a device with DOM0.
Device passthrough to another guest won't work out-of-box.

Signed-off-by: Julien Grall <julien.grall@linaro.org>
Acked-by: Jan Beulich <jbeulich@suse.com>
[for existing files]
Cc: Xiantao Zhang <xiantao.zhang@intel.com>

---
    Changes in v4:
        - Use hardware_domain instead of dom0 (recently changes in Xen)
        - iommu_deassign_dt_device: add back to dom0 list the device
        - add comments about the current support of DT device
        passthrough
        - update commit message
        - rename next_assigned to domain_list
    Changes in v3:
        - Remove iommu_dt_domain_{init,destroy} call in common code. Let
        architecture code to call them
        - Fix indentation in xen/include/xen/hvm/iommu.h
    Changes in v2:
        - Patch added
---
 xen/common/device_tree.c              |    4 ++
 xen/drivers/passthrough/Makefile      |    1 +
 xen/drivers/passthrough/device_tree.c |  111 +++++++++++++++++++++++++++++++++
 xen/include/xen/device_tree.h         |   14 +++++
 xen/include/xen/hvm/iommu.h           |    6 ++
 xen/include/xen/iommu.h               |   16 +++++
 6 files changed, 152 insertions(+)
 create mode 100644 xen/drivers/passthrough/device_tree.c

diff --git a/xen/common/device_tree.c b/xen/common/device_tree.c
index 6e8e263..f0b17a3 100644
--- a/xen/common/device_tree.c
+++ b/xen/common/device_tree.c
@@ -1695,6 +1695,10 @@ static unsigned long __init unflatten_dt_node(const void *fdt,
         np->full_name = ((char *)np) + sizeof(struct dt_device_node);
         /* By default dom0 owns the device */
         np->used_by = 0;
+        /* By default the device is not protected */
+        np->is_protected = false;
+        INIT_LIST_HEAD(&np->domain_list);
+
         if ( new_format )
         {
             char *fn = np->full_name;
diff --git a/xen/drivers/passthrough/Makefile b/xen/drivers/passthrough/Makefile
index 6e08f89..5a0a35e 100644
--- a/xen/drivers/passthrough/Makefile
+++ b/xen/drivers/passthrough/Makefile
@@ -5,3 +5,4 @@ subdir-$(x86_64) += x86
 obj-y += iommu.o
 obj-$(x86) += io.o
 obj-$(HAS_PCI) += pci.o
+obj-$(HAS_DEVICE_TREE) += device_tree.o
diff --git a/xen/drivers/passthrough/device_tree.c b/xen/drivers/passthrough/device_tree.c
new file mode 100644
index 0000000..ae05b19
--- /dev/null
+++ b/xen/drivers/passthrough/device_tree.c
@@ -0,0 +1,111 @@
+/*
+ * xen/drivers/passthrough/arm/device_tree.c
+ *
+ * Code to passthrough device tree node to a guest
+ *
+ * TODO: This contains only necessary code to protected device with
+ * dom0. It will need some updates when device passthrough will be
+ * added.
+ *
+ * Julien Grall <julien.grall@linaro.org>
+ * Copyright (c) 2014 Linaro Limited.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <xen/lib.h>
+#include <xen/sched.h>
+#include <xen/iommu.h>
+#include <xen/device_tree.h>
+
+static spinlock_t dtdevs_lock = SPIN_LOCK_UNLOCKED;
+
+int iommu_assign_dt_device(struct domain *d, struct dt_device_node *dev)
+{
+    int rc = -EBUSY;
+    struct hvm_iommu *hd = domain_hvm_iommu(d);
+
+    if ( !iommu_enabled || !hd->platform_ops )
+        return -EINVAL;
+
+    if ( !dt_device_is_protected(dev) )
+        return -EINVAL;
+
+    spin_lock(&dtdevs_lock);
+
+    if ( !list_empty(&dev->domain_list) )
+        goto fail;
+
+    rc = hd->platform_ops->assign_dt_device(d, dev);
+
+    if ( rc )
+        goto fail;
+
+    list_add(&dev->domain_list, &hd->dt_devices);
+    dt_device_set_used_by(dev, d->domain_id);
+
+fail:
+    spin_unlock(&dtdevs_lock);
+
+    return rc;
+}
+
+int iommu_deassign_dt_device(struct domain *d, struct dt_device_node *dev)
+{
+    struct hvm_iommu *hd = domain_hvm_iommu(d);
+    int rc;
+
+    if ( !iommu_enabled || !hd->platform_ops )
+        return -EINVAL;
+
+    if ( !dt_device_is_protected(dev) )
+        return -EINVAL;
+
+    spin_lock(&dtdevs_lock);
+
+    rc = hd->platform_ops->reassign_dt_device(d, hardware_domain, dev);
+    if ( rc )
+        goto fail;
+
+    list_del(&dev->domain_list);
+
+    dt_device_set_used_by(dev, hardware_domain->domain_id);
+    list_add(&dev->domain_list, &domain_hvm_iommu(hardware_domain)->dt_devices);
+
+fail:
+    spin_unlock(&dtdevs_lock);
+
+    return rc;
+}
+
+int iommu_dt_domain_init(struct domain *d)
+{
+    struct hvm_iommu *hd = domain_hvm_iommu(d);
+
+    INIT_LIST_HEAD(&hd->dt_devices);
+
+    return 0;
+}
+
+void iommu_dt_domain_destroy(struct domain *d)
+{
+    struct hvm_iommu *hd = domain_hvm_iommu(d);
+    struct dt_device_node *dev, *_dev;
+    int rc;
+
+    list_for_each_entry_safe(dev, _dev, &hd->dt_devices, domain_list)
+    {
+        rc = iommu_deassign_dt_device(d, dev);
+        if ( rc )
+            dprintk(XENLOG_ERR, "Failed to deassign %s in domain %u\n",
+                    dt_node_full_name(dev), d->domain_id);
+    }
+}
diff --git a/xen/include/xen/device_tree.h b/xen/include/xen/device_tree.h
index 5542958..ded7a31 100644
--- a/xen/include/xen/device_tree.h
+++ b/xen/include/xen/device_tree.h
@@ -16,6 +16,7 @@
 #include <xen/string.h>
 #include <xen/types.h>
 #include <xen/stdbool.h>
+#include <xen/list.h>
 
 #define DEVICE_TREE_MAX_DEPTH 16
 
@@ -111,6 +112,9 @@ struct dt_device_node {
     struct dt_device_node *next; /* TODO: Remove it. Only use to know the last children */
     struct dt_device_node *allnext;
 
+    /* IOMMU specific fields */
+    bool is_protected; /* Tell if the device is protected by an IOMMU */
+    struct list_head domain_list;
 };
 
 #define MAX_PHANDLE_ARGS 16
@@ -326,6 +330,16 @@ static inline domid_t dt_device_used_by(const struct dt_device_node *device)
     return device->used_by;
 }
 
+static inline void dt_device_set_protected(struct dt_device_node *device)
+{
+    device->is_protected = true;
+}
+
+static inline bool dt_device_is_protected(const struct dt_device_node *device)
+{
+    return device->is_protected;
+}
+
 static inline bool_t dt_property_name_is_equal(const struct dt_property *pp,
                                                const char *name)
 {
diff --git a/xen/include/xen/hvm/iommu.h b/xen/include/xen/hvm/iommu.h
index f8f8a93..1259e16 100644
--- a/xen/include/xen/hvm/iommu.h
+++ b/xen/include/xen/hvm/iommu.h
@@ -21,6 +21,7 @@
 #define __XEN_HVM_IOMMU_H__
 
 #include <xen/iommu.h>
+#include <xen/list.h>
 #include <asm/hvm/iommu.h>
 
 struct hvm_iommu {
@@ -28,6 +29,11 @@ struct hvm_iommu {
 
     /* iommu_ops */
     const struct iommu_ops *platform_ops;
+
+#ifdef HAS_DEVICE_TREE
+    /* List of DT devices assigned to this domain */
+    struct list_head dt_devices;
+#endif
 };
 
 #endif /* __XEN_HVM_IOMMU_H__ */
diff --git a/xen/include/xen/iommu.h b/xen/include/xen/iommu.h
index 9ffc92a..e119379 100644
--- a/xen/include/xen/iommu.h
+++ b/xen/include/xen/iommu.h
@@ -90,6 +90,16 @@ void iommu_read_msi_from_ire(struct msi_desc *msi_desc, struct msi_msg *msg);
 #define PT_IRQ_TIME_OUT MILLISECS(8)
 #endif /* HAS_PCI */
 
+#ifdef HAS_DEVICE_TREE
+#include <xen/device_tree.h>
+
+int iommu_assign_dt_device(struct domain *d, struct dt_device_node *dev);
+int iommu_deassign_dt_device(struct domain *d, struct dt_device_node *dev);
+int iommu_dt_domain_init(struct domain *d);
+void iommu_dt_domain_destroy(struct domain *d);
+
+#endif /* HAS_DEVICE_TREE */
+
 struct page_info;
 
 struct iommu_ops {
@@ -106,6 +116,12 @@ struct iommu_ops {
     int (*update_ire_from_msi)(struct msi_desc *msi_desc, struct msi_msg *msg);
     void (*read_msi_from_ire)(struct msi_desc *msi_desc, struct msi_msg *msg);
 #endif /* HAS_PCI */
+#ifdef HAS_DEVICE_TREE
+    int (*assign_dt_device)(struct domain *d, const struct dt_device_node *dev);
+    int (*reassign_dt_device)(struct domain *s, struct domain *t,
+                              const struct dt_device_node *dev);
+#endif
+
     void (*teardown)(struct domain *d);
     int (*map_page)(struct domain *d, unsigned long gfn, unsigned long mfn,
                     unsigned int flags);
-- 
1.7.10.4

  parent reply	other threads:[~2014-04-22 13:15 UTC|newest]

Thread overview: 72+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-22 13:14 [PATCH v4 00/21] IOMMU support for ARM Julien Grall
2014-04-22 13:14 ` [PATCH v4 01/21] xen/arm: map_device: Don't hardcode dom0 in print message Julien Grall
2014-04-28 13:49   ` Ian Campbell
2014-04-22 13:14 ` [PATCH v4 02/21] xen/arm: Constify address pointer for cache helpers Julien Grall
2014-04-28 13:52   ` Ian Campbell
2014-04-22 13:14 ` [PATCH v4 03/21] xen/arm: p2m: Move comment that was misplaced Julien Grall
2014-04-28 13:52   ` Ian Campbell
2014-04-22 13:14 ` [PATCH v4 04/21] xen/arm: p2m: apply_p2m_changes: Only load domain P2M when we flush TLBs Julien Grall
2014-04-28 13:54   ` Ian Campbell
2014-04-28 13:57     ` Julien Grall
2014-04-22 13:14 ` [PATCH v4 05/21] xen/common: grant-table: only call IOMMU if paging mode translate is disabled Julien Grall
2014-04-22 13:14 ` [PATCH v4 06/21] xen/passthrough: amd: Remove domain_id from hvm_iommu Julien Grall
2014-04-28 16:38   ` Julien Grall
2014-04-29  7:43     ` Jan Beulich
2014-04-30  4:02       ` Suravee Suthikulpanit
2014-04-30  3:56   ` Suravee Suthikulpanit
2014-04-30 11:32     ` Julien Grall
2014-04-22 13:14 ` [PATCH v4 07/21] xen/passthrough: amd: rename iommu_has_feature into amd_iommu_has_feature Julien Grall
2014-04-28 13:56   ` Ian Campbell
2014-04-28 16:38   ` Julien Grall
2014-05-02 13:11     ` Julien Grall
2014-05-02 16:35       ` Aravind Gopalakrishnan
2014-04-22 13:14 ` [PATCH v4 08/21] xen/passthrough: vtd: iommu_set_hwdom_mapping is VTD specific Julien Grall
2014-04-22 13:41   ` Jan Beulich
2014-04-22 14:47     ` Julien Grall
2014-04-22 13:14 ` [PATCH v4 09/21] xen/dts: Add dt_property_read_bool Julien Grall
2014-04-22 13:14 ` [PATCH v4 10/21] xen/dts: Add dt_parse_phandle_with_args and dt_parse_phandle Julien Grall
2014-04-22 13:14 ` [PATCH v4 11/21] xen/passthrough: rework hwdom_pvh_reqs to use it also on ARM Julien Grall
2014-04-28 13:57   ` Ian Campbell
2014-04-22 13:14 ` [PATCH v4 12/21] xen/passthrough: iommu: Split generic IOMMU code Julien Grall
2014-04-22 13:48   ` Jan Beulich
2014-04-22 13:52     ` Jan Beulich
2014-04-22 14:58     ` Julien Grall
2014-04-22 16:33       ` Jan Beulich
2014-04-22 16:45         ` Julien Grall
2014-04-22 16:59           ` Jan Beulich
2014-04-22 18:02             ` Julien Grall
2014-04-23  8:43               ` Jan Beulich
2014-04-23 12:42                 ` Julien Grall
2014-04-22 13:14 ` [PATCH v4 13/21] xen/passthrough: iommu: Introduce arch specific code Julien Grall
2014-04-28 16:39   ` Julien Grall
     [not found]   ` <CAAAAutDRYoqdSBdDsETqtzVDdXmy6jq1Jnm1ck8c5eTqbkMUFw@mail.gmail.com>
2014-05-05 21:12     ` Fwd: " Aravind Gopalakrishnan
2014-04-22 13:14 ` Julien Grall [this message]
2014-04-28 14:01   ` [PATCH v4 14/21] xen/passthrough: iommu: Basic support of device tree assignment Ian Campbell
2014-04-28 14:12     ` Julien Grall
2014-04-22 13:14 ` [PATCH v4 15/21] xen/passthrough: Introduce IOMMU ARM architecture Julien Grall
2014-04-28 14:04   ` Ian Campbell
2014-04-28 14:21     ` Julien Grall
2014-04-22 13:14 ` [PATCH v4 16/21] MAINTAINERS: Add drivers/passthrough/arm Julien Grall
2014-04-22 13:50   ` Jan Beulich
2014-04-22 15:02     ` Julien Grall
2014-04-22 16:35       ` Jan Beulich
2014-04-22 18:02         ` Julien Grall
2014-04-22 13:14 ` [PATCH v4 17/21] xen/arm: Don't give IOMMU devices to dom0 when iommu is disabled Julien Grall
2014-04-22 13:14 ` [PATCH v4 18/21] xen/arm: p2m: Clean cache PT when the IOMMU doesn't support coherent walk Julien Grall
2014-04-28 14:09   ` Ian Campbell
2014-04-28 14:46     ` Julien Grall
2014-04-28 16:34   ` Julien Grall
2014-04-29  7:40   ` Jan Beulich
2014-05-02 15:15     ` Julien Grall
2014-04-22 13:14 ` [PATCH v4 19/21] xen/arm: grant: Add another entry to map MFN 1:1 in dom0 p2m Julien Grall
2014-04-28 14:11   ` Ian Campbell
2014-04-28 14:47     ` Julien Grall
2014-04-22 13:14 ` [PATCH v4 20/21] drivers/passthrough: arm: Add support for SMMU drivers Julien Grall
2014-04-28 14:13   ` Ian Campbell
2014-04-22 13:14 ` [PATCH v4 21/21] xen/arm: Add the property "protected-devices" in the hypervisor node Julien Grall
2014-05-02 12:54 ` [PATCH v4 00/21] IOMMU support for ARM Ian Campbell
2014-05-02 13:09   ` Julien Grall
2014-05-02 13:15   ` Julien Grall
2014-05-02 13:25     ` Ian Campbell
2014-05-02 13:29       ` Julien Grall
2014-05-02 14:20         ` Ian Campbell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1398172475-27873-15-git-send-email-julien.grall@linaro.org \
    --to=julien.grall@linaro.org \
    --cc=ian.campbell@citrix.com \
    --cc=jbeulich@suse.com \
    --cc=stefano.stabellini@citrix.com \
    --cc=tim@xen.org \
    --cc=xen-devel@lists.xenproject.org \
    --cc=xiantao.zhang@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.