Skip to contentSkip to conversation

dma-heap: Keep track of the heap device struct

RFC PATCH Superseded John Stultz 3 participants 5 messages Jul 29, 2020 – Aug 14, 2020

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/2 dma-heap: Keep track of the heap device struct

Keep track of the heap device struct.

This will be useful for special DMA allocations
and actions.

Cc Sumit Semwal <[email protected]>
Cc Andrew F. Davis <[email protected]>
Cc Benjamin Gaignard <[email protected]>
Cc Liam Mark <[email protected]>
Cc Laura Abbott <[email protected]>
Cc Brian Starkey <[email protected]>
Cc Hridya Valsaraju <[email protected]>
---
 drivers/dma-buf/dma-heap.c | 33 +++++++++++++++++++++++++--------
 include/linux/dma-heap.h   |  9 +++++++++
 2 files changed, 34 insertions(+), 8 deletions(-)
drivers/dma-buf/dma-heap.c +25 -8
index afd22c9dbdcf..72c746755d89 100644
@@ -30,6 +30,7 @@
  * @heap_devt		heap device node
  * @list		list head connecting to list of heaps
  * @heap_cdev		heap char device
+ * @heap_dev		heap device struct
  *
  * Represents a heap of memory from which buffers can be made.
  */
@@ -40,6 +41,7 @@ struct dma_heap {
 	dev_t heap_devt;
 	struct list_head list;
 	struct cdev heap_cdev;
+	struct device *heap_dev;
 };
 
 static LIST_HEAD(heap_list);
@@ -190,10 +192,21 @@ void *dma_heap_get_drvdata(struct dma_heap *heap)
 	return heap->priv;
 }
 
+/**
+ * dma_heap_get_dev() - get device struct for the heap
+ * @heap: DMA-Heap to retrieve device struct from
+ *
+ * Returns:
+ * The device struct for the heap.
+ */
+struct device *dma_heap_get_dev(struct dma_heap *heap)
+{
+	return heap->heap_dev;
+}
+
 struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info)
 {
 	struct dma_heap *heap, *h, *err_ret;
-	struct device *dev_ret;
 	unsigned int minor;
 	int ret;
 
@@ -247,16 +260,20 @@ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info)
 		goto err1;
 	}
 
