drm_atomic.c 75 KB

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