Merge tag 'drm-misc-next-2019-05-24' of git://anongit.freedesktop.org/drm/drm-misc...
[linux-2.6-microblaze.git] / drivers / gpu / drm / drm_connector.c
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_connector.h>
24 #include <drm/drm_edid.h>
25 #include <drm/drm_encoder.h>
26 #include <drm/drm_utils.h>
27 #include <drm/drm_print.h>
28 #include <drm/drm_drv.h>
29 #include <drm/drm_file.h>
30
31 #include <linux/uaccess.h>
32
33 #include "drm_crtc_internal.h"
34 #include "drm_internal.h"
35
36 /**
37  * DOC: overview
38  *
39  * In DRM connectors are the general abstraction for display sinks, and include
40  * als fixed panels or anything else that can display pixels in some form. As
41  * opposed to all other KMS objects representing hardware (like CRTC, encoder or
42  * plane abstractions) connectors can be hotplugged and unplugged at runtime.
43  * Hence they are reference-counted using drm_connector_get() and
44  * drm_connector_put().
45  *
46  * KMS driver must create, initialize, register and attach at a &struct
47  * drm_connector for each such sink. The instance is created as other KMS
48  * objects and initialized by setting the following fields. The connector is
49  * initialized with a call to drm_connector_init() with a pointer to the
50  * &struct drm_connector_funcs and a connector type, and then exposed to
51  * userspace with a call to drm_connector_register().
52  *
53  * Connectors must be attached to an encoder to be used. For devices that map
54  * connectors to encoders 1:1, the connector should be attached at
55  * initialization time with a call to drm_connector_attach_encoder(). The
56  * driver must also set the &drm_connector.encoder field to point to the
57  * attached encoder.
58  *
59  * For connectors which are not fixed (like built-in panels) the driver needs to
60  * support hotplug notifications. The simplest way to do that is by using the
61  * probe helpers, see drm_kms_helper_poll_init() for connectors which don't have
62  * hardware support for hotplug interrupts. Connectors with hardware hotplug
63  * support can instead use e.g. drm_helper_hpd_irq_event().
64  */
65
66 struct drm_conn_prop_enum_list {
67         int type;
68         const char *name;
69         struct ida ida;
70 };
71
72 /*
73  * Connector and encoder types.
74  */
75 static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
76         { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
77         { DRM_MODE_CONNECTOR_VGA, "VGA" },
78         { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
79         { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
80         { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
81         { DRM_MODE_CONNECTOR_Composite, "Composite" },
82         { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
83         { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
84         { DRM_MODE_CONNECTOR_Component, "Component" },
85         { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
86         { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
87         { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
88         { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
89         { DRM_MODE_CONNECTOR_TV, "TV" },
90         { DRM_MODE_CONNECTOR_eDP, "eDP" },
91         { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
92         { DRM_MODE_CONNECTOR_DSI, "DSI" },
93         { DRM_MODE_CONNECTOR_DPI, "DPI" },
94         { DRM_MODE_CONNECTOR_WRITEBACK, "Writeback" },
95 };
96
97 void drm_connector_ida_init(void)
98 {
99         int i;
100
101         for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
102                 ida_init(&drm_connector_enum_list[i].ida);
103 }
104
105 void drm_connector_ida_destroy(void)
106 {
107         int i;
108
109         for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
110                 ida_destroy(&drm_connector_enum_list[i].ida);
111 }
112
113 /**
114  * drm_connector_get_cmdline_mode - reads the user's cmdline mode
115  * @connector: connector to quwery
116  *
117  * The kernel supports per-connector configuration of its consoles through
118  * use of the video= parameter. This function parses that option and
119  * extracts the user's specified mode (or enable/disable status) for a
120  * particular connector. This is typically only used during the early fbdev
121  * setup.
122  */
123 static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
124 {
125         struct drm_cmdline_mode *mode = &connector->cmdline_mode;
126         char *option = NULL;
127
128         if (fb_get_options(connector->name, &option))
129                 return;
130
131         if (!drm_mode_parse_command_line_for_connector(option,
132                                                        connector,
133                                                        mode))
134                 return;
135
136         if (mode->force) {
137                 DRM_INFO("forcing %s connector %s\n", connector->name,
138                          drm_get_connector_force_name(mode->force));
139                 connector->force = mode->force;
140         }
141
142         DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
143                       connector->name,
144                       mode->xres, mode->yres,
145                       mode->refresh_specified ? mode->refresh : 60,
146                       mode->rb ? " reduced blanking" : "",
147                       mode->margins ? " with margins" : "",
148                       mode->interlace ?  " interlaced" : "");
149 }
150
151 static void drm_connector_free(struct kref *kref)
152 {
153         struct drm_connector *connector =
154                 container_of(kref, struct drm_connector, base.refcount);
155         struct drm_device *dev = connector->dev;
156
157         drm_mode_object_unregister(dev, &connector->base);
158         connector->funcs->destroy(connector);
159 }
160
161 void drm_connector_free_work_fn(struct work_struct *work)
162 {
163         struct drm_connector *connector, *n;
164         struct drm_device *dev =
165                 container_of(work, struct drm_device, mode_config.connector_free_work);
166         struct drm_mode_config *config = &dev->mode_config;
167         unsigned long flags;
168         struct llist_node *freed;
169
170         spin_lock_irqsave(&config->connector_list_lock, flags);
171         freed = llist_del_all(&config->connector_free_list);
172         spin_unlock_irqrestore(&config->connector_list_lock, flags);
173
174         llist_for_each_entry_safe(connector, n, freed, free_node) {
175                 drm_mode_object_unregister(dev, &connector->base);
176                 connector->funcs->destroy(connector);
177         }
178 }
179
180 /**
181  * drm_connector_init - Init a preallocated connector
182  * @dev: DRM device
183  * @connector: the connector to init
184  * @funcs: callbacks for this connector
185  * @connector_type: user visible type of the connector
186  *
187  * Initialises a preallocated connector. Connectors should be
188  * subclassed as part of driver connector objects.
189  *
190  * Returns:
191  * Zero on success, error code on failure.
192  */
193 int drm_connector_init(struct drm_device *dev,
194                        struct drm_connector *connector,
195                        const struct drm_connector_funcs *funcs,
196                        int connector_type)
197 {
198         struct drm_mode_config *config = &dev->mode_config;
199         int ret;
200         struct ida *connector_ida =
201                 &drm_connector_enum_list[connector_type].ida;
202
203         WARN_ON(drm_drv_uses_atomic_modeset(dev) &&
204                 (!funcs->atomic_destroy_state ||
205                  !funcs->atomic_duplicate_state));
206
207         ret = __drm_mode_object_add(dev, &connector->base,
208                                     DRM_MODE_OBJECT_CONNECTOR,
209                                     false, drm_connector_free);
210         if (ret)
211                 return ret;
212
213         connector->base.properties = &connector->properties;
214         connector->dev = dev;
215         connector->funcs = funcs;
216
217         /* connector index is used with 32bit bitmasks */
218         ret = ida_simple_get(&config->connector_ida, 0, 32, GFP_KERNEL);
219         if (ret < 0) {
220                 DRM_DEBUG_KMS("Failed to allocate %s connector index: %d\n",
221                               drm_connector_enum_list[connector_type].name,
222                               ret);
223                 goto out_put;
224         }
225         connector->index = ret;
226         ret = 0;
227
228         connector->connector_type = connector_type;
229         connector->connector_type_id =
230                 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
231         if (connector->connector_type_id < 0) {
232                 ret = connector->connector_type_id;
233                 goto out_put_id;
234         }
235         connector->name =
236                 kasprintf(GFP_KERNEL, "%s-%d",
237                           drm_connector_enum_list[connector_type].name,
238                           connector->connector_type_id);
239         if (!connector->name) {
240                 ret = -ENOMEM;
241                 goto out_put_type_id;
242         }
243
244         INIT_LIST_HEAD(&connector->probed_modes);
245         INIT_LIST_HEAD(&connector->modes);
246         mutex_init(&connector->mutex);
247         connector->edid_blob_ptr = NULL;
248         connector->tile_blob_ptr = NULL;
249         connector->status = connector_status_unknown;
250         connector->display_info.panel_orientation =
251                 DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
252
253         drm_connector_get_cmdline_mode(connector);
254
255         /* We should add connectors at the end to avoid upsetting the connector
256          * index too much. */
257         spin_lock_irq(&config->connector_list_lock);
258         list_add_tail(&connector->head, &config->connector_list);
259         config->num_connector++;
260         spin_unlock_irq(&config->connector_list_lock);
261
262         if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL &&
263             connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
264                 drm_connector_attach_edid_property(connector);
265
266         drm_object_attach_property(&connector->base,
267                                       config->dpms_property, 0);
268
269         drm_object_attach_property(&connector->base,
270                                    config->link_status_property,
271                                    0);
272
273         drm_object_attach_property(&connector->base,
274                                    config->non_desktop_property,
275                                    0);
276         drm_object_attach_property(&connector->base,
277                                    config->tile_property,
278                                    0);
279
280         if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
281                 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
282         }
283
284         connector->debugfs_entry = NULL;
285 out_put_type_id:
286         if (ret)
287                 ida_simple_remove(connector_ida, connector->connector_type_id);
288 out_put_id:
289         if (ret)
290                 ida_simple_remove(&config->connector_ida, connector->index);
291 out_put:
292         if (ret)
293                 drm_mode_object_unregister(dev, &connector->base);
294
295         return ret;
296 }
297 EXPORT_SYMBOL(drm_connector_init);
298
299 /**
300  * drm_connector_attach_edid_property - attach edid property.
301  * @connector: the connector
302  *
303  * Some connector types like DRM_MODE_CONNECTOR_VIRTUAL do not get a
304  * edid property attached by default.  This function can be used to
305  * explicitly enable the edid property in these cases.
306  */
307 void drm_connector_attach_edid_property(struct drm_connector *connector)
308 {
309         struct drm_mode_config *config = &connector->dev->mode_config;
310
311         drm_object_attach_property(&connector->base,
312                                    config->edid_property,
313                                    0);
314 }
315 EXPORT_SYMBOL(drm_connector_attach_edid_property);
316
317 /**
318  * drm_connector_attach_encoder - attach a connector to an encoder
319  * @connector: connector to attach
320  * @encoder: encoder to attach @connector to
321  *
322  * This function links up a connector to an encoder. Note that the routing
323  * restrictions between encoders and crtcs are exposed to userspace through the
324  * possible_clones and possible_crtcs bitmasks.
325  *
326  * Returns:
327  * Zero on success, negative errno on failure.
328  */
329 int drm_connector_attach_encoder(struct drm_connector *connector,
330                                  struct drm_encoder *encoder)
331 {
332         int i;
333
334         /*
335          * In the past, drivers have attempted to model the static association
336          * of connector to encoder in simple connector/encoder devices using a
337          * direct assignment of connector->encoder = encoder. This connection
338          * is a logical one and the responsibility of the core, so drivers are
339          * expected not to mess with this.
340          *
341          * Note that the error return should've been enough here, but a large
342          * majority of drivers ignores the return value, so add in a big WARN
343          * to get people's attention.
344          */
345         if (WARN_ON(connector->encoder))
346                 return -EINVAL;
347
348         for (i = 0; i < ARRAY_SIZE(connector->encoder_ids); i++) {
349                 if (connector->encoder_ids[i] == 0) {
350                         connector->encoder_ids[i] = encoder->base.id;
351                         return 0;
352                 }
353         }
354         return -ENOMEM;
355 }
356 EXPORT_SYMBOL(drm_connector_attach_encoder);
357
358 /**
359  * drm_connector_has_possible_encoder - check if the connector and encoder are assosicated with each other
360  * @connector: the connector
361  * @encoder: the encoder
362  *
363  * Returns:
364  * True if @encoder is one of the possible encoders for @connector.
365  */
366 bool drm_connector_has_possible_encoder(struct drm_connector *connector,
367                                         struct drm_encoder *encoder)
368 {
369         struct drm_encoder *enc;
370         int i;
371
372         drm_connector_for_each_possible_encoder(connector, enc, i) {
373                 if (enc == encoder)
374                         return true;
375         }
376
377         return false;
378 }
379 EXPORT_SYMBOL(drm_connector_has_possible_encoder);
380
381 static void drm_mode_remove(struct drm_connector *connector,
382                             struct drm_display_mode *mode)
383 {
384         list_del(&mode->head);
385         drm_mode_destroy(connector->dev, mode);
386 }
387
388 /**
389  * drm_connector_cleanup - cleans up an initialised connector
390  * @connector: connector to cleanup
391  *
392  * Cleans up the connector but doesn't free the object.
393  */
394 void drm_connector_cleanup(struct drm_connector *connector)
395 {
396         struct drm_device *dev = connector->dev;
397         struct drm_display_mode *mode, *t;
398
399         /* The connector should have been removed from userspace long before
400          * it is finally destroyed.
401          */
402         if (WARN_ON(connector->registration_state ==
403                     DRM_CONNECTOR_REGISTERED))
404                 drm_connector_unregister(connector);
405
406         if (connector->tile_group) {
407                 drm_mode_put_tile_group(dev, connector->tile_group);
408                 connector->tile_group = NULL;
409         }
410
411         list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
412                 drm_mode_remove(connector, mode);
413
414         list_for_each_entry_safe(mode, t, &connector->modes, head)
415                 drm_mode_remove(connector, mode);
416
417         ida_simple_remove(&drm_connector_enum_list[connector->connector_type].ida,
418                           connector->connector_type_id);
419
420         ida_simple_remove(&dev->mode_config.connector_ida,
421                           connector->index);
422
423         kfree(connector->display_info.bus_formats);
424         drm_mode_object_unregister(dev, &connector->base);
425         kfree(connector->name);
426         connector->name = NULL;
427         spin_lock_irq(&dev->mode_config.connector_list_lock);
428         list_del(&connector->head);
429         dev->mode_config.num_connector--;
430         spin_unlock_irq(&dev->mode_config.connector_list_lock);
431
432         WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
433         if (connector->state && connector->funcs->atomic_destroy_state)
434                 connector->funcs->atomic_destroy_state(connector,
435                                                        connector->state);
436
437         mutex_destroy(&connector->mutex);
438
439         memset(connector, 0, sizeof(*connector));
440 }
441 EXPORT_SYMBOL(drm_connector_cleanup);
442
443 /**
444  * drm_connector_register - register a connector
445  * @connector: the connector to register
446  *
447  * Register userspace interfaces for a connector
448  *
449  * Returns:
450  * Zero on success, error code on failure.
451  */
452 int drm_connector_register(struct drm_connector *connector)
453 {
454         int ret = 0;
455
456         if (!connector->dev->registered)
457                 return 0;
458
459         mutex_lock(&connector->mutex);
460         if (connector->registration_state != DRM_CONNECTOR_INITIALIZING)
461                 goto unlock;
462
463         ret = drm_sysfs_connector_add(connector);
464         if (ret)
465                 goto unlock;
466
467         ret = drm_debugfs_connector_add(connector);
468         if (ret) {
469                 goto err_sysfs;
470         }
471
472         if (connector->funcs->late_register) {
473                 ret = connector->funcs->late_register(connector);
474                 if (ret)
475                         goto err_debugfs;
476         }
477
478         drm_mode_object_register(connector->dev, &connector->base);
479
480         connector->registration_state = DRM_CONNECTOR_REGISTERED;
481         goto unlock;
482
483 err_debugfs:
484         drm_debugfs_connector_remove(connector);
485 err_sysfs:
486         drm_sysfs_connector_remove(connector);
487 unlock:
488         mutex_unlock(&connector->mutex);
489         return ret;
490 }
491 EXPORT_SYMBOL(drm_connector_register);
492
493 /**
494  * drm_connector_unregister - unregister a connector
495  * @connector: the connector to unregister
496  *
497  * Unregister userspace interfaces for a connector
498  */
499 void drm_connector_unregister(struct drm_connector *connector)
500 {
501         mutex_lock(&connector->mutex);
502         if (connector->registration_state != DRM_CONNECTOR_REGISTERED) {
503                 mutex_unlock(&connector->mutex);
504                 return;
505         }
506
507         if (connector->funcs->early_unregister)
508                 connector->funcs->early_unregister(connector);
509
510         drm_sysfs_connector_remove(connector);
511         drm_debugfs_connector_remove(connector);
512
513         connector->registration_state = DRM_CONNECTOR_UNREGISTERED;
514         mutex_unlock(&connector->mutex);
515 }
516 EXPORT_SYMBOL(drm_connector_unregister);
517
518 void drm_connector_unregister_all(struct drm_device *dev)
519 {
520         struct drm_connector *connector;
521         struct drm_connector_list_iter conn_iter;
522
523         drm_connector_list_iter_begin(dev, &conn_iter);
524         drm_for_each_connector_iter(connector, &conn_iter)
525                 drm_connector_unregister(connector);
526         drm_connector_list_iter_end(&conn_iter);
527 }
528
529 int drm_connector_register_all(struct drm_device *dev)
530 {
531         struct drm_connector *connector;
532         struct drm_connector_list_iter conn_iter;
533         int ret = 0;
534
535         drm_connector_list_iter_begin(dev, &conn_iter);
536         drm_for_each_connector_iter(connector, &conn_iter) {
537                 ret = drm_connector_register(connector);
538                 if (ret)
539                         break;
540         }
541         drm_connector_list_iter_end(&conn_iter);
542
543         if (ret)
544                 drm_connector_unregister_all(dev);
545         return ret;
546 }
547
548 /**
549  * drm_get_connector_status_name - return a string for connector status
550  * @status: connector status to compute name of
551  *
552  * In contrast to the other drm_get_*_name functions this one here returns a
553  * const pointer and hence is threadsafe.
554  */
555 const char *drm_get_connector_status_name(enum drm_connector_status status)
556 {
557         if (status == connector_status_connected)
558                 return "connected";
559         else if (status == connector_status_disconnected)
560                 return "disconnected";
561         else
562                 return "unknown";
563 }
564 EXPORT_SYMBOL(drm_get_connector_status_name);
565
566 /**
567  * drm_get_connector_force_name - return a string for connector force
568  * @force: connector force to get name of
569  *
570  * Returns: const pointer to name.
571  */
572 const char *drm_get_connector_force_name(enum drm_connector_force force)
573 {
574         switch (force) {
575         case DRM_FORCE_UNSPECIFIED:
576                 return "unspecified";
577         case DRM_FORCE_OFF:
578                 return "off";
579         case DRM_FORCE_ON:
580                 return "on";
581         case DRM_FORCE_ON_DIGITAL:
582                 return "digital";
583         default:
584                 return "unknown";
585         }
586 }
587
588 #ifdef CONFIG_LOCKDEP
589 static struct lockdep_map connector_list_iter_dep_map = {
590         .name = "drm_connector_list_iter"
591 };
592 #endif
593
594 /**
595  * drm_connector_list_iter_begin - initialize a connector_list iterator
596  * @dev: DRM device
597  * @iter: connector_list iterator
598  *
599  * Sets @iter up to walk the &drm_mode_config.connector_list of @dev. @iter
600  * must always be cleaned up again by calling drm_connector_list_iter_end().
601  * Iteration itself happens using drm_connector_list_iter_next() or
602  * drm_for_each_connector_iter().
603  */
604 void drm_connector_list_iter_begin(struct drm_device *dev,
605                                    struct drm_connector_list_iter *iter)
606 {
607         iter->dev = dev;
608         iter->conn = NULL;
609         lock_acquire_shared_recursive(&connector_list_iter_dep_map, 0, 1, NULL, _RET_IP_);
610 }
611 EXPORT_SYMBOL(drm_connector_list_iter_begin);
612
613 /*
614  * Extra-safe connector put function that works in any context. Should only be
615  * used from the connector_iter functions, where we never really expect to
616  * actually release the connector when dropping our final reference.
617  */
618 static void
619 __drm_connector_put_safe(struct drm_connector *conn)
620 {
621         struct drm_mode_config *config = &conn->dev->mode_config;
622
623         lockdep_assert_held(&config->connector_list_lock);
624
625         if (!refcount_dec_and_test(&conn->base.refcount.refcount))
626                 return;
627
628         llist_add(&conn->free_node, &config->connector_free_list);
629         schedule_work(&config->connector_free_work);
630 }
631
632 /**
633  * drm_connector_list_iter_next - return next connector
634  * @iter: connector_list iterator
635  *
636  * Returns the next connector for @iter, or NULL when the list walk has
637  * completed.
638  */
639 struct drm_connector *
640 drm_connector_list_iter_next(struct drm_connector_list_iter *iter)
641 {
642         struct drm_connector *old_conn = iter->conn;
643         struct drm_mode_config *config = &iter->dev->mode_config;
644         struct list_head *lhead;
645         unsigned long flags;
646
647         spin_lock_irqsave(&config->connector_list_lock, flags);
648         lhead = old_conn ? &old_conn->head : &config->connector_list;
649
650         do {
651                 if (lhead->next == &config->connector_list) {
652                         iter->conn = NULL;
653                         break;
654                 }
655
656                 lhead = lhead->next;
657                 iter->conn = list_entry(lhead, struct drm_connector, head);
658
659                 /* loop until it's not a zombie connector */
660         } while (!kref_get_unless_zero(&iter->conn->base.refcount));
661
662         if (old_conn)
663                 __drm_connector_put_safe(old_conn);
664         spin_unlock_irqrestore(&config->connector_list_lock, flags);
665
666         return iter->conn;
667 }
668 EXPORT_SYMBOL(drm_connector_list_iter_next);
669
670 /**
671  * drm_connector_list_iter_end - tear down a connector_list iterator
672  * @iter: connector_list iterator
673  *
674  * Tears down @iter and releases any resources (like &drm_connector references)
675  * acquired while walking the list. This must always be called, both when the
676  * iteration completes fully or when it was aborted without walking the entire
677  * list.
678  */
679 void drm_connector_list_iter_end(struct drm_connector_list_iter *iter)
680 {
681         struct drm_mode_config *config = &iter->dev->mode_config;
682         unsigned long flags;
683
684         iter->dev = NULL;
685         if (iter->conn) {
686                 spin_lock_irqsave(&config->connector_list_lock, flags);
687                 __drm_connector_put_safe(iter->conn);
688                 spin_unlock_irqrestore(&config->connector_list_lock, flags);
689         }
690         lock_release(&connector_list_iter_dep_map, 0, _RET_IP_);
691 }
692 EXPORT_SYMBOL(drm_connector_list_iter_end);
693
694 static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
695         { SubPixelUnknown, "Unknown" },
696         { SubPixelHorizontalRGB, "Horizontal RGB" },
697         { SubPixelHorizontalBGR, "Horizontal BGR" },
698         { SubPixelVerticalRGB, "Vertical RGB" },
699         { SubPixelVerticalBGR, "Vertical BGR" },
700         { SubPixelNone, "None" },
701 };
702
703 /**
704  * drm_get_subpixel_order_name - return a string for a given subpixel enum
705  * @order: enum of subpixel_order
706  *
707  * Note you could abuse this and return something out of bounds, but that
708  * would be a caller error.  No unscrubbed user data should make it here.
709  */
710 const char *drm_get_subpixel_order_name(enum subpixel_order order)
711 {
712         return drm_subpixel_enum_list[order].name;
713 }
714 EXPORT_SYMBOL(drm_get_subpixel_order_name);
715
716 static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
717         { DRM_MODE_DPMS_ON, "On" },
718         { DRM_MODE_DPMS_STANDBY, "Standby" },
719         { DRM_MODE_DPMS_SUSPEND, "Suspend" },
720         { DRM_MODE_DPMS_OFF, "Off" }
721 };
722 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
723
724 static const struct drm_prop_enum_list drm_link_status_enum_list[] = {
725         { DRM_MODE_LINK_STATUS_GOOD, "Good" },
726         { DRM_MODE_LINK_STATUS_BAD, "Bad" },
727 };
728
729 /**
730  * drm_display_info_set_bus_formats - set the supported bus formats
731  * @info: display info to store bus formats in
732  * @formats: array containing the supported bus formats
733  * @num_formats: the number of entries in the fmts array
734  *
735  * Store the supported bus formats in display info structure.
736  * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
737  * a full list of available formats.
738  */
739 int drm_display_info_set_bus_formats(struct drm_display_info *info,
740                                      const u32 *formats,
741                                      unsigned int num_formats)
742 {
743         u32 *fmts = NULL;
744
745         if (!formats && num_formats)
746                 return -EINVAL;
747
748         if (formats && num_formats) {
749                 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
750                                GFP_KERNEL);
751                 if (!fmts)
752                         return -ENOMEM;
753         }
754
755         kfree(info->bus_formats);
756         info->bus_formats = fmts;
757         info->num_bus_formats = num_formats;
758
759         return 0;
760 }
761 EXPORT_SYMBOL(drm_display_info_set_bus_formats);
762
763 /* Optional connector properties. */
764 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
765         { DRM_MODE_SCALE_NONE, "None" },
766         { DRM_MODE_SCALE_FULLSCREEN, "Full" },
767         { DRM_MODE_SCALE_CENTER, "Center" },
768         { DRM_MODE_SCALE_ASPECT, "Full aspect" },
769 };
770
771 static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
772         { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
773         { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
774         { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
775 };
776
777 static const struct drm_prop_enum_list drm_content_type_enum_list[] = {
778         { DRM_MODE_CONTENT_TYPE_NO_DATA, "No Data" },
779         { DRM_MODE_CONTENT_TYPE_GRAPHICS, "Graphics" },
780         { DRM_MODE_CONTENT_TYPE_PHOTO, "Photo" },
781         { DRM_MODE_CONTENT_TYPE_CINEMA, "Cinema" },
782         { DRM_MODE_CONTENT_TYPE_GAME, "Game" },
783 };
784
785 static const struct drm_prop_enum_list drm_panel_orientation_enum_list[] = {
786         { DRM_MODE_PANEL_ORIENTATION_NORMAL,    "Normal"        },
787         { DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP, "Upside Down"   },
788         { DRM_MODE_PANEL_ORIENTATION_LEFT_UP,   "Left Side Up"  },
789         { DRM_MODE_PANEL_ORIENTATION_RIGHT_UP,  "Right Side Up" },
790 };
791
792 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
793         { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
794         { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
795         { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
796 };
797 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
798
799 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
800         { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
801         { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
802         { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
803 };
804 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
805                  drm_dvi_i_subconnector_enum_list)
806
807 static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
808         { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
809         { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
810         { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
811         { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
812         { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
813 };
814 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
815
816 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
817         { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
818         { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
819         { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
820         { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
821         { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
822 };
823 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
824                  drm_tv_subconnector_enum_list)
825
826 static struct drm_prop_enum_list drm_cp_enum_list[] = {
827         { DRM_MODE_CONTENT_PROTECTION_UNDESIRED, "Undesired" },
828         { DRM_MODE_CONTENT_PROTECTION_DESIRED, "Desired" },
829         { DRM_MODE_CONTENT_PROTECTION_ENABLED, "Enabled" },
830 };
831 DRM_ENUM_NAME_FN(drm_get_content_protection_name, drm_cp_enum_list)
832
833 static const struct drm_prop_enum_list hdmi_colorspaces[] = {
834         /* For Default case, driver will set the colorspace */
835         { DRM_MODE_COLORIMETRY_DEFAULT, "Default" },
836         /* Standard Definition Colorimetry based on CEA 861 */
837         { DRM_MODE_COLORIMETRY_SMPTE_170M_YCC, "SMPTE_170M_YCC" },
838         { DRM_MODE_COLORIMETRY_BT709_YCC, "BT709_YCC" },
839         /* Standard Definition Colorimetry based on IEC 61966-2-4 */
840         { DRM_MODE_COLORIMETRY_XVYCC_601, "XVYCC_601" },
841         /* High Definition Colorimetry based on IEC 61966-2-4 */
842         { DRM_MODE_COLORIMETRY_XVYCC_709, "XVYCC_709" },
843         /* Colorimetry based on IEC 61966-2-1/Amendment 1 */
844         { DRM_MODE_COLORIMETRY_SYCC_601, "SYCC_601" },
845         /* Colorimetry based on IEC 61966-2-5 [33] */
846         { DRM_MODE_COLORIMETRY_OPYCC_601, "opYCC_601" },
847         /* Colorimetry based on IEC 61966-2-5 */
848         { DRM_MODE_COLORIMETRY_OPRGB, "opRGB" },
849         /* Colorimetry based on ITU-R BT.2020 */
850         { DRM_MODE_COLORIMETRY_BT2020_CYCC, "BT2020_CYCC" },
851         /* Colorimetry based on ITU-R BT.2020 */
852         { DRM_MODE_COLORIMETRY_BT2020_RGB, "BT2020_RGB" },
853         /* Colorimetry based on ITU-R BT.2020 */
854         { DRM_MODE_COLORIMETRY_BT2020_YCC, "BT2020_YCC" },
855         /* Added as part of Additional Colorimetry Extension in 861.G */
856         { DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65, "DCI-P3_RGB_D65" },
857         { DRM_MODE_COLORIMETRY_DCI_P3_RGB_THEATER, "DCI-P3_RGB_Theater" },
858 };
859
860 /**
861  * DOC: standard connector properties
862  *
863  * DRM connectors have a few standardized properties:
864  *
865  * EDID:
866  *      Blob property which contains the current EDID read from the sink. This
867  *      is useful to parse sink identification information like vendor, model
868  *      and serial. Drivers should update this property by calling
869  *      drm_connector_update_edid_property(), usually after having parsed
870  *      the EDID using drm_add_edid_modes(). Userspace cannot change this
871  *      property.
872  * DPMS:
873  *      Legacy property for setting the power state of the connector. For atomic
874  *      drivers this is only provided for backwards compatibility with existing
875  *      drivers, it remaps to controlling the "ACTIVE" property on the CRTC the
876  *      connector is linked to. Drivers should never set this property directly,
877  *      it is handled by the DRM core by calling the &drm_connector_funcs.dpms
878  *      callback. For atomic drivers the remapping to the "ACTIVE" property is
879  *      implemented in the DRM core.  This is the only standard connector
880  *      property that userspace can change.
881  *
882  *      Note that this property cannot be set through the MODE_ATOMIC ioctl,
883  *      userspace must use "ACTIVE" on the CRTC instead.
884  *
885  *      WARNING:
886  *
887  *      For userspace also running on legacy drivers the "DPMS" semantics are a
888  *      lot more complicated. First, userspace cannot rely on the "DPMS" value
889  *      returned by the GETCONNECTOR actually reflecting reality, because many
890  *      drivers fail to update it. For atomic drivers this is taken care of in
891  *      drm_atomic_helper_update_legacy_modeset_state().
892  *
893  *      The second issue is that the DPMS state is only well-defined when the
894  *      connector is connected to a CRTC. In atomic the DRM core enforces that
895  *      "ACTIVE" is off in such a case, no such checks exists for "DPMS".
896  *
897  *      Finally, when enabling an output using the legacy SETCONFIG ioctl then
898  *      "DPMS" is forced to ON. But see above, that might not be reflected in
899  *      the software value on legacy drivers.
900  *
901  *      Summarizing: Only set "DPMS" when the connector is known to be enabled,
902  *      assume that a successful SETCONFIG call also sets "DPMS" to on, and
903  *      never read back the value of "DPMS" because it can be incorrect.
904  * PATH:
905  *      Connector path property to identify how this sink is physically
906  *      connected. Used by DP MST. This should be set by calling
907  *      drm_connector_set_path_property(), in the case of DP MST with the
908  *      path property the MST manager created. Userspace cannot change this
909  *      property.
910  * TILE:
911  *      Connector tile group property to indicate how a set of DRM connector
912  *      compose together into one logical screen. This is used by both high-res
913  *      external screens (often only using a single cable, but exposing multiple
914  *      DP MST sinks), or high-res integrated panels (like dual-link DSI) which
915  *      are not gen-locked. Note that for tiled panels which are genlocked, like
916  *      dual-link LVDS or dual-link DSI, the driver should try to not expose the
917  *      tiling and virtualize both &drm_crtc and &drm_plane if needed. Drivers
918  *      should update this value using drm_connector_set_tile_property().
919  *      Userspace cannot change this property.
920  * link-status:
921  *      Connector link-status property to indicate the status of link. The
922  *      default value of link-status is "GOOD". If something fails during or
923  *      after modeset, the kernel driver may set this to "BAD" and issue a
924  *      hotplug uevent. Drivers should update this value using
925  *      drm_connector_set_link_status_property().
926  * non_desktop:
927  *      Indicates the output should be ignored for purposes of displaying a
928  *      standard desktop environment or console. This is most likely because
929  *      the output device is not rectilinear.
930  * Content Protection:
931  *      This property is used by userspace to request the kernel protect future
932  *      content communicated over the link. When requested, kernel will apply
933  *      the appropriate means of protection (most often HDCP), and use the
934  *      property to tell userspace the protection is active.
935  *
936  *      Drivers can set this up by calling
937  *      drm_connector_attach_content_protection_property() on initialization.
938  *
939  *      The value of this property can be one of the following:
940  *
941  *      DRM_MODE_CONTENT_PROTECTION_UNDESIRED = 0
942  *              The link is not protected, content is transmitted in the clear.
943  *      DRM_MODE_CONTENT_PROTECTION_DESIRED = 1
944  *              Userspace has requested content protection, but the link is not
945  *              currently protected. When in this state, kernel should enable
946  *              Content Protection as soon as possible.
947  *      DRM_MODE_CONTENT_PROTECTION_ENABLED = 2
948  *              Userspace has requested content protection, and the link is
949  *              protected. Only the driver can set the property to this value.
950  *              If userspace attempts to set to ENABLED, kernel will return
951  *              -EINVAL.
952  *
953  *      A few guidelines:
954  *
955  *      - DESIRED state should be preserved until userspace de-asserts it by
956  *        setting the property to UNDESIRED. This means ENABLED should only
957  *        transition to UNDESIRED when the user explicitly requests it.
958  *      - If the state is DESIRED, kernel should attempt to re-authenticate the
959  *        link whenever possible. This includes across disable/enable, dpms,
960  *        hotplug, downstream device changes, link status failures, etc..
961  *      - Userspace is responsible for polling the property to determine when
962  *        the value transitions from ENABLED to DESIRED. This signifies the link
963  *        is no longer protected and userspace should take appropriate action
964  *        (whatever that might be).
965  *
966  * max bpc:
967  *      This range property is used by userspace to limit the bit depth. When
968  *      used the driver would limit the bpc in accordance with the valid range
969  *      supported by the hardware and sink. Drivers to use the function
970  *      drm_connector_attach_max_bpc_property() to create and attach the
971  *      property to the connector during initialization.
972  *
973  * Connectors also have one standardized atomic property:
974  *
975  * CRTC_ID:
976  *      Mode object ID of the &drm_crtc this connector should be connected to.
977  *
978  * Connectors for LCD panels may also have one standardized property:
979  *
980  * panel orientation:
981  *      On some devices the LCD panel is mounted in the casing in such a way
982  *      that the up/top side of the panel does not match with the top side of
983  *      the device. Userspace can use this property to check for this.
984  *      Note that input coordinates from touchscreens (input devices with
985  *      INPUT_PROP_DIRECT) will still map 1:1 to the actual LCD panel
986  *      coordinates, so if userspace rotates the picture to adjust for
987  *      the orientation it must also apply the same transformation to the
988  *      touchscreen input coordinates. This property is initialized by calling
989  *      drm_connector_init_panel_orientation_property().
990  *
991  * scaling mode:
992  *      This property defines how a non-native mode is upscaled to the native
993  *      mode of an LCD panel:
994  *
995  *      None:
996  *              No upscaling happens, scaling is left to the panel. Not all
997  *              drivers expose this mode.
998  *      Full:
999  *              The output is upscaled to the full resolution of the panel,
1000  *              ignoring the aspect ratio.
1001  *      Center:
1002  *              No upscaling happens, the output is centered within the native
1003  *              resolution the panel.
1004  *      Full aspect:
1005  *              The output is upscaled to maximize either the width or height
1006  *              while retaining the aspect ratio.
1007  *
1008  *      This property should be set up by calling
1009  *      drm_connector_attach_scaling_mode_property(). Note that drivers
1010  *      can also expose this property to external outputs, in which case they
1011  *      must support "None", which should be the default (since external screens
1012  *      have a built-in scaler).
1013  */
1014
1015 int drm_connector_create_standard_properties(struct drm_device *dev)
1016 {
1017         struct drm_property *prop;
1018
1019         prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1020                                    DRM_MODE_PROP_IMMUTABLE,
1021                                    "EDID", 0);
1022         if (!prop)
1023                 return -ENOMEM;
1024         dev->mode_config.edid_property = prop;
1025
1026         prop = drm_property_create_enum(dev, 0,
1027                                    "DPMS", drm_dpms_enum_list,
1028                                    ARRAY_SIZE(drm_dpms_enum_list));
1029         if (!prop)
1030                 return -ENOMEM;
1031         dev->mode_config.dpms_property = prop;
1032
1033         prop = drm_property_create(dev,
1034                                    DRM_MODE_PROP_BLOB |
1035                                    DRM_MODE_PROP_IMMUTABLE,
1036                                    "PATH", 0);
1037         if (!prop)
1038                 return -ENOMEM;
1039         dev->mode_config.path_property = prop;
1040
1041         prop = drm_property_create(dev,
1042                                    DRM_MODE_PROP_BLOB |
1043                                    DRM_MODE_PROP_IMMUTABLE,
1044                                    "TILE", 0);
1045         if (!prop)
1046                 return -ENOMEM;
1047         dev->mode_config.tile_property = prop;
1048
1049         prop = drm_property_create_enum(dev, 0, "link-status",
1050                                         drm_link_status_enum_list,
1051                                         ARRAY_SIZE(drm_link_status_enum_list));
1052         if (!prop)
1053                 return -ENOMEM;
1054         dev->mode_config.link_status_property = prop;
1055
1056         prop = drm_property_create_bool(dev, DRM_MODE_PROP_IMMUTABLE, "non-desktop");
1057         if (!prop)
1058                 return -ENOMEM;
1059         dev->mode_config.non_desktop_property = prop;
1060
1061         prop = drm_property_create(dev, DRM_MODE_PROP_BLOB,
1062                                    "HDR_OUTPUT_METADATA", 0);
1063         if (!prop)
1064                 return -ENOMEM;
1065         dev->mode_config.hdr_output_metadata_property = prop;
1066
1067         return 0;
1068 }
1069
1070 /**
1071  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1072  * @dev: DRM device
1073  *
1074  * Called by a driver the first time a DVI-I connector is made.
1075  */
1076 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1077 {
1078         struct drm_property *dvi_i_selector;
1079         struct drm_property *dvi_i_subconnector;
1080
1081         if (dev->mode_config.dvi_i_select_subconnector_property)
1082                 return 0;
1083
1084         dvi_i_selector =
1085                 drm_property_create_enum(dev, 0,
1086                                     "select subconnector",
1087                                     drm_dvi_i_select_enum_list,
1088                                     ARRAY_SIZE(drm_dvi_i_select_enum_list));
1089         dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1090
1091         dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1092                                     "subconnector",
1093                                     drm_dvi_i_subconnector_enum_list,
1094                                     ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1095         dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1096
1097         return 0;
1098 }
1099 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1100
1101 /**
1102  * DOC: HDMI connector properties
1103  *
1104  * content type (HDMI specific):
1105  *      Indicates content type setting to be used in HDMI infoframes to indicate
1106  *      content type for the external device, so that it adjusts its display
1107  *      settings accordingly.
1108  *
1109  *      The value of this property can be one of the following:
1110  *
1111  *      No Data:
1112  *              Content type is unknown
1113  *      Graphics:
1114  *              Content type is graphics
1115  *      Photo:
1116  *              Content type is photo
1117  *      Cinema:
1118  *              Content type is cinema
1119  *      Game:
1120  *              Content type is game
1121  *
1122  *      Drivers can set up this property by calling
1123  *      drm_connector_attach_content_type_property(). Decoding to
1124  *      infoframe values is done through drm_hdmi_avi_infoframe_content_type().
1125  */
1126
1127 /**
1128  * drm_connector_attach_content_type_property - attach content-type property
1129  * @connector: connector to attach content type property on.
1130  *
1131  * Called by a driver the first time a HDMI connector is made.
1132  */
1133 int drm_connector_attach_content_type_property(struct drm_connector *connector)
1134 {
1135         if (!drm_mode_create_content_type_property(connector->dev))
1136                 drm_object_attach_property(&connector->base,
1137                                            connector->dev->mode_config.content_type_property,
1138                                            DRM_MODE_CONTENT_TYPE_NO_DATA);
1139         return 0;
1140 }
1141 EXPORT_SYMBOL(drm_connector_attach_content_type_property);
1142
1143
1144 /**
1145  * drm_hdmi_avi_infoframe_content_type() - fill the HDMI AVI infoframe
1146  *                                         content type information, based
1147  *                                         on correspondent DRM property.
1148  * @frame: HDMI AVI infoframe
1149  * @conn_state: DRM display connector state
1150  *
1151  */
1152 void drm_hdmi_avi_infoframe_content_type(struct hdmi_avi_infoframe *frame,
1153                                          const struct drm_connector_state *conn_state)
1154 {
1155         switch (conn_state->content_type) {
1156         case DRM_MODE_CONTENT_TYPE_GRAPHICS:
1157                 frame->content_type = HDMI_CONTENT_TYPE_GRAPHICS;
1158                 break;
1159         case DRM_MODE_CONTENT_TYPE_CINEMA:
1160                 frame->content_type = HDMI_CONTENT_TYPE_CINEMA;
1161                 break;
1162         case DRM_MODE_CONTENT_TYPE_GAME:
1163                 frame->content_type = HDMI_CONTENT_TYPE_GAME;
1164                 break;
1165         case DRM_MODE_CONTENT_TYPE_PHOTO:
1166                 frame->content_type = HDMI_CONTENT_TYPE_PHOTO;
1167                 break;
1168         default:
1169                 /* Graphics is the default(0) */
1170                 frame->content_type = HDMI_CONTENT_TYPE_GRAPHICS;
1171         }
1172
1173         frame->itc = conn_state->content_type != DRM_MODE_CONTENT_TYPE_NO_DATA;
1174 }
1175 EXPORT_SYMBOL(drm_hdmi_avi_infoframe_content_type);
1176
1177 /**
1178  * drm_mode_attach_tv_margin_properties - attach TV connector margin properties
1179  * @connector: DRM connector
1180  *
1181  * Called by a driver when it needs to attach TV margin props to a connector.
1182  * Typically used on SDTV and HDMI connectors.
1183  */
1184 void drm_connector_attach_tv_margin_properties(struct drm_connector *connector)
1185 {
1186         struct drm_device *dev = connector->dev;
1187
1188         drm_object_attach_property(&connector->base,
1189                                    dev->mode_config.tv_left_margin_property,
1190                                    0);
1191         drm_object_attach_property(&connector->base,
1192                                    dev->mode_config.tv_right_margin_property,
1193                                    0);
1194         drm_object_attach_property(&connector->base,
1195                                    dev->mode_config.tv_top_margin_property,
1196                                    0);
1197         drm_object_attach_property(&connector->base,
1198                                    dev->mode_config.tv_bottom_margin_property,
1199                                    0);
1200 }
1201 EXPORT_SYMBOL(drm_connector_attach_tv_margin_properties);
1202
1203 /**
1204  * drm_mode_create_tv_margin_properties - create TV connector margin properties
1205  * @dev: DRM device
1206  *
1207  * Called by a driver's HDMI connector initialization routine, this function
1208  * creates the TV margin properties for a given device. No need to call this
1209  * function for an SDTV connector, it's already called from
1210  * drm_mode_create_tv_properties().
1211  */
1212 int drm_mode_create_tv_margin_properties(struct drm_device *dev)
1213 {
1214         if (dev->mode_config.tv_left_margin_property)
1215                 return 0;
1216
1217         dev->mode_config.tv_left_margin_property =
1218                 drm_property_create_range(dev, 0, "left margin", 0, 100);
1219         if (!dev->mode_config.tv_left_margin_property)
1220                 return -ENOMEM;
1221
1222         dev->mode_config.tv_right_margin_property =
1223                 drm_property_create_range(dev, 0, "right margin", 0, 100);
1224         if (!dev->mode_config.tv_right_margin_property)
1225                 return -ENOMEM;
1226
1227         dev->mode_config.tv_top_margin_property =
1228                 drm_property_create_range(dev, 0, "top margin", 0, 100);
1229         if (!dev->mode_config.tv_top_margin_property)
1230                 return -ENOMEM;
1231
1232         dev->mode_config.tv_bottom_margin_property =
1233                 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1234         if (!dev->mode_config.tv_bottom_margin_property)
1235                 return -ENOMEM;
1236
1237         return 0;
1238 }
1239 EXPORT_SYMBOL(drm_mode_create_tv_margin_properties);
1240
1241 /**
1242  * drm_mode_create_tv_properties - create TV specific connector properties
1243  * @dev: DRM device
1244  * @num_modes: number of different TV formats (modes) supported
1245  * @modes: array of pointers to strings containing name of each format
1246  *
1247  * Called by a driver's TV initialization routine, this function creates
1248  * the TV specific connector properties for a given device.  Caller is
1249  * responsible for allocating a list of format names and passing them to
1250  * this routine.
1251  */
1252 int drm_mode_create_tv_properties(struct drm_device *dev,
1253                                   unsigned int num_modes,
1254                                   const char * const modes[])
1255 {
1256         struct drm_property *tv_selector;
1257         struct drm_property *tv_subconnector;
1258         unsigned int i;
1259
1260         if (dev->mode_config.tv_select_subconnector_property)
1261                 return 0;
1262
1263         /*
1264          * Basic connector properties
1265          */
1266         tv_selector = drm_property_create_enum(dev, 0,
1267                                           "select subconnector",
1268                                           drm_tv_select_enum_list,
1269                                           ARRAY_SIZE(drm_tv_select_enum_list));
1270         if (!tv_selector)
1271                 goto nomem;
1272
1273         dev->mode_config.tv_select_subconnector_property = tv_selector;
1274
1275         tv_subconnector =
1276                 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1277                                     "subconnector",
1278                                     drm_tv_subconnector_enum_list,
1279                                     ARRAY_SIZE(drm_tv_subconnector_enum_list));
1280         if (!tv_subconnector)
1281                 goto nomem;
1282         dev->mode_config.tv_subconnector_property = tv_subconnector;
1283
1284         /*
1285          * Other, TV specific properties: margins & TV modes.
1286          */
1287         if (drm_mode_create_tv_margin_properties(dev))
1288                 goto nomem;
1289
1290         dev->mode_config.tv_mode_property =
1291                 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1292                                     "mode", num_modes);
1293         if (!dev->mode_config.tv_mode_property)
1294                 goto nomem;
1295
1296         for (i = 0; i < num_modes; i++)
1297                 drm_property_add_enum(dev->mode_config.tv_mode_property,
1298                                       i, modes[i]);
1299
1300         dev->mode_config.tv_brightness_property =
1301                 drm_property_create_range(dev, 0, "brightness", 0, 100);
1302         if (!dev->mode_config.tv_brightness_property)
1303                 goto nomem;
1304
1305         dev->mode_config.tv_contrast_property =
1306                 drm_property_create_range(dev, 0, "contrast", 0, 100);
1307         if (!dev->mode_config.tv_contrast_property)
1308                 goto nomem;
1309
1310         dev->mode_config.tv_flicker_reduction_property =
1311                 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1312         if (!dev->mode_config.tv_flicker_reduction_property)
1313                 goto nomem;
1314
1315         dev->mode_config.tv_overscan_property =
1316                 drm_property_create_range(dev, 0, "overscan", 0, 100);
1317         if (!dev->mode_config.tv_overscan_property)
1318                 goto nomem;
1319
1320         dev->mode_config.tv_saturation_property =
1321                 drm_property_create_range(dev, 0, "saturation", 0, 100);
1322         if (!dev->mode_config.tv_saturation_property)
1323                 goto nomem;
1324
1325         dev->mode_config.tv_hue_property =
1326                 drm_property_create_range(dev, 0, "hue", 0, 100);
1327         if (!dev->mode_config.tv_hue_property)
1328                 goto nomem;
1329
1330         return 0;
1331 nomem:
1332         return -ENOMEM;
1333 }
1334 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1335
1336 /**
1337  * drm_mode_create_scaling_mode_property - create scaling mode property
1338  * @dev: DRM device
1339  *
1340  * Called by a driver the first time it's needed, must be attached to desired
1341  * connectors.
1342  *
1343  * Atomic drivers should use drm_connector_attach_scaling_mode_property()
1344  * instead to correctly assign &drm_connector_state.picture_aspect_ratio
1345  * in the atomic state.
1346  */
1347 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1348 {
1349         struct drm_property *scaling_mode;
1350
1351         if (dev->mode_config.scaling_mode_property)
1352                 return 0;
1353
1354         scaling_mode =
1355                 drm_property_create_enum(dev, 0, "scaling mode",
1356                                 drm_scaling_mode_enum_list,
1357                                     ARRAY_SIZE(drm_scaling_mode_enum_list));
1358
1359         dev->mode_config.scaling_mode_property = scaling_mode;
1360
1361         return 0;
1362 }
1363 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1364
1365 /**
1366  * DOC: Variable refresh properties
1367  *
1368  * Variable refresh rate capable displays can dynamically adjust their
1369  * refresh rate by extending the duration of their vertical front porch
1370  * until page flip or timeout occurs. This can reduce or remove stuttering
1371  * and latency in scenarios where the page flip does not align with the
1372  * vblank interval.
1373  *
1374  * An example scenario would be an application flipping at a constant rate
1375  * of 48Hz on a 60Hz display. The page flip will frequently miss the vblank
1376  * interval and the same contents will be displayed twice. This can be
1377  * observed as stuttering for content with motion.
1378  *
1379  * If variable refresh rate was active on a display that supported a
1380  * variable refresh range from 35Hz to 60Hz no stuttering would be observable
1381  * for the example scenario. The minimum supported variable refresh rate of
1382  * 35Hz is below the page flip frequency and the vertical front porch can
1383  * be extended until the page flip occurs. The vblank interval will be
1384  * directly aligned to the page flip rate.
1385  *
1386  * Not all userspace content is suitable for use with variable refresh rate.
1387  * Large and frequent changes in vertical front porch duration may worsen
1388  * perceived stuttering for input sensitive applications.
1389  *
1390  * Panel brightness will also vary with vertical front porch duration. Some
1391  * panels may have noticeable differences in brightness between the minimum
1392  * vertical front porch duration and the maximum vertical front porch duration.
1393  * Large and frequent changes in vertical front porch duration may produce
1394  * observable flickering for such panels.
1395  *
1396  * Userspace control for variable refresh rate is supported via properties
1397  * on the &drm_connector and &drm_crtc objects.
1398  *
1399  * "vrr_capable":
1400  *      Optional &drm_connector boolean property that drivers should attach
1401  *      with drm_connector_attach_vrr_capable_property() on connectors that
1402  *      could support variable refresh rates. Drivers should update the
1403  *      property value by calling drm_connector_set_vrr_capable_property().
1404  *
1405  *      Absence of the property should indicate absence of support.
1406  *
1407  * "VRR_ENABLED":
1408  *      Default &drm_crtc boolean property that notifies the driver that the
1409  *      content on the CRTC is suitable for variable refresh rate presentation.
1410  *      The driver will take this property as a hint to enable variable
1411  *      refresh rate support if the receiver supports it, ie. if the
1412  *      "vrr_capable" property is true on the &drm_connector object. The
1413  *      vertical front porch duration will be extended until page-flip or
1414  *      timeout when enabled.
1415  *
1416  *      The minimum vertical front porch duration is defined as the vertical
1417  *      front porch duration for the current mode.
1418  *
1419  *      The maximum vertical front porch duration is greater than or equal to
1420  *      the minimum vertical front porch duration. The duration is derived
1421  *      from the minimum supported variable refresh rate for the connector.
1422  *
1423  *      The driver may place further restrictions within these minimum
1424  *      and maximum bounds.
1425  */
1426
1427 /**
1428  * drm_connector_attach_vrr_capable_property - creates the
1429  * vrr_capable property
1430  * @connector: connector to create the vrr_capable property on.
1431  *
1432  * This is used by atomic drivers to add support for querying
1433  * variable refresh rate capability for a connector.
1434  *
1435  * Returns:
1436  * Zero on success, negative errono on failure.
1437  */
1438 int drm_connector_attach_vrr_capable_property(
1439         struct drm_connector *connector)
1440 {
1441         struct drm_device *dev = connector->dev;
1442         struct drm_property *prop;
1443
1444         if (!connector->vrr_capable_property) {
1445                 prop = drm_property_create_bool(dev, DRM_MODE_PROP_IMMUTABLE,
1446                         "vrr_capable");
1447                 if (!prop)
1448                         return -ENOMEM;
1449
1450                 connector->vrr_capable_property = prop;
1451                 drm_object_attach_property(&connector->base, prop, 0);
1452         }
1453
1454         return 0;
1455 }
1456 EXPORT_SYMBOL(drm_connector_attach_vrr_capable_property);
1457
1458 /**
1459  * drm_connector_attach_scaling_mode_property - attach atomic scaling mode property
1460  * @connector: connector to attach scaling mode property on.
1461  * @scaling_mode_mask: or'ed mask of BIT(%DRM_MODE_SCALE_\*).
1462  *
1463  * This is used to add support for scaling mode to atomic drivers.
1464  * The scaling mode will be set to &drm_connector_state.picture_aspect_ratio
1465  * and can be used from &drm_connector_helper_funcs->atomic_check for validation.
1466  *
1467  * This is the atomic version of drm_mode_create_scaling_mode_property().
1468  *
1469  * Returns:
1470  * Zero on success, negative errno on failure.
1471  */
1472 int drm_connector_attach_scaling_mode_property(struct drm_connector *connector,
1473                                                u32 scaling_mode_mask)
1474 {
1475         struct drm_device *dev = connector->dev;
1476         struct drm_property *scaling_mode_property;
1477         int i;
1478         const unsigned valid_scaling_mode_mask =
1479                 (1U << ARRAY_SIZE(drm_scaling_mode_enum_list)) - 1;
1480
1481         if (WARN_ON(hweight32(scaling_mode_mask) < 2 ||
1482                     scaling_mode_mask & ~valid_scaling_mode_mask))
1483                 return -EINVAL;
1484
1485         scaling_mode_property =
1486                 drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
1487                                     hweight32(scaling_mode_mask));
1488
1489         if (!scaling_mode_property)
1490                 return -ENOMEM;
1491
1492         for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++) {
1493                 int ret;
1494
1495                 if (!(BIT(i) & scaling_mode_mask))
1496                         continue;
1497
1498                 ret = drm_property_add_enum(scaling_mode_property,
1499                                             drm_scaling_mode_enum_list[i].type,
1500                                             drm_scaling_mode_enum_list[i].name);
1501
1502                 if (ret) {
1503                         drm_property_destroy(dev, scaling_mode_property);
1504
1505                         return ret;
1506                 }
1507         }
1508
1509         drm_object_attach_property(&connector->base,
1510                                    scaling_mode_property, 0);
1511
1512         connector->scaling_mode_property = scaling_mode_property;
1513
1514         return 0;
1515 }
1516 EXPORT_SYMBOL(drm_connector_attach_scaling_mode_property);
1517
1518 /**
1519  * drm_connector_attach_content_protection_property - attach content protection
1520  * property
1521  *
1522  * @connector: connector to attach CP property on.
1523  *
1524  * This is used to add support for content protection on select connectors.
1525  * Content Protection is intentionally vague to allow for different underlying
1526  * technologies, however it is most implemented by HDCP.
1527  *
1528  * The content protection will be set to &drm_connector_state.content_protection
1529  *
1530  * Returns:
1531  * Zero on success, negative errno on failure.
1532  */
1533 int drm_connector_attach_content_protection_property(
1534                 struct drm_connector *connector)
1535 {
1536         struct drm_device *dev = connector->dev;
1537         struct drm_property *prop;
1538
1539         prop = drm_property_create_enum(dev, 0, "Content Protection",
1540                                         drm_cp_enum_list,
1541                                         ARRAY_SIZE(drm_cp_enum_list));
1542         if (!prop)
1543                 return -ENOMEM;
1544
1545         drm_object_attach_property(&connector->base, prop,
1546                                    DRM_MODE_CONTENT_PROTECTION_UNDESIRED);
1547
1548         connector->content_protection_property = prop;
1549
1550         return 0;
1551 }
1552 EXPORT_SYMBOL(drm_connector_attach_content_protection_property);
1553
1554 /**
1555  * drm_mode_create_aspect_ratio_property - create aspect ratio property
1556  * @dev: DRM device
1557  *
1558  * Called by a driver the first time it's needed, must be attached to desired
1559  * connectors.
1560  *
1561  * Returns:
1562  * Zero on success, negative errno on failure.
1563  */
1564 int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1565 {
1566         if (dev->mode_config.aspect_ratio_property)
1567                 return 0;
1568
1569         dev->mode_config.aspect_ratio_property =
1570                 drm_property_create_enum(dev, 0, "aspect ratio",
1571                                 drm_aspect_ratio_enum_list,
1572                                 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1573
1574         if (dev->mode_config.aspect_ratio_property == NULL)
1575                 return -ENOMEM;
1576
1577         return 0;
1578 }
1579 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1580
1581 /**
1582  * DOC: standard connector properties
1583  *
1584  * Colorspace:
1585  *     drm_mode_create_colorspace_property - create colorspace property
1586  *     This property helps select a suitable colorspace based on the sink
1587  *     capability. Modern sink devices support wider gamut like BT2020.
1588  *     This helps switch to BT2020 mode if the BT2020 encoded video stream
1589  *     is being played by the user, same for any other colorspace. Thereby
1590  *     giving a good visual experience to users.
1591  *
1592  *     The expectation from userspace is that it should parse the EDID
1593  *     and get supported colorspaces. Use this property and switch to the
1594  *     one supported. Sink supported colorspaces should be retrieved by
1595  *     userspace from EDID and driver will not explicitly expose them.
1596  *
1597  *     Basically the expectation from userspace is:
1598  *      - Set up CRTC DEGAMMA/CTM/GAMMA to convert to some sink
1599  *        colorspace
1600  *      - Set this new property to let the sink know what it
1601  *        converted the CRTC output to.
1602  *      - This property is just to inform sink what colorspace
1603  *        source is trying to drive.
1604  *
1605  * Called by a driver the first time it's needed, must be attached to desired
1606  * connectors.
1607  */
1608 int drm_mode_create_colorspace_property(struct drm_connector *connector)
1609 {
1610         struct drm_device *dev = connector->dev;
1611         struct drm_property *prop;
1612
1613         if (connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
1614             connector->connector_type == DRM_MODE_CONNECTOR_HDMIB) {
1615                 prop = drm_property_create_enum(dev, DRM_MODE_PROP_ENUM,
1616                                                 "Colorspace",
1617                                                 hdmi_colorspaces,
1618                                                 ARRAY_SIZE(hdmi_colorspaces));
1619                 if (!prop)
1620                         return -ENOMEM;
1621         } else {
1622                 DRM_DEBUG_KMS("Colorspace property not supported\n");
1623                 return 0;
1624         }
1625
1626         connector->colorspace_property = prop;
1627
1628         return 0;
1629 }
1630 EXPORT_SYMBOL(drm_mode_create_colorspace_property);
1631
1632 /**
1633  * drm_mode_create_content_type_property - create content type property
1634  * @dev: DRM device
1635  *
1636  * Called by a driver the first time it's needed, must be attached to desired
1637  * connectors.
1638  *
1639  * Returns:
1640  * Zero on success, negative errno on failure.
1641  */
1642 int drm_mode_create_content_type_property(struct drm_device *dev)
1643 {
1644         if (dev->mode_config.content_type_property)
1645                 return 0;
1646
1647         dev->mode_config.content_type_property =
1648                 drm_property_create_enum(dev, 0, "content type",
1649                                          drm_content_type_enum_list,
1650                                          ARRAY_SIZE(drm_content_type_enum_list));
1651
1652         if (dev->mode_config.content_type_property == NULL)
1653                 return -ENOMEM;
1654
1655         return 0;
1656 }
1657 EXPORT_SYMBOL(drm_mode_create_content_type_property);
1658
1659 /**
1660  * drm_mode_create_suggested_offset_properties - create suggests offset properties
1661  * @dev: DRM device
1662  *
1663  * Create the the suggested x/y offset property for connectors.
1664  */
1665 int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1666 {
1667         if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1668                 return 0;
1669
1670         dev->mode_config.suggested_x_property =
1671                 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1672
1673         dev->mode_config.suggested_y_property =
1674                 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1675
1676         if (dev->mode_config.suggested_x_property == NULL ||
1677             dev->mode_config.suggested_y_property == NULL)
1678                 return -ENOMEM;
1679         return 0;
1680 }
1681 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1682
1683 /**
1684  * drm_connector_set_path_property - set tile property on connector
1685  * @connector: connector to set property on.
1686  * @path: path to use for property; must not be NULL.
1687  *
1688  * This creates a property to expose to userspace to specify a
1689  * connector path. This is mainly used for DisplayPort MST where
1690  * connectors have a topology and we want to allow userspace to give
1691  * them more meaningful names.
1692  *
1693  * Returns:
1694  * Zero on success, negative errno on failure.
1695  */
1696 int drm_connector_set_path_property(struct drm_connector *connector,
1697                                     const char *path)
1698 {
1699         struct drm_device *dev = connector->dev;
1700         int ret;
1701
1702         ret = drm_property_replace_global_blob(dev,
1703                                                &connector->path_blob_ptr,
1704                                                strlen(path) + 1,
1705                                                path,
1706                                                &connector->base,
1707                                                dev->mode_config.path_property);
1708         return ret;
1709 }
1710 EXPORT_SYMBOL(drm_connector_set_path_property);
1711
1712 /**
1713  * drm_connector_set_tile_property - set tile property on connector
1714  * @connector: connector to set property on.
1715  *
1716  * This looks up the tile information for a connector, and creates a
1717  * property for userspace to parse if it exists. The property is of
1718  * the form of 8 integers using ':' as a separator.
1719  * This is used for dual port tiled displays with DisplayPort SST
1720  * or DisplayPort MST connectors.
1721  *
1722  * Returns:
1723  * Zero on success, errno on failure.
1724  */
1725 int drm_connector_set_tile_property(struct drm_connector *connector)
1726 {
1727         struct drm_device *dev = connector->dev;
1728         char tile[256];
1729         int ret;
1730
1731         if (!connector->has_tile) {
1732                 ret  = drm_property_replace_global_blob(dev,
1733                                                         &connector->tile_blob_ptr,
1734                                                         0,
1735                                                         NULL,
1736                                                         &connector->base,
1737                                                         dev->mode_config.tile_property);
1738                 return ret;
1739         }
1740
1741         snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
1742                  connector->tile_group->id, connector->tile_is_single_monitor,
1743                  connector->num_h_tile, connector->num_v_tile,
1744                  connector->tile_h_loc, connector->tile_v_loc,
1745                  connector->tile_h_size, connector->tile_v_size);
1746
1747         ret = drm_property_replace_global_blob(dev,
1748                                                &connector->tile_blob_ptr,
1749                                                strlen(tile) + 1,
1750                                                tile,
1751                                                &connector->base,
1752                                                dev->mode_config.tile_property);
1753         return ret;
1754 }
1755 EXPORT_SYMBOL(drm_connector_set_tile_property);
1756
1757 /**
1758  * drm_connector_update_edid_property - update the edid property of a connector
1759  * @connector: drm connector
1760  * @edid: new value of the edid property
1761  *
1762  * This function creates a new blob modeset object and assigns its id to the
1763  * connector's edid property.
1764  * Since we also parse tile information from EDID's displayID block, we also
1765  * set the connector's tile property here. See drm_connector_set_tile_property()
1766  * for more details.
1767  *
1768  * Returns:
1769  * Zero on success, negative errno on failure.
1770  */
1771 int drm_connector_update_edid_property(struct drm_connector *connector,
1772                                        const struct edid *edid)
1773 {
1774         struct drm_device *dev = connector->dev;
1775         size_t size = 0;
1776         int ret;
1777
1778         /* ignore requests to set edid when overridden */
1779         if (connector->override_edid)
1780                 return 0;
1781
1782         if (edid)
1783                 size = EDID_LENGTH * (1 + edid->extensions);
1784
1785         /* Set the display info, using edid if available, otherwise
1786          * reseting the values to defaults. This duplicates the work
1787          * done in drm_add_edid_modes, but that function is not
1788          * consistently called before this one in all drivers and the
1789          * computation is cheap enough that it seems better to
1790          * duplicate it rather than attempt to ensure some arbitrary
1791          * ordering of calls.
1792          */
1793         if (edid)
1794                 drm_add_display_info(connector, edid);
1795         else
1796                 drm_reset_display_info(connector);
1797
1798         drm_object_property_set_value(&connector->base,
1799                                       dev->mode_config.non_desktop_property,
1800                                       connector->display_info.non_desktop);
1801
1802         ret = drm_property_replace_global_blob(dev,
1803                                                &connector->edid_blob_ptr,
1804                                                size,
1805                                                edid,
1806                                                &connector->base,
1807                                                dev->mode_config.edid_property);
1808         if (ret)
1809                 return ret;
1810         return drm_connector_set_tile_property(connector);
1811 }
1812 EXPORT_SYMBOL(drm_connector_update_edid_property);
1813
1814 /**
1815  * drm_connector_set_link_status_property - Set link status property of a connector
1816  * @connector: drm connector
1817  * @link_status: new value of link status property (0: Good, 1: Bad)
1818  *
1819  * In usual working scenario, this link status property will always be set to
1820  * "GOOD". If something fails during or after a mode set, the kernel driver
1821  * may set this link status property to "BAD". The caller then needs to send a
1822  * hotplug uevent for userspace to re-check the valid modes through
1823  * GET_CONNECTOR_IOCTL and retry modeset.
1824  *
1825  * Note: Drivers cannot rely on userspace to support this property and
1826  * issue a modeset. As such, they may choose to handle issues (like
1827  * re-training a link) without userspace's intervention.
1828  *
1829  * The reason for adding this property is to handle link training failures, but
1830  * it is not limited to DP or link training. For example, if we implement
1831  * asynchronous setcrtc, this property can be used to report any failures in that.
1832  */
1833 void drm_connector_set_link_status_property(struct drm_connector *connector,
1834                                             uint64_t link_status)
1835 {
1836         struct drm_device *dev = connector->dev;
1837
1838         drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1839         connector->state->link_status = link_status;
1840         drm_modeset_unlock(&dev->mode_config.connection_mutex);
1841 }
1842 EXPORT_SYMBOL(drm_connector_set_link_status_property);
1843
1844 /**
1845  * drm_connector_attach_max_bpc_property - attach "max bpc" property
1846  * @connector: connector to attach max bpc property on.
1847  * @min: The minimum bit depth supported by the connector.
1848  * @max: The maximum bit depth supported by the connector.
1849  *
1850  * This is used to add support for limiting the bit depth on a connector.
1851  *
1852  * Returns:
1853  * Zero on success, negative errno on failure.
1854  */
1855 int drm_connector_attach_max_bpc_property(struct drm_connector *connector,
1856                                           int min, int max)
1857 {
1858         struct drm_device *dev = connector->dev;
1859         struct drm_property *prop;
1860
1861         prop = connector->max_bpc_property;
1862         if (!prop) {
1863                 prop = drm_property_create_range(dev, 0, "max bpc", min, max);
1864                 if (!prop)
1865                         return -ENOMEM;
1866
1867                 connector->max_bpc_property = prop;
1868         }
1869
1870         drm_object_attach_property(&connector->base, prop, max);
1871         connector->state->max_requested_bpc = max;
1872         connector->state->max_bpc = max;
1873
1874         return 0;
1875 }
1876 EXPORT_SYMBOL(drm_connector_attach_max_bpc_property);
1877
1878 /**
1879  * drm_connector_set_vrr_capable_property - sets the variable refresh rate
1880  * capable property for a connector
1881  * @connector: drm connector
1882  * @capable: True if the connector is variable refresh rate capable
1883  *
1884  * Should be used by atomic drivers to update the indicated support for
1885  * variable refresh rate over a connector.
1886  */
1887 void drm_connector_set_vrr_capable_property(
1888                 struct drm_connector *connector, bool capable)
1889 {
1890         drm_object_property_set_value(&connector->base,
1891                                       connector->vrr_capable_property,
1892                                       capable);
1893 }
1894 EXPORT_SYMBOL(drm_connector_set_vrr_capable_property);
1895
1896 /**
1897  * drm_connector_init_panel_orientation_property -
1898  *      initialize the connecters panel_orientation property
1899  * @connector: connector for which to init the panel-orientation property.
1900  * @width: width in pixels of the panel, used for panel quirk detection
1901  * @height: height in pixels of the panel, used for panel quirk detection
1902  *
1903  * This function should only be called for built-in panels, after setting
1904  * connector->display_info.panel_orientation first (if known).
1905  *
1906  * This function will check for platform specific (e.g. DMI based) quirks
1907  * overriding display_info.panel_orientation first, then if panel_orientation
1908  * is not DRM_MODE_PANEL_ORIENTATION_UNKNOWN it will attach the
1909  * "panel orientation" property to the connector.
1910  *
1911  * Returns:
1912  * Zero on success, negative errno on failure.
1913  */
1914 int drm_connector_init_panel_orientation_property(
1915         struct drm_connector *connector, int width, int height)
1916 {
1917         struct drm_device *dev = connector->dev;
1918         struct drm_display_info *info = &connector->display_info;
1919         struct drm_property *prop;
1920         int orientation_quirk;
1921
1922         orientation_quirk = drm_get_panel_orientation_quirk(width, height);
1923         if (orientation_quirk != DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
1924                 info->panel_orientation = orientation_quirk;
1925
1926         if (info->panel_orientation == DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
1927                 return 0;
1928
1929         prop = dev->mode_config.panel_orientation_property;
1930         if (!prop) {
1931                 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1932                                 "panel orientation",
1933                                 drm_panel_orientation_enum_list,
1934                                 ARRAY_SIZE(drm_panel_orientation_enum_list));
1935                 if (!prop)
1936                         return -ENOMEM;
1937
1938                 dev->mode_config.panel_orientation_property = prop;
1939         }
1940
1941         drm_object_attach_property(&connector->base, prop,
1942                                    info->panel_orientation);
1943         return 0;
1944 }
1945 EXPORT_SYMBOL(drm_connector_init_panel_orientation_property);
1946
1947 int drm_connector_set_obj_prop(struct drm_mode_object *obj,
1948                                     struct drm_property *property,
1949                                     uint64_t value)
1950 {
1951         int ret = -EINVAL;
1952         struct drm_connector *connector = obj_to_connector(obj);
1953
1954         /* Do DPMS ourselves */
1955         if (property == connector->dev->mode_config.dpms_property) {
1956                 ret = (*connector->funcs->dpms)(connector, (int)value);
1957         } else if (connector->funcs->set_property)
1958                 ret = connector->funcs->set_property(connector, property, value);
1959
1960         if (!ret)
1961                 drm_object_property_set_value(&connector->base, property, value);
1962         return ret;
1963 }
1964
1965 int drm_connector_property_set_ioctl(struct drm_device *dev,
1966                                      void *data, struct drm_file *file_priv)
1967 {
1968         struct drm_mode_connector_set_property *conn_set_prop = data;
1969         struct drm_mode_obj_set_property obj_set_prop = {
1970                 .value = conn_set_prop->value,
1971                 .prop_id = conn_set_prop->prop_id,
1972                 .obj_id = conn_set_prop->connector_id,
1973                 .obj_type = DRM_MODE_OBJECT_CONNECTOR
1974         };
1975
1976         /* It does all the locking and checking we need */
1977         return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
1978 }
1979
1980 static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
1981 {
1982         /* For atomic drivers only state objects are synchronously updated and
1983          * protected by modeset locks, so check those first. */
1984         if (connector->state)
1985                 return connector->state->best_encoder;
1986         return connector->encoder;
1987 }
1988
1989 static bool
1990 drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1991                              const struct list_head *export_list,
1992                              const struct drm_file *file_priv)
1993 {
1994         /*
1995          * If user-space hasn't configured the driver to expose the stereo 3D
1996          * modes, don't expose them.
1997          */
1998         if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1999                 return false;
2000         /*
2001          * If user-space hasn't configured the driver to expose the modes
2002          * with aspect-ratio, don't expose them. However if such a mode
2003          * is unique, let it be exposed, but reset the aspect-ratio flags
2004          * while preparing the list of user-modes.
2005          */
2006         if (!file_priv->aspect_ratio_allowed) {
2007                 struct drm_display_mode *mode_itr;
2008
2009                 list_for_each_entry(mode_itr, export_list, export_head)
2010                         if (drm_mode_match(mode_itr, mode,
2011                                            DRM_MODE_MATCH_TIMINGS |
2012                                            DRM_MODE_MATCH_CLOCK |
2013                                            DRM_MODE_MATCH_FLAGS |
2014                                            DRM_MODE_MATCH_3D_FLAGS))
2015                                 return false;
2016         }
2017
2018         return true;
2019 }
2020
2021 int drm_mode_getconnector(struct drm_device *dev, void *data,
2022                           struct drm_file *file_priv)
2023 {
2024         struct drm_mode_get_connector *out_resp = data;
2025         struct drm_connector *connector;
2026         struct drm_encoder *encoder;
2027         struct drm_display_mode *mode;
2028         int mode_count = 0;
2029         int encoders_count = 0;
2030         int ret = 0;
2031         int copied = 0;
2032         int i;
2033         struct drm_mode_modeinfo u_mode;
2034         struct drm_mode_modeinfo __user *mode_ptr;
2035         uint32_t __user *encoder_ptr;
2036         LIST_HEAD(export_list);
2037
2038         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2039                 return -EOPNOTSUPP;
2040
2041         memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2042
2043         connector = drm_connector_lookup(dev, file_priv, out_resp->connector_id);
2044         if (!connector)
2045                 return -ENOENT;
2046
2047         drm_connector_for_each_possible_encoder(connector, encoder, i)
2048                 encoders_count++;
2049
2050         if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2051                 copied = 0;
2052                 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
2053
2054                 drm_connector_for_each_possible_encoder(connector, encoder, i) {
2055                         if (put_user(encoder->base.id, encoder_ptr + copied)) {
2056                                 ret = -EFAULT;
2057                                 goto out;
2058                         }
2059                         copied++;
2060                 }
2061         }
2062         out_resp->count_encoders = encoders_count;
2063
2064         out_resp->connector_id = connector->base.id;
2065         out_resp->connector_type = connector->connector_type;
2066         out_resp->connector_type_id = connector->connector_type_id;
2067
2068         mutex_lock(&dev->mode_config.mutex);
2069         if (out_resp->count_modes == 0) {
2070                 connector->funcs->fill_modes(connector,
2071                                              dev->mode_config.max_width,
2072                                              dev->mode_config.max_height);
2073         }
2074
2075         out_resp->mm_width = connector->display_info.width_mm;
2076         out_resp->mm_height = connector->display_info.height_mm;
2077         out_resp->subpixel = connector->display_info.subpixel_order;
2078         out_resp->connection = connector->status;
2079
2080         /* delayed so we get modes regardless of pre-fill_modes state */
2081         list_for_each_entry(mode, &connector->modes, head)
2082                 if (drm_mode_expose_to_userspace(mode, &export_list,
2083                                                  file_priv)) {
2084                         list_add_tail(&mode->export_head, &export_list);
2085                         mode_count++;
2086                 }
2087
2088         /*
2089          * This ioctl is called twice, once to determine how much space is
2090          * needed, and the 2nd time to fill it.
2091          * The modes that need to be exposed to the user are maintained in the
2092          * 'export_list'. When the ioctl is called first time to determine the,
2093          * space, the export_list gets filled, to find the no.of modes. In the
2094          * 2nd time, the user modes are filled, one by one from the export_list.
2095          */
2096         if ((out_resp->count_modes >= mode_count) && mode_count) {
2097                 copied = 0;
2098                 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
2099                 list_for_each_entry(mode, &export_list, export_head) {
2100                         drm_mode_convert_to_umode(&u_mode, mode);
2101                         /*
2102                          * Reset aspect ratio flags of user-mode, if modes with
2103                          * aspect-ratio are not supported.
2104                          */
2105                         if (!file_priv->aspect_ratio_allowed)
2106                                 u_mode.flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
2107                         if (copy_to_user(mode_ptr + copied,
2108                                          &u_mode, sizeof(u_mode))) {
2109                                 ret = -EFAULT;
2110                                 mutex_unlock(&dev->mode_config.mutex);
2111
2112                                 goto out;
2113                         }
2114                         copied++;
2115                 }
2116         }
2117         out_resp->count_modes = mode_count;
2118         mutex_unlock(&dev->mode_config.mutex);
2119
2120         drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2121         encoder = drm_connector_get_encoder(connector);
2122         if (encoder)
2123                 out_resp->encoder_id = encoder->base.id;
2124         else
2125                 out_resp->encoder_id = 0;
2126
2127         /* Only grab properties after probing, to make sure EDID and other
2128          * properties reflect the latest status. */
2129         ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
2130                         (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2131                         (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2132                         &out_resp->count_props);
2133         drm_modeset_unlock(&dev->mode_config.connection_mutex);
2134
2135 out:
2136         drm_connector_put(connector);
2137
2138         return ret;
2139 }
2140
2141
2142 /**
2143  * DOC: Tile group
2144  *
2145  * Tile groups are used to represent tiled monitors with a unique integer
2146  * identifier. Tiled monitors using DisplayID v1.3 have a unique 8-byte handle,
2147  * we store this in a tile group, so we have a common identifier for all tiles
2148  * in a monitor group. The property is called "TILE". Drivers can manage tile
2149  * groups using drm_mode_create_tile_group(), drm_mode_put_tile_group() and
2150  * drm_mode_get_tile_group(). But this is only needed for internal panels where
2151  * the tile group information is exposed through a non-standard way.
2152  */
2153
2154 static void drm_tile_group_free(struct kref *kref)
2155 {
2156         struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
2157         struct drm_device *dev = tg->dev;
2158         mutex_lock(&dev->mode_config.idr_mutex);
2159         idr_remove(&dev->mode_config.tile_idr, tg->id);
2160         mutex_unlock(&dev->mode_config.idr_mutex);
2161         kfree(tg);
2162 }
2163
2164 /**
2165  * drm_mode_put_tile_group - drop a reference to a tile group.
2166  * @dev: DRM device
2167  * @tg: tile group to drop reference to.
2168  *
2169  * drop reference to tile group and free if 0.
2170  */
2171 void drm_mode_put_tile_group(struct drm_device *dev,
2172                              struct drm_tile_group *tg)
2173 {
2174         kref_put(&tg->refcount, drm_tile_group_free);
2175 }
2176 EXPORT_SYMBOL(drm_mode_put_tile_group);
2177
2178 /**
2179  * drm_mode_get_tile_group - get a reference to an existing tile group
2180  * @dev: DRM device
2181  * @topology: 8-bytes unique per monitor.
2182  *
2183  * Use the unique bytes to get a reference to an existing tile group.
2184  *
2185  * RETURNS:
2186  * tile group or NULL if not found.
2187  */
2188 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
2189                                                char topology[8])
2190 {
2191         struct drm_tile_group *tg;
2192         int id;
2193         mutex_lock(&dev->mode_config.idr_mutex);
2194         idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
2195                 if (!memcmp(tg->group_data, topology, 8)) {
2196                         if (!kref_get_unless_zero(&tg->refcount))
2197                                 tg = NULL;
2198                         mutex_unlock(&dev->mode_config.idr_mutex);
2199                         return tg;
2200                 }
2201         }
2202         mutex_unlock(&dev->mode_config.idr_mutex);
2203         return NULL;
2204 }
2205 EXPORT_SYMBOL(drm_mode_get_tile_group);
2206
2207 /**
2208  * drm_mode_create_tile_group - create a tile group from a displayid description
2209  * @dev: DRM device
2210  * @topology: 8-bytes unique per monitor.
2211  *
2212  * Create a tile group for the unique monitor, and get a unique
2213  * identifier for the tile group.
2214  *
2215  * RETURNS:
2216  * new tile group or NULL.
2217  */
2218 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
2219                                                   char topology[8])
2220 {
2221         struct drm_tile_group *tg;
2222         int ret;
2223
2224         tg = kzalloc(sizeof(*tg), GFP_KERNEL);
2225         if (!tg)
2226                 return NULL;
2227
2228         kref_init(&tg->refcount);
2229         memcpy(tg->group_data, topology, 8);
2230         tg->dev = dev;
2231
2232         mutex_lock(&dev->mode_config.idr_mutex);
2233         ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
2234         if (ret >= 0) {
2235                 tg->id = ret;
2236         } else {
2237                 kfree(tg);
2238                 tg = NULL;
2239         }
2240
2241         mutex_unlock(&dev->mode_config.idr_mutex);
2242         return tg;
2243 }
2244 EXPORT_SYMBOL(drm_mode_create_tile_group);