drm_plane.c 27 KB

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