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