drm_crtc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. /*
  2. * Copyright (c) 2006-2008 Intel Corporation
  3. * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
  4. * Copyright (c) 2008 Red Hat Inc.
  5. *
  6. * DRM core CRTC related functions
  7. *
  8. * Permission to use, copy, modify, distribute, and sell this software and its
  9. * documentation for any purpose is hereby granted without fee, provided that
  10. * the above copyright notice appear in all copies and that both that copyright
  11. * notice and this permission notice appear in supporting documentation, and
  12. * that the name of the copyright holders not be used in advertising or
  13. * publicity pertaining to distribution of the software without specific,
  14. * written prior permission. The copyright holders make no representations
  15. * about the suitability of this software for any purpose. It is provided "as
  16. * is" without express or implied warranty.
  17. *
  18. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  19. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  20. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  21. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  22. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  23. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  24. * OF THIS SOFTWARE.
  25. *
  26. * Authors:
  27. * Keith Packard
  28. * Eric Anholt <eric@anholt.net>
  29. * Dave Airlie <airlied@linux.ie>
  30. * Jesse Barnes <jesse.barnes@intel.com>
  31. */
  32. #include <linux/ctype.h>
  33. #include <linux/list.h>
  34. #include <linux/slab.h>
  35. #include <linux/export.h>
  36. #include <linux/dma-fence.h>
  37. #include <linux/uaccess.h>
  38. #include <drm/drm_crtc.h>
  39. #include <drm/drm_edid.h>
  40. #include <drm/drm_fourcc.h>
  41. #include <drm/drm_modeset_lock.h>
  42. #include <drm/drm_atomic.h>
  43. #include <drm/drm_auth.h>
  44. #include <drm/drm_debugfs_crc.h>
  45. #include <drm/drm_drv.h>
  46. #include <drm/drm_print.h>
  47. #include <drm/drm_file.h>
  48. #include "drm_crtc_internal.h"
  49. #include "drm_internal.h"
  50. /**
  51. * DOC: overview
  52. *
  53. * A CRTC represents the overall display pipeline. It receives pixel data from
  54. * &drm_plane and blends them together. The &drm_display_mode is also attached
  55. * to the CRTC, specifying display timings. On the output side the data is fed
  56. * to one or more &drm_encoder, which are then each connected to one
  57. * &drm_connector.
  58. *
  59. * To create a CRTC, a KMS drivers allocates and zeroes an instances of
  60. * &struct drm_crtc (possibly as part of a larger structure) and registers it
  61. * with a call to drm_crtc_init_with_planes().
  62. *
  63. * The CRTC is also the entry point for legacy modeset operations, see
  64. * &drm_crtc_funcs.set_config, legacy plane operations, see
  65. * &drm_crtc_funcs.page_flip and &drm_crtc_funcs.cursor_set2, and other legacy
  66. * operations like &drm_crtc_funcs.gamma_set. For atomic drivers all these
  67. * features are controlled through &drm_property and
  68. * &drm_mode_config_funcs.atomic_check and &drm_mode_config_funcs.atomic_check.
  69. */
  70. /**
  71. * drm_crtc_from_index - find the registered CRTC at an index
  72. * @dev: DRM device
  73. * @idx: index of registered CRTC to find for
  74. *
  75. * Given a CRTC index, return the registered CRTC from DRM device's
  76. * list of CRTCs with matching index. This is the inverse of drm_crtc_index().
  77. * It's useful in the vblank callbacks (like &drm_driver.enable_vblank or
  78. * &drm_driver.disable_vblank), since that still deals with indices instead
  79. * of pointers to &struct drm_crtc."
  80. */
  81. struct drm_crtc *drm_crtc_from_index(struct drm_device *dev, int idx)
  82. {
  83. struct drm_crtc *crtc;
  84. drm_for_each_crtc(crtc, dev)
  85. if (idx == crtc->index)
  86. return crtc;
  87. return NULL;
  88. }
  89. EXPORT_SYMBOL(drm_crtc_from_index);
  90. /**
  91. * drm_crtc_force_disable - Forcibly turn off a CRTC
  92. * @crtc: CRTC to turn off
  93. *
  94. * Note: This should only be used by non-atomic legacy drivers.
  95. *
  96. * Returns:
  97. * Zero on success, error code on failure.
  98. */
  99. int drm_crtc_force_disable(struct drm_crtc *crtc)
  100. {
  101. struct drm_mode_set set = {
  102. .crtc = crtc,
  103. };
  104. WARN_ON(drm_drv_uses_atomic_modeset(crtc->dev));
  105. return drm_mode_set_config_internal(&set);
  106. }
  107. EXPORT_SYMBOL(drm_crtc_force_disable);
  108. /**
  109. * drm_crtc_force_disable_all - Forcibly turn off all enabled CRTCs
  110. * @dev: DRM device whose CRTCs to turn off
  111. *
  112. * Drivers may want to call this on unload to ensure that all displays are
  113. * unlit and the GPU is in a consistent, low power state. Takes modeset locks.
  114. *
  115. * Note: This should only be used by non-atomic legacy drivers. For an atomic
  116. * version look at drm_atomic_helper_shutdown().
  117. *
  118. * Returns:
  119. * Zero on success, error code on failure.
  120. */
  121. int drm_crtc_force_disable_all(struct drm_device *dev)
  122. {
  123. struct drm_crtc *crtc;
  124. int ret = 0;
  125. drm_modeset_lock_all(dev);
  126. drm_for_each_crtc(crtc, dev)
  127. if (crtc->enabled) {
  128. ret = drm_crtc_force_disable(crtc);
  129. if (ret)
  130. goto out;
  131. }
  132. out:
  133. drm_modeset_unlock_all(dev);
  134. return ret;
  135. }
  136. EXPORT_SYMBOL(drm_crtc_force_disable_all);
  137. static unsigned int drm_num_crtcs(struct drm_device *dev)
  138. {
  139. unsigned int num = 0;
  140. struct drm_crtc *tmp;
  141. drm_for_each_crtc(tmp, dev) {
  142. num++;
  143. }
  144. return num;
  145. }
  146. int drm_crtc_register_all(struct drm_device *dev)
  147. {
  148. struct drm_crtc *crtc;
  149. int ret = 0;
  150. drm_for_each_crtc(crtc, dev) {
  151. if (drm_debugfs_crtc_add(crtc))
  152. DRM_ERROR("Failed to initialize debugfs entry for CRTC '%s'.\n",
  153. crtc->name);
  154. if (crtc->funcs->late_register)
  155. ret = crtc->funcs->late_register(crtc);
  156. if (ret)
  157. return ret;
  158. }
  159. return 0;
  160. }
  161. void drm_crtc_unregister_all(struct drm_device *dev)
  162. {
  163. struct drm_crtc *crtc;
  164. drm_for_each_crtc(crtc, dev) {
  165. if (crtc->funcs->early_unregister)
  166. crtc->funcs->early_unregister(crtc);
  167. drm_debugfs_crtc_remove(crtc);
  168. }
  169. }
  170. static int drm_crtc_crc_init(struct drm_crtc *crtc)
  171. {
  172. #ifdef CONFIG_DEBUG_FS
  173. spin_lock_init(&crtc->crc.lock);
  174. init_waitqueue_head(&crtc->crc.wq);
  175. crtc->crc.source = kstrdup("auto", GFP_KERNEL);
  176. if (!crtc->crc.source)
  177. return -ENOMEM;
  178. #endif
  179. return 0;
  180. }
  181. static void drm_crtc_crc_fini(struct drm_crtc *crtc)
  182. {
  183. #ifdef CONFIG_DEBUG_FS
  184. kfree(crtc->crc.source);
  185. #endif
  186. }
  187. static const struct dma_fence_ops drm_crtc_fence_ops;
  188. static struct drm_crtc *fence_to_crtc(struct dma_fence *fence)
  189. {
  190. BUG_ON(fence->ops != &drm_crtc_fence_ops);
  191. return container_of(fence->lock, struct drm_crtc, fence_lock);
  192. }
  193. static const char *drm_crtc_fence_get_driver_name(struct dma_fence *fence)
  194. {
  195. struct drm_crtc *crtc = fence_to_crtc(fence);
  196. return crtc->dev->driver->name;
  197. }
  198. static const char *drm_crtc_fence_get_timeline_name(struct dma_fence *fence)
  199. {
  200. struct drm_crtc *crtc = fence_to_crtc(fence);
  201. return crtc->timeline_name;
  202. }
  203. static const struct dma_fence_ops drm_crtc_fence_ops = {
  204. .get_driver_name = drm_crtc_fence_get_driver_name,
  205. .get_timeline_name = drm_crtc_fence_get_timeline_name,
  206. };
  207. struct dma_fence *drm_crtc_create_fence(struct drm_crtc *crtc)
  208. {
  209. struct dma_fence *fence;
  210. fence = kzalloc(sizeof(*fence), GFP_KERNEL);
  211. if (!fence)
  212. return NULL;
  213. dma_fence_init(fence, &drm_crtc_fence_ops, &crtc->fence_lock,
  214. crtc->fence_context, ++crtc->fence_seqno);
  215. return fence;
  216. }
  217. /**
  218. * drm_crtc_init_with_planes - Initialise a new CRTC object with
  219. * specified primary and cursor planes.
  220. * @dev: DRM device
  221. * @crtc: CRTC object to init
  222. * @primary: Primary plane for CRTC
  223. * @cursor: Cursor plane for CRTC
  224. * @funcs: callbacks for the new CRTC
  225. * @name: printf style format string for the CRTC name, or NULL for default name
  226. *
  227. * Inits a new object created as base part of a driver crtc object. Drivers
  228. * should use this function instead of drm_crtc_init(), which is only provided
  229. * for backwards compatibility with drivers which do not yet support universal
  230. * planes). For really simple hardware which has only 1 plane look at
  231. * drm_simple_display_pipe_init() instead.
  232. *
  233. * Returns:
  234. * Zero on success, error code on failure.
  235. */
  236. int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
  237. struct drm_plane *primary,
  238. struct drm_plane *cursor,
  239. const struct drm_crtc_funcs *funcs,
  240. const char *name, ...)
  241. {
  242. struct drm_mode_config *config = &dev->mode_config;
  243. int ret;
  244. WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
  245. WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
  246. /* crtc index is used with 32bit bitmasks */
  247. if (WARN_ON(config->num_crtc >= 32))
  248. return -EINVAL;
  249. WARN_ON(drm_drv_uses_atomic_modeset(dev) &&
  250. (!funcs->atomic_destroy_state ||
  251. !funcs->atomic_duplicate_state));
  252. crtc->dev = dev;
  253. crtc->funcs = funcs;
  254. INIT_LIST_HEAD(&crtc->commit_list);
  255. spin_lock_init(&crtc->commit_lock);
  256. drm_modeset_lock_init(&crtc->mutex);
  257. ret = drm_mode_object_add(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
  258. if (ret)
  259. return ret;
  260. if (name) {
  261. va_list ap;
  262. va_start(ap, name);
  263. crtc->name = kvasprintf(GFP_KERNEL, name, ap);
  264. va_end(ap);
  265. } else {
  266. crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
  267. drm_num_crtcs(dev));
  268. }
  269. if (!crtc->name) {
  270. drm_mode_object_unregister(dev, &crtc->base);
  271. return -ENOMEM;
  272. }
  273. crtc->fence_context = dma_fence_context_alloc(1);
  274. spin_lock_init(&crtc->fence_lock);
  275. snprintf(crtc->timeline_name, sizeof(crtc->timeline_name),
  276. "CRTC:%d-%s", crtc->base.id, crtc->name);
  277. crtc->base.properties = &crtc->properties;
  278. list_add_tail(&crtc->head, &config->crtc_list);
  279. crtc->index = config->num_crtc++;
  280. crtc->primary = primary;
  281. crtc->cursor = cursor;
  282. if (primary && !primary->possible_crtcs)
  283. primary->possible_crtcs = drm_crtc_mask(crtc);
  284. if (cursor && !cursor->possible_crtcs)
  285. cursor->possible_crtcs = drm_crtc_mask(crtc);
  286. ret = drm_crtc_crc_init(crtc);
  287. if (ret) {
  288. drm_mode_object_unregister(dev, &crtc->base);
  289. return ret;
  290. }
  291. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  292. drm_object_attach_property(&crtc->base, config->prop_active, 0);
  293. drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
  294. drm_object_attach_property(&crtc->base,
  295. config->prop_out_fence_ptr, 0);
  296. }
  297. return 0;
  298. }
  299. EXPORT_SYMBOL(drm_crtc_init_with_planes);
  300. /**
  301. * drm_crtc_cleanup - Clean up the core crtc usage
  302. * @crtc: CRTC to cleanup
  303. *
  304. * This function cleans up @crtc and removes it from the DRM mode setting
  305. * core. Note that the function does *not* free the crtc structure itself,
  306. * this is the responsibility of the caller.
  307. */
  308. void drm_crtc_cleanup(struct drm_crtc *crtc)
  309. {
  310. struct drm_device *dev = crtc->dev;
  311. /* Note that the crtc_list is considered to be static; should we
  312. * remove the drm_crtc at runtime we would have to decrement all
  313. * the indices on the drm_crtc after us in the crtc_list.
  314. */
  315. drm_crtc_crc_fini(crtc);
  316. kfree(crtc->gamma_store);
  317. crtc->gamma_store = NULL;
  318. drm_modeset_lock_fini(&crtc->mutex);
  319. drm_mode_object_unregister(dev, &crtc->base);
  320. list_del(&crtc->head);
  321. dev->mode_config.num_crtc--;
  322. WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
  323. if (crtc->state && crtc->funcs->atomic_destroy_state)
  324. crtc->funcs->atomic_destroy_state(crtc, crtc->state);
  325. kfree(crtc->name);
  326. memset(crtc, 0, sizeof(*crtc));
  327. }
  328. EXPORT_SYMBOL(drm_crtc_cleanup);
  329. /**
  330. * drm_mode_getcrtc - get CRTC configuration
  331. * @dev: drm device for the ioctl
  332. * @data: data pointer for the ioctl
  333. * @file_priv: drm file for the ioctl call
  334. *
  335. * Construct a CRTC configuration structure to return to the user.
  336. *
  337. * Called by the user via ioctl.
  338. *
  339. * Returns:
  340. * Zero on success, negative errno on failure.
  341. */
  342. int drm_mode_getcrtc(struct drm_device *dev,
  343. void *data, struct drm_file *file_priv)
  344. {
  345. struct drm_mode_crtc *crtc_resp = data;
  346. struct drm_crtc *crtc;
  347. struct drm_plane *plane;
  348. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  349. return -EOPNOTSUPP;
  350. crtc = drm_crtc_find(dev, file_priv, crtc_resp->crtc_id);
  351. if (!crtc)
  352. return -ENOENT;
  353. plane = crtc->primary;
  354. crtc_resp->gamma_size = crtc->gamma_size;
  355. drm_modeset_lock(&plane->mutex, NULL);
  356. if (plane->state && plane->state->fb)
  357. crtc_resp->fb_id = plane->state->fb->base.id;
  358. else if (!plane->state && plane->fb)
  359. crtc_resp->fb_id = plane->fb->base.id;
  360. else
  361. crtc_resp->fb_id = 0;
  362. if (plane->state) {
  363. crtc_resp->x = plane->state->src_x >> 16;
  364. crtc_resp->y = plane->state->src_y >> 16;
  365. }
  366. drm_modeset_unlock(&plane->mutex);
  367. drm_modeset_lock(&crtc->mutex, NULL);
  368. if (crtc->state) {
  369. if (crtc->state->enable) {
  370. drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
  371. crtc_resp->mode_valid = 1;
  372. } else {
  373. crtc_resp->mode_valid = 0;
  374. }
  375. } else {
  376. crtc_resp->x = crtc->x;
  377. crtc_resp->y = crtc->y;
  378. if (crtc->enabled) {
  379. drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
  380. crtc_resp->mode_valid = 1;
  381. } else {
  382. crtc_resp->mode_valid = 0;
  383. }
  384. }
  385. if (!file_priv->aspect_ratio_allowed)
  386. crtc_resp->mode.flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
  387. drm_modeset_unlock(&crtc->mutex);
  388. return 0;
  389. }
  390. static int __drm_mode_set_config_internal(struct drm_mode_set *set,
  391. struct drm_modeset_acquire_ctx *ctx)
  392. {
  393. struct drm_crtc *crtc = set->crtc;
  394. struct drm_framebuffer *fb;
  395. struct drm_crtc *tmp;
  396. int ret;
  397. WARN_ON(drm_drv_uses_atomic_modeset(crtc->dev));
  398. /*
  399. * NOTE: ->set_config can also disable other crtcs (if we steal all
  400. * connectors from it), hence we need to refcount the fbs across all
  401. * crtcs. Atomic modeset will have saner semantics ...
  402. */
  403. drm_for_each_crtc(tmp, crtc->dev) {
  404. struct drm_plane *plane = tmp->primary;
  405. plane->old_fb = plane->fb;
  406. }
  407. fb = set->fb;
  408. ret = crtc->funcs->set_config(set, ctx);
  409. if (ret == 0) {
  410. struct drm_plane *plane = crtc->primary;
  411. plane->crtc = fb ? crtc : NULL;
  412. plane->fb = fb;
  413. }
  414. drm_for_each_crtc(tmp, crtc->dev) {
  415. struct drm_plane *plane = tmp->primary;
  416. if (plane->fb)
  417. drm_framebuffer_get(plane->fb);
  418. if (plane->old_fb)
  419. drm_framebuffer_put(plane->old_fb);
  420. plane->old_fb = NULL;
  421. }
  422. return ret;
  423. }
  424. /**
  425. * drm_mode_set_config_internal - helper to call &drm_mode_config_funcs.set_config
  426. * @set: modeset config to set
  427. *
  428. * This is a little helper to wrap internal calls to the
  429. * &drm_mode_config_funcs.set_config driver interface. The only thing it adds is
  430. * correct refcounting dance.
  431. *
  432. * This should only be used by non-atomic legacy drivers.
  433. *
  434. * Returns:
  435. * Zero on success, negative errno on failure.
  436. */
  437. int drm_mode_set_config_internal(struct drm_mode_set *set)
  438. {
  439. WARN_ON(drm_drv_uses_atomic_modeset(set->crtc->dev));
  440. return __drm_mode_set_config_internal(set, NULL);
  441. }
  442. EXPORT_SYMBOL(drm_mode_set_config_internal);
  443. /**
  444. * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
  445. * CRTC viewport
  446. * @crtc: CRTC that framebuffer will be displayed on
  447. * @x: x panning
  448. * @y: y panning
  449. * @mode: mode that framebuffer will be displayed under
  450. * @fb: framebuffer to check size of
  451. */
  452. int drm_crtc_check_viewport(const struct drm_crtc *crtc,
  453. int x, int y,
  454. const struct drm_display_mode *mode,
  455. const struct drm_framebuffer *fb)
  456. {
  457. int hdisplay, vdisplay;
  458. drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
  459. if (crtc->state &&
  460. drm_rotation_90_or_270(crtc->primary->state->rotation))
  461. swap(hdisplay, vdisplay);
  462. return drm_framebuffer_check_src_coords(x << 16, y << 16,
  463. hdisplay << 16, vdisplay << 16,
  464. fb);
  465. }
  466. EXPORT_SYMBOL(drm_crtc_check_viewport);
  467. /**
  468. * drm_mode_setcrtc - set CRTC configuration
  469. * @dev: drm device for the ioctl
  470. * @data: data pointer for the ioctl
  471. * @file_priv: drm file for the ioctl call
  472. *
  473. * Build a new CRTC configuration based on user request.
  474. *
  475. * Called by the user via ioctl.
  476. *
  477. * Returns:
  478. * Zero on success, negative errno on failure.
  479. */
  480. int drm_mode_setcrtc(struct drm_device *dev, void *data,
  481. struct drm_file *file_priv)
  482. {
  483. struct drm_mode_config *config = &dev->mode_config;
  484. struct drm_mode_crtc *crtc_req = data;
  485. struct drm_crtc *crtc;
  486. struct drm_plane *plane;
  487. struct drm_connector **connector_set, *connector;
  488. struct drm_framebuffer *fb;
  489. struct drm_display_mode *mode;
  490. struct drm_mode_set set;
  491. uint32_t __user *set_connectors_ptr;
  492. struct drm_modeset_acquire_ctx ctx;
  493. int ret;
  494. int i;
  495. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  496. return -EOPNOTSUPP;
  497. /*
  498. * Universal plane src offsets are only 16.16, prevent havoc for
  499. * drivers using universal plane code internally.
  500. */
  501. if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
  502. return -ERANGE;
  503. crtc = drm_crtc_find(dev, file_priv, crtc_req->crtc_id);
  504. if (!crtc) {
  505. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
  506. return -ENOENT;
  507. }
  508. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  509. plane = crtc->primary;
  510. mutex_lock(&crtc->dev->mode_config.mutex);
  511. drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
  512. retry:
  513. connector_set = NULL;
  514. fb = NULL;
  515. mode = NULL;
  516. ret = drm_modeset_lock_all_ctx(crtc->dev, &ctx);
  517. if (ret)
  518. goto out;
  519. if (crtc_req->mode_valid) {
  520. /* If we have a mode we need a framebuffer. */
  521. /* If we pass -1, set the mode with the currently bound fb */
  522. if (crtc_req->fb_id == -1) {
  523. struct drm_framebuffer *old_fb;
  524. if (plane->state)
  525. old_fb = plane->state->fb;
  526. else
  527. old_fb = plane->fb;
  528. if (!old_fb) {
  529. DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
  530. ret = -EINVAL;
  531. goto out;
  532. }
  533. fb = old_fb;
  534. /* Make refcounting symmetric with the lookup path. */
  535. drm_framebuffer_get(fb);
  536. } else {
  537. fb = drm_framebuffer_lookup(dev, file_priv, crtc_req->fb_id);
  538. if (!fb) {
  539. DRM_DEBUG_KMS("Unknown FB ID%d\n",
  540. crtc_req->fb_id);
  541. ret = -ENOENT;
  542. goto out;
  543. }
  544. }
  545. mode = drm_mode_create(dev);
  546. if (!mode) {
  547. ret = -ENOMEM;
  548. goto out;
  549. }
  550. if (!file_priv->aspect_ratio_allowed &&
  551. (crtc_req->mode.flags & DRM_MODE_FLAG_PIC_AR_MASK) != DRM_MODE_FLAG_PIC_AR_NONE) {
  552. DRM_DEBUG_KMS("Unexpected aspect-ratio flag bits\n");
  553. ret = -EINVAL;
  554. goto out;
  555. }
  556. ret = drm_mode_convert_umode(dev, mode, &crtc_req->mode);
  557. if (ret) {
  558. DRM_DEBUG_KMS("Invalid mode (ret=%d, status=%s)\n",
  559. ret, drm_get_mode_status_name(mode->status));
  560. drm_mode_debug_printmodeline(mode);
  561. goto out;
  562. }
  563. /*
  564. * Check whether the primary plane supports the fb pixel format.
  565. * Drivers not implementing the universal planes API use a
  566. * default formats list provided by the DRM core which doesn't
  567. * match real hardware capabilities. Skip the check in that
  568. * case.
  569. */
  570. if (!plane->format_default) {
  571. ret = drm_plane_check_pixel_format(plane,
  572. fb->format->format,
  573. fb->modifier);
  574. if (ret) {
  575. struct drm_format_name_buf format_name;
  576. DRM_DEBUG_KMS("Invalid pixel format %s, modifier 0x%llx\n",
  577. drm_get_format_name(fb->format->format,
  578. &format_name),
  579. fb->modifier);
  580. goto out;
  581. }
  582. }
  583. ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
  584. mode, fb);
  585. if (ret)
  586. goto out;
  587. }
  588. if (crtc_req->count_connectors == 0 && mode) {
  589. DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
  590. ret = -EINVAL;
  591. goto out;
  592. }
  593. if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
  594. DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
  595. crtc_req->count_connectors);
  596. ret = -EINVAL;
  597. goto out;
  598. }
  599. if (crtc_req->count_connectors > 0) {
  600. u32 out_id;
  601. /* Avoid unbounded kernel memory allocation */
  602. if (crtc_req->count_connectors > config->num_connector) {
  603. ret = -EINVAL;
  604. goto out;
  605. }
  606. connector_set = kmalloc_array(crtc_req->count_connectors,
  607. sizeof(struct drm_connector *),
  608. GFP_KERNEL);
  609. if (!connector_set) {
  610. ret = -ENOMEM;
  611. goto out;
  612. }
  613. for (i = 0; i < crtc_req->count_connectors; i++) {
  614. connector_set[i] = NULL;
  615. set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
  616. if (get_user(out_id, &set_connectors_ptr[i])) {
  617. ret = -EFAULT;
  618. goto out;
  619. }
  620. connector = drm_connector_lookup(dev, file_priv, out_id);
  621. if (!connector) {
  622. DRM_DEBUG_KMS("Connector id %d unknown\n",
  623. out_id);
  624. ret = -ENOENT;
  625. goto out;
  626. }
  627. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  628. connector->base.id,
  629. connector->name);
  630. connector_set[i] = connector;
  631. }
  632. }
  633. set.crtc = crtc;
  634. set.x = crtc_req->x;
  635. set.y = crtc_req->y;
  636. set.mode = mode;
  637. set.connectors = connector_set;
  638. set.num_connectors = crtc_req->count_connectors;
  639. set.fb = fb;
  640. if (drm_drv_uses_atomic_modeset(dev))
  641. ret = crtc->funcs->set_config(&set, &ctx);
  642. else
  643. ret = __drm_mode_set_config_internal(&set, &ctx);
  644. out:
  645. if (fb)
  646. drm_framebuffer_put(fb);
  647. if (connector_set) {
  648. for (i = 0; i < crtc_req->count_connectors; i++) {
  649. if (connector_set[i])
  650. drm_connector_put(connector_set[i]);
  651. }
  652. }
  653. kfree(connector_set);
  654. drm_mode_destroy(dev, mode);
  655. if (ret == -EDEADLK) {
  656. ret = drm_modeset_backoff(&ctx);
  657. if (!ret)
  658. goto retry;
  659. }
  660. drm_modeset_drop_locks(&ctx);
  661. drm_modeset_acquire_fini(&ctx);
  662. mutex_unlock(&crtc->dev->mode_config.mutex);
  663. return ret;
  664. }
  665. int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
  666. struct drm_property *property,
  667. uint64_t value)
  668. {
  669. int ret = -EINVAL;
  670. struct drm_crtc *crtc = obj_to_crtc(obj);
  671. if (crtc->funcs->set_property)
  672. ret = crtc->funcs->set_property(crtc, property, value);
  673. if (!ret)
  674. drm_object_property_set_value(obj, property, value);
  675. return ret;
  676. }