drm_framebuffer.c 22 KB

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