exynos_drm_fb.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* exynos_drm_fb.c
  3. *
  4. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  5. * Authors:
  6. * Inki Dae <inki.dae@samsung.com>
  7. * Joonyoung Shim <jy0922.shim@samsung.com>
  8. * Seung-Woo Kim <sw0312.kim@samsung.com>
  9. */
  10. #include <drm/drm_atomic.h>
  11. #include <drm/drm_atomic_helper.h>
  12. #include <drm/drm_crtc.h>
  13. #include <drm/drm_framebuffer.h>
  14. #include <drm/drm_fourcc.h>
  15. #include <drm/drm_gem_framebuffer_helper.h>
  16. #include <drm/drm_print.h>
  17. #include <drm/drm_probe_helper.h>
  18. #include <drm/exynos_drm.h>
  19. #include "exynos_drm_crtc.h"
  20. #include "exynos_drm_drv.h"
  21. #include "exynos_drm_fb.h"
  22. #include "exynos_drm_fbdev.h"
  23. static int check_fb_gem_memory_type(struct drm_device *drm_dev,
  24. struct exynos_drm_gem *exynos_gem)
  25. {
  26. unsigned int flags;
  27. /*
  28. * if exynos drm driver supports iommu then framebuffer can use
  29. * all the buffer types.
  30. */
  31. if (is_drm_iommu_supported(drm_dev))
  32. return 0;
  33. flags = exynos_gem->flags;
  34. /*
  35. * Physically non-contiguous memory type for framebuffer is not
  36. * supported without IOMMU.
  37. */
  38. if (IS_NONCONTIG_BUFFER(flags)) {
  39. DRM_DEV_ERROR(drm_dev->dev,
  40. "Non-contiguous GEM memory is not supported.\n");
  41. return -EINVAL;
  42. }
  43. return 0;
  44. }
  45. static const struct drm_framebuffer_funcs exynos_drm_fb_funcs = {
  46. .destroy = drm_gem_fb_destroy,
  47. .create_handle = drm_gem_fb_create_handle,
  48. };
  49. struct drm_framebuffer *
  50. exynos_drm_framebuffer_init(struct drm_device *dev,
  51. const struct drm_format_info *info,
  52. const struct drm_mode_fb_cmd2 *mode_cmd,
  53. struct exynos_drm_gem **exynos_gem,
  54. int count)
  55. {
  56. struct drm_framebuffer *fb;
  57. int i;
  58. int ret;
  59. fb = kzalloc_obj(*fb);
  60. if (!fb)
  61. return ERR_PTR(-ENOMEM);
  62. for (i = 0; i < count; i++) {
  63. ret = check_fb_gem_memory_type(dev, exynos_gem[i]);
  64. if (ret < 0)
  65. goto err;
  66. fb->obj[i] = &exynos_gem[i]->base;
  67. }
  68. drm_helper_mode_fill_fb_struct(dev, fb, info, mode_cmd);
  69. ret = drm_framebuffer_init(dev, fb, &exynos_drm_fb_funcs);
  70. if (ret < 0) {
  71. DRM_DEV_ERROR(dev->dev,
  72. "failed to initialize framebuffer\n");
  73. goto err;
  74. }
  75. return fb;
  76. err:
  77. kfree(fb);
  78. return ERR_PTR(ret);
  79. }
  80. static struct drm_framebuffer *
  81. exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
  82. const struct drm_format_info *info,
  83. const struct drm_mode_fb_cmd2 *mode_cmd)
  84. {
  85. struct exynos_drm_gem *exynos_gem[MAX_FB_BUFFER];
  86. struct drm_framebuffer *fb;
  87. int i;
  88. int ret;
  89. for (i = 0; i < info->num_planes; i++) {
  90. unsigned int height = (i == 0) ? mode_cmd->height :
  91. DIV_ROUND_UP(mode_cmd->height, info->vsub);
  92. unsigned long size = height * mode_cmd->pitches[i] +
  93. mode_cmd->offsets[i];
  94. exynos_gem[i] = exynos_drm_gem_get(file_priv,
  95. mode_cmd->handles[i]);
  96. if (!exynos_gem[i]) {
  97. DRM_DEV_ERROR(dev->dev,
  98. "failed to lookup gem object\n");
  99. ret = -ENOENT;
  100. goto err;
  101. }
  102. if (size > exynos_gem[i]->size) {
  103. i++;
  104. ret = -EINVAL;
  105. goto err;
  106. }
  107. }
  108. fb = exynos_drm_framebuffer_init(dev, info, mode_cmd, exynos_gem, i);
  109. if (IS_ERR(fb)) {
  110. ret = PTR_ERR(fb);
  111. goto err;
  112. }
  113. return fb;
  114. err:
  115. while (i--)
  116. exynos_drm_gem_put(exynos_gem[i]);
  117. return ERR_PTR(ret);
  118. }
  119. dma_addr_t exynos_drm_fb_dma_addr(struct drm_framebuffer *fb, int index)
  120. {
  121. struct exynos_drm_gem *exynos_gem;
  122. if (WARN_ON_ONCE(index >= MAX_FB_BUFFER))
  123. return 0;
  124. exynos_gem = to_exynos_gem(fb->obj[index]);
  125. return exynos_gem->dma_addr + fb->offsets[index];
  126. }
  127. static struct drm_mode_config_helper_funcs exynos_drm_mode_config_helpers = {
  128. .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
  129. };
  130. static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
  131. .fb_create = exynos_user_fb_create,
  132. .atomic_check = drm_atomic_helper_check,
  133. .atomic_commit = drm_atomic_helper_commit,
  134. };
  135. void exynos_drm_mode_config_init(struct drm_device *dev)
  136. {
  137. dev->mode_config.min_width = 0;
  138. dev->mode_config.min_height = 0;
  139. /*
  140. * set max width and height as default value(4096x4096).
  141. * this value would be used to check framebuffer size limitation
  142. * at drm_mode_addfb().
  143. */
  144. dev->mode_config.max_width = 4096;
  145. dev->mode_config.max_height = 4096;
  146. dev->mode_config.funcs = &exynos_drm_mode_config_funcs;
  147. dev->mode_config.helper_private = &exynos_drm_mode_config_helpers;
  148. dev->mode_config.normalize_zpos = true;
  149. }