drm_atomic.c 38 KB

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