drm_atomic.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  1. /*
  2. * Copyright (C) 2014 Red Hat
  3. * Copyright (C) 2014 Intel Corp.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Rob Clark <robdclark@gmail.com>
  25. * Daniel Vetter <daniel.vetter@ffwll.ch>
  26. */
  27. #include <drm/drmP.h>
  28. #include <drm/drm_atomic.h>
  29. #include <drm/drm_plane_helper.h>
  30. static void kfree_state(struct drm_atomic_state *state)
  31. {
  32. kfree(state->connectors);
  33. kfree(state->connector_states);
  34. kfree(state->crtcs);
  35. kfree(state->crtc_states);
  36. kfree(state->planes);
  37. kfree(state->plane_states);
  38. kfree(state);
  39. }
  40. /**
  41. * drm_atomic_state_alloc - allocate atomic state
  42. * @dev: DRM device
  43. *
  44. * This allocates an empty atomic state to track updates.
  45. */
  46. struct drm_atomic_state *
  47. drm_atomic_state_alloc(struct drm_device *dev)
  48. {
  49. struct drm_atomic_state *state;
  50. state = kzalloc(sizeof(*state), GFP_KERNEL);
  51. if (!state)
  52. return NULL;
  53. /* TODO legacy paths should maybe do a better job about
  54. * setting this appropriately?
  55. */
  56. state->allow_modeset = true;
  57. state->num_connector = ACCESS_ONCE(dev->mode_config.num_connector);
  58. state->crtcs = kcalloc(dev->mode_config.num_crtc,
  59. sizeof(*state->crtcs), GFP_KERNEL);
  60. if (!state->crtcs)
  61. goto fail;
  62. state->crtc_states = kcalloc(dev->mode_config.num_crtc,
  63. sizeof(*state->crtc_states), GFP_KERNEL);
  64. if (!state->crtc_states)
  65. goto fail;
  66. state->planes = kcalloc(dev->mode_config.num_total_plane,
  67. sizeof(*state->planes), GFP_KERNEL);
  68. if (!state->planes)
  69. goto fail;
  70. state->plane_states = kcalloc(dev->mode_config.num_total_plane,
  71. sizeof(*state->plane_states), GFP_KERNEL);
  72. if (!state->plane_states)
  73. goto fail;
  74. state->connectors = kcalloc(state->num_connector,
  75. sizeof(*state->connectors),
  76. GFP_KERNEL);
  77. if (!state->connectors)
  78. goto fail;
  79. state->connector_states = kcalloc(state->num_connector,
  80. sizeof(*state->connector_states),
  81. GFP_KERNEL);
  82. if (!state->connector_states)
  83. goto fail;
  84. state->dev = dev;
  85. DRM_DEBUG_ATOMIC("Allocate atomic state %p\n", state);
  86. return state;
  87. fail:
  88. kfree_state(state);
  89. return NULL;
  90. }
  91. EXPORT_SYMBOL(drm_atomic_state_alloc);
  92. /**
  93. * drm_atomic_state_clear - clear state object
  94. * @state: atomic state
  95. *
  96. * When the w/w mutex algorithm detects a deadlock we need to back off and drop
  97. * all locks. So someone else could sneak in and change the current modeset
  98. * configuration. Which means that all the state assembled in @state is no
  99. * longer an atomic update to the current state, but to some arbitrary earlier
  100. * state. Which could break assumptions the driver's ->atomic_check likely
  101. * relies on.
  102. *
  103. * Hence we must clear all cached state and completely start over, using this
  104. * function.
  105. */
  106. void drm_atomic_state_clear(struct drm_atomic_state *state)
  107. {
  108. struct drm_device *dev = state->dev;
  109. struct drm_mode_config *config = &dev->mode_config;
  110. int i;
  111. DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
  112. for (i = 0; i < state->num_connector; i++) {
  113. struct drm_connector *connector = state->connectors[i];
  114. if (!connector)
  115. continue;
  116. WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  117. connector->funcs->atomic_destroy_state(connector,
  118. state->connector_states[i]);
  119. state->connectors[i] = NULL;
  120. state->connector_states[i] = NULL;
  121. }
  122. for (i = 0; i < config->num_crtc; i++) {
  123. struct drm_crtc *crtc = state->crtcs[i];
  124. if (!crtc)
  125. continue;
  126. crtc->funcs->atomic_destroy_state(crtc,
  127. state->crtc_states[i]);
  128. state->crtcs[i] = NULL;
  129. state->crtc_states[i] = NULL;
  130. }
  131. for (i = 0; i < config->num_total_plane; i++) {
  132. struct drm_plane *plane = state->planes[i];
  133. if (!plane)
  134. continue;
  135. plane->funcs->atomic_destroy_state(plane,
  136. state->plane_states[i]);
  137. state->planes[i] = NULL;
  138. state->plane_states[i] = NULL;
  139. }
  140. }
  141. EXPORT_SYMBOL(drm_atomic_state_clear);
  142. /**
  143. * drm_atomic_state_free - free all memory for an atomic state
  144. * @state: atomic state to deallocate
  145. *
  146. * This frees all memory associated with an atomic state, including all the
  147. * per-object state for planes, crtcs and connectors.
  148. */
  149. void drm_atomic_state_free(struct drm_atomic_state *state)
  150. {
  151. if (!state)
  152. return;
  153. drm_atomic_state_clear(state);
  154. DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
  155. kfree_state(state);
  156. }
  157. EXPORT_SYMBOL(drm_atomic_state_free);
  158. /**
  159. * drm_atomic_get_crtc_state - get crtc state
  160. * @state: global atomic state object
  161. * @crtc: crtc to get state object for
  162. *
  163. * This function returns the crtc state for the given crtc, allocating it if
  164. * needed. It will also grab the relevant crtc lock to make sure that the state
  165. * is consistent.
  166. *
  167. * Returns:
  168. *
  169. * Either the allocated state or the error code encoded into the pointer. When
  170. * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
  171. * entire atomic sequence must be restarted. All other errors are fatal.
  172. */
  173. struct drm_crtc_state *
  174. drm_atomic_get_crtc_state(struct drm_atomic_state *state,
  175. struct drm_crtc *crtc)
  176. {
  177. int ret, index;
  178. struct drm_crtc_state *crtc_state;
  179. index = drm_crtc_index(crtc);
  180. if (state->crtc_states[index])
  181. return state->crtc_states[index];
  182. ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
  183. if (ret)
  184. return ERR_PTR(ret);
  185. crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
  186. if (!crtc_state)
  187. return ERR_PTR(-ENOMEM);
  188. state->crtc_states[index] = crtc_state;
  189. state->crtcs[index] = crtc;
  190. crtc_state->state = state;
  191. DRM_DEBUG_ATOMIC("Added [CRTC:%d] %p state to %p\n",
  192. crtc->base.id, crtc_state, state);
  193. return crtc_state;
  194. }
  195. EXPORT_SYMBOL(drm_atomic_get_crtc_state);
  196. /**
  197. * drm_atomic_crtc_set_property - set property on CRTC
  198. * @crtc: the drm CRTC to set a property on
  199. * @state: the state object to update with the new property value
  200. * @property: the property to set
  201. * @val: the new property value
  202. *
  203. * Use this instead of calling crtc->atomic_set_property directly.
  204. * This function handles generic/core properties and calls out to
  205. * driver's ->atomic_set_property() for driver properties. To ensure
  206. * consistent behavior you must call this function rather than the
  207. * driver hook directly.
  208. *
  209. * RETURNS:
  210. * Zero on success, error code on failure
  211. */
  212. int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
  213. struct drm_crtc_state *state, struct drm_property *property,
  214. uint64_t val)
  215. {
  216. struct drm_device *dev = crtc->dev;
  217. struct drm_mode_config *config = &dev->mode_config;
  218. /* FIXME: Mode prop is missing, which also controls ->enable. */
  219. if (property == config->prop_active)
  220. state->active = val;
  221. else if (crtc->funcs->atomic_set_property)
  222. return crtc->funcs->atomic_set_property(crtc, state, property, val);
  223. else
  224. return -EINVAL;
  225. return 0;
  226. }
  227. EXPORT_SYMBOL(drm_atomic_crtc_set_property);
  228. /*
  229. * This function handles generic/core properties and calls out to
  230. * driver's ->atomic_get_property() for driver properties. To ensure
  231. * consistent behavior you must call this function rather than the
  232. * driver hook directly.
  233. */
  234. int drm_atomic_crtc_get_property(struct drm_crtc *crtc,
  235. const struct drm_crtc_state *state,
  236. struct drm_property *property, uint64_t *val)
  237. {
  238. struct drm_device *dev = crtc->dev;
  239. struct drm_mode_config *config = &dev->mode_config;
  240. if (property == config->prop_active)
  241. *val = state->active;
  242. else if (crtc->funcs->atomic_get_property)
  243. return crtc->funcs->atomic_get_property(crtc, state, property, val);
  244. else
  245. return -EINVAL;
  246. return 0;
  247. }
  248. /**
  249. * drm_atomic_crtc_check - check crtc state
  250. * @crtc: crtc to check
  251. * @state: crtc state to check
  252. *
  253. * Provides core sanity checks for crtc state.
  254. *
  255. * RETURNS:
  256. * Zero on success, error code on failure
  257. */
  258. static int drm_atomic_crtc_check(struct drm_crtc *crtc,
  259. struct drm_crtc_state *state)
  260. {
  261. /* NOTE: we explicitly don't enforce constraints such as primary
  262. * layer covering entire screen, since that is something we want
  263. * to allow (on hw that supports it). For hw that does not, it
  264. * should be checked in driver's crtc->atomic_check() vfunc.
  265. *
  266. * TODO: Add generic modeset state checks once we support those.
  267. */
  268. if (state->active && !state->enable) {
  269. DRM_DEBUG_ATOMIC("[CRTC:%d] active without enabled\n",
  270. crtc->base.id);
  271. return -EINVAL;
  272. }
  273. return 0;
  274. }
  275. /**
  276. * drm_atomic_get_plane_state - get plane state
  277. * @state: global atomic state object
  278. * @plane: plane to get state object for
  279. *
  280. * This function returns the plane state for the given plane, allocating it if
  281. * needed. It will also grab the relevant plane lock to make sure that the state
  282. * is consistent.
  283. *
  284. * Returns:
  285. *
  286. * Either the allocated state or the error code encoded into the pointer. When
  287. * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
  288. * entire atomic sequence must be restarted. All other errors are fatal.
  289. */
  290. struct drm_plane_state *
  291. drm_atomic_get_plane_state(struct drm_atomic_state *state,
  292. struct drm_plane *plane)
  293. {
  294. int ret, index;
  295. struct drm_plane_state *plane_state;
  296. index = drm_plane_index(plane);
  297. if (state->plane_states[index])
  298. return state->plane_states[index];
  299. ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
  300. if (ret)
  301. return ERR_PTR(ret);
  302. plane_state = plane->funcs->atomic_duplicate_state(plane);
  303. if (!plane_state)
  304. return ERR_PTR(-ENOMEM);
  305. state->plane_states[index] = plane_state;
  306. state->planes[index] = plane;
  307. plane_state->state = state;
  308. DRM_DEBUG_ATOMIC("Added [PLANE:%d] %p state to %p\n",
  309. plane->base.id, plane_state, state);
  310. if (plane_state->crtc) {
  311. struct drm_crtc_state *crtc_state;
  312. crtc_state = drm_atomic_get_crtc_state(state,
  313. plane_state->crtc);
  314. if (IS_ERR(crtc_state))
  315. return ERR_CAST(crtc_state);
  316. }
  317. return plane_state;
  318. }
  319. EXPORT_SYMBOL(drm_atomic_get_plane_state);
  320. /**
  321. * drm_atomic_plane_set_property - set property on plane
  322. * @plane: the drm plane to set a property on
  323. * @state: the state object to update with the new property value
  324. * @property: the property to set
  325. * @val: the new property value
  326. *
  327. * Use this instead of calling plane->atomic_set_property directly.
  328. * This function handles generic/core properties and calls out to
  329. * driver's ->atomic_set_property() for driver properties. To ensure
  330. * consistent behavior you must call this function rather than the
  331. * driver hook directly.
  332. *
  333. * RETURNS:
  334. * Zero on success, error code on failure
  335. */
  336. int drm_atomic_plane_set_property(struct drm_plane *plane,
  337. struct drm_plane_state *state, struct drm_property *property,
  338. uint64_t val)
  339. {
  340. struct drm_device *dev = plane->dev;
  341. struct drm_mode_config *config = &dev->mode_config;
  342. if (property == config->prop_fb_id) {
  343. struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
  344. drm_atomic_set_fb_for_plane(state, fb);
  345. if (fb)
  346. drm_framebuffer_unreference(fb);
  347. } else if (property == config->prop_crtc_id) {
  348. struct drm_crtc *crtc = drm_crtc_find(dev, val);
  349. return drm_atomic_set_crtc_for_plane(state, crtc);
  350. } else if (property == config->prop_crtc_x) {
  351. state->crtc_x = U642I64(val);
  352. } else if (property == config->prop_crtc_y) {
  353. state->crtc_y = U642I64(val);
  354. } else if (property == config->prop_crtc_w) {
  355. state->crtc_w = val;
  356. } else if (property == config->prop_crtc_h) {
  357. state->crtc_h = val;
  358. } else if (property == config->prop_src_x) {
  359. state->src_x = val;
  360. } else if (property == config->prop_src_y) {
  361. state->src_y = val;
  362. } else if (property == config->prop_src_w) {
  363. state->src_w = val;
  364. } else if (property == config->prop_src_h) {
  365. state->src_h = val;
  366. } else if (property == config->rotation_property) {
  367. state->rotation = val;
  368. } else if (plane->funcs->atomic_set_property) {
  369. return plane->funcs->atomic_set_property(plane, state,
  370. property, val);
  371. } else {
  372. return -EINVAL;
  373. }
  374. return 0;
  375. }
  376. EXPORT_SYMBOL(drm_atomic_plane_set_property);
  377. /*
  378. * This function handles generic/core properties and calls out to
  379. * driver's ->atomic_get_property() for driver properties. To ensure
  380. * consistent behavior you must call this function rather than the
  381. * driver hook directly.
  382. */
  383. static int
  384. drm_atomic_plane_get_property(struct drm_plane *plane,
  385. const struct drm_plane_state *state,
  386. struct drm_property *property, uint64_t *val)
  387. {
  388. struct drm_device *dev = plane->dev;
  389. struct drm_mode_config *config = &dev->mode_config;
  390. if (property == config->prop_fb_id) {
  391. *val = (state->fb) ? state->fb->base.id : 0;
  392. } else if (property == config->prop_crtc_id) {
  393. *val = (state->crtc) ? state->crtc->base.id : 0;
  394. } else if (property == config->prop_crtc_x) {
  395. *val = I642U64(state->crtc_x);
  396. } else if (property == config->prop_crtc_y) {
  397. *val = I642U64(state->crtc_y);
  398. } else if (property == config->prop_crtc_w) {
  399. *val = state->crtc_w;
  400. } else if (property == config->prop_crtc_h) {
  401. *val = state->crtc_h;
  402. } else if (property == config->prop_src_x) {
  403. *val = state->src_x;
  404. } else if (property == config->prop_src_y) {
  405. *val = state->src_y;
  406. } else if (property == config->prop_src_w) {
  407. *val = state->src_w;
  408. } else if (property == config->prop_src_h) {
  409. *val = state->src_h;
  410. } else if (property == config->rotation_property) {
  411. *val = state->rotation;
  412. } else if (plane->funcs->atomic_get_property) {
  413. return plane->funcs->atomic_get_property(plane, state, property, val);
  414. } else {
  415. return -EINVAL;
  416. }
  417. return 0;
  418. }
  419. /**
  420. * drm_atomic_plane_check - check plane state
  421. * @plane: plane to check
  422. * @state: plane state to check
  423. *
  424. * Provides core sanity checks for plane state.
  425. *
  426. * RETURNS:
  427. * Zero on success, error code on failure
  428. */
  429. static int drm_atomic_plane_check(struct drm_plane *plane,
  430. struct drm_plane_state *state)
  431. {
  432. unsigned int fb_width, fb_height;
  433. int ret;
  434. /* either *both* CRTC and FB must be set, or neither */
  435. if (WARN_ON(state->crtc && !state->fb)) {
  436. DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
  437. return -EINVAL;
  438. } else if (WARN_ON(state->fb && !state->crtc)) {
  439. DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
  440. return -EINVAL;
  441. }
  442. /* if disabled, we don't care about the rest of the state: */
  443. if (!state->crtc)
  444. return 0;
  445. /* Check whether this plane is usable on this CRTC */
  446. if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
  447. DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
  448. return -EINVAL;
  449. }
  450. /* Check whether this plane supports the fb pixel format. */
  451. ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format);
  452. if (ret) {
  453. DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
  454. drm_get_format_name(state->fb->pixel_format));
  455. return ret;
  456. }
  457. /* Give drivers some help against integer overflows */
  458. if (state->crtc_w > INT_MAX ||
  459. state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
  460. state->crtc_h > INT_MAX ||
  461. state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
  462. DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
  463. state->crtc_w, state->crtc_h,
  464. state->crtc_x, state->crtc_y);
  465. return -ERANGE;
  466. }
  467. fb_width = state->fb->width << 16;
  468. fb_height = state->fb->height << 16;
  469. /* Make sure source coordinates are inside the fb. */
  470. if (state->src_w > fb_width ||
  471. state->src_x > fb_width - state->src_w ||
  472. state->src_h > fb_height ||
  473. state->src_y > fb_height - state->src_h) {
  474. DRM_DEBUG_ATOMIC("Invalid source coordinates "
  475. "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
  476. state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
  477. state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
  478. state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
  479. state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
  480. return -ENOSPC;
  481. }
  482. return 0;
  483. }
  484. /**
  485. * drm_atomic_get_connector_state - get connector state
  486. * @state: global atomic state object
  487. * @connector: connector to get state object for
  488. *
  489. * This function returns the connector state for the given connector,
  490. * allocating it if needed. It will also grab the relevant connector lock to
  491. * make sure that the state is consistent.
  492. *
  493. * Returns:
  494. *
  495. * Either the allocated state or the error code encoded into the pointer. When
  496. * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
  497. * entire atomic sequence must be restarted. All other errors are fatal.
  498. */
  499. struct drm_connector_state *
  500. drm_atomic_get_connector_state(struct drm_atomic_state *state,
  501. struct drm_connector *connector)
  502. {
  503. int ret, index;
  504. struct drm_mode_config *config = &connector->dev->mode_config;
  505. struct drm_connector_state *connector_state;
  506. ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
  507. if (ret)
  508. return ERR_PTR(ret);
  509. index = drm_connector_index(connector);
  510. /*
  511. * Construction of atomic state updates can race with a connector
  512. * hot-add which might overflow. In this case flip the table and just
  513. * restart the entire ioctl - no one is fast enough to livelock a cpu
  514. * with physical hotplug events anyway.
  515. *
  516. * Note that we only grab the indexes once we have the right lock to
  517. * prevent hotplug/unplugging of connectors. So removal is no problem,
  518. * at most the array is a bit too large.
  519. */
  520. if (index >= state->num_connector) {
  521. DRM_DEBUG_ATOMIC("Hot-added connector would overflow state array, restarting\n");
  522. return ERR_PTR(-EAGAIN);
  523. }
  524. if (state->connector_states[index])
  525. return state->connector_states[index];
  526. connector_state = connector->funcs->atomic_duplicate_state(connector);
  527. if (!connector_state)
  528. return ERR_PTR(-ENOMEM);
  529. state->connector_states[index] = connector_state;
  530. state->connectors[index] = connector;
  531. connector_state->state = state;
  532. DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
  533. connector->base.id, connector_state, state);
  534. if (connector_state->crtc) {
  535. struct drm_crtc_state *crtc_state;
  536. crtc_state = drm_atomic_get_crtc_state(state,
  537. connector_state->crtc);
  538. if (IS_ERR(crtc_state))
  539. return ERR_CAST(crtc_state);
  540. }
  541. return connector_state;
  542. }
  543. EXPORT_SYMBOL(drm_atomic_get_connector_state);
  544. /**
  545. * drm_atomic_connector_set_property - set property on connector.
  546. * @connector: the drm connector to set a property on
  547. * @state: the state object to update with the new property value
  548. * @property: the property to set
  549. * @val: the new property value
  550. *
  551. * Use this instead of calling connector->atomic_set_property directly.
  552. * This function handles generic/core properties and calls out to
  553. * driver's ->atomic_set_property() for driver properties. To ensure
  554. * consistent behavior you must call this function rather than the
  555. * driver hook directly.
  556. *
  557. * RETURNS:
  558. * Zero on success, error code on failure
  559. */
  560. int drm_atomic_connector_set_property(struct drm_connector *connector,
  561. struct drm_connector_state *state, struct drm_property *property,
  562. uint64_t val)
  563. {
  564. struct drm_device *dev = connector->dev;
  565. struct drm_mode_config *config = &dev->mode_config;
  566. if (property == config->prop_crtc_id) {
  567. struct drm_crtc *crtc = drm_crtc_find(dev, val);
  568. return drm_atomic_set_crtc_for_connector(state, crtc);
  569. } else if (property == config->dpms_property) {
  570. /* setting DPMS property requires special handling, which
  571. * is done in legacy setprop path for us. Disallow (for
  572. * now?) atomic writes to DPMS property:
  573. */
  574. return -EINVAL;
  575. } else if (connector->funcs->atomic_set_property) {
  576. return connector->funcs->atomic_set_property(connector,
  577. state, property, val);
  578. } else {
  579. return -EINVAL;
  580. }
  581. }
  582. EXPORT_SYMBOL(drm_atomic_connector_set_property);
  583. /*
  584. * This function handles generic/core properties and calls out to
  585. * driver's ->atomic_get_property() for driver properties. To ensure
  586. * consistent behavior you must call this function rather than the
  587. * driver hook directly.
  588. */
  589. static int
  590. drm_atomic_connector_get_property(struct drm_connector *connector,
  591. const struct drm_connector_state *state,
  592. struct drm_property *property, uint64_t *val)
  593. {
  594. struct drm_device *dev = connector->dev;
  595. struct drm_mode_config *config = &dev->mode_config;
  596. if (property == config->prop_crtc_id) {
  597. *val = (state->crtc) ? state->crtc->base.id : 0;
  598. } else if (property == config->dpms_property) {
  599. *val = connector->dpms;
  600. } else if (connector->funcs->atomic_get_property) {
  601. return connector->funcs->atomic_get_property(connector,
  602. state, property, val);
  603. } else {
  604. return -EINVAL;
  605. }
  606. return 0;
  607. }
  608. int drm_atomic_get_property(struct drm_mode_object *obj,
  609. struct drm_property *property, uint64_t *val)
  610. {
  611. struct drm_device *dev = property->dev;
  612. int ret;
  613. switch (obj->type) {
  614. case DRM_MODE_OBJECT_CONNECTOR: {
  615. struct drm_connector *connector = obj_to_connector(obj);
  616. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  617. ret = drm_atomic_connector_get_property(connector,
  618. connector->state, property, val);
  619. break;
  620. }
  621. case DRM_MODE_OBJECT_CRTC: {
  622. struct drm_crtc *crtc = obj_to_crtc(obj);
  623. WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
  624. ret = drm_atomic_crtc_get_property(crtc,
  625. crtc->state, property, val);
  626. break;
  627. }
  628. case DRM_MODE_OBJECT_PLANE: {
  629. struct drm_plane *plane = obj_to_plane(obj);
  630. WARN_ON(!drm_modeset_is_locked(&plane->mutex));
  631. ret = drm_atomic_plane_get_property(plane,
  632. plane->state, property, val);
  633. break;
  634. }
  635. default:
  636. ret = -EINVAL;
  637. break;
  638. }
  639. return ret;
  640. }
  641. /**
  642. * drm_atomic_set_crtc_for_plane - set crtc for plane
  643. * @plane_state: the plane whose incoming state to update
  644. * @crtc: crtc to use for the plane
  645. *
  646. * Changing the assigned crtc for a plane requires us to grab the lock and state
  647. * for the new crtc, as needed. This function takes care of all these details
  648. * besides updating the pointer in the state object itself.
  649. *
  650. * Returns:
  651. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  652. * then the w/w mutex code has detected a deadlock and the entire atomic
  653. * sequence must be restarted. All other errors are fatal.
  654. */
  655. int
  656. drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
  657. struct drm_crtc *crtc)
  658. {
  659. struct drm_plane *plane = plane_state->plane;
  660. struct drm_crtc_state *crtc_state;
  661. if (plane_state->crtc) {
  662. crtc_state = drm_atomic_get_crtc_state(plane_state->state,
  663. plane_state->crtc);
  664. if (WARN_ON(IS_ERR(crtc_state)))
  665. return PTR_ERR(crtc_state);
  666. crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
  667. }
  668. plane_state->crtc = crtc;
  669. if (crtc) {
  670. crtc_state = drm_atomic_get_crtc_state(plane_state->state,
  671. crtc);
  672. if (IS_ERR(crtc_state))
  673. return PTR_ERR(crtc_state);
  674. crtc_state->plane_mask |= (1 << drm_plane_index(plane));
  675. }
  676. if (crtc)
  677. DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d]\n",
  678. plane_state, crtc->base.id);
  679. else
  680. DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
  681. plane_state);
  682. return 0;
  683. }
  684. EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
  685. /**
  686. * drm_atomic_set_fb_for_plane - set framebuffer for plane
  687. * @plane_state: atomic state object for the plane
  688. * @fb: fb to use for the plane
  689. *
  690. * Changing the assigned framebuffer for a plane requires us to grab a reference
  691. * to the new fb and drop the reference to the old fb, if there is one. This
  692. * function takes care of all these details besides updating the pointer in the
  693. * state object itself.
  694. */
  695. void
  696. drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
  697. struct drm_framebuffer *fb)
  698. {
  699. if (plane_state->fb)
  700. drm_framebuffer_unreference(plane_state->fb);
  701. if (fb)
  702. drm_framebuffer_reference(fb);
  703. plane_state->fb = fb;
  704. if (fb)
  705. DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
  706. fb->base.id, plane_state);
  707. else
  708. DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
  709. plane_state);
  710. }
  711. EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
  712. /**
  713. * drm_atomic_set_crtc_for_connector - set crtc for connector
  714. * @conn_state: atomic state object for the connector
  715. * @crtc: crtc to use for the connector
  716. *
  717. * Changing the assigned crtc for a connector requires us to grab the lock and
  718. * state for the new crtc, as needed. This function takes care of all these
  719. * details besides updating the pointer in the state object itself.
  720. *
  721. * Returns:
  722. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  723. * then the w/w mutex code has detected a deadlock and the entire atomic
  724. * sequence must be restarted. All other errors are fatal.
  725. */
  726. int
  727. drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
  728. struct drm_crtc *crtc)
  729. {
  730. struct drm_crtc_state *crtc_state;
  731. if (crtc) {
  732. crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
  733. if (IS_ERR(crtc_state))
  734. return PTR_ERR(crtc_state);
  735. }
  736. conn_state->crtc = crtc;
  737. if (crtc)
  738. DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d]\n",
  739. conn_state, crtc->base.id);
  740. else
  741. DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
  742. conn_state);
  743. return 0;
  744. }
  745. EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
  746. /**
  747. * drm_atomic_add_affected_connectors - add connectors for crtc
  748. * @state: atomic state
  749. * @crtc: DRM crtc
  750. *
  751. * This function walks the current configuration and adds all connectors
  752. * currently using @crtc to the atomic configuration @state. Note that this
  753. * function must acquire the connection mutex. This can potentially cause
  754. * unneeded seralization if the update is just for the planes on one crtc. Hence
  755. * drivers and helpers should only call this when really needed (e.g. when a
  756. * full modeset needs to happen due to some change).
  757. *
  758. * Returns:
  759. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  760. * then the w/w mutex code has detected a deadlock and the entire atomic
  761. * sequence must be restarted. All other errors are fatal.
  762. */
  763. int
  764. drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
  765. struct drm_crtc *crtc)
  766. {
  767. struct drm_mode_config *config = &state->dev->mode_config;
  768. struct drm_connector *connector;
  769. struct drm_connector_state *conn_state;
  770. int ret;
  771. ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
  772. if (ret)
  773. return ret;
  774. DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d] to %p\n",
  775. crtc->base.id, state);
  776. /*
  777. * Changed connectors are already in @state, so only need to look at the
  778. * current configuration.
  779. */
  780. list_for_each_entry(connector, &config->connector_list, head) {
  781. if (connector->state->crtc != crtc)
  782. continue;
  783. conn_state = drm_atomic_get_connector_state(state, connector);
  784. if (IS_ERR(conn_state))
  785. return PTR_ERR(conn_state);
  786. }
  787. return 0;
  788. }
  789. EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
  790. /**
  791. * drm_atomic_connectors_for_crtc - count number of connected outputs
  792. * @state: atomic state
  793. * @crtc: DRM crtc
  794. *
  795. * This function counts all connectors which will be connected to @crtc
  796. * according to @state. Useful to recompute the enable state for @crtc.
  797. */
  798. int
  799. drm_atomic_connectors_for_crtc(struct drm_atomic_state *state,
  800. struct drm_crtc *crtc)
  801. {
  802. struct drm_connector *connector;
  803. struct drm_connector_state *conn_state;
  804. int i, num_connected_connectors = 0;
  805. for_each_connector_in_state(state, connector, conn_state, i) {
  806. if (conn_state->crtc == crtc)
  807. num_connected_connectors++;
  808. }
  809. DRM_DEBUG_ATOMIC("State %p has %i connectors for [CRTC:%d]\n",
  810. state, num_connected_connectors, crtc->base.id);
  811. return num_connected_connectors;
  812. }
  813. EXPORT_SYMBOL(drm_atomic_connectors_for_crtc);
  814. /**
  815. * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
  816. * @state: atomic state
  817. *
  818. * This function should be used by legacy entry points which don't understand
  819. * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
  820. * the slowpath completed.
  821. */
  822. void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
  823. {
  824. int ret;
  825. retry:
  826. drm_modeset_backoff(state->acquire_ctx);
  827. ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
  828. state->acquire_ctx);
  829. if (ret)
  830. goto retry;
  831. ret = drm_modeset_lock_all_crtcs(state->dev,
  832. state->acquire_ctx);
  833. if (ret)
  834. goto retry;
  835. }
  836. EXPORT_SYMBOL(drm_atomic_legacy_backoff);
  837. /**
  838. * drm_atomic_check_only - check whether a given config would work
  839. * @state: atomic configuration to check
  840. *
  841. * Note that this function can return -EDEADLK if the driver needed to acquire
  842. * more locks but encountered a deadlock. The caller must then do the usual w/w
  843. * backoff dance and restart. All other errors are fatal.
  844. *
  845. * Returns:
  846. * 0 on success, negative error code on failure.
  847. */
  848. int drm_atomic_check_only(struct drm_atomic_state *state)
  849. {
  850. struct drm_device *dev = state->dev;
  851. struct drm_mode_config *config = &dev->mode_config;
  852. struct drm_plane *plane;
  853. struct drm_plane_state *plane_state;
  854. struct drm_crtc *crtc;
  855. struct drm_crtc_state *crtc_state;
  856. int i, ret = 0;
  857. DRM_DEBUG_ATOMIC("checking %p\n", state);
  858. for_each_plane_in_state(state, plane, plane_state, i) {
  859. ret = drm_atomic_plane_check(plane, plane_state);
  860. if (ret) {
  861. DRM_DEBUG_ATOMIC("[PLANE:%d] atomic core check failed\n",
  862. plane->base.id);
  863. return ret;
  864. }
  865. }
  866. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  867. ret = drm_atomic_crtc_check(crtc, crtc_state);
  868. if (ret) {
  869. DRM_DEBUG_ATOMIC("[CRTC:%d] atomic core check failed\n",
  870. crtc->base.id);
  871. return ret;
  872. }
  873. }
  874. if (config->funcs->atomic_check)
  875. ret = config->funcs->atomic_check(state->dev, state);
  876. if (!state->allow_modeset) {
  877. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  878. if (crtc_state->mode_changed ||
  879. crtc_state->active_changed) {
  880. DRM_DEBUG_ATOMIC("[CRTC:%d] requires full modeset\n",
  881. crtc->base.id);
  882. return -EINVAL;
  883. }
  884. }
  885. }
  886. return ret;
  887. }
  888. EXPORT_SYMBOL(drm_atomic_check_only);
  889. /**
  890. * drm_atomic_commit - commit configuration atomically
  891. * @state: atomic configuration to check
  892. *
  893. * Note that this function can return -EDEADLK if the driver needed to acquire
  894. * more locks but encountered a deadlock. The caller must then do the usual w/w
  895. * backoff dance and restart. All other errors are fatal.
  896. *
  897. * Also note that on successful execution ownership of @state is transferred
  898. * from the caller of this function to the function itself. The caller must not
  899. * free or in any other way access @state. If the function fails then the caller
  900. * must clean up @state itself.
  901. *
  902. * Returns:
  903. * 0 on success, negative error code on failure.
  904. */
  905. int drm_atomic_commit(struct drm_atomic_state *state)
  906. {
  907. struct drm_mode_config *config = &state->dev->mode_config;
  908. int ret;
  909. ret = drm_atomic_check_only(state);
  910. if (ret)
  911. return ret;
  912. DRM_DEBUG_ATOMIC("commiting %p\n", state);
  913. return config->funcs->atomic_commit(state->dev, state, false);
  914. }
  915. EXPORT_SYMBOL(drm_atomic_commit);
  916. /**
  917. * drm_atomic_async_commit - atomic&async configuration commit
  918. * @state: atomic configuration to check
  919. *
  920. * Note that this function can return -EDEADLK if the driver needed to acquire
  921. * more locks but encountered a deadlock. The caller must then do the usual w/w
  922. * backoff dance and restart. All other errors are fatal.
  923. *
  924. * Also note that on successful execution ownership of @state is transferred
  925. * from the caller of this function to the function itself. The caller must not
  926. * free or in any other way access @state. If the function fails then the caller
  927. * must clean up @state itself.
  928. *
  929. * Returns:
  930. * 0 on success, negative error code on failure.
  931. */
  932. int drm_atomic_async_commit(struct drm_atomic_state *state)
  933. {
  934. struct drm_mode_config *config = &state->dev->mode_config;
  935. int ret;
  936. ret = drm_atomic_check_only(state);
  937. if (ret)
  938. return ret;
  939. DRM_DEBUG_ATOMIC("commiting %p asynchronously\n", state);
  940. return config->funcs->atomic_commit(state->dev, state, true);
  941. }
  942. EXPORT_SYMBOL(drm_atomic_async_commit);
  943. /*
  944. * The big monstor ioctl
  945. */
  946. static struct drm_pending_vblank_event *create_vblank_event(
  947. struct drm_device *dev, struct drm_file *file_priv, uint64_t user_data)
  948. {
  949. struct drm_pending_vblank_event *e = NULL;
  950. unsigned long flags;
  951. spin_lock_irqsave(&dev->event_lock, flags);
  952. if (file_priv->event_space < sizeof e->event) {
  953. spin_unlock_irqrestore(&dev->event_lock, flags);
  954. goto out;
  955. }
  956. file_priv->event_space -= sizeof e->event;
  957. spin_unlock_irqrestore(&dev->event_lock, flags);
  958. e = kzalloc(sizeof *e, GFP_KERNEL);
  959. if (e == NULL) {
  960. spin_lock_irqsave(&dev->event_lock, flags);
  961. file_priv->event_space += sizeof e->event;
  962. spin_unlock_irqrestore(&dev->event_lock, flags);
  963. goto out;
  964. }
  965. e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
  966. e->event.base.length = sizeof e->event;
  967. e->event.user_data = user_data;
  968. e->base.event = &e->event.base;
  969. e->base.file_priv = file_priv;
  970. e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
  971. out:
  972. return e;
  973. }
  974. static void destroy_vblank_event(struct drm_device *dev,
  975. struct drm_file *file_priv, struct drm_pending_vblank_event *e)
  976. {
  977. unsigned long flags;
  978. spin_lock_irqsave(&dev->event_lock, flags);
  979. file_priv->event_space += sizeof e->event;
  980. spin_unlock_irqrestore(&dev->event_lock, flags);
  981. kfree(e);
  982. }
  983. static int atomic_set_prop(struct drm_atomic_state *state,
  984. struct drm_mode_object *obj, struct drm_property *prop,
  985. uint64_t prop_value)
  986. {
  987. struct drm_mode_object *ref;
  988. int ret;
  989. if (!drm_property_change_valid_get(prop, prop_value, &ref))
  990. return -EINVAL;
  991. switch (obj->type) {
  992. case DRM_MODE_OBJECT_CONNECTOR: {
  993. struct drm_connector *connector = obj_to_connector(obj);
  994. struct drm_connector_state *connector_state;
  995. connector_state = drm_atomic_get_connector_state(state, connector);
  996. if (IS_ERR(connector_state)) {
  997. ret = PTR_ERR(connector_state);
  998. break;
  999. }
  1000. ret = drm_atomic_connector_set_property(connector,
  1001. connector_state, prop, prop_value);
  1002. break;
  1003. }
  1004. case DRM_MODE_OBJECT_CRTC: {
  1005. struct drm_crtc *crtc = obj_to_crtc(obj);
  1006. struct drm_crtc_state *crtc_state;
  1007. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  1008. if (IS_ERR(crtc_state)) {
  1009. ret = PTR_ERR(crtc_state);
  1010. break;
  1011. }
  1012. ret = drm_atomic_crtc_set_property(crtc,
  1013. crtc_state, prop, prop_value);
  1014. break;
  1015. }
  1016. case DRM_MODE_OBJECT_PLANE: {
  1017. struct drm_plane *plane = obj_to_plane(obj);
  1018. struct drm_plane_state *plane_state;
  1019. plane_state = drm_atomic_get_plane_state(state, plane);
  1020. if (IS_ERR(plane_state)) {
  1021. ret = PTR_ERR(plane_state);
  1022. break;
  1023. }
  1024. ret = drm_atomic_plane_set_property(plane,
  1025. plane_state, prop, prop_value);
  1026. break;
  1027. }
  1028. default:
  1029. ret = -EINVAL;
  1030. break;
  1031. }
  1032. drm_property_change_valid_put(prop, ref);
  1033. return ret;
  1034. }
  1035. int drm_mode_atomic_ioctl(struct drm_device *dev,
  1036. void *data, struct drm_file *file_priv)
  1037. {
  1038. struct drm_mode_atomic *arg = data;
  1039. uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
  1040. uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
  1041. uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
  1042. uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
  1043. unsigned int copied_objs, copied_props;
  1044. struct drm_atomic_state *state;
  1045. struct drm_modeset_acquire_ctx ctx;
  1046. struct drm_plane *plane;
  1047. struct drm_crtc *crtc;
  1048. struct drm_crtc_state *crtc_state;
  1049. unsigned plane_mask = 0;
  1050. int ret = 0;
  1051. unsigned int i, j;
  1052. /* disallow for drivers not supporting atomic: */
  1053. if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
  1054. return -EINVAL;
  1055. /* disallow for userspace that has not enabled atomic cap (even
  1056. * though this may be a bit overkill, since legacy userspace
  1057. * wouldn't know how to call this ioctl)
  1058. */
  1059. if (!file_priv->atomic)
  1060. return -EINVAL;
  1061. if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
  1062. return -EINVAL;
  1063. if (arg->reserved)
  1064. return -EINVAL;
  1065. if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
  1066. !dev->mode_config.async_page_flip)
  1067. return -EINVAL;
  1068. /* can't test and expect an event at the same time. */
  1069. if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
  1070. (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
  1071. return -EINVAL;
  1072. drm_modeset_acquire_init(&ctx, 0);
  1073. state = drm_atomic_state_alloc(dev);
  1074. if (!state)
  1075. return -ENOMEM;
  1076. state->acquire_ctx = &ctx;
  1077. state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
  1078. retry:
  1079. copied_objs = 0;
  1080. copied_props = 0;
  1081. for (i = 0; i < arg->count_objs; i++) {
  1082. uint32_t obj_id, count_props;
  1083. struct drm_mode_object *obj;
  1084. if (get_user(obj_id, objs_ptr + copied_objs)) {
  1085. ret = -EFAULT;
  1086. goto fail;
  1087. }
  1088. obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
  1089. if (!obj || !obj->properties) {
  1090. ret = -ENOENT;
  1091. goto fail;
  1092. }
  1093. if (obj->type == DRM_MODE_OBJECT_PLANE) {
  1094. plane = obj_to_plane(obj);
  1095. plane_mask |= (1 << drm_plane_index(plane));
  1096. plane->old_fb = plane->fb;
  1097. }
  1098. if (get_user(count_props, count_props_ptr + copied_objs)) {
  1099. ret = -EFAULT;
  1100. goto fail;
  1101. }
  1102. copied_objs++;
  1103. for (j = 0; j < count_props; j++) {
  1104. uint32_t prop_id;
  1105. uint64_t prop_value;
  1106. struct drm_property *prop;
  1107. if (get_user(prop_id, props_ptr + copied_props)) {
  1108. ret = -EFAULT;
  1109. goto fail;
  1110. }
  1111. prop = drm_property_find(dev, prop_id);
  1112. if (!prop) {
  1113. ret = -ENOENT;
  1114. goto fail;
  1115. }
  1116. if (copy_from_user(&prop_value,
  1117. prop_values_ptr + copied_props,
  1118. sizeof(prop_value))) {
  1119. ret = -EFAULT;
  1120. goto fail;
  1121. }
  1122. ret = atomic_set_prop(state, obj, prop, prop_value);
  1123. if (ret)
  1124. goto fail;
  1125. copied_props++;
  1126. }
  1127. }
  1128. if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  1129. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  1130. struct drm_pending_vblank_event *e;
  1131. e = create_vblank_event(dev, file_priv, arg->user_data);
  1132. if (!e) {
  1133. ret = -ENOMEM;
  1134. goto fail;
  1135. }
  1136. crtc_state->event = e;
  1137. }
  1138. }
  1139. if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
  1140. ret = drm_atomic_check_only(state);
  1141. /* _check_only() does not free state, unlike _commit() */
  1142. drm_atomic_state_free(state);
  1143. } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
  1144. ret = drm_atomic_async_commit(state);
  1145. } else {
  1146. ret = drm_atomic_commit(state);
  1147. }
  1148. /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
  1149. * locks (ie. while it is still safe to deref plane->state). We
  1150. * need to do this here because the driver entry points cannot
  1151. * distinguish between legacy and atomic ioctls.
  1152. */
  1153. drm_for_each_plane_mask(plane, dev, plane_mask) {
  1154. if (ret == 0) {
  1155. struct drm_framebuffer *new_fb = plane->state->fb;
  1156. if (new_fb)
  1157. drm_framebuffer_reference(new_fb);
  1158. plane->fb = new_fb;
  1159. plane->crtc = plane->state->crtc;
  1160. } else {
  1161. plane->old_fb = NULL;
  1162. }
  1163. if (plane->old_fb) {
  1164. drm_framebuffer_unreference(plane->old_fb);
  1165. plane->old_fb = NULL;
  1166. }
  1167. }
  1168. drm_modeset_drop_locks(&ctx);
  1169. drm_modeset_acquire_fini(&ctx);
  1170. return ret;
  1171. fail:
  1172. if (ret == -EDEADLK)
  1173. goto backoff;
  1174. if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  1175. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  1176. destroy_vblank_event(dev, file_priv, crtc_state->event);
  1177. crtc_state->event = NULL;
  1178. }
  1179. }
  1180. drm_atomic_state_free(state);
  1181. drm_modeset_drop_locks(&ctx);
  1182. drm_modeset_acquire_fini(&ctx);
  1183. return ret;
  1184. backoff:
  1185. drm_atomic_state_clear(state);
  1186. drm_modeset_backoff(&ctx);
  1187. goto retry;
  1188. }