Skip to contentSkip to conversation

DRM: add drm gem CMA helper

PATCH Superseded Sascha Hauer 3 participants 10 messages May 29, 2012 – May 31, 2012

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Sascha Hauer

author maintainer
#0 lore raw

PATCH 1/1 DRM: add drm gem CMA helper

Many embedded drm devices do not have a IOMMU and no dedicated memory for graphics. These devices use CMA (Contiguous Memory Allocator) backed graphics memory. This patch provides helper functions to be able to share the code.

---

Lars-Peter, please let me know if this fits your needs or if you are missing something.

 drivers/gpu/drm/Kconfig              |    6 +
 drivers/gpu/drm/Makefile             |    1 +
 drivers/gpu/drm/drm_gem_cma_helper.c |  321 ++++++++++++++++++++++++++++++++++
 include/drm/drm_gem_cma_helper.h     |   47 +++++
 4 files changed, 375 insertions(+)
 create mode 100644 drivers/gpu/drm/drm_gem_cma_helper.c
 create mode 100644 include/drm/drm_gem_cma_helper.h
drivers/gpu/drm/Kconfig +6 -0
index e354bc0..f62717e 100644
@@ -53,6 +53,12 @@ config DRM_TTM
 	  GPU memory types. Will be enabled automatically if a device driver
 	  uses it.
 
+config DRM_GEM_CMA_HELPER
+	tristate
+	depends on DRM
+	help
+	  Choose this if you need the GEM cma helper functions
+
 config DRM_TDFX
 	tristate "3dfx Banshee/Voodoo3+"
 	depends on DRM && PCI
drivers/gpu/drm/Makefile +1 -0
index c20da5b..9a0d98a 100644
@@ -15,6 +15,7 @@ drm-y       :=	drm_auth.o drm_buffer.o drm_bufs.o drm_cache.o \
 		drm_trace_points.o drm_global.o drm_prime.o
 
 drm-$(CONFIG_COMPAT) += drm_ioc32.o
+drm-$(CONFIG_DRM_GEM_CMA_HELPER) += drm_gem_cma_helper.o
 
 drm-usb-y   := drm_usb.o
 
drivers/gpu/drm/drm_gem_cma_helper.c new file +321 -0

321 lines not on this page · raw message

include/drm/drm_gem_cma_helper.h new file +47 -0

47 lines not on this page · raw message

Signature · 1 line
1.7.10
#1 lore raw
Quoted 11 lines · Sascha Hauer

Many embedded drm devices do not have a IOMMU and no dedicated memory for graphics. These devices use CMA (Contiguous Memory Allocator) backed graphics memory. This patch provides helper functions to be able to share the code.

---

Lars-Peter, please let me know if this fits your needs or if you are missing something.

Awesome :) The overall structure looks basically like what I had, so I can just use these functions as drop-in replacement. I had some minor differences in the implementation though. Comments inline.

Quoted 13 lines
 drivers/gpu/drm/Kconfig              |    6 +
 drivers/gpu/drm/Makefile             |    1 +
 drivers/gpu/drm/drm_gem_cma_helper.c |  321 ++++++++++++++++++++++++++++++++++
 include/drm/drm_gem_cma_helper.h     |   47 +++++
 4 files changed, 375 insertions(+)
 create mode 100644 drivers/gpu/drm/drm_gem_cma_helper.c
 create mode 100644 include/drm/drm_gem_cma_helper.h
drivers/gpu/drm/Kconfig +0 -0
index e354bc0..f62717e 100644

drivers/gpu/drm/Kconfig config DRM_TTM · line 60

Quoted 8 lines
drivers/gpu/drm/Kconfig +5 -0
@@ -53,6 +53,12 @@ config DRM_TTM
 	  GPU memory types. Will be enabled automatically if a device driver
 	  uses it.
 
+config DRM_GEM_CMA_HELPER
+	tristate
+	depends on DRM
+	help
+	  Choose this if you need the GEM cma helper functions

This shouldn't have a help text as it should be selected by the driver and not by the user. Also the 'depends on DRM' can go away, since it becomes meaningless if the symbol is not user-selectable.

Quoted 34 lines
drivers/gpu/drm/Kconfig +1 -0
+
 config DRM_TDFX
 	tristate "3dfx Banshee/Voodoo3+"
 	depends on DRM && PCI
drivers/gpu/drm/Makefile +1 -0
index c20da5b..9a0d98a 100644
@@ -15,6 +15,7 @@ drm-y       :=	drm_auth.o drm_buffer.o drm_bufs.o drm_cache.o \
 		drm_trace_points.o drm_global.o drm_prime.o
 
 drm-$(CONFIG_COMPAT) += drm_ioc32.o
+drm-$(CONFIG_DRM_GEM_CMA_HELPER) += drm_gem_cma_helper.o
 
 drm-usb-y   := drm_usb.o
 
