drm_framebuffer.c 29 KB

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