drm_atomic.c 51 KB

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