msm_fb.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
  3. * Copyright (c) 2018-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (C) 2013 Red Hat
  5. * Author: Rob Clark <[email protected]>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <drm/drm_crtc.h>
  20. #include <drm/drm_fourcc.h>
  21. #include <drm/drm_damage_helper.h>
  22. #include <drm/drm_gem_framebuffer_helper.h>
  23. #include <drm/drm_probe_helper.h>
  24. #include "msm_drv.h"
  25. #include "msm_kms.h"
  26. #include "msm_gem.h"
  27. struct msm_framebuffer {
  28. struct drm_framebuffer base;
  29. const struct msm_format *format;
  30. u32 cache_flags;
  31. u32 cache_type;
  32. };
  33. #define to_msm_framebuffer(x) container_of(x, struct msm_framebuffer, base)
  34. static const struct drm_framebuffer_funcs msm_framebuffer_funcs = {
  35. .create_handle = drm_gem_fb_create_handle,
  36. .destroy = drm_gem_fb_destroy,
  37. .dirty = drm_atomic_helper_dirtyfb,
  38. };
  39. /* prepare/pin all the fb's bo's for scanout. Note that it is not valid
  40. * to prepare an fb more multiple different initiator 'id's. But that
  41. * should be fine, since only the scanout (mdpN) side of things needs
  42. * this, the gpu doesn't care about fb's.
  43. */
  44. int msm_framebuffer_prepare(struct drm_framebuffer *fb,
  45. struct msm_gem_address_space *aspace)
  46. {
  47. struct msm_framebuffer *msm_fb;
  48. int ret, i, n;
  49. uint64_t iova;
  50. if (!fb) {
  51. DRM_ERROR("from:%pS null fb\n", __builtin_return_address(0));
  52. return -EINVAL;
  53. }
  54. msm_fb = to_msm_framebuffer(fb);
  55. n = fb->format->num_planes;
  56. for (i = 0; i < n; i++) {
  57. ret = msm_gem_get_iova(fb->obj[i], aspace, &iova);
  58. DBG("FB[%u]: iova[%d]: %08llx (%d)", fb->base.id, i, iova, ret);
  59. if (ret)
  60. return ret;
  61. }
  62. return 0;
  63. }
  64. void msm_framebuffer_cleanup(struct drm_framebuffer *fb,
  65. struct msm_gem_address_space *aspace)
  66. {
  67. struct msm_framebuffer *msm_fb;
  68. int i, n;
  69. if (fb == NULL) {
  70. DRM_ERROR("from:%pS null fb\n", __builtin_return_address(0));
  71. return;
  72. }
  73. msm_fb = to_msm_framebuffer(fb);
  74. n = fb->format->num_planes;
  75. for (i = 0; i < n; i++)
  76. msm_gem_put_iova(fb->obj[i], aspace);
  77. }
  78. uint32_t msm_framebuffer_iova(struct drm_framebuffer *fb,
  79. struct msm_gem_address_space *aspace, int plane)
  80. {
  81. if (!fb) {
  82. DRM_ERROR("from:%pS null fb\n", __builtin_return_address(0));
  83. return -EINVAL;
  84. }
  85. if (!fb->obj[plane])
  86. return 0;
  87. return msm_gem_iova(fb->obj[plane], aspace) + fb->offsets[plane];
  88. }
  89. uint32_t msm_framebuffer_phys(struct drm_framebuffer *fb,
  90. int plane)
  91. {
  92. struct msm_framebuffer *msm_fb;
  93. dma_addr_t phys_addr;
  94. if (!fb) {
  95. DRM_ERROR("from:%pS null fb\n", __builtin_return_address(0));
  96. return -EINVAL;
  97. }
  98. msm_fb = to_msm_framebuffer(fb);
  99. if (!msm_fb->base.obj[plane])
  100. return 0;
  101. phys_addr = msm_gem_get_dma_addr(msm_fb->base.obj[plane]);
  102. if (!phys_addr)
  103. return 0;
  104. return phys_addr + fb->offsets[plane];
  105. }
  106. struct drm_gem_object *msm_framebuffer_bo(struct drm_framebuffer *fb, int plane)
  107. {
  108. if (!fb) {
  109. DRM_ERROR("from:%pS null fb\n", __builtin_return_address(0));
  110. return ERR_PTR(-EINVAL);
  111. }
  112. return drm_gem_fb_get_obj(fb, plane);
  113. }
  114. const struct msm_format *msm_framebuffer_format(struct drm_framebuffer *fb)
  115. {
  116. return fb ? (to_msm_framebuffer(fb))->format : NULL;
  117. }
  118. struct drm_framebuffer *msm_framebuffer_create(struct drm_device *dev,
  119. struct drm_file *file, const struct drm_mode_fb_cmd2 *mode_cmd)
  120. {
  121. const struct drm_format_info *info = drm_get_format_info(dev,
  122. mode_cmd);
  123. struct drm_gem_object *bos[4] = {0};
  124. struct drm_framebuffer *fb;
  125. int ret, i, n = info->num_planes;
  126. for (i = 0; i < n; i++) {
  127. bos[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]);
  128. if (!bos[i]) {
  129. ret = -ENXIO;
  130. goto out_unref;
  131. }
  132. }
  133. fb = msm_framebuffer_init(dev, mode_cmd, bos);
  134. if (IS_ERR(fb)) {
  135. ret = PTR_ERR(fb);
  136. goto out_unref;
  137. }
  138. return fb;
  139. out_unref:
  140. for (i = 0; i < n; i++)
  141. drm_gem_object_put(bos[i]);
  142. return ERR_PTR(ret);
  143. }
  144. struct drm_framebuffer *msm_framebuffer_init(struct drm_device *dev,
  145. const struct drm_mode_fb_cmd2 *mode_cmd,
  146. struct drm_gem_object **bos)
  147. {
  148. const struct drm_format_info *info = drm_get_format_info(dev,
  149. mode_cmd);
  150. struct msm_drm_private *priv = dev->dev_private;
  151. struct msm_kms *kms = priv->kms;
  152. struct msm_framebuffer *msm_fb = NULL;
  153. struct drm_framebuffer *fb;
  154. const struct msm_format *format;
  155. int ret, i, num_planes;
  156. bool is_modified = false;
  157. DBG("create framebuffer: dev=%pK, mode_cmd=%pK (%dx%d@%4.4s)",
  158. dev, mode_cmd, mode_cmd->width, mode_cmd->height,
  159. (char *)&mode_cmd->pixel_format);
  160. num_planes = info->num_planes;
  161. format = kms->funcs->get_format(kms, mode_cmd->pixel_format,
  162. mode_cmd->modifier[0]);
  163. if (!format) {
  164. DISP_DEV_ERR(dev->dev, "unsupported pixel format: %4.4s\n",
  165. (char *)&mode_cmd->pixel_format);
  166. ret = -EINVAL;
  167. goto fail;
  168. }
  169. msm_fb = kzalloc(sizeof(*msm_fb), GFP_KERNEL);
  170. if (!msm_fb) {
  171. ret = -ENOMEM;
  172. goto fail;
  173. }
  174. fb = &msm_fb->base;
  175. msm_fb->format = format;
  176. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  177. for (i = 0; i < ARRAY_SIZE(mode_cmd->modifier); i++) {
  178. if (mode_cmd->modifier[i]) {
  179. is_modified = true;
  180. break;
  181. }
  182. }
  183. }
  184. if (num_planes > ARRAY_SIZE(fb->obj)) {
  185. ret = -EINVAL;
  186. goto fail;
  187. }
  188. if (is_modified) {
  189. if (!kms->funcs->check_modified_format) {
  190. DISP_DEV_ERR(dev->dev, "can't check modified fb format\n");
  191. ret = -EINVAL;
  192. goto fail;
  193. } else {
  194. ret = kms->funcs->check_modified_format(
  195. kms, msm_fb->format, mode_cmd, bos);
  196. if (ret)
  197. goto fail;
  198. }
  199. } else {
  200. const struct drm_format_info *info;
  201. info = drm_format_info(mode_cmd->pixel_format);
  202. if (!info || num_planes > ARRAY_SIZE(info->cpp)) {
  203. ret = -EINVAL;
  204. goto fail;
  205. }
  206. for (i = 0; i < num_planes; i++) {
  207. unsigned int width = mode_cmd->width / (i ?
  208. info->hsub : 1);
  209. unsigned int height = mode_cmd->height / (i ?
  210. info->vsub : 1);
  211. unsigned int min_size;
  212. min_size = (height - 1) * mode_cmd->pitches[i]
  213. + width * info->cpp[i]
  214. + mode_cmd->offsets[i];
  215. if (!bos[i] || bos[i]->size < min_size) {
  216. ret = -EINVAL;
  217. goto fail;
  218. }
  219. }
  220. }
  221. for (i = 0; i < num_planes; i++)
  222. msm_fb->base.obj[i] = bos[i];
  223. drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
  224. ret = drm_framebuffer_init(dev, fb, &msm_framebuffer_funcs);
  225. if (ret) {
  226. DISP_DEV_ERR(dev->dev, "framebuffer init failed: %d\n", ret);
  227. goto fail;
  228. }
  229. DBG("create: FB ID: %d (%pK)", fb->base.id, fb);
  230. return fb;
  231. fail:
  232. kfree(msm_fb);
  233. return ERR_PTR(ret);
  234. }
  235. void msm_framebuffer_set_cache_hint(struct drm_framebuffer *fb, u32 flags, u32 type)
  236. {
  237. struct msm_framebuffer *msm_fb;
  238. if (!fb)
  239. return;
  240. msm_fb = to_msm_framebuffer(fb);
  241. msm_fb->cache_flags = flags;
  242. msm_fb->cache_type = type;
  243. }
  244. void msm_framebuffer_get_cache_hint(struct drm_framebuffer *fb, u32 *flags, u32 *type)
  245. {
  246. struct msm_framebuffer *msm_fb;
  247. if (!fb) {
  248. *flags = 0;
  249. *type = 0;
  250. return;
  251. }
  252. msm_fb = to_msm_framebuffer(fb);
  253. *flags = msm_fb->cache_flags;
  254. *type = msm_fb->cache_type;
  255. }