drm_atomic.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734
  1. /*
  2. * Copyright (C) 2014 Red Hat
  3. * Copyright (C) 2014 Intel Corp.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Rob Clark <robdclark@gmail.com>
  25. * Daniel Vetter <daniel.vetter@ffwll.ch>
  26. */
  27. #include <drm/drmP.h>
  28. #include <drm/drm_atomic.h>
  29. #include <drm/drm_mode.h>
  30. #include <drm/drm_plane_helper.h>
  31. #include "drm_crtc_internal.h"
  32. /**
  33. * drm_atomic_state_default_release -
  34. * release memory initialized by drm_atomic_state_init
  35. * @state: atomic state
  36. *
  37. * Free all the memory allocated by drm_atomic_state_init.
  38. * This is useful for drivers that subclass the atomic state.
  39. */
  40. void drm_atomic_state_default_release(struct drm_atomic_state *state)
  41. {
  42. kfree(state->connectors);
  43. kfree(state->crtcs);
  44. kfree(state->planes);
  45. }
  46. EXPORT_SYMBOL(drm_atomic_state_default_release);
  47. /**
  48. * drm_atomic_state_init - init new atomic state
  49. * @dev: DRM device
  50. * @state: atomic state
  51. *
  52. * Default implementation for filling in a new atomic state.
  53. * This is useful for drivers that subclass the atomic state.
  54. */
  55. int
  56. drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
  57. {
  58. /* TODO legacy paths should maybe do a better job about
  59. * setting this appropriately?
  60. */
  61. state->allow_modeset = true;
  62. state->crtcs = kcalloc(dev->mode_config.num_crtc,
  63. sizeof(*state->crtcs), GFP_KERNEL);
  64. if (!state->crtcs)
  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->dev = dev;
  71. DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
  72. return 0;
  73. fail:
  74. drm_atomic_state_default_release(state);
  75. return -ENOMEM;
  76. }
  77. EXPORT_SYMBOL(drm_atomic_state_init);
  78. /**
  79. * drm_atomic_state_alloc - allocate atomic state
  80. * @dev: DRM device
  81. *
  82. * This allocates an empty atomic state to track updates.
  83. */
  84. struct drm_atomic_state *
  85. drm_atomic_state_alloc(struct drm_device *dev)
  86. {
  87. struct drm_mode_config *config = &dev->mode_config;
  88. struct drm_atomic_state *state;
  89. if (!config->funcs->atomic_state_alloc) {
  90. state = kzalloc(sizeof(*state), GFP_KERNEL);
  91. if (!state)
  92. return NULL;
  93. if (drm_atomic_state_init(dev, state) < 0) {
  94. kfree(state);
  95. return NULL;
  96. }
  97. return state;
  98. }
  99. return config->funcs->atomic_state_alloc(dev);
  100. }
  101. EXPORT_SYMBOL(drm_atomic_state_alloc);
  102. /**
  103. * drm_atomic_state_default_clear - clear base atomic state
  104. * @state: atomic state
  105. *
  106. * Default implementation for clearing atomic state.
  107. * This is useful for drivers that subclass the atomic state.
  108. */
  109. void drm_atomic_state_default_clear(struct drm_atomic_state *state)
  110. {
  111. struct drm_device *dev = state->dev;
  112. struct drm_mode_config *config = &dev->mode_config;
  113. int i;
  114. DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
  115. for (i = 0; i < state->num_connector; i++) {
  116. struct drm_connector *connector = state->connectors[i].ptr;
  117. if (!connector)
  118. continue;
  119. connector->funcs->atomic_destroy_state(connector,
  120. state->connectors[i].state);
  121. state->connectors[i].ptr = NULL;
  122. state->connectors[i].state = NULL;
  123. drm_connector_unreference(connector);
  124. }
  125. for (i = 0; i < config->num_crtc; i++) {
  126. struct drm_crtc *crtc = state->crtcs[i].ptr;
  127. if (!crtc)
  128. continue;
  129. crtc->funcs->atomic_destroy_state(crtc,
  130. state->crtcs[i].state);
  131. state->crtcs[i].ptr = NULL;
  132. state->crtcs[i].state = NULL;
  133. }
  134. for (i = 0; i < config->num_total_plane; i++) {
  135. struct drm_plane *plane = state->planes[i].ptr;
  136. if (!plane)
  137. continue;
  138. plane->funcs->atomic_destroy_state(plane,
  139. state->planes[i].state);
  140. state->planes[i].ptr = NULL;
  141. state->planes[i].state = NULL;
  142. }
  143. }
  144. EXPORT_SYMBOL(drm_atomic_state_default_clear);
  145. /**
  146. * drm_atomic_state_clear - clear state object
  147. * @state: atomic state
  148. *
  149. * When the w/w mutex algorithm detects a deadlock we need to back off and drop
  150. * all locks. So someone else could sneak in and change the current modeset
  151. * configuration. Which means that all the state assembled in @state is no
  152. * longer an atomic update to the current state, but to some arbitrary earlier
  153. * state. Which could break assumptions the driver's ->atomic_check likely
  154. * relies on.
  155. *
  156. * Hence we must clear all cached state and completely start over, using this
  157. * function.
  158. */
  159. void drm_atomic_state_clear(struct drm_atomic_state *state)
  160. {
  161. struct drm_device *dev = state->dev;
  162. struct drm_mode_config *config = &dev->mode_config;
  163. if (config->funcs->atomic_state_clear)
  164. config->funcs->atomic_state_clear(state);
  165. else
  166. drm_atomic_state_default_clear(state);
  167. }
  168. EXPORT_SYMBOL(drm_atomic_state_clear);
  169. /**
  170. * drm_atomic_state_free - free all memory for an atomic state
  171. * @state: atomic state to deallocate
  172. *
  173. * This frees all memory associated with an atomic state, including all the
  174. * per-object state for planes, crtcs and connectors.
  175. */
  176. void drm_atomic_state_free(struct drm_atomic_state *state)
  177. {
  178. struct drm_device *dev;
  179. struct drm_mode_config *config;
  180. if (!state)
  181. return;
  182. dev = state->dev;
  183. config = &dev->mode_config;
  184. drm_atomic_state_clear(state);
  185. DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
  186. if (config->funcs->atomic_state_free) {
  187. config->funcs->atomic_state_free(state);
  188. } else {
  189. drm_atomic_state_default_release(state);
  190. kfree(state);
  191. }
  192. }
  193. EXPORT_SYMBOL(drm_atomic_state_free);
  194. /**
  195. * drm_atomic_get_crtc_state - get crtc state
  196. * @state: global atomic state object
  197. * @crtc: crtc to get state object for
  198. *
  199. * This function returns the crtc state for the given crtc, allocating it if
  200. * needed. It will also grab the relevant crtc lock to make sure that the state
  201. * is consistent.
  202. *
  203. * Returns:
  204. *
  205. * Either the allocated state or the error code encoded into the pointer. When
  206. * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
  207. * entire atomic sequence must be restarted. All other errors are fatal.
  208. */
  209. struct drm_crtc_state *
  210. drm_atomic_get_crtc_state(struct drm_atomic_state *state,
  211. struct drm_crtc *crtc)
  212. {
  213. int ret, index = drm_crtc_index(crtc);
  214. struct drm_crtc_state *crtc_state;
  215. WARN_ON(!state->acquire_ctx);
  216. crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
  217. if (crtc_state)
  218. return crtc_state;
  219. ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
  220. if (ret)
  221. return ERR_PTR(ret);
  222. crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
  223. if (!crtc_state)
  224. return ERR_PTR(-ENOMEM);
  225. state->crtcs[index].state = crtc_state;
  226. state->crtcs[index].ptr = crtc;
  227. crtc_state->state = state;
  228. DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
  229. crtc->base.id, crtc->name, crtc_state, state);
  230. return crtc_state;
  231. }
  232. EXPORT_SYMBOL(drm_atomic_get_crtc_state);
  233. /**
  234. * drm_atomic_set_mode_for_crtc - set mode for CRTC
  235. * @state: the CRTC whose incoming state to update
  236. * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
  237. *
  238. * Set a mode (originating from the kernel) on the desired CRTC state. Does
  239. * not change any other state properties, including enable, active, or
  240. * mode_changed.
  241. *
  242. * RETURNS:
  243. * Zero on success, error code on failure. Cannot return -EDEADLK.
  244. */
  245. int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
  246. struct drm_display_mode *mode)
  247. {
  248. struct drm_mode_modeinfo umode;
  249. /* Early return for no change. */
  250. if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
  251. return 0;
  252. drm_property_unreference_blob(state->mode_blob);
  253. state->mode_blob = NULL;
  254. if (mode) {
  255. drm_mode_convert_to_umode(&umode, mode);
  256. state->mode_blob =
  257. drm_property_create_blob(state->crtc->dev,
  258. sizeof(umode),
  259. &umode);
  260. if (IS_ERR(state->mode_blob))
  261. return PTR_ERR(state->mode_blob);
  262. drm_mode_copy(&state->mode, mode);
  263. state->enable = true;
  264. DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
  265. mode->name, state);
  266. } else {
  267. memset(&state->mode, 0, sizeof(state->mode));
  268. state->enable = false;
  269. DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
  270. state);
  271. }
  272. return 0;
  273. }
  274. EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
  275. /**
  276. * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
  277. * @state: the CRTC whose incoming state to update
  278. * @blob: pointer to blob property to use for mode
  279. *
  280. * Set a mode (originating from a blob property) on the desired CRTC state.
  281. * This function will take a reference on the blob property for the CRTC state,
  282. * and release the reference held on the state's existing mode property, if any
  283. * was set.
  284. *
  285. * RETURNS:
  286. * Zero on success, error code on failure. Cannot return -EDEADLK.
  287. */
  288. int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
  289. struct drm_property_blob *blob)
  290. {
  291. if (blob == state->mode_blob)
  292. return 0;
  293. drm_property_unreference_blob(state->mode_blob);
  294. state->mode_blob = NULL;
  295. memset(&state->mode, 0, sizeof(state->mode));
  296. if (blob) {
  297. if (blob->length != sizeof(struct drm_mode_modeinfo) ||
  298. drm_mode_convert_umode(&state->mode,
  299. (const struct drm_mode_modeinfo *)
  300. blob->data))
  301. return -EINVAL;
  302. state->mode_blob = drm_property_reference_blob(blob);
  303. state->enable = true;
  304. DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
  305. state->mode.name, state);
  306. } else {
  307. state->enable = false;
  308. DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
  309. state);
  310. }
  311. return 0;
  312. }
  313. EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
  314. /**
  315. * drm_atomic_replace_property_blob - replace a blob property
  316. * @blob: a pointer to the member blob to be replaced
  317. * @new_blob: the new blob to replace with
  318. * @replaced: whether the blob has been replaced
  319. *
  320. * RETURNS:
  321. * Zero on success, error code on failure
  322. */
  323. static void
  324. drm_atomic_replace_property_blob(struct drm_property_blob **blob,
  325. struct drm_property_blob *new_blob,
  326. bool *replaced)
  327. {
  328. struct drm_property_blob *old_blob = *blob;
  329. if (old_blob == new_blob)
  330. return;
  331. if (old_blob)
  332. drm_property_unreference_blob(old_blob);
  333. if (new_blob)
  334. drm_property_reference_blob(new_blob);
  335. *blob = new_blob;
  336. *replaced = true;
  337. return;
  338. }
  339. static int
  340. drm_atomic_replace_property_blob_from_id(struct drm_crtc *crtc,
  341. struct drm_property_blob **blob,
  342. uint64_t blob_id,
  343. ssize_t expected_size,
  344. bool *replaced)
  345. {
  346. struct drm_device *dev = crtc->dev;
  347. struct drm_property_blob *new_blob = NULL;
  348. if (blob_id != 0) {
  349. new_blob = drm_property_lookup_blob(dev, blob_id);
  350. if (new_blob == NULL)
  351. return -EINVAL;
  352. if (expected_size > 0 && expected_size != new_blob->length)
  353. return -EINVAL;
  354. }
  355. drm_atomic_replace_property_blob(blob, new_blob, replaced);
  356. return 0;
  357. }
  358. /**
  359. * drm_atomic_crtc_set_property - set property on CRTC
  360. * @crtc: the drm CRTC to set a property on
  361. * @state: the state object to update with the new property value
  362. * @property: the property to set
  363. * @val: the new property value
  364. *
  365. * Use this instead of calling crtc->atomic_set_property directly.
  366. * This function handles generic/core properties and calls out to
  367. * driver's ->atomic_set_property() for driver properties. To ensure
  368. * consistent behavior you must call this function rather than the
  369. * driver hook directly.
  370. *
  371. * RETURNS:
  372. * Zero on success, error code on failure
  373. */
  374. int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
  375. struct drm_crtc_state *state, struct drm_property *property,
  376. uint64_t val)
  377. {
  378. struct drm_device *dev = crtc->dev;
  379. struct drm_mode_config *config = &dev->mode_config;
  380. bool replaced = false;
  381. int ret;
  382. if (property == config->prop_active)
  383. state->active = val;
  384. else if (property == config->prop_mode_id) {
  385. struct drm_property_blob *mode =
  386. drm_property_lookup_blob(dev, val);
  387. ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
  388. drm_property_unreference_blob(mode);
  389. return ret;
  390. } else if (property == config->degamma_lut_property) {
  391. ret = drm_atomic_replace_property_blob_from_id(crtc,
  392. &state->degamma_lut,
  393. val,
  394. -1,
  395. &replaced);
  396. state->color_mgmt_changed = replaced;
  397. return ret;
  398. } else if (property == config->ctm_property) {
  399. ret = drm_atomic_replace_property_blob_from_id(crtc,
  400. &state->ctm,
  401. val,
  402. sizeof(struct drm_color_ctm),
  403. &replaced);
  404. state->color_mgmt_changed = replaced;
  405. return ret;
  406. } else if (property == config->gamma_lut_property) {
  407. ret = drm_atomic_replace_property_blob_from_id(crtc,
  408. &state->gamma_lut,
  409. val,
  410. -1,
  411. &replaced);
  412. state->color_mgmt_changed = replaced;
  413. return ret;
  414. } else if (crtc->funcs->atomic_set_property)
  415. return crtc->funcs->atomic_set_property(crtc, state, property, val);
  416. else
  417. return -EINVAL;
  418. return 0;
  419. }
  420. EXPORT_SYMBOL(drm_atomic_crtc_set_property);
  421. /**
  422. * drm_atomic_crtc_get_property - get property value from CRTC state
  423. * @crtc: the drm CRTC to set a property on
  424. * @state: the state object to get the property value from
  425. * @property: the property to set
  426. * @val: return location for the property value
  427. *
  428. * This function handles generic/core properties and calls out to
  429. * driver's ->atomic_get_property() for driver properties. To ensure
  430. * consistent behavior you must call this function rather than the
  431. * driver hook directly.
  432. *
  433. * RETURNS:
  434. * Zero on success, error code on failure
  435. */
  436. static int
  437. drm_atomic_crtc_get_property(struct drm_crtc *crtc,
  438. const struct drm_crtc_state *state,
  439. struct drm_property *property, uint64_t *val)
  440. {
  441. struct drm_device *dev = crtc->dev;
  442. struct drm_mode_config *config = &dev->mode_config;
  443. if (property == config->prop_active)
  444. *val = state->active;
  445. else if (property == config->prop_mode_id)
  446. *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
  447. else if (property == config->degamma_lut_property)
  448. *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
  449. else if (property == config->ctm_property)
  450. *val = (state->ctm) ? state->ctm->base.id : 0;
  451. else if (property == config->gamma_lut_property)
  452. *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
  453. else if (crtc->funcs->atomic_get_property)
  454. return crtc->funcs->atomic_get_property(crtc, state, property, val);
  455. else
  456. return -EINVAL;
  457. return 0;
  458. }
  459. /**
  460. * drm_atomic_crtc_check - check crtc state
  461. * @crtc: crtc to check
  462. * @state: crtc state to check
  463. *
  464. * Provides core sanity checks for crtc state.
  465. *
  466. * RETURNS:
  467. * Zero on success, error code on failure
  468. */
  469. static int drm_atomic_crtc_check(struct drm_crtc *crtc,
  470. struct drm_crtc_state *state)
  471. {
  472. /* NOTE: we explicitly don't enforce constraints such as primary
  473. * layer covering entire screen, since that is something we want
  474. * to allow (on hw that supports it). For hw that does not, it
  475. * should be checked in driver's crtc->atomic_check() vfunc.
  476. *
  477. * TODO: Add generic modeset state checks once we support those.
  478. */
  479. if (state->active && !state->enable) {
  480. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
  481. crtc->base.id, crtc->name);
  482. return -EINVAL;
  483. }
  484. /* The state->enable vs. state->mode_blob checks can be WARN_ON,
  485. * as this is a kernel-internal detail that userspace should never
  486. * be able to trigger. */
  487. if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
  488. WARN_ON(state->enable && !state->mode_blob)) {
  489. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
  490. crtc->base.id, crtc->name);
  491. return -EINVAL;
  492. }
  493. if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
  494. WARN_ON(!state->enable && state->mode_blob)) {
  495. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
  496. crtc->base.id, crtc->name);
  497. return -EINVAL;
  498. }
  499. /*
  500. * Reject event generation for when a CRTC is off and stays off.
  501. * It wouldn't be hard to implement this, but userspace has a track
  502. * record of happily burning through 100% cpu (or worse, crash) when the
  503. * display pipe is suspended. To avoid all that fun just reject updates
  504. * that ask for events since likely that indicates a bug in the
  505. * compositor's drawing loop. This is consistent with the vblank IOCTL
  506. * and legacy page_flip IOCTL which also reject service on a disabled
  507. * pipe.
  508. */
  509. if (state->event && !state->active && !crtc->state->active) {
  510. DRM_DEBUG_ATOMIC("[CRTC:%d] requesting event but off\n",
  511. crtc->base.id);
  512. return -EINVAL;
  513. }
  514. return 0;
  515. }
  516. /**
  517. * drm_atomic_get_plane_state - get plane state
  518. * @state: global atomic state object
  519. * @plane: plane to get state object for
  520. *
  521. * This function returns the plane state for the given plane, allocating it if
  522. * needed. It will also grab the relevant plane lock to make sure that the state
  523. * is consistent.
  524. *
  525. * Returns:
  526. *
  527. * Either the allocated state or the error code encoded into the pointer. When
  528. * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
  529. * entire atomic sequence must be restarted. All other errors are fatal.
  530. */
  531. struct drm_plane_state *
  532. drm_atomic_get_plane_state(struct drm_atomic_state *state,
  533. struct drm_plane *plane)
  534. {
  535. int ret, index = drm_plane_index(plane);
  536. struct drm_plane_state *plane_state;
  537. WARN_ON(!state->acquire_ctx);
  538. plane_state = drm_atomic_get_existing_plane_state(state, plane);
  539. if (plane_state)
  540. return plane_state;
  541. ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
  542. if (ret)
  543. return ERR_PTR(ret);
  544. plane_state = plane->funcs->atomic_duplicate_state(plane);
  545. if (!plane_state)
  546. return ERR_PTR(-ENOMEM);
  547. state->planes[index].state = plane_state;
  548. state->planes[index].ptr = plane;
  549. plane_state->state = state;
  550. DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
  551. plane->base.id, plane->name, plane_state, state);
  552. if (plane_state->crtc) {
  553. struct drm_crtc_state *crtc_state;
  554. crtc_state = drm_atomic_get_crtc_state(state,
  555. plane_state->crtc);
  556. if (IS_ERR(crtc_state))
  557. return ERR_CAST(crtc_state);
  558. }
  559. return plane_state;
  560. }
  561. EXPORT_SYMBOL(drm_atomic_get_plane_state);
  562. /**
  563. * drm_atomic_plane_set_property - set property on plane
  564. * @plane: the drm plane to set a property on
  565. * @state: the state object to update with the new property value
  566. * @property: the property to set
  567. * @val: the new property value
  568. *
  569. * Use this instead of calling plane->atomic_set_property directly.
  570. * This function handles generic/core properties and calls out to
  571. * driver's ->atomic_set_property() for driver properties. To ensure
  572. * consistent behavior you must call this function rather than the
  573. * driver hook directly.
  574. *
  575. * RETURNS:
  576. * Zero on success, error code on failure
  577. */
  578. int drm_atomic_plane_set_property(struct drm_plane *plane,
  579. struct drm_plane_state *state, struct drm_property *property,
  580. uint64_t val)
  581. {
  582. struct drm_device *dev = plane->dev;
  583. struct drm_mode_config *config = &dev->mode_config;
  584. if (property == config->prop_fb_id) {
  585. struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
  586. drm_atomic_set_fb_for_plane(state, fb);
  587. if (fb)
  588. drm_framebuffer_unreference(fb);
  589. } else if (property == config->prop_crtc_id) {
  590. struct drm_crtc *crtc = drm_crtc_find(dev, val);
  591. return drm_atomic_set_crtc_for_plane(state, crtc);
  592. } else if (property == config->prop_crtc_x) {
  593. state->crtc_x = U642I64(val);
  594. } else if (property == config->prop_crtc_y) {
  595. state->crtc_y = U642I64(val);
  596. } else if (property == config->prop_crtc_w) {
  597. state->crtc_w = val;
  598. } else if (property == config->prop_crtc_h) {
  599. state->crtc_h = val;
  600. } else if (property == config->prop_src_x) {
  601. state->src_x = val;
  602. } else if (property == config->prop_src_y) {
  603. state->src_y = val;
  604. } else if (property == config->prop_src_w) {
  605. state->src_w = val;
  606. } else if (property == config->prop_src_h) {
  607. state->src_h = val;
  608. } else if (property == config->rotation_property) {
  609. state->rotation = val;
  610. } else if (plane->funcs->atomic_set_property) {
  611. return plane->funcs->atomic_set_property(plane, state,
  612. property, val);
  613. } else {
  614. return -EINVAL;
  615. }
  616. return 0;
  617. }
  618. EXPORT_SYMBOL(drm_atomic_plane_set_property);
  619. /**
  620. * drm_atomic_plane_get_property - get property value from plane state
  621. * @plane: the drm plane to set a property on
  622. * @state: the state object to get the property value from
  623. * @property: the property to set
  624. * @val: return location for the property value
  625. *
  626. * This function handles generic/core properties and calls out to
  627. * driver's ->atomic_get_property() for driver properties. To ensure
  628. * consistent behavior you must call this function rather than the
  629. * driver hook directly.
  630. *
  631. * RETURNS:
  632. * Zero on success, error code on failure
  633. */
  634. static int
  635. drm_atomic_plane_get_property(struct drm_plane *plane,
  636. const struct drm_plane_state *state,
  637. struct drm_property *property, uint64_t *val)
  638. {
  639. struct drm_device *dev = plane->dev;
  640. struct drm_mode_config *config = &dev->mode_config;
  641. if (property == config->prop_fb_id) {
  642. *val = (state->fb) ? state->fb->base.id : 0;
  643. } else if (property == config->prop_crtc_id) {
  644. *val = (state->crtc) ? state->crtc->base.id : 0;
  645. } else if (property == config->prop_crtc_x) {
  646. *val = I642U64(state->crtc_x);
  647. } else if (property == config->prop_crtc_y) {
  648. *val = I642U64(state->crtc_y);
  649. } else if (property == config->prop_crtc_w) {
  650. *val = state->crtc_w;
  651. } else if (property == config->prop_crtc_h) {
  652. *val = state->crtc_h;
  653. } else if (property == config->prop_src_x) {
  654. *val = state->src_x;
  655. } else if (property == config->prop_src_y) {
  656. *val = state->src_y;
  657. } else if (property == config->prop_src_w) {
  658. *val = state->src_w;
  659. } else if (property == config->prop_src_h) {
  660. *val = state->src_h;
  661. } else if (property == config->rotation_property) {
  662. *val = state->rotation;
  663. } else if (plane->funcs->atomic_get_property) {
  664. return plane->funcs->atomic_get_property(plane, state, property, val);
  665. } else {
  666. return -EINVAL;
  667. }
  668. return 0;
  669. }
  670. static bool
  671. plane_switching_crtc(struct drm_atomic_state *state,
  672. struct drm_plane *plane,
  673. struct drm_plane_state *plane_state)
  674. {
  675. if (!plane->state->crtc || !plane_state->crtc)
  676. return false;
  677. if (plane->state->crtc == plane_state->crtc)
  678. return false;
  679. /* This could be refined, but currently there's no helper or driver code
  680. * to implement direct switching of active planes nor userspace to take
  681. * advantage of more direct plane switching without the intermediate
  682. * full OFF state.
  683. */
  684. return true;
  685. }
  686. /**
  687. * drm_atomic_plane_check - check plane state
  688. * @plane: plane to check
  689. * @state: plane state to check
  690. *
  691. * Provides core sanity checks for plane state.
  692. *
  693. * RETURNS:
  694. * Zero on success, error code on failure
  695. */
  696. static int drm_atomic_plane_check(struct drm_plane *plane,
  697. struct drm_plane_state *state)
  698. {
  699. unsigned int fb_width, fb_height;
  700. int ret;
  701. /* either *both* CRTC and FB must be set, or neither */
  702. if (WARN_ON(state->crtc && !state->fb)) {
  703. DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
  704. return -EINVAL;
  705. } else if (WARN_ON(state->fb && !state->crtc)) {
  706. DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
  707. return -EINVAL;
  708. }
  709. /* if disabled, we don't care about the rest of the state: */
  710. if (!state->crtc)
  711. return 0;
  712. /* Check whether this plane is usable on this CRTC */
  713. if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
  714. DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
  715. return -EINVAL;
  716. }
  717. /* Check whether this plane supports the fb pixel format. */
  718. ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format);
  719. if (ret) {
  720. DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
  721. drm_get_format_name(state->fb->pixel_format));
  722. return ret;
  723. }
  724. /* Give drivers some help against integer overflows */
  725. if (state->crtc_w > INT_MAX ||
  726. state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
  727. state->crtc_h > INT_MAX ||
  728. state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
  729. DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
  730. state->crtc_w, state->crtc_h,
  731. state->crtc_x, state->crtc_y);
  732. return -ERANGE;
  733. }
  734. fb_width = state->fb->width << 16;
  735. fb_height = state->fb->height << 16;
  736. /* Make sure source coordinates are inside the fb. */
  737. if (state->src_w > fb_width ||
  738. state->src_x > fb_width - state->src_w ||
  739. state->src_h > fb_height ||
  740. state->src_y > fb_height - state->src_h) {
  741. DRM_DEBUG_ATOMIC("Invalid source coordinates "
  742. "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
  743. state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
  744. state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
  745. state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
  746. state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
  747. return -ENOSPC;
  748. }
  749. if (plane_switching_crtc(state->state, plane, state)) {
  750. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
  751. plane->base.id, plane->name);
  752. return -EINVAL;
  753. }
  754. return 0;
  755. }
  756. /**
  757. * drm_atomic_get_connector_state - get connector state
  758. * @state: global atomic state object
  759. * @connector: connector to get state object for
  760. *
  761. * This function returns the connector state for the given connector,
  762. * allocating it if needed. It will also grab the relevant connector lock to
  763. * make sure that the state is consistent.
  764. *
  765. * Returns:
  766. *
  767. * Either the allocated state or the error code encoded into the pointer. When
  768. * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
  769. * entire atomic sequence must be restarted. All other errors are fatal.
  770. */
  771. struct drm_connector_state *
  772. drm_atomic_get_connector_state(struct drm_atomic_state *state,
  773. struct drm_connector *connector)
  774. {
  775. int ret, index;
  776. struct drm_mode_config *config = &connector->dev->mode_config;
  777. struct drm_connector_state *connector_state;
  778. WARN_ON(!state->acquire_ctx);
  779. ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
  780. if (ret)
  781. return ERR_PTR(ret);
  782. index = drm_connector_index(connector);
  783. if (index >= state->num_connector) {
  784. struct __drm_connnectors_state *c;
  785. int alloc = max(index + 1, config->num_connector);
  786. c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL);
  787. if (!c)
  788. return ERR_PTR(-ENOMEM);
  789. state->connectors = c;
  790. memset(&state->connectors[state->num_connector], 0,
  791. sizeof(*state->connectors) * (alloc - state->num_connector));
  792. state->num_connector = alloc;
  793. }
  794. if (state->connectors[index].state)
  795. return state->connectors[index].state;
  796. connector_state = connector->funcs->atomic_duplicate_state(connector);
  797. if (!connector_state)
  798. return ERR_PTR(-ENOMEM);
  799. drm_connector_reference(connector);
  800. state->connectors[index].state = connector_state;
  801. state->connectors[index].ptr = connector;
  802. connector_state->state = state;
  803. DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
  804. connector->base.id, connector_state, state);
  805. if (connector_state->crtc) {
  806. struct drm_crtc_state *crtc_state;
  807. crtc_state = drm_atomic_get_crtc_state(state,
  808. connector_state->crtc);
  809. if (IS_ERR(crtc_state))
  810. return ERR_CAST(crtc_state);
  811. }
  812. return connector_state;
  813. }
  814. EXPORT_SYMBOL(drm_atomic_get_connector_state);
  815. /**
  816. * drm_atomic_connector_set_property - set property on connector.
  817. * @connector: the drm connector to set a property on
  818. * @state: the state object to update with the new property value
  819. * @property: the property to set
  820. * @val: the new property value
  821. *
  822. * Use this instead of calling connector->atomic_set_property directly.
  823. * This function handles generic/core properties and calls out to
  824. * driver's ->atomic_set_property() for driver properties. To ensure
  825. * consistent behavior you must call this function rather than the
  826. * driver hook directly.
  827. *
  828. * RETURNS:
  829. * Zero on success, error code on failure
  830. */
  831. int drm_atomic_connector_set_property(struct drm_connector *connector,
  832. struct drm_connector_state *state, struct drm_property *property,
  833. uint64_t val)
  834. {
  835. struct drm_device *dev = connector->dev;
  836. struct drm_mode_config *config = &dev->mode_config;
  837. if (property == config->prop_crtc_id) {
  838. struct drm_crtc *crtc = drm_crtc_find(dev, val);
  839. return drm_atomic_set_crtc_for_connector(state, crtc);
  840. } else if (property == config->dpms_property) {
  841. /* setting DPMS property requires special handling, which
  842. * is done in legacy setprop path for us. Disallow (for
  843. * now?) atomic writes to DPMS property:
  844. */
  845. return -EINVAL;
  846. } else if (connector->funcs->atomic_set_property) {
  847. return connector->funcs->atomic_set_property(connector,
  848. state, property, val);
  849. } else {
  850. return -EINVAL;
  851. }
  852. }
  853. EXPORT_SYMBOL(drm_atomic_connector_set_property);
  854. /**
  855. * drm_atomic_connector_get_property - get property value from connector state
  856. * @connector: the drm connector to set a property on
  857. * @state: the state object to get the property value from
  858. * @property: the property to set
  859. * @val: return location for the property value
  860. *
  861. * This function handles generic/core properties and calls out to
  862. * driver's ->atomic_get_property() for driver properties. To ensure
  863. * consistent behavior you must call this function rather than the
  864. * driver hook directly.
  865. *
  866. * RETURNS:
  867. * Zero on success, error code on failure
  868. */
  869. static int
  870. drm_atomic_connector_get_property(struct drm_connector *connector,
  871. const struct drm_connector_state *state,
  872. struct drm_property *property, uint64_t *val)
  873. {
  874. struct drm_device *dev = connector->dev;
  875. struct drm_mode_config *config = &dev->mode_config;
  876. if (property == config->prop_crtc_id) {
  877. *val = (state->crtc) ? state->crtc->base.id : 0;
  878. } else if (property == config->dpms_property) {
  879. *val = connector->dpms;
  880. } else if (connector->funcs->atomic_get_property) {
  881. return connector->funcs->atomic_get_property(connector,
  882. state, property, val);
  883. } else {
  884. return -EINVAL;
  885. }
  886. return 0;
  887. }
  888. int drm_atomic_get_property(struct drm_mode_object *obj,
  889. struct drm_property *property, uint64_t *val)
  890. {
  891. struct drm_device *dev = property->dev;
  892. int ret;
  893. switch (obj->type) {
  894. case DRM_MODE_OBJECT_CONNECTOR: {
  895. struct drm_connector *connector = obj_to_connector(obj);
  896. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  897. ret = drm_atomic_connector_get_property(connector,
  898. connector->state, property, val);
  899. break;
  900. }
  901. case DRM_MODE_OBJECT_CRTC: {
  902. struct drm_crtc *crtc = obj_to_crtc(obj);
  903. WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
  904. ret = drm_atomic_crtc_get_property(crtc,
  905. crtc->state, property, val);
  906. break;
  907. }
  908. case DRM_MODE_OBJECT_PLANE: {
  909. struct drm_plane *plane = obj_to_plane(obj);
  910. WARN_ON(!drm_modeset_is_locked(&plane->mutex));
  911. ret = drm_atomic_plane_get_property(plane,
  912. plane->state, property, val);
  913. break;
  914. }
  915. default:
  916. ret = -EINVAL;
  917. break;
  918. }
  919. return ret;
  920. }
  921. /**
  922. * drm_atomic_set_crtc_for_plane - set crtc for plane
  923. * @plane_state: the plane whose incoming state to update
  924. * @crtc: crtc to use for the plane
  925. *
  926. * Changing the assigned crtc for a plane requires us to grab the lock and state
  927. * for the new crtc, as needed. This function takes care of all these details
  928. * besides updating the pointer in the state object itself.
  929. *
  930. * Returns:
  931. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  932. * then the w/w mutex code has detected a deadlock and the entire atomic
  933. * sequence must be restarted. All other errors are fatal.
  934. */
  935. int
  936. drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
  937. struct drm_crtc *crtc)
  938. {
  939. struct drm_plane *plane = plane_state->plane;
  940. struct drm_crtc_state *crtc_state;
  941. if (plane_state->crtc) {
  942. crtc_state = drm_atomic_get_crtc_state(plane_state->state,
  943. plane_state->crtc);
  944. if (WARN_ON(IS_ERR(crtc_state)))
  945. return PTR_ERR(crtc_state);
  946. crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
  947. }
  948. plane_state->crtc = crtc;
  949. if (crtc) {
  950. crtc_state = drm_atomic_get_crtc_state(plane_state->state,
  951. crtc);
  952. if (IS_ERR(crtc_state))
  953. return PTR_ERR(crtc_state);
  954. crtc_state->plane_mask |= (1 << drm_plane_index(plane));
  955. }
  956. if (crtc)
  957. DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n",
  958. plane_state, crtc->base.id, crtc->name);
  959. else
  960. DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
  961. plane_state);
  962. return 0;
  963. }
  964. EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
  965. /**
  966. * drm_atomic_set_fb_for_plane - set framebuffer for plane
  967. * @plane_state: atomic state object for the plane
  968. * @fb: fb to use for the plane
  969. *
  970. * Changing the assigned framebuffer for a plane requires us to grab a reference
  971. * to the new fb and drop the reference to the old fb, if there is one. This
  972. * function takes care of all these details besides updating the pointer in the
  973. * state object itself.
  974. */
  975. void
  976. drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
  977. struct drm_framebuffer *fb)
  978. {
  979. if (plane_state->fb)
  980. drm_framebuffer_unreference(plane_state->fb);
  981. if (fb)
  982. drm_framebuffer_reference(fb);
  983. plane_state->fb = fb;
  984. if (fb)
  985. DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
  986. fb->base.id, plane_state);
  987. else
  988. DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
  989. plane_state);
  990. }
  991. EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
  992. /**
  993. * drm_atomic_set_crtc_for_connector - set crtc for connector
  994. * @conn_state: atomic state object for the connector
  995. * @crtc: crtc to use for the connector
  996. *
  997. * Changing the assigned crtc for a connector requires us to grab the lock and
  998. * state for the new crtc, as needed. This function takes care of all these
  999. * details besides updating the pointer in the state object itself.
  1000. *
  1001. * Returns:
  1002. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  1003. * then the w/w mutex code has detected a deadlock and the entire atomic
  1004. * sequence must be restarted. All other errors are fatal.
  1005. */
  1006. int
  1007. drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
  1008. struct drm_crtc *crtc)
  1009. {
  1010. struct drm_crtc_state *crtc_state;
  1011. if (conn_state->crtc == crtc)
  1012. return 0;
  1013. if (conn_state->crtc) {
  1014. crtc_state = drm_atomic_get_existing_crtc_state(conn_state->state,
  1015. conn_state->crtc);
  1016. crtc_state->connector_mask &=
  1017. ~(1 << drm_connector_index(conn_state->connector));
  1018. drm_connector_unreference(conn_state->connector);
  1019. conn_state->crtc = NULL;
  1020. }
  1021. if (crtc) {
  1022. crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
  1023. if (IS_ERR(crtc_state))
  1024. return PTR_ERR(crtc_state);
  1025. crtc_state->connector_mask |=
  1026. 1 << drm_connector_index(conn_state->connector);
  1027. drm_connector_reference(conn_state->connector);
  1028. conn_state->crtc = crtc;
  1029. DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n",
  1030. conn_state, crtc->base.id, crtc->name);
  1031. } else {
  1032. DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
  1033. conn_state);
  1034. }
  1035. return 0;
  1036. }
  1037. EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
  1038. /**
  1039. * drm_atomic_add_affected_connectors - add connectors for crtc
  1040. * @state: atomic state
  1041. * @crtc: DRM crtc
  1042. *
  1043. * This function walks the current configuration and adds all connectors
  1044. * currently using @crtc to the atomic configuration @state. Note that this
  1045. * function must acquire the connection mutex. This can potentially cause
  1046. * unneeded seralization if the update is just for the planes on one crtc. Hence
  1047. * drivers and helpers should only call this when really needed (e.g. when a
  1048. * full modeset needs to happen due to some change).
  1049. *
  1050. * Returns:
  1051. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  1052. * then the w/w mutex code has detected a deadlock and the entire atomic
  1053. * sequence must be restarted. All other errors are fatal.
  1054. */
  1055. int
  1056. drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
  1057. struct drm_crtc *crtc)
  1058. {
  1059. struct drm_mode_config *config = &state->dev->mode_config;
  1060. struct drm_connector *connector;
  1061. struct drm_connector_state *conn_state;
  1062. int ret;
  1063. ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
  1064. if (ret)
  1065. return ret;
  1066. DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
  1067. crtc->base.id, crtc->name, state);
  1068. /*
  1069. * Changed connectors are already in @state, so only need to look at the
  1070. * current configuration.
  1071. */
  1072. drm_for_each_connector(connector, state->dev) {
  1073. if (connector->state->crtc != crtc)
  1074. continue;
  1075. conn_state = drm_atomic_get_connector_state(state, connector);
  1076. if (IS_ERR(conn_state))
  1077. return PTR_ERR(conn_state);
  1078. }
  1079. return 0;
  1080. }
  1081. EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
  1082. /**
  1083. * drm_atomic_add_affected_planes - add planes for crtc
  1084. * @state: atomic state
  1085. * @crtc: DRM crtc
  1086. *
  1087. * This function walks the current configuration and adds all planes
  1088. * currently used by @crtc to the atomic configuration @state. This is useful
  1089. * when an atomic commit also needs to check all currently enabled plane on
  1090. * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
  1091. * to avoid special code to force-enable all planes.
  1092. *
  1093. * Since acquiring a plane state will always also acquire the w/w mutex of the
  1094. * current CRTC for that plane (if there is any) adding all the plane states for
  1095. * a CRTC will not reduce parallism of atomic updates.
  1096. *
  1097. * Returns:
  1098. * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
  1099. * then the w/w mutex code has detected a deadlock and the entire atomic
  1100. * sequence must be restarted. All other errors are fatal.
  1101. */
  1102. int
  1103. drm_atomic_add_affected_planes(struct drm_atomic_state *state,
  1104. struct drm_crtc *crtc)
  1105. {
  1106. struct drm_plane *plane;
  1107. WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
  1108. drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
  1109. struct drm_plane_state *plane_state =
  1110. drm_atomic_get_plane_state(state, plane);
  1111. if (IS_ERR(plane_state))
  1112. return PTR_ERR(plane_state);
  1113. }
  1114. return 0;
  1115. }
  1116. EXPORT_SYMBOL(drm_atomic_add_affected_planes);
  1117. /**
  1118. * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
  1119. * @state: atomic state
  1120. *
  1121. * This function should be used by legacy entry points which don't understand
  1122. * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
  1123. * the slowpath completed.
  1124. */
  1125. void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
  1126. {
  1127. int ret;
  1128. retry:
  1129. drm_modeset_backoff(state->acquire_ctx);
  1130. ret = drm_modeset_lock_all_ctx(state->dev, state->acquire_ctx);
  1131. if (ret)
  1132. goto retry;
  1133. }
  1134. EXPORT_SYMBOL(drm_atomic_legacy_backoff);
  1135. /**
  1136. * drm_atomic_check_only - check whether a given config would work
  1137. * @state: atomic configuration to check
  1138. *
  1139. * Note that this function can return -EDEADLK if the driver needed to acquire
  1140. * more locks but encountered a deadlock. The caller must then do the usual w/w
  1141. * backoff dance and restart. All other errors are fatal.
  1142. *
  1143. * Returns:
  1144. * 0 on success, negative error code on failure.
  1145. */
  1146. int drm_atomic_check_only(struct drm_atomic_state *state)
  1147. {
  1148. struct drm_device *dev = state->dev;
  1149. struct drm_mode_config *config = &dev->mode_config;
  1150. struct drm_plane *plane;
  1151. struct drm_plane_state *plane_state;
  1152. struct drm_crtc *crtc;
  1153. struct drm_crtc_state *crtc_state;
  1154. int i, ret = 0;
  1155. DRM_DEBUG_ATOMIC("checking %p\n", state);
  1156. for_each_plane_in_state(state, plane, plane_state, i) {
  1157. ret = drm_atomic_plane_check(plane, plane_state);
  1158. if (ret) {
  1159. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
  1160. plane->base.id, plane->name);
  1161. return ret;
  1162. }
  1163. }
  1164. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  1165. ret = drm_atomic_crtc_check(crtc, crtc_state);
  1166. if (ret) {
  1167. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
  1168. crtc->base.id, crtc->name);
  1169. return ret;
  1170. }
  1171. }
  1172. if (config->funcs->atomic_check)
  1173. ret = config->funcs->atomic_check(state->dev, state);
  1174. if (!state->allow_modeset) {
  1175. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  1176. if (drm_atomic_crtc_needs_modeset(crtc_state)) {
  1177. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
  1178. crtc->base.id, crtc->name);
  1179. return -EINVAL;
  1180. }
  1181. }
  1182. }
  1183. return ret;
  1184. }
  1185. EXPORT_SYMBOL(drm_atomic_check_only);
  1186. /**
  1187. * drm_atomic_commit - commit configuration atomically
  1188. * @state: atomic configuration to check
  1189. *
  1190. * Note that this function can return -EDEADLK if the driver needed to acquire
  1191. * more locks but encountered a deadlock. The caller must then do the usual w/w
  1192. * backoff dance and restart. All other errors are fatal.
  1193. *
  1194. * Also note that on successful execution ownership of @state is transferred
  1195. * from the caller of this function to the function itself. The caller must not
  1196. * free or in any other way access @state. If the function fails then the caller
  1197. * must clean up @state itself.
  1198. *
  1199. * Returns:
  1200. * 0 on success, negative error code on failure.
  1201. */
  1202. int drm_atomic_commit(struct drm_atomic_state *state)
  1203. {
  1204. struct drm_mode_config *config = &state->dev->mode_config;
  1205. int ret;
  1206. ret = drm_atomic_check_only(state);
  1207. if (ret)
  1208. return ret;
  1209. DRM_DEBUG_ATOMIC("commiting %p\n", state);
  1210. return config->funcs->atomic_commit(state->dev, state, false);
  1211. }
  1212. EXPORT_SYMBOL(drm_atomic_commit);
  1213. /**
  1214. * drm_atomic_nonblocking_commit - atomic&nonblocking configuration commit
  1215. * @state: atomic configuration to check
  1216. *
  1217. * Note that this function can return -EDEADLK if the driver needed to acquire
  1218. * more locks but encountered a deadlock. The caller must then do the usual w/w
  1219. * backoff dance and restart. All other errors are fatal.
  1220. *
  1221. * Also note that on successful execution ownership of @state is transferred
  1222. * from the caller of this function to the function itself. The caller must not
  1223. * free or in any other way access @state. If the function fails then the caller
  1224. * must clean up @state itself.
  1225. *
  1226. * Returns:
  1227. * 0 on success, negative error code on failure.
  1228. */
  1229. int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
  1230. {
  1231. struct drm_mode_config *config = &state->dev->mode_config;
  1232. int ret;
  1233. ret = drm_atomic_check_only(state);
  1234. if (ret)
  1235. return ret;
  1236. DRM_DEBUG_ATOMIC("commiting %p nonblocking\n", state);
  1237. return config->funcs->atomic_commit(state->dev, state, true);
  1238. }
  1239. EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
  1240. /*
  1241. * The big monstor ioctl
  1242. */
  1243. static struct drm_pending_vblank_event *create_vblank_event(
  1244. struct drm_device *dev, struct drm_file *file_priv,
  1245. struct fence *fence, uint64_t user_data)
  1246. {
  1247. struct drm_pending_vblank_event *e = NULL;
  1248. int ret;
  1249. e = kzalloc(sizeof *e, GFP_KERNEL);
  1250. if (!e)
  1251. return NULL;
  1252. e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
  1253. e->event.base.length = sizeof(e->event);
  1254. e->event.user_data = user_data;
  1255. if (file_priv) {
  1256. ret = drm_event_reserve_init(dev, file_priv, &e->base,
  1257. &e->event.base);
  1258. if (ret) {
  1259. kfree(e);
  1260. return NULL;
  1261. }
  1262. }
  1263. e->base.fence = fence;
  1264. return e;
  1265. }
  1266. static int atomic_set_prop(struct drm_atomic_state *state,
  1267. struct drm_mode_object *obj, struct drm_property *prop,
  1268. uint64_t prop_value)
  1269. {
  1270. struct drm_mode_object *ref;
  1271. int ret;
  1272. if (!drm_property_change_valid_get(prop, prop_value, &ref))
  1273. return -EINVAL;
  1274. switch (obj->type) {
  1275. case DRM_MODE_OBJECT_CONNECTOR: {
  1276. struct drm_connector *connector = obj_to_connector(obj);
  1277. struct drm_connector_state *connector_state;
  1278. connector_state = drm_atomic_get_connector_state(state, connector);
  1279. if (IS_ERR(connector_state)) {
  1280. ret = PTR_ERR(connector_state);
  1281. break;
  1282. }
  1283. ret = drm_atomic_connector_set_property(connector,
  1284. connector_state, prop, prop_value);
  1285. break;
  1286. }
  1287. case DRM_MODE_OBJECT_CRTC: {
  1288. struct drm_crtc *crtc = obj_to_crtc(obj);
  1289. struct drm_crtc_state *crtc_state;
  1290. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  1291. if (IS_ERR(crtc_state)) {
  1292. ret = PTR_ERR(crtc_state);
  1293. break;
  1294. }
  1295. ret = drm_atomic_crtc_set_property(crtc,
  1296. crtc_state, prop, prop_value);
  1297. break;
  1298. }
  1299. case DRM_MODE_OBJECT_PLANE: {
  1300. struct drm_plane *plane = obj_to_plane(obj);
  1301. struct drm_plane_state *plane_state;
  1302. plane_state = drm_atomic_get_plane_state(state, plane);
  1303. if (IS_ERR(plane_state)) {
  1304. ret = PTR_ERR(plane_state);
  1305. break;
  1306. }
  1307. ret = drm_atomic_plane_set_property(plane,
  1308. plane_state, prop, prop_value);
  1309. break;
  1310. }
  1311. default:
  1312. ret = -EINVAL;
  1313. break;
  1314. }
  1315. drm_property_change_valid_put(prop, ref);
  1316. return ret;
  1317. }
  1318. /**
  1319. * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers.
  1320. *
  1321. * @dev: drm device to check.
  1322. * @plane_mask: plane mask for planes that were updated.
  1323. * @ret: return value, can be -EDEADLK for a retry.
  1324. *
  1325. * Before doing an update plane->old_fb is set to plane->fb,
  1326. * but before dropping the locks old_fb needs to be set to NULL
  1327. * and plane->fb updated. This is a common operation for each
  1328. * atomic update, so this call is split off as a helper.
  1329. */
  1330. void drm_atomic_clean_old_fb(struct drm_device *dev,
  1331. unsigned plane_mask,
  1332. int ret)
  1333. {
  1334. struct drm_plane *plane;
  1335. /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
  1336. * locks (ie. while it is still safe to deref plane->state). We
  1337. * need to do this here because the driver entry points cannot
  1338. * distinguish between legacy and atomic ioctls.
  1339. */
  1340. drm_for_each_plane_mask(plane, dev, plane_mask) {
  1341. if (ret == 0) {
  1342. struct drm_framebuffer *new_fb = plane->state->fb;
  1343. if (new_fb)
  1344. drm_framebuffer_reference(new_fb);
  1345. plane->fb = new_fb;
  1346. plane->crtc = plane->state->crtc;
  1347. if (plane->old_fb)
  1348. drm_framebuffer_unreference(plane->old_fb);
  1349. }
  1350. plane->old_fb = NULL;
  1351. }
  1352. }
  1353. EXPORT_SYMBOL(drm_atomic_clean_old_fb);
  1354. int drm_mode_atomic_ioctl(struct drm_device *dev,
  1355. void *data, struct drm_file *file_priv)
  1356. {
  1357. struct drm_mode_atomic *arg = data;
  1358. uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
  1359. uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
  1360. uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
  1361. uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
  1362. unsigned int copied_objs, copied_props;
  1363. struct drm_atomic_state *state;
  1364. struct drm_modeset_acquire_ctx ctx;
  1365. struct drm_plane *plane;
  1366. struct drm_crtc *crtc;
  1367. struct drm_crtc_state *crtc_state;
  1368. unsigned plane_mask;
  1369. int ret = 0;
  1370. unsigned int i, j;
  1371. /* disallow for drivers not supporting atomic: */
  1372. if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
  1373. return -EINVAL;
  1374. /* disallow for userspace that has not enabled atomic cap (even
  1375. * though this may be a bit overkill, since legacy userspace
  1376. * wouldn't know how to call this ioctl)
  1377. */
  1378. if (!file_priv->atomic)
  1379. return -EINVAL;
  1380. if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
  1381. return -EINVAL;
  1382. if (arg->reserved)
  1383. return -EINVAL;
  1384. if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
  1385. !dev->mode_config.async_page_flip)
  1386. return -EINVAL;
  1387. /* can't test and expect an event at the same time. */
  1388. if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
  1389. (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
  1390. return -EINVAL;
  1391. drm_modeset_acquire_init(&ctx, 0);
  1392. state = drm_atomic_state_alloc(dev);
  1393. if (!state)
  1394. return -ENOMEM;
  1395. state->acquire_ctx = &ctx;
  1396. state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
  1397. retry:
  1398. plane_mask = 0;
  1399. copied_objs = 0;
  1400. copied_props = 0;
  1401. for (i = 0; i < arg->count_objs; i++) {
  1402. uint32_t obj_id, count_props;
  1403. struct drm_mode_object *obj;
  1404. if (get_user(obj_id, objs_ptr + copied_objs)) {
  1405. ret = -EFAULT;
  1406. goto out;
  1407. }
  1408. obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
  1409. if (!obj) {
  1410. ret = -ENOENT;
  1411. goto out;
  1412. }
  1413. if (!obj->properties) {
  1414. drm_mode_object_unreference(obj);
  1415. ret = -ENOENT;
  1416. goto out;
  1417. }
  1418. if (get_user(count_props, count_props_ptr + copied_objs)) {
  1419. drm_mode_object_unreference(obj);
  1420. ret = -EFAULT;
  1421. goto out;
  1422. }
  1423. copied_objs++;
  1424. for (j = 0; j < count_props; j++) {
  1425. uint32_t prop_id;
  1426. uint64_t prop_value;
  1427. struct drm_property *prop;
  1428. if (get_user(prop_id, props_ptr + copied_props)) {
  1429. drm_mode_object_unreference(obj);
  1430. ret = -EFAULT;
  1431. goto out;
  1432. }
  1433. prop = drm_property_find(dev, prop_id);
  1434. if (!prop) {
  1435. drm_mode_object_unreference(obj);
  1436. ret = -ENOENT;
  1437. goto out;
  1438. }
  1439. if (copy_from_user(&prop_value,
  1440. prop_values_ptr + copied_props,
  1441. sizeof(prop_value))) {
  1442. drm_mode_object_unreference(obj);
  1443. ret = -EFAULT;
  1444. goto out;
  1445. }
  1446. ret = atomic_set_prop(state, obj, prop, prop_value);
  1447. if (ret) {
  1448. drm_mode_object_unreference(obj);
  1449. goto out;
  1450. }
  1451. copied_props++;
  1452. }
  1453. if (obj->type == DRM_MODE_OBJECT_PLANE && count_props &&
  1454. !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
  1455. plane = obj_to_plane(obj);
  1456. plane_mask |= (1 << drm_plane_index(plane));
  1457. plane->old_fb = plane->fb;
  1458. }
  1459. drm_mode_object_unreference(obj);
  1460. }
  1461. if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  1462. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  1463. struct drm_pending_vblank_event *e;
  1464. e = create_vblank_event(dev, file_priv, NULL,
  1465. arg->user_data);
  1466. if (!e) {
  1467. ret = -ENOMEM;
  1468. goto out;
  1469. }
  1470. crtc_state->event = e;
  1471. }
  1472. }
  1473. if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
  1474. /*
  1475. * Unlike commit, check_only does not clean up state.
  1476. * Below we call drm_atomic_state_free for it.
  1477. */
  1478. ret = drm_atomic_check_only(state);
  1479. } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
  1480. ret = drm_atomic_nonblocking_commit(state);
  1481. } else {
  1482. ret = drm_atomic_commit(state);
  1483. }
  1484. out:
  1485. drm_atomic_clean_old_fb(dev, plane_mask, ret);
  1486. if (ret && arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  1487. /*
  1488. * TEST_ONLY and PAGE_FLIP_EVENT are mutually exclusive,
  1489. * if they weren't, this code should be called on success
  1490. * for TEST_ONLY too.
  1491. */
  1492. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  1493. if (!crtc_state->event)
  1494. continue;
  1495. drm_event_cancel_free(dev, &crtc_state->event->base);
  1496. }
  1497. }
  1498. if (ret == -EDEADLK) {
  1499. drm_atomic_state_clear(state);
  1500. drm_modeset_backoff(&ctx);
  1501. goto retry;
  1502. }
  1503. if (ret || arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
  1504. drm_atomic_state_free(state);
  1505. drm_modeset_drop_locks(&ctx);
  1506. drm_modeset_acquire_fini(&ctx);
  1507. return ret;
  1508. }