omap_plane.c 9.8 KB

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