drm_framebuffer.c 33 KB

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