xref: /linux/include/drm/drm_client.h (revision b36ca97592118196389e9cd7fa18226e955e33f7)
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 	 * @unregister:
33 	 *
34 	 * Called when &drm_device is unregistered. The client should respond by
35 	 * releasing its resources using drm_client_release().
36 	 *
37 	 * This callback is optional.
38 	 */
39 	void (*unregister)(struct drm_client_dev *client);
40 
41 	/**
42 	 * @restore:
43 	 *
44 	 * Called on drm_lastclose(). The first client instance in the list that
45 	 * returns zero gets the privilege to restore and no more clients are
46 	 * called. This callback is not called after @unregister has been called.
47 	 *
48 	 * Note that the core does not guarantee exclusion against concurrent
49 	 * drm_open(). Clients need to ensure this themselves, for example by
50 	 * using drm_master_internal_acquire() and
51 	 * drm_master_internal_release().
52 	 *
53 	 * This callback is optional.
54 	 */
55 	int (*restore)(struct drm_client_dev *client);
56 
57 	/**
58 	 * @hotplug:
59 	 *
60 	 * Called on drm_kms_helper_hotplug_event().
61 	 * This callback is not called after @unregister has been called.
62 	 *
63 	 * This callback is optional.
64 	 */
65 	int (*hotplug)(struct drm_client_dev *client);
66 
67 	/**
68 	 * @suspend:
69 	 *
70 	 * Called when suspending the device.
71 	 *
72 	 * This callback is optional.
73 	 */
74 	int (*suspend)(struct drm_client_dev *client);
75 
76 	/**
77 	 * @resume:
78 	 *
79 	 * Called when resuming the device from suspend.
80 	 *
81 	 * This callback is optional.
82 	 */
83 	int (*resume)(struct drm_client_dev *client);
84 };
85 
86 /**
87  * struct drm_client_dev - DRM client instance
88  */
89 struct drm_client_dev {
90 	/**
91 	 * @dev: DRM device
92 	 */
93 	struct drm_device *dev;
94 
95 	/**
96 	 * @name: Name of the client.
97 	 */
98 	const char *name;
99 
100 	/**
101 	 * @list:
102 	 *
103 	 * List of all clients of a DRM device, linked into
104 	 * &drm_device.clientlist. Protected by &drm_device.clientlist_mutex.
105 	 */
106 	struct list_head list;
107 
108 	/**
109 	 * @funcs: DRM client functions (optional)
110 	 */
111 	const struct drm_client_funcs *funcs;
112 
113 	/**
114 	 * @file: DRM file
115 	 */
116 	struct drm_file *file;
117 
118 	/**
119 	 * @modeset_mutex: Protects @modesets.
120 	 */
121 	struct mutex modeset_mutex;
122 
123 	/**
124 	 * @modesets: CRTC configurations
125 	 */
126 	struct drm_mode_set *modesets;
127 
128 	/**
129 	 * @suspended:
130 	 *
131 	 * The client has been suspended.
132 	 */
133 	bool suspended;
134 
135 	/**
136 	 * @hotplug_pending:
137 	 *
138 	 * A hotplug event has been received while the client was suspended.
139 	 * Try again on resume.
140 	 */
141 	bool hotplug_pending;
142 
143 	/**
144 	 * @hotplug_failed:
145 	 *
146 	 * Set by client hotplug helpers if the hotplugging failed
147 	 * before. It is usually not tried again.
148 	 */
149 	bool hotplug_failed;
150 };
151 
152 int drm_client_init(struct drm_device *dev, struct drm_client_dev *client,
153 		    const char *name, const struct drm_client_funcs *funcs);
154 void drm_client_release(struct drm_client_dev *client);
155 void drm_client_register(struct drm_client_dev *client);
156 
157 /**
158  * struct drm_client_buffer - DRM client buffer
159  */
160 struct drm_client_buffer {
161 	/**
162 	 * @client: DRM client
163 	 */
164 	struct drm_client_dev *client;
165 
166 	/**
167 	 * @pitch: Buffer pitch
168 	 */
169 	u32 pitch;
170 
171 	/**
172 	 * @gem: GEM object backing this buffer
173 	 *
174 	 * FIXME: The dependency on GEM here isn't required, we could
175 	 * convert the driver handle to a dma-buf instead and use the
176 	 * backend-agnostic dma-buf vmap support instead. This would
177 	 * require that the handle2fd prime ioctl is reworked to pull the
178 	 * fd_install step out of the driver backend hooks, to make that
179 	 * final step optional for internal users.
180 	 */
181 	struct drm_gem_object *gem;
182 
183 	/**
184 	 * @map: Virtual address for the buffer
185 	 */
186 	struct iosys_map map;
187 
188 	/**
189 	 * @fb: DRM framebuffer
190 	 */
191 	struct drm_framebuffer *fb;
192 };
193 
194 struct drm_client_buffer *
195 drm_client_framebuffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format);
196 void drm_client_framebuffer_delete(struct drm_client_buffer *buffer);
197 int drm_client_framebuffer_flush(struct drm_client_buffer *buffer, struct drm_rect *rect);
198 int drm_client_buffer_vmap_local(struct drm_client_buffer *buffer,
199 				 struct iosys_map *map_copy);
200 void drm_client_buffer_vunmap_local(struct drm_client_buffer *buffer);
201 int drm_client_buffer_vmap(struct drm_client_buffer *buffer,
202 			   struct iosys_map *map);
203 void drm_client_buffer_vunmap(struct drm_client_buffer *buffer);
204 
205 int drm_client_modeset_create(struct drm_client_dev *client);
206 void drm_client_modeset_free(struct drm_client_dev *client);
207 int drm_client_modeset_probe(struct drm_client_dev *client, unsigned int width, unsigned int height);
208 bool drm_client_rotation(struct drm_mode_set *modeset, unsigned int *rotation);
209 int drm_client_modeset_check(struct drm_client_dev *client);
210 int drm_client_modeset_commit_locked(struct drm_client_dev *client);
211 int drm_client_modeset_commit(struct drm_client_dev *client);
212 int drm_client_modeset_dpms(struct drm_client_dev *client, int mode);
213 int drm_client_modeset_wait_for_vblank(struct drm_client_dev *client, unsigned int crtc_index);
214 
215 /**
216  * drm_client_for_each_modeset() - Iterate over client modesets
217  * @modeset: &drm_mode_set loop cursor
218  * @client: DRM client
219  */
220 #define drm_client_for_each_modeset(modeset, client) \
221 	for (({ lockdep_assert_held(&(client)->modeset_mutex); }), \
222 	     modeset = (client)->modesets; modeset->crtc; modeset++)
223 
224 /**
225  * drm_client_for_each_connector_iter - connector_list iterator macro
226  * @connector: &struct drm_connector pointer used as cursor
227  * @iter: &struct drm_connector_list_iter
228  *
229  * This iterates the connectors that are useable for internal clients (excludes
230  * writeback connectors).
231  *
232  * For more info see drm_for_each_connector_iter().
233  */
234 #define drm_client_for_each_connector_iter(connector, iter) \
235 	drm_for_each_connector_iter(connector, iter) \
236 		if (connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
237 
238 #endif
239