drm_framebuffer.c 28 KB

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