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