xref: /openbmc/linux/drivers/gpu/drm/drm_connector.c (revision 2f828fb2)
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 
28 #include "drm_crtc_internal.h"
29 #include "drm_internal.h"
30 
31 /**
32  * DOC: overview
33  *
34  * In DRM connectors are the general abstraction for display sinks, and include
35  * als fixed panels or anything else that can display pixels in some form. As
36  * opposed to all other KMS objects representing hardware (like CRTC, encoder or
37  * plane abstractions) connectors can be hotplugged and unplugged at runtime.
38  * Hence they are reference-counted using drm_connector_get() and
39  * drm_connector_put().
40  *
41  * KMS driver must create, initialize, register and attach at a &struct
42  * drm_connector for each such sink. The instance is created as other KMS
43  * objects and initialized by setting the following fields. The connector is
44  * initialized with a call to drm_connector_init() with a pointer to the
45  * &struct drm_connector_funcs and a connector type, and then exposed to
46  * userspace with a call to drm_connector_register().
47  *
48  * Connectors must be attached to an encoder to be used. For devices that map
49  * connectors to encoders 1:1, the connector should be attached at
50  * initialization time with a call to drm_mode_connector_attach_encoder(). The
51  * driver must also set the &drm_connector.encoder field to point to the
52  * attached encoder.
53  *
54  * For connectors which are not fixed (like built-in panels) the driver needs to
55  * support hotplug notifications. The simplest way to do that is by using the
56  * probe helpers, see drm_kms_helper_poll_init() for connectors which don't have
57  * hardware support for hotplug interrupts. Connectors with hardware hotplug
58  * support can instead use e.g. drm_helper_hpd_irq_event().
59  */
60 
61 struct drm_conn_prop_enum_list {
62 	int type;
63 	const char *name;
64 	struct ida ida;
65 };
66 
67 /*
68  * Connector and encoder types.
69  */
70 static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
71 	{ DRM_MODE_CONNECTOR_Unknown, "Unknown" },
72 	{ DRM_MODE_CONNECTOR_VGA, "VGA" },
73 	{ DRM_MODE_CONNECTOR_DVII, "DVI-I" },
74 	{ DRM_MODE_CONNECTOR_DVID, "DVI-D" },
75 	{ DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
76 	{ DRM_MODE_CONNECTOR_Composite, "Composite" },
77 	{ DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
78 	{ DRM_MODE_CONNECTOR_LVDS, "LVDS" },
79 	{ DRM_MODE_CONNECTOR_Component, "Component" },
80 	{ DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
81 	{ DRM_MODE_CONNECTOR_DisplayPort, "DP" },
82 	{ DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
83 	{ DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
84 	{ DRM_MODE_CONNECTOR_TV, "TV" },
85 	{ DRM_MODE_CONNECTOR_eDP, "eDP" },
86 	{ DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
87 	{ DRM_MODE_CONNECTOR_DSI, "DSI" },
88 	{ DRM_MODE_CONNECTOR_DPI, "DPI" },
89 };
90 
91 void drm_connector_ida_init(void)
92 {
93 	int i;
94 
95 	for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
96 		ida_init(&drm_connector_enum_list[i].ida);
97 }
98 
99 void drm_connector_ida_destroy(void)
100 {
101 	int i;
102 
103 	for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
104 		ida_destroy(&drm_connector_enum_list[i].ida);
105 }
106 
107 /**
108  * drm_connector_get_cmdline_mode - reads the user's cmdline mode
109  * @connector: connector to quwery
110  *
111  * The kernel supports per-connector configuration of its consoles through
112  * use of the video= parameter. This function parses that option and
113  * extracts the user's specified mode (or enable/disable status) for a
114  * particular connector. This is typically only used during the early fbdev
115  * setup.
116  */
117 static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
118 {
119 	struct drm_cmdline_mode *mode = &connector->cmdline_mode;
120 	char *option = NULL;
121 
122 	if (fb_get_options(connector->name, &option))
123 		return;
124 
125 	if (!drm_mode_parse_command_line_for_connector(option,
126 						       connector,
127 						       mode))
128 		return;
129 
130 	if (mode->force) {
131 		DRM_INFO("forcing %s connector %s\n", connector->name,
132 			 drm_get_connector_force_name(mode->force));
133 		connector->force = mode->force;
134 	}
135 
136 	DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
137 		      connector->name,
138 		      mode->xres, mode->yres,
139 		      mode->refresh_specified ? mode->refresh : 60,
140 		      mode->rb ? " reduced blanking" : "",
141 		      mode->margins ? " with margins" : "",
142 		      mode->interlace ?  " interlaced" : "");
143 }
144 
145 static void drm_connector_free(struct kref *kref)
146 {
147 	struct drm_connector *connector =
148 		container_of(kref, struct drm_connector, base.refcount);
149 	struct drm_device *dev = connector->dev;
150 
151 	drm_mode_object_unregister(dev, &connector->base);
152 	connector->funcs->destroy(connector);
153 }
154 
155 /**
156  * drm_connector_init - Init a preallocated connector
157  * @dev: DRM device
158  * @connector: the connector to init
159  * @funcs: callbacks for this connector
160  * @connector_type: user visible type of the connector
161  *
162  * Initialises a preallocated connector. Connectors should be
163  * subclassed as part of driver connector objects.
164  *
165  * Returns:
166  * Zero on success, error code on failure.
167  */
168 int drm_connector_init(struct drm_device *dev,
169 		       struct drm_connector *connector,
170 		       const struct drm_connector_funcs *funcs,
171 		       int connector_type)
172 {
173 	struct drm_mode_config *config = &dev->mode_config;
174 	int ret;
175 	struct ida *connector_ida =
176 		&drm_connector_enum_list[connector_type].ida;
177 
178 	ret = __drm_mode_object_add(dev, &connector->base,
179 				    DRM_MODE_OBJECT_CONNECTOR,
180 				    false, drm_connector_free);
181 	if (ret)
182 		return ret;
183 
184 	connector->base.properties = &connector->properties;
185 	connector->dev = dev;
186 	connector->funcs = funcs;
187 
188 	ret = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
189 	if (ret < 0)
190 		goto out_put;
191 	connector->index = ret;
192 	ret = 0;
193 
194 	connector->connector_type = connector_type;
195 	connector->connector_type_id =
196 		ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
197 	if (connector->connector_type_id < 0) {
198 		ret = connector->connector_type_id;
199 		goto out_put_id;
200 	}
201 	connector->name =
202 		kasprintf(GFP_KERNEL, "%s-%d",
203 			  drm_connector_enum_list[connector_type].name,
204 			  connector->connector_type_id);
205 	if (!connector->name) {
206 		ret = -ENOMEM;
207 		goto out_put_type_id;
208 	}
209 
210 	INIT_LIST_HEAD(&connector->probed_modes);
211 	INIT_LIST_HEAD(&connector->modes);
212 	mutex_init(&connector->mutex);
213 	connector->edid_blob_ptr = NULL;
214 	connector->status = connector_status_unknown;
215 
216 	drm_connector_get_cmdline_mode(connector);
217 
218 	/* We should add connectors at the end to avoid upsetting the connector
219 	 * index too much. */
220 	spin_lock_irq(&config->connector_list_lock);
221 	list_add_tail(&connector->head, &config->connector_list);
222 	config->num_connector++;
223 	spin_unlock_irq(&config->connector_list_lock);
224 
225 	if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
226 		drm_object_attach_property(&connector->base,
227 					      config->edid_property,
228 					      0);
229 
230 	drm_object_attach_property(&connector->base,
231 				      config->dpms_property, 0);
232 
233 	drm_object_attach_property(&connector->base,
234 				   config->link_status_property,
235 				   0);
236 
237 	drm_object_attach_property(&connector->base,
238 				   config->non_desktop_property,
239 				   0);
240 
241 	if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
242 		drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
243 	}
244 
245 	connector->debugfs_entry = NULL;
246 out_put_type_id:
247 	if (ret)
248 		ida_simple_remove(connector_ida, connector->connector_type_id);
249 out_put_id:
250 	if (ret)
251 		ida_simple_remove(&config->connector_ida, connector->index);
252 out_put:
253 	if (ret)
254 		drm_mode_object_unregister(dev, &connector->base);
255 
256 	return ret;
257 }
258 EXPORT_SYMBOL(drm_connector_init);
259 
260 /**
261  * drm_mode_connector_attach_encoder - attach a connector to an encoder
262  * @connector: connector to attach
263  * @encoder: encoder to attach @connector to
264  *
265  * This function links up a connector to an encoder. Note that the routing
266  * restrictions between encoders and crtcs are exposed to userspace through the
267  * possible_clones and possible_crtcs bitmasks.
268  *
269  * Returns:
270  * Zero on success, negative errno on failure.
271  */
272 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
273 				      struct drm_encoder *encoder)
274 {
275 	int i;
276 
277 	/*
278 	 * In the past, drivers have attempted to model the static association
279 	 * of connector to encoder in simple connector/encoder devices using a
280 	 * direct assignment of connector->encoder = encoder. This connection
281 	 * is a logical one and the responsibility of the core, so drivers are
282 	 * expected not to mess with this.
283 	 *
284 	 * Note that the error return should've been enough here, but a large
285 	 * majority of drivers ignores the return value, so add in a big WARN
286 	 * to get people's attention.
287 	 */
288 	if (WARN_ON(connector->encoder))
289 		return -EINVAL;
290 
291 	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
292 		if (connector->encoder_ids[i] == 0) {
293 			connector->encoder_ids[i] = encoder->base.id;
294 			return 0;
295 		}
296 	}
297 	return -ENOMEM;
298 }
299 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
300 
301 static void drm_mode_remove(struct drm_connector *connector,
302 			    struct drm_display_mode *mode)
303 {
304 	list_del(&mode->head);
305 	drm_mode_destroy(connector->dev, mode);
306 }
307 
308 /**
309  * drm_connector_cleanup - cleans up an initialised connector
310  * @connector: connector to cleanup
311  *
312  * Cleans up the connector but doesn't free the object.
313  */
314 void drm_connector_cleanup(struct drm_connector *connector)
315 {
316 	struct drm_device *dev = connector->dev;
317 	struct drm_display_mode *mode, *t;
318 
319 	/* The connector should have been removed from userspace long before
320 	 * it is finally destroyed.
321 	 */
322 	if (WARN_ON(connector->registered))
323 		drm_connector_unregister(connector);
324 
325 	if (connector->tile_group) {
326 		drm_mode_put_tile_group(dev, connector->tile_group);
327 		connector->tile_group = NULL;
328 	}
329 
330 	list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
331 		drm_mode_remove(connector, mode);
332 
333 	list_for_each_entry_safe(mode, t, &connector->modes, head)
334 		drm_mode_remove(connector, mode);
335 
336 	ida_simple_remove(&drm_connector_enum_list[connector->connector_type].ida,
337 			  connector->connector_type_id);
338 
339 	ida_simple_remove(&dev->mode_config.connector_ida,
340 			  connector->index);
341 
342 	kfree(connector->display_info.bus_formats);
343 	drm_mode_object_unregister(dev, &connector->base);
344 	kfree(connector->name);
345 	connector->name = NULL;
346 	spin_lock_irq(&dev->mode_config.connector_list_lock);
347 	list_del(&connector->head);
348 	dev->mode_config.num_connector--;
349 	spin_unlock_irq(&dev->mode_config.connector_list_lock);
350 
351 	WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
352 	if (connector->state && connector->funcs->atomic_destroy_state)
353 		connector->funcs->atomic_destroy_state(connector,
354 						       connector->state);
355 
356 	mutex_destroy(&connector->mutex);
357 
358 	memset(connector, 0, sizeof(*connector));
359 }
360 EXPORT_SYMBOL(drm_connector_cleanup);
361 
362 /**
363  * drm_connector_register - register a connector
364  * @connector: the connector to register
365  *
366  * Register userspace interfaces for a connector
367  *
368  * Returns:
369  * Zero on success, error code on failure.
370  */
371 int drm_connector_register(struct drm_connector *connector)
372 {
373 	int ret = 0;
374 
375 	if (!connector->dev->registered)
376 		return 0;
377 
378 	mutex_lock(&connector->mutex);
379 	if (connector->registered)
380 		goto unlock;
381 
382 	ret = drm_sysfs_connector_add(connector);
383 	if (ret)
384 		goto unlock;
385 
386 	ret = drm_debugfs_connector_add(connector);
387 	if (ret) {
388 		goto err_sysfs;
389 	}
390 
391 	if (connector->funcs->late_register) {
392 		ret = connector->funcs->late_register(connector);
393 		if (ret)
394 			goto err_debugfs;
395 	}
396 
397 	drm_mode_object_register(connector->dev, &connector->base);
398 
399 	connector->registered = true;
400 	goto unlock;
401 
402 err_debugfs:
403 	drm_debugfs_connector_remove(connector);
404 err_sysfs:
405 	drm_sysfs_connector_remove(connector);
406 unlock:
407 	mutex_unlock(&connector->mutex);
408 	return ret;
409 }
410 EXPORT_SYMBOL(drm_connector_register);
411 
412 /**
413  * drm_connector_unregister - unregister a connector
414  * @connector: the connector to unregister
415  *
416  * Unregister userspace interfaces for a connector
417  */
418 void drm_connector_unregister(struct drm_connector *connector)
419 {
420 	mutex_lock(&connector->mutex);
421 	if (!connector->registered) {
422 		mutex_unlock(&connector->mutex);
423 		return;
424 	}
425 
426 	if (connector->funcs->early_unregister)
427 		connector->funcs->early_unregister(connector);
428 
429 	drm_sysfs_connector_remove(connector);
430 	drm_debugfs_connector_remove(connector);
431 
432 	connector->registered = false;
433 	mutex_unlock(&connector->mutex);
434 }
435 EXPORT_SYMBOL(drm_connector_unregister);
436 
437 void drm_connector_unregister_all(struct drm_device *dev)
438 {
439 	struct drm_connector *connector;
440 	struct drm_connector_list_iter conn_iter;
441 
442 	drm_connector_list_iter_begin(dev, &conn_iter);
443 	drm_for_each_connector_iter(connector, &conn_iter)
444 		drm_connector_unregister(connector);
445 	drm_connector_list_iter_end(&conn_iter);
446 }
447 
448 int drm_connector_register_all(struct drm_device *dev)
449 {
450 	struct drm_connector *connector;
451 	struct drm_connector_list_iter conn_iter;
452 	int ret = 0;
453 
454 	drm_connector_list_iter_begin(dev, &conn_iter);
455 	drm_for_each_connector_iter(connector, &conn_iter) {
456 		ret = drm_connector_register(connector);
457 		if (ret)
458 			break;
459 	}
460 	drm_connector_list_iter_end(&conn_iter);
461 
462 	if (ret)
463 		drm_connector_unregister_all(dev);
464 	return ret;
465 }
466 
467 /**
468  * drm_get_connector_status_name - return a string for connector status
469  * @status: connector status to compute name of
470  *
471  * In contrast to the other drm_get_*_name functions this one here returns a
472  * const pointer and hence is threadsafe.
473  */
474 const char *drm_get_connector_status_name(enum drm_connector_status status)
475 {
476 	if (status == connector_status_connected)
477 		return "connected";
478 	else if (status == connector_status_disconnected)
479 		return "disconnected";
480 	else
481 		return "unknown";
482 }
483 EXPORT_SYMBOL(drm_get_connector_status_name);
484 
485 /**
486  * drm_get_connector_force_name - return a string for connector force
487  * @force: connector force to get name of
488  *
489  * Returns: const pointer to name.
490  */
491 const char *drm_get_connector_force_name(enum drm_connector_force force)
492 {
493 	switch (force) {
494 	case DRM_FORCE_UNSPECIFIED:
495 		return "unspecified";
496 	case DRM_FORCE_OFF:
497 		return "off";
498 	case DRM_FORCE_ON:
499 		return "on";
500 	case DRM_FORCE_ON_DIGITAL:
501 		return "digital";
502 	default:
503 		return "unknown";
504 	}
505 }
506 
507 #ifdef CONFIG_LOCKDEP
508 static struct lockdep_map connector_list_iter_dep_map = {
509 	.name = "drm_connector_list_iter"
510 };
511 #endif
512 
513 /**
514  * drm_connector_list_iter_begin - initialize a connector_list iterator
515  * @dev: DRM device
516  * @iter: connector_list iterator
517  *
518  * Sets @iter up to walk the &drm_mode_config.connector_list of @dev. @iter
519  * must always be cleaned up again by calling drm_connector_list_iter_end().
520  * Iteration itself happens using drm_connector_list_iter_next() or
521  * drm_for_each_connector_iter().
522  */
523 void drm_connector_list_iter_begin(struct drm_device *dev,
524 				   struct drm_connector_list_iter *iter)
525 {
526 	iter->dev = dev;
527 	iter->conn = NULL;
528 	lock_acquire_shared_recursive(&connector_list_iter_dep_map, 0, 1, NULL, _RET_IP_);
529 }
530 EXPORT_SYMBOL(drm_connector_list_iter_begin);
531 
532 /**
533  * drm_connector_list_iter_next - return next connector
534  * @iter: connectr_list iterator
535  *
536  * Returns the next connector for @iter, or NULL when the list walk has
537  * completed.
538  */
539 struct drm_connector *
540 drm_connector_list_iter_next(struct drm_connector_list_iter *iter)
541 {
542 	struct drm_connector *old_conn = iter->conn;
543 	struct drm_mode_config *config = &iter->dev->mode_config;
544 	struct list_head *lhead;
545 	unsigned long flags;
546 
547 	spin_lock_irqsave(&config->connector_list_lock, flags);
548 	lhead = old_conn ? &old_conn->head : &config->connector_list;
549 
550 	do {
551 		if (lhead->next == &config->connector_list) {
552 			iter->conn = NULL;
553 			break;
554 		}
555 
556 		lhead = lhead->next;
557 		iter->conn = list_entry(lhead, struct drm_connector, head);
558 
559 		/* loop until it's not a zombie connector */
560 	} while (!kref_get_unless_zero(&iter->conn->base.refcount));
561 	spin_unlock_irqrestore(&config->connector_list_lock, flags);
562 
563 	if (old_conn)
564 		drm_connector_put(old_conn);
565 
566 	return iter->conn;
567 }
568 EXPORT_SYMBOL(drm_connector_list_iter_next);
569 
570 /**
571  * drm_connector_list_iter_end - tear down a connector_list iterator
572  * @iter: connector_list iterator
573  *
574  * Tears down @iter and releases any resources (like &drm_connector references)
575  * acquired while walking the list. This must always be called, both when the
576  * iteration completes fully or when it was aborted without walking the entire
577  * list.
578  */
579 void drm_connector_list_iter_end(struct drm_connector_list_iter *iter)
580 {
581 	iter->dev = NULL;
582 	if (iter->conn)
583 		drm_connector_put(iter->conn);
584 	lock_release(&connector_list_iter_dep_map, 0, _RET_IP_);
585 }
586 EXPORT_SYMBOL(drm_connector_list_iter_end);
587 
588 static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
589 	{ SubPixelUnknown, "Unknown" },
590 	{ SubPixelHorizontalRGB, "Horizontal RGB" },
591 	{ SubPixelHorizontalBGR, "Horizontal BGR" },
592 	{ SubPixelVerticalRGB, "Vertical RGB" },
593 	{ SubPixelVerticalBGR, "Vertical BGR" },
594 	{ SubPixelNone, "None" },
595 };
596 
597 /**
598  * drm_get_subpixel_order_name - return a string for a given subpixel enum
599  * @order: enum of subpixel_order
600  *
601  * Note you could abuse this and return something out of bounds, but that
602  * would be a caller error.  No unscrubbed user data should make it here.
603  */
604 const char *drm_get_subpixel_order_name(enum subpixel_order order)
605 {
606 	return drm_subpixel_enum_list[order].name;
607 }
608 EXPORT_SYMBOL(drm_get_subpixel_order_name);
609 
610 static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
611 	{ DRM_MODE_DPMS_ON, "On" },
612 	{ DRM_MODE_DPMS_STANDBY, "Standby" },
613 	{ DRM_MODE_DPMS_SUSPEND, "Suspend" },
614 	{ DRM_MODE_DPMS_OFF, "Off" }
615 };
616 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
617 
618 static const struct drm_prop_enum_list drm_link_status_enum_list[] = {
619 	{ DRM_MODE_LINK_STATUS_GOOD, "Good" },
620 	{ DRM_MODE_LINK_STATUS_BAD, "Bad" },
621 };
622 
623 /**
624  * drm_display_info_set_bus_formats - set the supported bus formats
625  * @info: display info to store bus formats in
626  * @formats: array containing the supported bus formats
627  * @num_formats: the number of entries in the fmts array
628  *
629  * Store the supported bus formats in display info structure.
630  * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
631  * a full list of available formats.
632  */
633 int drm_display_info_set_bus_formats(struct drm_display_info *info,
634 				     const u32 *formats,
635 				     unsigned int num_formats)
636 {
637 	u32 *fmts = NULL;
638 
639 	if (!formats && num_formats)
640 		return -EINVAL;
641 
642 	if (formats && num_formats) {
643 		fmts = kmemdup(formats, sizeof(*formats) * num_formats,
644 			       GFP_KERNEL);
645 		if (!fmts)
646 			return -ENOMEM;
647 	}
648 
649 	kfree(info->bus_formats);
650 	info->bus_formats = fmts;
651 	info->num_bus_formats = num_formats;
652 
653 	return 0;
654 }
655 EXPORT_SYMBOL(drm_display_info_set_bus_formats);
656 
657 /* Optional connector properties. */
658 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
659 	{ DRM_MODE_SCALE_NONE, "None" },
660 	{ DRM_MODE_SCALE_FULLSCREEN, "Full" },
661 	{ DRM_MODE_SCALE_CENTER, "Center" },
662 	{ DRM_MODE_SCALE_ASPECT, "Full aspect" },
663 };
664 
665 static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
666 	{ DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
667 	{ DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
668 	{ DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
669 };
670 
671 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
672 	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
673 	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
674 	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
675 };
676 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
677 
678 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
679 	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
680 	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
681 	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
682 };
683 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
684 		 drm_dvi_i_subconnector_enum_list)
685 
686 static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
687 	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
688 	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
689 	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
690 	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
691 	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
692 };
693 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
694 
695 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
696 	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
697 	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
698 	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
699 	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
700 	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
701 };
702 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
703 		 drm_tv_subconnector_enum_list)
704 
705 /**
706  * DOC: standard connector properties
707  *
708  * DRM connectors have a few standardized properties:
709  *
710  * EDID:
711  * 	Blob property which contains the current EDID read from the sink. This
712  * 	is useful to parse sink identification information like vendor, model
713  * 	and serial. Drivers should update this property by calling
714  * 	drm_mode_connector_update_edid_property(), usually after having parsed
715  * 	the EDID using drm_add_edid_modes(). Userspace cannot change this
716  * 	property.
717  * DPMS:
718  * 	Legacy property for setting the power state of the connector. For atomic
719  * 	drivers this is only provided for backwards compatibility with existing
720  * 	drivers, it remaps to controlling the "ACTIVE" property on the CRTC the
721  * 	connector is linked to. Drivers should never set this property directly,
722  * 	it is handled by the DRM core by calling the &drm_connector_funcs.dpms
723  * 	callback. For atomic drivers the remapping to the "ACTIVE" property is
724  * 	implemented in the DRM core.  This is the only standard connector
725  * 	property that userspace can change.
726  *
727  * 	Note that this property cannot be set through the MODE_ATOMIC ioctl,
728  * 	userspace must use "ACTIVE" on the CRTC instead.
729  *
730  * 	WARNING:
731  *
732  * 	For userspace also running on legacy drivers the "DPMS" semantics are a
733  * 	lot more complicated. First, userspace cannot rely on the "DPMS" value
734  * 	returned by the GETCONNECTOR actually reflecting reality, because many
735  * 	drivers fail to update it. For atomic drivers this is taken care of in
736  * 	drm_atomic_helper_update_legacy_modeset_state().
737  *
738  * 	The second issue is that the DPMS state is only well-defined when the
739  * 	connector is connected to a CRTC. In atomic the DRM core enforces that
740  * 	"ACTIVE" is off in such a case, no such checks exists for "DPMS".
741  *
742  * 	Finally, when enabling an output using the legacy SETCONFIG ioctl then
743  * 	"DPMS" is forced to ON. But see above, that might not be reflected in
744  * 	the software value on legacy drivers.
745  *
746  * 	Summarizing: Only set "DPMS" when the connector is known to be enabled,
747  * 	assume that a successful SETCONFIG call also sets "DPMS" to on, and
748  * 	never read back the value of "DPMS" because it can be incorrect.
749  * PATH:
750  * 	Connector path property to identify how this sink is physically
751  * 	connected. Used by DP MST. This should be set by calling
752  * 	drm_mode_connector_set_path_property(), in the case of DP MST with the
753  * 	path property the MST manager created. Userspace cannot change this
754  * 	property.
755  * TILE:
756  * 	Connector tile group property to indicate how a set of DRM connector
757  * 	compose together into one logical screen. This is used by both high-res
758  * 	external screens (often only using a single cable, but exposing multiple
759  * 	DP MST sinks), or high-res integrated panels (like dual-link DSI) which
760  * 	are not gen-locked. Note that for tiled panels which are genlocked, like
761  * 	dual-link LVDS or dual-link DSI, the driver should try to not expose the
762  * 	tiling and virtualize both &drm_crtc and &drm_plane if needed. Drivers
763  * 	should update this value using drm_mode_connector_set_tile_property().
764  * 	Userspace cannot change this property.
765  * link-status:
766  *      Connector link-status property to indicate the status of link. The default
767  *      value of link-status is "GOOD". If something fails during or after modeset,
768  *      the kernel driver may set this to "BAD" and issue a hotplug uevent. Drivers
769  *      should update this value using drm_mode_connector_set_link_status_property().
770  * non_desktop:
771  * 	Indicates the output should be ignored for purposes of displaying a
772  * 	standard desktop environment or console. This is most likely because
773  * 	the output device is not rectilinear.
774  *
775  * Connectors also have one standardized atomic property:
776  *
777  * CRTC_ID:
778  * 	Mode object ID of the &drm_crtc this connector should be connected to.
779  */
780 
781 int drm_connector_create_standard_properties(struct drm_device *dev)
782 {
783 	struct drm_property *prop;
784 
785 	prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
786 				   DRM_MODE_PROP_IMMUTABLE,
787 				   "EDID", 0);
788 	if (!prop)
789 		return -ENOMEM;
790 	dev->mode_config.edid_property = prop;
791 
792 	prop = drm_property_create_enum(dev, 0,
793 				   "DPMS", drm_dpms_enum_list,
794 				   ARRAY_SIZE(drm_dpms_enum_list));
795 	if (!prop)
796 		return -ENOMEM;
797 	dev->mode_config.dpms_property = prop;
798 
799 	prop = drm_property_create(dev,
800 				   DRM_MODE_PROP_BLOB |
801 				   DRM_MODE_PROP_IMMUTABLE,
802 				   "PATH", 0);
803 	if (!prop)
804 		return -ENOMEM;
805 	dev->mode_config.path_property = prop;
806 
807 	prop = drm_property_create(dev,
808 				   DRM_MODE_PROP_BLOB |
809 				   DRM_MODE_PROP_IMMUTABLE,
810 				   "TILE", 0);
811 	if (!prop)
812 		return -ENOMEM;
813 	dev->mode_config.tile_property = prop;
814 
815 	prop = drm_property_create_enum(dev, 0, "link-status",
816 					drm_link_status_enum_list,
817 					ARRAY_SIZE(drm_link_status_enum_list));
818 	if (!prop)
819 		return -ENOMEM;
820 	dev->mode_config.link_status_property = prop;
821 
822 	prop = drm_property_create_bool(dev, DRM_MODE_PROP_IMMUTABLE, "non-desktop");
823 	if (!prop)
824 		return -ENOMEM;
825 	dev->mode_config.non_desktop_property = prop;
826 
827 	return 0;
828 }
829 
830 /**
831  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
832  * @dev: DRM device
833  *
834  * Called by a driver the first time a DVI-I connector is made.
835  */
836 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
837 {
838 	struct drm_property *dvi_i_selector;
839 	struct drm_property *dvi_i_subconnector;
840 
841 	if (dev->mode_config.dvi_i_select_subconnector_property)
842 		return 0;
843 
844 	dvi_i_selector =
845 		drm_property_create_enum(dev, 0,
846 				    "select subconnector",
847 				    drm_dvi_i_select_enum_list,
848 				    ARRAY_SIZE(drm_dvi_i_select_enum_list));
849 	dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
850 
851 	dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
852 				    "subconnector",
853 				    drm_dvi_i_subconnector_enum_list,
854 				    ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
855 	dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
856 
857 	return 0;
858 }
859 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
860 
861 /**
862  * drm_create_tv_properties - create TV specific connector properties
863  * @dev: DRM device
864  * @num_modes: number of different TV formats (modes) supported
865  * @modes: array of pointers to strings containing name of each format
866  *
867  * Called by a driver's TV initialization routine, this function creates
868  * the TV specific connector properties for a given device.  Caller is
869  * responsible for allocating a list of format names and passing them to
870  * this routine.
871  */
872 int drm_mode_create_tv_properties(struct drm_device *dev,
873 				  unsigned int num_modes,
874 				  const char * const modes[])
875 {
876 	struct drm_property *tv_selector;
877 	struct drm_property *tv_subconnector;
878 	unsigned int i;
879 
880 	if (dev->mode_config.tv_select_subconnector_property)
881 		return 0;
882 
883 	/*
884 	 * Basic connector properties
885 	 */
886 	tv_selector = drm_property_create_enum(dev, 0,
887 					  "select subconnector",
888 					  drm_tv_select_enum_list,
889 					  ARRAY_SIZE(drm_tv_select_enum_list));
890 	if (!tv_selector)
891 		goto nomem;
892 
893 	dev->mode_config.tv_select_subconnector_property = tv_selector;
894 
895 	tv_subconnector =
896 		drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
897 				    "subconnector",
898 				    drm_tv_subconnector_enum_list,
899 				    ARRAY_SIZE(drm_tv_subconnector_enum_list));
900 	if (!tv_subconnector)
901 		goto nomem;
902 	dev->mode_config.tv_subconnector_property = tv_subconnector;
903 
904 	/*
905 	 * Other, TV specific properties: margins & TV modes.
906 	 */
907 	dev->mode_config.tv_left_margin_property =
908 		drm_property_create_range(dev, 0, "left margin", 0, 100);
909 	if (!dev->mode_config.tv_left_margin_property)
910 		goto nomem;
911 
912 	dev->mode_config.tv_right_margin_property =
913 		drm_property_create_range(dev, 0, "right margin", 0, 100);
914 	if (!dev->mode_config.tv_right_margin_property)
915 		goto nomem;
916 
917 	dev->mode_config.tv_top_margin_property =
918 		drm_property_create_range(dev, 0, "top margin", 0, 100);
919 	if (!dev->mode_config.tv_top_margin_property)
920 		goto nomem;
921 
922 	dev->mode_config.tv_bottom_margin_property =
923 		drm_property_create_range(dev, 0, "bottom margin", 0, 100);
924 	if (!dev->mode_config.tv_bottom_margin_property)
925 		goto nomem;
926 
927 	dev->mode_config.tv_mode_property =
928 		drm_property_create(dev, DRM_MODE_PROP_ENUM,
929 				    "mode", num_modes);
930 	if (!dev->mode_config.tv_mode_property)
931 		goto nomem;
932 
933 	for (i = 0; i < num_modes; i++)
934 		drm_property_add_enum(dev->mode_config.tv_mode_property, i,
935 				      i, modes[i]);
936 
937 	dev->mode_config.tv_brightness_property =
938 		drm_property_create_range(dev, 0, "brightness", 0, 100);
939 	if (!dev->mode_config.tv_brightness_property)
940 		goto nomem;
941 
942 	dev->mode_config.tv_contrast_property =
943 		drm_property_create_range(dev, 0, "contrast", 0, 100);
944 	if (!dev->mode_config.tv_contrast_property)
945 		goto nomem;
946 
947 	dev->mode_config.tv_flicker_reduction_property =
948 		drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
949 	if (!dev->mode_config.tv_flicker_reduction_property)
950 		goto nomem;
951 
952 	dev->mode_config.tv_overscan_property =
953 		drm_property_create_range(dev, 0, "overscan", 0, 100);
954 	if (!dev->mode_config.tv_overscan_property)
955 		goto nomem;
956 
957 	dev->mode_config.tv_saturation_property =
958 		drm_property_create_range(dev, 0, "saturation", 0, 100);
959 	if (!dev->mode_config.tv_saturation_property)
960 		goto nomem;
961 
962 	dev->mode_config.tv_hue_property =
963 		drm_property_create_range(dev, 0, "hue", 0, 100);
964 	if (!dev->mode_config.tv_hue_property)
965 		goto nomem;
966 
967 	return 0;
968 nomem:
969 	return -ENOMEM;
970 }
971 EXPORT_SYMBOL(drm_mode_create_tv_properties);
972 
973 /**
974  * drm_mode_create_scaling_mode_property - create scaling mode property
975  * @dev: DRM device
976  *
977  * Called by a driver the first time it's needed, must be attached to desired
978  * connectors.
979  *
980  * Atomic drivers should use drm_connector_attach_scaling_mode_property()
981  * instead to correctly assign &drm_connector_state.picture_aspect_ratio
982  * in the atomic state.
983  */
984 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
985 {
986 	struct drm_property *scaling_mode;
987 
988 	if (dev->mode_config.scaling_mode_property)
989 		return 0;
990 
991 	scaling_mode =
992 		drm_property_create_enum(dev, 0, "scaling mode",
993 				drm_scaling_mode_enum_list,
994 				    ARRAY_SIZE(drm_scaling_mode_enum_list));
995 
996 	dev->mode_config.scaling_mode_property = scaling_mode;
997 
998 	return 0;
999 }
1000 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1001 
1002 /**
1003  * drm_connector_attach_scaling_mode_property - attach atomic scaling mode property
1004  * @connector: connector to attach scaling mode property on.
1005  * @scaling_mode_mask: or'ed mask of BIT(%DRM_MODE_SCALE_\*).
1006  *
1007  * This is used to add support for scaling mode to atomic drivers.
1008  * The scaling mode will be set to &drm_connector_state.picture_aspect_ratio
1009  * and can be used from &drm_connector_helper_funcs->atomic_check for validation.
1010  *
1011  * This is the atomic version of drm_mode_create_scaling_mode_property().
1012  *
1013  * Returns:
1014  * Zero on success, negative errno on failure.
1015  */
1016 int drm_connector_attach_scaling_mode_property(struct drm_connector *connector,
1017 					       u32 scaling_mode_mask)
1018 {
1019 	struct drm_device *dev = connector->dev;
1020 	struct drm_property *scaling_mode_property;
1021 	int i, j = 0;
1022 	const unsigned valid_scaling_mode_mask =
1023 		(1U << ARRAY_SIZE(drm_scaling_mode_enum_list)) - 1;
1024 
1025 	if (WARN_ON(hweight32(scaling_mode_mask) < 2 ||
1026 		    scaling_mode_mask & ~valid_scaling_mode_mask))
1027 		return -EINVAL;
1028 
1029 	scaling_mode_property =
1030 		drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
1031 				    hweight32(scaling_mode_mask));
1032 
1033 	if (!scaling_mode_property)
1034 		return -ENOMEM;
1035 
1036 	for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++) {
1037 		int ret;
1038 
1039 		if (!(BIT(i) & scaling_mode_mask))
1040 			continue;
1041 
1042 		ret = drm_property_add_enum(scaling_mode_property, j++,
1043 					    drm_scaling_mode_enum_list[i].type,
1044 					    drm_scaling_mode_enum_list[i].name);
1045 
1046 		if (ret) {
1047 			drm_property_destroy(dev, scaling_mode_property);
1048 
1049 			return ret;
1050 		}
1051 	}
1052 
1053 	drm_object_attach_property(&connector->base,
1054 				   scaling_mode_property, 0);
1055 
1056 	connector->scaling_mode_property = scaling_mode_property;
1057 
1058 	return 0;
1059 }
1060 EXPORT_SYMBOL(drm_connector_attach_scaling_mode_property);
1061 
1062 /**
1063  * drm_mode_create_aspect_ratio_property - create aspect ratio property
1064  * @dev: DRM device
1065  *
1066  * Called by a driver the first time it's needed, must be attached to desired
1067  * connectors.
1068  *
1069  * Returns:
1070  * Zero on success, negative errno on failure.
1071  */
1072 int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1073 {
1074 	if (dev->mode_config.aspect_ratio_property)
1075 		return 0;
1076 
1077 	dev->mode_config.aspect_ratio_property =
1078 		drm_property_create_enum(dev, 0, "aspect ratio",
1079 				drm_aspect_ratio_enum_list,
1080 				ARRAY_SIZE(drm_aspect_ratio_enum_list));
1081 
1082 	if (dev->mode_config.aspect_ratio_property == NULL)
1083 		return -ENOMEM;
1084 
1085 	return 0;
1086 }
1087 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1088 
1089 /**
1090  * drm_mode_create_suggested_offset_properties - create suggests offset properties
1091  * @dev: DRM device
1092  *
1093  * Create the the suggested x/y offset property for connectors.
1094  */
1095 int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1096 {
1097 	if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1098 		return 0;
1099 
1100 	dev->mode_config.suggested_x_property =
1101 		drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1102 
1103 	dev->mode_config.suggested_y_property =
1104 		drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1105 
1106 	if (dev->mode_config.suggested_x_property == NULL ||
1107 	    dev->mode_config.suggested_y_property == NULL)
1108 		return -ENOMEM;
1109 	return 0;
1110 }
1111 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1112 
1113 /**
1114  * drm_mode_connector_set_path_property - set tile property on connector
1115  * @connector: connector to set property on.
1116  * @path: path to use for property; must not be NULL.
1117  *
1118  * This creates a property to expose to userspace to specify a
1119  * connector path. This is mainly used for DisplayPort MST where
1120  * connectors have a topology and we want to allow userspace to give
1121  * them more meaningful names.
1122  *
1123  * Returns:
1124  * Zero on success, negative errno on failure.
1125  */
1126 int drm_mode_connector_set_path_property(struct drm_connector *connector,
1127 					 const char *path)
1128 {
1129 	struct drm_device *dev = connector->dev;
1130 	int ret;
1131 
1132 	ret = drm_property_replace_global_blob(dev,
1133 	                                       &connector->path_blob_ptr,
1134 	                                       strlen(path) + 1,
1135 	                                       path,
1136 	                                       &connector->base,
1137 	                                       dev->mode_config.path_property);
1138 	return ret;
1139 }
1140 EXPORT_SYMBOL(drm_mode_connector_set_path_property);
1141 
1142 /**
1143  * drm_mode_connector_set_tile_property - set tile property on connector
1144  * @connector: connector to set property on.
1145  *
1146  * This looks up the tile information for a connector, and creates a
1147  * property for userspace to parse if it exists. The property is of
1148  * the form of 8 integers using ':' as a separator.
1149  *
1150  * Returns:
1151  * Zero on success, errno on failure.
1152  */
1153 int drm_mode_connector_set_tile_property(struct drm_connector *connector)
1154 {
1155 	struct drm_device *dev = connector->dev;
1156 	char tile[256];
1157 	int ret;
1158 
1159 	if (!connector->has_tile) {
1160 		ret  = drm_property_replace_global_blob(dev,
1161 		                                        &connector->tile_blob_ptr,
1162 		                                        0,
1163 		                                        NULL,
1164 		                                        &connector->base,
1165 		                                        dev->mode_config.tile_property);
1166 		return ret;
1167 	}
1168 
1169 	snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
1170 		 connector->tile_group->id, connector->tile_is_single_monitor,
1171 		 connector->num_h_tile, connector->num_v_tile,
1172 		 connector->tile_h_loc, connector->tile_v_loc,
1173 		 connector->tile_h_size, connector->tile_v_size);
1174 
1175 	ret = drm_property_replace_global_blob(dev,
1176 	                                       &connector->tile_blob_ptr,
1177 	                                       strlen(tile) + 1,
1178 	                                       tile,
1179 	                                       &connector->base,
1180 	                                       dev->mode_config.tile_property);
1181 	return ret;
1182 }
1183 EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
1184 
1185 /**
1186  * drm_mode_connector_update_edid_property - update the edid property of a connector
1187  * @connector: drm connector
1188  * @edid: new value of the edid property
1189  *
1190  * This function creates a new blob modeset object and assigns its id to the
1191  * connector's edid property.
1192  *
1193  * Returns:
1194  * Zero on success, negative errno on failure.
1195  */
1196 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
1197 					    const struct edid *edid)
1198 {
1199 	struct drm_device *dev = connector->dev;
1200 	size_t size = 0;
1201 	int ret;
1202 
1203 	/* ignore requests to set edid when overridden */
1204 	if (connector->override_edid)
1205 		return 0;
1206 
1207 	if (edid)
1208 		size = EDID_LENGTH * (1 + edid->extensions);
1209 
1210 	drm_object_property_set_value(&connector->base,
1211 				      dev->mode_config.non_desktop_property,
1212 				      connector->display_info.non_desktop);
1213 
1214 	ret = drm_property_replace_global_blob(dev,
1215 					       &connector->edid_blob_ptr,
1216 	                                       size,
1217 	                                       edid,
1218 	                                       &connector->base,
1219 	                                       dev->mode_config.edid_property);
1220 	return ret;
1221 }
1222 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
1223 
1224 /**
1225  * drm_mode_connector_set_link_status_property - Set link status property of a connector
1226  * @connector: drm connector
1227  * @link_status: new value of link status property (0: Good, 1: Bad)
1228  *
1229  * In usual working scenario, this link status property will always be set to
1230  * "GOOD". If something fails during or after a mode set, the kernel driver
1231  * may set this link status property to "BAD". The caller then needs to send a
1232  * hotplug uevent for userspace to re-check the valid modes through
1233  * GET_CONNECTOR_IOCTL and retry modeset.
1234  *
1235  * Note: Drivers cannot rely on userspace to support this property and
1236  * issue a modeset. As such, they may choose to handle issues (like
1237  * re-training a link) without userspace's intervention.
1238  *
1239  * The reason for adding this property is to handle link training failures, but
1240  * it is not limited to DP or link training. For example, if we implement
1241  * asynchronous setcrtc, this property can be used to report any failures in that.
1242  */
1243 void drm_mode_connector_set_link_status_property(struct drm_connector *connector,
1244 						 uint64_t link_status)
1245 {
1246 	struct drm_device *dev = connector->dev;
1247 
1248 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1249 	connector->state->link_status = link_status;
1250 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
1251 }
1252 EXPORT_SYMBOL(drm_mode_connector_set_link_status_property);
1253 
1254 int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
1255 				    struct drm_property *property,
1256 				    uint64_t value)
1257 {
1258 	int ret = -EINVAL;
1259 	struct drm_connector *connector = obj_to_connector(obj);
1260 
1261 	/* Do DPMS ourselves */
1262 	if (property == connector->dev->mode_config.dpms_property) {
1263 		ret = (*connector->funcs->dpms)(connector, (int)value);
1264 	} else if (connector->funcs->set_property)
1265 		ret = connector->funcs->set_property(connector, property, value);
1266 
1267 	if (!ret)
1268 		drm_object_property_set_value(&connector->base, property, value);
1269 	return ret;
1270 }
1271 
1272 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
1273 				       void *data, struct drm_file *file_priv)
1274 {
1275 	struct drm_mode_connector_set_property *conn_set_prop = data;
1276 	struct drm_mode_obj_set_property obj_set_prop = {
1277 		.value = conn_set_prop->value,
1278 		.prop_id = conn_set_prop->prop_id,
1279 		.obj_id = conn_set_prop->connector_id,
1280 		.obj_type = DRM_MODE_OBJECT_CONNECTOR
1281 	};
1282 
1283 	/* It does all the locking and checking we need */
1284 	return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
1285 }
1286 
1287 static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
1288 {
1289 	/* For atomic drivers only state objects are synchronously updated and
1290 	 * protected by modeset locks, so check those first. */
1291 	if (connector->state)
1292 		return connector->state->best_encoder;
1293 	return connector->encoder;
1294 }
1295 
1296 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1297 					 const struct drm_file *file_priv)
1298 {
1299 	/*
1300 	 * If user-space hasn't configured the driver to expose the stereo 3D
1301 	 * modes, don't expose them.
1302 	 */
1303 	if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1304 		return false;
1305 
1306 	return true;
1307 }
1308 
1309 int drm_mode_getconnector(struct drm_device *dev, void *data,
1310 			  struct drm_file *file_priv)
1311 {
1312 	struct drm_mode_get_connector *out_resp = data;
1313 	struct drm_connector *connector;
1314 	struct drm_encoder *encoder;
1315 	struct drm_display_mode *mode;
1316 	int mode_count = 0;
1317 	int encoders_count = 0;
1318 	int ret = 0;
1319 	int copied = 0;
1320 	int i;
1321 	struct drm_mode_modeinfo u_mode;
1322 	struct drm_mode_modeinfo __user *mode_ptr;
1323 	uint32_t __user *encoder_ptr;
1324 
1325 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1326 		return -EINVAL;
1327 
1328 	memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1329 
1330 	connector = drm_connector_lookup(dev, file_priv, out_resp->connector_id);
1331 	if (!connector)
1332 		return -ENOENT;
1333 
1334 	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
1335 		if (connector->encoder_ids[i] != 0)
1336 			encoders_count++;
1337 
1338 	if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1339 		copied = 0;
1340 		encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1341 		for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1342 			if (connector->encoder_ids[i] != 0) {
1343 				if (put_user(connector->encoder_ids[i],
1344 					     encoder_ptr + copied)) {
1345 					ret = -EFAULT;
1346 					goto out;
1347 				}
1348 				copied++;
1349 			}
1350 		}
1351 	}
1352 	out_resp->count_encoders = encoders_count;
1353 
1354 	out_resp->connector_id = connector->base.id;
1355 	out_resp->connector_type = connector->connector_type;
1356 	out_resp->connector_type_id = connector->connector_type_id;
1357 
1358 	mutex_lock(&dev->mode_config.mutex);
1359 	if (out_resp->count_modes == 0) {
1360 		connector->funcs->fill_modes(connector,
1361 					     dev->mode_config.max_width,
1362 					     dev->mode_config.max_height);
1363 	}
1364 
1365 	out_resp->mm_width = connector->display_info.width_mm;
1366 	out_resp->mm_height = connector->display_info.height_mm;
1367 	out_resp->subpixel = connector->display_info.subpixel_order;
1368 	out_resp->connection = connector->status;
1369 
1370 	/* delayed so we get modes regardless of pre-fill_modes state */
1371 	list_for_each_entry(mode, &connector->modes, head)
1372 		if (drm_mode_expose_to_userspace(mode, file_priv))
1373 			mode_count++;
1374 
1375 	/*
1376 	 * This ioctl is called twice, once to determine how much space is
1377 	 * needed, and the 2nd time to fill it.
1378 	 */
1379 	if ((out_resp->count_modes >= mode_count) && mode_count) {
1380 		copied = 0;
1381 		mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1382 		list_for_each_entry(mode, &connector->modes, head) {
1383 			if (!drm_mode_expose_to_userspace(mode, file_priv))
1384 				continue;
1385 
1386 			drm_mode_convert_to_umode(&u_mode, mode);
1387 			if (copy_to_user(mode_ptr + copied,
1388 					 &u_mode, sizeof(u_mode))) {
1389 				ret = -EFAULT;
1390 				mutex_unlock(&dev->mode_config.mutex);
1391 
1392 				goto out;
1393 			}
1394 			copied++;
1395 		}
1396 	}
1397 	out_resp->count_modes = mode_count;
1398 	mutex_unlock(&dev->mode_config.mutex);
1399 
1400 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1401 	encoder = drm_connector_get_encoder(connector);
1402 	if (encoder)
1403 		out_resp->encoder_id = encoder->base.id;
1404 	else
1405 		out_resp->encoder_id = 0;
1406 
1407 	/* Only grab properties after probing, to make sure EDID and other
1408 	 * properties reflect the latest status. */
1409 	ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
1410 			(uint32_t __user *)(unsigned long)(out_resp->props_ptr),
1411 			(uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
1412 			&out_resp->count_props);
1413 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
1414 
1415 out:
1416 	drm_connector_put(connector);
1417 
1418 	return ret;
1419 }
1420 
1421 
1422 /**
1423  * DOC: Tile group
1424  *
1425  * Tile groups are used to represent tiled monitors with a unique integer
1426  * identifier. Tiled monitors using DisplayID v1.3 have a unique 8-byte handle,
1427  * we store this in a tile group, so we have a common identifier for all tiles
1428  * in a monitor group. The property is called "TILE". Drivers can manage tile
1429  * groups using drm_mode_create_tile_group(), drm_mode_put_tile_group() and
1430  * drm_mode_get_tile_group(). But this is only needed for internal panels where
1431  * the tile group information is exposed through a non-standard way.
1432  */
1433 
1434 static void drm_tile_group_free(struct kref *kref)
1435 {
1436 	struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
1437 	struct drm_device *dev = tg->dev;
1438 	mutex_lock(&dev->mode_config.idr_mutex);
1439 	idr_remove(&dev->mode_config.tile_idr, tg->id);
1440 	mutex_unlock(&dev->mode_config.idr_mutex);
1441 	kfree(tg);
1442 }
1443 
1444 /**
1445  * drm_mode_put_tile_group - drop a reference to a tile group.
1446  * @dev: DRM device
1447  * @tg: tile group to drop reference to.
1448  *
1449  * drop reference to tile group and free if 0.
1450  */
1451 void drm_mode_put_tile_group(struct drm_device *dev,
1452 			     struct drm_tile_group *tg)
1453 {
1454 	kref_put(&tg->refcount, drm_tile_group_free);
1455 }
1456 EXPORT_SYMBOL(drm_mode_put_tile_group);
1457 
1458 /**
1459  * drm_mode_get_tile_group - get a reference to an existing tile group
1460  * @dev: DRM device
1461  * @topology: 8-bytes unique per monitor.
1462  *
1463  * Use the unique bytes to get a reference to an existing tile group.
1464  *
1465  * RETURNS:
1466  * tile group or NULL if not found.
1467  */
1468 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
1469 					       char topology[8])
1470 {
1471 	struct drm_tile_group *tg;
1472 	int id;
1473 	mutex_lock(&dev->mode_config.idr_mutex);
1474 	idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
1475 		if (!memcmp(tg->group_data, topology, 8)) {
1476 			if (!kref_get_unless_zero(&tg->refcount))
1477 				tg = NULL;
1478 			mutex_unlock(&dev->mode_config.idr_mutex);
1479 			return tg;
1480 		}
1481 	}
1482 	mutex_unlock(&dev->mode_config.idr_mutex);
1483 	return NULL;
1484 }
1485 EXPORT_SYMBOL(drm_mode_get_tile_group);
1486 
1487 /**
1488  * drm_mode_create_tile_group - create a tile group from a displayid description
1489  * @dev: DRM device
1490  * @topology: 8-bytes unique per monitor.
1491  *
1492  * Create a tile group for the unique monitor, and get a unique
1493  * identifier for the tile group.
1494  *
1495  * RETURNS:
1496  * new tile group or error.
1497  */
1498 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
1499 						  char topology[8])
1500 {
1501 	struct drm_tile_group *tg;
1502 	int ret;
1503 
1504 	tg = kzalloc(sizeof(*tg), GFP_KERNEL);
1505 	if (!tg)
1506 		return ERR_PTR(-ENOMEM);
1507 
1508 	kref_init(&tg->refcount);
1509 	memcpy(tg->group_data, topology, 8);
1510 	tg->dev = dev;
1511 
1512 	mutex_lock(&dev->mode_config.idr_mutex);
1513 	ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
1514 	if (ret >= 0) {
1515 		tg->id = ret;
1516 	} else {
1517 		kfree(tg);
1518 		tg = ERR_PTR(ret);
1519 	}
1520 
1521 	mutex_unlock(&dev->mode_config.idr_mutex);
1522 	return tg;
1523 }
1524 EXPORT_SYMBOL(drm_mode_create_tile_group);
1525