Merge tag 'vfs-6.7.misc' of gitolite.kernel.org:pub/scm/linux/kernel/git/vfs/vfs
[linux-block.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_auth.h>
24 #include <drm/drm_connector.h>
25 #include <drm/drm_drv.h>
26 #include <drm/drm_edid.h>
27 #include <drm/drm_encoder.h>
28 #include <drm/drm_file.h>
29 #include <drm/drm_managed.h>
30 #include <drm/drm_panel.h>
31 #include <drm/drm_print.h>
32 #include <drm/drm_privacy_screen_consumer.h>
33 #include <drm/drm_sysfs.h>
34 #include <drm/drm_utils.h>
35
36 #include <linux/property.h>
37 #include <linux/uaccess.h>
38
39 #include <video/cmdline.h>
40
41 #include "drm_crtc_internal.h"
42 #include "drm_internal.h"
43
44 /**
45  * DOC: overview
46  *
47  * In DRM connectors are the general abstraction for display sinks, and include
48  * also fixed panels or anything else that can display pixels in some form. As
49  * opposed to all other KMS objects representing hardware (like CRTC, encoder or
50  * plane abstractions) connectors can be hotplugged and unplugged at runtime.
51  * Hence they are reference-counted using drm_connector_get() and
52  * drm_connector_put().
53  *
54  * KMS driver must create, initialize, register and attach at a &struct
55  * drm_connector for each such sink. The instance is created as other KMS
56  * objects and initialized by setting the following fields. The connector is
57  * initialized with a call to drm_connector_init() with a pointer to the
58  * &struct drm_connector_funcs and a connector type, and then exposed to
59  * userspace with a call to drm_connector_register().
60  *
61  * Connectors must be attached to an encoder to be used. For devices that map
62  * connectors to encoders 1:1, the connector should be attached at
63  * initialization time with a call to drm_connector_attach_encoder(). The
64  * driver must also set the &drm_connector.encoder field to point to the
65  * attached encoder.
66  *
67  * For connectors which are not fixed (like built-in panels) the driver needs to
68  * support hotplug notifications. The simplest way to do that is by using the
69  * probe helpers, see drm_kms_helper_poll_init() for connectors which don't have
70  * hardware support for hotplug interrupts. Connectors with hardware hotplug
71  * support can instead use e.g. drm_helper_hpd_irq_event().
72  */
73
74 /*
75  * Global connector list for drm_connector_find_by_fwnode().
76  * Note drm_connector_[un]register() first take connector->lock and then
77  * take the connector_list_lock.
78  */
79 static DEFINE_MUTEX(connector_list_lock);
80 static LIST_HEAD(connector_list);
81
82 struct drm_conn_prop_enum_list {
83         int type;
84         const char *name;
85         struct ida ida;
86 };
87
88 /*
89  * Connector and encoder types.
90  */
91 static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
92         { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
93         { DRM_MODE_CONNECTOR_VGA, "VGA" },
94         { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
95         { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
96         { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
97         { DRM_MODE_CONNECTOR_Composite, "Composite" },
98         { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
99         { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
100         { DRM_MODE_CONNECTOR_Component, "Component" },
101         { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
102         { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
103         { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
104         { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
105         { DRM_MODE_CONNECTOR_TV, "TV" },
106         { DRM_MODE_CONNECTOR_eDP, "eDP" },
107         { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
108         { DRM_MODE_CONNECTOR_DSI, "DSI" },
109         { DRM_MODE_CONNECTOR_DPI, "DPI" },
110         { DRM_MODE_CONNECTOR_WRITEBACK, "Writeback" },
111         { DRM_MODE_CONNECTOR_SPI, "SPI" },
112         { DRM_MODE_CONNECTOR_USB, "USB" },
113 };
114
115 void drm_connector_ida_init(void)
116 {
117         int i;
118
119         for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
120                 ida_init(&drm_connector_enum_list[i].ida);
121 }
122
123 void drm_connector_ida_destroy(void)
124 {
125         int i;
126
127         for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
128                 ida_destroy(&drm_connector_enum_list[i].ida);
129 }
130
131 /**
132  * drm_get_connector_type_name - return a string for connector type
133  * @type: The connector type (DRM_MODE_CONNECTOR_*)
134  *
135  * Returns: the name of the connector type, or NULL if the type is not valid.
136  */
137 const char *drm_get_connector_type_name(unsigned int type)
138 {
139         if (type < ARRAY_SIZE(drm_connector_enum_list))
140                 return drm_connector_enum_list[type].name;
141
142         return NULL;
143 }
144 EXPORT_SYMBOL(drm_get_connector_type_name);
145
146 /**
147  * drm_connector_get_cmdline_mode - reads the user's cmdline mode
148  * @connector: connector to query
149  *
150  * The kernel supports per-connector configuration of its consoles through
151  * use of the video= parameter. This function parses that option and
152  * extracts the user's specified mode (or enable/disable status) for a
153  * particular connector. This is typically only used during the early fbdev
154  * setup.
155  */
156 static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
157 {
158         struct drm_cmdline_mode *mode = &connector->cmdline_mode;
159         const char *option;
160
161         option = video_get_options(connector->name);
162         if (!option)
163                 return;
164
165         if (!drm_mode_parse_command_line_for_connector(option,
166                                                        connector,
167                                                        mode))
168                 return;
169
170         if (mode->force) {
171                 DRM_INFO("forcing %s connector %s\n", connector->name,
172                          drm_get_connector_force_name(mode->force));
173                 connector->force = mode->force;
174         }
175
176         if (mode->panel_orientation != DRM_MODE_PANEL_ORIENTATION_UNKNOWN) {
177                 DRM_INFO("cmdline forces connector %s panel_orientation to %d\n",
178                          connector->name, mode->panel_orientation);
179                 drm_connector_set_panel_orientation(connector,
180                                                     mode->panel_orientation);
181         }
182
183         DRM_DEBUG_KMS("cmdline mode for connector %s %s %dx%d@%dHz%s%s%s\n",
184                       connector->name, mode->name,
185                       mode->xres, mode->yres,
186                       mode->refresh_specified ? mode->refresh : 60,
187                       mode->rb ? " reduced blanking" : "",
188                       mode->margins ? " with margins" : "",
189                       mode->interlace ?  " interlaced" : "");
190 }
191
192 static void drm_connector_free(struct kref *kref)
193 {
194         struct drm_connector *connector =
195                 container_of(kref, struct drm_connector, base.refcount);
196         struct drm_device *dev = connector->dev;
197
198         drm_mode_object_unregister(dev, &connector->base);
199         connector->funcs->destroy(connector);
200 }
201
202 void drm_connector_free_work_fn(struct work_struct *work)
203 {
204         struct drm_connector *connector, *n;
205         struct drm_device *dev =
206                 container_of(work, struct drm_device, mode_config.connector_free_work);
207         struct drm_mode_config *config = &dev->mode_config;
208         unsigned long flags;
209         struct llist_node *freed;
210
211         spin_lock_irqsave(&config->connector_list_lock, flags);
212         freed = llist_del_all(&config->connector_free_list);
213         spin_unlock_irqrestore(&config->connector_list_lock, flags);
214
215         llist_for_each_entry_safe(connector, n, freed, free_node) {
216                 drm_mode_object_unregister(dev, &connector->base);
217                 connector->funcs->destroy(connector);
218         }
219 }
220
221 static int __drm_connector_init(struct drm_device *dev,
222                                 struct drm_connector *connector,
223                                 const struct drm_connector_funcs *funcs,
224                                 int connector_type,
225                                 struct i2c_adapter *ddc)
226 {
227         struct drm_mode_config *config = &dev->mode_config;
228         int ret;
229         struct ida *connector_ida =
230                 &drm_connector_enum_list[connector_type].ida;
231
232         WARN_ON(drm_drv_uses_atomic_modeset(dev) &&
233                 (!funcs->atomic_destroy_state ||
234                  !funcs->atomic_duplicate_state));
235
236         ret = __drm_mode_object_add(dev, &connector->base,
237                                     DRM_MODE_OBJECT_CONNECTOR,
238                                     false, drm_connector_free);
239         if (ret)
240                 return ret;
241
242         connector->base.properties = &connector->properties;
243         connector->dev = dev;
244         connector->funcs = funcs;
245
246         /* connector index is used with 32bit bitmasks */
247         ret = ida_alloc_max(&config->connector_ida, 31, GFP_KERNEL);
248         if (ret < 0) {
249                 DRM_DEBUG_KMS("Failed to allocate %s connector index: %d\n",
250                               drm_connector_enum_list[connector_type].name,
251                               ret);
252                 goto out_put;
253         }
254         connector->index = ret;
255         ret = 0;
256
257         connector->connector_type = connector_type;
258         connector->connector_type_id =
259                 ida_alloc_min(connector_ida, 1, GFP_KERNEL);
260         if (connector->connector_type_id < 0) {
261                 ret = connector->connector_type_id;
262                 goto out_put_id;
263         }
264         connector->name =
265                 kasprintf(GFP_KERNEL, "%s-%d",
266                           drm_connector_enum_list[connector_type].name,
267                           connector->connector_type_id);
268         if (!connector->name) {
269                 ret = -ENOMEM;
270                 goto out_put_type_id;
271         }
272
273         /* provide ddc symlink in sysfs */
274         connector->ddc = ddc;
275
276         INIT_LIST_HEAD(&connector->global_connector_list_entry);
277         INIT_LIST_HEAD(&connector->probed_modes);
278         INIT_LIST_HEAD(&connector->modes);
279         mutex_init(&connector->mutex);
280         mutex_init(&connector->edid_override_mutex);
281         connector->edid_blob_ptr = NULL;
282         connector->epoch_counter = 0;
283         connector->tile_blob_ptr = NULL;
284         connector->status = connector_status_unknown;
285         connector->display_info.panel_orientation =
286                 DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
287
288         drm_connector_get_cmdline_mode(connector);
289
290         /* We should add connectors at the end to avoid upsetting the connector
291          * index too much.
292          */
293         spin_lock_irq(&config->connector_list_lock);
294         list_add_tail(&connector->head, &config->connector_list);
295         config->num_connector++;
296         spin_unlock_irq(&config->connector_list_lock);
297
298         if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL &&
299             connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
300                 drm_connector_attach_edid_property(connector);
301
302         drm_object_attach_property(&connector->base,
303                                       config->dpms_property, 0);
304
305         drm_object_attach_property(&connector->base,
306                                    config->link_status_property,
307                                    0);
308
309         drm_object_attach_property(&connector->base,
310                                    config->non_desktop_property,
311                                    0);
312         drm_object_attach_property(&connector->base,
313                                    config->tile_property,
314                                    0);
315
316         if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
317                 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
318         }
319
320         connector->debugfs_entry = NULL;
321 out_put_type_id:
322         if (ret)
323                 ida_free(connector_ida, connector->connector_type_id);
324 out_put_id:
325         if (ret)
326                 ida_free(&config->connector_ida, connector->index);
327 out_put:
328         if (ret)
329                 drm_mode_object_unregister(dev, &connector->base);
330
331         return ret;
332 }
333
334 /**
335  * drm_connector_init - Init a preallocated connector
336  * @dev: DRM device
337  * @connector: the connector to init
338  * @funcs: callbacks for this connector
339  * @connector_type: user visible type of the connector
340  *
341  * Initialises a preallocated connector. Connectors should be
342  * subclassed as part of driver connector objects.
343  *
344  * At driver unload time the driver's &drm_connector_funcs.destroy hook
345  * should call drm_connector_cleanup() and free the connector structure.
346  * The connector structure should not be allocated with devm_kzalloc().
347  *
348  * Note: consider using drmm_connector_init() instead of
349  * drm_connector_init() to let the DRM managed resource infrastructure
350  * take care of cleanup and deallocation.
351  *
352  * Returns:
353  * Zero on success, error code on failure.
354  */
355 int drm_connector_init(struct drm_device *dev,
356                        struct drm_connector *connector,
357                        const struct drm_connector_funcs *funcs,
358                        int connector_type)
359 {
360         if (drm_WARN_ON(dev, !(funcs && funcs->destroy)))
361                 return -EINVAL;
362
363         return __drm_connector_init(dev, connector, funcs, connector_type, NULL);
364 }
365 EXPORT_SYMBOL(drm_connector_init);
366
367 /**
368  * drm_connector_init_with_ddc - Init a preallocated connector
369  * @dev: DRM device
370  * @connector: the connector to init
371  * @funcs: callbacks for this connector
372  * @connector_type: user visible type of the connector
373  * @ddc: pointer to the associated ddc adapter
374  *
375  * Initialises a preallocated connector. Connectors should be
376  * subclassed as part of driver connector objects.
377  *
378  * At driver unload time the driver's &drm_connector_funcs.destroy hook
379  * should call drm_connector_cleanup() and free the connector structure.
380  * The connector structure should not be allocated with devm_kzalloc().
381  *
382  * Ensures that the ddc field of the connector is correctly set.
383  *
384  * Note: consider using drmm_connector_init() instead of
385  * drm_connector_init_with_ddc() to let the DRM managed resource
386  * infrastructure take care of cleanup and deallocation.
387  *
388  * Returns:
389  * Zero on success, error code on failure.
390  */
391 int drm_connector_init_with_ddc(struct drm_device *dev,
392                                 struct drm_connector *connector,
393                                 const struct drm_connector_funcs *funcs,
394                                 int connector_type,
395                                 struct i2c_adapter *ddc)
396 {
397         if (drm_WARN_ON(dev, !(funcs && funcs->destroy)))
398                 return -EINVAL;
399
400         return __drm_connector_init(dev, connector, funcs, connector_type, ddc);
401 }
402 EXPORT_SYMBOL(drm_connector_init_with_ddc);
403
404 static void drm_connector_cleanup_action(struct drm_device *dev,
405                                          void *ptr)
406 {
407         struct drm_connector *connector = ptr;
408
409         drm_connector_cleanup(connector);
410 }
411
412 /**
413  * drmm_connector_init - Init a preallocated connector
414  * @dev: DRM device
415  * @connector: the connector to init
416  * @funcs: callbacks for this connector
417  * @connector_type: user visible type of the connector
418  * @ddc: optional pointer to the associated ddc adapter
419  *
420  * Initialises a preallocated connector. Connectors should be
421  * subclassed as part of driver connector objects.
422  *
423  * Cleanup is automatically handled with a call to
424  * drm_connector_cleanup() in a DRM-managed action.
425  *
426  * The connector structure should be allocated with drmm_kzalloc().
427  *
428  * Returns:
429  * Zero on success, error code on failure.
430  */
431 int drmm_connector_init(struct drm_device *dev,
432                         struct drm_connector *connector,
433                         const struct drm_connector_funcs *funcs,
434                         int connector_type,
435                         struct i2c_adapter *ddc)
436 {
437         int ret;
438
439         if (drm_WARN_ON(dev, funcs && funcs->destroy))
440                 return -EINVAL;
441
442         ret = __drm_connector_init(dev, connector, funcs, connector_type, ddc);
443         if (ret)
444                 return ret;
445
446         ret = drmm_add_action_or_reset(dev, drm_connector_cleanup_action,
447                                        connector);
448         if (ret)
449                 return ret;
450
451         return 0;
452 }
453 EXPORT_SYMBOL(drmm_connector_init);
454
455 /**
456  * drm_connector_attach_edid_property - attach edid property.
457  * @connector: the connector
458  *
459  * Some connector types like DRM_MODE_CONNECTOR_VIRTUAL do not get a
460  * edid property attached by default.  This function can be used to
461  * explicitly enable the edid property in these cases.
462  */
463 void drm_connector_attach_edid_property(struct drm_connector *connector)
464 {
465         struct drm_mode_config *config = &connector->dev->mode_config;
466
467         drm_object_attach_property(&connector->base,
468                                    config->edid_property,
469                                    0);
470 }
471 EXPORT_SYMBOL(drm_connector_attach_edid_property);
472
473 /**
474  * drm_connector_attach_encoder - attach a connector to an encoder
475  * @connector: connector to attach
476  * @encoder: encoder to attach @connector to
477  *
478  * This function links up a connector to an encoder. Note that the routing
479  * restrictions between encoders and crtcs are exposed to userspace through the
480  * possible_clones and possible_crtcs bitmasks.
481  *
482  * Returns:
483  * Zero on success, negative errno on failure.
484  */
485 int drm_connector_attach_encoder(struct drm_connector *connector,
486                                  struct drm_encoder *encoder)
487 {
488         /*
489          * In the past, drivers have attempted to model the static association
490          * of connector to encoder in simple connector/encoder devices using a
491          * direct assignment of connector->encoder = encoder. This connection
492          * is a logical one and the responsibility of the core, so drivers are
493          * expected not to mess with this.
494          *
495          * Note that the error return should've been enough here, but a large
496          * majority of drivers ignores the return value, so add in a big WARN
497          * to get people's attention.
498          */
499         if (WARN_ON(connector->encoder))
500                 return -EINVAL;
501
502         connector->possible_encoders |= drm_encoder_mask(encoder);
503
504         return 0;
505 }
506 EXPORT_SYMBOL(drm_connector_attach_encoder);
507
508 /**
509  * drm_connector_has_possible_encoder - check if the connector and encoder are
510  * associated with each other
511  * @connector: the connector
512  * @encoder: the encoder
513  *
514  * Returns:
515  * True if @encoder is one of the possible encoders for @connector.
516  */
517 bool drm_connector_has_possible_encoder(struct drm_connector *connector,
518                                         struct drm_encoder *encoder)
519 {
520         return connector->possible_encoders & drm_encoder_mask(encoder);
521 }
522 EXPORT_SYMBOL(drm_connector_has_possible_encoder);
523
524 static void drm_mode_remove(struct drm_connector *connector,
525                             struct drm_display_mode *mode)
526 {
527         list_del(&mode->head);
528         drm_mode_destroy(connector->dev, mode);
529 }
530
531 /**
532  * drm_connector_cleanup - cleans up an initialised connector
533  * @connector: connector to cleanup
534  *
535  * Cleans up the connector but doesn't free the object.
536  */
537 void drm_connector_cleanup(struct drm_connector *connector)
538 {
539         struct drm_device *dev = connector->dev;
540         struct drm_display_mode *mode, *t;
541
542         /* The connector should have been removed from userspace long before
543          * it is finally destroyed.
544          */
545         if (WARN_ON(connector->registration_state ==
546                     DRM_CONNECTOR_REGISTERED))
547                 drm_connector_unregister(connector);
548
549         if (connector->privacy_screen) {
550                 drm_privacy_screen_put(connector->privacy_screen);
551                 connector->privacy_screen = NULL;
552         }
553
554         if (connector->tile_group) {
555                 drm_mode_put_tile_group(dev, connector->tile_group);
556                 connector->tile_group = NULL;
557         }
558
559         list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
560                 drm_mode_remove(connector, mode);
561
562         list_for_each_entry_safe(mode, t, &connector->modes, head)
563                 drm_mode_remove(connector, mode);
564
565         ida_free(&drm_connector_enum_list[connector->connector_type].ida,
566                           connector->connector_type_id);
567
568         ida_free(&dev->mode_config.connector_ida, connector->index);
569
570         kfree(connector->display_info.bus_formats);
571         kfree(connector->display_info.vics);
572         drm_mode_object_unregister(dev, &connector->base);
573         kfree(connector->name);
574         connector->name = NULL;
575         fwnode_handle_put(connector->fwnode);
576         connector->fwnode = NULL;
577         spin_lock_irq(&dev->mode_config.connector_list_lock);
578         list_del(&connector->head);
579         dev->mode_config.num_connector--;
580         spin_unlock_irq(&dev->mode_config.connector_list_lock);
581
582         WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
583         if (connector->state && connector->funcs->atomic_destroy_state)
584                 connector->funcs->atomic_destroy_state(connector,
585                                                        connector->state);
586
587         mutex_destroy(&connector->mutex);
588
589         memset(connector, 0, sizeof(*connector));
590
591         if (dev->registered)
592                 drm_sysfs_hotplug_event(dev);
593 }
594 EXPORT_SYMBOL(drm_connector_cleanup);
595
596 /**
597  * drm_connector_register - register a connector
598  * @connector: the connector to register
599  *
600  * Register userspace interfaces for a connector. Only call this for connectors
601  * which can be hotplugged after drm_dev_register() has been called already,
602  * e.g. DP MST connectors. All other connectors will be registered automatically
603  * when calling drm_dev_register().
604  *
605  * When the connector is no longer available, callers must call
606  * drm_connector_unregister().
607  *
608  * Returns:
609  * Zero on success, error code on failure.
610  */
611 int drm_connector_register(struct drm_connector *connector)
612 {
613         int ret = 0;
614
615         if (!connector->dev->registered)
616                 return 0;
617
618         mutex_lock(&connector->mutex);
619         if (connector->registration_state != DRM_CONNECTOR_INITIALIZING)
620                 goto unlock;
621
622         ret = drm_sysfs_connector_add(connector);
623         if (ret)
624                 goto unlock;
625
626         drm_debugfs_connector_add(connector);
627
628         if (connector->funcs->late_register) {
629                 ret = connector->funcs->late_register(connector);
630                 if (ret)
631                         goto err_debugfs;
632         }
633
634         drm_mode_object_register(connector->dev, &connector->base);
635
636         connector->registration_state = DRM_CONNECTOR_REGISTERED;
637
638         /* Let userspace know we have a new connector */
639         drm_sysfs_connector_hotplug_event(connector);
640
641         if (connector->privacy_screen)
642                 drm_privacy_screen_register_notifier(connector->privacy_screen,
643                                            &connector->privacy_screen_notifier);
644
645         mutex_lock(&connector_list_lock);
646         list_add_tail(&connector->global_connector_list_entry, &connector_list);
647         mutex_unlock(&connector_list_lock);
648         goto unlock;
649
650 err_debugfs:
651         drm_debugfs_connector_remove(connector);
652         drm_sysfs_connector_remove(connector);
653 unlock:
654         mutex_unlock(&connector->mutex);
655         return ret;
656 }
657 EXPORT_SYMBOL(drm_connector_register);
658
659 /**
660  * drm_connector_unregister - unregister a connector
661  * @connector: the connector to unregister
662  *
663  * Unregister userspace interfaces for a connector. Only call this for
664  * connectors which have been registered explicitly by calling
665  * drm_connector_register().
666  */
667 void drm_connector_unregister(struct drm_connector *connector)
668 {
669         mutex_lock(&connector->mutex);
670         if (connector->registration_state != DRM_CONNECTOR_REGISTERED) {
671                 mutex_unlock(&connector->mutex);
672                 return;
673         }
674
675         mutex_lock(&connector_list_lock);
676         list_del_init(&connector->global_connector_list_entry);
677         mutex_unlock(&connector_list_lock);
678
679         if (connector->privacy_screen)
680                 drm_privacy_screen_unregister_notifier(
681                                         connector->privacy_screen,
682                                         &connector->privacy_screen_notifier);
683
684         if (connector->funcs->early_unregister)
685                 connector->funcs->early_unregister(connector);
686
687         drm_sysfs_connector_remove(connector);
688         drm_debugfs_connector_remove(connector);
689
690         connector->registration_state = DRM_CONNECTOR_UNREGISTERED;
691         mutex_unlock(&connector->mutex);
692 }
693 EXPORT_SYMBOL(drm_connector_unregister);
694
695 void drm_connector_unregister_all(struct drm_device *dev)
696 {
697         struct drm_connector *connector;
698         struct drm_connector_list_iter conn_iter;
699
700         drm_connector_list_iter_begin(dev, &conn_iter);
701         drm_for_each_connector_iter(connector, &conn_iter)
702                 drm_connector_unregister(connector);
703         drm_connector_list_iter_end(&conn_iter);
704 }
705
706 int drm_connector_register_all(struct drm_device *dev)
707 {
708         struct drm_connector *connector;
709         struct drm_connector_list_iter conn_iter;
710         int ret = 0;
711
712         drm_connector_list_iter_begin(dev, &conn_iter);
713         drm_for_each_connector_iter(connector, &conn_iter) {
714                 ret = drm_connector_register(connector);
715                 if (ret)
716                         break;
717         }
718         drm_connector_list_iter_end(&conn_iter);
719
720         if (ret)
721                 drm_connector_unregister_all(dev);
722         return ret;
723 }
724
725 /**
726  * drm_get_connector_status_name - return a string for connector status
727  * @status: connector status to compute name of
728  *
729  * In contrast to the other drm_get_*_name functions this one here returns a
730  * const pointer and hence is threadsafe.
731  *
732  * Returns: connector status string
733  */
734 const char *drm_get_connector_status_name(enum drm_connector_status status)
735 {
736         if (status == connector_status_connected)
737                 return "connected";
738         else if (status == connector_status_disconnected)
739                 return "disconnected";
740         else
741                 return "unknown";
742 }
743 EXPORT_SYMBOL(drm_get_connector_status_name);
744
745 /**
746  * drm_get_connector_force_name - return a string for connector force
747  * @force: connector force to get name of
748  *
749  * Returns: const pointer to name.
750  */
751 const char *drm_get_connector_force_name(enum drm_connector_force force)
752 {
753         switch (force) {
754         case DRM_FORCE_UNSPECIFIED:
755                 return "unspecified";
756         case DRM_FORCE_OFF:
757                 return "off";
758         case DRM_FORCE_ON:
759                 return "on";
760         case DRM_FORCE_ON_DIGITAL:
761                 return "digital";
762         default:
763                 return "unknown";
764         }
765 }
766
767 #ifdef CONFIG_LOCKDEP
768 static struct lockdep_map connector_list_iter_dep_map = {
769         .name = "drm_connector_list_iter"
770 };
771 #endif
772
773 /**
774  * drm_connector_list_iter_begin - initialize a connector_list iterator
775  * @dev: DRM device
776  * @iter: connector_list iterator
777  *
778  * Sets @iter up to walk the &drm_mode_config.connector_list of @dev. @iter
779  * must always be cleaned up again by calling drm_connector_list_iter_end().
780  * Iteration itself happens using drm_connector_list_iter_next() or
781  * drm_for_each_connector_iter().
782  */
783 void drm_connector_list_iter_begin(struct drm_device *dev,
784                                    struct drm_connector_list_iter *iter)
785 {
786         iter->dev = dev;
787         iter->conn = NULL;
788         lock_acquire_shared_recursive(&connector_list_iter_dep_map, 0, 1, NULL, _RET_IP_);
789 }
790 EXPORT_SYMBOL(drm_connector_list_iter_begin);
791
792 /*
793  * Extra-safe connector put function that works in any context. Should only be
794  * used from the connector_iter functions, where we never really expect to
795  * actually release the connector when dropping our final reference.
796  */
797 static void
798 __drm_connector_put_safe(struct drm_connector *conn)
799 {
800         struct drm_mode_config *config = &conn->dev->mode_config;
801
802         lockdep_assert_held(&config->connector_list_lock);
803
804         if (!refcount_dec_and_test(&conn->base.refcount.refcount))
805                 return;
806
807         llist_add(&conn->free_node, &config->connector_free_list);
808         schedule_work(&config->connector_free_work);
809 }
810
811 /**
812  * drm_connector_list_iter_next - return next connector
813  * @iter: connector_list iterator
814  *
815  * Returns: the next connector for @iter, or NULL when the list walk has
816  * completed.
817  */
818 struct drm_connector *
819 drm_connector_list_iter_next(struct drm_connector_list_iter *iter)
820 {
821         struct drm_connector *old_conn = iter->conn;
822         struct drm_mode_config *config = &iter->dev->mode_config;
823         struct list_head *lhead;
824         unsigned long flags;
825
826         spin_lock_irqsave(&config->connector_list_lock, flags);
827         lhead = old_conn ? &old_conn->head : &config->connector_list;
828
829         do {
830                 if (lhead->next == &config->connector_list) {
831                         iter->conn = NULL;
832                         break;
833                 }
834
835                 lhead = lhead->next;
836                 iter->conn = list_entry(lhead, struct drm_connector, head);
837
838                 /* loop until it's not a zombie connector */
839         } while (!kref_get_unless_zero(&iter->conn->base.refcount));
840
841         if (old_conn)
842                 __drm_connector_put_safe(old_conn);
843         spin_unlock_irqrestore(&config->connector_list_lock, flags);
844
845         return iter->conn;
846 }
847 EXPORT_SYMBOL(drm_connector_list_iter_next);
848
849 /**
850  * drm_connector_list_iter_end - tear down a connector_list iterator
851  * @iter: connector_list iterator
852  *
853  * Tears down @iter and releases any resources (like &drm_connector references)
854  * acquired while walking the list. This must always be called, both when the
855  * iteration completes fully or when it was aborted without walking the entire
856  * list.
857  */
858 void drm_connector_list_iter_end(struct drm_connector_list_iter *iter)
859 {
860         struct drm_mode_config *config = &iter->dev->mode_config;
861         unsigned long flags;
862
863         iter->dev = NULL;
864         if (iter->conn) {
865                 spin_lock_irqsave(&config->connector_list_lock, flags);
866                 __drm_connector_put_safe(iter->conn);
867                 spin_unlock_irqrestore(&config->connector_list_lock, flags);
868         }
869         lock_release(&connector_list_iter_dep_map, _RET_IP_);
870 }
871 EXPORT_SYMBOL(drm_connector_list_iter_end);
872
873 static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
874         { SubPixelUnknown, "Unknown" },
875         { SubPixelHorizontalRGB, "Horizontal RGB" },
876         { SubPixelHorizontalBGR, "Horizontal BGR" },
877         { SubPixelVerticalRGB, "Vertical RGB" },
878         { SubPixelVerticalBGR, "Vertical BGR" },
879         { SubPixelNone, "None" },
880 };
881
882 /**
883  * drm_get_subpixel_order_name - return a string for a given subpixel enum
884  * @order: enum of subpixel_order
885  *
886  * Note you could abuse this and return something out of bounds, but that
887  * would be a caller error.  No unscrubbed user data should make it here.
888  *
889  * Returns: string describing an enumerated subpixel property
890  */
891 const char *drm_get_subpixel_order_name(enum subpixel_order order)
892 {
893         return drm_subpixel_enum_list[order].name;
894 }
895 EXPORT_SYMBOL(drm_get_subpixel_order_name);
896
897 static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
898         { DRM_MODE_DPMS_ON, "On" },
899         { DRM_MODE_DPMS_STANDBY, "Standby" },
900         { DRM_MODE_DPMS_SUSPEND, "Suspend" },
901         { DRM_MODE_DPMS_OFF, "Off" }
902 };
903 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
904
905 static const struct drm_prop_enum_list drm_link_status_enum_list[] = {
906         { DRM_MODE_LINK_STATUS_GOOD, "Good" },
907         { DRM_MODE_LINK_STATUS_BAD, "Bad" },
908 };
909
910 /**
911  * drm_display_info_set_bus_formats - set the supported bus formats
912  * @info: display info to store bus formats in
913  * @formats: array containing the supported bus formats
914  * @num_formats: the number of entries in the fmts array
915  *
916  * Store the supported bus formats in display info structure.
917  * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
918  * a full list of available formats.
919  *
920  * Returns:
921  * 0 on success or a negative error code on failure.
922  */
923 int drm_display_info_set_bus_formats(struct drm_display_info *info,
924                                      const u32 *formats,
925                                      unsigned int num_formats)
926 {
927         u32 *fmts = NULL;
928
929         if (!formats && num_formats)
930                 return -EINVAL;
931
932         if (formats && num_formats) {
933                 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
934                                GFP_KERNEL);
935                 if (!fmts)
936                         return -ENOMEM;
937         }
938
939         kfree(info->bus_formats);
940         info->bus_formats = fmts;
941         info->num_bus_formats = num_formats;
942
943         return 0;
944 }
945 EXPORT_SYMBOL(drm_display_info_set_bus_formats);
946
947 /* Optional connector properties. */
948 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
949         { DRM_MODE_SCALE_NONE, "None" },
950         { DRM_MODE_SCALE_FULLSCREEN, "Full" },
951         { DRM_MODE_SCALE_CENTER, "Center" },
952         { DRM_MODE_SCALE_ASPECT, "Full aspect" },
953 };
954
955 static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
956         { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
957         { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
958         { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
959 };
960
961 static const struct drm_prop_enum_list drm_content_type_enum_list[] = {
962         { DRM_MODE_CONTENT_TYPE_NO_DATA, "No Data" },
963         { DRM_MODE_CONTENT_TYPE_GRAPHICS, "Graphics" },
964         { DRM_MODE_CONTENT_TYPE_PHOTO, "Photo" },
965         { DRM_MODE_CONTENT_TYPE_CINEMA, "Cinema" },
966         { DRM_MODE_CONTENT_TYPE_GAME, "Game" },
967 };
968
969 static const struct drm_prop_enum_list drm_panel_orientation_enum_list[] = {
970         { DRM_MODE_PANEL_ORIENTATION_NORMAL,    "Normal"        },
971         { DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP, "Upside Down"   },
972         { DRM_MODE_PANEL_ORIENTATION_LEFT_UP,   "Left Side Up"  },
973         { DRM_MODE_PANEL_ORIENTATION_RIGHT_UP,  "Right Side Up" },
974 };
975
976 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
977         { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
978         { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
979         { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
980 };
981 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
982
983 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
984         { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I, TV-out and DP */
985         { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
986         { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
987 };
988 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
989                  drm_dvi_i_subconnector_enum_list)
990
991 static const struct drm_prop_enum_list drm_tv_mode_enum_list[] = {
992         { DRM_MODE_TV_MODE_NTSC, "NTSC" },
993         { DRM_MODE_TV_MODE_NTSC_443, "NTSC-443" },
994         { DRM_MODE_TV_MODE_NTSC_J, "NTSC-J" },
995         { DRM_MODE_TV_MODE_PAL, "PAL" },
996         { DRM_MODE_TV_MODE_PAL_M, "PAL-M" },
997         { DRM_MODE_TV_MODE_PAL_N, "PAL-N" },
998         { DRM_MODE_TV_MODE_SECAM, "SECAM" },
999 };
1000 DRM_ENUM_NAME_FN(drm_get_tv_mode_name, drm_tv_mode_enum_list)
1001
1002 /**
1003  * drm_get_tv_mode_from_name - Translates a TV mode name into its enum value
1004  * @name: TV Mode name we want to convert
1005  * @len: Length of @name
1006  *
1007  * Translates @name into an enum drm_connector_tv_mode.
1008  *
1009  * Returns: the enum value on success, a negative errno otherwise.
1010  */
1011 int drm_get_tv_mode_from_name(const char *name, size_t len)
1012 {
1013         unsigned int i;
1014
1015         for (i = 0; i < ARRAY_SIZE(drm_tv_mode_enum_list); i++) {
1016                 const struct drm_prop_enum_list *item = &drm_tv_mode_enum_list[i];
1017
1018                 if (strlen(item->name) == len && !strncmp(item->name, name, len))
1019                         return item->type;
1020         }
1021
1022         return -EINVAL;
1023 }
1024 EXPORT_SYMBOL(drm_get_tv_mode_from_name);
1025
1026 static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
1027         { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
1028         { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
1029         { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
1030         { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
1031         { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
1032 };
1033 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
1034
1035 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
1036         { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I, TV-out and DP */
1037         { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
1038         { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
1039         { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
1040         { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
1041 };
1042 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
1043                  drm_tv_subconnector_enum_list)
1044
1045 static const struct drm_prop_enum_list drm_dp_subconnector_enum_list[] = {
1046         { DRM_MODE_SUBCONNECTOR_Unknown,     "Unknown"   }, /* DVI-I, TV-out and DP */
1047         { DRM_MODE_SUBCONNECTOR_VGA,         "VGA"       }, /* DP */
1048         { DRM_MODE_SUBCONNECTOR_DVID,        "DVI-D"     }, /* DP */
1049         { DRM_MODE_SUBCONNECTOR_HDMIA,       "HDMI"      }, /* DP */
1050         { DRM_MODE_SUBCONNECTOR_DisplayPort, "DP"        }, /* DP */
1051         { DRM_MODE_SUBCONNECTOR_Wireless,    "Wireless"  }, /* DP */
1052         { DRM_MODE_SUBCONNECTOR_Native,      "Native"    }, /* DP */
1053 };
1054
1055 DRM_ENUM_NAME_FN(drm_get_dp_subconnector_name,
1056                  drm_dp_subconnector_enum_list)
1057
1058
1059 static const char * const colorspace_names[] = {
1060         /* For Default case, driver will set the colorspace */
1061         [DRM_MODE_COLORIMETRY_DEFAULT] = "Default",
1062         /* Standard Definition Colorimetry based on CEA 861 */
1063         [DRM_MODE_COLORIMETRY_SMPTE_170M_YCC] = "SMPTE_170M_YCC",
1064         [DRM_MODE_COLORIMETRY_BT709_YCC] = "BT709_YCC",
1065         /* Standard Definition Colorimetry based on IEC 61966-2-4 */
1066         [DRM_MODE_COLORIMETRY_XVYCC_601] = "XVYCC_601",
1067         /* High Definition Colorimetry based on IEC 61966-2-4 */
1068         [DRM_MODE_COLORIMETRY_XVYCC_709] = "XVYCC_709",
1069         /* Colorimetry based on IEC 61966-2-1/Amendment 1 */
1070         [DRM_MODE_COLORIMETRY_SYCC_601] = "SYCC_601",
1071         /* Colorimetry based on IEC 61966-2-5 [33] */
1072         [DRM_MODE_COLORIMETRY_OPYCC_601] = "opYCC_601",
1073         /* Colorimetry based on IEC 61966-2-5 */
1074         [DRM_MODE_COLORIMETRY_OPRGB] = "opRGB",
1075         /* Colorimetry based on ITU-R BT.2020 */
1076         [DRM_MODE_COLORIMETRY_BT2020_CYCC] = "BT2020_CYCC",
1077         /* Colorimetry based on ITU-R BT.2020 */
1078         [DRM_MODE_COLORIMETRY_BT2020_RGB] = "BT2020_RGB",
1079         /* Colorimetry based on ITU-R BT.2020 */
1080         [DRM_MODE_COLORIMETRY_BT2020_YCC] = "BT2020_YCC",
1081         /* Added as part of Additional Colorimetry Extension in 861.G */
1082         [DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65] = "DCI-P3_RGB_D65",
1083         [DRM_MODE_COLORIMETRY_DCI_P3_RGB_THEATER] = "DCI-P3_RGB_Theater",
1084         [DRM_MODE_COLORIMETRY_RGB_WIDE_FIXED] = "RGB_WIDE_FIXED",
1085         /* Colorimetry based on scRGB (IEC 61966-2-2) */
1086         [DRM_MODE_COLORIMETRY_RGB_WIDE_FLOAT] = "RGB_WIDE_FLOAT",
1087         [DRM_MODE_COLORIMETRY_BT601_YCC] = "BT601_YCC",
1088 };
1089
1090 /**
1091  * drm_get_colorspace_name - return a string for color encoding
1092  * @colorspace: color space to compute name of
1093  *
1094  * In contrast to the other drm_get_*_name functions this one here returns a
1095  * const pointer and hence is threadsafe.
1096  */
1097 const char *drm_get_colorspace_name(enum drm_colorspace colorspace)
1098 {
1099         if (colorspace < ARRAY_SIZE(colorspace_names) && colorspace_names[colorspace])
1100                 return colorspace_names[colorspace];
1101         else
1102                 return "(null)";
1103 }
1104
1105 static const u32 hdmi_colorspaces =
1106         BIT(DRM_MODE_COLORIMETRY_SMPTE_170M_YCC) |
1107         BIT(DRM_MODE_COLORIMETRY_BT709_YCC) |
1108         BIT(DRM_MODE_COLORIMETRY_XVYCC_601) |
1109         BIT(DRM_MODE_COLORIMETRY_XVYCC_709) |
1110         BIT(DRM_MODE_COLORIMETRY_SYCC_601) |
1111         BIT(DRM_MODE_COLORIMETRY_OPYCC_601) |
1112         BIT(DRM_MODE_COLORIMETRY_OPRGB) |
1113         BIT(DRM_MODE_COLORIMETRY_BT2020_CYCC) |
1114         BIT(DRM_MODE_COLORIMETRY_BT2020_RGB) |
1115         BIT(DRM_MODE_COLORIMETRY_BT2020_YCC) |
1116         BIT(DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65) |
1117         BIT(DRM_MODE_COLORIMETRY_DCI_P3_RGB_THEATER);
1118
1119 /*
1120  * As per DP 1.4a spec, 2.2.5.7.5 VSC SDP Payload for Pixel Encoding/Colorimetry
1121  * Format Table 2-120
1122  */
1123 static const u32 dp_colorspaces =
1124         BIT(DRM_MODE_COLORIMETRY_RGB_WIDE_FIXED) |
1125         BIT(DRM_MODE_COLORIMETRY_RGB_WIDE_FLOAT) |
1126         BIT(DRM_MODE_COLORIMETRY_OPRGB) |
1127         BIT(DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65) |
1128         BIT(DRM_MODE_COLORIMETRY_BT2020_RGB) |
1129         BIT(DRM_MODE_COLORIMETRY_BT601_YCC) |
1130         BIT(DRM_MODE_COLORIMETRY_BT709_YCC) |
1131         BIT(DRM_MODE_COLORIMETRY_XVYCC_601) |
1132         BIT(DRM_MODE_COLORIMETRY_XVYCC_709) |
1133         BIT(DRM_MODE_COLORIMETRY_SYCC_601) |
1134         BIT(DRM_MODE_COLORIMETRY_OPYCC_601) |
1135         BIT(DRM_MODE_COLORIMETRY_BT2020_CYCC) |
1136         BIT(DRM_MODE_COLORIMETRY_BT2020_YCC);
1137
1138 /**
1139  * DOC: standard connector properties
1140  *
1141  * DRM connectors have a few standardized properties:
1142  *
1143  * EDID:
1144  *      Blob property which contains the current EDID read from the sink. This
1145  *      is useful to parse sink identification information like vendor, model
1146  *      and serial. Drivers should update this property by calling
1147  *      drm_connector_update_edid_property(), usually after having parsed
1148  *      the EDID using drm_add_edid_modes(). Userspace cannot change this
1149  *      property.
1150  *
1151  *      User-space should not parse the EDID to obtain information exposed via
1152  *      other KMS properties (because the kernel might apply limits, quirks or
1153  *      fixups to the EDID). For instance, user-space should not try to parse
1154  *      mode lists from the EDID.
1155  * DPMS:
1156  *      Legacy property for setting the power state of the connector. For atomic
1157  *      drivers this is only provided for backwards compatibility with existing
1158  *      drivers, it remaps to controlling the "ACTIVE" property on the CRTC the
1159  *      connector is linked to. Drivers should never set this property directly,
1160  *      it is handled by the DRM core by calling the &drm_connector_funcs.dpms
1161  *      callback. For atomic drivers the remapping to the "ACTIVE" property is
1162  *      implemented in the DRM core.
1163  *
1164  *      Note that this property cannot be set through the MODE_ATOMIC ioctl,
1165  *      userspace must use "ACTIVE" on the CRTC instead.
1166  *
1167  *      WARNING:
1168  *
1169  *      For userspace also running on legacy drivers the "DPMS" semantics are a
1170  *      lot more complicated. First, userspace cannot rely on the "DPMS" value
1171  *      returned by the GETCONNECTOR actually reflecting reality, because many
1172  *      drivers fail to update it. For atomic drivers this is taken care of in
1173  *      drm_atomic_helper_update_legacy_modeset_state().
1174  *
1175  *      The second issue is that the DPMS state is only well-defined when the
1176  *      connector is connected to a CRTC. In atomic the DRM core enforces that
1177  *      "ACTIVE" is off in such a case, no such checks exists for "DPMS".
1178  *
1179  *      Finally, when enabling an output using the legacy SETCONFIG ioctl then
1180  *      "DPMS" is forced to ON. But see above, that might not be reflected in
1181  *      the software value on legacy drivers.
1182  *
1183  *      Summarizing: Only set "DPMS" when the connector is known to be enabled,
1184  *      assume that a successful SETCONFIG call also sets "DPMS" to on, and
1185  *      never read back the value of "DPMS" because it can be incorrect.
1186  * PATH:
1187  *      Connector path property to identify how this sink is physically
1188  *      connected. Used by DP MST. This should be set by calling
1189  *      drm_connector_set_path_property(), in the case of DP MST with the
1190  *      path property the MST manager created. Userspace cannot change this
1191  *      property.
1192  * TILE:
1193  *      Connector tile group property to indicate how a set of DRM connector
1194  *      compose together into one logical screen. This is used by both high-res
1195  *      external screens (often only using a single cable, but exposing multiple
1196  *      DP MST sinks), or high-res integrated panels (like dual-link DSI) which
1197  *      are not gen-locked. Note that for tiled panels which are genlocked, like
1198  *      dual-link LVDS or dual-link DSI, the driver should try to not expose the
1199  *      tiling and virtualise both &drm_crtc and &drm_plane if needed. Drivers
1200  *      should update this value using drm_connector_set_tile_property().
1201  *      Userspace cannot change this property.
1202  * link-status:
1203  *      Connector link-status property to indicate the status of link. The
1204  *      default value of link-status is "GOOD". If something fails during or
1205  *      after modeset, the kernel driver may set this to "BAD" and issue a
1206  *      hotplug uevent. Drivers should update this value using
1207  *      drm_connector_set_link_status_property().
1208  *
1209  *      When user-space receives the hotplug uevent and detects a "BAD"
1210  *      link-status, the sink doesn't receive pixels anymore (e.g. the screen
1211  *      becomes completely black). The list of available modes may have
1212  *      changed. User-space is expected to pick a new mode if the current one
1213  *      has disappeared and perform a new modeset with link-status set to
1214  *      "GOOD" to re-enable the connector.
1215  *
1216  *      If multiple connectors share the same CRTC and one of them gets a "BAD"
1217  *      link-status, the other are unaffected (ie. the sinks still continue to
1218  *      receive pixels).
1219  *
1220  *      When user-space performs an atomic commit on a connector with a "BAD"
1221  *      link-status without resetting the property to "GOOD", the sink may
1222  *      still not receive pixels. When user-space performs an atomic commit
1223  *      which resets the link-status property to "GOOD" without the
1224  *      ALLOW_MODESET flag set, it might fail because a modeset is required.
1225  *
1226  *      User-space can only change link-status to "GOOD", changing it to "BAD"
1227  *      is a no-op.
1228  *
1229  *      For backwards compatibility with non-atomic userspace the kernel
1230  *      tries to automatically set the link-status back to "GOOD" in the
1231  *      SETCRTC IOCTL. This might fail if the mode is no longer valid, similar
1232  *      to how it might fail if a different screen has been connected in the
1233  *      interim.
1234  * non_desktop:
1235  *      Indicates the output should be ignored for purposes of displaying a
1236  *      standard desktop environment or console. This is most likely because
1237  *      the output device is not rectilinear.
1238  * Content Protection:
1239  *      This property is used by userspace to request the kernel protect future
1240  *      content communicated over the link. When requested, kernel will apply
1241  *      the appropriate means of protection (most often HDCP), and use the
1242  *      property to tell userspace the protection is active.
1243  *
1244  *      Drivers can set this up by calling
1245  *      drm_connector_attach_content_protection_property() on initialization.
1246  *
1247  *      The value of this property can be one of the following:
1248  *
1249  *      DRM_MODE_CONTENT_PROTECTION_UNDESIRED = 0
1250  *              The link is not protected, content is transmitted in the clear.
1251  *      DRM_MODE_CONTENT_PROTECTION_DESIRED = 1
1252  *              Userspace has requested content protection, but the link is not
1253  *              currently protected. When in this state, kernel should enable
1254  *              Content Protection as soon as possible.
1255  *      DRM_MODE_CONTENT_PROTECTION_ENABLED = 2
1256  *              Userspace has requested content protection, and the link is
1257  *              protected. Only the driver can set the property to this value.
1258  *              If userspace attempts to set to ENABLED, kernel will return
1259  *              -EINVAL.
1260  *
1261  *      A few guidelines:
1262  *
1263  *      - DESIRED state should be preserved until userspace de-asserts it by
1264  *        setting the property to UNDESIRED. This means ENABLED should only
1265  *        transition to UNDESIRED when the user explicitly requests it.
1266  *      - If the state is DESIRED, kernel should attempt to re-authenticate the
1267  *        link whenever possible. This includes across disable/enable, dpms,
1268  *        hotplug, downstream device changes, link status failures, etc..
1269  *      - Kernel sends uevent with the connector id and property id through
1270  *        @drm_hdcp_update_content_protection, upon below kernel triggered
1271  *        scenarios:
1272  *
1273  *              - DESIRED -> ENABLED (authentication success)
1274  *              - ENABLED -> DESIRED (termination of authentication)
1275  *      - Please note no uevents for userspace triggered property state changes,
1276  *        which can't fail such as
1277  *
1278  *              - DESIRED/ENABLED -> UNDESIRED
1279  *              - UNDESIRED -> DESIRED
1280  *      - Userspace is responsible for polling the property or listen to uevents
1281  *        to determine when the value transitions from ENABLED to DESIRED.
1282  *        This signifies the link is no longer protected and userspace should
1283  *        take appropriate action (whatever that might be).
1284  *
1285  * HDCP Content Type:
1286  *      This Enum property is used by the userspace to declare the content type
1287  *      of the display stream, to kernel. Here display stream stands for any
1288  *      display content that userspace intended to display through HDCP
1289  *      encryption.
1290  *
1291  *      Content Type of a stream is decided by the owner of the stream, as
1292  *      "HDCP Type0" or "HDCP Type1".
1293  *
1294  *      The value of the property can be one of the below:
1295  *        - "HDCP Type0": DRM_MODE_HDCP_CONTENT_TYPE0 = 0
1296  *        - "HDCP Type1": DRM_MODE_HDCP_CONTENT_TYPE1 = 1
1297  *
1298  *      When kernel starts the HDCP authentication (see "Content Protection"
1299  *      for details), it uses the content type in "HDCP Content Type"
1300  *      for performing the HDCP authentication with the display sink.
1301  *
1302  *      Please note in HDCP spec versions, a link can be authenticated with
1303  *      HDCP 2.2 for Content Type 0/Content Type 1. Where as a link can be
1304  *      authenticated with HDCP1.4 only for Content Type 0(though it is implicit
1305  *      in nature. As there is no reference for Content Type in HDCP1.4).
1306  *
1307  *      HDCP2.2 authentication protocol itself takes the "Content Type" as a
1308  *      parameter, which is a input for the DP HDCP2.2 encryption algo.
1309  *
1310  *      In case of Type 0 content protection request, kernel driver can choose
1311  *      either of HDCP spec versions 1.4 and 2.2. When HDCP2.2 is used for
1312  *      "HDCP Type 0", a HDCP 2.2 capable repeater in the downstream can send
1313  *      that content to a HDCP 1.4 authenticated HDCP sink (Type0 link).
1314  *      But if the content is classified as "HDCP Type 1", above mentioned
1315  *      HDCP 2.2 repeater wont send the content to the HDCP sink as it can't
1316  *      authenticate the HDCP1.4 capable sink for "HDCP Type 1".
1317  *
1318  *      Please note userspace can be ignorant of the HDCP versions used by the
1319  *      kernel driver to achieve the "HDCP Content Type".
1320  *
1321  *      At current scenario, classifying a content as Type 1 ensures that the
1322  *      content will be displayed only through the HDCP2.2 encrypted link.
1323  *
1324  *      Note that the HDCP Content Type property is introduced at HDCP 2.2, and
1325  *      defaults to type 0. It is only exposed by drivers supporting HDCP 2.2
1326  *      (hence supporting Type 0 and Type 1). Based on how next versions of
1327  *      HDCP specs are defined content Type could be used for higher versions
1328  *      too.
1329  *
1330  *      If content type is changed when "Content Protection" is not UNDESIRED,
1331  *      then kernel will disable the HDCP and re-enable with new type in the
1332  *      same atomic commit. And when "Content Protection" is ENABLED, it means
1333  *      that link is HDCP authenticated and encrypted, for the transmission of
1334  *      the Type of stream mentioned at "HDCP Content Type".
1335  *
1336  * HDR_OUTPUT_METADATA:
1337  *      Connector property to enable userspace to send HDR Metadata to
1338  *      driver. This metadata is based on the composition and blending
1339  *      policies decided by user, taking into account the hardware and
1340  *      sink capabilities. The driver gets this metadata and creates a
1341  *      Dynamic Range and Mastering Infoframe (DRM) in case of HDMI,
1342  *      SDP packet (Non-audio INFOFRAME SDP v1.3) for DP. This is then
1343  *      sent to sink. This notifies the sink of the upcoming frame's Color
1344  *      Encoding and Luminance parameters.
1345  *
1346  *      Userspace first need to detect the HDR capabilities of sink by
1347  *      reading and parsing the EDID. Details of HDR metadata for HDMI
1348  *      are added in CTA 861.G spec. For DP , its defined in VESA DP
1349  *      Standard v1.4. It needs to then get the metadata information
1350  *      of the video/game/app content which are encoded in HDR (basically
1351  *      using HDR transfer functions). With this information it needs to
1352  *      decide on a blending policy and compose the relevant
1353  *      layers/overlays into a common format. Once this blending is done,
1354  *      userspace will be aware of the metadata of the composed frame to
1355  *      be send to sink. It then uses this property to communicate this
1356  *      metadata to driver which then make a Infoframe packet and sends
1357  *      to sink based on the type of encoder connected.
1358  *
1359  *      Userspace will be responsible to do Tone mapping operation in case:
1360  *              - Some layers are HDR and others are SDR
1361  *              - HDR layers luminance is not same as sink
1362  *
1363  *      It will even need to do colorspace conversion and get all layers
1364  *      to one common colorspace for blending. It can use either GL, Media
1365  *      or display engine to get this done based on the capabilities of the
1366  *      associated hardware.
1367  *
1368  *      Driver expects metadata to be put in &struct hdr_output_metadata
1369  *      structure from userspace. This is received as blob and stored in
1370  *      &drm_connector_state.hdr_output_metadata. It parses EDID and saves the
1371  *      sink metadata in &struct hdr_sink_metadata, as
1372  *      &drm_connector.hdr_sink_metadata.  Driver uses
1373  *      drm_hdmi_infoframe_set_hdr_metadata() helper to set the HDR metadata,
1374  *      hdmi_drm_infoframe_pack() to pack the infoframe as per spec, in case of
1375  *      HDMI encoder.
1376  *
1377  * max bpc:
1378  *      This range property is used by userspace to limit the bit depth. When
1379  *      used the driver would limit the bpc in accordance with the valid range
1380  *      supported by the hardware and sink. Drivers to use the function
1381  *      drm_connector_attach_max_bpc_property() to create and attach the
1382  *      property to the connector during initialization.
1383  *
1384  * Connectors also have one standardized atomic property:
1385  *
1386  * CRTC_ID:
1387  *      Mode object ID of the &drm_crtc this connector should be connected to.
1388  *
1389  * Connectors for LCD panels may also have one standardized property:
1390  *
1391  * panel orientation:
1392  *      On some devices the LCD panel is mounted in the casing in such a way
1393  *      that the up/top side of the panel does not match with the top side of
1394  *      the device. Userspace can use this property to check for this.
1395  *      Note that input coordinates from touchscreens (input devices with
1396  *      INPUT_PROP_DIRECT) will still map 1:1 to the actual LCD panel
1397  *      coordinates, so if userspace rotates the picture to adjust for
1398  *      the orientation it must also apply the same transformation to the
1399  *      touchscreen input coordinates. This property is initialized by calling
1400  *      drm_connector_set_panel_orientation() or
1401  *      drm_connector_set_panel_orientation_with_quirk()
1402  *
1403  * scaling mode:
1404  *      This property defines how a non-native mode is upscaled to the native
1405  *      mode of an LCD panel:
1406  *
1407  *      None:
1408  *              No upscaling happens, scaling is left to the panel. Not all
1409  *              drivers expose this mode.
1410  *      Full:
1411  *              The output is upscaled to the full resolution of the panel,
1412  *              ignoring the aspect ratio.
1413  *      Center:
1414  *              No upscaling happens, the output is centered within the native
1415  *              resolution the panel.
1416  *      Full aspect:
1417  *              The output is upscaled to maximize either the width or height
1418  *              while retaining the aspect ratio.
1419  *
1420  *      This property should be set up by calling
1421  *      drm_connector_attach_scaling_mode_property(). Note that drivers
1422  *      can also expose this property to external outputs, in which case they
1423  *      must support "None", which should be the default (since external screens
1424  *      have a built-in scaler).
1425  *
1426  * subconnector:
1427  *      This property is used by DVI-I, TVout and DisplayPort to indicate different
1428  *      connector subtypes. Enum values more or less match with those from main
1429  *      connector types.
1430  *      For DVI-I and TVout there is also a matching property "select subconnector"
1431  *      allowing to switch between signal types.
1432  *      DP subconnector corresponds to a downstream port.
1433  *
1434  * privacy-screen sw-state, privacy-screen hw-state:
1435  *      These 2 optional properties can be used to query the state of the
1436  *      electronic privacy screen that is available on some displays; and in
1437  *      some cases also control the state. If a driver implements these
1438  *      properties then both properties must be present.
1439  *
1440  *      "privacy-screen hw-state" is read-only and reflects the actual state
1441  *      of the privacy-screen, possible values: "Enabled", "Disabled,
1442  *      "Enabled-locked", "Disabled-locked". The locked states indicate
1443  *      that the state cannot be changed through the DRM API. E.g. there
1444  *      might be devices where the firmware-setup options, or a hardware
1445  *      slider-switch, offer always on / off modes.
1446  *
1447  *      "privacy-screen sw-state" can be set to change the privacy-screen state
1448  *      when not locked. In this case the driver must update the hw-state
1449  *      property to reflect the new state on completion of the commit of the
1450  *      sw-state property. Setting the sw-state property when the hw-state is
1451  *      locked must be interpreted by the driver as a request to change the
1452  *      state to the set state when the hw-state becomes unlocked. E.g. if
1453  *      "privacy-screen hw-state" is "Enabled-locked" and the sw-state
1454  *      gets set to "Disabled" followed by the user unlocking the state by
1455  *      changing the slider-switch position, then the driver must set the
1456  *      state to "Disabled" upon receiving the unlock event.
1457  *
1458  *      In some cases the privacy-screen's actual state might change outside of
1459  *      control of the DRM code. E.g. there might be a firmware handled hotkey
1460  *      which toggles the actual state, or the actual state might be changed
1461  *      through another userspace API such as writing /proc/acpi/ibm/lcdshadow.
1462  *      In this case the driver must update both the hw-state and the sw-state
1463  *      to reflect the new value, overwriting any pending state requests in the
1464  *      sw-state. Any pending sw-state requests are thus discarded.
1465  *
1466  *      Note that the ability for the state to change outside of control of
1467  *      the DRM master process means that userspace must not cache the value
1468  *      of the sw-state. Caching the sw-state value and including it in later
1469  *      atomic commits may lead to overriding a state change done through e.g.
1470  *      a firmware handled hotkey. Therefor userspace must not include the
1471  *      privacy-screen sw-state in an atomic commit unless it wants to change
1472  *      its value.
1473  *
1474  * left margin, right margin, top margin, bottom margin:
1475  *      Add margins to the connector's viewport. This is typically used to
1476  *      mitigate overscan on TVs.
1477  *
1478  *      The value is the size in pixels of the black border which will be
1479  *      added. The attached CRTC's content will be scaled to fill the whole
1480  *      area inside the margin.
1481  *
1482  *      The margins configuration might be sent to the sink, e.g. via HDMI AVI
1483  *      InfoFrames.
1484  *
1485  *      Drivers can set up these properties by calling
1486  *      drm_mode_create_tv_margin_properties().
1487  */
1488
1489 int drm_connector_create_standard_properties(struct drm_device *dev)
1490 {
1491         struct drm_property *prop;
1492
1493         prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1494                                    DRM_MODE_PROP_IMMUTABLE,
1495                                    "EDID", 0);
1496         if (!prop)
1497                 return -ENOMEM;
1498         dev->mode_config.edid_property = prop;
1499
1500         prop = drm_property_create_enum(dev, 0,
1501                                    "DPMS", drm_dpms_enum_list,
1502                                    ARRAY_SIZE(drm_dpms_enum_list));
1503         if (!prop)
1504                 return -ENOMEM;
1505         dev->mode_config.dpms_property = prop;
1506
1507         prop = drm_property_create(dev,
1508                                    DRM_MODE_PROP_BLOB |
1509                                    DRM_MODE_PROP_IMMUTABLE,
1510                                    "PATH", 0);
1511         if (!prop)
1512                 return -ENOMEM;
1513         dev->mode_config.path_property = prop;
1514
1515         prop = drm_property_create(dev,
1516                                    DRM_MODE_PROP_BLOB |
1517                                    DRM_MODE_PROP_IMMUTABLE,
1518                                    "TILE", 0);
1519         if (!prop)
1520                 return -ENOMEM;
1521         dev->mode_config.tile_property = prop;
1522
1523         prop = drm_property_create_enum(dev, 0, "link-status",
1524                                         drm_link_status_enum_list,
1525                                         ARRAY_SIZE(drm_link_status_enum_list));
1526         if (!prop)
1527                 return -ENOMEM;
1528         dev->mode_config.link_status_property = prop;
1529
1530         prop = drm_property_create_bool(dev, DRM_MODE_PROP_IMMUTABLE, "non-desktop");
1531         if (!prop)
1532                 return -ENOMEM;
1533         dev->mode_config.non_desktop_property = prop;
1534
1535         prop = drm_property_create(dev, DRM_MODE_PROP_BLOB,
1536                                    "HDR_OUTPUT_METADATA", 0);
1537         if (!prop)
1538                 return -ENOMEM;
1539         dev->mode_config.hdr_output_metadata_property = prop;
1540
1541         return 0;
1542 }
1543
1544 /**
1545  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1546  * @dev: DRM device
1547  *
1548  * Called by a driver the first time a DVI-I connector is made.
1549  *
1550  * Returns: %0
1551  */
1552 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1553 {
1554         struct drm_property *dvi_i_selector;
1555         struct drm_property *dvi_i_subconnector;
1556
1557         if (dev->mode_config.dvi_i_select_subconnector_property)
1558                 return 0;
1559
1560         dvi_i_selector =
1561                 drm_property_create_enum(dev, 0,
1562                                     "select subconnector",
1563                                     drm_dvi_i_select_enum_list,
1564                                     ARRAY_SIZE(drm_dvi_i_select_enum_list));
1565         dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1566
1567         dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1568                                     "subconnector",
1569                                     drm_dvi_i_subconnector_enum_list,
1570                                     ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1571         dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1572
1573         return 0;
1574 }
1575 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1576
1577 /**
1578  * drm_connector_attach_dp_subconnector_property - create subconnector property for DP
1579  * @connector: drm_connector to attach property
1580  *
1581  * Called by a driver when DP connector is created.
1582  */
1583 void drm_connector_attach_dp_subconnector_property(struct drm_connector *connector)
1584 {
1585         struct drm_mode_config *mode_config = &connector->dev->mode_config;
1586
1587         if (!mode_config->dp_subconnector_property)
1588                 mode_config->dp_subconnector_property =
1589                         drm_property_create_enum(connector->dev,
1590                                 DRM_MODE_PROP_IMMUTABLE,
1591                                 "subconnector",
1592                                 drm_dp_subconnector_enum_list,
1593                                 ARRAY_SIZE(drm_dp_subconnector_enum_list));
1594
1595         drm_object_attach_property(&connector->base,
1596                                    mode_config->dp_subconnector_property,
1597                                    DRM_MODE_SUBCONNECTOR_Unknown);
1598 }
1599 EXPORT_SYMBOL(drm_connector_attach_dp_subconnector_property);
1600
1601 /**
1602  * DOC: HDMI connector properties
1603  *
1604  * content type (HDMI specific):
1605  *      Indicates content type setting to be used in HDMI infoframes to indicate
1606  *      content type for the external device, so that it adjusts its display
1607  *      settings accordingly.
1608  *
1609  *      The value of this property can be one of the following:
1610  *
1611  *      No Data:
1612  *              Content type is unknown
1613  *      Graphics:
1614  *              Content type is graphics
1615  *      Photo:
1616  *              Content type is photo
1617  *      Cinema:
1618  *              Content type is cinema
1619  *      Game:
1620  *              Content type is game
1621  *
1622  *      The meaning of each content type is defined in CTA-861-G table 15.
1623  *
1624  *      Drivers can set up this property by calling
1625  *      drm_connector_attach_content_type_property(). Decoding to
1626  *      infoframe values is done through drm_hdmi_avi_infoframe_content_type().
1627  */
1628
1629 /*
1630  * TODO: Document the properties:
1631  *   - brightness
1632  *   - contrast
1633  *   - flicker reduction
1634  *   - hue
1635  *   - mode
1636  *   - overscan
1637  *   - saturation
1638  *   - select subconnector
1639  */
1640 /**
1641  * DOC: Analog TV Connector Properties
1642  *
1643  * TV Mode:
1644  *      Indicates the TV Mode used on an analog TV connector. The value
1645  *      of this property can be one of the following:
1646  *
1647  *      NTSC:
1648  *              TV Mode is CCIR System M (aka 525-lines) together with
1649  *              the NTSC Color Encoding.
1650  *
1651  *      NTSC-443:
1652  *
1653  *              TV Mode is CCIR System M (aka 525-lines) together with
1654  *              the NTSC Color Encoding, but with a color subcarrier
1655  *              frequency of 4.43MHz
1656  *
1657  *      NTSC-J:
1658  *
1659  *              TV Mode is CCIR System M (aka 525-lines) together with
1660  *              the NTSC Color Encoding, but with a black level equal to
1661  *              the blanking level.
1662  *
1663  *      PAL:
1664  *
1665  *              TV Mode is CCIR System B (aka 625-lines) together with
1666  *              the PAL Color Encoding.
1667  *
1668  *      PAL-M:
1669  *
1670  *              TV Mode is CCIR System M (aka 525-lines) together with
1671  *              the PAL Color Encoding.
1672  *
1673  *      PAL-N:
1674  *
1675  *              TV Mode is CCIR System N together with the PAL Color
1676  *              Encoding, a color subcarrier frequency of 3.58MHz, the
1677  *              SECAM color space, and narrower channels than other PAL
1678  *              variants.
1679  *
1680  *      SECAM:
1681  *
1682  *              TV Mode is CCIR System B (aka 625-lines) together with
1683  *              the SECAM Color Encoding.
1684  *
1685  *      Drivers can set up this property by calling
1686  *      drm_mode_create_tv_properties().
1687  */
1688
1689 /**
1690  * drm_connector_attach_content_type_property - attach content-type property
1691  * @connector: connector to attach content type property on.
1692  *
1693  * Called by a driver the first time a HDMI connector is made.
1694  *
1695  * Returns: %0
1696  */
1697 int drm_connector_attach_content_type_property(struct drm_connector *connector)
1698 {
1699         if (!drm_mode_create_content_type_property(connector->dev))
1700                 drm_object_attach_property(&connector->base,
1701                                            connector->dev->mode_config.content_type_property,
1702                                            DRM_MODE_CONTENT_TYPE_NO_DATA);
1703         return 0;
1704 }
1705 EXPORT_SYMBOL(drm_connector_attach_content_type_property);
1706
1707 /**
1708  * drm_connector_attach_tv_margin_properties - attach TV connector margin
1709  *      properties
1710  * @connector: DRM connector
1711  *
1712  * Called by a driver when it needs to attach TV margin props to a connector.
1713  * Typically used on SDTV and HDMI connectors.
1714  */
1715 void drm_connector_attach_tv_margin_properties(struct drm_connector *connector)
1716 {
1717         struct drm_device *dev = connector->dev;
1718
1719         drm_object_attach_property(&connector->base,
1720                                    dev->mode_config.tv_left_margin_property,
1721                                    0);
1722         drm_object_attach_property(&connector->base,
1723                                    dev->mode_config.tv_right_margin_property,
1724                                    0);
1725         drm_object_attach_property(&connector->base,
1726                                    dev->mode_config.tv_top_margin_property,
1727                                    0);
1728         drm_object_attach_property(&connector->base,
1729                                    dev->mode_config.tv_bottom_margin_property,
1730                                    0);
1731 }
1732 EXPORT_SYMBOL(drm_connector_attach_tv_margin_properties);
1733
1734 /**
1735  * drm_mode_create_tv_margin_properties - create TV connector margin properties
1736  * @dev: DRM device
1737  *
1738  * Called by a driver's HDMI connector initialization routine, this function
1739  * creates the TV margin properties for a given device. No need to call this
1740  * function for an SDTV connector, it's already called from
1741  * drm_mode_create_tv_properties_legacy().
1742  *
1743  * Returns:
1744  * 0 on success or a negative error code on failure.
1745  */
1746 int drm_mode_create_tv_margin_properties(struct drm_device *dev)
1747 {
1748         if (dev->mode_config.tv_left_margin_property)
1749                 return 0;
1750
1751         dev->mode_config.tv_left_margin_property =
1752                 drm_property_create_range(dev, 0, "left margin", 0, 100);
1753         if (!dev->mode_config.tv_left_margin_property)
1754                 return -ENOMEM;
1755
1756         dev->mode_config.tv_right_margin_property =
1757                 drm_property_create_range(dev, 0, "right margin", 0, 100);
1758         if (!dev->mode_config.tv_right_margin_property)
1759                 return -ENOMEM;
1760
1761         dev->mode_config.tv_top_margin_property =
1762                 drm_property_create_range(dev, 0, "top margin", 0, 100);
1763         if (!dev->mode_config.tv_top_margin_property)
1764                 return -ENOMEM;
1765
1766         dev->mode_config.tv_bottom_margin_property =
1767                 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1768         if (!dev->mode_config.tv_bottom_margin_property)
1769                 return -ENOMEM;
1770
1771         return 0;
1772 }
1773 EXPORT_SYMBOL(drm_mode_create_tv_margin_properties);
1774
1775 /**
1776  * drm_mode_create_tv_properties_legacy - create TV specific connector properties
1777  * @dev: DRM device
1778  * @num_modes: number of different TV formats (modes) supported
1779  * @modes: array of pointers to strings containing name of each format
1780  *
1781  * Called by a driver's TV initialization routine, this function creates
1782  * the TV specific connector properties for a given device.  Caller is
1783  * responsible for allocating a list of format names and passing them to
1784  * this routine.
1785  *
1786  * NOTE: This functions registers the deprecated "mode" connector
1787  * property to select the analog TV mode (ie, NTSC, PAL, etc.). New
1788  * drivers must use drm_mode_create_tv_properties() instead.
1789  *
1790  * Returns:
1791  * 0 on success or a negative error code on failure.
1792  */
1793 int drm_mode_create_tv_properties_legacy(struct drm_device *dev,
1794                                          unsigned int num_modes,
1795                                          const char * const modes[])
1796 {
1797         struct drm_property *tv_selector;
1798         struct drm_property *tv_subconnector;
1799         unsigned int i;
1800
1801         if (dev->mode_config.tv_select_subconnector_property)
1802                 return 0;
1803
1804         /*
1805          * Basic connector properties
1806          */
1807         tv_selector = drm_property_create_enum(dev, 0,
1808                                           "select subconnector",
1809                                           drm_tv_select_enum_list,
1810                                           ARRAY_SIZE(drm_tv_select_enum_list));
1811         if (!tv_selector)
1812                 goto nomem;
1813
1814         dev->mode_config.tv_select_subconnector_property = tv_selector;
1815
1816         tv_subconnector =
1817                 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1818                                     "subconnector",
1819                                     drm_tv_subconnector_enum_list,
1820                                     ARRAY_SIZE(drm_tv_subconnector_enum_list));
1821         if (!tv_subconnector)
1822                 goto nomem;
1823         dev->mode_config.tv_subconnector_property = tv_subconnector;
1824
1825         /*
1826          * Other, TV specific properties: margins & TV modes.
1827          */
1828         if (drm_mode_create_tv_margin_properties(dev))
1829                 goto nomem;
1830
1831         if (num_modes) {
1832                 dev->mode_config.legacy_tv_mode_property =
1833                         drm_property_create(dev, DRM_MODE_PROP_ENUM,
1834                                             "mode", num_modes);
1835                 if (!dev->mode_config.legacy_tv_mode_property)
1836                         goto nomem;
1837
1838                 for (i = 0; i < num_modes; i++)
1839                         drm_property_add_enum(dev->mode_config.legacy_tv_mode_property,
1840                                               i, modes[i]);
1841         }
1842
1843         dev->mode_config.tv_brightness_property =
1844                 drm_property_create_range(dev, 0, "brightness", 0, 100);
1845         if (!dev->mode_config.tv_brightness_property)
1846                 goto nomem;
1847
1848         dev->mode_config.tv_contrast_property =
1849                 drm_property_create_range(dev, 0, "contrast", 0, 100);
1850         if (!dev->mode_config.tv_contrast_property)
1851                 goto nomem;
1852
1853         dev->mode_config.tv_flicker_reduction_property =
1854                 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1855         if (!dev->mode_config.tv_flicker_reduction_property)
1856                 goto nomem;
1857
1858         dev->mode_config.tv_overscan_property =
1859                 drm_property_create_range(dev, 0, "overscan", 0, 100);
1860         if (!dev->mode_config.tv_overscan_property)
1861                 goto nomem;
1862
1863         dev->mode_config.tv_saturation_property =
1864                 drm_property_create_range(dev, 0, "saturation", 0, 100);
1865         if (!dev->mode_config.tv_saturation_property)
1866                 goto nomem;
1867
1868         dev->mode_config.tv_hue_property =
1869                 drm_property_create_range(dev, 0, "hue", 0, 100);
1870         if (!dev->mode_config.tv_hue_property)
1871                 goto nomem;
1872
1873         return 0;
1874 nomem:
1875         return -ENOMEM;
1876 }
1877 EXPORT_SYMBOL(drm_mode_create_tv_properties_legacy);
1878
1879 /**
1880  * drm_mode_create_tv_properties - create TV specific connector properties
1881  * @dev: DRM device
1882  * @supported_tv_modes: Bitmask of TV modes supported (See DRM_MODE_TV_MODE_*)
1883  *
1884  * Called by a driver's TV initialization routine, this function creates
1885  * the TV specific connector properties for a given device.
1886  *
1887  * Returns:
1888  * 0 on success or a negative error code on failure.
1889  */
1890 int drm_mode_create_tv_properties(struct drm_device *dev,
1891                                   unsigned int supported_tv_modes)
1892 {
1893         struct drm_prop_enum_list tv_mode_list[DRM_MODE_TV_MODE_MAX];
1894         struct drm_property *tv_mode;
1895         unsigned int i, len = 0;
1896
1897         if (dev->mode_config.tv_mode_property)
1898                 return 0;
1899
1900         for (i = 0; i < DRM_MODE_TV_MODE_MAX; i++) {
1901                 if (!(supported_tv_modes & BIT(i)))
1902                         continue;
1903
1904                 tv_mode_list[len].type = i;
1905                 tv_mode_list[len].name = drm_get_tv_mode_name(i);
1906                 len++;
1907         }
1908
1909         tv_mode = drm_property_create_enum(dev, 0, "TV mode",
1910                                            tv_mode_list, len);
1911         if (!tv_mode)
1912                 return -ENOMEM;
1913
1914         dev->mode_config.tv_mode_property = tv_mode;
1915
1916         return drm_mode_create_tv_properties_legacy(dev, 0, NULL);
1917 }
1918 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1919
1920 /**
1921  * drm_mode_create_scaling_mode_property - create scaling mode property
1922  * @dev: DRM device
1923  *
1924  * Called by a driver the first time it's needed, must be attached to desired
1925  * connectors.
1926  *
1927  * Atomic drivers should use drm_connector_attach_scaling_mode_property()
1928  * instead to correctly assign &drm_connector_state.scaling_mode
1929  * in the atomic state.
1930  *
1931  * Returns: %0
1932  */
1933 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1934 {
1935         struct drm_property *scaling_mode;
1936
1937         if (dev->mode_config.scaling_mode_property)
1938                 return 0;
1939
1940         scaling_mode =
1941                 drm_property_create_enum(dev, 0, "scaling mode",
1942                                 drm_scaling_mode_enum_list,
1943                                     ARRAY_SIZE(drm_scaling_mode_enum_list));
1944
1945         dev->mode_config.scaling_mode_property = scaling_mode;
1946
1947         return 0;
1948 }
1949 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1950
1951 /**
1952  * DOC: Variable refresh properties
1953  *
1954  * Variable refresh rate capable displays can dynamically adjust their
1955  * refresh rate by extending the duration of their vertical front porch
1956  * until page flip or timeout occurs. This can reduce or remove stuttering
1957  * and latency in scenarios where the page flip does not align with the
1958  * vblank interval.
1959  *
1960  * An example scenario would be an application flipping at a constant rate
1961  * of 48Hz on a 60Hz display. The page flip will frequently miss the vblank
1962  * interval and the same contents will be displayed twice. This can be
1963  * observed as stuttering for content with motion.
1964  *
1965  * If variable refresh rate was active on a display that supported a
1966  * variable refresh range from 35Hz to 60Hz no stuttering would be observable
1967  * for the example scenario. The minimum supported variable refresh rate of
1968  * 35Hz is below the page flip frequency and the vertical front porch can
1969  * be extended until the page flip occurs. The vblank interval will be
1970  * directly aligned to the page flip rate.
1971  *
1972  * Not all userspace content is suitable for use with variable refresh rate.
1973  * Large and frequent changes in vertical front porch duration may worsen
1974  * perceived stuttering for input sensitive applications.
1975  *
1976  * Panel brightness will also vary with vertical front porch duration. Some
1977  * panels may have noticeable differences in brightness between the minimum
1978  * vertical front porch duration and the maximum vertical front porch duration.
1979  * Large and frequent changes in vertical front porch duration may produce
1980  * observable flickering for such panels.
1981  *
1982  * Userspace control for variable refresh rate is supported via properties
1983  * on the &drm_connector and &drm_crtc objects.
1984  *
1985  * "vrr_capable":
1986  *      Optional &drm_connector boolean property that drivers should attach
1987  *      with drm_connector_attach_vrr_capable_property() on connectors that
1988  *      could support variable refresh rates. Drivers should update the
1989  *      property value by calling drm_connector_set_vrr_capable_property().
1990  *
1991  *      Absence of the property should indicate absence of support.
1992  *
1993  * "VRR_ENABLED":
1994  *      Default &drm_crtc boolean property that notifies the driver that the
1995  *      content on the CRTC is suitable for variable refresh rate presentation.
1996  *      The driver will take this property as a hint to enable variable
1997  *      refresh rate support if the receiver supports it, ie. if the
1998  *      "vrr_capable" property is true on the &drm_connector object. The
1999  *      vertical front porch duration will be extended until page-flip or
2000  *      timeout when enabled.
2001  *
2002  *      The minimum vertical front porch duration is defined as the vertical
2003  *      front porch duration for the current mode.
2004  *
2005  *      The maximum vertical front porch duration is greater than or equal to
2006  *      the minimum vertical front porch duration. The duration is derived
2007  *      from the minimum supported variable refresh rate for the connector.
2008  *
2009  *      The driver may place further restrictions within these minimum
2010  *      and maximum bounds.
2011  */
2012
2013 /**
2014  * drm_connector_attach_vrr_capable_property - creates the
2015  * vrr_capable property
2016  * @connector: connector to create the vrr_capable property on.
2017  *
2018  * This is used by atomic drivers to add support for querying
2019  * variable refresh rate capability for a connector.
2020  *
2021  * Returns:
2022  * Zero on success, negative errno on failure.
2023  */
2024 int drm_connector_attach_vrr_capable_property(
2025         struct drm_connector *connector)
2026 {
2027         struct drm_device *dev = connector->dev;
2028         struct drm_property *prop;
2029
2030         if (!connector->vrr_capable_property) {
2031                 prop = drm_property_create_bool(dev, DRM_MODE_PROP_IMMUTABLE,
2032                         "vrr_capable");
2033                 if (!prop)
2034                         return -ENOMEM;
2035
2036                 connector->vrr_capable_property = prop;
2037                 drm_object_attach_property(&connector->base, prop, 0);
2038         }
2039
2040         return 0;
2041 }
2042 EXPORT_SYMBOL(drm_connector_attach_vrr_capable_property);
2043
2044 /**
2045  * drm_connector_attach_scaling_mode_property - attach atomic scaling mode property
2046  * @connector: connector to attach scaling mode property on.
2047  * @scaling_mode_mask: or'ed mask of BIT(%DRM_MODE_SCALE_\*).
2048  *
2049  * This is used to add support for scaling mode to atomic drivers.
2050  * The scaling mode will be set to &drm_connector_state.scaling_mode
2051  * and can be used from &drm_connector_helper_funcs->atomic_check for validation.
2052  *
2053  * This is the atomic version of drm_mode_create_scaling_mode_property().
2054  *
2055  * Returns:
2056  * Zero on success, negative errno on failure.
2057  */
2058 int drm_connector_attach_scaling_mode_property(struct drm_connector *connector,
2059                                                u32 scaling_mode_mask)
2060 {
2061         struct drm_device *dev = connector->dev;
2062         struct drm_property *scaling_mode_property;
2063         int i;
2064         const unsigned valid_scaling_mode_mask =
2065                 (1U << ARRAY_SIZE(drm_scaling_mode_enum_list)) - 1;
2066
2067         if (WARN_ON(hweight32(scaling_mode_mask) < 2 ||
2068                     scaling_mode_mask & ~valid_scaling_mode_mask))
2069                 return -EINVAL;
2070
2071         scaling_mode_property =
2072                 drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
2073                                     hweight32(scaling_mode_mask));
2074
2075         if (!scaling_mode_property)
2076                 return -ENOMEM;
2077
2078         for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++) {
2079                 int ret;
2080
2081                 if (!(BIT(i) & scaling_mode_mask))
2082                         continue;
2083
2084                 ret = drm_property_add_enum(scaling_mode_property,
2085                                             drm_scaling_mode_enum_list[i].type,
2086                                             drm_scaling_mode_enum_list[i].name);
2087
2088                 if (ret) {
2089                         drm_property_destroy(dev, scaling_mode_property);
2090
2091                         return ret;
2092                 }
2093         }
2094
2095         drm_object_attach_property(&connector->base,
2096                                    scaling_mode_property, 0);
2097
2098         connector->scaling_mode_property = scaling_mode_property;
2099
2100         return 0;
2101 }
2102 EXPORT_SYMBOL(drm_connector_attach_scaling_mode_property);
2103
2104 /**
2105  * drm_mode_create_aspect_ratio_property - create aspect ratio property
2106  * @dev: DRM device
2107  *
2108  * Called by a driver the first time it's needed, must be attached to desired
2109  * connectors.
2110  *
2111  * Returns:
2112  * Zero on success, negative errno on failure.
2113  */
2114 int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
2115 {
2116         if (dev->mode_config.aspect_ratio_property)
2117                 return 0;
2118
2119         dev->mode_config.aspect_ratio_property =
2120                 drm_property_create_enum(dev, 0, "aspect ratio",
2121                                 drm_aspect_ratio_enum_list,
2122                                 ARRAY_SIZE(drm_aspect_ratio_enum_list));
2123
2124         if (dev->mode_config.aspect_ratio_property == NULL)
2125                 return -ENOMEM;
2126
2127         return 0;
2128 }
2129 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
2130
2131 /**
2132  * DOC: standard connector properties
2133  *
2134  * Colorspace:
2135  *     This property helps select a suitable colorspace based on the sink
2136  *     capability. Modern sink devices support wider gamut like BT2020.
2137  *     This helps switch to BT2020 mode if the BT2020 encoded video stream
2138  *     is being played by the user, same for any other colorspace. Thereby
2139  *     giving a good visual experience to users.
2140  *
2141  *     The expectation from userspace is that it should parse the EDID
2142  *     and get supported colorspaces. Use this property and switch to the
2143  *     one supported. Sink supported colorspaces should be retrieved by
2144  *     userspace from EDID and driver will not explicitly expose them.
2145  *
2146  *     Basically the expectation from userspace is:
2147  *      - Set up CRTC DEGAMMA/CTM/GAMMA to convert to some sink
2148  *        colorspace
2149  *      - Set this new property to let the sink know what it
2150  *        converted the CRTC output to.
2151  *      - This property is just to inform sink what colorspace
2152  *        source is trying to drive.
2153  *
2154  * Because between HDMI and DP have different colorspaces,
2155  * drm_mode_create_hdmi_colorspace_property() is used for HDMI connector and
2156  * drm_mode_create_dp_colorspace_property() is used for DP connector.
2157  */
2158
2159 static int drm_mode_create_colorspace_property(struct drm_connector *connector,
2160                                         u32 supported_colorspaces)
2161 {
2162         struct drm_device *dev = connector->dev;
2163         u32 colorspaces = supported_colorspaces | BIT(DRM_MODE_COLORIMETRY_DEFAULT);
2164         struct drm_prop_enum_list enum_list[DRM_MODE_COLORIMETRY_COUNT];
2165         int i, len;
2166
2167         if (connector->colorspace_property)
2168                 return 0;
2169
2170         if (!supported_colorspaces) {
2171                 drm_err(dev, "No supported colorspaces provded on [CONNECTOR:%d:%s]\n",
2172                             connector->base.id, connector->name);
2173                 return -EINVAL;
2174         }
2175
2176         if ((supported_colorspaces & -BIT(DRM_MODE_COLORIMETRY_COUNT)) != 0) {
2177                 drm_err(dev, "Unknown colorspace provded on [CONNECTOR:%d:%s]\n",
2178                             connector->base.id, connector->name);
2179                 return -EINVAL;
2180         }
2181
2182         len = 0;
2183         for (i = 0; i < DRM_MODE_COLORIMETRY_COUNT; i++) {
2184                 if ((colorspaces & BIT(i)) == 0)
2185                         continue;
2186
2187                 enum_list[len].type = i;
2188                 enum_list[len].name = colorspace_names[i];
2189                 len++;
2190         }
2191
2192         connector->colorspace_property =
2193                 drm_property_create_enum(dev, DRM_MODE_PROP_ENUM, "Colorspace",
2194                                         enum_list,
2195                                         len);
2196
2197         if (!connector->colorspace_property)
2198                 return -ENOMEM;
2199
2200         return 0;
2201 }
2202
2203 /**
2204  * drm_mode_create_hdmi_colorspace_property - create hdmi colorspace property
2205  * @connector: connector to create the Colorspace property on.
2206  * @supported_colorspaces: bitmap of supported color spaces
2207  *
2208  * Called by a driver the first time it's needed, must be attached to desired
2209  * HDMI connectors.
2210  *
2211  * Returns:
2212  * Zero on success, negative errno on failure.
2213  */
2214 int drm_mode_create_hdmi_colorspace_property(struct drm_connector *connector,
2215                                              u32 supported_colorspaces)
2216 {
2217         u32 colorspaces;
2218
2219         if (supported_colorspaces)
2220                 colorspaces = supported_colorspaces & hdmi_colorspaces;
2221         else
2222                 colorspaces = hdmi_colorspaces;
2223
2224         return drm_mode_create_colorspace_property(connector, colorspaces);
2225 }
2226 EXPORT_SYMBOL(drm_mode_create_hdmi_colorspace_property);
2227
2228 /**
2229  * drm_mode_create_dp_colorspace_property - create dp colorspace property
2230  * @connector: connector to create the Colorspace property on.
2231  * @supported_colorspaces: bitmap of supported color spaces
2232  *
2233  * Called by a driver the first time it's needed, must be attached to desired
2234  * DP connectors.
2235  *
2236  * Returns:
2237  * Zero on success, negative errno on failure.
2238  */
2239 int drm_mode_create_dp_colorspace_property(struct drm_connector *connector,
2240                                            u32 supported_colorspaces)
2241 {
2242         u32 colorspaces;
2243
2244         if (supported_colorspaces)
2245                 colorspaces = supported_colorspaces & dp_colorspaces;
2246         else
2247                 colorspaces = dp_colorspaces;
2248
2249         return drm_mode_create_colorspace_property(connector, colorspaces);
2250 }
2251 EXPORT_SYMBOL(drm_mode_create_dp_colorspace_property);
2252
2253 /**
2254  * drm_mode_create_content_type_property - create content type property
2255  * @dev: DRM device
2256  *
2257  * Called by a driver the first time it's needed, must be attached to desired
2258  * connectors.
2259  *
2260  * Returns:
2261  * Zero on success, negative errno on failure.
2262  */
2263 int drm_mode_create_content_type_property(struct drm_device *dev)
2264 {
2265         if (dev->mode_config.content_type_property)
2266                 return 0;
2267
2268         dev->mode_config.content_type_property =
2269                 drm_property_create_enum(dev, 0, "content type",
2270                                          drm_content_type_enum_list,
2271                                          ARRAY_SIZE(drm_content_type_enum_list));
2272
2273         if (dev->mode_config.content_type_property == NULL)
2274                 return -ENOMEM;
2275
2276         return 0;
2277 }
2278 EXPORT_SYMBOL(drm_mode_create_content_type_property);
2279
2280 /**
2281  * drm_mode_create_suggested_offset_properties - create suggests offset properties
2282  * @dev: DRM device
2283  *
2284  * Create the suggested x/y offset property for connectors.
2285  *
2286  * Returns:
2287  * 0 on success or a negative error code on failure.
2288  */
2289 int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
2290 {
2291         if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
2292                 return 0;
2293
2294         dev->mode_config.suggested_x_property =
2295                 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
2296
2297         dev->mode_config.suggested_y_property =
2298                 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
2299
2300         if (dev->mode_config.suggested_x_property == NULL ||
2301             dev->mode_config.suggested_y_property == NULL)
2302                 return -ENOMEM;
2303         return 0;
2304 }
2305 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
2306
2307 /**
2308  * drm_connector_set_path_property - set tile property on connector
2309  * @connector: connector to set property on.
2310  * @path: path to use for property; must not be NULL.
2311  *
2312  * This creates a property to expose to userspace to specify a
2313  * connector path. This is mainly used for DisplayPort MST where
2314  * connectors have a topology and we want to allow userspace to give
2315  * them more meaningful names.
2316  *
2317  * Returns:
2318  * Zero on success, negative errno on failure.
2319  */
2320 int drm_connector_set_path_property(struct drm_connector *connector,
2321                                     const char *path)
2322 {
2323         struct drm_device *dev = connector->dev;
2324         int ret;
2325
2326         ret = drm_property_replace_global_blob(dev,
2327                                                &connector->path_blob_ptr,
2328                                                strlen(path) + 1,
2329                                                path,
2330                                                &connector->base,
2331                                                dev->mode_config.path_property);
2332         return ret;
2333 }
2334 EXPORT_SYMBOL(drm_connector_set_path_property);
2335
2336 /**
2337  * drm_connector_set_tile_property - set tile property on connector
2338  * @connector: connector to set property on.
2339  *
2340  * This looks up the tile information for a connector, and creates a
2341  * property for userspace to parse if it exists. The property is of
2342  * the form of 8 integers using ':' as a separator.
2343  * This is used for dual port tiled displays with DisplayPort SST
2344  * or DisplayPort MST connectors.
2345  *
2346  * Returns:
2347  * Zero on success, errno on failure.
2348  */
2349 int drm_connector_set_tile_property(struct drm_connector *connector)
2350 {
2351         struct drm_device *dev = connector->dev;
2352         char tile[256];
2353         int ret;
2354
2355         if (!connector->has_tile) {
2356                 ret  = drm_property_replace_global_blob(dev,
2357                                                         &connector->tile_blob_ptr,
2358                                                         0,
2359                                                         NULL,
2360                                                         &connector->base,
2361                                                         dev->mode_config.tile_property);
2362                 return ret;
2363         }
2364
2365         snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
2366                  connector->tile_group->id, connector->tile_is_single_monitor,
2367                  connector->num_h_tile, connector->num_v_tile,
2368                  connector->tile_h_loc, connector->tile_v_loc,
2369                  connector->tile_h_size, connector->tile_v_size);
2370
2371         ret = drm_property_replace_global_blob(dev,
2372                                                &connector->tile_blob_ptr,
2373                                                strlen(tile) + 1,
2374                                                tile,
2375                                                &connector->base,
2376                                                dev->mode_config.tile_property);
2377         return ret;
2378 }
2379 EXPORT_SYMBOL(drm_connector_set_tile_property);
2380
2381 /**
2382  * drm_connector_set_link_status_property - Set link status property of a connector
2383  * @connector: drm connector
2384  * @link_status: new value of link status property (0: Good, 1: Bad)
2385  *
2386  * In usual working scenario, this link status property will always be set to
2387  * "GOOD". If something fails during or after a mode set, the kernel driver
2388  * may set this link status property to "BAD". The caller then needs to send a
2389  * hotplug uevent for userspace to re-check the valid modes through
2390  * GET_CONNECTOR_IOCTL and retry modeset.
2391  *
2392  * Note: Drivers cannot rely on userspace to support this property and
2393  * issue a modeset. As such, they may choose to handle issues (like
2394  * re-training a link) without userspace's intervention.
2395  *
2396  * The reason for adding this property is to handle link training failures, but
2397  * it is not limited to DP or link training. For example, if we implement
2398  * asynchronous setcrtc, this property can be used to report any failures in that.
2399  */
2400 void drm_connector_set_link_status_property(struct drm_connector *connector,
2401                                             uint64_t link_status)
2402 {
2403         struct drm_device *dev = connector->dev;
2404
2405         drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2406         connector->state->link_status = link_status;
2407         drm_modeset_unlock(&dev->mode_config.connection_mutex);
2408 }
2409 EXPORT_SYMBOL(drm_connector_set_link_status_property);
2410
2411 /**
2412  * drm_connector_attach_max_bpc_property - attach "max bpc" property
2413  * @connector: connector to attach max bpc property on.
2414  * @min: The minimum bit depth supported by the connector.
2415  * @max: The maximum bit depth supported by the connector.
2416  *
2417  * This is used to add support for limiting the bit depth on a connector.
2418  *
2419  * Returns:
2420  * Zero on success, negative errno on failure.
2421  */
2422 int drm_connector_attach_max_bpc_property(struct drm_connector *connector,
2423                                           int min, int max)
2424 {
2425         struct drm_device *dev = connector->dev;
2426         struct drm_property *prop;
2427
2428         prop = connector->max_bpc_property;
2429         if (!prop) {
2430                 prop = drm_property_create_range(dev, 0, "max bpc", min, max);
2431                 if (!prop)
2432                         return -ENOMEM;
2433
2434                 connector->max_bpc_property = prop;
2435         }
2436
2437         drm_object_attach_property(&connector->base, prop, max);
2438         connector->state->max_requested_bpc = max;
2439         connector->state->max_bpc = max;
2440
2441         return 0;
2442 }
2443 EXPORT_SYMBOL(drm_connector_attach_max_bpc_property);
2444
2445 /**
2446  * drm_connector_attach_hdr_output_metadata_property - attach "HDR_OUTPUT_METADA" property
2447  * @connector: connector to attach the property on.
2448  *
2449  * This is used to allow the userspace to send HDR Metadata to the
2450  * driver.
2451  *
2452  * Returns:
2453  * Zero on success, negative errno on failure.
2454  */
2455 int drm_connector_attach_hdr_output_metadata_property(struct drm_connector *connector)
2456 {
2457         struct drm_device *dev = connector->dev;
2458         struct drm_property *prop = dev->mode_config.hdr_output_metadata_property;
2459
2460         drm_object_attach_property(&connector->base, prop, 0);
2461
2462         return 0;
2463 }
2464 EXPORT_SYMBOL(drm_connector_attach_hdr_output_metadata_property);
2465
2466 /**
2467  * drm_connector_attach_colorspace_property - attach "Colorspace" property
2468  * @connector: connector to attach the property on.
2469  *
2470  * This is used to allow the userspace to signal the output colorspace
2471  * to the driver.
2472  *
2473  * Returns:
2474  * Zero on success, negative errno on failure.
2475  */
2476 int drm_connector_attach_colorspace_property(struct drm_connector *connector)
2477 {
2478         struct drm_property *prop = connector->colorspace_property;
2479
2480         drm_object_attach_property(&connector->base, prop, DRM_MODE_COLORIMETRY_DEFAULT);
2481
2482         return 0;
2483 }
2484 EXPORT_SYMBOL(drm_connector_attach_colorspace_property);
2485
2486 /**
2487  * drm_connector_atomic_hdr_metadata_equal - checks if the hdr metadata changed
2488  * @old_state: old connector state to compare
2489  * @new_state: new connector state to compare
2490  *
2491  * This is used by HDR-enabled drivers to test whether the HDR metadata
2492  * have changed between two different connector state (and thus probably
2493  * requires a full blown mode change).
2494  *
2495  * Returns:
2496  * True if the metadata are equal, False otherwise
2497  */
2498 bool drm_connector_atomic_hdr_metadata_equal(struct drm_connector_state *old_state,
2499                                              struct drm_connector_state *new_state)
2500 {
2501         struct drm_property_blob *old_blob = old_state->hdr_output_metadata;
2502         struct drm_property_blob *new_blob = new_state->hdr_output_metadata;
2503
2504         if (!old_blob || !new_blob)
2505                 return old_blob == new_blob;
2506
2507         if (old_blob->length != new_blob->length)
2508                 return false;
2509
2510         return !memcmp(old_blob->data, new_blob->data, old_blob->length);
2511 }
2512 EXPORT_SYMBOL(drm_connector_atomic_hdr_metadata_equal);
2513
2514 /**
2515  * drm_connector_set_vrr_capable_property - sets the variable refresh rate
2516  * capable property for a connector
2517  * @connector: drm connector
2518  * @capable: True if the connector is variable refresh rate capable
2519  *
2520  * Should be used by atomic drivers to update the indicated support for
2521  * variable refresh rate over a connector.
2522  */
2523 void drm_connector_set_vrr_capable_property(
2524                 struct drm_connector *connector, bool capable)
2525 {
2526         if (!connector->vrr_capable_property)
2527                 return;
2528
2529         drm_object_property_set_value(&connector->base,
2530                                       connector->vrr_capable_property,
2531                                       capable);
2532 }
2533 EXPORT_SYMBOL(drm_connector_set_vrr_capable_property);
2534
2535 /**
2536  * drm_connector_set_panel_orientation - sets the connector's panel_orientation
2537  * @connector: connector for which to set the panel-orientation property.
2538  * @panel_orientation: drm_panel_orientation value to set
2539  *
2540  * This function sets the connector's panel_orientation and attaches
2541  * a "panel orientation" property to the connector.
2542  *
2543  * Calling this function on a connector where the panel_orientation has
2544  * already been set is a no-op (e.g. the orientation has been overridden with
2545  * a kernel commandline option).
2546  *
2547  * It is allowed to call this function with a panel_orientation of
2548  * DRM_MODE_PANEL_ORIENTATION_UNKNOWN, in which case it is a no-op.
2549  *
2550  * The function shouldn't be called in panel after drm is registered (i.e.
2551  * drm_dev_register() is called in drm).
2552  *
2553  * Returns:
2554  * Zero on success, negative errno on failure.
2555  */
2556 int drm_connector_set_panel_orientation(
2557         struct drm_connector *connector,
2558         enum drm_panel_orientation panel_orientation)
2559 {
2560         struct drm_device *dev = connector->dev;
2561         struct drm_display_info *info = &connector->display_info;
2562         struct drm_property *prop;
2563
2564         /* Already set? */
2565         if (info->panel_orientation != DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
2566                 return 0;
2567
2568         /* Don't attach the property if the orientation is unknown */
2569         if (panel_orientation == DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
2570                 return 0;
2571
2572         info->panel_orientation = panel_orientation;
2573
2574         prop = dev->mode_config.panel_orientation_property;
2575         if (!prop) {
2576                 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
2577                                 "panel orientation",
2578                                 drm_panel_orientation_enum_list,
2579                                 ARRAY_SIZE(drm_panel_orientation_enum_list));
2580                 if (!prop)
2581                         return -ENOMEM;
2582
2583                 dev->mode_config.panel_orientation_property = prop;
2584         }
2585
2586         drm_object_attach_property(&connector->base, prop,
2587                                    info->panel_orientation);
2588         return 0;
2589 }
2590 EXPORT_SYMBOL(drm_connector_set_panel_orientation);
2591
2592 /**
2593  * drm_connector_set_panel_orientation_with_quirk - set the
2594  *      connector's panel_orientation after checking for quirks
2595  * @connector: connector for which to init the panel-orientation property.
2596  * @panel_orientation: drm_panel_orientation value to set
2597  * @width: width in pixels of the panel, used for panel quirk detection
2598  * @height: height in pixels of the panel, used for panel quirk detection
2599  *
2600  * Like drm_connector_set_panel_orientation(), but with a check for platform
2601  * specific (e.g. DMI based) quirks overriding the passed in panel_orientation.
2602  *
2603  * Returns:
2604  * Zero on success, negative errno on failure.
2605  */
2606 int drm_connector_set_panel_orientation_with_quirk(
2607         struct drm_connector *connector,
2608         enum drm_panel_orientation panel_orientation,
2609         int width, int height)
2610 {
2611         int orientation_quirk;
2612
2613         orientation_quirk = drm_get_panel_orientation_quirk(width, height);
2614         if (orientation_quirk != DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
2615                 panel_orientation = orientation_quirk;
2616
2617         return drm_connector_set_panel_orientation(connector,
2618                                                    panel_orientation);
2619 }
2620 EXPORT_SYMBOL(drm_connector_set_panel_orientation_with_quirk);
2621
2622 /**
2623  * drm_connector_set_orientation_from_panel -
2624  *      set the connector's panel_orientation from panel's callback.
2625  * @connector: connector for which to init the panel-orientation property.
2626  * @panel: panel that can provide orientation information.
2627  *
2628  * Drm drivers should call this function before drm_dev_register().
2629  * Orientation is obtained from panel's .get_orientation() callback.
2630  *
2631  * Returns:
2632  * Zero on success, negative errno on failure.
2633  */
2634 int drm_connector_set_orientation_from_panel(
2635         struct drm_connector *connector,
2636         struct drm_panel *panel)
2637 {
2638         enum drm_panel_orientation orientation;
2639
2640         if (panel && panel->funcs && panel->funcs->get_orientation)
2641                 orientation = panel->funcs->get_orientation(panel);
2642         else
2643                 orientation = DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
2644
2645         return drm_connector_set_panel_orientation(connector, orientation);
2646 }
2647 EXPORT_SYMBOL(drm_connector_set_orientation_from_panel);
2648
2649 static const struct drm_prop_enum_list privacy_screen_enum[] = {
2650         { PRIVACY_SCREEN_DISABLED,              "Disabled" },
2651         { PRIVACY_SCREEN_ENABLED,               "Enabled" },
2652         { PRIVACY_SCREEN_DISABLED_LOCKED,       "Disabled-locked" },
2653         { PRIVACY_SCREEN_ENABLED_LOCKED,        "Enabled-locked" },
2654 };
2655
2656 /**
2657  * drm_connector_create_privacy_screen_properties - create the drm connecter's
2658  *    privacy-screen properties.
2659  * @connector: connector for which to create the privacy-screen properties
2660  *
2661  * This function creates the "privacy-screen sw-state" and "privacy-screen
2662  * hw-state" properties for the connector. They are not attached.
2663  */
2664 void
2665 drm_connector_create_privacy_screen_properties(struct drm_connector *connector)
2666 {
2667         if (connector->privacy_screen_sw_state_property)
2668                 return;
2669
2670         /* Note sw-state only supports the first 2 values of the enum */
2671         connector->privacy_screen_sw_state_property =
2672                 drm_property_create_enum(connector->dev, DRM_MODE_PROP_ENUM,
2673                                 "privacy-screen sw-state",
2674                                 privacy_screen_enum, 2);
2675
2676         connector->privacy_screen_hw_state_property =
2677                 drm_property_create_enum(connector->dev,
2678                                 DRM_MODE_PROP_IMMUTABLE | DRM_MODE_PROP_ENUM,
2679                                 "privacy-screen hw-state",
2680                                 privacy_screen_enum,
2681                                 ARRAY_SIZE(privacy_screen_enum));
2682 }
2683 EXPORT_SYMBOL(drm_connector_create_privacy_screen_properties);
2684
2685 /**
2686  * drm_connector_attach_privacy_screen_properties - attach the drm connecter's
2687  *    privacy-screen properties.
2688  * @connector: connector on which to attach the privacy-screen properties
2689  *
2690  * This function attaches the "privacy-screen sw-state" and "privacy-screen
2691  * hw-state" properties to the connector. The initial state of both is set
2692  * to "Disabled".
2693  */
2694 void
2695 drm_connector_attach_privacy_screen_properties(struct drm_connector *connector)
2696 {
2697         if (!connector->privacy_screen_sw_state_property)
2698                 return;
2699
2700         drm_object_attach_property(&connector->base,
2701                                    connector->privacy_screen_sw_state_property,
2702                                    PRIVACY_SCREEN_DISABLED);
2703
2704         drm_object_attach_property(&connector->base,
2705                                    connector->privacy_screen_hw_state_property,
2706                                    PRIVACY_SCREEN_DISABLED);
2707 }
2708 EXPORT_SYMBOL(drm_connector_attach_privacy_screen_properties);
2709
2710 static void drm_connector_update_privacy_screen_properties(
2711         struct drm_connector *connector, bool set_sw_state)
2712 {
2713         enum drm_privacy_screen_status sw_state, hw_state;
2714
2715         drm_privacy_screen_get_state(connector->privacy_screen,
2716                                      &sw_state, &hw_state);
2717
2718         if (set_sw_state)
2719                 connector->state->privacy_screen_sw_state = sw_state;
2720         drm_object_property_set_value(&connector->base,
2721                         connector->privacy_screen_hw_state_property, hw_state);
2722 }
2723
2724 static int drm_connector_privacy_screen_notifier(
2725         struct notifier_block *nb, unsigned long action, void *data)
2726 {
2727         struct drm_connector *connector =
2728                 container_of(nb, struct drm_connector, privacy_screen_notifier);
2729         struct drm_device *dev = connector->dev;
2730
2731         drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2732         drm_connector_update_privacy_screen_properties(connector, true);
2733         drm_modeset_unlock(&dev->mode_config.connection_mutex);
2734
2735         drm_sysfs_connector_property_event(connector,
2736                                            connector->privacy_screen_sw_state_property);
2737         drm_sysfs_connector_property_event(connector,
2738                                            connector->privacy_screen_hw_state_property);
2739
2740         return NOTIFY_DONE;
2741 }
2742
2743 /**
2744  * drm_connector_attach_privacy_screen_provider - attach a privacy-screen to
2745  *    the connector
2746  * @connector: connector to attach the privacy-screen to
2747  * @priv: drm_privacy_screen to attach
2748  *
2749  * Create and attach the standard privacy-screen properties and register
2750  * a generic notifier for generating sysfs-connector-status-events
2751  * on external changes to the privacy-screen status.
2752  * This function takes ownership of the passed in drm_privacy_screen and will
2753  * call drm_privacy_screen_put() on it when the connector is destroyed.
2754  */
2755 void drm_connector_attach_privacy_screen_provider(
2756         struct drm_connector *connector, struct drm_privacy_screen *priv)
2757 {
2758         connector->privacy_screen = priv;
2759         connector->privacy_screen_notifier.notifier_call =
2760                 drm_connector_privacy_screen_notifier;
2761
2762         drm_connector_create_privacy_screen_properties(connector);
2763         drm_connector_update_privacy_screen_properties(connector, true);
2764         drm_connector_attach_privacy_screen_properties(connector);
2765 }
2766 EXPORT_SYMBOL(drm_connector_attach_privacy_screen_provider);
2767
2768 /**
2769  * drm_connector_update_privacy_screen - update connector's privacy-screen sw-state
2770  * @connector_state: connector-state to update the privacy-screen for
2771  *
2772  * This function calls drm_privacy_screen_set_sw_state() on the connector's
2773  * privacy-screen.
2774  *
2775  * If the connector has no privacy-screen, then this is a no-op.
2776  */
2777 void drm_connector_update_privacy_screen(const struct drm_connector_state *connector_state)
2778 {
2779         struct drm_connector *connector = connector_state->connector;
2780         int ret;
2781
2782         if (!connector->privacy_screen)
2783                 return;
2784
2785         ret = drm_privacy_screen_set_sw_state(connector->privacy_screen,
2786                                               connector_state->privacy_screen_sw_state);
2787         if (ret) {
2788                 drm_err(connector->dev, "Error updating privacy-screen sw_state\n");
2789                 return;
2790         }
2791
2792         /* The hw_state property value may have changed, update it. */
2793         drm_connector_update_privacy_screen_properties(connector, false);
2794 }
2795 EXPORT_SYMBOL(drm_connector_update_privacy_screen);
2796
2797 int drm_connector_set_obj_prop(struct drm_mode_object *obj,
2798                                     struct drm_property *property,
2799                                     uint64_t value)
2800 {
2801         int ret = -EINVAL;
2802         struct drm_connector *connector = obj_to_connector(obj);
2803
2804         /* Do DPMS ourselves */
2805         if (property == connector->dev->mode_config.dpms_property) {
2806                 ret = (*connector->funcs->dpms)(connector, (int)value);
2807         } else if (connector->funcs->set_property)
2808                 ret = connector->funcs->set_property(connector, property, value);
2809
2810         if (!ret)
2811                 drm_object_property_set_value(&connector->base, property, value);
2812         return ret;
2813 }
2814
2815 int drm_connector_property_set_ioctl(struct drm_device *dev,
2816                                      void *data, struct drm_file *file_priv)
2817 {
2818         struct drm_mode_connector_set_property *conn_set_prop = data;
2819         struct drm_mode_obj_set_property obj_set_prop = {
2820                 .value = conn_set_prop->value,
2821                 .prop_id = conn_set_prop->prop_id,
2822                 .obj_id = conn_set_prop->connector_id,
2823                 .obj_type = DRM_MODE_OBJECT_CONNECTOR
2824         };
2825
2826         /* It does all the locking and checking we need */
2827         return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
2828 }
2829
2830 static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
2831 {
2832         /* For atomic drivers only state objects are synchronously updated and
2833          * protected by modeset locks, so check those first.
2834          */
2835         if (connector->state)
2836                 return connector->state->best_encoder;
2837         return connector->encoder;
2838 }
2839
2840 static bool
2841 drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
2842                              const struct list_head *modes,
2843                              const struct drm_file *file_priv)
2844 {
2845         /*
2846          * If user-space hasn't configured the driver to expose the stereo 3D
2847          * modes, don't expose them.
2848          */
2849         if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
2850                 return false;
2851         /*
2852          * If user-space hasn't configured the driver to expose the modes
2853          * with aspect-ratio, don't expose them. However if such a mode
2854          * is unique, let it be exposed, but reset the aspect-ratio flags
2855          * while preparing the list of user-modes.
2856          */
2857         if (!file_priv->aspect_ratio_allowed) {
2858                 const struct drm_display_mode *mode_itr;
2859
2860                 list_for_each_entry(mode_itr, modes, head) {
2861                         if (mode_itr->expose_to_userspace &&
2862                             drm_mode_match(mode_itr, mode,
2863                                            DRM_MODE_MATCH_TIMINGS |
2864                                            DRM_MODE_MATCH_CLOCK |
2865                                            DRM_MODE_MATCH_FLAGS |
2866                                            DRM_MODE_MATCH_3D_FLAGS))
2867                                 return false;
2868                 }
2869         }
2870
2871         return true;
2872 }
2873
2874 int drm_mode_getconnector(struct drm_device *dev, void *data,
2875                           struct drm_file *file_priv)
2876 {
2877         struct drm_mode_get_connector *out_resp = data;
2878         struct drm_connector *connector;
2879         struct drm_encoder *encoder;
2880         struct drm_display_mode *mode;
2881         int mode_count = 0;
2882         int encoders_count = 0;
2883         int ret = 0;
2884         int copied = 0;
2885         struct drm_mode_modeinfo u_mode;
2886         struct drm_mode_modeinfo __user *mode_ptr;
2887         uint32_t __user *encoder_ptr;
2888         bool is_current_master;
2889
2890         if (!drm_core_check_feature(dev, DRIVER_MODESET))
2891                 return -EOPNOTSUPP;
2892
2893         memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2894
2895         connector = drm_connector_lookup(dev, file_priv, out_resp->connector_id);
2896         if (!connector)
2897                 return -ENOENT;
2898
2899         encoders_count = hweight32(connector->possible_encoders);
2900
2901         if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2902                 copied = 0;
2903                 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
2904
2905                 drm_connector_for_each_possible_encoder(connector, encoder) {
2906                         if (put_user(encoder->base.id, encoder_ptr + copied)) {
2907                                 ret = -EFAULT;
2908                                 goto out;
2909                         }
2910                         copied++;
2911                 }
2912         }
2913         out_resp->count_encoders = encoders_count;
2914
2915         out_resp->connector_id = connector->base.id;
2916         out_resp->connector_type = connector->connector_type;
2917         out_resp->connector_type_id = connector->connector_type_id;
2918
2919         is_current_master = drm_is_current_master(file_priv);
2920
2921         mutex_lock(&dev->mode_config.mutex);
2922         if (out_resp->count_modes == 0) {
2923                 if (is_current_master)
2924                         connector->funcs->fill_modes(connector,
2925                                                      dev->mode_config.max_width,
2926                                                      dev->mode_config.max_height);
2927                 else
2928                         drm_dbg_kms(dev, "User-space requested a forced probe on [CONNECTOR:%d:%s] but is not the DRM master, demoting to read-only probe",
2929                                     connector->base.id, connector->name);
2930         }
2931
2932         out_resp->mm_width = connector->display_info.width_mm;
2933         out_resp->mm_height = connector->display_info.height_mm;
2934         out_resp->subpixel = connector->display_info.subpixel_order;
2935         out_resp->connection = connector->status;
2936
2937         /* delayed so we get modes regardless of pre-fill_modes state */
2938         list_for_each_entry(mode, &connector->modes, head) {
2939                 WARN_ON(mode->expose_to_userspace);
2940
2941                 if (drm_mode_expose_to_userspace(mode, &connector->modes,
2942                                                  file_priv)) {
2943                         mode->expose_to_userspace = true;
2944                         mode_count++;
2945                 }
2946         }
2947
2948         /*
2949          * This ioctl is called twice, once to determine how much space is
2950          * needed, and the 2nd time to fill it.
2951          */
2952         if ((out_resp->count_modes >= mode_count) && mode_count) {
2953                 copied = 0;
2954                 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
2955                 list_for_each_entry(mode, &connector->modes, head) {
2956                         if (!mode->expose_to_userspace)
2957                                 continue;
2958
2959                         /* Clear the tag for the next time around */
2960                         mode->expose_to_userspace = false;
2961
2962                         drm_mode_convert_to_umode(&u_mode, mode);
2963                         /*
2964                          * Reset aspect ratio flags of user-mode, if modes with
2965                          * aspect-ratio are not supported.
2966                          */
2967                         if (!file_priv->aspect_ratio_allowed)
2968                                 u_mode.flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
2969                         if (copy_to_user(mode_ptr + copied,
2970                                          &u_mode, sizeof(u_mode))) {
2971                                 ret = -EFAULT;
2972
2973                                 /*
2974                                  * Clear the tag for the rest of
2975                                  * the modes for the next time around.
2976                                  */
2977                                 list_for_each_entry_continue(mode, &connector->modes, head)
2978                                         mode->expose_to_userspace = false;
2979
2980                                 mutex_unlock(&dev->mode_config.mutex);
2981
2982                                 goto out;
2983                         }
2984                         copied++;
2985                 }
2986         } else {
2987                 /* Clear the tag for the next time around */
2988                 list_for_each_entry(mode, &connector->modes, head)
2989                         mode->expose_to_userspace = false;
2990         }
2991
2992         out_resp->count_modes = mode_count;
2993         mutex_unlock(&dev->mode_config.mutex);
2994
2995         drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2996         encoder = drm_connector_get_encoder(connector);
2997         if (encoder)
2998                 out_resp->encoder_id = encoder->base.id;
2999         else
3000                 out_resp->encoder_id = 0;
3001
3002         /* Only grab properties after probing, to make sure EDID and other
3003          * properties reflect the latest status.
3004          */
3005         ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
3006                         (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
3007                         (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
3008                         &out_resp->count_props);
3009         drm_modeset_unlock(&dev->mode_config.connection_mutex);
3010
3011 out:
3012         drm_connector_put(connector);
3013
3014         return ret;
3015 }
3016
3017 /**
3018  * drm_connector_find_by_fwnode - Find a connector based on the associated fwnode
3019  * @fwnode: fwnode for which to find the matching drm_connector
3020  *
3021  * This functions looks up a drm_connector based on its associated fwnode. When
3022  * a connector is found a reference to the connector is returned. The caller must
3023  * call drm_connector_put() to release this reference when it is done with the
3024  * connector.
3025  *
3026  * Returns: A reference to the found connector or an ERR_PTR().
3027  */
3028 struct drm_connector *drm_connector_find_by_fwnode(struct fwnode_handle *fwnode)
3029 {
3030         struct drm_connector *connector, *found = ERR_PTR(-ENODEV);
3031
3032         if (!fwnode)
3033                 return ERR_PTR(-ENODEV);
3034
3035         mutex_lock(&connector_list_lock);
3036
3037         list_for_each_entry(connector, &connector_list, global_connector_list_entry) {
3038                 if (connector->fwnode == fwnode ||
3039                     (connector->fwnode && connector->fwnode->secondary == fwnode)) {
3040                         drm_connector_get(connector);
3041                         found = connector;
3042                         break;
3043                 }
3044         }
3045
3046         mutex_unlock(&connector_list_lock);
3047
3048         return found;
3049 }
3050
3051 /**
3052  * drm_connector_oob_hotplug_event - Report out-of-band hotplug event to connector
3053  * @connector_fwnode: fwnode_handle to report the event on
3054  *
3055  * On some hardware a hotplug event notification may come from outside the display
3056  * driver / device. An example of this is some USB Type-C setups where the hardware
3057  * muxes the DisplayPort data and aux-lines but does not pass the altmode HPD
3058  * status bit to the GPU's DP HPD pin.
3059  *
3060  * This function can be used to report these out-of-band events after obtaining
3061  * a drm_connector reference through calling drm_connector_find_by_fwnode().
3062  */
3063 void drm_connector_oob_hotplug_event(struct fwnode_handle *connector_fwnode)
3064 {
3065         struct drm_connector *connector;
3066
3067         connector = drm_connector_find_by_fwnode(connector_fwnode);
3068         if (IS_ERR(connector))
3069                 return;
3070
3071         if (connector->funcs->oob_hotplug_event)
3072                 connector->funcs->oob_hotplug_event(connector);
3073
3074         drm_connector_put(connector);
3075 }
3076 EXPORT_SYMBOL(drm_connector_oob_hotplug_event);
3077
3078
3079 /**
3080  * DOC: Tile group
3081  *
3082  * Tile groups are used to represent tiled monitors with a unique integer
3083  * identifier. Tiled monitors using DisplayID v1.3 have a unique 8-byte handle,
3084  * we store this in a tile group, so we have a common identifier for all tiles
3085  * in a monitor group. The property is called "TILE". Drivers can manage tile
3086  * groups using drm_mode_create_tile_group(), drm_mode_put_tile_group() and
3087  * drm_mode_get_tile_group(). But this is only needed for internal panels where
3088  * the tile group information is exposed through a non-standard way.
3089  */
3090
3091 static void drm_tile_group_free(struct kref *kref)
3092 {
3093         struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
3094         struct drm_device *dev = tg->dev;
3095
3096         mutex_lock(&dev->mode_config.idr_mutex);
3097         idr_remove(&dev->mode_config.tile_idr, tg->id);
3098         mutex_unlock(&dev->mode_config.idr_mutex);
3099         kfree(tg);
3100 }
3101
3102 /**
3103  * drm_mode_put_tile_group - drop a reference to a tile group.
3104  * @dev: DRM device
3105  * @tg: tile group to drop reference to.
3106  *
3107  * drop reference to tile group and free if 0.
3108  */
3109 void drm_mode_put_tile_group(struct drm_device *dev,
3110                              struct drm_tile_group *tg)
3111 {
3112         kref_put(&tg->refcount, drm_tile_group_free);
3113 }
3114 EXPORT_SYMBOL(drm_mode_put_tile_group);
3115
3116 /**
3117  * drm_mode_get_tile_group - get a reference to an existing tile group
3118  * @dev: DRM device
3119  * @topology: 8-bytes unique per monitor.
3120  *
3121  * Use the unique bytes to get a reference to an existing tile group.
3122  *
3123  * RETURNS:
3124  * tile group or NULL if not found.
3125  */
3126 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
3127                                                const char topology[8])
3128 {
3129         struct drm_tile_group *tg;
3130         int id;
3131
3132         mutex_lock(&dev->mode_config.idr_mutex);
3133         idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
3134                 if (!memcmp(tg->group_data, topology, 8)) {
3135                         if (!kref_get_unless_zero(&tg->refcount))
3136                                 tg = NULL;
3137                         mutex_unlock(&dev->mode_config.idr_mutex);
3138                         return tg;
3139                 }
3140         }
3141         mutex_unlock(&dev->mode_config.idr_mutex);
3142         return NULL;
3143 }
3144 EXPORT_SYMBOL(drm_mode_get_tile_group);
3145
3146 /**
3147  * drm_mode_create_tile_group - create a tile group from a displayid description
3148  * @dev: DRM device
3149  * @topology: 8-bytes unique per monitor.
3150  *
3151  * Create a tile group for the unique monitor, and get a unique
3152  * identifier for the tile group.
3153  *
3154  * RETURNS:
3155  * new tile group or NULL.
3156  */
3157 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
3158                                                   const char topology[8])
3159 {
3160         struct drm_tile_group *tg;
3161         int ret;
3162
3163         tg = kzalloc(sizeof(*tg), GFP_KERNEL);
3164         if (!tg)
3165                 return NULL;
3166
3167         kref_init(&tg->refcount);
3168         memcpy(tg->group_data, topology, 8);
3169         tg->dev = dev;
3170
3171         mutex_lock(&dev->mode_config.idr_mutex);
3172         ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
3173         if (ret >= 0) {
3174                 tg->id = ret;
3175         } else {
3176                 kfree(tg);
3177                 tg = NULL;
3178         }
3179
3180         mutex_unlock(&dev->mode_config.idr_mutex);
3181         return tg;
3182 }
3183 EXPORT_SYMBOL(drm_mode_create_tile_group);