drm_framebuffer.c 22 KB

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