drm_atomic.c 73 KB

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