drm_atomic_helper.c 100 KB

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