2 * Copyright (C) ROCKCHIP, Inc.
3 * Author:yzq<yzq@rock-chips.com>
5 * based on exynos_drm_gem.h
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 #ifndef _ROCKCHIP_DRM_GEM_H_
17 #define _ROCKCHIP_DRM_GEM_H_
19 #define to_rockchip_gem_obj(x) container_of(x,\
20 struct rockchip_drm_gem_obj, base)
22 #define IS_NONCONTIG_BUFFER(f) (f & ROCKCHIP_BO_NONCONTIG)
25 * rockchip drm gem buffer structure.
27 * @kvaddr: kernel virtual address to allocated memory region.
28 * *userptr: user space address.
29 * @dma_addr: bus address(accessed by dma) to allocated memory region.
30 * - this address could be physical address without IOMMU and
31 * device address with IOMMU.
32 * @write: whether pages will be written to by the caller.
33 * @pages: Array of backing pages.
34 * @sgt: sg table to transfer page data.
35 * @size: size of allocated memory region.
36 * @pfnmap: indicate whether memory region from userptr is mmaped with
39 struct rockchip_drm_gem_buf {
41 unsigned long userptr;
43 struct dma_attrs dma_attrs;
52 * rockchip drm buffer structure.
54 * @base: a gem object.
55 * - a new handle to this gem object would be created
56 * by drm_gem_handle_create().
57 * @buffer: a pointer to rockchip_drm_gem_buffer object.
58 * - contain the information to memory region allocated
59 * by user request or at framebuffer creation.
60 * continuous memory region allocated by user request
61 * or at framebuffer creation.
62 * @size: size requested from user, in bytes and this size is aligned
64 * @vma: a pointer to vm_area.
65 * @flags: indicate memory type to allocated buffer and cache attruibute.
67 * P.S. this object would be transfered to user as kms_bo.handle so
68 * user can access the buffer through kms_bo.handle.
70 struct rockchip_drm_gem_obj {
71 struct drm_gem_object base;
72 struct rockchip_drm_gem_buf *buffer;
74 struct vm_area_struct *vma;
78 struct page **rockchip_gem_get_pages(struct drm_gem_object *obj, gfp_t gfpmask);
80 /* destroy a buffer with gem object */
81 void rockchip_drm_gem_destroy(struct rockchip_drm_gem_obj *rockchip_gem_obj);
83 /* create a private gem object and initialize it. */
84 struct rockchip_drm_gem_obj *rockchip_drm_gem_init(struct drm_device *dev,
87 /* create a new buffer with gem object */
88 struct rockchip_drm_gem_obj *rockchip_drm_gem_create(struct drm_device *dev,
93 * request gem object creation and buffer allocation as the size
94 * that it is calculated with framebuffer information such as width,
97 int rockchip_drm_gem_create_ioctl(struct drm_device *dev, void *data,
98 struct drm_file *file_priv);
101 * get dma address from gem handle and this function could be used for
102 * other drivers such as 2d/3d acceleration drivers.
103 * with this function call, gem object reference count would be increased.
105 dma_addr_t *rockchip_drm_gem_get_dma_addr(struct drm_device *dev,
106 unsigned int gem_handle,
107 struct drm_file *filp);
110 * put dma address from gem handle and this function could be used for
111 * other drivers such as 2d/3d acceleration drivers.
112 * with this function call, gem object reference count would be decreased.
114 void rockchip_drm_gem_put_dma_addr(struct drm_device *dev,
115 unsigned int gem_handle,
116 struct drm_file *filp);
118 /* get buffer offset to map to user space. */
119 int rockchip_drm_gem_map_offset_ioctl(struct drm_device *dev, void *data,
120 struct drm_file *file_priv);
123 * mmap the physically continuous memory that a gem object contains
126 int rockchip_drm_gem_mmap_ioctl(struct drm_device *dev, void *data,
127 struct drm_file *file_priv);
129 /* map user space allocated by malloc to pages. */
130 int rockchip_drm_gem_userptr_ioctl(struct drm_device *dev, void *data,
131 struct drm_file *file_priv);
133 /* get buffer information to memory region allocated by gem. */
134 int rockchip_drm_gem_get_ioctl(struct drm_device *dev, void *data,
135 struct drm_file *file_priv);
137 /* get buffer size to gem handle. */
138 unsigned long rockchip_drm_gem_get_size(struct drm_device *dev,
139 unsigned int gem_handle,
140 struct drm_file *file_priv);
142 /* initialize gem object. */
143 int rockchip_drm_gem_init_object(struct drm_gem_object *obj);
145 /* free gem object. */
146 void rockchip_drm_gem_free_object(struct drm_gem_object *gem_obj);
148 /* create memory region for drm framebuffer. */
149 int rockchip_drm_gem_dumb_create(struct drm_file *file_priv,
150 struct drm_device *dev,
151 struct drm_mode_create_dumb *args);
153 /* map memory region for drm framebuffer to user space. */
154 int rockchip_drm_gem_dumb_map_offset(struct drm_file *file_priv,
155 struct drm_device *dev, uint32_t handle,
159 * destroy memory region allocated.
160 * - a gem handle and physical memory region pointed by a gem object
161 * would be released by drm_gem_handle_delete().
163 int rockchip_drm_gem_dumb_destroy(struct drm_file *file_priv,
164 struct drm_device *dev,
165 unsigned int handle);
167 /* page fault handler and mmap fault address(virtual) to physical memory. */
168 int rockchip_drm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf);
170 /* set vm_flags and we can change the vm attribute to other one at here. */
171 int rockchip_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
173 static inline int vma_is_io(struct vm_area_struct *vma)
175 return !!(vma->vm_flags & (VM_IO | VM_PFNMAP));
178 /* get a copy of a virtual memory region. */
179 struct vm_area_struct *rockchip_gem_get_vma(struct vm_area_struct *vma);
181 /* release a userspace virtual memory area. */
182 void rockchip_gem_put_vma(struct vm_area_struct *vma);
184 /* get pages from user space. */
185 int rockchip_gem_get_pages_from_userptr(unsigned long start,
188 struct vm_area_struct *vma);
190 /* drop the reference to pages. */
191 void rockchip_gem_put_pages_to_userptr(struct page **pages,
193 struct vm_area_struct *vma);
195 /* map sgt with dma region. */
196 int rockchip_gem_map_sgt_with_dma(struct drm_device *drm_dev,
197 struct sg_table *sgt,
198 enum dma_data_direction dir);
200 /* unmap sgt from dma region. */
201 void rockchip_gem_unmap_sgt_from_dma(struct drm_device *drm_dev,
202 struct sg_table *sgt,
203 enum dma_data_direction dir);