drm_atomic.c 65 KB

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