drm_atomic_helper.c 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020
  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_plane_helper.h>
  30. #include <drm/drm_crtc_helper.h>
  31. #include <drm/drm_atomic_helper.h>
  32. #include <linux/fence.h>
  33. /**
  34. * DOC: overview
  35. *
  36. * This helper library provides implementations of check and commit functions on
  37. * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
  38. * also provides convenience implementations for the atomic state handling
  39. * callbacks for drivers which don't need to subclass the drm core structures to
  40. * add their own additional internal state.
  41. *
  42. * This library also provides default implementations for the check callback in
  43. * drm_atomic_helper_check() and for the commit callback with
  44. * drm_atomic_helper_commit(). But the individual stages and callbacks are
  45. * exposed to allow drivers to mix and match and e.g. use the plane helpers only
  46. * together with a driver private modeset implementation.
  47. *
  48. * This library also provides implementations for all the legacy driver
  49. * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
  50. * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
  51. * various functions to implement set_property callbacks. New drivers must not
  52. * implement these functions themselves but must use the provided helpers.
  53. *
  54. * The atomic helper uses the same function table structures as all other
  55. * modesetting helpers. See the documentation for struct &drm_crtc_helper_funcs,
  56. * struct &drm_encoder_helper_funcs and struct &drm_connector_helper_funcs. It
  57. * also shares the struct &drm_plane_helper_funcs function table with the plane
  58. * helpers.
  59. */
  60. static void
  61. drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
  62. struct drm_plane_state *plane_state,
  63. struct drm_plane *plane)
  64. {
  65. struct drm_crtc_state *crtc_state;
  66. if (plane->state->crtc) {
  67. crtc_state = drm_atomic_get_existing_crtc_state(state,
  68. plane->state->crtc);
  69. if (WARN_ON(!crtc_state))
  70. return;
  71. crtc_state->planes_changed = true;
  72. }
  73. if (plane_state->crtc) {
  74. crtc_state = drm_atomic_get_existing_crtc_state(state,
  75. plane_state->crtc);
  76. if (WARN_ON(!crtc_state))
  77. return;
  78. crtc_state->planes_changed = true;
  79. }
  80. }
  81. static int handle_conflicting_encoders(struct drm_atomic_state *state,
  82. bool disable_conflicting_encoders)
  83. {
  84. struct drm_connector_state *conn_state;
  85. struct drm_connector *connector;
  86. struct drm_encoder *encoder;
  87. unsigned encoder_mask = 0;
  88. int i, ret;
  89. /*
  90. * First loop, find all newly assigned encoders from the connectors
  91. * part of the state. If the same encoder is assigned to multiple
  92. * connectors bail out.
  93. */
  94. for_each_connector_in_state(state, connector, conn_state, i) {
  95. const struct drm_connector_helper_funcs *funcs = connector->helper_private;
  96. struct drm_encoder *new_encoder;
  97. if (!conn_state->crtc)
  98. continue;
  99. if (funcs->atomic_best_encoder)
  100. new_encoder = funcs->atomic_best_encoder(connector, conn_state);
  101. else
  102. new_encoder = funcs->best_encoder(connector);
  103. if (new_encoder) {
  104. if (encoder_mask & (1 << drm_encoder_index(new_encoder))) {
  105. DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] on [CONNECTOR:%d:%s] already assigned\n",
  106. new_encoder->base.id, new_encoder->name,
  107. connector->base.id, connector->name);
  108. return -EINVAL;
  109. }
  110. encoder_mask |= 1 << drm_encoder_index(new_encoder);
  111. }
  112. }
  113. if (!encoder_mask)
  114. return 0;
  115. /*
  116. * Second loop, iterate over all connectors not part of the state.
  117. *
  118. * If a conflicting encoder is found and disable_conflicting_encoders
  119. * is not set, an error is returned. Userspace can provide a solution
  120. * through the atomic ioctl.
  121. *
  122. * If the flag is set conflicting connectors are removed from the crtc
  123. * and the crtc is disabled if no encoder is left. This preserves
  124. * compatibility with the legacy set_config behavior.
  125. */
  126. drm_for_each_connector(connector, state->dev) {
  127. struct drm_crtc_state *crtc_state;
  128. if (drm_atomic_get_existing_connector_state(state, connector))
  129. continue;
  130. encoder = connector->state->best_encoder;
  131. if (!encoder || !(encoder_mask & (1 << drm_encoder_index(encoder))))
  132. continue;
  133. if (!disable_conflicting_encoders) {
  134. DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s] by [CONNECTOR:%d:%s]\n",
  135. encoder->base.id, encoder->name,
  136. connector->state->crtc->base.id,
  137. connector->state->crtc->name,
  138. connector->base.id, connector->name);
  139. return -EINVAL;
  140. }
  141. conn_state = drm_atomic_get_connector_state(state, connector);
  142. if (IS_ERR(conn_state))
  143. return PTR_ERR(conn_state);
  144. DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], disabling [CONNECTOR:%d:%s]\n",
  145. encoder->base.id, encoder->name,
  146. conn_state->crtc->base.id, conn_state->crtc->name,
  147. connector->base.id, connector->name);
  148. crtc_state = drm_atomic_get_existing_crtc_state(state, conn_state->crtc);
  149. ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
  150. if (ret)
  151. return ret;
  152. if (!crtc_state->connector_mask) {
  153. ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
  154. NULL);
  155. if (ret < 0)
  156. return ret;
  157. crtc_state->active = false;
  158. }
  159. }
  160. return 0;
  161. }
  162. static void
  163. set_best_encoder(struct drm_atomic_state *state,
  164. struct drm_connector_state *conn_state,
  165. struct drm_encoder *encoder)
  166. {
  167. struct drm_crtc_state *crtc_state;
  168. struct drm_crtc *crtc;
  169. if (conn_state->best_encoder) {
  170. /* Unset the encoder_mask in the old crtc state. */
  171. crtc = conn_state->connector->state->crtc;
  172. /* A NULL crtc is an error here because we should have
  173. * duplicated a NULL best_encoder when crtc was NULL.
  174. * As an exception restoring duplicated atomic state
  175. * during resume is allowed, so don't warn when
  176. * best_encoder is equal to encoder we intend to set.
  177. */
  178. WARN_ON(!crtc && encoder != conn_state->best_encoder);
  179. if (crtc) {
  180. crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
  181. crtc_state->encoder_mask &=
  182. ~(1 << drm_encoder_index(conn_state->best_encoder));
  183. }
  184. }
  185. if (encoder) {
  186. crtc = conn_state->crtc;
  187. WARN_ON(!crtc);
  188. if (crtc) {
  189. crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
  190. crtc_state->encoder_mask |=
  191. 1 << drm_encoder_index(encoder);
  192. }
  193. }
  194. conn_state->best_encoder = encoder;
  195. }
  196. static void
  197. steal_encoder(struct drm_atomic_state *state,
  198. struct drm_encoder *encoder)
  199. {
  200. struct drm_crtc_state *crtc_state;
  201. struct drm_connector *connector;
  202. struct drm_connector_state *connector_state;
  203. int i;
  204. for_each_connector_in_state(state, connector, connector_state, i) {
  205. struct drm_crtc *encoder_crtc;
  206. if (connector_state->best_encoder != encoder)
  207. continue;
  208. encoder_crtc = connector->state->crtc;
  209. DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
  210. encoder->base.id, encoder->name,
  211. encoder_crtc->base.id, encoder_crtc->name);
  212. set_best_encoder(state, connector_state, NULL);
  213. crtc_state = drm_atomic_get_existing_crtc_state(state, encoder_crtc);
  214. crtc_state->connectors_changed = true;
  215. return;
  216. }
  217. }
  218. static int
  219. update_connector_routing(struct drm_atomic_state *state,
  220. struct drm_connector *connector,
  221. struct drm_connector_state *connector_state)
  222. {
  223. const struct drm_connector_helper_funcs *funcs;
  224. struct drm_encoder *new_encoder;
  225. struct drm_crtc_state *crtc_state;
  226. DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
  227. connector->base.id,
  228. connector->name);
  229. if (connector->state->crtc != connector_state->crtc) {
  230. if (connector->state->crtc) {
  231. crtc_state = drm_atomic_get_existing_crtc_state(state, connector->state->crtc);
  232. crtc_state->connectors_changed = true;
  233. }
  234. if (connector_state->crtc) {
  235. crtc_state = drm_atomic_get_existing_crtc_state(state, connector_state->crtc);
  236. crtc_state->connectors_changed = true;
  237. }
  238. }
  239. if (!connector_state->crtc) {
  240. DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
  241. connector->base.id,
  242. connector->name);
  243. set_best_encoder(state, connector_state, NULL);
  244. return 0;
  245. }
  246. funcs = connector->helper_private;
  247. if (funcs->atomic_best_encoder)
  248. new_encoder = funcs->atomic_best_encoder(connector,
  249. connector_state);
  250. else
  251. new_encoder = funcs->best_encoder(connector);
  252. if (!new_encoder) {
  253. DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
  254. connector->base.id,
  255. connector->name);
  256. return -EINVAL;
  257. }
  258. if (!drm_encoder_crtc_ok(new_encoder, connector_state->crtc)) {
  259. DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d]\n",
  260. new_encoder->base.id,
  261. new_encoder->name,
  262. connector_state->crtc->base.id);
  263. return -EINVAL;
  264. }
  265. if (new_encoder == connector_state->best_encoder) {
  266. set_best_encoder(state, connector_state, new_encoder);
  267. DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
  268. connector->base.id,
  269. connector->name,
  270. new_encoder->base.id,
  271. new_encoder->name,
  272. connector_state->crtc->base.id,
  273. connector_state->crtc->name);
  274. return 0;
  275. }
  276. steal_encoder(state, new_encoder);
  277. set_best_encoder(state, connector_state, new_encoder);
  278. crtc_state = drm_atomic_get_existing_crtc_state(state, connector_state->crtc);
  279. crtc_state->connectors_changed = true;
  280. DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
  281. connector->base.id,
  282. connector->name,
  283. new_encoder->base.id,
  284. new_encoder->name,
  285. connector_state->crtc->base.id,
  286. connector_state->crtc->name);
  287. return 0;
  288. }
  289. static int
  290. mode_fixup(struct drm_atomic_state *state)
  291. {
  292. struct drm_crtc *crtc;
  293. struct drm_crtc_state *crtc_state;
  294. struct drm_connector *connector;
  295. struct drm_connector_state *conn_state;
  296. int i;
  297. bool ret;
  298. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  299. if (!crtc_state->mode_changed &&
  300. !crtc_state->connectors_changed)
  301. continue;
  302. drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
  303. }
  304. for_each_connector_in_state(state, connector, conn_state, i) {
  305. const struct drm_encoder_helper_funcs *funcs;
  306. struct drm_encoder *encoder;
  307. WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
  308. if (!conn_state->crtc || !conn_state->best_encoder)
  309. continue;
  310. crtc_state = drm_atomic_get_existing_crtc_state(state,
  311. conn_state->crtc);
  312. /*
  313. * Each encoder has at most one connector (since we always steal
  314. * it away), so we won't call ->mode_fixup twice.
  315. */
  316. encoder = conn_state->best_encoder;
  317. funcs = encoder->helper_private;
  318. ret = drm_bridge_mode_fixup(encoder->bridge, &crtc_state->mode,
  319. &crtc_state->adjusted_mode);
  320. if (!ret) {
  321. DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
  322. return -EINVAL;
  323. }
  324. if (funcs && funcs->atomic_check) {
  325. ret = funcs->atomic_check(encoder, crtc_state,
  326. conn_state);
  327. if (ret) {
  328. DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
  329. encoder->base.id, encoder->name);
  330. return ret;
  331. }
  332. } else if (funcs && funcs->mode_fixup) {
  333. ret = funcs->mode_fixup(encoder, &crtc_state->mode,
  334. &crtc_state->adjusted_mode);
  335. if (!ret) {
  336. DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
  337. encoder->base.id, encoder->name);
  338. return -EINVAL;
  339. }
  340. }
  341. }
  342. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  343. const struct drm_crtc_helper_funcs *funcs;
  344. if (!crtc_state->mode_changed &&
  345. !crtc_state->connectors_changed)
  346. continue;
  347. funcs = crtc->helper_private;
  348. if (!funcs->mode_fixup)
  349. continue;
  350. ret = funcs->mode_fixup(crtc, &crtc_state->mode,
  351. &crtc_state->adjusted_mode);
  352. if (!ret) {
  353. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
  354. crtc->base.id, crtc->name);
  355. return -EINVAL;
  356. }
  357. }
  358. return 0;
  359. }
  360. /**
  361. * drm_atomic_helper_check_modeset - validate state object for modeset changes
  362. * @dev: DRM device
  363. * @state: the driver state object
  364. *
  365. * Check the state object to see if the requested state is physically possible.
  366. * This does all the crtc and connector related computations for an atomic
  367. * update and adds any additional connectors needed for full modesets and calls
  368. * down into ->mode_fixup functions of the driver backend.
  369. *
  370. * crtc_state->mode_changed is set when the input mode is changed.
  371. * crtc_state->connectors_changed is set when a connector is added or
  372. * removed from the crtc.
  373. * crtc_state->active_changed is set when crtc_state->active changes,
  374. * which is used for dpms.
  375. *
  376. * IMPORTANT:
  377. *
  378. * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
  379. * plane update can't be done without a full modeset) _must_ call this function
  380. * afterwards after that change. It is permitted to call this function multiple
  381. * times for the same update, e.g. when the ->atomic_check functions depend upon
  382. * the adjusted dotclock for fifo space allocation and watermark computation.
  383. *
  384. * RETURNS
  385. * Zero for success or -errno
  386. */
  387. int
  388. drm_atomic_helper_check_modeset(struct drm_device *dev,
  389. struct drm_atomic_state *state)
  390. {
  391. struct drm_crtc *crtc;
  392. struct drm_crtc_state *crtc_state;
  393. struct drm_connector *connector;
  394. struct drm_connector_state *connector_state;
  395. int i, ret;
  396. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  397. if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
  398. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
  399. crtc->base.id, crtc->name);
  400. crtc_state->mode_changed = true;
  401. }
  402. if (crtc->state->enable != crtc_state->enable) {
  403. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
  404. crtc->base.id, crtc->name);
  405. /*
  406. * For clarity this assignment is done here, but
  407. * enable == 0 is only true when there are no
  408. * connectors and a NULL mode.
  409. *
  410. * The other way around is true as well. enable != 0
  411. * iff connectors are attached and a mode is set.
  412. */
  413. crtc_state->mode_changed = true;
  414. crtc_state->connectors_changed = true;
  415. }
  416. }
  417. ret = handle_conflicting_encoders(state, state->legacy_set_config);
  418. if (ret)
  419. return ret;
  420. for_each_connector_in_state(state, connector, connector_state, i) {
  421. /*
  422. * This only sets crtc->mode_changed for routing changes,
  423. * drivers must set crtc->mode_changed themselves when connector
  424. * properties need to be updated.
  425. */
  426. ret = update_connector_routing(state, connector,
  427. connector_state);
  428. if (ret)
  429. return ret;
  430. }
  431. /*
  432. * After all the routing has been prepared we need to add in any
  433. * connector which is itself unchanged, but who's crtc changes it's
  434. * configuration. This must be done before calling mode_fixup in case a
  435. * crtc only changed its mode but has the same set of connectors.
  436. */
  437. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  438. bool has_connectors =
  439. !!crtc_state->connector_mask;
  440. /*
  441. * We must set ->active_changed after walking connectors for
  442. * otherwise an update that only changes active would result in
  443. * a full modeset because update_connector_routing force that.
  444. */
  445. if (crtc->state->active != crtc_state->active) {
  446. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
  447. crtc->base.id, crtc->name);
  448. crtc_state->active_changed = true;
  449. }
  450. if (!drm_atomic_crtc_needs_modeset(crtc_state))
  451. continue;
  452. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
  453. crtc->base.id, crtc->name,
  454. crtc_state->enable ? 'y' : 'n',
  455. crtc_state->active ? 'y' : 'n');
  456. ret = drm_atomic_add_affected_connectors(state, crtc);
  457. if (ret != 0)
  458. return ret;
  459. ret = drm_atomic_add_affected_planes(state, crtc);
  460. if (ret != 0)
  461. return ret;
  462. if (crtc_state->enable != has_connectors) {
  463. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
  464. crtc->base.id, crtc->name);
  465. return -EINVAL;
  466. }
  467. }
  468. return mode_fixup(state);
  469. }
  470. EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
  471. /**
  472. * drm_atomic_helper_check_planes - validate state object for planes changes
  473. * @dev: DRM device
  474. * @state: the driver state object
  475. *
  476. * Check the state object to see if the requested state is physically possible.
  477. * This does all the plane update related checks using by calling into the
  478. * ->atomic_check hooks provided by the driver.
  479. *
  480. * It also sets crtc_state->planes_changed to indicate that a crtc has
  481. * updated planes.
  482. *
  483. * RETURNS
  484. * Zero for success or -errno
  485. */
  486. int
  487. drm_atomic_helper_check_planes(struct drm_device *dev,
  488. struct drm_atomic_state *state)
  489. {
  490. struct drm_crtc *crtc;
  491. struct drm_crtc_state *crtc_state;
  492. struct drm_plane *plane;
  493. struct drm_plane_state *plane_state;
  494. int i, ret = 0;
  495. for_each_plane_in_state(state, plane, plane_state, i) {
  496. const struct drm_plane_helper_funcs *funcs;
  497. funcs = plane->helper_private;
  498. drm_atomic_helper_plane_changed(state, plane_state, plane);
  499. if (!funcs || !funcs->atomic_check)
  500. continue;
  501. ret = funcs->atomic_check(plane, plane_state);
  502. if (ret) {
  503. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
  504. plane->base.id, plane->name);
  505. return ret;
  506. }
  507. }
  508. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  509. const struct drm_crtc_helper_funcs *funcs;
  510. funcs = crtc->helper_private;
  511. if (!funcs || !funcs->atomic_check)
  512. continue;
  513. ret = funcs->atomic_check(crtc, state->crtc_states[i]);
  514. if (ret) {
  515. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
  516. crtc->base.id, crtc->name);
  517. return ret;
  518. }
  519. }
  520. return ret;
  521. }
  522. EXPORT_SYMBOL(drm_atomic_helper_check_planes);
  523. /**
  524. * drm_atomic_helper_check - validate state object
  525. * @dev: DRM device
  526. * @state: the driver state object
  527. *
  528. * Check the state object to see if the requested state is physically possible.
  529. * Only crtcs and planes have check callbacks, so for any additional (global)
  530. * checking that a driver needs it can simply wrap that around this function.
  531. * Drivers without such needs can directly use this as their ->atomic_check()
  532. * callback.
  533. *
  534. * This just wraps the two parts of the state checking for planes and modeset
  535. * state in the default order: First it calls drm_atomic_helper_check_modeset()
  536. * and then drm_atomic_helper_check_planes(). The assumption is that the
  537. * ->atomic_check functions depend upon an updated adjusted_mode.clock to
  538. * e.g. properly compute watermarks.
  539. *
  540. * RETURNS
  541. * Zero for success or -errno
  542. */
  543. int drm_atomic_helper_check(struct drm_device *dev,
  544. struct drm_atomic_state *state)
  545. {
  546. int ret;
  547. ret = drm_atomic_helper_check_modeset(dev, state);
  548. if (ret)
  549. return ret;
  550. ret = drm_atomic_helper_check_planes(dev, state);
  551. if (ret)
  552. return ret;
  553. return ret;
  554. }
  555. EXPORT_SYMBOL(drm_atomic_helper_check);
  556. static void
  557. disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
  558. {
  559. struct drm_connector *connector;
  560. struct drm_connector_state *old_conn_state;
  561. struct drm_crtc *crtc;
  562. struct drm_crtc_state *old_crtc_state;
  563. int i;
  564. for_each_connector_in_state(old_state, connector, old_conn_state, i) {
  565. const struct drm_encoder_helper_funcs *funcs;
  566. struct drm_encoder *encoder;
  567. /* Shut down everything that's in the changeset and currently
  568. * still on. So need to check the old, saved state. */
  569. if (!old_conn_state->crtc)
  570. continue;
  571. old_crtc_state = drm_atomic_get_existing_crtc_state(old_state,
  572. old_conn_state->crtc);
  573. if (!old_crtc_state->active ||
  574. !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
  575. continue;
  576. encoder = old_conn_state->best_encoder;
  577. /* We shouldn't get this far if we didn't previously have
  578. * an encoder.. but WARN_ON() rather than explode.
  579. */
  580. if (WARN_ON(!encoder))
  581. continue;
  582. funcs = encoder->helper_private;
  583. DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
  584. encoder->base.id, encoder->name);
  585. /*
  586. * Each encoder has at most one connector (since we always steal
  587. * it away), so we won't call disable hooks twice.
  588. */
  589. drm_bridge_disable(encoder->bridge);
  590. /* Right function depends upon target state. */
  591. if (funcs) {
  592. if (connector->state->crtc && funcs->prepare)
  593. funcs->prepare(encoder);
  594. else if (funcs->disable)
  595. funcs->disable(encoder);
  596. else if (funcs->dpms)
  597. funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  598. }
  599. drm_bridge_post_disable(encoder->bridge);
  600. }
  601. for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  602. const struct drm_crtc_helper_funcs *funcs;
  603. /* Shut down everything that needs a full modeset. */
  604. if (!drm_atomic_crtc_needs_modeset(crtc->state))
  605. continue;
  606. if (!old_crtc_state->active)
  607. continue;
  608. funcs = crtc->helper_private;
  609. DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
  610. crtc->base.id, crtc->name);
  611. /* Right function depends upon target state. */
  612. if (crtc->state->enable && funcs->prepare)
  613. funcs->prepare(crtc);
  614. else if (funcs->disable)
  615. funcs->disable(crtc);
  616. else
  617. funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  618. }
  619. }
  620. /**
  621. * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
  622. * @dev: DRM device
  623. * @old_state: atomic state object with old state structures
  624. *
  625. * This function updates all the various legacy modeset state pointers in
  626. * connectors, encoders and crtcs. It also updates the timestamping constants
  627. * used for precise vblank timestamps by calling
  628. * drm_calc_timestamping_constants().
  629. *
  630. * Drivers can use this for building their own atomic commit if they don't have
  631. * a pure helper-based modeset implementation.
  632. */
  633. void
  634. drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
  635. struct drm_atomic_state *old_state)
  636. {
  637. struct drm_connector *connector;
  638. struct drm_connector_state *old_conn_state;
  639. struct drm_crtc *crtc;
  640. struct drm_crtc_state *old_crtc_state;
  641. int i;
  642. /* clear out existing links and update dpms */
  643. for_each_connector_in_state(old_state, connector, old_conn_state, i) {
  644. if (connector->encoder) {
  645. WARN_ON(!connector->encoder->crtc);
  646. connector->encoder->crtc = NULL;
  647. connector->encoder = NULL;
  648. }
  649. crtc = connector->state->crtc;
  650. if ((!crtc && old_conn_state->crtc) ||
  651. (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
  652. struct drm_property *dpms_prop =
  653. dev->mode_config.dpms_property;
  654. int mode = DRM_MODE_DPMS_OFF;
  655. if (crtc && crtc->state->active)
  656. mode = DRM_MODE_DPMS_ON;
  657. connector->dpms = mode;
  658. drm_object_property_set_value(&connector->base,
  659. dpms_prop, mode);
  660. }
  661. }
  662. /* set new links */
  663. for_each_connector_in_state(old_state, connector, old_conn_state, i) {
  664. if (!connector->state->crtc)
  665. continue;
  666. if (WARN_ON(!connector->state->best_encoder))
  667. continue;
  668. connector->encoder = connector->state->best_encoder;
  669. connector->encoder->crtc = connector->state->crtc;
  670. }
  671. /* set legacy state in the crtc structure */
  672. for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  673. struct drm_plane *primary = crtc->primary;
  674. crtc->mode = crtc->state->mode;
  675. crtc->enabled = crtc->state->enable;
  676. if (drm_atomic_get_existing_plane_state(old_state, primary) &&
  677. primary->state->crtc == crtc) {
  678. crtc->x = primary->state->src_x >> 16;
  679. crtc->y = primary->state->src_y >> 16;
  680. }
  681. if (crtc->state->enable)
  682. drm_calc_timestamping_constants(crtc,
  683. &crtc->state->adjusted_mode);
  684. }
  685. }
  686. EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
  687. static void
  688. crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
  689. {
  690. struct drm_crtc *crtc;
  691. struct drm_crtc_state *old_crtc_state;
  692. struct drm_connector *connector;
  693. struct drm_connector_state *old_conn_state;
  694. int i;
  695. for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  696. const struct drm_crtc_helper_funcs *funcs;
  697. if (!crtc->state->mode_changed)
  698. continue;
  699. funcs = crtc->helper_private;
  700. if (crtc->state->enable && funcs->mode_set_nofb) {
  701. DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
  702. crtc->base.id, crtc->name);
  703. funcs->mode_set_nofb(crtc);
  704. }
  705. }
  706. for_each_connector_in_state(old_state, connector, old_conn_state, i) {
  707. const struct drm_encoder_helper_funcs *funcs;
  708. struct drm_crtc_state *new_crtc_state;
  709. struct drm_encoder *encoder;
  710. struct drm_display_mode *mode, *adjusted_mode;
  711. if (!connector->state->best_encoder)
  712. continue;
  713. encoder = connector->state->best_encoder;
  714. funcs = encoder->helper_private;
  715. new_crtc_state = connector->state->crtc->state;
  716. mode = &new_crtc_state->mode;
  717. adjusted_mode = &new_crtc_state->adjusted_mode;
  718. if (!new_crtc_state->mode_changed)
  719. continue;
  720. DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
  721. encoder->base.id, encoder->name);
  722. /*
  723. * Each encoder has at most one connector (since we always steal
  724. * it away), so we won't call mode_set hooks twice.
  725. */
  726. if (funcs && funcs->mode_set)
  727. funcs->mode_set(encoder, mode, adjusted_mode);
  728. drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
  729. }
  730. }
  731. /**
  732. * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
  733. * @dev: DRM device
  734. * @old_state: atomic state object with old state structures
  735. *
  736. * This function shuts down all the outputs that need to be shut down and
  737. * prepares them (if required) with the new mode.
  738. *
  739. * For compatibility with legacy crtc helpers this should be called before
  740. * drm_atomic_helper_commit_planes(), which is what the default commit function
  741. * does. But drivers with different needs can group the modeset commits together
  742. * and do the plane commits at the end. This is useful for drivers doing runtime
  743. * PM since planes updates then only happen when the CRTC is actually enabled.
  744. */
  745. void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
  746. struct drm_atomic_state *old_state)
  747. {
  748. disable_outputs(dev, old_state);
  749. drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
  750. crtc_set_mode(dev, old_state);
  751. }
  752. EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
  753. /**
  754. * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
  755. * @dev: DRM device
  756. * @old_state: atomic state object with old state structures
  757. *
  758. * This function enables all the outputs with the new configuration which had to
  759. * be turned off for the update.
  760. *
  761. * For compatibility with legacy crtc helpers this should be called after
  762. * drm_atomic_helper_commit_planes(), which is what the default commit function
  763. * does. But drivers with different needs can group the modeset commits together
  764. * and do the plane commits at the end. This is useful for drivers doing runtime
  765. * PM since planes updates then only happen when the CRTC is actually enabled.
  766. */
  767. void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
  768. struct drm_atomic_state *old_state)
  769. {
  770. struct drm_crtc *crtc;
  771. struct drm_crtc_state *old_crtc_state;
  772. struct drm_connector *connector;
  773. struct drm_connector_state *old_conn_state;
  774. int i;
  775. for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  776. const struct drm_crtc_helper_funcs *funcs;
  777. /* Need to filter out CRTCs where only planes change. */
  778. if (!drm_atomic_crtc_needs_modeset(crtc->state))
  779. continue;
  780. if (!crtc->state->active)
  781. continue;
  782. funcs = crtc->helper_private;
  783. if (crtc->state->enable) {
  784. DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
  785. crtc->base.id, crtc->name);
  786. if (funcs->enable)
  787. funcs->enable(crtc);
  788. else
  789. funcs->commit(crtc);
  790. }
  791. }
  792. for_each_connector_in_state(old_state, connector, old_conn_state, i) {
  793. const struct drm_encoder_helper_funcs *funcs;
  794. struct drm_encoder *encoder;
  795. if (!connector->state->best_encoder)
  796. continue;
  797. if (!connector->state->crtc->state->active ||
  798. !drm_atomic_crtc_needs_modeset(connector->state->crtc->state))
  799. continue;
  800. encoder = connector->state->best_encoder;
  801. funcs = encoder->helper_private;
  802. DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
  803. encoder->base.id, encoder->name);
  804. /*
  805. * Each encoder has at most one connector (since we always steal
  806. * it away), so we won't call enable hooks twice.
  807. */
  808. drm_bridge_pre_enable(encoder->bridge);
  809. if (funcs) {
  810. if (funcs->enable)
  811. funcs->enable(encoder);
  812. else if (funcs->commit)
  813. funcs->commit(encoder);
  814. }
  815. drm_bridge_enable(encoder->bridge);
  816. }
  817. }
  818. EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
  819. /**
  820. * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
  821. * @dev: DRM device
  822. * @state: atomic state object with old state structures
  823. *
  824. * For implicit sync, driver should fish the exclusive fence out from the
  825. * incoming fb's and stash it in the drm_plane_state. This is called after
  826. * drm_atomic_helper_swap_state() so it uses the current plane state (and
  827. * just uses the atomic state to find the changed planes)
  828. */
  829. void drm_atomic_helper_wait_for_fences(struct drm_device *dev,
  830. struct drm_atomic_state *state)
  831. {
  832. struct drm_plane *plane;
  833. struct drm_plane_state *plane_state;
  834. int i;
  835. for_each_plane_in_state(state, plane, plane_state, i) {
  836. if (!plane->state->fence)
  837. continue;
  838. WARN_ON(!plane->state->fb);
  839. fence_wait(plane->state->fence, false);
  840. fence_put(plane->state->fence);
  841. plane->state->fence = NULL;
  842. }
  843. }
  844. EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
  845. /**
  846. * drm_atomic_helper_framebuffer_changed - check if framebuffer has changed
  847. * @dev: DRM device
  848. * @old_state: atomic state object with old state structures
  849. * @crtc: DRM crtc
  850. *
  851. * Checks whether the framebuffer used for this CRTC changes as a result of
  852. * the atomic update. This is useful for drivers which cannot use
  853. * drm_atomic_helper_wait_for_vblanks() and need to reimplement its
  854. * functionality.
  855. *
  856. * Returns:
  857. * true if the framebuffer changed.
  858. */
  859. bool drm_atomic_helper_framebuffer_changed(struct drm_device *dev,
  860. struct drm_atomic_state *old_state,
  861. struct drm_crtc *crtc)
  862. {
  863. struct drm_plane *plane;
  864. struct drm_plane_state *old_plane_state;
  865. int i;
  866. for_each_plane_in_state(old_state, plane, old_plane_state, i) {
  867. if (plane->state->crtc != crtc &&
  868. old_plane_state->crtc != crtc)
  869. continue;
  870. if (plane->state->fb != old_plane_state->fb)
  871. return true;
  872. }
  873. return false;
  874. }
  875. EXPORT_SYMBOL(drm_atomic_helper_framebuffer_changed);
  876. /**
  877. * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
  878. * @dev: DRM device
  879. * @old_state: atomic state object with old state structures
  880. *
  881. * Helper to, after atomic commit, wait for vblanks on all effected
  882. * crtcs (ie. before cleaning up old framebuffers using
  883. * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
  884. * framebuffers have actually changed to optimize for the legacy cursor and
  885. * plane update use-case.
  886. */
  887. void
  888. drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
  889. struct drm_atomic_state *old_state)
  890. {
  891. struct drm_crtc *crtc;
  892. struct drm_crtc_state *old_crtc_state;
  893. int i, ret;
  894. for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  895. /* No one cares about the old state, so abuse it for tracking
  896. * and store whether we hold a vblank reference (and should do a
  897. * vblank wait) in the ->enable boolean. */
  898. old_crtc_state->enable = false;
  899. if (!crtc->state->enable)
  900. continue;
  901. /* Legacy cursor ioctls are completely unsynced, and userspace
  902. * relies on that (by doing tons of cursor updates). */
  903. if (old_state->legacy_cursor_update)
  904. continue;
  905. if (!drm_atomic_helper_framebuffer_changed(dev,
  906. old_state, crtc))
  907. continue;
  908. ret = drm_crtc_vblank_get(crtc);
  909. if (ret != 0)
  910. continue;
  911. old_crtc_state->enable = true;
  912. old_crtc_state->last_vblank_count = drm_crtc_vblank_count(crtc);
  913. }
  914. for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  915. if (!old_crtc_state->enable)
  916. continue;
  917. ret = wait_event_timeout(dev->vblank[i].queue,
  918. old_crtc_state->last_vblank_count !=
  919. drm_crtc_vblank_count(crtc),
  920. msecs_to_jiffies(50));
  921. WARN(!ret, "[CRTC:%d] vblank wait timed out\n", crtc->base.id);
  922. drm_crtc_vblank_put(crtc);
  923. }
  924. }
  925. EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
  926. /**
  927. * drm_atomic_helper_commit - commit validated state object
  928. * @dev: DRM device
  929. * @state: the driver state object
  930. * @nonblocking: whether nonblocking behavior is requested.
  931. *
  932. * This function commits a with drm_atomic_helper_check() pre-validated state
  933. * object. This can still fail when e.g. the framebuffer reservation fails. For
  934. * now this doesn't implement nonblocking commits.
  935. *
  936. * Note that right now this function does not support nonblocking commits, hence
  937. * driver writers must implement their own version for now. Also note that the
  938. * default ordering of how the various stages are called is to match the legacy
  939. * modeset helper library closest. One peculiarity of that is that it doesn't
  940. * mesh well with runtime PM at all.
  941. *
  942. * For drivers supporting runtime PM the recommended sequence is
  943. *
  944. * drm_atomic_helper_commit_modeset_disables(dev, state);
  945. *
  946. * drm_atomic_helper_commit_modeset_enables(dev, state);
  947. *
  948. * drm_atomic_helper_commit_planes(dev, state, true);
  949. *
  950. * See the kerneldoc entries for these three functions for more details.
  951. *
  952. * RETURNS
  953. * Zero for success or -errno.
  954. */
  955. int drm_atomic_helper_commit(struct drm_device *dev,
  956. struct drm_atomic_state *state,
  957. bool nonblock)
  958. {
  959. int ret;
  960. if (nonblock)
  961. return -EBUSY;
  962. ret = drm_atomic_helper_prepare_planes(dev, state);
  963. if (ret)
  964. return ret;
  965. /*
  966. * This is the point of no return - everything below never fails except
  967. * when the hw goes bonghits. Which means we can commit the new state on
  968. * the software side now.
  969. */
  970. drm_atomic_helper_swap_state(dev, state);
  971. /*
  972. * Everything below can be run asynchronously without the need to grab
  973. * any modeset locks at all under one condition: It must be guaranteed
  974. * that the asynchronous work has either been cancelled (if the driver
  975. * supports it, which at least requires that the framebuffers get
  976. * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
  977. * before the new state gets committed on the software side with
  978. * drm_atomic_helper_swap_state().
  979. *
  980. * This scheme allows new atomic state updates to be prepared and
  981. * checked in parallel to the asynchronous completion of the previous
  982. * update. Which is important since compositors need to figure out the
  983. * composition of the next frame right after having submitted the
  984. * current layout.
  985. */
  986. drm_atomic_helper_wait_for_fences(dev, state);
  987. drm_atomic_helper_commit_modeset_disables(dev, state);
  988. drm_atomic_helper_commit_planes(dev, state, false);
  989. drm_atomic_helper_commit_modeset_enables(dev, state);
  990. drm_atomic_helper_wait_for_vblanks(dev, state);
  991. drm_atomic_helper_cleanup_planes(dev, state);
  992. drm_atomic_state_free(state);
  993. return 0;
  994. }
  995. EXPORT_SYMBOL(drm_atomic_helper_commit);
  996. /**
  997. * DOC: implementing nonblocking commit
  998. *
  999. * For now the atomic helpers don't support nonblocking commit directly. If
  1000. * there is real need it could be added though, using the dma-buf fence
  1001. * infrastructure for generic synchronization with outstanding rendering.
  1002. *
  1003. * For now drivers have to implement nonblocking commit themselves, with the
  1004. * following sequence being the recommended one:
  1005. *
  1006. * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
  1007. * which commit needs to call which can fail, so we want to run it first and
  1008. * synchronously.
  1009. *
  1010. * 2. Synchronize with any outstanding nonblocking commit worker threads which
  1011. * might be affected the new state update. This can be done by either cancelling
  1012. * or flushing the work items, depending upon whether the driver can deal with
  1013. * cancelled updates. Note that it is important to ensure that the framebuffer
  1014. * cleanup is still done when cancelling.
  1015. *
  1016. * For sufficient parallelism it is recommended to have a work item per crtc
  1017. * (for updates which don't touch global state) and a global one. Then we only
  1018. * need to synchronize with the crtc work items for changed crtcs and the global
  1019. * work item, which allows nice concurrent updates on disjoint sets of crtcs.
  1020. *
  1021. * 3. The software state is updated synchronously with
  1022. * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
  1023. * locks means concurrent callers never see inconsistent state. And doing this
  1024. * while it's guaranteed that no relevant nonblocking worker runs means that
  1025. * nonblocking workers do not need grab any locks. Actually they must not grab
  1026. * locks, for otherwise the work flushing will deadlock.
  1027. *
  1028. * 4. Schedule a work item to do all subsequent steps, using the split-out
  1029. * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
  1030. * then cleaning up the framebuffers after the old framebuffer is no longer
  1031. * being displayed.
  1032. */
  1033. /**
  1034. * drm_atomic_helper_prepare_planes - prepare plane resources before commit
  1035. * @dev: DRM device
  1036. * @state: atomic state object with new state structures
  1037. *
  1038. * This function prepares plane state, specifically framebuffers, for the new
  1039. * configuration. If any failure is encountered this function will call
  1040. * ->cleanup_fb on any already successfully prepared framebuffer.
  1041. *
  1042. * Returns:
  1043. * 0 on success, negative error code on failure.
  1044. */
  1045. int drm_atomic_helper_prepare_planes(struct drm_device *dev,
  1046. struct drm_atomic_state *state)
  1047. {
  1048. int nplanes = dev->mode_config.num_total_plane;
  1049. int ret, i;
  1050. for (i = 0; i < nplanes; i++) {
  1051. const struct drm_plane_helper_funcs *funcs;
  1052. struct drm_plane *plane = state->planes[i];
  1053. struct drm_plane_state *plane_state = state->plane_states[i];
  1054. if (!plane)
  1055. continue;
  1056. funcs = plane->helper_private;
  1057. if (funcs->prepare_fb) {
  1058. ret = funcs->prepare_fb(plane, plane_state);
  1059. if (ret)
  1060. goto fail;
  1061. }
  1062. }
  1063. return 0;
  1064. fail:
  1065. for (i--; i >= 0; i--) {
  1066. const struct drm_plane_helper_funcs *funcs;
  1067. struct drm_plane *plane = state->planes[i];
  1068. struct drm_plane_state *plane_state = state->plane_states[i];
  1069. if (!plane)
  1070. continue;
  1071. funcs = plane->helper_private;
  1072. if (funcs->cleanup_fb)
  1073. funcs->cleanup_fb(plane, plane_state);
  1074. }
  1075. return ret;
  1076. }
  1077. EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
  1078. bool plane_crtc_active(struct drm_plane_state *state)
  1079. {
  1080. return state->crtc && state->crtc->state->active;
  1081. }
  1082. /**
  1083. * drm_atomic_helper_commit_planes - commit plane state
  1084. * @dev: DRM device
  1085. * @old_state: atomic state object with old state structures
  1086. * @active_only: Only commit on active CRTC if set
  1087. *
  1088. * This function commits the new plane state using the plane and atomic helper
  1089. * functions for planes and crtcs. It assumes that the atomic state has already
  1090. * been pushed into the relevant object state pointers, since this step can no
  1091. * longer fail.
  1092. *
  1093. * It still requires the global state object @old_state to know which planes and
  1094. * crtcs need to be updated though.
  1095. *
  1096. * Note that this function does all plane updates across all CRTCs in one step.
  1097. * If the hardware can't support this approach look at
  1098. * drm_atomic_helper_commit_planes_on_crtc() instead.
  1099. *
  1100. * Plane parameters can be updated by applications while the associated CRTC is
  1101. * disabled. The DRM/KMS core will store the parameters in the plane state,
  1102. * which will be available to the driver when the CRTC is turned on. As a result
  1103. * most drivers don't need to be immediately notified of plane updates for a
  1104. * disabled CRTC.
  1105. *
  1106. * Unless otherwise needed, drivers are advised to set the @active_only
  1107. * parameters to true in order not to receive plane update notifications related
  1108. * to a disabled CRTC. This avoids the need to manually ignore plane updates in
  1109. * driver code when the driver and/or hardware can't or just don't need to deal
  1110. * with updates on disabled CRTCs, for example when supporting runtime PM.
  1111. *
  1112. * The drm_atomic_helper_commit() default implementation only sets @active_only
  1113. * to false to most closely match the behaviour of the legacy helpers. This should
  1114. * not be copied blindly by drivers.
  1115. */
  1116. void drm_atomic_helper_commit_planes(struct drm_device *dev,
  1117. struct drm_atomic_state *old_state,
  1118. bool active_only)
  1119. {
  1120. struct drm_crtc *crtc;
  1121. struct drm_crtc_state *old_crtc_state;
  1122. struct drm_plane *plane;
  1123. struct drm_plane_state *old_plane_state;
  1124. int i;
  1125. for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  1126. const struct drm_crtc_helper_funcs *funcs;
  1127. funcs = crtc->helper_private;
  1128. if (!funcs || !funcs->atomic_begin)
  1129. continue;
  1130. if (active_only && !crtc->state->active)
  1131. continue;
  1132. funcs->atomic_begin(crtc, old_crtc_state);
  1133. }
  1134. for_each_plane_in_state(old_state, plane, old_plane_state, i) {
  1135. const struct drm_plane_helper_funcs *funcs;
  1136. bool disabling;
  1137. funcs = plane->helper_private;
  1138. if (!funcs)
  1139. continue;
  1140. disabling = drm_atomic_plane_disabling(plane, old_plane_state);
  1141. if (active_only) {
  1142. /*
  1143. * Skip planes related to inactive CRTCs. If the plane
  1144. * is enabled use the state of the current CRTC. If the
  1145. * plane is being disabled use the state of the old
  1146. * CRTC to avoid skipping planes being disabled on an
  1147. * active CRTC.
  1148. */
  1149. if (!disabling && !plane_crtc_active(plane->state))
  1150. continue;
  1151. if (disabling && !plane_crtc_active(old_plane_state))
  1152. continue;
  1153. }
  1154. /*
  1155. * Special-case disabling the plane if drivers support it.
  1156. */
  1157. if (disabling && funcs->atomic_disable)
  1158. funcs->atomic_disable(plane, old_plane_state);
  1159. else if (plane->state->crtc || disabling)
  1160. funcs->atomic_update(plane, old_plane_state);
  1161. }
  1162. for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  1163. const struct drm_crtc_helper_funcs *funcs;
  1164. funcs = crtc->helper_private;
  1165. if (!funcs || !funcs->atomic_flush)
  1166. continue;
  1167. if (active_only && !crtc->state->active)
  1168. continue;
  1169. funcs->atomic_flush(crtc, old_crtc_state);
  1170. }
  1171. }
  1172. EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
  1173. /**
  1174. * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
  1175. * @old_crtc_state: atomic state object with the old crtc state
  1176. *
  1177. * This function commits the new plane state using the plane and atomic helper
  1178. * functions for planes on the specific crtc. It assumes that the atomic state
  1179. * has already been pushed into the relevant object state pointers, since this
  1180. * step can no longer fail.
  1181. *
  1182. * This function is useful when plane updates should be done crtc-by-crtc
  1183. * instead of one global step like drm_atomic_helper_commit_planes() does.
  1184. *
  1185. * This function can only be savely used when planes are not allowed to move
  1186. * between different CRTCs because this function doesn't handle inter-CRTC
  1187. * depencies. Callers need to ensure that either no such depencies exist,
  1188. * resolve them through ordering of commit calls or through some other means.
  1189. */
  1190. void
  1191. drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
  1192. {
  1193. const struct drm_crtc_helper_funcs *crtc_funcs;
  1194. struct drm_crtc *crtc = old_crtc_state->crtc;
  1195. struct drm_atomic_state *old_state = old_crtc_state->state;
  1196. struct drm_plane *plane;
  1197. unsigned plane_mask;
  1198. plane_mask = old_crtc_state->plane_mask;
  1199. plane_mask |= crtc->state->plane_mask;
  1200. crtc_funcs = crtc->helper_private;
  1201. if (crtc_funcs && crtc_funcs->atomic_begin)
  1202. crtc_funcs->atomic_begin(crtc, old_crtc_state);
  1203. drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
  1204. struct drm_plane_state *old_plane_state =
  1205. drm_atomic_get_existing_plane_state(old_state, plane);
  1206. const struct drm_plane_helper_funcs *plane_funcs;
  1207. plane_funcs = plane->helper_private;
  1208. if (!old_plane_state || !plane_funcs)
  1209. continue;
  1210. WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
  1211. if (drm_atomic_plane_disabling(plane, old_plane_state) &&
  1212. plane_funcs->atomic_disable)
  1213. plane_funcs->atomic_disable(plane, old_plane_state);
  1214. else if (plane->state->crtc ||
  1215. drm_atomic_plane_disabling(plane, old_plane_state))
  1216. plane_funcs->atomic_update(plane, old_plane_state);
  1217. }
  1218. if (crtc_funcs && crtc_funcs->atomic_flush)
  1219. crtc_funcs->atomic_flush(crtc, old_crtc_state);
  1220. }
  1221. EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
  1222. /**
  1223. * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
  1224. * @crtc: CRTC
  1225. * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
  1226. *
  1227. * Disables all planes associated with the given CRTC. This can be
  1228. * used for instance in the CRTC helper disable callback to disable
  1229. * all planes before shutting down the display pipeline.
  1230. *
  1231. * If the atomic-parameter is set the function calls the CRTC's
  1232. * atomic_begin hook before and atomic_flush hook after disabling the
  1233. * planes.
  1234. *
  1235. * It is a bug to call this function without having implemented the
  1236. * ->atomic_disable() plane hook.
  1237. */
  1238. void drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc *crtc,
  1239. bool atomic)
  1240. {
  1241. const struct drm_crtc_helper_funcs *crtc_funcs =
  1242. crtc->helper_private;
  1243. struct drm_plane *plane;
  1244. if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
  1245. crtc_funcs->atomic_begin(crtc, NULL);
  1246. drm_for_each_plane(plane, crtc->dev) {
  1247. const struct drm_plane_helper_funcs *plane_funcs =
  1248. plane->helper_private;
  1249. if (plane->state->crtc != crtc || !plane_funcs)
  1250. continue;
  1251. WARN_ON(!plane_funcs->atomic_disable);
  1252. if (plane_funcs->atomic_disable)
  1253. plane_funcs->atomic_disable(plane, NULL);
  1254. }
  1255. if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
  1256. crtc_funcs->atomic_flush(crtc, NULL);
  1257. }
  1258. EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
  1259. /**
  1260. * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
  1261. * @dev: DRM device
  1262. * @old_state: atomic state object with old state structures
  1263. *
  1264. * This function cleans up plane state, specifically framebuffers, from the old
  1265. * configuration. Hence the old configuration must be perserved in @old_state to
  1266. * be able to call this function.
  1267. *
  1268. * This function must also be called on the new state when the atomic update
  1269. * fails at any point after calling drm_atomic_helper_prepare_planes().
  1270. */
  1271. void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
  1272. struct drm_atomic_state *old_state)
  1273. {
  1274. struct drm_plane *plane;
  1275. struct drm_plane_state *plane_state;
  1276. int i;
  1277. for_each_plane_in_state(old_state, plane, plane_state, i) {
  1278. const struct drm_plane_helper_funcs *funcs;
  1279. funcs = plane->helper_private;
  1280. if (funcs->cleanup_fb)
  1281. funcs->cleanup_fb(plane, plane_state);
  1282. }
  1283. }
  1284. EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
  1285. /**
  1286. * drm_atomic_helper_swap_state - store atomic state into current sw state
  1287. * @dev: DRM device
  1288. * @state: atomic state
  1289. *
  1290. * This function stores the atomic state into the current state pointers in all
  1291. * driver objects. It should be called after all failing steps have been done
  1292. * and succeeded, but before the actual hardware state is committed.
  1293. *
  1294. * For cleanup and error recovery the current state for all changed objects will
  1295. * be swaped into @state.
  1296. *
  1297. * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
  1298. *
  1299. * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
  1300. *
  1301. * 2. Do any other steps that might fail.
  1302. *
  1303. * 3. Put the staged state into the current state pointers with this function.
  1304. *
  1305. * 4. Actually commit the hardware state.
  1306. *
  1307. * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
  1308. * contains the old state. Also do any other cleanup required with that state.
  1309. */
  1310. void drm_atomic_helper_swap_state(struct drm_device *dev,
  1311. struct drm_atomic_state *state)
  1312. {
  1313. int i;
  1314. for (i = 0; i < state->num_connector; i++) {
  1315. struct drm_connector *connector = state->connectors[i];
  1316. if (!connector)
  1317. continue;
  1318. connector->state->state = state;
  1319. swap(state->connector_states[i], connector->state);
  1320. connector->state->state = NULL;
  1321. }
  1322. for (i = 0; i < dev->mode_config.num_crtc; i++) {
  1323. struct drm_crtc *crtc = state->crtcs[i];
  1324. if (!crtc)
  1325. continue;
  1326. crtc->state->state = state;
  1327. swap(state->crtc_states[i], crtc->state);
  1328. crtc->state->state = NULL;
  1329. }
  1330. for (i = 0; i < dev->mode_config.num_total_plane; i++) {
  1331. struct drm_plane *plane = state->planes[i];
  1332. if (!plane)
  1333. continue;
  1334. plane->state->state = state;
  1335. swap(state->plane_states[i], plane->state);
  1336. plane->state->state = NULL;
  1337. }
  1338. }
  1339. EXPORT_SYMBOL(drm_atomic_helper_swap_state);
  1340. /**
  1341. * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
  1342. * @plane: plane object to update
  1343. * @crtc: owning CRTC of owning plane
  1344. * @fb: framebuffer to flip onto plane
  1345. * @crtc_x: x offset of primary plane on crtc
  1346. * @crtc_y: y offset of primary plane on crtc
  1347. * @crtc_w: width of primary plane rectangle on crtc
  1348. * @crtc_h: height of primary plane rectangle on crtc
  1349. * @src_x: x offset of @fb for panning
  1350. * @src_y: y offset of @fb for panning
  1351. * @src_w: width of source rectangle in @fb
  1352. * @src_h: height of source rectangle in @fb
  1353. *
  1354. * Provides a default plane update handler using the atomic driver interface.
  1355. *
  1356. * RETURNS:
  1357. * Zero on success, error code on failure
  1358. */
  1359. int drm_atomic_helper_update_plane(struct drm_plane *plane,
  1360. struct drm_crtc *crtc,
  1361. struct drm_framebuffer *fb,
  1362. int crtc_x, int crtc_y,
  1363. unsigned int crtc_w, unsigned int crtc_h,
  1364. uint32_t src_x, uint32_t src_y,
  1365. uint32_t src_w, uint32_t src_h)
  1366. {
  1367. struct drm_atomic_state *state;
  1368. struct drm_plane_state *plane_state;
  1369. int ret = 0;
  1370. state = drm_atomic_state_alloc(plane->dev);
  1371. if (!state)
  1372. return -ENOMEM;
  1373. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  1374. retry:
  1375. plane_state = drm_atomic_get_plane_state(state, plane);
  1376. if (IS_ERR(plane_state)) {
  1377. ret = PTR_ERR(plane_state);
  1378. goto fail;
  1379. }
  1380. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  1381. if (ret != 0)
  1382. goto fail;
  1383. drm_atomic_set_fb_for_plane(plane_state, fb);
  1384. plane_state->crtc_x = crtc_x;
  1385. plane_state->crtc_y = crtc_y;
  1386. plane_state->crtc_w = crtc_w;
  1387. plane_state->crtc_h = crtc_h;
  1388. plane_state->src_x = src_x;
  1389. plane_state->src_y = src_y;
  1390. plane_state->src_w = src_w;
  1391. plane_state->src_h = src_h;
  1392. if (plane == crtc->cursor)
  1393. state->legacy_cursor_update = true;
  1394. ret = drm_atomic_commit(state);
  1395. if (ret != 0)
  1396. goto fail;
  1397. /* Driver takes ownership of state on successful commit. */
  1398. return 0;
  1399. fail:
  1400. if (ret == -EDEADLK)
  1401. goto backoff;
  1402. drm_atomic_state_free(state);
  1403. return ret;
  1404. backoff:
  1405. drm_atomic_state_clear(state);
  1406. drm_atomic_legacy_backoff(state);
  1407. /*
  1408. * Someone might have exchanged the framebuffer while we dropped locks
  1409. * in the backoff code. We need to fix up the fb refcount tracking the
  1410. * core does for us.
  1411. */
  1412. plane->old_fb = plane->fb;
  1413. goto retry;
  1414. }
  1415. EXPORT_SYMBOL(drm_atomic_helper_update_plane);
  1416. /**
  1417. * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
  1418. * @plane: plane to disable
  1419. *
  1420. * Provides a default plane disable handler using the atomic driver interface.
  1421. *
  1422. * RETURNS:
  1423. * Zero on success, error code on failure
  1424. */
  1425. int drm_atomic_helper_disable_plane(struct drm_plane *plane)
  1426. {
  1427. struct drm_atomic_state *state;
  1428. struct drm_plane_state *plane_state;
  1429. int ret = 0;
  1430. /*
  1431. * FIXME: Without plane->crtc set we can't get at the implicit legacy
  1432. * acquire context. The real fix will be to wire the acquire ctx through
  1433. * everywhere we need it, but meanwhile prevent chaos by just skipping
  1434. * this noop. The critical case is the cursor ioctls which a) only grab
  1435. * crtc/cursor-plane locks (so we need the crtc to get at the right
  1436. * acquire context) and b) can try to disable the plane multiple times.
  1437. */
  1438. if (!plane->crtc)
  1439. return 0;
  1440. state = drm_atomic_state_alloc(plane->dev);
  1441. if (!state)
  1442. return -ENOMEM;
  1443. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
  1444. retry:
  1445. plane_state = drm_atomic_get_plane_state(state, plane);
  1446. if (IS_ERR(plane_state)) {
  1447. ret = PTR_ERR(plane_state);
  1448. goto fail;
  1449. }
  1450. if (plane_state->crtc && (plane == plane->crtc->cursor))
  1451. plane_state->state->legacy_cursor_update = true;
  1452. ret = __drm_atomic_helper_disable_plane(plane, plane_state);
  1453. if (ret != 0)
  1454. goto fail;
  1455. ret = drm_atomic_commit(state);
  1456. if (ret != 0)
  1457. goto fail;
  1458. /* Driver takes ownership of state on successful commit. */
  1459. return 0;
  1460. fail:
  1461. if (ret == -EDEADLK)
  1462. goto backoff;
  1463. drm_atomic_state_free(state);
  1464. return ret;
  1465. backoff:
  1466. drm_atomic_state_clear(state);
  1467. drm_atomic_legacy_backoff(state);
  1468. /*
  1469. * Someone might have exchanged the framebuffer while we dropped locks
  1470. * in the backoff code. We need to fix up the fb refcount tracking the
  1471. * core does for us.
  1472. */
  1473. plane->old_fb = plane->fb;
  1474. goto retry;
  1475. }
  1476. EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
  1477. /* just used from fb-helper and atomic-helper: */
  1478. int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
  1479. struct drm_plane_state *plane_state)
  1480. {
  1481. int ret;
  1482. ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
  1483. if (ret != 0)
  1484. return ret;
  1485. drm_atomic_set_fb_for_plane(plane_state, NULL);
  1486. plane_state->crtc_x = 0;
  1487. plane_state->crtc_y = 0;
  1488. plane_state->crtc_w = 0;
  1489. plane_state->crtc_h = 0;
  1490. plane_state->src_x = 0;
  1491. plane_state->src_y = 0;
  1492. plane_state->src_w = 0;
  1493. plane_state->src_h = 0;
  1494. return 0;
  1495. }
  1496. static int update_output_state(struct drm_atomic_state *state,
  1497. struct drm_mode_set *set)
  1498. {
  1499. struct drm_device *dev = set->crtc->dev;
  1500. struct drm_crtc *crtc;
  1501. struct drm_crtc_state *crtc_state;
  1502. struct drm_connector *connector;
  1503. struct drm_connector_state *conn_state;
  1504. int ret, i;
  1505. ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
  1506. state->acquire_ctx);
  1507. if (ret)
  1508. return ret;
  1509. /* First disable all connectors on the target crtc. */
  1510. ret = drm_atomic_add_affected_connectors(state, set->crtc);
  1511. if (ret)
  1512. return ret;
  1513. for_each_connector_in_state(state, connector, conn_state, i) {
  1514. if (conn_state->crtc == set->crtc) {
  1515. ret = drm_atomic_set_crtc_for_connector(conn_state,
  1516. NULL);
  1517. if (ret)
  1518. return ret;
  1519. }
  1520. }
  1521. /* Then set all connectors from set->connectors on the target crtc */
  1522. for (i = 0; i < set->num_connectors; i++) {
  1523. conn_state = drm_atomic_get_connector_state(state,
  1524. set->connectors[i]);
  1525. if (IS_ERR(conn_state))
  1526. return PTR_ERR(conn_state);
  1527. ret = drm_atomic_set_crtc_for_connector(conn_state,
  1528. set->crtc);
  1529. if (ret)
  1530. return ret;
  1531. }
  1532. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  1533. /* Don't update ->enable for the CRTC in the set_config request,
  1534. * since a mismatch would indicate a bug in the upper layers.
  1535. * The actual modeset code later on will catch any
  1536. * inconsistencies here. */
  1537. if (crtc == set->crtc)
  1538. continue;
  1539. if (!crtc_state->connector_mask) {
  1540. ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
  1541. NULL);
  1542. if (ret < 0)
  1543. return ret;
  1544. crtc_state->active = false;
  1545. }
  1546. }
  1547. return 0;
  1548. }
  1549. /**
  1550. * drm_atomic_helper_set_config - set a new config from userspace
  1551. * @set: mode set configuration
  1552. *
  1553. * Provides a default crtc set_config handler using the atomic driver interface.
  1554. *
  1555. * Returns:
  1556. * Returns 0 on success, negative errno numbers on failure.
  1557. */
  1558. int drm_atomic_helper_set_config(struct drm_mode_set *set)
  1559. {
  1560. struct drm_atomic_state *state;
  1561. struct drm_crtc *crtc = set->crtc;
  1562. int ret = 0;
  1563. state = drm_atomic_state_alloc(crtc->dev);
  1564. if (!state)
  1565. return -ENOMEM;
  1566. state->legacy_set_config = true;
  1567. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  1568. retry:
  1569. ret = __drm_atomic_helper_set_config(set, state);
  1570. if (ret != 0)
  1571. goto fail;
  1572. ret = drm_atomic_commit(state);
  1573. if (ret != 0)
  1574. goto fail;
  1575. /* Driver takes ownership of state on successful commit. */
  1576. return 0;
  1577. fail:
  1578. if (ret == -EDEADLK)
  1579. goto backoff;
  1580. drm_atomic_state_free(state);
  1581. return ret;
  1582. backoff:
  1583. drm_atomic_state_clear(state);
  1584. drm_atomic_legacy_backoff(state);
  1585. /*
  1586. * Someone might have exchanged the framebuffer while we dropped locks
  1587. * in the backoff code. We need to fix up the fb refcount tracking the
  1588. * core does for us.
  1589. */
  1590. crtc->primary->old_fb = crtc->primary->fb;
  1591. goto retry;
  1592. }
  1593. EXPORT_SYMBOL(drm_atomic_helper_set_config);
  1594. /* just used from fb-helper and atomic-helper: */
  1595. int __drm_atomic_helper_set_config(struct drm_mode_set *set,
  1596. struct drm_atomic_state *state)
  1597. {
  1598. struct drm_crtc_state *crtc_state;
  1599. struct drm_plane_state *primary_state;
  1600. struct drm_crtc *crtc = set->crtc;
  1601. int hdisplay, vdisplay;
  1602. int ret;
  1603. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  1604. if (IS_ERR(crtc_state))
  1605. return PTR_ERR(crtc_state);
  1606. primary_state = drm_atomic_get_plane_state(state, crtc->primary);
  1607. if (IS_ERR(primary_state))
  1608. return PTR_ERR(primary_state);
  1609. if (!set->mode) {
  1610. WARN_ON(set->fb);
  1611. WARN_ON(set->num_connectors);
  1612. ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
  1613. if (ret != 0)
  1614. return ret;
  1615. crtc_state->active = false;
  1616. ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
  1617. if (ret != 0)
  1618. return ret;
  1619. drm_atomic_set_fb_for_plane(primary_state, NULL);
  1620. goto commit;
  1621. }
  1622. WARN_ON(!set->fb);
  1623. WARN_ON(!set->num_connectors);
  1624. ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
  1625. if (ret != 0)
  1626. return ret;
  1627. crtc_state->active = true;
  1628. ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
  1629. if (ret != 0)
  1630. return ret;
  1631. drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
  1632. drm_atomic_set_fb_for_plane(primary_state, set->fb);
  1633. primary_state->crtc_x = 0;
  1634. primary_state->crtc_y = 0;
  1635. primary_state->crtc_w = hdisplay;
  1636. primary_state->crtc_h = vdisplay;
  1637. primary_state->src_x = set->x << 16;
  1638. primary_state->src_y = set->y << 16;
  1639. if (primary_state->rotation & (BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270))) {
  1640. primary_state->src_w = vdisplay << 16;
  1641. primary_state->src_h = hdisplay << 16;
  1642. } else {
  1643. primary_state->src_w = hdisplay << 16;
  1644. primary_state->src_h = vdisplay << 16;
  1645. }
  1646. commit:
  1647. ret = update_output_state(state, set);
  1648. if (ret)
  1649. return ret;
  1650. return 0;
  1651. }
  1652. /**
  1653. * drm_atomic_helper_disable_all - disable all currently active outputs
  1654. * @dev: DRM device
  1655. * @ctx: lock acquisition context
  1656. *
  1657. * Loops through all connectors, finding those that aren't turned off and then
  1658. * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
  1659. * that they are connected to.
  1660. *
  1661. * This is used for example in suspend/resume to disable all currently active
  1662. * functions when suspending.
  1663. *
  1664. * Note that if callers haven't already acquired all modeset locks this might
  1665. * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
  1666. *
  1667. * Returns:
  1668. * 0 on success or a negative error code on failure.
  1669. *
  1670. * See also:
  1671. * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
  1672. */
  1673. int drm_atomic_helper_disable_all(struct drm_device *dev,
  1674. struct drm_modeset_acquire_ctx *ctx)
  1675. {
  1676. struct drm_atomic_state *state;
  1677. struct drm_connector *conn;
  1678. int err;
  1679. state = drm_atomic_state_alloc(dev);
  1680. if (!state)
  1681. return -ENOMEM;
  1682. state->acquire_ctx = ctx;
  1683. drm_for_each_connector(conn, dev) {
  1684. struct drm_crtc *crtc = conn->state->crtc;
  1685. struct drm_crtc_state *crtc_state;
  1686. if (!crtc || conn->dpms != DRM_MODE_DPMS_ON)
  1687. continue;
  1688. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  1689. if (IS_ERR(crtc_state)) {
  1690. err = PTR_ERR(crtc_state);
  1691. goto free;
  1692. }
  1693. crtc_state->active = false;
  1694. }
  1695. err = drm_atomic_commit(state);
  1696. free:
  1697. if (err < 0)
  1698. drm_atomic_state_free(state);
  1699. return err;
  1700. }
  1701. EXPORT_SYMBOL(drm_atomic_helper_disable_all);
  1702. /**
  1703. * drm_atomic_helper_suspend - subsystem-level suspend helper
  1704. * @dev: DRM device
  1705. *
  1706. * Duplicates the current atomic state, disables all active outputs and then
  1707. * returns a pointer to the original atomic state to the caller. Drivers can
  1708. * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
  1709. * restore the output configuration that was active at the time the system
  1710. * entered suspend.
  1711. *
  1712. * Note that it is potentially unsafe to use this. The atomic state object
  1713. * returned by this function is assumed to be persistent. Drivers must ensure
  1714. * that this holds true. Before calling this function, drivers must make sure
  1715. * to suspend fbdev emulation so that nothing can be using the device.
  1716. *
  1717. * Returns:
  1718. * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
  1719. * encoded error code on failure. Drivers should store the returned atomic
  1720. * state object and pass it to the drm_atomic_helper_resume() helper upon
  1721. * resume.
  1722. *
  1723. * See also:
  1724. * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
  1725. * drm_atomic_helper_resume()
  1726. */
  1727. struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
  1728. {
  1729. struct drm_modeset_acquire_ctx ctx;
  1730. struct drm_atomic_state *state;
  1731. int err;
  1732. drm_modeset_acquire_init(&ctx, 0);
  1733. retry:
  1734. err = drm_modeset_lock_all_ctx(dev, &ctx);
  1735. if (err < 0) {
  1736. state = ERR_PTR(err);
  1737. goto unlock;
  1738. }
  1739. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  1740. if (IS_ERR(state))
  1741. goto unlock;
  1742. err = drm_atomic_helper_disable_all(dev, &ctx);
  1743. if (err < 0) {
  1744. drm_atomic_state_free(state);
  1745. state = ERR_PTR(err);
  1746. goto unlock;
  1747. }
  1748. unlock:
  1749. if (PTR_ERR(state) == -EDEADLK) {
  1750. drm_modeset_backoff(&ctx);
  1751. goto retry;
  1752. }
  1753. drm_modeset_drop_locks(&ctx);
  1754. drm_modeset_acquire_fini(&ctx);
  1755. return state;
  1756. }
  1757. EXPORT_SYMBOL(drm_atomic_helper_suspend);
  1758. /**
  1759. * drm_atomic_helper_resume - subsystem-level resume helper
  1760. * @dev: DRM device
  1761. * @state: atomic state to resume to
  1762. *
  1763. * Calls drm_mode_config_reset() to synchronize hardware and software states,
  1764. * grabs all modeset locks and commits the atomic state object. This can be
  1765. * used in conjunction with the drm_atomic_helper_suspend() helper to
  1766. * implement suspend/resume for drivers that support atomic mode-setting.
  1767. *
  1768. * Returns:
  1769. * 0 on success or a negative error code on failure.
  1770. *
  1771. * See also:
  1772. * drm_atomic_helper_suspend()
  1773. */
  1774. int drm_atomic_helper_resume(struct drm_device *dev,
  1775. struct drm_atomic_state *state)
  1776. {
  1777. struct drm_mode_config *config = &dev->mode_config;
  1778. int err;
  1779. drm_mode_config_reset(dev);
  1780. drm_modeset_lock_all(dev);
  1781. state->acquire_ctx = config->acquire_ctx;
  1782. err = drm_atomic_commit(state);
  1783. drm_modeset_unlock_all(dev);
  1784. return err;
  1785. }
  1786. EXPORT_SYMBOL(drm_atomic_helper_resume);
  1787. /**
  1788. * drm_atomic_helper_crtc_set_property - helper for crtc properties
  1789. * @crtc: DRM crtc
  1790. * @property: DRM property
  1791. * @val: value of property
  1792. *
  1793. * Provides a default crtc set_property handler using the atomic driver
  1794. * interface.
  1795. *
  1796. * RETURNS:
  1797. * Zero on success, error code on failure
  1798. */
  1799. int
  1800. drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
  1801. struct drm_property *property,
  1802. uint64_t val)
  1803. {
  1804. struct drm_atomic_state *state;
  1805. struct drm_crtc_state *crtc_state;
  1806. int ret = 0;
  1807. state = drm_atomic_state_alloc(crtc->dev);
  1808. if (!state)
  1809. return -ENOMEM;
  1810. /* ->set_property is always called with all locks held. */
  1811. state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
  1812. retry:
  1813. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  1814. if (IS_ERR(crtc_state)) {
  1815. ret = PTR_ERR(crtc_state);
  1816. goto fail;
  1817. }
  1818. ret = drm_atomic_crtc_set_property(crtc, crtc_state,
  1819. property, val);
  1820. if (ret)
  1821. goto fail;
  1822. ret = drm_atomic_commit(state);
  1823. if (ret != 0)
  1824. goto fail;
  1825. /* Driver takes ownership of state on successful commit. */
  1826. return 0;
  1827. fail:
  1828. if (ret == -EDEADLK)
  1829. goto backoff;
  1830. drm_atomic_state_free(state);
  1831. return ret;
  1832. backoff:
  1833. drm_atomic_state_clear(state);
  1834. drm_atomic_legacy_backoff(state);
  1835. goto retry;
  1836. }
  1837. EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
  1838. /**
  1839. * drm_atomic_helper_plane_set_property - helper for plane properties
  1840. * @plane: DRM plane
  1841. * @property: DRM property
  1842. * @val: value of property
  1843. *
  1844. * Provides a default plane set_property handler using the atomic driver
  1845. * interface.
  1846. *
  1847. * RETURNS:
  1848. * Zero on success, error code on failure
  1849. */
  1850. int
  1851. drm_atomic_helper_plane_set_property(struct drm_plane *plane,
  1852. struct drm_property *property,
  1853. uint64_t val)
  1854. {
  1855. struct drm_atomic_state *state;
  1856. struct drm_plane_state *plane_state;
  1857. int ret = 0;
  1858. state = drm_atomic_state_alloc(plane->dev);
  1859. if (!state)
  1860. return -ENOMEM;
  1861. /* ->set_property is always called with all locks held. */
  1862. state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
  1863. retry:
  1864. plane_state = drm_atomic_get_plane_state(state, plane);
  1865. if (IS_ERR(plane_state)) {
  1866. ret = PTR_ERR(plane_state);
  1867. goto fail;
  1868. }
  1869. ret = drm_atomic_plane_set_property(plane, plane_state,
  1870. property, val);
  1871. if (ret)
  1872. goto fail;
  1873. ret = drm_atomic_commit(state);
  1874. if (ret != 0)
  1875. goto fail;
  1876. /* Driver takes ownership of state on successful commit. */
  1877. return 0;
  1878. fail:
  1879. if (ret == -EDEADLK)
  1880. goto backoff;
  1881. drm_atomic_state_free(state);
  1882. return ret;
  1883. backoff:
  1884. drm_atomic_state_clear(state);
  1885. drm_atomic_legacy_backoff(state);
  1886. goto retry;
  1887. }
  1888. EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
  1889. /**
  1890. * drm_atomic_helper_connector_set_property - helper for connector properties
  1891. * @connector: DRM connector
  1892. * @property: DRM property
  1893. * @val: value of property
  1894. *
  1895. * Provides a default connector set_property handler using the atomic driver
  1896. * interface.
  1897. *
  1898. * RETURNS:
  1899. * Zero on success, error code on failure
  1900. */
  1901. int
  1902. drm_atomic_helper_connector_set_property(struct drm_connector *connector,
  1903. struct drm_property *property,
  1904. uint64_t val)
  1905. {
  1906. struct drm_atomic_state *state;
  1907. struct drm_connector_state *connector_state;
  1908. int ret = 0;
  1909. state = drm_atomic_state_alloc(connector->dev);
  1910. if (!state)
  1911. return -ENOMEM;
  1912. /* ->set_property is always called with all locks held. */
  1913. state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
  1914. retry:
  1915. connector_state = drm_atomic_get_connector_state(state, connector);
  1916. if (IS_ERR(connector_state)) {
  1917. ret = PTR_ERR(connector_state);
  1918. goto fail;
  1919. }
  1920. ret = drm_atomic_connector_set_property(connector, connector_state,
  1921. property, val);
  1922. if (ret)
  1923. goto fail;
  1924. ret = drm_atomic_commit(state);
  1925. if (ret != 0)
  1926. goto fail;
  1927. /* Driver takes ownership of state on successful commit. */
  1928. return 0;
  1929. fail:
  1930. if (ret == -EDEADLK)
  1931. goto backoff;
  1932. drm_atomic_state_free(state);
  1933. return ret;
  1934. backoff:
  1935. drm_atomic_state_clear(state);
  1936. drm_atomic_legacy_backoff(state);
  1937. goto retry;
  1938. }
  1939. EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
  1940. /**
  1941. * drm_atomic_helper_page_flip - execute a legacy page flip
  1942. * @crtc: DRM crtc
  1943. * @fb: DRM framebuffer
  1944. * @event: optional DRM event to signal upon completion
  1945. * @flags: flip flags for non-vblank sync'ed updates
  1946. *
  1947. * Provides a default page flip implementation using the atomic driver interface.
  1948. *
  1949. * Note that for now so called async page flips (i.e. updates which are not
  1950. * synchronized to vblank) are not supported, since the atomic interfaces have
  1951. * no provisions for this yet.
  1952. *
  1953. * Returns:
  1954. * Returns 0 on success, negative errno numbers on failure.
  1955. */
  1956. int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
  1957. struct drm_framebuffer *fb,
  1958. struct drm_pending_vblank_event *event,
  1959. uint32_t flags)
  1960. {
  1961. struct drm_plane *plane = crtc->primary;
  1962. struct drm_atomic_state *state;
  1963. struct drm_plane_state *plane_state;
  1964. struct drm_crtc_state *crtc_state;
  1965. int ret = 0;
  1966. if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
  1967. return -EINVAL;
  1968. state = drm_atomic_state_alloc(plane->dev);
  1969. if (!state)
  1970. return -ENOMEM;
  1971. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  1972. retry:
  1973. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  1974. if (IS_ERR(crtc_state)) {
  1975. ret = PTR_ERR(crtc_state);
  1976. goto fail;
  1977. }
  1978. crtc_state->event = event;
  1979. plane_state = drm_atomic_get_plane_state(state, plane);
  1980. if (IS_ERR(plane_state)) {
  1981. ret = PTR_ERR(plane_state);
  1982. goto fail;
  1983. }
  1984. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  1985. if (ret != 0)
  1986. goto fail;
  1987. drm_atomic_set_fb_for_plane(plane_state, fb);
  1988. /* Make sure we don't accidentally do a full modeset. */
  1989. state->allow_modeset = false;
  1990. if (!crtc_state->active) {
  1991. DRM_DEBUG_ATOMIC("[CRTC:%d] disabled, rejecting legacy flip\n",
  1992. crtc->base.id);
  1993. ret = -EINVAL;
  1994. goto fail;
  1995. }
  1996. ret = drm_atomic_nonblocking_commit(state);
  1997. if (ret != 0)
  1998. goto fail;
  1999. /* Driver takes ownership of state on successful commit. */
  2000. return 0;
  2001. fail:
  2002. if (ret == -EDEADLK)
  2003. goto backoff;
  2004. drm_atomic_state_free(state);
  2005. return ret;
  2006. backoff:
  2007. drm_atomic_state_clear(state);
  2008. drm_atomic_legacy_backoff(state);
  2009. /*
  2010. * Someone might have exchanged the framebuffer while we dropped locks
  2011. * in the backoff code. We need to fix up the fb refcount tracking the
  2012. * core does for us.
  2013. */
  2014. plane->old_fb = plane->fb;
  2015. goto retry;
  2016. }
  2017. EXPORT_SYMBOL(drm_atomic_helper_page_flip);
  2018. /**
  2019. * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
  2020. * @connector: affected connector
  2021. * @mode: DPMS mode
  2022. *
  2023. * This is the main helper function provided by the atomic helper framework for
  2024. * implementing the legacy DPMS connector interface. It computes the new desired
  2025. * ->active state for the corresponding CRTC (if the connector is enabled) and
  2026. * updates it.
  2027. *
  2028. * Returns:
  2029. * Returns 0 on success, negative errno numbers on failure.
  2030. */
  2031. int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
  2032. int mode)
  2033. {
  2034. struct drm_mode_config *config = &connector->dev->mode_config;
  2035. struct drm_atomic_state *state;
  2036. struct drm_crtc_state *crtc_state;
  2037. struct drm_crtc *crtc;
  2038. struct drm_connector *tmp_connector;
  2039. int ret;
  2040. bool active = false;
  2041. int old_mode = connector->dpms;
  2042. if (mode != DRM_MODE_DPMS_ON)
  2043. mode = DRM_MODE_DPMS_OFF;
  2044. connector->dpms = mode;
  2045. crtc = connector->state->crtc;
  2046. if (!crtc)
  2047. return 0;
  2048. state = drm_atomic_state_alloc(connector->dev);
  2049. if (!state)
  2050. return -ENOMEM;
  2051. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  2052. retry:
  2053. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  2054. if (IS_ERR(crtc_state)) {
  2055. ret = PTR_ERR(crtc_state);
  2056. goto fail;
  2057. }
  2058. WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  2059. drm_for_each_connector(tmp_connector, connector->dev) {
  2060. if (tmp_connector->state->crtc != crtc)
  2061. continue;
  2062. if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
  2063. active = true;
  2064. break;
  2065. }
  2066. }
  2067. crtc_state->active = active;
  2068. ret = drm_atomic_commit(state);
  2069. if (ret != 0)
  2070. goto fail;
  2071. /* Driver takes ownership of state on successful commit. */
  2072. return 0;
  2073. fail:
  2074. if (ret == -EDEADLK)
  2075. goto backoff;
  2076. connector->dpms = old_mode;
  2077. drm_atomic_state_free(state);
  2078. return ret;
  2079. backoff:
  2080. drm_atomic_state_clear(state);
  2081. drm_atomic_legacy_backoff(state);
  2082. goto retry;
  2083. }
  2084. EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
  2085. /**
  2086. * drm_atomic_helper_best_encoder - Helper for &drm_connector_helper_funcs
  2087. * ->best_encoder callback
  2088. * @connector: Connector control structure
  2089. *
  2090. * This is a &drm_connector_helper_funcs ->best_encoder callback helper for
  2091. * connectors that support exactly 1 encoder, statically determined at driver
  2092. * init time.
  2093. */
  2094. struct drm_encoder *
  2095. drm_atomic_helper_best_encoder(struct drm_connector *connector)
  2096. {
  2097. WARN_ON(connector->encoder_ids[1]);
  2098. return drm_encoder_find(connector->dev, connector->encoder_ids[0]);
  2099. }
  2100. EXPORT_SYMBOL(drm_atomic_helper_best_encoder);
  2101. /**
  2102. * DOC: atomic state reset and initialization
  2103. *
  2104. * Both the drm core and the atomic helpers assume that there is always the full
  2105. * and correct atomic software state for all connectors, CRTCs and planes
  2106. * available. Which is a bit a problem on driver load and also after system
  2107. * suspend. One way to solve this is to have a hardware state read-out
  2108. * infrastructure which reconstructs the full software state (e.g. the i915
  2109. * driver).
  2110. *
  2111. * The simpler solution is to just reset the software state to everything off,
  2112. * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
  2113. * the atomic helpers provide default reset implementations for all hooks.
  2114. *
  2115. * On the upside the precise state tracking of atomic simplifies system suspend
  2116. * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
  2117. * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
  2118. * For other drivers the building blocks are split out, see the documentation
  2119. * for these functions.
  2120. */
  2121. /**
  2122. * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
  2123. * @crtc: drm CRTC
  2124. *
  2125. * Resets the atomic state for @crtc by freeing the state pointer (which might
  2126. * be NULL, e.g. at driver load time) and allocating a new empty state object.
  2127. */
  2128. void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
  2129. {
  2130. if (crtc->state)
  2131. __drm_atomic_helper_crtc_destroy_state(crtc->state);
  2132. kfree(crtc->state);
  2133. crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
  2134. if (crtc->state)
  2135. crtc->state->crtc = crtc;
  2136. }
  2137. EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
  2138. /**
  2139. * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
  2140. * @crtc: CRTC object
  2141. * @state: atomic CRTC state
  2142. *
  2143. * Copies atomic state from a CRTC's current state and resets inferred values.
  2144. * This is useful for drivers that subclass the CRTC state.
  2145. */
  2146. void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
  2147. struct drm_crtc_state *state)
  2148. {
  2149. memcpy(state, crtc->state, sizeof(*state));
  2150. if (state->mode_blob)
  2151. drm_property_reference_blob(state->mode_blob);
  2152. if (state->degamma_lut)
  2153. drm_property_reference_blob(state->degamma_lut);
  2154. if (state->ctm)
  2155. drm_property_reference_blob(state->ctm);
  2156. if (state->gamma_lut)
  2157. drm_property_reference_blob(state->gamma_lut);
  2158. state->mode_changed = false;
  2159. state->active_changed = false;
  2160. state->planes_changed = false;
  2161. state->connectors_changed = false;
  2162. state->color_mgmt_changed = false;
  2163. state->event = NULL;
  2164. }
  2165. EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
  2166. /**
  2167. * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
  2168. * @crtc: drm CRTC
  2169. *
  2170. * Default CRTC state duplicate hook for drivers which don't have their own
  2171. * subclassed CRTC state structure.
  2172. */
  2173. struct drm_crtc_state *
  2174. drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
  2175. {
  2176. struct drm_crtc_state *state;
  2177. if (WARN_ON(!crtc->state))
  2178. return NULL;
  2179. state = kmalloc(sizeof(*state), GFP_KERNEL);
  2180. if (state)
  2181. __drm_atomic_helper_crtc_duplicate_state(crtc, state);
  2182. return state;
  2183. }
  2184. EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
  2185. /**
  2186. * __drm_atomic_helper_crtc_destroy_state - release CRTC state
  2187. * @state: CRTC state object to release
  2188. *
  2189. * Releases all resources stored in the CRTC state without actually freeing
  2190. * the memory of the CRTC state. This is useful for drivers that subclass the
  2191. * CRTC state.
  2192. */
  2193. void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
  2194. {
  2195. drm_property_unreference_blob(state->mode_blob);
  2196. drm_property_unreference_blob(state->degamma_lut);
  2197. drm_property_unreference_blob(state->ctm);
  2198. drm_property_unreference_blob(state->gamma_lut);
  2199. }
  2200. EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
  2201. /**
  2202. * drm_atomic_helper_crtc_destroy_state - default state destroy hook
  2203. * @crtc: drm CRTC
  2204. * @state: CRTC state object to release
  2205. *
  2206. * Default CRTC state destroy hook for drivers which don't have their own
  2207. * subclassed CRTC state structure.
  2208. */
  2209. void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
  2210. struct drm_crtc_state *state)
  2211. {
  2212. __drm_atomic_helper_crtc_destroy_state(state);
  2213. kfree(state);
  2214. }
  2215. EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
  2216. /**
  2217. * drm_atomic_helper_plane_reset - default ->reset hook for planes
  2218. * @plane: drm plane
  2219. *
  2220. * Resets the atomic state for @plane by freeing the state pointer (which might
  2221. * be NULL, e.g. at driver load time) and allocating a new empty state object.
  2222. */
  2223. void drm_atomic_helper_plane_reset(struct drm_plane *plane)
  2224. {
  2225. if (plane->state)
  2226. __drm_atomic_helper_plane_destroy_state(plane->state);
  2227. kfree(plane->state);
  2228. plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
  2229. if (plane->state) {
  2230. plane->state->plane = plane;
  2231. plane->state->rotation = BIT(DRM_ROTATE_0);
  2232. }
  2233. }
  2234. EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
  2235. /**
  2236. * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
  2237. * @plane: plane object
  2238. * @state: atomic plane state
  2239. *
  2240. * Copies atomic state from a plane's current state. This is useful for
  2241. * drivers that subclass the plane state.
  2242. */
  2243. void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
  2244. struct drm_plane_state *state)
  2245. {
  2246. memcpy(state, plane->state, sizeof(*state));
  2247. if (state->fb)
  2248. drm_framebuffer_reference(state->fb);
  2249. }
  2250. EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
  2251. /**
  2252. * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
  2253. * @plane: drm plane
  2254. *
  2255. * Default plane state duplicate hook for drivers which don't have their own
  2256. * subclassed plane state structure.
  2257. */
  2258. struct drm_plane_state *
  2259. drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
  2260. {
  2261. struct drm_plane_state *state;
  2262. if (WARN_ON(!plane->state))
  2263. return NULL;
  2264. state = kmalloc(sizeof(*state), GFP_KERNEL);
  2265. if (state)
  2266. __drm_atomic_helper_plane_duplicate_state(plane, state);
  2267. return state;
  2268. }
  2269. EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
  2270. /**
  2271. * __drm_atomic_helper_plane_destroy_state - release plane state
  2272. * @state: plane state object to release
  2273. *
  2274. * Releases all resources stored in the plane state without actually freeing
  2275. * the memory of the plane state. This is useful for drivers that subclass the
  2276. * plane state.
  2277. */
  2278. void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
  2279. {
  2280. if (state->fb)
  2281. drm_framebuffer_unreference(state->fb);
  2282. }
  2283. EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
  2284. /**
  2285. * drm_atomic_helper_plane_destroy_state - default state destroy hook
  2286. * @plane: drm plane
  2287. * @state: plane state object to release
  2288. *
  2289. * Default plane state destroy hook for drivers which don't have their own
  2290. * subclassed plane state structure.
  2291. */
  2292. void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
  2293. struct drm_plane_state *state)
  2294. {
  2295. __drm_atomic_helper_plane_destroy_state(state);
  2296. kfree(state);
  2297. }
  2298. EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
  2299. /**
  2300. * __drm_atomic_helper_connector_reset - reset state on connector
  2301. * @connector: drm connector
  2302. * @conn_state: connector state to assign
  2303. *
  2304. * Initializes the newly allocated @conn_state and assigns it to
  2305. * #connector ->state, usually required when initializing the drivers
  2306. * or when called from the ->reset hook.
  2307. *
  2308. * This is useful for drivers that subclass the connector state.
  2309. */
  2310. void
  2311. __drm_atomic_helper_connector_reset(struct drm_connector *connector,
  2312. struct drm_connector_state *conn_state)
  2313. {
  2314. if (conn_state)
  2315. conn_state->connector = connector;
  2316. connector->state = conn_state;
  2317. }
  2318. EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
  2319. /**
  2320. * drm_atomic_helper_connector_reset - default ->reset hook for connectors
  2321. * @connector: drm connector
  2322. *
  2323. * Resets the atomic state for @connector by freeing the state pointer (which
  2324. * might be NULL, e.g. at driver load time) and allocating a new empty state
  2325. * object.
  2326. */
  2327. void drm_atomic_helper_connector_reset(struct drm_connector *connector)
  2328. {
  2329. struct drm_connector_state *conn_state =
  2330. kzalloc(sizeof(*conn_state), GFP_KERNEL);
  2331. if (connector->state)
  2332. __drm_atomic_helper_connector_destroy_state(connector->state);
  2333. kfree(connector->state);
  2334. __drm_atomic_helper_connector_reset(connector, conn_state);
  2335. }
  2336. EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
  2337. /**
  2338. * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
  2339. * @connector: connector object
  2340. * @state: atomic connector state
  2341. *
  2342. * Copies atomic state from a connector's current state. This is useful for
  2343. * drivers that subclass the connector state.
  2344. */
  2345. void
  2346. __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
  2347. struct drm_connector_state *state)
  2348. {
  2349. memcpy(state, connector->state, sizeof(*state));
  2350. if (state->crtc)
  2351. drm_connector_reference(connector);
  2352. }
  2353. EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
  2354. /**
  2355. * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
  2356. * @connector: drm connector
  2357. *
  2358. * Default connector state duplicate hook for drivers which don't have their own
  2359. * subclassed connector state structure.
  2360. */
  2361. struct drm_connector_state *
  2362. drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
  2363. {
  2364. struct drm_connector_state *state;
  2365. if (WARN_ON(!connector->state))
  2366. return NULL;
  2367. state = kmalloc(sizeof(*state), GFP_KERNEL);
  2368. if (state)
  2369. __drm_atomic_helper_connector_duplicate_state(connector, state);
  2370. return state;
  2371. }
  2372. EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
  2373. /**
  2374. * drm_atomic_helper_duplicate_state - duplicate an atomic state object
  2375. * @dev: DRM device
  2376. * @ctx: lock acquisition context
  2377. *
  2378. * Makes a copy of the current atomic state by looping over all objects and
  2379. * duplicating their respective states. This is used for example by suspend/
  2380. * resume support code to save the state prior to suspend such that it can
  2381. * be restored upon resume.
  2382. *
  2383. * Note that this treats atomic state as persistent between save and restore.
  2384. * Drivers must make sure that this is possible and won't result in confusion
  2385. * or erroneous behaviour.
  2386. *
  2387. * Note that if callers haven't already acquired all modeset locks this might
  2388. * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
  2389. *
  2390. * Returns:
  2391. * A pointer to the copy of the atomic state object on success or an
  2392. * ERR_PTR()-encoded error code on failure.
  2393. *
  2394. * See also:
  2395. * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
  2396. */
  2397. struct drm_atomic_state *
  2398. drm_atomic_helper_duplicate_state(struct drm_device *dev,
  2399. struct drm_modeset_acquire_ctx *ctx)
  2400. {
  2401. struct drm_atomic_state *state;
  2402. struct drm_connector *conn;
  2403. struct drm_plane *plane;
  2404. struct drm_crtc *crtc;
  2405. int err = 0;
  2406. state = drm_atomic_state_alloc(dev);
  2407. if (!state)
  2408. return ERR_PTR(-ENOMEM);
  2409. state->acquire_ctx = ctx;
  2410. drm_for_each_crtc(crtc, dev) {
  2411. struct drm_crtc_state *crtc_state;
  2412. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  2413. if (IS_ERR(crtc_state)) {
  2414. err = PTR_ERR(crtc_state);
  2415. goto free;
  2416. }
  2417. }
  2418. drm_for_each_plane(plane, dev) {
  2419. struct drm_plane_state *plane_state;
  2420. plane_state = drm_atomic_get_plane_state(state, plane);
  2421. if (IS_ERR(plane_state)) {
  2422. err = PTR_ERR(plane_state);
  2423. goto free;
  2424. }
  2425. }
  2426. drm_for_each_connector(conn, dev) {
  2427. struct drm_connector_state *conn_state;
  2428. conn_state = drm_atomic_get_connector_state(state, conn);
  2429. if (IS_ERR(conn_state)) {
  2430. err = PTR_ERR(conn_state);
  2431. goto free;
  2432. }
  2433. }
  2434. /* clear the acquire context so that it isn't accidentally reused */
  2435. state->acquire_ctx = NULL;
  2436. free:
  2437. if (err < 0) {
  2438. drm_atomic_state_free(state);
  2439. state = ERR_PTR(err);
  2440. }
  2441. return state;
  2442. }
  2443. EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
  2444. /**
  2445. * __drm_atomic_helper_connector_destroy_state - release connector state
  2446. * @state: connector state object to release
  2447. *
  2448. * Releases all resources stored in the connector state without actually
  2449. * freeing the memory of the connector state. This is useful for drivers that
  2450. * subclass the connector state.
  2451. */
  2452. void
  2453. __drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
  2454. {
  2455. /*
  2456. * This is currently a placeholder so that drivers that subclass the
  2457. * state will automatically do the right thing if code is ever added
  2458. * to this function.
  2459. */
  2460. if (state->crtc)
  2461. drm_connector_unreference(state->connector);
  2462. }
  2463. EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
  2464. /**
  2465. * drm_atomic_helper_connector_destroy_state - default state destroy hook
  2466. * @connector: drm connector
  2467. * @state: connector state object to release
  2468. *
  2469. * Default connector state destroy hook for drivers which don't have their own
  2470. * subclassed connector state structure.
  2471. */
  2472. void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
  2473. struct drm_connector_state *state)
  2474. {
  2475. __drm_atomic_helper_connector_destroy_state(state);
  2476. kfree(state);
  2477. }
  2478. EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
  2479. /**
  2480. * drm_atomic_helper_legacy_gamma_set - set the legacy gamma correction table
  2481. * @crtc: CRTC object
  2482. * @red: red correction table
  2483. * @green: green correction table
  2484. * @blue: green correction table
  2485. * @start:
  2486. * @size: size of the tables
  2487. *
  2488. * Implements support for legacy gamma correction table for drivers
  2489. * that support color management through the DEGAMMA_LUT/GAMMA_LUT
  2490. * properties.
  2491. */
  2492. void drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
  2493. u16 *red, u16 *green, u16 *blue,
  2494. uint32_t start, uint32_t size)
  2495. {
  2496. struct drm_device *dev = crtc->dev;
  2497. struct drm_mode_config *config = &dev->mode_config;
  2498. struct drm_atomic_state *state;
  2499. struct drm_crtc_state *crtc_state;
  2500. struct drm_property_blob *blob = NULL;
  2501. struct drm_color_lut *blob_data;
  2502. int i, ret = 0;
  2503. state = drm_atomic_state_alloc(crtc->dev);
  2504. if (!state)
  2505. return;
  2506. blob = drm_property_create_blob(dev,
  2507. sizeof(struct drm_color_lut) * size,
  2508. NULL);
  2509. if (IS_ERR(blob)) {
  2510. ret = PTR_ERR(blob);
  2511. blob = NULL;
  2512. goto fail;
  2513. }
  2514. /* Prepare GAMMA_LUT with the legacy values. */
  2515. blob_data = (struct drm_color_lut *) blob->data;
  2516. for (i = 0; i < size; i++) {
  2517. blob_data[i].red = red[i];
  2518. blob_data[i].green = green[i];
  2519. blob_data[i].blue = blue[i];
  2520. }
  2521. state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
  2522. retry:
  2523. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  2524. if (IS_ERR(crtc_state)) {
  2525. ret = PTR_ERR(crtc_state);
  2526. goto fail;
  2527. }
  2528. /* Reset DEGAMMA_LUT and CTM properties. */
  2529. ret = drm_atomic_crtc_set_property(crtc, crtc_state,
  2530. config->degamma_lut_property, 0);
  2531. if (ret)
  2532. goto fail;
  2533. ret = drm_atomic_crtc_set_property(crtc, crtc_state,
  2534. config->ctm_property, 0);
  2535. if (ret)
  2536. goto fail;
  2537. ret = drm_atomic_crtc_set_property(crtc, crtc_state,
  2538. config->gamma_lut_property, blob->base.id);
  2539. if (ret)
  2540. goto fail;
  2541. ret = drm_atomic_commit(state);
  2542. if (ret)
  2543. goto fail;
  2544. /* Driver takes ownership of state on successful commit. */
  2545. drm_property_unreference_blob(blob);
  2546. return;
  2547. fail:
  2548. if (ret == -EDEADLK)
  2549. goto backoff;
  2550. drm_atomic_state_free(state);
  2551. drm_property_unreference_blob(blob);
  2552. return;
  2553. backoff:
  2554. drm_atomic_state_clear(state);
  2555. drm_atomic_legacy_backoff(state);
  2556. goto retry;
  2557. }
  2558. EXPORT_SYMBOL(drm_atomic_helper_legacy_gamma_set);