drm_atomic_helper.c 53 KB

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