drivers/gpu/drm/drm_gem_cma_helper.c new file +11 -0
new file mode 100644
index 0000000..75534ff
@@ -0,0 +1,321 @@
[...]
+/*
+ * drm_gem_cma_create - allocate an object with the given size
+ *
+ * returns a struct drm_gem_cma_obj* on success or ERR_PTR values
+ * on failure.
+ */
+struct drm_gem_cma_obj *drm_gem_cma_create(struct drm_device *drm,
+		unsigned int size)
+{
+	struct drm_gem_cma_obj *cma_obj;
+	struct drm_gem_object *gem_obj;

drivers/gpu/drm/drm_gem_cma_helper.c line 78

Quoted 10 lines
drivers/gpu/drm/drm_gem_cma_helper.c new file +10 -0
+	int ret;
+
+	size = roundup(size, PAGE_SIZE);
+
+	cma_obj = kzalloc(sizeof(*cma_obj), GFP_KERNEL);
+	if (!cma_obj)
+		return ERR_PTR(-ENOMEM);
+
+	cma_obj->vaddr = dma_alloc_writecombine(drm->dev, size,
+			(dma_addr_t *)&cma_obj->paddr,

The cast shouldn't be necessary.

? [...]

Quoted 30 lines
drivers/gpu/drm/drm_gem_cma_helper.c new file +29 -0
+}
+EXPORT_SYMBOL_GPL(drm_gem_cma_create);
[...]
+
+static int drm_gem_cma_mmap_buffer(struct file *filp,
+		struct vm_area_struct *vma)
+{
+	struct drm_gem_object *gem_obj = filp->private_data;
+	struct drm_gem_cma_obj *cma_obj = to_dma_alloc_gem_obj(gem_obj);
+	unsigned long pfn, vm_size;
+
+	vma->vm_flags |= VM_IO | VM_RESERVED;
+
+	vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
+	vma->vm_file = filp;
+
+	vm_size = vma->vm_end - vma->vm_start;
+
+	/* check if user-requested size is valid. */
+	if (vm_size > gem_obj->size)
+		return -EINVAL;
+
+	/*
+	 * get page frame number to physical memory to be mapped
+	 * to user space.
+	 */
+	pfn = cma_obj->paddr >> PAGE_SHIFT;
+
+	if (remap_pfn_range(vma, vma->vm_start, pfn, vm_size,
+				vma->vm_page_prot)) {

drivers/gpu/drm/drm_gem_cma_helper.c line 187

Quoted 10 lines
drivers/gpu/drm/drm_gem_cma_helper.c new file +10 -0
+		dev_err(gem_obj->dev->dev, "failed to remap pfn range.\n");
+		return -EAGAIN;
+	}
+
+	return 0;
+}
+
+static const struct file_operations drm_gem_cma_fops = {
+	.mmap = drm_gem_cma_mmap_buffer,
+};

This and the function above seem to be unused. I think it's a relict from the old Exynos code.

Quoted 5 lines

[...]

drivers/gpu/drm/drm_gem_cma_helper.c new file +4 -0
+/*
+ * drm_gem_cma_dumb_create - (struct drm_driver)->dumb_create callback
+ * function
+ *

drivers/gpu/drm/drm_gem_cma_helper.c line 223

Quoted 10 lines
drivers/gpu/drm/drm_gem_cma_helper.c new file +10 -0
+ * This aligns the pitch and size arguments to the minimum required. wrap
+ * this into your own function if you need bigger alignment.
+ */
+int drm_gem_cma_dumb_create(struct drm_file *file_priv,
+		struct drm_device *dev, struct drm_mode_create_dumb *args)
+{
+	struct drm_gem_cma_obj *cma_obj;
+
+	if (args->pitch < args->width * args->bpp >> 3)
+		args->pitch = args->width * args->bpp >> 3;

I used DIV_ROUND_UP(args->bpp, 8) here. I think it makes more sense for the generic case.

Quoted 25 lines
drivers/gpu/drm/drm_gem_cma_helper.c new file +25 -0
+
+	if (args->size < args->pitch * args->height)
+		args->size = args->pitch * args->height;
+
+	cma_obj = drm_gem_cma_create_with_handle(file_priv, dev,
+			args->size, &args->handle);
+	if (IS_ERR(cma_obj))
+		return PTR_ERR(cma_obj);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create);
+
+/*
+ * drm_gem_cma_dumb_map_offset - (struct drm_driver)->dumb_map_offset callback
+ * function
+ */
+int drm_gem_cma_dumb_map_offset(struct drm_file *file_priv,
+		struct drm_device *drm, uint32_t handle, uint64_t *offset)
+{
+	struct drm_gem_cma_obj *cma_obj;
+	struct drm_gem_object *gem_obj;
+
+	mutex_lock(&drm->struct_mutex);
+

drivers/gpu/drm/drm_gem_cma_helper.c line 258

Quoted 10 lines
drivers/gpu/drm/drm_gem_cma_helper.c new file +10 -0
+	gem_obj = drm_gem_object_lookup(drm, file_priv, handle);
+	if (!gem_obj) {
+		dev_err(drm->dev, "failed to lookup gem object\n");
+		mutex_unlock(&drm->struct_mutex);
+		return -EINVAL;
+	}
+
+	cma_obj = to_dma_alloc_gem_obj(gem_obj);
+
+	*offset = get_gem_mmap_offset(&cma_obj->base);

Just get_gem_mmap_offset(gem_obj), also means the to_dma_alloc_gem_obj can go away.

Quoted 26 lines
drivers/gpu/drm/drm_gem_cma_helper.c new file +26 -0
+
+	drm_gem_object_unreference(gem_obj);
+
+	mutex_unlock(&drm->struct_mutex);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_map_offset);
+
+/*
+ * drm_gem_cma_fault - (struct vm_operations_struct)->fault callback function
+ */
+int drm_gem_cma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
+{
+	struct drm_gem_object *gem_obj = vma->vm_private_data;
+	struct drm_gem_cma_obj *cma_obj = to_dma_alloc_gem_obj(gem_obj);
+	struct drm_device *dev = gem_obj->dev;
+	unsigned long pfn;
+	pgoff_t page_offset;
+	int ret;
+
+	page_offset = ((unsigned long)vmf->virtual_address -
+			vma->vm_start) >> PAGE_SHIFT;
+
+	mutex_lock(&dev->struct_mutex);
+

drivers/gpu/drm/drm_gem_cma_helper.c line 294

Quoted 10 lines
drivers/gpu/drm/drm_gem_cma_helper.c new file +10 -0
+	pfn = (cma_obj->paddr >> PAGE_SHIFT) + page_offset;
+
+	ret = vm_insert_mixed(vma, (unsigned long)vmf->virtual_address, pfn);
+
+	mutex_unlock(&dev->struct_mutex);
+
+	return convert_to_vm_err_msg(ret);
+}
+EXPORT_SYMBOL_GPL(drm_gem_cma_fault);
+

Do you think it makes sense to have generic vm_operations struct as well, I think it will look the same for most drivers:

struct vm_operations_struct drm_gem_cma_vm_ops = {
.fault = drm_gem_cma_fault,
.open = drm_gem_vm_open,
.close = drm_gem_vm_close,
};

Quoted 7 lines
drivers/gpu/drm/drm_gem_cma_helper.c new file +7 -0
+/*
+ * drm_gem_cma_mmap - (struct file_operation)->mmap callback function
+ */
+int drm_gem_cma_mmap(struct file *filp, struct vm_area_struct *vma)
+{
+	int ret;
+

drivers/gpu/drm/drm_gem_cma_helper.c line 311

Quoted 10 lines
drivers/gpu/drm/drm_gem_cma_helper.c new file +10 -0
+	ret = drm_gem_mmap(filp, vma);
+	if (ret)
+		return ret;
+
+	vma->vm_flags &= ~VM_PFNMAP;
+	vma->vm_flags |= VM_MIXEDMAP;
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(drm_gem_cma_mmap);

This might be worth being made a more generic helper function, since we are not the only ones who want a mixed mapping. But that can wait for later.

Quoted 16 lines
drivers/gpu/drm/drm_gem_cma_helper.c new file +10 -0
+
+/*
+ * drm_gem_cma_dumb_destroy - (struct drm_driver)->dumb_destroy callback function
+ */
+int drm_gem_cma_dumb_destroy(struct drm_file *file_priv,
+		struct drm_device *drm, unsigned int handle)
+{
+	return drm_gem_handle_delete(file_priv, handle);
+}
+EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_destroy);
include/drm/drm_gem_cma_helper.h new file +0 -0
new file mode 100644
index 0000000..53b007c

include/drm/drm_gem_cma_helper.h line 7

Quoted 7 lines
include/drm/drm_gem_cma_helper.h new file +7 -0
@@ -0,0 +1,47 @@
+#ifndef __DRM_GEM_DMA_ALLOC_HELPER_H__
+#define __DRM_GEM_DMA_ALLOC_HELPER_H__
+
+struct drm_gem_cma_obj {
+	struct drm_gem_object base;
+	dma_addr_t paddr;
+	void __iomem *vaddr;

__iomem?

Quoted 30 lines
include/drm/drm_gem_cma_helper.h new file +30 -0
+};
+
+#define to_dma_alloc_gem_obj(x) \
+	container_of(x,	struct drm_gem_cma_obj, base)
+
+/* free gem object. */
+void drm_gem_cma_free_object(struct drm_gem_object *gem_obj);
+
+/* create memory region for drm framebuffer. */
+int drm_gem_cma_dumb_create(struct drm_file *file_priv,
+		struct drm_device *drm, struct drm_mode_create_dumb *args);
+
+/* map memory region for drm framebuffer to user space. */
+int drm_gem_cma_dumb_map_offset(struct drm_file *file_priv,
+		struct drm_device *drm, uint32_t handle, uint64_t *offset);
+
+/* page fault handler and mmap fault address(virtual) to physical memory. */
+int drm_gem_cma_fault(struct vm_area_struct *vma, struct vm_fault *vmf);
+
+/* set vm_flags and we can change the vm attribute to other one at here. */
+int drm_gem_cma_mmap(struct file *filp, struct vm_area_struct *vma);
+
+/*
+ * destroy memory region allocated.
+ *	- a gem handle and physical memory region pointed by a gem object
+ *	would be released by drm_gem_handle_delete().
+ */
+int drm_gem_cma_dumb_destroy(struct drm_file *file_priv,
+		struct drm_device *drm, unsigned int handle);
+

include/drm/drm_gem_cma_helper.h line 47

Quoted 10 lines
include/drm/drm_gem_cma_helper.h new file +10 -0
+/* allocate physical memory. */
+struct drm_gem_cma_obj *drm_gem_cma_create(struct drm_device *drm,
+		unsigned int size);
+
+struct drm_gem_cma_obj *drm_gem_cma_create_with_handle(
+		struct drm_file *file_priv,
+		struct drm_device *drm, unsigned int size,
+		unsigned int *handle);
+
+#endif

Thanks for taking care of this,
- Lars

#2 lore raw
Quoted 9 lines · Lars-Peter Clausen
Quoted 8 lines · Sascha Hauer

Many embedded drm devices do not have a IOMMU and no dedicated memory for graphics. These devices use CMA (Contiguous Memory Allocator) backed graphics memory. This patch provides helper functions to be able to share the code.

---

Quoted 7 lines
Quoted 3 lines

Lars-Peter, please let me know if this fits your needs or if you are missing something.

Awesome :) The overall structure looks basically like what I had, so I can just use these functions as drop-in replacement. I had some minor differences in the implementation though. Comments inline.

