omap_plane.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2011 Texas Instruments Incorporated - https://www.ti.com/
  4. * Author: Rob Clark <rob.clark@linaro.org>
  5. */
  6. #include <drm/drm_atomic.h>
  7. #include <drm/drm_atomic_helper.h>
  8. #include <drm/drm_blend.h>
  9. #include <drm/drm_gem_atomic_helper.h>
  10. #include <drm/drm_fourcc.h>
  11. #include <drm/drm_framebuffer.h>
  12. #include <drm/drm_print.h>
  13. #include "omap_dmm_tiler.h"
  14. #include "omap_drv.h"
  15. /*
  16. * plane funcs
  17. */
  18. #define to_omap_plane_state(x) container_of(x, struct omap_plane_state, base)
  19. struct omap_plane_state {
  20. /* Must be first. */
  21. struct drm_plane_state base;
  22. struct omap_hw_overlay *overlay;
  23. struct omap_hw_overlay *r_overlay; /* right overlay */
  24. };
  25. #define to_omap_plane(x) container_of(x, struct omap_plane, base)
  26. struct omap_plane {
  27. struct drm_plane base;
  28. enum omap_plane_id id;
  29. };
  30. bool is_omap_plane_dual_overlay(struct drm_plane_state *state)
  31. {
  32. struct omap_plane_state *omap_state = to_omap_plane_state(state);
  33. return !!omap_state->r_overlay;
  34. }
  35. static int omap_plane_prepare_fb(struct drm_plane *plane,
  36. struct drm_plane_state *new_state)
  37. {
  38. if (!new_state->fb)
  39. return 0;
  40. drm_gem_plane_helper_prepare_fb(plane, new_state);
  41. return omap_framebuffer_pin(new_state->fb);
  42. }
  43. static void omap_plane_cleanup_fb(struct drm_plane *plane,
  44. struct drm_plane_state *old_state)
  45. {
  46. if (old_state->fb)
  47. omap_framebuffer_unpin(old_state->fb);
  48. }
  49. static void omap_plane_atomic_update(struct drm_plane *plane,
  50. struct drm_atomic_state *state)
  51. {
  52. struct omap_drm_private *priv = plane->dev->dev_private;
  53. struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
  54. plane);
  55. struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
  56. plane);
  57. struct omap_plane_state *new_omap_state;
  58. struct omap_plane_state *old_omap_state;
  59. struct omap_overlay_info info, r_info;
  60. enum omap_plane_id ovl_id, r_ovl_id;
  61. int ret;
  62. bool dual_ovl;
  63. new_omap_state = to_omap_plane_state(new_state);
  64. old_omap_state = to_omap_plane_state(old_state);
  65. dual_ovl = is_omap_plane_dual_overlay(new_state);
  66. /* Cleanup previously held overlay if needed */
  67. if (old_omap_state->overlay)
  68. omap_overlay_update_state(priv, old_omap_state->overlay);
  69. if (old_omap_state->r_overlay)
  70. omap_overlay_update_state(priv, old_omap_state->r_overlay);
  71. if (!new_omap_state->overlay) {
  72. DBG("[PLANE:%d:%s] no overlay attached", plane->base.id, plane->name);
  73. return;
  74. }
  75. ovl_id = new_omap_state->overlay->id;
  76. DBG("%s, crtc=%p fb=%p", plane->name, new_state->crtc,
  77. new_state->fb);
  78. memset(&info, 0, sizeof(info));
  79. info.rotation_type = OMAP_DSS_ROT_NONE;
  80. info.rotation = DRM_MODE_ROTATE_0;
  81. info.global_alpha = new_state->alpha >> 8;
  82. info.zorder = new_state->normalized_zpos;
  83. if (new_state->pixel_blend_mode == DRM_MODE_BLEND_PREMULTI)
  84. info.pre_mult_alpha = 1;
  85. else
  86. info.pre_mult_alpha = 0;
  87. info.color_encoding = new_state->color_encoding;
  88. info.color_range = new_state->color_range;
  89. r_info = info;
  90. /* update scanout: */
  91. omap_framebuffer_update_scanout(new_state->fb, new_state, &info,
  92. dual_ovl ? &r_info : NULL);
  93. DBG("%s: %dx%d -> %dx%d (%d)",
  94. new_omap_state->overlay->name, info.width, info.height,
  95. info.out_width, info.out_height, info.screen_width);
  96. DBG("%d,%d %pad %pad", info.pos_x, info.pos_y,
  97. &info.paddr, &info.p_uv_addr);
  98. if (dual_ovl) {
  99. r_ovl_id = new_omap_state->r_overlay->id;
  100. /*
  101. * If the current plane uses 2 hw planes the very next
  102. * zorder is used by the r_overlay so we just use the
  103. * main overlay zorder + 1
  104. */
  105. r_info.zorder = info.zorder + 1;
  106. DBG("%s: %dx%d -> %dx%d (%d)",
  107. new_omap_state->r_overlay->name,
  108. r_info.width, r_info.height,
  109. r_info.out_width, r_info.out_height, r_info.screen_width);
  110. DBG("%d,%d %pad %pad", r_info.pos_x, r_info.pos_y,
  111. &r_info.paddr, &r_info.p_uv_addr);
  112. }
  113. /* and finally, update omapdss: */
  114. ret = dispc_ovl_setup(priv->dispc, ovl_id, &info,
  115. omap_crtc_timings(new_state->crtc), false,
  116. omap_crtc_channel(new_state->crtc));
  117. if (ret) {
  118. dev_err(plane->dev->dev, "Failed to setup plane %s\n",
  119. plane->name);
  120. dispc_ovl_enable(priv->dispc, ovl_id, false);
  121. return;
  122. }
  123. dispc_ovl_enable(priv->dispc, ovl_id, true);
  124. if (dual_ovl) {
  125. ret = dispc_ovl_setup(priv->dispc, r_ovl_id, &r_info,
  126. omap_crtc_timings(new_state->crtc), false,
  127. omap_crtc_channel(new_state->crtc));
  128. if (ret) {
  129. dev_err(plane->dev->dev, "Failed to setup plane right-overlay %s\n",
  130. plane->name);
  131. dispc_ovl_enable(priv->dispc, r_ovl_id, false);
  132. dispc_ovl_enable(priv->dispc, ovl_id, false);
  133. return;
  134. }
  135. dispc_ovl_enable(priv->dispc, r_ovl_id, true);
  136. }
  137. }
  138. static void omap_plane_atomic_disable(struct drm_plane *plane,
  139. struct drm_atomic_state *state)
  140. {
  141. struct omap_drm_private *priv = plane->dev->dev_private;
  142. struct omap_plane *omap_plane = to_omap_plane(plane);
  143. struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
  144. plane);
  145. struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
  146. plane);
  147. struct omap_plane_state *new_omap_state;
  148. struct omap_plane_state *old_omap_state;
  149. new_omap_state = to_omap_plane_state(new_state);
  150. old_omap_state = to_omap_plane_state(old_state);
  151. if (!old_omap_state->overlay)
  152. return;
  153. new_state->rotation = DRM_MODE_ROTATE_0;
  154. new_state->zpos = plane->type == DRM_PLANE_TYPE_PRIMARY ? 0 : omap_plane->id;
  155. omap_overlay_update_state(priv, old_omap_state->overlay);
  156. new_omap_state->overlay = NULL;
  157. if (is_omap_plane_dual_overlay(old_state)) {
  158. omap_overlay_update_state(priv, old_omap_state->r_overlay);
  159. new_omap_state->r_overlay = NULL;
  160. }
  161. }
  162. #define FRAC_16_16(mult, div) (((mult) << 16) / (div))
  163. static int omap_plane_atomic_check(struct drm_plane *plane,
  164. struct drm_atomic_state *state)
  165. {
  166. struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
  167. plane);
  168. struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state,
  169. plane);
  170. struct omap_drm_private *priv = plane->dev->dev_private;
  171. struct omap_plane_state *omap_state = to_omap_plane_state(new_plane_state);
  172. struct omap_global_state *omap_overlay_global_state;
  173. struct drm_crtc_state *crtc_state;
  174. bool new_r_hw_overlay = false;
  175. bool new_hw_overlay = false;
  176. u32 max_width, max_height;
  177. struct drm_crtc *crtc;
  178. u16 width, height;
  179. u32 caps = 0;
  180. u32 fourcc;
  181. int ret;
  182. omap_overlay_global_state = omap_get_global_state(state);
  183. if (IS_ERR(omap_overlay_global_state))
  184. return PTR_ERR(omap_overlay_global_state);
  185. dispc_ovl_get_max_size(priv->dispc, &width, &height);
  186. max_width = width << 16;
  187. max_height = height << 16;
  188. crtc = new_plane_state->crtc ? new_plane_state->crtc : plane->state->crtc;
  189. if (!crtc)
  190. return 0;
  191. crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
  192. /* we should have a crtc state if the plane is attached to a crtc */
  193. if (WARN_ON(!crtc_state))
  194. return 0;
  195. /*
  196. * Note: these are just sanity checks to filter out totally bad scaling
  197. * factors. The real limits must be calculated case by case, and
  198. * unfortunately we currently do those checks only at the commit
  199. * phase in dispc.
  200. */
  201. ret = drm_atomic_helper_check_plane_state(new_plane_state, crtc_state,
  202. FRAC_16_16(1, 8), FRAC_16_16(8, 1),
  203. true, true);
  204. if (ret)
  205. return ret;
  206. DBG("%s: visible %d -> %d", plane->name,
  207. old_plane_state->visible, new_plane_state->visible);
  208. if (!new_plane_state->visible) {
  209. omap_overlay_release(state, omap_state->overlay);
  210. omap_overlay_release(state, omap_state->r_overlay);
  211. omap_state->overlay = NULL;
  212. omap_state->r_overlay = NULL;
  213. return 0;
  214. }
  215. if (new_plane_state->crtc_x < 0 || new_plane_state->crtc_y < 0)
  216. return -EINVAL;
  217. if (new_plane_state->crtc_x + new_plane_state->crtc_w > crtc_state->adjusted_mode.hdisplay)
  218. return -EINVAL;
  219. if (new_plane_state->crtc_y + new_plane_state->crtc_h > crtc_state->adjusted_mode.vdisplay)
  220. return -EINVAL;
  221. /* Make sure dimensions are within bounds. */
  222. if (new_plane_state->src_h > max_height || new_plane_state->crtc_h > height)
  223. return -EINVAL;
  224. if (new_plane_state->src_w > max_width || new_plane_state->crtc_w > width) {
  225. bool is_fourcc_yuv = new_plane_state->fb->format->is_yuv;
  226. if (is_fourcc_yuv && (((new_plane_state->src_w >> 16) / 2 & 1) ||
  227. new_plane_state->crtc_w / 2 & 1)) {
  228. /*
  229. * When calculating the split overlay width
  230. * and it yield an odd value we will need to adjust
  231. * the indivual width +/- 1. So make sure it fits
  232. */
  233. if (new_plane_state->src_w <= ((2 * width - 1) << 16) &&
  234. new_plane_state->crtc_w <= (2 * width - 1))
  235. new_r_hw_overlay = true;
  236. else
  237. return -EINVAL;
  238. } else {
  239. if (new_plane_state->src_w <= (2 * max_width) &&
  240. new_plane_state->crtc_w <= (2 * width))
  241. new_r_hw_overlay = true;
  242. else
  243. return -EINVAL;
  244. }
  245. }
  246. if (new_plane_state->rotation != DRM_MODE_ROTATE_0 &&
  247. !omap_framebuffer_supports_rotation(new_plane_state->fb))
  248. return -EINVAL;
  249. if ((new_plane_state->src_w >> 16) != new_plane_state->crtc_w ||
  250. (new_plane_state->src_h >> 16) != new_plane_state->crtc_h)
  251. caps |= OMAP_DSS_OVL_CAP_SCALE;
  252. fourcc = new_plane_state->fb->format->format;
  253. /*
  254. * (re)allocate hw overlay if we don't have one or
  255. * there is a caps mismatch
  256. */
  257. if (!omap_state->overlay || (caps & ~omap_state->overlay->caps)) {
  258. new_hw_overlay = true;
  259. } else {
  260. /* check supported format */
  261. if (!dispc_ovl_color_mode_supported(priv->dispc, omap_state->overlay->id,
  262. fourcc))
  263. new_hw_overlay = true;
  264. }
  265. /*
  266. * check if we need two overlays and only have 1 or
  267. * if we had 2 overlays but will only need 1
  268. */
  269. if ((new_r_hw_overlay && !omap_state->r_overlay) ||
  270. (!new_r_hw_overlay && omap_state->r_overlay))
  271. new_hw_overlay = true;
  272. if (new_hw_overlay) {
  273. struct omap_hw_overlay *old_ovl = omap_state->overlay;
  274. struct omap_hw_overlay *old_r_ovl = omap_state->r_overlay;
  275. struct omap_hw_overlay *new_ovl = NULL;
  276. struct omap_hw_overlay *new_r_ovl = NULL;
  277. omap_overlay_release(state, old_ovl);
  278. omap_overlay_release(state, old_r_ovl);
  279. ret = omap_overlay_assign(state, plane, caps, fourcc, &new_ovl,
  280. new_r_hw_overlay ? &new_r_ovl : NULL);
  281. if (ret) {
  282. DBG("%s: failed to assign hw_overlay", plane->name);
  283. omap_state->overlay = NULL;
  284. omap_state->r_overlay = NULL;
  285. return ret;
  286. }
  287. omap_state->overlay = new_ovl;
  288. if (new_r_hw_overlay)
  289. omap_state->r_overlay = new_r_ovl;
  290. else
  291. omap_state->r_overlay = NULL;
  292. }
  293. DBG("plane: %s overlay_id: %d", plane->name, omap_state->overlay->id);
  294. if (omap_state->r_overlay)
  295. DBG("plane: %s r_overlay_id: %d", plane->name, omap_state->r_overlay->id);
  296. return 0;
  297. }
  298. static const struct drm_plane_helper_funcs omap_plane_helper_funcs = {
  299. .prepare_fb = omap_plane_prepare_fb,
  300. .cleanup_fb = omap_plane_cleanup_fb,
  301. .atomic_check = omap_plane_atomic_check,
  302. .atomic_update = omap_plane_atomic_update,
  303. .atomic_disable = omap_plane_atomic_disable,
  304. };
  305. static void omap_plane_destroy(struct drm_plane *plane)
  306. {
  307. struct omap_plane *omap_plane = to_omap_plane(plane);
  308. DBG("%s", plane->name);
  309. drm_plane_cleanup(plane);
  310. kfree(omap_plane);
  311. }
  312. /* helper to install properties which are common to planes and crtcs */
  313. void omap_plane_install_properties(struct drm_plane *plane,
  314. struct drm_mode_object *obj)
  315. {
  316. struct drm_device *dev = plane->dev;
  317. struct omap_drm_private *priv = dev->dev_private;
  318. if (priv->has_dmm) {
  319. if (!plane->rotation_property)
  320. drm_plane_create_rotation_property(plane,
  321. DRM_MODE_ROTATE_0,
  322. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
  323. DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270 |
  324. DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y);
  325. /* Attach the rotation property also to the crtc object */
  326. if (plane->rotation_property && obj != &plane->base)
  327. drm_object_attach_property(obj, plane->rotation_property,
  328. DRM_MODE_ROTATE_0);
  329. }
  330. drm_object_attach_property(obj, priv->zorder_prop, 0);
  331. }
  332. static void omap_plane_reset(struct drm_plane *plane)
  333. {
  334. struct omap_plane_state *omap_state;
  335. if (plane->state)
  336. drm_atomic_helper_plane_destroy_state(plane, plane->state);
  337. omap_state = kzalloc_obj(*omap_state);
  338. if (!omap_state)
  339. return;
  340. __drm_atomic_helper_plane_reset(plane, &omap_state->base);
  341. }
  342. static struct drm_plane_state *
  343. omap_plane_atomic_duplicate_state(struct drm_plane *plane)
  344. {
  345. struct omap_plane_state *state, *current_state;
  346. if (WARN_ON(!plane->state))
  347. return NULL;
  348. current_state = to_omap_plane_state(plane->state);
  349. state = kmalloc_obj(*state);
  350. if (!state)
  351. return NULL;
  352. __drm_atomic_helper_plane_duplicate_state(plane, &state->base);
  353. state->overlay = current_state->overlay;
  354. state->r_overlay = current_state->r_overlay;
  355. return &state->base;
  356. }
  357. static void omap_plane_atomic_print_state(struct drm_printer *p,
  358. const struct drm_plane_state *state)
  359. {
  360. struct omap_plane_state *omap_state = to_omap_plane_state(state);
  361. if (omap_state->overlay)
  362. drm_printf(p, "\toverlay=%s (caps=0x%x)\n",
  363. omap_state->overlay->name,
  364. omap_state->overlay->caps);
  365. else
  366. drm_printf(p, "\toverlay=None\n");
  367. if (omap_state->r_overlay)
  368. drm_printf(p, "\tr_overlay=%s (caps=0x%x)\n",
  369. omap_state->r_overlay->name,
  370. omap_state->r_overlay->caps);
  371. else
  372. drm_printf(p, "\tr_overlay=None\n");
  373. }
  374. static int omap_plane_atomic_set_property(struct drm_plane *plane,
  375. struct drm_plane_state *state,
  376. struct drm_property *property,
  377. u64 val)
  378. {
  379. struct omap_drm_private *priv = plane->dev->dev_private;
  380. if (property == priv->zorder_prop)
  381. state->zpos = val;
  382. else
  383. return -EINVAL;
  384. return 0;
  385. }
  386. static int omap_plane_atomic_get_property(struct drm_plane *plane,
  387. const struct drm_plane_state *state,
  388. struct drm_property *property,
  389. u64 *val)
  390. {
  391. struct omap_drm_private *priv = plane->dev->dev_private;
  392. if (property == priv->zorder_prop)
  393. *val = state->zpos;
  394. else
  395. return -EINVAL;
  396. return 0;
  397. }
  398. static const struct drm_plane_funcs omap_plane_funcs = {
  399. .update_plane = drm_atomic_helper_update_plane,
  400. .disable_plane = drm_atomic_helper_disable_plane,
  401. .reset = omap_plane_reset,
  402. .destroy = omap_plane_destroy,
  403. .atomic_duplicate_state = omap_plane_atomic_duplicate_state,
  404. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  405. .atomic_set_property = omap_plane_atomic_set_property,
  406. .atomic_get_property = omap_plane_atomic_get_property,
  407. .atomic_print_state = omap_plane_atomic_print_state,
  408. };
  409. static bool omap_plane_supports_yuv(struct drm_plane *plane)
  410. {
  411. struct omap_drm_private *priv = plane->dev->dev_private;
  412. struct omap_plane *omap_plane = to_omap_plane(plane);
  413. const u32 *formats = dispc_ovl_get_color_modes(priv->dispc, omap_plane->id);
  414. u32 i;
  415. for (i = 0; formats[i]; i++)
  416. if (formats[i] == DRM_FORMAT_YUYV ||
  417. formats[i] == DRM_FORMAT_UYVY ||
  418. formats[i] == DRM_FORMAT_NV12)
  419. return true;
  420. return false;
  421. }
  422. /* initialize plane */
  423. struct drm_plane *omap_plane_init(struct drm_device *dev,
  424. int idx, enum drm_plane_type type,
  425. u32 possible_crtcs)
  426. {
  427. struct omap_drm_private *priv = dev->dev_private;
  428. unsigned int num_planes = dispc_get_num_ovls(priv->dispc);
  429. struct drm_plane *plane;
  430. struct omap_plane *omap_plane;
  431. unsigned int zpos;
  432. int ret;
  433. u32 nformats;
  434. const u32 *formats;
  435. if (WARN_ON(idx >= num_planes))
  436. return ERR_PTR(-EINVAL);
  437. omap_plane = kzalloc_obj(*omap_plane);
  438. if (!omap_plane)
  439. return ERR_PTR(-ENOMEM);
  440. omap_plane->id = idx;
  441. DBG("%d: type=%d", omap_plane->id, type);
  442. DBG(" crtc_mask: 0x%04x", possible_crtcs);
  443. formats = dispc_ovl_get_color_modes(priv->dispc, omap_plane->id);
  444. for (nformats = 0; formats[nformats]; ++nformats)
  445. ;
  446. plane = &omap_plane->base;
  447. ret = drm_universal_plane_init(dev, plane, possible_crtcs,
  448. &omap_plane_funcs, formats,
  449. nformats, NULL, type, NULL);
  450. if (ret < 0)
  451. goto error;
  452. drm_plane_helper_add(plane, &omap_plane_helper_funcs);
  453. omap_plane_install_properties(plane, &plane->base);
  454. /*
  455. * Set the zpos default depending on whether we are a primary or overlay
  456. * plane.
  457. */
  458. if (plane->type == DRM_PLANE_TYPE_PRIMARY)
  459. zpos = 0;
  460. else
  461. zpos = omap_plane->id;
  462. drm_plane_create_zpos_property(plane, zpos, 0, num_planes - 1);
  463. drm_plane_create_alpha_property(plane);
  464. drm_plane_create_blend_mode_property(plane, BIT(DRM_MODE_BLEND_PREMULTI) |
  465. BIT(DRM_MODE_BLEND_COVERAGE));
  466. if (omap_plane_supports_yuv(plane))
  467. drm_plane_create_color_properties(plane,
  468. BIT(DRM_COLOR_YCBCR_BT601) |
  469. BIT(DRM_COLOR_YCBCR_BT709),
  470. BIT(DRM_COLOR_YCBCR_FULL_RANGE) |
  471. BIT(DRM_COLOR_YCBCR_LIMITED_RANGE),
  472. DRM_COLOR_YCBCR_BT601,
  473. DRM_COLOR_YCBCR_FULL_RANGE);
  474. return plane;
  475. error:
  476. dev_err(dev->dev, "%s(): could not create plane: %d\n",
  477. __func__, omap_plane->id);
  478. kfree(omap_plane);
  479. return NULL;
  480. }