mtk_drm_session.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (c) 2015 MediaTek Inc.
  3. * Copyright (C) 2021 XiaoMi, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <drm/drmP.h>
  15. #include <drm/drm_gem.h>
  16. #include <linux/dma-buf.h>
  17. #include <drm/mediatek_drm.h>
  18. #include "mtk_drm_drv.h"
  19. #include "mtk_drm_session.h"
  20. #include "mtk_drm_mmp.h"
  21. static DEFINE_MUTEX(disp_session_lock);
  22. int mtk_drm_session_create(struct drm_device *dev,
  23. struct drm_mtk_session *config)
  24. {
  25. int ret = 0;
  26. int is_session_inited = 0;
  27. struct mtk_drm_private *private = dev->dev_private;
  28. unsigned int session =
  29. MAKE_MTK_SESSION(config->type, config->device_id);
  30. int i, idx = -1;
  31. if (config->type < MTK_SESSION_PRIMARY ||
  32. config->type > MTK_SESSION_MEMORY) {
  33. DDPPR_ERR("%s create session type abnormal: %u,\n",
  34. __func__, config->type);
  35. return -EINVAL;
  36. }
  37. /* 1.To check if this session exists already */
  38. mutex_lock(&disp_session_lock);
  39. for (i = 0; i < MAX_SESSION_COUNT; i++) {
  40. if (private->session_id[i] == session) {
  41. is_session_inited = 1;
  42. idx = i;
  43. DDPPR_ERR("[DRM] create session is exited:0x%x\n",
  44. session);
  45. break;
  46. }
  47. }
  48. if (is_session_inited == 1) {
  49. config->session_id = session;
  50. goto done;
  51. }
  52. if (idx == -1)
  53. idx = config->type - 1;
  54. /* 1.To check if support this session (mode,type,dev) */
  55. /* 2. Create this session */
  56. if (idx != -1) {
  57. config->session_id = session;
  58. private
  59. ->session_id[idx] = session;
  60. private
  61. ->num_sessions = idx + 1;
  62. DDPINFO("[DRM] New session:0x%x, idx:%d\n", session, idx);
  63. } else {
  64. DDPPR_ERR("[DRM] Invalid session creation request\n");
  65. ret = -1;
  66. }
  67. done:
  68. mutex_unlock(&disp_session_lock);
  69. if (mtk_drm_helper_get_opt(private->helper_opt,
  70. MTK_DRM_OPT_VDS_PATH_SWITCH) &&
  71. (MTK_SESSION_TYPE(session) == MTK_SESSION_MEMORY)) {
  72. enum MTK_DRM_HELPER_OPT helper_opt;
  73. private->need_vds_path_switch = 1;
  74. private->vds_path_switch_dirty = 1;
  75. private->vds_path_switch_done = 0;
  76. private->vds_path_enable = 0;
  77. DDPMSG("Switch vds: crtc2 vds session create\n");
  78. /* Close RPO */
  79. mtk_drm_helper_set_opt_by_name(private->helper_opt,
  80. "MTK_DRM_OPT_RPO", 0);
  81. helper_opt =
  82. mtk_drm_helper_name_to_opt(private->helper_opt,
  83. "MTK_DRM_OPT_RPO");
  84. mtk_update_layering_opt_by_disp_opt(helper_opt, 0);
  85. mtk_set_layering_opt(LYE_OPT_RPO, 0);
  86. }
  87. DDPINFO("[DRM] new session done\n");
  88. return ret;
  89. }
  90. int mtk_session_get_mode(struct drm_device *dev, struct drm_crtc *crtc)
  91. {
  92. int crtc_idx = drm_crtc_index(crtc);
  93. struct mtk_drm_private *private = dev->dev_private;
  94. int session_mode = private->session_mode;
  95. const struct mtk_session_mode_tb *mode_tb = private->data->mode_tb;
  96. if (!mode_tb[session_mode].en)
  97. return -EINVAL;
  98. return mode_tb[session_mode].ddp_mode[crtc_idx];
  99. }
  100. int mtk_session_set_mode(struct drm_device *dev, unsigned int session_mode)
  101. {
  102. int i;
  103. struct mtk_drm_private *private = dev->dev_private;
  104. const struct mtk_session_mode_tb *mode_tb = private->data->mode_tb;
  105. mutex_lock(&private->commit.lock);
  106. if (session_mode >= MTK_DRM_SESSION_NUM) {
  107. DDPPR_ERR("%s Invalid session mode:%d\n",
  108. __func__, session_mode);
  109. goto error;
  110. }
  111. if (!mode_tb[session_mode].en) {
  112. DDPPR_ERR("%s Invalid mode_tb[%d].en = %d\n",
  113. __func__, session_mode, mode_tb[session_mode].en);
  114. goto error;
  115. }
  116. if (session_mode == private->session_mode)
  117. goto success;
  118. DRM_MMP_EVENT_START(set_mode, private->session_mode,
  119. session_mode);
  120. DDPMSG("%s from %u to %u\n", __func__,
  121. private->session_mode, session_mode);
  122. if (mtk_drm_helper_get_opt(private->helper_opt,
  123. MTK_DRM_OPT_VDS_PATH_SWITCH) &&
  124. (private->session_mode == MTK_DRM_SESSION_DOUBLE_DL) &&
  125. (session_mode == MTK_DRM_SESSION_DL)) {
  126. enum MTK_DRM_HELPER_OPT helper_opt;
  127. private->need_vds_path_switch = 0;
  128. private->vds_path_switch_done = 0;
  129. private->vds_path_enable = 0;
  130. /* Open RPO */
  131. mtk_drm_helper_set_opt_by_name(private->helper_opt,
  132. "MTK_DRM_OPT_RPO", 1);
  133. helper_opt =
  134. mtk_drm_helper_name_to_opt(private->helper_opt,
  135. "MTK_DRM_OPT_RPO");
  136. mtk_update_layering_opt_by_disp_opt(helper_opt, 1);
  137. mtk_set_layering_opt(LYE_OPT_RPO, 1);
  138. /* OVL0_2l switch back to main path */
  139. DDPMSG("Switch vds: crtc2 vds set ddp mode to DL\n");
  140. mtk_need_vds_path_switch(private->crtc[0]);
  141. }
  142. /* For releasing HW resource purpose, the ddp mode should
  143. * switching reversely in some situation.
  144. * CRTC2 -> CRTC1 ->CRTC0
  145. */
  146. if (session_mode == MTK_DRM_SESSION_DC_MIRROR ||
  147. private->session_mode == MTK_DRM_SESSION_TRIPLE_DL) {
  148. DRM_MMP_MARK(set_mode, 1, 0);
  149. for (i = MAX_CRTC - 1; i >= 0; i--) {
  150. if (private->crtc[i])
  151. mtk_crtc_path_switch(
  152. private->crtc[i],
  153. mode_tb[session_mode].ddp_mode[i], 1);
  154. }
  155. } else {
  156. DRM_MMP_MARK(set_mode, 1, 1);
  157. for (i = 0; i < MAX_CRTC; i++) {
  158. if (private->crtc[i])
  159. mtk_crtc_path_switch(
  160. private->crtc[i],
  161. mode_tb[session_mode].ddp_mode[i], 1);
  162. }
  163. }
  164. private->session_mode = session_mode;
  165. DRM_MMP_EVENT_END(set_mode, private->session_mode,
  166. session_mode);
  167. success:
  168. mutex_unlock(&private->commit.lock);
  169. return 0;
  170. error:
  171. mutex_unlock(&private->commit.lock);
  172. return -EINVAL;
  173. }
  174. int mtk_drm_session_destroy(struct drm_device *dev,
  175. struct drm_mtk_session *config)
  176. {
  177. int ret = -1;
  178. unsigned int session = config->session_id;
  179. struct mtk_drm_private *private = dev->dev_private;
  180. int i;
  181. DDPINFO("disp_destroy_session, 0x%x", config->session_id);
  182. /* 1.To check if this session exists already, and remove it */
  183. mutex_lock(&disp_session_lock);
  184. for (i = 0; i < MAX_SESSION_COUNT; i++) {
  185. if (private->session_id[i] == session) {
  186. private->session_id[i] = 0;
  187. ret = 0;
  188. break;
  189. }
  190. }
  191. mutex_unlock(&disp_session_lock);
  192. /* 2. Destroy this session */
  193. if (ret == 0)
  194. DDPINFO("Destroy session(0x%x)\n", session);
  195. else
  196. DDPPR_ERR("session(0x%x) does not exists\n", session);
  197. return ret;
  198. }
  199. int mtk_get_session_id(struct drm_crtc *crtc)
  200. {
  201. struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
  202. struct mtk_drm_private *private;
  203. int session_id = -1, id, i;
  204. id = drm_crtc_index(crtc);
  205. private = mtk_crtc->base.dev->dev_private;
  206. for (i = 0; i < MAX_SESSION_COUNT; i++) {
  207. if ((id + 1) == MTK_SESSION_TYPE(private->session_id[i])) {
  208. session_id = private->session_id[i];
  209. break;
  210. }
  211. }
  212. return session_id;
  213. }
  214. int mtk_drm_session_create_ioctl(struct drm_device *dev, void *data,
  215. struct drm_file *file_priv)
  216. {
  217. int ret = 0;
  218. struct drm_mtk_session *config = data;
  219. if (mtk_drm_session_create(dev, config) != 0)
  220. ret = -EFAULT;
  221. return ret;
  222. }
  223. int mtk_drm_session_destroy_ioctl(struct drm_device *dev, void *data,
  224. struct drm_file *file_priv)
  225. {
  226. int ret = 0;
  227. struct drm_mtk_session *config = data;
  228. if (mtk_drm_session_destroy(dev, config) != 0)
  229. ret = -EFAULT;
  230. return ret;
  231. }