drm_atomic_helper.c 78 KB

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