omap_plane.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. const 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. const 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. switch (state->rotation & DRM_ROTATE_MASK) {
  92. case BIT(DRM_ROTATE_90):
  93. case BIT(DRM_ROTATE_270):
  94. win.src_w = state->src_h >> 16;
  95. win.src_h = state->src_w >> 16;
  96. break;
  97. default:
  98. win.src_w = state->src_w >> 16;
  99. win.src_h = state->src_h >> 16;
  100. break;
  101. }
  102. /* update scanout: */
  103. omap_framebuffer_update_scanout(state->fb, &win, &info);
  104. DBG("%dx%d -> %dx%d (%d)", info.width, info.height,
  105. info.out_width, info.out_height,
  106. info.screen_width);
  107. DBG("%d,%d %pad %pad", info.pos_x, info.pos_y,
  108. &info.paddr, &info.p_uv_addr);
  109. dispc_ovl_set_channel_out(omap_plane->id,
  110. omap_crtc_channel(state->crtc));
  111. /* and finally, update omapdss: */
  112. ret = dispc_ovl_setup(omap_plane->id, &info, false,
  113. omap_crtc_timings(state->crtc), false);
  114. if (WARN_ON(ret)) {
  115. dispc_ovl_enable(omap_plane->id, false);
  116. return;
  117. }
  118. dispc_ovl_enable(omap_plane->id, true);
  119. }
  120. static void omap_plane_atomic_disable(struct drm_plane *plane,
  121. struct drm_plane_state *old_state)
  122. {
  123. struct omap_plane_state *omap_state = to_omap_plane_state(plane->state);
  124. struct omap_plane *omap_plane = to_omap_plane(plane);
  125. plane->state->rotation = BIT(DRM_ROTATE_0);
  126. omap_state->zorder = plane->type == DRM_PLANE_TYPE_PRIMARY
  127. ? 0 : omap_plane->id;
  128. dispc_ovl_enable(omap_plane->id, false);
  129. }
  130. static int omap_plane_atomic_check(struct drm_plane *plane,
  131. struct drm_plane_state *state)
  132. {
  133. struct drm_crtc_state *crtc_state;
  134. if (!state->crtc)
  135. return 0;
  136. crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
  137. if (IS_ERR(crtc_state))
  138. return PTR_ERR(crtc_state);
  139. if (state->crtc_x < 0 || state->crtc_y < 0)
  140. return -EINVAL;
  141. if (state->crtc_x + state->crtc_w > crtc_state->adjusted_mode.hdisplay)
  142. return -EINVAL;
  143. if (state->crtc_y + state->crtc_h > crtc_state->adjusted_mode.vdisplay)
  144. return -EINVAL;
  145. return 0;
  146. }
  147. static const struct drm_plane_helper_funcs omap_plane_helper_funcs = {
  148. .prepare_fb = omap_plane_prepare_fb,
  149. .cleanup_fb = omap_plane_cleanup_fb,
  150. .atomic_check = omap_plane_atomic_check,
  151. .atomic_update = omap_plane_atomic_update,
  152. .atomic_disable = omap_plane_atomic_disable,
  153. };
  154. static void omap_plane_reset(struct drm_plane *plane)
  155. {
  156. struct omap_plane *omap_plane = to_omap_plane(plane);
  157. struct omap_plane_state *omap_state;
  158. if (plane->state && plane->state->fb)
  159. drm_framebuffer_unreference(plane->state->fb);
  160. kfree(plane->state);
  161. plane->state = NULL;
  162. omap_state = kzalloc(sizeof(*omap_state), GFP_KERNEL);
  163. if (omap_state == NULL)
  164. return;
  165. /*
  166. * Set defaults depending on whether we are a primary or overlay
  167. * plane.
  168. */
  169. omap_state->zorder = plane->type == DRM_PLANE_TYPE_PRIMARY
  170. ? 0 : omap_plane->id;
  171. omap_state->base.rotation = BIT(DRM_ROTATE_0);
  172. plane->state = &omap_state->base;
  173. plane->state->plane = plane;
  174. }
  175. static void omap_plane_destroy(struct drm_plane *plane)
  176. {
  177. struct omap_plane *omap_plane = to_omap_plane(plane);
  178. DBG("%s", omap_plane->name);
  179. omap_irq_unregister(plane->dev, &omap_plane->error_irq);
  180. drm_plane_cleanup(plane);
  181. kfree(omap_plane);
  182. }
  183. /* helper to install properties which are common to planes and crtcs */
  184. void omap_plane_install_properties(struct drm_plane *plane,
  185. struct drm_mode_object *obj)
  186. {
  187. struct drm_device *dev = plane->dev;
  188. struct omap_drm_private *priv = dev->dev_private;
  189. if (priv->has_dmm) {
  190. struct drm_property *prop = dev->mode_config.rotation_property;
  191. drm_object_attach_property(obj, prop, 0);
  192. }
  193. drm_object_attach_property(obj, priv->zorder_prop, 0);
  194. }
  195. static struct drm_plane_state *
  196. omap_plane_atomic_duplicate_state(struct drm_plane *plane)
  197. {
  198. struct omap_plane_state *state;
  199. struct omap_plane_state *copy;
  200. if (WARN_ON(!plane->state))
  201. return NULL;
  202. state = to_omap_plane_state(plane->state);
  203. copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
  204. if (copy == NULL)
  205. return NULL;
  206. __drm_atomic_helper_plane_duplicate_state(plane, &copy->base);
  207. return &copy->base;
  208. }
  209. static void omap_plane_atomic_destroy_state(struct drm_plane *plane,
  210. struct drm_plane_state *state)
  211. {
  212. __drm_atomic_helper_plane_destroy_state(plane, state);
  213. kfree(to_omap_plane_state(state));
  214. }
  215. static int omap_plane_atomic_set_property(struct drm_plane *plane,
  216. struct drm_plane_state *state,
  217. struct drm_property *property,
  218. uint64_t val)
  219. {
  220. struct omap_drm_private *priv = plane->dev->dev_private;
  221. struct omap_plane_state *omap_state = to_omap_plane_state(state);
  222. if (property == priv->zorder_prop)
  223. omap_state->zorder = val;
  224. else
  225. return -EINVAL;
  226. return 0;
  227. }
  228. static int omap_plane_atomic_get_property(struct drm_plane *plane,
  229. const struct drm_plane_state *state,
  230. struct drm_property *property,
  231. uint64_t *val)
  232. {
  233. struct omap_drm_private *priv = plane->dev->dev_private;
  234. const struct omap_plane_state *omap_state =
  235. container_of(state, const struct omap_plane_state, base);
  236. if (property == priv->zorder_prop)
  237. *val = omap_state->zorder;
  238. else
  239. return -EINVAL;
  240. return 0;
  241. }
  242. static const struct drm_plane_funcs omap_plane_funcs = {
  243. .update_plane = drm_atomic_helper_update_plane,
  244. .disable_plane = drm_atomic_helper_disable_plane,
  245. .reset = omap_plane_reset,
  246. .destroy = omap_plane_destroy,
  247. .set_property = drm_atomic_helper_plane_set_property,
  248. .atomic_duplicate_state = omap_plane_atomic_duplicate_state,
  249. .atomic_destroy_state = omap_plane_atomic_destroy_state,
  250. .atomic_set_property = omap_plane_atomic_set_property,
  251. .atomic_get_property = omap_plane_atomic_get_property,
  252. };
  253. static void omap_plane_error_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
  254. {
  255. struct omap_plane *omap_plane =
  256. container_of(irq, struct omap_plane, error_irq);
  257. DRM_ERROR_RATELIMITED("%s: errors: %08x\n", omap_plane->name,
  258. irqstatus);
  259. }
  260. static const char *plane_names[] = {
  261. [OMAP_DSS_GFX] = "gfx",
  262. [OMAP_DSS_VIDEO1] = "vid1",
  263. [OMAP_DSS_VIDEO2] = "vid2",
  264. [OMAP_DSS_VIDEO3] = "vid3",
  265. };
  266. static const uint32_t error_irqs[] = {
  267. [OMAP_DSS_GFX] = DISPC_IRQ_GFX_FIFO_UNDERFLOW,
  268. [OMAP_DSS_VIDEO1] = DISPC_IRQ_VID1_FIFO_UNDERFLOW,
  269. [OMAP_DSS_VIDEO2] = DISPC_IRQ_VID2_FIFO_UNDERFLOW,
  270. [OMAP_DSS_VIDEO3] = DISPC_IRQ_VID3_FIFO_UNDERFLOW,
  271. };
  272. /* initialize plane */
  273. struct drm_plane *omap_plane_init(struct drm_device *dev,
  274. int id, enum drm_plane_type type)
  275. {
  276. struct omap_drm_private *priv = dev->dev_private;
  277. struct drm_plane *plane;
  278. struct omap_plane *omap_plane;
  279. int ret;
  280. DBG("%s: type=%d", plane_names[id], type);
  281. omap_plane = kzalloc(sizeof(*omap_plane), GFP_KERNEL);
  282. if (!omap_plane)
  283. return ERR_PTR(-ENOMEM);
  284. omap_plane->nformats = omap_framebuffer_get_formats(
  285. omap_plane->formats, ARRAY_SIZE(omap_plane->formats),
  286. dss_feat_get_supported_color_modes(id));
  287. omap_plane->id = id;
  288. omap_plane->name = plane_names[id];
  289. plane = &omap_plane->base;
  290. omap_plane->error_irq.irqmask = error_irqs[id];
  291. omap_plane->error_irq.irq = omap_plane_error_irq;
  292. omap_irq_register(dev, &omap_plane->error_irq);
  293. ret = drm_universal_plane_init(dev, plane, (1 << priv->num_crtcs) - 1,
  294. &omap_plane_funcs, omap_plane->formats,
  295. omap_plane->nformats, type);
  296. if (ret < 0)
  297. goto error;
  298. drm_plane_helper_add(plane, &omap_plane_helper_funcs);
  299. omap_plane_install_properties(plane, &plane->base);
  300. return plane;
  301. error:
  302. omap_irq_unregister(plane->dev, &omap_plane->error_irq);
  303. kfree(omap_plane);
  304. return NULL;
  305. }