drm_framebuffer.c 30 KB

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