drm_plane.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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_crtc_id, 0);
  125. drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
  126. drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
  127. drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
  128. drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
  129. drm_object_attach_property(&plane->base, config->prop_src_x, 0);
  130. drm_object_attach_property(&plane->base, config->prop_src_y, 0);
  131. drm_object_attach_property(&plane->base, config->prop_src_w, 0);
  132. drm_object_attach_property(&plane->base, config->prop_src_h, 0);
  133. }
  134. return 0;
  135. }
  136. EXPORT_SYMBOL(drm_universal_plane_init);
  137. int drm_plane_register_all(struct drm_device *dev)
  138. {
  139. struct drm_plane *plane;
  140. int ret = 0;
  141. drm_for_each_plane(plane, dev) {
  142. if (plane->funcs->late_register)
  143. ret = plane->funcs->late_register(plane);
  144. if (ret)
  145. return ret;
  146. }
  147. return 0;
  148. }
  149. void drm_plane_unregister_all(struct drm_device *dev)
  150. {
  151. struct drm_plane *plane;
  152. drm_for_each_plane(plane, dev) {
  153. if (plane->funcs->early_unregister)
  154. plane->funcs->early_unregister(plane);
  155. }
  156. }
  157. /**
  158. * drm_plane_init - Initialize a legacy plane
  159. * @dev: DRM device
  160. * @plane: plane object to init
  161. * @possible_crtcs: bitmask of possible CRTCs
  162. * @funcs: callbacks for the new plane
  163. * @formats: array of supported formats (DRM_FORMAT\_\*)
  164. * @format_count: number of elements in @formats
  165. * @is_primary: plane type (primary vs overlay)
  166. *
  167. * Legacy API to initialize a DRM plane.
  168. *
  169. * New drivers should call drm_universal_plane_init() instead.
  170. *
  171. * Returns:
  172. * Zero on success, error code on failure.
  173. */
  174. int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
  175. unsigned long possible_crtcs,
  176. const struct drm_plane_funcs *funcs,
  177. const uint32_t *formats, unsigned int format_count,
  178. bool is_primary)
  179. {
  180. enum drm_plane_type type;
  181. type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
  182. return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
  183. formats, format_count, type, NULL);
  184. }
  185. EXPORT_SYMBOL(drm_plane_init);
  186. /**
  187. * drm_plane_cleanup - Clean up the core plane usage
  188. * @plane: plane to cleanup
  189. *
  190. * This function cleans up @plane and removes it from the DRM mode setting
  191. * core. Note that the function does *not* free the plane structure itself,
  192. * this is the responsibility of the caller.
  193. */
  194. void drm_plane_cleanup(struct drm_plane *plane)
  195. {
  196. struct drm_device *dev = plane->dev;
  197. drm_modeset_lock_all(dev);
  198. kfree(plane->format_types);
  199. drm_mode_object_unregister(dev, &plane->base);
  200. BUG_ON(list_empty(&plane->head));
  201. /* Note that the plane_list is considered to be static; should we
  202. * remove the drm_plane at runtime we would have to decrement all
  203. * the indices on the drm_plane after us in the plane_list.
  204. */
  205. list_del(&plane->head);
  206. dev->mode_config.num_total_plane--;
  207. if (plane->type == DRM_PLANE_TYPE_OVERLAY)
  208. dev->mode_config.num_overlay_plane--;
  209. drm_modeset_unlock_all(dev);
  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.
  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->crtc)
  342. plane_resp->crtc_id = plane->crtc->base.id;
  343. else
  344. plane_resp->crtc_id = 0;
  345. if (plane->fb)
  346. plane_resp->fb_id = plane->fb->base.id;
  347. else
  348. plane_resp->fb_id = 0;
  349. drm_modeset_unlock(&plane->mutex);
  350. plane_resp->plane_id = plane->base.id;
  351. plane_resp->possible_crtcs = plane->possible_crtcs;
  352. plane_resp->gamma_size = 0;
  353. /*
  354. * This ioctl is called twice, once to determine how much space is
  355. * needed, and the 2nd time to fill it.
  356. */
  357. if (plane->format_count &&
  358. (plane_resp->count_format_types >= plane->format_count)) {
  359. format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
  360. if (copy_to_user(format_ptr,
  361. plane->format_types,
  362. sizeof(uint32_t) * plane->format_count)) {
  363. return -EFAULT;
  364. }
  365. }
  366. plane_resp->count_format_types = plane->format_count;
  367. return 0;
  368. }
  369. int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
  370. {
  371. unsigned int i;
  372. for (i = 0; i < plane->format_count; i++) {
  373. if (format == plane->format_types[i])
  374. return 0;
  375. }
  376. return -EINVAL;
  377. }
  378. /*
  379. * setplane_internal - setplane handler for internal callers
  380. *
  381. * Note that we assume an extra reference has already been taken on fb. If the
  382. * update fails, this reference will be dropped before return; if it succeeds,
  383. * the previous framebuffer (if any) will be unreferenced instead.
  384. *
  385. * src_{x,y,w,h} are provided in 16.16 fixed point format
  386. */
  387. static int __setplane_internal(struct drm_plane *plane,
  388. struct drm_crtc *crtc,
  389. struct drm_framebuffer *fb,
  390. int32_t crtc_x, int32_t crtc_y,
  391. uint32_t crtc_w, uint32_t crtc_h,
  392. /* src_{x,y,w,h} values are 16.16 fixed point */
  393. uint32_t src_x, uint32_t src_y,
  394. uint32_t src_w, uint32_t src_h)
  395. {
  396. int ret = 0;
  397. /* No fb means shut it down */
  398. if (!fb) {
  399. plane->old_fb = plane->fb;
  400. ret = plane->funcs->disable_plane(plane);
  401. if (!ret) {
  402. plane->crtc = NULL;
  403. plane->fb = NULL;
  404. } else {
  405. plane->old_fb = NULL;
  406. }
  407. goto out;
  408. }
  409. /* Check whether this plane is usable on this CRTC */
  410. if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
  411. DRM_DEBUG_KMS("Invalid crtc for plane\n");
  412. ret = -EINVAL;
  413. goto out;
  414. }
  415. /* Check whether this plane supports the fb pixel format. */
  416. ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
  417. if (ret) {
  418. char *format_name = drm_get_format_name(fb->pixel_format);
  419. DRM_DEBUG_KMS("Invalid pixel format %s\n", format_name);
  420. kfree(format_name);
  421. goto out;
  422. }
  423. /* Give drivers some help against integer overflows */
  424. if (crtc_w > INT_MAX ||
  425. crtc_x > INT_MAX - (int32_t) crtc_w ||
  426. crtc_h > INT_MAX ||
  427. crtc_y > INT_MAX - (int32_t) crtc_h) {
  428. DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
  429. crtc_w, crtc_h, crtc_x, crtc_y);
  430. ret = -ERANGE;
  431. goto out;
  432. }
  433. ret = drm_framebuffer_check_src_coords(src_x, src_y, src_w, src_h, fb);
  434. if (ret)
  435. goto out;
  436. plane->old_fb = plane->fb;
  437. ret = plane->funcs->update_plane(plane, crtc, fb,
  438. crtc_x, crtc_y, crtc_w, crtc_h,
  439. src_x, src_y, src_w, src_h);
  440. if (!ret) {
  441. plane->crtc = crtc;
  442. plane->fb = fb;
  443. fb = NULL;
  444. } else {
  445. plane->old_fb = NULL;
  446. }
  447. out:
  448. if (fb)
  449. drm_framebuffer_unreference(fb);
  450. if (plane->old_fb)
  451. drm_framebuffer_unreference(plane->old_fb);
  452. plane->old_fb = NULL;
  453. return ret;
  454. }
  455. static int setplane_internal(struct drm_plane *plane,
  456. struct drm_crtc *crtc,
  457. struct drm_framebuffer *fb,
  458. int32_t crtc_x, int32_t crtc_y,
  459. uint32_t crtc_w, uint32_t crtc_h,
  460. /* src_{x,y,w,h} values are 16.16 fixed point */
  461. uint32_t src_x, uint32_t src_y,
  462. uint32_t src_w, uint32_t src_h)
  463. {
  464. int ret;
  465. drm_modeset_lock_all(plane->dev);
  466. ret = __setplane_internal(plane, crtc, fb,
  467. crtc_x, crtc_y, crtc_w, crtc_h,
  468. src_x, src_y, src_w, src_h);
  469. drm_modeset_unlock_all(plane->dev);
  470. return ret;
  471. }
  472. int drm_mode_setplane(struct drm_device *dev, void *data,
  473. struct drm_file *file_priv)
  474. {
  475. struct drm_mode_set_plane *plane_req = data;
  476. struct drm_plane *plane;
  477. struct drm_crtc *crtc = NULL;
  478. struct drm_framebuffer *fb = NULL;
  479. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  480. return -EINVAL;
  481. /*
  482. * First, find the plane, crtc, and fb objects. If not available,
  483. * we don't bother to call the driver.
  484. */
  485. plane = drm_plane_find(dev, plane_req->plane_id);
  486. if (!plane) {
  487. DRM_DEBUG_KMS("Unknown plane ID %d\n",
  488. plane_req->plane_id);
  489. return -ENOENT;
  490. }
  491. if (plane_req->fb_id) {
  492. fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
  493. if (!fb) {
  494. DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
  495. plane_req->fb_id);
  496. return -ENOENT;
  497. }
  498. crtc = drm_crtc_find(dev, plane_req->crtc_id);
  499. if (!crtc) {
  500. DRM_DEBUG_KMS("Unknown crtc ID %d\n",
  501. plane_req->crtc_id);
  502. return -ENOENT;
  503. }
  504. }
  505. /*
  506. * setplane_internal will take care of deref'ing either the old or new
  507. * framebuffer depending on success.
  508. */
  509. return setplane_internal(plane, crtc, fb,
  510. plane_req->crtc_x, plane_req->crtc_y,
  511. plane_req->crtc_w, plane_req->crtc_h,
  512. plane_req->src_x, plane_req->src_y,
  513. plane_req->src_w, plane_req->src_h);
  514. }
  515. static int drm_mode_cursor_universal(struct drm_crtc *crtc,
  516. struct drm_mode_cursor2 *req,
  517. struct drm_file *file_priv)
  518. {
  519. struct drm_device *dev = crtc->dev;
  520. struct drm_framebuffer *fb = NULL;
  521. struct drm_mode_fb_cmd2 fbreq = {
  522. .width = req->width,
  523. .height = req->height,
  524. .pixel_format = DRM_FORMAT_ARGB8888,
  525. .pitches = { req->width * 4 },
  526. .handles = { req->handle },
  527. };
  528. int32_t crtc_x, crtc_y;
  529. uint32_t crtc_w = 0, crtc_h = 0;
  530. uint32_t src_w = 0, src_h = 0;
  531. int ret = 0;
  532. BUG_ON(!crtc->cursor);
  533. WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
  534. /*
  535. * Obtain fb we'll be using (either new or existing) and take an extra
  536. * reference to it if fb != null. setplane will take care of dropping
  537. * the reference if the plane update fails.
  538. */
  539. if (req->flags & DRM_MODE_CURSOR_BO) {
  540. if (req->handle) {
  541. fb = drm_internal_framebuffer_create(dev, &fbreq, file_priv);
  542. if (IS_ERR(fb)) {
  543. DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
  544. return PTR_ERR(fb);
  545. }
  546. fb->hot_x = req->hot_x;
  547. fb->hot_y = req->hot_y;
  548. } else {
  549. fb = NULL;
  550. }
  551. } else {
  552. fb = crtc->cursor->fb;
  553. if (fb)
  554. drm_framebuffer_reference(fb);
  555. }
  556. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  557. crtc_x = req->x;
  558. crtc_y = req->y;
  559. } else {
  560. crtc_x = crtc->cursor_x;
  561. crtc_y = crtc->cursor_y;
  562. }
  563. if (fb) {
  564. crtc_w = fb->width;
  565. crtc_h = fb->height;
  566. src_w = fb->width << 16;
  567. src_h = fb->height << 16;
  568. }
  569. /*
  570. * setplane_internal will take care of deref'ing either the old or new
  571. * framebuffer depending on success.
  572. */
  573. ret = __setplane_internal(crtc->cursor, crtc, fb,
  574. crtc_x, crtc_y, crtc_w, crtc_h,
  575. 0, 0, src_w, src_h);
  576. /* Update successful; save new cursor position, if necessary */
  577. if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
  578. crtc->cursor_x = req->x;
  579. crtc->cursor_y = req->y;
  580. }
  581. return ret;
  582. }
  583. static int drm_mode_cursor_common(struct drm_device *dev,
  584. struct drm_mode_cursor2 *req,
  585. struct drm_file *file_priv)
  586. {
  587. struct drm_crtc *crtc;
  588. int ret = 0;
  589. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  590. return -EINVAL;
  591. if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
  592. return -EINVAL;
  593. crtc = drm_crtc_find(dev, req->crtc_id);
  594. if (!crtc) {
  595. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
  596. return -ENOENT;
  597. }
  598. /*
  599. * If this crtc has a universal cursor plane, call that plane's update
  600. * handler rather than using legacy cursor handlers.
  601. */
  602. drm_modeset_lock_crtc(crtc, crtc->cursor);
  603. if (crtc->cursor) {
  604. ret = drm_mode_cursor_universal(crtc, req, file_priv);
  605. goto out;
  606. }
  607. if (req->flags & DRM_MODE_CURSOR_BO) {
  608. if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
  609. ret = -ENXIO;
  610. goto out;
  611. }
  612. /* Turns off the cursor if handle is 0 */
  613. if (crtc->funcs->cursor_set2)
  614. ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
  615. req->width, req->height, req->hot_x, req->hot_y);
  616. else
  617. ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
  618. req->width, req->height);
  619. }
  620. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  621. if (crtc->funcs->cursor_move) {
  622. ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
  623. } else {
  624. ret = -EFAULT;
  625. goto out;
  626. }
  627. }
  628. out:
  629. drm_modeset_unlock_crtc(crtc);
  630. return ret;
  631. }
  632. int drm_mode_cursor_ioctl(struct drm_device *dev,
  633. void *data, struct drm_file *file_priv)
  634. {
  635. struct drm_mode_cursor *req = data;
  636. struct drm_mode_cursor2 new_req;
  637. memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
  638. new_req.hot_x = new_req.hot_y = 0;
  639. return drm_mode_cursor_common(dev, &new_req, file_priv);
  640. }
  641. /*
  642. * Set the cursor configuration based on user request. This implements the 2nd
  643. * version of the cursor ioctl, which allows userspace to additionally specify
  644. * the hotspot of the pointer.
  645. */
  646. int drm_mode_cursor2_ioctl(struct drm_device *dev,
  647. void *data, struct drm_file *file_priv)
  648. {
  649. struct drm_mode_cursor2 *req = data;
  650. return drm_mode_cursor_common(dev, req, file_priv);
  651. }
  652. int drm_mode_page_flip_ioctl(struct drm_device *dev,
  653. void *data, struct drm_file *file_priv)
  654. {
  655. struct drm_mode_crtc_page_flip_target *page_flip = data;
  656. struct drm_crtc *crtc;
  657. struct drm_framebuffer *fb = NULL;
  658. struct drm_pending_vblank_event *e = NULL;
  659. u32 target_vblank = page_flip->sequence;
  660. int ret = -EINVAL;
  661. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  662. return -EINVAL;
  663. if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS)
  664. return -EINVAL;
  665. if (page_flip->sequence != 0 && !(page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET))
  666. return -EINVAL;
  667. /* Only one of the DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE/RELATIVE flags
  668. * can be specified
  669. */
  670. if ((page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) == DRM_MODE_PAGE_FLIP_TARGET)
  671. return -EINVAL;
  672. if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
  673. return -EINVAL;
  674. crtc = drm_crtc_find(dev, page_flip->crtc_id);
  675. if (!crtc)
  676. return -ENOENT;
  677. if (crtc->funcs->page_flip_target) {
  678. u32 current_vblank;
  679. int r;
  680. r = drm_crtc_vblank_get(crtc);
  681. if (r)
  682. return r;
  683. current_vblank = drm_crtc_vblank_count(crtc);
  684. switch (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) {
  685. case DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE:
  686. if ((int)(target_vblank - current_vblank) > 1) {
  687. DRM_DEBUG("Invalid absolute flip target %u, "
  688. "must be <= %u\n", target_vblank,
  689. current_vblank + 1);
  690. drm_crtc_vblank_put(crtc);
  691. return -EINVAL;
  692. }
  693. break;
  694. case DRM_MODE_PAGE_FLIP_TARGET_RELATIVE:
  695. if (target_vblank != 0 && target_vblank != 1) {
  696. DRM_DEBUG("Invalid relative flip target %u, "
  697. "must be 0 or 1\n", target_vblank);
  698. drm_crtc_vblank_put(crtc);
  699. return -EINVAL;
  700. }
  701. target_vblank += current_vblank;
  702. break;
  703. default:
  704. target_vblank = current_vblank +
  705. !(page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC);
  706. break;
  707. }
  708. } else if (crtc->funcs->page_flip == NULL ||
  709. (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET)) {
  710. return -EINVAL;
  711. }
  712. drm_modeset_lock_crtc(crtc, crtc->primary);
  713. if (crtc->primary->fb == NULL) {
  714. /* The framebuffer is currently unbound, presumably
  715. * due to a hotplug event, that userspace has not
  716. * yet discovered.
  717. */
  718. ret = -EBUSY;
  719. goto out;
  720. }
  721. fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
  722. if (!fb) {
  723. ret = -ENOENT;
  724. goto out;
  725. }
  726. if (crtc->state) {
  727. const struct drm_plane_state *state = crtc->primary->state;
  728. ret = drm_framebuffer_check_src_coords(state->src_x,
  729. state->src_y,
  730. state->src_w,
  731. state->src_h,
  732. fb);
  733. } else {
  734. ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
  735. }
  736. if (ret)
  737. goto out;
  738. if (crtc->primary->fb->pixel_format != fb->pixel_format) {
  739. DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
  740. ret = -EINVAL;
  741. goto out;
  742. }
  743. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  744. e = kzalloc(sizeof *e, GFP_KERNEL);
  745. if (!e) {
  746. ret = -ENOMEM;
  747. goto out;
  748. }
  749. e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
  750. e->event.base.length = sizeof(e->event);
  751. e->event.user_data = page_flip->user_data;
  752. ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
  753. if (ret) {
  754. kfree(e);
  755. goto out;
  756. }
  757. }
  758. crtc->primary->old_fb = crtc->primary->fb;
  759. if (crtc->funcs->page_flip_target)
  760. ret = crtc->funcs->page_flip_target(crtc, fb, e,
  761. page_flip->flags,
  762. target_vblank);
  763. else
  764. ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
  765. if (ret) {
  766. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
  767. drm_event_cancel_free(dev, &e->base);
  768. /* Keep the old fb, don't unref it. */
  769. crtc->primary->old_fb = NULL;
  770. } else {
  771. crtc->primary->fb = fb;
  772. /* Unref only the old framebuffer. */
  773. fb = NULL;
  774. }
  775. out:
  776. if (ret && crtc->funcs->page_flip_target)
  777. drm_crtc_vblank_put(crtc);
  778. if (fb)
  779. drm_framebuffer_unreference(fb);
  780. if (crtc->primary->old_fb)
  781. drm_framebuffer_unreference(crtc->primary->old_fb);
  782. crtc->primary->old_fb = NULL;
  783. drm_modeset_unlock_crtc(crtc);
  784. return ret;
  785. }