drm_atomic_helper.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584
  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. #include <drm/drm_crtc_helper.h>
  31. #include <drm/drm_atomic_helper.h>
  32. #include <linux/fence.h>
  33. static void
  34. drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
  35. struct drm_plane_state *plane_state,
  36. struct drm_plane *plane)
  37. {
  38. struct drm_crtc_state *crtc_state;
  39. if (plane->state->crtc) {
  40. crtc_state = state->crtc_states[drm_crtc_index(plane->crtc)];
  41. if (WARN_ON(!crtc_state))
  42. return;
  43. crtc_state->planes_changed = true;
  44. }
  45. if (plane_state->crtc) {
  46. crtc_state =
  47. state->crtc_states[drm_crtc_index(plane_state->crtc)];
  48. if (WARN_ON(!crtc_state))
  49. return;
  50. crtc_state->planes_changed = true;
  51. }
  52. }
  53. static struct drm_crtc *
  54. get_current_crtc_for_encoder(struct drm_device *dev,
  55. struct drm_encoder *encoder)
  56. {
  57. struct drm_mode_config *config = &dev->mode_config;
  58. struct drm_connector *connector;
  59. WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  60. list_for_each_entry(connector, &config->connector_list, head) {
  61. if (connector->state->best_encoder != encoder)
  62. continue;
  63. return connector->state->crtc;
  64. }
  65. return NULL;
  66. }
  67. static int
  68. steal_encoder(struct drm_atomic_state *state,
  69. struct drm_encoder *encoder,
  70. struct drm_crtc *encoder_crtc)
  71. {
  72. struct drm_mode_config *config = &state->dev->mode_config;
  73. struct drm_crtc_state *crtc_state;
  74. struct drm_connector *connector;
  75. struct drm_connector_state *connector_state;
  76. int ret;
  77. /*
  78. * We can only steal an encoder coming from a connector, which means we
  79. * must already hold the connection_mutex.
  80. */
  81. WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  82. DRM_DEBUG_KMS("[ENCODER:%d:%s] in use on [CRTC:%d], stealing it\n",
  83. encoder->base.id, encoder->name,
  84. encoder_crtc->base.id);
  85. crtc_state = drm_atomic_get_crtc_state(state, encoder_crtc);
  86. if (IS_ERR(crtc_state))
  87. return PTR_ERR(crtc_state);
  88. crtc_state->mode_changed = true;
  89. list_for_each_entry(connector, &config->connector_list, head) {
  90. if (connector->state->best_encoder != encoder)
  91. continue;
  92. DRM_DEBUG_KMS("Stealing encoder from [CONNECTOR:%d:%s]\n",
  93. connector->base.id,
  94. connector->name);
  95. connector_state = drm_atomic_get_connector_state(state,
  96. connector);
  97. if (IS_ERR(connector_state))
  98. return PTR_ERR(connector_state);
  99. ret = drm_atomic_set_crtc_for_connector(connector_state, NULL);
  100. if (ret)
  101. return ret;
  102. connector_state->best_encoder = NULL;
  103. }
  104. return 0;
  105. }
  106. static int
  107. update_connector_routing(struct drm_atomic_state *state, int conn_idx)
  108. {
  109. struct drm_connector_helper_funcs *funcs;
  110. struct drm_encoder *new_encoder;
  111. struct drm_crtc *encoder_crtc;
  112. struct drm_connector *connector;
  113. struct drm_connector_state *connector_state;
  114. struct drm_crtc_state *crtc_state;
  115. int idx, ret;
  116. connector = state->connectors[conn_idx];
  117. connector_state = state->connector_states[conn_idx];
  118. if (!connector)
  119. return 0;
  120. DRM_DEBUG_KMS("Updating routing for [CONNECTOR:%d:%s]\n",
  121. connector->base.id,
  122. connector->name);
  123. if (connector->state->crtc != connector_state->crtc) {
  124. if (connector->state->crtc) {
  125. idx = drm_crtc_index(connector->state->crtc);
  126. crtc_state = state->crtc_states[idx];
  127. crtc_state->mode_changed = true;
  128. }
  129. if (connector_state->crtc) {
  130. idx = drm_crtc_index(connector_state->crtc);
  131. crtc_state = state->crtc_states[idx];
  132. crtc_state->mode_changed = true;
  133. }
  134. }
  135. if (!connector_state->crtc) {
  136. DRM_DEBUG_KMS("Disabling [CONNECTOR:%d:%s]\n",
  137. connector->base.id,
  138. connector->name);
  139. connector_state->best_encoder = NULL;
  140. return 0;
  141. }
  142. funcs = connector->helper_private;
  143. new_encoder = funcs->best_encoder(connector);
  144. if (!new_encoder) {
  145. DRM_DEBUG_KMS("No suitable encoder found for [CONNECTOR:%d:%s]\n",
  146. connector->base.id,
  147. connector->name);
  148. return -EINVAL;
  149. }
  150. if (new_encoder == connector_state->best_encoder) {
  151. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d]\n",
  152. connector->base.id,
  153. connector->name,
  154. new_encoder->base.id,
  155. new_encoder->name,
  156. connector_state->crtc->base.id);
  157. return 0;
  158. }
  159. encoder_crtc = get_current_crtc_for_encoder(state->dev,
  160. new_encoder);
  161. if (encoder_crtc) {
  162. ret = steal_encoder(state, new_encoder, encoder_crtc);
  163. if (ret) {
  164. DRM_DEBUG_KMS("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
  165. connector->base.id,
  166. connector->name);
  167. return ret;
  168. }
  169. }
  170. connector_state->best_encoder = new_encoder;
  171. idx = drm_crtc_index(connector_state->crtc);
  172. crtc_state = state->crtc_states[idx];
  173. crtc_state->mode_changed = true;
  174. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d]\n",
  175. connector->base.id,
  176. connector->name,
  177. new_encoder->base.id,
  178. new_encoder->name,
  179. connector_state->crtc->base.id);
  180. return 0;
  181. }
  182. static int
  183. mode_fixup(struct drm_atomic_state *state)
  184. {
  185. int ncrtcs = state->dev->mode_config.num_crtc;
  186. int nconnectors = state->dev->mode_config.num_connector;
  187. struct drm_crtc_state *crtc_state;
  188. struct drm_connector_state *conn_state;
  189. int i;
  190. bool ret;
  191. for (i = 0; i < ncrtcs; i++) {
  192. crtc_state = state->crtc_states[i];
  193. if (!crtc_state || !crtc_state->mode_changed)
  194. continue;
  195. drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
  196. }
  197. for (i = 0; i < nconnectors; i++) {
  198. struct drm_encoder_helper_funcs *funcs;
  199. struct drm_encoder *encoder;
  200. conn_state = state->connector_states[i];
  201. if (!conn_state)
  202. continue;
  203. WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
  204. if (!conn_state->crtc || !conn_state->best_encoder)
  205. continue;
  206. crtc_state =
  207. state->crtc_states[drm_crtc_index(conn_state->crtc)];
  208. /*
  209. * Each encoder has at most one connector (since we always steal
  210. * it away), so we won't call ->mode_fixup twice.
  211. */
  212. encoder = conn_state->best_encoder;
  213. funcs = encoder->helper_private;
  214. if (encoder->bridge && encoder->bridge->funcs->mode_fixup) {
  215. ret = encoder->bridge->funcs->mode_fixup(
  216. encoder->bridge, &crtc_state->mode,
  217. &crtc_state->adjusted_mode);
  218. if (!ret) {
  219. DRM_DEBUG_KMS("Bridge fixup failed\n");
  220. return -EINVAL;
  221. }
  222. }
  223. ret = funcs->mode_fixup(encoder, &crtc_state->mode,
  224. &crtc_state->adjusted_mode);
  225. if (!ret) {
  226. DRM_DEBUG_KMS("[ENCODER:%d:%s] fixup failed\n",
  227. encoder->base.id, encoder->name);
  228. return -EINVAL;
  229. }
  230. }
  231. for (i = 0; i < ncrtcs; i++) {
  232. struct drm_crtc_helper_funcs *funcs;
  233. struct drm_crtc *crtc;
  234. crtc_state = state->crtc_states[i];
  235. crtc = state->crtcs[i];
  236. if (!crtc_state || !crtc_state->mode_changed)
  237. continue;
  238. funcs = crtc->helper_private;
  239. ret = funcs->mode_fixup(crtc, &crtc_state->mode,
  240. &crtc_state->adjusted_mode);
  241. if (!ret) {
  242. DRM_DEBUG_KMS("[CRTC:%d] fixup failed\n",
  243. crtc->base.id);
  244. return -EINVAL;
  245. }
  246. }
  247. return 0;
  248. }
  249. static int
  250. drm_atomic_helper_check_prepare(struct drm_device *dev,
  251. struct drm_atomic_state *state)
  252. {
  253. int ncrtcs = dev->mode_config.num_crtc;
  254. int nconnectors = dev->mode_config.num_connector;
  255. struct drm_crtc *crtc;
  256. struct drm_crtc_state *crtc_state;
  257. int i, ret;
  258. for (i = 0; i < ncrtcs; i++) {
  259. crtc = state->crtcs[i];
  260. crtc_state = state->crtc_states[i];
  261. if (!crtc)
  262. continue;
  263. if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
  264. DRM_DEBUG_KMS("[CRTC:%d] mode changed\n",
  265. crtc->base.id);
  266. crtc_state->mode_changed = true;
  267. }
  268. if (crtc->state->enable != crtc_state->enable) {
  269. DRM_DEBUG_KMS("[CRTC:%d] enable changed\n",
  270. crtc->base.id);
  271. crtc_state->mode_changed = true;
  272. }
  273. }
  274. for (i = 0; i < nconnectors; i++) {
  275. /*
  276. * This only sets crtc->mode_changed for routing changes,
  277. * drivers must set crtc->mode_changed themselves when connector
  278. * properties need to be updated.
  279. */
  280. ret = update_connector_routing(state, i);
  281. if (ret)
  282. return ret;
  283. }
  284. /*
  285. * After all the routing has been prepared we need to add in any
  286. * connector which is itself unchanged, but who's crtc changes it's
  287. * configuration. This must be done before calling mode_fixup in case a
  288. * crtc only changed its mode but has the same set of connectors.
  289. */
  290. for (i = 0; i < ncrtcs; i++) {
  291. int num_connectors;
  292. crtc = state->crtcs[i];
  293. crtc_state = state->crtc_states[i];
  294. if (!crtc || !crtc_state->mode_changed)
  295. continue;
  296. DRM_DEBUG_KMS("[CRTC:%d] needs full modeset, enable: %c\n",
  297. crtc->base.id,
  298. crtc_state->enable ? 'y' : 'n');
  299. ret = drm_atomic_add_affected_connectors(state, crtc);
  300. if (ret != 0)
  301. return ret;
  302. num_connectors = drm_atomic_connectors_for_crtc(state,
  303. crtc);
  304. if (crtc_state->enable != !!num_connectors) {
  305. DRM_DEBUG_KMS("[CRTC:%d] enabled/connectors mismatch\n",
  306. crtc->base.id);
  307. return -EINVAL;
  308. }
  309. }
  310. return mode_fixup(state);
  311. }
  312. /**
  313. * drm_atomic_helper_check - validate state object
  314. * @dev: DRM device
  315. * @state: the driver state object
  316. *
  317. * Check the state object to see if the requested state is physically possible.
  318. * Only crtcs and planes have check callbacks, so for any additional (global)
  319. * checking that a driver needs it can simply wrap that around this function.
  320. * Drivers without such needs can directly use this as their ->atomic_check()
  321. * callback.
  322. *
  323. * RETURNS
  324. * Zero for success or -errno
  325. */
  326. int drm_atomic_helper_check(struct drm_device *dev,
  327. struct drm_atomic_state *state)
  328. {
  329. int nplanes = dev->mode_config.num_total_plane;
  330. int ncrtcs = dev->mode_config.num_crtc;
  331. int i, ret = 0;
  332. ret = drm_atomic_helper_check_prepare(dev, state);
  333. if (ret)
  334. return ret;
  335. for (i = 0; i < nplanes; i++) {
  336. struct drm_plane_helper_funcs *funcs;
  337. struct drm_plane *plane = state->planes[i];
  338. struct drm_plane_state *plane_state = state->plane_states[i];
  339. if (!plane)
  340. continue;
  341. funcs = plane->helper_private;
  342. drm_atomic_helper_plane_changed(state, plane_state, plane);
  343. if (!funcs || !funcs->atomic_check)
  344. continue;
  345. ret = funcs->atomic_check(plane, plane_state);
  346. if (ret) {
  347. DRM_DEBUG_KMS("[PLANE:%d] atomic check failed\n",
  348. plane->base.id);
  349. return ret;
  350. }
  351. }
  352. for (i = 0; i < ncrtcs; i++) {
  353. struct drm_crtc_helper_funcs *funcs;
  354. struct drm_crtc *crtc = state->crtcs[i];
  355. if (!crtc)
  356. continue;
  357. funcs = crtc->helper_private;
  358. if (!funcs || !funcs->atomic_check)
  359. continue;
  360. ret = funcs->atomic_check(crtc, state->crtc_states[i]);
  361. if (ret) {
  362. DRM_DEBUG_KMS("[CRTC:%d] atomic check failed\n",
  363. crtc->base.id);
  364. return ret;
  365. }
  366. }
  367. return ret;
  368. }
  369. EXPORT_SYMBOL(drm_atomic_helper_check);
  370. static void
  371. disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
  372. {
  373. int ncrtcs = old_state->dev->mode_config.num_crtc;
  374. int nconnectors = old_state->dev->mode_config.num_connector;
  375. int i;
  376. for (i = 0; i < nconnectors; i++) {
  377. struct drm_connector_state *old_conn_state;
  378. struct drm_connector *connector;
  379. struct drm_encoder_helper_funcs *funcs;
  380. struct drm_encoder *encoder;
  381. old_conn_state = old_state->connector_states[i];
  382. connector = old_state->connectors[i];
  383. /* Shut down everything that's in the changeset and currently
  384. * still on. So need to check the old, saved state. */
  385. if (!old_conn_state || !old_conn_state->crtc)
  386. continue;
  387. encoder = connector->state->best_encoder;
  388. if (!encoder)
  389. continue;
  390. funcs = encoder->helper_private;
  391. /*
  392. * Each encoder has at most one connector (since we always steal
  393. * it away), so we won't call call disable hooks twice.
  394. */
  395. if (encoder->bridge)
  396. encoder->bridge->funcs->disable(encoder->bridge);
  397. /* Right function depends upon target state. */
  398. if (connector->state->crtc)
  399. funcs->prepare(encoder);
  400. else if (funcs->disable)
  401. funcs->disable(encoder);
  402. else
  403. funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  404. if (encoder->bridge)
  405. encoder->bridge->funcs->post_disable(encoder->bridge);
  406. }
  407. for (i = 0; i < ncrtcs; i++) {
  408. struct drm_crtc_helper_funcs *funcs;
  409. struct drm_crtc *crtc;
  410. crtc = old_state->crtcs[i];
  411. /* Shut down everything that needs a full modeset. */
  412. if (!crtc || !crtc->state->mode_changed)
  413. continue;
  414. funcs = crtc->helper_private;
  415. /* Right function depends upon target state. */
  416. if (crtc->state->enable)
  417. funcs->prepare(crtc);
  418. else if (funcs->disable)
  419. funcs->disable(crtc);
  420. else
  421. funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  422. }
  423. }
  424. static void
  425. set_routing_links(struct drm_device *dev, struct drm_atomic_state *old_state)
  426. {
  427. int nconnectors = dev->mode_config.num_connector;
  428. int ncrtcs = old_state->dev->mode_config.num_crtc;
  429. int i;
  430. /* clear out existing links */
  431. for (i = 0; i < nconnectors; i++) {
  432. struct drm_connector *connector;
  433. connector = old_state->connectors[i];
  434. if (!connector || !connector->encoder)
  435. continue;
  436. WARN_ON(!connector->encoder->crtc);
  437. connector->encoder->crtc = NULL;
  438. connector->encoder = NULL;
  439. }
  440. /* set new links */
  441. for (i = 0; i < nconnectors; i++) {
  442. struct drm_connector *connector;
  443. connector = old_state->connectors[i];
  444. if (!connector || !connector->state->crtc)
  445. continue;
  446. if (WARN_ON(!connector->state->best_encoder))
  447. continue;
  448. connector->encoder = connector->state->best_encoder;
  449. connector->encoder->crtc = connector->state->crtc;
  450. }
  451. /* set legacy state in the crtc structure */
  452. for (i = 0; i < ncrtcs; i++) {
  453. struct drm_crtc *crtc;
  454. crtc = old_state->crtcs[i];
  455. if (!crtc)
  456. continue;
  457. crtc->mode = crtc->state->mode;
  458. crtc->enabled = crtc->state->enable;
  459. crtc->x = crtc->primary->state->src_x >> 16;
  460. crtc->y = crtc->primary->state->src_y >> 16;
  461. }
  462. }
  463. static void
  464. crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
  465. {
  466. int ncrtcs = old_state->dev->mode_config.num_crtc;
  467. int nconnectors = old_state->dev->mode_config.num_connector;
  468. int i;
  469. for (i = 0; i < ncrtcs; i++) {
  470. struct drm_crtc_helper_funcs *funcs;
  471. struct drm_crtc *crtc;
  472. crtc = old_state->crtcs[i];
  473. if (!crtc || !crtc->state->mode_changed)
  474. continue;
  475. funcs = crtc->helper_private;
  476. if (crtc->state->enable)
  477. funcs->mode_set_nofb(crtc);
  478. }
  479. for (i = 0; i < nconnectors; i++) {
  480. struct drm_connector *connector;
  481. struct drm_crtc_state *new_crtc_state;
  482. struct drm_encoder_helper_funcs *funcs;
  483. struct drm_encoder *encoder;
  484. struct drm_display_mode *mode, *adjusted_mode;
  485. connector = old_state->connectors[i];
  486. if (!connector || !connector->state->best_encoder)
  487. continue;
  488. encoder = connector->state->best_encoder;
  489. funcs = encoder->helper_private;
  490. new_crtc_state = connector->state->crtc->state;
  491. mode = &new_crtc_state->mode;
  492. adjusted_mode = &new_crtc_state->adjusted_mode;
  493. /*
  494. * Each encoder has at most one connector (since we always steal
  495. * it away), so we won't call call mode_set hooks twice.
  496. */
  497. funcs->mode_set(encoder, mode, adjusted_mode);
  498. if (encoder->bridge && encoder->bridge->funcs->mode_set)
  499. encoder->bridge->funcs->mode_set(encoder->bridge,
  500. mode, adjusted_mode);
  501. }
  502. }
  503. /**
  504. * drm_atomic_helper_commit_pre_planes - modeset commit before plane updates
  505. * @dev: DRM device
  506. * @state: atomic state
  507. *
  508. * This function commits the modeset changes that need to be committed before
  509. * updating planes. It shuts down all the outputs that need to be shut down and
  510. * prepares them (if required) with the new mode.
  511. */
  512. void drm_atomic_helper_commit_pre_planes(struct drm_device *dev,
  513. struct drm_atomic_state *state)
  514. {
  515. disable_outputs(dev, state);
  516. set_routing_links(dev, state);
  517. crtc_set_mode(dev, state);
  518. }
  519. EXPORT_SYMBOL(drm_atomic_helper_commit_pre_planes);
  520. /**
  521. * drm_atomic_helper_commit_post_planes - modeset commit after plane updates
  522. * @dev: DRM device
  523. * @old_state: atomic state object with old state structures
  524. *
  525. * This function commits the modeset changes that need to be committed after
  526. * updating planes: It enables all the outputs with the new configuration which
  527. * had to be turned off for the update.
  528. */
  529. void drm_atomic_helper_commit_post_planes(struct drm_device *dev,
  530. struct drm_atomic_state *old_state)
  531. {
  532. int ncrtcs = old_state->dev->mode_config.num_crtc;
  533. int nconnectors = old_state->dev->mode_config.num_connector;
  534. int i;
  535. for (i = 0; i < ncrtcs; i++) {
  536. struct drm_crtc_helper_funcs *funcs;
  537. struct drm_crtc *crtc;
  538. crtc = old_state->crtcs[i];
  539. /* Need to filter out CRTCs where only planes change. */
  540. if (!crtc || !crtc->state->mode_changed)
  541. continue;
  542. funcs = crtc->helper_private;
  543. if (crtc->state->enable)
  544. funcs->commit(crtc);
  545. }
  546. for (i = 0; i < nconnectors; i++) {
  547. struct drm_connector *connector;
  548. struct drm_encoder_helper_funcs *funcs;
  549. struct drm_encoder *encoder;
  550. connector = old_state->connectors[i];
  551. if (!connector || !connector->state->best_encoder)
  552. continue;
  553. encoder = connector->state->best_encoder;
  554. funcs = encoder->helper_private;
  555. /*
  556. * Each encoder has at most one connector (since we always steal
  557. * it away), so we won't call call enable hooks twice.
  558. */
  559. if (encoder->bridge)
  560. encoder->bridge->funcs->pre_enable(encoder->bridge);
  561. funcs->commit(encoder);
  562. if (encoder->bridge)
  563. encoder->bridge->funcs->enable(encoder->bridge);
  564. }
  565. }
  566. EXPORT_SYMBOL(drm_atomic_helper_commit_post_planes);
  567. static void wait_for_fences(struct drm_device *dev,
  568. struct drm_atomic_state *state)
  569. {
  570. int nplanes = dev->mode_config.num_total_plane;
  571. int i;
  572. for (i = 0; i < nplanes; i++) {
  573. struct drm_plane *plane = state->planes[i];
  574. if (!plane || !plane->state->fence)
  575. continue;
  576. WARN_ON(!plane->state->fb);
  577. fence_wait(plane->state->fence, false);
  578. fence_put(plane->state->fence);
  579. plane->state->fence = NULL;
  580. }
  581. }
  582. static void
  583. wait_for_vblanks(struct drm_device *dev, struct drm_atomic_state *old_state)
  584. {
  585. struct drm_crtc *crtc;
  586. struct drm_crtc_state *old_crtc_state;
  587. int ncrtcs = old_state->dev->mode_config.num_crtc;
  588. int i, ret;
  589. for (i = 0; i < ncrtcs; i++) {
  590. crtc = old_state->crtcs[i];
  591. old_crtc_state = old_state->crtc_states[i];
  592. if (!crtc)
  593. continue;
  594. /* No one cares about the old state, so abuse it for tracking
  595. * and store whether we hold a vblank reference (and should do a
  596. * vblank wait) in the ->enable boolean. */
  597. old_crtc_state->enable = false;
  598. if (!crtc->state->enable)
  599. continue;
  600. ret = drm_crtc_vblank_get(crtc);
  601. if (ret != 0)
  602. continue;
  603. old_crtc_state->enable = true;
  604. old_crtc_state->last_vblank_count = drm_vblank_count(dev, i);
  605. }
  606. for (i = 0; i < ncrtcs; i++) {
  607. crtc = old_state->crtcs[i];
  608. old_crtc_state = old_state->crtc_states[i];
  609. if (!crtc || !old_crtc_state->enable)
  610. continue;
  611. ret = wait_event_timeout(dev->vblank[i].queue,
  612. old_crtc_state->last_vblank_count !=
  613. drm_vblank_count(dev, i),
  614. msecs_to_jiffies(50));
  615. drm_crtc_vblank_put(crtc);
  616. }
  617. }
  618. /**
  619. * drm_atomic_helper_commit - commit validated state object
  620. * @dev: DRM device
  621. * @state: the driver state object
  622. * @async: asynchronous commit
  623. *
  624. * This function commits a with drm_atomic_helper_check() pre-validated state
  625. * object. This can still fail when e.g. the framebuffer reservation fails. For
  626. * now this doesn't implement asynchronous commits.
  627. *
  628. * RETURNS
  629. * Zero for success or -errno.
  630. */
  631. int drm_atomic_helper_commit(struct drm_device *dev,
  632. struct drm_atomic_state *state,
  633. bool async)
  634. {
  635. int ret;
  636. if (async)
  637. return -EBUSY;
  638. ret = drm_atomic_helper_prepare_planes(dev, state);
  639. if (ret)
  640. return ret;
  641. /*
  642. * This is the point of no return - everything below never fails except
  643. * when the hw goes bonghits. Which means we can commit the new state on
  644. * the software side now.
  645. */
  646. drm_atomic_helper_swap_state(dev, state);
  647. /*
  648. * Everything below can be run asynchronously without the need to grab
  649. * any modeset locks at all under one conditions: It must be guaranteed
  650. * that the asynchronous work has either been cancelled (if the driver
  651. * supports it, which at least requires that the framebuffers get
  652. * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
  653. * before the new state gets committed on the software side with
  654. * drm_atomic_helper_swap_state().
  655. *
  656. * This scheme allows new atomic state updates to be prepared and
  657. * checked in parallel to the asynchronous completion of the previous
  658. * update. Which is important since compositors need to figure out the
  659. * composition of the next frame right after having submitted the
  660. * current layout.
  661. */
  662. wait_for_fences(dev, state);
  663. drm_atomic_helper_commit_pre_planes(dev, state);
  664. drm_atomic_helper_commit_planes(dev, state);
  665. drm_atomic_helper_commit_post_planes(dev, state);
  666. wait_for_vblanks(dev, state);
  667. drm_atomic_helper_cleanup_planes(dev, state);
  668. drm_atomic_state_free(state);
  669. return 0;
  670. }
  671. EXPORT_SYMBOL(drm_atomic_helper_commit);
  672. /**
  673. * drm_atomic_helper_prepare_planes - prepare plane resources after commit
  674. * @dev: DRM device
  675. * @state: atomic state object with old state structures
  676. *
  677. * This function prepares plane state, specifically framebuffers, for the new
  678. * configuration. If any failure is encountered this function will call
  679. * ->cleanup_fb on any already successfully prepared framebuffer.
  680. *
  681. * Returns:
  682. * 0 on success, negative error code on failure.
  683. */
  684. int drm_atomic_helper_prepare_planes(struct drm_device *dev,
  685. struct drm_atomic_state *state)
  686. {
  687. int nplanes = dev->mode_config.num_total_plane;
  688. int ret, i;
  689. for (i = 0; i < nplanes; i++) {
  690. struct drm_plane_helper_funcs *funcs;
  691. struct drm_plane *plane = state->planes[i];
  692. struct drm_framebuffer *fb;
  693. if (!plane)
  694. continue;
  695. funcs = plane->helper_private;
  696. fb = state->plane_states[i]->fb;
  697. if (fb && funcs->prepare_fb) {
  698. ret = funcs->prepare_fb(plane, fb);
  699. if (ret)
  700. goto fail;
  701. }
  702. }
  703. return 0;
  704. fail:
  705. for (i--; i >= 0; i--) {
  706. struct drm_plane_helper_funcs *funcs;
  707. struct drm_plane *plane = state->planes[i];
  708. struct drm_framebuffer *fb;
  709. if (!plane)
  710. continue;
  711. funcs = plane->helper_private;
  712. fb = state->plane_states[i]->fb;
  713. if (fb && funcs->cleanup_fb)
  714. funcs->cleanup_fb(plane, fb);
  715. }
  716. return ret;
  717. }
  718. EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
  719. /**
  720. * drm_atomic_helper_commit_planes - commit plane state
  721. * @dev: DRM device
  722. * @state: atomic state
  723. *
  724. * This function commits the new plane state using the plane and atomic helper
  725. * functions for planes and crtcs. It assumes that the atomic state has already
  726. * been pushed into the relevant object state pointers, since this step can no
  727. * longer fail.
  728. *
  729. * It still requires the global state object @state to know which planes and
  730. * crtcs need to be updated though.
  731. */
  732. void drm_atomic_helper_commit_planes(struct drm_device *dev,
  733. struct drm_atomic_state *state)
  734. {
  735. int nplanes = dev->mode_config.num_total_plane;
  736. int ncrtcs = dev->mode_config.num_crtc;
  737. int i;
  738. for (i = 0; i < ncrtcs; i++) {
  739. struct drm_crtc_helper_funcs *funcs;
  740. struct drm_crtc *crtc = state->crtcs[i];
  741. if (!crtc)
  742. continue;
  743. funcs = crtc->helper_private;
  744. if (!funcs || !funcs->atomic_begin)
  745. continue;
  746. funcs->atomic_begin(crtc);
  747. }
  748. for (i = 0; i < nplanes; i++) {
  749. struct drm_plane_helper_funcs *funcs;
  750. struct drm_plane *plane = state->planes[i];
  751. if (!plane)
  752. continue;
  753. funcs = plane->helper_private;
  754. if (!funcs || !funcs->atomic_update)
  755. continue;
  756. funcs->atomic_update(plane);
  757. }
  758. for (i = 0; i < ncrtcs; i++) {
  759. struct drm_crtc_helper_funcs *funcs;
  760. struct drm_crtc *crtc = state->crtcs[i];
  761. if (!crtc)
  762. continue;
  763. funcs = crtc->helper_private;
  764. if (!funcs || !funcs->atomic_flush)
  765. continue;
  766. funcs->atomic_flush(crtc);
  767. }
  768. }
  769. EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
  770. /**
  771. * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
  772. * @dev: DRM device
  773. * @old_state: atomic state object with old state structures
  774. *
  775. * This function cleans up plane state, specifically framebuffers, from the old
  776. * configuration. Hence the old configuration must be perserved in @old_state to
  777. * be able to call this function.
  778. *
  779. * This function must also be called on the new state when the atomic update
  780. * fails at any point after calling drm_atomic_helper_prepare_planes().
  781. */
  782. void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
  783. struct drm_atomic_state *old_state)
  784. {
  785. int nplanes = dev->mode_config.num_total_plane;
  786. int i;
  787. for (i = 0; i < nplanes; i++) {
  788. struct drm_plane_helper_funcs *funcs;
  789. struct drm_plane *plane = old_state->planes[i];
  790. struct drm_framebuffer *old_fb;
  791. if (!plane)
  792. continue;
  793. funcs = plane->helper_private;
  794. old_fb = old_state->plane_states[i]->fb;
  795. if (old_fb && funcs->cleanup_fb)
  796. funcs->cleanup_fb(plane, old_fb);
  797. }
  798. }
  799. EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
  800. /**
  801. * drm_atomic_helper_swap_state - store atomic state into current sw state
  802. * @dev: DRM device
  803. * @state: atomic state
  804. *
  805. * This function stores the atomic state into the current state pointers in all
  806. * driver objects. It should be called after all failing steps have been done
  807. * and succeeded, but before the actual hardware state is committed.
  808. *
  809. * For cleanup and error recovery the current state for all changed objects will
  810. * be swaped into @state.
  811. *
  812. * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
  813. *
  814. * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
  815. *
  816. * 2. Do any other steps that might fail.
  817. *
  818. * 3. Put the staged state into the current state pointers with this function.
  819. *
  820. * 4. Actually commit the hardware state.
  821. *
  822. * 5. Call drm_atomic_helper_cleanup_planes with @state, which since step 3
  823. * contains the old state. Also do any other cleanup required with that state.
  824. */
  825. void drm_atomic_helper_swap_state(struct drm_device *dev,
  826. struct drm_atomic_state *state)
  827. {
  828. int i;
  829. for (i = 0; i < dev->mode_config.num_connector; i++) {
  830. struct drm_connector *connector = state->connectors[i];
  831. if (!connector)
  832. continue;
  833. connector->state->state = state;
  834. swap(state->connector_states[i], connector->state);
  835. connector->state->state = NULL;
  836. }
  837. for (i = 0; i < dev->mode_config.num_crtc; i++) {
  838. struct drm_crtc *crtc = state->crtcs[i];
  839. if (!crtc)
  840. continue;
  841. crtc->state->state = state;
  842. swap(state->crtc_states[i], crtc->state);
  843. crtc->state->state = NULL;
  844. }
  845. for (i = 0; i < dev->mode_config.num_total_plane; i++) {
  846. struct drm_plane *plane = state->planes[i];
  847. if (!plane)
  848. continue;
  849. plane->state->state = state;
  850. swap(state->plane_states[i], plane->state);
  851. plane->state->state = NULL;
  852. }
  853. }
  854. EXPORT_SYMBOL(drm_atomic_helper_swap_state);
  855. /**
  856. * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
  857. * @plane: plane object to update
  858. * @crtc: owning CRTC of owning plane
  859. * @fb: framebuffer to flip onto plane
  860. * @crtc_x: x offset of primary plane on crtc
  861. * @crtc_y: y offset of primary plane on crtc
  862. * @crtc_w: width of primary plane rectangle on crtc
  863. * @crtc_h: height of primary plane rectangle on crtc
  864. * @src_x: x offset of @fb for panning
  865. * @src_y: y offset of @fb for panning
  866. * @src_w: width of source rectangle in @fb
  867. * @src_h: height of source rectangle in @fb
  868. *
  869. * Provides a default plane update handler using the atomic driver interface.
  870. *
  871. * RETURNS:
  872. * Zero on success, error code on failure
  873. */
  874. int drm_atomic_helper_update_plane(struct drm_plane *plane,
  875. struct drm_crtc *crtc,
  876. struct drm_framebuffer *fb,
  877. int crtc_x, int crtc_y,
  878. unsigned int crtc_w, unsigned int crtc_h,
  879. uint32_t src_x, uint32_t src_y,
  880. uint32_t src_w, uint32_t src_h)
  881. {
  882. struct drm_atomic_state *state;
  883. struct drm_plane_state *plane_state;
  884. int ret = 0;
  885. state = drm_atomic_state_alloc(plane->dev);
  886. if (!state)
  887. return -ENOMEM;
  888. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  889. retry:
  890. plane_state = drm_atomic_get_plane_state(state, plane);
  891. if (IS_ERR(plane_state)) {
  892. ret = PTR_ERR(plane_state);
  893. goto fail;
  894. }
  895. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  896. if (ret != 0)
  897. goto fail;
  898. plane_state->fb = fb;
  899. plane_state->crtc_x = crtc_x;
  900. plane_state->crtc_y = crtc_y;
  901. plane_state->crtc_h = crtc_h;
  902. plane_state->crtc_w = crtc_w;
  903. plane_state->src_x = src_x;
  904. plane_state->src_y = src_y;
  905. plane_state->src_h = src_h;
  906. plane_state->src_w = src_w;
  907. ret = drm_atomic_commit(state);
  908. if (ret != 0)
  909. goto fail;
  910. /* Driver takes ownership of state on successful commit. */
  911. return 0;
  912. fail:
  913. if (ret == -EDEADLK)
  914. goto backoff;
  915. drm_atomic_state_free(state);
  916. return ret;
  917. backoff:
  918. drm_atomic_legacy_backoff(state);
  919. drm_atomic_state_clear(state);
  920. /*
  921. * Someone might have exchanged the framebuffer while we dropped locks
  922. * in the backoff code. We need to fix up the fb refcount tracking the
  923. * core does for us.
  924. */
  925. plane->old_fb = plane->fb;
  926. goto retry;
  927. }
  928. EXPORT_SYMBOL(drm_atomic_helper_update_plane);
  929. /**
  930. * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
  931. * @plane: plane to disable
  932. *
  933. * Provides a default plane disable handler using the atomic driver interface.
  934. *
  935. * RETURNS:
  936. * Zero on success, error code on failure
  937. */
  938. int drm_atomic_helper_disable_plane(struct drm_plane *plane)
  939. {
  940. struct drm_atomic_state *state;
  941. struct drm_plane_state *plane_state;
  942. int ret = 0;
  943. state = drm_atomic_state_alloc(plane->dev);
  944. if (!state)
  945. return -ENOMEM;
  946. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
  947. retry:
  948. plane_state = drm_atomic_get_plane_state(state, plane);
  949. if (IS_ERR(plane_state)) {
  950. ret = PTR_ERR(plane_state);
  951. goto fail;
  952. }
  953. ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
  954. if (ret != 0)
  955. goto fail;
  956. plane_state->fb = NULL;
  957. plane_state->crtc_x = 0;
  958. plane_state->crtc_y = 0;
  959. plane_state->crtc_h = 0;
  960. plane_state->crtc_w = 0;
  961. plane_state->src_x = 0;
  962. plane_state->src_y = 0;
  963. plane_state->src_h = 0;
  964. plane_state->src_w = 0;
  965. ret = drm_atomic_commit(state);
  966. if (ret != 0)
  967. goto fail;
  968. /* Driver takes ownership of state on successful commit. */
  969. return 0;
  970. fail:
  971. if (ret == -EDEADLK)
  972. goto backoff;
  973. drm_atomic_state_free(state);
  974. return ret;
  975. backoff:
  976. drm_atomic_legacy_backoff(state);
  977. drm_atomic_state_clear(state);
  978. /*
  979. * Someone might have exchanged the framebuffer while we dropped locks
  980. * in the backoff code. We need to fix up the fb refcount tracking the
  981. * core does for us.
  982. */
  983. plane->old_fb = plane->fb;
  984. goto retry;
  985. }
  986. EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
  987. static int update_output_state(struct drm_atomic_state *state,
  988. struct drm_mode_set *set)
  989. {
  990. struct drm_device *dev = set->crtc->dev;
  991. struct drm_connector_state *conn_state;
  992. int nconnectors = state->dev->mode_config.num_connector;
  993. int ncrtcs = state->dev->mode_config.num_crtc;
  994. int ret, i, j;
  995. ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
  996. state->acquire_ctx);
  997. if (ret)
  998. return ret;
  999. /* First grab all affected connector/crtc states. */
  1000. for (i = 0; i < set->num_connectors; i++) {
  1001. conn_state = drm_atomic_get_connector_state(state,
  1002. set->connectors[i]);
  1003. if (IS_ERR(conn_state))
  1004. return PTR_ERR(conn_state);
  1005. }
  1006. for (i = 0; i < ncrtcs; i++) {
  1007. struct drm_crtc *crtc = state->crtcs[i];
  1008. if (!crtc)
  1009. continue;
  1010. ret = drm_atomic_add_affected_connectors(state, crtc);
  1011. if (ret)
  1012. return ret;
  1013. }
  1014. /* Then recompute connector->crtc links and crtc enabling state. */
  1015. for (i = 0; i < nconnectors; i++) {
  1016. struct drm_connector *connector;
  1017. connector = state->connectors[i];
  1018. conn_state = state->connector_states[i];
  1019. if (!connector)
  1020. continue;
  1021. if (conn_state->crtc == set->crtc) {
  1022. ret = drm_atomic_set_crtc_for_connector(conn_state,
  1023. NULL);
  1024. if (ret)
  1025. return ret;
  1026. }
  1027. for (j = 0; j < set->num_connectors; j++) {
  1028. if (set->connectors[j] == connector) {
  1029. ret = drm_atomic_set_crtc_for_connector(conn_state,
  1030. set->crtc);
  1031. if (ret)
  1032. return ret;
  1033. break;
  1034. }
  1035. }
  1036. }
  1037. for (i = 0; i < ncrtcs; i++) {
  1038. struct drm_crtc *crtc = state->crtcs[i];
  1039. struct drm_crtc_state *crtc_state = state->crtc_states[i];
  1040. if (!crtc)
  1041. continue;
  1042. /* Don't update ->enable for the CRTC in the set_config request,
  1043. * since a mismatch would indicate a bug in the upper layers.
  1044. * The actual modeset code later on will catch any
  1045. * inconsistencies here. */
  1046. if (crtc == set->crtc)
  1047. continue;
  1048. crtc_state->enable =
  1049. drm_atomic_connectors_for_crtc(state, crtc);
  1050. }
  1051. return 0;
  1052. }
  1053. /**
  1054. * drm_atomic_helper_set_config - set a new config from userspace
  1055. * @set: mode set configuration
  1056. *
  1057. * Provides a default crtc set_config handler using the atomic driver interface.
  1058. *
  1059. * Returns:
  1060. * Returns 0 on success, negative errno numbers on failure.
  1061. */
  1062. int drm_atomic_helper_set_config(struct drm_mode_set *set)
  1063. {
  1064. struct drm_atomic_state *state;
  1065. struct drm_crtc *crtc = set->crtc;
  1066. struct drm_crtc_state *crtc_state;
  1067. struct drm_plane_state *primary_state;
  1068. int ret = 0;
  1069. state = drm_atomic_state_alloc(crtc->dev);
  1070. if (!state)
  1071. return -ENOMEM;
  1072. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  1073. retry:
  1074. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  1075. if (IS_ERR(crtc_state)) {
  1076. ret = PTR_ERR(crtc_state);
  1077. goto fail;
  1078. }
  1079. if (!set->mode) {
  1080. WARN_ON(set->fb);
  1081. WARN_ON(set->num_connectors);
  1082. crtc_state->enable = false;
  1083. goto commit;
  1084. }
  1085. WARN_ON(!set->fb);
  1086. WARN_ON(!set->num_connectors);
  1087. crtc_state->enable = true;
  1088. drm_mode_copy(&crtc_state->mode, set->mode);
  1089. primary_state = drm_atomic_get_plane_state(state, crtc->primary);
  1090. if (IS_ERR(primary_state)) {
  1091. ret = PTR_ERR(primary_state);
  1092. goto fail;
  1093. }
  1094. ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
  1095. if (ret != 0)
  1096. goto fail;
  1097. primary_state->fb = set->fb;
  1098. primary_state->crtc_x = 0;
  1099. primary_state->crtc_y = 0;
  1100. primary_state->crtc_h = set->mode->vdisplay;
  1101. primary_state->crtc_w = set->mode->hdisplay;
  1102. primary_state->src_x = set->x << 16;
  1103. primary_state->src_y = set->y << 16;
  1104. primary_state->src_h = set->mode->vdisplay << 16;
  1105. primary_state->src_w = set->mode->hdisplay << 16;
  1106. commit:
  1107. ret = update_output_state(state, set);
  1108. if (ret)
  1109. goto fail;
  1110. ret = drm_atomic_commit(state);
  1111. if (ret != 0)
  1112. goto fail;
  1113. /* Driver takes ownership of state on successful commit. */
  1114. return 0;
  1115. fail:
  1116. if (ret == -EDEADLK)
  1117. goto backoff;
  1118. drm_atomic_state_free(state);
  1119. return ret;
  1120. backoff:
  1121. drm_atomic_legacy_backoff(state);
  1122. drm_atomic_state_clear(state);
  1123. /*
  1124. * Someone might have exchanged the framebuffer while we dropped locks
  1125. * in the backoff code. We need to fix up the fb refcount tracking the
  1126. * core does for us.
  1127. */
  1128. crtc->primary->old_fb = crtc->primary->fb;
  1129. goto retry;
  1130. }
  1131. EXPORT_SYMBOL(drm_atomic_helper_set_config);
  1132. /**
  1133. * drm_atomic_helper_crtc_set_property - helper for crtc prorties
  1134. * @crtc: DRM crtc
  1135. * @property: DRM property
  1136. * @val: value of property
  1137. *
  1138. * Provides a default plane disablle handler using the atomic driver interface.
  1139. *
  1140. * RETURNS:
  1141. * Zero on success, error code on failure
  1142. */
  1143. int
  1144. drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
  1145. struct drm_property *property,
  1146. uint64_t val)
  1147. {
  1148. struct drm_atomic_state *state;
  1149. struct drm_crtc_state *crtc_state;
  1150. int ret = 0;
  1151. state = drm_atomic_state_alloc(crtc->dev);
  1152. if (!state)
  1153. return -ENOMEM;
  1154. /* ->set_property is always called with all locks held. */
  1155. state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
  1156. retry:
  1157. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  1158. if (IS_ERR(crtc_state)) {
  1159. ret = PTR_ERR(crtc_state);
  1160. goto fail;
  1161. }
  1162. ret = crtc->funcs->atomic_set_property(crtc, crtc_state,
  1163. property, val);
  1164. if (ret)
  1165. goto fail;
  1166. ret = drm_atomic_commit(state);
  1167. if (ret != 0)
  1168. goto fail;
  1169. /* Driver takes ownership of state on successful commit. */
  1170. return 0;
  1171. fail:
  1172. if (ret == -EDEADLK)
  1173. goto backoff;
  1174. drm_atomic_state_free(state);
  1175. return ret;
  1176. backoff:
  1177. drm_atomic_legacy_backoff(state);
  1178. drm_atomic_state_clear(state);
  1179. goto retry;
  1180. }
  1181. EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
  1182. /**
  1183. * drm_atomic_helper_plane_set_property - helper for plane prorties
  1184. * @plane: DRM plane
  1185. * @property: DRM property
  1186. * @val: value of property
  1187. *
  1188. * Provides a default plane disable handler using the atomic driver interface.
  1189. *
  1190. * RETURNS:
  1191. * Zero on success, error code on failure
  1192. */
  1193. int
  1194. drm_atomic_helper_plane_set_property(struct drm_plane *plane,
  1195. struct drm_property *property,
  1196. uint64_t val)
  1197. {
  1198. struct drm_atomic_state *state;
  1199. struct drm_plane_state *plane_state;
  1200. int ret = 0;
  1201. state = drm_atomic_state_alloc(plane->dev);
  1202. if (!state)
  1203. return -ENOMEM;
  1204. /* ->set_property is always called with all locks held. */
  1205. state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
  1206. retry:
  1207. plane_state = drm_atomic_get_plane_state(state, plane);
  1208. if (IS_ERR(plane_state)) {
  1209. ret = PTR_ERR(plane_state);
  1210. goto fail;
  1211. }
  1212. ret = plane->funcs->atomic_set_property(plane, plane_state,
  1213. property, val);
  1214. if (ret)
  1215. goto fail;
  1216. ret = drm_atomic_commit(state);
  1217. if (ret != 0)
  1218. goto fail;
  1219. /* Driver takes ownership of state on successful commit. */
  1220. return 0;
  1221. fail:
  1222. if (ret == -EDEADLK)
  1223. goto backoff;
  1224. drm_atomic_state_free(state);
  1225. return ret;
  1226. backoff:
  1227. drm_atomic_legacy_backoff(state);
  1228. drm_atomic_state_clear(state);
  1229. goto retry;
  1230. }
  1231. EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
  1232. /**
  1233. * drm_atomic_helper_connector_set_property - helper for connector prorties
  1234. * @connector: DRM connector
  1235. * @property: DRM property
  1236. * @val: value of property
  1237. *
  1238. * Provides a default plane disablle handler using the atomic driver interface.
  1239. *
  1240. * RETURNS:
  1241. * Zero on success, error code on failure
  1242. */
  1243. int
  1244. drm_atomic_helper_connector_set_property(struct drm_connector *connector,
  1245. struct drm_property *property,
  1246. uint64_t val)
  1247. {
  1248. struct drm_atomic_state *state;
  1249. struct drm_connector_state *connector_state;
  1250. int ret = 0;
  1251. state = drm_atomic_state_alloc(connector->dev);
  1252. if (!state)
  1253. return -ENOMEM;
  1254. /* ->set_property is always called with all locks held. */
  1255. state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
  1256. retry:
  1257. connector_state = drm_atomic_get_connector_state(state, connector);
  1258. if (IS_ERR(connector_state)) {
  1259. ret = PTR_ERR(connector_state);
  1260. goto fail;
  1261. }
  1262. ret = connector->funcs->atomic_set_property(connector, connector_state,
  1263. property, val);
  1264. if (ret)
  1265. goto fail;
  1266. ret = drm_atomic_commit(state);
  1267. if (ret != 0)
  1268. goto fail;
  1269. /* Driver takes ownership of state on successful commit. */
  1270. return 0;
  1271. fail:
  1272. if (ret == -EDEADLK)
  1273. goto backoff;
  1274. drm_atomic_state_free(state);
  1275. return ret;
  1276. backoff:
  1277. drm_atomic_legacy_backoff(state);
  1278. drm_atomic_state_clear(state);
  1279. goto retry;
  1280. }
  1281. EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);