2 * NVIDIA Tegra DRM GEM helper functions
4 * Copyright (C) 2012 Sascha Hauer, Pengutronix
5 * Copyright (C) 2013 NVIDIA CORPORATION, All rights reserved.
7 * Based on the GEM/CMA helpers
9 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 #include <linux/dma-buf.h>
17 #include <linux/iommu.h>
18 #include <drm/tegra_drm.h>
23 static inline struct tegra_bo
*host1x_to_tegra_bo(struct host1x_bo
*bo
)
25 return container_of(bo
, struct tegra_bo
, base
);
28 static void tegra_bo_put(struct host1x_bo
*bo
)
30 struct tegra_bo
*obj
= host1x_to_tegra_bo(bo
);
32 drm_gem_object_unreference_unlocked(&obj
->gem
);
35 static dma_addr_t
tegra_bo_pin(struct host1x_bo
*bo
, struct sg_table
**sgt
)
37 struct tegra_bo
*obj
= host1x_to_tegra_bo(bo
);
42 static void tegra_bo_unpin(struct host1x_bo
*bo
, struct sg_table
*sgt
)
46 static void *tegra_bo_mmap(struct host1x_bo
*bo
)
48 struct tegra_bo
*obj
= host1x_to_tegra_bo(bo
);
53 static void tegra_bo_munmap(struct host1x_bo
*bo
, void *addr
)
57 static void *tegra_bo_kmap(struct host1x_bo
*bo
, unsigned int page
)
59 struct tegra_bo
*obj
= host1x_to_tegra_bo(bo
);
61 return obj
->vaddr
+ page
* PAGE_SIZE
;
64 static void tegra_bo_kunmap(struct host1x_bo
*bo
, unsigned int page
,
69 static struct host1x_bo
*tegra_bo_get(struct host1x_bo
*bo
)
71 struct tegra_bo
*obj
= host1x_to_tegra_bo(bo
);
73 drm_gem_object_reference(&obj
->gem
);
78 static const struct host1x_bo_ops tegra_bo_ops
= {
82 .unpin
= tegra_bo_unpin
,
83 .mmap
= tegra_bo_mmap
,
84 .munmap
= tegra_bo_munmap
,
85 .kmap
= tegra_bo_kmap
,
86 .kunmap
= tegra_bo_kunmap
,
89 static int tegra_bo_iommu_map(struct tegra_drm
*tegra
, struct tegra_bo
*bo
)
91 int prot
= IOMMU_READ
| IOMMU_WRITE
;
97 bo
->mm
= kzalloc(sizeof(*bo
->mm
), GFP_KERNEL
);
101 err
= drm_mm_insert_node_generic(&tegra
->mm
, bo
->mm
, bo
->gem
.size
,
104 dev_err(tegra
->drm
->dev
, "out of I/O virtual memory: %zd\n",
109 bo
->paddr
= bo
->mm
->start
;
111 err
= iommu_map_sg(tegra
->domain
, bo
->paddr
, bo
->sgt
->sgl
,
112 bo
->sgt
->nents
, prot
);
114 dev_err(tegra
->drm
->dev
, "failed to map buffer: %zd\n", err
);
123 drm_mm_remove_node(bo
->mm
);
129 static int tegra_bo_iommu_unmap(struct tegra_drm
*tegra
, struct tegra_bo
*bo
)
134 iommu_unmap(tegra
->domain
, bo
->paddr
, bo
->size
);
135 drm_mm_remove_node(bo
->mm
);
141 static struct tegra_bo
*tegra_bo_alloc_object(struct drm_device
*drm
,
147 bo
= kzalloc(sizeof(*bo
), GFP_KERNEL
);
149 return ERR_PTR(-ENOMEM
);
151 host1x_bo_init(&bo
->base
, &tegra_bo_ops
);
152 size
= round_up(size
, PAGE_SIZE
);
154 err
= drm_gem_object_init(drm
, &bo
->gem
, size
);
158 err
= drm_gem_create_mmap_offset(&bo
->gem
);
165 drm_gem_object_release(&bo
->gem
);
171 static void tegra_bo_free(struct drm_device
*drm
, struct tegra_bo
*bo
)
174 drm_gem_put_pages(&bo
->gem
, bo
->pages
, true, true);
175 sg_free_table(bo
->sgt
);
177 } else if (bo
->vaddr
) {
178 dma_free_wc(drm
->dev
, bo
->gem
.size
, bo
->vaddr
, bo
->paddr
);
182 static int tegra_bo_get_pages(struct drm_device
*drm
, struct tegra_bo
*bo
)
184 struct scatterlist
*s
;
187 bo
->pages
= drm_gem_get_pages(&bo
->gem
);
188 if (IS_ERR(bo
->pages
))
189 return PTR_ERR(bo
->pages
);
191 bo
->num_pages
= bo
->gem
.size
>> PAGE_SHIFT
;
193 bo
->sgt
= drm_prime_pages_to_sg(bo
->pages
, bo
->num_pages
);
198 * Fake up the SG table so that dma_sync_sg_for_device() can be used
199 * to flush the pages associated with it.
201 * TODO: Replace this by drm_clflash_sg() once it can be implemented
202 * without relying on symbols that are not exported.
204 for_each_sg(bo
->sgt
->sgl
, s
, bo
->sgt
->nents
, i
)
205 sg_dma_address(s
) = sg_phys(s
);
207 dma_sync_sg_for_device(drm
->dev
, bo
->sgt
->sgl
, bo
->sgt
->nents
,
213 drm_gem_put_pages(&bo
->gem
, bo
->pages
, false, false);
214 return PTR_ERR(bo
->sgt
);
217 static int tegra_bo_alloc(struct drm_device
*drm
, struct tegra_bo
*bo
)
219 struct tegra_drm
*tegra
= drm
->dev_private
;
223 err
= tegra_bo_get_pages(drm
, bo
);
227 err
= tegra_bo_iommu_map(tegra
, bo
);
229 tegra_bo_free(drm
, bo
);
233 size_t size
= bo
->gem
.size
;
235 bo
->vaddr
= dma_alloc_wc(drm
->dev
, size
, &bo
->paddr
,
236 GFP_KERNEL
| __GFP_NOWARN
);
239 "failed to allocate buffer of size %zu\n",
248 struct tegra_bo
*tegra_bo_create(struct drm_device
*drm
, size_t size
,
254 bo
= tegra_bo_alloc_object(drm
, size
);
258 err
= tegra_bo_alloc(drm
, bo
);
262 if (flags
& DRM_TEGRA_GEM_CREATE_TILED
)
263 bo
->tiling
.mode
= TEGRA_BO_TILING_MODE_TILED
;
265 if (flags
& DRM_TEGRA_GEM_CREATE_BOTTOM_UP
)
266 bo
->flags
|= TEGRA_BO_BOTTOM_UP
;
271 drm_gem_object_release(&bo
->gem
);
276 struct tegra_bo
*tegra_bo_create_with_handle(struct drm_file
*file
,
277 struct drm_device
*drm
,
285 bo
= tegra_bo_create(drm
, size
, flags
);
289 err
= drm_gem_handle_create(file
, &bo
->gem
, handle
);
291 tegra_bo_free_object(&bo
->gem
);
295 drm_gem_object_unreference_unlocked(&bo
->gem
);
300 static struct tegra_bo
*tegra_bo_import(struct drm_device
*drm
,
303 struct tegra_drm
*tegra
= drm
->dev_private
;
304 struct dma_buf_attachment
*attach
;
308 bo
= tegra_bo_alloc_object(drm
, buf
->size
);
312 attach
= dma_buf_attach(buf
, drm
->dev
);
313 if (IS_ERR(attach
)) {
314 err
= PTR_ERR(attach
);
320 bo
->sgt
= dma_buf_map_attachment(attach
, DMA_TO_DEVICE
);
326 if (IS_ERR(bo
->sgt
)) {
327 err
= PTR_ERR(bo
->sgt
);
332 err
= tegra_bo_iommu_map(tegra
, bo
);
336 if (bo
->sgt
->nents
> 1) {
341 bo
->paddr
= sg_dma_address(bo
->sgt
->sgl
);
344 bo
->gem
.import_attach
= attach
;
349 if (!IS_ERR_OR_NULL(bo
->sgt
))
350 dma_buf_unmap_attachment(attach
, bo
->sgt
, DMA_TO_DEVICE
);
352 dma_buf_detach(buf
, attach
);
355 drm_gem_object_release(&bo
->gem
);
360 void tegra_bo_free_object(struct drm_gem_object
*gem
)
362 struct tegra_drm
*tegra
= gem
->dev
->dev_private
;
363 struct tegra_bo
*bo
= to_tegra_bo(gem
);
366 tegra_bo_iommu_unmap(tegra
, bo
);
368 if (gem
->import_attach
) {
369 dma_buf_unmap_attachment(gem
->import_attach
, bo
->sgt
,
371 drm_prime_gem_destroy(gem
, NULL
);
373 tegra_bo_free(gem
->dev
, bo
);
376 drm_gem_object_release(gem
);
380 int tegra_bo_dumb_create(struct drm_file
*file
, struct drm_device
*drm
,
381 struct drm_mode_create_dumb
*args
)
383 unsigned int min_pitch
= DIV_ROUND_UP(args
->width
* args
->bpp
, 8);
384 struct tegra_drm
*tegra
= drm
->dev_private
;
387 args
->pitch
= round_up(min_pitch
, tegra
->pitch_align
);
388 args
->size
= args
->pitch
* args
->height
;
390 bo
= tegra_bo_create_with_handle(file
, drm
, args
->size
, 0,
398 int tegra_bo_dumb_map_offset(struct drm_file
*file
, struct drm_device
*drm
,
399 u32 handle
, u64
*offset
)
401 struct drm_gem_object
*gem
;
404 gem
= drm_gem_object_lookup(drm
, file
, handle
);
406 dev_err(drm
->dev
, "failed to lookup GEM object\n");
410 bo
= to_tegra_bo(gem
);
412 *offset
= drm_vma_node_offset_addr(&bo
->gem
.vma_node
);
414 drm_gem_object_unreference_unlocked(gem
);
419 static int tegra_bo_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
421 struct drm_gem_object
*gem
= vma
->vm_private_data
;
422 struct tegra_bo
*bo
= to_tegra_bo(gem
);
428 return VM_FAULT_SIGBUS
;
430 offset
= ((unsigned long)vmf
->virtual_address
- vma
->vm_start
) >> PAGE_SHIFT
;
431 page
= bo
->pages
[offset
];
433 err
= vm_insert_page(vma
, (unsigned long)vmf
->virtual_address
, page
);
440 return VM_FAULT_NOPAGE
;
446 return VM_FAULT_SIGBUS
;
449 const struct vm_operations_struct tegra_bo_vm_ops
= {
450 .fault
= tegra_bo_fault
,
451 .open
= drm_gem_vm_open
,
452 .close
= drm_gem_vm_close
,
455 int tegra_drm_mmap(struct file
*file
, struct vm_area_struct
*vma
)
457 struct drm_gem_object
*gem
;
461 ret
= drm_gem_mmap(file
, vma
);
465 gem
= vma
->vm_private_data
;
466 bo
= to_tegra_bo(gem
);
469 unsigned long vm_pgoff
= vma
->vm_pgoff
;
471 vma
->vm_flags
&= ~VM_PFNMAP
;
474 ret
= dma_mmap_wc(gem
->dev
->dev
, vma
, bo
->vaddr
, bo
->paddr
,
477 drm_gem_vm_close(vma
);
481 vma
->vm_pgoff
= vm_pgoff
;
483 pgprot_t prot
= vm_get_page_prot(vma
->vm_flags
);
485 vma
->vm_flags
|= VM_MIXEDMAP
;
486 vma
->vm_flags
&= ~VM_PFNMAP
;
488 vma
->vm_page_prot
= pgprot_writecombine(prot
);
494 static struct sg_table
*
495 tegra_gem_prime_map_dma_buf(struct dma_buf_attachment
*attach
,
496 enum dma_data_direction dir
)
498 struct drm_gem_object
*gem
= attach
->dmabuf
->priv
;
499 struct tegra_bo
*bo
= to_tegra_bo(gem
);
500 struct sg_table
*sgt
;
502 sgt
= kmalloc(sizeof(*sgt
), GFP_KERNEL
);
507 struct scatterlist
*sg
;
510 if (sg_alloc_table(sgt
, bo
->num_pages
, GFP_KERNEL
))
513 for_each_sg(sgt
->sgl
, sg
, bo
->num_pages
, i
)
514 sg_set_page(sg
, bo
->pages
[i
], PAGE_SIZE
, 0);
516 if (dma_map_sg(attach
->dev
, sgt
->sgl
, sgt
->nents
, dir
) == 0)
519 if (sg_alloc_table(sgt
, 1, GFP_KERNEL
))
522 sg_dma_address(sgt
->sgl
) = bo
->paddr
;
523 sg_dma_len(sgt
->sgl
) = gem
->size
;
534 static void tegra_gem_prime_unmap_dma_buf(struct dma_buf_attachment
*attach
,
535 struct sg_table
*sgt
,
536 enum dma_data_direction dir
)
538 struct drm_gem_object
*gem
= attach
->dmabuf
->priv
;
539 struct tegra_bo
*bo
= to_tegra_bo(gem
);
542 dma_unmap_sg(attach
->dev
, sgt
->sgl
, sgt
->nents
, dir
);
548 static void tegra_gem_prime_release(struct dma_buf
*buf
)
550 drm_gem_dmabuf_release(buf
);
553 static void *tegra_gem_prime_kmap_atomic(struct dma_buf
*buf
,
559 static void tegra_gem_prime_kunmap_atomic(struct dma_buf
*buf
,
565 static void *tegra_gem_prime_kmap(struct dma_buf
*buf
, unsigned long page
)
570 static void tegra_gem_prime_kunmap(struct dma_buf
*buf
, unsigned long page
,
575 static int tegra_gem_prime_mmap(struct dma_buf
*buf
, struct vm_area_struct
*vma
)
580 static void *tegra_gem_prime_vmap(struct dma_buf
*buf
)
582 struct drm_gem_object
*gem
= buf
->priv
;
583 struct tegra_bo
*bo
= to_tegra_bo(gem
);
588 static void tegra_gem_prime_vunmap(struct dma_buf
*buf
, void *vaddr
)
592 static const struct dma_buf_ops tegra_gem_prime_dmabuf_ops
= {
593 .map_dma_buf
= tegra_gem_prime_map_dma_buf
,
594 .unmap_dma_buf
= tegra_gem_prime_unmap_dma_buf
,
595 .release
= tegra_gem_prime_release
,
596 .kmap_atomic
= tegra_gem_prime_kmap_atomic
,
597 .kunmap_atomic
= tegra_gem_prime_kunmap_atomic
,
598 .kmap
= tegra_gem_prime_kmap
,
599 .kunmap
= tegra_gem_prime_kunmap
,
600 .mmap
= tegra_gem_prime_mmap
,
601 .vmap
= tegra_gem_prime_vmap
,
602 .vunmap
= tegra_gem_prime_vunmap
,
605 struct dma_buf
*tegra_gem_prime_export(struct drm_device
*drm
,
606 struct drm_gem_object
*gem
,
609 DEFINE_DMA_BUF_EXPORT_INFO(exp_info
);
611 exp_info
.ops
= &tegra_gem_prime_dmabuf_ops
;
612 exp_info
.size
= gem
->size
;
613 exp_info
.flags
= flags
;
616 return dma_buf_export(&exp_info
);
619 struct drm_gem_object
*tegra_gem_prime_import(struct drm_device
*drm
,
624 if (buf
->ops
== &tegra_gem_prime_dmabuf_ops
) {
625 struct drm_gem_object
*gem
= buf
->priv
;
627 if (gem
->dev
== drm
) {
628 drm_gem_object_reference(gem
);
633 bo
= tegra_bo_import(drm
, buf
);