drm_atomic.c 47 KB

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