drm_atomic.c 40 KB

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