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