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