exynos_drm_fb.c 4.3 KB

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