drm_atomic.c 69 KB

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