omap_plane.c 17 KB

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