mtk_plane.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015 MediaTek Inc.
  4. * Author: CK Hu <ck.hu@mediatek.com>
  5. */
  6. #include <drm/drm_atomic.h>
  7. #include <drm/drm_atomic_helper.h>
  8. #include <drm/drm_atomic_uapi.h>
  9. #include <drm/drm_blend.h>
  10. #include <drm/drm_fourcc.h>
  11. #include <drm/drm_framebuffer.h>
  12. #include <drm/drm_gem_atomic_helper.h>
  13. #include <linux/align.h>
  14. #include "mtk_crtc.h"
  15. #include "mtk_ddp_comp.h"
  16. #include "mtk_drm_drv.h"
  17. #include "mtk_gem.h"
  18. #include "mtk_plane.h"
  19. static const u64 modifiers[] = {
  20. DRM_FORMAT_MOD_LINEAR,
  21. DRM_FORMAT_MOD_ARM_AFBC(AFBC_FORMAT_MOD_BLOCK_SIZE_32x8 |
  22. AFBC_FORMAT_MOD_SPLIT |
  23. AFBC_FORMAT_MOD_SPARSE),
  24. DRM_FORMAT_MOD_INVALID,
  25. };
  26. static void mtk_plane_reset(struct drm_plane *plane)
  27. {
  28. struct mtk_plane_state *state;
  29. if (plane->state) {
  30. __drm_atomic_helper_plane_destroy_state(plane->state);
  31. state = to_mtk_plane_state(plane->state);
  32. memset(state, 0, sizeof(*state));
  33. } else {
  34. state = kzalloc(sizeof(*state), GFP_KERNEL);
  35. if (!state)
  36. return;
  37. }
  38. __drm_atomic_helper_plane_reset(plane, &state->base);
  39. state->base.plane = plane;
  40. state->pending.format = DRM_FORMAT_RGB565;
  41. state->pending.modifier = DRM_FORMAT_MOD_LINEAR;
  42. }
  43. static struct drm_plane_state *mtk_plane_duplicate_state(struct drm_plane *plane)
  44. {
  45. struct mtk_plane_state *old_state = to_mtk_plane_state(plane->state);
  46. struct mtk_plane_state *state;
  47. state = kmalloc(sizeof(*state), GFP_KERNEL);
  48. if (!state)
  49. return NULL;
  50. __drm_atomic_helper_plane_duplicate_state(plane, &state->base);
  51. WARN_ON(state->base.plane != plane);
  52. state->pending = old_state->pending;
  53. return &state->base;
  54. }
  55. static bool mtk_plane_format_mod_supported(struct drm_plane *plane,
  56. uint32_t format,
  57. uint64_t modifier)
  58. {
  59. if (modifier == DRM_FORMAT_MOD_LINEAR)
  60. return true;
  61. if (modifier != DRM_FORMAT_MOD_ARM_AFBC(
  62. AFBC_FORMAT_MOD_BLOCK_SIZE_32x8 |
  63. AFBC_FORMAT_MOD_SPLIT |
  64. AFBC_FORMAT_MOD_SPARSE))
  65. return false;
  66. if (format != DRM_FORMAT_XRGB8888 &&
  67. format != DRM_FORMAT_ARGB8888 &&
  68. format != DRM_FORMAT_BGRX8888 &&
  69. format != DRM_FORMAT_BGRA8888 &&
  70. format != DRM_FORMAT_ABGR8888 &&
  71. format != DRM_FORMAT_XBGR8888 &&
  72. format != DRM_FORMAT_RGB888 &&
  73. format != DRM_FORMAT_BGR888)
  74. return false;
  75. return true;
  76. }
  77. static void mtk_plane_destroy_state(struct drm_plane *plane,
  78. struct drm_plane_state *state)
  79. {
  80. __drm_atomic_helper_plane_destroy_state(state);
  81. kfree(to_mtk_plane_state(state));
  82. }
  83. static int mtk_plane_atomic_async_check(struct drm_plane *plane,
  84. struct drm_atomic_state *state)
  85. {
  86. struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
  87. plane);
  88. struct drm_crtc_state *crtc_state;
  89. int ret;
  90. if (plane != new_plane_state->crtc->cursor)
  91. return -EINVAL;
  92. if (!plane->state)
  93. return -EINVAL;
  94. if (!plane->state->fb)
  95. return -EINVAL;
  96. ret = mtk_crtc_plane_check(new_plane_state->crtc, plane,
  97. to_mtk_plane_state(new_plane_state));
  98. if (ret)
  99. return ret;
  100. crtc_state = drm_atomic_get_existing_crtc_state(state, new_plane_state->crtc);
  101. return drm_atomic_helper_check_plane_state(plane->state, crtc_state,
  102. DRM_PLANE_NO_SCALING,
  103. DRM_PLANE_NO_SCALING,
  104. true, true);
  105. }
  106. static void mtk_plane_update_new_state(struct drm_plane_state *new_state,
  107. struct mtk_plane_state *mtk_plane_state)
  108. {
  109. struct drm_framebuffer *fb = new_state->fb;
  110. struct drm_gem_object *gem;
  111. struct mtk_gem_obj *mtk_gem;
  112. unsigned int pitch, format;
  113. u64 modifier;
  114. dma_addr_t addr;
  115. dma_addr_t hdr_addr = 0;
  116. unsigned int hdr_pitch = 0;
  117. int offset;
  118. gem = fb->obj[0];
  119. mtk_gem = to_mtk_gem_obj(gem);
  120. addr = mtk_gem->dma_addr;
  121. pitch = fb->pitches[0];
  122. format = fb->format->format;
  123. modifier = fb->modifier;
  124. if (modifier == DRM_FORMAT_MOD_LINEAR) {
  125. /*
  126. * Using dma_addr_t variable to calculate with multiplier of different types,
  127. * for example: addr += (new_state->src.x1 >> 16) * fb->format->cpp[0];
  128. * may cause coverity issue with unintentional overflow.
  129. */
  130. offset = (new_state->src.x1 >> 16) * fb->format->cpp[0];
  131. addr += offset;
  132. offset = (new_state->src.y1 >> 16) * pitch;
  133. addr += offset;
  134. } else {
  135. int width_in_blocks = ALIGN(fb->width, AFBC_DATA_BLOCK_WIDTH)
  136. / AFBC_DATA_BLOCK_WIDTH;
  137. int height_in_blocks = ALIGN(fb->height, AFBC_DATA_BLOCK_HEIGHT)
  138. / AFBC_DATA_BLOCK_HEIGHT;
  139. int x_offset_in_blocks = (new_state->src.x1 >> 16) / AFBC_DATA_BLOCK_WIDTH;
  140. int y_offset_in_blocks = (new_state->src.y1 >> 16) / AFBC_DATA_BLOCK_HEIGHT;
  141. int hdr_size, hdr_offset;
  142. hdr_pitch = width_in_blocks * AFBC_HEADER_BLOCK_SIZE;
  143. pitch = width_in_blocks * AFBC_DATA_BLOCK_WIDTH *
  144. AFBC_DATA_BLOCK_HEIGHT * fb->format->cpp[0];
  145. hdr_size = ALIGN(hdr_pitch * height_in_blocks, AFBC_HEADER_ALIGNMENT);
  146. hdr_offset = hdr_pitch * y_offset_in_blocks +
  147. AFBC_HEADER_BLOCK_SIZE * x_offset_in_blocks;
  148. /*
  149. * Using dma_addr_t variable to calculate with multiplier of different types,
  150. * for example: addr += hdr_pitch * y_offset_in_blocks;
  151. * may cause coverity issue with unintentional overflow.
  152. */
  153. hdr_addr = addr + hdr_offset;
  154. /* The data plane is offset by 1 additional block. */
  155. offset = pitch * y_offset_in_blocks +
  156. AFBC_DATA_BLOCK_WIDTH * AFBC_DATA_BLOCK_HEIGHT *
  157. fb->format->cpp[0] * (x_offset_in_blocks + 1);
  158. /*
  159. * Using dma_addr_t variable to calculate with multiplier of different types,
  160. * for example: addr += pitch * y_offset_in_blocks;
  161. * may cause coverity issue with unintentional overflow.
  162. */
  163. addr = addr + hdr_size + offset;
  164. }
  165. mtk_plane_state->pending.enable = true;
  166. mtk_plane_state->pending.pitch = pitch;
  167. mtk_plane_state->pending.hdr_pitch = hdr_pitch;
  168. mtk_plane_state->pending.format = format;
  169. mtk_plane_state->pending.modifier = modifier;
  170. mtk_plane_state->pending.addr = addr;
  171. mtk_plane_state->pending.hdr_addr = hdr_addr;
  172. mtk_plane_state->pending.x = new_state->dst.x1;
  173. mtk_plane_state->pending.y = new_state->dst.y1;
  174. mtk_plane_state->pending.width = drm_rect_width(&new_state->dst);
  175. mtk_plane_state->pending.height = drm_rect_height(&new_state->dst);
  176. mtk_plane_state->pending.rotation = new_state->rotation;
  177. mtk_plane_state->pending.color_encoding = new_state->color_encoding;
  178. }
  179. static void mtk_plane_atomic_async_update(struct drm_plane *plane,
  180. struct drm_atomic_state *state)
  181. {
  182. struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
  183. plane);
  184. struct mtk_plane_state *new_plane_state = to_mtk_plane_state(plane->state);
  185. plane->state->crtc_x = new_state->crtc_x;
  186. plane->state->crtc_y = new_state->crtc_y;
  187. plane->state->crtc_h = new_state->crtc_h;
  188. plane->state->crtc_w = new_state->crtc_w;
  189. plane->state->src_x = new_state->src_x;
  190. plane->state->src_y = new_state->src_y;
  191. plane->state->src_h = new_state->src_h;
  192. plane->state->src_w = new_state->src_w;
  193. plane->state->dst.x1 = new_state->dst.x1;
  194. plane->state->dst.y1 = new_state->dst.y1;
  195. mtk_plane_update_new_state(new_state, new_plane_state);
  196. swap(plane->state->fb, new_state->fb);
  197. wmb(); /* Make sure the above parameters are set before update */
  198. new_plane_state->pending.async_dirty = true;
  199. mtk_crtc_async_update(new_state->crtc, plane, state);
  200. }
  201. static const struct drm_plane_funcs mtk_plane_funcs = {
  202. .update_plane = drm_atomic_helper_update_plane,
  203. .disable_plane = drm_atomic_helper_disable_plane,
  204. .destroy = drm_plane_cleanup,
  205. .reset = mtk_plane_reset,
  206. .atomic_duplicate_state = mtk_plane_duplicate_state,
  207. .atomic_destroy_state = mtk_plane_destroy_state,
  208. .format_mod_supported = mtk_plane_format_mod_supported,
  209. };
  210. static int mtk_plane_atomic_check(struct drm_plane *plane,
  211. struct drm_atomic_state *state)
  212. {
  213. struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
  214. plane);
  215. struct drm_framebuffer *fb = new_plane_state->fb;
  216. struct drm_crtc_state *crtc_state;
  217. int ret;
  218. if (!fb)
  219. return 0;
  220. if (WARN_ON(!new_plane_state->crtc))
  221. return 0;
  222. ret = mtk_crtc_plane_check(new_plane_state->crtc, plane,
  223. to_mtk_plane_state(new_plane_state));
  224. if (ret)
  225. return ret;
  226. crtc_state = drm_atomic_get_crtc_state(state,
  227. new_plane_state->crtc);
  228. if (IS_ERR(crtc_state))
  229. return PTR_ERR(crtc_state);
  230. return drm_atomic_helper_check_plane_state(new_plane_state,
  231. crtc_state,
  232. DRM_PLANE_NO_SCALING,
  233. DRM_PLANE_NO_SCALING,
  234. true, true);
  235. }
  236. static void mtk_plane_atomic_disable(struct drm_plane *plane,
  237. struct drm_atomic_state *state)
  238. {
  239. struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
  240. plane);
  241. struct mtk_plane_state *mtk_plane_state = to_mtk_plane_state(new_state);
  242. struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
  243. plane);
  244. mtk_plane_state->pending.enable = false;
  245. wmb(); /* Make sure the above parameter is set before update */
  246. mtk_plane_state->pending.dirty = true;
  247. if (old_state && old_state->crtc)
  248. mtk_crtc_plane_disable(old_state->crtc, plane);
  249. }
  250. static void mtk_plane_atomic_update(struct drm_plane *plane,
  251. struct drm_atomic_state *state)
  252. {
  253. struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
  254. plane);
  255. struct mtk_plane_state *mtk_plane_state = to_mtk_plane_state(new_state);
  256. if (!new_state->crtc || WARN_ON(!new_state->fb))
  257. return;
  258. if (!new_state->visible) {
  259. mtk_plane_atomic_disable(plane, state);
  260. return;
  261. }
  262. mtk_plane_update_new_state(new_state, mtk_plane_state);
  263. wmb(); /* Make sure the above parameters are set before update */
  264. mtk_plane_state->pending.dirty = true;
  265. }
  266. static const struct drm_plane_helper_funcs mtk_plane_helper_funcs = {
  267. .atomic_check = mtk_plane_atomic_check,
  268. .atomic_update = mtk_plane_atomic_update,
  269. .atomic_disable = mtk_plane_atomic_disable,
  270. .atomic_async_update = mtk_plane_atomic_async_update,
  271. .atomic_async_check = mtk_plane_atomic_async_check,
  272. };
  273. int mtk_plane_init(struct drm_device *dev, struct drm_plane *plane,
  274. unsigned long possible_crtcs, enum drm_plane_type type,
  275. unsigned int supported_rotations, const u32 blend_modes,
  276. const u32 *formats, size_t num_formats,
  277. bool supports_afbc, unsigned int plane_idx)
  278. {
  279. int err;
  280. if (!formats || !num_formats) {
  281. DRM_ERROR("no formats for plane\n");
  282. return -EINVAL;
  283. }
  284. err = drm_universal_plane_init(dev, plane, possible_crtcs,
  285. &mtk_plane_funcs, formats,
  286. num_formats,
  287. supports_afbc ? modifiers : NULL,
  288. type, NULL);
  289. if (err) {
  290. DRM_ERROR("failed to initialize plane\n");
  291. return err;
  292. }
  293. /*
  294. * The hardware does not support repositioning planes by muxing: their
  295. * Z-position is infact fixed and the only way to change the actual
  296. * order is to swap the contents of the entire register set of one
  297. * overlay with another, which may be more expensive than desired.
  298. *
  299. * With no repositioning, the caller of this function guarantees that
  300. * the plane_idx is correct. This means that, for example, the PRIMARY
  301. * plane fed to this function will always have plane_idx zero.
  302. */
  303. err = drm_plane_create_zpos_immutable_property(plane, plane_idx);
  304. if (err) {
  305. DRM_ERROR("Failed to create zpos property for plane %u\n", plane_idx);
  306. return err;
  307. }
  308. if (supported_rotations) {
  309. err = drm_plane_create_rotation_property(plane,
  310. DRM_MODE_ROTATE_0,
  311. supported_rotations);
  312. if (err)
  313. DRM_INFO("Create rotation property failed\n");
  314. }
  315. err = drm_plane_create_alpha_property(plane);
  316. if (err)
  317. DRM_ERROR("failed to create property: alpha\n");
  318. if (blend_modes) {
  319. err = drm_plane_create_blend_mode_property(plane, blend_modes);
  320. if (err)
  321. DRM_ERROR("failed to create property: blend_mode\n");
  322. }
  323. drm_plane_helper_add(plane, &mtk_plane_helper_funcs);
  324. return 0;
  325. }