drm_plane.c 30 KB

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