drm_framebuffer.c 24 KB

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