drm_plane.c 30 KB

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