drm_plane.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. /*
  2. * Copyright (c) 2016 Intel Corporation
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and its
  5. * documentation for any purpose is hereby granted without fee, provided that
  6. * the above copyright notice appear in all copies and that both that copyright
  7. * notice and this permission notice appear in supporting documentation, and
  8. * that the name of the copyright holders not be used in advertising or
  9. * publicity pertaining to distribution of the software without specific,
  10. * written prior permission. The copyright holders make no representations
  11. * about the suitability of this software for any purpose. It is provided "as
  12. * is" without express or implied warranty.
  13. *
  14. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  19. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  20. * OF THIS SOFTWARE.
  21. */
  22. #include <drm/drmP.h>
  23. #include <drm/drm_plane.h>
  24. #include "drm_crtc_internal.h"
  25. /**
  26. * DOC: overview
  27. *
  28. * A plane represents an image source that can be blended with or overlayed on
  29. * top of a CRTC during the scanout process. Planes take their input data from a
  30. * &drm_framebuffer object. The plane itself specifies the cropping and scaling
  31. * of that image, and where it is placed on the visible are of a display
  32. * pipeline, represented by &drm_crtc. A plane can also have additional
  33. * properties that specify how the pixels are positioned and blended, like
  34. * rotation or Z-position. All these properties are stored in &drm_plane_state.
  35. *
  36. * To create a plane, a KMS drivers allocates and zeroes an instances of
  37. * &struct drm_plane (possibly as part of a larger structure) and registers it
  38. * with a call to drm_universal_plane_init().
  39. *
  40. * Cursor and overlay planes are optional. All drivers should provide one
  41. * primary plane per CRTC to avoid surprising userspace too much. See enum
  42. * drm_plane_type for a more in-depth discussion of these special uapi-relevant
  43. * plane types. Special planes are associated with their CRTC by calling
  44. * drm_crtc_init_with_planes().
  45. *
  46. * The type of a plane is exposed in the immutable "type" enumeration property,
  47. * which has one of the following values: "Overlay", "Primary", "Cursor".
  48. */
  49. static unsigned int drm_num_planes(struct drm_device *dev)
  50. {
  51. unsigned int num = 0;
  52. struct drm_plane *tmp;
  53. drm_for_each_plane(tmp, dev) {
  54. num++;
  55. }
  56. return num;
  57. }
  58. static inline u32 *
  59. formats_ptr(struct drm_format_modifier_blob *blob)
  60. {
  61. return (u32 *)(((char *)blob) + blob->formats_offset);
  62. }
  63. static inline struct drm_format_modifier *
  64. modifiers_ptr(struct drm_format_modifier_blob *blob)
  65. {
  66. return (struct drm_format_modifier *)(((char *)blob) + blob->modifiers_offset);
  67. }
  68. static int create_in_format_blob(struct drm_device *dev, struct drm_plane *plane)
  69. {
  70. const struct drm_mode_config *config = &dev->mode_config;
  71. struct drm_property_blob *blob;
  72. struct drm_format_modifier *mod;
  73. size_t blob_size, formats_size, modifiers_size;
  74. struct drm_format_modifier_blob *blob_data;
  75. unsigned int i, j;
  76. formats_size = sizeof(__u32) * plane->format_count;
  77. if (WARN_ON(!formats_size)) {
  78. /* 0 formats are never expected */
  79. return 0;
  80. }
  81. modifiers_size =
  82. sizeof(struct drm_format_modifier) * plane->modifier_count;
  83. blob_size = sizeof(struct drm_format_modifier_blob);
  84. /* Modifiers offset is a pointer to a struct with a 64 bit field so it
  85. * should be naturally aligned to 8B.
  86. */
  87. BUILD_BUG_ON(sizeof(struct drm_format_modifier_blob) % 8);
  88. blob_size += ALIGN(formats_size, 8);
  89. blob_size += modifiers_size;
  90. blob = drm_property_create_blob(dev, blob_size, NULL);
  91. if (IS_ERR(blob))
  92. return -1;
  93. blob_data = (struct drm_format_modifier_blob *)blob->data;
  94. blob_data->version = FORMAT_BLOB_CURRENT;
  95. blob_data->count_formats = plane->format_count;
  96. blob_data->formats_offset = sizeof(struct drm_format_modifier_blob);
  97. blob_data->count_modifiers = plane->modifier_count;
  98. blob_data->modifiers_offset =
  99. ALIGN(blob_data->formats_offset + formats_size, 8);
  100. memcpy(formats_ptr(blob_data), plane->format_types, formats_size);
  101. /* If we can't determine support, just bail */
  102. if (!plane->funcs->format_mod_supported)
  103. goto done;
  104. mod = modifiers_ptr(blob_data);
  105. for (i = 0; i < plane->modifier_count; i++) {
  106. for (j = 0; j < plane->format_count; j++) {
  107. if (plane->funcs->format_mod_supported(plane,
  108. plane->format_types[j],
  109. plane->modifiers[i])) {
  110. mod->formats |= 1ULL << j;
  111. }
  112. }
  113. mod->modifier = plane->modifiers[i];
  114. mod->offset = 0;
  115. mod->pad = 0;
  116. mod++;
  117. }
  118. done:
  119. drm_object_attach_property(&plane->base, config->modifiers_property,
  120. blob->base.id);
  121. return 0;
  122. }
  123. /**
  124. * drm_universal_plane_init - Initialize a new universal plane object
  125. * @dev: DRM device
  126. * @plane: plane object to init
  127. * @possible_crtcs: bitmask of possible CRTCs
  128. * @funcs: callbacks for the new plane
  129. * @formats: array of supported formats (DRM_FORMAT\_\*)
  130. * @format_count: number of elements in @formats
  131. * @format_modifiers: array of struct drm_format modifiers terminated by
  132. * DRM_FORMAT_MOD_INVALID
  133. * @type: type of plane (overlay, primary, cursor)
  134. * @name: printf style format string for the plane name, or NULL for default name
  135. *
  136. * Initializes a plane object of type @type.
  137. *
  138. * Returns:
  139. * Zero on success, error code on failure.
  140. */
  141. int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
  142. uint32_t possible_crtcs,
  143. const struct drm_plane_funcs *funcs,
  144. const uint32_t *formats, unsigned int format_count,
  145. const uint64_t *format_modifiers,
  146. enum drm_plane_type type,
  147. const char *name, ...)
  148. {
  149. struct drm_mode_config *config = &dev->mode_config;
  150. unsigned int format_modifier_count = 0;
  151. int ret;
  152. /* plane index is used with 32bit bitmasks */
  153. if (WARN_ON(config->num_total_plane >= 32))
  154. return -EINVAL;
  155. ret = drm_mode_object_add(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
  156. if (ret)
  157. return ret;
  158. drm_modeset_lock_init(&plane->mutex);
  159. plane->base.properties = &plane->properties;
  160. plane->dev = dev;
  161. plane->funcs = funcs;
  162. plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
  163. GFP_KERNEL);
  164. if (!plane->format_types) {
  165. DRM_DEBUG_KMS("out of memory when allocating plane\n");
  166. drm_mode_object_unregister(dev, &plane->base);
  167. return -ENOMEM;
  168. }
  169. /*
  170. * First driver to need more than 64 formats needs to fix this. Each
  171. * format is encoded as a bit and the current code only supports a u64.
  172. */
  173. if (WARN_ON(format_count > 64))
  174. return -EINVAL;
  175. if (format_modifiers) {
  176. const uint64_t *temp_modifiers = format_modifiers;
  177. while (*temp_modifiers++ != DRM_FORMAT_MOD_INVALID)
  178. format_modifier_count++;
  179. }
  180. plane->modifier_count = format_modifier_count;
  181. plane->modifiers = kmalloc_array(format_modifier_count,
  182. sizeof(format_modifiers[0]),
  183. GFP_KERNEL);
  184. if (format_modifier_count && !plane->modifiers) {
  185. DRM_DEBUG_KMS("out of memory when allocating plane\n");
  186. kfree(plane->format_types);
  187. drm_mode_object_unregister(dev, &plane->base);
  188. return -ENOMEM;
  189. }
  190. if (name) {
  191. va_list ap;
  192. va_start(ap, name);
  193. plane->name = kvasprintf(GFP_KERNEL, name, ap);
  194. va_end(ap);
  195. } else {
  196. plane->name = kasprintf(GFP_KERNEL, "plane-%d",
  197. drm_num_planes(dev));
  198. }
  199. if (!plane->name) {
  200. kfree(plane->format_types);
  201. kfree(plane->modifiers);
  202. drm_mode_object_unregister(dev, &plane->base);
  203. return -ENOMEM;
  204. }
  205. memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
  206. plane->format_count = format_count;
  207. memcpy(plane->modifiers, format_modifiers,
  208. format_modifier_count * sizeof(format_modifiers[0]));
  209. plane->possible_crtcs = possible_crtcs;
  210. plane->type = type;
  211. list_add_tail(&plane->head, &config->plane_list);
  212. plane->index = config->num_total_plane++;
  213. drm_object_attach_property(&plane->base,
  214. config->plane_type_property,
  215. plane->type);
  216. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  217. drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
  218. drm_object_attach_property(&plane->base, config->prop_in_fence_fd, -1);
  219. drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
  220. drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
  221. drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
  222. drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
  223. drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
  224. drm_object_attach_property(&plane->base, config->prop_src_x, 0);
  225. drm_object_attach_property(&plane->base, config->prop_src_y, 0);
  226. drm_object_attach_property(&plane->base, config->prop_src_w, 0);
  227. drm_object_attach_property(&plane->base, config->prop_src_h, 0);
  228. }
  229. if (config->allow_fb_modifiers)
  230. create_in_format_blob(dev, plane);
  231. return 0;
  232. }
  233. EXPORT_SYMBOL(drm_universal_plane_init);
  234. int drm_plane_register_all(struct drm_device *dev)
  235. {
  236. struct drm_plane *plane;
  237. int ret = 0;
  238. drm_for_each_plane(plane, dev) {
  239. if (plane->funcs->late_register)
  240. ret = plane->funcs->late_register(plane);
  241. if (ret)
  242. return ret;
  243. }
  244. return 0;
  245. }
  246. void drm_plane_unregister_all(struct drm_device *dev)
  247. {
  248. struct drm_plane *plane;
  249. drm_for_each_plane(plane, dev) {
  250. if (plane->funcs->early_unregister)
  251. plane->funcs->early_unregister(plane);
  252. }
  253. }
  254. /**
  255. * drm_plane_init - Initialize a legacy plane
  256. * @dev: DRM device
  257. * @plane: plane object to init
  258. * @possible_crtcs: bitmask of possible CRTCs
  259. * @funcs: callbacks for the new plane
  260. * @formats: array of supported formats (DRM_FORMAT\_\*)
  261. * @format_count: number of elements in @formats
  262. * @is_primary: plane type (primary vs overlay)
  263. *
  264. * Legacy API to initialize a DRM plane.
  265. *
  266. * New drivers should call drm_universal_plane_init() instead.
  267. *
  268. * Returns:
  269. * Zero on success, error code on failure.
  270. */
  271. int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
  272. uint32_t possible_crtcs,
  273. const struct drm_plane_funcs *funcs,
  274. const uint32_t *formats, unsigned int format_count,
  275. bool is_primary)
  276. {
  277. enum drm_plane_type type;
  278. type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
  279. return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
  280. formats, format_count,
  281. NULL, type, NULL);
  282. }
  283. EXPORT_SYMBOL(drm_plane_init);
  284. /**
  285. * drm_plane_cleanup - Clean up the core plane usage
  286. * @plane: plane to cleanup
  287. *
  288. * This function cleans up @plane and removes it from the DRM mode setting
  289. * core. Note that the function does *not* free the plane structure itself,
  290. * this is the responsibility of the caller.
  291. */
  292. void drm_plane_cleanup(struct drm_plane *plane)
  293. {
  294. struct drm_device *dev = plane->dev;
  295. drm_modeset_lock_fini(&plane->mutex);
  296. kfree(plane->format_types);
  297. kfree(plane->modifiers);
  298. drm_mode_object_unregister(dev, &plane->base);
  299. BUG_ON(list_empty(&plane->head));
  300. /* Note that the plane_list is considered to be static; should we
  301. * remove the drm_plane at runtime we would have to decrement all
  302. * the indices on the drm_plane after us in the plane_list.
  303. */
  304. list_del(&plane->head);
  305. dev->mode_config.num_total_plane--;
  306. WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
  307. if (plane->state && plane->funcs->atomic_destroy_state)
  308. plane->funcs->atomic_destroy_state(plane, plane->state);
  309. kfree(plane->name);
  310. memset(plane, 0, sizeof(*plane));
  311. }
  312. EXPORT_SYMBOL(drm_plane_cleanup);
  313. /**
  314. * drm_plane_from_index - find the registered plane at an index
  315. * @dev: DRM device
  316. * @idx: index of registered plane to find for
  317. *
  318. * Given a plane index, return the registered plane from DRM device's
  319. * list of planes with matching index. This is the inverse of drm_plane_index().
  320. */
  321. struct drm_plane *
  322. drm_plane_from_index(struct drm_device *dev, int idx)
  323. {
  324. struct drm_plane *plane;
  325. drm_for_each_plane(plane, dev)
  326. if (idx == plane->index)
  327. return plane;
  328. return NULL;
  329. }
  330. EXPORT_SYMBOL(drm_plane_from_index);
  331. /**
  332. * drm_plane_force_disable - Forcibly disable a plane
  333. * @plane: plane to disable
  334. *
  335. * Forces the plane to be disabled.
  336. *
  337. * Used when the plane's current framebuffer is destroyed,
  338. * and when restoring fbdev mode.
  339. *
  340. * Note that this function is not suitable for atomic drivers, since it doesn't
  341. * wire through the lock acquisition context properly and hence can't handle
  342. * retries or driver private locks. You probably want to use
  343. * drm_atomic_helper_disable_plane() or
  344. * drm_atomic_helper_disable_planes_on_crtc() instead.
  345. */
  346. void drm_plane_force_disable(struct drm_plane *plane)
  347. {
  348. int ret;
  349. if (!plane->fb)
  350. return;
  351. WARN_ON(drm_drv_uses_atomic_modeset(plane->dev));
  352. plane->old_fb = plane->fb;
  353. ret = plane->funcs->disable_plane(plane, NULL);
  354. if (ret) {
  355. DRM_ERROR("failed to disable plane with busy fb\n");
  356. plane->old_fb = NULL;
  357. return;
  358. }
  359. /* disconnect the plane from the fb and crtc: */
  360. drm_framebuffer_put(plane->old_fb);
  361. plane->old_fb = NULL;
  362. plane->fb = NULL;
  363. plane->crtc = NULL;
  364. }
  365. EXPORT_SYMBOL(drm_plane_force_disable);
  366. /**
  367. * drm_mode_plane_set_obj_prop - set the value of a property
  368. * @plane: drm plane object to set property value for
  369. * @property: property to set
  370. * @value: value the property should be set to
  371. *
  372. * This functions sets a given property on a given plane object. This function
  373. * calls the driver's ->set_property callback and changes the software state of
  374. * the property if the callback succeeds.
  375. *
  376. * Returns:
  377. * Zero on success, error code on failure.
  378. */
  379. int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
  380. struct drm_property *property,
  381. uint64_t value)
  382. {
  383. int ret = -EINVAL;
  384. struct drm_mode_object *obj = &plane->base;
  385. if (plane->funcs->set_property)
  386. ret = plane->funcs->set_property(plane, property, value);
  387. if (!ret)
  388. drm_object_property_set_value(obj, property, value);
  389. return ret;
  390. }
  391. EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
  392. int drm_mode_getplane_res(struct drm_device *dev, void *data,
  393. struct drm_file *file_priv)
  394. {
  395. struct drm_mode_get_plane_res *plane_resp = data;
  396. struct drm_mode_config *config;
  397. struct drm_plane *plane;
  398. uint32_t __user *plane_ptr;
  399. int count = 0;
  400. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  401. return -EINVAL;
  402. config = &dev->mode_config;
  403. plane_ptr = u64_to_user_ptr(plane_resp->plane_id_ptr);
  404. /*
  405. * This ioctl is called twice, once to determine how much space is
  406. * needed, and the 2nd time to fill it.
  407. */
  408. drm_for_each_plane(plane, dev) {
  409. /*
  410. * Unless userspace set the 'universal planes'
  411. * capability bit, only advertise overlays.
  412. */
  413. if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
  414. !file_priv->universal_planes)
  415. continue;
  416. if (drm_lease_held(file_priv, plane->base.id)) {
  417. if (count < plane_resp->count_planes &&
  418. put_user(plane->base.id, plane_ptr + count))
  419. return -EFAULT;
  420. count++;
  421. }
  422. }
  423. plane_resp->count_planes = count;
  424. return 0;
  425. }
  426. int drm_mode_getplane(struct drm_device *dev, void *data,
  427. struct drm_file *file_priv)
  428. {
  429. struct drm_mode_get_plane *plane_resp = data;
  430. struct drm_plane *plane;
  431. uint32_t __user *format_ptr;
  432. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  433. return -EINVAL;
  434. plane = drm_plane_find(dev, file_priv, plane_resp->plane_id);
  435. if (!plane)
  436. return -ENOENT;
  437. drm_modeset_lock(&plane->mutex, NULL);
  438. if (plane->state && plane->state->crtc && drm_lease_held(file_priv, plane->state->crtc->base.id))
  439. plane_resp->crtc_id = plane->state->crtc->base.id;
  440. else if (!plane->state && plane->crtc && drm_lease_held(file_priv, plane->crtc->base.id))
  441. plane_resp->crtc_id = plane->crtc->base.id;
  442. else
  443. plane_resp->crtc_id = 0;
  444. if (plane->state && plane->state->fb)
  445. plane_resp->fb_id = plane->state->fb->base.id;
  446. else if (!plane->state && plane->fb)
  447. plane_resp->fb_id = plane->fb->base.id;
  448. else
  449. plane_resp->fb_id = 0;
  450. drm_modeset_unlock(&plane->mutex);
  451. plane_resp->plane_id = plane->base.id;
  452. plane_resp->possible_crtcs = drm_lease_filter_crtcs(file_priv,
  453. plane->possible_crtcs);
  454. plane_resp->gamma_size = 0;
  455. /*
  456. * This ioctl is called twice, once to determine how much space is
  457. * needed, and the 2nd time to fill it.
  458. */
  459. if (plane->format_count &&
  460. (plane_resp->count_format_types >= plane->format_count)) {
  461. format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
  462. if (copy_to_user(format_ptr,
  463. plane->format_types,
  464. sizeof(uint32_t) * plane->format_count)) {
  465. return -EFAULT;
  466. }
  467. }
  468. plane_resp->count_format_types = plane->format_count;
  469. return 0;
  470. }
  471. int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
  472. {
  473. unsigned int i;
  474. for (i = 0; i < plane->format_count; i++) {
  475. if (format == plane->format_types[i])
  476. return 0;
  477. }
  478. return -EINVAL;
  479. }
  480. /*
  481. * __setplane_internal - setplane handler for internal callers
  482. *
  483. * This function will take a reference on the new fb for the plane
  484. * on success.
  485. *
  486. * src_{x,y,w,h} are provided in 16.16 fixed point format
  487. */
  488. static int __setplane_internal(struct drm_plane *plane,
  489. struct drm_crtc *crtc,
  490. struct drm_framebuffer *fb,
  491. int32_t crtc_x, int32_t crtc_y,
  492. uint32_t crtc_w, uint32_t crtc_h,
  493. /* src_{x,y,w,h} values are 16.16 fixed point */
  494. uint32_t src_x, uint32_t src_y,
  495. uint32_t src_w, uint32_t src_h,
  496. struct drm_modeset_acquire_ctx *ctx)
  497. {
  498. int ret = 0;
  499. /* No fb means shut it down */
  500. if (!fb) {
  501. plane->old_fb = plane->fb;
  502. ret = plane->funcs->disable_plane(plane, ctx);
  503. if (!ret) {
  504. plane->crtc = NULL;
  505. plane->fb = NULL;
  506. } else {
  507. plane->old_fb = NULL;
  508. }
  509. goto out;
  510. }
  511. /* Check whether this plane is usable on this CRTC */
  512. if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
  513. DRM_DEBUG_KMS("Invalid crtc for plane\n");
  514. ret = -EINVAL;
  515. goto out;
  516. }
  517. /* Check whether this plane supports the fb pixel format. */
  518. ret = drm_plane_check_pixel_format(plane, fb->format->format);
  519. if (ret) {
  520. struct drm_format_name_buf format_name;
  521. DRM_DEBUG_KMS("Invalid pixel format %s\n",
  522. drm_get_format_name(fb->format->format,
  523. &format_name));
  524. goto out;
  525. }
  526. /* Give drivers some help against integer overflows */
  527. if (crtc_w > INT_MAX ||
  528. crtc_x > INT_MAX - (int32_t) crtc_w ||
  529. crtc_h > INT_MAX ||
  530. crtc_y > INT_MAX - (int32_t) crtc_h) {
  531. DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
  532. crtc_w, crtc_h, crtc_x, crtc_y);
  533. ret = -ERANGE;
  534. goto out;
  535. }
  536. ret = drm_framebuffer_check_src_coords(src_x, src_y, src_w, src_h, fb);
  537. if (ret)
  538. goto out;
  539. plane->old_fb = plane->fb;
  540. ret = plane->funcs->update_plane(plane, crtc, fb,
  541. crtc_x, crtc_y, crtc_w, crtc_h,
  542. src_x, src_y, src_w, src_h, ctx);
  543. if (!ret) {
  544. plane->crtc = crtc;
  545. plane->fb = fb;
  546. drm_framebuffer_get(plane->fb);
  547. } else {
  548. plane->old_fb = NULL;
  549. }
  550. out:
  551. if (plane->old_fb)
  552. drm_framebuffer_put(plane->old_fb);
  553. plane->old_fb = NULL;
  554. return ret;
  555. }
  556. static int setplane_internal(struct drm_plane *plane,
  557. struct drm_crtc *crtc,
  558. struct drm_framebuffer *fb,
  559. int32_t crtc_x, int32_t crtc_y,
  560. uint32_t crtc_w, uint32_t crtc_h,
  561. /* src_{x,y,w,h} values are 16.16 fixed point */
  562. uint32_t src_x, uint32_t src_y,
  563. uint32_t src_w, uint32_t src_h)
  564. {
  565. struct drm_modeset_acquire_ctx ctx;
  566. int ret;
  567. drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
  568. retry:
  569. ret = drm_modeset_lock_all_ctx(plane->dev, &ctx);
  570. if (ret)
  571. goto fail;
  572. ret = __setplane_internal(plane, crtc, fb,
  573. crtc_x, crtc_y, crtc_w, crtc_h,
  574. src_x, src_y, src_w, src_h, &ctx);
  575. fail:
  576. if (ret == -EDEADLK) {
  577. ret = drm_modeset_backoff(&ctx);
  578. if (!ret)
  579. goto retry;
  580. }
  581. drm_modeset_drop_locks(&ctx);
  582. drm_modeset_acquire_fini(&ctx);
  583. return ret;
  584. }
  585. int drm_mode_setplane(struct drm_device *dev, void *data,
  586. struct drm_file *file_priv)
  587. {
  588. struct drm_mode_set_plane *plane_req = data;
  589. struct drm_plane *plane;
  590. struct drm_crtc *crtc = NULL;
  591. struct drm_framebuffer *fb = NULL;
  592. int ret;
  593. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  594. return -EINVAL;
  595. /*
  596. * First, find the plane, crtc, and fb objects. If not available,
  597. * we don't bother to call the driver.
  598. */
  599. plane = drm_plane_find(dev, file_priv, plane_req->plane_id);
  600. if (!plane) {
  601. DRM_DEBUG_KMS("Unknown plane ID %d\n",
  602. plane_req->plane_id);
  603. return -ENOENT;
  604. }
  605. if (plane_req->fb_id) {
  606. fb = drm_framebuffer_lookup(dev, file_priv, plane_req->fb_id);
  607. if (!fb) {
  608. DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
  609. plane_req->fb_id);
  610. return -ENOENT;
  611. }
  612. crtc = drm_crtc_find(dev, file_priv, plane_req->crtc_id);
  613. if (!crtc) {
  614. drm_framebuffer_put(fb);
  615. DRM_DEBUG_KMS("Unknown crtc ID %d\n",
  616. plane_req->crtc_id);
  617. return -ENOENT;
  618. }
  619. }
  620. ret = setplane_internal(plane, crtc, fb,
  621. plane_req->crtc_x, plane_req->crtc_y,
  622. plane_req->crtc_w, plane_req->crtc_h,
  623. plane_req->src_x, plane_req->src_y,
  624. plane_req->src_w, plane_req->src_h);
  625. if (fb)
  626. drm_framebuffer_put(fb);
  627. return ret;
  628. }
  629. static int drm_mode_cursor_universal(struct drm_crtc *crtc,
  630. struct drm_mode_cursor2 *req,
  631. struct drm_file *file_priv,
  632. struct drm_modeset_acquire_ctx *ctx)
  633. {
  634. struct drm_device *dev = crtc->dev;
  635. struct drm_framebuffer *fb = NULL;
  636. struct drm_mode_fb_cmd2 fbreq = {
  637. .width = req->width,
  638. .height = req->height,
  639. .pixel_format = DRM_FORMAT_ARGB8888,
  640. .pitches = { req->width * 4 },
  641. .handles = { req->handle },
  642. };
  643. int32_t crtc_x, crtc_y;
  644. uint32_t crtc_w = 0, crtc_h = 0;
  645. uint32_t src_w = 0, src_h = 0;
  646. int ret = 0;
  647. BUG_ON(!crtc->cursor);
  648. WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
  649. /*
  650. * Obtain fb we'll be using (either new or existing) and take an extra
  651. * reference to it if fb != null. setplane will take care of dropping
  652. * the reference if the plane update fails.
  653. */
  654. if (req->flags & DRM_MODE_CURSOR_BO) {
  655. if (req->handle) {
  656. fb = drm_internal_framebuffer_create(dev, &fbreq, file_priv);
  657. if (IS_ERR(fb)) {
  658. DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
  659. return PTR_ERR(fb);
  660. }
  661. fb->hot_x = req->hot_x;
  662. fb->hot_y = req->hot_y;
  663. } else {
  664. fb = NULL;
  665. }
  666. } else {
  667. fb = crtc->cursor->fb;
  668. if (fb)
  669. drm_framebuffer_get(fb);
  670. }
  671. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  672. crtc_x = req->x;
  673. crtc_y = req->y;
  674. } else {
  675. crtc_x = crtc->cursor_x;
  676. crtc_y = crtc->cursor_y;
  677. }
  678. if (fb) {
  679. crtc_w = fb->width;
  680. crtc_h = fb->height;
  681. src_w = fb->width << 16;
  682. src_h = fb->height << 16;
  683. }
  684. ret = __setplane_internal(crtc->cursor, crtc, fb,
  685. crtc_x, crtc_y, crtc_w, crtc_h,
  686. 0, 0, src_w, src_h, ctx);
  687. if (fb)
  688. drm_framebuffer_put(fb);
  689. /* Update successful; save new cursor position, if necessary */
  690. if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
  691. crtc->cursor_x = req->x;
  692. crtc->cursor_y = req->y;
  693. }
  694. return ret;
  695. }
  696. static int drm_mode_cursor_common(struct drm_device *dev,
  697. struct drm_mode_cursor2 *req,
  698. struct drm_file *file_priv)
  699. {
  700. struct drm_crtc *crtc;
  701. struct drm_modeset_acquire_ctx ctx;
  702. int ret = 0;
  703. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  704. return -EINVAL;
  705. if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
  706. return -EINVAL;
  707. crtc = drm_crtc_find(dev, file_priv, req->crtc_id);
  708. if (!crtc) {
  709. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
  710. return -ENOENT;
  711. }
  712. drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
  713. retry:
  714. ret = drm_modeset_lock(&crtc->mutex, &ctx);
  715. if (ret)
  716. goto out;
  717. /*
  718. * If this crtc has a universal cursor plane, call that plane's update
  719. * handler rather than using legacy cursor handlers.
  720. */
  721. if (crtc->cursor) {
  722. ret = drm_modeset_lock(&crtc->cursor->mutex, &ctx);
  723. if (ret)
  724. goto out;
  725. ret = drm_mode_cursor_universal(crtc, req, file_priv, &ctx);
  726. goto out;
  727. }
  728. if (req->flags & DRM_MODE_CURSOR_BO) {
  729. if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
  730. ret = -ENXIO;
  731. goto out;
  732. }
  733. /* Turns off the cursor if handle is 0 */
  734. if (crtc->funcs->cursor_set2)
  735. ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
  736. req->width, req->height, req->hot_x, req->hot_y);
  737. else
  738. ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
  739. req->width, req->height);
  740. }
  741. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  742. if (crtc->funcs->cursor_move) {
  743. ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
  744. } else {
  745. ret = -EFAULT;
  746. goto out;
  747. }
  748. }
  749. out:
  750. if (ret == -EDEADLK) {
  751. ret = drm_modeset_backoff(&ctx);
  752. if (!ret)
  753. goto retry;
  754. }
  755. drm_modeset_drop_locks(&ctx);
  756. drm_modeset_acquire_fini(&ctx);
  757. return ret;
  758. }
  759. int drm_mode_cursor_ioctl(struct drm_device *dev,
  760. void *data, struct drm_file *file_priv)
  761. {
  762. struct drm_mode_cursor *req = data;
  763. struct drm_mode_cursor2 new_req;
  764. memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
  765. new_req.hot_x = new_req.hot_y = 0;
  766. return drm_mode_cursor_common(dev, &new_req, file_priv);
  767. }
  768. /*
  769. * Set the cursor configuration based on user request. This implements the 2nd
  770. * version of the cursor ioctl, which allows userspace to additionally specify
  771. * the hotspot of the pointer.
  772. */
  773. int drm_mode_cursor2_ioctl(struct drm_device *dev,
  774. void *data, struct drm_file *file_priv)
  775. {
  776. struct drm_mode_cursor2 *req = data;
  777. return drm_mode_cursor_common(dev, req, file_priv);
  778. }
  779. int drm_mode_page_flip_ioctl(struct drm_device *dev,
  780. void *data, struct drm_file *file_priv)
  781. {
  782. struct drm_mode_crtc_page_flip_target *page_flip = data;
  783. struct drm_crtc *crtc;
  784. struct drm_framebuffer *fb = NULL;
  785. struct drm_pending_vblank_event *e = NULL;
  786. u32 target_vblank = page_flip->sequence;
  787. struct drm_modeset_acquire_ctx ctx;
  788. int ret = -EINVAL;
  789. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  790. return -EINVAL;
  791. if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS)
  792. return -EINVAL;
  793. if (page_flip->sequence != 0 && !(page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET))
  794. return -EINVAL;
  795. /* Only one of the DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE/RELATIVE flags
  796. * can be specified
  797. */
  798. if ((page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) == DRM_MODE_PAGE_FLIP_TARGET)
  799. return -EINVAL;
  800. if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
  801. return -EINVAL;
  802. crtc = drm_crtc_find(dev, file_priv, page_flip->crtc_id);
  803. if (!crtc)
  804. return -ENOENT;
  805. if (crtc->funcs->page_flip_target) {
  806. u32 current_vblank;
  807. int r;
  808. r = drm_crtc_vblank_get(crtc);
  809. if (r)
  810. return r;
  811. current_vblank = (u32)drm_crtc_vblank_count(crtc);
  812. switch (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) {
  813. case DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE:
  814. if ((int)(target_vblank - current_vblank) > 1) {
  815. DRM_DEBUG("Invalid absolute flip target %u, "
  816. "must be <= %u\n", target_vblank,
  817. current_vblank + 1);
  818. drm_crtc_vblank_put(crtc);
  819. return -EINVAL;
  820. }
  821. break;
  822. case DRM_MODE_PAGE_FLIP_TARGET_RELATIVE:
  823. if (target_vblank != 0 && target_vblank != 1) {
  824. DRM_DEBUG("Invalid relative flip target %u, "
  825. "must be 0 or 1\n", target_vblank);
  826. drm_crtc_vblank_put(crtc);
  827. return -EINVAL;
  828. }
  829. target_vblank += current_vblank;
  830. break;
  831. default:
  832. target_vblank = current_vblank +
  833. !(page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC);
  834. break;
  835. }
  836. } else if (crtc->funcs->page_flip == NULL ||
  837. (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET)) {
  838. return -EINVAL;
  839. }
  840. drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
  841. retry:
  842. ret = drm_modeset_lock(&crtc->mutex, &ctx);
  843. if (ret)
  844. goto out;
  845. ret = drm_modeset_lock(&crtc->primary->mutex, &ctx);
  846. if (ret)
  847. goto out;
  848. if (crtc->primary->fb == NULL) {
  849. /* The framebuffer is currently unbound, presumably
  850. * due to a hotplug event, that userspace has not
  851. * yet discovered.
  852. */
  853. ret = -EBUSY;
  854. goto out;
  855. }
  856. fb = drm_framebuffer_lookup(dev, file_priv, page_flip->fb_id);
  857. if (!fb) {
  858. ret = -ENOENT;
  859. goto out;
  860. }
  861. if (crtc->state) {
  862. const struct drm_plane_state *state = crtc->primary->state;
  863. ret = drm_framebuffer_check_src_coords(state->src_x,
  864. state->src_y,
  865. state->src_w,
  866. state->src_h,
  867. fb);
  868. } else {
  869. ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
  870. }
  871. if (ret)
  872. goto out;
  873. if (crtc->primary->fb->format != fb->format) {
  874. DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
  875. ret = -EINVAL;
  876. goto out;
  877. }
  878. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  879. e = kzalloc(sizeof *e, GFP_KERNEL);
  880. if (!e) {
  881. ret = -ENOMEM;
  882. goto out;
  883. }
  884. e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
  885. e->event.base.length = sizeof(e->event);
  886. e->event.vbl.user_data = page_flip->user_data;
  887. e->event.vbl.crtc_id = crtc->base.id;
  888. ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
  889. if (ret) {
  890. kfree(e);
  891. e = NULL;
  892. goto out;
  893. }
  894. }
  895. crtc->primary->old_fb = crtc->primary->fb;
  896. if (crtc->funcs->page_flip_target)
  897. ret = crtc->funcs->page_flip_target(crtc, fb, e,
  898. page_flip->flags,
  899. target_vblank,
  900. &ctx);
  901. else
  902. ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags,
  903. &ctx);
  904. if (ret) {
  905. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
  906. drm_event_cancel_free(dev, &e->base);
  907. /* Keep the old fb, don't unref it. */
  908. crtc->primary->old_fb = NULL;
  909. } else {
  910. crtc->primary->fb = fb;
  911. /* Unref only the old framebuffer. */
  912. fb = NULL;
  913. }
  914. out:
  915. if (fb)
  916. drm_framebuffer_put(fb);
  917. if (crtc->primary->old_fb)
  918. drm_framebuffer_put(crtc->primary->old_fb);
  919. crtc->primary->old_fb = NULL;
  920. if (ret == -EDEADLK) {
  921. ret = drm_modeset_backoff(&ctx);
  922. if (!ret)
  923. goto retry;
  924. }
  925. drm_modeset_drop_locks(&ctx);
  926. drm_modeset_acquire_fini(&ctx);
  927. if (ret && crtc->funcs->page_flip_target)
  928. drm_crtc_vblank_put(crtc);
  929. return ret;
  930. }