drm_atomic.c 48 KB

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