drm_atomic.c 71 KB

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