drm_framebuffer.c 23 KB

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