drm_encoder.c 7.7 KB

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