exynos_drm_fb.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* exynos_drm_fb.c
  2. *
  3. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  4. * Authors:
  5. * Inki Dae <inki.dae@samsung.com>
  6. * Joonyoung Shim <jy0922.shim@samsung.com>
  7. * Seung-Woo Kim <sw0312.kim@samsung.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. */
  14. #include <drm/drmP.h>
  15. #include <drm/drm_crtc.h>
  16. #include <drm/drm_crtc_helper.h>
  17. #include <drm/drm_fb_helper.h>
  18. #include <drm/drm_atomic.h>
  19. #include <drm/drm_atomic_helper.h>
  20. #include <drm/drm_gem_framebuffer_helper.h>
  21. #include <uapi/drm/exynos_drm.h>
  22. #include "exynos_drm_drv.h"
  23. #include "exynos_drm_fb.h"
  24. #include "exynos_drm_fbdev.h"
  25. #include "exynos_drm_iommu.h"
  26. #include "exynos_drm_crtc.h"
  27. static int check_fb_gem_memory_type(struct drm_device *drm_dev,
  28. struct exynos_drm_gem *exynos_gem)
  29. {
  30. unsigned int flags;
  31. /*
  32. * if exynos drm driver supports iommu then framebuffer can use
  33. * all the buffer types.
  34. */
  35. if (is_drm_iommu_supported(drm_dev))
  36. return 0;
  37. flags = exynos_gem->flags;
  38. /*
  39. * Physically non-contiguous memory type for framebuffer is not
  40. * supported without IOMMU.
  41. */
  42. if (IS_NONCONTIG_BUFFER(flags)) {
  43. DRM_ERROR("Non-contiguous GEM memory is not supported.\n");
  44. return -EINVAL;
  45. }
  46. return 0;
  47. }
  48. static const struct drm_framebuffer_funcs exynos_drm_fb_funcs = {
  49. .destroy = drm_gem_fb_destroy,
  50. .create_handle = drm_gem_fb_create_handle,
  51. };
  52. struct drm_framebuffer *
  53. exynos_drm_framebuffer_init(struct drm_device *dev,
  54. const struct drm_mode_fb_cmd2 *mode_cmd,
  55. struct exynos_drm_gem **exynos_gem,
  56. int count)
  57. {
  58. struct drm_framebuffer *fb;
  59. int i;
  60. int ret;
  61. fb = kzalloc(sizeof(*fb), GFP_KERNEL);
  62. if (!fb)
  63. return ERR_PTR(-ENOMEM);
  64. for (i = 0; i < count; i++) {
  65. ret = check_fb_gem_memory_type(dev, exynos_gem[i]);
  66. if (ret < 0)
  67. goto err;
  68. fb->obj[i] = &exynos_gem[i]->base;
  69. }
  70. drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
  71. ret = drm_framebuffer_init(dev, fb, &exynos_drm_fb_funcs);
  72. if (ret < 0) {
  73. DRM_ERROR("failed to initialize framebuffer\n");
  74. goto err;
  75. }
  76. return fb;
  77. err:
  78. kfree(fb);
  79. return ERR_PTR(ret);
  80. }
  81. static struct drm_framebuffer *
  82. exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
  83. const struct drm_mode_fb_cmd2 *mode_cmd)
  84. {
  85. const struct drm_format_info *info = drm_get_format_info(dev, mode_cmd);
  86. struct exynos_drm_gem *exynos_gem[MAX_FB_BUFFER];
  87. struct drm_framebuffer *fb;
  88. int i;
  89. int ret;
  90. for (i = 0; i < info->num_planes; i++) {
  91. unsigned int height = (i == 0) ? mode_cmd->height :
  92. DIV_ROUND_UP(mode_cmd->height, info->vsub);
  93. unsigned long size = height * mode_cmd->pitches[i] +
  94. mode_cmd->offsets[i];
  95. exynos_gem[i] = exynos_drm_gem_get(file_priv,
  96. mode_cmd->handles[i]);
  97. if (!exynos_gem[i]) {
  98. DRM_ERROR("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, 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. .output_poll_changed = drm_fb_helper_output_poll_changed,
  133. .atomic_check = drm_atomic_helper_check,
  134. .atomic_commit = drm_atomic_helper_commit,
  135. };
  136. void exynos_drm_mode_config_init(struct drm_device *dev)
  137. {
  138. dev->mode_config.min_width = 0;
  139. dev->mode_config.min_height = 0;
  140. /*
  141. * set max width and height as default value(4096x4096).
  142. * this value would be used to check framebuffer size limitation
  143. * at drm_mode_addfb().
  144. */
  145. dev->mode_config.max_width = 4096;
  146. dev->mode_config.max_height = 4096;
  147. dev->mode_config.funcs = &exynos_drm_mode_config_funcs;
  148. dev->mode_config.helper_private = &exynos_drm_mode_config_helpers;
  149. dev->mode_config.allow_fb_modifiers = true;
  150. dev->mode_config.normalize_zpos = true;
  151. }