mtk_drm_fb.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2015 MediaTek Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <drm/drmP.h>
  14. #include <drm/drm_crtc_helper.h>
  15. #include <drm/drm_fb_helper.h>
  16. #include <drm/drm_gem.h>
  17. #include <drm/drm_gem_framebuffer_helper.h>
  18. #include <linux/dma-buf.h>
  19. #include <linux/reservation.h>
  20. #include "mtk_drm_drv.h"
  21. #include "mtk_drm_fb.h"
  22. #include "mtk_drm_gem.h"
  23. static const struct drm_framebuffer_funcs mtk_drm_fb_funcs = {
  24. .create_handle = drm_gem_fb_create_handle,
  25. .destroy = drm_gem_fb_destroy,
  26. };
  27. static struct drm_framebuffer *mtk_drm_framebuffer_init(struct drm_device *dev,
  28. const struct drm_mode_fb_cmd2 *mode,
  29. struct drm_gem_object *obj)
  30. {
  31. struct drm_framebuffer *fb;
  32. int ret;
  33. if (drm_format_num_planes(mode->pixel_format) != 1)
  34. return ERR_PTR(-EINVAL);
  35. fb = kzalloc(sizeof(*fb), GFP_KERNEL);
  36. if (!fb)
  37. return ERR_PTR(-ENOMEM);
  38. drm_helper_mode_fill_fb_struct(dev, fb, mode);
  39. fb->obj[0] = obj;
  40. ret = drm_framebuffer_init(dev, fb, &mtk_drm_fb_funcs);
  41. if (ret) {
  42. DRM_ERROR("failed to initialize framebuffer\n");
  43. kfree(fb);
  44. return ERR_PTR(ret);
  45. }
  46. return fb;
  47. }
  48. /*
  49. * Wait for any exclusive fence in fb's gem object's reservation object.
  50. *
  51. * Returns -ERESTARTSYS if interrupted, else 0.
  52. */
  53. int mtk_fb_wait(struct drm_framebuffer *fb)
  54. {
  55. struct drm_gem_object *gem;
  56. struct reservation_object *resv;
  57. long ret;
  58. if (!fb)
  59. return 0;
  60. gem = fb->obj[0];
  61. if (!gem || !gem->dma_buf || !gem->dma_buf->resv)
  62. return 0;
  63. resv = gem->dma_buf->resv;
  64. ret = reservation_object_wait_timeout_rcu(resv, false, true,
  65. MAX_SCHEDULE_TIMEOUT);
  66. /* MAX_SCHEDULE_TIMEOUT on success, -ERESTARTSYS if interrupted */
  67. if (WARN_ON(ret < 0))
  68. return ret;
  69. return 0;
  70. }
  71. struct drm_framebuffer *mtk_drm_mode_fb_create(struct drm_device *dev,
  72. struct drm_file *file,
  73. const struct drm_mode_fb_cmd2 *cmd)
  74. {
  75. struct drm_framebuffer *fb;
  76. struct drm_gem_object *gem;
  77. unsigned int width = cmd->width;
  78. unsigned int height = cmd->height;
  79. unsigned int size, bpp;
  80. int ret;
  81. if (drm_format_num_planes(cmd->pixel_format) != 1)
  82. return ERR_PTR(-EINVAL);
  83. gem = drm_gem_object_lookup(file, cmd->handles[0]);
  84. if (!gem)
  85. return ERR_PTR(-ENOENT);
  86. bpp = drm_format_plane_cpp(cmd->pixel_format, 0);
  87. size = (height - 1) * cmd->pitches[0] + width * bpp;
  88. size += cmd->offsets[0];
  89. if (gem->size < size) {
  90. ret = -EINVAL;
  91. goto unreference;
  92. }
  93. fb = mtk_drm_framebuffer_init(dev, cmd, gem);
  94. if (IS_ERR(fb)) {
  95. ret = PTR_ERR(fb);
  96. goto unreference;
  97. }
  98. return fb;
  99. unreference:
  100. drm_gem_object_put_unlocked(gem);
  101. return ERR_PTR(ret);
  102. }