mdp5_plane.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Copyright (C) 2013 Red Hat
  3. * Author: Rob Clark <robdclark@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "mdp5_kms.h"
  18. struct mdp5_plane {
  19. struct drm_plane base;
  20. const char *name;
  21. enum mdp5_pipe pipe;
  22. uint32_t nformats;
  23. uint32_t formats[32];
  24. bool enabled;
  25. };
  26. #define to_mdp5_plane(x) container_of(x, struct mdp5_plane, base)
  27. static struct mdp5_kms *get_kms(struct drm_plane *plane)
  28. {
  29. struct msm_drm_private *priv = plane->dev->dev_private;
  30. return to_mdp5_kms(to_mdp_kms(priv->kms));
  31. }
  32. static int mdp5_plane_update(struct drm_plane *plane,
  33. struct drm_crtc *crtc, struct drm_framebuffer *fb,
  34. int crtc_x, int crtc_y,
  35. unsigned int crtc_w, unsigned int crtc_h,
  36. uint32_t src_x, uint32_t src_y,
  37. uint32_t src_w, uint32_t src_h)
  38. {
  39. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  40. mdp5_plane->enabled = true;
  41. if (plane->fb)
  42. drm_framebuffer_unreference(plane->fb);
  43. drm_framebuffer_reference(fb);
  44. return mdp5_plane_mode_set(plane, crtc, fb,
  45. crtc_x, crtc_y, crtc_w, crtc_h,
  46. src_x, src_y, src_w, src_h);
  47. }
  48. static int mdp5_plane_disable(struct drm_plane *plane)
  49. {
  50. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  51. struct mdp5_kms *mdp5_kms = get_kms(plane);
  52. enum mdp5_pipe pipe = mdp5_plane->pipe;
  53. int i;
  54. DBG("%s: disable", mdp5_plane->name);
  55. /* update our SMP request to zero (release all our blks): */
  56. for (i = 0; i < pipe2nclients(pipe); i++)
  57. mdp5_smp_request(mdp5_kms, pipe2client(pipe, i), 0);
  58. /* TODO detaching now will cause us not to get the last
  59. * vblank and mdp5_smp_commit().. so other planes will
  60. * still see smp blocks previously allocated to us as
  61. * in-use..
  62. */
  63. if (plane->crtc)
  64. mdp5_crtc_detach(plane->crtc, plane);
  65. return 0;
  66. }
  67. static void mdp5_plane_destroy(struct drm_plane *plane)
  68. {
  69. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  70. struct msm_drm_private *priv = plane->dev->dev_private;
  71. if (priv->kms)
  72. mdp5_plane_disable(plane);
  73. drm_plane_cleanup(plane);
  74. kfree(mdp5_plane);
  75. }
  76. /* helper to install properties which are common to planes and crtcs */
  77. void mdp5_plane_install_properties(struct drm_plane *plane,
  78. struct drm_mode_object *obj)
  79. {
  80. // XXX
  81. }
  82. int mdp5_plane_set_property(struct drm_plane *plane,
  83. struct drm_property *property, uint64_t val)
  84. {
  85. // XXX
  86. return -EINVAL;
  87. }
  88. static const struct drm_plane_funcs mdp5_plane_funcs = {
  89. .update_plane = mdp5_plane_update,
  90. .disable_plane = mdp5_plane_disable,
  91. .destroy = mdp5_plane_destroy,
  92. .set_property = mdp5_plane_set_property,
  93. };
  94. void mdp5_plane_set_scanout(struct drm_plane *plane,
  95. struct drm_framebuffer *fb)
  96. {
  97. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  98. struct mdp5_kms *mdp5_kms = get_kms(plane);
  99. enum mdp5_pipe pipe = mdp5_plane->pipe;
  100. uint32_t nplanes = drm_format_num_planes(fb->pixel_format);
  101. uint32_t iova[4];
  102. int i;
  103. for (i = 0; i < nplanes; i++) {
  104. struct drm_gem_object *bo = msm_framebuffer_bo(fb, i);
  105. msm_gem_get_iova(bo, mdp5_kms->id, &iova[i]);
  106. }
  107. for (; i < 4; i++)
  108. iova[i] = 0;
  109. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_STRIDE_A(pipe),
  110. MDP5_PIPE_SRC_STRIDE_A_P0(fb->pitches[0]) |
  111. MDP5_PIPE_SRC_STRIDE_A_P1(fb->pitches[1]));
  112. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_STRIDE_B(pipe),
  113. MDP5_PIPE_SRC_STRIDE_B_P2(fb->pitches[2]) |
  114. MDP5_PIPE_SRC_STRIDE_B_P3(fb->pitches[3]));
  115. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC0_ADDR(pipe), iova[0]);
  116. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC1_ADDR(pipe), iova[1]);
  117. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC2_ADDR(pipe), iova[2]);
  118. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC3_ADDR(pipe), iova[3]);
  119. plane->fb = fb;
  120. }
  121. /* NOTE: looks like if horizontal decimation is used (if we supported that)
  122. * then the width used to calculate SMP block requirements is the post-
  123. * decimated width. Ie. SMP buffering sits downstream of decimation (which
  124. * presumably happens during the dma from scanout buffer).
  125. */
  126. static int request_smp_blocks(struct drm_plane *plane, uint32_t format,
  127. uint32_t nplanes, uint32_t width)
  128. {
  129. struct drm_device *dev = plane->dev;
  130. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  131. struct mdp5_kms *mdp5_kms = get_kms(plane);
  132. enum mdp5_pipe pipe = mdp5_plane->pipe;
  133. int i, hsub, nlines, nblks, ret;
  134. hsub = drm_format_horz_chroma_subsampling(format);
  135. /* different if BWC (compressed framebuffer?) enabled: */
  136. nlines = 2;
  137. for (i = 0, nblks = 0; i < nplanes; i++) {
  138. int n, fetch_stride, cpp;
  139. cpp = drm_format_plane_cpp(format, i);
  140. fetch_stride = width * cpp / (i ? hsub : 1);
  141. n = DIV_ROUND_UP(fetch_stride * nlines, SMP_BLK_SIZE);
  142. /* for hw rev v1.00 */
  143. if (mdp5_kms->rev == 0)
  144. n = roundup_pow_of_two(n);
  145. DBG("%s[%d]: request %d SMP blocks", mdp5_plane->name, i, n);
  146. ret = mdp5_smp_request(mdp5_kms, pipe2client(pipe, i), n);
  147. if (ret) {
  148. dev_err(dev->dev, "Could not allocate %d SMP blocks: %d\n",
  149. n, ret);
  150. return ret;
  151. }
  152. nblks += n;
  153. }
  154. /* in success case, return total # of blocks allocated: */
  155. return nblks;
  156. }
  157. static void set_fifo_thresholds(struct drm_plane *plane, int nblks)
  158. {
  159. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  160. struct mdp5_kms *mdp5_kms = get_kms(plane);
  161. enum mdp5_pipe pipe = mdp5_plane->pipe;
  162. uint32_t val;
  163. /* 1/4 of SMP pool that is being fetched */
  164. val = (nblks * SMP_ENTRIES_PER_BLK) / 4;
  165. mdp5_write(mdp5_kms, REG_MDP5_PIPE_REQPRIO_FIFO_WM_0(pipe), val * 1);
  166. mdp5_write(mdp5_kms, REG_MDP5_PIPE_REQPRIO_FIFO_WM_1(pipe), val * 2);
  167. mdp5_write(mdp5_kms, REG_MDP5_PIPE_REQPRIO_FIFO_WM_2(pipe), val * 3);
  168. }
  169. int mdp5_plane_mode_set(struct drm_plane *plane,
  170. struct drm_crtc *crtc, struct drm_framebuffer *fb,
  171. int crtc_x, int crtc_y,
  172. unsigned int crtc_w, unsigned int crtc_h,
  173. uint32_t src_x, uint32_t src_y,
  174. uint32_t src_w, uint32_t src_h)
  175. {
  176. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  177. struct mdp5_kms *mdp5_kms = get_kms(plane);
  178. enum mdp5_pipe pipe = mdp5_plane->pipe;
  179. const struct mdp_format *format;
  180. uint32_t nplanes, config = 0;
  181. uint32_t phasex_step = 0, phasey_step = 0;
  182. uint32_t hdecm = 0, vdecm = 0;
  183. int i, nblks;
  184. nplanes = drm_format_num_planes(fb->pixel_format);
  185. /* bad formats should already be rejected: */
  186. if (WARN_ON(nplanes > pipe2nclients(pipe)))
  187. return -EINVAL;
  188. /* src values are in Q16 fixed point, convert to integer: */
  189. src_x = src_x >> 16;
  190. src_y = src_y >> 16;
  191. src_w = src_w >> 16;
  192. src_h = src_h >> 16;
  193. DBG("%s: FB[%u] %u,%u,%u,%u -> CRTC[%u] %d,%d,%u,%u", mdp5_plane->name,
  194. fb->base.id, src_x, src_y, src_w, src_h,
  195. crtc->base.id, crtc_x, crtc_y, crtc_w, crtc_h);
  196. /*
  197. * Calculate and request required # of smp blocks:
  198. */
  199. nblks = request_smp_blocks(plane, fb->pixel_format, nplanes, src_w);
  200. if (nblks < 0)
  201. return nblks;
  202. /*
  203. * Currently we update the hw for allocations/requests immediately,
  204. * but once atomic modeset/pageflip is in place, the allocation
  205. * would move into atomic->check_plane_state(), while updating the
  206. * hw would remain here:
  207. */
  208. for (i = 0; i < pipe2nclients(pipe); i++)
  209. mdp5_smp_configure(mdp5_kms, pipe2client(pipe, i));
  210. if (src_w != crtc_w) {
  211. config |= MDP5_PIPE_SCALE_CONFIG_SCALEX_EN;
  212. /* TODO calc phasex_step, hdecm */
  213. }
  214. if (src_h != crtc_h) {
  215. config |= MDP5_PIPE_SCALE_CONFIG_SCALEY_EN;
  216. /* TODO calc phasey_step, vdecm */
  217. }
  218. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_IMG_SIZE(pipe),
  219. MDP5_PIPE_SRC_IMG_SIZE_WIDTH(src_w) |
  220. MDP5_PIPE_SRC_IMG_SIZE_HEIGHT(src_h));
  221. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_SIZE(pipe),
  222. MDP5_PIPE_SRC_SIZE_WIDTH(src_w) |
  223. MDP5_PIPE_SRC_SIZE_HEIGHT(src_h));
  224. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_XY(pipe),
  225. MDP5_PIPE_SRC_XY_X(src_x) |
  226. MDP5_PIPE_SRC_XY_Y(src_y));
  227. mdp5_write(mdp5_kms, REG_MDP5_PIPE_OUT_SIZE(pipe),
  228. MDP5_PIPE_OUT_SIZE_WIDTH(crtc_w) |
  229. MDP5_PIPE_OUT_SIZE_HEIGHT(crtc_h));
  230. mdp5_write(mdp5_kms, REG_MDP5_PIPE_OUT_XY(pipe),
  231. MDP5_PIPE_OUT_XY_X(crtc_x) |
  232. MDP5_PIPE_OUT_XY_Y(crtc_y));
  233. mdp5_plane_set_scanout(plane, fb);
  234. format = to_mdp_format(msm_framebuffer_format(fb));
  235. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_FORMAT(pipe),
  236. MDP5_PIPE_SRC_FORMAT_A_BPC(format->bpc_a) |
  237. MDP5_PIPE_SRC_FORMAT_R_BPC(format->bpc_r) |
  238. MDP5_PIPE_SRC_FORMAT_G_BPC(format->bpc_g) |
  239. MDP5_PIPE_SRC_FORMAT_B_BPC(format->bpc_b) |
  240. COND(format->alpha_enable, MDP5_PIPE_SRC_FORMAT_ALPHA_ENABLE) |
  241. MDP5_PIPE_SRC_FORMAT_CPP(format->cpp - 1) |
  242. MDP5_PIPE_SRC_FORMAT_UNPACK_COUNT(format->unpack_count - 1) |
  243. COND(format->unpack_tight, MDP5_PIPE_SRC_FORMAT_UNPACK_TIGHT) |
  244. MDP5_PIPE_SRC_FORMAT_NUM_PLANES(nplanes - 1) |
  245. MDP5_PIPE_SRC_FORMAT_CHROMA_SAMP(CHROMA_RGB));
  246. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_UNPACK(pipe),
  247. MDP5_PIPE_SRC_UNPACK_ELEM0(format->unpack[0]) |
  248. MDP5_PIPE_SRC_UNPACK_ELEM1(format->unpack[1]) |
  249. MDP5_PIPE_SRC_UNPACK_ELEM2(format->unpack[2]) |
  250. MDP5_PIPE_SRC_UNPACK_ELEM3(format->unpack[3]));
  251. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_OP_MODE(pipe),
  252. MDP5_PIPE_SRC_OP_MODE_BWC(BWC_LOSSLESS));
  253. /* not using secure mode: */
  254. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_ADDR_SW_STATUS(pipe), 0);
  255. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_PHASE_STEP_X(pipe), phasex_step);
  256. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_PHASE_STEP_Y(pipe), phasey_step);
  257. mdp5_write(mdp5_kms, REG_MDP5_PIPE_DECIMATION(pipe),
  258. MDP5_PIPE_DECIMATION_VERT(vdecm) |
  259. MDP5_PIPE_DECIMATION_HORZ(hdecm));
  260. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_CONFIG(pipe),
  261. MDP5_PIPE_SCALE_CONFIG_SCALEX_MIN_FILTER(SCALE_FILTER_NEAREST) |
  262. MDP5_PIPE_SCALE_CONFIG_SCALEY_MIN_FILTER(SCALE_FILTER_NEAREST) |
  263. MDP5_PIPE_SCALE_CONFIG_SCALEX_CR_FILTER(SCALE_FILTER_NEAREST) |
  264. MDP5_PIPE_SCALE_CONFIG_SCALEY_CR_FILTER(SCALE_FILTER_NEAREST) |
  265. MDP5_PIPE_SCALE_CONFIG_SCALEX_MAX_FILTER(SCALE_FILTER_NEAREST) |
  266. MDP5_PIPE_SCALE_CONFIG_SCALEY_MAX_FILTER(SCALE_FILTER_NEAREST));
  267. set_fifo_thresholds(plane, nblks);
  268. /* TODO detach from old crtc (if we had more than one) */
  269. mdp5_crtc_attach(crtc, plane);
  270. return 0;
  271. }
  272. void mdp5_plane_complete_flip(struct drm_plane *plane)
  273. {
  274. struct mdp5_kms *mdp5_kms = get_kms(plane);
  275. enum mdp5_pipe pipe = to_mdp5_plane(plane)->pipe;
  276. int i;
  277. for (i = 0; i < pipe2nclients(pipe); i++)
  278. mdp5_smp_commit(mdp5_kms, pipe2client(pipe, i));
  279. }
  280. enum mdp5_pipe mdp5_plane_pipe(struct drm_plane *plane)
  281. {
  282. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  283. return mdp5_plane->pipe;
  284. }
  285. /* initialize plane */
  286. struct drm_plane *mdp5_plane_init(struct drm_device *dev,
  287. enum mdp5_pipe pipe, bool private_plane)
  288. {
  289. struct drm_plane *plane = NULL;
  290. struct mdp5_plane *mdp5_plane;
  291. int ret;
  292. enum drm_plane_type type;
  293. mdp5_plane = kzalloc(sizeof(*mdp5_plane), GFP_KERNEL);
  294. if (!mdp5_plane) {
  295. ret = -ENOMEM;
  296. goto fail;
  297. }
  298. plane = &mdp5_plane->base;
  299. mdp5_plane->pipe = pipe;
  300. mdp5_plane->name = pipe2name(pipe);
  301. mdp5_plane->nformats = mdp5_get_formats(pipe, mdp5_plane->formats,
  302. ARRAY_SIZE(mdp5_plane->formats));
  303. type = private_plane ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
  304. drm_universal_plane_init(dev, plane, 0xff, &mdp5_plane_funcs,
  305. mdp5_plane->formats, mdp5_plane->nformats,
  306. type);
  307. mdp5_plane_install_properties(plane, &plane->base);
  308. return plane;
  309. fail:
  310. if (plane)
  311. mdp5_plane_destroy(plane);
  312. return ERR_PTR(ret);
  313. }