drm_plane.c 33 KB

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