drm_framebuffer.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  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/uaccess.h>
  24. #include <drm/drm_atomic.h>
  25. #include <drm/drm_atomic_uapi.h>
  26. #include <drm/drm_auth.h>
  27. #include <drm/drm_debugfs.h>
  28. #include <drm/drm_drv.h>
  29. #include <drm/drm_file.h>
  30. #include <drm/drm_fourcc.h>
  31. #include <drm/drm_framebuffer.h>
  32. #include <drm/drm_gem.h>
  33. #include <drm/drm_print.h>
  34. #include <drm/drm_util.h>
  35. #include "drm_crtc_internal.h"
  36. #include "drm_internal.h"
  37. /**
  38. * DOC: overview
  39. *
  40. * Frame buffers are abstract memory objects that provide a source of pixels to
  41. * scanout to a CRTC. Applications explicitly request the creation of frame
  42. * buffers through the DRM_IOCTL_MODE_ADDFB(2) ioctls and receive an opaque
  43. * handle that can be passed to the KMS CRTC control, plane configuration and
  44. * page flip functions.
  45. *
  46. * Frame buffers rely on the underlying memory manager for allocating backing
  47. * storage. When creating a frame buffer applications pass a memory handle
  48. * (or a list of memory handles for multi-planar formats) through the
  49. * &struct drm_mode_fb_cmd2 argument. For drivers using GEM as their userspace
  50. * buffer management interface this would be a GEM handle. Drivers are however
  51. * free to use their own backing storage object handles, e.g. vmwgfx directly
  52. * exposes special TTM handles to userspace and so expects TTM handles in the
  53. * create ioctl and not GEM handles.
  54. *
  55. * Framebuffers are tracked with &struct drm_framebuffer. They are published
  56. * using drm_framebuffer_init() - after calling that function userspace can use
  57. * and access the framebuffer object. The helper function
  58. * drm_helper_mode_fill_fb_struct() can be used to pre-fill the required
  59. * metadata fields.
  60. *
  61. * The lifetime of a drm framebuffer is controlled with a reference count,
  62. * drivers can grab additional references with drm_framebuffer_get() and drop
  63. * them again with drm_framebuffer_put(). For driver-private framebuffers for
  64. * which the last reference is never dropped (e.g. for the fbdev framebuffer
  65. * when the struct &struct drm_framebuffer is embedded into the fbdev helper
  66. * struct) drivers can manually clean up a framebuffer at module unload time
  67. * with drm_framebuffer_unregister_private(). But doing this is not
  68. * recommended, and it's better to have a normal free-standing &struct
  69. * drm_framebuffer.
  70. */
  71. int drm_framebuffer_check_src_coords(uint32_t src_x, uint32_t src_y,
  72. uint32_t src_w, uint32_t src_h,
  73. const struct drm_framebuffer *fb)
  74. {
  75. unsigned int fb_width, fb_height;
  76. fb_width = fb->width << 16;
  77. fb_height = fb->height << 16;
  78. /* Make sure source coordinates are inside the fb. */
  79. if (src_w > fb_width ||
  80. src_x > fb_width - src_w ||
  81. src_h > fb_height ||
  82. src_y > fb_height - src_h) {
  83. DRM_DEBUG_KMS("Invalid source coordinates "
  84. "%u.%06ux%u.%06u+%u.%06u+%u.%06u (fb %ux%u)\n",
  85. src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
  86. src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
  87. src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
  88. src_y >> 16, ((src_y & 0xffff) * 15625) >> 10,
  89. fb->width, fb->height);
  90. return -ENOSPC;
  91. }
  92. return 0;
  93. }
  94. /**
  95. * drm_mode_addfb - add an FB to the graphics configuration
  96. * @dev: drm device for the ioctl
  97. * @or: pointer to request structure
  98. * @file_priv: drm file
  99. *
  100. * Add a new FB to the specified CRTC, given a user request. This is the
  101. * original addfb ioctl which only supported RGB formats.
  102. *
  103. * Called by the user via ioctl, or by an in-kernel client.
  104. *
  105. * Returns:
  106. * Zero on success, negative errno on failure.
  107. */
  108. int drm_mode_addfb(struct drm_device *dev, struct drm_mode_fb_cmd *or,
  109. struct drm_file *file_priv)
  110. {
  111. struct drm_mode_fb_cmd2 r = {};
  112. int ret;
  113. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  114. return -EOPNOTSUPP;
  115. r.pixel_format = drm_driver_legacy_fb_format(dev, or->bpp, or->depth);
  116. if (r.pixel_format == DRM_FORMAT_INVALID) {
  117. DRM_DEBUG("bad {bpp:%d, depth:%d}\n", or->bpp, or->depth);
  118. return -EINVAL;
  119. }
  120. /* convert to new format and call new ioctl */
  121. r.fb_id = or->fb_id;
  122. r.width = or->width;
  123. r.height = or->height;
  124. r.pitches[0] = or->pitch;
  125. r.handles[0] = or->handle;
  126. ret = drm_mode_addfb2(dev, &r, file_priv);
  127. if (ret)
  128. return ret;
  129. or->fb_id = r.fb_id;
  130. return 0;
  131. }
  132. int drm_mode_addfb_ioctl(struct drm_device *dev,
  133. void *data, struct drm_file *file_priv)
  134. {
  135. return drm_mode_addfb(dev, data, file_priv);
  136. }
  137. static int fb_plane_width(int width,
  138. const struct drm_format_info *format, int plane)
  139. {
  140. if (plane == 0)
  141. return width;
  142. return DIV_ROUND_UP(width, format->hsub);
  143. }
  144. static int fb_plane_height(int height,
  145. const struct drm_format_info *format, int plane)
  146. {
  147. if (plane == 0)
  148. return height;
  149. return DIV_ROUND_UP(height, format->vsub);
  150. }
  151. static int framebuffer_check(struct drm_device *dev,
  152. const struct drm_mode_fb_cmd2 *r)
  153. {
  154. const struct drm_format_info *info;
  155. int i;
  156. /* check if the format is supported at all */
  157. if (!__drm_format_info(r->pixel_format)) {
  158. struct drm_format_name_buf format_name;
  159. DRM_DEBUG_KMS("bad framebuffer format %s\n",
  160. drm_get_format_name(r->pixel_format,
  161. &format_name));
  162. return -EINVAL;
  163. }
  164. if (r->width == 0) {
  165. DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
  166. return -EINVAL;
  167. }
  168. if (r->height == 0) {
  169. DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
  170. return -EINVAL;
  171. }
  172. /* now let the driver pick its own format info */
  173. info = drm_get_format_info(dev, r);
  174. for (i = 0; i < info->num_planes; i++) {
  175. unsigned int width = fb_plane_width(r->width, info, i);
  176. unsigned int height = fb_plane_height(r->height, info, i);
  177. unsigned int block_size = info->char_per_block[i];
  178. u64 min_pitch = drm_format_info_min_pitch(info, i, width);
  179. if (!block_size && (r->modifier[i] == DRM_FORMAT_MOD_LINEAR)) {
  180. DRM_DEBUG_KMS("Format requires non-linear modifier for plane %d\n", i);
  181. return -EINVAL;
  182. }
  183. if (!r->handles[i]) {
  184. DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
  185. return -EINVAL;
  186. }
  187. if (min_pitch > UINT_MAX)
  188. return -ERANGE;
  189. if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
  190. return -ERANGE;
  191. if (block_size && r->pitches[i] < min_pitch) {
  192. DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
  193. return -EINVAL;
  194. }
  195. if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
  196. DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
  197. r->modifier[i], i);
  198. return -EINVAL;
  199. }
  200. if (r->flags & DRM_MODE_FB_MODIFIERS &&
  201. r->modifier[i] != r->modifier[0]) {
  202. DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
  203. r->modifier[i], i);
  204. return -EINVAL;
  205. }
  206. /* modifier specific checks: */
  207. switch (r->modifier[i]) {
  208. case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
  209. /* NOTE: the pitch restriction may be lifted later if it turns
  210. * out that no hw has this restriction:
  211. */
  212. if (r->pixel_format != DRM_FORMAT_NV12 ||
  213. width % 128 || height % 32 ||
  214. r->pitches[i] % 128) {
  215. DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
  216. return -EINVAL;
  217. }
  218. break;
  219. default:
  220. break;
  221. }
  222. }
  223. for (i = info->num_planes; i < 4; i++) {
  224. if (r->modifier[i]) {
  225. DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
  226. return -EINVAL;
  227. }
  228. /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
  229. if (!(r->flags & DRM_MODE_FB_MODIFIERS))
  230. continue;
  231. if (r->handles[i]) {
  232. DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
  233. return -EINVAL;
  234. }
  235. if (r->pitches[i]) {
  236. DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
  237. return -EINVAL;
  238. }
  239. if (r->offsets[i]) {
  240. DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
  241. return -EINVAL;
  242. }
  243. }
  244. return 0;
  245. }
  246. struct drm_framebuffer *
  247. drm_internal_framebuffer_create(struct drm_device *dev,
  248. const struct drm_mode_fb_cmd2 *r,
  249. struct drm_file *file_priv)
  250. {
  251. struct drm_mode_config *config = &dev->mode_config;
  252. struct drm_framebuffer *fb;
  253. int ret;
  254. if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
  255. DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
  256. return ERR_PTR(-EINVAL);
  257. }
  258. if ((config->min_width > r->width) || (r->width > config->max_width)) {
  259. DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
  260. r->width, config->min_width, config->max_width);
  261. return ERR_PTR(-EINVAL);
  262. }
  263. if ((config->min_height > r->height) || (r->height > config->max_height)) {
  264. DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
  265. r->height, config->min_height, config->max_height);
  266. return ERR_PTR(-EINVAL);
  267. }
  268. if (r->flags & DRM_MODE_FB_MODIFIERS &&
  269. !dev->mode_config.allow_fb_modifiers) {
  270. DRM_DEBUG_KMS("driver does not support fb modifiers\n");
  271. return ERR_PTR(-EINVAL);
  272. }
  273. ret = framebuffer_check(dev, r);
  274. if (ret)
  275. return ERR_PTR(ret);
  276. fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
  277. if (IS_ERR(fb)) {
  278. DRM_DEBUG_KMS("could not create framebuffer\n");
  279. return fb;
  280. }
  281. return fb;
  282. }
  283. EXPORT_SYMBOL_FOR_TESTS_ONLY(drm_internal_framebuffer_create);
  284. /**
  285. * drm_mode_addfb2 - add an FB to the graphics configuration
  286. * @dev: drm device for the ioctl
  287. * @data: data pointer for the ioctl
  288. * @file_priv: drm file for the ioctl call
  289. *
  290. * Add a new FB to the specified CRTC, given a user request with format. This is
  291. * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
  292. * and uses fourcc codes as pixel format specifiers.
  293. *
  294. * Called by the user via ioctl.
  295. *
  296. * Returns:
  297. * Zero on success, negative errno on failure.
  298. */
  299. int drm_mode_addfb2(struct drm_device *dev,
  300. void *data, struct drm_file *file_priv)
  301. {
  302. struct drm_mode_fb_cmd2 *r = data;
  303. struct drm_framebuffer *fb;
  304. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  305. return -EOPNOTSUPP;
  306. fb = drm_internal_framebuffer_create(dev, r, file_priv);
  307. if (IS_ERR(fb))
  308. return PTR_ERR(fb);
  309. DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
  310. r->fb_id = fb->base.id;
  311. /* Transfer ownership to the filp for reaping on close */
  312. mutex_lock(&file_priv->fbs_lock);
  313. list_add(&fb->filp_head, &file_priv->fbs);
  314. mutex_unlock(&file_priv->fbs_lock);
  315. return 0;
  316. }
  317. int drm_mode_addfb2_ioctl(struct drm_device *dev,
  318. void *data, struct drm_file *file_priv)
  319. {
  320. #ifdef __BIG_ENDIAN
  321. if (!dev->mode_config.quirk_addfb_prefer_host_byte_order) {
  322. /*
  323. * Drivers must set the
  324. * quirk_addfb_prefer_host_byte_order quirk to make
  325. * the drm_mode_addfb() compat code work correctly on
  326. * bigendian machines.
  327. *
  328. * If they don't they interpret pixel_format values
  329. * incorrectly for bug compatibility, which in turn
  330. * implies the ADDFB2 ioctl does not work correctly
  331. * then. So block it to make userspace fallback to
  332. * ADDFB.
  333. */
  334. DRM_DEBUG_KMS("addfb2 broken on bigendian");
  335. return -EOPNOTSUPP;
  336. }
  337. #endif
  338. return drm_mode_addfb2(dev, data, file_priv);
  339. }
  340. struct drm_mode_rmfb_work {
  341. struct work_struct work;
  342. struct list_head fbs;
  343. };
  344. static void drm_mode_rmfb_work_fn(struct work_struct *w)
  345. {
  346. struct drm_mode_rmfb_work *arg = container_of(w, typeof(*arg), work);
  347. while (!list_empty(&arg->fbs)) {
  348. struct drm_framebuffer *fb =
  349. list_first_entry(&arg->fbs, typeof(*fb), filp_head);
  350. list_del_init(&fb->filp_head);
  351. drm_framebuffer_remove(fb);
  352. }
  353. }
  354. /**
  355. * drm_mode_rmfb - remove an FB from the configuration
  356. * @dev: drm device
  357. * @fb_id: id of framebuffer to remove
  358. * @file_priv: drm file
  359. *
  360. * Remove the specified FB.
  361. *
  362. * Called by the user via ioctl, or by an in-kernel client.
  363. *
  364. * Returns:
  365. * Zero on success, negative errno on failure.
  366. */
  367. int drm_mode_rmfb(struct drm_device *dev, u32 fb_id,
  368. struct drm_file *file_priv)
  369. {
  370. struct drm_framebuffer *fb = NULL;
  371. struct drm_framebuffer *fbl = NULL;
  372. int found = 0;
  373. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  374. return -EOPNOTSUPP;
  375. fb = drm_framebuffer_lookup(dev, file_priv, fb_id);
  376. if (!fb)
  377. return -ENOENT;
  378. mutex_lock(&file_priv->fbs_lock);
  379. list_for_each_entry(fbl, &file_priv->fbs, filp_head)
  380. if (fb == fbl)
  381. found = 1;
  382. if (!found) {
  383. mutex_unlock(&file_priv->fbs_lock);
  384. goto fail_unref;
  385. }
  386. list_del_init(&fb->filp_head);
  387. mutex_unlock(&file_priv->fbs_lock);
  388. /* drop the reference we picked up in framebuffer lookup */
  389. drm_framebuffer_put(fb);
  390. /*
  391. * we now own the reference that was stored in the fbs list
  392. *
  393. * drm_framebuffer_remove may fail with -EINTR on pending signals,
  394. * so run this in a separate stack as there's no way to correctly
  395. * handle this after the fb is already removed from the lookup table.
  396. */
  397. if (drm_framebuffer_read_refcount(fb) > 1) {
  398. struct drm_mode_rmfb_work arg;
  399. INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
  400. INIT_LIST_HEAD(&arg.fbs);
  401. list_add_tail(&fb->filp_head, &arg.fbs);
  402. schedule_work(&arg.work);
  403. flush_work(&arg.work);
  404. destroy_work_on_stack(&arg.work);
  405. } else
  406. drm_framebuffer_put(fb);
  407. return 0;
  408. fail_unref:
  409. drm_framebuffer_put(fb);
  410. return -ENOENT;
  411. }
  412. int drm_mode_rmfb_ioctl(struct drm_device *dev,
  413. void *data, struct drm_file *file_priv)
  414. {
  415. uint32_t *fb_id = data;
  416. return drm_mode_rmfb(dev, *fb_id, file_priv);
  417. }
  418. /**
  419. * drm_mode_getfb - get FB info
  420. * @dev: drm device for the ioctl
  421. * @data: data pointer for the ioctl
  422. * @file_priv: drm file for the ioctl call
  423. *
  424. * Lookup the FB given its ID and return info about it.
  425. *
  426. * Called by the user via ioctl.
  427. *
  428. * Returns:
  429. * Zero on success, negative errno on failure.
  430. */
  431. int drm_mode_getfb(struct drm_device *dev,
  432. void *data, struct drm_file *file_priv)
  433. {
  434. struct drm_mode_fb_cmd *r = data;
  435. struct drm_framebuffer *fb;
  436. int ret;
  437. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  438. return -EOPNOTSUPP;
  439. fb = drm_framebuffer_lookup(dev, file_priv, r->fb_id);
  440. if (!fb)
  441. return -ENOENT;
  442. /* Multi-planar framebuffers need getfb2. */
  443. if (fb->format->num_planes > 1) {
  444. ret = -EINVAL;
  445. goto out;
  446. }
  447. if (!fb->funcs->create_handle) {
  448. ret = -ENODEV;
  449. goto out;
  450. }
  451. r->height = fb->height;
  452. r->width = fb->width;
  453. r->depth = fb->format->depth;
  454. r->bpp = fb->format->cpp[0] * 8;
  455. r->pitch = fb->pitches[0];
  456. /* GET_FB() is an unprivileged ioctl so we must not return a
  457. * buffer-handle to non-master processes! For
  458. * backwards-compatibility reasons, we cannot make GET_FB() privileged,
  459. * so just return an invalid handle for non-masters.
  460. */
  461. if (!drm_is_current_master(file_priv) && !capable(CAP_SYS_ADMIN)) {
  462. r->handle = 0;
  463. ret = 0;
  464. goto out;
  465. }
  466. ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
  467. out:
  468. drm_framebuffer_put(fb);
  469. return ret;
  470. }
  471. /**
  472. * drm_mode_getfb2 - get extended FB info
  473. * @dev: drm device for the ioctl
  474. * @data: data pointer for the ioctl
  475. * @file_priv: drm file for the ioctl call
  476. *
  477. * Lookup the FB given its ID and return info about it.
  478. *
  479. * Called by the user via ioctl.
  480. *
  481. * Returns:
  482. * Zero on success, negative errno on failure.
  483. */
  484. int drm_mode_getfb2_ioctl(struct drm_device *dev,
  485. void *data, struct drm_file *file_priv)
  486. {
  487. struct drm_mode_fb_cmd2 *r = data;
  488. struct drm_framebuffer *fb;
  489. unsigned int i;
  490. int ret;
  491. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  492. return -EINVAL;
  493. fb = drm_framebuffer_lookup(dev, file_priv, r->fb_id);
  494. if (!fb)
  495. return -ENOENT;
  496. /* For multi-plane framebuffers, we require the driver to place the
  497. * GEM objects directly in the drm_framebuffer. For single-plane
  498. * framebuffers, we can fall back to create_handle.
  499. */
  500. if (!fb->obj[0] &&
  501. (fb->format->num_planes > 1 || !fb->funcs->create_handle)) {
  502. ret = -ENODEV;
  503. goto out;
  504. }
  505. r->height = fb->height;
  506. r->width = fb->width;
  507. r->pixel_format = fb->format->format;
  508. r->flags = 0;
  509. if (dev->mode_config.allow_fb_modifiers)
  510. r->flags |= DRM_MODE_FB_MODIFIERS;
  511. for (i = 0; i < ARRAY_SIZE(r->handles); i++) {
  512. r->handles[i] = 0;
  513. r->pitches[i] = 0;
  514. r->offsets[i] = 0;
  515. r->modifier[i] = 0;
  516. }
  517. for (i = 0; i < fb->format->num_planes; i++) {
  518. r->pitches[i] = fb->pitches[i];
  519. r->offsets[i] = fb->offsets[i];
  520. if (dev->mode_config.allow_fb_modifiers)
  521. r->modifier[i] = fb->modifier;
  522. }
  523. /* GET_FB2() is an unprivileged ioctl so we must not return a
  524. * buffer-handle to non master/root processes! To match GET_FB()
  525. * just return invalid handles (0) for non masters/root
  526. * rather than making GET_FB2() privileged.
  527. */
  528. if (!drm_is_current_master(file_priv) && !capable(CAP_SYS_ADMIN)) {
  529. ret = 0;
  530. goto out;
  531. }
  532. for (i = 0; i < fb->format->num_planes; i++) {
  533. int j;
  534. /* If we reuse the same object for multiple planes, also
  535. * return the same handle.
  536. */
  537. for (j = 0; j < i; j++) {
  538. if (fb->obj[i] == fb->obj[j]) {
  539. r->handles[i] = r->handles[j];
  540. break;
  541. }
  542. }
  543. if (r->handles[i])
  544. continue;
  545. if (fb->obj[i]) {
  546. ret = drm_gem_handle_create(file_priv, fb->obj[i],
  547. &r->handles[i]);
  548. } else {
  549. WARN_ON(i > 0);
  550. ret = fb->funcs->create_handle(fb, file_priv,
  551. &r->handles[i]);
  552. }
  553. if (ret != 0)
  554. goto out;
  555. }
  556. out:
  557. if (ret != 0) {
  558. /* Delete any previously-created handles on failure. */
  559. for (i = 0; i < ARRAY_SIZE(r->handles); i++) {
  560. int j;
  561. if (r->handles[i])
  562. drm_gem_handle_delete(file_priv, r->handles[i]);
  563. /* Zero out any handles identical to the one we just
  564. * deleted.
  565. */
  566. for (j = i + 1; j < ARRAY_SIZE(r->handles); j++) {
  567. if (r->handles[j] == r->handles[i])
  568. r->handles[j] = 0;
  569. }
  570. }
  571. }
  572. drm_framebuffer_put(fb);
  573. return ret;
  574. }
  575. /**
  576. * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
  577. * @dev: drm device for the ioctl
  578. * @data: data pointer for the ioctl
  579. * @file_priv: drm file for the ioctl call
  580. *
  581. * Lookup the FB and flush out the damaged area supplied by userspace as a clip
  582. * rectangle list. Generic userspace which does frontbuffer rendering must call
  583. * this ioctl to flush out the changes on manual-update display outputs, e.g.
  584. * usb display-link, mipi manual update panels or edp panel self refresh modes.
  585. *
  586. * Modesetting drivers which always update the frontbuffer do not need to
  587. * implement the corresponding &drm_framebuffer_funcs.dirty callback.
  588. *
  589. * Called by the user via ioctl.
  590. *
  591. * Returns:
  592. * Zero on success, negative errno on failure.
  593. */
  594. int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
  595. void *data, struct drm_file *file_priv)
  596. {
  597. struct drm_clip_rect __user *clips_ptr;
  598. struct drm_clip_rect *clips = NULL;
  599. struct drm_mode_fb_dirty_cmd *r = data;
  600. struct drm_framebuffer *fb;
  601. unsigned flags;
  602. int num_clips;
  603. int ret;
  604. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  605. return -EOPNOTSUPP;
  606. fb = drm_framebuffer_lookup(dev, file_priv, r->fb_id);
  607. if (!fb)
  608. return -ENOENT;
  609. num_clips = r->num_clips;
  610. clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
  611. if (!num_clips != !clips_ptr) {
  612. ret = -EINVAL;
  613. goto out_err1;
  614. }
  615. flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
  616. /* If userspace annotates copy, clips must come in pairs */
  617. if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
  618. ret = -EINVAL;
  619. goto out_err1;
  620. }
  621. if (num_clips && clips_ptr) {
  622. if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
  623. ret = -EINVAL;
  624. goto out_err1;
  625. }
  626. clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
  627. if (!clips) {
  628. ret = -ENOMEM;
  629. goto out_err1;
  630. }
  631. ret = copy_from_user(clips, clips_ptr,
  632. num_clips * sizeof(*clips));
  633. if (ret) {
  634. ret = -EFAULT;
  635. goto out_err2;
  636. }
  637. }
  638. if (fb->funcs->dirty) {
  639. ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
  640. clips, num_clips);
  641. } else {
  642. ret = -ENOSYS;
  643. }
  644. out_err2:
  645. kfree(clips);
  646. out_err1:
  647. drm_framebuffer_put(fb);
  648. return ret;
  649. }
  650. /**
  651. * drm_fb_release - remove and free the FBs on this file
  652. * @priv: drm file for the ioctl
  653. *
  654. * Destroy all the FBs associated with @filp.
  655. *
  656. * Called by the user via ioctl.
  657. *
  658. * Returns:
  659. * Zero on success, negative errno on failure.
  660. */
  661. void drm_fb_release(struct drm_file *priv)
  662. {
  663. struct drm_framebuffer *fb, *tfb;
  664. struct drm_mode_rmfb_work arg;
  665. INIT_LIST_HEAD(&arg.fbs);
  666. /*
  667. * When the file gets released that means no one else can access the fb
  668. * list any more, so no need to grab fpriv->fbs_lock. And we need to
  669. * avoid upsetting lockdep since the universal cursor code adds a
  670. * framebuffer while holding mutex locks.
  671. *
  672. * Note that a real deadlock between fpriv->fbs_lock and the modeset
  673. * locks is impossible here since no one else but this function can get
  674. * at it any more.
  675. */
  676. list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
  677. if (drm_framebuffer_read_refcount(fb) > 1) {
  678. list_move_tail(&fb->filp_head, &arg.fbs);
  679. } else {
  680. list_del_init(&fb->filp_head);
  681. /* This drops the fpriv->fbs reference. */
  682. drm_framebuffer_put(fb);
  683. }
  684. }
  685. if (!list_empty(&arg.fbs)) {
  686. INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
  687. schedule_work(&arg.work);
  688. flush_work(&arg.work);
  689. destroy_work_on_stack(&arg.work);
  690. }
  691. }
  692. void drm_framebuffer_free(struct kref *kref)
  693. {
  694. struct drm_framebuffer *fb =
  695. container_of(kref, struct drm_framebuffer, base.refcount);
  696. struct drm_device *dev = fb->dev;
  697. /*
  698. * The lookup idr holds a weak reference, which has not necessarily been
  699. * removed at this point. Check for that.
  700. */
  701. drm_mode_object_unregister(dev, &fb->base);
  702. fb->funcs->destroy(fb);
  703. }
  704. /**
  705. * drm_framebuffer_init - initialize a framebuffer
  706. * @dev: DRM device
  707. * @fb: framebuffer to be initialized
  708. * @funcs: ... with these functions
  709. *
  710. * Allocates an ID for the framebuffer's parent mode object, sets its mode
  711. * functions & device file and adds it to the master fd list.
  712. *
  713. * IMPORTANT:
  714. * This functions publishes the fb and makes it available for concurrent access
  715. * by other users. Which means by this point the fb _must_ be fully set up -
  716. * since all the fb attributes are invariant over its lifetime, no further
  717. * locking but only correct reference counting is required.
  718. *
  719. * Returns:
  720. * Zero on success, error code on failure.
  721. */
  722. int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
  723. const struct drm_framebuffer_funcs *funcs)
  724. {
  725. int ret;
  726. if (WARN_ON_ONCE(fb->dev != dev || !fb->format))
  727. return -EINVAL;
  728. INIT_LIST_HEAD(&fb->filp_head);
  729. fb->funcs = funcs;
  730. strcpy(fb->comm, current->comm);
  731. ret = __drm_mode_object_add(dev, &fb->base, DRM_MODE_OBJECT_FB,
  732. false, drm_framebuffer_free);
  733. if (ret)
  734. goto out;
  735. mutex_lock(&dev->mode_config.fb_lock);
  736. dev->mode_config.num_fb++;
  737. list_add(&fb->head, &dev->mode_config.fb_list);
  738. mutex_unlock(&dev->mode_config.fb_lock);
  739. drm_mode_object_register(dev, &fb->base);
  740. out:
  741. return ret;
  742. }
  743. EXPORT_SYMBOL(drm_framebuffer_init);
  744. /**
  745. * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
  746. * @dev: drm device
  747. * @file_priv: drm file to check for lease against.
  748. * @id: id of the fb object
  749. *
  750. * If successful, this grabs an additional reference to the framebuffer -
  751. * callers need to make sure to eventually unreference the returned framebuffer
  752. * again, using drm_framebuffer_put().
  753. */
  754. struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
  755. struct drm_file *file_priv,
  756. uint32_t id)
  757. {
  758. struct drm_mode_object *obj;
  759. struct drm_framebuffer *fb = NULL;
  760. obj = __drm_mode_object_find(dev, file_priv, id, DRM_MODE_OBJECT_FB);
  761. if (obj)
  762. fb = obj_to_fb(obj);
  763. return fb;
  764. }
  765. EXPORT_SYMBOL(drm_framebuffer_lookup);
  766. /**
  767. * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
  768. * @fb: fb to unregister
  769. *
  770. * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
  771. * those used for fbdev. Note that the caller must hold a reference of its own,
  772. * i.e. the object may not be destroyed through this call (since it'll lead to a
  773. * locking inversion).
  774. *
  775. * NOTE: This function is deprecated. For driver-private framebuffers it is not
  776. * recommended to embed a framebuffer struct info fbdev struct, instead, a
  777. * framebuffer pointer is preferred and drm_framebuffer_put() should be called
  778. * when the framebuffer is to be cleaned up.
  779. */
  780. void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
  781. {
  782. struct drm_device *dev;
  783. if (!fb)
  784. return;
  785. dev = fb->dev;
  786. /* Mark fb as reaped and drop idr ref. */
  787. drm_mode_object_unregister(dev, &fb->base);
  788. }
  789. EXPORT_SYMBOL(drm_framebuffer_unregister_private);
  790. /**
  791. * drm_framebuffer_cleanup - remove a framebuffer object
  792. * @fb: framebuffer to remove
  793. *
  794. * Cleanup framebuffer. This function is intended to be used from the drivers
  795. * &drm_framebuffer_funcs.destroy callback. It can also be used to clean up
  796. * driver private framebuffers embedded into a larger structure.
  797. *
  798. * Note that this function does not remove the fb from active usage - if it is
  799. * still used anywhere, hilarity can ensue since userspace could call getfb on
  800. * the id and get back -EINVAL. Obviously no concern at driver unload time.
  801. *
  802. * Also, the framebuffer will not be removed from the lookup idr - for
  803. * user-created framebuffers this will happen in in the rmfb ioctl. For
  804. * driver-private objects (e.g. for fbdev) drivers need to explicitly call
  805. * drm_framebuffer_unregister_private.
  806. */
  807. void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
  808. {
  809. struct drm_device *dev = fb->dev;
  810. mutex_lock(&dev->mode_config.fb_lock);
  811. list_del(&fb->head);
  812. dev->mode_config.num_fb--;
  813. mutex_unlock(&dev->mode_config.fb_lock);
  814. }
  815. EXPORT_SYMBOL(drm_framebuffer_cleanup);
  816. static int atomic_remove_fb(struct drm_framebuffer *fb)
  817. {
  818. struct drm_modeset_acquire_ctx ctx;
  819. struct drm_device *dev = fb->dev;
  820. struct drm_atomic_state *state;
  821. struct drm_plane *plane;
  822. struct drm_connector *conn __maybe_unused;
  823. struct drm_connector_state *conn_state;
  824. int i, ret;
  825. unsigned plane_mask;
  826. bool disable_crtcs = false;
  827. retry_disable:
  828. drm_modeset_acquire_init(&ctx, 0);
  829. state = drm_atomic_state_alloc(dev);
  830. if (!state) {
  831. ret = -ENOMEM;
  832. goto out;
  833. }
  834. state->acquire_ctx = &ctx;
  835. retry:
  836. plane_mask = 0;
  837. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  838. if (ret)
  839. goto unlock;
  840. drm_for_each_plane(plane, dev) {
  841. struct drm_plane_state *plane_state;
  842. if (plane->state->fb != fb)
  843. continue;
  844. plane_state = drm_atomic_get_plane_state(state, plane);
  845. if (IS_ERR(plane_state)) {
  846. ret = PTR_ERR(plane_state);
  847. goto unlock;
  848. }
  849. if (disable_crtcs && plane_state->crtc->primary == plane) {
  850. struct drm_crtc_state *crtc_state;
  851. crtc_state = drm_atomic_get_existing_crtc_state(state, plane_state->crtc);
  852. ret = drm_atomic_add_affected_connectors(state, plane_state->crtc);
  853. if (ret)
  854. goto unlock;
  855. crtc_state->active = false;
  856. ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
  857. if (ret)
  858. goto unlock;
  859. }
  860. drm_atomic_set_fb_for_plane(plane_state, NULL);
  861. ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
  862. if (ret)
  863. goto unlock;
  864. plane_mask |= drm_plane_mask(plane);
  865. }
  866. /* This list is only filled when disable_crtcs is set. */
  867. for_each_new_connector_in_state(state, conn, conn_state, i) {
  868. ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
  869. if (ret)
  870. goto unlock;
  871. }
  872. if (plane_mask)
  873. ret = drm_atomic_commit(state);
  874. unlock:
  875. if (ret == -EDEADLK) {
  876. drm_atomic_state_clear(state);
  877. drm_modeset_backoff(&ctx);
  878. goto retry;
  879. }
  880. drm_atomic_state_put(state);
  881. out:
  882. drm_modeset_drop_locks(&ctx);
  883. drm_modeset_acquire_fini(&ctx);
  884. if (ret == -EINVAL && !disable_crtcs) {
  885. disable_crtcs = true;
  886. goto retry_disable;
  887. }
  888. return ret;
  889. }
  890. static void legacy_remove_fb(struct drm_framebuffer *fb)
  891. {
  892. struct drm_device *dev = fb->dev;
  893. struct drm_crtc *crtc;
  894. struct drm_plane *plane;
  895. drm_modeset_lock_all(dev);
  896. /* remove from any CRTC */
  897. drm_for_each_crtc(crtc, dev) {
  898. if (crtc->primary->fb == fb) {
  899. /* should turn off the crtc */
  900. if (drm_crtc_force_disable(crtc))
  901. DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
  902. }
  903. }
  904. drm_for_each_plane(plane, dev) {
  905. if (plane->fb == fb)
  906. drm_plane_force_disable(plane);
  907. }
  908. drm_modeset_unlock_all(dev);
  909. }
  910. /**
  911. * drm_framebuffer_remove - remove and unreference a framebuffer object
  912. * @fb: framebuffer to remove
  913. *
  914. * Scans all the CRTCs and planes in @dev's mode_config. If they're
  915. * using @fb, removes it, setting it to NULL. Then drops the reference to the
  916. * passed-in framebuffer. Might take the modeset locks.
  917. *
  918. * Note that this function optimizes the cleanup away if the caller holds the
  919. * last reference to the framebuffer. It is also guaranteed to not take the
  920. * modeset locks in this case.
  921. */
  922. void drm_framebuffer_remove(struct drm_framebuffer *fb)
  923. {
  924. struct drm_device *dev;
  925. if (!fb)
  926. return;
  927. dev = fb->dev;
  928. WARN_ON(!list_empty(&fb->filp_head));
  929. /*
  930. * drm ABI mandates that we remove any deleted framebuffers from active
  931. * useage. But since most sane clients only remove framebuffers they no
  932. * longer need, try to optimize this away.
  933. *
  934. * Since we're holding a reference ourselves, observing a refcount of 1
  935. * means that we're the last holder and can skip it. Also, the refcount
  936. * can never increase from 1 again, so we don't need any barriers or
  937. * locks.
  938. *
  939. * Note that userspace could try to race with use and instate a new
  940. * usage _after_ we've cleared all current ones. End result will be an
  941. * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
  942. * in this manner.
  943. */
  944. if (drm_framebuffer_read_refcount(fb) > 1) {
  945. if (drm_drv_uses_atomic_modeset(dev)) {
  946. int ret = atomic_remove_fb(fb);
  947. WARN(ret, "atomic remove_fb failed with %i\n", ret);
  948. } else
  949. legacy_remove_fb(fb);
  950. }
  951. drm_framebuffer_put(fb);
  952. }
  953. EXPORT_SYMBOL(drm_framebuffer_remove);
  954. /**
  955. * drm_framebuffer_plane_width - width of the plane given the first plane
  956. * @width: width of the first plane
  957. * @fb: the framebuffer
  958. * @plane: plane index
  959. *
  960. * Returns:
  961. * The width of @plane, given that the width of the first plane is @width.
  962. */
  963. int drm_framebuffer_plane_width(int width,
  964. const struct drm_framebuffer *fb, int plane)
  965. {
  966. if (plane >= fb->format->num_planes)
  967. return 0;
  968. return fb_plane_width(width, fb->format, plane);
  969. }
  970. EXPORT_SYMBOL(drm_framebuffer_plane_width);
  971. /**
  972. * drm_framebuffer_plane_height - height of the plane given the first plane
  973. * @height: height of the first plane
  974. * @fb: the framebuffer
  975. * @plane: plane index
  976. *
  977. * Returns:
  978. * The height of @plane, given that the height of the first plane is @height.
  979. */
  980. int drm_framebuffer_plane_height(int height,
  981. const struct drm_framebuffer *fb, int plane)
  982. {
  983. if (plane >= fb->format->num_planes)
  984. return 0;
  985. return fb_plane_height(height, fb->format, plane);
  986. }
  987. EXPORT_SYMBOL(drm_framebuffer_plane_height);
  988. void drm_framebuffer_print_info(struct drm_printer *p, unsigned int indent,
  989. const struct drm_framebuffer *fb)
  990. {
  991. struct drm_format_name_buf format_name;
  992. unsigned int i;
  993. drm_printf_indent(p, indent, "allocated by = %s\n", fb->comm);
  994. drm_printf_indent(p, indent, "refcount=%u\n",
  995. drm_framebuffer_read_refcount(fb));
  996. drm_printf_indent(p, indent, "format=%s\n",
  997. drm_get_format_name(fb->format->format, &format_name));
  998. drm_printf_indent(p, indent, "modifier=0x%llx\n", fb->modifier);
  999. drm_printf_indent(p, indent, "size=%ux%u\n", fb->width, fb->height);
  1000. drm_printf_indent(p, indent, "layers:\n");
  1001. for (i = 0; i < fb->format->num_planes; i++) {
  1002. drm_printf_indent(p, indent + 1, "size[%u]=%dx%d\n", i,
  1003. drm_framebuffer_plane_width(fb->width, fb, i),
  1004. drm_framebuffer_plane_height(fb->height, fb, i));
  1005. drm_printf_indent(p, indent + 1, "pitch[%u]=%u\n", i, fb->pitches[i]);
  1006. drm_printf_indent(p, indent + 1, "offset[%u]=%u\n", i, fb->offsets[i]);
  1007. drm_printf_indent(p, indent + 1, "obj[%u]:%s\n", i,
  1008. fb->obj[i] ? "" : "(null)");
  1009. if (fb->obj[i])
  1010. drm_gem_print_info(p, indent + 2, fb->obj[i]);
  1011. }
  1012. }
  1013. #ifdef CONFIG_DEBUG_FS
  1014. static int drm_framebuffer_info(struct seq_file *m, void *data)
  1015. {
  1016. struct drm_info_node *node = m->private;
  1017. struct drm_device *dev = node->minor->dev;
  1018. struct drm_printer p = drm_seq_file_printer(m);
  1019. struct drm_framebuffer *fb;
  1020. mutex_lock(&dev->mode_config.fb_lock);
  1021. drm_for_each_fb(fb, dev) {
  1022. drm_printf(&p, "framebuffer[%u]:\n", fb->base.id);
  1023. drm_framebuffer_print_info(&p, 1, fb);
  1024. }
  1025. mutex_unlock(&dev->mode_config.fb_lock);
  1026. return 0;
  1027. }
  1028. static const struct drm_info_list drm_framebuffer_debugfs_list[] = {
  1029. { "framebuffer", drm_framebuffer_info, 0 },
  1030. };
  1031. void drm_framebuffer_debugfs_init(struct drm_minor *minor)
  1032. {
  1033. drm_debugfs_create_files(drm_framebuffer_debugfs_list,
  1034. ARRAY_SIZE(drm_framebuffer_debugfs_list),
  1035. minor->debugfs_root, minor);
  1036. }
  1037. #endif