omap_plane.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. struct omap_overlay_info info;
  37. /* position/orientation of scanout within the fb: */
  38. struct omap_drm_window win;
  39. bool enabled;
  40. /* last fb that we pinned: */
  41. struct drm_framebuffer *pinned_fb;
  42. uint32_t nformats;
  43. uint32_t formats[32];
  44. struct omap_drm_irq error_irq;
  45. };
  46. /* update which fb (if any) is pinned for scanout */
  47. static int omap_plane_update_pin(struct drm_plane *plane)
  48. {
  49. struct omap_plane *omap_plane = to_omap_plane(plane);
  50. struct drm_framebuffer *pinned_fb = omap_plane->pinned_fb;
  51. struct drm_framebuffer *fb = omap_plane->enabled ? plane->fb : NULL;
  52. int ret = 0;
  53. if (pinned_fb == fb)
  54. return 0;
  55. DBG("%p -> %p", pinned_fb, fb);
  56. if (fb) {
  57. drm_framebuffer_reference(fb);
  58. ret = omap_framebuffer_pin(fb);
  59. }
  60. if (pinned_fb)
  61. omap_crtc_queue_unpin(plane->crtc, pinned_fb);
  62. if (ret) {
  63. dev_err(plane->dev->dev, "could not swap %p -> %p\n",
  64. omap_plane->pinned_fb, fb);
  65. drm_framebuffer_unreference(fb);
  66. omap_plane->pinned_fb = NULL;
  67. return ret;
  68. }
  69. omap_plane->pinned_fb = fb;
  70. return 0;
  71. }
  72. static int __omap_plane_setup(struct omap_plane *omap_plane,
  73. struct drm_crtc *crtc,
  74. struct drm_framebuffer *fb)
  75. {
  76. struct omap_overlay_info *info = &omap_plane->info;
  77. struct drm_device *dev = omap_plane->base.dev;
  78. int ret;
  79. DBG("%s, enabled=%d", omap_plane->name, omap_plane->enabled);
  80. if (!omap_plane->enabled) {
  81. dispc_ovl_enable(omap_plane->id, false);
  82. return 0;
  83. }
  84. /* update scanout: */
  85. omap_framebuffer_update_scanout(fb, &omap_plane->win, info);
  86. DBG("%dx%d -> %dx%d (%d)", info->width, info->height,
  87. info->out_width, info->out_height,
  88. info->screen_width);
  89. DBG("%d,%d %pad %pad", info->pos_x, info->pos_y,
  90. &info->paddr, &info->p_uv_addr);
  91. dispc_ovl_set_channel_out(omap_plane->id,
  92. omap_crtc_channel(crtc));
  93. /* and finally, update omapdss: */
  94. ret = dispc_ovl_setup(omap_plane->id, info, false,
  95. omap_crtc_timings(crtc), false);
  96. if (ret) {
  97. dev_err(dev->dev, "dispc_ovl_setup failed: %d\n", ret);
  98. return ret;
  99. }
  100. dispc_ovl_enable(omap_plane->id, true);
  101. return 0;
  102. }
  103. static int omap_plane_setup(struct omap_plane *omap_plane)
  104. {
  105. struct drm_plane *plane = &omap_plane->base;
  106. int ret;
  107. ret = omap_plane_update_pin(plane);
  108. if (ret < 0)
  109. return ret;
  110. dispc_runtime_get();
  111. ret = __omap_plane_setup(omap_plane, plane->crtc, plane->fb);
  112. dispc_runtime_put();
  113. return ret;
  114. }
  115. int omap_plane_mode_set(struct drm_plane *plane,
  116. struct drm_crtc *crtc, struct drm_framebuffer *fb,
  117. int crtc_x, int crtc_y,
  118. unsigned int crtc_w, unsigned int crtc_h,
  119. unsigned int src_x, unsigned int src_y,
  120. unsigned int src_w, unsigned int src_h)
  121. {
  122. struct omap_plane *omap_plane = to_omap_plane(plane);
  123. struct omap_drm_window *win = &omap_plane->win;
  124. win->crtc_x = crtc_x;
  125. win->crtc_y = crtc_y;
  126. win->crtc_w = crtc_w;
  127. win->crtc_h = crtc_h;
  128. win->src_x = src_x;
  129. win->src_y = src_y;
  130. win->src_w = src_w;
  131. win->src_h = src_h;
  132. return omap_plane_setup(omap_plane);
  133. }
  134. int omap_plane_set_enable(struct drm_plane *plane, bool enable)
  135. {
  136. struct omap_plane *omap_plane = to_omap_plane(plane);
  137. if (enable == omap_plane->enabled)
  138. return 0;
  139. omap_plane->enabled = enable;
  140. return omap_plane_setup(omap_plane);
  141. }
  142. static int omap_plane_prepare_fb(struct drm_plane *plane,
  143. struct drm_framebuffer *fb,
  144. const struct drm_plane_state *new_state)
  145. {
  146. return omap_framebuffer_pin(fb);
  147. }
  148. static void omap_plane_cleanup_fb(struct drm_plane *plane,
  149. struct drm_framebuffer *fb,
  150. const struct drm_plane_state *old_state)
  151. {
  152. omap_framebuffer_unpin(fb);
  153. }
  154. static void omap_plane_atomic_update(struct drm_plane *plane,
  155. struct drm_plane_state *old_state)
  156. {
  157. struct omap_plane *omap_plane = to_omap_plane(plane);
  158. struct omap_drm_window *win = &omap_plane->win;
  159. struct drm_plane_state *state = plane->state;
  160. uint32_t src_w;
  161. uint32_t src_h;
  162. if (!state->fb || !state->crtc)
  163. return;
  164. /* omap_framebuffer_update_scanout() takes adjusted src */
  165. switch (omap_plane->win.rotation & 0xf) {
  166. case BIT(DRM_ROTATE_90):
  167. case BIT(DRM_ROTATE_270):
  168. src_w = state->src_h;
  169. src_h = state->src_w;
  170. break;
  171. default:
  172. src_w = state->src_w;
  173. src_h = state->src_h;
  174. break;
  175. }
  176. /* src values are in Q16 fixed point, convert to integer. */
  177. win->crtc_x = state->crtc_x;
  178. win->crtc_y = state->crtc_y;
  179. win->crtc_w = state->crtc_w;
  180. win->crtc_h = state->crtc_h;
  181. win->src_x = state->src_x >> 16;
  182. win->src_y = state->src_y >> 16;
  183. win->src_w = src_w >> 16;
  184. win->src_h = src_h >> 16;
  185. omap_plane->enabled = true;
  186. __omap_plane_setup(omap_plane, state->crtc, state->fb);
  187. }
  188. static void omap_plane_atomic_disable(struct drm_plane *plane,
  189. struct drm_plane_state *old_state)
  190. {
  191. struct omap_plane *omap_plane = to_omap_plane(plane);
  192. omap_plane->win.rotation = BIT(DRM_ROTATE_0);
  193. omap_plane->info.zorder = plane->type == DRM_PLANE_TYPE_PRIMARY
  194. ? 0 : omap_plane->id;
  195. if (!omap_plane->enabled)
  196. return;
  197. omap_plane->enabled = false;
  198. __omap_plane_setup(omap_plane, NULL, NULL);
  199. }
  200. static const struct drm_plane_helper_funcs omap_plane_helper_funcs = {
  201. .prepare_fb = omap_plane_prepare_fb,
  202. .cleanup_fb = omap_plane_cleanup_fb,
  203. .atomic_update = omap_plane_atomic_update,
  204. .atomic_disable = omap_plane_atomic_disable,
  205. };
  206. static void omap_plane_destroy(struct drm_plane *plane)
  207. {
  208. struct omap_plane *omap_plane = to_omap_plane(plane);
  209. DBG("%s", omap_plane->name);
  210. omap_irq_unregister(plane->dev, &omap_plane->error_irq);
  211. drm_plane_cleanup(plane);
  212. kfree(omap_plane);
  213. }
  214. /* helper to install properties which are common to planes and crtcs */
  215. void omap_plane_install_properties(struct drm_plane *plane,
  216. struct drm_mode_object *obj)
  217. {
  218. struct drm_device *dev = plane->dev;
  219. struct omap_drm_private *priv = dev->dev_private;
  220. if (priv->has_dmm) {
  221. struct drm_property *prop = dev->mode_config.rotation_property;
  222. drm_object_attach_property(obj, prop, 0);
  223. }
  224. drm_object_attach_property(obj, priv->zorder_prop, 0);
  225. }
  226. int omap_plane_set_property(struct drm_plane *plane,
  227. struct drm_property *property, uint64_t val)
  228. {
  229. struct omap_plane *omap_plane = to_omap_plane(plane);
  230. struct omap_drm_private *priv = plane->dev->dev_private;
  231. int ret;
  232. if (property == plane->dev->mode_config.rotation_property) {
  233. DBG("%s: rotation: %02x", omap_plane->name, (uint32_t)val);
  234. omap_plane->win.rotation = val;
  235. } else if (property == priv->zorder_prop) {
  236. DBG("%s: zorder: %02x", omap_plane->name, (uint32_t)val);
  237. omap_plane->info.zorder = val;
  238. } else {
  239. return -EINVAL;
  240. }
  241. /*
  242. * We're done if the plane is disabled, properties will be applied the
  243. * next time it becomes enabled.
  244. */
  245. if (!omap_plane->enabled)
  246. return 0;
  247. ret = omap_plane_setup(omap_plane);
  248. if (ret < 0)
  249. return ret;
  250. return omap_crtc_flush(plane->crtc);
  251. }
  252. static const struct drm_plane_funcs omap_plane_funcs = {
  253. .update_plane = drm_atomic_helper_update_plane,
  254. .disable_plane = drm_atomic_helper_disable_plane,
  255. .reset = drm_atomic_helper_plane_reset,
  256. .destroy = omap_plane_destroy,
  257. .set_property = omap_plane_set_property,
  258. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  259. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  260. };
  261. static void omap_plane_error_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
  262. {
  263. struct omap_plane *omap_plane =
  264. container_of(irq, struct omap_plane, error_irq);
  265. DRM_ERROR_RATELIMITED("%s: errors: %08x\n", omap_plane->name,
  266. irqstatus);
  267. }
  268. static const char *plane_names[] = {
  269. [OMAP_DSS_GFX] = "gfx",
  270. [OMAP_DSS_VIDEO1] = "vid1",
  271. [OMAP_DSS_VIDEO2] = "vid2",
  272. [OMAP_DSS_VIDEO3] = "vid3",
  273. };
  274. static const uint32_t error_irqs[] = {
  275. [OMAP_DSS_GFX] = DISPC_IRQ_GFX_FIFO_UNDERFLOW,
  276. [OMAP_DSS_VIDEO1] = DISPC_IRQ_VID1_FIFO_UNDERFLOW,
  277. [OMAP_DSS_VIDEO2] = DISPC_IRQ_VID2_FIFO_UNDERFLOW,
  278. [OMAP_DSS_VIDEO3] = DISPC_IRQ_VID3_FIFO_UNDERFLOW,
  279. };
  280. /* initialize plane */
  281. struct drm_plane *omap_plane_init(struct drm_device *dev,
  282. int id, enum drm_plane_type type)
  283. {
  284. struct omap_drm_private *priv = dev->dev_private;
  285. struct drm_plane *plane;
  286. struct omap_plane *omap_plane;
  287. struct omap_overlay_info *info;
  288. int ret;
  289. DBG("%s: type=%d", plane_names[id], type);
  290. omap_plane = kzalloc(sizeof(*omap_plane), GFP_KERNEL);
  291. if (!omap_plane)
  292. return ERR_PTR(-ENOMEM);
  293. omap_plane->nformats = omap_framebuffer_get_formats(
  294. omap_plane->formats, ARRAY_SIZE(omap_plane->formats),
  295. dss_feat_get_supported_color_modes(id));
  296. omap_plane->id = id;
  297. omap_plane->name = plane_names[id];
  298. plane = &omap_plane->base;
  299. omap_plane->error_irq.irqmask = error_irqs[id];
  300. omap_plane->error_irq.irq = omap_plane_error_irq;
  301. omap_irq_register(dev, &omap_plane->error_irq);
  302. ret = drm_universal_plane_init(dev, plane, (1 << priv->num_crtcs) - 1,
  303. &omap_plane_funcs, omap_plane->formats,
  304. omap_plane->nformats, type);
  305. if (ret < 0)
  306. goto error;
  307. drm_plane_helper_add(plane, &omap_plane_helper_funcs);
  308. omap_plane_install_properties(plane, &plane->base);
  309. /* get our starting configuration, set defaults for parameters
  310. * we don't currently use, etc:
  311. */
  312. info = &omap_plane->info;
  313. info->rotation_type = OMAP_DSS_ROT_DMA;
  314. info->rotation = OMAP_DSS_ROT_0;
  315. info->global_alpha = 0xff;
  316. info->mirror = 0;
  317. /* Set defaults depending on whether we are a CRTC or overlay
  318. * layer.
  319. * TODO add ioctl to give userspace an API to change this.. this
  320. * will come in a subsequent patch.
  321. */
  322. if (type == DRM_PLANE_TYPE_PRIMARY)
  323. omap_plane->info.zorder = 0;
  324. else
  325. omap_plane->info.zorder = id;
  326. return plane;
  327. error:
  328. omap_irq_unregister(plane->dev, &omap_plane->error_irq);
  329. kfree(omap_plane);
  330. return NULL;
  331. }