drm_plane.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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. ret = __setplane_internal(plane, crtc, fb,
  485. crtc_x, crtc_y, crtc_w, crtc_h,
  486. src_x, src_y, src_w, src_h, &ctx);
  487. fail:
  488. if (ret == -EDEADLK) {
  489. drm_modeset_backoff(&ctx);
  490. goto retry;
  491. }
  492. drm_modeset_drop_locks(&ctx);
  493. drm_modeset_acquire_fini(&ctx);
  494. return ret;
  495. }
  496. int drm_mode_setplane(struct drm_device *dev, void *data,
  497. struct drm_file *file_priv)
  498. {
  499. struct drm_mode_set_plane *plane_req = data;
  500. struct drm_plane *plane;
  501. struct drm_crtc *crtc = NULL;
  502. struct drm_framebuffer *fb = NULL;
  503. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  504. return -EINVAL;
  505. /*
  506. * First, find the plane, crtc, and fb objects. If not available,
  507. * we don't bother to call the driver.
  508. */
  509. plane = drm_plane_find(dev, plane_req->plane_id);
  510. if (!plane) {
  511. DRM_DEBUG_KMS("Unknown plane ID %d\n",
  512. plane_req->plane_id);
  513. return -ENOENT;
  514. }
  515. if (plane_req->fb_id) {
  516. fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
  517. if (!fb) {
  518. DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
  519. plane_req->fb_id);
  520. return -ENOENT;
  521. }
  522. crtc = drm_crtc_find(dev, plane_req->crtc_id);
  523. if (!crtc) {
  524. DRM_DEBUG_KMS("Unknown crtc ID %d\n",
  525. plane_req->crtc_id);
  526. return -ENOENT;
  527. }
  528. }
  529. /*
  530. * setplane_internal will take care of deref'ing either the old or new
  531. * framebuffer depending on success.
  532. */
  533. return setplane_internal(plane, crtc, fb,
  534. plane_req->crtc_x, plane_req->crtc_y,
  535. plane_req->crtc_w, plane_req->crtc_h,
  536. plane_req->src_x, plane_req->src_y,
  537. plane_req->src_w, plane_req->src_h);
  538. }
  539. static int drm_mode_cursor_universal(struct drm_crtc *crtc,
  540. struct drm_mode_cursor2 *req,
  541. struct drm_file *file_priv,
  542. struct drm_modeset_acquire_ctx *ctx)
  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. int ret = 0;
  557. BUG_ON(!crtc->cursor);
  558. WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
  559. /*
  560. * Obtain fb we'll be using (either new or existing) and take an extra
  561. * reference to it if fb != null. setplane will take care of dropping
  562. * the reference if the plane update fails.
  563. */
  564. if (req->flags & DRM_MODE_CURSOR_BO) {
  565. if (req->handle) {
  566. fb = drm_internal_framebuffer_create(dev, &fbreq, file_priv);
  567. if (IS_ERR(fb)) {
  568. DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
  569. return PTR_ERR(fb);
  570. }
  571. fb->hot_x = req->hot_x;
  572. fb->hot_y = req->hot_y;
  573. } else {
  574. fb = NULL;
  575. }
  576. } else {
  577. fb = crtc->cursor->fb;
  578. if (fb)
  579. drm_framebuffer_get(fb);
  580. }
  581. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  582. crtc_x = req->x;
  583. crtc_y = req->y;
  584. } else {
  585. crtc_x = crtc->cursor_x;
  586. crtc_y = crtc->cursor_y;
  587. }
  588. if (fb) {
  589. crtc_w = fb->width;
  590. crtc_h = fb->height;
  591. src_w = fb->width << 16;
  592. src_h = fb->height << 16;
  593. }
  594. /*
  595. * setplane_internal will take care of deref'ing either the old or new
  596. * framebuffer depending on success.
  597. */
  598. ret = __setplane_internal(crtc->cursor, crtc, fb,
  599. crtc_x, crtc_y, crtc_w, crtc_h,
  600. 0, 0, src_w, src_h, ctx);
  601. /* Update successful; save new cursor position, if necessary */
  602. if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
  603. crtc->cursor_x = req->x;
  604. crtc->cursor_y = req->y;
  605. }
  606. return ret;
  607. }
  608. static int drm_mode_cursor_common(struct drm_device *dev,
  609. struct drm_mode_cursor2 *req,
  610. struct drm_file *file_priv)
  611. {
  612. struct drm_crtc *crtc;
  613. struct drm_modeset_acquire_ctx ctx;
  614. int ret = 0;
  615. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  616. return -EINVAL;
  617. if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
  618. return -EINVAL;
  619. crtc = drm_crtc_find(dev, req->crtc_id);
  620. if (!crtc) {
  621. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
  622. return -ENOENT;
  623. }
  624. drm_modeset_acquire_init(&ctx, 0);
  625. retry:
  626. ret = drm_modeset_lock(&crtc->mutex, &ctx);
  627. if (ret)
  628. goto out;
  629. /*
  630. * If this crtc has a universal cursor plane, call that plane's update
  631. * handler rather than using legacy cursor handlers.
  632. */
  633. if (crtc->cursor) {
  634. ret = drm_modeset_lock(&crtc->cursor->mutex, &ctx);
  635. if (ret)
  636. goto out;
  637. ret = drm_mode_cursor_universal(crtc, req, file_priv, &ctx);
  638. goto out;
  639. }
  640. if (req->flags & DRM_MODE_CURSOR_BO) {
  641. if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
  642. ret = -ENXIO;
  643. goto out;
  644. }
  645. /* Turns off the cursor if handle is 0 */
  646. if (crtc->funcs->cursor_set2)
  647. ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
  648. req->width, req->height, req->hot_x, req->hot_y);
  649. else
  650. ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
  651. req->width, req->height);
  652. }
  653. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  654. if (crtc->funcs->cursor_move) {
  655. ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
  656. } else {
  657. ret = -EFAULT;
  658. goto out;
  659. }
  660. }
  661. out:
  662. if (ret == -EDEADLK) {
  663. drm_modeset_backoff(&ctx);
  664. goto retry;
  665. }
  666. drm_modeset_drop_locks(&ctx);
  667. drm_modeset_acquire_fini(&ctx);
  668. return ret;
  669. }
  670. int drm_mode_cursor_ioctl(struct drm_device *dev,
  671. void *data, struct drm_file *file_priv)
  672. {
  673. struct drm_mode_cursor *req = data;
  674. struct drm_mode_cursor2 new_req;
  675. memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
  676. new_req.hot_x = new_req.hot_y = 0;
  677. return drm_mode_cursor_common(dev, &new_req, file_priv);
  678. }
  679. /*
  680. * Set the cursor configuration based on user request. This implements the 2nd
  681. * version of the cursor ioctl, which allows userspace to additionally specify
  682. * the hotspot of the pointer.
  683. */
  684. int drm_mode_cursor2_ioctl(struct drm_device *dev,
  685. void *data, struct drm_file *file_priv)
  686. {
  687. struct drm_mode_cursor2 *req = data;
  688. return drm_mode_cursor_common(dev, req, file_priv);
  689. }
  690. int drm_mode_page_flip_ioctl(struct drm_device *dev,
  691. void *data, struct drm_file *file_priv)
  692. {
  693. struct drm_mode_crtc_page_flip_target *page_flip = data;
  694. struct drm_crtc *crtc;
  695. struct drm_framebuffer *fb = NULL;
  696. struct drm_pending_vblank_event *e = NULL;
  697. u32 target_vblank = page_flip->sequence;
  698. struct drm_modeset_acquire_ctx ctx;
  699. int ret = -EINVAL;
  700. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  701. return -EINVAL;
  702. if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS)
  703. return -EINVAL;
  704. if (page_flip->sequence != 0 && !(page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET))
  705. return -EINVAL;
  706. /* Only one of the DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE/RELATIVE flags
  707. * can be specified
  708. */
  709. if ((page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) == DRM_MODE_PAGE_FLIP_TARGET)
  710. return -EINVAL;
  711. if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
  712. return -EINVAL;
  713. crtc = drm_crtc_find(dev, page_flip->crtc_id);
  714. if (!crtc)
  715. return -ENOENT;
  716. if (crtc->funcs->page_flip_target) {
  717. u32 current_vblank;
  718. int r;
  719. r = drm_crtc_vblank_get(crtc);
  720. if (r)
  721. return r;
  722. current_vblank = drm_crtc_vblank_count(crtc);
  723. switch (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) {
  724. case DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE:
  725. if ((int)(target_vblank - current_vblank) > 1) {
  726. DRM_DEBUG("Invalid absolute flip target %u, "
  727. "must be <= %u\n", target_vblank,
  728. current_vblank + 1);
  729. drm_crtc_vblank_put(crtc);
  730. return -EINVAL;
  731. }
  732. break;
  733. case DRM_MODE_PAGE_FLIP_TARGET_RELATIVE:
  734. if (target_vblank != 0 && target_vblank != 1) {
  735. DRM_DEBUG("Invalid relative flip target %u, "
  736. "must be 0 or 1\n", target_vblank);
  737. drm_crtc_vblank_put(crtc);
  738. return -EINVAL;
  739. }
  740. target_vblank += current_vblank;
  741. break;
  742. default:
  743. target_vblank = current_vblank +
  744. !(page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC);
  745. break;
  746. }
  747. } else if (crtc->funcs->page_flip == NULL ||
  748. (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET)) {
  749. return -EINVAL;
  750. }
  751. drm_modeset_acquire_init(&ctx, 0);
  752. retry:
  753. ret = drm_modeset_lock(&crtc->mutex, &ctx);
  754. if (ret)
  755. goto out;
  756. ret = drm_modeset_lock(&crtc->primary->mutex, &ctx);
  757. if (ret)
  758. goto out;
  759. if (crtc->primary->fb == NULL) {
  760. /* The framebuffer is currently unbound, presumably
  761. * due to a hotplug event, that userspace has not
  762. * yet discovered.
  763. */
  764. ret = -EBUSY;
  765. goto out;
  766. }
  767. fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
  768. if (!fb) {
  769. ret = -ENOENT;
  770. goto out;
  771. }
  772. if (crtc->state) {
  773. const struct drm_plane_state *state = crtc->primary->state;
  774. ret = drm_framebuffer_check_src_coords(state->src_x,
  775. state->src_y,
  776. state->src_w,
  777. state->src_h,
  778. fb);
  779. } else {
  780. ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
  781. }
  782. if (ret)
  783. goto out;
  784. if (crtc->primary->fb->format != fb->format) {
  785. DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
  786. ret = -EINVAL;
  787. goto out;
  788. }
  789. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  790. e = kzalloc(sizeof *e, GFP_KERNEL);
  791. if (!e) {
  792. ret = -ENOMEM;
  793. goto out;
  794. }
  795. e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
  796. e->event.base.length = sizeof(e->event);
  797. e->event.user_data = page_flip->user_data;
  798. ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
  799. if (ret) {
  800. kfree(e);
  801. e = NULL;
  802. goto out;
  803. }
  804. }
  805. crtc->primary->old_fb = crtc->primary->fb;
  806. if (crtc->funcs->page_flip_target)
  807. ret = crtc->funcs->page_flip_target(crtc, fb, e,
  808. page_flip->flags,
  809. target_vblank,
  810. &ctx);
  811. else
  812. ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags,
  813. &ctx);
  814. if (ret) {
  815. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
  816. drm_event_cancel_free(dev, &e->base);
  817. /* Keep the old fb, don't unref it. */
  818. crtc->primary->old_fb = NULL;
  819. } else {
  820. crtc->primary->fb = fb;
  821. /* Unref only the old framebuffer. */
  822. fb = NULL;
  823. }
  824. out:
  825. if (fb)
  826. drm_framebuffer_put(fb);
  827. if (crtc->primary->old_fb)
  828. drm_framebuffer_put(crtc->primary->old_fb);
  829. crtc->primary->old_fb = NULL;
  830. if (ret == -EDEADLK) {
  831. drm_modeset_backoff(&ctx);
  832. goto retry;
  833. }
  834. drm_modeset_drop_locks(&ctx);
  835. drm_modeset_acquire_fini(&ctx);
  836. if (ret && crtc->funcs->page_flip_target)
  837. drm_crtc_vblank_put(crtc);
  838. return ret;
  839. }