drm_plane.c 32 KB

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