blob: 05c97c5350a1b3fde396ed7a0a17d0af7d85e755 [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 int ret;
Sascha Hauerb9d47452012-06-27 15:30:18 +020083
84 size = round_up(size, PAGE_SIZE);
85
Laurent Pincharta5ed8942013-02-17 01:54:26 +010086 cma_obj = __drm_gem_cma_create(drm, size);
87 if (IS_ERR(cma_obj))
88 return cma_obj;
Sascha Hauerb9d47452012-06-27 15:30:18 +020089
90 cma_obj->vaddr = dma_alloc_writecombine(drm->dev, size,
91 &cma_obj->paddr, GFP_KERNEL | __GFP_NOWARN);
92 if (!cma_obj->vaddr) {
Laurent Pincharta5ed8942013-02-17 01:54:26 +010093 dev_err(drm->dev, "failed to allocate buffer with size %d\n",
94 size);
Laurent Pinchart71d72822013-02-17 01:57:30 +010095 ret = -ENOMEM;
96 goto error;
Sascha Hauerb9d47452012-06-27 15:30:18 +020097 }
98
Sascha Hauerb9d47452012-06-27 15:30:18 +020099 return cma_obj;
Laurent Pinchart71d72822013-02-17 01:57:30 +0100100
101error:
Laurent Pinchart71d72822013-02-17 01:57:30 +0100102 drm_gem_cma_free_object(&cma_obj->base);
103 return ERR_PTR(ret);
Sascha Hauerb9d47452012-06-27 15:30:18 +0200104}
105EXPORT_SYMBOL_GPL(drm_gem_cma_create);
106
107/*
108 * drm_gem_cma_create_with_handle - allocate an object with the given
109 * size and create a gem handle on it
110 *
111 * returns a struct drm_gem_cma_object* on success or ERR_PTR values
112 * on failure.
113 */
114static struct drm_gem_cma_object *drm_gem_cma_create_with_handle(
115 struct drm_file *file_priv,
116 struct drm_device *drm, unsigned int size,
117 unsigned int *handle)
118{
119 struct drm_gem_cma_object *cma_obj;
120 struct drm_gem_object *gem_obj;
121 int ret;
122
123 cma_obj = drm_gem_cma_create(drm, size);
124 if (IS_ERR(cma_obj))
125 return cma_obj;
126
127 gem_obj = &cma_obj->base;
128
129 /*
130 * allocate a id of idr table where the obj is registered
131 * and handle has the id what user can see.
132 */
133 ret = drm_gem_handle_create(file_priv, gem_obj, handle);
134 if (ret)
135 goto err_handle_create;
136
137 /* drop reference from allocate - handle holds it now. */
138 drm_gem_object_unreference_unlocked(gem_obj);
139
140 return cma_obj;
141
142err_handle_create:
143 drm_gem_cma_free_object(gem_obj);
144
145 return ERR_PTR(ret);
146}
147
148/*
149 * drm_gem_cma_free_object - (struct drm_driver)->gem_free_object callback
150 * function
151 */
152void drm_gem_cma_free_object(struct drm_gem_object *gem_obj)
153{
154 struct drm_gem_cma_object *cma_obj;
155
David Herrmann0de23972013-07-24 21:07:52 +0200156 drm_gem_free_mmap_offset(gem_obj);
Sascha Hauerb9d47452012-06-27 15:30:18 +0200157
Sascha Hauerb9d47452012-06-27 15:30:18 +0200158 cma_obj = to_drm_gem_cma_obj(gem_obj);
159
Laurent Pinchart71d72822013-02-17 01:57:30 +0100160 if (cma_obj->vaddr) {
Laurent Pincharta5ed8942013-02-17 01:54:26 +0100161 dma_free_writecombine(gem_obj->dev->dev, cma_obj->base.size,
162 cma_obj->vaddr, cma_obj->paddr);
Laurent Pinchart71d72822013-02-17 01:57:30 +0100163 } else if (gem_obj->import_attach) {
164 drm_prime_gem_destroy(gem_obj, cma_obj->sgt);
165 }
Laurent Pincharta5ed8942013-02-17 01:54:26 +0100166
167 drm_gem_object_release(gem_obj);
Sascha Hauerb9d47452012-06-27 15:30:18 +0200168
169 kfree(cma_obj);
170}
171EXPORT_SYMBOL_GPL(drm_gem_cma_free_object);
172
173/*
174 * drm_gem_cma_dumb_create - (struct drm_driver)->dumb_create callback
175 * function
176 *
177 * This aligns the pitch and size arguments to the minimum required. wrap
178 * this into your own function if you need bigger alignment.
179 */
180int drm_gem_cma_dumb_create(struct drm_file *file_priv,
181 struct drm_device *dev, struct drm_mode_create_dumb *args)
182{
183 struct drm_gem_cma_object *cma_obj;
184 int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
185
186 if (args->pitch < min_pitch)
187 args->pitch = min_pitch;
188
189 if (args->size < args->pitch * args->height)
190 args->size = args->pitch * args->height;
191
192 cma_obj = drm_gem_cma_create_with_handle(file_priv, dev,
193 args->size, &args->handle);
Sachin Kamatb03cda52013-07-15 16:04:46 +0530194 return PTR_ERR_OR_ZERO(cma_obj);
Sascha Hauerb9d47452012-06-27 15:30:18 +0200195}
196EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create);
197
198/*
199 * drm_gem_cma_dumb_map_offset - (struct drm_driver)->dumb_map_offset callback
200 * function
201 */
202int drm_gem_cma_dumb_map_offset(struct drm_file *file_priv,
203 struct drm_device *drm, uint32_t handle, uint64_t *offset)
204{
205 struct drm_gem_object *gem_obj;
206
207 mutex_lock(&drm->struct_mutex);
208
209 gem_obj = drm_gem_object_lookup(drm, file_priv, handle);
210 if (!gem_obj) {
211 dev_err(drm->dev, "failed to lookup gem object\n");
212 mutex_unlock(&drm->struct_mutex);
213 return -EINVAL;
214 }
215
David Herrmann0de23972013-07-24 21:07:52 +0200216 *offset = drm_vma_node_offset_addr(&gem_obj->vma_node);
Sascha Hauerb9d47452012-06-27 15:30:18 +0200217
218 drm_gem_object_unreference(gem_obj);
219
220 mutex_unlock(&drm->struct_mutex);
221
222 return 0;
223}
224EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_map_offset);
225
226const struct vm_operations_struct drm_gem_cma_vm_ops = {
227 .open = drm_gem_vm_open,
228 .close = drm_gem_vm_close,
229};
230EXPORT_SYMBOL_GPL(drm_gem_cma_vm_ops);
231
Laurent Pinchartebaf9e02013-04-16 14:32:34 +0200232static int drm_gem_cma_mmap_obj(struct drm_gem_cma_object *cma_obj,
233 struct vm_area_struct *vma)
234{
235 int ret;
236
Laurent Pinchartb65e64f2014-03-02 20:09:48 +0100237 /*
238 * Clear the VM_PFNMAP flag that was set by drm_gem_mmap(), and set the
239 * vm_pgoff (used as a fake buffer offset by DRM) to 0 as we want to map
240 * the whole buffer.
241 */
242 vma->vm_flags &= ~VM_PFNMAP;
243 vma->vm_pgoff = 0;
244
245 ret = dma_mmap_writecombine(cma_obj->base.dev->dev, vma,
246 cma_obj->vaddr, cma_obj->paddr,
247 vma->vm_end - vma->vm_start);
Laurent Pinchartebaf9e02013-04-16 14:32:34 +0200248 if (ret)
249 drm_gem_vm_close(vma);
250
251 return ret;
252}
253
Sascha Hauerb9d47452012-06-27 15:30:18 +0200254/*
255 * drm_gem_cma_mmap - (struct file_operation)->mmap callback function
256 */
257int drm_gem_cma_mmap(struct file *filp, struct vm_area_struct *vma)
258{
Sascha Hauerb9d47452012-06-27 15:30:18 +0200259 struct drm_gem_cma_object *cma_obj;
Laurent Pinchartebaf9e02013-04-16 14:32:34 +0200260 struct drm_gem_object *gem_obj;
Sascha Hauerb9d47452012-06-27 15:30:18 +0200261 int ret;
262
263 ret = drm_gem_mmap(filp, vma);
264 if (ret)
265 return ret;
266
267 gem_obj = vma->vm_private_data;
268 cma_obj = to_drm_gem_cma_obj(gem_obj);
269
Laurent Pinchartebaf9e02013-04-16 14:32:34 +0200270 return drm_gem_cma_mmap_obj(cma_obj, vma);
Sascha Hauerb9d47452012-06-27 15:30:18 +0200271}
272EXPORT_SYMBOL_GPL(drm_gem_cma_mmap);
273
Rob Clark6f646092012-12-10 10:46:43 -0600274#ifdef CONFIG_DEBUG_FS
275void drm_gem_cma_describe(struct drm_gem_cma_object *cma_obj, struct seq_file *m)
276{
277 struct drm_gem_object *obj = &cma_obj->base;
278 struct drm_device *dev = obj->dev;
David Herrmann0de23972013-07-24 21:07:52 +0200279 uint64_t off;
Rob Clark6f646092012-12-10 10:46:43 -0600280
281 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
282
David Herrmann0de23972013-07-24 21:07:52 +0200283 off = drm_vma_node_start(&obj->vma_node);
Rob Clark6f646092012-12-10 10:46:43 -0600284
Laurent Pinchartf4d1b022014-03-04 19:10:17 +0100285 seq_printf(m, "%2d (%2d) %08llx %pad %p %d",
Rob Clark6f646092012-12-10 10:46:43 -0600286 obj->name, obj->refcount.refcount.counter,
Laurent Pinchartf4d1b022014-03-04 19:10:17 +0100287 off, &cma_obj->paddr, cma_obj->vaddr, obj->size);
Rob Clark6f646092012-12-10 10:46:43 -0600288
289 seq_printf(m, "\n");
290}
291EXPORT_SYMBOL_GPL(drm_gem_cma_describe);
292#endif
Laurent Pinchart71d72822013-02-17 01:57:30 +0100293
Joonyoung Shim78467dc2013-06-28 14:24:54 +0900294/* low-level interface prime helpers */
295struct sg_table *drm_gem_cma_prime_get_sg_table(struct drm_gem_object *obj)
296{
297 struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
298 struct sg_table *sgt;
299 int ret;
300
301 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
302 if (!sgt)
303 return NULL;
304
305 ret = dma_get_sgtable(obj->dev->dev, sgt, cma_obj->vaddr,
306 cma_obj->paddr, obj->size);
307 if (ret < 0)
308 goto out;
309
310 return sgt;
311
312out:
313 kfree(sgt);
314 return NULL;
315}
316EXPORT_SYMBOL_GPL(drm_gem_cma_prime_get_sg_table);
317
318struct drm_gem_object *
319drm_gem_cma_prime_import_sg_table(struct drm_device *dev, size_t size,
320 struct sg_table *sgt)
321{
322 struct drm_gem_cma_object *cma_obj;
323
324 if (sgt->nents != 1)
325 return ERR_PTR(-EINVAL);
326
327 /* Create a CMA GEM buffer. */
328 cma_obj = __drm_gem_cma_create(dev, size);
329 if (IS_ERR(cma_obj))
330 return ERR_PTR(PTR_ERR(cma_obj));
331
332 cma_obj->paddr = sg_dma_address(sgt->sgl);
333 cma_obj->sgt = sgt;
334
Laurent Pinchartf4d1b022014-03-04 19:10:17 +0100335 DRM_DEBUG_PRIME("dma_addr = %pad, size = %zu\n", &cma_obj->paddr, size);
Joonyoung Shim78467dc2013-06-28 14:24:54 +0900336
337 return &cma_obj->base;
338}
339EXPORT_SYMBOL_GPL(drm_gem_cma_prime_import_sg_table);
340
341int drm_gem_cma_prime_mmap(struct drm_gem_object *obj,
342 struct vm_area_struct *vma)
343{
344 struct drm_gem_cma_object *cma_obj;
345 struct drm_device *dev = obj->dev;
346 int ret;
347
348 mutex_lock(&dev->struct_mutex);
349 ret = drm_gem_mmap_obj(obj, obj->size, vma);
350 mutex_unlock(&dev->struct_mutex);
351 if (ret < 0)
352 return ret;
353
354 cma_obj = to_drm_gem_cma_obj(obj);
355 return drm_gem_cma_mmap_obj(cma_obj, vma);
356}
357EXPORT_SYMBOL_GPL(drm_gem_cma_prime_mmap);
358
359void *drm_gem_cma_prime_vmap(struct drm_gem_object *obj)
360{
361 struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
362
363 return cma_obj->vaddr;
364}
365EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vmap);
366
367void drm_gem_cma_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
368{
369 /* Nothing to do */
370}
371EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vunmap);