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>
11 #include <drm/drm_gem.h>
12 #include <drm/drm_prime.h>
13 #include <drm/drm_vma_manager.h>
15 #include "rockchip_drm_drv.h"
16 #include "rockchip_drm_gem.h"
18 static int rockchip_gem_iommu_map(struct rockchip_gem_object
*rk_obj
)
20 struct drm_device
*drm
= rk_obj
->base
.dev
;
21 struct rockchip_drm_private
*private = drm
->dev_private
;
22 int prot
= IOMMU_READ
| IOMMU_WRITE
;
25 mutex_lock(&private->mm_lock
);
26 ret
= drm_mm_insert_node_generic(&private->mm
, &rk_obj
->mm
,
27 rk_obj
->base
.size
, PAGE_SIZE
,
29 mutex_unlock(&private->mm_lock
);
32 DRM_ERROR("out of I/O virtual memory: %zd\n", ret
);
36 rk_obj
->dma_addr
= rk_obj
->mm
.start
;
38 ret
= iommu_map_sg(private->domain
, rk_obj
->dma_addr
, rk_obj
->sgt
->sgl
,
39 rk_obj
->sgt
->nents
, prot
);
40 if (ret
< rk_obj
->base
.size
) {
41 DRM_ERROR("failed to map buffer: size=%zd request_size=%zd\n",
42 ret
, rk_obj
->base
.size
);
52 mutex_lock(&private->mm_lock
);
53 drm_mm_remove_node(&rk_obj
->mm
);
54 mutex_unlock(&private->mm_lock
);
59 static int rockchip_gem_iommu_unmap(struct rockchip_gem_object
*rk_obj
)
61 struct drm_device
*drm
= rk_obj
->base
.dev
;
62 struct rockchip_drm_private
*private = drm
->dev_private
;
64 iommu_unmap(private->domain
, rk_obj
->dma_addr
, rk_obj
->size
);
66 mutex_lock(&private->mm_lock
);
68 drm_mm_remove_node(&rk_obj
->mm
);
70 mutex_unlock(&private->mm_lock
);
75 static int rockchip_gem_get_pages(struct rockchip_gem_object
*rk_obj
)
77 struct drm_device
*drm
= rk_obj
->base
.dev
;
79 struct scatterlist
*s
;
81 rk_obj
->pages
= drm_gem_get_pages(&rk_obj
->base
);
82 if (IS_ERR(rk_obj
->pages
))
83 return PTR_ERR(rk_obj
->pages
);
85 rk_obj
->num_pages
= rk_obj
->base
.size
>> PAGE_SHIFT
;
87 rk_obj
->sgt
= drm_prime_pages_to_sg(rk_obj
->pages
, rk_obj
->num_pages
);
88 if (IS_ERR(rk_obj
->sgt
)) {
89 ret
= PTR_ERR(rk_obj
->sgt
);
94 * Fake up the SG table so that dma_sync_sg_for_device() can be used
95 * to flush the pages associated with it.
97 * TODO: Replace this by drm_clflush_sg() once it can be implemented
98 * without relying on symbols that are not exported.
100 for_each_sg(rk_obj
->sgt
->sgl
, s
, rk_obj
->sgt
->nents
, i
)
101 sg_dma_address(s
) = sg_phys(s
);
103 dma_sync_sg_for_device(drm
->dev
, rk_obj
->sgt
->sgl
, rk_obj
->sgt
->nents
,
109 drm_gem_put_pages(&rk_obj
->base
, rk_obj
->pages
, false, false);
113 static void rockchip_gem_put_pages(struct rockchip_gem_object
*rk_obj
)
115 sg_free_table(rk_obj
->sgt
);
117 drm_gem_put_pages(&rk_obj
->base
, rk_obj
->pages
, true, true);
120 static int rockchip_gem_alloc_iommu(struct rockchip_gem_object
*rk_obj
,
125 ret
= rockchip_gem_get_pages(rk_obj
);
129 ret
= rockchip_gem_iommu_map(rk_obj
);
134 rk_obj
->kvaddr
= vmap(rk_obj
->pages
, rk_obj
->num_pages
, VM_MAP
,
135 pgprot_writecombine(PAGE_KERNEL
));
136 if (!rk_obj
->kvaddr
) {
137 DRM_ERROR("failed to vmap() buffer\n");
146 rockchip_gem_iommu_unmap(rk_obj
);
148 rockchip_gem_put_pages(rk_obj
);
153 static int rockchip_gem_alloc_dma(struct rockchip_gem_object
*rk_obj
,
156 struct drm_gem_object
*obj
= &rk_obj
->base
;
157 struct drm_device
*drm
= obj
->dev
;
159 rk_obj
->dma_attrs
= DMA_ATTR_WRITE_COMBINE
;
162 rk_obj
->dma_attrs
|= DMA_ATTR_NO_KERNEL_MAPPING
;
164 rk_obj
->kvaddr
= dma_alloc_attrs(drm
->dev
, obj
->size
,
165 &rk_obj
->dma_addr
, GFP_KERNEL
,
167 if (!rk_obj
->kvaddr
) {
168 DRM_ERROR("failed to allocate %zu byte dma buffer", obj
->size
);
175 static int rockchip_gem_alloc_buf(struct rockchip_gem_object
*rk_obj
,
178 struct drm_gem_object
*obj
= &rk_obj
->base
;
179 struct drm_device
*drm
= obj
->dev
;
180 struct rockchip_drm_private
*private = drm
->dev_private
;
183 return rockchip_gem_alloc_iommu(rk_obj
, alloc_kmap
);
185 return rockchip_gem_alloc_dma(rk_obj
, alloc_kmap
);
188 static void rockchip_gem_free_iommu(struct rockchip_gem_object
*rk_obj
)
190 vunmap(rk_obj
->kvaddr
);
191 rockchip_gem_iommu_unmap(rk_obj
);
192 rockchip_gem_put_pages(rk_obj
);
195 static void rockchip_gem_free_dma(struct rockchip_gem_object
*rk_obj
)
197 struct drm_gem_object
*obj
= &rk_obj
->base
;
198 struct drm_device
*drm
= obj
->dev
;
200 dma_free_attrs(drm
->dev
, obj
->size
, rk_obj
->kvaddr
, rk_obj
->dma_addr
,
204 static void rockchip_gem_free_buf(struct rockchip_gem_object
*rk_obj
)
207 rockchip_gem_free_iommu(rk_obj
);
209 rockchip_gem_free_dma(rk_obj
);
212 static int rockchip_drm_gem_object_mmap_iommu(struct drm_gem_object
*obj
,
213 struct vm_area_struct
*vma
)
215 struct rockchip_gem_object
*rk_obj
= to_rockchip_obj(obj
);
216 unsigned int count
= obj
->size
>> PAGE_SHIFT
;
217 unsigned long user_count
= vma_pages(vma
);
222 return vm_map_pages(vma
, rk_obj
->pages
, count
);
225 static int rockchip_drm_gem_object_mmap_dma(struct drm_gem_object
*obj
,
226 struct vm_area_struct
*vma
)
228 struct rockchip_gem_object
*rk_obj
= to_rockchip_obj(obj
);
229 struct drm_device
*drm
= obj
->dev
;
231 return dma_mmap_attrs(drm
->dev
, vma
, rk_obj
->kvaddr
, rk_obj
->dma_addr
,
232 obj
->size
, rk_obj
->dma_attrs
);
235 static int rockchip_drm_gem_object_mmap(struct drm_gem_object
*obj
,
236 struct vm_area_struct
*vma
)
239 struct rockchip_gem_object
*rk_obj
= to_rockchip_obj(obj
);
242 * We allocated a struct page table for rk_obj, so clear
243 * VM_PFNMAP flag that was set by drm_gem_mmap_obj()/drm_gem_mmap().
245 vma
->vm_flags
&= ~VM_PFNMAP
;
248 ret
= rockchip_drm_gem_object_mmap_iommu(obj
, vma
);
250 ret
= rockchip_drm_gem_object_mmap_dma(obj
, vma
);
253 drm_gem_vm_close(vma
);
258 int rockchip_gem_mmap_buf(struct drm_gem_object
*obj
,
259 struct vm_area_struct
*vma
)
263 ret
= drm_gem_mmap_obj(obj
, obj
->size
, vma
);
267 return rockchip_drm_gem_object_mmap(obj
, vma
);
270 /* drm driver mmap file operations */
271 int rockchip_gem_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
273 struct drm_gem_object
*obj
;
276 ret
= drm_gem_mmap(filp
, vma
);
281 * Set vm_pgoff (used as a fake buffer offset by DRM) to 0 and map the
282 * whole buffer from the start.
286 obj
= vma
->vm_private_data
;
288 return rockchip_drm_gem_object_mmap(obj
, vma
);
291 static void rockchip_gem_release_object(struct rockchip_gem_object
*rk_obj
)
293 drm_gem_object_release(&rk_obj
->base
);
297 static struct rockchip_gem_object
*
298 rockchip_gem_alloc_object(struct drm_device
*drm
, unsigned int size
)
300 struct rockchip_gem_object
*rk_obj
;
301 struct drm_gem_object
*obj
;
303 size
= round_up(size
, PAGE_SIZE
);
305 rk_obj
= kzalloc(sizeof(*rk_obj
), GFP_KERNEL
);
307 return ERR_PTR(-ENOMEM
);
311 drm_gem_object_init(drm
, obj
, size
);
316 struct rockchip_gem_object
*
317 rockchip_gem_create_object(struct drm_device
*drm
, unsigned int size
,
320 struct rockchip_gem_object
*rk_obj
;
323 rk_obj
= rockchip_gem_alloc_object(drm
, size
);
327 ret
= rockchip_gem_alloc_buf(rk_obj
, alloc_kmap
);
329 goto err_free_rk_obj
;
334 rockchip_gem_release_object(rk_obj
);
339 * rockchip_gem_free_object - (struct drm_driver)->gem_free_object_unlocked
342 void rockchip_gem_free_object(struct drm_gem_object
*obj
)
344 struct drm_device
*drm
= obj
->dev
;
345 struct rockchip_drm_private
*private = drm
->dev_private
;
346 struct rockchip_gem_object
*rk_obj
= to_rockchip_obj(obj
);
348 if (obj
->import_attach
) {
349 if (private->domain
) {
350 rockchip_gem_iommu_unmap(rk_obj
);
352 dma_unmap_sg(drm
->dev
, rk_obj
->sgt
->sgl
,
353 rk_obj
->sgt
->nents
, DMA_BIDIRECTIONAL
);
355 drm_prime_gem_destroy(obj
, rk_obj
->sgt
);
357 rockchip_gem_free_buf(rk_obj
);
360 rockchip_gem_release_object(rk_obj
);
364 * rockchip_gem_create_with_handle - allocate an object with the given
365 * size and create a gem handle on it
367 * returns a struct rockchip_gem_object* on success or ERR_PTR values
370 static struct rockchip_gem_object
*
371 rockchip_gem_create_with_handle(struct drm_file
*file_priv
,
372 struct drm_device
*drm
, unsigned int size
,
373 unsigned int *handle
)
375 struct rockchip_gem_object
*rk_obj
;
376 struct drm_gem_object
*obj
;
379 rk_obj
= rockchip_gem_create_object(drm
, size
, false);
381 return ERR_CAST(rk_obj
);
386 * allocate a id of idr table where the obj is registered
387 * and handle has the id what user can see.
389 ret
= drm_gem_handle_create(file_priv
, obj
, handle
);
391 goto err_handle_create
;
393 /* drop reference from allocate - handle holds it now. */
394 drm_gem_object_put_unlocked(obj
);
399 rockchip_gem_free_object(obj
);
405 * rockchip_gem_dumb_create - (struct drm_driver)->dumb_create callback
408 * This aligns the pitch and size arguments to the minimum required. wrap
409 * this into your own function if you need bigger alignment.
411 int rockchip_gem_dumb_create(struct drm_file
*file_priv
,
412 struct drm_device
*dev
,
413 struct drm_mode_create_dumb
*args
)
415 struct rockchip_gem_object
*rk_obj
;
416 int min_pitch
= DIV_ROUND_UP(args
->width
* args
->bpp
, 8);
419 * align to 64 bytes since Mali requires it.
421 args
->pitch
= ALIGN(min_pitch
, 64);
422 args
->size
= args
->pitch
* args
->height
;
424 rk_obj
= rockchip_gem_create_with_handle(file_priv
, dev
, args
->size
,
427 return PTR_ERR_OR_ZERO(rk_obj
);
431 * Allocate a sg_table for this GEM object.
432 * Note: Both the table's contents, and the sg_table itself must be freed by
434 * Returns a pointer to the newly allocated sg_table, or an ERR_PTR() error.
436 struct sg_table
*rockchip_gem_prime_get_sg_table(struct drm_gem_object
*obj
)
438 struct rockchip_gem_object
*rk_obj
= to_rockchip_obj(obj
);
439 struct drm_device
*drm
= obj
->dev
;
440 struct sg_table
*sgt
;
444 return drm_prime_pages_to_sg(rk_obj
->pages
, rk_obj
->num_pages
);
446 sgt
= kzalloc(sizeof(*sgt
), GFP_KERNEL
);
448 return ERR_PTR(-ENOMEM
);
450 ret
= dma_get_sgtable_attrs(drm
->dev
, sgt
, rk_obj
->kvaddr
,
451 rk_obj
->dma_addr
, obj
->size
,
454 DRM_ERROR("failed to allocate sgt, %d\n", ret
);
462 static unsigned long rockchip_sg_get_contiguous_size(struct sg_table
*sgt
,
465 struct scatterlist
*s
;
466 dma_addr_t expected
= sg_dma_address(sgt
->sgl
);
468 unsigned long size
= 0;
470 for_each_sg(sgt
->sgl
, s
, count
, i
) {
471 if (sg_dma_address(s
) != expected
)
473 expected
= sg_dma_address(s
) + sg_dma_len(s
);
474 size
+= sg_dma_len(s
);
480 rockchip_gem_iommu_map_sg(struct drm_device
*drm
,
481 struct dma_buf_attachment
*attach
,
483 struct rockchip_gem_object
*rk_obj
)
486 return rockchip_gem_iommu_map(rk_obj
);
490 rockchip_gem_dma_map_sg(struct drm_device
*drm
,
491 struct dma_buf_attachment
*attach
,
493 struct rockchip_gem_object
*rk_obj
)
495 int count
= dma_map_sg(drm
->dev
, sg
->sgl
, sg
->nents
,
500 if (rockchip_sg_get_contiguous_size(sg
, count
) < attach
->dmabuf
->size
) {
501 DRM_ERROR("failed to map sg_table to contiguous linear address.\n");
502 dma_unmap_sg(drm
->dev
, sg
->sgl
, sg
->nents
,
507 rk_obj
->dma_addr
= sg_dma_address(sg
->sgl
);
512 struct drm_gem_object
*
513 rockchip_gem_prime_import_sg_table(struct drm_device
*drm
,
514 struct dma_buf_attachment
*attach
,
517 struct rockchip_drm_private
*private = drm
->dev_private
;
518 struct rockchip_gem_object
*rk_obj
;
521 rk_obj
= rockchip_gem_alloc_object(drm
, attach
->dmabuf
->size
);
523 return ERR_CAST(rk_obj
);
526 ret
= rockchip_gem_iommu_map_sg(drm
, attach
, sg
, rk_obj
);
528 ret
= rockchip_gem_dma_map_sg(drm
, attach
, sg
, rk_obj
);
531 DRM_ERROR("failed to import sg table: %d\n", ret
);
532 goto err_free_rk_obj
;
535 return &rk_obj
->base
;
538 rockchip_gem_release_object(rk_obj
);
542 void *rockchip_gem_prime_vmap(struct drm_gem_object
*obj
)
544 struct rockchip_gem_object
*rk_obj
= to_rockchip_obj(obj
);
547 return vmap(rk_obj
->pages
, rk_obj
->num_pages
, VM_MAP
,
548 pgprot_writecombine(PAGE_KERNEL
));
550 if (rk_obj
->dma_attrs
& DMA_ATTR_NO_KERNEL_MAPPING
)
553 return rk_obj
->kvaddr
;
556 void rockchip_gem_prime_vunmap(struct drm_gem_object
*obj
, void *vaddr
)
558 struct rockchip_gem_object
*rk_obj
= to_rockchip_obj(obj
);
565 /* Nothing to do if allocated by DMA mapping API. */