drm_crtc.c 18 KB

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