blob: 025abb3e3b67906948c1c68cf684333d915d9d75 [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
Joonyoung Shim23648392011-12-13 14:39:13 +090058static int exynos_drm_gem_handle_create(struct drm_gem_object *obj,
59 struct drm_file *file_priv,
60 unsigned int *handle)
Inki Dae1c248b72011-10-04 19:19:01 +090061{
Inki Dae1c248b72011-10-04 19:19:01 +090062 int ret;
63
Inki Dae1c248b72011-10-04 19:19:01 +090064 /*
65 * allocate a id of idr table where the obj is registered
66 * and handle has the id what user can see.
67 */
68 ret = drm_gem_handle_create(file_priv, obj, handle);
69 if (ret)
Joonyoung Shim23648392011-12-13 14:39:13 +090070 return ret;
Inki Dae1c248b72011-10-04 19:19:01 +090071
72 DRM_DEBUG_KMS("gem handle = 0x%x\n", *handle);
73
74 /* drop reference from allocate - handle holds it now. */
75 drm_gem_object_unreference_unlocked(obj);
76
Joonyoung Shim23648392011-12-13 14:39:13 +090077 return 0;
78}
Inki Dae1c248b72011-10-04 19:19:01 +090079
Joonyoung Shim23648392011-12-13 14:39:13 +090080void exynos_drm_gem_destroy(struct exynos_drm_gem_obj *exynos_gem_obj)
81{
82 struct drm_gem_object *obj;
83
84 DRM_DEBUG_KMS("%s\n", __FILE__);
85
86 if (!exynos_gem_obj)
87 return;
88
89 obj = &exynos_gem_obj->base;
90
91 DRM_DEBUG_KMS("handle count = %d\n", atomic_read(&obj->handle_count));
92
93 exynos_drm_buf_destroy(obj->dev, exynos_gem_obj->buffer);
94
95 if (obj->map_list.map)
96 drm_gem_free_mmap_offset(obj);
97
98 /* release file pointer to gem object. */
Inki Dae1c248b72011-10-04 19:19:01 +090099 drm_gem_object_release(obj);
100
Inki Dae1c248b72011-10-04 19:19:01 +0900101 kfree(exynos_gem_obj);
Joonyoung Shim23648392011-12-13 14:39:13 +0900102}
103
104static struct exynos_drm_gem_obj *exynos_drm_gem_init(struct drm_device *dev,
105 unsigned long size)
106{
107 struct exynos_drm_gem_obj *exynos_gem_obj;
108 struct drm_gem_object *obj;
109 int ret;
110
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;
115 }
116
117 obj = &exynos_gem_obj->base;
118
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;
124 }
125
126 DRM_DEBUG_KMS("created file object = 0x%x\n", (unsigned int)obj->filp);
127
128 return exynos_gem_obj;
Inki Dae1c248b72011-10-04 19:19:01 +0900129}
130
Inki Daef088d5a2011-11-12 14:51:23 +0900131struct exynos_drm_gem_obj *exynos_drm_gem_create(struct drm_device *dev,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900132 unsigned long size)
Inki Daef088d5a2011-11-12 14:51:23 +0900133{
Inki Dae2c871122011-11-12 15:23:32 +0900134 struct exynos_drm_gem_buf *buffer;
Joonyoung Shim23648392011-12-13 14:39:13 +0900135 struct exynos_drm_gem_obj *exynos_gem_obj;
Inki Daef088d5a2011-11-12 14:51:23 +0900136
137 size = roundup(size, PAGE_SIZE);
Inki Daef088d5a2011-11-12 14:51:23 +0900138 DRM_DEBUG_KMS("%s: size = 0x%lx\n", __FILE__, size);
139
Inki Dae2c871122011-11-12 15:23:32 +0900140 buffer = exynos_drm_buf_create(dev, size);
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900141 if (!buffer)
142 return ERR_PTR(-ENOMEM);
Inki Daef088d5a2011-11-12 14:51:23 +0900143
Joonyoung Shim23648392011-12-13 14:39:13 +0900144 exynos_gem_obj = exynos_drm_gem_init(dev, size);
145 if (!exynos_gem_obj) {
Seung-Woo Kimca22e3cc2011-11-15 16:25:39 +0900146 exynos_drm_buf_destroy(dev, buffer);
Joonyoung Shim23648392011-12-13 14:39:13 +0900147 return ERR_PTR(-ENOMEM);
Inki Daef088d5a2011-11-12 14:51:23 +0900148 }
149
Inki Dae2c871122011-11-12 15:23:32 +0900150 exynos_gem_obj->buffer = buffer;
Inki Daef088d5a2011-11-12 14:51:23 +0900151
152 return exynos_gem_obj;
Inki Daef088d5a2011-11-12 14:51:23 +0900153}
154
Inki Dae1c248b72011-10-04 19:19:01 +0900155int exynos_drm_gem_create_ioctl(struct drm_device *dev, void *data,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900156 struct drm_file *file_priv)
Inki Dae1c248b72011-10-04 19:19:01 +0900157{
158 struct drm_exynos_gem_create *args = data;
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900159 struct exynos_drm_gem_obj *exynos_gem_obj;
Joonyoung Shim23648392011-12-13 14:39:13 +0900160 int ret;
Inki Dae1c248b72011-10-04 19:19:01 +0900161
Inki Daef088d5a2011-11-12 14:51:23 +0900162 DRM_DEBUG_KMS("%s\n", __FILE__);
Inki Dae1c248b72011-10-04 19:19:01 +0900163
Joonyoung Shim23648392011-12-13 14:39:13 +0900164 exynos_gem_obj = exynos_drm_gem_create(dev, args->size);
Inki Dae1c248b72011-10-04 19:19:01 +0900165 if (IS_ERR(exynos_gem_obj))
166 return PTR_ERR(exynos_gem_obj);
167
Joonyoung Shim23648392011-12-13 14:39:13 +0900168 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;
173 }
174
Inki Dae1c248b72011-10-04 19:19:01 +0900175 return 0;
176}
177
178int exynos_drm_gem_map_offset_ioctl(struct drm_device *dev, void *data,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900179 struct drm_file *file_priv)
Inki Dae1c248b72011-10-04 19:19:01 +0900180{
181 struct drm_exynos_gem_map_off *args = data;
182
183 DRM_DEBUG_KMS("%s\n", __FILE__);
184
185 DRM_DEBUG_KMS("handle = 0x%x, offset = 0x%lx\n",
186 args->handle, (unsigned long)args->offset);
187
188 if (!(dev->driver->driver_features & DRIVER_GEM)) {
189 DRM_ERROR("does not support GEM.\n");
190 return -ENODEV;
191 }
192
193 return exynos_drm_gem_dumb_map_offset(file_priv, dev, args->handle,
194 &args->offset);
195}
196
197static int exynos_drm_gem_mmap_buffer(struct file *filp,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900198 struct vm_area_struct *vma)
Inki Dae1c248b72011-10-04 19:19:01 +0900199{
200 struct drm_gem_object *obj = filp->private_data;
201 struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
Inki Dae2c871122011-11-12 15:23:32 +0900202 struct exynos_drm_gem_buf *buffer;
Inki Dae1c248b72011-10-04 19:19:01 +0900203 unsigned long pfn, vm_size;
204
205 DRM_DEBUG_KMS("%s\n", __FILE__);
206
207 vma->vm_flags |= (VM_IO | VM_RESERVED);
208
Joonyoung Shim23648392011-12-13 14:39:13 +0900209 /* in case of direct mapping, always having non-cachable attribute */
Inki Dae1c248b72011-10-04 19:19:01 +0900210 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
211 vma->vm_file = filp;
212
213 vm_size = vma->vm_end - vma->vm_start;
214 /*
Inki Dae2c871122011-11-12 15:23:32 +0900215 * a buffer contains information to physically continuous memory
Inki Dae1c248b72011-10-04 19:19:01 +0900216 * allocated by user request or at framebuffer creation.
217 */
Inki Dae2c871122011-11-12 15:23:32 +0900218 buffer = exynos_gem_obj->buffer;
Inki Dae1c248b72011-10-04 19:19:01 +0900219
220 /* check if user-requested size is valid. */
Inki Dae2c871122011-11-12 15:23:32 +0900221 if (vm_size > buffer->size)
Inki Dae1c248b72011-10-04 19:19:01 +0900222 return -EINVAL;
223
224 /*
225 * get page frame number to physical memory to be mapped
226 * to user space.
227 */
Inki Dae2c871122011-11-12 15:23:32 +0900228 pfn = ((unsigned long)exynos_gem_obj->buffer->dma_addr) >> PAGE_SHIFT;
Inki Dae1c248b72011-10-04 19:19:01 +0900229
230 DRM_DEBUG_KMS("pfn = 0x%lx\n", pfn);
231
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;
236 }
237
238 return 0;
239}
240
241static const struct file_operations exynos_drm_gem_fops = {
242 .mmap = exynos_drm_gem_mmap_buffer,
243};
244
245int exynos_drm_gem_mmap_ioctl(struct drm_device *dev, void *data,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900246 struct drm_file *file_priv)
Inki Dae1c248b72011-10-04 19:19:01 +0900247{
248 struct drm_exynos_gem_mmap *args = data;
249 struct drm_gem_object *obj;
250 unsigned int addr;
251
252 DRM_DEBUG_KMS("%s\n", __FILE__);
253
254 if (!(dev->driver->driver_features & DRIVER_GEM)) {
255 DRM_ERROR("does not support GEM.\n");
256 return -ENODEV;
257 }
258
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;
263 }
264
265 obj->filp->f_op = &exynos_drm_gem_fops;
266 obj->filp->private_data = obj;
267
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);
272
273 drm_gem_object_unreference_unlocked(obj);
274
275 if (IS_ERR((void *)addr))
276 return PTR_ERR((void *)addr);
277
278 args->mapped = addr;
279
280 DRM_DEBUG_KMS("mapped = 0x%lx\n", (unsigned long)args->mapped);
281
282 return 0;
283}
284
285int exynos_drm_gem_init_object(struct drm_gem_object *obj)
286{
287 DRM_DEBUG_KMS("%s\n", __FILE__);
288
289 return 0;
290}
291
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900292void exynos_drm_gem_free_object(struct drm_gem_object *obj)
Inki Dae1c248b72011-10-04 19:19:01 +0900293{
Inki Dae1c248b72011-10-04 19:19:01 +0900294 DRM_DEBUG_KMS("%s\n", __FILE__);
295
Joonyoung Shim23648392011-12-13 14:39:13 +0900296 exynos_drm_gem_destroy(to_exynos_gem_obj(obj));
Inki Dae1c248b72011-10-04 19:19:01 +0900297}
298
299int exynos_drm_gem_dumb_create(struct drm_file *file_priv,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900300 struct drm_device *dev,
301 struct drm_mode_create_dumb *args)
Inki Dae1c248b72011-10-04 19:19:01 +0900302{
303 struct exynos_drm_gem_obj *exynos_gem_obj;
Joonyoung Shim23648392011-12-13 14:39:13 +0900304 int ret;
Inki Dae1c248b72011-10-04 19:19:01 +0900305
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
Joonyoung Shim23648392011-12-13 14:39:13 +0900317 exynos_gem_obj = exynos_drm_gem_create(dev, args->size);
Inki Dae1c248b72011-10-04 19:19:01 +0900318 if (IS_ERR(exynos_gem_obj))
319 return PTR_ERR(exynos_gem_obj);
320
Joonyoung Shim23648392011-12-13 14:39:13 +0900321 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;
326 }
327
Inki Dae1c248b72011-10-04 19:19:01 +0900328 return 0;
329}
330
331int exynos_drm_gem_dumb_map_offset(struct drm_file *file_priv,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900332 struct drm_device *dev, uint32_t handle,
333 uint64_t *offset)
Inki Dae1c248b72011-10-04 19:19:01 +0900334{
335 struct exynos_drm_gem_obj *exynos_gem_obj;
336 struct drm_gem_object *obj;
Joonyoung Shim2d91cf12011-12-13 14:32:24 +0900337 int ret = 0;
Inki Dae1c248b72011-10-04 19:19:01 +0900338
339 DRM_DEBUG_KMS("%s\n", __FILE__);
340
341 mutex_lock(&dev->struct_mutex);
342
343 /*
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.
347 */
348
349 obj = drm_gem_object_lookup(dev, file_priv, handle);
350 if (!obj) {
351 DRM_ERROR("failed to lookup gem object.\n");
Joonyoung Shim2d91cf12011-12-13 14:32:24 +0900352 ret = -EINVAL;
353 goto unlock;
Inki Dae1c248b72011-10-04 19:19:01 +0900354 }
355
356 exynos_gem_obj = to_exynos_gem_obj(obj);
357
Joonyoung Shim2d91cf12011-12-13 14:32:24 +0900358 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;
362 }
Inki Dae1c248b72011-10-04 19:19:01 +0900363
Joonyoung Shim2d91cf12011-12-13 14:32:24 +0900364 *offset = (u64)exynos_gem_obj->base.map_list.hash.key << PAGE_SHIFT;
Inki Dae1c248b72011-10-04 19:19:01 +0900365 DRM_DEBUG_KMS("offset = 0x%lx\n", (unsigned long)*offset);
366
Joonyoung Shim2d91cf12011-12-13 14:32:24 +0900367out:
368 drm_gem_object_unreference(obj);
369unlock:
Inki Dae1c248b72011-10-04 19:19:01 +0900370 mutex_unlock(&dev->struct_mutex);
Joonyoung Shim2d91cf12011-12-13 14:32:24 +0900371 return ret;
Inki Dae1c248b72011-10-04 19:19:01 +0900372}
373
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900374int exynos_drm_gem_dumb_destroy(struct drm_file *file_priv,
375 struct drm_device *dev,
376 unsigned int handle)
377{
378 int ret;
379
380 DRM_DEBUG_KMS("%s\n", __FILE__);
381
382 /*
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.
386 */
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;
391 }
392
393 return 0;
394}
395
Inki Dae1c248b72011-10-04 19:19:01 +0900396int exynos_drm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
397{
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;
404
405 page_offset = ((unsigned long)vmf->virtual_address -
406 vma->vm_start) >> PAGE_SHIFT;
407
408 mutex_lock(&dev->struct_mutex);
409
Inki Dae2c871122011-11-12 15:23:32 +0900410 pfn = (((unsigned long)exynos_gem_obj->buffer->dma_addr) >>
411 PAGE_SHIFT) + page_offset;
Inki Dae1c248b72011-10-04 19:19:01 +0900412
413 ret = vm_insert_mixed(vma, (unsigned long)vmf->virtual_address, pfn);
414
415 mutex_unlock(&dev->struct_mutex);
416
417 return convert_to_vm_err_msg(ret);
418}
419
420int exynos_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
421{
422 int ret;
423
424 DRM_DEBUG_KMS("%s\n", __FILE__);
425
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;
431 }
432
433 vma->vm_flags &= ~VM_PFNMAP;
434 vma->vm_flags |= VM_MIXEDMAP;
435
436 return ret;
437}
438
Inki Dae1c248b72011-10-04 19:19:01 +0900439MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
440MODULE_DESCRIPTION("Samsung SoC DRM GEM Module");
441MODULE_LICENSE("GPL");