vc4_plane.c 24 KB

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