drm_crtc.c 19 KB

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