drm_atomic_helper.c 107 KB

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