Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __DRM_GEM_CMA_HELPER_H__
0003 #define __DRM_GEM_CMA_HELPER_H__
0004 
0005 #include <drm/drm_file.h>
0006 #include <drm/drm_ioctl.h>
0007 #include <drm/drm_gem.h>
0008 
0009 struct drm_mode_create_dumb;
0010 
0011 /**
0012  * struct drm_gem_cma_object - GEM object backed by CMA memory allocations
0013  * @base: base GEM object
0014  * @paddr: physical address of the backing memory
0015  * @sgt: scatter/gather table for imported PRIME buffers. The table can have
0016  *       more than one entry but they are guaranteed to have contiguous
0017  *       DMA addresses.
0018  * @vaddr: kernel virtual address of the backing memory
0019  * @map_noncoherent: if true, the GEM object is backed by non-coherent memory
0020  */
0021 struct drm_gem_cma_object {
0022     struct drm_gem_object base;
0023     dma_addr_t paddr;
0024     struct sg_table *sgt;
0025 
0026     /* For objects with DMA memory allocated by GEM CMA */
0027     void *vaddr;
0028 
0029     bool map_noncoherent;
0030 };
0031 
0032 #define to_drm_gem_cma_obj(gem_obj) \
0033     container_of(gem_obj, struct drm_gem_cma_object, base)
0034 
0035 struct drm_gem_cma_object *drm_gem_cma_create(struct drm_device *drm,
0036                           size_t size);
0037 void drm_gem_cma_free(struct drm_gem_cma_object *cma_obj);
0038 void drm_gem_cma_print_info(const struct drm_gem_cma_object *cma_obj,
0039                 struct drm_printer *p, unsigned int indent);
0040 struct sg_table *drm_gem_cma_get_sg_table(struct drm_gem_cma_object *cma_obj);
0041 int drm_gem_cma_vmap(struct drm_gem_cma_object *cma_obj,
0042              struct iosys_map *map);
0043 int drm_gem_cma_mmap(struct drm_gem_cma_object *cma_obj, struct vm_area_struct *vma);
0044 
0045 extern const struct vm_operations_struct drm_gem_cma_vm_ops;
0046 
0047 /*
0048  * GEM object functions
0049  */
0050 
0051 /**
0052  * drm_gem_cma_object_free - GEM object function for drm_gem_cma_free()
0053  * @obj: GEM object to free
0054  *
0055  * This function wraps drm_gem_cma_free_object(). Drivers that employ the CMA helpers
0056  * should use it as their &drm_gem_object_funcs.free handler.
0057  */
0058 static inline void drm_gem_cma_object_free(struct drm_gem_object *obj)
0059 {
0060     struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
0061 
0062     drm_gem_cma_free(cma_obj);
0063 }
0064 
0065 /**
0066  * drm_gem_cma_object_print_info() - Print &drm_gem_cma_object info for debugfs
0067  * @p: DRM printer
0068  * @indent: Tab indentation level
0069  * @obj: GEM object
0070  *
0071  * This function wraps drm_gem_cma_print_info(). Drivers that employ the CMA helpers
0072  * should use this function as their &drm_gem_object_funcs.print_info handler.
0073  */
0074 static inline void drm_gem_cma_object_print_info(struct drm_printer *p, unsigned int indent,
0075                          const struct drm_gem_object *obj)
0076 {
0077     const struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
0078 
0079     drm_gem_cma_print_info(cma_obj, p, indent);
0080 }
0081 
0082 /**
0083  * drm_gem_cma_object_get_sg_table - GEM object function for drm_gem_cma_get_sg_table()
0084  * @obj: GEM object
0085  *
0086  * This function wraps drm_gem_cma_get_sg_table(). Drivers that employ the CMA helpers should
0087  * use it as their &drm_gem_object_funcs.get_sg_table handler.
0088  *
0089  * Returns:
0090  * A pointer to the scatter/gather table of pinned pages or NULL on failure.
0091  */
0092 static inline struct sg_table *drm_gem_cma_object_get_sg_table(struct drm_gem_object *obj)
0093 {
0094     struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
0095 
0096     return drm_gem_cma_get_sg_table(cma_obj);
0097 }
0098 
0099 /*
0100  * drm_gem_cma_object_vmap - GEM object function for drm_gem_cma_vmap()
0101  * @obj: GEM object
0102  * @map: Returns the kernel virtual address of the CMA GEM object's backing store.
0103  *
0104  * This function wraps drm_gem_cma_vmap(). Drivers that employ the CMA helpers should
0105  * use it as their &drm_gem_object_funcs.vmap handler.
0106  *
0107  * Returns:
0108  * 0 on success or a negative error code on failure.
0109  */
0110 static inline int drm_gem_cma_object_vmap(struct drm_gem_object *obj,
0111                       struct iosys_map *map)
0112 {
0113     struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
0114 
0115     return drm_gem_cma_vmap(cma_obj, map);
0116 }
0117 
0118 /**
0119  * drm_gem_cma_object_mmap - GEM object function for drm_gem_cma_mmap()
0120  * @obj: GEM object
0121  * @vma: VMA for the area to be mapped
0122  *
0123  * This function wraps drm_gem_cma_mmap(). Drivers that employ the cma helpers should
0124  * use it as their &drm_gem_object_funcs.mmap handler.
0125  *
0126  * Returns:
0127  * 0 on success or a negative error code on failure.
0128  */
0129 static inline int drm_gem_cma_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
0130 {
0131     struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
0132 
0133     return drm_gem_cma_mmap(cma_obj, vma);
0134 }
0135 
0136 /*
0137  * Driver ops
0138  */
0139 
0140 /* create memory region for DRM framebuffer */
0141 int drm_gem_cma_dumb_create_internal(struct drm_file *file_priv,
0142                      struct drm_device *drm,
0143                      struct drm_mode_create_dumb *args);
0144 
0145 /* create memory region for DRM framebuffer */
0146 int drm_gem_cma_dumb_create(struct drm_file *file_priv,
0147                 struct drm_device *drm,
0148                 struct drm_mode_create_dumb *args);
0149 
0150 struct drm_gem_object *
0151 drm_gem_cma_prime_import_sg_table(struct drm_device *dev,
0152                   struct dma_buf_attachment *attach,
0153                   struct sg_table *sgt);
0154 
0155 /**
0156  * DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE - CMA GEM driver operations
0157  * @dumb_create_func: callback function for .dumb_create
0158  *
0159  * This macro provides a shortcut for setting the default GEM operations in the
0160  * &drm_driver structure.
0161  *
0162  * This macro is a variant of DRM_GEM_CMA_DRIVER_OPS for drivers that
0163  * override the default implementation of &struct rm_driver.dumb_create. Use
0164  * DRM_GEM_CMA_DRIVER_OPS if possible. Drivers that require a virtual address
0165  * on imported buffers should use
0166  * DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE() instead.
0167  */
0168 #define DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(dumb_create_func) \
0169     .dumb_create        = (dumb_create_func), \
0170     .prime_handle_to_fd = drm_gem_prime_handle_to_fd, \
0171     .prime_fd_to_handle = drm_gem_prime_fd_to_handle, \
0172     .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table, \
0173     .gem_prime_mmap     = drm_gem_prime_mmap
0174 
0175 /**
0176  * DRM_GEM_CMA_DRIVER_OPS - CMA GEM driver operations
0177  *
0178  * This macro provides a shortcut for setting the default GEM operations in the
0179  * &drm_driver structure.
0180  *
0181  * Drivers that come with their own implementation of
0182  * &struct drm_driver.dumb_create should use
0183  * DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE() instead. Use
0184  * DRM_GEM_CMA_DRIVER_OPS if possible. Drivers that require a virtual address
0185  * on imported buffers should use DRM_GEM_CMA_DRIVER_OPS_VMAP instead.
0186  */
0187 #define DRM_GEM_CMA_DRIVER_OPS \
0188     DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(drm_gem_cma_dumb_create)
0189 
0190 /**
0191  * DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE - CMA GEM driver operations
0192  *                                                ensuring a virtual address
0193  *                                                on the buffer
0194  * @dumb_create_func: callback function for .dumb_create
0195  *
0196  * This macro provides a shortcut for setting the default GEM operations in the
0197  * &drm_driver structure for drivers that need the virtual address also on
0198  * imported buffers.
0199  *
0200  * This macro is a variant of DRM_GEM_CMA_DRIVER_OPS_VMAP for drivers that
0201  * override the default implementation of &struct drm_driver.dumb_create. Use
0202  * DRM_GEM_CMA_DRIVER_OPS_VMAP if possible. Drivers that do not require a
0203  * virtual address on imported buffers should use
0204  * DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE() instead.
0205  */
0206 #define DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE(dumb_create_func) \
0207     .dumb_create        = dumb_create_func, \
0208     .prime_handle_to_fd = drm_gem_prime_handle_to_fd, \
0209     .prime_fd_to_handle = drm_gem_prime_fd_to_handle, \
0210     .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table_vmap, \
0211     .gem_prime_mmap     = drm_gem_prime_mmap
0212 
0213 /**
0214  * DRM_GEM_CMA_DRIVER_OPS_VMAP - CMA GEM driver operations ensuring a virtual
0215  *                               address on the buffer
0216  *
0217  * This macro provides a shortcut for setting the default GEM operations in the
0218  * &drm_driver structure for drivers that need the virtual address also on
0219  * imported buffers.
0220  *
0221  * Drivers that come with their own implementation of
0222  * &struct drm_driver.dumb_create should use
0223  * DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE() instead. Use
0224  * DRM_GEM_CMA_DRIVER_OPS_VMAP if possible. Drivers that do not require a
0225  * virtual address on imported buffers should use DRM_GEM_CMA_DRIVER_OPS
0226  * instead.
0227  */
0228 #define DRM_GEM_CMA_DRIVER_OPS_VMAP \
0229     DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE(drm_gem_cma_dumb_create)
0230 
0231 struct drm_gem_object *
0232 drm_gem_cma_prime_import_sg_table_vmap(struct drm_device *drm,
0233                        struct dma_buf_attachment *attach,
0234                        struct sg_table *sgt);
0235 
0236 /*
0237  * File ops
0238  */
0239 
0240 #ifndef CONFIG_MMU
0241 unsigned long drm_gem_cma_get_unmapped_area(struct file *filp,
0242                         unsigned long addr,
0243                         unsigned long len,
0244                         unsigned long pgoff,
0245                         unsigned long flags);
0246 #define DRM_GEM_CMA_UNMAPPED_AREA_FOPS \
0247     .get_unmapped_area  = drm_gem_cma_get_unmapped_area,
0248 #else
0249 #define DRM_GEM_CMA_UNMAPPED_AREA_FOPS
0250 #endif
0251 
0252 /**
0253  * DEFINE_DRM_GEM_CMA_FOPS() - macro to generate file operations for CMA drivers
0254  * @name: name for the generated structure
0255  *
0256  * This macro autogenerates a suitable &struct file_operations for CMA based
0257  * drivers, which can be assigned to &drm_driver.fops. Note that this structure
0258  * cannot be shared between drivers, because it contains a reference to the
0259  * current module using THIS_MODULE.
0260  *
0261  * Note that the declaration is already marked as static - if you need a
0262  * non-static version of this you're probably doing it wrong and will break the
0263  * THIS_MODULE reference by accident.
0264  */
0265 #define DEFINE_DRM_GEM_CMA_FOPS(name) \
0266     static const struct file_operations name = {\
0267         .owner      = THIS_MODULE,\
0268         .open       = drm_open,\
0269         .release    = drm_release,\
0270         .unlocked_ioctl = drm_ioctl,\
0271         .compat_ioctl   = drm_compat_ioctl,\
0272         .poll       = drm_poll,\
0273         .read       = drm_read,\
0274         .llseek     = noop_llseek,\
0275         .mmap       = drm_gem_mmap,\
0276         DRM_GEM_CMA_UNMAPPED_AREA_FOPS \
0277     }
0278 
0279 #endif /* __DRM_GEM_CMA_HELPER_H__ */