drm_framebuffer.c 29 KB

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