drm_atomic_helper.c 84 KB

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