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
;
71 ret
= dma_mmap_attrs(drm
->dev
, vma
, rk_obj
->kvaddr
, rk_obj
->dma_addr
,
72 obj
->size
, &rk_obj
->dma_attrs
);
74 drm_gem_vm_close(vma
);
79 int rockchip_gem_mmap_buf(struct drm_gem_object
*obj
,
80 struct vm_area_struct
*vma
)
82 struct drm_device
*drm
= obj
->dev
;
85 mutex_lock(&drm
->struct_mutex
);
86 ret
= drm_gem_mmap_obj(obj
, obj
->size
, vma
);
87 mutex_unlock(&drm
->struct_mutex
);
91 return rockchip_drm_gem_object_mmap(obj
, vma
);
94 /* drm driver mmap file operations */
95 int rockchip_gem_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
97 struct drm_gem_object
*obj
;
100 ret
= drm_gem_mmap(filp
, vma
);
104 obj
= vma
->vm_private_data
;
106 return rockchip_drm_gem_object_mmap(obj
, vma
);
109 struct rockchip_gem_object
*
110 rockchip_gem_create_object(struct drm_device
*drm
, unsigned int size
,
113 struct rockchip_gem_object
*rk_obj
;
114 struct drm_gem_object
*obj
;
117 size
= round_up(size
, PAGE_SIZE
);
119 rk_obj
= kzalloc(sizeof(*rk_obj
), GFP_KERNEL
);
121 return ERR_PTR(-ENOMEM
);
125 drm_gem_private_object_init(drm
, obj
, size
);
127 ret
= rockchip_gem_alloc_buf(rk_obj
, alloc_kmap
);
129 goto err_free_rk_obj
;
139 * rockchip_gem_free_object - (struct drm_driver)->gem_free_object callback
142 void rockchip_gem_free_object(struct drm_gem_object
*obj
)
144 struct rockchip_gem_object
*rk_obj
;
146 drm_gem_free_mmap_offset(obj
);
148 rk_obj
= to_rockchip_obj(obj
);
150 rockchip_gem_free_buf(rk_obj
);
156 * rockchip_gem_create_with_handle - allocate an object with the given
157 * size and create a gem handle on it
159 * returns a struct rockchip_gem_object* on success or ERR_PTR values
162 static struct rockchip_gem_object
*
163 rockchip_gem_create_with_handle(struct drm_file
*file_priv
,
164 struct drm_device
*drm
, unsigned int size
,
165 unsigned int *handle
)
167 struct rockchip_gem_object
*rk_obj
;
168 struct drm_gem_object
*obj
;
171 rk_obj
= rockchip_gem_create_object(drm
, size
, false);
173 return ERR_CAST(rk_obj
);
178 * allocate a id of idr table where the obj is registered
179 * and handle has the id what user can see.
181 ret
= drm_gem_handle_create(file_priv
, obj
, handle
);
183 goto err_handle_create
;
185 /* drop reference from allocate - handle holds it now. */
186 drm_gem_object_unreference_unlocked(obj
);
191 rockchip_gem_free_object(obj
);
196 int rockchip_gem_dumb_map_offset(struct drm_file
*file_priv
,
197 struct drm_device
*dev
, uint32_t handle
,
200 struct drm_gem_object
*obj
;
203 mutex_lock(&dev
->struct_mutex
);
205 obj
= drm_gem_object_lookup(dev
, file_priv
, handle
);
207 DRM_ERROR("failed to lookup gem object.\n");
212 ret
= drm_gem_create_mmap_offset(obj
);
216 *offset
= drm_vma_node_offset_addr(&obj
->vma_node
);
217 DRM_DEBUG_KMS("offset = 0x%llx\n", *offset
);
220 drm_gem_object_unreference(obj
);
222 mutex_unlock(&dev
->struct_mutex
);
227 * rockchip_gem_dumb_create - (struct drm_driver)->dumb_create callback
230 * This aligns the pitch and size arguments to the minimum required. wrap
231 * this into your own function if you need bigger alignment.
233 int rockchip_gem_dumb_create(struct drm_file
*file_priv
,
234 struct drm_device
*dev
,
235 struct drm_mode_create_dumb
*args
)
237 struct rockchip_gem_object
*rk_obj
;
238 int min_pitch
= DIV_ROUND_UP(args
->width
* args
->bpp
, 8);
241 * align to 64 bytes since Mali requires it.
243 min_pitch
= ALIGN(min_pitch
, 64);
245 if (args
->pitch
< min_pitch
)
246 args
->pitch
= min_pitch
;
248 if (args
->size
< args
->pitch
* args
->height
)
249 args
->size
= args
->pitch
* args
->height
;
251 rk_obj
= rockchip_gem_create_with_handle(file_priv
, dev
, args
->size
,
254 return PTR_ERR_OR_ZERO(rk_obj
);
258 * Allocate a sg_table for this GEM object.
259 * Note: Both the table's contents, and the sg_table itself must be freed by
261 * Returns a pointer to the newly allocated sg_table, or an ERR_PTR() error.
263 struct sg_table
*rockchip_gem_prime_get_sg_table(struct drm_gem_object
*obj
)
265 struct rockchip_gem_object
*rk_obj
= to_rockchip_obj(obj
);
266 struct drm_device
*drm
= obj
->dev
;
267 struct sg_table
*sgt
;
270 sgt
= kzalloc(sizeof(*sgt
), GFP_KERNEL
);
272 return ERR_PTR(-ENOMEM
);
274 ret
= dma_get_sgtable_attrs(drm
->dev
, sgt
, rk_obj
->kvaddr
,
275 rk_obj
->dma_addr
, obj
->size
,
278 DRM_ERROR("failed to allocate sgt, %d\n", ret
);
286 void *rockchip_gem_prime_vmap(struct drm_gem_object
*obj
)
288 struct rockchip_gem_object
*rk_obj
= to_rockchip_obj(obj
);
290 if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING
, &rk_obj
->dma_attrs
))
293 return rk_obj
->kvaddr
;
296 void rockchip_gem_prime_vunmap(struct drm_gem_object
*obj
, void *vaddr
)