drm_plane.c 25 KB

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