drm_plane.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  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 = 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. WARN_ON(drm_drv_uses_atomic_modeset(dev) &&
  156. (!funcs->atomic_destroy_state ||
  157. !funcs->atomic_duplicate_state));
  158. ret = drm_mode_object_add(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
  159. if (ret)
  160. return ret;
  161. drm_modeset_lock_init(&plane->mutex);
  162. plane->base.properties = &plane->properties;
  163. plane->dev = dev;
  164. plane->funcs = funcs;
  165. plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
  166. GFP_KERNEL);
  167. if (!plane->format_types) {
  168. DRM_DEBUG_KMS("out of memory when allocating plane\n");
  169. drm_mode_object_unregister(dev, &plane->base);
  170. return -ENOMEM;
  171. }
  172. /*
  173. * First driver to need more than 64 formats needs to fix this. Each
  174. * format is encoded as a bit and the current code only supports a u64.
  175. */
  176. if (WARN_ON(format_count > 64))
  177. return -EINVAL;
  178. if (format_modifiers) {
  179. const uint64_t *temp_modifiers = format_modifiers;
  180. while (*temp_modifiers++ != DRM_FORMAT_MOD_INVALID)
  181. format_modifier_count++;
  182. }
  183. if (format_modifier_count)
  184. config->allow_fb_modifiers = true;
  185. plane->modifier_count = format_modifier_count;
  186. plane->modifiers = kmalloc_array(format_modifier_count,
  187. sizeof(format_modifiers[0]),
  188. GFP_KERNEL);
  189. if (format_modifier_count && !plane->modifiers) {
  190. DRM_DEBUG_KMS("out of memory when allocating plane\n");
  191. kfree(plane->format_types);
  192. drm_mode_object_unregister(dev, &plane->base);
  193. return -ENOMEM;
  194. }
  195. if (name) {
  196. va_list ap;
  197. va_start(ap, name);
  198. plane->name = kvasprintf(GFP_KERNEL, name, ap);
  199. va_end(ap);
  200. } else {
  201. plane->name = kasprintf(GFP_KERNEL, "plane-%d",
  202. drm_num_planes(dev));
  203. }
  204. if (!plane->name) {
  205. kfree(plane->format_types);
  206. kfree(plane->modifiers);
  207. drm_mode_object_unregister(dev, &plane->base);
  208. return -ENOMEM;
  209. }
  210. memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
  211. plane->format_count = format_count;
  212. memcpy(plane->modifiers, format_modifiers,
  213. format_modifier_count * sizeof(format_modifiers[0]));
  214. plane->possible_crtcs = possible_crtcs;
  215. plane->type = type;
  216. list_add_tail(&plane->head, &config->plane_list);
  217. plane->index = config->num_total_plane++;
  218. drm_object_attach_property(&plane->base,
  219. config->plane_type_property,
  220. plane->type);
  221. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  222. drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
  223. drm_object_attach_property(&plane->base, config->prop_in_fence_fd, -1);
  224. drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
  225. drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
  226. drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
  227. drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
  228. drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
  229. drm_object_attach_property(&plane->base, config->prop_src_x, 0);
  230. drm_object_attach_property(&plane->base, config->prop_src_y, 0);
  231. drm_object_attach_property(&plane->base, config->prop_src_w, 0);
  232. drm_object_attach_property(&plane->base, config->prop_src_h, 0);
  233. }
  234. if (config->allow_fb_modifiers)
  235. create_in_format_blob(dev, plane);
  236. return 0;
  237. }
  238. EXPORT_SYMBOL(drm_universal_plane_init);
  239. int drm_plane_register_all(struct drm_device *dev)
  240. {
  241. struct drm_plane *plane;
  242. int ret = 0;
  243. drm_for_each_plane(plane, dev) {
  244. if (plane->funcs->late_register)
  245. ret = plane->funcs->late_register(plane);
  246. if (ret)
  247. return ret;
  248. }
  249. return 0;
  250. }
  251. void drm_plane_unregister_all(struct drm_device *dev)
  252. {
  253. struct drm_plane *plane;
  254. drm_for_each_plane(plane, dev) {
  255. if (plane->funcs->early_unregister)
  256. plane->funcs->early_unregister(plane);
  257. }
  258. }
  259. /**
  260. * drm_plane_init - Initialize a legacy plane
  261. * @dev: DRM device
  262. * @plane: plane object to init
  263. * @possible_crtcs: bitmask of possible CRTCs
  264. * @funcs: callbacks for the new plane
  265. * @formats: array of supported formats (DRM_FORMAT\_\*)
  266. * @format_count: number of elements in @formats
  267. * @is_primary: plane type (primary vs overlay)
  268. *
  269. * Legacy API to initialize a DRM plane.
  270. *
  271. * New drivers should call drm_universal_plane_init() instead.
  272. *
  273. * Returns:
  274. * Zero on success, error code on failure.
  275. */
  276. int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
  277. uint32_t possible_crtcs,
  278. const struct drm_plane_funcs *funcs,
  279. const uint32_t *formats, unsigned int format_count,
  280. bool is_primary)
  281. {
  282. enum drm_plane_type type;
  283. type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
  284. return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
  285. formats, format_count,
  286. NULL, type, NULL);
  287. }
  288. EXPORT_SYMBOL(drm_plane_init);
  289. /**
  290. * drm_plane_cleanup - Clean up the core plane usage
  291. * @plane: plane to cleanup
  292. *
  293. * This function cleans up @plane and removes it from the DRM mode setting
  294. * core. Note that the function does *not* free the plane structure itself,
  295. * this is the responsibility of the caller.
  296. */
  297. void drm_plane_cleanup(struct drm_plane *plane)
  298. {
  299. struct drm_device *dev = plane->dev;
  300. drm_modeset_lock_fini(&plane->mutex);
  301. kfree(plane->format_types);
  302. kfree(plane->modifiers);
  303. drm_mode_object_unregister(dev, &plane->base);
  304. BUG_ON(list_empty(&plane->head));
  305. /* Note that the plane_list is considered to be static; should we
  306. * remove the drm_plane at runtime we would have to decrement all
  307. * the indices on the drm_plane after us in the plane_list.
  308. */
  309. list_del(&plane->head);
  310. dev->mode_config.num_total_plane--;
  311. WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
  312. if (plane->state && plane->funcs->atomic_destroy_state)
  313. plane->funcs->atomic_destroy_state(plane, plane->state);
  314. kfree(plane->name);
  315. memset(plane, 0, sizeof(*plane));
  316. }
  317. EXPORT_SYMBOL(drm_plane_cleanup);
  318. /**
  319. * drm_plane_from_index - find the registered plane at an index
  320. * @dev: DRM device
  321. * @idx: index of registered plane to find for
  322. *
  323. * Given a plane index, return the registered plane from DRM device's
  324. * list of planes with matching index. This is the inverse of drm_plane_index().
  325. */
  326. struct drm_plane *
  327. drm_plane_from_index(struct drm_device *dev, int idx)
  328. {
  329. struct drm_plane *plane;
  330. drm_for_each_plane(plane, dev)
  331. if (idx == plane->index)
  332. return plane;
  333. return NULL;
  334. }
  335. EXPORT_SYMBOL(drm_plane_from_index);
  336. /**
  337. * drm_plane_force_disable - Forcibly disable a plane
  338. * @plane: plane to disable
  339. *
  340. * Forces the plane to be disabled.
  341. *
  342. * Used when the plane's current framebuffer is destroyed,
  343. * and when restoring fbdev mode.
  344. *
  345. * Note that this function is not suitable for atomic drivers, since it doesn't
  346. * wire through the lock acquisition context properly and hence can't handle
  347. * retries or driver private locks. You probably want to use
  348. * drm_atomic_helper_disable_plane() or
  349. * drm_atomic_helper_disable_planes_on_crtc() instead.
  350. */
  351. void drm_plane_force_disable(struct drm_plane *plane)
  352. {
  353. int ret;
  354. if (!plane->fb)
  355. return;
  356. WARN_ON(drm_drv_uses_atomic_modeset(plane->dev));
  357. plane->old_fb = plane->fb;
  358. ret = plane->funcs->disable_plane(plane, NULL);
  359. if (ret) {
  360. DRM_ERROR("failed to disable plane with busy fb\n");
  361. plane->old_fb = NULL;
  362. return;
  363. }
  364. /* disconnect the plane from the fb and crtc: */
  365. drm_framebuffer_put(plane->old_fb);
  366. plane->old_fb = NULL;
  367. plane->fb = NULL;
  368. plane->crtc = NULL;
  369. }
  370. EXPORT_SYMBOL(drm_plane_force_disable);
  371. /**
  372. * drm_mode_plane_set_obj_prop - set the value of a property
  373. * @plane: drm plane object to set property value for
  374. * @property: property to set
  375. * @value: value the property should be set to
  376. *
  377. * This functions sets a given property on a given plane object. This function
  378. * calls the driver's ->set_property callback and changes the software state of
  379. * the property if the callback succeeds.
  380. *
  381. * Returns:
  382. * Zero on success, error code on failure.
  383. */
  384. int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
  385. struct drm_property *property,
  386. uint64_t value)
  387. {
  388. int ret = -EINVAL;
  389. struct drm_mode_object *obj = &plane->base;
  390. if (plane->funcs->set_property)
  391. ret = plane->funcs->set_property(plane, property, value);
  392. if (!ret)
  393. drm_object_property_set_value(obj, property, value);
  394. return ret;
  395. }
  396. EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
  397. int drm_mode_getplane_res(struct drm_device *dev, void *data,
  398. struct drm_file *file_priv)
  399. {
  400. struct drm_mode_get_plane_res *plane_resp = data;
  401. struct drm_mode_config *config;
  402. struct drm_plane *plane;
  403. uint32_t __user *plane_ptr;
  404. int count = 0;
  405. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  406. return -EINVAL;
  407. config = &dev->mode_config;
  408. plane_ptr = u64_to_user_ptr(plane_resp->plane_id_ptr);
  409. /*
  410. * This ioctl is called twice, once to determine how much space is
  411. * needed, and the 2nd time to fill it.
  412. */
  413. drm_for_each_plane(plane, dev) {
  414. /*
  415. * Unless userspace set the 'universal planes'
  416. * capability bit, only advertise overlays.
  417. */
  418. if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
  419. !file_priv->universal_planes)
  420. continue;
  421. if (drm_lease_held(file_priv, plane->base.id)) {
  422. if (count < plane_resp->count_planes &&
  423. put_user(plane->base.id, plane_ptr + count))
  424. return -EFAULT;
  425. count++;
  426. }
  427. }
  428. plane_resp->count_planes = count;
  429. return 0;
  430. }
  431. int drm_mode_getplane(struct drm_device *dev, void *data,
  432. struct drm_file *file_priv)
  433. {
  434. struct drm_mode_get_plane *plane_resp = data;
  435. struct drm_plane *plane;
  436. uint32_t __user *format_ptr;
  437. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  438. return -EINVAL;
  439. plane = drm_plane_find(dev, file_priv, plane_resp->plane_id);
  440. if (!plane)
  441. return -ENOENT;
  442. drm_modeset_lock(&plane->mutex, NULL);
  443. if (plane->state && plane->state->crtc && drm_lease_held(file_priv, plane->state->crtc->base.id))
  444. plane_resp->crtc_id = plane->state->crtc->base.id;
  445. else if (!plane->state && plane->crtc && drm_lease_held(file_priv, plane->crtc->base.id))
  446. plane_resp->crtc_id = plane->crtc->base.id;
  447. else
  448. plane_resp->crtc_id = 0;
  449. if (plane->state && plane->state->fb)
  450. plane_resp->fb_id = plane->state->fb->base.id;
  451. else if (!plane->state && plane->fb)
  452. plane_resp->fb_id = plane->fb->base.id;
  453. else
  454. plane_resp->fb_id = 0;
  455. drm_modeset_unlock(&plane->mutex);
  456. plane_resp->plane_id = plane->base.id;
  457. plane_resp->possible_crtcs = drm_lease_filter_crtcs(file_priv,
  458. plane->possible_crtcs);
  459. plane_resp->gamma_size = 0;
  460. /*
  461. * This ioctl is called twice, once to determine how much space is
  462. * needed, and the 2nd time to fill it.
  463. */
  464. if (plane->format_count &&
  465. (plane_resp->count_format_types >= plane->format_count)) {
  466. format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
  467. if (copy_to_user(format_ptr,
  468. plane->format_types,
  469. sizeof(uint32_t) * plane->format_count)) {
  470. return -EFAULT;
  471. }
  472. }
  473. plane_resp->count_format_types = plane->format_count;
  474. return 0;
  475. }
  476. int drm_plane_check_pixel_format(struct drm_plane *plane,
  477. u32 format, u64 modifier)
  478. {
  479. unsigned int i;
  480. for (i = 0; i < plane->format_count; i++) {
  481. if (format == plane->format_types[i])
  482. break;
  483. }
  484. if (i == plane->format_count)
  485. return -EINVAL;
  486. if (plane->funcs->format_mod_supported) {
  487. if (!plane->funcs->format_mod_supported(plane, format, modifier))
  488. return -EINVAL;
  489. } else {
  490. if (!plane->modifier_count)
  491. return 0;
  492. for (i = 0; i < plane->modifier_count; i++) {
  493. if (modifier == plane->modifiers[i])
  494. break;
  495. }
  496. if (i == plane->modifier_count)
  497. return -EINVAL;
  498. }
  499. return 0;
  500. }
  501. static int __setplane_check(struct drm_plane *plane,
  502. struct drm_crtc *crtc,
  503. struct drm_framebuffer *fb,
  504. int32_t crtc_x, int32_t crtc_y,
  505. uint32_t crtc_w, uint32_t crtc_h,
  506. uint32_t src_x, uint32_t src_y,
  507. uint32_t src_w, uint32_t src_h)
  508. {
  509. int ret;
  510. /* Check whether this plane is usable on this CRTC */
  511. if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
  512. DRM_DEBUG_KMS("Invalid crtc for plane\n");
  513. return -EINVAL;
  514. }
  515. /* Check whether this plane supports the fb pixel format. */
  516. ret = drm_plane_check_pixel_format(plane, fb->format->format,
  517. fb->modifier);
  518. if (ret) {
  519. struct drm_format_name_buf format_name;
  520. DRM_DEBUG_KMS("Invalid pixel format %s, modifier 0x%llx\n",
  521. drm_get_format_name(fb->format->format,
  522. &format_name),
  523. fb->modifier);
  524. return ret;
  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. return -ERANGE;
  534. }
  535. ret = drm_framebuffer_check_src_coords(src_x, src_y, src_w, src_h, fb);
  536. if (ret)
  537. return ret;
  538. return 0;
  539. }
  540. /*
  541. * __setplane_internal - setplane handler for internal callers
  542. *
  543. * This function will take a reference on the new fb for the plane
  544. * on success.
  545. *
  546. * src_{x,y,w,h} are provided in 16.16 fixed point format
  547. */
  548. static int __setplane_internal(struct drm_plane *plane,
  549. struct drm_crtc *crtc,
  550. struct drm_framebuffer *fb,
  551. int32_t crtc_x, int32_t crtc_y,
  552. uint32_t crtc_w, uint32_t crtc_h,
  553. /* src_{x,y,w,h} values are 16.16 fixed point */
  554. uint32_t src_x, uint32_t src_y,
  555. uint32_t src_w, uint32_t src_h,
  556. struct drm_modeset_acquire_ctx *ctx)
  557. {
  558. int ret = 0;
  559. WARN_ON(drm_drv_uses_atomic_modeset(plane->dev));
  560. /* No fb means shut it down */
  561. if (!fb) {
  562. plane->old_fb = plane->fb;
  563. ret = plane->funcs->disable_plane(plane, ctx);
  564. if (!ret) {
  565. plane->crtc = NULL;
  566. plane->fb = NULL;
  567. } else {
  568. plane->old_fb = NULL;
  569. }
  570. goto out;
  571. }
  572. ret = __setplane_check(plane, crtc, fb,
  573. crtc_x, crtc_y, crtc_w, crtc_h,
  574. src_x, src_y, src_w, src_h);
  575. if (ret)
  576. goto out;
  577. plane->old_fb = plane->fb;
  578. ret = plane->funcs->update_plane(plane, crtc, fb,
  579. crtc_x, crtc_y, crtc_w, crtc_h,
  580. src_x, src_y, src_w, src_h, ctx);
  581. if (!ret) {
  582. plane->crtc = crtc;
  583. plane->fb = fb;
  584. drm_framebuffer_get(plane->fb);
  585. } else {
  586. plane->old_fb = NULL;
  587. }
  588. out:
  589. if (plane->old_fb)
  590. drm_framebuffer_put(plane->old_fb);
  591. plane->old_fb = NULL;
  592. return ret;
  593. }
  594. static int __setplane_atomic(struct drm_plane *plane,
  595. struct drm_crtc *crtc,
  596. struct drm_framebuffer *fb,
  597. int32_t crtc_x, int32_t crtc_y,
  598. uint32_t crtc_w, uint32_t crtc_h,
  599. uint32_t src_x, uint32_t src_y,
  600. uint32_t src_w, uint32_t src_h,
  601. struct drm_modeset_acquire_ctx *ctx)
  602. {
  603. int ret;
  604. WARN_ON(!drm_drv_uses_atomic_modeset(plane->dev));
  605. /* No fb means shut it down */
  606. if (!fb)
  607. return plane->funcs->disable_plane(plane, ctx);
  608. /*
  609. * FIXME: This is redundant with drm_atomic_plane_check(),
  610. * but the legacy cursor/"async" .update_plane() tricks
  611. * don't call that so we still need this here. Should remove
  612. * this when all .update_plane() implementations have been
  613. * fixed to call drm_atomic_plane_check().
  614. */
  615. ret = __setplane_check(plane, crtc, fb,
  616. crtc_x, crtc_y, crtc_w, crtc_h,
  617. src_x, src_y, src_w, src_h);
  618. if (ret)
  619. return ret;
  620. return plane->funcs->update_plane(plane, crtc, fb,
  621. crtc_x, crtc_y, crtc_w, crtc_h,
  622. src_x, src_y, src_w, src_h, ctx);
  623. }
  624. static int setplane_internal(struct drm_plane *plane,
  625. struct drm_crtc *crtc,
  626. struct drm_framebuffer *fb,
  627. int32_t crtc_x, int32_t crtc_y,
  628. uint32_t crtc_w, uint32_t crtc_h,
  629. /* src_{x,y,w,h} values are 16.16 fixed point */
  630. uint32_t src_x, uint32_t src_y,
  631. uint32_t src_w, uint32_t src_h)
  632. {
  633. struct drm_modeset_acquire_ctx ctx;
  634. int ret;
  635. drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
  636. retry:
  637. ret = drm_modeset_lock_all_ctx(plane->dev, &ctx);
  638. if (ret)
  639. goto fail;
  640. if (drm_drv_uses_atomic_modeset(plane->dev))
  641. ret = __setplane_atomic(plane, crtc, fb,
  642. crtc_x, crtc_y, crtc_w, crtc_h,
  643. src_x, src_y, src_w, src_h, &ctx);
  644. else
  645. ret = __setplane_internal(plane, crtc, fb,
  646. crtc_x, crtc_y, crtc_w, crtc_h,
  647. src_x, src_y, src_w, src_h, &ctx);
  648. fail:
  649. if (ret == -EDEADLK) {
  650. ret = drm_modeset_backoff(&ctx);
  651. if (!ret)
  652. goto retry;
  653. }
  654. drm_modeset_drop_locks(&ctx);
  655. drm_modeset_acquire_fini(&ctx);
  656. return ret;
  657. }
  658. int drm_mode_setplane(struct drm_device *dev, void *data,
  659. struct drm_file *file_priv)
  660. {
  661. struct drm_mode_set_plane *plane_req = data;
  662. struct drm_plane *plane;
  663. struct drm_crtc *crtc = NULL;
  664. struct drm_framebuffer *fb = NULL;
  665. int ret;
  666. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  667. return -EINVAL;
  668. /*
  669. * First, find the plane, crtc, and fb objects. If not available,
  670. * we don't bother to call the driver.
  671. */
  672. plane = drm_plane_find(dev, file_priv, plane_req->plane_id);
  673. if (!plane) {
  674. DRM_DEBUG_KMS("Unknown plane ID %d\n",
  675. plane_req->plane_id);
  676. return -ENOENT;
  677. }
  678. if (plane_req->fb_id) {
  679. fb = drm_framebuffer_lookup(dev, file_priv, plane_req->fb_id);
  680. if (!fb) {
  681. DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
  682. plane_req->fb_id);
  683. return -ENOENT;
  684. }
  685. crtc = drm_crtc_find(dev, file_priv, plane_req->crtc_id);
  686. if (!crtc) {
  687. drm_framebuffer_put(fb);
  688. DRM_DEBUG_KMS("Unknown crtc ID %d\n",
  689. plane_req->crtc_id);
  690. return -ENOENT;
  691. }
  692. }
  693. ret = setplane_internal(plane, crtc, fb,
  694. plane_req->crtc_x, plane_req->crtc_y,
  695. plane_req->crtc_w, plane_req->crtc_h,
  696. plane_req->src_x, plane_req->src_y,
  697. plane_req->src_w, plane_req->src_h);
  698. if (fb)
  699. drm_framebuffer_put(fb);
  700. return ret;
  701. }
  702. static int drm_mode_cursor_universal(struct drm_crtc *crtc,
  703. struct drm_mode_cursor2 *req,
  704. struct drm_file *file_priv,
  705. struct drm_modeset_acquire_ctx *ctx)
  706. {
  707. struct drm_device *dev = crtc->dev;
  708. struct drm_plane *plane = crtc->cursor;
  709. struct drm_framebuffer *fb = NULL;
  710. struct drm_mode_fb_cmd2 fbreq = {
  711. .width = req->width,
  712. .height = req->height,
  713. .pixel_format = DRM_FORMAT_ARGB8888,
  714. .pitches = { req->width * 4 },
  715. .handles = { req->handle },
  716. };
  717. int32_t crtc_x, crtc_y;
  718. uint32_t crtc_w = 0, crtc_h = 0;
  719. uint32_t src_w = 0, src_h = 0;
  720. int ret = 0;
  721. BUG_ON(!plane);
  722. WARN_ON(plane->crtc != crtc && plane->crtc != NULL);
  723. /*
  724. * Obtain fb we'll be using (either new or existing) and take an extra
  725. * reference to it if fb != null. setplane will take care of dropping
  726. * the reference if the plane update fails.
  727. */
  728. if (req->flags & DRM_MODE_CURSOR_BO) {
  729. if (req->handle) {
  730. fb = drm_internal_framebuffer_create(dev, &fbreq, file_priv);
  731. if (IS_ERR(fb)) {
  732. DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
  733. return PTR_ERR(fb);
  734. }
  735. fb->hot_x = req->hot_x;
  736. fb->hot_y = req->hot_y;
  737. } else {
  738. fb = NULL;
  739. }
  740. } else {
  741. if (plane->state)
  742. fb = plane->state->fb;
  743. else
  744. fb = plane->fb;
  745. if (fb)
  746. drm_framebuffer_get(fb);
  747. }
  748. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  749. crtc_x = req->x;
  750. crtc_y = req->y;
  751. } else {
  752. crtc_x = crtc->cursor_x;
  753. crtc_y = crtc->cursor_y;
  754. }
  755. if (fb) {
  756. crtc_w = fb->width;
  757. crtc_h = fb->height;
  758. src_w = fb->width << 16;
  759. src_h = fb->height << 16;
  760. }
  761. if (drm_drv_uses_atomic_modeset(dev))
  762. ret = __setplane_atomic(plane, crtc, fb,
  763. crtc_x, crtc_y, crtc_w, crtc_h,
  764. 0, 0, src_w, src_h, ctx);
  765. else
  766. ret = __setplane_internal(plane, crtc, fb,
  767. crtc_x, crtc_y, crtc_w, crtc_h,
  768. 0, 0, src_w, src_h, ctx);
  769. if (fb)
  770. drm_framebuffer_put(fb);
  771. /* Update successful; save new cursor position, if necessary */
  772. if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
  773. crtc->cursor_x = req->x;
  774. crtc->cursor_y = req->y;
  775. }
  776. return ret;
  777. }
  778. static int drm_mode_cursor_common(struct drm_device *dev,
  779. struct drm_mode_cursor2 *req,
  780. struct drm_file *file_priv)
  781. {
  782. struct drm_crtc *crtc;
  783. struct drm_modeset_acquire_ctx ctx;
  784. int ret = 0;
  785. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  786. return -EINVAL;
  787. if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
  788. return -EINVAL;
  789. crtc = drm_crtc_find(dev, file_priv, req->crtc_id);
  790. if (!crtc) {
  791. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
  792. return -ENOENT;
  793. }
  794. drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
  795. retry:
  796. ret = drm_modeset_lock(&crtc->mutex, &ctx);
  797. if (ret)
  798. goto out;
  799. /*
  800. * If this crtc has a universal cursor plane, call that plane's update
  801. * handler rather than using legacy cursor handlers.
  802. */
  803. if (crtc->cursor) {
  804. ret = drm_modeset_lock(&crtc->cursor->mutex, &ctx);
  805. if (ret)
  806. goto out;
  807. if (!drm_lease_held(file_priv, crtc->cursor->base.id)) {
  808. ret = -EACCES;
  809. goto out;
  810. }
  811. ret = drm_mode_cursor_universal(crtc, req, file_priv, &ctx);
  812. goto out;
  813. }
  814. if (req->flags & DRM_MODE_CURSOR_BO) {
  815. if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
  816. ret = -ENXIO;
  817. goto out;
  818. }
  819. /* Turns off the cursor if handle is 0 */
  820. if (crtc->funcs->cursor_set2)
  821. ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
  822. req->width, req->height, req->hot_x, req->hot_y);
  823. else
  824. ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
  825. req->width, req->height);
  826. }
  827. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  828. if (crtc->funcs->cursor_move) {
  829. ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
  830. } else {
  831. ret = -EFAULT;
  832. goto out;
  833. }
  834. }
  835. out:
  836. if (ret == -EDEADLK) {
  837. ret = drm_modeset_backoff(&ctx);
  838. if (!ret)
  839. goto retry;
  840. }
  841. drm_modeset_drop_locks(&ctx);
  842. drm_modeset_acquire_fini(&ctx);
  843. return ret;
  844. }
  845. int drm_mode_cursor_ioctl(struct drm_device *dev,
  846. void *data, struct drm_file *file_priv)
  847. {
  848. struct drm_mode_cursor *req = data;
  849. struct drm_mode_cursor2 new_req;
  850. memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
  851. new_req.hot_x = new_req.hot_y = 0;
  852. return drm_mode_cursor_common(dev, &new_req, file_priv);
  853. }
  854. /*
  855. * Set the cursor configuration based on user request. This implements the 2nd
  856. * version of the cursor ioctl, which allows userspace to additionally specify
  857. * the hotspot of the pointer.
  858. */
  859. int drm_mode_cursor2_ioctl(struct drm_device *dev,
  860. void *data, struct drm_file *file_priv)
  861. {
  862. struct drm_mode_cursor2 *req = data;
  863. return drm_mode_cursor_common(dev, req, file_priv);
  864. }
  865. int drm_mode_page_flip_ioctl(struct drm_device *dev,
  866. void *data, struct drm_file *file_priv)
  867. {
  868. struct drm_mode_crtc_page_flip_target *page_flip = data;
  869. struct drm_crtc *crtc;
  870. struct drm_plane *plane;
  871. struct drm_framebuffer *fb = NULL, *old_fb;
  872. struct drm_pending_vblank_event *e = NULL;
  873. u32 target_vblank = page_flip->sequence;
  874. struct drm_modeset_acquire_ctx ctx;
  875. int ret = -EINVAL;
  876. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  877. return -EINVAL;
  878. if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS)
  879. return -EINVAL;
  880. if (page_flip->sequence != 0 && !(page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET))
  881. return -EINVAL;
  882. /* Only one of the DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE/RELATIVE flags
  883. * can be specified
  884. */
  885. if ((page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) == DRM_MODE_PAGE_FLIP_TARGET)
  886. return -EINVAL;
  887. if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
  888. return -EINVAL;
  889. crtc = drm_crtc_find(dev, file_priv, page_flip->crtc_id);
  890. if (!crtc)
  891. return -ENOENT;
  892. plane = crtc->primary;
  893. if (!drm_lease_held(file_priv, plane->base.id))
  894. return -EACCES;
  895. if (crtc->funcs->page_flip_target) {
  896. u32 current_vblank;
  897. int r;
  898. r = drm_crtc_vblank_get(crtc);
  899. if (r)
  900. return r;
  901. current_vblank = (u32)drm_crtc_vblank_count(crtc);
  902. switch (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) {
  903. case DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE:
  904. if ((int)(target_vblank - current_vblank) > 1) {
  905. DRM_DEBUG("Invalid absolute flip target %u, "
  906. "must be <= %u\n", target_vblank,
  907. current_vblank + 1);
  908. drm_crtc_vblank_put(crtc);
  909. return -EINVAL;
  910. }
  911. break;
  912. case DRM_MODE_PAGE_FLIP_TARGET_RELATIVE:
  913. if (target_vblank != 0 && target_vblank != 1) {
  914. DRM_DEBUG("Invalid relative flip target %u, "
  915. "must be 0 or 1\n", target_vblank);
  916. drm_crtc_vblank_put(crtc);
  917. return -EINVAL;
  918. }
  919. target_vblank += current_vblank;
  920. break;
  921. default:
  922. target_vblank = current_vblank +
  923. !(page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC);
  924. break;
  925. }
  926. } else if (crtc->funcs->page_flip == NULL ||
  927. (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET)) {
  928. return -EINVAL;
  929. }
  930. drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
  931. retry:
  932. ret = drm_modeset_lock(&crtc->mutex, &ctx);
  933. if (ret)
  934. goto out;
  935. ret = drm_modeset_lock(&plane->mutex, &ctx);
  936. if (ret)
  937. goto out;
  938. if (plane->state)
  939. old_fb = plane->state->fb;
  940. else
  941. old_fb = plane->fb;
  942. if (old_fb == NULL) {
  943. /* The framebuffer is currently unbound, presumably
  944. * due to a hotplug event, that userspace has not
  945. * yet discovered.
  946. */
  947. ret = -EBUSY;
  948. goto out;
  949. }
  950. fb = drm_framebuffer_lookup(dev, file_priv, page_flip->fb_id);
  951. if (!fb) {
  952. ret = -ENOENT;
  953. goto out;
  954. }
  955. if (plane->state) {
  956. const struct drm_plane_state *state = plane->state;
  957. ret = drm_framebuffer_check_src_coords(state->src_x,
  958. state->src_y,
  959. state->src_w,
  960. state->src_h,
  961. fb);
  962. } else {
  963. ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y,
  964. &crtc->mode, fb);
  965. }
  966. if (ret)
  967. goto out;
  968. if (old_fb->format != fb->format) {
  969. DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
  970. ret = -EINVAL;
  971. goto out;
  972. }
  973. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  974. e = kzalloc(sizeof *e, GFP_KERNEL);
  975. if (!e) {
  976. ret = -ENOMEM;
  977. goto out;
  978. }
  979. e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
  980. e->event.base.length = sizeof(e->event);
  981. e->event.vbl.user_data = page_flip->user_data;
  982. e->event.vbl.crtc_id = crtc->base.id;
  983. ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
  984. if (ret) {
  985. kfree(e);
  986. e = NULL;
  987. goto out;
  988. }
  989. }
  990. plane->old_fb = plane->fb;
  991. if (crtc->funcs->page_flip_target)
  992. ret = crtc->funcs->page_flip_target(crtc, fb, e,
  993. page_flip->flags,
  994. target_vblank,
  995. &ctx);
  996. else
  997. ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags,
  998. &ctx);
  999. if (ret) {
  1000. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
  1001. drm_event_cancel_free(dev, &e->base);
  1002. /* Keep the old fb, don't unref it. */
  1003. plane->old_fb = NULL;
  1004. } else {
  1005. if (!plane->state) {
  1006. plane->fb = fb;
  1007. drm_framebuffer_get(fb);
  1008. }
  1009. }
  1010. out:
  1011. if (fb)
  1012. drm_framebuffer_put(fb);
  1013. if (plane->old_fb)
  1014. drm_framebuffer_put(plane->old_fb);
  1015. plane->old_fb = NULL;
  1016. if (ret == -EDEADLK) {
  1017. ret = drm_modeset_backoff(&ctx);
  1018. if (!ret)
  1019. goto retry;
  1020. }
  1021. drm_modeset_drop_locks(&ctx);
  1022. drm_modeset_acquire_fini(&ctx);
  1023. if (ret && crtc->funcs->page_flip_target)
  1024. drm_crtc_vblank_put(crtc);
  1025. return ret;
  1026. }