drm/amdgpu: fix rmmod KCQ disable failed error
[sfrench/cifs-2.6.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_powerplay.c
1 /*
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"
33 #include "si_dpm.h"
34 #include "cik_dpm.h"
35 #include "vi_dpm.h"
36
37 static int amdgpu_pp_early_init(void *handle)
38 {
39         struct amdgpu_device *adev = (struct amdgpu_device *)handle;
40         struct amd_powerplay *amd_pp;
41         int ret = 0;
42
43         amd_pp = &(adev->powerplay);
44         amd_pp->pp_handle = (void *)adev;
45
46         switch (adev->asic_type) {
47         case CHIP_POLARIS11:
48         case CHIP_POLARIS10:
49         case CHIP_POLARIS12:
50         case CHIP_TONGA:
51         case CHIP_FIJI:
52         case CHIP_TOPAZ:
53         case CHIP_CARRIZO:
54         case CHIP_STONEY:
55         case CHIP_VEGA10:
56         case CHIP_RAVEN:
57                 amd_pp->cgs_device = amdgpu_cgs_create_device(adev);
58                 amd_pp->ip_funcs = &pp_ip_funcs;
59                 amd_pp->pp_funcs = &pp_dpm_funcs;
60                 break;
61         /* These chips don't have powerplay implemenations */
62 #ifdef CONFIG_DRM_AMDGPU_SI
63         case CHIP_TAHITI:
64         case CHIP_PITCAIRN:
65         case CHIP_VERDE:
66         case CHIP_OLAND:
67         case CHIP_HAINAN:
68                 amd_pp->ip_funcs = &si_dpm_ip_funcs;
69                 amd_pp->pp_funcs = &si_dpm_funcs;
70         break;
71 #endif
72 #ifdef CONFIG_DRM_AMDGPU_CIK
73         case CHIP_BONAIRE:
74         case CHIP_HAWAII:
75                 if (amdgpu_dpm == -1) {
76                         amd_pp->ip_funcs = &ci_dpm_ip_funcs;
77                         amd_pp->pp_funcs = &ci_dpm_funcs;
78                 } else {
79                         amd_pp->cgs_device = amdgpu_cgs_create_device(adev);
80                         amd_pp->ip_funcs = &pp_ip_funcs;
81                         amd_pp->pp_funcs = &pp_dpm_funcs;
82                 }
83                 break;
84         case CHIP_KABINI:
85         case CHIP_MULLINS:
86         case CHIP_KAVERI:
87                 amd_pp->ip_funcs = &kv_dpm_ip_funcs;
88                 amd_pp->pp_funcs = &kv_dpm_funcs;
89                 break;
90 #endif
91         default:
92                 ret = -EINVAL;
93                 break;
94         }
95
96         if (adev->powerplay.ip_funcs->early_init)
97                 ret = adev->powerplay.ip_funcs->early_init(
98                                         amd_pp->cgs_device ? amd_pp->cgs_device :
99                                         amd_pp->pp_handle);
100
101         return ret;
102 }
103
104
105 static int amdgpu_pp_late_init(void *handle)
106 {
107         int ret = 0;
108         struct amdgpu_device *adev = (struct amdgpu_device *)handle;
109
110         if (adev->powerplay.ip_funcs->late_init)
111                 ret = adev->powerplay.ip_funcs->late_init(
112                                         adev->powerplay.pp_handle);
113
114         return ret;
115 }
116
117 static int amdgpu_pp_sw_init(void *handle)
118 {
119         int ret = 0;
120         struct amdgpu_device *adev = (struct amdgpu_device *)handle;
121
122         if (adev->powerplay.ip_funcs->sw_init)
123                 ret = adev->powerplay.ip_funcs->sw_init(
124                                         adev->powerplay.pp_handle);
125
126         return ret;
127 }
128
129 static int amdgpu_pp_sw_fini(void *handle)
130 {
131         int ret = 0;
132         struct amdgpu_device *adev = (struct amdgpu_device *)handle;
133
134         if (adev->powerplay.ip_funcs->sw_fini)
135                 ret = adev->powerplay.ip_funcs->sw_fini(
136                                         adev->powerplay.pp_handle);
137         if (ret)
138                 return ret;
139
140         return ret;
141 }
142
143 static int amdgpu_pp_hw_init(void *handle)
144 {
145         int ret = 0;
146         struct amdgpu_device *adev = (struct amdgpu_device *)handle;
147
148         if (adev->firmware.load_type == AMDGPU_FW_LOAD_SMU)
149                 amdgpu_ucode_init_bo(adev);
150
151         if (adev->powerplay.ip_funcs->hw_init)
152                 ret = adev->powerplay.ip_funcs->hw_init(
153                                         adev->powerplay.pp_handle);
154
155         return ret;
156 }
157
158 static int amdgpu_pp_hw_fini(void *handle)
159 {
160         int ret = 0;
161         struct amdgpu_device *adev = (struct amdgpu_device *)handle;
162
163         if (adev->powerplay.ip_funcs->hw_fini)
164                 ret = adev->powerplay.ip_funcs->hw_fini(
165                                         adev->powerplay.pp_handle);
166
167         return ret;
168 }
169
170 static void amdgpu_pp_late_fini(void *handle)
171 {
172         struct amdgpu_device *adev = (struct amdgpu_device *)handle;
173
174         if (adev->powerplay.ip_funcs->late_fini)
175                 adev->powerplay.ip_funcs->late_fini(
176                           adev->powerplay.pp_handle);
177
178         if (adev->powerplay.cgs_device)
179                 amdgpu_cgs_destroy_device(adev->powerplay.cgs_device);
180 }
181
182 static int amdgpu_pp_suspend(void *handle)
183 {
184         int ret = 0;
185         struct amdgpu_device *adev = (struct amdgpu_device *)handle;
186
187         if (adev->powerplay.ip_funcs->suspend)
188                 ret = adev->powerplay.ip_funcs->suspend(
189                                          adev->powerplay.pp_handle);
190         return ret;
191 }
192
193 static int amdgpu_pp_resume(void *handle)
194 {
195         int ret = 0;
196         struct amdgpu_device *adev = (struct amdgpu_device *)handle;
197
198         if (adev->powerplay.ip_funcs->resume)
199                 ret = adev->powerplay.ip_funcs->resume(
200                                         adev->powerplay.pp_handle);
201         return ret;
202 }
203
204 static int amdgpu_pp_set_clockgating_state(void *handle,
205                                         enum amd_clockgating_state state)
206 {
207         int ret = 0;
208         struct amdgpu_device *adev = (struct amdgpu_device *)handle;
209
210         if (adev->powerplay.ip_funcs->set_clockgating_state)
211                 ret = adev->powerplay.ip_funcs->set_clockgating_state(
212                                 adev->powerplay.pp_handle, state);
213         return ret;
214 }
215
216 static int amdgpu_pp_set_powergating_state(void *handle,
217                                         enum amd_powergating_state state)
218 {
219         int ret = 0;
220         struct amdgpu_device *adev = (struct amdgpu_device *)handle;
221
222         if (adev->powerplay.ip_funcs->set_powergating_state)
223                 ret = adev->powerplay.ip_funcs->set_powergating_state(
224                                  adev->powerplay.pp_handle, state);
225         return ret;
226 }
227
228
229 static bool amdgpu_pp_is_idle(void *handle)
230 {
231         bool ret = true;
232         struct amdgpu_device *adev = (struct amdgpu_device *)handle;
233
234         if (adev->powerplay.ip_funcs->is_idle)
235                 ret = adev->powerplay.ip_funcs->is_idle(
236                                         adev->powerplay.pp_handle);
237         return ret;
238 }
239
240 static int amdgpu_pp_wait_for_idle(void *handle)
241 {
242         int ret = 0;
243         struct amdgpu_device *adev = (struct amdgpu_device *)handle;
244
245         if (adev->powerplay.ip_funcs->wait_for_idle)
246                 ret = adev->powerplay.ip_funcs->wait_for_idle(
247                                         adev->powerplay.pp_handle);
248         return ret;
249 }
250
251 static int amdgpu_pp_soft_reset(void *handle)
252 {
253         int ret = 0;
254         struct amdgpu_device *adev = (struct amdgpu_device *)handle;
255
256         if (adev->powerplay.ip_funcs->soft_reset)
257                 ret = adev->powerplay.ip_funcs->soft_reset(
258                                         adev->powerplay.pp_handle);
259         return ret;
260 }
261
262 static const struct amd_ip_funcs amdgpu_pp_ip_funcs = {
263         .name = "amdgpu_powerplay",
264         .early_init = amdgpu_pp_early_init,
265         .late_init = amdgpu_pp_late_init,
266         .sw_init = amdgpu_pp_sw_init,
267         .sw_fini = amdgpu_pp_sw_fini,
268         .hw_init = amdgpu_pp_hw_init,
269         .hw_fini = amdgpu_pp_hw_fini,
270         .late_fini = amdgpu_pp_late_fini,
271         .suspend = amdgpu_pp_suspend,
272         .resume = amdgpu_pp_resume,
273         .is_idle = amdgpu_pp_is_idle,
274         .wait_for_idle = amdgpu_pp_wait_for_idle,
275         .soft_reset = amdgpu_pp_soft_reset,
276         .set_clockgating_state = amdgpu_pp_set_clockgating_state,
277         .set_powergating_state = amdgpu_pp_set_powergating_state,
278 };
279
280 const struct amdgpu_ip_block_version amdgpu_pp_ip_block =
281 {
282         .type = AMD_IP_BLOCK_TYPE_SMC,
283         .major = 1,
284         .minor = 0,
285         .rev = 0,
286         .funcs = &amdgpu_pp_ip_funcs,
287 };