vc4_plane.c 29 KB

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