rcutorture: Eliminate unused ts_rem local from rcu_trace_clock_local()
[linux/fpc-iii.git] / drivers / gpu / drm / exynos / exynos_drm_gem.c
blobc23479be48500091fcb7aa0e0bdaf962a1f9fb59
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 = kvmalloc_array(nr_pages, sizeof(struct page *),
63 GFP_KERNEL | __GFP_ZERO);
64 if (!exynos_gem->pages) {
65 DRM_ERROR("failed to allocate pages.\n");
66 return -ENOMEM;
69 exynos_gem->cookie = dma_alloc_attrs(to_dma_dev(dev), exynos_gem->size,
70 &exynos_gem->dma_addr, GFP_KERNEL,
71 exynos_gem->dma_attrs);
72 if (!exynos_gem->cookie) {
73 DRM_ERROR("failed to allocate buffer.\n");
74 goto err_free;
77 ret = dma_get_sgtable_attrs(to_dma_dev(dev), &sgt, exynos_gem->cookie,
78 exynos_gem->dma_addr, exynos_gem->size,
79 exynos_gem->dma_attrs);
80 if (ret < 0) {
81 DRM_ERROR("failed to get sgtable.\n");
82 goto err_dma_free;
85 if (drm_prime_sg_to_page_addr_arrays(&sgt, exynos_gem->pages, NULL,
86 nr_pages)) {
87 DRM_ERROR("invalid sgtable.\n");
88 ret = -EINVAL;
89 goto err_sgt_free;
92 sg_free_table(&sgt);
94 DRM_DEBUG_KMS("dma_addr(0x%lx), size(0x%lx)\n",
95 (unsigned long)exynos_gem->dma_addr, exynos_gem->size);
97 return 0;
99 err_sgt_free:
100 sg_free_table(&sgt);
101 err_dma_free:
102 dma_free_attrs(to_dma_dev(dev), exynos_gem->size, exynos_gem->cookie,
103 exynos_gem->dma_addr, exynos_gem->dma_attrs);
104 err_free:
105 kvfree(exynos_gem->pages);
107 return ret;
110 static void exynos_drm_free_buf(struct exynos_drm_gem *exynos_gem)
112 struct drm_device *dev = exynos_gem->base.dev;
114 if (!exynos_gem->dma_addr) {
115 DRM_DEBUG_KMS("dma_addr is invalid.\n");
116 return;
119 DRM_DEBUG_KMS("dma_addr(0x%lx), size(0x%lx)\n",
120 (unsigned long)exynos_gem->dma_addr, exynos_gem->size);
122 dma_free_attrs(to_dma_dev(dev), exynos_gem->size, exynos_gem->cookie,
123 (dma_addr_t)exynos_gem->dma_addr,
124 exynos_gem->dma_attrs);
126 kvfree(exynos_gem->pages);
129 static int exynos_drm_gem_handle_create(struct drm_gem_object *obj,
130 struct drm_file *file_priv,
131 unsigned int *handle)
133 int ret;
136 * allocate a id of idr table where the obj is registered
137 * and handle has the id what user can see.
139 ret = drm_gem_handle_create(file_priv, obj, handle);
140 if (ret)
141 return ret;
143 DRM_DEBUG_KMS("gem handle = 0x%x\n", *handle);
145 /* drop reference from allocate - handle holds it now. */
146 drm_gem_object_unreference_unlocked(obj);
148 return 0;
151 void exynos_drm_gem_destroy(struct exynos_drm_gem *exynos_gem)
153 struct drm_gem_object *obj = &exynos_gem->base;
155 DRM_DEBUG_KMS("handle count = %d\n", obj->handle_count);
158 * do not release memory region from exporter.
160 * the region will be released by exporter
161 * once dmabuf's refcount becomes 0.
163 if (obj->import_attach)
164 drm_prime_gem_destroy(obj, exynos_gem->sgt);
165 else
166 exynos_drm_free_buf(exynos_gem);
168 /* release file pointer to gem object. */
169 drm_gem_object_release(obj);
171 kfree(exynos_gem);
174 unsigned long exynos_drm_gem_get_size(struct drm_device *dev,
175 unsigned int gem_handle,
176 struct drm_file *file_priv)
178 struct exynos_drm_gem *exynos_gem;
179 struct drm_gem_object *obj;
181 obj = drm_gem_object_lookup(file_priv, gem_handle);
182 if (!obj) {
183 DRM_ERROR("failed to lookup gem object.\n");
184 return 0;
187 exynos_gem = to_exynos_gem(obj);
189 drm_gem_object_unreference_unlocked(obj);
191 return exynos_gem->size;
194 static struct exynos_drm_gem *exynos_drm_gem_init(struct drm_device *dev,
195 unsigned long size)
197 struct exynos_drm_gem *exynos_gem;
198 struct drm_gem_object *obj;
199 int ret;
201 exynos_gem = kzalloc(sizeof(*exynos_gem), GFP_KERNEL);
202 if (!exynos_gem)
203 return ERR_PTR(-ENOMEM);
205 exynos_gem->size = size;
206 obj = &exynos_gem->base;
208 ret = drm_gem_object_init(dev, obj, size);
209 if (ret < 0) {
210 DRM_ERROR("failed to initialize gem object\n");
211 kfree(exynos_gem);
212 return ERR_PTR(ret);
215 ret = drm_gem_create_mmap_offset(obj);
216 if (ret < 0) {
217 drm_gem_object_release(obj);
218 kfree(exynos_gem);
219 return ERR_PTR(ret);
222 DRM_DEBUG_KMS("created file object = %pK\n", obj->filp);
224 return exynos_gem;
227 struct exynos_drm_gem *exynos_drm_gem_create(struct drm_device *dev,
228 unsigned int flags,
229 unsigned long size)
231 struct exynos_drm_gem *exynos_gem;
232 int ret;
234 if (flags & ~(EXYNOS_BO_MASK)) {
235 DRM_ERROR("invalid GEM buffer flags: %u\n", flags);
236 return ERR_PTR(-EINVAL);
239 if (!size) {
240 DRM_ERROR("invalid GEM buffer size: %lu\n", size);
241 return ERR_PTR(-EINVAL);
244 size = roundup(size, PAGE_SIZE);
246 exynos_gem = exynos_drm_gem_init(dev, size);
247 if (IS_ERR(exynos_gem))
248 return exynos_gem;
250 /* set memory type and cache attribute from user side. */
251 exynos_gem->flags = flags;
253 ret = exynos_drm_alloc_buf(exynos_gem);
254 if (ret < 0) {
255 drm_gem_object_release(&exynos_gem->base);
256 kfree(exynos_gem);
257 return ERR_PTR(ret);
260 return exynos_gem;
263 int exynos_drm_gem_create_ioctl(struct drm_device *dev, void *data,
264 struct drm_file *file_priv)
266 struct drm_exynos_gem_create *args = data;
267 struct exynos_drm_gem *exynos_gem;
268 int ret;
270 exynos_gem = exynos_drm_gem_create(dev, args->flags, args->size);
271 if (IS_ERR(exynos_gem))
272 return PTR_ERR(exynos_gem);
274 ret = exynos_drm_gem_handle_create(&exynos_gem->base, file_priv,
275 &args->handle);
276 if (ret) {
277 exynos_drm_gem_destroy(exynos_gem);
278 return ret;
281 return 0;
284 int exynos_drm_gem_map_ioctl(struct drm_device *dev, void *data,
285 struct drm_file *file_priv)
287 struct drm_exynos_gem_map *args = data;
289 return exynos_drm_gem_dumb_map_offset(file_priv, dev, args->handle,
290 &args->offset);
293 dma_addr_t *exynos_drm_gem_get_dma_addr(struct drm_device *dev,
294 unsigned int gem_handle,
295 struct drm_file *filp)
297 struct exynos_drm_gem *exynos_gem;
298 struct drm_gem_object *obj;
300 obj = drm_gem_object_lookup(filp, gem_handle);
301 if (!obj) {
302 DRM_ERROR("failed to lookup gem object.\n");
303 return ERR_PTR(-EINVAL);
306 exynos_gem = to_exynos_gem(obj);
308 return &exynos_gem->dma_addr;
311 void exynos_drm_gem_put_dma_addr(struct drm_device *dev,
312 unsigned int gem_handle,
313 struct drm_file *filp)
315 struct drm_gem_object *obj;
317 obj = drm_gem_object_lookup(filp, gem_handle);
318 if (!obj) {
319 DRM_ERROR("failed to lookup gem object.\n");
320 return;
323 drm_gem_object_unreference_unlocked(obj);
326 * decrease obj->refcount one more time because we has already
327 * increased it at exynos_drm_gem_get_dma_addr().
329 drm_gem_object_unreference_unlocked(obj);
332 static int exynos_drm_gem_mmap_buffer(struct exynos_drm_gem *exynos_gem,
333 struct vm_area_struct *vma)
335 struct drm_device *drm_dev = exynos_gem->base.dev;
336 unsigned long vm_size;
337 int ret;
339 vma->vm_flags &= ~VM_PFNMAP;
340 vma->vm_pgoff = 0;
342 vm_size = vma->vm_end - vma->vm_start;
344 /* check if user-requested size is valid. */
345 if (vm_size > exynos_gem->size)
346 return -EINVAL;
348 ret = dma_mmap_attrs(to_dma_dev(drm_dev), vma, exynos_gem->cookie,
349 exynos_gem->dma_addr, exynos_gem->size,
350 exynos_gem->dma_attrs);
351 if (ret < 0) {
352 DRM_ERROR("failed to mmap.\n");
353 return ret;
356 return 0;
359 int exynos_drm_gem_get_ioctl(struct drm_device *dev, void *data,
360 struct drm_file *file_priv)
362 struct exynos_drm_gem *exynos_gem;
363 struct drm_exynos_gem_info *args = data;
364 struct drm_gem_object *obj;
366 obj = drm_gem_object_lookup(file_priv, args->handle);
367 if (!obj) {
368 DRM_ERROR("failed to lookup gem object.\n");
369 return -EINVAL;
372 exynos_gem = to_exynos_gem(obj);
374 args->flags = exynos_gem->flags;
375 args->size = exynos_gem->size;
377 drm_gem_object_unreference_unlocked(obj);
379 return 0;
382 void exynos_drm_gem_free_object(struct drm_gem_object *obj)
384 exynos_drm_gem_destroy(to_exynos_gem(obj));
387 int exynos_drm_gem_dumb_create(struct drm_file *file_priv,
388 struct drm_device *dev,
389 struct drm_mode_create_dumb *args)
391 struct exynos_drm_gem *exynos_gem;
392 unsigned int flags;
393 int ret;
396 * allocate memory to be used for framebuffer.
397 * - this callback would be called by user application
398 * with DRM_IOCTL_MODE_CREATE_DUMB command.
401 args->pitch = args->width * ((args->bpp + 7) / 8);
402 args->size = args->pitch * args->height;
404 if (is_drm_iommu_supported(dev))
405 flags = EXYNOS_BO_NONCONTIG | EXYNOS_BO_WC;
406 else
407 flags = EXYNOS_BO_CONTIG | EXYNOS_BO_WC;
409 exynos_gem = exynos_drm_gem_create(dev, flags, args->size);
410 if (IS_ERR(exynos_gem)) {
411 dev_warn(dev->dev, "FB allocation failed.\n");
412 return PTR_ERR(exynos_gem);
415 ret = exynos_drm_gem_handle_create(&exynos_gem->base, file_priv,
416 &args->handle);
417 if (ret) {
418 exynos_drm_gem_destroy(exynos_gem);
419 return ret;
422 return 0;
425 int exynos_drm_gem_dumb_map_offset(struct drm_file *file_priv,
426 struct drm_device *dev, uint32_t handle,
427 uint64_t *offset)
429 struct drm_gem_object *obj;
430 int ret = 0;
433 * get offset of memory allocated for drm framebuffer.
434 * - this callback would be called by user application
435 * with DRM_IOCTL_MODE_MAP_DUMB command.
438 obj = drm_gem_object_lookup(file_priv, handle);
439 if (!obj) {
440 DRM_ERROR("failed to lookup gem object.\n");
441 return -EINVAL;
444 *offset = drm_vma_node_offset_addr(&obj->vma_node);
445 DRM_DEBUG_KMS("offset = 0x%lx\n", (unsigned long)*offset);
447 drm_gem_object_unreference_unlocked(obj);
448 return ret;
451 int exynos_drm_gem_fault(struct vm_fault *vmf)
453 struct vm_area_struct *vma = vmf->vma;
454 struct drm_gem_object *obj = vma->vm_private_data;
455 struct exynos_drm_gem *exynos_gem = to_exynos_gem(obj);
456 unsigned long pfn;
457 pgoff_t page_offset;
458 int ret;
460 page_offset = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
462 if (page_offset >= (exynos_gem->size >> PAGE_SHIFT)) {
463 DRM_ERROR("invalid page offset\n");
464 ret = -EINVAL;
465 goto out;
468 pfn = page_to_pfn(exynos_gem->pages[page_offset]);
469 ret = vm_insert_mixed(vma, vmf->address, __pfn_to_pfn_t(pfn, PFN_DEV));
471 out:
472 switch (ret) {
473 case 0:
474 case -ERESTARTSYS:
475 case -EINTR:
476 return VM_FAULT_NOPAGE;
477 case -ENOMEM:
478 return VM_FAULT_OOM;
479 default:
480 return VM_FAULT_SIGBUS;
484 static int exynos_drm_gem_mmap_obj(struct drm_gem_object *obj,
485 struct vm_area_struct *vma)
487 struct exynos_drm_gem *exynos_gem = to_exynos_gem(obj);
488 int ret;
490 DRM_DEBUG_KMS("flags = 0x%x\n", exynos_gem->flags);
492 /* non-cachable as default. */
493 if (exynos_gem->flags & EXYNOS_BO_CACHABLE)
494 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
495 else if (exynos_gem->flags & EXYNOS_BO_WC)
496 vma->vm_page_prot =
497 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
498 else
499 vma->vm_page_prot =
500 pgprot_noncached(vm_get_page_prot(vma->vm_flags));
502 ret = exynos_drm_gem_mmap_buffer(exynos_gem, vma);
503 if (ret)
504 goto err_close_vm;
506 return ret;
508 err_close_vm:
509 drm_gem_vm_close(vma);
511 return ret;
514 int exynos_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
516 struct drm_gem_object *obj;
517 int ret;
519 /* set vm_area_struct. */
520 ret = drm_gem_mmap(filp, vma);
521 if (ret < 0) {
522 DRM_ERROR("failed to mmap.\n");
523 return ret;
526 obj = vma->vm_private_data;
528 if (obj->import_attach)
529 return dma_buf_mmap(obj->dma_buf, vma, 0);
531 return exynos_drm_gem_mmap_obj(obj, vma);
534 /* low-level interface prime helpers */
535 struct sg_table *exynos_drm_gem_prime_get_sg_table(struct drm_gem_object *obj)
537 struct exynos_drm_gem *exynos_gem = to_exynos_gem(obj);
538 int npages;
540 npages = exynos_gem->size >> PAGE_SHIFT;
542 return drm_prime_pages_to_sg(exynos_gem->pages, npages);
545 struct drm_gem_object *
546 exynos_drm_gem_prime_import_sg_table(struct drm_device *dev,
547 struct dma_buf_attachment *attach,
548 struct sg_table *sgt)
550 struct exynos_drm_gem *exynos_gem;
551 int npages;
552 int ret;
554 exynos_gem = exynos_drm_gem_init(dev, attach->dmabuf->size);
555 if (IS_ERR(exynos_gem)) {
556 ret = PTR_ERR(exynos_gem);
557 return ERR_PTR(ret);
560 exynos_gem->dma_addr = sg_dma_address(sgt->sgl);
562 npages = exynos_gem->size >> PAGE_SHIFT;
563 exynos_gem->pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
564 if (!exynos_gem->pages) {
565 ret = -ENOMEM;
566 goto err;
569 ret = drm_prime_sg_to_page_addr_arrays(sgt, exynos_gem->pages, NULL,
570 npages);
571 if (ret < 0)
572 goto err_free_large;
574 exynos_gem->sgt = sgt;
576 if (sgt->nents == 1) {
577 /* always physically continuous memory if sgt->nents is 1. */
578 exynos_gem->flags |= EXYNOS_BO_CONTIG;
579 } else {
581 * this case could be CONTIG or NONCONTIG type but for now
582 * sets NONCONTIG.
583 * TODO. we have to find a way that exporter can notify
584 * the type of its own buffer to importer.
586 exynos_gem->flags |= EXYNOS_BO_NONCONTIG;
589 return &exynos_gem->base;
591 err_free_large:
592 kvfree(exynos_gem->pages);
593 err:
594 drm_gem_object_release(&exynos_gem->base);
595 kfree(exynos_gem);
596 return ERR_PTR(ret);
599 void *exynos_drm_gem_prime_vmap(struct drm_gem_object *obj)
601 return NULL;
604 void exynos_drm_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
606 /* Nothing to do */
609 int exynos_drm_gem_prime_mmap(struct drm_gem_object *obj,
610 struct vm_area_struct *vma)
612 int ret;
614 ret = drm_gem_mmap_obj(obj, obj->size, vma);
615 if (ret < 0)
616 return ret;
618 return exynos_drm_gem_mmap_obj(obj, vma);