dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / gpu / drm / rockchip / rockchip_drm_gem.c
bloba8db758d523e94780584090b191625ffdcd28a16
1 /*
2 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
3 * Author:Mark Yao <mark.yao@rock-chips.com>
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <drm/drm.h>
16 #include <drm/drmP.h>
17 #include <drm/drm_gem.h>
18 #include <drm/drm_vma_manager.h>
20 #include <linux/dma-buf.h>
21 #include <linux/iommu.h>
23 #include "rockchip_drm_drv.h"
24 #include "rockchip_drm_gem.h"
26 static int rockchip_gem_iommu_map(struct rockchip_gem_object *rk_obj)
28 struct drm_device *drm = rk_obj->base.dev;
29 struct rockchip_drm_private *private = drm->dev_private;
30 int prot = IOMMU_READ | IOMMU_WRITE;
31 ssize_t ret;
33 mutex_lock(&private->mm_lock);
34 ret = drm_mm_insert_node_generic(&private->mm, &rk_obj->mm,
35 rk_obj->base.size, PAGE_SIZE,
36 0, 0);
37 mutex_unlock(&private->mm_lock);
39 if (ret < 0) {
40 DRM_ERROR("out of I/O virtual memory: %zd\n", ret);
41 return ret;
44 rk_obj->dma_addr = rk_obj->mm.start;
46 ret = iommu_map_sg(private->domain, rk_obj->dma_addr, rk_obj->sgt->sgl,
47 rk_obj->sgt->nents, prot);
48 if (ret < rk_obj->base.size) {
49 DRM_ERROR("failed to map buffer: size=%zd request_size=%zd\n",
50 ret, rk_obj->base.size);
51 ret = -ENOMEM;
52 goto err_remove_node;
55 rk_obj->size = ret;
57 return 0;
59 err_remove_node:
60 mutex_lock(&private->mm_lock);
61 drm_mm_remove_node(&rk_obj->mm);
62 mutex_unlock(&private->mm_lock);
64 return ret;
67 static int rockchip_gem_iommu_unmap(struct rockchip_gem_object *rk_obj)
69 struct drm_device *drm = rk_obj->base.dev;
70 struct rockchip_drm_private *private = drm->dev_private;
72 iommu_unmap(private->domain, rk_obj->dma_addr, rk_obj->size);
74 mutex_lock(&private->mm_lock);
76 drm_mm_remove_node(&rk_obj->mm);
78 mutex_unlock(&private->mm_lock);
80 return 0;
83 static int rockchip_gem_get_pages(struct rockchip_gem_object *rk_obj)
85 struct drm_device *drm = rk_obj->base.dev;
86 int ret, i;
87 struct scatterlist *s;
89 rk_obj->pages = drm_gem_get_pages(&rk_obj->base);
90 if (IS_ERR(rk_obj->pages))
91 return PTR_ERR(rk_obj->pages);
93 rk_obj->num_pages = rk_obj->base.size >> PAGE_SHIFT;
95 rk_obj->sgt = drm_prime_pages_to_sg(rk_obj->pages, rk_obj->num_pages);
96 if (IS_ERR(rk_obj->sgt)) {
97 ret = PTR_ERR(rk_obj->sgt);
98 goto err_put_pages;
102 * Fake up the SG table so that dma_sync_sg_for_device() can be used
103 * to flush the pages associated with it.
105 * TODO: Replace this by drm_clflush_sg() once it can be implemented
106 * without relying on symbols that are not exported.
108 for_each_sg(rk_obj->sgt->sgl, s, rk_obj->sgt->nents, i)
109 sg_dma_address(s) = sg_phys(s);
111 dma_sync_sg_for_device(drm->dev, rk_obj->sgt->sgl, rk_obj->sgt->nents,
112 DMA_TO_DEVICE);
114 return 0;
116 err_put_pages:
117 drm_gem_put_pages(&rk_obj->base, rk_obj->pages, false, false);
118 return ret;
121 static void rockchip_gem_put_pages(struct rockchip_gem_object *rk_obj)
123 sg_free_table(rk_obj->sgt);
124 kfree(rk_obj->sgt);
125 drm_gem_put_pages(&rk_obj->base, rk_obj->pages, true, true);
128 static int rockchip_gem_alloc_iommu(struct rockchip_gem_object *rk_obj,
129 bool alloc_kmap)
131 int ret;
133 ret = rockchip_gem_get_pages(rk_obj);
134 if (ret < 0)
135 return ret;
137 ret = rockchip_gem_iommu_map(rk_obj);
138 if (ret < 0)
139 goto err_free;
141 if (alloc_kmap) {
142 rk_obj->kvaddr = vmap(rk_obj->pages, rk_obj->num_pages, VM_MAP,
143 pgprot_writecombine(PAGE_KERNEL));
144 if (!rk_obj->kvaddr) {
145 DRM_ERROR("failed to vmap() buffer\n");
146 ret = -ENOMEM;
147 goto err_unmap;
151 return 0;
153 err_unmap:
154 rockchip_gem_iommu_unmap(rk_obj);
155 err_free:
156 rockchip_gem_put_pages(rk_obj);
158 return ret;
161 static int rockchip_gem_alloc_dma(struct rockchip_gem_object *rk_obj,
162 bool alloc_kmap)
164 struct drm_gem_object *obj = &rk_obj->base;
165 struct drm_device *drm = obj->dev;
167 rk_obj->dma_attrs = DMA_ATTR_WRITE_COMBINE;
169 if (!alloc_kmap)
170 rk_obj->dma_attrs |= DMA_ATTR_NO_KERNEL_MAPPING;
172 rk_obj->kvaddr = dma_alloc_attrs(drm->dev, obj->size,
173 &rk_obj->dma_addr, GFP_KERNEL,
174 rk_obj->dma_attrs);
175 if (!rk_obj->kvaddr) {
176 DRM_ERROR("failed to allocate %zu byte dma buffer", obj->size);
177 return -ENOMEM;
180 return 0;
183 static int rockchip_gem_alloc_buf(struct rockchip_gem_object *rk_obj,
184 bool alloc_kmap)
186 struct drm_gem_object *obj = &rk_obj->base;
187 struct drm_device *drm = obj->dev;
188 struct rockchip_drm_private *private = drm->dev_private;
190 if (private->domain)
191 return rockchip_gem_alloc_iommu(rk_obj, alloc_kmap);
192 else
193 return rockchip_gem_alloc_dma(rk_obj, alloc_kmap);
196 static void rockchip_gem_free_iommu(struct rockchip_gem_object *rk_obj)
198 vunmap(rk_obj->kvaddr);
199 rockchip_gem_iommu_unmap(rk_obj);
200 rockchip_gem_put_pages(rk_obj);
203 static void rockchip_gem_free_dma(struct rockchip_gem_object *rk_obj)
205 struct drm_gem_object *obj = &rk_obj->base;
206 struct drm_device *drm = obj->dev;
208 dma_free_attrs(drm->dev, obj->size, rk_obj->kvaddr, rk_obj->dma_addr,
209 rk_obj->dma_attrs);
212 static void rockchip_gem_free_buf(struct rockchip_gem_object *rk_obj)
214 if (rk_obj->pages)
215 rockchip_gem_free_iommu(rk_obj);
216 else
217 rockchip_gem_free_dma(rk_obj);
220 static int rockchip_drm_gem_object_mmap_iommu(struct drm_gem_object *obj,
221 struct vm_area_struct *vma)
223 struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
224 unsigned int i, count = obj->size >> PAGE_SHIFT;
225 unsigned long user_count = vma_pages(vma);
226 unsigned long uaddr = vma->vm_start;
227 unsigned long offset = vma->vm_pgoff;
228 unsigned long end = user_count + offset;
229 int ret;
231 if (user_count == 0)
232 return -ENXIO;
233 if (end > count)
234 return -ENXIO;
236 for (i = offset; i < end; i++) {
237 ret = vm_insert_page(vma, uaddr, rk_obj->pages[i]);
238 if (ret)
239 return ret;
240 uaddr += PAGE_SIZE;
243 return 0;
246 static int rockchip_drm_gem_object_mmap_dma(struct drm_gem_object *obj,
247 struct vm_area_struct *vma)
249 struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
250 struct drm_device *drm = obj->dev;
252 return dma_mmap_attrs(drm->dev, vma, rk_obj->kvaddr, rk_obj->dma_addr,
253 obj->size, rk_obj->dma_attrs);
256 static int rockchip_drm_gem_object_mmap(struct drm_gem_object *obj,
257 struct vm_area_struct *vma)
259 int ret;
260 struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
263 * We allocated a struct page table for rk_obj, so clear
264 * VM_PFNMAP flag that was set by drm_gem_mmap_obj()/drm_gem_mmap().
266 vma->vm_flags &= ~VM_PFNMAP;
268 if (rk_obj->pages)
269 ret = rockchip_drm_gem_object_mmap_iommu(obj, vma);
270 else
271 ret = rockchip_drm_gem_object_mmap_dma(obj, vma);
273 if (ret)
274 drm_gem_vm_close(vma);
276 return ret;
279 int rockchip_gem_mmap_buf(struct drm_gem_object *obj,
280 struct vm_area_struct *vma)
282 int ret;
284 ret = drm_gem_mmap_obj(obj, obj->size, vma);
285 if (ret)
286 return ret;
288 return rockchip_drm_gem_object_mmap(obj, vma);
291 /* drm driver mmap file operations */
292 int rockchip_gem_mmap(struct file *filp, struct vm_area_struct *vma)
294 struct drm_gem_object *obj;
295 int ret;
297 ret = drm_gem_mmap(filp, vma);
298 if (ret)
299 return ret;
302 * Set vm_pgoff (used as a fake buffer offset by DRM) to 0 and map the
303 * whole buffer from the start.
305 vma->vm_pgoff = 0;
307 obj = vma->vm_private_data;
309 return rockchip_drm_gem_object_mmap(obj, vma);
312 static void rockchip_gem_release_object(struct rockchip_gem_object *rk_obj)
314 drm_gem_object_release(&rk_obj->base);
315 kfree(rk_obj);
318 struct rockchip_gem_object *
319 rockchip_gem_alloc_object(struct drm_device *drm, unsigned int size)
321 struct rockchip_gem_object *rk_obj;
322 struct drm_gem_object *obj;
324 size = round_up(size, PAGE_SIZE);
326 rk_obj = kzalloc(sizeof(*rk_obj), GFP_KERNEL);
327 if (!rk_obj)
328 return ERR_PTR(-ENOMEM);
330 obj = &rk_obj->base;
332 drm_gem_object_init(drm, obj, size);
334 return rk_obj;
337 struct rockchip_gem_object *
338 rockchip_gem_create_object(struct drm_device *drm, unsigned int size,
339 bool alloc_kmap)
341 struct rockchip_gem_object *rk_obj;
342 int ret;
344 rk_obj = rockchip_gem_alloc_object(drm, size);
345 if (IS_ERR(rk_obj))
346 return rk_obj;
348 ret = rockchip_gem_alloc_buf(rk_obj, alloc_kmap);
349 if (ret)
350 goto err_free_rk_obj;
352 return rk_obj;
354 err_free_rk_obj:
355 rockchip_gem_release_object(rk_obj);
356 return ERR_PTR(ret);
360 * rockchip_gem_free_object - (struct drm_driver)->gem_free_object_unlocked
361 * callback function
363 void rockchip_gem_free_object(struct drm_gem_object *obj)
365 struct drm_device *drm = obj->dev;
366 struct rockchip_drm_private *private = drm->dev_private;
367 struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
369 if (obj->import_attach) {
370 if (private->domain) {
371 rockchip_gem_iommu_unmap(rk_obj);
372 } else {
373 dma_unmap_sg(drm->dev, rk_obj->sgt->sgl,
374 rk_obj->sgt->nents, DMA_BIDIRECTIONAL);
376 drm_prime_gem_destroy(obj, rk_obj->sgt);
377 } else {
378 rockchip_gem_free_buf(rk_obj);
381 rockchip_gem_release_object(rk_obj);
385 * rockchip_gem_create_with_handle - allocate an object with the given
386 * size and create a gem handle on it
388 * returns a struct rockchip_gem_object* on success or ERR_PTR values
389 * on failure.
391 static struct rockchip_gem_object *
392 rockchip_gem_create_with_handle(struct drm_file *file_priv,
393 struct drm_device *drm, unsigned int size,
394 unsigned int *handle)
396 struct rockchip_gem_object *rk_obj;
397 struct drm_gem_object *obj;
398 int ret;
400 rk_obj = rockchip_gem_create_object(drm, size, false);
401 if (IS_ERR(rk_obj))
402 return ERR_CAST(rk_obj);
404 obj = &rk_obj->base;
407 * allocate a id of idr table where the obj is registered
408 * and handle has the id what user can see.
410 ret = drm_gem_handle_create(file_priv, obj, handle);
411 if (ret)
412 goto err_handle_create;
414 /* drop reference from allocate - handle holds it now. */
415 drm_gem_object_put_unlocked(obj);
417 return rk_obj;
419 err_handle_create:
420 rockchip_gem_free_object(obj);
422 return ERR_PTR(ret);
426 * rockchip_gem_dumb_create - (struct drm_driver)->dumb_create callback
427 * function
429 * This aligns the pitch and size arguments to the minimum required. wrap
430 * this into your own function if you need bigger alignment.
432 int rockchip_gem_dumb_create(struct drm_file *file_priv,
433 struct drm_device *dev,
434 struct drm_mode_create_dumb *args)
436 struct rockchip_gem_object *rk_obj;
437 int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
440 * align to 64 bytes since Mali requires it.
442 args->pitch = ALIGN(min_pitch, 64);
443 args->size = args->pitch * args->height;
445 rk_obj = rockchip_gem_create_with_handle(file_priv, dev, args->size,
446 &args->handle);
448 return PTR_ERR_OR_ZERO(rk_obj);
452 * Allocate a sg_table for this GEM object.
453 * Note: Both the table's contents, and the sg_table itself must be freed by
454 * the caller.
455 * Returns a pointer to the newly allocated sg_table, or an ERR_PTR() error.
457 struct sg_table *rockchip_gem_prime_get_sg_table(struct drm_gem_object *obj)
459 struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
460 struct drm_device *drm = obj->dev;
461 struct sg_table *sgt;
462 int ret;
464 if (rk_obj->pages)
465 return drm_prime_pages_to_sg(rk_obj->pages, rk_obj->num_pages);
467 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
468 if (!sgt)
469 return ERR_PTR(-ENOMEM);
471 ret = dma_get_sgtable_attrs(drm->dev, sgt, rk_obj->kvaddr,
472 rk_obj->dma_addr, obj->size,
473 rk_obj->dma_attrs);
474 if (ret) {
475 DRM_ERROR("failed to allocate sgt, %d\n", ret);
476 kfree(sgt);
477 return ERR_PTR(ret);
480 return sgt;
483 static unsigned long rockchip_sg_get_contiguous_size(struct sg_table *sgt,
484 int count)
486 struct scatterlist *s;
487 dma_addr_t expected = sg_dma_address(sgt->sgl);
488 unsigned int i;
489 unsigned long size = 0;
491 for_each_sg(sgt->sgl, s, count, i) {
492 if (sg_dma_address(s) != expected)
493 break;
494 expected = sg_dma_address(s) + sg_dma_len(s);
495 size += sg_dma_len(s);
497 return size;
500 static int
501 rockchip_gem_iommu_map_sg(struct drm_device *drm,
502 struct dma_buf_attachment *attach,
503 struct sg_table *sg,
504 struct rockchip_gem_object *rk_obj)
506 rk_obj->sgt = sg;
507 return rockchip_gem_iommu_map(rk_obj);
510 static int
511 rockchip_gem_dma_map_sg(struct drm_device *drm,
512 struct dma_buf_attachment *attach,
513 struct sg_table *sg,
514 struct rockchip_gem_object *rk_obj)
516 int count = dma_map_sg(drm->dev, sg->sgl, sg->nents,
517 DMA_BIDIRECTIONAL);
518 if (!count)
519 return -EINVAL;
521 if (rockchip_sg_get_contiguous_size(sg, count) < attach->dmabuf->size) {
522 DRM_ERROR("failed to map sg_table to contiguous linear address.\n");
523 dma_unmap_sg(drm->dev, sg->sgl, sg->nents,
524 DMA_BIDIRECTIONAL);
525 return -EINVAL;
528 rk_obj->dma_addr = sg_dma_address(sg->sgl);
529 rk_obj->sgt = sg;
530 return 0;
533 struct drm_gem_object *
534 rockchip_gem_prime_import_sg_table(struct drm_device *drm,
535 struct dma_buf_attachment *attach,
536 struct sg_table *sg)
538 struct rockchip_drm_private *private = drm->dev_private;
539 struct rockchip_gem_object *rk_obj;
540 int ret;
542 rk_obj = rockchip_gem_alloc_object(drm, attach->dmabuf->size);
543 if (IS_ERR(rk_obj))
544 return ERR_CAST(rk_obj);
546 if (private->domain)
547 ret = rockchip_gem_iommu_map_sg(drm, attach, sg, rk_obj);
548 else
549 ret = rockchip_gem_dma_map_sg(drm, attach, sg, rk_obj);
551 if (ret < 0) {
552 DRM_ERROR("failed to import sg table: %d\n", ret);
553 goto err_free_rk_obj;
556 return &rk_obj->base;
558 err_free_rk_obj:
559 rockchip_gem_release_object(rk_obj);
560 return ERR_PTR(ret);
563 void *rockchip_gem_prime_vmap(struct drm_gem_object *obj)
565 struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
567 if (rk_obj->pages)
568 return vmap(rk_obj->pages, rk_obj->num_pages, VM_MAP,
569 pgprot_writecombine(PAGE_KERNEL));
571 if (rk_obj->dma_attrs & DMA_ATTR_NO_KERNEL_MAPPING)
572 return NULL;
574 return rk_obj->kvaddr;
577 void rockchip_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
579 struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
581 if (rk_obj->pages) {
582 vunmap(vaddr);
583 return;
586 /* Nothing to do if allocated by DMA mapping API. */