drm_atomic.c 51 KB

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