blob: 6b51bf90df0ef0c1849fdcba6e476c4dc53d3bc4 [file] [log] [blame]
Sascha Hauerb9d47452012-06-27 15:30:18 +02001/*
2 * drm gem CMA (contiguous memory allocator) helper functions
3 *
4 * Copyright (C) 2012 Sascha Hauer, Pengutronix
5 *
6 * Based on Samsung Exynos code
7 *
8 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 */
19
20#include <linux/mm.h>
21#include <linux/slab.h>
22#include <linux/mutex.h>
23#include <linux/export.h>
Laurent Pinchart71d72822013-02-17 01:57:30 +010024#include <linux/dma-buf.h>
Sascha Hauerb9d47452012-06-27 15:30:18 +020025#include <linux/dma-mapping.h>
26
27#include <drm/drmP.h>
28#include <drm/drm.h>
29#include <drm/drm_gem_cma_helper.h>
David Herrmann0de23972013-07-24 21:07:52 +020030#include <drm/drm_vma_manager.h>
Sascha Hauerb9d47452012-06-27 15:30:18 +020031
Laurent Pincharta5ed8942013-02-17 01:54:26 +010032/*
33 * __drm_gem_cma_create - Create a GEM CMA object without allocating memory
34 * @drm: The drm device
35 * @size: The GEM object size
36 *
37 * This function creates and initializes a GEM CMA object of the given size, but
38 * doesn't allocate any memory to back the object.
39 *
40 * Return a struct drm_gem_cma_object* on success or ERR_PTR values on failure.
41 */
42static struct drm_gem_cma_object *
43__drm_gem_cma_create(struct drm_device *drm, unsigned int size)
Sascha Hauerb9d47452012-06-27 15:30:18 +020044{
Laurent Pincharta5ed8942013-02-17 01:54:26 +010045 struct drm_gem_cma_object *cma_obj;
46 struct drm_gem_object *gem_obj;
47 int ret;
48
49 cma_obj = kzalloc(sizeof(*cma_obj), GFP_KERNEL);
50 if (!cma_obj)
51 return ERR_PTR(-ENOMEM);
52
53 gem_obj = &cma_obj->base;
54
55 ret = drm_gem_object_init(drm, gem_obj, size);
56 if (ret)
57 goto error;
58
59 ret = drm_gem_create_mmap_offset(gem_obj);
60 if (ret) {
61 drm_gem_object_release(gem_obj);
62 goto error;
63 }
64
65 return cma_obj;
66
67error:
68 kfree(cma_obj);
69 return ERR_PTR(ret);
Sascha Hauerb9d47452012-06-27 15:30:18 +020070}
71
72/*
73 * drm_gem_cma_create - allocate an object with the given size
74 *
75 * returns a struct drm_gem_cma_object* on success or ERR_PTR values
76 * on failure.
77 */
78struct drm_gem_cma_object *drm_gem_cma_create(struct drm_device *drm,
79 unsigned int size)
80{
81 struct drm_gem_cma_object *cma_obj;
Laurent Pinchart71d72822013-02-17 01:57:30 +010082 struct sg_table *sgt = NULL;
83 int ret;
Sascha Hauerb9d47452012-06-27 15:30:18 +020084
85 size = round_up(size, PAGE_SIZE);
86
Laurent Pincharta5ed8942013-02-17 01:54:26 +010087 cma_obj = __drm_gem_cma_create(drm, size);
88 if (IS_ERR(cma_obj))
89 return cma_obj;
Sascha Hauerb9d47452012-06-27 15:30:18 +020090
91 cma_obj->vaddr = dma_alloc_writecombine(drm->dev, size,
92 &cma_obj->paddr, GFP_KERNEL | __GFP_NOWARN);
93 if (!cma_obj->vaddr) {
Laurent Pincharta5ed8942013-02-17 01:54:26 +010094 dev_err(drm->dev, "failed to allocate buffer with size %d\n",
95 size);
Laurent Pinchart71d72822013-02-17 01:57:30 +010096 ret = -ENOMEM;
97 goto error;
Sascha Hauerb9d47452012-06-27 15:30:18 +020098 }
99
Laurent Pinchart71d72822013-02-17 01:57:30 +0100100 sgt = kzalloc(sizeof(*cma_obj->sgt), GFP_KERNEL);
101 if (sgt == NULL) {
102 ret = -ENOMEM;
103 goto error;
104 }
105
106 ret = dma_get_sgtable(drm->dev, sgt, cma_obj->vaddr,
107 cma_obj->paddr, size);
108 if (ret < 0)
109 goto error;
110
111 cma_obj->sgt = sgt;
112
Sascha Hauerb9d47452012-06-27 15:30:18 +0200113 return cma_obj;
Laurent Pinchart71d72822013-02-17 01:57:30 +0100114
115error:
116 kfree(sgt);
117 drm_gem_cma_free_object(&cma_obj->base);
118 return ERR_PTR(ret);
Sascha Hauerb9d47452012-06-27 15:30:18 +0200119}
120EXPORT_SYMBOL_GPL(drm_gem_cma_create);
121
122/*
123 * drm_gem_cma_create_with_handle - allocate an object with the given
124 * size and create a gem handle on it
125 *
126 * returns a struct drm_gem_cma_object* on success or ERR_PTR values
127 * on failure.
128 */
129static struct drm_gem_cma_object *drm_gem_cma_create_with_handle(
130 struct drm_file *file_priv,
131 struct drm_device *drm, unsigned int size,
132 unsigned int *handle)
133{
134 struct drm_gem_cma_object *cma_obj;
135 struct drm_gem_object *gem_obj;
136 int ret;
137
138 cma_obj = drm_gem_cma_create(drm, size);
139 if (IS_ERR(cma_obj))
140 return cma_obj;
141
142 gem_obj = &cma_obj->base;
143
144 /*
145 * allocate a id of idr table where the obj is registered
146 * and handle has the id what user can see.
147 */
148 ret = drm_gem_handle_create(file_priv, gem_obj, handle);
149 if (ret)
150 goto err_handle_create;
151
152 /* drop reference from allocate - handle holds it now. */
153 drm_gem_object_unreference_unlocked(gem_obj);
154
155 return cma_obj;
156
157err_handle_create:
158 drm_gem_cma_free_object(gem_obj);
159
160 return ERR_PTR(ret);
161}
162
163/*
164 * drm_gem_cma_free_object - (struct drm_driver)->gem_free_object callback
165 * function
166 */
167void drm_gem_cma_free_object(struct drm_gem_object *gem_obj)
168{
169 struct drm_gem_cma_object *cma_obj;
170
David Herrmann0de23972013-07-24 21:07:52 +0200171 drm_gem_free_mmap_offset(gem_obj);
Sascha Hauerb9d47452012-06-27 15:30:18 +0200172
Sascha Hauerb9d47452012-06-27 15:30:18 +0200173 cma_obj = to_drm_gem_cma_obj(gem_obj);
174
Laurent Pinchart71d72822013-02-17 01:57:30 +0100175 if (cma_obj->vaddr) {
Laurent Pincharta5ed8942013-02-17 01:54:26 +0100176 dma_free_writecombine(gem_obj->dev->dev, cma_obj->base.size,
177 cma_obj->vaddr, cma_obj->paddr);
Laurent Pinchart71d72822013-02-17 01:57:30 +0100178 if (cma_obj->sgt) {
179 sg_free_table(cma_obj->sgt);
180 kfree(cma_obj->sgt);
181 }
182 } else if (gem_obj->import_attach) {
183 drm_prime_gem_destroy(gem_obj, cma_obj->sgt);
184 }
Laurent Pincharta5ed8942013-02-17 01:54:26 +0100185
186 drm_gem_object_release(gem_obj);
Sascha Hauerb9d47452012-06-27 15:30:18 +0200187
188 kfree(cma_obj);
189}
190EXPORT_SYMBOL_GPL(drm_gem_cma_free_object);
191
192/*
193 * drm_gem_cma_dumb_create - (struct drm_driver)->dumb_create callback
194 * function
195 *
196 * This aligns the pitch and size arguments to the minimum required. wrap
197 * this into your own function if you need bigger alignment.
198 */
199int drm_gem_cma_dumb_create(struct drm_file *file_priv,
200 struct drm_device *dev, struct drm_mode_create_dumb *args)
201{
202 struct drm_gem_cma_object *cma_obj;
203 int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
204
205 if (args->pitch < min_pitch)
206 args->pitch = min_pitch;
207
208 if (args->size < args->pitch * args->height)
209 args->size = args->pitch * args->height;
210
211 cma_obj = drm_gem_cma_create_with_handle(file_priv, dev,
212 args->size, &args->handle);
Sachin Kamatb03cda52013-07-15 16:04:46 +0530213 return PTR_ERR_OR_ZERO(cma_obj);
Sascha Hauerb9d47452012-06-27 15:30:18 +0200214}
215EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create);
216
217/*
218 * drm_gem_cma_dumb_map_offset - (struct drm_driver)->dumb_map_offset callback
219 * function
220 */
221int drm_gem_cma_dumb_map_offset(struct drm_file *file_priv,
222 struct drm_device *drm, uint32_t handle, uint64_t *offset)
223{
224 struct drm_gem_object *gem_obj;
225
226 mutex_lock(&drm->struct_mutex);
227
228 gem_obj = drm_gem_object_lookup(drm, file_priv, handle);
229 if (!gem_obj) {
230 dev_err(drm->dev, "failed to lookup gem object\n");
231 mutex_unlock(&drm->struct_mutex);
232 return -EINVAL;
233 }
234
David Herrmann0de23972013-07-24 21:07:52 +0200235 *offset = drm_vma_node_offset_addr(&gem_obj->vma_node);
Sascha Hauerb9d47452012-06-27 15:30:18 +0200236
237 drm_gem_object_unreference(gem_obj);
238
239 mutex_unlock(&drm->struct_mutex);
240
241 return 0;
242}
243EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_map_offset);
244
245const struct vm_operations_struct drm_gem_cma_vm_ops = {
246 .open = drm_gem_vm_open,
247 .close = drm_gem_vm_close,
248};
249EXPORT_SYMBOL_GPL(drm_gem_cma_vm_ops);
250
Laurent Pinchartebaf9e02013-04-16 14:32:34 +0200251static int drm_gem_cma_mmap_obj(struct drm_gem_cma_object *cma_obj,
252 struct vm_area_struct *vma)
253{
254 int ret;
255
256 ret = remap_pfn_range(vma, vma->vm_start, cma_obj->paddr >> PAGE_SHIFT,
257 vma->vm_end - vma->vm_start, vma->vm_page_prot);
258 if (ret)
259 drm_gem_vm_close(vma);
260
261 return ret;
262}
263
Sascha Hauerb9d47452012-06-27 15:30:18 +0200264/*
265 * drm_gem_cma_mmap - (struct file_operation)->mmap callback function
266 */
267int drm_gem_cma_mmap(struct file *filp, struct vm_area_struct *vma)
268{
Sascha Hauerb9d47452012-06-27 15:30:18 +0200269 struct drm_gem_cma_object *cma_obj;
Laurent Pinchartebaf9e02013-04-16 14:32:34 +0200270 struct drm_gem_object *gem_obj;
Sascha Hauerb9d47452012-06-27 15:30:18 +0200271 int ret;
272
273 ret = drm_gem_mmap(filp, vma);
274 if (ret)
275 return ret;
276
277 gem_obj = vma->vm_private_data;
278 cma_obj = to_drm_gem_cma_obj(gem_obj);
279
Laurent Pinchartebaf9e02013-04-16 14:32:34 +0200280 return drm_gem_cma_mmap_obj(cma_obj, vma);
Sascha Hauerb9d47452012-06-27 15:30:18 +0200281}
282EXPORT_SYMBOL_GPL(drm_gem_cma_mmap);
283
Rob Clark6f646092012-12-10 10:46:43 -0600284#ifdef CONFIG_DEBUG_FS
285void drm_gem_cma_describe(struct drm_gem_cma_object *cma_obj, struct seq_file *m)
286{
287 struct drm_gem_object *obj = &cma_obj->base;
288 struct drm_device *dev = obj->dev;
David Herrmann0de23972013-07-24 21:07:52 +0200289 uint64_t off;
Rob Clark6f646092012-12-10 10:46:43 -0600290
291 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
292
David Herrmann0de23972013-07-24 21:07:52 +0200293 off = drm_vma_node_start(&obj->vma_node);
Rob Clark6f646092012-12-10 10:46:43 -0600294
295 seq_printf(m, "%2d (%2d) %08llx %08Zx %p %d",
296 obj->name, obj->refcount.refcount.counter,
297 off, cma_obj->paddr, cma_obj->vaddr, obj->size);
298
299 seq_printf(m, "\n");
300}
301EXPORT_SYMBOL_GPL(drm_gem_cma_describe);
302#endif
Laurent Pinchart71d72822013-02-17 01:57:30 +0100303
Joonyoung Shim78467dc2013-06-28 14:24:54 +0900304/* low-level interface prime helpers */
305struct sg_table *drm_gem_cma_prime_get_sg_table(struct drm_gem_object *obj)
306{
307 struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
308 struct sg_table *sgt;
309 int ret;
310
311 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
312 if (!sgt)
313 return NULL;
314
315 ret = dma_get_sgtable(obj->dev->dev, sgt, cma_obj->vaddr,
316 cma_obj->paddr, obj->size);
317 if (ret < 0)
318 goto out;
319
320 return sgt;
321
322out:
323 kfree(sgt);
324 return NULL;
325}
326EXPORT_SYMBOL_GPL(drm_gem_cma_prime_get_sg_table);
327
328struct drm_gem_object *
329drm_gem_cma_prime_import_sg_table(struct drm_device *dev, size_t size,
330 struct sg_table *sgt)
331{
332 struct drm_gem_cma_object *cma_obj;
333
334 if (sgt->nents != 1)
335 return ERR_PTR(-EINVAL);
336
337 /* Create a CMA GEM buffer. */
338 cma_obj = __drm_gem_cma_create(dev, size);
339 if (IS_ERR(cma_obj))
340 return ERR_PTR(PTR_ERR(cma_obj));
341
342 cma_obj->paddr = sg_dma_address(sgt->sgl);
343 cma_obj->sgt = sgt;
344
345 DRM_DEBUG_PRIME("dma_addr = 0x%x, size = %zu\n", cma_obj->paddr, size);
346
347 return &cma_obj->base;
348}
349EXPORT_SYMBOL_GPL(drm_gem_cma_prime_import_sg_table);
350
351int drm_gem_cma_prime_mmap(struct drm_gem_object *obj,
352 struct vm_area_struct *vma)
353{
354 struct drm_gem_cma_object *cma_obj;
355 struct drm_device *dev = obj->dev;
356 int ret;
357
358 mutex_lock(&dev->struct_mutex);
359 ret = drm_gem_mmap_obj(obj, obj->size, vma);
360 mutex_unlock(&dev->struct_mutex);
361 if (ret < 0)
362 return ret;
363
364 cma_obj = to_drm_gem_cma_obj(obj);
365 return drm_gem_cma_mmap_obj(cma_obj, vma);
366}
367EXPORT_SYMBOL_GPL(drm_gem_cma_prime_mmap);
368
369void *drm_gem_cma_prime_vmap(struct drm_gem_object *obj)
370{
371 struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
372
373 return cma_obj->vaddr;
374}
375EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vmap);
376
377void drm_gem_cma_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
378{
379 /* Nothing to do */
380}
381EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vunmap);