msm_fb.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 "msm_kms.h"
  19. #include "drm_crtc.h"
  20. #include "drm_crtc_helper.h"
  21. struct msm_framebuffer {
  22. struct drm_framebuffer base;
  23. const struct msm_format *format;
  24. struct drm_gem_object *planes[2];
  25. };
  26. #define to_msm_framebuffer(x) container_of(x, struct msm_framebuffer, base)
  27. static int msm_framebuffer_create_handle(struct drm_framebuffer *fb,
  28. struct drm_file *file_priv,
  29. unsigned int *handle)
  30. {
  31. struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
  32. return drm_gem_handle_create(file_priv,
  33. msm_fb->planes[0], handle);
  34. }
  35. static void msm_framebuffer_destroy(struct drm_framebuffer *fb)
  36. {
  37. struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
  38. int i, n = drm_format_num_planes(fb->pixel_format);
  39. DBG("destroy: FB ID: %d (%p)", fb->base.id, fb);
  40. drm_framebuffer_cleanup(fb);
  41. for (i = 0; i < n; i++) {
  42. struct drm_gem_object *bo = msm_fb->planes[i];
  43. if (bo)
  44. drm_gem_object_unreference_unlocked(bo);
  45. }
  46. kfree(msm_fb);
  47. }
  48. static int msm_framebuffer_dirty(struct drm_framebuffer *fb,
  49. struct drm_file *file_priv, unsigned flags, unsigned color,
  50. struct drm_clip_rect *clips, unsigned num_clips)
  51. {
  52. return 0;
  53. }
  54. static const struct drm_framebuffer_funcs msm_framebuffer_funcs = {
  55. .create_handle = msm_framebuffer_create_handle,
  56. .destroy = msm_framebuffer_destroy,
  57. .dirty = msm_framebuffer_dirty,
  58. };
  59. #ifdef CONFIG_DEBUG_FS
  60. void msm_framebuffer_describe(struct drm_framebuffer *fb, struct seq_file *m)
  61. {
  62. struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
  63. int i, n = drm_format_num_planes(fb->pixel_format);
  64. seq_printf(m, "fb: %dx%d@%4.4s (%2d, ID:%d)\n",
  65. fb->width, fb->height, (char *)&fb->pixel_format,
  66. fb->refcount.refcount.counter, fb->base.id);
  67. for (i = 0; i < n; i++) {
  68. seq_printf(m, " %d: offset=%d pitch=%d, obj: ",
  69. i, fb->offsets[i], fb->pitches[i]);
  70. msm_gem_describe(msm_fb->planes[i], m);
  71. }
  72. }
  73. #endif
  74. struct drm_gem_object *msm_framebuffer_bo(struct drm_framebuffer *fb, int plane)
  75. {
  76. struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
  77. return msm_fb->planes[plane];
  78. }
  79. const struct msm_format *msm_framebuffer_format(struct drm_framebuffer *fb)
  80. {
  81. struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
  82. return msm_fb->format;
  83. }
  84. struct drm_framebuffer *msm_framebuffer_create(struct drm_device *dev,
  85. struct drm_file *file, struct drm_mode_fb_cmd2 *mode_cmd)
  86. {
  87. struct drm_gem_object *bos[4] = {0};
  88. struct drm_framebuffer *fb;
  89. int ret, i, n = drm_format_num_planes(mode_cmd->pixel_format);
  90. for (i = 0; i < n; i++) {
  91. bos[i] = drm_gem_object_lookup(dev, file,
  92. mode_cmd->handles[i]);
  93. if (!bos[i]) {
  94. ret = -ENXIO;
  95. goto out_unref;
  96. }
  97. }
  98. fb = msm_framebuffer_init(dev, mode_cmd, bos);
  99. if (IS_ERR(fb)) {
  100. ret = PTR_ERR(fb);
  101. goto out_unref;
  102. }
  103. return fb;
  104. out_unref:
  105. for (i = 0; i < n; i++)
  106. drm_gem_object_unreference_unlocked(bos[i]);
  107. return ERR_PTR(ret);
  108. }
  109. struct drm_framebuffer *msm_framebuffer_init(struct drm_device *dev,
  110. struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos)
  111. {
  112. struct msm_drm_private *priv = dev->dev_private;
  113. struct msm_kms *kms = priv->kms;
  114. struct msm_framebuffer *msm_fb;
  115. struct drm_framebuffer *fb = NULL;
  116. const struct msm_format *format;
  117. int ret, i, n;
  118. unsigned int hsub, vsub;
  119. DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%4.4s)",
  120. dev, mode_cmd, mode_cmd->width, mode_cmd->height,
  121. (char *)&mode_cmd->pixel_format);
  122. n = drm_format_num_planes(mode_cmd->pixel_format);
  123. hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format);
  124. vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format);
  125. format = kms->funcs->get_format(kms, mode_cmd->pixel_format);
  126. if (!format) {
  127. dev_err(dev->dev, "unsupported pixel format: %4.4s\n",
  128. (char *)&mode_cmd->pixel_format);
  129. ret = -EINVAL;
  130. goto fail;
  131. }
  132. msm_fb = kzalloc(sizeof(*msm_fb), GFP_KERNEL);
  133. if (!msm_fb) {
  134. ret = -ENOMEM;
  135. goto fail;
  136. }
  137. fb = &msm_fb->base;
  138. msm_fb->format = format;
  139. for (i = 0; i < n; i++) {
  140. unsigned int width = mode_cmd->width / (i ? hsub : 1);
  141. unsigned int height = mode_cmd->height / (i ? vsub : 1);
  142. unsigned int min_size;
  143. min_size = (height - 1) * mode_cmd->pitches[i]
  144. + width * drm_format_plane_cpp(mode_cmd->pixel_format, i)
  145. + mode_cmd->offsets[i];
  146. if (bos[i]->size < min_size) {
  147. ret = -EINVAL;
  148. goto fail;
  149. }
  150. msm_fb->planes[i] = bos[i];
  151. }
  152. drm_helper_mode_fill_fb_struct(fb, mode_cmd);
  153. ret = drm_framebuffer_init(dev, fb, &msm_framebuffer_funcs);
  154. if (ret) {
  155. dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
  156. goto fail;
  157. }
  158. DBG("create: FB ID: %d (%p)", fb->base.id, fb);
  159. return fb;
  160. fail:
  161. if (fb)
  162. msm_framebuffer_destroy(fb);
  163. return ERR_PTR(ret);
  164. }