From patchwork Tue Apr 19 03:17:24 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: yao mark X-Patchwork-Id: 8876351 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 0AEE39F36E for ; Tue, 19 Apr 2016 03:21:19 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id E674E201E4 for ; Tue, 19 Apr 2016 03:21:17 +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 A8E4B201BC for ; Tue, 19 Apr 2016 03:21:16 +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 1asMC5-00035c-Vy; Tue, 19 Apr 2016 03:19:18 +0000 Received: from regular1.263xmail.com ([211.150.99.132]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1asMC1-00032t-P9; Tue, 19 Apr 2016 03:19:15 +0000 Received: from mark.yao?rock-chips.com (unknown [192.168.167.190]) by regular1.263xmail.com (Postfix) with SMTP id 1E1619304; Tue, 19 Apr 2016 11:18:48 +0800 (CST) X-263anti-spam: KSV:0; X-MAIL-GRAY: 0 X-MAIL-DELIVERY: 1 X-ABS-CHECKED: 4 X-ADDR-CHECKED: 0 X-KSVirus-check: 0 Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.263.net (Postfix) with ESMTP id C4C412CB19; Tue, 19 Apr 2016 11:18:44 +0800 (CST) X-RL-SENDER: mark.yao@rock-chips.com X-FST-TO: airlied@linux.ie X-SENDER-IP: 58.22.7.114 X-LOGIN-NAME: mark.yao@rock-chips.com X-UNIQUE-TAG: X-ATTACHMENT-NUM: 0 X-SENDER: yzq@rock-chips.com X-DNS-TYPE: 0 Received: from localhost.localdomain (unknown [58.22.7.114]) by smtp.263.net (Postfix) whith ESMTP id 2903MZPZBY; Tue, 19 Apr 2016 11:18:45 +0800 (CST) From: Mark Yao To: David Airlie , Heiko Stuebner , dri-devel@lists.freedesktop.org, linux-arm-kernel@lists.infradead.org, linux-rockchip@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH] drm/rockchip: rewrite IOMMU support code Date: Tue, 19 Apr 2016 11:17:24 +0800 Message-Id: <1461035844-23026-1-git-send-email-mark.yao@rock-chips.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1455870164-25337-1-git-send-email-m.szyprowski@samsung.com> References: <1455870164-25337-1-git-send-email-m.szyprowski@samsung.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160418_201914_602792_83ED38EA X-CRM114-Status: GOOD ( 15.73 ) X-Spam-Score: -2.6 (--) 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: Marek Szyprowski , Mark Yao 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.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, 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 This patch is base on Marek Szyprowski's patch: [RFC 0/3] Unify IOMMU-based DMA-mapping code for ARM and ARM64 [https://lkml.org/lkml/2016/2/19/79] And the patch is learn from Marek Szyprowski's exynos patch: (drm/exynos: rewrite IOMMU support code) The patch replaces usage of ARM-specific IOMMU/DMA-mapping related calls with new generic code for managing DMA-IOMMU integration layer. It also removes all the hacks, which were needed to configure common DMA/IO address space on the virtual rockchip-drm device. This patch now works on Rockchip ARM64 rk3399 platform. Cc: Marek Szyprowski Signed-off-by: Mark Yao --- drivers/gpu/drm/rockchip/Kconfig | 3 +- drivers/gpu/drm/rockchip/rockchip_drm_drv.c | 81 +++++++++++++++++++-------- drivers/gpu/drm/rockchip/rockchip_drm_drv.h | 1 + 3 files changed, 60 insertions(+), 25 deletions(-) diff --git a/drivers/gpu/drm/rockchip/Kconfig b/drivers/gpu/drm/rockchip/Kconfig index d4c6a89..731f5c3 100644 --- a/drivers/gpu/drm/rockchip/Kconfig +++ b/drivers/gpu/drm/rockchip/Kconfig @@ -1,7 +1,6 @@ config DRM_ROCKCHIP tristate "DRM Support for Rockchip" - depends on BROKEN - depends on DRM && ROCKCHIP_IOMMU + depends on DRM && IOMMU_DMA depends on RESET_CONTROLLER select DRM_KMS_HELPER select DRM_KMS_FB_HELPER diff --git a/drivers/gpu/drm/rockchip/rockchip_drm_drv.c b/drivers/gpu/drm/rockchip/rockchip_drm_drv.c index 7176483..772bca3 100644 --- a/drivers/gpu/drm/rockchip/rockchip_drm_drv.c +++ b/drivers/gpu/drm/rockchip/rockchip_drm_drv.c @@ -14,7 +14,7 @@ * GNU General Public License for more details. */ -#include +#include #include #include @@ -44,7 +44,8 @@ int rockchip_drm_dma_attach_device(struct drm_device *drm_dev, struct device *dev) { - struct dma_iommu_mapping *mapping = drm_dev->dev->archdata.mapping; + struct rockchip_drm_private *private = drm_dev->dev_private; + struct iommu_domain *domain = private->domain; int ret; ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32)); @@ -52,14 +53,28 @@ int rockchip_drm_dma_attach_device(struct drm_device *drm_dev, return ret; dma_set_max_seg_size(dev, DMA_BIT_MASK(32)); + ret = iommu_attach_device(domain, dev); + if (ret) { + dev_err(dev, "Failed to attach iommu device\n"); + return ret; + } - return arm_iommu_attach_device(dev, mapping); + if (!common_iommu_setup_dma_ops(dev, 0x10000000, SZ_2G, domain->ops)) { + dev_err(dev, "Failed to set dma_ops\n"); + iommu_detach_device(domain, dev); + ret = -ENODEV; + } + + return ret; } void rockchip_drm_dma_detach_device(struct drm_device *drm_dev, struct device *dev) { - arm_iommu_detach_device(dev); + struct rockchip_drm_private *private = drm_dev->dev_private; + struct iommu_domain *domain = private->domain; + + iommu_detach_device(domain, dev); } int rockchip_register_crtc_funcs(struct drm_crtc *crtc, @@ -127,9 +142,9 @@ static void rockchip_drm_crtc_disable_vblank(struct drm_device *dev, static int rockchip_drm_load(struct drm_device *drm_dev, unsigned long flags) { struct rockchip_drm_private *private; - struct dma_iommu_mapping *mapping; struct device *dev = drm_dev->dev; struct drm_connector *connector; + struct iommu_group *group; int ret; private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL); @@ -152,23 +167,36 @@ static int rockchip_drm_load(struct drm_device *drm_dev, unsigned long flags) goto err_config_cleanup; } - /* TODO(djkurtz): fetch the mapping start/size from somewhere */ - mapping = arm_iommu_create_mapping(&platform_bus_type, 0x00000000, - SZ_2G); - if (IS_ERR(mapping)) { - ret = PTR_ERR(mapping); - goto err_config_cleanup; - } + private->domain = iommu_domain_alloc(&platform_bus_type); + if (!private->domain) + return -ENOMEM; - ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32)); + ret = iommu_get_dma_cookie(private->domain); if (ret) - goto err_release_mapping; - - dma_set_max_seg_size(dev, DMA_BIT_MASK(32)); + goto err_free_domain; + + group = iommu_group_get(dev); + if (!group) { + group = iommu_group_alloc(); + if (IS_ERR(group)) { + dev_err(dev, "Failed to allocate IOMMU group\n"); + goto err_put_cookie; + } - ret = arm_iommu_attach_device(dev, mapping); + ret = iommu_group_add_device(group, dev); + iommu_group_put(group); + if (ret) { + dev_err(dev, "failed to add device to IOMMU group\n"); + goto err_put_cookie; + } + } + /* + * Attach virtual iommu device, sub iommu device can share the same + * mapping with it. + */ + ret = rockchip_drm_dma_attach_device(drm_dev, dev); if (ret) - goto err_release_mapping; + goto err_group_remove_device; /* Try to bind all sub drivers. */ ret = component_bind_all(dev, drm_dev); @@ -226,9 +254,13 @@ err_kms_helper_poll_fini: err_unbind: component_unbind_all(dev, drm_dev); err_detach_device: - arm_iommu_detach_device(dev); -err_release_mapping: - arm_iommu_release_mapping(dev->archdata.mapping); + rockchip_drm_dma_detach_device(drm_dev, dev); +err_group_remove_device: + iommu_group_remove_device(dev); +err_put_cookie: + iommu_put_dma_cookie(private->domain); +err_free_domain: + iommu_domain_free(private->domain); err_config_cleanup: drm_mode_config_cleanup(drm_dev); drm_dev->dev_private = NULL; @@ -238,13 +270,16 @@ err_config_cleanup: static int rockchip_drm_unload(struct drm_device *drm_dev) { struct device *dev = drm_dev->dev; + struct rockchip_drm_private *private = drm_dev->dev_private; rockchip_drm_fbdev_fini(drm_dev); drm_vblank_cleanup(drm_dev); drm_kms_helper_poll_fini(drm_dev); component_unbind_all(dev, drm_dev); - arm_iommu_detach_device(dev); - arm_iommu_release_mapping(dev->archdata.mapping); + rockchip_drm_dma_detach_device(drm_dev, dev); + iommu_group_remove_device(dev); + iommu_put_dma_cookie(private->domain); + iommu_domain_free(private->domain); drm_mode_config_cleanup(drm_dev); drm_dev->dev_private = NULL; diff --git a/drivers/gpu/drm/rockchip/rockchip_drm_drv.h b/drivers/gpu/drm/rockchip/rockchip_drm_drv.h index 234cec2..2677b95 100644 --- a/drivers/gpu/drm/rockchip/rockchip_drm_drv.h +++ b/drivers/gpu/drm/rockchip/rockchip_drm_drv.h @@ -62,6 +62,7 @@ struct rockchip_drm_private { const struct rockchip_crtc_funcs *crtc_funcs[ROCKCHIP_MAX_CRTC]; struct rockchip_atomic_commit commit; + struct iommu_domain *domain; }; void rockchip_drm_atomic_work(struct work_struct *work);