drm_mode_config.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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/uaccess.h>
  23. #include <drm/drm_drv.h>
  24. #include <drm/drm_encoder.h>
  25. #include <drm/drm_file.h>
  26. #include <drm/drm_framebuffer.h>
  27. #include <drm/drm_managed.h>
  28. #include <drm/drm_mode_config.h>
  29. #include <drm/drm_print.h>
  30. #include <linux/dma-resv.h>
  31. #include "drm_crtc_internal.h"
  32. #include "drm_internal.h"
  33. int drm_modeset_register_all(struct drm_device *dev)
  34. {
  35. int ret;
  36. ret = drm_plane_register_all(dev);
  37. if (ret)
  38. goto err_plane;
  39. ret = drm_crtc_register_all(dev);
  40. if (ret)
  41. goto err_crtc;
  42. ret = drm_encoder_register_all(dev);
  43. if (ret)
  44. goto err_encoder;
  45. ret = drm_connector_register_all(dev);
  46. if (ret)
  47. goto err_connector;
  48. return 0;
  49. err_connector:
  50. drm_encoder_unregister_all(dev);
  51. err_encoder:
  52. drm_crtc_unregister_all(dev);
  53. err_crtc:
  54. drm_plane_unregister_all(dev);
  55. err_plane:
  56. return ret;
  57. }
  58. void drm_modeset_unregister_all(struct drm_device *dev)
  59. {
  60. drm_connector_unregister_all(dev);
  61. drm_encoder_unregister_all(dev);
  62. drm_crtc_unregister_all(dev);
  63. drm_plane_unregister_all(dev);
  64. }
  65. /**
  66. * drm_mode_getresources - get graphics configuration
  67. * @dev: drm device for the ioctl
  68. * @data: data pointer for the ioctl
  69. * @file_priv: drm file for the ioctl call
  70. *
  71. * Construct a set of configuration description structures and return
  72. * them to the user, including CRTC, connector and framebuffer configuration.
  73. *
  74. * Called by the user via ioctl.
  75. *
  76. * Returns:
  77. * Zero on success, negative errno on failure.
  78. */
  79. int drm_mode_getresources(struct drm_device *dev, void *data,
  80. struct drm_file *file_priv)
  81. {
  82. struct drm_mode_card_res *card_res = data;
  83. struct drm_framebuffer *fb;
  84. struct drm_connector *connector;
  85. struct drm_crtc *crtc;
  86. struct drm_encoder *encoder;
  87. int count, ret = 0;
  88. uint32_t __user *fb_id;
  89. uint32_t __user *crtc_id;
  90. uint32_t __user *connector_id;
  91. uint32_t __user *encoder_id;
  92. struct drm_connector_list_iter conn_iter;
  93. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  94. return -EOPNOTSUPP;
  95. mutex_lock(&file_priv->fbs_lock);
  96. count = 0;
  97. fb_id = u64_to_user_ptr(card_res->fb_id_ptr);
  98. list_for_each_entry(fb, &file_priv->fbs, filp_head) {
  99. if (count < card_res->count_fbs &&
  100. put_user(fb->base.id, fb_id + count)) {
  101. mutex_unlock(&file_priv->fbs_lock);
  102. return -EFAULT;
  103. }
  104. count++;
  105. }
  106. card_res->count_fbs = count;
  107. mutex_unlock(&file_priv->fbs_lock);
  108. card_res->max_height = dev->mode_config.max_height;
  109. card_res->min_height = dev->mode_config.min_height;
  110. card_res->max_width = dev->mode_config.max_width;
  111. card_res->min_width = dev->mode_config.min_width;
  112. count = 0;
  113. crtc_id = u64_to_user_ptr(card_res->crtc_id_ptr);
  114. drm_for_each_crtc(crtc, dev) {
  115. if (drm_lease_held(file_priv, crtc->base.id)) {
  116. if (count < card_res->count_crtcs &&
  117. put_user(crtc->base.id, crtc_id + count))
  118. return -EFAULT;
  119. count++;
  120. }
  121. }
  122. card_res->count_crtcs = count;
  123. count = 0;
  124. encoder_id = u64_to_user_ptr(card_res->encoder_id_ptr);
  125. drm_for_each_encoder(encoder, dev) {
  126. if (count < card_res->count_encoders &&
  127. put_user(encoder->base.id, encoder_id + count))
  128. return -EFAULT;
  129. count++;
  130. }
  131. card_res->count_encoders = count;
  132. drm_connector_list_iter_begin(dev, &conn_iter);
  133. count = 0;
  134. connector_id = u64_to_user_ptr(card_res->connector_id_ptr);
  135. drm_for_each_connector_iter(connector, &conn_iter) {
  136. /* only expose writeback connectors if userspace understands them */
  137. if (!file_priv->writeback_connectors &&
  138. (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK))
  139. continue;
  140. if (drm_lease_held(file_priv, connector->base.id)) {
  141. if (count < card_res->count_connectors &&
  142. put_user(connector->base.id, connector_id + count)) {
  143. drm_connector_list_iter_end(&conn_iter);
  144. return -EFAULT;
  145. }
  146. count++;
  147. }
  148. }
  149. card_res->count_connectors = count;
  150. drm_connector_list_iter_end(&conn_iter);
  151. return ret;
  152. }
  153. /**
  154. * drm_mode_config_reset - call ->reset callbacks
  155. * @dev: drm device
  156. *
  157. * This functions calls all the crtc's, encoder's and connector's ->reset
  158. * callback. Drivers can use this in e.g. their driver load or resume code to
  159. * reset hardware and software state.
  160. */
  161. void drm_mode_config_reset(struct drm_device *dev)
  162. {
  163. struct drm_crtc *crtc;
  164. struct drm_plane *plane;
  165. struct drm_encoder *encoder;
  166. struct drm_connector *connector;
  167. struct drm_connector_list_iter conn_iter;
  168. drm_for_each_plane(plane, dev)
  169. if (plane->funcs->reset)
  170. plane->funcs->reset(plane);
  171. drm_for_each_crtc(crtc, dev)
  172. if (crtc->funcs->reset)
  173. crtc->funcs->reset(crtc);
  174. drm_for_each_encoder(encoder, dev)
  175. if (encoder->funcs && encoder->funcs->reset)
  176. encoder->funcs->reset(encoder);
  177. drm_connector_list_iter_begin(dev, &conn_iter);
  178. drm_for_each_connector_iter(connector, &conn_iter)
  179. if (connector->funcs->reset)
  180. connector->funcs->reset(connector);
  181. drm_connector_list_iter_end(&conn_iter);
  182. }
  183. EXPORT_SYMBOL(drm_mode_config_reset);
  184. /*
  185. * Global properties
  186. */
  187. static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
  188. { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
  189. { DRM_PLANE_TYPE_PRIMARY, "Primary" },
  190. { DRM_PLANE_TYPE_CURSOR, "Cursor" },
  191. };
  192. static int drm_mode_create_standard_properties(struct drm_device *dev)
  193. {
  194. struct drm_property *prop;
  195. int ret;
  196. ret = drm_connector_create_standard_properties(dev);
  197. if (ret)
  198. return ret;
  199. prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  200. "type", drm_plane_type_enum_list,
  201. ARRAY_SIZE(drm_plane_type_enum_list));
  202. if (!prop)
  203. return -ENOMEM;
  204. dev->mode_config.plane_type_property = prop;
  205. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  206. "SRC_X", 0, UINT_MAX);
  207. if (!prop)
  208. return -ENOMEM;
  209. dev->mode_config.prop_src_x = prop;
  210. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  211. "SRC_Y", 0, UINT_MAX);
  212. if (!prop)
  213. return -ENOMEM;
  214. dev->mode_config.prop_src_y = prop;
  215. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  216. "SRC_W", 0, UINT_MAX);
  217. if (!prop)
  218. return -ENOMEM;
  219. dev->mode_config.prop_src_w = prop;
  220. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  221. "SRC_H", 0, UINT_MAX);
  222. if (!prop)
  223. return -ENOMEM;
  224. dev->mode_config.prop_src_h = prop;
  225. prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
  226. "CRTC_X", INT_MIN, INT_MAX);
  227. if (!prop)
  228. return -ENOMEM;
  229. dev->mode_config.prop_crtc_x = prop;
  230. prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
  231. "CRTC_Y", INT_MIN, INT_MAX);
  232. if (!prop)
  233. return -ENOMEM;
  234. dev->mode_config.prop_crtc_y = prop;
  235. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  236. "CRTC_W", 0, INT_MAX);
  237. if (!prop)
  238. return -ENOMEM;
  239. dev->mode_config.prop_crtc_w = prop;
  240. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  241. "CRTC_H", 0, INT_MAX);
  242. if (!prop)
  243. return -ENOMEM;
  244. dev->mode_config.prop_crtc_h = prop;
  245. prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
  246. "FB_ID", DRM_MODE_OBJECT_FB);
  247. if (!prop)
  248. return -ENOMEM;
  249. dev->mode_config.prop_fb_id = prop;
  250. prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
  251. "IN_FENCE_FD", -1, INT_MAX);
  252. if (!prop)
  253. return -ENOMEM;
  254. dev->mode_config.prop_in_fence_fd = prop;
  255. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  256. "OUT_FENCE_PTR", 0, U64_MAX);
  257. if (!prop)
  258. return -ENOMEM;
  259. dev->mode_config.prop_out_fence_ptr = prop;
  260. prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
  261. "CRTC_ID", DRM_MODE_OBJECT_CRTC);
  262. if (!prop)
  263. return -ENOMEM;
  264. dev->mode_config.prop_crtc_id = prop;
  265. prop = drm_property_create(dev,
  266. DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
  267. "FB_DAMAGE_CLIPS", 0);
  268. if (!prop)
  269. return -ENOMEM;
  270. dev->mode_config.prop_fb_damage_clips = prop;
  271. prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
  272. "ACTIVE");
  273. if (!prop)
  274. return -ENOMEM;
  275. dev->mode_config.prop_active = prop;
  276. prop = drm_property_create(dev,
  277. DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
  278. "MODE_ID", 0);
  279. if (!prop)
  280. return -ENOMEM;
  281. dev->mode_config.prop_mode_id = prop;
  282. prop = drm_property_create_bool(dev, 0,
  283. "VRR_ENABLED");
  284. if (!prop)
  285. return -ENOMEM;
  286. dev->mode_config.prop_vrr_enabled = prop;
  287. prop = drm_property_create(dev,
  288. DRM_MODE_PROP_BLOB,
  289. "DEGAMMA_LUT", 0);
  290. if (!prop)
  291. return -ENOMEM;
  292. dev->mode_config.degamma_lut_property = prop;
  293. prop = drm_property_create_range(dev,
  294. DRM_MODE_PROP_IMMUTABLE,
  295. "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
  296. if (!prop)
  297. return -ENOMEM;
  298. dev->mode_config.degamma_lut_size_property = prop;
  299. prop = drm_property_create(dev,
  300. DRM_MODE_PROP_BLOB,
  301. "CTM", 0);
  302. if (!prop)
  303. return -ENOMEM;
  304. dev->mode_config.ctm_property = prop;
  305. prop = drm_property_create(dev,
  306. DRM_MODE_PROP_BLOB,
  307. "GAMMA_LUT", 0);
  308. if (!prop)
  309. return -ENOMEM;
  310. dev->mode_config.gamma_lut_property = prop;
  311. prop = drm_property_create_range(dev,
  312. DRM_MODE_PROP_IMMUTABLE,
  313. "GAMMA_LUT_SIZE", 0, UINT_MAX);
  314. if (!prop)
  315. return -ENOMEM;
  316. dev->mode_config.gamma_lut_size_property = prop;
  317. prop = drm_property_create(dev,
  318. DRM_MODE_PROP_IMMUTABLE | DRM_MODE_PROP_BLOB,
  319. "IN_FORMATS", 0);
  320. if (!prop)
  321. return -ENOMEM;
  322. dev->mode_config.modifiers_property = prop;
  323. return 0;
  324. }
  325. static void drm_mode_config_init_release(struct drm_device *dev, void *ptr)
  326. {
  327. drm_mode_config_cleanup(dev);
  328. }
  329. /**
  330. * drmm_mode_config_init - managed DRM mode_configuration structure
  331. * initialization
  332. * @dev: DRM device
  333. *
  334. * Initialize @dev's mode_config structure, used for tracking the graphics
  335. * configuration of @dev.
  336. *
  337. * Since this initializes the modeset locks, no locking is possible. Which is no
  338. * problem, since this should happen single threaded at init time. It is the
  339. * driver's problem to ensure this guarantee.
  340. *
  341. * Cleanup is automatically handled through registering drm_mode_config_cleanup
  342. * with drmm_add_action().
  343. *
  344. * Returns: 0 on success, negative error value on failure.
  345. */
  346. int drmm_mode_config_init(struct drm_device *dev)
  347. {
  348. int ret;
  349. mutex_init(&dev->mode_config.mutex);
  350. drm_modeset_lock_init(&dev->mode_config.connection_mutex);
  351. mutex_init(&dev->mode_config.idr_mutex);
  352. mutex_init(&dev->mode_config.fb_lock);
  353. mutex_init(&dev->mode_config.blob_lock);
  354. INIT_LIST_HEAD(&dev->mode_config.fb_list);
  355. INIT_LIST_HEAD(&dev->mode_config.crtc_list);
  356. INIT_LIST_HEAD(&dev->mode_config.connector_list);
  357. INIT_LIST_HEAD(&dev->mode_config.encoder_list);
  358. INIT_LIST_HEAD(&dev->mode_config.property_list);
  359. INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
  360. INIT_LIST_HEAD(&dev->mode_config.plane_list);
  361. INIT_LIST_HEAD(&dev->mode_config.privobj_list);
  362. idr_init_base(&dev->mode_config.object_idr, 1);
  363. idr_init_base(&dev->mode_config.tile_idr, 1);
  364. ida_init(&dev->mode_config.connector_ida);
  365. spin_lock_init(&dev->mode_config.connector_list_lock);
  366. init_llist_head(&dev->mode_config.connector_free_list);
  367. INIT_WORK(&dev->mode_config.connector_free_work, drm_connector_free_work_fn);
  368. ret = drm_mode_create_standard_properties(dev);
  369. if (ret) {
  370. drm_mode_config_cleanup(dev);
  371. return ret;
  372. }
  373. /* Just to be sure */
  374. dev->mode_config.num_fb = 0;
  375. dev->mode_config.num_connector = 0;
  376. dev->mode_config.num_crtc = 0;
  377. dev->mode_config.num_encoder = 0;
  378. dev->mode_config.num_total_plane = 0;
  379. if (IS_ENABLED(CONFIG_LOCKDEP)) {
  380. struct drm_modeset_acquire_ctx modeset_ctx;
  381. struct ww_acquire_ctx resv_ctx;
  382. struct dma_resv resv;
  383. int ret;
  384. dma_resv_init(&resv);
  385. drm_modeset_acquire_init(&modeset_ctx, 0);
  386. ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
  387. &modeset_ctx);
  388. if (ret == -EDEADLK)
  389. ret = drm_modeset_backoff(&modeset_ctx);
  390. ww_acquire_init(&resv_ctx, &reservation_ww_class);
  391. ret = dma_resv_lock(&resv, &resv_ctx);
  392. if (ret == -EDEADLK)
  393. dma_resv_lock_slow(&resv, &resv_ctx);
  394. dma_resv_unlock(&resv);
  395. ww_acquire_fini(&resv_ctx);
  396. drm_modeset_drop_locks(&modeset_ctx);
  397. drm_modeset_acquire_fini(&modeset_ctx);
  398. dma_resv_fini(&resv);
  399. }
  400. return drmm_add_action_or_reset(dev, drm_mode_config_init_release,
  401. NULL);
  402. }
  403. EXPORT_SYMBOL(drmm_mode_config_init);
  404. /**
  405. * drm_mode_config_cleanup - free up DRM mode_config info
  406. * @dev: DRM device
  407. *
  408. * Free up all the connectors and CRTCs associated with this DRM device, then
  409. * free up the framebuffers and associated buffer objects.
  410. *
  411. * Note that since this /should/ happen single-threaded at driver/device
  412. * teardown time, no locking is required. It's the driver's job to ensure that
  413. * this guarantee actually holds true.
  414. *
  415. * FIXME: With the managed drmm_mode_config_init() it is no longer necessary for
  416. * drivers to explicitly call this function.
  417. */
  418. void drm_mode_config_cleanup(struct drm_device *dev)
  419. {
  420. struct drm_connector *connector;
  421. struct drm_connector_list_iter conn_iter;
  422. struct drm_crtc *crtc, *ct;
  423. struct drm_encoder *encoder, *enct;
  424. struct drm_framebuffer *fb, *fbt;
  425. struct drm_property *property, *pt;
  426. struct drm_property_blob *blob, *bt;
  427. struct drm_plane *plane, *plt;
  428. list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
  429. head) {
  430. encoder->funcs->destroy(encoder);
  431. }
  432. drm_connector_list_iter_begin(dev, &conn_iter);
  433. drm_for_each_connector_iter(connector, &conn_iter) {
  434. /* drm_connector_list_iter holds an full reference to the
  435. * current connector itself, which means it is inherently safe
  436. * against unreferencing the current connector - but not against
  437. * deleting it right away. */
  438. drm_connector_put(connector);
  439. }
  440. drm_connector_list_iter_end(&conn_iter);
  441. /* connector_iter drops references in a work item. */
  442. flush_work(&dev->mode_config.connector_free_work);
  443. if (WARN_ON(!list_empty(&dev->mode_config.connector_list))) {
  444. drm_connector_list_iter_begin(dev, &conn_iter);
  445. drm_for_each_connector_iter(connector, &conn_iter)
  446. DRM_ERROR("connector %s leaked!\n", connector->name);
  447. drm_connector_list_iter_end(&conn_iter);
  448. }
  449. list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
  450. head) {
  451. drm_property_destroy(dev, property);
  452. }
  453. list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
  454. head) {
  455. plane->funcs->destroy(plane);
  456. }
  457. list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
  458. crtc->funcs->destroy(crtc);
  459. }
  460. list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
  461. head_global) {
  462. drm_property_blob_put(blob);
  463. }
  464. /*
  465. * Single-threaded teardown context, so it's not required to grab the
  466. * fb_lock to protect against concurrent fb_list access. Contrary, it
  467. * would actually deadlock with the drm_framebuffer_cleanup function.
  468. *
  469. * Also, if there are any framebuffers left, that's a driver leak now,
  470. * so politely WARN about this.
  471. */
  472. WARN_ON(!list_empty(&dev->mode_config.fb_list));
  473. list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
  474. struct drm_printer p = drm_debug_printer("[leaked fb]");
  475. drm_printf(&p, "framebuffer[%u]:\n", fb->base.id);
  476. drm_framebuffer_print_info(&p, 1, fb);
  477. drm_framebuffer_free(&fb->base.refcount);
  478. }
  479. ida_destroy(&dev->mode_config.connector_ida);
  480. idr_destroy(&dev->mode_config.tile_idr);
  481. idr_destroy(&dev->mode_config.object_idr);
  482. drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
  483. }
  484. EXPORT_SYMBOL(drm_mode_config_cleanup);
  485. static u32 full_encoder_mask(struct drm_device *dev)
  486. {
  487. struct drm_encoder *encoder;
  488. u32 encoder_mask = 0;
  489. drm_for_each_encoder(encoder, dev)
  490. encoder_mask |= drm_encoder_mask(encoder);
  491. return encoder_mask;
  492. }
  493. /*
  494. * For some reason we want the encoder itself included in
  495. * possible_clones. Make life easy for drivers by allowing them
  496. * to leave possible_clones unset if no cloning is possible.
  497. */
  498. static void fixup_encoder_possible_clones(struct drm_encoder *encoder)
  499. {
  500. if (encoder->possible_clones == 0)
  501. encoder->possible_clones = drm_encoder_mask(encoder);
  502. }
  503. static void validate_encoder_possible_clones(struct drm_encoder *encoder)
  504. {
  505. struct drm_device *dev = encoder->dev;
  506. u32 encoder_mask = full_encoder_mask(dev);
  507. struct drm_encoder *other;
  508. drm_for_each_encoder(other, dev) {
  509. WARN(!!(encoder->possible_clones & drm_encoder_mask(other)) !=
  510. !!(other->possible_clones & drm_encoder_mask(encoder)),
  511. "possible_clones mismatch: "
  512. "[ENCODER:%d:%s] mask=0x%x possible_clones=0x%x vs. "
  513. "[ENCODER:%d:%s] mask=0x%x possible_clones=0x%x\n",
  514. encoder->base.id, encoder->name,
  515. drm_encoder_mask(encoder), encoder->possible_clones,
  516. other->base.id, other->name,
  517. drm_encoder_mask(other), other->possible_clones);
  518. }
  519. WARN((encoder->possible_clones & drm_encoder_mask(encoder)) == 0 ||
  520. (encoder->possible_clones & ~encoder_mask) != 0,
  521. "Bogus possible_clones: "
  522. "[ENCODER:%d:%s] possible_clones=0x%x (full encoder mask=0x%x)\n",
  523. encoder->base.id, encoder->name,
  524. encoder->possible_clones, encoder_mask);
  525. }
  526. static u32 full_crtc_mask(struct drm_device *dev)
  527. {
  528. struct drm_crtc *crtc;
  529. u32 crtc_mask = 0;
  530. drm_for_each_crtc(crtc, dev)
  531. crtc_mask |= drm_crtc_mask(crtc);
  532. return crtc_mask;
  533. }
  534. static void validate_encoder_possible_crtcs(struct drm_encoder *encoder)
  535. {
  536. u32 crtc_mask = full_crtc_mask(encoder->dev);
  537. WARN((encoder->possible_crtcs & crtc_mask) == 0 ||
  538. (encoder->possible_crtcs & ~crtc_mask) != 0,
  539. "Bogus possible_crtcs: "
  540. "[ENCODER:%d:%s] possible_crtcs=0x%x (full crtc mask=0x%x)\n",
  541. encoder->base.id, encoder->name,
  542. encoder->possible_crtcs, crtc_mask);
  543. }
  544. void drm_mode_config_validate(struct drm_device *dev)
  545. {
  546. struct drm_encoder *encoder;
  547. struct drm_crtc *crtc;
  548. struct drm_plane *plane;
  549. u32 primary_with_crtc = 0, cursor_with_crtc = 0;
  550. unsigned int num_primary = 0;
  551. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  552. return;
  553. drm_for_each_encoder(encoder, dev)
  554. fixup_encoder_possible_clones(encoder);
  555. drm_for_each_encoder(encoder, dev) {
  556. validate_encoder_possible_clones(encoder);
  557. validate_encoder_possible_crtcs(encoder);
  558. }
  559. drm_for_each_crtc(crtc, dev) {
  560. WARN(!crtc->primary, "Missing primary plane on [CRTC:%d:%s]\n",
  561. crtc->base.id, crtc->name);
  562. WARN(crtc->cursor && crtc->funcs->cursor_set,
  563. "[CRTC:%d:%s] must not have both a cursor plane and a cursor_set func",
  564. crtc->base.id, crtc->name);
  565. WARN(crtc->cursor && crtc->funcs->cursor_set2,
  566. "[CRTC:%d:%s] must not have both a cursor plane and a cursor_set2 func",
  567. crtc->base.id, crtc->name);
  568. WARN(crtc->cursor && crtc->funcs->cursor_move,
  569. "[CRTC:%d:%s] must not have both a cursor plane and a cursor_move func",
  570. crtc->base.id, crtc->name);
  571. if (crtc->primary) {
  572. WARN(!(crtc->primary->possible_crtcs & drm_crtc_mask(crtc)),
  573. "Bogus primary plane possible_crtcs: [PLANE:%d:%s] must be compatible with [CRTC:%d:%s]\n",
  574. crtc->primary->base.id, crtc->primary->name,
  575. crtc->base.id, crtc->name);
  576. WARN(primary_with_crtc & drm_plane_mask(crtc->primary),
  577. "Primary plane [PLANE:%d:%s] used for multiple CRTCs",
  578. crtc->primary->base.id, crtc->primary->name);
  579. primary_with_crtc |= drm_plane_mask(crtc->primary);
  580. }
  581. if (crtc->cursor) {
  582. WARN(!(crtc->cursor->possible_crtcs & drm_crtc_mask(crtc)),
  583. "Bogus cursor plane possible_crtcs: [PLANE:%d:%s] must be compatible with [CRTC:%d:%s]\n",
  584. crtc->cursor->base.id, crtc->cursor->name,
  585. crtc->base.id, crtc->name);
  586. WARN(cursor_with_crtc & drm_plane_mask(crtc->cursor),
  587. "Cursor plane [PLANE:%d:%s] used for multiple CRTCs",
  588. crtc->cursor->base.id, crtc->cursor->name);
  589. cursor_with_crtc |= drm_plane_mask(crtc->cursor);
  590. }
  591. }
  592. drm_for_each_plane(plane, dev) {
  593. if (plane->type == DRM_PLANE_TYPE_PRIMARY)
  594. num_primary++;
  595. }
  596. WARN(num_primary != dev->mode_config.num_crtc,
  597. "Must have as many primary planes as there are CRTCs, but have %u primary planes and %u CRTCs",
  598. num_primary, dev->mode_config.num_crtc);
  599. }