drm_atomic_uapi.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  1. /*
  2. * Copyright (C) 2014 Red Hat
  3. * Copyright (C) 2014 Intel Corp.
  4. * Copyright (C) 2018 Intel Corp.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors:
  25. * Rob Clark <robdclark@gmail.com>
  26. * Daniel Vetter <daniel.vetter@ffwll.ch>
  27. */
  28. #include <drm/drm_atomic_uapi.h>
  29. #include <drm/drm_atomic.h>
  30. #include <drm/drm_print.h>
  31. #include <drm/drm_drv.h>
  32. #include <drm/drm_writeback.h>
  33. #include <drm/drm_vblank.h>
  34. #include <linux/dma-fence.h>
  35. #include <linux/uaccess.h>
  36. #include <linux/sync_file.h>
  37. #include <linux/file.h>
  38. #include "drm_crtc_internal.h"
  39. /**
  40. * DOC: overview
  41. *
  42. * This file contains the marshalling and demarshalling glue for the atomic UAPI
  43. * in all its forms: The monster ATOMIC IOCTL itself, code for GET_PROPERTY and
  44. * SET_PROPERTY IOCTLs. Plus interface functions for compatibility helpers and
  45. * drivers which have special needs to construct their own atomic updates, e.g.
  46. * for load detect or similiar.
  47. */
  48. /**
  49. * drm_atomic_set_mode_for_crtc - set mode for CRTC
  50. * @state: the CRTC whose incoming state to update
  51. * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
  52. *
  53. * Set a mode (originating from the kernel) on the desired CRTC state and update
  54. * the enable property.
  55. *
  56. * RETURNS:
  57. * Zero on success, error code on failure. Cannot return -EDEADLK.
  58. */
  59. int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
  60. const struct drm_display_mode *mode)
  61. {
  62. struct drm_crtc *crtc = state->crtc;
  63. struct drm_mode_modeinfo umode;
  64. /* Early return for no change. */
  65. if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
  66. return 0;
  67. drm_property_blob_put(state->mode_blob);
  68. state->mode_blob = NULL;
  69. if (mode) {
  70. drm_mode_convert_to_umode(&umode, mode);
  71. state->mode_blob =
  72. drm_property_create_blob(state->crtc->dev,
  73. sizeof(umode),
  74. &umode);
  75. if (IS_ERR(state->mode_blob))
  76. return PTR_ERR(state->mode_blob);
  77. drm_mode_copy(&state->mode, mode);
  78. state->enable = true;
  79. DRM_DEBUG_ATOMIC("Set [MODE:%s] for [CRTC:%d:%s] state %p\n",
  80. mode->name, crtc->base.id, crtc->name, state);
  81. } else {
  82. memset(&state->mode, 0, sizeof(state->mode));
  83. state->enable = false;
  84. DRM_DEBUG_ATOMIC("Set [NOMODE] for [CRTC:%d:%s] state %p\n",
  85. crtc->base.id, crtc->name, state);
  86. }
  87. return 0;
  88. }
  89. EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
  90. /**
  91. * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
  92. * @state: the CRTC whose incoming state to update
  93. * @blob: pointer to blob property to use for mode
  94. *
  95. * Set a mode (originating from a blob property) on the desired CRTC state.
  96. * This function will take a reference on the blob property for the CRTC state,
  97. * and release the reference held on the state's existing mode property, if any
  98. * was set.
  99. *
  100. * RETURNS:
  101. * Zero on success, error code on failure. Cannot return -EDEADLK.
  102. */
  103. int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
  104. struct drm_property_blob *blob)
  105. {
  106. struct drm_crtc *crtc = state->crtc;
  107. if (blob == state->mode_blob)
  108. return 0;
  109. drm_property_blob_put(state->mode_blob);
  110. state->mode_blob = NULL;
  111. memset(&state->mode, 0, sizeof(state->mode));
  112. if (blob) {
  113. int ret;
  114. if (blob->length != sizeof(struct drm_mode_modeinfo)) {
  115. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] bad mode blob length: %zu\n",
  116. crtc->base.id, crtc->name,
  117. blob->length);
  118. return -EINVAL;
  119. }
  120. ret = drm_mode_convert_umode(crtc->dev,
  121. &state->mode, blob->data);
  122. if (ret) {
  123. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] invalid mode (ret=%d, status=%s):\n",
  124. crtc->base.id, crtc->name,
  125. ret, drm_get_mode_status_name(state->mode.status));
  126. drm_mode_debug_printmodeline(&state->mode);
  127. return -EINVAL;
  128. }
  129. state->mode_blob = drm_property_blob_get(blob);
  130. state->enable = true;
  131. DRM_DEBUG_ATOMIC("Set [MODE:%s] for [CRTC:%d:%s] state %p\n",
  132. state->mode.name, crtc->base.id, crtc->name,
  133. state);
  134. } else {
  135. state->enable = false;
  136. DRM_DEBUG_ATOMIC("Set [NOMODE] for [CRTC:%d:%s] state %p\n",
  137. crtc->base.id, crtc->name, state);
  138. }
  139. return 0;
  140. }
  141. EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
  142. /**
  143. * drm_atomic_set_crtc_for_plane - set CRTC for plane
  144. * @plane_state: the plane whose incoming state to update
  145. * @crtc: CRTC to use for the plane
  146. *
  147. * Changing the assigned CRTC for a plane requires us to grab the lock and state
  148. * for the new CRTC, as needed. This function takes care of all these details
  149. * besides updating the pointer in the state object itself.
  150. *
  151. * Returns:
  152. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  153. * then the w/w mutex code has detected a deadlock and the entire atomic
  154. * sequence must be restarted. All other errors are fatal.
  155. */
  156. int
  157. drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
  158. struct drm_crtc *crtc)
  159. {
  160. struct drm_plane *plane = plane_state->plane;
  161. struct drm_crtc_state *crtc_state;
  162. /* Nothing to do for same crtc*/
  163. if (plane_state->crtc == crtc)
  164. return 0;
  165. if (plane_state->crtc) {
  166. crtc_state = drm_atomic_get_crtc_state(plane_state->state,
  167. plane_state->crtc);
  168. if (WARN_ON(IS_ERR(crtc_state)))
  169. return PTR_ERR(crtc_state);
  170. crtc_state->plane_mask &= ~drm_plane_mask(plane);
  171. }
  172. plane_state->crtc = crtc;
  173. if (crtc) {
  174. crtc_state = drm_atomic_get_crtc_state(plane_state->state,
  175. crtc);
  176. if (IS_ERR(crtc_state))
  177. return PTR_ERR(crtc_state);
  178. crtc_state->plane_mask |= drm_plane_mask(plane);
  179. }
  180. if (crtc)
  181. DRM_DEBUG_ATOMIC("Link [PLANE:%d:%s] state %p to [CRTC:%d:%s]\n",
  182. plane->base.id, plane->name, plane_state,
  183. crtc->base.id, crtc->name);
  184. else
  185. DRM_DEBUG_ATOMIC("Link [PLANE:%d:%s] state %p to [NOCRTC]\n",
  186. plane->base.id, plane->name, plane_state);
  187. return 0;
  188. }
  189. EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
  190. /**
  191. * drm_atomic_set_fb_for_plane - set framebuffer for plane
  192. * @plane_state: atomic state object for the plane
  193. * @fb: fb to use for the plane
  194. *
  195. * Changing the assigned framebuffer for a plane requires us to grab a reference
  196. * to the new fb and drop the reference to the old fb, if there is one. This
  197. * function takes care of all these details besides updating the pointer in the
  198. * state object itself.
  199. */
  200. void
  201. drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
  202. struct drm_framebuffer *fb)
  203. {
  204. struct drm_plane *plane = plane_state->plane;
  205. if (fb)
  206. DRM_DEBUG_ATOMIC("Set [FB:%d] for [PLANE:%d:%s] state %p\n",
  207. fb->base.id, plane->base.id, plane->name,
  208. plane_state);
  209. else
  210. DRM_DEBUG_ATOMIC("Set [NOFB] for [PLANE:%d:%s] state %p\n",
  211. plane->base.id, plane->name, plane_state);
  212. drm_framebuffer_assign(&plane_state->fb, fb);
  213. }
  214. EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
  215. /**
  216. * drm_atomic_set_fence_for_plane - set fence for plane
  217. * @plane_state: atomic state object for the plane
  218. * @fence: dma_fence to use for the plane
  219. *
  220. * Helper to setup the plane_state fence in case it is not set yet.
  221. * By using this drivers doesn't need to worry if the user choose
  222. * implicit or explicit fencing.
  223. *
  224. * This function will not set the fence to the state if it was set
  225. * via explicit fencing interfaces on the atomic ioctl. In that case it will
  226. * drop the reference to the fence as we are not storing it anywhere.
  227. * Otherwise, if &drm_plane_state.fence is not set this function we just set it
  228. * with the received implicit fence. In both cases this function consumes a
  229. * reference for @fence.
  230. *
  231. * This way explicit fencing can be used to overrule implicit fencing, which is
  232. * important to make explicit fencing use-cases work: One example is using one
  233. * buffer for 2 screens with different refresh rates. Implicit fencing will
  234. * clamp rendering to the refresh rate of the slower screen, whereas explicit
  235. * fence allows 2 independent render and display loops on a single buffer. If a
  236. * driver allows obeys both implicit and explicit fences for plane updates, then
  237. * it will break all the benefits of explicit fencing.
  238. */
  239. void
  240. drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
  241. struct dma_fence *fence)
  242. {
  243. if (plane_state->fence) {
  244. dma_fence_put(fence);
  245. return;
  246. }
  247. plane_state->fence = fence;
  248. }
  249. EXPORT_SYMBOL(drm_atomic_set_fence_for_plane);
  250. /**
  251. * drm_atomic_set_crtc_for_connector - set CRTC for connector
  252. * @conn_state: atomic state object for the connector
  253. * @crtc: CRTC to use for the connector
  254. *
  255. * Changing the assigned CRTC for a connector requires us to grab the lock and
  256. * state for the new CRTC, as needed. This function takes care of all these
  257. * details besides updating the pointer in the state object itself.
  258. *
  259. * Returns:
  260. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  261. * then the w/w mutex code has detected a deadlock and the entire atomic
  262. * sequence must be restarted. All other errors are fatal.
  263. */
  264. int
  265. drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
  266. struct drm_crtc *crtc)
  267. {
  268. struct drm_connector *connector = conn_state->connector;
  269. struct drm_crtc_state *crtc_state;
  270. if (conn_state->crtc == crtc)
  271. return 0;
  272. if (conn_state->crtc) {
  273. crtc_state = drm_atomic_get_new_crtc_state(conn_state->state,
  274. conn_state->crtc);
  275. crtc_state->connector_mask &=
  276. ~drm_connector_mask(conn_state->connector);
  277. drm_connector_put(conn_state->connector);
  278. conn_state->crtc = NULL;
  279. }
  280. if (crtc) {
  281. crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
  282. if (IS_ERR(crtc_state))
  283. return PTR_ERR(crtc_state);
  284. crtc_state->connector_mask |=
  285. drm_connector_mask(conn_state->connector);
  286. drm_connector_get(conn_state->connector);
  287. conn_state->crtc = crtc;
  288. DRM_DEBUG_ATOMIC("Link [CONNECTOR:%d:%s] state %p to [CRTC:%d:%s]\n",
  289. connector->base.id, connector->name,
  290. conn_state, crtc->base.id, crtc->name);
  291. } else {
  292. DRM_DEBUG_ATOMIC("Link [CONNECTOR:%d:%s] state %p to [NOCRTC]\n",
  293. connector->base.id, connector->name,
  294. conn_state);
  295. }
  296. return 0;
  297. }
  298. EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
  299. static void set_out_fence_for_crtc(struct drm_atomic_state *state,
  300. struct drm_crtc *crtc, s32 __user *fence_ptr)
  301. {
  302. state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
  303. }
  304. static s32 __user *get_out_fence_for_crtc(struct drm_atomic_state *state,
  305. struct drm_crtc *crtc)
  306. {
  307. s32 __user *fence_ptr;
  308. fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
  309. state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;
  310. return fence_ptr;
  311. }
  312. static int set_out_fence_for_connector(struct drm_atomic_state *state,
  313. struct drm_connector *connector,
  314. s32 __user *fence_ptr)
  315. {
  316. unsigned int index = drm_connector_index(connector);
  317. if (!fence_ptr)
  318. return 0;
  319. if (put_user(-1, fence_ptr))
  320. return -EFAULT;
  321. state->connectors[index].out_fence_ptr = fence_ptr;
  322. return 0;
  323. }
  324. static s32 __user *get_out_fence_for_connector(struct drm_atomic_state *state,
  325. struct drm_connector *connector)
  326. {
  327. unsigned int index = drm_connector_index(connector);
  328. s32 __user *fence_ptr;
  329. fence_ptr = state->connectors[index].out_fence_ptr;
  330. state->connectors[index].out_fence_ptr = NULL;
  331. return fence_ptr;
  332. }
  333. static int
  334. drm_atomic_replace_property_blob_from_id(struct drm_device *dev,
  335. struct drm_property_blob **blob,
  336. uint64_t blob_id,
  337. ssize_t expected_size,
  338. ssize_t expected_elem_size,
  339. bool *replaced)
  340. {
  341. struct drm_property_blob *new_blob = NULL;
  342. if (blob_id != 0) {
  343. new_blob = drm_property_lookup_blob(dev, blob_id);
  344. if (new_blob == NULL)
  345. return -EINVAL;
  346. if (expected_size > 0 &&
  347. new_blob->length != expected_size) {
  348. drm_property_blob_put(new_blob);
  349. return -EINVAL;
  350. }
  351. if (expected_elem_size > 0 &&
  352. new_blob->length % expected_elem_size != 0) {
  353. drm_property_blob_put(new_blob);
  354. return -EINVAL;
  355. }
  356. }
  357. *replaced |= drm_property_replace_blob(blob, new_blob);
  358. drm_property_blob_put(new_blob);
  359. return 0;
  360. }
  361. static int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
  362. struct drm_crtc_state *state, struct drm_property *property,
  363. uint64_t val)
  364. {
  365. struct drm_device *dev = crtc->dev;
  366. struct drm_mode_config *config = &dev->mode_config;
  367. bool replaced = false;
  368. int ret;
  369. if (property == config->prop_active)
  370. state->active = val;
  371. else if (property == config->prop_mode_id) {
  372. struct drm_property_blob *mode =
  373. drm_property_lookup_blob(dev, val);
  374. ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
  375. drm_property_blob_put(mode);
  376. return ret;
  377. } else if (property == config->prop_vrr_enabled) {
  378. state->vrr_enabled = val;
  379. } else if (property == config->degamma_lut_property) {
  380. ret = drm_atomic_replace_property_blob_from_id(dev,
  381. &state->degamma_lut,
  382. val,
  383. -1, sizeof(struct drm_color_lut),
  384. &replaced);
  385. state->color_mgmt_changed |= replaced;
  386. return ret;
  387. } else if (property == config->ctm_property) {
  388. ret = drm_atomic_replace_property_blob_from_id(dev,
  389. &state->ctm,
  390. val,
  391. sizeof(struct drm_color_ctm), -1,
  392. &replaced);
  393. state->color_mgmt_changed |= replaced;
  394. return ret;
  395. } else if (property == config->gamma_lut_property) {
  396. ret = drm_atomic_replace_property_blob_from_id(dev,
  397. &state->gamma_lut,
  398. val,
  399. -1, sizeof(struct drm_color_lut),
  400. &replaced);
  401. state->color_mgmt_changed |= replaced;
  402. return ret;
  403. } else if (property == config->prop_out_fence_ptr) {
  404. s32 __user *fence_ptr = u64_to_user_ptr(val);
  405. if (!fence_ptr)
  406. return 0;
  407. if (put_user(-1, fence_ptr))
  408. return -EFAULT;
  409. set_out_fence_for_crtc(state->state, crtc, fence_ptr);
  410. } else if (crtc->funcs->atomic_set_property) {
  411. return crtc->funcs->atomic_set_property(crtc, state, property, val);
  412. } else {
  413. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] unknown property [PROP:%d:%s]]\n",
  414. crtc->base.id, crtc->name,
  415. property->base.id, property->name);
  416. return -EINVAL;
  417. }
  418. return 0;
  419. }
  420. static int
  421. drm_atomic_crtc_get_property(struct drm_crtc *crtc,
  422. const struct drm_crtc_state *state,
  423. struct drm_property *property, uint64_t *val)
  424. {
  425. struct drm_device *dev = crtc->dev;
  426. struct drm_mode_config *config = &dev->mode_config;
  427. if (property == config->prop_active)
  428. *val = drm_atomic_crtc_effectively_active(state);
  429. else if (property == config->prop_mode_id)
  430. *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
  431. else if (property == config->prop_vrr_enabled)
  432. *val = state->vrr_enabled;
  433. else if (property == config->degamma_lut_property)
  434. *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
  435. else if (property == config->ctm_property)
  436. *val = (state->ctm) ? state->ctm->base.id : 0;
  437. else if (property == config->gamma_lut_property)
  438. *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
  439. else if (property == config->prop_out_fence_ptr)
  440. *val = 0;
  441. else if (crtc->funcs->atomic_get_property)
  442. return crtc->funcs->atomic_get_property(crtc, state, property, val);
  443. else
  444. return -EINVAL;
  445. return 0;
  446. }
  447. static int drm_atomic_plane_set_property(struct drm_plane *plane,
  448. struct drm_plane_state *state, struct drm_file *file_priv,
  449. struct drm_property *property, uint64_t val)
  450. {
  451. struct drm_device *dev = plane->dev;
  452. struct drm_mode_config *config = &dev->mode_config;
  453. bool replaced = false;
  454. int ret;
  455. if (property == config->prop_fb_id) {
  456. struct drm_framebuffer *fb;
  457. fb = drm_framebuffer_lookup(dev, file_priv, val);
  458. drm_atomic_set_fb_for_plane(state, fb);
  459. if (fb)
  460. drm_framebuffer_put(fb);
  461. } else if (property == config->prop_in_fence_fd) {
  462. if (state->fence)
  463. return -EINVAL;
  464. if (U642I64(val) == -1)
  465. return 0;
  466. state->fence = sync_file_get_fence(val);
  467. if (!state->fence)
  468. return -EINVAL;
  469. } else if (property == config->prop_crtc_id) {
  470. struct drm_crtc *crtc = drm_crtc_find(dev, file_priv, val);
  471. if (val && !crtc)
  472. return -EACCES;
  473. return drm_atomic_set_crtc_for_plane(state, crtc);
  474. } else if (property == config->prop_crtc_x) {
  475. state->crtc_x = U642I64(val);
  476. } else if (property == config->prop_crtc_y) {
  477. state->crtc_y = U642I64(val);
  478. } else if (property == config->prop_crtc_w) {
  479. state->crtc_w = val;
  480. } else if (property == config->prop_crtc_h) {
  481. state->crtc_h = val;
  482. } else if (property == config->prop_src_x) {
  483. state->src_x = val;
  484. } else if (property == config->prop_src_y) {
  485. state->src_y = val;
  486. } else if (property == config->prop_src_w) {
  487. state->src_w = val;
  488. } else if (property == config->prop_src_h) {
  489. state->src_h = val;
  490. } else if (property == plane->alpha_property) {
  491. state->alpha = val;
  492. } else if (property == plane->blend_mode_property) {
  493. state->pixel_blend_mode = val;
  494. } else if (property == plane->rotation_property) {
  495. if (!is_power_of_2(val & DRM_MODE_ROTATE_MASK)) {
  496. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] bad rotation bitmask: 0x%llx\n",
  497. plane->base.id, plane->name, val);
  498. return -EINVAL;
  499. }
  500. state->rotation = val;
  501. } else if (property == plane->zpos_property) {
  502. state->zpos = val;
  503. } else if (property == plane->color_encoding_property) {
  504. state->color_encoding = val;
  505. } else if (property == plane->color_range_property) {
  506. state->color_range = val;
  507. } else if (property == config->prop_fb_damage_clips) {
  508. ret = drm_atomic_replace_property_blob_from_id(dev,
  509. &state->fb_damage_clips,
  510. val,
  511. -1,
  512. sizeof(struct drm_rect),
  513. &replaced);
  514. return ret;
  515. } else if (plane->funcs->atomic_set_property) {
  516. return plane->funcs->atomic_set_property(plane, state,
  517. property, val);
  518. } else {
  519. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] unknown property [PROP:%d:%s]]\n",
  520. plane->base.id, plane->name,
  521. property->base.id, property->name);
  522. return -EINVAL;
  523. }
  524. return 0;
  525. }
  526. static int
  527. drm_atomic_plane_get_property(struct drm_plane *plane,
  528. const struct drm_plane_state *state,
  529. struct drm_property *property, uint64_t *val)
  530. {
  531. struct drm_device *dev = plane->dev;
  532. struct drm_mode_config *config = &dev->mode_config;
  533. if (property == config->prop_fb_id) {
  534. *val = (state->fb) ? state->fb->base.id : 0;
  535. } else if (property == config->prop_in_fence_fd) {
  536. *val = -1;
  537. } else if (property == config->prop_crtc_id) {
  538. *val = (state->crtc) ? state->crtc->base.id : 0;
  539. } else if (property == config->prop_crtc_x) {
  540. *val = I642U64(state->crtc_x);
  541. } else if (property == config->prop_crtc_y) {
  542. *val = I642U64(state->crtc_y);
  543. } else if (property == config->prop_crtc_w) {
  544. *val = state->crtc_w;
  545. } else if (property == config->prop_crtc_h) {
  546. *val = state->crtc_h;
  547. } else if (property == config->prop_src_x) {
  548. *val = state->src_x;
  549. } else if (property == config->prop_src_y) {
  550. *val = state->src_y;
  551. } else if (property == config->prop_src_w) {
  552. *val = state->src_w;
  553. } else if (property == config->prop_src_h) {
  554. *val = state->src_h;
  555. } else if (property == plane->alpha_property) {
  556. *val = state->alpha;
  557. } else if (property == plane->blend_mode_property) {
  558. *val = state->pixel_blend_mode;
  559. } else if (property == plane->rotation_property) {
  560. *val = state->rotation;
  561. } else if (property == plane->zpos_property) {
  562. *val = state->zpos;
  563. } else if (property == plane->color_encoding_property) {
  564. *val = state->color_encoding;
  565. } else if (property == plane->color_range_property) {
  566. *val = state->color_range;
  567. } else if (property == config->prop_fb_damage_clips) {
  568. *val = (state->fb_damage_clips) ?
  569. state->fb_damage_clips->base.id : 0;
  570. } else if (plane->funcs->atomic_get_property) {
  571. return plane->funcs->atomic_get_property(plane, state, property, val);
  572. } else {
  573. return -EINVAL;
  574. }
  575. return 0;
  576. }
  577. static int drm_atomic_set_writeback_fb_for_connector(
  578. struct drm_connector_state *conn_state,
  579. struct drm_framebuffer *fb)
  580. {
  581. int ret;
  582. ret = drm_writeback_set_fb(conn_state, fb);
  583. if (ret < 0)
  584. return ret;
  585. if (fb)
  586. DRM_DEBUG_ATOMIC("Set [FB:%d] for connector state %p\n",
  587. fb->base.id, conn_state);
  588. else
  589. DRM_DEBUG_ATOMIC("Set [NOFB] for connector state %p\n",
  590. conn_state);
  591. return 0;
  592. }
  593. static int drm_atomic_connector_set_property(struct drm_connector *connector,
  594. struct drm_connector_state *state, struct drm_file *file_priv,
  595. struct drm_property *property, uint64_t val)
  596. {
  597. struct drm_device *dev = connector->dev;
  598. struct drm_mode_config *config = &dev->mode_config;
  599. bool replaced = false;
  600. int ret;
  601. if (property == config->prop_crtc_id) {
  602. struct drm_crtc *crtc = drm_crtc_find(dev, file_priv, val);
  603. if (val && !crtc)
  604. return -EACCES;
  605. return drm_atomic_set_crtc_for_connector(state, crtc);
  606. } else if (property == config->dpms_property) {
  607. /* setting DPMS property requires special handling, which
  608. * is done in legacy setprop path for us. Disallow (for
  609. * now?) atomic writes to DPMS property:
  610. */
  611. return -EINVAL;
  612. } else if (property == config->tv_select_subconnector_property) {
  613. state->tv.subconnector = val;
  614. } else if (property == config->tv_left_margin_property) {
  615. state->tv.margins.left = val;
  616. } else if (property == config->tv_right_margin_property) {
  617. state->tv.margins.right = val;
  618. } else if (property == config->tv_top_margin_property) {
  619. state->tv.margins.top = val;
  620. } else if (property == config->tv_bottom_margin_property) {
  621. state->tv.margins.bottom = val;
  622. } else if (property == config->tv_mode_property) {
  623. state->tv.mode = val;
  624. } else if (property == config->tv_brightness_property) {
  625. state->tv.brightness = val;
  626. } else if (property == config->tv_contrast_property) {
  627. state->tv.contrast = val;
  628. } else if (property == config->tv_flicker_reduction_property) {
  629. state->tv.flicker_reduction = val;
  630. } else if (property == config->tv_overscan_property) {
  631. state->tv.overscan = val;
  632. } else if (property == config->tv_saturation_property) {
  633. state->tv.saturation = val;
  634. } else if (property == config->tv_hue_property) {
  635. state->tv.hue = val;
  636. } else if (property == config->link_status_property) {
  637. /* Never downgrade from GOOD to BAD on userspace's request here,
  638. * only hw issues can do that.
  639. *
  640. * For an atomic property the userspace doesn't need to be able
  641. * to understand all the properties, but needs to be able to
  642. * restore the state it wants on VT switch. So if the userspace
  643. * tries to change the link_status from GOOD to BAD, driver
  644. * silently rejects it and returns a 0. This prevents userspace
  645. * from accidently breaking the display when it restores the
  646. * state.
  647. */
  648. if (state->link_status != DRM_LINK_STATUS_GOOD)
  649. state->link_status = val;
  650. } else if (property == config->hdr_output_metadata_property) {
  651. ret = drm_atomic_replace_property_blob_from_id(dev,
  652. &state->hdr_output_metadata,
  653. val,
  654. sizeof(struct hdr_output_metadata), -1,
  655. &replaced);
  656. return ret;
  657. } else if (property == config->aspect_ratio_property) {
  658. state->picture_aspect_ratio = val;
  659. } else if (property == config->content_type_property) {
  660. state->content_type = val;
  661. } else if (property == connector->scaling_mode_property) {
  662. state->scaling_mode = val;
  663. } else if (property == config->content_protection_property) {
  664. if (val == DRM_MODE_CONTENT_PROTECTION_ENABLED) {
  665. DRM_DEBUG_KMS("only drivers can set CP Enabled\n");
  666. return -EINVAL;
  667. }
  668. state->content_protection = val;
  669. } else if (property == config->hdcp_content_type_property) {
  670. state->hdcp_content_type = val;
  671. } else if (property == connector->colorspace_property) {
  672. state->colorspace = val;
  673. } else if (property == config->writeback_fb_id_property) {
  674. struct drm_framebuffer *fb;
  675. int ret;
  676. fb = drm_framebuffer_lookup(dev, file_priv, val);
  677. ret = drm_atomic_set_writeback_fb_for_connector(state, fb);
  678. if (fb)
  679. drm_framebuffer_put(fb);
  680. return ret;
  681. } else if (property == config->writeback_out_fence_ptr_property) {
  682. s32 __user *fence_ptr = u64_to_user_ptr(val);
  683. return set_out_fence_for_connector(state->state, connector,
  684. fence_ptr);
  685. } else if (property == connector->max_bpc_property) {
  686. state->max_requested_bpc = val;
  687. } else if (connector->funcs->atomic_set_property) {
  688. return connector->funcs->atomic_set_property(connector,
  689. state, property, val);
  690. } else {
  691. DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] unknown property [PROP:%d:%s]]\n",
  692. connector->base.id, connector->name,
  693. property->base.id, property->name);
  694. return -EINVAL;
  695. }
  696. return 0;
  697. }
  698. static int
  699. drm_atomic_connector_get_property(struct drm_connector *connector,
  700. const struct drm_connector_state *state,
  701. struct drm_property *property, uint64_t *val)
  702. {
  703. struct drm_device *dev = connector->dev;
  704. struct drm_mode_config *config = &dev->mode_config;
  705. if (property == config->prop_crtc_id) {
  706. *val = (state->crtc) ? state->crtc->base.id : 0;
  707. } else if (property == config->dpms_property) {
  708. if (state->crtc && state->crtc->state->self_refresh_active)
  709. *val = DRM_MODE_DPMS_ON;
  710. else
  711. *val = connector->dpms;
  712. } else if (property == config->tv_select_subconnector_property) {
  713. *val = state->tv.subconnector;
  714. } else if (property == config->tv_left_margin_property) {
  715. *val = state->tv.margins.left;
  716. } else if (property == config->tv_right_margin_property) {
  717. *val = state->tv.margins.right;
  718. } else if (property == config->tv_top_margin_property) {
  719. *val = state->tv.margins.top;
  720. } else if (property == config->tv_bottom_margin_property) {
  721. *val = state->tv.margins.bottom;
  722. } else if (property == config->tv_mode_property) {
  723. *val = state->tv.mode;
  724. } else if (property == config->tv_brightness_property) {
  725. *val = state->tv.brightness;
  726. } else if (property == config->tv_contrast_property) {
  727. *val = state->tv.contrast;
  728. } else if (property == config->tv_flicker_reduction_property) {
  729. *val = state->tv.flicker_reduction;
  730. } else if (property == config->tv_overscan_property) {
  731. *val = state->tv.overscan;
  732. } else if (property == config->tv_saturation_property) {
  733. *val = state->tv.saturation;
  734. } else if (property == config->tv_hue_property) {
  735. *val = state->tv.hue;
  736. } else if (property == config->link_status_property) {
  737. *val = state->link_status;
  738. } else if (property == config->aspect_ratio_property) {
  739. *val = state->picture_aspect_ratio;
  740. } else if (property == config->content_type_property) {
  741. *val = state->content_type;
  742. } else if (property == connector->colorspace_property) {
  743. *val = state->colorspace;
  744. } else if (property == connector->scaling_mode_property) {
  745. *val = state->scaling_mode;
  746. } else if (property == config->hdr_output_metadata_property) {
  747. *val = state->hdr_output_metadata ?
  748. state->hdr_output_metadata->base.id : 0;
  749. } else if (property == config->content_protection_property) {
  750. *val = state->content_protection;
  751. } else if (property == config->hdcp_content_type_property) {
  752. *val = state->hdcp_content_type;
  753. } else if (property == config->writeback_fb_id_property) {
  754. /* Writeback framebuffer is one-shot, write and forget */
  755. *val = 0;
  756. } else if (property == config->writeback_out_fence_ptr_property) {
  757. *val = 0;
  758. } else if (property == connector->max_bpc_property) {
  759. *val = state->max_requested_bpc;
  760. } else if (connector->funcs->atomic_get_property) {
  761. return connector->funcs->atomic_get_property(connector,
  762. state, property, val);
  763. } else {
  764. return -EINVAL;
  765. }
  766. return 0;
  767. }
  768. int drm_atomic_get_property(struct drm_mode_object *obj,
  769. struct drm_property *property, uint64_t *val)
  770. {
  771. struct drm_device *dev = property->dev;
  772. int ret;
  773. switch (obj->type) {
  774. case DRM_MODE_OBJECT_CONNECTOR: {
  775. struct drm_connector *connector = obj_to_connector(obj);
  776. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  777. ret = drm_atomic_connector_get_property(connector,
  778. connector->state, property, val);
  779. break;
  780. }
  781. case DRM_MODE_OBJECT_CRTC: {
  782. struct drm_crtc *crtc = obj_to_crtc(obj);
  783. WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
  784. ret = drm_atomic_crtc_get_property(crtc,
  785. crtc->state, property, val);
  786. break;
  787. }
  788. case DRM_MODE_OBJECT_PLANE: {
  789. struct drm_plane *plane = obj_to_plane(obj);
  790. WARN_ON(!drm_modeset_is_locked(&plane->mutex));
  791. ret = drm_atomic_plane_get_property(plane,
  792. plane->state, property, val);
  793. break;
  794. }
  795. default:
  796. ret = -EINVAL;
  797. break;
  798. }
  799. return ret;
  800. }
  801. /*
  802. * The big monster ioctl
  803. */
  804. static struct drm_pending_vblank_event *create_vblank_event(
  805. struct drm_crtc *crtc, uint64_t user_data)
  806. {
  807. struct drm_pending_vblank_event *e = NULL;
  808. e = kzalloc(sizeof *e, GFP_KERNEL);
  809. if (!e)
  810. return NULL;
  811. e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
  812. e->event.base.length = sizeof(e->event);
  813. e->event.vbl.crtc_id = crtc->base.id;
  814. e->event.vbl.user_data = user_data;
  815. return e;
  816. }
  817. int drm_atomic_connector_commit_dpms(struct drm_atomic_state *state,
  818. struct drm_connector *connector,
  819. int mode)
  820. {
  821. struct drm_connector *tmp_connector;
  822. struct drm_connector_state *new_conn_state;
  823. struct drm_crtc *crtc;
  824. struct drm_crtc_state *crtc_state;
  825. int i, ret, old_mode = connector->dpms;
  826. bool active = false;
  827. ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
  828. state->acquire_ctx);
  829. if (ret)
  830. return ret;
  831. if (mode != DRM_MODE_DPMS_ON)
  832. mode = DRM_MODE_DPMS_OFF;
  833. connector->dpms = mode;
  834. crtc = connector->state->crtc;
  835. if (!crtc)
  836. goto out;
  837. ret = drm_atomic_add_affected_connectors(state, crtc);
  838. if (ret)
  839. goto out;
  840. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  841. if (IS_ERR(crtc_state)) {
  842. ret = PTR_ERR(crtc_state);
  843. goto out;
  844. }
  845. for_each_new_connector_in_state(state, tmp_connector, new_conn_state, i) {
  846. if (new_conn_state->crtc != crtc)
  847. continue;
  848. if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
  849. active = true;
  850. break;
  851. }
  852. }
  853. crtc_state->active = active;
  854. ret = drm_atomic_commit(state);
  855. out:
  856. if (ret != 0)
  857. connector->dpms = old_mode;
  858. return ret;
  859. }
  860. int drm_atomic_set_property(struct drm_atomic_state *state,
  861. struct drm_file *file_priv,
  862. struct drm_mode_object *obj,
  863. struct drm_property *prop,
  864. uint64_t prop_value)
  865. {
  866. struct drm_mode_object *ref;
  867. int ret;
  868. if (!drm_property_change_valid_get(prop, prop_value, &ref))
  869. return -EINVAL;
  870. switch (obj->type) {
  871. case DRM_MODE_OBJECT_CONNECTOR: {
  872. struct drm_connector *connector = obj_to_connector(obj);
  873. struct drm_connector_state *connector_state;
  874. connector_state = drm_atomic_get_connector_state(state, connector);
  875. if (IS_ERR(connector_state)) {
  876. ret = PTR_ERR(connector_state);
  877. break;
  878. }
  879. ret = drm_atomic_connector_set_property(connector,
  880. connector_state, file_priv,
  881. prop, prop_value);
  882. break;
  883. }
  884. case DRM_MODE_OBJECT_CRTC: {
  885. struct drm_crtc *crtc = obj_to_crtc(obj);
  886. struct drm_crtc_state *crtc_state;
  887. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  888. if (IS_ERR(crtc_state)) {
  889. ret = PTR_ERR(crtc_state);
  890. break;
  891. }
  892. ret = drm_atomic_crtc_set_property(crtc,
  893. crtc_state, prop, prop_value);
  894. break;
  895. }
  896. case DRM_MODE_OBJECT_PLANE: {
  897. struct drm_plane *plane = obj_to_plane(obj);
  898. struct drm_plane_state *plane_state;
  899. plane_state = drm_atomic_get_plane_state(state, plane);
  900. if (IS_ERR(plane_state)) {
  901. ret = PTR_ERR(plane_state);
  902. break;
  903. }
  904. ret = drm_atomic_plane_set_property(plane,
  905. plane_state, file_priv,
  906. prop, prop_value);
  907. break;
  908. }
  909. default:
  910. ret = -EINVAL;
  911. break;
  912. }
  913. drm_property_change_valid_put(prop, ref);
  914. return ret;
  915. }
  916. /**
  917. * DOC: explicit fencing properties
  918. *
  919. * Explicit fencing allows userspace to control the buffer synchronization
  920. * between devices. A Fence or a group of fences are transfered to/from
  921. * userspace using Sync File fds and there are two DRM properties for that.
  922. * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
  923. * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
  924. *
  925. * As a contrast, with implicit fencing the kernel keeps track of any
  926. * ongoing rendering, and automatically ensures that the atomic update waits
  927. * for any pending rendering to complete. For shared buffers represented with
  928. * a &struct dma_buf this is tracked in &struct dma_resv.
  929. * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org),
  930. * whereas explicit fencing is what Android wants.
  931. *
  932. * "IN_FENCE_FD”:
  933. * Use this property to pass a fence that DRM should wait on before
  934. * proceeding with the Atomic Commit request and show the framebuffer for
  935. * the plane on the screen. The fence can be either a normal fence or a
  936. * merged one, the sync_file framework will handle both cases and use a
  937. * fence_array if a merged fence is received. Passing -1 here means no
  938. * fences to wait on.
  939. *
  940. * If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
  941. * it will only check if the Sync File is a valid one.
  942. *
  943. * On the driver side the fence is stored on the @fence parameter of
  944. * &struct drm_plane_state. Drivers which also support implicit fencing
  945. * should set the implicit fence using drm_atomic_set_fence_for_plane(),
  946. * to make sure there's consistent behaviour between drivers in precedence
  947. * of implicit vs. explicit fencing.
  948. *
  949. * "OUT_FENCE_PTR”:
  950. * Use this property to pass a file descriptor pointer to DRM. Once the
  951. * Atomic Commit request call returns OUT_FENCE_PTR will be filled with
  952. * the file descriptor number of a Sync File. This Sync File contains the
  953. * CRTC fence that will be signaled when all framebuffers present on the
  954. * Atomic Commit * request for that given CRTC are scanned out on the
  955. * screen.
  956. *
  957. * The Atomic Commit request fails if a invalid pointer is passed. If the
  958. * Atomic Commit request fails for any other reason the out fence fd
  959. * returned will be -1. On a Atomic Commit with the
  960. * DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
  961. *
  962. * Note that out-fences don't have a special interface to drivers and are
  963. * internally represented by a &struct drm_pending_vblank_event in struct
  964. * &drm_crtc_state, which is also used by the nonblocking atomic commit
  965. * helpers and for the DRM event handling for existing userspace.
  966. */
  967. struct drm_out_fence_state {
  968. s32 __user *out_fence_ptr;
  969. struct sync_file *sync_file;
  970. int fd;
  971. };
  972. static int setup_out_fence(struct drm_out_fence_state *fence_state,
  973. struct dma_fence *fence)
  974. {
  975. fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
  976. if (fence_state->fd < 0)
  977. return fence_state->fd;
  978. if (put_user(fence_state->fd, fence_state->out_fence_ptr))
  979. return -EFAULT;
  980. fence_state->sync_file = sync_file_create(fence);
  981. if (!fence_state->sync_file)
  982. return -ENOMEM;
  983. return 0;
  984. }
  985. static int prepare_signaling(struct drm_device *dev,
  986. struct drm_atomic_state *state,
  987. struct drm_mode_atomic *arg,
  988. struct drm_file *file_priv,
  989. struct drm_out_fence_state **fence_state,
  990. unsigned int *num_fences)
  991. {
  992. struct drm_crtc *crtc;
  993. struct drm_crtc_state *crtc_state;
  994. struct drm_connector *conn;
  995. struct drm_connector_state *conn_state;
  996. int i, c = 0, ret;
  997. if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
  998. return 0;
  999. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  1000. s32 __user *fence_ptr;
  1001. fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
  1002. if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
  1003. struct drm_pending_vblank_event *e;
  1004. e = create_vblank_event(crtc, arg->user_data);
  1005. if (!e)
  1006. return -ENOMEM;
  1007. crtc_state->event = e;
  1008. }
  1009. if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  1010. struct drm_pending_vblank_event *e = crtc_state->event;
  1011. if (!file_priv)
  1012. continue;
  1013. ret = drm_event_reserve_init(dev, file_priv, &e->base,
  1014. &e->event.base);
  1015. if (ret) {
  1016. kfree(e);
  1017. crtc_state->event = NULL;
  1018. return ret;
  1019. }
  1020. }
  1021. if (fence_ptr) {
  1022. struct dma_fence *fence;
  1023. struct drm_out_fence_state *f;
  1024. f = krealloc(*fence_state, sizeof(**fence_state) *
  1025. (*num_fences + 1), GFP_KERNEL);
  1026. if (!f)
  1027. return -ENOMEM;
  1028. memset(&f[*num_fences], 0, sizeof(*f));
  1029. f[*num_fences].out_fence_ptr = fence_ptr;
  1030. *fence_state = f;
  1031. fence = drm_crtc_create_fence(crtc);
  1032. if (!fence)
  1033. return -ENOMEM;
  1034. ret = setup_out_fence(&f[(*num_fences)++], fence);
  1035. if (ret) {
  1036. dma_fence_put(fence);
  1037. return ret;
  1038. }
  1039. crtc_state->event->base.fence = fence;
  1040. }
  1041. c++;
  1042. }
  1043. for_each_new_connector_in_state(state, conn, conn_state, i) {
  1044. struct drm_writeback_connector *wb_conn;
  1045. struct drm_out_fence_state *f;
  1046. struct dma_fence *fence;
  1047. s32 __user *fence_ptr;
  1048. if (!conn_state->writeback_job)
  1049. continue;
  1050. fence_ptr = get_out_fence_for_connector(state, conn);
  1051. if (!fence_ptr)
  1052. continue;
  1053. f = krealloc(*fence_state, sizeof(**fence_state) *
  1054. (*num_fences + 1), GFP_KERNEL);
  1055. if (!f)
  1056. return -ENOMEM;
  1057. memset(&f[*num_fences], 0, sizeof(*f));
  1058. f[*num_fences].out_fence_ptr = fence_ptr;
  1059. *fence_state = f;
  1060. wb_conn = drm_connector_to_writeback(conn);
  1061. fence = drm_writeback_get_out_fence(wb_conn);
  1062. if (!fence)
  1063. return -ENOMEM;
  1064. ret = setup_out_fence(&f[(*num_fences)++], fence);
  1065. if (ret) {
  1066. dma_fence_put(fence);
  1067. return ret;
  1068. }
  1069. conn_state->writeback_job->out_fence = fence;
  1070. }
  1071. /*
  1072. * Having this flag means user mode pends on event which will never
  1073. * reach due to lack of at least one CRTC for signaling
  1074. */
  1075. if (c == 0 && (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
  1076. return -EINVAL;
  1077. return 0;
  1078. }
  1079. static void complete_signaling(struct drm_device *dev,
  1080. struct drm_atomic_state *state,
  1081. struct drm_out_fence_state *fence_state,
  1082. unsigned int num_fences,
  1083. bool install_fds)
  1084. {
  1085. struct drm_crtc *crtc;
  1086. struct drm_crtc_state *crtc_state;
  1087. int i;
  1088. if (install_fds) {
  1089. for (i = 0; i < num_fences; i++)
  1090. fd_install(fence_state[i].fd,
  1091. fence_state[i].sync_file->file);
  1092. kfree(fence_state);
  1093. return;
  1094. }
  1095. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  1096. struct drm_pending_vblank_event *event = crtc_state->event;
  1097. /*
  1098. * Free the allocated event. drm_atomic_helper_setup_commit
  1099. * can allocate an event too, so only free it if it's ours
  1100. * to prevent a double free in drm_atomic_state_clear.
  1101. */
  1102. if (event && (event->base.fence || event->base.file_priv)) {
  1103. drm_event_cancel_free(dev, &event->base);
  1104. crtc_state->event = NULL;
  1105. }
  1106. }
  1107. if (!fence_state)
  1108. return;
  1109. for (i = 0; i < num_fences; i++) {
  1110. if (fence_state[i].sync_file)
  1111. fput(fence_state[i].sync_file->file);
  1112. if (fence_state[i].fd >= 0)
  1113. put_unused_fd(fence_state[i].fd);
  1114. /* If this fails log error to the user */
  1115. if (fence_state[i].out_fence_ptr &&
  1116. put_user(-1, fence_state[i].out_fence_ptr))
  1117. DRM_DEBUG_ATOMIC("Couldn't clear out_fence_ptr\n");
  1118. }
  1119. kfree(fence_state);
  1120. }
  1121. int drm_mode_atomic_ioctl(struct drm_device *dev,
  1122. void *data, struct drm_file *file_priv)
  1123. {
  1124. struct drm_mode_atomic *arg = data;
  1125. uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
  1126. uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
  1127. uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
  1128. uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
  1129. unsigned int copied_objs, copied_props;
  1130. struct drm_atomic_state *state;
  1131. struct drm_modeset_acquire_ctx ctx;
  1132. struct drm_out_fence_state *fence_state;
  1133. int ret = 0;
  1134. unsigned int i, j, num_fences;
  1135. /* disallow for drivers not supporting atomic: */
  1136. if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
  1137. return -EOPNOTSUPP;
  1138. /* disallow for userspace that has not enabled atomic cap (even
  1139. * though this may be a bit overkill, since legacy userspace
  1140. * wouldn't know how to call this ioctl)
  1141. */
  1142. if (!file_priv->atomic)
  1143. return -EINVAL;
  1144. if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
  1145. return -EINVAL;
  1146. if (arg->reserved)
  1147. return -EINVAL;
  1148. if (arg->flags & DRM_MODE_PAGE_FLIP_ASYNC)
  1149. return -EINVAL;
  1150. /* can't test and expect an event at the same time. */
  1151. if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
  1152. (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
  1153. return -EINVAL;
  1154. state = drm_atomic_state_alloc(dev);
  1155. if (!state)
  1156. return -ENOMEM;
  1157. drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
  1158. state->acquire_ctx = &ctx;
  1159. state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
  1160. retry:
  1161. copied_objs = 0;
  1162. copied_props = 0;
  1163. fence_state = NULL;
  1164. num_fences = 0;
  1165. for (i = 0; i < arg->count_objs; i++) {
  1166. uint32_t obj_id, count_props;
  1167. struct drm_mode_object *obj;
  1168. if (get_user(obj_id, objs_ptr + copied_objs)) {
  1169. ret = -EFAULT;
  1170. goto out;
  1171. }
  1172. obj = drm_mode_object_find(dev, file_priv, obj_id, DRM_MODE_OBJECT_ANY);
  1173. if (!obj) {
  1174. ret = -ENOENT;
  1175. goto out;
  1176. }
  1177. if (!obj->properties) {
  1178. drm_mode_object_put(obj);
  1179. ret = -ENOENT;
  1180. goto out;
  1181. }
  1182. if (get_user(count_props, count_props_ptr + copied_objs)) {
  1183. drm_mode_object_put(obj);
  1184. ret = -EFAULT;
  1185. goto out;
  1186. }
  1187. copied_objs++;
  1188. for (j = 0; j < count_props; j++) {
  1189. uint32_t prop_id;
  1190. uint64_t prop_value;
  1191. struct drm_property *prop;
  1192. if (get_user(prop_id, props_ptr + copied_props)) {
  1193. drm_mode_object_put(obj);
  1194. ret = -EFAULT;
  1195. goto out;
  1196. }
  1197. prop = drm_mode_obj_find_prop_id(obj, prop_id);
  1198. if (!prop) {
  1199. drm_mode_object_put(obj);
  1200. ret = -ENOENT;
  1201. goto out;
  1202. }
  1203. if (copy_from_user(&prop_value,
  1204. prop_values_ptr + copied_props,
  1205. sizeof(prop_value))) {
  1206. drm_mode_object_put(obj);
  1207. ret = -EFAULT;
  1208. goto out;
  1209. }
  1210. ret = drm_atomic_set_property(state, file_priv,
  1211. obj, prop, prop_value);
  1212. if (ret) {
  1213. drm_mode_object_put(obj);
  1214. goto out;
  1215. }
  1216. copied_props++;
  1217. }
  1218. drm_mode_object_put(obj);
  1219. }
  1220. ret = prepare_signaling(dev, state, arg, file_priv, &fence_state,
  1221. &num_fences);
  1222. if (ret)
  1223. goto out;
  1224. if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
  1225. ret = drm_atomic_check_only(state);
  1226. } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
  1227. ret = drm_atomic_nonblocking_commit(state);
  1228. } else {
  1229. if (drm_debug_enabled(DRM_UT_STATE))
  1230. drm_atomic_print_state(state);
  1231. ret = drm_atomic_commit(state);
  1232. }
  1233. out:
  1234. complete_signaling(dev, state, fence_state, num_fences, !ret);
  1235. if (ret == -EDEADLK) {
  1236. drm_atomic_state_clear(state);
  1237. ret = drm_modeset_backoff(&ctx);
  1238. if (!ret)
  1239. goto retry;
  1240. }
  1241. drm_atomic_state_put(state);
  1242. drm_modeset_drop_locks(&ctx);
  1243. drm_modeset_acquire_fini(&ctx);
  1244. return ret;
  1245. }