blob: bc323b3dbe4df01666e85f28c3bd36aa9bb4dbf2 [file] [log] [blame]
Arto Merilainende2ba662013-03-22 16:34:08 +02001/*
2 * NVIDIA Tegra DRM GEM helper functions
3 *
4 * Copyright (C) 2012 Sascha Hauer, Pengutronix
5 * Copyright (C) 2013 NVIDIA CORPORATION, All rights reserved.
6 *
7 * Based on the GEM/CMA helpers
8 *
9 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
10 *
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.
19 */
20
21#include <linux/mm.h>
22#include <linux/slab.h>
23#include <linux/mutex.h>
24#include <linux/export.h>
25#include <linux/dma-mapping.h>
26
27#include <drm/drmP.h>
28#include <drm/drm.h>
29
30#include "gem.h"
31
32static inline struct tegra_bo *host1x_to_drm_bo(struct host1x_bo *bo)
33{
34 return container_of(bo, struct tegra_bo, base);
35}
36
37static void tegra_bo_put(struct host1x_bo *bo)
38{
39 struct tegra_bo *obj = host1x_to_drm_bo(bo);
40 struct drm_device *drm = obj->gem.dev;
41
42 mutex_lock(&drm->struct_mutex);
43 drm_gem_object_unreference(&obj->gem);
44 mutex_unlock(&drm->struct_mutex);
45}
46
47static dma_addr_t tegra_bo_pin(struct host1x_bo *bo, struct sg_table **sgt)
48{
49 struct tegra_bo *obj = host1x_to_drm_bo(bo);
50
51 return obj->paddr;
52}
53
54static void tegra_bo_unpin(struct host1x_bo *bo, struct sg_table *sgt)
55{
56}
57
58static void *tegra_bo_mmap(struct host1x_bo *bo)
59{
60 struct tegra_bo *obj = host1x_to_drm_bo(bo);
61
62 return obj->vaddr;
63}
64
65static void tegra_bo_munmap(struct host1x_bo *bo, void *addr)
66{
67}
68
69static void *tegra_bo_kmap(struct host1x_bo *bo, unsigned int page)
70{
71 struct tegra_bo *obj = host1x_to_drm_bo(bo);
72
73 return obj->vaddr + page * PAGE_SIZE;
74}
75
76static void tegra_bo_kunmap(struct host1x_bo *bo, unsigned int page,
77 void *addr)
78{
79}
80
81static struct host1x_bo *tegra_bo_get(struct host1x_bo *bo)
82{
83 struct tegra_bo *obj = host1x_to_drm_bo(bo);
84 struct drm_device *drm = obj->gem.dev;
85
86 mutex_lock(&drm->struct_mutex);
87 drm_gem_object_reference(&obj->gem);
88 mutex_unlock(&drm->struct_mutex);
89
90 return bo;
91}
92
93const struct host1x_bo_ops tegra_bo_ops = {
94 .get = tegra_bo_get,
95 .put = tegra_bo_put,
96 .pin = tegra_bo_pin,
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,
102};
103
104static void tegra_bo_destroy(struct drm_device *drm, struct tegra_bo *bo)
105{
106 dma_free_writecombine(drm->dev, bo->gem.size, bo->vaddr, bo->paddr);
107}
108
109unsigned int tegra_bo_get_mmap_offset(struct tegra_bo *bo)
110{
David Herrmann0de23972013-07-24 21:07:52 +0200111 return (unsigned int)drm_vma_node_offset_addr(&bo->gem.vma_node);
Arto Merilainende2ba662013-03-22 16:34:08 +0200112}
113
114struct tegra_bo *tegra_bo_create(struct drm_device *drm, unsigned int size)
115{
116 struct tegra_bo *bo;
117 int err;
118
119 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
120 if (!bo)
121 return ERR_PTR(-ENOMEM);
122
123 host1x_bo_init(&bo->base, &tegra_bo_ops);
124 size = round_up(size, PAGE_SIZE);
125
126 bo->vaddr = dma_alloc_writecombine(drm->dev, size, &bo->paddr,
127 GFP_KERNEL | __GFP_NOWARN);
128 if (!bo->vaddr) {
129 dev_err(drm->dev, "failed to allocate buffer with size %u\n",
130 size);
131 err = -ENOMEM;
132 goto err_dma;
133 }
134
135 err = drm_gem_object_init(drm, &bo->gem, size);
136 if (err)
137 goto err_init;
138
139 err = drm_gem_create_mmap_offset(&bo->gem);
140 if (err)
141 goto err_mmap;
142
143 return bo;
144
145err_mmap:
146 drm_gem_object_release(&bo->gem);
147err_init:
148 tegra_bo_destroy(drm, bo);
149err_dma:
150 kfree(bo);
151
152 return ERR_PTR(err);
153
154}
155
156struct tegra_bo *tegra_bo_create_with_handle(struct drm_file *file,
157 struct drm_device *drm,
158 unsigned int size,
159 unsigned int *handle)
160{
161 struct tegra_bo *bo;
162 int ret;
163
164 bo = tegra_bo_create(drm, size);
165 if (IS_ERR(bo))
166 return bo;
167
168 ret = drm_gem_handle_create(file, &bo->gem, handle);
169 if (ret)
170 goto err;
171
172 drm_gem_object_unreference_unlocked(&bo->gem);
173
174 return bo;
175
176err:
177 tegra_bo_free_object(&bo->gem);
178 return ERR_PTR(ret);
179}
180
181void tegra_bo_free_object(struct drm_gem_object *gem)
182{
183 struct tegra_bo *bo = to_tegra_bo(gem);
184
David Herrmann0de23972013-07-24 21:07:52 +0200185 drm_gem_free_mmap_offset(gem);
Arto Merilainende2ba662013-03-22 16:34:08 +0200186
187 drm_gem_object_release(gem);
188 tegra_bo_destroy(gem->dev, bo);
189
190 kfree(bo);
191}
192
193int tegra_bo_dumb_create(struct drm_file *file, struct drm_device *drm,
194 struct drm_mode_create_dumb *args)
195{
196 int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
197 struct tegra_bo *bo;
198
199 if (args->pitch < min_pitch)
200 args->pitch = min_pitch;
201
202 if (args->size < args->pitch * args->height)
203 args->size = args->pitch * args->height;
204
205 bo = tegra_bo_create_with_handle(file, drm, args->size,
206 &args->handle);
207 if (IS_ERR(bo))
208 return PTR_ERR(bo);
209
210 return 0;
211}
212
213int tegra_bo_dumb_map_offset(struct drm_file *file, struct drm_device *drm,
214 uint32_t handle, uint64_t *offset)
215{
216 struct drm_gem_object *gem;
217 struct tegra_bo *bo;
218
219 mutex_lock(&drm->struct_mutex);
220
221 gem = drm_gem_object_lookup(drm, file, handle);
222 if (!gem) {
223 dev_err(drm->dev, "failed to lookup GEM object\n");
224 mutex_unlock(&drm->struct_mutex);
225 return -EINVAL;
226 }
227
228 bo = to_tegra_bo(gem);
229
230 *offset = tegra_bo_get_mmap_offset(bo);
231
232 drm_gem_object_unreference(gem);
233
234 mutex_unlock(&drm->struct_mutex);
235
236 return 0;
237}
238
239const struct vm_operations_struct tegra_bo_vm_ops = {
240 .open = drm_gem_vm_open,
241 .close = drm_gem_vm_close,
242};
243
244int tegra_drm_mmap(struct file *file, struct vm_area_struct *vma)
245{
246 struct drm_gem_object *gem;
247 struct tegra_bo *bo;
248 int ret;
249
250 ret = drm_gem_mmap(file, vma);
251 if (ret)
252 return ret;
253
254 gem = vma->vm_private_data;
255 bo = to_tegra_bo(gem);
256
257 ret = remap_pfn_range(vma, vma->vm_start, bo->paddr >> PAGE_SHIFT,
258 vma->vm_end - vma->vm_start, vma->vm_page_prot);
259 if (ret)
260 drm_gem_vm_close(vma);
261
262 return ret;
263}
264
265int tegra_bo_dumb_destroy(struct drm_file *file, struct drm_device *drm,
266 unsigned int handle)
267{
268 return drm_gem_handle_delete(file, handle);
269}