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