drm_plane.c 30 KB

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