root/drivers/gpu/drm/drm_gem_cma_helper.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. __drm_gem_cma_create
  2. drm_gem_cma_create
  3. drm_gem_cma_create_with_handle
  4. drm_gem_cma_free_object
  5. drm_gem_cma_dumb_create_internal
  6. drm_gem_cma_dumb_create
  7. drm_gem_cma_mmap_obj
  8. drm_gem_cma_mmap
  9. drm_gem_cma_get_unmapped_area
  10. drm_gem_cma_print_info
  11. drm_gem_cma_prime_get_sg_table
  12. drm_gem_cma_prime_import_sg_table
  13. drm_gem_cma_prime_mmap
  14. drm_gem_cma_prime_vmap
  15. drm_gem_cma_prime_vunmap
  16. drm_cma_gem_create_object_default_funcs
  17. drm_gem_cma_prime_import_sg_table_vmap

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

/* [<][>][^][v][top][bottom][index][help] */