msm_fb.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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_rd_type;
  32. u32 cache_wr_type;
  33. };
  34. #define to_msm_framebuffer(x) container_of(x, struct msm_framebuffer, base)
  35. static const struct drm_framebuffer_funcs msm_framebuffer_funcs = {
  36. .create_handle = drm_gem_fb_create_handle,
  37. .destroy = drm_gem_fb_destroy,
  38. .dirty = drm_atomic_helper_dirtyfb,
  39. };
  40. /* prepare/pin all the fb's bo's for scanout. Note that it is not valid
  41. * to prepare an fb more multiple different initiator 'id's. But that
  42. * should be fine, since only the scanout (mdpN) side of things needs
  43. * this, the gpu doesn't care about fb's.
  44. */
  45. int msm_framebuffer_prepare(struct drm_framebuffer *fb,
  46. struct msm_gem_address_space *aspace)
  47. {
  48. struct msm_framebuffer *msm_fb;
  49. int ret, i, n;
  50. uint64_t iova;
  51. if (!fb) {
  52. DRM_ERROR("from:%pS null fb\n", __builtin_return_address(0));
  53. return -EINVAL;
  54. }
  55. msm_fb = to_msm_framebuffer(fb);
  56. n = fb->format->num_planes;
  57. for (i = 0; i < n; i++) {
  58. ret = msm_gem_get_iova(fb->obj[i], aspace, &iova);
  59. DBG("FB[%u]: iova[%d]: %08llx (%d)", fb->base.id, i, iova, ret);
  60. if (ret)
  61. return ret;
  62. }
  63. return 0;
  64. }
  65. void msm_framebuffer_cleanup(struct drm_framebuffer *fb,
  66. struct msm_gem_address_space *aspace)
  67. {
  68. struct msm_framebuffer *msm_fb;
  69. int i, n;
  70. if (fb == NULL) {
  71. DRM_ERROR("from:%pS null fb\n", __builtin_return_address(0));
  72. return;
  73. }
  74. msm_fb = to_msm_framebuffer(fb);
  75. n = fb->format->num_planes;
  76. for (i = 0; i < n; i++)
  77. msm_gem_put_iova(fb->obj[i], aspace);
  78. }
  79. uint32_t msm_framebuffer_iova(struct drm_framebuffer *fb,
  80. struct msm_gem_address_space *aspace, int plane)
  81. {
  82. if (!fb) {
  83. DRM_ERROR("from:%pS null fb\n", __builtin_return_address(0));
  84. return -EINVAL;
  85. }
  86. if (!fb->obj[plane])
  87. return 0;
  88. return msm_gem_iova(fb->obj[plane], aspace) + fb->offsets[plane];
  89. }
  90. uint32_t msm_framebuffer_phys(struct drm_framebuffer *fb,
  91. int plane)
  92. {
  93. struct msm_framebuffer *msm_fb;
  94. dma_addr_t phys_addr;
  95. if (!fb) {
  96. DRM_ERROR("from:%pS null fb\n", __builtin_return_address(0));
  97. return -EINVAL;
  98. }
  99. msm_fb = to_msm_framebuffer(fb);
  100. if (!msm_fb->base.obj[plane])
  101. return 0;
  102. phys_addr = msm_gem_get_dma_addr(msm_fb->base.obj[plane]);
  103. if (!phys_addr)
  104. return 0;
  105. return phys_addr + fb->offsets[plane];
  106. }
  107. struct drm_gem_object *msm_framebuffer_bo(struct drm_framebuffer *fb, int plane)
  108. {
  109. if (!fb) {
  110. DRM_ERROR("from:%pS null fb\n", __builtin_return_address(0));
  111. return ERR_PTR(-EINVAL);
  112. }
  113. return drm_gem_fb_get_obj(fb, plane);
  114. }
  115. const struct msm_format *msm_framebuffer_format(struct drm_framebuffer *fb)
  116. {
  117. return fb ? (to_msm_framebuffer(fb))->format : NULL;
  118. }
  119. struct drm_framebuffer *msm_framebuffer_create(struct drm_device *dev,
  120. struct drm_file *file, const struct drm_mode_fb_cmd2 *mode_cmd)
  121. {
  122. const struct drm_format_info *info = drm_get_format_info(dev,
  123. mode_cmd);
  124. struct drm_gem_object *bos[4] = {0};
  125. struct drm_framebuffer *fb;
  126. int ret, i, n = info->num_planes;
  127. for (i = 0; i < n; i++) {
  128. bos[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]);
  129. if (!bos[i]) {
  130. ret = -ENXIO;
  131. goto out_unref;
  132. }
  133. }
  134. fb = msm_framebuffer_init(dev, mode_cmd, bos);
  135. if (IS_ERR(fb)) {
  136. ret = PTR_ERR(fb);
  137. goto out_unref;
  138. }
  139. return fb;
  140. out_unref:
  141. for (i = 0; i < n; i++)
  142. drm_gem_object_put(bos[i]);
  143. return ERR_PTR(ret);
  144. }
  145. struct drm_framebuffer *msm_framebuffer_init(struct drm_device *dev,
  146. const struct drm_mode_fb_cmd2 *mode_cmd,
  147. struct drm_gem_object **bos)
  148. {
  149. const struct drm_format_info *info = drm_get_format_info(dev,
  150. mode_cmd);
  151. struct msm_drm_private *priv = dev->dev_private;
  152. struct msm_kms *kms = priv->kms;
  153. struct msm_framebuffer *msm_fb = NULL;
  154. struct drm_framebuffer *fb;
  155. const struct msm_format *format;
  156. int ret, i, num_planes;
  157. bool is_modified = false;
  158. DBG("create framebuffer: dev=%pK, mode_cmd=%pK (%dx%d@%4.4s)",
  159. dev, mode_cmd, mode_cmd->width, mode_cmd->height,
  160. (char *)&mode_cmd->pixel_format);
  161. num_planes = info->num_planes;
  162. format = kms->funcs->get_format(kms, mode_cmd->pixel_format,
  163. mode_cmd->modifier[0]);
  164. if (!format) {
  165. DISP_DEV_ERR(dev->dev, "unsupported pixel format: %4.4s\n",
  166. (char *)&mode_cmd->pixel_format);
  167. ret = -EINVAL;
  168. goto fail;
  169. }
  170. msm_fb = kzalloc(sizeof(*msm_fb), GFP_KERNEL);
  171. if (!msm_fb) {
  172. ret = -ENOMEM;
  173. goto fail;
  174. }
  175. fb = &msm_fb->base;
  176. msm_fb->format = format;
  177. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  178. for (i = 0; i < ARRAY_SIZE(mode_cmd->modifier); i++) {
  179. if (mode_cmd->modifier[i]) {
  180. is_modified = true;
  181. break;
  182. }
  183. }
  184. }
  185. if (num_planes > ARRAY_SIZE(fb->obj)) {
  186. ret = -EINVAL;
  187. goto fail;
  188. }
  189. if (is_modified) {
  190. if (!kms->funcs->check_modified_format) {
  191. DISP_DEV_ERR(dev->dev, "can't check modified fb format\n");
  192. ret = -EINVAL;
  193. goto fail;
  194. } else {
  195. ret = kms->funcs->check_modified_format(
  196. kms, msm_fb->format, mode_cmd, bos);
  197. if (ret)
  198. goto fail;
  199. }
  200. } else {
  201. const struct drm_format_info *info;
  202. info = drm_format_info(mode_cmd->pixel_format);
  203. if (!info || num_planes > ARRAY_SIZE(info->cpp)) {
  204. ret = -EINVAL;
  205. goto fail;
  206. }
  207. for (i = 0; i < num_planes; i++) {
  208. unsigned int width = mode_cmd->width / (i ?
  209. info->hsub : 1);
  210. unsigned int height = mode_cmd->height / (i ?
  211. info->vsub : 1);
  212. unsigned int min_size;
  213. min_size = (height - 1) * mode_cmd->pitches[i]
  214. + width * info->cpp[i]
  215. + mode_cmd->offsets[i];
  216. if (!bos[i] || bos[i]->size < min_size) {
  217. ret = -EINVAL;
  218. goto fail;
  219. }
  220. }
  221. }
  222. for (i = 0; i < num_planes; i++)
  223. msm_fb->base.obj[i] = bos[i];
  224. drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
  225. ret = drm_framebuffer_init(dev, fb, &msm_framebuffer_funcs);
  226. if (ret) {
  227. DISP_DEV_ERR(dev->dev, "framebuffer init failed: %d\n", ret);
  228. goto fail;
  229. }
  230. DBG("create: FB ID: %d (%pK)", fb->base.id, fb);
  231. return fb;
  232. fail:
  233. kfree(msm_fb);
  234. return ERR_PTR(ret);
  235. }
  236. int msm_framebuffer_set_cache_hint(struct drm_framebuffer *fb,
  237. u32 flags, u32 rd_type, u32 wr_type)
  238. {
  239. struct msm_framebuffer *msm_fb;
  240. if (!fb)
  241. return -EINVAL;
  242. msm_fb = to_msm_framebuffer(fb);
  243. msm_fb->cache_flags = flags;
  244. msm_fb->cache_rd_type = rd_type;
  245. msm_fb->cache_wr_type = wr_type;
  246. return 0;
  247. }
  248. int msm_framebuffer_get_cache_hint(struct drm_framebuffer *fb,
  249. u32 *flags, u32 *rd_type, u32 *wr_type)
  250. {
  251. struct msm_framebuffer *msm_fb;
  252. if (!fb)
  253. return -EINVAL;
  254. msm_fb = to_msm_framebuffer(fb);
  255. *flags = msm_fb->cache_flags;
  256. *rd_type = msm_fb->cache_rd_type;
  257. *wr_type = msm_fb->cache_wr_type;
  258. return 0;
  259. }