drm_framebuffer.c 29 KB

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