vc4_plane.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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. u64 base_format_mod = fourcc_mod_broadcom_mod(fb->modifier);
  408. int num_planes = drm_format_num_planes(format->drm);
  409. bool mix_plane_alpha;
  410. bool covers_screen;
  411. u32 scl0, scl1, pitch0;
  412. u32 lbm_size, tiling;
  413. unsigned long irqflags;
  414. u32 hvs_format = format->hvs;
  415. int ret, i;
  416. ret = vc4_plane_setup_clipping_and_scaling(state);
  417. if (ret)
  418. return ret;
  419. /* Allocate the LBM memory that the HVS will use for temporary
  420. * storage due to our scaling/format conversion.
  421. */
  422. lbm_size = vc4_lbm_size(state);
  423. if (lbm_size) {
  424. if (!vc4_state->lbm.allocated) {
  425. spin_lock_irqsave(&vc4->hvs->mm_lock, irqflags);
  426. ret = drm_mm_insert_node_generic(&vc4->hvs->lbm_mm,
  427. &vc4_state->lbm,
  428. lbm_size, 32, 0, 0);
  429. spin_unlock_irqrestore(&vc4->hvs->mm_lock, irqflags);
  430. } else {
  431. WARN_ON_ONCE(lbm_size != vc4_state->lbm.size);
  432. }
  433. }
  434. if (ret)
  435. return ret;
  436. /* SCL1 is used for Cb/Cr scaling of planar formats. For RGB
  437. * and 4:4:4, scl1 should be set to scl0 so both channels of
  438. * the scaler do the same thing. For YUV, the Y plane needs
  439. * to be put in channel 1 and Cb/Cr in channel 0, so we swap
  440. * the scl fields here.
  441. */
  442. if (num_planes == 1) {
  443. scl0 = vc4_get_scl_field(state, 0);
  444. scl1 = scl0;
  445. } else {
  446. scl0 = vc4_get_scl_field(state, 1);
  447. scl1 = vc4_get_scl_field(state, 0);
  448. }
  449. switch (base_format_mod) {
  450. case DRM_FORMAT_MOD_LINEAR:
  451. tiling = SCALER_CTL0_TILING_LINEAR;
  452. pitch0 = VC4_SET_FIELD(fb->pitches[0], SCALER_SRC_PITCH);
  453. break;
  454. case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED: {
  455. /* For T-tiled, the FB pitch is "how many bytes from
  456. * one row to the next, such that pitch * tile_h ==
  457. * tile_size * tiles_per_row."
  458. */
  459. u32 tile_size_shift = 12; /* T tiles are 4kb */
  460. u32 tile_h_shift = 5; /* 16 and 32bpp are 32 pixels high */
  461. u32 tiles_w = fb->pitches[0] >> (tile_size_shift - tile_h_shift);
  462. tiling = SCALER_CTL0_TILING_256B_OR_T;
  463. pitch0 = (VC4_SET_FIELD(0, SCALER_PITCH0_TILE_Y_OFFSET) |
  464. VC4_SET_FIELD(0, SCALER_PITCH0_TILE_WIDTH_L) |
  465. VC4_SET_FIELD(tiles_w, SCALER_PITCH0_TILE_WIDTH_R));
  466. break;
  467. }
  468. case DRM_FORMAT_MOD_BROADCOM_SAND64:
  469. case DRM_FORMAT_MOD_BROADCOM_SAND128:
  470. case DRM_FORMAT_MOD_BROADCOM_SAND256: {
  471. uint32_t param = fourcc_mod_broadcom_param(fb->modifier);
  472. /* Column-based NV12 or RGBA.
  473. */
  474. if (fb->format->num_planes > 1) {
  475. if (hvs_format != HVS_PIXEL_FORMAT_YCBCR_YUV420_2PLANE) {
  476. DRM_DEBUG_KMS("SAND format only valid for NV12/21");
  477. return -EINVAL;
  478. }
  479. hvs_format = HVS_PIXEL_FORMAT_H264;
  480. } else {
  481. if (base_format_mod == DRM_FORMAT_MOD_BROADCOM_SAND256) {
  482. DRM_DEBUG_KMS("SAND256 format only valid for H.264");
  483. return -EINVAL;
  484. }
  485. }
  486. switch (base_format_mod) {
  487. case DRM_FORMAT_MOD_BROADCOM_SAND64:
  488. tiling = SCALER_CTL0_TILING_64B;
  489. break;
  490. case DRM_FORMAT_MOD_BROADCOM_SAND128:
  491. tiling = SCALER_CTL0_TILING_128B;
  492. break;
  493. case DRM_FORMAT_MOD_BROADCOM_SAND256:
  494. tiling = SCALER_CTL0_TILING_256B_OR_T;
  495. break;
  496. default:
  497. break;
  498. }
  499. if (param > SCALER_TILE_HEIGHT_MASK) {
  500. DRM_DEBUG_KMS("SAND height too large (%d)\n", param);
  501. return -EINVAL;
  502. }
  503. pitch0 = VC4_SET_FIELD(param, SCALER_TILE_HEIGHT);
  504. break;
  505. }
  506. default:
  507. DRM_DEBUG_KMS("Unsupported FB tiling flag 0x%16llx",
  508. (long long)fb->modifier);
  509. return -EINVAL;
  510. }
  511. /* Control word */
  512. vc4_dlist_write(vc4_state,
  513. SCALER_CTL0_VALID |
  514. VC4_SET_FIELD(SCALER_CTL0_RGBA_EXPAND_ROUND, SCALER_CTL0_RGBA_EXPAND) |
  515. (format->pixel_order << SCALER_CTL0_ORDER_SHIFT) |
  516. (hvs_format << SCALER_CTL0_PIXEL_FORMAT_SHIFT) |
  517. VC4_SET_FIELD(tiling, SCALER_CTL0_TILING) |
  518. (vc4_state->is_unity ? SCALER_CTL0_UNITY : 0) |
  519. VC4_SET_FIELD(scl0, SCALER_CTL0_SCL0) |
  520. VC4_SET_FIELD(scl1, SCALER_CTL0_SCL1));
  521. /* Position Word 0: Image Positions and Alpha Value */
  522. vc4_state->pos0_offset = vc4_state->dlist_count;
  523. vc4_dlist_write(vc4_state,
  524. VC4_SET_FIELD(state->alpha >> 8, SCALER_POS0_FIXED_ALPHA) |
  525. VC4_SET_FIELD(vc4_state->crtc_x, SCALER_POS0_START_X) |
  526. VC4_SET_FIELD(vc4_state->crtc_y, SCALER_POS0_START_Y));
  527. /* Position Word 1: Scaled Image Dimensions. */
  528. if (!vc4_state->is_unity) {
  529. vc4_dlist_write(vc4_state,
  530. VC4_SET_FIELD(vc4_state->crtc_w,
  531. SCALER_POS1_SCL_WIDTH) |
  532. VC4_SET_FIELD(vc4_state->crtc_h,
  533. SCALER_POS1_SCL_HEIGHT));
  534. }
  535. /* Don't waste cycles mixing with plane alpha if the set alpha
  536. * is opaque or there is no per-pixel alpha information.
  537. * In any case we use the alpha property value as the fixed alpha.
  538. */
  539. mix_plane_alpha = state->alpha != DRM_BLEND_ALPHA_OPAQUE &&
  540. fb->format->has_alpha;
  541. /* Position Word 2: Source Image Size, Alpha */
  542. vc4_state->pos2_offset = vc4_state->dlist_count;
  543. vc4_dlist_write(vc4_state,
  544. VC4_SET_FIELD(fb->format->has_alpha ?
  545. SCALER_POS2_ALPHA_MODE_PIPELINE :
  546. SCALER_POS2_ALPHA_MODE_FIXED,
  547. SCALER_POS2_ALPHA_MODE) |
  548. (mix_plane_alpha ? SCALER_POS2_ALPHA_MIX : 0) |
  549. (fb->format->has_alpha ? SCALER_POS2_ALPHA_PREMULT : 0) |
  550. VC4_SET_FIELD(vc4_state->src_w[0], SCALER_POS2_WIDTH) |
  551. VC4_SET_FIELD(vc4_state->src_h[0], SCALER_POS2_HEIGHT));
  552. /* Position Word 3: Context. Written by the HVS. */
  553. vc4_dlist_write(vc4_state, 0xc0c0c0c0);
  554. /* Pointer Word 0/1/2: RGB / Y / Cb / Cr Pointers
  555. *
  556. * The pointers may be any byte address.
  557. */
  558. vc4_state->ptr0_offset = vc4_state->dlist_count;
  559. for (i = 0; i < num_planes; i++)
  560. vc4_dlist_write(vc4_state, vc4_state->offsets[i]);
  561. /* Pointer Context Word 0/1/2: Written by the HVS */
  562. for (i = 0; i < num_planes; i++)
  563. vc4_dlist_write(vc4_state, 0xc0c0c0c0);
  564. /* Pitch word 0 */
  565. vc4_dlist_write(vc4_state, pitch0);
  566. /* Pitch word 1/2 */
  567. for (i = 1; i < num_planes; i++) {
  568. if (hvs_format != HVS_PIXEL_FORMAT_H264) {
  569. vc4_dlist_write(vc4_state,
  570. VC4_SET_FIELD(fb->pitches[i],
  571. SCALER_SRC_PITCH));
  572. } else {
  573. vc4_dlist_write(vc4_state, pitch0);
  574. }
  575. }
  576. /* Colorspace conversion words */
  577. if (vc4_state->is_yuv) {
  578. vc4_dlist_write(vc4_state, SCALER_CSC0_ITR_R_601_5);
  579. vc4_dlist_write(vc4_state, SCALER_CSC1_ITR_R_601_5);
  580. vc4_dlist_write(vc4_state, SCALER_CSC2_ITR_R_601_5);
  581. }
  582. if (!vc4_state->is_unity) {
  583. /* LBM Base Address. */
  584. if (vc4_state->y_scaling[0] != VC4_SCALING_NONE ||
  585. vc4_state->y_scaling[1] != VC4_SCALING_NONE) {
  586. vc4_dlist_write(vc4_state, vc4_state->lbm.start);
  587. }
  588. if (num_planes > 1) {
  589. /* Emit Cb/Cr as channel 0 and Y as channel
  590. * 1. This matches how we set up scl0/scl1
  591. * above.
  592. */
  593. vc4_write_scaling_parameters(state, 1);
  594. }
  595. vc4_write_scaling_parameters(state, 0);
  596. /* If any PPF setup was done, then all the kernel
  597. * pointers get uploaded.
  598. */
  599. if (vc4_state->x_scaling[0] == VC4_SCALING_PPF ||
  600. vc4_state->y_scaling[0] == VC4_SCALING_PPF ||
  601. vc4_state->x_scaling[1] == VC4_SCALING_PPF ||
  602. vc4_state->y_scaling[1] == VC4_SCALING_PPF) {
  603. u32 kernel = VC4_SET_FIELD(vc4->hvs->mitchell_netravali_filter.start,
  604. SCALER_PPF_KERNEL_OFFSET);
  605. /* HPPF plane 0 */
  606. vc4_dlist_write(vc4_state, kernel);
  607. /* VPPF plane 0 */
  608. vc4_dlist_write(vc4_state, kernel);
  609. /* HPPF plane 1 */
  610. vc4_dlist_write(vc4_state, kernel);
  611. /* VPPF plane 1 */
  612. vc4_dlist_write(vc4_state, kernel);
  613. }
  614. }
  615. vc4_state->dlist[ctl0_offset] |=
  616. VC4_SET_FIELD(vc4_state->dlist_count, SCALER_CTL0_SIZE);
  617. /* crtc_* are already clipped coordinates. */
  618. covers_screen = vc4_state->crtc_x == 0 && vc4_state->crtc_y == 0 &&
  619. vc4_state->crtc_w == state->crtc->mode.hdisplay &&
  620. vc4_state->crtc_h == state->crtc->mode.vdisplay;
  621. /* Background fill might be necessary when the plane has per-pixel
  622. * alpha content or a non-opaque plane alpha and could blend from the
  623. * background or does not cover the entire screen.
  624. */
  625. vc4_state->needs_bg_fill = fb->format->has_alpha || !covers_screen ||
  626. state->alpha != DRM_BLEND_ALPHA_OPAQUE;
  627. return 0;
  628. }
  629. /* If a modeset involves changing the setup of a plane, the atomic
  630. * infrastructure will call this to validate a proposed plane setup.
  631. * However, if a plane isn't getting updated, this (and the
  632. * corresponding vc4_plane_atomic_update) won't get called. Thus, we
  633. * compute the dlist here and have all active plane dlists get updated
  634. * in the CRTC's flush.
  635. */
  636. static int vc4_plane_atomic_check(struct drm_plane *plane,
  637. struct drm_plane_state *state)
  638. {
  639. struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
  640. vc4_state->dlist_count = 0;
  641. if (plane_enabled(state))
  642. return vc4_plane_mode_set(plane, state);
  643. else
  644. return 0;
  645. }
  646. static void vc4_plane_atomic_update(struct drm_plane *plane,
  647. struct drm_plane_state *old_state)
  648. {
  649. /* No contents here. Since we don't know where in the CRTC's
  650. * dlist we should be stored, our dlist is uploaded to the
  651. * hardware with vc4_plane_write_dlist() at CRTC atomic_flush
  652. * time.
  653. */
  654. }
  655. u32 vc4_plane_write_dlist(struct drm_plane *plane, u32 __iomem *dlist)
  656. {
  657. struct vc4_plane_state *vc4_state = to_vc4_plane_state(plane->state);
  658. int i;
  659. vc4_state->hw_dlist = dlist;
  660. /* Can't memcpy_toio() because it needs to be 32-bit writes. */
  661. for (i = 0; i < vc4_state->dlist_count; i++)
  662. writel(vc4_state->dlist[i], &dlist[i]);
  663. return vc4_state->dlist_count;
  664. }
  665. u32 vc4_plane_dlist_size(const struct drm_plane_state *state)
  666. {
  667. const struct vc4_plane_state *vc4_state =
  668. container_of(state, typeof(*vc4_state), base);
  669. return vc4_state->dlist_count;
  670. }
  671. /* Updates the plane to immediately (well, once the FIFO needs
  672. * refilling) scan out from at a new framebuffer.
  673. */
  674. void vc4_plane_async_set_fb(struct drm_plane *plane, struct drm_framebuffer *fb)
  675. {
  676. struct vc4_plane_state *vc4_state = to_vc4_plane_state(plane->state);
  677. struct drm_gem_cma_object *bo = drm_fb_cma_get_gem_obj(fb, 0);
  678. uint32_t addr;
  679. /* We're skipping the address adjustment for negative origin,
  680. * because this is only called on the primary plane.
  681. */
  682. WARN_ON_ONCE(plane->state->crtc_x < 0 || plane->state->crtc_y < 0);
  683. addr = bo->paddr + fb->offsets[0];
  684. /* Write the new address into the hardware immediately. The
  685. * scanout will start from this address as soon as the FIFO
  686. * needs to refill with pixels.
  687. */
  688. writel(addr, &vc4_state->hw_dlist[vc4_state->ptr0_offset]);
  689. /* Also update the CPU-side dlist copy, so that any later
  690. * atomic updates that don't do a new modeset on our plane
  691. * also use our updated address.
  692. */
  693. vc4_state->dlist[vc4_state->ptr0_offset] = addr;
  694. }
  695. static void vc4_plane_atomic_async_update(struct drm_plane *plane,
  696. struct drm_plane_state *state)
  697. {
  698. struct vc4_plane_state *vc4_state = to_vc4_plane_state(plane->state);
  699. if (plane->state->fb != state->fb) {
  700. vc4_plane_async_set_fb(plane, state->fb);
  701. drm_atomic_set_fb_for_plane(plane->state, state->fb);
  702. }
  703. /* Set the cursor's position on the screen. This is the
  704. * expected change from the drm_mode_cursor_universal()
  705. * helper.
  706. */
  707. plane->state->crtc_x = state->crtc_x;
  708. plane->state->crtc_y = state->crtc_y;
  709. /* Allow changing the start position within the cursor BO, if
  710. * that matters.
  711. */
  712. plane->state->src_x = state->src_x;
  713. plane->state->src_y = state->src_y;
  714. /* Update the display list based on the new crtc_x/y. */
  715. vc4_plane_atomic_check(plane, plane->state);
  716. /* Note that we can't just call vc4_plane_write_dlist()
  717. * because that would smash the context data that the HVS is
  718. * currently using.
  719. */
  720. writel(vc4_state->dlist[vc4_state->pos0_offset],
  721. &vc4_state->hw_dlist[vc4_state->pos0_offset]);
  722. writel(vc4_state->dlist[vc4_state->pos2_offset],
  723. &vc4_state->hw_dlist[vc4_state->pos2_offset]);
  724. writel(vc4_state->dlist[vc4_state->ptr0_offset],
  725. &vc4_state->hw_dlist[vc4_state->ptr0_offset]);
  726. }
  727. static int vc4_plane_atomic_async_check(struct drm_plane *plane,
  728. struct drm_plane_state *state)
  729. {
  730. /* No configuring new scaling in the fast path. */
  731. if (plane->state->crtc_w != state->crtc_w ||
  732. plane->state->crtc_h != state->crtc_h ||
  733. plane->state->src_w != state->src_w ||
  734. plane->state->src_h != state->src_h)
  735. return -EINVAL;
  736. return 0;
  737. }
  738. static int vc4_prepare_fb(struct drm_plane *plane,
  739. struct drm_plane_state *state)
  740. {
  741. struct vc4_bo *bo;
  742. struct dma_fence *fence;
  743. int ret;
  744. if (!state->fb)
  745. return 0;
  746. bo = to_vc4_bo(&drm_fb_cma_get_gem_obj(state->fb, 0)->base);
  747. fence = reservation_object_get_excl_rcu(bo->resv);
  748. drm_atomic_set_fence_for_plane(state, fence);
  749. if (plane->state->fb == state->fb)
  750. return 0;
  751. ret = vc4_bo_inc_usecnt(bo);
  752. if (ret)
  753. return ret;
  754. return 0;
  755. }
  756. static void vc4_cleanup_fb(struct drm_plane *plane,
  757. struct drm_plane_state *state)
  758. {
  759. struct vc4_bo *bo;
  760. if (plane->state->fb == state->fb || !state->fb)
  761. return;
  762. bo = to_vc4_bo(&drm_fb_cma_get_gem_obj(state->fb, 0)->base);
  763. vc4_bo_dec_usecnt(bo);
  764. }
  765. static const struct drm_plane_helper_funcs vc4_plane_helper_funcs = {
  766. .atomic_check = vc4_plane_atomic_check,
  767. .atomic_update = vc4_plane_atomic_update,
  768. .prepare_fb = vc4_prepare_fb,
  769. .cleanup_fb = vc4_cleanup_fb,
  770. .atomic_async_check = vc4_plane_atomic_async_check,
  771. .atomic_async_update = vc4_plane_atomic_async_update,
  772. };
  773. static void vc4_plane_destroy(struct drm_plane *plane)
  774. {
  775. drm_plane_helper_disable(plane);
  776. drm_plane_cleanup(plane);
  777. }
  778. static bool vc4_format_mod_supported(struct drm_plane *plane,
  779. uint32_t format,
  780. uint64_t modifier)
  781. {
  782. /* Support T_TILING for RGB formats only. */
  783. switch (format) {
  784. case DRM_FORMAT_XRGB8888:
  785. case DRM_FORMAT_ARGB8888:
  786. case DRM_FORMAT_ABGR8888:
  787. case DRM_FORMAT_XBGR8888:
  788. case DRM_FORMAT_RGB565:
  789. case DRM_FORMAT_BGR565:
  790. case DRM_FORMAT_ARGB1555:
  791. case DRM_FORMAT_XRGB1555:
  792. switch (fourcc_mod_broadcom_mod(modifier)) {
  793. case DRM_FORMAT_MOD_LINEAR:
  794. case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED:
  795. case DRM_FORMAT_MOD_BROADCOM_SAND64:
  796. case DRM_FORMAT_MOD_BROADCOM_SAND128:
  797. return true;
  798. default:
  799. return false;
  800. }
  801. case DRM_FORMAT_NV12:
  802. case DRM_FORMAT_NV21:
  803. switch (fourcc_mod_broadcom_mod(modifier)) {
  804. case DRM_FORMAT_MOD_LINEAR:
  805. case DRM_FORMAT_MOD_BROADCOM_SAND64:
  806. case DRM_FORMAT_MOD_BROADCOM_SAND128:
  807. case DRM_FORMAT_MOD_BROADCOM_SAND256:
  808. return true;
  809. default:
  810. return false;
  811. }
  812. case DRM_FORMAT_YUV422:
  813. case DRM_FORMAT_YVU422:
  814. case DRM_FORMAT_YUV420:
  815. case DRM_FORMAT_YVU420:
  816. case DRM_FORMAT_NV16:
  817. case DRM_FORMAT_NV61:
  818. default:
  819. return (modifier == DRM_FORMAT_MOD_LINEAR);
  820. }
  821. }
  822. static const struct drm_plane_funcs vc4_plane_funcs = {
  823. .update_plane = drm_atomic_helper_update_plane,
  824. .disable_plane = drm_atomic_helper_disable_plane,
  825. .destroy = vc4_plane_destroy,
  826. .set_property = NULL,
  827. .reset = vc4_plane_reset,
  828. .atomic_duplicate_state = vc4_plane_duplicate_state,
  829. .atomic_destroy_state = vc4_plane_destroy_state,
  830. .format_mod_supported = vc4_format_mod_supported,
  831. };
  832. struct drm_plane *vc4_plane_init(struct drm_device *dev,
  833. enum drm_plane_type type)
  834. {
  835. struct drm_plane *plane = NULL;
  836. struct vc4_plane *vc4_plane;
  837. u32 formats[ARRAY_SIZE(hvs_formats)];
  838. u32 num_formats = 0;
  839. int ret = 0;
  840. unsigned i;
  841. static const uint64_t modifiers[] = {
  842. DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED,
  843. DRM_FORMAT_MOD_BROADCOM_SAND128,
  844. DRM_FORMAT_MOD_BROADCOM_SAND64,
  845. DRM_FORMAT_MOD_BROADCOM_SAND256,
  846. DRM_FORMAT_MOD_LINEAR,
  847. DRM_FORMAT_MOD_INVALID
  848. };
  849. vc4_plane = devm_kzalloc(dev->dev, sizeof(*vc4_plane),
  850. GFP_KERNEL);
  851. if (!vc4_plane)
  852. return ERR_PTR(-ENOMEM);
  853. for (i = 0; i < ARRAY_SIZE(hvs_formats); i++) {
  854. /* Don't allow YUV in cursor planes, since that means
  855. * tuning on the scaler, which we don't allow for the
  856. * cursor.
  857. */
  858. if (type != DRM_PLANE_TYPE_CURSOR ||
  859. hvs_formats[i].hvs < HVS_PIXEL_FORMAT_YCBCR_YUV420_3PLANE) {
  860. formats[num_formats++] = hvs_formats[i].drm;
  861. }
  862. }
  863. plane = &vc4_plane->base;
  864. ret = drm_universal_plane_init(dev, plane, 0,
  865. &vc4_plane_funcs,
  866. formats, num_formats,
  867. modifiers, type, NULL);
  868. drm_plane_helper_add(plane, &vc4_plane_helper_funcs);
  869. drm_plane_create_alpha_property(plane);
  870. return plane;
  871. }