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.
22 #include <linux/slab.h>
23 #include <linux/mutex.h>
24 #include <linux/export.h>
25 #include <linux/dma-mapping.h>
32 static inline struct tegra_bo
*host1x_to_drm_bo(struct host1x_bo
*bo
)
34 return container_of(bo
, struct tegra_bo
, base
);
37 static void tegra_bo_put(struct host1x_bo
*bo
)
39 struct tegra_bo
*obj
= host1x_to_drm_bo(bo
);
40 struct drm_device
*drm
= obj
->gem
.dev
;
42 mutex_lock(&drm
->struct_mutex
);
43 drm_gem_object_unreference(&obj
->gem
);
44 mutex_unlock(&drm
->struct_mutex
);
47 static dma_addr_t
tegra_bo_pin(struct host1x_bo
*bo
, struct sg_table
**sgt
)
49 struct tegra_bo
*obj
= host1x_to_drm_bo(bo
);
54 static void tegra_bo_unpin(struct host1x_bo
*bo
, struct sg_table
*sgt
)
58 static void *tegra_bo_mmap(struct host1x_bo
*bo
)
60 struct tegra_bo
*obj
= host1x_to_drm_bo(bo
);
65 static void tegra_bo_munmap(struct host1x_bo
*bo
, void *addr
)
69 static void *tegra_bo_kmap(struct host1x_bo
*bo
, unsigned int page
)
71 struct tegra_bo
*obj
= host1x_to_drm_bo(bo
);
73 return obj
->vaddr
+ page
* PAGE_SIZE
;
76 static void tegra_bo_kunmap(struct host1x_bo
*bo
, unsigned int page
,
81 static struct host1x_bo
*tegra_bo_get(struct host1x_bo
*bo
)
83 struct tegra_bo
*obj
= host1x_to_drm_bo(bo
);
84 struct drm_device
*drm
= obj
->gem
.dev
;
86 mutex_lock(&drm
->struct_mutex
);
87 drm_gem_object_reference(&obj
->gem
);
88 mutex_unlock(&drm
->struct_mutex
);
93 const struct host1x_bo_ops tegra_bo_ops
= {
97 .unpin
= tegra_bo_unpin
,
98 .mmap
= tegra_bo_mmap
,
99 .munmap
= tegra_bo_munmap
,
100 .kmap
= tegra_bo_kmap
,
101 .kunmap
= tegra_bo_kunmap
,
104 static void tegra_bo_destroy(struct drm_device
*drm
, struct tegra_bo
*bo
)
106 dma_free_writecombine(drm
->dev
, bo
->gem
.size
, bo
->vaddr
, bo
->paddr
);
109 unsigned int tegra_bo_get_mmap_offset(struct tegra_bo
*bo
)
111 return (unsigned int)drm_vma_node_offset_addr(&bo
->gem
.vma_node
);
114 struct tegra_bo
*tegra_bo_create(struct drm_device
*drm
, unsigned int size
)
119 bo
= kzalloc(sizeof(*bo
), GFP_KERNEL
);
121 return ERR_PTR(-ENOMEM
);
123 host1x_bo_init(&bo
->base
, &tegra_bo_ops
);
124 size
= round_up(size
, PAGE_SIZE
);
126 bo
->vaddr
= dma_alloc_writecombine(drm
->dev
, size
, &bo
->paddr
,
127 GFP_KERNEL
| __GFP_NOWARN
);
129 dev_err(drm
->dev
, "failed to allocate buffer with size %u\n",
135 err
= drm_gem_object_init(drm
, &bo
->gem
, size
);
139 err
= drm_gem_create_mmap_offset(&bo
->gem
);
146 drm_gem_object_release(&bo
->gem
);
148 tegra_bo_destroy(drm
, bo
);
156 struct tegra_bo
*tegra_bo_create_with_handle(struct drm_file
*file
,
157 struct drm_device
*drm
,
159 unsigned int *handle
)
164 bo
= tegra_bo_create(drm
, size
);
168 ret
= drm_gem_handle_create(file
, &bo
->gem
, handle
);
172 drm_gem_object_unreference_unlocked(&bo
->gem
);
177 tegra_bo_free_object(&bo
->gem
);
181 void tegra_bo_free_object(struct drm_gem_object
*gem
)
183 struct tegra_bo
*bo
= to_tegra_bo(gem
);
185 drm_gem_free_mmap_offset(gem
);
187 drm_gem_object_release(gem
);
188 tegra_bo_destroy(gem
->dev
, bo
);
193 int tegra_bo_dumb_create(struct drm_file
*file
, struct drm_device
*drm
,
194 struct drm_mode_create_dumb
*args
)
196 int min_pitch
= DIV_ROUND_UP(args
->width
* args
->bpp
, 8);
199 if (args
->pitch
< min_pitch
)
200 args
->pitch
= min_pitch
;
202 if (args
->size
< args
->pitch
* args
->height
)
203 args
->size
= args
->pitch
* args
->height
;
205 bo
= tegra_bo_create_with_handle(file
, drm
, args
->size
,
213 int tegra_bo_dumb_map_offset(struct drm_file
*file
, struct drm_device
*drm
,
214 uint32_t handle
, uint64_t *offset
)
216 struct drm_gem_object
*gem
;
219 mutex_lock(&drm
->struct_mutex
);
221 gem
= drm_gem_object_lookup(drm
, file
, handle
);
223 dev_err(drm
->dev
, "failed to lookup GEM object\n");
224 mutex_unlock(&drm
->struct_mutex
);
228 bo
= to_tegra_bo(gem
);
230 *offset
= tegra_bo_get_mmap_offset(bo
);
232 drm_gem_object_unreference(gem
);
234 mutex_unlock(&drm
->struct_mutex
);
239 const struct vm_operations_struct tegra_bo_vm_ops
= {
240 .open
= drm_gem_vm_open
,
241 .close
= drm_gem_vm_close
,
244 int tegra_drm_mmap(struct file
*file
, struct vm_area_struct
*vma
)
246 struct drm_gem_object
*gem
;
250 ret
= drm_gem_mmap(file
, vma
);
254 gem
= vma
->vm_private_data
;
255 bo
= to_tegra_bo(gem
);
257 ret
= remap_pfn_range(vma
, vma
->vm_start
, bo
->paddr
>> PAGE_SHIFT
,
258 vma
->vm_end
- vma
->vm_start
, vma
->vm_page_prot
);
260 drm_gem_vm_close(vma
);
265 int tegra_bo_dumb_destroy(struct drm_file
*file
, struct drm_device
*drm
,
268 return drm_gem_handle_delete(file
, handle
);