Linux Audio

Check our new training course

Loading...
v4.6
 
  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>
 24#include <linux/dma-buf.h>
 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>
 30#include <drm/drm_vma_manager.h>
 31
 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/**
 44 * __drm_gem_cma_create - Create a GEM CMA object without allocating memory
 45 * @drm: DRM device
 46 * @size: size of the object to allocate
 
 47 *
 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.
 50 *
 51 * Returns:
 52 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
 53 * error code on failure.
 54 */
 55static struct drm_gem_cma_object *
 56__drm_gem_cma_create(struct drm_device *drm, size_t size)
 57{
 58	struct drm_gem_cma_object *cma_obj;
 59	struct drm_gem_object *gem_obj;
 60	int ret;
 61
 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)
 67		return ERR_PTR(-ENOMEM);
 
 
 
 
 68	cma_obj = container_of(gem_obj, struct drm_gem_cma_object, base);
 69
 70	ret = drm_gem_object_init(drm, gem_obj, size);
 
 
 
 
 
 
 
 71	if (ret)
 72		goto error;
 73
 74	ret = drm_gem_create_mmap_offset(gem_obj);
 75	if (ret) {
 76		drm_gem_object_release(gem_obj);
 77		goto error;
 78	}
 79
 80	return cma_obj;
 81
 82error:
 83	kfree(cma_obj);
 84	return ERR_PTR(ret);
 85}
 86
 87/**
 88 * drm_gem_cma_create - allocate an object with the given size
 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.
 95 *
 96 * Returns:
 97 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
 98 * error code on failure.
 99 */
100struct drm_gem_cma_object *drm_gem_cma_create(struct drm_device *drm,
101					      size_t size)
102{
103	struct drm_gem_cma_object *cma_obj;
104	int ret;
105
106	size = round_up(size, PAGE_SIZE);
107
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_wc(drm->dev, size, &cma_obj->paddr,
113				      GFP_KERNEL | __GFP_NOWARN);
 
 
 
 
 
 
 
114	if (!cma_obj->vaddr) {
115		dev_err(drm->dev, "failed to allocate buffer with size %zu\n",
116			size);
117		ret = -ENOMEM;
118		goto error;
119	}
120
121	return cma_obj;
122
123error:
124	drm->driver->gem_free_object(&cma_obj->base);
125	return ERR_PTR(ret);
126}
127EXPORT_SYMBOL_GPL(drm_gem_cma_create);
128
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.
140 *
141 * Returns:
142 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
143 * error code on failure.
144 */
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)
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:
174	drm->driver->gem_free_object(gem_obj);
175
176	return ERR_PTR(ret);
177}
178
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.
 
187 */
188void drm_gem_cma_free_object(struct drm_gem_object *gem_obj)
189{
190	struct drm_gem_cma_object *cma_obj;
191
192	cma_obj = to_drm_gem_cma_obj(gem_obj);
193
194	if (cma_obj->vaddr) {
 
 
 
 
195		dma_free_wc(gem_obj->dev->dev, cma_obj->base.size,
196			    cma_obj->vaddr, cma_obj->paddr);
197	} else if (gem_obj->import_attach) {
198		drm_prime_gem_destroy(gem_obj, cma_obj->sgt);
199	}
200
201	drm_gem_object_release(gem_obj);
202
203	kfree(cma_obj);
204}
205EXPORT_SYMBOL_GPL(drm_gem_cma_free_object);
206
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
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.
250 *
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 *
255 * Returns:
256 * 0 on success or a negative error code on failure.
257 */
258int drm_gem_cma_dumb_create(struct drm_file *file_priv,
259			    struct drm_device *drm,
260			    struct drm_mode_create_dumb *args)
261{
262	struct drm_gem_cma_object *cma_obj;
263
264	args->pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
265	args->size = args->pitch * args->height;
266
267	cma_obj = drm_gem_cma_create_with_handle(file_priv, drm, args->size,
268						 &args->handle);
269	return PTR_ERR_OR_ZERO(cma_obj);
270}
271EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create);
272
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.
287 */
288int drm_gem_cma_dumb_map_offset(struct drm_file *file_priv,
289				struct drm_device *drm, u32 handle,
290				u64 *offset)
291{
292	struct drm_gem_object *gem_obj;
293
294	gem_obj = drm_gem_object_lookup(drm, file_priv, handle);
295	if (!gem_obj) {
296		dev_err(drm->dev, "failed to lookup GEM object\n");
297		return -EINVAL;
298	}
299
300	*offset = drm_vma_node_offset_addr(&gem_obj->vma_node);
301
302	drm_gem_object_unreference_unlocked(gem_obj);
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
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
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_wc(cma_obj->base.dev->dev, vma, cma_obj->vaddr,
328			  cma_obj->paddr, vma->vm_end - vma->vm_start);
329	if (ret)
330		drm_gem_vm_close(vma);
331
332	return ret;
333}
334
335/**
336 * drm_gem_cma_mmap - memory-map a CMA GEM object
337 * @filp: file object
338 * @vma: VMA for the area to be mapped
339 *
340 * This function implements an augmented version of the GEM DRM file mmap
341 * operation for CMA objects: In addition to the usual GEM VMA setup it
342 * immediately faults in the entire object instead of using on-demaind
343 * faulting. Drivers which employ the CMA helpers should use this function
344 * as their ->mmap() handler in the DRM device file's file_operations
345 * structure.
346 *
347 * Returns:
348 * 0 on success or a negative error code on failure.
349 */
350int drm_gem_cma_mmap(struct file *filp, struct vm_area_struct *vma)
 
 
 
 
 
