xref: /linux/include/drm/drm_client.h (revision b0a3a811634aa7422f8a92196db47a06a70bda16)
1 /* SPDX-License-Identifier: GPL-2.0 or MIT */
2 
3 #ifndef _DRM_CLIENT_H_
4 #define _DRM_CLIENT_H_
5 
6 #include <linux/iosys-map.h>
7 #include <linux/lockdep.h>
8 #include <linux/mutex.h>
9 #include <linux/types.h>
10 
11 #include <drm/drm_connector.h>
12 #include <drm/drm_crtc.h>
13 
14 struct drm_client_dev;
15 struct drm_device;
16 struct drm_file;
17 struct drm_framebuffer;
18 struct drm_gem_object;
19 struct drm_minor;
20 struct module;
21 
22 /**
23  * struct drm_client_funcs - DRM client callbacks
24  */
25 struct drm_client_funcs {
26 	/**
27 	 * @owner: The module owner
28 	 */
29 	struct module *owner;
30 
31 	/**
32 	 * @free:
33 	 *
34 	 * Called when the client gets unregistered. Implementations should
35 	 * release all client-specific data and free the memory.
36 	 *
37 	 * This callback is optional.
38 	 */
39 	void (*free)(struct drm_client_dev *client);
40 
41 	/**
42 	 * @unregister:
43 	 *
44 	 * Called when &drm_device is unregistered. The client should respond by
45 	 * releasing its resources using drm_client_release().
46 	 *
47 	 * This callback is optional.
48 	 */
49 	void (*unregister)(struct drm_client_dev *client);
50 
51 	/**
52 	 * @restore:
53 	 *
54 	 * Called on drm_lastclose(). The first client instance in the list that
55 	 * returns zero gets the privilege to restore and no more clients are
56 	 * called. This callback is not called after @unregister has been called.
57 	 *
58 	 * Note that the core does not guarantee exclusion against concurrent
59 	 * drm_open(). Clients need to ensure this themselves, for example by
60 	 * using drm_master_internal_acquire() and drm_master_internal_release().
61 	 *
62 	 * If the caller passes force, the client should ignore any present DRM
63 	 * master and restore the display anyway.
64 	 *
65 	 * This callback is optional.
66 	 */
67 	int (*restore)(struct drm_client_dev *client, bool force);
68 
69 	/**
70 	 * @acquire_outputs:
71 	 *
72 	 * Called by vga-switcheroo. Informs the client that the outputs will
73 	 * be switched to its device. When @acquire_outputs runs, the outputs
74 	 * have not been switched yet. The client should only prepare the software
75 	 * state. After the switch happened, the client might get a hotplug
76 	 * event to update the hardware state.
77 	 *
78 	 * This callback exists for remapping framebuffers to virtual terminals
79 	 * in fbcon.
80 	 */
81 	void (*acquire_outputs)(struct drm_client_dev *client);
82 
83 	/**
84 	 * @hotplug:
85 	 *
86 	 * Called on drm_kms_helper_hotplug_event().
87 	 * This callback is not called after @unregister has been called.
88 	 *
89 	 * This callback is optional.
90 	 */
91 	int (*hotplug)(struct drm_client_dev *client);
92 
93 	/**
94 	 * @suspend:
95 	 *
96 	 * Called when suspending the device.
97 	 *
98 	 * This callback is optional.
99 	 */
100 	int (*suspend)(struct drm_client_dev *client);
101 
102 	/**
103 	 * @resume:
104 	 *
105 	 * Called when resuming the device from suspend.
106 	 *
107 	 * This callback is optional.
108 	 */
109 	int (*resume)(struct drm_client_dev *client);
110 };
111 
112 /**
113  * struct drm_client_dev - DRM client instance
114  */
115 struct drm_client_dev {
116 	/**
117 	 * @dev: DRM device
118 	 */
119 	struct drm_device *dev;
120 
121 	/**
122 	 * @name: Name of the client.
123 	 */
124 	const char *name;
125 
126 	/**
127 	 * @list:
128 	 *
129 	 * List of all clients of a DRM device, linked into
130 	 * &drm_device.clientlist. Protected by &drm_device.clientlist_mutex.
131 	 */
132 	struct list_head list;
133 
134 	/**
135 	 * @funcs: DRM client functions (optional)
136 	 */
137 	const struct drm_client_funcs *funcs;
138 
139 	/**
140 	 * @file: DRM file
141 	 */
142 	struct drm_file *file;
143 
144 	/**
145 	 * @modeset_mutex: Protects @modesets.
146 	 */
147 	struct mutex modeset_mutex;
148 
149 	/**
150 	 * @modesets: CRTC configurations
151 	 */
152 	struct drm_mode_set *modesets;
153 
154 	/**
155 	 * @suspended:
156 	 *
157 	 * The client has been suspended.
158 	 */
159 	bool suspended;
160 
161 	/**
162 	 * @hotplug_pending:
163 	 *
164 	 * A hotplug event has been received while the client was suspended.
165 	 * Try again on resume.
166 	 */
167 	bool hotplug_pending;
168 
169 	/**
170 	 * @hotplug_failed:
171 	 *
172 	 * Set by client hotplug helpers if the hotplugging failed
173 	 * before. It is usually not tried again.
174 	 */
175 	bool hotplug_failed;
176 };
177 
178 int drm_client_init(struct drm_device *dev, struct drm_client_dev *client,
179 		    const char *name, const struct drm_client_funcs *funcs);
180 void drm_client_release(struct drm_client_dev *client);
181 void drm_client_register(struct drm_client_dev *client);
182 
183 /**
184  * struct drm_client_buffer - DRM client buffer
185  */
186 struct drm_client_buffer {
187 	/**
188 	 * @client: DRM client
189 	 */
190 	struct drm_client_dev *client;
191 
192 	/**
193 	 * @gem: GEM object backing this buffer
194 	 *
195 	 * FIXME: The DRM framebuffer holds a reference on its GEM
196 	 * buffer objects. Do not use this field in new code and
197 	 * update existing users.
198 	 */
199 	struct drm_gem_object *gem;
200 
201 	/**
202 	 * @map: Virtual address for the buffer
203 	 */
204 	struct iosys_map map;
205 
206 	/**
207 	 * @fb: DRM framebuffer
208 	 */
209 	struct drm_framebuffer *fb;
210 };
211 
212 struct drm_client_buffer *
213 drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height,
214 			 u32 format, u32 handle, u32 pitch);
215 struct drm_client_buffer *
216 drm_client_buffer_create_dumb(struct drm_client_dev *client, u32 width, u32 height, u32 format);
217 void drm_client_buffer_delete(struct drm_client_buffer *buffer);
218 int drm_client_buffer_flush(struct drm_client_buffer *buffer, struct drm_rect *rect);
219 int drm_client_buffer_vmap_local(struct drm_client_buffer *buffer,
220 				 struct iosys_map *map_copy);
221 void drm_client_buffer_vunmap_local(struct drm_client_buffer *buffer);
222 int drm_client_buffer_vmap(struct drm_client_buffer *buffer,
223 			   struct iosys_map *map);
224 void drm_client_buffer_vunmap(struct drm_client_buffer *buffer);
225 
226 int drm_client_modeset_create(struct drm_client_dev *client);
227 void drm_client_modeset_free(struct drm_client_dev *client);
228 int drm_client_modeset_probe(struct drm_client_dev *client, unsigned int width, unsigned int height);
229 bool drm_client_rotation(struct drm_mode_set *modeset, unsigned int *rotation);
230 int drm_client_modeset_check(struct drm_client_dev *client);
231 int drm_client_modeset_commit_locked(struct drm_client_dev *client);
232 int drm_client_modeset_commit(struct drm_client_dev *client);
233 int drm_client_modeset_dpms(struct drm_client_dev *client, int mode);
234 int drm_client_modeset_wait_for_vblank(struct drm_client_dev *client, unsigned int crtc_index);
235 
236 /**
237  * drm_client_for_each_modeset() - Iterate over client modesets
238  * @modeset: &drm_mode_set loop cursor
239  * @client: DRM client
240  */
241 #define drm_client_for_each_modeset(modeset, client) \
242 	for (({ lockdep_assert_held(&(client)->modeset_mutex); }), \
243 	     modeset = (client)->modesets; modeset->crtc; modeset++)
244 
245 /**
246  * drm_client_for_each_connector_iter - connector_list iterator macro
247  * @connector: &struct drm_connector pointer used as cursor
248  * @iter: &struct drm_connector_list_iter
249  *
250  * This iterates the connectors that are useable for internal clients (excludes
251  * writeback connectors).
252  *
253  * For more info see drm_for_each_connector_iter().
254  */
255 #define drm_client_for_each_connector_iter(connector, iter) \
256 	drm_for_each_connector_iter(connector, iter) \
257 		if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
258 
259 #endif
260