Did some initial testing and it seems to work fine :)

Quoted 17 lines
Quoted 17 lines
 drivers/gpu/drm/Kconfig              |    6 +
 drivers/gpu/drm/Makefile             |    1 +
 drivers/gpu/drm/drm_gem_cma_helper.c |  321 ++++++++++++++++++++++++++++++++++
 include/drm/drm_gem_cma_helper.h     |   47 +++++
 4 files changed, 375 insertions(+)
 create mode 100644 drivers/gpu/drm/drm_gem_cma_helper.c
 create mode 100644 include/drm/drm_gem_cma_helper.h
drivers/gpu/drm/Kconfig +0 -0
index e354bc0..f62717e 100644
@@ -53,6 +53,12 @@ config DRM_TTM
 	  GPU memory types. Will be enabled automatically if a device driver
 	  uses it.
 
Quoted 10 lines

drivers/gpu/drm/Kconfig config DRM_TTM · line 60

Quoted 5 lines
drivers/gpu/drm/Kconfig +5 -0
+config DRM_GEM_CMA_HELPER
+	tristate
+	depends on DRM
+	help
+	  Choose this if you need the GEM cma helper functions

This shouldn't have a help text as it should be selected by the driver and not by the user. Also the 'depends on DRM' can go away, since it becomes meaningless if the symbol is not user-selectable.

