From patchwork Tue Mar 7 17:14:19 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jordan Crouse X-Patchwork-Id: 9609517 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id BDC50602B4 for ; Tue, 7 Mar 2017 17:14:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9DA0828491 for ; Tue, 7 Mar 2017 17:14:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 927D328507; Tue, 7 Mar 2017 17:14:45 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 841A128491 for ; Tue, 7 Mar 2017 17:14:44 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 53F366E793; Tue, 7 Mar 2017 17:14:32 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from smtp.codeaurora.org (smtp.codeaurora.org [198.145.29.96]) by gabe.freedesktop.org (Postfix) with ESMTPS id DAE746E786; Tue, 7 Mar 2017 17:14:30 +0000 (UTC) Received: by smtp.codeaurora.org (Postfix, from userid 1000) id 831D1607E7; Tue, 7 Mar 2017 17:14:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1488906870; bh=FySKNAXhvkLy9kL1+LTarS9oPTVES/FgyzL6udDLDcw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JrUwVxAW39zRtQ8utKlYsUtBBUwSrOgaV8lUJg9sYd/K6JWwEuGX8jtq8yuK7cfaH 4I9EBFWu+jdPcYucwwllJ/K6ynB6zC50KwozAGk/hdLoKzq7zdf7UYmYPD5QFaAt2m XNc4yzxB1EzSdQk3LP39/+Tx87vaWIvTKbXy1F04= Received: from jcrouse-lnx.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: jcrouse@smtp.codeaurora.org) by smtp.codeaurora.org (Postfix) with ESMTPSA id 70AFB607A6; Tue, 7 Mar 2017 17:14:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1488906869; bh=FySKNAXhvkLy9kL1+LTarS9oPTVES/FgyzL6udDLDcw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pGsnjcmVw+Ol2vZ54YjYcsroZpWziBYTIg4am4KDuBkbWFg8wykwyleq7R60mJUXU 3x9TiBwQHNQX15SLsa3y7KZXZ79zoNzQ+praLrVmoDIQKHkpNnOs3IPuXuuhba1smz 1dpFf1jq8/cZLoxg1Nf1JVpOU+sTPYNTBPcm6qhY= DMARC-Filter: OpenDMARC Filter v1.3.2 smtp.codeaurora.org 70AFB607A6 Authentication-Results: pdx-caf-mail.web.codeaurora.org; dmarc=none (p=none dis=none) header.from=codeaurora.org Authentication-Results: pdx-caf-mail.web.codeaurora.org; spf=none smtp.mailfrom=jcrouse@codeaurora.org From: Jordan Crouse To: freedreno@lists.freedesktop.org Subject: [PATCH 5/6] drm/msm: Support dynamic IOMMU domains Date: Tue, 7 Mar 2017 10:14:19 -0700 Message-Id: <1488906860-11073-6-git-send-email-jcrouse@codeaurora.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1488906860-11073-1-git-send-email-jcrouse@codeaurora.org> References: <1488906860-11073-1-git-send-email-jcrouse@codeaurora.org> Cc: linux-arm-msm@vger.kernel.org, dri-devel@lists.freedesktop.org X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP Dynamic IOMMU domains allow multiple pagetables to be attached to the same IOMMU device. These can be used by smart devices like the GPU that can switch the pagetable dynamically between DRM instances. Add support for dynamic IOMMU domains if they are enabled and supported by your friendly neighborhood IOMMU driver. Signed-off-by: Jordan Crouse --- drivers/gpu/drm/msm/msm_iommu.c | 99 ++++++++++++++++++++++++++++++++++------- drivers/gpu/drm/msm/msm_iommu.h | 34 ++++++++++++++ drivers/gpu/drm/msm/msm_mmu.h | 2 +- 3 files changed, 117 insertions(+), 18 deletions(-) create mode 100644 drivers/gpu/drm/msm/msm_iommu.h diff --git a/drivers/gpu/drm/msm/msm_iommu.c b/drivers/gpu/drm/msm/msm_iommu.c index cc82410..38ec5e8 100644 --- a/drivers/gpu/drm/msm/msm_iommu.c +++ b/drivers/gpu/drm/msm/msm_iommu.c @@ -16,13 +16,7 @@ */ #include "msm_drv.h" -#include "msm_mmu.h" - -struct msm_iommu { - struct msm_mmu base; - struct iommu_domain *domain; -}; -#define to_msm_iommu(x) container_of(x, struct msm_iommu, base) +#include "msm_iommu.h" static int msm_fault_handler(struct iommu_domain *domain, struct device *dev, unsigned long iova, int flags, void *arg) @@ -83,14 +77,19 @@ static int msm_iommu_v2_attach(struct msm_mmu *mmu, const char * const *names, int cnt) { struct msm_iommu *iommu = to_msm_iommu(mmu); - int val = 1; + int val = 1, ret; /* Use TTBR1 if it exists */ - /* FIXME: This should only be for GPU and in theory only for A5XX */ - - iommu_domain_set_attr(iommu->domain, DOMAIN_ATTR_ENABLE_TTBR1, + ret = iommu_domain_set_attr(iommu->domain, DOMAIN_ATTR_ENABLE_TTBR1, &val); + /* + * We will only allow per-instance pagetables if TTBR1 is available + * because the alternative is too labor intensive + */ + iommu->allow_dynamic = !ret ? true : false; + + /* Attach the device to the domain */ return iommu_attach_device(iommu->domain, mmu->dev); } @@ -102,6 +101,18 @@ static void msm_iommu_v2_detach(struct msm_mmu *mmu, const char * const *names, iommu_detach_device(iommu->domain, mmu->dev); } +/* Dynamic domains do not have attach/detach steps */ +static int msm_iommu_attach_dynamic(struct msm_mmu *mmu, + const char * const *names, int cnt) +{ + return 0; +} + +static void msm_iommu_detach_dynamic(struct msm_mmu *mmu, + const char * const *names, int cnt) +{ +} + static int msm_iommu_map(struct msm_mmu *mmu, uint64_t iova, struct sg_table *sgt, unsigned len, int prot) { @@ -193,24 +204,78 @@ static void msm_iommu_destroy(struct msm_mmu *mmu) .destroy = msm_iommu_destroy, }; -struct msm_mmu *msm_iommu_new(struct device *dev, struct iommu_domain *domain) +/* These are for dynamic per-instance domains */ +static const struct msm_mmu_funcs dynamic_funcs = { + .attach = msm_iommu_attach_dynamic, + .detach = msm_iommu_detach_dynamic, + .map = msm_iommu_map, + .unmap = msm_iommu_unmap, + .destroy = msm_iommu_destroy, +}; + +struct msm_mmu *_msm_iommu_new(struct device *dev, struct iommu_domain *domain, + const struct msm_mmu_funcs *funcs) { struct msm_iommu *iommu; - const struct msm_mmu_funcs *funcs; iommu = kzalloc(sizeof(*iommu), GFP_KERNEL); if (!iommu) return ERR_PTR(-ENOMEM); + iommu->domain = domain; + msm_mmu_init(&iommu->base, dev, funcs); + iommu_set_fault_handler(domain, msm_fault_handler, iommu, true); + + return &iommu->base; +} + +struct msm_mmu *msm_iommu_new(struct device *dev, struct iommu_domain *domain) +{ + const struct msm_mmu_funcs *funcs; + if (of_find_compatible_node(NULL, NULL, "qcom,msm-smmu-v2") || of_find_compatible_node(NULL, NULL, "qcom,msm-mmu-500")) funcs = &funcs_v1; else funcs = &funcs_v2; - iommu->domain = domain; - msm_mmu_init(&iommu->base, dev, funcs); - iommu_set_fault_handler(domain, msm_fault_handler, iommu, true); + return _msm_iommu_new(dev, domain, funcs); +} - return &iommu->base; +/* + * Given a base domain that is attached to a IOMMU device try to create a + * dynamic domain that is also attached to the same device but allocates a new + * pagetable. This is used to allow multiple pagetables to be attached to the + * same device. + */ +struct msm_mmu *msm_iommu_new_dynamic(struct msm_mmu *base) +{ + static unsigned int procid; + struct msm_iommu *base_iommu = to_msm_iommu(base); + struct msm_iommu *iommu; + struct iommu_domain *domain; + struct msm_mmu *mmu; + + /* Don't continue if the base domain didn't have the support we need */ + if (!base || base_iommu->allow_dynamic == false) + return ERR_PTR(-EOPNOTSUPP); + + domain = iommu_domain_create_dynamic(base_iommu->domain); + if (!domain) + return ERR_PTR(-ENODEV); + + mmu = _msm_iommu_new(base->dev, domain, &dynamic_funcs); + + if (IS_ERR(mmu)) { + if (domain) + iommu_domain_free(domain); + } + + iommu = to_msm_iommu(mmu); + + iommu_domain_get_attr(iommu->domain, DOMAIN_ATTR_TTBR0, &iommu->ttbr0); + + iommu->contextidr = ++procid; + + return mmu; } diff --git a/drivers/gpu/drm/msm/msm_iommu.h b/drivers/gpu/drm/msm/msm_iommu.h new file mode 100644 index 0000000..bfb7501 --- /dev/null +++ b/drivers/gpu/drm/msm/msm_iommu.h @@ -0,0 +1,34 @@ +/* Copyright (c) 2016 The Linux Foundation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * 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. + */ + +#ifndef _MSM_IOMMU_H_ +#define _MSM_IOMMU_H_ + +#include "msm_mmu.h" + +struct msm_iommu { + struct msm_mmu base; + struct iommu_domain *domain; + int cb; + phys_addr_t ttbr0; + uint32_t contextidr; + bool allow_dynamic; +}; +#define to_msm_iommu(x) container_of(x, struct msm_iommu, base) + +static inline bool msm_iommu_allow_dynamic(struct msm_mmu *mmu) +{ + struct msm_iommu *iommu = to_msm_iommu(mmu); + + return iommu->allow_dynamic; +} +#endif diff --git a/drivers/gpu/drm/msm/msm_mmu.h b/drivers/gpu/drm/msm/msm_mmu.h index e7d1967..65e5202 100644 --- a/drivers/gpu/drm/msm/msm_mmu.h +++ b/drivers/gpu/drm/msm/msm_mmu.h @@ -45,7 +45,7 @@ static inline void msm_mmu_init(struct msm_mmu *mmu, struct device *dev, } struct msm_mmu *msm_iommu_new(struct device *dev, struct iommu_domain *domain); -struct msm_mmu *msm_gpummu_new(struct device *dev, struct msm_gpu *gpu); +struct msm_mmu *msm_iommu_new_dynamic(struct msm_mmu *orig); #ifdef CONFIG_QCOM_IOMMU_V1 struct device *msm_iommu_get_ctx(const char *ctx_name);