drm_atomic_helper.c 79 KB

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