From: Jean-Philippe Brucker <jean-philippe@linaro.org>
To: Jason Gunthorpe <jgg@nvidia.com>
Cc: iommu@lists.linux.dev, Joerg Roedel <joro@8bytes.org>,
Robin Murphy <robin.murphy@arm.com>,
virtualization@lists.linux.dev, Will Deacon <will@kernel.org>,
Eric Auger <eric.auger@redhat.com>,
patches@lists.linux.dev
Subject: Re: [PATCH 1/5] iommu/virtio: Break out bypass identity support into a global static
Date: Fri, 21 Feb 2025 11:35:27 +0000 [thread overview]
Message-ID: <20250221113527.GA719702@myrica> (raw)
In-Reply-To: <1-v1-91eed9c8014a+53a37-iommu_virtio_domains_jgg@nvidia.com>
On Fri, Feb 07, 2025 at 10:46:01AM -0400, Jason Gunthorpe wrote:
> To make way for a domain_alloc_paging conversion add the typical global
> static IDENTITY domain. This supports VMMs that have a
> VIRTIO_IOMMU_F_BYPASS_CONFIG config.
>
> If the VMM does not have support then the domain_alloc path is still used,
> which creates an IDENTITY domain out of a paging domain.
>
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> ---
> drivers/iommu/virtio-iommu.c | 86 ++++++++++++++++++++++++++++--------
> 1 file changed, 67 insertions(+), 19 deletions(-)
>
> diff --git a/drivers/iommu/virtio-iommu.c b/drivers/iommu/virtio-iommu.c
> index b85ce6310ddbda..c71a996760bddb 100644
> --- a/drivers/iommu/virtio-iommu.c
> +++ b/drivers/iommu/virtio-iommu.c
> @@ -48,6 +48,7 @@ struct viommu_dev {
> u64 pgsize_bitmap;
> u32 first_domain;
> u32 last_domain;
> + u32 identity_domain_id;
> /* Supported MAP flags */
> u32 map_flags;
> u32 probe_size;
> @@ -70,7 +71,6 @@ struct viommu_domain {
> struct rb_root_cached mappings;
>
> unsigned long nr_endpoints;
> - bool bypass;
> };
>
> struct viommu_endpoint {
> @@ -305,6 +305,22 @@ static int viommu_send_req_sync(struct viommu_dev *viommu, void *buf,
> return ret;
> }
>
> +static int viommu_send_attach_req(struct viommu_dev *viommu, struct device *dev,
> + struct virtio_iommu_req_attach *req)
> +{
> + struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
> + int ret;
> + int i;
> +
> + for (i = 0; i < fwspec->num_ids; i++) {
> + req->endpoint = cpu_to_le32(fwspec->ids[i]);
> + ret = viommu_send_req_sync(viommu, req, sizeof(*req));
> + if (ret)
> + return ret;
> + }
> + return 0;
> +}
> +
> /*
> * viommu_add_mapping - add a mapping to the internal tree
> *
> @@ -687,12 +703,6 @@ static int viommu_domain_finalise(struct viommu_endpoint *vdev,
> vdomain->viommu = viommu;
>
> if (domain->type == IOMMU_DOMAIN_IDENTITY) {
> - if (virtio_has_feature(viommu->vdev,
> - VIRTIO_IOMMU_F_BYPASS_CONFIG)) {
> - vdomain->bypass = true;
> - return 0;
> - }
> -
> ret = viommu_domain_map_identity(vdev, vdomain);
> if (ret) {
> ida_free(&viommu->domain_ids, vdomain->id);
> @@ -719,10 +729,8 @@ static void viommu_domain_free(struct iommu_domain *domain)
>
> static int viommu_attach_dev(struct iommu_domain *domain, struct device *dev)
> {
> - int i;
> int ret = 0;
> struct virtio_iommu_req_attach req;
> - struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
> struct viommu_endpoint *vdev = dev_iommu_priv_get(dev);
> struct viommu_domain *vdomain = to_viommu_domain(domain);
>
> @@ -761,16 +769,9 @@ static int viommu_attach_dev(struct iommu_domain *domain, struct device *dev)
> .domain = cpu_to_le32(vdomain->id),
> };
>
> - if (vdomain->bypass)
> - req.flags |= cpu_to_le32(VIRTIO_IOMMU_ATTACH_F_BYPASS);
> -
> - for (i = 0; i < fwspec->num_ids; i++) {
> - req.endpoint = cpu_to_le32(fwspec->ids[i]);
> -
> - ret = viommu_send_req_sync(vdomain->viommu, &req, sizeof(req));
> - if (ret)
> - return ret;
> - }
> + ret = viommu_send_attach_req(vdomain->viommu, dev, &req);
> + if (ret)
> + return ret;
>
> if (!vdomain->nr_endpoints) {
> /*
> @@ -788,6 +789,40 @@ static int viommu_attach_dev(struct iommu_domain *domain, struct device *dev)
> return 0;
> }
>
> +static int viommu_attach_identity_domain(struct iommu_domain *domain,
> + struct device *dev)
> +{
> + int ret = 0;
> + struct virtio_iommu_req_attach req;
> + struct viommu_endpoint *vdev = dev_iommu_priv_get(dev);
> + struct viommu_domain *vdomain = to_viommu_domain(domain);
> +
> + req = (struct virtio_iommu_req_attach) {
> + .head.type = VIRTIO_IOMMU_T_ATTACH,
> + .domain = cpu_to_le32(vdev->viommu->identity_domain_id),
> + .flags = cpu_to_le32(VIRTIO_IOMMU_ATTACH_F_BYPASS),
> + };
> +
> + ret = viommu_send_attach_req(vdev->viommu, dev, &req);
> + if (ret)
> + return ret;
> +
> + if (vdev->vdomain) {
> + vdev->vdomain->nr_endpoints--;
> + vdomain->nr_endpoints++;
> + vdev->vdomain = vdomain;
These two need to be unconditional
> + }
> + return 0;
> +}
> +
> +static struct viommu_domain viommu_identity_domain = {
> + .domain = { .type = IOMMU_DOMAIN_IDENTITY,
> + .ops =
> + &(const struct iommu_domain_ops){
> + .attach_dev = viommu_attach_identity_domain,
> + } }
> +};
nit: how about
static struct viommu_domain viommu_identity_domain = {
.domain = {
.type = IOMMU_DOMAIN_IDENTITY,
.ops = &(const struct iommu_domain_ops) {
.attach_dev = viommu_attach_identity_domain,
},
},
};
> +
> static void viommu_detach_dev(struct viommu_endpoint *vdev)
> {
> int i;
> @@ -1061,6 +1096,7 @@ static bool viommu_capable(struct device *dev, enum iommu_cap cap)
> }
>
> static struct iommu_ops viommu_ops = {
> + .identity_domain = &viommu_identity_domain.domain,
> .capable = viommu_capable,
> .domain_alloc = viommu_domain_alloc,
> .probe_device = viommu_probe_device,
> @@ -1184,6 +1220,18 @@ static int viommu_probe(struct virtio_device *vdev)
> if (virtio_has_feature(vdev, VIRTIO_IOMMU_F_MMIO))
> viommu->map_flags |= VIRTIO_IOMMU_MAP_F_MMIO;
>
> + /* Reserve an ID to use as the bypass domain */
> + if (virtio_has_feature(viommu->vdev, VIRTIO_IOMMU_F_BYPASS_CONFIG)) {
> + viommu->identity_domain_id = viommu->first_domain;
> + viommu->first_domain++;
> + } else {
> + /*
> + * Assume the VMM is sensible and it either supports bypass on
> + * all instances or no instances.
> + */
Maybe also a WARN_ON(!viommu_ops.identity_domain) above?
Thanks,
Jean
> + viommu_ops.identity_domain = NULL;
> + }
> +
> viommu_ops.pgsize_bitmap = viommu->pgsize_bitmap;
>
> virtio_device_ready(vdev);
> --
> 2.43.0
>
>
next prev parent reply other threads:[~2025-02-21 11:35 UTC|newest]
Thread overview: 37+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-07 14:46 [PATCH 0/5] Convert virtio-iommu to domain_alloc_paging() Jason Gunthorpe
2025-02-07 14:46 ` [PATCH 1/5] iommu/virtio: Break out bypass identity support into a global static Jason Gunthorpe
2025-02-12 0:43 ` Jacob Pan
2025-02-18 18:21 ` Jason Gunthorpe
2025-02-21 11:35 ` Jean-Philippe Brucker [this message]
2025-02-24 19:37 ` Jason Gunthorpe
2025-02-07 14:46 ` [PATCH 2/5] iommu: Add domain_alloc_identity() Jason Gunthorpe
2025-02-12 13:56 ` Robin Murphy
2025-02-12 14:03 ` Jason Gunthorpe
2025-02-12 14:16 ` Robin Murphy
2025-02-12 14:45 ` Jason Gunthorpe
2025-02-07 14:46 ` [PATCH 3/5] iommu/virtio: Move to domain_alloc_paging() Jason Gunthorpe
2025-02-12 19:22 ` Jacob Pan
2025-02-12 23:30 ` Jason Gunthorpe
2025-02-13 5:47 ` Jacob Pan
2025-02-18 20:01 ` Jason Gunthorpe
[not found] ` <67ad876d.170a0220.3c21dc.85ceSMTPIN_ADDED_BROKEN@mx.google.com>
2025-02-13 9:46 ` Jean-Philippe Brucker
2025-02-13 17:03 ` Yu Zhang
2025-02-13 18:09 ` Jean-Philippe Brucker
2025-02-19 9:39 ` Yu Zhang
2025-02-19 10:35 ` Jean-Philippe Brucker
2025-02-19 11:11 ` Yu Zhang
2025-02-19 11:57 ` Jean-Philippe Brucker
2025-02-19 13:10 ` Yi Liu
2025-02-20 2:58 ` Baolu Lu
2025-02-20 3:44 ` Yu Zhang
2025-02-07 14:46 ` [PATCH 4/5] iommu: Do not call domain_alloc() in iommu_sva_domain_alloc() Jason Gunthorpe
2025-02-07 14:46 ` [PATCH 5/5] iommu: Hide ops.domain_alloc behind CONFIG_FSL_PAMU Jason Gunthorpe
2025-02-12 0:41 ` [PATCH 0/5] Convert virtio-iommu to domain_alloc_paging() Jacob Pan
2025-02-12 12:50 ` Jason Gunthorpe
2025-02-12 18:50 ` Jacob Pan
2025-02-12 20:10 ` Robin Murphy
2025-02-21 11:42 ` Jean-Philippe Brucker
2025-02-24 19:39 ` Jason Gunthorpe
[not found] ` <67abee53.170a0220.154671.ae28SMTPIN_ADDED_BROKEN@mx.google.com>
2025-02-12 11:58 ` Jean-Philippe Brucker
2025-02-12 17:05 ` Jacob Pan
[not found] ` <67acd4e2.630a0220.365aab.e098SMTPIN_ADDED_BROKEN@mx.google.com>
2025-02-12 19:16 ` Jean-Philippe Brucker
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=20250221113527.GA719702@myrica \
--to=jean-philippe@linaro.org \
--cc=eric.auger@redhat.com \
--cc=iommu@lists.linux.dev \
--cc=jgg@nvidia.com \
--cc=joro@8bytes.org \
--cc=patches@lists.linux.dev \
--cc=robin.murphy@arm.com \
--cc=virtualization@lists.linux.dev \
--cc=will@kernel.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox