ipuv3-plane.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * i.MX IPUv3 DP Overlay Planes
  4. *
  5. * Copyright (C) 2013 Philipp Zabel, Pengutronix
  6. */
  7. #include <drm/drm_atomic.h>
  8. #include <drm/drm_atomic_helper.h>
  9. #include <drm/drm_blend.h>
  10. #include <drm/drm_fb_dma_helper.h>
  11. #include <drm/drm_fourcc.h>
  12. #include <drm/drm_framebuffer.h>
  13. #include <drm/drm_gem_atomic_helper.h>
  14. #include <drm/drm_gem_dma_helper.h>
  15. #include <drm/drm_managed.h>
  16. #include <drm/drm_print.h>
  17. #include <video/imx-ipu-v3.h>
  18. #include "imx-drm.h"
  19. #include "ipuv3-plane.h"
  20. struct ipu_plane_state {
  21. struct drm_plane_state base;
  22. bool use_pre;
  23. };
  24. static inline struct ipu_plane_state *
  25. to_ipu_plane_state(struct drm_plane_state *p)
  26. {
  27. return container_of(p, struct ipu_plane_state, base);
  28. }
  29. static unsigned int ipu_src_rect_width(const struct drm_plane_state *state)
  30. {
  31. return ALIGN(drm_rect_width(&state->src) >> 16, 8);
  32. }
  33. static inline struct ipu_plane *to_ipu_plane(struct drm_plane *p)
  34. {
  35. return container_of(p, struct ipu_plane, base);
  36. }
  37. static const uint32_t ipu_plane_all_formats[] = {
  38. DRM_FORMAT_ARGB1555,
  39. DRM_FORMAT_XRGB1555,
  40. DRM_FORMAT_ABGR1555,
  41. DRM_FORMAT_XBGR1555,
  42. DRM_FORMAT_RGBA5551,
  43. DRM_FORMAT_BGRA5551,
  44. DRM_FORMAT_ARGB4444,
  45. DRM_FORMAT_ARGB8888,
  46. DRM_FORMAT_XRGB8888,
  47. DRM_FORMAT_ABGR8888,
  48. DRM_FORMAT_XBGR8888,
  49. DRM_FORMAT_RGBA8888,
  50. DRM_FORMAT_RGBX8888,
  51. DRM_FORMAT_BGRA8888,
  52. DRM_FORMAT_BGRX8888,
  53. DRM_FORMAT_UYVY,
  54. DRM_FORMAT_VYUY,
  55. DRM_FORMAT_YUYV,
  56. DRM_FORMAT_YVYU,
  57. DRM_FORMAT_YUV420,
  58. DRM_FORMAT_YVU420,
  59. DRM_FORMAT_YUV422,
  60. DRM_FORMAT_YVU422,
  61. DRM_FORMAT_YUV444,
  62. DRM_FORMAT_YVU444,
  63. DRM_FORMAT_NV12,
  64. DRM_FORMAT_NV16,
  65. DRM_FORMAT_RGB565,
  66. DRM_FORMAT_RGB565_A8,
  67. DRM_FORMAT_BGR565_A8,
  68. DRM_FORMAT_RGB888_A8,
  69. DRM_FORMAT_BGR888_A8,
  70. DRM_FORMAT_RGBX8888_A8,
  71. DRM_FORMAT_BGRX8888_A8,
  72. };
  73. static const uint32_t ipu_plane_rgb_formats[] = {
  74. DRM_FORMAT_ARGB1555,
  75. DRM_FORMAT_XRGB1555,
  76. DRM_FORMAT_ABGR1555,
  77. DRM_FORMAT_XBGR1555,
  78. DRM_FORMAT_RGBA5551,
  79. DRM_FORMAT_BGRA5551,
  80. DRM_FORMAT_ARGB4444,
  81. DRM_FORMAT_ARGB8888,
  82. DRM_FORMAT_XRGB8888,
  83. DRM_FORMAT_ABGR8888,
  84. DRM_FORMAT_XBGR8888,
  85. DRM_FORMAT_RGBA8888,
  86. DRM_FORMAT_RGBX8888,
  87. DRM_FORMAT_BGRA8888,
  88. DRM_FORMAT_BGRX8888,
  89. DRM_FORMAT_RGB565,
  90. DRM_FORMAT_RGB565_A8,
  91. DRM_FORMAT_BGR565_A8,
  92. DRM_FORMAT_RGB888_A8,
  93. DRM_FORMAT_BGR888_A8,
  94. DRM_FORMAT_RGBX8888_A8,
  95. DRM_FORMAT_BGRX8888_A8,
  96. };
  97. static const uint64_t ipu_format_modifiers[] = {
  98. DRM_FORMAT_MOD_LINEAR,
  99. DRM_FORMAT_MOD_INVALID
  100. };
  101. static const uint64_t pre_format_modifiers[] = {
  102. DRM_FORMAT_MOD_LINEAR,
  103. DRM_FORMAT_MOD_VIVANTE_TILED,
  104. DRM_FORMAT_MOD_VIVANTE_SUPER_TILED,
  105. DRM_FORMAT_MOD_INVALID
  106. };
  107. int ipu_plane_irq(struct ipu_plane *ipu_plane)
  108. {
  109. return ipu_idmac_channel_irq(ipu_plane->ipu, ipu_plane->ipu_ch,
  110. IPU_IRQ_EOF);
  111. }
  112. static inline unsigned long
  113. drm_plane_state_to_eba(struct drm_plane_state *state, int plane)
  114. {
  115. struct drm_framebuffer *fb = state->fb;
  116. struct drm_gem_dma_object *dma_obj;
  117. int x = state->src.x1 >> 16;
  118. int y = state->src.y1 >> 16;
  119. dma_obj = drm_fb_dma_get_gem_obj(fb, plane);
  120. BUG_ON(!dma_obj);
  121. return dma_obj->dma_addr + fb->offsets[plane] + fb->pitches[plane] * y +
  122. fb->format->cpp[plane] * x;
  123. }
  124. static inline unsigned long
  125. drm_plane_state_to_ubo(struct drm_plane_state *state)
  126. {
  127. struct drm_framebuffer *fb = state->fb;
  128. struct drm_gem_dma_object *dma_obj;
  129. unsigned long eba = drm_plane_state_to_eba(state, 0);
  130. int x = state->src.x1 >> 16;
  131. int y = state->src.y1 >> 16;
  132. dma_obj = drm_fb_dma_get_gem_obj(fb, 1);
  133. BUG_ON(!dma_obj);
  134. x /= fb->format->hsub;
  135. y /= fb->format->vsub;
  136. return dma_obj->dma_addr + fb->offsets[1] + fb->pitches[1] * y +
  137. fb->format->cpp[1] * x - eba;
  138. }
  139. static inline unsigned long
  140. drm_plane_state_to_vbo(struct drm_plane_state *state)
  141. {
  142. struct drm_framebuffer *fb = state->fb;
  143. struct drm_gem_dma_object *dma_obj;
  144. unsigned long eba = drm_plane_state_to_eba(state, 0);
  145. int x = state->src.x1 >> 16;
  146. int y = state->src.y1 >> 16;
  147. dma_obj = drm_fb_dma_get_gem_obj(fb, 2);
  148. BUG_ON(!dma_obj);
  149. x /= fb->format->hsub;
  150. y /= fb->format->vsub;
  151. return dma_obj->dma_addr + fb->offsets[2] + fb->pitches[2] * y +
  152. fb->format->cpp[2] * x - eba;
  153. }
  154. static void ipu_plane_put_resources(struct drm_device *dev, void *ptr)
  155. {
  156. struct ipu_plane *ipu_plane = ptr;
  157. if (!IS_ERR_OR_NULL(ipu_plane->dp))
  158. ipu_dp_put(ipu_plane->dp);
  159. if (!IS_ERR_OR_NULL(ipu_plane->dmfc))
  160. ipu_dmfc_put(ipu_plane->dmfc);
  161. if (!IS_ERR_OR_NULL(ipu_plane->ipu_ch))
  162. ipu_idmac_put(ipu_plane->ipu_ch);
  163. if (!IS_ERR_OR_NULL(ipu_plane->alpha_ch))
  164. ipu_idmac_put(ipu_plane->alpha_ch);
  165. }
  166. static int ipu_plane_get_resources(struct drm_device *dev,
  167. struct ipu_plane *ipu_plane)
  168. {
  169. int ret;
  170. int alpha_ch;
  171. ipu_plane->ipu_ch = ipu_idmac_get(ipu_plane->ipu, ipu_plane->dma);
  172. if (IS_ERR(ipu_plane->ipu_ch)) {
  173. ret = PTR_ERR(ipu_plane->ipu_ch);
  174. DRM_ERROR("failed to get idmac channel: %d\n", ret);
  175. return ret;
  176. }
  177. ret = drmm_add_action_or_reset(dev, ipu_plane_put_resources, ipu_plane);
  178. if (ret)
  179. return ret;
  180. alpha_ch = ipu_channel_alpha_channel(ipu_plane->dma);
  181. if (alpha_ch >= 0) {
  182. ipu_plane->alpha_ch = ipu_idmac_get(ipu_plane->ipu, alpha_ch);
  183. if (IS_ERR(ipu_plane->alpha_ch)) {
  184. ret = PTR_ERR(ipu_plane->alpha_ch);
  185. DRM_ERROR("failed to get alpha idmac channel %d: %d\n",
  186. alpha_ch, ret);
  187. return ret;
  188. }
  189. }
  190. ipu_plane->dmfc = ipu_dmfc_get(ipu_plane->ipu, ipu_plane->dma);
  191. if (IS_ERR(ipu_plane->dmfc)) {
  192. ret = PTR_ERR(ipu_plane->dmfc);
  193. DRM_ERROR("failed to get dmfc: ret %d\n", ret);
  194. return ret;
  195. }
  196. if (ipu_plane->dp_flow >= 0) {
  197. ipu_plane->dp = ipu_dp_get(ipu_plane->ipu, ipu_plane->dp_flow);
  198. if (IS_ERR(ipu_plane->dp)) {
  199. ret = PTR_ERR(ipu_plane->dp);
  200. DRM_ERROR("failed to get dp flow: %d\n", ret);
  201. return ret;
  202. }
  203. }
  204. return 0;
  205. }
  206. static bool ipu_plane_separate_alpha(struct ipu_plane *ipu_plane)
  207. {
  208. switch (ipu_plane->base.state->fb->format->format) {
  209. case DRM_FORMAT_RGB565_A8:
  210. case DRM_FORMAT_BGR565_A8:
  211. case DRM_FORMAT_RGB888_A8:
  212. case DRM_FORMAT_BGR888_A8:
  213. case DRM_FORMAT_RGBX8888_A8:
  214. case DRM_FORMAT_BGRX8888_A8:
  215. return true;
  216. default:
  217. return false;
  218. }
  219. }
  220. static void ipu_plane_enable(struct ipu_plane *ipu_plane)
  221. {
  222. if (ipu_plane->dp)
  223. ipu_dp_enable(ipu_plane->ipu);
  224. ipu_dmfc_enable_channel(ipu_plane->dmfc);
  225. ipu_idmac_enable_channel(ipu_plane->ipu_ch);
  226. if (ipu_plane_separate_alpha(ipu_plane))
  227. ipu_idmac_enable_channel(ipu_plane->alpha_ch);
  228. if (ipu_plane->dp)
  229. ipu_dp_enable_channel(ipu_plane->dp);
  230. }
  231. void ipu_plane_disable(struct ipu_plane *ipu_plane, bool disable_dp_channel)
  232. {
  233. int ret;
  234. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  235. ret = ipu_idmac_wait_busy(ipu_plane->ipu_ch, 50);
  236. if (ret == -ETIMEDOUT) {
  237. DRM_ERROR("[PLANE:%d] IDMAC timeout\n",
  238. ipu_plane->base.base.id);
  239. }
  240. if (ipu_plane->dp && disable_dp_channel)
  241. ipu_dp_disable_channel(ipu_plane->dp, false);
  242. ipu_idmac_disable_channel(ipu_plane->ipu_ch);
  243. if (ipu_plane->alpha_ch)
  244. ipu_idmac_disable_channel(ipu_plane->alpha_ch);
  245. ipu_dmfc_disable_channel(ipu_plane->dmfc);
  246. if (ipu_plane->dp)
  247. ipu_dp_disable(ipu_plane->ipu);
  248. if (ipu_prg_present(ipu_plane->ipu))
  249. ipu_prg_channel_disable(ipu_plane->ipu_ch);
  250. }
  251. void ipu_plane_disable_deferred(struct drm_plane *plane)
  252. {
  253. struct ipu_plane *ipu_plane = to_ipu_plane(plane);
  254. if (ipu_plane->disabling) {
  255. ipu_plane->disabling = false;
  256. ipu_plane_disable(ipu_plane, false);
  257. }
  258. }
  259. static void ipu_plane_state_reset(struct drm_plane *plane)
  260. {
  261. struct ipu_plane_state *ipu_state;
  262. if (plane->state) {
  263. ipu_state = to_ipu_plane_state(plane->state);
  264. __drm_atomic_helper_plane_destroy_state(plane->state);
  265. kfree(ipu_state);
  266. plane->state = NULL;
  267. }
  268. ipu_state = kzalloc_obj(*ipu_state);
  269. if (ipu_state)
  270. __drm_atomic_helper_plane_reset(plane, &ipu_state->base);
  271. }
  272. static struct drm_plane_state *
  273. ipu_plane_duplicate_state(struct drm_plane *plane)
  274. {
  275. struct ipu_plane_state *state;
  276. if (WARN_ON(!plane->state))
  277. return NULL;
  278. state = kmalloc_obj(*state);
  279. if (state)
  280. __drm_atomic_helper_plane_duplicate_state(plane, &state->base);
  281. return &state->base;
  282. }
  283. static void ipu_plane_destroy_state(struct drm_plane *plane,
  284. struct drm_plane_state *state)
  285. {
  286. struct ipu_plane_state *ipu_state = to_ipu_plane_state(state);
  287. __drm_atomic_helper_plane_destroy_state(state);
  288. kfree(ipu_state);
  289. }
  290. static bool ipu_plane_format_mod_supported(struct drm_plane *plane,
  291. uint32_t format, uint64_t modifier)
  292. {
  293. struct ipu_soc *ipu = to_ipu_plane(plane)->ipu;
  294. /* linear is supported for all planes and formats */
  295. if (modifier == DRM_FORMAT_MOD_LINEAR)
  296. return true;
  297. /*
  298. * Without a PRG the possible modifiers list only includes the linear
  299. * modifier, so we always take the early return from this function and
  300. * only end up here if the PRG is present.
  301. */
  302. return ipu_prg_format_supported(ipu, format, modifier);
  303. }
  304. static const struct drm_plane_funcs ipu_plane_funcs = {
  305. .update_plane = drm_atomic_helper_update_plane,
  306. .disable_plane = drm_atomic_helper_disable_plane,
  307. .reset = ipu_plane_state_reset,
  308. .atomic_duplicate_state = ipu_plane_duplicate_state,
  309. .atomic_destroy_state = ipu_plane_destroy_state,
  310. .format_mod_supported = ipu_plane_format_mod_supported,
  311. };
  312. static int ipu_plane_atomic_check(struct drm_plane *plane,
  313. struct drm_atomic_state *state)
  314. {
  315. struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
  316. plane);
  317. struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
  318. plane);
  319. struct drm_crtc_state *crtc_state;
  320. struct device *dev = plane->dev->dev;
  321. struct drm_framebuffer *fb = new_state->fb;
  322. struct drm_framebuffer *old_fb = old_state->fb;
  323. unsigned long eba, ubo, vbo, old_ubo, old_vbo, alpha_eba;
  324. bool can_position = (plane->type == DRM_PLANE_TYPE_OVERLAY);
  325. int ret;
  326. /* Ok to disable */
  327. if (!fb)
  328. return 0;
  329. if (WARN_ON(!new_state->crtc))
  330. return -EINVAL;
  331. crtc_state =
  332. drm_atomic_get_new_crtc_state(state, new_state->crtc);
  333. if (WARN_ON(!crtc_state))
  334. return -EINVAL;
  335. ret = drm_atomic_helper_check_plane_state(new_state, crtc_state,
  336. DRM_PLANE_NO_SCALING,
  337. DRM_PLANE_NO_SCALING,
  338. can_position, true);
  339. if (ret)
  340. return ret;
  341. /* nothing to check when disabling or disabled */
  342. if (!crtc_state->enable)
  343. return 0;
  344. switch (plane->type) {
  345. case DRM_PLANE_TYPE_PRIMARY:
  346. /* full plane minimum width is 13 pixels */
  347. if (drm_rect_width(&new_state->dst) < 13)
  348. return -EINVAL;
  349. break;
  350. case DRM_PLANE_TYPE_OVERLAY:
  351. break;
  352. default:
  353. dev_warn(dev, "Unsupported plane type %d\n", plane->type);
  354. return -EINVAL;
  355. }
  356. if (drm_rect_height(&new_state->dst) < 2)
  357. return -EINVAL;
  358. /*
  359. * We support resizing active plane or changing its format by
  360. * forcing CRTC mode change in plane's ->atomic_check callback
  361. * and disabling all affected active planes in CRTC's ->atomic_disable
  362. * callback. The planes will be reenabled in plane's ->atomic_update
  363. * callback.
  364. */
  365. if (old_fb &&
  366. (drm_rect_width(&new_state->dst) != drm_rect_width(&old_state->dst) ||
  367. drm_rect_height(&new_state->dst) != drm_rect_height(&old_state->dst) ||
  368. fb->format != old_fb->format))
  369. crtc_state->mode_changed = true;
  370. eba = drm_plane_state_to_eba(new_state, 0);
  371. if (eba & 0x7)
  372. return -EINVAL;
  373. if (fb->pitches[0] < 1 || fb->pitches[0] > 16384)
  374. return -EINVAL;
  375. if (old_fb && fb->pitches[0] != old_fb->pitches[0])
  376. crtc_state->mode_changed = true;
  377. if (ALIGN(fb->width, 8) * fb->format->cpp[0] >
  378. fb->pitches[0] + fb->offsets[0]) {
  379. dev_warn(dev, "pitch is not big enough for 8 pixels alignment");
  380. return -EINVAL;
  381. }
  382. switch (fb->format->format) {
  383. case DRM_FORMAT_YUV420:
  384. case DRM_FORMAT_YVU420:
  385. case DRM_FORMAT_YUV422:
  386. case DRM_FORMAT_YVU422:
  387. case DRM_FORMAT_YUV444:
  388. case DRM_FORMAT_YVU444:
  389. /*
  390. * Multiplanar formats have to meet the following restrictions:
  391. * - The (up to) three plane addresses are EBA, EBA+UBO, EBA+VBO
  392. * - EBA, UBO and VBO are a multiple of 8
  393. * - UBO and VBO are unsigned and not larger than 0xfffff8
  394. * - Only EBA may be changed while scanout is active
  395. * - The strides of U and V planes must be identical.
  396. */
  397. vbo = drm_plane_state_to_vbo(new_state);
  398. if (vbo & 0x7 || vbo > 0xfffff8)
  399. return -EINVAL;
  400. if (old_fb && (fb->format == old_fb->format)) {
  401. old_vbo = drm_plane_state_to_vbo(old_state);
  402. if (vbo != old_vbo)
  403. crtc_state->mode_changed = true;
  404. }
  405. if (fb->pitches[1] != fb->pitches[2])
  406. return -EINVAL;
  407. fallthrough;
  408. case DRM_FORMAT_NV12:
  409. case DRM_FORMAT_NV16:
  410. ubo = drm_plane_state_to_ubo(new_state);
  411. if (ubo & 0x7 || ubo > 0xfffff8)
  412. return -EINVAL;
  413. if (old_fb && (fb->format == old_fb->format)) {
  414. old_ubo = drm_plane_state_to_ubo(old_state);
  415. if (ubo != old_ubo)
  416. crtc_state->mode_changed = true;
  417. }
  418. if (fb->pitches[1] < 1 || fb->pitches[1] > 16384)
  419. return -EINVAL;
  420. if (old_fb && old_fb->pitches[1] != fb->pitches[1])
  421. crtc_state->mode_changed = true;
  422. /*
  423. * The x/y offsets must be even in case of horizontal/vertical
  424. * chroma subsampling.
  425. */
  426. if (((new_state->src.x1 >> 16) & (fb->format->hsub - 1)) ||
  427. ((new_state->src.y1 >> 16) & (fb->format->vsub - 1)))
  428. return -EINVAL;
  429. break;
  430. case DRM_FORMAT_RGB565_A8:
  431. case DRM_FORMAT_BGR565_A8:
  432. case DRM_FORMAT_RGB888_A8:
  433. case DRM_FORMAT_BGR888_A8:
  434. case DRM_FORMAT_RGBX8888_A8:
  435. case DRM_FORMAT_BGRX8888_A8:
  436. alpha_eba = drm_plane_state_to_eba(new_state, 1);
  437. if (alpha_eba & 0x7)
  438. return -EINVAL;
  439. if (fb->pitches[1] < 1 || fb->pitches[1] > 16384)
  440. return -EINVAL;
  441. if (old_fb && old_fb->pitches[1] != fb->pitches[1])
  442. crtc_state->mode_changed = true;
  443. break;
  444. }
  445. return 0;
  446. }
  447. static void ipu_plane_atomic_disable(struct drm_plane *plane,
  448. struct drm_atomic_state *state)
  449. {
  450. struct ipu_plane *ipu_plane = to_ipu_plane(plane);
  451. if (ipu_plane->dp)
  452. ipu_dp_disable_channel(ipu_plane->dp, true);
  453. ipu_plane->disabling = true;
  454. }
  455. static int ipu_chan_assign_axi_id(int ipu_chan)
  456. {
  457. switch (ipu_chan) {
  458. case IPUV3_CHANNEL_MEM_BG_SYNC:
  459. return 1;
  460. case IPUV3_CHANNEL_MEM_FG_SYNC:
  461. return 2;
  462. case IPUV3_CHANNEL_MEM_DC_SYNC:
  463. return 3;
  464. default:
  465. return 0;
  466. }
  467. }
  468. static void ipu_calculate_bursts(u32 width, u32 cpp, u32 stride,
  469. u8 *burstsize, u8 *num_bursts)
  470. {
  471. const unsigned int width_bytes = width * cpp;
  472. unsigned int npb, bursts;
  473. /* Maximum number of pixels per burst without overshooting stride */
  474. for (npb = 64 / cpp; npb > 0; --npb) {
  475. if (round_up(width_bytes, npb * cpp) <= stride)
  476. break;
  477. }
  478. *burstsize = npb;
  479. /* Maximum number of consecutive bursts without overshooting stride */
  480. for (bursts = 8; bursts > 1; bursts /= 2) {
  481. if (round_up(width_bytes, npb * cpp * bursts) <= stride)
  482. break;
  483. }
  484. *num_bursts = bursts;
  485. }
  486. static void ipu_plane_atomic_update(struct drm_plane *plane,
  487. struct drm_atomic_state *state)
  488. {
  489. struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
  490. plane);
  491. struct ipu_plane *ipu_plane = to_ipu_plane(plane);
  492. struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
  493. plane);
  494. struct ipu_plane_state *ipu_state = to_ipu_plane_state(new_state);
  495. struct drm_crtc_state *crtc_state = new_state->crtc->state;
  496. struct drm_framebuffer *fb = new_state->fb;
  497. struct drm_rect *dst = &new_state->dst;
  498. unsigned long eba, ubo, vbo;
  499. unsigned long alpha_eba = 0;
  500. enum ipu_color_space ics;
  501. unsigned int axi_id = 0;
  502. const struct drm_format_info *info;
  503. u8 burstsize, num_bursts;
  504. u32 width, height;
  505. int active;
  506. if (ipu_plane->dp_flow == IPU_DP_FLOW_SYNC_FG)
  507. ipu_dp_set_window_pos(ipu_plane->dp, dst->x1, dst->y1);
  508. switch (ipu_plane->dp_flow) {
  509. case IPU_DP_FLOW_SYNC_BG:
  510. if (new_state->normalized_zpos == 1) {
  511. ipu_dp_set_global_alpha(ipu_plane->dp,
  512. !fb->format->has_alpha, 0xff,
  513. true);
  514. } else {
  515. ipu_dp_set_global_alpha(ipu_plane->dp, true, 0, true);
  516. }
  517. break;
  518. case IPU_DP_FLOW_SYNC_FG:
  519. if (new_state->normalized_zpos == 1) {
  520. ipu_dp_set_global_alpha(ipu_plane->dp,
  521. !fb->format->has_alpha, 0xff,
  522. false);
  523. }
  524. break;
  525. }
  526. if (ipu_plane->dp_flow == IPU_DP_FLOW_SYNC_BG)
  527. width = ipu_src_rect_width(new_state);
  528. else
  529. width = drm_rect_width(&new_state->src) >> 16;
  530. height = drm_rect_height(&new_state->src) >> 16;
  531. eba = drm_plane_state_to_eba(new_state, 0);
  532. /*
  533. * Configure PRG channel and attached PRE, this changes the EBA to an
  534. * internal SRAM location.
  535. */
  536. if (ipu_state->use_pre) {
  537. axi_id = ipu_chan_assign_axi_id(ipu_plane->dma);
  538. ipu_prg_channel_configure(ipu_plane->ipu_ch, axi_id, width,
  539. height, fb->pitches[0],
  540. fb->format->format, fb->modifier,
  541. &eba);
  542. }
  543. if (!old_state->fb ||
  544. old_state->fb->format->format != fb->format->format ||
  545. old_state->color_encoding != new_state->color_encoding ||
  546. old_state->color_range != new_state->color_range) {
  547. ics = ipu_drm_fourcc_to_colorspace(fb->format->format);
  548. switch (ipu_plane->dp_flow) {
  549. case IPU_DP_FLOW_SYNC_BG:
  550. ipu_dp_setup_channel(ipu_plane->dp, new_state->color_encoding,
  551. new_state->color_range, ics,
  552. IPUV3_COLORSPACE_RGB);
  553. break;
  554. case IPU_DP_FLOW_SYNC_FG:
  555. ipu_dp_setup_channel(ipu_plane->dp, new_state->color_encoding,
  556. new_state->color_range, ics,
  557. IPUV3_COLORSPACE_UNKNOWN);
  558. break;
  559. }
  560. }
  561. if (old_state->fb && !drm_atomic_crtc_needs_modeset(crtc_state)) {
  562. /* nothing to do if PRE is used */
  563. if (ipu_state->use_pre)
  564. return;
  565. active = ipu_idmac_get_current_buffer(ipu_plane->ipu_ch);
  566. ipu_cpmem_set_buffer(ipu_plane->ipu_ch, !active, eba);
  567. ipu_idmac_select_buffer(ipu_plane->ipu_ch, !active);
  568. if (ipu_plane_separate_alpha(ipu_plane)) {
  569. active = ipu_idmac_get_current_buffer(ipu_plane->alpha_ch);
  570. ipu_cpmem_set_buffer(ipu_plane->alpha_ch, !active,
  571. alpha_eba);
  572. ipu_idmac_select_buffer(ipu_plane->alpha_ch, !active);
  573. }
  574. return;
  575. }
  576. ics = ipu_drm_fourcc_to_colorspace(fb->format->format);
  577. switch (ipu_plane->dp_flow) {
  578. case IPU_DP_FLOW_SYNC_BG:
  579. ipu_dp_setup_channel(ipu_plane->dp, DRM_COLOR_YCBCR_BT601,
  580. DRM_COLOR_YCBCR_LIMITED_RANGE, ics,
  581. IPUV3_COLORSPACE_RGB);
  582. break;
  583. case IPU_DP_FLOW_SYNC_FG:
  584. ipu_dp_setup_channel(ipu_plane->dp, DRM_COLOR_YCBCR_BT601,
  585. DRM_COLOR_YCBCR_LIMITED_RANGE, ics,
  586. IPUV3_COLORSPACE_UNKNOWN);
  587. break;
  588. }
  589. ipu_dmfc_config_wait4eot(ipu_plane->dmfc, width);
  590. info = drm_format_info(fb->format->format);
  591. ipu_calculate_bursts(width, info->cpp[0], fb->pitches[0],
  592. &burstsize, &num_bursts);
  593. ipu_cpmem_zero(ipu_plane->ipu_ch);
  594. ipu_cpmem_set_resolution(ipu_plane->ipu_ch, width, height);
  595. ipu_cpmem_set_fmt(ipu_plane->ipu_ch, fb->format->format);
  596. ipu_cpmem_set_burstsize(ipu_plane->ipu_ch, burstsize);
  597. ipu_cpmem_set_high_priority(ipu_plane->ipu_ch);
  598. ipu_idmac_enable_watermark(ipu_plane->ipu_ch, true);
  599. ipu_idmac_set_double_buffer(ipu_plane->ipu_ch, 1);
  600. ipu_cpmem_set_stride(ipu_plane->ipu_ch, fb->pitches[0]);
  601. ipu_cpmem_set_axi_id(ipu_plane->ipu_ch, axi_id);
  602. switch (fb->format->format) {
  603. case DRM_FORMAT_YUV420:
  604. case DRM_FORMAT_YVU420:
  605. case DRM_FORMAT_YUV422:
  606. case DRM_FORMAT_YVU422:
  607. case DRM_FORMAT_YUV444:
  608. case DRM_FORMAT_YVU444:
  609. ubo = drm_plane_state_to_ubo(new_state);
  610. vbo = drm_plane_state_to_vbo(new_state);
  611. if (fb->format->format == DRM_FORMAT_YVU420 ||
  612. fb->format->format == DRM_FORMAT_YVU422 ||
  613. fb->format->format == DRM_FORMAT_YVU444)
  614. swap(ubo, vbo);
  615. ipu_cpmem_set_yuv_planar_full(ipu_plane->ipu_ch,
  616. fb->pitches[1], ubo, vbo);
  617. dev_dbg(ipu_plane->base.dev->dev,
  618. "phy = %lu %lu %lu, x = %d, y = %d", eba, ubo, vbo,
  619. new_state->src.x1 >> 16, new_state->src.y1 >> 16);
  620. break;
  621. case DRM_FORMAT_NV12:
  622. case DRM_FORMAT_NV16:
  623. ubo = drm_plane_state_to_ubo(new_state);
  624. ipu_cpmem_set_yuv_planar_full(ipu_plane->ipu_ch,
  625. fb->pitches[1], ubo, ubo);
  626. dev_dbg(ipu_plane->base.dev->dev,
  627. "phy = %lu %lu, x = %d, y = %d", eba, ubo,
  628. new_state->src.x1 >> 16, new_state->src.y1 >> 16);
  629. break;
  630. case DRM_FORMAT_RGB565_A8:
  631. case DRM_FORMAT_BGR565_A8:
  632. case DRM_FORMAT_RGB888_A8:
  633. case DRM_FORMAT_BGR888_A8:
  634. case DRM_FORMAT_RGBX8888_A8:
  635. case DRM_FORMAT_BGRX8888_A8:
  636. alpha_eba = drm_plane_state_to_eba(new_state, 1);
  637. num_bursts = 0;
  638. dev_dbg(ipu_plane->base.dev->dev, "phys = %lu %lu, x = %d, y = %d",
  639. eba, alpha_eba, new_state->src.x1 >> 16,
  640. new_state->src.y1 >> 16);
  641. ipu_cpmem_set_burstsize(ipu_plane->ipu_ch, 16);
  642. ipu_cpmem_zero(ipu_plane->alpha_ch);
  643. ipu_cpmem_set_resolution(ipu_plane->alpha_ch, width, height);
  644. ipu_cpmem_set_format_passthrough(ipu_plane->alpha_ch, 8);
  645. ipu_cpmem_set_high_priority(ipu_plane->alpha_ch);
  646. ipu_idmac_set_double_buffer(ipu_plane->alpha_ch, 1);
  647. ipu_cpmem_set_stride(ipu_plane->alpha_ch, fb->pitches[1]);
  648. ipu_cpmem_set_burstsize(ipu_plane->alpha_ch, 16);
  649. ipu_cpmem_set_buffer(ipu_plane->alpha_ch, 0, alpha_eba);
  650. ipu_cpmem_set_buffer(ipu_plane->alpha_ch, 1, alpha_eba);
  651. break;
  652. default:
  653. dev_dbg(ipu_plane->base.dev->dev, "phys = %lu, x = %d, y = %d",
  654. eba, new_state->src.x1 >> 16, new_state->src.y1 >> 16);
  655. break;
  656. }
  657. ipu_cpmem_set_buffer(ipu_plane->ipu_ch, 0, eba);
  658. ipu_cpmem_set_buffer(ipu_plane->ipu_ch, 1, eba);
  659. ipu_idmac_lock_enable(ipu_plane->ipu_ch, num_bursts);
  660. ipu_plane_enable(ipu_plane);
  661. }
  662. static const struct drm_plane_helper_funcs ipu_plane_helper_funcs = {
  663. .atomic_check = ipu_plane_atomic_check,
  664. .atomic_disable = ipu_plane_atomic_disable,
  665. .atomic_update = ipu_plane_atomic_update,
  666. };
  667. static const struct drm_plane_helper_funcs ipu_primary_plane_helper_funcs = {
  668. .atomic_check = ipu_plane_atomic_check,
  669. .atomic_disable = ipu_plane_atomic_disable,
  670. .atomic_update = ipu_plane_atomic_update,
  671. .get_scanout_buffer = drm_fb_dma_get_scanout_buffer,
  672. };
  673. bool ipu_plane_atomic_update_pending(struct drm_plane *plane)
  674. {
  675. struct ipu_plane *ipu_plane = to_ipu_plane(plane);
  676. struct drm_plane_state *state = plane->state;
  677. struct ipu_plane_state *ipu_state = to_ipu_plane_state(state);
  678. /* disabled crtcs must not block the update */
  679. if (!state->crtc)
  680. return false;
  681. if (ipu_state->use_pre)
  682. return ipu_prg_channel_configure_pending(ipu_plane->ipu_ch);
  683. /*
  684. * Pretend no update is pending in the non-PRE/PRG case. For this to
  685. * happen, an atomic update would have to be deferred until after the
  686. * start of the next frame and simultaneously interrupt latency would
  687. * have to be high enough to let the atomic update finish and issue an
  688. * event before the previous end of frame interrupt handler can be
  689. * executed.
  690. */
  691. return false;
  692. }
  693. int ipu_planes_assign_pre(struct drm_device *dev,
  694. struct drm_atomic_state *state)
  695. {
  696. struct drm_crtc_state *old_crtc_state, *crtc_state;
  697. struct drm_plane_state *plane_state;
  698. struct ipu_plane_state *ipu_state;
  699. struct ipu_plane *ipu_plane;
  700. struct drm_plane *plane;
  701. struct drm_crtc *crtc;
  702. int available_pres = ipu_prg_max_active_channels();
  703. int ret, i;
  704. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
  705. ret = drm_atomic_add_affected_planes(state, crtc);
  706. if (ret)
  707. return ret;
  708. }
  709. /*
  710. * We are going over the planes in 2 passes: first we assign PREs to
  711. * planes with a tiling modifier, which need the PREs to resolve into
  712. * linear. Any failure to assign a PRE there is fatal. In the second
  713. * pass we try to assign PREs to linear FBs, to improve memory access
  714. * patterns for them. Failure at this point is non-fatal, as we can
  715. * scan out linear FBs without a PRE.
  716. */
  717. for_each_new_plane_in_state(state, plane, plane_state, i) {
  718. ipu_state = to_ipu_plane_state(plane_state);
  719. ipu_plane = to_ipu_plane(plane);
  720. if (!plane_state->fb) {
  721. ipu_state->use_pre = false;
  722. continue;
  723. }
  724. if (!(plane_state->fb->flags & DRM_MODE_FB_MODIFIERS) ||
  725. plane_state->fb->modifier == DRM_FORMAT_MOD_LINEAR)
  726. continue;
  727. if (!ipu_prg_present(ipu_plane->ipu) || !available_pres)
  728. return -EINVAL;
  729. if (!ipu_prg_format_supported(ipu_plane->ipu,
  730. plane_state->fb->format->format,
  731. plane_state->fb->modifier))
  732. return -EINVAL;
  733. ipu_state->use_pre = true;
  734. available_pres--;
  735. }
  736. for_each_new_plane_in_state(state, plane, plane_state, i) {
  737. ipu_state = to_ipu_plane_state(plane_state);
  738. ipu_plane = to_ipu_plane(plane);
  739. if (!plane_state->fb) {
  740. ipu_state->use_pre = false;
  741. continue;
  742. }
  743. if ((plane_state->fb->flags & DRM_MODE_FB_MODIFIERS) &&
  744. plane_state->fb->modifier != DRM_FORMAT_MOD_LINEAR)
  745. continue;
  746. /* make sure that modifier is initialized */
  747. plane_state->fb->modifier = DRM_FORMAT_MOD_LINEAR;
  748. if (ipu_prg_present(ipu_plane->ipu) && available_pres &&
  749. ipu_prg_format_supported(ipu_plane->ipu,
  750. plane_state->fb->format->format,
  751. plane_state->fb->modifier)) {
  752. ipu_state->use_pre = true;
  753. available_pres--;
  754. } else {
  755. ipu_state->use_pre = false;
  756. }
  757. }
  758. return 0;
  759. }
  760. struct ipu_plane *ipu_plane_init(struct drm_device *dev, struct ipu_soc *ipu,
  761. int dma, int dp, unsigned int possible_crtcs,
  762. enum drm_plane_type type)
  763. {
  764. struct ipu_plane *ipu_plane;
  765. const uint64_t *modifiers = ipu_format_modifiers;
  766. unsigned int zpos = (type == DRM_PLANE_TYPE_PRIMARY) ? 0 : 1;
  767. unsigned int format_count;
  768. const uint32_t *formats;
  769. int ret;
  770. DRM_DEBUG_KMS("channel %d, dp flow %d, possible_crtcs=0x%x\n",
  771. dma, dp, possible_crtcs);
  772. if (dp == IPU_DP_FLOW_SYNC_BG || dp == IPU_DP_FLOW_SYNC_FG) {
  773. formats = ipu_plane_all_formats;
  774. format_count = ARRAY_SIZE(ipu_plane_all_formats);
  775. } else {
  776. formats = ipu_plane_rgb_formats;
  777. format_count = ARRAY_SIZE(ipu_plane_rgb_formats);
  778. }
  779. if (ipu_prg_present(ipu))
  780. modifiers = pre_format_modifiers;
  781. ipu_plane = drmm_universal_plane_alloc(dev, struct ipu_plane, base,
  782. possible_crtcs, &ipu_plane_funcs,
  783. formats, format_count, modifiers,
  784. type, NULL);
  785. if (IS_ERR(ipu_plane)) {
  786. DRM_ERROR("failed to allocate and initialize %s plane\n",
  787. zpos ? "overlay" : "primary");
  788. return ipu_plane;
  789. }
  790. ipu_plane->ipu = ipu;
  791. ipu_plane->dma = dma;
  792. ipu_plane->dp_flow = dp;
  793. if (type == DRM_PLANE_TYPE_PRIMARY)
  794. drm_plane_helper_add(&ipu_plane->base, &ipu_primary_plane_helper_funcs);
  795. else
  796. drm_plane_helper_add(&ipu_plane->base, &ipu_plane_helper_funcs);
  797. if (dp == IPU_DP_FLOW_SYNC_BG || dp == IPU_DP_FLOW_SYNC_FG)
  798. ret = drm_plane_create_zpos_property(&ipu_plane->base, zpos, 0,
  799. 1);
  800. else
  801. ret = drm_plane_create_zpos_immutable_property(&ipu_plane->base,
  802. 0);
  803. if (ret)
  804. return ERR_PTR(ret);
  805. ret = drm_plane_create_color_properties(&ipu_plane->base,
  806. BIT(DRM_COLOR_YCBCR_BT601) |
  807. BIT(DRM_COLOR_YCBCR_BT709),
  808. BIT(DRM_COLOR_YCBCR_LIMITED_RANGE),
  809. DRM_COLOR_YCBCR_BT601,
  810. DRM_COLOR_YCBCR_LIMITED_RANGE);
  811. if (ret)
  812. return ERR_PTR(ret);
  813. ret = ipu_plane_get_resources(dev, ipu_plane);
  814. if (ret) {
  815. DRM_ERROR("failed to get %s plane resources: %pe\n",
  816. zpos ? "overlay" : "primary", &ret);
  817. return ERR_PTR(ret);
  818. }
  819. return ipu_plane;
  820. }