mdp5_plane.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * Copyright (c) 2014 The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2013 Red Hat
  4. * Author: Rob Clark <robdclark@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "mdp5_kms.h"
  19. #define MAX_PLANE 4
  20. struct mdp5_plane {
  21. struct drm_plane base;
  22. const char *name;
  23. enum mdp5_pipe pipe;
  24. spinlock_t pipe_lock; /* protect REG_MDP5_PIPE_* registers */
  25. uint32_t reg_offset;
  26. uint32_t flush_mask; /* used to commit pipe registers */
  27. uint32_t nformats;
  28. uint32_t formats[32];
  29. bool enabled;
  30. };
  31. #define to_mdp5_plane(x) container_of(x, struct mdp5_plane, base)
  32. static int mdp5_plane_mode_set(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. static void set_scanout_locked(struct drm_plane *plane,
  39. struct drm_framebuffer *fb);
  40. static struct mdp5_kms *get_kms(struct drm_plane *plane)
  41. {
  42. struct msm_drm_private *priv = plane->dev->dev_private;
  43. return to_mdp5_kms(to_mdp_kms(priv->kms));
  44. }
  45. static bool plane_enabled(struct drm_plane_state *state)
  46. {
  47. return state->fb && state->crtc;
  48. }
  49. static int mdp5_plane_disable(struct drm_plane *plane)
  50. {
  51. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  52. struct mdp5_kms *mdp5_kms = get_kms(plane);
  53. enum mdp5_pipe pipe = mdp5_plane->pipe;
  54. DBG("%s: disable", mdp5_plane->name);
  55. if (mdp5_kms) {
  56. /* Release the memory we requested earlier from the SMP: */
  57. mdp5_smp_release(mdp5_kms->smp, pipe);
  58. }
  59. return 0;
  60. }
  61. static void mdp5_plane_destroy(struct drm_plane *plane)
  62. {
  63. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  64. drm_plane_helper_disable(plane);
  65. drm_plane_cleanup(plane);
  66. kfree(mdp5_plane);
  67. }
  68. /* helper to install properties which are common to planes and crtcs */
  69. void mdp5_plane_install_properties(struct drm_plane *plane,
  70. struct drm_mode_object *obj)
  71. {
  72. // XXX
  73. }
  74. int mdp5_plane_set_property(struct drm_plane *plane,
  75. struct drm_property *property, uint64_t val)
  76. {
  77. // XXX
  78. return -EINVAL;
  79. }
  80. static void mdp5_plane_reset(struct drm_plane *plane)
  81. {
  82. struct mdp5_plane_state *mdp5_state;
  83. if (plane->state && plane->state->fb)
  84. drm_framebuffer_unreference(plane->state->fb);
  85. kfree(to_mdp5_plane_state(plane->state));
  86. mdp5_state = kzalloc(sizeof(*mdp5_state), GFP_KERNEL);
  87. if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
  88. mdp5_state->zpos = 0;
  89. } else {
  90. mdp5_state->zpos = 1 + drm_plane_index(plane);
  91. }
  92. plane->state = &mdp5_state->base;
  93. }
  94. static struct drm_plane_state *
  95. mdp5_plane_duplicate_state(struct drm_plane *plane)
  96. {
  97. struct mdp5_plane_state *mdp5_state;
  98. if (WARN_ON(!plane->state))
  99. return NULL;
  100. mdp5_state = kmemdup(to_mdp5_plane_state(plane->state),
  101. sizeof(*mdp5_state), GFP_KERNEL);
  102. if (mdp5_state && mdp5_state->base.fb)
  103. drm_framebuffer_reference(mdp5_state->base.fb);
  104. mdp5_state->mode_changed = false;
  105. mdp5_state->pending = false;
  106. return &mdp5_state->base;
  107. }
  108. static void mdp5_plane_destroy_state(struct drm_plane *plane,
  109. struct drm_plane_state *state)
  110. {
  111. if (state->fb)
  112. drm_framebuffer_unreference(state->fb);
  113. kfree(to_mdp5_plane_state(state));
  114. }
  115. static const struct drm_plane_funcs mdp5_plane_funcs = {
  116. .update_plane = drm_atomic_helper_update_plane,
  117. .disable_plane = drm_atomic_helper_disable_plane,
  118. .destroy = mdp5_plane_destroy,
  119. .set_property = mdp5_plane_set_property,
  120. .reset = mdp5_plane_reset,
  121. .atomic_duplicate_state = mdp5_plane_duplicate_state,
  122. .atomic_destroy_state = mdp5_plane_destroy_state,
  123. };
  124. static int mdp5_plane_prepare_fb(struct drm_plane *plane,
  125. struct drm_framebuffer *fb)
  126. {
  127. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  128. struct mdp5_kms *mdp5_kms = get_kms(plane);
  129. DBG("%s: prepare: FB[%u]", mdp5_plane->name, fb->base.id);
  130. return msm_framebuffer_prepare(fb, mdp5_kms->id);
  131. }
  132. static void mdp5_plane_cleanup_fb(struct drm_plane *plane,
  133. struct drm_framebuffer *fb)
  134. {
  135. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  136. struct mdp5_kms *mdp5_kms = get_kms(plane);
  137. DBG("%s: cleanup: FB[%u]", mdp5_plane->name, fb->base.id);
  138. msm_framebuffer_cleanup(fb, mdp5_kms->id);
  139. }
  140. static int mdp5_plane_atomic_check(struct drm_plane *plane,
  141. struct drm_plane_state *state)
  142. {
  143. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  144. struct drm_plane_state *old_state = plane->state;
  145. DBG("%s: check (%d -> %d)", mdp5_plane->name,
  146. plane_enabled(old_state), plane_enabled(state));
  147. if (plane_enabled(state) && plane_enabled(old_state)) {
  148. /* we cannot change SMP block configuration during scanout: */
  149. bool full_modeset = false;
  150. if (state->fb->pixel_format != old_state->fb->pixel_format) {
  151. DBG("%s: pixel_format change!", mdp5_plane->name);
  152. full_modeset = true;
  153. }
  154. if (state->src_w != old_state->src_w) {
  155. DBG("%s: src_w change!", mdp5_plane->name);
  156. full_modeset = true;
  157. }
  158. if (to_mdp5_plane_state(old_state)->pending) {
  159. DBG("%s: still pending!", mdp5_plane->name);
  160. full_modeset = true;
  161. }
  162. if (full_modeset) {
  163. struct drm_crtc_state *crtc_state =
  164. drm_atomic_get_crtc_state(state->state, state->crtc);
  165. crtc_state->mode_changed = true;
  166. to_mdp5_plane_state(state)->mode_changed = true;
  167. }
  168. } else {
  169. to_mdp5_plane_state(state)->mode_changed = true;
  170. }
  171. return 0;
  172. }
  173. static void mdp5_plane_atomic_update(struct drm_plane *plane,
  174. struct drm_plane_state *old_state)
  175. {
  176. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  177. struct drm_plane_state *state = plane->state;
  178. DBG("%s: update", mdp5_plane->name);
  179. if (!plane_enabled(state)) {
  180. to_mdp5_plane_state(state)->pending = true;
  181. mdp5_plane_disable(plane);
  182. } else if (to_mdp5_plane_state(state)->mode_changed) {
  183. int ret;
  184. to_mdp5_plane_state(state)->pending = true;
  185. ret = mdp5_plane_mode_set(plane,
  186. state->crtc, state->fb,
  187. state->crtc_x, state->crtc_y,
  188. state->crtc_w, state->crtc_h,
  189. state->src_x, state->src_y,
  190. state->src_w, state->src_h);
  191. /* atomic_check should have ensured that this doesn't fail */
  192. WARN_ON(ret < 0);
  193. } else {
  194. unsigned long flags;
  195. spin_lock_irqsave(&mdp5_plane->pipe_lock, flags);
  196. set_scanout_locked(plane, state->fb);
  197. spin_unlock_irqrestore(&mdp5_plane->pipe_lock, flags);
  198. }
  199. }
  200. static const struct drm_plane_helper_funcs mdp5_plane_helper_funcs = {
  201. .prepare_fb = mdp5_plane_prepare_fb,
  202. .cleanup_fb = mdp5_plane_cleanup_fb,
  203. .atomic_check = mdp5_plane_atomic_check,
  204. .atomic_update = mdp5_plane_atomic_update,
  205. };
  206. static void set_scanout_locked(struct drm_plane *plane,
  207. struct drm_framebuffer *fb)
  208. {
  209. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  210. struct mdp5_kms *mdp5_kms = get_kms(plane);
  211. enum mdp5_pipe pipe = mdp5_plane->pipe;
  212. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_STRIDE_A(pipe),
  213. MDP5_PIPE_SRC_STRIDE_A_P0(fb->pitches[0]) |
  214. MDP5_PIPE_SRC_STRIDE_A_P1(fb->pitches[1]));
  215. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_STRIDE_B(pipe),
  216. MDP5_PIPE_SRC_STRIDE_B_P2(fb->pitches[2]) |
  217. MDP5_PIPE_SRC_STRIDE_B_P3(fb->pitches[3]));
  218. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC0_ADDR(pipe),
  219. msm_framebuffer_iova(fb, mdp5_kms->id, 0));
  220. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC1_ADDR(pipe),
  221. msm_framebuffer_iova(fb, mdp5_kms->id, 1));
  222. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC2_ADDR(pipe),
  223. msm_framebuffer_iova(fb, mdp5_kms->id, 2));
  224. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC3_ADDR(pipe),
  225. msm_framebuffer_iova(fb, mdp5_kms->id, 4));
  226. plane->fb = fb;
  227. }
  228. static int mdp5_plane_mode_set(struct drm_plane *plane,
  229. struct drm_crtc *crtc, struct drm_framebuffer *fb,
  230. int crtc_x, int crtc_y,
  231. unsigned int crtc_w, unsigned int crtc_h,
  232. uint32_t src_x, uint32_t src_y,
  233. uint32_t src_w, uint32_t src_h)
  234. {
  235. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  236. struct mdp5_kms *mdp5_kms = get_kms(plane);
  237. enum mdp5_pipe pipe = mdp5_plane->pipe;
  238. const struct mdp_format *format;
  239. uint32_t nplanes, config = 0;
  240. uint32_t phasex_step = 0, phasey_step = 0;
  241. uint32_t hdecm = 0, vdecm = 0;
  242. unsigned long flags;
  243. int ret;
  244. nplanes = drm_format_num_planes(fb->pixel_format);
  245. /* bad formats should already be rejected: */
  246. if (WARN_ON(nplanes > pipe2nclients(pipe)))
  247. return -EINVAL;
  248. /* src values are in Q16 fixed point, convert to integer: */
  249. src_x = src_x >> 16;
  250. src_y = src_y >> 16;
  251. src_w = src_w >> 16;
  252. src_h = src_h >> 16;
  253. DBG("%s: FB[%u] %u,%u,%u,%u -> CRTC[%u] %d,%d,%u,%u", mdp5_plane->name,
  254. fb->base.id, src_x, src_y, src_w, src_h,
  255. crtc->base.id, crtc_x, crtc_y, crtc_w, crtc_h);
  256. /* Request some memory from the SMP: */
  257. ret = mdp5_smp_request(mdp5_kms->smp,
  258. mdp5_plane->pipe, fb->pixel_format, src_w);
  259. if (ret)
  260. return ret;
  261. /*
  262. * Currently we update the hw for allocations/requests immediately,
  263. * but once atomic modeset/pageflip is in place, the allocation
  264. * would move into atomic->check_plane_state(), while updating the
  265. * hw would remain here:
  266. */
  267. mdp5_smp_configure(mdp5_kms->smp, pipe);
  268. if (src_w != crtc_w) {
  269. config |= MDP5_PIPE_SCALE_CONFIG_SCALEX_EN;
  270. /* TODO calc phasex_step, hdecm */
  271. }
  272. if (src_h != crtc_h) {
  273. config |= MDP5_PIPE_SCALE_CONFIG_SCALEY_EN;
  274. /* TODO calc phasey_step, vdecm */
  275. }
  276. spin_lock_irqsave(&mdp5_plane->pipe_lock, flags);
  277. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_IMG_SIZE(pipe),
  278. MDP5_PIPE_SRC_IMG_SIZE_WIDTH(src_w) |
  279. MDP5_PIPE_SRC_IMG_SIZE_HEIGHT(src_h));
  280. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_SIZE(pipe),
  281. MDP5_PIPE_SRC_SIZE_WIDTH(src_w) |
  282. MDP5_PIPE_SRC_SIZE_HEIGHT(src_h));
  283. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_XY(pipe),
  284. MDP5_PIPE_SRC_XY_X(src_x) |
  285. MDP5_PIPE_SRC_XY_Y(src_y));
  286. mdp5_write(mdp5_kms, REG_MDP5_PIPE_OUT_SIZE(pipe),
  287. MDP5_PIPE_OUT_SIZE_WIDTH(crtc_w) |
  288. MDP5_PIPE_OUT_SIZE_HEIGHT(crtc_h));
  289. mdp5_write(mdp5_kms, REG_MDP5_PIPE_OUT_XY(pipe),
  290. MDP5_PIPE_OUT_XY_X(crtc_x) |
  291. MDP5_PIPE_OUT_XY_Y(crtc_y));
  292. format = to_mdp_format(msm_framebuffer_format(fb));
  293. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_FORMAT(pipe),
  294. MDP5_PIPE_SRC_FORMAT_A_BPC(format->bpc_a) |
  295. MDP5_PIPE_SRC_FORMAT_R_BPC(format->bpc_r) |
  296. MDP5_PIPE_SRC_FORMAT_G_BPC(format->bpc_g) |
  297. MDP5_PIPE_SRC_FORMAT_B_BPC(format->bpc_b) |
  298. COND(format->alpha_enable, MDP5_PIPE_SRC_FORMAT_ALPHA_ENABLE) |
  299. MDP5_PIPE_SRC_FORMAT_CPP(format->cpp - 1) |
  300. MDP5_PIPE_SRC_FORMAT_UNPACK_COUNT(format->unpack_count - 1) |
  301. COND(format->unpack_tight, MDP5_PIPE_SRC_FORMAT_UNPACK_TIGHT) |
  302. MDP5_PIPE_SRC_FORMAT_NUM_PLANES(nplanes - 1) |
  303. MDP5_PIPE_SRC_FORMAT_CHROMA_SAMP(CHROMA_RGB));
  304. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_UNPACK(pipe),
  305. MDP5_PIPE_SRC_UNPACK_ELEM0(format->unpack[0]) |
  306. MDP5_PIPE_SRC_UNPACK_ELEM1(format->unpack[1]) |
  307. MDP5_PIPE_SRC_UNPACK_ELEM2(format->unpack[2]) |
  308. MDP5_PIPE_SRC_UNPACK_ELEM3(format->unpack[3]));
  309. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_OP_MODE(pipe),
  310. MDP5_PIPE_SRC_OP_MODE_BWC(BWC_LOSSLESS));
  311. /* not using secure mode: */
  312. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_ADDR_SW_STATUS(pipe), 0);
  313. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_PHASE_STEP_X(pipe), phasex_step);
  314. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_PHASE_STEP_Y(pipe), phasey_step);
  315. mdp5_write(mdp5_kms, REG_MDP5_PIPE_DECIMATION(pipe),
  316. MDP5_PIPE_DECIMATION_VERT(vdecm) |
  317. MDP5_PIPE_DECIMATION_HORZ(hdecm));
  318. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_CONFIG(pipe),
  319. MDP5_PIPE_SCALE_CONFIG_SCALEX_MIN_FILTER(SCALE_FILTER_NEAREST) |
  320. MDP5_PIPE_SCALE_CONFIG_SCALEY_MIN_FILTER(SCALE_FILTER_NEAREST) |
  321. MDP5_PIPE_SCALE_CONFIG_SCALEX_CR_FILTER(SCALE_FILTER_NEAREST) |
  322. MDP5_PIPE_SCALE_CONFIG_SCALEY_CR_FILTER(SCALE_FILTER_NEAREST) |
  323. MDP5_PIPE_SCALE_CONFIG_SCALEX_MAX_FILTER(SCALE_FILTER_NEAREST) |
  324. MDP5_PIPE_SCALE_CONFIG_SCALEY_MAX_FILTER(SCALE_FILTER_NEAREST));
  325. set_scanout_locked(plane, fb);
  326. spin_unlock_irqrestore(&mdp5_plane->pipe_lock, flags);
  327. return ret;
  328. }
  329. void mdp5_plane_complete_flip(struct drm_plane *plane)
  330. {
  331. struct mdp5_kms *mdp5_kms = get_kms(plane);
  332. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  333. enum mdp5_pipe pipe = mdp5_plane->pipe;
  334. DBG("%s: complete flip", mdp5_plane->name);
  335. mdp5_smp_commit(mdp5_kms->smp, pipe);
  336. to_mdp5_plane_state(plane->state)->pending = false;
  337. }
  338. enum mdp5_pipe mdp5_plane_pipe(struct drm_plane *plane)
  339. {
  340. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  341. return mdp5_plane->pipe;
  342. }
  343. uint32_t mdp5_plane_get_flush(struct drm_plane *plane)
  344. {
  345. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  346. return mdp5_plane->flush_mask;
  347. }
  348. /* initialize plane */
  349. struct drm_plane *mdp5_plane_init(struct drm_device *dev,
  350. enum mdp5_pipe pipe, bool private_plane, uint32_t reg_offset)
  351. {
  352. struct drm_plane *plane = NULL;
  353. struct mdp5_plane *mdp5_plane;
  354. int ret;
  355. enum drm_plane_type type;
  356. mdp5_plane = kzalloc(sizeof(*mdp5_plane), GFP_KERNEL);
  357. if (!mdp5_plane) {
  358. ret = -ENOMEM;
  359. goto fail;
  360. }
  361. plane = &mdp5_plane->base;
  362. mdp5_plane->pipe = pipe;
  363. mdp5_plane->name = pipe2name(pipe);
  364. mdp5_plane->nformats = mdp5_get_formats(pipe, mdp5_plane->formats,
  365. ARRAY_SIZE(mdp5_plane->formats));
  366. mdp5_plane->flush_mask = mdp_ctl_flush_mask_pipe(pipe);
  367. mdp5_plane->reg_offset = reg_offset;
  368. spin_lock_init(&mdp5_plane->pipe_lock);
  369. type = private_plane ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
  370. ret = drm_universal_plane_init(dev, plane, 0xff, &mdp5_plane_funcs,
  371. mdp5_plane->formats, mdp5_plane->nformats,
  372. type);
  373. if (ret)
  374. goto fail;
  375. drm_plane_helper_add(plane, &mdp5_plane_helper_funcs);
  376. mdp5_plane_install_properties(plane, &plane->base);
  377. return plane;
  378. fail:
  379. if (plane)
  380. mdp5_plane_destroy(plane);
  381. return ERR_PTR(ret);
  382. }