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