drm_encoder.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 destroy hook in &drm_encoder_funcs.
  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. drm_modeset_lock_all(dev);
  104. ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
  105. if (ret)
  106. goto out_unlock;
  107. encoder->dev = dev;
  108. encoder->encoder_type = encoder_type;
  109. encoder->funcs = funcs;
  110. if (name) {
  111. va_list ap;
  112. va_start(ap, name);
  113. encoder->name = kvasprintf(GFP_KERNEL, name, ap);
  114. va_end(ap);
  115. } else {
  116. encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
  117. drm_encoder_enum_list[encoder_type].name,
  118. encoder->base.id);
  119. }
  120. if (!encoder->name) {
  121. ret = -ENOMEM;
  122. goto out_put;
  123. }
  124. list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
  125. encoder->index = dev->mode_config.num_encoder++;
  126. out_put:
  127. if (ret)
  128. drm_mode_object_unregister(dev, &encoder->base);
  129. out_unlock:
  130. drm_modeset_unlock_all(dev);
  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. drm_modeset_lock_all(dev);
  148. drm_mode_object_unregister(dev, &encoder->base);
  149. kfree(encoder->name);
  150. list_del(&encoder->head);
  151. dev->mode_config.num_encoder--;
  152. drm_modeset_unlock_all(dev);
  153. memset(encoder, 0, sizeof(*encoder));
  154. }
  155. EXPORT_SYMBOL(drm_encoder_cleanup);
  156. static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
  157. {
  158. struct drm_connector *connector;
  159. struct drm_device *dev = encoder->dev;
  160. bool uses_atomic = false;
  161. /* For atomic drivers only state objects are synchronously updated and
  162. * protected by modeset locks, so check those first. */
  163. drm_for_each_connector(connector, dev) {
  164. if (!connector->state)
  165. continue;
  166. uses_atomic = true;
  167. if (connector->state->best_encoder != encoder)
  168. continue;
  169. return connector->state->crtc;
  170. }
  171. /* Don't return stale data (e.g. pending async disable). */
  172. if (uses_atomic)
  173. return NULL;
  174. return encoder->crtc;
  175. }
  176. int drm_mode_getencoder(struct drm_device *dev, void *data,
  177. struct drm_file *file_priv)
  178. {
  179. struct drm_mode_get_encoder *enc_resp = data;
  180. struct drm_encoder *encoder;
  181. struct drm_crtc *crtc;
  182. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  183. return -EINVAL;
  184. encoder = drm_encoder_find(dev, enc_resp->encoder_id);
  185. if (!encoder)
  186. return -ENOENT;
  187. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  188. crtc = drm_encoder_get_crtc(encoder);
  189. if (crtc)
  190. enc_resp->crtc_id = crtc->base.id;
  191. else
  192. enc_resp->crtc_id = 0;
  193. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  194. enc_resp->encoder_type = encoder->encoder_type;
  195. enc_resp->encoder_id = encoder->base.id;
  196. enc_resp->possible_crtcs = encoder->possible_crtcs;
  197. enc_resp->possible_clones = encoder->possible_clones;
  198. return 0;
  199. }