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, "DAC" }, 63 { DRM_MODE_ENCODER_TMDS, "TMDS" }, 64 { DRM_MODE_ENCODER_LVDS, "LVDS" }, 65 { DRM_MODE_ENCODER_TVDAC, "TV" }, 66 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" }, 67 { DRM_MODE_ENCODER_DSI, "DSI" }, 68 { DRM_MODE_ENCODER_DPMST, "DP MST" }, 69 { DRM_MODE_ENCODER_DPI, "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, &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, name, ap); 121 } else { 122 encoder->name = kasprintf(GFP_KERNEL, "%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(&encoder->bridge_chain); 132 mutex_init(&encoder->bridge_chain_mutex); 133 list_add_tail(&encoder->head, &dev->mode_config.encoder_list); 134 encoder->index = dev->mode_config.num_encoder++; 135 136 out_put: 137 if (ret) 138 drm_mode_object_unregister(dev, &encoder->base); 139 140 return ret; 141 } 142 143 /** 144 * drm_encoder_init - Init a preallocated encoder 145 * @dev: drm device 146 * @encoder: the encoder to init 147 * @funcs: callbacks for this encoder 148 * @encoder_type: user visible type of the encoder 149 * @name: printf style format string for the encoder name, or NULL for default name 150 * 151 * Initializes a preallocated encoder. Encoder should be subclassed as part of 152 * driver encoder objects. At driver unload time the driver's 153 * &drm_encoder_funcs.destroy hook should call drm_encoder_cleanup() and kfree() 154 * the encoder structure. The encoder structure should not be allocated with 155 * devm_kzalloc(). 156 * 157 * Note: consider using drmm_encoder_alloc() or drmm_encoder_init() 158 * instead of drm_encoder_init() to let the DRM managed resource 159 * infrastructure take care of cleanup and deallocation. 160 * 161 * Returns: 162 * Zero on success, error code on failure. 163 */ 164 int drm_encoder_init(struct drm_device *dev, 165 struct drm_encoder *encoder, 166 const struct drm_encoder_funcs *funcs, 167 int encoder_type, const char *name, ...) 168 { 169 va_list ap; 170 int ret; 171 172 WARN_ON(!funcs->destroy); 173 174 va_start(ap, name); 175 ret = __drm_encoder_init(dev, encoder, funcs, encoder_type, name, ap); 176 va_end(ap); 177 178 return ret; 179 } 180 EXPORT_SYMBOL(drm_encoder_init); 181 182 /** 183 * drm_encoder_cleanup - cleans up an initialised encoder 184 * @encoder: encoder to cleanup 185 * 186 * Cleans up the encoder but doesn't free the object. 187 */ 188 void drm_encoder_cleanup(struct drm_encoder *encoder) 189 { 190 struct drm_device *dev = encoder->dev; 191 struct drm_bridge *bridge, *next; 192 LIST_HEAD(tmplist); 193 194 /* Note that the encoder_list is considered to be static; should we 195 * remove the drm_encoder at runtime we would have to decrement all 196 * the indices on the drm_encoder after us in the encoder_list. 197 */ 198 199 /* 200 * We need the bridge_chain_mutex to modify the chain, but 201 * drm_bridge_detach() will call DRM_MODESET_LOCK_ALL_BEGIN() (in 202 * drm_modeset_lock_fini()), resulting in a possible ABBA circular 203 * deadlock. Avoid it by first moving all the bridges to a 204 * temporary list holding the lock, and then calling 205 * drm_bridge_detach() without the lock. 206 */ 207 mutex_lock(&encoder->bridge_chain_mutex); 208 list_cut_before(&tmplist, &encoder->bridge_chain, &encoder->bridge_chain); 209 mutex_unlock(&encoder->bridge_chain_mutex); 210 211 list_for_each_entry_safe(bridge, next, &tmplist, chain_node) 212 drm_bridge_detach(bridge); 213 214 drm_mode_object_unregister(dev, &encoder->base); 215 kfree(encoder->name); 216 list_del(&encoder->head); 217 dev->mode_config.num_encoder--; 218 mutex_destroy(&encoder->bridge_chain_mutex); 219 220 memset(encoder, 0, sizeof(*encoder)); 221 } 222 EXPORT_SYMBOL(drm_encoder_cleanup); 223 224 static void drmm_encoder_alloc_release(struct drm_device *dev, void *ptr) 225 { 226 struct drm_encoder *encoder = ptr; 227 228 if (WARN_ON(!encoder->dev)) 229 return; 230 231 drm_encoder_cleanup(encoder); 232 } 233 234 __printf(5, 0) 235 static int __drmm_encoder_init(struct drm_device *dev, 236 struct drm_encoder *encoder, 237 const struct drm_encoder_funcs *funcs, 238 int encoder_type, 239 const char *name, 240 va_list args) 241 { 242 int ret; 243 244 if (drm_WARN_ON(dev, funcs && funcs->destroy)) 245 return -EINVAL; 246 247 ret = __drm_encoder_init(dev, encoder, funcs, encoder_type, name, args); 248 if (ret) 249 return ret; 250 251 ret = drmm_add_action_or_reset(dev, drmm_encoder_alloc_release, encoder); 252 if (ret) 253 return ret; 254 255 return 0; 256 } 257 258 void *__drmm_encoder_alloc(struct drm_device *dev, size_t size, size_t offset, 259 const struct drm_encoder_funcs *funcs, 260 int encoder_type, const char *name, ...) 261 { 262 void *container; 263 struct drm_encoder *encoder; 264 va_list ap; 265 int ret; 266 267 container = drmm_kzalloc(dev, size, GFP_KERNEL); 268 if (!container) 269 return ERR_PTR(-ENOMEM); 270 271 encoder = container + offset; 272 273 va_start(ap, name); 274 ret = __drmm_encoder_init(dev, encoder, funcs, encoder_type, name, ap); 275 va_end(ap); 276 if (ret) 277 return ERR_PTR(ret); 278 279 return container; 280 } 281 EXPORT_SYMBOL(__drmm_encoder_alloc); 282 283 /** 284 * drmm_encoder_init - Initialize a preallocated encoder 285 * @dev: drm device 286 * @encoder: the encoder to init 287 * @funcs: callbacks for this encoder (optional) 288 * @encoder_type: user visible type of the encoder 289 * @name: printf style format string for the encoder name, or NULL for default name 290 * 291 * Initializes a preallocated encoder. Encoder should be subclassed as 292 * part of driver encoder objects. Cleanup is automatically handled 293 * through registering drm_encoder_cleanup() with drmm_add_action(). The 294 * encoder structure should be allocated with drmm_kzalloc(). 295 * 296 * The @drm_encoder_funcs.destroy hook must be NULL. 297 * 298 * Returns: 299 * Zero on success, error code on failure. 300 */ 301 int drmm_encoder_init(struct drm_device *dev, struct drm_encoder *encoder, 302 const struct drm_encoder_funcs *funcs, 303 int encoder_type, const char *name, ...) 304 { 305 va_list ap; 306 int ret; 307 308 va_start(ap, name); 309 ret = __drmm_encoder_init(dev, encoder, funcs, encoder_type, name, ap); 310 va_end(ap); 311 if (ret) 312 return ret; 313 314 return 0; 315 } 316 EXPORT_SYMBOL(drmm_encoder_init); 317 318 static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder) 319 { 320 struct drm_connector *connector; 321 struct drm_device *dev = encoder->dev; 322 bool uses_atomic = false; 323 struct drm_connector_list_iter conn_iter; 324 325 /* For atomic drivers only state objects are synchronously updated and 326 * protected by modeset locks, so check those first. */ 327 drm_connector_list_iter_begin(dev, &conn_iter); 328 drm_for_each_connector_iter(connector, &conn_iter) { 329 if (!connector->state) 330 continue; 331 332 uses_atomic = true; 333 334 if (connector->state->best_encoder != encoder) 335 continue; 336 337 drm_connector_list_iter_end(&conn_iter); 338 return connector->state->crtc; 339 } 340 drm_connector_list_iter_end(&conn_iter); 341 342 /* Don't return stale data (e.g. pending async disable). */ 343 if (uses_atomic) 344 return NULL; 345 346 return encoder->crtc; 347 } 348 349 int drm_mode_getencoder(struct drm_device *dev, void *data, 350 struct drm_file *file_priv) 351 { 352 struct drm_mode_get_encoder *enc_resp = data; 353 struct drm_encoder *encoder; 354 struct drm_crtc *crtc; 355 356 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 357 return -EOPNOTSUPP; 358 359 encoder = drm_encoder_find(dev, file_priv, enc_resp->encoder_id); 360 if (!encoder) 361 return -ENOENT; 362 363 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL); 364 crtc = drm_encoder_get_crtc(encoder); 365 if (crtc && drm_lease_held(file_priv, crtc->base.id)) 366 enc_resp->crtc_id = crtc->base.id; 367 else 368 enc_resp->crtc_id = 0; 369 drm_modeset_unlock(&dev->mode_config.connection_mutex); 370 371 enc_resp->encoder_type = encoder->encoder_type; 372 enc_resp->encoder_id = encoder->base.id; 373 enc_resp->possible_crtcs = drm_lease_filter_crtcs(file_priv, 374 encoder->possible_crtcs); 375 enc_resp->possible_clones = encoder->possible_clones; 376 377 return 0; 378 } 379