vc4_plane.c 10 KB

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