xref: /openbmc/linux/drivers/gpu/drm/drm_connector.c (revision 9221b289)
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_auth.h>
24 #include <drm/drm_connector.h>
25 #include <drm/drm_edid.h>
26 #include <drm/drm_encoder.h>
27 #include <drm/drm_panel.h>
28 #include <drm/drm_utils.h>
29 #include <drm/drm_print.h>
30 #include <drm/drm_drv.h>
31 #include <drm/drm_file.h>
32 #include <drm/drm_privacy_screen_consumer.h>
33 #include <drm/drm_sysfs.h>
34 
35 #include <linux/fb.h>
36 #include <linux/uaccess.h>
37 
38 #include "drm_crtc_internal.h"
39 #include "drm_internal.h"
40 
41 /**
42  * DOC: overview
43  *
44  * In DRM connectors are the general abstraction for display sinks, and include
45  * also fixed panels or anything else that can display pixels in some form. As
46  * opposed to all other KMS objects representing hardware (like CRTC, encoder or
47  * plane abstractions) connectors can be hotplugged and unplugged at runtime.
48  * Hence they are reference-counted using drm_connector_get() and
49  * drm_connector_put().
50  *
51  * KMS driver must create, initialize, register and attach at a &struct
52  * drm_connector for each such sink. The instance is created as other KMS
53  * objects and initialized by setting the following fields. The connector is
54  * initialized with a call to drm_connector_init() with a pointer to the
55  * &struct drm_connector_funcs and a connector type, and then exposed to
56  * userspace with a call to drm_connector_register().
57  *
58  * Connectors must be attached to an encoder to be used. For devices that map
59  * connectors to encoders 1:1, the connector should be attached at
60  * initialization time with a call to drm_connector_attach_encoder(). The
61  * driver must also set the &drm_connector.encoder field to point to the
62  * attached encoder.
63  *
64  * For connectors which are not fixed (like built-in panels) the driver needs to
65  * support hotplug notifications. The simplest way to do that is by using the
66  * probe helpers, see drm_kms_helper_poll_init() for connectors which don't have
67  * hardware support for hotplug interrupts. Connectors with hardware hotplug
68  * support can instead use e.g. drm_helper_hpd_irq_event().
69  */
70 
71 /*
72  * Global connector list for drm_connector_find_by_fwnode().
73  * Note drm_connector_[un]register() first take connector->lock and then
74  * take the connector_list_lock.
75  */
76 static DEFINE_MUTEX(connector_list_lock);
77 static LIST_HEAD(connector_list);
78 
79 struct drm_conn_prop_enum_list {
80 	int type;
81 	const char *name;
82 	struct ida ida;
83 };
84 
85 /*
86  * Connector and encoder types.
87  */
88 static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
89 	{ DRM_MODE_CONNECTOR_Unknown, "Unknown" },
90 	{ DRM_MODE_CONNECTOR_VGA, "VGA" },
91 	{ DRM_MODE_CONNECTOR_DVII, "DVI-I" },
92 	{ DRM_MODE_CONNECTOR_DVID, "DVI-D" },
93 	{ DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
94 	{ DRM_MODE_CONNECTOR_Composite, "Composite" },
95 	{ DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
96 	{ DRM_MODE_CONNECTOR_LVDS, "LVDS" },
97 	{ DRM_MODE_CONNECTOR_Component, "Component" },
98 	{ DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
99 	{ DRM_MODE_CONNECTOR_DisplayPort, "DP" },
100 	{ DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
101 	{ DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
102 	{ DRM_MODE_CONNECTOR_TV, "TV" },
103 	{ DRM_MODE_CONNECTOR_eDP, "eDP" },
104 	{ DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
105 	{ DRM_MODE_CONNECTOR_DSI, "DSI" },
106 	{ DRM_MODE_CONNECTOR_DPI, "DPI" },
107 	{ DRM_MODE_CONNECTOR_WRITEBACK, "Writeback" },
108 	{ DRM_MODE_CONNECTOR_SPI, "SPI" },
109 	{ DRM_MODE_CONNECTOR_USB, "USB" },
110 };
111 
112 void drm_connector_ida_init(void)
113 {
114 	int i;
115 
116 	for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
117 		ida_init(&drm_connector_enum_list[i].ida);
118 }
119 
120 void drm_connector_ida_destroy(void)
121 {
122 	int i;
123 
124 	for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
125 		ida_destroy(&drm_connector_enum_list[i].ida);
126 }
127 
128 /**
129  * drm_get_connector_type_name - return a string for connector type
130  * @type: The connector type (DRM_MODE_CONNECTOR_*)
131  *
132  * Returns: the name of the connector type, or NULL if the type is not valid.
133  */
134 const char *drm_get_connector_type_name(unsigned int type)
135 {
136 	if (type < ARRAY_SIZE(drm_connector_enum_list))
137 		return drm_connector_enum_list[type].name;
138 
139 	return NULL;
140 }
141 EXPORT_SYMBOL(drm_get_connector_type_name);
142 
143 /**
144  * drm_connector_get_cmdline_mode - reads the user's cmdline mode
145  * @connector: connector to query
146  *
147  * The kernel supports per-connector configuration of its consoles through
148  * use of the video= parameter. This function parses that option and
149  * extracts the user's specified mode (or enable/disable status) for a
150  * particular connector. This is typically only used during the early fbdev
151  * setup.
152  */
153 static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
154 {
155 	struct drm_cmdline_mode *mode = &connector->cmdline_mode;
156 	char *option = NULL;
157 
158 	if (fb_get_options(connector->name, &option))
159 		return;
160 
161 	if (!drm_mode_parse_command_line_for_connector(option,
162 						       connector,
163 						       mode))
164 		return;
165 
166 	if (mode->force) {
167 		DRM_INFO("forcing %s connector %s\n", connector->name,
168 			 drm_get_connector_force_name(mode->force));
169 		connector->force = mode->force;
170 	}
171 
172 	if (mode->panel_orientation != DRM_MODE_PANEL_ORIENTATION_UNKNOWN) {
173 		DRM_INFO("cmdline forces connector %s panel_orientation to %d\n",
174 			 connector->name, mode->panel_orientation);
175 		drm_connector_set_panel_orientation(connector,
176 						    mode->panel_orientation);
177 	}
178 
179 	DRM_DEBUG_KMS("cmdline mode for connector %s %s %dx%d@%dHz%s%s%s\n",
180 		      connector->name, mode->name,
181 		      mode->xres, mode->yres,
182 		      mode->refresh_specified ? mode->refresh : 60,
183 		      mode->rb ? " reduced blanking" : "",
184 		      mode->margins ? " with margins" : "",
185 		      mode->interlace ?  " interlaced" : "");
186 }
187 
188 static void drm_connector_free(struct kref *kref)
189 {
190 	struct drm_connector *connector =
191 		container_of(kref, struct drm_connector, base.refcount);
192 	struct drm_device *dev = connector->dev;
193 
194 	drm_mode_object_unregister(dev, &connector->base);
195 	connector->funcs->destroy(connector);
196 }
197 
198 void drm_connector_free_work_fn(struct work_struct *work)
199 {
200 	struct drm_connector *connector, *n;
201 	struct drm_device *dev =
202 		container_of(work, struct drm_device, mode_config.connector_free_work);
203 	struct drm_mode_config *config = &dev->mode_config;
204 	unsigned long flags;
205 	struct llist_node *freed;
206 
207 	spin_lock_irqsave(&config->connector_list_lock, flags);
208 	freed = llist_del_all(&config->connector_free_list);
209 	spin_unlock_irqrestore(&config->connector_list_lock, flags);
210 
211 	llist_for_each_entry_safe(connector, n, freed, free_node) {
212 		drm_mode_object_unregister(dev, &connector->base);
213 		connector->funcs->destroy(connector);
214 	}
215 }
216 
217 /**
218  * drm_connector_init - Init a preallocated connector
219  * @dev: DRM device
220  * @connector: the connector to init
221  * @funcs: callbacks for this connector
222  * @connector_type: user visible type of the connector
223  *
224  * Initialises a preallocated connector. Connectors should be
225  * subclassed as part of driver connector objects.
226  *
227  * Returns:
228  * Zero on success, error code on failure.
229  */
230 int drm_connector_init(struct drm_device *dev,
231 		       struct drm_connector *connector,
232 		       const struct drm_connector_funcs *funcs,
233 		       int connector_type)
234 {
235 	struct drm_mode_config *config = &dev->mode_config;
236 	int ret;
237 	struct ida *connector_ida =
238 		&drm_connector_enum_list[connector_type].ida;
239 
240 	WARN_ON(drm_drv_uses_atomic_modeset(dev) &&
241 		(!funcs->atomic_destroy_state ||
242 		 !funcs->atomic_duplicate_state));
243 
244 	ret = __drm_mode_object_add(dev, &connector->base,
245 				    DRM_MODE_OBJECT_CONNECTOR,
246 				    false, drm_connector_free);
247 	if (ret)
248 		return ret;
249 
250 	connector->base.properties = &connector->properties;
251 	connector->dev = dev;
252 	connector->funcs = funcs;
253 
254 	/* connector index is used with 32bit bitmasks */
255 	ret = ida_alloc_max(&config->connector_ida, 31, GFP_KERNEL);
256 	if (ret < 0) {
257 		DRM_DEBUG_KMS("Failed to allocate %s connector index: %d\n",
258 			      drm_connector_enum_list[connector_type].name,
259 			      ret);
260 		goto out_put;
261 	}
262 	connector->index = ret;
263 	ret = 0;
264 
265 	connector->connector_type = connector_type;
266 	connector->connector_type_id =
267 		ida_alloc_min(connector_ida, 1, GFP_KERNEL);
268 	if (connector->connector_type_id < 0) {
269 		ret = connector->connector_type_id;
270 		goto out_put_id;
271 	}
272 	connector->name =
273 		kasprintf(GFP_KERNEL, "%s-%d",
274 			  drm_connector_enum_list[connector_type].name,
275 			  connector->connector_type_id);
276 	if (!connector->name) {
277 		ret = -ENOMEM;
278 		goto out_put_type_id;
279 	}
280 
281 	INIT_LIST_HEAD(&connector->global_connector_list_entry);
282 	INIT_LIST_HEAD(&connector->probed_modes);
283 	INIT_LIST_HEAD(&connector->modes);
284 	mutex_init(&connector->mutex);
285 	connector->edid_blob_ptr = NULL;
286 	connector->epoch_counter = 0;
287 	connector->tile_blob_ptr = NULL;
288 	connector->status = connector_status_unknown;
289 	connector->display_info.panel_orientation =
290 		DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
291 
292 	drm_connector_get_cmdline_mode(connector);
293 
294 	/* We should add connectors at the end to avoid upsetting the connector
295 	 * index too much.
296 	 */
297 	spin_lock_irq(&config->connector_list_lock);
298 	list_add_tail(&connector->head, &config->connector_list);
299 	config->num_connector++;
300 	spin_unlock_irq(&config->connector_list_lock);
301 
302 	if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL &&
303 	    connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
304 		drm_connector_attach_edid_property(connector);
305 
306 	drm_object_attach_property(&connector->base,
307 				      config->dpms_property, 0);
308 
309 	drm_object_attach_property(&connector->base,
310 				   config->link_status_property,
311 				   0);
312 
313 	drm_object_attach_property(&connector->base,
314 				   config->non_desktop_property,
315 				   0);
316 	drm_object_attach_property(&connector->base,
317 				   config->tile_property,
318 				   0);
319 
320 	if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
321 		drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
322 	}
323 
324 	connector->debugfs_entry = NULL;
325 out_put_type_id:
326 	if (ret)
327 		ida_free(connector_ida, connector->connector_type_id);
328 out_put_id:
329 	if (ret)
330 		ida_free(&config->connector_ida, connector->index);
331 out_put:
332 	if (ret)
333 		drm_mode_object_unregister(dev, &connector->base);
334 
335 	return ret;
336 }
337 EXPORT_SYMBOL(drm_connector_init);
338 
339 /**
340  * drm_connector_init_with_ddc - Init a preallocated connector
341  * @dev: DRM device
342  * @connector: the connector to init
343  * @funcs: callbacks for this connector
344  * @connector_type: user visible type of the connector
345  * @ddc: pointer to the associated ddc adapter
346  *
347  * Initialises a preallocated connector. Connectors should be
348  * subclassed as part of driver connector objects.
349  *
350  * Ensures that the ddc field of the connector is correctly set.
351  *
352  * Returns:
353  * Zero on success, error code on failure.
354  */
355 int drm_connector_init_with_ddc(struct drm_device *dev,
356 				struct drm_connector *connector,
357 				const struct drm_connector_funcs *funcs,
358 				int connector_type,
359 				struct i2c_adapter *ddc)
360 {
361 	int ret;
362 
363 	ret = drm_connector_init(dev, connector, funcs, connector_type);
364 	if (ret)
365 		return ret;
366 
367 	/* provide ddc symlink in sysfs */
368 	connector->ddc = ddc;
369 
370 	return ret;
371 }
372 EXPORT_SYMBOL(drm_connector_init_with_ddc);
373 
374 /**
375  * drm_connector_attach_edid_property - attach edid property.
376  * @connector: the connector
377  *
378  * Some connector types like DRM_MODE_CONNECTOR_VIRTUAL do not get a
379  * edid property attached by default.  This function can be used to
380  * explicitly enable the edid property in these cases.
381  */
382 void drm_connector_attach_edid_property(struct drm_connector *connector)
383 {
384 	struct drm_mode_config *config = &connector->dev->mode_config;
385 
386 	drm_object_attach_property(&connector->base,
387 				   config->edid_property,
388 				   0);
389 }
390 EXPORT_SYMBOL(drm_connector_attach_edid_property);
391 
392 /**
393  * drm_connector_attach_encoder - attach a connector to an encoder
394  * @connector: connector to attach
395  * @encoder: encoder to attach @connector to
396  *
397  * This function links up a connector to an encoder. Note that the routing
398  * restrictions between encoders and crtcs are exposed to userspace through the
399  * possible_clones and possible_crtcs bitmasks.
400  *
401  * Returns:
402  * Zero on success, negative errno on failure.
403  */
404 int drm_connector_attach_encoder(struct drm_connector *connector,
405 				 struct drm_encoder *encoder)
406 {
407 	/*
408 	 * In the past, drivers have attempted to model the static association
409 	 * of connector to encoder in simple connector/encoder devices using a
410 	 * direct assignment of connector->encoder = encoder. This connection
411 	 * is a logical one and the responsibility of the core, so drivers are
412 	 * expected not to mess with this.
413 	 *
414 	 * Note that the error return should've been enough here, but a large
415 	 * majority of drivers ignores the return value, so add in a big WARN
416 	 * to get people's attention.
417 	 */
418 	if (WARN_ON(connector->encoder))
419 		return -EINVAL;
420 
421 	connector->possible_encoders |= drm_encoder_mask(encoder);
422 
423 	return 0;
424 }
425 EXPORT_SYMBOL(drm_connector_attach_encoder);
426 
427 /**
428  * drm_connector_has_possible_encoder - check if the connector and encoder are
429  * associated with each other
430  * @connector: the connector
431  * @encoder: the encoder
432  *
433  * Returns:
434  * True if @encoder is one of the possible encoders for @connector.
435  */
436 bool drm_connector_has_possible_encoder(struct drm_connector *connector,
437 					struct drm_encoder *encoder)
438 {
439 	return connector->possible_encoders & drm_encoder_mask(encoder);
440 }
441 EXPORT_SYMBOL(drm_connector_has_possible_encoder);
442 
443 static void drm_mode_remove(struct drm_connector *connector,
444 			    struct drm_display_mode *mode)
445 {
446 	list_del(&mode->head);
447 	drm_mode_destroy(connector->dev, mode);
448 }
449 
450 /**
451  * drm_connector_cleanup - cleans up an initialised connector
452  * @connector: connector to cleanup
453  *
454  * Cleans up the connector but doesn't free the object.
455  */
456 void drm_connector_cleanup(struct drm_connector *connector)
457 {
458 	struct drm_device *dev = connector->dev;
459 	struct drm_display_mode *mode, *t;
460 
461 	/* The connector should have been removed from userspace long before
462 	 * it is finally destroyed.
463 	 */
464 	if (WARN_ON(connector->registration_state ==
465 		    DRM_CONNECTOR_REGISTERED))
466 		drm_connector_unregister(connector);
467 
468 	if (connector->privacy_screen) {
469 		drm_privacy_screen_put(connector->privacy_screen);
470 		connector->privacy_screen = NULL;
471 	}
472 
473 	if (connector->tile_group) {
474 		drm_mode_put_tile_group(dev, connector->tile_group);
475 		connector->tile_group = NULL;
476 	}
477 
478 	list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
479 		drm_mode_remove(connector, mode);
480 
481 	list_for_each_entry_safe(mode, t, &connector->modes, head)
482 		drm_mode_remove(connector, mode);
483 
484 	ida_free(&drm_connector_enum_list[connector->connector_type].ida,
485 			  connector->connector_type_id);
486 
487 	ida_free(&dev->mode_config.connector_ida, connector->index);
488 
489 	kfree(connector->display_info.bus_formats);
490 	drm_mode_object_unregister(dev, &connector->base);
491 	kfree(connector->name);
492 	connector->name = NULL;
493 	fwnode_handle_put(connector->fwnode);
494 	connector->fwnode = NULL;
495 	spin_lock_irq(&dev->mode_config.connector_list_lock);
496 	list_del(&connector->head);
497 	dev->mode_config.num_connector--;
498 	spin_unlock_irq(&dev->mode_config.connector_list_lock);
499 
500 	WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
501 	if (connector->state && connector->funcs->atomic_destroy_state)
502 		connector->funcs->atomic_destroy_state(connector,
503 						       connector->state);
504 
505 	mutex_destroy(&connector->mutex);
506 
507 	memset(connector, 0, sizeof(*connector));
508 }
509 EXPORT_SYMBOL(drm_connector_cleanup);
510 
511 /**
512  * drm_connector_register - register a connector
513  * @connector: the connector to register
514  *
515  * Register userspace interfaces for a connector. Only call this for connectors
516  * which can be hotplugged after drm_dev_register() has been called already,
517  * e.g. DP MST connectors. All other connectors will be registered automatically
518  * when calling drm_dev_register().
519  *
520  * Returns:
521  * Zero on success, error code on failure.
522  */
523 int drm_connector_register(struct drm_connector *connector)
524 {
525 	int ret = 0;
526 
527 	if (!connector->dev->registered)
528 		return 0;
529 
530 	mutex_lock(&connector->mutex);
531 	if (connector->registration_state != DRM_CONNECTOR_INITIALIZING)
532 		goto unlock;
533 
534 	ret = drm_sysfs_connector_add(connector);
535 	if (ret)
536 		goto unlock;
537 
538 	drm_debugfs_connector_add(connector);
539 
540 	if (connector->funcs->late_register) {
541 		ret = connector->funcs->late_register(connector);
542 		if (ret)
543 			goto err_debugfs;
544 	}
545 
546 	drm_mode_object_register(connector->dev, &connector->base);
547 
548 	connector->registration_state = DRM_CONNECTOR_REGISTERED;
549 
550 	/* Let userspace know we have a new connector */
551 	drm_sysfs_connector_hotplug_event(connector);
552 
553 	if (connector->privacy_screen)
554 		drm_privacy_screen_register_notifier(connector->privacy_screen,
555 					   &connector->privacy_screen_notifier);
556 
557 	mutex_lock(&connector_list_lock);
558 	list_add_tail(&connector->global_connector_list_entry, &connector_list);
559 	mutex_unlock(&connector_list_lock);
560 	goto unlock;
561 
562 err_debugfs:
563 	drm_debugfs_connector_remove(connector);
564 	drm_sysfs_connector_remove(connector);
565 unlock:
566 	mutex_unlock(&connector->mutex);
567 	return ret;
568 }
569 EXPORT_SYMBOL(drm_connector_register);
570 
571 /**
572  * drm_connector_unregister - unregister a connector
573  * @connector: the connector to unregister
574  *
575  * Unregister userspace interfaces for a connector. Only call this for
576  * connectors which have registered explicitly by calling drm_dev_register(),
577  * since connectors are unregistered automatically when drm_dev_unregister() is
578  * called.
579  */
580 void drm_connector_unregister(struct drm_connector *connector)
581 {
582 	mutex_lock(&connector->mutex);
583 	if (connector->registration_state != DRM_CONNECTOR_REGISTERED) {
584 		mutex_unlock(&connector->mutex);
585 		return;
586 	}
587 
588 	mutex_lock(&connector_list_lock);
589 	list_del_init(&connector->global_connector_list_entry);
590 	mutex_unlock(&connector_list_lock);
591 
592 	if (connector->privacy_screen)
593 		drm_privacy_screen_unregister_notifier(
594 					connector->privacy_screen,
595 					&connector->privacy_screen_notifier);
596 
597 	if (connector->funcs->early_unregister)
598 		connector->funcs->early_unregister(connector);
599 
600 	drm_sysfs_connector_remove(connector);
601 	drm_debugfs_connector_remove(connector);
602 
603 	connector->registration_state = DRM_CONNECTOR_UNREGISTERED;
604 	mutex_unlock(&connector->mutex);
605 }
606 EXPORT_SYMBOL(drm_connector_unregister);
607 
608 void drm_connector_unregister_all(struct drm_device *dev)
609 {
610 	struct drm_connector *connector;
611 	struct drm_connector_list_iter conn_iter;
612 
613 	drm_connector_list_iter_begin(dev, &conn_iter);
614 	drm_for_each_connector_iter(connector, &conn_iter)
615 		drm_connector_unregister(connector);
616 	drm_connector_list_iter_end(&conn_iter);
617 }
618 
619 int drm_connector_register_all(struct drm_device *dev)
620 {
621 	struct drm_connector *connector;
622 	struct drm_connector_list_iter conn_iter;
623 	int ret = 0;
624 
625 	drm_connector_list_iter_begin(dev, &conn_iter);
626 	drm_for_each_connector_iter(connector, &conn_iter) {
627 		ret = drm_connector_register(connector);
628 		if (ret)
629 			break;
630 	}
631 	drm_connector_list_iter_end(&conn_iter);
632 
633 	if (ret)
634 		drm_connector_unregister_all(dev);
635 	return ret;
636 }
637 
638 /**
639  * drm_get_connector_status_name - return a string for connector status
640  * @status: connector status to compute name of
641  *
642  * In contrast to the other drm_get_*_name functions this one here returns a
643  * const pointer and hence is threadsafe.
644  *
645  * Returns: connector status string
646  */
647 const char *drm_get_connector_status_name(enum drm_connector_status status)
648 {
649 	if (status == connector_status_connected)
650 		return "connected";
651 	else if (status == connector_status_disconnected)
652 		return "disconnected";
653 	else
654 		return "unknown";
655 }
656 EXPORT_SYMBOL(drm_get_connector_status_name);
657 
658 /**
659  * drm_get_connector_force_name - return a string for connector force
660  * @force: connector force to get name of
661  *
662  * Returns: const pointer to name.
663  */
664 const char *drm_get_connector_force_name(enum drm_connector_force force)
665 {
666 	switch (force) {
667 	case DRM_FORCE_UNSPECIFIED:
668 		return "unspecified";
669 	case DRM_FORCE_OFF:
670 		return "off";
671 	case DRM_FORCE_ON:
672 		return "on";
673 	case DRM_FORCE_ON_DIGITAL:
674 		return "digital";
675 	default:
676 		return "unknown";
677 	}
678 }
679 
680 #ifdef CONFIG_LOCKDEP
681 static struct lockdep_map connector_list_iter_dep_map = {
682 	.name = "drm_connector_list_iter"
683 };
684 #endif
685 
686 /**
687  * drm_connector_list_iter_begin - initialize a connector_list iterator
688  * @dev: DRM device
689  * @iter: connector_list iterator
690  *
691  * Sets @iter up to walk the &drm_mode_config.connector_list of @dev. @iter
692  * must always be cleaned up again by calling drm_connector_list_iter_end().
693  * Iteration itself happens using drm_connector_list_iter_next() or
694  * drm_for_each_connector_iter().
695  */
696 void drm_connector_list_iter_begin(struct drm_device *dev,
697 				   struct drm_connector_list_iter *iter)
698 {
699 	iter->dev = dev;
700 	iter->conn = NULL;
701 	lock_acquire_shared_recursive(&connector_list_iter_dep_map, 0, 1, NULL, _RET_IP_);
702 }
703 EXPORT_SYMBOL(drm_connector_list_iter_begin);
704 
705 /*
706  * Extra-safe connector put function that works in any context. Should only be
707  * used from the connector_iter functions, where we never really expect to
708  * actually release the connector when dropping our final reference.
709  */
710 static void
711 __drm_connector_put_safe(struct drm_connector *conn)
712 {
713 	struct drm_mode_config *config = &conn->dev->mode_config;
714 
715 	lockdep_assert_held(&config->connector_list_lock);
716 
717 	if (!refcount_dec_and_test(&conn->base.refcount.refcount))
718 		return;
719 
720 	llist_add(&conn->free_node, &config->connector_free_list);
721 	schedule_work(&config->connector_free_work);
722 }
723 
724 /**
725  * drm_connector_list_iter_next - return next connector
726  * @iter: connector_list iterator
727  *
728  * Returns: the next connector for @iter, or NULL when the list walk has
729  * completed.
730  */
731 struct drm_connector *
732 drm_connector_list_iter_next(struct drm_connector_list_iter *iter)
733 {
734 	struct drm_connector *old_conn = iter->conn;
735 	struct drm_mode_config *config = &iter->dev->mode_config;
736 	struct list_head *lhead;
737 	unsigned long flags;
738 
739 	spin_lock_irqsave(&config->connector_list_lock, flags);
740 	lhead = old_conn ? &old_conn->head : &config->connector_list;
741 
742 	do {
743 		if (lhead->next == &config->connector_list) {
744 			iter->conn = NULL;
745 			break;
746 		}
747 
748 		lhead = lhead->next;
749 		iter->conn = list_entry(lhead, struct drm_connector, head);
750 
751 		/* loop until it's not a zombie connector */
752 	} while (!kref_get_unless_zero(&iter->conn->base.refcount));
753 
754 	if (old_conn)
755 		__drm_connector_put_safe(old_conn);
756 	spin_unlock_irqrestore(&config->connector_list_lock, flags);
757 
758 	return iter->conn;
759 }
760 EXPORT_SYMBOL(drm_connector_list_iter_next);
761 
762 /**
763  * drm_connector_list_iter_end - tear down a connector_list iterator
764  * @iter: connector_list iterator
765  *
766  * Tears down @iter and releases any resources (like &drm_connector references)
767  * acquired while walking the list. This must always be called, both when the
768  * iteration completes fully or when it was aborted without walking the entire
769  * list.
770  */
771 void drm_connector_list_iter_end(struct drm_connector_list_iter *iter)
772 {
773 	struct drm_mode_config *config = &iter->dev->mode_config;
774 	unsigned long flags;
775 
776 	iter->dev = NULL;
777 	if (iter->conn) {
778 		spin_lock_irqsave(&config->connector_list_lock, flags);
779 		__drm_connector_put_safe(iter->conn);
780 		spin_unlock_irqrestore(&config->connector_list_lock, flags);
781 	}
782 	lock_release(&connector_list_iter_dep_map, _RET_IP_);
783 }
784 EXPORT_SYMBOL(drm_connector_list_iter_end);
785 
786 static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
787 	{ SubPixelUnknown, "Unknown" },
788 	{ SubPixelHorizontalRGB, "Horizontal RGB" },
789 	{ SubPixelHorizontalBGR, "Horizontal BGR" },
790 	{ SubPixelVerticalRGB, "Vertical RGB" },
791 	{ SubPixelVerticalBGR, "Vertical BGR" },
792 	{ SubPixelNone, "None" },
793 };
794 
795 /**
796  * drm_get_subpixel_order_name - return a string for a given subpixel enum
797  * @order: enum of subpixel_order
798  *
799  * Note you could abuse this and return something out of bounds, but that
800  * would be a caller error.  No unscrubbed user data should make it here.
801  *
802  * Returns: string describing an enumerated subpixel property
803  */
804 const char *drm_get_subpixel_order_name(enum subpixel_order order)
805 {
806 	return drm_subpixel_enum_list[order].name;
807 }
808 EXPORT_SYMBOL(drm_get_subpixel_order_name);
809 
810 static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
811 	{ DRM_MODE_DPMS_ON, "On" },
812 	{ DRM_MODE_DPMS_STANDBY, "Standby" },
813 	{ DRM_MODE_DPMS_SUSPEND, "Suspend" },
814 	{ DRM_MODE_DPMS_OFF, "Off" }
815 };
816 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
817 
818 static const struct drm_prop_enum_list drm_link_status_enum_list[] = {
819 	{ DRM_MODE_LINK_STATUS_GOOD, "Good" },
820 	{ DRM_MODE_LINK_STATUS_BAD, "Bad" },
821 };
822 
823 /**
824  * drm_display_info_set_bus_formats - set the supported bus formats
825  * @info: display info to store bus formats in
826  * @formats: array containing the supported bus formats
827  * @num_formats: the number of entries in the fmts array
828  *
829  * Store the supported bus formats in display info structure.
830  * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
831  * a full list of available formats.
832  *
833  * Returns:
834  * 0 on success or a negative error code on failure.
835  */
836 int drm_display_info_set_bus_formats(struct drm_display_info *info,
837 				     const u32 *formats,
838 				     unsigned int num_formats)
839 {
840 	u32 *fmts = NULL;
841 
842 	if (!formats && num_formats)
843 		return -EINVAL;
844 
845 	if (formats && num_formats) {
846 		fmts = kmemdup(formats, sizeof(*formats) * num_formats,
847 			       GFP_KERNEL);
848 		if (!fmts)
849 			return -ENOMEM;
850 	}
851 
852 	kfree(info->bus_formats);
853 	info->bus_formats = fmts;
854 	info->num_bus_formats = num_formats;
855 
856 	return 0;
857 }
858 EXPORT_SYMBOL(drm_display_info_set_bus_formats);
859 
860 /* Optional connector properties. */
861 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
862 	{ DRM_MODE_SCALE_NONE, "None" },
863 	{ DRM_MODE_SCALE_FULLSCREEN, "Full" },
864 	{ DRM_MODE_SCALE_CENTER, "Center" },
865 	{ DRM_MODE_SCALE_ASPECT, "Full aspect" },
866 };
867 
868 static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
869 	{ DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
870 	{ DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
871 	{ DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
872 };
873 
874 static const struct drm_prop_enum_list drm_content_type_enum_list[] = {
875 	{ DRM_MODE_CONTENT_TYPE_NO_DATA, "No Data" },
876 	{ DRM_MODE_CONTENT_TYPE_GRAPHICS, "Graphics" },
877 	{ DRM_MODE_CONTENT_TYPE_PHOTO, "Photo" },
878 	{ DRM_MODE_CONTENT_TYPE_CINEMA, "Cinema" },
879 	{ DRM_MODE_CONTENT_TYPE_GAME, "Game" },
880 };
881 
882 static const struct drm_prop_enum_list drm_panel_orientation_enum_list[] = {
883 	{ DRM_MODE_PANEL_ORIENTATION_NORMAL,	"Normal"	},
884 	{ DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP,	"Upside Down"	},
885 	{ DRM_MODE_PANEL_ORIENTATION_LEFT_UP,	"Left Side Up"	},
886 	{ DRM_MODE_PANEL_ORIENTATION_RIGHT_UP,	"Right Side Up"	},
887 };
888 
889 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
890 	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
891 	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
892 	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
893 };
894 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
895 
896 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
897 	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I, TV-out and DP */
898 	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
899 	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
900 };
901 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
902 		 drm_dvi_i_subconnector_enum_list)
903 
904 static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
905 	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
906 	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
907 	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
908 	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
909 	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
910 };
911 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
912 
913 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
914 	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I, TV-out and DP */
915 	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
916 	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
917 	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
918 	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
919 };
920 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
921 		 drm_tv_subconnector_enum_list)
922 
923 static const struct drm_prop_enum_list drm_dp_subconnector_enum_list[] = {
924 	{ DRM_MODE_SUBCONNECTOR_Unknown,     "Unknown"   }, /* DVI-I, TV-out and DP */
925 	{ DRM_MODE_SUBCONNECTOR_VGA,	     "VGA"       }, /* DP */
926 	{ DRM_MODE_SUBCONNECTOR_DVID,	     "DVI-D"     }, /* DP */
927 	{ DRM_MODE_SUBCONNECTOR_HDMIA,	     "HDMI"      }, /* DP */
928 	{ DRM_MODE_SUBCONNECTOR_DisplayPort, "DP"        }, /* DP */
929 	{ DRM_MODE_SUBCONNECTOR_Wireless,    "Wireless"  }, /* DP */
930 	{ DRM_MODE_SUBCONNECTOR_Native,	     "Native"    }, /* DP */
931 };
932 
933 DRM_ENUM_NAME_FN(drm_get_dp_subconnector_name,
934 		 drm_dp_subconnector_enum_list)
935 
936 static const struct drm_prop_enum_list hdmi_colorspaces[] = {
937 	/* For Default case, driver will set the colorspace */
938 	{ DRM_MODE_COLORIMETRY_DEFAULT, "Default" },
939 	/* Standard Definition Colorimetry based on CEA 861 */
940 	{ DRM_MODE_COLORIMETRY_SMPTE_170M_YCC, "SMPTE_170M_YCC" },
941 	{ DRM_MODE_COLORIMETRY_BT709_YCC, "BT709_YCC" },
942 	/* Standard Definition Colorimetry based on IEC 61966-2-4 */
943 	{ DRM_MODE_COLORIMETRY_XVYCC_601, "XVYCC_601" },
944 	/* High Definition Colorimetry based on IEC 61966-2-4 */
945 	{ DRM_MODE_COLORIMETRY_XVYCC_709, "XVYCC_709" },
946 	/* Colorimetry based on IEC 61966-2-1/Amendment 1 */
947 	{ DRM_MODE_COLORIMETRY_SYCC_601, "SYCC_601" },
948 	/* Colorimetry based on IEC 61966-2-5 [33] */
949 	{ DRM_MODE_COLORIMETRY_OPYCC_601, "opYCC_601" },
950 	/* Colorimetry based on IEC 61966-2-5 */
951 	{ DRM_MODE_COLORIMETRY_OPRGB, "opRGB" },
952 	/* Colorimetry based on ITU-R BT.2020 */
953 	{ DRM_MODE_COLORIMETRY_BT2020_CYCC, "BT2020_CYCC" },
954 	/* Colorimetry based on ITU-R BT.2020 */
955 	{ DRM_MODE_COLORIMETRY_BT2020_RGB, "BT2020_RGB" },
956 	/* Colorimetry based on ITU-R BT.2020 */
957 	{ DRM_MODE_COLORIMETRY_BT2020_YCC, "BT2020_YCC" },
958 	/* Added as part of Additional Colorimetry Extension in 861.G */
959 	{ DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65, "DCI-P3_RGB_D65" },
960 	{ DRM_MODE_COLORIMETRY_DCI_P3_RGB_THEATER, "DCI-P3_RGB_Theater" },
961 };
962 
963 /*
964  * As per DP 1.4a spec, 2.2.5.7.5 VSC SDP Payload for Pixel Encoding/Colorimetry
965  * Format Table 2-120
966  */
967 static const struct drm_prop_enum_list dp_colorspaces[] = {
968 	/* For Default case, driver will set the colorspace */
969 	{ DRM_MODE_COLORIMETRY_DEFAULT, "Default" },
970 	{ DRM_MODE_COLORIMETRY_RGB_WIDE_FIXED, "RGB_Wide_Gamut_Fixed_Point" },
971 	/* Colorimetry based on scRGB (IEC 61966-2-2) */
972 	{ DRM_MODE_COLORIMETRY_RGB_WIDE_FLOAT, "RGB_Wide_Gamut_Floating_Point" },
973 	/* Colorimetry based on IEC 61966-2-5 */
974 	{ DRM_MODE_COLORIMETRY_OPRGB, "opRGB" },
975 	/* Colorimetry based on SMPTE RP 431-2 */
976 	{ DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65, "DCI-P3_RGB_D65" },
977 	/* Colorimetry based on ITU-R BT.2020 */
978 	{ DRM_MODE_COLORIMETRY_BT2020_RGB, "BT2020_RGB" },
979 	{ DRM_MODE_COLORIMETRY_BT601_YCC, "BT601_YCC" },
980 	{ DRM_MODE_COLORIMETRY_BT709_YCC, "BT709_YCC" },
981 	/* Standard Definition Colorimetry based on IEC 61966-2-4 */
982 	{ DRM_MODE_COLORIMETRY_XVYCC_601, "XVYCC_601" },
983 	/* High Definition Colorimetry based on IEC 61966-2-4 */
984 	{ DRM_MODE_COLORIMETRY_XVYCC_709, "XVYCC_709" },
985 	/* Colorimetry based on IEC 61966-2-1/Amendment 1 */
986 	{ DRM_MODE_COLORIMETRY_SYCC_601, "SYCC_601" },
987 	/* Colorimetry based on IEC 61966-2-5 [33] */
988 	{ DRM_MODE_COLORIMETRY_OPYCC_601, "opYCC_601" },
989 	/* Colorimetry based on ITU-R BT.2020 */
990 	{ DRM_MODE_COLORIMETRY_BT2020_CYCC, "BT2020_CYCC" },
991 	/* Colorimetry based on ITU-R BT.2020 */
992 	{ DRM_MODE_COLORIMETRY_BT2020_YCC, "BT2020_YCC" },
993 };
994 
995 /**
996  * DOC: standard connector properties
997  *
998  * DRM connectors have a few standardized properties:
999  *
1000  * EDID:
1001  * 	Blob property which contains the current EDID read from the sink. This
1002  * 	is useful to parse sink identification information like vendor, model
1003  * 	and serial. Drivers should update this property by calling
1004  * 	drm_connector_update_edid_property(), usually after having parsed
1005  * 	the EDID using drm_add_edid_modes(). Userspace cannot change this
1006  * 	property.
1007  *
1008  * 	User-space should not parse the EDID to obtain information exposed via
1009  * 	other KMS properties (because the kernel might apply limits, quirks or
1010  * 	fixups to the EDID). For instance, user-space should not try to parse
1011  * 	mode lists from the EDID.
1012  * DPMS:
1013  * 	Legacy property for setting the power state of the connector. For atomic
1014  * 	drivers this is only provided for backwards compatibility with existing
1015  * 	drivers, it remaps to controlling the "ACTIVE" property on the CRTC the
1016  * 	connector is linked to. Drivers should never set this property directly,
1017  * 	it is handled by the DRM core by calling the &drm_connector_funcs.dpms
1018  * 	callback. For atomic drivers the remapping to the "ACTIVE" property is
1019  * 	implemented in the DRM core.
1020  *
1021  * 	Note that this property cannot be set through the MODE_ATOMIC ioctl,
1022  * 	userspace must use "ACTIVE" on the CRTC instead.
1023  *
1024  * 	WARNING:
1025  *
1026  * 	For userspace also running on legacy drivers the "DPMS" semantics are a
1027  * 	lot more complicated. First, userspace cannot rely on the "DPMS" value
1028  * 	returned by the GETCONNECTOR actually reflecting reality, because many
1029  * 	drivers fail to update it. For atomic drivers this is taken care of in
1030  * 	drm_atomic_helper_update_legacy_modeset_state().
1031  *
1032  * 	The second issue is that the DPMS state is only well-defined when the
1033  * 	connector is connected to a CRTC. In atomic the DRM core enforces that
1034  * 	"ACTIVE" is off in such a case, no such checks exists for "DPMS".
1035  *
1036  * 	Finally, when enabling an output using the legacy SETCONFIG ioctl then
1037  * 	"DPMS" is forced to ON. But see above, that might not be reflected in
1038  * 	the software value on legacy drivers.
1039  *
1040  * 	Summarizing: Only set "DPMS" when the connector is known to be enabled,
1041  * 	assume that a successful SETCONFIG call also sets "DPMS" to on, and
1042  * 	never read back the value of "DPMS" because it can be incorrect.
1043  * PATH:
1044  * 	Connector path property to identify how this sink is physically
1045  * 	connected. Used by DP MST. This should be set by calling
1046  * 	drm_connector_set_path_property(), in the case of DP MST with the
1047  * 	path property the MST manager created. Userspace cannot change this
1048  * 	property.
1049  * TILE:
1050  * 	Connector tile group property to indicate how a set of DRM connector
1051  * 	compose together into one logical screen. This is used by both high-res
1052  * 	external screens (often only using a single cable, but exposing multiple
1053  * 	DP MST sinks), or high-res integrated panels (like dual-link DSI) which
1054  * 	are not gen-locked. Note that for tiled panels which are genlocked, like
1055  * 	dual-link LVDS or dual-link DSI, the driver should try to not expose the
1056  * 	tiling and virtualise both &drm_crtc and &drm_plane if needed. Drivers
1057  * 	should update this value using drm_connector_set_tile_property().
1058  * 	Userspace cannot change this property.
1059  * link-status:
1060  *      Connector link-status property to indicate the status of link. The
1061  *      default value of link-status is "GOOD". If something fails during or
1062  *      after modeset, the kernel driver may set this to "BAD" and issue a
1063  *      hotplug uevent. Drivers should update this value using
1064  *      drm_connector_set_link_status_property().
1065  *
1066  *      When user-space receives the hotplug uevent and detects a "BAD"
1067  *      link-status, the sink doesn't receive pixels anymore (e.g. the screen
1068  *      becomes completely black). The list of available modes may have
1069  *      changed. User-space is expected to pick a new mode if the current one
1070  *      has disappeared and perform a new modeset with link-status set to
1071  *      "GOOD" to re-enable the connector.
1072  *
1073  *      If multiple connectors share the same CRTC and one of them gets a "BAD"
1074  *      link-status, the other are unaffected (ie. the sinks still continue to
1075  *      receive pixels).
1076  *
1077  *      When user-space performs an atomic commit on a connector with a "BAD"
1078  *      link-status without resetting the property to "GOOD", the sink may
1079  *      still not receive pixels. When user-space performs an atomic commit
1080  *      which resets the link-status property to "GOOD" without the
1081  *      ALLOW_MODESET flag set, it might fail because a modeset is required.
1082  *
1083  *      User-space can only change link-status to "GOOD", changing it to "BAD"
1084  *      is a no-op.
1085  *
1086  *      For backwards compatibility with non-atomic userspace the kernel
1087  *      tries to automatically set the link-status back to "GOOD" in the
1088  *      SETCRTC IOCTL. This might fail if the mode is no longer valid, similar
1089  *      to how it might fail if a different screen has been connected in the
1090  *      interim.
1091  * non_desktop:
1092  * 	Indicates the output should be ignored for purposes of displaying a
1093  * 	standard desktop environment or console. This is most likely because
1094  * 	the output device is not rectilinear.
1095  * Content Protection:
1096  *	This property is used by userspace to request the kernel protect future
1097  *	content communicated over the link. When requested, kernel will apply
1098  *	the appropriate means of protection (most often HDCP), and use the
1099  *	property to tell userspace the protection is active.
1100  *
1101  *	Drivers can set this up by calling
1102  *	drm_connector_attach_content_protection_property() on initialization.
1103  *
1104  *	The value of this property can be one of the following:
1105  *
1106  *	DRM_MODE_CONTENT_PROTECTION_UNDESIRED = 0
1107  *		The link is not protected, content is transmitted in the clear.
1108  *	DRM_MODE_CONTENT_PROTECTION_DESIRED = 1
1109  *		Userspace has requested content protection, but the link is not
1110  *		currently protected. When in this state, kernel should enable
1111  *		Content Protection as soon as possible.
1112  *	DRM_MODE_CONTENT_PROTECTION_ENABLED = 2
1113  *		Userspace has requested content protection, and the link is
1114  *		protected. Only the driver can set the property to this value.
1115  *		If userspace attempts to set to ENABLED, kernel will return
1116  *		-EINVAL.
1117  *
1118  *	A few guidelines:
1119  *
1120  *	- DESIRED state should be preserved until userspace de-asserts it by
1121  *	  setting the property to UNDESIRED. This means ENABLED should only
1122  *	  transition to UNDESIRED when the user explicitly requests it.
1123  *	- If the state is DESIRED, kernel should attempt to re-authenticate the
1124  *	  link whenever possible. This includes across disable/enable, dpms,
1125  *	  hotplug, downstream device changes, link status failures, etc..
1126  *	- Kernel sends uevent with the connector id and property id through
1127  *	  @drm_hdcp_update_content_protection, upon below kernel triggered
1128  *	  scenarios:
1129  *
1130  *		- DESIRED -> ENABLED (authentication success)
1131  *		- ENABLED -> DESIRED (termination of authentication)
1132  *	- Please note no uevents for userspace triggered property state changes,
1133  *	  which can't fail such as
1134  *
1135  *		- DESIRED/ENABLED -> UNDESIRED
1136  *		- UNDESIRED -> DESIRED
1137  *	- Userspace is responsible for polling the property or listen to uevents
1138  *	  to determine when the value transitions from ENABLED to DESIRED.
1139  *	  This signifies the link is no longer protected and userspace should
1140  *	  take appropriate action (whatever that might be).
1141  *
1142  * HDCP Content Type:
1143  *	This Enum property is used by the userspace to declare the content type
1144  *	of the display stream, to kernel. Here display stream stands for any
1145  *	display content that userspace intended to display through HDCP
1146  *	encryption.
1147  *
1148  *	Content Type of a stream is decided by the owner of the stream, as
1149  *	"HDCP Type0" or "HDCP Type1".
1150  *
1151  *	The value of the property can be one of the below:
1152  *	  - "HDCP Type0": DRM_MODE_HDCP_CONTENT_TYPE0 = 0
1153  *	  - "HDCP Type1": DRM_MODE_HDCP_CONTENT_TYPE1 = 1
1154  *
1155  *	When kernel starts the HDCP authentication (see "Content Protection"
1156  *	for details), it uses the content type in "HDCP Content Type"
1157  *	for performing the HDCP authentication with the display sink.
1158  *
1159  *	Please note in HDCP spec versions, a link can be authenticated with
1160  *	HDCP 2.2 for Content Type 0/Content Type 1. Where as a link can be
1161  *	authenticated with HDCP1.4 only for Content Type 0(though it is implicit
1162  *	in nature. As there is no reference for Content Type in HDCP1.4).
1163  *
1164  *	HDCP2.2 authentication protocol itself takes the "Content Type" as a
1165  *	parameter, which is a input for the DP HDCP2.2 encryption algo.
1166  *
1167  *	In case of Type 0 content protection request, kernel driver can choose
1168  *	either of HDCP spec versions 1.4 and 2.2. When HDCP2.2 is used for
1169  *	"HDCP Type 0", a HDCP 2.2 capable repeater in the downstream can send
1170  *	that content to a HDCP 1.4 authenticated HDCP sink (Type0 link).
1171  *	But if the content is classified as "HDCP Type 1", above mentioned
1172  *	HDCP 2.2 repeater wont send the content to the HDCP sink as it can't
1173  *	authenticate the HDCP1.4 capable sink for "HDCP Type 1".
1174  *
1175  *	Please note userspace can be ignorant of the HDCP versions used by the
1176  *	kernel driver to achieve the "HDCP Content Type".
1177  *
1178  *	At current scenario, classifying a content as Type 1 ensures that the
1179  *	content will be displayed only through the HDCP2.2 encrypted link.
1180  *
1181  *	Note that the HDCP Content Type property is introduced at HDCP 2.2, and
1182  *	defaults to type 0. It is only exposed by drivers supporting HDCP 2.2
1183  *	(hence supporting Type 0 and Type 1). Based on how next versions of
1184  *	HDCP specs are defined content Type could be used for higher versions
1185  *	too.
1186  *
1187  *	If content type is changed when "Content Protection" is not UNDESIRED,
1188  *	then kernel will disable the HDCP and re-enable with new type in the
1189  *	same atomic commit. And when "Content Protection" is ENABLED, it means
1190  *	that link is HDCP authenticated and encrypted, for the transmission of
1191  *	the Type of stream mentioned at "HDCP Content Type".
1192  *
1193  * HDR_OUTPUT_METADATA:
1194  *	Connector property to enable userspace to send HDR Metadata to
1195  *	driver. This metadata is based on the composition and blending
1196  *	policies decided by user, taking into account the hardware and
1197  *	sink capabilities. The driver gets this metadata and creates a
1198  *	Dynamic Range and Mastering Infoframe (DRM) in case of HDMI,
1199  *	SDP packet (Non-audio INFOFRAME SDP v1.3) for DP. This is then
1200  *	sent to sink. This notifies the sink of the upcoming frame's Color
1201  *	Encoding and Luminance parameters.
1202  *
1203  *	Userspace first need to detect the HDR capabilities of sink by
1204  *	reading and parsing the EDID. Details of HDR metadata for HDMI
1205  *	are added in CTA 861.G spec. For DP , its defined in VESA DP
1206  *	Standard v1.4. It needs to then get the metadata information
1207  *	of the video/game/app content which are encoded in HDR (basically
1208  *	using HDR transfer functions). With this information it needs to
1209  *	decide on a blending policy and compose the relevant
1210  *	layers/overlays into a common format. Once this blending is done,
1211  *	userspace will be aware of the metadata of the composed frame to
1212  *	be send to sink. It then uses this property to communicate this
1213  *	metadata to driver which then make a Infoframe packet and sends
1214  *	to sink based on the type of encoder connected.
1215  *
1216  *	Userspace will be responsible to do Tone mapping operation in case:
1217  *		- Some layers are HDR and others are SDR
1218  *		- HDR layers luminance is not same as sink
1219  *
1220  *	It will even need to do colorspace conversion and get all layers
1221  *	to one common colorspace for blending. It can use either GL, Media
1222  *	or display engine to get this done based on the capabilities of the
1223  *	associated hardware.
1224  *
1225  *	Driver expects metadata to be put in &struct hdr_output_metadata
1226  *	structure from userspace. This is received as blob and stored in
1227  *	&drm_connector_state.hdr_output_metadata. It parses EDID and saves the
1228  *	sink metadata in &struct hdr_sink_metadata, as
1229  *	&drm_connector.hdr_sink_metadata.  Driver uses
1230  *	drm_hdmi_infoframe_set_hdr_metadata() helper to set the HDR metadata,
1231  *	hdmi_drm_infoframe_pack() to pack the infoframe as per spec, in case of
1232  *	HDMI encoder.
1233  *
1234  * max bpc:
1235  *	This range property is used by userspace to limit the bit depth. When
1236  *	used the driver would limit the bpc in accordance with the valid range
1237  *	supported by the hardware and sink. Drivers to use the function
1238  *	drm_connector_attach_max_bpc_property() to create and attach the
1239  *	property to the connector during initialization.
1240  *
1241  * Connectors also have one standardized atomic property:
1242  *
1243  * CRTC_ID:
1244  * 	Mode object ID of the &drm_crtc this connector should be connected to.
1245  *
1246  * Connectors for LCD panels may also have one standardized property:
1247  *
1248  * panel orientation:
1249  *	On some devices the LCD panel is mounted in the casing in such a way
1250  *	that the up/top side of the panel does not match with the top side of
1251  *	the device. Userspace can use this property to check for this.
1252  *	Note that input coordinates from touchscreens (input devices with
1253  *	INPUT_PROP_DIRECT) will still map 1:1 to the actual LCD panel
1254  *	coordinates, so if userspace rotates the picture to adjust for
1255  *	the orientation it must also apply the same transformation to the
1256  *	touchscreen input coordinates. This property is initialized by calling
1257  *	drm_connector_set_panel_orientation() or
1258  *	drm_connector_set_panel_orientation_with_quirk()
1259  *
1260  * scaling mode:
1261  *	This property defines how a non-native mode is upscaled to the native
1262  *	mode of an LCD panel:
1263  *
1264  *	None:
1265  *		No upscaling happens, scaling is left to the panel. Not all
1266  *		drivers expose this mode.
1267  *	Full:
1268  *		The output is upscaled to the full resolution of the panel,
1269  *		ignoring the aspect ratio.
1270  *	Center:
1271  *		No upscaling happens, the output is centered within the native
1272  *		resolution the panel.
1273  *	Full aspect:
1274  *		The output is upscaled to maximize either the width or height
1275  *		while retaining the aspect ratio.
1276  *
1277  *	This property should be set up by calling
1278  *	drm_connector_attach_scaling_mode_property(). Note that drivers
1279  *	can also expose this property to external outputs, in which case they
1280  *	must support "None", which should be the default (since external screens
1281  *	have a built-in scaler).
1282  *
1283  * subconnector:
1284  *	This property is used by DVI-I, TVout and DisplayPort to indicate different
1285  *	connector subtypes. Enum values more or less match with those from main
1286  *	connector types.
1287  *	For DVI-I and TVout there is also a matching property "select subconnector"
1288  *	allowing to switch between signal types.
1289  *	DP subconnector corresponds to a downstream port.
1290  *
1291  * privacy-screen sw-state, privacy-screen hw-state:
1292  *	These 2 optional properties can be used to query the state of the
1293  *	electronic privacy screen that is available on some displays; and in
1294  *	some cases also control the state. If a driver implements these
1295  *	properties then both properties must be present.
1296  *
1297  *	"privacy-screen hw-state" is read-only and reflects the actual state
1298  *	of the privacy-screen, possible values: "Enabled", "Disabled,
1299  *	"Enabled-locked", "Disabled-locked". The locked states indicate
1300  *	that the state cannot be changed through the DRM API. E.g. there
1301  *	might be devices where the firmware-setup options, or a hardware
1302  *	slider-switch, offer always on / off modes.
1303  *
1304  *	"privacy-screen sw-state" can be set to change the privacy-screen state
1305  *	when not locked. In this case the driver must update the hw-state
1306  *	property to reflect the new state on completion of the commit of the
1307  *	sw-state property. Setting the sw-state property when the hw-state is
1308  *	locked must be interpreted by the driver as a request to change the
1309  *	state to the set state when the hw-state becomes unlocked. E.g. if
1310  *	"privacy-screen hw-state" is "Enabled-locked" and the sw-state
1311  *	gets set to "Disabled" followed by the user unlocking the state by
1312  *	changing the slider-switch position, then the driver must set the
1313  *	state to "Disabled" upon receiving the unlock event.
1314  *
1315  *	In some cases the privacy-screen's actual state might change outside of
1316  *	control of the DRM code. E.g. there might be a firmware handled hotkey
1317  *	which toggles the actual state, or the actual state might be changed
1318  *	through another userspace API such as writing /proc/acpi/ibm/lcdshadow.
1319  *	In this case the driver must update both the hw-state and the sw-state
1320  *	to reflect the new value, overwriting any pending state requests in the
1321  *	sw-state. Any pending sw-state requests are thus discarded.
1322  *
1323  *	Note that the ability for the state to change outside of control of
1324  *	the DRM master process means that userspace must not cache the value
1325  *	of the sw-state. Caching the sw-state value and including it in later
1326  *	atomic commits may lead to overriding a state change done through e.g.
1327  *	a firmware handled hotkey. Therefor userspace must not include the
1328  *	privacy-screen sw-state in an atomic commit unless it wants to change
1329  *	its value.
1330  */
1331 
1332 int drm_connector_create_standard_properties(struct drm_device *dev)
1333 {
1334 	struct drm_property *prop;
1335 
1336 	prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1337 				   DRM_MODE_PROP_IMMUTABLE,
1338 				   "EDID", 0);
1339 	if (!prop)
1340 		return -ENOMEM;
1341 	dev->mode_config.edid_property = prop;
1342 
1343 	prop = drm_property_create_enum(dev, 0,
1344 				   "DPMS", drm_dpms_enum_list,
1345 				   ARRAY_SIZE(drm_dpms_enum_list));
1346 	if (!prop)
1347 		return -ENOMEM;
1348 	dev->mode_config.dpms_property = prop;
1349 
1350 	prop = drm_property_create(dev,
1351 				   DRM_MODE_PROP_BLOB |
1352 				   DRM_MODE_PROP_IMMUTABLE,
1353 				   "PATH", 0);
1354 	if (!prop)
1355 		return -ENOMEM;
1356 	dev->mode_config.path_property = prop;
1357 
1358 	prop = drm_property_create(dev,
1359 				   DRM_MODE_PROP_BLOB |
1360 				   DRM_MODE_PROP_IMMUTABLE,
1361 				   "TILE", 0);
1362 	if (!prop)
1363 		return -ENOMEM;
1364 	dev->mode_config.tile_property = prop;
1365 
1366 	prop = drm_property_create_enum(dev, 0, "link-status",
1367 					drm_link_status_enum_list,
1368 					ARRAY_SIZE(drm_link_status_enum_list));
1369 	if (!prop)
1370 		return -ENOMEM;
1371 	dev->mode_config.link_status_property = prop;
1372 
1373 	prop = drm_property_create_bool(dev, DRM_MODE_PROP_IMMUTABLE, "non-desktop");
1374 	if (!prop)
1375 		return -ENOMEM;
1376 	dev->mode_config.non_desktop_property = prop;
1377 
1378 	prop = drm_property_create(dev, DRM_MODE_PROP_BLOB,
1379 				   "HDR_OUTPUT_METADATA", 0);
1380 	if (!prop)
1381 		return -ENOMEM;
1382 	dev->mode_config.hdr_output_metadata_property = prop;
1383 
1384 	return 0;
1385 }
1386 
1387 /**
1388  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1389  * @dev: DRM device
1390  *
1391  * Called by a driver the first time a DVI-I connector is made.
1392  *
1393  * Returns: %0
1394  */
1395 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1396 {
1397 	struct drm_property *dvi_i_selector;
1398 	struct drm_property *dvi_i_subconnector;
1399 
1400 	if (dev->mode_config.dvi_i_select_subconnector_property)
1401 		return 0;
1402 
1403 	dvi_i_selector =
1404 		drm_property_create_enum(dev, 0,
1405 				    "select subconnector",
1406 				    drm_dvi_i_select_enum_list,
1407 				    ARRAY_SIZE(drm_dvi_i_select_enum_list));
1408 	dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1409 
1410 	dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1411 				    "subconnector",
1412 				    drm_dvi_i_subconnector_enum_list,
1413 				    ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1414 	dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1415 
1416 	return 0;
1417 }
1418 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1419 
1420 /**
1421  * drm_connector_attach_dp_subconnector_property - create subconnector property for DP
1422  * @connector: drm_connector to attach property
1423  *
1424  * Called by a driver when DP connector is created.
1425  */
1426 void drm_connector_attach_dp_subconnector_property(struct drm_connector *connector)
1427 {
1428 	struct drm_mode_config *mode_config = &connector->dev->mode_config;
1429 
1430 	if (!mode_config->dp_subconnector_property)
1431 		mode_config->dp_subconnector_property =
1432 			drm_property_create_enum(connector->dev,
1433 				DRM_MODE_PROP_IMMUTABLE,
1434 				"subconnector",
1435 				drm_dp_subconnector_enum_list,
1436 				ARRAY_SIZE(drm_dp_subconnector_enum_list));
1437 
1438 	drm_object_attach_property(&connector->base,
1439 				   mode_config->dp_subconnector_property,
1440 				   DRM_MODE_SUBCONNECTOR_Unknown);
1441 }
1442 EXPORT_SYMBOL(drm_connector_attach_dp_subconnector_property);
1443 
1444 /**
1445  * DOC: HDMI connector properties
1446  *
1447  * content type (HDMI specific):
1448  *	Indicates content type setting to be used in HDMI infoframes to indicate
1449  *	content type for the external device, so that it adjusts its display
1450  *	settings accordingly.
1451  *
1452  *	The value of this property can be one of the following:
1453  *
1454  *	No Data:
1455  *		Content type is unknown
1456  *	Graphics:
1457  *		Content type is graphics
1458  *	Photo:
1459  *		Content type is photo
1460  *	Cinema:
1461  *		Content type is cinema
1462  *	Game:
1463  *		Content type is game
1464  *
1465  *	The meaning of each content type is defined in CTA-861-G table 15.
1466  *
1467  *	Drivers can set up this property by calling
1468  *	drm_connector_attach_content_type_property(). Decoding to
1469  *	infoframe values is done through drm_hdmi_avi_infoframe_content_type().
1470  */
1471 
1472 /**
1473  * drm_connector_attach_content_type_property - attach content-type property
1474  * @connector: connector to attach content type property on.
1475  *
1476  * Called by a driver the first time a HDMI connector is made.
1477  *
1478  * Returns: %0
1479  */
1480 int drm_connector_attach_content_type_property(struct drm_connector *connector)
1481 {
1482 	if (!drm_mode_create_content_type_property(connector->dev))
1483 		drm_object_attach_property(&connector->base,
1484 					   connector->dev->mode_config.content_type_property,
1485 					   DRM_MODE_CONTENT_TYPE_NO_DATA);
1486 	return 0;
1487 }
1488 EXPORT_SYMBOL(drm_connector_attach_content_type_property);
1489 
1490 /**
1491  * drm_connector_attach_tv_margin_properties - attach TV connector margin
1492  * 	properties
1493  * @connector: DRM connector
1494  *
1495  * Called by a driver when it needs to attach TV margin props to a connector.
1496  * Typically used on SDTV and HDMI connectors.
1497  */
1498 void drm_connector_attach_tv_margin_properties(struct drm_connector *connector)
1499 {
1500 	struct drm_device *dev = connector->dev;
1501 
1502 	drm_object_attach_property(&connector->base,
1503 				   dev->mode_config.tv_left_margin_property,
1504 				   0);
1505 	drm_object_attach_property(&connector->base,
1506 				   dev->mode_config.tv_right_margin_property,
1507 				   0);
1508 	drm_object_attach_property(&connector->base,
1509 				   dev->mode_config.tv_top_margin_property,
1510 				   0);
1511 	drm_object_attach_property(&connector->base,
1512 				   dev->mode_config.tv_bottom_margin_property,
1513 				   0);
1514 }
1515 EXPORT_SYMBOL(drm_connector_attach_tv_margin_properties);
1516 
1517 /**
1518  * drm_mode_create_tv_margin_properties - create TV connector margin properties
1519  * @dev: DRM device
1520  *
1521  * Called by a driver's HDMI connector initialization routine, this function
1522  * creates the TV margin properties for a given device. No need to call this
1523  * function for an SDTV connector, it's already called from
1524  * drm_mode_create_tv_properties().
1525  *
1526  * Returns:
1527  * 0 on success or a negative error code on failure.
1528  */
1529 int drm_mode_create_tv_margin_properties(struct drm_device *dev)
1530 {
1531 	if (dev->mode_config.tv_left_margin_property)
1532 		return 0;
1533 
1534 	dev->mode_config.tv_left_margin_property =
1535 		drm_property_create_range(dev, 0, "left margin", 0, 100);
1536 	if (!dev->mode_config.tv_left_margin_property)
1537 		return -ENOMEM;
1538 
1539 	dev->mode_config.tv_right_margin_property =
1540 		drm_property_create_range(dev, 0, "right margin", 0, 100);
1541 	if (!dev->mode_config.tv_right_margin_property)
1542 		return -ENOMEM;
1543 
1544 	dev->mode_config.tv_top_margin_property =
1545 		drm_property_create_range(dev, 0, "top margin", 0, 100);
1546 	if (!dev->mode_config.tv_top_margin_property)
1547 		return -ENOMEM;
1548 
1549 	dev->mode_config.tv_bottom_margin_property =
1550 		drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1551 	if (!dev->mode_config.tv_bottom_margin_property)
1552 		return -ENOMEM;
1553 
1554 	return 0;
1555 }
1556 EXPORT_SYMBOL(drm_mode_create_tv_margin_properties);
1557 
1558 /**
1559  * drm_mode_create_tv_properties - create TV specific connector properties
1560  * @dev: DRM device
1561  * @num_modes: number of different TV formats (modes) supported
1562  * @modes: array of pointers to strings containing name of each format
1563  *
1564  * Called by a driver's TV initialization routine, this function creates
1565  * the TV specific connector properties for a given device.  Caller is
1566  * responsible for allocating a list of format names and passing them to
1567  * this routine.
1568  *
1569  * Returns:
1570  * 0 on success or a negative error code on failure.
1571  */
1572 int drm_mode_create_tv_properties(struct drm_device *dev,
1573 				  unsigned int num_modes,
1574 				  const char * const modes[])
1575 {
1576 	struct drm_property *tv_selector;
1577 	struct drm_property *tv_subconnector;
1578 	unsigned int i;
1579 
1580 	if (dev->mode_config.tv_select_subconnector_property)
1581 		return 0;
1582 
1583 	/*
1584 	 * Basic connector properties
1585 	 */
1586 	tv_selector = drm_property_create_enum(dev, 0,
1587 					  "select subconnector",
1588 					  drm_tv_select_enum_list,
1589 					  ARRAY_SIZE(drm_tv_select_enum_list));
1590 	if (!tv_selector)
1591 		goto nomem;
1592 
1593 	dev->mode_config.tv_select_subconnector_property = tv_selector;
1594 
1595 	tv_subconnector =
1596 		drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1597 				    "subconnector",
1598 				    drm_tv_subconnector_enum_list,
1599 				    ARRAY_SIZE(drm_tv_subconnector_enum_list));
1600 	if (!tv_subconnector)
1601 		goto nomem;
1602 	dev->mode_config.tv_subconnector_property = tv_subconnector;
1603 
1604 	/*
1605 	 * Other, TV specific properties: margins & TV modes.
1606 	 */
1607 	if (drm_mode_create_tv_margin_properties(dev))
1608 		goto nomem;
1609 
1610 	dev->mode_config.tv_mode_property =
1611 		drm_property_create(dev, DRM_MODE_PROP_ENUM,
1612 				    "mode", num_modes);
1613 	if (!dev->mode_config.tv_mode_property)
1614 		goto nomem;
1615 
1616 	for (i = 0; i < num_modes; i++)
1617 		drm_property_add_enum(dev->mode_config.tv_mode_property,
1618 				      i, modes[i]);
1619 
1620 	dev->mode_config.tv_brightness_property =
1621 		drm_property_create_range(dev, 0, "brightness", 0, 100);
1622 	if (!dev->mode_config.tv_brightness_property)
1623 		goto nomem;
1624 
1625 	dev->mode_config.tv_contrast_property =
1626 		drm_property_create_range(dev, 0, "contrast", 0, 100);
1627 	if (!dev->mode_config.tv_contrast_property)
1628 		goto nomem;
1629 
1630 	dev->mode_config.tv_flicker_reduction_property =
1631 		drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1632 	if (!dev->mode_config.tv_flicker_reduction_property)
1633 		goto nomem;
1634 
1635 	dev->mode_config.tv_overscan_property =
1636 		drm_property_create_range(dev, 0, "overscan", 0, 100);
1637 	if (!dev->mode_config.tv_overscan_property)
1638 		goto nomem;
1639 
1640 	dev->mode_config.tv_saturation_property =
1641 		drm_property_create_range(dev, 0, "saturation", 0, 100);
1642 	if (!dev->mode_config.tv_saturation_property)
1643 		goto nomem;
1644 
1645 	dev->mode_config.tv_hue_property =
1646 		drm_property_create_range(dev, 0, "hue", 0, 100);
1647 	if (!dev->mode_config.tv_hue_property)
1648 		goto nomem;
1649 
1650 	return 0;
1651 nomem:
1652 	return -ENOMEM;
1653 }
1654 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1655 
1656 /**
1657  * drm_mode_create_scaling_mode_property - create scaling mode property
1658  * @dev: DRM device
1659  *
1660  * Called by a driver the first time it's needed, must be attached to desired
1661  * connectors.
1662  *
1663  * Atomic drivers should use drm_connector_attach_scaling_mode_property()
1664  * instead to correctly assign &drm_connector_state.scaling_mode
1665  * in the atomic state.
1666  *
1667  * Returns: %0
1668  */
1669 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1670 {
1671 	struct drm_property *scaling_mode;
1672 
1673 	if (dev->mode_config.scaling_mode_property)
1674 		return 0;
1675 
1676 	scaling_mode =
1677 		drm_property_create_enum(dev, 0, "scaling mode",
1678 				drm_scaling_mode_enum_list,
1679 				    ARRAY_SIZE(drm_scaling_mode_enum_list));
1680 
1681 	dev->mode_config.scaling_mode_property = scaling_mode;
1682 
1683 	return 0;
1684 }
1685 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1686 
1687 /**
1688  * DOC: Variable refresh properties
1689  *
1690  * Variable refresh rate capable displays can dynamically adjust their
1691  * refresh rate by extending the duration of their vertical front porch
1692  * until page flip or timeout occurs. This can reduce or remove stuttering
1693  * and latency in scenarios where the page flip does not align with the
1694  * vblank interval.
1695  *
1696  * An example scenario would be an application flipping at a constant rate
1697  * of 48Hz on a 60Hz display. The page flip will frequently miss the vblank
1698  * interval and the same contents will be displayed twice. This can be
1699  * observed as stuttering for content with motion.
1700  *
1701  * If variable refresh rate was active on a display that supported a
1702  * variable refresh range from 35Hz to 60Hz no stuttering would be observable
1703  * for the example scenario. The minimum supported variable refresh rate of
1704  * 35Hz is below the page flip frequency and the vertical front porch can
1705  * be extended until the page flip occurs. The vblank interval will be
1706  * directly aligned to the page flip rate.
1707  *
1708  * Not all userspace content is suitable for use with variable refresh rate.
1709  * Large and frequent changes in vertical front porch duration may worsen
1710  * perceived stuttering for input sensitive applications.
1711  *
1712  * Panel brightness will also vary with vertical front porch duration. Some
1713  * panels may have noticeable differences in brightness between the minimum
1714  * vertical front porch duration and the maximum vertical front porch duration.
1715  * Large and frequent changes in vertical front porch duration may produce
1716  * observable flickering for such panels.
1717  *
1718  * Userspace control for variable refresh rate is supported via properties
1719  * on the &drm_connector and &drm_crtc objects.
1720  *
1721  * "vrr_capable":
1722  *	Optional &drm_connector boolean property that drivers should attach
1723  *	with drm_connector_attach_vrr_capable_property() on connectors that
1724  *	could support variable refresh rates. Drivers should update the
1725  *	property value by calling drm_connector_set_vrr_capable_property().
1726  *
1727  *	Absence of the property should indicate absence of support.
1728  *
1729  * "VRR_ENABLED":
1730  *	Default &drm_crtc boolean property that notifies the driver that the
1731  *	content on the CRTC is suitable for variable refresh rate presentation.
1732  *	The driver will take this property as a hint to enable variable
1733  *	refresh rate support if the receiver supports it, ie. if the
1734  *	"vrr_capable" property is true on the &drm_connector object. The
1735  *	vertical front porch duration will be extended until page-flip or
1736  *	timeout when enabled.
1737  *
1738  *	The minimum vertical front porch duration is defined as the vertical
1739  *	front porch duration for the current mode.
1740  *
1741  *	The maximum vertical front porch duration is greater than or equal to
1742  *	the minimum vertical front porch duration. The duration is derived
1743  *	from the minimum supported variable refresh rate for the connector.
1744  *
1745  *	The driver may place further restrictions within these minimum
1746  *	and maximum bounds.
1747  */
1748 
1749 /**
1750  * drm_connector_attach_vrr_capable_property - creates the
1751  * vrr_capable property
1752  * @connector: connector to create the vrr_capable property on.
1753  *
1754  * This is used by atomic drivers to add support for querying
1755  * variable refresh rate capability for a connector.
1756  *
1757  * Returns:
1758  * Zero on success, negative errno on failure.
1759  */
1760 int drm_connector_attach_vrr_capable_property(
1761 	struct drm_connector *connector)
1762 {
1763 	struct drm_device *dev = connector->dev;
1764 	struct drm_property *prop;
1765 
1766 	if (!connector->vrr_capable_property) {
1767 		prop = drm_property_create_bool(dev, DRM_MODE_PROP_IMMUTABLE,
1768 			"vrr_capable");
1769 		if (!prop)
1770 			return -ENOMEM;
1771 
1772 		connector->vrr_capable_property = prop;
1773 		drm_object_attach_property(&connector->base, prop, 0);
1774 	}
1775 
1776 	return 0;
1777 }
1778 EXPORT_SYMBOL(drm_connector_attach_vrr_capable_property);
1779 
1780 /**
1781  * drm_connector_attach_scaling_mode_property - attach atomic scaling mode property
1782  * @connector: connector to attach scaling mode property on.
1783  * @scaling_mode_mask: or'ed mask of BIT(%DRM_MODE_SCALE_\*).
1784  *
1785  * This is used to add support for scaling mode to atomic drivers.
1786  * The scaling mode will be set to &drm_connector_state.scaling_mode
1787  * and can be used from &drm_connector_helper_funcs->atomic_check for validation.
1788  *
1789  * This is the atomic version of drm_mode_create_scaling_mode_property().
1790  *
1791  * Returns:
1792  * Zero on success, negative errno on failure.
1793  */
1794 int drm_connector_attach_scaling_mode_property(struct drm_connector *connector,
1795 					       u32 scaling_mode_mask)
1796 {
1797 	struct drm_device *dev = connector->dev;
1798 	struct drm_property *scaling_mode_property;
1799 	int i;
1800 	const unsigned valid_scaling_mode_mask =
1801 		(1U << ARRAY_SIZE(drm_scaling_mode_enum_list)) - 1;
1802 
1803 	if (WARN_ON(hweight32(scaling_mode_mask) < 2 ||
1804 		    scaling_mode_mask & ~valid_scaling_mode_mask))
1805 		return -EINVAL;
1806 
1807 	scaling_mode_property =
1808 		drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
1809 				    hweight32(scaling_mode_mask));
1810 
1811 	if (!scaling_mode_property)
1812 		return -ENOMEM;
1813 
1814 	for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++) {
1815 		int ret;
1816 
1817 		if (!(BIT(i) & scaling_mode_mask))
1818 			continue;
1819 
1820 		ret = drm_property_add_enum(scaling_mode_property,
1821 					    drm_scaling_mode_enum_list[i].type,
1822 					    drm_scaling_mode_enum_list[i].name);
1823 
1824 		if (ret) {
1825 			drm_property_destroy(dev, scaling_mode_property);
1826 
1827 			return ret;
1828 		}
1829 	}
1830 
1831 	drm_object_attach_property(&connector->base,
1832 				   scaling_mode_property, 0);
1833 
1834 	connector->scaling_mode_property = scaling_mode_property;
1835 
1836 	return 0;
1837 }
1838 EXPORT_SYMBOL(drm_connector_attach_scaling_mode_property);
1839 
1840 /**
1841  * drm_mode_create_aspect_ratio_property - create aspect ratio property
1842  * @dev: DRM device
1843  *
1844  * Called by a driver the first time it's needed, must be attached to desired
1845  * connectors.
1846  *
1847  * Returns:
1848  * Zero on success, negative errno on failure.
1849  */
1850 int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1851 {
1852 	if (dev->mode_config.aspect_ratio_property)
1853 		return 0;
1854 
1855 	dev->mode_config.aspect_ratio_property =
1856 		drm_property_create_enum(dev, 0, "aspect ratio",
1857 				drm_aspect_ratio_enum_list,
1858 				ARRAY_SIZE(drm_aspect_ratio_enum_list));
1859 
1860 	if (dev->mode_config.aspect_ratio_property == NULL)
1861 		return -ENOMEM;
1862 
1863 	return 0;
1864 }
1865 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1866 
1867 /**
1868  * DOC: standard connector properties
1869  *
1870  * Colorspace:
1871  *     This property helps select a suitable colorspace based on the sink
1872  *     capability. Modern sink devices support wider gamut like BT2020.
1873  *     This helps switch to BT2020 mode if the BT2020 encoded video stream
1874  *     is being played by the user, same for any other colorspace. Thereby
1875  *     giving a good visual experience to users.
1876  *
1877  *     The expectation from userspace is that it should parse the EDID
1878  *     and get supported colorspaces. Use this property and switch to the
1879  *     one supported. Sink supported colorspaces should be retrieved by
1880  *     userspace from EDID and driver will not explicitly expose them.
1881  *
1882  *     Basically the expectation from userspace is:
1883  *      - Set up CRTC DEGAMMA/CTM/GAMMA to convert to some sink
1884  *        colorspace
1885  *      - Set this new property to let the sink know what it
1886  *        converted the CRTC output to.
1887  *      - This property is just to inform sink what colorspace
1888  *        source is trying to drive.
1889  *
1890  * Because between HDMI and DP have different colorspaces,
1891  * drm_mode_create_hdmi_colorspace_property() is used for HDMI connector and
1892  * drm_mode_create_dp_colorspace_property() is used for DP connector.
1893  */
1894 
1895 /**
1896  * drm_mode_create_hdmi_colorspace_property - create hdmi colorspace property
1897  * @connector: connector to create the Colorspace property on.
1898  *
1899  * Called by a driver the first time it's needed, must be attached to desired
1900  * HDMI connectors.
1901  *
1902  * Returns:
1903  * Zero on success, negative errno on failure.
1904  */
1905 int drm_mode_create_hdmi_colorspace_property(struct drm_connector *connector)
1906 {
1907 	struct drm_device *dev = connector->dev;
1908 
1909 	if (connector->colorspace_property)
1910 		return 0;
1911 
1912 	connector->colorspace_property =
1913 		drm_property_create_enum(dev, DRM_MODE_PROP_ENUM, "Colorspace",
1914 					 hdmi_colorspaces,
1915 					 ARRAY_SIZE(hdmi_colorspaces));
1916 
1917 	if (!connector->colorspace_property)
1918 		return -ENOMEM;
1919 
1920 	return 0;
1921 }
1922 EXPORT_SYMBOL(drm_mode_create_hdmi_colorspace_property);
1923 
1924 /**
1925  * drm_mode_create_dp_colorspace_property - create dp colorspace property
1926  * @connector: connector to create the Colorspace property on.
1927  *
1928  * Called by a driver the first time it's needed, must be attached to desired
1929  * DP connectors.
1930  *
1931  * Returns:
1932  * Zero on success, negative errno on failure.
1933  */
1934 int drm_mode_create_dp_colorspace_property(struct drm_connector *connector)
1935 {
1936 	struct drm_device *dev = connector->dev;
1937 
1938 	if (connector->colorspace_property)
1939 		return 0;
1940 
1941 	connector->colorspace_property =
1942 		drm_property_create_enum(dev, DRM_MODE_PROP_ENUM, "Colorspace",
1943 					 dp_colorspaces,
1944 					 ARRAY_SIZE(dp_colorspaces));
1945 
1946 	if (!connector->colorspace_property)
1947 		return -ENOMEM;
1948 
1949 	return 0;
1950 }
1951 EXPORT_SYMBOL(drm_mode_create_dp_colorspace_property);
1952 
1953 /**
1954  * drm_mode_create_content_type_property - create content type property
1955  * @dev: DRM device
1956  *
1957  * Called by a driver the first time it's needed, must be attached to desired
1958  * connectors.
1959  *
1960  * Returns:
1961  * Zero on success, negative errno on failure.
1962  */
1963 int drm_mode_create_content_type_property(struct drm_device *dev)
1964 {
1965 	if (dev->mode_config.content_type_property)
1966 		return 0;
1967 
1968 	dev->mode_config.content_type_property =
1969 		drm_property_create_enum(dev, 0, "content type",
1970 					 drm_content_type_enum_list,
1971 					 ARRAY_SIZE(drm_content_type_enum_list));
1972 
1973 	if (dev->mode_config.content_type_property == NULL)
1974 		return -ENOMEM;
1975 
1976 	return 0;
1977 }
1978 EXPORT_SYMBOL(drm_mode_create_content_type_property);
1979 
1980 /**
1981  * drm_mode_create_suggested_offset_properties - create suggests offset properties
1982  * @dev: DRM device
1983  *
1984  * Create the suggested x/y offset property for connectors.
1985  *
1986  * Returns:
1987  * 0 on success or a negative error code on failure.
1988  */
1989 int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1990 {
1991 	if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1992 		return 0;
1993 
1994 	dev->mode_config.suggested_x_property =
1995 		drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1996 
1997 	dev->mode_config.suggested_y_property =
1998 		drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1999 
2000 	if (dev->mode_config.suggested_x_property == NULL ||
2001 	    dev->mode_config.suggested_y_property == NULL)
2002 		return -ENOMEM;
2003 	return 0;
2004 }
2005 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
2006 
2007 /**
2008  * drm_connector_set_path_property - set tile property on connector
2009  * @connector: connector to set property on.
2010  * @path: path to use for property; must not be NULL.
2011  *
2012  * This creates a property to expose to userspace to specify a
2013  * connector path. This is mainly used for DisplayPort MST where
2014  * connectors have a topology and we want to allow userspace to give
2015  * them more meaningful names.
2016  *
2017  * Returns:
2018  * Zero on success, negative errno on failure.
2019  */
2020 int drm_connector_set_path_property(struct drm_connector *connector,
2021 				    const char *path)
2022 {
2023 	struct drm_device *dev = connector->dev;
2024 	int ret;
2025 
2026 	ret = drm_property_replace_global_blob(dev,
2027 					       &connector->path_blob_ptr,
2028 					       strlen(path) + 1,
2029 					       path,
2030 					       &connector->base,
2031 					       dev->mode_config.path_property);
2032 	return ret;
2033 }
2034 EXPORT_SYMBOL(drm_connector_set_path_property);
2035 
2036 /**
2037  * drm_connector_set_tile_property - set tile property on connector
2038  * @connector: connector to set property on.
2039  *
2040  * This looks up the tile information for a connector, and creates a
2041  * property for userspace to parse if it exists. The property is of
2042  * the form of 8 integers using ':' as a separator.
2043  * This is used for dual port tiled displays with DisplayPort SST
2044  * or DisplayPort MST connectors.
2045  *
2046  * Returns:
2047  * Zero on success, errno on failure.
2048  */
2049 int drm_connector_set_tile_property(struct drm_connector *connector)
2050 {
2051 	struct drm_device *dev = connector->dev;
2052 	char tile[256];
2053 	int ret;
2054 
2055 	if (!connector->has_tile) {
2056 		ret  = drm_property_replace_global_blob(dev,
2057 							&connector->tile_blob_ptr,
2058 							0,
2059 							NULL,
2060 							&connector->base,
2061 							dev->mode_config.tile_property);
2062 		return ret;
2063 	}
2064 
2065 	snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
2066 		 connector->tile_group->id, connector->tile_is_single_monitor,
2067 		 connector->num_h_tile, connector->num_v_tile,
2068 		 connector->tile_h_loc, connector->tile_v_loc,
2069 		 connector->tile_h_size, connector->tile_v_size);
2070 
2071 	ret = drm_property_replace_global_blob(dev,
2072 					       &connector->tile_blob_ptr,
2073 					       strlen(tile) + 1,
2074 					       tile,
2075 					       &connector->base,
2076 					       dev->mode_config.tile_property);
2077 	return ret;
2078 }
2079 EXPORT_SYMBOL(drm_connector_set_tile_property);
2080 
2081 /**
2082  * drm_connector_set_link_status_property - Set link status property of a connector
2083  * @connector: drm connector
2084  * @link_status: new value of link status property (0: Good, 1: Bad)
2085  *
2086  * In usual working scenario, this link status property will always be set to
2087  * "GOOD". If something fails during or after a mode set, the kernel driver
2088  * may set this link status property to "BAD". The caller then needs to send a
2089  * hotplug uevent for userspace to re-check the valid modes through
2090  * GET_CONNECTOR_IOCTL and retry modeset.
2091  *
2092  * Note: Drivers cannot rely on userspace to support this property and
2093  * issue a modeset. As such, they may choose to handle issues (like
2094  * re-training a link) without userspace's intervention.
2095  *
2096  * The reason for adding this property is to handle link training failures, but
2097  * it is not limited to DP or link training. For example, if we implement
2098  * asynchronous setcrtc, this property can be used to report any failures in that.
2099  */
2100 void drm_connector_set_link_status_property(struct drm_connector *connector,
2101 					    uint64_t link_status)
2102 {
2103 	struct drm_device *dev = connector->dev;
2104 
2105 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2106 	connector->state->link_status = link_status;
2107 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
2108 }
2109 EXPORT_SYMBOL(drm_connector_set_link_status_property);
2110 
2111 /**
2112  * drm_connector_attach_max_bpc_property - attach "max bpc" property
2113  * @connector: connector to attach max bpc property on.
2114  * @min: The minimum bit depth supported by the connector.
2115  * @max: The maximum bit depth supported by the connector.
2116  *
2117  * This is used to add support for limiting the bit depth on a connector.
2118  *
2119  * Returns:
2120  * Zero on success, negative errno on failure.
2121  */
2122 int drm_connector_attach_max_bpc_property(struct drm_connector *connector,
2123 					  int min, int max)
2124 {
2125 	struct drm_device *dev = connector->dev;
2126 	struct drm_property *prop;
2127 
2128 	prop = connector->max_bpc_property;
2129 	if (!prop) {
2130 		prop = drm_property_create_range(dev, 0, "max bpc", min, max);
2131 		if (!prop)
2132 			return -ENOMEM;
2133 
2134 		connector->max_bpc_property = prop;
2135 	}
2136 
2137 	drm_object_attach_property(&connector->base, prop, max);
2138 	connector->state->max_requested_bpc = max;
2139 	connector->state->max_bpc = max;
2140 
2141 	return 0;
2142 }
2143 EXPORT_SYMBOL(drm_connector_attach_max_bpc_property);
2144 
2145 /**
2146  * drm_connector_attach_hdr_output_metadata_property - attach "HDR_OUTPUT_METADA" property
2147  * @connector: connector to attach the property on.
2148  *
2149  * This is used to allow the userspace to send HDR Metadata to the
2150  * driver.
2151  *
2152  * Returns:
2153  * Zero on success, negative errno on failure.
2154  */
2155 int drm_connector_attach_hdr_output_metadata_property(struct drm_connector *connector)
2156 {
2157 	struct drm_device *dev = connector->dev;
2158 	struct drm_property *prop = dev->mode_config.hdr_output_metadata_property;
2159 
2160 	drm_object_attach_property(&connector->base, prop, 0);
2161 
2162 	return 0;
2163 }
2164 EXPORT_SYMBOL(drm_connector_attach_hdr_output_metadata_property);
2165 
2166 /**
2167  * drm_connector_attach_colorspace_property - attach "Colorspace" property
2168  * @connector: connector to attach the property on.
2169  *
2170  * This is used to allow the userspace to signal the output colorspace
2171  * to the driver.
2172  *
2173  * Returns:
2174  * Zero on success, negative errno on failure.
2175  */
2176 int drm_connector_attach_colorspace_property(struct drm_connector *connector)
2177 {
2178 	struct drm_property *prop = connector->colorspace_property;
2179 
2180 	drm_object_attach_property(&connector->base, prop, DRM_MODE_COLORIMETRY_DEFAULT);
2181 
2182 	return 0;
2183 }
2184 EXPORT_SYMBOL(drm_connector_attach_colorspace_property);
2185 
2186 /**
2187  * drm_connector_atomic_hdr_metadata_equal - checks if the hdr metadata changed
2188  * @old_state: old connector state to compare
2189  * @new_state: new connector state to compare
2190  *
2191  * This is used by HDR-enabled drivers to test whether the HDR metadata
2192  * have changed between two different connector state (and thus probably
2193  * requires a full blown mode change).
2194  *
2195  * Returns:
2196  * True if the metadata are equal, False otherwise
2197  */
2198 bool drm_connector_atomic_hdr_metadata_equal(struct drm_connector_state *old_state,
2199 					     struct drm_connector_state *new_state)
2200 {
2201 	struct drm_property_blob *old_blob = old_state->hdr_output_metadata;
2202 	struct drm_property_blob *new_blob = new_state->hdr_output_metadata;
2203 
2204 	if (!old_blob || !new_blob)
2205 		return old_blob == new_blob;
2206 
2207 	if (old_blob->length != new_blob->length)
2208 		return false;
2209 
2210 	return !memcmp(old_blob->data, new_blob->data, old_blob->length);
2211 }
2212 EXPORT_SYMBOL(drm_connector_atomic_hdr_metadata_equal);
2213 
2214 /**
2215  * drm_connector_set_vrr_capable_property - sets the variable refresh rate
2216  * capable property for a connector
2217  * @connector: drm connector
2218  * @capable: True if the connector is variable refresh rate capable
2219  *
2220  * Should be used by atomic drivers to update the indicated support for
2221  * variable refresh rate over a connector.
2222  */
2223 void drm_connector_set_vrr_capable_property(
2224 		struct drm_connector *connector, bool capable)
2225 {
2226 	if (!connector->vrr_capable_property)
2227 		return;
2228 
2229 	drm_object_property_set_value(&connector->base,
2230 				      connector->vrr_capable_property,
2231 				      capable);
2232 }
2233 EXPORT_SYMBOL(drm_connector_set_vrr_capable_property);
2234 
2235 /**
2236  * drm_connector_set_panel_orientation - sets the connector's panel_orientation
2237  * @connector: connector for which to set the panel-orientation property.
2238  * @panel_orientation: drm_panel_orientation value to set
2239  *
2240  * This function sets the connector's panel_orientation and attaches
2241  * a "panel orientation" property to the connector.
2242  *
2243  * Calling this function on a connector where the panel_orientation has
2244  * already been set is a no-op (e.g. the orientation has been overridden with
2245  * a kernel commandline option).
2246  *
2247  * It is allowed to call this function with a panel_orientation of
2248  * DRM_MODE_PANEL_ORIENTATION_UNKNOWN, in which case it is a no-op.
2249  *
2250  * The function shouldn't be called in panel after drm is registered (i.e.
2251  * drm_dev_register() is called in drm).
2252  *
2253  * Returns:
2254  * Zero on success, negative errno on failure.
2255  */
2256 int drm_connector_set_panel_orientation(
2257 	struct drm_connector *connector,
2258 	enum drm_panel_orientation panel_orientation)
2259 {
2260 	struct drm_device *dev = connector->dev;
2261 	struct drm_display_info *info = &connector->display_info;
2262 	struct drm_property *prop;
2263 
2264 	/* Already set? */
2265 	if (info->panel_orientation != DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
2266 		return 0;
2267 
2268 	/* Don't attach the property if the orientation is unknown */
2269 	if (panel_orientation == DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
2270 		return 0;
2271 
2272 	info->panel_orientation = panel_orientation;
2273 
2274 	prop = dev->mode_config.panel_orientation_property;
2275 	if (!prop) {
2276 		prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
2277 				"panel orientation",
2278 				drm_panel_orientation_enum_list,
2279 				ARRAY_SIZE(drm_panel_orientation_enum_list));
2280 		if (!prop)
2281 			return -ENOMEM;
2282 
2283 		dev->mode_config.panel_orientation_property = prop;
2284 	}
2285 
2286 	drm_object_attach_property(&connector->base, prop,
2287 				   info->panel_orientation);
2288 	return 0;
2289 }
2290 EXPORT_SYMBOL(drm_connector_set_panel_orientation);
2291 
2292 /**
2293  * drm_connector_set_panel_orientation_with_quirk - set the
2294  *	connector's panel_orientation after checking for quirks
2295  * @connector: connector for which to init the panel-orientation property.
2296  * @panel_orientation: drm_panel_orientation value to set
2297  * @width: width in pixels of the panel, used for panel quirk detection
2298  * @height: height in pixels of the panel, used for panel quirk detection
2299  *
2300  * Like drm_connector_set_panel_orientation(), but with a check for platform
2301  * specific (e.g. DMI based) quirks overriding the passed in panel_orientation.
2302  *
2303  * Returns:
2304  * Zero on success, negative errno on failure.
2305  */
2306 int drm_connector_set_panel_orientation_with_quirk(
2307 	struct drm_connector *connector,
2308 	enum drm_panel_orientation panel_orientation,
2309 	int width, int height)
2310 {
2311 	int orientation_quirk;
2312 
2313 	orientation_quirk = drm_get_panel_orientation_quirk(width, height);
2314 	if (orientation_quirk != DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
2315 		panel_orientation = orientation_quirk;
2316 
2317 	return drm_connector_set_panel_orientation(connector,
2318 						   panel_orientation);
2319 }
2320 EXPORT_SYMBOL(drm_connector_set_panel_orientation_with_quirk);
2321 
2322 /**
2323  * drm_connector_set_orientation_from_panel -
2324  *	set the connector's panel_orientation from panel's callback.
2325  * @connector: connector for which to init the panel-orientation property.
2326  * @panel: panel that can provide orientation information.
2327  *
2328  * Drm drivers should call this function before drm_dev_register().
2329  * Orientation is obtained from panel's .get_orientation() callback.
2330  *
2331  * Returns:
2332  * Zero on success, negative errno on failure.
2333  */
2334 int drm_connector_set_orientation_from_panel(
2335 	struct drm_connector *connector,
2336 	struct drm_panel *panel)
2337 {
2338 	enum drm_panel_orientation orientation;
2339 
2340 	if (panel && panel->funcs && panel->funcs->get_orientation)
2341 		orientation = panel->funcs->get_orientation(panel);
2342 	else
2343 		orientation = DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
2344 
2345 	return drm_connector_set_panel_orientation(connector, orientation);
2346 }
2347 EXPORT_SYMBOL(drm_connector_set_orientation_from_panel);
2348 
2349 static const struct drm_prop_enum_list privacy_screen_enum[] = {
2350 	{ PRIVACY_SCREEN_DISABLED,		"Disabled" },
2351 	{ PRIVACY_SCREEN_ENABLED,		"Enabled" },
2352 	{ PRIVACY_SCREEN_DISABLED_LOCKED,	"Disabled-locked" },
2353 	{ PRIVACY_SCREEN_ENABLED_LOCKED,	"Enabled-locked" },
2354 };
2355 
2356 /**
2357  * drm_connector_create_privacy_screen_properties - create the drm connecter's
2358  *    privacy-screen properties.
2359  * @connector: connector for which to create the privacy-screen properties
2360  *
2361  * This function creates the "privacy-screen sw-state" and "privacy-screen
2362  * hw-state" properties for the connector. They are not attached.
2363  */
2364 void
2365 drm_connector_create_privacy_screen_properties(struct drm_connector *connector)
2366 {
2367 	if (connector->privacy_screen_sw_state_property)
2368 		return;
2369 
2370 	/* Note sw-state only supports the first 2 values of the enum */
2371 	connector->privacy_screen_sw_state_property =
2372 		drm_property_create_enum(connector->dev, DRM_MODE_PROP_ENUM,
2373 				"privacy-screen sw-state",
2374 				privacy_screen_enum, 2);
2375 
2376 	connector->privacy_screen_hw_state_property =
2377 		drm_property_create_enum(connector->dev,
2378 				DRM_MODE_PROP_IMMUTABLE | DRM_MODE_PROP_ENUM,
2379 				"privacy-screen hw-state",
2380 				privacy_screen_enum,
2381 				ARRAY_SIZE(privacy_screen_enum));
2382 }
2383 EXPORT_SYMBOL(drm_connector_create_privacy_screen_properties);
2384 
2385 /**
2386  * drm_connector_attach_privacy_screen_properties - attach the drm connecter's
2387  *    privacy-screen properties.
2388  * @connector: connector on which to attach the privacy-screen properties
2389  *
2390  * This function attaches the "privacy-screen sw-state" and "privacy-screen
2391  * hw-state" properties to the connector. The initial state of both is set
2392  * to "Disabled".
2393  */
2394 void
2395 drm_connector_attach_privacy_screen_properties(struct drm_connector *connector)
2396 {
2397 	if (!connector->privacy_screen_sw_state_property)
2398 		return;
2399 
2400 	drm_object_attach_property(&connector->base,
2401 				   connector->privacy_screen_sw_state_property,
2402 				   PRIVACY_SCREEN_DISABLED);
2403 
2404 	drm_object_attach_property(&connector->base,
2405 				   connector->privacy_screen_hw_state_property,
2406 				   PRIVACY_SCREEN_DISABLED);
2407 }
2408 EXPORT_SYMBOL(drm_connector_attach_privacy_screen_properties);
2409 
2410 static void drm_connector_update_privacy_screen_properties(
2411 	struct drm_connector *connector, bool set_sw_state)
2412 {
2413 	enum drm_privacy_screen_status sw_state, hw_state;
2414 
2415 	drm_privacy_screen_get_state(connector->privacy_screen,
2416 				     &sw_state, &hw_state);
2417 
2418 	if (set_sw_state)
2419 		connector->state->privacy_screen_sw_state = sw_state;
2420 	drm_object_property_set_value(&connector->base,
2421 			connector->privacy_screen_hw_state_property, hw_state);
2422 }
2423 
2424 static int drm_connector_privacy_screen_notifier(
2425 	struct notifier_block *nb, unsigned long action, void *data)
2426 {
2427 	struct drm_connector *connector =
2428 		container_of(nb, struct drm_connector, privacy_screen_notifier);
2429 	struct drm_device *dev = connector->dev;
2430 
2431 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2432 	drm_connector_update_privacy_screen_properties(connector, true);
2433 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
2434 
2435 	drm_sysfs_connector_status_event(connector,
2436 				connector->privacy_screen_sw_state_property);
2437 	drm_sysfs_connector_status_event(connector,
2438 				connector->privacy_screen_hw_state_property);
2439 
2440 	return NOTIFY_DONE;
2441 }
2442 
2443 /**
2444  * drm_connector_attach_privacy_screen_provider - attach a privacy-screen to
2445  *    the connector
2446  * @connector: connector to attach the privacy-screen to
2447  * @priv: drm_privacy_screen to attach
2448  *
2449  * Create and attach the standard privacy-screen properties and register
2450  * a generic notifier for generating sysfs-connector-status-events
2451  * on external changes to the privacy-screen status.
2452  * This function takes ownership of the passed in drm_privacy_screen and will
2453  * call drm_privacy_screen_put() on it when the connector is destroyed.
2454  */
2455 void drm_connector_attach_privacy_screen_provider(
2456 	struct drm_connector *connector, struct drm_privacy_screen *priv)
2457 {
2458 	connector->privacy_screen = priv;
2459 	connector->privacy_screen_notifier.notifier_call =
2460 		drm_connector_privacy_screen_notifier;
2461 
2462 	drm_connector_create_privacy_screen_properties(connector);
2463 	drm_connector_update_privacy_screen_properties(connector, true);
2464 	drm_connector_attach_privacy_screen_properties(connector);
2465 }
2466 EXPORT_SYMBOL(drm_connector_attach_privacy_screen_provider);
2467 
2468 /**
2469  * drm_connector_update_privacy_screen - update connector's privacy-screen sw-state
2470  * @connector_state: connector-state to update the privacy-screen for
2471  *
2472  * This function calls drm_privacy_screen_set_sw_state() on the connector's
2473  * privacy-screen.
2474  *
2475  * If the connector has no privacy-screen, then this is a no-op.
2476  */
2477 void drm_connector_update_privacy_screen(const struct drm_connector_state *connector_state)
2478 {
2479 	struct drm_connector *connector = connector_state->connector;
2480 	int ret;
2481 
2482 	if (!connector->privacy_screen)
2483 		return;
2484 
2485 	ret = drm_privacy_screen_set_sw_state(connector->privacy_screen,
2486 					      connector_state->privacy_screen_sw_state);
2487 	if (ret) {
2488 		drm_err(connector->dev, "Error updating privacy-screen sw_state\n");
2489 		return;
2490 	}
2491 
2492 	/* The hw_state property value may have changed, update it. */
2493 	drm_connector_update_privacy_screen_properties(connector, false);
2494 }
2495 EXPORT_SYMBOL(drm_connector_update_privacy_screen);
2496 
2497 int drm_connector_set_obj_prop(struct drm_mode_object *obj,
2498 				    struct drm_property *property,
2499 				    uint64_t value)
2500 {
2501 	int ret = -EINVAL;
2502 	struct drm_connector *connector = obj_to_connector(obj);
2503 
2504 	/* Do DPMS ourselves */
2505 	if (property == connector->dev->mode_config.dpms_property) {
2506 		ret = (*connector->funcs->dpms)(connector, (int)value);
2507 	} else if (connector->funcs->set_property)
2508 		ret = connector->funcs->set_property(connector, property, value);
2509 
2510 	if (!ret)
2511 		drm_object_property_set_value(&connector->base, property, value);
2512 	return ret;
2513 }
2514 
2515 int drm_connector_property_set_ioctl(struct drm_device *dev,
2516 				     void *data, struct drm_file *file_priv)
2517 {
2518 	struct drm_mode_connector_set_property *conn_set_prop = data;
2519 	struct drm_mode_obj_set_property obj_set_prop = {
2520 		.value = conn_set_prop->value,
2521 		.prop_id = conn_set_prop->prop_id,
2522 		.obj_id = conn_set_prop->connector_id,
2523 		.obj_type = DRM_MODE_OBJECT_CONNECTOR
2524 	};
2525 
2526 	/* It does all the locking and checking we need */
2527 	return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
2528 }
2529 
2530 static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
2531 {
2532 	/* For atomic drivers only state objects are synchronously updated and
2533 	 * protected by modeset locks, so check those first.
2534 	 */
2535 	if (connector->state)
2536 		return connector->state->best_encoder;
2537 	return connector->encoder;
2538 }
2539 
2540 static bool
2541 drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
2542 			     const struct list_head *modes,
2543 			     const struct drm_file *file_priv)
2544 {
2545 	/*
2546 	 * If user-space hasn't configured the driver to expose the stereo 3D
2547 	 * modes, don't expose them.
2548 	 */
2549 	if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
2550 		return false;
2551 	/*
2552 	 * If user-space hasn't configured the driver to expose the modes
2553 	 * with aspect-ratio, don't expose them. However if such a mode
2554 	 * is unique, let it be exposed, but reset the aspect-ratio flags
2555 	 * while preparing the list of user-modes.
2556 	 */
2557 	if (!file_priv->aspect_ratio_allowed) {
2558 		const struct drm_display_mode *mode_itr;
2559 
2560 		list_for_each_entry(mode_itr, modes, head) {
2561 			if (mode_itr->expose_to_userspace &&
2562 			    drm_mode_match(mode_itr, mode,
2563 					   DRM_MODE_MATCH_TIMINGS |
2564 					   DRM_MODE_MATCH_CLOCK |
2565 					   DRM_MODE_MATCH_FLAGS |
2566 					   DRM_MODE_MATCH_3D_FLAGS))
2567 				return false;
2568 		}
2569 	}
2570 
2571 	return true;
2572 }
2573 
2574 int drm_mode_getconnector(struct drm_device *dev, void *data,
2575 			  struct drm_file *file_priv)
2576 {
2577 	struct drm_mode_get_connector *out_resp = data;
2578 	struct drm_connector *connector;
2579 	struct drm_encoder *encoder;
2580 	struct drm_display_mode *mode;
2581 	int mode_count = 0;
2582 	int encoders_count = 0;
2583 	int ret = 0;
2584 	int copied = 0;
2585 	struct drm_mode_modeinfo u_mode;
2586 	struct drm_mode_modeinfo __user *mode_ptr;
2587 	uint32_t __user *encoder_ptr;
2588 	bool is_current_master;
2589 
2590 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
2591 		return -EOPNOTSUPP;
2592 
2593 	memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2594 
2595 	connector = drm_connector_lookup(dev, file_priv, out_resp->connector_id);
2596 	if (!connector)
2597 		return -ENOENT;
2598 
2599 	encoders_count = hweight32(connector->possible_encoders);
2600 
2601 	if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2602 		copied = 0;
2603 		encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
2604 
2605 		drm_connector_for_each_possible_encoder(connector, encoder) {
2606 			if (put_user(encoder->base.id, encoder_ptr + copied)) {
2607 				ret = -EFAULT;
2608 				goto out;
2609 			}
2610 			copied++;
2611 		}
2612 	}
2613 	out_resp->count_encoders = encoders_count;
2614 
2615 	out_resp->connector_id = connector->base.id;
2616 	out_resp->connector_type = connector->connector_type;
2617 	out_resp->connector_type_id = connector->connector_type_id;
2618 
2619 	is_current_master = drm_is_current_master(file_priv);
2620 
2621 	mutex_lock(&dev->mode_config.mutex);
2622 	if (out_resp->count_modes == 0) {
2623 		if (is_current_master)
2624 			connector->funcs->fill_modes(connector,
2625 						     dev->mode_config.max_width,
2626 						     dev->mode_config.max_height);
2627 		else
2628 			drm_dbg_kms(dev, "User-space requested a forced probe on [CONNECTOR:%d:%s] but is not the DRM master, demoting to read-only probe",
2629 				    connector->base.id, connector->name);
2630 	}
2631 
2632 	out_resp->mm_width = connector->display_info.width_mm;
2633 	out_resp->mm_height = connector->display_info.height_mm;
2634 	out_resp->subpixel = connector->display_info.subpixel_order;
2635 	out_resp->connection = connector->status;
2636 
2637 	/* delayed so we get modes regardless of pre-fill_modes state */
2638 	list_for_each_entry(mode, &connector->modes, head) {
2639 		WARN_ON(mode->expose_to_userspace);
2640 
2641 		if (drm_mode_expose_to_userspace(mode, &connector->modes,
2642 						 file_priv)) {
2643 			mode->expose_to_userspace = true;
2644 			mode_count++;
2645 		}
2646 	}
2647 
2648 	/*
2649 	 * This ioctl is called twice, once to determine how much space is
2650 	 * needed, and the 2nd time to fill it.
2651 	 */
2652 	if ((out_resp->count_modes >= mode_count) && mode_count) {
2653 		copied = 0;
2654 		mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
2655 		list_for_each_entry(mode, &connector->modes, head) {
2656 			if (!mode->expose_to_userspace)
2657 				continue;
2658 
2659 			/* Clear the tag for the next time around */
2660 			mode->expose_to_userspace = false;
2661 
2662 			drm_mode_convert_to_umode(&u_mode, mode);
2663 			/*
2664 			 * Reset aspect ratio flags of user-mode, if modes with
2665 			 * aspect-ratio are not supported.
2666 			 */
2667 			if (!file_priv->aspect_ratio_allowed)
2668 				u_mode.flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
2669 			if (copy_to_user(mode_ptr + copied,
2670 					 &u_mode, sizeof(u_mode))) {
2671 				ret = -EFAULT;
2672 
2673 				/*
2674 				 * Clear the tag for the rest of
2675 				 * the modes for the next time around.
2676 				 */
2677 				list_for_each_entry_continue(mode, &connector->modes, head)
2678 					mode->expose_to_userspace = false;
2679 
2680 				mutex_unlock(&dev->mode_config.mutex);
2681 
2682 				goto out;
2683 			}
2684 			copied++;
2685 		}
2686 	} else {
2687 		/* Clear the tag for the next time around */
2688 		list_for_each_entry(mode, &connector->modes, head)
2689 			mode->expose_to_userspace = false;
2690 	}
2691 
2692 	out_resp->count_modes = mode_count;
2693 	mutex_unlock(&dev->mode_config.mutex);
2694 
2695 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2696 	encoder = drm_connector_get_encoder(connector);
2697 	if (encoder)
2698 		out_resp->encoder_id = encoder->base.id;
2699 	else
2700 		out_resp->encoder_id = 0;
2701 
2702 	/* Only grab properties after probing, to make sure EDID and other
2703 	 * properties reflect the latest status.
2704 	 */
2705 	ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
2706 			(uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2707 			(uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2708 			&out_resp->count_props);
2709 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
2710 
2711 out:
2712 	drm_connector_put(connector);
2713 
2714 	return ret;
2715 }
2716 
2717 /**
2718  * drm_connector_find_by_fwnode - Find a connector based on the associated fwnode
2719  * @fwnode: fwnode for which to find the matching drm_connector
2720  *
2721  * This functions looks up a drm_connector based on its associated fwnode. When
2722  * a connector is found a reference to the connector is returned. The caller must
2723  * call drm_connector_put() to release this reference when it is done with the
2724  * connector.
2725  *
2726  * Returns: A reference to the found connector or an ERR_PTR().
2727  */
2728 struct drm_connector *drm_connector_find_by_fwnode(struct fwnode_handle *fwnode)
2729 {
2730 	struct drm_connector *connector, *found = ERR_PTR(-ENODEV);
2731 
2732 	if (!fwnode)
2733 		return ERR_PTR(-ENODEV);
2734 
2735 	mutex_lock(&connector_list_lock);
2736 
2737 	list_for_each_entry(connector, &connector_list, global_connector_list_entry) {
2738 		if (connector->fwnode == fwnode ||
2739 		    (connector->fwnode && connector->fwnode->secondary == fwnode)) {
2740 			drm_connector_get(connector);
2741 			found = connector;
2742 			break;
2743 		}
2744 	}
2745 
2746 	mutex_unlock(&connector_list_lock);
2747 
2748 	return found;
2749 }
2750 
2751 /**
2752  * drm_connector_oob_hotplug_event - Report out-of-band hotplug event to connector
2753  * @connector_fwnode: fwnode_handle to report the event on
2754  *
2755  * On some hardware a hotplug event notification may come from outside the display
2756  * driver / device. An example of this is some USB Type-C setups where the hardware
2757  * muxes the DisplayPort data and aux-lines but does not pass the altmode HPD
2758  * status bit to the GPU's DP HPD pin.
2759  *
2760  * This function can be used to report these out-of-band events after obtaining
2761  * a drm_connector reference through calling drm_connector_find_by_fwnode().
2762  */
2763 void drm_connector_oob_hotplug_event(struct fwnode_handle *connector_fwnode)
2764 {
2765 	struct drm_connector *connector;
2766 
2767 	connector = drm_connector_find_by_fwnode(connector_fwnode);
2768 	if (IS_ERR(connector))
2769 		return;
2770 
2771 	if (connector->funcs->oob_hotplug_event)
2772 		connector->funcs->oob_hotplug_event(connector);
2773 
2774 	drm_connector_put(connector);
2775 }
2776 EXPORT_SYMBOL(drm_connector_oob_hotplug_event);
2777 
2778 
2779 /**
2780  * DOC: Tile group
2781  *
2782  * Tile groups are used to represent tiled monitors with a unique integer
2783  * identifier. Tiled monitors using DisplayID v1.3 have a unique 8-byte handle,
2784  * we store this in a tile group, so we have a common identifier for all tiles
2785  * in a monitor group. The property is called "TILE". Drivers can manage tile
2786  * groups using drm_mode_create_tile_group(), drm_mode_put_tile_group() and
2787  * drm_mode_get_tile_group(). But this is only needed for internal panels where
2788  * the tile group information is exposed through a non-standard way.
2789  */
2790 
2791 static void drm_tile_group_free(struct kref *kref)
2792 {
2793 	struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
2794 	struct drm_device *dev = tg->dev;
2795 
2796 	mutex_lock(&dev->mode_config.idr_mutex);
2797 	idr_remove(&dev->mode_config.tile_idr, tg->id);
2798 	mutex_unlock(&dev->mode_config.idr_mutex);
2799 	kfree(tg);
2800 }
2801 
2802 /**
2803  * drm_mode_put_tile_group - drop a reference to a tile group.
2804  * @dev: DRM device
2805  * @tg: tile group to drop reference to.
2806  *
2807  * drop reference to tile group and free if 0.
2808  */
2809 void drm_mode_put_tile_group(struct drm_device *dev,
2810 			     struct drm_tile_group *tg)
2811 {
2812 	kref_put(&tg->refcount, drm_tile_group_free);
2813 }
2814 EXPORT_SYMBOL(drm_mode_put_tile_group);
2815 
2816 /**
2817  * drm_mode_get_tile_group - get a reference to an existing tile group
2818  * @dev: DRM device
2819  * @topology: 8-bytes unique per monitor.
2820  *
2821  * Use the unique bytes to get a reference to an existing tile group.
2822  *
2823  * RETURNS:
2824  * tile group or NULL if not found.
2825  */
2826 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
2827 					       const char topology[8])
2828 {
2829 	struct drm_tile_group *tg;
2830 	int id;
2831 
2832 	mutex_lock(&dev->mode_config.idr_mutex);
2833 	idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
2834 		if (!memcmp(tg->group_data, topology, 8)) {
2835 			if (!kref_get_unless_zero(&tg->refcount))
2836 				tg = NULL;
2837 			mutex_unlock(&dev->mode_config.idr_mutex);
2838 			return tg;
2839 		}
2840 	}
2841 	mutex_unlock(&dev->mode_config.idr_mutex);
2842 	return NULL;
2843 }
2844 EXPORT_SYMBOL(drm_mode_get_tile_group);
2845 
2846 /**
2847  * drm_mode_create_tile_group - create a tile group from a displayid description
2848  * @dev: DRM device
2849  * @topology: 8-bytes unique per monitor.
2850  *
2851  * Create a tile group for the unique monitor, and get a unique
2852  * identifier for the tile group.
2853  *
2854  * RETURNS:
2855  * new tile group or NULL.
2856  */
2857 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
2858 						  const char topology[8])
2859 {
2860 	struct drm_tile_group *tg;
2861 	int ret;
2862 
2863 	tg = kzalloc(sizeof(*tg), GFP_KERNEL);
2864 	if (!tg)
2865 		return NULL;
2866 
2867 	kref_init(&tg->refcount);
2868 	memcpy(tg->group_data, topology, 8);
2869 	tg->dev = dev;
2870 
2871 	mutex_lock(&dev->mode_config.idr_mutex);
2872 	ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
2873 	if (ret >= 0) {
2874 		tg->id = ret;
2875 	} else {
2876 		kfree(tg);
2877 		tg = NULL;
2878 	}
2879 
2880 	mutex_unlock(&dev->mode_config.idr_mutex);
2881 	return tg;
2882 }
2883 EXPORT_SYMBOL(drm_mode_create_tile_group);
2884