drm_plane.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579
  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_managed.h>
  32. #include <drm/drm_vblank.h>
  33. #include "drm_crtc_internal.h"
  34. /**
  35. * DOC: overview
  36. *
  37. * A plane represents an image source that can be blended with or overlaid on
  38. * top of a CRTC during the scanout process. Planes take their input data from a
  39. * &drm_framebuffer object. The plane itself specifies the cropping and scaling
  40. * of that image, and where it is placed on the visible area of a display
  41. * pipeline, represented by &drm_crtc. A plane can also have additional
  42. * properties that specify how the pixels are positioned and blended, like
  43. * rotation or Z-position. All these properties are stored in &drm_plane_state.
  44. *
  45. * To create a plane, a KMS drivers allocates and zeroes an instances of
  46. * &struct drm_plane (possibly as part of a larger structure) and registers it
  47. * with a call to drm_universal_plane_init().
  48. *
  49. * Each plane has a type, see enum drm_plane_type. A plane can be compatible
  50. * with multiple CRTCs, see &drm_plane.possible_crtcs.
  51. *
  52. * Each CRTC must have a unique primary plane userspace can attach to enable
  53. * the CRTC. In other words, userspace must be able to attach a different
  54. * primary plane to each CRTC at the same time. Primary planes can still be
  55. * compatible with multiple CRTCs. There must be exactly as many primary planes
  56. * as there are CRTCs.
  57. *
  58. * Legacy uAPI doesn't expose the primary and cursor planes directly. DRM core
  59. * relies on the driver to set the primary and optionally the cursor plane used
  60. * for legacy IOCTLs. This is done by calling drm_crtc_init_with_planes(). All
  61. * drivers must provide one primary plane per CRTC to avoid surprising legacy
  62. * userspace too much.
  63. */
  64. /**
  65. * DOC: standard plane properties
  66. *
  67. * DRM planes have a few standardized properties:
  68. *
  69. * type:
  70. * Immutable property describing the type of the plane.
  71. *
  72. * For user-space which has enabled the &DRM_CLIENT_CAP_ATOMIC capability,
  73. * the plane type is just a hint and is mostly superseded by atomic
  74. * test-only commits. The type hint can still be used to come up more
  75. * easily with a plane configuration accepted by the driver.
  76. *
  77. * The value of this property can be one of the following:
  78. *
  79. * "Primary":
  80. * To light up a CRTC, attaching a primary plane is the most likely to
  81. * work if it covers the whole CRTC and doesn't have scaling or
  82. * cropping set up.
  83. *
  84. * Drivers may support more features for the primary plane, user-space
  85. * can find out with test-only atomic commits.
  86. *
  87. * Some primary planes are implicitly used by the kernel in the legacy
  88. * IOCTLs &DRM_IOCTL_MODE_SETCRTC and &DRM_IOCTL_MODE_PAGE_FLIP.
  89. * Therefore user-space must not mix explicit usage of any primary
  90. * plane (e.g. through an atomic commit) with these legacy IOCTLs.
  91. *
  92. * "Cursor":
  93. * To enable this plane, using a framebuffer configured without scaling
  94. * or cropping and with the following properties is the most likely to
  95. * work:
  96. *
  97. * - If the driver provides the capabilities &DRM_CAP_CURSOR_WIDTH and
  98. * &DRM_CAP_CURSOR_HEIGHT, create the framebuffer with this size.
  99. * Otherwise, create a framebuffer with the size 64x64.
  100. * - If the driver doesn't support modifiers, create a framebuffer with
  101. * a linear layout. Otherwise, use the IN_FORMATS plane property.
  102. *
  103. * Drivers may support more features for the cursor plane, user-space
  104. * can find out with test-only atomic commits.
  105. *
  106. * Some cursor planes are implicitly used by the kernel in the legacy
  107. * IOCTLs &DRM_IOCTL_MODE_CURSOR and &DRM_IOCTL_MODE_CURSOR2.
  108. * Therefore user-space must not mix explicit usage of any cursor
  109. * plane (e.g. through an atomic commit) with these legacy IOCTLs.
  110. *
  111. * Some drivers may support cursors even if no cursor plane is exposed.
  112. * In this case, the legacy cursor IOCTLs can be used to configure the
  113. * cursor.
  114. *
  115. * "Overlay":
  116. * Neither primary nor cursor.
  117. *
  118. * Overlay planes are the only planes exposed when the
  119. * &DRM_CLIENT_CAP_UNIVERSAL_PLANES capability is disabled.
  120. *
  121. * IN_FORMATS:
  122. * Blob property which contains the set of buffer format and modifier
  123. * pairs supported by this plane. The blob is a struct
  124. * drm_format_modifier_blob. Without this property the plane doesn't
  125. * support buffers with modifiers. Userspace cannot change this property.
  126. *
  127. * Note that userspace can check the &DRM_CAP_ADDFB2_MODIFIERS driver
  128. * capability for general modifier support. If this flag is set then every
  129. * plane will have the IN_FORMATS property, even when it only supports
  130. * DRM_FORMAT_MOD_LINEAR. Before linux kernel release v5.1 there have been
  131. * various bugs in this area with inconsistencies between the capability
  132. * flag and per-plane properties.
  133. */
  134. static unsigned int drm_num_planes(struct drm_device *dev)
  135. {
  136. unsigned int num = 0;
  137. struct drm_plane *tmp;
  138. drm_for_each_plane(tmp, dev) {
  139. num++;
  140. }
  141. return num;
  142. }
  143. static inline u32 *
  144. formats_ptr(struct drm_format_modifier_blob *blob)
  145. {
  146. return (u32 *)(((char *)blob) + blob->formats_offset);
  147. }
  148. static inline struct drm_format_modifier *
  149. modifiers_ptr(struct drm_format_modifier_blob *blob)
  150. {
  151. return (struct drm_format_modifier *)(((char *)blob) + blob->modifiers_offset);
  152. }
  153. static int create_in_format_blob(struct drm_device *dev, struct drm_plane *plane)
  154. {
  155. const struct drm_mode_config *config = &dev->mode_config;
  156. struct drm_property_blob *blob;
  157. struct drm_format_modifier *mod;
  158. size_t blob_size, formats_size, modifiers_size;
  159. struct drm_format_modifier_blob *blob_data;
  160. unsigned int i, j;
  161. formats_size = sizeof(__u32) * plane->format_count;
  162. if (WARN_ON(!formats_size)) {
  163. /* 0 formats are never expected */
  164. return 0;
  165. }
  166. modifiers_size =
  167. sizeof(struct drm_format_modifier) * plane->modifier_count;
  168. blob_size = sizeof(struct drm_format_modifier_blob);
  169. /* Modifiers offset is a pointer to a struct with a 64 bit field so it
  170. * should be naturally aligned to 8B.
  171. */
  172. BUILD_BUG_ON(sizeof(struct drm_format_modifier_blob) % 8);
  173. blob_size += ALIGN(formats_size, 8);
  174. blob_size += modifiers_size;
  175. blob = drm_property_create_blob(dev, blob_size, NULL);
  176. if (IS_ERR(blob))
  177. return -1;
  178. blob_data = blob->data;
  179. blob_data->version = FORMAT_BLOB_CURRENT;
  180. blob_data->count_formats = plane->format_count;
  181. blob_data->formats_offset = sizeof(struct drm_format_modifier_blob);
  182. blob_data->count_modifiers = plane->modifier_count;
  183. blob_data->modifiers_offset =
  184. ALIGN(blob_data->formats_offset + formats_size, 8);
  185. memcpy(formats_ptr(blob_data), plane->format_types, formats_size);
  186. mod = modifiers_ptr(blob_data);
  187. for (i = 0; i < plane->modifier_count; i++) {
  188. for (j = 0; j < plane->format_count; j++) {
  189. if (!plane->funcs->format_mod_supported ||
  190. plane->funcs->format_mod_supported(plane,
  191. plane->format_types[j],
  192. plane->modifiers[i])) {
  193. mod->formats |= 1ULL << j;
  194. }
  195. }
  196. mod->modifier = plane->modifiers[i];
  197. mod->offset = 0;
  198. mod->pad = 0;
  199. mod++;
  200. }
  201. drm_object_attach_property(&plane->base, config->modifiers_property,
  202. blob->base.id);
  203. return 0;
  204. }
  205. __printf(9, 0)
  206. static int __drm_universal_plane_init(struct drm_device *dev,
  207. struct drm_plane *plane,
  208. uint32_t possible_crtcs,
  209. const struct drm_plane_funcs *funcs,
  210. const uint32_t *formats,
  211. unsigned int format_count,
  212. const uint64_t *format_modifiers,
  213. enum drm_plane_type type,
  214. const char *name, va_list ap)
  215. {
  216. struct drm_mode_config *config = &dev->mode_config;
  217. static const uint64_t default_modifiers[] = {
  218. DRM_FORMAT_MOD_LINEAR,
  219. };
  220. unsigned int format_modifier_count = 0;
  221. int ret;
  222. /* plane index is used with 32bit bitmasks */
  223. if (WARN_ON(config->num_total_plane >= 32))
  224. return -EINVAL;
  225. /*
  226. * First driver to need more than 64 formats needs to fix this. Each
  227. * format is encoded as a bit and the current code only supports a u64.
  228. */
  229. if (WARN_ON(format_count > 64))
  230. return -EINVAL;
  231. WARN_ON(drm_drv_uses_atomic_modeset(dev) &&
  232. (!funcs->atomic_destroy_state ||
  233. !funcs->atomic_duplicate_state));
  234. ret = drm_mode_object_add(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
  235. if (ret)
  236. return ret;
  237. drm_modeset_lock_init(&plane->mutex);
  238. plane->base.properties = &plane->properties;
  239. plane->dev = dev;
  240. plane->funcs = funcs;
  241. plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
  242. GFP_KERNEL);
  243. if (!plane->format_types) {
  244. DRM_DEBUG_KMS("out of memory when allocating plane\n");
  245. drm_mode_object_unregister(dev, &plane->base);
  246. return -ENOMEM;
  247. }
  248. if (format_modifiers) {
  249. const uint64_t *temp_modifiers = format_modifiers;
  250. while (*temp_modifiers++ != DRM_FORMAT_MOD_INVALID)
  251. format_modifier_count++;
  252. } else {
  253. if (!dev->mode_config.fb_modifiers_not_supported) {
  254. format_modifiers = default_modifiers;
  255. format_modifier_count = ARRAY_SIZE(default_modifiers);
  256. }
  257. }
  258. /* autoset the cap and check for consistency across all planes */
  259. drm_WARN_ON(dev, config->fb_modifiers_not_supported &&
  260. format_modifier_count);
  261. plane->modifier_count = format_modifier_count;
  262. plane->modifiers = kmalloc_array(format_modifier_count,
  263. sizeof(format_modifiers[0]),
  264. GFP_KERNEL);
  265. if (format_modifier_count && !plane->modifiers) {
  266. DRM_DEBUG_KMS("out of memory when allocating plane\n");
  267. kfree(plane->format_types);
  268. drm_mode_object_unregister(dev, &plane->base);
  269. return -ENOMEM;
  270. }
  271. if (name) {
  272. plane->name = kvasprintf(GFP_KERNEL, name, ap);
  273. } else {
  274. plane->name = kasprintf(GFP_KERNEL, "plane-%d",
  275. drm_num_planes(dev));
  276. }
  277. if (!plane->name) {
  278. kfree(plane->format_types);
  279. kfree(plane->modifiers);
  280. drm_mode_object_unregister(dev, &plane->base);
  281. return -ENOMEM;
  282. }
  283. memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
  284. plane->format_count = format_count;
  285. memcpy(plane->modifiers, format_modifiers,
  286. format_modifier_count * sizeof(format_modifiers[0]));
  287. plane->possible_crtcs = possible_crtcs;
  288. plane->type = type;
  289. list_add_tail(&plane->head, &config->plane_list);
  290. plane->index = config->num_total_plane++;
  291. drm_object_attach_property(&plane->base,
  292. config->plane_type_property,
  293. plane->type);
  294. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  295. drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
  296. drm_object_attach_property(&plane->base, config->prop_in_fence_fd, -1);
  297. drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
  298. drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
  299. drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
  300. drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
  301. drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
  302. drm_object_attach_property(&plane->base, config->prop_src_x, 0);
  303. drm_object_attach_property(&plane->base, config->prop_src_y, 0);
  304. drm_object_attach_property(&plane->base, config->prop_src_w, 0);
  305. drm_object_attach_property(&plane->base, config->prop_src_h, 0);
  306. }
  307. if (format_modifier_count)
  308. create_in_format_blob(dev, plane);
  309. return 0;
  310. }
  311. /**
  312. * drm_universal_plane_init - Initialize a new universal plane object
  313. * @dev: DRM device
  314. * @plane: plane object to init
  315. * @possible_crtcs: bitmask of possible CRTCs
  316. * @funcs: callbacks for the new plane
  317. * @formats: array of supported formats (DRM_FORMAT\_\*)
  318. * @format_count: number of elements in @formats
  319. * @format_modifiers: array of struct drm_format modifiers terminated by
  320. * DRM_FORMAT_MOD_INVALID
  321. * @type: type of plane (overlay, primary, cursor)
  322. * @name: printf style format string for the plane name, or NULL for default name
  323. *
  324. * Initializes a plane object of type @type. The &drm_plane_funcs.destroy hook
  325. * should call drm_plane_cleanup() and kfree() the plane structure. The plane
  326. * structure should not be allocated with devm_kzalloc().
  327. *
  328. * Note: consider using drmm_universal_plane_alloc() instead of
  329. * drm_universal_plane_init() to let the DRM managed resource infrastructure
  330. * take care of cleanup and deallocation.
  331. *
  332. * Drivers that only support the DRM_FORMAT_MOD_LINEAR modifier support may set
  333. * @format_modifiers to NULL. The plane will advertise the linear modifier.
  334. *
  335. * Returns:
  336. * Zero on success, error code on failure.
  337. */
  338. int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
  339. uint32_t possible_crtcs,
  340. const struct drm_plane_funcs *funcs,
  341. const uint32_t *formats, unsigned int format_count,
  342. const uint64_t *format_modifiers,
  343. enum drm_plane_type type,
  344. const char *name, ...)
  345. {
  346. va_list ap;
  347. int ret;
  348. WARN_ON(!funcs->destroy);
  349. va_start(ap, name);
  350. ret = __drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
  351. formats, format_count, format_modifiers,
  352. type, name, ap);
  353. va_end(ap);
  354. return ret;
  355. }
  356. EXPORT_SYMBOL(drm_universal_plane_init);
  357. static void drmm_universal_plane_alloc_release(struct drm_device *dev, void *ptr)
  358. {
  359. struct drm_plane *plane = ptr;
  360. if (WARN_ON(!plane->dev))
  361. return;
  362. drm_plane_cleanup(plane);
  363. }
  364. void *__drmm_universal_plane_alloc(struct drm_device *dev, size_t size,
  365. size_t offset, uint32_t possible_crtcs,
  366. const struct drm_plane_funcs *funcs,
  367. const uint32_t *formats, unsigned int format_count,
  368. const uint64_t *format_modifiers,
  369. enum drm_plane_type type,
  370. const char *name, ...)
  371. {
  372. void *container;
  373. struct drm_plane *plane;
  374. va_list ap;
  375. int ret;
  376. if (WARN_ON(!funcs || funcs->destroy))
  377. return ERR_PTR(-EINVAL);
  378. container = drmm_kzalloc(dev, size, GFP_KERNEL);
  379. if (!container)
  380. return ERR_PTR(-ENOMEM);
  381. plane = container + offset;
  382. va_start(ap, name);
  383. ret = __drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
  384. formats, format_count, format_modifiers,
  385. type, name, ap);
  386. va_end(ap);
  387. if (ret)
  388. return ERR_PTR(ret);
  389. ret = drmm_add_action_or_reset(dev, drmm_universal_plane_alloc_release,
  390. plane);
  391. if (ret)
  392. return ERR_PTR(ret);
  393. return container;
  394. }
  395. EXPORT_SYMBOL(__drmm_universal_plane_alloc);
  396. void *__drm_universal_plane_alloc(struct drm_device *dev, size_t size,
  397. size_t offset, uint32_t possible_crtcs,
  398. const struct drm_plane_funcs *funcs,
  399. const uint32_t *formats, unsigned int format_count,
  400. const uint64_t *format_modifiers,
  401. enum drm_plane_type type,
  402. const char *name, ...)
  403. {
  404. void *container;
  405. struct drm_plane *plane;
  406. va_list ap;
  407. int ret;
  408. if (drm_WARN_ON(dev, !funcs))
  409. return ERR_PTR(-EINVAL);
  410. container = kzalloc(size, GFP_KERNEL);
  411. if (!container)
  412. return ERR_PTR(-ENOMEM);
  413. plane = container + offset;
  414. va_start(ap, name);
  415. ret = __drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
  416. formats, format_count, format_modifiers,
  417. type, name, ap);
  418. va_end(ap);
  419. if (ret)
  420. goto err_kfree;
  421. return container;
  422. err_kfree:
  423. kfree(container);
  424. return ERR_PTR(ret);
  425. }
  426. EXPORT_SYMBOL(__drm_universal_plane_alloc);
  427. int drm_plane_register_all(struct drm_device *dev)
  428. {
  429. unsigned int num_planes = 0;
  430. unsigned int num_zpos = 0;
  431. struct drm_plane *plane;
  432. int ret = 0;
  433. drm_for_each_plane(plane, dev) {
  434. if (plane->funcs->late_register)
  435. ret = plane->funcs->late_register(plane);
  436. if (ret)
  437. return ret;
  438. if (plane->zpos_property)
  439. num_zpos++;
  440. num_planes++;
  441. }
  442. drm_WARN(dev, num_zpos && num_planes != num_zpos,
  443. "Mixing planes with and without zpos property is invalid\n");
  444. return 0;
  445. }
  446. void drm_plane_unregister_all(struct drm_device *dev)
  447. {
  448. struct drm_plane *plane;
  449. drm_for_each_plane(plane, dev) {
  450. if (plane->funcs->early_unregister)
  451. plane->funcs->early_unregister(plane);
  452. }
  453. }
  454. /**
  455. * drm_plane_cleanup - Clean up the core plane usage
  456. * @plane: plane to cleanup
  457. *
  458. * This function cleans up @plane and removes it from the DRM mode setting
  459. * core. Note that the function does *not* free the plane structure itself,
  460. * this is the responsibility of the caller.
  461. */
  462. void drm_plane_cleanup(struct drm_plane *plane)
  463. {
  464. struct drm_device *dev = plane->dev;
  465. drm_modeset_lock_fini(&plane->mutex);
  466. kfree(plane->format_types);
  467. kfree(plane->modifiers);
  468. drm_mode_object_unregister(dev, &plane->base);
  469. BUG_ON(list_empty(&plane->head));
  470. /* Note that the plane_list is considered to be static; should we
  471. * remove the drm_plane at runtime we would have to decrement all
  472. * the indices on the drm_plane after us in the plane_list.
  473. */
  474. list_del(&plane->head);
  475. dev->mode_config.num_total_plane--;
  476. WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
  477. if (plane->state && plane->funcs->atomic_destroy_state)
  478. plane->funcs->atomic_destroy_state(plane, plane->state);
  479. kfree(plane->name);
  480. memset(plane, 0, sizeof(*plane));
  481. }
  482. EXPORT_SYMBOL(drm_plane_cleanup);
  483. /**
  484. * drm_plane_from_index - find the registered plane at an index
  485. * @dev: DRM device
  486. * @idx: index of registered plane to find for
  487. *
  488. * Given a plane index, return the registered plane from DRM device's
  489. * list of planes with matching index. This is the inverse of drm_plane_index().
  490. */
  491. struct drm_plane *
  492. drm_plane_from_index(struct drm_device *dev, int idx)
  493. {
  494. struct drm_plane *plane;
  495. drm_for_each_plane(plane, dev)
  496. if (idx == plane->index)
  497. return plane;
  498. return NULL;
  499. }
  500. EXPORT_SYMBOL(drm_plane_from_index);
  501. /**
  502. * drm_plane_force_disable - Forcibly disable a plane
  503. * @plane: plane to disable
  504. *
  505. * Forces the plane to be disabled.
  506. *
  507. * Used when the plane's current framebuffer is destroyed,
  508. * and when restoring fbdev mode.
  509. *
  510. * Note that this function is not suitable for atomic drivers, since it doesn't
  511. * wire through the lock acquisition context properly and hence can't handle
  512. * retries or driver private locks. You probably want to use
  513. * drm_atomic_helper_disable_plane() or
  514. * drm_atomic_helper_disable_planes_on_crtc() instead.
  515. */
  516. void drm_plane_force_disable(struct drm_plane *plane)
  517. {
  518. int ret;
  519. if (!plane->fb)
  520. return;
  521. WARN_ON(drm_drv_uses_atomic_modeset(plane->dev));
  522. plane->old_fb = plane->fb;
  523. ret = plane->funcs->disable_plane(plane, NULL);
  524. if (ret) {
  525. DRM_ERROR("failed to disable plane with busy fb\n");
  526. plane->old_fb = NULL;
  527. return;
  528. }
  529. /* disconnect the plane from the fb and crtc: */
  530. drm_framebuffer_put(plane->old_fb);
  531. plane->old_fb = NULL;
  532. plane->fb = NULL;
  533. plane->crtc = NULL;
  534. }
  535. EXPORT_SYMBOL(drm_plane_force_disable);
  536. /**
  537. * drm_mode_plane_set_obj_prop - set the value of a property
  538. * @plane: drm plane object to set property value for
  539. * @property: property to set
  540. * @value: value the property should be set to
  541. *
  542. * This functions sets a given property on a given plane object. This function
  543. * calls the driver's ->set_property callback and changes the software state of
  544. * the property if the callback succeeds.
  545. *
  546. * Returns:
  547. * Zero on success, error code on failure.
  548. */
  549. int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
  550. struct drm_property *property,
  551. uint64_t value)
  552. {
  553. int ret = -EINVAL;
  554. struct drm_mode_object *obj = &plane->base;
  555. if (plane->funcs->set_property)
  556. ret = plane->funcs->set_property(plane, property, value);
  557. if (!ret)
  558. drm_object_property_set_value(obj, property, value);
  559. return ret;
  560. }
  561. EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
  562. int drm_mode_getplane_res(struct drm_device *dev, void *data,
  563. struct drm_file *file_priv)
  564. {
  565. struct drm_mode_get_plane_res *plane_resp = data;
  566. struct drm_plane *plane;
  567. uint32_t __user *plane_ptr;
  568. int count = 0;
  569. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  570. return -EOPNOTSUPP;
  571. plane_ptr = u64_to_user_ptr(plane_resp->plane_id_ptr);
  572. /*
  573. * This ioctl is called twice, once to determine how much space is
  574. * needed, and the 2nd time to fill it.
  575. */
  576. drm_for_each_plane(plane, dev) {
  577. /*
  578. * Unless userspace set the 'universal planes'
  579. * capability bit, only advertise overlays.
  580. */
  581. if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
  582. !file_priv->universal_planes)
  583. continue;
  584. if (drm_lease_held(file_priv, plane->base.id)) {
  585. if (count < plane_resp->count_planes &&
  586. put_user(plane->base.id, plane_ptr + count))
  587. return -EFAULT;
  588. count++;
  589. }
  590. }
  591. plane_resp->count_planes = count;
  592. return 0;
  593. }
  594. int drm_mode_getplane(struct drm_device *dev, void *data,
  595. struct drm_file *file_priv)
  596. {
  597. struct drm_mode_get_plane *plane_resp = data;
  598. struct drm_plane *plane;
  599. uint32_t __user *format_ptr;
  600. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  601. return -EOPNOTSUPP;
  602. plane = drm_plane_find(dev, file_priv, plane_resp->plane_id);
  603. if (!plane)
  604. return -ENOENT;
  605. drm_modeset_lock(&plane->mutex, NULL);
  606. if (plane->state && plane->state->crtc && drm_lease_held(file_priv, plane->state->crtc->base.id))
  607. plane_resp->crtc_id = plane->state->crtc->base.id;
  608. else if (!plane->state && plane->crtc && drm_lease_held(file_priv, plane->crtc->base.id))
  609. plane_resp->crtc_id = plane->crtc->base.id;
  610. else
  611. plane_resp->crtc_id = 0;
  612. if (plane->state && plane->state->fb)
  613. plane_resp->fb_id = plane->state->fb->base.id;
  614. else if (!plane->state && plane->fb)
  615. plane_resp->fb_id = plane->fb->base.id;
  616. else
  617. plane_resp->fb_id = 0;
  618. drm_modeset_unlock(&plane->mutex);
  619. plane_resp->plane_id = plane->base.id;
  620. plane_resp->possible_crtcs = drm_lease_filter_crtcs(file_priv,
  621. plane->possible_crtcs);
  622. plane_resp->gamma_size = 0;
  623. /*
  624. * This ioctl is called twice, once to determine how much space is
  625. * needed, and the 2nd time to fill it.
  626. */
  627. if (plane->format_count &&
  628. (plane_resp->count_format_types >= plane->format_count)) {
  629. format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
  630. if (copy_to_user(format_ptr,
  631. plane->format_types,
  632. sizeof(uint32_t) * plane->format_count)) {
  633. return -EFAULT;
  634. }
  635. }
  636. plane_resp->count_format_types = plane->format_count;
  637. return 0;
  638. }
  639. int drm_plane_check_pixel_format(struct drm_plane *plane,
  640. u32 format, u64 modifier)
  641. {
  642. unsigned int i;
  643. for (i = 0; i < plane->format_count; i++) {
  644. if (format == plane->format_types[i])
  645. break;
  646. }
  647. if (i == plane->format_count)
  648. return -EINVAL;
  649. if (plane->funcs->format_mod_supported) {
  650. if (!plane->funcs->format_mod_supported(plane, format, modifier))
  651. return -EINVAL;
  652. } else {
  653. if (!plane->modifier_count)
  654. return 0;
  655. for (i = 0; i < plane->modifier_count; i++) {
  656. if (modifier == plane->modifiers[i])
  657. break;
  658. }
  659. if (i == plane->modifier_count)
  660. return -EINVAL;
  661. }
  662. return 0;
  663. }
  664. static int __setplane_check(struct drm_plane *plane,
  665. struct drm_crtc *crtc,
  666. struct drm_framebuffer *fb,
  667. int32_t crtc_x, int32_t crtc_y,
  668. uint32_t crtc_w, uint32_t crtc_h,
  669. uint32_t src_x, uint32_t src_y,
  670. uint32_t src_w, uint32_t src_h)
  671. {
  672. int ret;
  673. /* Check whether this plane is usable on this CRTC */
  674. if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
  675. DRM_DEBUG_KMS("Invalid crtc for plane\n");
  676. return -EINVAL;
  677. }
  678. /* Check whether this plane supports the fb pixel format. */
  679. ret = drm_plane_check_pixel_format(plane, fb->format->format,
  680. fb->modifier);
  681. if (ret) {
  682. DRM_DEBUG_KMS("Invalid pixel format %p4cc, modifier 0x%llx\n",
  683. &fb->format->format, fb->modifier);
  684. return ret;
  685. }
  686. /* Give drivers some help against integer overflows */
  687. if (crtc_w > INT_MAX ||
  688. crtc_x > INT_MAX - (int32_t) crtc_w ||
  689. crtc_h > INT_MAX ||
  690. crtc_y > INT_MAX - (int32_t) crtc_h) {
  691. DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
  692. crtc_w, crtc_h, crtc_x, crtc_y);
  693. return -ERANGE;
  694. }
  695. ret = drm_framebuffer_check_src_coords(src_x, src_y, src_w, src_h, fb);
  696. if (ret)
  697. return ret;
  698. return 0;
  699. }
  700. /**
  701. * drm_any_plane_has_format - Check whether any plane supports this format and modifier combination
  702. * @dev: DRM device
  703. * @format: pixel format (DRM_FORMAT_*)
  704. * @modifier: data layout modifier
  705. *
  706. * Returns:
  707. * Whether at least one plane supports the specified format and modifier combination.
  708. */
  709. bool drm_any_plane_has_format(struct drm_device *dev,
  710. u32 format, u64 modifier)
  711. {
  712. struct drm_plane *plane;
  713. drm_for_each_plane(plane, dev) {
  714. if (drm_plane_check_pixel_format(plane, format, modifier) == 0)
  715. return true;
  716. }
  717. return false;
  718. }
  719. EXPORT_SYMBOL(drm_any_plane_has_format);
  720. /*
  721. * __setplane_internal - setplane handler for internal callers
  722. *
  723. * This function will take a reference on the new fb for the plane
  724. * on success.
  725. *
  726. * src_{x,y,w,h} are provided in 16.16 fixed point format
  727. */
  728. static int __setplane_internal(struct drm_plane *plane,
  729. struct drm_crtc *crtc,
  730. struct drm_framebuffer *fb,
  731. int32_t crtc_x, int32_t crtc_y,
  732. uint32_t crtc_w, uint32_t crtc_h,
  733. /* src_{x,y,w,h} values are 16.16 fixed point */
  734. uint32_t src_x, uint32_t src_y,
  735. uint32_t src_w, uint32_t src_h,
  736. struct drm_modeset_acquire_ctx *ctx)
  737. {
  738. int ret = 0;
  739. WARN_ON(drm_drv_uses_atomic_modeset(plane->dev));
  740. /* No fb means shut it down */
  741. if (!fb) {
  742. plane->old_fb = plane->fb;
  743. ret = plane->funcs->disable_plane(plane, ctx);
  744. if (!ret) {
  745. plane->crtc = NULL;
  746. plane->fb = NULL;
  747. } else {
  748. plane->old_fb = NULL;
  749. }
  750. goto out;
  751. }
  752. ret = __setplane_check(plane, crtc, fb,
  753. crtc_x, crtc_y, crtc_w, crtc_h,
  754. src_x, src_y, src_w, src_h);
  755. if (ret)
  756. goto out;
  757. plane->old_fb = plane->fb;
  758. ret = plane->funcs->update_plane(plane, crtc, fb,
  759. crtc_x, crtc_y, crtc_w, crtc_h,
  760. src_x, src_y, src_w, src_h, ctx);
  761. if (!ret) {
  762. plane->crtc = crtc;
  763. plane->fb = fb;
  764. drm_framebuffer_get(plane->fb);
  765. } else {
  766. plane->old_fb = NULL;
  767. }
  768. out:
  769. if (plane->old_fb)
  770. drm_framebuffer_put(plane->old_fb);
  771. plane->old_fb = NULL;
  772. return ret;
  773. }
  774. static int __setplane_atomic(struct drm_plane *plane,
  775. struct drm_crtc *crtc,
  776. struct drm_framebuffer *fb,
  777. int32_t crtc_x, int32_t crtc_y,
  778. uint32_t crtc_w, uint32_t crtc_h,
  779. uint32_t src_x, uint32_t src_y,
  780. uint32_t src_w, uint32_t src_h,
  781. struct drm_modeset_acquire_ctx *ctx)
  782. {
  783. int ret;
  784. WARN_ON(!drm_drv_uses_atomic_modeset(plane->dev));
  785. /* No fb means shut it down */
  786. if (!fb)
  787. return plane->funcs->disable_plane(plane, ctx);
  788. /*
  789. * FIXME: This is redundant with drm_atomic_plane_check(),
  790. * but the legacy cursor/"async" .update_plane() tricks
  791. * don't call that so we still need this here. Should remove
  792. * this when all .update_plane() implementations have been
  793. * fixed to call drm_atomic_plane_check().
  794. */
  795. ret = __setplane_check(plane, crtc, fb,
  796. crtc_x, crtc_y, crtc_w, crtc_h,
  797. src_x, src_y, src_w, src_h);
  798. if (ret)
  799. return ret;
  800. return plane->funcs->update_plane(plane, crtc, fb,
  801. crtc_x, crtc_y, crtc_w, crtc_h,
  802. src_x, src_y, src_w, src_h, ctx);
  803. }
  804. static int setplane_internal(struct drm_plane *plane,
  805. struct drm_crtc *crtc,
  806. struct drm_framebuffer *fb,
  807. int32_t crtc_x, int32_t crtc_y,
  808. uint32_t crtc_w, uint32_t crtc_h,
  809. /* src_{x,y,w,h} values are 16.16 fixed point */
  810. uint32_t src_x, uint32_t src_y,
  811. uint32_t src_w, uint32_t src_h)
  812. {
  813. struct drm_modeset_acquire_ctx ctx;
  814. int ret;
  815. DRM_MODESET_LOCK_ALL_BEGIN(plane->dev, ctx,
  816. DRM_MODESET_ACQUIRE_INTERRUPTIBLE, ret);
  817. if (drm_drv_uses_atomic_modeset(plane->dev))
  818. ret = __setplane_atomic(plane, crtc, fb,
  819. crtc_x, crtc_y, crtc_w, crtc_h,
  820. src_x, src_y, src_w, src_h, &ctx);
  821. else
  822. ret = __setplane_internal(plane, crtc, fb,
  823. crtc_x, crtc_y, crtc_w, crtc_h,
  824. src_x, src_y, src_w, src_h, &ctx);
  825. DRM_MODESET_LOCK_ALL_END(plane->dev, ctx, ret);
  826. return ret;
  827. }
  828. int drm_mode_setplane(struct drm_device *dev, void *data,
  829. struct drm_file *file_priv)
  830. {
  831. struct drm_mode_set_plane *plane_req = data;
  832. struct drm_plane *plane;
  833. struct drm_crtc *crtc = NULL;
  834. struct drm_framebuffer *fb = NULL;
  835. int ret;
  836. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  837. return -EOPNOTSUPP;
  838. /*
  839. * First, find the plane, crtc, and fb objects. If not available,
  840. * we don't bother to call the driver.
  841. */
  842. plane = drm_plane_find(dev, file_priv, plane_req->plane_id);
  843. if (!plane) {
  844. DRM_DEBUG_KMS("Unknown plane ID %d\n",
  845. plane_req->plane_id);
  846. return -ENOENT;
  847. }
  848. if (plane_req->fb_id) {
  849. fb = drm_framebuffer_lookup(dev, file_priv, plane_req->fb_id);
  850. if (!fb) {
  851. DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
  852. plane_req->fb_id);
  853. return -ENOENT;
  854. }
  855. crtc = drm_crtc_find(dev, file_priv, plane_req->crtc_id);
  856. if (!crtc) {
  857. drm_framebuffer_put(fb);
  858. DRM_DEBUG_KMS("Unknown crtc ID %d\n",
  859. plane_req->crtc_id);
  860. return -ENOENT;
  861. }
  862. }
  863. ret = setplane_internal(plane, crtc, fb,
  864. plane_req->crtc_x, plane_req->crtc_y,
  865. plane_req->crtc_w, plane_req->crtc_h,
  866. plane_req->src_x, plane_req->src_y,
  867. plane_req->src_w, plane_req->src_h);
  868. if (fb)
  869. drm_framebuffer_put(fb);
  870. return ret;
  871. }
  872. static int drm_mode_cursor_universal(struct drm_crtc *crtc,
  873. struct drm_mode_cursor2 *req,
  874. struct drm_file *file_priv,
  875. struct drm_modeset_acquire_ctx *ctx)
  876. {
  877. struct drm_device *dev = crtc->dev;
  878. struct drm_plane *plane = crtc->cursor;
  879. struct drm_framebuffer *fb = NULL;
  880. struct drm_mode_fb_cmd2 fbreq = {
  881. .width = req->width,
  882. .height = req->height,
  883. .pixel_format = DRM_FORMAT_ARGB8888,
  884. .pitches = { req->width * 4 },
  885. .handles = { req->handle },
  886. };
  887. int32_t crtc_x, crtc_y;
  888. uint32_t crtc_w = 0, crtc_h = 0;
  889. uint32_t src_w = 0, src_h = 0;
  890. int ret = 0;
  891. BUG_ON(!plane);
  892. WARN_ON(plane->crtc != crtc && plane->crtc != NULL);
  893. /*
  894. * Obtain fb we'll be using (either new or existing) and take an extra
  895. * reference to it if fb != null. setplane will take care of dropping
  896. * the reference if the plane update fails.
  897. */
  898. if (req->flags & DRM_MODE_CURSOR_BO) {
  899. if (req->handle) {
  900. fb = drm_internal_framebuffer_create(dev, &fbreq, file_priv);
  901. if (IS_ERR(fb)) {
  902. DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
  903. return PTR_ERR(fb);
  904. }
  905. fb->hot_x = req->hot_x;
  906. fb->hot_y = req->hot_y;
  907. } else {
  908. fb = NULL;
  909. }
  910. } else {
  911. if (plane->state)
  912. fb = plane->state->fb;
  913. else
  914. fb = plane->fb;
  915. if (fb)
  916. drm_framebuffer_get(fb);
  917. }
  918. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  919. crtc_x = req->x;
  920. crtc_y = req->y;
  921. } else {
  922. crtc_x = crtc->cursor_x;
  923. crtc_y = crtc->cursor_y;
  924. }
  925. if (fb) {
  926. crtc_w = fb->width;
  927. crtc_h = fb->height;
  928. src_w = fb->width << 16;
  929. src_h = fb->height << 16;
  930. }
  931. if (drm_drv_uses_atomic_modeset(dev))
  932. ret = __setplane_atomic(plane, crtc, fb,
  933. crtc_x, crtc_y, crtc_w, crtc_h,
  934. 0, 0, src_w, src_h, ctx);
  935. else
  936. ret = __setplane_internal(plane, crtc, fb,
  937. crtc_x, crtc_y, crtc_w, crtc_h,
  938. 0, 0, src_w, src_h, ctx);
  939. if (fb)
  940. drm_framebuffer_put(fb);
  941. /* Update successful; save new cursor position, if necessary */
  942. if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
  943. crtc->cursor_x = req->x;
  944. crtc->cursor_y = req->y;
  945. }
  946. return ret;
  947. }
  948. static int drm_mode_cursor_common(struct drm_device *dev,
  949. struct drm_mode_cursor2 *req,
  950. struct drm_file *file_priv)
  951. {
  952. struct drm_crtc *crtc;
  953. struct drm_modeset_acquire_ctx ctx;
  954. int ret = 0;
  955. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  956. return -EOPNOTSUPP;
  957. if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
  958. return -EINVAL;
  959. crtc = drm_crtc_find(dev, file_priv, req->crtc_id);
  960. if (!crtc) {
  961. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
  962. return -ENOENT;
  963. }
  964. drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
  965. retry:
  966. ret = drm_modeset_lock(&crtc->mutex, &ctx);
  967. if (ret)
  968. goto out;
  969. /*
  970. * If this crtc has a universal cursor plane, call that plane's update
  971. * handler rather than using legacy cursor handlers.
  972. */
  973. if (crtc->cursor) {
  974. ret = drm_modeset_lock(&crtc->cursor->mutex, &ctx);
  975. if (ret)
  976. goto out;
  977. if (!drm_lease_held(file_priv, crtc->cursor->base.id)) {
  978. ret = -EACCES;
  979. goto out;
  980. }
  981. ret = drm_mode_cursor_universal(crtc, req, file_priv, &ctx);
  982. goto out;
  983. }
  984. if (req->flags & DRM_MODE_CURSOR_BO) {
  985. if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
  986. ret = -ENXIO;
  987. goto out;
  988. }
  989. /* Turns off the cursor if handle is 0 */
  990. if (crtc->funcs->cursor_set2)
  991. ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
  992. req->width, req->height, req->hot_x, req->hot_y);
  993. else
  994. ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
  995. req->width, req->height);
  996. }
  997. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  998. if (crtc->funcs->cursor_move) {
  999. ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
  1000. } else {
  1001. ret = -EFAULT;
  1002. goto out;
  1003. }
  1004. }
  1005. out:
  1006. if (ret == -EDEADLK) {
  1007. ret = drm_modeset_backoff(&ctx);
  1008. if (!ret)
  1009. goto retry;
  1010. }
  1011. drm_modeset_drop_locks(&ctx);
  1012. drm_modeset_acquire_fini(&ctx);
  1013. return ret;
  1014. }
  1015. int drm_mode_cursor_ioctl(struct drm_device *dev,
  1016. void *data, struct drm_file *file_priv)
  1017. {
  1018. struct drm_mode_cursor *req = data;
  1019. struct drm_mode_cursor2 new_req;
  1020. memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
  1021. new_req.hot_x = new_req.hot_y = 0;
  1022. return drm_mode_cursor_common(dev, &new_req, file_priv);
  1023. }
  1024. /*
  1025. * Set the cursor configuration based on user request. This implements the 2nd
  1026. * version of the cursor ioctl, which allows userspace to additionally specify
  1027. * the hotspot of the pointer.
  1028. */
  1029. int drm_mode_cursor2_ioctl(struct drm_device *dev,
  1030. void *data, struct drm_file *file_priv)
  1031. {
  1032. struct drm_mode_cursor2 *req = data;
  1033. return drm_mode_cursor_common(dev, req, file_priv);
  1034. }
  1035. int drm_mode_page_flip_ioctl(struct drm_device *dev,
  1036. void *data, struct drm_file *file_priv)
  1037. {
  1038. struct drm_mode_crtc_page_flip_target *page_flip = data;
  1039. struct drm_crtc *crtc;
  1040. struct drm_plane *plane;
  1041. struct drm_framebuffer *fb = NULL, *old_fb;
  1042. struct drm_pending_vblank_event *e = NULL;
  1043. u32 target_vblank = page_flip->sequence;
  1044. struct drm_modeset_acquire_ctx ctx;
  1045. int ret = -EINVAL;
  1046. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1047. return -EOPNOTSUPP;
  1048. if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS)
  1049. return -EINVAL;
  1050. if (page_flip->sequence != 0 && !(page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET))
  1051. return -EINVAL;
  1052. /* Only one of the DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE/RELATIVE flags
  1053. * can be specified
  1054. */
  1055. if ((page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) == DRM_MODE_PAGE_FLIP_TARGET)
  1056. return -EINVAL;
  1057. if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
  1058. return -EINVAL;
  1059. crtc = drm_crtc_find(dev, file_priv, page_flip->crtc_id);
  1060. if (!crtc)
  1061. return -ENOENT;
  1062. plane = crtc->primary;
  1063. if (!drm_lease_held(file_priv, plane->base.id))
  1064. return -EACCES;
  1065. if (crtc->funcs->page_flip_target) {
  1066. u32 current_vblank;
  1067. int r;
  1068. r = drm_crtc_vblank_get(crtc);
  1069. if (r)
  1070. return r;
  1071. current_vblank = (u32)drm_crtc_vblank_count(crtc);
  1072. switch (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) {
  1073. case DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE:
  1074. if ((int)(target_vblank - current_vblank) > 1) {
  1075. DRM_DEBUG("Invalid absolute flip target %u, "
  1076. "must be <= %u\n", target_vblank,
  1077. current_vblank + 1);
  1078. drm_crtc_vblank_put(crtc);
  1079. return -EINVAL;
  1080. }
  1081. break;
  1082. case DRM_MODE_PAGE_FLIP_TARGET_RELATIVE:
  1083. if (target_vblank != 0 && target_vblank != 1) {
  1084. DRM_DEBUG("Invalid relative flip target %u, "
  1085. "must be 0 or 1\n", target_vblank);
  1086. drm_crtc_vblank_put(crtc);
  1087. return -EINVAL;
  1088. }
  1089. target_vblank += current_vblank;
  1090. break;
  1091. default:
  1092. target_vblank = current_vblank +
  1093. !(page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC);
  1094. break;
  1095. }
  1096. } else if (crtc->funcs->page_flip == NULL ||
  1097. (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET)) {
  1098. return -EINVAL;
  1099. }
  1100. drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
  1101. retry:
  1102. ret = drm_modeset_lock(&crtc->mutex, &ctx);
  1103. if (ret)
  1104. goto out;
  1105. ret = drm_modeset_lock(&plane->mutex, &ctx);
  1106. if (ret)
  1107. goto out;
  1108. if (plane->state)
  1109. old_fb = plane->state->fb;
  1110. else
  1111. old_fb = plane->fb;
  1112. if (old_fb == NULL) {
  1113. /* The framebuffer is currently unbound, presumably
  1114. * due to a hotplug event, that userspace has not
  1115. * yet discovered.
  1116. */
  1117. ret = -EBUSY;
  1118. goto out;
  1119. }
  1120. fb = drm_framebuffer_lookup(dev, file_priv, page_flip->fb_id);
  1121. if (!fb) {
  1122. ret = -ENOENT;
  1123. goto out;
  1124. }
  1125. if (plane->state) {
  1126. const struct drm_plane_state *state = plane->state;
  1127. ret = drm_framebuffer_check_src_coords(state->src_x,
  1128. state->src_y,
  1129. state->src_w,
  1130. state->src_h,
  1131. fb);
  1132. } else {
  1133. ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y,
  1134. &crtc->mode, fb);
  1135. }
  1136. if (ret)
  1137. goto out;
  1138. /*
  1139. * Only check the FOURCC format code, excluding modifiers. This is
  1140. * enough for all legacy drivers. Atomic drivers have their own
  1141. * checks in their ->atomic_check implementation, which will
  1142. * return -EINVAL if any hw or driver constraint is violated due
  1143. * to modifier changes.
  1144. */
  1145. if (old_fb->format->format != fb->format->format) {
  1146. DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
  1147. ret = -EINVAL;
  1148. goto out;
  1149. }
  1150. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  1151. e = kzalloc(sizeof *e, GFP_KERNEL);
  1152. if (!e) {
  1153. ret = -ENOMEM;
  1154. goto out;
  1155. }
  1156. e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
  1157. e->event.base.length = sizeof(e->event);
  1158. e->event.vbl.user_data = page_flip->user_data;
  1159. e->event.vbl.crtc_id = crtc->base.id;
  1160. ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
  1161. if (ret) {
  1162. kfree(e);
  1163. e = NULL;
  1164. goto out;
  1165. }
  1166. }
  1167. plane->old_fb = plane->fb;
  1168. if (crtc->funcs->page_flip_target)
  1169. ret = crtc->funcs->page_flip_target(crtc, fb, e,
  1170. page_flip->flags,
  1171. target_vblank,
  1172. &ctx);
  1173. else
  1174. ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags,
  1175. &ctx);
  1176. if (ret) {
  1177. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
  1178. drm_event_cancel_free(dev, &e->base);
  1179. /* Keep the old fb, don't unref it. */
  1180. plane->old_fb = NULL;
  1181. } else {
  1182. if (!plane->state) {
  1183. plane->fb = fb;
  1184. drm_framebuffer_get(fb);
  1185. }
  1186. }
  1187. out:
  1188. if (fb)
  1189. drm_framebuffer_put(fb);
  1190. if (plane->old_fb)
  1191. drm_framebuffer_put(plane->old_fb);
  1192. plane->old_fb = NULL;
  1193. if (ret == -EDEADLK) {
  1194. ret = drm_modeset_backoff(&ctx);
  1195. if (!ret)
  1196. goto retry;
  1197. }
  1198. drm_modeset_drop_locks(&ctx);
  1199. drm_modeset_acquire_fini(&ctx);
  1200. if (ret && crtc->funcs->page_flip_target)
  1201. drm_crtc_vblank_put(crtc);
  1202. return ret;
  1203. }
  1204. /**
  1205. * DOC: damage tracking
  1206. *
  1207. * FB_DAMAGE_CLIPS is an optional plane property which provides a means to
  1208. * specify a list of damage rectangles on a plane in framebuffer coordinates of
  1209. * the framebuffer attached to the plane. In current context damage is the area
  1210. * of plane framebuffer that has changed since last plane update (also called
  1211. * page-flip), irrespective of whether currently attached framebuffer is same as
  1212. * framebuffer attached during last plane update or not.
  1213. *
  1214. * FB_DAMAGE_CLIPS is a hint to kernel which could be helpful for some drivers
  1215. * to optimize internally especially for virtual devices where each framebuffer
  1216. * change needs to be transmitted over network, usb, etc.
  1217. *
  1218. * Since FB_DAMAGE_CLIPS is a hint so it is an optional property. User-space can
  1219. * ignore damage clips property and in that case driver will do a full plane
  1220. * update. In case damage clips are provided then it is guaranteed that the area
  1221. * inside damage clips will be updated to plane. For efficiency driver can do
  1222. * full update or can update more than specified in damage clips. Since driver
  1223. * is free to read more, user-space must always render the entire visible
  1224. * framebuffer. Otherwise there can be corruptions. Also, if a user-space
  1225. * provides damage clips which doesn't encompass the actual damage to
  1226. * framebuffer (since last plane update) can result in incorrect rendering.
  1227. *
  1228. * FB_DAMAGE_CLIPS is a blob property with the layout of blob data is simply an
  1229. * array of &drm_mode_rect. Unlike plane &drm_plane_state.src coordinates,
  1230. * damage clips are not in 16.16 fixed point. Similar to plane src in
  1231. * framebuffer, damage clips cannot be negative. In damage clip, x1/y1 are
  1232. * inclusive and x2/y2 are exclusive. While kernel does not error for overlapped
  1233. * damage clips, it is strongly discouraged.
  1234. *
  1235. * Drivers that are interested in damage interface for plane should enable
  1236. * FB_DAMAGE_CLIPS property by calling drm_plane_enable_fb_damage_clips().
  1237. * Drivers implementing damage can use drm_atomic_helper_damage_iter_init() and
  1238. * drm_atomic_helper_damage_iter_next() helper iterator function to get damage
  1239. * rectangles clipped to &drm_plane_state.src.
  1240. */
  1241. /**
  1242. * drm_plane_enable_fb_damage_clips - Enables plane fb damage clips property.
  1243. * @plane: Plane on which to enable damage clips property.
  1244. *
  1245. * This function lets driver to enable the damage clips property on a plane.
  1246. */
  1247. void drm_plane_enable_fb_damage_clips(struct drm_plane *plane)
  1248. {
  1249. struct drm_device *dev = plane->dev;
  1250. struct drm_mode_config *config = &dev->mode_config;
  1251. drm_object_attach_property(&plane->base, config->prop_fb_damage_clips,
  1252. 0);
  1253. }
  1254. EXPORT_SYMBOL(drm_plane_enable_fb_damage_clips);
  1255. /**
  1256. * drm_plane_get_damage_clips_count - Returns damage clips count.
  1257. * @state: Plane state.
  1258. *
  1259. * Simple helper to get the number of &drm_mode_rect clips set by user-space
  1260. * during plane update.
  1261. *
  1262. * Return: Number of clips in plane fb_damage_clips blob property.
  1263. */
  1264. unsigned int
  1265. drm_plane_get_damage_clips_count(const struct drm_plane_state *state)
  1266. {
  1267. return (state && state->fb_damage_clips) ?
  1268. state->fb_damage_clips->length/sizeof(struct drm_mode_rect) : 0;
  1269. }
  1270. EXPORT_SYMBOL(drm_plane_get_damage_clips_count);
  1271. struct drm_mode_rect *
  1272. __drm_plane_get_damage_clips(const struct drm_plane_state *state)
  1273. {
  1274. return (struct drm_mode_rect *)((state && state->fb_damage_clips) ?
  1275. state->fb_damage_clips->data : NULL);
  1276. }
  1277. /**
  1278. * drm_plane_get_damage_clips - Returns damage clips.
  1279. * @state: Plane state.
  1280. *
  1281. * Note that this function returns uapi type &drm_mode_rect. Drivers might want
  1282. * to use the helper functions drm_atomic_helper_damage_iter_init() and
  1283. * drm_atomic_helper_damage_iter_next() or drm_atomic_helper_damage_merged() if
  1284. * the driver can only handle a single damage region at most.
  1285. *
  1286. * Return: Damage clips in plane fb_damage_clips blob property.
  1287. */
  1288. struct drm_mode_rect *
  1289. drm_plane_get_damage_clips(const struct drm_plane_state *state)
  1290. {
  1291. struct drm_device *dev = state->plane->dev;
  1292. struct drm_mode_config *config = &dev->mode_config;
  1293. /* check that drm_plane_enable_fb_damage_clips() was called */
  1294. if (!drm_mode_obj_find_prop_id(&state->plane->base,
  1295. config->prop_fb_damage_clips->base.id))
  1296. drm_warn_once(dev, "drm_plane_enable_fb_damage_clips() not called\n");
  1297. return __drm_plane_get_damage_clips(state);
  1298. }
  1299. EXPORT_SYMBOL(drm_plane_get_damage_clips);
  1300. struct drm_property *
  1301. drm_create_scaling_filter_prop(struct drm_device *dev,
  1302. unsigned int supported_filters)
  1303. {
  1304. struct drm_property *prop;
  1305. static const struct drm_prop_enum_list props[] = {
  1306. { DRM_SCALING_FILTER_DEFAULT, "Default" },
  1307. { DRM_SCALING_FILTER_NEAREST_NEIGHBOR, "Nearest Neighbor" },
  1308. };
  1309. unsigned int valid_mode_mask = BIT(DRM_SCALING_FILTER_DEFAULT) |
  1310. BIT(DRM_SCALING_FILTER_NEAREST_NEIGHBOR);
  1311. int i;
  1312. if (WARN_ON((supported_filters & ~valid_mode_mask) ||
  1313. ((supported_filters & BIT(DRM_SCALING_FILTER_DEFAULT)) == 0)))
  1314. return ERR_PTR(-EINVAL);
  1315. prop = drm_property_create(dev, DRM_MODE_PROP_ENUM,
  1316. "SCALING_FILTER",
  1317. hweight32(supported_filters));
  1318. if (!prop)
  1319. return ERR_PTR(-ENOMEM);
  1320. for (i = 0; i < ARRAY_SIZE(props); i++) {
  1321. int ret;
  1322. if (!(BIT(props[i].type) & supported_filters))
  1323. continue;
  1324. ret = drm_property_add_enum(prop, props[i].type,
  1325. props[i].name);
  1326. if (ret) {
  1327. drm_property_destroy(dev, prop);
  1328. return ERR_PTR(ret);
  1329. }
  1330. }
  1331. return prop;
  1332. }
  1333. /**
  1334. * drm_plane_create_scaling_filter_property - create a new scaling filter
  1335. * property
  1336. *
  1337. * @plane: drm plane
  1338. * @supported_filters: bitmask of supported scaling filters, must include
  1339. * BIT(DRM_SCALING_FILTER_DEFAULT).
  1340. *
  1341. * This function lets driver to enable the scaling filter property on a given
  1342. * plane.
  1343. *
  1344. * RETURNS:
  1345. * Zero for success or -errno
  1346. */
  1347. int drm_plane_create_scaling_filter_property(struct drm_plane *plane,
  1348. unsigned int supported_filters)
  1349. {
  1350. struct drm_property *prop =
  1351. drm_create_scaling_filter_prop(plane->dev, supported_filters);
  1352. if (IS_ERR(prop))
  1353. return PTR_ERR(prop);
  1354. drm_object_attach_property(&plane->base, prop,
  1355. DRM_SCALING_FILTER_DEFAULT);
  1356. plane->scaling_filter_property = prop;
  1357. return 0;
  1358. }
  1359. EXPORT_SYMBOL(drm_plane_create_scaling_filter_property);