omap_plane.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /*
  2. * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
  3. * Author: Rob Clark <rob.clark@linaro.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <drm/drm_atomic.h>
  18. #include <drm/drm_atomic_helper.h>
  19. #include <drm/drm_plane_helper.h>
  20. #include "omap_dmm_tiler.h"
  21. #include "omap_drv.h"
  22. /*
  23. * plane funcs
  24. */
  25. #define to_omap_plane_state(x) container_of(x, struct omap_plane_state, base)
  26. struct omap_plane_state {
  27. struct drm_plane_state base;
  28. struct omap_hw_overlay *overlay;
  29. struct omap_hw_overlay *r_overlay; /* right overlay */
  30. };
  31. #define to_omap_plane(x) container_of(x, struct omap_plane, base)
  32. struct omap_plane {
  33. struct drm_plane base;
  34. enum omap_plane_id id;
  35. const char *name;
  36. };
  37. bool is_omap_plane_dual_overlay(struct drm_plane_state *state)
  38. {
  39. struct omap_plane_state *omap_state = to_omap_plane_state(state);
  40. return !!omap_state->r_overlay;
  41. }
  42. static int omap_plane_prepare_fb(struct drm_plane *plane,
  43. struct drm_plane_state *new_state)
  44. {
  45. if (!new_state->fb)
  46. return 0;
  47. return omap_framebuffer_pin(new_state->fb);
  48. }
  49. static void omap_plane_cleanup_fb(struct drm_plane *plane,
  50. struct drm_plane_state *old_state)
  51. {
  52. if (old_state->fb)
  53. omap_framebuffer_unpin(old_state->fb);
  54. }
  55. static void omap_plane_atomic_update(struct drm_plane *plane,
  56. struct drm_plane_state *old_state)
  57. {
  58. struct omap_drm_private *priv = plane->dev->dev_private;
  59. struct omap_plane *omap_plane = to_omap_plane(plane);
  60. struct drm_plane_state *state = plane->state;
  61. struct omap_plane_state *new_omap_state;
  62. struct omap_plane_state *old_omap_state;
  63. struct omap_overlay_info info, r_info;
  64. enum omap_plane_id ovl_id, r_ovl_id;
  65. int ret;
  66. bool dual_ovl;
  67. new_omap_state = to_omap_plane_state(state);
  68. old_omap_state = to_omap_plane_state(old_state);
  69. dual_ovl = is_omap_plane_dual_overlay(state);
  70. /* Cleanup previously held overlay if needed */
  71. omap_overlay_disable(old_state->state, plane, old_omap_state->overlay);
  72. omap_overlay_disable(old_state->state, plane,
  73. old_omap_state->r_overlay);
  74. if (!new_omap_state->overlay) {
  75. DBG("[PLANE:%d:%s] overlay_id: ??? (%p)", plane->base.id, plane->name,
  76. new_omap_state->overlay);
  77. return;
  78. }
  79. ovl_id = new_omap_state->overlay->overlay_id;
  80. DBG("[PLANE:%d:%s] overlay_id: %d", plane->base.id, plane->name,
  81. ovl_id);
  82. DBG("%s, crtc=%p fb=%p", omap_plane->name, state->crtc, state->fb);
  83. memset(&info, 0, sizeof(info));
  84. info.rotation_type = OMAP_DSS_ROT_NONE;
  85. info.rotation = DRM_MODE_ROTATE_0;
  86. info.global_alpha = 0xff;
  87. info.zorder = state->normalized_zpos;
  88. r_info = info;
  89. /* update scanout: */
  90. omap_framebuffer_update_scanout(state->fb, 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->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 = priv->dispc_ops->ovl_setup(priv->dispc, ovl_id, &info,
  114. omap_crtc_timings(state->crtc), false,
  115. omap_crtc_channel(state->crtc));
  116. if (ret) {
  117. dev_err(plane->dev->dev, "Failed to setup plane1 %s\n",
  118. omap_plane->name);
  119. priv->dispc_ops->ovl_enable(priv->dispc, ovl_id, false);
  120. return;
  121. }
  122. priv->dispc_ops->ovl_enable(priv->dispc, ovl_id, true);
  123. if (dual_ovl) {
  124. ret = priv->dispc_ops->ovl_setup(priv->dispc, r_ovl_id, &r_info,
  125. omap_crtc_timings(state->crtc), false,
  126. omap_crtc_channel(state->crtc));
  127. if (ret) {
  128. dev_err(plane->dev->dev, "Failed to setup plane2 %s\n",
  129. omap_plane->name);
  130. priv->dispc_ops->ovl_enable(priv->dispc, r_ovl_id, false);
  131. priv->dispc_ops->ovl_enable(priv->dispc, ovl_id, false);
  132. return;
  133. }
  134. priv->dispc_ops->ovl_enable(priv->dispc, r_ovl_id, true);
  135. }
  136. }
  137. static void omap_plane_atomic_disable(struct drm_plane *plane,
  138. struct drm_plane_state *old_state)
  139. {
  140. struct drm_plane_state *state = plane->state;
  141. struct omap_plane_state *new_omap_state;
  142. struct omap_plane_state *old_omap_state;
  143. new_omap_state = to_omap_plane_state(state);
  144. old_omap_state = to_omap_plane_state(old_state);
  145. if (!old_omap_state->overlay)
  146. return;
  147. plane->state->rotation = DRM_MODE_ROTATE_0;
  148. plane->state->zpos = plane->type == DRM_PLANE_TYPE_PRIMARY
  149. ? 0 : old_omap_state->overlay->overlay_id;
  150. omap_overlay_disable(old_state->state, plane, old_omap_state->overlay);
  151. new_omap_state->overlay = NULL;
  152. if (is_omap_plane_dual_overlay(old_state)) {
  153. omap_overlay_disable(old_state->state, plane,
  154. old_omap_state->r_overlay);
  155. new_omap_state->r_overlay = NULL;
  156. }
  157. }
  158. #define FRAC_16_16(mult, div) (((mult) << 16) / (div))
  159. static int omap_plane_atomic_check(struct drm_plane *plane,
  160. struct drm_plane_state *state)
  161. {
  162. struct omap_drm_private *priv = plane->dev->dev_private;
  163. struct drm_crtc *crtc;
  164. struct drm_crtc_state *crtc_state;
  165. u16 width, height;
  166. u32 width_fp, height_fp;
  167. struct drm_plane_state *old_state = plane->state;
  168. struct omap_plane_state *omap_state = to_omap_plane_state(state);
  169. struct omap_global_state *omap_overlay_global_state;
  170. u32 crtc_mask;
  171. u32 fourcc;
  172. u32 caps = 0;
  173. bool new_hw_overlay = false;
  174. bool new_r_hw_overlay = false;
  175. bool is_fourcc_yuv = false;
  176. int min_scale, max_scale;
  177. int ret;
  178. omap_overlay_global_state = omap_get_global_state(state->state);
  179. if (IS_ERR(omap_overlay_global_state))
  180. return PTR_ERR(omap_overlay_global_state);
  181. DBG("%s: omap_overlay_global_state: %p", plane->name,
  182. omap_overlay_global_state);
  183. priv->dispc_ops->ovl_get_max_size(priv->dispc, &width, &height);
  184. width_fp = width << 16;
  185. height_fp = height << 16;
  186. crtc = state->crtc ? state->crtc : plane->state->crtc;
  187. if (!crtc)
  188. return 0;
  189. crtc_state = drm_atomic_get_existing_crtc_state(state->state, crtc);
  190. /* we should have a crtc state if the plane is attached to a crtc */
  191. if (WARN_ON(!crtc_state))
  192. return 0;
  193. /* Make sure dimensions are within bounds. */
  194. if (state->src_h > height_fp || state->crtc_h > height)
  195. return -EINVAL;
  196. if (state->fb)
  197. is_fourcc_yuv = state->fb->format->is_yuv;
  198. if (state->src_w > width_fp || state->crtc_w > width) {
  199. if (is_fourcc_yuv &&
  200. (((state->src_w >> 16) / 2 & 1) ||
  201. state->crtc_w / 2 & 1)) {
  202. /*
  203. * When calculating the split overlay width
  204. * and it yield an odd value we will need to adjust
  205. * the indivual width +/- 1. So make sure it fits
  206. */
  207. if (state->src_w <= ((2 * width - 1) << 16) &&
  208. state->crtc_w <= (2 * width - 1))
  209. new_r_hw_overlay = true;
  210. else
  211. return -EINVAL;
  212. } else {
  213. if (state->src_w <= (2 * width_fp) &&
  214. state->crtc_w <= (2 * width))
  215. new_r_hw_overlay = true;
  216. else
  217. return -EINVAL;
  218. }
  219. }
  220. min_scale = FRAC_16_16(1, 4);
  221. max_scale = FRAC_16_16(8, 1);
  222. ret = drm_atomic_helper_check_plane_state(state, crtc_state,
  223. min_scale, max_scale,
  224. true, true);
  225. if (ret)
  226. return ret;
  227. if (state->rotation != DRM_MODE_ROTATE_0 &&
  228. !omap_framebuffer_supports_rotation(state->fb))
  229. return -EINVAL;
  230. DBG("%s: check (%d -> %d)", plane->name,
  231. old_state->visible, state->visible);
  232. if (state->visible) {
  233. if ((state->src_w >> 16) != state->crtc_w ||
  234. (state->src_h >> 16) != state->crtc_h)
  235. caps |= OMAP_DSS_OVL_CAP_SCALE;
  236. fourcc = state->fb->format->format;
  237. crtc_mask = drm_crtc_mask(state->crtc);
  238. /*
  239. * (re)allocate hw overlay if we don't have one or
  240. * there is a caps mismatch
  241. */
  242. if (!omap_state->overlay ||
  243. (caps & ~omap_state->overlay->caps)) {
  244. new_hw_overlay = true;
  245. } else {
  246. /* check if allowed on crtc */
  247. if (!(omap_state->overlay->possible_crtcs & crtc_mask))
  248. new_hw_overlay = true;
  249. /* check supported format */
  250. if (!priv->dispc_ops->ovl_color_mode_supported(priv->dispc,
  251. omap_state->overlay->overlay_id,
  252. fourcc))
  253. new_hw_overlay = true;
  254. }
  255. /*
  256. * check if we need two overlays and only have 1 or
  257. * if we had 2 overlays but will only need 1
  258. */
  259. if ((new_r_hw_overlay && !omap_state->r_overlay) ||
  260. (!new_r_hw_overlay && omap_state->r_overlay))
  261. new_hw_overlay = true;
  262. if (new_hw_overlay) {
  263. struct omap_hw_overlay *old_ovl =
  264. omap_state->overlay;
  265. struct omap_hw_overlay *old_r_ovl =
  266. omap_state->r_overlay;
  267. struct omap_hw_overlay *new_ovl = NULL;
  268. struct omap_hw_overlay *new_r_ovl = NULL;
  269. omap_overlay_release(state->state, plane, old_ovl);
  270. omap_overlay_release(state->state, plane, old_r_ovl);
  271. ret = omap_overlay_assign(state->state, plane, caps,
  272. fourcc, crtc_mask, &new_ovl,
  273. new_r_hw_overlay ?
  274. &new_r_ovl : NULL);
  275. if (ret) {
  276. DBG("%s: failed to assign hw_overlay(s)!",
  277. plane->name);
  278. omap_state->overlay = NULL;
  279. omap_state->r_overlay = NULL;
  280. return ret;
  281. }
  282. omap_state->overlay = new_ovl;
  283. if (new_r_hw_overlay)
  284. omap_state->r_overlay = new_r_ovl;
  285. else
  286. omap_state->r_overlay = NULL;
  287. }
  288. } else {
  289. omap_overlay_release(state->state, plane, omap_state->overlay);
  290. omap_overlay_release(state->state, plane,
  291. omap_state->r_overlay);
  292. omap_state->overlay = NULL;
  293. omap_state->r_overlay = NULL;
  294. }
  295. if (omap_state->overlay)
  296. DBG("plane: %s overlay_id: %d", plane->name,
  297. omap_state->overlay->overlay_id);
  298. if (omap_state->r_overlay)
  299. DBG("plane: %s r_overlay_id: %d", plane->name,
  300. omap_state->r_overlay->overlay_id);
  301. return 0;
  302. }
  303. static const struct drm_plane_helper_funcs omap_plane_helper_funcs = {
  304. .prepare_fb = omap_plane_prepare_fb,
  305. .cleanup_fb = omap_plane_cleanup_fb,
  306. .atomic_check = omap_plane_atomic_check,
  307. .atomic_update = omap_plane_atomic_update,
  308. .atomic_disable = omap_plane_atomic_disable,
  309. };
  310. static void omap_plane_destroy(struct drm_plane *plane)
  311. {
  312. struct omap_plane *omap_plane = to_omap_plane(plane);
  313. DBG("%s", omap_plane->name);
  314. drm_plane_cleanup(plane);
  315. kfree(omap_plane);
  316. }
  317. /* helper to install properties which are common to planes and crtcs */
  318. void omap_plane_install_properties(struct drm_plane *plane,
  319. struct drm_mode_object *obj)
  320. {
  321. struct drm_device *dev = plane->dev;
  322. struct omap_drm_private *priv = dev->dev_private;
  323. if (priv->has_dmm) {
  324. if (!plane->rotation_property)
  325. drm_plane_create_rotation_property(plane,
  326. DRM_MODE_ROTATE_0,
  327. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
  328. DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270 |
  329. DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y);
  330. /* Attach the rotation property also to the crtc object */
  331. if (plane->rotation_property && obj != &plane->base)
  332. drm_object_attach_property(obj, plane->rotation_property,
  333. DRM_MODE_ROTATE_0);
  334. }
  335. drm_object_attach_property(obj, priv->zorder_prop, 0);
  336. }
  337. static void omap_plane_atomic_destroy_state(struct drm_plane *plane,
  338. struct drm_plane_state *state)
  339. {
  340. __drm_atomic_helper_plane_destroy_state(state);
  341. kfree(to_omap_plane_state(state));
  342. }
  343. static void omap_plane_reset(struct drm_plane *plane)
  344. {
  345. struct omap_plane *omap_plane = to_omap_plane(plane);
  346. struct omap_plane_state *omap_state;
  347. if (plane->state)
  348. omap_plane_atomic_destroy_state(plane, plane->state);
  349. omap_state = kzalloc(sizeof(*omap_state), GFP_KERNEL);
  350. if (!omap_state)
  351. return;
  352. omap_state->base.plane = plane;
  353. plane->state = &omap_state->base;
  354. plane->state->plane = plane;
  355. plane->state->rotation = DRM_MODE_ROTATE_0;
  356. /*
  357. * Set the zpos default depending on whether we are a primary or overlay
  358. * plane.
  359. */
  360. plane->state->zpos = plane->type == DRM_PLANE_TYPE_PRIMARY
  361. ? 0 : omap_plane->id;
  362. }
  363. static struct drm_plane_state *
  364. omap_plane_atomic_duplicate_state(struct drm_plane *plane)
  365. {
  366. struct omap_plane_state *state;
  367. struct omap_plane_state *copy;
  368. if (WARN_ON(!plane->state))
  369. return NULL;
  370. state = to_omap_plane_state(plane->state);
  371. copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
  372. if (!copy)
  373. return NULL;
  374. __drm_atomic_helper_plane_duplicate_state(plane, &copy->base);
  375. return &copy->base;
  376. }
  377. static void omap_plane_atomic_print_state(struct drm_printer *p,
  378. const struct drm_plane_state *state)
  379. {
  380. struct omap_plane_state *omap_state = to_omap_plane_state(state);
  381. drm_printf(p, "\toverlay=%s\n", omap_state->overlay ?
  382. omap_state->overlay->name : "(null)");
  383. if (omap_state->overlay) {
  384. drm_printf(p, "\t\tidx=%d\n", omap_state->overlay->idx);
  385. drm_printf(p, "\t\toverlay_id=%d\n",
  386. omap_state->overlay->overlay_id);
  387. drm_printf(p, "\t\tcaps=0x%x\n", omap_state->overlay->caps);
  388. drm_printf(p, "\t\tpossible_crtcs=0x%x\n",
  389. omap_state->overlay->possible_crtcs);
  390. }
  391. drm_printf(p, "\tr_overlay=%s\n", omap_state->r_overlay ?
  392. omap_state->r_overlay->name :
  393. "(null)");
  394. if (omap_state->r_overlay) {
  395. drm_printf(p, "\t\tidx=%d\n", omap_state->r_overlay->idx);
  396. drm_printf(p, "\t\toverlay_id=%d\n",
  397. omap_state->r_overlay->overlay_id);
  398. drm_printf(p, "\t\tcaps=0x%x\n", omap_state->r_overlay->caps);
  399. drm_printf(p, "\t\tpossible_crtcs=0x%x\n",
  400. omap_state->r_overlay->possible_crtcs);
  401. }
  402. }
  403. static int omap_plane_atomic_set_property(struct drm_plane *plane,
  404. struct drm_plane_state *state,
  405. struct drm_property *property,
  406. u64 val)
  407. {
  408. struct omap_drm_private *priv = plane->dev->dev_private;
  409. if (property == priv->zorder_prop)
  410. state->zpos = val;
  411. else
  412. return -EINVAL;
  413. return 0;
  414. }
  415. static int omap_plane_atomic_get_property(struct drm_plane *plane,
  416. const struct drm_plane_state *state,
  417. struct drm_property *property,
  418. u64 *val)
  419. {
  420. struct omap_drm_private *priv = plane->dev->dev_private;
  421. if (property == priv->zorder_prop)
  422. *val = state->zpos;
  423. else
  424. return -EINVAL;
  425. return 0;
  426. }
  427. static const struct drm_plane_funcs omap_plane_funcs = {
  428. .update_plane = drm_atomic_helper_update_plane,
  429. .disable_plane = drm_atomic_helper_disable_plane,
  430. .reset = omap_plane_reset,
  431. .destroy = omap_plane_destroy,
  432. .atomic_duplicate_state = omap_plane_atomic_duplicate_state,
  433. .atomic_destroy_state = omap_plane_atomic_destroy_state,
  434. .atomic_set_property = omap_plane_atomic_set_property,
  435. .atomic_get_property = omap_plane_atomic_get_property,
  436. .atomic_print_state = omap_plane_atomic_print_state,
  437. };
  438. static const char *plane_id_to_name[] = {
  439. [OMAP_DSS_GFX] = "gfx",
  440. [OMAP_DSS_VIDEO1] = "vid1",
  441. [OMAP_DSS_VIDEO2] = "vid2",
  442. [OMAP_DSS_VIDEO3] = "vid3",
  443. };
  444. /* initialize plane */
  445. struct drm_plane *omap_plane_init(struct drm_device *dev,
  446. int idx, enum drm_plane_type type,
  447. u32 possible_crtcs)
  448. {
  449. struct omap_drm_private *priv = dev->dev_private;
  450. unsigned int num_planes = priv->dispc_ops->get_num_ovls(priv->dispc);
  451. struct drm_plane *plane;
  452. struct omap_plane *omap_plane;
  453. int ret;
  454. u32 nformats;
  455. const u32 *formats;
  456. if (WARN_ON(idx >= num_planes))
  457. return ERR_PTR(-EINVAL);
  458. omap_plane = kzalloc(sizeof(*omap_plane), GFP_KERNEL);
  459. if (!omap_plane)
  460. return ERR_PTR(-ENOMEM);
  461. omap_plane->id = idx;
  462. omap_plane->name = plane_id_to_name[idx];
  463. DBG("%s: type=%d", omap_plane->name, type);
  464. DBG(" omap_plane->id: %d", omap_plane->id);
  465. DBG(" crtc_mask: 0x%04x", possible_crtcs);
  466. formats = priv->dispc_ops->ovl_get_color_modes(priv->dispc,
  467. omap_plane->id);
  468. for (nformats = 0; formats[nformats]; ++nformats)
  469. ;
  470. plane = &omap_plane->base;
  471. ret = drm_universal_plane_init(dev, plane, possible_crtcs,
  472. &omap_plane_funcs, formats,
  473. nformats, NULL, type, NULL);
  474. if (ret < 0)
  475. goto error;
  476. drm_plane_helper_add(plane, &omap_plane_helper_funcs);
  477. omap_plane_install_properties(plane, &plane->base);
  478. drm_plane_create_zpos_property(plane, 0, 0, num_planes - 1);
  479. return plane;
  480. error:
  481. dev_err(dev->dev, "%s(): could not create plane: %s\n",
  482. __func__, omap_plane->name);
  483. kfree(omap_plane);
  484. return NULL;
  485. }