blob: 864b0863d042e237caa75fb2697f1414cbdebbc6 [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
Thierry Redingd7883f82014-11-03 13:56:55 +010032/**
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/**
Laurent Pincharta5ed8942013-02-17 01:54:26 +010044 * __drm_gem_cma_create - Create a GEM CMA object without allocating memory
Thierry Redingd7883f82014-11-03 13:56:55 +010045 * @drm: DRM device
46 * @size: size of the object to allocate
Laurent Pincharta5ed8942013-02-17 01:54:26 +010047 *
Thierry Redingd7883f82014-11-03 13:56:55 +010048 * This function creates and initializes a GEM CMA object of the given size,
49 * but doesn't allocate any memory to back the object.
Laurent Pincharta5ed8942013-02-17 01:54:26 +010050 *
Thierry Redingd7883f82014-11-03 13:56:55 +010051 * Returns:
52 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
53 * error code on failure.
Laurent Pincharta5ed8942013-02-17 01:54:26 +010054 */
55static struct drm_gem_cma_object *
Thierry Redingd7883f82014-11-03 13:56:55 +010056__drm_gem_cma_create(struct drm_device *drm, size_t size)
Sascha Hauerb9d47452012-06-27 15:30:18 +020057{
Laurent Pincharta5ed8942013-02-17 01:54:26 +010058 struct drm_gem_cma_object *cma_obj;
59 struct drm_gem_object *gem_obj;
60 int ret;
61
62 cma_obj = kzalloc(sizeof(*cma_obj), GFP_KERNEL);
63 if (!cma_obj)
64 return ERR_PTR(-ENOMEM);
65
66 gem_obj = &cma_obj->base;
67
68 ret = drm_gem_object_init(drm, gem_obj, size);
69 if (ret)
70 goto error;
71
72 ret = drm_gem_create_mmap_offset(gem_obj);
73 if (ret) {
74 drm_gem_object_release(gem_obj);
75 goto error;
76 }
77
78 return cma_obj;
79
80error:
81 kfree(cma_obj);
82 return ERR_PTR(ret);
Sascha Hauerb9d47452012-06-27 15:30:18 +020083}
84
Thierry Redingd7883f82014-11-03 13:56:55 +010085/**
Sascha Hauerb9d47452012-06-27 15:30:18 +020086 * drm_gem_cma_create - allocate an object with the given size
Thierry Redingd7883f82014-11-03 13:56:55 +010087 * @drm: DRM device
88 * @size: size of the object to allocate
Sascha Hauerb9d47452012-06-27 15:30:18 +020089 *
Thierry Redingd7883f82014-11-03 13:56:55 +010090 * This function creates a CMA GEM object and allocates a contiguous chunk of
91 * memory as backing store. The backing memory has the writecombine attribute
92 * set.
93 *
94 * Returns:
95 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
96 * error code on failure.
Sascha Hauerb9d47452012-06-27 15:30:18 +020097 */
98struct drm_gem_cma_object *drm_gem_cma_create(struct drm_device *drm,
Thierry Redingd7883f82014-11-03 13:56:55 +010099 size_t size)
Sascha Hauerb9d47452012-06-27 15:30:18 +0200100{
101 struct drm_gem_cma_object *cma_obj;
Laurent Pinchart71d72822013-02-17 01:57:30 +0100102 int ret;
Sascha Hauerb9d47452012-06-27 15:30:18 +0200103
104 size = round_up(size, PAGE_SIZE);
105
Laurent Pincharta5ed8942013-02-17 01:54:26 +0100106 cma_obj = __drm_gem_cma_create(drm, size);
107 if (IS_ERR(cma_obj))
108 return cma_obj;
Sascha Hauerb9d47452012-06-27 15:30:18 +0200109
110 cma_obj->vaddr = dma_alloc_writecombine(drm->dev, size,
111 &cma_obj->paddr, GFP_KERNEL | __GFP_NOWARN);
112 if (!cma_obj->vaddr) {
Laurent Pincharta5ed8942013-02-17 01:54:26 +0100113 dev_err(drm->dev, "failed to allocate buffer with size %d\n",
114 size);
Laurent Pinchart71d72822013-02-17 01:57:30 +0100115 ret = -ENOMEM;
116 goto error;
Sascha Hauerb9d47452012-06-27 15:30:18 +0200117 }
118
Sascha Hauerb9d47452012-06-27 15:30:18 +0200119 return cma_obj;
Laurent Pinchart71d72822013-02-17 01:57:30 +0100120
121error:
Laurent Pinchart71d72822013-02-17 01:57:30 +0100122 drm_gem_cma_free_object(&cma_obj->base);
123 return ERR_PTR(ret);
Sascha Hauerb9d47452012-06-27 15:30:18 +0200124}
125EXPORT_SYMBOL_GPL(drm_gem_cma_create);
126
Thierry Redingd7883f82014-11-03 13:56:55 +0100127/**
128 * drm_gem_cma_create_with_handle - allocate an object with the given size and
129 * return a GEM handle to it
130 * @file_priv: DRM file-private structure to register the handle for
131 * @drm: DRM device
132 * @size: size of the object to allocate
133 * @handle: return location for the GEM handle
Sascha Hauerb9d47452012-06-27 15:30:18 +0200134 *
Thierry Redingd7883f82014-11-03 13:56:55 +0100135 * This function creates a CMA GEM object, allocating a physically contiguous
136 * chunk of memory as backing store. The GEM object is then added to the list
137 * of object associated with the given file and a handle to it is returned.
138 *
139 * Returns:
140 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
141 * error code on failure.
Sascha Hauerb9d47452012-06-27 15:30:18 +0200142 */
Thierry Redingd7883f82014-11-03 13:56:55 +0100143static struct drm_gem_cma_object *
144drm_gem_cma_create_with_handle(struct drm_file *file_priv,
145 struct drm_device *drm, size_t size,
146 uint32_t *handle)
Sascha Hauerb9d47452012-06-27 15:30:18 +0200147{
148 struct drm_gem_cma_object *cma_obj;
149 struct drm_gem_object *gem_obj;
150 int ret;
151
152 cma_obj = drm_gem_cma_create(drm, size);
153 if (IS_ERR(cma_obj))
154 return cma_obj;
155
156 gem_obj = &cma_obj->base;
157
158 /*
159 * allocate a id of idr table where the obj is registered
160 * and handle has the id what user can see.
161 */
162 ret = drm_gem_handle_create(file_priv, gem_obj, handle);
163 if (ret)
164 goto err_handle_create;
165
166 /* drop reference from allocate - handle holds it now. */
167 drm_gem_object_unreference_unlocked(gem_obj);
168
169 return cma_obj;
170
171err_handle_create:
172 drm_gem_cma_free_object(gem_obj);
173
174 return ERR_PTR(ret);
175}
176
Thierry Redingd7883f82014-11-03 13:56:55 +0100177/**
178 * drm_gem_cma_free_object - free resources associated with a CMA GEM object
179 * @gem_obj: GEM object to free
180 *
181 * This function frees the backing memory of the CMA GEM object, cleans up the
182 * GEM object state and frees the memory used to store the object itself.
183 * Drivers using the CMA helpers should set this as their DRM driver's
184 * ->gem_free_object() callback.
Sascha Hauerb9d47452012-06-27 15:30:18 +0200185 */
186void drm_gem_cma_free_object(struct drm_gem_object *gem_obj)
187{
188 struct drm_gem_cma_object *cma_obj;
189
David Herrmann0de23972013-07-24 21:07:52 +0200190 drm_gem_free_mmap_offset(gem_obj);
Sascha Hauerb9d47452012-06-27 15:30:18 +0200191
Sascha Hauerb9d47452012-06-27 15:30:18 +0200192 cma_obj = to_drm_gem_cma_obj(gem_obj);
193
Laurent Pinchart71d72822013-02-17 01:57:30 +0100194 if (cma_obj->vaddr) {
Laurent Pincharta5ed8942013-02-17 01:54:26 +0100195 dma_free_writecombine(gem_obj->dev->dev, cma_obj->base.size,
196 cma_obj->vaddr, cma_obj->paddr);
Laurent Pinchart71d72822013-02-17 01:57:30 +0100197 } else if (gem_obj->import_attach) {
198 drm_prime_gem_destroy(gem_obj, cma_obj->sgt);
199 }
Laurent Pincharta5ed8942013-02-17 01:54:26 +0100200
201 drm_gem_object_release(gem_obj);
Sascha Hauerb9d47452012-06-27 15:30:18 +0200202
203 kfree(cma_obj);
204}
205EXPORT_SYMBOL_GPL(drm_gem_cma_free_object);
206
Thierry Redingd7883f82014-11-03 13:56:55 +0100207/**
Thierry Reding6d178292014-11-03 11:48:49 +0100208 * 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
240/**
Thierry Redingd7883f82014-11-03 13:56:55 +0100241 * 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
Sascha Hauerb9d47452012-06-27 15:30:18 +0200245 *
Thierry Redingd7883f82014-11-03 13:56:55 +0100246 * 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.
250 *
Thierry Reding6d178292014-11-03 11:48:49 +0100251 * 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 *
Thierry Redingd7883f82014-11-03 13:56:55 +0100255 * Returns:
256 * 0 on success or a negative error code on failure.
Sascha Hauerb9d47452012-06-27 15:30:18 +0200257 */
258int drm_gem_cma_dumb_create(struct drm_file *file_priv,
Thierry Redingd7883f82014-11-03 13:56:55 +0100259 struct drm_device *drm,
260 struct drm_mode_create_dumb *args)
Sascha Hauerb9d47452012-06-27 15:30:18 +0200261{
262 struct drm_gem_cma_object *cma_obj;
Sascha Hauerb9d47452012-06-27 15:30:18 +0200263
Thierry Reding6d178292014-11-03 11:48:49 +0100264 args->pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
265 args->size = args->pitch * args->height;
Sascha Hauerb9d47452012-06-27 15:30:18 +0200266
Thierry Redingd7883f82014-11-03 13:56:55 +0100267 cma_obj = drm_gem_cma_create_with_handle(file_priv, drm, args->size,
268 &args->handle);
Sachin Kamatb03cda52013-07-15 16:04:46 +0530269 return PTR_ERR_OR_ZERO(cma_obj);
Sascha Hauerb9d47452012-06-27 15:30:18 +0200270}
271EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create);
272
Thierry Redingd7883f82014-11-03 13:56:55 +0100273/**
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.
Sascha Hauerb9d47452012-06-27 15:30:18 +0200287 */
288int drm_gem_cma_dumb_map_offset(struct drm_file *file_priv,
Thierry Redingd7883f82014-11-03 13:56:55 +0100289 struct drm_device *drm, u32 handle,
290 u64 *offset)
Sascha Hauerb9d47452012-06-27 15:30:18 +0200291{
292 struct drm_gem_object *gem_obj;
293
294 mutex_lock(&drm->struct_mutex);
295
296 gem_obj = drm_gem_object_lookup(drm, file_priv, handle);
297 if (!gem_obj) {
Thierry Redingd7883f82014-11-03 13:56:55 +0100298 dev_err(drm->dev, "failed to lookup GEM object\n");
Sascha Hauerb9d47452012-06-27 15:30:18 +0200299 mutex_unlock(&drm->struct_mutex);
300 return -EINVAL;
301 }
302
David Herrmann0de23972013-07-24 21:07:52 +0200303 *offset = drm_vma_node_offset_addr(&gem_obj->vma_node);
Sascha Hauerb9d47452012-06-27 15:30:18 +0200304
305 drm_gem_object_unreference(gem_obj);
306
307 mutex_unlock(&drm->struct_mutex);
308
309 return 0;
310}
311EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_map_offset);
312
313const struct vm_operations_struct drm_gem_cma_vm_ops = {
314 .open = drm_gem_vm_open,
315 .close = drm_gem_vm_close,
316};
317EXPORT_SYMBOL_GPL(drm_gem_cma_vm_ops);
318
Laurent Pinchartebaf9e02013-04-16 14:32:34 +0200319static int drm_gem_cma_mmap_obj(struct drm_gem_cma_object *cma_obj,
320 struct vm_area_struct *vma)
321{
322 int ret;
323
Laurent Pinchartb65e64f2014-03-02 20:09:48 +0100324 /*
325 * Clear the VM_PFNMAP flag that was set by drm_gem_mmap(), and set the
326 * vm_pgoff (used as a fake buffer offset by DRM) to 0 as we want to map
327 * the whole buffer.
328 */
329 vma->vm_flags &= ~VM_PFNMAP;
330 vma->vm_pgoff = 0;
331
332 ret = dma_mmap_writecombine(cma_obj->base.dev->dev, vma,
333 cma_obj->vaddr, cma_obj->paddr,
334 vma->vm_end - vma->vm_start);
Laurent Pinchartebaf9e02013-04-16 14:32:34 +0200335 if (ret)
336 drm_gem_vm_close(vma);
337
338 return ret;
339}
340
Thierry Redingd7883f82014-11-03 13:56:55 +0100341/**
342 * drm_gem_cma_mmap - memory-map a CMA GEM object
343 * @filp: file object
344 * @vma: VMA for the area to be mapped
345 *
346 * This function implements an augmented version of the GEM DRM file mmap
347 * operation for CMA objects: In addition to the usual GEM VMA setup it
348 * immediately faults in the entire object instead of using on-demaind
349 * faulting. Drivers which employ the CMA helpers should use this function
350 * as their ->mmap() handler in the DRM device file's file_operations
351 * structure.
352 *
353 * Returns:
354 * 0 on success or a negative error code on failure.
Sascha Hauerb9d47452012-06-27 15:30:18 +0200355 */
356int drm_gem_cma_mmap(struct file *filp, struct vm_area_struct *vma)
357{
Sascha Hauerb9d47452012-06-27 15:30:18 +0200358 struct drm_gem_cma_object *cma_obj;
Laurent Pinchartebaf9e02013-04-16 14:32:34 +0200359 struct drm_gem_object *gem_obj;
Sascha Hauerb9d47452012-06-27 15:30:18 +0200360 int ret;
361
362 ret = drm_gem_mmap(filp, vma);
363 if (ret)
364 return ret;
365
366 gem_obj = vma->vm_private_data;
367 cma_obj = to_drm_gem_cma_obj(gem_obj);
368
Laurent Pinchartebaf9e02013-04-16 14:32:34 +0200369 return drm_gem_cma_mmap_obj(cma_obj, vma);
Sascha Hauerb9d47452012-06-27 15:30:18 +0200370}
371EXPORT_SYMBOL_GPL(drm_gem_cma_mmap);
372
Rob Clark6f646092012-12-10 10:46:43 -0600373#ifdef CONFIG_DEBUG_FS
Thierry Redingd7883f82014-11-03 13:56:55 +0100374/**
375 * drm_gem_cma_describe - describe a CMA GEM object for debugfs
376 * @cma_obj: CMA GEM object
377 * @m: debugfs file handle
378 *
379 * This function can be used to dump a human-readable representation of the
380 * CMA GEM object into a synthetic file.
381 */
382void drm_gem_cma_describe(struct drm_gem_cma_object *cma_obj,
383 struct seq_file *m)
Rob Clark6f646092012-12-10 10:46:43 -0600384{
385 struct drm_gem_object *obj = &cma_obj->base;
386 struct drm_device *dev = obj->dev;
David Herrmann0de23972013-07-24 21:07:52 +0200387 uint64_t off;
Rob Clark6f646092012-12-10 10:46:43 -0600388
389 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
390
David Herrmann0de23972013-07-24 21:07:52 +0200391 off = drm_vma_node_start(&obj->vma_node);
Rob Clark6f646092012-12-10 10:46:43 -0600392
Laurent Pinchartf4d1b022014-03-04 19:10:17 +0100393 seq_printf(m, "%2d (%2d) %08llx %pad %p %d",
Rob Clark6f646092012-12-10 10:46:43 -0600394 obj->name, obj->refcount.refcount.counter,
Laurent Pinchartf4d1b022014-03-04 19:10:17 +0100395 off, &cma_obj->paddr, cma_obj->vaddr, obj->size);
Rob Clark6f646092012-12-10 10:46:43 -0600396
397 seq_printf(m, "\n");
398}
399EXPORT_SYMBOL_GPL(drm_gem_cma_describe);
400#endif
Laurent Pinchart71d72822013-02-17 01:57:30 +0100401
Thierry Redingd7883f82014-11-03 13:56:55 +0100402/**
403 * drm_gem_cma_prime_get_sg_table - provide a scatter/gather table of pinned
404 * pages for a CMA GEM object
405 * @obj: GEM object
406 *
407 * This function exports a scatter/gather table suitable for PRIME usage by
408 * calling the standard DMA mapping API. Drivers using the CMA helpers should
409 * set this as their DRM driver's ->gem_prime_get_sg_table() callback.
410 *
411 * Returns:
412 * A pointer to the scatter/gather table of pinned pages or NULL on failure.
413 */
Joonyoung Shim78467dc2013-06-28 14:24:54 +0900414struct sg_table *drm_gem_cma_prime_get_sg_table(struct drm_gem_object *obj)
415{
416 struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
417 struct sg_table *sgt;
418 int ret;
419
420 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
421 if (!sgt)
422 return NULL;
423
424 ret = dma_get_sgtable(obj->dev->dev, sgt, cma_obj->vaddr,
425 cma_obj->paddr, obj->size);
426 if (ret < 0)
427 goto out;
428
429 return sgt;
430
431out:
432 kfree(sgt);
433 return NULL;
434}
435EXPORT_SYMBOL_GPL(drm_gem_cma_prime_get_sg_table);
436
Thierry Redingd7883f82014-11-03 13:56:55 +0100437/**
438 * drm_gem_cma_prime_import_sg_table - produce a CMA GEM object from another
439 * driver's scatter/gather table of pinned pages
440 * @dev: device to import into
441 * @attach: DMA-BUF attachment
442 * @sgt: scatter/gather table of pinned pages
443 *
444 * This function imports a scatter/gather table exported via DMA-BUF by
445 * another driver. Imported buffers must be physically contiguous in memory
446 * (i.e. the scatter/gather table must contain a single entry). Drivers that
447 * use the CMA helpers should set this as their DRM driver's
448 * ->gem_prime_import_sg_table() callback.
449 *
450 * Returns:
451 * A pointer to a newly created GEM object or an ERR_PTR-encoded negative
452 * error code on failure.
453 */
Joonyoung Shim78467dc2013-06-28 14:24:54 +0900454struct drm_gem_object *
Maarten Lankhorstb5e9c1a2014-01-09 11:03:14 +0100455drm_gem_cma_prime_import_sg_table(struct drm_device *dev,
456 struct dma_buf_attachment *attach,
Joonyoung Shim78467dc2013-06-28 14:24:54 +0900457 struct sg_table *sgt)
458{
459 struct drm_gem_cma_object *cma_obj;
460
461 if (sgt->nents != 1)
462 return ERR_PTR(-EINVAL);
463
464 /* Create a CMA GEM buffer. */
Maarten Lankhorstb5e9c1a2014-01-09 11:03:14 +0100465 cma_obj = __drm_gem_cma_create(dev, attach->dmabuf->size);
Joonyoung Shim78467dc2013-06-28 14:24:54 +0900466 if (IS_ERR(cma_obj))
Sachin Kamate7c36342014-07-03 17:33:33 +0530467 return ERR_CAST(cma_obj);
Joonyoung Shim78467dc2013-06-28 14:24:54 +0900468
469 cma_obj->paddr = sg_dma_address(sgt->sgl);
470 cma_obj->sgt = sgt;
471
Maarten Lankhorstb5e9c1a2014-01-09 11:03:14 +0100472 DRM_DEBUG_PRIME("dma_addr = %pad, size = %zu\n", &cma_obj->paddr, attach->dmabuf->size);
Joonyoung Shim78467dc2013-06-28 14:24:54 +0900473
474 return &cma_obj->base;
475}
476EXPORT_SYMBOL_GPL(drm_gem_cma_prime_import_sg_table);
477
Thierry Redingd7883f82014-11-03 13:56:55 +0100478/**
479 * drm_gem_cma_prime_mmap - memory-map an exported CMA GEM object
480 * @obj: GEM object
481 * @vma: VMA for the area to be mapped
482 *
483 * This function maps a buffer imported via DRM PRIME into a userspace
484 * process's address space. Drivers that use the CMA helpers should set this
485 * as their DRM driver's ->gem_prime_mmap() callback.
486 *
487 * Returns:
488 * 0 on success or a negative error code on failure.
489 */
Joonyoung Shim78467dc2013-06-28 14:24:54 +0900490int drm_gem_cma_prime_mmap(struct drm_gem_object *obj,
491 struct vm_area_struct *vma)
492{
493 struct drm_gem_cma_object *cma_obj;
494 struct drm_device *dev = obj->dev;
495 int ret;
496
497 mutex_lock(&dev->struct_mutex);
498 ret = drm_gem_mmap_obj(obj, obj->size, vma);
499 mutex_unlock(&dev->struct_mutex);
500 if (ret < 0)
501 return ret;
502
503 cma_obj = to_drm_gem_cma_obj(obj);
504 return drm_gem_cma_mmap_obj(cma_obj, vma);
505}
506EXPORT_SYMBOL_GPL(drm_gem_cma_prime_mmap);
507
Thierry Redingd7883f82014-11-03 13:56:55 +0100508/**
509 * drm_gem_cma_prime_vmap - map a CMA GEM object into the kernel's virtual
510 * address space
511 * @obj: GEM object
512 *
513 * This function maps a buffer exported via DRM PRIME into the kernel's
514 * virtual address space. Since the CMA buffers are already mapped into the
515 * kernel virtual address space this simply returns the cached virtual
516 * address. Drivers using the CMA helpers should set this as their DRM
517 * driver's ->gem_prime_vmap() callback.
518 *
519 * Returns:
520 * The kernel virtual address of the CMA GEM object's backing store.
521 */
Joonyoung Shim78467dc2013-06-28 14:24:54 +0900522void *drm_gem_cma_prime_vmap(struct drm_gem_object *obj)
523{
524 struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
525
526 return cma_obj->vaddr;
527}
528EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vmap);
529
Thierry Redingd7883f82014-11-03 13:56:55 +0100530/**
531 * drm_gem_cma_prime_vunmap - unmap a CMA GEM object from the kernel's virtual
532 * address space
533 * @obj: GEM object
534 * @vaddr: kernel virtual address where the CMA GEM object was mapped
535 *
536 * This function removes a buffer exported via DRM PRIME from the kernel's
537 * virtual address space. This is a no-op because CMA buffers cannot be
538 * unmapped from kernel space. Drivers using the CMA helpers should set this
539 * as their DRM driver's ->gem_prime_vunmap() callback.
540 */
Joonyoung Shim78467dc2013-06-28 14:24:54 +0900541void drm_gem_cma_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
542{
543 /* Nothing to do */
544}
545EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vunmap);