drm_atomic.c 50 KB

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