2 * Copyright 2017 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
24 #include <linux/types.h>
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
29 int psm_init_power_state_table(struct pp_hwmgr *hwmgr)
33 unsigned int table_entries;
34 struct pp_power_state *state;
37 if (hwmgr->hwmgr_func->get_num_of_pp_table_entries == NULL)
40 if (hwmgr->hwmgr_func->get_power_state_size == NULL)
43 hwmgr->num_ps = table_entries = hwmgr->hwmgr_func->get_num_of_pp_table_entries(hwmgr);
45 hwmgr->ps_size = size = hwmgr->hwmgr_func->get_power_state_size(hwmgr) +
46 sizeof(struct pp_power_state);
48 hwmgr->ps = kzalloc(size * table_entries, GFP_KERNEL);
49 if (hwmgr->ps == NULL)
52 hwmgr->request_ps = kzalloc(size, GFP_KERNEL);
53 if (hwmgr->request_ps == NULL) {
59 hwmgr->current_ps = kzalloc(size, GFP_KERNEL);
60 if (hwmgr->current_ps == NULL) {
61 kfree(hwmgr->request_ps);
63 hwmgr->request_ps = NULL;
70 for (i = 0; i < table_entries; i++) {
71 result = hwmgr->hwmgr_func->get_pp_table_entry(hwmgr, i, state);
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);
79 state->id = i + 1; /* assigned unique num for every power state id */
81 if (state->classification.flags & PP_StateClassificationFlag_Uvd)
82 hwmgr->uvd_ps = state;
83 state = (struct pp_power_state *)((unsigned long)state + size);
89 int psm_fini_power_state_table(struct pp_hwmgr *hwmgr)
94 kfree(hwmgr->current_ps);
95 kfree(hwmgr->request_ps);
97 hwmgr->request_ps = NULL;
99 hwmgr->current_ps = NULL;
103 static int psm_get_ui_state(struct pp_hwmgr *hwmgr,
104 enum PP_StateUILabel ui_label,
105 unsigned long *state_id)
107 struct pp_power_state *state;
111 table_entries = hwmgr->num_ps;
114 for (i = 0; i < table_entries; i++) {
115 if (state->classification.ui_label & ui_label) {
116 *state_id = state->id;
119 state = (struct pp_power_state *)((unsigned long)state + hwmgr->ps_size);
124 static int psm_get_state_by_classification(struct pp_hwmgr *hwmgr,
125 enum PP_StateClassificationFlag flag,
126 unsigned long *state_id)
128 struct pp_power_state *state;
132 table_entries = hwmgr->num_ps;
135 for (i = 0; i < table_entries; i++) {
136 if (state->classification.flags & flag) {
137 *state_id = state->id;
140 state = (struct pp_power_state *)((unsigned long)state + hwmgr->ps_size);
145 static int psm_set_states(struct pp_hwmgr *hwmgr, unsigned long state_id)
147 struct pp_power_state *state;
151 table_entries = hwmgr->num_ps;
155 for (i = 0; i < table_entries; i++) {
156 if (state->id == state_id) {
157 memcpy(hwmgr->request_ps, state, hwmgr->ps_size);
160 state = (struct pp_power_state *)((unsigned long)state + hwmgr->ps_size);
165 int psm_set_boot_states(struct pp_hwmgr *hwmgr)
167 unsigned long state_id;
170 if (!psm_get_state_by_classification(hwmgr, PP_StateClassificationFlag_Boot,
172 ret = psm_set_states(hwmgr, state_id);
177 int psm_set_performance_states(struct pp_hwmgr *hwmgr)
179 unsigned long state_id;
182 if (!psm_get_ui_state(hwmgr, PP_StateUILabel_Performance,
184 ret = psm_set_states(hwmgr, state_id);
189 int psm_set_user_performance_state(struct pp_hwmgr *hwmgr,
190 enum PP_StateUILabel label_id,
191 struct pp_power_state **state)
196 table_entries = hwmgr->num_ps;
200 for (i = 0; i < table_entries; i++) {
201 if ((*state)->classification.ui_label & label_id)
203 *state = (struct pp_power_state *)((uintptr_t)*state + hwmgr->ps_size);
207 case PP_StateUILabel_Battery:
208 case PP_StateUILabel_Balanced:
209 label_id = PP_StateUILabel_Performance;
217 int psm_adjust_power_state_dynamic(struct pp_hwmgr *hwmgr, bool skip,
218 struct pp_power_state *new_ps)
220 struct pp_power_state *pcurrent;
221 struct pp_power_state *requested;
230 requested = hwmgr->request_ps;
232 pcurrent = hwmgr->current_ps;
234 phm_apply_state_adjust_rules(hwmgr, requested, pcurrent);
236 if (pcurrent == NULL || (0 != phm_check_states_equal(hwmgr,
237 &pcurrent->hardware, &requested->hardware, &equal)))
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);