vc4_plane.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. /* Offset in the dlist to pointer word 0. */
  30. u32 pw0_offset;
  31. /* Offset where the plane's dlist was last stored in the
  32. hardware at vc4_crtc_atomic_flush() time.
  33. */
  34. u32 *hw_dlist;
  35. };
  36. static inline struct vc4_plane_state *
  37. to_vc4_plane_state(struct drm_plane_state *state)
  38. {
  39. return (struct vc4_plane_state *)state;
  40. }
  41. static const struct hvs_format {
  42. u32 drm; /* DRM_FORMAT_* */
  43. u32 hvs; /* HVS_FORMAT_* */
  44. u32 pixel_order;
  45. bool has_alpha;
  46. } hvs_formats[] = {
  47. {
  48. .drm = DRM_FORMAT_XRGB8888, .hvs = HVS_PIXEL_FORMAT_RGBA8888,
  49. .pixel_order = HVS_PIXEL_ORDER_ABGR, .has_alpha = false,
  50. },
  51. {
  52. .drm = DRM_FORMAT_ARGB8888, .hvs = HVS_PIXEL_FORMAT_RGBA8888,
  53. .pixel_order = HVS_PIXEL_ORDER_ABGR, .has_alpha = true,
  54. },
  55. };
  56. static const struct hvs_format *vc4_get_hvs_format(u32 drm_format)
  57. {
  58. unsigned i;
  59. for (i = 0; i < ARRAY_SIZE(hvs_formats); i++) {
  60. if (hvs_formats[i].drm == drm_format)
  61. return &hvs_formats[i];
  62. }
  63. return NULL;
  64. }
  65. static bool plane_enabled(struct drm_plane_state *state)
  66. {
  67. return state->fb && state->crtc;
  68. }
  69. static struct drm_plane_state *vc4_plane_duplicate_state(struct drm_plane *plane)
  70. {
  71. struct vc4_plane_state *vc4_state;
  72. if (WARN_ON(!plane->state))
  73. return NULL;
  74. vc4_state = kmemdup(plane->state, sizeof(*vc4_state), GFP_KERNEL);
  75. if (!vc4_state)
  76. return NULL;
  77. __drm_atomic_helper_plane_duplicate_state(plane, &vc4_state->base);
  78. if (vc4_state->dlist) {
  79. vc4_state->dlist = kmemdup(vc4_state->dlist,
  80. vc4_state->dlist_count * 4,
  81. GFP_KERNEL);
  82. if (!vc4_state->dlist) {
  83. kfree(vc4_state);
  84. return NULL;
  85. }
  86. vc4_state->dlist_size = vc4_state->dlist_count;
  87. }
  88. return &vc4_state->base;
  89. }
  90. static void vc4_plane_destroy_state(struct drm_plane *plane,
  91. struct drm_plane_state *state)
  92. {
  93. struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
  94. kfree(vc4_state->dlist);
  95. __drm_atomic_helper_plane_destroy_state(plane, &vc4_state->base);
  96. kfree(state);
  97. }
  98. /* Called during init to allocate the plane's atomic state. */
  99. static void vc4_plane_reset(struct drm_plane *plane)
  100. {
  101. struct vc4_plane_state *vc4_state;
  102. WARN_ON(plane->state);
  103. vc4_state = kzalloc(sizeof(*vc4_state), GFP_KERNEL);
  104. if (!vc4_state)
  105. return;
  106. plane->state = &vc4_state->base;
  107. vc4_state->base.plane = plane;
  108. }
  109. static void vc4_dlist_write(struct vc4_plane_state *vc4_state, u32 val)
  110. {
  111. if (vc4_state->dlist_count == vc4_state->dlist_size) {
  112. u32 new_size = max(4u, vc4_state->dlist_count * 2);
  113. u32 *new_dlist = kmalloc(new_size * 4, GFP_KERNEL);
  114. if (!new_dlist)
  115. return;
  116. memcpy(new_dlist, vc4_state->dlist, vc4_state->dlist_count * 4);
  117. kfree(vc4_state->dlist);
  118. vc4_state->dlist = new_dlist;
  119. vc4_state->dlist_size = new_size;
  120. }
  121. vc4_state->dlist[vc4_state->dlist_count++] = val;
  122. }
  123. /* Writes out a full display list for an active plane to the plane's
  124. * private dlist state.
  125. */
  126. static int vc4_plane_mode_set(struct drm_plane *plane,
  127. struct drm_plane_state *state)
  128. {
  129. struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
  130. struct drm_framebuffer *fb = state->fb;
  131. struct drm_gem_cma_object *bo = drm_fb_cma_get_gem_obj(fb, 0);
  132. u32 ctl0_offset = vc4_state->dlist_count;
  133. const struct hvs_format *format = vc4_get_hvs_format(fb->pixel_format);
  134. uint32_t offset = fb->offsets[0];
  135. int crtc_x = state->crtc_x;
  136. int crtc_y = state->crtc_y;
  137. int crtc_w = state->crtc_w;
  138. int crtc_h = state->crtc_h;
  139. if (state->crtc_w << 16 != state->src_w ||
  140. state->crtc_h << 16 != state->src_h) {
  141. /* We don't support scaling yet, which involves
  142. * allocating the LBM memory for scaling temporary
  143. * storage, and putting filter kernels in the HVS
  144. * context.
  145. */
  146. return -EINVAL;
  147. }
  148. if (crtc_x < 0) {
  149. offset += drm_format_plane_cpp(fb->pixel_format, 0) * -crtc_x;
  150. crtc_w += crtc_x;
  151. crtc_x = 0;
  152. }
  153. if (crtc_y < 0) {
  154. offset += fb->pitches[0] * -crtc_y;
  155. crtc_h += crtc_y;
  156. crtc_y = 0;
  157. }
  158. vc4_dlist_write(vc4_state,
  159. SCALER_CTL0_VALID |
  160. (format->pixel_order << SCALER_CTL0_ORDER_SHIFT) |
  161. (format->hvs << SCALER_CTL0_PIXEL_FORMAT_SHIFT) |
  162. SCALER_CTL0_UNITY);
  163. /* Position Word 0: Image Positions and Alpha Value */
  164. vc4_dlist_write(vc4_state,
  165. VC4_SET_FIELD(0xff, SCALER_POS0_FIXED_ALPHA) |
  166. VC4_SET_FIELD(crtc_x, SCALER_POS0_START_X) |
  167. VC4_SET_FIELD(crtc_y, SCALER_POS0_START_Y));
  168. /* Position Word 1: Scaled Image Dimensions.
  169. * Skipped due to SCALER_CTL0_UNITY scaling.
  170. */
  171. /* Position Word 2: Source Image Size, Alpha Mode */
  172. vc4_dlist_write(vc4_state,
  173. VC4_SET_FIELD(format->has_alpha ?
  174. SCALER_POS2_ALPHA_MODE_PIPELINE :
  175. SCALER_POS2_ALPHA_MODE_FIXED,
  176. SCALER_POS2_ALPHA_MODE) |
  177. VC4_SET_FIELD(crtc_w, SCALER_POS2_WIDTH) |
  178. VC4_SET_FIELD(crtc_h, SCALER_POS2_HEIGHT));
  179. /* Position Word 3: Context. Written by the HVS. */
  180. vc4_dlist_write(vc4_state, 0xc0c0c0c0);
  181. vc4_state->pw0_offset = vc4_state->dlist_count;
  182. /* Pointer Word 0: RGB / Y Pointer */
  183. vc4_dlist_write(vc4_state, bo->paddr + offset);
  184. /* Pointer Context Word 0: Written by the HVS */
  185. vc4_dlist_write(vc4_state, 0xc0c0c0c0);
  186. /* Pitch word 0: Pointer 0 Pitch */
  187. vc4_dlist_write(vc4_state,
  188. VC4_SET_FIELD(fb->pitches[0], SCALER_SRC_PITCH));
  189. vc4_state->dlist[ctl0_offset] |=
  190. VC4_SET_FIELD(vc4_state->dlist_count, SCALER_CTL0_SIZE);
  191. return 0;
  192. }
  193. /* If a modeset involves changing the setup of a plane, the atomic
  194. * infrastructure will call this to validate a proposed plane setup.
  195. * However, if a plane isn't getting updated, this (and the
  196. * corresponding vc4_plane_atomic_update) won't get called. Thus, we
  197. * compute the dlist here and have all active plane dlists get updated
  198. * in the CRTC's flush.
  199. */
  200. static int vc4_plane_atomic_check(struct drm_plane *plane,
  201. struct drm_plane_state *state)
  202. {
  203. struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
  204. vc4_state->dlist_count = 0;
  205. if (plane_enabled(state))
  206. return vc4_plane_mode_set(plane, state);
  207. else
  208. return 0;
  209. }
  210. static void vc4_plane_atomic_update(struct drm_plane *plane,
  211. struct drm_plane_state *old_state)
  212. {
  213. /* No contents here. Since we don't know where in the CRTC's
  214. * dlist we should be stored, our dlist is uploaded to the
  215. * hardware with vc4_plane_write_dlist() at CRTC atomic_flush
  216. * time.
  217. */
  218. }
  219. u32 vc4_plane_write_dlist(struct drm_plane *plane, u32 __iomem *dlist)
  220. {
  221. struct vc4_plane_state *vc4_state = to_vc4_plane_state(plane->state);
  222. int i;
  223. vc4_state->hw_dlist = dlist;
  224. /* Can't memcpy_toio() because it needs to be 32-bit writes. */
  225. for (i = 0; i < vc4_state->dlist_count; i++)
  226. writel(vc4_state->dlist[i], &dlist[i]);
  227. return vc4_state->dlist_count;
  228. }
  229. u32 vc4_plane_dlist_size(struct drm_plane_state *state)
  230. {
  231. struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
  232. return vc4_state->dlist_count;
  233. }
  234. /* Updates the plane to immediately (well, once the FIFO needs
  235. * refilling) scan out from at a new framebuffer.
  236. */
  237. void vc4_plane_async_set_fb(struct drm_plane *plane, struct drm_framebuffer *fb)
  238. {
  239. struct vc4_plane_state *vc4_state = to_vc4_plane_state(plane->state);
  240. struct drm_gem_cma_object *bo = drm_fb_cma_get_gem_obj(fb, 0);
  241. uint32_t addr;
  242. /* We're skipping the address adjustment for negative origin,
  243. * because this is only called on the primary plane.
  244. */
  245. WARN_ON_ONCE(plane->state->crtc_x < 0 || plane->state->crtc_y < 0);
  246. addr = bo->paddr + fb->offsets[0];
  247. /* Write the new address into the hardware immediately. The
  248. * scanout will start from this address as soon as the FIFO
  249. * needs to refill with pixels.
  250. */
  251. writel(addr, &vc4_state->hw_dlist[vc4_state->pw0_offset]);
  252. /* Also update the CPU-side dlist copy, so that any later
  253. * atomic updates that don't do a new modeset on our plane
  254. * also use our updated address.
  255. */
  256. vc4_state->dlist[vc4_state->pw0_offset] = addr;
  257. }
  258. static const struct drm_plane_helper_funcs vc4_plane_helper_funcs = {
  259. .prepare_fb = NULL,
  260. .cleanup_fb = NULL,
  261. .atomic_check = vc4_plane_atomic_check,
  262. .atomic_update = vc4_plane_atomic_update,
  263. };
  264. static void vc4_plane_destroy(struct drm_plane *plane)
  265. {
  266. drm_plane_helper_disable(plane);
  267. drm_plane_cleanup(plane);
  268. }
  269. static const struct drm_plane_funcs vc4_plane_funcs = {
  270. .update_plane = drm_atomic_helper_update_plane,
  271. .disable_plane = drm_atomic_helper_disable_plane,
  272. .destroy = vc4_plane_destroy,
  273. .set_property = NULL,
  274. .reset = vc4_plane_reset,
  275. .atomic_duplicate_state = vc4_plane_duplicate_state,
  276. .atomic_destroy_state = vc4_plane_destroy_state,
  277. };
  278. struct drm_plane *vc4_plane_init(struct drm_device *dev,
  279. enum drm_plane_type type)
  280. {
  281. struct drm_plane *plane = NULL;
  282. struct vc4_plane *vc4_plane;
  283. u32 formats[ARRAY_SIZE(hvs_formats)];
  284. int ret = 0;
  285. unsigned i;
  286. vc4_plane = devm_kzalloc(dev->dev, sizeof(*vc4_plane),
  287. GFP_KERNEL);
  288. if (!vc4_plane) {
  289. ret = -ENOMEM;
  290. goto fail;
  291. }
  292. for (i = 0; i < ARRAY_SIZE(hvs_formats); i++)
  293. formats[i] = hvs_formats[i].drm;
  294. plane = &vc4_plane->base;
  295. ret = drm_universal_plane_init(dev, plane, 0xff,
  296. &vc4_plane_funcs,
  297. formats, ARRAY_SIZE(formats),
  298. type, NULL);
  299. drm_plane_helper_add(plane, &vc4_plane_helper_funcs);
  300. return plane;
  301. fail:
  302. if (plane)
  303. vc4_plane_destroy(plane);
  304. return ERR_PTR(ret);
  305. }