vc4_plane.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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. enum vc4_scaling_mode {
  25. VC4_SCALING_NONE,
  26. VC4_SCALING_TPZ,
  27. VC4_SCALING_PPF,
  28. };
  29. struct vc4_plane_state {
  30. struct drm_plane_state base;
  31. /* System memory copy of the display list for this element, computed
  32. * at atomic_check time.
  33. */
  34. u32 *dlist;
  35. u32 dlist_size; /* Number of dwords allocated for the display list */
  36. u32 dlist_count; /* Number of used dwords in the display list. */
  37. /* Offset in the dlist to various words, for pageflip or
  38. * cursor updates.
  39. */
  40. u32 pos0_offset;
  41. u32 pos2_offset;
  42. u32 ptr0_offset;
  43. /* Offset where the plane's dlist was last stored in the
  44. * hardware at vc4_crtc_atomic_flush() time.
  45. */
  46. u32 __iomem *hw_dlist;
  47. /* Clipped coordinates of the plane on the display. */
  48. int crtc_x, crtc_y, crtc_w, crtc_h;
  49. /* Clipped area being scanned from in the FB. */
  50. u32 src_x, src_y;
  51. u32 src_w[2], src_h[2];
  52. /* Scaling selection for the RGB/Y plane and the Cb/Cr planes. */
  53. enum vc4_scaling_mode x_scaling[2], y_scaling[2];
  54. bool is_unity;
  55. bool is_yuv;
  56. /* Offset to start scanning out from the start of the plane's
  57. * BO.
  58. */
  59. u32 offsets[3];
  60. /* Our allocation in LBM for temporary storage during scaling. */
  61. struct drm_mm_node lbm;
  62. };
  63. static inline struct vc4_plane_state *
  64. to_vc4_plane_state(struct drm_plane_state *state)
  65. {
  66. return (struct vc4_plane_state *)state;
  67. }
  68. static const struct hvs_format {
  69. u32 drm; /* DRM_FORMAT_* */
  70. u32 hvs; /* HVS_FORMAT_* */
  71. u32 pixel_order;
  72. bool has_alpha;
  73. bool flip_cbcr;
  74. } hvs_formats[] = {
  75. {
  76. .drm = DRM_FORMAT_XRGB8888, .hvs = HVS_PIXEL_FORMAT_RGBA8888,
  77. .pixel_order = HVS_PIXEL_ORDER_ABGR, .has_alpha = false,
  78. },
  79. {
  80. .drm = DRM_FORMAT_ARGB8888, .hvs = HVS_PIXEL_FORMAT_RGBA8888,
  81. .pixel_order = HVS_PIXEL_ORDER_ABGR, .has_alpha = true,
  82. },
  83. {
  84. .drm = DRM_FORMAT_RGB565, .hvs = HVS_PIXEL_FORMAT_RGB565,
  85. .pixel_order = HVS_PIXEL_ORDER_XRGB, .has_alpha = false,
  86. },
  87. {
  88. .drm = DRM_FORMAT_BGR565, .hvs = HVS_PIXEL_FORMAT_RGB565,
  89. .pixel_order = HVS_PIXEL_ORDER_XBGR, .has_alpha = false,
  90. },
  91. {
  92. .drm = DRM_FORMAT_ARGB1555, .hvs = HVS_PIXEL_FORMAT_RGBA5551,
  93. .pixel_order = HVS_PIXEL_ORDER_ABGR, .has_alpha = true,
  94. },
  95. {
  96. .drm = DRM_FORMAT_XRGB1555, .hvs = HVS_PIXEL_FORMAT_RGBA5551,
  97. .pixel_order = HVS_PIXEL_ORDER_ABGR, .has_alpha = false,
  98. },
  99. {
  100. .drm = DRM_FORMAT_YUV422,
  101. .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_3PLANE,
  102. },
  103. {
  104. .drm = DRM_FORMAT_YVU422,
  105. .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_3PLANE,
  106. .flip_cbcr = true,
  107. },
  108. {
  109. .drm = DRM_FORMAT_YUV420,
  110. .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV420_3PLANE,
  111. },
  112. {
  113. .drm = DRM_FORMAT_YVU420,
  114. .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV420_3PLANE,
  115. .flip_cbcr = true,
  116. },
  117. {
  118. .drm = DRM_FORMAT_NV12,
  119. .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV420_2PLANE,
  120. },
  121. {
  122. .drm = DRM_FORMAT_NV16,
  123. .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_2PLANE,
  124. },
  125. };
  126. static const struct hvs_format *vc4_get_hvs_format(u32 drm_format)
  127. {
  128. unsigned i;
  129. for (i = 0; i < ARRAY_SIZE(hvs_formats); i++) {
  130. if (hvs_formats[i].drm == drm_format)
  131. return &hvs_formats[i];
  132. }
  133. return NULL;
  134. }
  135. static enum vc4_scaling_mode vc4_get_scaling_mode(u32 src, u32 dst)
  136. {
  137. if (dst > src)
  138. return VC4_SCALING_PPF;
  139. else if (dst < src)
  140. return VC4_SCALING_TPZ;
  141. else
  142. return VC4_SCALING_NONE;
  143. }
  144. static bool plane_enabled(struct drm_plane_state *state)
  145. {
  146. return state->fb && state->crtc;
  147. }
  148. static struct drm_plane_state *vc4_plane_duplicate_state(struct drm_plane *plane)
  149. {
  150. struct vc4_plane_state *vc4_state;
  151. if (WARN_ON(!plane->state))
  152. return NULL;
  153. vc4_state = kmemdup(plane->state, sizeof(*vc4_state), GFP_KERNEL);
  154. if (!vc4_state)
  155. return NULL;
  156. memset(&vc4_state->lbm, 0, sizeof(vc4_state->lbm));
  157. __drm_atomic_helper_plane_duplicate_state(plane, &vc4_state->base);
  158. if (vc4_state->dlist) {
  159. vc4_state->dlist = kmemdup(vc4_state->dlist,
  160. vc4_state->dlist_count * 4,
  161. GFP_KERNEL);
  162. if (!vc4_state->dlist) {
  163. kfree(vc4_state);
  164. return NULL;
  165. }
  166. vc4_state->dlist_size = vc4_state->dlist_count;
  167. }
  168. return &vc4_state->base;
  169. }
  170. static void vc4_plane_destroy_state(struct drm_plane *plane,
  171. struct drm_plane_state *state)
  172. {
  173. struct vc4_dev *vc4 = to_vc4_dev(plane->dev);
  174. struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
  175. if (vc4_state->lbm.allocated) {
  176. unsigned long irqflags;
  177. spin_lock_irqsave(&vc4->hvs->mm_lock, irqflags);
  178. drm_mm_remove_node(&vc4_state->lbm);
  179. spin_unlock_irqrestore(&vc4->hvs->mm_lock, irqflags);
  180. }
  181. kfree(vc4_state->dlist);
  182. __drm_atomic_helper_plane_destroy_state(plane, &vc4_state->base);
  183. kfree(state);
  184. }
  185. /* Called during init to allocate the plane's atomic state. */
  186. static void vc4_plane_reset(struct drm_plane *plane)
  187. {
  188. struct vc4_plane_state *vc4_state;
  189. WARN_ON(plane->state);
  190. vc4_state = kzalloc(sizeof(*vc4_state), GFP_KERNEL);
  191. if (!vc4_state)
  192. return;
  193. plane->state = &vc4_state->base;
  194. vc4_state->base.plane = plane;
  195. }
  196. static void vc4_dlist_write(struct vc4_plane_state *vc4_state, u32 val)
  197. {
  198. if (vc4_state->dlist_count == vc4_state->dlist_size) {
  199. u32 new_size = max(4u, vc4_state->dlist_count * 2);
  200. u32 *new_dlist = kmalloc(new_size * 4, GFP_KERNEL);
  201. if (!new_dlist)
  202. return;
  203. memcpy(new_dlist, vc4_state->dlist, vc4_state->dlist_count * 4);
  204. kfree(vc4_state->dlist);
  205. vc4_state->dlist = new_dlist;
  206. vc4_state->dlist_size = new_size;
  207. }
  208. vc4_state->dlist[vc4_state->dlist_count++] = val;
  209. }
  210. /* Returns the scl0/scl1 field based on whether the dimensions need to
  211. * be up/down/non-scaled.
  212. *
  213. * This is a replication of a table from the spec.
  214. */
  215. static u32 vc4_get_scl_field(struct drm_plane_state *state, int plane)
  216. {
  217. struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
  218. switch (vc4_state->x_scaling[plane] << 2 | vc4_state->y_scaling[plane]) {
  219. case VC4_SCALING_PPF << 2 | VC4_SCALING_PPF:
  220. return SCALER_CTL0_SCL_H_PPF_V_PPF;
  221. case VC4_SCALING_TPZ << 2 | VC4_SCALING_PPF:
  222. return SCALER_CTL0_SCL_H_TPZ_V_PPF;
  223. case VC4_SCALING_PPF << 2 | VC4_SCALING_TPZ:
  224. return SCALER_CTL0_SCL_H_PPF_V_TPZ;
  225. case VC4_SCALING_TPZ << 2 | VC4_SCALING_TPZ:
  226. return SCALER_CTL0_SCL_H_TPZ_V_TPZ;
  227. case VC4_SCALING_PPF << 2 | VC4_SCALING_NONE:
  228. return SCALER_CTL0_SCL_H_PPF_V_NONE;
  229. case VC4_SCALING_NONE << 2 | VC4_SCALING_PPF:
  230. return SCALER_CTL0_SCL_H_NONE_V_PPF;
  231. case VC4_SCALING_NONE << 2 | VC4_SCALING_TPZ:
  232. return SCALER_CTL0_SCL_H_NONE_V_TPZ;
  233. case VC4_SCALING_TPZ << 2 | VC4_SCALING_NONE:
  234. return SCALER_CTL0_SCL_H_TPZ_V_NONE;
  235. default:
  236. case VC4_SCALING_NONE << 2 | VC4_SCALING_NONE:
  237. /* The unity case is independently handled by
  238. * SCALER_CTL0_UNITY.
  239. */
  240. return 0;
  241. }
  242. }
  243. static int vc4_plane_setup_clipping_and_scaling(struct drm_plane_state *state)
  244. {
  245. struct drm_plane *plane = state->plane;
  246. struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
  247. struct drm_framebuffer *fb = state->fb;
  248. struct drm_gem_cma_object *bo = drm_fb_cma_get_gem_obj(fb, 0);
  249. u32 subpixel_src_mask = (1 << 16) - 1;
  250. u32 format = fb->pixel_format;
  251. int num_planes = drm_format_num_planes(format);
  252. u32 h_subsample = 1;
  253. u32 v_subsample = 1;
  254. int i;
  255. for (i = 0; i < num_planes; i++)
  256. vc4_state->offsets[i] = bo->paddr + fb->offsets[i];
  257. /* We don't support subpixel source positioning for scaling. */
  258. if ((state->src_x & subpixel_src_mask) ||
  259. (state->src_y & subpixel_src_mask) ||
  260. (state->src_w & subpixel_src_mask) ||
  261. (state->src_h & subpixel_src_mask)) {
  262. return -EINVAL;
  263. }
  264. vc4_state->src_x = state->src_x >> 16;
  265. vc4_state->src_y = state->src_y >> 16;
  266. vc4_state->src_w[0] = state->src_w >> 16;
  267. vc4_state->src_h[0] = state->src_h >> 16;
  268. vc4_state->crtc_x = state->crtc_x;
  269. vc4_state->crtc_y = state->crtc_y;
  270. vc4_state->crtc_w = state->crtc_w;
  271. vc4_state->crtc_h = state->crtc_h;
  272. vc4_state->x_scaling[0] = vc4_get_scaling_mode(vc4_state->src_w[0],
  273. vc4_state->crtc_w);
  274. vc4_state->y_scaling[0] = vc4_get_scaling_mode(vc4_state->src_h[0],
  275. vc4_state->crtc_h);
  276. if (num_planes > 1) {
  277. vc4_state->is_yuv = true;
  278. h_subsample = drm_format_horz_chroma_subsampling(format);
  279. v_subsample = drm_format_vert_chroma_subsampling(format);
  280. vc4_state->src_w[1] = vc4_state->src_w[0] / h_subsample;
  281. vc4_state->src_h[1] = vc4_state->src_h[0] / v_subsample;
  282. vc4_state->x_scaling[1] =
  283. vc4_get_scaling_mode(vc4_state->src_w[1],
  284. vc4_state->crtc_w);
  285. vc4_state->y_scaling[1] =
  286. vc4_get_scaling_mode(vc4_state->src_h[1],
  287. vc4_state->crtc_h);
  288. /* YUV conversion requires that scaling be enabled,
  289. * even on a plane that's otherwise 1:1. Choose TPZ
  290. * for simplicity.
  291. */
  292. if (vc4_state->x_scaling[0] == VC4_SCALING_NONE)
  293. vc4_state->x_scaling[0] = VC4_SCALING_TPZ;
  294. if (vc4_state->y_scaling[0] == VC4_SCALING_NONE)
  295. vc4_state->y_scaling[0] = VC4_SCALING_TPZ;
  296. }
  297. vc4_state->is_unity = (vc4_state->x_scaling[0] == VC4_SCALING_NONE &&
  298. vc4_state->y_scaling[0] == VC4_SCALING_NONE &&
  299. vc4_state->x_scaling[1] == VC4_SCALING_NONE &&
  300. vc4_state->y_scaling[1] == VC4_SCALING_NONE);
  301. /* No configuring scaling on the cursor plane, since it gets
  302. non-vblank-synced updates, and scaling requires requires
  303. LBM changes which have to be vblank-synced.
  304. */
  305. if (plane->type == DRM_PLANE_TYPE_CURSOR && !vc4_state->is_unity)
  306. return -EINVAL;
  307. /* Clamp the on-screen start x/y to 0. The hardware doesn't
  308. * support negative y, and negative x wastes bandwidth.
  309. */
  310. if (vc4_state->crtc_x < 0) {
  311. for (i = 0; i < num_planes; i++) {
  312. u32 cpp = drm_format_plane_cpp(fb->pixel_format, i);
  313. u32 subs = ((i == 0) ? 1 : h_subsample);
  314. vc4_state->offsets[i] += (cpp *
  315. (-vc4_state->crtc_x) / subs);
  316. }
  317. vc4_state->src_w[0] += vc4_state->crtc_x;
  318. vc4_state->src_w[1] += vc4_state->crtc_x / h_subsample;
  319. vc4_state->crtc_x = 0;
  320. }
  321. if (vc4_state->crtc_y < 0) {
  322. for (i = 0; i < num_planes; i++) {
  323. u32 subs = ((i == 0) ? 1 : v_subsample);
  324. vc4_state->offsets[i] += (fb->pitches[i] *
  325. (-vc4_state->crtc_y) / subs);
  326. }
  327. vc4_state->src_h[0] += vc4_state->crtc_y;
  328. vc4_state->src_h[1] += vc4_state->crtc_y / v_subsample;
  329. vc4_state->crtc_y = 0;
  330. }
  331. return 0;
  332. }
  333. static void vc4_write_tpz(struct vc4_plane_state *vc4_state, u32 src, u32 dst)
  334. {
  335. u32 scale, recip;
  336. scale = (1 << 16) * src / dst;
  337. /* The specs note that while the reciprocal would be defined
  338. * as (1<<32)/scale, ~0 is close enough.
  339. */
  340. recip = ~0 / scale;
  341. vc4_dlist_write(vc4_state,
  342. VC4_SET_FIELD(scale, SCALER_TPZ0_SCALE) |
  343. VC4_SET_FIELD(0, SCALER_TPZ0_IPHASE));
  344. vc4_dlist_write(vc4_state,
  345. VC4_SET_FIELD(recip, SCALER_TPZ1_RECIP));
  346. }
  347. static void vc4_write_ppf(struct vc4_plane_state *vc4_state, u32 src, u32 dst)
  348. {
  349. u32 scale = (1 << 16) * src / dst;
  350. vc4_dlist_write(vc4_state,
  351. SCALER_PPF_AGC |
  352. VC4_SET_FIELD(scale, SCALER_PPF_SCALE) |
  353. VC4_SET_FIELD(0, SCALER_PPF_IPHASE));
  354. }
  355. static u32 vc4_lbm_size(struct drm_plane_state *state)
  356. {
  357. struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
  358. /* This is the worst case number. One of the two sizes will
  359. * be used depending on the scaling configuration.
  360. */
  361. u32 pix_per_line = max(vc4_state->src_w[0], (u32)vc4_state->crtc_w);
  362. u32 lbm;
  363. if (!vc4_state->is_yuv) {
  364. if (vc4_state->is_unity)
  365. return 0;
  366. else if (vc4_state->y_scaling[0] == VC4_SCALING_TPZ)
  367. lbm = pix_per_line * 8;
  368. else {
  369. /* In special cases, this multiplier might be 12. */
  370. lbm = pix_per_line * 16;
  371. }
  372. } else {
  373. /* There are cases for this going down to a multiplier
  374. * of 2, but according to the firmware source, the
  375. * table in the docs is somewhat wrong.
  376. */
  377. lbm = pix_per_line * 16;
  378. }
  379. lbm = roundup(lbm, 32);
  380. return lbm;
  381. }
  382. static void vc4_write_scaling_parameters(struct drm_plane_state *state,
  383. int channel)
  384. {
  385. struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
  386. /* Ch0 H-PPF Word 0: Scaling Parameters */
  387. if (vc4_state->x_scaling[channel] == VC4_SCALING_PPF) {
  388. vc4_write_ppf(vc4_state,
  389. vc4_state->src_w[channel], vc4_state->crtc_w);
  390. }
  391. /* Ch0 V-PPF Words 0-1: Scaling Parameters, Context */
  392. if (vc4_state->y_scaling[channel] == VC4_SCALING_PPF) {
  393. vc4_write_ppf(vc4_state,
  394. vc4_state->src_h[channel], vc4_state->crtc_h);
  395. vc4_dlist_write(vc4_state, 0xc0c0c0c0);
  396. }
  397. /* Ch0 H-TPZ Words 0-1: Scaling Parameters, Recip */
  398. if (vc4_state->x_scaling[channel] == VC4_SCALING_TPZ) {
  399. vc4_write_tpz(vc4_state,
  400. vc4_state->src_w[channel], vc4_state->crtc_w);
  401. }
  402. /* Ch0 V-TPZ Words 0-2: Scaling Parameters, Recip, Context */
  403. if (vc4_state->y_scaling[channel] == VC4_SCALING_TPZ) {
  404. vc4_write_tpz(vc4_state,
  405. vc4_state->src_h[channel], vc4_state->crtc_h);
  406. vc4_dlist_write(vc4_state, 0xc0c0c0c0);
  407. }
  408. }
  409. /* Writes out a full display list for an active plane to the plane's
  410. * private dlist state.
  411. */
  412. static int vc4_plane_mode_set(struct drm_plane *plane,
  413. struct drm_plane_state *state)
  414. {
  415. struct vc4_dev *vc4 = to_vc4_dev(plane->dev);
  416. struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
  417. struct drm_framebuffer *fb = state->fb;
  418. u32 ctl0_offset = vc4_state->dlist_count;
  419. const struct hvs_format *format = vc4_get_hvs_format(fb->pixel_format);
  420. int num_planes = drm_format_num_planes(format->drm);
  421. u32 scl0, scl1;
  422. u32 lbm_size;
  423. unsigned long irqflags;
  424. int ret, i;
  425. ret = vc4_plane_setup_clipping_and_scaling(state);
  426. if (ret)
  427. return ret;
  428. /* Allocate the LBM memory that the HVS will use for temporary
  429. * storage due to our scaling/format conversion.
  430. */
  431. lbm_size = vc4_lbm_size(state);
  432. if (lbm_size) {
  433. if (!vc4_state->lbm.allocated) {
  434. spin_lock_irqsave(&vc4->hvs->mm_lock, irqflags);
  435. ret = drm_mm_insert_node(&vc4->hvs->lbm_mm,
  436. &vc4_state->lbm,
  437. lbm_size, 32, 0);
  438. spin_unlock_irqrestore(&vc4->hvs->mm_lock, irqflags);
  439. } else {
  440. WARN_ON_ONCE(lbm_size != vc4_state->lbm.size);
  441. }
  442. }
  443. if (ret)
  444. return ret;
  445. /* SCL1 is used for Cb/Cr scaling of planar formats. For RGB
  446. * and 4:4:4, scl1 should be set to scl0 so both channels of
  447. * the scaler do the same thing. For YUV, the Y plane needs
  448. * to be put in channel 1 and Cb/Cr in channel 0, so we swap
  449. * the scl fields here.
  450. */
  451. if (num_planes == 1) {
  452. scl0 = vc4_get_scl_field(state, 1);
  453. scl1 = scl0;
  454. } else {
  455. scl0 = vc4_get_scl_field(state, 1);
  456. scl1 = vc4_get_scl_field(state, 0);
  457. }
  458. /* Control word */
  459. vc4_dlist_write(vc4_state,
  460. SCALER_CTL0_VALID |
  461. (format->pixel_order << SCALER_CTL0_ORDER_SHIFT) |
  462. (format->hvs << SCALER_CTL0_PIXEL_FORMAT_SHIFT) |
  463. (vc4_state->is_unity ? SCALER_CTL0_UNITY : 0) |
  464. VC4_SET_FIELD(scl0, SCALER_CTL0_SCL0) |
  465. VC4_SET_FIELD(scl1, SCALER_CTL0_SCL1));
  466. /* Position Word 0: Image Positions and Alpha Value */
  467. vc4_state->pos0_offset = vc4_state->dlist_count;
  468. vc4_dlist_write(vc4_state,
  469. VC4_SET_FIELD(0xff, SCALER_POS0_FIXED_ALPHA) |
  470. VC4_SET_FIELD(vc4_state->crtc_x, SCALER_POS0_START_X) |
  471. VC4_SET_FIELD(vc4_state->crtc_y, SCALER_POS0_START_Y));
  472. /* Position Word 1: Scaled Image Dimensions. */
  473. if (!vc4_state->is_unity) {
  474. vc4_dlist_write(vc4_state,
  475. VC4_SET_FIELD(vc4_state->crtc_w,
  476. SCALER_POS1_SCL_WIDTH) |
  477. VC4_SET_FIELD(vc4_state->crtc_h,
  478. SCALER_POS1_SCL_HEIGHT));
  479. }
  480. /* Position Word 2: Source Image Size, Alpha Mode */
  481. vc4_state->pos2_offset = vc4_state->dlist_count;
  482. vc4_dlist_write(vc4_state,
  483. VC4_SET_FIELD(format->has_alpha ?
  484. SCALER_POS2_ALPHA_MODE_PIPELINE :
  485. SCALER_POS2_ALPHA_MODE_FIXED,
  486. SCALER_POS2_ALPHA_MODE) |
  487. VC4_SET_FIELD(vc4_state->src_w[0], SCALER_POS2_WIDTH) |
  488. VC4_SET_FIELD(vc4_state->src_h[0], SCALER_POS2_HEIGHT));
  489. /* Position Word 3: Context. Written by the HVS. */
  490. vc4_dlist_write(vc4_state, 0xc0c0c0c0);
  491. /* Pointer Word 0/1/2: RGB / Y / Cb / Cr Pointers
  492. *
  493. * The pointers may be any byte address.
  494. */
  495. vc4_state->ptr0_offset = vc4_state->dlist_count;
  496. if (!format->flip_cbcr) {
  497. for (i = 0; i < num_planes; i++)
  498. vc4_dlist_write(vc4_state, vc4_state->offsets[i]);
  499. } else {
  500. WARN_ON_ONCE(num_planes != 3);
  501. vc4_dlist_write(vc4_state, vc4_state->offsets[0]);
  502. vc4_dlist_write(vc4_state, vc4_state->offsets[2]);
  503. vc4_dlist_write(vc4_state, vc4_state->offsets[1]);
  504. }
  505. /* Pointer Context Word 0/1/2: Written by the HVS */
  506. for (i = 0; i < num_planes; i++)
  507. vc4_dlist_write(vc4_state, 0xc0c0c0c0);
  508. /* Pitch word 0/1/2 */
  509. for (i = 0; i < num_planes; i++) {
  510. vc4_dlist_write(vc4_state,
  511. VC4_SET_FIELD(fb->pitches[i], SCALER_SRC_PITCH));
  512. }
  513. /* Colorspace conversion words */
  514. if (vc4_state->is_yuv) {
  515. vc4_dlist_write(vc4_state, SCALER_CSC0_ITR_R_601_5);
  516. vc4_dlist_write(vc4_state, SCALER_CSC1_ITR_R_601_5);
  517. vc4_dlist_write(vc4_state, SCALER_CSC2_ITR_R_601_5);
  518. }
  519. if (!vc4_state->is_unity) {
  520. /* LBM Base Address. */
  521. if (vc4_state->y_scaling[0] != VC4_SCALING_NONE ||
  522. vc4_state->y_scaling[1] != VC4_SCALING_NONE) {
  523. vc4_dlist_write(vc4_state, vc4_state->lbm.start);
  524. }
  525. if (num_planes > 1) {
  526. /* Emit Cb/Cr as channel 0 and Y as channel
  527. * 1. This matches how we set up scl0/scl1
  528. * above.
  529. */
  530. vc4_write_scaling_parameters(state, 1);
  531. }
  532. vc4_write_scaling_parameters(state, 0);
  533. /* If any PPF setup was done, then all the kernel
  534. * pointers get uploaded.
  535. */
  536. if (vc4_state->x_scaling[0] == VC4_SCALING_PPF ||
  537. vc4_state->y_scaling[0] == VC4_SCALING_PPF ||
  538. vc4_state->x_scaling[1] == VC4_SCALING_PPF ||
  539. vc4_state->y_scaling[1] == VC4_SCALING_PPF) {
  540. u32 kernel = VC4_SET_FIELD(vc4->hvs->mitchell_netravali_filter.start,
  541. SCALER_PPF_KERNEL_OFFSET);
  542. /* HPPF plane 0 */
  543. vc4_dlist_write(vc4_state, kernel);
  544. /* VPPF plane 0 */
  545. vc4_dlist_write(vc4_state, kernel);
  546. /* HPPF plane 1 */
  547. vc4_dlist_write(vc4_state, kernel);
  548. /* VPPF plane 1 */
  549. vc4_dlist_write(vc4_state, kernel);
  550. }
  551. }
  552. vc4_state->dlist[ctl0_offset] |=
  553. VC4_SET_FIELD(vc4_state->dlist_count, SCALER_CTL0_SIZE);
  554. return 0;
  555. }
  556. /* If a modeset involves changing the setup of a plane, the atomic
  557. * infrastructure will call this to validate a proposed plane setup.
  558. * However, if a plane isn't getting updated, this (and the
  559. * corresponding vc4_plane_atomic_update) won't get called. Thus, we
  560. * compute the dlist here and have all active plane dlists get updated
  561. * in the CRTC's flush.
  562. */
  563. static int vc4_plane_atomic_check(struct drm_plane *plane,
  564. struct drm_plane_state *state)
  565. {
  566. struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
  567. vc4_state->dlist_count = 0;
  568. if (plane_enabled(state))
  569. return vc4_plane_mode_set(plane, state);
  570. else
  571. return 0;
  572. }
  573. static void vc4_plane_atomic_update(struct drm_plane *plane,
  574. struct drm_plane_state *old_state)
  575. {
  576. /* No contents here. Since we don't know where in the CRTC's
  577. * dlist we should be stored, our dlist is uploaded to the
  578. * hardware with vc4_plane_write_dlist() at CRTC atomic_flush
  579. * time.
  580. */
  581. }
  582. u32 vc4_plane_write_dlist(struct drm_plane *plane, u32 __iomem *dlist)
  583. {
  584. struct vc4_plane_state *vc4_state = to_vc4_plane_state(plane->state);
  585. int i;
  586. vc4_state->hw_dlist = dlist;
  587. /* Can't memcpy_toio() because it needs to be 32-bit writes. */
  588. for (i = 0; i < vc4_state->dlist_count; i++)
  589. writel(vc4_state->dlist[i], &dlist[i]);
  590. return vc4_state->dlist_count;
  591. }
  592. u32 vc4_plane_dlist_size(struct drm_plane_state *state)
  593. {
  594. struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
  595. return vc4_state->dlist_count;
  596. }
  597. /* Updates the plane to immediately (well, once the FIFO needs
  598. * refilling) scan out from at a new framebuffer.
  599. */
  600. void vc4_plane_async_set_fb(struct drm_plane *plane, struct drm_framebuffer *fb)
  601. {
  602. struct vc4_plane_state *vc4_state = to_vc4_plane_state(plane->state);
  603. struct drm_gem_cma_object *bo = drm_fb_cma_get_gem_obj(fb, 0);
  604. uint32_t addr;
  605. /* We're skipping the address adjustment for negative origin,
  606. * because this is only called on the primary plane.
  607. */
  608. WARN_ON_ONCE(plane->state->crtc_x < 0 || plane->state->crtc_y < 0);
  609. addr = bo->paddr + fb->offsets[0];
  610. /* Write the new address into the hardware immediately. The
  611. * scanout will start from this address as soon as the FIFO
  612. * needs to refill with pixels.
  613. */
  614. writel(addr, &vc4_state->hw_dlist[vc4_state->ptr0_offset]);
  615. /* Also update the CPU-side dlist copy, so that any later
  616. * atomic updates that don't do a new modeset on our plane
  617. * also use our updated address.
  618. */
  619. vc4_state->dlist[vc4_state->ptr0_offset] = addr;
  620. }
  621. static const struct drm_plane_helper_funcs vc4_plane_helper_funcs = {
  622. .prepare_fb = NULL,
  623. .cleanup_fb = NULL,
  624. .atomic_check = vc4_plane_atomic_check,
  625. .atomic_update = vc4_plane_atomic_update,
  626. };
  627. static void vc4_plane_destroy(struct drm_plane *plane)
  628. {
  629. drm_plane_helper_disable(plane);
  630. drm_plane_cleanup(plane);
  631. }
  632. /* Implements immediate (non-vblank-synced) updates of the cursor
  633. * position, or falls back to the atomic helper otherwise.
  634. */
  635. static int
  636. vc4_update_plane(struct drm_plane *plane,
  637. struct drm_crtc *crtc,
  638. struct drm_framebuffer *fb,
  639. int crtc_x, int crtc_y,
  640. unsigned int crtc_w, unsigned int crtc_h,
  641. uint32_t src_x, uint32_t src_y,
  642. uint32_t src_w, uint32_t src_h)
  643. {
  644. struct drm_plane_state *plane_state;
  645. struct vc4_plane_state *vc4_state;
  646. if (plane != crtc->cursor)
  647. goto out;
  648. plane_state = plane->state;
  649. vc4_state = to_vc4_plane_state(plane_state);
  650. if (!plane_state)
  651. goto out;
  652. /* If we're changing the cursor contents, do that in the
  653. * normal vblank-synced atomic path.
  654. */
  655. if (fb != plane_state->fb)
  656. goto out;
  657. /* No configuring new scaling in the fast path. */
  658. if (crtc_w != plane_state->crtc_w ||
  659. crtc_h != plane_state->crtc_h ||
  660. src_w != plane_state->src_w ||
  661. src_h != plane_state->src_h) {
  662. goto out;
  663. }
  664. /* Set the cursor's position on the screen. This is the
  665. * expected change from the drm_mode_cursor_universal()
  666. * helper.
  667. */
  668. plane_state->crtc_x = crtc_x;
  669. plane_state->crtc_y = crtc_y;
  670. /* Allow changing the start position within the cursor BO, if
  671. * that matters.
  672. */
  673. plane_state->src_x = src_x;
  674. plane_state->src_y = src_y;
  675. /* Update the display list based on the new crtc_x/y. */
  676. vc4_plane_atomic_check(plane, plane_state);
  677. /* Note that we can't just call vc4_plane_write_dlist()
  678. * because that would smash the context data that the HVS is
  679. * currently using.
  680. */
  681. writel(vc4_state->dlist[vc4_state->pos0_offset],
  682. &vc4_state->hw_dlist[vc4_state->pos0_offset]);
  683. writel(vc4_state->dlist[vc4_state->pos2_offset],
  684. &vc4_state->hw_dlist[vc4_state->pos2_offset]);
  685. writel(vc4_state->dlist[vc4_state->ptr0_offset],
  686. &vc4_state->hw_dlist[vc4_state->ptr0_offset]);
  687. return 0;
  688. out:
  689. return drm_atomic_helper_update_plane(plane, crtc, fb,
  690. crtc_x, crtc_y,
  691. crtc_w, crtc_h,
  692. src_x, src_y,
  693. src_w, src_h);
  694. }
  695. static const struct drm_plane_funcs vc4_plane_funcs = {
  696. .update_plane = vc4_update_plane,
  697. .disable_plane = drm_atomic_helper_disable_plane,
  698. .destroy = vc4_plane_destroy,
  699. .set_property = NULL,
  700. .reset = vc4_plane_reset,
  701. .atomic_duplicate_state = vc4_plane_duplicate_state,
  702. .atomic_destroy_state = vc4_plane_destroy_state,
  703. };
  704. struct drm_plane *vc4_plane_init(struct drm_device *dev,
  705. enum drm_plane_type type)
  706. {
  707. struct drm_plane *plane = NULL;
  708. struct vc4_plane *vc4_plane;
  709. u32 formats[ARRAY_SIZE(hvs_formats)];
  710. u32 num_formats = 0;
  711. int ret = 0;
  712. unsigned i;
  713. vc4_plane = devm_kzalloc(dev->dev, sizeof(*vc4_plane),
  714. GFP_KERNEL);
  715. if (!vc4_plane) {
  716. ret = -ENOMEM;
  717. goto fail;
  718. }
  719. for (i = 0; i < ARRAY_SIZE(hvs_formats); i++) {
  720. /* Don't allow YUV in cursor planes, since that means
  721. * tuning on the scaler, which we don't allow for the
  722. * cursor.
  723. */
  724. if (type != DRM_PLANE_TYPE_CURSOR ||
  725. hvs_formats[i].hvs < HVS_PIXEL_FORMAT_YCBCR_YUV420_3PLANE) {
  726. formats[num_formats++] = hvs_formats[i].drm;
  727. }
  728. }
  729. plane = &vc4_plane->base;
  730. ret = drm_universal_plane_init(dev, plane, 0xff,
  731. &vc4_plane_funcs,
  732. formats, num_formats,
  733. type, NULL);
  734. drm_plane_helper_add(plane, &vc4_plane_helper_funcs);
  735. return plane;
  736. fail:
  737. if (plane)
  738. vc4_plane_destroy(plane);
  739. return ERR_PTR(ret);
  740. }