vc4_plane.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2015 Broadcom
  4. */
  5. /**
  6. * DOC: VC4 plane module
  7. *
  8. * Each DRM plane is a layer of pixels being scanned out by the HVS.
  9. *
  10. * At atomic modeset check time, we compute the HVS display element
  11. * state that would be necessary for displaying the plane (giving us a
  12. * chance to figure out if a plane configuration is invalid), then at
  13. * atomic flush time the CRTC will ask us to write our element state
  14. * into the region of the HVS that it has allocated for us.
  15. */
  16. #include <drm/drm_atomic.h>
  17. #include <drm/drm_atomic_helper.h>
  18. #include <drm/drm_atomic_uapi.h>
  19. #include <drm/drm_blend.h>
  20. #include <drm/drm_drv.h>
  21. #include <drm/drm_fb_dma_helper.h>
  22. #include <drm/drm_fourcc.h>
  23. #include <drm/drm_framebuffer.h>
  24. #include <drm/drm_gem_atomic_helper.h>
  25. #include "uapi/drm/vc4_drm.h"
  26. #include "vc4_drv.h"
  27. #include "vc4_regs.h"
  28. static const struct hvs_format {
  29. u32 drm; /* DRM_FORMAT_* */
  30. u32 hvs; /* HVS_FORMAT_* */
  31. u32 pixel_order;
  32. u32 pixel_order_hvs5;
  33. bool hvs5_only;
  34. } hvs_formats[] = {
  35. {
  36. .drm = DRM_FORMAT_XRGB8888,
  37. .hvs = HVS_PIXEL_FORMAT_RGBA8888,
  38. .pixel_order = HVS_PIXEL_ORDER_ABGR,
  39. .pixel_order_hvs5 = HVS_PIXEL_ORDER_ARGB,
  40. },
  41. {
  42. .drm = DRM_FORMAT_ARGB8888,
  43. .hvs = HVS_PIXEL_FORMAT_RGBA8888,
  44. .pixel_order = HVS_PIXEL_ORDER_ABGR,
  45. .pixel_order_hvs5 = HVS_PIXEL_ORDER_ARGB,
  46. },
  47. {
  48. .drm = DRM_FORMAT_ABGR8888,
  49. .hvs = HVS_PIXEL_FORMAT_RGBA8888,
  50. .pixel_order = HVS_PIXEL_ORDER_ARGB,
  51. .pixel_order_hvs5 = HVS_PIXEL_ORDER_ABGR,
  52. },
  53. {
  54. .drm = DRM_FORMAT_XBGR8888,
  55. .hvs = HVS_PIXEL_FORMAT_RGBA8888,
  56. .pixel_order = HVS_PIXEL_ORDER_ARGB,
  57. .pixel_order_hvs5 = HVS_PIXEL_ORDER_ABGR,
  58. },
  59. {
  60. .drm = DRM_FORMAT_RGB565,
  61. .hvs = HVS_PIXEL_FORMAT_RGB565,
  62. .pixel_order = HVS_PIXEL_ORDER_XRGB,
  63. .pixel_order_hvs5 = HVS_PIXEL_ORDER_XRGB,
  64. },
  65. {
  66. .drm = DRM_FORMAT_BGR565,
  67. .hvs = HVS_PIXEL_FORMAT_RGB565,
  68. .pixel_order = HVS_PIXEL_ORDER_XBGR,
  69. .pixel_order_hvs5 = HVS_PIXEL_ORDER_XBGR,
  70. },
  71. {
  72. .drm = DRM_FORMAT_ARGB1555,
  73. .hvs = HVS_PIXEL_FORMAT_RGBA5551,
  74. .pixel_order = HVS_PIXEL_ORDER_ABGR,
  75. .pixel_order_hvs5 = HVS_PIXEL_ORDER_ARGB,
  76. },
  77. {
  78. .drm = DRM_FORMAT_XRGB1555,
  79. .hvs = HVS_PIXEL_FORMAT_RGBA5551,
  80. .pixel_order = HVS_PIXEL_ORDER_ABGR,
  81. .pixel_order_hvs5 = HVS_PIXEL_ORDER_ARGB,
  82. },
  83. {
  84. .drm = DRM_FORMAT_RGB888,
  85. .hvs = HVS_PIXEL_FORMAT_RGB888,
  86. .pixel_order = HVS_PIXEL_ORDER_XRGB,
  87. .pixel_order_hvs5 = HVS_PIXEL_ORDER_XRGB,
  88. },
  89. {
  90. .drm = DRM_FORMAT_BGR888,
  91. .hvs = HVS_PIXEL_FORMAT_RGB888,
  92. .pixel_order = HVS_PIXEL_ORDER_XBGR,
  93. .pixel_order_hvs5 = HVS_PIXEL_ORDER_XBGR,
  94. },
  95. {
  96. .drm = DRM_FORMAT_YUV422,
  97. .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_3PLANE,
  98. .pixel_order = HVS_PIXEL_ORDER_XYCBCR,
  99. .pixel_order_hvs5 = HVS_PIXEL_ORDER_XYCBCR,
  100. },
  101. {
  102. .drm = DRM_FORMAT_YVU422,
  103. .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_3PLANE,
  104. .pixel_order = HVS_PIXEL_ORDER_XYCRCB,
  105. .pixel_order_hvs5 = HVS_PIXEL_ORDER_XYCRCB,
  106. },
  107. {
  108. .drm = DRM_FORMAT_YUV420,
  109. .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV420_3PLANE,
  110. .pixel_order = HVS_PIXEL_ORDER_XYCBCR,
  111. .pixel_order_hvs5 = HVS_PIXEL_ORDER_XYCBCR,
  112. },
  113. {
  114. .drm = DRM_FORMAT_YVU420,
  115. .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV420_3PLANE,
  116. .pixel_order = HVS_PIXEL_ORDER_XYCRCB,
  117. .pixel_order_hvs5 = HVS_PIXEL_ORDER_XYCRCB,
  118. },
  119. {
  120. .drm = DRM_FORMAT_NV12,
  121. .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV420_2PLANE,
  122. .pixel_order = HVS_PIXEL_ORDER_XYCBCR,
  123. .pixel_order_hvs5 = HVS_PIXEL_ORDER_XYCBCR,
  124. },
  125. {
  126. .drm = DRM_FORMAT_NV21,
  127. .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV420_2PLANE,
  128. .pixel_order = HVS_PIXEL_ORDER_XYCRCB,
  129. .pixel_order_hvs5 = HVS_PIXEL_ORDER_XYCRCB,
  130. },
  131. {
  132. .drm = DRM_FORMAT_NV16,
  133. .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_2PLANE,
  134. .pixel_order = HVS_PIXEL_ORDER_XYCBCR,
  135. .pixel_order_hvs5 = HVS_PIXEL_ORDER_XYCBCR,
  136. },
  137. {
  138. .drm = DRM_FORMAT_NV61,
  139. .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_2PLANE,
  140. .pixel_order = HVS_PIXEL_ORDER_XYCRCB,
  141. .pixel_order_hvs5 = HVS_PIXEL_ORDER_XYCRCB,
  142. },
  143. {
  144. .drm = DRM_FORMAT_P030,
  145. .hvs = HVS_PIXEL_FORMAT_YCBCR_10BIT,
  146. .pixel_order_hvs5 = HVS_PIXEL_ORDER_XYCBCR,
  147. .hvs5_only = true,
  148. },
  149. {
  150. .drm = DRM_FORMAT_XRGB2101010,
  151. .hvs = HVS_PIXEL_FORMAT_RGBA1010102,
  152. .pixel_order_hvs5 = HVS_PIXEL_ORDER_ARGB,
  153. .hvs5_only = true,
  154. },
  155. {
  156. .drm = DRM_FORMAT_ARGB2101010,
  157. .hvs = HVS_PIXEL_FORMAT_RGBA1010102,
  158. .pixel_order_hvs5 = HVS_PIXEL_ORDER_ARGB,
  159. .hvs5_only = true,
  160. },
  161. {
  162. .drm = DRM_FORMAT_ABGR2101010,
  163. .hvs = HVS_PIXEL_FORMAT_RGBA1010102,
  164. .pixel_order_hvs5 = HVS_PIXEL_ORDER_ABGR,
  165. .hvs5_only = true,
  166. },
  167. {
  168. .drm = DRM_FORMAT_XBGR2101010,
  169. .hvs = HVS_PIXEL_FORMAT_RGBA1010102,
  170. .pixel_order_hvs5 = HVS_PIXEL_ORDER_ABGR,
  171. .hvs5_only = true,
  172. },
  173. {
  174. .drm = DRM_FORMAT_RGB332,
  175. .hvs = HVS_PIXEL_FORMAT_RGB332,
  176. .pixel_order = HVS_PIXEL_ORDER_ARGB,
  177. .pixel_order_hvs5 = HVS_PIXEL_ORDER_ARGB,
  178. },
  179. {
  180. .drm = DRM_FORMAT_BGR233,
  181. .hvs = HVS_PIXEL_FORMAT_RGB332,
  182. .pixel_order = HVS_PIXEL_ORDER_ABGR,
  183. .pixel_order_hvs5 = HVS_PIXEL_ORDER_ABGR,
  184. },
  185. {
  186. .drm = DRM_FORMAT_XRGB4444,
  187. .hvs = HVS_PIXEL_FORMAT_RGBA4444,
  188. .pixel_order = HVS_PIXEL_ORDER_ABGR,
  189. .pixel_order_hvs5 = HVS_PIXEL_ORDER_ARGB,
  190. },
  191. {
  192. .drm = DRM_FORMAT_ARGB4444,
  193. .hvs = HVS_PIXEL_FORMAT_RGBA4444,
  194. .pixel_order = HVS_PIXEL_ORDER_ABGR,
  195. .pixel_order_hvs5 = HVS_PIXEL_ORDER_ARGB,
  196. },
  197. {
  198. .drm = DRM_FORMAT_XBGR4444,
  199. .hvs = HVS_PIXEL_FORMAT_RGBA4444,
  200. .pixel_order = HVS_PIXEL_ORDER_ARGB,
  201. .pixel_order_hvs5 = HVS_PIXEL_ORDER_ABGR,
  202. },
  203. {
  204. .drm = DRM_FORMAT_ABGR4444,
  205. .hvs = HVS_PIXEL_FORMAT_RGBA4444,
  206. .pixel_order = HVS_PIXEL_ORDER_ARGB,
  207. .pixel_order_hvs5 = HVS_PIXEL_ORDER_ABGR,
  208. },
  209. {
  210. .drm = DRM_FORMAT_BGRX4444,
  211. .hvs = HVS_PIXEL_FORMAT_RGBA4444,
  212. .pixel_order = HVS_PIXEL_ORDER_RGBA,
  213. .pixel_order_hvs5 = HVS_PIXEL_ORDER_BGRA,
  214. },
  215. {
  216. .drm = DRM_FORMAT_BGRA4444,
  217. .hvs = HVS_PIXEL_FORMAT_RGBA4444,
  218. .pixel_order = HVS_PIXEL_ORDER_RGBA,
  219. .pixel_order_hvs5 = HVS_PIXEL_ORDER_BGRA,
  220. },
  221. {
  222. .drm = DRM_FORMAT_RGBX4444,
  223. .hvs = HVS_PIXEL_FORMAT_RGBA4444,
  224. .pixel_order = HVS_PIXEL_ORDER_BGRA,
  225. .pixel_order_hvs5 = HVS_PIXEL_ORDER_RGBA,
  226. },
  227. {
  228. .drm = DRM_FORMAT_RGBA4444,
  229. .hvs = HVS_PIXEL_FORMAT_RGBA4444,
  230. .pixel_order = HVS_PIXEL_ORDER_BGRA,
  231. .pixel_order_hvs5 = HVS_PIXEL_ORDER_RGBA,
  232. },
  233. };
  234. static const struct hvs_format *vc4_get_hvs_format(u32 drm_format)
  235. {
  236. unsigned i;
  237. for (i = 0; i < ARRAY_SIZE(hvs_formats); i++) {
  238. if (hvs_formats[i].drm == drm_format)
  239. return &hvs_formats[i];
  240. }
  241. return NULL;
  242. }
  243. static enum vc4_scaling_mode vc4_get_scaling_mode(u32 src, u32 dst)
  244. {
  245. if (dst == src)
  246. return VC4_SCALING_NONE;
  247. if (3 * dst >= 2 * src)
  248. return VC4_SCALING_PPF;
  249. else
  250. return VC4_SCALING_TPZ;
  251. }
  252. static bool plane_enabled(struct drm_plane_state *state)
  253. {
  254. return state->fb && !WARN_ON(!state->crtc);
  255. }
  256. static struct drm_plane_state *vc4_plane_duplicate_state(struct drm_plane *plane)
  257. {
  258. struct vc4_plane_state *vc4_state;
  259. if (WARN_ON(!plane->state))
  260. return NULL;
  261. vc4_state = kmemdup(plane->state, sizeof(*vc4_state), GFP_KERNEL);
  262. if (!vc4_state)
  263. return NULL;
  264. memset(&vc4_state->lbm, 0, sizeof(vc4_state->lbm));
  265. vc4_state->dlist_initialized = 0;
  266. __drm_atomic_helper_plane_duplicate_state(plane, &vc4_state->base);
  267. if (vc4_state->dlist) {
  268. vc4_state->dlist = kmemdup(vc4_state->dlist,
  269. vc4_state->dlist_count * 4,
  270. GFP_KERNEL);
  271. if (!vc4_state->dlist) {
  272. kfree(vc4_state);
  273. return NULL;
  274. }
  275. vc4_state->dlist_size = vc4_state->dlist_count;
  276. }
  277. return &vc4_state->base;
  278. }
  279. static void vc4_plane_destroy_state(struct drm_plane *plane,
  280. struct drm_plane_state *state)
  281. {
  282. struct vc4_dev *vc4 = to_vc4_dev(plane->dev);
  283. struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
  284. if (drm_mm_node_allocated(&vc4_state->lbm)) {
  285. unsigned long irqflags;
  286. spin_lock_irqsave(&vc4->hvs->mm_lock, irqflags);
  287. drm_mm_remove_node(&vc4_state->lbm);
  288. spin_unlock_irqrestore(&vc4->hvs->mm_lock, irqflags);
  289. }
  290. kfree(vc4_state->dlist);
  291. __drm_atomic_helper_plane_destroy_state(&vc4_state->base);
  292. kfree(state);
  293. }
  294. /* Called during init to allocate the plane's atomic state. */
  295. static void vc4_plane_reset(struct drm_plane *plane)
  296. {
  297. struct vc4_plane_state *vc4_state;
  298. WARN_ON(plane->state);
  299. vc4_state = kzalloc(sizeof(*vc4_state), GFP_KERNEL);
  300. if (!vc4_state)
  301. return;
  302. __drm_atomic_helper_plane_reset(plane, &vc4_state->base);
  303. }
  304. static void vc4_dlist_counter_increment(struct vc4_plane_state *vc4_state)
  305. {
  306. if (vc4_state->dlist_count == vc4_state->dlist_size) {
  307. u32 new_size = max(4u, vc4_state->dlist_count * 2);
  308. u32 *new_dlist = kmalloc_array(new_size, 4, GFP_KERNEL);
  309. if (!new_dlist)
  310. return;
  311. memcpy(new_dlist, vc4_state->dlist, vc4_state->dlist_count * 4);
  312. kfree(vc4_state->dlist);
  313. vc4_state->dlist = new_dlist;
  314. vc4_state->dlist_size = new_size;
  315. }
  316. vc4_state->dlist_count++;
  317. }
  318. static void vc4_dlist_write(struct vc4_plane_state *vc4_state, u32 val)
  319. {
  320. unsigned int idx = vc4_state->dlist_count;
  321. vc4_dlist_counter_increment(vc4_state);
  322. vc4_state->dlist[idx] = val;
  323. }
  324. /* Returns the scl0/scl1 field based on whether the dimensions need to
  325. * be up/down/non-scaled.
  326. *
  327. * This is a replication of a table from the spec.
  328. */
  329. static u32 vc4_get_scl_field(struct drm_plane_state *state, int plane)
  330. {
  331. struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
  332. switch (vc4_state->x_scaling[plane] << 2 | vc4_state->y_scaling[plane]) {
  333. case VC4_SCALING_PPF << 2 | VC4_SCALING_PPF:
  334. return SCALER_CTL0_SCL_H_PPF_V_PPF;
  335. case VC4_SCALING_TPZ << 2 | VC4_SCALING_PPF:
  336. return SCALER_CTL0_SCL_H_TPZ_V_PPF;
  337. case VC4_SCALING_PPF << 2 | VC4_SCALING_TPZ:
  338. return SCALER_CTL0_SCL_H_PPF_V_TPZ;
  339. case VC4_SCALING_TPZ << 2 | VC4_SCALING_TPZ:
  340. return SCALER_CTL0_SCL_H_TPZ_V_TPZ;
  341. case VC4_SCALING_PPF << 2 | VC4_SCALING_NONE:
  342. return SCALER_CTL0_SCL_H_PPF_V_NONE;
  343. case VC4_SCALING_NONE << 2 | VC4_SCALING_PPF:
  344. return SCALER_CTL0_SCL_H_NONE_V_PPF;
  345. case VC4_SCALING_NONE << 2 | VC4_SCALING_TPZ:
  346. return SCALER_CTL0_SCL_H_NONE_V_TPZ;
  347. case VC4_SCALING_TPZ << 2 | VC4_SCALING_NONE:
  348. return SCALER_CTL0_SCL_H_TPZ_V_NONE;
  349. default:
  350. case VC4_SCALING_NONE << 2 | VC4_SCALING_NONE:
  351. /* The unity case is independently handled by
  352. * SCALER_CTL0_UNITY.
  353. */
  354. return 0;
  355. }
  356. }
  357. static int vc4_plane_margins_adj(struct drm_plane_state *pstate)
  358. {
  359. struct vc4_plane_state *vc4_pstate = to_vc4_plane_state(pstate);
  360. unsigned int left, right, top, bottom, adjhdisplay, adjvdisplay;
  361. struct drm_crtc_state *crtc_state;
  362. crtc_state = drm_atomic_get_new_crtc_state(pstate->state,
  363. pstate->crtc);
  364. vc4_crtc_get_margins(crtc_state, &left, &right, &top, &bottom);
  365. if (!left && !right && !top && !bottom)
  366. return 0;
  367. if (left + right >= crtc_state->mode.hdisplay ||
  368. top + bottom >= crtc_state->mode.vdisplay)
  369. return -EINVAL;
  370. adjhdisplay = crtc_state->mode.hdisplay - (left + right);
  371. vc4_pstate->crtc_x = DIV_ROUND_CLOSEST(vc4_pstate->crtc_x *
  372. adjhdisplay,
  373. crtc_state->mode.hdisplay);
  374. vc4_pstate->crtc_x += left;
  375. if (vc4_pstate->crtc_x > crtc_state->mode.hdisplay - right)
  376. vc4_pstate->crtc_x = crtc_state->mode.hdisplay - right;
  377. adjvdisplay = crtc_state->mode.vdisplay - (top + bottom);
  378. vc4_pstate->crtc_y = DIV_ROUND_CLOSEST(vc4_pstate->crtc_y *
  379. adjvdisplay,
  380. crtc_state->mode.vdisplay);
  381. vc4_pstate->crtc_y += top;
  382. if (vc4_pstate->crtc_y > crtc_state->mode.vdisplay - bottom)
  383. vc4_pstate->crtc_y = crtc_state->mode.vdisplay - bottom;
  384. vc4_pstate->crtc_w = DIV_ROUND_CLOSEST(vc4_pstate->crtc_w *
  385. adjhdisplay,
  386. crtc_state->mode.hdisplay);
  387. vc4_pstate->crtc_h = DIV_ROUND_CLOSEST(vc4_pstate->crtc_h *
  388. adjvdisplay,
  389. crtc_state->mode.vdisplay);
  390. if (!vc4_pstate->crtc_w || !vc4_pstate->crtc_h)
  391. return -EINVAL;
  392. return 0;
  393. }
  394. static int vc4_plane_setup_clipping_and_scaling(struct drm_plane_state *state)
  395. {
  396. struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
  397. struct drm_framebuffer *fb = state->fb;
  398. struct drm_gem_dma_object *bo;
  399. int num_planes = fb->format->num_planes;
  400. struct drm_crtc_state *crtc_state;
  401. u32 h_subsample = fb->format->hsub;
  402. u32 v_subsample = fb->format->vsub;
  403. int i, ret;
  404. crtc_state = drm_atomic_get_existing_crtc_state(state->state,
  405. state->crtc);
  406. if (!crtc_state) {
  407. DRM_DEBUG_KMS("Invalid crtc state\n");
  408. return -EINVAL;
  409. }
  410. ret = drm_atomic_helper_check_plane_state(state, crtc_state, 1,
  411. INT_MAX, true, true);
  412. if (ret)
  413. return ret;
  414. for (i = 0; i < num_planes; i++) {
  415. bo = drm_fb_dma_get_gem_obj(fb, i);
  416. vc4_state->offsets[i] = bo->dma_addr + fb->offsets[i];
  417. }
  418. /*
  419. * We don't support subpixel source positioning for scaling,
  420. * but fractional coordinates can be generated by clipping
  421. * so just round for now
  422. */
  423. vc4_state->src_x = DIV_ROUND_CLOSEST(state->src.x1, 1 << 16);
  424. vc4_state->src_y = DIV_ROUND_CLOSEST(state->src.y1, 1 << 16);
  425. vc4_state->src_w[0] = DIV_ROUND_CLOSEST(state->src.x2, 1 << 16) - vc4_state->src_x;
  426. vc4_state->src_h[0] = DIV_ROUND_CLOSEST(state->src.y2, 1 << 16) - vc4_state->src_y;
  427. vc4_state->crtc_x = state->dst.x1;
  428. vc4_state->crtc_y = state->dst.y1;
  429. vc4_state->crtc_w = state->dst.x2 - state->dst.x1;
  430. vc4_state->crtc_h = state->dst.y2 - state->dst.y1;
  431. ret = vc4_plane_margins_adj(state);
  432. if (ret)
  433. return ret;
  434. vc4_state->x_scaling[0] = vc4_get_scaling_mode(vc4_state->src_w[0],
  435. vc4_state->crtc_w);
  436. vc4_state->y_scaling[0] = vc4_get_scaling_mode(vc4_state->src_h[0],
  437. vc4_state->crtc_h);
  438. vc4_state->is_unity = (vc4_state->x_scaling[0] == VC4_SCALING_NONE &&
  439. vc4_state->y_scaling[0] == VC4_SCALING_NONE);
  440. if (num_planes > 1) {
  441. vc4_state->is_yuv = true;
  442. vc4_state->src_w[1] = vc4_state->src_w[0] / h_subsample;
  443. vc4_state->src_h[1] = vc4_state->src_h[0] / v_subsample;
  444. vc4_state->x_scaling[1] =
  445. vc4_get_scaling_mode(vc4_state->src_w[1],
  446. vc4_state->crtc_w);
  447. vc4_state->y_scaling[1] =
  448. vc4_get_scaling_mode(vc4_state->src_h[1],
  449. vc4_state->crtc_h);
  450. /* YUV conversion requires that horizontal scaling be enabled
  451. * on the UV plane even if vc4_get_scaling_mode() returned
  452. * VC4_SCALING_NONE (which can happen when the down-scaling
  453. * ratio is 0.5). Let's force it to VC4_SCALING_PPF in this
  454. * case.
  455. */
  456. if (vc4_state->x_scaling[1] == VC4_SCALING_NONE)
  457. vc4_state->x_scaling[1] = VC4_SCALING_PPF;
  458. } else {
  459. vc4_state->is_yuv = false;
  460. vc4_state->x_scaling[1] = VC4_SCALING_NONE;
  461. vc4_state->y_scaling[1] = VC4_SCALING_NONE;
  462. }
  463. return 0;
  464. }
  465. static void vc4_write_tpz(struct vc4_plane_state *vc4_state, u32 src, u32 dst)
  466. {
  467. u32 scale, recip;
  468. scale = (1 << 16) * src / dst;
  469. /* The specs note that while the reciprocal would be defined
  470. * as (1<<32)/scale, ~0 is close enough.
  471. */
  472. recip = ~0 / scale;
  473. vc4_dlist_write(vc4_state,
  474. VC4_SET_FIELD(scale, SCALER_TPZ0_SCALE) |
  475. VC4_SET_FIELD(0, SCALER_TPZ0_IPHASE));
  476. vc4_dlist_write(vc4_state,
  477. VC4_SET_FIELD(recip, SCALER_TPZ1_RECIP));
  478. }
  479. static void vc4_write_ppf(struct vc4_plane_state *vc4_state, u32 src, u32 dst)
  480. {
  481. u32 scale = (1 << 16) * src / dst;
  482. vc4_dlist_write(vc4_state,
  483. SCALER_PPF_AGC |
  484. VC4_SET_FIELD(scale, SCALER_PPF_SCALE) |
  485. VC4_SET_FIELD(0, SCALER_PPF_IPHASE));
  486. }
  487. static u32 vc4_lbm_size(struct drm_plane_state *state)
  488. {
  489. struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
  490. struct vc4_dev *vc4 = to_vc4_dev(state->plane->dev);
  491. u32 pix_per_line;
  492. u32 lbm;
  493. /* LBM is not needed when there's no vertical scaling. */
  494. if (vc4_state->y_scaling[0] == VC4_SCALING_NONE &&
  495. vc4_state->y_scaling[1] == VC4_SCALING_NONE)
  496. return 0;
  497. /*
  498. * This can be further optimized in the RGB/YUV444 case if the PPF
  499. * decimation factor is between 0.5 and 1.0 by using crtc_w.
  500. *
  501. * It's not an issue though, since in that case since src_w[0] is going
  502. * to be greater than or equal to crtc_w.
  503. */
  504. if (vc4_state->x_scaling[0] == VC4_SCALING_TPZ)
  505. pix_per_line = vc4_state->crtc_w;
  506. else
  507. pix_per_line = vc4_state->src_w[0];
  508. if (!vc4_state->is_yuv) {
  509. if (vc4_state->y_scaling[0] == VC4_SCALING_TPZ)
  510. lbm = pix_per_line * 8;
  511. else {
  512. /* In special cases, this multiplier might be 12. */
  513. lbm = pix_per_line * 16;
  514. }
  515. } else {
  516. /* There are cases for this going down to a multiplier
  517. * of 2, but according to the firmware source, the
  518. * table in the docs is somewhat wrong.
  519. */
  520. lbm = pix_per_line * 16;
  521. }
  522. /* Align it to 64 or 128 (hvs5) bytes */
  523. lbm = roundup(lbm, vc4->gen == VC4_GEN_5 ? 128 : 64);
  524. /* Each "word" of the LBM memory contains 2 or 4 (hvs5) pixels */
  525. lbm /= vc4->gen == VC4_GEN_5 ? 4 : 2;
  526. return lbm;
  527. }
  528. static void vc4_write_scaling_parameters(struct drm_plane_state *state,
  529. int channel)
  530. {
  531. struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
  532. /* Ch0 H-PPF Word 0: Scaling Parameters */
  533. if (vc4_state->x_scaling[channel] == VC4_SCALING_PPF) {
  534. vc4_write_ppf(vc4_state,
  535. vc4_state->src_w[channel], vc4_state->crtc_w);
  536. }
  537. /* Ch0 V-PPF Words 0-1: Scaling Parameters, Context */
  538. if (vc4_state->y_scaling[channel] == VC4_SCALING_PPF) {
  539. vc4_write_ppf(vc4_state,
  540. vc4_state->src_h[channel], vc4_state->crtc_h);
  541. vc4_dlist_write(vc4_state, 0xc0c0c0c0);
  542. }
  543. /* Ch0 H-TPZ Words 0-1: Scaling Parameters, Recip */
  544. if (vc4_state->x_scaling[channel] == VC4_SCALING_TPZ) {
  545. vc4_write_tpz(vc4_state,
  546. vc4_state->src_w[channel], vc4_state->crtc_w);
  547. }
  548. /* Ch0 V-TPZ Words 0-2: Scaling Parameters, Recip, Context */
  549. if (vc4_state->y_scaling[channel] == VC4_SCALING_TPZ) {
  550. vc4_write_tpz(vc4_state,
  551. vc4_state->src_h[channel], vc4_state->crtc_h);
  552. vc4_dlist_write(vc4_state, 0xc0c0c0c0);
  553. }
  554. }
  555. static void vc4_plane_calc_load(struct drm_plane_state *state)
  556. {
  557. unsigned int hvs_load_shift, vrefresh, i;
  558. struct drm_framebuffer *fb = state->fb;
  559. struct vc4_plane_state *vc4_state;
  560. struct drm_crtc_state *crtc_state;
  561. unsigned int vscale_factor;
  562. vc4_state = to_vc4_plane_state(state);
  563. crtc_state = drm_atomic_get_existing_crtc_state(state->state,
  564. state->crtc);
  565. vrefresh = drm_mode_vrefresh(&crtc_state->adjusted_mode);
  566. /* The HVS is able to process 2 pixels/cycle when scaling the source,
  567. * 4 pixels/cycle otherwise.
  568. * Alpha blending step seems to be pipelined and it's always operating
  569. * at 4 pixels/cycle, so the limiting aspect here seems to be the
  570. * scaler block.
  571. * HVS load is expressed in clk-cycles/sec (AKA Hz).
  572. */
  573. if (vc4_state->x_scaling[0] != VC4_SCALING_NONE ||
  574. vc4_state->x_scaling[1] != VC4_SCALING_NONE ||
  575. vc4_state->y_scaling[0] != VC4_SCALING_NONE ||
  576. vc4_state->y_scaling[1] != VC4_SCALING_NONE)
  577. hvs_load_shift = 1;
  578. else
  579. hvs_load_shift = 2;
  580. vc4_state->membus_load = 0;
  581. vc4_state->hvs_load = 0;
  582. for (i = 0; i < fb->format->num_planes; i++) {
  583. /* Even if the bandwidth/plane required for a single frame is
  584. *
  585. * vc4_state->src_w[i] * vc4_state->src_h[i] * cpp * vrefresh
  586. *
  587. * when downscaling, we have to read more pixels per line in
  588. * the time frame reserved for a single line, so the bandwidth
  589. * demand can be punctually higher. To account for that, we
  590. * calculate the down-scaling factor and multiply the plane
  591. * load by this number. We're likely over-estimating the read
  592. * demand, but that's better than under-estimating it.
  593. */
  594. vscale_factor = DIV_ROUND_UP(vc4_state->src_h[i],
  595. vc4_state->crtc_h);
  596. vc4_state->membus_load += vc4_state->src_w[i] *
  597. vc4_state->src_h[i] * vscale_factor *
  598. fb->format->cpp[i];
  599. vc4_state->hvs_load += vc4_state->crtc_h * vc4_state->crtc_w;
  600. }
  601. vc4_state->hvs_load *= vrefresh;
  602. vc4_state->hvs_load >>= hvs_load_shift;
  603. vc4_state->membus_load *= vrefresh;
  604. }
  605. static int vc4_plane_allocate_lbm(struct drm_plane_state *state)
  606. {
  607. struct vc4_dev *vc4 = to_vc4_dev(state->plane->dev);
  608. struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
  609. unsigned long irqflags;
  610. u32 lbm_size;
  611. lbm_size = vc4_lbm_size(state);
  612. if (!lbm_size)
  613. return 0;
  614. if (WARN_ON(!vc4_state->lbm_offset))
  615. return -EINVAL;
  616. /* Allocate the LBM memory that the HVS will use for temporary
  617. * storage due to our scaling/format conversion.
  618. */
  619. if (!drm_mm_node_allocated(&vc4_state->lbm)) {
  620. int ret;
  621. spin_lock_irqsave(&vc4->hvs->mm_lock, irqflags);
  622. ret = drm_mm_insert_node_generic(&vc4->hvs->lbm_mm,
  623. &vc4_state->lbm,
  624. lbm_size,
  625. vc4->gen == VC4_GEN_5 ? 64 : 32,
  626. 0, 0);
  627. spin_unlock_irqrestore(&vc4->hvs->mm_lock, irqflags);
  628. if (ret)
  629. return ret;
  630. } else {
  631. WARN_ON_ONCE(lbm_size != vc4_state->lbm.size);
  632. }
  633. vc4_state->dlist[vc4_state->lbm_offset] = vc4_state->lbm.start;
  634. return 0;
  635. }
  636. /*
  637. * The colorspace conversion matrices are held in 3 entries in the dlist.
  638. * Create an array of them, with entries for each full and limited mode, and
  639. * each supported colorspace.
  640. */
  641. static const u32 colorspace_coeffs[2][DRM_COLOR_ENCODING_MAX][3] = {
  642. {
  643. /* Limited range */
  644. {
  645. /* BT601 */
  646. SCALER_CSC0_ITR_R_601_5,
  647. SCALER_CSC1_ITR_R_601_5,
  648. SCALER_CSC2_ITR_R_601_5,
  649. }, {
  650. /* BT709 */
  651. SCALER_CSC0_ITR_R_709_3,
  652. SCALER_CSC1_ITR_R_709_3,
  653. SCALER_CSC2_ITR_R_709_3,
  654. }, {
  655. /* BT2020 */
  656. SCALER_CSC0_ITR_R_2020,
  657. SCALER_CSC1_ITR_R_2020,
  658. SCALER_CSC2_ITR_R_2020,
  659. }
  660. }, {
  661. /* Full range */
  662. {
  663. /* JFIF */
  664. SCALER_CSC0_JPEG_JFIF,
  665. SCALER_CSC1_JPEG_JFIF,
  666. SCALER_CSC2_JPEG_JFIF,
  667. }, {
  668. /* BT709 */
  669. SCALER_CSC0_ITR_R_709_3_FR,
  670. SCALER_CSC1_ITR_R_709_3_FR,
  671. SCALER_CSC2_ITR_R_709_3_FR,
  672. }, {
  673. /* BT2020 */
  674. SCALER_CSC0_ITR_R_2020_FR,
  675. SCALER_CSC1_ITR_R_2020_FR,
  676. SCALER_CSC2_ITR_R_2020_FR,
  677. }
  678. }
  679. };
  680. static u32 vc4_hvs4_get_alpha_blend_mode(struct drm_plane_state *state)
  681. {
  682. if (!state->fb->format->has_alpha)
  683. return VC4_SET_FIELD(SCALER_POS2_ALPHA_MODE_FIXED,
  684. SCALER_POS2_ALPHA_MODE);
  685. switch (state->pixel_blend_mode) {
  686. case DRM_MODE_BLEND_PIXEL_NONE:
  687. return VC4_SET_FIELD(SCALER_POS2_ALPHA_MODE_FIXED,
  688. SCALER_POS2_ALPHA_MODE);
  689. default:
  690. case DRM_MODE_BLEND_PREMULTI:
  691. return VC4_SET_FIELD(SCALER_POS2_ALPHA_MODE_PIPELINE,
  692. SCALER_POS2_ALPHA_MODE) |
  693. SCALER_POS2_ALPHA_PREMULT;
  694. case DRM_MODE_BLEND_COVERAGE:
  695. return VC4_SET_FIELD(SCALER_POS2_ALPHA_MODE_PIPELINE,
  696. SCALER_POS2_ALPHA_MODE);
  697. }
  698. }
  699. static u32 vc4_hvs5_get_alpha_blend_mode(struct drm_plane_state *state)
  700. {
  701. if (!state->fb->format->has_alpha)
  702. return VC4_SET_FIELD(SCALER5_CTL2_ALPHA_MODE_FIXED,
  703. SCALER5_CTL2_ALPHA_MODE);
  704. switch (state->pixel_blend_mode) {
  705. case DRM_MODE_BLEND_PIXEL_NONE:
  706. return VC4_SET_FIELD(SCALER5_CTL2_ALPHA_MODE_FIXED,
  707. SCALER5_CTL2_ALPHA_MODE);
  708. default:
  709. case DRM_MODE_BLEND_PREMULTI:
  710. return VC4_SET_FIELD(SCALER5_CTL2_ALPHA_MODE_PIPELINE,
  711. SCALER5_CTL2_ALPHA_MODE) |
  712. SCALER5_CTL2_ALPHA_PREMULT;
  713. case DRM_MODE_BLEND_COVERAGE:
  714. return VC4_SET_FIELD(SCALER5_CTL2_ALPHA_MODE_PIPELINE,
  715. SCALER5_CTL2_ALPHA_MODE);
  716. }
  717. }
  718. /* Writes out a full display list for an active plane to the plane's
  719. * private dlist state.
  720. */
  721. static int vc4_plane_mode_set(struct drm_plane *plane,
  722. struct drm_plane_state *state)
  723. {
  724. struct vc4_dev *vc4 = to_vc4_dev(plane->dev);
  725. struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
  726. struct drm_framebuffer *fb = state->fb;
  727. u32 ctl0_offset = vc4_state->dlist_count;
  728. const struct hvs_format *format = vc4_get_hvs_format(fb->format->format);
  729. u64 base_format_mod = fourcc_mod_broadcom_mod(fb->modifier);
  730. int num_planes = fb->format->num_planes;
  731. u32 h_subsample = fb->format->hsub;
  732. u32 v_subsample = fb->format->vsub;
  733. bool mix_plane_alpha;
  734. bool covers_screen;
  735. u32 scl0, scl1, pitch0;
  736. u32 tiling, src_y;
  737. u32 hvs_format = format->hvs;
  738. unsigned int rotation;
  739. int ret, i;
  740. if (vc4_state->dlist_initialized)
  741. return 0;
  742. ret = vc4_plane_setup_clipping_and_scaling(state);
  743. if (ret)
  744. return ret;
  745. /* SCL1 is used for Cb/Cr scaling of planar formats. For RGB
  746. * and 4:4:4, scl1 should be set to scl0 so both channels of
  747. * the scaler do the same thing. For YUV, the Y plane needs
  748. * to be put in channel 1 and Cb/Cr in channel 0, so we swap
  749. * the scl fields here.
  750. */
  751. if (num_planes == 1) {
  752. scl0 = vc4_get_scl_field(state, 0);
  753. scl1 = scl0;
  754. } else {
  755. scl0 = vc4_get_scl_field(state, 1);
  756. scl1 = vc4_get_scl_field(state, 0);
  757. }
  758. rotation = drm_rotation_simplify(state->rotation,
  759. DRM_MODE_ROTATE_0 |
  760. DRM_MODE_REFLECT_X |
  761. DRM_MODE_REFLECT_Y);
  762. /* We must point to the last line when Y reflection is enabled. */
  763. src_y = vc4_state->src_y;
  764. if (rotation & DRM_MODE_REFLECT_Y)
  765. src_y += vc4_state->src_h[0] - 1;
  766. switch (base_format_mod) {
  767. case DRM_FORMAT_MOD_LINEAR:
  768. tiling = SCALER_CTL0_TILING_LINEAR;
  769. pitch0 = VC4_SET_FIELD(fb->pitches[0], SCALER_SRC_PITCH);
  770. /* Adjust the base pointer to the first pixel to be scanned
  771. * out.
  772. */
  773. for (i = 0; i < num_planes; i++) {
  774. vc4_state->offsets[i] += src_y /
  775. (i ? v_subsample : 1) *
  776. fb->pitches[i];
  777. vc4_state->offsets[i] += vc4_state->src_x /
  778. (i ? h_subsample : 1) *
  779. fb->format->cpp[i];
  780. }
  781. break;
  782. case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED: {
  783. u32 tile_size_shift = 12; /* T tiles are 4kb */
  784. /* Whole-tile offsets, mostly for setting the pitch. */
  785. u32 tile_w_shift = fb->format->cpp[0] == 2 ? 6 : 5;
  786. u32 tile_h_shift = 5; /* 16 and 32bpp are 32 pixels high */
  787. u32 tile_w_mask = (1 << tile_w_shift) - 1;
  788. /* The height mask on 32-bit-per-pixel tiles is 63, i.e. twice
  789. * the height (in pixels) of a 4k tile.
  790. */
  791. u32 tile_h_mask = (2 << tile_h_shift) - 1;
  792. /* For T-tiled, the FB pitch is "how many bytes from one row to
  793. * the next, such that
  794. *
  795. * pitch * tile_h == tile_size * tiles_per_row
  796. */
  797. u32 tiles_w = fb->pitches[0] >> (tile_size_shift - tile_h_shift);
  798. u32 tiles_l = vc4_state->src_x >> tile_w_shift;
  799. u32 tiles_r = tiles_w - tiles_l;
  800. u32 tiles_t = src_y >> tile_h_shift;
  801. /* Intra-tile offsets, which modify the base address (the
  802. * SCALER_PITCH0_TILE_Y_OFFSET tells HVS how to walk from that
  803. * base address).
  804. */
  805. u32 tile_y = (src_y >> 4) & 1;
  806. u32 subtile_y = (src_y >> 2) & 3;
  807. u32 utile_y = src_y & 3;
  808. u32 x_off = vc4_state->src_x & tile_w_mask;
  809. u32 y_off = src_y & tile_h_mask;
  810. /* When Y reflection is requested we must set the
  811. * SCALER_PITCH0_TILE_LINE_DIR flag to tell HVS that all lines
  812. * after the initial one should be fetched in descending order,
  813. * which makes sense since we start from the last line and go
  814. * backward.
  815. * Don't know why we need y_off = max_y_off - y_off, but it's
  816. * definitely required (I guess it's also related to the "going
  817. * backward" situation).
  818. */
  819. if (rotation & DRM_MODE_REFLECT_Y) {
  820. y_off = tile_h_mask - y_off;
  821. pitch0 = SCALER_PITCH0_TILE_LINE_DIR;
  822. } else {
  823. pitch0 = 0;
  824. }
  825. tiling = SCALER_CTL0_TILING_256B_OR_T;
  826. pitch0 |= (VC4_SET_FIELD(x_off, SCALER_PITCH0_SINK_PIX) |
  827. VC4_SET_FIELD(y_off, SCALER_PITCH0_TILE_Y_OFFSET) |
  828. VC4_SET_FIELD(tiles_l, SCALER_PITCH0_TILE_WIDTH_L) |
  829. VC4_SET_FIELD(tiles_r, SCALER_PITCH0_TILE_WIDTH_R));
  830. vc4_state->offsets[0] += tiles_t * (tiles_w << tile_size_shift);
  831. vc4_state->offsets[0] += subtile_y << 8;
  832. vc4_state->offsets[0] += utile_y << 4;
  833. /* Rows of tiles alternate left-to-right and right-to-left. */
  834. if (tiles_t & 1) {
  835. pitch0 |= SCALER_PITCH0_TILE_INITIAL_LINE_DIR;
  836. vc4_state->offsets[0] += (tiles_w - tiles_l) <<
  837. tile_size_shift;
  838. vc4_state->offsets[0] -= (1 + !tile_y) << 10;
  839. } else {
  840. vc4_state->offsets[0] += tiles_l << tile_size_shift;
  841. vc4_state->offsets[0] += tile_y << 10;
  842. }
  843. break;
  844. }
  845. case DRM_FORMAT_MOD_BROADCOM_SAND64:
  846. case DRM_FORMAT_MOD_BROADCOM_SAND128:
  847. case DRM_FORMAT_MOD_BROADCOM_SAND256: {
  848. uint32_t param = fourcc_mod_broadcom_param(fb->modifier);
  849. if (param > SCALER_TILE_HEIGHT_MASK) {
  850. DRM_DEBUG_KMS("SAND height too large (%d)\n",
  851. param);
  852. return -EINVAL;
  853. }
  854. if (fb->format->format == DRM_FORMAT_P030) {
  855. hvs_format = HVS_PIXEL_FORMAT_YCBCR_10BIT;
  856. tiling = SCALER_CTL0_TILING_128B;
  857. } else {
  858. hvs_format = HVS_PIXEL_FORMAT_H264;
  859. switch (base_format_mod) {
  860. case DRM_FORMAT_MOD_BROADCOM_SAND64:
  861. tiling = SCALER_CTL0_TILING_64B;
  862. break;
  863. case DRM_FORMAT_MOD_BROADCOM_SAND128:
  864. tiling = SCALER_CTL0_TILING_128B;
  865. break;
  866. case DRM_FORMAT_MOD_BROADCOM_SAND256:
  867. tiling = SCALER_CTL0_TILING_256B_OR_T;
  868. break;
  869. default:
  870. return -EINVAL;
  871. }
  872. }
  873. /* Adjust the base pointer to the first pixel to be scanned
  874. * out.
  875. *
  876. * For P030, y_ptr [31:4] is the 128bit word for the start pixel
  877. * y_ptr [3:0] is the pixel (0-11) contained within that 128bit
  878. * word that should be taken as the first pixel.
  879. * Ditto uv_ptr [31:4] vs [3:0], however [3:0] contains the
  880. * element within the 128bit word, eg for pixel 3 the value
  881. * should be 6.
  882. */
  883. for (i = 0; i < num_planes; i++) {
  884. u32 tile_w, tile, x_off, pix_per_tile;
  885. if (fb->format->format == DRM_FORMAT_P030) {
  886. /*
  887. * Spec says: bits [31:4] of the given address
  888. * should point to the 128-bit word containing
  889. * the desired starting pixel, and bits[3:0]
  890. * should be between 0 and 11, indicating which
  891. * of the 12-pixels in that 128-bit word is the
  892. * first pixel to be used
  893. */
  894. u32 remaining_pixels = vc4_state->src_x % 96;
  895. u32 aligned = remaining_pixels / 12;
  896. u32 last_bits = remaining_pixels % 12;
  897. x_off = aligned * 16 + last_bits;
  898. tile_w = 128;
  899. pix_per_tile = 96;
  900. } else {
  901. switch (base_format_mod) {
  902. case DRM_FORMAT_MOD_BROADCOM_SAND64:
  903. tile_w = 64;
  904. break;
  905. case DRM_FORMAT_MOD_BROADCOM_SAND128:
  906. tile_w = 128;
  907. break;
  908. case DRM_FORMAT_MOD_BROADCOM_SAND256:
  909. tile_w = 256;
  910. break;
  911. default:
  912. return -EINVAL;
  913. }
  914. pix_per_tile = tile_w / fb->format->cpp[0];
  915. x_off = (vc4_state->src_x % pix_per_tile) /
  916. (i ? h_subsample : 1) *
  917. fb->format->cpp[i];
  918. }
  919. tile = vc4_state->src_x / pix_per_tile;
  920. vc4_state->offsets[i] += param * tile_w * tile;
  921. vc4_state->offsets[i] += src_y /
  922. (i ? v_subsample : 1) *
  923. tile_w;
  924. vc4_state->offsets[i] += x_off & ~(i ? 1 : 0);
  925. }
  926. pitch0 = VC4_SET_FIELD(param, SCALER_TILE_HEIGHT);
  927. break;
  928. }
  929. default:
  930. DRM_DEBUG_KMS("Unsupported FB tiling flag 0x%16llx",
  931. (long long)fb->modifier);
  932. return -EINVAL;
  933. }
  934. /* Don't waste cycles mixing with plane alpha if the set alpha
  935. * is opaque or there is no per-pixel alpha information.
  936. * In any case we use the alpha property value as the fixed alpha.
  937. */
  938. mix_plane_alpha = state->alpha != DRM_BLEND_ALPHA_OPAQUE &&
  939. fb->format->has_alpha;
  940. if (vc4->gen == VC4_GEN_4) {
  941. /* Control word */
  942. vc4_dlist_write(vc4_state,
  943. SCALER_CTL0_VALID |
  944. (rotation & DRM_MODE_REFLECT_X ? SCALER_CTL0_HFLIP : 0) |
  945. (rotation & DRM_MODE_REFLECT_Y ? SCALER_CTL0_VFLIP : 0) |
  946. VC4_SET_FIELD(SCALER_CTL0_RGBA_EXPAND_ROUND, SCALER_CTL0_RGBA_EXPAND) |
  947. (format->pixel_order << SCALER_CTL0_ORDER_SHIFT) |
  948. (hvs_format << SCALER_CTL0_PIXEL_FORMAT_SHIFT) |
  949. VC4_SET_FIELD(tiling, SCALER_CTL0_TILING) |
  950. (vc4_state->is_unity ? SCALER_CTL0_UNITY : 0) |
  951. VC4_SET_FIELD(scl0, SCALER_CTL0_SCL0) |
  952. VC4_SET_FIELD(scl1, SCALER_CTL0_SCL1));
  953. /* Position Word 0: Image Positions and Alpha Value */
  954. vc4_state->pos0_offset = vc4_state->dlist_count;
  955. vc4_dlist_write(vc4_state,
  956. VC4_SET_FIELD(state->alpha >> 8, SCALER_POS0_FIXED_ALPHA) |
  957. VC4_SET_FIELD(vc4_state->crtc_x, SCALER_POS0_START_X) |
  958. VC4_SET_FIELD(vc4_state->crtc_y, SCALER_POS0_START_Y));
  959. /* Position Word 1: Scaled Image Dimensions. */
  960. if (!vc4_state->is_unity) {
  961. vc4_dlist_write(vc4_state,
  962. VC4_SET_FIELD(vc4_state->crtc_w,
  963. SCALER_POS1_SCL_WIDTH) |
  964. VC4_SET_FIELD(vc4_state->crtc_h,
  965. SCALER_POS1_SCL_HEIGHT));
  966. }
  967. /* Position Word 2: Source Image Size, Alpha */
  968. vc4_state->pos2_offset = vc4_state->dlist_count;
  969. vc4_dlist_write(vc4_state,
  970. (mix_plane_alpha ? SCALER_POS2_ALPHA_MIX : 0) |
  971. vc4_hvs4_get_alpha_blend_mode(state) |
  972. VC4_SET_FIELD(vc4_state->src_w[0],
  973. SCALER_POS2_WIDTH) |
  974. VC4_SET_FIELD(vc4_state->src_h[0],
  975. SCALER_POS2_HEIGHT));
  976. /* Position Word 3: Context. Written by the HVS. */
  977. vc4_dlist_write(vc4_state, 0xc0c0c0c0);
  978. } else {
  979. /* Control word */
  980. vc4_dlist_write(vc4_state,
  981. SCALER_CTL0_VALID |
  982. (format->pixel_order_hvs5 << SCALER_CTL0_ORDER_SHIFT) |
  983. (hvs_format << SCALER_CTL0_PIXEL_FORMAT_SHIFT) |
  984. VC4_SET_FIELD(tiling, SCALER_CTL0_TILING) |
  985. (vc4_state->is_unity ?
  986. SCALER5_CTL0_UNITY : 0) |
  987. VC4_SET_FIELD(scl0, SCALER_CTL0_SCL0) |
  988. VC4_SET_FIELD(scl1, SCALER_CTL0_SCL1) |
  989. SCALER5_CTL0_ALPHA_EXPAND |
  990. SCALER5_CTL0_RGB_EXPAND);
  991. /* Position Word 0: Image Positions and Alpha Value */
  992. vc4_state->pos0_offset = vc4_state->dlist_count;
  993. vc4_dlist_write(vc4_state,
  994. (rotation & DRM_MODE_REFLECT_Y ?
  995. SCALER5_POS0_VFLIP : 0) |
  996. VC4_SET_FIELD(vc4_state->crtc_x,
  997. SCALER_POS0_START_X) |
  998. (rotation & DRM_MODE_REFLECT_X ?
  999. SCALER5_POS0_HFLIP : 0) |
  1000. VC4_SET_FIELD(vc4_state->crtc_y,
  1001. SCALER5_POS0_START_Y)
  1002. );
  1003. /* Control Word 2 */
  1004. vc4_dlist_write(vc4_state,
  1005. VC4_SET_FIELD(state->alpha >> 4,
  1006. SCALER5_CTL2_ALPHA) |
  1007. vc4_hvs5_get_alpha_blend_mode(state) |
  1008. (mix_plane_alpha ?
  1009. SCALER5_CTL2_ALPHA_MIX : 0)
  1010. );
  1011. /* Position Word 1: Scaled Image Dimensions. */
  1012. if (!vc4_state->is_unity) {
  1013. vc4_dlist_write(vc4_state,
  1014. VC4_SET_FIELD(vc4_state->crtc_w,
  1015. SCALER5_POS1_SCL_WIDTH) |
  1016. VC4_SET_FIELD(vc4_state->crtc_h,
  1017. SCALER5_POS1_SCL_HEIGHT));
  1018. }
  1019. /* Position Word 2: Source Image Size */
  1020. vc4_state->pos2_offset = vc4_state->dlist_count;
  1021. vc4_dlist_write(vc4_state,
  1022. VC4_SET_FIELD(vc4_state->src_w[0],
  1023. SCALER5_POS2_WIDTH) |
  1024. VC4_SET_FIELD(vc4_state->src_h[0],
  1025. SCALER5_POS2_HEIGHT));
  1026. /* Position Word 3: Context. Written by the HVS. */
  1027. vc4_dlist_write(vc4_state, 0xc0c0c0c0);
  1028. }
  1029. /* Pointer Word 0/1/2: RGB / Y / Cb / Cr Pointers
  1030. *
  1031. * The pointers may be any byte address.
  1032. */
  1033. vc4_state->ptr0_offset = vc4_state->dlist_count;
  1034. for (i = 0; i < num_planes; i++)
  1035. vc4_dlist_write(vc4_state, vc4_state->offsets[i]);
  1036. /* Pointer Context Word 0/1/2: Written by the HVS */
  1037. for (i = 0; i < num_planes; i++)
  1038. vc4_dlist_write(vc4_state, 0xc0c0c0c0);
  1039. /* Pitch word 0 */
  1040. vc4_dlist_write(vc4_state, pitch0);
  1041. /* Pitch word 1/2 */
  1042. for (i = 1; i < num_planes; i++) {
  1043. if (hvs_format != HVS_PIXEL_FORMAT_H264 &&
  1044. hvs_format != HVS_PIXEL_FORMAT_YCBCR_10BIT) {
  1045. vc4_dlist_write(vc4_state,
  1046. VC4_SET_FIELD(fb->pitches[i],
  1047. SCALER_SRC_PITCH));
  1048. } else {
  1049. vc4_dlist_write(vc4_state, pitch0);
  1050. }
  1051. }
  1052. /* Colorspace conversion words */
  1053. if (vc4_state->is_yuv) {
  1054. enum drm_color_encoding color_encoding = state->color_encoding;
  1055. enum drm_color_range color_range = state->color_range;
  1056. const u32 *ccm;
  1057. if (color_encoding >= DRM_COLOR_ENCODING_MAX)
  1058. color_encoding = DRM_COLOR_YCBCR_BT601;
  1059. if (color_range >= DRM_COLOR_RANGE_MAX)
  1060. color_range = DRM_COLOR_YCBCR_LIMITED_RANGE;
  1061. ccm = colorspace_coeffs[color_range][color_encoding];
  1062. vc4_dlist_write(vc4_state, ccm[0]);
  1063. vc4_dlist_write(vc4_state, ccm[1]);
  1064. vc4_dlist_write(vc4_state, ccm[2]);
  1065. }
  1066. vc4_state->lbm_offset = 0;
  1067. if (vc4_state->x_scaling[0] != VC4_SCALING_NONE ||
  1068. vc4_state->x_scaling[1] != VC4_SCALING_NONE ||
  1069. vc4_state->y_scaling[0] != VC4_SCALING_NONE ||
  1070. vc4_state->y_scaling[1] != VC4_SCALING_NONE) {
  1071. /* Reserve a slot for the LBM Base Address. The real value will
  1072. * be set when calling vc4_plane_allocate_lbm().
  1073. */
  1074. if (vc4_state->y_scaling[0] != VC4_SCALING_NONE ||
  1075. vc4_state->y_scaling[1] != VC4_SCALING_NONE) {
  1076. vc4_state->lbm_offset = vc4_state->dlist_count;
  1077. vc4_dlist_counter_increment(vc4_state);
  1078. }
  1079. if (num_planes > 1) {
  1080. /* Emit Cb/Cr as channel 0 and Y as channel
  1081. * 1. This matches how we set up scl0/scl1
  1082. * above.
  1083. */
  1084. vc4_write_scaling_parameters(state, 1);
  1085. }
  1086. vc4_write_scaling_parameters(state, 0);
  1087. /* If any PPF setup was done, then all the kernel
  1088. * pointers get uploaded.
  1089. */
  1090. if (vc4_state->x_scaling[0] == VC4_SCALING_PPF ||
  1091. vc4_state->y_scaling[0] == VC4_SCALING_PPF ||
  1092. vc4_state->x_scaling[1] == VC4_SCALING_PPF ||
  1093. vc4_state->y_scaling[1] == VC4_SCALING_PPF) {
  1094. u32 kernel = VC4_SET_FIELD(vc4->hvs->mitchell_netravali_filter.start,
  1095. SCALER_PPF_KERNEL_OFFSET);
  1096. /* HPPF plane 0 */
  1097. vc4_dlist_write(vc4_state, kernel);
  1098. /* VPPF plane 0 */
  1099. vc4_dlist_write(vc4_state, kernel);
  1100. /* HPPF plane 1 */
  1101. vc4_dlist_write(vc4_state, kernel);
  1102. /* VPPF plane 1 */
  1103. vc4_dlist_write(vc4_state, kernel);
  1104. }
  1105. }
  1106. vc4_state->dlist[ctl0_offset] |=
  1107. VC4_SET_FIELD(vc4_state->dlist_count, SCALER_CTL0_SIZE);
  1108. /* crtc_* are already clipped coordinates. */
  1109. covers_screen = vc4_state->crtc_x == 0 && vc4_state->crtc_y == 0 &&
  1110. vc4_state->crtc_w == state->crtc->mode.hdisplay &&
  1111. vc4_state->crtc_h == state->crtc->mode.vdisplay;
  1112. /* Background fill might be necessary when the plane has per-pixel
  1113. * alpha content or a non-opaque plane alpha and could blend from the
  1114. * background or does not cover the entire screen.
  1115. */
  1116. vc4_state->needs_bg_fill = fb->format->has_alpha || !covers_screen ||
  1117. state->alpha != DRM_BLEND_ALPHA_OPAQUE;
  1118. /* Flag the dlist as initialized to avoid checking it twice in case
  1119. * the async update check already called vc4_plane_mode_set() and
  1120. * decided to fallback to sync update because async update was not
  1121. * possible.
  1122. */
  1123. vc4_state->dlist_initialized = 1;
  1124. vc4_plane_calc_load(state);
  1125. return 0;
  1126. }
  1127. /* If a modeset involves changing the setup of a plane, the atomic
  1128. * infrastructure will call this to validate a proposed plane setup.
  1129. * However, if a plane isn't getting updated, this (and the
  1130. * corresponding vc4_plane_atomic_update) won't get called. Thus, we
  1131. * compute the dlist here and have all active plane dlists get updated
  1132. * in the CRTC's flush.
  1133. */
  1134. static int vc4_plane_atomic_check(struct drm_plane *plane,
  1135. struct drm_atomic_state *state)
  1136. {
  1137. struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
  1138. plane);
  1139. struct vc4_plane_state *vc4_state = to_vc4_plane_state(new_plane_state);
  1140. int ret;
  1141. vc4_state->dlist_count = 0;
  1142. if (!plane_enabled(new_plane_state))
  1143. return 0;
  1144. ret = vc4_plane_mode_set(plane, new_plane_state);
  1145. if (ret)
  1146. return ret;
  1147. return vc4_plane_allocate_lbm(new_plane_state);
  1148. }
  1149. static void vc4_plane_atomic_update(struct drm_plane *plane,
  1150. struct drm_atomic_state *state)
  1151. {
  1152. /* No contents here. Since we don't know where in the CRTC's
  1153. * dlist we should be stored, our dlist is uploaded to the
  1154. * hardware with vc4_plane_write_dlist() at CRTC atomic_flush
  1155. * time.
  1156. */
  1157. }
  1158. u32 vc4_plane_write_dlist(struct drm_plane *plane, u32 __iomem *dlist)
  1159. {
  1160. struct vc4_plane_state *vc4_state = to_vc4_plane_state(plane->state);
  1161. int i;
  1162. int idx;
  1163. if (!drm_dev_enter(plane->dev, &idx))
  1164. goto out;
  1165. vc4_state->hw_dlist = dlist;
  1166. /* Can't memcpy_toio() because it needs to be 32-bit writes. */
  1167. for (i = 0; i < vc4_state->dlist_count; i++)
  1168. writel(vc4_state->dlist[i], &dlist[i]);
  1169. drm_dev_exit(idx);
  1170. out:
  1171. return vc4_state->dlist_count;
  1172. }
  1173. u32 vc4_plane_dlist_size(const struct drm_plane_state *state)
  1174. {
  1175. const struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
  1176. return vc4_state->dlist_count;
  1177. }
  1178. /* Updates the plane to immediately (well, once the FIFO needs
  1179. * refilling) scan out from at a new framebuffer.
  1180. */
  1181. void vc4_plane_async_set_fb(struct drm_plane *plane, struct drm_framebuffer *fb)
  1182. {
  1183. struct vc4_plane_state *vc4_state = to_vc4_plane_state(plane->state);
  1184. struct drm_gem_dma_object *bo = drm_fb_dma_get_gem_obj(fb, 0);
  1185. uint32_t addr;
  1186. int idx;
  1187. if (!drm_dev_enter(plane->dev, &idx))
  1188. return;
  1189. /* We're skipping the address adjustment for negative origin,
  1190. * because this is only called on the primary plane.
  1191. */
  1192. WARN_ON_ONCE(plane->state->crtc_x < 0 || plane->state->crtc_y < 0);
  1193. addr = bo->dma_addr + fb->offsets[0];
  1194. /* Write the new address into the hardware immediately. The
  1195. * scanout will start from this address as soon as the FIFO
  1196. * needs to refill with pixels.
  1197. */
  1198. writel(addr, &vc4_state->hw_dlist[vc4_state->ptr0_offset]);
  1199. /* Also update the CPU-side dlist copy, so that any later
  1200. * atomic updates that don't do a new modeset on our plane
  1201. * also use our updated address.
  1202. */
  1203. vc4_state->dlist[vc4_state->ptr0_offset] = addr;
  1204. drm_dev_exit(idx);
  1205. }
  1206. static void vc4_plane_atomic_async_update(struct drm_plane *plane,
  1207. struct drm_atomic_state *state)
  1208. {
  1209. struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
  1210. plane);
  1211. struct vc4_plane_state *vc4_state, *new_vc4_state;
  1212. int idx;
  1213. if (!drm_dev_enter(plane->dev, &idx))
  1214. return;
  1215. swap(plane->state->fb, new_plane_state->fb);
  1216. plane->state->crtc_x = new_plane_state->crtc_x;
  1217. plane->state->crtc_y = new_plane_state->crtc_y;
  1218. plane->state->crtc_w = new_plane_state->crtc_w;
  1219. plane->state->crtc_h = new_plane_state->crtc_h;
  1220. plane->state->src_x = new_plane_state->src_x;
  1221. plane->state->src_y = new_plane_state->src_y;
  1222. plane->state->src_w = new_plane_state->src_w;
  1223. plane->state->src_h = new_plane_state->src_h;
  1224. plane->state->alpha = new_plane_state->alpha;
  1225. plane->state->pixel_blend_mode = new_plane_state->pixel_blend_mode;
  1226. plane->state->rotation = new_plane_state->rotation;
  1227. plane->state->zpos = new_plane_state->zpos;
  1228. plane->state->normalized_zpos = new_plane_state->normalized_zpos;
  1229. plane->state->color_encoding = new_plane_state->color_encoding;
  1230. plane->state->color_range = new_plane_state->color_range;
  1231. plane->state->src = new_plane_state->src;
  1232. plane->state->dst = new_plane_state->dst;
  1233. plane->state->visible = new_plane_state->visible;
  1234. new_vc4_state = to_vc4_plane_state(new_plane_state);
  1235. vc4_state = to_vc4_plane_state(plane->state);
  1236. vc4_state->crtc_x = new_vc4_state->crtc_x;
  1237. vc4_state->crtc_y = new_vc4_state->crtc_y;
  1238. vc4_state->crtc_h = new_vc4_state->crtc_h;
  1239. vc4_state->crtc_w = new_vc4_state->crtc_w;
  1240. vc4_state->src_x = new_vc4_state->src_x;
  1241. vc4_state->src_y = new_vc4_state->src_y;
  1242. memcpy(vc4_state->src_w, new_vc4_state->src_w,
  1243. sizeof(vc4_state->src_w));
  1244. memcpy(vc4_state->src_h, new_vc4_state->src_h,
  1245. sizeof(vc4_state->src_h));
  1246. memcpy(vc4_state->x_scaling, new_vc4_state->x_scaling,
  1247. sizeof(vc4_state->x_scaling));
  1248. memcpy(vc4_state->y_scaling, new_vc4_state->y_scaling,
  1249. sizeof(vc4_state->y_scaling));
  1250. vc4_state->is_unity = new_vc4_state->is_unity;
  1251. vc4_state->is_yuv = new_vc4_state->is_yuv;
  1252. memcpy(vc4_state->offsets, new_vc4_state->offsets,
  1253. sizeof(vc4_state->offsets));
  1254. vc4_state->needs_bg_fill = new_vc4_state->needs_bg_fill;
  1255. /* Update the current vc4_state pos0, pos2 and ptr0 dlist entries. */
  1256. vc4_state->dlist[vc4_state->pos0_offset] =
  1257. new_vc4_state->dlist[vc4_state->pos0_offset];
  1258. vc4_state->dlist[vc4_state->pos2_offset] =
  1259. new_vc4_state->dlist[vc4_state->pos2_offset];
  1260. vc4_state->dlist[vc4_state->ptr0_offset] =
  1261. new_vc4_state->dlist[vc4_state->ptr0_offset];
  1262. /* Note that we can't just call vc4_plane_write_dlist()
  1263. * because that would smash the context data that the HVS is
  1264. * currently using.
  1265. */
  1266. writel(vc4_state->dlist[vc4_state->pos0_offset],
  1267. &vc4_state->hw_dlist[vc4_state->pos0_offset]);
  1268. writel(vc4_state->dlist[vc4_state->pos2_offset],
  1269. &vc4_state->hw_dlist[vc4_state->pos2_offset]);
  1270. writel(vc4_state->dlist[vc4_state->ptr0_offset],
  1271. &vc4_state->hw_dlist[vc4_state->ptr0_offset]);
  1272. drm_dev_exit(idx);
  1273. }
  1274. static int vc4_plane_atomic_async_check(struct drm_plane *plane,
  1275. struct drm_atomic_state *state)
  1276. {
  1277. struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
  1278. plane);
  1279. struct vc4_plane_state *old_vc4_state, *new_vc4_state;
  1280. int ret;
  1281. u32 i;
  1282. ret = vc4_plane_mode_set(plane, new_plane_state);
  1283. if (ret)
  1284. return ret;
  1285. old_vc4_state = to_vc4_plane_state(plane->state);
  1286. new_vc4_state = to_vc4_plane_state(new_plane_state);
  1287. if (!new_vc4_state->hw_dlist)
  1288. return -EINVAL;
  1289. if (old_vc4_state->dlist_count != new_vc4_state->dlist_count ||
  1290. old_vc4_state->pos0_offset != new_vc4_state->pos0_offset ||
  1291. old_vc4_state->pos2_offset != new_vc4_state->pos2_offset ||
  1292. old_vc4_state->ptr0_offset != new_vc4_state->ptr0_offset ||
  1293. vc4_lbm_size(plane->state) != vc4_lbm_size(new_plane_state))
  1294. return -EINVAL;
  1295. /* Only pos0, pos2 and ptr0 DWORDS can be updated in an async update
  1296. * if anything else has changed, fallback to a sync update.
  1297. */
  1298. for (i = 0; i < new_vc4_state->dlist_count; i++) {
  1299. if (i == new_vc4_state->pos0_offset ||
  1300. i == new_vc4_state->pos2_offset ||
  1301. i == new_vc4_state->ptr0_offset ||
  1302. (new_vc4_state->lbm_offset &&
  1303. i == new_vc4_state->lbm_offset))
  1304. continue;
  1305. if (new_vc4_state->dlist[i] != old_vc4_state->dlist[i])
  1306. return -EINVAL;
  1307. }
  1308. return 0;
  1309. }
  1310. static int vc4_prepare_fb(struct drm_plane *plane,
  1311. struct drm_plane_state *state)
  1312. {
  1313. struct vc4_bo *bo;
  1314. int ret;
  1315. if (!state->fb)
  1316. return 0;
  1317. bo = to_vc4_bo(&drm_fb_dma_get_gem_obj(state->fb, 0)->base);
  1318. ret = drm_gem_plane_helper_prepare_fb(plane, state);
  1319. if (ret)
  1320. return ret;
  1321. return vc4_bo_inc_usecnt(bo);
  1322. }
  1323. static void vc4_cleanup_fb(struct drm_plane *plane,
  1324. struct drm_plane_state *state)
  1325. {
  1326. struct vc4_bo *bo;
  1327. if (!state->fb)
  1328. return;
  1329. bo = to_vc4_bo(&drm_fb_dma_get_gem_obj(state->fb, 0)->base);
  1330. vc4_bo_dec_usecnt(bo);
  1331. }
  1332. static const struct drm_plane_helper_funcs vc4_plane_helper_funcs = {
  1333. .atomic_check = vc4_plane_atomic_check,
  1334. .atomic_update = vc4_plane_atomic_update,
  1335. .prepare_fb = vc4_prepare_fb,
  1336. .cleanup_fb = vc4_cleanup_fb,
  1337. .atomic_async_check = vc4_plane_atomic_async_check,
  1338. .atomic_async_update = vc4_plane_atomic_async_update,
  1339. };
  1340. static const struct drm_plane_helper_funcs vc5_plane_helper_funcs = {
  1341. .atomic_check = vc4_plane_atomic_check,
  1342. .atomic_update = vc4_plane_atomic_update,
  1343. .atomic_async_check = vc4_plane_atomic_async_check,
  1344. .atomic_async_update = vc4_plane_atomic_async_update,
  1345. };
  1346. static bool vc4_format_mod_supported(struct drm_plane *plane,
  1347. uint32_t format,
  1348. uint64_t modifier)
  1349. {
  1350. /* Support T_TILING for RGB formats only. */
  1351. switch (format) {
  1352. case DRM_FORMAT_XRGB8888:
  1353. case DRM_FORMAT_ARGB8888:
  1354. case DRM_FORMAT_ABGR8888:
  1355. case DRM_FORMAT_XBGR8888:
  1356. case DRM_FORMAT_RGB565:
  1357. case DRM_FORMAT_BGR565:
  1358. case DRM_FORMAT_ARGB1555:
  1359. case DRM_FORMAT_XRGB1555:
  1360. switch (fourcc_mod_broadcom_mod(modifier)) {
  1361. case DRM_FORMAT_MOD_LINEAR:
  1362. case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED:
  1363. return true;
  1364. default:
  1365. return false;
  1366. }
  1367. case DRM_FORMAT_NV12:
  1368. case DRM_FORMAT_NV21:
  1369. switch (fourcc_mod_broadcom_mod(modifier)) {
  1370. case DRM_FORMAT_MOD_LINEAR:
  1371. case DRM_FORMAT_MOD_BROADCOM_SAND64:
  1372. case DRM_FORMAT_MOD_BROADCOM_SAND128:
  1373. case DRM_FORMAT_MOD_BROADCOM_SAND256:
  1374. return true;
  1375. default:
  1376. return false;
  1377. }
  1378. case DRM_FORMAT_P030:
  1379. switch (fourcc_mod_broadcom_mod(modifier)) {
  1380. case DRM_FORMAT_MOD_BROADCOM_SAND128:
  1381. return true;
  1382. default:
  1383. return false;
  1384. }
  1385. case DRM_FORMAT_RGBX1010102:
  1386. case DRM_FORMAT_BGRX1010102:
  1387. case DRM_FORMAT_RGBA1010102:
  1388. case DRM_FORMAT_BGRA1010102:
  1389. case DRM_FORMAT_XRGB4444:
  1390. case DRM_FORMAT_ARGB4444:
  1391. case DRM_FORMAT_XBGR4444:
  1392. case DRM_FORMAT_ABGR4444:
  1393. case DRM_FORMAT_RGBX4444:
  1394. case DRM_FORMAT_RGBA4444:
  1395. case DRM_FORMAT_BGRX4444:
  1396. case DRM_FORMAT_BGRA4444:
  1397. case DRM_FORMAT_RGB332:
  1398. case DRM_FORMAT_BGR233:
  1399. case DRM_FORMAT_YUV422:
  1400. case DRM_FORMAT_YVU422:
  1401. case DRM_FORMAT_YUV420:
  1402. case DRM_FORMAT_YVU420:
  1403. case DRM_FORMAT_NV16:
  1404. case DRM_FORMAT_NV61:
  1405. default:
  1406. return (modifier == DRM_FORMAT_MOD_LINEAR);
  1407. }
  1408. }
  1409. static const struct drm_plane_funcs vc4_plane_funcs = {
  1410. .update_plane = drm_atomic_helper_update_plane,
  1411. .disable_plane = drm_atomic_helper_disable_plane,
  1412. .reset = vc4_plane_reset,
  1413. .atomic_duplicate_state = vc4_plane_duplicate_state,
  1414. .atomic_destroy_state = vc4_plane_destroy_state,
  1415. .format_mod_supported = vc4_format_mod_supported,
  1416. };
  1417. struct drm_plane *vc4_plane_init(struct drm_device *dev,
  1418. enum drm_plane_type type,
  1419. uint32_t possible_crtcs)
  1420. {
  1421. struct vc4_dev *vc4 = to_vc4_dev(dev);
  1422. struct drm_plane *plane;
  1423. struct vc4_plane *vc4_plane;
  1424. u32 formats[ARRAY_SIZE(hvs_formats)];
  1425. int num_formats = 0;
  1426. unsigned i;
  1427. static const uint64_t modifiers[] = {
  1428. DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED,
  1429. DRM_FORMAT_MOD_BROADCOM_SAND128,
  1430. DRM_FORMAT_MOD_BROADCOM_SAND64,
  1431. DRM_FORMAT_MOD_BROADCOM_SAND256,
  1432. DRM_FORMAT_MOD_LINEAR,
  1433. DRM_FORMAT_MOD_INVALID
  1434. };
  1435. for (i = 0; i < ARRAY_SIZE(hvs_formats); i++) {
  1436. if (!hvs_formats[i].hvs5_only || vc4->gen == VC4_GEN_5) {
  1437. formats[num_formats] = hvs_formats[i].drm;
  1438. num_formats++;
  1439. }
  1440. }
  1441. vc4_plane = drmm_universal_plane_alloc(dev, struct vc4_plane, base,
  1442. possible_crtcs,
  1443. &vc4_plane_funcs,
  1444. formats, num_formats,
  1445. modifiers, type, NULL);
  1446. if (IS_ERR(vc4_plane))
  1447. return ERR_CAST(vc4_plane);
  1448. plane = &vc4_plane->base;
  1449. if (vc4->gen == VC4_GEN_5)
  1450. drm_plane_helper_add(plane, &vc5_plane_helper_funcs);
  1451. else
  1452. drm_plane_helper_add(plane, &vc4_plane_helper_funcs);
  1453. drm_plane_create_alpha_property(plane);
  1454. drm_plane_create_blend_mode_property(plane,
  1455. BIT(DRM_MODE_BLEND_PIXEL_NONE) |
  1456. BIT(DRM_MODE_BLEND_PREMULTI) |
  1457. BIT(DRM_MODE_BLEND_COVERAGE));
  1458. drm_plane_create_rotation_property(plane, DRM_MODE_ROTATE_0,
  1459. DRM_MODE_ROTATE_0 |
  1460. DRM_MODE_ROTATE_180 |
  1461. DRM_MODE_REFLECT_X |
  1462. DRM_MODE_REFLECT_Y);
  1463. drm_plane_create_color_properties(plane,
  1464. BIT(DRM_COLOR_YCBCR_BT601) |
  1465. BIT(DRM_COLOR_YCBCR_BT709) |
  1466. BIT(DRM_COLOR_YCBCR_BT2020),
  1467. BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) |
  1468. BIT(DRM_COLOR_YCBCR_FULL_RANGE),
  1469. DRM_COLOR_YCBCR_BT709,
  1470. DRM_COLOR_YCBCR_LIMITED_RANGE);
  1471. if (type == DRM_PLANE_TYPE_PRIMARY)
  1472. drm_plane_create_zpos_immutable_property(plane, 0);
  1473. return plane;
  1474. }
  1475. #define VC4_NUM_OVERLAY_PLANES 16
  1476. int vc4_plane_create_additional_planes(struct drm_device *drm)
  1477. {
  1478. struct drm_plane *cursor_plane;
  1479. struct drm_crtc *crtc;
  1480. unsigned int i;
  1481. /* Set up some arbitrary number of planes. We're not limited
  1482. * by a set number of physical registers, just the space in
  1483. * the HVS (16k) and how small an plane can be (28 bytes).
  1484. * However, each plane we set up takes up some memory, and
  1485. * increases the cost of looping over planes, which atomic
  1486. * modesetting does quite a bit. As a result, we pick a
  1487. * modest number of planes to expose, that should hopefully
  1488. * still cover any sane usecase.
  1489. */
  1490. for (i = 0; i < VC4_NUM_OVERLAY_PLANES; i++) {
  1491. struct drm_plane *plane =
  1492. vc4_plane_init(drm, DRM_PLANE_TYPE_OVERLAY,
  1493. GENMASK(drm->mode_config.num_crtc - 1, 0));
  1494. if (IS_ERR(plane))
  1495. continue;
  1496. /* Create zpos property. Max of all the overlays + 1 primary +
  1497. * 1 cursor plane on a crtc.
  1498. */
  1499. drm_plane_create_zpos_property(plane, i + 1, 1,
  1500. VC4_NUM_OVERLAY_PLANES + 1);
  1501. }
  1502. drm_for_each_crtc(crtc, drm) {
  1503. /* Set up the legacy cursor after overlay initialization,
  1504. * since the zpos fallback is that planes are rendered by plane
  1505. * ID order, and that then puts the cursor on top.
  1506. */
  1507. cursor_plane = vc4_plane_init(drm, DRM_PLANE_TYPE_CURSOR,
  1508. drm_crtc_mask(crtc));
  1509. if (!IS_ERR(cursor_plane)) {
  1510. crtc->cursor = cursor_plane;
  1511. drm_plane_create_zpos_property(cursor_plane,
  1512. VC4_NUM_OVERLAY_PLANES + 1,
  1513. 1,
  1514. VC4_NUM_OVERLAY_PLANES + 1);
  1515. }
  1516. }
  1517. return 0;
  1518. }