Loading...
1/*
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
23#include <linux/uaccess.h>
24
25#include <drm/drm_drv.h>
26#include <drm/drm_encoder.h>
27#include <drm/drm_file.h>
28#include <drm/drm_framebuffer.h>
29#include <drm/drm_managed.h>
30#include <drm/drm_mode_config.h>
31#include <drm/drm_print.h>
32#include <linux/dma-resv.h>
33
34#include "drm_crtc_internal.h"
35#include "drm_internal.h"
36
37int drm_modeset_register_all(struct drm_device *dev)
38{
39 int ret;
40
41 ret = drm_plane_register_all(dev);
42 if (ret)
43 goto err_plane;
44
45 ret = drm_crtc_register_all(dev);
46 if (ret)
47 goto err_crtc;
48
49 ret = drm_encoder_register_all(dev);
50 if (ret)
51 goto err_encoder;
52
53 ret = drm_connector_register_all(dev);
54 if (ret)
55 goto err_connector;
56
57 return 0;
58
59err_connector:
60 drm_encoder_unregister_all(dev);
61err_encoder:
62 drm_crtc_unregister_all(dev);
63err_crtc:
64 drm_plane_unregister_all(dev);
65err_plane:
66 return ret;
67}
68
69void drm_modeset_unregister_all(struct drm_device *dev)
70{
71 drm_connector_unregister_all(dev);
72 drm_encoder_unregister_all(dev);
73 drm_crtc_unregister_all(dev);
74 drm_plane_unregister_all(dev);
75}
76
77/**
78 * drm_mode_getresources - get graphics configuration
79 * @dev: drm device for the ioctl
80 * @data: data pointer for the ioctl
81 * @file_priv: drm file for the ioctl call
82 *
83 * Construct a set of configuration description structures and return
84 * them to the user, including CRTC, connector and framebuffer configuration.
85 *
86 * Called by the user via ioctl.
87 *
88 * Returns:
89 * Zero on success, negative errno on failure.
90 */
91int drm_mode_getresources(struct drm_device *dev, void *data,
92 struct drm_file *file_priv)
93{
94 struct drm_mode_card_res *card_res = data;
95 struct drm_framebuffer *fb;
96 struct drm_connector *connector;
97 struct drm_crtc *crtc;
98 struct drm_encoder *encoder;
99 int count, ret = 0;
100 uint32_t __user *fb_id;
101 uint32_t __user *crtc_id;
102 uint32_t __user *connector_id;
103 uint32_t __user *encoder_id;
104 struct drm_connector_list_iter conn_iter;
105
106 if (!drm_core_check_feature(dev, DRIVER_MODESET))
107 return -EOPNOTSUPP;
108
109 mutex_lock(&file_priv->fbs_lock);
110 count = 0;
111 fb_id = u64_to_user_ptr(card_res->fb_id_ptr);
112 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
113 if (count < card_res->count_fbs &&
114 put_user(fb->base.id, fb_id + count)) {
115 mutex_unlock(&file_priv->fbs_lock);
116 return -EFAULT;
117 }
118 count++;
119 }
120 card_res->count_fbs = count;
121 mutex_unlock(&file_priv->fbs_lock);
122
123 card_res->max_height = dev->mode_config.max_height;
124 card_res->min_height = dev->mode_config.min_height;
125 card_res->max_width = dev->mode_config.max_width;
126 card_res->min_width = dev->mode_config.min_width;
127
128 count = 0;
129 crtc_id = u64_to_user_ptr(card_res->crtc_id_ptr);
130 drm_for_each_crtc(crtc, dev) {
131 if (drm_lease_held(file_priv, crtc->base.id)) {
132 if (count < card_res->count_crtcs &&
133 put_user(crtc->base.id, crtc_id + count))
134 return -EFAULT;
135 count++;
136 }
137 }
138 card_res->count_crtcs = count;
139
140 count = 0;
141 encoder_id = u64_to_user_ptr(card_res->encoder_id_ptr);
142 drm_for_each_encoder(encoder, dev) {
143 if (count < card_res->count_encoders &&
144 put_user(encoder->base.id, encoder_id + count))
145 return -EFAULT;
146 count++;
147 }
148 card_res->count_encoders = count;
149
150 drm_connector_list_iter_begin(dev, &conn_iter);
151 count = 0;
152 connector_id = u64_to_user_ptr(card_res->connector_id_ptr);
153 drm_for_each_connector_iter(connector, &conn_iter) {
154 /* only expose writeback connectors if userspace understands them */
155 if (!file_priv->writeback_connectors &&
156 (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK))
157 continue;
158
159 if (drm_lease_held(file_priv, connector->base.id)) {
160 if (count < card_res->count_connectors &&
161 put_user(connector->base.id, connector_id + count)) {
162 drm_connector_list_iter_end(&conn_iter);
163 return -EFAULT;
164 }
165 count++;
166 }
167 }
168 card_res->count_connectors = count;
169 drm_connector_list_iter_end(&conn_iter);
170
171 return ret;
172}
173
174/**
175 * drm_mode_config_reset - call ->reset callbacks
176 * @dev: drm device
177 *
178 * This functions calls all the crtc's, encoder's and connector's ->reset
179 * callback. Drivers can use this in e.g. their driver load or resume code to
180 * reset hardware and software state.
181 */
182void drm_mode_config_reset(struct drm_device *dev)
183{
184 struct drm_crtc *crtc;
185 struct drm_plane *plane;
186 struct drm_encoder *encoder;
187 struct drm_connector *connector;
188 struct drm_connector_list_iter conn_iter;
189
190 drm_for_each_plane(plane, dev)
191 if (plane->funcs->reset)
192 plane->funcs->reset(plane);
193
194 drm_for_each_crtc(crtc, dev)
195 if (crtc->funcs->reset)
196 crtc->funcs->reset(crtc);
197
198 drm_for_each_encoder(encoder, dev)
199 if (encoder->funcs && encoder->funcs->reset)
200 encoder->funcs->reset(encoder);
201
202 drm_connector_list_iter_begin(dev, &conn_iter);
203 drm_for_each_connector_iter(connector, &conn_iter)
204 if (connector->funcs->reset)
205 connector->funcs->reset(connector);
206 drm_connector_list_iter_end(&conn_iter);
207}
208EXPORT_SYMBOL(drm_mode_config_reset);
209
210/*
211 * Global properties
212 */
213static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
214 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
215 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
216 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
217};
218
219static int drm_mode_create_standard_properties(struct drm_device *dev)
220{
221 struct drm_property *prop;
222 int ret;
223
224 ret = drm_connector_create_standard_properties(dev);
225 if (ret)
226 return ret;
227
228 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
229 "type", drm_plane_type_enum_list,
230 ARRAY_SIZE(drm_plane_type_enum_list));
231 if (!prop)
232 return -ENOMEM;
233 dev->mode_config.plane_type_property = prop;
234
235 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
236 "SRC_X", 0, UINT_MAX);
237 if (!prop)
238 return -ENOMEM;
239 dev->mode_config.prop_src_x = prop;
240
241 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
242 "SRC_Y", 0, UINT_MAX);
243 if (!prop)
244 return -ENOMEM;
245 dev->mode_config.prop_src_y = prop;
246
247 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
248 "SRC_W", 0, UINT_MAX);
249 if (!prop)
250 return -ENOMEM;
251 dev->mode_config.prop_src_w = prop;
252
253 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
254 "SRC_H", 0, UINT_MAX);
255 if (!prop)
256 return -ENOMEM;
257 dev->mode_config.prop_src_h = prop;
258
259 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
260 "CRTC_X", INT_MIN, INT_MAX);
261 if (!prop)
262 return -ENOMEM;
263 dev->mode_config.prop_crtc_x = prop;
264
265 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
266 "CRTC_Y", INT_MIN, INT_MAX);
267 if (!prop)
268 return -ENOMEM;
269 dev->mode_config.prop_crtc_y = prop;
270
271 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
272 "CRTC_W", 0, INT_MAX);
273 if (!prop)
274 return -ENOMEM;
275 dev->mode_config.prop_crtc_w = prop;
276
277 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
278 "CRTC_H", 0, INT_MAX);
279 if (!prop)
280 return -ENOMEM;
281 dev->mode_config.prop_crtc_h = prop;
282
283 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
284 "FB_ID", DRM_MODE_OBJECT_FB);
285 if (!prop)
286 return -ENOMEM;
287 dev->mode_config.prop_fb_id = prop;
288
289 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
290 "IN_FENCE_FD", -1, INT_MAX);
291 if (!prop)
292 return -ENOMEM;
293 dev->mode_config.prop_in_fence_fd = prop;
294
295 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
296 "OUT_FENCE_PTR", 0, U64_MAX);
297 if (!prop)
298 return -ENOMEM;
299 dev->mode_config.prop_out_fence_ptr = prop;
300
301 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
302 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
303 if (!prop)
304 return -ENOMEM;
305 dev->mode_config.prop_crtc_id = prop;
306
307 prop = drm_property_create(dev,
308 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
309 "FB_DAMAGE_CLIPS", 0);
310 if (!prop)
311 return -ENOMEM;
312 dev->mode_config.prop_fb_damage_clips = prop;
313
314 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
315 "ACTIVE");
316 if (!prop)
317 return -ENOMEM;
318 dev->mode_config.prop_active = prop;
319
320 prop = drm_property_create(dev,
321 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
322 "MODE_ID", 0);
323 if (!prop)
324 return -ENOMEM;
325 dev->mode_config.prop_mode_id = prop;
326
327 prop = drm_property_create_bool(dev, 0,
328 "VRR_ENABLED");
329 if (!prop)
330 return -ENOMEM;
331 dev->mode_config.prop_vrr_enabled = prop;
332
333 prop = drm_property_create(dev,
334 DRM_MODE_PROP_BLOB,
335 "DEGAMMA_LUT", 0);
336 if (!prop)
337 return -ENOMEM;
338 dev->mode_config.degamma_lut_property = prop;
339
340 prop = drm_property_create_range(dev,
341 DRM_MODE_PROP_IMMUTABLE,
342 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
343 if (!prop)
344 return -ENOMEM;
345 dev->mode_config.degamma_lut_size_property = prop;
346
347 prop = drm_property_create(dev,
348 DRM_MODE_PROP_BLOB,
349 "CTM", 0);
350 if (!prop)
351 return -ENOMEM;
352 dev->mode_config.ctm_property = prop;
353
354 prop = drm_property_create(dev,
355 DRM_MODE_PROP_BLOB,
356 "GAMMA_LUT", 0);
357 if (!prop)
358 return -ENOMEM;
359 dev->mode_config.gamma_lut_property = prop;
360
361 prop = drm_property_create_range(dev,
362 DRM_MODE_PROP_IMMUTABLE,
363 "GAMMA_LUT_SIZE", 0, UINT_MAX);
364 if (!prop)
365 return -ENOMEM;
366 dev->mode_config.gamma_lut_size_property = prop;
367
368 prop = drm_property_create(dev,
369 DRM_MODE_PROP_IMMUTABLE | DRM_MODE_PROP_BLOB,
370 "IN_FORMATS", 0);
371 if (!prop)
372 return -ENOMEM;
373 dev->mode_config.modifiers_property = prop;
374
375 return 0;
376}
377
378static void drm_mode_config_init_release(struct drm_device *dev, void *ptr)
379{
380 drm_mode_config_cleanup(dev);
381}
382
383/**
384 * drmm_mode_config_init - managed DRM mode_configuration structure
385 * initialization
386 * @dev: DRM device
387 *
388 * Initialize @dev's mode_config structure, used for tracking the graphics
389 * configuration of @dev.
390 *
391 * Since this initializes the modeset locks, no locking is possible. Which is no
392 * problem, since this should happen single threaded at init time. It is the
393 * driver's problem to ensure this guarantee.
394 *
395 * Cleanup is automatically handled through registering drm_mode_config_cleanup
396 * with drmm_add_action().
397 *
398 * Returns: 0 on success, negative error value on failure.
399 */
400int drmm_mode_config_init(struct drm_device *dev)
401{
402 int ret;
403
404 mutex_init(&dev->mode_config.mutex);
405 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
406 mutex_init(&dev->mode_config.idr_mutex);
407 mutex_init(&dev->mode_config.fb_lock);
408 mutex_init(&dev->mode_config.blob_lock);
409 INIT_LIST_HEAD(&dev->mode_config.fb_list);
410 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
411 INIT_LIST_HEAD(&dev->mode_config.connector_list);
412 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
413 INIT_LIST_HEAD(&dev->mode_config.property_list);
414 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
415 INIT_LIST_HEAD(&dev->mode_config.plane_list);
416 INIT_LIST_HEAD(&dev->mode_config.privobj_list);
417 idr_init_base(&dev->mode_config.object_idr, 1);
418 idr_init_base(&dev->mode_config.tile_idr, 1);
419 ida_init(&dev->mode_config.connector_ida);
420 spin_lock_init(&dev->mode_config.connector_list_lock);
421
422 init_llist_head(&dev->mode_config.connector_free_list);
423 INIT_WORK(&dev->mode_config.connector_free_work, drm_connector_free_work_fn);
424
425 ret = drm_mode_create_standard_properties(dev);
426 if (ret) {
427 drm_mode_config_cleanup(dev);
428 return ret;
429 }
430
431 /* Just to be sure */
432 dev->mode_config.num_fb = 0;
433 dev->mode_config.num_connector = 0;
434 dev->mode_config.num_crtc = 0;
435 dev->mode_config.num_encoder = 0;
436 dev->mode_config.num_total_plane = 0;
437
438 if (IS_ENABLED(CONFIG_LOCKDEP)) {
439 struct drm_modeset_acquire_ctx modeset_ctx;
440 struct ww_acquire_ctx resv_ctx;
441 struct dma_resv resv;
442 int ret;
443
444 dma_resv_init(&resv);
445
446 drm_modeset_acquire_init(&modeset_ctx, 0);
447 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
448 &modeset_ctx);
449 if (ret == -EDEADLK)
450 ret = drm_modeset_backoff(&modeset_ctx);
451
452 ww_acquire_init(&resv_ctx, &reservation_ww_class);
453 ret = dma_resv_lock(&resv, &resv_ctx);
454 if (ret == -EDEADLK)
455 dma_resv_lock_slow(&resv, &resv_ctx);
456
457 dma_resv_unlock(&resv);
458 ww_acquire_fini(&resv_ctx);
459
460 drm_modeset_drop_locks(&modeset_ctx);
461 drm_modeset_acquire_fini(&modeset_ctx);
462 dma_resv_fini(&resv);
463 }
464
465 return drmm_add_action_or_reset(dev, drm_mode_config_init_release,
466 NULL);
467}
468EXPORT_SYMBOL(drmm_mode_config_init);
469
470/**
471 * drm_mode_config_cleanup - free up DRM mode_config info
472 * @dev: DRM device
473 *
474 * Free up all the connectors and CRTCs associated with this DRM device, then
475 * free up the framebuffers and associated buffer objects.
476 *
477 * Note that since this /should/ happen single-threaded at driver/device
478 * teardown time, no locking is required. It's the driver's job to ensure that
479 * this guarantee actually holds true.
480 *
481 * FIXME: With the managed drmm_mode_config_init() it is no longer necessary for
482 * drivers to explicitly call this function.
483 */
484void drm_mode_config_cleanup(struct drm_device *dev)
485{
486 struct drm_connector *connector;
487 struct drm_connector_list_iter conn_iter;
488 struct drm_crtc *crtc, *ct;
489 struct drm_encoder *encoder, *enct;
490 struct drm_framebuffer *fb, *fbt;
491 struct drm_property *property, *pt;
492 struct drm_property_blob *blob, *bt;
493 struct drm_plane *plane, *plt;
494
495 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
496 head) {
497 encoder->funcs->destroy(encoder);
498 }
499
500 drm_connector_list_iter_begin(dev, &conn_iter);
501 drm_for_each_connector_iter(connector, &conn_iter) {
502 /* drm_connector_list_iter holds an full reference to the
503 * current connector itself, which means it is inherently safe
504 * against unreferencing the current connector - but not against
505 * deleting it right away. */
506 drm_connector_put(connector);
507 }
508 drm_connector_list_iter_end(&conn_iter);
509 /* connector_iter drops references in a work item. */
510 flush_work(&dev->mode_config.connector_free_work);
511 if (WARN_ON(!list_empty(&dev->mode_config.connector_list))) {
512 drm_connector_list_iter_begin(dev, &conn_iter);
513 drm_for_each_connector_iter(connector, &conn_iter)
514 DRM_ERROR("connector %s leaked!\n", connector->name);
515 drm_connector_list_iter_end(&conn_iter);
516 }
517
518 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
519 head) {
520 drm_property_destroy(dev, property);
521 }
522
523 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
524 head) {
525 plane->funcs->destroy(plane);
526 }
527
528 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
529 crtc->funcs->destroy(crtc);
530 }
531
532 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
533 head_global) {
534 drm_property_blob_put(blob);
535 }
536
537 /*
538 * Single-threaded teardown context, so it's not required to grab the
539 * fb_lock to protect against concurrent fb_list access. Contrary, it
540 * would actually deadlock with the drm_framebuffer_cleanup function.
541 *
542 * Also, if there are any framebuffers left, that's a driver leak now,
543 * so politely WARN about this.
544 */
545 WARN_ON(!list_empty(&dev->mode_config.fb_list));
546 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
547 struct drm_printer p = drm_debug_printer("[leaked fb]");
548
549 drm_printf(&p, "framebuffer[%u]:\n", fb->base.id);
550 drm_framebuffer_print_info(&p, 1, fb);
551 drm_framebuffer_free(&fb->base.refcount);
552 }
553
554 ida_destroy(&dev->mode_config.connector_ida);
555 idr_destroy(&dev->mode_config.tile_idr);
556 idr_destroy(&dev->mode_config.object_idr);
557 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
558}
559EXPORT_SYMBOL(drm_mode_config_cleanup);
560
561static u32 full_encoder_mask(struct drm_device *dev)
562{
563 struct drm_encoder *encoder;
564 u32 encoder_mask = 0;
565
566 drm_for_each_encoder(encoder, dev)
567 encoder_mask |= drm_encoder_mask(encoder);
568
569 return encoder_mask;
570}
571
572/*
573 * For some reason we want the encoder itself included in
574 * possible_clones. Make life easy for drivers by allowing them
575 * to leave possible_clones unset if no cloning is possible.
576 */
577static void fixup_encoder_possible_clones(struct drm_encoder *encoder)
578{
579 if (encoder->possible_clones == 0)
580 encoder->possible_clones = drm_encoder_mask(encoder);
581}
582
583static void validate_encoder_possible_clones(struct drm_encoder *encoder)
584{
585 struct drm_device *dev = encoder->dev;
586 u32 encoder_mask = full_encoder_mask(dev);
587 struct drm_encoder *other;
588
589 drm_for_each_encoder(other, dev) {
590 WARN(!!(encoder->possible_clones & drm_encoder_mask(other)) !=
591 !!(other->possible_clones & drm_encoder_mask(encoder)),
592 "possible_clones mismatch: "
593 "[ENCODER:%d:%s] mask=0x%x possible_clones=0x%x vs. "
594 "[ENCODER:%d:%s] mask=0x%x possible_clones=0x%x\n",
595 encoder->base.id, encoder->name,
596 drm_encoder_mask(encoder), encoder->possible_clones,
597 other->base.id, other->name,
598 drm_encoder_mask(other), other->possible_clones);
599 }
600
601 WARN((encoder->possible_clones & drm_encoder_mask(encoder)) == 0 ||
602 (encoder->possible_clones & ~encoder_mask) != 0,
603 "Bogus possible_clones: "
604 "[ENCODER:%d:%s] possible_clones=0x%x (full encoder mask=0x%x)\n",
605 encoder->base.id, encoder->name,
606 encoder->possible_clones, encoder_mask);
607}
608
609static u32 full_crtc_mask(struct drm_device *dev)
610{
611 struct drm_crtc *crtc;
612 u32 crtc_mask = 0;
613
614 drm_for_each_crtc(crtc, dev)
615 crtc_mask |= drm_crtc_mask(crtc);
616
617 return crtc_mask;
618}
619
620static void validate_encoder_possible_crtcs(struct drm_encoder *encoder)
621{
622 u32 crtc_mask = full_crtc_mask(encoder->dev);
623
624 WARN((encoder->possible_crtcs & crtc_mask) == 0 ||
625 (encoder->possible_crtcs & ~crtc_mask) != 0,
626 "Bogus possible_crtcs: "
627 "[ENCODER:%d:%s] possible_crtcs=0x%x (full crtc mask=0x%x)\n",
628 encoder->base.id, encoder->name,
629 encoder->possible_crtcs, crtc_mask);
630}
631
632void drm_mode_config_validate(struct drm_device *dev)
633{
634 struct drm_encoder *encoder;
635 struct drm_crtc *crtc;
636 struct drm_plane *plane;
637 u32 primary_with_crtc = 0, cursor_with_crtc = 0;
638 unsigned int num_primary = 0;
639
640 if (!drm_core_check_feature(dev, DRIVER_MODESET))
641 return;
642
643 drm_for_each_encoder(encoder, dev)
644 fixup_encoder_possible_clones(encoder);
645
646 drm_for_each_encoder(encoder, dev) {
647 validate_encoder_possible_clones(encoder);
648 validate_encoder_possible_crtcs(encoder);
649 }
650
651 drm_for_each_crtc(crtc, dev) {
652 WARN(!crtc->primary, "Missing primary plane on [CRTC:%d:%s]\n",
653 crtc->base.id, crtc->name);
654
655 WARN(crtc->cursor && crtc->funcs->cursor_set,
656 "[CRTC:%d:%s] must not have both a cursor plane and a cursor_set func",
657 crtc->base.id, crtc->name);
658 WARN(crtc->cursor && crtc->funcs->cursor_set2,
659 "[CRTC:%d:%s] must not have both a cursor plane and a cursor_set2 func",
660 crtc->base.id, crtc->name);
661 WARN(crtc->cursor && crtc->funcs->cursor_move,
662 "[CRTC:%d:%s] must not have both a cursor plane and a cursor_move func",
663 crtc->base.id, crtc->name);
664
665 if (crtc->primary) {
666 WARN(!(crtc->primary->possible_crtcs & drm_crtc_mask(crtc)),
667 "Bogus primary plane possible_crtcs: [PLANE:%d:%s] must be compatible with [CRTC:%d:%s]\n",
668 crtc->primary->base.id, crtc->primary->name,
669 crtc->base.id, crtc->name);
670 WARN(primary_with_crtc & drm_plane_mask(crtc->primary),
671 "Primary plane [PLANE:%d:%s] used for multiple CRTCs",
672 crtc->primary->base.id, crtc->primary->name);
673 primary_with_crtc |= drm_plane_mask(crtc->primary);
674 }
675 if (crtc->cursor) {
676 WARN(!(crtc->cursor->possible_crtcs & drm_crtc_mask(crtc)),
677 "Bogus cursor plane possible_crtcs: [PLANE:%d:%s] must be compatible with [CRTC:%d:%s]\n",
678 crtc->cursor->base.id, crtc->cursor->name,
679 crtc->base.id, crtc->name);
680 WARN(cursor_with_crtc & drm_plane_mask(crtc->cursor),
681 "Cursor plane [PLANE:%d:%s] used for multiple CRTCs",
682 crtc->cursor->base.id, crtc->cursor->name);
683 cursor_with_crtc |= drm_plane_mask(crtc->cursor);
684 }
685 }
686
687 drm_for_each_plane(plane, dev) {
688 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
689 num_primary++;
690 }
691
692 WARN(num_primary != dev->mode_config.num_crtc,
693 "Must have as many primary planes as there are CRTCs, but have %u primary planes and %u CRTCs",
694 num_primary, dev->mode_config.num_crtc);
695}
1/*
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
23#include <drm/drm_mode_config.h>
24#include <drm/drmP.h>
25
26#include "drm_crtc_internal.h"
27#include "drm_internal.h"
28
29int drm_modeset_register_all(struct drm_device *dev)
30{
31 int ret;
32
33 ret = drm_plane_register_all(dev);
34 if (ret)
35 goto err_plane;
36
37 ret = drm_crtc_register_all(dev);
38 if (ret)
39 goto err_crtc;
40
41 ret = drm_encoder_register_all(dev);
42 if (ret)
43 goto err_encoder;
44
45 ret = drm_connector_register_all(dev);
46 if (ret)
47 goto err_connector;
48
49 return 0;
50
51err_connector:
52 drm_encoder_unregister_all(dev);
53err_encoder:
54 drm_crtc_unregister_all(dev);
55err_crtc:
56 drm_plane_unregister_all(dev);
57err_plane:
58 return ret;
59}
60
61void drm_modeset_unregister_all(struct drm_device *dev)
62{
63 drm_connector_unregister_all(dev);
64 drm_encoder_unregister_all(dev);
65 drm_crtc_unregister_all(dev);
66 drm_plane_unregister_all(dev);
67}
68
69/**
70 * drm_mode_getresources - get graphics configuration
71 * @dev: drm device for the ioctl
72 * @data: data pointer for the ioctl
73 * @file_priv: drm file for the ioctl call
74 *
75 * Construct a set of configuration description structures and return
76 * them to the user, including CRTC, connector and framebuffer configuration.
77 *
78 * Called by the user via ioctl.
79 *
80 * Returns:
81 * Zero on success, negative errno on failure.
82 */
83int drm_mode_getresources(struct drm_device *dev, void *data,
84 struct drm_file *file_priv)
85{
86 struct drm_mode_card_res *card_res = data;
87 struct list_head *lh;
88 struct drm_framebuffer *fb;
89 struct drm_connector *connector;
90 struct drm_crtc *crtc;
91 struct drm_encoder *encoder;
92 int ret = 0;
93 int connector_count = 0;
94 int crtc_count = 0;
95 int fb_count = 0;
96 int encoder_count = 0;
97 int copied = 0;
98 uint32_t __user *fb_id;
99 uint32_t __user *crtc_id;
100 uint32_t __user *connector_id;
101 uint32_t __user *encoder_id;
102
103 if (!drm_core_check_feature(dev, DRIVER_MODESET))
104 return -EINVAL;
105
106
107 mutex_lock(&file_priv->fbs_lock);
108 /*
109 * For the non-control nodes we need to limit the list of resources
110 * by IDs in the group list for this node
111 */
112 list_for_each(lh, &file_priv->fbs)
113 fb_count++;
114
115 /* handle this in 4 parts */
116 /* FBs */
117 if (card_res->count_fbs >= fb_count) {
118 copied = 0;
119 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
120 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
121 if (put_user(fb->base.id, fb_id + copied)) {
122 mutex_unlock(&file_priv->fbs_lock);
123 return -EFAULT;
124 }
125 copied++;
126 }
127 }
128 card_res->count_fbs = fb_count;
129 mutex_unlock(&file_priv->fbs_lock);
130
131 /* mode_config.mutex protects the connector list against e.g. DP MST
132 * connector hot-adding. CRTC/Plane lists are invariant. */
133 mutex_lock(&dev->mode_config.mutex);
134 drm_for_each_crtc(crtc, dev)
135 crtc_count++;
136
137 drm_for_each_connector(connector, dev)
138 connector_count++;
139
140 drm_for_each_encoder(encoder, dev)
141 encoder_count++;
142
143 card_res->max_height = dev->mode_config.max_height;
144 card_res->min_height = dev->mode_config.min_height;
145 card_res->max_width = dev->mode_config.max_width;
146 card_res->min_width = dev->mode_config.min_width;
147
148 /* CRTCs */
149 if (card_res->count_crtcs >= crtc_count) {
150 copied = 0;
151 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
152 drm_for_each_crtc(crtc, dev) {
153 if (put_user(crtc->base.id, crtc_id + copied)) {
154 ret = -EFAULT;
155 goto out;
156 }
157 copied++;
158 }
159 }
160 card_res->count_crtcs = crtc_count;
161
162 /* Encoders */
163 if (card_res->count_encoders >= encoder_count) {
164 copied = 0;
165 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
166 drm_for_each_encoder(encoder, dev) {
167 if (put_user(encoder->base.id, encoder_id +
168 copied)) {
169 ret = -EFAULT;
170 goto out;
171 }
172 copied++;
173 }
174 }
175 card_res->count_encoders = encoder_count;
176
177 /* Connectors */
178 if (card_res->count_connectors >= connector_count) {
179 copied = 0;
180 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
181 drm_for_each_connector(connector, dev) {
182 if (put_user(connector->base.id,
183 connector_id + copied)) {
184 ret = -EFAULT;
185 goto out;
186 }
187 copied++;
188 }
189 }
190 card_res->count_connectors = connector_count;
191
192out:
193 mutex_unlock(&dev->mode_config.mutex);
194 return ret;
195}
196
197/**
198 * drm_mode_config_reset - call ->reset callbacks
199 * @dev: drm device
200 *
201 * This functions calls all the crtc's, encoder's and connector's ->reset
202 * callback. Drivers can use this in e.g. their driver load or resume code to
203 * reset hardware and software state.
204 */
205void drm_mode_config_reset(struct drm_device *dev)
206{
207 struct drm_crtc *crtc;
208 struct drm_plane *plane;
209 struct drm_encoder *encoder;
210 struct drm_connector *connector;
211
212 drm_for_each_plane(plane, dev)
213 if (plane->funcs->reset)
214 plane->funcs->reset(plane);
215
216 drm_for_each_crtc(crtc, dev)
217 if (crtc->funcs->reset)
218 crtc->funcs->reset(crtc);
219
220 drm_for_each_encoder(encoder, dev)
221 if (encoder->funcs->reset)
222 encoder->funcs->reset(encoder);
223
224 mutex_lock(&dev->mode_config.mutex);
225 drm_for_each_connector(connector, dev)
226 if (connector->funcs->reset)
227 connector->funcs->reset(connector);
228 mutex_unlock(&dev->mode_config.mutex);
229}
230EXPORT_SYMBOL(drm_mode_config_reset);
231
232/*
233 * Global properties
234 */
235static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
236 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
237 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
238 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
239};
240
241static int drm_mode_create_standard_properties(struct drm_device *dev)
242{
243 struct drm_property *prop;
244 int ret;
245
246 ret = drm_connector_create_standard_properties(dev);
247 if (ret)
248 return ret;
249
250 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
251 "type", drm_plane_type_enum_list,
252 ARRAY_SIZE(drm_plane_type_enum_list));
253 if (!prop)
254 return -ENOMEM;
255 dev->mode_config.plane_type_property = prop;
256
257 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
258 "SRC_X", 0, UINT_MAX);
259 if (!prop)
260 return -ENOMEM;
261 dev->mode_config.prop_src_x = prop;
262
263 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
264 "SRC_Y", 0, UINT_MAX);
265 if (!prop)
266 return -ENOMEM;
267 dev->mode_config.prop_src_y = prop;
268
269 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
270 "SRC_W", 0, UINT_MAX);
271 if (!prop)
272 return -ENOMEM;
273 dev->mode_config.prop_src_w = prop;
274
275 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
276 "SRC_H", 0, UINT_MAX);
277 if (!prop)
278 return -ENOMEM;
279 dev->mode_config.prop_src_h = prop;
280
281 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
282 "CRTC_X", INT_MIN, INT_MAX);
283 if (!prop)
284 return -ENOMEM;
285 dev->mode_config.prop_crtc_x = prop;
286
287 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
288 "CRTC_Y", INT_MIN, INT_MAX);
289 if (!prop)
290 return -ENOMEM;
291 dev->mode_config.prop_crtc_y = prop;
292
293 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
294 "CRTC_W", 0, INT_MAX);
295 if (!prop)
296 return -ENOMEM;
297 dev->mode_config.prop_crtc_w = prop;
298
299 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
300 "CRTC_H", 0, INT_MAX);
301 if (!prop)
302 return -ENOMEM;
303 dev->mode_config.prop_crtc_h = prop;
304
305 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
306 "FB_ID", DRM_MODE_OBJECT_FB);
307 if (!prop)
308 return -ENOMEM;
309 dev->mode_config.prop_fb_id = prop;
310
311 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
312 "IN_FENCE_FD", -1, INT_MAX);
313 if (!prop)
314 return -ENOMEM;
315 dev->mode_config.prop_in_fence_fd = prop;
316
317 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
318 "OUT_FENCE_PTR", 0, U64_MAX);
319 if (!prop)
320 return -ENOMEM;
321 dev->mode_config.prop_out_fence_ptr = prop;
322
323 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
324 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
325 if (!prop)
326 return -ENOMEM;
327 dev->mode_config.prop_crtc_id = prop;
328
329 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
330 "ACTIVE");
331 if (!prop)
332 return -ENOMEM;
333 dev->mode_config.prop_active = prop;
334
335 prop = drm_property_create(dev,
336 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
337 "MODE_ID", 0);
338 if (!prop)
339 return -ENOMEM;
340 dev->mode_config.prop_mode_id = prop;
341
342 prop = drm_property_create(dev,
343 DRM_MODE_PROP_BLOB,
344 "DEGAMMA_LUT", 0);
345 if (!prop)
346 return -ENOMEM;
347 dev->mode_config.degamma_lut_property = prop;
348
349 prop = drm_property_create_range(dev,
350 DRM_MODE_PROP_IMMUTABLE,
351 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
352 if (!prop)
353 return -ENOMEM;
354 dev->mode_config.degamma_lut_size_property = prop;
355
356 prop = drm_property_create(dev,
357 DRM_MODE_PROP_BLOB,
358 "CTM", 0);
359 if (!prop)
360 return -ENOMEM;
361 dev->mode_config.ctm_property = prop;
362
363 prop = drm_property_create(dev,
364 DRM_MODE_PROP_BLOB,
365 "GAMMA_LUT", 0);
366 if (!prop)
367 return -ENOMEM;
368 dev->mode_config.gamma_lut_property = prop;
369
370 prop = drm_property_create_range(dev,
371 DRM_MODE_PROP_IMMUTABLE,
372 "GAMMA_LUT_SIZE", 0, UINT_MAX);
373 if (!prop)
374 return -ENOMEM;
375 dev->mode_config.gamma_lut_size_property = prop;
376
377 return 0;
378}
379
380/**
381 * drm_mode_config_init - initialize DRM mode_configuration structure
382 * @dev: DRM device
383 *
384 * Initialize @dev's mode_config structure, used for tracking the graphics
385 * configuration of @dev.
386 *
387 * Since this initializes the modeset locks, no locking is possible. Which is no
388 * problem, since this should happen single threaded at init time. It is the
389 * driver's problem to ensure this guarantee.
390 *
391 */
392void drm_mode_config_init(struct drm_device *dev)
393{
394 mutex_init(&dev->mode_config.mutex);
395 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
396 mutex_init(&dev->mode_config.idr_mutex);
397 mutex_init(&dev->mode_config.fb_lock);
398 mutex_init(&dev->mode_config.blob_lock);
399 INIT_LIST_HEAD(&dev->mode_config.fb_list);
400 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
401 INIT_LIST_HEAD(&dev->mode_config.connector_list);
402 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
403 INIT_LIST_HEAD(&dev->mode_config.property_list);
404 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
405 INIT_LIST_HEAD(&dev->mode_config.plane_list);
406 idr_init(&dev->mode_config.crtc_idr);
407 idr_init(&dev->mode_config.tile_idr);
408 ida_init(&dev->mode_config.connector_ida);
409
410 drm_modeset_lock_all(dev);
411 drm_mode_create_standard_properties(dev);
412 drm_modeset_unlock_all(dev);
413
414 /* Just to be sure */
415 dev->mode_config.num_fb = 0;
416 dev->mode_config.num_connector = 0;
417 dev->mode_config.num_crtc = 0;
418 dev->mode_config.num_encoder = 0;
419 dev->mode_config.num_overlay_plane = 0;
420 dev->mode_config.num_total_plane = 0;
421}
422EXPORT_SYMBOL(drm_mode_config_init);
423
424/**
425 * drm_mode_config_cleanup - free up DRM mode_config info
426 * @dev: DRM device
427 *
428 * Free up all the connectors and CRTCs associated with this DRM device, then
429 * free up the framebuffers and associated buffer objects.
430 *
431 * Note that since this /should/ happen single-threaded at driver/device
432 * teardown time, no locking is required. It's the driver's job to ensure that
433 * this guarantee actually holds true.
434 *
435 * FIXME: cleanup any dangling user buffer objects too
436 */
437void drm_mode_config_cleanup(struct drm_device *dev)
438{
439 struct drm_connector *connector, *ot;
440 struct drm_crtc *crtc, *ct;
441 struct drm_encoder *encoder, *enct;
442 struct drm_framebuffer *fb, *fbt;
443 struct drm_property *property, *pt;
444 struct drm_property_blob *blob, *bt;
445 struct drm_plane *plane, *plt;
446
447 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
448 head) {
449 encoder->funcs->destroy(encoder);
450 }
451
452 list_for_each_entry_safe(connector, ot,
453 &dev->mode_config.connector_list, head) {
454 connector->funcs->destroy(connector);
455 }
456
457 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
458 head) {
459 drm_property_destroy(dev, property);
460 }
461
462 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
463 head) {
464 plane->funcs->destroy(plane);
465 }
466
467 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
468 crtc->funcs->destroy(crtc);
469 }
470
471 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
472 head_global) {
473 drm_property_unreference_blob(blob);
474 }
475
476 /*
477 * Single-threaded teardown context, so it's not required to grab the
478 * fb_lock to protect against concurrent fb_list access. Contrary, it
479 * would actually deadlock with the drm_framebuffer_cleanup function.
480 *
481 * Also, if there are any framebuffers left, that's a driver leak now,
482 * so politely WARN about this.
483 */
484 WARN_ON(!list_empty(&dev->mode_config.fb_list));
485 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
486 drm_framebuffer_free(&fb->base.refcount);
487 }
488
489 ida_destroy(&dev->mode_config.connector_ida);
490 idr_destroy(&dev->mode_config.tile_idr);
491 idr_destroy(&dev->mode_config.crtc_idr);
492 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
493}
494EXPORT_SYMBOL(drm_mode_config_cleanup);