drm_atomic.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*
  2. * Copyright (C) 2014 Red Hat
  3. * Copyright (C) 2014 Intel Corp.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Rob Clark <robdclark@gmail.com>
  25. * Daniel Vetter <daniel.vetter@ffwll.ch>
  26. */
  27. #include <drm/drmP.h>
  28. #include <drm/drm_atomic.h>
  29. #include <drm/drm_plane_helper.h>
  30. static void kfree_state(struct drm_atomic_state *state)
  31. {
  32. kfree(state->connectors);
  33. kfree(state->connector_states);
  34. kfree(state->crtcs);
  35. kfree(state->crtc_states);
  36. kfree(state->planes);
  37. kfree(state->plane_states);
  38. kfree(state);
  39. }
  40. /**
  41. * drm_atomic_state_alloc - allocate atomic state
  42. * @dev: DRM device
  43. *
  44. * This allocates an empty atomic state to track updates.
  45. */
  46. struct drm_atomic_state *
  47. drm_atomic_state_alloc(struct drm_device *dev)
  48. {
  49. struct drm_atomic_state *state;
  50. state = kzalloc(sizeof(*state), GFP_KERNEL);
  51. if (!state)
  52. return NULL;
  53. state->crtcs = kcalloc(dev->mode_config.num_crtc,
  54. sizeof(*state->crtcs), GFP_KERNEL);
  55. if (!state->crtcs)
  56. goto fail;
  57. state->crtc_states = kcalloc(dev->mode_config.num_crtc,
  58. sizeof(*state->crtc_states), GFP_KERNEL);
  59. if (!state->crtc_states)
  60. goto fail;
  61. state->planes = kcalloc(dev->mode_config.num_total_plane,
  62. sizeof(*state->planes), GFP_KERNEL);
  63. if (!state->planes)
  64. goto fail;
  65. state->plane_states = kcalloc(dev->mode_config.num_total_plane,
  66. sizeof(*state->plane_states), GFP_KERNEL);
  67. if (!state->plane_states)
  68. goto fail;
  69. state->connectors = kcalloc(dev->mode_config.num_connector,
  70. sizeof(*state->connectors),
  71. GFP_KERNEL);
  72. if (!state->connectors)
  73. goto fail;
  74. state->connector_states = kcalloc(dev->mode_config.num_connector,
  75. sizeof(*state->connector_states),
  76. GFP_KERNEL);
  77. if (!state->connector_states)
  78. goto fail;
  79. state->dev = dev;
  80. DRM_DEBUG_KMS("Allocate atomic state %p\n", state);
  81. return state;
  82. fail:
  83. kfree_state(state);
  84. return NULL;
  85. }
  86. EXPORT_SYMBOL(drm_atomic_state_alloc);
  87. /**
  88. * drm_atomic_state_clear - clear state object
  89. * @state: atomic state
  90. *
  91. * When the w/w mutex algorithm detects a deadlock we need to back off and drop
  92. * all locks. So someone else could sneak in and change the current modeset
  93. * configuration. Which means that all the state assembled in @state is no
  94. * longer an atomic update to the current state, but to some arbitrary earlier
  95. * state. Which could break assumptions the driver's ->atomic_check likely
  96. * relies on.
  97. *
  98. * Hence we must clear all cached state and completely start over, using this
  99. * function.
  100. */
  101. void drm_atomic_state_clear(struct drm_atomic_state *state)
  102. {
  103. struct drm_device *dev = state->dev;
  104. int i;
  105. DRM_DEBUG_KMS("Clearing atomic state %p\n", state);
  106. for (i = 0; i < dev->mode_config.num_connector; i++) {
  107. struct drm_connector *connector = state->connectors[i];
  108. if (!connector)
  109. continue;
  110. connector->funcs->atomic_destroy_state(connector,
  111. state->connector_states[i]);
  112. }
  113. for (i = 0; i < dev->mode_config.num_crtc; i++) {
  114. struct drm_crtc *crtc = state->crtcs[i];
  115. if (!crtc)
  116. continue;
  117. crtc->funcs->atomic_destroy_state(crtc,
  118. state->crtc_states[i]);
  119. }
  120. for (i = 0; i < dev->mode_config.num_total_plane; i++) {
  121. struct drm_plane *plane = state->planes[i];
  122. if (!plane)
  123. continue;
  124. plane->funcs->atomic_destroy_state(plane,
  125. state->plane_states[i]);
  126. }
  127. }
  128. EXPORT_SYMBOL(drm_atomic_state_clear);
  129. /**
  130. * drm_atomic_state_free - free all memory for an atomic state
  131. * @state: atomic state to deallocate
  132. *
  133. * This frees all memory associated with an atomic state, including all the
  134. * per-object state for planes, crtcs and connectors.
  135. */
  136. void drm_atomic_state_free(struct drm_atomic_state *state)
  137. {
  138. drm_atomic_state_clear(state);
  139. DRM_DEBUG_KMS("Freeing atomic state %p\n", state);
  140. kfree_state(state);
  141. }
  142. EXPORT_SYMBOL(drm_atomic_state_free);
  143. /**
  144. * drm_atomic_get_crtc_state - get crtc state
  145. * @state: global atomic state object
  146. * @crtc: crtc to get state object for
  147. *
  148. * This function returns the crtc state for the given crtc, allocating it if
  149. * needed. It will also grab the relevant crtc lock to make sure that the state
  150. * is consistent.
  151. *
  152. * Returns:
  153. *
  154. * Either the allocated state or the error code encoded into the pointer. When
  155. * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
  156. * entire atomic sequence must be restarted. All other errors are fatal.
  157. */
  158. struct drm_crtc_state *
  159. drm_atomic_get_crtc_state(struct drm_atomic_state *state,
  160. struct drm_crtc *crtc)
  161. {
  162. int ret, index;
  163. struct drm_crtc_state *crtc_state;
  164. index = drm_crtc_index(crtc);
  165. if (state->crtc_states[index])
  166. return state->crtc_states[index];
  167. ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
  168. if (ret)
  169. return ERR_PTR(ret);
  170. crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
  171. if (!crtc_state)
  172. return ERR_PTR(-ENOMEM);
  173. state->crtc_states[index] = crtc_state;
  174. state->crtcs[index] = crtc;
  175. crtc_state->state = state;
  176. DRM_DEBUG_KMS("Added [CRTC:%d] %p state to %p\n",
  177. crtc->base.id, crtc_state, state);
  178. return crtc_state;
  179. }
  180. EXPORT_SYMBOL(drm_atomic_get_crtc_state);
  181. /**
  182. * drm_atomic_get_plane_state - get plane state
  183. * @state: global atomic state object
  184. * @plane: plane to get state object for
  185. *
  186. * This function returns the plane state for the given plane, allocating it if
  187. * needed. It will also grab the relevant plane lock to make sure that the state
  188. * is consistent.
  189. *
  190. * Returns:
  191. *
  192. * Either the allocated state or the error code encoded into the pointer. When
  193. * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
  194. * entire atomic sequence must be restarted. All other errors are fatal.
  195. */
  196. struct drm_plane_state *
  197. drm_atomic_get_plane_state(struct drm_atomic_state *state,
  198. struct drm_plane *plane)
  199. {
  200. int ret, index;
  201. struct drm_plane_state *plane_state;
  202. index = drm_plane_index(plane);
  203. if (state->plane_states[index])
  204. return state->plane_states[index];
  205. /*
  206. * TODO: We currently don't have per-plane mutexes. So instead of trying
  207. * crazy tricks with deferring plane->crtc and hoping for the best just
  208. * grab all crtc locks. Once we have per-plane locks we must update this
  209. * to only take the plane mutex.
  210. */
  211. ret = drm_modeset_lock_all_crtcs(state->dev, state->acquire_ctx);
  212. if (ret)
  213. return ERR_PTR(ret);
  214. plane_state = plane->funcs->atomic_duplicate_state(plane);
  215. if (!plane_state)
  216. return ERR_PTR(-ENOMEM);
  217. state->plane_states[index] = plane_state;
  218. state->planes[index] = plane;
  219. plane_state->state = state;
  220. DRM_DEBUG_KMS("Added [PLANE:%d] %p state to %p\n",
  221. plane->base.id, plane_state, state);
  222. if (plane_state->crtc) {
  223. struct drm_crtc_state *crtc_state;
  224. crtc_state = drm_atomic_get_crtc_state(state,
  225. plane_state->crtc);
  226. if (IS_ERR(crtc_state))
  227. return ERR_CAST(crtc_state);
  228. }
  229. return plane_state;
  230. }
  231. EXPORT_SYMBOL(drm_atomic_get_plane_state);
  232. /**
  233. * drm_atomic_get_connector_state - get connector state
  234. * @state: global atomic state object
  235. * @connector: connector to get state object for
  236. *
  237. * This function returns the connector state for the given connector,
  238. * allocating it if needed. It will also grab the relevant connector lock to
  239. * make sure that the state is consistent.
  240. *
  241. * Returns:
  242. *
  243. * Either the allocated state or the error code encoded into the pointer. When
  244. * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
  245. * entire atomic sequence must be restarted. All other errors are fatal.
  246. */
  247. struct drm_connector_state *
  248. drm_atomic_get_connector_state(struct drm_atomic_state *state,
  249. struct drm_connector *connector)
  250. {
  251. int ret, index;
  252. struct drm_mode_config *config = &connector->dev->mode_config;
  253. struct drm_connector_state *connector_state;
  254. index = drm_connector_index(connector);
  255. if (state->connector_states[index])
  256. return state->connector_states[index];
  257. ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
  258. if (ret)
  259. return ERR_PTR(ret);
  260. connector_state = connector->funcs->atomic_duplicate_state(connector);
  261. if (!connector_state)
  262. return ERR_PTR(-ENOMEM);
  263. state->connector_states[index] = connector_state;
  264. state->connectors[index] = connector;
  265. connector_state->state = state;
  266. DRM_DEBUG_KMS("Added [CONNECTOR:%d] %p state to %p\n",
  267. connector->base.id, connector_state, state);
  268. if (connector_state->crtc) {
  269. struct drm_crtc_state *crtc_state;
  270. crtc_state = drm_atomic_get_crtc_state(state,
  271. connector_state->crtc);
  272. if (IS_ERR(crtc_state))
  273. return ERR_CAST(crtc_state);
  274. }
  275. return connector_state;
  276. }
  277. EXPORT_SYMBOL(drm_atomic_get_connector_state);
  278. /**
  279. * drm_atomic_set_crtc_for_plane - set crtc for plane
  280. * @plane_state: atomic state object for the plane
  281. * @crtc: crtc to use for the plane
  282. *
  283. * Changing the assigned crtc for a plane requires us to grab the lock and state
  284. * for the new crtc, as needed. This function takes care of all these details
  285. * besides updating the pointer in the state object itself.
  286. *
  287. * Returns:
  288. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  289. * then the w/w mutex code has detected a deadlock and the entire atomic
  290. * sequence must be restarted. All other errors are fatal.
  291. */
  292. int
  293. drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
  294. struct drm_crtc *crtc)
  295. {
  296. struct drm_crtc_state *crtc_state;
  297. if (crtc) {
  298. crtc_state = drm_atomic_get_crtc_state(plane_state->state,
  299. crtc);
  300. if (IS_ERR(crtc_state))
  301. return PTR_ERR(crtc_state);
  302. }
  303. plane_state->crtc = crtc;
  304. if (crtc)
  305. DRM_DEBUG_KMS("Link plane state %p to [CRTC:%d]\n",
  306. plane_state, crtc->base.id);
  307. else
  308. DRM_DEBUG_KMS("Link plane state %p to [NOCRTC]\n", plane_state);
  309. return 0;
  310. }
  311. EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
  312. /**
  313. * drm_atomic_set_fb_for_plane - set crtc for plane
  314. * @plane_state: atomic state object for the plane
  315. * @fb: fb to use for the plane
  316. *
  317. * Changing the assigned framebuffer for a plane requires us to grab a reference
  318. * to the new fb and drop the reference to the old fb, if there is one. This
  319. * function takes care of all these details besides updating the pointer in the
  320. * state object itself.
  321. */
  322. void
  323. drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
  324. struct drm_framebuffer *fb)
  325. {
  326. if (plane_state->fb)
  327. drm_framebuffer_unreference(plane_state->fb);
  328. if (fb)
  329. drm_framebuffer_reference(fb);
  330. plane_state->fb = fb;
  331. if (fb)
  332. DRM_DEBUG_KMS("Set [FB:%d] for plane state %p\n",
  333. fb->base.id, plane_state);
  334. else
  335. DRM_DEBUG_KMS("Set [NOFB] for plane state %p\n", plane_state);
  336. }
  337. EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
  338. /**
  339. * drm_atomic_set_crtc_for_connector - set crtc for connector
  340. * @conn_state: atomic state object for the connector
  341. * @crtc: crtc to use for the connector
  342. *
  343. * Changing the assigned crtc for a connector requires us to grab the lock and
  344. * state for the new crtc, as needed. This function takes care of all these
  345. * details besides updating the pointer in the state object itself.
  346. *
  347. * Returns:
  348. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  349. * then the w/w mutex code has detected a deadlock and the entire atomic
  350. * sequence must be restarted. All other errors are fatal.
  351. */
  352. int
  353. drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
  354. struct drm_crtc *crtc)
  355. {
  356. struct drm_crtc_state *crtc_state;
  357. if (crtc) {
  358. crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
  359. if (IS_ERR(crtc_state))
  360. return PTR_ERR(crtc_state);
  361. }
  362. conn_state->crtc = crtc;
  363. if (crtc)
  364. DRM_DEBUG_KMS("Link connector state %p to [CRTC:%d]\n",
  365. conn_state, crtc->base.id);
  366. else
  367. DRM_DEBUG_KMS("Link connector state %p to [NOCRTC]\n",
  368. conn_state);
  369. return 0;
  370. }
  371. EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
  372. /**
  373. * drm_atomic_add_affected_connectors - add connectors for crtc
  374. * @state: atomic state
  375. * @crtc: DRM crtc
  376. *
  377. * This function walks the current configuration and adds all connectors
  378. * currently using @crtc to the atomic configuration @state. Note that this
  379. * function must acquire the connection mutex. This can potentially cause
  380. * unneeded seralization if the update is just for the planes on one crtc. Hence
  381. * drivers and helpers should only call this when really needed (e.g. when a
  382. * full modeset needs to happen due to some change).
  383. *
  384. * Returns:
  385. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  386. * then the w/w mutex code has detected a deadlock and the entire atomic
  387. * sequence must be restarted. All other errors are fatal.
  388. */
  389. int
  390. drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
  391. struct drm_crtc *crtc)
  392. {
  393. struct drm_mode_config *config = &state->dev->mode_config;
  394. struct drm_connector *connector;
  395. struct drm_connector_state *conn_state;
  396. int ret;
  397. ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
  398. if (ret)
  399. return ret;
  400. DRM_DEBUG_KMS("Adding all current connectors for [CRTC:%d] to %p\n",
  401. crtc->base.id, state);
  402. /*
  403. * Changed connectors are already in @state, so only need to look at the
  404. * current configuration.
  405. */
  406. list_for_each_entry(connector, &config->connector_list, head) {
  407. if (connector->state->crtc != crtc)
  408. continue;
  409. conn_state = drm_atomic_get_connector_state(state, connector);
  410. if (IS_ERR(conn_state))
  411. return PTR_ERR(conn_state);
  412. }
  413. return 0;
  414. }
  415. EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
  416. /**
  417. * drm_atomic_connectors_for_crtc - count number of connected outputs
  418. * @state: atomic state
  419. * @crtc: DRM crtc
  420. *
  421. * This function counts all connectors which will be connected to @crtc
  422. * according to @state. Useful to recompute the enable state for @crtc.
  423. */
  424. int
  425. drm_atomic_connectors_for_crtc(struct drm_atomic_state *state,
  426. struct drm_crtc *crtc)
  427. {
  428. int nconnectors = state->dev->mode_config.num_connector;
  429. int i, num_connected_connectors = 0;
  430. for (i = 0; i < nconnectors; i++) {
  431. struct drm_connector_state *conn_state;
  432. conn_state = state->connector_states[i];
  433. if (conn_state && conn_state->crtc == crtc)
  434. num_connected_connectors++;
  435. }
  436. DRM_DEBUG_KMS("State %p has %i connectors for [CRTC:%d]\n",
  437. state, num_connected_connectors, crtc->base.id);
  438. return num_connected_connectors;
  439. }
  440. EXPORT_SYMBOL(drm_atomic_connectors_for_crtc);
  441. /**
  442. * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
  443. * @state: atomic state
  444. *
  445. * This function should be used by legacy entry points which don't understand
  446. * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
  447. * the slowpath completed.
  448. */
  449. void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
  450. {
  451. int ret;
  452. retry:
  453. drm_modeset_backoff(state->acquire_ctx);
  454. ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
  455. state->acquire_ctx);
  456. if (ret)
  457. goto retry;
  458. ret = drm_modeset_lock_all_crtcs(state->dev,
  459. state->acquire_ctx);
  460. if (ret)
  461. goto retry;
  462. }
  463. EXPORT_SYMBOL(drm_atomic_legacy_backoff);
  464. /**
  465. * drm_atomic_check_only - check whether a given config would work
  466. * @state: atomic configuration to check
  467. *
  468. * Note that this function can return -EDEADLK if the driver needed to acquire
  469. * more locks but encountered a deadlock. The caller must then do the usual w/w
  470. * backoff dance and restart. All other errors are fatal.
  471. *
  472. * Returns:
  473. * 0 on success, negative error code on failure.
  474. */
  475. int drm_atomic_check_only(struct drm_atomic_state *state)
  476. {
  477. struct drm_mode_config *config = &state->dev->mode_config;
  478. DRM_DEBUG_KMS("checking %p\n", state);
  479. if (config->funcs->atomic_check)
  480. return config->funcs->atomic_check(state->dev, state);
  481. else
  482. return 0;
  483. }
  484. EXPORT_SYMBOL(drm_atomic_check_only);
  485. /**
  486. * drm_atomic_commit - commit configuration atomically
  487. * @state: atomic configuration to check
  488. *
  489. * Note that this function can return -EDEADLK if the driver needed to acquire
  490. * more locks but encountered a deadlock. The caller must then do the usual w/w
  491. * backoff dance and restart. All other errors are fatal.
  492. *
  493. * Also note that on successful execution ownership of @state is transferred
  494. * from the caller of this function to the function itself. The caller must not
  495. * free or in any other way access @state. If the function fails then the caller
  496. * must clean up @state itself.
  497. *
  498. * Returns:
  499. * 0 on success, negative error code on failure.
  500. */
  501. int drm_atomic_commit(struct drm_atomic_state *state)
  502. {
  503. struct drm_mode_config *config = &state->dev->mode_config;
  504. int ret;
  505. ret = drm_atomic_check_only(state);
  506. if (ret)
  507. return ret;
  508. DRM_DEBUG_KMS("commiting %p\n", state);
  509. return config->funcs->atomic_commit(state->dev, state, false);
  510. }
  511. EXPORT_SYMBOL(drm_atomic_commit);
  512. /**
  513. * drm_atomic_async_commit - atomic&async configuration commit
  514. * @state: atomic configuration to check
  515. *
  516. * Note that this function can return -EDEADLK if the driver needed to acquire
  517. * more locks but encountered a deadlock. The caller must then do the usual w/w
  518. * backoff dance and restart. All other errors are fatal.
  519. *
  520. * Also note that on successful execution ownership of @state is transferred
  521. * from the caller of this function to the function itself. The caller must not
  522. * free or in any other way access @state. If the function fails then the caller
  523. * must clean up @state itself.
  524. *
  525. * Returns:
  526. * 0 on success, negative error code on failure.
  527. */
  528. int drm_atomic_async_commit(struct drm_atomic_state *state)
  529. {
  530. struct drm_mode_config *config = &state->dev->mode_config;
  531. int ret;
  532. ret = drm_atomic_check_only(state);
  533. if (ret)
  534. return ret;
  535. DRM_DEBUG_KMS("commiting %p asynchronously\n", state);
  536. return config->funcs->atomic_commit(state->dev, state, true);
  537. }
  538. EXPORT_SYMBOL(drm_atomic_async_commit);