351{
352	struct drm_gem_cma_object *cma_obj;
353	struct drm_gem_object *gem_obj;
354	int ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
355
356	ret = drm_gem_mmap(filp, vma);
357	if (ret)
358		return ret;
359
360	gem_obj = vma->vm_private_data;
361	cma_obj = to_drm_gem_cma_obj(gem_obj);
362
363	return drm_gem_cma_mmap_obj(cma_obj, vma);
 
 
 
 
 
 
 
 
 
364}
365EXPORT_SYMBOL_GPL(drm_gem_cma_mmap);
 
366
367#ifdef CONFIG_DEBUG_FS
368/**
369 * drm_gem_cma_describe - describe a CMA GEM object for debugfs
370 * @cma_obj: CMA GEM object
371 * @m: debugfs file handle
 
372 *
373 * This function can be used to dump a human-readable representation of the
374 * CMA GEM object into a synthetic file.
375 */
376void drm_gem_cma_describe(struct drm_gem_cma_object *cma_obj,
377			  struct seq_file *m)
378{
379	struct drm_gem_object *obj = &cma_obj->base;
380	uint64_t off;
381
382	off = drm_vma_node_start(&obj->vma_node);
383
384	seq_printf(m, "%2d (%2d) %08llx %pad %p %zu",
385			obj->name, obj->refcount.refcount.counter,
386			off, &cma_obj->paddr, cma_obj->vaddr, obj->size);
387
388	seq_printf(m, "\n");
 
389}
390EXPORT_SYMBOL_GPL(drm_gem_cma_describe);
391#endif
392
393/**
394 * drm_gem_cma_prime_get_sg_table - provide a scatter/gather table of pinned
395 *     pages for a CMA GEM object
396 * @obj: GEM object
397 *
398 * This function exports a scatter/gather table suitable for PRIME usage by
399 * calling the standard DMA mapping API. Drivers using the CMA helpers should
400 * set this as their DRM driver's ->gem_prime_get_sg_table() callback.
401 *
402 * Returns:
403 * A pointer to the scatter/gather table of pinned pages or NULL on failure.
404 */
405struct sg_table *drm_gem_cma_prime_get_sg_table(struct drm_gem_object *obj)
406{
407	struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
408	struct sg_table *sgt;
409	int ret;
410
411	sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
412	if (!sgt)
413		return NULL;
414
415	ret = dma_get_sgtable(obj->dev->dev, sgt, cma_obj->vaddr,
416			      cma_obj->paddr, obj->size);
417	if (ret < 0)
418		goto out;
419
420	return sgt;
421
422out:
423	kfree(sgt);
424	return NULL;
425}
426EXPORT_SYMBOL_GPL(drm_gem_cma_prime_get_sg_table);
427
428/**
429 * drm_gem_cma_prime_import_sg_table - produce a CMA GEM object from another
430 *     driver's scatter/gather table of pinned pages
431 * @dev: device to import into
432 * @attach: DMA-BUF attachment
433 * @sgt: scatter/gather table of pinned pages
434 *
435 * This function imports a scatter/gather table exported via DMA-BUF by
436 * another driver. Imported buffers must be physically contiguous in memory
437 * (i.e. the scatter/gather table must contain a single entry). Drivers that
438 * use the CMA helpers should set this as their DRM driver's
439 * ->gem_prime_import_sg_table() callback.
440 *
441 * Returns:
442 * A pointer to a newly created GEM object or an ERR_PTR-encoded negative
443 * error code on failure.
444 */
445struct drm_gem_object *
446drm_gem_cma_prime_import_sg_table(struct drm_device *dev,
447				  struct dma_buf_attachment *attach,
448				  struct sg_table *sgt)
449{
450	struct drm_gem_cma_object *cma_obj;
451
452	if (sgt->nents != 1)
 
453		return ERR_PTR(-EINVAL);
454
455	/* Create a CMA GEM buffer. */
456	cma_obj = __drm_gem_cma_create(dev, attach->dmabuf->size);
457	if (IS_ERR(cma_obj))
458		return ERR_CAST(cma_obj);
459
460	cma_obj->paddr = sg_dma_address(sgt->sgl);
461	cma_obj->sgt = sgt;
462
463	DRM_DEBUG_PRIME("dma_addr = %pad, size = %zu\n", &cma_obj->paddr, attach->dmabuf->size);
464
465	return &cma_obj->base;
466}
467EXPORT_SYMBOL_GPL(drm_gem_cma_prime_import_sg_table);
468
469/**
470 * drm_gem_cma_prime_mmap - memory-map an exported CMA GEM object
 
471 * @obj: GEM object
472 * @vma: VMA for the area to be mapped
 
473 *
474 * This function maps a buffer imported via DRM PRIME into a userspace
475 * process's address space. Drivers that use the CMA helpers should set this
476 * as their DRM driver's ->gem_prime_mmap() callback.
 
 
477 *
478 * Returns:
479 * 0 on success or a negative error code on failure.
480 */
481int drm_gem_cma_prime_mmap(struct drm_gem_object *obj,
482			   struct vm_area_struct *vma)
483{
484	struct drm_gem_cma_object *cma_obj;
485	int ret;
486
487	ret = drm_gem_mmap_obj(obj, obj->size, vma);
488	if (ret < 0)
489		return ret;
490
491	cma_obj = to_drm_gem_cma_obj(obj);
492	return drm_gem_cma_mmap_obj(cma_obj, vma);
493}
494EXPORT_SYMBOL_GPL(drm_gem_cma_prime_mmap);
495
496/**
497 * drm_gem_cma_prime_vmap - map a CMA GEM object into the kernel's virtual
498 *     address space
499 * @obj: GEM object
 
500 *
501 * This function maps a buffer exported via DRM PRIME into the kernel's
502 * virtual address space. Since the CMA buffers are already mapped into the
503 * kernel virtual address space this simply returns the cached virtual
504 * address. Drivers using the CMA helpers should set this as their DRM
505 * driver's ->gem_prime_vmap() callback.
506 *
507 * Returns:
508 * The kernel virtual address of the CMA GEM object's backing store.
509 */
510void *drm_gem_cma_prime_vmap(struct drm_gem_object *obj)
511{
512	struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
513
514	return cma_obj->vaddr;
 
 
 
 
 
 
 
 
 
 
515}
516EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vmap);
517
518/**
519 * drm_gem_cma_prime_vunmap - unmap a CMA GEM object from the kernel's virtual
520 *     address space
521 * @obj: GEM object
522 * @vaddr: kernel virtual address where the CMA GEM object was mapped
 
 
 
 
 
 
 
 
 
 
523 *
524 * This function removes a buffer exported via DRM PRIME from the kernel's
525 * virtual address space. This is a no-op because CMA buffers cannot be
526 * unmapped from kernel space. Drivers using the CMA helpers should set this
527 * as their DRM driver's ->gem_prime_vunmap() callback.
528 */
529void drm_gem_cma_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
 
 
 
