drm/amd/powerplay: Fix psm_set_user_performance_state()
[linux-2.6-microblaze.git] / drivers / gpu / drm / amd / powerplay / hwmgr / pp_psm.c
1 /*
2  * Copyright 2017 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  */
23
24 #include <linux/types.h>
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
27 #include "pp_psm.h"
28
29 int psm_init_power_state_table(struct pp_hwmgr *hwmgr)
30 {
31         int result;
32         unsigned int i;
33         unsigned int table_entries;
34         struct pp_power_state *state;
35         int size;
36
37         if (hwmgr->hwmgr_func->get_num_of_pp_table_entries == NULL)
38                 return -EINVAL;
39
40         if (hwmgr->hwmgr_func->get_power_state_size == NULL)
41                 return -EINVAL;
42
43         hwmgr->num_ps = table_entries = hwmgr->hwmgr_func->get_num_of_pp_table_entries(hwmgr);
44
45         hwmgr->ps_size = size = hwmgr->hwmgr_func->get_power_state_size(hwmgr) +
46                                           sizeof(struct pp_power_state);
47
48         hwmgr->ps = kzalloc(size * table_entries, GFP_KERNEL);
49         if (hwmgr->ps == NULL)
50                 return -ENOMEM;
51
52         hwmgr->request_ps = kzalloc(size, GFP_KERNEL);
53         if (hwmgr->request_ps == NULL) {
54                 kfree(hwmgr->ps);
55                 hwmgr->ps = NULL;
56                 return -ENOMEM;
57         }
58
59         hwmgr->current_ps = kzalloc(size, GFP_KERNEL);
60         if (hwmgr->current_ps == NULL) {
61                 kfree(hwmgr->request_ps);
62                 kfree(hwmgr->ps);
63                 hwmgr->request_ps = NULL;
64                 hwmgr->ps = NULL;
65                 return -ENOMEM;
66         }
67
68         state = hwmgr->ps;
69
70         for (i = 0; i < table_entries; i++) {
71                 result = hwmgr->hwmgr_func->get_pp_table_entry(hwmgr, i, state);
72
73                 if (state->classification.flags & PP_StateClassificationFlag_Boot) {
74                         hwmgr->boot_ps = state;
75                         memcpy(hwmgr->current_ps, state, size);
76                         memcpy(hwmgr->request_ps, state, size);
77                 }
78
79                 state->id = i + 1; /* assigned unique num for every power state id */
80
81                 if (state->classification.flags & PP_StateClassificationFlag_Uvd)
82                         hwmgr->uvd_ps = state;
83                 state = (struct pp_power_state *)((unsigned long)state + size);
84         }
85
86         return 0;
87 }
88
89 int psm_fini_power_state_table(struct pp_hwmgr *hwmgr)
90 {
91         if (hwmgr == NULL)
92                 return -EINVAL;
93
94         kfree(hwmgr->current_ps);
95         kfree(hwmgr->request_ps);
96         kfree(hwmgr->ps);
97         hwmgr->request_ps = NULL;
98         hwmgr->ps = NULL;
99         hwmgr->current_ps = NULL;
100         return 0;
101 }
102
103 static int psm_get_ui_state(struct pp_hwmgr *hwmgr,
104                                 enum PP_StateUILabel ui_label,
105                                 unsigned long *state_id)
106 {
107         struct pp_power_state *state;
108         int table_entries;
109         int i;
110
111         table_entries = hwmgr->num_ps;
112         state = hwmgr->ps;
113
114         for (i = 0; i < table_entries; i++) {
115                 if (state->classification.ui_label & ui_label) {
116                         *state_id = state->id;
117                         return 0;
118                 }
119                 state = (struct pp_power_state *)((unsigned long)state + hwmgr->ps_size);
120         }
121         return -EINVAL;
122 }
123
124 static int psm_get_state_by_classification(struct pp_hwmgr *hwmgr,
125                                         enum PP_StateClassificationFlag flag,
126                                         unsigned long *state_id)
127 {
128         struct pp_power_state *state;
129         int table_entries;
130         int i;
131
132         table_entries = hwmgr->num_ps;
133         state = hwmgr->ps;
134
135         for (i = 0; i < table_entries; i++) {
136                 if (state->classification.flags & flag) {
137                         *state_id = state->id;
138                         return 0;
139                 }
140                 state = (struct pp_power_state *)((unsigned long)state + hwmgr->ps_size);
141         }
142         return -EINVAL;
143 }
144
145 static int psm_set_states(struct pp_hwmgr *hwmgr, unsigned long state_id)
146 {
147         struct pp_power_state *state;
148         int table_entries;
149         int i;
150
151         table_entries = hwmgr->num_ps;
152
153         state = hwmgr->ps;
154
155         for (i = 0; i < table_entries; i++) {
156                 if (state->id == state_id) {
157                         memcpy(hwmgr->request_ps, state, hwmgr->ps_size);
158                         return 0;
159                 }
160                 state = (struct pp_power_state *)((unsigned long)state + hwmgr->ps_size);
161         }
162         return -EINVAL;
163 }
164
165 int psm_set_boot_states(struct pp_hwmgr *hwmgr)
166 {
167         unsigned long state_id;
168         int ret = -EINVAL;
169
170         if (!psm_get_state_by_classification(hwmgr, PP_StateClassificationFlag_Boot,
171                                         &state_id))
172                 ret = psm_set_states(hwmgr, state_id);
173
174         return ret;
175 }
176
177 int psm_set_performance_states(struct pp_hwmgr *hwmgr)
178 {
179         unsigned long state_id;
180         int ret = -EINVAL;
181
182         if (!psm_get_ui_state(hwmgr, PP_StateUILabel_Performance,
183                                         &state_id))
184                 ret = psm_set_states(hwmgr, state_id);
185
186         return ret;
187 }
188
189 int psm_set_user_performance_state(struct pp_hwmgr *hwmgr,
190                                         enum PP_StateUILabel label_id,
191                                         struct pp_power_state **state)
192 {
193         int table_entries;
194         int i;
195
196         table_entries = hwmgr->num_ps;
197         *state = hwmgr->ps;
198
199 restart_search:
200         for (i = 0; i < table_entries; i++) {
201                 if ((*state)->classification.ui_label & label_id)
202                         return 0;
203                 *state = (struct pp_power_state *)((uintptr_t)*state + hwmgr->ps_size);
204         }
205
206         switch (label_id) {
207         case PP_StateUILabel_Battery:
208         case PP_StateUILabel_Balanced:
209                 label_id = PP_StateUILabel_Performance;
210                 goto restart_search;
211         default:
212                 break;
213         }
214         return -EINVAL;
215 }
216
217 int psm_adjust_power_state_dynamic(struct pp_hwmgr *hwmgr, bool skip,
218                                                 struct pp_power_state *new_ps)
219 {
220         struct pp_power_state *pcurrent;
221         struct pp_power_state *requested;
222         bool equal;
223
224         if (skip)
225                 return 0;
226
227         if (new_ps != NULL)
228                 requested = new_ps;
229         else
230                 requested = hwmgr->request_ps;
231
232         pcurrent = hwmgr->current_ps;
233
234         phm_apply_state_adjust_rules(hwmgr, requested, pcurrent);
235
236         if (pcurrent == NULL || (0 != phm_check_states_equal(hwmgr,
237                         &pcurrent->hardware, &requested->hardware, &equal)))
238                 equal = false;
239
240         if (!equal || phm_check_smc_update_required_for_display_configuration(hwmgr)) {
241                 phm_set_power_state(hwmgr, &pcurrent->hardware, &requested->hardware);
242                 memcpy(hwmgr->current_ps, hwmgr->request_ps, hwmgr->ps_size);
243         }
244         return 0;
245 }
246