GNU Linux-libre 4.14.330-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
28 #include "drm_crtc_internal.h"
29 #include "drm_internal.h"
30
31 /**
32  * DOC: overview
33  *
34  * In DRM connectors are the general abstraction for display sinks, and include
35  * als fixed panels or anything else that can display pixels in some form. As
36  * opposed to all other KMS objects representing hardware (like CRTC, encoder or
37  * plane abstractions) connectors can be hotplugged and unplugged at runtime.
38  * Hence they are reference-counted using drm_connector_get() and
39  * drm_connector_put().
40  *
41  * KMS driver must create, initialize, register and attach at a &struct
42  * drm_connector for each such sink. The instance is created as other KMS
43  * objects and initialized by setting the following fields. The connector is
44  * initialized with a call to drm_connector_init() with a pointer to the
45  * &struct drm_connector_funcs and a connector type, and then exposed to
46  * userspace with a call to drm_connector_register().
47  *
48  * Connectors must be attached to an encoder to be used. For devices that map
49  * connectors to encoders 1:1, the connector should be attached at
50  * initialization time with a call to drm_mode_connector_attach_encoder(). The
51  * driver must also set the &drm_connector.encoder field to point to the
52  * attached encoder.
53  *
54  * For connectors which are not fixed (like built-in panels) the driver needs to
55  * support hotplug notifications. The simplest way to do that is by using the
56  * probe helpers, see drm_kms_helper_poll_init() for connectors which don't have
57  * hardware support for hotplug interrupts. Connectors with hardware hotplug
58  * support can instead use e.g. drm_helper_hpd_irq_event().
59  */
60
61 struct drm_conn_prop_enum_list {
62         int type;
63         const char *name;
64         struct ida ida;
65 };
66
67 /*
68  * Connector and encoder types.
69  */
70 static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
71         { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
72         { DRM_MODE_CONNECTOR_VGA, "VGA" },
73         { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
74         { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
75         { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
76         { DRM_MODE_CONNECTOR_Composite, "Composite" },
77         { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
78         { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
79         { DRM_MODE_CONNECTOR_Component, "Component" },
80         { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
81         { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
82         { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
83         { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
84         { DRM_MODE_CONNECTOR_TV, "TV" },
85         { DRM_MODE_CONNECTOR_eDP, "eDP" },
86         { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
87         { DRM_MODE_CONNECTOR_DSI, "DSI" },
88         { DRM_MODE_CONNECTOR_DPI, "DPI" },
89 };
90
91 void drm_connector_ida_init(void)
92 {
93         int i;
94
95         for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
96                 ida_init(&drm_connector_enum_list[i].ida);
97 }
98
99 void drm_connector_ida_destroy(void)
100 {
101         int i;
102
103         for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
104                 ida_destroy(&drm_connector_enum_list[i].ida);
105 }
106
107 /**
108  * drm_connector_get_cmdline_mode - reads the user's cmdline mode
109  * @connector: connector to quwery
110  *
111  * The kernel supports per-connector configuration of its consoles through
112  * use of the video= parameter. This function parses that option and
113  * extracts the user's specified mode (or enable/disable status) for a
114  * particular connector. This is typically only used during the early fbdev
115  * setup.
116  */
117 static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
118 {
119         struct drm_cmdline_mode *mode = &connector->cmdline_mode;
120         char *option = NULL;
121
122         if (fb_get_options(connector->name, &option))
123                 return;
124
125         if (!drm_mode_parse_command_line_for_connector(option,
126                                                        connector,
127                                                        mode))
128                 return;
129
130         if (mode->force) {
131                 DRM_INFO("forcing %s connector %s\n", connector->name,
132                          drm_get_connector_force_name(mode->force));
133                 connector->force = mode->force;
134         }
135
136         DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
137                       connector->name,
138                       mode->xres, mode->yres,
139                       mode->refresh_specified ? mode->refresh : 60,
140                       mode->rb ? " reduced blanking" : "",
141                       mode->margins ? " with margins" : "",
142                       mode->interlace ?  " interlaced" : "");
143 }
144
145 static void drm_connector_free(struct kref *kref)
146 {
147         struct drm_connector *connector =
148                 container_of(kref, struct drm_connector, base.refcount);
149         struct drm_device *dev = connector->dev;
150
151         drm_mode_object_unregister(dev, &connector->base);
152         connector->funcs->destroy(connector);
153 }
154
155 /**
156  * drm_connector_init - Init a preallocated connector
157  * @dev: DRM device
158  * @connector: the connector to init
159  * @funcs: callbacks for this connector
160  * @connector_type: user visible type of the connector
161  *
162  * Initialises a preallocated connector. Connectors should be
163  * subclassed as part of driver connector objects.
164  *
165  * Returns:
166  * Zero on success, error code on failure.
167  */
168 int drm_connector_init(struct drm_device *dev,
169                        struct drm_connector *connector,
170                        const struct drm_connector_funcs *funcs,
171                        int connector_type)
172 {
173         struct drm_mode_config *config = &dev->mode_config;
174         int ret;
175         struct ida *connector_ida =
176                 &drm_connector_enum_list[connector_type].ida;
177
178         ret = __drm_mode_object_add(dev, &connector->base,
179                                     DRM_MODE_OBJECT_CONNECTOR,
180                                     false, drm_connector_free);
181         if (ret)
182                 return ret;
183
184         connector->base.properties = &connector->properties;
185         connector->dev = dev;
186         connector->funcs = funcs;
187
188         ret = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
189         if (ret < 0)
190                 goto out_put;
191         connector->index = ret;
192         ret = 0;
193
194         connector->connector_type = connector_type;
195         connector->connector_type_id =
196                 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
197         if (connector->connector_type_id < 0) {
198                 ret = connector->connector_type_id;
199                 goto out_put_id;
200         }
201         connector->name =
202                 kasprintf(GFP_KERNEL, "%s-%d",
203                           drm_connector_enum_list[connector_type].name,
204                           connector->connector_type_id);
205         if (!connector->name) {
206                 ret = -ENOMEM;
207                 goto out_put_type_id;
208         }
209
210         INIT_LIST_HEAD(&connector->probed_modes);
211         INIT_LIST_HEAD(&connector->modes);
212         mutex_init(&connector->mutex);
213         connector->edid_blob_ptr = NULL;
214         connector->status = connector_status_unknown;
215
216         drm_connector_get_cmdline_mode(connector);
217
218         /* We should add connectors at the end to avoid upsetting the connector
219          * index too much. */
220         spin_lock_irq(&config->connector_list_lock);
221         list_add_tail(&connector->head, &config->connector_list);
222         config->num_connector++;
223         spin_unlock_irq(&config->connector_list_lock);
224
225         if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
226                 drm_object_attach_property(&connector->base,
227                                               config->edid_property,
228                                               0);
229
230         drm_object_attach_property(&connector->base,
231                                       config->dpms_property, 0);
232
233         drm_object_attach_property(&connector->base,
234                                    config->link_status_property,
235                                    0);
236
237         if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
238                 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
239         }
240
241         connector->debugfs_entry = NULL;
242 out_put_type_id:
243         if (ret)
244                 ida_simple_remove(connector_ida, connector->connector_type_id);
245 out_put_id:
246         if (ret)
247                 ida_simple_remove(&config->connector_ida, connector->index);
248 out_put:
249         if (ret)
250                 drm_mode_object_unregister(dev, &connector->base);
251
252         return ret;
253 }
254 EXPORT_SYMBOL(drm_connector_init);
255
256 /**
257  * drm_mode_connector_attach_encoder - attach a connector to an encoder
258  * @connector: connector to attach
259  * @encoder: encoder to attach @connector to
260  *
261  * This function links up a connector to an encoder. Note that the routing
262  * restrictions between encoders and crtcs are exposed to userspace through the
263  * possible_clones and possible_crtcs bitmasks.
264  *
265  * Returns:
266  * Zero on success, negative errno on failure.
267  */
268 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
269                                       struct drm_encoder *encoder)
270 {
271         int i;
272
273         /*
274          * In the past, drivers have attempted to model the static association
275          * of connector to encoder in simple connector/encoder devices using a
276          * direct assignment of connector->encoder = encoder. This connection
277          * is a logical one and the responsibility of the core, so drivers are
278          * expected not to mess with this.
279          *
280          * Note that the error return should've been enough here, but a large
281          * majority of drivers ignores the return value, so add in a big WARN
282          * to get people's attention.
283          */
284         if (WARN_ON(connector->encoder))
285                 return -EINVAL;
286
287         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
288                 if (connector->encoder_ids[i] == 0) {
289                         connector->encoder_ids[i] = encoder->base.id;
290                         return 0;
291                 }
292         }
293         return -ENOMEM;
294 }
295 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
296
297 static void drm_mode_remove(struct drm_connector *connector,
298                             struct drm_display_mode *mode)
299 {
300         list_del(&mode->head);
301         drm_mode_destroy(connector->dev, mode);
302 }
303
304 /**
305  * drm_connector_cleanup - cleans up an initialised connector
306  * @connector: connector to cleanup
307  *
308  * Cleans up the connector but doesn't free the object.
309  */
310 void drm_connector_cleanup(struct drm_connector *connector)
311 {
312         struct drm_device *dev = connector->dev;
313         struct drm_display_mode *mode, *t;
314
315         /* The connector should have been removed from userspace long before
316          * it is finally destroyed.
317          */
318         if (WARN_ON(connector->registered))
319                 drm_connector_unregister(connector);
320
321         if (connector->tile_group) {
322                 drm_mode_put_tile_group(dev, connector->tile_group);
323                 connector->tile_group = NULL;
324         }
325
326         list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
327                 drm_mode_remove(connector, mode);
328
329         list_for_each_entry_safe(mode, t, &connector->modes, head)
330                 drm_mode_remove(connector, mode);
331
332         ida_simple_remove(&drm_connector_enum_list[connector->connector_type].ida,
333                           connector->connector_type_id);
334
335         ida_simple_remove(&dev->mode_config.connector_ida,
336                           connector->index);
337
338         kfree(connector->display_info.bus_formats);
339         drm_mode_object_unregister(dev, &connector->base);
340         kfree(connector->name);
341         connector->name = NULL;
342         spin_lock_irq(&dev->mode_config.connector_list_lock);
343         list_del(&connector->head);
344         dev->mode_config.num_connector--;
345         spin_unlock_irq(&dev->mode_config.connector_list_lock);
346
347         WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
348         if (connector->state && connector->funcs->atomic_destroy_state)
349                 connector->funcs->atomic_destroy_state(connector,
350                                                        connector->state);
351
352         mutex_destroy(&connector->mutex);
353
354         memset(connector, 0, sizeof(*connector));
355
356         if (dev->registered)
357                 drm_sysfs_hotplug_event(dev);
358 }
359 EXPORT_SYMBOL(drm_connector_cleanup);
360
361 /**
362  * drm_connector_register - register a connector
363  * @connector: the connector to register
364  *
365  * Register userspace interfaces for a connector
366  *
367  * Returns:
368  * Zero on success, error code on failure.
369  */
370 int drm_connector_register(struct drm_connector *connector)
371 {
372         int ret = 0;
373
374         if (!connector->dev->registered)
375                 return 0;
376
377         mutex_lock(&connector->mutex);
378         if (connector->registered)
379                 goto unlock;
380
381         ret = drm_sysfs_connector_add(connector);
382         if (ret)
383                 goto unlock;
384
385         ret = drm_debugfs_connector_add(connector);
386         if (ret) {
387                 goto err_sysfs;
388         }
389
390         if (connector->funcs->late_register) {
391                 ret = connector->funcs->late_register(connector);
392                 if (ret)
393                         goto err_debugfs;
394         }
395
396         drm_mode_object_register(connector->dev, &connector->base);
397
398         connector->registered = true;
399         goto unlock;
400
401 err_debugfs:
402         drm_debugfs_connector_remove(connector);
403 err_sysfs:
404         drm_sysfs_connector_remove(connector);
405 unlock:
406         mutex_unlock(&connector->mutex);
407         return ret;
408 }
409 EXPORT_SYMBOL(drm_connector_register);
410
411 /**
412  * drm_connector_unregister - unregister a connector
413  * @connector: the connector to unregister
414  *
415  * Unregister userspace interfaces for a connector
416  */
417 void drm_connector_unregister(struct drm_connector *connector)
418 {
419         mutex_lock(&connector->mutex);
420         if (!connector->registered) {
421                 mutex_unlock(&connector->mutex);
422                 return;
423         }
424
425         if (connector->funcs->early_unregister)
426                 connector->funcs->early_unregister(connector);
427
428         drm_sysfs_connector_remove(connector);
429         drm_debugfs_connector_remove(connector);
430
431         connector->registered = false;
432         mutex_unlock(&connector->mutex);
433 }
434 EXPORT_SYMBOL(drm_connector_unregister);
435
436 void drm_connector_unregister_all(struct drm_device *dev)
437 {
438         struct drm_connector *connector;
439         struct drm_connector_list_iter conn_iter;
440
441         drm_connector_list_iter_begin(dev, &conn_iter);
442         drm_for_each_connector_iter(connector, &conn_iter)
443                 drm_connector_unregister(connector);
444         drm_connector_list_iter_end(&conn_iter);
445 }
446
447 int drm_connector_register_all(struct drm_device *dev)
448 {
449         struct drm_connector *connector;
450         struct drm_connector_list_iter conn_iter;
451         int ret = 0;
452
453         drm_connector_list_iter_begin(dev, &conn_iter);
454         drm_for_each_connector_iter(connector, &conn_iter) {
455                 ret = drm_connector_register(connector);
456                 if (ret)
457                         break;
458         }
459         drm_connector_list_iter_end(&conn_iter);
460
461         if (ret)
462                 drm_connector_unregister_all(dev);
463         return ret;
464 }
465
466 /**
467  * drm_get_connector_status_name - return a string for connector status
468  * @status: connector status to compute name of
469  *
470  * In contrast to the other drm_get_*_name functions this one here returns a
471  * const pointer and hence is threadsafe.
472  */
473 const char *drm_get_connector_status_name(enum drm_connector_status status)
474 {
475         if (status == connector_status_connected)
476                 return "connected";
477         else if (status == connector_status_disconnected)
478                 return "disconnected";
479         else
480                 return "unknown";
481 }
482 EXPORT_SYMBOL(drm_get_connector_status_name);
483
484 /**
485  * drm_get_connector_force_name - return a string for connector force
486  * @force: connector force to get name of
487  *
488  * Returns: const pointer to name.
489  */
490 const char *drm_get_connector_force_name(enum drm_connector_force force)
491 {
492         switch (force) {
493         case DRM_FORCE_UNSPECIFIED:
494                 return "unspecified";
495         case DRM_FORCE_OFF:
496                 return "off";
497         case DRM_FORCE_ON:
498                 return "on";
499         case DRM_FORCE_ON_DIGITAL:
500                 return "digital";
501         default:
502                 return "unknown";
503         }
504 }
505
506 #ifdef CONFIG_LOCKDEP
507 static struct lockdep_map connector_list_iter_dep_map = {
508         .name = "drm_connector_list_iter"
509 };
510 #endif
511
512 /**
513  * drm_connector_list_iter_begin - initialize a connector_list iterator
514  * @dev: DRM device
515  * @iter: connector_list iterator
516  *
517  * Sets @iter up to walk the &drm_mode_config.connector_list of @dev. @iter
518  * must always be cleaned up again by calling drm_connector_list_iter_end().
519  * Iteration itself happens using drm_connector_list_iter_next() or
520  * drm_for_each_connector_iter().
521  */
522 void drm_connector_list_iter_begin(struct drm_device *dev,
523                                    struct drm_connector_list_iter *iter)
524 {
525         iter->dev = dev;
526         iter->conn = NULL;
527         lock_acquire_shared_recursive(&connector_list_iter_dep_map, 0, 1, NULL, _RET_IP_);
528 }
529 EXPORT_SYMBOL(drm_connector_list_iter_begin);
530
531 /**
532  * drm_connector_list_iter_next - return next connector
533  * @iter: connectr_list iterator
534  *
535  * Returns the next connector for @iter, or NULL when the list walk has
536  * completed.
537  */
538 struct drm_connector *
539 drm_connector_list_iter_next(struct drm_connector_list_iter *iter)
540 {
541         struct drm_connector *old_conn = iter->conn;
542         struct drm_mode_config *config = &iter->dev->mode_config;
543         struct list_head *lhead;
544         unsigned long flags;
545
546         spin_lock_irqsave(&config->connector_list_lock, flags);
547         lhead = old_conn ? &old_conn->head : &config->connector_list;
548
549         do {
550                 if (lhead->next == &config->connector_list) {
551                         iter->conn = NULL;
552                         break;
553                 }
554
555                 lhead = lhead->next;
556                 iter->conn = list_entry(lhead, struct drm_connector, head);
557
558                 /* loop until it's not a zombie connector */
559         } while (!kref_get_unless_zero(&iter->conn->base.refcount));
560         spin_unlock_irqrestore(&config->connector_list_lock, flags);
561
562         if (old_conn)
563                 drm_connector_put(old_conn);
564
565         return iter->conn;
566 }
567 EXPORT_SYMBOL(drm_connector_list_iter_next);
568
569 /**
570  * drm_connector_list_iter_end - tear down a connector_list iterator
571  * @iter: connector_list iterator
572  *
573  * Tears down @iter and releases any resources (like &drm_connector references)
574  * acquired while walking the list. This must always be called, both when the
575  * iteration completes fully or when it was aborted without walking the entire
576  * list.
577  */
578 void drm_connector_list_iter_end(struct drm_connector_list_iter *iter)
579 {
580         iter->dev = NULL;
581         if (iter->conn)
582                 drm_connector_put(iter->conn);
583         lock_release(&connector_list_iter_dep_map, 0, _RET_IP_);
584 }
585 EXPORT_SYMBOL(drm_connector_list_iter_end);
586
587 static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
588         { SubPixelUnknown, "Unknown" },
589         { SubPixelHorizontalRGB, "Horizontal RGB" },
590         { SubPixelHorizontalBGR, "Horizontal BGR" },
591         { SubPixelVerticalRGB, "Vertical RGB" },
592         { SubPixelVerticalBGR, "Vertical BGR" },
593         { SubPixelNone, "None" },
594 };
595
596 /**
597  * drm_get_subpixel_order_name - return a string for a given subpixel enum
598  * @order: enum of subpixel_order
599  *
600  * Note you could abuse this and return something out of bounds, but that
601  * would be a caller error.  No unscrubbed user data should make it here.
602  */
603 const char *drm_get_subpixel_order_name(enum subpixel_order order)
604 {
605         return drm_subpixel_enum_list[order].name;
606 }
607 EXPORT_SYMBOL(drm_get_subpixel_order_name);
608
609 static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
610         { DRM_MODE_DPMS_ON, "On" },
611         { DRM_MODE_DPMS_STANDBY, "Standby" },
612         { DRM_MODE_DPMS_SUSPEND, "Suspend" },
613         { DRM_MODE_DPMS_OFF, "Off" }
614 };
615 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
616
617 static const struct drm_prop_enum_list drm_link_status_enum_list[] = {
618         { DRM_MODE_LINK_STATUS_GOOD, "Good" },
619         { DRM_MODE_LINK_STATUS_BAD, "Bad" },
620 };
621 DRM_ENUM_NAME_FN(drm_get_link_status_name, drm_link_status_enum_list)
622
623 /**
624  * drm_display_info_set_bus_formats - set the supported bus formats
625  * @info: display info to store bus formats in
626  * @formats: array containing the supported bus formats
627  * @num_formats: the number of entries in the fmts array
628  *
629  * Store the supported bus formats in display info structure.
630  * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
631  * a full list of available formats.
632  */
633 int drm_display_info_set_bus_formats(struct drm_display_info *info,
634                                      const u32 *formats,
635                                      unsigned int num_formats)
636 {
637         u32 *fmts = NULL;
638
639         if (!formats && num_formats)
640                 return -EINVAL;
641
642         if (formats && num_formats) {
643                 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
644                                GFP_KERNEL);
645                 if (!fmts)
646                         return -ENOMEM;
647         }
648
649         kfree(info->bus_formats);
650         info->bus_formats = fmts;
651         info->num_bus_formats = num_formats;
652
653         return 0;
654 }
655 EXPORT_SYMBOL(drm_display_info_set_bus_formats);
656
657 /* Optional connector properties. */
658 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
659         { DRM_MODE_SCALE_NONE, "None" },
660         { DRM_MODE_SCALE_FULLSCREEN, "Full" },
661         { DRM_MODE_SCALE_CENTER, "Center" },
662         { DRM_MODE_SCALE_ASPECT, "Full aspect" },
663 };
664
665 static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
666         { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
667         { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
668         { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
669 };
670
671 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
672         { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
673         { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
674         { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
675 };
676 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
677
678 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
679         { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
680         { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
681         { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
682 };
683 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
684                  drm_dvi_i_subconnector_enum_list)
685
686 static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
687         { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
688         { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
689         { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
690         { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
691         { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
692 };
693 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
694
695 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
696         { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
697         { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
698         { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
699         { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
700         { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
701 };
702 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
703                  drm_tv_subconnector_enum_list)
704
705 /**
706  * DOC: standard connector properties
707  *
708  * DRM connectors have a few standardized properties:
709  *
710  * EDID:
711  *      Blob property which contains the current EDID read from the sink. This
712  *      is useful to parse sink identification information like vendor, model
713  *      and serial. Drivers should update this property by calling
714  *      drm_mode_connector_update_edid_property(), usually after having parsed
715  *      the EDID using drm_add_edid_modes(). Userspace cannot change this
716  *      property.
717  * DPMS:
718  *      Legacy property for setting the power state of the connector. For atomic
719  *      drivers this is only provided for backwards compatibility with existing
720  *      drivers, it remaps to controlling the "ACTIVE" property on the CRTC the
721  *      connector is linked to. Drivers should never set this property directly,
722  *      it is handled by the DRM core by calling the &drm_connector_funcs.dpms
723  *      callback. For atomic drivers the remapping to the "ACTIVE" property is
724  *      implemented in the DRM core.  This is the only standard connector
725  *      property that userspace can change.
726  * PATH:
727  *      Connector path property to identify how this sink is physically
728  *      connected. Used by DP MST. This should be set by calling
729  *      drm_mode_connector_set_path_property(), in the case of DP MST with the
730  *      path property the MST manager created. Userspace cannot change this
731  *      property.
732  * TILE:
733  *      Connector tile group property to indicate how a set of DRM connector
734  *      compose together into one logical screen. This is used by both high-res
735  *      external screens (often only using a single cable, but exposing multiple
736  *      DP MST sinks), or high-res integrated panels (like dual-link DSI) which
737  *      are not gen-locked. Note that for tiled panels which are genlocked, like
738  *      dual-link LVDS or dual-link DSI, the driver should try to not expose the
739  *      tiling and virtualize both &drm_crtc and &drm_plane if needed. Drivers
740  *      should update this value using drm_mode_connector_set_tile_property().
741  *      Userspace cannot change this property.
742  * link-status:
743  *      Connector link-status property to indicate the status of link. The default
744  *      value of link-status is "GOOD". If something fails during or after modeset,
745  *      the kernel driver may set this to "BAD" and issue a hotplug uevent. Drivers
746  *      should update this value using drm_mode_connector_set_link_status_property().
747  *
748  * Connectors also have one standardized atomic property:
749  *
750  * CRTC_ID:
751  *      Mode object ID of the &drm_crtc this connector should be connected to.
752  */
753
754 int drm_connector_create_standard_properties(struct drm_device *dev)
755 {
756         struct drm_property *prop;
757
758         prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
759                                    DRM_MODE_PROP_IMMUTABLE,
760                                    "EDID", 0);
761         if (!prop)
762                 return -ENOMEM;
763         dev->mode_config.edid_property = prop;
764
765         prop = drm_property_create_enum(dev, 0,
766                                    "DPMS", drm_dpms_enum_list,
767                                    ARRAY_SIZE(drm_dpms_enum_list));
768         if (!prop)
769                 return -ENOMEM;
770         dev->mode_config.dpms_property = prop;
771
772         prop = drm_property_create(dev,
773                                    DRM_MODE_PROP_BLOB |
774                                    DRM_MODE_PROP_IMMUTABLE,
775                                    "PATH", 0);
776         if (!prop)
777                 return -ENOMEM;
778         dev->mode_config.path_property = prop;
779
780         prop = drm_property_create(dev,
781                                    DRM_MODE_PROP_BLOB |
782                                    DRM_MODE_PROP_IMMUTABLE,
783                                    "TILE", 0);
784         if (!prop)
785                 return -ENOMEM;
786         dev->mode_config.tile_property = prop;
787
788         prop = drm_property_create_enum(dev, 0, "link-status",
789                                         drm_link_status_enum_list,
790                                         ARRAY_SIZE(drm_link_status_enum_list));
791         if (!prop)
792                 return -ENOMEM;
793         dev->mode_config.link_status_property = prop;
794
795         return 0;
796 }
797
798 /**
799  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
800  * @dev: DRM device
801  *
802  * Called by a driver the first time a DVI-I connector is made.
803  */
804 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
805 {
806         struct drm_property *dvi_i_selector;
807         struct drm_property *dvi_i_subconnector;
808
809         if (dev->mode_config.dvi_i_select_subconnector_property)
810                 return 0;
811
812         dvi_i_selector =
813                 drm_property_create_enum(dev, 0,
814                                     "select subconnector",
815                                     drm_dvi_i_select_enum_list,
816                                     ARRAY_SIZE(drm_dvi_i_select_enum_list));
817         dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
818
819         dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
820                                     "subconnector",
821                                     drm_dvi_i_subconnector_enum_list,
822                                     ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
823         dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
824
825         return 0;
826 }
827 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
828
829 /**
830  * drm_create_tv_properties - create TV specific connector properties
831  * @dev: DRM device
832  * @num_modes: number of different TV formats (modes) supported
833  * @modes: array of pointers to strings containing name of each format
834  *
835  * Called by a driver's TV initialization routine, this function creates
836  * the TV specific connector properties for a given device.  Caller is
837  * responsible for allocating a list of format names and passing them to
838  * this routine.
839  */
840 int drm_mode_create_tv_properties(struct drm_device *dev,
841                                   unsigned int num_modes,
842                                   const char * const modes[])
843 {
844         struct drm_property *tv_selector;
845         struct drm_property *tv_subconnector;
846         unsigned int i;
847
848         if (dev->mode_config.tv_select_subconnector_property)
849                 return 0;
850
851         /*
852          * Basic connector properties
853          */
854         tv_selector = drm_property_create_enum(dev, 0,
855                                           "select subconnector",
856                                           drm_tv_select_enum_list,
857                                           ARRAY_SIZE(drm_tv_select_enum_list));
858         if (!tv_selector)
859                 goto nomem;
860
861         dev->mode_config.tv_select_subconnector_property = tv_selector;
862
863         tv_subconnector =
864                 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
865                                     "subconnector",
866                                     drm_tv_subconnector_enum_list,
867                                     ARRAY_SIZE(drm_tv_subconnector_enum_list));
868         if (!tv_subconnector)
869                 goto nomem;
870         dev->mode_config.tv_subconnector_property = tv_subconnector;
871
872         /*
873          * Other, TV specific properties: margins & TV modes.
874          */
875         dev->mode_config.tv_left_margin_property =
876                 drm_property_create_range(dev, 0, "left margin", 0, 100);
877         if (!dev->mode_config.tv_left_margin_property)
878                 goto nomem;
879
880         dev->mode_config.tv_right_margin_property =
881                 drm_property_create_range(dev, 0, "right margin", 0, 100);
882         if (!dev->mode_config.tv_right_margin_property)
883                 goto nomem;
884
885         dev->mode_config.tv_top_margin_property =
886                 drm_property_create_range(dev, 0, "top margin", 0, 100);
887         if (!dev->mode_config.tv_top_margin_property)
888                 goto nomem;
889
890         dev->mode_config.tv_bottom_margin_property =
891                 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
892         if (!dev->mode_config.tv_bottom_margin_property)
893                 goto nomem;
894
895         dev->mode_config.tv_mode_property =
896                 drm_property_create(dev, DRM_MODE_PROP_ENUM,
897                                     "mode", num_modes);
898         if (!dev->mode_config.tv_mode_property)
899                 goto nomem;
900
901         for (i = 0; i < num_modes; i++)
902                 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
903                                       i, modes[i]);
904
905         dev->mode_config.tv_brightness_property =
906                 drm_property_create_range(dev, 0, "brightness", 0, 100);
907         if (!dev->mode_config.tv_brightness_property)
908                 goto nomem;
909
910         dev->mode_config.tv_contrast_property =
911                 drm_property_create_range(dev, 0, "contrast", 0, 100);
912         if (!dev->mode_config.tv_contrast_property)
913                 goto nomem;
914
915         dev->mode_config.tv_flicker_reduction_property =
916                 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
917         if (!dev->mode_config.tv_flicker_reduction_property)
918                 goto nomem;
919
920         dev->mode_config.tv_overscan_property =
921                 drm_property_create_range(dev, 0, "overscan", 0, 100);
922         if (!dev->mode_config.tv_overscan_property)
923                 goto nomem;
924
925         dev->mode_config.tv_saturation_property =
926                 drm_property_create_range(dev, 0, "saturation", 0, 100);
927         if (!dev->mode_config.tv_saturation_property)
928                 goto nomem;
929
930         dev->mode_config.tv_hue_property =
931                 drm_property_create_range(dev, 0, "hue", 0, 100);
932         if (!dev->mode_config.tv_hue_property)
933                 goto nomem;
934
935         return 0;
936 nomem:
937         return -ENOMEM;
938 }
939 EXPORT_SYMBOL(drm_mode_create_tv_properties);
940
941 /**
942  * drm_mode_create_scaling_mode_property - create scaling mode property
943  * @dev: DRM device
944  *
945  * Called by a driver the first time it's needed, must be attached to desired
946  * connectors.
947  *
948  * Atomic drivers should use drm_connector_attach_scaling_mode_property()
949  * instead to correctly assign &drm_connector_state.picture_aspect_ratio
950  * in the atomic state.
951  */
952 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
953 {
954         struct drm_property *scaling_mode;
955
956         if (dev->mode_config.scaling_mode_property)
957                 return 0;
958
959         scaling_mode =
960                 drm_property_create_enum(dev, 0, "scaling mode",
961                                 drm_scaling_mode_enum_list,
962                                     ARRAY_SIZE(drm_scaling_mode_enum_list));
963
964         dev->mode_config.scaling_mode_property = scaling_mode;
965
966         return 0;
967 }
968 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
969
970 /**
971  * drm_connector_attach_scaling_mode_property - attach atomic scaling mode property
972  * @connector: connector to attach scaling mode property on.
973  * @scaling_mode_mask: or'ed mask of BIT(%DRM_MODE_SCALE_\*).
974  *
975  * This is used to add support for scaling mode to atomic drivers.
976  * The scaling mode will be set to &drm_connector_state.picture_aspect_ratio
977  * and can be used from &drm_connector_helper_funcs->atomic_check for validation.
978  *
979  * This is the atomic version of drm_mode_create_scaling_mode_property().
980  *
981  * Returns:
982  * Zero on success, negative errno on failure.
983  */
984 int drm_connector_attach_scaling_mode_property(struct drm_connector *connector,
985                                                u32 scaling_mode_mask)
986 {
987         struct drm_device *dev = connector->dev;
988         struct drm_property *scaling_mode_property;
989         int i, j = 0;
990         const unsigned valid_scaling_mode_mask =
991                 (1U << ARRAY_SIZE(drm_scaling_mode_enum_list)) - 1;
992
993         if (WARN_ON(hweight32(scaling_mode_mask) < 2 ||
994                     scaling_mode_mask & ~valid_scaling_mode_mask))
995                 return -EINVAL;
996
997         scaling_mode_property =
998                 drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
999                                     hweight32(scaling_mode_mask));
1000
1001         if (!scaling_mode_property)
1002                 return -ENOMEM;
1003
1004         for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++) {
1005                 int ret;
1006
1007                 if (!(BIT(i) & scaling_mode_mask))
1008                         continue;
1009
1010                 ret = drm_property_add_enum(scaling_mode_property, j++,
1011                                             drm_scaling_mode_enum_list[i].type,
1012                                             drm_scaling_mode_enum_list[i].name);
1013
1014                 if (ret) {
1015                         drm_property_destroy(dev, scaling_mode_property);
1016
1017                         return ret;
1018                 }
1019         }
1020
1021         drm_object_attach_property(&connector->base,
1022                                    scaling_mode_property, 0);
1023
1024         connector->scaling_mode_property = scaling_mode_property;
1025
1026         return 0;
1027 }
1028 EXPORT_SYMBOL(drm_connector_attach_scaling_mode_property);
1029
1030 /**
1031  * drm_mode_create_aspect_ratio_property - create aspect ratio property
1032  * @dev: DRM device
1033  *
1034  * Called by a driver the first time it's needed, must be attached to desired
1035  * connectors.
1036  *
1037  * Returns:
1038  * Zero on success, negative errno on failure.
1039  */
1040 int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1041 {
1042         if (dev->mode_config.aspect_ratio_property)
1043                 return 0;
1044
1045         dev->mode_config.aspect_ratio_property =
1046                 drm_property_create_enum(dev, 0, "aspect ratio",
1047                                 drm_aspect_ratio_enum_list,
1048                                 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1049
1050         if (dev->mode_config.aspect_ratio_property == NULL)
1051                 return -ENOMEM;
1052
1053         return 0;
1054 }
1055 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1056
1057 /**
1058  * drm_mode_create_suggested_offset_properties - create suggests offset properties
1059  * @dev: DRM device
1060  *
1061  * Create the the suggested x/y offset property for connectors.
1062  */
1063 int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1064 {
1065         if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1066                 return 0;
1067
1068         dev->mode_config.suggested_x_property =
1069                 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1070
1071         dev->mode_config.suggested_y_property =
1072                 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1073
1074         if (dev->mode_config.suggested_x_property == NULL ||
1075             dev->mode_config.suggested_y_property == NULL)
1076                 return -ENOMEM;
1077         return 0;
1078 }
1079 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1080
1081 /**
1082  * drm_mode_connector_set_path_property - set tile property on connector
1083  * @connector: connector to set property on.
1084  * @path: path to use for property; must not be NULL.
1085  *
1086  * This creates a property to expose to userspace to specify a
1087  * connector path. This is mainly used for DisplayPort MST where
1088  * connectors have a topology and we want to allow userspace to give
1089  * them more meaningful names.
1090  *
1091  * Returns:
1092  * Zero on success, negative errno on failure.
1093  */
1094 int drm_mode_connector_set_path_property(struct drm_connector *connector,
1095                                          const char *path)
1096 {
1097         struct drm_device *dev = connector->dev;
1098         int ret;
1099
1100         ret = drm_property_replace_global_blob(dev,
1101                                                &connector->path_blob_ptr,
1102                                                strlen(path) + 1,
1103                                                path,
1104                                                &connector->base,
1105                                                dev->mode_config.path_property);
1106         return ret;
1107 }
1108 EXPORT_SYMBOL(drm_mode_connector_set_path_property);
1109
1110 /**
1111  * drm_mode_connector_set_tile_property - set tile property on connector
1112  * @connector: connector to set property on.
1113  *
1114  * This looks up the tile information for a connector, and creates a
1115  * property for userspace to parse if it exists. The property is of
1116  * the form of 8 integers using ':' as a separator.
1117  *
1118  * Returns:
1119  * Zero on success, errno on failure.
1120  */
1121 int drm_mode_connector_set_tile_property(struct drm_connector *connector)
1122 {
1123         struct drm_device *dev = connector->dev;
1124         char tile[256];
1125         int ret;
1126
1127         if (!connector->has_tile) {
1128                 ret  = drm_property_replace_global_blob(dev,
1129                                                         &connector->tile_blob_ptr,
1130                                                         0,
1131                                                         NULL,
1132                                                         &connector->base,
1133                                                         dev->mode_config.tile_property);
1134                 return ret;
1135         }
1136
1137         snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
1138                  connector->tile_group->id, connector->tile_is_single_monitor,
1139                  connector->num_h_tile, connector->num_v_tile,
1140                  connector->tile_h_loc, connector->tile_v_loc,
1141                  connector->tile_h_size, connector->tile_v_size);
1142
1143         ret = drm_property_replace_global_blob(dev,
1144                                                &connector->tile_blob_ptr,
1145                                                strlen(tile) + 1,
1146                                                tile,
1147                                                &connector->base,
1148                                                dev->mode_config.tile_property);
1149         return ret;
1150 }
1151 EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
1152
1153 /**
1154  * drm_mode_connector_update_edid_property - update the edid property of a connector
1155  * @connector: drm connector
1156  * @edid: new value of the edid property
1157  *
1158  * This function creates a new blob modeset object and assigns its id to the
1159  * connector's edid property.
1160  *
1161  * Returns:
1162  * Zero on success, negative errno on failure.
1163  */
1164 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
1165                                             const struct edid *edid)
1166 {
1167         struct drm_device *dev = connector->dev;
1168         size_t size = 0;
1169         int ret;
1170
1171         /* ignore requests to set edid when overridden */
1172         if (connector->override_edid)
1173                 return 0;
1174
1175         if (edid)
1176                 size = EDID_LENGTH * (1 + edid->extensions);
1177
1178         ret = drm_property_replace_global_blob(dev,
1179                                                &connector->edid_blob_ptr,
1180                                                size,
1181                                                edid,
1182                                                &connector->base,
1183                                                dev->mode_config.edid_property);
1184         return ret;
1185 }
1186 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
1187
1188 /**
1189  * drm_mode_connector_set_link_status_property - Set link status property of a connector
1190  * @connector: drm connector
1191  * @link_status: new value of link status property (0: Good, 1: Bad)
1192  *
1193  * In usual working scenario, this link status property will always be set to
1194  * "GOOD". If something fails during or after a mode set, the kernel driver
1195  * may set this link status property to "BAD". The caller then needs to send a
1196  * hotplug uevent for userspace to re-check the valid modes through
1197  * GET_CONNECTOR_IOCTL and retry modeset.
1198  *
1199  * Note: Drivers cannot rely on userspace to support this property and
1200  * issue a modeset. As such, they may choose to handle issues (like
1201  * re-training a link) without userspace's intervention.
1202  *
1203  * The reason for adding this property is to handle link training failures, but
1204  * it is not limited to DP or link training. For example, if we implement
1205  * asynchronous setcrtc, this property can be used to report any failures in that.
1206  */
1207 void drm_mode_connector_set_link_status_property(struct drm_connector *connector,
1208                                                  uint64_t link_status)
1209 {
1210         struct drm_device *dev = connector->dev;
1211
1212         drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1213         connector->state->link_status = link_status;
1214         drm_modeset_unlock(&dev->mode_config.connection_mutex);
1215 }
1216 EXPORT_SYMBOL(drm_mode_connector_set_link_status_property);
1217
1218 int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
1219                                     struct drm_property *property,
1220                                     uint64_t value)
1221 {
1222         int ret = -EINVAL;
1223         struct drm_connector *connector = obj_to_connector(obj);
1224
1225         /* Do DPMS ourselves */
1226         if (property == connector->dev->mode_config.dpms_property) {
1227                 ret = (*connector->funcs->dpms)(connector, (int)value);
1228         } else if (connector->funcs->set_property)
1229                 ret = connector->funcs->set_property(connector, property, value);
1230
1231         if (!ret)
1232                 drm_object_property_set_value(&connector->base, property, value);
1233         return ret;
1234 }
1235
1236 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
1237                                        void *data, struct drm_file *file_priv)
1238 {
1239         struct drm_mode_connector_set_property *conn_set_prop = data;
1240         struct drm_mode_obj_set_property obj_set_prop = {
1241                 .value = conn_set_prop->value,
1242                 .prop_id = conn_set_prop->prop_id,
1243                 .obj_id = conn_set_prop->connector_id,
1244                 .obj_type = DRM_MODE_OBJECT_CONNECTOR
1245         };
1246
1247         /* It does all the locking and checking we need */
1248         return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
1249 }
1250
1251 static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
1252 {
1253         /* For atomic drivers only state objects are synchronously updated and
1254          * protected by modeset locks, so check those first. */
1255         if (connector->state)
1256                 return connector->state->best_encoder;
1257         return connector->encoder;
1258 }
1259
1260 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1261                                          const struct drm_file *file_priv)
1262 {
1263         /*
1264          * If user-space hasn't configured the driver to expose the stereo 3D
1265          * modes, don't expose them.
1266          */
1267         if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1268                 return false;
1269
1270         return true;
1271 }
1272
1273 int drm_mode_getconnector(struct drm_device *dev, void *data,
1274                           struct drm_file *file_priv)
1275 {
1276         struct drm_mode_get_connector *out_resp = data;
1277         struct drm_connector *connector;
1278         struct drm_encoder *encoder;
1279         struct drm_display_mode *mode;
1280         int mode_count = 0;
1281         int encoders_count = 0;
1282         int ret = 0;
1283         int copied = 0;
1284         int i;
1285         struct drm_mode_modeinfo u_mode;
1286         struct drm_mode_modeinfo __user *mode_ptr;
1287         uint32_t __user *encoder_ptr;
1288
1289         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1290                 return -EINVAL;
1291
1292         memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1293
1294         connector = drm_connector_lookup(dev, out_resp->connector_id);
1295         if (!connector)
1296                 return -ENOENT;
1297
1298         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
1299                 if (connector->encoder_ids[i] != 0)
1300                         encoders_count++;
1301
1302         if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1303                 copied = 0;
1304                 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1305                 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1306                         if (connector->encoder_ids[i] != 0) {
1307                                 if (put_user(connector->encoder_ids[i],
1308                                              encoder_ptr + copied)) {
1309                                         ret = -EFAULT;
1310                                         goto out;
1311                                 }
1312                                 copied++;
1313                         }
1314                 }
1315         }
1316         out_resp->count_encoders = encoders_count;
1317
1318         out_resp->connector_id = connector->base.id;
1319         out_resp->connector_type = connector->connector_type;
1320         out_resp->connector_type_id = connector->connector_type_id;
1321
1322         mutex_lock(&dev->mode_config.mutex);
1323         if (out_resp->count_modes == 0) {
1324                 connector->funcs->fill_modes(connector,
1325                                              dev->mode_config.max_width,
1326                                              dev->mode_config.max_height);
1327         }
1328
1329         out_resp->mm_width = connector->display_info.width_mm;
1330         out_resp->mm_height = connector->display_info.height_mm;
1331         out_resp->subpixel = connector->display_info.subpixel_order;
1332         out_resp->connection = connector->status;
1333
1334         /* delayed so we get modes regardless of pre-fill_modes state */
1335         list_for_each_entry(mode, &connector->modes, head)
1336                 if (drm_mode_expose_to_userspace(mode, file_priv))
1337                         mode_count++;
1338
1339         /*
1340          * This ioctl is called twice, once to determine how much space is
1341          * needed, and the 2nd time to fill it.
1342          */
1343         if ((out_resp->count_modes >= mode_count) && mode_count) {
1344                 copied = 0;
1345                 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1346                 list_for_each_entry(mode, &connector->modes, head) {
1347                         if (!drm_mode_expose_to_userspace(mode, file_priv))
1348                                 continue;
1349
1350                         drm_mode_convert_to_umode(&u_mode, mode);
1351                         if (copy_to_user(mode_ptr + copied,
1352                                          &u_mode, sizeof(u_mode))) {
1353                                 ret = -EFAULT;
1354                                 mutex_unlock(&dev->mode_config.mutex);
1355
1356                                 goto out;
1357                         }
1358                         copied++;
1359                 }
1360         }
1361         out_resp->count_modes = mode_count;
1362         mutex_unlock(&dev->mode_config.mutex);
1363
1364         drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1365         encoder = drm_connector_get_encoder(connector);
1366         if (encoder)
1367                 out_resp->encoder_id = encoder->base.id;
1368         else
1369                 out_resp->encoder_id = 0;
1370
1371         /* Only grab properties after probing, to make sure EDID and other
1372          * properties reflect the latest status. */
1373         ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
1374                         (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
1375                         (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
1376                         &out_resp->count_props);
1377         drm_modeset_unlock(&dev->mode_config.connection_mutex);
1378
1379 out:
1380         drm_connector_put(connector);
1381
1382         return ret;
1383 }
1384
1385
1386 /**
1387  * DOC: Tile group
1388  *
1389  * Tile groups are used to represent tiled monitors with a unique integer
1390  * identifier. Tiled monitors using DisplayID v1.3 have a unique 8-byte handle,
1391  * we store this in a tile group, so we have a common identifier for all tiles
1392  * in a monitor group. The property is called "TILE". Drivers can manage tile
1393  * groups using drm_mode_create_tile_group(), drm_mode_put_tile_group() and
1394  * drm_mode_get_tile_group(). But this is only needed for internal panels where
1395  * the tile group information is exposed through a non-standard way.
1396  */
1397
1398 static void drm_tile_group_free(struct kref *kref)
1399 {
1400         struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
1401         struct drm_device *dev = tg->dev;
1402         mutex_lock(&dev->mode_config.idr_mutex);
1403         idr_remove(&dev->mode_config.tile_idr, tg->id);
1404         mutex_unlock(&dev->mode_config.idr_mutex);
1405         kfree(tg);
1406 }
1407
1408 /**
1409  * drm_mode_put_tile_group - drop a reference to a tile group.
1410  * @dev: DRM device
1411  * @tg: tile group to drop reference to.
1412  *
1413  * drop reference to tile group and free if 0.
1414  */
1415 void drm_mode_put_tile_group(struct drm_device *dev,
1416                              struct drm_tile_group *tg)
1417 {
1418         kref_put(&tg->refcount, drm_tile_group_free);
1419 }
1420 EXPORT_SYMBOL(drm_mode_put_tile_group);
1421
1422 /**
1423  * drm_mode_get_tile_group - get a reference to an existing tile group
1424  * @dev: DRM device
1425  * @topology: 8-bytes unique per monitor.
1426  *
1427  * Use the unique bytes to get a reference to an existing tile group.
1428  *
1429  * RETURNS:
1430  * tile group or NULL if not found.
1431  */
1432 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
1433                                                char topology[8])
1434 {
1435         struct drm_tile_group *tg;
1436         int id;
1437         mutex_lock(&dev->mode_config.idr_mutex);
1438         idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
1439                 if (!memcmp(tg->group_data, topology, 8)) {
1440                         if (!kref_get_unless_zero(&tg->refcount))
1441                                 tg = NULL;
1442                         mutex_unlock(&dev->mode_config.idr_mutex);
1443                         return tg;
1444                 }
1445         }
1446         mutex_unlock(&dev->mode_config.idr_mutex);
1447         return NULL;
1448 }
1449 EXPORT_SYMBOL(drm_mode_get_tile_group);
1450
1451 /**
1452  * drm_mode_create_tile_group - create a tile group from a displayid description
1453  * @dev: DRM device
1454  * @topology: 8-bytes unique per monitor.
1455  *
1456  * Create a tile group for the unique monitor, and get a unique
1457  * identifier for the tile group.
1458  *
1459  * RETURNS:
1460  * new tile group or error.
1461  */
1462 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
1463                                                   char topology[8])
1464 {
1465         struct drm_tile_group *tg;
1466         int ret;
1467
1468         tg = kzalloc(sizeof(*tg), GFP_KERNEL);
1469         if (!tg)
1470                 return ERR_PTR(-ENOMEM);
1471
1472         kref_init(&tg->refcount);
1473         memcpy(tg->group_data, topology, 8);
1474         tg->dev = dev;
1475
1476         mutex_lock(&dev->mode_config.idr_mutex);
1477         ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
1478         if (ret >= 0) {
1479                 tg->id = ret;
1480         } else {
1481                 kfree(tg);
1482                 tg = ERR_PTR(ret);
1483         }
1484
1485         mutex_unlock(&dev->mode_config.idr_mutex);
1486         return tg;
1487 }
1488 EXPORT_SYMBOL(drm_mode_create_tile_group);