drm/tegra: gem: Miscellaneous cleanups
[deliverable/linux.git] / drivers / gpu / host1x / drm / gem.c
CommitLineData
de2ba664
AM
1/*
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
de2ba664
AM
21#include "gem.h"
22
3be82743 23static inline struct tegra_bo *host1x_to_tegra_bo(struct host1x_bo *bo)
de2ba664
AM
24{
25 return container_of(bo, struct tegra_bo, base);
26}
27
28static void tegra_bo_put(struct host1x_bo *bo)
29{
3be82743 30 struct tegra_bo *obj = host1x_to_tegra_bo(bo);
de2ba664
AM
31 struct drm_device *drm = obj->gem.dev;
32
33 mutex_lock(&drm->struct_mutex);
34 drm_gem_object_unreference(&obj->gem);
35 mutex_unlock(&drm->struct_mutex);
36}
37
38static dma_addr_t tegra_bo_pin(struct host1x_bo *bo, struct sg_table **sgt)
39{
3be82743 40 struct tegra_bo *obj = host1x_to_tegra_bo(bo);
de2ba664
AM
41
42 return obj->paddr;
43}
44
45static void tegra_bo_unpin(struct host1x_bo *bo, struct sg_table *sgt)
46{
47}
48
49static void *tegra_bo_mmap(struct host1x_bo *bo)
50{
3be82743 51 struct tegra_bo *obj = host1x_to_tegra_bo(bo);
de2ba664
AM
52
53 return obj->vaddr;
54}
55
56static void tegra_bo_munmap(struct host1x_bo *bo, void *addr)
57{
58}
59
60static void *tegra_bo_kmap(struct host1x_bo *bo, unsigned int page)
61{
3be82743 62 struct tegra_bo *obj = host1x_to_tegra_bo(bo);
de2ba664
AM
63
64 return obj->vaddr + page * PAGE_SIZE;
65}
66
67static void tegra_bo_kunmap(struct host1x_bo *bo, unsigned int page,
68 void *addr)
69{
70}
71
72static struct host1x_bo *tegra_bo_get(struct host1x_bo *bo)
73{
3be82743 74 struct tegra_bo *obj = host1x_to_tegra_bo(bo);
de2ba664
AM
75 struct drm_device *drm = obj->gem.dev;
76
77 mutex_lock(&drm->struct_mutex);
78 drm_gem_object_reference(&obj->gem);
79 mutex_unlock(&drm->struct_mutex);
80
81 return bo;
82}
83
84const struct host1x_bo_ops tegra_bo_ops = {
85 .get = tegra_bo_get,
86 .put = tegra_bo_put,
87 .pin = tegra_bo_pin,
88 .unpin = tegra_bo_unpin,
89 .mmap = tegra_bo_mmap,
90 .munmap = tegra_bo_munmap,
91 .kmap = tegra_bo_kmap,
92 .kunmap = tegra_bo_kunmap,
93};
94
95static void tegra_bo_destroy(struct drm_device *drm, struct tegra_bo *bo)
96{
97 dma_free_writecombine(drm->dev, bo->gem.size, bo->vaddr, bo->paddr);
98}
99
de2ba664
AM
100struct tegra_bo *tegra_bo_create(struct drm_device *drm, unsigned int size)
101{
102 struct tegra_bo *bo;
103 int err;
104
105 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
106 if (!bo)
107 return ERR_PTR(-ENOMEM);
108
109 host1x_bo_init(&bo->base, &tegra_bo_ops);
110 size = round_up(size, PAGE_SIZE);
111
112 bo->vaddr = dma_alloc_writecombine(drm->dev, size, &bo->paddr,
113 GFP_KERNEL | __GFP_NOWARN);
114 if (!bo->vaddr) {
115 dev_err(drm->dev, "failed to allocate buffer with size %u\n",
116 size);
117 err = -ENOMEM;
118 goto err_dma;
119 }
120
121 err = drm_gem_object_init(drm, &bo->gem, size);
122 if (err)
123 goto err_init;
124
125 err = drm_gem_create_mmap_offset(&bo->gem);
126 if (err)
127 goto err_mmap;
128
129 return bo;
130
131err_mmap:
132 drm_gem_object_release(&bo->gem);
133err_init:
134 tegra_bo_destroy(drm, bo);
135err_dma:
136 kfree(bo);
137
138 return ERR_PTR(err);
139
140}
141
142struct tegra_bo *tegra_bo_create_with_handle(struct drm_file *file,
3be82743
TR
143 struct drm_device *drm,
144 unsigned int size,
145 unsigned int *handle)
de2ba664
AM
146{
147 struct tegra_bo *bo;
148 int ret;
149
150 bo = tegra_bo_create(drm, size);
151 if (IS_ERR(bo))
152 return bo;
153
154 ret = drm_gem_handle_create(file, &bo->gem, handle);
155 if (ret)
156 goto err;
157
158 drm_gem_object_unreference_unlocked(&bo->gem);
159
160 return bo;
161
162err:
163 tegra_bo_free_object(&bo->gem);
164 return ERR_PTR(ret);
165}
166
167void tegra_bo_free_object(struct drm_gem_object *gem)
168{
169 struct tegra_bo *bo = to_tegra_bo(gem);
170
0de23977 171 drm_gem_free_mmap_offset(gem);
de2ba664
AM
172 drm_gem_object_release(gem);
173 tegra_bo_destroy(gem->dev, bo);
174
175 kfree(bo);
176}
177
178int tegra_bo_dumb_create(struct drm_file *file, struct drm_device *drm,
179 struct drm_mode_create_dumb *args)
180{
181 int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
182 struct tegra_bo *bo;
183
184 if (args->pitch < min_pitch)
185 args->pitch = min_pitch;
186
187 if (args->size < args->pitch * args->height)
188 args->size = args->pitch * args->height;
189
190 bo = tegra_bo_create_with_handle(file, drm, args->size,
3be82743 191 &args->handle);
de2ba664
AM
192 if (IS_ERR(bo))
193 return PTR_ERR(bo);
194
195 return 0;
196}
197
198int tegra_bo_dumb_map_offset(struct drm_file *file, struct drm_device *drm,
199 uint32_t handle, uint64_t *offset)
200{
201 struct drm_gem_object *gem;
202 struct tegra_bo *bo;
203
204 mutex_lock(&drm->struct_mutex);
205
206 gem = drm_gem_object_lookup(drm, file, handle);
207 if (!gem) {
208 dev_err(drm->dev, "failed to lookup GEM object\n");
209 mutex_unlock(&drm->struct_mutex);
210 return -EINVAL;
211 }
212
213 bo = to_tegra_bo(gem);
214
2bc7b0ca 215 *offset = drm_vma_node_offset_addr(&bo->gem.vma_node);
de2ba664
AM
216
217 drm_gem_object_unreference(gem);
218
219 mutex_unlock(&drm->struct_mutex);
220
221 return 0;
222}
223
224const struct vm_operations_struct tegra_bo_vm_ops = {
225 .open = drm_gem_vm_open,
226 .close = drm_gem_vm_close,
227};
228
229int tegra_drm_mmap(struct file *file, struct vm_area_struct *vma)
230{
231 struct drm_gem_object *gem;
232 struct tegra_bo *bo;
233 int ret;
234
235 ret = drm_gem_mmap(file, vma);
236 if (ret)
237 return ret;
238
239 gem = vma->vm_private_data;
240 bo = to_tegra_bo(gem);
241
242 ret = remap_pfn_range(vma, vma->vm_start, bo->paddr >> PAGE_SHIFT,
243 vma->vm_end - vma->vm_start, vma->vm_page_prot);
244 if (ret)
245 drm_gem_vm_close(vma);
246
247 return ret;
248}
This page took 0.059686 seconds and 5 git commands to generate.