blob: b1921c7da36b8a1484abf8050fa0bb43e9ba6f48 [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 * Authors: AMD
23 *
24 */
25#include "atom.h"
26#include "amdgpu.h"
27#include "amd_shared.h"
28#include <linux/module.h>
29#include <linux/moduleparam.h>
30#include "amdgpu_pm.h"
31#include <drm/amdgpu_drm.h>
32#include "amdgpu_powerplay.h"
Maruthi Srinivas Bayyavarapu19196962016-04-26 20:35:36 +053033#include "si_dpm.h"
Alex Deucher1f7371b2015-12-02 17:46:21 -050034#include "cik_dpm.h"
35#include "vi_dpm.h"
36
37static int amdgpu_powerplay_init(struct amdgpu_device *adev)
38{
39 int ret = 0;
40 struct amd_powerplay *amd_pp;
41
42 amd_pp = &(adev->powerplay);
43
Jammy Zhoue61710c2015-11-10 18:31:08 -050044 if (adev->pp_enabled) {
Alex Deucher1f7371b2015-12-02 17:46:21 -050045 struct amd_pp_init *pp_init;
46
47 pp_init = kzalloc(sizeof(struct amd_pp_init), GFP_KERNEL);
48
49 if (pp_init == NULL)
50 return -ENOMEM;
51
52 pp_init->chip_family = adev->family;
53 pp_init->chip_id = adev->asic_type;
54 pp_init->device = amdgpu_cgs_create_device(adev);
Alex Deucher1f7371b2015-12-02 17:46:21 -050055 ret = amd_powerplay_init(pp_init, amd_pp);
56 kfree(pp_init);
Alex Deucher1f7371b2015-12-02 17:46:21 -050057 } else {
58 amd_pp->pp_handle = (void *)adev;
59
60 switch (adev->asic_type) {
Maruthi Srinivas Bayyavarapu19196962016-04-26 20:35:36 +053061#ifdef CONFIG_DRM_AMDGPU_SI
62 case CHIP_TAHITI:
63 case CHIP_PITCAIRN:
64 case CHIP_VERDE:
65 case CHIP_OLAND:
66 case CHIP_HAINAN:
67 amd_pp->ip_funcs = &si_dpm_ip_funcs;
68 break;
69#endif
Alex Deucher1f7371b2015-12-02 17:46:21 -050070#ifdef CONFIG_DRM_AMDGPU_CIK
71 case CHIP_BONAIRE:
72 case CHIP_HAWAII:
73 amd_pp->ip_funcs = &ci_dpm_ip_funcs;
74 break;
75 case CHIP_KABINI:
76 case CHIP_MULLINS:
77 case CHIP_KAVERI:
78 amd_pp->ip_funcs = &kv_dpm_ip_funcs;
79 break;
80#endif
Alex Deucher1f7371b2015-12-02 17:46:21 -050081 default:
82 ret = -EINVAL;
83 break;
84 }
85 }
86 return ret;
87}
88
89static int amdgpu_pp_early_init(void *handle)
90{
91 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
92 int ret = 0;
93
Rex Zhu76c8cc62015-10-17 17:57:58 +080094 switch (adev->asic_type) {
Flora Cui2cc0c0b2016-03-14 18:33:29 -040095 case CHIP_POLARIS11:
96 case CHIP_POLARIS10:
Junwei Zhangf4309522016-12-14 15:40:48 -050097 case CHIP_POLARIS12:
Jordan Lazare34669042016-01-18 17:00:03 -050098 case CHIP_TONGA:
99 case CHIP_FIJI:
Alex Deucher70bb2462016-07-28 13:35:42 -0400100 case CHIP_TOPAZ:
Jordan Lazare34669042016-01-18 17:00:03 -0500101 case CHIP_CARRIZO:
102 case CHIP_STONEY:
Rex Zhudb7da7a2016-12-23 14:07:25 +0800103 adev->pp_enabled = true;
Jordan Lazare34669042016-01-18 17:00:03 -0500104 break;
105 /* These chips don't have powerplay implemenations */
106 case CHIP_BONAIRE:
107 case CHIP_HAWAII:
108 case CHIP_KABINI:
109 case CHIP_MULLINS:
110 case CHIP_KAVERI:
Jordan Lazare34669042016-01-18 17:00:03 -0500111 default:
112 adev->pp_enabled = false;
113 break;
Rex Zhu76c8cc62015-10-17 17:57:58 +0800114 }
115
Alex Deucher1f7371b2015-12-02 17:46:21 -0500116 ret = amdgpu_powerplay_init(adev);
117 if (ret)
118 return ret;
119
120 if (adev->powerplay.ip_funcs->early_init)
121 ret = adev->powerplay.ip_funcs->early_init(
122 adev->powerplay.pp_handle);
123 return ret;
124}
125
Rex Zhu7ad4e7f2015-12-07 16:42:35 +0800126
127static int amdgpu_pp_late_init(void *handle)
128{
129 int ret = 0;
130 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
131
132 if (adev->powerplay.ip_funcs->late_init)
133 ret = adev->powerplay.ip_funcs->late_init(
134 adev->powerplay.pp_handle);
135
Rex Zhu5349ece2016-03-29 14:34:51 +0800136 if (adev->pp_enabled && adev->pm.dpm_enabled) {
Alex Deucher898b1de2015-12-08 17:28:28 -0500137 amdgpu_pm_sysfs_init(adev);
Rex Zhu4ea2efa2016-02-25 17:32:45 +0800138 amdgpu_dpm_dispatch_task(adev, AMD_PP_EVENT_COMPLETE_INIT, NULL, NULL);
139 }
Alex Deucherc64474e2016-09-28 16:37:15 -0400140
Rex Zhu7ad4e7f2015-12-07 16:42:35 +0800141 return ret;
142}
143
Alex Deucher1f7371b2015-12-02 17:46:21 -0500144static int amdgpu_pp_sw_init(void *handle)
145{
146 int ret = 0;
147 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
148
149 if (adev->powerplay.ip_funcs->sw_init)
150 ret = adev->powerplay.ip_funcs->sw_init(
151 adev->powerplay.pp_handle);
152
Alex Deucher1f7371b2015-12-02 17:46:21 -0500153 return ret;
154}
155
156static int amdgpu_pp_sw_fini(void *handle)
157{
158 int ret = 0;
159 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
160
161 if (adev->powerplay.ip_funcs->sw_fini)
162 ret = adev->powerplay.ip_funcs->sw_fini(
163 adev->powerplay.pp_handle);
164 if (ret)
165 return ret;
166
Alex Deucher1f7371b2015-12-02 17:46:21 -0500167 return ret;
168}
169
170static int amdgpu_pp_hw_init(void *handle)
171{
172 int ret = 0;
173 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
174
Jammy Zhoue61710c2015-11-10 18:31:08 -0500175 if (adev->pp_enabled && adev->firmware.smu_load)
Alex Deucher1f7371b2015-12-02 17:46:21 -0500176 amdgpu_ucode_init_bo(adev);
177
178 if (adev->powerplay.ip_funcs->hw_init)
179 ret = adev->powerplay.ip_funcs->hw_init(
180 adev->powerplay.pp_handle);
181
Trigger Huang7b1e8ca2016-11-16 10:13:45 -0500182 if ((amdgpu_dpm != 0) && !amdgpu_sriov_vf(adev))
Rex Zhuba5f8842016-10-27 15:29:57 +0800183 adev->pm.dpm_enabled = true;
184
Alex Deucher1f7371b2015-12-02 17:46:21 -0500185 return ret;
186}
187
188static int amdgpu_pp_hw_fini(void *handle)
189{
190 int ret = 0;
191 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
192
193 if (adev->powerplay.ip_funcs->hw_fini)
194 ret = adev->powerplay.ip_funcs->hw_fini(
195 adev->powerplay.pp_handle);
196
Jammy Zhoue61710c2015-11-10 18:31:08 -0500197 if (adev->pp_enabled && adev->firmware.smu_load)
Alex Deucher1f7371b2015-12-02 17:46:21 -0500198 amdgpu_ucode_fini_bo(adev);
199
200 return ret;
201}
202
Monk Liu482587e2016-05-19 14:36:01 +0800203static void amdgpu_pp_late_fini(void *handle)
204{
205 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
206
Monk Liu482587e2016-05-19 14:36:01 +0800207 if (adev->pp_enabled) {
208 amdgpu_pm_sysfs_fini(adev);
209 amd_powerplay_fini(adev->powerplay.pp_handle);
210 }
211
212 if (adev->powerplay.ip_funcs->late_fini)
213 adev->powerplay.ip_funcs->late_fini(
214 adev->powerplay.pp_handle);
Monk Liu482587e2016-05-19 14:36:01 +0800215}
216
Alex Deucher1f7371b2015-12-02 17:46:21 -0500217static int amdgpu_pp_suspend(void *handle)
218{
219 int ret = 0;
220 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
221
222 if (adev->powerplay.ip_funcs->suspend)
223 ret = adev->powerplay.ip_funcs->suspend(
224 adev->powerplay.pp_handle);
225 return ret;
226}
227
228static int amdgpu_pp_resume(void *handle)
229{
230 int ret = 0;
231 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
232
233 if (adev->powerplay.ip_funcs->resume)
234 ret = adev->powerplay.ip_funcs->resume(
235 adev->powerplay.pp_handle);
236 return ret;
237}
238
239static int amdgpu_pp_set_clockgating_state(void *handle,
240 enum amd_clockgating_state state)
241{
242 int ret = 0;
243 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
244
245 if (adev->powerplay.ip_funcs->set_clockgating_state)
246 ret = adev->powerplay.ip_funcs->set_clockgating_state(
247 adev->powerplay.pp_handle, state);
248 return ret;
249}
250
251static int amdgpu_pp_set_powergating_state(void *handle,
252 enum amd_powergating_state state)
253{
254 int ret = 0;
255 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
256
257 if (adev->powerplay.ip_funcs->set_powergating_state)
258 ret = adev->powerplay.ip_funcs->set_powergating_state(
259 adev->powerplay.pp_handle, state);
260 return ret;
261}
262
263
264static bool amdgpu_pp_is_idle(void *handle)
265{
266 bool ret = true;
267 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
268
269 if (adev->powerplay.ip_funcs->is_idle)
270 ret = adev->powerplay.ip_funcs->is_idle(
271 adev->powerplay.pp_handle);
272 return ret;
273}
274
275static int amdgpu_pp_wait_for_idle(void *handle)
276{
277 int ret = 0;
278 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
279
280 if (adev->powerplay.ip_funcs->wait_for_idle)
281 ret = adev->powerplay.ip_funcs->wait_for_idle(
282 adev->powerplay.pp_handle);
283 return ret;
284}
285
286static int amdgpu_pp_soft_reset(void *handle)
287{
288 int ret = 0;
289 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
290
291 if (adev->powerplay.ip_funcs->soft_reset)
292 ret = adev->powerplay.ip_funcs->soft_reset(
293 adev->powerplay.pp_handle);
294 return ret;
295}
296
Alex Deuchera1255102016-10-13 17:41:13 -0400297static const struct amd_ip_funcs amdgpu_pp_ip_funcs = {
Tom St Denis88a907d2016-05-04 14:28:35 -0400298 .name = "amdgpu_powerplay",
Alex Deucher1f7371b2015-12-02 17:46:21 -0500299 .early_init = amdgpu_pp_early_init,
Rex Zhu7ad4e7f2015-12-07 16:42:35 +0800300 .late_init = amdgpu_pp_late_init,
Alex Deucher1f7371b2015-12-02 17:46:21 -0500301 .sw_init = amdgpu_pp_sw_init,
302 .sw_fini = amdgpu_pp_sw_fini,
303 .hw_init = amdgpu_pp_hw_init,
304 .hw_fini = amdgpu_pp_hw_fini,
Monk Liu482587e2016-05-19 14:36:01 +0800305 .late_fini = amdgpu_pp_late_fini,
Alex Deucher1f7371b2015-12-02 17:46:21 -0500306 .suspend = amdgpu_pp_suspend,
307 .resume = amdgpu_pp_resume,
308 .is_idle = amdgpu_pp_is_idle,
309 .wait_for_idle = amdgpu_pp_wait_for_idle,
310 .soft_reset = amdgpu_pp_soft_reset,
Alex Deucher1f7371b2015-12-02 17:46:21 -0500311 .set_clockgating_state = amdgpu_pp_set_clockgating_state,
312 .set_powergating_state = amdgpu_pp_set_powergating_state,
313};
Alex Deuchera1255102016-10-13 17:41:13 -0400314
315const struct amdgpu_ip_block_version amdgpu_pp_ip_block =
316{
317 .type = AMD_IP_BLOCK_TYPE_SMC,
318 .major = 1,
319 .minor = 0,
320 .rev = 0,
321 .funcs = &amdgpu_pp_ip_funcs,
322};