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
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.
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
)
43 out_msg
= VM_FAULT_NOPAGE
;
47 out_msg
= VM_FAULT_OOM
;
51 out_msg
= VM_FAULT_SIGBUS
;
58 static int exynos_drm_gem_handle_create(struct drm_gem_object
*obj
,
59 struct drm_file
*file_priv
,
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
);
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
);
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__
);
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
,
107 struct exynos_drm_gem_obj
*exynos_gem_obj
;
108 struct drm_gem_object
*obj
;
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");
117 obj
= &exynos_gem_obj
->base
;
119 ret
= drm_gem_object_init(dev
, obj
, size
);
121 DRM_ERROR("failed to initialize gem object\n");
122 kfree(exynos_gem_obj
);
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
,
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
);
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
;
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
,
171 exynos_drm_gem_destroy(exynos_gem_obj
);
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");
193 return exynos_drm_gem_dumb_map_offset(file_priv
, dev
, args
->handle
,
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
);
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
)
225 * get page frame number to physical memory to be mapped
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");
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
;
252 DRM_DEBUG_KMS("%s\n", __FILE__
);
254 if (!(dev
->driver
->driver_features
& DRIVER_GEM
)) {
255 DRM_ERROR("does not support GEM.\n");
259 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
261 DRM_ERROR("failed to lookup gem object.\n");
265 obj
->filp
->f_op
= &exynos_drm_gem_fops
;
266 obj
->filp
->private_data
= obj
;
268 down_write(¤t
->mm
->mmap_sem
);
269 addr
= do_mmap(obj
->filp
, 0, args
->size
,
270 PROT_READ
| PROT_WRITE
, MAP_SHARED
, 0);
271 up_write(¤t
->mm
->mmap_sem
);
273 drm_gem_object_unreference_unlocked(obj
);
275 if (IS_ERR((void *)addr
))
276 return PTR_ERR((void *)addr
);
280 DRM_DEBUG_KMS("mapped = 0x%lx\n", (unsigned long)args
->mapped
);
285 int exynos_drm_gem_init_object(struct drm_gem_object
*obj
)
287 DRM_DEBUG_KMS("%s\n", __FILE__
);
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
;
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
,
324 exynos_drm_gem_destroy(exynos_gem_obj
);
331 int exynos_drm_gem_dumb_map_offset(struct drm_file
*file_priv
,
332 struct drm_device
*dev
, uint32_t handle
,
335 struct exynos_drm_gem_obj
*exynos_gem_obj
;
336 struct drm_gem_object
*obj
;
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
);
351 DRM_ERROR("failed to lookup gem object.\n");
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
);
364 *offset
= (u64
)exynos_gem_obj
->base
.map_list
.hash
.key
<< PAGE_SHIFT
;
365 DRM_DEBUG_KMS("offset = 0x%lx\n", (unsigned long)*offset
);
368 drm_gem_object_unreference(obj
);
370 mutex_unlock(&dev
->struct_mutex
);
374 int exynos_drm_gem_dumb_destroy(struct drm_file
*file_priv
,
375 struct drm_device
*dev
,
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
);
389 DRM_ERROR("failed to delete drm_gem_handle.\n");
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
;
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
)
424 DRM_DEBUG_KMS("%s\n", __FILE__
);
426 /* set vm_area_struct. */
427 ret
= drm_gem_mmap(filp
, vma
);
429 DRM_ERROR("failed to mmap.\n");
433 vma
->vm_flags
&= ~VM_PFNMAP
;
434 vma
->vm_flags
|= VM_MIXEDMAP
;
439 MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
440 MODULE_DESCRIPTION("Samsung SoC DRM GEM Module");
441 MODULE_LICENSE("GPL");