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