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 <linux/dma-attrs.h>
22 #include "rockchip_drm_drv.h"
23 #include "rockchip_drm_gem.h"
25 static int rockchip_gem_alloc_buf(struct rockchip_gem_object
*rk_obj
,
28 struct drm_gem_object
*obj
= &rk_obj
->base
;
29 struct drm_device
*drm
= obj
->dev
;
31 init_dma_attrs(&rk_obj
->dma_attrs
);
32 dma_set_attr(DMA_ATTR_WRITE_COMBINE
, &rk_obj
->dma_attrs
);
35 dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING
, &rk_obj
->dma_attrs
);
37 rk_obj
->kvaddr
= dma_alloc_attrs(drm
->dev
, obj
->size
,
38 &rk_obj
->dma_addr
, GFP_KERNEL
,
40 if (!rk_obj
->kvaddr
) {
41 DRM_ERROR("failed to allocate %#x byte dma buffer", obj
->size
);
48 static void rockchip_gem_free_buf(struct rockchip_gem_object
*rk_obj
)
50 struct drm_gem_object
*obj
= &rk_obj
->base
;
51 struct drm_device
*drm
= obj
->dev
;
53 dma_free_attrs(drm
->dev
, obj
->size
, rk_obj
->kvaddr
, rk_obj
->dma_addr
,
57 static int rockchip_drm_gem_object_mmap(struct drm_gem_object
*obj
,
58 struct vm_area_struct
*vma
)
62 struct rockchip_gem_object
*rk_obj
= to_rockchip_obj(obj
);
63 struct drm_device
*drm
= obj
->dev
;
66 * dma_alloc_attrs() allocated a struct page table for rk_obj, so clear
67 * VM_PFNMAP flag that was set by drm_gem_mmap_obj()/drm_gem_mmap().
69 vma
->vm_flags
&= ~VM_PFNMAP
;
72 ret
= dma_mmap_attrs(drm
->dev
, vma
, rk_obj
->kvaddr
, rk_obj
->dma_addr
,
73 obj
->size
, &rk_obj
->dma_attrs
);
75 drm_gem_vm_close(vma
);
80 int rockchip_gem_mmap_buf(struct drm_gem_object
*obj
,
81 struct vm_area_struct
*vma
)
85 ret
= drm_gem_mmap_obj(obj
, obj
->size
, vma
);
89 return rockchip_drm_gem_object_mmap(obj
, vma
);
92 /* drm driver mmap file operations */
93 int rockchip_gem_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
95 struct drm_gem_object
*obj
;
98 ret
= drm_gem_mmap(filp
, vma
);
102 obj
= vma
->vm_private_data
;
104 return rockchip_drm_gem_object_mmap(obj
, vma
);
107 struct rockchip_gem_object
*
108 rockchip_gem_create_object(struct drm_device
*drm
, unsigned int size
,
111 struct rockchip_gem_object
*rk_obj
;
112 struct drm_gem_object
*obj
;
115 size
= round_up(size
, PAGE_SIZE
);
117 rk_obj
= kzalloc(sizeof(*rk_obj
), GFP_KERNEL
);
119 return ERR_PTR(-ENOMEM
);
123 drm_gem_private_object_init(drm
, obj
, size
);
125 ret
= rockchip_gem_alloc_buf(rk_obj
, alloc_kmap
);
127 goto err_free_rk_obj
;
137 * rockchip_gem_free_object - (struct drm_driver)->gem_free_object callback
140 void rockchip_gem_free_object(struct drm_gem_object
*obj
)
142 struct rockchip_gem_object
*rk_obj
;
144 drm_gem_free_mmap_offset(obj
);
146 rk_obj
= to_rockchip_obj(obj
);
148 rockchip_gem_free_buf(rk_obj
);
154 * rockchip_gem_create_with_handle - allocate an object with the given
155 * size and create a gem handle on it
157 * returns a struct rockchip_gem_object* on success or ERR_PTR values
160 static struct rockchip_gem_object
*
161 rockchip_gem_create_with_handle(struct drm_file
*file_priv
,
162 struct drm_device
*drm
, unsigned int size
,
163 unsigned int *handle
)
165 struct rockchip_gem_object
*rk_obj
;
166 struct drm_gem_object
*obj
;
169 rk_obj
= rockchip_gem_create_object(drm
, size
, false);
171 return ERR_CAST(rk_obj
);
176 * allocate a id of idr table where the obj is registered
177 * and handle has the id what user can see.
179 ret
= drm_gem_handle_create(file_priv
, obj
, handle
);
181 goto err_handle_create
;
183 /* drop reference from allocate - handle holds it now. */
184 drm_gem_object_unreference_unlocked(obj
);
189 rockchip_gem_free_object(obj
);
194 int rockchip_gem_dumb_map_offset(struct drm_file
*file_priv
,
195 struct drm_device
*dev
, uint32_t handle
,
198 struct drm_gem_object
*obj
;
201 obj
= drm_gem_object_lookup(dev
, file_priv
, handle
);
203 DRM_ERROR("failed to lookup gem object.\n");
207 ret
= drm_gem_create_mmap_offset(obj
);
211 *offset
= drm_vma_node_offset_addr(&obj
->vma_node
);
212 DRM_DEBUG_KMS("offset = 0x%llx\n", *offset
);
215 drm_gem_object_unreference_unlocked(obj
);
221 * rockchip_gem_dumb_create - (struct drm_driver)->dumb_create callback
224 * This aligns the pitch and size arguments to the minimum required. wrap
225 * this into your own function if you need bigger alignment.
227 int rockchip_gem_dumb_create(struct drm_file
*file_priv
,
228 struct drm_device
*dev
,
229 struct drm_mode_create_dumb
*args
)
231 struct rockchip_gem_object
*rk_obj
;
232 int min_pitch
= DIV_ROUND_UP(args
->width
* args
->bpp
, 8);
235 * align to 64 bytes since Mali requires it.
237 min_pitch
= ALIGN(min_pitch
, 64);
239 if (args
->pitch
< min_pitch
)
240 args
->pitch
= min_pitch
;
242 if (args
->size
< args
->pitch
* args
->height
)
243 args
->size
= args
->pitch
* args
->height
;
245 rk_obj
= rockchip_gem_create_with_handle(file_priv
, dev
, args
->size
,
248 return PTR_ERR_OR_ZERO(rk_obj
);
252 * Allocate a sg_table for this GEM object.
253 * Note: Both the table's contents, and the sg_table itself must be freed by
255 * Returns a pointer to the newly allocated sg_table, or an ERR_PTR() error.
257 struct sg_table
*rockchip_gem_prime_get_sg_table(struct drm_gem_object
*obj
)
259 struct rockchip_gem_object
*rk_obj
= to_rockchip_obj(obj
);
260 struct drm_device
*drm
= obj
->dev
;
261 struct sg_table
*sgt
;
264 sgt
= kzalloc(sizeof(*sgt
), GFP_KERNEL
);
266 return ERR_PTR(-ENOMEM
);
268 ret
= dma_get_sgtable_attrs(drm
->dev
, sgt
, rk_obj
->kvaddr
,
269 rk_obj
->dma_addr
, obj
->size
,
272 DRM_ERROR("failed to allocate sgt, %d\n", ret
);
280 void *rockchip_gem_prime_vmap(struct drm_gem_object
*obj
)
282 struct rockchip_gem_object
*rk_obj
= to_rockchip_obj(obj
);
284 if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING
, &rk_obj
->dma_attrs
))
287 return rk_obj
->kvaddr
;
290 void rockchip_gem_prime_vunmap(struct drm_gem_object
*obj
, void *vaddr
)