drm_framebuffer.c 29 KB

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