Linux 4.9.243
[linux/fpc-iii.git] / drivers / gpu / drm / exynos / exynos_drm_gem.c
blob2abc47b554ab8456f866be5ef5de3d11f85577bc
1 /* exynos_drm_gem.c
3 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4 * Author: Inki Dae <inki.dae@samsung.com>
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.
12 #include <drm/drmP.h>
13 #include <drm/drm_vma_manager.h>
15 #include <linux/shmem_fs.h>
16 #include <linux/dma-buf.h>
17 #include <linux/pfn_t.h>
18 #include <drm/exynos_drm.h>
20 #include "exynos_drm_drv.h"
21 #include "exynos_drm_gem.h"
22 #include "exynos_drm_iommu.h"
24 static int exynos_drm_alloc_buf(struct exynos_drm_gem *exynos_gem)
26 struct drm_device *dev = exynos_gem->base.dev;
27 unsigned long attr;
28 unsigned int nr_pages;
29 struct sg_table sgt;
30 int ret = -ENOMEM;
32 if (exynos_gem->dma_addr) {
33 DRM_DEBUG_KMS("already allocated.\n");
34 return 0;
37 exynos_gem->dma_attrs = 0;
40 * if EXYNOS_BO_CONTIG, fully physically contiguous memory
41 * region will be allocated else physically contiguous
42 * as possible.
44 if (!(exynos_gem->flags & EXYNOS_BO_NONCONTIG))
45 exynos_gem->dma_attrs |= DMA_ATTR_FORCE_CONTIGUOUS;
48 * if EXYNOS_BO_WC or EXYNOS_BO_NONCACHABLE, writecombine mapping
49 * else cachable mapping.
51 if (exynos_gem->flags & EXYNOS_BO_WC ||
52 !(exynos_gem->flags & EXYNOS_BO_CACHABLE))
53 attr = DMA_ATTR_WRITE_COMBINE;
54 else
55 attr = DMA_ATTR_NON_CONSISTENT;
57 exynos_gem->dma_attrs |= attr;
58 exynos_gem->dma_attrs |= DMA_ATTR_NO_KERNEL_MAPPING;
60 nr_pages = exynos_gem->size >> PAGE_SHIFT;
62 exynos_gem->pages = drm_calloc_large(nr_pages, sizeof(struct page *));
63 if (!exynos_gem->pages) {
64 DRM_ERROR("failed to allocate pages.\n");
65 return -ENOMEM;
68 exynos_gem->cookie = dma_alloc_attrs(to_dma_dev(dev), exynos_gem->size,
69 &exynos_gem->dma_addr, GFP_KERNEL,
70 exynos_gem->dma_attrs);
71 if (!exynos_gem->cookie) {
72 DRM_ERROR("failed to allocate buffer.\n");
73 goto err_free;
76 ret = dma_get_sgtable_attrs(to_dma_dev(dev), &sgt, exynos_gem->cookie,
77 exynos_gem->dma_addr, exynos_gem->size,
78 exynos_gem->dma_attrs);
79 if (ret < 0) {
80 DRM_ERROR("failed to get sgtable.\n");
81 goto err_dma_free;
84 if (drm_prime_sg_to_page_addr_arrays(&sgt, exynos_gem->pages, NULL,
85 nr_pages)) {
86 DRM_ERROR("invalid sgtable.\n");
87 ret = -EINVAL;
88 goto err_sgt_free;
91 sg_free_table(&sgt);
93 DRM_DEBUG_KMS("dma_addr(0x%lx), size(0x%lx)\n",
94 (unsigned long)exynos_gem->dma_addr, exynos_gem->size);
96 return 0;
98 err_sgt_free:
99 sg_free_table(&sgt);
100 err_dma_free:
101 dma_free_attrs(to_dma_dev(dev), exynos_gem->size, exynos_gem->cookie,
102 exynos_gem->dma_addr, exynos_gem->dma_attrs);
103 err_free:
104 drm_free_large(exynos_gem->pages);
106 return ret;
109 static void exynos_drm_free_buf(struct exynos_drm_gem *exynos_gem)
111 struct drm_device *dev = exynos_gem->base.dev;
113 if (!exynos_gem->dma_addr) {
114 DRM_DEBUG_KMS("dma_addr is invalid.\n");
115 return;
118 DRM_DEBUG_KMS("dma_addr(0x%lx), size(0x%lx)\n",
119 (unsigned long)exynos_gem->dma_addr, exynos_gem->size);
121 dma_free_attrs(to_dma_dev(dev), exynos_gem->size, exynos_gem->cookie,
122 (dma_addr_t)exynos_gem->dma_addr,
123 exynos_gem->dma_attrs);
125 drm_free_large(exynos_gem->pages);
128 static int exynos_drm_gem_handle_create(struct drm_gem_object *obj,
129 struct drm_file *file_priv,
130 unsigned int *handle)
132 int ret;
135 * allocate a id of idr table where the obj is registered
136 * and handle has the id what user can see.
138 ret = drm_gem_handle_create(file_priv, obj, handle);
139 if (ret)
140 return ret;
142 DRM_DEBUG_KMS("gem handle = 0x%x\n", *handle);
144 /* drop reference from allocate - handle holds it now. */
145 drm_gem_object_unreference_unlocked(obj);
147 return 0;
150 void exynos_drm_gem_destroy(struct exynos_drm_gem *exynos_gem)
152 struct drm_gem_object *obj = &exynos_gem->base;
154 DRM_DEBUG_KMS("handle count = %d\n", obj->handle_count);
157 * do not release memory region from exporter.
159 * the region will be released by exporter
160 * once dmabuf's refcount becomes 0.
162 if (obj->import_attach)
163 drm_prime_gem_destroy(obj, exynos_gem->sgt);
164 else
165 exynos_drm_free_buf(exynos_gem);
167 /* release file pointer to gem object. */
168 drm_gem_object_release(obj);
170 kfree(exynos_gem);
173 unsigned long exynos_drm_gem_get_size(struct drm_device *dev,
174 unsigned int gem_handle,
175 struct drm_file *file_priv)
177 struct exynos_drm_gem *exynos_gem;
178 struct drm_gem_object *obj;
180 obj = drm_gem_object_lookup(file_priv, gem_handle);
181 if (!obj) {
182 DRM_ERROR("failed to lookup gem object.\n");
183 return 0;
186 exynos_gem = to_exynos_gem(obj);
188 drm_gem_object_unreference_unlocked(obj);
190 return exynos_gem->size;
193 static struct exynos_drm_gem *exynos_drm_gem_init(struct drm_device *dev,
194 unsigned long size)
196 struct exynos_drm_gem *exynos_gem;
197 struct drm_gem_object *obj;
198 int ret;
200 exynos_gem = kzalloc(sizeof(*exynos_gem), GFP_KERNEL);
201 if (!exynos_gem)
202 return ERR_PTR(-ENOMEM);
204 exynos_gem->size = size;
205 obj = &exynos_gem->base;
207 ret = drm_gem_object_init(dev, obj, size);
208 if (ret < 0) {
209 DRM_ERROR("failed to initialize gem object\n");
210 kfree(exynos_gem);
211 return ERR_PTR(ret);
214 ret = drm_gem_create_mmap_offset(obj);
215 if (ret < 0) {
216 drm_gem_object_release(obj);
217 kfree(exynos_gem);
218 return ERR_PTR(ret);
221 DRM_DEBUG_KMS("created file object = %p\n", obj->filp);
223 return exynos_gem;
226 struct exynos_drm_gem *exynos_drm_gem_create(struct drm_device *dev,
227 unsigned int flags,
228 unsigned long size)
230 struct exynos_drm_gem *exynos_gem;
231 int ret;
233 if (flags & ~(EXYNOS_BO_MASK)) {
234 DRM_ERROR("invalid flags.\n");
235 return ERR_PTR(-EINVAL);
238 if (!size) {
239 DRM_ERROR("invalid size.\n");
240 return ERR_PTR(-EINVAL);
243 size = roundup(size, PAGE_SIZE);
245 exynos_gem = exynos_drm_gem_init(dev, size);
246 if (IS_ERR(exynos_gem))
247 return exynos_gem;
249 if (!is_drm_iommu_supported(dev) && (flags & EXYNOS_BO_NONCONTIG)) {
251 * when no IOMMU is available, all allocated buffers are
252 * contiguous anyway, so drop EXYNOS_BO_NONCONTIG flag
254 flags &= ~EXYNOS_BO_NONCONTIG;
255 DRM_WARN("Non-contiguous allocation is not supported without IOMMU, falling back to contiguous buffer\n");
258 /* set memory type and cache attribute from user side. */
259 exynos_gem->flags = flags;
261 ret = exynos_drm_alloc_buf(exynos_gem);
262 if (ret < 0) {
263 drm_gem_object_release(&exynos_gem->base);
264 kfree(exynos_gem);
265 return ERR_PTR(ret);
268 return exynos_gem;
271 int exynos_drm_gem_create_ioctl(struct drm_device *dev, void *data,
272 struct drm_file *file_priv)
274 struct drm_exynos_gem_create *args = data;
275 struct exynos_drm_gem *exynos_gem;
276 int ret;
278 exynos_gem = exynos_drm_gem_create(dev, args->flags, args->size);
279 if (IS_ERR(exynos_gem))
280 return PTR_ERR(exynos_gem);
282 ret = exynos_drm_gem_handle_create(&exynos_gem->base, file_priv,
283 &args->handle);
284 if (ret) {
285 exynos_drm_gem_destroy(exynos_gem);
286 return ret;
289 return 0;
292 int exynos_drm_gem_map_ioctl(struct drm_device *dev, void *data,
293 struct drm_file *file_priv)
295 struct drm_exynos_gem_map *args = data;
297 return exynos_drm_gem_dumb_map_offset(file_priv, dev, args->handle,
298 &args->offset);
301 dma_addr_t *exynos_drm_gem_get_dma_addr(struct drm_device *dev,
302 unsigned int gem_handle,
303 struct drm_file *filp)
305 struct exynos_drm_gem *exynos_gem;
306 struct drm_gem_object *obj;
308 obj = drm_gem_object_lookup(filp, gem_handle);
309 if (!obj) {
310 DRM_ERROR("failed to lookup gem object.\n");
311 return ERR_PTR(-EINVAL);
314 exynos_gem = to_exynos_gem(obj);
316 return &exynos_gem->dma_addr;
319 void exynos_drm_gem_put_dma_addr(struct drm_device *dev,
320 unsigned int gem_handle,
321 struct drm_file *filp)
323 struct drm_gem_object *obj;
325 obj = drm_gem_object_lookup(filp, gem_handle);
326 if (!obj) {
327 DRM_ERROR("failed to lookup gem object.\n");
328 return;
331 drm_gem_object_unreference_unlocked(obj);
334 * decrease obj->refcount one more time because we has already
335 * increased it at exynos_drm_gem_get_dma_addr().
337 drm_gem_object_unreference_unlocked(obj);
340 static int exynos_drm_gem_mmap_buffer(struct exynos_drm_gem *exynos_gem,
341 struct vm_area_struct *vma)
343 struct drm_device *drm_dev = exynos_gem->base.dev;
344 unsigned long vm_size;
345 int ret;
347 vma->vm_flags &= ~VM_PFNMAP;
348 vma->vm_pgoff = 0;
350 vm_size = vma->vm_end - vma->vm_start;
352 /* check if user-requested size is valid. */
353 if (vm_size > exynos_gem->size)
354 return -EINVAL;
356 ret = dma_mmap_attrs(to_dma_dev(drm_dev), vma, exynos_gem->cookie,
357 exynos_gem->dma_addr, exynos_gem->size,
358 exynos_gem->dma_attrs);
359 if (ret < 0) {
360 DRM_ERROR("failed to mmap.\n");
361 return ret;
364 return 0;
367 int exynos_drm_gem_get_ioctl(struct drm_device *dev, void *data,
368 struct drm_file *file_priv)
370 struct exynos_drm_gem *exynos_gem;
371 struct drm_exynos_gem_info *args = data;
372 struct drm_gem_object *obj;
374 obj = drm_gem_object_lookup(file_priv, args->handle);
375 if (!obj) {
376 DRM_ERROR("failed to lookup gem object.\n");
377 return -EINVAL;
380 exynos_gem = to_exynos_gem(obj);
382 args->flags = exynos_gem->flags;
383 args->size = exynos_gem->size;
385 drm_gem_object_unreference_unlocked(obj);
387 return 0;
390 void exynos_drm_gem_free_object(struct drm_gem_object *obj)
392 exynos_drm_gem_destroy(to_exynos_gem(obj));
395 int exynos_drm_gem_dumb_create(struct drm_file *file_priv,
396 struct drm_device *dev,
397 struct drm_mode_create_dumb *args)
399 struct exynos_drm_gem *exynos_gem;
400 unsigned int flags;
401 int ret;
404 * allocate memory to be used for framebuffer.
405 * - this callback would be called by user application
406 * with DRM_IOCTL_MODE_CREATE_DUMB command.
409 args->pitch = args->width * ((args->bpp + 7) / 8);
410 args->size = args->pitch * args->height;
412 if (is_drm_iommu_supported(dev))
413 flags = EXYNOS_BO_NONCONTIG | EXYNOS_BO_WC;
414 else
415 flags = EXYNOS_BO_CONTIG | EXYNOS_BO_WC;
417 exynos_gem = exynos_drm_gem_create(dev, flags, args->size);
418 if (IS_ERR(exynos_gem)) {
419 dev_warn(dev->dev, "FB allocation failed.\n");
420 return PTR_ERR(exynos_gem);
423 ret = exynos_drm_gem_handle_create(&exynos_gem->base, file_priv,
424 &args->handle);
425 if (ret) {
426 exynos_drm_gem_destroy(exynos_gem);
427 return ret;
430 return 0;
433 int exynos_drm_gem_dumb_map_offset(struct drm_file *file_priv,
434 struct drm_device *dev, uint32_t handle,
435 uint64_t *offset)
437 struct drm_gem_object *obj;
438 int ret = 0;
441 * get offset of memory allocated for drm framebuffer.
442 * - this callback would be called by user application
443 * with DRM_IOCTL_MODE_MAP_DUMB command.
446 obj = drm_gem_object_lookup(file_priv, handle);
447 if (!obj) {
448 DRM_ERROR("failed to lookup gem object.\n");
449 return -EINVAL;
452 *offset = drm_vma_node_offset_addr(&obj->vma_node);
453 DRM_DEBUG_KMS("offset = 0x%lx\n", (unsigned long)*offset);
455 drm_gem_object_unreference_unlocked(obj);
456 return ret;
459 int exynos_drm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
461 struct drm_gem_object *obj = vma->vm_private_data;
462 struct exynos_drm_gem *exynos_gem = to_exynos_gem(obj);
463 unsigned long pfn;
464 pgoff_t page_offset;
465 int ret;
467 page_offset = ((unsigned long)vmf->virtual_address -
468 vma->vm_start) >> PAGE_SHIFT;
470 if (page_offset >= (exynos_gem->size >> PAGE_SHIFT)) {
471 DRM_ERROR("invalid page offset\n");
472 ret = -EINVAL;
473 goto out;
476 pfn = page_to_pfn(exynos_gem->pages[page_offset]);
477 ret = vm_insert_mixed(vma, (unsigned long)vmf->virtual_address,
478 __pfn_to_pfn_t(pfn, PFN_DEV));
480 out:
481 switch (ret) {
482 case 0:
483 case -ERESTARTSYS:
484 case -EINTR:
485 return VM_FAULT_NOPAGE;
486 case -ENOMEM:
487 return VM_FAULT_OOM;
488 default:
489 return VM_FAULT_SIGBUS;
493 static int exynos_drm_gem_mmap_obj(struct drm_gem_object *obj,
494 struct vm_area_struct *vma)
496 struct exynos_drm_gem *exynos_gem = to_exynos_gem(obj);
497 int ret;
499 DRM_DEBUG_KMS("flags = 0x%x\n", exynos_gem->flags);
501 /* non-cachable as default. */
502 if (exynos_gem->flags & EXYNOS_BO_CACHABLE)
503 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
504 else if (exynos_gem->flags & EXYNOS_BO_WC)
505 vma->vm_page_prot =
506 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
507 else
508 vma->vm_page_prot =
509 pgprot_noncached(vm_get_page_prot(vma->vm_flags));
511 ret = exynos_drm_gem_mmap_buffer(exynos_gem, vma);
512 if (ret)
513 goto err_close_vm;
515 return ret;
517 err_close_vm:
518 drm_gem_vm_close(vma);
520 return ret;
523 int exynos_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
525 struct drm_gem_object *obj;
526 int ret;
528 /* set vm_area_struct. */
529 ret = drm_gem_mmap(filp, vma);
530 if (ret < 0) {
531 DRM_ERROR("failed to mmap.\n");
532 return ret;
535 obj = vma->vm_private_data;
537 if (obj->import_attach)
538 return dma_buf_mmap(obj->dma_buf, vma, 0);
540 return exynos_drm_gem_mmap_obj(obj, vma);
543 /* low-level interface prime helpers */
544 struct sg_table *exynos_drm_gem_prime_get_sg_table(struct drm_gem_object *obj)
546 struct exynos_drm_gem *exynos_gem = to_exynos_gem(obj);
547 int npages;
549 npages = exynos_gem->size >> PAGE_SHIFT;
551 return drm_prime_pages_to_sg(exynos_gem->pages, npages);
554 struct drm_gem_object *
555 exynos_drm_gem_prime_import_sg_table(struct drm_device *dev,
556 struct dma_buf_attachment *attach,
557 struct sg_table *sgt)
559 struct exynos_drm_gem *exynos_gem;
560 int npages;
561 int ret;
563 exynos_gem = exynos_drm_gem_init(dev, attach->dmabuf->size);
564 if (IS_ERR(exynos_gem)) {
565 ret = PTR_ERR(exynos_gem);
566 return ERR_PTR(ret);
569 exynos_gem->dma_addr = sg_dma_address(sgt->sgl);
571 npages = exynos_gem->size >> PAGE_SHIFT;
572 exynos_gem->pages = drm_malloc_ab(npages, sizeof(struct page *));
573 if (!exynos_gem->pages) {
574 ret = -ENOMEM;
575 goto err;
578 ret = drm_prime_sg_to_page_addr_arrays(sgt, exynos_gem->pages, NULL,
579 npages);
580 if (ret < 0)
581 goto err_free_large;
583 exynos_gem->sgt = sgt;
585 if (sgt->nents == 1) {
586 /* always physically continuous memory if sgt->nents is 1. */
587 exynos_gem->flags |= EXYNOS_BO_CONTIG;
588 } else {
590 * this case could be CONTIG or NONCONTIG type but for now
591 * sets NONCONTIG.
592 * TODO. we have to find a way that exporter can notify
593 * the type of its own buffer to importer.
595 exynos_gem->flags |= EXYNOS_BO_NONCONTIG;
598 return &exynos_gem->base;
600 err_free_large:
601 drm_free_large(exynos_gem->pages);
602 err:
603 drm_gem_object_release(&exynos_gem->base);
604 kfree(exynos_gem);
605 return ERR_PTR(ret);
608 void *exynos_drm_gem_prime_vmap(struct drm_gem_object *obj)
610 return NULL;
613 void exynos_drm_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
615 /* Nothing to do */
618 int exynos_drm_gem_prime_mmap(struct drm_gem_object *obj,
619 struct vm_area_struct *vma)
621 int ret;
623 ret = drm_gem_mmap_obj(obj, obj->size, vma);
624 if (ret < 0)
625 return ret;
627 return exynos_drm_gem_mmap_obj(obj, vma);