vc4_plane.c 27 KB

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