From patchwork Tue Apr 19 16:56:29 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Auger X-Patchwork-Id: 8881871 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id A96419F39D for ; Tue, 19 Apr 2016 16:59:30 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id C23D82017D for ; Tue, 19 Apr 2016 16:59:29 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id C034C201B4 for ; Tue, 19 Apr 2016 16:59:25 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1asYy3-0001Oy-S1; Tue, 19 Apr 2016 16:57:39 +0000 Received: from mail-wm0-x22f.google.com ([2a00:1450:400c:c09::22f]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1asYxb-00013B-8e for linux-arm-kernel@lists.infradead.org; Tue, 19 Apr 2016 16:57:12 +0000 Received: by mail-wm0-x22f.google.com with SMTP id v188so170473273wme.1 for ; Tue, 19 Apr 2016 09:56:53 -0700 (PDT) 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=2sW2onO0OgAk2H9R6KN5bpkVdnkFlK/Uxb8j8QfJTgM=; b=SCDXBIOlj05x935cgExp59/STLxr/lvYuOoFP9dSqvkAwv49HFJd5SISKilpLv2R0G ECCsghCxGZ3/g0C2uAA/Ayg5mGx6hei8Ld/mqLlRzD4mAnAqwpLYSkay393cy1jH8YUa ayxQqNvkSb2hjgf3vtvEBHkIawvBmBjCd3dvw= 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=2sW2onO0OgAk2H9R6KN5bpkVdnkFlK/Uxb8j8QfJTgM=; b=B4fctc7yQeF3Jks4xTGu3SaSleiE2/InYBuvwtQp5i05jeHrbIL0JqwPx4jmuABS2m HCQImBhogmZ3Rih9+0PEcbANpdPsdqfFM7aHZV/MfBFX40HFj3Cu0X2+VOtvopvg4Orp dzrxecvUUo5h8IM8H2WRsWfcKIrHsnwFle/5ZYj/kDPzwDwYEJnyuoeaq+gulAbBezlK 0hyFv38k/zWAlawv9e9ajxwbGHHiwgxM9OMhk1Muaa7VGnuPycxfrFQNy+lEWbUtX6Nx CkEhO0fWmZXb3J3iVaKyuexuYe3nNRZFHfgtmgreHm/5Uo3K46jTJ5x4N2j9FDOjWXdO UwZQ== X-Gm-Message-State: AOPr4FU+cRo3HaQH7QwIQhKCzeEAUlwfhjm9uk590jSQSrzvfd5V5bqlZHd1AlwCcwRofYcX X-Received: by 10.28.140.12 with SMTP id o12mr25348087wmd.19.1461085012679; Tue, 19 Apr 2016 09:56:52 -0700 (PDT) Received: from new-host-34.home (LMontsouris-657-1-37-90.w80-11.abo.wanadoo.fr. [80.11.198.90]) by smtp.gmail.com with ESMTPSA id x2sm1169329wjr.33.2016.04.19.09.56.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 19 Apr 2016 09:56:51 -0700 (PDT) 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 Subject: [PATCH v7 05/10] iommu/dma-reserved-iommu: reserved binding rb-tree and helpers Date: Tue, 19 Apr 2016 16:56:29 +0000 Message-Id: <1461084994-2355-6-git-send-email-eric.auger@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1461084994-2355-1-git-send-email-eric.auger@linaro.org> References: <1461084994-2355-1-git-send-email-eric.auger@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160419_095711_488703_A45B1474 X-CRM114-Status: GOOD ( 14.55 ) X-Spam-Score: -2.7 (--) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: julien.grall@arm.com, patches@linaro.org, Jean-Philippe.Brucker@arm.com, p.fedin@samsung.com, linux-kernel@vger.kernel.org, Bharat.Bhushan@freescale.com, iommu@lists.linux-foundation.org, pranav.sawargaonkar@gmail.com MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-5.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,RP_MATCHES_RCVD,T_DKIM_INVALID,UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 --- v5 -> v6: - add comment about @d->reserved_lock to be held v3 -> v4: - that code was formerly in "iommu/arm-smmu: add a reserved binding RB tree" --- drivers/iommu/dma-reserved-iommu.c | 63 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/drivers/iommu/dma-reserved-iommu.c b/drivers/iommu/dma-reserved-iommu.c index 2562af0..f6fa18e 100644 --- a/drivers/iommu/dma-reserved-iommu.c +++ b/drivers/iommu/dma-reserved-iommu.c @@ -23,6 +23,69 @@ struct reserved_iova_domain { int prot; /* iommu protection attributes to be obeyed */ }; +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 */ + +/* @d->reserved_lock must be held */ +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; +} + +/* @d->reserved_lock must be held */ +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); +} + +/* @d->reserved_lock must be held */ +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, int prot, unsigned long order)