drm_atomic.c 69 KB

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