drm_atomic_helper.c 117 KB

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