drm_mode_config.c 13 KB

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