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