drm_encoder.c 7.6 KB

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