drm_mode_config.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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 <drm/drm_encoder.h>
  23. #include <drm/drm_mode_config.h>
  24. #include <drm/drmP.h>
  25. #include "drm_crtc_internal.h"
  26. #include "drm_internal.h"
  27. int drm_modeset_register_all(struct drm_device *dev)
  28. {
  29. int ret;
  30. ret = drm_plane_register_all(dev);
  31. if (ret)
  32. goto err_plane;
  33. ret = drm_crtc_register_all(dev);
  34. if (ret)
  35. goto err_crtc;
  36. ret = drm_encoder_register_all(dev);
  37. if (ret)
  38. goto err_encoder;
  39. ret = drm_connector_register_all(dev);
  40. if (ret)
  41. goto err_connector;
  42. return 0;
  43. err_connector:
  44. drm_encoder_unregister_all(dev);
  45. err_encoder:
  46. drm_crtc_unregister_all(dev);
  47. err_crtc:
  48. drm_plane_unregister_all(dev);
  49. err_plane:
  50. return ret;
  51. }
  52. void drm_modeset_unregister_all(struct drm_device *dev)
  53. {
  54. drm_connector_unregister_all(dev);
  55. drm_encoder_unregister_all(dev);
  56. drm_crtc_unregister_all(dev);
  57. drm_plane_unregister_all(dev);
  58. }
  59. /**
  60. * drm_mode_getresources - get graphics configuration
  61. * @dev: drm device for the ioctl
  62. * @data: data pointer for the ioctl
  63. * @file_priv: drm file for the ioctl call
  64. *
  65. * Construct a set of configuration description structures and return
  66. * them to the user, including CRTC, connector and framebuffer configuration.
  67. *
  68. * Called by the user via ioctl.
  69. *
  70. * Returns:
  71. * Zero on success, negative errno on failure.
  72. */
  73. int drm_mode_getresources(struct drm_device *dev, void *data,
  74. struct drm_file *file_priv)
  75. {
  76. struct drm_mode_card_res *card_res = data;
  77. struct drm_framebuffer *fb;
  78. struct drm_connector *connector;
  79. struct drm_crtc *crtc;
  80. struct drm_encoder *encoder;
  81. int count, ret = 0;
  82. uint32_t __user *fb_id;
  83. uint32_t __user *crtc_id;
  84. uint32_t __user *connector_id;
  85. uint32_t __user *encoder_id;
  86. struct drm_connector_list_iter conn_iter;
  87. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  88. return -EINVAL;
  89. mutex_lock(&file_priv->fbs_lock);
  90. count = 0;
  91. fb_id = u64_to_user_ptr(card_res->fb_id_ptr);
  92. list_for_each_entry(fb, &file_priv->fbs, filp_head) {
  93. if (count < card_res->count_fbs &&
  94. put_user(fb->base.id, fb_id + count)) {
  95. mutex_unlock(&file_priv->fbs_lock);
  96. return -EFAULT;
  97. }
  98. count++;
  99. }
  100. card_res->count_fbs = count;
  101. mutex_unlock(&file_priv->fbs_lock);
  102. card_res->max_height = dev->mode_config.max_height;
  103. card_res->min_height = dev->mode_config.min_height;
  104. card_res->max_width = dev->mode_config.max_width;
  105. card_res->min_width = dev->mode_config.min_width;
  106. count = 0;
  107. crtc_id = u64_to_user_ptr(card_res->crtc_id_ptr);
  108. drm_for_each_crtc(crtc, dev) {
  109. if (drm_lease_held(file_priv, crtc->base.id)) {
  110. if (count < card_res->count_crtcs &&
  111. put_user(crtc->base.id, crtc_id + count))
  112. return -EFAULT;
  113. count++;
  114. }
  115. }
  116. card_res->count_crtcs = count;
  117. count = 0;
  118. encoder_id = u64_to_user_ptr(card_res->encoder_id_ptr);
  119. drm_for_each_encoder(encoder, dev) {
  120. if (count < card_res->count_encoders &&
  121. put_user(encoder->base.id, encoder_id + count))
  122. return -EFAULT;
  123. count++;
  124. }
  125. card_res->count_encoders = count;
  126. drm_connector_list_iter_begin(dev, &conn_iter);
  127. count = 0;
  128. connector_id = u64_to_user_ptr(card_res->connector_id_ptr);
  129. drm_for_each_connector_iter(connector, &conn_iter) {
  130. /* only expose writeback connectors if userspace understands them */
  131. if (!file_priv->writeback_connectors &&
  132. (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK))
  133. continue;
  134. if (drm_lease_held(file_priv, connector->base.id)) {
  135. if (count < card_res->count_connectors &&
  136. put_user(connector->base.id, connector_id + count)) {
  137. drm_connector_list_iter_end(&conn_iter);
  138. return -EFAULT;
  139. }
  140. count++;
  141. }
  142. }
  143. card_res->count_connectors = count;
  144. drm_connector_list_iter_end(&conn_iter);
  145. return ret;
  146. }
  147. /**
  148. * drm_mode_config_reset - call ->reset callbacks
  149. * @dev: drm device
  150. *
  151. * This functions calls all the crtc's, encoder's and connector's ->reset
  152. * callback. Drivers can use this in e.g. their driver load or resume code to
  153. * reset hardware and software state.
  154. */
  155. void drm_mode_config_reset(struct drm_device *dev)
  156. {
  157. struct drm_crtc *crtc;
  158. struct drm_plane *plane;
  159. struct drm_encoder *encoder;
  160. struct drm_connector *connector;
  161. struct drm_connector_list_iter conn_iter;
  162. drm_for_each_plane(plane, dev)
  163. if (plane->funcs->reset)
  164. plane->funcs->reset(plane);
  165. drm_for_each_crtc(crtc, dev)
  166. if (crtc->funcs->reset)
  167. crtc->funcs->reset(crtc);
  168. drm_for_each_encoder(encoder, dev)
  169. if (encoder->funcs->reset)
  170. encoder->funcs->reset(encoder);
  171. drm_connector_list_iter_begin(dev, &conn_iter);
  172. drm_for_each_connector_iter(connector, &conn_iter)
  173. if (connector->funcs->reset)
  174. connector->funcs->reset(connector);
  175. drm_connector_list_iter_end(&conn_iter);
  176. }
  177. EXPORT_SYMBOL(drm_mode_config_reset);
  178. /*
  179. * Global properties
  180. */
  181. static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
  182. { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
  183. { DRM_PLANE_TYPE_PRIMARY, "Primary" },
  184. { DRM_PLANE_TYPE_CURSOR, "Cursor" },
  185. };
  186. static int drm_mode_create_standard_properties(struct drm_device *dev)
  187. {
  188. struct drm_property *prop;
  189. int ret;
  190. ret = drm_connector_create_standard_properties(dev);
  191. if (ret)
  192. return ret;
  193. prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  194. "type", drm_plane_type_enum_list,
  195. ARRAY_SIZE(drm_plane_type_enum_list));
  196. if (!prop)
  197. return -ENOMEM;
  198. dev->mode_config.plane_type_property = prop;
  199. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  200. "SRC_X", 0, UINT_MAX);
  201. if (!prop)
  202. return -ENOMEM;
  203. dev->mode_config.prop_src_x = prop;
  204. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  205. "SRC_Y", 0, UINT_MAX);
  206. if (!prop)
  207. return -ENOMEM;
  208. dev->mode_config.prop_src_y = prop;
  209. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  210. "SRC_W", 0, UINT_MAX);
  211. if (!prop)
  212. return -ENOMEM;
  213. dev->mode_config.prop_src_w = prop;
  214. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  215. "SRC_H", 0, UINT_MAX);
  216. if (!prop)
  217. return -ENOMEM;
  218. dev->mode_config.prop_src_h = prop;
  219. prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
  220. "CRTC_X", INT_MIN, INT_MAX);
  221. if (!prop)
  222. return -ENOMEM;
  223. dev->mode_config.prop_crtc_x = prop;
  224. prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
  225. "CRTC_Y", INT_MIN, INT_MAX);
  226. if (!prop)
  227. return -ENOMEM;
  228. dev->mode_config.prop_crtc_y = prop;
  229. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  230. "CRTC_W", 0, INT_MAX);
  231. if (!prop)
  232. return -ENOMEM;
  233. dev->mode_config.prop_crtc_w = prop;
  234. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  235. "CRTC_H", 0, INT_MAX);
  236. if (!prop)
  237. return -ENOMEM;
  238. dev->mode_config.prop_crtc_h = prop;
  239. prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
  240. "FB_ID", DRM_MODE_OBJECT_FB);
  241. if (!prop)
  242. return -ENOMEM;
  243. dev->mode_config.prop_fb_id = prop;
  244. prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
  245. "IN_FENCE_FD", -1, INT_MAX);
  246. if (!prop)
  247. return -ENOMEM;
  248. dev->mode_config.prop_in_fence_fd = prop;
  249. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  250. "OUT_FENCE_PTR", 0, U64_MAX);
  251. if (!prop)
  252. return -ENOMEM;
  253. dev->mode_config.prop_out_fence_ptr = prop;
  254. prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
  255. "CRTC_ID", DRM_MODE_OBJECT_CRTC);
  256. if (!prop)
  257. return -ENOMEM;
  258. dev->mode_config.prop_crtc_id = prop;
  259. prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
  260. "ACTIVE");
  261. if (!prop)
  262. return -ENOMEM;
  263. dev->mode_config.prop_active = prop;
  264. prop = drm_property_create(dev,
  265. DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
  266. "MODE_ID", 0);
  267. if (!prop)
  268. return -ENOMEM;
  269. dev->mode_config.prop_mode_id = prop;
  270. prop = drm_property_create(dev,
  271. DRM_MODE_PROP_BLOB,
  272. "DEGAMMA_LUT", 0);
  273. if (!prop)
  274. return -ENOMEM;
  275. dev->mode_config.degamma_lut_property = prop;
  276. prop = drm_property_create_range(dev,
  277. DRM_MODE_PROP_IMMUTABLE,
  278. "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
  279. if (!prop)
  280. return -ENOMEM;
  281. dev->mode_config.degamma_lut_size_property = prop;
  282. prop = drm_property_create(dev,
  283. DRM_MODE_PROP_BLOB,
  284. "CTM", 0);
  285. if (!prop)
  286. return -ENOMEM;
  287. dev->mode_config.ctm_property = prop;
  288. prop = drm_property_create(dev,
  289. DRM_MODE_PROP_BLOB,
  290. "GAMMA_LUT", 0);
  291. if (!prop)
  292. return -ENOMEM;
  293. dev->mode_config.gamma_lut_property = prop;
  294. prop = drm_property_create_range(dev,
  295. DRM_MODE_PROP_IMMUTABLE,
  296. "GAMMA_LUT_SIZE", 0, UINT_MAX);
  297. if (!prop)
  298. return -ENOMEM;
  299. dev->mode_config.gamma_lut_size_property = prop;
  300. prop = drm_property_create(dev,
  301. DRM_MODE_PROP_IMMUTABLE | DRM_MODE_PROP_BLOB,
  302. "IN_FORMATS", 0);
  303. if (!prop)
  304. return -ENOMEM;
  305. dev->mode_config.modifiers_property = prop;
  306. return 0;
  307. }
  308. /**
  309. * drm_mode_config_init - initialize DRM mode_configuration structure
  310. * @dev: DRM device
  311. *
  312. * Initialize @dev's mode_config structure, used for tracking the graphics
  313. * configuration of @dev.
  314. *
  315. * Since this initializes the modeset locks, no locking is possible. Which is no
  316. * problem, since this should happen single threaded at init time. It is the
  317. * driver's problem to ensure this guarantee.
  318. *
  319. */
  320. void drm_mode_config_init(struct drm_device *dev)
  321. {
  322. mutex_init(&dev->mode_config.mutex);
  323. drm_modeset_lock_init(&dev->mode_config.connection_mutex);
  324. mutex_init(&dev->mode_config.idr_mutex);
  325. mutex_init(&dev->mode_config.fb_lock);
  326. mutex_init(&dev->mode_config.blob_lock);
  327. INIT_LIST_HEAD(&dev->mode_config.fb_list);
  328. INIT_LIST_HEAD(&dev->mode_config.crtc_list);
  329. INIT_LIST_HEAD(&dev->mode_config.connector_list);
  330. INIT_LIST_HEAD(&dev->mode_config.encoder_list);
  331. INIT_LIST_HEAD(&dev->mode_config.property_list);
  332. INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
  333. INIT_LIST_HEAD(&dev->mode_config.plane_list);
  334. idr_init(&dev->mode_config.crtc_idr);
  335. idr_init(&dev->mode_config.tile_idr);
  336. ida_init(&dev->mode_config.connector_ida);
  337. spin_lock_init(&dev->mode_config.connector_list_lock);
  338. init_llist_head(&dev->mode_config.connector_free_list);
  339. INIT_WORK(&dev->mode_config.connector_free_work, drm_connector_free_work_fn);
  340. drm_mode_create_standard_properties(dev);
  341. /* Just to be sure */
  342. dev->mode_config.num_fb = 0;
  343. dev->mode_config.num_connector = 0;
  344. dev->mode_config.num_crtc = 0;
  345. dev->mode_config.num_encoder = 0;
  346. dev->mode_config.num_total_plane = 0;
  347. }
  348. EXPORT_SYMBOL(drm_mode_config_init);
  349. /**
  350. * drm_mode_config_cleanup - free up DRM mode_config info
  351. * @dev: DRM device
  352. *
  353. * Free up all the connectors and CRTCs associated with this DRM device, then
  354. * free up the framebuffers and associated buffer objects.
  355. *
  356. * Note that since this /should/ happen single-threaded at driver/device
  357. * teardown time, no locking is required. It's the driver's job to ensure that
  358. * this guarantee actually holds true.
  359. *
  360. * FIXME: cleanup any dangling user buffer objects too
  361. */
  362. void drm_mode_config_cleanup(struct drm_device *dev)
  363. {
  364. struct drm_connector *connector;
  365. struct drm_connector_list_iter conn_iter;
  366. struct drm_crtc *crtc, *ct;
  367. struct drm_encoder *encoder, *enct;
  368. struct drm_framebuffer *fb, *fbt;
  369. struct drm_property *property, *pt;
  370. struct drm_property_blob *blob, *bt;
  371. struct drm_plane *plane, *plt;
  372. list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
  373. head) {
  374. encoder->funcs->destroy(encoder);
  375. }
  376. drm_connector_list_iter_begin(dev, &conn_iter);
  377. drm_for_each_connector_iter(connector, &conn_iter) {
  378. /* drm_connector_list_iter holds an full reference to the
  379. * current connector itself, which means it is inherently safe
  380. * against unreferencing the current connector - but not against
  381. * deleting it right away. */
  382. drm_connector_put(connector);
  383. }
  384. drm_connector_list_iter_end(&conn_iter);
  385. /* connector_iter drops references in a work item. */
  386. flush_work(&dev->mode_config.connector_free_work);
  387. if (WARN_ON(!list_empty(&dev->mode_config.connector_list))) {
  388. drm_connector_list_iter_begin(dev, &conn_iter);
  389. drm_for_each_connector_iter(connector, &conn_iter)
  390. DRM_ERROR("connector %s leaked!\n", connector->name);
  391. drm_connector_list_iter_end(&conn_iter);
  392. }
  393. list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
  394. head) {
  395. drm_property_destroy(dev, property);
  396. }
  397. list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
  398. head) {
  399. plane->funcs->destroy(plane);
  400. }
  401. list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
  402. crtc->funcs->destroy(crtc);
  403. }
  404. list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
  405. head_global) {
  406. drm_property_blob_put(blob);
  407. }
  408. /*
  409. * Single-threaded teardown context, so it's not required to grab the
  410. * fb_lock to protect against concurrent fb_list access. Contrary, it
  411. * would actually deadlock with the drm_framebuffer_cleanup function.
  412. *
  413. * Also, if there are any framebuffers left, that's a driver leak now,
  414. * so politely WARN about this.
  415. */
  416. WARN_ON(!list_empty(&dev->mode_config.fb_list));
  417. list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
  418. struct drm_printer p = drm_debug_printer("[leaked fb]");
  419. drm_printf(&p, "framebuffer[%u]:\n", fb->base.id);
  420. drm_framebuffer_print_info(&p, 1, fb);
  421. drm_framebuffer_free(&fb->base.refcount);
  422. }
  423. ida_destroy(&dev->mode_config.connector_ida);
  424. idr_destroy(&dev->mode_config.tile_idr);
  425. idr_destroy(&dev->mode_config.crtc_idr);
  426. drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
  427. }
  428. EXPORT_SYMBOL(drm_mode_config_cleanup);