blob: 19777145cf8ede8ff9a0272513f82fc971149345 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Sascha Hauerb9d47452012-06-27 15:30:18 +02002#ifndef __DRM_GEM_CMA_HELPER_H__
3#define __DRM_GEM_CMA_HELPER_H__
4
Shawn Guo740c22a2014-03-24 20:14:48 +08005#include <drm/drmP.h>
Daniel Vetterd9fc9412014-09-23 15:46:53 +02006#include <drm/drm_gem.h>
Shawn Guo740c22a2014-03-24 20:14:48 +08007
Thierry Redingd7883f82014-11-03 13:56:55 +01008/**
9 * struct drm_gem_cma_object - GEM object backed by CMA memory allocations
10 * @base: base GEM object
11 * @paddr: physical address of the backing memory
Liviu Dudau998fb1a2017-11-10 13:33:10 +000012 * @sgt: scatter/gather table for imported PRIME buffers. The table can have
13 * more than one entry but they are guaranteed to have contiguous
14 * DMA addresses.
Thierry Redingd7883f82014-11-03 13:56:55 +010015 * @vaddr: kernel virtual address of the backing memory
16 */
Sascha Hauerb9d47452012-06-27 15:30:18 +020017struct drm_gem_cma_object {
18 struct drm_gem_object base;
19 dma_addr_t paddr;
Laurent Pinchart71d72822013-02-17 01:57:30 +010020 struct sg_table *sgt;
21
22 /* For objects with DMA memory allocated by GEM CMA */
Sascha Hauerb9d47452012-06-27 15:30:18 +020023 void *vaddr;
24};
25
Noralf Trønnes8d25cce2017-11-07 20:13:42 +010026#define to_drm_gem_cma_obj(gem_obj) \
27 container_of(gem_obj, struct drm_gem_cma_object, base)
Sascha Hauerb9d47452012-06-27 15:30:18 +020028
Eric Anholt75cccac2017-04-17 16:31:24 -070029#ifndef CONFIG_MMU
30#define DRM_GEM_CMA_UNMAPPED_AREA_FOPS \
31 .get_unmapped_area = drm_gem_cma_get_unmapped_area,
32#else
33#define DRM_GEM_CMA_UNMAPPED_AREA_FOPS
34#endif
35
Daniel Vetterd55f7e52017-03-08 15:12:56 +010036/**
37 * DEFINE_DRM_GEM_CMA_FOPS() - macro to generate file operations for CMA drivers
38 * @name: name for the generated structure
39 *
40 * This macro autogenerates a suitable &struct file_operations for CMA based
41 * drivers, which can be assigned to &drm_driver.fops. Note that this structure
42 * cannot be shared between drivers, because it contains a reference to the
43 * current module using THIS_MODULE.
44 *
45 * Note that the declaration is already marked as static - if you need a
46 * non-static version of this you're probably doing it wrong and will break the
47 * THIS_MODULE reference by accident.
48 */
49#define DEFINE_DRM_GEM_CMA_FOPS(name) \
50 static const struct file_operations name = {\
51 .owner = THIS_MODULE,\
52 .open = drm_open,\
53 .release = drm_release,\
54 .unlocked_ioctl = drm_ioctl,\
55 .compat_ioctl = drm_compat_ioctl,\
56 .poll = drm_poll,\
57 .read = drm_read,\
58 .llseek = noop_llseek,\
59 .mmap = drm_gem_cma_mmap,\
Eric Anholt75cccac2017-04-17 16:31:24 -070060 DRM_GEM_CMA_UNMAPPED_AREA_FOPS \
Daniel Vetterd55f7e52017-03-08 15:12:56 +010061 }
62
Thierry Redingd7883f82014-11-03 13:56:55 +010063/* free GEM object */
Sascha Hauerb9d47452012-06-27 15:30:18 +020064void drm_gem_cma_free_object(struct drm_gem_object *gem_obj);
65
Thierry Redingd7883f82014-11-03 13:56:55 +010066/* create memory region for DRM framebuffer */
Thierry Reding6d178292014-11-03 11:48:49 +010067int drm_gem_cma_dumb_create_internal(struct drm_file *file_priv,
68 struct drm_device *drm,
69 struct drm_mode_create_dumb *args);
70
71/* create memory region for DRM framebuffer */
Sascha Hauerb9d47452012-06-27 15:30:18 +020072int drm_gem_cma_dumb_create(struct drm_file *file_priv,
Thierry Redingd7883f82014-11-03 13:56:55 +010073 struct drm_device *drm,
74 struct drm_mode_create_dumb *args);
Sascha Hauerb9d47452012-06-27 15:30:18 +020075
Thierry Redingd7883f82014-11-03 13:56:55 +010076/* set vm_flags and we can change the VM attribute to other one at here */
Sascha Hauerb9d47452012-06-27 15:30:18 +020077int drm_gem_cma_mmap(struct file *filp, struct vm_area_struct *vma);
78
Thierry Redingd7883f82014-11-03 13:56:55 +010079/* allocate physical memory */
Sascha Hauerb9d47452012-06-27 15:30:18 +020080struct drm_gem_cma_object *drm_gem_cma_create(struct drm_device *drm,
Thierry Redingd7883f82014-11-03 13:56:55 +010081 size_t size);
Sascha Hauerb9d47452012-06-27 15:30:18 +020082
83extern const struct vm_operations_struct drm_gem_cma_vm_ops;
84
Benjamin Gaignard62a0d982017-01-04 10:12:57 +010085#ifndef CONFIG_MMU
86unsigned long drm_gem_cma_get_unmapped_area(struct file *filp,
87 unsigned long addr,
88 unsigned long len,
89 unsigned long pgoff,
90 unsigned long flags);
Benjamin Gaignard62a0d982017-01-04 10:12:57 +010091#endif
92
Noralf Trønnesd6892012017-11-07 20:13:43 +010093void drm_gem_cma_print_info(struct drm_printer *p, unsigned int indent,
94 const struct drm_gem_object *obj);
Rob Clark6f646092012-12-10 10:46:43 -060095
Joonyoung Shim78467dc2013-06-28 14:24:54 +090096struct sg_table *drm_gem_cma_prime_get_sg_table(struct drm_gem_object *obj);
97struct drm_gem_object *
Maarten Lankhorstb5e9c1a2014-01-09 11:03:14 +010098drm_gem_cma_prime_import_sg_table(struct drm_device *dev,
99 struct dma_buf_attachment *attach,
Joonyoung Shim78467dc2013-06-28 14:24:54 +0900100 struct sg_table *sgt);
101int drm_gem_cma_prime_mmap(struct drm_gem_object *obj,
102 struct vm_area_struct *vma);
103void *drm_gem_cma_prime_vmap(struct drm_gem_object *obj);
104void drm_gem_cma_prime_vunmap(struct drm_gem_object *obj, void *vaddr);
105
Sascha Hauerb9d47452012-06-27 15:30:18 +0200106#endif /* __DRM_GEM_CMA_HELPER_H__ */