drm_atomic_helper.c 57 KB

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