2 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
3 * Author:Mark Yao <mark.yao@rock-chips.com>
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
17 #include <drm/drm_gem.h>
18 #include <drm/drm_vma_manager.h>
20 #include "rockchip_drm_drv.h"
21 #include "rockchip_drm_gem.h"
23 static int rockchip_gem_alloc_buf(struct rockchip_gem_object
*rk_obj
,
26 struct drm_gem_object
*obj
= &rk_obj
->base
;
27 struct drm_device
*drm
= obj
->dev
;
29 rk_obj
->dma_attrs
= DMA_ATTR_WRITE_COMBINE
;
32 rk_obj
->dma_attrs
|= DMA_ATTR_NO_KERNEL_MAPPING
;
34 rk_obj
->kvaddr
= dma_alloc_attrs(drm
->dev
, obj
->size
,
35 &rk_obj
->dma_addr
, GFP_KERNEL
,
37 if (!rk_obj
->kvaddr
) {
38 DRM_ERROR("failed to allocate %zu byte dma buffer", obj
->size
);
45 static void rockchip_gem_free_buf(struct rockchip_gem_object
*rk_obj
)
47 struct drm_gem_object
*obj
= &rk_obj
->base
;
48 struct drm_device
*drm
= obj
->dev
;
50 dma_free_attrs(drm
->dev
, obj
->size
, rk_obj
->kvaddr
, rk_obj
->dma_addr
,
54 static int rockchip_drm_gem_object_mmap(struct drm_gem_object
*obj
,
55 struct vm_area_struct
*vma
)
59 struct rockchip_gem_object
*rk_obj
= to_rockchip_obj(obj
);
60 struct drm_device
*drm
= obj
->dev
;
63 * dma_alloc_attrs() allocated a struct page table for rk_obj, so clear
64 * VM_PFNMAP flag that was set by drm_gem_mmap_obj()/drm_gem_mmap().
66 vma
->vm_flags
&= ~VM_PFNMAP
;
69 ret
= dma_mmap_attrs(drm
->dev
, vma
, rk_obj
->kvaddr
, rk_obj
->dma_addr
,
70 obj
->size
, rk_obj
->dma_attrs
);
72 drm_gem_vm_close(vma
);
77 int rockchip_gem_mmap_buf(struct drm_gem_object
*obj
,
78 struct vm_area_struct
*vma
)
82 ret
= drm_gem_mmap_obj(obj
, obj
->size
, vma
);
86 return rockchip_drm_gem_object_mmap(obj
, vma
);
89 /* drm driver mmap file operations */
90 int rockchip_gem_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
92 struct drm_gem_object
*obj
;
95 ret
= drm_gem_mmap(filp
, vma
);
99 obj
= vma
->vm_private_data
;
101 return rockchip_drm_gem_object_mmap(obj
, vma
);
104 struct rockchip_gem_object
*
105 rockchip_gem_create_object(struct drm_device
*drm
, unsigned int size
,
108 struct rockchip_gem_object
*rk_obj
;
109 struct drm_gem_object
*obj
;
112 size
= round_up(size
, PAGE_SIZE
);
114 rk_obj
= kzalloc(sizeof(*rk_obj
), GFP_KERNEL
);
116 return ERR_PTR(-ENOMEM
);
120 drm_gem_private_object_init(drm
, obj
, size
);
122 ret
= rockchip_gem_alloc_buf(rk_obj
, alloc_kmap
);
124 goto err_free_rk_obj
;
134 * rockchip_gem_free_object - (struct drm_driver)->gem_free_object callback
137 void rockchip_gem_free_object(struct drm_gem_object
*obj
)
139 struct rockchip_gem_object
*rk_obj
;
141 drm_gem_free_mmap_offset(obj
);
143 rk_obj
= to_rockchip_obj(obj
);
145 rockchip_gem_free_buf(rk_obj
);
151 * rockchip_gem_create_with_handle - allocate an object with the given
152 * size and create a gem handle on it
154 * returns a struct rockchip_gem_object* on success or ERR_PTR values
157 static struct rockchip_gem_object
*
158 rockchip_gem_create_with_handle(struct drm_file
*file_priv
,
159 struct drm_device
*drm
, unsigned int size
,
160 unsigned int *handle
)
162 struct rockchip_gem_object
*rk_obj
;
163 struct drm_gem_object
*obj
;
166 rk_obj
= rockchip_gem_create_object(drm
, size
, false);
168 return ERR_CAST(rk_obj
);
173 * allocate a id of idr table where the obj is registered
174 * and handle has the id what user can see.
176 ret
= drm_gem_handle_create(file_priv
, obj
, handle
);
178 goto err_handle_create
;
180 /* drop reference from allocate - handle holds it now. */
181 drm_gem_object_unreference_unlocked(obj
);
186 rockchip_gem_free_object(obj
);
191 int rockchip_gem_dumb_map_offset(struct drm_file
*file_priv
,
192 struct drm_device
*dev
, uint32_t handle
,
195 struct drm_gem_object
*obj
;
198 obj
= drm_gem_object_lookup(file_priv
, handle
);
200 DRM_ERROR("failed to lookup gem object.\n");
204 ret
= drm_gem_create_mmap_offset(obj
);
208 *offset
= drm_vma_node_offset_addr(&obj
->vma_node
);
209 DRM_DEBUG_KMS("offset = 0x%llx\n", *offset
);
212 drm_gem_object_unreference_unlocked(obj
);
218 * rockchip_gem_dumb_create - (struct drm_driver)->dumb_create callback
221 * This aligns the pitch and size arguments to the minimum required. wrap
222 * this into your own function if you need bigger alignment.
224 int rockchip_gem_dumb_create(struct drm_file
*file_priv
,
225 struct drm_device
*dev
,
226 struct drm_mode_create_dumb
*args
)
228 struct rockchip_gem_object
*rk_obj
;
229 int min_pitch
= DIV_ROUND_UP(args
->width
* args
->bpp
, 8);
232 * align to 64 bytes since Mali requires it.
234 args
->pitch
= ALIGN(min_pitch
, 64);
235 args
->size
= args
->pitch
* args
->height
;
237 rk_obj
= rockchip_gem_create_with_handle(file_priv
, dev
, args
->size
,
240 return PTR_ERR_OR_ZERO(rk_obj
);
244 * Allocate a sg_table for this GEM object.
245 * Note: Both the table's contents, and the sg_table itself must be freed by
247 * Returns a pointer to the newly allocated sg_table, or an ERR_PTR() error.
249 struct sg_table
*rockchip_gem_prime_get_sg_table(struct drm_gem_object
*obj
)
251 struct rockchip_gem_object
*rk_obj
= to_rockchip_obj(obj
);
252 struct drm_device
*drm
= obj
->dev
;
253 struct sg_table
*sgt
;
256 sgt
= kzalloc(sizeof(*sgt
), GFP_KERNEL
);
258 return ERR_PTR(-ENOMEM
);
260 ret
= dma_get_sgtable_attrs(drm
->dev
, sgt
, rk_obj
->kvaddr
,
261 rk_obj
->dma_addr
, obj
->size
,
264 DRM_ERROR("failed to allocate sgt, %d\n", ret
);
272 void *rockchip_gem_prime_vmap(struct drm_gem_object
*obj
)
274 struct rockchip_gem_object
*rk_obj
= to_rockchip_obj(obj
);
276 if (rk_obj
->dma_attrs
& DMA_ATTR_NO_KERNEL_MAPPING
)
279 return rk_obj
->kvaddr
;
282 void rockchip_gem_prime_vunmap(struct drm_gem_object
*obj
, void *vaddr
)