msm_fb.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 <drm/drm_crtc.h>
  18. #include <drm/drm_crtc_helper.h>
  19. #include <drm/drm_gem_framebuffer_helper.h>
  20. #include "msm_drv.h"
  21. #include "msm_kms.h"
  22. #include "msm_gem.h"
  23. struct msm_framebuffer {
  24. struct drm_framebuffer base;
  25. const struct msm_format *format;
  26. };
  27. #define to_msm_framebuffer(x) container_of(x, struct msm_framebuffer, base)
  28. static struct drm_framebuffer *msm_framebuffer_init(struct drm_device *dev,
  29. const struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos);
  30. static const struct drm_framebuffer_funcs msm_framebuffer_funcs = {
  31. .create_handle = drm_gem_fb_create_handle,
  32. .destroy = drm_gem_fb_destroy,
  33. };
  34. #ifdef CONFIG_DEBUG_FS
  35. void msm_framebuffer_describe(struct drm_framebuffer *fb, struct seq_file *m)
  36. {
  37. int i, n = fb->format->num_planes;
  38. seq_printf(m, "fb: %dx%d@%4.4s (%2d, ID:%d)\n",
  39. fb->width, fb->height, (char *)&fb->format->format,
  40. drm_framebuffer_read_refcount(fb), fb->base.id);
  41. for (i = 0; i < n; i++) {
  42. seq_printf(m, " %d: offset=%d pitch=%d, obj: ",
  43. i, fb->offsets[i], fb->pitches[i]);
  44. msm_gem_describe(fb->obj[i], m);
  45. }
  46. }
  47. #endif
  48. /* prepare/pin all the fb's bo's for scanout. Note that it is not valid
  49. * to prepare an fb more multiple different initiator 'id's. But that
  50. * should be fine, since only the scanout (mdpN) side of things needs
  51. * this, the gpu doesn't care about fb's.
  52. */
  53. int msm_framebuffer_prepare(struct drm_framebuffer *fb,
  54. struct msm_gem_address_space *aspace)
  55. {
  56. int ret, i, n = fb->format->num_planes;
  57. uint64_t iova;
  58. for (i = 0; i < n; i++) {
  59. ret = msm_gem_get_iova(fb->obj[i], aspace, &iova);
  60. DBG("FB[%u]: iova[%d]: %08llx (%d)", fb->base.id, i, iova, ret);
  61. if (ret)
  62. return ret;
  63. }
  64. return 0;
  65. }
  66. void msm_framebuffer_cleanup(struct drm_framebuffer *fb,
  67. struct msm_gem_address_space *aspace)
  68. {
  69. int i, n = fb->format->num_planes;
  70. for (i = 0; i < n; i++)
  71. msm_gem_put_iova(fb->obj[i], aspace);
  72. }
  73. uint32_t msm_framebuffer_iova(struct drm_framebuffer *fb,
  74. struct msm_gem_address_space *aspace, int plane)
  75. {
  76. if (!fb->obj[plane])
  77. return 0;
  78. return msm_gem_iova(fb->obj[plane], aspace) + fb->offsets[plane];
  79. }
  80. struct drm_gem_object *msm_framebuffer_bo(struct drm_framebuffer *fb, int plane)
  81. {
  82. return drm_gem_fb_get_obj(fb, plane);
  83. }
  84. const struct msm_format *msm_framebuffer_format(struct drm_framebuffer *fb)
  85. {
  86. struct msm_framebuffer *msm_fb = to_msm_framebuffer(fb);
  87. return msm_fb->format;
  88. }
  89. struct drm_framebuffer *msm_framebuffer_create(struct drm_device *dev,
  90. struct drm_file *file, const struct drm_mode_fb_cmd2 *mode_cmd)
  91. {
  92. struct drm_gem_object *bos[4] = {0};
  93. struct drm_framebuffer *fb;
  94. int ret, i, n = drm_format_num_planes(mode_cmd->pixel_format);
  95. for (i = 0; i < n; i++) {
  96. bos[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]);
  97. if (!bos[i]) {
  98. ret = -ENXIO;
  99. goto out_unref;
  100. }
  101. }
  102. fb = msm_framebuffer_init(dev, mode_cmd, bos);
  103. if (IS_ERR(fb)) {
  104. ret = PTR_ERR(fb);
  105. goto out_unref;
  106. }
  107. return fb;
  108. out_unref:
  109. for (i = 0; i < n; i++)
  110. drm_gem_object_put_unlocked(bos[i]);
  111. return ERR_PTR(ret);
  112. }
  113. static struct drm_framebuffer *msm_framebuffer_init(struct drm_device *dev,
  114. const struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos)
  115. {
  116. struct msm_drm_private *priv = dev->dev_private;
  117. struct msm_kms *kms = priv->kms;
  118. struct msm_framebuffer *msm_fb = NULL;
  119. struct drm_framebuffer *fb;
  120. const struct msm_format *format;
  121. int ret, i, n;
  122. unsigned int hsub, vsub;
  123. DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%4.4s)",
  124. dev, mode_cmd, mode_cmd->width, mode_cmd->height,
  125. (char *)&mode_cmd->pixel_format);
  126. n = drm_format_num_planes(mode_cmd->pixel_format);
  127. hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format);
  128. vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format);
  129. format = kms->funcs->get_format(kms, mode_cmd->pixel_format,
  130. mode_cmd->modifier[0]);
  131. if (!format) {
  132. dev_err(dev->dev, "unsupported pixel format: %4.4s\n",
  133. (char *)&mode_cmd->pixel_format);
  134. ret = -EINVAL;
  135. goto fail;
  136. }
  137. msm_fb = kzalloc(sizeof(*msm_fb), GFP_KERNEL);
  138. if (!msm_fb) {
  139. ret = -ENOMEM;
  140. goto fail;
  141. }
  142. fb = &msm_fb->base;
  143. msm_fb->format = format;
  144. if (n > ARRAY_SIZE(fb->obj)) {
  145. ret = -EINVAL;
  146. goto fail;
  147. }
  148. for (i = 0; i < n; i++) {
  149. unsigned int width = mode_cmd->width / (i ? hsub : 1);
  150. unsigned int height = mode_cmd->height / (i ? vsub : 1);
  151. unsigned int min_size;
  152. min_size = (height - 1) * mode_cmd->pitches[i]
  153. + width * drm_format_plane_cpp(mode_cmd->pixel_format, i)
  154. + mode_cmd->offsets[i];
  155. if (bos[i]->size < min_size) {
  156. ret = -EINVAL;
  157. goto fail;
  158. }
  159. msm_fb->base.obj[i] = bos[i];
  160. }
  161. drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
  162. ret = drm_framebuffer_init(dev, fb, &msm_framebuffer_funcs);
  163. if (ret) {
  164. dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
  165. goto fail;
  166. }
  167. DBG("create: FB ID: %d (%p)", fb->base.id, fb);
  168. return fb;
  169. fail:
  170. kfree(msm_fb);
  171. return ERR_PTR(ret);
  172. }
  173. struct drm_framebuffer *
  174. msm_alloc_stolen_fb(struct drm_device *dev, int w, int h, int p, uint32_t format)
  175. {
  176. struct drm_mode_fb_cmd2 mode_cmd = {
  177. .pixel_format = format,
  178. .width = w,
  179. .height = h,
  180. .pitches = { p },
  181. };
  182. struct drm_gem_object *bo;
  183. struct drm_framebuffer *fb;
  184. int size;
  185. /* allocate backing bo */
  186. size = mode_cmd.pitches[0] * mode_cmd.height;
  187. DBG("allocating %d bytes for fb %d", size, dev->primary->index);
  188. bo = msm_gem_new(dev, size, MSM_BO_SCANOUT | MSM_BO_WC | MSM_BO_STOLEN);
  189. if (IS_ERR(bo)) {
  190. dev_warn(dev->dev, "could not allocate stolen bo\n");
  191. /* try regular bo: */
  192. bo = msm_gem_new(dev, size, MSM_BO_SCANOUT | MSM_BO_WC);
  193. }
  194. if (IS_ERR(bo)) {
  195. dev_err(dev->dev, "failed to allocate buffer object\n");
  196. return ERR_CAST(bo);
  197. }
  198. fb = msm_framebuffer_init(dev, &mode_cmd, &bo);
  199. if (IS_ERR(fb)) {
  200. dev_err(dev->dev, "failed to allocate fb\n");
  201. /* note: if fb creation failed, we can't rely on fb destroy
  202. * to unref the bo:
  203. */
  204. drm_gem_object_put_unlocked(bo);
  205. return ERR_CAST(fb);
  206. }
  207. return fb;
  208. }