ipuv3-plane.c 14 KB

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