Merge tag 'staging-4.5-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[deliverable/linux.git] / drivers / gpu / drm / drm_gem_cma_helper.c
CommitLineData
b9d47450
SH
1/*
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>
71d7282a 24#include <linux/dma-buf.h>
b9d47450
SH
25#include <linux/dma-mapping.h>
26
27#include <drm/drmP.h>
28#include <drm/drm.h>
29#include <drm/drm_gem_cma_helper.h>
0de23977 30#include <drm/drm_vma_manager.h>
b9d47450 31
d7883f87
TR
32/**
33 * DOC: cma helpers
34 *
35 * The Contiguous Memory Allocator reserves a pool of memory at early boot
36 * that is used to service requests for large blocks of contiguous memory.
37 *
38 * The DRM GEM/CMA helpers use this allocator as a means to provide buffer
39 * objects that are physically contiguous in memory. This is useful for
40 * display drivers that are unable to map scattered buffers via an IOMMU.
41 */
42
43/**
a5ed8940 44 * __drm_gem_cma_create - Create a GEM CMA object without allocating memory
d7883f87
TR
45 * @drm: DRM device
46 * @size: size of the object to allocate
b9d47450 47 *
d7883f87
TR
48 * This function creates and initializes a GEM CMA object of the given size,
49 * but doesn't allocate any memory to back the object.
a5ed8940 50 *
d7883f87
TR
51 * Returns:
52 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
53 * error code on failure.
b9d47450 54 */
a5ed8940 55static struct drm_gem_cma_object *
d7883f87 56__drm_gem_cma_create(struct drm_device *drm, size_t size)
b9d47450
SH
57{
58 struct drm_gem_cma_object *cma_obj;
59 struct drm_gem_object *gem_obj;
60 int ret;
61
10028c5a
EA
62 if (drm->driver->gem_create_object)
63 gem_obj = drm->driver->gem_create_object(drm, size);
64 else
65 gem_obj = kzalloc(sizeof(*cma_obj), GFP_KERNEL);
66 if (!gem_obj)
b9d47450 67 return ERR_PTR(-ENOMEM);
10028c5a 68 cma_obj = container_of(gem_obj, struct drm_gem_cma_object, base);
b9d47450
SH
69
70 ret = drm_gem_object_init(drm, gem_obj, size);
71 if (ret)
a5ed8940 72 goto error;
b9d47450
SH
73
74 ret = drm_gem_create_mmap_offset(gem_obj);
a5ed8940
LP
75 if (ret) {
76 drm_gem_object_release(gem_obj);
77 goto error;
78 }
b9d47450
SH
79
80 return cma_obj;
81
a5ed8940
LP
82error:
83 kfree(cma_obj);
84 return ERR_PTR(ret);
85}
b9d47450 86
d7883f87 87/**
a5ed8940 88 * drm_gem_cma_create - allocate an object with the given size
d7883f87
TR
89 * @drm: DRM device
90 * @size: size of the object to allocate
91 *
92 * This function creates a CMA GEM object and allocates a contiguous chunk of
93 * memory as backing store. The backing memory has the writecombine attribute
94 * set.
a5ed8940 95 *
d7883f87
TR
96 * Returns:
97 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
98 * error code on failure.
a5ed8940
LP
99 */
100struct drm_gem_cma_object *drm_gem_cma_create(struct drm_device *drm,
d7883f87 101 size_t size)
a5ed8940
LP
102{
103 struct drm_gem_cma_object *cma_obj;
71d7282a 104 int ret;
b9d47450 105
a5ed8940 106 size = round_up(size, PAGE_SIZE);
b9d47450 107
a5ed8940
LP
108 cma_obj = __drm_gem_cma_create(drm, size);
109 if (IS_ERR(cma_obj))
110 return cma_obj;
111
112 cma_obj->vaddr = dma_alloc_writecombine(drm->dev, size,
113 &cma_obj->paddr, GFP_KERNEL | __GFP_NOWARN);
114 if (!cma_obj->vaddr) {
30c4cf3b 115 dev_err(drm->dev, "failed to allocate buffer with size %zu\n",
a5ed8940 116 size);
71d7282a
LP
117 ret = -ENOMEM;
118 goto error;
119 }
120
a5ed8940 121 return cma_obj;
71d7282a
LP
122
123error:
50cbc132 124 drm->driver->gem_free_object(&cma_obj->base);
71d7282a 125 return ERR_PTR(ret);
b9d47450
SH
126}
127EXPORT_SYMBOL_GPL(drm_gem_cma_create);
128
d7883f87
TR
129/**
130 * drm_gem_cma_create_with_handle - allocate an object with the given size and
131 * return a GEM handle to it
132 * @file_priv: DRM file-private structure to register the handle for
133 * @drm: DRM device
134 * @size: size of the object to allocate
135 * @handle: return location for the GEM handle
136 *
137 * This function creates a CMA GEM object, allocating a physically contiguous
138 * chunk of memory as backing store. The GEM object is then added to the list
139 * of object associated with the given file and a handle to it is returned.
b9d47450 140 *
d7883f87
TR
141 * Returns:
142 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
143 * error code on failure.
b9d47450 144 */
d7883f87
TR
145static struct drm_gem_cma_object *
146drm_gem_cma_create_with_handle(struct drm_file *file_priv,
147 struct drm_device *drm, size_t size,
148 uint32_t *handle)
b9d47450
SH
149{
150 struct drm_gem_cma_object *cma_obj;
151 struct drm_gem_object *gem_obj;
152 int ret;
153
154 cma_obj = drm_gem_cma_create(drm, size);
155 if (IS_ERR(cma_obj))
156 return cma_obj;
157
158 gem_obj = &cma_obj->base;
159
160 /*
161 * allocate a id of idr table where the obj is registered
162 * and handle has the id what user can see.
163 */
164 ret = drm_gem_handle_create(file_priv, gem_obj, handle);
165 if (ret)
166 goto err_handle_create;
167
168 /* drop reference from allocate - handle holds it now. */
169 drm_gem_object_unreference_unlocked(gem_obj);
170
171 return cma_obj;
172
173err_handle_create:
50cbc132 174 drm->driver->gem_free_object(gem_obj);
b9d47450
SH
175
176 return ERR_PTR(ret);
177}
178
d7883f87
TR
179/**
180 * drm_gem_cma_free_object - free resources associated with a CMA GEM object
181 * @gem_obj: GEM object to free
182 *
183 * This function frees the backing memory of the CMA GEM object, cleans up the
184 * GEM object state and frees the memory used to store the object itself.
185 * Drivers using the CMA helpers should set this as their DRM driver's
186 * ->gem_free_object() callback.
b9d47450
SH
187 */
188void drm_gem_cma_free_object(struct drm_gem_object *gem_obj)
189{
190 struct drm_gem_cma_object *cma_obj;
191
b9d47450
SH
192 cma_obj = to_drm_gem_cma_obj(gem_obj);
193
71d7282a 194 if (cma_obj->vaddr) {
a5ed8940
LP
195 dma_free_writecombine(gem_obj->dev->dev, cma_obj->base.size,
196 cma_obj->vaddr, cma_obj->paddr);
71d7282a
LP
197 } else if (gem_obj->import_attach) {
198 drm_prime_gem_destroy(gem_obj, cma_obj->sgt);
199 }
a5ed8940
LP
200
201 drm_gem_object_release(gem_obj);
b9d47450
SH
202
203 kfree(cma_obj);
204}
205EXPORT_SYMBOL_GPL(drm_gem_cma_free_object);
206
6d178291
TR
207/**
208 * drm_gem_cma_dumb_create_internal - create a dumb buffer object
209 * @file_priv: DRM file-private structure to create the dumb buffer for
210 * @drm: DRM device
211 * @args: IOCTL data
212 *
213 * This aligns the pitch and size arguments to the minimum required. This is
214 * an internal helper that can be wrapped by a driver to account for hardware
215 * with more specific alignment requirements. It should not be used directly
216 * as the ->dumb_create() callback in a DRM driver.
217 *
218 * Returns:
219 * 0 on success or a negative error code on failure.
220 */
221int drm_gem_cma_dumb_create_internal(struct drm_file *file_priv,
222 struct drm_device *drm,
223 struct drm_mode_create_dumb *args)
224{
225 unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
226 struct drm_gem_cma_object *cma_obj;
227
228 if (args->pitch < min_pitch)
229 args->pitch = min_pitch;
230
231 if (args->size < args->pitch * args->height)
232 args->size = args->pitch * args->height;
233
234 cma_obj = drm_gem_cma_create_with_handle(file_priv, drm, args->size,
235 &args->handle);
236 return PTR_ERR_OR_ZERO(cma_obj);
237}
238EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create_internal);
239
d7883f87
TR
240/**
241 * drm_gem_cma_dumb_create - create a dumb buffer object
242 * @file_priv: DRM file-private structure to create the dumb buffer for
243 * @drm: DRM device
244 * @args: IOCTL data
245 *
246 * This function computes the pitch of the dumb buffer and rounds it up to an
247 * integer number of bytes per pixel. Drivers for hardware that doesn't have
248 * any additional restrictions on the pitch can directly use this function as
249 * their ->dumb_create() callback.
b9d47450 250 *
6d178291
TR
251 * For hardware with additional restrictions, drivers can adjust the fields
252 * set up by userspace and pass the IOCTL data along to the
253 * drm_gem_cma_dumb_create_internal() function.
254 *
d7883f87
TR
255 * Returns:
256 * 0 on success or a negative error code on failure.
b9d47450
SH
257 */
258int drm_gem_cma_dumb_create(struct drm_file *file_priv,
d7883f87
TR
259 struct drm_device *drm,
260 struct drm_mode_create_dumb *args)
b9d47450
SH
261{
262 struct drm_gem_cma_object *cma_obj;
b9d47450 263
6d178291
TR
264 args->pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
265 args->size = args->pitch * args->height;
b9d47450 266
d7883f87
TR
267 cma_obj = drm_gem_cma_create_with_handle(file_priv, drm, args->size,
268 &args->handle);
b03cda51 269 return PTR_ERR_OR_ZERO(cma_obj);
b9d47450
SH
270}
271EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create);
272
d7883f87
TR
273/**
274 * drm_gem_cma_dumb_map_offset - return the fake mmap offset for a CMA GEM
275 * object
276 * @file_priv: DRM file-private structure containing the GEM object
277 * @drm: DRM device
278 * @handle: GEM object handle
279 * @offset: return location for the fake mmap offset
280 *
281 * This function look up an object by its handle and returns the fake mmap
282 * offset associated with it. Drivers using the CMA helpers should set this
283 * as their DRM driver's ->dumb_map_offset() callback.
284 *
285 * Returns:
286 * 0 on success or a negative error code on failure.
b9d47450
SH
287 */
288int drm_gem_cma_dumb_map_offset(struct drm_file *file_priv,
d7883f87
TR
289 struct drm_device *drm, u32 handle,
290 u64 *offset)
b9d47450
SH
291{
292 struct drm_gem_object *gem_obj;
293
b9d47450
SH
294 gem_obj = drm_gem_object_lookup(drm, file_priv, handle);
295 if (!gem_obj) {
d7883f87 296 dev_err(drm->dev, "failed to lookup GEM object\n");
b9d47450
SH
297 return -EINVAL;
298 }
299
0de23977 300 *offset = drm_vma_node_offset_addr(&gem_obj->vma_node);
b9d47450 301
141518b6 302 drm_gem_object_unreference_unlocked(gem_obj);
b9d47450
SH
303
304 return 0;
305}
306EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_map_offset);
307
308const struct vm_operations_struct drm_gem_cma_vm_ops = {
309 .open = drm_gem_vm_open,
310 .close = drm_gem_vm_close,
311};
312EXPORT_SYMBOL_GPL(drm_gem_cma_vm_ops);
313
ebaf9e03
LP
314static int drm_gem_cma_mmap_obj(struct drm_gem_cma_object *cma_obj,
315 struct vm_area_struct *vma)
316{
317 int ret;
318
b65e64f7
LP
319 /*
320 * Clear the VM_PFNMAP flag that was set by drm_gem_mmap(), and set the
321 * vm_pgoff (used as a fake buffer offset by DRM) to 0 as we want to map
322 * the whole buffer.
323 */
324 vma->vm_flags &= ~VM_PFNMAP;
325 vma->vm_pgoff = 0;
326
327 ret = dma_mmap_writecombine(cma_obj->base.dev->dev, vma,
328 cma_obj->vaddr, cma_obj->paddr,
329 vma->vm_end - vma->vm_start);
ebaf9e03
LP
330 if (ret)
331 drm_gem_vm_close(vma);
332
333 return ret;
334}
335
d7883f87
TR
336/**
337 * drm_gem_cma_mmap - memory-map a CMA GEM object
338 * @filp: file object
339 * @vma: VMA for the area to be mapped
340 *
341 * This function implements an augmented version of the GEM DRM file mmap
342 * operation for CMA objects: In addition to the usual GEM VMA setup it
343 * immediately faults in the entire object instead of using on-demaind
344 * faulting. Drivers which employ the CMA helpers should use this function
345 * as their ->mmap() handler in the DRM device file's file_operations
346 * structure.
347 *
348 * Returns:
349 * 0 on success or a negative error code on failure.
b9d47450
SH
350 */
351int drm_gem_cma_mmap(struct file *filp, struct vm_area_struct *vma)
352{
b9d47450 353 struct drm_gem_cma_object *cma_obj;
ebaf9e03 354 struct drm_gem_object *gem_obj;
b9d47450
SH
355 int ret;
356
357 ret = drm_gem_mmap(filp, vma);
358 if (ret)
359 return ret;
360
361 gem_obj = vma->vm_private_data;
362 cma_obj = to_drm_gem_cma_obj(gem_obj);
363
ebaf9e03 364 return drm_gem_cma_mmap_obj(cma_obj, vma);
b9d47450
SH
365}
366EXPORT_SYMBOL_GPL(drm_gem_cma_mmap);
367
6f646095 368#ifdef CONFIG_DEBUG_FS
d7883f87
TR
369/**
370 * drm_gem_cma_describe - describe a CMA GEM object for debugfs
371 * @cma_obj: CMA GEM object
372 * @m: debugfs file handle
373 *
374 * This function can be used to dump a human-readable representation of the
375 * CMA GEM object into a synthetic file.
376 */
377void drm_gem_cma_describe(struct drm_gem_cma_object *cma_obj,
378 struct seq_file *m)
6f646095
RC
379{
380 struct drm_gem_object *obj = &cma_obj->base;
0de23977 381 uint64_t off;
6f646095 382
0de23977 383 off = drm_vma_node_start(&obj->vma_node);
6f646095 384
30c4cf3b 385 seq_printf(m, "%2d (%2d) %08llx %pad %p %zu",
6f646095 386 obj->name, obj->refcount.refcount.counter,
f4d1b021 387 off, &cma_obj->paddr, cma_obj->vaddr, obj->size);
6f646095
RC
388
389 seq_printf(m, "\n");
390}
391EXPORT_SYMBOL_GPL(drm_gem_cma_describe);
392#endif
71d7282a 393
d7883f87
TR
394/**
395 * drm_gem_cma_prime_get_sg_table - provide a scatter/gather table of pinned
396 * pages for a CMA GEM object
397 * @obj: GEM object
398 *
399 * This function exports a scatter/gather table suitable for PRIME usage by
400 * calling the standard DMA mapping API. Drivers using the CMA helpers should
401 * set this as their DRM driver's ->gem_prime_get_sg_table() callback.
402 *
403 * Returns:
404 * A pointer to the scatter/gather table of pinned pages or NULL on failure.
405 */
78467dc5
JS
406struct sg_table *drm_gem_cma_prime_get_sg_table(struct drm_gem_object *obj)
407{
408 struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
409 struct sg_table *sgt;
410 int ret;
411
412 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
413 if (!sgt)
414 return NULL;
415
416 ret = dma_get_sgtable(obj->dev->dev, sgt, cma_obj->vaddr,
417 cma_obj->paddr, obj->size);
418 if (ret < 0)
419 goto out;
420
421 return sgt;
422
423out:
424 kfree(sgt);
425 return NULL;
426}
427EXPORT_SYMBOL_GPL(drm_gem_cma_prime_get_sg_table);
428
d7883f87
TR
429/**
430 * drm_gem_cma_prime_import_sg_table - produce a CMA GEM object from another
431 * driver's scatter/gather table of pinned pages
432 * @dev: device to import into
433 * @attach: DMA-BUF attachment
434 * @sgt: scatter/gather table of pinned pages
435 *
436 * This function imports a scatter/gather table exported via DMA-BUF by
437 * another driver. Imported buffers must be physically contiguous in memory
438 * (i.e. the scatter/gather table must contain a single entry). Drivers that
439 * use the CMA helpers should set this as their DRM driver's
440 * ->gem_prime_import_sg_table() callback.
441 *
442 * Returns:
443 * A pointer to a newly created GEM object or an ERR_PTR-encoded negative
444 * error code on failure.
445 */
78467dc5 446struct drm_gem_object *
b5e9c1a2
ML
447drm_gem_cma_prime_import_sg_table(struct drm_device *dev,
448 struct dma_buf_attachment *attach,
78467dc5
JS
449 struct sg_table *sgt)
450{
451 struct drm_gem_cma_object *cma_obj;
452
453 if (sgt->nents != 1)
454 return ERR_PTR(-EINVAL);
455
456 /* Create a CMA GEM buffer. */
b5e9c1a2 457 cma_obj = __drm_gem_cma_create(dev, attach->dmabuf->size);
78467dc5 458 if (IS_ERR(cma_obj))
e7c36347 459 return ERR_CAST(cma_obj);
78467dc5
JS
460
461 cma_obj->paddr = sg_dma_address(sgt->sgl);
462 cma_obj->sgt = sgt;
463
b5e9c1a2 464 DRM_DEBUG_PRIME("dma_addr = %pad, size = %zu\n", &cma_obj->paddr, attach->dmabuf->size);
78467dc5
JS
465
466 return &cma_obj->base;
467}
468EXPORT_SYMBOL_GPL(drm_gem_cma_prime_import_sg_table);
469
d7883f87
TR
470/**
471 * drm_gem_cma_prime_mmap - memory-map an exported CMA GEM object
472 * @obj: GEM object
473 * @vma: VMA for the area to be mapped
474 *
475 * This function maps a buffer imported via DRM PRIME into a userspace
476 * process's address space. Drivers that use the CMA helpers should set this
477 * as their DRM driver's ->gem_prime_mmap() callback.
478 *
479 * Returns:
480 * 0 on success or a negative error code on failure.
481 */
78467dc5
JS
482int drm_gem_cma_prime_mmap(struct drm_gem_object *obj,
483 struct vm_area_struct *vma)
484{
485 struct drm_gem_cma_object *cma_obj;
78467dc5
JS
486 int ret;
487
78467dc5 488 ret = drm_gem_mmap_obj(obj, obj->size, vma);
78467dc5
JS
489 if (ret < 0)
490 return ret;
491
492 cma_obj = to_drm_gem_cma_obj(obj);
493 return drm_gem_cma_mmap_obj(cma_obj, vma);
494}
495EXPORT_SYMBOL_GPL(drm_gem_cma_prime_mmap);
496
d7883f87
TR
497/**
498 * drm_gem_cma_prime_vmap - map a CMA GEM object into the kernel's virtual
499 * address space
500 * @obj: GEM object
501 *
502 * This function maps a buffer exported via DRM PRIME into the kernel's
503 * virtual address space. Since the CMA buffers are already mapped into the
504 * kernel virtual address space this simply returns the cached virtual
505 * address. Drivers using the CMA helpers should set this as their DRM
506 * driver's ->gem_prime_vmap() callback.
507 *
508 * Returns:
509 * The kernel virtual address of the CMA GEM object's backing store.
510 */
78467dc5
JS
511void *drm_gem_cma_prime_vmap(struct drm_gem_object *obj)
512{
513 struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
514
515 return cma_obj->vaddr;
516}
517EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vmap);
518
d7883f87
TR
519/**
520 * drm_gem_cma_prime_vunmap - unmap a CMA GEM object from the kernel's virtual
521 * address space
522 * @obj: GEM object
523 * @vaddr: kernel virtual address where the CMA GEM object was mapped
524 *
525 * This function removes a buffer exported via DRM PRIME from the kernel's
526 * virtual address space. This is a no-op because CMA buffers cannot be
527 * unmapped from kernel space. Drivers using the CMA helpers should set this
528 * as their DRM driver's ->gem_prime_vunmap() callback.
529 */
78467dc5
JS
530void drm_gem_cma_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
531{
532 /* Nothing to do */
533}
534EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vunmap);
This page took 0.191293 seconds and 5 git commands to generate.