drm_atomic.c 50 KB

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