drm/exynos: Stop using drm_framebuffer_unregister_private
[linux/fpc-iii.git] / drivers / gpu / drm / rockchip / rockchip_drm_gem.c
blobb70f9423379c4d29fdb90f340bfcfbb8b56d299b
1 /*
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.
15 #include <drm/drm.h>
16 #include <drm/drmP.h>
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,
24 bool alloc_kmap)
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;
31 if (!alloc_kmap)
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,
36 rk_obj->dma_attrs);
37 if (!rk_obj->kvaddr) {
38 DRM_ERROR("failed to allocate %zu byte dma buffer", obj->size);
39 return -ENOMEM;
42 return 0;
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,
51 rk_obj->dma_attrs);
54 static int rockchip_drm_gem_object_mmap(struct drm_gem_object *obj,
55 struct vm_area_struct *vma)
58 int ret;
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;
67 vma->vm_pgoff = 0;
69 ret = dma_mmap_attrs(drm->dev, vma, rk_obj->kvaddr, rk_obj->dma_addr,
70 obj->size, rk_obj->dma_attrs);
71 if (ret)
72 drm_gem_vm_close(vma);
74 return ret;
77 int rockchip_gem_mmap_buf(struct drm_gem_object *obj,
78 struct vm_area_struct *vma)
80 int ret;
82 ret = drm_gem_mmap_obj(obj, obj->size, vma);
83 if (ret)
84 return ret;
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;
93 int ret;
95 ret = drm_gem_mmap(filp, vma);
96 if (ret)
97 return ret;
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,
106 bool alloc_kmap)
108 struct rockchip_gem_object *rk_obj;
109 struct drm_gem_object *obj;
110 int ret;
112 size = round_up(size, PAGE_SIZE);
114 rk_obj = kzalloc(sizeof(*rk_obj), GFP_KERNEL);
115 if (!rk_obj)
116 return ERR_PTR(-ENOMEM);
118 obj = &rk_obj->base;
120 drm_gem_private_object_init(drm, obj, size);
122 ret = rockchip_gem_alloc_buf(rk_obj, alloc_kmap);
123 if (ret)
124 goto err_free_rk_obj;
126 return rk_obj;
128 err_free_rk_obj:
129 kfree(rk_obj);
130 return ERR_PTR(ret);
134 * rockchip_gem_free_object - (struct drm_driver)->gem_free_object callback
135 * function
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);
147 kfree(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
155 * on failure.
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;
164 int ret;
166 rk_obj = rockchip_gem_create_object(drm, size, false);
167 if (IS_ERR(rk_obj))
168 return ERR_CAST(rk_obj);
170 obj = &rk_obj->base;
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);
177 if (ret)
178 goto err_handle_create;
180 /* drop reference from allocate - handle holds it now. */
181 drm_gem_object_unreference_unlocked(obj);
183 return rk_obj;
185 err_handle_create:
186 rockchip_gem_free_object(obj);
188 return ERR_PTR(ret);
191 int rockchip_gem_dumb_map_offset(struct drm_file *file_priv,
192 struct drm_device *dev, uint32_t handle,
193 uint64_t *offset)
195 struct drm_gem_object *obj;
196 int ret;
198 obj = drm_gem_object_lookup(file_priv, handle);
199 if (!obj) {
200 DRM_ERROR("failed to lookup gem object.\n");
201 return -EINVAL;
204 ret = drm_gem_create_mmap_offset(obj);
205 if (ret)
206 goto out;
208 *offset = drm_vma_node_offset_addr(&obj->vma_node);
209 DRM_DEBUG_KMS("offset = 0x%llx\n", *offset);
211 out:
212 drm_gem_object_unreference_unlocked(obj);
214 return 0;
218 * rockchip_gem_dumb_create - (struct drm_driver)->dumb_create callback
219 * function
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,
238 &args->handle);
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
246 * the caller.
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;
254 int ret;
256 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
257 if (!sgt)
258 return ERR_PTR(-ENOMEM);
260 ret = dma_get_sgtable_attrs(drm->dev, sgt, rk_obj->kvaddr,
261 rk_obj->dma_addr, obj->size,
262 rk_obj->dma_attrs);
263 if (ret) {
264 DRM_ERROR("failed to allocate sgt, %d\n", ret);
265 kfree(sgt);
266 return ERR_PTR(ret);
269 return sgt;
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)
277 return NULL;
279 return rk_obj->kvaddr;
282 void rockchip_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
284 /* Nothing to do */