mtk_drm_fb.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 <linux/dma-buf.h>
  18. #include <linux/reservation.h>
  19. #include "mtk_drm_drv.h"
  20. #include "mtk_drm_fb.h"
  21. #include "mtk_drm_gem.h"
  22. /*
  23. * mtk specific framebuffer structure.
  24. *
  25. * @fb: drm framebuffer object.
  26. * @gem_obj: array of gem objects.
  27. */
  28. struct mtk_drm_fb {
  29. struct drm_framebuffer base;
  30. /* For now we only support a single plane */
  31. struct drm_gem_object *gem_obj;
  32. };
  33. #define to_mtk_fb(x) container_of(x, struct mtk_drm_fb, base)
  34. struct drm_gem_object *mtk_fb_get_gem_obj(struct drm_framebuffer *fb)
  35. {
  36. struct mtk_drm_fb *mtk_fb = to_mtk_fb(fb);
  37. return mtk_fb->gem_obj;
  38. }
  39. static int mtk_drm_fb_create_handle(struct drm_framebuffer *fb,
  40. struct drm_file *file_priv,
  41. unsigned int *handle)
  42. {
  43. struct mtk_drm_fb *mtk_fb = to_mtk_fb(fb);
  44. return drm_gem_handle_create(file_priv, mtk_fb->gem_obj, handle);
  45. }
  46. static void mtk_drm_fb_destroy(struct drm_framebuffer *fb)
  47. {
  48. struct mtk_drm_fb *mtk_fb = to_mtk_fb(fb);
  49. drm_framebuffer_cleanup(fb);
  50. drm_gem_object_unreference_unlocked(mtk_fb->gem_obj);
  51. kfree(mtk_fb);
  52. }
  53. static const struct drm_framebuffer_funcs mtk_drm_fb_funcs = {
  54. .create_handle = mtk_drm_fb_create_handle,
  55. .destroy = mtk_drm_fb_destroy,
  56. };
  57. static struct mtk_drm_fb *mtk_drm_framebuffer_init(struct drm_device *dev,
  58. const struct drm_mode_fb_cmd2 *mode,
  59. struct drm_gem_object *obj)
  60. {
  61. struct mtk_drm_fb *mtk_fb;
  62. int ret;
  63. if (drm_format_num_planes(mode->pixel_format) != 1)
  64. return ERR_PTR(-EINVAL);
  65. mtk_fb = kzalloc(sizeof(*mtk_fb), GFP_KERNEL);
  66. if (!mtk_fb)
  67. return ERR_PTR(-ENOMEM);
  68. drm_helper_mode_fill_fb_struct(dev, &mtk_fb->base, mode);
  69. mtk_fb->gem_obj = obj;
  70. ret = drm_framebuffer_init(dev, &mtk_fb->base, &mtk_drm_fb_funcs);
  71. if (ret) {
  72. DRM_ERROR("failed to initialize framebuffer\n");
  73. kfree(mtk_fb);
  74. return ERR_PTR(ret);
  75. }
  76. return mtk_fb;
  77. }
  78. /*
  79. * Wait for any exclusive fence in fb's gem object's reservation object.
  80. *
  81. * Returns -ERESTARTSYS if interrupted, else 0.
  82. */
  83. int mtk_fb_wait(struct drm_framebuffer *fb)
  84. {
  85. struct drm_gem_object *gem;
  86. struct reservation_object *resv;
  87. long ret;
  88. if (!fb)
  89. return 0;
  90. gem = mtk_fb_get_gem_obj(fb);
  91. if (!gem || !gem->dma_buf || !gem->dma_buf->resv)
  92. return 0;
  93. resv = gem->dma_buf->resv;
  94. ret = reservation_object_wait_timeout_rcu(resv, false, true,
  95. MAX_SCHEDULE_TIMEOUT);
  96. /* MAX_SCHEDULE_TIMEOUT on success, -ERESTARTSYS if interrupted */
  97. if (WARN_ON(ret < 0))
  98. return ret;
  99. return 0;
  100. }
  101. struct drm_framebuffer *mtk_drm_mode_fb_create(struct drm_device *dev,
  102. struct drm_file *file,
  103. const struct drm_mode_fb_cmd2 *cmd)
  104. {
  105. struct mtk_drm_fb *mtk_fb;
  106. struct drm_gem_object *gem;
  107. unsigned int width = cmd->width;
  108. unsigned int height = cmd->height;
  109. unsigned int size, bpp;
  110. int ret;
  111. if (drm_format_num_planes(cmd->pixel_format) != 1)
  112. return ERR_PTR(-EINVAL);
  113. gem = drm_gem_object_lookup(file, cmd->handles[0]);
  114. if (!gem)
  115. return ERR_PTR(-ENOENT);
  116. bpp = drm_format_plane_cpp(cmd->pixel_format, 0);
  117. size = (height - 1) * cmd->pitches[0] + width * bpp;
  118. size += cmd->offsets[0];
  119. if (gem->size < size) {
  120. ret = -EINVAL;
  121. goto unreference;
  122. }
  123. mtk_fb = mtk_drm_framebuffer_init(dev, cmd, gem);
  124. if (IS_ERR(mtk_fb)) {
  125. ret = PTR_ERR(mtk_fb);
  126. goto unreference;
  127. }
  128. return &mtk_fb->base;
  129. unreference:
  130. drm_gem_object_unreference_unlocked(gem);
  131. return ERR_PTR(ret);
  132. }