530{
531	/* Nothing to do */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
532}
533EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vunmap);
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * drm gem CMA (contiguous memory allocator) helper functions
  4 *
  5 * Copyright (C) 2012 Sascha Hauer, Pengutronix
  6 *
  7 * Based on Samsung Exynos code
  8 *
  9 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
 
 
 
 
 
 
 
 
 
 10 */
 11
 
 
 
 
 12#include <linux/dma-buf.h>
 13#include <linux/dma-mapping.h>
 14#include <linux/export.h>
 15#include <linux/mm.h>
 16#include <linux/mutex.h>
 17#include <linux/slab.h>
 18
 
 19#include <drm/drm.h>
 20#include <drm/drm_device.h>
 21#include <drm/drm_drv.h>
 22#include <drm/drm_gem_cma_helper.h>
 23#include <drm/drm_vma_manager.h>
 24
 25/**
 26 * DOC: cma helpers
 27 *
 28 * The Contiguous Memory Allocator reserves a pool of memory at early boot
 29 * that is used to service requests for large blocks of contiguous memory.
 30 *
 31 * The DRM GEM/CMA helpers use this allocator as a means to provide buffer
 32 * objects that are physically contiguous in memory. This is useful for
 33 * display drivers that are unable to map scattered buffers via an IOMMU.
 34 */
 35
 36static const struct drm_gem_object_funcs drm_gem_cma_default_funcs = {
 37	.free = drm_gem_cma_free_object,
 38	.print_info = drm_gem_cma_print_info,
 39	.get_sg_table = drm_gem_cma_get_sg_table,
 40	.vmap = drm_gem_cma_vmap,
 41	.mmap = drm_gem_cma_mmap,
 42	.vm_ops = &drm_gem_cma_vm_ops,
 43};
 44
 45/**
 46 * __drm_gem_cma_create - Create a GEM CMA object without allocating memory
 47 * @drm: DRM device
 48 * @size: size of the object to allocate
 49 * @private: true if used for internal purposes
 50 *
 51 * This function creates and initializes a GEM CMA object of the given size,
 52 * but doesn't allocate any memory to back the object.
 53 *
 54 * Returns:
 55 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
 56 * error code on failure.
 57 */
 58static struct drm_gem_cma_object *
 59__drm_gem_cma_create(struct drm_device *drm, size_t size, bool private)
 60{
 61	struct drm_gem_cma_object *cma_obj;
 62	struct drm_gem_object *gem_obj;
 63	int ret = 0;
 64
 65	if (drm->driver->gem_create_object)
 66		gem_obj = drm->driver->gem_create_object(drm, size);
 67	else
 68		gem_obj = kzalloc(sizeof(*cma_obj), GFP_KERNEL);
 69	if (!gem_obj)
 70		return ERR_PTR(-ENOMEM);
 71
 72	if (!gem_obj->funcs)
 73		gem_obj->funcs = &drm_gem_cma_default_funcs;
 74
 75	cma_obj = container_of(gem_obj, struct drm_gem_cma_object, base);
 76
 77	if (private) {
 78		drm_gem_private_object_init(drm, gem_obj, size);
 79
 80		/* Always use writecombine for dma-buf mappings */
 81		cma_obj->map_noncoherent = false;
 82	} else {
 83		ret = drm_gem_object_init(drm, gem_obj, size);
 84	}
 85	if (ret)
 86		goto error;
 87
 88	ret = drm_gem_create_mmap_offset(gem_obj);
 89	if (ret) {
 90		drm_gem_object_release(gem_obj);
 91		goto error;
 92	}
 93
 94	return cma_obj;
 95
 96error:
 97	kfree(cma_obj);
 98	return ERR_PTR(ret);
 99}
