drm_atomic_helper.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336
  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 expose
  45. * 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. static void
  55. drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
  56. struct drm_plane_state *plane_state,
  57. struct drm_plane *plane)
  58. {
  59. struct drm_crtc_state *crtc_state;
  60. if (plane->state->crtc) {
  61. crtc_state = state->crtc_states[drm_crtc_index(plane->state->crtc)];
  62. if (WARN_ON(!crtc_state))
  63. return;
  64. crtc_state->planes_changed = true;
  65. }
  66. if (plane_state->crtc) {
  67. crtc_state =
  68. state->crtc_states[drm_crtc_index(plane_state->crtc)];
  69. if (WARN_ON(!crtc_state))
  70. return;
  71. crtc_state->planes_changed = true;
  72. }
  73. }
  74. static struct drm_crtc *
  75. get_current_crtc_for_encoder(struct drm_device *dev,
  76. struct drm_encoder *encoder)
  77. {
  78. struct drm_mode_config *config = &dev->mode_config;
  79. struct drm_connector *connector;
  80. WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  81. list_for_each_entry(connector, &config->connector_list, head) {
  82. if (connector->state->best_encoder != encoder)
  83. continue;
  84. return connector->state->crtc;
  85. }
  86. return NULL;
  87. }
  88. static int
  89. steal_encoder(struct drm_atomic_state *state,
  90. struct drm_encoder *encoder,
  91. struct drm_crtc *encoder_crtc)
  92. {
  93. struct drm_mode_config *config = &state->dev->mode_config;
  94. struct drm_crtc_state *crtc_state;
  95. struct drm_connector *connector;
  96. struct drm_connector_state *connector_state;
  97. int ret;
  98. /*
  99. * We can only steal an encoder coming from a connector, which means we
  100. * must already hold the connection_mutex.
  101. */
  102. WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  103. DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d], stealing it\n",
  104. encoder->base.id, encoder->name,
  105. encoder_crtc->base.id);
  106. crtc_state = drm_atomic_get_crtc_state(state, encoder_crtc);
  107. if (IS_ERR(crtc_state))
  108. return PTR_ERR(crtc_state);
  109. crtc_state->mode_changed = true;
  110. list_for_each_entry(connector, &config->connector_list, head) {
  111. if (connector->state->best_encoder != encoder)
  112. continue;
  113. DRM_DEBUG_ATOMIC("Stealing encoder from [CONNECTOR:%d:%s]\n",
  114. connector->base.id,
  115. connector->name);
  116. connector_state = drm_atomic_get_connector_state(state,
  117. connector);
  118. if (IS_ERR(connector_state))
  119. return PTR_ERR(connector_state);
  120. ret = drm_atomic_set_crtc_for_connector(connector_state, NULL);
  121. if (ret)
  122. return ret;
  123. connector_state->best_encoder = NULL;
  124. }
  125. return 0;
  126. }
  127. static int
  128. update_connector_routing(struct drm_atomic_state *state, int conn_idx)
  129. {
  130. const struct drm_connector_helper_funcs *funcs;
  131. struct drm_encoder *new_encoder;
  132. struct drm_crtc *encoder_crtc;
  133. struct drm_connector *connector;
  134. struct drm_connector_state *connector_state;
  135. struct drm_crtc_state *crtc_state;
  136. int idx, ret;
  137. connector = state->connectors[conn_idx];
  138. connector_state = state->connector_states[conn_idx];
  139. if (!connector)
  140. return 0;
  141. DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
  142. connector->base.id,
  143. connector->name);
  144. if (connector->state->crtc != connector_state->crtc) {
  145. if (connector->state->crtc) {
  146. idx = drm_crtc_index(connector->state->crtc);
  147. crtc_state = state->crtc_states[idx];
  148. crtc_state->mode_changed = true;
  149. }
  150. if (connector_state->crtc) {
  151. idx = drm_crtc_index(connector_state->crtc);
  152. crtc_state = state->crtc_states[idx];
  153. crtc_state->mode_changed = true;
  154. }
  155. }
  156. if (!connector_state->crtc) {
  157. DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
  158. connector->base.id,
  159. connector->name);
  160. connector_state->best_encoder = NULL;
  161. return 0;
  162. }
  163. funcs = connector->helper_private;
  164. new_encoder = funcs->best_encoder(connector);
  165. if (!new_encoder) {
  166. DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
  167. connector->base.id,
  168. connector->name);
  169. return -EINVAL;
  170. }
  171. if (new_encoder == connector_state->best_encoder) {
  172. DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d]\n",
  173. connector->base.id,
  174. connector->name,
  175. new_encoder->base.id,
  176. new_encoder->name,
  177. connector_state->crtc->base.id);
  178. return 0;
  179. }
  180. encoder_crtc = get_current_crtc_for_encoder(state->dev,
  181. new_encoder);
  182. if (encoder_crtc) {
  183. ret = steal_encoder(state, new_encoder, encoder_crtc);
  184. if (ret) {
  185. DRM_DEBUG_ATOMIC("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
  186. connector->base.id,
  187. connector->name);
  188. return ret;
  189. }
  190. }
  191. connector_state->best_encoder = new_encoder;
  192. idx = drm_crtc_index(connector_state->crtc);
  193. crtc_state = state->crtc_states[idx];
  194. crtc_state->mode_changed = true;
  195. DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d]\n",
  196. connector->base.id,
  197. connector->name,
  198. new_encoder->base.id,
  199. new_encoder->name,
  200. connector_state->crtc->base.id);
  201. return 0;
  202. }
  203. static int
  204. mode_fixup(struct drm_atomic_state *state)
  205. {
  206. int ncrtcs = state->dev->mode_config.num_crtc;
  207. struct drm_crtc_state *crtc_state;
  208. struct drm_connector_state *conn_state;
  209. int i;
  210. bool ret;
  211. for (i = 0; i < ncrtcs; i++) {
  212. crtc_state = state->crtc_states[i];
  213. if (!crtc_state || !crtc_state->mode_changed)
  214. continue;
  215. drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
  216. }
  217. for (i = 0; i < state->num_connector; i++) {
  218. const struct drm_encoder_helper_funcs *funcs;
  219. struct drm_encoder *encoder;
  220. conn_state = state->connector_states[i];
  221. if (!conn_state)
  222. continue;
  223. WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
  224. if (!conn_state->crtc || !conn_state->best_encoder)
  225. continue;
  226. crtc_state =
  227. state->crtc_states[drm_crtc_index(conn_state->crtc)];
  228. /*
  229. * Each encoder has at most one connector (since we always steal
  230. * it away), so we won't call ->mode_fixup twice.
  231. */
  232. encoder = conn_state->best_encoder;
  233. funcs = encoder->helper_private;
  234. if (encoder->bridge && encoder->bridge->funcs->mode_fixup) {
  235. ret = encoder->bridge->funcs->mode_fixup(
  236. encoder->bridge, &crtc_state->mode,
  237. &crtc_state->adjusted_mode);
  238. if (!ret) {
  239. DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
  240. return -EINVAL;
  241. }
  242. }
  243. if (funcs->atomic_check) {
  244. ret = funcs->atomic_check(encoder, crtc_state,
  245. conn_state);
  246. if (ret) {
  247. DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
  248. encoder->base.id, encoder->name);
  249. return ret;
  250. }
  251. } else {
  252. ret = funcs->mode_fixup(encoder, &crtc_state->mode,
  253. &crtc_state->adjusted_mode);
  254. if (!ret) {
  255. DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
  256. encoder->base.id, encoder->name);
  257. return -EINVAL;
  258. }
  259. }
  260. }
  261. for (i = 0; i < ncrtcs; i++) {
  262. const struct drm_crtc_helper_funcs *funcs;
  263. struct drm_crtc *crtc;
  264. crtc_state = state->crtc_states[i];
  265. crtc = state->crtcs[i];
  266. if (!crtc_state || !crtc_state->mode_changed)
  267. continue;
  268. funcs = crtc->helper_private;
  269. ret = funcs->mode_fixup(crtc, &crtc_state->mode,
  270. &crtc_state->adjusted_mode);
  271. if (!ret) {
  272. DRM_DEBUG_ATOMIC("[CRTC:%d] fixup failed\n",
  273. crtc->base.id);
  274. return -EINVAL;
  275. }
  276. }
  277. return 0;
  278. }
  279. static bool
  280. needs_modeset(struct drm_crtc_state *state)
  281. {
  282. return state->mode_changed || state->active_changed;
  283. }
  284. /**
  285. * drm_atomic_helper_check_modeset - validate state object for modeset changes
  286. * @dev: DRM device
  287. * @state: the driver state object
  288. *
  289. * Check the state object to see if the requested state is physically possible.
  290. * This does all the crtc and connector related computations for an atomic
  291. * update. It computes and updates crtc_state->mode_changed, adds any additional
  292. * connectors needed for full modesets and calls down into ->mode_fixup
  293. * functions of the driver backend.
  294. *
  295. * IMPORTANT:
  296. *
  297. * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
  298. * plane update can't be done without a full modeset) _must_ call this function
  299. * afterwards after that change. It is permitted to call this function multiple
  300. * times for the same update, e.g. when the ->atomic_check functions depend upon
  301. * the adjusted dotclock for fifo space allocation and watermark computation.
  302. *
  303. * RETURNS
  304. * Zero for success or -errno
  305. */
  306. int
  307. drm_atomic_helper_check_modeset(struct drm_device *dev,
  308. struct drm_atomic_state *state)
  309. {
  310. int ncrtcs = dev->mode_config.num_crtc;
  311. struct drm_crtc *crtc;
  312. struct drm_crtc_state *crtc_state;
  313. int i, ret;
  314. for (i = 0; i < ncrtcs; i++) {
  315. crtc = state->crtcs[i];
  316. crtc_state = state->crtc_states[i];
  317. if (!crtc)
  318. continue;
  319. if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
  320. DRM_DEBUG_ATOMIC("[CRTC:%d] mode changed\n",
  321. crtc->base.id);
  322. crtc_state->mode_changed = true;
  323. }
  324. if (crtc->state->enable != crtc_state->enable) {
  325. DRM_DEBUG_ATOMIC("[CRTC:%d] enable changed\n",
  326. crtc->base.id);
  327. crtc_state->mode_changed = true;
  328. }
  329. }
  330. for (i = 0; i < state->num_connector; i++) {
  331. /*
  332. * This only sets crtc->mode_changed for routing changes,
  333. * drivers must set crtc->mode_changed themselves when connector
  334. * properties need to be updated.
  335. */
  336. ret = update_connector_routing(state, i);
  337. if (ret)
  338. return ret;
  339. }
  340. /*
  341. * After all the routing has been prepared we need to add in any
  342. * connector which is itself unchanged, but who's crtc changes it's
  343. * configuration. This must be done before calling mode_fixup in case a
  344. * crtc only changed its mode but has the same set of connectors.
  345. */
  346. for (i = 0; i < ncrtcs; i++) {
  347. int num_connectors;
  348. crtc = state->crtcs[i];
  349. crtc_state = state->crtc_states[i];
  350. if (!crtc)
  351. continue;
  352. /*
  353. * We must set ->active_changed after walking connectors for
  354. * otherwise an update that only changes active would result in
  355. * a full modeset because update_connector_routing force that.
  356. */
  357. if (crtc->state->active != crtc_state->active) {
  358. DRM_DEBUG_ATOMIC("[CRTC:%d] active changed\n",
  359. crtc->base.id);
  360. crtc_state->active_changed = true;
  361. }
  362. if (!needs_modeset(crtc_state))
  363. continue;
  364. DRM_DEBUG_ATOMIC("[CRTC:%d] needs all connectors, enable: %c, active: %c\n",
  365. crtc->base.id,
  366. crtc_state->enable ? 'y' : 'n',
  367. crtc_state->active ? 'y' : 'n');
  368. ret = drm_atomic_add_affected_connectors(state, crtc);
  369. if (ret != 0)
  370. return ret;
  371. num_connectors = drm_atomic_connectors_for_crtc(state,
  372. crtc);
  373. if (crtc_state->enable != !!num_connectors) {
  374. DRM_DEBUG_ATOMIC("[CRTC:%d] enabled/connectors mismatch\n",
  375. crtc->base.id);
  376. return -EINVAL;
  377. }
  378. }
  379. return mode_fixup(state);
  380. }
  381. EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
  382. /**
  383. * drm_atomic_helper_check_planes - validate state object for planes changes
  384. * @dev: DRM device
  385. * @state: the driver state object
  386. *
  387. * Check the state object to see if the requested state is physically possible.
  388. * This does all the plane update related checks using by calling into the
  389. * ->atomic_check hooks provided by the driver.
  390. *
  391. * RETURNS
  392. * Zero for success or -errno
  393. */
  394. int
  395. drm_atomic_helper_check_planes(struct drm_device *dev,
  396. struct drm_atomic_state *state)
  397. {
  398. int nplanes = dev->mode_config.num_total_plane;
  399. int ncrtcs = dev->mode_config.num_crtc;
  400. int i, ret = 0;
  401. for (i = 0; i < nplanes; i++) {
  402. const struct drm_plane_helper_funcs *funcs;
  403. struct drm_plane *plane = state->planes[i];
  404. struct drm_plane_state *plane_state = state->plane_states[i];
  405. if (!plane)
  406. continue;
  407. funcs = plane->helper_private;
  408. drm_atomic_helper_plane_changed(state, plane_state, plane);
  409. if (!funcs || !funcs->atomic_check)
  410. continue;
  411. ret = funcs->atomic_check(plane, plane_state);
  412. if (ret) {
  413. DRM_DEBUG_ATOMIC("[PLANE:%d] atomic driver check failed\n",
  414. plane->base.id);
  415. return ret;
  416. }
  417. }
  418. for (i = 0; i < ncrtcs; i++) {
  419. const struct drm_crtc_helper_funcs *funcs;
  420. struct drm_crtc *crtc = state->crtcs[i];
  421. if (!crtc)
  422. continue;
  423. funcs = crtc->helper_private;
  424. if (!funcs || !funcs->atomic_check)
  425. continue;
  426. ret = funcs->atomic_check(crtc, state->crtc_states[i]);
  427. if (ret) {
  428. DRM_DEBUG_ATOMIC("[CRTC:%d] atomic driver check failed\n",
  429. crtc->base.id);
  430. return ret;
  431. }
  432. }
  433. return ret;
  434. }
  435. EXPORT_SYMBOL(drm_atomic_helper_check_planes);
  436. /**
  437. * drm_atomic_helper_check - validate state object
  438. * @dev: DRM device
  439. * @state: the driver state object
  440. *
  441. * Check the state object to see if the requested state is physically possible.
  442. * Only crtcs and planes have check callbacks, so for any additional (global)
  443. * checking that a driver needs it can simply wrap that around this function.
  444. * Drivers without such needs can directly use this as their ->atomic_check()
  445. * callback.
  446. *
  447. * This just wraps the two parts of the state checking for planes and modeset
  448. * state in the default order: First it calls drm_atomic_helper_check_modeset()
  449. * and then drm_atomic_helper_check_planes(). The assumption is that the
  450. * ->atomic_check functions depend upon an updated adjusted_mode.clock to
  451. * e.g. properly compute watermarks.
  452. *
  453. * RETURNS
  454. * Zero for success or -errno
  455. */
  456. int drm_atomic_helper_check(struct drm_device *dev,
  457. struct drm_atomic_state *state)
  458. {
  459. int ret;
  460. ret = drm_atomic_helper_check_modeset(dev, state);
  461. if (ret)
  462. return ret;
  463. ret = drm_atomic_helper_check_planes(dev, state);
  464. if (ret)
  465. return ret;
  466. return ret;
  467. }
  468. EXPORT_SYMBOL(drm_atomic_helper_check);
  469. static void
  470. disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
  471. {
  472. int ncrtcs = old_state->dev->mode_config.num_crtc;
  473. int i;
  474. for (i = 0; i < old_state->num_connector; i++) {
  475. const struct drm_encoder_helper_funcs *funcs;
  476. struct drm_connector_state *old_conn_state;
  477. struct drm_connector *connector;
  478. struct drm_encoder *encoder;
  479. struct drm_crtc_state *old_crtc_state;
  480. old_conn_state = old_state->connector_states[i];
  481. connector = old_state->connectors[i];
  482. /* Shut down everything that's in the changeset and currently
  483. * still on. So need to check the old, saved state. */
  484. if (!old_conn_state || !old_conn_state->crtc)
  485. continue;
  486. old_crtc_state = old_state->crtc_states[drm_crtc_index(old_conn_state->crtc)];
  487. if (!old_crtc_state->active ||
  488. !needs_modeset(old_conn_state->crtc->state))
  489. continue;
  490. encoder = old_conn_state->best_encoder;
  491. /* We shouldn't get this far if we didn't previously have
  492. * an encoder.. but WARN_ON() rather than explode.
  493. */
  494. if (WARN_ON(!encoder))
  495. continue;
  496. funcs = encoder->helper_private;
  497. DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
  498. encoder->base.id, encoder->name);
  499. /*
  500. * Each encoder has at most one connector (since we always steal
  501. * it away), so we won't call disable hooks twice.
  502. */
  503. if (encoder->bridge)
  504. encoder->bridge->funcs->disable(encoder->bridge);
  505. /* Right function depends upon target state. */
  506. if (connector->state->crtc && funcs->prepare)
  507. funcs->prepare(encoder);
  508. else if (funcs->disable)
  509. funcs->disable(encoder);
  510. else
  511. funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  512. if (encoder->bridge)
  513. encoder->bridge->funcs->post_disable(encoder->bridge);
  514. }
  515. for (i = 0; i < ncrtcs; i++) {
  516. const struct drm_crtc_helper_funcs *funcs;
  517. struct drm_crtc *crtc;
  518. struct drm_crtc_state *old_crtc_state;
  519. crtc = old_state->crtcs[i];
  520. old_crtc_state = old_state->crtc_states[i];
  521. /* Shut down everything that needs a full modeset. */
  522. if (!crtc || !needs_modeset(crtc->state))
  523. continue;
  524. if (!old_crtc_state->active)
  525. continue;
  526. funcs = crtc->helper_private;
  527. DRM_DEBUG_ATOMIC("disabling [CRTC:%d]\n",
  528. crtc->base.id);
  529. /* Right function depends upon target state. */
  530. if (crtc->state->enable && funcs->prepare)
  531. funcs->prepare(crtc);
  532. else if (funcs->disable)
  533. funcs->disable(crtc);
  534. else
  535. funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  536. }
  537. }
  538. static void
  539. set_routing_links(struct drm_device *dev, struct drm_atomic_state *old_state)
  540. {
  541. int ncrtcs = old_state->dev->mode_config.num_crtc;
  542. int i;
  543. /* clear out existing links */
  544. for (i = 0; i < old_state->num_connector; i++) {
  545. struct drm_connector *connector;
  546. connector = old_state->connectors[i];
  547. if (!connector || !connector->encoder)
  548. continue;
  549. WARN_ON(!connector->encoder->crtc);
  550. connector->encoder->crtc = NULL;
  551. connector->encoder = NULL;
  552. }
  553. /* set new links */
  554. for (i = 0; i < old_state->num_connector; i++) {
  555. struct drm_connector *connector;
  556. connector = old_state->connectors[i];
  557. if (!connector || !connector->state->crtc)
  558. continue;
  559. if (WARN_ON(!connector->state->best_encoder))
  560. continue;
  561. connector->encoder = connector->state->best_encoder;
  562. connector->encoder->crtc = connector->state->crtc;
  563. }
  564. /* set legacy state in the crtc structure */
  565. for (i = 0; i < ncrtcs; i++) {
  566. struct drm_crtc *crtc;
  567. crtc = old_state->crtcs[i];
  568. if (!crtc)
  569. continue;
  570. crtc->mode = crtc->state->mode;
  571. crtc->enabled = crtc->state->enable;
  572. crtc->x = crtc->primary->state->src_x >> 16;
  573. crtc->y = crtc->primary->state->src_y >> 16;
  574. }
  575. }
  576. static void
  577. crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
  578. {
  579. int ncrtcs = old_state->dev->mode_config.num_crtc;
  580. int i;
  581. for (i = 0; i < ncrtcs; i++) {
  582. const struct drm_crtc_helper_funcs *funcs;
  583. struct drm_crtc *crtc;
  584. crtc = old_state->crtcs[i];
  585. if (!crtc || !crtc->state->mode_changed)
  586. continue;
  587. funcs = crtc->helper_private;
  588. if (crtc->state->enable && funcs->mode_set_nofb) {
  589. DRM_DEBUG_ATOMIC("modeset on [CRTC:%d]\n",
  590. crtc->base.id);
  591. funcs->mode_set_nofb(crtc);
  592. }
  593. }
  594. for (i = 0; i < old_state->num_connector; i++) {
  595. const struct drm_encoder_helper_funcs *funcs;
  596. struct drm_connector *connector;
  597. struct drm_crtc_state *new_crtc_state;
  598. struct drm_encoder *encoder;
  599. struct drm_display_mode *mode, *adjusted_mode;
  600. connector = old_state->connectors[i];
  601. if (!connector || !connector->state->best_encoder)
  602. continue;
  603. encoder = connector->state->best_encoder;
  604. funcs = encoder->helper_private;
  605. new_crtc_state = connector->state->crtc->state;
  606. mode = &new_crtc_state->mode;
  607. adjusted_mode = &new_crtc_state->adjusted_mode;
  608. if (!new_crtc_state->mode_changed)
  609. continue;
  610. DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
  611. encoder->base.id, encoder->name);
  612. /*
  613. * Each encoder has at most one connector (since we always steal
  614. * it away), so we won't call mode_set hooks twice.
  615. */
  616. if (funcs->mode_set)
  617. funcs->mode_set(encoder, mode, adjusted_mode);
  618. if (encoder->bridge && encoder->bridge->funcs->mode_set)
  619. encoder->bridge->funcs->mode_set(encoder->bridge,
  620. mode, adjusted_mode);
  621. }
  622. }
  623. /**
  624. * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
  625. * @dev: DRM device
  626. * @old_state: atomic state object with old state structures
  627. *
  628. * This function shuts down all the outputs that need to be shut down and
  629. * prepares them (if required) with the new mode.
  630. *
  631. * For compatability with legacy crtc helpers this should be called before
  632. * drm_atomic_helper_commit_planes(), which is what the default commit function
  633. * does. But drivers with different needs can group the modeset commits together
  634. * and do the plane commits at the end. This is useful for drivers doing runtime
  635. * PM since planes updates then only happen when the CRTC is actually enabled.
  636. */
  637. void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
  638. struct drm_atomic_state *old_state)
  639. {
  640. disable_outputs(dev, old_state);
  641. set_routing_links(dev, old_state);
  642. crtc_set_mode(dev, old_state);
  643. }
  644. EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
  645. /**
  646. * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
  647. * @dev: DRM device
  648. * @old_state: atomic state object with old state structures
  649. *
  650. * This function enables all the outputs with the new configuration which had to
  651. * be turned off for the update.
  652. *
  653. * For compatability with legacy crtc helpers this should be called after
  654. * drm_atomic_helper_commit_planes(), which is what the default commit function
  655. * does. But drivers with different needs can group the modeset commits together
  656. * and do the plane commits at the end. This is useful for drivers doing runtime
  657. * PM since planes updates then only happen when the CRTC is actually enabled.
  658. */
  659. void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
  660. struct drm_atomic_state *old_state)
  661. {
  662. int ncrtcs = old_state->dev->mode_config.num_crtc;
  663. int i;
  664. for (i = 0; i < ncrtcs; i++) {
  665. const struct drm_crtc_helper_funcs *funcs;
  666. struct drm_crtc *crtc;
  667. crtc = old_state->crtcs[i];
  668. /* Need to filter out CRTCs where only planes change. */
  669. if (!crtc || !needs_modeset(crtc->state))
  670. continue;
  671. if (!crtc->state->active)
  672. continue;
  673. funcs = crtc->helper_private;
  674. if (crtc->state->enable) {
  675. DRM_DEBUG_ATOMIC("enabling [CRTC:%d]\n",
  676. crtc->base.id);
  677. if (funcs->enable)
  678. funcs->enable(crtc);
  679. else
  680. funcs->commit(crtc);
  681. }
  682. }
  683. for (i = 0; i < old_state->num_connector; i++) {
  684. const struct drm_encoder_helper_funcs *funcs;
  685. struct drm_connector *connector;
  686. struct drm_encoder *encoder;
  687. connector = old_state->connectors[i];
  688. if (!connector || !connector->state->best_encoder)
  689. continue;
  690. if (!connector->state->crtc->state->active ||
  691. !needs_modeset(connector->state->crtc->state))
  692. continue;
  693. encoder = connector->state->best_encoder;
  694. funcs = encoder->helper_private;
  695. DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
  696. encoder->base.id, encoder->name);
  697. /*
  698. * Each encoder has at most one connector (since we always steal
  699. * it away), so we won't call enable hooks twice.
  700. */
  701. if (encoder->bridge)
  702. encoder->bridge->funcs->pre_enable(encoder->bridge);
  703. if (funcs->enable)
  704. funcs->enable(encoder);
  705. else
  706. funcs->commit(encoder);
  707. if (encoder->bridge)
  708. encoder->bridge->funcs->enable(encoder->bridge);
  709. }
  710. }
  711. EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
  712. static void wait_for_fences(struct drm_device *dev,
  713. struct drm_atomic_state *state)
  714. {
  715. int nplanes = dev->mode_config.num_total_plane;
  716. int i;
  717. for (i = 0; i < nplanes; i++) {
  718. struct drm_plane *plane = state->planes[i];
  719. if (!plane || !plane->state->fence)
  720. continue;
  721. WARN_ON(!plane->state->fb);
  722. fence_wait(plane->state->fence, false);
  723. fence_put(plane->state->fence);
  724. plane->state->fence = NULL;
  725. }
  726. }
  727. static bool framebuffer_changed(struct drm_device *dev,
  728. struct drm_atomic_state *old_state,
  729. struct drm_crtc *crtc)
  730. {
  731. struct drm_plane *plane;
  732. struct drm_plane_state *old_plane_state;
  733. int nplanes = old_state->dev->mode_config.num_total_plane;
  734. int i;
  735. for (i = 0; i < nplanes; i++) {
  736. plane = old_state->planes[i];
  737. old_plane_state = old_state->plane_states[i];
  738. if (!plane)
  739. continue;
  740. if (plane->state->crtc != crtc &&
  741. old_plane_state->crtc != crtc)
  742. continue;
  743. if (plane->state->fb != old_plane_state->fb)
  744. return true;
  745. }
  746. return false;
  747. }
  748. /**
  749. * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
  750. * @dev: DRM device
  751. * @old_state: atomic state object with old state structures
  752. *
  753. * Helper to, after atomic commit, wait for vblanks on all effected
  754. * crtcs (ie. before cleaning up old framebuffers using
  755. * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
  756. * framebuffers have actually changed to optimize for the legacy cursor and
  757. * plane update use-case.
  758. */
  759. void
  760. drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
  761. struct drm_atomic_state *old_state)
  762. {
  763. struct drm_crtc *crtc;
  764. struct drm_crtc_state *old_crtc_state;
  765. int ncrtcs = old_state->dev->mode_config.num_crtc;
  766. int i, ret;
  767. for (i = 0; i < ncrtcs; i++) {
  768. crtc = old_state->crtcs[i];
  769. old_crtc_state = old_state->crtc_states[i];
  770. if (!crtc)
  771. continue;
  772. /* No one cares about the old state, so abuse it for tracking
  773. * and store whether we hold a vblank reference (and should do a
  774. * vblank wait) in the ->enable boolean. */
  775. old_crtc_state->enable = false;
  776. if (!crtc->state->enable)
  777. continue;
  778. /* Legacy cursor ioctls are completely unsynced, and userspace
  779. * relies on that (by doing tons of cursor updates). */
  780. if (old_state->legacy_cursor_update)
  781. continue;
  782. if (!framebuffer_changed(dev, old_state, crtc))
  783. continue;
  784. ret = drm_crtc_vblank_get(crtc);
  785. if (ret != 0)
  786. continue;
  787. old_crtc_state->enable = true;
  788. old_crtc_state->last_vblank_count = drm_vblank_count(dev, i);
  789. }
  790. for (i = 0; i < ncrtcs; i++) {
  791. crtc = old_state->crtcs[i];
  792. old_crtc_state = old_state->crtc_states[i];
  793. if (!crtc || !old_crtc_state->enable)
  794. continue;
  795. ret = wait_event_timeout(dev->vblank[i].queue,
  796. old_crtc_state->last_vblank_count !=
  797. drm_vblank_count(dev, i),
  798. msecs_to_jiffies(50));
  799. drm_crtc_vblank_put(crtc);
  800. }
  801. }
  802. EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
  803. /**
  804. * drm_atomic_helper_commit - commit validated state object
  805. * @dev: DRM device
  806. * @state: the driver state object
  807. * @async: asynchronous commit
  808. *
  809. * This function commits a with drm_atomic_helper_check() pre-validated state
  810. * object. This can still fail when e.g. the framebuffer reservation fails. For
  811. * now this doesn't implement asynchronous commits.
  812. *
  813. * RETURNS
  814. * Zero for success or -errno.
  815. */
  816. int drm_atomic_helper_commit(struct drm_device *dev,
  817. struct drm_atomic_state *state,
  818. bool async)
  819. {
  820. int ret;
  821. if (async)
  822. return -EBUSY;
  823. ret = drm_atomic_helper_prepare_planes(dev, state);
  824. if (ret)
  825. return ret;
  826. /*
  827. * This is the point of no return - everything below never fails except
  828. * when the hw goes bonghits. Which means we can commit the new state on
  829. * the software side now.
  830. */
  831. drm_atomic_helper_swap_state(dev, state);
  832. /*
  833. * Everything below can be run asynchronously without the need to grab
  834. * any modeset locks at all under one condition: It must be guaranteed
  835. * that the asynchronous work has either been cancelled (if the driver
  836. * supports it, which at least requires that the framebuffers get
  837. * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
  838. * before the new state gets committed on the software side with
  839. * drm_atomic_helper_swap_state().
  840. *
  841. * This scheme allows new atomic state updates to be prepared and
  842. * checked in parallel to the asynchronous completion of the previous
  843. * update. Which is important since compositors need to figure out the
  844. * composition of the next frame right after having submitted the
  845. * current layout.
  846. */
  847. wait_for_fences(dev, state);
  848. drm_atomic_helper_commit_modeset_disables(dev, state);
  849. drm_atomic_helper_commit_planes(dev, state);
  850. drm_atomic_helper_commit_modeset_enables(dev, state);
  851. drm_atomic_helper_wait_for_vblanks(dev, state);
  852. drm_atomic_helper_cleanup_planes(dev, state);
  853. drm_atomic_state_free(state);
  854. return 0;
  855. }
  856. EXPORT_SYMBOL(drm_atomic_helper_commit);
  857. /**
  858. * DOC: implementing async commit
  859. *
  860. * For now the atomic helpers don't support async commit directly. If there is
  861. * real need it could be added though, using the dma-buf fence infrastructure
  862. * for generic synchronization with outstanding rendering.
  863. *
  864. * For now drivers have to implement async commit themselves, with the following
  865. * sequence being the recommended one:
  866. *
  867. * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
  868. * which commit needs to call which can fail, so we want to run it first and
  869. * synchronously.
  870. *
  871. * 2. Synchronize with any outstanding asynchronous commit worker threads which
  872. * might be affected the new state update. This can be done by either cancelling
  873. * or flushing the work items, depending upon whether the driver can deal with
  874. * cancelled updates. Note that it is important to ensure that the framebuffer
  875. * cleanup is still done when cancelling.
  876. *
  877. * For sufficient parallelism it is recommended to have a work item per crtc
  878. * (for updates which don't touch global state) and a global one. Then we only
  879. * need to synchronize with the crtc work items for changed crtcs and the global
  880. * work item, which allows nice concurrent updates on disjoint sets of crtcs.
  881. *
  882. * 3. The software state is updated synchronously with
  883. * drm_atomic_helper_swap_state. Doing this under the protection of all modeset
  884. * locks means concurrent callers never see inconsistent state. And doing this
  885. * while it's guaranteed that no relevant async worker runs means that async
  886. * workers do not need grab any locks. Actually they must not grab locks, for
  887. * otherwise the work flushing will deadlock.
  888. *
  889. * 4. Schedule a work item to do all subsequent steps, using the split-out
  890. * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
  891. * then cleaning up the framebuffers after the old framebuffer is no longer
  892. * being displayed.
  893. */
  894. /**
  895. * drm_atomic_helper_prepare_planes - prepare plane resources before commit
  896. * @dev: DRM device
  897. * @state: atomic state object with new state structures
  898. *
  899. * This function prepares plane state, specifically framebuffers, for the new
  900. * configuration. If any failure is encountered this function will call
  901. * ->cleanup_fb on any already successfully prepared framebuffer.
  902. *
  903. * Returns:
  904. * 0 on success, negative error code on failure.
  905. */
  906. int drm_atomic_helper_prepare_planes(struct drm_device *dev,
  907. struct drm_atomic_state *state)
  908. {
  909. int nplanes = dev->mode_config.num_total_plane;
  910. int ret, i;
  911. for (i = 0; i < nplanes; i++) {
  912. const struct drm_plane_helper_funcs *funcs;
  913. struct drm_plane *plane = state->planes[i];
  914. struct drm_plane_state *plane_state = state->plane_states[i];
  915. struct drm_framebuffer *fb;
  916. if (!plane)
  917. continue;
  918. funcs = plane->helper_private;
  919. fb = plane_state->fb;
  920. if (fb && funcs->prepare_fb) {
  921. ret = funcs->prepare_fb(plane, fb, plane_state);
  922. if (ret)
  923. goto fail;
  924. }
  925. }
  926. return 0;
  927. fail:
  928. for (i--; i >= 0; i--) {
  929. const struct drm_plane_helper_funcs *funcs;
  930. struct drm_plane *plane = state->planes[i];
  931. struct drm_plane_state *plane_state = state->plane_states[i];
  932. struct drm_framebuffer *fb;
  933. if (!plane)
  934. continue;
  935. funcs = plane->helper_private;
  936. fb = state->plane_states[i]->fb;
  937. if (fb && funcs->cleanup_fb)
  938. funcs->cleanup_fb(plane, fb, plane_state);
  939. }
  940. return ret;
  941. }
  942. EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
  943. /**
  944. * drm_atomic_helper_commit_planes - commit plane state
  945. * @dev: DRM device
  946. * @old_state: atomic state object with old state structures
  947. *
  948. * This function commits the new plane state using the plane and atomic helper
  949. * functions for planes and crtcs. It assumes that the atomic state has already
  950. * been pushed into the relevant object state pointers, since this step can no
  951. * longer fail.
  952. *
  953. * It still requires the global state object @old_state to know which planes and
  954. * crtcs need to be updated though.
  955. */
  956. void drm_atomic_helper_commit_planes(struct drm_device *dev,
  957. struct drm_atomic_state *old_state)
  958. {
  959. int nplanes = dev->mode_config.num_total_plane;
  960. int ncrtcs = dev->mode_config.num_crtc;
  961. int i;
  962. for (i = 0; i < ncrtcs; i++) {
  963. const struct drm_crtc_helper_funcs *funcs;
  964. struct drm_crtc *crtc = old_state->crtcs[i];
  965. if (!crtc)
  966. continue;
  967. funcs = crtc->helper_private;
  968. if (!funcs || !funcs->atomic_begin)
  969. continue;
  970. funcs->atomic_begin(crtc);
  971. }
  972. for (i = 0; i < nplanes; i++) {
  973. const struct drm_plane_helper_funcs *funcs;
  974. struct drm_plane *plane = old_state->planes[i];
  975. struct drm_plane_state *old_plane_state;
  976. if (!plane)
  977. continue;
  978. funcs = plane->helper_private;
  979. if (!funcs)
  980. continue;
  981. old_plane_state = old_state->plane_states[i];
  982. /*
  983. * Special-case disabling the plane if drivers support it.
  984. */
  985. if (drm_atomic_plane_disabling(plane, old_plane_state) &&
  986. funcs->atomic_disable)
  987. funcs->atomic_disable(plane, old_plane_state);
  988. else
  989. funcs->atomic_update(plane, old_plane_state);
  990. }
  991. for (i = 0; i < ncrtcs; i++) {
  992. const struct drm_crtc_helper_funcs *funcs;
  993. struct drm_crtc *crtc = old_state->crtcs[i];
  994. if (!crtc)
  995. continue;
  996. funcs = crtc->helper_private;
  997. if (!funcs || !funcs->atomic_flush)
  998. continue;
  999. funcs->atomic_flush(crtc);
  1000. }
  1001. }
  1002. EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
  1003. /**
  1004. * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
  1005. * @dev: DRM device
  1006. * @old_state: atomic state object with old state structures
  1007. *
  1008. * This function cleans up plane state, specifically framebuffers, from the old
  1009. * configuration. Hence the old configuration must be perserved in @old_state to
  1010. * be able to call this function.
  1011. *
  1012. * This function must also be called on the new state when the atomic update
  1013. * fails at any point after calling drm_atomic_helper_prepare_planes().
  1014. */
  1015. void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
  1016. struct drm_atomic_state *old_state)
  1017. {
  1018. int nplanes = dev->mode_config.num_total_plane;
  1019. int i;
  1020. for (i = 0; i < nplanes; i++) {
  1021. const struct drm_plane_helper_funcs *funcs;
  1022. struct drm_plane *plane = old_state->planes[i];
  1023. struct drm_plane_state *plane_state = old_state->plane_states[i];
  1024. struct drm_framebuffer *old_fb;
  1025. if (!plane)
  1026. continue;
  1027. funcs = plane->helper_private;
  1028. old_fb = plane_state->fb;
  1029. if (old_fb && funcs->cleanup_fb)
  1030. funcs->cleanup_fb(plane, old_fb, plane_state);
  1031. }
  1032. }
  1033. EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
  1034. /**
  1035. * drm_atomic_helper_swap_state - store atomic state into current sw state
  1036. * @dev: DRM device
  1037. * @state: atomic state
  1038. *
  1039. * This function stores the atomic state into the current state pointers in all
  1040. * driver objects. It should be called after all failing steps have been done
  1041. * and succeeded, but before the actual hardware state is committed.
  1042. *
  1043. * For cleanup and error recovery the current state for all changed objects will
  1044. * be swaped into @state.
  1045. *
  1046. * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
  1047. *
  1048. * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
  1049. *
  1050. * 2. Do any other steps that might fail.
  1051. *
  1052. * 3. Put the staged state into the current state pointers with this function.
  1053. *
  1054. * 4. Actually commit the hardware state.
  1055. *
  1056. * 5. Call drm_atomic_helper_cleanup_planes with @state, which since step 3
  1057. * contains the old state. Also do any other cleanup required with that state.
  1058. */
  1059. void drm_atomic_helper_swap_state(struct drm_device *dev,
  1060. struct drm_atomic_state *state)
  1061. {
  1062. int i;
  1063. for (i = 0; i < dev->mode_config.num_connector; i++) {
  1064. struct drm_connector *connector = state->connectors[i];
  1065. if (!connector)
  1066. continue;
  1067. connector->state->state = state;
  1068. swap(state->connector_states[i], connector->state);
  1069. connector->state->state = NULL;
  1070. }
  1071. for (i = 0; i < dev->mode_config.num_crtc; i++) {
  1072. struct drm_crtc *crtc = state->crtcs[i];
  1073. if (!crtc)
  1074. continue;
  1075. crtc->state->state = state;
  1076. swap(state->crtc_states[i], crtc->state);
  1077. crtc->state->state = NULL;
  1078. }
  1079. for (i = 0; i < dev->mode_config.num_total_plane; i++) {
  1080. struct drm_plane *plane = state->planes[i];
  1081. if (!plane)
  1082. continue;
  1083. plane->state->state = state;
  1084. swap(state->plane_states[i], plane->state);
  1085. plane->state->state = NULL;
  1086. }
  1087. }
  1088. EXPORT_SYMBOL(drm_atomic_helper_swap_state);
  1089. /**
  1090. * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
  1091. * @plane: plane object to update
  1092. * @crtc: owning CRTC of owning plane
  1093. * @fb: framebuffer to flip onto plane
  1094. * @crtc_x: x offset of primary plane on crtc
  1095. * @crtc_y: y offset of primary plane on crtc
  1096. * @crtc_w: width of primary plane rectangle on crtc
  1097. * @crtc_h: height of primary plane rectangle on crtc
  1098. * @src_x: x offset of @fb for panning
  1099. * @src_y: y offset of @fb for panning
  1100. * @src_w: width of source rectangle in @fb
  1101. * @src_h: height of source rectangle in @fb
  1102. *
  1103. * Provides a default plane update handler using the atomic driver interface.
  1104. *
  1105. * RETURNS:
  1106. * Zero on success, error code on failure
  1107. */
  1108. int drm_atomic_helper_update_plane(struct drm_plane *plane,
  1109. struct drm_crtc *crtc,
  1110. struct drm_framebuffer *fb,
  1111. int crtc_x, int crtc_y,
  1112. unsigned int crtc_w, unsigned int crtc_h,
  1113. uint32_t src_x, uint32_t src_y,
  1114. uint32_t src_w, uint32_t src_h)
  1115. {
  1116. struct drm_atomic_state *state;
  1117. struct drm_plane_state *plane_state;
  1118. int ret = 0;
  1119. state = drm_atomic_state_alloc(plane->dev);
  1120. if (!state)
  1121. return -ENOMEM;
  1122. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  1123. retry:
  1124. plane_state = drm_atomic_get_plane_state(state, plane);
  1125. if (IS_ERR(plane_state)) {
  1126. ret = PTR_ERR(plane_state);
  1127. goto fail;
  1128. }
  1129. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  1130. if (ret != 0)
  1131. goto fail;
  1132. drm_atomic_set_fb_for_plane(plane_state, fb);
  1133. plane_state->crtc_x = crtc_x;
  1134. plane_state->crtc_y = crtc_y;
  1135. plane_state->crtc_h = crtc_h;
  1136. plane_state->crtc_w = crtc_w;
  1137. plane_state->src_x = src_x;
  1138. plane_state->src_y = src_y;
  1139. plane_state->src_h = src_h;
  1140. plane_state->src_w = src_w;
  1141. ret = drm_atomic_commit(state);
  1142. if (ret != 0)
  1143. goto fail;
  1144. if (plane == crtc->cursor)
  1145. state->legacy_cursor_update = true;
  1146. /* Driver takes ownership of state on successful commit. */
  1147. return 0;
  1148. fail:
  1149. if (ret == -EDEADLK)
  1150. goto backoff;
  1151. drm_atomic_state_free(state);
  1152. return ret;
  1153. backoff:
  1154. drm_atomic_state_clear(state);
  1155. drm_atomic_legacy_backoff(state);
  1156. /*
  1157. * Someone might have exchanged the framebuffer while we dropped locks
  1158. * in the backoff code. We need to fix up the fb refcount tracking the
  1159. * core does for us.
  1160. */
  1161. plane->old_fb = plane->fb;
  1162. goto retry;
  1163. }
  1164. EXPORT_SYMBOL(drm_atomic_helper_update_plane);
  1165. /**
  1166. * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
  1167. * @plane: plane to disable
  1168. *
  1169. * Provides a default plane disable handler using the atomic driver interface.
  1170. *
  1171. * RETURNS:
  1172. * Zero on success, error code on failure
  1173. */
  1174. int drm_atomic_helper_disable_plane(struct drm_plane *plane)
  1175. {
  1176. struct drm_atomic_state *state;
  1177. struct drm_plane_state *plane_state;
  1178. int ret = 0;
  1179. /*
  1180. * FIXME: Without plane->crtc set we can't get at the implicit legacy
  1181. * acquire context. The real fix will be to wire the acquire ctx through
  1182. * everywhere we need it, but meanwhile prevent chaos by just skipping
  1183. * this noop. The critical case is the cursor ioctls which a) only grab
  1184. * crtc/cursor-plane locks (so we need the crtc to get at the right
  1185. * acquire context) and b) can try to disable the plane multiple times.
  1186. */
  1187. if (!plane->crtc)
  1188. return 0;
  1189. state = drm_atomic_state_alloc(plane->dev);
  1190. if (!state)
  1191. return -ENOMEM;
  1192. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
  1193. retry:
  1194. plane_state = drm_atomic_get_plane_state(state, plane);
  1195. if (IS_ERR(plane_state)) {
  1196. ret = PTR_ERR(plane_state);
  1197. goto fail;
  1198. }
  1199. ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
  1200. if (ret != 0)
  1201. goto fail;
  1202. drm_atomic_set_fb_for_plane(plane_state, NULL);
  1203. plane_state->crtc_x = 0;
  1204. plane_state->crtc_y = 0;
  1205. plane_state->crtc_h = 0;
  1206. plane_state->crtc_w = 0;
  1207. plane_state->src_x = 0;
  1208. plane_state->src_y = 0;
  1209. plane_state->src_h = 0;
  1210. plane_state->src_w = 0;
  1211. if (plane == plane->crtc->cursor)
  1212. state->legacy_cursor_update = true;
  1213. ret = drm_atomic_commit(state);
  1214. if (ret != 0)
  1215. goto fail;
  1216. /* Driver takes ownership of state on successful commit. */
  1217. return 0;
  1218. fail:
  1219. if (ret == -EDEADLK)
  1220. goto backoff;
  1221. drm_atomic_state_free(state);
  1222. return ret;
  1223. backoff:
  1224. drm_atomic_state_clear(state);
  1225. drm_atomic_legacy_backoff(state);
  1226. /*
  1227. * Someone might have exchanged the framebuffer while we dropped locks
  1228. * in the backoff code. We need to fix up the fb refcount tracking the
  1229. * core does for us.
  1230. */
  1231. plane->old_fb = plane->fb;
  1232. goto retry;
  1233. }
  1234. EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
  1235. static int update_output_state(struct drm_atomic_state *state,
  1236. struct drm_mode_set *set)
  1237. {
  1238. struct drm_device *dev = set->crtc->dev;
  1239. struct drm_connector_state *conn_state;
  1240. int ncrtcs = state->dev->mode_config.num_crtc;
  1241. int ret, i, j;
  1242. ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
  1243. state->acquire_ctx);
  1244. if (ret)
  1245. return ret;
  1246. /* First grab all affected connector/crtc states. */
  1247. for (i = 0; i < set->num_connectors; i++) {
  1248. conn_state = drm_atomic_get_connector_state(state,
  1249. set->connectors[i]);
  1250. if (IS_ERR(conn_state))
  1251. return PTR_ERR(conn_state);
  1252. }
  1253. for (i = 0; i < ncrtcs; i++) {
  1254. struct drm_crtc *crtc = state->crtcs[i];
  1255. if (!crtc)
  1256. continue;
  1257. ret = drm_atomic_add_affected_connectors(state, crtc);
  1258. if (ret)
  1259. return ret;
  1260. }
  1261. /* Then recompute connector->crtc links and crtc enabling state. */
  1262. for (i = 0; i < state->num_connector; i++) {
  1263. struct drm_connector *connector;
  1264. connector = state->connectors[i];
  1265. conn_state = state->connector_states[i];
  1266. if (!connector)
  1267. continue;
  1268. if (conn_state->crtc == set->crtc) {
  1269. ret = drm_atomic_set_crtc_for_connector(conn_state,
  1270. NULL);
  1271. if (ret)
  1272. return ret;
  1273. }
  1274. for (j = 0; j < set->num_connectors; j++) {
  1275. if (set->connectors[j] == connector) {
  1276. ret = drm_atomic_set_crtc_for_connector(conn_state,
  1277. set->crtc);
  1278. if (ret)
  1279. return ret;
  1280. break;
  1281. }
  1282. }
  1283. }
  1284. for (i = 0; i < ncrtcs; i++) {
  1285. struct drm_crtc *crtc = state->crtcs[i];
  1286. struct drm_crtc_state *crtc_state = state->crtc_states[i];
  1287. if (!crtc)
  1288. continue;
  1289. /* Don't update ->enable for the CRTC in the set_config request,
  1290. * since a mismatch would indicate a bug in the upper layers.
  1291. * The actual modeset code later on will catch any
  1292. * inconsistencies here. */
  1293. if (crtc == set->crtc)
  1294. continue;
  1295. crtc_state->enable =
  1296. drm_atomic_connectors_for_crtc(state, crtc);
  1297. }
  1298. return 0;
  1299. }
  1300. /**
  1301. * drm_atomic_helper_set_config - set a new config from userspace
  1302. * @set: mode set configuration
  1303. *
  1304. * Provides a default crtc set_config handler using the atomic driver interface.
  1305. *
  1306. * Returns:
  1307. * Returns 0 on success, negative errno numbers on failure.
  1308. */
  1309. int drm_atomic_helper_set_config(struct drm_mode_set *set)
  1310. {
  1311. struct drm_atomic_state *state;
  1312. struct drm_crtc *crtc = set->crtc;
  1313. struct drm_crtc_state *crtc_state;
  1314. struct drm_plane_state *primary_state;
  1315. int ret = 0;
  1316. state = drm_atomic_state_alloc(crtc->dev);
  1317. if (!state)
  1318. return -ENOMEM;
  1319. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  1320. retry:
  1321. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  1322. if (IS_ERR(crtc_state)) {
  1323. ret = PTR_ERR(crtc_state);
  1324. goto fail;
  1325. }
  1326. primary_state = drm_atomic_get_plane_state(state, crtc->primary);
  1327. if (IS_ERR(primary_state)) {
  1328. ret = PTR_ERR(primary_state);
  1329. goto fail;
  1330. }
  1331. if (!set->mode) {
  1332. WARN_ON(set->fb);
  1333. WARN_ON(set->num_connectors);
  1334. crtc_state->enable = false;
  1335. crtc_state->active = false;
  1336. ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
  1337. if (ret != 0)
  1338. goto fail;
  1339. drm_atomic_set_fb_for_plane(primary_state, NULL);
  1340. goto commit;
  1341. }
  1342. WARN_ON(!set->fb);
  1343. WARN_ON(!set->num_connectors);
  1344. crtc_state->enable = true;
  1345. crtc_state->active = true;
  1346. drm_mode_copy(&crtc_state->mode, set->mode);
  1347. ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
  1348. if (ret != 0)
  1349. goto fail;
  1350. drm_atomic_set_fb_for_plane(primary_state, set->fb);
  1351. primary_state->crtc_x = 0;
  1352. primary_state->crtc_y = 0;
  1353. primary_state->crtc_h = set->mode->vdisplay;
  1354. primary_state->crtc_w = set->mode->hdisplay;
  1355. primary_state->src_x = set->x << 16;
  1356. primary_state->src_y = set->y << 16;
  1357. primary_state->src_h = set->mode->vdisplay << 16;
  1358. primary_state->src_w = set->mode->hdisplay << 16;
  1359. commit:
  1360. ret = update_output_state(state, set);
  1361. if (ret)
  1362. goto fail;
  1363. ret = drm_atomic_commit(state);
  1364. if (ret != 0)
  1365. goto fail;
  1366. /* Driver takes ownership of state on successful commit. */
  1367. return 0;
  1368. fail:
  1369. if (ret == -EDEADLK)
  1370. goto backoff;
  1371. drm_atomic_state_free(state);
  1372. return ret;
  1373. backoff:
  1374. drm_atomic_state_clear(state);
  1375. drm_atomic_legacy_backoff(state);
  1376. /*
  1377. * Someone might have exchanged the framebuffer while we dropped locks
  1378. * in the backoff code. We need to fix up the fb refcount tracking the
  1379. * core does for us.
  1380. */
  1381. crtc->primary->old_fb = crtc->primary->fb;
  1382. goto retry;
  1383. }
  1384. EXPORT_SYMBOL(drm_atomic_helper_set_config);
  1385. /**
  1386. * drm_atomic_helper_crtc_set_property - helper for crtc properties
  1387. * @crtc: DRM crtc
  1388. * @property: DRM property
  1389. * @val: value of property
  1390. *
  1391. * Provides a default crtc set_property handler using the atomic driver
  1392. * interface.
  1393. *
  1394. * RETURNS:
  1395. * Zero on success, error code on failure
  1396. */
  1397. int
  1398. drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
  1399. struct drm_property *property,
  1400. uint64_t val)
  1401. {
  1402. struct drm_atomic_state *state;
  1403. struct drm_crtc_state *crtc_state;
  1404. int ret = 0;
  1405. state = drm_atomic_state_alloc(crtc->dev);
  1406. if (!state)
  1407. return -ENOMEM;
  1408. /* ->set_property is always called with all locks held. */
  1409. state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
  1410. retry:
  1411. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  1412. if (IS_ERR(crtc_state)) {
  1413. ret = PTR_ERR(crtc_state);
  1414. goto fail;
  1415. }
  1416. ret = drm_atomic_crtc_set_property(crtc, crtc_state,
  1417. property, val);
  1418. if (ret)
  1419. goto fail;
  1420. ret = drm_atomic_commit(state);
  1421. if (ret != 0)
  1422. goto fail;
  1423. /* Driver takes ownership of state on successful commit. */
  1424. return 0;
  1425. fail:
  1426. if (ret == -EDEADLK)
  1427. goto backoff;
  1428. drm_atomic_state_free(state);
  1429. return ret;
  1430. backoff:
  1431. drm_atomic_state_clear(state);
  1432. drm_atomic_legacy_backoff(state);
  1433. goto retry;
  1434. }
  1435. EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
  1436. /**
  1437. * drm_atomic_helper_plane_set_property - helper for plane properties
  1438. * @plane: DRM plane
  1439. * @property: DRM property
  1440. * @val: value of property
  1441. *
  1442. * Provides a default plane set_property handler using the atomic driver
  1443. * interface.
  1444. *
  1445. * RETURNS:
  1446. * Zero on success, error code on failure
  1447. */
  1448. int
  1449. drm_atomic_helper_plane_set_property(struct drm_plane *plane,
  1450. struct drm_property *property,
  1451. uint64_t val)
  1452. {
  1453. struct drm_atomic_state *state;
  1454. struct drm_plane_state *plane_state;
  1455. int ret = 0;
  1456. state = drm_atomic_state_alloc(plane->dev);
  1457. if (!state)
  1458. return -ENOMEM;
  1459. /* ->set_property is always called with all locks held. */
  1460. state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
  1461. retry:
  1462. plane_state = drm_atomic_get_plane_state(state, plane);
  1463. if (IS_ERR(plane_state)) {
  1464. ret = PTR_ERR(plane_state);
  1465. goto fail;
  1466. }
  1467. ret = drm_atomic_plane_set_property(plane, plane_state,
  1468. property, val);
  1469. if (ret)
  1470. goto fail;
  1471. ret = drm_atomic_commit(state);
  1472. if (ret != 0)
  1473. goto fail;
  1474. /* Driver takes ownership of state on successful commit. */
  1475. return 0;
  1476. fail:
  1477. if (ret == -EDEADLK)
  1478. goto backoff;
  1479. drm_atomic_state_free(state);
  1480. return ret;
  1481. backoff:
  1482. drm_atomic_state_clear(state);
  1483. drm_atomic_legacy_backoff(state);
  1484. goto retry;
  1485. }
  1486. EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
  1487. /**
  1488. * drm_atomic_helper_connector_set_property - helper for connector properties
  1489. * @connector: DRM connector
  1490. * @property: DRM property
  1491. * @val: value of property
  1492. *
  1493. * Provides a default connector set_property handler using the atomic driver
  1494. * interface.
  1495. *
  1496. * RETURNS:
  1497. * Zero on success, error code on failure
  1498. */
  1499. int
  1500. drm_atomic_helper_connector_set_property(struct drm_connector *connector,
  1501. struct drm_property *property,
  1502. uint64_t val)
  1503. {
  1504. struct drm_atomic_state *state;
  1505. struct drm_connector_state *connector_state;
  1506. int ret = 0;
  1507. state = drm_atomic_state_alloc(connector->dev);
  1508. if (!state)
  1509. return -ENOMEM;
  1510. /* ->set_property is always called with all locks held. */
  1511. state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
  1512. retry:
  1513. connector_state = drm_atomic_get_connector_state(state, connector);
  1514. if (IS_ERR(connector_state)) {
  1515. ret = PTR_ERR(connector_state);
  1516. goto fail;
  1517. }
  1518. ret = drm_atomic_connector_set_property(connector, connector_state,
  1519. property, val);
  1520. if (ret)
  1521. goto fail;
  1522. ret = drm_atomic_commit(state);
  1523. if (ret != 0)
  1524. goto fail;
  1525. /* Driver takes ownership of state on successful commit. */
  1526. return 0;
  1527. fail:
  1528. if (ret == -EDEADLK)
  1529. goto backoff;
  1530. drm_atomic_state_free(state);
  1531. return ret;
  1532. backoff:
  1533. drm_atomic_state_clear(state);
  1534. drm_atomic_legacy_backoff(state);
  1535. goto retry;
  1536. }
  1537. EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
  1538. /**
  1539. * drm_atomic_helper_page_flip - execute a legacy page flip
  1540. * @crtc: DRM crtc
  1541. * @fb: DRM framebuffer
  1542. * @event: optional DRM event to signal upon completion
  1543. * @flags: flip flags for non-vblank sync'ed updates
  1544. *
  1545. * Provides a default page flip implementation using the atomic driver interface.
  1546. *
  1547. * Note that for now so called async page flips (i.e. updates which are not
  1548. * synchronized to vblank) are not supported, since the atomic interfaces have
  1549. * no provisions for this yet.
  1550. *
  1551. * Returns:
  1552. * Returns 0 on success, negative errno numbers on failure.
  1553. */
  1554. int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
  1555. struct drm_framebuffer *fb,
  1556. struct drm_pending_vblank_event *event,
  1557. uint32_t flags)
  1558. {
  1559. struct drm_plane *plane = crtc->primary;
  1560. struct drm_atomic_state *state;
  1561. struct drm_plane_state *plane_state;
  1562. struct drm_crtc_state *crtc_state;
  1563. int ret = 0;
  1564. if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
  1565. return -EINVAL;
  1566. state = drm_atomic_state_alloc(plane->dev);
  1567. if (!state)
  1568. return -ENOMEM;
  1569. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  1570. retry:
  1571. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  1572. if (IS_ERR(crtc_state)) {
  1573. ret = PTR_ERR(crtc_state);
  1574. goto fail;
  1575. }
  1576. crtc_state->event = event;
  1577. plane_state = drm_atomic_get_plane_state(state, plane);
  1578. if (IS_ERR(plane_state)) {
  1579. ret = PTR_ERR(plane_state);
  1580. goto fail;
  1581. }
  1582. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  1583. if (ret != 0)
  1584. goto fail;
  1585. drm_atomic_set_fb_for_plane(plane_state, fb);
  1586. ret = drm_atomic_async_commit(state);
  1587. if (ret != 0)
  1588. goto fail;
  1589. /* TODO: ->page_flip is the only driver callback where the core
  1590. * doesn't update plane->fb. For now patch it up here. */
  1591. plane->fb = plane->state->fb;
  1592. /* Driver takes ownership of state on successful async commit. */
  1593. return 0;
  1594. fail:
  1595. if (ret == -EDEADLK)
  1596. goto backoff;
  1597. drm_atomic_state_free(state);
  1598. return ret;
  1599. backoff:
  1600. drm_atomic_state_clear(state);
  1601. drm_atomic_legacy_backoff(state);
  1602. /*
  1603. * Someone might have exchanged the framebuffer while we dropped locks
  1604. * in the backoff code. We need to fix up the fb refcount tracking the
  1605. * core does for us.
  1606. */
  1607. plane->old_fb = plane->fb;
  1608. goto retry;
  1609. }
  1610. EXPORT_SYMBOL(drm_atomic_helper_page_flip);
  1611. /**
  1612. * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
  1613. * @connector: affected connector
  1614. * @mode: DPMS mode
  1615. *
  1616. * This is the main helper function provided by the atomic helper framework for
  1617. * implementing the legacy DPMS connector interface. It computes the new desired
  1618. * ->active state for the corresponding CRTC (if the connector is enabled) and
  1619. * updates it.
  1620. */
  1621. void drm_atomic_helper_connector_dpms(struct drm_connector *connector,
  1622. int mode)
  1623. {
  1624. struct drm_mode_config *config = &connector->dev->mode_config;
  1625. struct drm_atomic_state *state;
  1626. struct drm_crtc_state *crtc_state;
  1627. struct drm_crtc *crtc;
  1628. struct drm_connector *tmp_connector;
  1629. int ret;
  1630. bool active = false;
  1631. if (mode != DRM_MODE_DPMS_ON)
  1632. mode = DRM_MODE_DPMS_OFF;
  1633. connector->dpms = mode;
  1634. crtc = connector->state->crtc;
  1635. if (!crtc)
  1636. return;
  1637. /* FIXME: ->dpms has no return value so can't forward the -ENOMEM. */
  1638. state = drm_atomic_state_alloc(connector->dev);
  1639. if (!state)
  1640. return;
  1641. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  1642. retry:
  1643. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  1644. if (IS_ERR(crtc_state))
  1645. return;
  1646. WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  1647. list_for_each_entry(tmp_connector, &config->connector_list, head) {
  1648. if (tmp_connector->state->crtc != crtc)
  1649. continue;
  1650. if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
  1651. active = true;
  1652. break;
  1653. }
  1654. }
  1655. crtc_state->active = active;
  1656. ret = drm_atomic_commit(state);
  1657. if (ret != 0)
  1658. goto fail;
  1659. /* Driver takes ownership of state on successful async commit. */
  1660. return;
  1661. fail:
  1662. if (ret == -EDEADLK)
  1663. goto backoff;
  1664. drm_atomic_state_free(state);
  1665. WARN(1, "Driver bug: Changing ->active failed with ret=%i\n", ret);
  1666. return;
  1667. backoff:
  1668. drm_atomic_state_clear(state);
  1669. drm_atomic_legacy_backoff(state);
  1670. goto retry;
  1671. }
  1672. EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
  1673. /**
  1674. * DOC: atomic state reset and initialization
  1675. *
  1676. * Both the drm core and the atomic helpers assume that there is always the full
  1677. * and correct atomic software state for all connectors, CRTCs and planes
  1678. * available. Which is a bit a problem on driver load and also after system
  1679. * suspend. One way to solve this is to have a hardware state read-out
  1680. * infrastructure which reconstructs the full software state (e.g. the i915
  1681. * driver).
  1682. *
  1683. * The simpler solution is to just reset the software state to everything off,
  1684. * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
  1685. * the atomic helpers provide default reset implementations for all hooks.
  1686. */
  1687. /**
  1688. * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
  1689. * @crtc: drm CRTC
  1690. *
  1691. * Resets the atomic state for @crtc by freeing the state pointer (which might
  1692. * be NULL, e.g. at driver load time) and allocating a new empty state object.
  1693. */
  1694. void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
  1695. {
  1696. kfree(crtc->state);
  1697. crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
  1698. if (crtc->state)
  1699. crtc->state->crtc = crtc;
  1700. }
  1701. EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
  1702. /**
  1703. * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
  1704. * @crtc: CRTC object
  1705. * @state: atomic CRTC state
  1706. *
  1707. * Copies atomic state from a CRTC's current state and resets inferred values.
  1708. * This is useful for drivers that subclass the CRTC state.
  1709. */
  1710. void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
  1711. struct drm_crtc_state *state)
  1712. {
  1713. memcpy(state, crtc->state, sizeof(*state));
  1714. state->mode_changed = false;
  1715. state->active_changed = false;
  1716. state->planes_changed = false;
  1717. state->event = NULL;
  1718. }
  1719. EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
  1720. /**
  1721. * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
  1722. * @crtc: drm CRTC
  1723. *
  1724. * Default CRTC state duplicate hook for drivers which don't have their own
  1725. * subclassed CRTC state structure.
  1726. */
  1727. struct drm_crtc_state *
  1728. drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
  1729. {
  1730. struct drm_crtc_state *state;
  1731. if (WARN_ON(!crtc->state))
  1732. return NULL;
  1733. state = kmalloc(sizeof(*state), GFP_KERNEL);
  1734. if (state)
  1735. __drm_atomic_helper_crtc_duplicate_state(crtc, state);
  1736. return state;
  1737. }
  1738. EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
  1739. /**
  1740. * __drm_atomic_helper_crtc_destroy_state - release CRTC state
  1741. * @crtc: CRTC object
  1742. * @state: CRTC state object to release
  1743. *
  1744. * Releases all resources stored in the CRTC state without actually freeing
  1745. * the memory of the CRTC state. This is useful for drivers that subclass the
  1746. * CRTC state.
  1747. */
  1748. void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
  1749. struct drm_crtc_state *state)
  1750. {
  1751. /*
  1752. * This is currently a placeholder so that drivers that subclass the
  1753. * state will automatically do the right thing if code is ever added
  1754. * to this function.
  1755. */
  1756. }
  1757. EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
  1758. /**
  1759. * drm_atomic_helper_crtc_destroy_state - default state destroy hook
  1760. * @crtc: drm CRTC
  1761. * @state: CRTC state object to release
  1762. *
  1763. * Default CRTC state destroy hook for drivers which don't have their own
  1764. * subclassed CRTC state structure.
  1765. */
  1766. void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
  1767. struct drm_crtc_state *state)
  1768. {
  1769. __drm_atomic_helper_crtc_destroy_state(crtc, state);
  1770. kfree(state);
  1771. }
  1772. EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
  1773. /**
  1774. * drm_atomic_helper_plane_reset - default ->reset hook for planes
  1775. * @plane: drm plane
  1776. *
  1777. * Resets the atomic state for @plane by freeing the state pointer (which might
  1778. * be NULL, e.g. at driver load time) and allocating a new empty state object.
  1779. */
  1780. void drm_atomic_helper_plane_reset(struct drm_plane *plane)
  1781. {
  1782. if (plane->state && plane->state->fb)
  1783. drm_framebuffer_unreference(plane->state->fb);
  1784. kfree(plane->state);
  1785. plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
  1786. if (plane->state)
  1787. plane->state->plane = plane;
  1788. }
  1789. EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
  1790. /**
  1791. * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
  1792. * @plane: plane object
  1793. * @state: atomic plane state
  1794. *
  1795. * Copies atomic state from a plane's current state. This is useful for
  1796. * drivers that subclass the plane state.
  1797. */
  1798. void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
  1799. struct drm_plane_state *state)
  1800. {
  1801. memcpy(state, plane->state, sizeof(*state));
  1802. if (state->fb)
  1803. drm_framebuffer_reference(state->fb);
  1804. }
  1805. EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
  1806. /**
  1807. * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
  1808. * @plane: drm plane
  1809. *
  1810. * Default plane state duplicate hook for drivers which don't have their own
  1811. * subclassed plane state structure.
  1812. */
  1813. struct drm_plane_state *
  1814. drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
  1815. {
  1816. struct drm_plane_state *state;
  1817. if (WARN_ON(!plane->state))
  1818. return NULL;
  1819. state = kmalloc(sizeof(*state), GFP_KERNEL);
  1820. if (state)
  1821. __drm_atomic_helper_plane_duplicate_state(plane, state);
  1822. return state;
  1823. }
  1824. EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
  1825. /**
  1826. * __drm_atomic_helper_plane_destroy_state - release plane state
  1827. * @plane: plane object
  1828. * @state: plane state object to release
  1829. *
  1830. * Releases all resources stored in the plane state without actually freeing
  1831. * the memory of the plane state. This is useful for drivers that subclass the
  1832. * plane state.
  1833. */
  1834. void __drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
  1835. struct drm_plane_state *state)
  1836. {
  1837. if (state->fb)
  1838. drm_framebuffer_unreference(state->fb);
  1839. }
  1840. EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
  1841. /**
  1842. * drm_atomic_helper_plane_destroy_state - default state destroy hook
  1843. * @plane: drm plane
  1844. * @state: plane state object to release
  1845. *
  1846. * Default plane state destroy hook for drivers which don't have their own
  1847. * subclassed plane state structure.
  1848. */
  1849. void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
  1850. struct drm_plane_state *state)
  1851. {
  1852. __drm_atomic_helper_plane_destroy_state(plane, state);
  1853. kfree(state);
  1854. }
  1855. EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
  1856. /**
  1857. * drm_atomic_helper_connector_reset - default ->reset hook for connectors
  1858. * @connector: drm connector
  1859. *
  1860. * Resets the atomic state for @connector by freeing the state pointer (which
  1861. * might be NULL, e.g. at driver load time) and allocating a new empty state
  1862. * object.
  1863. */
  1864. void drm_atomic_helper_connector_reset(struct drm_connector *connector)
  1865. {
  1866. kfree(connector->state);
  1867. connector->state = kzalloc(sizeof(*connector->state), GFP_KERNEL);
  1868. if (connector->state)
  1869. connector->state->connector = connector;
  1870. }
  1871. EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
  1872. /**
  1873. * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
  1874. * @connector: connector object
  1875. * @state: atomic connector state
  1876. *
  1877. * Copies atomic state from a connector's current state. This is useful for
  1878. * drivers that subclass the connector state.
  1879. */
  1880. void
  1881. __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
  1882. struct drm_connector_state *state)
  1883. {
  1884. memcpy(state, connector->state, sizeof(*state));
  1885. }
  1886. EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
  1887. /**
  1888. * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
  1889. * @connector: drm connector
  1890. *
  1891. * Default connector state duplicate hook for drivers which don't have their own
  1892. * subclassed connector state structure.
  1893. */
  1894. struct drm_connector_state *
  1895. drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
  1896. {
  1897. struct drm_connector_state *state;
  1898. if (WARN_ON(!connector->state))
  1899. return NULL;
  1900. state = kmalloc(sizeof(*state), GFP_KERNEL);
  1901. if (state)
  1902. __drm_atomic_helper_connector_duplicate_state(connector, state);
  1903. return state;
  1904. }
  1905. EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
  1906. /**
  1907. * __drm_atomic_helper_connector_destroy_state - release connector state
  1908. * @connector: connector object
  1909. * @state: connector state object to release
  1910. *
  1911. * Releases all resources stored in the connector state without actually
  1912. * freeing the memory of the connector state. This is useful for drivers that
  1913. * subclass the connector state.
  1914. */
  1915. void
  1916. __drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
  1917. struct drm_connector_state *state)
  1918. {
  1919. /*
  1920. * This is currently a placeholder so that drivers that subclass the
  1921. * state will automatically do the right thing if code is ever added
  1922. * to this function.
  1923. */
  1924. }
  1925. EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
  1926. /**
  1927. * drm_atomic_helper_connector_destroy_state - default state destroy hook
  1928. * @connector: drm connector
  1929. * @state: connector state object to release
  1930. *
  1931. * Default connector state destroy hook for drivers which don't have their own
  1932. * subclassed connector state structure.
  1933. */
  1934. void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
  1935. struct drm_connector_state *state)
  1936. {
  1937. __drm_atomic_helper_connector_destroy_state(connector, state);
  1938. kfree(state);
  1939. }
  1940. EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);