vc4_plane.c 29 KB

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