msm_fb.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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[MAX_PLANE];
  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. /* prepare/pin all the fb's bo's for scanout. Note that it is not valid
  75. * to prepare an fb more multiple different initiator 'id's. But that
  76. * should be fine, since only the scanout (mdpN) side of things needs
  77. * this, the gpu doesn't care about fb's.
  78. */
  79. int msm_framebuffer_prepare(struct drm_framebuffer *fb, int id)
  80. {
  81. struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
  82. int ret, i, n = drm_format_num_planes(fb->pixel_format);
  83. uint32_t iova;
  84. for (i = 0; i < n; i++) {
  85. ret = msm_gem_get_iova(msm_fb->planes[i], id, &iova);
  86. DBG("FB[%u]: iova[%d]: %08x (%d)", fb->base.id, i, iova, ret);
  87. if (ret)
  88. return ret;
  89. }
  90. return 0;
  91. }
  92. void msm_framebuffer_cleanup(struct drm_framebuffer *fb, int id)
  93. {
  94. struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
  95. int i, n = drm_format_num_planes(fb->pixel_format);
  96. for (i = 0; i < n; i++)
  97. msm_gem_put_iova(msm_fb->planes[i], id);
  98. }
  99. uint32_t msm_framebuffer_iova(struct drm_framebuffer *fb, int id, int plane)
  100. {
  101. struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
  102. if (!msm_fb->planes[plane])
  103. return 0;
  104. return msm_gem_iova(msm_fb->planes[plane], id) + fb->offsets[plane];
  105. }
  106. struct drm_gem_object *msm_framebuffer_bo(struct drm_framebuffer *fb, int plane)
  107. {
  108. struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
  109. return msm_fb->planes[plane];
  110. }
  111. const struct msm_format *msm_framebuffer_format(struct drm_framebuffer *fb)
  112. {
  113. struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
  114. return msm_fb->format;
  115. }
  116. struct drm_framebuffer *msm_framebuffer_create(struct drm_device *dev,
  117. struct drm_file *file, struct drm_mode_fb_cmd2 *mode_cmd)
  118. {
  119. struct drm_gem_object *bos[4] = {0};
  120. struct drm_framebuffer *fb;
  121. int ret, i, n = drm_format_num_planes(mode_cmd->pixel_format);
  122. for (i = 0; i < n; i++) {
  123. bos[i] = drm_gem_object_lookup(dev, file,
  124. mode_cmd->handles[i]);
  125. if (!bos[i]) {
  126. ret = -ENXIO;
  127. goto out_unref;
  128. }
  129. }
  130. fb = msm_framebuffer_init(dev, mode_cmd, bos);
  131. if (IS_ERR(fb)) {
  132. ret = PTR_ERR(fb);
  133. goto out_unref;
  134. }
  135. return fb;
  136. out_unref:
  137. for (i = 0; i < n; i++)
  138. drm_gem_object_unreference_unlocked(bos[i]);
  139. return ERR_PTR(ret);
  140. }
  141. struct drm_framebuffer *msm_framebuffer_init(struct drm_device *dev,
  142. struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos)
  143. {
  144. struct msm_drm_private *priv = dev->dev_private;
  145. struct msm_kms *kms = priv->kms;
  146. struct msm_framebuffer *msm_fb = NULL;
  147. struct drm_framebuffer *fb;
  148. const struct msm_format *format;
  149. int ret, i, n;
  150. unsigned int hsub, vsub;
  151. DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%4.4s)",
  152. dev, mode_cmd, mode_cmd->width, mode_cmd->height,
  153. (char *)&mode_cmd->pixel_format);
  154. n = drm_format_num_planes(mode_cmd->pixel_format);
  155. hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format);
  156. vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format);
  157. format = kms->funcs->get_format(kms, mode_cmd->pixel_format);
  158. if (!format) {
  159. dev_err(dev->dev, "unsupported pixel format: %4.4s\n",
  160. (char *)&mode_cmd->pixel_format);
  161. ret = -EINVAL;
  162. goto fail;
  163. }
  164. msm_fb = kzalloc(sizeof(*msm_fb), GFP_KERNEL);
  165. if (!msm_fb) {
  166. ret = -ENOMEM;
  167. goto fail;
  168. }
  169. fb = &msm_fb->base;
  170. msm_fb->format = format;
  171. if (n > ARRAY_SIZE(msm_fb->planes)) {
  172. ret = -EINVAL;
  173. goto fail;
  174. }
  175. for (i = 0; i < n; i++) {
  176. unsigned int width = mode_cmd->width / (i ? hsub : 1);
  177. unsigned int height = mode_cmd->height / (i ? vsub : 1);
  178. unsigned int min_size;
  179. min_size = (height - 1) * mode_cmd->pitches[i]
  180. + width * drm_format_plane_cpp(mode_cmd->pixel_format, i)
  181. + mode_cmd->offsets[i];
  182. if (bos[i]->size < min_size) {
  183. ret = -EINVAL;
  184. goto fail;
  185. }
  186. msm_fb->planes[i] = bos[i];
  187. }
  188. drm_helper_mode_fill_fb_struct(fb, mode_cmd);
  189. ret = drm_framebuffer_init(dev, fb, &msm_framebuffer_funcs);
  190. if (ret) {
  191. dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
  192. goto fail;
  193. }
  194. DBG("create: FB ID: %d (%p)", fb->base.id, fb);
  195. return fb;
  196. fail:
  197. kfree(msm_fb);
  198. return ERR_PTR(ret);
  199. }