mdp5_ctl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /*
  2. * Copyright (c) 2014-2015 The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include "mdp5_kms.h"
  14. #include "mdp5_ctl.h"
  15. /*
  16. * CTL - MDP Control Pool Manager
  17. *
  18. * Controls are shared between all CRTCs.
  19. *
  20. * They are intended to be used for data path configuration.
  21. * The top level register programming describes the complete data path for
  22. * a specific data path ID - REG_MDP5_CTL_*(<id>, ...)
  23. *
  24. * Hardware capabilities determine the number of concurrent data paths
  25. *
  26. * In certain use cases (high-resolution dual pipe), one single CTL can be
  27. * shared across multiple CRTCs.
  28. *
  29. * Because the number of CTLs can be less than the number of CRTCs,
  30. * CTLs are dynamically allocated from a pool of CTLs, only once a CRTC is
  31. * requested by the client (in mdp5_crtc_mode_set()).
  32. */
  33. struct op_mode {
  34. struct mdp5_interface intf;
  35. bool encoder_enabled;
  36. uint32_t start_mask;
  37. };
  38. struct mdp5_ctl {
  39. struct mdp5_ctl_manager *ctlm;
  40. u32 id;
  41. int lm;
  42. /* whether this CTL has been allocated or not: */
  43. bool busy;
  44. /* Operation Mode Configuration for the Pipeline */
  45. struct op_mode pipeline;
  46. /* REG_MDP5_CTL_*(<id>) registers access info + lock: */
  47. spinlock_t hw_lock;
  48. u32 reg_offset;
  49. /* when do CTL registers need to be flushed? (mask of trigger bits) */
  50. u32 pending_ctl_trigger;
  51. bool cursor_on;
  52. struct drm_crtc *crtc;
  53. };
  54. struct mdp5_ctl_manager {
  55. struct drm_device *dev;
  56. /* number of CTL / Layer Mixers in this hw config: */
  57. u32 nlm;
  58. u32 nctl;
  59. /* to filter out non-present bits in the current hardware config */
  60. u32 flush_hw_mask;
  61. /* pool of CTLs + lock to protect resource allocation (ctls[i].busy) */
  62. spinlock_t pool_lock;
  63. struct mdp5_ctl ctls[MAX_CTL];
  64. };
  65. static inline
  66. struct mdp5_kms *get_kms(struct mdp5_ctl_manager *ctl_mgr)
  67. {
  68. struct msm_drm_private *priv = ctl_mgr->dev->dev_private;
  69. return to_mdp5_kms(to_mdp_kms(priv->kms));
  70. }
  71. static inline
  72. void ctl_write(struct mdp5_ctl *ctl, u32 reg, u32 data)
  73. {
  74. struct mdp5_kms *mdp5_kms = get_kms(ctl->ctlm);
  75. (void)ctl->reg_offset; /* TODO use this instead of mdp5_write */
  76. mdp5_write(mdp5_kms, reg, data);
  77. }
  78. static inline
  79. u32 ctl_read(struct mdp5_ctl *ctl, u32 reg)
  80. {
  81. struct mdp5_kms *mdp5_kms = get_kms(ctl->ctlm);
  82. (void)ctl->reg_offset; /* TODO use this instead of mdp5_write */
  83. return mdp5_read(mdp5_kms, reg);
  84. }
  85. static void set_display_intf(struct mdp5_kms *mdp5_kms,
  86. struct mdp5_interface *intf)
  87. {
  88. unsigned long flags;
  89. u32 intf_sel;
  90. spin_lock_irqsave(&mdp5_kms->resource_lock, flags);
  91. intf_sel = mdp5_read(mdp5_kms, REG_MDP5_MDP_DISP_INTF_SEL(0));
  92. switch (intf->num) {
  93. case 0:
  94. intf_sel &= ~MDP5_MDP_DISP_INTF_SEL_INTF0__MASK;
  95. intf_sel |= MDP5_MDP_DISP_INTF_SEL_INTF0(intf->type);
  96. break;
  97. case 1:
  98. intf_sel &= ~MDP5_MDP_DISP_INTF_SEL_INTF1__MASK;
  99. intf_sel |= MDP5_MDP_DISP_INTF_SEL_INTF1(intf->type);
  100. break;
  101. case 2:
  102. intf_sel &= ~MDP5_MDP_DISP_INTF_SEL_INTF2__MASK;
  103. intf_sel |= MDP5_MDP_DISP_INTF_SEL_INTF2(intf->type);
  104. break;
  105. case 3:
  106. intf_sel &= ~MDP5_MDP_DISP_INTF_SEL_INTF3__MASK;
  107. intf_sel |= MDP5_MDP_DISP_INTF_SEL_INTF3(intf->type);
  108. break;
  109. default:
  110. BUG();
  111. break;
  112. }
  113. mdp5_write(mdp5_kms, REG_MDP5_MDP_DISP_INTF_SEL(0), intf_sel);
  114. spin_unlock_irqrestore(&mdp5_kms->resource_lock, flags);
  115. }
  116. static void set_ctl_op(struct mdp5_ctl *ctl, struct mdp5_interface *intf)
  117. {
  118. unsigned long flags;
  119. u32 ctl_op = 0;
  120. if (!mdp5_cfg_intf_is_virtual(intf->type))
  121. ctl_op |= MDP5_CTL_OP_INTF_NUM(INTF0 + intf->num);
  122. switch (intf->type) {
  123. case INTF_DSI:
  124. if (intf->mode == MDP5_INTF_DSI_MODE_COMMAND)
  125. ctl_op |= MDP5_CTL_OP_CMD_MODE;
  126. break;
  127. case INTF_WB:
  128. if (intf->mode == MDP5_INTF_WB_MODE_LINE)
  129. ctl_op |= MDP5_CTL_OP_MODE(MODE_WB_2_LINE);
  130. break;
  131. default:
  132. break;
  133. }
  134. spin_lock_irqsave(&ctl->hw_lock, flags);
  135. ctl_write(ctl, REG_MDP5_CTL_OP(ctl->id), ctl_op);
  136. spin_unlock_irqrestore(&ctl->hw_lock, flags);
  137. }
  138. int mdp5_ctl_set_intf(struct mdp5_ctl *ctl, struct mdp5_interface *intf)
  139. {
  140. struct mdp5_ctl_manager *ctl_mgr = ctl->ctlm;
  141. struct mdp5_kms *mdp5_kms = get_kms(ctl_mgr);
  142. memcpy(&ctl->pipeline.intf, intf, sizeof(*intf));
  143. ctl->pipeline.start_mask = mdp_ctl_flush_mask_lm(ctl->lm) |
  144. mdp_ctl_flush_mask_encoder(intf);
  145. /* Virtual interfaces need not set a display intf (e.g.: Writeback) */
  146. if (!mdp5_cfg_intf_is_virtual(intf->type))
  147. set_display_intf(mdp5_kms, intf);
  148. set_ctl_op(ctl, intf);
  149. return 0;
  150. }
  151. static bool start_signal_needed(struct mdp5_ctl *ctl)
  152. {
  153. struct op_mode *pipeline = &ctl->pipeline;
  154. if (!pipeline->encoder_enabled || pipeline->start_mask != 0)
  155. return false;
  156. switch (pipeline->intf.type) {
  157. case INTF_WB:
  158. return true;
  159. case INTF_DSI:
  160. return pipeline->intf.mode == MDP5_INTF_DSI_MODE_COMMAND;
  161. default:
  162. return false;
  163. }
  164. }
  165. /*
  166. * send_start_signal() - Overlay Processor Start Signal
  167. *
  168. * For a given control operation (display pipeline), a START signal needs to be
  169. * executed in order to kick off operation and activate all layers.
  170. * e.g.: DSI command mode, Writeback
  171. */
  172. static void send_start_signal(struct mdp5_ctl *ctl)
  173. {
  174. unsigned long flags;
  175. spin_lock_irqsave(&ctl->hw_lock, flags);
  176. ctl_write(ctl, REG_MDP5_CTL_START(ctl->id), 1);
  177. spin_unlock_irqrestore(&ctl->hw_lock, flags);
  178. }
  179. static void refill_start_mask(struct mdp5_ctl *ctl)
  180. {
  181. struct op_mode *pipeline = &ctl->pipeline;
  182. struct mdp5_interface *intf = &ctl->pipeline.intf;
  183. pipeline->start_mask = mdp_ctl_flush_mask_lm(ctl->lm);
  184. /*
  185. * Writeback encoder needs to program & flush
  186. * address registers for each page flip..
  187. */
  188. if (intf->type == INTF_WB)
  189. pipeline->start_mask |= mdp_ctl_flush_mask_encoder(intf);
  190. }
  191. /**
  192. * mdp5_ctl_set_encoder_state() - set the encoder state
  193. *
  194. * @enable: true, when encoder is ready for data streaming; false, otherwise.
  195. *
  196. * Note:
  197. * This encoder state is needed to trigger START signal (data path kickoff).
  198. */
  199. int mdp5_ctl_set_encoder_state(struct mdp5_ctl *ctl, bool enabled)
  200. {
  201. if (WARN_ON(!ctl))
  202. return -EINVAL;
  203. ctl->pipeline.encoder_enabled = enabled;
  204. DBG("intf_%d: %s", ctl->pipeline.intf.num, enabled ? "on" : "off");
  205. if (start_signal_needed(ctl)) {
  206. send_start_signal(ctl);
  207. refill_start_mask(ctl);
  208. }
  209. return 0;
  210. }
  211. /*
  212. * Note:
  213. * CTL registers need to be flushed after calling this function
  214. * (call mdp5_ctl_commit() with mdp_ctl_flush_mask_ctl() mask)
  215. */
  216. int mdp5_ctl_set_cursor(struct mdp5_ctl *ctl, int cursor_id, bool enable)
  217. {
  218. struct mdp5_ctl_manager *ctl_mgr = ctl->ctlm;
  219. unsigned long flags;
  220. u32 blend_cfg;
  221. int lm = ctl->lm;
  222. if (unlikely(WARN_ON(lm < 0))) {
  223. dev_err(ctl_mgr->dev->dev, "CTL %d cannot find LM: %d",
  224. ctl->id, lm);
  225. return -EINVAL;
  226. }
  227. spin_lock_irqsave(&ctl->hw_lock, flags);
  228. blend_cfg = ctl_read(ctl, REG_MDP5_CTL_LAYER_REG(ctl->id, lm));
  229. if (enable)
  230. blend_cfg |= MDP5_CTL_LAYER_REG_CURSOR_OUT;
  231. else
  232. blend_cfg &= ~MDP5_CTL_LAYER_REG_CURSOR_OUT;
  233. ctl_write(ctl, REG_MDP5_CTL_LAYER_REG(ctl->id, lm), blend_cfg);
  234. spin_unlock_irqrestore(&ctl->hw_lock, flags);
  235. ctl->pending_ctl_trigger = mdp_ctl_flush_mask_cursor(cursor_id);
  236. ctl->cursor_on = enable;
  237. return 0;
  238. }
  239. int mdp5_ctl_blend(struct mdp5_ctl *ctl, u32 lm, u32 blend_cfg)
  240. {
  241. unsigned long flags;
  242. if (ctl->cursor_on)
  243. blend_cfg |= MDP5_CTL_LAYER_REG_CURSOR_OUT;
  244. else
  245. blend_cfg &= ~MDP5_CTL_LAYER_REG_CURSOR_OUT;
  246. spin_lock_irqsave(&ctl->hw_lock, flags);
  247. ctl_write(ctl, REG_MDP5_CTL_LAYER_REG(ctl->id, lm), blend_cfg);
  248. spin_unlock_irqrestore(&ctl->hw_lock, flags);
  249. ctl->pending_ctl_trigger = mdp_ctl_flush_mask_lm(lm);
  250. return 0;
  251. }
  252. u32 mdp_ctl_flush_mask_encoder(struct mdp5_interface *intf)
  253. {
  254. if (intf->type == INTF_WB)
  255. return MDP5_CTL_FLUSH_WB;
  256. switch (intf->num) {
  257. case 0: return MDP5_CTL_FLUSH_TIMING_0;
  258. case 1: return MDP5_CTL_FLUSH_TIMING_1;
  259. case 2: return MDP5_CTL_FLUSH_TIMING_2;
  260. case 3: return MDP5_CTL_FLUSH_TIMING_3;
  261. default: return 0;
  262. }
  263. }
  264. u32 mdp_ctl_flush_mask_cursor(int cursor_id)
  265. {
  266. switch (cursor_id) {
  267. case 0: return MDP5_CTL_FLUSH_CURSOR_0;
  268. case 1: return MDP5_CTL_FLUSH_CURSOR_1;
  269. default: return 0;
  270. }
  271. }
  272. u32 mdp_ctl_flush_mask_pipe(enum mdp5_pipe pipe)
  273. {
  274. switch (pipe) {
  275. case SSPP_VIG0: return MDP5_CTL_FLUSH_VIG0;
  276. case SSPP_VIG1: return MDP5_CTL_FLUSH_VIG1;
  277. case SSPP_VIG2: return MDP5_CTL_FLUSH_VIG2;
  278. case SSPP_RGB0: return MDP5_CTL_FLUSH_RGB0;
  279. case SSPP_RGB1: return MDP5_CTL_FLUSH_RGB1;
  280. case SSPP_RGB2: return MDP5_CTL_FLUSH_RGB2;
  281. case SSPP_DMA0: return MDP5_CTL_FLUSH_DMA0;
  282. case SSPP_DMA1: return MDP5_CTL_FLUSH_DMA1;
  283. case SSPP_VIG3: return MDP5_CTL_FLUSH_VIG3;
  284. case SSPP_RGB3: return MDP5_CTL_FLUSH_RGB3;
  285. default: return 0;
  286. }
  287. }
  288. u32 mdp_ctl_flush_mask_lm(int lm)
  289. {
  290. switch (lm) {
  291. case 0: return MDP5_CTL_FLUSH_LM0;
  292. case 1: return MDP5_CTL_FLUSH_LM1;
  293. case 2: return MDP5_CTL_FLUSH_LM2;
  294. case 5: return MDP5_CTL_FLUSH_LM5;
  295. default: return 0;
  296. }
  297. }
  298. static u32 fix_sw_flush(struct mdp5_ctl *ctl, u32 flush_mask)
  299. {
  300. struct mdp5_ctl_manager *ctl_mgr = ctl->ctlm;
  301. u32 sw_mask = 0;
  302. #define BIT_NEEDS_SW_FIX(bit) \
  303. (!(ctl_mgr->flush_hw_mask & bit) && (flush_mask & bit))
  304. /* for some targets, cursor bit is the same as LM bit */
  305. if (BIT_NEEDS_SW_FIX(MDP5_CTL_FLUSH_CURSOR_0))
  306. sw_mask |= mdp_ctl_flush_mask_lm(ctl->lm);
  307. return sw_mask;
  308. }
  309. /**
  310. * mdp5_ctl_commit() - Register Flush
  311. *
  312. * The flush register is used to indicate several registers are all
  313. * programmed, and are safe to update to the back copy of the double
  314. * buffered registers.
  315. *
  316. * Some registers FLUSH bits are shared when the hardware does not have
  317. * dedicated bits for them; handling these is the job of fix_sw_flush().
  318. *
  319. * CTL registers need to be flushed in some circumstances; if that is the
  320. * case, some trigger bits will be present in both flush mask and
  321. * ctl->pending_ctl_trigger.
  322. */
  323. int mdp5_ctl_commit(struct mdp5_ctl *ctl, u32 flush_mask)
  324. {
  325. struct mdp5_ctl_manager *ctl_mgr = ctl->ctlm;
  326. struct op_mode *pipeline = &ctl->pipeline;
  327. unsigned long flags;
  328. pipeline->start_mask &= ~flush_mask;
  329. VERB("flush_mask=%x, start_mask=%x, trigger=%x", flush_mask,
  330. pipeline->start_mask, ctl->pending_ctl_trigger);
  331. if (ctl->pending_ctl_trigger & flush_mask) {
  332. flush_mask |= MDP5_CTL_FLUSH_CTL;
  333. ctl->pending_ctl_trigger = 0;
  334. }
  335. flush_mask |= fix_sw_flush(ctl, flush_mask);
  336. flush_mask &= ctl_mgr->flush_hw_mask;
  337. if (flush_mask) {
  338. spin_lock_irqsave(&ctl->hw_lock, flags);
  339. ctl_write(ctl, REG_MDP5_CTL_FLUSH(ctl->id), flush_mask);
  340. spin_unlock_irqrestore(&ctl->hw_lock, flags);
  341. }
  342. if (start_signal_needed(ctl)) {
  343. send_start_signal(ctl);
  344. refill_start_mask(ctl);
  345. }
  346. return 0;
  347. }
  348. void mdp5_ctl_release(struct mdp5_ctl *ctl)
  349. {
  350. struct mdp5_ctl_manager *ctl_mgr = ctl->ctlm;
  351. unsigned long flags;
  352. if (unlikely(WARN_ON(ctl->id >= MAX_CTL) || !ctl->busy)) {
  353. dev_err(ctl_mgr->dev->dev, "CTL %d in bad state (%d)",
  354. ctl->id, ctl->busy);
  355. return;
  356. }
  357. spin_lock_irqsave(&ctl_mgr->pool_lock, flags);
  358. ctl->busy = false;
  359. spin_unlock_irqrestore(&ctl_mgr->pool_lock, flags);
  360. DBG("CTL %d released", ctl->id);
  361. }
  362. int mdp5_ctl_get_ctl_id(struct mdp5_ctl *ctl)
  363. {
  364. return WARN_ON(!ctl) ? -EINVAL : ctl->id;
  365. }
  366. /*
  367. * mdp5_ctl_request() - CTL dynamic allocation
  368. *
  369. * Note: Current implementation considers that we can only have one CRTC per CTL
  370. *
  371. * @return first free CTL
  372. */
  373. struct mdp5_ctl *mdp5_ctlm_request(struct mdp5_ctl_manager *ctl_mgr,
  374. struct drm_crtc *crtc)
  375. {
  376. struct mdp5_ctl *ctl = NULL;
  377. unsigned long flags;
  378. int c;
  379. spin_lock_irqsave(&ctl_mgr->pool_lock, flags);
  380. for (c = 0; c < ctl_mgr->nctl; c++)
  381. if (!ctl_mgr->ctls[c].busy)
  382. break;
  383. if (unlikely(c >= ctl_mgr->nctl)) {
  384. dev_err(ctl_mgr->dev->dev, "No more CTL available!");
  385. goto unlock;
  386. }
  387. ctl = &ctl_mgr->ctls[c];
  388. ctl->lm = mdp5_crtc_get_lm(crtc);
  389. ctl->crtc = crtc;
  390. ctl->busy = true;
  391. ctl->pending_ctl_trigger = 0;
  392. DBG("CTL %d allocated", ctl->id);
  393. unlock:
  394. spin_unlock_irqrestore(&ctl_mgr->pool_lock, flags);
  395. return ctl;
  396. }
  397. void mdp5_ctlm_hw_reset(struct mdp5_ctl_manager *ctl_mgr)
  398. {
  399. unsigned long flags;
  400. int c;
  401. for (c = 0; c < ctl_mgr->nctl; c++) {
  402. struct mdp5_ctl *ctl = &ctl_mgr->ctls[c];
  403. spin_lock_irqsave(&ctl->hw_lock, flags);
  404. ctl_write(ctl, REG_MDP5_CTL_OP(ctl->id), 0);
  405. spin_unlock_irqrestore(&ctl->hw_lock, flags);
  406. }
  407. }
  408. void mdp5_ctlm_destroy(struct mdp5_ctl_manager *ctl_mgr)
  409. {
  410. kfree(ctl_mgr);
  411. }
  412. struct mdp5_ctl_manager *mdp5_ctlm_init(struct drm_device *dev,
  413. void __iomem *mmio_base, const struct mdp5_cfg_hw *hw_cfg)
  414. {
  415. struct mdp5_ctl_manager *ctl_mgr;
  416. const struct mdp5_ctl_block *ctl_cfg = &hw_cfg->ctl;
  417. unsigned long flags;
  418. int c, ret;
  419. ctl_mgr = kzalloc(sizeof(*ctl_mgr), GFP_KERNEL);
  420. if (!ctl_mgr) {
  421. dev_err(dev->dev, "failed to allocate CTL manager\n");
  422. ret = -ENOMEM;
  423. goto fail;
  424. }
  425. if (unlikely(WARN_ON(ctl_cfg->count > MAX_CTL))) {
  426. dev_err(dev->dev, "Increase static pool size to at least %d\n",
  427. ctl_cfg->count);
  428. ret = -ENOSPC;
  429. goto fail;
  430. }
  431. /* initialize the CTL manager: */
  432. ctl_mgr->dev = dev;
  433. ctl_mgr->nlm = hw_cfg->lm.count;
  434. ctl_mgr->nctl = ctl_cfg->count;
  435. ctl_mgr->flush_hw_mask = ctl_cfg->flush_hw_mask;
  436. spin_lock_init(&ctl_mgr->pool_lock);
  437. /* initialize each CTL of the pool: */
  438. spin_lock_irqsave(&ctl_mgr->pool_lock, flags);
  439. for (c = 0; c < ctl_mgr->nctl; c++) {
  440. struct mdp5_ctl *ctl = &ctl_mgr->ctls[c];
  441. if (WARN_ON(!ctl_cfg->base[c])) {
  442. dev_err(dev->dev, "CTL_%d: base is null!\n", c);
  443. ret = -EINVAL;
  444. goto fail;
  445. }
  446. ctl->ctlm = ctl_mgr;
  447. ctl->id = c;
  448. ctl->reg_offset = ctl_cfg->base[c];
  449. ctl->busy = false;
  450. spin_lock_init(&ctl->hw_lock);
  451. }
  452. spin_unlock_irqrestore(&ctl_mgr->pool_lock, flags);
  453. DBG("Pool of %d CTLs created.", ctl_mgr->nctl);
  454. return ctl_mgr;
  455. fail:
  456. if (ctl_mgr)
  457. mdp5_ctlm_destroy(ctl_mgr);
  458. return ERR_PTR(ret);
  459. }