drm_framebuffer.c 24 KB

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