drm_atomic.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359
  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, NULL, 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, NULL, 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, NULL, 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_crtc *crtc, 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.vbl.crtc_id = crtc->base.id;
  1599. e->event.vbl.user_data = user_data;
  1600. return e;
  1601. }
  1602. int drm_atomic_connector_commit_dpms(struct drm_atomic_state *state,
  1603. struct drm_connector *connector,
  1604. int mode)
  1605. {
  1606. struct drm_connector *tmp_connector;
  1607. struct drm_connector_state *new_conn_state;
  1608. struct drm_crtc *crtc;
  1609. struct drm_crtc_state *crtc_state;
  1610. int i, ret, old_mode = connector->dpms;
  1611. bool active = false;
  1612. ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
  1613. state->acquire_ctx);
  1614. if (ret)
  1615. return ret;
  1616. if (mode != DRM_MODE_DPMS_ON)
  1617. mode = DRM_MODE_DPMS_OFF;
  1618. connector->dpms = mode;
  1619. crtc = connector->state->crtc;
  1620. if (!crtc)
  1621. goto out;
  1622. ret = drm_atomic_add_affected_connectors(state, crtc);
  1623. if (ret)
  1624. goto out;
  1625. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  1626. if (IS_ERR(crtc_state)) {
  1627. ret = PTR_ERR(crtc_state);
  1628. goto out;
  1629. }
  1630. for_each_new_connector_in_state(state, tmp_connector, new_conn_state, i) {
  1631. if (new_conn_state->crtc != crtc)
  1632. continue;
  1633. if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
  1634. active = true;
  1635. break;
  1636. }
  1637. }
  1638. crtc_state->active = active;
  1639. ret = drm_atomic_commit(state);
  1640. out:
  1641. if (ret != 0)
  1642. connector->dpms = old_mode;
  1643. return ret;
  1644. }
  1645. int drm_atomic_set_property(struct drm_atomic_state *state,
  1646. struct drm_mode_object *obj,
  1647. struct drm_property *prop,
  1648. uint64_t prop_value)
  1649. {
  1650. struct drm_mode_object *ref;
  1651. int ret;
  1652. if (!drm_property_change_valid_get(prop, prop_value, &ref))
  1653. return -EINVAL;
  1654. switch (obj->type) {
  1655. case DRM_MODE_OBJECT_CONNECTOR: {
  1656. struct drm_connector *connector = obj_to_connector(obj);
  1657. struct drm_connector_state *connector_state;
  1658. connector_state = drm_atomic_get_connector_state(state, connector);
  1659. if (IS_ERR(connector_state)) {
  1660. ret = PTR_ERR(connector_state);
  1661. break;
  1662. }
  1663. ret = drm_atomic_connector_set_property(connector,
  1664. connector_state, prop, prop_value);
  1665. break;
  1666. }
  1667. case DRM_MODE_OBJECT_CRTC: {
  1668. struct drm_crtc *crtc = obj_to_crtc(obj);
  1669. struct drm_crtc_state *crtc_state;
  1670. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  1671. if (IS_ERR(crtc_state)) {
  1672. ret = PTR_ERR(crtc_state);
  1673. break;
  1674. }
  1675. ret = drm_atomic_crtc_set_property(crtc,
  1676. crtc_state, prop, prop_value);
  1677. break;
  1678. }
  1679. case DRM_MODE_OBJECT_PLANE: {
  1680. struct drm_plane *plane = obj_to_plane(obj);
  1681. struct drm_plane_state *plane_state;
  1682. plane_state = drm_atomic_get_plane_state(state, plane);
  1683. if (IS_ERR(plane_state)) {
  1684. ret = PTR_ERR(plane_state);
  1685. break;
  1686. }
  1687. ret = drm_atomic_plane_set_property(plane,
  1688. plane_state, prop, prop_value);
  1689. break;
  1690. }
  1691. default:
  1692. ret = -EINVAL;
  1693. break;
  1694. }
  1695. drm_property_change_valid_put(prop, ref);
  1696. return ret;
  1697. }
  1698. /**
  1699. * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers.
  1700. *
  1701. * @dev: drm device to check.
  1702. * @plane_mask: plane mask for planes that were updated.
  1703. * @ret: return value, can be -EDEADLK for a retry.
  1704. *
  1705. * Before doing an update &drm_plane.old_fb is set to &drm_plane.fb, but before
  1706. * dropping the locks old_fb needs to be set to NULL and plane->fb updated. This
  1707. * is a common operation for each atomic update, so this call is split off as a
  1708. * helper.
  1709. */
  1710. void drm_atomic_clean_old_fb(struct drm_device *dev,
  1711. unsigned plane_mask,
  1712. int ret)
  1713. {
  1714. struct drm_plane *plane;
  1715. /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
  1716. * locks (ie. while it is still safe to deref plane->state). We
  1717. * need to do this here because the driver entry points cannot
  1718. * distinguish between legacy and atomic ioctls.
  1719. */
  1720. drm_for_each_plane_mask(plane, dev, plane_mask) {
  1721. if (ret == 0) {
  1722. struct drm_framebuffer *new_fb = plane->state->fb;
  1723. if (new_fb)
  1724. drm_framebuffer_get(new_fb);
  1725. plane->fb = new_fb;
  1726. plane->crtc = plane->state->crtc;
  1727. if (plane->old_fb)
  1728. drm_framebuffer_put(plane->old_fb);
  1729. }
  1730. plane->old_fb = NULL;
  1731. }
  1732. }
  1733. EXPORT_SYMBOL(drm_atomic_clean_old_fb);
  1734. /**
  1735. * DOC: explicit fencing properties
  1736. *
  1737. * Explicit fencing allows userspace to control the buffer synchronization
  1738. * between devices. A Fence or a group of fences are transfered to/from
  1739. * userspace using Sync File fds and there are two DRM properties for that.
  1740. * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
  1741. * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
  1742. *
  1743. * As a contrast, with implicit fencing the kernel keeps track of any
  1744. * ongoing rendering, and automatically ensures that the atomic update waits
  1745. * for any pending rendering to complete. For shared buffers represented with
  1746. * a &struct dma_buf this is tracked in &struct reservation_object.
  1747. * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org),
  1748. * whereas explicit fencing is what Android wants.
  1749. *
  1750. * "IN_FENCE_FD”:
  1751. * Use this property to pass a fence that DRM should wait on before
  1752. * proceeding with the Atomic Commit request and show the framebuffer for
  1753. * the plane on the screen. The fence can be either a normal fence or a
  1754. * merged one, the sync_file framework will handle both cases and use a
  1755. * fence_array if a merged fence is received. Passing -1 here means no
  1756. * fences to wait on.
  1757. *
  1758. * If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
  1759. * it will only check if the Sync File is a valid one.
  1760. *
  1761. * On the driver side the fence is stored on the @fence parameter of
  1762. * &struct drm_plane_state. Drivers which also support implicit fencing
  1763. * should set the implicit fence using drm_atomic_set_fence_for_plane(),
  1764. * to make sure there's consistent behaviour between drivers in precedence
  1765. * of implicit vs. explicit fencing.
  1766. *
  1767. * "OUT_FENCE_PTR”:
  1768. * Use this property to pass a file descriptor pointer to DRM. Once the
  1769. * Atomic Commit request call returns OUT_FENCE_PTR will be filled with
  1770. * the file descriptor number of a Sync File. This Sync File contains the
  1771. * CRTC fence that will be signaled when all framebuffers present on the
  1772. * Atomic Commit * request for that given CRTC are scanned out on the
  1773. * screen.
  1774. *
  1775. * The Atomic Commit request fails if a invalid pointer is passed. If the
  1776. * Atomic Commit request fails for any other reason the out fence fd
  1777. * returned will be -1. On a Atomic Commit with the
  1778. * DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
  1779. *
  1780. * Note that out-fences don't have a special interface to drivers and are
  1781. * internally represented by a &struct drm_pending_vblank_event in struct
  1782. * &drm_crtc_state, which is also used by the nonblocking atomic commit
  1783. * helpers and for the DRM event handling for existing userspace.
  1784. */
  1785. struct drm_out_fence_state {
  1786. s32 __user *out_fence_ptr;
  1787. struct sync_file *sync_file;
  1788. int fd;
  1789. };
  1790. static int setup_out_fence(struct drm_out_fence_state *fence_state,
  1791. struct dma_fence *fence)
  1792. {
  1793. fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
  1794. if (fence_state->fd < 0)
  1795. return fence_state->fd;
  1796. if (put_user(fence_state->fd, fence_state->out_fence_ptr))
  1797. return -EFAULT;
  1798. fence_state->sync_file = sync_file_create(fence);
  1799. if (!fence_state->sync_file)
  1800. return -ENOMEM;
  1801. return 0;
  1802. }
  1803. static int prepare_crtc_signaling(struct drm_device *dev,
  1804. struct drm_atomic_state *state,
  1805. struct drm_mode_atomic *arg,
  1806. struct drm_file *file_priv,
  1807. struct drm_out_fence_state **fence_state,
  1808. unsigned int *num_fences)
  1809. {
  1810. struct drm_crtc *crtc;
  1811. struct drm_crtc_state *crtc_state;
  1812. int i, c = 0, ret;
  1813. if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
  1814. return 0;
  1815. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  1816. s32 __user *fence_ptr;
  1817. fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
  1818. if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
  1819. struct drm_pending_vblank_event *e;
  1820. e = create_vblank_event(crtc, arg->user_data);
  1821. if (!e)
  1822. return -ENOMEM;
  1823. crtc_state->event = e;
  1824. }
  1825. if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  1826. struct drm_pending_vblank_event *e = crtc_state->event;
  1827. if (!file_priv)
  1828. continue;
  1829. ret = drm_event_reserve_init(dev, file_priv, &e->base,
  1830. &e->event.base);
  1831. if (ret) {
  1832. kfree(e);
  1833. crtc_state->event = NULL;
  1834. return ret;
  1835. }
  1836. }
  1837. if (fence_ptr) {
  1838. struct dma_fence *fence;
  1839. struct drm_out_fence_state *f;
  1840. f = krealloc(*fence_state, sizeof(**fence_state) *
  1841. (*num_fences + 1), GFP_KERNEL);
  1842. if (!f)
  1843. return -ENOMEM;
  1844. memset(&f[*num_fences], 0, sizeof(*f));
  1845. f[*num_fences].out_fence_ptr = fence_ptr;
  1846. *fence_state = f;
  1847. fence = drm_crtc_create_fence(crtc);
  1848. if (!fence)
  1849. return -ENOMEM;
  1850. ret = setup_out_fence(&f[(*num_fences)++], fence);
  1851. if (ret) {
  1852. dma_fence_put(fence);
  1853. return ret;
  1854. }
  1855. crtc_state->event->base.fence = fence;
  1856. }
  1857. c++;
  1858. }
  1859. /*
  1860. * Having this flag means user mode pends on event which will never
  1861. * reach due to lack of at least one CRTC for signaling
  1862. */
  1863. if (c == 0 && (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
  1864. return -EINVAL;
  1865. return 0;
  1866. }
  1867. static void complete_crtc_signaling(struct drm_device *dev,
  1868. struct drm_atomic_state *state,
  1869. struct drm_out_fence_state *fence_state,
  1870. unsigned int num_fences,
  1871. bool install_fds)
  1872. {
  1873. struct drm_crtc *crtc;
  1874. struct drm_crtc_state *crtc_state;
  1875. int i;
  1876. if (install_fds) {
  1877. for (i = 0; i < num_fences; i++)
  1878. fd_install(fence_state[i].fd,
  1879. fence_state[i].sync_file->file);
  1880. kfree(fence_state);
  1881. return;
  1882. }
  1883. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  1884. struct drm_pending_vblank_event *event = crtc_state->event;
  1885. /*
  1886. * Free the allocated event. drm_atomic_helper_setup_commit
  1887. * can allocate an event too, so only free it if it's ours
  1888. * to prevent a double free in drm_atomic_state_clear.
  1889. */
  1890. if (event && (event->base.fence || event->base.file_priv)) {
  1891. drm_event_cancel_free(dev, &event->base);
  1892. crtc_state->event = NULL;
  1893. }
  1894. }
  1895. if (!fence_state)
  1896. return;
  1897. for (i = 0; i < num_fences; i++) {
  1898. if (fence_state[i].sync_file)
  1899. fput(fence_state[i].sync_file->file);
  1900. if (fence_state[i].fd >= 0)
  1901. put_unused_fd(fence_state[i].fd);
  1902. /* If this fails log error to the user */
  1903. if (fence_state[i].out_fence_ptr &&
  1904. put_user(-1, fence_state[i].out_fence_ptr))
  1905. DRM_DEBUG_ATOMIC("Couldn't clear out_fence_ptr\n");
  1906. }
  1907. kfree(fence_state);
  1908. }
  1909. int drm_mode_atomic_ioctl(struct drm_device *dev,
  1910. void *data, struct drm_file *file_priv)
  1911. {
  1912. struct drm_mode_atomic *arg = data;
  1913. uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
  1914. uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
  1915. uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
  1916. uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
  1917. unsigned int copied_objs, copied_props;
  1918. struct drm_atomic_state *state;
  1919. struct drm_modeset_acquire_ctx ctx;
  1920. struct drm_plane *plane;
  1921. struct drm_out_fence_state *fence_state;
  1922. unsigned plane_mask;
  1923. int ret = 0;
  1924. unsigned int i, j, num_fences;
  1925. /* disallow for drivers not supporting atomic: */
  1926. if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
  1927. return -EINVAL;
  1928. /* disallow for userspace that has not enabled atomic cap (even
  1929. * though this may be a bit overkill, since legacy userspace
  1930. * wouldn't know how to call this ioctl)
  1931. */
  1932. if (!file_priv->atomic)
  1933. return -EINVAL;
  1934. if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
  1935. return -EINVAL;
  1936. if (arg->reserved)
  1937. return -EINVAL;
  1938. if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
  1939. !dev->mode_config.async_page_flip)
  1940. return -EINVAL;
  1941. /* can't test and expect an event at the same time. */
  1942. if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
  1943. (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
  1944. return -EINVAL;
  1945. drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
  1946. state = drm_atomic_state_alloc(dev);
  1947. if (!state)
  1948. return -ENOMEM;
  1949. state->acquire_ctx = &ctx;
  1950. state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
  1951. retry:
  1952. plane_mask = 0;
  1953. copied_objs = 0;
  1954. copied_props = 0;
  1955. fence_state = NULL;
  1956. num_fences = 0;
  1957. for (i = 0; i < arg->count_objs; i++) {
  1958. uint32_t obj_id, count_props;
  1959. struct drm_mode_object *obj;
  1960. if (get_user(obj_id, objs_ptr + copied_objs)) {
  1961. ret = -EFAULT;
  1962. goto out;
  1963. }
  1964. obj = drm_mode_object_find(dev, file_priv, obj_id, DRM_MODE_OBJECT_ANY);
  1965. if (!obj) {
  1966. ret = -ENOENT;
  1967. goto out;
  1968. }
  1969. if (!obj->properties) {
  1970. drm_mode_object_put(obj);
  1971. ret = -ENOENT;
  1972. goto out;
  1973. }
  1974. if (get_user(count_props, count_props_ptr + copied_objs)) {
  1975. drm_mode_object_put(obj);
  1976. ret = -EFAULT;
  1977. goto out;
  1978. }
  1979. copied_objs++;
  1980. for (j = 0; j < count_props; j++) {
  1981. uint32_t prop_id;
  1982. uint64_t prop_value;
  1983. struct drm_property *prop;
  1984. if (get_user(prop_id, props_ptr + copied_props)) {
  1985. drm_mode_object_put(obj);
  1986. ret = -EFAULT;
  1987. goto out;
  1988. }
  1989. prop = drm_mode_obj_find_prop_id(obj, prop_id);
  1990. if (!prop) {
  1991. drm_mode_object_put(obj);
  1992. ret = -ENOENT;
  1993. goto out;
  1994. }
  1995. if (copy_from_user(&prop_value,
  1996. prop_values_ptr + copied_props,
  1997. sizeof(prop_value))) {
  1998. drm_mode_object_put(obj);
  1999. ret = -EFAULT;
  2000. goto out;
  2001. }
  2002. ret = drm_atomic_set_property(state, obj, prop,
  2003. prop_value);
  2004. if (ret) {
  2005. drm_mode_object_put(obj);
  2006. goto out;
  2007. }
  2008. copied_props++;
  2009. }
  2010. if (obj->type == DRM_MODE_OBJECT_PLANE && count_props &&
  2011. !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
  2012. plane = obj_to_plane(obj);
  2013. plane_mask |= (1 << drm_plane_index(plane));
  2014. plane->old_fb = plane->fb;
  2015. }
  2016. drm_mode_object_put(obj);
  2017. }
  2018. ret = prepare_crtc_signaling(dev, state, arg, file_priv, &fence_state,
  2019. &num_fences);
  2020. if (ret)
  2021. goto out;
  2022. if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
  2023. ret = drm_atomic_check_only(state);
  2024. } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
  2025. ret = drm_atomic_nonblocking_commit(state);
  2026. } else {
  2027. if (unlikely(drm_debug & DRM_UT_STATE))
  2028. drm_atomic_print_state(state);
  2029. ret = drm_atomic_commit(state);
  2030. }
  2031. out:
  2032. drm_atomic_clean_old_fb(dev, plane_mask, ret);
  2033. complete_crtc_signaling(dev, state, fence_state, num_fences, !ret);
  2034. if (ret == -EDEADLK) {
  2035. drm_atomic_state_clear(state);
  2036. ret = drm_modeset_backoff(&ctx);
  2037. if (!ret)
  2038. goto retry;
  2039. }
  2040. drm_atomic_state_put(state);
  2041. drm_modeset_drop_locks(&ctx);
  2042. drm_modeset_acquire_fini(&ctx);
  2043. return ret;
  2044. }