drm_framebuffer.c 24 KB

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