Subversion Repositories Kolibri OS

Rev

Rev 6937 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright © 2008 Intel Corporation
  3.  *
  4.  * Permission is hereby granted, free of charge, to any person obtaining a
  5.  * copy of this software and associated documentation files (the "Software"),
  6.  * to deal in the Software without restriction, including without limitation
  7.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8.  * and/or sell copies of the Software, and to permit persons to whom the
  9.  * Software is furnished to do so, subject to the following conditions:
  10.  *
  11.  * The above copyright notice and this permission notice (including the next
  12.  * paragraph) shall be included in all copies or substantial portions of the
  13.  * Software.
  14.  *
  15.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  18.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21.  * IN THE SOFTWARE.
  22.  *
  23.  * Authors:
  24.  *    Eric Anholt <eric@anholt.net>
  25.  *
  26.  */
  27.  
  28. #include <linux/types.h>
  29. #include <linux/slab.h>
  30. #include <linux/mm.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/fs.h>
  33. #include <linux/file.h>
  34. #include <linux/module.h>
  35. #include <linux/shmem_fs.h>
  36. #include <linux/err.h>
  37. #include <drm/drmP.h>
  38. #include <drm/drm_vma_manager.h>
  39. #include <drm/drm_gem.h>
  40. #include "drm_internal.h"
  41.  
  42. /** @file drm_gem.c
  43.  *
  44.  * This file provides some of the base ioctls and library routines for
  45.  * the graphics memory manager implemented by each device driver.
  46.  *
  47.  * Because various devices have different requirements in terms of
  48.  * synchronization and migration strategies, implementing that is left up to
  49.  * the driver, and all that the general API provides should be generic --
  50.  * allocating objects, reading/writing data with the cpu, freeing objects.
  51.  * Even there, platform-dependent optimizations for reading/writing data with
  52.  * the CPU mean we'll likely hook those out to driver-specific calls.  However,
  53.  * the DRI2 implementation wants to have at least allocate/mmap be generic.
  54.  *
  55.  * The goal was to have swap-backed object allocation managed through
  56.  * struct file.  However, file descriptors as handles to a struct file have
  57.  * two major failings:
  58.  * - Process limits prevent more than 1024 or so being used at a time by
  59.  *   default.
  60.  * - Inability to allocate high fds will aggravate the X Server's select()
  61.  *   handling, and likely that of many GL client applications as well.
  62.  *
  63.  * This led to a plan of using our own integer IDs (called handles, following
  64.  * DRM terminology) to mimic fds, and implement the fd syscalls we need as
  65.  * ioctls.  The objects themselves will still include the struct file so
  66.  * that we can transition to fds if the required kernel infrastructure shows
  67.  * up at a later date, and as our interface with shmfs for memory allocation.
  68.  */
  69.  
  70. /*
  71.  * We make up offsets for buffer objects so we can recognize them at
  72.  * mmap time.
  73.  */
  74.  
  75. /* pgoff in mmap is an unsigned long, so we need to make sure that
  76.  * the faked up offset will fit
  77.  */
  78.  
  79. #if BITS_PER_LONG == 64
  80. #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
  81. #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
  82. #else
  83. #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1)
  84. #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16)
  85. #endif
  86.  
  87. /**
  88.  * drm_gem_init - Initialize the GEM device fields
  89.  * @dev: drm_devic structure to initialize
  90.  */
  91. int
  92. drm_gem_init(struct drm_device *dev)
  93. {
  94.         struct drm_vma_offset_manager *vma_offset_manager;
  95.  
  96.         mutex_init(&dev->object_name_lock);
  97.         idr_init(&dev->object_name_idr);
  98.  
  99.         vma_offset_manager = kzalloc(sizeof(*vma_offset_manager), GFP_KERNEL);
  100.         if (!vma_offset_manager) {
  101.                 DRM_ERROR("out of memory\n");
  102.                 return -ENOMEM;
  103.         }
  104.  
  105.         dev->vma_offset_manager = vma_offset_manager;
  106.         drm_vma_offset_manager_init(vma_offset_manager,
  107.                                     DRM_FILE_PAGE_OFFSET_START,
  108.                                     DRM_FILE_PAGE_OFFSET_SIZE);
  109.  
  110.         return 0;
  111. }
  112.  
  113. void
  114. drm_gem_destroy(struct drm_device *dev)
  115. {
  116.  
  117.         drm_vma_offset_manager_destroy(dev->vma_offset_manager);
  118.         kfree(dev->vma_offset_manager);
  119.         dev->vma_offset_manager = NULL;
  120. }
  121.  
  122. /**
  123.  * drm_gem_object_init - initialize an allocated shmem-backed GEM object
  124.  * @dev: drm_device the object should be initialized for
  125.  * @obj: drm_gem_object to initialize
  126.  * @size: object size
  127.  *
  128.  * Initialize an already allocated GEM object of the specified size with
  129.  * shmfs backing store.
  130.  */
  131. int drm_gem_object_init(struct drm_device *dev,
  132.                         struct drm_gem_object *obj, size_t size)
  133. {
  134.         struct file *filp;
  135.  
  136.         drm_gem_private_object_init(dev, obj, size);
  137.  
  138.         filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
  139.         if (IS_ERR(filp))
  140.                 return PTR_ERR(filp);
  141.  
  142.         obj->filp = filp;
  143.  
  144.         return 0;
  145. }
  146. EXPORT_SYMBOL(drm_gem_object_init);
  147.  
  148. /**
  149.  * drm_gem_private_object_init - initialize an allocated private GEM object
  150.  * @dev: drm_device the object should be initialized for
  151.  * @obj: drm_gem_object to initialize
  152.  * @size: object size
  153.  *
  154.  * Initialize an already allocated GEM object of the specified size with
  155.  * no GEM provided backing store. Instead the caller is responsible for
  156.  * backing the object and handling it.
  157.  */
  158. void drm_gem_private_object_init(struct drm_device *dev,
  159.                                  struct drm_gem_object *obj, size_t size)
  160. {
  161.         BUG_ON((size & (PAGE_SIZE - 1)) != 0);
  162.  
  163.         obj->dev = dev;
  164.         obj->filp = NULL;
  165.  
  166.         kref_init(&obj->refcount);
  167.         obj->handle_count = 0;
  168.         obj->size = size;
  169.         drm_vma_node_reset(&obj->vma_node);
  170. }
  171. EXPORT_SYMBOL(drm_gem_private_object_init);
  172.  
  173. static void
  174. drm_gem_remove_prime_handles(struct drm_gem_object *obj, struct drm_file *filp)
  175. {
  176.         /*
  177.          * Note: obj->dma_buf can't disappear as long as we still hold a
  178.          * handle reference in obj->handle_count.
  179.          */
  180. }
  181.  
  182. /**
  183.  * drm_gem_object_handle_free - release resources bound to userspace handles
  184.  * @obj: GEM object to clean up.
  185.  *
  186.  * Called after the last handle to the object has been closed
  187.  *
  188.  * Removes any name for the object. Note that this must be
  189.  * called before drm_gem_object_free or we'll be touching
  190.  * freed memory
  191.  */
  192. static void drm_gem_object_handle_free(struct drm_gem_object *obj)
  193. {
  194.         struct drm_device *dev = obj->dev;
  195.  
  196.         /* Remove any name for this object */
  197.         if (obj->name) {
  198.                 idr_remove(&dev->object_name_idr, obj->name);
  199.                 obj->name = 0;
  200.         }
  201. }
  202.  
  203.  
  204. static void
  205. drm_gem_object_handle_unreference_unlocked(struct drm_gem_object *obj)
  206. {
  207.         struct drm_device *dev = obj->dev;
  208.         bool final = false;
  209.  
  210.         if (WARN_ON(obj->handle_count == 0))
  211.                 return;
  212.  
  213.         /*
  214.         * Must bump handle count first as this may be the last
  215.         * ref, in which case the object would disappear before we
  216.         * checked for a name
  217.         */
  218.  
  219.         mutex_lock(&dev->object_name_lock);
  220.         if (--obj->handle_count == 0) {
  221.                 drm_gem_object_handle_free(obj);
  222.                 final = true;
  223.         }
  224.         mutex_unlock(&dev->object_name_lock);
  225.  
  226.         if (final)
  227.                 drm_gem_object_unreference_unlocked(obj);
  228. }
  229.  
  230. /*
  231.  * Called at device or object close to release the file's
  232.  * handle references on objects.
  233.  */
  234. static int
  235. drm_gem_object_release_handle(int id, void *ptr, void *data)
  236. {
  237.         struct drm_file *file_priv = data;
  238.         struct drm_gem_object *obj = ptr;
  239.         struct drm_device *dev = obj->dev;
  240.  
  241.         if (drm_core_check_feature(dev, DRIVER_PRIME))
  242.                 drm_gem_remove_prime_handles(obj, file_priv);
  243. //      drm_vma_node_revoke(&obj->vma_node, file_priv->filp);
  244.  
  245.         if (dev->driver->gem_close_object)
  246.                 dev->driver->gem_close_object(obj, file_priv);
  247.  
  248.         drm_gem_object_handle_unreference_unlocked(obj);
  249.  
  250.         return 0;
  251. }
  252.  
  253. /**
  254.  * drm_gem_handle_delete - deletes the given file-private handle
  255.  * @filp: drm file-private structure to use for the handle look up
  256.  * @handle: userspace handle to delete
  257.  *
  258.  * Removes the GEM handle from the @filp lookup table which has been added with
  259.  * drm_gem_handle_create(). If this is the last handle also cleans up linked
  260.  * resources like GEM names.
  261.  */
  262. int
  263. drm_gem_handle_delete(struct drm_file *filp, u32 handle)
  264. {
  265.         struct drm_device *dev;
  266.         struct drm_gem_object *obj;
  267.  
  268.         /* This is gross. The idr system doesn't let us try a delete and
  269.          * return an error code.  It just spews if you fail at deleting.
  270.          * So, we have to grab a lock around finding the object and then
  271.          * doing the delete on it and dropping the refcount, or the user
  272.          * could race us to double-decrement the refcount and cause a
  273.          * use-after-free later.  Given the frequency of our handle lookups,
  274.          * we may want to use ida for number allocation and a hash table
  275.          * for the pointers, anyway.
  276.          */
  277.         spin_lock(&filp->table_lock);
  278.  
  279.         /* Check if we currently have a reference on the object */
  280.         obj = idr_find(&filp->object_idr, handle);
  281.         if (obj == NULL) {
  282.                 spin_unlock(&filp->table_lock);
  283.                 return -EINVAL;
  284.         }
  285.         dev = obj->dev;
  286.  
  287.         /* Release reference and decrement refcount. */
  288.         idr_remove(&filp->object_idr, handle);
  289.         spin_unlock(&filp->table_lock);
  290.  
  291.         drm_gem_object_release_handle(handle, obj, filp);
  292.         return 0;
  293. }
  294. EXPORT_SYMBOL(drm_gem_handle_delete);
  295.  
  296. /**
  297.  * drm_gem_dumb_destroy - dumb fb callback helper for gem based drivers
  298.  * @file: drm file-private structure to remove the dumb handle from
  299.  * @dev: corresponding drm_device
  300.  * @handle: the dumb handle to remove
  301.  *
  302.  * This implements the ->dumb_destroy kms driver callback for drivers which use
  303.  * gem to manage their backing storage.
  304.  */
  305. int drm_gem_dumb_destroy(struct drm_file *file,
  306.                          struct drm_device *dev,
  307.                          uint32_t handle)
  308. {
  309.         return drm_gem_handle_delete(file, handle);
  310. }
  311. EXPORT_SYMBOL(drm_gem_dumb_destroy);
  312.  
  313. /**
  314.  * drm_gem_handle_create_tail - internal functions to create a handle
  315.  * @file_priv: drm file-private structure to register the handle for
  316.  * @obj: object to register
  317.  * @handlep: pointer to return the created handle to the caller
  318.  *
  319.  * This expects the dev->object_name_lock to be held already and will drop it
  320.  * before returning. Used to avoid races in establishing new handles when
  321.  * importing an object from either an flink name or a dma-buf.
  322.  *
  323.  * Handles must be release again through drm_gem_handle_delete(). This is done
  324.  * when userspace closes @file_priv for all attached handles, or through the
  325.  * GEM_CLOSE ioctl for individual handles.
  326.  */
  327. int
  328. drm_gem_handle_create_tail(struct drm_file *file_priv,
  329.                            struct drm_gem_object *obj,
  330.                            u32 *handlep)
  331. {
  332.         struct drm_device *dev = obj->dev;
  333.         u32 handle;
  334.         int ret;
  335.  
  336.         WARN_ON(!mutex_is_locked(&dev->object_name_lock));
  337.         if (obj->handle_count++ == 0)
  338.                 drm_gem_object_reference(obj);
  339.  
  340.         /*
  341.          * Get the user-visible handle using idr.  Preload and perform
  342.          * allocation under our spinlock.
  343.          */
  344.         idr_preload(GFP_KERNEL);
  345.         spin_lock(&file_priv->table_lock);
  346.  
  347.         ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT);
  348.  
  349.         spin_unlock(&file_priv->table_lock);
  350.         idr_preload_end();
  351.  
  352.         mutex_unlock(&dev->object_name_lock);
  353.         if (ret < 0)
  354.                 goto err_unref;
  355.  
  356.         handle = ret;
  357.  
  358. //      ret = drm_vma_node_allow(&obj->vma_node, file_priv->filp);
  359. //      if (ret) {
  360. //              drm_gem_handle_delete(file_priv, *handlep);
  361. //              return ret;
  362. //      }
  363.  
  364.         if (dev->driver->gem_open_object) {
  365.                 ret = dev->driver->gem_open_object(obj, file_priv);
  366.                 if (ret)
  367.                         goto err_revoke;
  368.         }
  369.  
  370.         *handlep = handle;
  371.         return 0;
  372.  
  373. err_revoke:
  374. //      drm_vma_node_revoke(&obj->vma_node, file_priv->filp);
  375. err_remove:
  376.         spin_lock(&file_priv->table_lock);
  377.         idr_remove(&file_priv->object_idr, handle);
  378.         spin_unlock(&file_priv->table_lock);
  379. err_unref:
  380.         drm_gem_object_handle_unreference_unlocked(obj);
  381.         return ret;
  382. }
  383.  
  384. /**
  385.  * drm_gem_handle_create - create a gem handle for an object
  386.  * @file_priv: drm file-private structure to register the handle for
  387.  * @obj: object to register
  388.  * @handlep: pionter to return the created handle to the caller
  389.  *
  390.  * Create a handle for this object. This adds a handle reference
  391.  * to the object, which includes a regular reference count. Callers
  392.  * will likely want to dereference the object afterwards.
  393.  */
  394. int drm_gem_handle_create(struct drm_file *file_priv,
  395.                           struct drm_gem_object *obj,
  396.                           u32 *handlep)
  397. {
  398.         mutex_lock(&obj->dev->object_name_lock);
  399.  
  400.         return drm_gem_handle_create_tail(file_priv, obj, handlep);
  401. }
  402. EXPORT_SYMBOL(drm_gem_handle_create);
  403.  
  404. #if 0
  405. /**
  406.  * drm_gem_free_mmap_offset - release a fake mmap offset for an object
  407.  * @obj: obj in question
  408.  *
  409.  * This routine frees fake offsets allocated by drm_gem_create_mmap_offset().
  410.  */
  411. void
  412. drm_gem_free_mmap_offset(struct drm_gem_object *obj)
  413. {
  414.         struct drm_device *dev = obj->dev;
  415.  
  416.         drm_vma_offset_remove(dev->vma_offset_manager, &obj->vma_node);
  417. }
  418. EXPORT_SYMBOL(drm_gem_free_mmap_offset);
  419.  
  420. /**
  421.  * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object
  422.  * @obj: obj in question
  423.  * @size: the virtual size
  424.  *
  425.  * GEM memory mapping works by handing back to userspace a fake mmap offset
  426.  * it can use in a subsequent mmap(2) call.  The DRM core code then looks
  427.  * up the object based on the offset and sets up the various memory mapping
  428.  * structures.
  429.  *
  430.  * This routine allocates and attaches a fake offset for @obj, in cases where
  431.  * the virtual size differs from the physical size (ie. obj->size).  Otherwise
  432.  * just use drm_gem_create_mmap_offset().
  433.  */
  434. int
  435. drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size)
  436. {
  437.         struct drm_device *dev = obj->dev;
  438.  
  439.         return drm_vma_offset_add(dev->vma_offset_manager, &obj->vma_node,
  440.                                   size / PAGE_SIZE);
  441. }
  442. EXPORT_SYMBOL(drm_gem_create_mmap_offset_size);
  443.  
  444. /**
  445.  * drm_gem_create_mmap_offset - create a fake mmap offset for an object
  446.  * @obj: obj in question
  447.  *
  448.  * GEM memory mapping works by handing back to userspace a fake mmap offset
  449.  * it can use in a subsequent mmap(2) call.  The DRM core code then looks
  450.  * up the object based on the offset and sets up the various memory mapping
  451.  * structures.
  452.  *
  453.  * This routine allocates and attaches a fake offset for @obj.
  454.  */
  455. int drm_gem_create_mmap_offset(struct drm_gem_object *obj)
  456. {
  457.         return drm_gem_create_mmap_offset_size(obj, obj->size);
  458. }
  459. EXPORT_SYMBOL(drm_gem_create_mmap_offset);
  460.  
  461. /**
  462.  * drm_gem_get_pages - helper to allocate backing pages for a GEM object
  463.  * from shmem
  464.  * @obj: obj in question
  465.  *
  466.  * This reads the page-array of the shmem-backing storage of the given gem
  467.  * object. An array of pages is returned. If a page is not allocated or
  468.  * swapped-out, this will allocate/swap-in the required pages. Note that the
  469.  * whole object is covered by the page-array and pinned in memory.
  470.  *
  471.  * Use drm_gem_put_pages() to release the array and unpin all pages.
  472.  *
  473.  * This uses the GFP-mask set on the shmem-mapping (see mapping_set_gfp_mask()).
  474.  * If you require other GFP-masks, you have to do those allocations yourself.
  475.  *
  476.  * Note that you are not allowed to change gfp-zones during runtime. That is,
  477.  * shmem_read_mapping_page_gfp() must be called with the same gfp_zone(gfp) as
  478.  * set during initialization. If you have special zone constraints, set them
  479.  * after drm_gem_init_object() via mapping_set_gfp_mask(). shmem-core takes care
  480.  * to keep pages in the required zone during swap-in.
  481.  */
  482. struct page **drm_gem_get_pages(struct drm_gem_object *obj)
  483. {
  484.         struct address_space *mapping;
  485.         struct page *p, **pages;
  486.         int i, npages;
  487.  
  488.         /* This is the shared memory object that backs the GEM resource */
  489.         mapping = file_inode(obj->filp)->i_mapping;
  490.  
  491.         /* We already BUG_ON() for non-page-aligned sizes in
  492.          * drm_gem_object_init(), so we should never hit this unless
  493.          * driver author is doing something really wrong:
  494.          */
  495.         WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
  496.  
  497.         npages = obj->size >> PAGE_SHIFT;
  498.  
  499.         pages = drm_malloc_ab(npages, sizeof(struct page *));
  500.         if (pages == NULL)
  501.                 return ERR_PTR(-ENOMEM);
  502.  
  503.         for (i = 0; i < npages; i++) {
  504.                 p = shmem_read_mapping_page(mapping, i);
  505.                 if (IS_ERR(p))
  506.                         goto fail;
  507.                 pages[i] = p;
  508.  
  509.                 /* Make sure shmem keeps __GFP_DMA32 allocated pages in the
  510.                  * correct region during swapin. Note that this requires
  511.                  * __GFP_DMA32 to be set in mapping_gfp_mask(inode->i_mapping)
  512.                  * so shmem can relocate pages during swapin if required.
  513.                  */
  514.                 BUG_ON(mapping_gfp_constraint(mapping, __GFP_DMA32) &&
  515.                                 (page_to_pfn(p) >= 0x00100000UL));
  516.         }
  517.  
  518.         return pages;
  519.  
  520. fail:
  521.         while (i--)
  522.                 page_cache_release(pages[i]);
  523.  
  524.         drm_free_large(pages);
  525.         return ERR_CAST(p);
  526. }
  527. EXPORT_SYMBOL(drm_gem_get_pages);
  528.  
  529. /**
  530.  * drm_gem_put_pages - helper to free backing pages for a GEM object
  531.  * @obj: obj in question
  532.  * @pages: pages to free
  533.  * @dirty: if true, pages will be marked as dirty
  534.  * @accessed: if true, the pages will be marked as accessed
  535.  */
  536. void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
  537.                 bool dirty, bool accessed)
  538. {
  539.         int i, npages;
  540.  
  541.         /* We already BUG_ON() for non-page-aligned sizes in
  542.          * drm_gem_object_init(), so we should never hit this unless
  543.          * driver author is doing something really wrong:
  544.          */
  545.         WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
  546.  
  547.         npages = obj->size >> PAGE_SHIFT;
  548.  
  549.         for (i = 0; i < npages; i++) {
  550.                 if (dirty)
  551.                         set_page_dirty(pages[i]);
  552.  
  553.                 if (accessed)
  554.                         mark_page_accessed(pages[i]);
  555.  
  556.                 /* Undo the reference we took when populating the table */
  557.                 page_cache_release(pages[i]);
  558.         }
  559.  
  560.         drm_free_large(pages);
  561. }
  562. EXPORT_SYMBOL(drm_gem_put_pages);
  563. #endif
  564.  
  565. /**
  566.  * drm_gem_object_lookup - look up a GEM object from it's handle
  567.  * @dev: DRM device
  568.  * @filp: DRM file private date
  569.  * @handle: userspace handle
  570.  *
  571.  * Returns:
  572.  *
  573.  * A reference to the object named by the handle if such exists on @filp, NULL
  574.  * otherwise.
  575.  */
  576. struct drm_gem_object *
  577. drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
  578.                       u32 handle)
  579. {
  580.         struct drm_gem_object *obj;
  581.  
  582.         spin_lock(&filp->table_lock);
  583.  
  584.         /* Check if we currently have a reference on the object */
  585.         obj = idr_find(&filp->object_idr, handle);
  586.         if (obj == NULL) {
  587.                 spin_unlock(&filp->table_lock);
  588.                 return NULL;
  589.         }
  590.  
  591.         drm_gem_object_reference(obj);
  592.  
  593.         spin_unlock(&filp->table_lock);
  594.  
  595.         return obj;
  596. }
  597. EXPORT_SYMBOL(drm_gem_object_lookup);
  598.  
  599. /**
  600.  * drm_gem_close_ioctl - implementation of the GEM_CLOSE ioctl
  601.  * @dev: drm_device
  602.  * @data: ioctl data
  603.  * @file_priv: drm file-private structure
  604.  *
  605.  * Releases the handle to an mm object.
  606.  */
  607. int
  608. drm_gem_close_ioctl(struct drm_device *dev, void *data,
  609.                     struct drm_file *file_priv)
  610. {
  611.         struct drm_gem_close *args = data;
  612.         int ret;
  613.  
  614.         if (!drm_core_check_feature(dev, DRIVER_GEM))
  615.                 return -ENODEV;
  616.  
  617.         ret = drm_gem_handle_delete(file_priv, args->handle);
  618.  
  619.         return ret;
  620. }
  621.  
  622. /**
  623.  * drm_gem_flink_ioctl - implementation of the GEM_FLINK ioctl
  624.  * @dev: drm_device
  625.  * @data: ioctl data
  626.  * @file_priv: drm file-private structure
  627.  *
  628.  * Create a global name for an object, returning the name.
  629.  *
  630.  * Note that the name does not hold a reference; when the object
  631.  * is freed, the name goes away.
  632.  */
  633. int
  634. drm_gem_flink_ioctl(struct drm_device *dev, void *data,
  635.                     struct drm_file *file_priv)
  636. {
  637.         struct drm_gem_flink *args = data;
  638.         struct drm_gem_object *obj;
  639.         int ret;
  640.  
  641.         if (!drm_core_check_feature(dev, DRIVER_GEM))
  642.                 return -ENODEV;
  643.  
  644.         obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  645.         if (obj == NULL)
  646.                 return -ENOENT;
  647.  
  648.         mutex_lock(&dev->object_name_lock);
  649.         /* prevent races with concurrent gem_close. */
  650.         if (obj->handle_count == 0) {
  651.                 ret = -ENOENT;
  652.                 goto err;
  653.         }
  654.  
  655.         if (!obj->name) {
  656.                 ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_KERNEL);
  657.                 if (ret < 0)
  658.                         goto err;
  659.  
  660.                 obj->name = ret;
  661.         }
  662.  
  663.         args->name = (uint64_t) obj->name;
  664.         ret = 0;
  665.  
  666. err:
  667.         mutex_unlock(&dev->object_name_lock);
  668.         drm_gem_object_unreference_unlocked(obj);
  669.  
  670. //    printf("%s object %p name %d refcount %d\n",
  671. //           __FUNCTION__, obj, obj->name, obj->refcount.refcount);
  672.  
  673.         return ret;
  674. }
  675.  
  676. /**
  677.  * drm_gem_open - implementation of the GEM_OPEN ioctl
  678.  * @dev: drm_device
  679.  * @data: ioctl data
  680.  * @file_priv: drm file-private structure
  681.  *
  682.  * Open an object using the global name, returning a handle and the size.
  683.  *
  684.  * This handle (of course) holds a reference to the object, so the object
  685.  * will not go away until the handle is deleted.
  686.  */
  687. int
  688. drm_gem_open_ioctl(struct drm_device *dev, void *data,
  689.                    struct drm_file *file_priv)
  690. {
  691.         struct drm_gem_open *args = data;
  692.         struct drm_gem_object *obj;
  693.         int ret;
  694.         u32 handle;
  695.  
  696.         if (!drm_core_check_feature(dev, DRIVER_GEM))
  697.                 return -ENODEV;
  698.  
  699.         mutex_lock(&dev->object_name_lock);
  700.         obj = idr_find(&dev->object_name_idr, (int) args->name);
  701.         if (obj) {
  702.                 drm_gem_object_reference(obj);
  703.         } else {
  704.                 mutex_unlock(&dev->object_name_lock);
  705.                 return -ENOENT;
  706.         }
  707.  
  708.         /* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
  709.         ret = drm_gem_handle_create_tail(file_priv, obj, &handle);
  710.         drm_gem_object_unreference_unlocked(obj);
  711.         if (ret)
  712.                 return ret;
  713.  
  714.         args->handle = handle;
  715.         args->size = obj->size;
  716.  
  717. //    printf("%s object %p handle %d refcount %d\n",
  718. //           __FUNCTION__, obj, handle, obj->refcount.refcount);
  719.  
  720.         return 0;
  721. }
  722.  
  723. #if 0
  724. /**
  725.  * gem_gem_open - initalizes GEM file-private structures at devnode open time
  726.  * @dev: drm_device which is being opened by userspace
  727.  * @file_private: drm file-private structure to set up
  728.  *
  729.  * Called at device open time, sets up the structure for handling refcounting
  730.  * of mm objects.
  731.  */
  732. void
  733. drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
  734. {
  735.         idr_init(&file_private->object_idr);
  736.         spin_lock_init(&file_private->table_lock);
  737. }
  738.  
  739. /**
  740.  * drm_gem_release - release file-private GEM resources
  741.  * @dev: drm_device which is being closed by userspace
  742.  * @file_private: drm file-private structure to clean up
  743.  *
  744.  * Called at close time when the filp is going away.
  745.  *
  746.  * Releases any remaining references on objects by this filp.
  747.  */
  748. void
  749. drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
  750. {
  751.         idr_for_each(&file_private->object_idr,
  752.                      &drm_gem_object_release_handle, file_private);
  753.         idr_destroy(&file_private->object_idr);
  754. }
  755. #endif
  756.  
  757. void
  758. drm_gem_object_release(struct drm_gem_object *obj)
  759. {
  760.         WARN_ON(obj->dma_buf);
  761.  
  762.         if (obj->filp)
  763.             free(obj->filp);
  764. }
  765. EXPORT_SYMBOL(drm_gem_object_release);
  766.  
  767. /**
  768.  * drm_gem_object_free - free a GEM object
  769.  * @kref: kref of the object to free
  770.  *
  771.  * Called after the last reference to the object has been lost.
  772.  * Must be called holding struct_ mutex
  773.  *
  774.  * Frees the object
  775.  */
  776. void
  777. drm_gem_object_free(struct kref *kref)
  778. {
  779.         struct drm_gem_object *obj =
  780.                 container_of(kref, struct drm_gem_object, refcount);
  781.         struct drm_device *dev = obj->dev;
  782.  
  783.         WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  784.  
  785.         if (dev->driver->gem_free_object != NULL)
  786.                 dev->driver->gem_free_object(obj);
  787. }
  788. EXPORT_SYMBOL(drm_gem_object_free);
  789.  
  790. /**
  791.  * drm_gem_vm_open - vma->ops->open implementation for GEM
  792.  * @vma: VM area structure
  793.  *
  794.  * This function implements the #vm_operations_struct open() callback for GEM
  795.  * drivers. This must be used together with drm_gem_vm_close().
  796.  */
  797. #if 0
  798. void drm_gem_vm_open(struct vm_area_struct *vma)
  799. {
  800.         struct drm_gem_object *obj = vma->vm_private_data;
  801.  
  802.         drm_gem_object_reference(obj);
  803. }
  804. EXPORT_SYMBOL(drm_gem_vm_open);
  805.  
  806. /**
  807.  * drm_gem_vm_close - vma->ops->close implementation for GEM
  808.  * @vma: VM area structure
  809.  *
  810.  * This function implements the #vm_operations_struct close() callback for GEM
  811.  * drivers. This must be used together with drm_gem_vm_open().
  812.  */
  813. void drm_gem_vm_close(struct vm_area_struct *vma)
  814. {
  815.         struct drm_gem_object *obj = vma->vm_private_data;
  816.  
  817.         drm_gem_object_unreference_unlocked(obj);
  818. }
  819. EXPORT_SYMBOL(drm_gem_vm_close);
  820.  
  821. /**
  822.  * drm_gem_mmap_obj - memory map a GEM object
  823.  * @obj: the GEM object to map
  824.  * @obj_size: the object size to be mapped, in bytes
  825.  * @vma: VMA for the area to be mapped
  826.  *
  827.  * Set up the VMA to prepare mapping of the GEM object using the gem_vm_ops
  828.  * provided by the driver. Depending on their requirements, drivers can either
  829.  * provide a fault handler in their gem_vm_ops (in which case any accesses to
  830.  * the object will be trapped, to perform migration, GTT binding, surface
  831.  * register allocation, or performance monitoring), or mmap the buffer memory
  832.  * synchronously after calling drm_gem_mmap_obj.
  833.  *
  834.  * This function is mainly intended to implement the DMABUF mmap operation, when
  835.  * the GEM object is not looked up based on its fake offset. To implement the
  836.  * DRM mmap operation, drivers should use the drm_gem_mmap() function.
  837.  *
  838.  * drm_gem_mmap_obj() assumes the user is granted access to the buffer while
  839.  * drm_gem_mmap() prevents unprivileged users from mapping random objects. So
  840.  * callers must verify access restrictions before calling this helper.
  841.  *
  842.  * Return 0 or success or -EINVAL if the object size is smaller than the VMA
  843.  * size, or if no gem_vm_ops are provided.
  844.  */
  845. int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
  846.                      struct vm_area_struct *vma)
  847. {
  848.         struct drm_device *dev = obj->dev;
  849.  
  850.         /* Check for valid size. */
  851.         if (obj_size < vma->vm_end - vma->vm_start)
  852.                 return -EINVAL;
  853.  
  854.         if (!dev->driver->gem_vm_ops)
  855.                 return -EINVAL;
  856.  
  857.         vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
  858.         vma->vm_ops = dev->driver->gem_vm_ops;
  859.         vma->vm_private_data = obj;
  860.         vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
  861.  
  862.         /* Take a ref for this mapping of the object, so that the fault
  863.          * handler can dereference the mmap offset's pointer to the object.
  864.          * This reference is cleaned up by the corresponding vm_close
  865.          * (which should happen whether the vma was created by this call, or
  866.          * by a vm_open due to mremap or partial unmap or whatever).
  867.          */
  868.         drm_gem_object_reference(obj);
  869.  
  870.         return 0;
  871. }
  872. EXPORT_SYMBOL(drm_gem_mmap_obj);
  873.  
  874. /**
  875.  * drm_gem_mmap - memory map routine for GEM objects
  876.  * @filp: DRM file pointer
  877.  * @vma: VMA for the area to be mapped
  878.  *
  879.  * If a driver supports GEM object mapping, mmap calls on the DRM file
  880.  * descriptor will end up here.
  881.  *
  882.  * Look up the GEM object based on the offset passed in (vma->vm_pgoff will
  883.  * contain the fake offset we created when the GTT map ioctl was called on
  884.  * the object) and map it with a call to drm_gem_mmap_obj().
  885.  *
  886.  * If the caller is not granted access to the buffer object, the mmap will fail
  887.  * with EACCES. Please see the vma manager for more information.
  888.  */
  889. int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
  890. {
  891.         struct drm_file *priv = filp->private_data;
  892.         struct drm_device *dev = priv->minor->dev;
  893.         struct drm_gem_object *obj = NULL;
  894.         struct drm_vma_offset_node *node;
  895.         int ret;
  896.  
  897.         if (drm_device_is_unplugged(dev))
  898.                 return -ENODEV;
  899.  
  900.         drm_vma_offset_lock_lookup(dev->vma_offset_manager);
  901.         node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager,
  902.                                                   vma->vm_pgoff,
  903.                                                   vma_pages(vma));
  904.         if (likely(node)) {
  905.                 obj = container_of(node, struct drm_gem_object, vma_node);
  906.                 /*
  907.                  * When the object is being freed, after it hits 0-refcnt it
  908.                  * proceeds to tear down the object. In the process it will
  909.                  * attempt to remove the VMA offset and so acquire this
  910.                  * mgr->vm_lock.  Therefore if we find an object with a 0-refcnt
  911.                  * that matches our range, we know it is in the process of being
  912.                  * destroyed and will be freed as soon as we release the lock -
  913.                  * so we have to check for the 0-refcnted object and treat it as
  914.                  * invalid.
  915.                  */
  916.                 if (!kref_get_unless_zero(&obj->refcount))
  917.                         obj = NULL;
  918.         }
  919.         drm_vma_offset_unlock_lookup(dev->vma_offset_manager);
  920.  
  921.         if (!obj)
  922.                 return -EINVAL;
  923.  
  924.         if (!drm_vma_node_is_allowed(node, filp)) {
  925.                 drm_gem_object_unreference_unlocked(obj);
  926.                 return -EACCES;
  927.         }
  928.  
  929.         ret = drm_gem_mmap_obj(obj, drm_vma_node_size(node) << PAGE_SHIFT,
  930.                                vma);
  931.  
  932.         drm_gem_object_unreference_unlocked(obj);
  933.  
  934.         return ret;
  935. }
  936. EXPORT_SYMBOL(drm_gem_mmap);
  937. #endif