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
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include <linux/dma-buf.h>
22 #include <drm/tegra_drm.h>
26 static inline struct tegra_bo
*host1x_to_tegra_bo(struct host1x_bo
*bo
)
28 return container_of(bo
, struct tegra_bo
, base
);
31 static void tegra_bo_put(struct host1x_bo
*bo
)
33 struct tegra_bo
*obj
= host1x_to_tegra_bo(bo
);
34 struct drm_device
*drm
= obj
->gem
.dev
;
36 mutex_lock(&drm
->struct_mutex
);
37 drm_gem_object_unreference(&obj
->gem
);
38 mutex_unlock(&drm
->struct_mutex
);
41 static dma_addr_t
tegra_bo_pin(struct host1x_bo
*bo
, struct sg_table
**sgt
)
43 struct tegra_bo
*obj
= host1x_to_tegra_bo(bo
);
48 static void tegra_bo_unpin(struct host1x_bo
*bo
, struct sg_table
*sgt
)
52 static void *tegra_bo_mmap(struct host1x_bo
*bo
)
54 struct tegra_bo
*obj
= host1x_to_tegra_bo(bo
);
59 static void tegra_bo_munmap(struct host1x_bo
*bo
, void *addr
)
63 static void *tegra_bo_kmap(struct host1x_bo
*bo
, unsigned int page
)
65 struct tegra_bo
*obj
= host1x_to_tegra_bo(bo
);
67 return obj
->vaddr
+ page
* PAGE_SIZE
;
70 static void tegra_bo_kunmap(struct host1x_bo
*bo
, unsigned int page
,
75 static struct host1x_bo
*tegra_bo_get(struct host1x_bo
*bo
)
77 struct tegra_bo
*obj
= host1x_to_tegra_bo(bo
);
78 struct drm_device
*drm
= obj
->gem
.dev
;
80 mutex_lock(&drm
->struct_mutex
);
81 drm_gem_object_reference(&obj
->gem
);
82 mutex_unlock(&drm
->struct_mutex
);
87 static const struct host1x_bo_ops tegra_bo_ops
= {
91 .unpin
= tegra_bo_unpin
,
92 .mmap
= tegra_bo_mmap
,
93 .munmap
= tegra_bo_munmap
,
94 .kmap
= tegra_bo_kmap
,
95 .kunmap
= tegra_bo_kunmap
,
98 static void tegra_bo_destroy(struct drm_device
*drm
, struct tegra_bo
*bo
)
100 dma_free_writecombine(drm
->dev
, bo
->gem
.size
, bo
->vaddr
, bo
->paddr
);
103 struct tegra_bo
*tegra_bo_create(struct drm_device
*drm
, unsigned int size
,
109 bo
= kzalloc(sizeof(*bo
), GFP_KERNEL
);
111 return ERR_PTR(-ENOMEM
);
113 host1x_bo_init(&bo
->base
, &tegra_bo_ops
);
114 size
= round_up(size
, PAGE_SIZE
);
116 bo
->vaddr
= dma_alloc_writecombine(drm
->dev
, size
, &bo
->paddr
,
117 GFP_KERNEL
| __GFP_NOWARN
);
119 dev_err(drm
->dev
, "failed to allocate buffer with size %u\n",
125 err
= drm_gem_object_init(drm
, &bo
->gem
, size
);
129 err
= drm_gem_create_mmap_offset(&bo
->gem
);
133 if (flags
& DRM_TEGRA_GEM_CREATE_TILED
)
134 bo
->flags
|= TEGRA_BO_TILED
;
136 if (flags
& DRM_TEGRA_GEM_CREATE_BOTTOM_UP
)
137 bo
->flags
|= TEGRA_BO_BOTTOM_UP
;
142 drm_gem_object_release(&bo
->gem
);
144 tegra_bo_destroy(drm
, bo
);
151 struct tegra_bo
*tegra_bo_create_with_handle(struct drm_file
*file
,
152 struct drm_device
*drm
,
155 unsigned int *handle
)
160 bo
= tegra_bo_create(drm
, size
, flags
);
164 ret
= drm_gem_handle_create(file
, &bo
->gem
, handle
);
168 drm_gem_object_unreference_unlocked(&bo
->gem
);
173 tegra_bo_free_object(&bo
->gem
);
177 struct tegra_bo
*tegra_bo_import(struct drm_device
*drm
, struct dma_buf
*buf
)
179 struct dma_buf_attachment
*attach
;
184 bo
= kzalloc(sizeof(*bo
), GFP_KERNEL
);
186 return ERR_PTR(-ENOMEM
);
188 host1x_bo_init(&bo
->base
, &tegra_bo_ops
);
189 size
= round_up(buf
->size
, PAGE_SIZE
);
191 err
= drm_gem_object_init(drm
, &bo
->gem
, size
);
195 err
= drm_gem_create_mmap_offset(&bo
->gem
);
199 attach
= dma_buf_attach(buf
, drm
->dev
);
200 if (IS_ERR(attach
)) {
201 err
= PTR_ERR(attach
);
207 bo
->sgt
= dma_buf_map_attachment(attach
, DMA_TO_DEVICE
);
213 if (IS_ERR(bo
->sgt
)) {
214 err
= PTR_ERR(bo
->sgt
);
218 if (bo
->sgt
->nents
> 1) {
223 bo
->paddr
= sg_dma_address(bo
->sgt
->sgl
);
224 bo
->gem
.import_attach
= attach
;
229 if (!IS_ERR_OR_NULL(bo
->sgt
))
230 dma_buf_unmap_attachment(attach
, bo
->sgt
, DMA_TO_DEVICE
);
232 dma_buf_detach(buf
, attach
);
235 drm_gem_free_mmap_offset(&bo
->gem
);
237 drm_gem_object_release(&bo
->gem
);
244 void tegra_bo_free_object(struct drm_gem_object
*gem
)
246 struct tegra_bo
*bo
= to_tegra_bo(gem
);
248 if (gem
->import_attach
) {
249 dma_buf_unmap_attachment(gem
->import_attach
, bo
->sgt
,
251 drm_prime_gem_destroy(gem
, NULL
);
253 tegra_bo_destroy(gem
->dev
, bo
);
256 drm_gem_free_mmap_offset(gem
);
257 drm_gem_object_release(gem
);
262 int tegra_bo_dumb_create(struct drm_file
*file
, struct drm_device
*drm
,
263 struct drm_mode_create_dumb
*args
)
265 int min_pitch
= DIV_ROUND_UP(args
->width
* args
->bpp
, 8);
268 if (args
->pitch
< min_pitch
)
269 args
->pitch
= min_pitch
;
271 if (args
->size
< args
->pitch
* args
->height
)
272 args
->size
= args
->pitch
* args
->height
;
274 bo
= tegra_bo_create_with_handle(file
, drm
, args
->size
, 0,
282 int tegra_bo_dumb_map_offset(struct drm_file
*file
, struct drm_device
*drm
,
283 uint32_t handle
, uint64_t *offset
)
285 struct drm_gem_object
*gem
;
288 mutex_lock(&drm
->struct_mutex
);
290 gem
= drm_gem_object_lookup(drm
, file
, handle
);
292 dev_err(drm
->dev
, "failed to lookup GEM object\n");
293 mutex_unlock(&drm
->struct_mutex
);
297 bo
= to_tegra_bo(gem
);
299 *offset
= drm_vma_node_offset_addr(&bo
->gem
.vma_node
);
301 drm_gem_object_unreference(gem
);
303 mutex_unlock(&drm
->struct_mutex
);
308 const struct vm_operations_struct tegra_bo_vm_ops
= {
309 .open
= drm_gem_vm_open
,
310 .close
= drm_gem_vm_close
,
313 int tegra_drm_mmap(struct file
*file
, struct vm_area_struct
*vma
)
315 struct drm_gem_object
*gem
;
319 ret
= drm_gem_mmap(file
, vma
);
323 gem
= vma
->vm_private_data
;
324 bo
= to_tegra_bo(gem
);
326 ret
= remap_pfn_range(vma
, vma
->vm_start
, bo
->paddr
>> PAGE_SHIFT
,
327 vma
->vm_end
- vma
->vm_start
, vma
->vm_page_prot
);
329 drm_gem_vm_close(vma
);
334 static struct sg_table
*
335 tegra_gem_prime_map_dma_buf(struct dma_buf_attachment
*attach
,
336 enum dma_data_direction dir
)
338 struct drm_gem_object
*gem
= attach
->dmabuf
->priv
;
339 struct tegra_bo
*bo
= to_tegra_bo(gem
);
340 struct sg_table
*sgt
;
342 sgt
= kmalloc(sizeof(*sgt
), GFP_KERNEL
);
346 if (sg_alloc_table(sgt
, 1, GFP_KERNEL
)) {
351 sg_dma_address(sgt
->sgl
) = bo
->paddr
;
352 sg_dma_len(sgt
->sgl
) = gem
->size
;
357 static void tegra_gem_prime_unmap_dma_buf(struct dma_buf_attachment
*attach
,
358 struct sg_table
*sgt
,
359 enum dma_data_direction dir
)
365 static void tegra_gem_prime_release(struct dma_buf
*buf
)
367 drm_gem_dmabuf_release(buf
);
370 static void *tegra_gem_prime_kmap_atomic(struct dma_buf
*buf
,
376 static void tegra_gem_prime_kunmap_atomic(struct dma_buf
*buf
,
382 static void *tegra_gem_prime_kmap(struct dma_buf
*buf
, unsigned long page
)
387 static void tegra_gem_prime_kunmap(struct dma_buf
*buf
, unsigned long page
,
392 static int tegra_gem_prime_mmap(struct dma_buf
*buf
, struct vm_area_struct
*vma
)
397 static const struct dma_buf_ops tegra_gem_prime_dmabuf_ops
= {
398 .map_dma_buf
= tegra_gem_prime_map_dma_buf
,
399 .unmap_dma_buf
= tegra_gem_prime_unmap_dma_buf
,
400 .release
= tegra_gem_prime_release
,
401 .kmap_atomic
= tegra_gem_prime_kmap_atomic
,
402 .kunmap_atomic
= tegra_gem_prime_kunmap_atomic
,
403 .kmap
= tegra_gem_prime_kmap
,
404 .kunmap
= tegra_gem_prime_kunmap
,
405 .mmap
= tegra_gem_prime_mmap
,
408 struct dma_buf
*tegra_gem_prime_export(struct drm_device
*drm
,
409 struct drm_gem_object
*gem
,
412 return dma_buf_export(gem
, &tegra_gem_prime_dmabuf_ops
, gem
->size
,
416 struct drm_gem_object
*tegra_gem_prime_import(struct drm_device
*drm
,
421 if (buf
->ops
== &tegra_gem_prime_dmabuf_ops
) {
422 struct drm_gem_object
*gem
= buf
->priv
;
424 if (gem
->dev
== drm
) {
425 drm_gem_object_reference(gem
);
430 bo
= tegra_bo_import(drm
, buf
);