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.
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
;
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
,
37 mutex_unlock(&private->mm_lock
);
40 DRM_ERROR("out of I/O virtual memory: %zd\n", 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
);
60 mutex_lock(&private->mm_lock
);
61 drm_mm_remove_node(&rk_obj
->mm
);
62 mutex_unlock(&private->mm_lock
);
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
);
83 static int rockchip_gem_get_pages(struct rockchip_gem_object
*rk_obj
)
85 struct drm_device
*drm
= rk_obj
->base
.dev
;
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
);
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
,
117 drm_gem_put_pages(&rk_obj
->base
, rk_obj
->pages
, false, false);
121 static void rockchip_gem_put_pages(struct rockchip_gem_object
*rk_obj
)
123 sg_free_table(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
,
133 ret
= rockchip_gem_get_pages(rk_obj
);
137 ret
= rockchip_gem_iommu_map(rk_obj
);
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");
154 rockchip_gem_iommu_unmap(rk_obj
);
156 rockchip_gem_put_pages(rk_obj
);
161 static int rockchip_gem_alloc_dma(struct rockchip_gem_object
*rk_obj
,
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
;
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
,
175 if (!rk_obj
->kvaddr
) {
176 DRM_ERROR("failed to allocate %zu byte dma buffer", obj
->size
);
183 static int rockchip_gem_alloc_buf(struct rockchip_gem_object
*rk_obj
,
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
;
191 return rockchip_gem_alloc_iommu(rk_obj
, alloc_kmap
);
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
,
212 static void rockchip_gem_free_buf(struct rockchip_gem_object
*rk_obj
)
215 rockchip_gem_free_iommu(rk_obj
);
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
;
236 for (i
= offset
; i
< end
; i
++) {
237 ret
= vm_insert_page(vma
, uaddr
, rk_obj
->pages
[i
]);
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
)
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
;
269 ret
= rockchip_drm_gem_object_mmap_iommu(obj
, vma
);
271 ret
= rockchip_drm_gem_object_mmap_dma(obj
, vma
);
274 drm_gem_vm_close(vma
);
279 int rockchip_gem_mmap_buf(struct drm_gem_object
*obj
,
280 struct vm_area_struct
*vma
)
284 ret
= drm_gem_mmap_obj(obj
, obj
->size
, vma
);
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
;
297 ret
= drm_gem_mmap(filp
, vma
);
302 * Set vm_pgoff (used as a fake buffer offset by DRM) to 0 and map the
303 * whole buffer from the start.
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
);
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
);
328 return ERR_PTR(-ENOMEM
);
332 drm_gem_object_init(drm
, obj
, size
);
337 struct rockchip_gem_object
*
338 rockchip_gem_create_object(struct drm_device
*drm
, unsigned int size
,
341 struct rockchip_gem_object
*rk_obj
;
344 rk_obj
= rockchip_gem_alloc_object(drm
, size
);
348 ret
= rockchip_gem_alloc_buf(rk_obj
, alloc_kmap
);
350 goto err_free_rk_obj
;
355 rockchip_gem_release_object(rk_obj
);
360 * rockchip_gem_free_object - (struct drm_driver)->gem_free_object_unlocked
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
);
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
);
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
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
;
400 rk_obj
= rockchip_gem_create_object(drm
, size
, false);
402 return ERR_CAST(rk_obj
);
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
);
412 goto err_handle_create
;
414 /* drop reference from allocate - handle holds it now. */
415 drm_gem_object_put_unlocked(obj
);
420 rockchip_gem_free_object(obj
);
426 * rockchip_gem_dumb_create - (struct drm_driver)->dumb_create callback
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
,
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
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
;
465 return drm_prime_pages_to_sg(rk_obj
->pages
, rk_obj
->num_pages
);
467 sgt
= kzalloc(sizeof(*sgt
), GFP_KERNEL
);
469 return ERR_PTR(-ENOMEM
);
471 ret
= dma_get_sgtable_attrs(drm
->dev
, sgt
, rk_obj
->kvaddr
,
472 rk_obj
->dma_addr
, obj
->size
,
475 DRM_ERROR("failed to allocate sgt, %d\n", ret
);
483 static unsigned long rockchip_sg_get_contiguous_size(struct sg_table
*sgt
,
486 struct scatterlist
*s
;
487 dma_addr_t expected
= sg_dma_address(sgt
->sgl
);
489 unsigned long size
= 0;
491 for_each_sg(sgt
->sgl
, s
, count
, i
) {
492 if (sg_dma_address(s
) != expected
)
494 expected
= sg_dma_address(s
) + sg_dma_len(s
);
495 size
+= sg_dma_len(s
);
501 rockchip_gem_iommu_map_sg(struct drm_device
*drm
,
502 struct dma_buf_attachment
*attach
,
504 struct rockchip_gem_object
*rk_obj
)
507 return rockchip_gem_iommu_map(rk_obj
);
511 rockchip_gem_dma_map_sg(struct drm_device
*drm
,
512 struct dma_buf_attachment
*attach
,
514 struct rockchip_gem_object
*rk_obj
)
516 int count
= dma_map_sg(drm
->dev
, sg
->sgl
, sg
->nents
,
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
,
528 rk_obj
->dma_addr
= sg_dma_address(sg
->sgl
);
533 struct drm_gem_object
*
534 rockchip_gem_prime_import_sg_table(struct drm_device
*drm
,
535 struct dma_buf_attachment
*attach
,
538 struct rockchip_drm_private
*private = drm
->dev_private
;
539 struct rockchip_gem_object
*rk_obj
;
542 rk_obj
= rockchip_gem_alloc_object(drm
, attach
->dmabuf
->size
);
544 return ERR_CAST(rk_obj
);
547 ret
= rockchip_gem_iommu_map_sg(drm
, attach
, sg
, rk_obj
);
549 ret
= rockchip_gem_dma_map_sg(drm
, attach
, sg
, rk_obj
);
552 DRM_ERROR("failed to import sg table: %d\n", ret
);
553 goto err_free_rk_obj
;
556 return &rk_obj
->base
;
559 rockchip_gem_release_object(rk_obj
);
563 void *rockchip_gem_prime_vmap(struct drm_gem_object
*obj
)
565 struct rockchip_gem_object
*rk_obj
= to_rockchip_obj(obj
);
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
)
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
);
586 /* Nothing to do if allocated by DMA mapping API. */