msm_fb.c 6.2 KB

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