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