vc4_plane.c 27 KB

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