omap_plane.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * drivers/gpu/drm/omapdrm/omap_plane.c
  3. *
  4. * Copyright (C) 2011 Texas Instruments
  5. * Author: Rob Clark <rob.clark@linaro.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <drm/drm_atomic.h>
  20. #include <drm/drm_atomic_helper.h>
  21. #include <drm/drm_plane_helper.h>
  22. #include "omap_dmm_tiler.h"
  23. #include "omap_drv.h"
  24. /* some hackery because omapdss has an 'enum omap_plane' (which would be
  25. * better named omap_plane_id).. and compiler seems unhappy about having
  26. * both a 'struct omap_plane' and 'enum omap_plane'
  27. */
  28. #define omap_plane _omap_plane
  29. /*
  30. * plane funcs
  31. */
  32. #define to_omap_plane(x) container_of(x, struct omap_plane, base)
  33. struct omap_plane {
  34. struct drm_plane base;
  35. int id; /* TODO rename omap_plane -> omap_plane_id in omapdss so I can use the enum */
  36. const char *name;
  37. uint32_t nformats;
  38. uint32_t formats[32];
  39. struct omap_drm_irq error_irq;
  40. };
  41. struct omap_plane_state {
  42. struct drm_plane_state base;
  43. unsigned int zorder;
  44. };
  45. static inline struct omap_plane_state *
  46. to_omap_plane_state(struct drm_plane_state *state)
  47. {
  48. return container_of(state, struct omap_plane_state, base);
  49. }
  50. static int omap_plane_prepare_fb(struct drm_plane *plane,
  51. struct drm_plane_state *new_state)
  52. {
  53. if (!new_state->fb)
  54. return 0;
  55. return omap_framebuffer_pin(new_state->fb);
  56. }
  57. static void omap_plane_cleanup_fb(struct drm_plane *plane,
  58. struct drm_plane_state *old_state)
  59. {
  60. if (old_state->fb)
  61. omap_framebuffer_unpin(old_state->fb);
  62. }
  63. static void omap_plane_atomic_update(struct drm_plane *plane,
  64. struct drm_plane_state *old_state)
  65. {
  66. struct omap_plane *omap_plane = to_omap_plane(plane);
  67. struct drm_plane_state *state = plane->state;
  68. struct omap_plane_state *omap_state = to_omap_plane_state(state);
  69. struct omap_overlay_info info;
  70. struct omap_drm_window win;
  71. int ret;
  72. DBG("%s, crtc=%p fb=%p", omap_plane->name, state->crtc, state->fb);
  73. memset(&info, 0, sizeof(info));
  74. info.rotation_type = OMAP_DSS_ROT_DMA;
  75. info.rotation = OMAP_DSS_ROT_0;
  76. info.global_alpha = 0xff;
  77. info.mirror = 0;
  78. info.zorder = omap_state->zorder;
  79. memset(&win, 0, sizeof(win));
  80. win.rotation = state->rotation;
  81. win.crtc_x = state->crtc_x;
  82. win.crtc_y = state->crtc_y;
  83. win.crtc_w = state->crtc_w;
  84. win.crtc_h = state->crtc_h;
  85. /*
  86. * src values are in Q16 fixed point, convert to integer.
  87. * omap_framebuffer_update_scanout() takes adjusted src.
  88. */
  89. win.src_x = state->src_x >> 16;
  90. win.src_y = state->src_y >> 16;
  91. if (drm_rotation_90_or_270(state->rotation)) {
  92. win.src_w = state->src_h >> 16;
  93. win.src_h = state->src_w >> 16;
  94. } else {
  95. win.src_w = state->src_w >> 16;
  96. win.src_h = state->src_h >> 16;
  97. }
  98. /* update scanout: */
  99. omap_framebuffer_update_scanout(state->fb, &win, &info);
  100. DBG("%dx%d -> %dx%d (%d)", info.width, info.height,
  101. info.out_width, info.out_height,
  102. info.screen_width);
  103. DBG("%d,%d %pad %pad", info.pos_x, info.pos_y,
  104. &info.paddr, &info.p_uv_addr);
  105. dispc_ovl_set_channel_out(omap_plane->id,
  106. omap_crtc_channel(state->crtc));
  107. /* and finally, update omapdss: */
  108. ret = dispc_ovl_setup(omap_plane->id, &info, false,
  109. omap_crtc_timings(state->crtc), false);
  110. if (ret) {
  111. dev_err(plane->dev->dev, "Failed to setup plane %s\n",
  112. omap_plane->name);
  113. dispc_ovl_enable(omap_plane->id, false);
  114. return;
  115. }
  116. dispc_ovl_enable(omap_plane->id, true);
  117. }
  118. static void omap_plane_atomic_disable(struct drm_plane *plane,
  119. struct drm_plane_state *old_state)
  120. {
  121. struct omap_plane_state *omap_state = to_omap_plane_state(plane->state);
  122. struct omap_plane *omap_plane = to_omap_plane(plane);
  123. plane->state->rotation = DRM_ROTATE_0;
  124. omap_state->zorder = plane->type == DRM_PLANE_TYPE_PRIMARY
  125. ? 0 : omap_plane->id;
  126. dispc_ovl_enable(omap_plane->id, false);
  127. }
  128. static int omap_plane_atomic_check(struct drm_plane *plane,
  129. struct drm_plane_state *state)
  130. {
  131. struct drm_crtc_state *crtc_state;
  132. if (!state->fb)
  133. return 0;
  134. /* crtc should only be NULL when disabling (i.e., !state->fb) */
  135. if (WARN_ON(!state->crtc))
  136. return 0;
  137. crtc_state = drm_atomic_get_existing_crtc_state(state->state, state->crtc);
  138. /* we should have a crtc state if the plane is attached to a crtc */
  139. if (WARN_ON(!crtc_state))
  140. return 0;
  141. if (!crtc_state->enable)
  142. return 0;
  143. if (state->crtc_x < 0 || state->crtc_y < 0)
  144. return -EINVAL;
  145. if (state->crtc_x + state->crtc_w > crtc_state->adjusted_mode.hdisplay)
  146. return -EINVAL;
  147. if (state->crtc_y + state->crtc_h > crtc_state->adjusted_mode.vdisplay)
  148. return -EINVAL;
  149. if (state->rotation != DRM_ROTATE_0 &&
  150. !omap_framebuffer_supports_rotation(state->fb))
  151. return -EINVAL;
  152. return 0;
  153. }
  154. static const struct drm_plane_helper_funcs omap_plane_helper_funcs = {
  155. .prepare_fb = omap_plane_prepare_fb,
  156. .cleanup_fb = omap_plane_cleanup_fb,
  157. .atomic_check = omap_plane_atomic_check,
  158. .atomic_update = omap_plane_atomic_update,
  159. .atomic_disable = omap_plane_atomic_disable,
  160. };
  161. static void omap_plane_destroy(struct drm_plane *plane)
  162. {
  163. struct omap_plane *omap_plane = to_omap_plane(plane);
  164. DBG("%s", omap_plane->name);
  165. omap_irq_unregister(plane->dev, &omap_plane->error_irq);
  166. drm_plane_cleanup(plane);
  167. kfree(omap_plane);
  168. }
  169. /* helper to install properties which are common to planes and crtcs */
  170. void omap_plane_install_properties(struct drm_plane *plane,
  171. struct drm_mode_object *obj)
  172. {
  173. struct drm_device *dev = plane->dev;
  174. struct omap_drm_private *priv = dev->dev_private;
  175. if (priv->has_dmm) {
  176. if (!plane->rotation_property)
  177. drm_plane_create_rotation_property(plane,
  178. DRM_ROTATE_0,
  179. DRM_ROTATE_0 | DRM_ROTATE_90 |
  180. DRM_ROTATE_180 | DRM_ROTATE_270 |
  181. DRM_REFLECT_X | DRM_REFLECT_Y);
  182. /* Attach the rotation property also to the crtc object */
  183. if (plane->rotation_property && obj != &plane->base)
  184. drm_object_attach_property(obj, plane->rotation_property,
  185. DRM_ROTATE_0);
  186. }
  187. drm_object_attach_property(obj, priv->zorder_prop, 0);
  188. }
  189. static struct drm_plane_state *
  190. omap_plane_atomic_duplicate_state(struct drm_plane *plane)
  191. {
  192. struct omap_plane_state *state;
  193. struct omap_plane_state *copy;
  194. if (WARN_ON(!plane->state))
  195. return NULL;
  196. state = to_omap_plane_state(plane->state);
  197. copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
  198. if (copy == NULL)
  199. return NULL;
  200. __drm_atomic_helper_plane_duplicate_state(plane, &copy->base);
  201. return &copy->base;
  202. }
  203. static void omap_plane_atomic_destroy_state(struct drm_plane *plane,
  204. struct drm_plane_state *state)
  205. {
  206. __drm_atomic_helper_plane_destroy_state(state);
  207. kfree(to_omap_plane_state(state));
  208. }
  209. static void omap_plane_reset(struct drm_plane *plane)
  210. {
  211. struct omap_plane *omap_plane = to_omap_plane(plane);
  212. struct omap_plane_state *omap_state;
  213. if (plane->state) {
  214. omap_plane_atomic_destroy_state(plane, plane->state);
  215. plane->state = NULL;
  216. }
  217. omap_state = kzalloc(sizeof(*omap_state), GFP_KERNEL);
  218. if (omap_state == NULL)
  219. return;
  220. /*
  221. * Set defaults depending on whether we are a primary or overlay
  222. * plane.
  223. */
  224. omap_state->zorder = plane->type == DRM_PLANE_TYPE_PRIMARY
  225. ? 0 : omap_plane->id;
  226. omap_state->base.rotation = DRM_ROTATE_0;
  227. plane->state = &omap_state->base;
  228. plane->state->plane = plane;
  229. }
  230. static int omap_plane_atomic_set_property(struct drm_plane *plane,
  231. struct drm_plane_state *state,
  232. struct drm_property *property,
  233. uint64_t val)
  234. {
  235. struct omap_drm_private *priv = plane->dev->dev_private;
  236. struct omap_plane_state *omap_state = to_omap_plane_state(state);
  237. if (property == priv->zorder_prop)
  238. omap_state->zorder = val;
  239. else
  240. return -EINVAL;
  241. return 0;
  242. }
  243. static int omap_plane_atomic_get_property(struct drm_plane *plane,
  244. const struct drm_plane_state *state,
  245. struct drm_property *property,
  246. uint64_t *val)
  247. {
  248. struct omap_drm_private *priv = plane->dev->dev_private;
  249. const struct omap_plane_state *omap_state =
  250. container_of(state, const struct omap_plane_state, base);
  251. if (property == priv->zorder_prop)
  252. *val = omap_state->zorder;
  253. else
  254. return -EINVAL;
  255. return 0;
  256. }
  257. static const struct drm_plane_funcs omap_plane_funcs = {
  258. .update_plane = drm_atomic_helper_update_plane,
  259. .disable_plane = drm_atomic_helper_disable_plane,
  260. .reset = omap_plane_reset,
  261. .destroy = omap_plane_destroy,
  262. .set_property = drm_atomic_helper_plane_set_property,
  263. .atomic_duplicate_state = omap_plane_atomic_duplicate_state,
  264. .atomic_destroy_state = omap_plane_atomic_destroy_state,
  265. .atomic_set_property = omap_plane_atomic_set_property,
  266. .atomic_get_property = omap_plane_atomic_get_property,
  267. };
  268. static void omap_plane_error_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
  269. {
  270. struct omap_plane *omap_plane =
  271. container_of(irq, struct omap_plane, error_irq);
  272. DRM_ERROR_RATELIMITED("%s: errors: %08x\n", omap_plane->name,
  273. irqstatus);
  274. }
  275. static const char *plane_names[] = {
  276. [OMAP_DSS_GFX] = "gfx",
  277. [OMAP_DSS_VIDEO1] = "vid1",
  278. [OMAP_DSS_VIDEO2] = "vid2",
  279. [OMAP_DSS_VIDEO3] = "vid3",
  280. };
  281. static const uint32_t error_irqs[] = {
  282. [OMAP_DSS_GFX] = DISPC_IRQ_GFX_FIFO_UNDERFLOW,
  283. [OMAP_DSS_VIDEO1] = DISPC_IRQ_VID1_FIFO_UNDERFLOW,
  284. [OMAP_DSS_VIDEO2] = DISPC_IRQ_VID2_FIFO_UNDERFLOW,
  285. [OMAP_DSS_VIDEO3] = DISPC_IRQ_VID3_FIFO_UNDERFLOW,
  286. };
  287. /* initialize plane */
  288. struct drm_plane *omap_plane_init(struct drm_device *dev,
  289. int id, enum drm_plane_type type,
  290. u32 possible_crtcs)
  291. {
  292. struct drm_plane *plane;
  293. struct omap_plane *omap_plane;
  294. int ret;
  295. DBG("%s: type=%d", plane_names[id], type);
  296. omap_plane = kzalloc(sizeof(*omap_plane), GFP_KERNEL);
  297. if (!omap_plane)
  298. return ERR_PTR(-ENOMEM);
  299. omap_plane->nformats = omap_framebuffer_get_formats(
  300. omap_plane->formats, ARRAY_SIZE(omap_plane->formats),
  301. dss_feat_get_supported_color_modes(id));
  302. omap_plane->id = id;
  303. omap_plane->name = plane_names[id];
  304. plane = &omap_plane->base;
  305. omap_plane->error_irq.irqmask = error_irqs[id];
  306. omap_plane->error_irq.irq = omap_plane_error_irq;
  307. omap_irq_register(dev, &omap_plane->error_irq);
  308. ret = drm_universal_plane_init(dev, plane, possible_crtcs,
  309. &omap_plane_funcs, omap_plane->formats,
  310. omap_plane->nformats, type, NULL);
  311. if (ret < 0)
  312. goto error;
  313. drm_plane_helper_add(plane, &omap_plane_helper_funcs);
  314. omap_plane_install_properties(plane, &plane->base);
  315. return plane;
  316. error:
  317. omap_irq_unregister(plane->dev, &omap_plane->error_irq);
  318. kfree(omap_plane);
  319. return NULL;
  320. }