drm_crtc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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_blend.h>
  39. #include <drm/drm_crtc.h>
  40. #include <drm/drm_edid.h>
  41. #include <drm/drm_fourcc.h>
  42. #include <drm/drm_framebuffer.h>
  43. #include <drm/drm_managed.h>
  44. #include <drm/drm_modeset_lock.h>
  45. #include <drm/drm_atomic.h>
  46. #include <drm/drm_auth.h>
  47. #include <drm/drm_debugfs_crc.h>
  48. #include <drm/drm_drv.h>
  49. #include <drm/drm_print.h>
  50. #include <drm/drm_file.h>
  51. #include "drm_crtc_internal.h"
  52. #include "drm_internal.h"
  53. /**
  54. * DOC: overview
  55. *
  56. * A CRTC represents the overall display pipeline. It receives pixel data from
  57. * &drm_plane and blends them together. The &drm_display_mode is also attached
  58. * to the CRTC, specifying display timings. On the output side the data is fed
  59. * to one or more &drm_encoder, which are then each connected to one
  60. * &drm_connector.
  61. *
  62. * To create a CRTC, a KMS driver allocates and zeroes an instance of
  63. * &struct drm_crtc (possibly as part of a larger structure) and registers it
  64. * with a call to drm_crtc_init_with_planes().
  65. *
  66. * The CRTC is also the entry point for legacy modeset operations (see
  67. * &drm_crtc_funcs.set_config), legacy plane operations (see
  68. * &drm_crtc_funcs.page_flip and &drm_crtc_funcs.cursor_set2), and other legacy
  69. * operations like &drm_crtc_funcs.gamma_set. For atomic drivers all these
  70. * features are controlled through &drm_property and
  71. * &drm_mode_config_funcs.atomic_check.
  72. */
  73. /**
  74. * drm_crtc_from_index - find the registered CRTC at an index
  75. * @dev: DRM device
  76. * @idx: index of registered CRTC to find for
  77. *
  78. * Given a CRTC index, return the registered CRTC from DRM device's
  79. * list of CRTCs with matching index. This is the inverse of drm_crtc_index().
  80. * It's useful in the vblank callbacks (like &drm_driver.enable_vblank or
  81. * &drm_driver.disable_vblank), since that still deals with indices instead
  82. * of pointers to &struct drm_crtc."
  83. */
  84. struct drm_crtc *drm_crtc_from_index(struct drm_device *dev, int idx)
  85. {
  86. struct drm_crtc *crtc;
  87. drm_for_each_crtc(crtc, dev)
  88. if (idx == crtc->index)
  89. return crtc;
  90. return NULL;
  91. }
  92. EXPORT_SYMBOL(drm_crtc_from_index);
  93. int drm_crtc_force_disable(struct drm_crtc *crtc)
  94. {
  95. struct drm_mode_set set = {
  96. .crtc = crtc,
  97. };
  98. WARN_ON(drm_drv_uses_atomic_modeset(crtc->dev));
  99. return drm_mode_set_config_internal(&set);
  100. }
  101. int drm_crtc_register_all(struct drm_device *dev)
  102. {
  103. struct drm_crtc *crtc;
  104. int ret = 0;
  105. drm_for_each_crtc(crtc, dev) {
  106. drm_debugfs_crtc_add(crtc);
  107. if (crtc->funcs->late_register)
  108. ret = crtc->funcs->late_register(crtc);
  109. if (ret)
  110. return ret;
  111. }
  112. return 0;
  113. }
  114. void drm_crtc_unregister_all(struct drm_device *dev)
  115. {
  116. struct drm_crtc *crtc;
  117. drm_for_each_crtc(crtc, dev) {
  118. if (crtc->funcs->early_unregister)
  119. crtc->funcs->early_unregister(crtc);
  120. drm_debugfs_crtc_remove(crtc);
  121. }
  122. }
  123. static int drm_crtc_crc_init(struct drm_crtc *crtc)
  124. {
  125. #ifdef CONFIG_DEBUG_FS
  126. spin_lock_init(&crtc->crc.lock);
  127. init_waitqueue_head(&crtc->crc.wq);
  128. crtc->crc.source = kstrdup("auto", GFP_KERNEL);
  129. if (!crtc->crc.source)
  130. return -ENOMEM;
  131. #endif
  132. return 0;
  133. }
  134. static void drm_crtc_crc_fini(struct drm_crtc *crtc)
  135. {
  136. #ifdef CONFIG_DEBUG_FS
  137. kfree(crtc->crc.source);
  138. #endif
  139. }
  140. static const struct dma_fence_ops drm_crtc_fence_ops;
  141. static struct drm_crtc *fence_to_crtc(struct dma_fence *fence)
  142. {
  143. BUG_ON(fence->ops != &drm_crtc_fence_ops);
  144. return container_of(fence->lock, struct drm_crtc, fence_lock);
  145. }
  146. static const char *drm_crtc_fence_get_driver_name(struct dma_fence *fence)
  147. {
  148. struct drm_crtc *crtc = fence_to_crtc(fence);
  149. return crtc->dev->driver->name;
  150. }
  151. static const char *drm_crtc_fence_get_timeline_name(struct dma_fence *fence)
  152. {
  153. struct drm_crtc *crtc = fence_to_crtc(fence);
  154. return crtc->timeline_name;
  155. }
  156. static const struct dma_fence_ops drm_crtc_fence_ops = {
  157. .get_driver_name = drm_crtc_fence_get_driver_name,
  158. .get_timeline_name = drm_crtc_fence_get_timeline_name,
  159. };
  160. struct dma_fence *drm_crtc_create_fence(struct drm_crtc *crtc)
  161. {
  162. struct dma_fence *fence;
  163. fence = kzalloc_obj(*fence);
  164. if (!fence)
  165. return NULL;
  166. dma_fence_init(fence, &drm_crtc_fence_ops, &crtc->fence_lock,
  167. crtc->fence_context, ++crtc->fence_seqno);
  168. return fence;
  169. }
  170. /**
  171. * DOC: standard CRTC properties
  172. *
  173. * DRM CRTCs have a few standardized properties:
  174. *
  175. * ACTIVE:
  176. * Atomic property for setting the power state of the CRTC. When set to 1
  177. * the CRTC will actively display content. When set to 0 the CRTC will be
  178. * powered off. There is no expectation that user-space will reset CRTC
  179. * resources like the mode and planes when setting ACTIVE to 0.
  180. *
  181. * User-space can rely on an ACTIVE change to 1 to never fail an atomic
  182. * test as long as no other property has changed. If a change to ACTIVE
  183. * fails an atomic test, this is a driver bug. For this reason setting
  184. * ACTIVE to 0 must not release internal resources (like reserved memory
  185. * bandwidth or clock generators).
  186. *
  187. * Note that the legacy DPMS property on connectors is internally routed
  188. * to control this property for atomic drivers.
  189. * MODE_ID:
  190. * Atomic property for setting the CRTC display timings. The value is the
  191. * ID of a blob containing the DRM mode info. To disable the CRTC,
  192. * user-space must set this property to 0.
  193. *
  194. * Setting MODE_ID to 0 will release reserved resources for the CRTC.
  195. * SCALING_FILTER:
  196. * Atomic property for setting the scaling filter for CRTC scaler
  197. *
  198. * The value of this property can be one of the following:
  199. *
  200. * Default:
  201. * Driver's default scaling filter
  202. * Nearest Neighbor:
  203. * Nearest Neighbor scaling filter
  204. * SHARPNESS_STRENGTH:
  205. * Atomic property for setting the sharpness strength/intensity by userspace.
  206. *
  207. * The value of this property is set as an integer value ranging
  208. * from 0 - 255 where:
  209. *
  210. * 0: Sharpness feature is disabled(default value).
  211. *
  212. * 1: Minimum sharpness.
  213. *
  214. * 255: Maximum sharpness.
  215. *
  216. * User can gradually increase or decrease the sharpness level and can
  217. * set the optimum value depending on content.
  218. * This value will be passed to kernel through the UAPI.
  219. * The setting of this property does not require modeset.
  220. * The sharpness effect takes place post blending on the final composed output.
  221. * If the feature is disabled, the content remains same without any sharpening effect
  222. * and when this feature is applied, it enhances the clarity of the content.
  223. */
  224. __printf(6, 0)
  225. static int __drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
  226. struct drm_plane *primary,
  227. struct drm_plane *cursor,
  228. const struct drm_crtc_funcs *funcs,
  229. const char *name, va_list ap)
  230. {
  231. struct drm_mode_config *config = &dev->mode_config;
  232. int ret;
  233. WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
  234. WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
  235. /* crtc index is used with 32bit bitmasks */
  236. if (WARN_ON(config->num_crtc >= 32))
  237. return -EINVAL;
  238. WARN_ON(drm_drv_uses_atomic_modeset(dev) &&
  239. (!funcs->atomic_destroy_state ||
  240. !funcs->atomic_duplicate_state));
  241. crtc->dev = dev;
  242. crtc->funcs = funcs;
  243. INIT_LIST_HEAD(&crtc->commit_list);
  244. spin_lock_init(&crtc->commit_lock);
  245. drm_modeset_lock_init(&crtc->mutex);
  246. ret = drm_mode_object_add(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
  247. if (ret)
  248. return ret;
  249. if (name) {
  250. crtc->name = kvasprintf(GFP_KERNEL, name, ap);
  251. } else {
  252. crtc->name = kasprintf(GFP_KERNEL, "crtc-%d", config->num_crtc);
  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. /**
  287. * drm_crtc_init_with_planes - Initialise a new CRTC object with
  288. * specified primary and cursor planes.
  289. * @dev: DRM device
  290. * @crtc: CRTC object to init
  291. * @primary: Primary plane for CRTC
  292. * @cursor: Cursor plane for CRTC
  293. * @funcs: callbacks for the new CRTC
  294. * @name: printf style format string for the CRTC name, or NULL for default name
  295. *
  296. * Inits a new object created as base part of a driver crtc object. Drivers
  297. * should use this function instead of drm_crtc_init(), which is only provided
  298. * for backwards compatibility with drivers which do not yet support universal
  299. * planes). For really simple hardware which has only 1 plane look at
  300. * drm_simple_display_pipe_init() instead.
  301. * The &drm_crtc_funcs.destroy hook should call drm_crtc_cleanup() and kfree()
  302. * the crtc structure. The crtc structure should not be allocated with
  303. * devm_kzalloc().
  304. *
  305. * The @primary and @cursor planes are only relevant for legacy uAPI, see
  306. * &drm_crtc.primary and &drm_crtc.cursor.
  307. *
  308. * Note: consider using drmm_crtc_alloc_with_planes() or
  309. * drmm_crtc_init_with_planes() instead of drm_crtc_init_with_planes()
  310. * to let the DRM managed resource infrastructure take care of cleanup
  311. * and deallocation.
  312. *
  313. * Returns:
  314. * Zero on success, error code on failure.
  315. */
  316. int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
  317. struct drm_plane *primary,
  318. struct drm_plane *cursor,
  319. const struct drm_crtc_funcs *funcs,
  320. const char *name, ...)
  321. {
  322. va_list ap;
  323. int ret;
  324. WARN_ON(!funcs->destroy);
  325. va_start(ap, name);
  326. ret = __drm_crtc_init_with_planes(dev, crtc, primary, cursor, funcs,
  327. name, ap);
  328. va_end(ap);
  329. return ret;
  330. }
  331. EXPORT_SYMBOL(drm_crtc_init_with_planes);
  332. static void drmm_crtc_init_with_planes_cleanup(struct drm_device *dev,
  333. void *ptr)
  334. {
  335. struct drm_crtc *crtc = ptr;
  336. drm_crtc_cleanup(crtc);
  337. }
  338. __printf(6, 0)
  339. static int __drmm_crtc_init_with_planes(struct drm_device *dev,
  340. struct drm_crtc *crtc,
  341. struct drm_plane *primary,
  342. struct drm_plane *cursor,
  343. const struct drm_crtc_funcs *funcs,
  344. const char *name,
  345. va_list args)
  346. {
  347. int ret;
  348. drm_WARN_ON(dev, funcs && funcs->destroy);
  349. ret = __drm_crtc_init_with_planes(dev, crtc, primary, cursor, funcs,
  350. name, args);
  351. if (ret)
  352. return ret;
  353. ret = drmm_add_action_or_reset(dev, drmm_crtc_init_with_planes_cleanup,
  354. crtc);
  355. if (ret)
  356. return ret;
  357. return 0;
  358. }
  359. /**
  360. * drmm_crtc_init_with_planes - Initialise a new CRTC object with
  361. * specified primary and cursor planes.
  362. * @dev: DRM device
  363. * @crtc: CRTC object to init
  364. * @primary: Primary plane for CRTC
  365. * @cursor: Cursor plane for CRTC
  366. * @funcs: callbacks for the new CRTC
  367. * @name: printf style format string for the CRTC name, or NULL for default name
  368. *
  369. * Inits a new object created as base part of a driver crtc object. Drivers
  370. * should use this function instead of drm_crtc_init(), which is only provided
  371. * for backwards compatibility with drivers which do not yet support universal
  372. * planes). For really simple hardware which has only 1 plane look at
  373. * drm_simple_display_pipe_init() instead.
  374. *
  375. * Cleanup is automatically handled through registering
  376. * drmm_crtc_cleanup() with drmm_add_action(). The crtc structure should
  377. * be allocated with drmm_kzalloc().
  378. *
  379. * The @drm_crtc_funcs.destroy hook must be NULL.
  380. *
  381. * The @primary and @cursor planes are only relevant for legacy uAPI, see
  382. * &drm_crtc.primary and &drm_crtc.cursor.
  383. *
  384. * Returns:
  385. * Zero on success, error code on failure.
  386. */
  387. int drmm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
  388. struct drm_plane *primary,
  389. struct drm_plane *cursor,
  390. const struct drm_crtc_funcs *funcs,
  391. const char *name, ...)
  392. {
  393. va_list ap;
  394. int ret;
  395. va_start(ap, name);
  396. ret = __drmm_crtc_init_with_planes(dev, crtc, primary, cursor, funcs,
  397. name, ap);
  398. va_end(ap);
  399. if (ret)
  400. return ret;
  401. return 0;
  402. }
  403. EXPORT_SYMBOL(drmm_crtc_init_with_planes);
  404. void *__drmm_crtc_alloc_with_planes(struct drm_device *dev,
  405. size_t size, size_t offset,
  406. struct drm_plane *primary,
  407. struct drm_plane *cursor,
  408. const struct drm_crtc_funcs *funcs,
  409. const char *name, ...)
  410. {
  411. void *container;
  412. struct drm_crtc *crtc;
  413. va_list ap;
  414. int ret;
  415. if (WARN_ON(!funcs || funcs->destroy))
  416. return ERR_PTR(-EINVAL);
  417. container = drmm_kzalloc(dev, size, GFP_KERNEL);
  418. if (!container)
  419. return ERR_PTR(-ENOMEM);
  420. crtc = container + offset;
  421. va_start(ap, name);
  422. ret = __drmm_crtc_init_with_planes(dev, crtc, primary, cursor, funcs,
  423. name, ap);
  424. va_end(ap);
  425. if (ret)
  426. return ERR_PTR(ret);
  427. return container;
  428. }
  429. EXPORT_SYMBOL(__drmm_crtc_alloc_with_planes);
  430. /**
  431. * drm_crtc_cleanup - Clean up the core crtc usage
  432. * @crtc: CRTC to cleanup
  433. *
  434. * This function cleans up @crtc and removes it from the DRM mode setting
  435. * core. Note that the function does *not* free the crtc structure itself,
  436. * this is the responsibility of the caller.
  437. */
  438. void drm_crtc_cleanup(struct drm_crtc *crtc)
  439. {
  440. struct drm_device *dev = crtc->dev;
  441. /* Note that the crtc_list is considered to be static; should we
  442. * remove the drm_crtc at runtime we would have to decrement all
  443. * the indices on the drm_crtc after us in the crtc_list.
  444. */
  445. drm_crtc_crc_fini(crtc);
  446. kfree(crtc->gamma_store);
  447. crtc->gamma_store = NULL;
  448. drm_modeset_lock_fini(&crtc->mutex);
  449. drm_mode_object_unregister(dev, &crtc->base);
  450. list_del(&crtc->head);
  451. dev->mode_config.num_crtc--;
  452. WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
  453. if (crtc->state && crtc->funcs->atomic_destroy_state)
  454. crtc->funcs->atomic_destroy_state(crtc, crtc->state);
  455. kfree(crtc->name);
  456. memset(crtc, 0, sizeof(*crtc));
  457. }
  458. EXPORT_SYMBOL(drm_crtc_cleanup);
  459. /**
  460. * drm_mode_getcrtc - get CRTC configuration
  461. * @dev: drm device for the ioctl
  462. * @data: data pointer for the ioctl
  463. * @file_priv: drm file for the ioctl call
  464. *
  465. * Construct a CRTC configuration structure to return to the user.
  466. *
  467. * Called by the user via ioctl.
  468. *
  469. * Returns:
  470. * Zero on success, negative errno on failure.
  471. */
  472. int drm_mode_getcrtc(struct drm_device *dev,
  473. void *data, struct drm_file *file_priv)
  474. {
  475. struct drm_mode_crtc *crtc_resp = data;
  476. struct drm_crtc *crtc;
  477. struct drm_plane *plane;
  478. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  479. return -EOPNOTSUPP;
  480. crtc = drm_crtc_find(dev, file_priv, crtc_resp->crtc_id);
  481. if (!crtc)
  482. return -ENOENT;
  483. plane = crtc->primary;
  484. crtc_resp->gamma_size = crtc->gamma_size;
  485. drm_modeset_lock(&plane->mutex, NULL);
  486. if (plane->state && plane->state->fb)
  487. crtc_resp->fb_id = plane->state->fb->base.id;
  488. else if (!plane->state && plane->fb)
  489. crtc_resp->fb_id = plane->fb->base.id;
  490. else
  491. crtc_resp->fb_id = 0;
  492. if (plane->state) {
  493. crtc_resp->x = plane->state->src_x >> 16;
  494. crtc_resp->y = plane->state->src_y >> 16;
  495. }
  496. drm_modeset_unlock(&plane->mutex);
  497. drm_modeset_lock(&crtc->mutex, NULL);
  498. if (crtc->state) {
  499. if (crtc->state->enable) {
  500. drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
  501. crtc_resp->mode_valid = 1;
  502. } else {
  503. crtc_resp->mode_valid = 0;
  504. }
  505. } else {
  506. crtc_resp->x = crtc->x;
  507. crtc_resp->y = crtc->y;
  508. if (crtc->enabled) {
  509. drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
  510. crtc_resp->mode_valid = 1;
  511. } else {
  512. crtc_resp->mode_valid = 0;
  513. }
  514. }
  515. if (!file_priv->aspect_ratio_allowed)
  516. crtc_resp->mode.flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
  517. drm_modeset_unlock(&crtc->mutex);
  518. return 0;
  519. }
  520. static int __drm_mode_set_config_internal(struct drm_mode_set *set,
  521. struct drm_modeset_acquire_ctx *ctx)
  522. {
  523. struct drm_crtc *crtc = set->crtc;
  524. struct drm_framebuffer *fb;
  525. struct drm_crtc *tmp;
  526. int ret;
  527. WARN_ON(drm_drv_uses_atomic_modeset(crtc->dev));
  528. /*
  529. * NOTE: ->set_config can also disable other crtcs (if we steal all
  530. * connectors from it), hence we need to refcount the fbs across all
  531. * crtcs. Atomic modeset will have saner semantics ...
  532. */
  533. drm_for_each_crtc(tmp, crtc->dev) {
  534. struct drm_plane *plane = tmp->primary;
  535. plane->old_fb = plane->fb;
  536. }
  537. fb = set->fb;
  538. ret = crtc->funcs->set_config(set, ctx);
  539. if (ret == 0) {
  540. struct drm_plane *plane = crtc->primary;
  541. plane->crtc = fb ? crtc : NULL;
  542. plane->fb = fb;
  543. }
  544. drm_for_each_crtc(tmp, crtc->dev) {
  545. struct drm_plane *plane = tmp->primary;
  546. if (plane->fb)
  547. drm_framebuffer_get(plane->fb);
  548. if (plane->old_fb)
  549. drm_framebuffer_put(plane->old_fb);
  550. plane->old_fb = NULL;
  551. }
  552. return ret;
  553. }
  554. /**
  555. * drm_mode_set_config_internal - helper to call &drm_mode_config_funcs.set_config
  556. * @set: modeset config to set
  557. *
  558. * This is a little helper to wrap internal calls to the
  559. * &drm_mode_config_funcs.set_config driver interface. The only thing it adds is
  560. * correct refcounting dance.
  561. *
  562. * This should only be used by non-atomic legacy drivers.
  563. *
  564. * Returns:
  565. * Zero on success, negative errno on failure.
  566. */
  567. int drm_mode_set_config_internal(struct drm_mode_set *set)
  568. {
  569. WARN_ON(drm_drv_uses_atomic_modeset(set->crtc->dev));
  570. return __drm_mode_set_config_internal(set, NULL);
  571. }
  572. EXPORT_SYMBOL(drm_mode_set_config_internal);
  573. /**
  574. * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
  575. * CRTC viewport
  576. * @crtc: CRTC that framebuffer will be displayed on
  577. * @x: x panning
  578. * @y: y panning
  579. * @mode: mode that framebuffer will be displayed under
  580. * @fb: framebuffer to check size of
  581. */
  582. int drm_crtc_check_viewport(const struct drm_crtc *crtc,
  583. int x, int y,
  584. const struct drm_display_mode *mode,
  585. const struct drm_framebuffer *fb)
  586. {
  587. int hdisplay, vdisplay;
  588. drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
  589. if (crtc->state &&
  590. drm_rotation_90_or_270(crtc->primary->state->rotation))
  591. swap(hdisplay, vdisplay);
  592. return drm_framebuffer_check_src_coords(x << 16, y << 16,
  593. hdisplay << 16, vdisplay << 16,
  594. fb);
  595. }
  596. EXPORT_SYMBOL(drm_crtc_check_viewport);
  597. /**
  598. * drm_mode_setcrtc - set CRTC configuration
  599. * @dev: drm device for the ioctl
  600. * @data: data pointer for the ioctl
  601. * @file_priv: drm file for the ioctl call
  602. *
  603. * Build a new CRTC configuration based on user request.
  604. *
  605. * Called by the user via ioctl.
  606. *
  607. * Returns:
  608. * Zero on success, negative errno on failure.
  609. */
  610. int drm_mode_setcrtc(struct drm_device *dev, void *data,
  611. struct drm_file *file_priv)
  612. {
  613. struct drm_mode_config *config = &dev->mode_config;
  614. struct drm_mode_crtc *crtc_req = data;
  615. struct drm_crtc *crtc;
  616. struct drm_plane *plane;
  617. struct drm_connector **connector_set = NULL, *connector;
  618. struct drm_framebuffer *fb = NULL;
  619. struct drm_display_mode *mode = NULL;
  620. struct drm_mode_set set;
  621. uint32_t __user *set_connectors_ptr;
  622. struct drm_modeset_acquire_ctx ctx;
  623. int ret, i, num_connectors = 0;
  624. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  625. return -EOPNOTSUPP;
  626. /*
  627. * Universal plane src offsets are only 16.16, prevent havoc for
  628. * drivers using universal plane code internally.
  629. */
  630. if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
  631. return -ERANGE;
  632. crtc = drm_crtc_find(dev, file_priv, crtc_req->crtc_id);
  633. if (!crtc) {
  634. drm_dbg_kms(dev, "Unknown CRTC ID %d\n", crtc_req->crtc_id);
  635. return -ENOENT;
  636. }
  637. drm_dbg_kms(dev, "[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  638. plane = crtc->primary;
  639. /* allow disabling with the primary plane leased */
  640. if (crtc_req->mode_valid && !drm_lease_held(file_priv, plane->base.id))
  641. return -EACCES;
  642. DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx,
  643. DRM_MODESET_ACQUIRE_INTERRUPTIBLE, ret);
  644. if (crtc_req->mode_valid) {
  645. /* If we have a mode we need a framebuffer. */
  646. /* If we pass -1, set the mode with the currently bound fb */
  647. if (crtc_req->fb_id == -1) {
  648. struct drm_framebuffer *old_fb;
  649. if (plane->state)
  650. old_fb = plane->state->fb;
  651. else
  652. old_fb = plane->fb;
  653. if (!old_fb) {
  654. drm_dbg_kms(dev, "CRTC doesn't have current FB\n");
  655. ret = -EINVAL;
  656. goto out;
  657. }
  658. fb = old_fb;
  659. /* Make refcounting symmetric with the lookup path. */
  660. drm_framebuffer_get(fb);
  661. } else {
  662. fb = drm_framebuffer_lookup(dev, file_priv, crtc_req->fb_id);
  663. if (!fb) {
  664. drm_dbg_kms(dev, "Unknown FB ID%d\n",
  665. crtc_req->fb_id);
  666. ret = -ENOENT;
  667. goto out;
  668. }
  669. }
  670. mode = drm_mode_create(dev);
  671. if (!mode) {
  672. ret = -ENOMEM;
  673. goto out;
  674. }
  675. if (!file_priv->aspect_ratio_allowed &&
  676. (crtc_req->mode.flags & DRM_MODE_FLAG_PIC_AR_MASK) != DRM_MODE_FLAG_PIC_AR_NONE) {
  677. drm_dbg_kms(dev, "Unexpected aspect-ratio flag bits\n");
  678. ret = -EINVAL;
  679. goto out;
  680. }
  681. ret = drm_mode_convert_umode(dev, mode, &crtc_req->mode);
  682. if (ret) {
  683. drm_dbg_kms(dev, "Invalid mode (%s, %pe): " DRM_MODE_FMT "\n",
  684. drm_get_mode_status_name(mode->status),
  685. ERR_PTR(ret), DRM_MODE_ARG(mode));
  686. goto out;
  687. }
  688. /*
  689. * Check whether the primary plane supports the fb pixel format.
  690. * Drivers not implementing the universal planes API use a
  691. * default formats list provided by the DRM core which doesn't
  692. * match real hardware capabilities. Skip the check in that
  693. * case.
  694. */
  695. if (!plane->format_default) {
  696. if (!drm_plane_has_format(plane, fb->format->format, fb->modifier)) {
  697. drm_dbg_kms(dev, "Invalid pixel format %p4cc, modifier 0x%llx\n",
  698. &fb->format->format, fb->modifier);
  699. ret = -EINVAL;
  700. goto out;
  701. }
  702. }
  703. ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
  704. mode, fb);
  705. if (ret)
  706. goto out;
  707. }
  708. if (crtc_req->count_connectors == 0 && mode) {
  709. drm_dbg_kms(dev, "Count connectors is 0 but mode set\n");
  710. ret = -EINVAL;
  711. goto out;
  712. }
  713. if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
  714. drm_dbg_kms(dev, "Count connectors is %d but no mode or fb set\n",
  715. crtc_req->count_connectors);
  716. ret = -EINVAL;
  717. goto out;
  718. }
  719. if (crtc_req->count_connectors > 0) {
  720. u32 out_id;
  721. /* Avoid unbounded kernel memory allocation */
  722. if (crtc_req->count_connectors > config->num_connector) {
  723. ret = -EINVAL;
  724. goto out;
  725. }
  726. connector_set = kmalloc_objs(struct drm_connector *,
  727. crtc_req->count_connectors);
  728. if (!connector_set) {
  729. ret = -ENOMEM;
  730. goto out;
  731. }
  732. for (i = 0; i < crtc_req->count_connectors; i++) {
  733. connector_set[i] = NULL;
  734. set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
  735. if (get_user(out_id, &set_connectors_ptr[i])) {
  736. ret = -EFAULT;
  737. goto out;
  738. }
  739. connector = drm_connector_lookup(dev, file_priv, out_id);
  740. if (!connector) {
  741. drm_dbg_kms(dev, "Connector id %d unknown\n",
  742. out_id);
  743. ret = -ENOENT;
  744. goto out;
  745. }
  746. drm_dbg_kms(dev, "[CONNECTOR:%d:%s]\n",
  747. connector->base.id, connector->name);
  748. connector_set[i] = connector;
  749. num_connectors++;
  750. }
  751. }
  752. set.crtc = crtc;
  753. set.x = crtc_req->x;
  754. set.y = crtc_req->y;
  755. set.mode = mode;
  756. set.connectors = connector_set;
  757. set.num_connectors = num_connectors;
  758. set.fb = fb;
  759. if (drm_drv_uses_atomic_modeset(dev))
  760. ret = crtc->funcs->set_config(&set, &ctx);
  761. else
  762. ret = __drm_mode_set_config_internal(&set, &ctx);
  763. out:
  764. if (fb)
  765. drm_framebuffer_put(fb);
  766. if (connector_set) {
  767. for (i = 0; i < num_connectors; i++) {
  768. if (connector_set[i])
  769. drm_connector_put(connector_set[i]);
  770. }
  771. }
  772. kfree(connector_set);
  773. drm_mode_destroy(dev, mode);
  774. /* In case we need to retry... */
  775. connector_set = NULL;
  776. fb = NULL;
  777. mode = NULL;
  778. num_connectors = 0;
  779. DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
  780. return ret;
  781. }
  782. int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
  783. struct drm_property *property,
  784. uint64_t value)
  785. {
  786. int ret = -EINVAL;
  787. struct drm_crtc *crtc = obj_to_crtc(obj);
  788. if (crtc->funcs->set_property)
  789. ret = crtc->funcs->set_property(crtc, property, value);
  790. if (!ret)
  791. drm_object_property_set_value(obj, property, value);
  792. return ret;
  793. }
  794. /**
  795. * drm_crtc_create_scaling_filter_property - create a new scaling filter
  796. * property
  797. *
  798. * @crtc: drm CRTC
  799. * @supported_filters: bitmask of supported scaling filters, must include
  800. * BIT(DRM_SCALING_FILTER_DEFAULT).
  801. *
  802. * This function lets driver to enable the scaling filter property on a given
  803. * CRTC.
  804. *
  805. * RETURNS:
  806. * Zero for success or -errno
  807. */
  808. int drm_crtc_create_scaling_filter_property(struct drm_crtc *crtc,
  809. unsigned int supported_filters)
  810. {
  811. struct drm_property *prop =
  812. drm_create_scaling_filter_prop(crtc->dev, supported_filters);
  813. if (IS_ERR(prop))
  814. return PTR_ERR(prop);
  815. drm_object_attach_property(&crtc->base, prop,
  816. DRM_SCALING_FILTER_DEFAULT);
  817. crtc->scaling_filter_property = prop;
  818. return 0;
  819. }
  820. EXPORT_SYMBOL(drm_crtc_create_scaling_filter_property);
  821. int drm_crtc_create_sharpness_strength_property(struct drm_crtc *crtc)
  822. {
  823. struct drm_device *dev = crtc->dev;
  824. struct drm_property *prop =
  825. drm_property_create_range(dev, 0, "SHARPNESS_STRENGTH", 0, 255);
  826. if (!prop)
  827. return -ENOMEM;
  828. crtc->sharpness_strength_property = prop;
  829. drm_object_attach_property(&crtc->base, prop, 0);
  830. return 0;
  831. }
  832. EXPORT_SYMBOL(drm_crtc_create_sharpness_strength_property);
  833. /**
  834. * drm_crtc_in_clone_mode - check if the given CRTC state is in clone mode
  835. *
  836. * @crtc_state: CRTC state to check
  837. *
  838. * This function determines if the given CRTC state is being cloned by multiple
  839. * encoders.
  840. *
  841. * RETURNS:
  842. * True if the CRTC state is in clone mode. False otherwise
  843. */
  844. bool drm_crtc_in_clone_mode(struct drm_crtc_state *crtc_state)
  845. {
  846. if (!crtc_state)
  847. return false;
  848. return hweight32(crtc_state->encoder_mask) > 1;
  849. }
  850. EXPORT_SYMBOL(drm_crtc_in_clone_mode);