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