drm_mode_config.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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 (count < card_res->count_crtcs &&
  110. put_user(crtc->base.id, crtc_id + count))
  111. return -EFAULT;
  112. count++;
  113. }
  114. card_res->count_crtcs = count;
  115. count = 0;
  116. encoder_id = u64_to_user_ptr(card_res->encoder_id_ptr);
  117. drm_for_each_encoder(encoder, dev) {
  118. if (count < card_res->count_encoders &&
  119. put_user(encoder->base.id, encoder_id + count))
  120. return -EFAULT;
  121. count++;
  122. }
  123. card_res->count_encoders = count;
  124. drm_connector_list_iter_begin(dev, &conn_iter);
  125. count = 0;
  126. connector_id = u64_to_user_ptr(card_res->connector_id_ptr);
  127. drm_for_each_connector_iter(connector, &conn_iter) {
  128. if (count < card_res->count_connectors &&
  129. put_user(connector->base.id, connector_id + count)) {
  130. drm_connector_list_iter_end(&conn_iter);
  131. return -EFAULT;
  132. }
  133. count++;
  134. }
  135. card_res->count_connectors = count;
  136. drm_connector_list_iter_end(&conn_iter);
  137. return ret;
  138. }
  139. /**
  140. * drm_mode_config_reset - call ->reset callbacks
  141. * @dev: drm device
  142. *
  143. * This functions calls all the crtc's, encoder's and connector's ->reset
  144. * callback. Drivers can use this in e.g. their driver load or resume code to
  145. * reset hardware and software state.
  146. */
  147. void drm_mode_config_reset(struct drm_device *dev)
  148. {
  149. struct drm_crtc *crtc;
  150. struct drm_plane *plane;
  151. struct drm_encoder *encoder;
  152. struct drm_connector *connector;
  153. struct drm_connector_list_iter conn_iter;
  154. drm_for_each_plane(plane, dev)
  155. if (plane->funcs->reset)
  156. plane->funcs->reset(plane);
  157. drm_for_each_crtc(crtc, dev)
  158. if (crtc->funcs->reset)
  159. crtc->funcs->reset(crtc);
  160. drm_for_each_encoder(encoder, dev)
  161. if (encoder->funcs->reset)
  162. encoder->funcs->reset(encoder);
  163. drm_connector_list_iter_begin(dev, &conn_iter);
  164. drm_for_each_connector_iter(connector, &conn_iter)
  165. if (connector->funcs->reset)
  166. connector->funcs->reset(connector);
  167. drm_connector_list_iter_end(&conn_iter);
  168. }
  169. EXPORT_SYMBOL(drm_mode_config_reset);
  170. /*
  171. * Global properties
  172. */
  173. static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
  174. { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
  175. { DRM_PLANE_TYPE_PRIMARY, "Primary" },
  176. { DRM_PLANE_TYPE_CURSOR, "Cursor" },
  177. };
  178. static int drm_mode_create_standard_properties(struct drm_device *dev)
  179. {
  180. struct drm_property *prop;
  181. int ret;
  182. ret = drm_connector_create_standard_properties(dev);
  183. if (ret)
  184. return ret;
  185. prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  186. "type", drm_plane_type_enum_list,
  187. ARRAY_SIZE(drm_plane_type_enum_list));
  188. if (!prop)
  189. return -ENOMEM;
  190. dev->mode_config.plane_type_property = prop;
  191. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  192. "SRC_X", 0, UINT_MAX);
  193. if (!prop)
  194. return -ENOMEM;
  195. dev->mode_config.prop_src_x = prop;
  196. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  197. "SRC_Y", 0, UINT_MAX);
  198. if (!prop)
  199. return -ENOMEM;
  200. dev->mode_config.prop_src_y = prop;
  201. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  202. "SRC_W", 0, UINT_MAX);
  203. if (!prop)
  204. return -ENOMEM;
  205. dev->mode_config.prop_src_w = prop;
  206. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  207. "SRC_H", 0, UINT_MAX);
  208. if (!prop)
  209. return -ENOMEM;
  210. dev->mode_config.prop_src_h = prop;
  211. prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
  212. "CRTC_X", INT_MIN, INT_MAX);
  213. if (!prop)
  214. return -ENOMEM;
  215. dev->mode_config.prop_crtc_x = prop;
  216. prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
  217. "CRTC_Y", INT_MIN, INT_MAX);
  218. if (!prop)
  219. return -ENOMEM;
  220. dev->mode_config.prop_crtc_y = prop;
  221. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  222. "CRTC_W", 0, INT_MAX);
  223. if (!prop)
  224. return -ENOMEM;
  225. dev->mode_config.prop_crtc_w = prop;
  226. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  227. "CRTC_H", 0, INT_MAX);
  228. if (!prop)
  229. return -ENOMEM;
  230. dev->mode_config.prop_crtc_h = prop;
  231. prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
  232. "FB_ID", DRM_MODE_OBJECT_FB);
  233. if (!prop)
  234. return -ENOMEM;
  235. dev->mode_config.prop_fb_id = prop;
  236. prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
  237. "IN_FENCE_FD", -1, INT_MAX);
  238. if (!prop)
  239. return -ENOMEM;
  240. dev->mode_config.prop_in_fence_fd = prop;
  241. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  242. "OUT_FENCE_PTR", 0, U64_MAX);
  243. if (!prop)
  244. return -ENOMEM;
  245. dev->mode_config.prop_out_fence_ptr = prop;
  246. prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
  247. "CRTC_ID", DRM_MODE_OBJECT_CRTC);
  248. if (!prop)
  249. return -ENOMEM;
  250. dev->mode_config.prop_crtc_id = prop;
  251. prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
  252. "ACTIVE");
  253. if (!prop)
  254. return -ENOMEM;
  255. dev->mode_config.prop_active = prop;
  256. prop = drm_property_create(dev,
  257. DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
  258. "MODE_ID", 0);
  259. if (!prop)
  260. return -ENOMEM;
  261. dev->mode_config.prop_mode_id = prop;
  262. prop = drm_property_create(dev,
  263. DRM_MODE_PROP_BLOB,
  264. "DEGAMMA_LUT", 0);
  265. if (!prop)
  266. return -ENOMEM;
  267. dev->mode_config.degamma_lut_property = prop;
  268. prop = drm_property_create_range(dev,
  269. DRM_MODE_PROP_IMMUTABLE,
  270. "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
  271. if (!prop)
  272. return -ENOMEM;
  273. dev->mode_config.degamma_lut_size_property = prop;
  274. prop = drm_property_create(dev,
  275. DRM_MODE_PROP_BLOB,
  276. "CTM", 0);
  277. if (!prop)
  278. return -ENOMEM;
  279. dev->mode_config.ctm_property = prop;
  280. prop = drm_property_create(dev,
  281. DRM_MODE_PROP_BLOB,
  282. "GAMMA_LUT", 0);
  283. if (!prop)
  284. return -ENOMEM;
  285. dev->mode_config.gamma_lut_property = prop;
  286. prop = drm_property_create_range(dev,
  287. DRM_MODE_PROP_IMMUTABLE,
  288. "GAMMA_LUT_SIZE", 0, UINT_MAX);
  289. if (!prop)
  290. return -ENOMEM;
  291. dev->mode_config.gamma_lut_size_property = prop;
  292. prop = drm_property_create(dev,
  293. DRM_MODE_PROP_IMMUTABLE | DRM_MODE_PROP_BLOB,
  294. "IN_FORMATS", 0);
  295. if (!prop)
  296. return -ENOMEM;
  297. dev->mode_config.modifiers_property = prop;
  298. return 0;
  299. }
  300. /**
  301. * drm_mode_config_init - initialize DRM mode_configuration structure
  302. * @dev: DRM device
  303. *
  304. * Initialize @dev's mode_config structure, used for tracking the graphics
  305. * configuration of @dev.
  306. *
  307. * Since this initializes the modeset locks, no locking is possible. Which is no
  308. * problem, since this should happen single threaded at init time. It is the
  309. * driver's problem to ensure this guarantee.
  310. *
  311. */
  312. void drm_mode_config_init(struct drm_device *dev)
  313. {
  314. mutex_init(&dev->mode_config.mutex);
  315. drm_modeset_lock_init(&dev->mode_config.connection_mutex);
  316. mutex_init(&dev->mode_config.idr_mutex);
  317. mutex_init(&dev->mode_config.fb_lock);
  318. mutex_init(&dev->mode_config.blob_lock);
  319. INIT_LIST_HEAD(&dev->mode_config.fb_list);
  320. INIT_LIST_HEAD(&dev->mode_config.crtc_list);
  321. INIT_LIST_HEAD(&dev->mode_config.connector_list);
  322. INIT_LIST_HEAD(&dev->mode_config.encoder_list);
  323. INIT_LIST_HEAD(&dev->mode_config.property_list);
  324. INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
  325. INIT_LIST_HEAD(&dev->mode_config.plane_list);
  326. idr_init(&dev->mode_config.crtc_idr);
  327. idr_init(&dev->mode_config.tile_idr);
  328. ida_init(&dev->mode_config.connector_ida);
  329. spin_lock_init(&dev->mode_config.connector_list_lock);
  330. drm_mode_create_standard_properties(dev);
  331. /* Just to be sure */
  332. dev->mode_config.num_fb = 0;
  333. dev->mode_config.num_connector = 0;
  334. dev->mode_config.num_crtc = 0;
  335. dev->mode_config.num_encoder = 0;
  336. dev->mode_config.num_overlay_plane = 0;
  337. dev->mode_config.num_total_plane = 0;
  338. }
  339. EXPORT_SYMBOL(drm_mode_config_init);
  340. /**
  341. * drm_mode_config_cleanup - free up DRM mode_config info
  342. * @dev: DRM device
  343. *
  344. * Free up all the connectors and CRTCs associated with this DRM device, then
  345. * free up the framebuffers and associated buffer objects.
  346. *
  347. * Note that since this /should/ happen single-threaded at driver/device
  348. * teardown time, no locking is required. It's the driver's job to ensure that
  349. * this guarantee actually holds true.
  350. *
  351. * FIXME: cleanup any dangling user buffer objects too
  352. */
  353. void drm_mode_config_cleanup(struct drm_device *dev)
  354. {
  355. struct drm_connector *connector;
  356. struct drm_connector_list_iter conn_iter;
  357. struct drm_crtc *crtc, *ct;
  358. struct drm_encoder *encoder, *enct;
  359. struct drm_framebuffer *fb, *fbt;
  360. struct drm_property *property, *pt;
  361. struct drm_property_blob *blob, *bt;
  362. struct drm_plane *plane, *plt;
  363. list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
  364. head) {
  365. encoder->funcs->destroy(encoder);
  366. }
  367. drm_connector_list_iter_begin(dev, &conn_iter);
  368. drm_for_each_connector_iter(connector, &conn_iter) {
  369. /* drm_connector_list_iter holds an full reference to the
  370. * current connector itself, which means it is inherently safe
  371. * against unreferencing the current connector - but not against
  372. * deleting it right away. */
  373. drm_connector_put(connector);
  374. }
  375. drm_connector_list_iter_end(&conn_iter);
  376. if (WARN_ON(!list_empty(&dev->mode_config.connector_list))) {
  377. drm_connector_list_iter_begin(dev, &conn_iter);
  378. drm_for_each_connector_iter(connector, &conn_iter)
  379. DRM_ERROR("connector %s leaked!\n", connector->name);
  380. drm_connector_list_iter_end(&conn_iter);
  381. }
  382. list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
  383. head) {
  384. drm_property_destroy(dev, property);
  385. }
  386. list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
  387. head) {
  388. plane->funcs->destroy(plane);
  389. }
  390. list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
  391. crtc->funcs->destroy(crtc);
  392. }
  393. list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
  394. head_global) {
  395. drm_property_blob_put(blob);
  396. }
  397. /*
  398. * Single-threaded teardown context, so it's not required to grab the
  399. * fb_lock to protect against concurrent fb_list access. Contrary, it
  400. * would actually deadlock with the drm_framebuffer_cleanup function.
  401. *
  402. * Also, if there are any framebuffers left, that's a driver leak now,
  403. * so politely WARN about this.
  404. */
  405. WARN_ON(!list_empty(&dev->mode_config.fb_list));
  406. list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
  407. drm_framebuffer_free(&fb->base.refcount);
  408. }
  409. ida_destroy(&dev->mode_config.connector_ida);
  410. idr_destroy(&dev->mode_config.tile_idr);
  411. idr_destroy(&dev->mode_config.crtc_idr);
  412. drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
  413. }
  414. EXPORT_SYMBOL(drm_mode_config_cleanup);