drm_atomic.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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_crtc_get_property - get property on CRTC
  211. * @crtc: the drm CRTC to get a property on
  212. * @state: the state object with the property value to read
  213. * @property: the property to get
  214. * @val: the property value (returned by reference)
  215. *
  216. * Use this instead of calling crtc->atomic_get_property directly.
  217. * This function handles generic/core properties and calls out to
  218. * driver's ->atomic_get_property() for driver properties. To ensure
  219. * consistent behavior you must call this function rather than the
  220. * driver hook directly.
  221. *
  222. * RETURNS:
  223. * Zero on success, error code on failure
  224. */
  225. int drm_atomic_crtc_get_property(struct drm_crtc *crtc,
  226. const struct drm_crtc_state *state,
  227. struct drm_property *property, uint64_t *val)
  228. {
  229. if (crtc->funcs->atomic_get_property)
  230. return crtc->funcs->atomic_get_property(crtc, state, property, val);
  231. return -EINVAL;
  232. }
  233. EXPORT_SYMBOL(drm_atomic_crtc_get_property);
  234. /**
  235. * drm_atomic_get_plane_state - get plane state
  236. * @state: global atomic state object
  237. * @plane: plane to get state object for
  238. *
  239. * This function returns the plane state for the given plane, allocating it if
  240. * needed. It will also grab the relevant plane lock to make sure that the state
  241. * is consistent.
  242. *
  243. * Returns:
  244. *
  245. * Either the allocated state or the error code encoded into the pointer. When
  246. * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
  247. * entire atomic sequence must be restarted. All other errors are fatal.
  248. */
  249. struct drm_plane_state *
  250. drm_atomic_get_plane_state(struct drm_atomic_state *state,
  251. struct drm_plane *plane)
  252. {
  253. int ret, index;
  254. struct drm_plane_state *plane_state;
  255. index = drm_plane_index(plane);
  256. if (state->plane_states[index])
  257. return state->plane_states[index];
  258. ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
  259. if (ret)
  260. return ERR_PTR(ret);
  261. plane_state = plane->funcs->atomic_duplicate_state(plane);
  262. if (!plane_state)
  263. return ERR_PTR(-ENOMEM);
  264. state->plane_states[index] = plane_state;
  265. state->planes[index] = plane;
  266. plane_state->state = state;
  267. DRM_DEBUG_KMS("Added [PLANE:%d] %p state to %p\n",
  268. plane->base.id, plane_state, state);
  269. if (plane_state->crtc) {
  270. struct drm_crtc_state *crtc_state;
  271. crtc_state = drm_atomic_get_crtc_state(state,
  272. plane_state->crtc);
  273. if (IS_ERR(crtc_state))
  274. return ERR_CAST(crtc_state);
  275. }
  276. return plane_state;
  277. }
  278. EXPORT_SYMBOL(drm_atomic_get_plane_state);
  279. /**
  280. * drm_atomic_plane_set_property - set property on plane
  281. * @plane: the drm plane to set a property on
  282. * @state: the state object to update with the new property value
  283. * @property: the property to set
  284. * @val: the new property value
  285. *
  286. * Use this instead of calling plane->atomic_set_property directly.
  287. * This function handles generic/core properties and calls out to
  288. * driver's ->atomic_set_property() for driver properties. To ensure
  289. * consistent behavior you must call this function rather than the
  290. * driver hook directly.
  291. *
  292. * RETURNS:
  293. * Zero on success, error code on failure
  294. */
  295. int drm_atomic_plane_set_property(struct drm_plane *plane,
  296. struct drm_plane_state *state, struct drm_property *property,
  297. uint64_t val)
  298. {
  299. if (plane->funcs->atomic_set_property)
  300. return plane->funcs->atomic_set_property(plane, state, property, val);
  301. return -EINVAL;
  302. }
  303. EXPORT_SYMBOL(drm_atomic_plane_set_property);
  304. /**
  305. * drm_atomic_plane_get_property - get property on plane
  306. * @plane: the drm plane to get a property on
  307. * @state: the state object with the property value to read
  308. * @property: the property to get
  309. * @val: the property value (returned by reference)
  310. *
  311. * Use this instead of calling plane->atomic_get_property directly.
  312. * This function handles generic/core properties and calls out to
  313. * driver's ->atomic_get_property() for driver properties. To ensure
  314. * consistent behavior you must call this function rather than the
  315. * driver hook directly.
  316. *
  317. * RETURNS:
  318. * Zero on success, error code on failure
  319. */
  320. int drm_atomic_plane_get_property(struct drm_plane *plane,
  321. const struct drm_plane_state *state,
  322. struct drm_property *property, uint64_t *val)
  323. {
  324. if (plane->funcs->atomic_get_property)
  325. return plane->funcs->atomic_get_property(plane, state, property, val);
  326. return -EINVAL;
  327. }
  328. EXPORT_SYMBOL(drm_atomic_plane_get_property);
  329. /**
  330. * drm_atomic_get_connector_state - get connector state
  331. * @state: global atomic state object
  332. * @connector: connector to get state object for
  333. *
  334. * This function returns the connector state for the given connector,
  335. * allocating it if needed. It will also grab the relevant connector lock to
  336. * make sure that the state is consistent.
  337. *
  338. * Returns:
  339. *
  340. * Either the allocated state or the error code encoded into the pointer. When
  341. * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
  342. * entire atomic sequence must be restarted. All other errors are fatal.
  343. */
  344. struct drm_connector_state *
  345. drm_atomic_get_connector_state(struct drm_atomic_state *state,
  346. struct drm_connector *connector)
  347. {
  348. int ret, index;
  349. struct drm_mode_config *config = &connector->dev->mode_config;
  350. struct drm_connector_state *connector_state;
  351. ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
  352. if (ret)
  353. return ERR_PTR(ret);
  354. index = drm_connector_index(connector);
  355. /*
  356. * Construction of atomic state updates can race with a connector
  357. * hot-add which might overflow. In this case flip the table and just
  358. * restart the entire ioctl - no one is fast enough to livelock a cpu
  359. * with physical hotplug events anyway.
  360. *
  361. * Note that we only grab the indexes once we have the right lock to
  362. * prevent hotplug/unplugging of connectors. So removal is no problem,
  363. * at most the array is a bit too large.
  364. */
  365. if (index >= state->num_connector) {
  366. DRM_DEBUG_KMS("Hot-added connector would overflow state array, restarting\n");
  367. return ERR_PTR(-EAGAIN);
  368. }
  369. if (state->connector_states[index])
  370. return state->connector_states[index];
  371. connector_state = connector->funcs->atomic_duplicate_state(connector);
  372. if (!connector_state)
  373. return ERR_PTR(-ENOMEM);
  374. state->connector_states[index] = connector_state;
  375. state->connectors[index] = connector;
  376. connector_state->state = state;
  377. DRM_DEBUG_KMS("Added [CONNECTOR:%d] %p state to %p\n",
  378. connector->base.id, connector_state, state);
  379. if (connector_state->crtc) {
  380. struct drm_crtc_state *crtc_state;
  381. crtc_state = drm_atomic_get_crtc_state(state,
  382. connector_state->crtc);
  383. if (IS_ERR(crtc_state))
  384. return ERR_CAST(crtc_state);
  385. }
  386. return connector_state;
  387. }
  388. EXPORT_SYMBOL(drm_atomic_get_connector_state);
  389. /**
  390. * drm_atomic_connector_set_property - set property on connector.
  391. * @connector: the drm connector to set a property on
  392. * @state: the state object to update with the new property value
  393. * @property: the property to set
  394. * @val: the new property value
  395. *
  396. * Use this instead of calling connector->atomic_set_property directly.
  397. * This function handles generic/core properties and calls out to
  398. * driver's ->atomic_set_property() for driver properties. To ensure
  399. * consistent behavior you must call this function rather than the
  400. * driver hook directly.
  401. *
  402. * RETURNS:
  403. * Zero on success, error code on failure
  404. */
  405. int drm_atomic_connector_set_property(struct drm_connector *connector,
  406. struct drm_connector_state *state, struct drm_property *property,
  407. uint64_t val)
  408. {
  409. struct drm_device *dev = connector->dev;
  410. struct drm_mode_config *config = &dev->mode_config;
  411. if (property == config->dpms_property) {
  412. /* setting DPMS property requires special handling, which
  413. * is done in legacy setprop path for us. Disallow (for
  414. * now?) atomic writes to DPMS property:
  415. */
  416. return -EINVAL;
  417. } else if (connector->funcs->atomic_set_property) {
  418. return connector->funcs->atomic_set_property(connector,
  419. state, property, val);
  420. } else {
  421. return -EINVAL;
  422. }
  423. }
  424. EXPORT_SYMBOL(drm_atomic_connector_set_property);
  425. /**
  426. * drm_atomic_connector_get_property - get property on connector
  427. * @connector: the drm connector to get a property on
  428. * @state: the state object with the property value to read
  429. * @property: the property to get
  430. * @val: the property value (returned by reference)
  431. *
  432. * Use this instead of calling connector->atomic_get_property directly.
  433. * This function handles generic/core properties and calls out to
  434. * driver's ->atomic_get_property() for driver properties. To ensure
  435. * consistent behavior you must call this function rather than the
  436. * driver hook directly.
  437. *
  438. * RETURNS:
  439. * Zero on success, error code on failure
  440. */
  441. int drm_atomic_connector_get_property(struct drm_connector *connector,
  442. const struct drm_connector_state *state,
  443. struct drm_property *property, uint64_t *val)
  444. {
  445. struct drm_device *dev = connector->dev;
  446. struct drm_mode_config *config = &dev->mode_config;
  447. if (property == config->dpms_property) {
  448. *val = connector->dpms;
  449. } else if (connector->funcs->atomic_get_property) {
  450. return connector->funcs->atomic_get_property(connector,
  451. state, property, val);
  452. } else {
  453. return -EINVAL;
  454. }
  455. return 0;
  456. }
  457. EXPORT_SYMBOL(drm_atomic_connector_get_property);
  458. /**
  459. * drm_atomic_set_crtc_for_plane - set crtc for plane
  460. * @plane_state: the plane whose incoming state to update
  461. * @crtc: crtc to use for the plane
  462. *
  463. * Changing the assigned crtc for a plane requires us to grab the lock and state
  464. * for the new crtc, as needed. This function takes care of all these details
  465. * besides updating the pointer in the state object itself.
  466. *
  467. * Returns:
  468. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  469. * then the w/w mutex code has detected a deadlock and the entire atomic
  470. * sequence must be restarted. All other errors are fatal.
  471. */
  472. int
  473. drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
  474. struct drm_crtc *crtc)
  475. {
  476. struct drm_plane *plane = plane_state->plane;
  477. struct drm_crtc_state *crtc_state;
  478. if (plane_state->crtc) {
  479. crtc_state = drm_atomic_get_crtc_state(plane_state->state,
  480. plane_state->crtc);
  481. if (WARN_ON(IS_ERR(crtc_state)))
  482. return PTR_ERR(crtc_state);
  483. crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
  484. }
  485. plane_state->crtc = crtc;
  486. if (crtc) {
  487. crtc_state = drm_atomic_get_crtc_state(plane_state->state,
  488. crtc);
  489. if (IS_ERR(crtc_state))
  490. return PTR_ERR(crtc_state);
  491. crtc_state->plane_mask |= (1 << drm_plane_index(plane));
  492. }
  493. if (crtc)
  494. DRM_DEBUG_KMS("Link plane state %p to [CRTC:%d]\n",
  495. plane_state, crtc->base.id);
  496. else
  497. DRM_DEBUG_KMS("Link plane state %p to [NOCRTC]\n", plane_state);
  498. return 0;
  499. }
  500. EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
  501. /**
  502. * drm_atomic_set_fb_for_plane - set crtc for plane
  503. * @plane_state: atomic state object for the plane
  504. * @fb: fb to use for the plane
  505. *
  506. * Changing the assigned framebuffer for a plane requires us to grab a reference
  507. * to the new fb and drop the reference to the old fb, if there is one. This
  508. * function takes care of all these details besides updating the pointer in the
  509. * state object itself.
  510. */
  511. void
  512. drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
  513. struct drm_framebuffer *fb)
  514. {
  515. if (plane_state->fb)
  516. drm_framebuffer_unreference(plane_state->fb);
  517. if (fb)
  518. drm_framebuffer_reference(fb);
  519. plane_state->fb = fb;
  520. if (fb)
  521. DRM_DEBUG_KMS("Set [FB:%d] for plane state %p\n",
  522. fb->base.id, plane_state);
  523. else
  524. DRM_DEBUG_KMS("Set [NOFB] for plane state %p\n", plane_state);
  525. }
  526. EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
  527. /**
  528. * drm_atomic_set_crtc_for_connector - set crtc for connector
  529. * @conn_state: atomic state object for the connector
  530. * @crtc: crtc to use for the connector
  531. *
  532. * Changing the assigned crtc for a connector requires us to grab the lock and
  533. * state for the new crtc, as needed. This function takes care of all these
  534. * details besides updating the pointer in the state object itself.
  535. *
  536. * Returns:
  537. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  538. * then the w/w mutex code has detected a deadlock and the entire atomic
  539. * sequence must be restarted. All other errors are fatal.
  540. */
  541. int
  542. drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
  543. struct drm_crtc *crtc)
  544. {
  545. struct drm_crtc_state *crtc_state;
  546. if (crtc) {
  547. crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
  548. if (IS_ERR(crtc_state))
  549. return PTR_ERR(crtc_state);
  550. }
  551. conn_state->crtc = crtc;
  552. if (crtc)
  553. DRM_DEBUG_KMS("Link connector state %p to [CRTC:%d]\n",
  554. conn_state, crtc->base.id);
  555. else
  556. DRM_DEBUG_KMS("Link connector state %p to [NOCRTC]\n",
  557. conn_state);
  558. return 0;
  559. }
  560. EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
  561. /**
  562. * drm_atomic_add_affected_connectors - add connectors for crtc
  563. * @state: atomic state
  564. * @crtc: DRM crtc
  565. *
  566. * This function walks the current configuration and adds all connectors
  567. * currently using @crtc to the atomic configuration @state. Note that this
  568. * function must acquire the connection mutex. This can potentially cause
  569. * unneeded seralization if the update is just for the planes on one crtc. Hence
  570. * drivers and helpers should only call this when really needed (e.g. when a
  571. * full modeset needs to happen due to some change).
  572. *
  573. * Returns:
  574. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  575. * then the w/w mutex code has detected a deadlock and the entire atomic
  576. * sequence must be restarted. All other errors are fatal.
  577. */
  578. int
  579. drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
  580. struct drm_crtc *crtc)
  581. {
  582. struct drm_mode_config *config = &state->dev->mode_config;
  583. struct drm_connector *connector;
  584. struct drm_connector_state *conn_state;
  585. int ret;
  586. ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
  587. if (ret)
  588. return ret;
  589. DRM_DEBUG_KMS("Adding all current connectors for [CRTC:%d] to %p\n",
  590. crtc->base.id, state);
  591. /*
  592. * Changed connectors are already in @state, so only need to look at the
  593. * current configuration.
  594. */
  595. list_for_each_entry(connector, &config->connector_list, head) {
  596. if (connector->state->crtc != crtc)
  597. continue;
  598. conn_state = drm_atomic_get_connector_state(state, connector);
  599. if (IS_ERR(conn_state))
  600. return PTR_ERR(conn_state);
  601. }
  602. return 0;
  603. }
  604. EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
  605. /**
  606. * drm_atomic_connectors_for_crtc - count number of connected outputs
  607. * @state: atomic state
  608. * @crtc: DRM crtc
  609. *
  610. * This function counts all connectors which will be connected to @crtc
  611. * according to @state. Useful to recompute the enable state for @crtc.
  612. */
  613. int
  614. drm_atomic_connectors_for_crtc(struct drm_atomic_state *state,
  615. struct drm_crtc *crtc)
  616. {
  617. int i, num_connected_connectors = 0;
  618. for (i = 0; i < state->num_connector; i++) {
  619. struct drm_connector_state *conn_state;
  620. conn_state = state->connector_states[i];
  621. if (conn_state && conn_state->crtc == crtc)
  622. num_connected_connectors++;
  623. }
  624. DRM_DEBUG_KMS("State %p has %i connectors for [CRTC:%d]\n",
  625. state, num_connected_connectors, crtc->base.id);
  626. return num_connected_connectors;
  627. }
  628. EXPORT_SYMBOL(drm_atomic_connectors_for_crtc);
  629. /**
  630. * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
  631. * @state: atomic state
  632. *
  633. * This function should be used by legacy entry points which don't understand
  634. * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
  635. * the slowpath completed.
  636. */
  637. void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
  638. {
  639. int ret;
  640. retry:
  641. drm_modeset_backoff(state->acquire_ctx);
  642. ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
  643. state->acquire_ctx);
  644. if (ret)
  645. goto retry;
  646. ret = drm_modeset_lock_all_crtcs(state->dev,
  647. state->acquire_ctx);
  648. if (ret)
  649. goto retry;
  650. }
  651. EXPORT_SYMBOL(drm_atomic_legacy_backoff);
  652. /**
  653. * drm_atomic_check_only - check whether a given config would work
  654. * @state: atomic configuration to check
  655. *
  656. * Note that this function can return -EDEADLK if the driver needed to acquire
  657. * more locks but encountered a deadlock. The caller must then do the usual w/w
  658. * backoff dance and restart. All other errors are fatal.
  659. *
  660. * Returns:
  661. * 0 on success, negative error code on failure.
  662. */
  663. int drm_atomic_check_only(struct drm_atomic_state *state)
  664. {
  665. struct drm_mode_config *config = &state->dev->mode_config;
  666. DRM_DEBUG_KMS("checking %p\n", state);
  667. if (config->funcs->atomic_check)
  668. return config->funcs->atomic_check(state->dev, state);
  669. else
  670. return 0;
  671. }
  672. EXPORT_SYMBOL(drm_atomic_check_only);
  673. /**
  674. * drm_atomic_commit - commit configuration atomically
  675. * @state: atomic configuration to check
  676. *
  677. * Note that this function can return -EDEADLK if the driver needed to acquire
  678. * more locks but encountered a deadlock. The caller must then do the usual w/w
  679. * backoff dance and restart. All other errors are fatal.
  680. *
  681. * Also note that on successful execution ownership of @state is transferred
  682. * from the caller of this function to the function itself. The caller must not
  683. * free or in any other way access @state. If the function fails then the caller
  684. * must clean up @state itself.
  685. *
  686. * Returns:
  687. * 0 on success, negative error code on failure.
  688. */
  689. int drm_atomic_commit(struct drm_atomic_state *state)
  690. {
  691. struct drm_mode_config *config = &state->dev->mode_config;
  692. int ret;
  693. ret = drm_atomic_check_only(state);
  694. if (ret)
  695. return ret;
  696. DRM_DEBUG_KMS("commiting %p\n", state);
  697. return config->funcs->atomic_commit(state->dev, state, false);
  698. }
  699. EXPORT_SYMBOL(drm_atomic_commit);
  700. /**
  701. * drm_atomic_async_commit - atomic&async configuration commit
  702. * @state: atomic configuration to check
  703. *
  704. * Note that this function can return -EDEADLK if the driver needed to acquire
  705. * more locks but encountered a deadlock. The caller must then do the usual w/w
  706. * backoff dance and restart. All other errors are fatal.
  707. *
  708. * Also note that on successful execution ownership of @state is transferred
  709. * from the caller of this function to the function itself. The caller must not
  710. * free or in any other way access @state. If the function fails then the caller
  711. * must clean up @state itself.
  712. *
  713. * Returns:
  714. * 0 on success, negative error code on failure.
  715. */
  716. int drm_atomic_async_commit(struct drm_atomic_state *state)
  717. {
  718. struct drm_mode_config *config = &state->dev->mode_config;
  719. int ret;
  720. ret = drm_atomic_check_only(state);
  721. if (ret)
  722. return ret;
  723. DRM_DEBUG_KMS("commiting %p asynchronously\n", state);
  724. return config->funcs->atomic_commit(state->dev, state, true);
  725. }
  726. EXPORT_SYMBOL(drm_atomic_async_commit);