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 <linux/export.h>
24
25 #include <drm/drm_device.h>
26 #include <drm/drm_drv.h>
27 #include <drm/drm_encoder.h>
28
29 #include "drm_crtc_internal.h"
30
31 /**
32 * DOC: overview
33 *
34 * Encoders represent the connecting element between the CRTC (as the overall
35 * pixel pipeline, represented by &struct drm_crtc) and the connectors (as the
36 * generic sink entity, represented by &struct drm_connector). An encoder takes
37 * pixel data from a CRTC and converts it to a format suitable for any attached
38 * connector. Encoders are objects exposed to userspace, originally to allow
39 * userspace to infer cloning and connector/CRTC restrictions. Unfortunately
40 * almost all drivers get this wrong, making the uabi pretty much useless. On
41 * top of that the exposed restrictions are too simple for today's hardware, and
42 * the recommended way to infer restrictions is by using the
43 * DRM_MODE_ATOMIC_TEST_ONLY flag for the atomic IOCTL.
44 *
45 * Otherwise encoders aren't used in the uapi at all (any modeset request from
46 * userspace directly connects a connector with a CRTC), drivers are therefore
47 * free to use them however they wish. Modeset helper libraries make strong use
48 * of encoders to facilitate code sharing. But for more complex settings it is
49 * usually better to move shared code into a separate &drm_bridge. Compared to
50 * encoders, bridges also have the benefit of being purely an internal
51 * abstraction since they are not exposed to userspace at all.
52 *
53 * Encoders are initialized with drm_encoder_init() and cleaned up using
54 * drm_encoder_cleanup().
55 */
56 static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
57 { DRM_MODE_ENCODER_NONE, "None" },
58 { DRM_MODE_ENCODER_DAC, "DAC" },
59 { DRM_MODE_ENCODER_TMDS, "TMDS" },
60 { DRM_MODE_ENCODER_LVDS, "LVDS" },
61 { DRM_MODE_ENCODER_TVDAC, "TV" },
62 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
63 { DRM_MODE_ENCODER_DSI, "DSI" },
64 { DRM_MODE_ENCODER_DPMST, "DP MST" },
65 { DRM_MODE_ENCODER_DPI, "DPI" },
66 };
67
drm_encoder_register_all(struct drm_device * dev)68 int drm_encoder_register_all(struct drm_device *dev)
69 {
70 struct drm_encoder *encoder;
71 int ret = 0;
72
73 drm_for_each_encoder(encoder, dev) {
74 if (encoder->funcs->late_register)
75 ret = encoder->funcs->late_register(encoder);
76 if (ret)
77 return ret;
78 }
79
80 return 0;
81 }
82
drm_encoder_unregister_all(struct drm_device * dev)83 void drm_encoder_unregister_all(struct drm_device *dev)
84 {
85 struct drm_encoder *encoder;
86
87 drm_for_each_encoder(encoder, dev) {
88 if (encoder->funcs->early_unregister)
89 encoder->funcs->early_unregister(encoder);
90 }
91 }
92
93 /**
94 * drm_encoder_init - Init a preallocated encoder
95 * @dev: drm device
96 * @encoder: the encoder to init
97 * @funcs: callbacks for this encoder
98 * @encoder_type: user visible type of the encoder
99 * @name: printf style format string for the encoder name, or NULL for default name
100 *
101 * Initialises a preallocated encoder. Encoder should be subclassed as part of
102 * driver encoder objects. At driver unload time drm_encoder_cleanup() should be
103 * called from the driver's &drm_encoder_funcs.destroy hook.
104 *
105 * Returns:
106 * Zero on success, error code on failure.
107 */
drm_encoder_init(struct drm_device * dev,struct drm_encoder * encoder,const struct drm_encoder_funcs * funcs,int encoder_type,const char * name,...)108 int drm_encoder_init(struct drm_device *dev,
109 struct drm_encoder *encoder,
110 const struct drm_encoder_funcs *funcs,
111 int encoder_type, const char *name, ...)
112 {
113 int ret;
114
115 /* encoder index is used with 32bit bitmasks */
116 if (WARN_ON(dev->mode_config.num_encoder >= 32))
117 return -EINVAL;
118
119 ret = drm_mode_object_add(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
120 if (ret)
121 return ret;
122
123 encoder->dev = dev;
124 encoder->encoder_type = encoder_type;
125 encoder->funcs = funcs;
126 if (name) {
127 va_list ap;
128
129 va_start(ap, name);
130 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
131 va_end(ap);
132 } else {
133 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
134 drm_encoder_enum_list[encoder_type].name,
135 encoder->base.id);
136 }
137 if (!encoder->name) {
138 ret = -ENOMEM;
139 goto out_put;
140 }
141
142 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
143 encoder->index = dev->mode_config.num_encoder++;
144
145 out_put:
146 if (ret)
147 drm_mode_object_unregister(dev, &encoder->base);
148
149 return ret;
150 }
151 EXPORT_SYMBOL(drm_encoder_init);
152
153 /**
154 * drm_encoder_cleanup - cleans up an initialised encoder
155 * @encoder: encoder to cleanup
156 *
157 * Cleans up the encoder but doesn't free the object.
158 */
drm_encoder_cleanup(struct drm_encoder * encoder)159 void drm_encoder_cleanup(struct drm_encoder *encoder)
160 {
161 struct drm_device *dev = encoder->dev;
162
163 /* Note that the encoder_list is considered to be static; should we
164 * remove the drm_encoder at runtime we would have to decrement all
165 * the indices on the drm_encoder after us in the encoder_list.
166 */
167
168 if (encoder->bridge) {
169 struct drm_bridge *bridge = encoder->bridge;
170 struct drm_bridge *next;
171
172 while (bridge) {
173 next = bridge->next;
174 drm_bridge_detach(bridge);
175 bridge = next;
176 }
177 }
178
179 drm_mode_object_unregister(dev, &encoder->base);
180 kfree(encoder->name);
181 list_del(&encoder->head);
182 dev->mode_config.num_encoder--;
183
184 memset(encoder, 0, sizeof(*encoder));
185 }
186 EXPORT_SYMBOL(drm_encoder_cleanup);
187
drm_encoder_get_crtc(struct drm_encoder * encoder)188 static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
189 {
190 struct drm_connector *connector;
191 struct drm_device *dev = encoder->dev;
192 bool uses_atomic = false;
193 struct drm_connector_list_iter conn_iter;
194
195 /* For atomic drivers only state objects are synchronously updated and
196 * protected by modeset locks, so check those first. */
197 drm_connector_list_iter_begin(dev, &conn_iter);
198 drm_for_each_connector_iter(connector, &conn_iter) {
199 if (!connector->state)
200 continue;
201
202 uses_atomic = true;
203
204 if (connector->state->best_encoder != encoder)
205 continue;
206
207 drm_connector_list_iter_end(&conn_iter);
208 return connector->state->crtc;
209 }
210 drm_connector_list_iter_end(&conn_iter);
211
212 /* Don't return stale data (e.g. pending async disable). */
213 if (uses_atomic)
214 return NULL;
215
216 return encoder->crtc;
217 }
218
drm_mode_getencoder(struct drm_device * dev,void * data,struct drm_file * file_priv)219 int drm_mode_getencoder(struct drm_device *dev, void *data,
220 struct drm_file *file_priv)
221 {
222 struct drm_mode_get_encoder *enc_resp = data;
223 struct drm_encoder *encoder;
224 struct drm_crtc *crtc;
225
226 if (!drm_core_check_feature(dev, DRIVER_MODESET))
227 return -EOPNOTSUPP;
228
229 encoder = drm_encoder_find(dev, file_priv, enc_resp->encoder_id);
230 if (!encoder)
231 return -ENOENT;
232
233 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
234 crtc = drm_encoder_get_crtc(encoder);
235 if (crtc && drm_lease_held(file_priv, crtc->base.id))
236 enc_resp->crtc_id = crtc->base.id;
237 else
238 enc_resp->crtc_id = 0;
239 drm_modeset_unlock(&dev->mode_config.connection_mutex);
240
241 enc_resp->encoder_type = encoder->encoder_type;
242 enc_resp->encoder_id = encoder->base.id;
243 enc_resp->possible_crtcs = drm_lease_filter_crtcs(file_priv,
244 encoder->possible_crtcs);
245 enc_resp->possible_clones = encoder->possible_clones;
246
247 return 0;
248 }
249