drm_framebuffer.c 30 KB

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