msm_fb.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (C) 2013 Red Hat
  3. * Author: Rob Clark <robdclark@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "msm_drv.h"
  18. #include "drm_crtc.h"
  19. #include "drm_crtc_helper.h"
  20. struct msm_framebuffer {
  21. struct drm_framebuffer base;
  22. const struct msm_format *format;
  23. struct drm_gem_object *planes[2];
  24. };
  25. #define to_msm_framebuffer(x) container_of(x, struct msm_framebuffer, base)
  26. static int msm_framebuffer_create_handle(struct drm_framebuffer *fb,
  27. struct drm_file *file_priv,
  28. unsigned int *handle)
  29. {
  30. struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
  31. return drm_gem_handle_create(file_priv,
  32. msm_fb->planes[0], handle);
  33. }
  34. static void msm_framebuffer_destroy(struct drm_framebuffer *fb)
  35. {
  36. struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
  37. int i, n = drm_format_num_planes(fb->pixel_format);
  38. DBG("destroy: FB ID: %d (%p)", fb->base.id, fb);
  39. drm_framebuffer_cleanup(fb);
  40. for (i = 0; i < n; i++) {
  41. struct drm_gem_object *bo = msm_fb->planes[i];
  42. if (bo)
  43. drm_gem_object_unreference_unlocked(bo);
  44. }
  45. kfree(msm_fb);
  46. }
  47. static int msm_framebuffer_dirty(struct drm_framebuffer *fb,
  48. struct drm_file *file_priv, unsigned flags, unsigned color,
  49. struct drm_clip_rect *clips, unsigned num_clips)
  50. {
  51. return 0;
  52. }
  53. static const struct drm_framebuffer_funcs msm_framebuffer_funcs = {
  54. .create_handle = msm_framebuffer_create_handle,
  55. .destroy = msm_framebuffer_destroy,
  56. .dirty = msm_framebuffer_dirty,
  57. };
  58. #ifdef CONFIG_DEBUG_FS
  59. void msm_framebuffer_describe(struct drm_framebuffer *fb, struct seq_file *m)
  60. {
  61. struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
  62. int i, n = drm_format_num_planes(fb->pixel_format);
  63. seq_printf(m, "fb: %dx%d@%4.4s (%2d, ID:%d)\n",
  64. fb->width, fb->height, (char *)&fb->pixel_format,
  65. fb->refcount.refcount.counter, fb->base.id);
  66. for (i = 0; i < n; i++) {
  67. seq_printf(m, " %d: offset=%d pitch=%d, obj: ",
  68. i, fb->offsets[i], fb->pitches[i]);
  69. msm_gem_describe(msm_fb->planes[i], m);
  70. }
  71. }
  72. #endif
  73. struct drm_gem_object *msm_framebuffer_bo(struct drm_framebuffer *fb, int plane)
  74. {
  75. struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
  76. return msm_fb->planes[plane];
  77. }
  78. const struct msm_format *msm_framebuffer_format(struct drm_framebuffer *fb)
  79. {
  80. struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
  81. return msm_fb->format;
  82. }
  83. struct drm_framebuffer *msm_framebuffer_create(struct drm_device *dev,
  84. struct drm_file *file, struct drm_mode_fb_cmd2 *mode_cmd)
  85. {
  86. struct drm_gem_object *bos[4] = {0};
  87. struct drm_framebuffer *fb;
  88. int ret, i, n = drm_format_num_planes(mode_cmd->pixel_format);
  89. for (i = 0; i < n; i++) {
  90. bos[i] = drm_gem_object_lookup(dev, file,
  91. mode_cmd->handles[i]);
  92. if (!bos[i]) {
  93. ret = -ENXIO;
  94. goto out_unref;
  95. }
  96. }
  97. fb = msm_framebuffer_init(dev, mode_cmd, bos);
  98. if (IS_ERR(fb)) {
  99. ret = PTR_ERR(fb);
  100. goto out_unref;
  101. }
  102. return fb;
  103. out_unref:
  104. for (i = 0; i < n; i++)
  105. drm_gem_object_unreference_unlocked(bos[i]);
  106. return ERR_PTR(ret);
  107. }
  108. struct drm_framebuffer *msm_framebuffer_init(struct drm_device *dev,
  109. struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos)
  110. {
  111. struct msm_drm_private *priv = dev->dev_private;
  112. struct msm_kms *kms = priv->kms;
  113. struct msm_framebuffer *msm_fb;
  114. struct drm_framebuffer *fb = NULL;
  115. const struct msm_format *format;
  116. int ret, i, n;
  117. unsigned int hsub, vsub;
  118. DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%4.4s)",
  119. dev, mode_cmd, mode_cmd->width, mode_cmd->height,
  120. (char *)&mode_cmd->pixel_format);
  121. n = drm_format_num_planes(mode_cmd->pixel_format);
  122. hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format);
  123. vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format);
  124. format = kms->funcs->get_format(kms, mode_cmd->pixel_format);
  125. if (!format) {
  126. dev_err(dev->dev, "unsupported pixel format: %4.4s\n",
  127. (char *)&mode_cmd->pixel_format);
  128. ret = -EINVAL;
  129. goto fail;
  130. }
  131. msm_fb = kzalloc(sizeof(*msm_fb), GFP_KERNEL);
  132. if (!msm_fb) {
  133. ret = -ENOMEM;
  134. goto fail;
  135. }
  136. fb = &msm_fb->base;
  137. msm_fb->format = format;
  138. for (i = 0; i < n; i++) {
  139. unsigned int width = mode_cmd->width / (i ? hsub : 1);
  140. unsigned int height = mode_cmd->height / (i ? vsub : 1);
  141. unsigned int min_size;
  142. min_size = (height - 1) * mode_cmd->pitches[i]
  143. + width * drm_format_plane_cpp(mode_cmd->pixel_format, i)
  144. + mode_cmd->offsets[i];
  145. if (bos[i]->size < min_size) {
  146. ret = -EINVAL;
  147. goto fail;
  148. }
  149. msm_fb->planes[i] = bos[i];
  150. }
  151. drm_helper_mode_fill_fb_struct(fb, mode_cmd);
  152. ret = drm_framebuffer_init(dev, fb, &msm_framebuffer_funcs);
  153. if (ret) {
  154. dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
  155. goto fail;
  156. }
  157. DBG("create: FB ID: %d (%p)", fb->base.id, fb);
  158. return fb;
  159. fail:
  160. if (fb)
  161. msm_framebuffer_destroy(fb);
  162. return ERR_PTR(ret);
  163. }