drm_encoder.c 11 KB

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