drm_atomic.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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->num_connector = ACCESS_ONCE(dev->mode_config.num_connector);
  54. state->crtcs = kcalloc(dev->mode_config.num_crtc,
  55. sizeof(*state->crtcs), GFP_KERNEL);
  56. if (!state->crtcs)
  57. goto fail;
  58. state->crtc_states = kcalloc(dev->mode_config.num_crtc,
  59. sizeof(*state->crtc_states), GFP_KERNEL);
  60. if (!state->crtc_states)
  61. goto fail;
  62. state->planes = kcalloc(dev->mode_config.num_total_plane,
  63. sizeof(*state->planes), GFP_KERNEL);
  64. if (!state->planes)
  65. goto fail;
  66. state->plane_states = kcalloc(dev->mode_config.num_total_plane,
  67. sizeof(*state->plane_states), GFP_KERNEL);
  68. if (!state->plane_states)
  69. goto fail;
  70. state->connectors = kcalloc(state->num_connector,
  71. sizeof(*state->connectors),
  72. GFP_KERNEL);
  73. if (!state->connectors)
  74. goto fail;
  75. state->connector_states = kcalloc(state->num_connector,
  76. sizeof(*state->connector_states),
  77. GFP_KERNEL);
  78. if (!state->connector_states)
  79. goto fail;
  80. state->dev = dev;
  81. DRM_DEBUG_KMS("Allocate atomic state %p\n", state);
  82. return state;
  83. fail:
  84. kfree_state(state);
  85. return NULL;
  86. }
  87. EXPORT_SYMBOL(drm_atomic_state_alloc);
  88. /**
  89. * drm_atomic_state_clear - clear state object
  90. * @state: atomic state
  91. *
  92. * When the w/w mutex algorithm detects a deadlock we need to back off and drop
  93. * all locks. So someone else could sneak in and change the current modeset
  94. * configuration. Which means that all the state assembled in @state is no
  95. * longer an atomic update to the current state, but to some arbitrary earlier
  96. * state. Which could break assumptions the driver's ->atomic_check likely
  97. * relies on.
  98. *
  99. * Hence we must clear all cached state and completely start over, using this
  100. * function.
  101. */
  102. void drm_atomic_state_clear(struct drm_atomic_state *state)
  103. {
  104. struct drm_device *dev = state->dev;
  105. struct drm_mode_config *config = &dev->mode_config;
  106. int i;
  107. DRM_DEBUG_KMS("Clearing atomic state %p\n", state);
  108. for (i = 0; i < state->num_connector; i++) {
  109. struct drm_connector *connector = state->connectors[i];
  110. if (!connector)
  111. continue;
  112. WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  113. connector->funcs->atomic_destroy_state(connector,
  114. state->connector_states[i]);
  115. }
  116. for (i = 0; i < config->num_crtc; i++) {
  117. struct drm_crtc *crtc = state->crtcs[i];
  118. if (!crtc)
  119. continue;
  120. crtc->funcs->atomic_destroy_state(crtc,
  121. state->crtc_states[i]);
  122. }
  123. for (i = 0; i < config->num_total_plane; i++) {
  124. struct drm_plane *plane = state->planes[i];
  125. if (!plane)
  126. continue;
  127. plane->funcs->atomic_destroy_state(plane,
  128. state->plane_states[i]);
  129. }
  130. }
  131. EXPORT_SYMBOL(drm_atomic_state_clear);
  132. /**
  133. * drm_atomic_state_free - free all memory for an atomic state
  134. * @state: atomic state to deallocate
  135. *
  136. * This frees all memory associated with an atomic state, including all the
  137. * per-object state for planes, crtcs and connectors.
  138. */
  139. void drm_atomic_state_free(struct drm_atomic_state *state)
  140. {
  141. drm_atomic_state_clear(state);
  142. DRM_DEBUG_KMS("Freeing atomic state %p\n", state);
  143. kfree_state(state);
  144. }
  145. EXPORT_SYMBOL(drm_atomic_state_free);
  146. /**
  147. * drm_atomic_get_crtc_state - get crtc state
  148. * @state: global atomic state object
  149. * @crtc: crtc to get state object for
  150. *
  151. * This function returns the crtc state for the given crtc, allocating it if
  152. * needed. It will also grab the relevant crtc lock to make sure that the state
  153. * is consistent.
  154. *
  155. * Returns:
  156. *
  157. * Either the allocated state or the error code encoded into the pointer. When
  158. * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
  159. * entire atomic sequence must be restarted. All other errors are fatal.
  160. */
  161. struct drm_crtc_state *
  162. drm_atomic_get_crtc_state(struct drm_atomic_state *state,
  163. struct drm_crtc *crtc)
  164. {
  165. int ret, index;
  166. struct drm_crtc_state *crtc_state;
  167. index = drm_crtc_index(crtc);
  168. if (state->crtc_states[index])
  169. return state->crtc_states[index];
  170. ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
  171. if (ret)
  172. return ERR_PTR(ret);
  173. crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
  174. if (!crtc_state)
  175. return ERR_PTR(-ENOMEM);
  176. state->crtc_states[index] = crtc_state;
  177. state->crtcs[index] = crtc;
  178. crtc_state->state = state;
  179. DRM_DEBUG_KMS("Added [CRTC:%d] %p state to %p\n",
  180. crtc->base.id, crtc_state, state);
  181. return crtc_state;
  182. }
  183. EXPORT_SYMBOL(drm_atomic_get_crtc_state);
  184. /**
  185. * drm_atomic_crtc_set_property - set property on CRTC
  186. * @crtc: the drm CRTC to set a property on
  187. * @state: the state object to update with the new property value
  188. * @property: the property to set
  189. * @val: the new property value
  190. *
  191. * Use this instead of calling crtc->atomic_set_property directly.
  192. * This function handles generic/core properties and calls out to
  193. * driver's ->atomic_set_property() for driver properties. To ensure
  194. * consistent behavior you must call this function rather than the
  195. * driver hook directly.
  196. *
  197. * RETURNS:
  198. * Zero on success, error code on failure
  199. */
  200. int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
  201. struct drm_crtc_state *state, struct drm_property *property,
  202. uint64_t val)
  203. {
  204. if (crtc->funcs->atomic_set_property)
  205. return crtc->funcs->atomic_set_property(crtc, state, property, val);
  206. return -EINVAL;
  207. }
  208. EXPORT_SYMBOL(drm_atomic_crtc_set_property);
  209. /**
  210. * drm_atomic_get_plane_state - get plane state
  211. * @state: global atomic state object
  212. * @plane: plane to get state object for
  213. *
  214. * This function returns the plane state for the given plane, allocating it if
  215. * needed. It will also grab the relevant plane lock to make sure that the state
  216. * is consistent.
  217. *
  218. * Returns:
  219. *
  220. * Either the allocated state or the error code encoded into the pointer. When
  221. * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
  222. * entire atomic sequence must be restarted. All other errors are fatal.
  223. */
  224. struct drm_plane_state *
  225. drm_atomic_get_plane_state(struct drm_atomic_state *state,
  226. struct drm_plane *plane)
  227. {
  228. int ret, index;
  229. struct drm_plane_state *plane_state;
  230. index = drm_plane_index(plane);
  231. if (state->plane_states[index])
  232. return state->plane_states[index];
  233. ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
  234. if (ret)
  235. return ERR_PTR(ret);
  236. plane_state = plane->funcs->atomic_duplicate_state(plane);
  237. if (!plane_state)
  238. return ERR_PTR(-ENOMEM);
  239. state->plane_states[index] = plane_state;
  240. state->planes[index] = plane;
  241. plane_state->state = state;
  242. DRM_DEBUG_KMS("Added [PLANE:%d] %p state to %p\n",
  243. plane->base.id, plane_state, state);
  244. if (plane_state->crtc) {
  245. struct drm_crtc_state *crtc_state;
  246. crtc_state = drm_atomic_get_crtc_state(state,
  247. plane_state->crtc);
  248. if (IS_ERR(crtc_state))
  249. return ERR_CAST(crtc_state);
  250. }
  251. return plane_state;
  252. }
  253. EXPORT_SYMBOL(drm_atomic_get_plane_state);
  254. /**
  255. * drm_atomic_plane_set_property - set property on plane
  256. * @plane: the drm plane to set a property on
  257. * @state: the state object to update with the new property value
  258. * @property: the property to set
  259. * @val: the new property value
  260. *
  261. * Use this instead of calling plane->atomic_set_property directly.
  262. * This function handles generic/core properties and calls out to
  263. * driver's ->atomic_set_property() for driver properties. To ensure
  264. * consistent behavior you must call this function rather than the
  265. * driver hook directly.
  266. *
  267. * RETURNS:
  268. * Zero on success, error code on failure
  269. */
  270. int drm_atomic_plane_set_property(struct drm_plane *plane,
  271. struct drm_plane_state *state, struct drm_property *property,
  272. uint64_t val)
  273. {
  274. if (plane->funcs->atomic_set_property)
  275. return plane->funcs->atomic_set_property(plane, state, property, val);
  276. return -EINVAL;
  277. }
  278. EXPORT_SYMBOL(drm_atomic_plane_set_property);
  279. /**
  280. * drm_atomic_get_connector_state - get connector state
  281. * @state: global atomic state object
  282. * @connector: connector to get state object for
  283. *
  284. * This function returns the connector state for the given connector,
  285. * allocating it if needed. It will also grab the relevant connector lock to
  286. * make sure that the state is consistent.
  287. *
  288. * Returns:
  289. *
  290. * Either the allocated state or the error code encoded into the pointer. When
  291. * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
  292. * entire atomic sequence must be restarted. All other errors are fatal.
  293. */
  294. struct drm_connector_state *
  295. drm_atomic_get_connector_state(struct drm_atomic_state *state,
  296. struct drm_connector *connector)
  297. {
  298. int ret, index;
  299. struct drm_mode_config *config = &connector->dev->mode_config;
  300. struct drm_connector_state *connector_state;
  301. ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
  302. if (ret)
  303. return ERR_PTR(ret);
  304. index = drm_connector_index(connector);
  305. /*
  306. * Construction of atomic state updates can race with a connector
  307. * hot-add which might overflow. In this case flip the table and just
  308. * restart the entire ioctl - no one is fast enough to livelock a cpu
  309. * with physical hotplug events anyway.
  310. *
  311. * Note that we only grab the indexes once we have the right lock to
  312. * prevent hotplug/unplugging of connectors. So removal is no problem,
  313. * at most the array is a bit too large.
  314. */
  315. if (index >= state->num_connector) {
  316. DRM_DEBUG_KMS("Hot-added connector would overflow state array, restarting\n");
  317. return ERR_PTR(-EAGAIN);
  318. }
  319. if (state->connector_states[index])
  320. return state->connector_states[index];
  321. connector_state = connector->funcs->atomic_duplicate_state(connector);
  322. if (!connector_state)
  323. return ERR_PTR(-ENOMEM);
  324. state->connector_states[index] = connector_state;
  325. state->connectors[index] = connector;
  326. connector_state->state = state;
  327. DRM_DEBUG_KMS("Added [CONNECTOR:%d] %p state to %p\n",
  328. connector->base.id, connector_state, state);
  329. if (connector_state->crtc) {
  330. struct drm_crtc_state *crtc_state;
  331. crtc_state = drm_atomic_get_crtc_state(state,
  332. connector_state->crtc);
  333. if (IS_ERR(crtc_state))
  334. return ERR_CAST(crtc_state);
  335. }
  336. return connector_state;
  337. }
  338. EXPORT_SYMBOL(drm_atomic_get_connector_state);
  339. /**
  340. * drm_atomic_connector_set_property - set property on connector.
  341. * @connector: the drm connector to set a property on
  342. * @state: the state object to update with the new property value
  343. * @property: the property to set
  344. * @val: the new property value
  345. *
  346. * Use this instead of calling connector->atomic_set_property directly.
  347. * This function handles generic/core properties and calls out to
  348. * driver's ->atomic_set_property() for driver properties. To ensure
  349. * consistent behavior you must call this function rather than the
  350. * driver hook directly.
  351. *
  352. * RETURNS:
  353. * Zero on success, error code on failure
  354. */
  355. int drm_atomic_connector_set_property(struct drm_connector *connector,
  356. struct drm_connector_state *state, struct drm_property *property,
  357. uint64_t val)
  358. {
  359. struct drm_device *dev = connector->dev;
  360. struct drm_mode_config *config = &dev->mode_config;
  361. if (property == config->dpms_property) {
  362. /* setting DPMS property requires special handling, which
  363. * is done in legacy setprop path for us. Disallow (for
  364. * now?) atomic writes to DPMS property:
  365. */
  366. return -EINVAL;
  367. } else if (connector->funcs->atomic_set_property) {
  368. return connector->funcs->atomic_set_property(connector,
  369. state, property, val);
  370. } else {
  371. return -EINVAL;
  372. }
  373. }
  374. EXPORT_SYMBOL(drm_atomic_connector_set_property);
  375. /**
  376. * drm_atomic_set_crtc_for_plane - set crtc for plane
  377. * @plane_state: the plane whose incoming state to update
  378. * @crtc: crtc to use for the plane
  379. *
  380. * Changing the assigned crtc for a plane requires us to grab the lock and state
  381. * for the new crtc, as needed. This function takes care of all these details
  382. * besides updating the pointer in the state object itself.
  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_set_crtc_for_plane(struct drm_plane_state *plane_state,
  391. struct drm_crtc *crtc)
  392. {
  393. struct drm_plane *plane = plane_state->plane;
  394. struct drm_crtc_state *crtc_state;
  395. if (plane_state->crtc) {
  396. crtc_state = drm_atomic_get_crtc_state(plane_state->state,
  397. plane_state->crtc);
  398. if (WARN_ON(IS_ERR(crtc_state)))
  399. return PTR_ERR(crtc_state);
  400. crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
  401. }
  402. plane_state->crtc = crtc;
  403. if (crtc) {
  404. crtc_state = drm_atomic_get_crtc_state(plane_state->state,
  405. crtc);
  406. if (IS_ERR(crtc_state))
  407. return PTR_ERR(crtc_state);
  408. crtc_state->plane_mask |= (1 << drm_plane_index(plane));
  409. }
  410. if (crtc)
  411. DRM_DEBUG_KMS("Link plane state %p to [CRTC:%d]\n",
  412. plane_state, crtc->base.id);
  413. else
  414. DRM_DEBUG_KMS("Link plane state %p to [NOCRTC]\n", plane_state);
  415. return 0;
  416. }
  417. EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
  418. /**
  419. * drm_atomic_set_fb_for_plane - set crtc for plane
  420. * @plane_state: atomic state object for the plane
  421. * @fb: fb to use for the plane
  422. *
  423. * Changing the assigned framebuffer for a plane requires us to grab a reference
  424. * to the new fb and drop the reference to the old fb, if there is one. This
  425. * function takes care of all these details besides updating the pointer in the
  426. * state object itself.
  427. */
  428. void
  429. drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
  430. struct drm_framebuffer *fb)
  431. {
  432. if (plane_state->fb)
  433. drm_framebuffer_unreference(plane_state->fb);
  434. if (fb)
  435. drm_framebuffer_reference(fb);
  436. plane_state->fb = fb;
  437. if (fb)
  438. DRM_DEBUG_KMS("Set [FB:%d] for plane state %p\n",
  439. fb->base.id, plane_state);
  440. else
  441. DRM_DEBUG_KMS("Set [NOFB] for plane state %p\n", plane_state);
  442. }
  443. EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
  444. /**
  445. * drm_atomic_set_crtc_for_connector - set crtc for connector
  446. * @conn_state: atomic state object for the connector
  447. * @crtc: crtc to use for the connector
  448. *
  449. * Changing the assigned crtc for a connector requires us to grab the lock and
  450. * state for the new crtc, as needed. This function takes care of all these
  451. * details besides updating the pointer in the state object itself.
  452. *
  453. * Returns:
  454. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  455. * then the w/w mutex code has detected a deadlock and the entire atomic
  456. * sequence must be restarted. All other errors are fatal.
  457. */
  458. int
  459. drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
  460. struct drm_crtc *crtc)
  461. {
  462. struct drm_crtc_state *crtc_state;
  463. if (crtc) {
  464. crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
  465. if (IS_ERR(crtc_state))
  466. return PTR_ERR(crtc_state);
  467. }
  468. conn_state->crtc = crtc;
  469. if (crtc)
  470. DRM_DEBUG_KMS("Link connector state %p to [CRTC:%d]\n",
  471. conn_state, crtc->base.id);
  472. else
  473. DRM_DEBUG_KMS("Link connector state %p to [NOCRTC]\n",
  474. conn_state);
  475. return 0;
  476. }
  477. EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
  478. /**
  479. * drm_atomic_add_affected_connectors - add connectors for crtc
  480. * @state: atomic state
  481. * @crtc: DRM crtc
  482. *
  483. * This function walks the current configuration and adds all connectors
  484. * currently using @crtc to the atomic configuration @state. Note that this
  485. * function must acquire the connection mutex. This can potentially cause
  486. * unneeded seralization if the update is just for the planes on one crtc. Hence
  487. * drivers and helpers should only call this when really needed (e.g. when a
  488. * full modeset needs to happen due to some change).
  489. *
  490. * Returns:
  491. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  492. * then the w/w mutex code has detected a deadlock and the entire atomic
  493. * sequence must be restarted. All other errors are fatal.
  494. */
  495. int
  496. drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
  497. struct drm_crtc *crtc)
  498. {
  499. struct drm_mode_config *config = &state->dev->mode_config;
  500. struct drm_connector *connector;
  501. struct drm_connector_state *conn_state;
  502. int ret;
  503. ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
  504. if (ret)
  505. return ret;
  506. DRM_DEBUG_KMS("Adding all current connectors for [CRTC:%d] to %p\n",
  507. crtc->base.id, state);
  508. /*
  509. * Changed connectors are already in @state, so only need to look at the
  510. * current configuration.
  511. */
  512. list_for_each_entry(connector, &config->connector_list, head) {
  513. if (connector->state->crtc != crtc)
  514. continue;
  515. conn_state = drm_atomic_get_connector_state(state, connector);
  516. if (IS_ERR(conn_state))
  517. return PTR_ERR(conn_state);
  518. }
  519. return 0;
  520. }
  521. EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
  522. /**
  523. * drm_atomic_connectors_for_crtc - count number of connected outputs
  524. * @state: atomic state
  525. * @crtc: DRM crtc
  526. *
  527. * This function counts all connectors which will be connected to @crtc
  528. * according to @state. Useful to recompute the enable state for @crtc.
  529. */
  530. int
  531. drm_atomic_connectors_for_crtc(struct drm_atomic_state *state,
  532. struct drm_crtc *crtc)
  533. {
  534. int i, num_connected_connectors = 0;
  535. for (i = 0; i < state->num_connector; i++) {
  536. struct drm_connector_state *conn_state;
  537. conn_state = state->connector_states[i];
  538. if (conn_state && conn_state->crtc == crtc)
  539. num_connected_connectors++;
  540. }
  541. DRM_DEBUG_KMS("State %p has %i connectors for [CRTC:%d]\n",
  542. state, num_connected_connectors, crtc->base.id);
  543. return num_connected_connectors;
  544. }
  545. EXPORT_SYMBOL(drm_atomic_connectors_for_crtc);
  546. /**
  547. * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
  548. * @state: atomic state
  549. *
  550. * This function should be used by legacy entry points which don't understand
  551. * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
  552. * the slowpath completed.
  553. */
  554. void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
  555. {
  556. int ret;
  557. retry:
  558. drm_modeset_backoff(state->acquire_ctx);
  559. ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
  560. state->acquire_ctx);
  561. if (ret)
  562. goto retry;
  563. ret = drm_modeset_lock_all_crtcs(state->dev,
  564. state->acquire_ctx);
  565. if (ret)
  566. goto retry;
  567. }
  568. EXPORT_SYMBOL(drm_atomic_legacy_backoff);
  569. /**
  570. * drm_atomic_check_only - check whether a given config would work
  571. * @state: atomic configuration to check
  572. *
  573. * Note that this function can return -EDEADLK if the driver needed to acquire
  574. * more locks but encountered a deadlock. The caller must then do the usual w/w
  575. * backoff dance and restart. All other errors are fatal.
  576. *
  577. * Returns:
  578. * 0 on success, negative error code on failure.
  579. */
  580. int drm_atomic_check_only(struct drm_atomic_state *state)
  581. {
  582. struct drm_mode_config *config = &state->dev->mode_config;
  583. DRM_DEBUG_KMS("checking %p\n", state);
  584. if (config->funcs->atomic_check)
  585. return config->funcs->atomic_check(state->dev, state);
  586. else
  587. return 0;
  588. }
  589. EXPORT_SYMBOL(drm_atomic_check_only);
  590. /**
  591. * drm_atomic_commit - commit configuration atomically
  592. * @state: atomic configuration to check
  593. *
  594. * Note that this function can return -EDEADLK if the driver needed to acquire
  595. * more locks but encountered a deadlock. The caller must then do the usual w/w
  596. * backoff dance and restart. All other errors are fatal.
  597. *
  598. * Also note that on successful execution ownership of @state is transferred
  599. * from the caller of this function to the function itself. The caller must not
  600. * free or in any other way access @state. If the function fails then the caller
  601. * must clean up @state itself.
  602. *
  603. * Returns:
  604. * 0 on success, negative error code on failure.
  605. */
  606. int drm_atomic_commit(struct drm_atomic_state *state)
  607. {
  608. struct drm_mode_config *config = &state->dev->mode_config;
  609. int ret;
  610. ret = drm_atomic_check_only(state);
  611. if (ret)
  612. return ret;
  613. DRM_DEBUG_KMS("commiting %p\n", state);
  614. return config->funcs->atomic_commit(state->dev, state, false);
  615. }
  616. EXPORT_SYMBOL(drm_atomic_commit);
  617. /**
  618. * drm_atomic_async_commit - atomic&async configuration commit
  619. * @state: atomic configuration to check
  620. *
  621. * Note that this function can return -EDEADLK if the driver needed to acquire
  622. * more locks but encountered a deadlock. The caller must then do the usual w/w
  623. * backoff dance and restart. All other errors are fatal.
  624. *
  625. * Also note that on successful execution ownership of @state is transferred
  626. * from the caller of this function to the function itself. The caller must not
  627. * free or in any other way access @state. If the function fails then the caller
  628. * must clean up @state itself.
  629. *
  630. * Returns:
  631. * 0 on success, negative error code on failure.
  632. */
  633. int drm_atomic_async_commit(struct drm_atomic_state *state)
  634. {
  635. struct drm_mode_config *config = &state->dev->mode_config;
  636. int ret;
  637. ret = drm_atomic_check_only(state);
  638. if (ret)
  639. return ret;
  640. DRM_DEBUG_KMS("commiting %p asynchronously\n", state);
  641. return config->funcs->atomic_commit(state->dev, state, true);
  642. }
  643. EXPORT_SYMBOL(drm_atomic_async_commit);