drm_framebuffer.c 26 KB

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