1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
4 * Author:Mark Yao <mark.yao@rock-chips.com>
7 #include <linux/dma-buf.h>
8 #include <linux/iommu.h>
9 #include <linux/vmalloc.h>
12 #include <drm/drm_gem.h>
13 #include <drm/drm_gem_cma_helper.h>
14 #include <drm/drm_prime.h>
15 #include <drm/drm_vma_manager.h>
17 #include "rockchip_drm_drv.h"
18 #include "rockchip_drm_gem.h"
20 static int rockchip_gem_iommu_map(struct rockchip_gem_object *rk_obj)
22 struct drm_device *drm = rk_obj->base.dev;
23 struct rockchip_drm_private *private = drm->dev_private;
24 int prot = IOMMU_READ | IOMMU_WRITE;
27 mutex_lock(&private->mm_lock);
28 ret = drm_mm_insert_node_generic(&private->mm, &rk_obj->mm,
29 rk_obj->base.size, PAGE_SIZE,
31 mutex_unlock(&private->mm_lock);
34 DRM_ERROR("out of I/O virtual memory: %zd\n", ret);
38 rk_obj->dma_addr = rk_obj->mm.start;
40 ret = iommu_map_sgtable(private->domain, rk_obj->dma_addr, rk_obj->sgt,
42 if (ret < rk_obj->base.size) {
43 DRM_ERROR("failed to map buffer: size=%zd request_size=%zd\n",
44 ret, rk_obj->base.size);
54 mutex_lock(&private->mm_lock);
55 drm_mm_remove_node(&rk_obj->mm);
56 mutex_unlock(&private->mm_lock);
61 static int rockchip_gem_iommu_unmap(struct rockchip_gem_object *rk_obj)
63 struct drm_device *drm = rk_obj->base.dev;
64 struct rockchip_drm_private *private = drm->dev_private;
66 iommu_unmap(private->domain, rk_obj->dma_addr, rk_obj->size);
68 mutex_lock(&private->mm_lock);
70 drm_mm_remove_node(&rk_obj->mm);
72 mutex_unlock(&private->mm_lock);
77 static int rockchip_gem_get_pages(struct rockchip_gem_object *rk_obj)
79 struct drm_device *drm = rk_obj->base.dev;
81 struct scatterlist *s;
83 rk_obj->pages = drm_gem_get_pages(&rk_obj->base);
84 if (IS_ERR(rk_obj->pages))
85 return PTR_ERR(rk_obj->pages);
87 rk_obj->num_pages = rk_obj->base.size >> PAGE_SHIFT;
89 rk_obj->sgt = drm_prime_pages_to_sg(rk_obj->base.dev,
90 rk_obj->pages, rk_obj->num_pages);
91 if (IS_ERR(rk_obj->sgt)) {
92 ret = PTR_ERR(rk_obj->sgt);
97 * Fake up the SG table so that dma_sync_sg_for_device() can be used
98 * to flush the pages associated with it.
100 * TODO: Replace this by drm_clflush_sg() once it can be implemented
101 * without relying on symbols that are not exported.
103 for_each_sgtable_sg(rk_obj->sgt, s, i)
104 sg_dma_address(s) = sg_phys(s);
106 dma_sync_sgtable_for_device(drm->dev, rk_obj->sgt, DMA_TO_DEVICE);
111 drm_gem_put_pages(&rk_obj->base, rk_obj->pages, false, false);
115 static void rockchip_gem_put_pages(struct rockchip_gem_object *rk_obj)
117 sg_free_table(rk_obj->sgt);
119 drm_gem_put_pages(&rk_obj->base, rk_obj->pages, true, true);
122 static int rockchip_gem_alloc_iommu(struct rockchip_gem_object *rk_obj,
127 ret = rockchip_gem_get_pages(rk_obj);
131 ret = rockchip_gem_iommu_map(rk_obj);
136 rk_obj->kvaddr = vmap(rk_obj->pages, rk_obj->num_pages, VM_MAP,
137 pgprot_writecombine(PAGE_KERNEL));
138 if (!rk_obj->kvaddr) {
139 DRM_ERROR("failed to vmap() buffer\n");
148 rockchip_gem_iommu_unmap(rk_obj);
150 rockchip_gem_put_pages(rk_obj);
155 static int rockchip_gem_alloc_dma(struct rockchip_gem_object *rk_obj,
158 struct drm_gem_object *obj = &rk_obj->base;
159 struct drm_device *drm = obj->dev;
161 rk_obj->dma_attrs = DMA_ATTR_WRITE_COMBINE;
164 rk_obj->dma_attrs |= DMA_ATTR_NO_KERNEL_MAPPING;
166 rk_obj->kvaddr = dma_alloc_attrs(drm->dev, obj->size,
167 &rk_obj->dma_addr, GFP_KERNEL,
169 if (!rk_obj->kvaddr) {
170 DRM_ERROR("failed to allocate %zu byte dma buffer", obj->size);
177 static int rockchip_gem_alloc_buf(struct rockchip_gem_object *rk_obj,
180 struct drm_gem_object *obj = &rk_obj->base;
181 struct drm_device *drm = obj->dev;
182 struct rockchip_drm_private *private = drm->dev_private;
185 return rockchip_gem_alloc_iommu(rk_obj, alloc_kmap);
187 return rockchip_gem_alloc_dma(rk_obj, alloc_kmap);
190 static void rockchip_gem_free_iommu(struct rockchip_gem_object *rk_obj)
192 vunmap(rk_obj->kvaddr);
193 rockchip_gem_iommu_unmap(rk_obj);
194 rockchip_gem_put_pages(rk_obj);
197 static void rockchip_gem_free_dma(struct rockchip_gem_object *rk_obj)
199 struct drm_gem_object *obj = &rk_obj->base;
200 struct drm_device *drm = obj->dev;
202 dma_free_attrs(drm->dev, obj->size, rk_obj->kvaddr, rk_obj->dma_addr,
206 static void rockchip_gem_free_buf(struct rockchip_gem_object *rk_obj)
209 rockchip_gem_free_iommu(rk_obj);
211 rockchip_gem_free_dma(rk_obj);
214 static int rockchip_drm_gem_object_mmap_iommu(struct drm_gem_object *obj,
215 struct vm_area_struct *vma)
217 struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
218 unsigned int count = obj->size >> PAGE_SHIFT;
219 unsigned long user_count = vma_pages(vma);
224 return vm_map_pages(vma, rk_obj->pages, count);
227 static int rockchip_drm_gem_object_mmap_dma(struct drm_gem_object *obj,
228 struct vm_area_struct *vma)
230 struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
231 struct drm_device *drm = obj->dev;
233 return dma_mmap_attrs(drm->dev, vma, rk_obj->kvaddr, rk_obj->dma_addr,
234 obj->size, rk_obj->dma_attrs);
237 static int rockchip_drm_gem_object_mmap(struct drm_gem_object *obj,
238 struct vm_area_struct *vma)
241 struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
244 * We allocated a struct page table for rk_obj, so clear
245 * VM_PFNMAP flag that was set by drm_gem_mmap_obj()/drm_gem_mmap().
247 vma->vm_flags &= ~VM_PFNMAP;
250 ret = rockchip_drm_gem_object_mmap_iommu(obj, vma);
252 ret = rockchip_drm_gem_object_mmap_dma(obj, vma);
255 drm_gem_vm_close(vma);
260 int rockchip_gem_mmap_buf(struct drm_gem_object *obj,
261 struct vm_area_struct *vma)
265 ret = drm_gem_mmap_obj(obj, obj->size, vma);
269 return rockchip_drm_gem_object_mmap(obj, vma);
272 /* drm driver mmap file operations */
273 int rockchip_gem_mmap(struct file *filp, struct vm_area_struct *vma)
275 struct drm_gem_object *obj;
278 ret = drm_gem_mmap(filp, vma);
283 * Set vm_pgoff (used as a fake buffer offset by DRM) to 0 and map the
284 * whole buffer from the start.
288 obj = vma->vm_private_data;
290 return rockchip_drm_gem_object_mmap(obj, vma);
293 static void rockchip_gem_release_object(struct rockchip_gem_object *rk_obj)
295 drm_gem_object_release(&rk_obj->base);
299 static const struct drm_gem_object_funcs rockchip_gem_object_funcs = {
300 .free = rockchip_gem_free_object,
301 .get_sg_table = rockchip_gem_prime_get_sg_table,
302 .vmap = rockchip_gem_prime_vmap,
303 .vunmap = rockchip_gem_prime_vunmap,
304 .vm_ops = &drm_gem_cma_vm_ops,
307 static struct rockchip_gem_object *
308 rockchip_gem_alloc_object(struct drm_device *drm, unsigned int size)
310 struct rockchip_gem_object *rk_obj;
311 struct drm_gem_object *obj;
313 size = round_up(size, PAGE_SIZE);
315 rk_obj = kzalloc(sizeof(*rk_obj), GFP_KERNEL);
317 return ERR_PTR(-ENOMEM);
321 obj->funcs = &rockchip_gem_object_funcs;
323 drm_gem_object_init(drm, obj, size);
328 struct rockchip_gem_object *
329 rockchip_gem_create_object(struct drm_device *drm, unsigned int size,
332 struct rockchip_gem_object *rk_obj;
335 rk_obj = rockchip_gem_alloc_object(drm, size);
339 ret = rockchip_gem_alloc_buf(rk_obj, alloc_kmap);
341 goto err_free_rk_obj;
346 rockchip_gem_release_object(rk_obj);
351 * rockchip_gem_free_object - (struct drm_gem_object_funcs)->free
354 void rockchip_gem_free_object(struct drm_gem_object *obj)
356 struct drm_device *drm = obj->dev;
357 struct rockchip_drm_private *private = drm->dev_private;
358 struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
360 if (obj->import_attach) {
361 if (private->domain) {
362 rockchip_gem_iommu_unmap(rk_obj);
364 dma_unmap_sgtable(drm->dev, rk_obj->sgt,
365 DMA_BIDIRECTIONAL, 0);
367 drm_prime_gem_destroy(obj, rk_obj->sgt);
369 rockchip_gem_free_buf(rk_obj);
372 rockchip_gem_release_object(rk_obj);
376 * rockchip_gem_create_with_handle - allocate an object with the given
377 * size and create a gem handle on it
379 * returns a struct rockchip_gem_object* on success or ERR_PTR values
382 static struct rockchip_gem_object *
383 rockchip_gem_create_with_handle(struct drm_file *file_priv,
384 struct drm_device *drm, unsigned int size,
385 unsigned int *handle)
387 struct rockchip_gem_object *rk_obj;
388 struct drm_gem_object *obj;
391 rk_obj = rockchip_gem_create_object(drm, size, false);
393 return ERR_CAST(rk_obj);
398 * allocate a id of idr table where the obj is registered
399 * and handle has the id what user can see.
401 ret = drm_gem_handle_create(file_priv, obj, handle);
403 goto err_handle_create;
405 /* drop reference from allocate - handle holds it now. */
406 drm_gem_object_put(obj);
411 rockchip_gem_free_object(obj);
417 * rockchip_gem_dumb_create - (struct drm_driver)->dumb_create callback
420 * This aligns the pitch and size arguments to the minimum required. wrap
421 * this into your own function if you need bigger alignment.
423 int rockchip_gem_dumb_create(struct drm_file *file_priv,
424 struct drm_device *dev,
425 struct drm_mode_create_dumb *args)
427 struct rockchip_gem_object *rk_obj;
428 int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
431 * align to 64 bytes since Mali requires it.
433 args->pitch = ALIGN(min_pitch, 64);
434 args->size = args->pitch * args->height;
436 rk_obj = rockchip_gem_create_with_handle(file_priv, dev, args->size,
439 return PTR_ERR_OR_ZERO(rk_obj);
443 * Allocate a sg_table for this GEM object.
444 * Note: Both the table's contents, and the sg_table itself must be freed by
446 * Returns a pointer to the newly allocated sg_table, or an ERR_PTR() error.
448 struct sg_table *rockchip_gem_prime_get_sg_table(struct drm_gem_object *obj)
450 struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
451 struct drm_device *drm = obj->dev;
452 struct sg_table *sgt;
456 return drm_prime_pages_to_sg(obj->dev, rk_obj->pages, rk_obj->num_pages);
458 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
460 return ERR_PTR(-ENOMEM);
462 ret = dma_get_sgtable_attrs(drm->dev, sgt, rk_obj->kvaddr,
463 rk_obj->dma_addr, obj->size,
466 DRM_ERROR("failed to allocate sgt, %d\n", ret);
475 rockchip_gem_iommu_map_sg(struct drm_device *drm,
476 struct dma_buf_attachment *attach,
478 struct rockchip_gem_object *rk_obj)
481 return rockchip_gem_iommu_map(rk_obj);
485 rockchip_gem_dma_map_sg(struct drm_device *drm,
486 struct dma_buf_attachment *attach,
488 struct rockchip_gem_object *rk_obj)
490 int err = dma_map_sgtable(drm->dev, sg, DMA_BIDIRECTIONAL, 0);
494 if (drm_prime_get_contiguous_size(sg) < attach->dmabuf->size) {
495 DRM_ERROR("failed to map sg_table to contiguous linear address.\n");
496 dma_unmap_sgtable(drm->dev, sg, DMA_BIDIRECTIONAL, 0);
500 rk_obj->dma_addr = sg_dma_address(sg->sgl);
505 struct drm_gem_object *
506 rockchip_gem_prime_import_sg_table(struct drm_device *drm,
507 struct dma_buf_attachment *attach,
510 struct rockchip_drm_private *private = drm->dev_private;
511 struct rockchip_gem_object *rk_obj;
514 rk_obj = rockchip_gem_alloc_object(drm, attach->dmabuf->size);
516 return ERR_CAST(rk_obj);
519 ret = rockchip_gem_iommu_map_sg(drm, attach, sg, rk_obj);
521 ret = rockchip_gem_dma_map_sg(drm, attach, sg, rk_obj);
524 DRM_ERROR("failed to import sg table: %d\n", ret);
525 goto err_free_rk_obj;
528 return &rk_obj->base;
531 rockchip_gem_release_object(rk_obj);
535 int rockchip_gem_prime_vmap(struct drm_gem_object *obj, struct dma_buf_map *map)
537 struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
540 void *vaddr = vmap(rk_obj->pages, rk_obj->num_pages, VM_MAP,
541 pgprot_writecombine(PAGE_KERNEL));
544 dma_buf_map_set_vaddr(map, vaddr);
548 if (rk_obj->dma_attrs & DMA_ATTR_NO_KERNEL_MAPPING)
550 dma_buf_map_set_vaddr(map, rk_obj->kvaddr);
555 void rockchip_gem_prime_vunmap(struct drm_gem_object *obj, struct dma_buf_map *map)
557 struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
564 /* Nothing to do if allocated by DMA mapping API. */