vc4_plane.c 26 KB

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