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