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