/drivers/gpu/drm/omapdrm/omap_plane.c

https://gitlab.com/tbwtiot/kernel_source · C · 452 lines · 318 code · 86 blank · 48 comment · 32 complexity · 6c90323b685f15d03d62267eddabcedd MD5 · raw file

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