drm_atomic.c 81 KB

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