vc4_plane.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Copyright (C) 2015 Broadcom
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. /**
  9. * DOC: VC4 plane module
  10. *
  11. * Each DRM plane is a layer of pixels being scanned out by the HVS.
  12. *
  13. * At atomic modeset check time, we compute the HVS display element
  14. * state that would be necessary for displaying the plane (giving us a
  15. * chance to figure out if a plane configuration is invalid), then at
  16. * atomic flush time the CRTC will ask us to write our element state
  17. * into the region of the HVS that it has allocated for us.
  18. */
  19. #include "vc4_drv.h"
  20. #include "vc4_regs.h"
  21. #include "drm_atomic_helper.h"
  22. #include "drm_fb_cma_helper.h"
  23. #include "drm_plane_helper.h"
  24. struct vc4_plane_state {
  25. struct drm_plane_state base;
  26. u32 *dlist;
  27. u32 dlist_size; /* Number of dwords in allocated for the display list */
  28. u32 dlist_count; /* Number of used dwords in the display list. */
  29. };
  30. static inline struct vc4_plane_state *
  31. to_vc4_plane_state(struct drm_plane_state *state)
  32. {
  33. return (struct vc4_plane_state *)state;
  34. }
  35. static const struct hvs_format {
  36. u32 drm; /* DRM_FORMAT_* */
  37. u32 hvs; /* HVS_FORMAT_* */
  38. u32 pixel_order;
  39. bool has_alpha;
  40. } hvs_formats[] = {
  41. {
  42. .drm = DRM_FORMAT_XRGB8888, .hvs = HVS_PIXEL_FORMAT_RGBA8888,
  43. .pixel_order = HVS_PIXEL_ORDER_ABGR, .has_alpha = false,
  44. },
  45. {
  46. .drm = DRM_FORMAT_ARGB8888, .hvs = HVS_PIXEL_FORMAT_RGBA8888,
  47. .pixel_order = HVS_PIXEL_ORDER_ABGR, .has_alpha = true,
  48. },
  49. };
  50. static const struct hvs_format *vc4_get_hvs_format(u32 drm_format)
  51. {
  52. unsigned i;
  53. for (i = 0; i < ARRAY_SIZE(hvs_formats); i++) {
  54. if (hvs_formats[i].drm == drm_format)
  55. return &hvs_formats[i];
  56. }
  57. return NULL;
  58. }
  59. static bool plane_enabled(struct drm_plane_state *state)
  60. {
  61. return state->fb && state->crtc;
  62. }
  63. static struct drm_plane_state *vc4_plane_duplicate_state(struct drm_plane *plane)
  64. {
  65. struct vc4_plane_state *vc4_state;
  66. if (WARN_ON(!plane->state))
  67. return NULL;
  68. vc4_state = kmemdup(plane->state, sizeof(*vc4_state), GFP_KERNEL);
  69. if (!vc4_state)
  70. return NULL;
  71. __drm_atomic_helper_plane_duplicate_state(plane, &vc4_state->base);
  72. if (vc4_state->dlist) {
  73. vc4_state->dlist = kmemdup(vc4_state->dlist,
  74. vc4_state->dlist_count * 4,
  75. GFP_KERNEL);
  76. if (!vc4_state->dlist) {
  77. kfree(vc4_state);
  78. return NULL;
  79. }
  80. vc4_state->dlist_size = vc4_state->dlist_count;
  81. }
  82. return &vc4_state->base;
  83. }
  84. static void vc4_plane_destroy_state(struct drm_plane *plane,
  85. struct drm_plane_state *state)
  86. {
  87. struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
  88. kfree(vc4_state->dlist);
  89. __drm_atomic_helper_plane_destroy_state(plane, &vc4_state->base);
  90. kfree(state);
  91. }
  92. /* Called during init to allocate the plane's atomic state. */
  93. static void vc4_plane_reset(struct drm_plane *plane)
  94. {
  95. struct vc4_plane_state *vc4_state;
  96. WARN_ON(plane->state);
  97. vc4_state = kzalloc(sizeof(*vc4_state), GFP_KERNEL);
  98. if (!vc4_state)
  99. return;
  100. plane->state = &vc4_state->base;
  101. vc4_state->base.plane = plane;
  102. }
  103. static void vc4_dlist_write(struct vc4_plane_state *vc4_state, u32 val)
  104. {
  105. if (vc4_state->dlist_count == vc4_state->dlist_size) {
  106. u32 new_size = max(4u, vc4_state->dlist_count * 2);
  107. u32 *new_dlist = kmalloc(new_size * 4, GFP_KERNEL);
  108. if (!new_dlist)
  109. return;
  110. memcpy(new_dlist, vc4_state->dlist, vc4_state->dlist_count * 4);
  111. kfree(vc4_state->dlist);
  112. vc4_state->dlist = new_dlist;
  113. vc4_state->dlist_size = new_size;
  114. }
  115. vc4_state->dlist[vc4_state->dlist_count++] = val;
  116. }
  117. /* Writes out a full display list for an active plane to the plane's
  118. * private dlist state.
  119. */
  120. static int vc4_plane_mode_set(struct drm_plane *plane,
  121. struct drm_plane_state *state)
  122. {
  123. struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
  124. struct drm_framebuffer *fb = state->fb;
  125. struct drm_gem_cma_object *bo = drm_fb_cma_get_gem_obj(fb, 0);
  126. u32 ctl0_offset = vc4_state->dlist_count;
  127. const struct hvs_format *format = vc4_get_hvs_format(fb->pixel_format);
  128. uint32_t offset = fb->offsets[0];
  129. int crtc_x = state->crtc_x;
  130. int crtc_y = state->crtc_y;
  131. int crtc_w = state->crtc_w;
  132. int crtc_h = state->crtc_h;
  133. if (state->crtc_w << 16 != state->src_w ||
  134. state->crtc_h << 16 != state->src_h) {
  135. /* We don't support scaling yet, which involves
  136. * allocating the LBM memory for scaling temporary
  137. * storage, and putting filter kernels in the HVS
  138. * context.
  139. */
  140. return -EINVAL;
  141. }
  142. if (crtc_x < 0) {
  143. offset += drm_format_plane_cpp(fb->pixel_format, 0) * -crtc_x;
  144. crtc_w += crtc_x;
  145. crtc_x = 0;
  146. }
  147. if (crtc_y < 0) {
  148. offset += fb->pitches[0] * -crtc_y;
  149. crtc_h += crtc_y;
  150. crtc_y = 0;
  151. }
  152. vc4_dlist_write(vc4_state,
  153. SCALER_CTL0_VALID |
  154. (format->pixel_order << SCALER_CTL0_ORDER_SHIFT) |
  155. (format->hvs << SCALER_CTL0_PIXEL_FORMAT_SHIFT) |
  156. SCALER_CTL0_UNITY);
  157. /* Position Word 0: Image Positions and Alpha Value */
  158. vc4_dlist_write(vc4_state,
  159. VC4_SET_FIELD(0xff, SCALER_POS0_FIXED_ALPHA) |
  160. VC4_SET_FIELD(crtc_x, SCALER_POS0_START_X) |
  161. VC4_SET_FIELD(crtc_y, SCALER_POS0_START_Y));
  162. /* Position Word 1: Scaled Image Dimensions.
  163. * Skipped due to SCALER_CTL0_UNITY scaling.
  164. */
  165. /* Position Word 2: Source Image Size, Alpha Mode */
  166. vc4_dlist_write(vc4_state,
  167. VC4_SET_FIELD(format->has_alpha ?
  168. SCALER_POS2_ALPHA_MODE_PIPELINE :
  169. SCALER_POS2_ALPHA_MODE_FIXED,
  170. SCALER_POS2_ALPHA_MODE) |
  171. VC4_SET_FIELD(crtc_w, SCALER_POS2_WIDTH) |
  172. VC4_SET_FIELD(crtc_h, SCALER_POS2_HEIGHT));
  173. /* Position Word 3: Context. Written by the HVS. */
  174. vc4_dlist_write(vc4_state, 0xc0c0c0c0);
  175. /* Pointer Word 0: RGB / Y Pointer */
  176. vc4_dlist_write(vc4_state, bo->paddr + offset);
  177. /* Pointer Context Word 0: Written by the HVS */
  178. vc4_dlist_write(vc4_state, 0xc0c0c0c0);
  179. /* Pitch word 0: Pointer 0 Pitch */
  180. vc4_dlist_write(vc4_state,
  181. VC4_SET_FIELD(fb->pitches[0], SCALER_SRC_PITCH));
  182. vc4_state->dlist[ctl0_offset] |=
  183. VC4_SET_FIELD(vc4_state->dlist_count, SCALER_CTL0_SIZE);
  184. return 0;
  185. }
  186. /* If a modeset involves changing the setup of a plane, the atomic
  187. * infrastructure will call this to validate a proposed plane setup.
  188. * However, if a plane isn't getting updated, this (and the
  189. * corresponding vc4_plane_atomic_update) won't get called. Thus, we
  190. * compute the dlist here and have all active plane dlists get updated
  191. * in the CRTC's flush.
  192. */
  193. static int vc4_plane_atomic_check(struct drm_plane *plane,
  194. struct drm_plane_state *state)
  195. {
  196. struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
  197. vc4_state->dlist_count = 0;
  198. if (plane_enabled(state))
  199. return vc4_plane_mode_set(plane, state);
  200. else
  201. return 0;
  202. }
  203. static void vc4_plane_atomic_update(struct drm_plane *plane,
  204. struct drm_plane_state *old_state)
  205. {
  206. /* No contents here. Since we don't know where in the CRTC's
  207. * dlist we should be stored, our dlist is uploaded to the
  208. * hardware with vc4_plane_write_dlist() at CRTC atomic_flush
  209. * time.
  210. */
  211. }
  212. u32 vc4_plane_write_dlist(struct drm_plane *plane, u32 __iomem *dlist)
  213. {
  214. struct vc4_plane_state *vc4_state = to_vc4_plane_state(plane->state);
  215. int i;
  216. /* Can't memcpy_toio() because it needs to be 32-bit writes. */
  217. for (i = 0; i < vc4_state->dlist_count; i++)
  218. writel(vc4_state->dlist[i], &dlist[i]);
  219. return vc4_state->dlist_count;
  220. }
  221. u32 vc4_plane_dlist_size(struct drm_plane_state *state)
  222. {
  223. struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
  224. return vc4_state->dlist_count;
  225. }
  226. static const struct drm_plane_helper_funcs vc4_plane_helper_funcs = {
  227. .prepare_fb = NULL,
  228. .cleanup_fb = NULL,
  229. .atomic_check = vc4_plane_atomic_check,
  230. .atomic_update = vc4_plane_atomic_update,
  231. };
  232. static void vc4_plane_destroy(struct drm_plane *plane)
  233. {
  234. drm_plane_helper_disable(plane);
  235. drm_plane_cleanup(plane);
  236. }
  237. static const struct drm_plane_funcs vc4_plane_funcs = {
  238. .update_plane = drm_atomic_helper_update_plane,
  239. .disable_plane = drm_atomic_helper_disable_plane,
  240. .destroy = vc4_plane_destroy,
  241. .set_property = NULL,
  242. .reset = vc4_plane_reset,
  243. .atomic_duplicate_state = vc4_plane_duplicate_state,
  244. .atomic_destroy_state = vc4_plane_destroy_state,
  245. };
  246. struct drm_plane *vc4_plane_init(struct drm_device *dev,
  247. enum drm_plane_type type)
  248. {
  249. struct drm_plane *plane = NULL;
  250. struct vc4_plane *vc4_plane;
  251. u32 formats[ARRAY_SIZE(hvs_formats)];
  252. int ret = 0;
  253. unsigned i;
  254. vc4_plane = devm_kzalloc(dev->dev, sizeof(*vc4_plane),
  255. GFP_KERNEL);
  256. if (!vc4_plane) {
  257. ret = -ENOMEM;
  258. goto fail;
  259. }
  260. for (i = 0; i < ARRAY_SIZE(hvs_formats); i++)
  261. formats[i] = hvs_formats[i].drm;
  262. plane = &vc4_plane->base;
  263. ret = drm_universal_plane_init(dev, plane, 0xff,
  264. &vc4_plane_funcs,
  265. formats, ARRAY_SIZE(formats),
  266. type);
  267. drm_plane_helper_add(plane, &vc4_plane_helper_funcs);
  268. return plane;
  269. fail:
  270. if (plane)
  271. vc4_plane_destroy(plane);
  272. return ERR_PTR(ret);
  273. }