drm_atomic.c 74 KB

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