drm_atomic.c 61 KB

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