drm_atomic.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224
  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/drm_print.h>
  32. #include <linux/sync_file.h>
  33. #include "drm_crtc_internal.h"
  34. void __drm_crtc_commit_free(struct kref *kref)
  35. {
  36. struct drm_crtc_commit *commit =
  37. container_of(kref, struct drm_crtc_commit, ref);
  38. kfree(commit);
  39. }
  40. EXPORT_SYMBOL(__drm_crtc_commit_free);
  41. /**
  42. * drm_atomic_state_default_release -
  43. * release memory initialized by drm_atomic_state_init
  44. * @state: atomic state
  45. *
  46. * Free all the memory allocated by drm_atomic_state_init.
  47. * This is useful for drivers that subclass the atomic state.
  48. */
  49. void drm_atomic_state_default_release(struct drm_atomic_state *state)
  50. {
  51. kfree(state->connectors);
  52. kfree(state->crtcs);
  53. kfree(state->planes);
  54. }
  55. EXPORT_SYMBOL(drm_atomic_state_default_release);
  56. /**
  57. * drm_atomic_state_init - init new atomic state
  58. * @dev: DRM device
  59. * @state: atomic state
  60. *
  61. * Default implementation for filling in a new atomic state.
  62. * This is useful for drivers that subclass the atomic state.
  63. */
  64. int
  65. drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
  66. {
  67. kref_init(&state->ref);
  68. /* TODO legacy paths should maybe do a better job about
  69. * setting this appropriately?
  70. */
  71. state->allow_modeset = true;
  72. state->crtcs = kcalloc(dev->mode_config.num_crtc,
  73. sizeof(*state->crtcs), GFP_KERNEL);
  74. if (!state->crtcs)
  75. goto fail;
  76. state->planes = kcalloc(dev->mode_config.num_total_plane,
  77. sizeof(*state->planes), GFP_KERNEL);
  78. if (!state->planes)
  79. goto fail;
  80. state->dev = dev;
  81. DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
  82. return 0;
  83. fail:
  84. drm_atomic_state_default_release(state);
  85. return -ENOMEM;
  86. }
  87. EXPORT_SYMBOL(drm_atomic_state_init);
  88. /**
  89. * drm_atomic_state_alloc - allocate atomic state
  90. * @dev: DRM device
  91. *
  92. * This allocates an empty atomic state to track updates.
  93. */
  94. struct drm_atomic_state *
  95. drm_atomic_state_alloc(struct drm_device *dev)
  96. {
  97. struct drm_mode_config *config = &dev->mode_config;
  98. struct drm_atomic_state *state;
  99. if (!config->funcs->atomic_state_alloc) {
  100. state = kzalloc(sizeof(*state), GFP_KERNEL);
  101. if (!state)
  102. return NULL;
  103. if (drm_atomic_state_init(dev, state) < 0) {
  104. kfree(state);
  105. return NULL;
  106. }
  107. return state;
  108. }
  109. return config->funcs->atomic_state_alloc(dev);
  110. }
  111. EXPORT_SYMBOL(drm_atomic_state_alloc);
  112. /**
  113. * drm_atomic_state_default_clear - clear base atomic state
  114. * @state: atomic state
  115. *
  116. * Default implementation for clearing atomic state.
  117. * This is useful for drivers that subclass the atomic state.
  118. */
  119. void drm_atomic_state_default_clear(struct drm_atomic_state *state)
  120. {
  121. struct drm_device *dev = state->dev;
  122. struct drm_mode_config *config = &dev->mode_config;
  123. int i;
  124. DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
  125. for (i = 0; i < state->num_connector; i++) {
  126. struct drm_connector *connector = state->connectors[i].ptr;
  127. if (!connector)
  128. continue;
  129. connector->funcs->atomic_destroy_state(connector,
  130. state->connectors[i].state);
  131. state->connectors[i].ptr = NULL;
  132. state->connectors[i].state = NULL;
  133. drm_connector_unreference(connector);
  134. }
  135. for (i = 0; i < config->num_crtc; i++) {
  136. struct drm_crtc *crtc = state->crtcs[i].ptr;
  137. if (!crtc)
  138. continue;
  139. crtc->funcs->atomic_destroy_state(crtc,
  140. state->crtcs[i].state);
  141. if (state->crtcs[i].commit) {
  142. kfree(state->crtcs[i].commit->event);
  143. state->crtcs[i].commit->event = NULL;
  144. drm_crtc_commit_put(state->crtcs[i].commit);
  145. }
  146. state->crtcs[i].commit = NULL;
  147. state->crtcs[i].ptr = NULL;
  148. state->crtcs[i].state = NULL;
  149. }
  150. for (i = 0; i < config->num_total_plane; i++) {
  151. struct drm_plane *plane = state->planes[i].ptr;
  152. if (!plane)
  153. continue;
  154. plane->funcs->atomic_destroy_state(plane,
  155. state->planes[i].state);
  156. state->planes[i].ptr = NULL;
  157. state->planes[i].state = NULL;
  158. }
  159. }
  160. EXPORT_SYMBOL(drm_atomic_state_default_clear);
  161. /**
  162. * drm_atomic_state_clear - clear state object
  163. * @state: atomic state
  164. *
  165. * When the w/w mutex algorithm detects a deadlock we need to back off and drop
  166. * all locks. So someone else could sneak in and change the current modeset
  167. * configuration. Which means that all the state assembled in @state is no
  168. * longer an atomic update to the current state, but to some arbitrary earlier
  169. * state. Which could break assumptions the driver's ->atomic_check likely
  170. * relies on.
  171. *
  172. * Hence we must clear all cached state and completely start over, using this
  173. * function.
  174. */
  175. void drm_atomic_state_clear(struct drm_atomic_state *state)
  176. {
  177. struct drm_device *dev = state->dev;
  178. struct drm_mode_config *config = &dev->mode_config;
  179. if (config->funcs->atomic_state_clear)
  180. config->funcs->atomic_state_clear(state);
  181. else
  182. drm_atomic_state_default_clear(state);
  183. }
  184. EXPORT_SYMBOL(drm_atomic_state_clear);
  185. /**
  186. * __drm_atomic_state_free - free all memory for an atomic state
  187. * @ref: This atomic state to deallocate
  188. *
  189. * This frees all memory associated with an atomic state, including all the
  190. * per-object state for planes, crtcs and connectors.
  191. */
  192. void __drm_atomic_state_free(struct kref *ref)
  193. {
  194. struct drm_atomic_state *state = container_of(ref, typeof(*state), ref);
  195. struct drm_mode_config *config = &state->dev->mode_config;
  196. drm_atomic_state_clear(state);
  197. DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
  198. if (config->funcs->atomic_state_free) {
  199. config->funcs->atomic_state_free(state);
  200. } else {
  201. drm_atomic_state_default_release(state);
  202. kfree(state);
  203. }
  204. }
  205. EXPORT_SYMBOL(__drm_atomic_state_free);
  206. /**
  207. * drm_atomic_get_crtc_state - get crtc state
  208. * @state: global atomic state object
  209. * @crtc: crtc to get state object for
  210. *
  211. * This function returns the crtc state for the given crtc, allocating it if
  212. * needed. It will also grab the relevant crtc lock to make sure that the state
  213. * is consistent.
  214. *
  215. * Returns:
  216. *
  217. * Either the allocated state or the error code encoded into the pointer. When
  218. * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
  219. * entire atomic sequence must be restarted. All other errors are fatal.
  220. */
  221. struct drm_crtc_state *
  222. drm_atomic_get_crtc_state(struct drm_atomic_state *state,
  223. struct drm_crtc *crtc)
  224. {
  225. int ret, index = drm_crtc_index(crtc);
  226. struct drm_crtc_state *crtc_state;
  227. WARN_ON(!state->acquire_ctx);
  228. crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
  229. if (crtc_state)
  230. return crtc_state;
  231. ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
  232. if (ret)
  233. return ERR_PTR(ret);
  234. crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
  235. if (!crtc_state)
  236. return ERR_PTR(-ENOMEM);
  237. state->crtcs[index].state = crtc_state;
  238. state->crtcs[index].ptr = crtc;
  239. crtc_state->state = state;
  240. DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
  241. crtc->base.id, crtc->name, crtc_state, state);
  242. return crtc_state;
  243. }
  244. EXPORT_SYMBOL(drm_atomic_get_crtc_state);
  245. static void set_out_fence_for_crtc(struct drm_atomic_state *state,
  246. struct drm_crtc *crtc, s64 __user *fence_ptr)
  247. {
  248. state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
  249. }
  250. static s64 __user *get_out_fence_for_crtc(struct drm_atomic_state *state,
  251. struct drm_crtc *crtc)
  252. {
  253. s64 __user *fence_ptr;
  254. fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
  255. state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;
  256. return fence_ptr;
  257. }
  258. /**
  259. * drm_atomic_set_mode_for_crtc - set mode for CRTC
  260. * @state: the CRTC whose incoming state to update
  261. * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
  262. *
  263. * Set a mode (originating from the kernel) on the desired CRTC state. Does
  264. * not change any other state properties, including enable, active, or
  265. * mode_changed.
  266. *
  267. * RETURNS:
  268. * Zero on success, error code on failure. Cannot return -EDEADLK.
  269. */
  270. int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
  271. struct drm_display_mode *mode)
  272. {
  273. struct drm_mode_modeinfo umode;
  274. /* Early return for no change. */
  275. if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
  276. return 0;
  277. drm_property_unreference_blob(state->mode_blob);
  278. state->mode_blob = NULL;
  279. if (mode) {
  280. drm_mode_convert_to_umode(&umode, mode);
  281. state->mode_blob =
  282. drm_property_create_blob(state->crtc->dev,
  283. sizeof(umode),
  284. &umode);
  285. if (IS_ERR(state->mode_blob))
  286. return PTR_ERR(state->mode_blob);
  287. drm_mode_copy(&state->mode, mode);
  288. state->enable = true;
  289. DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
  290. mode->name, state);
  291. } else {
  292. memset(&state->mode, 0, sizeof(state->mode));
  293. state->enable = false;
  294. DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
  295. state);
  296. }
  297. return 0;
  298. }
  299. EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
  300. /**
  301. * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
  302. * @state: the CRTC whose incoming state to update
  303. * @blob: pointer to blob property to use for mode
  304. *
  305. * Set a mode (originating from a blob property) on the desired CRTC state.
  306. * This function will take a reference on the blob property for the CRTC state,
  307. * and release the reference held on the state's existing mode property, if any
  308. * was set.
  309. *
  310. * RETURNS:
  311. * Zero on success, error code on failure. Cannot return -EDEADLK.
  312. */
  313. int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
  314. struct drm_property_blob *blob)
  315. {
  316. if (blob == state->mode_blob)
  317. return 0;
  318. drm_property_unreference_blob(state->mode_blob);
  319. state->mode_blob = NULL;
  320. memset(&state->mode, 0, sizeof(state->mode));
  321. if (blob) {
  322. if (blob->length != sizeof(struct drm_mode_modeinfo) ||
  323. drm_mode_convert_umode(&state->mode,
  324. (const struct drm_mode_modeinfo *)
  325. blob->data))
  326. return -EINVAL;
  327. state->mode_blob = drm_property_reference_blob(blob);
  328. state->enable = true;
  329. DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
  330. state->mode.name, state);
  331. } else {
  332. state->enable = false;
  333. DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
  334. state);
  335. }
  336. return 0;
  337. }
  338. EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
  339. /**
  340. * drm_atomic_replace_property_blob - replace a blob property
  341. * @blob: a pointer to the member blob to be replaced
  342. * @new_blob: the new blob to replace with
  343. * @replaced: whether the blob has been replaced
  344. *
  345. * RETURNS:
  346. * Zero on success, error code on failure
  347. */
  348. static void
  349. drm_atomic_replace_property_blob(struct drm_property_blob **blob,
  350. struct drm_property_blob *new_blob,
  351. bool *replaced)
  352. {
  353. struct drm_property_blob *old_blob = *blob;
  354. if (old_blob == new_blob)
  355. return;
  356. drm_property_unreference_blob(old_blob);
  357. if (new_blob)
  358. drm_property_reference_blob(new_blob);
  359. *blob = new_blob;
  360. *replaced = true;
  361. return;
  362. }
  363. static int
  364. drm_atomic_replace_property_blob_from_id(struct drm_crtc *crtc,
  365. struct drm_property_blob **blob,
  366. uint64_t blob_id,
  367. ssize_t expected_size,
  368. bool *replaced)
  369. {
  370. struct drm_property_blob *new_blob = NULL;
  371. if (blob_id != 0) {
  372. new_blob = drm_property_lookup_blob(crtc->dev, blob_id);
  373. if (new_blob == NULL)
  374. return -EINVAL;
  375. if (expected_size > 0 && expected_size != new_blob->length) {
  376. drm_property_unreference_blob(new_blob);
  377. return -EINVAL;
  378. }
  379. }
  380. drm_atomic_replace_property_blob(blob, new_blob, replaced);
  381. drm_property_unreference_blob(new_blob);
  382. return 0;
  383. }
  384. /**
  385. * drm_atomic_crtc_set_property - set property on CRTC
  386. * @crtc: the drm CRTC to set a property on
  387. * @state: the state object to update with the new property value
  388. * @property: the property to set
  389. * @val: the new property value
  390. *
  391. * Use this instead of calling crtc->atomic_set_property directly.
  392. * This function handles generic/core properties and calls out to
  393. * driver's ->atomic_set_property() for driver properties. To ensure
  394. * consistent behavior you must call this function rather than the
  395. * driver hook directly.
  396. *
  397. * RETURNS:
  398. * Zero on success, error code on failure
  399. */
  400. int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
  401. struct drm_crtc_state *state, struct drm_property *property,
  402. uint64_t val)
  403. {
  404. struct drm_device *dev = crtc->dev;
  405. struct drm_mode_config *config = &dev->mode_config;
  406. bool replaced = false;
  407. int ret;
  408. if (property == config->prop_active)
  409. state->active = val;
  410. else if (property == config->prop_mode_id) {
  411. struct drm_property_blob *mode =
  412. drm_property_lookup_blob(dev, val);
  413. ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
  414. drm_property_unreference_blob(mode);
  415. return ret;
  416. } else if (property == config->degamma_lut_property) {
  417. ret = drm_atomic_replace_property_blob_from_id(crtc,
  418. &state->degamma_lut,
  419. val,
  420. -1,
  421. &replaced);
  422. state->color_mgmt_changed |= replaced;
  423. return ret;
  424. } else if (property == config->ctm_property) {
  425. ret = drm_atomic_replace_property_blob_from_id(crtc,
  426. &state->ctm,
  427. val,
  428. sizeof(struct drm_color_ctm),
  429. &replaced);
  430. state->color_mgmt_changed |= replaced;
  431. return ret;
  432. } else if (property == config->gamma_lut_property) {
  433. ret = drm_atomic_replace_property_blob_from_id(crtc,
  434. &state->gamma_lut,
  435. val,
  436. -1,
  437. &replaced);
  438. state->color_mgmt_changed |= replaced;
  439. return ret;
  440. } else if (property == config->prop_out_fence_ptr) {
  441. s64 __user *fence_ptr = u64_to_user_ptr(val);
  442. if (!fence_ptr)
  443. return 0;
  444. if (put_user(-1, fence_ptr))
  445. return -EFAULT;
  446. set_out_fence_for_crtc(state->state, crtc, fence_ptr);
  447. } else if (crtc->funcs->atomic_set_property)
  448. return crtc->funcs->atomic_set_property(crtc, state, property, val);
  449. else
  450. return -EINVAL;
  451. return 0;
  452. }
  453. EXPORT_SYMBOL(drm_atomic_crtc_set_property);
  454. /**
  455. * drm_atomic_crtc_get_property - get property value from CRTC state
  456. * @crtc: the drm CRTC to set a property on
  457. * @state: the state object to get the property value from
  458. * @property: the property to set
  459. * @val: return location for the property value
  460. *
  461. * This function handles generic/core properties and calls out to
  462. * driver's ->atomic_get_property() for driver properties. To ensure
  463. * consistent behavior you must call this function rather than the
  464. * driver hook directly.
  465. *
  466. * RETURNS:
  467. * Zero on success, error code on failure
  468. */
  469. static int
  470. drm_atomic_crtc_get_property(struct drm_crtc *crtc,
  471. const struct drm_crtc_state *state,
  472. struct drm_property *property, uint64_t *val)
  473. {
  474. struct drm_device *dev = crtc->dev;
  475. struct drm_mode_config *config = &dev->mode_config;
  476. if (property == config->prop_active)
  477. *val = state->active;
  478. else if (property == config->prop_mode_id)
  479. *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
  480. else if (property == config->degamma_lut_property)
  481. *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
  482. else if (property == config->ctm_property)
  483. *val = (state->ctm) ? state->ctm->base.id : 0;
  484. else if (property == config->gamma_lut_property)
  485. *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
  486. else if (property == config->prop_out_fence_ptr)
  487. *val = 0;
  488. else if (crtc->funcs->atomic_get_property)
  489. return crtc->funcs->atomic_get_property(crtc, state, property, val);
  490. else
  491. return -EINVAL;
  492. return 0;
  493. }
  494. /**
  495. * drm_atomic_crtc_check - check crtc state
  496. * @crtc: crtc to check
  497. * @state: crtc state to check
  498. *
  499. * Provides core sanity checks for crtc state.
  500. *
  501. * RETURNS:
  502. * Zero on success, error code on failure
  503. */
  504. static int drm_atomic_crtc_check(struct drm_crtc *crtc,
  505. struct drm_crtc_state *state)
  506. {
  507. /* NOTE: we explicitly don't enforce constraints such as primary
  508. * layer covering entire screen, since that is something we want
  509. * to allow (on hw that supports it). For hw that does not, it
  510. * should be checked in driver's crtc->atomic_check() vfunc.
  511. *
  512. * TODO: Add generic modeset state checks once we support those.
  513. */
  514. if (state->active && !state->enable) {
  515. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
  516. crtc->base.id, crtc->name);
  517. return -EINVAL;
  518. }
  519. /* The state->enable vs. state->mode_blob checks can be WARN_ON,
  520. * as this is a kernel-internal detail that userspace should never
  521. * be able to trigger. */
  522. if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
  523. WARN_ON(state->enable && !state->mode_blob)) {
  524. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
  525. crtc->base.id, crtc->name);
  526. return -EINVAL;
  527. }
  528. if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
  529. WARN_ON(!state->enable && state->mode_blob)) {
  530. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
  531. crtc->base.id, crtc->name);
  532. return -EINVAL;
  533. }
  534. /*
  535. * Reject event generation for when a CRTC is off and stays off.
  536. * It wouldn't be hard to implement this, but userspace has a track
  537. * record of happily burning through 100% cpu (or worse, crash) when the
  538. * display pipe is suspended. To avoid all that fun just reject updates
  539. * that ask for events since likely that indicates a bug in the
  540. * compositor's drawing loop. This is consistent with the vblank IOCTL
  541. * and legacy page_flip IOCTL which also reject service on a disabled
  542. * pipe.
  543. */
  544. if (state->event && !state->active && !crtc->state->active) {
  545. DRM_DEBUG_ATOMIC("[CRTC:%d] requesting event but off\n",
  546. crtc->base.id);
  547. return -EINVAL;
  548. }
  549. return 0;
  550. }
  551. static void drm_atomic_crtc_print_state(struct drm_printer *p,
  552. const struct drm_crtc_state *state)
  553. {
  554. struct drm_crtc *crtc = state->crtc;
  555. drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name);
  556. drm_printf(p, "\tenable=%d\n", state->enable);
  557. drm_printf(p, "\tactive=%d\n", state->active);
  558. drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed);
  559. drm_printf(p, "\tmode_changed=%d\n", state->mode_changed);
  560. drm_printf(p, "\tactive_changed=%d\n", state->active_changed);
  561. drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed);
  562. drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
  563. drm_printf(p, "\tplane_mask=%x\n", state->plane_mask);
  564. drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask);
  565. drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask);
  566. drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode));
  567. if (crtc->funcs->atomic_print_state)
  568. crtc->funcs->atomic_print_state(p, state);
  569. }
  570. /**
  571. * drm_atomic_get_plane_state - get plane state
  572. * @state: global atomic state object
  573. * @plane: plane to get state object for
  574. *
  575. * This function returns the plane state for the given plane, allocating it if
  576. * needed. It will also grab the relevant plane lock to make sure that the state
  577. * is consistent.
  578. *
  579. * Returns:
  580. *
  581. * Either the allocated state or the error code encoded into the pointer. When
  582. * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
  583. * entire atomic sequence must be restarted. All other errors are fatal.
  584. */
  585. struct drm_plane_state *
  586. drm_atomic_get_plane_state(struct drm_atomic_state *state,
  587. struct drm_plane *plane)
  588. {
  589. int ret, index = drm_plane_index(plane);
  590. struct drm_plane_state *plane_state;
  591. WARN_ON(!state->acquire_ctx);
  592. plane_state = drm_atomic_get_existing_plane_state(state, plane);
  593. if (plane_state)
  594. return plane_state;
  595. ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
  596. if (ret)
  597. return ERR_PTR(ret);
  598. plane_state = plane->funcs->atomic_duplicate_state(plane);
  599. if (!plane_state)
  600. return ERR_PTR(-ENOMEM);
  601. state->planes[index].state = plane_state;
  602. state->planes[index].ptr = plane;
  603. plane_state->state = state;
  604. DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
  605. plane->base.id, plane->name, plane_state, state);
  606. if (plane_state->crtc) {
  607. struct drm_crtc_state *crtc_state;
  608. crtc_state = drm_atomic_get_crtc_state(state,
  609. plane_state->crtc);
  610. if (IS_ERR(crtc_state))
  611. return ERR_CAST(crtc_state);
  612. }
  613. return plane_state;
  614. }
  615. EXPORT_SYMBOL(drm_atomic_get_plane_state);
  616. /**
  617. * drm_atomic_plane_set_property - set property on plane
  618. * @plane: the drm plane to set a property on
  619. * @state: the state object to update with the new property value
  620. * @property: the property to set
  621. * @val: the new property value
  622. *
  623. * Use this instead of calling plane->atomic_set_property directly.
  624. * This function handles generic/core properties and calls out to
  625. * driver's ->atomic_set_property() for driver properties. To ensure
  626. * consistent behavior you must call this function rather than the
  627. * driver hook directly.
  628. *
  629. * RETURNS:
  630. * Zero on success, error code on failure
  631. */
  632. int drm_atomic_plane_set_property(struct drm_plane *plane,
  633. struct drm_plane_state *state, struct drm_property *property,
  634. uint64_t val)
  635. {
  636. struct drm_device *dev = plane->dev;
  637. struct drm_mode_config *config = &dev->mode_config;
  638. if (property == config->prop_fb_id) {
  639. struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
  640. drm_atomic_set_fb_for_plane(state, fb);
  641. if (fb)
  642. drm_framebuffer_unreference(fb);
  643. } else if (property == config->prop_in_fence_fd) {
  644. if (state->fence)
  645. return -EINVAL;
  646. if (U642I64(val) == -1)
  647. return 0;
  648. state->fence = sync_file_get_fence(val);
  649. if (!state->fence)
  650. return -EINVAL;
  651. } else if (property == config->prop_crtc_id) {
  652. struct drm_crtc *crtc = drm_crtc_find(dev, val);
  653. return drm_atomic_set_crtc_for_plane(state, crtc);
  654. } else if (property == config->prop_crtc_x) {
  655. state->crtc_x = U642I64(val);
  656. } else if (property == config->prop_crtc_y) {
  657. state->crtc_y = U642I64(val);
  658. } else if (property == config->prop_crtc_w) {
  659. state->crtc_w = val;
  660. } else if (property == config->prop_crtc_h) {
  661. state->crtc_h = val;
  662. } else if (property == config->prop_src_x) {
  663. state->src_x = val;
  664. } else if (property == config->prop_src_y) {
  665. state->src_y = val;
  666. } else if (property == config->prop_src_w) {
  667. state->src_w = val;
  668. } else if (property == config->prop_src_h) {
  669. state->src_h = val;
  670. } else if (property == plane->rotation_property) {
  671. if (!is_power_of_2(val & DRM_ROTATE_MASK))
  672. return -EINVAL;
  673. state->rotation = val;
  674. } else if (property == plane->zpos_property) {
  675. state->zpos = val;
  676. } else if (plane->funcs->atomic_set_property) {
  677. return plane->funcs->atomic_set_property(plane, state,
  678. property, val);
  679. } else {
  680. return -EINVAL;
  681. }
  682. return 0;
  683. }
  684. EXPORT_SYMBOL(drm_atomic_plane_set_property);
  685. /**
  686. * drm_atomic_plane_get_property - get property value from plane state
  687. * @plane: the drm plane to set a property on
  688. * @state: the state object to get the property value from
  689. * @property: the property to set
  690. * @val: return location for the property value
  691. *
  692. * This function handles generic/core properties and calls out to
  693. * driver's ->atomic_get_property() for driver properties. To ensure
  694. * consistent behavior you must call this function rather than the
  695. * driver hook directly.
  696. *
  697. * RETURNS:
  698. * Zero on success, error code on failure
  699. */
  700. static int
  701. drm_atomic_plane_get_property(struct drm_plane *plane,
  702. const struct drm_plane_state *state,
  703. struct drm_property *property, uint64_t *val)
  704. {
  705. struct drm_device *dev = plane->dev;
  706. struct drm_mode_config *config = &dev->mode_config;
  707. if (property == config->prop_fb_id) {
  708. *val = (state->fb) ? state->fb->base.id : 0;
  709. } else if (property == config->prop_in_fence_fd) {
  710. *val = -1;
  711. } else if (property == config->prop_crtc_id) {
  712. *val = (state->crtc) ? state->crtc->base.id : 0;
  713. } else if (property == config->prop_crtc_x) {
  714. *val = I642U64(state->crtc_x);
  715. } else if (property == config->prop_crtc_y) {
  716. *val = I642U64(state->crtc_y);
  717. } else if (property == config->prop_crtc_w) {
  718. *val = state->crtc_w;
  719. } else if (property == config->prop_crtc_h) {
  720. *val = state->crtc_h;
  721. } else if (property == config->prop_src_x) {
  722. *val = state->src_x;
  723. } else if (property == config->prop_src_y) {
  724. *val = state->src_y;
  725. } else if (property == config->prop_src_w) {
  726. *val = state->src_w;
  727. } else if (property == config->prop_src_h) {
  728. *val = state->src_h;
  729. } else if (property == plane->rotation_property) {
  730. *val = state->rotation;
  731. } else if (property == plane->zpos_property) {
  732. *val = state->zpos;
  733. } else if (plane->funcs->atomic_get_property) {
  734. return plane->funcs->atomic_get_property(plane, state, property, val);
  735. } else {
  736. return -EINVAL;
  737. }
  738. return 0;
  739. }
  740. static bool
  741. plane_switching_crtc(struct drm_atomic_state *state,
  742. struct drm_plane *plane,
  743. struct drm_plane_state *plane_state)
  744. {
  745. if (!plane->state->crtc || !plane_state->crtc)
  746. return false;
  747. if (plane->state->crtc == plane_state->crtc)
  748. return false;
  749. /* This could be refined, but currently there's no helper or driver code
  750. * to implement direct switching of active planes nor userspace to take
  751. * advantage of more direct plane switching without the intermediate
  752. * full OFF state.
  753. */
  754. return true;
  755. }
  756. /**
  757. * drm_atomic_plane_check - check plane state
  758. * @plane: plane to check
  759. * @state: plane state to check
  760. *
  761. * Provides core sanity checks for plane state.
  762. *
  763. * RETURNS:
  764. * Zero on success, error code on failure
  765. */
  766. static int drm_atomic_plane_check(struct drm_plane *plane,
  767. struct drm_plane_state *state)
  768. {
  769. unsigned int fb_width, fb_height;
  770. int ret;
  771. /* either *both* CRTC and FB must be set, or neither */
  772. if (WARN_ON(state->crtc && !state->fb)) {
  773. DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
  774. return -EINVAL;
  775. } else if (WARN_ON(state->fb && !state->crtc)) {
  776. DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
  777. return -EINVAL;
  778. }
  779. /* if disabled, we don't care about the rest of the state: */
  780. if (!state->crtc)
  781. return 0;
  782. /* Check whether this plane is usable on this CRTC */
  783. if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
  784. DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
  785. return -EINVAL;
  786. }
  787. /* Check whether this plane supports the fb pixel format. */
  788. ret = drm_plane_check_pixel_format(plane, state->fb->format->format);
  789. if (ret) {
  790. struct drm_format_name_buf format_name;
  791. DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
  792. drm_get_format_name(state->fb->format->format,
  793. &format_name));
  794. return ret;
  795. }
  796. /* Give drivers some help against integer overflows */
  797. if (state->crtc_w > INT_MAX ||
  798. state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
  799. state->crtc_h > INT_MAX ||
  800. state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
  801. DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
  802. state->crtc_w, state->crtc_h,
  803. state->crtc_x, state->crtc_y);
  804. return -ERANGE;
  805. }
  806. fb_width = state->fb->width << 16;
  807. fb_height = state->fb->height << 16;
  808. /* Make sure source coordinates are inside the fb. */
  809. if (state->src_w > fb_width ||
  810. state->src_x > fb_width - state->src_w ||
  811. state->src_h > fb_height ||
  812. state->src_y > fb_height - state->src_h) {
  813. DRM_DEBUG_ATOMIC("Invalid source coordinates "
  814. "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
  815. state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
  816. state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
  817. state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
  818. state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
  819. return -ENOSPC;
  820. }
  821. if (plane_switching_crtc(state->state, plane, state)) {
  822. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
  823. plane->base.id, plane->name);
  824. return -EINVAL;
  825. }
  826. return 0;
  827. }
  828. static void drm_atomic_plane_print_state(struct drm_printer *p,
  829. const struct drm_plane_state *state)
  830. {
  831. struct drm_plane *plane = state->plane;
  832. struct drm_rect src = drm_plane_state_src(state);
  833. struct drm_rect dest = drm_plane_state_dest(state);
  834. drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name);
  835. drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
  836. drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0);
  837. if (state->fb) {
  838. struct drm_framebuffer *fb = state->fb;
  839. int i, n = fb->format->num_planes;
  840. struct drm_format_name_buf format_name;
  841. drm_printf(p, "\t\tformat=%s\n",
  842. drm_get_format_name(fb->format->format, &format_name));
  843. drm_printf(p, "\t\t\tmodifier=0x%llx\n", fb->modifier);
  844. drm_printf(p, "\t\tsize=%dx%d\n", fb->width, fb->height);
  845. drm_printf(p, "\t\tlayers:\n");
  846. for (i = 0; i < n; i++) {
  847. drm_printf(p, "\t\t\tpitch[%d]=%u\n", i, fb->pitches[i]);
  848. drm_printf(p, "\t\t\toffset[%d]=%u\n", i, fb->offsets[i]);
  849. }
  850. }
  851. drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest));
  852. drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src));
  853. drm_printf(p, "\trotation=%x\n", state->rotation);
  854. if (plane->funcs->atomic_print_state)
  855. plane->funcs->atomic_print_state(p, state);
  856. }
  857. /**
  858. * drm_atomic_get_connector_state - get connector state
  859. * @state: global atomic state object
  860. * @connector: connector to get state object for
  861. *
  862. * This function returns the connector state for the given connector,
  863. * allocating it if needed. It will also grab the relevant connector lock to
  864. * make sure that the state is consistent.
  865. *
  866. * Returns:
  867. *
  868. * Either the allocated state or the error code encoded into the pointer. When
  869. * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
  870. * entire atomic sequence must be restarted. All other errors are fatal.
  871. */
  872. struct drm_connector_state *
  873. drm_atomic_get_connector_state(struct drm_atomic_state *state,
  874. struct drm_connector *connector)
  875. {
  876. int ret, index;
  877. struct drm_mode_config *config = &connector->dev->mode_config;
  878. struct drm_connector_state *connector_state;
  879. WARN_ON(!state->acquire_ctx);
  880. ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
  881. if (ret)
  882. return ERR_PTR(ret);
  883. index = drm_connector_index(connector);
  884. if (index >= state->num_connector) {
  885. struct __drm_connnectors_state *c;
  886. int alloc = max(index + 1, config->num_connector);
  887. c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL);
  888. if (!c)
  889. return ERR_PTR(-ENOMEM);
  890. state->connectors = c;
  891. memset(&state->connectors[state->num_connector], 0,
  892. sizeof(*state->connectors) * (alloc - state->num_connector));
  893. state->num_connector = alloc;
  894. }
  895. if (state->connectors[index].state)
  896. return state->connectors[index].state;
  897. connector_state = connector->funcs->atomic_duplicate_state(connector);
  898. if (!connector_state)
  899. return ERR_PTR(-ENOMEM);
  900. drm_connector_reference(connector);
  901. state->connectors[index].state = connector_state;
  902. state->connectors[index].ptr = connector;
  903. connector_state->state = state;
  904. DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
  905. connector->base.id, connector_state, state);
  906. if (connector_state->crtc) {
  907. struct drm_crtc_state *crtc_state;
  908. crtc_state = drm_atomic_get_crtc_state(state,
  909. connector_state->crtc);
  910. if (IS_ERR(crtc_state))
  911. return ERR_CAST(crtc_state);
  912. }
  913. return connector_state;
  914. }
  915. EXPORT_SYMBOL(drm_atomic_get_connector_state);
  916. /**
  917. * drm_atomic_connector_set_property - set property on connector.
  918. * @connector: the drm connector to set a property on
  919. * @state: the state object to update with the new property value
  920. * @property: the property to set
  921. * @val: the new property value
  922. *
  923. * Use this instead of calling connector->atomic_set_property directly.
  924. * This function handles generic/core properties and calls out to
  925. * driver's ->atomic_set_property() for driver properties. To ensure
  926. * consistent behavior you must call this function rather than the
  927. * driver hook directly.
  928. *
  929. * RETURNS:
  930. * Zero on success, error code on failure
  931. */
  932. int drm_atomic_connector_set_property(struct drm_connector *connector,
  933. struct drm_connector_state *state, struct drm_property *property,
  934. uint64_t val)
  935. {
  936. struct drm_device *dev = connector->dev;
  937. struct drm_mode_config *config = &dev->mode_config;
  938. if (property == config->prop_crtc_id) {
  939. struct drm_crtc *crtc = drm_crtc_find(dev, val);
  940. return drm_atomic_set_crtc_for_connector(state, crtc);
  941. } else if (property == config->dpms_property) {
  942. /* setting DPMS property requires special handling, which
  943. * is done in legacy setprop path for us. Disallow (for
  944. * now?) atomic writes to DPMS property:
  945. */
  946. return -EINVAL;
  947. } else if (property == config->tv_select_subconnector_property) {
  948. state->tv.subconnector = val;
  949. } else if (property == config->tv_left_margin_property) {
  950. state->tv.margins.left = val;
  951. } else if (property == config->tv_right_margin_property) {
  952. state->tv.margins.right = val;
  953. } else if (property == config->tv_top_margin_property) {
  954. state->tv.margins.top = val;
  955. } else if (property == config->tv_bottom_margin_property) {
  956. state->tv.margins.bottom = val;
  957. } else if (property == config->tv_mode_property) {
  958. state->tv.mode = val;
  959. } else if (property == config->tv_brightness_property) {
  960. state->tv.brightness = val;
  961. } else if (property == config->tv_contrast_property) {
  962. state->tv.contrast = val;
  963. } else if (property == config->tv_flicker_reduction_property) {
  964. state->tv.flicker_reduction = val;
  965. } else if (property == config->tv_overscan_property) {
  966. state->tv.overscan = val;
  967. } else if (property == config->tv_saturation_property) {
  968. state->tv.saturation = val;
  969. } else if (property == config->tv_hue_property) {
  970. state->tv.hue = val;
  971. } else if (connector->funcs->atomic_set_property) {
  972. return connector->funcs->atomic_set_property(connector,
  973. state, property, val);
  974. } else {
  975. return -EINVAL;
  976. }
  977. return 0;
  978. }
  979. EXPORT_SYMBOL(drm_atomic_connector_set_property);
  980. static void drm_atomic_connector_print_state(struct drm_printer *p,
  981. const struct drm_connector_state *state)
  982. {
  983. struct drm_connector *connector = state->connector;
  984. drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
  985. drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
  986. if (connector->funcs->atomic_print_state)
  987. connector->funcs->atomic_print_state(p, state);
  988. }
  989. /**
  990. * drm_atomic_connector_get_property - get property value from connector state
  991. * @connector: the drm connector to set a property on
  992. * @state: the state object to get the property value from
  993. * @property: the property to set
  994. * @val: return location for the property value
  995. *
  996. * This function handles generic/core properties and calls out to
  997. * driver's ->atomic_get_property() for driver properties. To ensure
  998. * consistent behavior you must call this function rather than the
  999. * driver hook directly.
  1000. *
  1001. * RETURNS:
  1002. * Zero on success, error code on failure
  1003. */
  1004. static int
  1005. drm_atomic_connector_get_property(struct drm_connector *connector,
  1006. const struct drm_connector_state *state,
  1007. struct drm_property *property, uint64_t *val)
  1008. {
  1009. struct drm_device *dev = connector->dev;
  1010. struct drm_mode_config *config = &dev->mode_config;
  1011. if (property == config->prop_crtc_id) {
  1012. *val = (state->crtc) ? state->crtc->base.id : 0;
  1013. } else if (property == config->dpms_property) {
  1014. *val = connector->dpms;
  1015. } else if (property == config->tv_select_subconnector_property) {
  1016. *val = state->tv.subconnector;
  1017. } else if (property == config->tv_left_margin_property) {
  1018. *val = state->tv.margins.left;
  1019. } else if (property == config->tv_right_margin_property) {
  1020. *val = state->tv.margins.right;
  1021. } else if (property == config->tv_top_margin_property) {
  1022. *val = state->tv.margins.top;
  1023. } else if (property == config->tv_bottom_margin_property) {
  1024. *val = state->tv.margins.bottom;
  1025. } else if (property == config->tv_mode_property) {
  1026. *val = state->tv.mode;
  1027. } else if (property == config->tv_brightness_property) {
  1028. *val = state->tv.brightness;
  1029. } else if (property == config->tv_contrast_property) {
  1030. *val = state->tv.contrast;
  1031. } else if (property == config->tv_flicker_reduction_property) {
  1032. *val = state->tv.flicker_reduction;
  1033. } else if (property == config->tv_overscan_property) {
  1034. *val = state->tv.overscan;
  1035. } else if (property == config->tv_saturation_property) {
  1036. *val = state->tv.saturation;
  1037. } else if (property == config->tv_hue_property) {
  1038. *val = state->tv.hue;
  1039. } else if (connector->funcs->atomic_get_property) {
  1040. return connector->funcs->atomic_get_property(connector,
  1041. state, property, val);
  1042. } else {
  1043. return -EINVAL;
  1044. }
  1045. return 0;
  1046. }
  1047. int drm_atomic_get_property(struct drm_mode_object *obj,
  1048. struct drm_property *property, uint64_t *val)
  1049. {
  1050. struct drm_device *dev = property->dev;
  1051. int ret;
  1052. switch (obj->type) {
  1053. case DRM_MODE_OBJECT_CONNECTOR: {
  1054. struct drm_connector *connector = obj_to_connector(obj);
  1055. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  1056. ret = drm_atomic_connector_get_property(connector,
  1057. connector->state, property, val);
  1058. break;
  1059. }
  1060. case DRM_MODE_OBJECT_CRTC: {
  1061. struct drm_crtc *crtc = obj_to_crtc(obj);
  1062. WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
  1063. ret = drm_atomic_crtc_get_property(crtc,
  1064. crtc->state, property, val);
  1065. break;
  1066. }
  1067. case DRM_MODE_OBJECT_PLANE: {
  1068. struct drm_plane *plane = obj_to_plane(obj);
  1069. WARN_ON(!drm_modeset_is_locked(&plane->mutex));
  1070. ret = drm_atomic_plane_get_property(plane,
  1071. plane->state, property, val);
  1072. break;
  1073. }
  1074. default:
  1075. ret = -EINVAL;
  1076. break;
  1077. }
  1078. return ret;
  1079. }
  1080. /**
  1081. * drm_atomic_set_crtc_for_plane - set crtc for plane
  1082. * @plane_state: the plane whose incoming state to update
  1083. * @crtc: crtc to use for the plane
  1084. *
  1085. * Changing the assigned crtc for a plane requires us to grab the lock and state
  1086. * for the new crtc, as needed. This function takes care of all these details
  1087. * besides updating the pointer in the state object itself.
  1088. *
  1089. * Returns:
  1090. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  1091. * then the w/w mutex code has detected a deadlock and the entire atomic
  1092. * sequence must be restarted. All other errors are fatal.
  1093. */
  1094. int
  1095. drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
  1096. struct drm_crtc *crtc)
  1097. {
  1098. struct drm_plane *plane = plane_state->plane;
  1099. struct drm_crtc_state *crtc_state;
  1100. if (plane_state->crtc) {
  1101. crtc_state = drm_atomic_get_crtc_state(plane_state->state,
  1102. plane_state->crtc);
  1103. if (WARN_ON(IS_ERR(crtc_state)))
  1104. return PTR_ERR(crtc_state);
  1105. crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
  1106. }
  1107. plane_state->crtc = crtc;
  1108. if (crtc) {
  1109. crtc_state = drm_atomic_get_crtc_state(plane_state->state,
  1110. crtc);
  1111. if (IS_ERR(crtc_state))
  1112. return PTR_ERR(crtc_state);
  1113. crtc_state->plane_mask |= (1 << drm_plane_index(plane));
  1114. }
  1115. if (crtc)
  1116. DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n",
  1117. plane_state, crtc->base.id, crtc->name);
  1118. else
  1119. DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
  1120. plane_state);
  1121. return 0;
  1122. }
  1123. EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
  1124. /**
  1125. * drm_atomic_set_fb_for_plane - set framebuffer for plane
  1126. * @plane_state: atomic state object for the plane
  1127. * @fb: fb to use for the plane
  1128. *
  1129. * Changing the assigned framebuffer for a plane requires us to grab a reference
  1130. * to the new fb and drop the reference to the old fb, if there is one. This
  1131. * function takes care of all these details besides updating the pointer in the
  1132. * state object itself.
  1133. */
  1134. void
  1135. drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
  1136. struct drm_framebuffer *fb)
  1137. {
  1138. if (fb)
  1139. DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
  1140. fb->base.id, plane_state);
  1141. else
  1142. DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
  1143. plane_state);
  1144. drm_framebuffer_assign(&plane_state->fb, fb);
  1145. }
  1146. EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
  1147. /**
  1148. * drm_atomic_set_fence_for_plane - set fence for plane
  1149. * @plane_state: atomic state object for the plane
  1150. * @fence: dma_fence to use for the plane
  1151. *
  1152. * Helper to setup the plane_state fence in case it is not set yet.
  1153. * By using this drivers doesn't need to worry if the user choose
  1154. * implicit or explicit fencing.
  1155. *
  1156. * This function will not set the fence to the state if it was set
  1157. * via explicit fencing interfaces on the atomic ioctl. It will
  1158. * all drope the reference to the fence as we not storing it
  1159. * anywhere.
  1160. *
  1161. * Otherwise, if plane_state->fence is not set this function we
  1162. * just set it with the received implict fence.
  1163. */
  1164. void
  1165. drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
  1166. struct dma_fence *fence)
  1167. {
  1168. if (plane_state->fence) {
  1169. dma_fence_put(fence);
  1170. return;
  1171. }
  1172. plane_state->fence = fence;
  1173. }
  1174. EXPORT_SYMBOL(drm_atomic_set_fence_for_plane);
  1175. /**
  1176. * drm_atomic_set_crtc_for_connector - set crtc for connector
  1177. * @conn_state: atomic state object for the connector
  1178. * @crtc: crtc to use for the connector
  1179. *
  1180. * Changing the assigned crtc for a connector requires us to grab the lock and
  1181. * state for the new crtc, as needed. This function takes care of all these
  1182. * details besides updating the pointer in the state object itself.
  1183. *
  1184. * Returns:
  1185. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  1186. * then the w/w mutex code has detected a deadlock and the entire atomic
  1187. * sequence must be restarted. All other errors are fatal.
  1188. */
  1189. int
  1190. drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
  1191. struct drm_crtc *crtc)
  1192. {
  1193. struct drm_crtc_state *crtc_state;
  1194. if (conn_state->crtc == crtc)
  1195. return 0;
  1196. if (conn_state->crtc) {
  1197. crtc_state = drm_atomic_get_existing_crtc_state(conn_state->state,
  1198. conn_state->crtc);
  1199. crtc_state->connector_mask &=
  1200. ~(1 << drm_connector_index(conn_state->connector));
  1201. drm_connector_unreference(conn_state->connector);
  1202. conn_state->crtc = NULL;
  1203. }
  1204. if (crtc) {
  1205. crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
  1206. if (IS_ERR(crtc_state))
  1207. return PTR_ERR(crtc_state);
  1208. crtc_state->connector_mask |=
  1209. 1 << drm_connector_index(conn_state->connector);
  1210. drm_connector_reference(conn_state->connector);
  1211. conn_state->crtc = crtc;
  1212. DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n",
  1213. conn_state, crtc->base.id, crtc->name);
  1214. } else {
  1215. DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
  1216. conn_state);
  1217. }
  1218. return 0;
  1219. }
  1220. EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
  1221. /**
  1222. * drm_atomic_add_affected_connectors - add connectors for crtc
  1223. * @state: atomic state
  1224. * @crtc: DRM crtc
  1225. *
  1226. * This function walks the current configuration and adds all connectors
  1227. * currently using @crtc to the atomic configuration @state. Note that this
  1228. * function must acquire the connection mutex. This can potentially cause
  1229. * unneeded seralization if the update is just for the planes on one crtc. Hence
  1230. * drivers and helpers should only call this when really needed (e.g. when a
  1231. * full modeset needs to happen due to some change).
  1232. *
  1233. * Returns:
  1234. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  1235. * then the w/w mutex code has detected a deadlock and the entire atomic
  1236. * sequence must be restarted. All other errors are fatal.
  1237. */
  1238. int
  1239. drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
  1240. struct drm_crtc *crtc)
  1241. {
  1242. struct drm_mode_config *config = &state->dev->mode_config;
  1243. struct drm_connector *connector;
  1244. struct drm_connector_state *conn_state;
  1245. struct drm_connector_list_iter conn_iter;
  1246. int ret;
  1247. ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
  1248. if (ret)
  1249. return ret;
  1250. DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
  1251. crtc->base.id, crtc->name, state);
  1252. /*
  1253. * Changed connectors are already in @state, so only need to look at the
  1254. * current configuration.
  1255. */
  1256. drm_connector_list_iter_get(state->dev, &conn_iter);
  1257. drm_for_each_connector_iter(connector, &conn_iter) {
  1258. if (connector->state->crtc != crtc)
  1259. continue;
  1260. conn_state = drm_atomic_get_connector_state(state, connector);
  1261. if (IS_ERR(conn_state)) {
  1262. drm_connector_list_iter_put(&conn_iter);
  1263. return PTR_ERR(conn_state);
  1264. }
  1265. }
  1266. drm_connector_list_iter_put(&conn_iter);
  1267. return 0;
  1268. }
  1269. EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
  1270. /**
  1271. * drm_atomic_add_affected_planes - add planes for crtc
  1272. * @state: atomic state
  1273. * @crtc: DRM crtc
  1274. *
  1275. * This function walks the current configuration and adds all planes
  1276. * currently used by @crtc to the atomic configuration @state. This is useful
  1277. * when an atomic commit also needs to check all currently enabled plane on
  1278. * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
  1279. * to avoid special code to force-enable all planes.
  1280. *
  1281. * Since acquiring a plane state will always also acquire the w/w mutex of the
  1282. * current CRTC for that plane (if there is any) adding all the plane states for
  1283. * a CRTC will not reduce parallism of atomic updates.
  1284. *
  1285. * Returns:
  1286. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  1287. * then the w/w mutex code has detected a deadlock and the entire atomic
  1288. * sequence must be restarted. All other errors are fatal.
  1289. */
  1290. int
  1291. drm_atomic_add_affected_planes(struct drm_atomic_state *state,
  1292. struct drm_crtc *crtc)
  1293. {
  1294. struct drm_plane *plane;
  1295. WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
  1296. drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
  1297. struct drm_plane_state *plane_state =
  1298. drm_atomic_get_plane_state(state, plane);
  1299. if (IS_ERR(plane_state))
  1300. return PTR_ERR(plane_state);
  1301. }
  1302. return 0;
  1303. }
  1304. EXPORT_SYMBOL(drm_atomic_add_affected_planes);
  1305. /**
  1306. * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
  1307. * @state: atomic state
  1308. *
  1309. * This function should be used by legacy entry points which don't understand
  1310. * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
  1311. * the slowpath completed.
  1312. */
  1313. void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
  1314. {
  1315. struct drm_device *dev = state->dev;
  1316. unsigned crtc_mask = 0;
  1317. struct drm_crtc *crtc;
  1318. int ret;
  1319. bool global = false;
  1320. drm_for_each_crtc(crtc, dev) {
  1321. if (crtc->acquire_ctx != state->acquire_ctx)
  1322. continue;
  1323. crtc_mask |= drm_crtc_mask(crtc);
  1324. crtc->acquire_ctx = NULL;
  1325. }
  1326. if (WARN_ON(dev->mode_config.acquire_ctx == state->acquire_ctx)) {
  1327. global = true;
  1328. dev->mode_config.acquire_ctx = NULL;
  1329. }
  1330. retry:
  1331. drm_modeset_backoff(state->acquire_ctx);
  1332. ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
  1333. if (ret)
  1334. goto retry;
  1335. drm_for_each_crtc(crtc, dev)
  1336. if (drm_crtc_mask(crtc) & crtc_mask)
  1337. crtc->acquire_ctx = state->acquire_ctx;
  1338. if (global)
  1339. dev->mode_config.acquire_ctx = state->acquire_ctx;
  1340. }
  1341. EXPORT_SYMBOL(drm_atomic_legacy_backoff);
  1342. /**
  1343. * drm_atomic_check_only - check whether a given config would work
  1344. * @state: atomic configuration to check
  1345. *
  1346. * Note that this function can return -EDEADLK if the driver needed to acquire
  1347. * more locks but encountered a deadlock. The caller must then do the usual w/w
  1348. * backoff dance and restart. All other errors are fatal.
  1349. *
  1350. * Returns:
  1351. * 0 on success, negative error code on failure.
  1352. */
  1353. int drm_atomic_check_only(struct drm_atomic_state *state)
  1354. {
  1355. struct drm_device *dev = state->dev;
  1356. struct drm_mode_config *config = &dev->mode_config;
  1357. struct drm_plane *plane;
  1358. struct drm_plane_state *plane_state;
  1359. struct drm_crtc *crtc;
  1360. struct drm_crtc_state *crtc_state;
  1361. int i, ret = 0;
  1362. DRM_DEBUG_ATOMIC("checking %p\n", state);
  1363. for_each_plane_in_state(state, plane, plane_state, i) {
  1364. ret = drm_atomic_plane_check(plane, plane_state);
  1365. if (ret) {
  1366. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
  1367. plane->base.id, plane->name);
  1368. return ret;
  1369. }
  1370. }
  1371. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  1372. ret = drm_atomic_crtc_check(crtc, crtc_state);
  1373. if (ret) {
  1374. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
  1375. crtc->base.id, crtc->name);
  1376. return ret;
  1377. }
  1378. }
  1379. if (config->funcs->atomic_check)
  1380. ret = config->funcs->atomic_check(state->dev, state);
  1381. if (!state->allow_modeset) {
  1382. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  1383. if (drm_atomic_crtc_needs_modeset(crtc_state)) {
  1384. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
  1385. crtc->base.id, crtc->name);
  1386. return -EINVAL;
  1387. }
  1388. }
  1389. }
  1390. return ret;
  1391. }
  1392. EXPORT_SYMBOL(drm_atomic_check_only);
  1393. /**
  1394. * drm_atomic_commit - commit configuration atomically
  1395. * @state: atomic configuration to check
  1396. *
  1397. * Note that this function can return -EDEADLK if the driver needed to acquire
  1398. * more locks but encountered a deadlock. The caller must then do the usual w/w
  1399. * backoff dance and restart. All other errors are fatal.
  1400. *
  1401. * This function will take its own reference on @state.
  1402. * Callers should always release their reference with drm_atomic_state_put().
  1403. *
  1404. * Returns:
  1405. * 0 on success, negative error code on failure.
  1406. */
  1407. int drm_atomic_commit(struct drm_atomic_state *state)
  1408. {
  1409. struct drm_mode_config *config = &state->dev->mode_config;
  1410. int ret;
  1411. ret = drm_atomic_check_only(state);
  1412. if (ret)
  1413. return ret;
  1414. DRM_DEBUG_ATOMIC("commiting %p\n", state);
  1415. return config->funcs->atomic_commit(state->dev, state, false);
  1416. }
  1417. EXPORT_SYMBOL(drm_atomic_commit);
  1418. /**
  1419. * drm_atomic_nonblocking_commit - atomic&nonblocking configuration commit
  1420. * @state: atomic configuration to check
  1421. *
  1422. * Note that this function can return -EDEADLK if the driver needed to acquire
  1423. * more locks but encountered a deadlock. The caller must then do the usual w/w
  1424. * backoff dance and restart. All other errors are fatal.
  1425. *
  1426. * This function will take its own reference on @state.
  1427. * Callers should always release their reference with drm_atomic_state_put().
  1428. *
  1429. * Returns:
  1430. * 0 on success, negative error code on failure.
  1431. */
  1432. int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
  1433. {
  1434. struct drm_mode_config *config = &state->dev->mode_config;
  1435. int ret;
  1436. ret = drm_atomic_check_only(state);
  1437. if (ret)
  1438. return ret;
  1439. DRM_DEBUG_ATOMIC("commiting %p nonblocking\n", state);
  1440. return config->funcs->atomic_commit(state->dev, state, true);
  1441. }
  1442. EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
  1443. static void drm_atomic_print_state(const struct drm_atomic_state *state)
  1444. {
  1445. struct drm_printer p = drm_info_printer(state->dev->dev);
  1446. struct drm_plane *plane;
  1447. struct drm_plane_state *plane_state;
  1448. struct drm_crtc *crtc;
  1449. struct drm_crtc_state *crtc_state;
  1450. struct drm_connector *connector;
  1451. struct drm_connector_state *connector_state;
  1452. int i;
  1453. DRM_DEBUG_ATOMIC("checking %p\n", state);
  1454. for_each_plane_in_state(state, plane, plane_state, i)
  1455. drm_atomic_plane_print_state(&p, plane_state);
  1456. for_each_crtc_in_state(state, crtc, crtc_state, i)
  1457. drm_atomic_crtc_print_state(&p, crtc_state);
  1458. for_each_connector_in_state(state, connector, connector_state, i)
  1459. drm_atomic_connector_print_state(&p, connector_state);
  1460. }
  1461. /**
  1462. * drm_state_dump - dump entire device atomic state
  1463. * @dev: the drm device
  1464. * @p: where to print the state to
  1465. *
  1466. * Just for debugging. Drivers might want an option to dump state
  1467. * to dmesg in case of error irq's. (Hint, you probably want to
  1468. * ratelimit this!)
  1469. *
  1470. * The caller must drm_modeset_lock_all(), or if this is called
  1471. * from error irq handler, it should not be enabled by default.
  1472. * (Ie. if you are debugging errors you might not care that this
  1473. * is racey. But calling this without all modeset locks held is
  1474. * not inherently safe.)
  1475. */
  1476. void drm_state_dump(struct drm_device *dev, struct drm_printer *p)
  1477. {
  1478. struct drm_mode_config *config = &dev->mode_config;
  1479. struct drm_plane *plane;
  1480. struct drm_crtc *crtc;
  1481. struct drm_connector *connector;
  1482. struct drm_connector_list_iter conn_iter;
  1483. if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
  1484. return;
  1485. list_for_each_entry(plane, &config->plane_list, head)
  1486. drm_atomic_plane_print_state(p, plane->state);
  1487. list_for_each_entry(crtc, &config->crtc_list, head)
  1488. drm_atomic_crtc_print_state(p, crtc->state);
  1489. drm_connector_list_iter_get(dev, &conn_iter);
  1490. drm_for_each_connector_iter(connector, &conn_iter)
  1491. drm_atomic_connector_print_state(p, connector->state);
  1492. drm_connector_list_iter_put(&conn_iter);
  1493. }
  1494. EXPORT_SYMBOL(drm_state_dump);
  1495. #ifdef CONFIG_DEBUG_FS
  1496. static int drm_state_info(struct seq_file *m, void *data)
  1497. {
  1498. struct drm_info_node *node = (struct drm_info_node *) m->private;
  1499. struct drm_device *dev = node->minor->dev;
  1500. struct drm_printer p = drm_seq_file_printer(m);
  1501. drm_modeset_lock_all(dev);
  1502. drm_state_dump(dev, &p);
  1503. drm_modeset_unlock_all(dev);
  1504. return 0;
  1505. }
  1506. /* any use in debugfs files to dump individual planes/crtc/etc? */
  1507. static const struct drm_info_list drm_atomic_debugfs_list[] = {
  1508. {"state", drm_state_info, 0},
  1509. };
  1510. int drm_atomic_debugfs_init(struct drm_minor *minor)
  1511. {
  1512. return drm_debugfs_create_files(drm_atomic_debugfs_list,
  1513. ARRAY_SIZE(drm_atomic_debugfs_list),
  1514. minor->debugfs_root, minor);
  1515. }
  1516. int drm_atomic_debugfs_cleanup(struct drm_minor *minor)
  1517. {
  1518. return drm_debugfs_remove_files(drm_atomic_debugfs_list,
  1519. ARRAY_SIZE(drm_atomic_debugfs_list),
  1520. minor);
  1521. }
  1522. #endif
  1523. /*
  1524. * The big monstor ioctl
  1525. */
  1526. static struct drm_pending_vblank_event *create_vblank_event(
  1527. struct drm_device *dev, uint64_t user_data)
  1528. {
  1529. struct drm_pending_vblank_event *e = NULL;
  1530. e = kzalloc(sizeof *e, GFP_KERNEL);
  1531. if (!e)
  1532. return NULL;
  1533. e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
  1534. e->event.base.length = sizeof(e->event);
  1535. e->event.user_data = user_data;
  1536. return e;
  1537. }
  1538. static int atomic_set_prop(struct drm_atomic_state *state,
  1539. struct drm_mode_object *obj, struct drm_property *prop,
  1540. uint64_t prop_value)
  1541. {
  1542. struct drm_mode_object *ref;
  1543. int ret;
  1544. if (!drm_property_change_valid_get(prop, prop_value, &ref))
  1545. return -EINVAL;
  1546. switch (obj->type) {
  1547. case DRM_MODE_OBJECT_CONNECTOR: {
  1548. struct drm_connector *connector = obj_to_connector(obj);
  1549. struct drm_connector_state *connector_state;
  1550. connector_state = drm_atomic_get_connector_state(state, connector);
  1551. if (IS_ERR(connector_state)) {
  1552. ret = PTR_ERR(connector_state);
  1553. break;
  1554. }
  1555. ret = drm_atomic_connector_set_property(connector,
  1556. connector_state, prop, prop_value);
  1557. break;
  1558. }
  1559. case DRM_MODE_OBJECT_CRTC: {
  1560. struct drm_crtc *crtc = obj_to_crtc(obj);
  1561. struct drm_crtc_state *crtc_state;
  1562. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  1563. if (IS_ERR(crtc_state)) {
  1564. ret = PTR_ERR(crtc_state);
  1565. break;
  1566. }
  1567. ret = drm_atomic_crtc_set_property(crtc,
  1568. crtc_state, prop, prop_value);
  1569. break;
  1570. }
  1571. case DRM_MODE_OBJECT_PLANE: {
  1572. struct drm_plane *plane = obj_to_plane(obj);
  1573. struct drm_plane_state *plane_state;
  1574. plane_state = drm_atomic_get_plane_state(state, plane);
  1575. if (IS_ERR(plane_state)) {
  1576. ret = PTR_ERR(plane_state);
  1577. break;
  1578. }
  1579. ret = drm_atomic_plane_set_property(plane,
  1580. plane_state, prop, prop_value);
  1581. break;
  1582. }
  1583. default:
  1584. ret = -EINVAL;
  1585. break;
  1586. }
  1587. drm_property_change_valid_put(prop, ref);
  1588. return ret;
  1589. }
  1590. /**
  1591. * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers.
  1592. *
  1593. * @dev: drm device to check.
  1594. * @plane_mask: plane mask for planes that were updated.
  1595. * @ret: return value, can be -EDEADLK for a retry.
  1596. *
  1597. * Before doing an update plane->old_fb is set to plane->fb,
  1598. * but before dropping the locks old_fb needs to be set to NULL
  1599. * and plane->fb updated. This is a common operation for each
  1600. * atomic update, so this call is split off as a helper.
  1601. */
  1602. void drm_atomic_clean_old_fb(struct drm_device *dev,
  1603. unsigned plane_mask,
  1604. int ret)
  1605. {
  1606. struct drm_plane *plane;
  1607. /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
  1608. * locks (ie. while it is still safe to deref plane->state). We
  1609. * need to do this here because the driver entry points cannot
  1610. * distinguish between legacy and atomic ioctls.
  1611. */
  1612. drm_for_each_plane_mask(plane, dev, plane_mask) {
  1613. if (ret == 0) {
  1614. struct drm_framebuffer *new_fb = plane->state->fb;
  1615. if (new_fb)
  1616. drm_framebuffer_reference(new_fb);
  1617. plane->fb = new_fb;
  1618. plane->crtc = plane->state->crtc;
  1619. if (plane->old_fb)
  1620. drm_framebuffer_unreference(plane->old_fb);
  1621. }
  1622. plane->old_fb = NULL;
  1623. }
  1624. }
  1625. EXPORT_SYMBOL(drm_atomic_clean_old_fb);
  1626. /**
  1627. * DOC: explicit fencing properties
  1628. *
  1629. * Explicit fencing allows userspace to control the buffer synchronization
  1630. * between devices. A Fence or a group of fences are transfered to/from
  1631. * userspace using Sync File fds and there are two DRM properties for that.
  1632. * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
  1633. * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
  1634. *
  1635. * As a contrast, with implicit fencing the kernel keeps track of any
  1636. * ongoing rendering, and automatically ensures that the atomic update waits
  1637. * for any pending rendering to complete. For shared buffers represented with
  1638. * a &struct dma_buf this is tracked in &reservation_object structures.
  1639. * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org),
  1640. * whereas explicit fencing is what Android wants.
  1641. *
  1642. * "IN_FENCE_FD”:
  1643. * Use this property to pass a fence that DRM should wait on before
  1644. * proceeding with the Atomic Commit request and show the framebuffer for
  1645. * the plane on the screen. The fence can be either a normal fence or a
  1646. * merged one, the sync_file framework will handle both cases and use a
  1647. * fence_array if a merged fence is received. Passing -1 here means no
  1648. * fences to wait on.
  1649. *
  1650. * If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
  1651. * it will only check if the Sync File is a valid one.
  1652. *
  1653. * On the driver side the fence is stored on the @fence parameter of
  1654. * &struct drm_plane_state. Drivers which also support implicit fencing
  1655. * should set the implicit fence using drm_atomic_set_fence_for_plane(),
  1656. * to make sure there's consistent behaviour between drivers in precedence
  1657. * of implicit vs. explicit fencing.
  1658. *
  1659. * "OUT_FENCE_PTR”:
  1660. * Use this property to pass a file descriptor pointer to DRM. Once the
  1661. * Atomic Commit request call returns OUT_FENCE_PTR will be filled with
  1662. * the file descriptor number of a Sync File. This Sync File contains the
  1663. * CRTC fence that will be signaled when all framebuffers present on the
  1664. * Atomic Commit * request for that given CRTC are scanned out on the
  1665. * screen.
  1666. *
  1667. * The Atomic Commit request fails if a invalid pointer is passed. If the
  1668. * Atomic Commit request fails for any other reason the out fence fd
  1669. * returned will be -1. On a Atomic Commit with the
  1670. * DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
  1671. *
  1672. * Note that out-fences don't have a special interface to drivers and are
  1673. * internally represented by a &struct drm_pending_vblank_event in struct
  1674. * &drm_crtc_state, which is also used by the nonblocking atomic commit
  1675. * helpers and for the DRM event handling for existing userspace.
  1676. */
  1677. struct drm_out_fence_state {
  1678. s64 __user *out_fence_ptr;
  1679. struct sync_file *sync_file;
  1680. int fd;
  1681. };
  1682. static int setup_out_fence(struct drm_out_fence_state *fence_state,
  1683. struct dma_fence *fence)
  1684. {
  1685. fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
  1686. if (fence_state->fd < 0)
  1687. return fence_state->fd;
  1688. if (put_user(fence_state->fd, fence_state->out_fence_ptr))
  1689. return -EFAULT;
  1690. fence_state->sync_file = sync_file_create(fence);
  1691. if (!fence_state->sync_file)
  1692. return -ENOMEM;
  1693. return 0;
  1694. }
  1695. static int prepare_crtc_signaling(struct drm_device *dev,
  1696. struct drm_atomic_state *state,
  1697. struct drm_mode_atomic *arg,
  1698. struct drm_file *file_priv,
  1699. struct drm_out_fence_state **fence_state,
  1700. unsigned int *num_fences)
  1701. {
  1702. struct drm_crtc *crtc;
  1703. struct drm_crtc_state *crtc_state;
  1704. int i, ret;
  1705. if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
  1706. return 0;
  1707. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  1708. u64 __user *fence_ptr;
  1709. fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
  1710. if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
  1711. struct drm_pending_vblank_event *e;
  1712. e = create_vblank_event(dev, arg->user_data);
  1713. if (!e)
  1714. return -ENOMEM;
  1715. crtc_state->event = e;
  1716. }
  1717. if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  1718. struct drm_pending_vblank_event *e = crtc_state->event;
  1719. if (!file_priv)
  1720. continue;
  1721. ret = drm_event_reserve_init(dev, file_priv, &e->base,
  1722. &e->event.base);
  1723. if (ret) {
  1724. kfree(e);
  1725. crtc_state->event = NULL;
  1726. return ret;
  1727. }
  1728. }
  1729. if (fence_ptr) {
  1730. struct dma_fence *fence;
  1731. struct drm_out_fence_state *f;
  1732. f = krealloc(*fence_state, sizeof(**fence_state) *
  1733. (*num_fences + 1), GFP_KERNEL);
  1734. if (!f)
  1735. return -ENOMEM;
  1736. memset(&f[*num_fences], 0, sizeof(*f));
  1737. f[*num_fences].out_fence_ptr = fence_ptr;
  1738. *fence_state = f;
  1739. fence = drm_crtc_create_fence(crtc);
  1740. if (!fence)
  1741. return -ENOMEM;
  1742. ret = setup_out_fence(&f[(*num_fences)++], fence);
  1743. if (ret) {
  1744. dma_fence_put(fence);
  1745. return ret;
  1746. }
  1747. crtc_state->event->base.fence = fence;
  1748. }
  1749. }
  1750. return 0;
  1751. }
  1752. static void complete_crtc_signaling(struct drm_device *dev,
  1753. struct drm_atomic_state *state,
  1754. struct drm_out_fence_state *fence_state,
  1755. unsigned int num_fences,
  1756. bool install_fds)
  1757. {
  1758. struct drm_crtc *crtc;
  1759. struct drm_crtc_state *crtc_state;
  1760. int i;
  1761. if (install_fds) {
  1762. for (i = 0; i < num_fences; i++)
  1763. fd_install(fence_state[i].fd,
  1764. fence_state[i].sync_file->file);
  1765. kfree(fence_state);
  1766. return;
  1767. }
  1768. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  1769. /*
  1770. * TEST_ONLY and PAGE_FLIP_EVENT are mutually
  1771. * exclusive, if they weren't, this code should be
  1772. * called on success for TEST_ONLY too.
  1773. */
  1774. if (crtc_state->event)
  1775. drm_event_cancel_free(dev, &crtc_state->event->base);
  1776. }
  1777. if (!fence_state)
  1778. return;
  1779. for (i = 0; i < num_fences; i++) {
  1780. if (fence_state[i].sync_file)
  1781. fput(fence_state[i].sync_file->file);
  1782. if (fence_state[i].fd >= 0)
  1783. put_unused_fd(fence_state[i].fd);
  1784. /* If this fails log error to the user */
  1785. if (fence_state[i].out_fence_ptr &&
  1786. put_user(-1, fence_state[i].out_fence_ptr))
  1787. DRM_DEBUG_ATOMIC("Couldn't clear out_fence_ptr\n");
  1788. }
  1789. kfree(fence_state);
  1790. }
  1791. int drm_mode_atomic_ioctl(struct drm_device *dev,
  1792. void *data, struct drm_file *file_priv)
  1793. {
  1794. struct drm_mode_atomic *arg = data;
  1795. uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
  1796. uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
  1797. uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
  1798. uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
  1799. unsigned int copied_objs, copied_props;
  1800. struct drm_atomic_state *state;
  1801. struct drm_modeset_acquire_ctx ctx;
  1802. struct drm_plane *plane;
  1803. struct drm_out_fence_state *fence_state = NULL;
  1804. unsigned plane_mask;
  1805. int ret = 0;
  1806. unsigned int i, j, num_fences = 0;
  1807. /* disallow for drivers not supporting atomic: */
  1808. if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
  1809. return -EINVAL;
  1810. /* disallow for userspace that has not enabled atomic cap (even
  1811. * though this may be a bit overkill, since legacy userspace
  1812. * wouldn't know how to call this ioctl)
  1813. */
  1814. if (!file_priv->atomic)
  1815. return -EINVAL;
  1816. if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
  1817. return -EINVAL;
  1818. if (arg->reserved)
  1819. return -EINVAL;
  1820. if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
  1821. !dev->mode_config.async_page_flip)
  1822. return -EINVAL;
  1823. /* can't test and expect an event at the same time. */
  1824. if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
  1825. (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
  1826. return -EINVAL;
  1827. drm_modeset_acquire_init(&ctx, 0);
  1828. state = drm_atomic_state_alloc(dev);
  1829. if (!state)
  1830. return -ENOMEM;
  1831. state->acquire_ctx = &ctx;
  1832. state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
  1833. retry:
  1834. plane_mask = 0;
  1835. copied_objs = 0;
  1836. copied_props = 0;
  1837. for (i = 0; i < arg->count_objs; i++) {
  1838. uint32_t obj_id, count_props;
  1839. struct drm_mode_object *obj;
  1840. if (get_user(obj_id, objs_ptr + copied_objs)) {
  1841. ret = -EFAULT;
  1842. goto out;
  1843. }
  1844. obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
  1845. if (!obj) {
  1846. ret = -ENOENT;
  1847. goto out;
  1848. }
  1849. if (!obj->properties) {
  1850. drm_mode_object_unreference(obj);
  1851. ret = -ENOENT;
  1852. goto out;
  1853. }
  1854. if (get_user(count_props, count_props_ptr + copied_objs)) {
  1855. drm_mode_object_unreference(obj);
  1856. ret = -EFAULT;
  1857. goto out;
  1858. }
  1859. copied_objs++;
  1860. for (j = 0; j < count_props; j++) {
  1861. uint32_t prop_id;
  1862. uint64_t prop_value;
  1863. struct drm_property *prop;
  1864. if (get_user(prop_id, props_ptr + copied_props)) {
  1865. drm_mode_object_unreference(obj);
  1866. ret = -EFAULT;
  1867. goto out;
  1868. }
  1869. prop = drm_mode_obj_find_prop_id(obj, prop_id);
  1870. if (!prop) {
  1871. drm_mode_object_unreference(obj);
  1872. ret = -ENOENT;
  1873. goto out;
  1874. }
  1875. if (copy_from_user(&prop_value,
  1876. prop_values_ptr + copied_props,
  1877. sizeof(prop_value))) {
  1878. drm_mode_object_unreference(obj);
  1879. ret = -EFAULT;
  1880. goto out;
  1881. }
  1882. ret = atomic_set_prop(state, obj, prop, prop_value);
  1883. if (ret) {
  1884. drm_mode_object_unreference(obj);
  1885. goto out;
  1886. }
  1887. copied_props++;
  1888. }
  1889. if (obj->type == DRM_MODE_OBJECT_PLANE && count_props &&
  1890. !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
  1891. plane = obj_to_plane(obj);
  1892. plane_mask |= (1 << drm_plane_index(plane));
  1893. plane->old_fb = plane->fb;
  1894. }
  1895. drm_mode_object_unreference(obj);
  1896. }
  1897. ret = prepare_crtc_signaling(dev, state, arg, file_priv, &fence_state,
  1898. &num_fences);
  1899. if (ret)
  1900. goto out;
  1901. if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
  1902. ret = drm_atomic_check_only(state);
  1903. } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
  1904. ret = drm_atomic_nonblocking_commit(state);
  1905. } else {
  1906. if (unlikely(drm_debug & DRM_UT_STATE))
  1907. drm_atomic_print_state(state);
  1908. ret = drm_atomic_commit(state);
  1909. }
  1910. out:
  1911. drm_atomic_clean_old_fb(dev, plane_mask, ret);
  1912. complete_crtc_signaling(dev, state, fence_state, num_fences, !ret);
  1913. if (ret == -EDEADLK) {
  1914. drm_atomic_state_clear(state);
  1915. drm_modeset_backoff(&ctx);
  1916. goto retry;
  1917. }
  1918. drm_atomic_state_put(state);
  1919. drm_modeset_drop_locks(&ctx);
  1920. drm_modeset_acquire_fini(&ctx);
  1921. return ret;
  1922. }