100
101/**
102 * drm_gem_cma_create - allocate an object with the given size
103 * @drm: DRM device
104 * @size: size of the object to allocate
105 *
106 * This function creates a CMA GEM object and allocates a contiguous chunk of
107 * memory as backing store.
 
108 *
109 * Returns:
110 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
111 * error code on failure.
112 */
113struct drm_gem_cma_object *drm_gem_cma_create(struct drm_device *drm,
114					      size_t size)
115{
116	struct drm_gem_cma_object *cma_obj;
117	int ret;
118
119	size = round_up(size, PAGE_SIZE);
120
121	cma_obj = __drm_gem_cma_create(drm, size, false);
122	if (IS_ERR(cma_obj))
123		return cma_obj;
124
125	if (cma_obj->map_noncoherent) {
126		cma_obj->vaddr = dma_alloc_noncoherent(drm->dev, size,
127						       &cma_obj->paddr,
128						       DMA_TO_DEVICE,
129						       GFP_KERNEL | __GFP_NOWARN);
130	} else {
131		cma_obj->vaddr = dma_alloc_wc(drm->dev, size, &cma_obj->paddr,
132					      GFP_KERNEL | __GFP_NOWARN);
133	}
134	if (!cma_obj->vaddr) {
135		drm_dbg(drm, "failed to allocate buffer with size %zu\n",
136			 size);
137		ret = -ENOMEM;
138		goto error;
139	}
140
141	return cma_obj;
142
143error:
144	drm_gem_object_put(&cma_obj->base);
145	return ERR_PTR(ret);
146}
147EXPORT_SYMBOL_GPL(drm_gem_cma_create);
148
149/**
150 * drm_gem_cma_create_with_handle - allocate an object with the given size and
151 *     return a GEM handle to it
152 * @file_priv: DRM file-private structure to register the handle for
153 * @drm: DRM device
154 * @size: size of the object to allocate
155 * @handle: return location for the GEM handle
156 *
157 * This function creates a CMA GEM object, allocating a physically contiguous
158 * chunk of memory as backing store. The GEM object is then added to the list
159 * of object associated with the given file and a handle to it is returned.
160 *
161 * Returns:
162 * A struct drm_gem_cma_object * on success or an ERR_PTR()-encoded negative
163 * error code on failure.
164 */
165static struct drm_gem_cma_object *
166drm_gem_cma_create_with_handle(struct drm_file *file_priv,
167			       struct drm_device *drm, size_t size,
168			       uint32_t *handle)
169{
170	struct drm_gem_cma_object *cma_obj;
171	struct drm_gem_object *gem_obj;
172	int ret;
173
174	cma_obj = drm_gem_cma_create(drm, size);
175	if (IS_ERR(cma_obj))
176		return cma_obj;
177
178	gem_obj = &cma_obj->base;
179
180	/*
181	 * allocate a id of idr table where the obj is registered
182	 * and handle has the id what user can see.
183	 */
184	ret = drm_gem_handle_create(file_priv, gem_obj, handle);
 
 
 
185	/* drop reference from allocate - handle holds it now. */
186	drm_gem_object_put(gem_obj);
187	if (ret)
188		return ERR_PTR(ret);
189
190	return cma_obj;
 
 
 
 
 
191}
192
193/**
194 * drm_gem_cma_free_object - free resources associated with a CMA GEM object
195 * @gem_obj: GEM object to free
196 *
197 * This function frees the backing memory of the CMA GEM object, cleans up the
198 * GEM object state and frees the memory used to store the object itself.
199 * If the buffer is imported and the virtual address is set, it is released.
200 * Drivers using the CMA helpers should set this as their
201 * &drm_gem_object_funcs.free callback.
202 */
203void drm_gem_cma_free_object(struct drm_gem_object *gem_obj)
204{
205	struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(gem_obj);
206	struct dma_buf_map map = DMA_BUF_MAP_INIT_VADDR(cma_obj->vaddr);
 
207
208	if (gem_obj->import_attach) {
209		if (cma_obj->vaddr)
210			dma_buf_vunmap(gem_obj->import_attach->dmabuf, &map);
211		drm_prime_gem_destroy(gem_obj, cma_obj->sgt);
212	} else if (cma_obj->vaddr) {
213		dma_free_wc(gem_obj->dev->dev, cma_obj->base.size,
214			    cma_obj->vaddr, cma_obj->paddr);
 
 
215	}
216
217	drm_gem_object_release(gem_obj);
218
219	kfree(cma_obj);
220}
221EXPORT_SYMBOL_GPL(drm_gem_cma_free_object);
222
223/**
224 * drm_gem_cma_dumb_create_internal - create a dumb buffer object
225 * @file_priv: DRM file-private structure to create the dumb buffer for
226 * @drm: DRM device
227 * @args: IOCTL data
228 *
229 * This aligns the pitch and size arguments to the minimum required. This is
230 * an internal helper that can be wrapped by a driver to account for hardware
231 * with more specific alignment requirements. It should not be used directly
232 * as their &drm_driver.dumb_create callback.
233 *
234 * Returns:
235 * 0 on success or a negative error code on failure.
236 */
237int drm_gem_cma_dumb_create_internal(struct drm_file *file_priv,
238				     struct drm_device *drm,
239				     struct drm_mode_create_dumb *args)
240{
241	unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
242	struct drm_gem_cma_object *cma_obj;
243
244	if (args->pitch < min_pitch)
245		args->pitch = min_pitch;
246
247	if (args->size < args->pitch * args->height)
248		args->size = args->pitch * args->height;
249
250	cma_obj = drm_gem_cma_create_with_handle(file_priv, drm, args->size,
251						 &args->handle);
252	return PTR_ERR_OR_ZERO(cma_obj);
253}
254EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create_internal);
255
256/**
257 * drm_gem_cma_dumb_create - create a dumb buffer object
258 * @file_priv: DRM file-private structure to create the dumb buffer for
259 * @drm: DRM device
260 * @args: IOCTL data
261 *
262 * This function computes the pitch of the dumb buffer and rounds it up to an
263 * integer number of bytes per pixel. Drivers for hardware that doesn't have
264 * any additional restrictions on the pitch can directly use this function as
265 * their &drm_driver.dumb_create callback.
266 *
267 * For hardware with additional restrictions, drivers can adjust the fields
268 * set up by userspace and pass the IOCTL data along to the
269 * drm_gem_cma_dumb_create_internal() function.
270 *
271 * Returns:
272 * 0 on success or a negative error code on failure.
273 */
274int drm_gem_cma_dumb_create(struct drm_file *file_priv,
275			    struct drm_device *drm,
276			    struct drm_mode_create_dumb *args)
277{
278	struct drm_gem_cma_object *cma_obj;
279
280	args->pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
281	args->size = args->pitch * args->height;
282
283	cma_obj = drm_gem_cma_create_with_handle(file_priv, drm, args->size,
284						 &args->handle);
285	return PTR_ERR_OR_ZERO(cma_obj);
286}
287EXPORT_SYMBOL_GPL(drm_gem_cma_dumb_create);
288
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
289const struct vm_operations_struct drm_gem_cma_vm_ops = {
290	.open = drm_gem_vm_open,
291	.close = drm_gem_vm_close,
292};
293EXPORT_SYMBOL_GPL(drm_gem_cma_vm_ops);
294
295#ifndef CONFIG_MMU
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
296/**
297 * drm_gem_cma_get_unmapped_area - propose address for mapping in noMMU cases
298 * @filp: file object
299 * @addr: memory address
300 * @len: buffer size
301 * @pgoff: page offset
302 * @flags: memory flags
303 *
304 * This function is used in noMMU platforms to propose address mapping
305 * for a given buffer.
306 * It's intended to be used as a direct handler for the struct
307 * &file_operations.get_unmapped_area operation.
308 *
309 * Returns:
310 * mapping address on success or a negative error code on failure.
311 */
312unsigned long drm_gem_cma_get_unmapped_area(struct file *filp,
313					    unsigned long addr,
314					    unsigned long len,
315					    unsigned long pgoff,
316					    unsigned long flags)
317{
318	struct drm_gem_cma_object *cma_obj;
319	struct drm_gem_object *obj = NULL;
320	struct drm_file *priv = filp->private_data;
321	struct drm_device *dev = priv->minor->dev;
322	struct drm_vma_offset_node *node;
323
324	if (drm_dev_is_unplugged(dev))
325		return -ENODEV;
326
327	drm_vma_offset_lock_lookup(dev->vma_offset_manager);
328	node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager,
329						  pgoff,
330						  len >> PAGE_SHIFT);
331	if (likely(node)) {
332		obj = container_of(node, struct drm_gem_object, vma_node);
333		/*
334		 * When the object is being freed, after it hits 0-refcnt it
335		 * proceeds to tear down the object. In the process it will
336		 * attempt to remove the VMA offset and so acquire this
337		 * mgr->vm_lock.  Therefore if we find an object with a 0-refcnt
338		 * that matches our range, we know it is in the process of being
339		 * destroyed and will be freed as soon as we release the lock -
340		 * so we have to check for the 0-refcnted object and treat it as
341		 * invalid.
342		 */
343		if (!kref_get_unless_zero(&obj->refcount))
344			obj = NULL;
345	}
346
347	drm_vma_offset_unlock_lookup(dev->vma_offset_manager);
 
 
348
349	if (!obj)
350		return -EINVAL;
351
352	if (!drm_vma_node_is_allowed(node, priv)) {
353		drm_gem_object_put(obj);
354		return -EACCES;
355	}
356
357	cma_obj = to_drm_gem_cma_obj(obj);
358
359	drm_gem_object_put(obj);
360
361	return cma_obj->vaddr ? (unsigned long)cma_obj->vaddr : -EINVAL;
362}
363EXPORT_SYMBOL_GPL(drm_gem_cma_get_unmapped_area);
364#endif
365
 
