blob: 44ce07c2133f1747636113c74df810f03dbf337b [file] [log] [blame]
Inki Dae1c248b72011-10-04 19:19:01 +09001/* exynos_drm_gem.c
2 *
3 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4 * Author: Inki Dae <inki.dae@samsung.com>
5 *
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:
12 *
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.
16 *
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.
24 */
25
26#include "drmP.h"
27#include "drm.h"
28
29#include <drm/exynos_drm.h>
30
31#include "exynos_drm_drv.h"
32#include "exynos_drm_gem.h"
33#include "exynos_drm_buf.h"
34
35static unsigned int convert_to_vm_err_msg(int msg)
36{
37 unsigned int out_msg;
38
39 switch (msg) {
40 case 0:
41 case -ERESTARTSYS:
42 case -EINTR:
43 out_msg = VM_FAULT_NOPAGE;
44 break;
45
46 case -ENOMEM:
47 out_msg = VM_FAULT_OOM;
48 break;
49
50 default:
51 out_msg = VM_FAULT_SIGBUS;
52 break;
53 }
54
55 return out_msg;
56}
57
58static unsigned int get_gem_mmap_offset(struct drm_gem_object *obj)
59{
60 DRM_DEBUG_KMS("%s\n", __FILE__);
61
62 return (unsigned int)obj->map_list.hash.key << PAGE_SHIFT;
63}
64
Joonyoung Shimee5e7702011-12-13 14:20:23 +090065static struct exynos_drm_gem_obj *
66exynos_drm_gem_init(struct drm_device *drm_dev, struct drm_file *file_priv,
67 unsigned int *handle, unsigned int size)
Inki Dae1c248b72011-10-04 19:19:01 +090068{
69 struct exynos_drm_gem_obj *exynos_gem_obj;
Inki Dae1c248b72011-10-04 19:19:01 +090070 struct drm_gem_object *obj;
71 int ret;
72
Inki Dae1c248b72011-10-04 19:19:01 +090073 exynos_gem_obj = kzalloc(sizeof(*exynos_gem_obj), GFP_KERNEL);
74 if (!exynos_gem_obj) {
75 DRM_ERROR("failed to allocate exynos gem object.\n");
76 return ERR_PTR(-ENOMEM);
77 }
78
Inki Dae1c248b72011-10-04 19:19:01 +090079 obj = &exynos_gem_obj->base;
80
Inki Daef088d5a2011-11-12 14:51:23 +090081 ret = drm_gem_object_init(drm_dev, obj, size);
Inki Dae1c248b72011-10-04 19:19:01 +090082 if (ret < 0) {
Inki Daef088d5a2011-11-12 14:51:23 +090083 DRM_ERROR("failed to initialize gem object.\n");
84 ret = -EINVAL;
Joonyoung Shimee5e7702011-12-13 14:20:23 +090085 goto err;
Inki Dae1c248b72011-10-04 19:19:01 +090086 }
87
88 DRM_DEBUG_KMS("created file object = 0x%x\n", (unsigned int)obj->filp);
89
90 ret = drm_gem_create_mmap_offset(obj);
91 if (ret < 0) {
92 DRM_ERROR("failed to allocate mmap offset.\n");
Joonyoung Shimee5e7702011-12-13 14:20:23 +090093 goto err_release;
Inki Dae1c248b72011-10-04 19:19:01 +090094 }
95
96 /*
97 * allocate a id of idr table where the obj is registered
98 * and handle has the id what user can see.
99 */
100 ret = drm_gem_handle_create(file_priv, obj, handle);
101 if (ret)
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900102 goto err_free_mmap_offset;
Inki Dae1c248b72011-10-04 19:19:01 +0900103
104 DRM_DEBUG_KMS("gem handle = 0x%x\n", *handle);
105
106 /* drop reference from allocate - handle holds it now. */
107 drm_gem_object_unreference_unlocked(obj);
108
109 return exynos_gem_obj;
110
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900111err_free_mmap_offset:
Inki Dae1c248b72011-10-04 19:19:01 +0900112 drm_gem_free_mmap_offset(obj);
113
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900114err_release:
Inki Dae1c248b72011-10-04 19:19:01 +0900115 drm_gem_object_release(obj);
116
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900117err:
Inki Dae1c248b72011-10-04 19:19:01 +0900118 kfree(exynos_gem_obj);
Inki Dae1c248b72011-10-04 19:19:01 +0900119 return ERR_PTR(ret);
120}
121
Inki Daef088d5a2011-11-12 14:51:23 +0900122struct exynos_drm_gem_obj *exynos_drm_gem_create(struct drm_device *dev,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900123 struct drm_file *file_priv,
124 unsigned int *handle,
125 unsigned long size)
Inki Daef088d5a2011-11-12 14:51:23 +0900126{
127
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900128 struct exynos_drm_gem_obj *exynos_gem_obj;
Inki Dae2c871122011-11-12 15:23:32 +0900129 struct exynos_drm_gem_buf *buffer;
Inki Daef088d5a2011-11-12 14:51:23 +0900130
131 size = roundup(size, PAGE_SIZE);
Inki Daef088d5a2011-11-12 14:51:23 +0900132 DRM_DEBUG_KMS("%s: size = 0x%lx\n", __FILE__, size);
133
Inki Dae2c871122011-11-12 15:23:32 +0900134 buffer = exynos_drm_buf_create(dev, size);
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900135 if (!buffer)
136 return ERR_PTR(-ENOMEM);
Inki Daef088d5a2011-11-12 14:51:23 +0900137
138 exynos_gem_obj = exynos_drm_gem_init(dev, file_priv, handle, size);
139 if (IS_ERR(exynos_gem_obj)) {
Seung-Woo Kimca22e3cc2011-11-15 16:25:39 +0900140 exynos_drm_buf_destroy(dev, buffer);
141 return exynos_gem_obj;
Inki Daef088d5a2011-11-12 14:51:23 +0900142 }
143
Inki Dae2c871122011-11-12 15:23:32 +0900144 exynos_gem_obj->buffer = buffer;
Inki Daef088d5a2011-11-12 14:51:23 +0900145
146 return exynos_gem_obj;
Inki Daef088d5a2011-11-12 14:51:23 +0900147}
148
Inki Dae1c248b72011-10-04 19:19:01 +0900149int exynos_drm_gem_create_ioctl(struct drm_device *dev, void *data,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900150 struct drm_file *file_priv)
Inki Dae1c248b72011-10-04 19:19:01 +0900151{
152 struct drm_exynos_gem_create *args = data;
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900153 struct exynos_drm_gem_obj *exynos_gem_obj;
Inki Dae1c248b72011-10-04 19:19:01 +0900154
Inki Daef088d5a2011-11-12 14:51:23 +0900155 DRM_DEBUG_KMS("%s\n", __FILE__);
Inki Dae1c248b72011-10-04 19:19:01 +0900156
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900157 exynos_gem_obj = exynos_drm_gem_create(dev, file_priv, &args->handle,
158 args->size);
Inki Dae1c248b72011-10-04 19:19:01 +0900159 if (IS_ERR(exynos_gem_obj))
160 return PTR_ERR(exynos_gem_obj);
161
162 return 0;
163}
164
165int exynos_drm_gem_map_offset_ioctl(struct drm_device *dev, void *data,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900166 struct drm_file *file_priv)
Inki Dae1c248b72011-10-04 19:19:01 +0900167{
168 struct drm_exynos_gem_map_off *args = data;
169
170 DRM_DEBUG_KMS("%s\n", __FILE__);
171
172 DRM_DEBUG_KMS("handle = 0x%x, offset = 0x%lx\n",
173 args->handle, (unsigned long)args->offset);
174
175 if (!(dev->driver->driver_features & DRIVER_GEM)) {
176 DRM_ERROR("does not support GEM.\n");
177 return -ENODEV;
178 }
179
180 return exynos_drm_gem_dumb_map_offset(file_priv, dev, args->handle,
181 &args->offset);
182}
183
184static int exynos_drm_gem_mmap_buffer(struct file *filp,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900185 struct vm_area_struct *vma)
Inki Dae1c248b72011-10-04 19:19:01 +0900186{
187 struct drm_gem_object *obj = filp->private_data;
188 struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
Inki Dae2c871122011-11-12 15:23:32 +0900189 struct exynos_drm_gem_buf *buffer;
Inki Dae1c248b72011-10-04 19:19:01 +0900190 unsigned long pfn, vm_size;
191
192 DRM_DEBUG_KMS("%s\n", __FILE__);
193
194 vma->vm_flags |= (VM_IO | VM_RESERVED);
195
196 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
197 vma->vm_file = filp;
198
199 vm_size = vma->vm_end - vma->vm_start;
200 /*
Inki Dae2c871122011-11-12 15:23:32 +0900201 * a buffer contains information to physically continuous memory
Inki Dae1c248b72011-10-04 19:19:01 +0900202 * allocated by user request or at framebuffer creation.
203 */
Inki Dae2c871122011-11-12 15:23:32 +0900204 buffer = exynos_gem_obj->buffer;
Inki Dae1c248b72011-10-04 19:19:01 +0900205
206 /* check if user-requested size is valid. */
Inki Dae2c871122011-11-12 15:23:32 +0900207 if (vm_size > buffer->size)
Inki Dae1c248b72011-10-04 19:19:01 +0900208 return -EINVAL;
209
210 /*
211 * get page frame number to physical memory to be mapped
212 * to user space.
213 */
Inki Dae2c871122011-11-12 15:23:32 +0900214 pfn = ((unsigned long)exynos_gem_obj->buffer->dma_addr) >> PAGE_SHIFT;
Inki Dae1c248b72011-10-04 19:19:01 +0900215
216 DRM_DEBUG_KMS("pfn = 0x%lx\n", pfn);
217
218 if (remap_pfn_range(vma, vma->vm_start, pfn, vm_size,
219 vma->vm_page_prot)) {
220 DRM_ERROR("failed to remap pfn range.\n");
221 return -EAGAIN;
222 }
223
224 return 0;
225}
226
227static const struct file_operations exynos_drm_gem_fops = {
228 .mmap = exynos_drm_gem_mmap_buffer,
229};
230
231int exynos_drm_gem_mmap_ioctl(struct drm_device *dev, void *data,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900232 struct drm_file *file_priv)
Inki Dae1c248b72011-10-04 19:19:01 +0900233{
234 struct drm_exynos_gem_mmap *args = data;
235 struct drm_gem_object *obj;
236 unsigned int addr;
237
238 DRM_DEBUG_KMS("%s\n", __FILE__);
239
240 if (!(dev->driver->driver_features & DRIVER_GEM)) {
241 DRM_ERROR("does not support GEM.\n");
242 return -ENODEV;
243 }
244
245 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
246 if (!obj) {
247 DRM_ERROR("failed to lookup gem object.\n");
248 return -EINVAL;
249 }
250
251 obj->filp->f_op = &exynos_drm_gem_fops;
252 obj->filp->private_data = obj;
253
254 down_write(&current->mm->mmap_sem);
255 addr = do_mmap(obj->filp, 0, args->size,
256 PROT_READ | PROT_WRITE, MAP_SHARED, 0);
257 up_write(&current->mm->mmap_sem);
258
259 drm_gem_object_unreference_unlocked(obj);
260
261 if (IS_ERR((void *)addr))
262 return PTR_ERR((void *)addr);
263
264 args->mapped = addr;
265
266 DRM_DEBUG_KMS("mapped = 0x%lx\n", (unsigned long)args->mapped);
267
268 return 0;
269}
270
271int exynos_drm_gem_init_object(struct drm_gem_object *obj)
272{
273 DRM_DEBUG_KMS("%s\n", __FILE__);
274
275 return 0;
276}
277
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900278void exynos_drm_gem_free_object(struct drm_gem_object *obj)
Inki Dae1c248b72011-10-04 19:19:01 +0900279{
280 struct exynos_drm_gem_obj *exynos_gem_obj;
281
282 DRM_DEBUG_KMS("%s\n", __FILE__);
283
284 DRM_DEBUG_KMS("handle count = %d\n",
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900285 atomic_read(&obj->handle_count));
Inki Dae1c248b72011-10-04 19:19:01 +0900286
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900287 if (obj->map_list.map)
288 drm_gem_free_mmap_offset(obj);
Inki Dae1c248b72011-10-04 19:19:01 +0900289
290 /* release file pointer to gem object. */
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900291 drm_gem_object_release(obj);
Inki Dae1c248b72011-10-04 19:19:01 +0900292
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900293 exynos_gem_obj = to_exynos_gem_obj(obj);
Inki Dae1c248b72011-10-04 19:19:01 +0900294
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900295 exynos_drm_buf_destroy(obj->dev, exynos_gem_obj->buffer);
Inki Dae1c248b72011-10-04 19:19:01 +0900296
297 kfree(exynos_gem_obj);
298}
299
300int exynos_drm_gem_dumb_create(struct drm_file *file_priv,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900301 struct drm_device *dev,
302 struct drm_mode_create_dumb *args)
Inki Dae1c248b72011-10-04 19:19:01 +0900303{
304 struct exynos_drm_gem_obj *exynos_gem_obj;
305
306 DRM_DEBUG_KMS("%s\n", __FILE__);
307
308 /*
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.
312 */
313
314 args->pitch = args->width * args->bpp >> 3;
315 args->size = args->pitch * args->height;
316
Inki Daef088d5a2011-11-12 14:51:23 +0900317 exynos_gem_obj = exynos_drm_gem_create(dev, file_priv, &args->handle,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900318 args->size);
Inki Dae1c248b72011-10-04 19:19:01 +0900319 if (IS_ERR(exynos_gem_obj))
320 return PTR_ERR(exynos_gem_obj);
321
322 return 0;
323}
324
325int exynos_drm_gem_dumb_map_offset(struct drm_file *file_priv,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900326 struct drm_device *dev, uint32_t handle,
327 uint64_t *offset)
Inki Dae1c248b72011-10-04 19:19:01 +0900328{
329 struct exynos_drm_gem_obj *exynos_gem_obj;
330 struct drm_gem_object *obj;
331
332 DRM_DEBUG_KMS("%s\n", __FILE__);
333
334 mutex_lock(&dev->struct_mutex);
335
336 /*
337 * get offset of memory allocated for drm framebuffer.
338 * - this callback would be called by user application
339 * with DRM_IOCTL_MODE_MAP_DUMB command.
340 */
341
342 obj = drm_gem_object_lookup(dev, file_priv, handle);
343 if (!obj) {
344 DRM_ERROR("failed to lookup gem object.\n");
345 mutex_unlock(&dev->struct_mutex);
346 return -EINVAL;
347 }
348
349 exynos_gem_obj = to_exynos_gem_obj(obj);
350
351 *offset = get_gem_mmap_offset(&exynos_gem_obj->base);
352
353 drm_gem_object_unreference(obj);
354
355 DRM_DEBUG_KMS("offset = 0x%lx\n", (unsigned long)*offset);
356
357 mutex_unlock(&dev->struct_mutex);
358
359 return 0;
360}
361
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900362int exynos_drm_gem_dumb_destroy(struct drm_file *file_priv,
363 struct drm_device *dev,
364 unsigned int handle)
365{
366 int ret;
367
368 DRM_DEBUG_KMS("%s\n", __FILE__);
369
370 /*
371 * obj->refcount and obj->handle_count are decreased and
372 * if both them are 0 then exynos_drm_gem_free_object()
373 * would be called by callback to release resources.
374 */
375 ret = drm_gem_handle_delete(file_priv, handle);
376 if (ret < 0) {
377 DRM_ERROR("failed to delete drm_gem_handle.\n");
378 return ret;
379 }
380
381 return 0;
382}
383
Inki Dae1c248b72011-10-04 19:19:01 +0900384int exynos_drm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
385{
386 struct drm_gem_object *obj = vma->vm_private_data;
387 struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
388 struct drm_device *dev = obj->dev;
389 unsigned long pfn;
390 pgoff_t page_offset;
391 int ret;
392
393 page_offset = ((unsigned long)vmf->virtual_address -
394 vma->vm_start) >> PAGE_SHIFT;
395
396 mutex_lock(&dev->struct_mutex);
397
Inki Dae2c871122011-11-12 15:23:32 +0900398 pfn = (((unsigned long)exynos_gem_obj->buffer->dma_addr) >>
399 PAGE_SHIFT) + page_offset;
Inki Dae1c248b72011-10-04 19:19:01 +0900400
401 ret = vm_insert_mixed(vma, (unsigned long)vmf->virtual_address, pfn);
402
403 mutex_unlock(&dev->struct_mutex);
404
405 return convert_to_vm_err_msg(ret);
406}
407
408int exynos_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
409{
410 int ret;
411
412 DRM_DEBUG_KMS("%s\n", __FILE__);
413
414 /* set vm_area_struct. */
415 ret = drm_gem_mmap(filp, vma);
416 if (ret < 0) {
417 DRM_ERROR("failed to mmap.\n");
418 return ret;
419 }
420
421 vma->vm_flags &= ~VM_PFNMAP;
422 vma->vm_flags |= VM_MIXEDMAP;
423
424 return ret;
425}
426
Inki Dae1c248b72011-10-04 19:19:01 +0900427MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
428MODULE_DESCRIPTION("Samsung SoC DRM GEM Module");
429MODULE_LICENSE("GPL");