omap_plane.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 <linux/kfifo.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. /* set of bo's pending unpin until next post_apply() */
  50. DECLARE_KFIFO_PTR(unpin_fifo, struct drm_gem_object *);
  51. // XXX maybe get rid of this and handle vblank in crtc too?
  52. struct callback apply_done_cb;
  53. };
  54. static void unpin(void *arg, struct drm_gem_object *bo)
  55. {
  56. struct drm_plane *plane = arg;
  57. struct omap_plane *omap_plane = to_omap_plane(plane);
  58. if (kfifo_put(&omap_plane->unpin_fifo,
  59. (const struct drm_gem_object **)&bo)) {
  60. /* also hold a ref so it isn't free'd while pinned */
  61. drm_gem_object_reference(bo);
  62. } else {
  63. dev_err(plane->dev->dev, "unpin fifo full!\n");
  64. omap_gem_put_paddr(bo);
  65. }
  66. }
  67. /* update which fb (if any) is pinned for scanout */
  68. static int update_pin(struct drm_plane *plane, struct drm_framebuffer *fb)
  69. {
  70. struct omap_plane *omap_plane = to_omap_plane(plane);
  71. struct drm_framebuffer *pinned_fb = omap_plane->pinned_fb;
  72. if (pinned_fb != fb) {
  73. int ret;
  74. DBG("%p -> %p", pinned_fb, fb);
  75. if (fb)
  76. drm_framebuffer_reference(fb);
  77. ret = omap_framebuffer_replace(pinned_fb, fb, plane, unpin);
  78. if (pinned_fb)
  79. drm_framebuffer_unreference(pinned_fb);
  80. if (ret) {
  81. dev_err(plane->dev->dev, "could not swap %p -> %p\n",
  82. omap_plane->pinned_fb, fb);
  83. if (fb)
  84. drm_framebuffer_unreference(fb);
  85. omap_plane->pinned_fb = NULL;
  86. return ret;
  87. }
  88. omap_plane->pinned_fb = fb;
  89. }
  90. return 0;
  91. }
  92. static void omap_plane_pre_apply(struct omap_drm_apply *apply)
  93. {
  94. struct omap_plane *omap_plane =
  95. container_of(apply, struct omap_plane, apply);
  96. struct omap_drm_window *win = &omap_plane->win;
  97. struct drm_plane *plane = &omap_plane->base;
  98. struct drm_device *dev = plane->dev;
  99. struct omap_overlay_info *info = &omap_plane->info;
  100. struct drm_crtc *crtc = plane->crtc;
  101. enum omap_channel channel;
  102. bool enabled = omap_plane->enabled && crtc;
  103. bool ilace, replication;
  104. int ret;
  105. DBG("%s, enabled=%d", omap_plane->name, enabled);
  106. /* if fb has changed, pin new fb: */
  107. update_pin(plane, enabled ? plane->fb : NULL);
  108. if (!enabled) {
  109. dispc_ovl_enable(omap_plane->id, false);
  110. return;
  111. }
  112. channel = omap_crtc_channel(crtc);
  113. /* update scanout: */
  114. omap_framebuffer_update_scanout(plane->fb, win, info);
  115. DBG("%dx%d -> %dx%d (%d)", info->width, info->height,
  116. info->out_width, info->out_height,
  117. info->screen_width);
  118. DBG("%d,%d %08x %08x", info->pos_x, info->pos_y,
  119. info->paddr, info->p_uv_addr);
  120. /* TODO: */
  121. ilace = false;
  122. replication = false;
  123. /* and finally, update omapdss: */
  124. ret = dispc_ovl_setup(omap_plane->id, info,
  125. replication, omap_crtc_timings(crtc), false);
  126. if (ret) {
  127. dev_err(dev->dev, "dispc_ovl_setup failed: %d\n", ret);
  128. return;
  129. }
  130. dispc_ovl_enable(omap_plane->id, true);
  131. dispc_ovl_set_channel_out(omap_plane->id, channel);
  132. }
  133. static void omap_plane_post_apply(struct omap_drm_apply *apply)
  134. {
  135. struct omap_plane *omap_plane =
  136. container_of(apply, struct omap_plane, apply);
  137. struct drm_plane *plane = &omap_plane->base;
  138. struct omap_overlay_info *info = &omap_plane->info;
  139. struct drm_gem_object *bo = NULL;
  140. struct callback cb;
  141. cb = omap_plane->apply_done_cb;
  142. omap_plane->apply_done_cb.fxn = NULL;
  143. while (kfifo_get(&omap_plane->unpin_fifo, &bo)) {
  144. omap_gem_put_paddr(bo);
  145. drm_gem_object_unreference_unlocked(bo);
  146. }
  147. if (cb.fxn)
  148. cb.fxn(cb.arg);
  149. if (omap_plane->enabled) {
  150. omap_framebuffer_flush(plane->fb, info->pos_x, info->pos_y,
  151. info->out_width, info->out_height);
  152. }
  153. }
  154. static int apply(struct drm_plane *plane)
  155. {
  156. if (plane->crtc) {
  157. struct omap_plane *omap_plane = to_omap_plane(plane);
  158. return omap_crtc_apply(plane->crtc, &omap_plane->apply);
  159. }
  160. return 0;
  161. }
  162. int omap_plane_mode_set(struct drm_plane *plane,
  163. struct drm_crtc *crtc, struct drm_framebuffer *fb,
  164. int crtc_x, int crtc_y,
  165. unsigned int crtc_w, unsigned int crtc_h,
  166. uint32_t src_x, uint32_t src_y,
  167. uint32_t src_w, uint32_t src_h,
  168. void (*fxn)(void *), void *arg)
  169. {
  170. struct omap_plane *omap_plane = to_omap_plane(plane);
  171. struct omap_drm_window *win = &omap_plane->win;
  172. win->crtc_x = crtc_x;
  173. win->crtc_y = crtc_y;
  174. win->crtc_w = crtc_w;
  175. win->crtc_h = crtc_h;
  176. /* src values are in Q16 fixed point, convert to integer: */
  177. win->src_x = src_x >> 16;
  178. win->src_y = src_y >> 16;
  179. win->src_w = src_w >> 16;
  180. win->src_h = src_h >> 16;
  181. if (fxn) {
  182. /* omap_crtc should ensure that a new page flip
  183. * isn't permitted while there is one pending:
  184. */
  185. BUG_ON(omap_plane->apply_done_cb.fxn);
  186. omap_plane->apply_done_cb.fxn = fxn;
  187. omap_plane->apply_done_cb.arg = arg;
  188. }
  189. plane->fb = fb;
  190. plane->crtc = crtc;
  191. return apply(plane);
  192. }
  193. static int omap_plane_update(struct drm_plane *plane,
  194. struct drm_crtc *crtc, struct drm_framebuffer *fb,
  195. int crtc_x, int crtc_y,
  196. unsigned int crtc_w, unsigned int crtc_h,
  197. uint32_t src_x, uint32_t src_y,
  198. uint32_t src_w, uint32_t src_h)
  199. {
  200. struct omap_plane *omap_plane = to_omap_plane(plane);
  201. omap_plane->enabled = true;
  202. if (plane->fb)
  203. drm_framebuffer_unreference(plane->fb);
  204. drm_framebuffer_reference(fb);
  205. return omap_plane_mode_set(plane, crtc, fb,
  206. crtc_x, crtc_y, crtc_w, crtc_h,
  207. src_x, src_y, src_w, src_h,
  208. NULL, NULL);
  209. }
  210. static int omap_plane_disable(struct drm_plane *plane)
  211. {
  212. struct omap_plane *omap_plane = to_omap_plane(plane);
  213. omap_plane->win.rotation = BIT(DRM_ROTATE_0);
  214. return omap_plane_dpms(plane, DRM_MODE_DPMS_OFF);
  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. omap_plane_disable(plane);
  222. drm_plane_cleanup(plane);
  223. WARN_ON(!kfifo_is_empty(&omap_plane->unpin_fifo));
  224. kfifo_free(&omap_plane->unpin_fifo);
  225. kfree(omap_plane);
  226. }
  227. int omap_plane_dpms(struct drm_plane *plane, int mode)
  228. {
  229. struct omap_plane *omap_plane = to_omap_plane(plane);
  230. bool enabled = (mode == DRM_MODE_DPMS_ON);
  231. int ret = 0;
  232. if (enabled != omap_plane->enabled) {
  233. omap_plane->enabled = enabled;
  234. ret = apply(plane);
  235. }
  236. return ret;
  237. }
  238. /* helper to install properties which are common to planes and crtcs */
  239. void omap_plane_install_properties(struct drm_plane *plane,
  240. struct drm_mode_object *obj)
  241. {
  242. struct drm_device *dev = plane->dev;
  243. struct omap_drm_private *priv = dev->dev_private;
  244. struct drm_property *prop;
  245. if (priv->has_dmm) {
  246. prop = priv->rotation_prop;
  247. if (!prop) {
  248. const struct drm_prop_enum_list props[] = {
  249. { DRM_ROTATE_0, "rotate-0" },
  250. { DRM_ROTATE_90, "rotate-90" },
  251. { DRM_ROTATE_180, "rotate-180" },
  252. { DRM_ROTATE_270, "rotate-270" },
  253. { DRM_REFLECT_X, "reflect-x" },
  254. { DRM_REFLECT_Y, "reflect-y" },
  255. };
  256. prop = drm_property_create_bitmask(dev, 0, "rotation",
  257. props, ARRAY_SIZE(props));
  258. if (prop == NULL)
  259. return;
  260. priv->rotation_prop = prop;
  261. }
  262. drm_object_attach_property(obj, prop, 0);
  263. }
  264. prop = priv->zorder_prop;
  265. if (!prop) {
  266. prop = drm_property_create_range(dev, 0, "zorder", 0, 3);
  267. if (prop == NULL)
  268. return;
  269. priv->zorder_prop = prop;
  270. }
  271. drm_object_attach_property(obj, prop, 0);
  272. }
  273. int omap_plane_set_property(struct drm_plane *plane,
  274. struct drm_property *property, uint64_t val)
  275. {
  276. struct omap_plane *omap_plane = to_omap_plane(plane);
  277. struct omap_drm_private *priv = plane->dev->dev_private;
  278. int ret = -EINVAL;
  279. if (property == priv->rotation_prop) {
  280. DBG("%s: rotation: %02x", omap_plane->name, (uint32_t)val);
  281. omap_plane->win.rotation = val;
  282. ret = apply(plane);
  283. } else if (property == priv->zorder_prop) {
  284. DBG("%s: zorder: %02x", omap_plane->name, (uint32_t)val);
  285. omap_plane->info.zorder = val;
  286. ret = apply(plane);
  287. }
  288. return ret;
  289. }
  290. static const struct drm_plane_funcs omap_plane_funcs = {
  291. .update_plane = omap_plane_update,
  292. .disable_plane = omap_plane_disable,
  293. .destroy = omap_plane_destroy,
  294. .set_property = omap_plane_set_property,
  295. };
  296. static void omap_plane_error_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
  297. {
  298. struct omap_plane *omap_plane =
  299. container_of(irq, struct omap_plane, error_irq);
  300. DRM_ERROR("%s: errors: %08x\n", omap_plane->name, irqstatus);
  301. }
  302. static const char *plane_names[] = {
  303. [OMAP_DSS_GFX] = "gfx",
  304. [OMAP_DSS_VIDEO1] = "vid1",
  305. [OMAP_DSS_VIDEO2] = "vid2",
  306. [OMAP_DSS_VIDEO3] = "vid3",
  307. };
  308. static const uint32_t error_irqs[] = {
  309. [OMAP_DSS_GFX] = DISPC_IRQ_GFX_FIFO_UNDERFLOW,
  310. [OMAP_DSS_VIDEO1] = DISPC_IRQ_VID1_FIFO_UNDERFLOW,
  311. [OMAP_DSS_VIDEO2] = DISPC_IRQ_VID2_FIFO_UNDERFLOW,
  312. [OMAP_DSS_VIDEO3] = DISPC_IRQ_VID3_FIFO_UNDERFLOW,
  313. };
  314. /* initialize plane */
  315. struct drm_plane *omap_plane_init(struct drm_device *dev,
  316. int id, bool private_plane)
  317. {
  318. struct omap_drm_private *priv = dev->dev_private;
  319. struct drm_plane *plane = NULL;
  320. struct omap_plane *omap_plane;
  321. struct omap_overlay_info *info;
  322. int ret;
  323. DBG("%s: priv=%d", plane_names[id], private_plane);
  324. omap_plane = kzalloc(sizeof(*omap_plane), GFP_KERNEL);
  325. if (!omap_plane)
  326. goto fail;
  327. ret = kfifo_alloc(&omap_plane->unpin_fifo, 16, GFP_KERNEL);
  328. if (ret) {
  329. dev_err(dev->dev, "could not allocate unpin FIFO\n");
  330. goto fail;
  331. }
  332. omap_plane->nformats = omap_framebuffer_get_formats(
  333. omap_plane->formats, ARRAY_SIZE(omap_plane->formats),
  334. dss_feat_get_supported_color_modes(id));
  335. omap_plane->id = id;
  336. omap_plane->name = plane_names[id];
  337. plane = &omap_plane->base;
  338. omap_plane->apply.pre_apply = omap_plane_pre_apply;
  339. omap_plane->apply.post_apply = omap_plane_post_apply;
  340. omap_plane->error_irq.irqmask = error_irqs[id];
  341. omap_plane->error_irq.irq = omap_plane_error_irq;
  342. omap_irq_register(dev, &omap_plane->error_irq);
  343. drm_plane_init(dev, plane, (1 << priv->num_crtcs) - 1, &omap_plane_funcs,
  344. omap_plane->formats, omap_plane->nformats, private_plane);
  345. omap_plane_install_properties(plane, &plane->base);
  346. /* get our starting configuration, set defaults for parameters
  347. * we don't currently use, etc:
  348. */
  349. info = &omap_plane->info;
  350. info->rotation_type = OMAP_DSS_ROT_DMA;
  351. info->rotation = OMAP_DSS_ROT_0;
  352. info->global_alpha = 0xff;
  353. info->mirror = 0;
  354. /* Set defaults depending on whether we are a CRTC or overlay
  355. * layer.
  356. * TODO add ioctl to give userspace an API to change this.. this
  357. * will come in a subsequent patch.
  358. */
  359. if (private_plane)
  360. omap_plane->info.zorder = 0;
  361. else
  362. omap_plane->info.zorder = id;
  363. return plane;
  364. fail:
  365. if (plane)
  366. omap_plane_destroy(plane);
  367. return NULL;
  368. }