-	dev_ret = device_create(dma_heap_class,
-				NULL,
-				heap->heap_devt,
-				NULL,
-				heap->name);
-	if (IS_ERR(dev_ret)) {
+	heap->heap_dev = device_create(dma_heap_class,
+				       NULL,
+				       heap->heap_devt,
+				       NULL,
+				       heap->name);
+	if (IS_ERR(heap->heap_dev)) {
 		pr_err("dma_heap: Unable to create device\n");
-		err_ret = ERR_CAST(dev_ret);
+		err_ret = ERR_CAST(heap->heap_dev);
 		goto err2;
 	}
+
+	/* Make sure it doesn't disappear on us */
+	heap->heap_dev = get_device(heap->heap_dev);
+
 	/* Add heap to the list */
 	mutex_lock(&heap_list_lock);
 	list_add(&heap->list, &heap_list);
include/linux/dma-heap.h +9 -0
index 454e354d1ffb..82857e096910 100644
@@ -50,6 +50,15 @@ struct dma_heap_export_info {
  */
 void *dma_heap_get_drvdata(struct dma_heap *heap);
 
+/**
+ * dma_heap_get_dev() - get device struct for the heap
+ * @heap: DMA-Heap to retrieve device struct from
+ *
+ * Returns:
+ * The device struct for the heap.
+ */
+struct device *dma_heap_get_dev(struct dma_heap *heap);
+
 /**
  * dma_heap_add - adds a heap to dmabuf heaps
  * @exp_info:		information needed to register this heap
Signature · 6 lines
2.17.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#1 lore raw

RFC PATCH 2/2 dma-heap: Add a system-uncached heap

This adds a heap that allocates non-contiguous buffers that are marked as writecombined, so they are not cached by the CPU.

This is useful, as most graphics buffers are usually not touched by the CPU or only written into once by the CPU. So when mapping the buffer over and over between devices, we can skip the CPU syncing, which saves a lot of cache management overhead, greatly improving performance.

For folk using ION, there was a ION_FLAG_CACHED flag, which signaled if the returned buffer should be CPU cacheable or not. With DMA-BUF heaps, we have no such flag, and by default the current heaps (system and cma) produce CPU cachable buffers. So for folks transitioning from ION to DMA-BUF Heaps, this fills in some of that missing functionality.

This does have a few "ugly" bits that were required to get the buffer properly flushed out initially which I'd like to improve. So feedback would be very welcome!

Many thanks to Liam Mark for his help to get this working.

Cc Sumit Semwal <[email protected]>
Cc Andrew F. Davis <[email protected]>
Cc Benjamin Gaignard <[email protected]>
Cc Liam Mark <[email protected]>
Cc Laura Abbott <[email protected]>
Cc Brian Starkey <[email protected]>
Cc Hridya Valsaraju <[email protected]>
---
 drivers/dma-buf/heaps/Kconfig                |  10 +
 drivers/dma-buf/heaps/Makefile               |   1 +
 drivers/dma-buf/heaps/system_uncached_heap.c | 392 +++++++++++++++++++
 3 files changed, 403 insertions(+)
 create mode 100644 drivers/dma-buf/heaps/system_uncached_heap.c
drivers/dma-buf/heaps/Kconfig +10 -0
index a5eef06c4226..420b0ed0a512 100644
@@ -5,6 +5,16 @@ config DMABUF_HEAPS_SYSTEM
 	  Choose this option to enable the system dmabuf heap. The system heap
 	  is backed by pages from the buddy allocator. If in doubt, say Y.
 
+config DMABUF_HEAPS_SYSTEM_UNCACHED
+	bool "DMA-BUF Uncached System Heap"
+	depends on DMABUF_HEAPS
+	help
+	  Choose this option to enable the uncached system dmabuf heap. This
+	  heap is backed by pages from the buddy allocator, but pages are setup
+	  for write combining. This avoids cache management overhead, and can
+	  be faster if pages are mostly untouched by the cpu.  If in doubt,
+	  say Y.
+
 config DMABUF_HEAPS_CMA
 	bool "DMA-BUF CMA Heap"
 	depends on DMABUF_HEAPS && DMA_CMA
drivers/dma-buf/heaps/Makefile +1 -0
index 6e54cdec3da0..085685ec478f 100644
@@ -1,4 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-y					+= heap-helpers.o
 obj-$(CONFIG_DMABUF_HEAPS_SYSTEM)	+= system_heap.o
+obj-$(CONFIG_DMABUF_HEAPS_SYSTEM_UNCACHED) += system_uncached_heap.o
 obj-$(CONFIG_DMABUF_HEAPS_CMA)		+= cma_heap.o
drivers/dma-buf/heaps/system_uncached_heap.c new file +392 -0

392 lines not on this page · raw message

Signature · 6 lines
2.17.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#2 lore raw

Re: [RFC][PATCH 2/2] dma-heap: Add a system-uncached heap

[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]

Hi John,

[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on linux/master]
[also build test ERROR on tegra-drm/drm/tegra/for-next linus/master v5.8-rc7 next-20200728] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/John-Stultz/dma-heap-Keep-track-of-the-heap-device-struct/20200729-131809
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 6ba1b005ffc388c2aeaddae20da29e4810dea298
config: sh-allmodconfig (attached as .config)
compiler: sh4-linux-gcc (GCC) 9.3.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=sh

If you fix the issue, kindly add following tag as appropriate

All error/warnings (new ones prefixed by >>):

drivers/dma-buf/heaps/system_uncached_heap.c: In function 'dma_heap_do_vmap':

>> drivers/dma-buf/heaps/system_uncached_heap.c:175:24: error: implicit declaration of function 'vmalloc'; did you mean 'kmalloc'? [-Werror=implicit-function-declaration]
     175 |  struct page **pages = vmalloc(sizeof(struct page *) * npages);
         |                        ^~~~~~~
         |                        kmalloc
>> drivers/dma-buf/heaps/system_uncached_heap.c:175:24: warning: initialization of 'struct page **' from 'int' makes pointer from integer without a cast [-Wint-conversion]
>> drivers/dma-buf/heaps/system_uncached_heap.c:195:10: error: implicit declaration of function 'vmap'; did you mean 'kmap'? [-Werror=implicit-function-declaration]
     195 |  vaddr = vmap(pages, npages, VM_MAP, pgprot);
         |          ^~~~
         |          kmap
>> drivers/dma-buf/heaps/system_uncached_heap.c:195:30: error: 'VM_MAP' undeclared (first use in this function)
     195 |  vaddr = vmap(pages, npages, VM_MAP, pgprot);
         |                              ^~~~~~
   drivers/dma-buf/heaps/system_uncached_heap.c:195:30: note: each undeclared identifier is reported only once for each function it appears in
>> drivers/dma-buf/heaps/system_uncached_heap.c:196:2: error: implicit declaration of function 'vfree'; did you mean 'kfree'? [-Werror=implicit-function-declaration]
     196 |  vfree(pages);
         |  ^~~~~
         |  kfree
   drivers/dma-buf/heaps/system_uncached_heap.c: In function 'dma_heap_buffer_vmap_put':
>> drivers/dma-buf/heaps/system_uncached_heap.c:225:3: error: implicit declaration of function 'vunmap'; did you mean 'kunmap'? [-Werror=implicit-function-declaration]
     225 |   vunmap(buffer->vaddr);
         |   ^~~~~~
         |   kunmap

cc1: some warnings being treated as errors

vim +175 drivers/dma-buf/heaps/system_uncached_heap.c

170
171 static void *dma_heap_do_vmap(struct uncached_heap_buffer *buffer)
172 {
173 struct sg_table *table = &buffer->sg_table;
174 int npages = PAGE_ALIGN(buffer->len) / PAGE_SIZE;

 > 175		struct page **pages = vmalloc(sizeof(struct page *) * npages);
   176		struct page **tmp = pages;
   177		struct scatterlist *sg;
   178		pgprot_t pgprot;
   179		void *vaddr;
   180		int i, j;
   181	
   182		if (!pages)
   183			return ERR_PTR(-ENOMEM);
   184	
   185		pgprot = pgprot_writecombine(PAGE_KERNEL);
   186	
   187		for_each_sg(table->sgl, sg, table->nents, i) {
   188			int npages_this_entry = PAGE_ALIGN(sg->length) / PAGE_SIZE;
   189			struct page *page = sg_page(sg);
   190	
   191			WARN_ON(i >= npages);
   192			for (j = 0; j < npages_this_entry; j++)
   193				*(tmp++) = page++;
   194		}
 > 195		vaddr = vmap(pages, npages, VM_MAP, pgprot);
 > 196		vfree(pages);
   197	
   198		if (!vaddr)
   199			return ERR_PTR(-ENOMEM);
   200	
   201		return vaddr;
   202	}
   203	
   204	static void *dma_heap_buffer_vmap_get(struct uncached_heap_buffer *buffer)
   205	{
   206		void *vaddr;
   207	
   208		if (buffer->vmap_cnt) {
   209			buffer->vmap_cnt++;
   210			return buffer->vaddr;
   211		}
   212	
   213		vaddr = dma_heap_do_vmap(buffer);
   214		if (IS_ERR(vaddr))
   215			return vaddr;
   216	
   217		buffer->vaddr = vaddr;
   218		buffer->vmap_cnt++;
   219		return vaddr;
   220	}
   221	
   222	static void dma_heap_buffer_vmap_put(struct uncached_heap_buffer *buffer)
   223	{
   224		if (!--buffer->vmap_cnt) {
 > 225			vunmap(buffer->vaddr);

226 buffer->vaddr = NULL;
227 }
228 }
229

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#3 lore raw

Re: [RFC][PATCH 2/2] dma-heap: Add a system-uncached heap

In reply to John Stultz
Quoted 125 lines · John Stultz

This adds a heap that allocates non-contiguous buffers that are marked as writecombined, so they are not cached by the CPU.

This is useful, as most graphics buffers are usually not touched by the CPU or only written into once by the CPU. So when mapping the buffer over and over between devices, we can skip the CPU syncing, which saves a lot of cache management overhead, greatly improving performance.

For folk using ION, there was a ION_FLAG_CACHED flag, which signaled if the returned buffer should be CPU cacheable or not. With DMA-BUF heaps, we have no such flag, and by default the current heaps (system and cma) produce CPU cachable buffers. So for folks transitioning from ION to DMA-BUF Heaps, this fills in some of that missing functionality.

This does have a few "ugly" bits that were required to get the buffer properly flushed out initially which I'd like to improve. So feedback would be very welcome!

Many thanks to Liam Mark for his help to get this working.

Cc Sumit Semwal <[email protected]>
Cc Andrew F. Davis <[email protected]>
Cc Benjamin Gaignard <[email protected]>
Cc Liam Mark <[email protected]>
Cc Laura Abbott <[email protected]>
Cc Brian Starkey <[email protected]>
Cc Hridya Valsaraju <[email protected]>
---
  drivers/dma-buf/heaps/Kconfig                |  10 +
  drivers/dma-buf/heaps/Makefile               |   1 +
  drivers/dma-buf/heaps/system_uncached_heap.c | 392 +++++++++++++++++++
  3 files changed, 403 insertions(+)

create mode 100644 drivers/dma-buf/heaps/system_uncached_heap.c

drivers/dma-buf/heaps/Kconfig +10 -0
index a5eef06c4226..420b0ed0a512 100644
@@ -5,6 +5,16 @@ config DMABUF_HEAPS_SYSTEM
  	  Choose this option to enable the system dmabuf heap. The system heap
  	  is backed by pages from the buddy allocator. If in doubt, say Y.
 
+config DMABUF_HEAPS_SYSTEM_UNCACHED
+	bool "DMA-BUF Uncached System Heap"
+	depends on DMABUF_HEAPS
+	help
+	  Choose this option to enable the uncached system dmabuf heap. This
+	  heap is backed by pages from the buddy allocator, but pages are setup
+	  for write combining. This avoids cache management overhead, and can
+	  be faster if pages are mostly untouched by the cpu.  If in doubt,
+	  say Y.
+
  config DMABUF_HEAPS_CMA
  	bool "DMA-BUF CMA Heap"
  	depends on DMABUF_HEAPS && DMA_CMA
drivers/dma-buf/heaps/Makefile +1 -0
index 6e54cdec3da0..085685ec478f 100644
@@ -1,4 +1,5 @@
  # SPDX-License-Identifier: GPL-2.0
  obj-y					+= heap-helpers.o
  obj-$(CONFIG_DMABUF_HEAPS_SYSTEM)	+= system_heap.o
+obj-$(CONFIG_DMABUF_HEAPS_SYSTEM_UNCACHED) += system_uncached_heap.o
  obj-$(CONFIG_DMABUF_HEAPS_CMA)		+= cma_heap.o
drivers/dma-buf/heaps/system_uncached_heap.c new file +49 -0
new file mode 100644
index 000000000000..d23908038376
@@ -0,0 +1,392 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Uncached System DMA-Heap exporter
+ *
+ * Copyright (C) 2020 Linaro Ltd.
+ *
+ * Based off of Andrew Davis' SRAM heap:
+ * Copyright (C) 2019 Texas Instruments Incorporated - http://www.ti.com/
+ *	Andrew F. Davis <[email protected]>
+ */
+
+#include <linux/dma-mapping.h>
+#include <linux/err.h>
+#include <linux/highmem.h>
+#include <linux/io.h>
+#include <linux/mm.h>
+#include <linux/scatterlist.h>
+#include <linux/slab.h>
+#include <linux/dma-buf.h>
+#include <linux/dma-heap.h>
+
+struct uncached_heap {
+	struct dma_heap *heap;
+};
+
+struct uncached_heap_buffer {
+	struct dma_heap *heap;
+	struct list_head attachments;
+	struct mutex lock;
+	unsigned long len;
+	struct sg_table sg_table;
+	int vmap_cnt;
+	void *vaddr;
+};
+
+struct dma_heap_attachment {
+	struct device *dev;
+	struct sg_table *table;
+	struct list_head list;
+};
+
+static struct sg_table *dup_sg_table(struct sg_table *table)
+{
+	struct sg_table *new_table;
+	int ret, i;
+	struct scatterlist *sg, *new_sg;
+
+	new_table = kzalloc(sizeof(*new_table), GFP_KERNEL);
+	if (!new_table)

drivers/dma-buf/heaps/system_uncached_heap.c line 59

Quoted 10 lines
drivers/dma-buf/heaps/system_uncached_heap.c new file +10 -0
+		return ERR_PTR(-ENOMEM);
+
+	ret = sg_alloc_table(new_table, table->nents, GFP_KERNEL);
+	if (ret) {
+		kfree(new_table);
+		return ERR_PTR(-ENOMEM);
+	}
+
+	new_sg = new_table->sgl;
+	for_each_sg(table->sgl, sg, table->nents, i) {

Consider using the new sgtable helpers that are just about to land - in this case, for_each_sgtable_sg().

drivers/dma-buf/heaps/system_uncached_heap.c line 61

Quoted 2 lines
drivers/dma-buf/heaps/system_uncached_heap.c new file +2 -0
+		memcpy(new_sg, sg, sizeof(*sg));
+		new_sg->dma_address = 0;

This seems a little bit hairy, as in theory a consumer could still treat a nonzero DMA length as the address being valid. Rather than copying the whole entry then trying to undo parts of that, maybe just:

	sg_set_page(new_sg, sg_page(sg), sg->len, sg->offset);

?

Quoted 49 lines
drivers/dma-buf/heaps/system_uncached_heap.c new file +49 -0
+		new_sg = sg_next(new_sg);
+	}
+
+	return new_table;
+}
+
+static int dma_heap_attach(struct dma_buf *dmabuf,
+			   struct dma_buf_attachment *attachment)
+{
+	struct uncached_heap_buffer *buffer = dmabuf->priv;
+	struct dma_heap_attachment *a;
+	struct sg_table *table;
+
+	a = kzalloc(sizeof(*a), GFP_KERNEL);
+	if (!a)
+		return -ENOMEM;
+
+	table = dup_sg_table(&buffer->sg_table);
+	if (IS_ERR(table)) {
+		kfree(a);
+		return -ENOMEM;
+	}
+
+	a->table = table;
+	a->dev = attachment->dev;
+	INIT_LIST_HEAD(&a->list);
+
+	attachment->priv = a;
+
+	mutex_lock(&buffer->lock);
+	list_add(&a->list, &buffer->attachments);
+	mutex_unlock(&buffer->lock);
+
+	return 0;
+}
+
+static void dma_heap_detatch(struct dma_buf *dmabuf,
+			     struct dma_buf_attachment *attachment)
+{
+	struct uncached_heap_buffer *buffer = dmabuf->priv;
+	struct dma_heap_attachment *a = attachment->priv;
+
+	mutex_lock(&buffer->lock);
+	list_del(&a->list);
+	mutex_unlock(&buffer->lock);
+
+	sg_free_table(a->table);
+	kfree(a->table);
+	kfree(a);

drivers/dma-buf/heaps/system_uncached_heap.c line 120

Quoted 10 lines
drivers/dma-buf/heaps/system_uncached_heap.c new file +10 -0
+}
+
+static struct sg_table *dma_heap_map_dma_buf(struct dma_buf_attachment *attachment,
+					     enum dma_data_direction direction)
+{
+	struct dma_heap_attachment *a = attachment->priv;
+	struct sg_table *table = a->table;
+
+	if (!dma_map_sg_attrs(attachment->dev, table->sgl, table->nents, direction,
+			      DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WRITE_COMBINE))

dma_map_sgtable()

Also, DMA_ATTR_WRITE_COMBINE is meaningless for streaming DMA.

drivers/dma-buf/heaps/system_uncached_heap.c line 131

Quoted 11 lines
drivers/dma-buf/heaps/system_uncached_heap.c new file +11 -0
+		return ERR_PTR(-ENOMEM);
+
+	return table;
+}
+
+static void dma_heap_unmap_dma_buf(struct dma_buf_attachment *attachment,
+				   struct sg_table *table,
+				   enum dma_data_direction direction)
+{
+	dma_unmap_sg_attrs(attachment->dev, table->sgl, table->nents, direction,
+			   DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WRITE_COMBINE);

Similarly.

Quoted 46 lines
drivers/dma-buf/heaps/system_uncached_heap.c new file +46 -0
+}
+
+static int dma_heap_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
+{
+	struct uncached_heap_buffer *buffer = dmabuf->priv;
+	struct sg_table *table = &buffer->sg_table;
+	unsigned long addr = vma->vm_start;
+	unsigned long offset = vma->vm_pgoff * PAGE_SIZE;
+	struct scatterlist *sg;
+	int i;
+	int ret;
+
+	vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
+
+	for_each_sg(table->sgl, sg, table->nents, i) {
+		struct page *page = sg_page(sg);
+		unsigned long remainder = vma->vm_end - addr;
+		unsigned long len = sg->length;
+
+		if (offset >= sg->length) {
+			offset -= sg->length;
+			continue;
+		} else if (offset) {
+			page += offset / PAGE_SIZE;
+			len = sg->length - offset;
+			offset = 0;
+		}
+		len = min(len, remainder);
+		ret = remap_pfn_range(vma, addr, page_to_pfn(page), len,
+				      vma->vm_page_prot);
+		if (ret)
+			return ret;
+		addr += len;
+		if (addr >= vma->vm_end)
+			return 0;
+	}
+	return 0;
+}
+
+static void *dma_heap_do_vmap(struct uncached_heap_buffer *buffer)
+{
+	struct sg_table *table = &buffer->sg_table;
+	int npages = PAGE_ALIGN(buffer->len) / PAGE_SIZE;
+	struct page **pages = vmalloc(sizeof(struct page *) * npages);
+	struct page **tmp = pages;
+	struct scatterlist *sg;

drivers/dma-buf/heaps/system_uncached_heap.c line 187

Quoted 10 lines
drivers/dma-buf/heaps/system_uncached_heap.c new file +10 -0
+	pgprot_t pgprot;
+	void *vaddr;
+	int i, j;
+
+	if (!pages)
+		return ERR_PTR(-ENOMEM);
+
+	pgprot = pgprot_writecombine(PAGE_KERNEL);
+
+	for_each_sg(table->sgl, sg, table->nents, i) {

for_each_sg_page()

Quoted 150 lines
drivers/dma-buf/heaps/system_uncached_heap.c new file +150 -0
+		int npages_this_entry = PAGE_ALIGN(sg->length) / PAGE_SIZE;
+		struct page *page = sg_page(sg);
+
+		WARN_ON(i >= npages);
+		for (j = 0; j < npages_this_entry; j++)
+			*(tmp++) = page++;
+	}
+	vaddr = vmap(pages, npages, VM_MAP, pgprot);
+	vfree(pages);
+
+	if (!vaddr)
+		return ERR_PTR(-ENOMEM);
+
+	return vaddr;
+}
+
+static void *dma_heap_buffer_vmap_get(struct uncached_heap_buffer *buffer)
+{
+	void *vaddr;
+
+	if (buffer->vmap_cnt) {
+		buffer->vmap_cnt++;
+		return buffer->vaddr;
+	}
+
+	vaddr = dma_heap_do_vmap(buffer);
+	if (IS_ERR(vaddr))
+		return vaddr;
+
+	buffer->vaddr = vaddr;
+	buffer->vmap_cnt++;
+	return vaddr;
+}
+
+static void dma_heap_buffer_vmap_put(struct uncached_heap_buffer *buffer)
+{
+	if (!--buffer->vmap_cnt) {
+		vunmap(buffer->vaddr);
+		buffer->vaddr = NULL;
+	}
+}
+
+static void *dma_heap_vmap(struct dma_buf *dmabuf)
+{
+	struct uncached_heap_buffer *buffer = dmabuf->priv;
+	void *vaddr;
+
+	mutex_lock(&buffer->lock);
+	vaddr = dma_heap_buffer_vmap_get(buffer);
+	mutex_unlock(&buffer->lock);
+
+	return vaddr;
+}
+
+static void dma_heap_vunmap(struct dma_buf *dmabuf, void *vaddr)
+{
+	struct uncached_heap_buffer *buffer = dmabuf->priv;
+
+	mutex_lock(&buffer->lock);
+	dma_heap_buffer_vmap_put(buffer);
+	mutex_unlock(&buffer->lock);
+}
+
+static void dma_heap_dma_buf_release(struct dma_buf *dmabuf)
+{
+	struct uncached_heap_buffer *buffer = dmabuf->priv;
+	struct sg_table *table;
+	struct scatterlist *sg;
+	int i;
+
+	table = &buffer->sg_table;
+	for_each_sg(table->sgl, sg, table->nents, i)
+		__free_page(sg_page(sg));
+	sg_free_table(table);
+	kfree(buffer);
+}
+
+const struct dma_buf_ops uncached_heap_buf_ops = {
+	.attach = dma_heap_attach,
+	.detach = dma_heap_detatch,
+	.map_dma_buf = dma_heap_map_dma_buf,
+	.unmap_dma_buf = dma_heap_unmap_dma_buf,
+	.mmap = dma_heap_mmap,
+	.vmap = dma_heap_vmap,
+	.vunmap = dma_heap_vunmap,
+	.release = dma_heap_dma_buf_release,
+};
+
+static int uncached_heap_allocate(struct dma_heap *heap,
+				  unsigned long len,
+				  unsigned long fd_flags,
+				  unsigned long heap_flags)
+{
+	struct uncached_heap_buffer *buffer;
+	DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
+	struct dma_buf *dmabuf;
+	struct sg_table *table;
+	struct scatterlist *sg;
+	pgoff_t pagecount;
+	pgoff_t pg;
+	int i, ret = -ENOMEM;
+
+	buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
+	if (!buffer)
+		return -ENOMEM;
+
+	INIT_LIST_HEAD(&buffer->attachments);
+	mutex_init(&buffer->lock);
+	buffer->heap = heap;
+	buffer->len = len;
+
+	table = &buffer->sg_table;
+	pagecount = len / PAGE_SIZE;
+	if (sg_alloc_table(table, pagecount, GFP_KERNEL))
+		goto free_buffer;
+
+	sg = table->sgl;
+	for (pg = 0; pg < pagecount; pg++) {
+		struct page *page;
+		/*
+		 * Avoid trying to allocate memory if the process
+		 * has been killed by by SIGKILL
+		 */
+		if (fatal_signal_pending(current))
+			goto free_pages;
+		page = alloc_page(GFP_KERNEL | __GFP_ZERO);
+		if (!page)
+			goto free_pages;
+		sg_set_page(sg, page, page_size(page), 0);
+		sg = sg_next(sg);
+	}
+
+	/* create the dmabuf */
+	exp_info.ops = &uncached_heap_buf_ops;
+	exp_info.size = buffer->len;
+	exp_info.flags = fd_flags;
+	exp_info.priv = buffer;
+	dmabuf = dma_buf_export(&exp_info);
+	if (IS_ERR(dmabuf)) {
+		ret = PTR_ERR(dmabuf);
+		goto free_pages;
+	}
+
+	ret = dma_buf_fd(dmabuf, fd_flags);
+	if (ret < 0) {
+		dma_buf_put(dmabuf);
+		/* just return, as put will call release and that will free */
+		return ret;
+	}
+

drivers/dma-buf/heaps/system_uncached_heap.c line 347

Quoted 10 lines
drivers/dma-buf/heaps/system_uncached_heap.c new file +10 -0
+	/*
+	 * XXX This is hackish. While the buffer will be uncached, we need
+	 * to initially flush cpu cache, since the the __GFP_ZERO on the
+	 * allocation means the zeroing was done by the cpu and thus it is likely
+	 * cached. Map & flush it out now so we don't get corruption later on.
+	 *
+	 * Ideally we could do this without using the heap device as a dummy dev.
+	 */
+	dma_map_sg_attrs(dma_heap_get_dev(heap), table->sgl, table->nents,
+			 DMA_BIDIRECTIONAL, DMA_ATTR_WRITE_COMBINE);

Again, DMA_ATTR_WRITE_COMBINE is meaningless here.

drivers/dma-buf/heaps/system_uncached_heap.c line 349

Quoted 2 lines
drivers/dma-buf/heaps/system_uncached_heap.c new file +2 -0
+	dma_sync_sg_for_device(dma_heap_get_dev(heap), table->sgl, table->nents,
+			       DMA_BIDIRECTIONAL);

This doesn't do anything that the map hasn't already just done.

Quoted 29 lines
drivers/dma-buf/heaps/system_uncached_heap.c new file +29 -0
+
+	return ret;
+
+free_pages:
+	for_each_sg(table->sgl, sg, table->nents, i)
+		__free_page(sg_page(sg));
+	sg_free_table(table);
+free_buffer:
+	kfree(buffer);
+
+	return ret;
+}
+
+static struct dma_heap_ops uncached_heap_ops = {
+	.allocate = uncached_heap_allocate,
+};
+
+static int uncached_heap_create(void)
+{
+	struct uncached_heap *heap;
+	struct dma_heap_export_info exp_info;
+	static u64 dummy_mask;
+
+	heap = kzalloc(sizeof(*heap), GFP_KERNEL);
+	if (!heap)
+		return -ENOMEM;
+
+	exp_info.name = "system-uncached";
+	exp_info.ops = &uncached_heap_ops;

drivers/dma-buf/heaps/system_uncached_heap.c line 388

Quoted 10 lines
drivers/dma-buf/heaps/system_uncached_heap.c new file +10 -0
+	exp_info.priv = heap;
+	heap->heap = dma_heap_add(&exp_info);
+	if (IS_ERR(heap->heap)) {
+		int ret = PTR_ERR(heap->heap);
+
+		kfree(heap);
+		return ret;
+	}
+	dma_heap_get_dev(heap->heap)->dma_mask = &dummy_mask;
+	dma_set_mask(dma_heap_get_dev(heap->heap), DMA_BIT_MASK(64));

Much as I'd hate to encourage using dma_coerce_mask_and_coherent(), I'm not sure this is really any better :/

Robin.

drivers/dma-buf/heaps/system_uncached_heap.c line 392

Quoted 5 lines
drivers/dma-buf/heaps/system_uncached_heap.c new file +4 -0
+
+	return 0;
+}
+device_initcall(uncached_heap_create);

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#4 lore raw

Re: [RFC][PATCH 2/2] dma-heap: Add a system-uncached heap

Quoted 5 lines · Robin Murphy
Quoted 3 lines · John Stultz

This adds a heap that allocates non-contiguous buffers that are marked as writecombined, so they are not cached by the CPU.

...

Quoted 11 lines

drivers/dma-buf/heaps/system_uncached_heap.c line 59

Quoted 8 lines
drivers/dma-buf/heaps/system_uncached_heap.c new file +8 -0
+     ret = sg_alloc_table(new_table, table->nents, GFP_KERNEL);
+     if (ret) {
+             kfree(new_table);
+             return ERR_PTR(-ENOMEM);
+     }
+
+     new_sg = new_table->sgl;
+     for_each_sg(table->sgl, sg, table->nents, i) {

Consider using the new sgtable helpers that are just about to land - in this case, for_each_sgtable_sg().

Ack! Thanks for the suggestion!

Quoted 10 lines

drivers/dma-buf/heaps/system_uncached_heap.c line 61

Quoted 2 lines
drivers/dma-buf/heaps/system_uncached_heap.c new file +2 -0
+             memcpy(new_sg, sg, sizeof(*sg));
+             new_sg->dma_address = 0;

This seems a little bit hairy, as in theory a consumer could still treat a nonzero DMA length as the address being valid. Rather than copying the whole entry then trying to undo parts of that, maybe just:

        sg_set_page(new_sg, sg_page(sg), sg->len, sg->offset);

?

Sounds good.

Quoted 11 lines

drivers/dma-buf/heaps/system_uncached_heap.c line 120

Quoted 8 lines
drivers/dma-buf/heaps/system_uncached_heap.c new file +8 -0
+static struct sg_table *dma_heap_map_dma_buf(struct dma_buf_attachment *attachment,
+                                          enum dma_data_direction direction)
+{
+     struct dma_heap_attachment *a = attachment->priv;
+     struct sg_table *table = a->table;
+
+     if (!dma_map_sg_attrs(attachment->dev, table->sgl, table->nents, direction,
+                           DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WRITE_COMBINE))

dma_map_sgtable()

Quoted 2 lines

Also, DMA_ATTR_WRITE_COMBINE is meaningless for streaming DMA.

Hrm. Ok, my grasp of "streaming" vs "consistent" definitions are maybe slightly off, since while we are mapping and unmapping buffers, the point of this heap is that the allocated memory is uncached/coherent, so we avoid the cache sync overhead on each mapping/unmapping, which I thought was closer to the "consistent" definition.

But maybe if the mapping and unmapping part is really the key difference, then ok.

Either way, from my testing, you seem to be right that the ATTR_WRITE_COMBINE doesn't seem to make any difference in behavior.

Quoted 5 lines

drivers/dma-buf/heaps/system_uncached_heap.c line 187

Quoted 3 lines
drivers/dma-buf/heaps/system_uncached_heap.c new file +3 -0
+     pgprot = pgprot_writecombine(PAGE_KERNEL);
+
+     for_each_sg(table->sgl, sg, table->nents, i) {

for_each_sg_page()

Ack.

Quoted 11 lines

drivers/dma-buf/heaps/system_uncached_heap.c line 347

Quoted 10 lines
drivers/dma-buf/heaps/system_uncached_heap.c new file +10 -0
+     /*
+      * XXX This is hackish. While the buffer will be uncached, we need
+      * to initially flush cpu cache, since the the __GFP_ZERO on the
+      * allocation means the zeroing was done by the cpu and thus it is likely
+      * cached. Map & flush it out now so we don't get corruption later on.
+      *
+      * Ideally we could do this without using the heap device as a dummy dev.
+      */
+     dma_map_sg_attrs(dma_heap_get_dev(heap), table->sgl, table->nents,
+                      DMA_BIDIRECTIONAL, DMA_ATTR_WRITE_COMBINE);
Quoted 6 lines

Again, DMA_ATTR_WRITE_COMBINE is meaningless here.

drivers/dma-buf/heaps/system_uncached_heap.c line 349

Quoted 2 lines
drivers/dma-buf/heaps/system_uncached_heap.c new file +2 -0
+     dma_sync_sg_for_device(dma_heap_get_dev(heap), table->sgl, table->nents,
+                            DMA_BIDIRECTIONAL);

This doesn't do anything that the map hasn't already just done.

Good point!

Quoted 6 lines

drivers/dma-buf/heaps/system_uncached_heap.c line 388

Quoted 3 lines
drivers/dma-buf/heaps/system_uncached_heap.c new file +3 -0
+     }
+     dma_heap_get_dev(heap->heap)->dma_mask = &dummy_mask;
+     dma_set_mask(dma_heap_get_dev(heap->heap), DMA_BIT_MASK(64));

Much as I'd hate to encourage using dma_coerce_mask_and_coherent(), I'm not sure this is really any better :/

Sounds fair.

Thanks so much for the review, I really appreciate the feedback! (Sorry I was a little slow to respond. The merge window has really been something this cycle.. :P)

I'll get this all integrated and resend the patch here shortly.

thanks
-john
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel