drm_encoder.c 11 KB

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