1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
3 * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
6 #include <linux/delay.h>
8 #include "dpu_hw_ctl.h"
10 #include "dpu_trace.h"
12 #define CTL_LAYER(lm) \
13 (((lm) == LM_5) ? (0x024) : (((lm) - LM_0) * 0x004))
14 #define CTL_LAYER_EXT(lm) \
15 (0x40 + (((lm) - LM_0) * 0x004))
16 #define CTL_LAYER_EXT2(lm) \
17 (0x70 + (((lm) - LM_0) * 0x004))
18 #define CTL_LAYER_EXT3(lm) \
19 (0xA0 + (((lm) - LM_0) * 0x004))
20 #define CTL_LAYER_EXT4(lm) \
21 (0xB8 + (((lm) - LM_0) * 0x004))
23 #define CTL_FLUSH 0x018
24 #define CTL_START 0x01C
25 #define CTL_PREPARE 0x0d0
26 #define CTL_SW_RESET 0x030
27 #define CTL_LAYER_EXTN_OFFSET 0x40
28 #define CTL_MERGE_3D_ACTIVE 0x0E4
29 #define CTL_WB_ACTIVE 0x0EC
30 #define CTL_INTF_ACTIVE 0x0F4
31 #define CTL_MERGE_3D_FLUSH 0x100
32 #define CTL_DSC_ACTIVE 0x0E8
33 #define CTL_DSC_FLUSH 0x104
34 #define CTL_WB_FLUSH 0x108
35 #define CTL_INTF_FLUSH 0x110
36 #define CTL_INTF_MASTER 0x134
37 #define CTL_FETCH_PIPE_ACTIVE 0x0FC
39 #define CTL_MIXER_BORDER_OUT BIT(24)
40 #define CTL_FLUSH_MASK_CTL BIT(17)
42 #define DPU_REG_RESET_TIMEOUT_US 2000
43 #define MERGE_3D_IDX 23
47 #define CTL_INVALID_BIT 0xffff
48 #define CTL_DEFAULT_GROUP_ID 0xf
50 static const u32 fetch_tbl[SSPP_MAX] = {CTL_INVALID_BIT, 16, 17, 18, 19,
51 CTL_INVALID_BIT, CTL_INVALID_BIT, CTL_INVALID_BIT, CTL_INVALID_BIT, 0,
52 1, 2, 3, CTL_INVALID_BIT, CTL_INVALID_BIT};
54 static const struct dpu_ctl_cfg *_ctl_offset(enum dpu_ctl ctl,
55 const struct dpu_mdss_cfg *m,
57 struct dpu_hw_blk_reg_map *b)
61 for (i = 0; i < m->ctl_count; i++) {
62 if (ctl == m->ctl[i].id) {
63 b->blk_addr = addr + m->ctl[i].base;
64 b->log_mask = DPU_DBG_MASK_CTL;
68 return ERR_PTR(-ENOMEM);
71 static int _mixer_stages(const struct dpu_lm_cfg *mixer, int count,
77 for (i = 0; i < count; i++) {
78 if (lm == mixer[i].id) {
79 stages = mixer[i].sblk->maxblendstages;
87 static inline u32 dpu_hw_ctl_get_flush_register(struct dpu_hw_ctl *ctx)
89 struct dpu_hw_blk_reg_map *c = &ctx->hw;
91 return DPU_REG_READ(c, CTL_FLUSH);
94 static inline void dpu_hw_ctl_trigger_start(struct dpu_hw_ctl *ctx)
96 trace_dpu_hw_ctl_trigger_start(ctx->pending_flush_mask,
97 dpu_hw_ctl_get_flush_register(ctx));
98 DPU_REG_WRITE(&ctx->hw, CTL_START, 0x1);
101 static inline bool dpu_hw_ctl_is_started(struct dpu_hw_ctl *ctx)
103 return !!(DPU_REG_READ(&ctx->hw, CTL_START) & BIT(0));
106 static inline void dpu_hw_ctl_trigger_pending(struct dpu_hw_ctl *ctx)
108 trace_dpu_hw_ctl_trigger_prepare(ctx->pending_flush_mask,
109 dpu_hw_ctl_get_flush_register(ctx));
110 DPU_REG_WRITE(&ctx->hw, CTL_PREPARE, 0x1);
113 static inline void dpu_hw_ctl_clear_pending_flush(struct dpu_hw_ctl *ctx)
115 trace_dpu_hw_ctl_clear_pending_flush(ctx->pending_flush_mask,
116 dpu_hw_ctl_get_flush_register(ctx));
117 ctx->pending_flush_mask = 0x0;
120 static inline void dpu_hw_ctl_update_pending_flush(struct dpu_hw_ctl *ctx,
123 trace_dpu_hw_ctl_update_pending_flush(flushbits,
124 ctx->pending_flush_mask);
125 ctx->pending_flush_mask |= flushbits;
128 static u32 dpu_hw_ctl_get_pending_flush(struct dpu_hw_ctl *ctx)
130 return ctx->pending_flush_mask;
133 static inline void dpu_hw_ctl_trigger_flush_v1(struct dpu_hw_ctl *ctx)
135 if (ctx->pending_flush_mask & BIT(MERGE_3D_IDX))
136 DPU_REG_WRITE(&ctx->hw, CTL_MERGE_3D_FLUSH,
137 ctx->pending_merge_3d_flush_mask);
138 if (ctx->pending_flush_mask & BIT(INTF_IDX))
139 DPU_REG_WRITE(&ctx->hw, CTL_INTF_FLUSH,
140 ctx->pending_intf_flush_mask);
141 if (ctx->pending_flush_mask & BIT(WB_IDX))
142 DPU_REG_WRITE(&ctx->hw, CTL_WB_FLUSH,
143 ctx->pending_wb_flush_mask);
145 DPU_REG_WRITE(&ctx->hw, CTL_FLUSH, ctx->pending_flush_mask);
148 static inline void dpu_hw_ctl_trigger_flush(struct dpu_hw_ctl *ctx)
150 trace_dpu_hw_ctl_trigger_pending_flush(ctx->pending_flush_mask,
151 dpu_hw_ctl_get_flush_register(ctx));
152 DPU_REG_WRITE(&ctx->hw, CTL_FLUSH, ctx->pending_flush_mask);
155 static void dpu_hw_ctl_update_pending_flush_sspp(struct dpu_hw_ctl *ctx,
160 ctx->pending_flush_mask |= BIT(0);
163 ctx->pending_flush_mask |= BIT(1);
166 ctx->pending_flush_mask |= BIT(2);
169 ctx->pending_flush_mask |= BIT(18);
172 ctx->pending_flush_mask |= BIT(3);
175 ctx->pending_flush_mask |= BIT(4);
178 ctx->pending_flush_mask |= BIT(5);
181 ctx->pending_flush_mask |= BIT(19);
184 ctx->pending_flush_mask |= BIT(11);
187 ctx->pending_flush_mask |= BIT(12);
190 ctx->pending_flush_mask |= BIT(24);
193 ctx->pending_flush_mask |= BIT(25);
196 ctx->pending_flush_mask |= BIT(22);
199 ctx->pending_flush_mask |= BIT(23);
206 static void dpu_hw_ctl_update_pending_flush_mixer(struct dpu_hw_ctl *ctx,
211 ctx->pending_flush_mask |= BIT(6);
214 ctx->pending_flush_mask |= BIT(7);
217 ctx->pending_flush_mask |= BIT(8);
220 ctx->pending_flush_mask |= BIT(9);
223 ctx->pending_flush_mask |= BIT(10);
226 ctx->pending_flush_mask |= BIT(20);
232 ctx->pending_flush_mask |= CTL_FLUSH_MASK_CTL;
235 static void dpu_hw_ctl_update_pending_flush_intf(struct dpu_hw_ctl *ctx,
240 ctx->pending_flush_mask |= BIT(31);
243 ctx->pending_flush_mask |= BIT(30);
246 ctx->pending_flush_mask |= BIT(29);
249 ctx->pending_flush_mask |= BIT(28);
256 static void dpu_hw_ctl_update_pending_flush_wb(struct dpu_hw_ctl *ctx,
263 ctx->pending_flush_mask |= BIT(WB_IDX);
270 static void dpu_hw_ctl_update_pending_flush_wb_v1(struct dpu_hw_ctl *ctx,
273 ctx->pending_wb_flush_mask |= BIT(wb - WB_0);
274 ctx->pending_flush_mask |= BIT(WB_IDX);
277 static void dpu_hw_ctl_update_pending_flush_intf_v1(struct dpu_hw_ctl *ctx,
280 ctx->pending_intf_flush_mask |= BIT(intf - INTF_0);
281 ctx->pending_flush_mask |= BIT(INTF_IDX);
284 static void dpu_hw_ctl_update_pending_flush_merge_3d_v1(struct dpu_hw_ctl *ctx,
285 enum dpu_merge_3d merge_3d)
287 ctx->pending_merge_3d_flush_mask |= BIT(merge_3d - MERGE_3D_0);
288 ctx->pending_flush_mask |= BIT(MERGE_3D_IDX);
291 static void dpu_hw_ctl_update_pending_flush_dspp(struct dpu_hw_ctl *ctx,
296 ctx->pending_flush_mask |= BIT(13);
299 ctx->pending_flush_mask |= BIT(14);
302 ctx->pending_flush_mask |= BIT(15);
305 ctx->pending_flush_mask |= BIT(21);
312 static u32 dpu_hw_ctl_poll_reset_status(struct dpu_hw_ctl *ctx, u32 timeout_us)
314 struct dpu_hw_blk_reg_map *c = &ctx->hw;
318 timeout = ktime_add_us(ktime_get(), timeout_us);
321 * it takes around 30us to have mdp finish resetting its ctl path
322 * poll every 50us so that reset should be completed at 1st poll
325 status = DPU_REG_READ(c, CTL_SW_RESET);
328 usleep_range(20, 50);
329 } while (status && ktime_compare_safe(ktime_get(), timeout) < 0);
334 static int dpu_hw_ctl_reset_control(struct dpu_hw_ctl *ctx)
336 struct dpu_hw_blk_reg_map *c = &ctx->hw;
338 pr_debug("issuing hw ctl reset for ctl:%d\n", ctx->idx);
339 DPU_REG_WRITE(c, CTL_SW_RESET, 0x1);
340 if (dpu_hw_ctl_poll_reset_status(ctx, DPU_REG_RESET_TIMEOUT_US))
346 static int dpu_hw_ctl_wait_reset_status(struct dpu_hw_ctl *ctx)
348 struct dpu_hw_blk_reg_map *c = &ctx->hw;
351 status = DPU_REG_READ(c, CTL_SW_RESET);
356 pr_debug("hw ctl reset is set for ctl:%d\n", ctx->idx);
357 if (dpu_hw_ctl_poll_reset_status(ctx, DPU_REG_RESET_TIMEOUT_US)) {
358 pr_err("hw recovery is not complete for ctl:%d\n", ctx->idx);
365 static void dpu_hw_ctl_clear_all_blendstages(struct dpu_hw_ctl *ctx)
367 struct dpu_hw_blk_reg_map *c = &ctx->hw;
370 for (i = 0; i < ctx->mixer_count; i++) {
371 enum dpu_lm mixer_id = ctx->mixer_hw_caps[i].id;
373 DPU_REG_WRITE(c, CTL_LAYER(mixer_id), 0);
374 DPU_REG_WRITE(c, CTL_LAYER_EXT(mixer_id), 0);
375 DPU_REG_WRITE(c, CTL_LAYER_EXT2(mixer_id), 0);
376 DPU_REG_WRITE(c, CTL_LAYER_EXT3(mixer_id), 0);
379 DPU_REG_WRITE(c, CTL_FETCH_PIPE_ACTIVE, 0);
382 struct ctl_blend_config {
383 int idx, shift, ext_shift;
386 static const struct ctl_blend_config ctl_blend_config[][2] = {
387 [SSPP_NONE] = { { -1 }, { -1 } },
388 [SSPP_MAX] = { { -1 }, { -1 } },
389 [SSPP_VIG0] = { { 0, 0, 0 }, { 3, 0 } },
390 [SSPP_VIG1] = { { 0, 3, 2 }, { 3, 4 } },
391 [SSPP_VIG2] = { { 0, 6, 4 }, { 3, 8 } },
392 [SSPP_VIG3] = { { 0, 26, 6 }, { 3, 12 } },
393 [SSPP_RGB0] = { { 0, 9, 8 }, { -1 } },
394 [SSPP_RGB1] = { { 0, 12, 10 }, { -1 } },
395 [SSPP_RGB2] = { { 0, 15, 12 }, { -1 } },
396 [SSPP_RGB3] = { { 0, 29, 14 }, { -1 } },
397 [SSPP_DMA0] = { { 0, 18, 16 }, { 2, 8 } },
398 [SSPP_DMA1] = { { 0, 21, 18 }, { 2, 12 } },
399 [SSPP_DMA2] = { { 2, 0 }, { 2, 16 } },
400 [SSPP_DMA3] = { { 2, 4 }, { 2, 20 } },
401 [SSPP_DMA4] = { { 4, 0 }, { 4, 8 } },
402 [SSPP_DMA5] = { { 4, 4 }, { 4, 12 } },
403 [SSPP_CURSOR0] = { { 1, 20 }, { -1 } },
404 [SSPP_CURSOR1] = { { 1, 26 }, { -1 } },
407 static void dpu_hw_ctl_setup_blendstage(struct dpu_hw_ctl *ctx,
408 enum dpu_lm lm, struct dpu_hw_stage_cfg *stage_cfg)
410 struct dpu_hw_blk_reg_map *c = &ctx->hw;
411 u32 mix, ext, mix_ext;
412 u32 mixercfg[5] = { 0 };
417 stages = _mixer_stages(ctx->mixer_hw_caps, ctx->mixer_count, lm);
421 if (test_bit(DPU_MIXER_SOURCESPLIT,
422 &ctx->mixer_hw_caps->features))
423 pipes_per_stage = PIPES_PER_STAGE;
427 mixercfg[0] = CTL_MIXER_BORDER_OUT; /* always set BORDER_OUT */
432 for (i = 0; i <= stages; i++) {
433 /* overflow to ext register if 'i + 1 > 7' */
436 mix_ext = (i + 1) & 0xf;
438 for (j = 0 ; j < pipes_per_stage; j++) {
439 enum dpu_sspp_multirect_index rect_index =
440 stage_cfg->multirect_index[i][j];
441 enum dpu_sspp pipe = stage_cfg->stage[i][j];
442 const struct ctl_blend_config *cfg =
443 &ctl_blend_config[pipe][rect_index == DPU_SSPP_RECT_1];
446 * CTL_LAYER has 3-bit field (and extra bits in EXT register),
447 * all EXT registers has 4-bit fields.
449 if (cfg->idx == -1) {
451 } else if (cfg->idx == 0) {
452 mixercfg[0] |= mix << cfg->shift;
453 mixercfg[1] |= ext << cfg->ext_shift;
455 mixercfg[cfg->idx] |= mix_ext << cfg->shift;
461 DPU_REG_WRITE(c, CTL_LAYER(lm), mixercfg[0]);
462 DPU_REG_WRITE(c, CTL_LAYER_EXT(lm), mixercfg[1]);
463 DPU_REG_WRITE(c, CTL_LAYER_EXT2(lm), mixercfg[2]);
464 DPU_REG_WRITE(c, CTL_LAYER_EXT3(lm), mixercfg[3]);
465 if ((test_bit(DPU_CTL_HAS_LAYER_EXT4, &ctx->caps->features)))
466 DPU_REG_WRITE(c, CTL_LAYER_EXT4(lm), mixercfg[4]);
470 static void dpu_hw_ctl_intf_cfg_v1(struct dpu_hw_ctl *ctx,
471 struct dpu_hw_intf_cfg *cfg)
473 struct dpu_hw_blk_reg_map *c = &ctx->hw;
478 /* CTL_TOP[31:28] carries group_id to collate CTL paths
479 * per VM. Explicitly disable it until VM support is
480 * added in SW. Power on reset value is not disable.
482 if ((test_bit(DPU_CTL_VM_CFG, &ctx->caps->features)))
483 mode_sel = CTL_DEFAULT_GROUP_ID << 28;
486 DPU_REG_WRITE(&ctx->hw, CTL_DSC_FLUSH, cfg->dsc);
488 if (cfg->intf_mode_sel == DPU_CTL_MODE_SEL_CMD)
491 intf_active = DPU_REG_READ(c, CTL_INTF_ACTIVE);
492 wb_active = DPU_REG_READ(c, CTL_WB_ACTIVE);
495 intf_active |= BIT(cfg->intf - INTF_0);
498 wb_active |= BIT(cfg->wb - WB_0);
500 DPU_REG_WRITE(c, CTL_TOP, mode_sel);
501 DPU_REG_WRITE(c, CTL_INTF_ACTIVE, intf_active);
502 DPU_REG_WRITE(c, CTL_WB_ACTIVE, wb_active);
505 DPU_REG_WRITE(c, CTL_MERGE_3D_ACTIVE,
506 BIT(cfg->merge_3d - MERGE_3D_0));
508 DPU_REG_WRITE(&ctx->hw, CTL_FLUSH, DSC_IDX);
509 DPU_REG_WRITE(c, CTL_DSC_ACTIVE, cfg->dsc);
513 static void dpu_hw_ctl_intf_cfg(struct dpu_hw_ctl *ctx,
514 struct dpu_hw_intf_cfg *cfg)
516 struct dpu_hw_blk_reg_map *c = &ctx->hw;
519 intf_cfg |= (cfg->intf & 0xF) << 4;
523 intf_cfg |= (cfg->mode_3d - 0x1) << 20;
527 intf_cfg |= (cfg->wb & 0x3) + 2;
529 switch (cfg->intf_mode_sel) {
530 case DPU_CTL_MODE_SEL_VID:
531 intf_cfg &= ~BIT(17);
532 intf_cfg &= ~(0x3 << 15);
534 case DPU_CTL_MODE_SEL_CMD:
536 intf_cfg |= ((cfg->stream_sel & 0x3) << 15);
539 pr_err("unknown interface type %d\n", cfg->intf_mode_sel);
543 DPU_REG_WRITE(c, CTL_TOP, intf_cfg);
546 static void dpu_hw_ctl_reset_intf_cfg_v1(struct dpu_hw_ctl *ctx,
547 struct dpu_hw_intf_cfg *cfg)
549 struct dpu_hw_blk_reg_map *c = &ctx->hw;
552 u32 merge3d_active = 0;
555 * This API resets each portion of the CTL path namely,
556 * clearing the sspps staged on the lm, merge_3d block,
557 * interfaces , writeback etc to ensure clean teardown of the pipeline.
558 * This will be used for writeback to begin with to have a
559 * proper teardown of the writeback session but upon further
560 * validation, this can be extended to all interfaces.
563 merge3d_active = DPU_REG_READ(c, CTL_MERGE_3D_ACTIVE);
564 merge3d_active &= ~BIT(cfg->merge_3d - MERGE_3D_0);
565 DPU_REG_WRITE(c, CTL_MERGE_3D_ACTIVE,
569 dpu_hw_ctl_clear_all_blendstages(ctx);
572 intf_active = DPU_REG_READ(c, CTL_INTF_ACTIVE);
573 intf_active &= ~BIT(cfg->intf - INTF_0);
574 DPU_REG_WRITE(c, CTL_INTF_ACTIVE, intf_active);
578 wb_active = DPU_REG_READ(c, CTL_WB_ACTIVE);
579 wb_active &= ~BIT(cfg->wb - WB_0);
580 DPU_REG_WRITE(c, CTL_WB_ACTIVE, wb_active);
584 static void dpu_hw_ctl_set_fetch_pipe_active(struct dpu_hw_ctl *ctx,
585 unsigned long *fetch_active)
591 for (i = 0; i < SSPP_MAX; i++) {
592 if (test_bit(i, fetch_active) &&
593 fetch_tbl[i] != CTL_INVALID_BIT)
594 val |= BIT(fetch_tbl[i]);
598 DPU_REG_WRITE(&ctx->hw, CTL_FETCH_PIPE_ACTIVE, val);
601 static void _setup_ctl_ops(struct dpu_hw_ctl_ops *ops,
604 if (cap & BIT(DPU_CTL_ACTIVE_CFG)) {
605 ops->trigger_flush = dpu_hw_ctl_trigger_flush_v1;
606 ops->setup_intf_cfg = dpu_hw_ctl_intf_cfg_v1;
607 ops->reset_intf_cfg = dpu_hw_ctl_reset_intf_cfg_v1;
608 ops->update_pending_flush_intf =
609 dpu_hw_ctl_update_pending_flush_intf_v1;
610 ops->update_pending_flush_merge_3d =
611 dpu_hw_ctl_update_pending_flush_merge_3d_v1;
612 ops->update_pending_flush_wb = dpu_hw_ctl_update_pending_flush_wb_v1;
614 ops->trigger_flush = dpu_hw_ctl_trigger_flush;
615 ops->setup_intf_cfg = dpu_hw_ctl_intf_cfg;
616 ops->update_pending_flush_intf =
617 dpu_hw_ctl_update_pending_flush_intf;
618 ops->update_pending_flush_wb = dpu_hw_ctl_update_pending_flush_wb;
620 ops->clear_pending_flush = dpu_hw_ctl_clear_pending_flush;
621 ops->update_pending_flush = dpu_hw_ctl_update_pending_flush;
622 ops->get_pending_flush = dpu_hw_ctl_get_pending_flush;
623 ops->get_flush_register = dpu_hw_ctl_get_flush_register;
624 ops->trigger_start = dpu_hw_ctl_trigger_start;
625 ops->is_started = dpu_hw_ctl_is_started;
626 ops->trigger_pending = dpu_hw_ctl_trigger_pending;
627 ops->reset = dpu_hw_ctl_reset_control;
628 ops->wait_reset_status = dpu_hw_ctl_wait_reset_status;
629 ops->clear_all_blendstages = dpu_hw_ctl_clear_all_blendstages;
630 ops->setup_blendstage = dpu_hw_ctl_setup_blendstage;
631 ops->update_pending_flush_sspp = dpu_hw_ctl_update_pending_flush_sspp;
632 ops->update_pending_flush_mixer = dpu_hw_ctl_update_pending_flush_mixer;
633 ops->update_pending_flush_dspp = dpu_hw_ctl_update_pending_flush_dspp;
634 if (cap & BIT(DPU_CTL_FETCH_ACTIVE))
635 ops->set_active_pipes = dpu_hw_ctl_set_fetch_pipe_active;
638 struct dpu_hw_ctl *dpu_hw_ctl_init(enum dpu_ctl idx,
640 const struct dpu_mdss_cfg *m)
642 struct dpu_hw_ctl *c;
643 const struct dpu_ctl_cfg *cfg;
645 c = kzalloc(sizeof(*c), GFP_KERNEL);
647 return ERR_PTR(-ENOMEM);
649 cfg = _ctl_offset(idx, m, addr, &c->hw);
650 if (IS_ERR_OR_NULL(cfg)) {
652 pr_err("failed to create dpu_hw_ctl %d\n", idx);
653 return ERR_PTR(-EINVAL);
657 _setup_ctl_ops(&c->ops, c->caps->features);
659 c->mixer_count = m->mixer_count;
660 c->mixer_hw_caps = m->mixer;
665 void dpu_hw_ctl_destroy(struct dpu_hw_ctl *ctx)