185438a8dSThomas Zimmermann /* SPDX-License-Identifier: GPL-2.0-or-later */
285438a8dSThomas Zimmermann
385438a8dSThomas Zimmermann #ifndef DRM_GEM_VRAM_HELPER_H
485438a8dSThomas Zimmermann #define DRM_GEM_VRAM_HELPER_H
585438a8dSThomas Zimmermann
66b5ce4a1SThomas Zimmermann #include <drm/drm_file.h>
785438a8dSThomas Zimmermann #include <drm/drm_gem.h>
8ede0c69cSThomas Zimmermann #include <drm/drm_gem_ttm_helper.h>
96b5ce4a1SThomas Zimmermann #include <drm/drm_ioctl.h>
1080f7c3f7SThomas Zimmermann #include <drm/drm_modes.h>
11a3185f91SChristian König #include <drm/ttm/ttm_bo.h>
12a3185f91SChristian König #include <drm/ttm/ttm_placement.h>
136b5ce4a1SThomas Zimmermann
14ae710a45SAndy Shevchenko #include <linux/container_of.h>
157938f421SLucas De Marchi #include <linux/iosys-map.h>
1685438a8dSThomas Zimmermann
17fed1eec0SThomas Zimmermann struct drm_mode_create_dumb;
186542ad89SThomas Zimmermann struct drm_plane;
196542ad89SThomas Zimmermann struct drm_plane_state;
206542ad89SThomas Zimmermann struct drm_simple_display_pipe;
2185438a8dSThomas Zimmermann struct filp;
2259f5989aSThomas Zimmermann struct vm_area_struct;
2385438a8dSThomas Zimmermann
247053e0eaSChristian König #define DRM_GEM_VRAM_PL_FLAG_SYSTEM (1 << 0)
257053e0eaSChristian König #define DRM_GEM_VRAM_PL_FLAG_VRAM (1 << 1)
267053e0eaSChristian König #define DRM_GEM_VRAM_PL_FLAG_TOPDOWN (1 << 2)
2785438a8dSThomas Zimmermann
2885438a8dSThomas Zimmermann /*
2985438a8dSThomas Zimmermann * Buffer-object helpers
3085438a8dSThomas Zimmermann */
3185438a8dSThomas Zimmermann
3285438a8dSThomas Zimmermann /**
3385438a8dSThomas Zimmermann * struct drm_gem_vram_object - GEM object backed by VRAM
3485438a8dSThomas Zimmermann * @bo: TTM buffer object
3549a3f51dSThomas Zimmermann * @map: Mapping information for @bo
3685438a8dSThomas Zimmermann * @placement: TTM placement information. Supported placements are \
3785438a8dSThomas Zimmermann %TTM_PL_VRAM and %TTM_PL_SYSTEM
3885438a8dSThomas Zimmermann * @placements: TTM placement information.
3985438a8dSThomas Zimmermann *
4085438a8dSThomas Zimmermann * The type struct drm_gem_vram_object represents a GEM object that is
4185438a8dSThomas Zimmermann * backed by VRAM. It can be used for simple framebuffer devices with
4285438a8dSThomas Zimmermann * dedicated memory. The buffer object can be evicted to system memory if
4385438a8dSThomas Zimmermann * video memory becomes scarce.
4437a48adfSThomas Zimmermann *
4537a48adfSThomas Zimmermann * GEM VRAM objects perform reference counting for pin and mapping
4637a48adfSThomas Zimmermann * operations. So a buffer object that has been pinned N times with
4737a48adfSThomas Zimmermann * drm_gem_vram_pin() must be unpinned N times with
4837a48adfSThomas Zimmermann * drm_gem_vram_unpin(). The same applies to pairs of
49c8908bdeSThomas Zimmermann * drm_gem_vram_kmap() and drm_gem_vram_kunmap(), as well as pairs of
50c8908bdeSThomas Zimmermann * drm_gem_vram_vmap() and drm_gem_vram_vunmap().
5185438a8dSThomas Zimmermann */
5285438a8dSThomas Zimmermann struct drm_gem_vram_object {
5385438a8dSThomas Zimmermann struct ttm_buffer_object bo;
547938f421SLucas De Marchi struct iosys_map map;
5585438a8dSThomas Zimmermann
5637a48adfSThomas Zimmermann /**
5749a3f51dSThomas Zimmermann * @vmap_use_count:
5837a48adfSThomas Zimmermann *
5937a48adfSThomas Zimmermann * Reference count on the virtual address.
6037a48adfSThomas Zimmermann * The address are un-mapped when the count reaches zero.
6137a48adfSThomas Zimmermann */
6249a3f51dSThomas Zimmermann unsigned int vmap_use_count;
6337a48adfSThomas Zimmermann
6485438a8dSThomas Zimmermann /* Supported placements are %TTM_PL_VRAM and %TTM_PL_SYSTEM */
6585438a8dSThomas Zimmermann struct ttm_placement placement;
6685438a8dSThomas Zimmermann struct ttm_place placements[2];
6785438a8dSThomas Zimmermann };
6885438a8dSThomas Zimmermann
6985438a8dSThomas Zimmermann /**
70e9d2871fSMauro Carvalho Chehab * drm_gem_vram_of_bo - Returns the container of type
71e9d2871fSMauro Carvalho Chehab * &struct drm_gem_vram_object for field bo.
7285438a8dSThomas Zimmermann * @bo: the VRAM buffer object
7385438a8dSThomas Zimmermann * Returns: The containing GEM VRAM object
7485438a8dSThomas Zimmermann */
drm_gem_vram_of_bo(struct ttm_buffer_object * bo)7585438a8dSThomas Zimmermann static inline struct drm_gem_vram_object *drm_gem_vram_of_bo(
7685438a8dSThomas Zimmermann struct ttm_buffer_object *bo)
7785438a8dSThomas Zimmermann {
7885438a8dSThomas Zimmermann return container_of(bo, struct drm_gem_vram_object, bo);
7985438a8dSThomas Zimmermann }
8085438a8dSThomas Zimmermann
8185438a8dSThomas Zimmermann /**
82e9d2871fSMauro Carvalho Chehab * drm_gem_vram_of_gem - Returns the container of type
83e9d2871fSMauro Carvalho Chehab * &struct drm_gem_vram_object for field gem.
8485438a8dSThomas Zimmermann * @gem: the GEM object
8585438a8dSThomas Zimmermann * Returns: The containing GEM VRAM object
8685438a8dSThomas Zimmermann */
drm_gem_vram_of_gem(struct drm_gem_object * gem)8785438a8dSThomas Zimmermann static inline struct drm_gem_vram_object *drm_gem_vram_of_gem(
8885438a8dSThomas Zimmermann struct drm_gem_object *gem)
8985438a8dSThomas Zimmermann {
900e580c6dSGerd Hoffmann return container_of(gem, struct drm_gem_vram_object, bo.base);
9185438a8dSThomas Zimmermann }
9285438a8dSThomas Zimmermann
9385438a8dSThomas Zimmermann struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
9485438a8dSThomas Zimmermann size_t size,
95ebe9428bSThomas Zimmermann unsigned long pg_align);
9685438a8dSThomas Zimmermann void drm_gem_vram_put(struct drm_gem_vram_object *gbo);
9785438a8dSThomas Zimmermann s64 drm_gem_vram_offset(struct drm_gem_vram_object *gbo);
9885438a8dSThomas Zimmermann int drm_gem_vram_pin(struct drm_gem_vram_object *gbo, unsigned long pl_flag);
9985438a8dSThomas Zimmermann int drm_gem_vram_unpin(struct drm_gem_vram_object *gbo);
1007938f421SLucas De Marchi int drm_gem_vram_vmap(struct drm_gem_vram_object *gbo, struct iosys_map *map);
1017938f421SLucas De Marchi void drm_gem_vram_vunmap(struct drm_gem_vram_object *gbo,
1027938f421SLucas De Marchi struct iosys_map *map);
10385438a8dSThomas Zimmermann
104fed1eec0SThomas Zimmermann int drm_gem_vram_fill_create_dumb(struct drm_file *file,
105fed1eec0SThomas Zimmermann struct drm_device *dev,
106fed1eec0SThomas Zimmermann unsigned long pg_align,
10798707327SThomas Zimmermann unsigned long pitch_align,
108fed1eec0SThomas Zimmermann struct drm_mode_create_dumb *args);
109fed1eec0SThomas Zimmermann
1106c812bc5SThomas Zimmermann /*
111737000fdSThomas Zimmermann * Helpers for struct drm_driver
112737000fdSThomas Zimmermann */
113737000fdSThomas Zimmermann
11459f5989aSThomas Zimmermann int drm_gem_vram_driver_dumb_create(struct drm_file *file,
11559f5989aSThomas Zimmermann struct drm_device *dev,
11659f5989aSThomas Zimmermann struct drm_mode_create_dumb *args);
11759f5989aSThomas Zimmermann
1186542ad89SThomas Zimmermann /*
1196542ad89SThomas Zimmermann * Helpers for struct drm_plane_helper_funcs
1206542ad89SThomas Zimmermann */
1216542ad89SThomas Zimmermann int
1226542ad89SThomas Zimmermann drm_gem_vram_plane_helper_prepare_fb(struct drm_plane *plane,
1236542ad89SThomas Zimmermann struct drm_plane_state *new_state);
1246542ad89SThomas Zimmermann void
1256542ad89SThomas Zimmermann drm_gem_vram_plane_helper_cleanup_fb(struct drm_plane *plane,
1266542ad89SThomas Zimmermann struct drm_plane_state *old_state);
1276542ad89SThomas Zimmermann
128f8bd3dbbSDaniel Vetter /**
129f8bd3dbbSDaniel Vetter * DRM_GEM_VRAM_PLANE_HELPER_FUNCS -
130f8bd3dbbSDaniel Vetter * Initializes struct drm_plane_helper_funcs for VRAM handling
131f8bd3dbbSDaniel Vetter *
132f8bd3dbbSDaniel Vetter * Drivers may use GEM BOs as VRAM helpers for the framebuffer memory. This
133f8bd3dbbSDaniel Vetter * macro initializes struct drm_plane_helper_funcs to use the respective helper
134f8bd3dbbSDaniel Vetter * functions.
135f8bd3dbbSDaniel Vetter */
136f8bd3dbbSDaniel Vetter #define DRM_GEM_VRAM_PLANE_HELPER_FUNCS \
137f8bd3dbbSDaniel Vetter .prepare_fb = drm_gem_vram_plane_helper_prepare_fb, \
138f8bd3dbbSDaniel Vetter .cleanup_fb = drm_gem_vram_plane_helper_cleanup_fb
139f8bd3dbbSDaniel Vetter
1406542ad89SThomas Zimmermann /*
1416542ad89SThomas Zimmermann * Helpers for struct drm_simple_display_pipe_funcs
1426542ad89SThomas Zimmermann */
1436542ad89SThomas Zimmermann
1446542ad89SThomas Zimmermann int drm_gem_vram_simple_display_pipe_prepare_fb(
1456542ad89SThomas Zimmermann struct drm_simple_display_pipe *pipe,
1466542ad89SThomas Zimmermann struct drm_plane_state *new_state);
1476542ad89SThomas Zimmermann
1486542ad89SThomas Zimmermann void drm_gem_vram_simple_display_pipe_cleanup_fb(
1496542ad89SThomas Zimmermann struct drm_simple_display_pipe *pipe,
1506542ad89SThomas Zimmermann struct drm_plane_state *old_state);
1516542ad89SThomas Zimmermann
15259f5989aSThomas Zimmermann /**
15359f5989aSThomas Zimmermann * define DRM_GEM_VRAM_DRIVER - default callback functions for \
15459f5989aSThomas Zimmermann &struct drm_driver
15559f5989aSThomas Zimmermann *
15659f5989aSThomas Zimmermann * Drivers that use VRAM MM and GEM VRAM can use this macro to initialize
15759f5989aSThomas Zimmermann * &struct drm_driver with default functions.
15859f5989aSThomas Zimmermann */
15959f5989aSThomas Zimmermann #define DRM_GEM_VRAM_DRIVER \
1609286766bSGerd Hoffmann .debugfs_init = drm_vram_mm_debugfs_init, \
16159f5989aSThomas Zimmermann .dumb_create = drm_gem_vram_driver_dumb_create, \
162*71e801b9SThomas Zimmermann .dumb_map_offset = drm_gem_ttm_dumb_map_offset
16359f5989aSThomas Zimmermann
1646b5ce4a1SThomas Zimmermann /*
1656b5ce4a1SThomas Zimmermann * VRAM memory manager
1666b5ce4a1SThomas Zimmermann */
1676b5ce4a1SThomas Zimmermann
1686b5ce4a1SThomas Zimmermann /**
1696b5ce4a1SThomas Zimmermann * struct drm_vram_mm - An instance of VRAM MM
1706b5ce4a1SThomas Zimmermann * @vram_base: Base address of the managed video memory
1716b5ce4a1SThomas Zimmermann * @vram_size: Size of the managed video memory in bytes
1726b5ce4a1SThomas Zimmermann * @bdev: The TTM BO device.
1736b5ce4a1SThomas Zimmermann * @funcs: TTM BO functions
1746b5ce4a1SThomas Zimmermann *
1756b5ce4a1SThomas Zimmermann * The fields &struct drm_vram_mm.vram_base and
1766b5ce4a1SThomas Zimmermann * &struct drm_vram_mm.vrm_size are managed by VRAM MM, but are
1776b5ce4a1SThomas Zimmermann * available for public read access. Use the field
1786b5ce4a1SThomas Zimmermann * &struct drm_vram_mm.bdev to access the TTM BO device.
1796b5ce4a1SThomas Zimmermann */
1806b5ce4a1SThomas Zimmermann struct drm_vram_mm {
1816b5ce4a1SThomas Zimmermann uint64_t vram_base;
1826b5ce4a1SThomas Zimmermann size_t vram_size;
1836b5ce4a1SThomas Zimmermann
1848af8a109SChristian König struct ttm_device bdev;
1856b5ce4a1SThomas Zimmermann };
1866b5ce4a1SThomas Zimmermann
1876b5ce4a1SThomas Zimmermann /**
1886b5ce4a1SThomas Zimmermann * drm_vram_mm_of_bdev() - \
1898af8a109SChristian König Returns the container of type &struct ttm_device for field bdev.
1906b5ce4a1SThomas Zimmermann * @bdev: the TTM BO device
1916b5ce4a1SThomas Zimmermann *
1926b5ce4a1SThomas Zimmermann * Returns:
1936b5ce4a1SThomas Zimmermann * The containing instance of &struct drm_vram_mm
1946b5ce4a1SThomas Zimmermann */
drm_vram_mm_of_bdev(struct ttm_device * bdev)1956b5ce4a1SThomas Zimmermann static inline struct drm_vram_mm *drm_vram_mm_of_bdev(
1968af8a109SChristian König struct ttm_device *bdev)
1976b5ce4a1SThomas Zimmermann {
1986b5ce4a1SThomas Zimmermann return container_of(bdev, struct drm_vram_mm, bdev);
1996b5ce4a1SThomas Zimmermann }
2006b5ce4a1SThomas Zimmermann
2017ce84471SWambui Karuga void drm_vram_mm_debugfs_init(struct drm_minor *minor);
2026b5ce4a1SThomas Zimmermann
2036b5ce4a1SThomas Zimmermann /*
2046b5ce4a1SThomas Zimmermann * Helpers for integration with struct drm_device
2056b5ce4a1SThomas Zimmermann */
2066b5ce4a1SThomas Zimmermann
207a5f23a72SThomas Zimmermann int drmm_vram_helper_init(struct drm_device *dev, uint64_t vram_base,
208a5f23a72SThomas Zimmermann size_t vram_size);
209a5f23a72SThomas Zimmermann
21080f7c3f7SThomas Zimmermann /*
21180f7c3f7SThomas Zimmermann * Mode-config helpers
21280f7c3f7SThomas Zimmermann */
21380f7c3f7SThomas Zimmermann
21480f7c3f7SThomas Zimmermann enum drm_mode_status
21580f7c3f7SThomas Zimmermann drm_vram_helper_mode_valid(struct drm_device *dev,
21680f7c3f7SThomas Zimmermann const struct drm_display_mode *mode);
21780f7c3f7SThomas Zimmermann
21885438a8dSThomas Zimmermann #endif
219