drm_framebuffer.c 30 KB

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