| 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_bridge.h> | 
|---|
| 26 | #include <drm/drm_device.h> | 
|---|
| 27 | #include <drm/drm_drv.h> | 
|---|
| 28 | #include <drm/drm_encoder.h> | 
|---|
| 29 | #include <drm/drm_managed.h> | 
|---|
| 30 | #include <drm/drm_print.h> | 
|---|
| 31 |  | 
|---|
| 32 | #include "drm_crtc_internal.h" | 
|---|
| 33 | #include "drm_internal.h" | 
|---|
| 34 |  | 
|---|
| 35 | /** | 
|---|
| 36 | * DOC: overview | 
|---|
| 37 | * | 
|---|
| 38 | * Encoders represent the connecting element between the CRTC (as the overall | 
|---|
| 39 | * pixel pipeline, represented by &struct drm_crtc) and the connectors (as the | 
|---|
| 40 | * generic sink entity, represented by &struct drm_connector). An encoder takes | 
|---|
| 41 | * pixel data from a CRTC and converts it to a format suitable for any attached | 
|---|
| 42 | * connector. Encoders are objects exposed to userspace, originally to allow | 
|---|
| 43 | * userspace to infer cloning and connector/CRTC restrictions. Unfortunately | 
|---|
| 44 | * almost all drivers get this wrong, making the uabi pretty much useless. On | 
|---|
| 45 | * top of that the exposed restrictions are too simple for today's hardware, and | 
|---|
| 46 | * the recommended way to infer restrictions is by using the | 
|---|
| 47 | * DRM_MODE_ATOMIC_TEST_ONLY flag for the atomic IOCTL. | 
|---|
| 48 | * | 
|---|
| 49 | * Otherwise encoders aren't used in the uapi at all (any modeset request from | 
|---|
| 50 | * userspace directly connects a connector with a CRTC), drivers are therefore | 
|---|
| 51 | * free to use them however they wish. Modeset helper libraries make strong use | 
|---|
| 52 | * of encoders to facilitate code sharing. But for more complex settings it is | 
|---|
| 53 | * usually better to move shared code into a separate &drm_bridge. Compared to | 
|---|
| 54 | * encoders, bridges also have the benefit of being purely an internal | 
|---|
| 55 | * abstraction since they are not exposed to userspace at all. | 
|---|
| 56 | * | 
|---|
| 57 | * Encoders are initialized with drm_encoder_init() and cleaned up using | 
|---|
| 58 | * drm_encoder_cleanup(). | 
|---|
| 59 | */ | 
|---|
| 60 | static const struct drm_prop_enum_list drm_encoder_enum_list[] = { | 
|---|
| 61 | { DRM_MODE_ENCODER_NONE, "None"}, | 
|---|
| 62 | { DRM_MODE_ENCODER_DAC, .name: "DAC"}, | 
|---|
| 63 | { DRM_MODE_ENCODER_TMDS, .name: "TMDS"}, | 
|---|
| 64 | { DRM_MODE_ENCODER_LVDS, .name: "LVDS"}, | 
|---|
| 65 | { DRM_MODE_ENCODER_TVDAC, .name: "TV"}, | 
|---|
| 66 | { DRM_MODE_ENCODER_VIRTUAL, .name: "Virtual"}, | 
|---|
| 67 | { DRM_MODE_ENCODER_DSI, .name: "DSI"}, | 
|---|
| 68 | { DRM_MODE_ENCODER_DPMST, .name: "DP MST"}, | 
|---|
| 69 | { DRM_MODE_ENCODER_DPI, .name: "DPI"}, | 
|---|
| 70 | }; | 
|---|
| 71 |  | 
|---|
| 72 | int drm_encoder_register_all(struct drm_device *dev) | 
|---|
| 73 | { | 
|---|
| 74 | struct drm_encoder *encoder; | 
|---|
| 75 | int ret = 0; | 
|---|
| 76 |  | 
|---|
| 77 | drm_for_each_encoder(encoder, dev) { | 
|---|
| 78 | drm_debugfs_encoder_add(encoder); | 
|---|
| 79 |  | 
|---|
| 80 | if (encoder->funcs && encoder->funcs->late_register) | 
|---|
| 81 | ret = encoder->funcs->late_register(encoder); | 
|---|
| 82 | if (ret) | 
|---|
| 83 | return ret; | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | return 0; | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | void drm_encoder_unregister_all(struct drm_device *dev) | 
|---|
| 90 | { | 
|---|
| 91 | struct drm_encoder *encoder; | 
|---|
| 92 |  | 
|---|
| 93 | drm_for_each_encoder(encoder, dev) { | 
|---|
| 94 | if (encoder->funcs && encoder->funcs->early_unregister) | 
|---|
| 95 | encoder->funcs->early_unregister(encoder); | 
|---|
| 96 | drm_debugfs_encoder_remove(encoder); | 
|---|
| 97 | } | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | __printf(5, 0) | 
|---|
| 101 | static int __drm_encoder_init(struct drm_device *dev, | 
|---|
| 102 | struct drm_encoder *encoder, | 
|---|
| 103 | const struct drm_encoder_funcs *funcs, | 
|---|
| 104 | int encoder_type, const char *name, va_list ap) | 
|---|
| 105 | { | 
|---|
| 106 | int ret; | 
|---|
| 107 |  | 
|---|
| 108 | /* encoder index is used with 32bit bitmasks */ | 
|---|
| 109 | if (WARN_ON(dev->mode_config.num_encoder >= 32)) | 
|---|
| 110 | return -EINVAL; | 
|---|
| 111 |  | 
|---|
| 112 | ret = drm_mode_object_add(dev, obj: &encoder->base, DRM_MODE_OBJECT_ENCODER); | 
|---|
| 113 | if (ret) | 
|---|
| 114 | return ret; | 
|---|
| 115 |  | 
|---|
| 116 | encoder->dev = dev; | 
|---|
| 117 | encoder->encoder_type = encoder_type; | 
|---|
| 118 | encoder->funcs = funcs; | 
|---|
| 119 | if (name) { | 
|---|
| 120 | encoder->name = kvasprintf(GFP_KERNEL, fmt: name, args: ap); | 
|---|
| 121 | } else { | 
|---|
| 122 | encoder->name = kasprintf(GFP_KERNEL, fmt: "%s-%d", | 
|---|
| 123 | drm_encoder_enum_list[encoder_type].name, | 
|---|
| 124 | encoder->base.id); | 
|---|
| 125 | } | 
|---|
| 126 | if (!encoder->name) { | 
|---|
| 127 | ret = -ENOMEM; | 
|---|
| 128 | goto out_put; | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | INIT_LIST_HEAD(list: &encoder->bridge_chain); | 
|---|
| 132 | list_add_tail(new: &encoder->head, head: &dev->mode_config.encoder_list); | 
|---|
| 133 | encoder->index = dev->mode_config.num_encoder++; | 
|---|
| 134 |  | 
|---|
| 135 | out_put: | 
|---|
| 136 | if (ret) | 
|---|
| 137 | drm_mode_object_unregister(dev, object: &encoder->base); | 
|---|
| 138 |  | 
|---|
| 139 | return ret; | 
|---|
| 140 | } | 
|---|
| 141 |  | 
|---|
| 142 | /** | 
|---|
| 143 | * drm_encoder_init - Init a preallocated encoder | 
|---|
| 144 | * @dev: drm device | 
|---|
| 145 | * @encoder: the encoder to init | 
|---|
| 146 | * @funcs: callbacks for this encoder | 
|---|
| 147 | * @encoder_type: user visible type of the encoder | 
|---|
| 148 | * @name: printf style format string for the encoder name, or NULL for default name | 
|---|
| 149 | * | 
|---|
| 150 | * Initializes a preallocated encoder. Encoder should be subclassed as part of | 
|---|
| 151 | * driver encoder objects. At driver unload time the driver's | 
|---|
| 152 | * &drm_encoder_funcs.destroy hook should call drm_encoder_cleanup() and kfree() | 
|---|
| 153 | * the encoder structure. The encoder structure should not be allocated with | 
|---|
| 154 | * devm_kzalloc(). | 
|---|
| 155 | * | 
|---|
| 156 | * Note: consider using drmm_encoder_alloc() or drmm_encoder_init() | 
|---|
| 157 | * instead of drm_encoder_init() to let the DRM managed resource | 
|---|
| 158 | * infrastructure take care of cleanup and deallocation. | 
|---|
| 159 | * | 
|---|
| 160 | * Returns: | 
|---|
| 161 | * Zero on success, error code on failure. | 
|---|
| 162 | */ | 
|---|
| 163 | int drm_encoder_init(struct drm_device *dev, | 
|---|
| 164 | struct drm_encoder *encoder, | 
|---|
| 165 | const struct drm_encoder_funcs *funcs, | 
|---|
| 166 | int encoder_type, const char *name, ...) | 
|---|
| 167 | { | 
|---|
| 168 | va_list ap; | 
|---|
| 169 | int ret; | 
|---|
| 170 |  | 
|---|
| 171 | WARN_ON(!funcs->destroy); | 
|---|
| 172 |  | 
|---|
| 173 | va_start(ap, name); | 
|---|
| 174 | ret = __drm_encoder_init(dev, encoder, funcs, encoder_type, name, ap); | 
|---|
| 175 | va_end(ap); | 
|---|
| 176 |  | 
|---|
| 177 | return ret; | 
|---|
| 178 | } | 
|---|
| 179 | EXPORT_SYMBOL(drm_encoder_init); | 
|---|
| 180 |  | 
|---|
| 181 | /** | 
|---|
| 182 | * drm_encoder_cleanup - cleans up an initialised encoder | 
|---|
| 183 | * @encoder: encoder to cleanup | 
|---|
| 184 | * | 
|---|
| 185 | * Cleans up the encoder but doesn't free the object. | 
|---|
| 186 | */ | 
|---|
| 187 | void drm_encoder_cleanup(struct drm_encoder *encoder) | 
|---|
| 188 | { | 
|---|
| 189 | struct drm_device *dev = encoder->dev; | 
|---|
| 190 | struct drm_bridge *bridge, *next; | 
|---|
| 191 |  | 
|---|
| 192 | /* Note that the encoder_list is considered to be static; should we | 
|---|
| 193 | * remove the drm_encoder at runtime we would have to decrement all | 
|---|
| 194 | * the indices on the drm_encoder after us in the encoder_list. | 
|---|
| 195 | */ | 
|---|
| 196 |  | 
|---|
| 197 | list_for_each_entry_safe(bridge, next, &encoder->bridge_chain, | 
|---|
| 198 | chain_node) | 
|---|
| 199 | drm_bridge_detach(bridge); | 
|---|
| 200 |  | 
|---|
| 201 | drm_mode_object_unregister(dev, object: &encoder->base); | 
|---|
| 202 | kfree(objp: encoder->name); | 
|---|
| 203 | list_del(entry: &encoder->head); | 
|---|
| 204 | dev->mode_config.num_encoder--; | 
|---|
| 205 |  | 
|---|
| 206 | memset(s: encoder, c: 0, n: sizeof(*encoder)); | 
|---|
| 207 | } | 
|---|
| 208 | EXPORT_SYMBOL(drm_encoder_cleanup); | 
|---|
| 209 |  | 
|---|
| 210 | static void drmm_encoder_alloc_release(struct drm_device *dev, void *ptr) | 
|---|
| 211 | { | 
|---|
| 212 | struct drm_encoder *encoder = ptr; | 
|---|
| 213 |  | 
|---|
| 214 | if (WARN_ON(!encoder->dev)) | 
|---|
| 215 | return; | 
|---|
| 216 |  | 
|---|
| 217 | drm_encoder_cleanup(encoder); | 
|---|
| 218 | } | 
|---|
| 219 |  | 
|---|
| 220 | __printf(5, 0) | 
|---|
| 221 | static int __drmm_encoder_init(struct drm_device *dev, | 
|---|
| 222 | struct drm_encoder *encoder, | 
|---|
| 223 | const struct drm_encoder_funcs *funcs, | 
|---|
| 224 | int encoder_type, | 
|---|
| 225 | const char *name, | 
|---|
| 226 | va_list args) | 
|---|
| 227 | { | 
|---|
| 228 | int ret; | 
|---|
| 229 |  | 
|---|
| 230 | if (drm_WARN_ON(dev, funcs && funcs->destroy)) | 
|---|
| 231 | return -EINVAL; | 
|---|
| 232 |  | 
|---|
| 233 | ret = __drm_encoder_init(dev, encoder, funcs, encoder_type, name, ap: args); | 
|---|
| 234 | if (ret) | 
|---|
| 235 | return ret; | 
|---|
| 236 |  | 
|---|
| 237 | ret = drmm_add_action_or_reset(dev, drmm_encoder_alloc_release, encoder); | 
|---|
| 238 | if (ret) | 
|---|
| 239 | return ret; | 
|---|
| 240 |  | 
|---|
| 241 | return 0; | 
|---|
| 242 | } | 
|---|
| 243 |  | 
|---|
| 244 | void *__drmm_encoder_alloc(struct drm_device *dev, size_t size, size_t offset, | 
|---|
| 245 | const struct drm_encoder_funcs *funcs, | 
|---|
| 246 | int encoder_type, const char *name, ...) | 
|---|
| 247 | { | 
|---|
| 248 | void *container; | 
|---|
| 249 | struct drm_encoder *encoder; | 
|---|
| 250 | va_list ap; | 
|---|
| 251 | int ret; | 
|---|
| 252 |  | 
|---|
| 253 | container = drmm_kzalloc(dev, size, GFP_KERNEL); | 
|---|
| 254 | if (!container) | 
|---|
| 255 | return ERR_PTR(error: -ENOMEM); | 
|---|
| 256 |  | 
|---|
| 257 | encoder = container + offset; | 
|---|
| 258 |  | 
|---|
| 259 | va_start(ap, name); | 
|---|
| 260 | ret = __drmm_encoder_init(dev, encoder, funcs, encoder_type, name, args: ap); | 
|---|
| 261 | va_end(ap); | 
|---|
| 262 | if (ret) | 
|---|
| 263 | return ERR_PTR(error: ret); | 
|---|
| 264 |  | 
|---|
| 265 | return container; | 
|---|
| 266 | } | 
|---|
| 267 | EXPORT_SYMBOL(__drmm_encoder_alloc); | 
|---|
| 268 |  | 
|---|
| 269 | /** | 
|---|
| 270 | * drmm_encoder_init - Initialize a preallocated encoder | 
|---|
| 271 | * @dev: drm device | 
|---|
| 272 | * @encoder: the encoder to init | 
|---|
| 273 | * @funcs: callbacks for this encoder (optional) | 
|---|
| 274 | * @encoder_type: user visible type of the encoder | 
|---|
| 275 | * @name: printf style format string for the encoder name, or NULL for default name | 
|---|
| 276 | * | 
|---|
| 277 | * Initializes a preallocated encoder. Encoder should be subclassed as | 
|---|
| 278 | * part of driver encoder objects. Cleanup is automatically handled | 
|---|
| 279 | * through registering drm_encoder_cleanup() with drmm_add_action(). The | 
|---|
| 280 | * encoder structure should be allocated with drmm_kzalloc(). | 
|---|
| 281 | * | 
|---|
| 282 | * The @drm_encoder_funcs.destroy hook must be NULL. | 
|---|
| 283 | * | 
|---|
| 284 | * Returns: | 
|---|
| 285 | * Zero on success, error code on failure. | 
|---|
| 286 | */ | 
|---|
| 287 | int drmm_encoder_init(struct drm_device *dev, struct drm_encoder *encoder, | 
|---|
| 288 | const struct drm_encoder_funcs *funcs, | 
|---|
| 289 | int encoder_type, const char *name, ...) | 
|---|
| 290 | { | 
|---|
| 291 | va_list ap; | 
|---|
| 292 | int ret; | 
|---|
| 293 |  | 
|---|
| 294 | va_start(ap, name); | 
|---|
| 295 | ret = __drmm_encoder_init(dev, encoder, funcs, encoder_type, name, args: ap); | 
|---|
| 296 | va_end(ap); | 
|---|
| 297 | if (ret) | 
|---|
| 298 | return ret; | 
|---|
| 299 |  | 
|---|
| 300 | return 0; | 
|---|
| 301 | } | 
|---|
| 302 | EXPORT_SYMBOL(drmm_encoder_init); | 
|---|
| 303 |  | 
|---|
| 304 | static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder) | 
|---|
| 305 | { | 
|---|
| 306 | struct drm_connector *connector; | 
|---|
| 307 | struct drm_device *dev = encoder->dev; | 
|---|
| 308 | bool uses_atomic = false; | 
|---|
| 309 | struct drm_connector_list_iter conn_iter; | 
|---|
| 310 |  | 
|---|
| 311 | /* For atomic drivers only state objects are synchronously updated and | 
|---|
| 312 | * protected by modeset locks, so check those first. */ | 
|---|
| 313 | drm_connector_list_iter_begin(dev, iter: &conn_iter); | 
|---|
| 314 | drm_for_each_connector_iter(connector, &conn_iter) { | 
|---|
| 315 | if (!connector->state) | 
|---|
| 316 | continue; | 
|---|
| 317 |  | 
|---|
| 318 | uses_atomic = true; | 
|---|
| 319 |  | 
|---|
| 320 | if (connector->state->best_encoder != encoder) | 
|---|
| 321 | continue; | 
|---|
| 322 |  | 
|---|
| 323 | drm_connector_list_iter_end(iter: &conn_iter); | 
|---|
| 324 | return connector->state->crtc; | 
|---|
| 325 | } | 
|---|
| 326 | drm_connector_list_iter_end(iter: &conn_iter); | 
|---|
| 327 |  | 
|---|
| 328 | /* Don't return stale data (e.g. pending async disable). */ | 
|---|
| 329 | if (uses_atomic) | 
|---|
| 330 | return NULL; | 
|---|
| 331 |  | 
|---|
| 332 | return encoder->crtc; | 
|---|
| 333 | } | 
|---|
| 334 |  | 
|---|
| 335 | int drm_mode_getencoder(struct drm_device *dev, void *data, | 
|---|
| 336 | struct drm_file *file_priv) | 
|---|
| 337 | { | 
|---|
| 338 | struct drm_mode_get_encoder *enc_resp = data; | 
|---|
| 339 | struct drm_encoder *encoder; | 
|---|
| 340 | struct drm_crtc *crtc; | 
|---|
| 341 |  | 
|---|
| 342 | if (!drm_core_check_feature(dev, feature: DRIVER_MODESET)) | 
|---|
| 343 | return -EOPNOTSUPP; | 
|---|
| 344 |  | 
|---|
| 345 | encoder = drm_encoder_find(dev, file_priv, id: enc_resp->encoder_id); | 
|---|
| 346 | if (!encoder) | 
|---|
| 347 | return -ENOENT; | 
|---|
| 348 |  | 
|---|
| 349 | drm_modeset_lock(lock: &dev->mode_config.connection_mutex, NULL); | 
|---|
| 350 | crtc = drm_encoder_get_crtc(encoder); | 
|---|
| 351 | if (crtc && drm_lease_held(file_priv, id: crtc->base.id)) | 
|---|
| 352 | enc_resp->crtc_id = crtc->base.id; | 
|---|
| 353 | else | 
|---|
| 354 | enc_resp->crtc_id = 0; | 
|---|
| 355 | drm_modeset_unlock(lock: &dev->mode_config.connection_mutex); | 
|---|
| 356 |  | 
|---|
| 357 | enc_resp->encoder_type = encoder->encoder_type; | 
|---|
| 358 | enc_resp->encoder_id = encoder->base.id; | 
|---|
| 359 | enc_resp->possible_crtcs = drm_lease_filter_crtcs(file_priv, | 
|---|
| 360 | crtcs: encoder->possible_crtcs); | 
|---|
| 361 | enc_resp->possible_clones = encoder->possible_clones; | 
|---|
| 362 |  | 
|---|
| 363 | return 0; | 
|---|
| 364 | } | 
|---|
| 365 |  | 
|---|