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