drm_atomic_helper.c 99 KB

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