blob: 90aa6302eaf9f97f53dc574dc162ce5799bbc7f3 [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 Shimee5e7702011-12-13 14:20:23 +090058static struct exynos_drm_gem_obj *
59exynos_drm_gem_init(struct drm_device *drm_dev, struct drm_file *file_priv,
60 unsigned int *handle, unsigned int size)
Inki Dae1c248b72011-10-04 19:19:01 +090061{
62 struct exynos_drm_gem_obj *exynos_gem_obj;
Inki Dae1c248b72011-10-04 19:19:01 +090063 struct drm_gem_object *obj;
64 int ret;
65
Inki Dae1c248b72011-10-04 19:19:01 +090066 exynos_gem_obj = kzalloc(sizeof(*exynos_gem_obj), GFP_KERNEL);
67 if (!exynos_gem_obj) {
68 DRM_ERROR("failed to allocate exynos gem object.\n");
69 return ERR_PTR(-ENOMEM);
70 }
71
Inki Dae1c248b72011-10-04 19:19:01 +090072 obj = &exynos_gem_obj->base;
73
Inki Daef088d5a2011-11-12 14:51:23 +090074 ret = drm_gem_object_init(drm_dev, obj, size);
Inki Dae1c248b72011-10-04 19:19:01 +090075 if (ret < 0) {
Inki Daef088d5a2011-11-12 14:51:23 +090076 DRM_ERROR("failed to initialize gem object.\n");
77 ret = -EINVAL;
Joonyoung Shimee5e7702011-12-13 14:20:23 +090078 goto err;
Inki Dae1c248b72011-10-04 19:19:01 +090079 }
80
81 DRM_DEBUG_KMS("created file object = 0x%x\n", (unsigned int)obj->filp);
82
Inki Dae1c248b72011-10-04 19:19:01 +090083 /*
84 * allocate a id of idr table where the obj is registered
85 * and handle has the id what user can see.
86 */
87 ret = drm_gem_handle_create(file_priv, obj, handle);
88 if (ret)
Joonyoung Shim2d91cf12011-12-13 14:32:24 +090089 goto err_release;
Inki Dae1c248b72011-10-04 19:19:01 +090090
91 DRM_DEBUG_KMS("gem handle = 0x%x\n", *handle);
92
93 /* drop reference from allocate - handle holds it now. */
94 drm_gem_object_unreference_unlocked(obj);
95
96 return exynos_gem_obj;
97
Joonyoung Shimee5e7702011-12-13 14:20:23 +090098err_release:
Inki Dae1c248b72011-10-04 19:19:01 +090099 drm_gem_object_release(obj);
100
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900101err:
Inki Dae1c248b72011-10-04 19:19:01 +0900102 kfree(exynos_gem_obj);
Inki Dae1c248b72011-10-04 19:19:01 +0900103 return ERR_PTR(ret);
104}
105
Inki Daef088d5a2011-11-12 14:51:23 +0900106struct exynos_drm_gem_obj *exynos_drm_gem_create(struct drm_device *dev,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900107 struct drm_file *file_priv,
108 unsigned int *handle,
109 unsigned long size)
Inki Daef088d5a2011-11-12 14:51:23 +0900110{
111
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900112 struct exynos_drm_gem_obj *exynos_gem_obj;
Inki Dae2c871122011-11-12 15:23:32 +0900113 struct exynos_drm_gem_buf *buffer;
Inki Daef088d5a2011-11-12 14:51:23 +0900114
115 size = roundup(size, PAGE_SIZE);
Inki Daef088d5a2011-11-12 14:51:23 +0900116 DRM_DEBUG_KMS("%s: size = 0x%lx\n", __FILE__, size);
117
Inki Dae2c871122011-11-12 15:23:32 +0900118 buffer = exynos_drm_buf_create(dev, size);
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900119 if (!buffer)
120 return ERR_PTR(-ENOMEM);
Inki Daef088d5a2011-11-12 14:51:23 +0900121
122 exynos_gem_obj = exynos_drm_gem_init(dev, file_priv, handle, size);
123 if (IS_ERR(exynos_gem_obj)) {
Seung-Woo Kimca22e3cc2011-11-15 16:25:39 +0900124 exynos_drm_buf_destroy(dev, buffer);
125 return exynos_gem_obj;
Inki Daef088d5a2011-11-12 14:51:23 +0900126 }
127
Inki Dae2c871122011-11-12 15:23:32 +0900128 exynos_gem_obj->buffer = buffer;
Inki Daef088d5a2011-11-12 14:51:23 +0900129
130 return exynos_gem_obj;
Inki Daef088d5a2011-11-12 14:51:23 +0900131}
132
Inki Dae1c248b72011-10-04 19:19:01 +0900133int exynos_drm_gem_create_ioctl(struct drm_device *dev, void *data,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900134 struct drm_file *file_priv)
Inki Dae1c248b72011-10-04 19:19:01 +0900135{
136 struct drm_exynos_gem_create *args = data;
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900137 struct exynos_drm_gem_obj *exynos_gem_obj;
Inki Dae1c248b72011-10-04 19:19:01 +0900138
Inki Daef088d5a2011-11-12 14:51:23 +0900139 DRM_DEBUG_KMS("%s\n", __FILE__);
Inki Dae1c248b72011-10-04 19:19:01 +0900140
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900141 exynos_gem_obj = exynos_drm_gem_create(dev, file_priv, &args->handle,
142 args->size);
Inki Dae1c248b72011-10-04 19:19:01 +0900143 if (IS_ERR(exynos_gem_obj))
144 return PTR_ERR(exynos_gem_obj);
145
146 return 0;
147}
148
149int exynos_drm_gem_map_offset_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_map_off *args = data;
153
154 DRM_DEBUG_KMS("%s\n", __FILE__);
155
156 DRM_DEBUG_KMS("handle = 0x%x, offset = 0x%lx\n",
157 args->handle, (unsigned long)args->offset);
158
159 if (!(dev->driver->driver_features & DRIVER_GEM)) {
160 DRM_ERROR("does not support GEM.\n");
161 return -ENODEV;
162 }
163
164 return exynos_drm_gem_dumb_map_offset(file_priv, dev, args->handle,
165 &args->offset);
166}
167
168static int exynos_drm_gem_mmap_buffer(struct file *filp,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900169 struct vm_area_struct *vma)
Inki Dae1c248b72011-10-04 19:19:01 +0900170{
171 struct drm_gem_object *obj = filp->private_data;
172 struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
Inki Dae2c871122011-11-12 15:23:32 +0900173 struct exynos_drm_gem_buf *buffer;
Inki Dae1c248b72011-10-04 19:19:01 +0900174 unsigned long pfn, vm_size;
175
176 DRM_DEBUG_KMS("%s\n", __FILE__);
177
178 vma->vm_flags |= (VM_IO | VM_RESERVED);
179
180 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
181 vma->vm_file = filp;
182
183 vm_size = vma->vm_end - vma->vm_start;
184 /*
Inki Dae2c871122011-11-12 15:23:32 +0900185 * a buffer contains information to physically continuous memory
Inki Dae1c248b72011-10-04 19:19:01 +0900186 * allocated by user request or at framebuffer creation.
187 */
Inki Dae2c871122011-11-12 15:23:32 +0900188 buffer = exynos_gem_obj->buffer;
Inki Dae1c248b72011-10-04 19:19:01 +0900189
190 /* check if user-requested size is valid. */
Inki Dae2c871122011-11-12 15:23:32 +0900191 if (vm_size > buffer->size)
Inki Dae1c248b72011-10-04 19:19:01 +0900192 return -EINVAL;
193
194 /*
195 * get page frame number to physical memory to be mapped
196 * to user space.
197 */
Inki Dae2c871122011-11-12 15:23:32 +0900198 pfn = ((unsigned long)exynos_gem_obj->buffer->dma_addr) >> PAGE_SHIFT;
Inki Dae1c248b72011-10-04 19:19:01 +0900199
200 DRM_DEBUG_KMS("pfn = 0x%lx\n", pfn);
201
202 if (remap_pfn_range(vma, vma->vm_start, pfn, vm_size,
203 vma->vm_page_prot)) {
204 DRM_ERROR("failed to remap pfn range.\n");
205 return -EAGAIN;
206 }
207
208 return 0;
209}
210
211static const struct file_operations exynos_drm_gem_fops = {
212 .mmap = exynos_drm_gem_mmap_buffer,
213};
214
215int exynos_drm_gem_mmap_ioctl(struct drm_device *dev, void *data,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900216 struct drm_file *file_priv)
Inki Dae1c248b72011-10-04 19:19:01 +0900217{
218 struct drm_exynos_gem_mmap *args = data;
219 struct drm_gem_object *obj;
220 unsigned int addr;
221
222 DRM_DEBUG_KMS("%s\n", __FILE__);
223
224 if (!(dev->driver->driver_features & DRIVER_GEM)) {
225 DRM_ERROR("does not support GEM.\n");
226 return -ENODEV;
227 }
228
229 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
230 if (!obj) {
231 DRM_ERROR("failed to lookup gem object.\n");
232 return -EINVAL;
233 }
234
235 obj->filp->f_op = &exynos_drm_gem_fops;
236 obj->filp->private_data = obj;
237
238 down_write(&current->mm->mmap_sem);
239 addr = do_mmap(obj->filp, 0, args->size,
240 PROT_READ | PROT_WRITE, MAP_SHARED, 0);
241 up_write(&current->mm->mmap_sem);
242
243 drm_gem_object_unreference_unlocked(obj);
244
245 if (IS_ERR((void *)addr))
246 return PTR_ERR((void *)addr);
247
248 args->mapped = addr;
249
250 DRM_DEBUG_KMS("mapped = 0x%lx\n", (unsigned long)args->mapped);
251
252 return 0;
253}
254
255int exynos_drm_gem_init_object(struct drm_gem_object *obj)
256{
257 DRM_DEBUG_KMS("%s\n", __FILE__);
258
259 return 0;
260}
261
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900262void exynos_drm_gem_free_object(struct drm_gem_object *obj)
Inki Dae1c248b72011-10-04 19:19:01 +0900263{
264 struct exynos_drm_gem_obj *exynos_gem_obj;
265
266 DRM_DEBUG_KMS("%s\n", __FILE__);
267
268 DRM_DEBUG_KMS("handle count = %d\n",
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900269 atomic_read(&obj->handle_count));
Inki Dae1c248b72011-10-04 19:19:01 +0900270
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900271 if (obj->map_list.map)
272 drm_gem_free_mmap_offset(obj);
Inki Dae1c248b72011-10-04 19:19:01 +0900273
274 /* release file pointer to gem object. */
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900275 drm_gem_object_release(obj);
Inki Dae1c248b72011-10-04 19:19:01 +0900276
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900277 exynos_gem_obj = to_exynos_gem_obj(obj);
Inki Dae1c248b72011-10-04 19:19:01 +0900278
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900279 exynos_drm_buf_destroy(obj->dev, exynos_gem_obj->buffer);
Inki Dae1c248b72011-10-04 19:19:01 +0900280
281 kfree(exynos_gem_obj);
282}
283
284int exynos_drm_gem_dumb_create(struct drm_file *file_priv,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900285 struct drm_device *dev,
286 struct drm_mode_create_dumb *args)
Inki Dae1c248b72011-10-04 19:19:01 +0900287{
288 struct exynos_drm_gem_obj *exynos_gem_obj;
289
290 DRM_DEBUG_KMS("%s\n", __FILE__);
291
292 /*
293 * alocate memory to be used for framebuffer.
294 * - this callback would be called by user application
295 * with DRM_IOCTL_MODE_CREATE_DUMB command.
296 */
297
298 args->pitch = args->width * args->bpp >> 3;
299 args->size = args->pitch * args->height;
300
Inki Daef088d5a2011-11-12 14:51:23 +0900301 exynos_gem_obj = exynos_drm_gem_create(dev, file_priv, &args->handle,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900302 args->size);
Inki Dae1c248b72011-10-04 19:19:01 +0900303 if (IS_ERR(exynos_gem_obj))
304 return PTR_ERR(exynos_gem_obj);
305
306 return 0;
307}
308
309int exynos_drm_gem_dumb_map_offset(struct drm_file *file_priv,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900310 struct drm_device *dev, uint32_t handle,
311 uint64_t *offset)
Inki Dae1c248b72011-10-04 19:19:01 +0900312{
313 struct exynos_drm_gem_obj *exynos_gem_obj;
314 struct drm_gem_object *obj;
Joonyoung Shim2d91cf12011-12-13 14:32:24 +0900315 int ret = 0;
Inki Dae1c248b72011-10-04 19:19:01 +0900316
317 DRM_DEBUG_KMS("%s\n", __FILE__);
318
319 mutex_lock(&dev->struct_mutex);
320
321 /*
322 * get offset of memory allocated for drm framebuffer.
323 * - this callback would be called by user application
324 * with DRM_IOCTL_MODE_MAP_DUMB command.
325 */
326
327 obj = drm_gem_object_lookup(dev, file_priv, handle);
328 if (!obj) {
329 DRM_ERROR("failed to lookup gem object.\n");
Joonyoung Shim2d91cf12011-12-13 14:32:24 +0900330 ret = -EINVAL;
331 goto unlock;
Inki Dae1c248b72011-10-04 19:19:01 +0900332 }
333
334 exynos_gem_obj = to_exynos_gem_obj(obj);
335
Joonyoung Shim2d91cf12011-12-13 14:32:24 +0900336 if (!exynos_gem_obj->base.map_list.map) {
337 ret = drm_gem_create_mmap_offset(&exynos_gem_obj->base);
338 if (ret)
339 goto out;
340 }
Inki Dae1c248b72011-10-04 19:19:01 +0900341
Joonyoung Shim2d91cf12011-12-13 14:32:24 +0900342 *offset = (u64)exynos_gem_obj->base.map_list.hash.key << PAGE_SHIFT;
Inki Dae1c248b72011-10-04 19:19:01 +0900343 DRM_DEBUG_KMS("offset = 0x%lx\n", (unsigned long)*offset);
344
Joonyoung Shim2d91cf12011-12-13 14:32:24 +0900345out:
346 drm_gem_object_unreference(obj);
347unlock:
Inki Dae1c248b72011-10-04 19:19:01 +0900348 mutex_unlock(&dev->struct_mutex);
Joonyoung Shim2d91cf12011-12-13 14:32:24 +0900349 return ret;
Inki Dae1c248b72011-10-04 19:19:01 +0900350}
351
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900352int exynos_drm_gem_dumb_destroy(struct drm_file *file_priv,
353 struct drm_device *dev,
354 unsigned int handle)
355{
356 int ret;
357
358 DRM_DEBUG_KMS("%s\n", __FILE__);
359
360 /*
361 * obj->refcount and obj->handle_count are decreased and
362 * if both them are 0 then exynos_drm_gem_free_object()
363 * would be called by callback to release resources.
364 */
365 ret = drm_gem_handle_delete(file_priv, handle);
366 if (ret < 0) {
367 DRM_ERROR("failed to delete drm_gem_handle.\n");
368 return ret;
369 }
370
371 return 0;
372}
373
Inki Dae1c248b72011-10-04 19:19:01 +0900374int exynos_drm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
375{
376 struct drm_gem_object *obj = vma->vm_private_data;
377 struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
378 struct drm_device *dev = obj->dev;
379 unsigned long pfn;
380 pgoff_t page_offset;
381 int ret;
382
383 page_offset = ((unsigned long)vmf->virtual_address -
384 vma->vm_start) >> PAGE_SHIFT;
385
386 mutex_lock(&dev->struct_mutex);
387
Inki Dae2c871122011-11-12 15:23:32 +0900388 pfn = (((unsigned long)exynos_gem_obj->buffer->dma_addr) >>
389 PAGE_SHIFT) + page_offset;
Inki Dae1c248b72011-10-04 19:19:01 +0900390
391 ret = vm_insert_mixed(vma, (unsigned long)vmf->virtual_address, pfn);
392
393 mutex_unlock(&dev->struct_mutex);
394
395 return convert_to_vm_err_msg(ret);
396}
397
398int exynos_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
399{
400 int ret;
401
402 DRM_DEBUG_KMS("%s\n", __FILE__);
403
404 /* set vm_area_struct. */
405 ret = drm_gem_mmap(filp, vma);
406 if (ret < 0) {
407 DRM_ERROR("failed to mmap.\n");
408 return ret;
409 }
410
411 vma->vm_flags &= ~VM_PFNMAP;
412 vma->vm_flags |= VM_MIXEDMAP;
413
414 return ret;
415}
416
Inki Dae1c248b72011-10-04 19:19:01 +0900417MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
418MODULE_DESCRIPTION("Samsung SoC DRM GEM Module");
419MODULE_LICENSE("GPL");