drm_atomic_helper.c 99 KB

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