ipuv3-plane.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * i.MX IPUv3 DP Overlay Planes
  3. *
  4. * Copyright (C) 2013 Philipp Zabel, Pengutronix
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <drm/drmP.h>
  16. #include <drm/drm_atomic.h>
  17. #include <drm/drm_atomic_helper.h>
  18. #include <drm/drm_fb_cma_helper.h>
  19. #include <drm/drm_gem_cma_helper.h>
  20. #include <drm/drm_plane_helper.h>
  21. #include "video/imx-ipu-v3.h"
  22. #include "ipuv3-plane.h"
  23. static inline struct ipu_plane *to_ipu_plane(struct drm_plane *p)
  24. {
  25. return container_of(p, struct ipu_plane, base);
  26. }
  27. static const uint32_t ipu_plane_formats[] = {
  28. DRM_FORMAT_ARGB1555,
  29. DRM_FORMAT_XRGB1555,
  30. DRM_FORMAT_ABGR1555,
  31. DRM_FORMAT_XBGR1555,
  32. DRM_FORMAT_RGBA5551,
  33. DRM_FORMAT_BGRA5551,
  34. DRM_FORMAT_ARGB4444,
  35. DRM_FORMAT_ARGB8888,
  36. DRM_FORMAT_XRGB8888,
  37. DRM_FORMAT_ABGR8888,
  38. DRM_FORMAT_XBGR8888,
  39. DRM_FORMAT_RGBA8888,
  40. DRM_FORMAT_RGBX8888,
  41. DRM_FORMAT_BGRA8888,
  42. DRM_FORMAT_BGRA8888,
  43. DRM_FORMAT_UYVY,
  44. DRM_FORMAT_VYUY,
  45. DRM_FORMAT_YUYV,
  46. DRM_FORMAT_YVYU,
  47. DRM_FORMAT_YUV420,
  48. DRM_FORMAT_YVU420,
  49. DRM_FORMAT_YUV422,
  50. DRM_FORMAT_YVU422,
  51. DRM_FORMAT_YUV444,
  52. DRM_FORMAT_YVU444,
  53. DRM_FORMAT_NV12,
  54. DRM_FORMAT_NV16,
  55. DRM_FORMAT_RGB565,
  56. };
  57. int ipu_plane_irq(struct ipu_plane *ipu_plane)
  58. {
  59. return ipu_idmac_channel_irq(ipu_plane->ipu, ipu_plane->ipu_ch,
  60. IPU_IRQ_EOF);
  61. }
  62. static inline unsigned long
  63. drm_plane_state_to_eba(struct drm_plane_state *state)
  64. {
  65. struct drm_framebuffer *fb = state->fb;
  66. struct drm_gem_cma_object *cma_obj;
  67. int x = state->src_x >> 16;
  68. int y = state->src_y >> 16;
  69. cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
  70. BUG_ON(!cma_obj);
  71. return cma_obj->paddr + fb->offsets[0] + fb->pitches[0] * y +
  72. fb->format->cpp[0] * x;
  73. }
  74. static inline unsigned long
  75. drm_plane_state_to_ubo(struct drm_plane_state *state)
  76. {
  77. struct drm_framebuffer *fb = state->fb;
  78. struct drm_gem_cma_object *cma_obj;
  79. unsigned long eba = drm_plane_state_to_eba(state);
  80. int x = state->src_x >> 16;
  81. int y = state->src_y >> 16;
  82. cma_obj = drm_fb_cma_get_gem_obj(fb, 1);
  83. BUG_ON(!cma_obj);
  84. x /= drm_format_horz_chroma_subsampling(fb->format->format);
  85. y /= drm_format_vert_chroma_subsampling(fb->format->format);
  86. return cma_obj->paddr + fb->offsets[1] + fb->pitches[1] * y +
  87. fb->format->cpp[1] * x - eba;
  88. }
  89. static inline unsigned long
  90. drm_plane_state_to_vbo(struct drm_plane_state *state)
  91. {
  92. struct drm_framebuffer *fb = state->fb;
  93. struct drm_gem_cma_object *cma_obj;
  94. unsigned long eba = drm_plane_state_to_eba(state);
  95. int x = state->src_x >> 16;
  96. int y = state->src_y >> 16;
  97. cma_obj = drm_fb_cma_get_gem_obj(fb, 2);
  98. BUG_ON(!cma_obj);
  99. x /= drm_format_horz_chroma_subsampling(fb->format->format);
  100. y /= drm_format_vert_chroma_subsampling(fb->format->format);
  101. return cma_obj->paddr + fb->offsets[2] + fb->pitches[2] * y +
  102. fb->format->cpp[2] * x - eba;
  103. }
  104. void ipu_plane_put_resources(struct ipu_plane *ipu_plane)
  105. {
  106. if (!IS_ERR_OR_NULL(ipu_plane->dp))
  107. ipu_dp_put(ipu_plane->dp);
  108. if (!IS_ERR_OR_NULL(ipu_plane->dmfc))
  109. ipu_dmfc_put(ipu_plane->dmfc);
  110. if (!IS_ERR_OR_NULL(ipu_plane->ipu_ch))
  111. ipu_idmac_put(ipu_plane->ipu_ch);
  112. }
  113. int ipu_plane_get_resources(struct ipu_plane *ipu_plane)
  114. {
  115. int ret;
  116. ipu_plane->ipu_ch = ipu_idmac_get(ipu_plane->ipu, ipu_plane->dma);
  117. if (IS_ERR(ipu_plane->ipu_ch)) {
  118. ret = PTR_ERR(ipu_plane->ipu_ch);
  119. DRM_ERROR("failed to get idmac channel: %d\n", ret);
  120. return ret;
  121. }
  122. ipu_plane->dmfc = ipu_dmfc_get(ipu_plane->ipu, ipu_plane->dma);
  123. if (IS_ERR(ipu_plane->dmfc)) {
  124. ret = PTR_ERR(ipu_plane->dmfc);
  125. DRM_ERROR("failed to get dmfc: ret %d\n", ret);
  126. goto err_out;
  127. }
  128. if (ipu_plane->dp_flow >= 0) {
  129. ipu_plane->dp = ipu_dp_get(ipu_plane->ipu, ipu_plane->dp_flow);
  130. if (IS_ERR(ipu_plane->dp)) {
  131. ret = PTR_ERR(ipu_plane->dp);
  132. DRM_ERROR("failed to get dp flow: %d\n", ret);
  133. goto err_out;
  134. }
  135. }
  136. return 0;
  137. err_out:
  138. ipu_plane_put_resources(ipu_plane);
  139. return ret;
  140. }
  141. static void ipu_plane_enable(struct ipu_plane *ipu_plane)
  142. {
  143. if (ipu_plane->dp)
  144. ipu_dp_enable(ipu_plane->ipu);
  145. ipu_dmfc_enable_channel(ipu_plane->dmfc);
  146. ipu_idmac_enable_channel(ipu_plane->ipu_ch);
  147. if (ipu_plane->dp)
  148. ipu_dp_enable_channel(ipu_plane->dp);
  149. }
  150. static int ipu_disable_plane(struct drm_plane *plane)
  151. {
  152. struct ipu_plane *ipu_plane = to_ipu_plane(plane);
  153. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  154. ipu_idmac_wait_busy(ipu_plane->ipu_ch, 50);
  155. if (ipu_plane->dp)
  156. ipu_dp_disable_channel(ipu_plane->dp);
  157. ipu_idmac_disable_channel(ipu_plane->ipu_ch);
  158. ipu_dmfc_disable_channel(ipu_plane->dmfc);
  159. if (ipu_plane->dp)
  160. ipu_dp_disable(ipu_plane->ipu);
  161. return 0;
  162. }
  163. static void ipu_plane_destroy(struct drm_plane *plane)
  164. {
  165. struct ipu_plane *ipu_plane = to_ipu_plane(plane);
  166. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  167. drm_plane_cleanup(plane);
  168. kfree(ipu_plane);
  169. }
  170. static const struct drm_plane_funcs ipu_plane_funcs = {
  171. .update_plane = drm_atomic_helper_update_plane,
  172. .disable_plane = drm_atomic_helper_disable_plane,
  173. .destroy = ipu_plane_destroy,
  174. .reset = drm_atomic_helper_plane_reset,
  175. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  176. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  177. };
  178. static int ipu_plane_atomic_check(struct drm_plane *plane,
  179. struct drm_plane_state *state)
  180. {
  181. struct drm_plane_state *old_state = plane->state;
  182. struct drm_crtc_state *crtc_state;
  183. struct device *dev = plane->dev->dev;
  184. struct drm_framebuffer *fb = state->fb;
  185. struct drm_framebuffer *old_fb = old_state->fb;
  186. unsigned long eba, ubo, vbo, old_ubo, old_vbo;
  187. int hsub, vsub;
  188. /* Ok to disable */
  189. if (!fb)
  190. return 0;
  191. if (!state->crtc)
  192. return -EINVAL;
  193. crtc_state =
  194. drm_atomic_get_existing_crtc_state(state->state, state->crtc);
  195. if (WARN_ON(!crtc_state))
  196. return -EINVAL;
  197. /* CRTC should be enabled */
  198. if (!crtc_state->enable)
  199. return -EINVAL;
  200. /* no scaling */
  201. if (state->src_w >> 16 != state->crtc_w ||
  202. state->src_h >> 16 != state->crtc_h)
  203. return -EINVAL;
  204. switch (plane->type) {
  205. case DRM_PLANE_TYPE_PRIMARY:
  206. /* full plane doesn't support partial off screen */
  207. if (state->crtc_x || state->crtc_y ||
  208. state->crtc_w != crtc_state->adjusted_mode.hdisplay ||
  209. state->crtc_h != crtc_state->adjusted_mode.vdisplay)
  210. return -EINVAL;
  211. /* full plane minimum width is 13 pixels */
  212. if (state->crtc_w < 13)
  213. return -EINVAL;
  214. break;
  215. case DRM_PLANE_TYPE_OVERLAY:
  216. if (state->crtc_x < 0 || state->crtc_y < 0)
  217. return -EINVAL;
  218. if (state->crtc_x + state->crtc_w >
  219. crtc_state->adjusted_mode.hdisplay)
  220. return -EINVAL;
  221. if (state->crtc_y + state->crtc_h >
  222. crtc_state->adjusted_mode.vdisplay)
  223. return -EINVAL;
  224. break;
  225. default:
  226. dev_warn(dev, "Unsupported plane type\n");
  227. return -EINVAL;
  228. }
  229. if (state->crtc_h < 2)
  230. return -EINVAL;
  231. /*
  232. * We support resizing active plane or changing its format by
  233. * forcing CRTC mode change in plane's ->atomic_check callback
  234. * and disabling all affected active planes in CRTC's ->atomic_disable
  235. * callback. The planes will be reenabled in plane's ->atomic_update
  236. * callback.
  237. */
  238. if (old_fb && (state->src_w != old_state->src_w ||
  239. state->src_h != old_state->src_h ||
  240. fb->format != old_fb->format))
  241. crtc_state->mode_changed = true;
  242. eba = drm_plane_state_to_eba(state);
  243. if (eba & 0x7)
  244. return -EINVAL;
  245. if (fb->pitches[0] < 1 || fb->pitches[0] > 16384)
  246. return -EINVAL;
  247. if (old_fb && fb->pitches[0] != old_fb->pitches[0])
  248. crtc_state->mode_changed = true;
  249. switch (fb->format->format) {
  250. case DRM_FORMAT_YUV420:
  251. case DRM_FORMAT_YVU420:
  252. case DRM_FORMAT_YUV422:
  253. case DRM_FORMAT_YVU422:
  254. case DRM_FORMAT_YUV444:
  255. case DRM_FORMAT_YVU444:
  256. /*
  257. * Multiplanar formats have to meet the following restrictions:
  258. * - The (up to) three plane addresses are EBA, EBA+UBO, EBA+VBO
  259. * - EBA, UBO and VBO are a multiple of 8
  260. * - UBO and VBO are unsigned and not larger than 0xfffff8
  261. * - Only EBA may be changed while scanout is active
  262. * - The strides of U and V planes must be identical.
  263. */
  264. vbo = drm_plane_state_to_vbo(state);
  265. if (vbo & 0x7 || vbo > 0xfffff8)
  266. return -EINVAL;
  267. if (old_fb && (fb->format == old_fb->format)) {
  268. old_vbo = drm_plane_state_to_vbo(old_state);
  269. if (vbo != old_vbo)
  270. crtc_state->mode_changed = true;
  271. }
  272. if (fb->pitches[1] != fb->pitches[2])
  273. return -EINVAL;
  274. /* fall-through */
  275. case DRM_FORMAT_NV12:
  276. case DRM_FORMAT_NV16:
  277. ubo = drm_plane_state_to_ubo(state);
  278. if (ubo & 0x7 || ubo > 0xfffff8)
  279. return -EINVAL;
  280. if (old_fb && (fb->format == old_fb->format)) {
  281. old_ubo = drm_plane_state_to_ubo(old_state);
  282. if (ubo != old_ubo)
  283. crtc_state->mode_changed = true;
  284. }
  285. if (fb->pitches[1] < 1 || fb->pitches[1] > 16384)
  286. return -EINVAL;
  287. if (old_fb && old_fb->pitches[1] != fb->pitches[1])
  288. crtc_state->mode_changed = true;
  289. /*
  290. * The x/y offsets must be even in case of horizontal/vertical
  291. * chroma subsampling.
  292. */
  293. hsub = drm_format_horz_chroma_subsampling(fb->format->format);
  294. vsub = drm_format_vert_chroma_subsampling(fb->format->format);
  295. if (((state->src_x >> 16) & (hsub - 1)) ||
  296. ((state->src_y >> 16) & (vsub - 1)))
  297. return -EINVAL;
  298. }
  299. return 0;
  300. }
  301. static void ipu_plane_atomic_disable(struct drm_plane *plane,
  302. struct drm_plane_state *old_state)
  303. {
  304. ipu_disable_plane(plane);
  305. }
  306. static void ipu_plane_atomic_update(struct drm_plane *plane,
  307. struct drm_plane_state *old_state)
  308. {
  309. struct ipu_plane *ipu_plane = to_ipu_plane(plane);
  310. struct drm_plane_state *state = plane->state;
  311. struct drm_crtc_state *crtc_state = state->crtc->state;
  312. struct drm_framebuffer *fb = state->fb;
  313. unsigned long eba, ubo, vbo;
  314. enum ipu_color_space ics;
  315. int active;
  316. eba = drm_plane_state_to_eba(state);
  317. if (old_state->fb && !drm_atomic_crtc_needs_modeset(crtc_state)) {
  318. active = ipu_idmac_get_current_buffer(ipu_plane->ipu_ch);
  319. ipu_cpmem_set_buffer(ipu_plane->ipu_ch, !active, eba);
  320. ipu_idmac_select_buffer(ipu_plane->ipu_ch, !active);
  321. return;
  322. }
  323. switch (ipu_plane->dp_flow) {
  324. case IPU_DP_FLOW_SYNC_BG:
  325. ipu_dp_setup_channel(ipu_plane->dp,
  326. IPUV3_COLORSPACE_RGB,
  327. IPUV3_COLORSPACE_RGB);
  328. ipu_dp_set_global_alpha(ipu_plane->dp, true, 0, true);
  329. break;
  330. case IPU_DP_FLOW_SYNC_FG:
  331. ics = ipu_drm_fourcc_to_colorspace(state->fb->format->format);
  332. ipu_dp_setup_channel(ipu_plane->dp, ics,
  333. IPUV3_COLORSPACE_UNKNOWN);
  334. ipu_dp_set_window_pos(ipu_plane->dp, state->crtc_x,
  335. state->crtc_y);
  336. /* Enable local alpha on partial plane */
  337. switch (state->fb->format->format) {
  338. case DRM_FORMAT_ARGB1555:
  339. case DRM_FORMAT_ABGR1555:
  340. case DRM_FORMAT_RGBA5551:
  341. case DRM_FORMAT_BGRA5551:
  342. case DRM_FORMAT_ARGB4444:
  343. case DRM_FORMAT_ARGB8888:
  344. case DRM_FORMAT_ABGR8888:
  345. case DRM_FORMAT_RGBA8888:
  346. case DRM_FORMAT_BGRA8888:
  347. ipu_dp_set_global_alpha(ipu_plane->dp, false, 0, false);
  348. break;
  349. default:
  350. ipu_dp_set_global_alpha(ipu_plane->dp, true, 0, true);
  351. break;
  352. }
  353. }
  354. ipu_dmfc_config_wait4eot(ipu_plane->dmfc, state->crtc_w);
  355. ipu_cpmem_zero(ipu_plane->ipu_ch);
  356. ipu_cpmem_set_resolution(ipu_plane->ipu_ch, state->src_w >> 16,
  357. state->src_h >> 16);
  358. ipu_cpmem_set_fmt(ipu_plane->ipu_ch, state->fb->format->format);
  359. ipu_cpmem_set_high_priority(ipu_plane->ipu_ch);
  360. ipu_idmac_set_double_buffer(ipu_plane->ipu_ch, 1);
  361. ipu_cpmem_set_stride(ipu_plane->ipu_ch, state->fb->pitches[0]);
  362. switch (fb->format->format) {
  363. case DRM_FORMAT_YUV420:
  364. case DRM_FORMAT_YVU420:
  365. case DRM_FORMAT_YUV422:
  366. case DRM_FORMAT_YVU422:
  367. case DRM_FORMAT_YUV444:
  368. case DRM_FORMAT_YVU444:
  369. ubo = drm_plane_state_to_ubo(state);
  370. vbo = drm_plane_state_to_vbo(state);
  371. if (fb->format->format == DRM_FORMAT_YVU420 ||
  372. fb->format->format == DRM_FORMAT_YVU422 ||
  373. fb->format->format == DRM_FORMAT_YVU444)
  374. swap(ubo, vbo);
  375. ipu_cpmem_set_yuv_planar_full(ipu_plane->ipu_ch,
  376. fb->pitches[1], ubo, vbo);
  377. dev_dbg(ipu_plane->base.dev->dev,
  378. "phy = %lu %lu %lu, x = %d, y = %d", eba, ubo, vbo,
  379. state->src_x >> 16, state->src_y >> 16);
  380. break;
  381. case DRM_FORMAT_NV12:
  382. case DRM_FORMAT_NV16:
  383. ubo = drm_plane_state_to_ubo(state);
  384. ipu_cpmem_set_yuv_planar_full(ipu_plane->ipu_ch,
  385. fb->pitches[1], ubo, ubo);
  386. dev_dbg(ipu_plane->base.dev->dev,
  387. "phy = %lu %lu, x = %d, y = %d", eba, ubo,
  388. state->src_x >> 16, state->src_y >> 16);
  389. break;
  390. default:
  391. dev_dbg(ipu_plane->base.dev->dev, "phys = %lu, x = %d, y = %d",
  392. eba, state->src_x >> 16, state->src_y >> 16);
  393. break;
  394. }
  395. ipu_cpmem_set_buffer(ipu_plane->ipu_ch, 0, eba);
  396. ipu_cpmem_set_buffer(ipu_plane->ipu_ch, 1, eba);
  397. ipu_plane_enable(ipu_plane);
  398. }
  399. static const struct drm_plane_helper_funcs ipu_plane_helper_funcs = {
  400. .prepare_fb = drm_fb_cma_prepare_fb,
  401. .atomic_check = ipu_plane_atomic_check,
  402. .atomic_disable = ipu_plane_atomic_disable,
  403. .atomic_update = ipu_plane_atomic_update,
  404. };
  405. struct ipu_plane *ipu_plane_init(struct drm_device *dev, struct ipu_soc *ipu,
  406. int dma, int dp, unsigned int possible_crtcs,
  407. enum drm_plane_type type)
  408. {
  409. struct ipu_plane *ipu_plane;
  410. int ret;
  411. DRM_DEBUG_KMS("channel %d, dp flow %d, possible_crtcs=0x%x\n",
  412. dma, dp, possible_crtcs);
  413. ipu_plane = kzalloc(sizeof(*ipu_plane), GFP_KERNEL);
  414. if (!ipu_plane) {
  415. DRM_ERROR("failed to allocate plane\n");
  416. return ERR_PTR(-ENOMEM);
  417. }
  418. ipu_plane->ipu = ipu;
  419. ipu_plane->dma = dma;
  420. ipu_plane->dp_flow = dp;
  421. ret = drm_universal_plane_init(dev, &ipu_plane->base, possible_crtcs,
  422. &ipu_plane_funcs, ipu_plane_formats,
  423. ARRAY_SIZE(ipu_plane_formats), type,
  424. NULL);
  425. if (ret) {
  426. DRM_ERROR("failed to initialize plane\n");
  427. kfree(ipu_plane);
  428. return ERR_PTR(ret);
  429. }
  430. drm_plane_helper_add(&ipu_plane->base, &ipu_plane_helper_funcs);
  431. return ipu_plane;
  432. }