xref: /linux/drivers/gpu/drm/amd/pm/powerplay/hwmgr/smu_helper.c (revision 323bbfcf1ef8836d0d2ad9e2c1f1c684f0e3b5b3)
1 /*
2  * Copyright 2018 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  */
23 
24 #include <linux/pci.h>
25 #include <linux/reboot.h>
26 
27 #include "hwmgr.h"
28 #include "pp_debug.h"
29 #include "ppatomctrl.h"
30 #include "ppsmc.h"
31 #include "atom.h"
32 #include "ivsrcid/thm/irqsrcs_thm_9_0.h"
33 #include "ivsrcid/smuio/irqsrcs_smuio_9_0.h"
34 #include "ivsrcid/ivsrcid_vislands30.h"
35 
convert_to_vid(uint16_t vddc)36 uint8_t convert_to_vid(uint16_t vddc)
37 {
38 	return (uint8_t) ((6200 - (vddc * VOLTAGE_SCALE)) / 25);
39 }
40 
convert_to_vddc(uint8_t vid)41 uint16_t convert_to_vddc(uint8_t vid)
42 {
43 	return (uint16_t) ((6200 - (vid * 25)) / VOLTAGE_SCALE);
44 }
45 
phm_copy_clock_limits_array(struct pp_hwmgr * hwmgr,uint32_t ** pptable_info_array,const uint32_t * pptable_array,uint32_t power_saving_clock_count)46 int phm_copy_clock_limits_array(
47 	struct pp_hwmgr *hwmgr,
48 	uint32_t **pptable_info_array,
49 	const uint32_t *pptable_array,
50 	uint32_t power_saving_clock_count)
51 {
52 	uint32_t array_size, i;
53 	uint32_t *table;
54 
55 	array_size = sizeof(uint32_t) * power_saving_clock_count;
56 	table = kzalloc(array_size, GFP_KERNEL);
57 	if (NULL == table)
58 		return -ENOMEM;
59 
60 	for (i = 0; i < power_saving_clock_count; i++)
61 		table[i] = le32_to_cpu(pptable_array[i]);
62 
63 	*pptable_info_array = table;
64 
65 	return 0;
66 }
67 
phm_copy_overdrive_settings_limits_array(struct pp_hwmgr * hwmgr,uint32_t ** pptable_info_array,const uint32_t * pptable_array,uint32_t od_setting_count)68 int phm_copy_overdrive_settings_limits_array(
69 	struct pp_hwmgr *hwmgr,
70 	uint32_t **pptable_info_array,
71 	const uint32_t *pptable_array,
72 	uint32_t od_setting_count)
73 {
74 	uint32_t array_size, i;
75 	uint32_t *table;
76 
77 	array_size = sizeof(uint32_t) * od_setting_count;
78 	table = kzalloc(array_size, GFP_KERNEL);
79 	if (NULL == table)
80 		return -ENOMEM;
81 
82 	for (i = 0; i < od_setting_count; i++)
83 		table[i] = le32_to_cpu(pptable_array[i]);
84 
85 	*pptable_info_array = table;
86 
87 	return 0;
88 }
89 
phm_set_field_to_u32(u32 offset,u32 original_data,u32 field,u32 size)90 uint32_t phm_set_field_to_u32(u32 offset, u32 original_data, u32 field, u32 size)
91 {
92 	u32 mask = 0;
93 	u32 shift = 0;
94 
95 	shift = (offset % 4) << 3;
96 	if (size == sizeof(uint8_t))
97 		mask = 0xFF << shift;
98 	else if (size == sizeof(uint16_t))
99 		mask = 0xFFFF << shift;
100 
101 	original_data &= ~mask;
102 	original_data |= (field << shift);
103 	return original_data;
104 }
105 
106 /*
107  * Returns once the part of the register indicated by the mask has
108  * reached the given value.
109  */
phm_wait_on_register(struct pp_hwmgr * hwmgr,uint32_t index,uint32_t value,uint32_t mask)110 int phm_wait_on_register(struct pp_hwmgr *hwmgr, uint32_t index,
111 			 uint32_t value, uint32_t mask)
112 {
113 	uint32_t i;
114 	uint32_t cur_value;
115 
116 	if (hwmgr == NULL || hwmgr->device == NULL) {
117 		pr_err("Invalid Hardware Manager!");
118 		return -EINVAL;
119 	}
120 
121 	for (i = 0; i < hwmgr->usec_timeout; i++) {
122 		cur_value = cgs_read_register(hwmgr->device, index);
123 		if ((cur_value & mask) == (value & mask))
124 			break;
125 		udelay(1);
126 	}
127 
128 	/* timeout means wrong logic*/
129 	if (i == hwmgr->usec_timeout)
130 		return -1;
131 	return 0;
132 }
133 
134 
135 /*
136  * Returns once the part of the register indicated by the mask has
137  * reached the given value.The indirect space is described by giving
138  * the memory-mapped index of the indirect index register.
139  */
phm_wait_on_indirect_register(struct pp_hwmgr * hwmgr,uint32_t indirect_port,uint32_t index,uint32_t value,uint32_t mask)140 int phm_wait_on_indirect_register(struct pp_hwmgr *hwmgr,
141 				uint32_t indirect_port,
142 				uint32_t index,
143 				uint32_t value,
144 				uint32_t mask)
145 {
146 	if (hwmgr == NULL || hwmgr->device == NULL) {
147 		pr_err("Invalid Hardware Manager!");
148 		return -EINVAL;
149 	}
150 
151 	cgs_write_register(hwmgr->device, indirect_port, index);
152 	return phm_wait_on_register(hwmgr, indirect_port + 1, value, mask);
153 }
154 
phm_wait_for_register_unequal(struct pp_hwmgr * hwmgr,uint32_t index,uint32_t value,uint32_t mask)155 int phm_wait_for_register_unequal(struct pp_hwmgr *hwmgr,
156 					uint32_t index,
157 					uint32_t value, uint32_t mask)
158 {
159 	uint32_t i;
160 	uint32_t cur_value;
161 
162 	if (hwmgr == NULL || hwmgr->device == NULL)
163 		return -EINVAL;
164 
165 	for (i = 0; i < hwmgr->usec_timeout; i++) {
166 		cur_value = cgs_read_register(hwmgr->device,
167 									index);
168 		if ((cur_value & mask) != (value & mask))
169 			break;
170 		udelay(1);
171 	}
172 
173 	/* timeout means wrong logic */
174 	if (i == hwmgr->usec_timeout)
175 		return -ETIME;
176 	return 0;
177 }
178 
phm_wait_for_indirect_register_unequal(struct pp_hwmgr * hwmgr,uint32_t indirect_port,uint32_t index,uint32_t value,uint32_t mask)179 int phm_wait_for_indirect_register_unequal(struct pp_hwmgr *hwmgr,
180 						uint32_t indirect_port,
181 						uint32_t index,
182 						uint32_t value,
183 						uint32_t mask)
184 {
185 	if (hwmgr == NULL || hwmgr->device == NULL)
186 		return -EINVAL;
187 
188 	cgs_write_register(hwmgr->device, indirect_port, index);
189 	return phm_wait_for_register_unequal(hwmgr, indirect_port + 1,
190 						value, mask);
191 }
192 
phm_cf_want_uvd_power_gating(struct pp_hwmgr * hwmgr)193 bool phm_cf_want_uvd_power_gating(struct pp_hwmgr *hwmgr)
194 {
195 	return phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_UVDPowerGating);
196 }
197 
phm_cf_want_vce_power_gating(struct pp_hwmgr * hwmgr)198 bool phm_cf_want_vce_power_gating(struct pp_hwmgr *hwmgr)
199 {
200 	return phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_VCEPowerGating);
201 }
202 
203 
phm_trim_voltage_table(struct pp_atomctrl_voltage_table * vol_table)204 int phm_trim_voltage_table(struct pp_atomctrl_voltage_table *vol_table)
205 {
206 	uint32_t i, j;
207 	uint16_t vvalue;
208 	bool found = false;
209 	struct pp_atomctrl_voltage_table *table;
210 
211 	PP_ASSERT_WITH_CODE((NULL != vol_table),
212 			"Voltage Table empty.", return -EINVAL);
213 
214 	table = kzalloc_obj(struct pp_atomctrl_voltage_table);
215 
216 	if (NULL == table)
217 		return -EINVAL;
218 
219 	table->mask_low = vol_table->mask_low;
220 	table->phase_delay = vol_table->phase_delay;
221 
222 	for (i = 0; i < vol_table->count; i++) {
223 		vvalue = vol_table->entries[i].value;
224 		found = false;
225 
226 		for (j = 0; j < table->count; j++) {
227 			if (vvalue == table->entries[j].value) {
228 				found = true;
229 				break;
230 			}
231 		}
232 
233 		if (!found) {
234 			table->entries[table->count].value = vvalue;
235 			table->entries[table->count].smio_low =
236 					vol_table->entries[i].smio_low;
237 			table->count++;
238 		}
239 	}
240 
241 	memcpy(vol_table, table, sizeof(struct pp_atomctrl_voltage_table));
242 	kfree(table);
243 	table = NULL;
244 	return 0;
245 }
246 
phm_get_svi2_mvdd_voltage_table(struct pp_atomctrl_voltage_table * vol_table,phm_ppt_v1_clock_voltage_dependency_table * dep_table)247 int phm_get_svi2_mvdd_voltage_table(struct pp_atomctrl_voltage_table *vol_table,
248 		phm_ppt_v1_clock_voltage_dependency_table *dep_table)
249 {
250 	uint32_t i;
251 	int result;
252 
253 	PP_ASSERT_WITH_CODE((0 != dep_table->count),
254 			"Voltage Dependency Table empty.", return -EINVAL);
255 
256 	PP_ASSERT_WITH_CODE((NULL != vol_table),
257 			"vol_table empty.", return -EINVAL);
258 
259 	vol_table->mask_low = 0;
260 	vol_table->phase_delay = 0;
261 	vol_table->count = dep_table->count;
262 
263 	for (i = 0; i < dep_table->count; i++) {
264 		vol_table->entries[i].value = dep_table->entries[i].mvdd;
265 		vol_table->entries[i].smio_low = 0;
266 	}
267 
268 	result = phm_trim_voltage_table(vol_table);
269 	PP_ASSERT_WITH_CODE((0 == result),
270 			"Failed to trim MVDD table.", return result);
271 
272 	return 0;
273 }
274 
phm_get_svi2_vddci_voltage_table(struct pp_atomctrl_voltage_table * vol_table,phm_ppt_v1_clock_voltage_dependency_table * dep_table)275 int phm_get_svi2_vddci_voltage_table(struct pp_atomctrl_voltage_table *vol_table,
276 		phm_ppt_v1_clock_voltage_dependency_table *dep_table)
277 {
278 	uint32_t i;
279 	int result;
280 
281 	PP_ASSERT_WITH_CODE((0 != dep_table->count),
282 			"Voltage Dependency Table empty.", return -EINVAL);
283 
284 	PP_ASSERT_WITH_CODE((NULL != vol_table),
285 			"vol_table empty.", return -EINVAL);
286 
287 	vol_table->mask_low = 0;
288 	vol_table->phase_delay = 0;
289 	vol_table->count = dep_table->count;
290 
291 	for (i = 0; i < dep_table->count; i++) {
292 		vol_table->entries[i].value = dep_table->entries[i].vddci;
293 		vol_table->entries[i].smio_low = 0;
294 	}
295 
296 	result = phm_trim_voltage_table(vol_table);
297 	PP_ASSERT_WITH_CODE((0 == result),
298 			"Failed to trim VDDCI table.", return result);
299 
300 	return 0;
301 }
302 
phm_get_svi2_vdd_voltage_table(struct pp_atomctrl_voltage_table * vol_table,phm_ppt_v1_voltage_lookup_table * lookup_table)303 int phm_get_svi2_vdd_voltage_table(struct pp_atomctrl_voltage_table *vol_table,
304 		phm_ppt_v1_voltage_lookup_table *lookup_table)
305 {
306 	int i = 0;
307 
308 	PP_ASSERT_WITH_CODE((0 != lookup_table->count),
309 			"Voltage Lookup Table empty.", return -EINVAL);
310 
311 	PP_ASSERT_WITH_CODE((NULL != vol_table),
312 			"vol_table empty.", return -EINVAL);
313 
314 	vol_table->mask_low = 0;
315 	vol_table->phase_delay = 0;
316 
317 	vol_table->count = lookup_table->count;
318 
319 	for (i = 0; i < vol_table->count; i++) {
320 		vol_table->entries[i].value = lookup_table->entries[i].us_vdd;
321 		vol_table->entries[i].smio_low = 0;
322 	}
323 
324 	return 0;
325 }
326 
phm_trim_voltage_table_to_fit_state_table(uint32_t max_vol_steps,struct pp_atomctrl_voltage_table * vol_table)327 void phm_trim_voltage_table_to_fit_state_table(uint32_t max_vol_steps,
328 				struct pp_atomctrl_voltage_table *vol_table)
329 {
330 	unsigned int i, diff;
331 
332 	if (vol_table->count <= max_vol_steps)
333 		return;
334 
335 	diff = vol_table->count - max_vol_steps;
336 
337 	for (i = 0; i < max_vol_steps; i++)
338 		vol_table->entries[i] = vol_table->entries[i + diff];
339 
340 	vol_table->count = max_vol_steps;
341 
342 	return;
343 }
344 
phm_reset_single_dpm_table(void * table,uint32_t count,int max)345 int phm_reset_single_dpm_table(void *table,
346 				uint32_t count, int max)
347 {
348 	int i;
349 
350 	struct vi_dpm_table *dpm_table = (struct vi_dpm_table *)table;
351 
352 	dpm_table->count = count > max ? max : count;
353 
354 	for (i = 0; i < dpm_table->count; i++)
355 		dpm_table->dpm_level[i].enabled = false;
356 
357 	return 0;
358 }
359 
phm_setup_pcie_table_entry(void * table,uint32_t index,uint32_t pcie_gen,uint32_t pcie_lanes)360 void phm_setup_pcie_table_entry(
361 	void *table,
362 	uint32_t index, uint32_t pcie_gen,
363 	uint32_t pcie_lanes)
364 {
365 	struct vi_dpm_table *dpm_table = (struct vi_dpm_table *)table;
366 	dpm_table->dpm_level[index].value = pcie_gen;
367 	dpm_table->dpm_level[index].param1 = pcie_lanes;
368 	dpm_table->dpm_level[index].enabled = 1;
369 }
370 
phm_get_dpm_level_enable_mask_value(void * table)371 int32_t phm_get_dpm_level_enable_mask_value(void *table)
372 {
373 	int32_t i;
374 	int32_t mask = 0;
375 	struct vi_dpm_table *dpm_table = (struct vi_dpm_table *)table;
376 
377 	for (i = dpm_table->count; i > 0; i--) {
378 		mask = mask << 1;
379 		if (dpm_table->dpm_level[i - 1].enabled)
380 			mask |= 0x1;
381 		else
382 			mask &= 0xFFFFFFFE;
383 	}
384 
385 	return mask;
386 }
387 
phm_get_voltage_index(struct phm_ppt_v1_voltage_lookup_table * lookup_table,uint16_t voltage)388 uint8_t phm_get_voltage_index(
389 		struct phm_ppt_v1_voltage_lookup_table *lookup_table, uint16_t voltage)
390 {
391 	uint8_t count = (uint8_t) (lookup_table->count);
392 	uint8_t i;
393 
394 	PP_ASSERT_WITH_CODE((NULL != lookup_table),
395 			"Lookup Table empty.", return 0);
396 	PP_ASSERT_WITH_CODE((0 != count),
397 			"Lookup Table empty.", return 0);
398 
399 	for (i = 0; i < lookup_table->count; i++) {
400 		/* find first voltage equal or bigger than requested */
401 		if (lookup_table->entries[i].us_vdd >= voltage)
402 			return i;
403 	}
404 	/* voltage is bigger than max voltage in the table */
405 	return i - 1;
406 }
407 
phm_get_voltage_id(pp_atomctrl_voltage_table * voltage_table,uint32_t voltage)408 uint8_t phm_get_voltage_id(pp_atomctrl_voltage_table *voltage_table,
409 		uint32_t voltage)
410 {
411 	uint8_t count = (uint8_t) (voltage_table->count);
412 	uint8_t i = 0;
413 
414 	PP_ASSERT_WITH_CODE((NULL != voltage_table),
415 		"Voltage Table empty.", return 0;);
416 	PP_ASSERT_WITH_CODE((0 != count),
417 		"Voltage Table empty.", return 0;);
418 
419 	for (i = 0; i < count; i++) {
420 		/* find first voltage bigger than requested */
421 		if (voltage_table->entries[i].value >= voltage)
422 			return i;
423 	}
424 
425 	/* voltage is bigger than max voltage in the table */
426 	return i - 1;
427 }
428 
phm_find_closest_vddci(struct pp_atomctrl_voltage_table * vddci_table,uint16_t vddci)429 uint16_t phm_find_closest_vddci(struct pp_atomctrl_voltage_table *vddci_table, uint16_t vddci)
430 {
431 	uint32_t  i;
432 
433 	for (i = 0; i < vddci_table->count; i++) {
434 		if (vddci_table->entries[i].value >= vddci)
435 			return vddci_table->entries[i].value;
436 	}
437 
438 	pr_debug("vddci is larger than max value in vddci_table\n");
439 	return vddci_table->entries[i-1].value;
440 }
441 
phm_find_boot_level(void * table,uint32_t value,uint32_t * boot_level)442 int phm_find_boot_level(void *table,
443 		uint32_t value, uint32_t *boot_level)
444 {
445 	int result = -EINVAL;
446 	uint32_t i;
447 	struct vi_dpm_table *dpm_table = (struct vi_dpm_table *)table;
448 
449 	for (i = 0; i < dpm_table->count; i++) {
450 		if (value == dpm_table->dpm_level[i].value) {
451 			*boot_level = i;
452 			result = 0;
453 		}
454 	}
455 
456 	return result;
457 }
458 
phm_get_sclk_for_voltage_evv(struct pp_hwmgr * hwmgr,phm_ppt_v1_voltage_lookup_table * lookup_table,uint16_t virtual_voltage_id,int32_t * sclk)459 int phm_get_sclk_for_voltage_evv(struct pp_hwmgr *hwmgr,
460 	phm_ppt_v1_voltage_lookup_table *lookup_table,
461 	uint16_t virtual_voltage_id, int32_t *sclk)
462 {
463 	uint8_t entry_id;
464 	uint8_t voltage_id;
465 	struct phm_ppt_v1_information *table_info =
466 			(struct phm_ppt_v1_information *)(hwmgr->pptable);
467 
468 	PP_ASSERT_WITH_CODE(lookup_table->count != 0, "Lookup table is empty", return -EINVAL);
469 
470 	/* search for leakage voltage ID 0xff01 ~ 0xff08 and sckl */
471 	for (entry_id = 0; entry_id < table_info->vdd_dep_on_sclk->count; entry_id++) {
472 		voltage_id = table_info->vdd_dep_on_sclk->entries[entry_id].vddInd;
473 		if (lookup_table->entries[voltage_id].us_vdd == virtual_voltage_id)
474 			break;
475 	}
476 
477 	if (entry_id >= table_info->vdd_dep_on_sclk->count) {
478 		pr_debug("Can't find requested voltage id in vdd_dep_on_sclk table\n");
479 		return -EINVAL;
480 	}
481 
482 	*sclk = table_info->vdd_dep_on_sclk->entries[entry_id].clk;
483 
484 	return 0;
485 }
486 
487 /**
488  * phm_initializa_dynamic_state_adjustment_rule_settings - Initialize Dynamic State Adjustment Rule Settings
489  *
490  * @hwmgr:  the address of the powerplay hardware manager.
491  */
phm_initializa_dynamic_state_adjustment_rule_settings(struct pp_hwmgr * hwmgr)492 int phm_initializa_dynamic_state_adjustment_rule_settings(struct pp_hwmgr *hwmgr)
493 {
494 	struct phm_clock_voltage_dependency_table *table_clk_vlt;
495 	struct phm_ppt_v1_information *pptable_info = (struct phm_ppt_v1_information *)(hwmgr->pptable);
496 
497 	/* initialize vddc_dep_on_dal_pwrl table */
498 	table_clk_vlt = kzalloc_flex(*table_clk_vlt, entries, 4);
499 
500 	if (NULL == table_clk_vlt) {
501 		pr_err("Can not allocate space for vddc_dep_on_dal_pwrl! \n");
502 		return -ENOMEM;
503 	} else {
504 		table_clk_vlt->count = 4;
505 		table_clk_vlt->entries[0].clk = PP_DAL_POWERLEVEL_ULTRALOW;
506 		if (hwmgr->chip_id >= CHIP_POLARIS10 &&
507 		    hwmgr->chip_id <= CHIP_VEGAM)
508 			table_clk_vlt->entries[0].v = 700;
509 		else
510 			table_clk_vlt->entries[0].v = 0;
511 		table_clk_vlt->entries[1].clk = PP_DAL_POWERLEVEL_LOW;
512 		if (hwmgr->chip_id >= CHIP_POLARIS10 &&
513 		    hwmgr->chip_id <= CHIP_VEGAM)
514 			table_clk_vlt->entries[1].v = 740;
515 		else
516 			table_clk_vlt->entries[1].v = 720;
517 		table_clk_vlt->entries[2].clk = PP_DAL_POWERLEVEL_NOMINAL;
518 		if (hwmgr->chip_id >= CHIP_POLARIS10 &&
519 		    hwmgr->chip_id <= CHIP_VEGAM)
520 			table_clk_vlt->entries[2].v = 800;
521 		else
522 			table_clk_vlt->entries[2].v = 810;
523 		table_clk_vlt->entries[3].clk = PP_DAL_POWERLEVEL_PERFORMANCE;
524 		table_clk_vlt->entries[3].v = 900;
525 		if (pptable_info != NULL)
526 			pptable_info->vddc_dep_on_dal_pwrl = table_clk_vlt;
527 		hwmgr->dyn_state.vddc_dep_on_dal_pwrl = table_clk_vlt;
528 	}
529 
530 	return 0;
531 }
532 
phm_get_lowest_enabled_level(struct pp_hwmgr * hwmgr,uint32_t mask)533 uint32_t phm_get_lowest_enabled_level(struct pp_hwmgr *hwmgr, uint32_t mask)
534 {
535 	uint32_t level = 0;
536 
537 	while (0 == (mask & (1 << level)))
538 		level++;
539 
540 	return level;
541 }
542 
phm_apply_dal_min_voltage_request(struct pp_hwmgr * hwmgr)543 void phm_apply_dal_min_voltage_request(struct pp_hwmgr *hwmgr)
544 {
545 	struct phm_ppt_v1_information *table_info =
546 			(struct phm_ppt_v1_information *)hwmgr->pptable;
547 	struct phm_clock_voltage_dependency_table *table =
548 				table_info->vddc_dep_on_dal_pwrl;
549 	struct phm_ppt_v1_clock_voltage_dependency_table *vddc_table;
550 	enum PP_DAL_POWERLEVEL dal_power_level = hwmgr->dal_power_level;
551 	uint32_t req_vddc = 0, req_volt, i;
552 
553 	if (!table || table->count <= 0
554 		|| dal_power_level < PP_DAL_POWERLEVEL_ULTRALOW
555 		|| dal_power_level > PP_DAL_POWERLEVEL_PERFORMANCE)
556 		return;
557 
558 	for (i = 0; i < table->count; i++) {
559 		if (dal_power_level == table->entries[i].clk) {
560 			req_vddc = table->entries[i].v;
561 			break;
562 		}
563 	}
564 
565 	vddc_table = table_info->vdd_dep_on_sclk;
566 	for (i = 0; i < vddc_table->count; i++) {
567 		if (req_vddc <= vddc_table->entries[i].vddc) {
568 			req_volt = (((uint32_t)vddc_table->entries[i].vddc) * VOLTAGE_SCALE);
569 			smum_send_msg_to_smc_with_parameter(hwmgr,
570 					PPSMC_MSG_VddC_Request,
571 					req_volt,
572 					NULL);
573 			return;
574 		}
575 	}
576 	pr_err("DAL requested level can not"
577 			" found a available voltage in VDDC DPM Table \n");
578 }
579 
phm_get_voltage_evv_on_sclk(struct pp_hwmgr * hwmgr,uint8_t voltage_type,uint32_t sclk,uint16_t id,uint16_t * voltage)580 int phm_get_voltage_evv_on_sclk(struct pp_hwmgr *hwmgr, uint8_t voltage_type,
581 				uint32_t sclk, uint16_t id, uint16_t *voltage)
582 {
583 	uint32_t vol;
584 	int ret = 0;
585 
586 	if (hwmgr->chip_id < CHIP_TONGA) {
587 		ret = atomctrl_get_voltage_evv(hwmgr, id, voltage);
588 	} else if (hwmgr->chip_id < CHIP_POLARIS10) {
589 		ret = atomctrl_get_voltage_evv_on_sclk(hwmgr, voltage_type, sclk, id, voltage);
590 		if (*voltage >= 2000 || *voltage == 0)
591 			*voltage = 1150;
592 	} else {
593 		ret = atomctrl_get_voltage_evv_on_sclk_ai(hwmgr, voltage_type, sclk, id, &vol);
594 		*voltage = (uint16_t)(vol/100);
595 	}
596 	return ret;
597 }
598 
599 
phm_irq_process(struct amdgpu_device * adev,struct amdgpu_irq_src * source,struct amdgpu_iv_entry * entry)600 int phm_irq_process(struct amdgpu_device *adev,
601 			   struct amdgpu_irq_src *source,
602 			   struct amdgpu_iv_entry *entry)
603 {
604 	struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
605 	uint32_t client_id = entry->client_id;
606 	uint32_t src_id = entry->src_id;
607 
608 	if (client_id == AMDGPU_IRQ_CLIENTID_LEGACY) {
609 		if (src_id == VISLANDS30_IV_SRCID_CG_TSS_THERMAL_LOW_TO_HIGH) {
610 			schedule_delayed_work(&hwmgr->swctf_delayed_work,
611 					      msecs_to_jiffies(AMDGPU_SWCTF_EXTRA_DELAY));
612 		} else if (src_id == VISLANDS30_IV_SRCID_CG_TSS_THERMAL_HIGH_TO_LOW) {
613 			dev_emerg(adev->dev, "ERROR: GPU under temperature range detected!\n");
614 		} else if (src_id == VISLANDS30_IV_SRCID_GPIO_19) {
615 			dev_emerg(adev->dev, "ERROR: GPU HW Critical Temperature Fault(aka CTF) detected!\n");
616 			/*
617 			 * HW CTF just occurred. Shutdown to prevent further damage.
618 			 */
619 			dev_emerg(adev->dev, "ERROR: System is going to shutdown due to GPU HW CTF!\n");
620 			orderly_poweroff(true);
621 		}
622 	} else if (client_id == SOC15_IH_CLIENTID_THM) {
623 		if (src_id == 0)
624 			schedule_delayed_work(&hwmgr->swctf_delayed_work,
625 					      msecs_to_jiffies(AMDGPU_SWCTF_EXTRA_DELAY));
626 		else
627 			dev_emerg(adev->dev, "ERROR: GPU under temperature range detected!\n");
628 	} else if (client_id == SOC15_IH_CLIENTID_ROM_SMUIO) {
629 		dev_emerg(adev->dev, "ERROR: GPU HW Critical Temperature Fault(aka CTF) detected!\n");
630 		/*
631 		 * HW CTF just occurred. Shutdown to prevent further damage.
632 		 */
633 		dev_emerg(adev->dev, "ERROR: System is going to shutdown due to GPU HW CTF!\n");
634 		orderly_poweroff(true);
635 	}
636 
637 	return 0;
638 }
639 
640 static const struct amdgpu_irq_src_funcs smu9_irq_funcs = {
641 	.process = phm_irq_process,
642 };
643 
smu9_register_irq_handlers(struct pp_hwmgr * hwmgr)644 int smu9_register_irq_handlers(struct pp_hwmgr *hwmgr)
645 {
646 	struct amdgpu_irq_src *source =
647 		kzalloc_obj(struct amdgpu_irq_src);
648 
649 	if (!source)
650 		return -ENOMEM;
651 
652 	source->funcs = &smu9_irq_funcs;
653 
654 	amdgpu_irq_add_id((struct amdgpu_device *)(hwmgr->adev),
655 			SOC15_IH_CLIENTID_THM,
656 			THM_9_0__SRCID__THM_DIG_THERM_L2H,
657 			source);
658 	amdgpu_irq_add_id((struct amdgpu_device *)(hwmgr->adev),
659 			SOC15_IH_CLIENTID_THM,
660 			THM_9_0__SRCID__THM_DIG_THERM_H2L,
661 			source);
662 
663 	/* Register CTF(GPIO_19) interrupt */
664 	amdgpu_irq_add_id((struct amdgpu_device *)(hwmgr->adev),
665 			SOC15_IH_CLIENTID_ROM_SMUIO,
666 			SMUIO_9_0__SRCID__SMUIO_GPIO19,
667 			source);
668 
669 	return 0;
670 }
671 
smu_atom_get_data_table(void * dev,uint32_t table,uint16_t * size,uint8_t * frev,uint8_t * crev)672 void *smu_atom_get_data_table(void *dev, uint32_t table, uint16_t *size,
673 						uint8_t *frev, uint8_t *crev)
674 {
675 	struct amdgpu_device *adev = dev;
676 	uint16_t data_start;
677 
678 	if (amdgpu_atom_parse_data_header(
679 		    adev->mode_info.atom_context, table, size,
680 		    frev, crev, &data_start))
681 		return (uint8_t *)adev->mode_info.atom_context->bios +
682 			data_start;
683 
684 	return NULL;
685 }
686 
smu_get_voltage_dependency_table_ppt_v1(const struct phm_ppt_v1_clock_voltage_dependency_table * allowed_dep_table,struct phm_ppt_v1_clock_voltage_dependency_table * dep_table)687 int smu_get_voltage_dependency_table_ppt_v1(
688 			const struct phm_ppt_v1_clock_voltage_dependency_table *allowed_dep_table,
689 			struct phm_ppt_v1_clock_voltage_dependency_table *dep_table)
690 {
691 	uint8_t i = 0;
692 	PP_ASSERT_WITH_CODE((0 != allowed_dep_table->count),
693 				"Voltage Lookup Table empty",
694 				return -EINVAL);
695 
696 	dep_table->count = allowed_dep_table->count;
697 	for (i = 0; i < dep_table->count; i++) {
698 		dep_table->entries[i].clk = allowed_dep_table->entries[i].clk;
699 		dep_table->entries[i].vddInd = allowed_dep_table->entries[i].vddInd;
700 		dep_table->entries[i].vdd_offset = allowed_dep_table->entries[i].vdd_offset;
701 		dep_table->entries[i].vddc = allowed_dep_table->entries[i].vddc;
702 		dep_table->entries[i].vddgfx = allowed_dep_table->entries[i].vddgfx;
703 		dep_table->entries[i].vddci = allowed_dep_table->entries[i].vddci;
704 		dep_table->entries[i].mvdd = allowed_dep_table->entries[i].mvdd;
705 		dep_table->entries[i].phases = allowed_dep_table->entries[i].phases;
706 		dep_table->entries[i].cks_enable = allowed_dep_table->entries[i].cks_enable;
707 		dep_table->entries[i].cks_voffset = allowed_dep_table->entries[i].cks_voffset;
708 	}
709 
710 	return 0;
711 }
712 
smu_set_watermarks_for_clocks_ranges(void * wt_table,struct dm_pp_wm_sets_with_clock_ranges_soc15 * wm_with_clock_ranges)713 int smu_set_watermarks_for_clocks_ranges(void *wt_table,
714 		struct dm_pp_wm_sets_with_clock_ranges_soc15 *wm_with_clock_ranges)
715 {
716 	uint32_t i;
717 	struct watermarks *table = wt_table;
718 
719 	if (!table || !wm_with_clock_ranges)
720 		return -EINVAL;
721 
722 	if (wm_with_clock_ranges->num_wm_dmif_sets > 4 || wm_with_clock_ranges->num_wm_mcif_sets > 4)
723 		return -EINVAL;
724 
725 	for (i = 0; i < wm_with_clock_ranges->num_wm_dmif_sets; i++) {
726 		table->WatermarkRow[1][i].MinClock =
727 			cpu_to_le16((uint16_t)
728 			(wm_with_clock_ranges->wm_dmif_clocks_ranges[i].wm_min_dcfclk_clk_in_khz /
729 			1000));
730 		table->WatermarkRow[1][i].MaxClock =
731 			cpu_to_le16((uint16_t)
732 			(wm_with_clock_ranges->wm_dmif_clocks_ranges[i].wm_max_dcfclk_clk_in_khz /
733 			1000));
734 		table->WatermarkRow[1][i].MinUclk =
735 			cpu_to_le16((uint16_t)
736 			(wm_with_clock_ranges->wm_dmif_clocks_ranges[i].wm_min_mem_clk_in_khz /
737 			1000));
738 		table->WatermarkRow[1][i].MaxUclk =
739 			cpu_to_le16((uint16_t)
740 			(wm_with_clock_ranges->wm_dmif_clocks_ranges[i].wm_max_mem_clk_in_khz /
741 			1000));
742 		table->WatermarkRow[1][i].WmSetting = (uint8_t)
743 				wm_with_clock_ranges->wm_dmif_clocks_ranges[i].wm_set_id;
744 	}
745 
746 	for (i = 0; i < wm_with_clock_ranges->num_wm_mcif_sets; i++) {
747 		table->WatermarkRow[0][i].MinClock =
748 			cpu_to_le16((uint16_t)
749 			(wm_with_clock_ranges->wm_mcif_clocks_ranges[i].wm_min_socclk_clk_in_khz /
750 			1000));
751 		table->WatermarkRow[0][i].MaxClock =
752 			cpu_to_le16((uint16_t)
753 			(wm_with_clock_ranges->wm_mcif_clocks_ranges[i].wm_max_socclk_clk_in_khz /
754 			1000));
755 		table->WatermarkRow[0][i].MinUclk =
756 			cpu_to_le16((uint16_t)
757 			(wm_with_clock_ranges->wm_mcif_clocks_ranges[i].wm_min_mem_clk_in_khz /
758 			1000));
759 		table->WatermarkRow[0][i].MaxUclk =
760 			cpu_to_le16((uint16_t)
761 			(wm_with_clock_ranges->wm_mcif_clocks_ranges[i].wm_max_mem_clk_in_khz /
762 			1000));
763 		table->WatermarkRow[0][i].WmSetting = (uint8_t)
764 				wm_with_clock_ranges->wm_mcif_clocks_ranges[i].wm_set_id;
765 	}
766 	return 0;
767 }
768