drm_atomic_helper.c 103 KB

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