drm_framebuffer.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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_crtc_internal.h"
  27. /**
  28. * drm_mode_addfb - add an FB to the graphics configuration
  29. * @dev: drm device for the ioctl
  30. * @data: data pointer for the ioctl
  31. * @file_priv: drm file for the ioctl call
  32. *
  33. * Add a new FB to the specified CRTC, given a user request. This is the
  34. * original addfb ioctl which only supported RGB formats.
  35. *
  36. * Called by the user via ioctl.
  37. *
  38. * Returns:
  39. * Zero on success, negative errno on failure.
  40. */
  41. int drm_mode_addfb(struct drm_device *dev,
  42. void *data, struct drm_file *file_priv)
  43. {
  44. struct drm_mode_fb_cmd *or = data;
  45. struct drm_mode_fb_cmd2 r = {};
  46. int ret;
  47. /* convert to new format and call new ioctl */
  48. r.fb_id = or->fb_id;
  49. r.width = or->width;
  50. r.height = or->height;
  51. r.pitches[0] = or->pitch;
  52. r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
  53. r.handles[0] = or->handle;
  54. ret = drm_mode_addfb2(dev, &r, file_priv);
  55. if (ret)
  56. return ret;
  57. or->fb_id = r.fb_id;
  58. return 0;
  59. }
  60. static int format_check(const struct drm_mode_fb_cmd2 *r)
  61. {
  62. uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
  63. char *format_name;
  64. switch (format) {
  65. case DRM_FORMAT_C8:
  66. case DRM_FORMAT_RGB332:
  67. case DRM_FORMAT_BGR233:
  68. case DRM_FORMAT_XRGB4444:
  69. case DRM_FORMAT_XBGR4444:
  70. case DRM_FORMAT_RGBX4444:
  71. case DRM_FORMAT_BGRX4444:
  72. case DRM_FORMAT_ARGB4444:
  73. case DRM_FORMAT_ABGR4444:
  74. case DRM_FORMAT_RGBA4444:
  75. case DRM_FORMAT_BGRA4444:
  76. case DRM_FORMAT_XRGB1555:
  77. case DRM_FORMAT_XBGR1555:
  78. case DRM_FORMAT_RGBX5551:
  79. case DRM_FORMAT_BGRX5551:
  80. case DRM_FORMAT_ARGB1555:
  81. case DRM_FORMAT_ABGR1555:
  82. case DRM_FORMAT_RGBA5551:
  83. case DRM_FORMAT_BGRA5551:
  84. case DRM_FORMAT_RGB565:
  85. case DRM_FORMAT_BGR565:
  86. case DRM_FORMAT_RGB888:
  87. case DRM_FORMAT_BGR888:
  88. case DRM_FORMAT_XRGB8888:
  89. case DRM_FORMAT_XBGR8888:
  90. case DRM_FORMAT_RGBX8888:
  91. case DRM_FORMAT_BGRX8888:
  92. case DRM_FORMAT_ARGB8888:
  93. case DRM_FORMAT_ABGR8888:
  94. case DRM_FORMAT_RGBA8888:
  95. case DRM_FORMAT_BGRA8888:
  96. case DRM_FORMAT_XRGB2101010:
  97. case DRM_FORMAT_XBGR2101010:
  98. case DRM_FORMAT_RGBX1010102:
  99. case DRM_FORMAT_BGRX1010102:
  100. case DRM_FORMAT_ARGB2101010:
  101. case DRM_FORMAT_ABGR2101010:
  102. case DRM_FORMAT_RGBA1010102:
  103. case DRM_FORMAT_BGRA1010102:
  104. case DRM_FORMAT_YUYV:
  105. case DRM_FORMAT_YVYU:
  106. case DRM_FORMAT_UYVY:
  107. case DRM_FORMAT_VYUY:
  108. case DRM_FORMAT_AYUV:
  109. case DRM_FORMAT_NV12:
  110. case DRM_FORMAT_NV21:
  111. case DRM_FORMAT_NV16:
  112. case DRM_FORMAT_NV61:
  113. case DRM_FORMAT_NV24:
  114. case DRM_FORMAT_NV42:
  115. case DRM_FORMAT_YUV410:
  116. case DRM_FORMAT_YVU410:
  117. case DRM_FORMAT_YUV411:
  118. case DRM_FORMAT_YVU411:
  119. case DRM_FORMAT_YUV420:
  120. case DRM_FORMAT_YVU420:
  121. case DRM_FORMAT_YUV422:
  122. case DRM_FORMAT_YVU422:
  123. case DRM_FORMAT_YUV444:
  124. case DRM_FORMAT_YVU444:
  125. return 0;
  126. default:
  127. format_name = drm_get_format_name(r->pixel_format);
  128. DRM_DEBUG_KMS("invalid pixel format %s\n", format_name);
  129. kfree(format_name);
  130. return -EINVAL;
  131. }
  132. }
  133. static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
  134. {
  135. int ret, hsub, vsub, num_planes, i;
  136. ret = format_check(r);
  137. if (ret) {
  138. char *format_name = drm_get_format_name(r->pixel_format);
  139. DRM_DEBUG_KMS("bad framebuffer format %s\n", format_name);
  140. kfree(format_name);
  141. return ret;
  142. }
  143. hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
  144. vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
  145. num_planes = drm_format_num_planes(r->pixel_format);
  146. if (r->width == 0 || r->width % hsub) {
  147. DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
  148. return -EINVAL;
  149. }
  150. if (r->height == 0 || r->height % vsub) {
  151. DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
  152. return -EINVAL;
  153. }
  154. for (i = 0; i < num_planes; i++) {
  155. unsigned int width = r->width / (i != 0 ? hsub : 1);
  156. unsigned int height = r->height / (i != 0 ? vsub : 1);
  157. unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
  158. if (!r->handles[i]) {
  159. DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
  160. return -EINVAL;
  161. }
  162. if ((uint64_t) width * cpp > UINT_MAX)
  163. return -ERANGE;
  164. if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
  165. return -ERANGE;
  166. if (r->pitches[i] < width * cpp) {
  167. DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
  168. return -EINVAL;
  169. }
  170. if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
  171. DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
  172. r->modifier[i], i);
  173. return -EINVAL;
  174. }
  175. /* modifier specific checks: */
  176. switch (r->modifier[i]) {
  177. case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
  178. /* NOTE: the pitch restriction may be lifted later if it turns
  179. * out that no hw has this restriction:
  180. */
  181. if (r->pixel_format != DRM_FORMAT_NV12 ||
  182. width % 128 || height % 32 ||
  183. r->pitches[i] % 128) {
  184. DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
  185. return -EINVAL;
  186. }
  187. break;
  188. default:
  189. break;
  190. }
  191. }
  192. for (i = num_planes; i < 4; i++) {
  193. if (r->modifier[i]) {
  194. DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
  195. return -EINVAL;
  196. }
  197. /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
  198. if (!(r->flags & DRM_MODE_FB_MODIFIERS))
  199. continue;
  200. if (r->handles[i]) {
  201. DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
  202. return -EINVAL;
  203. }
  204. if (r->pitches[i]) {
  205. DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
  206. return -EINVAL;
  207. }
  208. if (r->offsets[i]) {
  209. DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
  210. return -EINVAL;
  211. }
  212. }
  213. return 0;
  214. }
  215. struct drm_framebuffer *
  216. drm_internal_framebuffer_create(struct drm_device *dev,
  217. const struct drm_mode_fb_cmd2 *r,
  218. struct drm_file *file_priv)
  219. {
  220. struct drm_mode_config *config = &dev->mode_config;
  221. struct drm_framebuffer *fb;
  222. int ret;
  223. if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
  224. DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
  225. return ERR_PTR(-EINVAL);
  226. }
  227. if ((config->min_width > r->width) || (r->width > config->max_width)) {
  228. DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
  229. r->width, config->min_width, config->max_width);
  230. return ERR_PTR(-EINVAL);
  231. }
  232. if ((config->min_height > r->height) || (r->height > config->max_height)) {
  233. DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
  234. r->height, config->min_height, config->max_height);
  235. return ERR_PTR(-EINVAL);
  236. }
  237. if (r->flags & DRM_MODE_FB_MODIFIERS &&
  238. !dev->mode_config.allow_fb_modifiers) {
  239. DRM_DEBUG_KMS("driver does not support fb modifiers\n");
  240. return ERR_PTR(-EINVAL);
  241. }
  242. ret = framebuffer_check(r);
  243. if (ret)
  244. return ERR_PTR(ret);
  245. fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
  246. if (IS_ERR(fb)) {
  247. DRM_DEBUG_KMS("could not create framebuffer\n");
  248. return fb;
  249. }
  250. return fb;
  251. }
  252. /**
  253. * drm_mode_addfb2 - add an FB to the graphics configuration
  254. * @dev: drm device for the ioctl
  255. * @data: data pointer for the ioctl
  256. * @file_priv: drm file for the ioctl call
  257. *
  258. * Add a new FB to the specified CRTC, given a user request with format. This is
  259. * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
  260. * and uses fourcc codes as pixel format specifiers.
  261. *
  262. * Called by the user via ioctl.
  263. *
  264. * Returns:
  265. * Zero on success, negative errno on failure.
  266. */
  267. int drm_mode_addfb2(struct drm_device *dev,
  268. void *data, struct drm_file *file_priv)
  269. {
  270. struct drm_mode_fb_cmd2 *r = data;
  271. struct drm_framebuffer *fb;
  272. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  273. return -EINVAL;
  274. fb = drm_internal_framebuffer_create(dev, r, file_priv);
  275. if (IS_ERR(fb))
  276. return PTR_ERR(fb);
  277. DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
  278. r->fb_id = fb->base.id;
  279. /* Transfer ownership to the filp for reaping on close */
  280. mutex_lock(&file_priv->fbs_lock);
  281. list_add(&fb->filp_head, &file_priv->fbs);
  282. mutex_unlock(&file_priv->fbs_lock);
  283. return 0;
  284. }
  285. struct drm_mode_rmfb_work {
  286. struct work_struct work;
  287. struct list_head fbs;
  288. };
  289. static void drm_mode_rmfb_work_fn(struct work_struct *w)
  290. {
  291. struct drm_mode_rmfb_work *arg = container_of(w, typeof(*arg), work);
  292. while (!list_empty(&arg->fbs)) {
  293. struct drm_framebuffer *fb =
  294. list_first_entry(&arg->fbs, typeof(*fb), filp_head);
  295. list_del_init(&fb->filp_head);
  296. drm_framebuffer_remove(fb);
  297. }
  298. }
  299. /**
  300. * drm_mode_rmfb - remove an FB from the configuration
  301. * @dev: drm device for the ioctl
  302. * @data: data pointer for the ioctl
  303. * @file_priv: drm file for the ioctl call
  304. *
  305. * Remove the FB specified by the user.
  306. *
  307. * Called by the user via ioctl.
  308. *
  309. * Returns:
  310. * Zero on success, negative errno on failure.
  311. */
  312. int drm_mode_rmfb(struct drm_device *dev,
  313. void *data, struct drm_file *file_priv)
  314. {
  315. struct drm_framebuffer *fb = NULL;
  316. struct drm_framebuffer *fbl = NULL;
  317. uint32_t *id = data;
  318. int found = 0;
  319. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  320. return -EINVAL;
  321. fb = drm_framebuffer_lookup(dev, *id);
  322. if (!fb)
  323. return -ENOENT;
  324. mutex_lock(&file_priv->fbs_lock);
  325. list_for_each_entry(fbl, &file_priv->fbs, filp_head)
  326. if (fb == fbl)
  327. found = 1;
  328. if (!found) {
  329. mutex_unlock(&file_priv->fbs_lock);
  330. goto fail_unref;
  331. }
  332. list_del_init(&fb->filp_head);
  333. mutex_unlock(&file_priv->fbs_lock);
  334. /* drop the reference we picked up in framebuffer lookup */
  335. drm_framebuffer_unreference(fb);
  336. /*
  337. * we now own the reference that was stored in the fbs list
  338. *
  339. * drm_framebuffer_remove may fail with -EINTR on pending signals,
  340. * so run this in a separate stack as there's no way to correctly
  341. * handle this after the fb is already removed from the lookup table.
  342. */
  343. if (drm_framebuffer_read_refcount(fb) > 1) {
  344. struct drm_mode_rmfb_work arg;
  345. INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
  346. INIT_LIST_HEAD(&arg.fbs);
  347. list_add_tail(&fb->filp_head, &arg.fbs);
  348. schedule_work(&arg.work);
  349. flush_work(&arg.work);
  350. destroy_work_on_stack(&arg.work);
  351. } else
  352. drm_framebuffer_unreference(fb);
  353. return 0;
  354. fail_unref:
  355. drm_framebuffer_unreference(fb);
  356. return -ENOENT;
  357. }
  358. /**
  359. * drm_mode_getfb - get FB info
  360. * @dev: drm device for the ioctl
  361. * @data: data pointer for the ioctl
  362. * @file_priv: drm file for the ioctl call
  363. *
  364. * Lookup the FB given its ID and return info about it.
  365. *
  366. * Called by the user via ioctl.
  367. *
  368. * Returns:
  369. * Zero on success, negative errno on failure.
  370. */
  371. int drm_mode_getfb(struct drm_device *dev,
  372. void *data, struct drm_file *file_priv)
  373. {
  374. struct drm_mode_fb_cmd *r = data;
  375. struct drm_framebuffer *fb;
  376. int ret;
  377. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  378. return -EINVAL;
  379. fb = drm_framebuffer_lookup(dev, r->fb_id);
  380. if (!fb)
  381. return -ENOENT;
  382. r->height = fb->height;
  383. r->width = fb->width;
  384. r->depth = fb->depth;
  385. r->bpp = fb->bits_per_pixel;
  386. r->pitch = fb->pitches[0];
  387. if (fb->funcs->create_handle) {
  388. if (drm_is_current_master(file_priv) || capable(CAP_SYS_ADMIN) ||
  389. drm_is_control_client(file_priv)) {
  390. ret = fb->funcs->create_handle(fb, file_priv,
  391. &r->handle);
  392. } else {
  393. /* GET_FB() is an unprivileged ioctl so we must not
  394. * return a buffer-handle to non-master processes! For
  395. * backwards-compatibility reasons, we cannot make
  396. * GET_FB() privileged, so just return an invalid handle
  397. * for non-masters. */
  398. r->handle = 0;
  399. ret = 0;
  400. }
  401. } else {
  402. ret = -ENODEV;
  403. }
  404. drm_framebuffer_unreference(fb);
  405. return ret;
  406. }
  407. /**
  408. * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
  409. * @dev: drm device for the ioctl
  410. * @data: data pointer for the ioctl
  411. * @file_priv: drm file for the ioctl call
  412. *
  413. * Lookup the FB and flush out the damaged area supplied by userspace as a clip
  414. * rectangle list. Generic userspace which does frontbuffer rendering must call
  415. * this ioctl to flush out the changes on manual-update display outputs, e.g.
  416. * usb display-link, mipi manual update panels or edp panel self refresh modes.
  417. *
  418. * Modesetting drivers which always update the frontbuffer do not need to
  419. * implement the corresponding ->dirty framebuffer callback.
  420. *
  421. * Called by the user via ioctl.
  422. *
  423. * Returns:
  424. * Zero on success, negative errno on failure.
  425. */
  426. int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
  427. void *data, struct drm_file *file_priv)
  428. {
  429. struct drm_clip_rect __user *clips_ptr;
  430. struct drm_clip_rect *clips = NULL;
  431. struct drm_mode_fb_dirty_cmd *r = data;
  432. struct drm_framebuffer *fb;
  433. unsigned flags;
  434. int num_clips;
  435. int ret;
  436. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  437. return -EINVAL;
  438. fb = drm_framebuffer_lookup(dev, r->fb_id);
  439. if (!fb)
  440. return -ENOENT;
  441. num_clips = r->num_clips;
  442. clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
  443. if (!num_clips != !clips_ptr) {
  444. ret = -EINVAL;
  445. goto out_err1;
  446. }
  447. flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
  448. /* If userspace annotates copy, clips must come in pairs */
  449. if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
  450. ret = -EINVAL;
  451. goto out_err1;
  452. }
  453. if (num_clips && clips_ptr) {
  454. if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
  455. ret = -EINVAL;
  456. goto out_err1;
  457. }
  458. clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
  459. if (!clips) {
  460. ret = -ENOMEM;
  461. goto out_err1;
  462. }
  463. ret = copy_from_user(clips, clips_ptr,
  464. num_clips * sizeof(*clips));
  465. if (ret) {
  466. ret = -EFAULT;
  467. goto out_err2;
  468. }
  469. }
  470. if (fb->funcs->dirty) {
  471. ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
  472. clips, num_clips);
  473. } else {
  474. ret = -ENOSYS;
  475. }
  476. out_err2:
  477. kfree(clips);
  478. out_err1:
  479. drm_framebuffer_unreference(fb);
  480. return ret;
  481. }
  482. /**
  483. * drm_fb_release - remove and free the FBs on this file
  484. * @priv: drm file for the ioctl
  485. *
  486. * Destroy all the FBs associated with @filp.
  487. *
  488. * Called by the user via ioctl.
  489. *
  490. * Returns:
  491. * Zero on success, negative errno on failure.
  492. */
  493. void drm_fb_release(struct drm_file *priv)
  494. {
  495. struct drm_framebuffer *fb, *tfb;
  496. struct drm_mode_rmfb_work arg;
  497. INIT_LIST_HEAD(&arg.fbs);
  498. /*
  499. * When the file gets released that means no one else can access the fb
  500. * list any more, so no need to grab fpriv->fbs_lock. And we need to
  501. * avoid upsetting lockdep since the universal cursor code adds a
  502. * framebuffer while holding mutex locks.
  503. *
  504. * Note that a real deadlock between fpriv->fbs_lock and the modeset
  505. * locks is impossible here since no one else but this function can get
  506. * at it any more.
  507. */
  508. list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
  509. if (drm_framebuffer_read_refcount(fb) > 1) {
  510. list_move_tail(&fb->filp_head, &arg.fbs);
  511. } else {
  512. list_del_init(&fb->filp_head);
  513. /* This drops the fpriv->fbs reference. */
  514. drm_framebuffer_unreference(fb);
  515. }
  516. }
  517. if (!list_empty(&arg.fbs)) {
  518. INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
  519. schedule_work(&arg.work);
  520. flush_work(&arg.work);
  521. destroy_work_on_stack(&arg.work);
  522. }
  523. }
  524. void drm_framebuffer_free(struct kref *kref)
  525. {
  526. struct drm_framebuffer *fb =
  527. container_of(kref, struct drm_framebuffer, base.refcount);
  528. struct drm_device *dev = fb->dev;
  529. /*
  530. * The lookup idr holds a weak reference, which has not necessarily been
  531. * removed at this point. Check for that.
  532. */
  533. drm_mode_object_unregister(dev, &fb->base);
  534. fb->funcs->destroy(fb);
  535. }
  536. /**
  537. * drm_framebuffer_init - initialize a framebuffer
  538. * @dev: DRM device
  539. * @fb: framebuffer to be initialized
  540. * @funcs: ... with these functions
  541. *
  542. * Allocates an ID for the framebuffer's parent mode object, sets its mode
  543. * functions & device file and adds it to the master fd list.
  544. *
  545. * IMPORTANT:
  546. * This functions publishes the fb and makes it available for concurrent access
  547. * by other users. Which means by this point the fb _must_ be fully set up -
  548. * since all the fb attributes are invariant over its lifetime, no further
  549. * locking but only correct reference counting is required.
  550. *
  551. * Returns:
  552. * Zero on success, error code on failure.
  553. */
  554. int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
  555. const struct drm_framebuffer_funcs *funcs)
  556. {
  557. int ret;
  558. INIT_LIST_HEAD(&fb->filp_head);
  559. fb->dev = dev;
  560. fb->funcs = funcs;
  561. ret = drm_mode_object_get_reg(dev, &fb->base, DRM_MODE_OBJECT_FB,
  562. false, drm_framebuffer_free);
  563. if (ret)
  564. goto out;
  565. mutex_lock(&dev->mode_config.fb_lock);
  566. dev->mode_config.num_fb++;
  567. list_add(&fb->head, &dev->mode_config.fb_list);
  568. mutex_unlock(&dev->mode_config.fb_lock);
  569. drm_mode_object_register(dev, &fb->base);
  570. out:
  571. return ret;
  572. }
  573. EXPORT_SYMBOL(drm_framebuffer_init);
  574. /**
  575. * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
  576. * @dev: drm device
  577. * @id: id of the fb object
  578. *
  579. * If successful, this grabs an additional reference to the framebuffer -
  580. * callers need to make sure to eventually unreference the returned framebuffer
  581. * again, using @drm_framebuffer_unreference.
  582. */
  583. struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
  584. uint32_t id)
  585. {
  586. struct drm_mode_object *obj;
  587. struct drm_framebuffer *fb = NULL;
  588. obj = __drm_mode_object_find(dev, id, DRM_MODE_OBJECT_FB);
  589. if (obj)
  590. fb = obj_to_fb(obj);
  591. return fb;
  592. }
  593. EXPORT_SYMBOL(drm_framebuffer_lookup);
  594. /**
  595. * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
  596. * @fb: fb to unregister
  597. *
  598. * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
  599. * those used for fbdev. Note that the caller must hold a reference of it's own,
  600. * i.e. the object may not be destroyed through this call (since it'll lead to a
  601. * locking inversion).
  602. */
  603. void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
  604. {
  605. struct drm_device *dev;
  606. if (!fb)
  607. return;
  608. dev = fb->dev;
  609. /* Mark fb as reaped and drop idr ref. */
  610. drm_mode_object_unregister(dev, &fb->base);
  611. }
  612. EXPORT_SYMBOL(drm_framebuffer_unregister_private);
  613. /**
  614. * drm_framebuffer_cleanup - remove a framebuffer object
  615. * @fb: framebuffer to remove
  616. *
  617. * Cleanup framebuffer. This function is intended to be used from the drivers
  618. * ->destroy callback. It can also be used to clean up driver private
  619. * framebuffers embedded into a larger structure.
  620. *
  621. * Note that this function does not remove the fb from active usuage - if it is
  622. * still used anywhere, hilarity can ensue since userspace could call getfb on
  623. * the id and get back -EINVAL. Obviously no concern at driver unload time.
  624. *
  625. * Also, the framebuffer will not be removed from the lookup idr - for
  626. * user-created framebuffers this will happen in in the rmfb ioctl. For
  627. * driver-private objects (e.g. for fbdev) drivers need to explicitly call
  628. * drm_framebuffer_unregister_private.
  629. */
  630. void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
  631. {
  632. struct drm_device *dev = fb->dev;
  633. mutex_lock(&dev->mode_config.fb_lock);
  634. list_del(&fb->head);
  635. dev->mode_config.num_fb--;
  636. mutex_unlock(&dev->mode_config.fb_lock);
  637. }
  638. EXPORT_SYMBOL(drm_framebuffer_cleanup);
  639. /**
  640. * drm_framebuffer_remove - remove and unreference a framebuffer object
  641. * @fb: framebuffer to remove
  642. *
  643. * Scans all the CRTCs and planes in @dev's mode_config. If they're
  644. * using @fb, removes it, setting it to NULL. Then drops the reference to the
  645. * passed-in framebuffer. Might take the modeset locks.
  646. *
  647. * Note that this function optimizes the cleanup away if the caller holds the
  648. * last reference to the framebuffer. It is also guaranteed to not take the
  649. * modeset locks in this case.
  650. */
  651. void drm_framebuffer_remove(struct drm_framebuffer *fb)
  652. {
  653. struct drm_device *dev;
  654. struct drm_crtc *crtc;
  655. struct drm_plane *plane;
  656. if (!fb)
  657. return;
  658. dev = fb->dev;
  659. WARN_ON(!list_empty(&fb->filp_head));
  660. /*
  661. * drm ABI mandates that we remove any deleted framebuffers from active
  662. * useage. But since most sane clients only remove framebuffers they no
  663. * longer need, try to optimize this away.
  664. *
  665. * Since we're holding a reference ourselves, observing a refcount of 1
  666. * means that we're the last holder and can skip it. Also, the refcount
  667. * can never increase from 1 again, so we don't need any barriers or
  668. * locks.
  669. *
  670. * Note that userspace could try to race with use and instate a new
  671. * usage _after_ we've cleared all current ones. End result will be an
  672. * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
  673. * in this manner.
  674. */
  675. if (drm_framebuffer_read_refcount(fb) > 1) {
  676. drm_modeset_lock_all(dev);
  677. /* remove from any CRTC */
  678. drm_for_each_crtc(crtc, dev) {
  679. if (crtc->primary->fb == fb) {
  680. /* should turn off the crtc */
  681. if (drm_crtc_force_disable(crtc))
  682. DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
  683. }
  684. }
  685. drm_for_each_plane(plane, dev) {
  686. if (plane->fb == fb)
  687. drm_plane_force_disable(plane);
  688. }
  689. drm_modeset_unlock_all(dev);
  690. }
  691. drm_framebuffer_unreference(fb);
  692. }
  693. EXPORT_SYMBOL(drm_framebuffer_remove);