2 * Copyright 2015 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.
23 #include <linux/types.h>
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <drm/amdgpu_drm.h>
27 #include "pp_instance.h"
29 #include "cgs_common.h"
30 #include "linux/delay.h"
33 int smum_init(struct amd_pp_init *pp_init, struct pp_instance *handle)
35 struct pp_smumgr *smumgr;
37 if ((handle == NULL) || (pp_init == NULL))
40 smumgr = kzalloc(sizeof(struct pp_smumgr), GFP_KERNEL);
44 smumgr->device = pp_init->device;
45 smumgr->chip_family = pp_init->chip_family;
46 smumgr->chip_id = pp_init->chip_id;
47 smumgr->usec_timeout = AMD_MAX_USEC_TIMEOUT;
48 smumgr->reload_fw = 1;
49 handle->smu_mgr = smumgr;
51 switch (smumgr->chip_family) {
52 case AMDGPU_FAMILY_CZ:
55 case AMDGPU_FAMILY_VI:
56 switch (smumgr->chip_id) {
58 iceland_smum_init(smumgr);
61 tonga_smum_init(smumgr);
64 fiji_smum_init(smumgr);
68 polaris10_smum_init(smumgr);
82 int smum_fini(struct pp_smumgr *smumgr)
84 kfree(smumgr->device);
89 int smum_thermal_avfs_enable(struct pp_hwmgr *hwmgr,
90 void *input, void *output, void *storage, int result)
92 if (NULL != hwmgr->smumgr->smumgr_funcs->thermal_avfs_enable)
93 return hwmgr->smumgr->smumgr_funcs->thermal_avfs_enable(hwmgr);
98 int smum_thermal_setup_fan_table(struct pp_hwmgr *hwmgr,
99 void *input, void *output, void *storage, int result)
101 if (NULL != hwmgr->smumgr->smumgr_funcs->thermal_setup_fan_table)
102 return hwmgr->smumgr->smumgr_funcs->thermal_setup_fan_table(hwmgr);
107 int smum_update_sclk_threshold(struct pp_hwmgr *hwmgr)
110 if (NULL != hwmgr->smumgr->smumgr_funcs->update_sclk_threshold)
111 return hwmgr->smumgr->smumgr_funcs->update_sclk_threshold(hwmgr);
116 int smum_update_smc_table(struct pp_hwmgr *hwmgr, uint32_t type)
119 if (NULL != hwmgr->smumgr->smumgr_funcs->update_smc_table)
120 return hwmgr->smumgr->smumgr_funcs->update_smc_table(hwmgr, type);
125 uint32_t smum_get_offsetof(struct pp_smumgr *smumgr, uint32_t type, uint32_t member)
127 if (NULL != smumgr->smumgr_funcs->get_offsetof)
128 return smumgr->smumgr_funcs->get_offsetof(type, member);
133 int smum_process_firmware_header(struct pp_hwmgr *hwmgr)
135 if (NULL != hwmgr->smumgr->smumgr_funcs->process_firmware_header)
136 return hwmgr->smumgr->smumgr_funcs->process_firmware_header(hwmgr);
140 int smum_get_argument(struct pp_smumgr *smumgr)
142 if (NULL != smumgr->smumgr_funcs->get_argument)
143 return smumgr->smumgr_funcs->get_argument(smumgr);
148 uint32_t smum_get_mac_definition(struct pp_smumgr *smumgr, uint32_t value)
150 if (NULL != smumgr->smumgr_funcs->get_mac_definition)
151 return smumgr->smumgr_funcs->get_mac_definition(value);
156 int smum_download_powerplay_table(struct pp_smumgr *smumgr,
159 if (NULL != smumgr->smumgr_funcs->download_pptable_settings)
160 return smumgr->smumgr_funcs->download_pptable_settings(smumgr,
165 int smum_upload_powerplay_table(struct pp_smumgr *smumgr)
167 if (NULL != smumgr->smumgr_funcs->upload_pptable_settings)
168 return smumgr->smumgr_funcs->upload_pptable_settings(smumgr);
173 int smum_send_msg_to_smc(struct pp_smumgr *smumgr, uint16_t msg)
175 if (smumgr == NULL || smumgr->smumgr_funcs->send_msg_to_smc == NULL)
178 return smumgr->smumgr_funcs->send_msg_to_smc(smumgr, msg);
181 int smum_send_msg_to_smc_with_parameter(struct pp_smumgr *smumgr,
182 uint16_t msg, uint32_t parameter)
184 if (smumgr == NULL ||
185 smumgr->smumgr_funcs->send_msg_to_smc_with_parameter == NULL)
187 return smumgr->smumgr_funcs->send_msg_to_smc_with_parameter(
188 smumgr, msg, parameter);
192 * Returns once the part of the register indicated by the mask has
193 * reached the given value.
195 int smum_wait_on_register(struct pp_smumgr *smumgr,
197 uint32_t value, uint32_t mask)
202 if (smumgr == NULL || smumgr->device == NULL)
205 for (i = 0; i < smumgr->usec_timeout; i++) {
206 cur_value = cgs_read_register(smumgr->device, index);
207 if ((cur_value & mask) == (value & mask))
212 /* timeout means wrong logic*/
213 if (i == smumgr->usec_timeout)
219 int smum_wait_for_register_unequal(struct pp_smumgr *smumgr,
221 uint32_t value, uint32_t mask)
229 for (i = 0; i < smumgr->usec_timeout; i++) {
230 cur_value = cgs_read_register(smumgr->device,
232 if ((cur_value & mask) != (value & mask))
237 /* timeout means wrong logic */
238 if (i == smumgr->usec_timeout)
246 * Returns once the part of the register indicated by the mask
247 * has reached the given value.The indirect space is described by
248 * giving the memory-mapped index of the indirect index register.
250 int smum_wait_on_indirect_register(struct pp_smumgr *smumgr,
251 uint32_t indirect_port,
256 if (smumgr == NULL || smumgr->device == NULL)
259 cgs_write_register(smumgr->device, indirect_port, index);
260 return smum_wait_on_register(smumgr, indirect_port + 1,
264 void smum_wait_for_indirect_register_unequal(
265 struct pp_smumgr *smumgr,
266 uint32_t indirect_port,
271 if (smumgr == NULL || smumgr->device == NULL)
273 cgs_write_register(smumgr->device, indirect_port, index);
274 smum_wait_for_register_unequal(smumgr, indirect_port + 1,
278 int smu_allocate_memory(void *device, uint32_t size,
279 enum cgs_gpu_mem_type type,
280 uint32_t byte_align, uint64_t *mc_addr,
281 void **kptr, void *handle)
284 cgs_handle_t cgs_handle;
286 if (device == NULL || handle == NULL ||
287 mc_addr == NULL || kptr == NULL)
290 ret = cgs_alloc_gpu_mem(device, type, size, byte_align,
291 0, 0, (cgs_handle_t *)handle);
295 cgs_handle = *(cgs_handle_t *)handle;
297 ret = cgs_gmap_gpu_mem(device, cgs_handle, mc_addr);
301 ret = cgs_kmap_gpu_mem(device, cgs_handle, kptr);
308 cgs_gunmap_gpu_mem(device, cgs_handle);
311 cgs_free_gpu_mem(device, cgs_handle);
315 int smu_free_memory(void *device, void *handle)
317 cgs_handle_t cgs_handle = (cgs_handle_t)handle;
319 if (device == NULL || handle == NULL)
322 cgs_kunmap_gpu_mem(device, cgs_handle);
323 cgs_gunmap_gpu_mem(device, cgs_handle);
324 cgs_free_gpu_mem(device, cgs_handle);
329 int smum_init_smc_table(struct pp_hwmgr *hwmgr)
331 if (NULL != hwmgr->smumgr->smumgr_funcs->init_smc_table)
332 return hwmgr->smumgr->smumgr_funcs->init_smc_table(hwmgr);
337 int smum_populate_all_graphic_levels(struct pp_hwmgr *hwmgr)
339 if (NULL != hwmgr->smumgr->smumgr_funcs->populate_all_graphic_levels)
340 return hwmgr->smumgr->smumgr_funcs->populate_all_graphic_levels(hwmgr);
345 int smum_populate_all_memory_levels(struct pp_hwmgr *hwmgr)
347 if (NULL != hwmgr->smumgr->smumgr_funcs->populate_all_memory_levels)
348 return hwmgr->smumgr->smumgr_funcs->populate_all_memory_levels(hwmgr);
353 /*this interface is needed by island ci/vi */
354 int smum_initialize_mc_reg_table(struct pp_hwmgr *hwmgr)
356 if (NULL != hwmgr->smumgr->smumgr_funcs->initialize_mc_reg_table)
357 return hwmgr->smumgr->smumgr_funcs->initialize_mc_reg_table(hwmgr);
362 bool smum_is_dpm_running(struct pp_hwmgr *hwmgr)
364 if (NULL != hwmgr->smumgr->smumgr_funcs->is_dpm_running)
365 return hwmgr->smumgr->smumgr_funcs->is_dpm_running(hwmgr);