drm_plane.c 57 KB

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