blob: d9862259a2a7919859fea4de629b1b34c7f1abfe [file] [log] [blame]
Daniel Vetter28575f12016-11-14 12:58:23 +01001/*
2 * Copyright (c) 2016 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Laurent Pinchart93382032016-11-28 20:51:09 +020023#include <drm/drm_encoder.h>
Daniel Vetter28575f12016-11-14 12:58:23 +010024#include <drm/drm_mode_config.h>
25#include <drm/drmP.h>
26
27#include "drm_crtc_internal.h"
28#include "drm_internal.h"
29
30int drm_modeset_register_all(struct drm_device *dev)
31{
32 int ret;
33
34 ret = drm_plane_register_all(dev);
35 if (ret)
36 goto err_plane;
37
38 ret = drm_crtc_register_all(dev);
39 if (ret)
40 goto err_crtc;
41
42 ret = drm_encoder_register_all(dev);
43 if (ret)
44 goto err_encoder;
45
46 ret = drm_connector_register_all(dev);
47 if (ret)
48 goto err_connector;
49
50 return 0;
51
52err_connector:
53 drm_encoder_unregister_all(dev);
54err_encoder:
55 drm_crtc_unregister_all(dev);
56err_crtc:
57 drm_plane_unregister_all(dev);
58err_plane:
59 return ret;
60}
61
62void drm_modeset_unregister_all(struct drm_device *dev)
63{
64 drm_connector_unregister_all(dev);
65 drm_encoder_unregister_all(dev);
66 drm_crtc_unregister_all(dev);
67 drm_plane_unregister_all(dev);
68}
69
70/**
71 * drm_mode_getresources - get graphics configuration
72 * @dev: drm device for the ioctl
73 * @data: data pointer for the ioctl
74 * @file_priv: drm file for the ioctl call
75 *
76 * Construct a set of configuration description structures and return
77 * them to the user, including CRTC, connector and framebuffer configuration.
78 *
79 * Called by the user via ioctl.
80 *
81 * Returns:
82 * Zero on success, negative errno on failure.
83 */
84int drm_mode_getresources(struct drm_device *dev, void *data,
85 struct drm_file *file_priv)
86{
87 struct drm_mode_card_res *card_res = data;
Daniel Vetter28575f12016-11-14 12:58:23 +010088 struct drm_framebuffer *fb;
89 struct drm_connector *connector;
90 struct drm_crtc *crtc;
91 struct drm_encoder *encoder;
Daniel Vetter697cc9c2016-12-11 20:20:19 +010092 int count, ret = 0;
Daniel Vetter28575f12016-11-14 12:58:23 +010093 uint32_t __user *fb_id;
94 uint32_t __user *crtc_id;
95 uint32_t __user *connector_id;
96 uint32_t __user *encoder_id;
Daniel Vetter613051d2016-12-14 00:08:06 +010097 struct drm_connector_list_iter conn_iter;
Daniel Vetter28575f12016-11-14 12:58:23 +010098
99 if (!drm_core_check_feature(dev, DRIVER_MODESET))
100 return -EINVAL;
101
102
103 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter697cc9c2016-12-11 20:20:19 +0100104 count = 0;
105 fb_id = u64_to_user_ptr(card_res->fb_id_ptr);
106 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
107 if (count < card_res->count_fbs &&
108 put_user(fb->base.id, fb_id + count)) {
109 mutex_unlock(&file_priv->fbs_lock);
110 return -EFAULT;
Daniel Vetter28575f12016-11-14 12:58:23 +0100111 }
Daniel Vetter697cc9c2016-12-11 20:20:19 +0100112 count++;
Daniel Vetter28575f12016-11-14 12:58:23 +0100113 }
Daniel Vetter697cc9c2016-12-11 20:20:19 +0100114 card_res->count_fbs = count;
Daniel Vetter28575f12016-11-14 12:58:23 +0100115 mutex_unlock(&file_priv->fbs_lock);
116
Daniel Vetter28575f12016-11-14 12:58:23 +0100117 card_res->max_height = dev->mode_config.max_height;
118 card_res->min_height = dev->mode_config.min_height;
119 card_res->max_width = dev->mode_config.max_width;
120 card_res->min_width = dev->mode_config.min_width;
121
Daniel Vetter697cc9c2016-12-11 20:20:19 +0100122 count = 0;
123 crtc_id = u64_to_user_ptr(card_res->crtc_id_ptr);
124 drm_for_each_crtc(crtc, dev) {
125 if (count < card_res->count_crtcs &&
Daniel Vetter613051d2016-12-14 00:08:06 +0100126 put_user(crtc->base.id, crtc_id + count))
127 return -EFAULT;
Daniel Vetter697cc9c2016-12-11 20:20:19 +0100128 count++;
Daniel Vetter28575f12016-11-14 12:58:23 +0100129 }
Daniel Vetter697cc9c2016-12-11 20:20:19 +0100130 card_res->count_crtcs = count;
Daniel Vetter28575f12016-11-14 12:58:23 +0100131
Daniel Vetter697cc9c2016-12-11 20:20:19 +0100132 count = 0;
133 encoder_id = u64_to_user_ptr(card_res->encoder_id_ptr);
134 drm_for_each_encoder(encoder, dev) {
135 if (count < card_res->count_encoders &&
Daniel Vetter613051d2016-12-14 00:08:06 +0100136 put_user(encoder->base.id, encoder_id + count))
137 return -EFAULT;
Daniel Vetter697cc9c2016-12-11 20:20:19 +0100138 count++;
Daniel Vetter28575f12016-11-14 12:58:23 +0100139 }
Daniel Vetter697cc9c2016-12-11 20:20:19 +0100140 card_res->count_encoders = count;
Daniel Vetter28575f12016-11-14 12:58:23 +0100141
Thierry Redingb982dab2017-02-28 15:46:43 +0100142 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetter697cc9c2016-12-11 20:20:19 +0100143 count = 0;
144 connector_id = u64_to_user_ptr(card_res->connector_id_ptr);
Daniel Vetter613051d2016-12-14 00:08:06 +0100145 drm_for_each_connector_iter(connector, &conn_iter) {
Daniel Vetter697cc9c2016-12-11 20:20:19 +0100146 if (count < card_res->count_connectors &&
147 put_user(connector->base.id, connector_id + count)) {
Thierry Redingb982dab2017-02-28 15:46:43 +0100148 drm_connector_list_iter_end(&conn_iter);
Daniel Vetter613051d2016-12-14 00:08:06 +0100149 return -EFAULT;
Daniel Vetter28575f12016-11-14 12:58:23 +0100150 }
Daniel Vetter697cc9c2016-12-11 20:20:19 +0100151 count++;
Daniel Vetter28575f12016-11-14 12:58:23 +0100152 }
Daniel Vetter697cc9c2016-12-11 20:20:19 +0100153 card_res->count_connectors = count;
Thierry Redingb982dab2017-02-28 15:46:43 +0100154 drm_connector_list_iter_end(&conn_iter);
Daniel Vetter28575f12016-11-14 12:58:23 +0100155
Daniel Vetter28575f12016-11-14 12:58:23 +0100156 return ret;
157}
158
159/**
160 * drm_mode_config_reset - call ->reset callbacks
161 * @dev: drm device
162 *
163 * This functions calls all the crtc's, encoder's and connector's ->reset
164 * callback. Drivers can use this in e.g. their driver load or resume code to
165 * reset hardware and software state.
166 */
167void drm_mode_config_reset(struct drm_device *dev)
168{
169 struct drm_crtc *crtc;
170 struct drm_plane *plane;
171 struct drm_encoder *encoder;
172 struct drm_connector *connector;
Daniel Vetter613051d2016-12-14 00:08:06 +0100173 struct drm_connector_list_iter conn_iter;
Daniel Vetter28575f12016-11-14 12:58:23 +0100174
175 drm_for_each_plane(plane, dev)
176 if (plane->funcs->reset)
177 plane->funcs->reset(plane);
178
179 drm_for_each_crtc(crtc, dev)
180 if (crtc->funcs->reset)
181 crtc->funcs->reset(crtc);
182
183 drm_for_each_encoder(encoder, dev)
184 if (encoder->funcs->reset)
185 encoder->funcs->reset(encoder);
186
Thierry Redingb982dab2017-02-28 15:46:43 +0100187 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetter613051d2016-12-14 00:08:06 +0100188 drm_for_each_connector_iter(connector, &conn_iter)
Daniel Vetter28575f12016-11-14 12:58:23 +0100189 if (connector->funcs->reset)
190 connector->funcs->reset(connector);
Thierry Redingb982dab2017-02-28 15:46:43 +0100191 drm_connector_list_iter_end(&conn_iter);
Daniel Vetter28575f12016-11-14 12:58:23 +0100192}
193EXPORT_SYMBOL(drm_mode_config_reset);
194
195/*
196 * Global properties
197 */
198static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
199 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
200 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
201 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
202};
203
204static int drm_mode_create_standard_properties(struct drm_device *dev)
205{
206 struct drm_property *prop;
207 int ret;
208
209 ret = drm_connector_create_standard_properties(dev);
210 if (ret)
211 return ret;
212
213 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
214 "type", drm_plane_type_enum_list,
215 ARRAY_SIZE(drm_plane_type_enum_list));
216 if (!prop)
217 return -ENOMEM;
218 dev->mode_config.plane_type_property = prop;
219
220 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
221 "SRC_X", 0, UINT_MAX);
222 if (!prop)
223 return -ENOMEM;
224 dev->mode_config.prop_src_x = prop;
225
226 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
227 "SRC_Y", 0, UINT_MAX);
228 if (!prop)
229 return -ENOMEM;
230 dev->mode_config.prop_src_y = prop;
231
232 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
233 "SRC_W", 0, UINT_MAX);
234 if (!prop)
235 return -ENOMEM;
236 dev->mode_config.prop_src_w = prop;
237
238 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
239 "SRC_H", 0, UINT_MAX);
240 if (!prop)
241 return -ENOMEM;
242 dev->mode_config.prop_src_h = prop;
243
244 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
245 "CRTC_X", INT_MIN, INT_MAX);
246 if (!prop)
247 return -ENOMEM;
248 dev->mode_config.prop_crtc_x = prop;
249
250 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
251 "CRTC_Y", INT_MIN, INT_MAX);
252 if (!prop)
253 return -ENOMEM;
254 dev->mode_config.prop_crtc_y = prop;
255
256 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
257 "CRTC_W", 0, INT_MAX);
258 if (!prop)
259 return -ENOMEM;
260 dev->mode_config.prop_crtc_w = prop;
261
262 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
263 "CRTC_H", 0, INT_MAX);
264 if (!prop)
265 return -ENOMEM;
266 dev->mode_config.prop_crtc_h = prop;
267
268 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
269 "FB_ID", DRM_MODE_OBJECT_FB);
270 if (!prop)
271 return -ENOMEM;
272 dev->mode_config.prop_fb_id = prop;
273
Gustavo Padovan96260142016-11-15 22:06:39 +0900274 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
275 "IN_FENCE_FD", -1, INT_MAX);
276 if (!prop)
277 return -ENOMEM;
278 dev->mode_config.prop_in_fence_fd = prop;
279
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900280 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
281 "OUT_FENCE_PTR", 0, U64_MAX);
282 if (!prop)
283 return -ENOMEM;
284 dev->mode_config.prop_out_fence_ptr = prop;
285
Daniel Vetter28575f12016-11-14 12:58:23 +0100286 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
287 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
288 if (!prop)
289 return -ENOMEM;
290 dev->mode_config.prop_crtc_id = prop;
291
292 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
293 "ACTIVE");
294 if (!prop)
295 return -ENOMEM;
296 dev->mode_config.prop_active = prop;
297
298 prop = drm_property_create(dev,
299 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
300 "MODE_ID", 0);
301 if (!prop)
302 return -ENOMEM;
303 dev->mode_config.prop_mode_id = prop;
304
305 prop = drm_property_create(dev,
306 DRM_MODE_PROP_BLOB,
307 "DEGAMMA_LUT", 0);
308 if (!prop)
309 return -ENOMEM;
310 dev->mode_config.degamma_lut_property = prop;
311
312 prop = drm_property_create_range(dev,
313 DRM_MODE_PROP_IMMUTABLE,
314 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
315 if (!prop)
316 return -ENOMEM;
317 dev->mode_config.degamma_lut_size_property = prop;
318
319 prop = drm_property_create(dev,
320 DRM_MODE_PROP_BLOB,
321 "CTM", 0);
322 if (!prop)
323 return -ENOMEM;
324 dev->mode_config.ctm_property = prop;
325
326 prop = drm_property_create(dev,
327 DRM_MODE_PROP_BLOB,
328 "GAMMA_LUT", 0);
329 if (!prop)
330 return -ENOMEM;
331 dev->mode_config.gamma_lut_property = prop;
332
333 prop = drm_property_create_range(dev,
334 DRM_MODE_PROP_IMMUTABLE,
335 "GAMMA_LUT_SIZE", 0, UINT_MAX);
336 if (!prop)
337 return -ENOMEM;
338 dev->mode_config.gamma_lut_size_property = prop;
339
340 return 0;
341}
342
343/**
344 * drm_mode_config_init - initialize DRM mode_configuration structure
345 * @dev: DRM device
346 *
347 * Initialize @dev's mode_config structure, used for tracking the graphics
348 * configuration of @dev.
349 *
350 * Since this initializes the modeset locks, no locking is possible. Which is no
351 * problem, since this should happen single threaded at init time. It is the
352 * driver's problem to ensure this guarantee.
353 *
354 */
355void drm_mode_config_init(struct drm_device *dev)
356{
357 mutex_init(&dev->mode_config.mutex);
358 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
359 mutex_init(&dev->mode_config.idr_mutex);
360 mutex_init(&dev->mode_config.fb_lock);
361 mutex_init(&dev->mode_config.blob_lock);
362 INIT_LIST_HEAD(&dev->mode_config.fb_list);
363 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
364 INIT_LIST_HEAD(&dev->mode_config.connector_list);
365 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
366 INIT_LIST_HEAD(&dev->mode_config.property_list);
367 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
368 INIT_LIST_HEAD(&dev->mode_config.plane_list);
369 idr_init(&dev->mode_config.crtc_idr);
370 idr_init(&dev->mode_config.tile_idr);
371 ida_init(&dev->mode_config.connector_ida);
Daniel Vetter613051d2016-12-14 00:08:06 +0100372 spin_lock_init(&dev->mode_config.connector_list_lock);
Daniel Vetter28575f12016-11-14 12:58:23 +0100373
Daniel Vetter28575f12016-11-14 12:58:23 +0100374 drm_mode_create_standard_properties(dev);
Daniel Vetter28575f12016-11-14 12:58:23 +0100375
376 /* Just to be sure */
377 dev->mode_config.num_fb = 0;
378 dev->mode_config.num_connector = 0;
379 dev->mode_config.num_crtc = 0;
380 dev->mode_config.num_encoder = 0;
381 dev->mode_config.num_overlay_plane = 0;
382 dev->mode_config.num_total_plane = 0;
383}
384EXPORT_SYMBOL(drm_mode_config_init);
385
386/**
387 * drm_mode_config_cleanup - free up DRM mode_config info
388 * @dev: DRM device
389 *
390 * Free up all the connectors and CRTCs associated with this DRM device, then
391 * free up the framebuffers and associated buffer objects.
392 *
393 * Note that since this /should/ happen single-threaded at driver/device
394 * teardown time, no locking is required. It's the driver's job to ensure that
395 * this guarantee actually holds true.
396 *
397 * FIXME: cleanup any dangling user buffer objects too
398 */
399void drm_mode_config_cleanup(struct drm_device *dev)
400{
Daniel Vetter2ab8c5f2016-12-14 00:08:08 +0100401 struct drm_connector *connector;
402 struct drm_connector_list_iter conn_iter;
Daniel Vetter28575f12016-11-14 12:58:23 +0100403 struct drm_crtc *crtc, *ct;
404 struct drm_encoder *encoder, *enct;
405 struct drm_framebuffer *fb, *fbt;
406 struct drm_property *property, *pt;
407 struct drm_property_blob *blob, *bt;
408 struct drm_plane *plane, *plt;
409
410 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
411 head) {
412 encoder->funcs->destroy(encoder);
413 }
414
Thierry Redingb982dab2017-02-28 15:46:43 +0100415 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetter2ab8c5f2016-12-14 00:08:08 +0100416 drm_for_each_connector_iter(connector, &conn_iter) {
417 /* drm_connector_list_iter holds an full reference to the
418 * current connector itself, which means it is inherently safe
419 * against unreferencing the current connector - but not against
420 * deleting it right away. */
Thierry Redingad093602017-02-28 15:46:39 +0100421 drm_connector_put(connector);
Daniel Vetter28575f12016-11-14 12:58:23 +0100422 }
Thierry Redingb982dab2017-02-28 15:46:43 +0100423 drm_connector_list_iter_end(&conn_iter);
Chris Wilsond352f352017-01-19 09:05:13 +0000424 if (WARN_ON(!list_empty(&dev->mode_config.connector_list))) {
Thierry Redingb982dab2017-02-28 15:46:43 +0100425 drm_connector_list_iter_begin(dev, &conn_iter);
Chris Wilsond352f352017-01-19 09:05:13 +0000426 drm_for_each_connector_iter(connector, &conn_iter)
427 DRM_ERROR("connector %s leaked!\n", connector->name);
Thierry Redingb982dab2017-02-28 15:46:43 +0100428 drm_connector_list_iter_end(&conn_iter);
Chris Wilsond352f352017-01-19 09:05:13 +0000429 }
Daniel Vetter28575f12016-11-14 12:58:23 +0100430
431 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
432 head) {
433 drm_property_destroy(dev, property);
434 }
435
436 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
437 head) {
438 plane->funcs->destroy(plane);
439 }
440
441 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
442 crtc->funcs->destroy(crtc);
443 }
444
445 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
446 head_global) {
Thierry Reding6472e502017-02-28 15:46:42 +0100447 drm_property_blob_put(blob);
Daniel Vetter28575f12016-11-14 12:58:23 +0100448 }
449
450 /*
451 * Single-threaded teardown context, so it's not required to grab the
452 * fb_lock to protect against concurrent fb_list access. Contrary, it
453 * would actually deadlock with the drm_framebuffer_cleanup function.
454 *
455 * Also, if there are any framebuffers left, that's a driver leak now,
456 * so politely WARN about this.
457 */
458 WARN_ON(!list_empty(&dev->mode_config.fb_list));
459 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
460 drm_framebuffer_free(&fb->base.refcount);
461 }
462
463 ida_destroy(&dev->mode_config.connector_ida);
464 idr_destroy(&dev->mode_config.tile_idr);
465 idr_destroy(&dev->mode_config.crtc_idr);
466 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
467}
468EXPORT_SYMBOL(drm_mode_config_cleanup);