Thomas Zimmermann | 85438a8 | 2019-05-08 10:26:11 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 | |
| 3 | #ifndef DRM_GEM_VRAM_HELPER_H |
| 4 | #define DRM_GEM_VRAM_HELPER_H |
| 5 | |
Thomas Zimmermann | 6b5ce4a | 2019-09-11 13:09:07 +0200 | [diff] [blame] | 6 | #include <drm/drm_file.h> |
Thomas Zimmermann | 85438a8 | 2019-05-08 10:26:11 +0200 | [diff] [blame] | 7 | #include <drm/drm_gem.h> |
Thomas Zimmermann | ede0c69 | 2021-04-08 16:01:37 +0200 | [diff] [blame] | 8 | #include <drm/drm_gem_ttm_helper.h> |
Thomas Zimmermann | 6b5ce4a | 2019-09-11 13:09:07 +0200 | [diff] [blame] | 9 | #include <drm/drm_ioctl.h> |
Thomas Zimmermann | 80f7c3f | 2020-02-03 16:52:55 +0100 | [diff] [blame] | 10 | #include <drm/drm_modes.h> |
Thomas Zimmermann | 85438a8 | 2019-05-08 10:26:11 +0200 | [diff] [blame] | 11 | #include <drm/ttm/ttm_bo_api.h> |
Thomas Zimmermann | 6b5ce4a | 2019-09-11 13:09:07 +0200 | [diff] [blame] | 12 | #include <drm/ttm/ttm_bo_driver.h> |
Thomas Zimmermann | 6b5ce4a | 2019-09-11 13:09:07 +0200 | [diff] [blame] | 13 | |
Thomas Zimmermann | 49a3f51 | 2020-11-03 10:30:11 +0100 | [diff] [blame] | 14 | #include <linux/dma-buf-map.h> |
Thomas Zimmermann | 85438a8 | 2019-05-08 10:26:11 +0200 | [diff] [blame] | 15 | #include <linux/kernel.h> /* for container_of() */ |
| 16 | |
Thomas Zimmermann | fed1eec | 2019-05-08 10:26:14 +0200 | [diff] [blame] | 17 | struct drm_mode_create_dumb; |
Thomas Zimmermann | 6542ad8 | 2019-10-24 10:14:01 +0200 | [diff] [blame] | 18 | struct drm_plane; |
| 19 | struct drm_plane_state; |
| 20 | struct drm_simple_display_pipe; |
Thomas Zimmermann | 85438a8 | 2019-05-08 10:26:11 +0200 | [diff] [blame] | 21 | struct filp; |
Thomas Zimmermann | 59f5989 | 2019-05-08 10:26:18 +0200 | [diff] [blame] | 22 | struct vm_area_struct; |
Thomas Zimmermann | 85438a8 | 2019-05-08 10:26:11 +0200 | [diff] [blame] | 23 | |
Christian König | 7053e0e | 2020-09-07 16:14:52 +0200 | [diff] [blame] | 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) |
Thomas Zimmermann | 85438a8 | 2019-05-08 10:26:11 +0200 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | * Buffer-object helpers |
| 30 | */ |
| 31 | |
| 32 | /** |
| 33 | * struct drm_gem_vram_object - GEM object backed by VRAM |
Thomas Zimmermann | 85438a8 | 2019-05-08 10:26:11 +0200 | [diff] [blame] | 34 | * @bo: TTM buffer object |
Thomas Zimmermann | 49a3f51 | 2020-11-03 10:30:11 +0100 | [diff] [blame] | 35 | * @map: Mapping information for @bo |
Thomas Zimmermann | 85438a8 | 2019-05-08 10:26:11 +0200 | [diff] [blame] | 36 | * @placement: TTM placement information. Supported placements are \ |
| 37 | %TTM_PL_VRAM and %TTM_PL_SYSTEM |
| 38 | * @placements: TTM placement information. |
Thomas Zimmermann | 85438a8 | 2019-05-08 10:26:11 +0200 | [diff] [blame] | 39 | * |
| 40 | * The type struct drm_gem_vram_object represents a GEM object that is |
| 41 | * backed by VRAM. It can be used for simple framebuffer devices with |
| 42 | * dedicated memory. The buffer object can be evicted to system memory if |
| 43 | * video memory becomes scarce. |
Thomas Zimmermann | 37a48ad | 2019-09-06 14:20:53 +0200 | [diff] [blame] | 44 | * |
| 45 | * GEM VRAM objects perform reference counting for pin and mapping |
| 46 | * operations. So a buffer object that has been pinned N times with |
| 47 | * drm_gem_vram_pin() must be unpinned N times with |
| 48 | * drm_gem_vram_unpin(). The same applies to pairs of |
Thomas Zimmermann | c8908bd | 2019-09-11 14:03:50 +0200 | [diff] [blame] | 49 | * drm_gem_vram_kmap() and drm_gem_vram_kunmap(), as well as pairs of |
| 50 | * drm_gem_vram_vmap() and drm_gem_vram_vunmap(). |
Thomas Zimmermann | 85438a8 | 2019-05-08 10:26:11 +0200 | [diff] [blame] | 51 | */ |
| 52 | struct drm_gem_vram_object { |
Thomas Zimmermann | 85438a8 | 2019-05-08 10:26:11 +0200 | [diff] [blame] | 53 | struct ttm_buffer_object bo; |
Thomas Zimmermann | 49a3f51 | 2020-11-03 10:30:11 +0100 | [diff] [blame] | 54 | struct dma_buf_map map; |
Thomas Zimmermann | 85438a8 | 2019-05-08 10:26:11 +0200 | [diff] [blame] | 55 | |
Thomas Zimmermann | 37a48ad | 2019-09-06 14:20:53 +0200 | [diff] [blame] | 56 | /** |
Thomas Zimmermann | 49a3f51 | 2020-11-03 10:30:11 +0100 | [diff] [blame] | 57 | * @vmap_use_count: |
Thomas Zimmermann | 37a48ad | 2019-09-06 14:20:53 +0200 | [diff] [blame] | 58 | * |
| 59 | * Reference count on the virtual address. |
| 60 | * The address are un-mapped when the count reaches zero. |
| 61 | */ |
Thomas Zimmermann | 49a3f51 | 2020-11-03 10:30:11 +0100 | [diff] [blame] | 62 | unsigned int vmap_use_count; |
Thomas Zimmermann | 37a48ad | 2019-09-06 14:20:53 +0200 | [diff] [blame] | 63 | |
Thomas Zimmermann | 85438a8 | 2019-05-08 10:26:11 +0200 | [diff] [blame] | 64 | /* Supported placements are %TTM_PL_VRAM and %TTM_PL_SYSTEM */ |
| 65 | struct ttm_placement placement; |
| 66 | struct ttm_place placements[2]; |
Thomas Zimmermann | 85438a8 | 2019-05-08 10:26:11 +0200 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | /** |
Mauro Carvalho Chehab | e9d2871 | 2020-11-16 11:18:01 +0100 | [diff] [blame] | 70 | * drm_gem_vram_of_bo - Returns the container of type |
| 71 | * &struct drm_gem_vram_object for field bo. |
Thomas Zimmermann | 85438a8 | 2019-05-08 10:26:11 +0200 | [diff] [blame] | 72 | * @bo: the VRAM buffer object |
| 73 | * Returns: The containing GEM VRAM object |
| 74 | */ |
| 75 | static 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 | /** |
Mauro Carvalho Chehab | e9d2871 | 2020-11-16 11:18:01 +0100 | [diff] [blame] | 82 | * drm_gem_vram_of_gem - Returns the container of type |
| 83 | * &struct drm_gem_vram_object for field gem. |
Thomas Zimmermann | 85438a8 | 2019-05-08 10:26:11 +0200 | [diff] [blame] | 84 | * @gem: the GEM object |
| 85 | * Returns: The containing GEM VRAM object |
| 86 | */ |
| 87 | static inline struct drm_gem_vram_object *drm_gem_vram_of_gem( |
| 88 | struct drm_gem_object *gem) |
| 89 | { |
Gerd Hoffmann | 0e580c6 | 2019-08-05 16:01:04 +0200 | [diff] [blame] | 90 | return container_of(gem, struct drm_gem_vram_object, bo.base); |
Thomas Zimmermann | 85438a8 | 2019-05-08 10:26:11 +0200 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev, |
Thomas Zimmermann | 85438a8 | 2019-05-08 10:26:11 +0200 | [diff] [blame] | 94 | size_t size, |
Thomas Zimmermann | ebe9428 | 2020-01-06 13:57:43 +0100 | [diff] [blame] | 95 | unsigned long pg_align); |
Thomas Zimmermann | 85438a8 | 2019-05-08 10:26:11 +0200 | [diff] [blame] | 96 | void drm_gem_vram_put(struct drm_gem_vram_object *gbo); |
Thomas Zimmermann | 85438a8 | 2019-05-08 10:26:11 +0200 | [diff] [blame] | 97 | s64 drm_gem_vram_offset(struct drm_gem_vram_object *gbo); |
| 98 | int drm_gem_vram_pin(struct drm_gem_vram_object *gbo, unsigned long pl_flag); |
| 99 | int drm_gem_vram_unpin(struct drm_gem_vram_object *gbo); |
Thomas Zimmermann | 49a3f51 | 2020-11-03 10:30:11 +0100 | [diff] [blame] | 100 | int drm_gem_vram_vmap(struct drm_gem_vram_object *gbo, struct dma_buf_map *map); |
| 101 | void drm_gem_vram_vunmap(struct drm_gem_vram_object *gbo, struct dma_buf_map *map); |
Thomas Zimmermann | 85438a8 | 2019-05-08 10:26:11 +0200 | [diff] [blame] | 102 | |
Thomas Zimmermann | fed1eec | 2019-05-08 10:26:14 +0200 | [diff] [blame] | 103 | int drm_gem_vram_fill_create_dumb(struct drm_file *file, |
| 104 | struct drm_device *dev, |
Thomas Zimmermann | fed1eec | 2019-05-08 10:26:14 +0200 | [diff] [blame] | 105 | unsigned long pg_align, |
Thomas Zimmermann | 9870732 | 2019-12-03 09:38:17 +0100 | [diff] [blame] | 106 | unsigned long pitch_align, |
Thomas Zimmermann | fed1eec | 2019-05-08 10:26:14 +0200 | [diff] [blame] | 107 | struct drm_mode_create_dumb *args); |
| 108 | |
Thomas Zimmermann | 6c812bc | 2019-05-08 10:26:12 +0200 | [diff] [blame] | 109 | /* |
Thomas Zimmermann | 737000f | 2019-05-08 10:26:13 +0200 | [diff] [blame] | 110 | * Helpers for struct drm_driver |
| 111 | */ |
| 112 | |
Thomas Zimmermann | 59f5989 | 2019-05-08 10:26:18 +0200 | [diff] [blame] | 113 | int drm_gem_vram_driver_dumb_create(struct drm_file *file, |
| 114 | struct drm_device *dev, |
| 115 | struct drm_mode_create_dumb *args); |
Thomas Zimmermann | 59f5989 | 2019-05-08 10:26:18 +0200 | [diff] [blame] | 116 | |
Thomas Zimmermann | 6542ad8 | 2019-10-24 10:14:01 +0200 | [diff] [blame] | 117 | /* |
| 118 | * Helpers for struct drm_plane_helper_funcs |
| 119 | */ |
| 120 | int |
| 121 | drm_gem_vram_plane_helper_prepare_fb(struct drm_plane *plane, |
| 122 | struct drm_plane_state *new_state); |
| 123 | void |
| 124 | drm_gem_vram_plane_helper_cleanup_fb(struct drm_plane *plane, |
| 125 | struct drm_plane_state *old_state); |
| 126 | |
| 127 | /* |
| 128 | * Helpers for struct drm_simple_display_pipe_funcs |
| 129 | */ |
| 130 | |
| 131 | int drm_gem_vram_simple_display_pipe_prepare_fb( |
| 132 | struct drm_simple_display_pipe *pipe, |
| 133 | struct drm_plane_state *new_state); |
| 134 | |
| 135 | void drm_gem_vram_simple_display_pipe_cleanup_fb( |
| 136 | struct drm_simple_display_pipe *pipe, |
| 137 | struct drm_plane_state *old_state); |
| 138 | |
Thomas Zimmermann | 59f5989 | 2019-05-08 10:26:18 +0200 | [diff] [blame] | 139 | /** |
| 140 | * define DRM_GEM_VRAM_DRIVER - default callback functions for \ |
| 141 | &struct drm_driver |
| 142 | * |
| 143 | * Drivers that use VRAM MM and GEM VRAM can use this macro to initialize |
| 144 | * &struct drm_driver with default functions. |
| 145 | */ |
| 146 | #define DRM_GEM_VRAM_DRIVER \ |
Gerd Hoffmann | 9286766 | 2019-09-04 07:47:37 +0200 | [diff] [blame] | 147 | .debugfs_init = drm_vram_mm_debugfs_init, \ |
Thomas Zimmermann | 59f5989 | 2019-05-08 10:26:18 +0200 | [diff] [blame] | 148 | .dumb_create = drm_gem_vram_driver_dumb_create, \ |
Thomas Zimmermann | ede0c69 | 2021-04-08 16:01:37 +0200 | [diff] [blame] | 149 | .dumb_map_offset = drm_gem_ttm_dumb_map_offset, \ |
Thomas Zimmermann | 31070a8 | 2019-07-02 13:50:08 +0200 | [diff] [blame] | 150 | .gem_prime_mmap = drm_gem_prime_mmap |
Thomas Zimmermann | 59f5989 | 2019-05-08 10:26:18 +0200 | [diff] [blame] | 151 | |
Thomas Zimmermann | 6b5ce4a | 2019-09-11 13:09:07 +0200 | [diff] [blame] | 152 | /* |
| 153 | * VRAM memory manager |
| 154 | */ |
| 155 | |
| 156 | /** |
| 157 | * struct drm_vram_mm - An instance of VRAM MM |
| 158 | * @vram_base: Base address of the managed video memory |
| 159 | * @vram_size: Size of the managed video memory in bytes |
| 160 | * @bdev: The TTM BO device. |
| 161 | * @funcs: TTM BO functions |
| 162 | * |
| 163 | * The fields &struct drm_vram_mm.vram_base and |
| 164 | * &struct drm_vram_mm.vrm_size are managed by VRAM MM, but are |
| 165 | * available for public read access. Use the field |
| 166 | * &struct drm_vram_mm.bdev to access the TTM BO device. |
| 167 | */ |
| 168 | struct drm_vram_mm { |
| 169 | uint64_t vram_base; |
| 170 | size_t vram_size; |
| 171 | |
Christian König | 8af8a10 | 2020-10-01 14:51:40 +0200 | [diff] [blame] | 172 | struct ttm_device bdev; |
Thomas Zimmermann | 6b5ce4a | 2019-09-11 13:09:07 +0200 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | /** |
| 176 | * drm_vram_mm_of_bdev() - \ |
Christian König | 8af8a10 | 2020-10-01 14:51:40 +0200 | [diff] [blame] | 177 | Returns the container of type &struct ttm_device for field bdev. |
Thomas Zimmermann | 6b5ce4a | 2019-09-11 13:09:07 +0200 | [diff] [blame] | 178 | * @bdev: the TTM BO device |
| 179 | * |
| 180 | * Returns: |
| 181 | * The containing instance of &struct drm_vram_mm |
| 182 | */ |
| 183 | static inline struct drm_vram_mm *drm_vram_mm_of_bdev( |
Christian König | 8af8a10 | 2020-10-01 14:51:40 +0200 | [diff] [blame] | 184 | struct ttm_device *bdev) |
Thomas Zimmermann | 6b5ce4a | 2019-09-11 13:09:07 +0200 | [diff] [blame] | 185 | { |
| 186 | return container_of(bdev, struct drm_vram_mm, bdev); |
| 187 | } |
| 188 | |
Wambui Karuga | 7ce84471 | 2020-03-10 16:31:21 +0300 | [diff] [blame] | 189 | void drm_vram_mm_debugfs_init(struct drm_minor *minor); |
Thomas Zimmermann | 6b5ce4a | 2019-09-11 13:09:07 +0200 | [diff] [blame] | 190 | |
| 191 | /* |
| 192 | * Helpers for integration with struct drm_device |
| 193 | */ |
| 194 | |
| 195 | struct drm_vram_mm *drm_vram_helper_alloc_mm( |
Thomas Zimmermann | b0e40e0 | 2019-09-11 13:09:08 +0200 | [diff] [blame] | 196 | struct drm_device *dev, uint64_t vram_base, size_t vram_size); |
Thomas Zimmermann | 6b5ce4a | 2019-09-11 13:09:07 +0200 | [diff] [blame] | 197 | void drm_vram_helper_release_mm(struct drm_device *dev); |
| 198 | |
Thomas Zimmermann | a5f23a7 | 2020-07-16 14:53:48 +0200 | [diff] [blame] | 199 | int drmm_vram_helper_init(struct drm_device *dev, uint64_t vram_base, |
| 200 | size_t vram_size); |
| 201 | |
Thomas Zimmermann | 80f7c3f | 2020-02-03 16:52:55 +0100 | [diff] [blame] | 202 | /* |
| 203 | * Mode-config helpers |
| 204 | */ |
| 205 | |
| 206 | enum drm_mode_status |
| 207 | drm_vram_helper_mode_valid(struct drm_device *dev, |
| 208 | const struct drm_display_mode *mode); |
| 209 | |
Thomas Zimmermann | 85438a8 | 2019-05-08 10:26:11 +0200 | [diff] [blame] | 210 | #endif |