1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
5 #include <linux/delay.h>
7 #include "dpu_hw_ctl.h"
11 #define CTL_LAYER(lm) \
12 (((lm) == LM_5) ? (0x024) : (((lm) - LM_0) * 0x004))
13 #define CTL_LAYER_EXT(lm) \
14 (0x40 + (((lm) - LM_0) * 0x004))
15 #define CTL_LAYER_EXT2(lm) \
16 (0x70 + (((lm) - LM_0) * 0x004))
17 #define CTL_LAYER_EXT3(lm) \
18 (0xA0 + (((lm) - LM_0) * 0x004))
20 #define CTL_FLUSH 0x018
21 #define CTL_START 0x01C
22 #define CTL_PREPARE 0x0d0
23 #define CTL_SW_RESET 0x030
24 #define CTL_LAYER_EXTN_OFFSET 0x40
25 #define CTL_MERGE_3D_ACTIVE 0x0E4
26 #define CTL_INTF_ACTIVE 0x0F4
27 #define CTL_MERGE_3D_FLUSH 0x100
28 #define CTL_INTF_FLUSH 0x110
29 #define CTL_INTF_MASTER 0x134
30 #define CTL_FETCH_PIPE_ACTIVE 0x0FC
32 #define CTL_MIXER_BORDER_OUT BIT(24)
33 #define CTL_FLUSH_MASK_CTL BIT(17)
35 #define DPU_REG_RESET_TIMEOUT_US 2000
36 #define MERGE_3D_IDX 23
38 #define CTL_INVALID_BIT 0xffff
40 static const u32 fetch_tbl[SSPP_MAX] = {CTL_INVALID_BIT, 16, 17, 18, 19,
41 CTL_INVALID_BIT, CTL_INVALID_BIT, CTL_INVALID_BIT, CTL_INVALID_BIT, 0,
42 1, 2, 3, CTL_INVALID_BIT, CTL_INVALID_BIT};
44 static const struct dpu_ctl_cfg *_ctl_offset(enum dpu_ctl ctl,
45 const struct dpu_mdss_cfg *m,
47 struct dpu_hw_blk_reg_map *b)
51 for (i = 0; i < m->ctl_count; i++) {
52 if (ctl == m->ctl[i].id) {
54 b->blk_off = m->ctl[i].base;
55 b->length = m->ctl[i].len;
56 b->hwversion = m->hwversion;
57 b->log_mask = DPU_DBG_MASK_CTL;
61 return ERR_PTR(-ENOMEM);
64 static int _mixer_stages(const struct dpu_lm_cfg *mixer, int count,
70 for (i = 0; i < count; i++) {
71 if (lm == mixer[i].id) {
72 stages = mixer[i].sblk->maxblendstages;
80 static inline u32 dpu_hw_ctl_get_flush_register(struct dpu_hw_ctl *ctx)
82 struct dpu_hw_blk_reg_map *c = &ctx->hw;
84 return DPU_REG_READ(c, CTL_FLUSH);
87 static inline void dpu_hw_ctl_trigger_start(struct dpu_hw_ctl *ctx)
89 trace_dpu_hw_ctl_trigger_start(ctx->pending_flush_mask,
90 dpu_hw_ctl_get_flush_register(ctx));
91 DPU_REG_WRITE(&ctx->hw, CTL_START, 0x1);
94 static inline void dpu_hw_ctl_trigger_pending(struct dpu_hw_ctl *ctx)
96 trace_dpu_hw_ctl_trigger_prepare(ctx->pending_flush_mask,
97 dpu_hw_ctl_get_flush_register(ctx));
98 DPU_REG_WRITE(&ctx->hw, CTL_PREPARE, 0x1);
101 static inline void dpu_hw_ctl_clear_pending_flush(struct dpu_hw_ctl *ctx)
103 trace_dpu_hw_ctl_clear_pending_flush(ctx->pending_flush_mask,
104 dpu_hw_ctl_get_flush_register(ctx));
105 ctx->pending_flush_mask = 0x0;
108 static inline void dpu_hw_ctl_update_pending_flush(struct dpu_hw_ctl *ctx,
111 trace_dpu_hw_ctl_update_pending_flush(flushbits,
112 ctx->pending_flush_mask);
113 ctx->pending_flush_mask |= flushbits;
116 static u32 dpu_hw_ctl_get_pending_flush(struct dpu_hw_ctl *ctx)
118 return ctx->pending_flush_mask;
121 static inline void dpu_hw_ctl_trigger_flush_v1(struct dpu_hw_ctl *ctx)
124 if (ctx->pending_flush_mask & BIT(MERGE_3D_IDX))
125 DPU_REG_WRITE(&ctx->hw, CTL_MERGE_3D_FLUSH,
126 ctx->pending_merge_3d_flush_mask);
127 if (ctx->pending_flush_mask & BIT(INTF_IDX))
128 DPU_REG_WRITE(&ctx->hw, CTL_INTF_FLUSH,
129 ctx->pending_intf_flush_mask);
131 DPU_REG_WRITE(&ctx->hw, CTL_FLUSH, ctx->pending_flush_mask);
134 static inline void dpu_hw_ctl_trigger_flush(struct dpu_hw_ctl *ctx)
136 trace_dpu_hw_ctl_trigger_pending_flush(ctx->pending_flush_mask,
137 dpu_hw_ctl_get_flush_register(ctx));
138 DPU_REG_WRITE(&ctx->hw, CTL_FLUSH, ctx->pending_flush_mask);
141 static uint32_t dpu_hw_ctl_get_bitmask_sspp(struct dpu_hw_ctl *ctx,
144 uint32_t flushbits = 0;
196 static uint32_t dpu_hw_ctl_get_bitmask_mixer(struct dpu_hw_ctl *ctx,
199 uint32_t flushbits = 0;
224 flushbits |= CTL_FLUSH_MASK_CTL;
229 static void dpu_hw_ctl_update_pending_flush_intf(struct dpu_hw_ctl *ctx,
234 ctx->pending_flush_mask |= BIT(31);
237 ctx->pending_flush_mask |= BIT(30);
240 ctx->pending_flush_mask |= BIT(29);
243 ctx->pending_flush_mask |= BIT(28);
250 static void dpu_hw_ctl_update_pending_flush_intf_v1(struct dpu_hw_ctl *ctx,
253 ctx->pending_intf_flush_mask |= BIT(intf - INTF_0);
254 ctx->pending_flush_mask |= BIT(INTF_IDX);
257 static void dpu_hw_ctl_update_pending_flush_merge_3d_v1(struct dpu_hw_ctl *ctx,
258 enum dpu_merge_3d merge_3d)
260 ctx->pending_merge_3d_flush_mask |= BIT(merge_3d - MERGE_3D_0);
261 ctx->pending_flush_mask |= BIT(MERGE_3D_IDX);
264 static uint32_t dpu_hw_ctl_get_bitmask_dspp(struct dpu_hw_ctl *ctx,
267 uint32_t flushbits = 0;
289 static u32 dpu_hw_ctl_poll_reset_status(struct dpu_hw_ctl *ctx, u32 timeout_us)
291 struct dpu_hw_blk_reg_map *c = &ctx->hw;
295 timeout = ktime_add_us(ktime_get(), timeout_us);
298 * it takes around 30us to have mdp finish resetting its ctl path
299 * poll every 50us so that reset should be completed at 1st poll
302 status = DPU_REG_READ(c, CTL_SW_RESET);
305 usleep_range(20, 50);
306 } while (status && ktime_compare_safe(ktime_get(), timeout) < 0);
311 static int dpu_hw_ctl_reset_control(struct dpu_hw_ctl *ctx)
313 struct dpu_hw_blk_reg_map *c = &ctx->hw;
315 pr_debug("issuing hw ctl reset for ctl:%d\n", ctx->idx);
316 DPU_REG_WRITE(c, CTL_SW_RESET, 0x1);
317 if (dpu_hw_ctl_poll_reset_status(ctx, DPU_REG_RESET_TIMEOUT_US))
323 static int dpu_hw_ctl_wait_reset_status(struct dpu_hw_ctl *ctx)
325 struct dpu_hw_blk_reg_map *c = &ctx->hw;
328 status = DPU_REG_READ(c, CTL_SW_RESET);
333 pr_debug("hw ctl reset is set for ctl:%d\n", ctx->idx);
334 if (dpu_hw_ctl_poll_reset_status(ctx, DPU_REG_RESET_TIMEOUT_US)) {
335 pr_err("hw recovery is not complete for ctl:%d\n", ctx->idx);
342 static void dpu_hw_ctl_clear_all_blendstages(struct dpu_hw_ctl *ctx)
344 struct dpu_hw_blk_reg_map *c = &ctx->hw;
347 for (i = 0; i < ctx->mixer_count; i++) {
348 enum dpu_lm mixer_id = ctx->mixer_hw_caps[i].id;
350 DPU_REG_WRITE(c, CTL_LAYER(mixer_id), 0);
351 DPU_REG_WRITE(c, CTL_LAYER_EXT(mixer_id), 0);
352 DPU_REG_WRITE(c, CTL_LAYER_EXT2(mixer_id), 0);
353 DPU_REG_WRITE(c, CTL_LAYER_EXT3(mixer_id), 0);
356 DPU_REG_WRITE(c, CTL_FETCH_PIPE_ACTIVE, 0);
359 static void dpu_hw_ctl_setup_blendstage(struct dpu_hw_ctl *ctx,
360 enum dpu_lm lm, struct dpu_hw_stage_cfg *stage_cfg)
362 struct dpu_hw_blk_reg_map *c = &ctx->hw;
363 u32 mixercfg = 0, mixercfg_ext = 0, mix, ext;
364 u32 mixercfg_ext2 = 0, mixercfg_ext3 = 0;
369 stages = _mixer_stages(ctx->mixer_hw_caps, ctx->mixer_count, lm);
373 if (test_bit(DPU_MIXER_SOURCESPLIT,
374 &ctx->mixer_hw_caps->features))
375 pipes_per_stage = PIPES_PER_STAGE;
379 mixercfg = CTL_MIXER_BORDER_OUT; /* always set BORDER_OUT */
384 for (i = 0; i <= stages; i++) {
385 /* overflow to ext register if 'i + 1 > 7' */
389 for (j = 0 ; j < pipes_per_stage; j++) {
390 enum dpu_sspp_multirect_index rect_index =
391 stage_cfg->multirect_index[i][j];
393 switch (stage_cfg->stage[i][j]) {
395 if (rect_index == DPU_SSPP_RECT_1) {
396 mixercfg_ext3 |= ((i + 1) & 0xF) << 0;
398 mixercfg |= mix << 0;
399 mixercfg_ext |= ext << 0;
403 if (rect_index == DPU_SSPP_RECT_1) {
404 mixercfg_ext3 |= ((i + 1) & 0xF) << 4;
406 mixercfg |= mix << 3;
407 mixercfg_ext |= ext << 2;
411 if (rect_index == DPU_SSPP_RECT_1) {
412 mixercfg_ext3 |= ((i + 1) & 0xF) << 8;
414 mixercfg |= mix << 6;
415 mixercfg_ext |= ext << 4;
419 if (rect_index == DPU_SSPP_RECT_1) {
420 mixercfg_ext3 |= ((i + 1) & 0xF) << 12;
422 mixercfg |= mix << 26;
423 mixercfg_ext |= ext << 6;
427 mixercfg |= mix << 9;
428 mixercfg_ext |= ext << 8;
431 mixercfg |= mix << 12;
432 mixercfg_ext |= ext << 10;
435 mixercfg |= mix << 15;
436 mixercfg_ext |= ext << 12;
439 mixercfg |= mix << 29;
440 mixercfg_ext |= ext << 14;
443 if (rect_index == DPU_SSPP_RECT_1) {
444 mixercfg_ext2 |= ((i + 1) & 0xF) << 8;
446 mixercfg |= mix << 18;
447 mixercfg_ext |= ext << 16;
451 if (rect_index == DPU_SSPP_RECT_1) {
452 mixercfg_ext2 |= ((i + 1) & 0xF) << 12;
454 mixercfg |= mix << 21;
455 mixercfg_ext |= ext << 18;
459 if (rect_index == DPU_SSPP_RECT_1) {
460 mixercfg_ext2 |= ((i + 1) & 0xF) << 16;
462 mix |= (i + 1) & 0xF;
463 mixercfg_ext2 |= mix << 0;
467 if (rect_index == DPU_SSPP_RECT_1) {
468 mixercfg_ext2 |= ((i + 1) & 0xF) << 20;
470 mix |= (i + 1) & 0xF;
471 mixercfg_ext2 |= mix << 4;
475 mixercfg_ext |= ((i + 1) & 0xF) << 20;
478 mixercfg_ext |= ((i + 1) & 0xF) << 26;
487 DPU_REG_WRITE(c, CTL_LAYER(lm), mixercfg);
488 DPU_REG_WRITE(c, CTL_LAYER_EXT(lm), mixercfg_ext);
489 DPU_REG_WRITE(c, CTL_LAYER_EXT2(lm), mixercfg_ext2);
490 DPU_REG_WRITE(c, CTL_LAYER_EXT3(lm), mixercfg_ext3);
494 static void dpu_hw_ctl_intf_cfg_v1(struct dpu_hw_ctl *ctx,
495 struct dpu_hw_intf_cfg *cfg)
497 struct dpu_hw_blk_reg_map *c = &ctx->hw;
501 if (cfg->intf_mode_sel == DPU_CTL_MODE_SEL_CMD)
504 intf_active = DPU_REG_READ(c, CTL_INTF_ACTIVE);
505 intf_active |= BIT(cfg->intf - INTF_0);
507 DPU_REG_WRITE(c, CTL_TOP, mode_sel);
508 DPU_REG_WRITE(c, CTL_INTF_ACTIVE, intf_active);
510 DPU_REG_WRITE(c, CTL_MERGE_3D_ACTIVE,
511 BIT(cfg->merge_3d - MERGE_3D_0));
514 static void dpu_hw_ctl_intf_cfg(struct dpu_hw_ctl *ctx,
515 struct dpu_hw_intf_cfg *cfg)
517 struct dpu_hw_blk_reg_map *c = &ctx->hw;
520 intf_cfg |= (cfg->intf & 0xF) << 4;
524 intf_cfg |= (cfg->mode_3d - 0x1) << 20;
527 switch (cfg->intf_mode_sel) {
528 case DPU_CTL_MODE_SEL_VID:
529 intf_cfg &= ~BIT(17);
530 intf_cfg &= ~(0x3 << 15);
532 case DPU_CTL_MODE_SEL_CMD:
534 intf_cfg |= ((cfg->stream_sel & 0x3) << 15);
537 pr_err("unknown interface type %d\n", cfg->intf_mode_sel);
541 DPU_REG_WRITE(c, CTL_TOP, intf_cfg);
544 static void dpu_hw_ctl_set_fetch_pipe_active(struct dpu_hw_ctl *ctx,
545 unsigned long *fetch_active)
551 for (i = 0; i < SSPP_MAX; i++) {
552 if (test_bit(i, fetch_active) &&
553 fetch_tbl[i] != CTL_INVALID_BIT)
554 val |= BIT(fetch_tbl[i]);
558 DPU_REG_WRITE(&ctx->hw, CTL_FETCH_PIPE_ACTIVE, val);
561 static void _setup_ctl_ops(struct dpu_hw_ctl_ops *ops,
564 if (cap & BIT(DPU_CTL_ACTIVE_CFG)) {
565 ops->trigger_flush = dpu_hw_ctl_trigger_flush_v1;
566 ops->setup_intf_cfg = dpu_hw_ctl_intf_cfg_v1;
567 ops->update_pending_flush_intf =
568 dpu_hw_ctl_update_pending_flush_intf_v1;
569 ops->update_pending_flush_merge_3d =
570 dpu_hw_ctl_update_pending_flush_merge_3d_v1;
572 ops->trigger_flush = dpu_hw_ctl_trigger_flush;
573 ops->setup_intf_cfg = dpu_hw_ctl_intf_cfg;
574 ops->update_pending_flush_intf =
575 dpu_hw_ctl_update_pending_flush_intf;
577 ops->clear_pending_flush = dpu_hw_ctl_clear_pending_flush;
578 ops->update_pending_flush = dpu_hw_ctl_update_pending_flush;
579 ops->get_pending_flush = dpu_hw_ctl_get_pending_flush;
580 ops->get_flush_register = dpu_hw_ctl_get_flush_register;
581 ops->trigger_start = dpu_hw_ctl_trigger_start;
582 ops->trigger_pending = dpu_hw_ctl_trigger_pending;
583 ops->reset = dpu_hw_ctl_reset_control;
584 ops->wait_reset_status = dpu_hw_ctl_wait_reset_status;
585 ops->clear_all_blendstages = dpu_hw_ctl_clear_all_blendstages;
586 ops->setup_blendstage = dpu_hw_ctl_setup_blendstage;
587 ops->get_bitmask_sspp = dpu_hw_ctl_get_bitmask_sspp;
588 ops->get_bitmask_mixer = dpu_hw_ctl_get_bitmask_mixer;
589 ops->get_bitmask_dspp = dpu_hw_ctl_get_bitmask_dspp;
590 if (cap & BIT(DPU_CTL_FETCH_ACTIVE))
591 ops->set_active_pipes = dpu_hw_ctl_set_fetch_pipe_active;
594 struct dpu_hw_ctl *dpu_hw_ctl_init(enum dpu_ctl idx,
596 const struct dpu_mdss_cfg *m)
598 struct dpu_hw_ctl *c;
599 const struct dpu_ctl_cfg *cfg;
601 c = kzalloc(sizeof(*c), GFP_KERNEL);
603 return ERR_PTR(-ENOMEM);
605 cfg = _ctl_offset(idx, m, addr, &c->hw);
606 if (IS_ERR_OR_NULL(cfg)) {
608 pr_err("failed to create dpu_hw_ctl %d\n", idx);
609 return ERR_PTR(-EINVAL);
613 _setup_ctl_ops(&c->ops, c->caps->features);
615 c->mixer_count = m->mixer_count;
616 c->mixer_hw_caps = m->mixer;
621 void dpu_hw_ctl_destroy(struct dpu_hw_ctl *ctx)