drm_crtc.c 26 KB

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