blob: 1e7f160f23d8cfa42217f7033e2ccd5a6f44e9e6 [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#ifdef CONFIG_DRM_AMD_POWERPLAY
46 struct amd_pp_init *pp_init;
47
48 pp_init = kzalloc(sizeof(struct amd_pp_init), GFP_KERNEL);
49
50 if (pp_init == NULL)
51 return -ENOMEM;
52
53 pp_init->chip_family = adev->family;
54 pp_init->chip_id = adev->asic_type;
55 pp_init->device = amdgpu_cgs_create_device(adev);
Alex Deucher1f7371b2015-12-02 17:46:21 -050056 ret = amd_powerplay_init(pp_init, amd_pp);
57 kfree(pp_init);
58#endif
59 } else {
60 amd_pp->pp_handle = (void *)adev;
61
62 switch (adev->asic_type) {
Maruthi Srinivas Bayyavarapu19196962016-04-26 20:35:36 +053063#ifdef CONFIG_DRM_AMDGPU_SI
64 case CHIP_TAHITI:
65 case CHIP_PITCAIRN:
66 case CHIP_VERDE:
67 case CHIP_OLAND:
68 case CHIP_HAINAN:
69 amd_pp->ip_funcs = &si_dpm_ip_funcs;
70 break;
71#endif
Alex Deucher1f7371b2015-12-02 17:46:21 -050072#ifdef CONFIG_DRM_AMDGPU_CIK
73 case CHIP_BONAIRE:
74 case CHIP_HAWAII:
75 amd_pp->ip_funcs = &ci_dpm_ip_funcs;
76 break;
77 case CHIP_KABINI:
78 case CHIP_MULLINS:
79 case CHIP_KAVERI:
80 amd_pp->ip_funcs = &kv_dpm_ip_funcs;
81 break;
82#endif
83 case CHIP_TOPAZ:
84 amd_pp->ip_funcs = &iceland_dpm_ip_funcs;
85 break;
86 case CHIP_TONGA:
87 amd_pp->ip_funcs = &tonga_dpm_ip_funcs;
88 break;
Eric Huang899fa4c2015-09-29 14:58:53 -040089 case CHIP_FIJI:
90 amd_pp->ip_funcs = &fiji_dpm_ip_funcs;
91 break;
Alex Deucher1f7371b2015-12-02 17:46:21 -050092 case CHIP_CARRIZO:
Tom St Denis9c97e752015-11-20 13:33:44 -050093 case CHIP_STONEY:
Alex Deucher1f7371b2015-12-02 17:46:21 -050094 amd_pp->ip_funcs = &cz_dpm_ip_funcs;
95 break;
96 default:
97 ret = -EINVAL;
98 break;
99 }
100 }
101 return ret;
102}
103
104static int amdgpu_pp_early_init(void *handle)
105{
106 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
107 int ret = 0;
108
Rex Zhuedb611c2015-10-20 11:05:45 +0800109#ifdef CONFIG_DRM_AMD_POWERPLAY
Rex Zhu76c8cc62015-10-17 17:57:58 +0800110 switch (adev->asic_type) {
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400111 case CHIP_POLARIS11:
112 case CHIP_POLARIS10:
113 adev->pp_enabled = true;
114 break;
Jordan Lazare34669042016-01-18 17:00:03 -0500115 case CHIP_TONGA:
116 case CHIP_FIJI:
Alex Deucher70bb2462016-07-28 13:35:42 -0400117 case CHIP_TOPAZ:
Jordan Lazare34669042016-01-18 17:00:03 -0500118 case CHIP_CARRIZO:
119 case CHIP_STONEY:
Huang Ruifad2af12016-04-18 23:29:32 +0800120 adev->pp_enabled = (amdgpu_powerplay == 0) ? false : true;
Jordan Lazare34669042016-01-18 17:00:03 -0500121 break;
122 /* These chips don't have powerplay implemenations */
123 case CHIP_BONAIRE:
124 case CHIP_HAWAII:
125 case CHIP_KABINI:
126 case CHIP_MULLINS:
127 case CHIP_KAVERI:
Jordan Lazare34669042016-01-18 17:00:03 -0500128 default:
129 adev->pp_enabled = false;
130 break;
Rex Zhu76c8cc62015-10-17 17:57:58 +0800131 }
Jammy Zhoue61710c2015-11-10 18:31:08 -0500132#else
133 adev->pp_enabled = false;
Rex Zhuedb611c2015-10-20 11:05:45 +0800134#endif
Rex Zhu76c8cc62015-10-17 17:57:58 +0800135
Alex Deucher1f7371b2015-12-02 17:46:21 -0500136 ret = amdgpu_powerplay_init(adev);
137 if (ret)
138 return ret;
139
140 if (adev->powerplay.ip_funcs->early_init)
141 ret = adev->powerplay.ip_funcs->early_init(
142 adev->powerplay.pp_handle);
143 return ret;
144}
145
Rex Zhu7ad4e7f2015-12-07 16:42:35 +0800146
147static int amdgpu_pp_late_init(void *handle)
148{
149 int ret = 0;
150 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
151
152 if (adev->powerplay.ip_funcs->late_init)
153 ret = adev->powerplay.ip_funcs->late_init(
154 adev->powerplay.pp_handle);
155
Alex Deucher898b1de2015-12-08 17:28:28 -0500156#ifdef CONFIG_DRM_AMD_POWERPLAY
Rex Zhu5349ece2016-03-29 14:34:51 +0800157 if (adev->pp_enabled && adev->pm.dpm_enabled) {
Alex Deucher898b1de2015-12-08 17:28:28 -0500158 amdgpu_pm_sysfs_init(adev);
Rex Zhu4ea2efa2016-02-25 17:32:45 +0800159 amdgpu_dpm_dispatch_task(adev, AMD_PP_EVENT_COMPLETE_INIT, NULL, NULL);
160 }
Alex Deucher898b1de2015-12-08 17:28:28 -0500161#endif
Rex Zhu7ad4e7f2015-12-07 16:42:35 +0800162 return ret;
163}
164
Alex Deucher1f7371b2015-12-02 17:46:21 -0500165static int amdgpu_pp_sw_init(void *handle)
166{
167 int ret = 0;
168 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
169
170 if (adev->powerplay.ip_funcs->sw_init)
171 ret = adev->powerplay.ip_funcs->sw_init(
172 adev->powerplay.pp_handle);
173
174#ifdef CONFIG_DRM_AMD_POWERPLAY
Rex Zhu1587f6e2016-03-29 14:21:50 +0800175 if (adev->pp_enabled)
176 adev->pm.dpm_enabled = true;
Alex Deucher1f7371b2015-12-02 17:46:21 -0500177#endif
178
179 return ret;
180}
181
182static int amdgpu_pp_sw_fini(void *handle)
183{
184 int ret = 0;
185 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
186
187 if (adev->powerplay.ip_funcs->sw_fini)
188 ret = adev->powerplay.ip_funcs->sw_fini(
189 adev->powerplay.pp_handle);
190 if (ret)
191 return ret;
192
Alex Deucher1f7371b2015-12-02 17:46:21 -0500193 return ret;
194}
195
196static int amdgpu_pp_hw_init(void *handle)
197{
198 int ret = 0;
199 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
200
Jammy Zhoue61710c2015-11-10 18:31:08 -0500201 if (adev->pp_enabled && adev->firmware.smu_load)
Alex Deucher1f7371b2015-12-02 17:46:21 -0500202 amdgpu_ucode_init_bo(adev);
203
204 if (adev->powerplay.ip_funcs->hw_init)
205 ret = adev->powerplay.ip_funcs->hw_init(
206 adev->powerplay.pp_handle);
207
208 return ret;
209}
210
211static int amdgpu_pp_hw_fini(void *handle)
212{
213 int ret = 0;
214 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
215
216 if (adev->powerplay.ip_funcs->hw_fini)
217 ret = adev->powerplay.ip_funcs->hw_fini(
218 adev->powerplay.pp_handle);
219
Jammy Zhoue61710c2015-11-10 18:31:08 -0500220 if (adev->pp_enabled && adev->firmware.smu_load)
Alex Deucher1f7371b2015-12-02 17:46:21 -0500221 amdgpu_ucode_fini_bo(adev);
222
223 return ret;
224}
225
Monk Liu482587e2016-05-19 14:36:01 +0800226static void amdgpu_pp_late_fini(void *handle)
227{
Dave Airlie29ccf752016-06-10 11:40:49 +1000228#ifdef CONFIG_DRM_AMD_POWERPLAY
Monk Liu482587e2016-05-19 14:36:01 +0800229 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
230
Monk Liu482587e2016-05-19 14:36:01 +0800231 if (adev->pp_enabled) {
232 amdgpu_pm_sysfs_fini(adev);
233 amd_powerplay_fini(adev->powerplay.pp_handle);
234 }
235
236 if (adev->powerplay.ip_funcs->late_fini)
237 adev->powerplay.ip_funcs->late_fini(
238 adev->powerplay.pp_handle);
239#endif
240}
241
Alex Deucher1f7371b2015-12-02 17:46:21 -0500242static int amdgpu_pp_suspend(void *handle)
243{
244 int ret = 0;
245 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
246
247 if (adev->powerplay.ip_funcs->suspend)
248 ret = adev->powerplay.ip_funcs->suspend(
249 adev->powerplay.pp_handle);
250 return ret;
251}
252
253static int amdgpu_pp_resume(void *handle)
254{
255 int ret = 0;
256 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
257
258 if (adev->powerplay.ip_funcs->resume)
259 ret = adev->powerplay.ip_funcs->resume(
260 adev->powerplay.pp_handle);
261 return ret;
262}
263
264static int amdgpu_pp_set_clockgating_state(void *handle,
265 enum amd_clockgating_state state)
266{
267 int ret = 0;
268 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
269
270 if (adev->powerplay.ip_funcs->set_clockgating_state)
271 ret = adev->powerplay.ip_funcs->set_clockgating_state(
272 adev->powerplay.pp_handle, state);
273 return ret;
274}
275
276static int amdgpu_pp_set_powergating_state(void *handle,
277 enum amd_powergating_state state)
278{
279 int ret = 0;
280 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
281
282 if (adev->powerplay.ip_funcs->set_powergating_state)
283 ret = adev->powerplay.ip_funcs->set_powergating_state(
284 adev->powerplay.pp_handle, state);
285 return ret;
286}
287
288
289static bool amdgpu_pp_is_idle(void *handle)
290{
291 bool ret = true;
292 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
293
294 if (adev->powerplay.ip_funcs->is_idle)
295 ret = adev->powerplay.ip_funcs->is_idle(
296 adev->powerplay.pp_handle);
297 return ret;
298}
299
300static int amdgpu_pp_wait_for_idle(void *handle)
301{
302 int ret = 0;
303 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
304
305 if (adev->powerplay.ip_funcs->wait_for_idle)
306 ret = adev->powerplay.ip_funcs->wait_for_idle(
307 adev->powerplay.pp_handle);
308 return ret;
309}
310
311static int amdgpu_pp_soft_reset(void *handle)
312{
313 int ret = 0;
314 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
315
316 if (adev->powerplay.ip_funcs->soft_reset)
317 ret = adev->powerplay.ip_funcs->soft_reset(
318 adev->powerplay.pp_handle);
319 return ret;
320}
321
Alex Deucher1f7371b2015-12-02 17:46:21 -0500322const struct amd_ip_funcs amdgpu_pp_ip_funcs = {
Tom St Denis88a907d2016-05-04 14:28:35 -0400323 .name = "amdgpu_powerplay",
Alex Deucher1f7371b2015-12-02 17:46:21 -0500324 .early_init = amdgpu_pp_early_init,
Rex Zhu7ad4e7f2015-12-07 16:42:35 +0800325 .late_init = amdgpu_pp_late_init,
Alex Deucher1f7371b2015-12-02 17:46:21 -0500326 .sw_init = amdgpu_pp_sw_init,
327 .sw_fini = amdgpu_pp_sw_fini,
328 .hw_init = amdgpu_pp_hw_init,
329 .hw_fini = amdgpu_pp_hw_fini,
Monk Liu482587e2016-05-19 14:36:01 +0800330 .late_fini = amdgpu_pp_late_fini,
Alex Deucher1f7371b2015-12-02 17:46:21 -0500331 .suspend = amdgpu_pp_suspend,
332 .resume = amdgpu_pp_resume,
333 .is_idle = amdgpu_pp_is_idle,
334 .wait_for_idle = amdgpu_pp_wait_for_idle,
335 .soft_reset = amdgpu_pp_soft_reset,
Alex Deucher1f7371b2015-12-02 17:46:21 -0500336 .set_clockgating_state = amdgpu_pp_set_clockgating_state,
337 .set_powergating_state = amdgpu_pp_set_powergating_state,
338};