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