Back to home page

OSCL-LXR

 
 

    


0001 #ifndef __DRM_GEM_H__
0002 #define __DRM_GEM_H__
0003 
0004 /*
0005  * GEM Graphics Execution Manager Driver Interfaces
0006  *
0007  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
0008  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
0009  * Copyright (c) 2009-2010, Code Aurora Forum.
0010  * All rights reserved.
0011  * Copyright © 2014 Intel Corporation
0012  *   Daniel Vetter <daniel.vetter@ffwll.ch>
0013  *
0014  * Author: Rickard E. (Rik) Faith <faith@valinux.com>
0015  * Author: Gareth Hughes <gareth@valinux.com>
0016  *
0017  * Permission is hereby granted, free of charge, to any person obtaining a
0018  * copy of this software and associated documentation files (the "Software"),
0019  * to deal in the Software without restriction, including without limitation
0020  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0021  * and/or sell copies of the Software, and to permit persons to whom the
0022  * Software is furnished to do so, subject to the following conditions:
0023  *
0024  * The above copyright notice and this permission notice (including the next
0025  * paragraph) shall be included in all copies or substantial portions of the
0026  * Software.
0027  *
0028  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0029  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0030  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0031  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
0032  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0033  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0034  * OTHER DEALINGS IN THE SOFTWARE.
0035  */
0036 
0037 #include <linux/kref.h>
0038 #include <linux/dma-resv.h>
0039 
0040 #include <drm/drm_vma_manager.h>
0041 
0042 struct iosys_map;
0043 struct drm_gem_object;
0044 
0045 /**
0046  * struct drm_gem_object_funcs - GEM object functions
0047  */
0048 struct drm_gem_object_funcs {
0049     /**
0050      * @free:
0051      *
0052      * Deconstructor for drm_gem_objects.
0053      *
0054      * This callback is mandatory.
0055      */
0056     void (*free)(struct drm_gem_object *obj);
0057 
0058     /**
0059      * @open:
0060      *
0061      * Called upon GEM handle creation.
0062      *
0063      * This callback is optional.
0064      */
0065     int (*open)(struct drm_gem_object *obj, struct drm_file *file);
0066 
0067     /**
0068      * @close:
0069      *
0070      * Called upon GEM handle release.
0071      *
0072      * This callback is optional.
0073      */
0074     void (*close)(struct drm_gem_object *obj, struct drm_file *file);
0075 
0076     /**
0077      * @print_info:
0078      *
0079      * If driver subclasses struct &drm_gem_object, it can implement this
0080      * optional hook for printing additional driver specific info.
0081      *
0082      * drm_printf_indent() should be used in the callback passing it the
0083      * indent argument.
0084      *
0085      * This callback is called from drm_gem_print_info().
0086      *
0087      * This callback is optional.
0088      */
0089     void (*print_info)(struct drm_printer *p, unsigned int indent,
0090                const struct drm_gem_object *obj);
0091 
0092     /**
0093      * @export:
0094      *
0095      * Export backing buffer as a &dma_buf.
0096      * If this is not set drm_gem_prime_export() is used.
0097      *
0098      * This callback is optional.
0099      */
0100     struct dma_buf *(*export)(struct drm_gem_object *obj, int flags);
0101 
0102     /**
0103      * @pin:
0104      *
0105      * Pin backing buffer in memory. Used by the drm_gem_map_attach() helper.
0106      *
0107      * This callback is optional.
0108      */
0109     int (*pin)(struct drm_gem_object *obj);
0110 
0111     /**
0112      * @unpin:
0113      *
0114      * Unpin backing buffer. Used by the drm_gem_map_detach() helper.
0115      *
0116      * This callback is optional.
0117      */
0118     void (*unpin)(struct drm_gem_object *obj);
0119 
0120     /**
0121      * @get_sg_table:
0122      *
0123      * Returns a Scatter-Gather table representation of the buffer.
0124      * Used when exporting a buffer by the drm_gem_map_dma_buf() helper.
0125      * Releasing is done by calling dma_unmap_sg_attrs() and sg_free_table()
0126      * in drm_gem_unmap_buf(), therefore these helpers and this callback
0127      * here cannot be used for sg tables pointing at driver private memory
0128      * ranges.
0129      *
0130      * See also drm_prime_pages_to_sg().
0131      */
0132     struct sg_table *(*get_sg_table)(struct drm_gem_object *obj);
0133 
0134     /**
0135      * @vmap:
0136      *
0137      * Returns a virtual address for the buffer. Used by the
0138      * drm_gem_dmabuf_vmap() helper.
0139      *
0140      * This callback is optional.
0141      */
0142     int (*vmap)(struct drm_gem_object *obj, struct iosys_map *map);
0143 
0144     /**
0145      * @vunmap:
0146      *
0147      * Releases the address previously returned by @vmap. Used by the
0148      * drm_gem_dmabuf_vunmap() helper.
0149      *
0150      * This callback is optional.
0151      */
0152     void (*vunmap)(struct drm_gem_object *obj, struct iosys_map *map);
0153 
0154     /**
0155      * @mmap:
0156      *
0157      * Handle mmap() of the gem object, setup vma accordingly.
0158      *
0159      * This callback is optional.
0160      *
0161      * The callback is used by both drm_gem_mmap_obj() and
0162      * drm_gem_prime_mmap().  When @mmap is present @vm_ops is not
0163      * used, the @mmap callback must set vma->vm_ops instead.
0164      */
0165     int (*mmap)(struct drm_gem_object *obj, struct vm_area_struct *vma);
0166 
0167     /**
0168      * @vm_ops:
0169      *
0170      * Virtual memory operations used with mmap.
0171      *
0172      * This is optional but necessary for mmap support.
0173      */
0174     const struct vm_operations_struct *vm_ops;
0175 };
0176 
0177 /**
0178  * struct drm_gem_object - GEM buffer object
0179  *
0180  * This structure defines the generic parts for GEM buffer objects, which are
0181  * mostly around handling mmap and userspace handles.
0182  *
0183  * Buffer objects are often abbreviated to BO.
0184  */
0185 struct drm_gem_object {
0186     /**
0187      * @refcount:
0188      *
0189      * Reference count of this object
0190      *
0191      * Please use drm_gem_object_get() to acquire and drm_gem_object_put_locked()
0192      * or drm_gem_object_put() to release a reference to a GEM
0193      * buffer object.
0194      */
0195     struct kref refcount;
0196 
0197     /**
0198      * @handle_count:
0199      *
0200      * This is the GEM file_priv handle count of this object.
0201      *
0202      * Each handle also holds a reference. Note that when the handle_count
0203      * drops to 0 any global names (e.g. the id in the flink namespace) will
0204      * be cleared.
0205      *
0206      * Protected by &drm_device.object_name_lock.
0207      */
0208     unsigned handle_count;
0209 
0210     /**
0211      * @dev: DRM dev this object belongs to.
0212      */
0213     struct drm_device *dev;
0214 
0215     /**
0216      * @filp:
0217      *
0218      * SHMEM file node used as backing storage for swappable buffer objects.
0219      * GEM also supports driver private objects with driver-specific backing
0220      * storage (contiguous CMA memory, special reserved blocks). In this
0221      * case @filp is NULL.
0222      */
0223     struct file *filp;
0224 
0225     /**
0226      * @vma_node:
0227      *
0228      * Mapping info for this object to support mmap. Drivers are supposed to
0229      * allocate the mmap offset using drm_gem_create_mmap_offset(). The
0230      * offset itself can be retrieved using drm_vma_node_offset_addr().
0231      *
0232      * Memory mapping itself is handled by drm_gem_mmap(), which also checks
0233      * that userspace is allowed to access the object.
0234      */
0235     struct drm_vma_offset_node vma_node;
0236 
0237     /**
0238      * @size:
0239      *
0240      * Size of the object, in bytes.  Immutable over the object's
0241      * lifetime.
0242      */
0243     size_t size;
0244 
0245     /**
0246      * @name:
0247      *
0248      * Global name for this object, starts at 1. 0 means unnamed.
0249      * Access is covered by &drm_device.object_name_lock. This is used by
0250      * the GEM_FLINK and GEM_OPEN ioctls.
0251      */
0252     int name;
0253 
0254     /**
0255      * @dma_buf:
0256      *
0257      * dma-buf associated with this GEM object.
0258      *
0259      * Pointer to the dma-buf associated with this gem object (either
0260      * through importing or exporting). We break the resulting reference
0261      * loop when the last gem handle for this object is released.
0262      *
0263      * Protected by &drm_device.object_name_lock.
0264      */
0265     struct dma_buf *dma_buf;
0266 
0267     /**
0268      * @import_attach:
0269      *
0270      * dma-buf attachment backing this object.
0271      *
0272      * Any foreign dma_buf imported as a gem object has this set to the
0273      * attachment point for the device. This is invariant over the lifetime
0274      * of a gem object.
0275      *
0276      * The &drm_gem_object_funcs.free callback is responsible for
0277      * cleaning up the dma_buf attachment and references acquired at import
0278      * time.
0279      *
0280      * Note that the drm gem/prime core does not depend upon drivers setting
0281      * this field any more. So for drivers where this doesn't make sense
0282      * (e.g. virtual devices or a displaylink behind an usb bus) they can
0283      * simply leave it as NULL.
0284      */
0285     struct dma_buf_attachment *import_attach;
0286 
0287     /**
0288      * @resv:
0289      *
0290      * Pointer to reservation object associated with the this GEM object.
0291      *
0292      * Normally (@resv == &@_resv) except for imported GEM objects.
0293      */
0294     struct dma_resv *resv;
0295 
0296     /**
0297      * @_resv:
0298      *
0299      * A reservation object for this GEM object.
0300      *
0301      * This is unused for imported GEM objects.
0302      */
0303     struct dma_resv _resv;
0304 
0305     /**
0306      * @funcs:
0307      *
0308      * Optional GEM object functions. If this is set, it will be used instead of the
0309      * corresponding &drm_driver GEM callbacks.
0310      *
0311      * New drivers should use this.
0312      *
0313      */
0314     const struct drm_gem_object_funcs *funcs;
0315 };
0316 
0317 /**
0318  * DRM_GEM_FOPS - Default drm GEM file operations
0319  *
0320  * This macro provides a shorthand for setting the GEM file ops in the
0321  * &file_operations structure.  If all you need are the default ops, use
0322  * DEFINE_DRM_GEM_FOPS instead.
0323  */
0324 #define DRM_GEM_FOPS \
0325     .open       = drm_open,\
0326     .release    = drm_release,\
0327     .unlocked_ioctl = drm_ioctl,\
0328     .compat_ioctl   = drm_compat_ioctl,\
0329     .poll       = drm_poll,\
0330     .read       = drm_read,\
0331     .llseek     = noop_llseek,\
0332     .mmap       = drm_gem_mmap
0333 
0334 /**
0335  * DEFINE_DRM_GEM_FOPS() - macro to generate file operations for GEM drivers
0336  * @name: name for the generated structure
0337  *
0338  * This macro autogenerates a suitable &struct file_operations for GEM based
0339  * drivers, which can be assigned to &drm_driver.fops. Note that this structure
0340  * cannot be shared between drivers, because it contains a reference to the
0341  * current module using THIS_MODULE.
0342  *
0343  * Note that the declaration is already marked as static - if you need a
0344  * non-static version of this you're probably doing it wrong and will break the
0345  * THIS_MODULE reference by accident.
0346  */
0347 #define DEFINE_DRM_GEM_FOPS(name) \
0348     static const struct file_operations name = {\
0349         .owner      = THIS_MODULE,\
0350         DRM_GEM_FOPS,\
0351     }
0352 
0353 void drm_gem_object_release(struct drm_gem_object *obj);
0354 void drm_gem_object_free(struct kref *kref);
0355 int drm_gem_object_init(struct drm_device *dev,
0356             struct drm_gem_object *obj, size_t size);
0357 void drm_gem_private_object_init(struct drm_device *dev,
0358                  struct drm_gem_object *obj, size_t size);
0359 void drm_gem_vm_open(struct vm_area_struct *vma);
0360 void drm_gem_vm_close(struct vm_area_struct *vma);
0361 int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
0362              struct vm_area_struct *vma);
0363 int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
0364 
0365 /**
0366  * drm_gem_object_get - acquire a GEM buffer object reference
0367  * @obj: GEM buffer object
0368  *
0369  * This function acquires an additional reference to @obj. It is illegal to
0370  * call this without already holding a reference. No locks required.
0371  */
0372 static inline void drm_gem_object_get(struct drm_gem_object *obj)
0373 {
0374     kref_get(&obj->refcount);
0375 }
0376 
0377 __attribute__((nonnull))
0378 static inline void
0379 __drm_gem_object_put(struct drm_gem_object *obj)
0380 {
0381     kref_put(&obj->refcount, drm_gem_object_free);
0382 }
0383 
0384 /**
0385  * drm_gem_object_put - drop a GEM buffer object reference
0386  * @obj: GEM buffer object
0387  *
0388  * This releases a reference to @obj.
0389  */
0390 static inline void
0391 drm_gem_object_put(struct drm_gem_object *obj)
0392 {
0393     if (obj)
0394         __drm_gem_object_put(obj);
0395 }
0396 
0397 int drm_gem_handle_create(struct drm_file *file_priv,
0398               struct drm_gem_object *obj,
0399               u32 *handlep);
0400 int drm_gem_handle_delete(struct drm_file *filp, u32 handle);
0401 
0402 
0403 void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
0404 int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
0405 int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
0406 
0407 struct page **drm_gem_get_pages(struct drm_gem_object *obj);
0408 void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
0409         bool dirty, bool accessed);
0410 
0411 int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
0412                int count, struct drm_gem_object ***objs_out);
0413 struct drm_gem_object *drm_gem_object_lookup(struct drm_file *filp, u32 handle);
0414 long drm_gem_dma_resv_wait(struct drm_file *filep, u32 handle,
0415                     bool wait_all, unsigned long timeout);
0416 int drm_gem_lock_reservations(struct drm_gem_object **objs, int count,
0417                   struct ww_acquire_ctx *acquire_ctx);
0418 void drm_gem_unlock_reservations(struct drm_gem_object **objs, int count,
0419                  struct ww_acquire_ctx *acquire_ctx);
0420 int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
0421                 u32 handle, u64 *offset);
0422 
0423 #endif /* __DRM_GEM_H__ */