drm_plane.c 26 KB

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