drm/exynos: remove function roundup_gem_size
[deliverable/linux.git] / drivers / gpu / drm / exynos / exynos_drm_gem.c
1 /* exynos_drm_gem.c
2 *
3 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4 * Author: Inki Dae <inki.dae@samsung.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
10 */
11
12 #include <drm/drmP.h>
13 #include <drm/drm_vma_manager.h>
14
15 #include <linux/shmem_fs.h>
16 #include <drm/exynos_drm.h>
17
18 #include "exynos_drm_drv.h"
19 #include "exynos_drm_gem.h"
20 #include "exynos_drm_buf.h"
21 #include "exynos_drm_iommu.h"
22
23 static int exynos_drm_gem_handle_create(struct drm_gem_object *obj,
24 struct drm_file *file_priv,
25 unsigned int *handle)
26 {
27 int ret;
28
29 /*
30 * allocate a id of idr table where the obj is registered
31 * and handle has the id what user can see.
32 */
33 ret = drm_gem_handle_create(file_priv, obj, handle);
34 if (ret)
35 return ret;
36
37 DRM_DEBUG_KMS("gem handle = 0x%x\n", *handle);
38
39 /* drop reference from allocate - handle holds it now. */
40 drm_gem_object_unreference_unlocked(obj);
41
42 return 0;
43 }
44
45 void exynos_drm_gem_destroy(struct exynos_drm_gem_obj *exynos_gem_obj)
46 {
47 struct drm_gem_object *obj;
48 struct exynos_drm_gem_buf *buf;
49
50 obj = &exynos_gem_obj->base;
51 buf = exynos_gem_obj->buffer;
52
53 DRM_DEBUG_KMS("handle count = %d\n", obj->handle_count);
54
55 /*
56 * do not release memory region from exporter.
57 *
58 * the region will be released by exporter
59 * once dmabuf's refcount becomes 0.
60 */
61 if (obj->import_attach)
62 goto out;
63
64 exynos_drm_free_buf(obj->dev, exynos_gem_obj->flags, buf);
65
66 out:
67 exynos_drm_fini_buf(obj->dev, buf);
68 exynos_gem_obj->buffer = NULL;
69
70 drm_gem_free_mmap_offset(obj);
71
72 /* release file pointer to gem object. */
73 drm_gem_object_release(obj);
74
75 kfree(exynos_gem_obj);
76 exynos_gem_obj = NULL;
77 }
78
79 unsigned long exynos_drm_gem_get_size(struct drm_device *dev,
80 unsigned int gem_handle,
81 struct drm_file *file_priv)
82 {
83 struct exynos_drm_gem_obj *exynos_gem_obj;
84 struct drm_gem_object *obj;
85
86 obj = drm_gem_object_lookup(dev, file_priv, gem_handle);
87 if (!obj) {
88 DRM_ERROR("failed to lookup gem object.\n");
89 return 0;
90 }
91
92 exynos_gem_obj = to_exynos_gem_obj(obj);
93
94 drm_gem_object_unreference_unlocked(obj);
95
96 return exynos_gem_obj->buffer->size;
97 }
98
99
100 struct exynos_drm_gem_obj *exynos_drm_gem_init(struct drm_device *dev,
101 unsigned long size)
102 {
103 struct exynos_drm_gem_obj *exynos_gem_obj;
104 struct drm_gem_object *obj;
105 int ret;
106
107 exynos_gem_obj = kzalloc(sizeof(*exynos_gem_obj), GFP_KERNEL);
108 if (!exynos_gem_obj)
109 return ERR_PTR(-ENOMEM);
110
111 exynos_gem_obj->size = size;
112 obj = &exynos_gem_obj->base;
113
114 ret = drm_gem_object_init(dev, obj, size);
115 if (ret < 0) {
116 DRM_ERROR("failed to initialize gem object\n");
117 kfree(exynos_gem_obj);
118 return ERR_PTR(ret);
119 }
120
121 DRM_DEBUG_KMS("created file object = 0x%x\n", (unsigned int)obj->filp);
122
123 return exynos_gem_obj;
124 }
125
126 struct exynos_drm_gem_obj *exynos_drm_gem_create(struct drm_device *dev,
127 unsigned int flags,
128 unsigned long size)
129 {
130 struct exynos_drm_gem_obj *exynos_gem_obj;
131 struct exynos_drm_gem_buf *buf;
132 int ret;
133
134 if (flags & ~(EXYNOS_BO_MASK)) {
135 DRM_ERROR("invalid flags.\n");
136 return ERR_PTR(-EINVAL);
137 }
138
139 if (!size) {
140 DRM_ERROR("invalid size.\n");
141 return ERR_PTR(-EINVAL);
142 }
143
144 size = roundup(size, PAGE_SIZE);
145
146 buf = exynos_drm_init_buf(dev, size);
147 if (!buf)
148 return ERR_PTR(-ENOMEM);
149
150 exynos_gem_obj = exynos_drm_gem_init(dev, size);
151 if (IS_ERR(exynos_gem_obj)) {
152 ret = PTR_ERR(exynos_gem_obj);
153 goto err_fini_buf;
154 }
155
156 exynos_gem_obj->buffer = buf;
157
158 /* set memory type and cache attribute from user side. */
159 exynos_gem_obj->flags = flags;
160
161 ret = exynos_drm_alloc_buf(dev, buf, flags);
162 if (ret < 0)
163 goto err_gem_fini;
164
165 return exynos_gem_obj;
166
167 err_gem_fini:
168 drm_gem_object_release(&exynos_gem_obj->base);
169 kfree(exynos_gem_obj);
170 err_fini_buf:
171 exynos_drm_fini_buf(dev, buf);
172 return ERR_PTR(ret);
173 }
174
175 int exynos_drm_gem_create_ioctl(struct drm_device *dev, void *data,
176 struct drm_file *file_priv)
177 {
178 struct drm_exynos_gem_create *args = data;
179 struct exynos_drm_gem_obj *exynos_gem_obj;
180 int ret;
181
182 exynos_gem_obj = exynos_drm_gem_create(dev, args->flags, args->size);
183 if (IS_ERR(exynos_gem_obj))
184 return PTR_ERR(exynos_gem_obj);
185
186 ret = exynos_drm_gem_handle_create(&exynos_gem_obj->base, file_priv,
187 &args->handle);
188 if (ret) {
189 exynos_drm_gem_destroy(exynos_gem_obj);
190 return ret;
191 }
192
193 return 0;
194 }
195
196 dma_addr_t *exynos_drm_gem_get_dma_addr(struct drm_device *dev,
197 unsigned int gem_handle,
198 struct drm_file *filp)
199 {
200 struct exynos_drm_gem_obj *exynos_gem_obj;
201 struct drm_gem_object *obj;
202
203 obj = drm_gem_object_lookup(dev, filp, gem_handle);
204 if (!obj) {
205 DRM_ERROR("failed to lookup gem object.\n");
206 return ERR_PTR(-EINVAL);
207 }
208
209 exynos_gem_obj = to_exynos_gem_obj(obj);
210
211 return &exynos_gem_obj->buffer->dma_addr;
212 }
213
214 void exynos_drm_gem_put_dma_addr(struct drm_device *dev,
215 unsigned int gem_handle,
216 struct drm_file *filp)
217 {
218 struct drm_gem_object *obj;
219
220 obj = drm_gem_object_lookup(dev, filp, gem_handle);
221 if (!obj) {
222 DRM_ERROR("failed to lookup gem object.\n");
223 return;
224 }
225
226 drm_gem_object_unreference_unlocked(obj);
227
228 /*
229 * decrease obj->refcount one more time because we has already
230 * increased it at exynos_drm_gem_get_dma_addr().
231 */
232 drm_gem_object_unreference_unlocked(obj);
233 }
234
235 int exynos_drm_gem_mmap_buffer(struct exynos_drm_gem_obj *exynos_gem_obj,
236 struct vm_area_struct *vma)
237 {
238 struct drm_device *drm_dev = exynos_gem_obj->base.dev;
239 struct exynos_drm_gem_buf *buffer;
240 unsigned long vm_size;
241 int ret;
242
243 vma->vm_flags &= ~VM_PFNMAP;
244 vma->vm_pgoff = 0;
245
246 vm_size = vma->vm_end - vma->vm_start;
247
248 /*
249 * a buffer contains information to physically continuous memory
250 * allocated by user request or at framebuffer creation.
251 */
252 buffer = exynos_gem_obj->buffer;
253
254 /* check if user-requested size is valid. */
255 if (vm_size > buffer->size)
256 return -EINVAL;
257
258 ret = dma_mmap_attrs(drm_dev->dev, vma, buffer->pages,
259 buffer->dma_addr, buffer->size,
260 &buffer->dma_attrs);
261 if (ret < 0) {
262 DRM_ERROR("failed to mmap.\n");
263 return ret;
264 }
265
266 return 0;
267 }
268
269 int exynos_drm_gem_get_ioctl(struct drm_device *dev, void *data,
270 struct drm_file *file_priv)
271 { struct exynos_drm_gem_obj *exynos_gem_obj;
272 struct drm_exynos_gem_info *args = data;
273 struct drm_gem_object *obj;
274
275 mutex_lock(&dev->struct_mutex);
276
277 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
278 if (!obj) {
279 DRM_ERROR("failed to lookup gem object.\n");
280 mutex_unlock(&dev->struct_mutex);
281 return -EINVAL;
282 }
283
284 exynos_gem_obj = to_exynos_gem_obj(obj);
285
286 args->flags = exynos_gem_obj->flags;
287 args->size = exynos_gem_obj->size;
288
289 drm_gem_object_unreference(obj);
290 mutex_unlock(&dev->struct_mutex);
291
292 return 0;
293 }
294
295 struct vm_area_struct *exynos_gem_get_vma(struct vm_area_struct *vma)
296 {
297 struct vm_area_struct *vma_copy;
298
299 vma_copy = kmalloc(sizeof(*vma_copy), GFP_KERNEL);
300 if (!vma_copy)
301 return NULL;
302
303 if (vma->vm_ops && vma->vm_ops->open)
304 vma->vm_ops->open(vma);
305
306 if (vma->vm_file)
307 get_file(vma->vm_file);
308
309 memcpy(vma_copy, vma, sizeof(*vma));
310
311 vma_copy->vm_mm = NULL;
312 vma_copy->vm_next = NULL;
313 vma_copy->vm_prev = NULL;
314
315 return vma_copy;
316 }
317
318 void exynos_gem_put_vma(struct vm_area_struct *vma)
319 {
320 if (!vma)
321 return;
322
323 if (vma->vm_ops && vma->vm_ops->close)
324 vma->vm_ops->close(vma);
325
326 if (vma->vm_file)
327 fput(vma->vm_file);
328
329 kfree(vma);
330 }
331
332 int exynos_gem_get_pages_from_userptr(unsigned long start,
333 unsigned int npages,
334 struct page **pages,
335 struct vm_area_struct *vma)
336 {
337 int get_npages;
338
339 /* the memory region mmaped with VM_PFNMAP. */
340 if (vma_is_io(vma)) {
341 unsigned int i;
342
343 for (i = 0; i < npages; ++i, start += PAGE_SIZE) {
344 unsigned long pfn;
345 int ret = follow_pfn(vma, start, &pfn);
346 if (ret)
347 return ret;
348
349 pages[i] = pfn_to_page(pfn);
350 }
351
352 if (i != npages) {
353 DRM_ERROR("failed to get user_pages.\n");
354 return -EINVAL;
355 }
356
357 return 0;
358 }
359
360 get_npages = get_user_pages(current, current->mm, start,
361 npages, 1, 1, pages, NULL);
362 get_npages = max(get_npages, 0);
363 if (get_npages != npages) {
364 DRM_ERROR("failed to get user_pages.\n");
365 while (get_npages)
366 put_page(pages[--get_npages]);
367 return -EFAULT;
368 }
369
370 return 0;
371 }
372
373 void exynos_gem_put_pages_to_userptr(struct page **pages,
374 unsigned int npages,
375 struct vm_area_struct *vma)
376 {
377 if (!vma_is_io(vma)) {
378 unsigned int i;
379
380 for (i = 0; i < npages; i++) {
381 set_page_dirty_lock(pages[i]);
382
383 /*
384 * undo the reference we took when populating
385 * the table.
386 */
387 put_page(pages[i]);
388 }
389 }
390 }
391
392 int exynos_gem_map_sgt_with_dma(struct drm_device *drm_dev,
393 struct sg_table *sgt,
394 enum dma_data_direction dir)
395 {
396 int nents;
397
398 mutex_lock(&drm_dev->struct_mutex);
399
400 nents = dma_map_sg(drm_dev->dev, sgt->sgl, sgt->nents, dir);
401 if (!nents) {
402 DRM_ERROR("failed to map sgl with dma.\n");
403 mutex_unlock(&drm_dev->struct_mutex);
404 return nents;
405 }
406
407 mutex_unlock(&drm_dev->struct_mutex);
408 return 0;
409 }
410
411 void exynos_gem_unmap_sgt_from_dma(struct drm_device *drm_dev,
412 struct sg_table *sgt,
413 enum dma_data_direction dir)
414 {
415 dma_unmap_sg(drm_dev->dev, sgt->sgl, sgt->nents, dir);
416 }
417
418 void exynos_drm_gem_free_object(struct drm_gem_object *obj)
419 {
420 struct exynos_drm_gem_obj *exynos_gem_obj;
421 struct exynos_drm_gem_buf *buf;
422
423 exynos_gem_obj = to_exynos_gem_obj(obj);
424 buf = exynos_gem_obj->buffer;
425
426 exynos_drm_gem_destroy(to_exynos_gem_obj(obj));
427 }
428
429 int exynos_drm_gem_dumb_create(struct drm_file *file_priv,
430 struct drm_device *dev,
431 struct drm_mode_create_dumb *args)
432 {
433 struct exynos_drm_gem_obj *exynos_gem_obj;
434 int ret;
435
436 /*
437 * allocate memory to be used for framebuffer.
438 * - this callback would be called by user application
439 * with DRM_IOCTL_MODE_CREATE_DUMB command.
440 */
441
442 args->pitch = args->width * ((args->bpp + 7) / 8);
443 args->size = args->pitch * args->height;
444
445 if (is_drm_iommu_supported(dev)) {
446 exynos_gem_obj = exynos_drm_gem_create(dev,
447 EXYNOS_BO_NONCONTIG | EXYNOS_BO_WC,
448 args->size);
449 } else {
450 exynos_gem_obj = exynos_drm_gem_create(dev,
451 EXYNOS_BO_CONTIG | EXYNOS_BO_WC,
452 args->size);
453 }
454
455 if (IS_ERR(exynos_gem_obj)) {
456 dev_warn(dev->dev, "FB allocation failed.\n");
457 return PTR_ERR(exynos_gem_obj);
458 }
459
460 ret = exynos_drm_gem_handle_create(&exynos_gem_obj->base, file_priv,
461 &args->handle);
462 if (ret) {
463 exynos_drm_gem_destroy(exynos_gem_obj);
464 return ret;
465 }
466
467 return 0;
468 }
469
470 int exynos_drm_gem_dumb_map_offset(struct drm_file *file_priv,
471 struct drm_device *dev, uint32_t handle,
472 uint64_t *offset)
473 {
474 struct drm_gem_object *obj;
475 int ret = 0;
476
477 mutex_lock(&dev->struct_mutex);
478
479 /*
480 * get offset of memory allocated for drm framebuffer.
481 * - this callback would be called by user application
482 * with DRM_IOCTL_MODE_MAP_DUMB command.
483 */
484
485 obj = drm_gem_object_lookup(dev, file_priv, handle);
486 if (!obj) {
487 DRM_ERROR("failed to lookup gem object.\n");
488 ret = -EINVAL;
489 goto unlock;
490 }
491
492 ret = drm_gem_create_mmap_offset(obj);
493 if (ret)
494 goto out;
495
496 *offset = drm_vma_node_offset_addr(&obj->vma_node);
497 DRM_DEBUG_KMS("offset = 0x%lx\n", (unsigned long)*offset);
498
499 out:
500 drm_gem_object_unreference(obj);
501 unlock:
502 mutex_unlock(&dev->struct_mutex);
503 return ret;
504 }
505
506 int exynos_drm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
507 {
508 struct drm_gem_object *obj = vma->vm_private_data;
509 struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
510 struct exynos_drm_gem_buf *buf = exynos_gem_obj->buffer;
511 unsigned long pfn;
512 pgoff_t page_offset;
513 int ret;
514
515 page_offset = ((unsigned long)vmf->virtual_address -
516 vma->vm_start) >> PAGE_SHIFT;
517
518 if (page_offset >= (buf->size >> PAGE_SHIFT)) {
519 DRM_ERROR("invalid page offset\n");
520 ret = -EINVAL;
521 goto out;
522 }
523
524 pfn = page_to_pfn(buf->pages[page_offset]);
525 ret = vm_insert_mixed(vma, (unsigned long)vmf->virtual_address, pfn);
526
527 out:
528 switch (ret) {
529 case 0:
530 case -ERESTARTSYS:
531 case -EINTR:
532 return VM_FAULT_NOPAGE;
533 case -ENOMEM:
534 return VM_FAULT_OOM;
535 default:
536 return VM_FAULT_SIGBUS;
537 }
538 }
539
540 int exynos_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
541 {
542 struct exynos_drm_gem_obj *exynos_gem_obj;
543 struct drm_gem_object *obj;
544 int ret;
545
546 /* set vm_area_struct. */
547 ret = drm_gem_mmap(filp, vma);
548 if (ret < 0) {
549 DRM_ERROR("failed to mmap.\n");
550 return ret;
551 }
552
553 obj = vma->vm_private_data;
554 exynos_gem_obj = to_exynos_gem_obj(obj);
555
556 DRM_DEBUG_KMS("flags = 0x%x\n", exynos_gem_obj->flags);
557
558 /* non-cachable as default. */
559 if (exynos_gem_obj->flags & EXYNOS_BO_CACHABLE)
560 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
561 else if (exynos_gem_obj->flags & EXYNOS_BO_WC)
562 vma->vm_page_prot =
563 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
564 else
565 vma->vm_page_prot =
566 pgprot_noncached(vm_get_page_prot(vma->vm_flags));
567
568 ret = exynos_drm_gem_mmap_buffer(exynos_gem_obj, vma);
569 if (ret)
570 goto err_close_vm;
571
572 return ret;
573
574 err_close_vm:
575 drm_gem_vm_close(vma);
576 drm_gem_free_mmap_offset(obj);
577
578 return ret;
579 }
This page took 0.04241 seconds and 5 git commands to generate.