drm_atomic.c 46 KB

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