5a452628939272969c54239498b9a1ca86cd4a67
[sfrench/cifs-2.6.git] / drivers / gpu / drm / drm_connector.c
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
27 #include "drm_crtc_internal.h"
28 #include "drm_internal.h"
29
30 /**
31  * DOC: overview
32  *
33  * In DRM connectors are the general abstraction for display sinks, and include
34  * als fixed panels or anything else that can display pixels in some form. As
35  * opposed to all other KMS objects representing hardware (like CRTC, encoder or
36  * plane abstractions) connectors can be hotplugged and unplugged at runtime.
37  * Hence they are reference-counted using drm_connector_reference() and
38  * drm_connector_unreference().
39  *
40  * KMS driver must create, initialize, register and attach at a struct
41  * &drm_connector for each such sink. The instance is created as other KMS
42  * objects and initialized by setting the following fields.
43  *
44  * The connector is then registered with a call to drm_connector_init() with a
45  * pointer to the connector functions and a connector type, and exposed through
46  * sysfs 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 struct &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                 const char *s;
132
133                 switch (mode->force) {
134                 case DRM_FORCE_OFF:
135                         s = "OFF";
136                         break;
137                 case DRM_FORCE_ON_DIGITAL:
138                         s = "ON - dig";
139                         break;
140                 default:
141                 case DRM_FORCE_ON:
142                         s = "ON";
143                         break;
144                 }
145
146                 DRM_INFO("forcing %s connector %s\n", connector->name, s);
147                 connector->force = mode->force;
148         }
149
150         DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
151                       connector->name,
152                       mode->xres, mode->yres,
153                       mode->refresh_specified ? mode->refresh : 60,
154                       mode->rb ? " reduced blanking" : "",
155                       mode->margins ? " with margins" : "",
156                       mode->interlace ?  " interlaced" : "");
157 }
158
159 static void drm_connector_free(struct kref *kref)
160 {
161         struct drm_connector *connector =
162                 container_of(kref, struct drm_connector, base.refcount);
163         struct drm_device *dev = connector->dev;
164
165         drm_mode_object_unregister(dev, &connector->base);
166         connector->funcs->destroy(connector);
167 }
168
169 /**
170  * drm_connector_init - Init a preallocated connector
171  * @dev: DRM device
172  * @connector: the connector to init
173  * @funcs: callbacks for this connector
174  * @connector_type: user visible type of the connector
175  *
176  * Initialises a preallocated connector. Connectors should be
177  * subclassed as part of driver connector objects.
178  *
179  * Returns:
180  * Zero on success, error code on failure.
181  */
182 int drm_connector_init(struct drm_device *dev,
183                        struct drm_connector *connector,
184                        const struct drm_connector_funcs *funcs,
185                        int connector_type)
186 {
187         struct drm_mode_config *config = &dev->mode_config;
188         int ret;
189         struct ida *connector_ida =
190                 &drm_connector_enum_list[connector_type].ida;
191
192         drm_modeset_lock_all(dev);
193
194         ret = drm_mode_object_get_reg(dev, &connector->base,
195                                       DRM_MODE_OBJECT_CONNECTOR,
196                                       false, drm_connector_free);
197         if (ret)
198                 goto out_unlock;
199
200         connector->base.properties = &connector->properties;
201         connector->dev = dev;
202         connector->funcs = funcs;
203
204         ret = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
205         if (ret < 0)
206                 goto out_put;
207         connector->index = ret;
208         ret = 0;
209
210         connector->connector_type = connector_type;
211         connector->connector_type_id =
212                 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
213         if (connector->connector_type_id < 0) {
214                 ret = connector->connector_type_id;
215                 goto out_put_id;
216         }
217         connector->name =
218                 kasprintf(GFP_KERNEL, "%s-%d",
219                           drm_connector_enum_list[connector_type].name,
220                           connector->connector_type_id);
221         if (!connector->name) {
222                 ret = -ENOMEM;
223                 goto out_put_type_id;
224         }
225
226         INIT_LIST_HEAD(&connector->probed_modes);
227         INIT_LIST_HEAD(&connector->modes);
228         connector->edid_blob_ptr = NULL;
229         connector->status = connector_status_unknown;
230
231         drm_connector_get_cmdline_mode(connector);
232
233         /* We should add connectors at the end to avoid upsetting the connector
234          * index too much. */
235         list_add_tail(&connector->head, &config->connector_list);
236         config->num_connector++;
237
238         if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
239                 drm_object_attach_property(&connector->base,
240                                               config->edid_property,
241                                               0);
242
243         drm_object_attach_property(&connector->base,
244                                       config->dpms_property, 0);
245
246         if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
247                 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
248         }
249
250         connector->debugfs_entry = NULL;
251 out_put_type_id:
252         if (ret)
253                 ida_simple_remove(connector_ida, connector->connector_type_id);
254 out_put_id:
255         if (ret)
256                 ida_simple_remove(&config->connector_ida, connector->index);
257 out_put:
258         if (ret)
259                 drm_mode_object_unregister(dev, &connector->base);
260
261 out_unlock:
262         drm_modeset_unlock_all(dev);
263
264         return ret;
265 }
266 EXPORT_SYMBOL(drm_connector_init);
267
268 /**
269  * drm_mode_connector_attach_encoder - attach a connector to an encoder
270  * @connector: connector to attach
271  * @encoder: encoder to attach @connector to
272  *
273  * This function links up a connector to an encoder. Note that the routing
274  * restrictions between encoders and crtcs are exposed to userspace through the
275  * possible_clones and possible_crtcs bitmasks.
276  *
277  * Returns:
278  * Zero on success, negative errno on failure.
279  */
280 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
281                                       struct drm_encoder *encoder)
282 {
283         int i;
284
285         /*
286          * In the past, drivers have attempted to model the static association
287          * of connector to encoder in simple connector/encoder devices using a
288          * direct assignment of connector->encoder = encoder. This connection
289          * is a logical one and the responsibility of the core, so drivers are
290          * expected not to mess with this.
291          *
292          * Note that the error return should've been enough here, but a large
293          * majority of drivers ignores the return value, so add in a big WARN
294          * to get people's attention.
295          */
296         if (WARN_ON(connector->encoder))
297                 return -EINVAL;
298
299         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
300                 if (connector->encoder_ids[i] == 0) {
301                         connector->encoder_ids[i] = encoder->base.id;
302                         return 0;
303                 }
304         }
305         return -ENOMEM;
306 }
307 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
308
309 static void drm_mode_remove(struct drm_connector *connector,
310                             struct drm_display_mode *mode)
311 {
312         list_del(&mode->head);
313         drm_mode_destroy(connector->dev, mode);
314 }
315
316 /**
317  * drm_connector_cleanup - cleans up an initialised connector
318  * @connector: connector to cleanup
319  *
320  * Cleans up the connector but doesn't free the object.
321  */
322 void drm_connector_cleanup(struct drm_connector *connector)
323 {
324         struct drm_device *dev = connector->dev;
325         struct drm_display_mode *mode, *t;
326
327         /* The connector should have been removed from userspace long before
328          * it is finally destroyed.
329          */
330         if (WARN_ON(connector->registered))
331                 drm_connector_unregister(connector);
332
333         if (connector->tile_group) {
334                 drm_mode_put_tile_group(dev, connector->tile_group);
335                 connector->tile_group = NULL;
336         }
337
338         list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
339                 drm_mode_remove(connector, mode);
340
341         list_for_each_entry_safe(mode, t, &connector->modes, head)
342                 drm_mode_remove(connector, mode);
343
344         ida_simple_remove(&drm_connector_enum_list[connector->connector_type].ida,
345                           connector->connector_type_id);
346
347         ida_simple_remove(&dev->mode_config.connector_ida,
348                           connector->index);
349
350         kfree(connector->display_info.bus_formats);
351         drm_mode_object_unregister(dev, &connector->base);
352         kfree(connector->name);
353         connector->name = NULL;
354         list_del(&connector->head);
355         dev->mode_config.num_connector--;
356
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 }
364 EXPORT_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  */
375 int 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
402 err_debugfs:
403         drm_debugfs_connector_remove(connector);
404 err_sysfs:
405         drm_sysfs_connector_remove(connector);
406         return ret;
407 }
408 EXPORT_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  */
416 void 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 }
429 EXPORT_SYMBOL(drm_connector_unregister);
430
431 void drm_connector_unregister_all(struct drm_device *dev)
432 {
433         struct drm_connector *connector;
434
435         /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
436         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
437                 drm_connector_unregister(connector);
438 }
439
440 int drm_connector_register_all(struct drm_device *dev)
441 {
442         struct drm_connector *connector;
443         int ret;
444
445         /* FIXME: taking the mode config mutex ends up in a clash with
446          * fbcon/backlight registration */
447         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
448                 ret = drm_connector_register(connector);
449                 if (ret)
450                         goto err;
451         }
452
453         return 0;
454
455 err:
456         mutex_unlock(&dev->mode_config.mutex);
457         drm_connector_unregister_all(dev);
458         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  */
468 const 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 }
477 EXPORT_SYMBOL(drm_get_connector_status_name);
478
479 static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
480         { SubPixelUnknown, "Unknown" },
481         { SubPixelHorizontalRGB, "Horizontal RGB" },
482         { SubPixelHorizontalBGR, "Horizontal BGR" },
483         { SubPixelVerticalRGB, "Vertical RGB" },
484         { SubPixelVerticalBGR, "Vertical BGR" },
485         { SubPixelNone, "None" },
486 };
487
488 /**
489  * drm_get_subpixel_order_name - return a string for a given subpixel enum
490  * @order: enum of subpixel_order
491  *
492  * Note you could abuse this and return something out of bounds, but that
493  * would be a caller error.  No unscrubbed user data should make it here.
494  */
495 const char *drm_get_subpixel_order_name(enum subpixel_order order)
496 {
497         return drm_subpixel_enum_list[order].name;
498 }
499 EXPORT_SYMBOL(drm_get_subpixel_order_name);
500
501 static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
502         { DRM_MODE_DPMS_ON, "On" },
503         { DRM_MODE_DPMS_STANDBY, "Standby" },
504         { DRM_MODE_DPMS_SUSPEND, "Suspend" },
505         { DRM_MODE_DPMS_OFF, "Off" }
506 };
507 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
508
509 /**
510  * drm_display_info_set_bus_formats - set the supported bus formats
511  * @info: display info to store bus formats in
512  * @formats: array containing the supported bus formats
513  * @num_formats: the number of entries in the fmts array
514  *
515  * Store the supported bus formats in display info structure.
516  * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
517  * a full list of available formats.
518  */
519 int drm_display_info_set_bus_formats(struct drm_display_info *info,
520                                      const u32 *formats,
521                                      unsigned int num_formats)
522 {
523         u32 *fmts = NULL;
524
525         if (!formats && num_formats)
526                 return -EINVAL;
527
528         if (formats && num_formats) {
529                 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
530                                GFP_KERNEL);
531                 if (!fmts)
532                         return -ENOMEM;
533         }
534
535         kfree(info->bus_formats);
536         info->bus_formats = fmts;
537         info->num_bus_formats = num_formats;
538
539         return 0;
540 }
541 EXPORT_SYMBOL(drm_display_info_set_bus_formats);
542
543 /* Optional connector properties. */
544 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
545         { DRM_MODE_SCALE_NONE, "None" },
546         { DRM_MODE_SCALE_FULLSCREEN, "Full" },
547         { DRM_MODE_SCALE_CENTER, "Center" },
548         { DRM_MODE_SCALE_ASPECT, "Full aspect" },
549 };
550
551 static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
552         { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
553         { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
554         { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
555 };
556
557 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
558         { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
559         { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
560         { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
561 };
562 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
563
564 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
565         { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
566         { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
567         { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
568 };
569 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
570                  drm_dvi_i_subconnector_enum_list)
571
572 static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
573         { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
574         { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
575         { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
576         { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
577         { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
578 };
579 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
580
581 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
582         { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
583         { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
584         { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
585         { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
586         { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
587 };
588 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
589                  drm_tv_subconnector_enum_list)
590
591 /**
592  * DOC: standard connector properties
593  *
594  * DRM connectors have a few standardized properties:
595  *
596  * EDID:
597  *      Blob property which contains the current EDID read from the sink. This
598  *      is useful to parse sink identification information like vendor, model
599  *      and serial. Drivers should update this property by calling
600  *      drm_mode_connector_update_edid_property(), usually after having parsed
601  *      the EDID using drm_add_edid_modes(). Userspace cannot change this
602  *      property.
603  * DPMS:
604  *      Legacy property for setting the power state of the connector. For atomic
605  *      drivers this is only provided for backwards compatibility with existing
606  *      drivers, it remaps to controlling the "ACTIVE" property on the CRTC the
607  *      connector is linked to. Drivers should never set this property directly,
608  *      it is handled by the DRM core by calling the ->dpms() callback in
609  *      &drm_connector_funcs. Atomic drivers should implement this hook using
610  *      drm_atomic_helper_connector_dpms(). This is the only property standard
611  *      connector property that userspace can change.
612  * PATH:
613  *      Connector path property to identify how this sink is physically
614  *      connected. Used by DP MST. This should be set by calling
615  *      drm_mode_connector_set_path_property(), in the case of DP MST with the
616  *      path property the MST manager created. Userspace cannot change this
617  *      property.
618  * TILE:
619  *      Connector tile group property to indicate how a set of DRM connector
620  *      compose together into one logical screen. This is used by both high-res
621  *      external screens (often only using a single cable, but exposing multiple
622  *      DP MST sinks), or high-res integrated panels (like dual-link DSI) which
623  *      are not gen-locked. Note that for tiled panels which are genlocked, like
624  *      dual-link LVDS or dual-link DSI, the driver should try to not expose the
625  *      tiling and virtualize both &drm_crtc and &drm_plane if needed. Drivers
626  *      should update this value using drm_mode_connector_set_tile_property().
627  *      Userspace cannot change this property.
628  *
629  * Connectors also have one standardized atomic property:
630  *
631  * CRTC_ID:
632  *      Mode object ID of the &drm_crtc this connector should be connected to.
633  */
634
635 int drm_connector_create_standard_properties(struct drm_device *dev)
636 {
637         struct drm_property *prop;
638
639         prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
640                                    DRM_MODE_PROP_IMMUTABLE,
641                                    "EDID", 0);
642         if (!prop)
643                 return -ENOMEM;
644         dev->mode_config.edid_property = prop;
645
646         prop = drm_property_create_enum(dev, 0,
647                                    "DPMS", drm_dpms_enum_list,
648                                    ARRAY_SIZE(drm_dpms_enum_list));
649         if (!prop)
650                 return -ENOMEM;
651         dev->mode_config.dpms_property = prop;
652
653         prop = drm_property_create(dev,
654                                    DRM_MODE_PROP_BLOB |
655                                    DRM_MODE_PROP_IMMUTABLE,
656                                    "PATH", 0);
657         if (!prop)
658                 return -ENOMEM;
659         dev->mode_config.path_property = prop;
660
661         prop = drm_property_create(dev,
662                                    DRM_MODE_PROP_BLOB |
663                                    DRM_MODE_PROP_IMMUTABLE,
664                                    "TILE", 0);
665         if (!prop)
666                 return -ENOMEM;
667         dev->mode_config.tile_property = prop;
668
669         return 0;
670 }
671
672 /**
673  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
674  * @dev: DRM device
675  *
676  * Called by a driver the first time a DVI-I connector is made.
677  */
678 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
679 {
680         struct drm_property *dvi_i_selector;
681         struct drm_property *dvi_i_subconnector;
682
683         if (dev->mode_config.dvi_i_select_subconnector_property)
684                 return 0;
685
686         dvi_i_selector =
687                 drm_property_create_enum(dev, 0,
688                                     "select subconnector",
689                                     drm_dvi_i_select_enum_list,
690                                     ARRAY_SIZE(drm_dvi_i_select_enum_list));
691         dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
692
693         dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
694                                     "subconnector",
695                                     drm_dvi_i_subconnector_enum_list,
696                                     ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
697         dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
698
699         return 0;
700 }
701 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
702
703 /**
704  * drm_create_tv_properties - create TV specific connector properties
705  * @dev: DRM device
706  * @num_modes: number of different TV formats (modes) supported
707  * @modes: array of pointers to strings containing name of each format
708  *
709  * Called by a driver's TV initialization routine, this function creates
710  * the TV specific connector properties for a given device.  Caller is
711  * responsible for allocating a list of format names and passing them to
712  * this routine.
713  */
714 int drm_mode_create_tv_properties(struct drm_device *dev,
715                                   unsigned int num_modes,
716                                   const char * const modes[])
717 {
718         struct drm_property *tv_selector;
719         struct drm_property *tv_subconnector;
720         unsigned int i;
721
722         if (dev->mode_config.tv_select_subconnector_property)
723                 return 0;
724
725         /*
726          * Basic connector properties
727          */
728         tv_selector = drm_property_create_enum(dev, 0,
729                                           "select subconnector",
730                                           drm_tv_select_enum_list,
731                                           ARRAY_SIZE(drm_tv_select_enum_list));
732         if (!tv_selector)
733                 goto nomem;
734
735         dev->mode_config.tv_select_subconnector_property = tv_selector;
736
737         tv_subconnector =
738                 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
739                                     "subconnector",
740                                     drm_tv_subconnector_enum_list,
741                                     ARRAY_SIZE(drm_tv_subconnector_enum_list));
742         if (!tv_subconnector)
743                 goto nomem;
744         dev->mode_config.tv_subconnector_property = tv_subconnector;
745
746         /*
747          * Other, TV specific properties: margins & TV modes.
748          */
749         dev->mode_config.tv_left_margin_property =
750                 drm_property_create_range(dev, 0, "left margin", 0, 100);
751         if (!dev->mode_config.tv_left_margin_property)
752                 goto nomem;
753
754         dev->mode_config.tv_right_margin_property =
755                 drm_property_create_range(dev, 0, "right margin", 0, 100);
756         if (!dev->mode_config.tv_right_margin_property)
757                 goto nomem;
758
759         dev->mode_config.tv_top_margin_property =
760                 drm_property_create_range(dev, 0, "top margin", 0, 100);
761         if (!dev->mode_config.tv_top_margin_property)
762                 goto nomem;
763
764         dev->mode_config.tv_bottom_margin_property =
765                 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
766         if (!dev->mode_config.tv_bottom_margin_property)
767                 goto nomem;
768
769         dev->mode_config.tv_mode_property =
770                 drm_property_create(dev, DRM_MODE_PROP_ENUM,
771                                     "mode", num_modes);
772         if (!dev->mode_config.tv_mode_property)
773                 goto nomem;
774
775         for (i = 0; i < num_modes; i++)
776                 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
777                                       i, modes[i]);
778
779         dev->mode_config.tv_brightness_property =
780                 drm_property_create_range(dev, 0, "brightness", 0, 100);
781         if (!dev->mode_config.tv_brightness_property)
782                 goto nomem;
783
784         dev->mode_config.tv_contrast_property =
785                 drm_property_create_range(dev, 0, "contrast", 0, 100);
786         if (!dev->mode_config.tv_contrast_property)
787                 goto nomem;
788
789         dev->mode_config.tv_flicker_reduction_property =
790                 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
791         if (!dev->mode_config.tv_flicker_reduction_property)
792                 goto nomem;
793
794         dev->mode_config.tv_overscan_property =
795                 drm_property_create_range(dev, 0, "overscan", 0, 100);
796         if (!dev->mode_config.tv_overscan_property)
797                 goto nomem;
798
799         dev->mode_config.tv_saturation_property =
800                 drm_property_create_range(dev, 0, "saturation", 0, 100);
801         if (!dev->mode_config.tv_saturation_property)
802                 goto nomem;
803
804         dev->mode_config.tv_hue_property =
805                 drm_property_create_range(dev, 0, "hue", 0, 100);
806         if (!dev->mode_config.tv_hue_property)
807                 goto nomem;
808
809         return 0;
810 nomem:
811         return -ENOMEM;
812 }
813 EXPORT_SYMBOL(drm_mode_create_tv_properties);
814
815 /**
816  * drm_mode_create_scaling_mode_property - create scaling mode property
817  * @dev: DRM device
818  *
819  * Called by a driver the first time it's needed, must be attached to desired
820  * connectors.
821  */
822 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
823 {
824         struct drm_property *scaling_mode;
825
826         if (dev->mode_config.scaling_mode_property)
827                 return 0;
828
829         scaling_mode =
830                 drm_property_create_enum(dev, 0, "scaling mode",
831                                 drm_scaling_mode_enum_list,
832                                     ARRAY_SIZE(drm_scaling_mode_enum_list));
833
834         dev->mode_config.scaling_mode_property = scaling_mode;
835
836         return 0;
837 }
838 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
839
840 /**
841  * drm_mode_create_aspect_ratio_property - create aspect ratio property
842  * @dev: DRM device
843  *
844  * Called by a driver the first time it's needed, must be attached to desired
845  * connectors.
846  *
847  * Returns:
848  * Zero on success, negative errno on failure.
849  */
850 int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
851 {
852         if (dev->mode_config.aspect_ratio_property)
853                 return 0;
854
855         dev->mode_config.aspect_ratio_property =
856                 drm_property_create_enum(dev, 0, "aspect ratio",
857                                 drm_aspect_ratio_enum_list,
858                                 ARRAY_SIZE(drm_aspect_ratio_enum_list));
859
860         if (dev->mode_config.aspect_ratio_property == NULL)
861                 return -ENOMEM;
862
863         return 0;
864 }
865 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
866
867 /**
868  * drm_mode_create_suggested_offset_properties - create suggests offset properties
869  * @dev: DRM device
870  *
871  * Create the the suggested x/y offset property for connectors.
872  */
873 int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
874 {
875         if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
876                 return 0;
877
878         dev->mode_config.suggested_x_property =
879                 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
880
881         dev->mode_config.suggested_y_property =
882                 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
883
884         if (dev->mode_config.suggested_x_property == NULL ||
885             dev->mode_config.suggested_y_property == NULL)
886                 return -ENOMEM;
887         return 0;
888 }
889 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
890
891 /**
892  * drm_mode_connector_set_path_property - set tile property on connector
893  * @connector: connector to set property on.
894  * @path: path to use for property; must not be NULL.
895  *
896  * This creates a property to expose to userspace to specify a
897  * connector path. This is mainly used for DisplayPort MST where
898  * connectors have a topology and we want to allow userspace to give
899  * them more meaningful names.
900  *
901  * Returns:
902  * Zero on success, negative errno on failure.
903  */
904 int drm_mode_connector_set_path_property(struct drm_connector *connector,
905                                          const char *path)
906 {
907         struct drm_device *dev = connector->dev;
908         int ret;
909
910         ret = drm_property_replace_global_blob(dev,
911                                                &connector->path_blob_ptr,
912                                                strlen(path) + 1,
913                                                path,
914                                                &connector->base,
915                                                dev->mode_config.path_property);
916         return ret;
917 }
918 EXPORT_SYMBOL(drm_mode_connector_set_path_property);
919
920 /**
921  * drm_mode_connector_set_tile_property - set tile property on connector
922  * @connector: connector to set property on.
923  *
924  * This looks up the tile information for a connector, and creates a
925  * property for userspace to parse if it exists. The property is of
926  * the form of 8 integers using ':' as a separator.
927  *
928  * Returns:
929  * Zero on success, errno on failure.
930  */
931 int drm_mode_connector_set_tile_property(struct drm_connector *connector)
932 {
933         struct drm_device *dev = connector->dev;
934         char tile[256];
935         int ret;
936
937         if (!connector->has_tile) {
938                 ret  = drm_property_replace_global_blob(dev,
939                                                         &connector->tile_blob_ptr,
940                                                         0,
941                                                         NULL,
942                                                         &connector->base,
943                                                         dev->mode_config.tile_property);
944                 return ret;
945         }
946
947         snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
948                  connector->tile_group->id, connector->tile_is_single_monitor,
949                  connector->num_h_tile, connector->num_v_tile,
950                  connector->tile_h_loc, connector->tile_v_loc,
951                  connector->tile_h_size, connector->tile_v_size);
952
953         ret = drm_property_replace_global_blob(dev,
954                                                &connector->tile_blob_ptr,
955                                                strlen(tile) + 1,
956                                                tile,
957                                                &connector->base,
958                                                dev->mode_config.tile_property);
959         return ret;
960 }
961 EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
962
963 /**
964  * drm_mode_connector_update_edid_property - update the edid property of a connector
965  * @connector: drm connector
966  * @edid: new value of the edid property
967  *
968  * This function creates a new blob modeset object and assigns its id to the
969  * connector's edid property.
970  *
971  * Returns:
972  * Zero on success, negative errno on failure.
973  */
974 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
975                                             const struct edid *edid)
976 {
977         struct drm_device *dev = connector->dev;
978         size_t size = 0;
979         int ret;
980
981         /* ignore requests to set edid when overridden */
982         if (connector->override_edid)
983                 return 0;
984
985         if (edid)
986                 size = EDID_LENGTH * (1 + edid->extensions);
987
988         ret = drm_property_replace_global_blob(dev,
989                                                &connector->edid_blob_ptr,
990                                                size,
991                                                edid,
992                                                &connector->base,
993                                                dev->mode_config.edid_property);
994         return ret;
995 }
996 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
997
998 int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
999                                     struct drm_property *property,
1000                                     uint64_t value)
1001 {
1002         int ret = -EINVAL;
1003         struct drm_connector *connector = obj_to_connector(obj);
1004
1005         /* Do DPMS ourselves */
1006         if (property == connector->dev->mode_config.dpms_property) {
1007                 ret = (*connector->funcs->dpms)(connector, (int)value);
1008         } else if (connector->funcs->set_property)
1009                 ret = connector->funcs->set_property(connector, property, value);
1010
1011         /* store the property value if successful */
1012         if (!ret)
1013                 drm_object_property_set_value(&connector->base, property, value);
1014         return ret;
1015 }
1016
1017 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
1018                                        void *data, struct drm_file *file_priv)
1019 {
1020         struct drm_mode_connector_set_property *conn_set_prop = data;
1021         struct drm_mode_obj_set_property obj_set_prop = {
1022                 .value = conn_set_prop->value,
1023                 .prop_id = conn_set_prop->prop_id,
1024                 .obj_id = conn_set_prop->connector_id,
1025                 .obj_type = DRM_MODE_OBJECT_CONNECTOR
1026         };
1027
1028         /* It does all the locking and checking we need */
1029         return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
1030 }
1031
1032 static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
1033 {
1034         /* For atomic drivers only state objects are synchronously updated and
1035          * protected by modeset locks, so check those first. */
1036         if (connector->state)
1037                 return connector->state->best_encoder;
1038         return connector->encoder;
1039 }
1040
1041 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1042                                          const struct drm_file *file_priv)
1043 {
1044         /*
1045          * If user-space hasn't configured the driver to expose the stereo 3D
1046          * modes, don't expose them.
1047          */
1048         if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1049                 return false;
1050
1051         return true;
1052 }
1053
1054 int drm_mode_getconnector(struct drm_device *dev, void *data,
1055                           struct drm_file *file_priv)
1056 {
1057         struct drm_mode_get_connector *out_resp = data;
1058         struct drm_connector *connector;
1059         struct drm_encoder *encoder;
1060         struct drm_display_mode *mode;
1061         int mode_count = 0;
1062         int encoders_count = 0;
1063         int ret = 0;
1064         int copied = 0;
1065         int i;
1066         struct drm_mode_modeinfo u_mode;
1067         struct drm_mode_modeinfo __user *mode_ptr;
1068         uint32_t __user *encoder_ptr;
1069
1070         if (!drm_core_check_feature(dev, DRIVER_MODESET))
1071                 return -EINVAL;
1072
1073         memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1074
1075         mutex_lock(&dev->mode_config.mutex);
1076
1077         connector = drm_connector_lookup(dev, out_resp->connector_id);
1078         if (!connector) {
1079                 ret = -ENOENT;
1080                 goto out_unlock;
1081         }
1082
1083         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
1084                 if (connector->encoder_ids[i] != 0)
1085                         encoders_count++;
1086
1087         if (out_resp->count_modes == 0) {
1088                 connector->funcs->fill_modes(connector,
1089                                              dev->mode_config.max_width,
1090                                              dev->mode_config.max_height);
1091         }
1092
1093         /* delayed so we get modes regardless of pre-fill_modes state */
1094         list_for_each_entry(mode, &connector->modes, head)
1095                 if (drm_mode_expose_to_userspace(mode, file_priv))
1096                         mode_count++;
1097
1098         out_resp->connector_id = connector->base.id;
1099         out_resp->connector_type = connector->connector_type;
1100         out_resp->connector_type_id = connector->connector_type_id;
1101         out_resp->mm_width = connector->display_info.width_mm;
1102         out_resp->mm_height = connector->display_info.height_mm;
1103         out_resp->subpixel = connector->display_info.subpixel_order;
1104         out_resp->connection = connector->status;
1105
1106         drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1107         encoder = drm_connector_get_encoder(connector);
1108         if (encoder)
1109                 out_resp->encoder_id = encoder->base.id;
1110         else
1111                 out_resp->encoder_id = 0;
1112
1113         /*
1114          * This ioctl is called twice, once to determine how much space is
1115          * needed, and the 2nd time to fill it.
1116          */
1117         if ((out_resp->count_modes >= mode_count) && mode_count) {
1118                 copied = 0;
1119                 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1120                 list_for_each_entry(mode, &connector->modes, head) {
1121                         if (!drm_mode_expose_to_userspace(mode, file_priv))
1122                                 continue;
1123
1124                         drm_mode_convert_to_umode(&u_mode, mode);
1125                         if (copy_to_user(mode_ptr + copied,
1126                                          &u_mode, sizeof(u_mode))) {
1127                                 ret = -EFAULT;
1128                                 goto out;
1129                         }
1130                         copied++;
1131                 }
1132         }
1133         out_resp->count_modes = mode_count;
1134
1135         ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
1136                         (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
1137                         (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
1138                         &out_resp->count_props);
1139         if (ret)
1140                 goto out;
1141
1142         if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1143                 copied = 0;
1144                 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1145                 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1146                         if (connector->encoder_ids[i] != 0) {
1147                                 if (put_user(connector->encoder_ids[i],
1148                                              encoder_ptr + copied)) {
1149                                         ret = -EFAULT;
1150                                         goto out;
1151                                 }
1152                                 copied++;
1153                         }
1154                 }
1155         }
1156         out_resp->count_encoders = encoders_count;
1157
1158 out:
1159         drm_modeset_unlock(&dev->mode_config.connection_mutex);
1160
1161         drm_connector_unreference(connector);
1162 out_unlock:
1163         mutex_unlock(&dev->mode_config.mutex);
1164
1165         return ret;
1166 }
1167
1168
1169 /**
1170  * DOC: Tile group
1171  *
1172  * Tile groups are used to represent tiled monitors with a unique integer
1173  * identifier. Tiled monitors using DisplayID v1.3 have a unique 8-byte handle,
1174  * we store this in a tile group, so we have a common identifier for all tiles
1175  * in a monitor group. The property is called "TILE". Drivers can manage tile
1176  * groups using drm_mode_create_tile_group(), drm_mode_put_tile_group() and
1177  * drm_mode_get_tile_group(). But this is only needed for internal panels where
1178  * the tile group information is exposed through a non-standard way.
1179  */
1180
1181 static void drm_tile_group_free(struct kref *kref)
1182 {
1183         struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
1184         struct drm_device *dev = tg->dev;
1185         mutex_lock(&dev->mode_config.idr_mutex);
1186         idr_remove(&dev->mode_config.tile_idr, tg->id);
1187         mutex_unlock(&dev->mode_config.idr_mutex);
1188         kfree(tg);
1189 }
1190
1191 /**
1192  * drm_mode_put_tile_group - drop a reference to a tile group.
1193  * @dev: DRM device
1194  * @tg: tile group to drop reference to.
1195  *
1196  * drop reference to tile group and free if 0.
1197  */
1198 void drm_mode_put_tile_group(struct drm_device *dev,
1199                              struct drm_tile_group *tg)
1200 {
1201         kref_put(&tg->refcount, drm_tile_group_free);
1202 }
1203 EXPORT_SYMBOL(drm_mode_put_tile_group);
1204
1205 /**
1206  * drm_mode_get_tile_group - get a reference to an existing tile group
1207  * @dev: DRM device
1208  * @topology: 8-bytes unique per monitor.
1209  *
1210  * Use the unique bytes to get a reference to an existing tile group.
1211  *
1212  * RETURNS:
1213  * tile group or NULL if not found.
1214  */
1215 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
1216                                                char topology[8])
1217 {
1218         struct drm_tile_group *tg;
1219         int id;
1220         mutex_lock(&dev->mode_config.idr_mutex);
1221         idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
1222                 if (!memcmp(tg->group_data, topology, 8)) {
1223                         if (!kref_get_unless_zero(&tg->refcount))
1224                                 tg = NULL;
1225                         mutex_unlock(&dev->mode_config.idr_mutex);
1226                         return tg;
1227                 }
1228         }
1229         mutex_unlock(&dev->mode_config.idr_mutex);
1230         return NULL;
1231 }
1232 EXPORT_SYMBOL(drm_mode_get_tile_group);
1233
1234 /**
1235  * drm_mode_create_tile_group - create a tile group from a displayid description
1236  * @dev: DRM device
1237  * @topology: 8-bytes unique per monitor.
1238  *
1239  * Create a tile group for the unique monitor, and get a unique
1240  * identifier for the tile group.
1241  *
1242  * RETURNS:
1243  * new tile group or error.
1244  */
1245 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
1246                                                   char topology[8])
1247 {
1248         struct drm_tile_group *tg;
1249         int ret;
1250
1251         tg = kzalloc(sizeof(*tg), GFP_KERNEL);
1252         if (!tg)
1253                 return ERR_PTR(-ENOMEM);
1254
1255         kref_init(&tg->refcount);
1256         memcpy(tg->group_data, topology, 8);
1257         tg->dev = dev;
1258
1259         mutex_lock(&dev->mode_config.idr_mutex);
1260         ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
1261         if (ret >= 0) {
1262                 tg->id = ret;
1263         } else {
1264                 kfree(tg);
1265                 tg = ERR_PTR(ret);
1266         }
1267
1268         mutex_unlock(&dev->mode_config.idr_mutex);
1269         return tg;
1270 }
1271 EXPORT_SYMBOL(drm_mode_create_tile_group);