From patchwork Sat Jun 3 10:59:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sui Jingfeng <15330273260@189.cn> X-Patchwork-Id: 102825 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1594179vqr; Sat, 3 Jun 2023 04:12:41 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7WM707eFlIZpfTRtG1Iebm+o6yueeTvskFdizjfiSSRKoY3V7p0VsJ71WByJdyP0ZNjhEK X-Received: by 2002:a05:6a20:c513:b0:10e:43e:e223 with SMTP id gm19-20020a056a20c51300b0010e043ee223mr176257pzb.1.1685790760693; Sat, 03 Jun 2023 04:12:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685790760; cv=none; d=google.com; s=arc-20160816; b=ULTI4fl/A+8ptgkruT+mep4PlDoVHK+RI6B7bzSETWRlWYyM+aVmWY7JNe28sMp70z s5XR2w8gy+LNI9SFz0upg1m6QieNhXYNq4LeW2/gFsK2ZhZ86z0SgC1RtfEGg63GRi4a Am/MoRi7ChNoZvPbVkJAOS7DKbRsdW9Ybnt/P+UBmu8c0nCdDglSeAv9m25Pydavh+R+ 2D5BHNRnH60en+1B0oR21po2waz/f437MT3Hzoj3c4u++ANcMEQ58rbiLVE8Ig6iN05Q +HteIlcISVRVui7JQU6gU6O15s0OfTETI7IWF3MWLIJaF/afeZ0Q5G6A37VlKXbAgl36 OB6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from:sender :hmm_source_type:hmm_attache_num:hmm_source_ip; bh=2eVF406j7jlvZZifgsgsEuc0m0pLr1Cx8tzSTyvg5JQ=; b=o+NKvmVVFzwSpCIi1+xNtiqFGX7NudtjhsAlmO+lv6KbgOlGmGq6dWqifAHO6BPOE5 nZyO9itTXI7KTX0NdoATMgsQUkb888IodhjxfPboY1QGnI003oRERIh77UhT16269dFO +n82RRdG4sZxgLl1HDQqpPiCZQHB8/4qiTiWWxLpHQFEib2RM95fR61NqPE8RuaQlF0Y 13bLquuVjtsrxYBkxrzV5MTthzUDoU5w80ete1LIGMOri/RUxj3ZxW0KkUpqXVI/EkFC GPljaj6vpW2m5n+Jwc8ae7ALTI3EAdGmZTqTsNolkWtBG2PNiFz6BRSTJfH59jEmjgVF Zzuw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j19-20020a170902759300b001ae52127485si2355859pll.433.2023.06.03.04.12.23; Sat, 03 Jun 2023 04:12:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237198AbjFCLAe (ORCPT + 99 others); Sat, 3 Jun 2023 07:00:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58956 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231932AbjFCLAN (ORCPT ); Sat, 3 Jun 2023 07:00:13 -0400 Received: from 189.cn (ptr.189.cn [183.61.185.101]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 4E530BD for ; Sat, 3 Jun 2023 04:00:12 -0700 (PDT) HMM_SOURCE_IP: 10.64.8.43:51732.1826327623 HMM_ATTACHE_NUM: 0000 HMM_SOURCE_TYPE: SMTP Received: from clientip-114.242.206.180 (unknown [10.64.8.43]) by 189.cn (HERMES) with SMTP id 90B4210013F; Sat, 3 Jun 2023 19:00:10 +0800 (CST) Received: from ([114.242.206.180]) by gateway-151646-dep-75648544bd-7vx9t with ESMTP id 2dcf4eaa048745feabcf6e04b324142e for l.stach@pengutronix.de; Sat, 03 Jun 2023 19:00:11 CST X-Transaction-ID: 2dcf4eaa048745feabcf6e04b324142e X-Real-From: 15330273260@189.cn X-Receive-IP: 114.242.206.180 X-MEDUSA-Status: 0 Sender: 15330273260@189.cn From: Sui Jingfeng <15330273260@189.cn> To: Lucas Stach , Christian Gmeiner , Daniel Vetter , Bjorn Helgaas Cc: linux-kernel@vger.kernel.org, etnaviv@lists.freedesktop.org, dri-devel@lists.freedesktop.org, Sui Jingfeng Subject: [PATCH v7 7/7] drm/etnaviv: add support for the dma coherent device Date: Sat, 3 Jun 2023 18:59:43 +0800 Message-Id: <20230603105943.3042766-8-15330273260@189.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230603105943.3042766-1-15330273260@189.cn> References: <20230603105943.3042766-1-15330273260@189.cn> MIME-Version: 1.0 X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00, FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,FROM_LOCAL_DIGITS, FROM_LOCAL_HEX,SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767679732696942807?= X-GMAIL-MSGID: =?utf-8?q?1767679732696942807?= From: Sui Jingfeng Loongson CPUs maintain cache coherency by hardware, which means that the data in the CPU cache is identical to the data in main system memory. As for the peripheral device, most of Loongson chips chose to define the peripherals as DMA coherent by default, device drivers do not need to maintain the coherency between a processor and an I/O device manually. There are exceptions, for LS2K1000 SoC, part of peripheral device can be configured as dma non-coherent. But there is no released version of such firmware exist in the market. Peripherals of older ls2k1000 is also DMA non-conherent, but they are nearly outdated. So, those are trivial cases. Nevertheless, kernel space still need to do probe work, because vivante GPU IP has been integrated into various platform. Hence, this patch add runtime detection code to probe if a specific gpu is DMA coherent, If the answer is yes, we are going to utilize such features. On Loongson platfform, When a buffer is accesed by both the GPU and the CPU, The driver should prefer ETNA_BO_CACHED over ETNA_BO_WC. This patch also add a new parameter: etnaviv_param_gpu_coherent, which allow userspace to know if such a feature is available. Because write-combined BO is still preferred in some case, especially where don't need CPU read, for example, uploading shader bin. Signed-off-by: Sui Jingfeng --- drivers/gpu/drm/etnaviv/etnaviv_drv.c | 36 +++++++++++++++++++++ drivers/gpu/drm/etnaviv/etnaviv_drv.h | 6 ++++ drivers/gpu/drm/etnaviv/etnaviv_gem.c | 22 ++++++++++--- drivers/gpu/drm/etnaviv/etnaviv_gem_prime.c | 7 +++- drivers/gpu/drm/etnaviv/etnaviv_gpu.c | 7 +++- include/uapi/drm/etnaviv_drm.h | 1 + 6 files changed, 73 insertions(+), 6 deletions(-) diff --git a/drivers/gpu/drm/etnaviv/etnaviv_drv.c b/drivers/gpu/drm/etnaviv/etnaviv_drv.c index 033afe542a3a..3d05c33cce9f 100644 --- a/drivers/gpu/drm/etnaviv/etnaviv_drv.c +++ b/drivers/gpu/drm/etnaviv/etnaviv_drv.c @@ -5,7 +5,9 @@ #include #include +#include #include +#include #include #include @@ -27,6 +29,36 @@ #include "etnaviv_pci_drv.h" #endif +static struct device_node *etnaviv_of_first_available_node(void) +{ + struct device_node *core_node; + + for_each_compatible_node(core_node, NULL, "vivante,gc") { + if (!of_device_is_available(core_node)) + continue; + + return core_node; + } + + return NULL; +} + +static bool etnaviv_is_dma_coherent(struct device *dev) +{ + struct device_node *np; + bool coherent; + + np = etnaviv_of_first_available_node(); + if (np) { + coherent = of_dma_is_coherent(np); + of_node_put(np); + } else { + coherent = dev_is_dma_coherent(dev); + } + + return coherent; +} + /* * etnaviv private data construction and destructions: */ @@ -55,6 +87,10 @@ etnaviv_alloc_private(struct device *dev, struct drm_device *drm) return ERR_PTR(-ENOMEM); } + priv->dma_coherent = etnaviv_is_dma_coherent(dev); + + drm_info(drm, "%s is dma coherent\n", dev_name(dev)); + return priv; } diff --git a/drivers/gpu/drm/etnaviv/etnaviv_drv.h b/drivers/gpu/drm/etnaviv/etnaviv_drv.h index 9cd72948cfad..ad386ea21c3b 100644 --- a/drivers/gpu/drm/etnaviv/etnaviv_drv.h +++ b/drivers/gpu/drm/etnaviv/etnaviv_drv.h @@ -46,6 +46,12 @@ struct etnaviv_drm_private { struct xarray active_contexts; u32 next_context_id; + /* + * If true, the GPU is capable of snoop cpu cache, here, it also + * means that cache coherency is enforced by the hardware. + */ + bool dma_coherent; + /* list of GEM objects: */ struct mutex gem_lock; struct list_head gem_list; diff --git a/drivers/gpu/drm/etnaviv/etnaviv_gem.c b/drivers/gpu/drm/etnaviv/etnaviv_gem.c index b5f73502e3dd..39bdc3774f2d 100644 --- a/drivers/gpu/drm/etnaviv/etnaviv_gem.c +++ b/drivers/gpu/drm/etnaviv/etnaviv_gem.c @@ -343,6 +343,7 @@ void *etnaviv_gem_vmap(struct drm_gem_object *obj) static void *etnaviv_gem_vmap_impl(struct etnaviv_gem_object *obj) { struct page **pages; + pgprot_t prot; lockdep_assert_held(&obj->lock); @@ -350,8 +351,19 @@ static void *etnaviv_gem_vmap_impl(struct etnaviv_gem_object *obj) if (IS_ERR(pages)) return NULL; - return vmap(pages, obj->base.size >> PAGE_SHIFT, - VM_MAP, pgprot_writecombine(PAGE_KERNEL)); + switch (obj->flags) { + case ETNA_BO_CACHED: + prot = PAGE_KERNEL; + break; + case ETNA_BO_UNCACHED: + prot = pgprot_noncached(PAGE_KERNEL); + break; + case ETNA_BO_WC: + default: + prot = pgprot_writecombine(PAGE_KERNEL); + } + + return vmap(pages, obj->base.size >> PAGE_SHIFT, VM_MAP, prot); } static inline enum dma_data_direction etnaviv_op_to_dma_dir(u32 op) @@ -369,6 +381,7 @@ int etnaviv_gem_cpu_prep(struct drm_gem_object *obj, u32 op, { struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); struct drm_device *dev = obj->dev; + struct etnaviv_drm_private *priv = dev->dev_private; bool write = !!(op & ETNA_PREP_WRITE); int ret; @@ -395,7 +408,7 @@ int etnaviv_gem_cpu_prep(struct drm_gem_object *obj, u32 op, return ret == 0 ? -ETIMEDOUT : ret; } - if (etnaviv_obj->flags & ETNA_BO_CACHED) { + if (!priv->dma_coherent && etnaviv_obj->flags & ETNA_BO_CACHED) { dma_sync_sgtable_for_cpu(dev->dev, etnaviv_obj->sgt, etnaviv_op_to_dma_dir(op)); etnaviv_obj->last_cpu_prep_op = op; @@ -408,8 +421,9 @@ int etnaviv_gem_cpu_fini(struct drm_gem_object *obj) { struct drm_device *dev = obj->dev; struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj); + struct etnaviv_drm_private *priv = dev->dev_private; - if (etnaviv_obj->flags & ETNA_BO_CACHED) { + if (!priv->dma_coherent && etnaviv_obj->flags & ETNA_BO_CACHED) { /* fini without a prep is almost certainly a userspace error */ WARN_ON(etnaviv_obj->last_cpu_prep_op == 0); dma_sync_sgtable_for_device(dev->dev, etnaviv_obj->sgt, diff --git a/drivers/gpu/drm/etnaviv/etnaviv_gem_prime.c b/drivers/gpu/drm/etnaviv/etnaviv_gem_prime.c index 3524b5811682..754126992264 100644 --- a/drivers/gpu/drm/etnaviv/etnaviv_gem_prime.c +++ b/drivers/gpu/drm/etnaviv/etnaviv_gem_prime.c @@ -112,11 +112,16 @@ static const struct etnaviv_gem_ops etnaviv_gem_prime_ops = { struct drm_gem_object *etnaviv_gem_prime_import_sg_table(struct drm_device *dev, struct dma_buf_attachment *attach, struct sg_table *sgt) { + struct etnaviv_drm_private *priv = dev->dev_private; struct etnaviv_gem_object *etnaviv_obj; size_t size = PAGE_ALIGN(attach->dmabuf->size); + u32 cache_flags = ETNA_BO_WC; int ret, npages; - ret = etnaviv_gem_new_private(dev, size, ETNA_BO_WC, + if (priv->dma_coherent) + cache_flags = ETNA_BO_CACHED; + + ret = etnaviv_gem_new_private(dev, size, cache_flags, &etnaviv_gem_prime_ops, &etnaviv_obj); if (ret < 0) return ERR_PTR(ret); diff --git a/drivers/gpu/drm/etnaviv/etnaviv_gpu.c b/drivers/gpu/drm/etnaviv/etnaviv_gpu.c index f0eb808496e2..4dc4645a2e52 100644 --- a/drivers/gpu/drm/etnaviv/etnaviv_gpu.c +++ b/drivers/gpu/drm/etnaviv/etnaviv_gpu.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -164,6 +165,10 @@ int etnaviv_gpu_get_param(struct etnaviv_gpu *gpu, u32 param, u64 *value) *value = gpu->identity.eco_id; break; + case ETNAVIV_PARAM_GPU_COHERENT: + *value = priv->dma_coherent; + break; + default: DBG("%s: invalid param: %u", dev_name(gpu->dev), param); return -EINVAL; @@ -1861,7 +1866,7 @@ static int etnaviv_gpu_register_irq(struct etnaviv_gpu *gpu, int irq) gpu->irq = irq; - dev_info(dev, "IRQ handler registered, irq = %d\n", irq); + dev_info(dev, "irq(%d) handler registered\n", irq); return 0; } diff --git a/include/uapi/drm/etnaviv_drm.h b/include/uapi/drm/etnaviv_drm.h index af024d90453d..76baf45d7158 100644 --- a/include/uapi/drm/etnaviv_drm.h +++ b/include/uapi/drm/etnaviv_drm.h @@ -77,6 +77,7 @@ struct drm_etnaviv_timespec { #define ETNAVIV_PARAM_GPU_PRODUCT_ID 0x1c #define ETNAVIV_PARAM_GPU_CUSTOMER_ID 0x1d #define ETNAVIV_PARAM_GPU_ECO_ID 0x1e +#define ETNAVIV_PARAM_GPU_COHERENT 0x1f #define ETNA_MAX_PIPES 4