drm_atomic.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318
  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_mode.h>
  30. #include <drm/drm_plane_helper.h>
  31. #include <drm/drm_print.h>
  32. #include <linux/sync_file.h>
  33. #include "drm_crtc_internal.h"
  34. void __drm_crtc_commit_free(struct kref *kref)
  35. {
  36. struct drm_crtc_commit *commit =
  37. container_of(kref, struct drm_crtc_commit, ref);
  38. kfree(commit);
  39. }
  40. EXPORT_SYMBOL(__drm_crtc_commit_free);
  41. /**
  42. * drm_atomic_state_default_release -
  43. * release memory initialized by drm_atomic_state_init
  44. * @state: atomic state
  45. *
  46. * Free all the memory allocated by drm_atomic_state_init.
  47. * This is useful for drivers that subclass the atomic state.
  48. */
  49. void drm_atomic_state_default_release(struct drm_atomic_state *state)
  50. {
  51. kfree(state->connectors);
  52. kfree(state->crtcs);
  53. kfree(state->planes);
  54. }
  55. EXPORT_SYMBOL(drm_atomic_state_default_release);
  56. /**
  57. * drm_atomic_state_init - init new atomic state
  58. * @dev: DRM device
  59. * @state: atomic state
  60. *
  61. * Default implementation for filling in a new atomic state.
  62. * This is useful for drivers that subclass the atomic state.
  63. */
  64. int
  65. drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
  66. {
  67. kref_init(&state->ref);
  68. /* TODO legacy paths should maybe do a better job about
  69. * setting this appropriately?
  70. */
  71. state->allow_modeset = true;
  72. state->crtcs = kcalloc(dev->mode_config.num_crtc,
  73. sizeof(*state->crtcs), GFP_KERNEL);
  74. if (!state->crtcs)
  75. goto fail;
  76. state->planes = kcalloc(dev->mode_config.num_total_plane,
  77. sizeof(*state->planes), GFP_KERNEL);
  78. if (!state->planes)
  79. goto fail;
  80. state->dev = dev;
  81. DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
  82. return 0;
  83. fail:
  84. drm_atomic_state_default_release(state);
  85. return -ENOMEM;
  86. }
  87. EXPORT_SYMBOL(drm_atomic_state_init);
  88. /**
  89. * drm_atomic_state_alloc - allocate atomic state
  90. * @dev: DRM device
  91. *
  92. * This allocates an empty atomic state to track updates.
  93. */
  94. struct drm_atomic_state *
  95. drm_atomic_state_alloc(struct drm_device *dev)
  96. {
  97. struct drm_mode_config *config = &dev->mode_config;
  98. struct drm_atomic_state *state;
  99. if (!config->funcs->atomic_state_alloc) {
  100. state = kzalloc(sizeof(*state), GFP_KERNEL);
  101. if (!state)
  102. return NULL;
  103. if (drm_atomic_state_init(dev, state) < 0) {
  104. kfree(state);
  105. return NULL;
  106. }
  107. return state;
  108. }
  109. return config->funcs->atomic_state_alloc(dev);
  110. }
  111. EXPORT_SYMBOL(drm_atomic_state_alloc);
  112. /**
  113. * drm_atomic_state_default_clear - clear base atomic state
  114. * @state: atomic state
  115. *
  116. * Default implementation for clearing atomic state.
  117. * This is useful for drivers that subclass the atomic state.
  118. */
  119. void drm_atomic_state_default_clear(struct drm_atomic_state *state)
  120. {
  121. struct drm_device *dev = state->dev;
  122. struct drm_mode_config *config = &dev->mode_config;
  123. int i;
  124. DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
  125. for (i = 0; i < state->num_connector; i++) {
  126. struct drm_connector *connector = state->connectors[i].ptr;
  127. if (!connector)
  128. continue;
  129. connector->funcs->atomic_destroy_state(connector,
  130. state->connectors[i].state);
  131. state->connectors[i].ptr = NULL;
  132. state->connectors[i].state = NULL;
  133. drm_connector_unreference(connector);
  134. }
  135. for (i = 0; i < config->num_crtc; i++) {
  136. struct drm_crtc *crtc = state->crtcs[i].ptr;
  137. if (!crtc)
  138. continue;
  139. crtc->funcs->atomic_destroy_state(crtc,
  140. state->crtcs[i].state);
  141. if (state->crtcs[i].commit) {
  142. kfree(state->crtcs[i].commit->event);
  143. state->crtcs[i].commit->event = NULL;
  144. drm_crtc_commit_put(state->crtcs[i].commit);
  145. }
  146. state->crtcs[i].commit = NULL;
  147. state->crtcs[i].ptr = NULL;
  148. state->crtcs[i].state = NULL;
  149. }
  150. for (i = 0; i < config->num_total_plane; i++) {
  151. struct drm_plane *plane = state->planes[i].ptr;
  152. if (!plane)
  153. continue;
  154. plane->funcs->atomic_destroy_state(plane,
  155. state->planes[i].state);
  156. state->planes[i].ptr = NULL;
  157. state->planes[i].state = NULL;
  158. }
  159. }
  160. EXPORT_SYMBOL(drm_atomic_state_default_clear);
  161. /**
  162. * drm_atomic_state_clear - clear state object
  163. * @state: atomic state
  164. *
  165. * When the w/w mutex algorithm detects a deadlock we need to back off and drop
  166. * all locks. So someone else could sneak in and change the current modeset
  167. * configuration. Which means that all the state assembled in @state is no
  168. * longer an atomic update to the current state, but to some arbitrary earlier
  169. * state. Which could break assumptions the driver's
  170. * &drm_mode_config_funcs.atomic_check likely relies on.
  171. *
  172. * Hence we must clear all cached state and completely start over, using this
  173. * function.
  174. */
  175. void drm_atomic_state_clear(struct drm_atomic_state *state)
  176. {
  177. struct drm_device *dev = state->dev;
  178. struct drm_mode_config *config = &dev->mode_config;
  179. if (config->funcs->atomic_state_clear)
  180. config->funcs->atomic_state_clear(state);
  181. else
  182. drm_atomic_state_default_clear(state);
  183. }
  184. EXPORT_SYMBOL(drm_atomic_state_clear);
  185. /**
  186. * __drm_atomic_state_free - free all memory for an atomic state
  187. * @ref: This atomic state to deallocate
  188. *
  189. * This frees all memory associated with an atomic state, including all the
  190. * per-object state for planes, crtcs and connectors.
  191. */
  192. void __drm_atomic_state_free(struct kref *ref)
  193. {
  194. struct drm_atomic_state *state = container_of(ref, typeof(*state), ref);
  195. struct drm_mode_config *config = &state->dev->mode_config;
  196. drm_atomic_state_clear(state);
  197. DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
  198. if (config->funcs->atomic_state_free) {
  199. config->funcs->atomic_state_free(state);
  200. } else {
  201. drm_atomic_state_default_release(state);
  202. kfree(state);
  203. }
  204. }
  205. EXPORT_SYMBOL(__drm_atomic_state_free);
  206. /**
  207. * drm_atomic_get_crtc_state - get crtc state
  208. * @state: global atomic state object
  209. * @crtc: crtc to get state object for
  210. *
  211. * This function returns the crtc state for the given crtc, allocating it if
  212. * needed. It will also grab the relevant crtc lock to make sure that the state
  213. * is consistent.
  214. *
  215. * Returns:
  216. *
  217. * Either the allocated state or the error code encoded into the pointer. When
  218. * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
  219. * entire atomic sequence must be restarted. All other errors are fatal.
  220. */
  221. struct drm_crtc_state *
  222. drm_atomic_get_crtc_state(struct drm_atomic_state *state,
  223. struct drm_crtc *crtc)
  224. {
  225. int ret, index = drm_crtc_index(crtc);
  226. struct drm_crtc_state *crtc_state;
  227. WARN_ON(!state->acquire_ctx);
  228. crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
  229. if (crtc_state)
  230. return crtc_state;
  231. ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
  232. if (ret)
  233. return ERR_PTR(ret);
  234. crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
  235. if (!crtc_state)
  236. return ERR_PTR(-ENOMEM);
  237. state->crtcs[index].state = crtc_state;
  238. state->crtcs[index].ptr = crtc;
  239. crtc_state->state = state;
  240. DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
  241. crtc->base.id, crtc->name, crtc_state, state);
  242. return crtc_state;
  243. }
  244. EXPORT_SYMBOL(drm_atomic_get_crtc_state);
  245. static void set_out_fence_for_crtc(struct drm_atomic_state *state,
  246. struct drm_crtc *crtc, s64 __user *fence_ptr)
  247. {
  248. state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
  249. }
  250. static s64 __user *get_out_fence_for_crtc(struct drm_atomic_state *state,
  251. struct drm_crtc *crtc)
  252. {
  253. s64 __user *fence_ptr;
  254. fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
  255. state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;
  256. return fence_ptr;
  257. }
  258. /**
  259. * drm_atomic_set_mode_for_crtc - set mode for CRTC
  260. * @state: the CRTC whose incoming state to update
  261. * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
  262. *
  263. * Set a mode (originating from the kernel) on the desired CRTC state and update
  264. * the enable property.
  265. *
  266. * RETURNS:
  267. * Zero on success, error code on failure. Cannot return -EDEADLK.
  268. */
  269. int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
  270. struct drm_display_mode *mode)
  271. {
  272. struct drm_mode_modeinfo umode;
  273. /* Early return for no change. */
  274. if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
  275. return 0;
  276. drm_property_unreference_blob(state->mode_blob);
  277. state->mode_blob = NULL;
  278. if (mode) {
  279. drm_mode_convert_to_umode(&umode, mode);
  280. state->mode_blob =
  281. drm_property_create_blob(state->crtc->dev,
  282. sizeof(umode),
  283. &umode);
  284. if (IS_ERR(state->mode_blob))
  285. return PTR_ERR(state->mode_blob);
  286. drm_mode_copy(&state->mode, mode);
  287. state->enable = true;
  288. DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
  289. mode->name, state);
  290. } else {
  291. memset(&state->mode, 0, sizeof(state->mode));
  292. state->enable = false;
  293. DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
  294. state);
  295. }
  296. return 0;
  297. }
  298. EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
  299. /**
  300. * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
  301. * @state: the CRTC whose incoming state to update
  302. * @blob: pointer to blob property to use for mode
  303. *
  304. * Set a mode (originating from a blob property) on the desired CRTC state.
  305. * This function will take a reference on the blob property for the CRTC state,
  306. * and release the reference held on the state's existing mode property, if any
  307. * was set.
  308. *
  309. * RETURNS:
  310. * Zero on success, error code on failure. Cannot return -EDEADLK.
  311. */
  312. int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
  313. struct drm_property_blob *blob)
  314. {
  315. if (blob == state->mode_blob)
  316. return 0;
  317. drm_property_unreference_blob(state->mode_blob);
  318. state->mode_blob = NULL;
  319. memset(&state->mode, 0, sizeof(state->mode));
  320. if (blob) {
  321. if (blob->length != sizeof(struct drm_mode_modeinfo) ||
  322. drm_mode_convert_umode(&state->mode,
  323. (const struct drm_mode_modeinfo *)
  324. blob->data))
  325. return -EINVAL;
  326. state->mode_blob = drm_property_reference_blob(blob);
  327. state->enable = true;
  328. DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
  329. state->mode.name, state);
  330. } else {
  331. state->enable = false;
  332. DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
  333. state);
  334. }
  335. return 0;
  336. }
  337. EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
  338. /**
  339. * drm_atomic_replace_property_blob - replace a blob property
  340. * @blob: a pointer to the member blob to be replaced
  341. * @new_blob: the new blob to replace with
  342. * @replaced: whether the blob has been replaced
  343. *
  344. * RETURNS:
  345. * Zero on success, error code on failure
  346. */
  347. static void
  348. drm_atomic_replace_property_blob(struct drm_property_blob **blob,
  349. struct drm_property_blob *new_blob,
  350. bool *replaced)
  351. {
  352. struct drm_property_blob *old_blob = *blob;
  353. if (old_blob == new_blob)
  354. return;
  355. drm_property_unreference_blob(old_blob);
  356. if (new_blob)
  357. drm_property_reference_blob(new_blob);
  358. *blob = new_blob;
  359. *replaced = true;
  360. return;
  361. }
  362. static int
  363. drm_atomic_replace_property_blob_from_id(struct drm_crtc *crtc,
  364. struct drm_property_blob **blob,
  365. uint64_t blob_id,
  366. ssize_t expected_size,
  367. bool *replaced)
  368. {
  369. struct drm_property_blob *new_blob = NULL;
  370. if (blob_id != 0) {
  371. new_blob = drm_property_lookup_blob(crtc->dev, blob_id);
  372. if (new_blob == NULL)
  373. return -EINVAL;
  374. if (expected_size > 0 && expected_size != new_blob->length) {
  375. drm_property_unreference_blob(new_blob);
  376. return -EINVAL;
  377. }
  378. }
  379. drm_atomic_replace_property_blob(blob, new_blob, replaced);
  380. drm_property_unreference_blob(new_blob);
  381. return 0;
  382. }
  383. /**
  384. * drm_atomic_crtc_set_property - set property on CRTC
  385. * @crtc: the drm CRTC to set a property on
  386. * @state: the state object to update with the new property value
  387. * @property: the property to set
  388. * @val: the new property value
  389. *
  390. * This function handles generic/core properties and calls out to driver's
  391. * &drm_crtc_funcs.atomic_set_property for driver properties. To ensure
  392. * consistent behavior you must call this function rather than the driver hook
  393. * directly.
  394. *
  395. * RETURNS:
  396. * Zero on success, error code on failure
  397. */
  398. int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
  399. struct drm_crtc_state *state, struct drm_property *property,
  400. uint64_t val)
  401. {
  402. struct drm_device *dev = crtc->dev;
  403. struct drm_mode_config *config = &dev->mode_config;
  404. bool replaced = false;
  405. int ret;
  406. if (property == config->prop_active)
  407. state->active = val;
  408. else if (property == config->prop_mode_id) {
  409. struct drm_property_blob *mode =
  410. drm_property_lookup_blob(dev, val);
  411. ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
  412. drm_property_unreference_blob(mode);
  413. return ret;
  414. } else if (property == config->degamma_lut_property) {
  415. ret = drm_atomic_replace_property_blob_from_id(crtc,
  416. &state->degamma_lut,
  417. val,
  418. -1,
  419. &replaced);
  420. state->color_mgmt_changed |= replaced;
  421. return ret;
  422. } else if (property == config->ctm_property) {
  423. ret = drm_atomic_replace_property_blob_from_id(crtc,
  424. &state->ctm,
  425. val,
  426. sizeof(struct drm_color_ctm),
  427. &replaced);
  428. state->color_mgmt_changed |= replaced;
  429. return ret;
  430. } else if (property == config->gamma_lut_property) {
  431. ret = drm_atomic_replace_property_blob_from_id(crtc,
  432. &state->gamma_lut,
  433. val,
  434. -1,
  435. &replaced);
  436. state->color_mgmt_changed |= replaced;
  437. return ret;
  438. } else if (property == config->prop_out_fence_ptr) {
  439. s64 __user *fence_ptr = u64_to_user_ptr(val);
  440. if (!fence_ptr)
  441. return 0;
  442. if (put_user(-1, fence_ptr))
  443. return -EFAULT;
  444. set_out_fence_for_crtc(state->state, crtc, fence_ptr);
  445. } else if (crtc->funcs->atomic_set_property)
  446. return crtc->funcs->atomic_set_property(crtc, state, property, val);
  447. else
  448. return -EINVAL;
  449. return 0;
  450. }
  451. EXPORT_SYMBOL(drm_atomic_crtc_set_property);
  452. /**
  453. * drm_atomic_crtc_get_property - get property value from CRTC state
  454. * @crtc: the drm CRTC to set a property on
  455. * @state: the state object to get the property value from
  456. * @property: the property to set
  457. * @val: return location for the property value
  458. *
  459. * This function handles generic/core properties and calls out to driver's
  460. * &drm_crtc_funcs.atomic_get_property for driver properties. To ensure
  461. * consistent behavior you must call this function rather than the driver hook
  462. * directly.
  463. *
  464. * RETURNS:
  465. * Zero on success, error code on failure
  466. */
  467. static int
  468. drm_atomic_crtc_get_property(struct drm_crtc *crtc,
  469. const struct drm_crtc_state *state,
  470. struct drm_property *property, uint64_t *val)
  471. {
  472. struct drm_device *dev = crtc->dev;
  473. struct drm_mode_config *config = &dev->mode_config;
  474. if (property == config->prop_active)
  475. *val = state->active;
  476. else if (property == config->prop_mode_id)
  477. *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
  478. else if (property == config->degamma_lut_property)
  479. *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
  480. else if (property == config->ctm_property)
  481. *val = (state->ctm) ? state->ctm->base.id : 0;
  482. else if (property == config->gamma_lut_property)
  483. *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
  484. else if (property == config->prop_out_fence_ptr)
  485. *val = 0;
  486. else if (crtc->funcs->atomic_get_property)
  487. return crtc->funcs->atomic_get_property(crtc, state, property, val);
  488. else
  489. return -EINVAL;
  490. return 0;
  491. }
  492. /**
  493. * drm_atomic_crtc_check - check crtc state
  494. * @crtc: crtc to check
  495. * @state: crtc state to check
  496. *
  497. * Provides core sanity checks for crtc state.
  498. *
  499. * RETURNS:
  500. * Zero on success, error code on failure
  501. */
  502. static int drm_atomic_crtc_check(struct drm_crtc *crtc,
  503. struct drm_crtc_state *state)
  504. {
  505. /* NOTE: we explicitly don't enforce constraints such as primary
  506. * layer covering entire screen, since that is something we want
  507. * to allow (on hw that supports it). For hw that does not, it
  508. * should be checked in driver's crtc->atomic_check() vfunc.
  509. *
  510. * TODO: Add generic modeset state checks once we support those.
  511. */
  512. if (state->active && !state->enable) {
  513. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
  514. crtc->base.id, crtc->name);
  515. return -EINVAL;
  516. }
  517. /* The state->enable vs. state->mode_blob checks can be WARN_ON,
  518. * as this is a kernel-internal detail that userspace should never
  519. * be able to trigger. */
  520. if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
  521. WARN_ON(state->enable && !state->mode_blob)) {
  522. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
  523. crtc->base.id, crtc->name);
  524. return -EINVAL;
  525. }
  526. if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
  527. WARN_ON(!state->enable && state->mode_blob)) {
  528. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
  529. crtc->base.id, crtc->name);
  530. return -EINVAL;
  531. }
  532. /*
  533. * Reject event generation for when a CRTC is off and stays off.
  534. * It wouldn't be hard to implement this, but userspace has a track
  535. * record of happily burning through 100% cpu (or worse, crash) when the
  536. * display pipe is suspended. To avoid all that fun just reject updates
  537. * that ask for events since likely that indicates a bug in the
  538. * compositor's drawing loop. This is consistent with the vblank IOCTL
  539. * and legacy page_flip IOCTL which also reject service on a disabled
  540. * pipe.
  541. */
  542. if (state->event && !state->active && !crtc->state->active) {
  543. DRM_DEBUG_ATOMIC("[CRTC:%d] requesting event but off\n",
  544. crtc->base.id);
  545. return -EINVAL;
  546. }
  547. return 0;
  548. }
  549. static void drm_atomic_crtc_print_state(struct drm_printer *p,
  550. const struct drm_crtc_state *state)
  551. {
  552. struct drm_crtc *crtc = state->crtc;
  553. drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name);
  554. drm_printf(p, "\tenable=%d\n", state->enable);
  555. drm_printf(p, "\tactive=%d\n", state->active);
  556. drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed);
  557. drm_printf(p, "\tmode_changed=%d\n", state->mode_changed);
  558. drm_printf(p, "\tactive_changed=%d\n", state->active_changed);
  559. drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed);
  560. drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
  561. drm_printf(p, "\tplane_mask=%x\n", state->plane_mask);
  562. drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask);
  563. drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask);
  564. drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode));
  565. if (crtc->funcs->atomic_print_state)
  566. crtc->funcs->atomic_print_state(p, state);
  567. }
  568. /**
  569. * drm_atomic_get_plane_state - get plane state
  570. * @state: global atomic state object
  571. * @plane: plane to get state object for
  572. *
  573. * This function returns the plane state for the given plane, allocating it if
  574. * needed. It will also grab the relevant plane lock to make sure that the state
  575. * is consistent.
  576. *
  577. * Returns:
  578. *
  579. * Either the allocated state or the error code encoded into the pointer. When
  580. * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
  581. * entire atomic sequence must be restarted. All other errors are fatal.
  582. */
  583. struct drm_plane_state *
  584. drm_atomic_get_plane_state(struct drm_atomic_state *state,
  585. struct drm_plane *plane)
  586. {
  587. int ret, index = drm_plane_index(plane);
  588. struct drm_plane_state *plane_state;
  589. WARN_ON(!state->acquire_ctx);
  590. plane_state = drm_atomic_get_existing_plane_state(state, plane);
  591. if (plane_state)
  592. return plane_state;
  593. ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
  594. if (ret)
  595. return ERR_PTR(ret);
  596. plane_state = plane->funcs->atomic_duplicate_state(plane);
  597. if (!plane_state)
  598. return ERR_PTR(-ENOMEM);
  599. state->planes[index].state = plane_state;
  600. state->planes[index].ptr = plane;
  601. plane_state->state = state;
  602. DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
  603. plane->base.id, plane->name, plane_state, state);
  604. if (plane_state->crtc) {
  605. struct drm_crtc_state *crtc_state;
  606. crtc_state = drm_atomic_get_crtc_state(state,
  607. plane_state->crtc);
  608. if (IS_ERR(crtc_state))
  609. return ERR_CAST(crtc_state);
  610. }
  611. return plane_state;
  612. }
  613. EXPORT_SYMBOL(drm_atomic_get_plane_state);
  614. /**
  615. * drm_atomic_plane_set_property - set property on plane
  616. * @plane: the drm plane to set a property on
  617. * @state: the state object to update with the new property value
  618. * @property: the property to set
  619. * @val: the new property value
  620. *
  621. * This function handles generic/core properties and calls out to driver's
  622. * &drm_plane_funcs.atomic_set_property for driver properties. To ensure
  623. * consistent behavior you must call this function rather than the driver hook
  624. * directly.
  625. *
  626. * RETURNS:
  627. * Zero on success, error code on failure
  628. */
  629. int drm_atomic_plane_set_property(struct drm_plane *plane,
  630. struct drm_plane_state *state, struct drm_property *property,
  631. uint64_t val)
  632. {
  633. struct drm_device *dev = plane->dev;
  634. struct drm_mode_config *config = &dev->mode_config;
  635. if (property == config->prop_fb_id) {
  636. struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
  637. drm_atomic_set_fb_for_plane(state, fb);
  638. if (fb)
  639. drm_framebuffer_unreference(fb);
  640. } else if (property == config->prop_in_fence_fd) {
  641. if (state->fence)
  642. return -EINVAL;
  643. if (U642I64(val) == -1)
  644. return 0;
  645. state->fence = sync_file_get_fence(val);
  646. if (!state->fence)
  647. return -EINVAL;
  648. } else if (property == config->prop_crtc_id) {
  649. struct drm_crtc *crtc = drm_crtc_find(dev, val);
  650. return drm_atomic_set_crtc_for_plane(state, crtc);
  651. } else if (property == config->prop_crtc_x) {
  652. state->crtc_x = U642I64(val);
  653. } else if (property == config->prop_crtc_y) {
  654. state->crtc_y = U642I64(val);
  655. } else if (property == config->prop_crtc_w) {
  656. state->crtc_w = val;
  657. } else if (property == config->prop_crtc_h) {
  658. state->crtc_h = val;
  659. } else if (property == config->prop_src_x) {
  660. state->src_x = val;
  661. } else if (property == config->prop_src_y) {
  662. state->src_y = val;
  663. } else if (property == config->prop_src_w) {
  664. state->src_w = val;
  665. } else if (property == config->prop_src_h) {
  666. state->src_h = val;
  667. } else if (property == plane->rotation_property) {
  668. if (!is_power_of_2(val & DRM_ROTATE_MASK))
  669. return -EINVAL;
  670. state->rotation = val;
  671. } else if (property == plane->zpos_property) {
  672. state->zpos = val;
  673. } else if (plane->funcs->atomic_set_property) {
  674. return plane->funcs->atomic_set_property(plane, state,
  675. property, val);
  676. } else {
  677. return -EINVAL;
  678. }
  679. return 0;
  680. }
  681. EXPORT_SYMBOL(drm_atomic_plane_set_property);
  682. /**
  683. * drm_atomic_plane_get_property - get property value from plane state
  684. * @plane: the drm plane to set a property on
  685. * @state: the state object to get the property value from
  686. * @property: the property to set
  687. * @val: return location for the property value
  688. *
  689. * This function handles generic/core properties and calls out to driver's
  690. * &drm_plane_funcs.atomic_get_property for driver properties. To ensure
  691. * consistent behavior you must call this function rather than the driver hook
  692. * directly.
  693. *
  694. * RETURNS:
  695. * Zero on success, error code on failure
  696. */
  697. static int
  698. drm_atomic_plane_get_property(struct drm_plane *plane,
  699. const struct drm_plane_state *state,
  700. struct drm_property *property, uint64_t *val)
  701. {
  702. struct drm_device *dev = plane->dev;
  703. struct drm_mode_config *config = &dev->mode_config;
  704. if (property == config->prop_fb_id) {
  705. *val = (state->fb) ? state->fb->base.id : 0;
  706. } else if (property == config->prop_in_fence_fd) {
  707. *val = -1;
  708. } else if (property == config->prop_crtc_id) {
  709. *val = (state->crtc) ? state->crtc->base.id : 0;
  710. } else if (property == config->prop_crtc_x) {
  711. *val = I642U64(state->crtc_x);
  712. } else if (property == config->prop_crtc_y) {
  713. *val = I642U64(state->crtc_y);
  714. } else if (property == config->prop_crtc_w) {
  715. *val = state->crtc_w;
  716. } else if (property == config->prop_crtc_h) {
  717. *val = state->crtc_h;
  718. } else if (property == config->prop_src_x) {
  719. *val = state->src_x;
  720. } else if (property == config->prop_src_y) {
  721. *val = state->src_y;
  722. } else if (property == config->prop_src_w) {
  723. *val = state->src_w;
  724. } else if (property == config->prop_src_h) {
  725. *val = state->src_h;
  726. } else if (property == plane->rotation_property) {
  727. *val = state->rotation;
  728. } else if (property == plane->zpos_property) {
  729. *val = state->zpos;
  730. } else if (plane->funcs->atomic_get_property) {
  731. return plane->funcs->atomic_get_property(plane, state, property, val);
  732. } else {
  733. return -EINVAL;
  734. }
  735. return 0;
  736. }
  737. static bool
  738. plane_switching_crtc(struct drm_atomic_state *state,
  739. struct drm_plane *plane,
  740. struct drm_plane_state *plane_state)
  741. {
  742. if (!plane->state->crtc || !plane_state->crtc)
  743. return false;
  744. if (plane->state->crtc == plane_state->crtc)
  745. return false;
  746. /* This could be refined, but currently there's no helper or driver code
  747. * to implement direct switching of active planes nor userspace to take
  748. * advantage of more direct plane switching without the intermediate
  749. * full OFF state.
  750. */
  751. return true;
  752. }
  753. /**
  754. * drm_atomic_plane_check - check plane state
  755. * @plane: plane to check
  756. * @state: plane state to check
  757. *
  758. * Provides core sanity checks for plane state.
  759. *
  760. * RETURNS:
  761. * Zero on success, error code on failure
  762. */
  763. static int drm_atomic_plane_check(struct drm_plane *plane,
  764. struct drm_plane_state *state)
  765. {
  766. unsigned int fb_width, fb_height;
  767. int ret;
  768. /* either *both* CRTC and FB must be set, or neither */
  769. if (WARN_ON(state->crtc && !state->fb)) {
  770. DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
  771. return -EINVAL;
  772. } else if (WARN_ON(state->fb && !state->crtc)) {
  773. DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
  774. return -EINVAL;
  775. }
  776. /* if disabled, we don't care about the rest of the state: */
  777. if (!state->crtc)
  778. return 0;
  779. /* Check whether this plane is usable on this CRTC */
  780. if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
  781. DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
  782. return -EINVAL;
  783. }
  784. /* Check whether this plane supports the fb pixel format. */
  785. ret = drm_plane_check_pixel_format(plane, state->fb->format->format);
  786. if (ret) {
  787. struct drm_format_name_buf format_name;
  788. DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
  789. drm_get_format_name(state->fb->format->format,
  790. &format_name));
  791. return ret;
  792. }
  793. /* Give drivers some help against integer overflows */
  794. if (state->crtc_w > INT_MAX ||
  795. state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
  796. state->crtc_h > INT_MAX ||
  797. state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
  798. DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
  799. state->crtc_w, state->crtc_h,
  800. state->crtc_x, state->crtc_y);
  801. return -ERANGE;
  802. }
  803. fb_width = state->fb->width << 16;
  804. fb_height = state->fb->height << 16;
  805. /* Make sure source coordinates are inside the fb. */
  806. if (state->src_w > fb_width ||
  807. state->src_x > fb_width - state->src_w ||
  808. state->src_h > fb_height ||
  809. state->src_y > fb_height - state->src_h) {
  810. DRM_DEBUG_ATOMIC("Invalid source coordinates "
  811. "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
  812. state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
  813. state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
  814. state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
  815. state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
  816. return -ENOSPC;
  817. }
  818. if (plane_switching_crtc(state->state, plane, state)) {
  819. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
  820. plane->base.id, plane->name);
  821. return -EINVAL;
  822. }
  823. return 0;
  824. }
  825. static void drm_atomic_plane_print_state(struct drm_printer *p,
  826. const struct drm_plane_state *state)
  827. {
  828. struct drm_plane *plane = state->plane;
  829. struct drm_rect src = drm_plane_state_src(state);
  830. struct drm_rect dest = drm_plane_state_dest(state);
  831. drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name);
  832. drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
  833. drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0);
  834. if (state->fb) {
  835. struct drm_framebuffer *fb = state->fb;
  836. int i, n = fb->format->num_planes;
  837. struct drm_format_name_buf format_name;
  838. drm_printf(p, "\t\tformat=%s\n",
  839. drm_get_format_name(fb->format->format, &format_name));
  840. drm_printf(p, "\t\t\tmodifier=0x%llx\n", fb->modifier);
  841. drm_printf(p, "\t\tsize=%dx%d\n", fb->width, fb->height);
  842. drm_printf(p, "\t\tlayers:\n");
  843. for (i = 0; i < n; i++) {
  844. drm_printf(p, "\t\t\tpitch[%d]=%u\n", i, fb->pitches[i]);
  845. drm_printf(p, "\t\t\toffset[%d]=%u\n", i, fb->offsets[i]);
  846. }
  847. }
  848. drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest));
  849. drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src));
  850. drm_printf(p, "\trotation=%x\n", state->rotation);
  851. if (plane->funcs->atomic_print_state)
  852. plane->funcs->atomic_print_state(p, state);
  853. }
  854. /**
  855. * drm_atomic_get_connector_state - get connector state
  856. * @state: global atomic state object
  857. * @connector: connector to get state object for
  858. *
  859. * This function returns the connector state for the given connector,
  860. * allocating it if needed. It will also grab the relevant connector lock to
  861. * make sure that the state is consistent.
  862. *
  863. * Returns:
  864. *
  865. * Either the allocated state or the error code encoded into the pointer. When
  866. * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
  867. * entire atomic sequence must be restarted. All other errors are fatal.
  868. */
  869. struct drm_connector_state *
  870. drm_atomic_get_connector_state(struct drm_atomic_state *state,
  871. struct drm_connector *connector)
  872. {
  873. int ret, index;
  874. struct drm_mode_config *config = &connector->dev->mode_config;
  875. struct drm_connector_state *connector_state;
  876. WARN_ON(!state->acquire_ctx);
  877. ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
  878. if (ret)
  879. return ERR_PTR(ret);
  880. index = drm_connector_index(connector);
  881. if (index >= state->num_connector) {
  882. struct __drm_connnectors_state *c;
  883. int alloc = max(index + 1, config->num_connector);
  884. c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL);
  885. if (!c)
  886. return ERR_PTR(-ENOMEM);
  887. state->connectors = c;
  888. memset(&state->connectors[state->num_connector], 0,
  889. sizeof(*state->connectors) * (alloc - state->num_connector));
  890. state->num_connector = alloc;
  891. }
  892. if (state->connectors[index].state)
  893. return state->connectors[index].state;
  894. connector_state = connector->funcs->atomic_duplicate_state(connector);
  895. if (!connector_state)
  896. return ERR_PTR(-ENOMEM);
  897. drm_connector_reference(connector);
  898. state->connectors[index].state = connector_state;
  899. state->connectors[index].ptr = connector;
  900. connector_state->state = state;
  901. DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
  902. connector->base.id, connector_state, state);
  903. if (connector_state->crtc) {
  904. struct drm_crtc_state *crtc_state;
  905. crtc_state = drm_atomic_get_crtc_state(state,
  906. connector_state->crtc);
  907. if (IS_ERR(crtc_state))
  908. return ERR_CAST(crtc_state);
  909. }
  910. return connector_state;
  911. }
  912. EXPORT_SYMBOL(drm_atomic_get_connector_state);
  913. /**
  914. * drm_atomic_connector_set_property - set property on connector.
  915. * @connector: the drm connector to set a property on
  916. * @state: the state object to update with the new property value
  917. * @property: the property to set
  918. * @val: the new property value
  919. *
  920. * This function handles generic/core properties and calls out to driver's
  921. * &drm_connector_funcs.atomic_set_property for driver properties. To ensure
  922. * consistent behavior you must call this function rather than the driver hook
  923. * directly.
  924. *
  925. * RETURNS:
  926. * Zero on success, error code on failure
  927. */
  928. int drm_atomic_connector_set_property(struct drm_connector *connector,
  929. struct drm_connector_state *state, struct drm_property *property,
  930. uint64_t val)
  931. {
  932. struct drm_device *dev = connector->dev;
  933. struct drm_mode_config *config = &dev->mode_config;
  934. if (property == config->prop_crtc_id) {
  935. struct drm_crtc *crtc = drm_crtc_find(dev, val);
  936. return drm_atomic_set_crtc_for_connector(state, crtc);
  937. } else if (property == config->dpms_property) {
  938. /* setting DPMS property requires special handling, which
  939. * is done in legacy setprop path for us. Disallow (for
  940. * now?) atomic writes to DPMS property:
  941. */
  942. return -EINVAL;
  943. } else if (property == config->tv_select_subconnector_property) {
  944. state->tv.subconnector = val;
  945. } else if (property == config->tv_left_margin_property) {
  946. state->tv.margins.left = val;
  947. } else if (property == config->tv_right_margin_property) {
  948. state->tv.margins.right = val;
  949. } else if (property == config->tv_top_margin_property) {
  950. state->tv.margins.top = val;
  951. } else if (property == config->tv_bottom_margin_property) {
  952. state->tv.margins.bottom = val;
  953. } else if (property == config->tv_mode_property) {
  954. state->tv.mode = val;
  955. } else if (property == config->tv_brightness_property) {
  956. state->tv.brightness = val;
  957. } else if (property == config->tv_contrast_property) {
  958. state->tv.contrast = val;
  959. } else if (property == config->tv_flicker_reduction_property) {
  960. state->tv.flicker_reduction = val;
  961. } else if (property == config->tv_overscan_property) {
  962. state->tv.overscan = val;
  963. } else if (property == config->tv_saturation_property) {
  964. state->tv.saturation = val;
  965. } else if (property == config->tv_hue_property) {
  966. state->tv.hue = val;
  967. } else if (connector->funcs->atomic_set_property) {
  968. return connector->funcs->atomic_set_property(connector,
  969. state, property, val);
  970. } else {
  971. return -EINVAL;
  972. }
  973. return 0;
  974. }
  975. EXPORT_SYMBOL(drm_atomic_connector_set_property);
  976. static void drm_atomic_connector_print_state(struct drm_printer *p,
  977. const struct drm_connector_state *state)
  978. {
  979. struct drm_connector *connector = state->connector;
  980. drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
  981. drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
  982. if (connector->funcs->atomic_print_state)
  983. connector->funcs->atomic_print_state(p, state);
  984. }
  985. /**
  986. * drm_atomic_connector_get_property - get property value from connector state
  987. * @connector: the drm connector to set a property on
  988. * @state: the state object to get the property value from
  989. * @property: the property to set
  990. * @val: return location for the property value
  991. *
  992. * This function handles generic/core properties and calls out to driver's
  993. * &drm_connector_funcs.atomic_get_property for driver properties. To ensure
  994. * consistent behavior you must call this function rather than the driver hook
  995. * directly.
  996. *
  997. * RETURNS:
  998. * Zero on success, error code on failure
  999. */
  1000. static int
  1001. drm_atomic_connector_get_property(struct drm_connector *connector,
  1002. const struct drm_connector_state *state,
  1003. struct drm_property *property, uint64_t *val)
  1004. {
  1005. struct drm_device *dev = connector->dev;
  1006. struct drm_mode_config *config = &dev->mode_config;
  1007. if (property == config->prop_crtc_id) {
  1008. *val = (state->crtc) ? state->crtc->base.id : 0;
  1009. } else if (property == config->dpms_property) {
  1010. *val = connector->dpms;
  1011. } else if (property == config->tv_select_subconnector_property) {
  1012. *val = state->tv.subconnector;
  1013. } else if (property == config->tv_left_margin_property) {
  1014. *val = state->tv.margins.left;
  1015. } else if (property == config->tv_right_margin_property) {
  1016. *val = state->tv.margins.right;
  1017. } else if (property == config->tv_top_margin_property) {
  1018. *val = state->tv.margins.top;
  1019. } else if (property == config->tv_bottom_margin_property) {
  1020. *val = state->tv.margins.bottom;
  1021. } else if (property == config->tv_mode_property) {
  1022. *val = state->tv.mode;
  1023. } else if (property == config->tv_brightness_property) {
  1024. *val = state->tv.brightness;
  1025. } else if (property == config->tv_contrast_property) {
  1026. *val = state->tv.contrast;
  1027. } else if (property == config->tv_flicker_reduction_property) {
  1028. *val = state->tv.flicker_reduction;
  1029. } else if (property == config->tv_overscan_property) {
  1030. *val = state->tv.overscan;
  1031. } else if (property == config->tv_saturation_property) {
  1032. *val = state->tv.saturation;
  1033. } else if (property == config->tv_hue_property) {
  1034. *val = state->tv.hue;
  1035. } else if (connector->funcs->atomic_get_property) {
  1036. return connector->funcs->atomic_get_property(connector,
  1037. state, property, val);
  1038. } else {
  1039. return -EINVAL;
  1040. }
  1041. return 0;
  1042. }
  1043. int drm_atomic_get_property(struct drm_mode_object *obj,
  1044. struct drm_property *property, uint64_t *val)
  1045. {
  1046. struct drm_device *dev = property->dev;
  1047. int ret;
  1048. switch (obj->type) {
  1049. case DRM_MODE_OBJECT_CONNECTOR: {
  1050. struct drm_connector *connector = obj_to_connector(obj);
  1051. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  1052. ret = drm_atomic_connector_get_property(connector,
  1053. connector->state, property, val);
  1054. break;
  1055. }
  1056. case DRM_MODE_OBJECT_CRTC: {
  1057. struct drm_crtc *crtc = obj_to_crtc(obj);
  1058. WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
  1059. ret = drm_atomic_crtc_get_property(crtc,
  1060. crtc->state, property, val);
  1061. break;
  1062. }
  1063. case DRM_MODE_OBJECT_PLANE: {
  1064. struct drm_plane *plane = obj_to_plane(obj);
  1065. WARN_ON(!drm_modeset_is_locked(&plane->mutex));
  1066. ret = drm_atomic_plane_get_property(plane,
  1067. plane->state, property, val);
  1068. break;
  1069. }
  1070. default:
  1071. ret = -EINVAL;
  1072. break;
  1073. }
  1074. return ret;
  1075. }
  1076. /**
  1077. * drm_atomic_set_crtc_for_plane - set crtc for plane
  1078. * @plane_state: the plane whose incoming state to update
  1079. * @crtc: crtc to use for the plane
  1080. *
  1081. * Changing the assigned crtc for a plane requires us to grab the lock and state
  1082. * for the new crtc, as needed. This function takes care of all these details
  1083. * besides updating the pointer in the state object itself.
  1084. *
  1085. * Returns:
  1086. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  1087. * then the w/w mutex code has detected a deadlock and the entire atomic
  1088. * sequence must be restarted. All other errors are fatal.
  1089. */
  1090. int
  1091. drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
  1092. struct drm_crtc *crtc)
  1093. {
  1094. struct drm_plane *plane = plane_state->plane;
  1095. struct drm_crtc_state *crtc_state;
  1096. if (plane_state->crtc) {
  1097. crtc_state = drm_atomic_get_crtc_state(plane_state->state,
  1098. plane_state->crtc);
  1099. if (WARN_ON(IS_ERR(crtc_state)))
  1100. return PTR_ERR(crtc_state);
  1101. crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
  1102. }
  1103. plane_state->crtc = crtc;
  1104. if (crtc) {
  1105. crtc_state = drm_atomic_get_crtc_state(plane_state->state,
  1106. crtc);
  1107. if (IS_ERR(crtc_state))
  1108. return PTR_ERR(crtc_state);
  1109. crtc_state->plane_mask |= (1 << drm_plane_index(plane));
  1110. }
  1111. if (crtc)
  1112. DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n",
  1113. plane_state, crtc->base.id, crtc->name);
  1114. else
  1115. DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
  1116. plane_state);
  1117. return 0;
  1118. }
  1119. EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
  1120. /**
  1121. * drm_atomic_set_fb_for_plane - set framebuffer for plane
  1122. * @plane_state: atomic state object for the plane
  1123. * @fb: fb to use for the plane
  1124. *
  1125. * Changing the assigned framebuffer for a plane requires us to grab a reference
  1126. * to the new fb and drop the reference to the old fb, if there is one. This
  1127. * function takes care of all these details besides updating the pointer in the
  1128. * state object itself.
  1129. */
  1130. void
  1131. drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
  1132. struct drm_framebuffer *fb)
  1133. {
  1134. if (fb)
  1135. DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
  1136. fb->base.id, plane_state);
  1137. else
  1138. DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
  1139. plane_state);
  1140. drm_framebuffer_assign(&plane_state->fb, fb);
  1141. }
  1142. EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
  1143. /**
  1144. * drm_atomic_set_fence_for_plane - set fence for plane
  1145. * @plane_state: atomic state object for the plane
  1146. * @fence: dma_fence to use for the plane
  1147. *
  1148. * Helper to setup the plane_state fence in case it is not set yet.
  1149. * By using this drivers doesn't need to worry if the user choose
  1150. * implicit or explicit fencing.
  1151. *
  1152. * This function will not set the fence to the state if it was set
  1153. * via explicit fencing interfaces on the atomic ioctl. In that case it will
  1154. * drop the reference to the fence as we are not storing it anywhere.
  1155. * Otherwise, if &drm_plane_state.fence is not set this function we just set it
  1156. * with the received implicit fence. In both cases this function consumes a
  1157. * reference for @fence.
  1158. */
  1159. void
  1160. drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
  1161. struct dma_fence *fence)
  1162. {
  1163. if (plane_state->fence) {
  1164. dma_fence_put(fence);
  1165. return;
  1166. }
  1167. plane_state->fence = fence;
  1168. }
  1169. EXPORT_SYMBOL(drm_atomic_set_fence_for_plane);
  1170. /**
  1171. * drm_atomic_set_crtc_for_connector - set crtc for connector
  1172. * @conn_state: atomic state object for the connector
  1173. * @crtc: crtc to use for the connector
  1174. *
  1175. * Changing the assigned crtc for a connector requires us to grab the lock and
  1176. * state for the new crtc, as needed. This function takes care of all these
  1177. * details besides updating the pointer in the state object itself.
  1178. *
  1179. * Returns:
  1180. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  1181. * then the w/w mutex code has detected a deadlock and the entire atomic
  1182. * sequence must be restarted. All other errors are fatal.
  1183. */
  1184. int
  1185. drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
  1186. struct drm_crtc *crtc)
  1187. {
  1188. struct drm_crtc_state *crtc_state;
  1189. if (conn_state->crtc == crtc)
  1190. return 0;
  1191. if (conn_state->crtc) {
  1192. crtc_state = drm_atomic_get_existing_crtc_state(conn_state->state,
  1193. conn_state->crtc);
  1194. crtc_state->connector_mask &=
  1195. ~(1 << drm_connector_index(conn_state->connector));
  1196. drm_connector_unreference(conn_state->connector);
  1197. conn_state->crtc = NULL;
  1198. }
  1199. if (crtc) {
  1200. crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
  1201. if (IS_ERR(crtc_state))
  1202. return PTR_ERR(crtc_state);
  1203. crtc_state->connector_mask |=
  1204. 1 << drm_connector_index(conn_state->connector);
  1205. drm_connector_reference(conn_state->connector);
  1206. conn_state->crtc = crtc;
  1207. DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n",
  1208. conn_state, crtc->base.id, crtc->name);
  1209. } else {
  1210. DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
  1211. conn_state);
  1212. }
  1213. return 0;
  1214. }
  1215. EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
  1216. /**
  1217. * drm_atomic_add_affected_connectors - add connectors for crtc
  1218. * @state: atomic state
  1219. * @crtc: DRM crtc
  1220. *
  1221. * This function walks the current configuration and adds all connectors
  1222. * currently using @crtc to the atomic configuration @state. Note that this
  1223. * function must acquire the connection mutex. This can potentially cause
  1224. * unneeded seralization if the update is just for the planes on one crtc. Hence
  1225. * drivers and helpers should only call this when really needed (e.g. when a
  1226. * full modeset needs to happen due to some change).
  1227. *
  1228. * Returns:
  1229. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  1230. * then the w/w mutex code has detected a deadlock and the entire atomic
  1231. * sequence must be restarted. All other errors are fatal.
  1232. */
  1233. int
  1234. drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
  1235. struct drm_crtc *crtc)
  1236. {
  1237. struct drm_mode_config *config = &state->dev->mode_config;
  1238. struct drm_connector *connector;
  1239. struct drm_connector_state *conn_state;
  1240. struct drm_connector_list_iter conn_iter;
  1241. int ret;
  1242. ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
  1243. if (ret)
  1244. return ret;
  1245. DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
  1246. crtc->base.id, crtc->name, state);
  1247. /*
  1248. * Changed connectors are already in @state, so only need to look at the
  1249. * current configuration.
  1250. */
  1251. drm_connector_list_iter_get(state->dev, &conn_iter);
  1252. drm_for_each_connector_iter(connector, &conn_iter) {
  1253. if (connector->state->crtc != crtc)
  1254. continue;
  1255. conn_state = drm_atomic_get_connector_state(state, connector);
  1256. if (IS_ERR(conn_state)) {
  1257. drm_connector_list_iter_put(&conn_iter);
  1258. return PTR_ERR(conn_state);
  1259. }
  1260. }
  1261. drm_connector_list_iter_put(&conn_iter);
  1262. return 0;
  1263. }
  1264. EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
  1265. /**
  1266. * drm_atomic_add_affected_planes - add planes for crtc
  1267. * @state: atomic state
  1268. * @crtc: DRM crtc
  1269. *
  1270. * This function walks the current configuration and adds all planes
  1271. * currently used by @crtc to the atomic configuration @state. This is useful
  1272. * when an atomic commit also needs to check all currently enabled plane on
  1273. * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
  1274. * to avoid special code to force-enable all planes.
  1275. *
  1276. * Since acquiring a plane state will always also acquire the w/w mutex of the
  1277. * current CRTC for that plane (if there is any) adding all the plane states for
  1278. * a CRTC will not reduce parallism of atomic updates.
  1279. *
  1280. * Returns:
  1281. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  1282. * then the w/w mutex code has detected a deadlock and the entire atomic
  1283. * sequence must be restarted. All other errors are fatal.
  1284. */
  1285. int
  1286. drm_atomic_add_affected_planes(struct drm_atomic_state *state,
  1287. struct drm_crtc *crtc)
  1288. {
  1289. struct drm_plane *plane;
  1290. WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
  1291. drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
  1292. struct drm_plane_state *plane_state =
  1293. drm_atomic_get_plane_state(state, plane);
  1294. if (IS_ERR(plane_state))
  1295. return PTR_ERR(plane_state);
  1296. }
  1297. return 0;
  1298. }
  1299. EXPORT_SYMBOL(drm_atomic_add_affected_planes);
  1300. /**
  1301. * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
  1302. * @state: atomic state
  1303. *
  1304. * This function should be used by legacy entry points which don't understand
  1305. * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
  1306. * the slowpath completed.
  1307. */
  1308. void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
  1309. {
  1310. struct drm_device *dev = state->dev;
  1311. unsigned crtc_mask = 0;
  1312. struct drm_crtc *crtc;
  1313. int ret;
  1314. bool global = false;
  1315. drm_for_each_crtc(crtc, dev) {
  1316. if (crtc->acquire_ctx != state->acquire_ctx)
  1317. continue;
  1318. crtc_mask |= drm_crtc_mask(crtc);
  1319. crtc->acquire_ctx = NULL;
  1320. }
  1321. if (WARN_ON(dev->mode_config.acquire_ctx == state->acquire_ctx)) {
  1322. global = true;
  1323. dev->mode_config.acquire_ctx = NULL;
  1324. }
  1325. retry:
  1326. drm_modeset_backoff(state->acquire_ctx);
  1327. ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
  1328. if (ret)
  1329. goto retry;
  1330. drm_for_each_crtc(crtc, dev)
  1331. if (drm_crtc_mask(crtc) & crtc_mask)
  1332. crtc->acquire_ctx = state->acquire_ctx;
  1333. if (global)
  1334. dev->mode_config.acquire_ctx = state->acquire_ctx;
  1335. }
  1336. EXPORT_SYMBOL(drm_atomic_legacy_backoff);
  1337. /**
  1338. * drm_atomic_check_only - check whether a given config would work
  1339. * @state: atomic configuration to check
  1340. *
  1341. * Note that this function can return -EDEADLK if the driver needed to acquire
  1342. * more locks but encountered a deadlock. The caller must then do the usual w/w
  1343. * backoff dance and restart. All other errors are fatal.
  1344. *
  1345. * Returns:
  1346. * 0 on success, negative error code on failure.
  1347. */
  1348. int drm_atomic_check_only(struct drm_atomic_state *state)
  1349. {
  1350. struct drm_device *dev = state->dev;
  1351. struct drm_mode_config *config = &dev->mode_config;
  1352. struct drm_plane *plane;
  1353. struct drm_plane_state *plane_state;
  1354. struct drm_crtc *crtc;
  1355. struct drm_crtc_state *crtc_state;
  1356. int i, ret = 0;
  1357. DRM_DEBUG_ATOMIC("checking %p\n", state);
  1358. for_each_plane_in_state(state, plane, plane_state, i) {
  1359. ret = drm_atomic_plane_check(plane, plane_state);
  1360. if (ret) {
  1361. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
  1362. plane->base.id, plane->name);
  1363. return ret;
  1364. }
  1365. }
  1366. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  1367. ret = drm_atomic_crtc_check(crtc, crtc_state);
  1368. if (ret) {
  1369. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
  1370. crtc->base.id, crtc->name);
  1371. return ret;
  1372. }
  1373. }
  1374. if (config->funcs->atomic_check)
  1375. ret = config->funcs->atomic_check(state->dev, state);
  1376. if (!state->allow_modeset) {
  1377. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  1378. if (drm_atomic_crtc_needs_modeset(crtc_state)) {
  1379. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
  1380. crtc->base.id, crtc->name);
  1381. return -EINVAL;
  1382. }
  1383. }
  1384. }
  1385. return ret;
  1386. }
  1387. EXPORT_SYMBOL(drm_atomic_check_only);
  1388. /**
  1389. * drm_atomic_commit - commit configuration atomically
  1390. * @state: atomic configuration to check
  1391. *
  1392. * Note that this function can return -EDEADLK if the driver needed to acquire
  1393. * more locks but encountered a deadlock. The caller must then do the usual w/w
  1394. * backoff dance and restart. All other errors are fatal.
  1395. *
  1396. * This function will take its own reference on @state.
  1397. * Callers should always release their reference with drm_atomic_state_put().
  1398. *
  1399. * Returns:
  1400. * 0 on success, negative error code on failure.
  1401. */
  1402. int drm_atomic_commit(struct drm_atomic_state *state)
  1403. {
  1404. struct drm_mode_config *config = &state->dev->mode_config;
  1405. int ret;
  1406. ret = drm_atomic_check_only(state);
  1407. if (ret)
  1408. return ret;
  1409. DRM_DEBUG_ATOMIC("commiting %p\n", state);
  1410. return config->funcs->atomic_commit(state->dev, state, false);
  1411. }
  1412. EXPORT_SYMBOL(drm_atomic_commit);
  1413. /**
  1414. * drm_atomic_nonblocking_commit - atomic nonblocking commit
  1415. * @state: atomic configuration to check
  1416. *
  1417. * Note that this function can return -EDEADLK if the driver needed to acquire
  1418. * more locks but encountered a deadlock. The caller must then do the usual w/w
  1419. * backoff dance and restart. All other errors are fatal.
  1420. *
  1421. * This function will take its own reference on @state.
  1422. * Callers should always release their reference with drm_atomic_state_put().
  1423. *
  1424. * Returns:
  1425. * 0 on success, negative error code on failure.
  1426. */
  1427. int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
  1428. {
  1429. struct drm_mode_config *config = &state->dev->mode_config;
  1430. int ret;
  1431. ret = drm_atomic_check_only(state);
  1432. if (ret)
  1433. return ret;
  1434. DRM_DEBUG_ATOMIC("commiting %p nonblocking\n", state);
  1435. return config->funcs->atomic_commit(state->dev, state, true);
  1436. }
  1437. EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
  1438. static void drm_atomic_print_state(const struct drm_atomic_state *state)
  1439. {
  1440. struct drm_printer p = drm_info_printer(state->dev->dev);
  1441. struct drm_plane *plane;
  1442. struct drm_plane_state *plane_state;
  1443. struct drm_crtc *crtc;
  1444. struct drm_crtc_state *crtc_state;
  1445. struct drm_connector *connector;
  1446. struct drm_connector_state *connector_state;
  1447. int i;
  1448. DRM_DEBUG_ATOMIC("checking %p\n", state);
  1449. for_each_plane_in_state(state, plane, plane_state, i)
  1450. drm_atomic_plane_print_state(&p, plane_state);
  1451. for_each_crtc_in_state(state, crtc, crtc_state, i)
  1452. drm_atomic_crtc_print_state(&p, crtc_state);
  1453. for_each_connector_in_state(state, connector, connector_state, i)
  1454. drm_atomic_connector_print_state(&p, connector_state);
  1455. }
  1456. /**
  1457. * drm_state_dump - dump entire device atomic state
  1458. * @dev: the drm device
  1459. * @p: where to print the state to
  1460. *
  1461. * Just for debugging. Drivers might want an option to dump state
  1462. * to dmesg in case of error irq's. (Hint, you probably want to
  1463. * ratelimit this!)
  1464. *
  1465. * The caller must drm_modeset_lock_all(), or if this is called
  1466. * from error irq handler, it should not be enabled by default.
  1467. * (Ie. if you are debugging errors you might not care that this
  1468. * is racey. But calling this without all modeset locks held is
  1469. * not inherently safe.)
  1470. */
  1471. void drm_state_dump(struct drm_device *dev, struct drm_printer *p)
  1472. {
  1473. struct drm_mode_config *config = &dev->mode_config;
  1474. struct drm_plane *plane;
  1475. struct drm_crtc *crtc;
  1476. struct drm_connector *connector;
  1477. struct drm_connector_list_iter conn_iter;
  1478. if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
  1479. return;
  1480. list_for_each_entry(plane, &config->plane_list, head)
  1481. drm_atomic_plane_print_state(p, plane->state);
  1482. list_for_each_entry(crtc, &config->crtc_list, head)
  1483. drm_atomic_crtc_print_state(p, crtc->state);
  1484. drm_connector_list_iter_get(dev, &conn_iter);
  1485. drm_for_each_connector_iter(connector, &conn_iter)
  1486. drm_atomic_connector_print_state(p, connector->state);
  1487. drm_connector_list_iter_put(&conn_iter);
  1488. }
  1489. EXPORT_SYMBOL(drm_state_dump);
  1490. #ifdef CONFIG_DEBUG_FS
  1491. static int drm_state_info(struct seq_file *m, void *data)
  1492. {
  1493. struct drm_info_node *node = (struct drm_info_node *) m->private;
  1494. struct drm_device *dev = node->minor->dev;
  1495. struct drm_printer p = drm_seq_file_printer(m);
  1496. drm_modeset_lock_all(dev);
  1497. drm_state_dump(dev, &p);
  1498. drm_modeset_unlock_all(dev);
  1499. return 0;
  1500. }
  1501. /* any use in debugfs files to dump individual planes/crtc/etc? */
  1502. static const struct drm_info_list drm_atomic_debugfs_list[] = {
  1503. {"state", drm_state_info, 0},
  1504. };
  1505. int drm_atomic_debugfs_init(struct drm_minor *minor)
  1506. {
  1507. return drm_debugfs_create_files(drm_atomic_debugfs_list,
  1508. ARRAY_SIZE(drm_atomic_debugfs_list),
  1509. minor->debugfs_root, minor);
  1510. }
  1511. #endif
  1512. /*
  1513. * The big monstor ioctl
  1514. */
  1515. static struct drm_pending_vblank_event *create_vblank_event(
  1516. struct drm_device *dev, uint64_t user_data)
  1517. {
  1518. struct drm_pending_vblank_event *e = NULL;
  1519. e = kzalloc(sizeof *e, GFP_KERNEL);
  1520. if (!e)
  1521. return NULL;
  1522. e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
  1523. e->event.base.length = sizeof(e->event);
  1524. e->event.user_data = user_data;
  1525. return e;
  1526. }
  1527. static int atomic_set_prop(struct drm_atomic_state *state,
  1528. struct drm_mode_object *obj, struct drm_property *prop,
  1529. uint64_t prop_value)
  1530. {
  1531. struct drm_mode_object *ref;
  1532. int ret;
  1533. if (!drm_property_change_valid_get(prop, prop_value, &ref))
  1534. return -EINVAL;
  1535. switch (obj->type) {
  1536. case DRM_MODE_OBJECT_CONNECTOR: {
  1537. struct drm_connector *connector = obj_to_connector(obj);
  1538. struct drm_connector_state *connector_state;
  1539. connector_state = drm_atomic_get_connector_state(state, connector);
  1540. if (IS_ERR(connector_state)) {
  1541. ret = PTR_ERR(connector_state);
  1542. break;
  1543. }
  1544. ret = drm_atomic_connector_set_property(connector,
  1545. connector_state, prop, prop_value);
  1546. break;
  1547. }
  1548. case DRM_MODE_OBJECT_CRTC: {
  1549. struct drm_crtc *crtc = obj_to_crtc(obj);
  1550. struct drm_crtc_state *crtc_state;
  1551. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  1552. if (IS_ERR(crtc_state)) {
  1553. ret = PTR_ERR(crtc_state);
  1554. break;
  1555. }
  1556. ret = drm_atomic_crtc_set_property(crtc,
  1557. crtc_state, prop, prop_value);
  1558. break;
  1559. }
  1560. case DRM_MODE_OBJECT_PLANE: {
  1561. struct drm_plane *plane = obj_to_plane(obj);
  1562. struct drm_plane_state *plane_state;
  1563. plane_state = drm_atomic_get_plane_state(state, plane);
  1564. if (IS_ERR(plane_state)) {
  1565. ret = PTR_ERR(plane_state);
  1566. break;
  1567. }
  1568. ret = drm_atomic_plane_set_property(plane,
  1569. plane_state, prop, prop_value);
  1570. break;
  1571. }
  1572. default:
  1573. ret = -EINVAL;
  1574. break;
  1575. }
  1576. drm_property_change_valid_put(prop, ref);
  1577. return ret;
  1578. }
  1579. /**
  1580. * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers.
  1581. *
  1582. * @dev: drm device to check.
  1583. * @plane_mask: plane mask for planes that were updated.
  1584. * @ret: return value, can be -EDEADLK for a retry.
  1585. *
  1586. * Before doing an update &drm_plane.old_fb is set to &drm_plane.fb, but before
  1587. * dropping the locks old_fb needs to be set to NULL and plane->fb updated. This
  1588. * is a common operation for each atomic update, so this call is split off as a
  1589. * helper.
  1590. */
  1591. void drm_atomic_clean_old_fb(struct drm_device *dev,
  1592. unsigned plane_mask,
  1593. int ret)
  1594. {
  1595. struct drm_plane *plane;
  1596. /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
  1597. * locks (ie. while it is still safe to deref plane->state). We
  1598. * need to do this here because the driver entry points cannot
  1599. * distinguish between legacy and atomic ioctls.
  1600. */
  1601. drm_for_each_plane_mask(plane, dev, plane_mask) {
  1602. if (ret == 0) {
  1603. struct drm_framebuffer *new_fb = plane->state->fb;
  1604. if (new_fb)
  1605. drm_framebuffer_reference(new_fb);
  1606. plane->fb = new_fb;
  1607. plane->crtc = plane->state->crtc;
  1608. if (plane->old_fb)
  1609. drm_framebuffer_unreference(plane->old_fb);
  1610. }
  1611. plane->old_fb = NULL;
  1612. }
  1613. }
  1614. EXPORT_SYMBOL(drm_atomic_clean_old_fb);
  1615. /**
  1616. * DOC: explicit fencing properties
  1617. *
  1618. * Explicit fencing allows userspace to control the buffer synchronization
  1619. * between devices. A Fence or a group of fences are transfered to/from
  1620. * userspace using Sync File fds and there are two DRM properties for that.
  1621. * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
  1622. * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
  1623. *
  1624. * As a contrast, with implicit fencing the kernel keeps track of any
  1625. * ongoing rendering, and automatically ensures that the atomic update waits
  1626. * for any pending rendering to complete. For shared buffers represented with
  1627. * a &struct dma_buf this is tracked in &struct reservation_object.
  1628. * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org),
  1629. * whereas explicit fencing is what Android wants.
  1630. *
  1631. * "IN_FENCE_FD”:
  1632. * Use this property to pass a fence that DRM should wait on before
  1633. * proceeding with the Atomic Commit request and show the framebuffer for
  1634. * the plane on the screen. The fence can be either a normal fence or a
  1635. * merged one, the sync_file framework will handle both cases and use a
  1636. * fence_array if a merged fence is received. Passing -1 here means no
  1637. * fences to wait on.
  1638. *
  1639. * If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
  1640. * it will only check if the Sync File is a valid one.
  1641. *
  1642. * On the driver side the fence is stored on the @fence parameter of
  1643. * &struct drm_plane_state. Drivers which also support implicit fencing
  1644. * should set the implicit fence using drm_atomic_set_fence_for_plane(),
  1645. * to make sure there's consistent behaviour between drivers in precedence
  1646. * of implicit vs. explicit fencing.
  1647. *
  1648. * "OUT_FENCE_PTR”:
  1649. * Use this property to pass a file descriptor pointer to DRM. Once the
  1650. * Atomic Commit request call returns OUT_FENCE_PTR will be filled with
  1651. * the file descriptor number of a Sync File. This Sync File contains the
  1652. * CRTC fence that will be signaled when all framebuffers present on the
  1653. * Atomic Commit * request for that given CRTC are scanned out on the
  1654. * screen.
  1655. *
  1656. * The Atomic Commit request fails if a invalid pointer is passed. If the
  1657. * Atomic Commit request fails for any other reason the out fence fd
  1658. * returned will be -1. On a Atomic Commit with the
  1659. * DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
  1660. *
  1661. * Note that out-fences don't have a special interface to drivers and are
  1662. * internally represented by a &struct drm_pending_vblank_event in struct
  1663. * &drm_crtc_state, which is also used by the nonblocking atomic commit
  1664. * helpers and for the DRM event handling for existing userspace.
  1665. */
  1666. struct drm_out_fence_state {
  1667. s64 __user *out_fence_ptr;
  1668. struct sync_file *sync_file;
  1669. int fd;
  1670. };
  1671. static int setup_out_fence(struct drm_out_fence_state *fence_state,
  1672. struct dma_fence *fence)
  1673. {
  1674. fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
  1675. if (fence_state->fd < 0)
  1676. return fence_state->fd;
  1677. if (put_user(fence_state->fd, fence_state->out_fence_ptr))
  1678. return -EFAULT;
  1679. fence_state->sync_file = sync_file_create(fence);
  1680. if (!fence_state->sync_file)
  1681. return -ENOMEM;
  1682. return 0;
  1683. }
  1684. static int prepare_crtc_signaling(struct drm_device *dev,
  1685. struct drm_atomic_state *state,
  1686. struct drm_mode_atomic *arg,
  1687. struct drm_file *file_priv,
  1688. struct drm_out_fence_state **fence_state,
  1689. unsigned int *num_fences)
  1690. {
  1691. struct drm_crtc *crtc;
  1692. struct drm_crtc_state *crtc_state;
  1693. int i, ret;
  1694. if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
  1695. return 0;
  1696. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  1697. u64 __user *fence_ptr;
  1698. fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
  1699. if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
  1700. struct drm_pending_vblank_event *e;
  1701. e = create_vblank_event(dev, arg->user_data);
  1702. if (!e)
  1703. return -ENOMEM;
  1704. crtc_state->event = e;
  1705. }
  1706. if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  1707. struct drm_pending_vblank_event *e = crtc_state->event;
  1708. if (!file_priv)
  1709. continue;
  1710. ret = drm_event_reserve_init(dev, file_priv, &e->base,
  1711. &e->event.base);
  1712. if (ret) {
  1713. kfree(e);
  1714. crtc_state->event = NULL;
  1715. return ret;
  1716. }
  1717. }
  1718. if (fence_ptr) {
  1719. struct dma_fence *fence;
  1720. struct drm_out_fence_state *f;
  1721. f = krealloc(*fence_state, sizeof(**fence_state) *
  1722. (*num_fences + 1), GFP_KERNEL);
  1723. if (!f)
  1724. return -ENOMEM;
  1725. memset(&f[*num_fences], 0, sizeof(*f));
  1726. f[*num_fences].out_fence_ptr = fence_ptr;
  1727. *fence_state = f;
  1728. fence = drm_crtc_create_fence(crtc);
  1729. if (!fence)
  1730. return -ENOMEM;
  1731. ret = setup_out_fence(&f[(*num_fences)++], fence);
  1732. if (ret) {
  1733. dma_fence_put(fence);
  1734. return ret;
  1735. }
  1736. crtc_state->event->base.fence = fence;
  1737. }
  1738. }
  1739. return 0;
  1740. }
  1741. static void complete_crtc_signaling(struct drm_device *dev,
  1742. struct drm_atomic_state *state,
  1743. struct drm_out_fence_state *fence_state,
  1744. unsigned int num_fences,
  1745. bool install_fds)
  1746. {
  1747. struct drm_crtc *crtc;
  1748. struct drm_crtc_state *crtc_state;
  1749. int i;
  1750. if (install_fds) {
  1751. for (i = 0; i < num_fences; i++)
  1752. fd_install(fence_state[i].fd,
  1753. fence_state[i].sync_file->file);
  1754. kfree(fence_state);
  1755. return;
  1756. }
  1757. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  1758. /*
  1759. * TEST_ONLY and PAGE_FLIP_EVENT are mutually
  1760. * exclusive, if they weren't, this code should be
  1761. * called on success for TEST_ONLY too.
  1762. */
  1763. if (crtc_state->event)
  1764. drm_event_cancel_free(dev, &crtc_state->event->base);
  1765. }
  1766. if (!fence_state)
  1767. return;
  1768. for (i = 0; i < num_fences; i++) {
  1769. if (fence_state[i].sync_file)
  1770. fput(fence_state[i].sync_file->file);
  1771. if (fence_state[i].fd >= 0)
  1772. put_unused_fd(fence_state[i].fd);
  1773. /* If this fails log error to the user */
  1774. if (fence_state[i].out_fence_ptr &&
  1775. put_user(-1, fence_state[i].out_fence_ptr))
  1776. DRM_DEBUG_ATOMIC("Couldn't clear out_fence_ptr\n");
  1777. }
  1778. kfree(fence_state);
  1779. }
  1780. int drm_atomic_remove_fb(struct drm_framebuffer *fb)
  1781. {
  1782. struct drm_modeset_acquire_ctx ctx;
  1783. struct drm_device *dev = fb->dev;
  1784. struct drm_atomic_state *state;
  1785. struct drm_plane *plane;
  1786. struct drm_connector *conn;
  1787. struct drm_connector_state *conn_state;
  1788. int i, ret = 0;
  1789. unsigned plane_mask, disable_crtcs = false;
  1790. state = drm_atomic_state_alloc(dev);
  1791. if (!state)
  1792. return -ENOMEM;
  1793. drm_modeset_acquire_init(&ctx, 0);
  1794. state->acquire_ctx = &ctx;
  1795. retry:
  1796. plane_mask = 0;
  1797. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  1798. if (ret)
  1799. goto unlock;
  1800. drm_for_each_plane(plane, dev) {
  1801. struct drm_plane_state *plane_state;
  1802. if (plane->state->fb != fb)
  1803. continue;
  1804. plane_state = drm_atomic_get_plane_state(state, plane);
  1805. if (IS_ERR(plane_state)) {
  1806. ret = PTR_ERR(plane_state);
  1807. goto unlock;
  1808. }
  1809. /*
  1810. * Some drivers do not support keeping crtc active with the
  1811. * primary plane disabled. If we fail to commit with -EINVAL
  1812. * then we will try to perform the same commit but with all
  1813. * crtc's disabled for primary planes as well.
  1814. */
  1815. if (disable_crtcs && plane_state->crtc->primary == plane) {
  1816. struct drm_crtc_state *crtc_state;
  1817. crtc_state = drm_atomic_get_existing_crtc_state(state, plane_state->crtc);
  1818. ret = drm_atomic_add_affected_connectors(state, plane_state->crtc);
  1819. if (ret)
  1820. goto unlock;
  1821. crtc_state->active = false;
  1822. ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
  1823. if (ret)
  1824. goto unlock;
  1825. }
  1826. drm_atomic_set_fb_for_plane(plane_state, NULL);
  1827. ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
  1828. if (ret)
  1829. goto unlock;
  1830. plane_mask |= BIT(drm_plane_index(plane));
  1831. plane->old_fb = plane->fb;
  1832. }
  1833. /* This list is only not empty when disable_crtcs is set. */
  1834. for_each_connector_in_state(state, conn, conn_state, i) {
  1835. ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
  1836. if (ret)
  1837. goto unlock;
  1838. }
  1839. if (plane_mask)
  1840. ret = drm_atomic_commit(state);
  1841. unlock:
  1842. if (plane_mask)
  1843. drm_atomic_clean_old_fb(dev, plane_mask, ret);
  1844. if (ret == -EDEADLK) {
  1845. drm_modeset_backoff(&ctx);
  1846. goto retry;
  1847. }
  1848. drm_atomic_state_put(state);
  1849. if (ret == -EINVAL && !disable_crtcs) {
  1850. disable_crtcs = true;
  1851. state = drm_atomic_state_alloc(dev);
  1852. if (state) {
  1853. state->acquire_ctx = &ctx;
  1854. goto retry;
  1855. }
  1856. ret = -ENOMEM;
  1857. }
  1858. drm_modeset_drop_locks(&ctx);
  1859. drm_modeset_acquire_fini(&ctx);
  1860. return ret;
  1861. }
  1862. int drm_mode_atomic_ioctl(struct drm_device *dev,
  1863. void *data, struct drm_file *file_priv)
  1864. {
  1865. struct drm_mode_atomic *arg = data;
  1866. uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
  1867. uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
  1868. uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
  1869. uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
  1870. unsigned int copied_objs, copied_props;
  1871. struct drm_atomic_state *state;
  1872. struct drm_modeset_acquire_ctx ctx;
  1873. struct drm_plane *plane;
  1874. struct drm_out_fence_state *fence_state = NULL;
  1875. unsigned plane_mask;
  1876. int ret = 0;
  1877. unsigned int i, j, num_fences = 0;
  1878. /* disallow for drivers not supporting atomic: */
  1879. if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
  1880. return -EINVAL;
  1881. /* disallow for userspace that has not enabled atomic cap (even
  1882. * though this may be a bit overkill, since legacy userspace
  1883. * wouldn't know how to call this ioctl)
  1884. */
  1885. if (!file_priv->atomic)
  1886. return -EINVAL;
  1887. if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
  1888. return -EINVAL;
  1889. if (arg->reserved)
  1890. return -EINVAL;
  1891. if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
  1892. !dev->mode_config.async_page_flip)
  1893. return -EINVAL;
  1894. /* can't test and expect an event at the same time. */
  1895. if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
  1896. (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
  1897. return -EINVAL;
  1898. drm_modeset_acquire_init(&ctx, 0);
  1899. state = drm_atomic_state_alloc(dev);
  1900. if (!state)
  1901. return -ENOMEM;
  1902. state->acquire_ctx = &ctx;
  1903. state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
  1904. retry:
  1905. plane_mask = 0;
  1906. copied_objs = 0;
  1907. copied_props = 0;
  1908. for (i = 0; i < arg->count_objs; i++) {
  1909. uint32_t obj_id, count_props;
  1910. struct drm_mode_object *obj;
  1911. if (get_user(obj_id, objs_ptr + copied_objs)) {
  1912. ret = -EFAULT;
  1913. goto out;
  1914. }
  1915. obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
  1916. if (!obj) {
  1917. ret = -ENOENT;
  1918. goto out;
  1919. }
  1920. if (!obj->properties) {
  1921. drm_mode_object_unreference(obj);
  1922. ret = -ENOENT;
  1923. goto out;
  1924. }
  1925. if (get_user(count_props, count_props_ptr + copied_objs)) {
  1926. drm_mode_object_unreference(obj);
  1927. ret = -EFAULT;
  1928. goto out;
  1929. }
  1930. copied_objs++;
  1931. for (j = 0; j < count_props; j++) {
  1932. uint32_t prop_id;
  1933. uint64_t prop_value;
  1934. struct drm_property *prop;
  1935. if (get_user(prop_id, props_ptr + copied_props)) {
  1936. drm_mode_object_unreference(obj);
  1937. ret = -EFAULT;
  1938. goto out;
  1939. }
  1940. prop = drm_mode_obj_find_prop_id(obj, prop_id);
  1941. if (!prop) {
  1942. drm_mode_object_unreference(obj);
  1943. ret = -ENOENT;
  1944. goto out;
  1945. }
  1946. if (copy_from_user(&prop_value,
  1947. prop_values_ptr + copied_props,
  1948. sizeof(prop_value))) {
  1949. drm_mode_object_unreference(obj);
  1950. ret = -EFAULT;
  1951. goto out;
  1952. }
  1953. ret = atomic_set_prop(state, obj, prop, prop_value);
  1954. if (ret) {
  1955. drm_mode_object_unreference(obj);
  1956. goto out;
  1957. }
  1958. copied_props++;
  1959. }
  1960. if (obj->type == DRM_MODE_OBJECT_PLANE && count_props &&
  1961. !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
  1962. plane = obj_to_plane(obj);
  1963. plane_mask |= (1 << drm_plane_index(plane));
  1964. plane->old_fb = plane->fb;
  1965. }
  1966. drm_mode_object_unreference(obj);
  1967. }
  1968. ret = prepare_crtc_signaling(dev, state, arg, file_priv, &fence_state,
  1969. &num_fences);
  1970. if (ret)
  1971. goto out;
  1972. if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
  1973. ret = drm_atomic_check_only(state);
  1974. } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
  1975. ret = drm_atomic_nonblocking_commit(state);
  1976. } else {
  1977. if (unlikely(drm_debug & DRM_UT_STATE))
  1978. drm_atomic_print_state(state);
  1979. ret = drm_atomic_commit(state);
  1980. }
  1981. out:
  1982. drm_atomic_clean_old_fb(dev, plane_mask, ret);
  1983. complete_crtc_signaling(dev, state, fence_state, num_fences, !ret);
  1984. if (ret == -EDEADLK) {
  1985. drm_atomic_state_clear(state);
  1986. drm_modeset_backoff(&ctx);
  1987. goto retry;
  1988. }
  1989. drm_atomic_state_put(state);
  1990. drm_modeset_drop_locks(&ctx);
  1991. drm_modeset_acquire_fini(&ctx);
  1992. return ret;
  1993. }