Sorry, ignore that. I though it would appear in menuconfig if it had an help text. But it needs to have a title. The "depends on" can still go away though.

Quoted 3 lines

drivers/gpu/drm/Kconfig config DRM_TTM · line 63

Quoted 3 lines
drivers/gpu/drm/Kconfig +3 -0
+
+#define to_dma_alloc_gem_obj(x) \
+	container_of(x,	struct drm_gem_cma_obj, base)

This looks like it has been missed during some renaming. It should probably be "to_drm_gem_cma_obj". And maybe make it an inline function.

Thanks,
- Lars

author maintainer
#3 lore raw
In reply to Lars-Peter Clausen

Hi Lars,

Thanks for your quick comments.

Quoted 10 lines · Lars-Peter Clausen
Quoted 9 lines · Sascha Hauer
drivers/gpu/drm/Kconfig +0 -0
index e354bc0..f62717e 100644
@@ -53,6 +53,12 @@ config DRM_TTM
 	  GPU memory types. Will be enabled automatically if a device driver
 	  uses it.
 
Quoted 9 lines

drivers/gpu/drm/Kconfig config DRM_TTM · line 60

Quoted 5 lines
drivers/gpu/drm/Kconfig +5 -0
+config DRM_GEM_CMA_HELPER
+	tristate
+	depends on DRM
+	help
+	  Choose this if you need the GEM cma helper functions

This shouldn't have a help text as it should be selected by the driver and not by the user. Also the 'depends on DRM' can go away, since it becomes meaningless if the symbol is not user-selectable.

The other helpers also have a depends on DRM in them. You are right, it's quite meaningless. The only advantage I can think of is that it would produce a 'has unmet direct dependencies' if someone outside DRM would select this. I'll drop it.

Quoted 7 lines

drivers/gpu/drm/Kconfig config DRM_TTM · line 64

Quoted 4 lines
drivers/gpu/drm/Kconfig +4 -0
+
+static const struct file_operations drm_gem_cma_fops = {
+	.mmap = drm_gem_cma_mmap_buffer,
+};

This and the function above seem to be unused. I think it's a relict from the old Exynos code.

Indeed. I wonder why my compiler hasn't warned me about this. Will remove.

Quoted 9 lines

Do you think it makes sense to have generic vm_operations struct as well, I think it will look the same for most drivers:

struct vm_operations_struct drm_gem_cma_vm_ops = {
.fault = drm_gem_cma_fault,
.open = drm_gem_vm_open,
.close = drm_gem_vm_close,
};

As we both can make use of this I'll add it. People who need something else can still add their own vm_operations_struct.

I integrated your other comments, will repost tomorrow unless there are more comments on this.

Sascha

Signature · 4 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#4 lore raw

PATCH 1/2 DRM: Add DRM kms/fb cma helper

In reply to Sascha Hauer

This patchset introduces a set of helper function for implementing the KMS framebuffer layer for drivers which use the drm gem CMA helper function.

---
 drivers/gpu/drm/Kconfig             |   11 ++
 drivers/gpu/drm/Makefile            |    1 +
 drivers/gpu/drm/drm_fb_cma_helper.c |  359 +++++++++++++++++++++++++++++++++++
 include/drm/drm_fb_cma_helper.h     |   24 +++
 4 files changed, 395 insertions(+)
 create mode 100644 drivers/gpu/drm/drm_fb_cma_helper.c
 create mode 100644 include/drm/drm_fb_cma_helper.h
