Merge tag 'for-linus-20180608' of git://git.kernel.dk/linux-block
[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 #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. This property is initialized by calling
893  *      drm_connector_init_panel_orientation_property().
894  *
895  * scaling mode:
896  *      This property defines how a non-native mode is upscaled to the native
897  *      mode of an LCD panel:
898  *
899  *      None:
900  *              No upscaling happens, scaling is left to the panel. Not all
901  *              drivers expose this mode.
902  *      Full:
903  *              The output is upscaled to the full resolution of the panel,
904  *              ignoring the aspect ratio.
905  *      Center:
906  *              No upscaling happens, the output is centered within the native
907  *              resolution the panel.
908  *      Full aspect:
909  *              The output is upscaled to maximize either the width or height
910  *              while retaining the aspect ratio.
911  *
912  *      This property should be set up by calling
913  *      drm_connector_attach_scaling_mode_property(). Note that drivers
914  *      can also expose this property to external outputs, in which case they
915  *      must support "None", which should be the default (since external screens
916  *      have a built-in scaler).
917  */
918
919 int drm_connector_create_standard_properties(struct drm_device *dev)
920 {
921         struct drm_property *prop;
922
923         prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
924                                    DRM_MODE_PROP_IMMUTABLE,
925                                    "EDID", 0);
926         if (!prop)
927                 return -ENOMEM;
928         dev->mode_config.edid_property = prop;
929
930         prop = drm_property_create_enum(dev, 0,
931                                    "DPMS", drm_dpms_enum_list,
932                                    ARRAY_SIZE(drm_dpms_enum_list));
933         if (!prop)
934                 return -ENOMEM;
935         dev->mode_config.dpms_property = prop;
936
937         prop = drm_property_create(dev,
938                                    DRM_MODE_PROP_BLOB |
939                                    DRM_MODE_PROP_IMMUTABLE,
940                                    "PATH", 0);
941         if (!prop)
942                 return -ENOMEM;
943         dev->mode_config.path_property = prop;
944
945         prop = drm_property_create(dev,
946                                    DRM_MODE_PROP_BLOB |
947                                    DRM_MODE_PROP_IMMUTABLE,
948                                    "TILE", 0);
949         if (!prop)
950                 return -ENOMEM;
951         dev->mode_config.tile_property = prop;
952
953         prop = drm_property_create_enum(dev, 0, "link-status",
954                                         drm_link_status_enum_list,
955                                         ARRAY_SIZE(drm_link_status_enum_list));
956         if (!prop)
957                 return -ENOMEM;
958         dev->mode_config.link_status_property = prop;
959
960         prop = drm_property_create_bool(dev, DRM_MODE_PROP_IMMUTABLE, "non-desktop");
961         if (!prop)
962                 return -ENOMEM;
963         dev->mode_config.non_desktop_property = prop;
964
965         return 0;
966 }
967
968 /**
969  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
970  * @dev: DRM device
971  *
972  * Called by a driver the first time a DVI-I connector is made.
973  */
974 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
975 {
976         struct drm_property *dvi_i_selector;
977         struct drm_property *dvi_i_subconnector;
978
979         if (dev->mode_config.dvi_i_select_subconnector_property)
980                 return 0;
981
982         dvi_i_selector =
983                 drm_property_create_enum(dev, 0,
984                                     "select subconnector",
985                                     drm_dvi_i_select_enum_list,
986                                     ARRAY_SIZE(drm_dvi_i_select_enum_list));
987         dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
988
989         dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
990                                     "subconnector",
991                                     drm_dvi_i_subconnector_enum_list,
992                                     ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
993         dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
994
995         return 0;
996 }
997 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
998
999 /**
1000  * drm_create_tv_properties - create TV specific connector properties
1001  * @dev: DRM device
1002  * @num_modes: number of different TV formats (modes) supported
1003  * @modes: array of pointers to strings containing name of each format
1004  *
1005  * Called by a driver's TV initialization routine, this function creates
1006  * the TV specific connector properties for a given device.  Caller is
1007  * responsible for allocating a list of format names and passing them to
1008  * this routine.
1009  */
1010 int drm_mode_create_tv_properties(struct drm_device *dev,
1011                                   unsigned int num_modes,
1012                                   const char * const modes[])
1013 {
1014         struct drm_property *tv_selector;
1015         struct drm_property *tv_subconnector;
1016         unsigned int i;
1017
1018         if (dev->mode_config.tv_select_subconnector_property)
1019                 return 0;
1020
1021         /*
1022          * Basic connector properties
1023          */
1024         tv_selector = drm_property_create_enum(dev, 0,
1025                                           "select subconnector",
1026                                           drm_tv_select_enum_list,
1027                                           ARRAY_SIZE(drm_tv_select_enum_list));
1028         if (!tv_selector)
1029                 goto nomem;
1030
1031         dev->mode_config.tv_select_subconnector_property = tv_selector;
1032
1033         tv_subconnector =
1034                 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1035                                     "subconnector",
1036                                     drm_tv_subconnector_enum_list,
1037                                     ARRAY_SIZE(drm_tv_subconnector_enum_list));
1038         if (!tv_subconnector)
1039                 goto nomem;
1040         dev->mode_config.tv_subconnector_property = tv_subconnector;
1041
1042         /*
1043          * Other, TV specific properties: margins & TV modes.
1044          */
1045         dev->mode_config.tv_left_margin_property =
1046                 drm_property_create_range(dev, 0, "left margin", 0, 100);
1047         if (!dev->mode_config.tv_left_margin_property)
1048                 goto nomem;
1049
1050         dev->mode_config.tv_right_margin_property =
1051                 drm_property_create_range(dev, 0, "right margin", 0, 100);
1052         if (!dev->mode_config.tv_right_margin_property)
1053                 goto nomem;
1054
1055         dev->mode_config.tv_top_margin_property =
1056                 drm_property_create_range(dev, 0, "top margin", 0, 100);
1057         if (!dev->mode_config.tv_top_margin_property)
1058                 goto nomem;
1059
1060         dev->mode_config.tv_bottom_margin_property =
1061                 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1062         if (!dev->mode_config.tv_bottom_margin_property)
1063                 goto nomem;
1064
1065         dev->mode_config.tv_mode_property =
1066                 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1067                                     "mode", num_modes);
1068         if (!dev->mode_config.tv_mode_property)
1069                 goto nomem;
1070
1071         for (i = 0; i < num_modes; i++)
1072                 drm_property_add_enum(dev->mode_config.tv_mode_property,
1073                                       i, modes[i]);
1074
1075         dev->mode_config.tv_brightness_property =
1076                 drm_property_create_range(dev, 0, "brightness", 0, 100);
1077         if (!dev->mode_config.tv_brightness_property)
1078                 goto nomem;
1079
1080         dev->mode_config.tv_contrast_property =
1081                 drm_property_create_range(dev, 0, "contrast", 0, 100);
1082         if (!dev->mode_config.tv_contrast_property)
1083                 goto nomem;
1084
1085         dev->mode_config.tv_flicker_reduction_property =
1086                 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1087         if (!dev->mode_config.tv_flicker_reduction_property)
1088                 goto nomem;
1089
1090         dev->mode_config.tv_overscan_property =
1091                 drm_property_create_range(dev, 0, "overscan", 0, 100);
1092         if (!dev->mode_config.tv_overscan_property)
1093                 goto nomem;
1094
1095         dev->mode_config.tv_saturation_property =
1096                 drm_property_create_range(dev, 0, "saturation", 0, 100);
1097         if (!dev->mode_config.tv_saturation_property)
1098                 goto nomem;
1099
1100         dev->mode_config.tv_hue_property =
1101                 drm_property_create_range(dev, 0, "hue", 0, 100);
1102         if (!dev->mode_config.tv_hue_property)
1103                 goto nomem;
1104
1105         return 0;
1106 nomem:
1107         return -ENOMEM;
1108 }
1109 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1110
1111 /**
1112  * drm_mode_create_scaling_mode_property - create scaling mode property
1113  * @dev: DRM device
1114  *
1115  * Called by a driver the first time it's needed, must be attached to desired
1116  * connectors.
1117  *
1118  * Atomic drivers should use drm_connector_attach_scaling_mode_property()
1119  * instead to correctly assign &drm_connector_state.picture_aspect_ratio
1120  * in the atomic state.
1121  */
1122 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1123 {
1124         struct drm_property *scaling_mode;
1125
1126         if (dev->mode_config.scaling_mode_property)
1127                 return 0;
1128
1129         scaling_mode =
1130                 drm_property_create_enum(dev, 0, "scaling mode",
1131                                 drm_scaling_mode_enum_list,
1132                                     ARRAY_SIZE(drm_scaling_mode_enum_list));
1133
1134         dev->mode_config.scaling_mode_property = scaling_mode;
1135
1136         return 0;
1137 }
1138 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1139
1140 /**
1141  * drm_connector_attach_scaling_mode_property - attach atomic scaling mode property
1142  * @connector: connector to attach scaling mode property on.
1143  * @scaling_mode_mask: or'ed mask of BIT(%DRM_MODE_SCALE_\*).
1144  *
1145  * This is used to add support for scaling mode to atomic drivers.
1146  * The scaling mode will be set to &drm_connector_state.picture_aspect_ratio
1147  * and can be used from &drm_connector_helper_funcs->atomic_check for validation.
1148  *
1149  * This is the atomic version of drm_mode_create_scaling_mode_property().
1150  *
1151  * Returns:
1152  * Zero on success, negative errno on failure.
1153  */
1154 int drm_connector_attach_scaling_mode_property(struct drm_connector *connector,
1155                                                u32 scaling_mode_mask)
1156 {
1157         struct drm_device *dev = connector->dev;
1158         struct drm_property *scaling_mode_property;
1159         int i;
1160         const unsigned valid_scaling_mode_mask =
1161                 (1U << ARRAY_SIZE(drm_scaling_mode_enum_list)) - 1;
1162
1163         if (WARN_ON(hweight32(scaling_mode_mask) < 2 ||
1164                     scaling_mode_mask & ~valid_scaling_mode_mask))
1165                 return -EINVAL;
1166
1167         scaling_mode_property =
1168                 drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
1169                                     hweight32(scaling_mode_mask));
1170
1171         if (!scaling_mode_property)
1172                 return -ENOMEM;
1173
1174         for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++) {
1175                 int ret;
1176
1177                 if (!(BIT(i) & scaling_mode_mask))
1178                         continue;
1179
1180                 ret = drm_property_add_enum(scaling_mode_property,
1181                                             drm_scaling_mode_enum_list[i].type,
1182                                             drm_scaling_mode_enum_list[i].name);
1183
1184                 if (ret) {
1185                         drm_property_destroy(dev, scaling_mode_property);
1186
1187                         return ret;
1188                 }
1189         }
1190
1191         drm_object_attach_property(&connector->base,
1192                                    scaling_mode_property, 0);
1193
1194         connector->scaling_mode_property = scaling_mode_property;
1195
1196         return 0;
1197 }
1198 EXPORT_SYMBOL(drm_connector_attach_scaling_mode_property);
1199
1200 /**
1201  * drm_connector_attach_content_protection_property - attach content protection
1202  * property
1203  *
1204  * @connector: connector to attach CP property on.
1205  *
1206  * This is used to add support for content protection on select connectors.
1207  * Content Protection is intentionally vague to allow for different underlying
1208  * technologies, however it is most implemented by HDCP.
1209  *
1210  * The content protection will be set to &drm_connector_state.content_protection
1211  *
1212  * Returns:
1213  * Zero on success, negative errno on failure.
1214  */
1215 int drm_connector_attach_content_protection_property(
1216                 struct drm_connector *connector)
1217 {
1218         struct drm_device *dev = connector->dev;
1219         struct drm_property *prop;
1220
1221         prop = drm_property_create_enum(dev, 0, "Content Protection",
1222                                         drm_cp_enum_list,
1223                                         ARRAY_SIZE(drm_cp_enum_list));
1224         if (!prop)
1225                 return -ENOMEM;
1226
1227         drm_object_attach_property(&connector->base, prop,
1228                                    DRM_MODE_CONTENT_PROTECTION_UNDESIRED);
1229
1230         connector->content_protection_property = prop;
1231
1232         return 0;
1233 }
1234 EXPORT_SYMBOL(drm_connector_attach_content_protection_property);
1235
1236 /**
1237  * drm_mode_create_aspect_ratio_property - create aspect ratio property
1238  * @dev: DRM device
1239  *
1240  * Called by a driver the first time it's needed, must be attached to desired
1241  * connectors.
1242  *
1243  * Returns:
1244  * Zero on success, negative errno on failure.
1245  */
1246 int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1247 {
1248         if (dev->mode_config.aspect_ratio_property)
1249                 return 0;
1250
1251         dev->mode_config.aspect_ratio_property =
1252                 drm_property_create_enum(dev, 0, "aspect ratio",
1253                                 drm_aspect_ratio_enum_list,
1254                                 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1255
1256         if (dev->mode_config.aspect_ratio_property == NULL)
1257                 return -ENOMEM;
1258
1259         return 0;
1260 }
1261 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1262
1263 /**
1264  * drm_mode_create_suggested_offset_properties - create suggests offset properties
1265  * @dev: DRM device
1266  *
1267  * Create the the suggested x/y offset property for connectors.
1268  */
1269 int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1270 {
1271         if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1272                 return 0;
1273
1274         dev->mode_config.suggested_x_property =
1275                 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1276
1277         dev->mode_config.suggested_y_property =
1278                 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1279
1280         if (dev->mode_config.suggested_x_property == NULL ||
1281             dev->mode_config.suggested_y_property == NULL)
1282                 return -ENOMEM;
1283         return 0;
1284 }
1285 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1286
1287 /**
1288  * drm_mode_connector_set_path_property - set tile property on connector
1289  * @connector: connector to set property on.
1290  * @path: path to use for property; must not be NULL.
1291  *
1292  * This creates a property to expose to userspace to specify a
1293  * connector path. This is mainly used for DisplayPort MST where
1294  * connectors have a topology and we want to allow userspace to give
1295  * them more meaningful names.
1296  *
1297  * Returns:
1298  * Zero on success, negative errno on failure.
1299  */
1300 int drm_mode_connector_set_path_property(struct drm_connector *connector,
1301                                          const char *path)
1302 {
1303         struct drm_device *dev = connector->dev;
1304         int ret;
1305
1306         ret = drm_property_replace_global_blob(dev,
1307                                                &connector->path_blob_ptr,
1308                                                strlen(path) + 1,
1309                                                path,
1310                                                &connector->base,
1311                                                dev->mode_config.path_property);
1312         return ret;
1313 }
1314 EXPORT_SYMBOL(drm_mode_connector_set_path_property);
1315
1316 /**
1317  * drm_mode_connector_set_tile_property - set tile property on connector
1318  * @connector: connector to set property on.
1319  *
1320  * This looks up the tile information for a connector, and creates a
1321  * property for userspace to parse if it exists. The property is of
1322  * the form of 8 integers using ':' as a separator.
1323  *
1324  * Returns:
1325  * Zero on success, errno on failure.
1326  */
1327 int drm_mode_connector_set_tile_property(struct drm_connector *connector)
1328 {
1329         struct drm_device *dev = connector->dev;
1330         char tile[256];
1331         int ret;
1332
1333         if (!connector->has_tile) {
1334                 ret  = drm_property_replace_global_blob(dev,
1335                                                         &connector->tile_blob_ptr,
1336                                                         0,
1337                                                         NULL,
1338                                                         &connector->base,
1339                                                         dev->mode_config.tile_property);
1340                 return ret;
1341         }
1342
1343         snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
1344                  connector->tile_group->id, connector->tile_is_single_monitor,
1345                  connector->num_h_tile, connector->num_v_tile,
1346                  connector->tile_h_loc, connector->tile_v_loc,
1347                  connector->tile_h_size, connector->tile_v_size);
1348
1349         ret = drm_property_replace_global_blob(dev,
1350                                                &connector->tile_blob_ptr,
1351                                                strlen(tile) + 1,
1352                                                tile,
1353                                                &connector->base,
1354                                                dev->mode_config.tile_property);
1355         return ret;
1356 }
1357 EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
1358
1359 /**
1360  * drm_mode_connector_update_edid_property - update the edid property of a connector
1361  * @connector: drm connector
1362  * @edid: new value of the edid property
1363  *
1364  * This function creates a new blob modeset object and assigns its id to the
1365  * connector's edid property.
1366  *
1367  * Returns:
1368  * Zero on success, negative errno on failure.
1369  */
1370 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
1371                                             const struct edid *edid)
1372 {
1373         struct drm_device *dev = connector->dev;
1374         size_t size = 0;
1375         int ret;
1376
1377         /* ignore requests to set edid when overridden */
1378         if (connector->override_edid)
1379                 return 0;
1380
1381         if (edid)
1382                 size = EDID_LENGTH * (1 + edid->extensions);
1383
1384         /* Set the display info, using edid if available, otherwise
1385          * reseting the values to defaults. This duplicates the work
1386          * done in drm_add_edid_modes, but that function is not
1387          * consistently called before this one in all drivers and the
1388          * computation is cheap enough that it seems better to
1389          * duplicate it rather than attempt to ensure some arbitrary
1390          * ordering of calls.
1391          */
1392         if (edid)
1393                 drm_add_display_info(connector, edid);
1394         else
1395                 drm_reset_display_info(connector);
1396
1397         drm_object_property_set_value(&connector->base,
1398                                       dev->mode_config.non_desktop_property,
1399                                       connector->display_info.non_desktop);
1400
1401         ret = drm_property_replace_global_blob(dev,
1402                                                &connector->edid_blob_ptr,
1403                                                size,
1404                                                edid,
1405                                                &connector->base,
1406                                                dev->mode_config.edid_property);
1407         return ret;
1408 }
1409 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
1410
1411 /**
1412  * drm_mode_connector_set_link_status_property - Set link status property of a connector
1413  * @connector: drm connector
1414  * @link_status: new value of link status property (0: Good, 1: Bad)
1415  *
1416  * In usual working scenario, this link status property will always be set to
1417  * "GOOD". If something fails during or after a mode set, the kernel driver
1418  * may set this link status property to "BAD". The caller then needs to send a
1419  * hotplug uevent for userspace to re-check the valid modes through
1420  * GET_CONNECTOR_IOCTL and retry modeset.
1421  *
1422  * Note: Drivers cannot rely on userspace to support this property and
1423  * issue a modeset. As such, they may choose to handle issues (like
1424  * re-training a link) without userspace's intervention.
1425  *
1426  * The reason for adding this property is to handle link training failures, but
1427  * it is not limited to DP or link training. For example, if we implement
1428  * asynchronous setcrtc, this property can be used to report any failures in that.
1429  */
1430 void drm_mode_connector_set_link_status_property(struct drm_connector *connector,
1431                                                  uint64_t link_status)
1432 {
1433         struct drm_device *dev = connector->dev;
1434
1435         drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1436         connector->state->link_status = link_status;
1437         drm_modeset_unlock(&dev->mode_config.connection_mutex);
1438 }
1439 EXPORT_SYMBOL(drm_mode_connector_set_link_status_property);
1440
1441 /**
1442  * drm_connector_init_panel_orientation_property -
1443  *      initialize the connecters panel_orientation property
1444  * @connector: connector for which to init the panel-orientation property.
1445  * @width: width in pixels of the panel, used for panel quirk detection
1446  * @height: height in pixels of the panel, used for panel quirk detection
1447  *
1448  * This function should only be called for built-in panels, after setting
1449  * connector->display_info.panel_orientation first (if known).
1450  *
1451  * This function will check for platform specific (e.g. DMI based) quirks
1452  * overriding display_info.panel_orientation first, then if panel_orientation
1453  * is not DRM_MODE_PANEL_ORIENTATION_UNKNOWN it will attach the
1454  * "panel orientation" property to the connector.
1455  *
1456  * Returns:
1457  * Zero on success, negative errno on failure.
1458  */
1459 int drm_connector_init_panel_orientation_property(
1460         struct drm_connector *connector, int width, int height)
1461 {
1462         struct drm_device *dev = connector->dev;
1463         struct drm_display_info *info = &connector->display_info;
1464         struct drm_property *prop;
1465         int orientation_quirk;
1466
1467         orientation_quirk = drm_get_panel_orientation_quirk(width, height);
1468         if (orientation_quirk != DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
1469                 info->panel_orientation = orientation_quirk;
1470
1471         if (info->panel_orientation == DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
1472                 return 0;
1473
1474         prop = dev->mode_config.panel_orientation_property;
1475         if (!prop) {
1476                 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1477                                 "panel orientation",
1478                                 drm_panel_orientation_enum_list,
1479                                 ARRAY_SIZE(drm_panel_orientation_enum_list));
1480                 if (!prop)
1481                         return -ENOMEM;
1482
1483                 dev->mode_config.panel_orientation_property = prop;
1484         }
1485
1486         drm_object_attach_property(&connector->base, prop,
1487                                    info->panel_orientation);
1488         return 0;
1489 }
1490 EXPORT_SYMBOL(drm_connector_init_panel_orientation_property);
1491
1492 int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
1493                                     struct drm_property *property,
1494                                     uint64_t value)
1495 {
1496         int ret = -EINVAL;
1497         struct drm_connector *connector = obj_to_connector(obj);
1498
1499         /* Do DPMS ourselves */
1500         if (property == connector->dev->mode_config.dpms_property) {
1501                 ret = (*connector->funcs->dpms)(connector, (int)value);
1502         } else if (connector->funcs->set_property)
1503                 ret = connector->funcs->set_property(connector, property, value);
1504
1505         if (!ret)
1506                 drm_object_property_set_value(&connector->base, property, value);
1507         return ret;
1508 }
1509
1510 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
1511                                        void *data, struct drm_file *file_priv)
1512 {
1513         struct drm_mode_connector_set_property *conn_set_prop = data;
1514         struct drm_mode_obj_set_property obj_set_prop = {
1515                 .value = conn_set_prop->value,
1516                 .prop_id = conn_set_prop->prop_id,
1517                 .obj_id = conn_set_prop->connector_id,
1518                 .obj_type = DRM_MODE_OBJECT_CONNECTOR
1519         };
1520
1521         /* It does all the locking and checking we need */
1522         return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
1523 }
1524
1525 static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
1526 {
1527         /* For atomic drivers only state objects are synchronously updated and
1528          * protected by modeset locks, so check those first. */
1529         if (connector->state)
1530                 return connector->state->best_encoder;
1531         return connector->encoder;
1532 }
1533
1534 static bool
1535 drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1536                              const struct list_head *export_list,
1537                              const struct drm_file *file_priv)
1538 {
1539         /*
1540          * If user-space hasn't configured the driver to expose the stereo 3D
1541          * modes, don't expose them.
1542          */
1543         if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1544                 return false;
1545         /*
1546          * If user-space hasn't configured the driver to expose the modes
1547          * with aspect-ratio, don't expose them. However if such a mode
1548          * is unique, let it be exposed, but reset the aspect-ratio flags
1549          * while preparing the list of user-modes.
1550          */
1551         if (!file_priv->aspect_ratio_allowed) {
1552                 struct drm_display_mode *mode_itr;
1553
1554                 list_for_each_entry(mode_itr, export_list, export_head)
1555                         if (drm_mode_match(mode_itr, mode,
1556                                            DRM_MODE_MATCH_TIMINGS |
1557                                            DRM_MODE_MATCH_CLOCK |
1558                                            DRM_MODE_MATCH_FLAGS |
1559                                            DRM_MODE_MATCH_3D_FLAGS))
1560                                 return false;
1561         }
1562
1563         return true;
1564 }
1565
1566 int drm_mode_getconnector(struct drm_device *dev, void *data,
1567                           struct drm_file *file_priv)
1568 {
1569         struct drm_mode_get_connector *out_resp = data;
1570         struct drm_connector *connector;
1571         struct drm_encoder *encoder;
1572         struct drm_display_mode *mode;
1573         int mode_count = 0;
1574         int encoders_count = 0;
1575         int ret = 0;
1576         int copied = 0;
1577         int i;
1578         struct drm_mode_modeinfo u_mode;
1579         struct drm_mode_modeinfo __user *mode_ptr;
1580         uint32_t __user *encoder_ptr;
1581         LIST_HEAD(export_list);
1582
1583         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1584                 return -EINVAL;
1585
1586         memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1587
1588         connector = drm_connector_lookup(dev, file_priv, out_resp->connector_id);
1589         if (!connector)
1590                 return -ENOENT;
1591
1592         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
1593                 if (connector->encoder_ids[i] != 0)
1594                         encoders_count++;
1595
1596         if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1597                 copied = 0;
1598                 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1599                 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1600                         if (connector->encoder_ids[i] != 0) {
1601                                 if (put_user(connector->encoder_ids[i],
1602                                              encoder_ptr + copied)) {
1603                                         ret = -EFAULT;
1604                                         goto out;
1605                                 }
1606                                 copied++;
1607                         }
1608                 }
1609         }
1610         out_resp->count_encoders = encoders_count;
1611
1612         out_resp->connector_id = connector->base.id;
1613         out_resp->connector_type = connector->connector_type;
1614         out_resp->connector_type_id = connector->connector_type_id;
1615
1616         mutex_lock(&dev->mode_config.mutex);
1617         if (out_resp->count_modes == 0) {
1618                 connector->funcs->fill_modes(connector,
1619                                              dev->mode_config.max_width,
1620                                              dev->mode_config.max_height);
1621         }
1622
1623         out_resp->mm_width = connector->display_info.width_mm;
1624         out_resp->mm_height = connector->display_info.height_mm;
1625         out_resp->subpixel = connector->display_info.subpixel_order;
1626         out_resp->connection = connector->status;
1627
1628         /* delayed so we get modes regardless of pre-fill_modes state */
1629         list_for_each_entry(mode, &connector->modes, head)
1630                 if (drm_mode_expose_to_userspace(mode, &export_list,
1631                                                  file_priv)) {
1632                         list_add_tail(&mode->export_head, &export_list);
1633                         mode_count++;
1634                 }
1635
1636         /*
1637          * This ioctl is called twice, once to determine how much space is
1638          * needed, and the 2nd time to fill it.
1639          * The modes that need to be exposed to the user are maintained in the
1640          * 'export_list'. When the ioctl is called first time to determine the,
1641          * space, the export_list gets filled, to find the no.of modes. In the
1642          * 2nd time, the user modes are filled, one by one from the export_list.
1643          */
1644         if ((out_resp->count_modes >= mode_count) && mode_count) {
1645                 copied = 0;
1646                 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1647                 list_for_each_entry(mode, &export_list, export_head) {
1648                         drm_mode_convert_to_umode(&u_mode, mode);
1649                         /*
1650                          * Reset aspect ratio flags of user-mode, if modes with
1651                          * aspect-ratio are not supported.
1652                          */
1653                         if (!file_priv->aspect_ratio_allowed)
1654                                 u_mode.flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
1655                         if (copy_to_user(mode_ptr + copied,
1656                                          &u_mode, sizeof(u_mode))) {
1657                                 ret = -EFAULT;
1658                                 mutex_unlock(&dev->mode_config.mutex);
1659
1660                                 goto out;
1661                         }
1662                         copied++;
1663                 }
1664         }
1665         out_resp->count_modes = mode_count;
1666         mutex_unlock(&dev->mode_config.mutex);
1667
1668         drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1669         encoder = drm_connector_get_encoder(connector);
1670         if (encoder)
1671                 out_resp->encoder_id = encoder->base.id;
1672         else
1673                 out_resp->encoder_id = 0;
1674
1675         /* Only grab properties after probing, to make sure EDID and other
1676          * properties reflect the latest status. */
1677         ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
1678                         (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
1679                         (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
1680                         &out_resp->count_props);
1681         drm_modeset_unlock(&dev->mode_config.connection_mutex);
1682
1683 out:
1684         drm_connector_put(connector);
1685
1686         return ret;
1687 }
1688
1689
1690 /**
1691  * DOC: Tile group
1692  *
1693  * Tile groups are used to represent tiled monitors with a unique integer
1694  * identifier. Tiled monitors using DisplayID v1.3 have a unique 8-byte handle,
1695  * we store this in a tile group, so we have a common identifier for all tiles
1696  * in a monitor group. The property is called "TILE". Drivers can manage tile
1697  * groups using drm_mode_create_tile_group(), drm_mode_put_tile_group() and
1698  * drm_mode_get_tile_group(). But this is only needed for internal panels where
1699  * the tile group information is exposed through a non-standard way.
1700  */
1701
1702 static void drm_tile_group_free(struct kref *kref)
1703 {
1704         struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
1705         struct drm_device *dev = tg->dev;
1706         mutex_lock(&dev->mode_config.idr_mutex);
1707         idr_remove(&dev->mode_config.tile_idr, tg->id);
1708         mutex_unlock(&dev->mode_config.idr_mutex);
1709         kfree(tg);
1710 }
1711
1712 /**
1713  * drm_mode_put_tile_group - drop a reference to a tile group.
1714  * @dev: DRM device
1715  * @tg: tile group to drop reference to.
1716  *
1717  * drop reference to tile group and free if 0.
1718  */
1719 void drm_mode_put_tile_group(struct drm_device *dev,
1720                              struct drm_tile_group *tg)
1721 {
1722         kref_put(&tg->refcount, drm_tile_group_free);
1723 }
1724 EXPORT_SYMBOL(drm_mode_put_tile_group);
1725
1726 /**
1727  * drm_mode_get_tile_group - get a reference to an existing tile group
1728  * @dev: DRM device
1729  * @topology: 8-bytes unique per monitor.
1730  *
1731  * Use the unique bytes to get a reference to an existing tile group.
1732  *
1733  * RETURNS:
1734  * tile group or NULL if not found.
1735  */
1736 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
1737                                                char topology[8])
1738 {
1739         struct drm_tile_group *tg;
1740         int id;
1741         mutex_lock(&dev->mode_config.idr_mutex);
1742         idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
1743                 if (!memcmp(tg->group_data, topology, 8)) {
1744                         if (!kref_get_unless_zero(&tg->refcount))
1745                                 tg = NULL;
1746                         mutex_unlock(&dev->mode_config.idr_mutex);
1747                         return tg;
1748                 }
1749         }
1750         mutex_unlock(&dev->mode_config.idr_mutex);
1751         return NULL;
1752 }
1753 EXPORT_SYMBOL(drm_mode_get_tile_group);
1754
1755 /**
1756  * drm_mode_create_tile_group - create a tile group from a displayid description
1757  * @dev: DRM device
1758  * @topology: 8-bytes unique per monitor.
1759  *
1760  * Create a tile group for the unique monitor, and get a unique
1761  * identifier for the tile group.
1762  *
1763  * RETURNS:
1764  * new tile group or error.
1765  */
1766 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
1767                                                   char topology[8])
1768 {
1769         struct drm_tile_group *tg;
1770         int ret;
1771
1772         tg = kzalloc(sizeof(*tg), GFP_KERNEL);
1773         if (!tg)
1774                 return ERR_PTR(-ENOMEM);
1775
1776         kref_init(&tg->refcount);
1777         memcpy(tg->group_data, topology, 8);
1778         tg->dev = dev;
1779
1780         mutex_lock(&dev->mode_config.idr_mutex);
1781         ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
1782         if (ret >= 0) {
1783                 tg->id = ret;
1784         } else {
1785                 kfree(tg);
1786                 tg = ERR_PTR(ret);
1787         }
1788
1789         mutex_unlock(&dev->mode_config.idr_mutex);
1790         return tg;
1791 }
1792 EXPORT_SYMBOL(drm_mode_create_tile_group);