blob: 27ed7e9243b9f3189b27ef41863e44b696e03c2b [file] [log] [blame]
Thomas Zimmermann85438a82019-05-08 10:26:11 +02001/* 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 Zimmermann6b5ce4a2019-09-11 13:09:07 +02006#include <drm/drm_file.h>
Thomas Zimmermann85438a82019-05-08 10:26:11 +02007#include <drm/drm_gem.h>
Thomas Zimmermannede0c692021-04-08 16:01:37 +02008#include <drm/drm_gem_ttm_helper.h>
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +02009#include <drm/drm_ioctl.h>
Thomas Zimmermann80f7c3f2020-02-03 16:52:55 +010010#include <drm/drm_modes.h>
Thomas Zimmermann85438a82019-05-08 10:26:11 +020011#include <drm/ttm/ttm_bo_api.h>
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +020012#include <drm/ttm/ttm_bo_driver.h>
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +020013
Thomas Zimmermann49a3f512020-11-03 10:30:11 +010014#include <linux/dma-buf-map.h>
Thomas Zimmermann85438a82019-05-08 10:26:11 +020015#include <linux/kernel.h> /* for container_of() */
16
Thomas Zimmermannfed1eec2019-05-08 10:26:14 +020017struct drm_mode_create_dumb;
Thomas Zimmermann6542ad82019-10-24 10:14:01 +020018struct drm_plane;
19struct drm_plane_state;
20struct drm_simple_display_pipe;
Thomas Zimmermann85438a82019-05-08 10:26:11 +020021struct filp;
Thomas Zimmermann59f59892019-05-08 10:26:18 +020022struct vm_area_struct;
Thomas Zimmermann85438a82019-05-08 10:26:11 +020023
Christian König7053e0e2020-09-07 16:14:52 +020024#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 Zimmermann85438a82019-05-08 10:26:11 +020027
28/*
29 * Buffer-object helpers
30 */
31
32/**
33 * struct drm_gem_vram_object - GEM object backed by VRAM
Thomas Zimmermann85438a82019-05-08 10:26:11 +020034 * @bo: TTM buffer object
Thomas Zimmermann49a3f512020-11-03 10:30:11 +010035 * @map: Mapping information for @bo
Thomas Zimmermann85438a82019-05-08 10:26:11 +020036 * @placement: TTM placement information. Supported placements are \
37 %TTM_PL_VRAM and %TTM_PL_SYSTEM
38 * @placements: TTM placement information.
Thomas Zimmermann85438a82019-05-08 10:26:11 +020039 *
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 Zimmermann37a48ad2019-09-06 14:20:53 +020044 *
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 Zimmermannc8908bd2019-09-11 14:03:50 +020049 * 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 Zimmermann85438a82019-05-08 10:26:11 +020051 */
52struct drm_gem_vram_object {
Thomas Zimmermann85438a82019-05-08 10:26:11 +020053 struct ttm_buffer_object bo;
Thomas Zimmermann49a3f512020-11-03 10:30:11 +010054 struct dma_buf_map map;
Thomas Zimmermann85438a82019-05-08 10:26:11 +020055
Thomas Zimmermann37a48ad2019-09-06 14:20:53 +020056 /**
Thomas Zimmermann49a3f512020-11-03 10:30:11 +010057 * @vmap_use_count:
Thomas Zimmermann37a48ad2019-09-06 14:20:53 +020058 *
59 * Reference count on the virtual address.
60 * The address are un-mapped when the count reaches zero.
61 */
Thomas Zimmermann49a3f512020-11-03 10:30:11 +010062 unsigned int vmap_use_count;
Thomas Zimmermann37a48ad2019-09-06 14:20:53 +020063
Thomas Zimmermann85438a82019-05-08 10:26:11 +020064 /* Supported placements are %TTM_PL_VRAM and %TTM_PL_SYSTEM */
65 struct ttm_placement placement;
66 struct ttm_place placements[2];
Thomas Zimmermann85438a82019-05-08 10:26:11 +020067};
68
69/**
Mauro Carvalho Chehabe9d28712020-11-16 11:18:01 +010070 * drm_gem_vram_of_bo - Returns the container of type
71 * &struct drm_gem_vram_object for field bo.
Thomas Zimmermann85438a82019-05-08 10:26:11 +020072 * @bo: the VRAM buffer object
73 * Returns: The containing GEM VRAM object
74 */
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/**
Mauro Carvalho Chehabe9d28712020-11-16 11:18:01 +010082 * drm_gem_vram_of_gem - Returns the container of type
83 * &struct drm_gem_vram_object for field gem.
Thomas Zimmermann85438a82019-05-08 10:26:11 +020084 * @gem: the GEM object
85 * Returns: The containing GEM VRAM object
86 */
87static inline struct drm_gem_vram_object *drm_gem_vram_of_gem(
88 struct drm_gem_object *gem)
89{
Gerd Hoffmann0e580c62019-08-05 16:01:04 +020090 return container_of(gem, struct drm_gem_vram_object, bo.base);
Thomas Zimmermann85438a82019-05-08 10:26:11 +020091}
92
93struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
Thomas Zimmermann85438a82019-05-08 10:26:11 +020094 size_t size,
Thomas Zimmermannebe94282020-01-06 13:57:43 +010095 unsigned long pg_align);
Thomas Zimmermann85438a82019-05-08 10:26:11 +020096void drm_gem_vram_put(struct drm_gem_vram_object *gbo);
Thomas Zimmermann85438a82019-05-08 10:26:11 +020097s64 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);
Thomas Zimmermann49a3f512020-11-03 10:30:11 +0100100int drm_gem_vram_vmap(struct drm_gem_vram_object *gbo, struct dma_buf_map *map);
101void drm_gem_vram_vunmap(struct drm_gem_vram_object *gbo, struct dma_buf_map *map);
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200102
Thomas Zimmermannfed1eec2019-05-08 10:26:14 +0200103int drm_gem_vram_fill_create_dumb(struct drm_file *file,
104 struct drm_device *dev,
Thomas Zimmermannfed1eec2019-05-08 10:26:14 +0200105 unsigned long pg_align,
Thomas Zimmermann98707322019-12-03 09:38:17 +0100106 unsigned long pitch_align,
Thomas Zimmermannfed1eec2019-05-08 10:26:14 +0200107 struct drm_mode_create_dumb *args);
108
Thomas Zimmermann6c812bc2019-05-08 10:26:12 +0200109/*
Thomas Zimmermann737000f2019-05-08 10:26:13 +0200110 * Helpers for struct drm_driver
111 */
112
Thomas Zimmermann59f59892019-05-08 10:26:18 +0200113int drm_gem_vram_driver_dumb_create(struct drm_file *file,
114 struct drm_device *dev,
115 struct drm_mode_create_dumb *args);
Thomas Zimmermann59f59892019-05-08 10:26:18 +0200116
Thomas Zimmermann6542ad82019-10-24 10:14:01 +0200117/*
118 * Helpers for struct drm_plane_helper_funcs
119 */
120int
121drm_gem_vram_plane_helper_prepare_fb(struct drm_plane *plane,
122 struct drm_plane_state *new_state);
123void
124drm_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
131int drm_gem_vram_simple_display_pipe_prepare_fb(
132 struct drm_simple_display_pipe *pipe,
133 struct drm_plane_state *new_state);
134
135void drm_gem_vram_simple_display_pipe_cleanup_fb(
136 struct drm_simple_display_pipe *pipe,
137 struct drm_plane_state *old_state);
138
Thomas Zimmermann59f59892019-05-08 10:26:18 +0200139/**
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 Hoffmann92867662019-09-04 07:47:37 +0200147 .debugfs_init = drm_vram_mm_debugfs_init, \
Thomas Zimmermann59f59892019-05-08 10:26:18 +0200148 .dumb_create = drm_gem_vram_driver_dumb_create, \
Thomas Zimmermannede0c692021-04-08 16:01:37 +0200149 .dumb_map_offset = drm_gem_ttm_dumb_map_offset, \
Thomas Zimmermann31070a82019-07-02 13:50:08 +0200150 .gem_prime_mmap = drm_gem_prime_mmap
Thomas Zimmermann59f59892019-05-08 10:26:18 +0200151
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200152/*
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 */
168struct drm_vram_mm {
169 uint64_t vram_base;
170 size_t vram_size;
171
Christian König8af8a102020-10-01 14:51:40 +0200172 struct ttm_device bdev;
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200173};
174
175/**
176 * drm_vram_mm_of_bdev() - \
Christian König8af8a102020-10-01 14:51:40 +0200177 Returns the container of type &struct ttm_device for field bdev.
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200178 * @bdev: the TTM BO device
179 *
180 * Returns:
181 * The containing instance of &struct drm_vram_mm
182 */
183static inline struct drm_vram_mm *drm_vram_mm_of_bdev(
Christian König8af8a102020-10-01 14:51:40 +0200184 struct ttm_device *bdev)
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200185{
186 return container_of(bdev, struct drm_vram_mm, bdev);
187}
188
Wambui Karuga7ce844712020-03-10 16:31:21 +0300189void drm_vram_mm_debugfs_init(struct drm_minor *minor);
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200190
191/*
192 * Helpers for integration with struct drm_device
193 */
194
195struct drm_vram_mm *drm_vram_helper_alloc_mm(
Thomas Zimmermannb0e40e02019-09-11 13:09:08 +0200196 struct drm_device *dev, uint64_t vram_base, size_t vram_size);
Thomas Zimmermann6b5ce4a2019-09-11 13:09:07 +0200197void drm_vram_helper_release_mm(struct drm_device *dev);
198
Thomas Zimmermanna5f23a72020-07-16 14:53:48 +0200199int drmm_vram_helper_init(struct drm_device *dev, uint64_t vram_base,
200 size_t vram_size);
201
Thomas Zimmermann80f7c3f2020-02-03 16:52:55 +0100202/*
203 * Mode-config helpers
204 */
205
206enum drm_mode_status
207drm_vram_helper_mode_valid(struct drm_device *dev,
208 const struct drm_display_mode *mode);
209
Thomas Zimmermann85438a82019-05-08 10:26:11 +0200210#endif