drivers/gpu/drm/Kconfig +11 -0
index 6b6e519..0050caa 100644
@@ -41,6 +41,17 @@ config DRM_GEM_CMA_HELPER
 	help
 	  Choose this if you need the GEM cma helper functions
 
+config DRM_KMS_CMA_HELPER
+	tristate
+	select DRM_GEM_CMA_HELPER
+	select DRM_KMS_HELPER
+	select FB_SYS_FILLRECT
+	select FB_SYS_COPYAREA
+	select FB_SYS_IMAGEBLIT
+	help
+	  Choose this if you need the KMS cma helper functions
+
+
 config DRM_TDFX
 	tristate "3dfx Banshee/Voodoo3+"
 	depends on DRM && PCI
drivers/gpu/drm/Makefile +1 -0
index 2c882af..8b332fe 100644
@@ -18,6 +18,7 @@ drm-$(CONFIG_COMPAT) += drm_ioc32.o
 drm-$(CONFIG_DRM_GEM_CMA_HELPER) += drm_gem_cma_helper.o
 
 drm_kms_helper-y := drm_fb_helper.o drm_crtc_helper.o drm_dp_i2c_helper.o
+drm_kms_helper-$(CONFIG_DRM_KMS_CMA_HELPER) += drm_fb_cma_helper.o
 
 obj-$(CONFIG_DRM_KMS_HELPER) += drm_kms_helper.o
 
drivers/gpu/drm/drm_fb_cma_helper.c new file +359 -0

359 lines not on this page · raw message

include/drm/drm_fb_cma_helper.h new file +24 -0
new file mode 100644
index 0000000..dc90e26
@@ -0,0 +1,24 @@
+#ifndef __DRM_FB_CMA_HELPER_H__
+#define __DRM_FB_CMA_HELPER_H__
+
+struct drm_fbdev_cma;
+struct drm_gem_cma_obj;
+
+struct drm_framebuffer;
+struct drm_device;
+struct drm_file;
+struct drm_mode_fb_cmd2;
+
+struct drm_fbdev_cma *drm_fbdev_cma_init(struct drm_device *dev, unsigned int prefered_bpp,
+	unsigned int num_crtc, unsigned int max_conn_count);
+void drm_fbdev_cma_fini(struct drm_fbdev_cma *fbdev_cma);
+void drm_fbdev_cma_restore_mode(struct drm_fbdev_cma *fbdev_cma);
+void drm_fbdev_cma_hotplug_event(struct drm_fbdev_cma *fbdev_cma);
+
+struct drm_gem_cma_obj *drm_fb_cma_get_gem_obj(struct drm_framebuffer *fb);
+
+struct drm_framebuffer *drm_fb_cma_create(struct drm_device *dev,
+	struct drm_file *file_priv, struct drm_mode_fb_cmd2 *mode_cmd);
+
+#endif
+
Signature · 1 line
1.7.10
#5 lore raw

PATCH 2/2 DRM: imx: Use new fb cma helper functions

---
Sascha, this is against your inital patch and I suppose it does not apply cleanly anymore to your current tree, but it shouldn't be to hard to make the changes manually. Can you test whether the generic cma fb helper functions works for you?

---
 drivers/gpu/drm/imx/Kconfig         |    1 +
 drivers/gpu/drm/imx/Makefile        |    2 +-
 drivers/gpu/drm/imx/imx-drm-core.c  |   13 +-
 drivers/gpu/drm/imx/imx-fb.c        |  137 +--------------------
 drivers/gpu/drm/imx/imx-fbdev.c     |  228 ++---------------------------------
 drivers/gpu/drm/imx/imx-lcdc-crtc.c |   10 +-
 6 files changed, 24 insertions(+), 367 deletions(-)
drivers/gpu/drm/imx/Kconfig +1 -0
index 5fc3a44..6b5e3ef 100644
@@ -5,6 +5,7 @@ config DRM_IMX
 
 config DRM_IMX_FB_HELPER
 	tristate "provide legacy framebuffer /dev/fb0"
+	select DRM_KMS_CMA_HELPER
 	depends on DRM_IMX
 
 config DRM_IMX_LCDC
drivers/gpu/drm/imx/Makefile +1 -1
index 0f7c038..6c7dd2d 100644
@@ -1,5 +1,5 @@
 
-imxdrm-objs := imx-drm-core.o imx-fb.o imx-gem.o
+imxdrm-objs := imx-drm-core.o imx-fb.o
 
 obj-$(CONFIG_DRM_IMX) += imxdrm.o
 
drivers/gpu/drm/imx/imx-drm-core.c +7 -6
index 29f5f10..68cf4da 100644
@@ -22,6 +22,7 @@
 #include <linux/fb.h>
 #include <asm/fb.h>
 #include <linux/module.h>
+#include <drm/drm_gem_cma_helper.h>
 
 #include "imx-drm.h"
 
@@ -149,7 +150,7 @@ static void imx_drm_disable_vblank(struct drm_device *drm, int crtc)
 }
 
 static struct vm_operations_struct imx_drm_gem_vm_ops = {
-	.fault = imx_drm_gem_fault,
+	.fault = drm_gem_cma_fault,
 	.open = drm_gem_vm_open,
 	.close = drm_gem_vm_close,
 };
