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: 9569907 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 DBC6F6045D for ; Mon, 13 Feb 2017 14:46:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BF60D26E5D for ; Mon, 13 Feb 2017 14:46:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B3887281DB; Mon, 13 Feb 2017 14:46:20 +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=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3D8EB2711E for ; Mon, 13 Feb 2017 14:46:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752322AbdBMOqG (ORCPT ); Mon, 13 Feb 2017 09:46:06 -0500 Received: from mail-wm0-f52.google.com ([74.125.82.52]:38429 "EHLO mail-wm0-f52.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752361AbdBMOpY (ORCPT ); Mon, 13 Feb 2017 09:45:24 -0500 Received: by mail-wm0-f52.google.com with SMTP id r141so95729384wmg.1 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=DiJl13J+px3sLEurKcidRlBr4UNuuOTHsuonMpC4xpv+cT4XCjJvtnnxawdKBBh3Kc vtNvH3dxgi6vaLx/oRFHC0cJph/SAGQbOtLQgeSQev8TGPMnXXwpYB22+zfnPx8bDQeO /lcBubcMYMJGOAi6s+HaUJxF2WZw52Sy+DNFoK72gh+3jkIdqqVBCQCoONj8fuBqPmv2 h2y9HpsKMlgtKZ8FGF9MTK6Jd6zqQcqDe2jzcphWDraS84ccwKZWmh0/VqHaHMwVLzSA p4LkYC0uF/Ftj46ZprF76xjOGiiK44zK+G7GvflYbhScY4THaIl8olMG8g0afilfPybc F7KA== X-Gm-Message-State: AMke39n7+U+38rfod3jgtGY3/RnreE2JMBa8OvJ46vu90Eiiiuf3xopU4YlcvPyORBlxnxjj 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 Cc: broonie@kernel.org, Benjamin Gaignard 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> Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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);