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 int rockchip_gem_mmap_buf(struct drm_gem_object
*obj
,
58 struct vm_area_struct
*vma
)
60 struct rockchip_gem_object
*rk_obj
= to_rockchip_obj(obj
);
61 struct drm_device
*drm
= obj
->dev
;
62 unsigned long vm_size
;
64 vma
->vm_flags
|= VM_IO
| VM_DONTEXPAND
| VM_DONTDUMP
;
65 vm_size
= vma
->vm_end
- vma
->vm_start
;
67 if (vm_size
> obj
->size
)
70 return dma_mmap_attrs(drm
->dev
, vma
, rk_obj
->kvaddr
, rk_obj
->dma_addr
,
71 obj
->size
, &rk_obj
->dma_attrs
);
74 /* drm driver mmap file operations */
75 int rockchip_gem_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
77 struct drm_file
*priv
= filp
->private_data
;
78 struct drm_device
*dev
= priv
->minor
->dev
;
79 struct drm_gem_object
*obj
;
80 struct drm_vma_offset_node
*node
;
83 if (drm_device_is_unplugged(dev
))
86 mutex_lock(&dev
->struct_mutex
);
88 node
= drm_vma_offset_exact_lookup(dev
->vma_offset_manager
,
92 mutex_unlock(&dev
->struct_mutex
);
93 DRM_ERROR("failed to find vma node.\n");
95 } else if (!drm_vma_node_is_allowed(node
, filp
)) {
96 mutex_unlock(&dev
->struct_mutex
);
100 obj
= container_of(node
, struct drm_gem_object
, vma_node
);
101 ret
= rockchip_gem_mmap_buf(obj
, vma
);
103 mutex_unlock(&dev
->struct_mutex
);
108 struct rockchip_gem_object
*
109 rockchip_gem_create_object(struct drm_device
*drm
, unsigned int size
,
112 struct rockchip_gem_object
*rk_obj
;
113 struct drm_gem_object
*obj
;
116 size
= round_up(size
, PAGE_SIZE
);
118 rk_obj
= kzalloc(sizeof(*rk_obj
), GFP_KERNEL
);
120 return ERR_PTR(-ENOMEM
);
124 drm_gem_private_object_init(drm
, obj
, size
);
126 ret
= rockchip_gem_alloc_buf(rk_obj
, alloc_kmap
);
128 goto err_free_rk_obj
;
138 * rockchip_gem_free_object - (struct drm_driver)->gem_free_object callback
141 void rockchip_gem_free_object(struct drm_gem_object
*obj
)
143 struct rockchip_gem_object
*rk_obj
;
145 drm_gem_free_mmap_offset(obj
);
147 rk_obj
= to_rockchip_obj(obj
);
149 rockchip_gem_free_buf(rk_obj
);
155 * rockchip_gem_create_with_handle - allocate an object with the given
156 * size and create a gem handle on it
158 * returns a struct rockchip_gem_object* on success or ERR_PTR values
161 static struct rockchip_gem_object
*
162 rockchip_gem_create_with_handle(struct drm_file
*file_priv
,
163 struct drm_device
*drm
, unsigned int size
,
164 unsigned int *handle
)
166 struct rockchip_gem_object
*rk_obj
;
167 struct drm_gem_object
*obj
;
170 rk_obj
= rockchip_gem_create_object(drm
, size
, false);
172 return ERR_CAST(rk_obj
);
177 * allocate a id of idr table where the obj is registered
178 * and handle has the id what user can see.
180 ret
= drm_gem_handle_create(file_priv
, obj
, handle
);
182 goto err_handle_create
;
184 /* drop reference from allocate - handle holds it now. */
185 drm_gem_object_unreference_unlocked(obj
);
190 rockchip_gem_free_object(obj
);
195 int rockchip_gem_dumb_map_offset(struct drm_file
*file_priv
,
196 struct drm_device
*dev
, uint32_t handle
,
199 struct drm_gem_object
*obj
;
202 mutex_lock(&dev
->struct_mutex
);
204 obj
= drm_gem_object_lookup(dev
, file_priv
, handle
);
206 DRM_ERROR("failed to lookup gem object.\n");
211 ret
= drm_gem_create_mmap_offset(obj
);
215 *offset
= drm_vma_node_offset_addr(&obj
->vma_node
);
216 DRM_DEBUG_KMS("offset = 0x%llx\n", *offset
);
219 drm_gem_object_unreference(obj
);
221 mutex_unlock(&dev
->struct_mutex
);
226 * rockchip_gem_dumb_create - (struct drm_driver)->dumb_create callback
229 * This aligns the pitch and size arguments to the minimum required. wrap
230 * this into your own function if you need bigger alignment.
232 int rockchip_gem_dumb_create(struct drm_file
*file_priv
,
233 struct drm_device
*dev
,
234 struct drm_mode_create_dumb
*args
)
236 struct rockchip_gem_object
*rk_obj
;
237 int min_pitch
= DIV_ROUND_UP(args
->width
* args
->bpp
, 8);
240 * align to 64 bytes since Mali requires it.
242 min_pitch
= ALIGN(min_pitch
, 64);
244 if (args
->pitch
< min_pitch
)
245 args
->pitch
= min_pitch
;
247 if (args
->size
< args
->pitch
* args
->height
)
248 args
->size
= args
->pitch
* args
->height
;
250 rk_obj
= rockchip_gem_create_with_handle(file_priv
, dev
, args
->size
,
253 return PTR_ERR_OR_ZERO(rk_obj
);
257 * Allocate a sg_table for this GEM object.
258 * Note: Both the table's contents, and the sg_table itself must be freed by
260 * Returns a pointer to the newly allocated sg_table, or an ERR_PTR() error.
262 struct sg_table
*rockchip_gem_prime_get_sg_table(struct drm_gem_object
*obj
)
264 struct rockchip_gem_object
*rk_obj
= to_rockchip_obj(obj
);
265 struct drm_device
*drm
= obj
->dev
;
266 struct sg_table
*sgt
;
269 sgt
= kzalloc(sizeof(*sgt
), GFP_KERNEL
);
271 return ERR_PTR(-ENOMEM
);
273 ret
= dma_get_sgtable_attrs(drm
->dev
, sgt
, rk_obj
->kvaddr
,
274 rk_obj
->dma_addr
, obj
->size
,
277 DRM_ERROR("failed to allocate sgt, %d\n", ret
);
285 void *rockchip_gem_prime_vmap(struct drm_gem_object
*obj
)
287 struct rockchip_gem_object
*rk_obj
= to_rockchip_obj(obj
);
289 if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING
, &rk_obj
->dma_attrs
))
292 return rk_obj
->kvaddr
;
295 void rockchip_gem_prime_vunmap(struct drm_gem_object
*obj
, void *vaddr
)