omap_plane.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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_helper.h>
  20. #include <drm/drm_plane_helper.h>
  21. #include "omap_dmm_tiler.h"
  22. #include "omap_drv.h"
  23. /* some hackery because omapdss has an 'enum omap_plane' (which would be
  24. * better named omap_plane_id).. and compiler seems unhappy about having
  25. * both a 'struct omap_plane' and 'enum omap_plane'
  26. */
  27. #define omap_plane _omap_plane
  28. /*
  29. * plane funcs
  30. */
  31. #define to_omap_plane(x) container_of(x, struct omap_plane, base)
  32. struct omap_plane {
  33. struct drm_plane base;
  34. int id; /* TODO rename omap_plane -> omap_plane_id in omapdss so I can use the enum */
  35. const char *name;
  36. uint32_t nformats;
  37. uint32_t formats[32];
  38. struct omap_drm_irq error_irq;
  39. };
  40. struct omap_plane_state {
  41. struct drm_plane_state base;
  42. unsigned int zorder;
  43. };
  44. static inline struct omap_plane_state *
  45. to_omap_plane_state(struct drm_plane_state *state)
  46. {
  47. return container_of(state, struct omap_plane_state, base);
  48. }
  49. static void omap_plane_setup(struct drm_plane *plane)
  50. {
  51. struct omap_plane *omap_plane = to_omap_plane(plane);
  52. struct drm_plane_state *state = plane->state;
  53. struct omap_plane_state *omap_state = to_omap_plane_state(state);
  54. struct omap_overlay_info info;
  55. struct omap_drm_window win;
  56. int ret;
  57. DBG("%s, crtc=%p fb=%p", omap_plane->name, state->crtc, state->fb);
  58. if (!state->crtc) {
  59. dispc_ovl_enable(omap_plane->id, false);
  60. return;
  61. }
  62. memset(&info, 0, sizeof(info));
  63. info.rotation_type = OMAP_DSS_ROT_DMA;
  64. info.rotation = OMAP_DSS_ROT_0;
  65. info.global_alpha = 0xff;
  66. info.mirror = 0;
  67. info.zorder = omap_state->zorder;
  68. memset(&win, 0, sizeof(win));
  69. win.rotation = state->rotation;
  70. win.crtc_x = state->crtc_x;
  71. win.crtc_y = state->crtc_y;
  72. win.crtc_w = state->crtc_w;
  73. win.crtc_h = state->crtc_h;
  74. /*
  75. * src values are in Q16 fixed point, convert to integer.
  76. * omap_framebuffer_update_scanout() takes adjusted src.
  77. */
  78. win.src_x = state->src_x >> 16;
  79. win.src_y = state->src_y >> 16;
  80. switch (state->rotation & 0xf) {
  81. case BIT(DRM_ROTATE_90):
  82. case BIT(DRM_ROTATE_270):
  83. win.src_w = state->src_h >> 16;
  84. win.src_h = state->src_w >> 16;
  85. break;
  86. default:
  87. win.src_w = state->src_w >> 16;
  88. win.src_h = state->src_h >> 16;
  89. break;
  90. }
  91. /* update scanout: */
  92. omap_framebuffer_update_scanout(state->fb, &win, &info);
  93. DBG("%dx%d -> %dx%d (%d)", info.width, info.height,
  94. info.out_width, info.out_height,
  95. info.screen_width);
  96. DBG("%d,%d %pad %pad", info.pos_x, info.pos_y,
  97. &info.paddr, &info.p_uv_addr);
  98. dispc_ovl_set_channel_out(omap_plane->id,
  99. omap_crtc_channel(state->crtc));
  100. /* and finally, update omapdss: */
  101. ret = dispc_ovl_setup(omap_plane->id, &info, false,
  102. omap_crtc_timings(state->crtc), false);
  103. if (WARN_ON(ret))
  104. return;
  105. dispc_ovl_enable(omap_plane->id, true);
  106. }
  107. static int omap_plane_prepare_fb(struct drm_plane *plane,
  108. struct drm_framebuffer *fb,
  109. const struct drm_plane_state *new_state)
  110. {
  111. return omap_framebuffer_pin(fb);
  112. }
  113. static void omap_plane_cleanup_fb(struct drm_plane *plane,
  114. struct drm_framebuffer *fb,
  115. const struct drm_plane_state *old_state)
  116. {
  117. omap_framebuffer_unpin(fb);
  118. }
  119. static void omap_plane_atomic_update(struct drm_plane *plane,
  120. struct drm_plane_state *old_state)
  121. {
  122. omap_plane_setup(plane);
  123. }
  124. static void omap_plane_atomic_disable(struct drm_plane *plane,
  125. struct drm_plane_state *old_state)
  126. {
  127. struct omap_plane_state *omap_state = to_omap_plane_state(plane->state);
  128. struct omap_plane *omap_plane = to_omap_plane(plane);
  129. plane->state->rotation = BIT(DRM_ROTATE_0);
  130. omap_state->zorder = plane->type == DRM_PLANE_TYPE_PRIMARY
  131. ? 0 : omap_plane->id;
  132. omap_plane_setup(plane);
  133. }
  134. static const struct drm_plane_helper_funcs omap_plane_helper_funcs = {
  135. .prepare_fb = omap_plane_prepare_fb,
  136. .cleanup_fb = omap_plane_cleanup_fb,
  137. .atomic_update = omap_plane_atomic_update,
  138. .atomic_disable = omap_plane_atomic_disable,
  139. };
  140. static void omap_plane_reset(struct drm_plane *plane)
  141. {
  142. struct omap_plane *omap_plane = to_omap_plane(plane);
  143. struct omap_plane_state *omap_state;
  144. if (plane->state && plane->state->fb)
  145. drm_framebuffer_unreference(plane->state->fb);
  146. kfree(plane->state);
  147. plane->state = NULL;
  148. omap_state = kzalloc(sizeof(*omap_state), GFP_KERNEL);
  149. if (omap_state == NULL)
  150. return;
  151. /*
  152. * Set defaults depending on whether we are a primary or overlay
  153. * plane.
  154. */
  155. omap_state->zorder = plane->type == DRM_PLANE_TYPE_PRIMARY
  156. ? 0 : omap_plane->id;
  157. omap_state->base.rotation = BIT(DRM_ROTATE_0);
  158. plane->state = &omap_state->base;
  159. plane->state->plane = plane;
  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. struct drm_property *prop = dev->mode_config.rotation_property;
  177. drm_object_attach_property(obj, prop, 0);
  178. }
  179. drm_object_attach_property(obj, priv->zorder_prop, 0);
  180. }
  181. static struct drm_plane_state *
  182. omap_plane_atomic_duplicate_state(struct drm_plane *plane)
  183. {
  184. struct omap_plane_state *state;
  185. struct omap_plane_state *copy;
  186. if (WARN_ON(!plane->state))
  187. return NULL;
  188. state = to_omap_plane_state(plane->state);
  189. copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
  190. if (copy == NULL)
  191. return NULL;
  192. __drm_atomic_helper_plane_duplicate_state(plane, &copy->base);
  193. return &copy->base;
  194. }
  195. static void omap_plane_atomic_destroy_state(struct drm_plane *plane,
  196. struct drm_plane_state *state)
  197. {
  198. __drm_atomic_helper_plane_destroy_state(plane, state);
  199. kfree(to_omap_plane_state(state));
  200. }
  201. static int omap_plane_atomic_set_property(struct drm_plane *plane,
  202. struct drm_plane_state *state,
  203. struct drm_property *property,
  204. uint64_t val)
  205. {
  206. struct omap_drm_private *priv = plane->dev->dev_private;
  207. struct omap_plane_state *omap_state = to_omap_plane_state(state);
  208. if (property == priv->zorder_prop)
  209. omap_state->zorder = val;
  210. else
  211. return -EINVAL;
  212. return 0;
  213. }
  214. static int omap_plane_atomic_get_property(struct drm_plane *plane,
  215. const struct drm_plane_state *state,
  216. struct drm_property *property,
  217. uint64_t *val)
  218. {
  219. struct omap_drm_private *priv = plane->dev->dev_private;
  220. const struct omap_plane_state *omap_state =
  221. container_of(state, const struct omap_plane_state, base);
  222. if (property == priv->zorder_prop)
  223. *val = omap_state->zorder;
  224. else
  225. return -EINVAL;
  226. return 0;
  227. }
  228. static const struct drm_plane_funcs omap_plane_funcs = {
  229. .update_plane = drm_atomic_helper_update_plane,
  230. .disable_plane = drm_atomic_helper_disable_plane,
  231. .reset = omap_plane_reset,
  232. .destroy = omap_plane_destroy,
  233. .set_property = drm_atomic_helper_plane_set_property,
  234. .atomic_duplicate_state = omap_plane_atomic_duplicate_state,
  235. .atomic_destroy_state = omap_plane_atomic_destroy_state,
  236. .atomic_set_property = omap_plane_atomic_set_property,
  237. .atomic_get_property = omap_plane_atomic_get_property,
  238. };
  239. static void omap_plane_error_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
  240. {
  241. struct omap_plane *omap_plane =
  242. container_of(irq, struct omap_plane, error_irq);
  243. DRM_ERROR_RATELIMITED("%s: errors: %08x\n", omap_plane->name,
  244. irqstatus);
  245. }
  246. static const char *plane_names[] = {
  247. [OMAP_DSS_GFX] = "gfx",
  248. [OMAP_DSS_VIDEO1] = "vid1",
  249. [OMAP_DSS_VIDEO2] = "vid2",
  250. [OMAP_DSS_VIDEO3] = "vid3",
  251. };
  252. static const uint32_t error_irqs[] = {
  253. [OMAP_DSS_GFX] = DISPC_IRQ_GFX_FIFO_UNDERFLOW,
  254. [OMAP_DSS_VIDEO1] = DISPC_IRQ_VID1_FIFO_UNDERFLOW,
  255. [OMAP_DSS_VIDEO2] = DISPC_IRQ_VID2_FIFO_UNDERFLOW,
  256. [OMAP_DSS_VIDEO3] = DISPC_IRQ_VID3_FIFO_UNDERFLOW,
  257. };
  258. /* initialize plane */
  259. struct drm_plane *omap_plane_init(struct drm_device *dev,
  260. int id, enum drm_plane_type type)
  261. {
  262. struct omap_drm_private *priv = dev->dev_private;
  263. struct drm_plane *plane;
  264. struct omap_plane *omap_plane;
  265. int ret;
  266. DBG("%s: type=%d", plane_names[id], type);
  267. omap_plane = kzalloc(sizeof(*omap_plane), GFP_KERNEL);
  268. if (!omap_plane)
  269. return ERR_PTR(-ENOMEM);
  270. omap_plane->nformats = omap_framebuffer_get_formats(
  271. omap_plane->formats, ARRAY_SIZE(omap_plane->formats),
  272. dss_feat_get_supported_color_modes(id));
  273. omap_plane->id = id;
  274. omap_plane->name = plane_names[id];
  275. plane = &omap_plane->base;
  276. omap_plane->error_irq.irqmask = error_irqs[id];
  277. omap_plane->error_irq.irq = omap_plane_error_irq;
  278. omap_irq_register(dev, &omap_plane->error_irq);
  279. ret = drm_universal_plane_init(dev, plane, (1 << priv->num_crtcs) - 1,
  280. &omap_plane_funcs, omap_plane->formats,
  281. omap_plane->nformats, type);
  282. if (ret < 0)
  283. goto error;
  284. drm_plane_helper_add(plane, &omap_plane_helper_funcs);
  285. omap_plane_install_properties(plane, &plane->base);
  286. return plane;
  287. error:
  288. omap_irq_unregister(plane->dev, &omap_plane->error_irq);
  289. kfree(omap_plane);
  290. return NULL;
  291. }