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