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);
69 polaris10_smum_init(smumgr);
83 int smum_fini(struct pp_smumgr *smumgr)
85 kfree(smumgr->device);
90 int smum_thermal_avfs_enable(struct pp_hwmgr *hwmgr,
91 void *input, void *output, void *storage, int result)
93 if (NULL != hwmgr->smumgr->smumgr_funcs->thermal_avfs_enable)
94 return hwmgr->smumgr->smumgr_funcs->thermal_avfs_enable(hwmgr);
99 int smum_thermal_setup_fan_table(struct pp_hwmgr *hwmgr,
100 void *input, void *output, void *storage, int result)
102 if (NULL != hwmgr->smumgr->smumgr_funcs->thermal_setup_fan_table)
103 return hwmgr->smumgr->smumgr_funcs->thermal_setup_fan_table(hwmgr);
108 int smum_update_sclk_threshold(struct pp_hwmgr *hwmgr)
111 if (NULL != hwmgr->smumgr->smumgr_funcs->update_sclk_threshold)
112 return hwmgr->smumgr->smumgr_funcs->update_sclk_threshold(hwmgr);
117 int smum_update_smc_table(struct pp_hwmgr *hwmgr, uint32_t type)
120 if (NULL != hwmgr->smumgr->smumgr_funcs->update_smc_table)
121 return hwmgr->smumgr->smumgr_funcs->update_smc_table(hwmgr, type);
126 uint32_t smum_get_offsetof(struct pp_smumgr *smumgr, uint32_t type, uint32_t member)
128 if (NULL != smumgr->smumgr_funcs->get_offsetof)
129 return smumgr->smumgr_funcs->get_offsetof(type, member);
134 int smum_process_firmware_header(struct pp_hwmgr *hwmgr)
136 if (NULL != hwmgr->smumgr->smumgr_funcs->process_firmware_header)
137 return hwmgr->smumgr->smumgr_funcs->process_firmware_header(hwmgr);
141 int smum_get_argument(struct pp_smumgr *smumgr)
143 if (NULL != smumgr->smumgr_funcs->get_argument)
144 return smumgr->smumgr_funcs->get_argument(smumgr);
149 uint32_t smum_get_mac_definition(struct pp_smumgr *smumgr, uint32_t value)
151 if (NULL != smumgr->smumgr_funcs->get_mac_definition)
152 return smumgr->smumgr_funcs->get_mac_definition(value);
157 int smum_download_powerplay_table(struct pp_smumgr *smumgr,
160 if (NULL != smumgr->smumgr_funcs->download_pptable_settings)
161 return smumgr->smumgr_funcs->download_pptable_settings(smumgr,
166 int smum_upload_powerplay_table(struct pp_smumgr *smumgr)
168 if (NULL != smumgr->smumgr_funcs->upload_pptable_settings)
169 return smumgr->smumgr_funcs->upload_pptable_settings(smumgr);
174 int smum_send_msg_to_smc(struct pp_smumgr *smumgr, uint16_t msg)
176 if (smumgr == NULL || smumgr->smumgr_funcs->send_msg_to_smc == NULL)
179 return smumgr->smumgr_funcs->send_msg_to_smc(smumgr, msg);
182 int smum_send_msg_to_smc_with_parameter(struct pp_smumgr *smumgr,
183 uint16_t msg, uint32_t parameter)
185 if (smumgr == NULL ||
186 smumgr->smumgr_funcs->send_msg_to_smc_with_parameter == NULL)
188 return smumgr->smumgr_funcs->send_msg_to_smc_with_parameter(
189 smumgr, msg, parameter);
193 * Returns once the part of the register indicated by the mask has
194 * reached the given value.
196 int smum_wait_on_register(struct pp_smumgr *smumgr,
198 uint32_t value, uint32_t mask)
203 if (smumgr == NULL || smumgr->device == NULL)
206 for (i = 0; i < smumgr->usec_timeout; i++) {
207 cur_value = cgs_read_register(smumgr->device, index);
208 if ((cur_value & mask) == (value & mask))
213 /* timeout means wrong logic*/
214 if (i == smumgr->usec_timeout)
220 int smum_wait_for_register_unequal(struct pp_smumgr *smumgr,
222 uint32_t value, uint32_t mask)
230 for (i = 0; i < smumgr->usec_timeout; i++) {
231 cur_value = cgs_read_register(smumgr->device,
233 if ((cur_value & mask) != (value & mask))
238 /* timeout means wrong logic */
239 if (i == smumgr->usec_timeout)
247 * Returns once the part of the register indicated by the mask
248 * has reached the given value.The indirect space is described by
249 * giving the memory-mapped index of the indirect index register.
251 int smum_wait_on_indirect_register(struct pp_smumgr *smumgr,
252 uint32_t indirect_port,
257 if (smumgr == NULL || smumgr->device == NULL)
260 cgs_write_register(smumgr->device, indirect_port, index);
261 return smum_wait_on_register(smumgr, indirect_port + 1,
265 void smum_wait_for_indirect_register_unequal(
266 struct pp_smumgr *smumgr,
267 uint32_t indirect_port,
272 if (smumgr == NULL || smumgr->device == NULL)
274 cgs_write_register(smumgr->device, indirect_port, index);
275 smum_wait_for_register_unequal(smumgr, indirect_port + 1,
279 int smu_allocate_memory(void *device, uint32_t size,
280 enum cgs_gpu_mem_type type,
281 uint32_t byte_align, uint64_t *mc_addr,
282 void **kptr, void *handle)
285 cgs_handle_t cgs_handle;
287 if (device == NULL || handle == NULL ||
288 mc_addr == NULL || kptr == NULL)
291 ret = cgs_alloc_gpu_mem(device, type, size, byte_align,
292 0, 0, (cgs_handle_t *)handle);
296 cgs_handle = *(cgs_handle_t *)handle;
298 ret = cgs_gmap_gpu_mem(device, cgs_handle, mc_addr);
302 ret = cgs_kmap_gpu_mem(device, cgs_handle, kptr);
309 cgs_gunmap_gpu_mem(device, cgs_handle);
312 cgs_free_gpu_mem(device, cgs_handle);
316 int smu_free_memory(void *device, void *handle)
318 cgs_handle_t cgs_handle = (cgs_handle_t)handle;
320 if (device == NULL || handle == NULL)
323 cgs_kunmap_gpu_mem(device, cgs_handle);
324 cgs_gunmap_gpu_mem(device, cgs_handle);
325 cgs_free_gpu_mem(device, cgs_handle);
330 int smum_init_smc_table(struct pp_hwmgr *hwmgr)
332 if (NULL != hwmgr->smumgr->smumgr_funcs->init_smc_table)
333 return hwmgr->smumgr->smumgr_funcs->init_smc_table(hwmgr);
338 int smum_populate_all_graphic_levels(struct pp_hwmgr *hwmgr)
340 if (NULL != hwmgr->smumgr->smumgr_funcs->populate_all_graphic_levels)
341 return hwmgr->smumgr->smumgr_funcs->populate_all_graphic_levels(hwmgr);
346 int smum_populate_all_memory_levels(struct pp_hwmgr *hwmgr)
348 if (NULL != hwmgr->smumgr->smumgr_funcs->populate_all_memory_levels)
349 return hwmgr->smumgr->smumgr_funcs->populate_all_memory_levels(hwmgr);
354 /*this interface is needed by island ci/vi */
355 int smum_initialize_mc_reg_table(struct pp_hwmgr *hwmgr)
357 if (NULL != hwmgr->smumgr->smumgr_funcs->initialize_mc_reg_table)
358 return hwmgr->smumgr->smumgr_funcs->initialize_mc_reg_table(hwmgr);
363 bool smum_is_dpm_running(struct pp_hwmgr *hwmgr)
365 if (NULL != hwmgr->smumgr->smumgr_funcs->is_dpm_running)
366 return hwmgr->smumgr->smumgr_funcs->is_dpm_running(hwmgr);