From: John Stultz <john.stultz@linaro.org>
To: Laura Abbott <labbott@redhat.com>
Cc: Chenbo Feng <fengc@google.com>,
Alistair Strachan <astrachan@google.com>,
Liam Mark <lmark@codeaurora.org>, "Andrew F . Davis" <afd@ti.com>,
dri-devel@lists.freedesktop.org
Subject: [EARLY RFC][PATCH 4/4] dma-buf: pools: Add CMA pool to dmabuf pools
Date: Wed, 20 Feb 2019 23:40:30 -0800 [thread overview]
Message-ID: <1550734830-23499-5-git-send-email-john.stultz@linaro.org> (raw)
In-Reply-To: <1550734830-23499-1-git-send-email-john.stultz@linaro.org>
This adds a CMA pool, which allows userspace to allocate
a dma-buf of contiguous memory out of a CMA region.
Cc: Laura Abbott <labbott@redhat.com>
Cc: Benjamin Gaignard <benjamin.gaignard@linaro.org>
Cc: Sumit Semwal <sumit.semwal@linaro.org>
Cc: Liam Mark <lmark@codeaurora.org>
Cc: Brian Starkey <Brian.Starkey@arm.com>
Cc: Andrew F. Davis <afd@ti.com>
Cc: Chenbo Feng <fengc@google.com>
Cc: Alistair Strachan <astrachan@google.com>
Cc: dri-devel@lists.freedesktop.org
Signed-off-by: John Stultz <john.stultz@linaro.org>
---
drivers/dma-buf/pools/Kconfig | 8 +++
drivers/dma-buf/pools/Makefile | 1 +
drivers/dma-buf/pools/cma_pool.c | 143 +++++++++++++++++++++++++++++++++++++++
3 files changed, 152 insertions(+)
create mode 100644 drivers/dma-buf/pools/cma_pool.c
diff --git a/drivers/dma-buf/pools/Kconfig b/drivers/dma-buf/pools/Kconfig
index 787b2a6..e984304 100644
--- a/drivers/dma-buf/pools/Kconfig
+++ b/drivers/dma-buf/pools/Kconfig
@@ -15,3 +15,11 @@ config DMABUF_POOLS_SYSTEM
help
Choose this option to enable the system dmabuf pool. The system pool
is backed by pages from the buddy allocator. If in doubt, say Y.
+
+config DMABUF_POOLS_CMA
+ bool "DMA-BUF CMA Pool"
+ depends on DMABUF_POOLS && DMA_CMA
+ help
+ Choose this option to enable dma-buf CMA pools. This pool is backed
+ by the Contiguous Memory Allocator (CMA). If your system has these
+ regions, you should say Y here.
diff --git a/drivers/dma-buf/pools/Makefile b/drivers/dma-buf/pools/Makefile
index 2ccf2a1..ac8aa28 100644
--- a/drivers/dma-buf/pools/Makefile
+++ b/drivers/dma-buf/pools/Makefile
@@ -1,3 +1,4 @@
# SPDX-License-Identifier: GPL-2.0
obj-$(CONFIG_DMABUF_POOLS) += dmabuf-pools.o pool-ioctl.o pool-helpers.o page_pool.o
obj-$(CONFIG_DMABUF_POOLS_SYSTEM) += system_pool.o
+obj-$(CONFIG_DMABUF_POOLS_CMA) += cma_pool.o
diff --git a/drivers/dma-buf/pools/cma_pool.c b/drivers/dma-buf/pools/cma_pool.c
new file mode 100644
index 0000000..0bd783f
--- /dev/null
+++ b/drivers/dma-buf/pools/cma_pool.c
@@ -0,0 +1,143 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * drivers/dma-buf/pools/cma_pool.c
+ *
+ * Copyright (C) 2012, 2019 Linaro Ltd.
+ * Author: <benjamin.gaignard@linaro.org> for ST-Ericsson.
+ */
+
+#include <linux/device.h>
+#include <linux/slab.h>
+#include <linux/errno.h>
+#include <linux/err.h>
+#include <linux/cma.h>
+#include <linux/scatterlist.h>
+#include <linux/highmem.h>
+
+#include "dmabuf-pools.h"
+
+struct cma_pool {
+ struct dmabuf_pool pool;
+ struct cma *cma;
+};
+
+#define to_cma_pool(x) container_of(x, struct cma_pool, pool)
+
+/* dmabuf pool CMA operations functions */
+static int cma_pool_allocate(struct dmabuf_pool *pool,
+ struct dmabuf_pool_buffer *buffer,
+ unsigned long len,
+ unsigned long flags)
+{
+ struct cma_pool *cma_pool = to_cma_pool(pool);
+ struct sg_table *table;
+ struct page *pages;
+ unsigned long size = PAGE_ALIGN(len);
+ unsigned long nr_pages = size >> PAGE_SHIFT;
+ unsigned long align = get_order(size);
+ int ret;
+
+ if (align > CONFIG_CMA_ALIGNMENT)
+ align = CONFIG_CMA_ALIGNMENT;
+
+ pages = cma_alloc(cma_pool->cma, nr_pages, align, false);
+ if (!pages)
+ return -ENOMEM;
+
+ if (PageHighMem(pages)) {
+ unsigned long nr_clear_pages = nr_pages;
+ struct page *page = pages;
+
+ while (nr_clear_pages > 0) {
+ void *vaddr = kmap_atomic(page);
+
+ memset(vaddr, 0, PAGE_SIZE);
+ kunmap_atomic(vaddr);
+ page++;
+ nr_clear_pages--;
+ }
+ } else {
+ memset(page_address(pages), 0, size);
+ }
+
+ table = kmalloc(sizeof(*table), GFP_KERNEL);
+ if (!table)
+ goto err;
+
+ ret = sg_alloc_table(table, 1, GFP_KERNEL);
+ if (ret)
+ goto free_mem;
+
+ sg_set_page(table->sgl, pages, size, 0);
+
+ buffer->priv_virt = pages;
+ buffer->sg_table = table;
+ return 0;
+
+free_mem:
+ kfree(table);
+err:
+ cma_release(cma_pool->cma, pages, nr_pages);
+ return -ENOMEM;
+}
+
+static void cma_pool_free(struct dmabuf_pool_buffer *buffer)
+{
+ struct cma_pool *cma_pool = to_cma_pool(buffer->pool);
+ struct page *pages = buffer->priv_virt;
+ unsigned long nr_pages = PAGE_ALIGN(buffer->size) >> PAGE_SHIFT;
+
+ /* release memory */
+ cma_release(cma_pool->cma, pages, nr_pages);
+ /* release sg table */
+ sg_free_table(buffer->sg_table);
+ kfree(buffer->sg_table);
+}
+
+static struct dmabuf_pool_ops cma_pool_ops = {
+ .allocate = cma_pool_allocate,
+ .free = cma_pool_free,
+ .map_user = dmabuf_pool_map_user,
+ .map_kernel = dmabuf_pool_map_kernel,
+ .unmap_kernel = dmabuf_pool_unmap_kernel,
+};
+
+static struct dmabuf_pool *__cma_pool_create(struct cma *cma)
+{
+ struct cma_pool *cma_pool;
+
+ cma_pool = kzalloc(sizeof(*cma_pool), GFP_KERNEL);
+
+ if (!cma_pool)
+ return ERR_PTR(-ENOMEM);
+
+ cma_pool->pool.ops = &cma_pool_ops;
+ /*
+ * get device from private pools data, later it will be
+ * used to make the link with reserved CMA memory
+ */
+ cma_pool->cma = cma;
+ return &cma_pool->pool;
+}
+
+static int __add_cma_pools(struct cma *cma, void *data)
+{
+ struct dmabuf_pool *pool;
+
+ pool = __cma_pool_create(cma);
+ if (IS_ERR(pool))
+ return PTR_ERR(pool);
+
+ pool->name = cma_get_name(cma);
+
+ dmabuf_pool_add(pool);
+
+ return 0;
+}
+
+static int add_cma_pools(void)
+{
+ cma_for_each_area(__add_cma_pools, NULL);
+ return 0;
+}
+device_initcall(add_cma_pools);
--
2.7.4
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel
next prev parent reply other threads:[~2019-02-21 7:40 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-02-21 7:40 [EARLY RFC][PATCH 0/4] dmabuf pools infrastructure (destaging ION) John Stultz
2019-02-21 7:40 ` [EARLY RFC][PATCH 1/4] dma-buf: Add dma-buf pools framework John Stultz
2019-02-21 7:40 ` [EARLY RFC][PATCH 2/4] dma-buf: pools: Add page-pool for dma-buf pools John Stultz
2019-02-21 7:40 ` [EARLY RFC][PATCH 3/4] dma-buf: pools: Add system/system-contig pools to dmabuf pools John Stultz
2019-02-21 7:40 ` John Stultz [this message]
2019-02-22 7:19 ` [EARLY RFC][PATCH 0/4] dmabuf pools infrastructure (destaging ION) John Stultz
2019-02-22 16:55 ` Andrew F. Davis
2019-02-22 17:24 ` John Stultz
2019-02-22 20:45 ` John Stultz
2019-02-23 6:21 ` John Stultz
2019-02-22 22:30 ` Laura Abbott
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1550734830-23499-5-git-send-email-john.stultz@linaro.org \
--to=john.stultz@linaro.org \
--cc=afd@ti.com \
--cc=astrachan@google.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=fengc@google.com \
--cc=labbott@redhat.com \
--cc=lmark@codeaurora.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox