drm_atomic.c 50 KB

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