From patchwork Wed Sep 30 17:16:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Cercueil X-Patchwork-Id: 11811129 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 31EDC92C for ; Thu, 1 Oct 2020 08:16:29 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 0877820BED for ; Thu, 1 Oct 2020 08:16:29 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=crapouillou.net header.i=@crapouillou.net header.b="MHTvilFi" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0877820BED Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=crapouillou.net Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 8847C6E86E; Thu, 1 Oct 2020 08:15:47 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from crapouillou.net (crapouillou.net [89.234.176.41]) by gabe.freedesktop.org (Postfix) with ESMTPS id DC55F6E5BD for ; Wed, 30 Sep 2020 17:17:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crapouillou.net; s=mail; t=1601486218; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=K4FdEWnxywTg58dVaxSVDCY6CPJH5VaSpaRHUxXoYO8=; b=MHTvilFiFcEoV8ERIrzbb2lSr7HlhKRtVWEcAwCR0gCnShWJ90CkLCYMXEbi/NCMHZluMx 3RXGT3638NQsQ6QarhTsZOtv2vWScq5hVTDCU+HePWop1+dwSJwxWY9TD231Xz7E1Gqvr2 BbsN3E3GQO9nRiMlFePjh24K6Oc+ys8= From: Paul Cercueil To: Christoph Hellwig Subject: [PATCH 1/3] drm: Add and export function drm_gem_cma_create_noalloc Date: Wed, 30 Sep 2020 19:16:42 +0200 Message-Id: <20200930171644.299363-1-paul@crapouillou.net> In-Reply-To: <20200930165212.GA8833@lst.de> References: <20200930165212.GA8833@lst.de> MIME-Version: 1.0 X-Mailman-Approved-At: Thu, 01 Oct 2020 08:15:43 +0000 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Stephen Rothwell , Dave Airlie , Linux Kernel Mailing List , DRI , Paul Cercueil , Linux Next Mailing List Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Add and export the function drm_gem_cma_create_noalloc(), which is just __drm_gem_cma_create() renamed. This function can be used by drivers that need to create a GEM object without allocating the backing memory. Signed-off-by: Paul Cercueil --- drivers/gpu/drm/drm_gem_cma_helper.c | 11 ++++++----- include/drm/drm_gem_cma_helper.h | 3 +++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/drivers/gpu/drm/drm_gem_cma_helper.c b/drivers/gpu/drm/drm_gem_cma_helper.c index 59b9ca207b42..6abc4b306832 100644 --- a/drivers/gpu/drm/drm_gem_cma_helper.c +++ b/drivers/gpu/drm/drm_gem_cma_helper.c @@ -34,7 +34,7 @@ */ /** - * __drm_gem_cma_create - Create a GEM CMA object without allocating memory + * drm_gem_cma_create_noalloc - Create a GEM CMA object without allocating memory * @drm: DRM device * @size: size of the object to allocate * @@ -45,8 +45,8 @@ * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative * error code on failure. */ -static struct drm_gem_cma_object * -__drm_gem_cma_create(struct drm_device *drm, size_t size) +struct drm_gem_cma_object * +drm_gem_cma_create_noalloc(struct drm_device *drm, size_t size) { struct drm_gem_cma_object *cma_obj; struct drm_gem_object *gem_obj; @@ -76,6 +76,7 @@ __drm_gem_cma_create(struct drm_device *drm, size_t size) kfree(cma_obj); return ERR_PTR(ret); } +EXPORT_SYMBOL_GPL(drm_gem_cma_create_noalloc); /** * drm_gem_cma_create - allocate an object with the given size @@ -98,7 +99,7 @@ struct drm_gem_cma_object *drm_gem_cma_create(struct drm_device *drm, size = round_up(size, PAGE_SIZE); - cma_obj = __drm_gem_cma_create(drm, size); + cma_obj = drm_gem_cma_create_noalloc(drm, size); if (IS_ERR(cma_obj)) return cma_obj; @@ -476,7 +477,7 @@ drm_gem_cma_prime_import_sg_table(struct drm_device *dev, return ERR_PTR(-EINVAL); /* Create a CMA GEM buffer. */ - cma_obj = __drm_gem_cma_create(dev, attach->dmabuf->size); + cma_obj = drm_gem_cma_create_noalloc(dev, attach->dmabuf->size); if (IS_ERR(cma_obj)) return ERR_CAST(cma_obj); diff --git a/include/drm/drm_gem_cma_helper.h b/include/drm/drm_gem_cma_helper.h index 2bfa2502607a..be2b8e3a8ab2 100644 --- a/include/drm/drm_gem_cma_helper.h +++ b/include/drm/drm_gem_cma_helper.h @@ -83,6 +83,9 @@ int drm_gem_cma_mmap(struct file *filp, struct vm_area_struct *vma); struct drm_gem_cma_object *drm_gem_cma_create(struct drm_device *drm, size_t size); +struct drm_gem_cma_object * +drm_gem_cma_create_noalloc(struct drm_device *drm, size_t size); + extern const struct vm_operations_struct drm_gem_cma_vm_ops; #ifndef CONFIG_MMU From patchwork Wed Sep 30 17:16:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Cercueil X-Patchwork-Id: 11811133 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id DD14E6CA for ; Thu, 1 Oct 2020 08:16:32 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id ABE0F20BED for ; Thu, 1 Oct 2020 08:16:32 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=crapouillou.net header.i=@crapouillou.net header.b="OsmF8Szl" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org ABE0F20BED Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=crapouillou.net Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id B0FA56E161; Thu, 1 Oct 2020 08:15:47 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from crapouillou.net (crapouillou.net [89.234.176.41]) by gabe.freedesktop.org (Postfix) with ESMTPS id B88936E7D1 for ; Wed, 30 Sep 2020 17:17:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crapouillou.net; s=mail; t=1601486219; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pIdTpoIPruFr7zDIb5GEOw9aXWxj4OPiHxHv5ba4g4M=; b=OsmF8Szlaf1dkvepalM6e6Wg9Av0V4QRmzmc4gtuZGzQgljOp2uaP/tpzHshzIdOzpy3xt BZwh6RSsb/u80Di9iucXTw/aoKPwww71tbT5kOhmLhvVmhxSnhzErpgtv+fIG0T+M/KDzQ C9rg6fxTyXIAiGBmhz6S+sPSucmC3UU= From: Paul Cercueil To: Christoph Hellwig Subject: [PATCH 2/3] drm/ingenic: Update code to mmap GEM buffers cached Date: Wed, 30 Sep 2020 19:16:43 +0200 Message-Id: <20200930171644.299363-2-paul@crapouillou.net> In-Reply-To: <20200930165212.GA8833@lst.de> References: <20200930165212.GA8833@lst.de> MIME-Version: 1.0 X-Mailman-Approved-At: Thu, 01 Oct 2020 08:15:43 +0000 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Stephen Rothwell , Dave Airlie , Linux Kernel Mailing List , DRI , Paul Cercueil , Linux Next Mailing List Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" The DMA API changed at the same time commit 37054fc81443 ("gpu/drm: ingenic: Add option to mmap GEM buffers cached") was added. Rework the code to work with the new DMA API. Signed-off-by: Paul Cercueil --- drivers/gpu/drm/ingenic/ingenic-drm-drv.c | 24 +++++++---------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/drivers/gpu/drm/ingenic/ingenic-drm-drv.c b/drivers/gpu/drm/ingenic/ingenic-drm-drv.c index 0225dc1f5eb8..07a1da7266e4 100644 --- a/drivers/gpu/drm/ingenic/ingenic-drm-drv.c +++ b/drivers/gpu/drm/ingenic/ingenic-drm-drv.c @@ -526,12 +526,10 @@ void ingenic_drm_sync_data(struct device *dev, struct drm_plane_state *state) { const struct drm_format_info *finfo = state->fb->format; - struct ingenic_drm *priv = dev_get_drvdata(dev); struct drm_atomic_helper_damage_iter iter; unsigned int offset, i; struct drm_rect clip; dma_addr_t paddr; - void *addr; if (!ingenic_drm_cached_gem_buf) return; @@ -541,12 +539,11 @@ void ingenic_drm_sync_data(struct device *dev, drm_atomic_for_each_plane_damage(&iter, &clip) { for (i = 0; i < finfo->num_planes; i++) { paddr = drm_fb_cma_get_gem_addr(state->fb, state, i); - addr = phys_to_virt(paddr); /* Ignore x1/x2 values, invalidate complete lines */ offset = clip.y1 * state->fb->pitches[i]; - dma_cache_sync(priv->dev, addr + offset, + dma_sync_single_for_device(dev, paddr + offset, (clip.y2 - clip.y1) * state->fb->pitches[i], DMA_TO_DEVICE); } @@ -766,14 +763,6 @@ static int ingenic_drm_gem_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma) { struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj); - struct device *dev = cma_obj->base.dev->dev; - unsigned long attrs; - int ret; - - if (ingenic_drm_cached_gem_buf) - attrs = DMA_ATTR_NON_CONSISTENT; - else - attrs = DMA_ATTR_WRITE_COMBINE; /* * Clear the VM_PFNMAP flag that was set by drm_gem_mmap(), and set the @@ -784,12 +773,13 @@ static int ingenic_drm_gem_mmap(struct drm_gem_object *obj, vma->vm_pgoff = 0; vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); - ret = dma_mmap_attrs(dev, vma, cma_obj->vaddr, cma_obj->paddr, - vma->vm_end - vma->vm_start, attrs); - if (ret) - drm_gem_vm_close(vma); + if (!ingenic_drm_cached_gem_buf) + vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot); - return ret; + return remap_pfn_range(vma, vma->vm_start, + cma_obj->paddr >> PAGE_SHIFT, + vma->vm_end - vma->vm_start, + vma->vm_page_prot); } static int ingenic_drm_gem_cma_mmap(struct file *filp, From patchwork Wed Sep 30 17:16:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Cercueil X-Patchwork-Id: 11811127 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 81CAC6CA for ; Thu, 1 Oct 2020 08:16:24 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 5A86821707 for ; Thu, 1 Oct 2020 08:16:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=crapouillou.net header.i=@crapouillou.net header.b="Q1M8TCre" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5A86821707 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=crapouillou.net Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 74D096E876; Thu, 1 Oct 2020 08:15:49 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from crapouillou.net (crapouillou.net [89.234.176.41]) by gabe.freedesktop.org (Postfix) with ESMTPS id BA8D26E5BD for ; Wed, 30 Sep 2020 17:17:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crapouillou.net; s=mail; t=1601486221; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=MXd9FesjqBdfbqp1WDnMgZQG0KSCw8ALHirqsj8ZIvU=; b=Q1M8TCreSMw4TEd/wVmYXUwXkzJWC/396oJNC4iYtdLXeXIX61dDCkObyoa5q4mgf1/JFq tyix7UkRyRWzxk5pe34OdvxuB2Axya0XEnpgBhz8Oa8Bj0lZEwFc6LZ5n3yqpss4kAAzwE rhzoJB7K3N5mZJenx1iM9S34L9Wg4tY= From: Paul Cercueil To: Christoph Hellwig Subject: [PATCH 3/3] drm/ingenic: Alloc cached GEM buffers with dma_alloc_noncoherent Date: Wed, 30 Sep 2020 19:16:44 +0200 Message-Id: <20200930171644.299363-3-paul@crapouillou.net> In-Reply-To: <20200930165212.GA8833@lst.de> References: <20200930165212.GA8833@lst.de> MIME-Version: 1.0 X-Mailman-Approved-At: Thu, 01 Oct 2020 08:15:43 +0000 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Stephen Rothwell , Dave Airlie , Linux Kernel Mailing List , DRI , Paul Cercueil , Linux Next Mailing List Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" It turns out that if you want to mmap GEM buffers fully cached, then they should be allocated as such as well. Who would have known? Introduce a custom .dumb_create callback, that will behave just like drm_gem_cma_dumb_create(), except that it will allocate the GEM buffer using dma_alloc_noncoherent() if non-coherent memory is what we want. Signed-off-by: Paul Cercueil --- drivers/gpu/drm/ingenic/ingenic-drm-drv.c | 48 ++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/ingenic/ingenic-drm-drv.c b/drivers/gpu/drm/ingenic/ingenic-drm-drv.c index 07a1da7266e4..8ece269c040f 100644 --- a/drivers/gpu/drm/ingenic/ingenic-drm-drv.c +++ b/drivers/gpu/drm/ingenic/ingenic-drm-drv.c @@ -794,6 +794,52 @@ static int ingenic_drm_gem_cma_mmap(struct file *filp, return ingenic_drm_gem_mmap(vma->vm_private_data, vma); } +static int ingenic_drm_gem_cma_dumb_create(struct drm_file *file_priv, + struct drm_device *drm, + struct drm_mode_create_dumb *args) +{ + /* + * This is basically a copy of drm_gem_cma_dumb_create, which supports + * creating fully cached GEM buffers. + */ + struct drm_gem_cma_object *cma_obj; + struct drm_gem_object *gem_obj; + size_t size; + int ret; + + args->pitch = DIV_ROUND_UP(args->width * args->bpp, 8); + args->size = args->pitch * args->height; + + size = PAGE_ALIGN(args->size); + + cma_obj = drm_gem_cma_create_noalloc(drm, size); + if (IS_ERR(cma_obj)) + return PTR_ERR(cma_obj); + + if (ingenic_drm_cached_gem_buf) { + cma_obj->vaddr = dma_alloc_noncoherent(drm->dev, size, + &cma_obj->paddr, + DMA_TO_DEVICE, + GFP_KERNEL | __GFP_NOWARN); + } else { + cma_obj->vaddr = dma_alloc_wc(drm->dev, size, &cma_obj->paddr, + GFP_KERNEL | __GFP_NOWARN); + } + if (!cma_obj->vaddr) { + dev_err(drm->dev, "Failed to allocate buffer with size %zu\n", size); + ret = -ENOMEM; + goto out_gem_object_put; + } + + gem_obj = &cma_obj->base; + + ret = drm_gem_handle_create(file_priv, gem_obj, &args->handle); + +out_gem_object_put: + drm_gem_object_put(gem_obj); + return ret; +} + static const struct file_operations ingenic_drm_fops = { .owner = THIS_MODULE, .open = drm_open, @@ -816,7 +862,7 @@ static struct drm_driver ingenic_drm_driver_data = { .patchlevel = 0, .fops = &ingenic_drm_fops, - DRM_GEM_CMA_DRIVER_OPS, + DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(ingenic_drm_gem_cma_dumb_create), .irq_handler = ingenic_drm_irq_handler, };