blob: e555281f43d4b4daf5919e61ccf1549fca90f1d6 [file] [log] [blame]
Daniel Vetter321a95a2016-08-29 10:27:49 +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 <linux/export.h>
Sam Ravnborg0500c042019-05-26 19:35:35 +020024
Boris Brezillonee68c742019-08-26 17:26:29 +020025#include <drm/drm_bridge.h>
Sam Ravnborg0500c042019-05-26 19:35:35 +020026#include <drm/drm_device.h>
27#include <drm/drm_drv.h>
Daniel Vetter321a95a2016-08-29 10:27:49 +020028#include <drm/drm_encoder.h>
29
30#include "drm_crtc_internal.h"
31
Daniel Vettere03e6de2016-08-29 10:27:50 +020032/**
33 * DOC: overview
34 *
35 * Encoders represent the connecting element between the CRTC (as the overall
Daniel Vetterea0dd852016-12-29 21:48:26 +010036 * pixel pipeline, represented by &struct drm_crtc) and the connectors (as the
37 * generic sink entity, represented by &struct drm_connector). An encoder takes
Dhinakaran Pandiyanf15e6bb2016-09-19 15:40:48 -070038 * pixel data from a CRTC and converts it to a format suitable for any attached
39 * connector. Encoders are objects exposed to userspace, originally to allow
40 * userspace to infer cloning and connector/CRTC restrictions. Unfortunately
41 * almost all drivers get this wrong, making the uabi pretty much useless. On
42 * top of that the exposed restrictions are too simple for today's hardware, and
43 * the recommended way to infer restrictions is by using the
44 * DRM_MODE_ATOMIC_TEST_ONLY flag for the atomic IOCTL.
Daniel Vettere03e6de2016-08-29 10:27:50 +020045 *
46 * Otherwise encoders aren't used in the uapi at all (any modeset request from
47 * userspace directly connects a connector with a CRTC), drivers are therefore
48 * free to use them however they wish. Modeset helper libraries make strong use
49 * of encoders to facilitate code sharing. But for more complex settings it is
50 * usually better to move shared code into a separate &drm_bridge. Compared to
Dhinakaran Pandiyanf15e6bb2016-09-19 15:40:48 -070051 * encoders, bridges also have the benefit of being purely an internal
Daniel Vettere03e6de2016-08-29 10:27:50 +020052 * abstraction since they are not exposed to userspace at all.
53 *
54 * Encoders are initialized with drm_encoder_init() and cleaned up using
55 * drm_encoder_cleanup().
56 */
Daniel Vetter321a95a2016-08-29 10:27:49 +020057static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
58 { DRM_MODE_ENCODER_NONE, "None" },
59 { DRM_MODE_ENCODER_DAC, "DAC" },
60 { DRM_MODE_ENCODER_TMDS, "TMDS" },
61 { DRM_MODE_ENCODER_LVDS, "LVDS" },
62 { DRM_MODE_ENCODER_TVDAC, "TV" },
63 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
64 { DRM_MODE_ENCODER_DSI, "DSI" },
65 { DRM_MODE_ENCODER_DPMST, "DP MST" },
66 { DRM_MODE_ENCODER_DPI, "DPI" },
67};
68
69int drm_encoder_register_all(struct drm_device *dev)
70{
71 struct drm_encoder *encoder;
72 int ret = 0;
73
74 drm_for_each_encoder(encoder, dev) {
75 if (encoder->funcs->late_register)
76 ret = encoder->funcs->late_register(encoder);
77 if (ret)
78 return ret;
79 }
80
81 return 0;
82}
83
84void drm_encoder_unregister_all(struct drm_device *dev)
85{
86 struct drm_encoder *encoder;
87
88 drm_for_each_encoder(encoder, dev) {
89 if (encoder->funcs->early_unregister)
90 encoder->funcs->early_unregister(encoder);
91 }
92}
93
94/**
95 * drm_encoder_init - Init a preallocated encoder
96 * @dev: drm device
97 * @encoder: the encoder to init
98 * @funcs: callbacks for this encoder
99 * @encoder_type: user visible type of the encoder
100 * @name: printf style format string for the encoder name, or NULL for default name
101 *
Daniel Vettere03e6de2016-08-29 10:27:50 +0200102 * Initialises a preallocated encoder. Encoder should be subclassed as part of
103 * driver encoder objects. At driver unload time drm_encoder_cleanup() should be
Daniel Vetterd5745282017-01-25 07:26:45 +0100104 * called from the driver's &drm_encoder_funcs.destroy hook.
Daniel Vetter321a95a2016-08-29 10:27:49 +0200105 *
106 * Returns:
107 * Zero on success, error code on failure.
108 */
109int drm_encoder_init(struct drm_device *dev,
Daniel Vettere03e6de2016-08-29 10:27:50 +0200110 struct drm_encoder *encoder,
111 const struct drm_encoder_funcs *funcs,
112 int encoder_type, const char *name, ...)
Daniel Vetter321a95a2016-08-29 10:27:49 +0200113{
114 int ret;
115
Ville Syrjälä2a8d3ea2018-01-25 15:30:20 +0200116 /* encoder index is used with 32bit bitmasks */
117 if (WARN_ON(dev->mode_config.num_encoder >= 32))
118 return -EINVAL;
119
Thierry Reding2135ea72017-02-28 15:46:37 +0100120 ret = drm_mode_object_add(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
Daniel Vetter321a95a2016-08-29 10:27:49 +0200121 if (ret)
Daniel Vetter661a3752016-11-29 10:45:38 +0100122 return ret;
Daniel Vetter321a95a2016-08-29 10:27:49 +0200123
124 encoder->dev = dev;
125 encoder->encoder_type = encoder_type;
126 encoder->funcs = funcs;
127 if (name) {
128 va_list ap;
129
130 va_start(ap, name);
131 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
132 va_end(ap);
133 } else {
134 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
135 drm_encoder_enum_list[encoder_type].name,
136 encoder->base.id);
137 }
138 if (!encoder->name) {
139 ret = -ENOMEM;
140 goto out_put;
141 }
142
Boris Brezillon05193dc2019-12-03 15:15:08 +0100143 INIT_LIST_HEAD(&encoder->bridge_chain);
Daniel Vetter321a95a2016-08-29 10:27:49 +0200144 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
145 encoder->index = dev->mode_config.num_encoder++;
146
147out_put:
148 if (ret)
149 drm_mode_object_unregister(dev, &encoder->base);
150
Daniel Vetter321a95a2016-08-29 10:27:49 +0200151 return ret;
152}
153EXPORT_SYMBOL(drm_encoder_init);
154
155/**
156 * drm_encoder_cleanup - cleans up an initialised encoder
157 * @encoder: encoder to cleanup
158 *
159 * Cleans up the encoder but doesn't free the object.
160 */
161void drm_encoder_cleanup(struct drm_encoder *encoder)
162{
163 struct drm_device *dev = encoder->dev;
Boris Brezillon05193dc2019-12-03 15:15:08 +0100164 struct drm_bridge *bridge, *next;
Daniel Vetter321a95a2016-08-29 10:27:49 +0200165
166 /* Note that the encoder_list is considered to be static; should we
167 * remove the drm_encoder at runtime we would have to decrement all
168 * the indices on the drm_encoder after us in the encoder_list.
169 */
170
Boris Brezillon05193dc2019-12-03 15:15:08 +0100171 list_for_each_entry_safe(bridge, next, &encoder->bridge_chain,
172 chain_node)
173 drm_bridge_detach(bridge);
Laurent Pinchart4a878c02016-11-28 18:32:05 +0200174
Daniel Vetter321a95a2016-08-29 10:27:49 +0200175 drm_mode_object_unregister(dev, &encoder->base);
176 kfree(encoder->name);
177 list_del(&encoder->head);
178 dev->mode_config.num_encoder--;
Daniel Vetter321a95a2016-08-29 10:27:49 +0200179
180 memset(encoder, 0, sizeof(*encoder));
181}
182EXPORT_SYMBOL(drm_encoder_cleanup);
183
184static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
185{
186 struct drm_connector *connector;
187 struct drm_device *dev = encoder->dev;
188 bool uses_atomic = false;
Daniel Vetter613051d2016-12-14 00:08:06 +0100189 struct drm_connector_list_iter conn_iter;
Daniel Vetter321a95a2016-08-29 10:27:49 +0200190
191 /* For atomic drivers only state objects are synchronously updated and
192 * protected by modeset locks, so check those first. */
Thierry Redingb982dab2017-02-28 15:46:43 +0100193 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetter613051d2016-12-14 00:08:06 +0100194 drm_for_each_connector_iter(connector, &conn_iter) {
Daniel Vetter321a95a2016-08-29 10:27:49 +0200195 if (!connector->state)
196 continue;
197
198 uses_atomic = true;
199
200 if (connector->state->best_encoder != encoder)
201 continue;
202
Thierry Redingb982dab2017-02-28 15:46:43 +0100203 drm_connector_list_iter_end(&conn_iter);
Daniel Vetter321a95a2016-08-29 10:27:49 +0200204 return connector->state->crtc;
205 }
Thierry Redingb982dab2017-02-28 15:46:43 +0100206 drm_connector_list_iter_end(&conn_iter);
Daniel Vetter321a95a2016-08-29 10:27:49 +0200207
208 /* Don't return stale data (e.g. pending async disable). */
209 if (uses_atomic)
210 return NULL;
211
212 return encoder->crtc;
213}
214
Daniel Vetter321a95a2016-08-29 10:27:49 +0200215int drm_mode_getencoder(struct drm_device *dev, void *data,
216 struct drm_file *file_priv)
217{
218 struct drm_mode_get_encoder *enc_resp = data;
219 struct drm_encoder *encoder;
220 struct drm_crtc *crtc;
221
222 if (!drm_core_check_feature(dev, DRIVER_MODESET))
Chris Wilson69fdf422018-09-13 20:20:50 +0100223 return -EOPNOTSUPP;
Daniel Vetter321a95a2016-08-29 10:27:49 +0200224
Keith Packard418da172017-03-14 23:25:07 -0700225 encoder = drm_encoder_find(dev, file_priv, enc_resp->encoder_id);
Daniel Vetter321a95a2016-08-29 10:27:49 +0200226 if (!encoder)
227 return -ENOENT;
228
229 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
230 crtc = drm_encoder_get_crtc(encoder);
Keith Packard7de440d2017-04-09 22:35:34 -0600231 if (crtc && drm_lease_held(file_priv, crtc->base.id))
Daniel Vetter321a95a2016-08-29 10:27:49 +0200232 enc_resp->crtc_id = crtc->base.id;
233 else
234 enc_resp->crtc_id = 0;
235 drm_modeset_unlock(&dev->mode_config.connection_mutex);
236
237 enc_resp->encoder_type = encoder->encoder_type;
238 enc_resp->encoder_id = encoder->base.id;
Keith Packard7de440d2017-04-09 22:35:34 -0600239 enc_resp->possible_crtcs = drm_lease_filter_crtcs(file_priv,
240 encoder->possible_crtcs);
Daniel Vetter321a95a2016-08-29 10:27:49 +0200241 enc_resp->possible_clones = encoder->possible_clones;
242
243 return 0;
244}