omap_plane.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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_flip_work.h"
  20. #include "omap_drv.h"
  21. #include "omap_dmm_tiler.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. struct callback {
  31. void (*fxn)(void *);
  32. void *arg;
  33. };
  34. #define to_omap_plane(x) container_of(x, struct omap_plane, base)
  35. struct omap_plane {
  36. struct drm_plane base;
  37. int id; /* TODO rename omap_plane -> omap_plane_id in omapdss so I can use the enum */
  38. const char *name;
  39. struct omap_overlay_info info;
  40. struct omap_drm_apply apply;
  41. /* position/orientation of scanout within the fb: */
  42. struct omap_drm_window win;
  43. bool enabled;
  44. /* last fb that we pinned: */
  45. struct drm_framebuffer *pinned_fb;
  46. uint32_t nformats;
  47. uint32_t formats[32];
  48. struct omap_drm_irq error_irq;
  49. /* for deferring bo unpin's until next post_apply(): */
  50. struct drm_flip_work unpin_work;
  51. // XXX maybe get rid of this and handle vblank in crtc too?
  52. struct callback apply_done_cb;
  53. };
  54. static void omap_plane_unpin_worker(struct drm_flip_work *work, void *val)
  55. {
  56. struct omap_plane *omap_plane =
  57. container_of(work, struct omap_plane, unpin_work);
  58. struct drm_device *dev = omap_plane->base.dev;
  59. /*
  60. * omap_framebuffer_pin/unpin are always called from priv->wq,
  61. * so there's no need for locking here.
  62. */
  63. omap_framebuffer_unpin(val);
  64. mutex_lock(&dev->mode_config.mutex);
  65. drm_framebuffer_unreference(val);
  66. mutex_unlock(&dev->mode_config.mutex);
  67. }
  68. /* update which fb (if any) is pinned for scanout */
  69. static int omap_plane_update_pin(struct drm_plane *plane,
  70. struct drm_framebuffer *fb)
  71. {
  72. struct omap_plane *omap_plane = to_omap_plane(plane);
  73. struct drm_framebuffer *pinned_fb = omap_plane->pinned_fb;
  74. if (pinned_fb != fb) {
  75. int ret = 0;
  76. DBG("%p -> %p", pinned_fb, fb);
  77. if (fb) {
  78. drm_framebuffer_reference(fb);
  79. ret = omap_framebuffer_pin(fb);
  80. }
  81. if (pinned_fb)
  82. drm_flip_work_queue(&omap_plane->unpin_work, pinned_fb);
  83. if (ret) {
  84. dev_err(plane->dev->dev, "could not swap %p -> %p\n",
  85. omap_plane->pinned_fb, fb);
  86. drm_framebuffer_unreference(fb);
  87. omap_plane->pinned_fb = NULL;
  88. return ret;
  89. }
  90. omap_plane->pinned_fb = fb;
  91. }
  92. return 0;
  93. }
  94. static void omap_plane_pre_apply(struct omap_drm_apply *apply)
  95. {
  96. struct omap_plane *omap_plane =
  97. container_of(apply, struct omap_plane, apply);
  98. struct omap_drm_window *win = &omap_plane->win;
  99. struct drm_plane *plane = &omap_plane->base;
  100. struct drm_device *dev = plane->dev;
  101. struct omap_overlay_info *info = &omap_plane->info;
  102. struct drm_crtc *crtc = plane->crtc;
  103. enum omap_channel channel;
  104. bool enabled = omap_plane->enabled && crtc;
  105. int ret;
  106. DBG("%s, enabled=%d", omap_plane->name, enabled);
  107. /* if fb has changed, pin new fb: */
  108. omap_plane_update_pin(plane, enabled ? plane->fb : NULL);
  109. if (!enabled) {
  110. dispc_ovl_enable(omap_plane->id, false);
  111. return;
  112. }
  113. channel = omap_crtc_channel(crtc);
  114. /* update scanout: */
  115. omap_framebuffer_update_scanout(plane->fb, win, info);
  116. DBG("%dx%d -> %dx%d (%d)", info->width, info->height,
  117. info->out_width, info->out_height,
  118. info->screen_width);
  119. DBG("%d,%d %pad %pad", info->pos_x, info->pos_y,
  120. &info->paddr, &info->p_uv_addr);
  121. dispc_ovl_set_channel_out(omap_plane->id, channel);
  122. /* and finally, update omapdss: */
  123. ret = dispc_ovl_setup(omap_plane->id, info, false,
  124. omap_crtc_timings(crtc), false);
  125. if (ret) {
  126. dev_err(dev->dev, "dispc_ovl_setup failed: %d\n", ret);
  127. return;
  128. }
  129. dispc_ovl_enable(omap_plane->id, true);
  130. }
  131. static void omap_plane_post_apply(struct omap_drm_apply *apply)
  132. {
  133. struct omap_plane *omap_plane =
  134. container_of(apply, struct omap_plane, apply);
  135. struct drm_plane *plane = &omap_plane->base;
  136. struct omap_drm_private *priv = plane->dev->dev_private;
  137. struct callback cb;
  138. cb = omap_plane->apply_done_cb;
  139. omap_plane->apply_done_cb.fxn = NULL;
  140. drm_flip_work_commit(&omap_plane->unpin_work, priv->wq);
  141. if (cb.fxn)
  142. cb.fxn(cb.arg);
  143. }
  144. static int omap_plane_apply(struct drm_plane *plane)
  145. {
  146. if (plane->crtc) {
  147. struct omap_plane *omap_plane = to_omap_plane(plane);
  148. return omap_crtc_apply(plane->crtc, &omap_plane->apply);
  149. }
  150. return 0;
  151. }
  152. int omap_plane_mode_set(struct drm_plane *plane,
  153. struct drm_crtc *crtc, struct drm_framebuffer *fb,
  154. int crtc_x, int crtc_y,
  155. unsigned int crtc_w, unsigned int crtc_h,
  156. unsigned int src_x, unsigned int src_y,
  157. unsigned int src_w, unsigned int src_h,
  158. void (*fxn)(void *), void *arg)
  159. {
  160. struct omap_plane *omap_plane = to_omap_plane(plane);
  161. struct omap_drm_window *win = &omap_plane->win;
  162. win->crtc_x = crtc_x;
  163. win->crtc_y = crtc_y;
  164. win->crtc_w = crtc_w;
  165. win->crtc_h = crtc_h;
  166. win->src_x = src_x;
  167. win->src_y = src_y;
  168. win->src_w = src_w;
  169. win->src_h = src_h;
  170. if (fxn) {
  171. /* omap_crtc should ensure that a new page flip
  172. * isn't permitted while there is one pending:
  173. */
  174. BUG_ON(omap_plane->apply_done_cb.fxn);
  175. omap_plane->apply_done_cb.fxn = fxn;
  176. omap_plane->apply_done_cb.arg = arg;
  177. }
  178. return omap_plane_apply(plane);
  179. }
  180. static int omap_plane_update(struct drm_plane *plane,
  181. struct drm_crtc *crtc, struct drm_framebuffer *fb,
  182. int crtc_x, int crtc_y,
  183. unsigned int crtc_w, unsigned int crtc_h,
  184. uint32_t src_x, uint32_t src_y,
  185. uint32_t src_w, uint32_t src_h)
  186. {
  187. struct omap_plane *omap_plane = to_omap_plane(plane);
  188. omap_plane->enabled = true;
  189. /* omap_plane_mode_set() takes adjusted src */
  190. switch (omap_plane->win.rotation & 0xf) {
  191. case BIT(DRM_ROTATE_90):
  192. case BIT(DRM_ROTATE_270):
  193. swap(src_w, src_h);
  194. break;
  195. }
  196. /*
  197. * We don't need to take a reference to the framebuffer as the DRM core
  198. * has already done so for the purpose of setting plane->fb.
  199. */
  200. plane->fb = fb;
  201. plane->crtc = crtc;
  202. /* src values are in Q16 fixed point, convert to integer: */
  203. return omap_plane_mode_set(plane, crtc, fb,
  204. crtc_x, crtc_y, crtc_w, crtc_h,
  205. src_x >> 16, src_y >> 16, src_w >> 16, src_h >> 16,
  206. NULL, NULL);
  207. }
  208. static int omap_plane_disable(struct drm_plane *plane)
  209. {
  210. struct omap_plane *omap_plane = to_omap_plane(plane);
  211. omap_plane->win.rotation = BIT(DRM_ROTATE_0);
  212. omap_plane->info.zorder = plane->type == DRM_PLANE_TYPE_PRIMARY
  213. ? 0 : omap_plane->id;
  214. return omap_plane_set_enable(plane, false);
  215. }
  216. static void omap_plane_destroy(struct drm_plane *plane)
  217. {
  218. struct omap_plane *omap_plane = to_omap_plane(plane);
  219. DBG("%s", omap_plane->name);
  220. omap_irq_unregister(plane->dev, &omap_plane->error_irq);
  221. drm_plane_cleanup(plane);
  222. drm_flip_work_cleanup(&omap_plane->unpin_work);
  223. kfree(omap_plane);
  224. }
  225. int omap_plane_set_enable(struct drm_plane *plane, bool enable)
  226. {
  227. struct omap_plane *omap_plane = to_omap_plane(plane);
  228. if (enable == omap_plane->enabled)
  229. return 0;
  230. omap_plane->enabled = enable;
  231. return omap_plane_apply(plane);
  232. }
  233. /* helper to install properties which are common to planes and crtcs */
  234. void omap_plane_install_properties(struct drm_plane *plane,
  235. struct drm_mode_object *obj)
  236. {
  237. struct drm_device *dev = plane->dev;
  238. struct omap_drm_private *priv = dev->dev_private;
  239. struct drm_property *prop;
  240. if (priv->has_dmm) {
  241. prop = priv->rotation_prop;
  242. if (!prop) {
  243. prop = drm_mode_create_rotation_property(dev,
  244. BIT(DRM_ROTATE_0) |
  245. BIT(DRM_ROTATE_90) |
  246. BIT(DRM_ROTATE_180) |
  247. BIT(DRM_ROTATE_270) |
  248. BIT(DRM_REFLECT_X) |
  249. BIT(DRM_REFLECT_Y));
  250. if (prop == NULL)
  251. return;
  252. priv->rotation_prop = prop;
  253. }
  254. drm_object_attach_property(obj, prop, 0);
  255. }
  256. prop = priv->zorder_prop;
  257. if (!prop) {
  258. prop = drm_property_create_range(dev, 0, "zorder", 0, 3);
  259. if (prop == NULL)
  260. return;
  261. priv->zorder_prop = prop;
  262. }
  263. drm_object_attach_property(obj, prop, 0);
  264. }
  265. int omap_plane_set_property(struct drm_plane *plane,
  266. struct drm_property *property, uint64_t val)
  267. {
  268. struct omap_plane *omap_plane = to_omap_plane(plane);
  269. struct omap_drm_private *priv = plane->dev->dev_private;
  270. int ret = -EINVAL;
  271. if (property == priv->rotation_prop) {
  272. DBG("%s: rotation: %02x", omap_plane->name, (uint32_t)val);
  273. omap_plane->win.rotation = val;
  274. ret = omap_plane_apply(plane);
  275. } else if (property == priv->zorder_prop) {
  276. DBG("%s: zorder: %02x", omap_plane->name, (uint32_t)val);
  277. omap_plane->info.zorder = val;
  278. ret = omap_plane_apply(plane);
  279. }
  280. return ret;
  281. }
  282. static const struct drm_plane_funcs omap_plane_funcs = {
  283. .update_plane = omap_plane_update,
  284. .disable_plane = omap_plane_disable,
  285. .destroy = omap_plane_destroy,
  286. .set_property = omap_plane_set_property,
  287. };
  288. static void omap_plane_error_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
  289. {
  290. struct omap_plane *omap_plane =
  291. container_of(irq, struct omap_plane, error_irq);
  292. DRM_ERROR_RATELIMITED("%s: errors: %08x\n", omap_plane->name,
  293. irqstatus);
  294. }
  295. static const char *plane_names[] = {
  296. [OMAP_DSS_GFX] = "gfx",
  297. [OMAP_DSS_VIDEO1] = "vid1",
  298. [OMAP_DSS_VIDEO2] = "vid2",
  299. [OMAP_DSS_VIDEO3] = "vid3",
  300. };
  301. static const uint32_t error_irqs[] = {
  302. [OMAP_DSS_GFX] = DISPC_IRQ_GFX_FIFO_UNDERFLOW,
  303. [OMAP_DSS_VIDEO1] = DISPC_IRQ_VID1_FIFO_UNDERFLOW,
  304. [OMAP_DSS_VIDEO2] = DISPC_IRQ_VID2_FIFO_UNDERFLOW,
  305. [OMAP_DSS_VIDEO3] = DISPC_IRQ_VID3_FIFO_UNDERFLOW,
  306. };
  307. /* initialize plane */
  308. struct drm_plane *omap_plane_init(struct drm_device *dev,
  309. int id, enum drm_plane_type type)
  310. {
  311. struct omap_drm_private *priv = dev->dev_private;
  312. struct drm_plane *plane;
  313. struct omap_plane *omap_plane;
  314. struct omap_overlay_info *info;
  315. int ret;
  316. DBG("%s: type=%d", plane_names[id], type);
  317. omap_plane = kzalloc(sizeof(*omap_plane), GFP_KERNEL);
  318. if (!omap_plane)
  319. return ERR_PTR(-ENOMEM);
  320. drm_flip_work_init(&omap_plane->unpin_work,
  321. "unpin", omap_plane_unpin_worker);
  322. omap_plane->nformats = omap_framebuffer_get_formats(
  323. omap_plane->formats, ARRAY_SIZE(omap_plane->formats),
  324. dss_feat_get_supported_color_modes(id));
  325. omap_plane->id = id;
  326. omap_plane->name = plane_names[id];
  327. plane = &omap_plane->base;
  328. omap_plane->apply.pre_apply = omap_plane_pre_apply;
  329. omap_plane->apply.post_apply = omap_plane_post_apply;
  330. omap_plane->error_irq.irqmask = error_irqs[id];
  331. omap_plane->error_irq.irq = omap_plane_error_irq;
  332. omap_irq_register(dev, &omap_plane->error_irq);
  333. ret = drm_universal_plane_init(dev, plane, (1 << priv->num_crtcs) - 1,
  334. &omap_plane_funcs, omap_plane->formats,
  335. omap_plane->nformats, type);
  336. if (ret < 0)
  337. goto error;
  338. omap_plane_install_properties(plane, &plane->base);
  339. /* get our starting configuration, set defaults for parameters
  340. * we don't currently use, etc:
  341. */
  342. info = &omap_plane->info;
  343. info->rotation_type = OMAP_DSS_ROT_DMA;
  344. info->rotation = OMAP_DSS_ROT_0;
  345. info->global_alpha = 0xff;
  346. info->mirror = 0;
  347. /* Set defaults depending on whether we are a CRTC or overlay
  348. * layer.
  349. * TODO add ioctl to give userspace an API to change this.. this
  350. * will come in a subsequent patch.
  351. */
  352. if (type == DRM_PLANE_TYPE_PRIMARY)
  353. omap_plane->info.zorder = 0;
  354. else
  355. omap_plane->info.zorder = id;
  356. return plane;
  357. error:
  358. omap_irq_unregister(plane->dev, &omap_plane->error_irq);
  359. kfree(omap_plane);
  360. return NULL;
  361. }