drm_atomic.c 69 KB

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