omap_plane.c 9.6 KB

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