blob: 1964a2add63fbef1ad5047ddb8e7ad9fd5fcd7cd [file] [log] [blame]
Alex Deucher1f7371b2015-12-02 17:46:21 -05001/*
2 * Copyright 2015 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#include <linux/types.h>
24#include <linux/kernel.h>
25#include <linux/gfp.h>
Jammy Zhouac885b32015-07-21 17:43:02 +080026#include <linux/slab.h>
Alex Deucher1f7371b2015-12-02 17:46:21 -050027#include "amd_shared.h"
28#include "amd_powerplay.h"
Jammy Zhouac885b32015-07-21 17:43:02 +080029#include "pp_instance.h"
Alex Deucher1f7371b2015-12-02 17:46:21 -050030
31static int pp_early_init(void *handle)
32{
33 return 0;
34}
35
36static int pp_sw_init(void *handle)
37{
Jammy Zhou3bace352015-07-21 21:18:15 +080038 struct pp_instance *pp_handle;
39 struct pp_hwmgr *hwmgr;
40 int ret = 0;
41
42 if (handle == NULL)
43 return -EINVAL;
44
45 pp_handle = (struct pp_instance *)handle;
46 hwmgr = pp_handle->hwmgr;
47
48 if (hwmgr == NULL || hwmgr->pptable_func == NULL ||
49 hwmgr->hwmgr_func == NULL ||
50 hwmgr->pptable_func->pptable_init == NULL ||
51 hwmgr->hwmgr_func->backend_init == NULL)
52 return -EINVAL;
53
54 ret = hwmgr->pptable_func->pptable_init(hwmgr);
Rex Zhue92a0372015-09-23 15:14:54 +080055
Jammy Zhou3bace352015-07-21 21:18:15 +080056 if (ret == 0)
57 ret = hwmgr->hwmgr_func->backend_init(hwmgr);
58
59 return ret;
Alex Deucher1f7371b2015-12-02 17:46:21 -050060}
61
62static int pp_sw_fini(void *handle)
63{
Jammy Zhou3bace352015-07-21 21:18:15 +080064 struct pp_instance *pp_handle;
65 struct pp_hwmgr *hwmgr;
66 int ret = 0;
67
68 if (handle == NULL)
69 return -EINVAL;
70
71 pp_handle = (struct pp_instance *)handle;
72 hwmgr = pp_handle->hwmgr;
73
74 if (hwmgr != NULL || hwmgr->hwmgr_func != NULL ||
75 hwmgr->hwmgr_func->backend_fini != NULL)
76 ret = hwmgr->hwmgr_func->backend_fini(hwmgr);
77
78 return ret;
Alex Deucher1f7371b2015-12-02 17:46:21 -050079}
80
81static int pp_hw_init(void *handle)
82{
Jammy Zhouac885b32015-07-21 17:43:02 +080083 struct pp_instance *pp_handle;
84 struct pp_smumgr *smumgr;
Rex Zhue92a0372015-09-23 15:14:54 +080085 struct pp_eventmgr *eventmgr;
Jammy Zhouac885b32015-07-21 17:43:02 +080086 int ret = 0;
87
88 if (handle == NULL)
89 return -EINVAL;
90
91 pp_handle = (struct pp_instance *)handle;
92 smumgr = pp_handle->smu_mgr;
93
94 if (smumgr == NULL || smumgr->smumgr_funcs == NULL ||
95 smumgr->smumgr_funcs->smu_init == NULL ||
96 smumgr->smumgr_funcs->start_smu == NULL)
97 return -EINVAL;
98
99 ret = smumgr->smumgr_funcs->smu_init(smumgr);
100 if (ret) {
101 printk(KERN_ERR "[ powerplay ] smc initialization failed\n");
102 return ret;
103 }
104
105 ret = smumgr->smumgr_funcs->start_smu(smumgr);
106 if (ret) {
107 printk(KERN_ERR "[ powerplay ] smc start failed\n");
108 smumgr->smumgr_funcs->smu_fini(smumgr);
109 return ret;
110 }
Jammy Zhou3bace352015-07-21 21:18:15 +0800111
Rex Zhue92a0372015-09-23 15:14:54 +0800112 hw_init_power_state_table(pp_handle->hwmgr);
113 eventmgr = pp_handle->eventmgr;
114
115 if (eventmgr == NULL || eventmgr->pp_eventmgr_init == NULL)
116 return -EINVAL;
117
118 ret = eventmgr->pp_eventmgr_init(eventmgr);
Alex Deucher1f7371b2015-12-02 17:46:21 -0500119 return 0;
120}
121
122static int pp_hw_fini(void *handle)
123{
Jammy Zhouac885b32015-07-21 17:43:02 +0800124 struct pp_instance *pp_handle;
125 struct pp_smumgr *smumgr;
Rex Zhue92a0372015-09-23 15:14:54 +0800126 struct pp_eventmgr *eventmgr;
Jammy Zhouac885b32015-07-21 17:43:02 +0800127
128 if (handle == NULL)
129 return -EINVAL;
130
131 pp_handle = (struct pp_instance *)handle;
Rex Zhue92a0372015-09-23 15:14:54 +0800132 eventmgr = pp_handle->eventmgr;
133
134 if (eventmgr != NULL || eventmgr->pp_eventmgr_fini != NULL)
135 eventmgr->pp_eventmgr_fini(eventmgr);
136
Jammy Zhouac885b32015-07-21 17:43:02 +0800137 smumgr = pp_handle->smu_mgr;
138
139 if (smumgr != NULL || smumgr->smumgr_funcs != NULL ||
140 smumgr->smumgr_funcs->smu_fini != NULL)
141 smumgr->smumgr_funcs->smu_fini(smumgr);
142
Alex Deucher1f7371b2015-12-02 17:46:21 -0500143 return 0;
144}
145
146static bool pp_is_idle(void *handle)
147{
148 return 0;
149}
150
151static int pp_wait_for_idle(void *handle)
152{
153 return 0;
154}
155
156static int pp_sw_reset(void *handle)
157{
158 return 0;
159}
160
161static void pp_print_status(void *handle)
162{
163
164}
165
166static int pp_set_clockgating_state(void *handle,
167 enum amd_clockgating_state state)
168{
169 return 0;
170}
171
172static int pp_set_powergating_state(void *handle,
173 enum amd_powergating_state state)
174{
175 return 0;
176}
177
178static int pp_suspend(void *handle)
179{
180 return 0;
181}
182
183static int pp_resume(void *handle)
184{
185 return 0;
186}
187
188const struct amd_ip_funcs pp_ip_funcs = {
189 .early_init = pp_early_init,
190 .late_init = NULL,
191 .sw_init = pp_sw_init,
192 .sw_fini = pp_sw_fini,
193 .hw_init = pp_hw_init,
194 .hw_fini = pp_hw_fini,
195 .suspend = pp_suspend,
196 .resume = pp_resume,
197 .is_idle = pp_is_idle,
198 .wait_for_idle = pp_wait_for_idle,
199 .soft_reset = pp_sw_reset,
200 .print_status = pp_print_status,
201 .set_clockgating_state = pp_set_clockgating_state,
202 .set_powergating_state = pp_set_powergating_state,
203};
204
205static int pp_dpm_load_fw(void *handle)
206{
207 return 0;
208}
209
210static int pp_dpm_fw_loading_complete(void *handle)
211{
212 return 0;
213}
214
215static int pp_dpm_force_performance_level(void *handle,
216 enum amd_dpm_forced_level level)
217{
218 return 0;
219}
220static enum amd_dpm_forced_level pp_dpm_get_performance_level(
221 void *handle)
222{
223 return 0;
224}
225static int pp_dpm_get_sclk(void *handle, bool low)
226{
227 return 0;
228}
229static int pp_dpm_get_mclk(void *handle, bool low)
230{
231 return 0;
232}
233static int pp_dpm_powergate_vce(void *handle, bool gate)
234{
235 return 0;
236}
237static int pp_dpm_powergate_uvd(void *handle, bool gate)
238{
239 return 0;
240}
241
242int pp_dpm_dispatch_tasks(void *handle, enum amd_pp_event event_id, void *input, void *output)
243{
244 return 0;
245}
246enum amd_pm_state_type pp_dpm_get_current_power_state(void *handle)
247{
248 return 0;
249}
250static void
251pp_debugfs_print_current_performance_level(void *handle,
252 struct seq_file *m)
253{
254 return;
255}
Jammy Zhou3bace352015-07-21 21:18:15 +0800256
Alex Deucher1f7371b2015-12-02 17:46:21 -0500257const struct amd_powerplay_funcs pp_dpm_funcs = {
258 .get_temperature = NULL,
259 .load_firmware = pp_dpm_load_fw,
260 .wait_for_fw_loading_complete = pp_dpm_fw_loading_complete,
261 .force_performance_level = pp_dpm_force_performance_level,
262 .get_performance_level = pp_dpm_get_performance_level,
263 .get_current_power_state = pp_dpm_get_current_power_state,
264 .get_sclk = pp_dpm_get_sclk,
265 .get_mclk = pp_dpm_get_mclk,
266 .powergate_vce = pp_dpm_powergate_vce,
267 .powergate_uvd = pp_dpm_powergate_uvd,
268 .dispatch_tasks = pp_dpm_dispatch_tasks,
269 .print_current_performance_level = pp_debugfs_print_current_performance_level,
270};
271
Jammy Zhouac885b32015-07-21 17:43:02 +0800272static int amd_pp_instance_init(struct amd_pp_init *pp_init,
273 struct amd_powerplay *amd_pp)
274{
275 int ret;
276 struct pp_instance *handle;
277
278 handle = kzalloc(sizeof(struct pp_instance), GFP_KERNEL);
279 if (handle == NULL)
280 return -ENOMEM;
281
282 ret = smum_init(pp_init, handle);
283 if (ret)
Jammy Zhou3bace352015-07-21 21:18:15 +0800284 goto fail_smum;
285
286 ret = hwmgr_init(pp_init, handle);
287 if (ret)
288 goto fail_hwmgr;
Jammy Zhouac885b32015-07-21 17:43:02 +0800289
Rex Zhue92a0372015-09-23 15:14:54 +0800290 ret = eventmgr_init(handle);
291 if (ret)
292 goto fail_eventmgr;
293
Jammy Zhouac885b32015-07-21 17:43:02 +0800294 amd_pp->pp_handle = handle;
295 return 0;
Jammy Zhou3bace352015-07-21 21:18:15 +0800296
Rex Zhue92a0372015-09-23 15:14:54 +0800297fail_eventmgr:
298 hwmgr_fini(handle->hwmgr);
Jammy Zhou3bace352015-07-21 21:18:15 +0800299fail_hwmgr:
300 smum_fini(handle->smu_mgr);
301fail_smum:
302 kfree(handle);
303 return ret;
Jammy Zhouac885b32015-07-21 17:43:02 +0800304}
305
306static int amd_pp_instance_fini(void *handle)
307{
308 struct pp_instance *instance = (struct pp_instance *)handle;
Rex Zhue92a0372015-09-23 15:14:54 +0800309
Jammy Zhouac885b32015-07-21 17:43:02 +0800310 if (instance == NULL)
311 return -EINVAL;
312
Rex Zhue92a0372015-09-23 15:14:54 +0800313 eventmgr_fini(instance->eventmgr);
314
Jammy Zhou3bace352015-07-21 21:18:15 +0800315 hwmgr_fini(instance->hwmgr);
316
Jammy Zhouac885b32015-07-21 17:43:02 +0800317 smum_fini(instance->smu_mgr);
318
319 kfree(handle);
320 return 0;
321}
322
Alex Deucher1f7371b2015-12-02 17:46:21 -0500323int amd_powerplay_init(struct amd_pp_init *pp_init,
324 struct amd_powerplay *amd_pp)
325{
Jammy Zhouac885b32015-07-21 17:43:02 +0800326 int ret;
327
Alex Deucher1f7371b2015-12-02 17:46:21 -0500328 if (pp_init == NULL || amd_pp == NULL)
329 return -EINVAL;
330
Jammy Zhouac885b32015-07-21 17:43:02 +0800331 ret = amd_pp_instance_init(pp_init, amd_pp);
332
333 if (ret)
334 return ret;
335
Alex Deucher1f7371b2015-12-02 17:46:21 -0500336 amd_pp->ip_funcs = &pp_ip_funcs;
337 amd_pp->pp_funcs = &pp_dpm_funcs;
338
339 return 0;
340}
341
342int amd_powerplay_fini(void *handle)
343{
Jammy Zhouac885b32015-07-21 17:43:02 +0800344 amd_pp_instance_fini(handle);
345
Alex Deucher1f7371b2015-12-02 17:46:21 -0500346 return 0;
347}