OMAPDSS: VENC: fix NULL pointer dereference in DSS2 VENC sysfs debug attr on OMAP4
[zen-stable.git] / drivers / gpu / drm / exynos / exynos_drm_gem.c
blob025abb3e3b67906948c1c68cf684333d915d9d75
1 /* exynos_drm_gem.c
3 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4 * Author: Inki Dae <inki.dae@samsung.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
26 #include "drmP.h"
27 #include "drm.h"
29 #include <drm/exynos_drm.h>
31 #include "exynos_drm_drv.h"
32 #include "exynos_drm_gem.h"
33 #include "exynos_drm_buf.h"
35 static unsigned int convert_to_vm_err_msg(int msg)
37 unsigned int out_msg;
39 switch (msg) {
40 case 0:
41 case -ERESTARTSYS:
42 case -EINTR:
43 out_msg = VM_FAULT_NOPAGE;
44 break;
46 case -ENOMEM:
47 out_msg = VM_FAULT_OOM;
48 break;
50 default:
51 out_msg = VM_FAULT_SIGBUS;
52 break;
55 return out_msg;
58 static int exynos_drm_gem_handle_create(struct drm_gem_object *obj,
59 struct drm_file *file_priv,
60 unsigned int *handle)
62 int ret;
65 * allocate a id of idr table where the obj is registered
66 * and handle has the id what user can see.
68 ret = drm_gem_handle_create(file_priv, obj, handle);
69 if (ret)
70 return ret;
72 DRM_DEBUG_KMS("gem handle = 0x%x\n", *handle);
74 /* drop reference from allocate - handle holds it now. */
75 drm_gem_object_unreference_unlocked(obj);
77 return 0;
80 void exynos_drm_gem_destroy(struct exynos_drm_gem_obj *exynos_gem_obj)
82 struct drm_gem_object *obj;
84 DRM_DEBUG_KMS("%s\n", __FILE__);
86 if (!exynos_gem_obj)
87 return;
89 obj = &exynos_gem_obj->base;
91 DRM_DEBUG_KMS("handle count = %d\n", atomic_read(&obj->handle_count));
93 exynos_drm_buf_destroy(obj->dev, exynos_gem_obj->buffer);
95 if (obj->map_list.map)
96 drm_gem_free_mmap_offset(obj);
98 /* release file pointer to gem object. */
99 drm_gem_object_release(obj);
101 kfree(exynos_gem_obj);
104 static struct exynos_drm_gem_obj *exynos_drm_gem_init(struct drm_device *dev,
105 unsigned long size)
107 struct exynos_drm_gem_obj *exynos_gem_obj;
108 struct drm_gem_object *obj;
109 int ret;
111 exynos_gem_obj = kzalloc(sizeof(*exynos_gem_obj), GFP_KERNEL);
112 if (!exynos_gem_obj) {
113 DRM_ERROR("failed to allocate exynos gem object\n");
114 return NULL;
117 obj = &exynos_gem_obj->base;
119 ret = drm_gem_object_init(dev, obj, size);
120 if (ret < 0) {
121 DRM_ERROR("failed to initialize gem object\n");
122 kfree(exynos_gem_obj);
123 return NULL;
126 DRM_DEBUG_KMS("created file object = 0x%x\n", (unsigned int)obj->filp);
128 return exynos_gem_obj;
131 struct exynos_drm_gem_obj *exynos_drm_gem_create(struct drm_device *dev,
132 unsigned long size)
134 struct exynos_drm_gem_buf *buffer;
135 struct exynos_drm_gem_obj *exynos_gem_obj;
137 size = roundup(size, PAGE_SIZE);
138 DRM_DEBUG_KMS("%s: size = 0x%lx\n", __FILE__, size);
140 buffer = exynos_drm_buf_create(dev, size);
141 if (!buffer)
142 return ERR_PTR(-ENOMEM);
144 exynos_gem_obj = exynos_drm_gem_init(dev, size);
145 if (!exynos_gem_obj) {
146 exynos_drm_buf_destroy(dev, buffer);
147 return ERR_PTR(-ENOMEM);
150 exynos_gem_obj->buffer = buffer;
152 return exynos_gem_obj;
155 int exynos_drm_gem_create_ioctl(struct drm_device *dev, void *data,
156 struct drm_file *file_priv)
158 struct drm_exynos_gem_create *args = data;
159 struct exynos_drm_gem_obj *exynos_gem_obj;
160 int ret;
162 DRM_DEBUG_KMS("%s\n", __FILE__);
164 exynos_gem_obj = exynos_drm_gem_create(dev, args->size);
165 if (IS_ERR(exynos_gem_obj))
166 return PTR_ERR(exynos_gem_obj);
168 ret = exynos_drm_gem_handle_create(&exynos_gem_obj->base, file_priv,
169 &args->handle);
170 if (ret) {
171 exynos_drm_gem_destroy(exynos_gem_obj);
172 return ret;
175 return 0;
178 int exynos_drm_gem_map_offset_ioctl(struct drm_device *dev, void *data,
179 struct drm_file *file_priv)
181 struct drm_exynos_gem_map_off *args = data;
183 DRM_DEBUG_KMS("%s\n", __FILE__);
185 DRM_DEBUG_KMS("handle = 0x%x, offset = 0x%lx\n",
186 args->handle, (unsigned long)args->offset);
188 if (!(dev->driver->driver_features & DRIVER_GEM)) {
189 DRM_ERROR("does not support GEM.\n");
190 return -ENODEV;
193 return exynos_drm_gem_dumb_map_offset(file_priv, dev, args->handle,
194 &args->offset);
197 static int exynos_drm_gem_mmap_buffer(struct file *filp,
198 struct vm_area_struct *vma)
200 struct drm_gem_object *obj = filp->private_data;
201 struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
202 struct exynos_drm_gem_buf *buffer;
203 unsigned long pfn, vm_size;
205 DRM_DEBUG_KMS("%s\n", __FILE__);
207 vma->vm_flags |= (VM_IO | VM_RESERVED);
209 /* in case of direct mapping, always having non-cachable attribute */
210 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
211 vma->vm_file = filp;
213 vm_size = vma->vm_end - vma->vm_start;
215 * a buffer contains information to physically continuous memory
216 * allocated by user request or at framebuffer creation.
218 buffer = exynos_gem_obj->buffer;
220 /* check if user-requested size is valid. */
221 if (vm_size > buffer->size)
222 return -EINVAL;
225 * get page frame number to physical memory to be mapped
226 * to user space.
228 pfn = ((unsigned long)exynos_gem_obj->buffer->dma_addr) >> PAGE_SHIFT;
230 DRM_DEBUG_KMS("pfn = 0x%lx\n", pfn);
232 if (remap_pfn_range(vma, vma->vm_start, pfn, vm_size,
233 vma->vm_page_prot)) {
234 DRM_ERROR("failed to remap pfn range.\n");
235 return -EAGAIN;
238 return 0;
241 static const struct file_operations exynos_drm_gem_fops = {
242 .mmap = exynos_drm_gem_mmap_buffer,
245 int exynos_drm_gem_mmap_ioctl(struct drm_device *dev, void *data,
246 struct drm_file *file_priv)
248 struct drm_exynos_gem_mmap *args = data;
249 struct drm_gem_object *obj;
250 unsigned int addr;
252 DRM_DEBUG_KMS("%s\n", __FILE__);
254 if (!(dev->driver->driver_features & DRIVER_GEM)) {
255 DRM_ERROR("does not support GEM.\n");
256 return -ENODEV;
259 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
260 if (!obj) {
261 DRM_ERROR("failed to lookup gem object.\n");
262 return -EINVAL;
265 obj->filp->f_op = &exynos_drm_gem_fops;
266 obj->filp->private_data = obj;
268 down_write(&current->mm->mmap_sem);
269 addr = do_mmap(obj->filp, 0, args->size,
270 PROT_READ | PROT_WRITE, MAP_SHARED, 0);
271 up_write(&current->mm->mmap_sem);
273 drm_gem_object_unreference_unlocked(obj);
275 if (IS_ERR((void *)addr))
276 return PTR_ERR((void *)addr);
278 args->mapped = addr;
280 DRM_DEBUG_KMS("mapped = 0x%lx\n", (unsigned long)args->mapped);
282 return 0;
285 int exynos_drm_gem_init_object(struct drm_gem_object *obj)
287 DRM_DEBUG_KMS("%s\n", __FILE__);
289 return 0;
292 void exynos_drm_gem_free_object(struct drm_gem_object *obj)
294 DRM_DEBUG_KMS("%s\n", __FILE__);
296 exynos_drm_gem_destroy(to_exynos_gem_obj(obj));
299 int exynos_drm_gem_dumb_create(struct drm_file *file_priv,
300 struct drm_device *dev,
301 struct drm_mode_create_dumb *args)
303 struct exynos_drm_gem_obj *exynos_gem_obj;
304 int ret;
306 DRM_DEBUG_KMS("%s\n", __FILE__);
309 * alocate memory to be used for framebuffer.
310 * - this callback would be called by user application
311 * with DRM_IOCTL_MODE_CREATE_DUMB command.
314 args->pitch = args->width * args->bpp >> 3;
315 args->size = args->pitch * args->height;
317 exynos_gem_obj = exynos_drm_gem_create(dev, args->size);
318 if (IS_ERR(exynos_gem_obj))
319 return PTR_ERR(exynos_gem_obj);
321 ret = exynos_drm_gem_handle_create(&exynos_gem_obj->base, file_priv,
322 &args->handle);
323 if (ret) {
324 exynos_drm_gem_destroy(exynos_gem_obj);
325 return ret;
328 return 0;
331 int exynos_drm_gem_dumb_map_offset(struct drm_file *file_priv,
332 struct drm_device *dev, uint32_t handle,
333 uint64_t *offset)
335 struct exynos_drm_gem_obj *exynos_gem_obj;
336 struct drm_gem_object *obj;
337 int ret = 0;
339 DRM_DEBUG_KMS("%s\n", __FILE__);
341 mutex_lock(&dev->struct_mutex);
344 * get offset of memory allocated for drm framebuffer.
345 * - this callback would be called by user application
346 * with DRM_IOCTL_MODE_MAP_DUMB command.
349 obj = drm_gem_object_lookup(dev, file_priv, handle);
350 if (!obj) {
351 DRM_ERROR("failed to lookup gem object.\n");
352 ret = -EINVAL;
353 goto unlock;
356 exynos_gem_obj = to_exynos_gem_obj(obj);
358 if (!exynos_gem_obj->base.map_list.map) {
359 ret = drm_gem_create_mmap_offset(&exynos_gem_obj->base);
360 if (ret)
361 goto out;
364 *offset = (u64)exynos_gem_obj->base.map_list.hash.key << PAGE_SHIFT;
365 DRM_DEBUG_KMS("offset = 0x%lx\n", (unsigned long)*offset);
367 out:
368 drm_gem_object_unreference(obj);
369 unlock:
370 mutex_unlock(&dev->struct_mutex);
371 return ret;
374 int exynos_drm_gem_dumb_destroy(struct drm_file *file_priv,
375 struct drm_device *dev,
376 unsigned int handle)
378 int ret;
380 DRM_DEBUG_KMS("%s\n", __FILE__);
383 * obj->refcount and obj->handle_count are decreased and
384 * if both them are 0 then exynos_drm_gem_free_object()
385 * would be called by callback to release resources.
387 ret = drm_gem_handle_delete(file_priv, handle);
388 if (ret < 0) {
389 DRM_ERROR("failed to delete drm_gem_handle.\n");
390 return ret;
393 return 0;
396 int exynos_drm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
398 struct drm_gem_object *obj = vma->vm_private_data;
399 struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
400 struct drm_device *dev = obj->dev;
401 unsigned long pfn;
402 pgoff_t page_offset;
403 int ret;
405 page_offset = ((unsigned long)vmf->virtual_address -
406 vma->vm_start) >> PAGE_SHIFT;
408 mutex_lock(&dev->struct_mutex);
410 pfn = (((unsigned long)exynos_gem_obj->buffer->dma_addr) >>
411 PAGE_SHIFT) + page_offset;
413 ret = vm_insert_mixed(vma, (unsigned long)vmf->virtual_address, pfn);
415 mutex_unlock(&dev->struct_mutex);
417 return convert_to_vm_err_msg(ret);
420 int exynos_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
422 int ret;
424 DRM_DEBUG_KMS("%s\n", __FILE__);
426 /* set vm_area_struct. */
427 ret = drm_gem_mmap(filp, vma);
428 if (ret < 0) {
429 DRM_ERROR("failed to mmap.\n");
430 return ret;
433 vma->vm_flags &= ~VM_PFNMAP;
434 vma->vm_flags |= VM_MIXEDMAP;
436 return ret;
439 MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
440 MODULE_DESCRIPTION("Samsung SoC DRM GEM Module");
441 MODULE_LICENSE("GPL");