drm_framebuffer.c 29 KB

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