From patchwork Fri Jan 20 15:32:31 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Gaignard X-Patchwork-Id: 9528811 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 8D3CE601AE for ; Fri, 20 Jan 2017 15:32:50 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7A0482842F for ; Fri, 20 Jan 2017 15:32:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6EAD028526; Fri, 20 Jan 2017 15:32:50 +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=-4.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 0B7122842F for ; Fri, 20 Jan 2017 15:32:50 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 3BA986EBDC; Fri, 20 Jan 2017 15:32:49 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wm0-x236.google.com (mail-wm0-x236.google.com [IPv6:2a00:1450:400c:c09::236]) by gabe.freedesktop.org (Postfix) with ESMTPS id 9D3886EBDA for ; Fri, 20 Jan 2017 15:32:45 +0000 (UTC) Received: by mail-wm0-x236.google.com with SMTP id r126so42131035wmr.0 for ; Fri, 20 Jan 2017 07:32:45 -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=leIwBamFMqNW7bK+yQxm7Fin+AUGVh3B1AJPotK2t1k=; b=bi67SoyWgpogdLribBTCMZY4f0NHEGZZ9t+ysg8vMwRdYltwrvBa6byZO2ubcSARA1 qI1agEbEPCCM/3OZZsskbGphKbuuOW9LXGtzJUqfNjkRghtwz9sONLujxgUgEPZ/lLiY /LtNzBKA1D85joGemgqP3T+qhwk4ABHLYgZAs= 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=leIwBamFMqNW7bK+yQxm7Fin+AUGVh3B1AJPotK2t1k=; b=bXTZwf1K+w1Pa77aTSVgnJ5b12Khu164vxQSCANHbiBdNrDvR/hoeKMo7D+uQd7aig VgI67+OlIwbJrzY/1+YRQ+L8zNmJhk60JY2lv6g01GuaYXNRW9JfsLDfJ0QcjY0Pdk5o 653mSgkMsO4XA0bSGKbgaiPOyQjyykSWYr4Kh+thzrJaucNdTXHkmnGo/DHtnGkRGEEg DnEr+l2GI0nSnHnpb9ZZjFKbLHyxqfIVqW45bJpCLrL05X6ask0hIJAotaeYWSODlu+5 pia/0JJFVKw8Ox7BE70YtIlpYp8hpDZR7S1x+qeEm60E6A9VgUyoiNF06TSirIjYGuIt 8RPw== X-Gm-Message-State: AIkVDXJdMuuQAvVo3XO8yMvu57D0WKH73TGX6QgZQT/yyjT7yQ2QJEOxiNz41rlzA1kL6GM9 X-Received: by 10.28.39.7 with SMTP id n7mr4020838wmn.112.1484926364210; Fri, 20 Jan 2017 07:32:44 -0800 (PST) Received: from lmenx321.st.com. ([80.214.31.157]) by smtp.gmail.com with ESMTPSA id 61sm252981wrs.29.2017.01.20.07.32.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 20 Jan 2017 07:32:43 -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 Subject: [RFC simple allocator v1 2/2] add CMA simple allocator module Date: Fri, 20 Jan 2017 16:32:31 +0100 Message-Id: <1484926351-30185-3-git-send-email-benjamin.gaignard@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1484926351-30185-1-git-send-email-benjamin.gaignard@linaro.org> References: <1484926351-30185-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" X-Virus-Scanned: ClamAV using ClamSMTP This patch add simple allocator for CMA regions 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..b240913 --- /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, PAGE_ALIGN(info->size), 0); + sg_dma_address(sgt->sgl) = (dma_addr_t) page_address(info->pages); + sg_dma_len(sgt->sgl) = PAGE_ALIGN(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->count = round_up(length, PAGE_SIZE); + info->size = length; + 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);