blob: 020225809ff495a3d7a0605d28028595c473966d [file] [log] [blame]
Daniel Vetter52217192016-08-12 22:48:50 +02001/*
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>
Laurent Pinchart93382032016-11-28 20:51:09 +020026#include <drm/drm_encoder.h>
Daniel Vetter52217192016-08-12 22:48:50 +020027
28#include "drm_crtc_internal.h"
29#include "drm_internal.h"
30
Daniel Vetterae2a6da2016-08-12 22:48:53 +020031/**
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_reference() and
39 * drm_connector_unreference().
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.
44 *
45 * The connector is then registered with a call to drm_connector_init() with a
46 * pointer to the connector functions and a connector type, and exposed through
47 * sysfs 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_mode_connector_attach_encoder(). The
52 * driver must also set the struct &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
Daniel Vetter52217192016-08-12 22:48:50 +020062struct 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 */
71static 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};
91
92void drm_connector_ida_init(void)
93{
94 int i;
95
96 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
97 ida_init(&drm_connector_enum_list[i].ida);
98}
99
100void drm_connector_ida_destroy(void)
101{
102 int i;
103
104 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
105 ida_destroy(&drm_connector_enum_list[i].ida);
106}
107
108/**
109 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
110 * @connector: connector to quwery
111 *
Daniel Vetterae2a6da2016-08-12 22:48:53 +0200112 * The kernel supports per-connector configuration of its consoles through
Daniel Vetter52217192016-08-12 22:48:50 +0200113 * use of the video= parameter. This function parses that option and
114 * extracts the user's specified mode (or enable/disable status) for a
115 * particular connector. This is typically only used during the early fbdev
116 * setup.
117 */
118static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
119{
120 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
121 char *option = NULL;
122
123 if (fb_get_options(connector->name, &option))
124 return;
125
126 if (!drm_mode_parse_command_line_for_connector(option,
127 connector,
128 mode))
129 return;
130
131 if (mode->force) {
132 const char *s;
133
134 switch (mode->force) {
135 case DRM_FORCE_OFF:
136 s = "OFF";
137 break;
138 case DRM_FORCE_ON_DIGITAL:
139 s = "ON - dig";
140 break;
141 default:
142 case DRM_FORCE_ON:
143 s = "ON";
144 break;
145 }
146
147 DRM_INFO("forcing %s connector %s\n", connector->name, s);
148 connector->force = mode->force;
149 }
150
151 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
152 connector->name,
153 mode->xres, mode->yres,
154 mode->refresh_specified ? mode->refresh : 60,
155 mode->rb ? " reduced blanking" : "",
156 mode->margins ? " with margins" : "",
157 mode->interlace ? " interlaced" : "");
158}
159
160static void drm_connector_free(struct kref *kref)
161{
162 struct drm_connector *connector =
163 container_of(kref, struct drm_connector, base.refcount);
164 struct drm_device *dev = connector->dev;
165
166 drm_mode_object_unregister(dev, &connector->base);
167 connector->funcs->destroy(connector);
168}
169
170/**
171 * drm_connector_init - Init a preallocated connector
172 * @dev: DRM device
173 * @connector: the connector to init
174 * @funcs: callbacks for this connector
175 * @connector_type: user visible type of the connector
176 *
177 * Initialises a preallocated connector. Connectors should be
178 * subclassed as part of driver connector objects.
179 *
180 * Returns:
181 * Zero on success, error code on failure.
182 */
183int drm_connector_init(struct drm_device *dev,
184 struct drm_connector *connector,
185 const struct drm_connector_funcs *funcs,
186 int connector_type)
187{
188 struct drm_mode_config *config = &dev->mode_config;
189 int ret;
190 struct ida *connector_ida =
191 &drm_connector_enum_list[connector_type].ida;
192
Daniel Vetter52217192016-08-12 22:48:50 +0200193 ret = drm_mode_object_get_reg(dev, &connector->base,
194 DRM_MODE_OBJECT_CONNECTOR,
195 false, drm_connector_free);
196 if (ret)
Daniel Vetter613051d2016-12-14 00:08:06 +0100197 return ret;
Daniel Vetter52217192016-08-12 22:48:50 +0200198
199 connector->base.properties = &connector->properties;
200 connector->dev = dev;
201 connector->funcs = funcs;
202
203 ret = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
204 if (ret < 0)
205 goto out_put;
206 connector->index = ret;
207 ret = 0;
208
209 connector->connector_type = connector_type;
210 connector->connector_type_id =
211 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
212 if (connector->connector_type_id < 0) {
213 ret = connector->connector_type_id;
214 goto out_put_id;
215 }
216 connector->name =
217 kasprintf(GFP_KERNEL, "%s-%d",
218 drm_connector_enum_list[connector_type].name,
219 connector->connector_type_id);
220 if (!connector->name) {
221 ret = -ENOMEM;
222 goto out_put_type_id;
223 }
224
225 INIT_LIST_HEAD(&connector->probed_modes);
226 INIT_LIST_HEAD(&connector->modes);
227 connector->edid_blob_ptr = NULL;
228 connector->status = connector_status_unknown;
229
230 drm_connector_get_cmdline_mode(connector);
231
232 /* We should add connectors at the end to avoid upsetting the connector
233 * index too much. */
Daniel Vetter613051d2016-12-14 00:08:06 +0100234 spin_lock_irq(&config->connector_list_lock);
Daniel Vetter52217192016-08-12 22:48:50 +0200235 list_add_tail(&connector->head, &config->connector_list);
236 config->num_connector++;
Daniel Vetter613051d2016-12-14 00:08:06 +0100237 spin_unlock_irq(&config->connector_list_lock);
Daniel Vetter52217192016-08-12 22:48:50 +0200238
239 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
240 drm_object_attach_property(&connector->base,
241 config->edid_property,
242 0);
243
244 drm_object_attach_property(&connector->base,
245 config->dpms_property, 0);
246
247 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
248 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
249 }
250
251 connector->debugfs_entry = NULL;
252out_put_type_id:
253 if (ret)
Christophe JAILLET587680c2016-10-02 08:01:22 +0200254 ida_simple_remove(connector_ida, connector->connector_type_id);
Daniel Vetter52217192016-08-12 22:48:50 +0200255out_put_id:
256 if (ret)
Christophe JAILLET587680c2016-10-02 08:01:22 +0200257 ida_simple_remove(&config->connector_ida, connector->index);
Daniel Vetter52217192016-08-12 22:48:50 +0200258out_put:
259 if (ret)
260 drm_mode_object_unregister(dev, &connector->base);
261
Daniel Vetter52217192016-08-12 22:48:50 +0200262 return ret;
263}
264EXPORT_SYMBOL(drm_connector_init);
265
266/**
267 * drm_mode_connector_attach_encoder - attach a connector to an encoder
268 * @connector: connector to attach
269 * @encoder: encoder to attach @connector to
270 *
271 * This function links up a connector to an encoder. Note that the routing
272 * restrictions between encoders and crtcs are exposed to userspace through the
273 * possible_clones and possible_crtcs bitmasks.
274 *
275 * Returns:
276 * Zero on success, negative errno on failure.
277 */
278int drm_mode_connector_attach_encoder(struct drm_connector *connector,
279 struct drm_encoder *encoder)
280{
281 int i;
282
283 /*
284 * In the past, drivers have attempted to model the static association
285 * of connector to encoder in simple connector/encoder devices using a
286 * direct assignment of connector->encoder = encoder. This connection
287 * is a logical one and the responsibility of the core, so drivers are
288 * expected not to mess with this.
289 *
290 * Note that the error return should've been enough here, but a large
291 * majority of drivers ignores the return value, so add in a big WARN
292 * to get people's attention.
293 */
294 if (WARN_ON(connector->encoder))
295 return -EINVAL;
296
297 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
298 if (connector->encoder_ids[i] == 0) {
299 connector->encoder_ids[i] = encoder->base.id;
300 return 0;
301 }
302 }
303 return -ENOMEM;
304}
305EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
306
307static void drm_mode_remove(struct drm_connector *connector,
308 struct drm_display_mode *mode)
309{
310 list_del(&mode->head);
311 drm_mode_destroy(connector->dev, mode);
312}
313
314/**
315 * drm_connector_cleanup - cleans up an initialised connector
316 * @connector: connector to cleanup
317 *
318 * Cleans up the connector but doesn't free the object.
319 */
320void drm_connector_cleanup(struct drm_connector *connector)
321{
322 struct drm_device *dev = connector->dev;
323 struct drm_display_mode *mode, *t;
324
325 /* The connector should have been removed from userspace long before
326 * it is finally destroyed.
327 */
328 if (WARN_ON(connector->registered))
329 drm_connector_unregister(connector);
330
331 if (connector->tile_group) {
332 drm_mode_put_tile_group(dev, connector->tile_group);
333 connector->tile_group = NULL;
334 }
335
336 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
337 drm_mode_remove(connector, mode);
338
339 list_for_each_entry_safe(mode, t, &connector->modes, head)
340 drm_mode_remove(connector, mode);
341
Christophe JAILLET9a47dba2016-10-07 09:27:41 +0200342 ida_simple_remove(&drm_connector_enum_list[connector->connector_type].ida,
343 connector->connector_type_id);
Daniel Vetter52217192016-08-12 22:48:50 +0200344
Christophe JAILLET9a47dba2016-10-07 09:27:41 +0200345 ida_simple_remove(&dev->mode_config.connector_ida,
346 connector->index);
Daniel Vetter52217192016-08-12 22:48:50 +0200347
348 kfree(connector->display_info.bus_formats);
349 drm_mode_object_unregister(dev, &connector->base);
350 kfree(connector->name);
351 connector->name = NULL;
Daniel Vetter613051d2016-12-14 00:08:06 +0100352 spin_lock_irq(&dev->mode_config.connector_list_lock);
Daniel Vetter52217192016-08-12 22:48:50 +0200353 list_del(&connector->head);
354 dev->mode_config.num_connector--;
Daniel Vetter613051d2016-12-14 00:08:06 +0100355 spin_unlock_irq(&dev->mode_config.connector_list_lock);
Daniel Vetter52217192016-08-12 22:48:50 +0200356
357 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
358 if (connector->state && connector->funcs->atomic_destroy_state)
359 connector->funcs->atomic_destroy_state(connector,
360 connector->state);
361
362 memset(connector, 0, sizeof(*connector));
363}
364EXPORT_SYMBOL(drm_connector_cleanup);
365
366/**
367 * drm_connector_register - register a connector
368 * @connector: the connector to register
369 *
370 * Register userspace interfaces for a connector
371 *
372 * Returns:
373 * Zero on success, error code on failure.
374 */
375int drm_connector_register(struct drm_connector *connector)
376{
377 int ret;
378
379 if (connector->registered)
380 return 0;
381
382 ret = drm_sysfs_connector_add(connector);
383 if (ret)
384 return ret;
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 return 0;
401
402err_debugfs:
403 drm_debugfs_connector_remove(connector);
404err_sysfs:
405 drm_sysfs_connector_remove(connector);
406 return ret;
407}
408EXPORT_SYMBOL(drm_connector_register);
409
410/**
411 * drm_connector_unregister - unregister a connector
412 * @connector: the connector to unregister
413 *
414 * Unregister userspace interfaces for a connector
415 */
416void drm_connector_unregister(struct drm_connector *connector)
417{
418 if (!connector->registered)
419 return;
420
421 if (connector->funcs->early_unregister)
422 connector->funcs->early_unregister(connector);
423
424 drm_sysfs_connector_remove(connector);
425 drm_debugfs_connector_remove(connector);
426
427 connector->registered = false;
428}
429EXPORT_SYMBOL(drm_connector_unregister);
430
431void drm_connector_unregister_all(struct drm_device *dev)
432{
433 struct drm_connector *connector;
Daniel Vetter613051d2016-12-14 00:08:06 +0100434 struct drm_connector_list_iter conn_iter;
Daniel Vetter52217192016-08-12 22:48:50 +0200435
Daniel Vetter613051d2016-12-14 00:08:06 +0100436 drm_connector_list_iter_get(dev, &conn_iter);
437 drm_for_each_connector_iter(connector, &conn_iter)
Daniel Vetter52217192016-08-12 22:48:50 +0200438 drm_connector_unregister(connector);
Daniel Vetter613051d2016-12-14 00:08:06 +0100439 drm_connector_list_iter_put(&conn_iter);
Daniel Vetter52217192016-08-12 22:48:50 +0200440}
441
442int drm_connector_register_all(struct drm_device *dev)
443{
444 struct drm_connector *connector;
Daniel Vetter613051d2016-12-14 00:08:06 +0100445 struct drm_connector_list_iter conn_iter;
446 int ret = 0;
Daniel Vetter52217192016-08-12 22:48:50 +0200447
Daniel Vetter613051d2016-12-14 00:08:06 +0100448 drm_connector_list_iter_get(dev, &conn_iter);
449 drm_for_each_connector_iter(connector, &conn_iter) {
Daniel Vetter52217192016-08-12 22:48:50 +0200450 ret = drm_connector_register(connector);
451 if (ret)
Daniel Vetter613051d2016-12-14 00:08:06 +0100452 break;
Daniel Vetter52217192016-08-12 22:48:50 +0200453 }
Daniel Vetter613051d2016-12-14 00:08:06 +0100454 drm_connector_list_iter_put(&conn_iter);
Daniel Vetter52217192016-08-12 22:48:50 +0200455
Daniel Vetter613051d2016-12-14 00:08:06 +0100456 if (ret)
457 drm_connector_unregister_all(dev);
Daniel Vetter52217192016-08-12 22:48:50 +0200458 return ret;
459}
460
461/**
462 * drm_get_connector_status_name - return a string for connector status
463 * @status: connector status to compute name of
464 *
465 * In contrast to the other drm_get_*_name functions this one here returns a
466 * const pointer and hence is threadsafe.
467 */
468const char *drm_get_connector_status_name(enum drm_connector_status status)
469{
470 if (status == connector_status_connected)
471 return "connected";
472 else if (status == connector_status_disconnected)
473 return "disconnected";
474 else
475 return "unknown";
476}
477EXPORT_SYMBOL(drm_get_connector_status_name);
478
Daniel Vetter613051d2016-12-14 00:08:06 +0100479#ifdef CONFIG_LOCKDEP
480static struct lockdep_map connector_list_iter_dep_map = {
481 .name = "drm_connector_list_iter"
482};
483#endif
484
485/**
486 * drm_connector_list_iter_get - initialize a connector_list iterator
487 * @dev: DRM device
488 * @iter: connector_list iterator
489 *
490 * Sets @iter up to walk the connector list in &drm_mode_config of @dev. @iter
491 * must always be cleaned up again by calling drm_connector_list_iter_put().
492 * Iteration itself happens using drm_connector_list_iter_next() or
493 * drm_for_each_connector_iter().
494 */
495void drm_connector_list_iter_get(struct drm_device *dev,
496 struct drm_connector_list_iter *iter)
497{
498 iter->dev = dev;
499 iter->conn = NULL;
500 lock_acquire_shared_recursive(&connector_list_iter_dep_map, 0, 1, NULL, _RET_IP_);
501}
502EXPORT_SYMBOL(drm_connector_list_iter_get);
503
504/**
505 * drm_connector_list_iter_next - return next connector
506 * @iter: connectr_list iterator
507 *
508 * Returns the next connector for @iter, or NULL when the list walk has
509 * completed.
510 */
511struct drm_connector *
512drm_connector_list_iter_next(struct drm_connector_list_iter *iter)
513{
514 struct drm_connector *old_conn = iter->conn;
515 struct drm_mode_config *config = &iter->dev->mode_config;
516 struct list_head *lhead;
517 unsigned long flags;
518
519 spin_lock_irqsave(&config->connector_list_lock, flags);
520 lhead = old_conn ? &old_conn->head : &config->connector_list;
521
522 do {
523 if (lhead->next == &config->connector_list) {
524 iter->conn = NULL;
525 break;
526 }
527
528 lhead = lhead->next;
529 iter->conn = list_entry(lhead, struct drm_connector, head);
530
531 /* loop until it's not a zombie connector */
532 } while (!kref_get_unless_zero(&iter->conn->base.refcount));
533 spin_unlock_irqrestore(&config->connector_list_lock, flags);
534
535 if (old_conn)
536 drm_connector_unreference(old_conn);
537
538 return iter->conn;
539}
540EXPORT_SYMBOL(drm_connector_list_iter_next);
541
542/**
543 * drm_connector_list_iter_put - tear down a connector_list iterator
544 * @iter: connector_list iterator
545 *
546 * Tears down @iter and releases any resources (like &drm_connector references)
547 * acquired while walking the list. This must always be called, both when the
548 * iteration completes fully or when it was aborted without walking the entire
549 * list.
550 */
551void drm_connector_list_iter_put(struct drm_connector_list_iter *iter)
552{
553 iter->dev = NULL;
554 if (iter->conn)
555 drm_connector_unreference(iter->conn);
556 lock_release(&connector_list_iter_dep_map, 0, _RET_IP_);
557}
558EXPORT_SYMBOL(drm_connector_list_iter_put);
559
Daniel Vetter52217192016-08-12 22:48:50 +0200560static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
561 { SubPixelUnknown, "Unknown" },
562 { SubPixelHorizontalRGB, "Horizontal RGB" },
563 { SubPixelHorizontalBGR, "Horizontal BGR" },
564 { SubPixelVerticalRGB, "Vertical RGB" },
565 { SubPixelVerticalBGR, "Vertical BGR" },
566 { SubPixelNone, "None" },
567};
568
569/**
570 * drm_get_subpixel_order_name - return a string for a given subpixel enum
571 * @order: enum of subpixel_order
572 *
573 * Note you could abuse this and return something out of bounds, but that
574 * would be a caller error. No unscrubbed user data should make it here.
575 */
576const char *drm_get_subpixel_order_name(enum subpixel_order order)
577{
578 return drm_subpixel_enum_list[order].name;
579}
580EXPORT_SYMBOL(drm_get_subpixel_order_name);
581
582static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
583 { DRM_MODE_DPMS_ON, "On" },
584 { DRM_MODE_DPMS_STANDBY, "Standby" },
585 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
586 { DRM_MODE_DPMS_OFF, "Off" }
587};
588DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
589
Daniel Vetterb3c6c8b2016-08-12 22:48:55 +0200590/**
591 * drm_display_info_set_bus_formats - set the supported bus formats
592 * @info: display info to store bus formats in
593 * @formats: array containing the supported bus formats
594 * @num_formats: the number of entries in the fmts array
595 *
596 * Store the supported bus formats in display info structure.
597 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
598 * a full list of available formats.
599 */
600int drm_display_info_set_bus_formats(struct drm_display_info *info,
601 const u32 *formats,
602 unsigned int num_formats)
603{
604 u32 *fmts = NULL;
605
606 if (!formats && num_formats)
607 return -EINVAL;
608
609 if (formats && num_formats) {
610 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
611 GFP_KERNEL);
612 if (!fmts)
613 return -ENOMEM;
614 }
615
616 kfree(info->bus_formats);
617 info->bus_formats = fmts;
618 info->num_bus_formats = num_formats;
619
620 return 0;
621}
622EXPORT_SYMBOL(drm_display_info_set_bus_formats);
623
Daniel Vetter52217192016-08-12 22:48:50 +0200624/* Optional connector properties. */
625static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
626 { DRM_MODE_SCALE_NONE, "None" },
627 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
628 { DRM_MODE_SCALE_CENTER, "Center" },
629 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
630};
631
632static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
633 { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
634 { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
635 { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
636};
637
638static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
639 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
640 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
641 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
642};
643DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
644
645static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
646 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
647 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
648 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
649};
650DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
651 drm_dvi_i_subconnector_enum_list)
652
653static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
654 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
655 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
656 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
657 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
658 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
659};
660DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
661
662static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
663 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
664 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
665 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
666 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
667 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
668};
669DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
670 drm_tv_subconnector_enum_list)
671
Daniel Vetter4ada6f22016-11-17 09:56:48 +0100672/**
673 * DOC: standard connector properties
674 *
675 * DRM connectors have a few standardized properties:
676 *
677 * EDID:
678 * Blob property which contains the current EDID read from the sink. This
679 * is useful to parse sink identification information like vendor, model
680 * and serial. Drivers should update this property by calling
681 * drm_mode_connector_update_edid_property(), usually after having parsed
682 * the EDID using drm_add_edid_modes(). Userspace cannot change this
683 * property.
684 * DPMS:
685 * Legacy property for setting the power state of the connector. For atomic
686 * drivers this is only provided for backwards compatibility with existing
687 * drivers, it remaps to controlling the "ACTIVE" property on the CRTC the
688 * connector is linked to. Drivers should never set this property directly,
689 * it is handled by the DRM core by calling the ->dpms() callback in
690 * &drm_connector_funcs. Atomic drivers should implement this hook using
691 * drm_atomic_helper_connector_dpms(). This is the only property standard
692 * connector property that userspace can change.
693 * PATH:
694 * Connector path property to identify how this sink is physically
695 * connected. Used by DP MST. This should be set by calling
696 * drm_mode_connector_set_path_property(), in the case of DP MST with the
697 * path property the MST manager created. Userspace cannot change this
698 * property.
699 * TILE:
700 * Connector tile group property to indicate how a set of DRM connector
701 * compose together into one logical screen. This is used by both high-res
702 * external screens (often only using a single cable, but exposing multiple
703 * DP MST sinks), or high-res integrated panels (like dual-link DSI) which
704 * are not gen-locked. Note that for tiled panels which are genlocked, like
705 * dual-link LVDS or dual-link DSI, the driver should try to not expose the
706 * tiling and virtualize both &drm_crtc and &drm_plane if needed. Drivers
707 * should update this value using drm_mode_connector_set_tile_property().
708 * Userspace cannot change this property.
709 *
710 * Connectors also have one standardized atomic property:
711 *
712 * CRTC_ID:
713 * Mode object ID of the &drm_crtc this connector should be connected to.
714 */
715
Daniel Vetter52217192016-08-12 22:48:50 +0200716int drm_connector_create_standard_properties(struct drm_device *dev)
717{
718 struct drm_property *prop;
719
720 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
721 DRM_MODE_PROP_IMMUTABLE,
722 "EDID", 0);
723 if (!prop)
724 return -ENOMEM;
725 dev->mode_config.edid_property = prop;
726
727 prop = drm_property_create_enum(dev, 0,
728 "DPMS", drm_dpms_enum_list,
729 ARRAY_SIZE(drm_dpms_enum_list));
730 if (!prop)
731 return -ENOMEM;
732 dev->mode_config.dpms_property = prop;
733
734 prop = drm_property_create(dev,
735 DRM_MODE_PROP_BLOB |
736 DRM_MODE_PROP_IMMUTABLE,
737 "PATH", 0);
738 if (!prop)
739 return -ENOMEM;
740 dev->mode_config.path_property = prop;
741
742 prop = drm_property_create(dev,
743 DRM_MODE_PROP_BLOB |
744 DRM_MODE_PROP_IMMUTABLE,
745 "TILE", 0);
746 if (!prop)
747 return -ENOMEM;
748 dev->mode_config.tile_property = prop;
749
750 return 0;
751}
752
753/**
754 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
755 * @dev: DRM device
756 *
757 * Called by a driver the first time a DVI-I connector is made.
758 */
759int drm_mode_create_dvi_i_properties(struct drm_device *dev)
760{
761 struct drm_property *dvi_i_selector;
762 struct drm_property *dvi_i_subconnector;
763
764 if (dev->mode_config.dvi_i_select_subconnector_property)
765 return 0;
766
767 dvi_i_selector =
768 drm_property_create_enum(dev, 0,
769 "select subconnector",
770 drm_dvi_i_select_enum_list,
771 ARRAY_SIZE(drm_dvi_i_select_enum_list));
772 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
773
774 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
775 "subconnector",
776 drm_dvi_i_subconnector_enum_list,
777 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
778 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
779
780 return 0;
781}
782EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
783
784/**
785 * drm_create_tv_properties - create TV specific connector properties
786 * @dev: DRM device
787 * @num_modes: number of different TV formats (modes) supported
788 * @modes: array of pointers to strings containing name of each format
789 *
790 * Called by a driver's TV initialization routine, this function creates
791 * the TV specific connector properties for a given device. Caller is
792 * responsible for allocating a list of format names and passing them to
793 * this routine.
794 */
795int drm_mode_create_tv_properties(struct drm_device *dev,
796 unsigned int num_modes,
797 const char * const modes[])
798{
799 struct drm_property *tv_selector;
800 struct drm_property *tv_subconnector;
801 unsigned int i;
802
803 if (dev->mode_config.tv_select_subconnector_property)
804 return 0;
805
806 /*
807 * Basic connector properties
808 */
809 tv_selector = drm_property_create_enum(dev, 0,
810 "select subconnector",
811 drm_tv_select_enum_list,
812 ARRAY_SIZE(drm_tv_select_enum_list));
813 if (!tv_selector)
814 goto nomem;
815
816 dev->mode_config.tv_select_subconnector_property = tv_selector;
817
818 tv_subconnector =
819 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
820 "subconnector",
821 drm_tv_subconnector_enum_list,
822 ARRAY_SIZE(drm_tv_subconnector_enum_list));
823 if (!tv_subconnector)
824 goto nomem;
825 dev->mode_config.tv_subconnector_property = tv_subconnector;
826
827 /*
828 * Other, TV specific properties: margins & TV modes.
829 */
830 dev->mode_config.tv_left_margin_property =
831 drm_property_create_range(dev, 0, "left margin", 0, 100);
832 if (!dev->mode_config.tv_left_margin_property)
833 goto nomem;
834
835 dev->mode_config.tv_right_margin_property =
836 drm_property_create_range(dev, 0, "right margin", 0, 100);
837 if (!dev->mode_config.tv_right_margin_property)
838 goto nomem;
839
840 dev->mode_config.tv_top_margin_property =
841 drm_property_create_range(dev, 0, "top margin", 0, 100);
842 if (!dev->mode_config.tv_top_margin_property)
843 goto nomem;
844
845 dev->mode_config.tv_bottom_margin_property =
846 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
847 if (!dev->mode_config.tv_bottom_margin_property)
848 goto nomem;
849
850 dev->mode_config.tv_mode_property =
851 drm_property_create(dev, DRM_MODE_PROP_ENUM,
852 "mode", num_modes);
853 if (!dev->mode_config.tv_mode_property)
854 goto nomem;
855
856 for (i = 0; i < num_modes; i++)
857 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
858 i, modes[i]);
859
860 dev->mode_config.tv_brightness_property =
861 drm_property_create_range(dev, 0, "brightness", 0, 100);
862 if (!dev->mode_config.tv_brightness_property)
863 goto nomem;
864
865 dev->mode_config.tv_contrast_property =
866 drm_property_create_range(dev, 0, "contrast", 0, 100);
867 if (!dev->mode_config.tv_contrast_property)
868 goto nomem;
869
870 dev->mode_config.tv_flicker_reduction_property =
871 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
872 if (!dev->mode_config.tv_flicker_reduction_property)
873 goto nomem;
874
875 dev->mode_config.tv_overscan_property =
876 drm_property_create_range(dev, 0, "overscan", 0, 100);
877 if (!dev->mode_config.tv_overscan_property)
878 goto nomem;
879
880 dev->mode_config.tv_saturation_property =
881 drm_property_create_range(dev, 0, "saturation", 0, 100);
882 if (!dev->mode_config.tv_saturation_property)
883 goto nomem;
884
885 dev->mode_config.tv_hue_property =
886 drm_property_create_range(dev, 0, "hue", 0, 100);
887 if (!dev->mode_config.tv_hue_property)
888 goto nomem;
889
890 return 0;
891nomem:
892 return -ENOMEM;
893}
894EXPORT_SYMBOL(drm_mode_create_tv_properties);
895
896/**
897 * drm_mode_create_scaling_mode_property - create scaling mode property
898 * @dev: DRM device
899 *
900 * Called by a driver the first time it's needed, must be attached to desired
901 * connectors.
902 */
903int drm_mode_create_scaling_mode_property(struct drm_device *dev)
904{
905 struct drm_property *scaling_mode;
906
907 if (dev->mode_config.scaling_mode_property)
908 return 0;
909
910 scaling_mode =
911 drm_property_create_enum(dev, 0, "scaling mode",
912 drm_scaling_mode_enum_list,
913 ARRAY_SIZE(drm_scaling_mode_enum_list));
914
915 dev->mode_config.scaling_mode_property = scaling_mode;
916
917 return 0;
918}
919EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
920
921/**
922 * drm_mode_create_aspect_ratio_property - create aspect ratio property
923 * @dev: DRM device
924 *
925 * Called by a driver the first time it's needed, must be attached to desired
926 * connectors.
927 *
928 * Returns:
929 * Zero on success, negative errno on failure.
930 */
931int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
932{
933 if (dev->mode_config.aspect_ratio_property)
934 return 0;
935
936 dev->mode_config.aspect_ratio_property =
937 drm_property_create_enum(dev, 0, "aspect ratio",
938 drm_aspect_ratio_enum_list,
939 ARRAY_SIZE(drm_aspect_ratio_enum_list));
940
941 if (dev->mode_config.aspect_ratio_property == NULL)
942 return -ENOMEM;
943
944 return 0;
945}
946EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
947
948/**
949 * drm_mode_create_suggested_offset_properties - create suggests offset properties
950 * @dev: DRM device
951 *
952 * Create the the suggested x/y offset property for connectors.
953 */
954int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
955{
956 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
957 return 0;
958
959 dev->mode_config.suggested_x_property =
960 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
961
962 dev->mode_config.suggested_y_property =
963 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
964
965 if (dev->mode_config.suggested_x_property == NULL ||
966 dev->mode_config.suggested_y_property == NULL)
967 return -ENOMEM;
968 return 0;
969}
970EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
971
972/**
973 * drm_mode_connector_set_path_property - set tile property on connector
974 * @connector: connector to set property on.
975 * @path: path to use for property; must not be NULL.
976 *
977 * This creates a property to expose to userspace to specify a
978 * connector path. This is mainly used for DisplayPort MST where
979 * connectors have a topology and we want to allow userspace to give
980 * them more meaningful names.
981 *
982 * Returns:
983 * Zero on success, negative errno on failure.
984 */
985int drm_mode_connector_set_path_property(struct drm_connector *connector,
986 const char *path)
987{
988 struct drm_device *dev = connector->dev;
989 int ret;
990
991 ret = drm_property_replace_global_blob(dev,
992 &connector->path_blob_ptr,
993 strlen(path) + 1,
994 path,
995 &connector->base,
996 dev->mode_config.path_property);
997 return ret;
998}
999EXPORT_SYMBOL(drm_mode_connector_set_path_property);
1000
1001/**
1002 * drm_mode_connector_set_tile_property - set tile property on connector
1003 * @connector: connector to set property on.
1004 *
1005 * This looks up the tile information for a connector, and creates a
1006 * property for userspace to parse if it exists. The property is of
1007 * the form of 8 integers using ':' as a separator.
1008 *
1009 * Returns:
1010 * Zero on success, errno on failure.
1011 */
1012int drm_mode_connector_set_tile_property(struct drm_connector *connector)
1013{
1014 struct drm_device *dev = connector->dev;
1015 char tile[256];
1016 int ret;
1017
1018 if (!connector->has_tile) {
1019 ret = drm_property_replace_global_blob(dev,
1020 &connector->tile_blob_ptr,
1021 0,
1022 NULL,
1023 &connector->base,
1024 dev->mode_config.tile_property);
1025 return ret;
1026 }
1027
1028 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
1029 connector->tile_group->id, connector->tile_is_single_monitor,
1030 connector->num_h_tile, connector->num_v_tile,
1031 connector->tile_h_loc, connector->tile_v_loc,
1032 connector->tile_h_size, connector->tile_v_size);
1033
1034 ret = drm_property_replace_global_blob(dev,
1035 &connector->tile_blob_ptr,
1036 strlen(tile) + 1,
1037 tile,
1038 &connector->base,
1039 dev->mode_config.tile_property);
1040 return ret;
1041}
1042EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
1043
1044/**
1045 * drm_mode_connector_update_edid_property - update the edid property of a connector
1046 * @connector: drm connector
1047 * @edid: new value of the edid property
1048 *
1049 * This function creates a new blob modeset object and assigns its id to the
1050 * connector's edid property.
1051 *
1052 * Returns:
1053 * Zero on success, negative errno on failure.
1054 */
1055int drm_mode_connector_update_edid_property(struct drm_connector *connector,
1056 const struct edid *edid)
1057{
1058 struct drm_device *dev = connector->dev;
1059 size_t size = 0;
1060 int ret;
1061
1062 /* ignore requests to set edid when overridden */
1063 if (connector->override_edid)
1064 return 0;
1065
1066 if (edid)
1067 size = EDID_LENGTH * (1 + edid->extensions);
1068
1069 ret = drm_property_replace_global_blob(dev,
1070 &connector->edid_blob_ptr,
1071 size,
1072 edid,
1073 &connector->base,
1074 dev->mode_config.edid_property);
1075 return ret;
1076}
1077EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
1078
1079int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
1080 struct drm_property *property,
1081 uint64_t value)
1082{
1083 int ret = -EINVAL;
1084 struct drm_connector *connector = obj_to_connector(obj);
1085
1086 /* Do DPMS ourselves */
1087 if (property == connector->dev->mode_config.dpms_property) {
1088 ret = (*connector->funcs->dpms)(connector, (int)value);
1089 } else if (connector->funcs->set_property)
1090 ret = connector->funcs->set_property(connector, property, value);
1091
1092 /* store the property value if successful */
1093 if (!ret)
1094 drm_object_property_set_value(&connector->base, property, value);
1095 return ret;
1096}
1097
1098int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
1099 void *data, struct drm_file *file_priv)
1100{
1101 struct drm_mode_connector_set_property *conn_set_prop = data;
1102 struct drm_mode_obj_set_property obj_set_prop = {
1103 .value = conn_set_prop->value,
1104 .prop_id = conn_set_prop->prop_id,
1105 .obj_id = conn_set_prop->connector_id,
1106 .obj_type = DRM_MODE_OBJECT_CONNECTOR
1107 };
1108
1109 /* It does all the locking and checking we need */
1110 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
1111}
1112
1113static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
1114{
1115 /* For atomic drivers only state objects are synchronously updated and
1116 * protected by modeset locks, so check those first. */
1117 if (connector->state)
1118 return connector->state->best_encoder;
1119 return connector->encoder;
1120}
1121
1122static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1123 const struct drm_file *file_priv)
1124{
1125 /*
1126 * If user-space hasn't configured the driver to expose the stereo 3D
1127 * modes, don't expose them.
1128 */
1129 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1130 return false;
1131
1132 return true;
1133}
1134
1135int drm_mode_getconnector(struct drm_device *dev, void *data,
1136 struct drm_file *file_priv)
1137{
1138 struct drm_mode_get_connector *out_resp = data;
1139 struct drm_connector *connector;
1140 struct drm_encoder *encoder;
1141 struct drm_display_mode *mode;
1142 int mode_count = 0;
1143 int encoders_count = 0;
1144 int ret = 0;
1145 int copied = 0;
1146 int i;
1147 struct drm_mode_modeinfo u_mode;
1148 struct drm_mode_modeinfo __user *mode_ptr;
1149 uint32_t __user *encoder_ptr;
1150
1151 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1152 return -EINVAL;
1153
1154 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1155
1156 mutex_lock(&dev->mode_config.mutex);
1157
1158 connector = drm_connector_lookup(dev, out_resp->connector_id);
1159 if (!connector) {
1160 ret = -ENOENT;
1161 goto out_unlock;
1162 }
1163
1164 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
1165 if (connector->encoder_ids[i] != 0)
1166 encoders_count++;
1167
1168 if (out_resp->count_modes == 0) {
1169 connector->funcs->fill_modes(connector,
1170 dev->mode_config.max_width,
1171 dev->mode_config.max_height);
1172 }
1173
1174 /* delayed so we get modes regardless of pre-fill_modes state */
1175 list_for_each_entry(mode, &connector->modes, head)
1176 if (drm_mode_expose_to_userspace(mode, file_priv))
1177 mode_count++;
1178
1179 out_resp->connector_id = connector->base.id;
1180 out_resp->connector_type = connector->connector_type;
1181 out_resp->connector_type_id = connector->connector_type_id;
1182 out_resp->mm_width = connector->display_info.width_mm;
1183 out_resp->mm_height = connector->display_info.height_mm;
1184 out_resp->subpixel = connector->display_info.subpixel_order;
1185 out_resp->connection = connector->status;
1186
1187 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1188 encoder = drm_connector_get_encoder(connector);
1189 if (encoder)
1190 out_resp->encoder_id = encoder->base.id;
1191 else
1192 out_resp->encoder_id = 0;
1193
1194 /*
1195 * This ioctl is called twice, once to determine how much space is
1196 * needed, and the 2nd time to fill it.
1197 */
1198 if ((out_resp->count_modes >= mode_count) && mode_count) {
1199 copied = 0;
1200 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1201 list_for_each_entry(mode, &connector->modes, head) {
1202 if (!drm_mode_expose_to_userspace(mode, file_priv))
1203 continue;
1204
1205 drm_mode_convert_to_umode(&u_mode, mode);
1206 if (copy_to_user(mode_ptr + copied,
1207 &u_mode, sizeof(u_mode))) {
1208 ret = -EFAULT;
1209 goto out;
1210 }
1211 copied++;
1212 }
1213 }
1214 out_resp->count_modes = mode_count;
1215
1216 ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
1217 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
1218 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
1219 &out_resp->count_props);
1220 if (ret)
1221 goto out;
1222
1223 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1224 copied = 0;
1225 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1226 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1227 if (connector->encoder_ids[i] != 0) {
1228 if (put_user(connector->encoder_ids[i],
1229 encoder_ptr + copied)) {
1230 ret = -EFAULT;
1231 goto out;
1232 }
1233 copied++;
1234 }
1235 }
1236 }
1237 out_resp->count_encoders = encoders_count;
1238
1239out:
1240 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1241
1242 drm_connector_unreference(connector);
1243out_unlock:
1244 mutex_unlock(&dev->mode_config.mutex);
1245
1246 return ret;
1247}
1248
Daniel Vetter9498c192016-11-14 12:58:24 +01001249
1250/**
1251 * DOC: Tile group
1252 *
1253 * Tile groups are used to represent tiled monitors with a unique integer
1254 * identifier. Tiled monitors using DisplayID v1.3 have a unique 8-byte handle,
1255 * we store this in a tile group, so we have a common identifier for all tiles
1256 * in a monitor group. The property is called "TILE". Drivers can manage tile
1257 * groups using drm_mode_create_tile_group(), drm_mode_put_tile_group() and
1258 * drm_mode_get_tile_group(). But this is only needed for internal panels where
1259 * the tile group information is exposed through a non-standard way.
1260 */
1261
1262static void drm_tile_group_free(struct kref *kref)
1263{
1264 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
1265 struct drm_device *dev = tg->dev;
1266 mutex_lock(&dev->mode_config.idr_mutex);
1267 idr_remove(&dev->mode_config.tile_idr, tg->id);
1268 mutex_unlock(&dev->mode_config.idr_mutex);
1269 kfree(tg);
1270}
1271
1272/**
1273 * drm_mode_put_tile_group - drop a reference to a tile group.
1274 * @dev: DRM device
1275 * @tg: tile group to drop reference to.
1276 *
1277 * drop reference to tile group and free if 0.
1278 */
1279void drm_mode_put_tile_group(struct drm_device *dev,
1280 struct drm_tile_group *tg)
1281{
1282 kref_put(&tg->refcount, drm_tile_group_free);
1283}
1284EXPORT_SYMBOL(drm_mode_put_tile_group);
1285
1286/**
1287 * drm_mode_get_tile_group - get a reference to an existing tile group
1288 * @dev: DRM device
1289 * @topology: 8-bytes unique per monitor.
1290 *
1291 * Use the unique bytes to get a reference to an existing tile group.
1292 *
1293 * RETURNS:
1294 * tile group or NULL if not found.
1295 */
1296struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
1297 char topology[8])
1298{
1299 struct drm_tile_group *tg;
1300 int id;
1301 mutex_lock(&dev->mode_config.idr_mutex);
1302 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
1303 if (!memcmp(tg->group_data, topology, 8)) {
1304 if (!kref_get_unless_zero(&tg->refcount))
1305 tg = NULL;
1306 mutex_unlock(&dev->mode_config.idr_mutex);
1307 return tg;
1308 }
1309 }
1310 mutex_unlock(&dev->mode_config.idr_mutex);
1311 return NULL;
1312}
1313EXPORT_SYMBOL(drm_mode_get_tile_group);
1314
1315/**
1316 * drm_mode_create_tile_group - create a tile group from a displayid description
1317 * @dev: DRM device
1318 * @topology: 8-bytes unique per monitor.
1319 *
1320 * Create a tile group for the unique monitor, and get a unique
1321 * identifier for the tile group.
1322 *
1323 * RETURNS:
1324 * new tile group or error.
1325 */
1326struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
1327 char topology[8])
1328{
1329 struct drm_tile_group *tg;
1330 int ret;
1331
1332 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
1333 if (!tg)
1334 return ERR_PTR(-ENOMEM);
1335
1336 kref_init(&tg->refcount);
1337 memcpy(tg->group_data, topology, 8);
1338 tg->dev = dev;
1339
1340 mutex_lock(&dev->mode_config.idr_mutex);
1341 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
1342 if (ret >= 0) {
1343 tg->id = ret;
1344 } else {
1345 kfree(tg);
1346 tg = ERR_PTR(ret);
1347 }
1348
1349 mutex_unlock(&dev->mode_config.idr_mutex);
1350 return tg;
1351}
1352EXPORT_SYMBOL(drm_mode_create_tile_group);