drm_atomic.c 46 KB

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