From patchwork Fri Feb 26 17:35:44 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Auger Eric X-Patchwork-Id: 63092 Delivered-To: patches@linaro.org Received: by 10.112.235.234 with SMTP id up10csp849lbc; Fri, 26 Feb 2016 09:36:15 -0800 (PST) X-Received: by 10.28.18.194 with SMTP id 185mr4673112wms.14.1456508173760; Fri, 26 Feb 2016 09:36:13 -0800 (PST) Return-Path: Received: from mail-wm0-x229.google.com (mail-wm0-x229.google.com. [2a00:1450:400c:c09::229]) by mx.google.com with ESMTPS id w128si5243819wmb.61.2016.02.26.09.36.13 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 26 Feb 2016 09:36:13 -0800 (PST) Received-SPF: pass (google.com: domain of eric.auger@linaro.org designates 2a00:1450:400c:c09::229 as permitted sender) client-ip=2a00:1450:400c:c09::229; Authentication-Results: mx.google.com; spf=pass (google.com: domain of eric.auger@linaro.org designates 2a00:1450:400c:c09::229 as permitted sender) smtp.mailfrom=eric.auger@linaro.org; dkim=pass header.i=@linaro.org Received: by mail-wm0-x229.google.com with SMTP id g62so81590285wme.0 for ; Fri, 26 Feb 2016 09:36:13 -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=WzEKhYY657a5L7ipV9LY50nXJb7Wcc2QQjUntBQCafNrCpcIzswJyQi76rQ1PPt4Wg Ef7s12EWTbDCM+DkkzIsaSENpLm1p68cJVMVh3hzS1X89uGRUZ5WgU8SxxYmLULA5svk 1RsrvIzkX7XFS4Yhd38eIP5zzcLbGlIoEgP28= 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=K2ZDVrhjdURW3ZPMLZBDmrsFuUtxAzmP0oE3VUt6YAboCupMYeyl6B602ldt9pW24Q hivbxY5FvfPrH7/TC6y/MkS5sKtMRqEz6df6Op6JB5eG+cUpZ+Pw0elO5ZfTsE2xCzIM R/Ta5fkZhRYGZ5sbfRHigA7xd0hKbO6cCnQBL+B+sRGoGafDwBjgxvDjOr6k4pzz6KrQ 7xRy+YFfkeLc+02t7ICaGOkhrfvjvlmbGi6M/DUke8uyxlf1miCP3ALN61qY41i6PJbN VI0RVZe3Gl4Z8B7+tQNAiWAKWnD/NKDrydjry8xKx4SdNJVbYsC2FrmX+b4HrG5f7ORf npVA== X-Gm-Message-State: AD7BkJI9eQM4g8hTCsAtpzD20d2jsWgwFZ94zx/AGfBAublG00Bs4MxjI4mpKXIZSC8kU7r6wfY= X-Received: by 10.194.76.72 with SMTP id i8mr2797715wjw.117.1456508173485; Fri, 26 Feb 2016 09:36:13 -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 77sm3750373wmp.18.2016.02.26.09.36.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 26 Feb 2016 09:36:12 -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 v4 04/14] dma-reserved-iommu: reserved binding rb-tree and helpers Date: Fri, 26 Feb 2016 17:35:44 +0000 Message-Id: <1456508154-2253-5-git-send-email-eric.auger@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1456508154-2253-1-git-send-email-eric.auger@linaro.org> References: <1456508154-2253-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)