From patchwork Mon Feb 13 14:45:06 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Gaignard X-Patchwork-Id: 93895 Delivered-To: patch@linaro.org Received: by 10.182.3.34 with SMTP id 2csp938404obz; Mon, 13 Feb 2017 06:45:26 -0800 (PST) X-Received: by 10.84.241.203 with SMTP id t11mr30513868plm.18.1486997126783; Mon, 13 Feb 2017 06:45:26 -0800 (PST) Return-Path: Received: from gabe.freedesktop.org (gabe.freedesktop.org. [131.252.210.177]) by mx.google.com with ESMTPS id m32si10265116pld.31.2017.02.13.06.45.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 13 Feb 2017 06:45:26 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of dri-devel-bounces@lists.freedesktop.org designates 131.252.210.177 as permitted sender) client-ip=131.252.210.177; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org; spf=pass (google.com: best guess record for domain of dri-devel-bounces@lists.freedesktop.org designates 131.252.210.177 as permitted sender) smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 7DE726E418; Mon, 13 Feb 2017 14:45:25 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wm0-x22e.google.com (mail-wm0-x22e.google.com [IPv6:2a00:1450:400c:c09::22e]) by gabe.freedesktop.org (Postfix) with ESMTPS id B91986E418 for ; Mon, 13 Feb 2017 14:45:23 +0000 (UTC) Received: by mail-wm0-x22e.google.com with SMTP id v77so95302881wmv.0 for ; Mon, 13 Feb 2017 06:45:23 -0800 (PST) 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=o5kctBeFsCGJQ/IJ3SQ765Kw1rQ+jVbh4SnzloqCgUc=; b=EN8FxJmVKtyAw1Kd5Ixf9uXExxTUQ+ZqNtZr+k83UTBRG+XHHjDq8lQi0u7Jd79Q/D BkcO9ZhKPikIX2lBlseRR+9+M0b8CBot2j/znDET9LirGjJwtzBoQfZNEWE23f5CVS1n uriWMr7QPwgUNVUJf0Z2qNSSxdX0fGNJeihoc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=o5kctBeFsCGJQ/IJ3SQ765Kw1rQ+jVbh4SnzloqCgUc=; b=SoB23HI9sQroR7GkXR2img0yFt4q8LwnGaGYjyGX0kK3X+YVBABaM97V6c+VuPWBwW TknSfevTCpHQtMfOjoutb4+PevKFABflkRTHGRcur/fR6vrqdeW0HM+J/NpsVv872uVL lqvg4mMSH6FKRbf5cYzxLV+S+EdRjrTTh8VubBzkm2DZ3PxJnWJ5ZCagBjKdpiKcHRNw G0hzo3ABdnxPLHkVFSGoFcozLBBLhxATrmgVsItTrgdO2cWAhzlqX20smjxrN2xGC0IO LlThQ48DmctijLi0D2HGtbKfb7DV2DROBuUwhXodi00nnH5GExj/0pD3hgWkzS6wzem4 hNMQ== X-Gm-Message-State: AMke39mlnNCl9ybFlL3Y6RS0XVgAcfHyD26U4HBxFSZOceRGbKfZde2OwxoS6NgwnpxYhuNc X-Received: by 10.28.70.2 with SMTP id t2mr9341655wma.34.1486997122268; Mon, 13 Feb 2017 06:45:22 -0800 (PST) Received: from lmenx321.st.com. ([80.215.84.161]) by smtp.gmail.com with ESMTPSA id m84sm5634788wmf.10.2017.02.13.06.45.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 13 Feb 2017 06:45:21 -0800 (PST) From: Benjamin Gaignard To: linaro-kernel@lists.linaro.org, arnd@arndb.de, labbott@redhat.com, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, daniel.vetter@ffwll.ch, laurent.pinchart@ideasonboard.com, robdclark@gmail.com, akpm@linux-foundation.org, hverkuil@xs4all.nl Subject: [RFC simple allocator v2 2/2] add CMA simple allocator module Date: Mon, 13 Feb 2017 15:45:06 +0100 Message-Id: <1486997106-23277-3-git-send-email-benjamin.gaignard@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1486997106-23277-1-git-send-email-benjamin.gaignard@linaro.org> References: <1486997106-23277-1-git-send-email-benjamin.gaignard@linaro.org> Cc: broonie@kernel.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" This patch add simple allocator for CMA regions. version 2: - fix size and page count computation Signed-off-by: Benjamin Gaignard --- drivers/simpleallocator/Kconfig | 7 + drivers/simpleallocator/Makefile | 1 + drivers/simpleallocator/simple-allocator-cma.c | 187 +++++++++++++++++++++++++ 3 files changed, 195 insertions(+) create mode 100644 drivers/simpleallocator/simple-allocator-cma.c diff --git a/drivers/simpleallocator/Kconfig b/drivers/simpleallocator/Kconfig index c6fc2e3..788fb0b 100644 --- a/drivers/simpleallocator/Kconfig +++ b/drivers/simpleallocator/Kconfig @@ -7,4 +7,11 @@ config SIMPLE_ALLOCATOR The Simple Allocator Framework adds an API to allocate and share memory in userland. +config SIMPLE_ALLOCATOR_CMA + tristate "Simple Allocator CMA" + select SIMPLE_ALLOCATOR + depends on DMA_CMA + ---help--- + Select this option to enable Simple Allocator on CMA area. + endmenu diff --git a/drivers/simpleallocator/Makefile b/drivers/simpleallocator/Makefile index e27c6ad..4e11611 100644 --- a/drivers/simpleallocator/Makefile +++ b/drivers/simpleallocator/Makefile @@ -1 +1,2 @@ obj-$(CONFIG_SIMPLE_ALLOCATOR) += simple-allocator.o +obj-$(CONFIG_SIMPLE_ALLOCATOR_CMA) += simple-allocator-cma.o diff --git a/drivers/simpleallocator/simple-allocator-cma.c b/drivers/simpleallocator/simple-allocator-cma.c new file mode 100644 index 0000000..07cbf5b --- /dev/null +++ b/drivers/simpleallocator/simple-allocator-cma.c @@ -0,0 +1,187 @@ +/* + * Copyright (C) Linaro 2017 + * + * Author: Benjamin Gaignard + * + * License terms: GNU General Public License (GPL) + */ + +#include +#include +#include + +#include "simple-allocator-priv.h" +#include "../mm/cma.h" + +struct sa_cma_device { + struct sa_device parent; + struct cma *cma; +}; + +struct sa_cma_buffer_info { + void *vaddr; + size_t count; + size_t size; + struct page *pages; + struct sa_cma_device *sa_cma; +}; + +static struct sa_cma_device *sa_cma[MAX_CMA_AREAS]; + +static inline struct sa_cma_device *to_sa_cma(struct sa_device *sadev) +{ + return container_of(sadev, struct sa_cma_device, parent); +} + +static struct sg_table *sa_cma_map_dma_buf(struct dma_buf_attachment *attach, + enum dma_data_direction direction) +{ + struct dma_buf *dmabuf = attach->dmabuf; + struct sa_cma_buffer_info *info = dmabuf->priv; + struct sg_table *sgt; + int ret; + + ret = sg_alloc_table(sgt, 1, GFP_KERNEL); + if (unlikely(ret)) + return NULL; + + sg_set_page(sgt->sgl, info->pages, info->size, 0); + sg_dma_address(sgt->sgl) = (dma_addr_t) page_address(info->pages); + sg_dma_len(sgt->sgl) = info->size; + + return sgt; +} + +static void sa_cma_unmap_dma_buf(struct dma_buf_attachment *attach, + struct sg_table *sgt, + enum dma_data_direction dir) +{ + kfree(sgt); +} + +static int sa_cma_mmap_dma_buf(struct dma_buf *dmabuf, + struct vm_area_struct *vma) +{ + struct sa_cma_buffer_info *info = dmabuf->priv; + unsigned long user_count = vma_pages(vma); + unsigned long count = info->count; + unsigned long pfn = page_to_pfn(info->pages); + unsigned long off = vma->vm_pgoff; + int ret = -ENXIO; + + if (off < count && user_count <= (count - off)) { + ret = remap_pfn_range(vma, vma->vm_start, + pfn + off, + user_count << PAGE_SHIFT, + vma->vm_page_prot); + } + + return ret; +} + +static void sa_cma_release_dma_buf(struct dma_buf *dmabuf) +{ + struct sa_cma_buffer_info *info = dmabuf->priv; + + cma_release(info->sa_cma->cma, info->pages, info->count); + + kfree(info); +} + +static void *sa_cma_kmap_dma_buf(struct dma_buf *dmabuf, unsigned long offset) +{ + struct sa_cma_buffer_info *info = dmabuf->priv; + + return page_address(info->pages) + offset; +} + +static struct dma_buf_ops sa_dma_buf_ops = { + .map_dma_buf = sa_cma_map_dma_buf, + .unmap_dma_buf = sa_cma_unmap_dma_buf, + .mmap = sa_cma_mmap_dma_buf, + .release = sa_cma_release_dma_buf, + .kmap_atomic = sa_cma_kmap_dma_buf, + .kmap = sa_cma_kmap_dma_buf, +}; + +static struct dma_buf *sa_cma_allocate(struct sa_device *sadev, + u64 length, u32 flags) +{ + DEFINE_DMA_BUF_EXPORT_INFO(exp_info); + struct sa_cma_buffer_info *info; + struct dma_buf *dmabuf; + + info = kzalloc(sizeof(*info), GFP_KERNEL); + if (!info) + return NULL; + + info->size = round_up(length, PAGE_SIZE); + info->count = PAGE_ALIGN(info->size) >> PAGE_SHIFT; + info->sa_cma = to_sa_cma(sadev); + + info->pages = cma_alloc(info->sa_cma->cma, info->count, 0); + + if (!info->pages) + goto cleanup; + + exp_info.ops = &sa_dma_buf_ops; + exp_info.size = info->size; + exp_info.flags = flags; + exp_info.priv = info; + + dmabuf = dma_buf_export(&exp_info); + if (IS_ERR(dmabuf)) + goto export_failed; + + return dmabuf; + +export_failed: + cma_release(info->sa_cma->cma, info->pages, info->count); +cleanup: + kfree(info); + return NULL; +} + +struct sa_cma_device *simple_allocator_register_cma(struct cma *cma) +{ + struct sa_cma_device *sa_cma; + int ret; + + sa_cma = kzalloc(sizeof(*sa_cma), GFP_KERNEL); + if (!sa_cma) + return NULL; + + sa_cma->cma = cma; + sa_cma->parent.owner = THIS_MODULE; + sa_cma->parent.name = "cma"; + sa_cma->parent.allocate = sa_cma_allocate; + + ret = simple_allocator_register(&sa_cma->parent); + if (ret) { + kfree(sa_cma); + return NULL; + } + + return sa_cma; +} + +static int __init sa_cma_init(void) +{ + int i; + + for (i = 0; i < cma_area_count; i++) + sa_cma[i] = simple_allocator_register_cma(&cma_areas[i]); + + return 0; +} + +static void __exit sa_cma_exit(void) +{ + int i; + + for (i = 0; i < cma_area_count; i++) + simple_allocator_unregister(&sa_cma[i]->parent); +} + +module_init(sa_cma_init); +module_exit(sa_cma_exit);