drm_atomic.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656
  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. /**
  31. * drm_atomic_state_default_release -
  32. * release memory initialized by drm_atomic_state_init
  33. * @state: atomic state
  34. *
  35. * Free all the memory allocated by drm_atomic_state_init.
  36. * This is useful for drivers that subclass the atomic state.
  37. */
  38. void drm_atomic_state_default_release(struct drm_atomic_state *state)
  39. {
  40. kfree(state->connectors);
  41. kfree(state->connector_states);
  42. kfree(state->crtcs);
  43. kfree(state->crtc_states);
  44. kfree(state->planes);
  45. kfree(state->plane_states);
  46. }
  47. EXPORT_SYMBOL(drm_atomic_state_default_release);
  48. /**
  49. * drm_atomic_state_init - init new atomic state
  50. * @dev: DRM device
  51. * @state: atomic state
  52. *
  53. * Default implementation for filling in a new atomic state.
  54. * This is useful for drivers that subclass the atomic state.
  55. */
  56. int
  57. drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
  58. {
  59. /* TODO legacy paths should maybe do a better job about
  60. * setting this appropriately?
  61. */
  62. state->allow_modeset = true;
  63. state->num_connector = ACCESS_ONCE(dev->mode_config.num_connector);
  64. state->crtcs = kcalloc(dev->mode_config.num_crtc,
  65. sizeof(*state->crtcs), GFP_KERNEL);
  66. if (!state->crtcs)
  67. goto fail;
  68. state->crtc_states = kcalloc(dev->mode_config.num_crtc,
  69. sizeof(*state->crtc_states), GFP_KERNEL);
  70. if (!state->crtc_states)
  71. goto fail;
  72. state->planes = kcalloc(dev->mode_config.num_total_plane,
  73. sizeof(*state->planes), GFP_KERNEL);
  74. if (!state->planes)
  75. goto fail;
  76. state->plane_states = kcalloc(dev->mode_config.num_total_plane,
  77. sizeof(*state->plane_states), GFP_KERNEL);
  78. if (!state->plane_states)
  79. goto fail;
  80. state->connectors = kcalloc(state->num_connector,
  81. sizeof(*state->connectors),
  82. GFP_KERNEL);
  83. if (!state->connectors)
  84. goto fail;
  85. state->connector_states = kcalloc(state->num_connector,
  86. sizeof(*state->connector_states),
  87. GFP_KERNEL);
  88. if (!state->connector_states)
  89. goto fail;
  90. state->dev = dev;
  91. DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
  92. return 0;
  93. fail:
  94. drm_atomic_state_default_release(state);
  95. return -ENOMEM;
  96. }
  97. EXPORT_SYMBOL(drm_atomic_state_init);
  98. /**
  99. * drm_atomic_state_alloc - allocate atomic state
  100. * @dev: DRM device
  101. *
  102. * This allocates an empty atomic state to track updates.
  103. */
  104. struct drm_atomic_state *
  105. drm_atomic_state_alloc(struct drm_device *dev)
  106. {
  107. struct drm_mode_config *config = &dev->mode_config;
  108. struct drm_atomic_state *state;
  109. if (!config->funcs->atomic_state_alloc) {
  110. state = kzalloc(sizeof(*state), GFP_KERNEL);
  111. if (!state)
  112. return NULL;
  113. if (drm_atomic_state_init(dev, state) < 0) {
  114. kfree(state);
  115. return NULL;
  116. }
  117. return state;
  118. }
  119. return config->funcs->atomic_state_alloc(dev);
  120. }
  121. EXPORT_SYMBOL(drm_atomic_state_alloc);
  122. /**
  123. * drm_atomic_state_default_clear - clear base atomic state
  124. * @state: atomic state
  125. *
  126. * Default implementation for clearing atomic state.
  127. * This is useful for drivers that subclass the atomic state.
  128. */
  129. void drm_atomic_state_default_clear(struct drm_atomic_state *state)
  130. {
  131. struct drm_device *dev = state->dev;
  132. struct drm_mode_config *config = &dev->mode_config;
  133. int i;
  134. DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
  135. for (i = 0; i < state->num_connector; i++) {
  136. struct drm_connector *connector = state->connectors[i];
  137. if (!connector || !connector->funcs)
  138. continue;
  139. /*
  140. * FIXME: Async commits can race with connector unplugging and
  141. * there's currently nothing that prevents cleanup up state for
  142. * deleted connectors. As long as the callback doesn't look at
  143. * the connector we'll be fine though, so make sure that's the
  144. * case by setting all connector pointers to NULL.
  145. */
  146. state->connector_states[i]->connector = NULL;
  147. connector->funcs->atomic_destroy_state(NULL,
  148. state->connector_states[i]);
  149. state->connectors[i] = NULL;
  150. state->connector_states[i] = NULL;
  151. }
  152. for (i = 0; i < config->num_crtc; i++) {
  153. struct drm_crtc *crtc = state->crtcs[i];
  154. if (!crtc)
  155. continue;
  156. crtc->funcs->atomic_destroy_state(crtc,
  157. state->crtc_states[i]);
  158. state->crtcs[i] = NULL;
  159. state->crtc_states[i] = NULL;
  160. }
  161. for (i = 0; i < config->num_total_plane; i++) {
  162. struct drm_plane *plane = state->planes[i];
  163. if (!plane)
  164. continue;
  165. plane->funcs->atomic_destroy_state(plane,
  166. state->plane_states[i]);
  167. state->planes[i] = NULL;
  168. state->plane_states[i] = NULL;
  169. }
  170. }
  171. EXPORT_SYMBOL(drm_atomic_state_default_clear);
  172. /**
  173. * drm_atomic_state_clear - clear state object
  174. * @state: atomic state
  175. *
  176. * When the w/w mutex algorithm detects a deadlock we need to back off and drop
  177. * all locks. So someone else could sneak in and change the current modeset
  178. * configuration. Which means that all the state assembled in @state is no
  179. * longer an atomic update to the current state, but to some arbitrary earlier
  180. * state. Which could break assumptions the driver's ->atomic_check likely
  181. * relies on.
  182. *
  183. * Hence we must clear all cached state and completely start over, using this
  184. * function.
  185. */
  186. void drm_atomic_state_clear(struct drm_atomic_state *state)
  187. {
  188. struct drm_device *dev = state->dev;
  189. struct drm_mode_config *config = &dev->mode_config;
  190. if (config->funcs->atomic_state_clear)
  191. config->funcs->atomic_state_clear(state);
  192. else
  193. drm_atomic_state_default_clear(state);
  194. }
  195. EXPORT_SYMBOL(drm_atomic_state_clear);
  196. /**
  197. * drm_atomic_state_free - free all memory for an atomic state
  198. * @state: atomic state to deallocate
  199. *
  200. * This frees all memory associated with an atomic state, including all the
  201. * per-object state for planes, crtcs and connectors.
  202. */
  203. void drm_atomic_state_free(struct drm_atomic_state *state)
  204. {
  205. struct drm_device *dev;
  206. struct drm_mode_config *config;
  207. if (!state)
  208. return;
  209. dev = state->dev;
  210. config = &dev->mode_config;
  211. drm_atomic_state_clear(state);
  212. DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
  213. if (config->funcs->atomic_state_free) {
  214. config->funcs->atomic_state_free(state);
  215. } else {
  216. drm_atomic_state_default_release(state);
  217. kfree(state);
  218. }
  219. }
  220. EXPORT_SYMBOL(drm_atomic_state_free);
  221. /**
  222. * drm_atomic_get_crtc_state - get crtc state
  223. * @state: global atomic state object
  224. * @crtc: crtc to get state object for
  225. *
  226. * This function returns the crtc state for the given crtc, allocating it if
  227. * needed. It will also grab the relevant crtc lock to make sure that the state
  228. * is consistent.
  229. *
  230. * Returns:
  231. *
  232. * Either the allocated state or the error code encoded into the pointer. When
  233. * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
  234. * entire atomic sequence must be restarted. All other errors are fatal.
  235. */
  236. struct drm_crtc_state *
  237. drm_atomic_get_crtc_state(struct drm_atomic_state *state,
  238. struct drm_crtc *crtc)
  239. {
  240. int ret, index = drm_crtc_index(crtc);
  241. struct drm_crtc_state *crtc_state;
  242. crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
  243. if (crtc_state)
  244. return crtc_state;
  245. ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
  246. if (ret)
  247. return ERR_PTR(ret);
  248. crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
  249. if (!crtc_state)
  250. return ERR_PTR(-ENOMEM);
  251. state->crtc_states[index] = crtc_state;
  252. state->crtcs[index] = crtc;
  253. crtc_state->state = state;
  254. DRM_DEBUG_ATOMIC("Added [CRTC:%d] %p state to %p\n",
  255. crtc->base.id, crtc_state, state);
  256. return crtc_state;
  257. }
  258. EXPORT_SYMBOL(drm_atomic_get_crtc_state);
  259. /**
  260. * drm_atomic_set_mode_for_crtc - set mode for CRTC
  261. * @state: the CRTC whose incoming state to update
  262. * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
  263. *
  264. * Set a mode (originating from the kernel) on the desired CRTC state. Does
  265. * not change any other state properties, including enable, active, or
  266. * mode_changed.
  267. *
  268. * RETURNS:
  269. * Zero on success, error code on failure. Cannot return -EDEADLK.
  270. */
  271. int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
  272. struct drm_display_mode *mode)
  273. {
  274. struct drm_mode_modeinfo umode;
  275. /* Early return for no change. */
  276. if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
  277. return 0;
  278. if (state->mode_blob)
  279. drm_property_unreference_blob(state->mode_blob);
  280. state->mode_blob = NULL;
  281. if (mode) {
  282. drm_mode_convert_to_umode(&umode, mode);
  283. state->mode_blob =
  284. drm_property_create_blob(state->crtc->dev,
  285. sizeof(umode),
  286. &umode);
  287. if (IS_ERR(state->mode_blob))
  288. return PTR_ERR(state->mode_blob);
  289. drm_mode_copy(&state->mode, mode);
  290. state->enable = true;
  291. DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
  292. mode->name, state);
  293. } else {
  294. memset(&state->mode, 0, sizeof(state->mode));
  295. state->enable = false;
  296. DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
  297. state);
  298. }
  299. return 0;
  300. }
  301. EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
  302. /**
  303. * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
  304. * @state: the CRTC whose incoming state to update
  305. * @blob: pointer to blob property to use for mode
  306. *
  307. * Set a mode (originating from a blob property) on the desired CRTC state.
  308. * This function will take a reference on the blob property for the CRTC state,
  309. * and release the reference held on the state's existing mode property, if any
  310. * was set.
  311. *
  312. * RETURNS:
  313. * Zero on success, error code on failure. Cannot return -EDEADLK.
  314. */
  315. int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
  316. struct drm_property_blob *blob)
  317. {
  318. if (blob == state->mode_blob)
  319. return 0;
  320. if (state->mode_blob)
  321. drm_property_unreference_blob(state->mode_blob);
  322. state->mode_blob = NULL;
  323. memset(&state->mode, 0, sizeof(state->mode));
  324. if (blob) {
  325. if (blob->length != sizeof(struct drm_mode_modeinfo) ||
  326. drm_mode_convert_umode(&state->mode,
  327. (const struct drm_mode_modeinfo *)
  328. blob->data))
  329. return -EINVAL;
  330. state->mode_blob = drm_property_reference_blob(blob);
  331. state->enable = true;
  332. DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
  333. state->mode.name, state);
  334. } else {
  335. state->enable = false;
  336. DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
  337. state);
  338. }
  339. return 0;
  340. }
  341. EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
  342. /**
  343. * drm_atomic_crtc_set_property - set property on CRTC
  344. * @crtc: the drm CRTC to set a property on
  345. * @state: the state object to update with the new property value
  346. * @property: the property to set
  347. * @val: the new property value
  348. *
  349. * Use this instead of calling crtc->atomic_set_property directly.
  350. * This function handles generic/core properties and calls out to
  351. * driver's ->atomic_set_property() for driver properties. To ensure
  352. * consistent behavior you must call this function rather than the
  353. * driver hook directly.
  354. *
  355. * RETURNS:
  356. * Zero on success, error code on failure
  357. */
  358. int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
  359. struct drm_crtc_state *state, struct drm_property *property,
  360. uint64_t val)
  361. {
  362. struct drm_device *dev = crtc->dev;
  363. struct drm_mode_config *config = &dev->mode_config;
  364. int ret;
  365. if (property == config->prop_active)
  366. state->active = val;
  367. else if (property == config->prop_mode_id) {
  368. struct drm_property_blob *mode =
  369. drm_property_lookup_blob(dev, val);
  370. ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
  371. if (mode)
  372. drm_property_unreference_blob(mode);
  373. return ret;
  374. }
  375. else if (crtc->funcs->atomic_set_property)
  376. return crtc->funcs->atomic_set_property(crtc, state, property, val);
  377. else
  378. return -EINVAL;
  379. return 0;
  380. }
  381. EXPORT_SYMBOL(drm_atomic_crtc_set_property);
  382. /*
  383. * This function handles generic/core properties and calls out to
  384. * driver's ->atomic_get_property() for driver properties. To ensure
  385. * consistent behavior you must call this function rather than the
  386. * driver hook directly.
  387. */
  388. static int
  389. drm_atomic_crtc_get_property(struct drm_crtc *crtc,
  390. const struct drm_crtc_state *state,
  391. struct drm_property *property, uint64_t *val)
  392. {
  393. struct drm_device *dev = crtc->dev;
  394. struct drm_mode_config *config = &dev->mode_config;
  395. if (property == config->prop_active)
  396. *val = state->active;
  397. else if (property == config->prop_mode_id)
  398. *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
  399. else if (crtc->funcs->atomic_get_property)
  400. return crtc->funcs->atomic_get_property(crtc, state, property, val);
  401. else
  402. return -EINVAL;
  403. return 0;
  404. }
  405. /**
  406. * drm_atomic_crtc_check - check crtc state
  407. * @crtc: crtc to check
  408. * @state: crtc state to check
  409. *
  410. * Provides core sanity checks for crtc state.
  411. *
  412. * RETURNS:
  413. * Zero on success, error code on failure
  414. */
  415. static int drm_atomic_crtc_check(struct drm_crtc *crtc,
  416. struct drm_crtc_state *state)
  417. {
  418. /* NOTE: we explicitly don't enforce constraints such as primary
  419. * layer covering entire screen, since that is something we want
  420. * to allow (on hw that supports it). For hw that does not, it
  421. * should be checked in driver's crtc->atomic_check() vfunc.
  422. *
  423. * TODO: Add generic modeset state checks once we support those.
  424. */
  425. if (state->active && !state->enable) {
  426. DRM_DEBUG_ATOMIC("[CRTC:%d] active without enabled\n",
  427. crtc->base.id);
  428. return -EINVAL;
  429. }
  430. /* The state->enable vs. state->mode_blob checks can be WARN_ON,
  431. * as this is a kernel-internal detail that userspace should never
  432. * be able to trigger. */
  433. if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
  434. WARN_ON(state->enable && !state->mode_blob)) {
  435. DRM_DEBUG_ATOMIC("[CRTC:%d] enabled without mode blob\n",
  436. crtc->base.id);
  437. return -EINVAL;
  438. }
  439. if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
  440. WARN_ON(!state->enable && state->mode_blob)) {
  441. DRM_DEBUG_ATOMIC("[CRTC:%d] disabled with mode blob\n",
  442. crtc->base.id);
  443. return -EINVAL;
  444. }
  445. return 0;
  446. }
  447. /**
  448. * drm_atomic_get_plane_state - get plane state
  449. * @state: global atomic state object
  450. * @plane: plane to get state object for
  451. *
  452. * This function returns the plane state for the given plane, allocating it if
  453. * needed. It will also grab the relevant plane lock to make sure that the state
  454. * is consistent.
  455. *
  456. * Returns:
  457. *
  458. * Either the allocated state or the error code encoded into the pointer. When
  459. * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
  460. * entire atomic sequence must be restarted. All other errors are fatal.
  461. */
  462. struct drm_plane_state *
  463. drm_atomic_get_plane_state(struct drm_atomic_state *state,
  464. struct drm_plane *plane)
  465. {
  466. int ret, index = drm_plane_index(plane);
  467. struct drm_plane_state *plane_state;
  468. plane_state = drm_atomic_get_existing_plane_state(state, plane);
  469. if (plane_state)
  470. return plane_state;
  471. ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
  472. if (ret)
  473. return ERR_PTR(ret);
  474. plane_state = plane->funcs->atomic_duplicate_state(plane);
  475. if (!plane_state)
  476. return ERR_PTR(-ENOMEM);
  477. state->plane_states[index] = plane_state;
  478. state->planes[index] = plane;
  479. plane_state->state = state;
  480. DRM_DEBUG_ATOMIC("Added [PLANE:%d] %p state to %p\n",
  481. plane->base.id, plane_state, state);
  482. if (plane_state->crtc) {
  483. struct drm_crtc_state *crtc_state;
  484. crtc_state = drm_atomic_get_crtc_state(state,
  485. plane_state->crtc);
  486. if (IS_ERR(crtc_state))
  487. return ERR_CAST(crtc_state);
  488. }
  489. return plane_state;
  490. }
  491. EXPORT_SYMBOL(drm_atomic_get_plane_state);
  492. /**
  493. * drm_atomic_plane_set_property - set property on plane
  494. * @plane: the drm plane to set a property on
  495. * @state: the state object to update with the new property value
  496. * @property: the property to set
  497. * @val: the new property value
  498. *
  499. * Use this instead of calling plane->atomic_set_property directly.
  500. * This function handles generic/core properties and calls out to
  501. * driver's ->atomic_set_property() for driver properties. To ensure
  502. * consistent behavior you must call this function rather than the
  503. * driver hook directly.
  504. *
  505. * RETURNS:
  506. * Zero on success, error code on failure
  507. */
  508. int drm_atomic_plane_set_property(struct drm_plane *plane,
  509. struct drm_plane_state *state, struct drm_property *property,
  510. uint64_t val)
  511. {
  512. struct drm_device *dev = plane->dev;
  513. struct drm_mode_config *config = &dev->mode_config;
  514. if (property == config->prop_fb_id) {
  515. struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
  516. drm_atomic_set_fb_for_plane(state, fb);
  517. if (fb)
  518. drm_framebuffer_unreference(fb);
  519. } else if (property == config->prop_crtc_id) {
  520. struct drm_crtc *crtc = drm_crtc_find(dev, val);
  521. return drm_atomic_set_crtc_for_plane(state, crtc);
  522. } else if (property == config->prop_crtc_x) {
  523. state->crtc_x = U642I64(val);
  524. } else if (property == config->prop_crtc_y) {
  525. state->crtc_y = U642I64(val);
  526. } else if (property == config->prop_crtc_w) {
  527. state->crtc_w = val;
  528. } else if (property == config->prop_crtc_h) {
  529. state->crtc_h = val;
  530. } else if (property == config->prop_src_x) {
  531. state->src_x = val;
  532. } else if (property == config->prop_src_y) {
  533. state->src_y = val;
  534. } else if (property == config->prop_src_w) {
  535. state->src_w = val;
  536. } else if (property == config->prop_src_h) {
  537. state->src_h = val;
  538. } else if (property == config->rotation_property) {
  539. state->rotation = val;
  540. } else if (plane->funcs->atomic_set_property) {
  541. return plane->funcs->atomic_set_property(plane, state,
  542. property, val);
  543. } else {
  544. return -EINVAL;
  545. }
  546. return 0;
  547. }
  548. EXPORT_SYMBOL(drm_atomic_plane_set_property);
  549. /*
  550. * This function handles generic/core properties and calls out to
  551. * driver's ->atomic_get_property() for driver properties. To ensure
  552. * consistent behavior you must call this function rather than the
  553. * driver hook directly.
  554. */
  555. static int
  556. drm_atomic_plane_get_property(struct drm_plane *plane,
  557. const struct drm_plane_state *state,
  558. struct drm_property *property, uint64_t *val)
  559. {
  560. struct drm_device *dev = plane->dev;
  561. struct drm_mode_config *config = &dev->mode_config;
  562. if (property == config->prop_fb_id) {
  563. *val = (state->fb) ? state->fb->base.id : 0;
  564. } else if (property == config->prop_crtc_id) {
  565. *val = (state->crtc) ? state->crtc->base.id : 0;
  566. } else if (property == config->prop_crtc_x) {
  567. *val = I642U64(state->crtc_x);
  568. } else if (property == config->prop_crtc_y) {
  569. *val = I642U64(state->crtc_y);
  570. } else if (property == config->prop_crtc_w) {
  571. *val = state->crtc_w;
  572. } else if (property == config->prop_crtc_h) {
  573. *val = state->crtc_h;
  574. } else if (property == config->prop_src_x) {
  575. *val = state->src_x;
  576. } else if (property == config->prop_src_y) {
  577. *val = state->src_y;
  578. } else if (property == config->prop_src_w) {
  579. *val = state->src_w;
  580. } else if (property == config->prop_src_h) {
  581. *val = state->src_h;
  582. } else if (property == config->rotation_property) {
  583. *val = state->rotation;
  584. } else if (plane->funcs->atomic_get_property) {
  585. return plane->funcs->atomic_get_property(plane, state, property, val);
  586. } else {
  587. return -EINVAL;
  588. }
  589. return 0;
  590. }
  591. static bool
  592. plane_switching_crtc(struct drm_atomic_state *state,
  593. struct drm_plane *plane,
  594. struct drm_plane_state *plane_state)
  595. {
  596. if (!plane->state->crtc || !plane_state->crtc)
  597. return false;
  598. if (plane->state->crtc == plane_state->crtc)
  599. return false;
  600. /* This could be refined, but currently there's no helper or driver code
  601. * to implement direct switching of active planes nor userspace to take
  602. * advantage of more direct plane switching without the intermediate
  603. * full OFF state.
  604. */
  605. return true;
  606. }
  607. /**
  608. * drm_atomic_plane_check - check plane state
  609. * @plane: plane to check
  610. * @state: plane state to check
  611. *
  612. * Provides core sanity checks for plane state.
  613. *
  614. * RETURNS:
  615. * Zero on success, error code on failure
  616. */
  617. static int drm_atomic_plane_check(struct drm_plane *plane,
  618. struct drm_plane_state *state)
  619. {
  620. unsigned int fb_width, fb_height;
  621. int ret;
  622. /* either *both* CRTC and FB must be set, or neither */
  623. if (WARN_ON(state->crtc && !state->fb)) {
  624. DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
  625. return -EINVAL;
  626. } else if (WARN_ON(state->fb && !state->crtc)) {
  627. DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
  628. return -EINVAL;
  629. }
  630. /* if disabled, we don't care about the rest of the state: */
  631. if (!state->crtc)
  632. return 0;
  633. /* Check whether this plane is usable on this CRTC */
  634. if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
  635. DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
  636. return -EINVAL;
  637. }
  638. /* Check whether this plane supports the fb pixel format. */
  639. ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format);
  640. if (ret) {
  641. DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
  642. drm_get_format_name(state->fb->pixel_format));
  643. return ret;
  644. }
  645. /* Give drivers some help against integer overflows */
  646. if (state->crtc_w > INT_MAX ||
  647. state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
  648. state->crtc_h > INT_MAX ||
  649. state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
  650. DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
  651. state->crtc_w, state->crtc_h,
  652. state->crtc_x, state->crtc_y);
  653. return -ERANGE;
  654. }
  655. fb_width = state->fb->width << 16;
  656. fb_height = state->fb->height << 16;
  657. /* Make sure source coordinates are inside the fb. */
  658. if (state->src_w > fb_width ||
  659. state->src_x > fb_width - state->src_w ||
  660. state->src_h > fb_height ||
  661. state->src_y > fb_height - state->src_h) {
  662. DRM_DEBUG_ATOMIC("Invalid source coordinates "
  663. "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
  664. state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
  665. state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
  666. state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
  667. state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
  668. return -ENOSPC;
  669. }
  670. if (plane_switching_crtc(state->state, plane, state)) {
  671. DRM_DEBUG_ATOMIC("[PLANE:%d] switching CRTC directly\n",
  672. plane->base.id);
  673. return -EINVAL;
  674. }
  675. return 0;
  676. }
  677. /**
  678. * drm_atomic_get_connector_state - get connector state
  679. * @state: global atomic state object
  680. * @connector: connector to get state object for
  681. *
  682. * This function returns the connector state for the given connector,
  683. * allocating it if needed. It will also grab the relevant connector lock to
  684. * make sure that the state is consistent.
  685. *
  686. * Returns:
  687. *
  688. * Either the allocated state or the error code encoded into the pointer. When
  689. * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
  690. * entire atomic sequence must be restarted. All other errors are fatal.
  691. */
  692. struct drm_connector_state *
  693. drm_atomic_get_connector_state(struct drm_atomic_state *state,
  694. struct drm_connector *connector)
  695. {
  696. int ret, index;
  697. struct drm_mode_config *config = &connector->dev->mode_config;
  698. struct drm_connector_state *connector_state;
  699. ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
  700. if (ret)
  701. return ERR_PTR(ret);
  702. index = drm_connector_index(connector);
  703. /*
  704. * Construction of atomic state updates can race with a connector
  705. * hot-add which might overflow. In this case flip the table and just
  706. * restart the entire ioctl - no one is fast enough to livelock a cpu
  707. * with physical hotplug events anyway.
  708. *
  709. * Note that we only grab the indexes once we have the right lock to
  710. * prevent hotplug/unplugging of connectors. So removal is no problem,
  711. * at most the array is a bit too large.
  712. */
  713. if (index >= state->num_connector) {
  714. DRM_DEBUG_ATOMIC("Hot-added connector would overflow state array, restarting\n");
  715. return ERR_PTR(-EAGAIN);
  716. }
  717. if (state->connector_states[index])
  718. return state->connector_states[index];
  719. connector_state = connector->funcs->atomic_duplicate_state(connector);
  720. if (!connector_state)
  721. return ERR_PTR(-ENOMEM);
  722. state->connector_states[index] = connector_state;
  723. state->connectors[index] = connector;
  724. connector_state->state = state;
  725. DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
  726. connector->base.id, connector_state, state);
  727. if (connector_state->crtc) {
  728. struct drm_crtc_state *crtc_state;
  729. crtc_state = drm_atomic_get_crtc_state(state,
  730. connector_state->crtc);
  731. if (IS_ERR(crtc_state))
  732. return ERR_CAST(crtc_state);
  733. }
  734. return connector_state;
  735. }
  736. EXPORT_SYMBOL(drm_atomic_get_connector_state);
  737. /**
  738. * drm_atomic_connector_set_property - set property on connector.
  739. * @connector: the drm connector to set a property on
  740. * @state: the state object to update with the new property value
  741. * @property: the property to set
  742. * @val: the new property value
  743. *
  744. * Use this instead of calling connector->atomic_set_property directly.
  745. * This function handles generic/core properties and calls out to
  746. * driver's ->atomic_set_property() for driver properties. To ensure
  747. * consistent behavior you must call this function rather than the
  748. * driver hook directly.
  749. *
  750. * RETURNS:
  751. * Zero on success, error code on failure
  752. */
  753. int drm_atomic_connector_set_property(struct drm_connector *connector,
  754. struct drm_connector_state *state, struct drm_property *property,
  755. uint64_t val)
  756. {
  757. struct drm_device *dev = connector->dev;
  758. struct drm_mode_config *config = &dev->mode_config;
  759. if (property == config->prop_crtc_id) {
  760. struct drm_crtc *crtc = drm_crtc_find(dev, val);
  761. return drm_atomic_set_crtc_for_connector(state, crtc);
  762. } else if (property == config->dpms_property) {
  763. /* setting DPMS property requires special handling, which
  764. * is done in legacy setprop path for us. Disallow (for
  765. * now?) atomic writes to DPMS property:
  766. */
  767. return -EINVAL;
  768. } else if (connector->funcs->atomic_set_property) {
  769. return connector->funcs->atomic_set_property(connector,
  770. state, property, val);
  771. } else {
  772. return -EINVAL;
  773. }
  774. }
  775. EXPORT_SYMBOL(drm_atomic_connector_set_property);
  776. /*
  777. * This function handles generic/core properties and calls out to
  778. * driver's ->atomic_get_property() for driver properties. To ensure
  779. * consistent behavior you must call this function rather than the
  780. * driver hook directly.
  781. */
  782. static int
  783. drm_atomic_connector_get_property(struct drm_connector *connector,
  784. const struct drm_connector_state *state,
  785. struct drm_property *property, uint64_t *val)
  786. {
  787. struct drm_device *dev = connector->dev;
  788. struct drm_mode_config *config = &dev->mode_config;
  789. if (property == config->prop_crtc_id) {
  790. *val = (state->crtc) ? state->crtc->base.id : 0;
  791. } else if (property == config->dpms_property) {
  792. *val = connector->dpms;
  793. } else if (connector->funcs->atomic_get_property) {
  794. return connector->funcs->atomic_get_property(connector,
  795. state, property, val);
  796. } else {
  797. return -EINVAL;
  798. }
  799. return 0;
  800. }
  801. int drm_atomic_get_property(struct drm_mode_object *obj,
  802. struct drm_property *property, uint64_t *val)
  803. {
  804. struct drm_device *dev = property->dev;
  805. int ret;
  806. switch (obj->type) {
  807. case DRM_MODE_OBJECT_CONNECTOR: {
  808. struct drm_connector *connector = obj_to_connector(obj);
  809. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  810. ret = drm_atomic_connector_get_property(connector,
  811. connector->state, property, val);
  812. break;
  813. }
  814. case DRM_MODE_OBJECT_CRTC: {
  815. struct drm_crtc *crtc = obj_to_crtc(obj);
  816. WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
  817. ret = drm_atomic_crtc_get_property(crtc,
  818. crtc->state, property, val);
  819. break;
  820. }
  821. case DRM_MODE_OBJECT_PLANE: {
  822. struct drm_plane *plane = obj_to_plane(obj);
  823. WARN_ON(!drm_modeset_is_locked(&plane->mutex));
  824. ret = drm_atomic_plane_get_property(plane,
  825. plane->state, property, val);
  826. break;
  827. }
  828. default:
  829. ret = -EINVAL;
  830. break;
  831. }
  832. return ret;
  833. }
  834. /**
  835. * drm_atomic_set_crtc_for_plane - set crtc for plane
  836. * @plane_state: the plane whose incoming state to update
  837. * @crtc: crtc to use for the plane
  838. *
  839. * Changing the assigned crtc for a plane requires us to grab the lock and state
  840. * for the new crtc, as needed. This function takes care of all these details
  841. * besides updating the pointer in the state object itself.
  842. *
  843. * Returns:
  844. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  845. * then the w/w mutex code has detected a deadlock and the entire atomic
  846. * sequence must be restarted. All other errors are fatal.
  847. */
  848. int
  849. drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
  850. struct drm_crtc *crtc)
  851. {
  852. struct drm_plane *plane = plane_state->plane;
  853. struct drm_crtc_state *crtc_state;
  854. /* Nothing to do for same crtc*/
  855. if (plane_state->crtc == crtc)
  856. return 0;
  857. if (plane_state->crtc) {
  858. crtc_state = drm_atomic_get_crtc_state(plane_state->state,
  859. plane_state->crtc);
  860. if (WARN_ON(IS_ERR(crtc_state)))
  861. return PTR_ERR(crtc_state);
  862. crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
  863. }
  864. plane_state->crtc = crtc;
  865. if (crtc) {
  866. crtc_state = drm_atomic_get_crtc_state(plane_state->state,
  867. crtc);
  868. if (IS_ERR(crtc_state))
  869. return PTR_ERR(crtc_state);
  870. crtc_state->plane_mask |= (1 << drm_plane_index(plane));
  871. }
  872. if (crtc)
  873. DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d]\n",
  874. plane_state, crtc->base.id);
  875. else
  876. DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
  877. plane_state);
  878. return 0;
  879. }
  880. EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
  881. /**
  882. * drm_atomic_set_fb_for_plane - set framebuffer for plane
  883. * @plane_state: atomic state object for the plane
  884. * @fb: fb to use for the plane
  885. *
  886. * Changing the assigned framebuffer for a plane requires us to grab a reference
  887. * to the new fb and drop the reference to the old fb, if there is one. This
  888. * function takes care of all these details besides updating the pointer in the
  889. * state object itself.
  890. */
  891. void
  892. drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
  893. struct drm_framebuffer *fb)
  894. {
  895. if (plane_state->fb)
  896. drm_framebuffer_unreference(plane_state->fb);
  897. if (fb)
  898. drm_framebuffer_reference(fb);
  899. plane_state->fb = fb;
  900. if (fb)
  901. DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
  902. fb->base.id, plane_state);
  903. else
  904. DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
  905. plane_state);
  906. }
  907. EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
  908. /**
  909. * drm_atomic_set_crtc_for_connector - set crtc for connector
  910. * @conn_state: atomic state object for the connector
  911. * @crtc: crtc to use for the connector
  912. *
  913. * Changing the assigned crtc for a connector requires us to grab the lock and
  914. * state for the new crtc, as needed. This function takes care of all these
  915. * details besides updating the pointer in the state object itself.
  916. *
  917. * Returns:
  918. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  919. * then the w/w mutex code has detected a deadlock and the entire atomic
  920. * sequence must be restarted. All other errors are fatal.
  921. */
  922. int
  923. drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
  924. struct drm_crtc *crtc)
  925. {
  926. struct drm_crtc_state *crtc_state;
  927. if (crtc) {
  928. crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
  929. if (IS_ERR(crtc_state))
  930. return PTR_ERR(crtc_state);
  931. }
  932. conn_state->crtc = crtc;
  933. if (crtc)
  934. DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d]\n",
  935. conn_state, crtc->base.id);
  936. else
  937. DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
  938. conn_state);
  939. return 0;
  940. }
  941. EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
  942. /**
  943. * drm_atomic_add_affected_connectors - add connectors for crtc
  944. * @state: atomic state
  945. * @crtc: DRM crtc
  946. *
  947. * This function walks the current configuration and adds all connectors
  948. * currently using @crtc to the atomic configuration @state. Note that this
  949. * function must acquire the connection mutex. This can potentially cause
  950. * unneeded seralization if the update is just for the planes on one crtc. Hence
  951. * drivers and helpers should only call this when really needed (e.g. when a
  952. * full modeset needs to happen due to some change).
  953. *
  954. * Returns:
  955. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  956. * then the w/w mutex code has detected a deadlock and the entire atomic
  957. * sequence must be restarted. All other errors are fatal.
  958. */
  959. int
  960. drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
  961. struct drm_crtc *crtc)
  962. {
  963. struct drm_mode_config *config = &state->dev->mode_config;
  964. struct drm_connector *connector;
  965. struct drm_connector_state *conn_state;
  966. int ret;
  967. ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
  968. if (ret)
  969. return ret;
  970. DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d] to %p\n",
  971. crtc->base.id, state);
  972. /*
  973. * Changed connectors are already in @state, so only need to look at the
  974. * current configuration.
  975. */
  976. drm_for_each_connector(connector, state->dev) {
  977. if (connector->state->crtc != crtc)
  978. continue;
  979. conn_state = drm_atomic_get_connector_state(state, connector);
  980. if (IS_ERR(conn_state))
  981. return PTR_ERR(conn_state);
  982. }
  983. return 0;
  984. }
  985. EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
  986. /**
  987. * drm_atomic_add_affected_planes - add planes for crtc
  988. * @state: atomic state
  989. * @crtc: DRM crtc
  990. *
  991. * This function walks the current configuration and adds all planes
  992. * currently used by @crtc to the atomic configuration @state. This is useful
  993. * when an atomic commit also needs to check all currently enabled plane on
  994. * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
  995. * to avoid special code to force-enable all planes.
  996. *
  997. * Since acquiring a plane state will always also acquire the w/w mutex of the
  998. * current CRTC for that plane (if there is any) adding all the plane states for
  999. * a CRTC will not reduce parallism of atomic updates.
  1000. *
  1001. * Returns:
  1002. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  1003. * then the w/w mutex code has detected a deadlock and the entire atomic
  1004. * sequence must be restarted. All other errors are fatal.
  1005. */
  1006. int
  1007. drm_atomic_add_affected_planes(struct drm_atomic_state *state,
  1008. struct drm_crtc *crtc)
  1009. {
  1010. struct drm_plane *plane;
  1011. WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
  1012. drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
  1013. struct drm_plane_state *plane_state =
  1014. drm_atomic_get_plane_state(state, plane);
  1015. if (IS_ERR(plane_state))
  1016. return PTR_ERR(plane_state);
  1017. }
  1018. return 0;
  1019. }
  1020. EXPORT_SYMBOL(drm_atomic_add_affected_planes);
  1021. /**
  1022. * drm_atomic_connectors_for_crtc - count number of connected outputs
  1023. * @state: atomic state
  1024. * @crtc: DRM crtc
  1025. *
  1026. * This function counts all connectors which will be connected to @crtc
  1027. * according to @state. Useful to recompute the enable state for @crtc.
  1028. */
  1029. int
  1030. drm_atomic_connectors_for_crtc(struct drm_atomic_state *state,
  1031. struct drm_crtc *crtc)
  1032. {
  1033. struct drm_connector *connector;
  1034. struct drm_connector_state *conn_state;
  1035. int i, num_connected_connectors = 0;
  1036. for_each_connector_in_state(state, connector, conn_state, i) {
  1037. if (conn_state->crtc == crtc)
  1038. num_connected_connectors++;
  1039. }
  1040. DRM_DEBUG_ATOMIC("State %p has %i connectors for [CRTC:%d]\n",
  1041. state, num_connected_connectors, crtc->base.id);
  1042. return num_connected_connectors;
  1043. }
  1044. EXPORT_SYMBOL(drm_atomic_connectors_for_crtc);
  1045. /**
  1046. * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
  1047. * @state: atomic state
  1048. *
  1049. * This function should be used by legacy entry points which don't understand
  1050. * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
  1051. * the slowpath completed.
  1052. */
  1053. void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
  1054. {
  1055. int ret;
  1056. retry:
  1057. drm_modeset_backoff(state->acquire_ctx);
  1058. ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
  1059. state->acquire_ctx);
  1060. if (ret)
  1061. goto retry;
  1062. ret = drm_modeset_lock_all_crtcs(state->dev,
  1063. state->acquire_ctx);
  1064. if (ret)
  1065. goto retry;
  1066. }
  1067. EXPORT_SYMBOL(drm_atomic_legacy_backoff);
  1068. /**
  1069. * drm_atomic_check_only - check whether a given config would work
  1070. * @state: atomic configuration to check
  1071. *
  1072. * Note that this function can return -EDEADLK if the driver needed to acquire
  1073. * more locks but encountered a deadlock. The caller must then do the usual w/w
  1074. * backoff dance and restart. All other errors are fatal.
  1075. *
  1076. * Returns:
  1077. * 0 on success, negative error code on failure.
  1078. */
  1079. int drm_atomic_check_only(struct drm_atomic_state *state)
  1080. {
  1081. struct drm_device *dev = state->dev;
  1082. struct drm_mode_config *config = &dev->mode_config;
  1083. struct drm_plane *plane;
  1084. struct drm_plane_state *plane_state;
  1085. struct drm_crtc *crtc;
  1086. struct drm_crtc_state *crtc_state;
  1087. int i, ret = 0;
  1088. DRM_DEBUG_ATOMIC("checking %p\n", state);
  1089. for_each_plane_in_state(state, plane, plane_state, i) {
  1090. ret = drm_atomic_plane_check(plane, plane_state);
  1091. if (ret) {
  1092. DRM_DEBUG_ATOMIC("[PLANE:%d] atomic core check failed\n",
  1093. plane->base.id);
  1094. return ret;
  1095. }
  1096. }
  1097. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  1098. ret = drm_atomic_crtc_check(crtc, crtc_state);
  1099. if (ret) {
  1100. DRM_DEBUG_ATOMIC("[CRTC:%d] atomic core check failed\n",
  1101. crtc->base.id);
  1102. return ret;
  1103. }
  1104. }
  1105. if (config->funcs->atomic_check)
  1106. ret = config->funcs->atomic_check(state->dev, state);
  1107. if (ret)
  1108. return ret;
  1109. if (!state->allow_modeset) {
  1110. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  1111. if (drm_atomic_crtc_needs_modeset(crtc_state)) {
  1112. DRM_DEBUG_ATOMIC("[CRTC:%d] requires full modeset\n",
  1113. crtc->base.id);
  1114. return -EINVAL;
  1115. }
  1116. }
  1117. }
  1118. return 0;
  1119. }
  1120. EXPORT_SYMBOL(drm_atomic_check_only);
  1121. /**
  1122. * drm_atomic_commit - commit configuration atomically
  1123. * @state: atomic configuration to check
  1124. *
  1125. * Note that this function can return -EDEADLK if the driver needed to acquire
  1126. * more locks but encountered a deadlock. The caller must then do the usual w/w
  1127. * backoff dance and restart. All other errors are fatal.
  1128. *
  1129. * Also note that on successful execution ownership of @state is transferred
  1130. * from the caller of this function to the function itself. The caller must not
  1131. * free or in any other way access @state. If the function fails then the caller
  1132. * must clean up @state itself.
  1133. *
  1134. * Returns:
  1135. * 0 on success, negative error code on failure.
  1136. */
  1137. int drm_atomic_commit(struct drm_atomic_state *state)
  1138. {
  1139. struct drm_mode_config *config = &state->dev->mode_config;
  1140. int ret;
  1141. ret = drm_atomic_check_only(state);
  1142. if (ret)
  1143. return ret;
  1144. DRM_DEBUG_ATOMIC("commiting %p\n", state);
  1145. return config->funcs->atomic_commit(state->dev, state, false);
  1146. }
  1147. EXPORT_SYMBOL(drm_atomic_commit);
  1148. /**
  1149. * drm_atomic_async_commit - atomic&async configuration commit
  1150. * @state: atomic configuration to check
  1151. *
  1152. * Note that this function can return -EDEADLK if the driver needed to acquire
  1153. * more locks but encountered a deadlock. The caller must then do the usual w/w
  1154. * backoff dance and restart. All other errors are fatal.
  1155. *
  1156. * Also note that on successful execution ownership of @state is transferred
  1157. * from the caller of this function to the function itself. The caller must not
  1158. * free or in any other way access @state. If the function fails then the caller
  1159. * must clean up @state itself.
  1160. *
  1161. * Returns:
  1162. * 0 on success, negative error code on failure.
  1163. */
  1164. int drm_atomic_async_commit(struct drm_atomic_state *state)
  1165. {
  1166. struct drm_mode_config *config = &state->dev->mode_config;
  1167. int ret;
  1168. ret = drm_atomic_check_only(state);
  1169. if (ret)
  1170. return ret;
  1171. DRM_DEBUG_ATOMIC("commiting %p asynchronously\n", state);
  1172. return config->funcs->atomic_commit(state->dev, state, true);
  1173. }
  1174. EXPORT_SYMBOL(drm_atomic_async_commit);
  1175. /*
  1176. * The big monstor ioctl
  1177. */
  1178. static struct drm_pending_vblank_event *create_vblank_event(
  1179. struct drm_device *dev, struct drm_file *file_priv, uint64_t user_data)
  1180. {
  1181. struct drm_pending_vblank_event *e = NULL;
  1182. unsigned long flags;
  1183. spin_lock_irqsave(&dev->event_lock, flags);
  1184. if (file_priv->event_space < sizeof e->event) {
  1185. spin_unlock_irqrestore(&dev->event_lock, flags);
  1186. goto out;
  1187. }
  1188. file_priv->event_space -= sizeof e->event;
  1189. spin_unlock_irqrestore(&dev->event_lock, flags);
  1190. e = kzalloc(sizeof *e, GFP_KERNEL);
  1191. if (e == NULL) {
  1192. spin_lock_irqsave(&dev->event_lock, flags);
  1193. file_priv->event_space += sizeof e->event;
  1194. spin_unlock_irqrestore(&dev->event_lock, flags);
  1195. goto out;
  1196. }
  1197. e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
  1198. e->event.base.length = sizeof e->event;
  1199. e->event.user_data = user_data;
  1200. e->base.event = &e->event.base;
  1201. e->base.file_priv = file_priv;
  1202. e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
  1203. out:
  1204. return e;
  1205. }
  1206. static void destroy_vblank_event(struct drm_device *dev,
  1207. struct drm_file *file_priv, struct drm_pending_vblank_event *e)
  1208. {
  1209. unsigned long flags;
  1210. spin_lock_irqsave(&dev->event_lock, flags);
  1211. file_priv->event_space += sizeof e->event;
  1212. spin_unlock_irqrestore(&dev->event_lock, flags);
  1213. kfree(e);
  1214. }
  1215. static int atomic_set_prop(struct drm_atomic_state *state,
  1216. struct drm_mode_object *obj, struct drm_property *prop,
  1217. uint64_t prop_value)
  1218. {
  1219. struct drm_mode_object *ref;
  1220. int ret;
  1221. if (!drm_property_change_valid_get(prop, prop_value, &ref))
  1222. return -EINVAL;
  1223. switch (obj->type) {
  1224. case DRM_MODE_OBJECT_CONNECTOR: {
  1225. struct drm_connector *connector = obj_to_connector(obj);
  1226. struct drm_connector_state *connector_state;
  1227. connector_state = drm_atomic_get_connector_state(state, connector);
  1228. if (IS_ERR(connector_state)) {
  1229. ret = PTR_ERR(connector_state);
  1230. break;
  1231. }
  1232. ret = drm_atomic_connector_set_property(connector,
  1233. connector_state, prop, prop_value);
  1234. break;
  1235. }
  1236. case DRM_MODE_OBJECT_CRTC: {
  1237. struct drm_crtc *crtc = obj_to_crtc(obj);
  1238. struct drm_crtc_state *crtc_state;
  1239. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  1240. if (IS_ERR(crtc_state)) {
  1241. ret = PTR_ERR(crtc_state);
  1242. break;
  1243. }
  1244. ret = drm_atomic_crtc_set_property(crtc,
  1245. crtc_state, prop, prop_value);
  1246. break;
  1247. }
  1248. case DRM_MODE_OBJECT_PLANE: {
  1249. struct drm_plane *plane = obj_to_plane(obj);
  1250. struct drm_plane_state *plane_state;
  1251. plane_state = drm_atomic_get_plane_state(state, plane);
  1252. if (IS_ERR(plane_state)) {
  1253. ret = PTR_ERR(plane_state);
  1254. break;
  1255. }
  1256. ret = drm_atomic_plane_set_property(plane,
  1257. plane_state, prop, prop_value);
  1258. break;
  1259. }
  1260. default:
  1261. ret = -EINVAL;
  1262. break;
  1263. }
  1264. drm_property_change_valid_put(prop, ref);
  1265. return ret;
  1266. }
  1267. /**
  1268. * drm_atomic_update_old_fb -- Unset old_fb pointers and set plane->fb pointers.
  1269. *
  1270. * @dev: drm device to check.
  1271. * @plane_mask: plane mask for planes that were updated.
  1272. * @ret: return value, can be -EDEADLK for a retry.
  1273. *
  1274. * Before doing an update plane->old_fb is set to plane->fb,
  1275. * but before dropping the locks old_fb needs to be set to NULL
  1276. * and plane->fb updated. This is a common operation for each
  1277. * atomic update, so this call is split off as a helper.
  1278. */
  1279. void drm_atomic_clean_old_fb(struct drm_device *dev,
  1280. unsigned plane_mask,
  1281. int ret)
  1282. {
  1283. struct drm_plane *plane;
  1284. /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
  1285. * locks (ie. while it is still safe to deref plane->state). We
  1286. * need to do this here because the driver entry points cannot
  1287. * distinguish between legacy and atomic ioctls.
  1288. */
  1289. drm_for_each_plane_mask(plane, dev, plane_mask) {
  1290. if (ret == 0) {
  1291. struct drm_framebuffer *new_fb = plane->state->fb;
  1292. if (new_fb)
  1293. drm_framebuffer_reference(new_fb);
  1294. plane->fb = new_fb;
  1295. plane->crtc = plane->state->crtc;
  1296. if (plane->old_fb)
  1297. drm_framebuffer_unreference(plane->old_fb);
  1298. }
  1299. plane->old_fb = NULL;
  1300. }
  1301. }
  1302. EXPORT_SYMBOL(drm_atomic_clean_old_fb);
  1303. int drm_mode_atomic_ioctl(struct drm_device *dev,
  1304. void *data, struct drm_file *file_priv)
  1305. {
  1306. struct drm_mode_atomic *arg = data;
  1307. uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
  1308. uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
  1309. uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
  1310. uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
  1311. unsigned int copied_objs, copied_props;
  1312. struct drm_atomic_state *state;
  1313. struct drm_modeset_acquire_ctx ctx;
  1314. struct drm_plane *plane;
  1315. struct drm_crtc *crtc;
  1316. struct drm_crtc_state *crtc_state;
  1317. unsigned plane_mask;
  1318. int ret = 0;
  1319. unsigned int i, j;
  1320. /* disallow for drivers not supporting atomic: */
  1321. if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
  1322. return -EINVAL;
  1323. /* disallow for userspace that has not enabled atomic cap (even
  1324. * though this may be a bit overkill, since legacy userspace
  1325. * wouldn't know how to call this ioctl)
  1326. */
  1327. if (!file_priv->atomic)
  1328. return -EINVAL;
  1329. if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
  1330. return -EINVAL;
  1331. if (arg->reserved)
  1332. return -EINVAL;
  1333. if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
  1334. !dev->mode_config.async_page_flip)
  1335. return -EINVAL;
  1336. /* can't test and expect an event at the same time. */
  1337. if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
  1338. (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
  1339. return -EINVAL;
  1340. drm_modeset_acquire_init(&ctx, 0);
  1341. state = drm_atomic_state_alloc(dev);
  1342. if (!state)
  1343. return -ENOMEM;
  1344. state->acquire_ctx = &ctx;
  1345. state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
  1346. retry:
  1347. plane_mask = 0;
  1348. copied_objs = 0;
  1349. copied_props = 0;
  1350. for (i = 0; i < arg->count_objs; i++) {
  1351. uint32_t obj_id, count_props;
  1352. struct drm_mode_object *obj;
  1353. if (get_user(obj_id, objs_ptr + copied_objs)) {
  1354. ret = -EFAULT;
  1355. goto out;
  1356. }
  1357. obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
  1358. if (!obj || !obj->properties) {
  1359. ret = -ENOENT;
  1360. goto out;
  1361. }
  1362. if (get_user(count_props, count_props_ptr + copied_objs)) {
  1363. ret = -EFAULT;
  1364. goto out;
  1365. }
  1366. copied_objs++;
  1367. for (j = 0; j < count_props; j++) {
  1368. uint32_t prop_id;
  1369. uint64_t prop_value;
  1370. struct drm_property *prop;
  1371. if (get_user(prop_id, props_ptr + copied_props)) {
  1372. ret = -EFAULT;
  1373. goto out;
  1374. }
  1375. prop = drm_property_find(dev, prop_id);
  1376. if (!prop) {
  1377. ret = -ENOENT;
  1378. goto out;
  1379. }
  1380. if (copy_from_user(&prop_value,
  1381. prop_values_ptr + copied_props,
  1382. sizeof(prop_value))) {
  1383. ret = -EFAULT;
  1384. goto out;
  1385. }
  1386. ret = atomic_set_prop(state, obj, prop, prop_value);
  1387. if (ret)
  1388. goto out;
  1389. copied_props++;
  1390. }
  1391. if (obj->type == DRM_MODE_OBJECT_PLANE && count_props &&
  1392. !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
  1393. plane = obj_to_plane(obj);
  1394. plane_mask |= (1 << drm_plane_index(plane));
  1395. plane->old_fb = plane->fb;
  1396. }
  1397. }
  1398. if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  1399. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  1400. struct drm_pending_vblank_event *e;
  1401. e = create_vblank_event(dev, file_priv, arg->user_data);
  1402. if (!e) {
  1403. ret = -ENOMEM;
  1404. goto out;
  1405. }
  1406. crtc_state->event = e;
  1407. }
  1408. }
  1409. if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
  1410. /*
  1411. * Unlike commit, check_only does not clean up state.
  1412. * Below we call drm_atomic_state_free for it.
  1413. */
  1414. ret = drm_atomic_check_only(state);
  1415. } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
  1416. ret = drm_atomic_async_commit(state);
  1417. } else {
  1418. ret = drm_atomic_commit(state);
  1419. }
  1420. out:
  1421. drm_atomic_clean_old_fb(dev, plane_mask, ret);
  1422. if (ret && arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  1423. /*
  1424. * TEST_ONLY and PAGE_FLIP_EVENT are mutually exclusive,
  1425. * if they weren't, this code should be called on success
  1426. * for TEST_ONLY too.
  1427. */
  1428. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  1429. if (!crtc_state->event)
  1430. continue;
  1431. destroy_vblank_event(dev, file_priv,
  1432. crtc_state->event);
  1433. }
  1434. }
  1435. if (ret == -EDEADLK) {
  1436. drm_atomic_state_clear(state);
  1437. drm_modeset_backoff(&ctx);
  1438. goto retry;
  1439. }
  1440. if (ret || arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
  1441. drm_atomic_state_free(state);
  1442. drm_modeset_drop_locks(&ctx);
  1443. drm_modeset_acquire_fini(&ctx);
  1444. return ret;
  1445. }