drm_atomic.c 65 KB

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