drm_atomic.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129
  1. /*
  2. * Copyright (C) 2014 Red Hat
  3. * Copyright (C) 2014 Intel Corp.
  4. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors:
  25. * Rob Clark <robdclark@gmail.com>
  26. * Daniel Vetter <daniel.vetter@ffwll.ch>
  27. */
  28. #include <linux/export.h>
  29. #include <linux/sync_file.h>
  30. #include <drm/drm_atomic.h>
  31. #include <drm/drm_atomic_uapi.h>
  32. #include <drm/drm_blend.h>
  33. #include <drm/drm_bridge.h>
  34. #include <drm/drm_debugfs.h>
  35. #include <drm/drm_device.h>
  36. #include <drm/drm_drv.h>
  37. #include <drm/drm_file.h>
  38. #include <drm/drm_fourcc.h>
  39. #include <drm/drm_framebuffer.h>
  40. #include <drm/drm_mode.h>
  41. #include <drm/drm_print.h>
  42. #include <drm/drm_writeback.h>
  43. #include <drm/drm_colorop.h>
  44. #include "drm_crtc_internal.h"
  45. #include "drm_internal.h"
  46. void __drm_crtc_commit_free(struct kref *kref)
  47. {
  48. struct drm_crtc_commit *commit =
  49. container_of(kref, struct drm_crtc_commit, ref);
  50. kfree(commit);
  51. }
  52. EXPORT_SYMBOL(__drm_crtc_commit_free);
  53. /**
  54. * drm_crtc_commit_wait - Waits for a commit to complete
  55. * @commit: &drm_crtc_commit to wait for
  56. *
  57. * Waits for a given &drm_crtc_commit to be programmed into the
  58. * hardware and flipped to.
  59. *
  60. * Returns:
  61. * 0 on success, a negative error code otherwise.
  62. */
  63. int drm_crtc_commit_wait(struct drm_crtc_commit *commit)
  64. {
  65. unsigned long timeout = 10 * HZ;
  66. int ret;
  67. if (!commit)
  68. return 0;
  69. ret = wait_for_completion_timeout(&commit->hw_done, timeout);
  70. if (!ret) {
  71. drm_err(commit->crtc->dev, "hw_done timed out\n");
  72. return -ETIMEDOUT;
  73. }
  74. /*
  75. * Currently no support for overwriting flips, hence
  76. * stall for previous one to execute completely.
  77. */
  78. ret = wait_for_completion_timeout(&commit->flip_done, timeout);
  79. if (!ret) {
  80. drm_err(commit->crtc->dev, "flip_done timed out\n");
  81. return -ETIMEDOUT;
  82. }
  83. return 0;
  84. }
  85. EXPORT_SYMBOL(drm_crtc_commit_wait);
  86. /**
  87. * drm_atomic_state_default_release -
  88. * release memory initialized by drm_atomic_state_init
  89. * @state: atomic state
  90. *
  91. * Free all the memory allocated by drm_atomic_state_init.
  92. * This should only be used by drivers which are still subclassing
  93. * &drm_atomic_state and haven't switched to &drm_private_state yet.
  94. */
  95. void drm_atomic_state_default_release(struct drm_atomic_state *state)
  96. {
  97. kfree(state->connectors);
  98. kfree(state->crtcs);
  99. kfree(state->planes);
  100. kfree(state->colorops);
  101. kfree(state->private_objs);
  102. }
  103. EXPORT_SYMBOL(drm_atomic_state_default_release);
  104. /**
  105. * drm_atomic_state_init - init new atomic state
  106. * @dev: DRM device
  107. * @state: atomic state
  108. *
  109. * Default implementation for filling in a new atomic state.
  110. * This should only be used by drivers which are still subclassing
  111. * &drm_atomic_state and haven't switched to &drm_private_state yet.
  112. */
  113. int
  114. drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
  115. {
  116. kref_init(&state->ref);
  117. /* TODO legacy paths should maybe do a better job about
  118. * setting this appropriately?
  119. */
  120. state->allow_modeset = true;
  121. state->crtcs = kzalloc_objs(*state->crtcs, dev->mode_config.num_crtc);
  122. if (!state->crtcs)
  123. goto fail;
  124. state->planes = kzalloc_objs(*state->planes,
  125. dev->mode_config.num_total_plane);
  126. if (!state->planes)
  127. goto fail;
  128. state->colorops = kzalloc_objs(*state->colorops,
  129. dev->mode_config.num_colorop);
  130. if (!state->colorops)
  131. goto fail;
  132. /*
  133. * Because drm_atomic_state can be committed asynchronously we need our
  134. * own reference and cannot rely on the on implied by drm_file in the
  135. * ioctl call.
  136. */
  137. drm_dev_get(dev);
  138. state->dev = dev;
  139. drm_dbg_atomic(dev, "Allocated atomic state %p\n", state);
  140. return 0;
  141. fail:
  142. drm_atomic_state_default_release(state);
  143. return -ENOMEM;
  144. }
  145. EXPORT_SYMBOL(drm_atomic_state_init);
  146. /**
  147. * drm_atomic_state_alloc - allocate atomic state
  148. * @dev: DRM device
  149. *
  150. * This allocates an empty atomic state to track updates.
  151. */
  152. struct drm_atomic_state *
  153. drm_atomic_state_alloc(struct drm_device *dev)
  154. {
  155. struct drm_mode_config *config = &dev->mode_config;
  156. if (!config->funcs->atomic_state_alloc) {
  157. struct drm_atomic_state *state;
  158. state = kzalloc_obj(*state);
  159. if (!state)
  160. return NULL;
  161. if (drm_atomic_state_init(dev, state) < 0) {
  162. kfree(state);
  163. return NULL;
  164. }
  165. return state;
  166. }
  167. return config->funcs->atomic_state_alloc(dev);
  168. }
  169. EXPORT_SYMBOL(drm_atomic_state_alloc);
  170. /**
  171. * drm_atomic_state_default_clear - clear base atomic state
  172. * @state: atomic state
  173. *
  174. * Default implementation for clearing atomic state.
  175. * This should only be used by drivers which are still subclassing
  176. * &drm_atomic_state and haven't switched to &drm_private_state yet.
  177. */
  178. void drm_atomic_state_default_clear(struct drm_atomic_state *state)
  179. {
  180. struct drm_device *dev = state->dev;
  181. struct drm_mode_config *config = &dev->mode_config;
  182. int i;
  183. drm_dbg_atomic(dev, "Clearing atomic state %p\n", state);
  184. state->checked = false;
  185. for (i = 0; i < state->num_connector; i++) {
  186. struct drm_connector *connector = state->connectors[i].ptr;
  187. if (!connector)
  188. continue;
  189. connector->funcs->atomic_destroy_state(connector,
  190. state->connectors[i].state_to_destroy);
  191. state->connectors[i].ptr = NULL;
  192. state->connectors[i].state_to_destroy = NULL;
  193. state->connectors[i].old_state = NULL;
  194. state->connectors[i].new_state = NULL;
  195. drm_connector_put(connector);
  196. }
  197. for (i = 0; i < config->num_crtc; i++) {
  198. struct drm_crtc *crtc = state->crtcs[i].ptr;
  199. if (!crtc)
  200. continue;
  201. crtc->funcs->atomic_destroy_state(crtc,
  202. state->crtcs[i].state_to_destroy);
  203. state->crtcs[i].ptr = NULL;
  204. state->crtcs[i].state_to_destroy = NULL;
  205. state->crtcs[i].old_state = NULL;
  206. state->crtcs[i].new_state = NULL;
  207. if (state->crtcs[i].commit) {
  208. drm_crtc_commit_put(state->crtcs[i].commit);
  209. state->crtcs[i].commit = NULL;
  210. }
  211. }
  212. for (i = 0; i < config->num_total_plane; i++) {
  213. struct drm_plane *plane = state->planes[i].ptr;
  214. if (!plane)
  215. continue;
  216. plane->funcs->atomic_destroy_state(plane,
  217. state->planes[i].state_to_destroy);
  218. state->planes[i].ptr = NULL;
  219. state->planes[i].state_to_destroy = NULL;
  220. state->planes[i].old_state = NULL;
  221. state->planes[i].new_state = NULL;
  222. }
  223. for (i = 0; i < config->num_colorop; i++) {
  224. struct drm_colorop *colorop = state->colorops[i].ptr;
  225. if (!colorop)
  226. continue;
  227. drm_colorop_atomic_destroy_state(colorop,
  228. state->colorops[i].state);
  229. state->colorops[i].ptr = NULL;
  230. state->colorops[i].state = NULL;
  231. state->colorops[i].old_state = NULL;
  232. state->colorops[i].new_state = NULL;
  233. }
  234. for (i = 0; i < state->num_private_objs; i++) {
  235. struct drm_private_obj *obj = state->private_objs[i].ptr;
  236. obj->funcs->atomic_destroy_state(obj,
  237. state->private_objs[i].state_to_destroy);
  238. state->private_objs[i].ptr = NULL;
  239. state->private_objs[i].state_to_destroy = NULL;
  240. state->private_objs[i].old_state = NULL;
  241. state->private_objs[i].new_state = NULL;
  242. }
  243. state->num_private_objs = 0;
  244. if (state->fake_commit) {
  245. drm_crtc_commit_put(state->fake_commit);
  246. state->fake_commit = NULL;
  247. }
  248. }
  249. EXPORT_SYMBOL(drm_atomic_state_default_clear);
  250. /**
  251. * drm_atomic_state_clear - clear state object
  252. * @state: atomic state
  253. *
  254. * When the w/w mutex algorithm detects a deadlock we need to back off and drop
  255. * all locks. So someone else could sneak in and change the current modeset
  256. * configuration. Which means that all the state assembled in @state is no
  257. * longer an atomic update to the current state, but to some arbitrary earlier
  258. * state. Which could break assumptions the driver's
  259. * &drm_mode_config_funcs.atomic_check likely relies on.
  260. *
  261. * Hence we must clear all cached state and completely start over, using this
  262. * function.
  263. */
  264. void drm_atomic_state_clear(struct drm_atomic_state *state)
  265. {
  266. struct drm_device *dev = state->dev;
  267. struct drm_mode_config *config = &dev->mode_config;
  268. if (config->funcs->atomic_state_clear)
  269. config->funcs->atomic_state_clear(state);
  270. else
  271. drm_atomic_state_default_clear(state);
  272. }
  273. EXPORT_SYMBOL(drm_atomic_state_clear);
  274. /**
  275. * __drm_atomic_state_free - free all memory for an atomic state
  276. * @ref: This atomic state to deallocate
  277. *
  278. * This frees all memory associated with an atomic state, including all the
  279. * per-object state for planes, CRTCs and connectors.
  280. */
  281. void __drm_atomic_state_free(struct kref *ref)
  282. {
  283. struct drm_atomic_state *state = container_of(ref, typeof(*state), ref);
  284. struct drm_device *dev = state->dev;
  285. struct drm_mode_config *config = &dev->mode_config;
  286. drm_atomic_state_clear(state);
  287. drm_dbg_atomic(state->dev, "Freeing atomic state %p\n", state);
  288. if (config->funcs->atomic_state_free) {
  289. config->funcs->atomic_state_free(state);
  290. } else {
  291. drm_atomic_state_default_release(state);
  292. kfree(state);
  293. }
  294. drm_dev_put(dev);
  295. }
  296. EXPORT_SYMBOL(__drm_atomic_state_free);
  297. /**
  298. * drm_atomic_get_crtc_state - get CRTC state
  299. * @state: global atomic state object
  300. * @crtc: CRTC to get state object for
  301. *
  302. * This function returns the CRTC state for the given CRTC, allocating it if
  303. * needed. It will also grab the relevant CRTC lock to make sure that the state
  304. * is consistent.
  305. *
  306. * WARNING: Drivers may only add new CRTC states to a @state if
  307. * drm_atomic_state.allow_modeset is set, or if it's a driver-internal commit
  308. * not created by userspace through an IOCTL call.
  309. *
  310. * Returns:
  311. * Either the allocated state or the error code encoded into the pointer. When
  312. * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
  313. * entire atomic sequence must be restarted. All other errors are fatal.
  314. */
  315. struct drm_crtc_state *
  316. drm_atomic_get_crtc_state(struct drm_atomic_state *state,
  317. struct drm_crtc *crtc)
  318. {
  319. int ret, index = drm_crtc_index(crtc);
  320. struct drm_crtc_state *crtc_state;
  321. WARN_ON(!state->acquire_ctx);
  322. drm_WARN_ON(state->dev, state->checked);
  323. crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
  324. if (crtc_state)
  325. return crtc_state;
  326. ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
  327. if (ret)
  328. return ERR_PTR(ret);
  329. crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
  330. if (!crtc_state)
  331. return ERR_PTR(-ENOMEM);
  332. state->crtcs[index].state_to_destroy = crtc_state;
  333. state->crtcs[index].old_state = crtc->state;
  334. state->crtcs[index].new_state = crtc_state;
  335. state->crtcs[index].ptr = crtc;
  336. crtc_state->state = state;
  337. drm_dbg_atomic(state->dev, "Added [CRTC:%d:%s] %p state to %p\n",
  338. crtc->base.id, crtc->name, crtc_state, state);
  339. return crtc_state;
  340. }
  341. EXPORT_SYMBOL(drm_atomic_get_crtc_state);
  342. static int drm_atomic_crtc_check(const struct drm_crtc_state *old_crtc_state,
  343. const struct drm_crtc_state *new_crtc_state)
  344. {
  345. struct drm_crtc *crtc = new_crtc_state->crtc;
  346. /* NOTE: we explicitly don't enforce constraints such as primary
  347. * layer covering entire screen, since that is something we want
  348. * to allow (on hw that supports it). For hw that does not, it
  349. * should be checked in driver's crtc->atomic_check() vfunc.
  350. *
  351. * TODO: Add generic modeset state checks once we support those.
  352. */
  353. if (new_crtc_state->active && !new_crtc_state->enable) {
  354. drm_dbg_atomic(crtc->dev,
  355. "[CRTC:%d:%s] active without enabled\n",
  356. crtc->base.id, crtc->name);
  357. return -EINVAL;
  358. }
  359. /* The state->enable vs. state->mode_blob checks can be WARN_ON,
  360. * as this is a kernel-internal detail that userspace should never
  361. * be able to trigger.
  362. */
  363. if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
  364. WARN_ON(new_crtc_state->enable && !new_crtc_state->mode_blob)) {
  365. drm_dbg_atomic(crtc->dev,
  366. "[CRTC:%d:%s] enabled without mode blob\n",
  367. crtc->base.id, crtc->name);
  368. return -EINVAL;
  369. }
  370. if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
  371. WARN_ON(!new_crtc_state->enable && new_crtc_state->mode_blob)) {
  372. drm_dbg_atomic(crtc->dev,
  373. "[CRTC:%d:%s] disabled with mode blob\n",
  374. crtc->base.id, crtc->name);
  375. return -EINVAL;
  376. }
  377. /*
  378. * Reject event generation for when a CRTC is off and stays off.
  379. * It wouldn't be hard to implement this, but userspace has a track
  380. * record of happily burning through 100% cpu (or worse, crash) when the
  381. * display pipe is suspended. To avoid all that fun just reject updates
  382. * that ask for events since likely that indicates a bug in the
  383. * compositor's drawing loop. This is consistent with the vblank IOCTL
  384. * and legacy page_flip IOCTL which also reject service on a disabled
  385. * pipe.
  386. */
  387. if (new_crtc_state->event &&
  388. !new_crtc_state->active && !old_crtc_state->active) {
  389. drm_dbg_atomic(crtc->dev,
  390. "[CRTC:%d:%s] requesting event but off\n",
  391. crtc->base.id, crtc->name);
  392. return -EINVAL;
  393. }
  394. return 0;
  395. }
  396. static void drm_atomic_crtc_print_state(struct drm_printer *p,
  397. const struct drm_crtc_state *state)
  398. {
  399. struct drm_crtc *crtc = state->crtc;
  400. drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name);
  401. drm_printf(p, "\tenable=%d\n", state->enable);
  402. drm_printf(p, "\tactive=%d\n", state->active);
  403. drm_printf(p, "\tself_refresh_active=%d\n", state->self_refresh_active);
  404. drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed);
  405. drm_printf(p, "\tmode_changed=%d\n", state->mode_changed);
  406. drm_printf(p, "\tactive_changed=%d\n", state->active_changed);
  407. drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed);
  408. drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
  409. drm_printf(p, "\tplane_mask=%x\n", state->plane_mask);
  410. drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask);
  411. drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask);
  412. drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode));
  413. if (crtc->funcs->atomic_print_state)
  414. crtc->funcs->atomic_print_state(p, state);
  415. }
  416. static int drm_atomic_connector_check(struct drm_connector *connector,
  417. struct drm_connector_state *state)
  418. {
  419. struct drm_crtc_state *crtc_state;
  420. struct drm_writeback_job *writeback_job = state->writeback_job;
  421. const struct drm_display_info *info = &connector->display_info;
  422. state->max_bpc = info->bpc ? info->bpc : 8;
  423. if (connector->max_bpc_property)
  424. state->max_bpc = min(state->max_bpc, state->max_requested_bpc);
  425. if ((connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK) || !writeback_job)
  426. return 0;
  427. if (writeback_job->fb && !state->crtc) {
  428. drm_dbg_atomic(connector->dev,
  429. "[CONNECTOR:%d:%s] framebuffer without CRTC\n",
  430. connector->base.id, connector->name);
  431. return -EINVAL;
  432. }
  433. if (state->crtc)
  434. crtc_state = drm_atomic_get_new_crtc_state(state->state,
  435. state->crtc);
  436. if (writeback_job->fb && !crtc_state->active) {
  437. drm_dbg_atomic(connector->dev,
  438. "[CONNECTOR:%d:%s] has framebuffer, but [CRTC:%d] is off\n",
  439. connector->base.id, connector->name,
  440. state->crtc->base.id);
  441. return -EINVAL;
  442. }
  443. if (!writeback_job->fb) {
  444. if (writeback_job->out_fence) {
  445. drm_dbg_atomic(connector->dev,
  446. "[CONNECTOR:%d:%s] requesting out-fence without framebuffer\n",
  447. connector->base.id, connector->name);
  448. return -EINVAL;
  449. }
  450. drm_writeback_cleanup_job(writeback_job);
  451. state->writeback_job = NULL;
  452. }
  453. return 0;
  454. }
  455. /**
  456. * drm_atomic_get_plane_state - get plane state
  457. * @state: global atomic state object
  458. * @plane: plane to get state object for
  459. *
  460. * This function returns the plane state for the given plane, allocating it if
  461. * needed. It will also grab the relevant plane lock to make sure that the state
  462. * is consistent.
  463. *
  464. * Returns:
  465. * Either the allocated state or the error code encoded into the pointer. When
  466. * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
  467. * entire atomic sequence must be restarted. All other errors are fatal.
  468. */
  469. struct drm_plane_state *
  470. drm_atomic_get_plane_state(struct drm_atomic_state *state,
  471. struct drm_plane *plane)
  472. {
  473. int ret, index = drm_plane_index(plane);
  474. struct drm_plane_state *plane_state;
  475. WARN_ON(!state->acquire_ctx);
  476. drm_WARN_ON(state->dev, state->checked);
  477. /* the legacy pointers should never be set */
  478. WARN_ON(plane->fb);
  479. WARN_ON(plane->old_fb);
  480. WARN_ON(plane->crtc);
  481. plane_state = drm_atomic_get_new_plane_state(state, plane);
  482. if (plane_state)
  483. return plane_state;
  484. ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
  485. if (ret)
  486. return ERR_PTR(ret);
  487. plane_state = plane->funcs->atomic_duplicate_state(plane);
  488. if (!plane_state)
  489. return ERR_PTR(-ENOMEM);
  490. state->planes[index].state_to_destroy = plane_state;
  491. state->planes[index].ptr = plane;
  492. state->planes[index].old_state = plane->state;
  493. state->planes[index].new_state = plane_state;
  494. plane_state->state = state;
  495. drm_dbg_atomic(plane->dev, "Added [PLANE:%d:%s] %p state to %p\n",
  496. plane->base.id, plane->name, plane_state, state);
  497. if (plane_state->crtc) {
  498. struct drm_crtc_state *crtc_state;
  499. crtc_state = drm_atomic_get_crtc_state(state,
  500. plane_state->crtc);
  501. if (IS_ERR(crtc_state))
  502. return ERR_CAST(crtc_state);
  503. }
  504. return plane_state;
  505. }
  506. EXPORT_SYMBOL(drm_atomic_get_plane_state);
  507. /**
  508. * drm_atomic_get_colorop_state - get colorop state
  509. * @state: global atomic state object
  510. * @colorop: colorop to get state object for
  511. *
  512. * This function returns the colorop state for the given colorop, allocating it
  513. * if needed. It will also grab the relevant plane lock to make sure that the
  514. * state is consistent.
  515. *
  516. * Returns:
  517. *
  518. * Either the allocated state or the error code encoded into the pointer. When
  519. * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
  520. * entire atomic sequence must be restarted. All other errors are fatal.
  521. */
  522. struct drm_colorop_state *
  523. drm_atomic_get_colorop_state(struct drm_atomic_state *state,
  524. struct drm_colorop *colorop)
  525. {
  526. int ret, index = drm_colorop_index(colorop);
  527. struct drm_colorop_state *colorop_state;
  528. WARN_ON(!state->acquire_ctx);
  529. colorop_state = drm_atomic_get_new_colorop_state(state, colorop);
  530. if (colorop_state)
  531. return colorop_state;
  532. ret = drm_modeset_lock(&colorop->plane->mutex, state->acquire_ctx);
  533. if (ret)
  534. return ERR_PTR(ret);
  535. colorop_state = drm_atomic_helper_colorop_duplicate_state(colorop);
  536. if (!colorop_state)
  537. return ERR_PTR(-ENOMEM);
  538. state->colorops[index].state = colorop_state;
  539. state->colorops[index].ptr = colorop;
  540. state->colorops[index].old_state = colorop->state;
  541. state->colorops[index].new_state = colorop_state;
  542. colorop_state->state = state;
  543. drm_dbg_atomic(colorop->dev, "Added [COLOROP:%d:%d] %p state to %p\n",
  544. colorop->base.id, colorop->type, colorop_state, state);
  545. return colorop_state;
  546. }
  547. EXPORT_SYMBOL(drm_atomic_get_colorop_state);
  548. /**
  549. * drm_atomic_get_old_colorop_state - get colorop state, if it exists
  550. * @state: global atomic state object
  551. * @colorop: colorop to grab
  552. *
  553. * This function returns the old colorop state for the given colorop, or
  554. * NULL if the colorop is not part of the global atomic state.
  555. */
  556. struct drm_colorop_state *
  557. drm_atomic_get_old_colorop_state(struct drm_atomic_state *state,
  558. struct drm_colorop *colorop)
  559. {
  560. return state->colorops[drm_colorop_index(colorop)].old_state;
  561. }
  562. EXPORT_SYMBOL(drm_atomic_get_old_colorop_state);
  563. /**
  564. * drm_atomic_get_new_colorop_state - get colorop state, if it exists
  565. * @state: global atomic state object
  566. * @colorop: colorop to grab
  567. *
  568. * This function returns the new colorop state for the given colorop, or
  569. * NULL if the colorop is not part of the global atomic state.
  570. */
  571. struct drm_colorop_state *
  572. drm_atomic_get_new_colorop_state(struct drm_atomic_state *state,
  573. struct drm_colorop *colorop)
  574. {
  575. return state->colorops[drm_colorop_index(colorop)].new_state;
  576. }
  577. EXPORT_SYMBOL(drm_atomic_get_new_colorop_state);
  578. static bool
  579. plane_switching_crtc(const struct drm_plane_state *old_plane_state,
  580. const struct drm_plane_state *new_plane_state)
  581. {
  582. if (!old_plane_state->crtc || !new_plane_state->crtc)
  583. return false;
  584. if (old_plane_state->crtc == new_plane_state->crtc)
  585. return false;
  586. /* This could be refined, but currently there's no helper or driver code
  587. * to implement direct switching of active planes nor userspace to take
  588. * advantage of more direct plane switching without the intermediate
  589. * full OFF state.
  590. */
  591. return true;
  592. }
  593. /**
  594. * drm_atomic_plane_check - check plane state
  595. * @old_plane_state: old plane state to check
  596. * @new_plane_state: new plane state to check
  597. *
  598. * Provides core sanity checks for plane state.
  599. *
  600. * RETURNS:
  601. * Zero on success, error code on failure
  602. */
  603. static int drm_atomic_plane_check(const struct drm_plane_state *old_plane_state,
  604. const struct drm_plane_state *new_plane_state)
  605. {
  606. struct drm_plane *plane = new_plane_state->plane;
  607. struct drm_crtc *crtc = new_plane_state->crtc;
  608. const struct drm_framebuffer *fb = new_plane_state->fb;
  609. unsigned int fb_width, fb_height;
  610. struct drm_mode_rect *clips;
  611. uint32_t num_clips;
  612. /* either *both* CRTC and FB must be set, or neither */
  613. if (crtc && !fb) {
  614. drm_dbg_atomic(plane->dev, "[PLANE:%d:%s] CRTC set but no FB\n",
  615. plane->base.id, plane->name);
  616. return -EINVAL;
  617. } else if (fb && !crtc) {
  618. drm_dbg_atomic(plane->dev, "[PLANE:%d:%s] FB set but no CRTC\n",
  619. plane->base.id, plane->name);
  620. return -EINVAL;
  621. }
  622. /* if disabled, we don't care about the rest of the state: */
  623. if (!crtc)
  624. return 0;
  625. /* Check whether this plane is usable on this CRTC */
  626. if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
  627. drm_dbg_atomic(plane->dev,
  628. "Invalid [CRTC:%d:%s] for [PLANE:%d:%s]\n",
  629. crtc->base.id, crtc->name,
  630. plane->base.id, plane->name);
  631. return -EINVAL;
  632. }
  633. /* Check whether this plane supports the fb pixel format. */
  634. if (!drm_plane_has_format(plane, fb->format->format, fb->modifier)) {
  635. drm_dbg_atomic(plane->dev,
  636. "[PLANE:%d:%s] invalid pixel format %p4cc, modifier 0x%llx\n",
  637. plane->base.id, plane->name,
  638. &fb->format->format, fb->modifier);
  639. return -EINVAL;
  640. }
  641. /* Give drivers some help against integer overflows */
  642. if (new_plane_state->crtc_w > INT_MAX ||
  643. new_plane_state->crtc_x > INT_MAX - (int32_t) new_plane_state->crtc_w ||
  644. new_plane_state->crtc_h > INT_MAX ||
  645. new_plane_state->crtc_y > INT_MAX - (int32_t) new_plane_state->crtc_h) {
  646. drm_dbg_atomic(plane->dev,
  647. "[PLANE:%d:%s] invalid CRTC coordinates %ux%u+%d+%d\n",
  648. plane->base.id, plane->name,
  649. new_plane_state->crtc_w, new_plane_state->crtc_h,
  650. new_plane_state->crtc_x, new_plane_state->crtc_y);
  651. return -ERANGE;
  652. }
  653. fb_width = fb->width << 16;
  654. fb_height = fb->height << 16;
  655. /* Make sure source coordinates are inside the fb. */
  656. if (new_plane_state->src_w > fb_width ||
  657. new_plane_state->src_x > fb_width - new_plane_state->src_w ||
  658. new_plane_state->src_h > fb_height ||
  659. new_plane_state->src_y > fb_height - new_plane_state->src_h) {
  660. drm_dbg_atomic(plane->dev,
  661. "[PLANE:%d:%s] invalid source coordinates "
  662. "%u.%06ux%u.%06u+%u.%06u+%u.%06u (fb %ux%u)\n",
  663. plane->base.id, plane->name,
  664. new_plane_state->src_w >> 16,
  665. ((new_plane_state->src_w & 0xffff) * 15625) >> 10,
  666. new_plane_state->src_h >> 16,
  667. ((new_plane_state->src_h & 0xffff) * 15625) >> 10,
  668. new_plane_state->src_x >> 16,
  669. ((new_plane_state->src_x & 0xffff) * 15625) >> 10,
  670. new_plane_state->src_y >> 16,
  671. ((new_plane_state->src_y & 0xffff) * 15625) >> 10,
  672. fb->width, fb->height);
  673. return -ENOSPC;
  674. }
  675. clips = __drm_plane_get_damage_clips(new_plane_state);
  676. num_clips = drm_plane_get_damage_clips_count(new_plane_state);
  677. /* Make sure damage clips are valid and inside the fb. */
  678. while (num_clips > 0) {
  679. if (clips->x1 >= clips->x2 ||
  680. clips->y1 >= clips->y2 ||
  681. clips->x1 < 0 ||
  682. clips->y1 < 0 ||
  683. clips->x2 > fb_width ||
  684. clips->y2 > fb_height) {
  685. drm_dbg_atomic(plane->dev,
  686. "[PLANE:%d:%s] invalid damage clip %d %d %d %d\n",
  687. plane->base.id, plane->name, clips->x1,
  688. clips->y1, clips->x2, clips->y2);
  689. return -EINVAL;
  690. }
  691. clips++;
  692. num_clips--;
  693. }
  694. if (plane_switching_crtc(old_plane_state, new_plane_state)) {
  695. drm_dbg_atomic(plane->dev,
  696. "[PLANE:%d:%s] switching CRTC directly\n",
  697. plane->base.id, plane->name);
  698. return -EINVAL;
  699. }
  700. return 0;
  701. }
  702. static void drm_atomic_colorop_print_state(struct drm_printer *p,
  703. const struct drm_colorop_state *state)
  704. {
  705. struct drm_colorop *colorop = state->colorop;
  706. drm_printf(p, "colorop[%u]:\n", colorop->base.id);
  707. drm_printf(p, "\ttype=%s\n", drm_get_colorop_type_name(colorop->type));
  708. if (colorop->bypass_property)
  709. drm_printf(p, "\tbypass=%u\n", state->bypass);
  710. switch (colorop->type) {
  711. case DRM_COLOROP_1D_CURVE:
  712. drm_printf(p, "\tcurve_1d_type=%s\n",
  713. drm_get_colorop_curve_1d_type_name(state->curve_1d_type));
  714. break;
  715. case DRM_COLOROP_1D_LUT:
  716. drm_printf(p, "\tsize=%d\n", colorop->size);
  717. drm_printf(p, "\tinterpolation=%s\n",
  718. drm_get_colorop_lut1d_interpolation_name(colorop->lut1d_interpolation));
  719. drm_printf(p, "\tdata blob id=%d\n", state->data ? state->data->base.id : 0);
  720. break;
  721. case DRM_COLOROP_CTM_3X4:
  722. drm_printf(p, "\tdata blob id=%d\n", state->data ? state->data->base.id : 0);
  723. break;
  724. case DRM_COLOROP_MULTIPLIER:
  725. drm_printf(p, "\tmultiplier=%llu\n", state->multiplier);
  726. break;
  727. case DRM_COLOROP_3D_LUT:
  728. drm_printf(p, "\tsize=%d\n", colorop->size);
  729. drm_printf(p, "\tinterpolation=%s\n",
  730. drm_get_colorop_lut3d_interpolation_name(colorop->lut3d_interpolation));
  731. drm_printf(p, "\tdata blob id=%d\n", state->data ? state->data->base.id : 0);
  732. break;
  733. default:
  734. break;
  735. }
  736. drm_printf(p, "\tnext=%d\n", colorop->next ? colorop->next->base.id : 0);
  737. }
  738. static void drm_atomic_plane_print_state(struct drm_printer *p,
  739. const struct drm_plane_state *state)
  740. {
  741. struct drm_plane *plane = state->plane;
  742. struct drm_rect src = drm_plane_state_src(state);
  743. struct drm_rect dest = drm_plane_state_dest(state);
  744. drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name);
  745. drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
  746. drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0);
  747. if (state->fb)
  748. drm_framebuffer_print_info(p, 2, state->fb);
  749. drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest));
  750. drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src));
  751. drm_printf(p, "\trotation=%x\n", state->rotation);
  752. drm_printf(p, "\tnormalized-zpos=%x\n", state->normalized_zpos);
  753. drm_printf(p, "\tcolor-encoding=%s\n",
  754. drm_get_color_encoding_name(state->color_encoding));
  755. drm_printf(p, "\tcolor-range=%s\n",
  756. drm_get_color_range_name(state->color_range));
  757. drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
  758. drm_printf(p, "\tcolor-pipeline=%d\n",
  759. state->color_pipeline ? state->color_pipeline->base.id : 0);
  760. if (plane->funcs->atomic_print_state)
  761. plane->funcs->atomic_print_state(p, state);
  762. }
  763. /**
  764. * DOC: handling driver private state
  765. *
  766. * Very often the DRM objects exposed to userspace in the atomic modeset api
  767. * (&drm_connector, &drm_crtc and &drm_plane) do not map neatly to the
  768. * underlying hardware. Especially for any kind of shared resources (e.g. shared
  769. * clocks, scaler units, bandwidth and fifo limits shared among a group of
  770. * planes or CRTCs, and so on) it makes sense to model these as independent
  771. * objects. Drivers then need to do similar state tracking and commit ordering for
  772. * such private (since not exposed to userspace) objects as the atomic core and
  773. * helpers already provide for connectors, planes and CRTCs.
  774. *
  775. * To make this easier on drivers the atomic core provides some support to track
  776. * driver private state objects using struct &drm_private_obj, with the
  777. * associated state struct &drm_private_state.
  778. *
  779. * Similar to userspace-exposed objects, private state structures can be
  780. * acquired by calling drm_atomic_get_private_obj_state(). This also takes care
  781. * of locking, hence drivers should not have a need to call drm_modeset_lock()
  782. * directly. Sequence of the actual hardware state commit is not handled,
  783. * drivers might need to keep track of struct drm_crtc_commit within subclassed
  784. * structure of &drm_private_state as necessary, e.g. similar to
  785. * &drm_plane_state.commit. See also &drm_atomic_state.fake_commit.
  786. *
  787. * All private state structures contained in a &drm_atomic_state update can be
  788. * iterated using for_each_oldnew_private_obj_in_state(),
  789. * for_each_new_private_obj_in_state() and for_each_old_private_obj_in_state().
  790. * Drivers are recommended to wrap these for each type of driver private state
  791. * object they have, filtering on &drm_private_obj.funcs using for_each_if(), at
  792. * least if they want to iterate over all objects of a given type.
  793. *
  794. * An earlier way to handle driver private state was by subclassing struct
  795. * &drm_atomic_state. But since that encourages non-standard ways to implement
  796. * the check/commit split atomic requires (by using e.g. "check and rollback or
  797. * commit instead" of "duplicate state, check, then either commit or release
  798. * duplicated state) it is deprecated in favour of using &drm_private_state.
  799. */
  800. /**
  801. * drm_atomic_private_obj_init - initialize private object
  802. * @dev: DRM device this object will be attached to
  803. * @obj: private object
  804. * @state: initial private object state
  805. * @funcs: pointer to the struct of function pointers that identify the object
  806. * type
  807. *
  808. * Initialize the private object, which can be embedded into any
  809. * driver private object that needs its own atomic state.
  810. */
  811. void
  812. drm_atomic_private_obj_init(struct drm_device *dev,
  813. struct drm_private_obj *obj,
  814. struct drm_private_state *state,
  815. const struct drm_private_state_funcs *funcs)
  816. {
  817. memset(obj, 0, sizeof(*obj));
  818. drm_modeset_lock_init(&obj->lock);
  819. obj->dev = dev;
  820. obj->state = state;
  821. obj->funcs = funcs;
  822. list_add_tail(&obj->head, &dev->mode_config.privobj_list);
  823. state->obj = obj;
  824. }
  825. EXPORT_SYMBOL(drm_atomic_private_obj_init);
  826. /**
  827. * drm_atomic_private_obj_fini - finalize private object
  828. * @obj: private object
  829. *
  830. * Finalize the private object.
  831. */
  832. void
  833. drm_atomic_private_obj_fini(struct drm_private_obj *obj)
  834. {
  835. list_del(&obj->head);
  836. obj->funcs->atomic_destroy_state(obj, obj->state);
  837. drm_modeset_lock_fini(&obj->lock);
  838. }
  839. EXPORT_SYMBOL(drm_atomic_private_obj_fini);
  840. /**
  841. * drm_atomic_get_private_obj_state - get private object state
  842. * @state: global atomic state
  843. * @obj: private object to get the state for
  844. *
  845. * This function returns the private object state for the given private object,
  846. * allocating the state if needed. It will also grab the relevant private
  847. * object lock to make sure that the state is consistent.
  848. *
  849. * RETURNS:
  850. * Either the allocated state or the error code encoded into a pointer.
  851. */
  852. struct drm_private_state *
  853. drm_atomic_get_private_obj_state(struct drm_atomic_state *state,
  854. struct drm_private_obj *obj)
  855. {
  856. int index, num_objs, ret;
  857. size_t size;
  858. struct __drm_private_objs_state *arr;
  859. struct drm_private_state *obj_state;
  860. WARN_ON(!state->acquire_ctx);
  861. drm_WARN_ON(state->dev, state->checked);
  862. obj_state = drm_atomic_get_new_private_obj_state(state, obj);
  863. if (obj_state)
  864. return obj_state;
  865. ret = drm_modeset_lock(&obj->lock, state->acquire_ctx);
  866. if (ret)
  867. return ERR_PTR(ret);
  868. num_objs = state->num_private_objs + 1;
  869. size = sizeof(*state->private_objs) * num_objs;
  870. arr = krealloc(state->private_objs, size, GFP_KERNEL);
  871. if (!arr)
  872. return ERR_PTR(-ENOMEM);
  873. state->private_objs = arr;
  874. index = state->num_private_objs;
  875. memset(&state->private_objs[index], 0, sizeof(*state->private_objs));
  876. obj_state = obj->funcs->atomic_duplicate_state(obj);
  877. if (!obj_state)
  878. return ERR_PTR(-ENOMEM);
  879. state->private_objs[index].state_to_destroy = obj_state;
  880. state->private_objs[index].old_state = obj->state;
  881. state->private_objs[index].new_state = obj_state;
  882. state->private_objs[index].ptr = obj;
  883. obj_state->state = state;
  884. state->num_private_objs = num_objs;
  885. drm_dbg_atomic(state->dev,
  886. "Added new private object %p state %p to %p\n",
  887. obj, obj_state, state);
  888. return obj_state;
  889. }
  890. EXPORT_SYMBOL(drm_atomic_get_private_obj_state);
  891. /**
  892. * drm_atomic_get_old_private_obj_state
  893. * @state: global atomic state object
  894. * @obj: private_obj to grab
  895. *
  896. * This function returns the old private object state for the given private_obj,
  897. * or NULL if the private_obj is not part of the global atomic state.
  898. */
  899. struct drm_private_state *
  900. drm_atomic_get_old_private_obj_state(const struct drm_atomic_state *state,
  901. struct drm_private_obj *obj)
  902. {
  903. int i;
  904. for (i = 0; i < state->num_private_objs; i++)
  905. if (obj == state->private_objs[i].ptr)
  906. return state->private_objs[i].old_state;
  907. return NULL;
  908. }
  909. EXPORT_SYMBOL(drm_atomic_get_old_private_obj_state);
  910. /**
  911. * drm_atomic_get_new_private_obj_state
  912. * @state: global atomic state object
  913. * @obj: private_obj to grab
  914. *
  915. * This function returns the new private object state for the given private_obj,
  916. * or NULL if the private_obj is not part of the global atomic state.
  917. */
  918. struct drm_private_state *
  919. drm_atomic_get_new_private_obj_state(const struct drm_atomic_state *state,
  920. struct drm_private_obj *obj)
  921. {
  922. int i;
  923. for (i = 0; i < state->num_private_objs; i++)
  924. if (obj == state->private_objs[i].ptr)
  925. return state->private_objs[i].new_state;
  926. return NULL;
  927. }
  928. EXPORT_SYMBOL(drm_atomic_get_new_private_obj_state);
  929. /**
  930. * drm_atomic_get_old_connector_for_encoder - Get old connector for an encoder
  931. * @state: Atomic state
  932. * @encoder: The encoder to fetch the connector state for
  933. *
  934. * This function finds and returns the connector that was connected to @encoder
  935. * as specified by the @state.
  936. *
  937. * If there is no connector in @state which previously had @encoder connected to
  938. * it, this function will return NULL. While this may seem like an invalid use
  939. * case, it is sometimes useful to differentiate commits which had no prior
  940. * connectors attached to @encoder vs ones that did (and to inspect their
  941. * state). This is especially true in enable hooks because the pipeline has
  942. * changed.
  943. *
  944. * If you don't have access to the atomic state, see
  945. * drm_atomic_get_connector_for_encoder().
  946. *
  947. * Returns: The old connector connected to @encoder, or NULL if the encoder is
  948. * not connected.
  949. */
  950. struct drm_connector *
  951. drm_atomic_get_old_connector_for_encoder(const struct drm_atomic_state *state,
  952. struct drm_encoder *encoder)
  953. {
  954. struct drm_connector_state *conn_state;
  955. struct drm_connector *connector;
  956. unsigned int i;
  957. for_each_old_connector_in_state(state, connector, conn_state, i) {
  958. if (conn_state->best_encoder == encoder)
  959. return connector;
  960. }
  961. return NULL;
  962. }
  963. EXPORT_SYMBOL(drm_atomic_get_old_connector_for_encoder);
  964. /**
  965. * drm_atomic_get_new_connector_for_encoder - Get new connector for an encoder
  966. * @state: Atomic state
  967. * @encoder: The encoder to fetch the connector state for
  968. *
  969. * This function finds and returns the connector that will be connected to
  970. * @encoder as specified by the @state.
  971. *
  972. * If there is no connector in @state which will have @encoder connected to it,
  973. * this function will return NULL. While this may seem like an invalid use case,
  974. * it is sometimes useful to differentiate commits which have no connectors
  975. * attached to @encoder vs ones that do (and to inspect their state). This is
  976. * especially true in disable hooks because the pipeline will change.
  977. *
  978. * If you don't have access to the atomic state, see
  979. * drm_atomic_get_connector_for_encoder().
  980. *
  981. * Returns: The new connector connected to @encoder, or NULL if the encoder is
  982. * not connected.
  983. */
  984. struct drm_connector *
  985. drm_atomic_get_new_connector_for_encoder(const struct drm_atomic_state *state,
  986. struct drm_encoder *encoder)
  987. {
  988. struct drm_connector_state *conn_state;
  989. struct drm_connector *connector;
  990. unsigned int i;
  991. for_each_new_connector_in_state(state, connector, conn_state, i) {
  992. if (conn_state->best_encoder == encoder)
  993. return connector;
  994. }
  995. return NULL;
  996. }
  997. EXPORT_SYMBOL(drm_atomic_get_new_connector_for_encoder);
  998. /**
  999. * drm_atomic_get_connector_for_encoder - Get connector currently assigned to an encoder
  1000. * @encoder: The encoder to find the connector of
  1001. * @ctx: Modeset locking context
  1002. *
  1003. * This function finds and returns the connector currently assigned to
  1004. * an @encoder.
  1005. *
  1006. * It is similar to the drm_atomic_get_old_connector_for_encoder() and
  1007. * drm_atomic_get_new_connector_for_encoder() helpers, but doesn't
  1008. * require access to the atomic state. If you have access to it, prefer
  1009. * using these. This helper is typically useful in situations where you
  1010. * don't have access to the atomic state, like detect, link repair,
  1011. * threaded interrupt handlers, or hooks from other frameworks (ALSA,
  1012. * CEC, etc.).
  1013. *
  1014. * Returns:
  1015. * The connector connected to @encoder, or an error pointer otherwise.
  1016. * When the error is EDEADLK, a deadlock has been detected and the
  1017. * sequence must be restarted.
  1018. */
  1019. struct drm_connector *
  1020. drm_atomic_get_connector_for_encoder(const struct drm_encoder *encoder,
  1021. struct drm_modeset_acquire_ctx *ctx)
  1022. {
  1023. struct drm_connector_list_iter conn_iter;
  1024. struct drm_connector *out_connector = ERR_PTR(-EINVAL);
  1025. struct drm_connector *connector;
  1026. struct drm_device *dev = encoder->dev;
  1027. int ret;
  1028. ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx);
  1029. if (ret)
  1030. return ERR_PTR(ret);
  1031. drm_connector_list_iter_begin(dev, &conn_iter);
  1032. drm_for_each_connector_iter(connector, &conn_iter) {
  1033. if (!connector->state)
  1034. continue;
  1035. if (encoder == connector->state->best_encoder) {
  1036. out_connector = connector;
  1037. break;
  1038. }
  1039. }
  1040. drm_connector_list_iter_end(&conn_iter);
  1041. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  1042. return out_connector;
  1043. }
  1044. EXPORT_SYMBOL(drm_atomic_get_connector_for_encoder);
  1045. /**
  1046. * drm_atomic_get_old_crtc_for_encoder - Get old crtc for an encoder
  1047. * @state: Atomic state
  1048. * @encoder: The encoder to fetch the crtc state for
  1049. *
  1050. * This function finds and returns the crtc that was connected to @encoder
  1051. * as specified by the @state.
  1052. *
  1053. * Returns: The old crtc connected to @encoder, or NULL if the encoder is
  1054. * not connected.
  1055. */
  1056. struct drm_crtc *
  1057. drm_atomic_get_old_crtc_for_encoder(struct drm_atomic_state *state,
  1058. struct drm_encoder *encoder)
  1059. {
  1060. struct drm_connector *connector;
  1061. struct drm_connector_state *conn_state;
  1062. connector = drm_atomic_get_old_connector_for_encoder(state, encoder);
  1063. if (!connector)
  1064. return NULL;
  1065. conn_state = drm_atomic_get_old_connector_state(state, connector);
  1066. if (!conn_state)
  1067. return NULL;
  1068. return conn_state->crtc;
  1069. }
  1070. EXPORT_SYMBOL(drm_atomic_get_old_crtc_for_encoder);
  1071. /**
  1072. * drm_atomic_get_new_crtc_for_encoder - Get new crtc for an encoder
  1073. * @state: Atomic state
  1074. * @encoder: The encoder to fetch the crtc state for
  1075. *
  1076. * This function finds and returns the crtc that will be connected to @encoder
  1077. * as specified by the @state.
  1078. *
  1079. * Returns: The new crtc connected to @encoder, or NULL if the encoder is
  1080. * not connected.
  1081. */
  1082. struct drm_crtc *
  1083. drm_atomic_get_new_crtc_for_encoder(struct drm_atomic_state *state,
  1084. struct drm_encoder *encoder)
  1085. {
  1086. struct drm_connector *connector;
  1087. struct drm_connector_state *conn_state;
  1088. connector = drm_atomic_get_new_connector_for_encoder(state, encoder);
  1089. if (!connector)
  1090. return NULL;
  1091. conn_state = drm_atomic_get_new_connector_state(state, connector);
  1092. if (!conn_state)
  1093. return NULL;
  1094. return conn_state->crtc;
  1095. }
  1096. EXPORT_SYMBOL(drm_atomic_get_new_crtc_for_encoder);
  1097. /**
  1098. * drm_atomic_get_connector_state - get connector state
  1099. * @state: global atomic state object
  1100. * @connector: connector to get state object for
  1101. *
  1102. * This function returns the connector state for the given connector,
  1103. * allocating it if needed. It will also grab the relevant connector lock to
  1104. * make sure that the state is consistent.
  1105. *
  1106. * Returns:
  1107. * Either the allocated state or the error code encoded into the pointer. When
  1108. * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
  1109. * entire atomic sequence must be restarted. All other errors are fatal.
  1110. */
  1111. struct drm_connector_state *
  1112. drm_atomic_get_connector_state(struct drm_atomic_state *state,
  1113. struct drm_connector *connector)
  1114. {
  1115. int ret, index;
  1116. struct drm_mode_config *config = &connector->dev->mode_config;
  1117. struct drm_connector_state *connector_state;
  1118. WARN_ON(!state->acquire_ctx);
  1119. drm_WARN_ON(state->dev, state->checked);
  1120. ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
  1121. if (ret)
  1122. return ERR_PTR(ret);
  1123. index = drm_connector_index(connector);
  1124. if (index >= state->num_connector) {
  1125. struct __drm_connnectors_state *c;
  1126. int alloc = max(index + 1, config->num_connector);
  1127. c = krealloc_array(state->connectors, alloc,
  1128. sizeof(*state->connectors), GFP_KERNEL);
  1129. if (!c)
  1130. return ERR_PTR(-ENOMEM);
  1131. state->connectors = c;
  1132. memset(&state->connectors[state->num_connector], 0,
  1133. sizeof(*state->connectors) * (alloc - state->num_connector));
  1134. state->num_connector = alloc;
  1135. }
  1136. connector_state = drm_atomic_get_new_connector_state(state, connector);
  1137. if (connector_state)
  1138. return connector_state;
  1139. connector_state = connector->funcs->atomic_duplicate_state(connector);
  1140. if (!connector_state)
  1141. return ERR_PTR(-ENOMEM);
  1142. drm_connector_get(connector);
  1143. state->connectors[index].state_to_destroy = connector_state;
  1144. state->connectors[index].old_state = connector->state;
  1145. state->connectors[index].new_state = connector_state;
  1146. state->connectors[index].ptr = connector;
  1147. connector_state->state = state;
  1148. drm_dbg_atomic(connector->dev, "Added [CONNECTOR:%d:%s] %p state to %p\n",
  1149. connector->base.id, connector->name,
  1150. connector_state, state);
  1151. if (connector_state->crtc) {
  1152. struct drm_crtc_state *crtc_state;
  1153. crtc_state = drm_atomic_get_crtc_state(state,
  1154. connector_state->crtc);
  1155. if (IS_ERR(crtc_state))
  1156. return ERR_CAST(crtc_state);
  1157. }
  1158. return connector_state;
  1159. }
  1160. EXPORT_SYMBOL(drm_atomic_get_connector_state);
  1161. static void drm_atomic_connector_print_state(struct drm_printer *p,
  1162. const struct drm_connector_state *state)
  1163. {
  1164. struct drm_connector *connector = state->connector;
  1165. drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
  1166. drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
  1167. drm_printf(p, "\tself_refresh_aware=%d\n", state->self_refresh_aware);
  1168. drm_printf(p, "\tinterlace_allowed=%d\n", connector->interlace_allowed);
  1169. drm_printf(p, "\tycbcr_420_allowed=%d\n", connector->ycbcr_420_allowed);
  1170. drm_printf(p, "\tmax_requested_bpc=%d\n", state->max_requested_bpc);
  1171. drm_printf(p, "\tcolorspace=%s\n", drm_get_colorspace_name(state->colorspace));
  1172. if (connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
  1173. connector->connector_type == DRM_MODE_CONNECTOR_HDMIB) {
  1174. drm_printf(p, "\tbroadcast_rgb=%s\n",
  1175. drm_hdmi_connector_get_broadcast_rgb_name(state->hdmi.broadcast_rgb));
  1176. drm_printf(p, "\tis_limited_range=%c\n", state->hdmi.is_limited_range ? 'y' : 'n');
  1177. drm_printf(p, "\toutput_bpc=%u\n", state->hdmi.output_bpc);
  1178. drm_printf(p, "\toutput_format=%s\n",
  1179. drm_hdmi_connector_get_output_format_name(state->hdmi.output_format));
  1180. drm_printf(p, "\ttmds_char_rate=%llu\n", state->hdmi.tmds_char_rate);
  1181. }
  1182. if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK)
  1183. if (state->writeback_job && state->writeback_job->fb)
  1184. drm_printf(p, "\tfb=%d\n", state->writeback_job->fb->base.id);
  1185. if (connector->funcs->atomic_print_state)
  1186. connector->funcs->atomic_print_state(p, state);
  1187. }
  1188. /**
  1189. * drm_atomic_get_bridge_state - get bridge state
  1190. * @state: global atomic state object
  1191. * @bridge: bridge to get state object for
  1192. *
  1193. * This function returns the bridge state for the given bridge, allocating it
  1194. * if needed. It will also grab the relevant bridge lock to make sure that the
  1195. * state is consistent.
  1196. *
  1197. * Returns:
  1198. * Either the allocated state or the error code encoded into the pointer. When
  1199. * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
  1200. * entire atomic sequence must be restarted.
  1201. */
  1202. struct drm_bridge_state *
  1203. drm_atomic_get_bridge_state(struct drm_atomic_state *state,
  1204. struct drm_bridge *bridge)
  1205. {
  1206. struct drm_private_state *obj_state;
  1207. obj_state = drm_atomic_get_private_obj_state(state, &bridge->base);
  1208. if (IS_ERR(obj_state))
  1209. return ERR_CAST(obj_state);
  1210. return drm_priv_to_bridge_state(obj_state);
  1211. }
  1212. EXPORT_SYMBOL(drm_atomic_get_bridge_state);
  1213. /**
  1214. * drm_atomic_get_old_bridge_state - get old bridge state, if it exists
  1215. * @state: global atomic state object
  1216. * @bridge: bridge to grab
  1217. *
  1218. * This function returns the old bridge state for the given bridge, or NULL if
  1219. * the bridge is not part of the global atomic state.
  1220. */
  1221. struct drm_bridge_state *
  1222. drm_atomic_get_old_bridge_state(const struct drm_atomic_state *state,
  1223. struct drm_bridge *bridge)
  1224. {
  1225. struct drm_private_state *obj_state;
  1226. obj_state = drm_atomic_get_old_private_obj_state(state, &bridge->base);
  1227. if (!obj_state)
  1228. return NULL;
  1229. return drm_priv_to_bridge_state(obj_state);
  1230. }
  1231. EXPORT_SYMBOL(drm_atomic_get_old_bridge_state);
  1232. /**
  1233. * drm_atomic_get_new_bridge_state - get new bridge state, if it exists
  1234. * @state: global atomic state object
  1235. * @bridge: bridge to grab
  1236. *
  1237. * This function returns the new bridge state for the given bridge, or NULL if
  1238. * the bridge is not part of the global atomic state.
  1239. */
  1240. struct drm_bridge_state *
  1241. drm_atomic_get_new_bridge_state(const struct drm_atomic_state *state,
  1242. struct drm_bridge *bridge)
  1243. {
  1244. struct drm_private_state *obj_state;
  1245. obj_state = drm_atomic_get_new_private_obj_state(state, &bridge->base);
  1246. if (!obj_state)
  1247. return NULL;
  1248. return drm_priv_to_bridge_state(obj_state);
  1249. }
  1250. EXPORT_SYMBOL(drm_atomic_get_new_bridge_state);
  1251. /**
  1252. * drm_atomic_add_encoder_bridges - add bridges attached to an encoder
  1253. * @state: atomic state
  1254. * @encoder: DRM encoder
  1255. *
  1256. * This function adds all bridges attached to @encoder. This is needed to add
  1257. * bridge states to @state and make them available when
  1258. * &drm_bridge_funcs.atomic_check(), &drm_bridge_funcs.atomic_pre_enable(),
  1259. * &drm_bridge_funcs.atomic_enable(),
  1260. * &drm_bridge_funcs.atomic_disable_post_disable() are called.
  1261. *
  1262. * Returns:
  1263. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  1264. * then the w/w mutex code has detected a deadlock and the entire atomic
  1265. * sequence must be restarted. All other errors are fatal.
  1266. */
  1267. int
  1268. drm_atomic_add_encoder_bridges(struct drm_atomic_state *state,
  1269. struct drm_encoder *encoder)
  1270. {
  1271. struct drm_bridge_state *bridge_state;
  1272. if (!encoder)
  1273. return 0;
  1274. drm_dbg_atomic(encoder->dev,
  1275. "Adding all bridges for [encoder:%d:%s] to %p\n",
  1276. encoder->base.id, encoder->name, state);
  1277. drm_for_each_bridge_in_chain_scoped(encoder, bridge) {
  1278. /* Skip bridges that don't implement the atomic state hooks. */
  1279. if (!bridge->funcs->atomic_duplicate_state)
  1280. continue;
  1281. bridge_state = drm_atomic_get_bridge_state(state, bridge);
  1282. if (IS_ERR(bridge_state))
  1283. return PTR_ERR(bridge_state);
  1284. }
  1285. return 0;
  1286. }
  1287. EXPORT_SYMBOL(drm_atomic_add_encoder_bridges);
  1288. /**
  1289. * drm_atomic_add_affected_connectors - add connectors for CRTC
  1290. * @state: atomic state
  1291. * @crtc: DRM CRTC
  1292. *
  1293. * This function walks the current configuration and adds all connectors
  1294. * currently using @crtc to the atomic configuration @state. Note that this
  1295. * function must acquire the connection mutex. This can potentially cause
  1296. * unneeded serialization if the update is just for the planes on one CRTC. Hence
  1297. * drivers and helpers should only call this when really needed (e.g. when a
  1298. * full modeset needs to happen due to some change).
  1299. *
  1300. * Returns:
  1301. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  1302. * then the w/w mutex code has detected a deadlock and the entire atomic
  1303. * sequence must be restarted. All other errors are fatal.
  1304. */
  1305. int
  1306. drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
  1307. struct drm_crtc *crtc)
  1308. {
  1309. struct drm_mode_config *config = &state->dev->mode_config;
  1310. struct drm_connector *connector;
  1311. struct drm_connector_state *conn_state;
  1312. struct drm_connector_list_iter conn_iter;
  1313. struct drm_crtc_state *crtc_state;
  1314. int ret;
  1315. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  1316. if (IS_ERR(crtc_state))
  1317. return PTR_ERR(crtc_state);
  1318. ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
  1319. if (ret)
  1320. return ret;
  1321. drm_dbg_atomic(crtc->dev,
  1322. "Adding all current connectors for [CRTC:%d:%s] to %p\n",
  1323. crtc->base.id, crtc->name, state);
  1324. /*
  1325. * Changed connectors are already in @state, so only need to look
  1326. * at the connector_mask in crtc_state.
  1327. */
  1328. drm_connector_list_iter_begin(state->dev, &conn_iter);
  1329. drm_for_each_connector_iter(connector, &conn_iter) {
  1330. if (!(crtc_state->connector_mask & drm_connector_mask(connector)))
  1331. continue;
  1332. conn_state = drm_atomic_get_connector_state(state, connector);
  1333. if (IS_ERR(conn_state)) {
  1334. drm_connector_list_iter_end(&conn_iter);
  1335. return PTR_ERR(conn_state);
  1336. }
  1337. }
  1338. drm_connector_list_iter_end(&conn_iter);
  1339. return 0;
  1340. }
  1341. EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
  1342. /**
  1343. * drm_atomic_add_affected_planes - add planes for CRTC
  1344. * @state: atomic state
  1345. * @crtc: DRM CRTC
  1346. *
  1347. * This function walks the current configuration and adds all planes
  1348. * currently used by @crtc to the atomic configuration @state. This is useful
  1349. * when an atomic commit also needs to check all currently enabled plane on
  1350. * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
  1351. * to avoid special code to force-enable all planes.
  1352. *
  1353. * Since acquiring a plane state will always also acquire the w/w mutex of the
  1354. * current CRTC for that plane (if there is any) adding all the plane states for
  1355. * a CRTC will not reduce parallelism of atomic updates.
  1356. *
  1357. * Returns:
  1358. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  1359. * then the w/w mutex code has detected a deadlock and the entire atomic
  1360. * sequence must be restarted. All other errors are fatal.
  1361. */
  1362. int
  1363. drm_atomic_add_affected_planes(struct drm_atomic_state *state,
  1364. struct drm_crtc *crtc)
  1365. {
  1366. const struct drm_crtc_state *old_crtc_state =
  1367. drm_atomic_get_old_crtc_state(state, crtc);
  1368. struct drm_plane *plane;
  1369. WARN_ON(!drm_atomic_get_new_crtc_state(state, crtc));
  1370. drm_dbg_atomic(crtc->dev,
  1371. "Adding all current planes for [CRTC:%d:%s] to %p\n",
  1372. crtc->base.id, crtc->name, state);
  1373. drm_for_each_plane_mask(plane, state->dev, old_crtc_state->plane_mask) {
  1374. struct drm_plane_state *plane_state =
  1375. drm_atomic_get_plane_state(state, plane);
  1376. if (IS_ERR(plane_state))
  1377. return PTR_ERR(plane_state);
  1378. }
  1379. return 0;
  1380. }
  1381. EXPORT_SYMBOL(drm_atomic_add_affected_planes);
  1382. /**
  1383. * drm_atomic_add_affected_colorops - add colorops for plane
  1384. * @state: atomic state
  1385. * @plane: DRM plane
  1386. *
  1387. * This function walks the current configuration and adds all colorops
  1388. * currently used by @plane to the atomic configuration @state. This is useful
  1389. * when an atomic commit also needs to check all currently enabled colorop on
  1390. * @plane, e.g. when changing the mode. It's also useful when re-enabling a plane
  1391. * to avoid special code to force-enable all colorops.
  1392. *
  1393. * Since acquiring a colorop state will always also acquire the w/w mutex of the
  1394. * current plane for that colorop (if there is any) adding all the colorop states for
  1395. * a plane will not reduce parallelism of atomic updates.
  1396. *
  1397. * Returns:
  1398. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  1399. * then the w/w mutex code has detected a deadlock and the entire atomic
  1400. * sequence must be restarted. All other errors are fatal.
  1401. */
  1402. int
  1403. drm_atomic_add_affected_colorops(struct drm_atomic_state *state,
  1404. struct drm_plane *plane)
  1405. {
  1406. struct drm_colorop *colorop;
  1407. struct drm_colorop_state *colorop_state;
  1408. WARN_ON(!drm_atomic_get_new_plane_state(state, plane));
  1409. drm_dbg_atomic(plane->dev,
  1410. "Adding all current colorops for [PLANE:%d:%s] to %p\n",
  1411. plane->base.id, plane->name, state);
  1412. drm_for_each_colorop(colorop, plane->dev) {
  1413. if (colorop->plane != plane)
  1414. continue;
  1415. colorop_state = drm_atomic_get_colorop_state(state, colorop);
  1416. if (IS_ERR(colorop_state))
  1417. return PTR_ERR(colorop_state);
  1418. }
  1419. return 0;
  1420. }
  1421. EXPORT_SYMBOL(drm_atomic_add_affected_colorops);
  1422. /**
  1423. * drm_atomic_check_only - check whether a given config would work
  1424. * @state: atomic configuration to check
  1425. *
  1426. * Note that this function can return -EDEADLK if the driver needed to acquire
  1427. * more locks but encountered a deadlock. The caller must then do the usual w/w
  1428. * backoff dance and restart. All other errors are fatal.
  1429. *
  1430. * Returns:
  1431. * 0 on success, negative error code on failure.
  1432. */
  1433. int drm_atomic_check_only(struct drm_atomic_state *state)
  1434. {
  1435. struct drm_device *dev = state->dev;
  1436. struct drm_mode_config *config = &dev->mode_config;
  1437. struct drm_plane *plane;
  1438. struct drm_plane_state *old_plane_state;
  1439. struct drm_plane_state *new_plane_state;
  1440. struct drm_crtc *crtc;
  1441. struct drm_crtc_state *old_crtc_state;
  1442. struct drm_crtc_state *new_crtc_state;
  1443. struct drm_connector *conn;
  1444. struct drm_connector_state *conn_state;
  1445. unsigned int requested_crtc = 0;
  1446. unsigned int affected_crtc = 0;
  1447. int i, ret = 0;
  1448. drm_dbg_atomic(dev, "checking %p\n", state);
  1449. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  1450. if (new_crtc_state->enable)
  1451. requested_crtc |= drm_crtc_mask(crtc);
  1452. }
  1453. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
  1454. ret = drm_atomic_plane_check(old_plane_state, new_plane_state);
  1455. if (ret) {
  1456. drm_dbg_atomic(dev, "[PLANE:%d:%s] atomic core check failed\n",
  1457. plane->base.id, plane->name);
  1458. return ret;
  1459. }
  1460. }
  1461. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  1462. ret = drm_atomic_crtc_check(old_crtc_state, new_crtc_state);
  1463. if (ret) {
  1464. drm_dbg_atomic(dev, "[CRTC:%d:%s] atomic core check failed\n",
  1465. crtc->base.id, crtc->name);
  1466. return ret;
  1467. }
  1468. }
  1469. for_each_new_connector_in_state(state, conn, conn_state, i) {
  1470. ret = drm_atomic_connector_check(conn, conn_state);
  1471. if (ret) {
  1472. drm_dbg_atomic(dev, "[CONNECTOR:%d:%s] atomic core check failed\n",
  1473. conn->base.id, conn->name);
  1474. return ret;
  1475. }
  1476. }
  1477. if (config->funcs->atomic_check) {
  1478. ret = config->funcs->atomic_check(state->dev, state);
  1479. if (ret) {
  1480. drm_dbg_atomic(dev, "atomic driver check for %p failed: %d\n",
  1481. state, ret);
  1482. return ret;
  1483. }
  1484. }
  1485. if (!state->allow_modeset) {
  1486. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  1487. if (drm_atomic_crtc_needs_modeset(new_crtc_state)) {
  1488. drm_dbg_atomic(dev, "[CRTC:%d:%s] requires full modeset\n",
  1489. crtc->base.id, crtc->name);
  1490. return -EINVAL;
  1491. }
  1492. }
  1493. }
  1494. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  1495. if (new_crtc_state->enable)
  1496. affected_crtc |= drm_crtc_mask(crtc);
  1497. }
  1498. /*
  1499. * For commits that allow modesets drivers can add other CRTCs to the
  1500. * atomic commit, e.g. when they need to reallocate global resources.
  1501. * This can cause spurious EBUSY, which robs compositors of a very
  1502. * effective sanity check for their drawing loop. Therefor only allow
  1503. * drivers to add unrelated CRTC states for modeset commits.
  1504. *
  1505. * FIXME: Should add affected_crtc mask to the ATOMIC IOCTL as an output
  1506. * so compositors know what's going on.
  1507. */
  1508. if (affected_crtc != requested_crtc) {
  1509. drm_dbg_atomic(dev,
  1510. "driver added CRTC to commit: requested 0x%x, affected 0x%0x\n",
  1511. requested_crtc, affected_crtc);
  1512. WARN(!state->allow_modeset, "adding CRTC not allowed without modesets: requested 0x%x, affected 0x%0x\n",
  1513. requested_crtc, affected_crtc);
  1514. }
  1515. state->checked = true;
  1516. return 0;
  1517. }
  1518. EXPORT_SYMBOL(drm_atomic_check_only);
  1519. /**
  1520. * drm_atomic_commit - commit configuration atomically
  1521. * @state: atomic configuration to check
  1522. *
  1523. * Note that this function can return -EDEADLK if the driver needed to acquire
  1524. * more locks but encountered a deadlock. The caller must then do the usual w/w
  1525. * backoff dance and restart. All other errors are fatal.
  1526. *
  1527. * This function will take its own reference on @state.
  1528. * Callers should always release their reference with drm_atomic_state_put().
  1529. *
  1530. * Returns:
  1531. * 0 on success, negative error code on failure.
  1532. */
  1533. int drm_atomic_commit(struct drm_atomic_state *state)
  1534. {
  1535. struct drm_mode_config *config = &state->dev->mode_config;
  1536. struct drm_printer p = drm_info_printer(state->dev->dev);
  1537. int ret;
  1538. if (drm_debug_enabled(DRM_UT_STATE))
  1539. drm_atomic_print_new_state(state, &p);
  1540. ret = drm_atomic_check_only(state);
  1541. if (ret)
  1542. return ret;
  1543. drm_dbg_atomic(state->dev, "committing %p\n", state);
  1544. return config->funcs->atomic_commit(state->dev, state, false);
  1545. }
  1546. EXPORT_SYMBOL(drm_atomic_commit);
  1547. /**
  1548. * drm_atomic_nonblocking_commit - atomic nonblocking commit
  1549. * @state: atomic configuration to check
  1550. *
  1551. * Note that this function can return -EDEADLK if the driver needed to acquire
  1552. * more locks but encountered a deadlock. The caller must then do the usual w/w
  1553. * backoff dance and restart. All other errors are fatal.
  1554. *
  1555. * This function will take its own reference on @state.
  1556. * Callers should always release their reference with drm_atomic_state_put().
  1557. *
  1558. * Returns:
  1559. * 0 on success, negative error code on failure.
  1560. */
  1561. int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
  1562. {
  1563. struct drm_mode_config *config = &state->dev->mode_config;
  1564. int ret;
  1565. ret = drm_atomic_check_only(state);
  1566. if (ret)
  1567. return ret;
  1568. drm_dbg_atomic(state->dev, "committing %p nonblocking\n", state);
  1569. return config->funcs->atomic_commit(state->dev, state, true);
  1570. }
  1571. EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
  1572. /* just used from drm-client and atomic-helper: */
  1573. int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
  1574. struct drm_plane_state *plane_state)
  1575. {
  1576. int ret;
  1577. ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
  1578. if (ret != 0)
  1579. return ret;
  1580. drm_atomic_set_fb_for_plane(plane_state, NULL);
  1581. plane_state->crtc_x = 0;
  1582. plane_state->crtc_y = 0;
  1583. plane_state->crtc_w = 0;
  1584. plane_state->crtc_h = 0;
  1585. plane_state->src_x = 0;
  1586. plane_state->src_y = 0;
  1587. plane_state->src_w = 0;
  1588. plane_state->src_h = 0;
  1589. return 0;
  1590. }
  1591. EXPORT_SYMBOL(__drm_atomic_helper_disable_plane);
  1592. static int update_output_state(struct drm_atomic_state *state,
  1593. struct drm_mode_set *set)
  1594. {
  1595. struct drm_device *dev = set->crtc->dev;
  1596. struct drm_crtc *crtc;
  1597. struct drm_crtc_state *new_crtc_state;
  1598. struct drm_connector *connector;
  1599. struct drm_connector_state *new_conn_state;
  1600. int ret, i;
  1601. ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
  1602. state->acquire_ctx);
  1603. if (ret)
  1604. return ret;
  1605. /* First disable all connectors on the target crtc. */
  1606. ret = drm_atomic_add_affected_connectors(state, set->crtc);
  1607. if (ret)
  1608. return ret;
  1609. for_each_new_connector_in_state(state, connector, new_conn_state, i) {
  1610. if (new_conn_state->crtc == set->crtc) {
  1611. ret = drm_atomic_set_crtc_for_connector(new_conn_state,
  1612. NULL);
  1613. if (ret)
  1614. return ret;
  1615. /* Make sure legacy setCrtc always re-trains */
  1616. new_conn_state->link_status = DRM_LINK_STATUS_GOOD;
  1617. }
  1618. }
  1619. /* Then set all connectors from set->connectors on the target crtc */
  1620. for (i = 0; i < set->num_connectors; i++) {
  1621. new_conn_state = drm_atomic_get_connector_state(state,
  1622. set->connectors[i]);
  1623. if (IS_ERR(new_conn_state))
  1624. return PTR_ERR(new_conn_state);
  1625. ret = drm_atomic_set_crtc_for_connector(new_conn_state,
  1626. set->crtc);
  1627. if (ret)
  1628. return ret;
  1629. }
  1630. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  1631. /*
  1632. * Don't update ->enable for the CRTC in the set_config request,
  1633. * since a mismatch would indicate a bug in the upper layers.
  1634. * The actual modeset code later on will catch any
  1635. * inconsistencies here.
  1636. */
  1637. if (crtc == set->crtc)
  1638. continue;
  1639. if (!new_crtc_state->connector_mask) {
  1640. ret = drm_atomic_set_mode_prop_for_crtc(new_crtc_state,
  1641. NULL);
  1642. if (ret < 0)
  1643. return ret;
  1644. new_crtc_state->active = false;
  1645. }
  1646. }
  1647. return 0;
  1648. }
  1649. /* just used from drm-client and atomic-helper: */
  1650. int __drm_atomic_helper_set_config(struct drm_mode_set *set,
  1651. struct drm_atomic_state *state)
  1652. {
  1653. struct drm_crtc_state *crtc_state;
  1654. struct drm_plane_state *primary_state;
  1655. struct drm_crtc *crtc = set->crtc;
  1656. int hdisplay, vdisplay;
  1657. int ret;
  1658. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  1659. if (IS_ERR(crtc_state))
  1660. return PTR_ERR(crtc_state);
  1661. primary_state = drm_atomic_get_plane_state(state, crtc->primary);
  1662. if (IS_ERR(primary_state))
  1663. return PTR_ERR(primary_state);
  1664. if (!set->mode) {
  1665. WARN_ON(set->fb);
  1666. WARN_ON(set->num_connectors);
  1667. ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
  1668. if (ret != 0)
  1669. return ret;
  1670. crtc_state->active = false;
  1671. ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
  1672. if (ret != 0)
  1673. return ret;
  1674. drm_atomic_set_fb_for_plane(primary_state, NULL);
  1675. goto commit;
  1676. }
  1677. WARN_ON(!set->fb);
  1678. WARN_ON(!set->num_connectors);
  1679. ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
  1680. if (ret != 0)
  1681. return ret;
  1682. crtc_state->active = true;
  1683. ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
  1684. if (ret != 0)
  1685. return ret;
  1686. drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay);
  1687. drm_atomic_set_fb_for_plane(primary_state, set->fb);
  1688. primary_state->crtc_x = 0;
  1689. primary_state->crtc_y = 0;
  1690. primary_state->crtc_w = hdisplay;
  1691. primary_state->crtc_h = vdisplay;
  1692. primary_state->src_x = set->x << 16;
  1693. primary_state->src_y = set->y << 16;
  1694. if (drm_rotation_90_or_270(primary_state->rotation)) {
  1695. primary_state->src_w = vdisplay << 16;
  1696. primary_state->src_h = hdisplay << 16;
  1697. } else {
  1698. primary_state->src_w = hdisplay << 16;
  1699. primary_state->src_h = vdisplay << 16;
  1700. }
  1701. commit:
  1702. ret = update_output_state(state, set);
  1703. if (ret)
  1704. return ret;
  1705. return 0;
  1706. }
  1707. EXPORT_SYMBOL(__drm_atomic_helper_set_config);
  1708. static void drm_atomic_private_obj_print_state(struct drm_printer *p,
  1709. const struct drm_private_state *state)
  1710. {
  1711. struct drm_private_obj *obj = state->obj;
  1712. if (obj->funcs->atomic_print_state)
  1713. obj->funcs->atomic_print_state(p, state);
  1714. }
  1715. /**
  1716. * drm_atomic_print_new_state - prints drm atomic state
  1717. * @state: atomic configuration to check
  1718. * @p: drm printer
  1719. *
  1720. * This functions prints the drm atomic state snapshot using the drm printer
  1721. * which is passed to it. This snapshot can be used for debugging purposes.
  1722. *
  1723. * Note that this function looks into the new state objects and hence its not
  1724. * safe to be used after the call to drm_atomic_helper_commit_hw_done().
  1725. */
  1726. void drm_atomic_print_new_state(const struct drm_atomic_state *state,
  1727. struct drm_printer *p)
  1728. {
  1729. struct drm_plane *plane;
  1730. struct drm_plane_state *plane_state;
  1731. struct drm_crtc *crtc;
  1732. struct drm_crtc_state *crtc_state;
  1733. struct drm_connector *connector;
  1734. struct drm_connector_state *connector_state;
  1735. struct drm_private_obj *obj;
  1736. struct drm_private_state *obj_state;
  1737. int i;
  1738. if (!p) {
  1739. drm_err(state->dev, "invalid drm printer\n");
  1740. return;
  1741. }
  1742. drm_dbg_atomic(state->dev, "checking %p\n", state);
  1743. for_each_new_plane_in_state(state, plane, plane_state, i)
  1744. drm_atomic_plane_print_state(p, plane_state);
  1745. for_each_new_crtc_in_state(state, crtc, crtc_state, i)
  1746. drm_atomic_crtc_print_state(p, crtc_state);
  1747. for_each_new_connector_in_state(state, connector, connector_state, i)
  1748. drm_atomic_connector_print_state(p, connector_state);
  1749. for_each_new_private_obj_in_state(state, obj, obj_state, i)
  1750. drm_atomic_private_obj_print_state(p, obj_state);
  1751. }
  1752. EXPORT_SYMBOL(drm_atomic_print_new_state);
  1753. static void __drm_state_dump(struct drm_device *dev, struct drm_printer *p,
  1754. bool take_locks)
  1755. {
  1756. struct drm_mode_config *config = &dev->mode_config;
  1757. struct drm_colorop *colorop;
  1758. struct drm_plane *plane;
  1759. struct drm_crtc *crtc;
  1760. struct drm_connector *connector;
  1761. struct drm_connector_list_iter conn_iter;
  1762. struct drm_private_obj *obj;
  1763. if (!drm_drv_uses_atomic_modeset(dev))
  1764. return;
  1765. list_for_each_entry(colorop, &config->colorop_list, head) {
  1766. if (take_locks)
  1767. drm_modeset_lock(&colorop->plane->mutex, NULL);
  1768. drm_atomic_colorop_print_state(p, colorop->state);
  1769. if (take_locks)
  1770. drm_modeset_unlock(&colorop->plane->mutex);
  1771. }
  1772. list_for_each_entry(plane, &config->plane_list, head) {
  1773. if (take_locks)
  1774. drm_modeset_lock(&plane->mutex, NULL);
  1775. drm_atomic_plane_print_state(p, plane->state);
  1776. if (take_locks)
  1777. drm_modeset_unlock(&plane->mutex);
  1778. }
  1779. list_for_each_entry(crtc, &config->crtc_list, head) {
  1780. if (take_locks)
  1781. drm_modeset_lock(&crtc->mutex, NULL);
  1782. drm_atomic_crtc_print_state(p, crtc->state);
  1783. if (take_locks)
  1784. drm_modeset_unlock(&crtc->mutex);
  1785. }
  1786. drm_connector_list_iter_begin(dev, &conn_iter);
  1787. if (take_locks)
  1788. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  1789. drm_for_each_connector_iter(connector, &conn_iter)
  1790. drm_atomic_connector_print_state(p, connector->state);
  1791. if (take_locks)
  1792. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  1793. drm_connector_list_iter_end(&conn_iter);
  1794. list_for_each_entry(obj, &config->privobj_list, head) {
  1795. if (take_locks)
  1796. drm_modeset_lock(&obj->lock, NULL);
  1797. drm_atomic_private_obj_print_state(p, obj->state);
  1798. if (take_locks)
  1799. drm_modeset_unlock(&obj->lock);
  1800. }
  1801. }
  1802. /**
  1803. * drm_state_dump - dump entire device atomic state
  1804. * @dev: the drm device
  1805. * @p: where to print the state to
  1806. *
  1807. * Just for debugging. Drivers might want an option to dump state
  1808. * to dmesg in case of error irq's. (Hint, you probably want to
  1809. * ratelimit this!)
  1810. *
  1811. * The caller must wrap this drm_modeset_lock_all_ctx() and
  1812. * drm_modeset_drop_locks(). If this is called from error irq handler, it should
  1813. * not be enabled by default - if you are debugging errors you might
  1814. * not care that this is racey, but calling this without all modeset locks held
  1815. * is inherently unsafe.
  1816. */
  1817. void drm_state_dump(struct drm_device *dev, struct drm_printer *p)
  1818. {
  1819. __drm_state_dump(dev, p, false);
  1820. }
  1821. EXPORT_SYMBOL(drm_state_dump);
  1822. #ifdef CONFIG_DEBUG_FS
  1823. static int drm_state_info(struct seq_file *m, void *data)
  1824. {
  1825. struct drm_debugfs_entry *entry = m->private;
  1826. struct drm_device *dev = entry->dev;
  1827. struct drm_printer p = drm_seq_file_printer(m);
  1828. __drm_state_dump(dev, &p, true);
  1829. return 0;
  1830. }
  1831. /* any use in debugfs files to dump individual planes/crtc/etc? */
  1832. static const struct drm_debugfs_info drm_atomic_debugfs_list[] = {
  1833. {"state", drm_state_info, 0},
  1834. };
  1835. void drm_atomic_debugfs_init(struct drm_device *dev)
  1836. {
  1837. drm_debugfs_add_files(dev, drm_atomic_debugfs_list,
  1838. ARRAY_SIZE(drm_atomic_debugfs_list));
  1839. }
  1840. #endif