drm_crtc.c 21 KB

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