drm_atomic.c 39 KB

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