366/**
367 * drm_gem_cma_print_info() - Print &drm_gem_cma_object info for debugfs
368 * @p: DRM printer
369 * @indent: Tab indentation level
370 * @obj: GEM object
371 *
372 * This function can be used as the &drm_driver->gem_print_info callback.
373 * It prints paddr and vaddr for use in e.g. debugfs output.
374 */
375void drm_gem_cma_print_info(struct drm_printer *p, unsigned int indent,
376			    const struct drm_gem_object *obj)
377{
378	const struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
 
 
 
 
 
 
 
379
380	drm_printf_indent(p, indent, "paddr=%pad\n", &cma_obj->paddr);
381	drm_printf_indent(p, indent, "vaddr=%p\n", cma_obj->vaddr);
382}
383EXPORT_SYMBOL(drm_gem_cma_print_info);
 
384
385/**
386 * drm_gem_cma_get_sg_table - provide a scatter/gather table of pinned
387 *     pages for a CMA GEM object
388 * @obj: GEM object
389 *
390 * This function exports a scatter/gather table by
391 * calling the standard DMA mapping API. Drivers using the CMA helpers should
392 * set this as their &drm_gem_object_funcs.get_sg_table callback.
393 *
394 * Returns:
395 * A pointer to the scatter/gather table of pinned pages or NULL on failure.
396 */
397struct sg_table *drm_gem_cma_get_sg_table(struct drm_gem_object *obj)
398{
399	struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
400	struct sg_table *sgt;
401	int ret;
402
403	sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
404	if (!sgt)
405		return ERR_PTR(-ENOMEM);
406
407	ret = dma_get_sgtable(obj->dev->dev, sgt, cma_obj->vaddr,
408			      cma_obj->paddr, obj->size);
409	if (ret < 0)
410		goto out;
411
412	return sgt;
413
414out:
415	kfree(sgt);
416	return ERR_PTR(ret);
417}
418EXPORT_SYMBOL_GPL(drm_gem_cma_get_sg_table);
419
420/**
421 * drm_gem_cma_prime_import_sg_table - produce a CMA GEM object from another
422 *     driver's scatter/gather table of pinned pages
423 * @dev: device to import into
424 * @attach: DMA-BUF attachment
425 * @sgt: scatter/gather table of pinned pages
426 *
427 * This function imports a scatter/gather table exported via DMA-BUF by
428 * another driver. Imported buffers must be physically contiguous in memory
429 * (i.e. the scatter/gather table must contain a single entry). Drivers that
430 * use the CMA helpers should set this as their
431 * &drm_driver.gem_prime_import_sg_table callback.
432 *
433 * Returns:
434 * A pointer to a newly created GEM object or an ERR_PTR-encoded negative
435 * error code on failure.
436 */
437struct drm_gem_object *
438drm_gem_cma_prime_import_sg_table(struct drm_device *dev,
439				  struct dma_buf_attachment *attach,
440				  struct sg_table *sgt)
441{
442	struct drm_gem_cma_object *cma_obj;
443
444	/* check if the entries in the sg_table are contiguous */
445	if (drm_prime_get_contiguous_size(sgt) < attach->dmabuf->size)
446		return ERR_PTR(-EINVAL);
447
448	/* Create a CMA GEM buffer. */
449	cma_obj = __drm_gem_cma_create(dev, attach->dmabuf->size, true);
450	if (IS_ERR(cma_obj))
451		return ERR_CAST(cma_obj);
452
453	cma_obj->paddr = sg_dma_address(sgt->sgl);
454	cma_obj->sgt = sgt;
455
456	DRM_DEBUG_PRIME("dma_addr = %pad, size = %zu\n", &cma_obj->paddr, attach->dmabuf->size);
457
458	return &cma_obj->base;
459}
460EXPORT_SYMBOL_GPL(drm_gem_cma_prime_import_sg_table);
461
462/**
463 * drm_gem_cma_vmap - map a CMA GEM object into the kernel's virtual
464 *     address space
465 * @obj: GEM object
466 * @map: Returns the kernel virtual address of the CMA GEM object's backing
467 *       store.
468 *
469 * This function maps a buffer into the kernel's
470 * virtual address space. Since the CMA buffers are already mapped into the
471 * kernel virtual address space this simply returns the cached virtual
472 * address. Drivers using the CMA helpers should set this as their DRM
473 * driver's &drm_gem_object_funcs.vmap callback.
474 *
475 * Returns:
476 * 0 on success, or a negative error code otherwise.
477 */
478int drm_gem_cma_vmap(struct drm_gem_object *obj, struct dma_buf_map *map)
 
