drm_plane.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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. unsigned long 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_get(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. unsigned long 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_all(dev);
  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. drm_modeset_unlock_all(dev);
  211. WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
  212. if (plane->state && plane->funcs->atomic_destroy_state)
  213. plane->funcs->atomic_destroy_state(plane, plane->state);
  214. kfree(plane->name);
  215. memset(plane, 0, sizeof(*plane));
  216. }
  217. EXPORT_SYMBOL(drm_plane_cleanup);
  218. /**
  219. * drm_plane_from_index - find the registered plane at an index
  220. * @dev: DRM device
  221. * @idx: index of registered plane to find for
  222. *
  223. * Given a plane index, return the registered plane from DRM device's
  224. * list of planes with matching index.
  225. */
  226. struct drm_plane *
  227. drm_plane_from_index(struct drm_device *dev, int idx)
  228. {
  229. struct drm_plane *plane;
  230. drm_for_each_plane(plane, dev)
  231. if (idx == plane->index)
  232. return plane;
  233. return NULL;
  234. }
  235. EXPORT_SYMBOL(drm_plane_from_index);
  236. /**
  237. * drm_plane_force_disable - Forcibly disable a plane
  238. * @plane: plane to disable
  239. *
  240. * Forces the plane to be disabled.
  241. *
  242. * Used when the plane's current framebuffer is destroyed,
  243. * and when restoring fbdev mode.
  244. */
  245. void drm_plane_force_disable(struct drm_plane *plane)
  246. {
  247. int ret;
  248. if (!plane->fb)
  249. return;
  250. plane->old_fb = plane->fb;
  251. ret = plane->funcs->disable_plane(plane);
  252. if (ret) {
  253. DRM_ERROR("failed to disable plane with busy fb\n");
  254. plane->old_fb = NULL;
  255. return;
  256. }
  257. /* disconnect the plane from the fb and crtc: */
  258. drm_framebuffer_unreference(plane->old_fb);
  259. plane->old_fb = NULL;
  260. plane->fb = NULL;
  261. plane->crtc = NULL;
  262. }
  263. EXPORT_SYMBOL(drm_plane_force_disable);
  264. /**
  265. * drm_mode_plane_set_obj_prop - set the value of a property
  266. * @plane: drm plane object to set property value for
  267. * @property: property to set
  268. * @value: value the property should be set to
  269. *
  270. * This functions sets a given property on a given plane object. This function
  271. * calls the driver's ->set_property callback and changes the software state of
  272. * the property if the callback succeeds.
  273. *
  274. * Returns:
  275. * Zero on success, error code on failure.
  276. */
  277. int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
  278. struct drm_property *property,
  279. uint64_t value)
  280. {
  281. int ret = -EINVAL;
  282. struct drm_mode_object *obj = &plane->base;
  283. if (plane->funcs->set_property)
  284. ret = plane->funcs->set_property(plane, property, value);
  285. if (!ret)
  286. drm_object_property_set_value(obj, property, value);
  287. return ret;
  288. }
  289. EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
  290. int drm_mode_getplane_res(struct drm_device *dev, void *data,
  291. struct drm_file *file_priv)
  292. {
  293. struct drm_mode_get_plane_res *plane_resp = data;
  294. struct drm_mode_config *config;
  295. struct drm_plane *plane;
  296. uint32_t __user *plane_ptr;
  297. int copied = 0;
  298. unsigned num_planes;
  299. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  300. return -EINVAL;
  301. config = &dev->mode_config;
  302. if (file_priv->universal_planes)
  303. num_planes = config->num_total_plane;
  304. else
  305. num_planes = config->num_overlay_plane;
  306. /*
  307. * This ioctl is called twice, once to determine how much space is
  308. * needed, and the 2nd time to fill it.
  309. */
  310. if (num_planes &&
  311. (plane_resp->count_planes >= num_planes)) {
  312. plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
  313. /* Plane lists are invariant, no locking needed. */
  314. drm_for_each_plane(plane, dev) {
  315. /*
  316. * Unless userspace set the 'universal planes'
  317. * capability bit, only advertise overlays.
  318. */
  319. if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
  320. !file_priv->universal_planes)
  321. continue;
  322. if (put_user(plane->base.id, plane_ptr + copied))
  323. return -EFAULT;
  324. copied++;
  325. }
  326. }
  327. plane_resp->count_planes = num_planes;
  328. return 0;
  329. }
  330. int drm_mode_getplane(struct drm_device *dev, void *data,
  331. struct drm_file *file_priv)
  332. {
  333. struct drm_mode_get_plane *plane_resp = data;
  334. struct drm_plane *plane;
  335. uint32_t __user *format_ptr;
  336. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  337. return -EINVAL;
  338. plane = drm_plane_find(dev, plane_resp->plane_id);
  339. if (!plane)
  340. return -ENOENT;
  341. drm_modeset_lock(&plane->mutex, NULL);
  342. if (plane->crtc)
  343. plane_resp->crtc_id = plane->crtc->base.id;
  344. else
  345. plane_resp->crtc_id = 0;
  346. if (plane->fb)
  347. plane_resp->fb_id = plane->fb->base.id;
  348. else
  349. plane_resp->fb_id = 0;
  350. drm_modeset_unlock(&plane->mutex);
  351. plane_resp->plane_id = plane->base.id;
  352. plane_resp->possible_crtcs = plane->possible_crtcs;
  353. plane_resp->gamma_size = 0;
  354. /*
  355. * This ioctl is called twice, once to determine how much space is
  356. * needed, and the 2nd time to fill it.
  357. */
  358. if (plane->format_count &&
  359. (plane_resp->count_format_types >= plane->format_count)) {
  360. format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
  361. if (copy_to_user(format_ptr,
  362. plane->format_types,
  363. sizeof(uint32_t) * plane->format_count)) {
  364. return -EFAULT;
  365. }
  366. }
  367. plane_resp->count_format_types = plane->format_count;
  368. return 0;
  369. }
  370. int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
  371. {
  372. unsigned int i;
  373. for (i = 0; i < plane->format_count; i++) {
  374. if (format == plane->format_types[i])
  375. return 0;
  376. }
  377. return -EINVAL;
  378. }
  379. /*
  380. * setplane_internal - setplane handler for internal callers
  381. *
  382. * Note that we assume an extra reference has already been taken on fb. If the
  383. * update fails, this reference will be dropped before return; if it succeeds,
  384. * the previous framebuffer (if any) will be unreferenced instead.
  385. *
  386. * src_{x,y,w,h} are provided in 16.16 fixed point format
  387. */
  388. static int __setplane_internal(struct drm_plane *plane,
  389. struct drm_crtc *crtc,
  390. struct drm_framebuffer *fb,
  391. int32_t crtc_x, int32_t crtc_y,
  392. uint32_t crtc_w, uint32_t crtc_h,
  393. /* src_{x,y,w,h} values are 16.16 fixed point */
  394. uint32_t src_x, uint32_t src_y,
  395. uint32_t src_w, uint32_t src_h)
  396. {
  397. int ret = 0;
  398. /* No fb means shut it down */
  399. if (!fb) {
  400. plane->old_fb = plane->fb;
  401. ret = plane->funcs->disable_plane(plane);
  402. if (!ret) {
  403. plane->crtc = NULL;
  404. plane->fb = NULL;
  405. } else {
  406. plane->old_fb = NULL;
  407. }
  408. goto out;
  409. }
  410. /* Check whether this plane is usable on this CRTC */
  411. if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
  412. DRM_DEBUG_KMS("Invalid crtc for plane\n");
  413. ret = -EINVAL;
  414. goto out;
  415. }
  416. /* Check whether this plane supports the fb pixel format. */
  417. ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
  418. if (ret) {
  419. struct drm_format_name_buf format_name;
  420. DRM_DEBUG_KMS("Invalid pixel format %s\n",
  421. drm_get_format_name(fb->pixel_format,
  422. &format_name));
  423. goto out;
  424. }
  425. /* Give drivers some help against integer overflows */
  426. if (crtc_w > INT_MAX ||
  427. crtc_x > INT_MAX - (int32_t) crtc_w ||
  428. crtc_h > INT_MAX ||
  429. crtc_y > INT_MAX - (int32_t) crtc_h) {
  430. DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
  431. crtc_w, crtc_h, crtc_x, crtc_y);
  432. ret = -ERANGE;
  433. goto out;
  434. }
  435. ret = drm_framebuffer_check_src_coords(src_x, src_y, src_w, src_h, fb);
  436. if (ret)
  437. goto out;
  438. plane->old_fb = plane->fb;
  439. ret = plane->funcs->update_plane(plane, crtc, fb,
  440. crtc_x, crtc_y, crtc_w, crtc_h,
  441. src_x, src_y, src_w, src_h);
  442. if (!ret) {
  443. plane->crtc = crtc;
  444. plane->fb = fb;
  445. fb = NULL;
  446. } else {
  447. plane->old_fb = NULL;
  448. }
  449. out:
  450. if (fb)
  451. drm_framebuffer_unreference(fb);
  452. if (plane->old_fb)
  453. drm_framebuffer_unreference(plane->old_fb);
  454. plane->old_fb = NULL;
  455. return ret;
  456. }
  457. static int setplane_internal(struct drm_plane *plane,
  458. struct drm_crtc *crtc,
  459. struct drm_framebuffer *fb,
  460. int32_t crtc_x, int32_t crtc_y,
  461. uint32_t crtc_w, uint32_t crtc_h,
  462. /* src_{x,y,w,h} values are 16.16 fixed point */
  463. uint32_t src_x, uint32_t src_y,
  464. uint32_t src_w, uint32_t src_h)
  465. {
  466. int ret;
  467. drm_modeset_lock_all(plane->dev);
  468. ret = __setplane_internal(plane, crtc, fb,
  469. crtc_x, crtc_y, crtc_w, crtc_h,
  470. src_x, src_y, src_w, src_h);
  471. drm_modeset_unlock_all(plane->dev);
  472. return ret;
  473. }
  474. int drm_mode_setplane(struct drm_device *dev, void *data,
  475. struct drm_file *file_priv)
  476. {
  477. struct drm_mode_set_plane *plane_req = data;
  478. struct drm_plane *plane;
  479. struct drm_crtc *crtc = NULL;
  480. struct drm_framebuffer *fb = NULL;
  481. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  482. return -EINVAL;
  483. /*
  484. * First, find the plane, crtc, and fb objects. If not available,
  485. * we don't bother to call the driver.
  486. */
  487. plane = drm_plane_find(dev, plane_req->plane_id);
  488. if (!plane) {
  489. DRM_DEBUG_KMS("Unknown plane ID %d\n",
  490. plane_req->plane_id);
  491. return -ENOENT;
  492. }
  493. if (plane_req->fb_id) {
  494. fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
  495. if (!fb) {
  496. DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
  497. plane_req->fb_id);
  498. return -ENOENT;
  499. }
  500. crtc = drm_crtc_find(dev, plane_req->crtc_id);
  501. if (!crtc) {
  502. DRM_DEBUG_KMS("Unknown crtc ID %d\n",
  503. plane_req->crtc_id);
  504. return -ENOENT;
  505. }
  506. }
  507. /*
  508. * setplane_internal will take care of deref'ing either the old or new
  509. * framebuffer depending on success.
  510. */
  511. return setplane_internal(plane, crtc, fb,
  512. plane_req->crtc_x, plane_req->crtc_y,
  513. plane_req->crtc_w, plane_req->crtc_h,
  514. plane_req->src_x, plane_req->src_y,
  515. plane_req->src_w, plane_req->src_h);
  516. }
  517. static int drm_mode_cursor_universal(struct drm_crtc *crtc,
  518. struct drm_mode_cursor2 *req,
  519. struct drm_file *file_priv)
  520. {
  521. struct drm_device *dev = crtc->dev;
  522. struct drm_framebuffer *fb = NULL;
  523. struct drm_mode_fb_cmd2 fbreq = {
  524. .width = req->width,
  525. .height = req->height,
  526. .pixel_format = DRM_FORMAT_ARGB8888,
  527. .pitches = { req->width * 4 },
  528. .handles = { req->handle },
  529. };
  530. int32_t crtc_x, crtc_y;
  531. uint32_t crtc_w = 0, crtc_h = 0;
  532. uint32_t src_w = 0, src_h = 0;
  533. int ret = 0;
  534. BUG_ON(!crtc->cursor);
  535. WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
  536. /*
  537. * Obtain fb we'll be using (either new or existing) and take an extra
  538. * reference to it if fb != null. setplane will take care of dropping
  539. * the reference if the plane update fails.
  540. */
  541. if (req->flags & DRM_MODE_CURSOR_BO) {
  542. if (req->handle) {
  543. fb = drm_internal_framebuffer_create(dev, &fbreq, file_priv);
  544. if (IS_ERR(fb)) {
  545. DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
  546. return PTR_ERR(fb);
  547. }
  548. fb->hot_x = req->hot_x;
  549. fb->hot_y = req->hot_y;
  550. } else {
  551. fb = NULL;
  552. }
  553. } else {
  554. fb = crtc->cursor->fb;
  555. if (fb)
  556. drm_framebuffer_reference(fb);
  557. }
  558. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  559. crtc_x = req->x;
  560. crtc_y = req->y;
  561. } else {
  562. crtc_x = crtc->cursor_x;
  563. crtc_y = crtc->cursor_y;
  564. }
  565. if (fb) {
  566. crtc_w = fb->width;
  567. crtc_h = fb->height;
  568. src_w = fb->width << 16;
  569. src_h = fb->height << 16;
  570. }
  571. /*
  572. * setplane_internal will take care of deref'ing either the old or new
  573. * framebuffer depending on success.
  574. */
  575. ret = __setplane_internal(crtc->cursor, crtc, fb,
  576. crtc_x, crtc_y, crtc_w, crtc_h,
  577. 0, 0, src_w, src_h);
  578. /* Update successful; save new cursor position, if necessary */
  579. if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
  580. crtc->cursor_x = req->x;
  581. crtc->cursor_y = req->y;
  582. }
  583. return ret;
  584. }
  585. static int drm_mode_cursor_common(struct drm_device *dev,
  586. struct drm_mode_cursor2 *req,
  587. struct drm_file *file_priv)
  588. {
  589. struct drm_crtc *crtc;
  590. int ret = 0;
  591. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  592. return -EINVAL;
  593. if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
  594. return -EINVAL;
  595. crtc = drm_crtc_find(dev, req->crtc_id);
  596. if (!crtc) {
  597. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
  598. return -ENOENT;
  599. }
  600. /*
  601. * If this crtc has a universal cursor plane, call that plane's update
  602. * handler rather than using legacy cursor handlers.
  603. */
  604. drm_modeset_lock_crtc(crtc, crtc->cursor);
  605. if (crtc->cursor) {
  606. ret = drm_mode_cursor_universal(crtc, req, file_priv);
  607. goto out;
  608. }
  609. if (req->flags & DRM_MODE_CURSOR_BO) {
  610. if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
  611. ret = -ENXIO;
  612. goto out;
  613. }
  614. /* Turns off the cursor if handle is 0 */
  615. if (crtc->funcs->cursor_set2)
  616. ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
  617. req->width, req->height, req->hot_x, req->hot_y);
  618. else
  619. ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
  620. req->width, req->height);
  621. }
  622. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  623. if (crtc->funcs->cursor_move) {
  624. ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
  625. } else {
  626. ret = -EFAULT;
  627. goto out;
  628. }
  629. }
  630. out:
  631. drm_modeset_unlock_crtc(crtc);
  632. return ret;
  633. }
  634. int drm_mode_cursor_ioctl(struct drm_device *dev,
  635. void *data, struct drm_file *file_priv)
  636. {
  637. struct drm_mode_cursor *req = data;
  638. struct drm_mode_cursor2 new_req;
  639. memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
  640. new_req.hot_x = new_req.hot_y = 0;
  641. return drm_mode_cursor_common(dev, &new_req, file_priv);
  642. }
  643. /*
  644. * Set the cursor configuration based on user request. This implements the 2nd
  645. * version of the cursor ioctl, which allows userspace to additionally specify
  646. * the hotspot of the pointer.
  647. */
  648. int drm_mode_cursor2_ioctl(struct drm_device *dev,
  649. void *data, struct drm_file *file_priv)
  650. {
  651. struct drm_mode_cursor2 *req = data;
  652. return drm_mode_cursor_common(dev, req, file_priv);
  653. }
  654. int drm_mode_page_flip_ioctl(struct drm_device *dev,
  655. void *data, struct drm_file *file_priv)
  656. {
  657. struct drm_mode_crtc_page_flip_target *page_flip = data;
  658. struct drm_crtc *crtc;
  659. struct drm_framebuffer *fb = NULL;
  660. struct drm_pending_vblank_event *e = NULL;
  661. u32 target_vblank = page_flip->sequence;
  662. int ret = -EINVAL;
  663. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  664. return -EINVAL;
  665. if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS)
  666. return -EINVAL;
  667. if (page_flip->sequence != 0 && !(page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET))
  668. return -EINVAL;
  669. /* Only one of the DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE/RELATIVE flags
  670. * can be specified
  671. */
  672. if ((page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) == DRM_MODE_PAGE_FLIP_TARGET)
  673. return -EINVAL;
  674. if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
  675. return -EINVAL;
  676. crtc = drm_crtc_find(dev, page_flip->crtc_id);
  677. if (!crtc)
  678. return -ENOENT;
  679. if (crtc->funcs->page_flip_target) {
  680. u32 current_vblank;
  681. int r;
  682. r = drm_crtc_vblank_get(crtc);
  683. if (r)
  684. return r;
  685. current_vblank = drm_crtc_vblank_count(crtc);
  686. switch (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) {
  687. case DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE:
  688. if ((int)(target_vblank - current_vblank) > 1) {
  689. DRM_DEBUG("Invalid absolute flip target %u, "
  690. "must be <= %u\n", target_vblank,
  691. current_vblank + 1);
  692. drm_crtc_vblank_put(crtc);
  693. return -EINVAL;
  694. }
  695. break;
  696. case DRM_MODE_PAGE_FLIP_TARGET_RELATIVE:
  697. if (target_vblank != 0 && target_vblank != 1) {
  698. DRM_DEBUG("Invalid relative flip target %u, "
  699. "must be 0 or 1\n", target_vblank);
  700. drm_crtc_vblank_put(crtc);
  701. return -EINVAL;
  702. }
  703. target_vblank += current_vblank;
  704. break;
  705. default:
  706. target_vblank = current_vblank +
  707. !(page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC);
  708. break;
  709. }
  710. } else if (crtc->funcs->page_flip == NULL ||
  711. (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET)) {
  712. return -EINVAL;
  713. }
  714. drm_modeset_lock_crtc(crtc, crtc->primary);
  715. if (crtc->primary->fb == NULL) {
  716. /* The framebuffer is currently unbound, presumably
  717. * due to a hotplug event, that userspace has not
  718. * yet discovered.
  719. */
  720. ret = -EBUSY;
  721. goto out;
  722. }
  723. fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
  724. if (!fb) {
  725. ret = -ENOENT;
  726. goto out;
  727. }
  728. if (crtc->state) {
  729. const struct drm_plane_state *state = crtc->primary->state;
  730. ret = drm_framebuffer_check_src_coords(state->src_x,
  731. state->src_y,
  732. state->src_w,
  733. state->src_h,
  734. fb);
  735. } else {
  736. ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
  737. }
  738. if (ret)
  739. goto out;
  740. if (crtc->primary->fb->pixel_format != fb->pixel_format) {
  741. DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
  742. ret = -EINVAL;
  743. goto out;
  744. }
  745. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  746. e = kzalloc(sizeof *e, GFP_KERNEL);
  747. if (!e) {
  748. ret = -ENOMEM;
  749. goto out;
  750. }
  751. e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
  752. e->event.base.length = sizeof(e->event);
  753. e->event.user_data = page_flip->user_data;
  754. ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
  755. if (ret) {
  756. kfree(e);
  757. goto out;
  758. }
  759. }
  760. crtc->primary->old_fb = crtc->primary->fb;
  761. if (crtc->funcs->page_flip_target)
  762. ret = crtc->funcs->page_flip_target(crtc, fb, e,
  763. page_flip->flags,
  764. target_vblank);
  765. else
  766. ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
  767. if (ret) {
  768. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
  769. drm_event_cancel_free(dev, &e->base);
  770. /* Keep the old fb, don't unref it. */
  771. crtc->primary->old_fb = NULL;
  772. } else {
  773. crtc->primary->fb = fb;
  774. /* Unref only the old framebuffer. */
  775. fb = NULL;
  776. }
  777. out:
  778. if (ret && crtc->funcs->page_flip_target)
  779. drm_crtc_vblank_put(crtc);
  780. if (fb)
  781. drm_framebuffer_unreference(fb);
  782. if (crtc->primary->old_fb)
  783. drm_framebuffer_unreference(crtc->primary->old_fb);
  784. crtc->primary->old_fb = NULL;
  785. drm_modeset_unlock_crtc(crtc);
  786. return ret;
  787. }