drm_framebuffer.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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. !drm_is_control_client(file_priv)) {
  416. r->handle = 0;
  417. ret = 0;
  418. goto out;
  419. }
  420. ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
  421. out:
  422. drm_framebuffer_put(fb);
  423. return ret;
  424. }
  425. /**
  426. * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
  427. * @dev: drm device for the ioctl
  428. * @data: data pointer for the ioctl
  429. * @file_priv: drm file for the ioctl call
  430. *
  431. * Lookup the FB and flush out the damaged area supplied by userspace as a clip
  432. * rectangle list. Generic userspace which does frontbuffer rendering must call
  433. * this ioctl to flush out the changes on manual-update display outputs, e.g.
  434. * usb display-link, mipi manual update panels or edp panel self refresh modes.
  435. *
  436. * Modesetting drivers which always update the frontbuffer do not need to
  437. * implement the corresponding &drm_framebuffer_funcs.dirty callback.
  438. *
  439. * Called by the user via ioctl.
  440. *
  441. * Returns:
  442. * Zero on success, negative errno on failure.
  443. */
  444. int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
  445. void *data, struct drm_file *file_priv)
  446. {
  447. struct drm_clip_rect __user *clips_ptr;
  448. struct drm_clip_rect *clips = NULL;
  449. struct drm_mode_fb_dirty_cmd *r = data;
  450. struct drm_framebuffer *fb;
  451. unsigned flags;
  452. int num_clips;
  453. int ret;
  454. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  455. return -EINVAL;
  456. fb = drm_framebuffer_lookup(dev, file_priv, r->fb_id);
  457. if (!fb)
  458. return -ENOENT;
  459. num_clips = r->num_clips;
  460. clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
  461. if (!num_clips != !clips_ptr) {
  462. ret = -EINVAL;
  463. goto out_err1;
  464. }
  465. flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
  466. /* If userspace annotates copy, clips must come in pairs */
  467. if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
  468. ret = -EINVAL;
  469. goto out_err1;
  470. }
  471. if (num_clips && clips_ptr) {
  472. if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
  473. ret = -EINVAL;
  474. goto out_err1;
  475. }
  476. clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
  477. if (!clips) {
  478. ret = -ENOMEM;
  479. goto out_err1;
  480. }
  481. ret = copy_from_user(clips, clips_ptr,
  482. num_clips * sizeof(*clips));
  483. if (ret) {
  484. ret = -EFAULT;
  485. goto out_err2;
  486. }
  487. }
  488. if (fb->funcs->dirty) {
  489. ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
  490. clips, num_clips);
  491. } else {
  492. ret = -ENOSYS;
  493. }
  494. out_err2:
  495. kfree(clips);
  496. out_err1:
  497. drm_framebuffer_put(fb);
  498. return ret;
  499. }
  500. /**
  501. * drm_fb_release - remove and free the FBs on this file
  502. * @priv: drm file for the ioctl
  503. *
  504. * Destroy all the FBs associated with @filp.
  505. *
  506. * Called by the user via ioctl.
  507. *
  508. * Returns:
  509. * Zero on success, negative errno on failure.
  510. */
  511. void drm_fb_release(struct drm_file *priv)
  512. {
  513. struct drm_framebuffer *fb, *tfb;
  514. struct drm_mode_rmfb_work arg;
  515. INIT_LIST_HEAD(&arg.fbs);
  516. /*
  517. * When the file gets released that means no one else can access the fb
  518. * list any more, so no need to grab fpriv->fbs_lock. And we need to
  519. * avoid upsetting lockdep since the universal cursor code adds a
  520. * framebuffer while holding mutex locks.
  521. *
  522. * Note that a real deadlock between fpriv->fbs_lock and the modeset
  523. * locks is impossible here since no one else but this function can get
  524. * at it any more.
  525. */
  526. list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
  527. if (drm_framebuffer_read_refcount(fb) > 1) {
  528. list_move_tail(&fb->filp_head, &arg.fbs);
  529. } else {
  530. list_del_init(&fb->filp_head);
  531. /* This drops the fpriv->fbs reference. */
  532. drm_framebuffer_put(fb);
  533. }
  534. }
  535. if (!list_empty(&arg.fbs)) {
  536. INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
  537. schedule_work(&arg.work);
  538. flush_work(&arg.work);
  539. destroy_work_on_stack(&arg.work);
  540. }
  541. }
  542. void drm_framebuffer_free(struct kref *kref)
  543. {
  544. struct drm_framebuffer *fb =
  545. container_of(kref, struct drm_framebuffer, base.refcount);
  546. struct drm_device *dev = fb->dev;
  547. /*
  548. * The lookup idr holds a weak reference, which has not necessarily been
  549. * removed at this point. Check for that.
  550. */
  551. drm_mode_object_unregister(dev, &fb->base);
  552. fb->funcs->destroy(fb);
  553. }
  554. /**
  555. * drm_framebuffer_init - initialize a framebuffer
  556. * @dev: DRM device
  557. * @fb: framebuffer to be initialized
  558. * @funcs: ... with these functions
  559. *
  560. * Allocates an ID for the framebuffer's parent mode object, sets its mode
  561. * functions & device file and adds it to the master fd list.
  562. *
  563. * IMPORTANT:
  564. * This functions publishes the fb and makes it available for concurrent access
  565. * by other users. Which means by this point the fb _must_ be fully set up -
  566. * since all the fb attributes are invariant over its lifetime, no further
  567. * locking but only correct reference counting is required.
  568. *
  569. * Returns:
  570. * Zero on success, error code on failure.
  571. */
  572. int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
  573. const struct drm_framebuffer_funcs *funcs)
  574. {
  575. int ret;
  576. if (WARN_ON_ONCE(fb->dev != dev || !fb->format))
  577. return -EINVAL;
  578. INIT_LIST_HEAD(&fb->filp_head);
  579. fb->funcs = funcs;
  580. strcpy(fb->comm, current->comm);
  581. ret = __drm_mode_object_add(dev, &fb->base, DRM_MODE_OBJECT_FB,
  582. false, drm_framebuffer_free);
  583. if (ret)
  584. goto out;
  585. mutex_lock(&dev->mode_config.fb_lock);
  586. dev->mode_config.num_fb++;
  587. list_add(&fb->head, &dev->mode_config.fb_list);
  588. mutex_unlock(&dev->mode_config.fb_lock);
  589. drm_mode_object_register(dev, &fb->base);
  590. out:
  591. return ret;
  592. }
  593. EXPORT_SYMBOL(drm_framebuffer_init);
  594. /**
  595. * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
  596. * @dev: drm device
  597. * @file_priv: drm file to check for lease against.
  598. * @id: id of the fb object
  599. *
  600. * If successful, this grabs an additional reference to the framebuffer -
  601. * callers need to make sure to eventually unreference the returned framebuffer
  602. * again, using drm_framebuffer_put().
  603. */
  604. struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
  605. struct drm_file *file_priv,
  606. uint32_t id)
  607. {
  608. struct drm_mode_object *obj;
  609. struct drm_framebuffer *fb = NULL;
  610. obj = __drm_mode_object_find(dev, file_priv, id, DRM_MODE_OBJECT_FB);
  611. if (obj)
  612. fb = obj_to_fb(obj);
  613. return fb;
  614. }
  615. EXPORT_SYMBOL(drm_framebuffer_lookup);
  616. /**
  617. * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
  618. * @fb: fb to unregister
  619. *
  620. * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
  621. * those used for fbdev. Note that the caller must hold a reference of it's own,
  622. * i.e. the object may not be destroyed through this call (since it'll lead to a
  623. * locking inversion).
  624. *
  625. * NOTE: This function is deprecated. For driver-private framebuffers it is not
  626. * recommended to embed a framebuffer struct info fbdev struct, instead, a
  627. * framebuffer pointer is preferred and drm_framebuffer_put() should be called
  628. * when the framebuffer is to be cleaned up.
  629. */
  630. void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
  631. {
  632. struct drm_device *dev;
  633. if (!fb)
  634. return;
  635. dev = fb->dev;
  636. /* Mark fb as reaped and drop idr ref. */
  637. drm_mode_object_unregister(dev, &fb->base);
  638. }
  639. EXPORT_SYMBOL(drm_framebuffer_unregister_private);
  640. /**
  641. * drm_framebuffer_cleanup - remove a framebuffer object
  642. * @fb: framebuffer to remove
  643. *
  644. * Cleanup framebuffer. This function is intended to be used from the drivers
  645. * &drm_framebuffer_funcs.destroy callback. It can also be used to clean up
  646. * driver private framebuffers embedded into a larger structure.
  647. *
  648. * Note that this function does not remove the fb from active usage - if it is
  649. * still used anywhere, hilarity can ensue since userspace could call getfb on
  650. * the id and get back -EINVAL. Obviously no concern at driver unload time.
  651. *
  652. * Also, the framebuffer will not be removed from the lookup idr - for
  653. * user-created framebuffers this will happen in in the rmfb ioctl. For
  654. * driver-private objects (e.g. for fbdev) drivers need to explicitly call
  655. * drm_framebuffer_unregister_private.
  656. */
  657. void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
  658. {
  659. struct drm_device *dev = fb->dev;
  660. mutex_lock(&dev->mode_config.fb_lock);
  661. list_del(&fb->head);
  662. dev->mode_config.num_fb--;
  663. mutex_unlock(&dev->mode_config.fb_lock);
  664. }
  665. EXPORT_SYMBOL(drm_framebuffer_cleanup);
  666. static int atomic_remove_fb(struct drm_framebuffer *fb)
  667. {
  668. struct drm_modeset_acquire_ctx ctx;
  669. struct drm_device *dev = fb->dev;
  670. struct drm_atomic_state *state;
  671. struct drm_plane *plane;
  672. struct drm_connector *conn;
  673. struct drm_connector_state *conn_state;
  674. int i, ret;
  675. unsigned plane_mask;
  676. bool disable_crtcs = false;
  677. retry_disable:
  678. drm_modeset_acquire_init(&ctx, 0);
  679. state = drm_atomic_state_alloc(dev);
  680. if (!state) {
  681. ret = -ENOMEM;
  682. goto out;
  683. }
  684. state->acquire_ctx = &ctx;
  685. retry:
  686. plane_mask = 0;
  687. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  688. if (ret)
  689. goto unlock;
  690. drm_for_each_plane(plane, dev) {
  691. struct drm_plane_state *plane_state;
  692. if (plane->state->fb != fb)
  693. continue;
  694. plane_state = drm_atomic_get_plane_state(state, plane);
  695. if (IS_ERR(plane_state)) {
  696. ret = PTR_ERR(plane_state);
  697. goto unlock;
  698. }
  699. if (disable_crtcs && plane_state->crtc->primary == plane) {
  700. struct drm_crtc_state *crtc_state;
  701. crtc_state = drm_atomic_get_existing_crtc_state(state, plane_state->crtc);
  702. ret = drm_atomic_add_affected_connectors(state, plane_state->crtc);
  703. if (ret)
  704. goto unlock;
  705. crtc_state->active = false;
  706. ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
  707. if (ret)
  708. goto unlock;
  709. }
  710. drm_atomic_set_fb_for_plane(plane_state, NULL);
  711. ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
  712. if (ret)
  713. goto unlock;
  714. plane_mask |= BIT(drm_plane_index(plane));
  715. plane->old_fb = plane->fb;
  716. }
  717. /* This list is only filled when disable_crtcs is set. */
  718. for_each_new_connector_in_state(state, conn, conn_state, i) {
  719. ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
  720. if (ret)
  721. goto unlock;
  722. }
  723. if (plane_mask)
  724. ret = drm_atomic_commit(state);
  725. unlock:
  726. if (plane_mask)
  727. drm_atomic_clean_old_fb(dev, plane_mask, ret);
  728. if (ret == -EDEADLK) {
  729. drm_atomic_state_clear(state);
  730. drm_modeset_backoff(&ctx);
  731. goto retry;
  732. }
  733. drm_atomic_state_put(state);
  734. out:
  735. drm_modeset_drop_locks(&ctx);
  736. drm_modeset_acquire_fini(&ctx);
  737. if (ret == -EINVAL && !disable_crtcs) {
  738. disable_crtcs = true;
  739. goto retry_disable;
  740. }
  741. return ret;
  742. }
  743. static void legacy_remove_fb(struct drm_framebuffer *fb)
  744. {
  745. struct drm_device *dev = fb->dev;
  746. struct drm_crtc *crtc;
  747. struct drm_plane *plane;
  748. drm_modeset_lock_all(dev);
  749. /* remove from any CRTC */
  750. drm_for_each_crtc(crtc, dev) {
  751. if (crtc->primary->fb == fb) {
  752. /* should turn off the crtc */
  753. if (drm_crtc_force_disable(crtc))
  754. DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
  755. }
  756. }
  757. drm_for_each_plane(plane, dev) {
  758. if (plane->fb == fb)
  759. drm_plane_force_disable(plane);
  760. }
  761. drm_modeset_unlock_all(dev);
  762. }
  763. /**
  764. * drm_framebuffer_remove - remove and unreference a framebuffer object
  765. * @fb: framebuffer to remove
  766. *
  767. * Scans all the CRTCs and planes in @dev's mode_config. If they're
  768. * using @fb, removes it, setting it to NULL. Then drops the reference to the
  769. * passed-in framebuffer. Might take the modeset locks.
  770. *
  771. * Note that this function optimizes the cleanup away if the caller holds the
  772. * last reference to the framebuffer. It is also guaranteed to not take the
  773. * modeset locks in this case.
  774. */
  775. void drm_framebuffer_remove(struct drm_framebuffer *fb)
  776. {
  777. struct drm_device *dev;
  778. if (!fb)
  779. return;
  780. dev = fb->dev;
  781. WARN_ON(!list_empty(&fb->filp_head));
  782. /*
  783. * drm ABI mandates that we remove any deleted framebuffers from active
  784. * useage. But since most sane clients only remove framebuffers they no
  785. * longer need, try to optimize this away.
  786. *
  787. * Since we're holding a reference ourselves, observing a refcount of 1
  788. * means that we're the last holder and can skip it. Also, the refcount
  789. * can never increase from 1 again, so we don't need any barriers or
  790. * locks.
  791. *
  792. * Note that userspace could try to race with use and instate a new
  793. * usage _after_ we've cleared all current ones. End result will be an
  794. * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
  795. * in this manner.
  796. */
  797. if (drm_framebuffer_read_refcount(fb) > 1) {
  798. if (drm_drv_uses_atomic_modeset(dev)) {
  799. int ret = atomic_remove_fb(fb);
  800. WARN(ret, "atomic remove_fb failed with %i\n", ret);
  801. } else
  802. legacy_remove_fb(fb);
  803. }
  804. drm_framebuffer_put(fb);
  805. }
  806. EXPORT_SYMBOL(drm_framebuffer_remove);
  807. /**
  808. * drm_framebuffer_plane_width - width of the plane given the first plane
  809. * @width: width of the first plane
  810. * @fb: the framebuffer
  811. * @plane: plane index
  812. *
  813. * Returns:
  814. * The width of @plane, given that the width of the first plane is @width.
  815. */
  816. int drm_framebuffer_plane_width(int width,
  817. const struct drm_framebuffer *fb, int plane)
  818. {
  819. if (plane >= fb->format->num_planes)
  820. return 0;
  821. return fb_plane_width(width, fb->format, plane);
  822. }
  823. EXPORT_SYMBOL(drm_framebuffer_plane_width);
  824. /**
  825. * drm_framebuffer_plane_height - height of the plane given the first plane
  826. * @height: height of the first plane
  827. * @fb: the framebuffer
  828. * @plane: plane index
  829. *
  830. * Returns:
  831. * The height of @plane, given that the height of the first plane is @height.
  832. */
  833. int drm_framebuffer_plane_height(int height,
  834. const struct drm_framebuffer *fb, int plane)
  835. {
  836. if (plane >= fb->format->num_planes)
  837. return 0;
  838. return fb_plane_height(height, fb->format, plane);
  839. }
  840. EXPORT_SYMBOL(drm_framebuffer_plane_height);
  841. void drm_framebuffer_print_info(struct drm_printer *p, unsigned int indent,
  842. const struct drm_framebuffer *fb)
  843. {
  844. struct drm_format_name_buf format_name;
  845. unsigned int i;
  846. drm_printf_indent(p, indent, "allocated by = %s\n", fb->comm);
  847. drm_printf_indent(p, indent, "refcount=%u\n",
  848. drm_framebuffer_read_refcount(fb));
  849. drm_printf_indent(p, indent, "format=%s\n",
  850. drm_get_format_name(fb->format->format, &format_name));
  851. drm_printf_indent(p, indent, "modifier=0x%llx\n", fb->modifier);
  852. drm_printf_indent(p, indent, "size=%ux%u\n", fb->width, fb->height);
  853. drm_printf_indent(p, indent, "layers:\n");
  854. for (i = 0; i < fb->format->num_planes; i++) {
  855. drm_printf_indent(p, indent + 1, "size[%u]=%dx%d\n", i,
  856. drm_framebuffer_plane_width(fb->width, fb, i),
  857. drm_framebuffer_plane_height(fb->height, fb, i));
  858. drm_printf_indent(p, indent + 1, "pitch[%u]=%u\n", i, fb->pitches[i]);
  859. drm_printf_indent(p, indent + 1, "offset[%u]=%u\n", i, fb->offsets[i]);
  860. drm_printf_indent(p, indent + 1, "obj[%u]:%s\n", i,
  861. fb->obj[i] ? "" : "(null)");
  862. if (fb->obj[i])
  863. drm_gem_print_info(p, indent + 2, fb->obj[i]);
  864. }
  865. }
  866. #ifdef CONFIG_DEBUG_FS
  867. static int drm_framebuffer_info(struct seq_file *m, void *data)
  868. {
  869. struct drm_info_node *node = m->private;
  870. struct drm_device *dev = node->minor->dev;
  871. struct drm_printer p = drm_seq_file_printer(m);
  872. struct drm_framebuffer *fb;
  873. mutex_lock(&dev->mode_config.fb_lock);
  874. drm_for_each_fb(fb, dev) {
  875. drm_printf(&p, "framebuffer[%u]:\n", fb->base.id);
  876. drm_framebuffer_print_info(&p, 1, fb);
  877. }
  878. mutex_unlock(&dev->mode_config.fb_lock);
  879. return 0;
  880. }
  881. static const struct drm_info_list drm_framebuffer_debugfs_list[] = {
  882. { "framebuffer", drm_framebuffer_info, 0 },
  883. };
  884. int drm_framebuffer_debugfs_init(struct drm_minor *minor)
  885. {
  886. return drm_debugfs_create_files(drm_framebuffer_debugfs_list,
  887. ARRAY_SIZE(drm_framebuffer_debugfs_list),
  888. minor->debugfs_root, minor);
  889. }
  890. #endif