From patchwork Tue Mar 1 18:27:45 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Auger Eric X-Patchwork-Id: 63328 Delivered-To: patches@linaro.org Received: by 10.112.199.169 with SMTP id jl9csp1981497lbc; Tue, 1 Mar 2016 10:28:27 -0800 (PST) X-Received: by 10.194.22.35 with SMTP id a3mr22196075wjf.165.1456856907467; Tue, 01 Mar 2016 10:28:27 -0800 (PST) Return-Path: Received: from mail-wm0-x231.google.com (mail-wm0-x231.google.com. [2a00:1450:400c:c09::231]) by mx.google.com with ESMTPS id g9si38580876wjq.235.2016.03.01.10.28.27 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 Mar 2016 10:28:27 -0800 (PST) Received-SPF: pass (google.com: domain of eric.auger@linaro.org designates 2a00:1450:400c:c09::231 as permitted sender) client-ip=2a00:1450:400c:c09::231; Authentication-Results: mx.google.com; spf=pass (google.com: domain of eric.auger@linaro.org designates 2a00:1450:400c:c09::231 as permitted sender) smtp.mailfrom=eric.auger@linaro.org; dkim=pass header.i=@linaro.org Received: by mail-wm0-x231.google.com with SMTP id n186so50787434wmn.1 for ; Tue, 01 Mar 2016 10:28:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=cYIwbjneOwaIYMwwJSy4RtESSV4Bki/+zT4Hbm0Ybyw=; b=U2fuolGb8XEdoKVDnEoNt4DYq4ohEjq/qDQE+uVpmsDzn5SpDd1PgKU5VSWgca0fse 89amLXMS4hYrSGqdTWcGU2TBfTs2cHv1NR7S9ZA2BTCsy8twFwhp7TYKGT8Stk4WHEl/ YDPyvfjzj5wbr1vp3Mosn68xj/Plse4efHK/s= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=cYIwbjneOwaIYMwwJSy4RtESSV4Bki/+zT4Hbm0Ybyw=; b=jErW01/K10lT2ytJfi6jFNygps+D348kB1MaMcWYutc4Agcn6ieQbPNpKO/HFNNDsd uikVsP+1aKPxGbfxpe5q5d4hdOs2zTKKO8BCeLEDIkJsfqdw03eSos/5Y2V+xgpj5vC+ kpSdNYyfPoaK73/B5yvQvlrn+NmN+TmPPLANoDVQSodaGVkgcjjP+S2Cu6FH6ea0Gq62 7mgJri1Rd3J9oAQ/P2Jfxt2AhiwC0VDH/vSmgBCSqEszZpF8ieqEBn+PZgPYXfgx3MjF sjvwfxq1KpaDFKqC5sD3wVZIBpcfcAVHh1ziap1FHech+WZ8srHjeubRg+lAo8UjPlCD gYLQ== X-Gm-Message-State: AD7BkJIrieGgI/J9tsc1La1UIeBqU0qkj9EB5KESJ0iI9hf8B2C2j1EFm843DpWUpbUsVJBqKsE= X-Received: by 10.194.71.70 with SMTP id s6mr21712634wju.1.1456856907273; Tue, 01 Mar 2016 10:28:27 -0800 (PST) Return-Path: Received: from new-host-8.home (LMontsouris-657-1-37-90.w80-11.abo.wanadoo.fr. [80.11.198.90]) by smtp.gmail.com with ESMTPSA id k8sm32176385wjr.38.2016.03.01.10.28.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 01 Mar 2016 10:28:24 -0800 (PST) From: Eric Auger To: eric.auger@st.com, eric.auger@linaro.org, robin.murphy@arm.com, alex.williamson@redhat.com, will.deacon@arm.com, joro@8bytes.org, tglx@linutronix.de, jason@lakedaemon.net, marc.zyngier@arm.com, christoffer.dall@linaro.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org Cc: suravee.suthikulpanit@amd.com, patches@linaro.org, linux-kernel@vger.kernel.org, Manish.Jaggi@caviumnetworks.com, Bharat.Bhushan@freescale.com, pranav.sawargaonkar@gmail.com, p.fedin@samsung.com, iommu@lists.linux-foundation.org Subject: [RFC v5 05/17] dma-reserved-iommu: reserved binding rb-tree and helpers Date: Tue, 1 Mar 2016 18:27:45 +0000 Message-Id: <1456856877-4817-6-git-send-email-eric.auger@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1456856877-4817-1-git-send-email-eric.auger@linaro.org> References: <1456856877-4817-1-git-send-email-eric.auger@linaro.org> we will need to track which host physical addresses are mapped to reserved IOVA. In that prospect we introduce a new RB tree indexed by physical address. This RB tree only is used for reserved IOVA bindings. It is expected this RB tree will contain very few bindings. Those generally correspond to single page mapping one MSI frame (GICv2m frame or ITS GITS_TRANSLATER frame). Signed-off-by: Eric Auger --- v3 -> v4: - that code was formerly in "iommu/arm-smmu: add a reserved binding RB tree" --- drivers/iommu/dma-reserved-iommu.c | 60 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) -- 1.9.1 diff --git a/drivers/iommu/dma-reserved-iommu.c b/drivers/iommu/dma-reserved-iommu.c index 41a1add..30d54d0 100644 --- a/drivers/iommu/dma-reserved-iommu.c +++ b/drivers/iommu/dma-reserved-iommu.c @@ -20,6 +20,66 @@ #include #include +struct iommu_reserved_binding { + struct kref kref; + struct rb_node node; + struct iommu_domain *domain; + phys_addr_t addr; + dma_addr_t iova; + size_t size; +}; + +/* Reserved binding RB-tree manipulation */ + +static struct iommu_reserved_binding *find_reserved_binding( + struct iommu_domain *d, + phys_addr_t start, size_t size) +{ + struct rb_node *node = d->reserved_binding_list.rb_node; + + while (node) { + struct iommu_reserved_binding *binding = + rb_entry(node, struct iommu_reserved_binding, node); + + if (start + size <= binding->addr) + node = node->rb_left; + else if (start >= binding->addr + binding->size) + node = node->rb_right; + else + return binding; + } + + return NULL; +} + +static void link_reserved_binding(struct iommu_domain *d, + struct iommu_reserved_binding *new) +{ + struct rb_node **link = &d->reserved_binding_list.rb_node; + struct rb_node *parent = NULL; + struct iommu_reserved_binding *binding; + + while (*link) { + parent = *link; + binding = rb_entry(parent, struct iommu_reserved_binding, + node); + + if (new->addr + new->size <= binding->addr) + link = &(*link)->rb_left; + else + link = &(*link)->rb_right; + } + + rb_link_node(&new->node, parent, link); + rb_insert_color(&new->node, &d->reserved_binding_list); +} + +static void unlink_reserved_binding(struct iommu_domain *d, + struct iommu_reserved_binding *old) +{ + rb_erase(&old->node, &d->reserved_binding_list); +} + int iommu_alloc_reserved_iova_domain(struct iommu_domain *domain, dma_addr_t iova, size_t size, unsigned long order)