drm_crtc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  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(sizeof(*fence), GFP_KERNEL);
  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. */
  205. __printf(6, 0)
  206. static int __drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
  207. struct drm_plane *primary,
  208. struct drm_plane *cursor,
  209. const struct drm_crtc_funcs *funcs,
  210. const char *name, va_list ap)
  211. {
  212. struct drm_mode_config *config = &dev->mode_config;
  213. int ret;
  214. WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
  215. WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
  216. /* crtc index is used with 32bit bitmasks */
  217. if (WARN_ON(config->num_crtc >= 32))
  218. return -EINVAL;
  219. WARN_ON(drm_drv_uses_atomic_modeset(dev) &&
  220. (!funcs->atomic_destroy_state ||
  221. !funcs->atomic_duplicate_state));
  222. crtc->dev = dev;
  223. crtc->funcs = funcs;
  224. INIT_LIST_HEAD(&crtc->commit_list);
  225. spin_lock_init(&crtc->commit_lock);
  226. drm_modeset_lock_init(&crtc->mutex);
  227. ret = drm_mode_object_add(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
  228. if (ret)
  229. return ret;
  230. if (name) {
  231. crtc->name = kvasprintf(GFP_KERNEL, name, ap);
  232. } else {
  233. crtc->name = kasprintf(GFP_KERNEL, "crtc-%d", config->num_crtc);
  234. }
  235. if (!crtc->name) {
  236. drm_mode_object_unregister(dev, &crtc->base);
  237. return -ENOMEM;
  238. }
  239. crtc->fence_context = dma_fence_context_alloc(1);
  240. spin_lock_init(&crtc->fence_lock);
  241. snprintf(crtc->timeline_name, sizeof(crtc->timeline_name),
  242. "CRTC:%d-%s", crtc->base.id, crtc->name);
  243. crtc->base.properties = &crtc->properties;
  244. list_add_tail(&crtc->head, &config->crtc_list);
  245. crtc->index = config->num_crtc++;
  246. crtc->primary = primary;
  247. crtc->cursor = cursor;
  248. if (primary && !primary->possible_crtcs)
  249. primary->possible_crtcs = drm_crtc_mask(crtc);
  250. if (cursor && !cursor->possible_crtcs)
  251. cursor->possible_crtcs = drm_crtc_mask(crtc);
  252. ret = drm_crtc_crc_init(crtc);
  253. if (ret) {
  254. drm_mode_object_unregister(dev, &crtc->base);
  255. return ret;
  256. }
  257. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  258. drm_object_attach_property(&crtc->base, config->prop_active, 0);
  259. drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
  260. drm_object_attach_property(&crtc->base,
  261. config->prop_out_fence_ptr, 0);
  262. drm_object_attach_property(&crtc->base,
  263. config->prop_vrr_enabled, 0);
  264. }
  265. return 0;
  266. }
  267. /**
  268. * drm_crtc_init_with_planes - Initialise a new CRTC object with
  269. * specified primary and cursor planes.
  270. * @dev: DRM device
  271. * @crtc: CRTC object to init
  272. * @primary: Primary plane for CRTC
  273. * @cursor: Cursor plane for CRTC
  274. * @funcs: callbacks for the new CRTC
  275. * @name: printf style format string for the CRTC name, or NULL for default name
  276. *
  277. * Inits a new object created as base part of a driver crtc object. Drivers
  278. * should use this function instead of drm_crtc_init(), which is only provided
  279. * for backwards compatibility with drivers which do not yet support universal
  280. * planes). For really simple hardware which has only 1 plane look at
  281. * drm_simple_display_pipe_init() instead.
  282. * The &drm_crtc_funcs.destroy hook should call drm_crtc_cleanup() and kfree()
  283. * the crtc structure. The crtc structure should not be allocated with
  284. * devm_kzalloc().
  285. *
  286. * The @primary and @cursor planes are only relevant for legacy uAPI, see
  287. * &drm_crtc.primary and &drm_crtc.cursor.
  288. *
  289. * Note: consider using drmm_crtc_alloc_with_planes() or
  290. * drmm_crtc_init_with_planes() instead of drm_crtc_init_with_planes()
  291. * to let the DRM managed resource infrastructure take care of cleanup
  292. * and deallocation.
  293. *
  294. * Returns:
  295. * Zero on success, error code on failure.
  296. */
  297. int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
  298. struct drm_plane *primary,
  299. struct drm_plane *cursor,
  300. const struct drm_crtc_funcs *funcs,
  301. const char *name, ...)
  302. {
  303. va_list ap;
  304. int ret;
  305. WARN_ON(!funcs->destroy);
  306. va_start(ap, name);
  307. ret = __drm_crtc_init_with_planes(dev, crtc, primary, cursor, funcs,
  308. name, ap);
  309. va_end(ap);
  310. return ret;
  311. }
  312. EXPORT_SYMBOL(drm_crtc_init_with_planes);
  313. static void drmm_crtc_init_with_planes_cleanup(struct drm_device *dev,
  314. void *ptr)
  315. {
  316. struct drm_crtc *crtc = ptr;
  317. drm_crtc_cleanup(crtc);
  318. }
  319. __printf(6, 0)
  320. static int __drmm_crtc_init_with_planes(struct drm_device *dev,
  321. struct drm_crtc *crtc,
  322. struct drm_plane *primary,
  323. struct drm_plane *cursor,
  324. const struct drm_crtc_funcs *funcs,
  325. const char *name,
  326. va_list args)
  327. {
  328. int ret;
  329. drm_WARN_ON(dev, funcs && funcs->destroy);
  330. ret = __drm_crtc_init_with_planes(dev, crtc, primary, cursor, funcs,
  331. name, args);
  332. if (ret)
  333. return ret;
  334. ret = drmm_add_action_or_reset(dev, drmm_crtc_init_with_planes_cleanup,
  335. crtc);
  336. if (ret)
  337. return ret;
  338. return 0;
  339. }
  340. /**
  341. * drmm_crtc_init_with_planes - Initialise a new CRTC object with
  342. * specified primary and cursor planes.
  343. * @dev: DRM device
  344. * @crtc: CRTC object to init
  345. * @primary: Primary plane for CRTC
  346. * @cursor: Cursor plane for CRTC
  347. * @funcs: callbacks for the new CRTC
  348. * @name: printf style format string for the CRTC name, or NULL for default name
  349. *
  350. * Inits a new object created as base part of a driver crtc object. Drivers
  351. * should use this function instead of drm_crtc_init(), which is only provided
  352. * for backwards compatibility with drivers which do not yet support universal
  353. * planes). For really simple hardware which has only 1 plane look at
  354. * drm_simple_display_pipe_init() instead.
  355. *
  356. * Cleanup is automatically handled through registering
  357. * drmm_crtc_cleanup() with drmm_add_action(). The crtc structure should
  358. * be allocated with drmm_kzalloc().
  359. *
  360. * The @drm_crtc_funcs.destroy hook must be NULL.
  361. *
  362. * The @primary and @cursor planes are only relevant for legacy uAPI, see
  363. * &drm_crtc.primary and &drm_crtc.cursor.
  364. *
  365. * Returns:
  366. * Zero on success, error code on failure.
  367. */
  368. int drmm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
  369. struct drm_plane *primary,
  370. struct drm_plane *cursor,
  371. const struct drm_crtc_funcs *funcs,
  372. const char *name, ...)
  373. {
  374. va_list ap;
  375. int ret;
  376. va_start(ap, name);
  377. ret = __drmm_crtc_init_with_planes(dev, crtc, primary, cursor, funcs,
  378. name, ap);
  379. va_end(ap);
  380. if (ret)
  381. return ret;
  382. return 0;
  383. }
  384. EXPORT_SYMBOL(drmm_crtc_init_with_planes);
  385. void *__drmm_crtc_alloc_with_planes(struct drm_device *dev,
  386. size_t size, size_t offset,
  387. struct drm_plane *primary,
  388. struct drm_plane *cursor,
  389. const struct drm_crtc_funcs *funcs,
  390. const char *name, ...)
  391. {
  392. void *container;
  393. struct drm_crtc *crtc;
  394. va_list ap;
  395. int ret;
  396. if (WARN_ON(!funcs || funcs->destroy))
  397. return ERR_PTR(-EINVAL);
  398. container = drmm_kzalloc(dev, size, GFP_KERNEL);
  399. if (!container)
  400. return ERR_PTR(-ENOMEM);
  401. crtc = container + offset;
  402. va_start(ap, name);
  403. ret = __drmm_crtc_init_with_planes(dev, crtc, primary, cursor, funcs,
  404. name, ap);
  405. va_end(ap);
  406. if (ret)
  407. return ERR_PTR(ret);
  408. return container;
  409. }
  410. EXPORT_SYMBOL(__drmm_crtc_alloc_with_planes);
  411. /**
  412. * drm_crtc_cleanup - Clean up the core crtc usage
  413. * @crtc: CRTC to cleanup
  414. *
  415. * This function cleans up @crtc and removes it from the DRM mode setting
  416. * core. Note that the function does *not* free the crtc structure itself,
  417. * this is the responsibility of the caller.
  418. */
  419. void drm_crtc_cleanup(struct drm_crtc *crtc)
  420. {
  421. struct drm_device *dev = crtc->dev;
  422. /* Note that the crtc_list is considered to be static; should we
  423. * remove the drm_crtc at runtime we would have to decrement all
  424. * the indices on the drm_crtc after us in the crtc_list.
  425. */
  426. drm_crtc_crc_fini(crtc);
  427. kfree(crtc->gamma_store);
  428. crtc->gamma_store = NULL;
  429. drm_modeset_lock_fini(&crtc->mutex);
  430. drm_mode_object_unregister(dev, &crtc->base);
  431. list_del(&crtc->head);
  432. dev->mode_config.num_crtc--;
  433. WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
  434. if (crtc->state && crtc->funcs->atomic_destroy_state)
  435. crtc->funcs->atomic_destroy_state(crtc, crtc->state);
  436. kfree(crtc->name);
  437. memset(crtc, 0, sizeof(*crtc));
  438. }
  439. EXPORT_SYMBOL(drm_crtc_cleanup);
  440. /**
  441. * drm_mode_getcrtc - get CRTC configuration
  442. * @dev: drm device for the ioctl
  443. * @data: data pointer for the ioctl
  444. * @file_priv: drm file for the ioctl call
  445. *
  446. * Construct a CRTC configuration structure to return to the user.
  447. *
  448. * Called by the user via ioctl.
  449. *
  450. * Returns:
  451. * Zero on success, negative errno on failure.
  452. */
  453. int drm_mode_getcrtc(struct drm_device *dev,
  454. void *data, struct drm_file *file_priv)
  455. {
  456. struct drm_mode_crtc *crtc_resp = data;
  457. struct drm_crtc *crtc;
  458. struct drm_plane *plane;
  459. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  460. return -EOPNOTSUPP;
  461. crtc = drm_crtc_find(dev, file_priv, crtc_resp->crtc_id);
  462. if (!crtc)
  463. return -ENOENT;
  464. plane = crtc->primary;
  465. crtc_resp->gamma_size = crtc->gamma_size;
  466. drm_modeset_lock(&plane->mutex, NULL);
  467. if (plane->state && plane->state->fb)
  468. crtc_resp->fb_id = plane->state->fb->base.id;
  469. else if (!plane->state && plane->fb)
  470. crtc_resp->fb_id = plane->fb->base.id;
  471. else
  472. crtc_resp->fb_id = 0;
  473. if (plane->state) {
  474. crtc_resp->x = plane->state->src_x >> 16;
  475. crtc_resp->y = plane->state->src_y >> 16;
  476. }
  477. drm_modeset_unlock(&plane->mutex);
  478. drm_modeset_lock(&crtc->mutex, NULL);
  479. if (crtc->state) {
  480. if (crtc->state->enable) {
  481. drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
  482. crtc_resp->mode_valid = 1;
  483. } else {
  484. crtc_resp->mode_valid = 0;
  485. }
  486. } else {
  487. crtc_resp->x = crtc->x;
  488. crtc_resp->y = crtc->y;
  489. if (crtc->enabled) {
  490. drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
  491. crtc_resp->mode_valid = 1;
  492. } else {
  493. crtc_resp->mode_valid = 0;
  494. }
  495. }
  496. if (!file_priv->aspect_ratio_allowed)
  497. crtc_resp->mode.flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
  498. drm_modeset_unlock(&crtc->mutex);
  499. return 0;
  500. }
  501. static int __drm_mode_set_config_internal(struct drm_mode_set *set,
  502. struct drm_modeset_acquire_ctx *ctx)
  503. {
  504. struct drm_crtc *crtc = set->crtc;
  505. struct drm_framebuffer *fb;
  506. struct drm_crtc *tmp;
  507. int ret;
  508. WARN_ON(drm_drv_uses_atomic_modeset(crtc->dev));
  509. /*
  510. * NOTE: ->set_config can also disable other crtcs (if we steal all
  511. * connectors from it), hence we need to refcount the fbs across all
  512. * crtcs. Atomic modeset will have saner semantics ...
  513. */
  514. drm_for_each_crtc(tmp, crtc->dev) {
  515. struct drm_plane *plane = tmp->primary;
  516. plane->old_fb = plane->fb;
  517. }
  518. fb = set->fb;
  519. ret = crtc->funcs->set_config(set, ctx);
  520. if (ret == 0) {
  521. struct drm_plane *plane = crtc->primary;
  522. plane->crtc = fb ? crtc : NULL;
  523. plane->fb = fb;
  524. }
  525. drm_for_each_crtc(tmp, crtc->dev) {
  526. struct drm_plane *plane = tmp->primary;
  527. if (plane->fb)
  528. drm_framebuffer_get(plane->fb);
  529. if (plane->old_fb)
  530. drm_framebuffer_put(plane->old_fb);
  531. plane->old_fb = NULL;
  532. }
  533. return ret;
  534. }
  535. /**
  536. * drm_mode_set_config_internal - helper to call &drm_mode_config_funcs.set_config
  537. * @set: modeset config to set
  538. *
  539. * This is a little helper to wrap internal calls to the
  540. * &drm_mode_config_funcs.set_config driver interface. The only thing it adds is
  541. * correct refcounting dance.
  542. *
  543. * This should only be used by non-atomic legacy drivers.
  544. *
  545. * Returns:
  546. * Zero on success, negative errno on failure.
  547. */
  548. int drm_mode_set_config_internal(struct drm_mode_set *set)
  549. {
  550. WARN_ON(drm_drv_uses_atomic_modeset(set->crtc->dev));
  551. return __drm_mode_set_config_internal(set, NULL);
  552. }
  553. EXPORT_SYMBOL(drm_mode_set_config_internal);
  554. /**
  555. * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
  556. * CRTC viewport
  557. * @crtc: CRTC that framebuffer will be displayed on
  558. * @x: x panning
  559. * @y: y panning
  560. * @mode: mode that framebuffer will be displayed under
  561. * @fb: framebuffer to check size of
  562. */
  563. int drm_crtc_check_viewport(const struct drm_crtc *crtc,
  564. int x, int y,
  565. const struct drm_display_mode *mode,
  566. const struct drm_framebuffer *fb)
  567. {
  568. int hdisplay, vdisplay;
  569. drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
  570. if (crtc->state &&
  571. drm_rotation_90_or_270(crtc->primary->state->rotation))
  572. swap(hdisplay, vdisplay);
  573. return drm_framebuffer_check_src_coords(x << 16, y << 16,
  574. hdisplay << 16, vdisplay << 16,
  575. fb);
  576. }
  577. EXPORT_SYMBOL(drm_crtc_check_viewport);
  578. /**
  579. * drm_mode_setcrtc - set CRTC configuration
  580. * @dev: drm device for the ioctl
  581. * @data: data pointer for the ioctl
  582. * @file_priv: drm file for the ioctl call
  583. *
  584. * Build a new CRTC configuration based on user request.
  585. *
  586. * Called by the user via ioctl.
  587. *
  588. * Returns:
  589. * Zero on success, negative errno on failure.
  590. */
  591. int drm_mode_setcrtc(struct drm_device *dev, void *data,
  592. struct drm_file *file_priv)
  593. {
  594. struct drm_mode_config *config = &dev->mode_config;
  595. struct drm_mode_crtc *crtc_req = data;
  596. struct drm_crtc *crtc;
  597. struct drm_plane *plane;
  598. struct drm_connector **connector_set = NULL, *connector;
  599. struct drm_framebuffer *fb = NULL;
  600. struct drm_display_mode *mode = NULL;
  601. struct drm_mode_set set;
  602. uint32_t __user *set_connectors_ptr;
  603. struct drm_modeset_acquire_ctx ctx;
  604. int ret, i, num_connectors = 0;
  605. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  606. return -EOPNOTSUPP;
  607. /*
  608. * Universal plane src offsets are only 16.16, prevent havoc for
  609. * drivers using universal plane code internally.
  610. */
  611. if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
  612. return -ERANGE;
  613. crtc = drm_crtc_find(dev, file_priv, crtc_req->crtc_id);
  614. if (!crtc) {
  615. drm_dbg_kms(dev, "Unknown CRTC ID %d\n", crtc_req->crtc_id);
  616. return -ENOENT;
  617. }
  618. drm_dbg_kms(dev, "[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  619. plane = crtc->primary;
  620. /* allow disabling with the primary plane leased */
  621. if (crtc_req->mode_valid && !drm_lease_held(file_priv, plane->base.id))
  622. return -EACCES;
  623. DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx,
  624. DRM_MODESET_ACQUIRE_INTERRUPTIBLE, ret);
  625. if (crtc_req->mode_valid) {
  626. /* If we have a mode we need a framebuffer. */
  627. /* If we pass -1, set the mode with the currently bound fb */
  628. if (crtc_req->fb_id == -1) {
  629. struct drm_framebuffer *old_fb;
  630. if (plane->state)
  631. old_fb = plane->state->fb;
  632. else
  633. old_fb = plane->fb;
  634. if (!old_fb) {
  635. drm_dbg_kms(dev, "CRTC doesn't have current FB\n");
  636. ret = -EINVAL;
  637. goto out;
  638. }
  639. fb = old_fb;
  640. /* Make refcounting symmetric with the lookup path. */
  641. drm_framebuffer_get(fb);
  642. } else {
  643. fb = drm_framebuffer_lookup(dev, file_priv, crtc_req->fb_id);
  644. if (!fb) {
  645. drm_dbg_kms(dev, "Unknown FB ID%d\n",
  646. crtc_req->fb_id);
  647. ret = -ENOENT;
  648. goto out;
  649. }
  650. }
  651. mode = drm_mode_create(dev);
  652. if (!mode) {
  653. ret = -ENOMEM;
  654. goto out;
  655. }
  656. if (!file_priv->aspect_ratio_allowed &&
  657. (crtc_req->mode.flags & DRM_MODE_FLAG_PIC_AR_MASK) != DRM_MODE_FLAG_PIC_AR_NONE) {
  658. drm_dbg_kms(dev, "Unexpected aspect-ratio flag bits\n");
  659. ret = -EINVAL;
  660. goto out;
  661. }
  662. ret = drm_mode_convert_umode(dev, mode, &crtc_req->mode);
  663. if (ret) {
  664. drm_dbg_kms(dev, "Invalid mode (%s, %pe): " DRM_MODE_FMT "\n",
  665. drm_get_mode_status_name(mode->status),
  666. ERR_PTR(ret), DRM_MODE_ARG(mode));
  667. goto out;
  668. }
  669. /*
  670. * Check whether the primary plane supports the fb pixel format.
  671. * Drivers not implementing the universal planes API use a
  672. * default formats list provided by the DRM core which doesn't
  673. * match real hardware capabilities. Skip the check in that
  674. * case.
  675. */
  676. if (!plane->format_default) {
  677. if (!drm_plane_has_format(plane, fb->format->format, fb->modifier)) {
  678. drm_dbg_kms(dev, "Invalid pixel format %p4cc, modifier 0x%llx\n",
  679. &fb->format->format, fb->modifier);
  680. ret = -EINVAL;
  681. goto out;
  682. }
  683. }
  684. ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
  685. mode, fb);
  686. if (ret)
  687. goto out;
  688. }
  689. if (crtc_req->count_connectors == 0 && mode) {
  690. drm_dbg_kms(dev, "Count connectors is 0 but mode set\n");
  691. ret = -EINVAL;
  692. goto out;
  693. }
  694. if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
  695. drm_dbg_kms(dev, "Count connectors is %d but no mode or fb set\n",
  696. crtc_req->count_connectors);
  697. ret = -EINVAL;
  698. goto out;
  699. }
  700. if (crtc_req->count_connectors > 0) {
  701. u32 out_id;
  702. /* Avoid unbounded kernel memory allocation */
  703. if (crtc_req->count_connectors > config->num_connector) {
  704. ret = -EINVAL;
  705. goto out;
  706. }
  707. connector_set = kmalloc_array(crtc_req->count_connectors,
  708. sizeof(struct drm_connector *),
  709. GFP_KERNEL);
  710. if (!connector_set) {
  711. ret = -ENOMEM;
  712. goto out;
  713. }
  714. for (i = 0; i < crtc_req->count_connectors; i++) {
  715. connector_set[i] = NULL;
  716. set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
  717. if (get_user(out_id, &set_connectors_ptr[i])) {
  718. ret = -EFAULT;
  719. goto out;
  720. }
  721. connector = drm_connector_lookup(dev, file_priv, out_id);
  722. if (!connector) {
  723. drm_dbg_kms(dev, "Connector id %d unknown\n",
  724. out_id);
  725. ret = -ENOENT;
  726. goto out;
  727. }
  728. drm_dbg_kms(dev, "[CONNECTOR:%d:%s]\n",
  729. connector->base.id, connector->name);
  730. connector_set[i] = connector;
  731. num_connectors++;
  732. }
  733. }
  734. set.crtc = crtc;
  735. set.x = crtc_req->x;
  736. set.y = crtc_req->y;
  737. set.mode = mode;
  738. set.connectors = connector_set;
  739. set.num_connectors = num_connectors;
  740. set.fb = fb;
  741. if (drm_drv_uses_atomic_modeset(dev))
  742. ret = crtc->funcs->set_config(&set, &ctx);
  743. else
  744. ret = __drm_mode_set_config_internal(&set, &ctx);
  745. out:
  746. if (fb)
  747. drm_framebuffer_put(fb);
  748. if (connector_set) {
  749. for (i = 0; i < num_connectors; i++) {
  750. if (connector_set[i])
  751. drm_connector_put(connector_set[i]);
  752. }
  753. }
  754. kfree(connector_set);
  755. drm_mode_destroy(dev, mode);
  756. /* In case we need to retry... */
  757. connector_set = NULL;
  758. fb = NULL;
  759. mode = NULL;
  760. num_connectors = 0;
  761. DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
  762. return ret;
  763. }
  764. int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
  765. struct drm_property *property,
  766. uint64_t value)
  767. {
  768. int ret = -EINVAL;
  769. struct drm_crtc *crtc = obj_to_crtc(obj);
  770. if (crtc->funcs->set_property)
  771. ret = crtc->funcs->set_property(crtc, property, value);
  772. if (!ret)
  773. drm_object_property_set_value(obj, property, value);
  774. return ret;
  775. }
  776. /**
  777. * drm_crtc_create_scaling_filter_property - create a new scaling filter
  778. * property
  779. *
  780. * @crtc: drm CRTC
  781. * @supported_filters: bitmask of supported scaling filters, must include
  782. * BIT(DRM_SCALING_FILTER_DEFAULT).
  783. *
  784. * This function lets driver to enable the scaling filter property on a given
  785. * CRTC.
  786. *
  787. * RETURNS:
  788. * Zero for success or -errno
  789. */
  790. int drm_crtc_create_scaling_filter_property(struct drm_crtc *crtc,
  791. unsigned int supported_filters)
  792. {
  793. struct drm_property *prop =
  794. drm_create_scaling_filter_prop(crtc->dev, supported_filters);
  795. if (IS_ERR(prop))
  796. return PTR_ERR(prop);
  797. drm_object_attach_property(&crtc->base, prop,
  798. DRM_SCALING_FILTER_DEFAULT);
  799. crtc->scaling_filter_property = prop;
  800. return 0;
  801. }
  802. EXPORT_SYMBOL(drm_crtc_create_scaling_filter_property);