drm_crtc.c 21 KB

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