218 lines · c
1/* SPDX-License-Identifier: GPL-2.0-or-later */2 3#ifndef DRM_GEM_VRAM_HELPER_H4#define DRM_GEM_VRAM_HELPER_H5 6#include <drm/drm_file.h>7#include <drm/drm_gem.h>8#include <drm/drm_gem_ttm_helper.h>9#include <drm/drm_ioctl.h>10#include <drm/drm_modes.h>11#include <drm/ttm/ttm_bo.h>12#include <drm/ttm/ttm_placement.h>13 14#include <linux/container_of.h>15#include <linux/iosys-map.h>16 17struct drm_mode_create_dumb;18struct drm_plane;19struct drm_plane_state;20struct drm_simple_display_pipe;21struct filp;22struct vm_area_struct;23 24#define DRM_GEM_VRAM_PL_FLAG_SYSTEM (1 << 0)25#define DRM_GEM_VRAM_PL_FLAG_VRAM (1 << 1)26#define DRM_GEM_VRAM_PL_FLAG_TOPDOWN (1 << 2)27 28/*29 * Buffer-object helpers30 */31 32/**33 * struct drm_gem_vram_object - GEM object backed by VRAM34 * @bo: TTM buffer object35 * @map: Mapping information for @bo36 * @placement: TTM placement information. Supported placements are %TTM_PL_VRAM37 * and %TTM_PL_SYSTEM38 * @placements: TTM placement information.39 *40 * The type struct drm_gem_vram_object represents a GEM object that is41 * backed by VRAM. It can be used for simple framebuffer devices with42 * dedicated memory. The buffer object can be evicted to system memory if43 * video memory becomes scarce.44 *45 * GEM VRAM objects perform reference counting for pin and mapping46 * operations. So a buffer object that has been pinned N times with47 * drm_gem_vram_pin() must be unpinned N times with48 * drm_gem_vram_unpin(). The same applies to pairs of49 * drm_gem_vram_kmap() and drm_gem_vram_kunmap(), as well as pairs of50 * drm_gem_vram_vmap() and drm_gem_vram_vunmap().51 */52struct drm_gem_vram_object {53 struct ttm_buffer_object bo;54 struct iosys_map map;55 56 /**57 * @vmap_use_count:58 *59 * Reference count on the virtual address.60 * The address are un-mapped when the count reaches zero.61 */62 unsigned int vmap_use_count;63 64 /* Supported placements are %TTM_PL_VRAM and %TTM_PL_SYSTEM */65 struct ttm_placement placement;66 struct ttm_place placements[2];67};68 69/**70 * drm_gem_vram_of_bo - Returns the container of type71 * &struct drm_gem_vram_object for field bo.72 * @bo: the VRAM buffer object73 * Returns: The containing GEM VRAM object74 */75static inline struct drm_gem_vram_object *drm_gem_vram_of_bo(76 struct ttm_buffer_object *bo)77{78 return container_of(bo, struct drm_gem_vram_object, bo);79}80 81/**82 * drm_gem_vram_of_gem - Returns the container of type83 * &struct drm_gem_vram_object for field gem.84 * @gem: the GEM object85 * Returns: The containing GEM VRAM object86 */87static inline struct drm_gem_vram_object *drm_gem_vram_of_gem(88 struct drm_gem_object *gem)89{90 return container_of(gem, struct drm_gem_vram_object, bo.base);91}92 93struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,94 size_t size,95 unsigned long pg_align);96void drm_gem_vram_put(struct drm_gem_vram_object *gbo);97s64 drm_gem_vram_offset(struct drm_gem_vram_object *gbo);98int drm_gem_vram_pin(struct drm_gem_vram_object *gbo, unsigned long pl_flag);99int drm_gem_vram_unpin(struct drm_gem_vram_object *gbo);100int drm_gem_vram_vmap(struct drm_gem_vram_object *gbo, struct iosys_map *map);101void drm_gem_vram_vunmap(struct drm_gem_vram_object *gbo,102 struct iosys_map *map);103 104int drm_gem_vram_fill_create_dumb(struct drm_file *file,105 struct drm_device *dev,106 unsigned long pg_align,107 unsigned long pitch_align,108 struct drm_mode_create_dumb *args);109 110/*111 * Helpers for struct drm_driver112 */113 114int drm_gem_vram_driver_dumb_create(struct drm_file *file,115 struct drm_device *dev,116 struct drm_mode_create_dumb *args);117 118/*119 * Helpers for struct drm_plane_helper_funcs120 */121int122drm_gem_vram_plane_helper_prepare_fb(struct drm_plane *plane,123 struct drm_plane_state *new_state);124void125drm_gem_vram_plane_helper_cleanup_fb(struct drm_plane *plane,126 struct drm_plane_state *old_state);127 128/**129 * DRM_GEM_VRAM_PLANE_HELPER_FUNCS - Initializes struct drm_plane_helper_funcs130 * for VRAM handling131 *132 * Drivers may use GEM BOs as VRAM helpers for the framebuffer memory. This133 * macro initializes struct drm_plane_helper_funcs to use the respective helper134 * functions.135 */136#define DRM_GEM_VRAM_PLANE_HELPER_FUNCS \137 .prepare_fb = drm_gem_vram_plane_helper_prepare_fb, \138 .cleanup_fb = drm_gem_vram_plane_helper_cleanup_fb139 140/*141 * Helpers for struct drm_simple_display_pipe_funcs142 */143 144int drm_gem_vram_simple_display_pipe_prepare_fb(145 struct drm_simple_display_pipe *pipe,146 struct drm_plane_state *new_state);147 148void drm_gem_vram_simple_display_pipe_cleanup_fb(149 struct drm_simple_display_pipe *pipe,150 struct drm_plane_state *old_state);151 152/**153 * define DRM_GEM_VRAM_DRIVER - default callback functions for154 * &struct drm_driver155 *156 * Drivers that use VRAM MM and GEM VRAM can use this macro to initialize157 * &struct drm_driver with default functions.158 */159#define DRM_GEM_VRAM_DRIVER \160 .debugfs_init = drm_vram_mm_debugfs_init, \161 .dumb_create = drm_gem_vram_driver_dumb_create, \162 .dumb_map_offset = drm_gem_ttm_dumb_map_offset163 164/*165 * VRAM memory manager166 */167 168/**169 * struct drm_vram_mm - An instance of VRAM MM170 * @vram_base: Base address of the managed video memory171 * @vram_size: Size of the managed video memory in bytes172 * @bdev: The TTM BO device.173 *174 * The fields &struct drm_vram_mm.vram_base and175 * &struct drm_vram_mm.vrm_size are managed by VRAM MM, but are176 * available for public read access. Use the field177 * &struct drm_vram_mm.bdev to access the TTM BO device.178 */179struct drm_vram_mm {180 uint64_t vram_base;181 size_t vram_size;182 183 struct ttm_device bdev;184};185 186/**187 * drm_vram_mm_of_bdev() - Returns the container of type &struct ttm_device for188 * field bdev.189 * @bdev: the TTM BO device190 *191 * Returns:192 * The containing instance of &struct drm_vram_mm193 */194static inline struct drm_vram_mm *drm_vram_mm_of_bdev(195 struct ttm_device *bdev)196{197 return container_of(bdev, struct drm_vram_mm, bdev);198}199 200void drm_vram_mm_debugfs_init(struct drm_minor *minor);201 202/*203 * Helpers for integration with struct drm_device204 */205 206int drmm_vram_helper_init(struct drm_device *dev, uint64_t vram_base,207 size_t vram_size);208 209/*210 * Mode-config helpers211 */212 213enum drm_mode_status214drm_vram_helper_mode_valid(struct drm_device *dev,215 const struct drm_display_mode *mode);216 217#endif218