/* SPDX-License-Identifier: GPL-2.0 */1#ifndef __DRM_GEM_DMA_HELPER_H__2#define __DRM_GEM_DMA_HELPER_H__34#include <drm/drm_file.h>5#include <drm/drm_ioctl.h>6#include <drm/drm_gem.h>78struct drm_mode_create_dumb;910/**11* struct drm_gem_dma_object - GEM object backed by DMA memory allocations12* @base: base GEM object13* @dma_addr: DMA address of the backing memory14* @sgt: scatter/gather table for imported PRIME buffers. The table can have15* more than one entry but they are guaranteed to have contiguous16* DMA addresses.17* @vaddr: kernel virtual address of the backing memory18* @map_noncoherent: if true, the GEM object is backed by non-coherent memory19*/20struct drm_gem_dma_object {21struct drm_gem_object base;22dma_addr_t dma_addr;23struct sg_table *sgt;2425/* For objects with DMA memory allocated by GEM DMA */26void *vaddr;2728bool map_noncoherent;29};3031#define to_drm_gem_dma_obj(gem_obj) \32container_of(gem_obj, struct drm_gem_dma_object, base)3334struct drm_gem_dma_object *drm_gem_dma_create(struct drm_device *drm,35size_t size);36void drm_gem_dma_free(struct drm_gem_dma_object *dma_obj);37void drm_gem_dma_print_info(const struct drm_gem_dma_object *dma_obj,38struct drm_printer *p, unsigned int indent);39struct sg_table *drm_gem_dma_get_sg_table(struct drm_gem_dma_object *dma_obj);40int drm_gem_dma_vmap(struct drm_gem_dma_object *dma_obj,41struct iosys_map *map);42int drm_gem_dma_mmap(struct drm_gem_dma_object *dma_obj, struct vm_area_struct *vma);4344extern const struct vm_operations_struct drm_gem_dma_vm_ops;4546/*47* GEM object functions48*/4950/**51* drm_gem_dma_object_free - GEM object function for drm_gem_dma_free()52* @obj: GEM object to free53*54* This function wraps drm_gem_dma_free_object(). Drivers that employ the DMA helpers55* should use it as their &drm_gem_object_funcs.free handler.56*/57static inline void drm_gem_dma_object_free(struct drm_gem_object *obj)58{59struct drm_gem_dma_object *dma_obj = to_drm_gem_dma_obj(obj);6061drm_gem_dma_free(dma_obj);62}6364/**65* drm_gem_dma_object_print_info() - Print &drm_gem_dma_object info for debugfs66* @p: DRM printer67* @indent: Tab indentation level68* @obj: GEM object69*70* This function wraps drm_gem_dma_print_info(). Drivers that employ the DMA helpers71* should use this function as their &drm_gem_object_funcs.print_info handler.72*/73static inline void drm_gem_dma_object_print_info(struct drm_printer *p, unsigned int indent,74const struct drm_gem_object *obj)75{76const struct drm_gem_dma_object *dma_obj = to_drm_gem_dma_obj(obj);7778drm_gem_dma_print_info(dma_obj, p, indent);79}8081/**82* drm_gem_dma_object_get_sg_table - GEM object function for drm_gem_dma_get_sg_table()83* @obj: GEM object84*85* This function wraps drm_gem_dma_get_sg_table(). Drivers that employ the DMA helpers should86* use it as their &drm_gem_object_funcs.get_sg_table handler.87*88* Returns:89* A pointer to the scatter/gather table of pinned pages or NULL on failure.90*/91static inline struct sg_table *drm_gem_dma_object_get_sg_table(struct drm_gem_object *obj)92{93struct drm_gem_dma_object *dma_obj = to_drm_gem_dma_obj(obj);9495return drm_gem_dma_get_sg_table(dma_obj);96}9798/*99* drm_gem_dma_object_vmap - GEM object function for drm_gem_dma_vmap()100* @obj: GEM object101* @map: Returns the kernel virtual address of the DMA GEM object's backing store.102*103* This function wraps drm_gem_dma_vmap(). Drivers that employ the DMA helpers should104* use it as their &drm_gem_object_funcs.vmap handler.105*106* Returns:107* 0 on success or a negative error code on failure.108*/109static inline int drm_gem_dma_object_vmap(struct drm_gem_object *obj,110struct iosys_map *map)111{112struct drm_gem_dma_object *dma_obj = to_drm_gem_dma_obj(obj);113114return drm_gem_dma_vmap(dma_obj, map);115}116117/**118* drm_gem_dma_object_mmap - GEM object function for drm_gem_dma_mmap()119* @obj: GEM object120* @vma: VMA for the area to be mapped121*122* This function wraps drm_gem_dma_mmap(). Drivers that employ the dma helpers should123* use it as their &drm_gem_object_funcs.mmap handler.124*125* Returns:126* 0 on success or a negative error code on failure.127*/128static inline int drm_gem_dma_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)129{130struct drm_gem_dma_object *dma_obj = to_drm_gem_dma_obj(obj);131132return drm_gem_dma_mmap(dma_obj, vma);133}134135/*136* Driver ops137*/138139/* create memory region for DRM framebuffer */140int drm_gem_dma_dumb_create_internal(struct drm_file *file_priv,141struct drm_device *drm,142struct drm_mode_create_dumb *args);143144/* create memory region for DRM framebuffer */145int drm_gem_dma_dumb_create(struct drm_file *file_priv,146struct drm_device *drm,147struct drm_mode_create_dumb *args);148149struct drm_gem_object *150drm_gem_dma_prime_import_sg_table(struct drm_device *dev,151struct dma_buf_attachment *attach,152struct sg_table *sgt);153154/**155* DRM_GEM_DMA_DRIVER_OPS_WITH_DUMB_CREATE - DMA GEM driver operations156* @dumb_create_func: callback function for .dumb_create157*158* This macro provides a shortcut for setting the default GEM operations in the159* &drm_driver structure.160*161* This macro is a variant of DRM_GEM_DMA_DRIVER_OPS for drivers that162* override the default implementation of &struct rm_driver.dumb_create. Use163* DRM_GEM_DMA_DRIVER_OPS if possible. Drivers that require a virtual address164* on imported buffers should use165* DRM_GEM_DMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE() instead.166*/167#define DRM_GEM_DMA_DRIVER_OPS_WITH_DUMB_CREATE(dumb_create_func) \168.dumb_create = (dumb_create_func), \169.gem_prime_import_sg_table = drm_gem_dma_prime_import_sg_table170171/**172* DRM_GEM_DMA_DRIVER_OPS - DMA GEM driver operations173*174* This macro provides a shortcut for setting the default GEM operations in the175* &drm_driver structure.176*177* Drivers that come with their own implementation of178* &struct drm_driver.dumb_create should use179* DRM_GEM_DMA_DRIVER_OPS_WITH_DUMB_CREATE() instead. Use180* DRM_GEM_DMA_DRIVER_OPS if possible. Drivers that require a virtual address181* on imported buffers should use DRM_GEM_DMA_DRIVER_OPS_VMAP instead.182*/183#define DRM_GEM_DMA_DRIVER_OPS \184DRM_GEM_DMA_DRIVER_OPS_WITH_DUMB_CREATE(drm_gem_dma_dumb_create)185186/**187* DRM_GEM_DMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE - DMA GEM driver operations188* ensuring a virtual address189* on the buffer190* @dumb_create_func: callback function for .dumb_create191*192* This macro provides a shortcut for setting the default GEM operations in the193* &drm_driver structure for drivers that need the virtual address also on194* imported buffers.195*196* This macro is a variant of DRM_GEM_DMA_DRIVER_OPS_VMAP for drivers that197* override the default implementation of &struct drm_driver.dumb_create. Use198* DRM_GEM_DMA_DRIVER_OPS_VMAP if possible. Drivers that do not require a199* virtual address on imported buffers should use200* DRM_GEM_DMA_DRIVER_OPS_WITH_DUMB_CREATE() instead.201*/202#define DRM_GEM_DMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE(dumb_create_func) \203.dumb_create = (dumb_create_func), \204.gem_prime_import_sg_table = drm_gem_dma_prime_import_sg_table_vmap205206/**207* DRM_GEM_DMA_DRIVER_OPS_VMAP - DMA GEM driver operations ensuring a virtual208* address on the buffer209*210* This macro provides a shortcut for setting the default GEM operations in the211* &drm_driver structure for drivers that need the virtual address also on212* imported buffers.213*214* Drivers that come with their own implementation of215* &struct drm_driver.dumb_create should use216* DRM_GEM_DMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE() instead. Use217* DRM_GEM_DMA_DRIVER_OPS_VMAP if possible. Drivers that do not require a218* virtual address on imported buffers should use DRM_GEM_DMA_DRIVER_OPS219* instead.220*/221#define DRM_GEM_DMA_DRIVER_OPS_VMAP \222DRM_GEM_DMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE(drm_gem_dma_dumb_create)223224struct drm_gem_object *225drm_gem_dma_prime_import_sg_table_vmap(struct drm_device *drm,226struct dma_buf_attachment *attach,227struct sg_table *sgt);228229/*230* File ops231*/232233#ifndef CONFIG_MMU234unsigned long drm_gem_dma_get_unmapped_area(struct file *filp,235unsigned long addr,236unsigned long len,237unsigned long pgoff,238unsigned long flags);239#define DRM_GEM_DMA_UNMAPPED_AREA_FOPS \240.get_unmapped_area = drm_gem_dma_get_unmapped_area,241#else242#define DRM_GEM_DMA_UNMAPPED_AREA_FOPS243#endif244245/**246* DEFINE_DRM_GEM_DMA_FOPS() - macro to generate file operations for DMA drivers247* @name: name for the generated structure248*249* This macro autogenerates a suitable &struct file_operations for DMA based250* drivers, which can be assigned to &drm_driver.fops. Note that this structure251* cannot be shared between drivers, because it contains a reference to the252* current module using THIS_MODULE.253*254* Note that the declaration is already marked as static - if you need a255* non-static version of this you're probably doing it wrong and will break the256* THIS_MODULE reference by accident.257*/258#define DEFINE_DRM_GEM_DMA_FOPS(name) \259static const struct file_operations name = {\260.owner = THIS_MODULE,\261.open = drm_open,\262.release = drm_release,\263.unlocked_ioctl = drm_ioctl,\264.compat_ioctl = drm_compat_ioctl,\265.poll = drm_poll,\266.read = drm_read,\267.llseek = noop_llseek,\268.mmap = drm_gem_mmap,\269.fop_flags = FOP_UNSIGNED_OFFSET, \270DRM_GEM_DMA_UNMAPPED_AREA_FOPS \271}272273#endif /* __DRM_GEM_DMA_HELPER_H__ */274275276