drm/amd/display: Implement DTBCLK ref switching on dcn32
[linux-2.6-microblaze.git] / drivers / gpu / drm / amd / display / dc / clk_mgr / dcn32 / dcn32_clk_mgr_smu_msg.c
1 /*
2  * Copyright 2021 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: AMD
23  *
24  */
25
26 #include "dcn32_clk_mgr_smu_msg.h"
27
28 #include "clk_mgr_internal.h"
29 #include "reg_helper.h"
30 #include "dalsmc.h"
31 #include "smu13_driver_if.h"
32
33 #define mmDAL_MSG_REG  0x1628A
34 #define mmDAL_ARG_REG  0x16273
35 #define mmDAL_RESP_REG 0x16274
36
37 #define DALSMC_MSG_TransferTableDram2Smu          0x8
38
39 #define REG(reg_name) \
40         mm ## reg_name
41
42 #include "logger_types.h"
43
44 #define smu_print(str, ...) {DC_LOG_SMU(str, ##__VA_ARGS__); }
45
46
47 /*
48  * Function to be used instead of REG_WAIT macro because the wait ends when
49  * the register is NOT EQUAL to zero, and because the translation in msg_if.h
50  * won't work with REG_WAIT.
51  */
52 static uint32_t dcn32_smu_wait_for_response(struct clk_mgr_internal *clk_mgr, unsigned int delay_us, unsigned int max_retries)
53 {
54         uint32_t reg = 0;
55
56         do {
57                 reg = REG_READ(DAL_RESP_REG);
58                 if (reg)
59                         break;
60
61                 if (delay_us >= 1000)
62                         msleep(delay_us/1000);
63                 else if (delay_us > 0)
64                         udelay(delay_us);
65         } while (max_retries--);
66
67         return reg;
68 }
69
70 static bool dcn32_smu_send_msg_with_param(struct clk_mgr_internal *clk_mgr, uint32_t msg_id, uint32_t param_in, uint32_t *param_out)
71 {
72         /* Wait for response register to be ready */
73         dcn32_smu_wait_for_response(clk_mgr, 10, 200000);
74
75         /* Clear response register */
76         REG_WRITE(DAL_RESP_REG, 0);
77
78         /* Set the parameter register for the SMU message */
79         REG_WRITE(DAL_ARG_REG, param_in);
80
81         /* Trigger the message transaction by writing the message ID */
82         REG_WRITE(DAL_MSG_REG, msg_id);
83
84         /* Wait for response */
85         if (dcn32_smu_wait_for_response(clk_mgr, 10, 200000) == DALSMC_Result_OK) {
86                 if (param_out)
87                         *param_out = REG_READ(DAL_ARG_REG);
88
89                 return true;
90         }
91
92         return false;
93 }
94
95 void dcn32_smu_send_fclk_pstate_message(struct clk_mgr_internal *clk_mgr, bool enable)
96 {
97         smu_print("FCLK P-state support value is : %d\n", enable);
98
99         dcn32_smu_send_msg_with_param(clk_mgr,
100                         DALSMC_MSG_SetFclkSwitchAllow, enable ? FCLK_PSTATE_SUPPORTED : FCLK_PSTATE_NOTSUPPORTED, NULL);
101 }
102
103 void dcn32_smu_send_cab_for_uclk_message(struct clk_mgr_internal *clk_mgr, unsigned int num_ways)
104 {
105         smu_print("Numways for SubVP : %d\n", num_ways);
106
107         dcn32_smu_send_msg_with_param(clk_mgr, DALSMC_MSG_SetCabForUclkPstate, num_ways, NULL);
108 }
109
110 void dcn32_smu_transfer_wm_table_dram_2_smu(struct clk_mgr_internal *clk_mgr)
111 {
112         smu_print("SMU Transfer WM table DRAM 2 SMU\n");
113
114         dcn32_smu_send_msg_with_param(clk_mgr,
115                         DALSMC_MSG_TransferTableDram2Smu, TABLE_WATERMARKS, NULL);
116 }
117
118 /* Returns the actual frequency that was set in MHz, 0 on failure */
119 unsigned int dcn32_smu_set_hard_min_by_freq(struct clk_mgr_internal *clk_mgr, uint32_t clk, uint16_t freq_mhz)
120 {
121         uint32_t response = 0;
122
123         /* bits 23:16 for clock type, lower 16 bits for frequency in MHz */
124         uint32_t param = (clk << 16) | freq_mhz;
125
126         smu_print("SMU Set hard min by freq: clk = %d, freq_mhz = %d MHz\n", clk, freq_mhz);
127
128         dcn32_smu_send_msg_with_param(clk_mgr,
129                         DALSMC_MSG_SetHardMinByFreq, param, &response);
130
131         smu_print("SMU Frequency set = %d KHz\n", response);
132
133         return response;
134 }