479{
480	struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
 
481
482	dma_buf_map_set_vaddr(map, cma_obj->vaddr);
 
 
483
484	return 0;
 
485}
486EXPORT_SYMBOL_GPL(drm_gem_cma_vmap);
487
488/**
489 * drm_gem_cma_mmap - memory-map an exported CMA GEM object
 
490 * @obj: GEM object
491 * @vma: VMA for the area to be mapped
492 *
493 * This function maps a buffer into a userspace process's address space.
494 * In addition to the usual GEM VMA setup it immediately faults in the entire
495 * object instead of using on-demand faulting. Drivers that use the CMA
496 * helpers should set this as their &drm_gem_object_funcs.mmap callback.
 
497 *
498 * Returns:
499 * 0 on success or a negative error code on failure.
500 */
501int drm_gem_cma_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
502{
503	struct drm_gem_cma_object *cma_obj;
504	int ret;
505
506	/*
507	 * Clear the VM_PFNMAP flag that was set by drm_gem_mmap(), and set the
508	 * vm_pgoff (used as a fake buffer offset by DRM) to 0 as we want to map
509	 * the whole buffer.
510	 */
511	vma->vm_pgoff -= drm_vma_node_start(&obj->vma_node);
512	vma->vm_flags &= ~VM_PFNMAP;
513
514	cma_obj = to_drm_gem_cma_obj(obj);
515
516	if (cma_obj->map_noncoherent) {
517		vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
518
519		ret = dma_mmap_pages(cma_obj->base.dev->dev,
520				     vma, vma->vm_end - vma->vm_start,
521				     virt_to_page(cma_obj->vaddr));
522	} else {
523		ret = dma_mmap_wc(cma_obj->base.dev->dev, vma, cma_obj->vaddr,
524				  cma_obj->paddr, vma->vm_end - vma->vm_start);
525	}
526	if (ret)
527		drm_gem_vm_close(vma);
528
529	return ret;
530}
531EXPORT_SYMBOL_GPL(drm_gem_cma_mmap);
532
533/**
534 * drm_gem_cma_prime_import_sg_table_vmap - PRIME import another driver's
535 *	scatter/gather table and get the virtual address of the buffer
536 * @dev: DRM device
537 * @attach: DMA-BUF attachment
538 * @sgt: Scatter/gather table of pinned pages
539 *
540 * This function imports a scatter/gather table using
541 * drm_gem_cma_prime_import_sg_table() and uses dma_buf_vmap() to get the kernel
542 * virtual address. This ensures that a CMA GEM object always has its virtual
543 * address set. This address is released when the object is freed.
544 *
545 * This function can be used as the &drm_driver.gem_prime_import_sg_table
546 * callback. The &DRM_GEM_CMA_DRIVER_OPS_VMAP macro provides a shortcut to set
547 * the necessary DRM driver operations.
548 *
549 * Returns:
550 * A pointer to a newly created GEM object or an ERR_PTR-encoded negative
551 * error code on failure.
 
552 */
553struct drm_gem_object *
554drm_gem_cma_prime_import_sg_table_vmap(struct drm_device *dev,
555				       struct dma_buf_attachment *attach,
556				       struct sg_table *sgt)
557{
558	struct drm_gem_cma_object *cma_obj;
559	struct drm_gem_object *obj;
560	struct dma_buf_map map;
561	int ret;
562
563	ret = dma_buf_vmap(attach->dmabuf, &map);
564	if (ret) {
565		DRM_ERROR("Failed to vmap PRIME buffer\n");
566		return ERR_PTR(ret);
567	}
568
569	obj = drm_gem_cma_prime_import_sg_table(dev, attach, sgt);
570	if (IS_ERR(obj)) {
571		dma_buf_vunmap(attach->dmabuf, &map);
572		return obj;
573	}
574
575	cma_obj = to_drm_gem_cma_obj(obj);
576	cma_obj->vaddr = map.vaddr;
577
578	return obj;
579}
580EXPORT_SYMBOL(drm_gem_cma_prime_import_sg_table_vmap);