@@ -159,7 +160,7 @@ static const struct file_operations imx_drm_driver_fops = {
 	.open = drm_open,
 	.release = drm_release,
 	.unlocked_ioctl = drm_ioctl,
-	.mmap = imx_drm_gem_mmap,
+	.mmap = drm_gem_cma_mmap,
 	.poll = drm_poll,
 	.fasync = drm_fasync,
 	.read = drm_read,
@@ -646,11 +647,11 @@ static struct drm_driver imx_drm_driver = {
 	.unload			= imx_drm_driver_unload,
 	.firstopen		= imx_drm_driver_firstopen,
 	.lastclose		= imx_drm_driver_lastclose,
-	.gem_free_object	= imx_drm_gem_free_object,
+	.gem_free_object	= drm_gem_cma_free_object,
 	.gem_vm_ops		= &imx_drm_gem_vm_ops,
-	.dumb_create		= imx_drm_gem_dumb_create,
-	.dumb_map_offset	= imx_drm_gem_dumb_map_offset,
-	.dumb_destroy		= imx_drm_gem_dumb_destroy,
+	.dumb_create		= drm_gem_cma_dumb_create,
+	.dumb_map_offset	= drm_gem_cma_dumb_map_offset,
+	.dumb_destroy		= drm_gem_cma_dumb_destroy,
 
 	.get_vblank_counter	= drm_vblank_count,
 	.enable_vblank		= imx_drm_enable_vblank,
drivers/gpu/drm/imx/imx-fb.c +2 -135

147 lines not on this page · raw message

drivers/gpu/drm/imx/imx-fbdev.c +7 -221

253 lines not on this page · raw message

drivers/gpu/drm/imx/imx-lcdc-crtc.c +6 -4
index e77c015..eb066df 100644
@@ -18,6 +18,8 @@
 #include <drm/drmP.h>
 #include <drm/drm_fb_helper.h>
 #include <drm/drm_crtc_helper.h>
+#include <drm/drm_gem_cma_helper.h>
+#include <drm/drm_fb_cma_helper.h>
 #include <linux/fb.h>
 #include <linux/clk.h>
 #include <asm/fb.h>
@@ -192,15 +194,15 @@ static int imx_drm_crtc_set(struct drm_crtc *crtc,
 static int imx_drm_set_base(struct drm_crtc *crtc, int x, int y)
 {
 	struct imx_crtc *imx_crtc = to_imx_crtc(crtc);
-	struct imx_drm_buf_entry *entry;
+	struct drm_gem_cma_obj *obj;
 	struct drm_framebuffer *fb = crtc->fb;
 	unsigned long phys;
 
-	entry = imx_drm_fb_get_buf(fb);
-	if (!entry)
+	obj = drm_fb_cma_get_gem_obj(fb);
+	if (!obj)
 		return -EFAULT;
 
-	phys = entry->paddr;
+	phys = obj->paddr;
 	phys += x * (fb->bits_per_pixel >> 3);
 	phys += y * fb->pitches[0];
 
Signature · 1 line
1.7.10
author maintainer
#6 lore raw

Re: [PATCH 1/2] DRM: Add DRM kms/fb cma helper

In reply to Lars-Peter Clausen
Quoted 2 lines · Lars-Peter Clausen

This patchset introduces a set of helper function for implementing the KMS framebuffer layer for drivers which use the drm gem CMA helper function.

I just integrated this into my series. Works great, thanks. Would be great to have this mainline.

Only some nitpicking and one missing kfree below.

Sascha

Quoted 28 lines
drivers/gpu/drm/drm_fb_cma_helper.c new file +28 -0
+static void drm_fb_cma_destroy(struct drm_framebuffer *fb)
+{
+	struct drm_fb_cma *fb_cma = to_fb_cma(fb);
+
+	if (fb_cma->obj)
+		drm_gem_object_unreference_unlocked(&fb_cma->obj->base);
+
+	drm_framebuffer_cleanup(fb);
+	kfree(fb_cma);
+}
+
+static int drm_fb_cma_create_handle(struct drm_framebuffer *fb,
+	struct drm_file *file_priv, unsigned int *handle)
+{
+	struct drm_fb_cma *fb_cma = to_fb_cma(fb);
+
+	return drm_gem_handle_create(file_priv,
+			&fb_cma->obj->base, handle);
+}
+
+static struct drm_framebuffer_funcs drm_fb_cma_funcs = {
+	.destroy	= drm_fb_cma_destroy,
+	.create_handle	= drm_fb_cma_create_handle,
+};
+
+static struct drm_fb_cma *drm_fb_cma_alloc(struct drm_device *dev,
+	struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_cma_obj *obj)
+{

drivers/gpu/drm/drm_fb_cma_helper.c line 85

Quoted 10 lines
drivers/gpu/drm/drm_fb_cma_helper.c new file +10 -0
+	struct drm_fb_cma *fb_cma;
+	int ret;
+
+	fb_cma = kzalloc(sizeof(*fb_cma), GFP_KERNEL);
+	if (!fb_cma)
+		return ERR_PTR(-ENOMEM);
+
+	ret = drm_framebuffer_init(dev, &fb_cma->fb, &drm_fb_cma_funcs);
+	if (ret) {
+		dev_err(dev->dev, "Failed to initalize framebuffer: %d\n", ret);

s/initalize/initialize/

kfree(fb_cma)?

drivers/gpu/drm/drm_fb_cma_helper.c line 251

Quoted 6 lines
drivers/gpu/drm/drm_fb_cma_helper.c new file +6 -0
+		return ERR_PTR(ret);
+	}
+
+
+/**
+ * drm_fbdev_cma_init() - Allocate and initlaize a drm_fbdev_cma struct

s/initlaize/initialize/

Quoted 2 lines

+ * @dev: DRM device
+ * @prefered_bpp: Prefered bits per pixel for the device

s/prefered/preferred/

Quoted 5 lines

+ * @num_crtc: Number of CRTCs
+ * @max_conn_count: Maximum number of connectors
+ *
+ * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR.
+ */

Signature · 4 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#7 lore raw
In reply to Sascha Hauer

Hi Sascha,

Thank you for the patch. I've successfully tested the helper with the new SH Mobile DRM driver. Just a couple of comments below in addition to Lars' comments (this is not a full review, just details that caught my attention when comparing the code with my implementation, and trying to use it).

Quoted 18 lines · On Tuesday 29 May 2012 16:10:27 Sascha Hauer wrote:

Many embedded drm devices do not have a IOMMU and no dedicated memory for graphics. These devices use CMA (Contiguous Memory Allocator) backed graphics memory. This patch provides helper functions to be able to share the code.

---

Lars-Peter, please let me know if this fits your needs or if you are missing something.

 drivers/gpu/drm/Kconfig              |    6 +
 drivers/gpu/drm/Makefile             |    1 +
 drivers/gpu/drm/drm_gem_cma_helper.c |  321 +++++++++++++++++++++++++++++++
 include/drm/drm_gem_cma_helper.h     |   47 +++++
 4 files changed, 375 insertions(+)
 create mode 100644 drivers/gpu/drm/drm_gem_cma_helper.c
 create mode 100644 include/drm/drm_gem_cma_helper.h

[snip]

Quoted 6 lines
(quoted diff) +0 -0

b/drivers/gpu/drm/drm_gem_cma_helper.c new file mode 100644 index 0000000..75534ff

drivers/gpu/drm/drm_gem_cma_helper.c new file +0 -0
@@ -0,0 +1,321 @@

[snip]

Quoted 4 lines
drivers/gpu/drm/drm_gem_cma_helper.c new file +4 -0
+/*
+ * drm_gem_cma_create - allocate an object with the given size
+ *
+ * returns a struct drm_gem_cma_obj* on success or ERR_PTR values

drivers/gpu/drm/drm_gem_cma_helper.c line 71

Quoted 10 lines
drivers/gpu/drm/drm_gem_cma_helper.c new file +10 -0
+ * on failure.
+ */
+struct drm_gem_cma_obj *drm_gem_cma_create(struct drm_device *drm,
+		unsigned int size)
+{
+	struct drm_gem_cma_obj *cma_obj;
+	struct drm_gem_object *gem_obj;
+	int ret;
+
+	size = roundup(size, PAGE_SIZE);

As PAGE_SIZE is guaranteed to be a power of two, you can use round_up, which should be faster.

Quoted 38 lines
drivers/gpu/drm/drm_gem_cma_helper.c new file +38 -0
+
+	cma_obj = kzalloc(sizeof(*cma_obj), GFP_KERNEL);
+	if (!cma_obj)
+		return ERR_PTR(-ENOMEM);
+
+	cma_obj->vaddr = dma_alloc_writecombine(drm->dev, size,
+			(dma_addr_t *)&cma_obj->paddr,
+			GFP_KERNEL | __GFP_NOWARN);
+	if (!cma_obj->vaddr) {
+		dev_err(drm->dev, "failed to allocate buffer with size %d\n", size);
+		ret = -ENOMEM;
+		goto err_dma_alloc;
+	}
+
+	gem_obj = &cma_obj->base;
+
+	ret = drm_gem_object_init(drm, gem_obj, size);
+	if (ret)
+		goto err_obj_init;
+
+	ret = drm_gem_create_mmap_offset(gem_obj);
+	if (ret)
+		goto err_create_mmap_offset;
+
+	return cma_obj;
+
+err_create_mmap_offset:
+	drm_gem_object_release(gem_obj);
+
+err_obj_init:
+	drm_gem_cma_buf_destroy(drm, cma_obj);
+
+err_dma_alloc:
+	kfree(cma_obj);
+
+	return ERR_PTR(ret);
+}
+EXPORT_SYMBOL_GPL(drm_gem_cma_create);

[snip]

drivers/gpu/drm/drm_gem_cma_helper.c line 307

Quoted 13 lines
drivers/gpu/drm/drm_gem_cma_helper.c new file +13 -0
+/*
+ * drm_gem_cma_mmap - (struct file_operation)->mmap callback function
+ */
+int drm_gem_cma_mmap(struct file *filp, struct vm_area_struct *vma)
+{
+	int ret;
+
+	ret = drm_gem_mmap(filp, vma);
+	if (ret)
+		return ret;
+
+	vma->vm_flags &= ~VM_PFNMAP;
+	vma->vm_flags |= VM_MIXEDMAP;

Why is this a mixed map ?

drivers/gpu/drm/drm_gem_cma_helper.c line 311

Quoted 4 lines
drivers/gpu/drm/drm_gem_cma_helper.c new file +4 -0
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(drm_gem_cma_mmap);

My implementation maps the whole buffer in one go at mmap time, not page by page at page fault time. Isn't that more efficient when mapping frame buffer memory ?

[snip]

include/drm/drm_gem_cma_helper.h line 2

Quoted 8 lines
(quoted diff) +0 -0

b/include/drm/drm_gem_cma_helper.h new file mode 100644 index 0000000..53b007c

include/drm/drm_gem_cma_helper.h new file +2 -0
@@ -0,0 +1,47 @@
+#ifndef __DRM_GEM_DMA_ALLOC_HELPER_H__
+#define __DRM_GEM_DMA_ALLOC_HELPER_H__

Should this be __DRM_GEM_CMA_HELPER_H__ ?

include/drm/drm_gem_cma_helper.h line 8

Quoted 6 lines
include/drm/drm_gem_cma_helper.h new file +6 -0
+
+struct drm_gem_cma_obj {
+	struct drm_gem_object base;
+	dma_addr_t paddr;
+	void __iomem *vaddr;
+};

All drm objects end with _object, would it be better to rename this to struct drm_gem_cma_object ?

Signature · 3 lines
Regards,

Laurent Pinchart
author maintainer
#8 lore raw
Quoted 7 lines · Laurent Pinchart

Hi Sascha,

Thank you for the patch. I've successfully tested the helper with the new SH Mobile DRM driver. Just a couple of comments below in addition to Lars' comments (this is not a full review, just details that caught my attention when comparing the code with my implementation, and trying to use it).

Quoted 15 lines

drivers/gpu/drm/drm_gem_cma_helper.c line 307

Quoted 13 lines
drivers/gpu/drm/drm_gem_cma_helper.c new file +13 -0
+/*
+ * drm_gem_cma_mmap - (struct file_operation)->mmap callback function
+ */
+int drm_gem_cma_mmap(struct file *filp, struct vm_area_struct *vma)
+{
+	int ret;
+
+	ret = drm_gem_mmap(filp, vma);
+	if (ret)
+		return ret;
+
+	vma->vm_flags &= ~VM_PFNMAP;
+	vma->vm_flags |= VM_MIXEDMAP;

Why is this a mixed map ?

Honestly, I don't know. This is copied from the exynos driver. I don't know much about these flags, so if you think something else is better here than you're probably right ;)

Quoted 9 lines

drivers/gpu/drm/drm_gem_cma_helper.c line 311

Quoted 5 lines
drivers/gpu/drm/drm_gem_cma_helper.c new file +4 -0
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(drm_gem_cma_mmap);

My implementation maps the whole buffer in one go at mmap time, not page by page at page fault time. Isn't that more efficient when mapping frame buffer memory ?

I remember Alan has mentioned this in an earlier review. I'll update the patch accordingly.

I will fix this and the other things you mentioned and repost.

Thanks
Sascha

Signature · 4 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#9 lore raw

Hi Sascha,

Quoted 24 lines · On Wednesday 30 May 2012 18:28:12 Sascha Hauer wrote:
Quoted 7 lines · Laurent Pinchart

Hi Sascha,

Thank you for the patch. I've successfully tested the helper with the new SH Mobile DRM driver. Just a couple of comments below in addition to Lars' comments (this is not a full review, just details that caught my attention when comparing the code with my implementation, and trying to use it).

Quoted 15 lines

drivers/gpu/drm/drm_gem_cma_helper.c line 307

Quoted 13 lines
drivers/gpu/drm/drm_gem_cma_helper.c new file +13 -0
+/*
+ * drm_gem_cma_mmap - (struct file_operation)->mmap callback function
+ */
+int drm_gem_cma_mmap(struct file *filp, struct vm_area_struct *vma)
+{
+	int ret;
+
+	ret = drm_gem_mmap(filp, vma);
+	if (ret)
+		return ret;
+
+	vma->vm_flags &= ~VM_PFNMAP;
+	vma->vm_flags |= VM_MIXEDMAP;

Why is this a mixed map ?

Quoted 3 lines

Honestly, I don't know. This is copied from the exynos driver. I don't know much about these flags, so if you think something else is better here than you're probably right ;)

I wouldn't claim to be an expert on this matter :-) As far as I know, PFNMAP means that the mapping refers to physical memory with no struct page associated with it (that could be I/O memory, DRAM reserved at boot time, ... basically any memory outside of the system memory resources controlled by the kernel page allocator). MIXEDMAP means that the mapping refers to memory that contains both PFNMAP regions and non-PFNMAP regions. I would be surprised if dma_alloc_writecombine() returned such a mix. On the other hand the memory it returns might be PFNMAP or non-PFNMAP depending on the underneath allocator, maybe that's why VM_MIXEDMAP was used.

I'd appreciate an expert's opinion on this :-)

Quoted 13 lines
Quoted 8 lines

drivers/gpu/drm/drm_gem_cma_helper.c line 311

Quoted 4 lines
drivers/gpu/drm/drm_gem_cma_helper.c new file +4 -0
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(drm_gem_cma_mmap);

My implementation maps the whole buffer in one go at mmap time, not page by page at page fault time. Isn't that more efficient when mapping frame buffer memory ?

I remember Alan has mentioned this in an earlier review. I'll update the patch accordingly.

I will fix this and the other things you mentioned and repost.

Signature · 3 lines
Regards,

Laurent Pinchart