summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/amd/powerplay/hwmgr/ppatomfwctrl.c
blob: 720d5006ff62842efbf46bdc47ea03bba6ad5bb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
 * Copyright 2016 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#include "ppatomfwctrl.h"
#include "atomfirmware.h"
#include "pp_debug.h"


static const union atom_voltage_object_v4 *pp_atomfwctrl_lookup_voltage_type_v4(
		const struct atom_voltage_objects_info_v4_1 *voltage_object_info_table,
		uint8_t voltage_type, uint8_t voltage_mode)
{
	unsigned int size = le16_to_cpu(
			voltage_object_info_table->table_header.structuresize);
	unsigned int offset =
			offsetof(struct atom_voltage_objects_info_v4_1, voltage_object[0]);
	unsigned long start = (unsigned long)voltage_object_info_table;

	while (offset < size) {
		const union atom_voltage_object_v4 *voltage_object =
				(const union atom_voltage_object_v4 *)(start + offset);

        if (voltage_type == voltage_object->gpio_voltage_obj.header.voltage_type &&
            voltage_mode == voltage_object->gpio_voltage_obj.header.voltage_mode)
            return voltage_object;

        offset += le16_to_cpu(voltage_object->gpio_voltage_obj.header.object_size);

    }

    return NULL;
}

static struct atom_voltage_objects_info_v4_1 *pp_atomfwctrl_get_voltage_info_table(
		struct pp_hwmgr *hwmgr)
{
    const void *table_address;
    uint16_t idx;

    idx = GetIndexIntoMasterDataTable(voltageobject_info);
    table_address =	cgs_atom_get_data_table(hwmgr->device,
    		idx, NULL, NULL, NULL);

    PP_ASSERT_WITH_CODE( 
        table_address,
        "Error retrieving BIOS Table Address!",
        return NULL);

    return (struct atom_voltage_objects_info_v4_1 *)table_address;
}

/**
* Returns TRUE if the given voltage type is controlled by GPIO pins.
* voltage_type is one of SET_VOLTAGE_TYPE_ASIC_VDDC, SET_VOLTAGE_TYPE_ASIC_MVDDC, SET_VOLTAGE_TYPE_ASIC_MVDDQ.
* voltage_mode is one of ATOM_SET_VOLTAGE, ATOM_SET_VOLTAGE_PHASE
*/
bool pp_atomfwctrl_is_voltage_controlled_by_gpio_v4(struct pp_hwmgr *hwmgr,
		uint8_t voltage_type, uint8_t voltage_mode)
{
	struct atom_voltage_objects_info_v4_1 *voltage_info =
			(struct atom_voltage_objects_info_v4_1 *)
			pp_atomfwctrl_get_voltage_info_table(hwmgr);
	bool ret;

	/* If we cannot find the table do NOT try to control this voltage. */
	PP_ASSERT_WITH_CODE(voltage_info,
			"Could not find Voltage Table in BIOS.",
			return false);

	ret = (pp_atomfwctrl_lookup_voltage_type_v4(voltage_info,
			voltage_type, voltage_mode)) ? true : false;

	return ret;
}

int pp_atomfwctrl_get_voltage_table_v4(struct pp_hwmgr *hwmgr,
		uint8_t voltage_type, uint8_t voltage_mode,
		struct pp_atomfwctrl_voltage_table *voltage_table)
{
	struct atom_voltage_objects_info_v4_1 *voltage_info =
			(struct atom_voltage_objects_info_v4_1 *)
			pp_atomfwctrl_get_voltage_info_table(hwmgr);
	const union atom_voltage_object_v4 *voltage_object;
	unsigned int i;
	int result = 0;

	PP_ASSERT_WITH_CODE(voltage_info,
			"Could not find Voltage Table in BIOS.",
			return -1);

	voltage_object = pp_atomfwctrl_lookup_voltage_type_v4(voltage_info,
			voltage_type, voltage_mode);

	if (!voltage_object)
		return -1;

	voltage_table->count = 0;
	if (voltage_mode == VOLTAGE_OBJ_GPIO_LUT) {
		PP_ASSERT_WITH_CODE(
				(voltage_object->gpio_voltage_obj.gpio_entry_num <=
				PP_ATOMFWCTRL_MAX_VOLTAGE_ENTRIES),
				"Too many voltage entries!",
				result = -1);

		if (!result) {
			for (i = 0; i < voltage_object->gpio_voltage_obj.
							gpio_entry_num; i++) {
				voltage_table->entries[i].value =
						le16_to_cpu(voltage_object->gpio_voltage_obj.
						voltage_gpio_lut[i].voltage_level_mv);
				voltage_table->entries[i].smio_low =
						le32_to_cpu(voltage_object->gpio_voltage_obj.
						voltage_gpio_lut[i].voltage_gpio_reg_val);
			}
			voltage_table->count =
					voltage_object->gpio_voltage_obj.gpio_entry_num;
			voltage_table->mask_low =
					le32_to_cpu(
					voltage_object->gpio_voltage_obj.gpio_mask_val);
			voltage_table->phase_delay =
					voltage_object->gpio_voltage_obj.phase_delay_us;
		}
	} else if (voltage_mode == VOLTAGE_OBJ_SVID2) {
		voltage_table->psi1_enable =
			voltage_object->svid2_voltage_obj.loadline_psi1 & 0x1;
		voltage_table->psi0_enable =
			voltage_object->svid2_voltage_obj.psi0_enable & 0x1;
		voltage_table->max_vid_step =
			voltage_object->svid2_voltage_obj.maxvstep;
		voltage_table->telemetry_offset =
			voltage_object->svid2_voltage_obj.telemetry_offset;
		voltage_table->telemetry_slope =
			voltage_object->svid2_voltage_obj.telemetry_gain;
	} else
		PP_ASSERT_WITH_CODE(false,
				"Unsupported Voltage Object Mode!",
				result = -1);

	return result;
}

 
static struct atom_gpio_pin_lut_v2_1 *pp_atomfwctrl_get_gpio_lookup_table(
		struct pp_hwmgr *hwmgr)
{
	const void *table_address;
	uint16_t idx;

	idx = GetIndexIntoMasterDataTable(gpio_pin_lut);
	table_address =	cgs_atom_get_data_table(hwmgr->device,
			idx, NULL, NULL, NULL);
	PP_ASSERT_WITH_CODE(table_address,
			"Error retrieving BIOS Table Address!",
			return NULL);

	return (struct atom_gpio_pin_lut_v2_1 *)table_address;
}

static bool pp_atomfwctrl_lookup_gpio_pin(
		struct atom_gpio_pin_lut_v2_1 *gpio_lookup_table,
		const uint32_t pin_id,
		struct pp_atomfwctrl_gpio_pin_assignment *gpio_pin_assignment)
{
	unsigned int size = le16_to_cpu(
			gpio_lookup_table->table_header.structuresize);
	unsigned int offset =
			offsetof(struct atom_gpio_pin_lut_v2_1, gpio_pin[0]);
	unsigned long start = (unsigned long)gpio_lookup_table;

	while (offset < size) {
		const struct  atom_gpio_pin_assignment *pin_assignment =
				(const struct  atom_gpio_pin_assignment *)(start + offset);

		if (pin_id == pin_assignment->gpio_id)  {
			gpio_pin_assignment->uc_gpio_pin_bit_shift =
					pin_assignment->gpio_bitshift;
			gpio_pin_assignment->us_gpio_pin_aindex =
					le16_to_cpu(pin_assignment->data_a_reg_index);
			return true;
		}
		offset += offsetof(struct atom_gpio_pin_assignment, gpio_id) + 1;
	}
	return false;
}

/**
* Returns TRUE if the given pin id find in lookup table.
*/
bool pp_atomfwctrl_get_pp_assign_pin(struct pp_hwmgr *hwmgr,
		const uint32_t pin_id,
		struct pp_atomfwctrl_gpio_pin_assignment *gpio_pin_assignment)
{
	bool ret = false;
	struct atom_gpio_pin_lut_v2_1 *gpio_lookup_table =
			pp_atomfwctrl_get_gpio_lookup_table(hwmgr);

	/* If we cannot find the table do NOT try to control this voltage. */
	PP_ASSERT_WITH_CODE(gpio_lookup_table,
			"Could not find GPIO lookup Table in BIOS.",
			return false);

	ret = pp_atomfwctrl_lookup_gpio_pin(gpio_lookup_table,
			pin_id, gpio_pin_assignment);

	return ret;
}

/**
* Enter to SelfRefresh mode.
* @param hwmgr
*/
int pp_atomfwctrl_enter_self_refresh(struct pp_hwmgr *hwmgr)
{
	/* 0 - no action
	 * 1 - leave power to video memory always on
	 */
	return 0;
}

/** pp_atomfwctrl_get_gpu_pll_dividers_vega10().
 *
 * @param hwmgr       input parameter: pointer to HwMgr
 * @param clock_type  input parameter: Clock type: 1 - GFXCLK, 2 - UCLK, 0 - All other clocks
 * @param clock_value input parameter: Clock
 * @param dividers    output parameter:Clock dividers
 */
int pp_atomfwctrl_get_gpu_pll_dividers_vega10(struct pp_hwmgr *hwmgr,
		uint32_t clock_type, uint32_t clock_value,
		struct pp_atomfwctrl_clock_dividers_soc15 *dividers)
{
	struct compute_gpu_clock_input_parameter_v1_8 pll_parameters;
	struct compute_gpu_clock_output_parameter_v1_8 *pll_output;
	int result;
	uint32_t idx;

	pll_parameters.gpuclock_10khz = (uint32_t)clock_value;
	pll_parameters.gpu_clock_type = clock_type;

	idx = GetIndexIntoMasterCmdTable(computegpuclockparam);
	result = cgs_atom_exec_cmd_table(hwmgr->device, idx, &pll_parameters);

	if (!result) {
		pll_output = (struct compute_gpu_clock_output_parameter_v1_8 *)
				&pll_parameters;
		dividers->ulClock = le32_to_cpu(pll_output->gpuclock_10khz);
		dividers->ulDid = le32_to_cpu(pll_output->dfs_did);
		dividers->ulPll_fb_mult = le32_to_cpu(pll_output->pll_fb_mult);
		dividers->ulPll_ss_fbsmult = le32_to_cpu(pll_output->pll_ss_fbsmult);
		dividers->usPll_ss_slew_frac = le16_to_cpu(pll_output->pll_ss_slew_frac);
		dividers->ucPll_ss_enable = pll_output->pll_ss_enable;
	}
	return result;
}

int pp_atomfwctrl_get_avfs_information(struct pp_hwmgr *hwmgr,
		struct pp_atomfwctrl_avfs_parameters *param)
{
	uint16_t idx;
	struct atom_asic_profiling_info_v4_1 *profile;

	idx = GetIndexIntoMasterDataTable(asic_profiling_info);
	profile = (struct atom_asic_profiling_info_v4_1 *)
			cgs_atom_get_data_table(hwmgr->device,
					idx, NULL, NULL, NULL);

	if (!profile)
		return -1;

	param->ulMaxVddc = le32_to_cpu(profile->maxvddc);
	param->ulMinVddc = le32_to_cpu(profile->minvddc);
	param->ulMeanNsigmaAcontant0 =
			le32_to_cpu(profile->avfs_meannsigma_acontant0);
	param->ulMeanNsigmaAcontant1 =
			le32_to_cpu(profile->avfs_meannsigma_acontant1);
	param->ulMeanNsigmaAcontant2 =
			le32_to_cpu(profile->avfs_meannsigma_acontant2);
	param->usMeanNsigmaDcTolSigma =
			le16_to_cpu(profile->avfs_meannsigma_dc_tol_sigma);
	param->usMeanNsigmaPlatformMean =
			le16_to_cpu(profile->avfs_meannsigma_platform_mean);
	param->usMeanNsigmaPlatformSigma =
			le16_to_cpu(profile->avfs_meannsigma_platform_sigma);
	param->ulGbVdroopTableCksoffA0 =
			le32_to_cpu(profile->gb_vdroop_table_cksoff_a0);
	param->ulGbVdroopTableCksoffA1 =
			le32_to_cpu(profile->gb_vdroop_table_cksoff_a1);
	param->ulGbVdroopTableCksoffA2 =
			le32_to_cpu(profile->gb_vdroop_table_cksoff_a2);
	param->ulGbVdroopTableCksonA0 =
			le32_to_cpu(profile->gb_vdroop_table_ckson_a0);
	param->ulGbVdroopTableCksonA1 =
			le32_to_cpu(profile->gb_vdroop_table_ckson_a1);
	param->ulGbVdroopTableCksonA2 =
			le32_to_cpu(profile->gb_vdroop_table_ckson_a2);
	param->ulGbFuseTableCksoffM1 =
			le32_to_cpu(profile->avfsgb_fuse_table_cksoff_m1);
	param->ulGbFuseTableCksoffM2 =
			le32_to_cpu(profile->avfsgb_fuse_table_cksoff_m2);
	param->ulGbFuseTableCksoffB =
			le32_to_cpu(profile->avfsgb_fuse_table_cksoff_b);
	param->ulGbFuseTableCksonM1 =
			le32_to_cpu(profile->avfsgb_fuse_table_ckson_m1);
	param->ulGbFuseTableCksonM2 =
			le32_to_cpu(profile->avfsgb_fuse_table_ckson_m2);
	param->ulGbFuseTableCksonB =
			le32_to_cpu(profile->avfsgb_fuse_table_ckson_b);

	param->ucEnableGbVdroopTableCkson =
			profile->enable_gb_vdroop_table_ckson;
	param->ucEnableGbFuseTableCkson =
			profile->enable_gb_fuse_table_ckson;
	param->usPsmAgeComfactor =
			le16_to_cpu(profile->psm_age_comfactor);

	param->ulDispclk2GfxclkM1 =
			le32_to_cpu(profile->dispclk2gfxclk_a);
	param->ulDispclk2GfxclkM2 =
			le32_to_cpu(profile->dispclk2gfxclk_b);
	param->ulDispclk2GfxclkB =
			le32_to_cpu(profile->dispclk2gfxclk_c);
	param->ulDcefclk2GfxclkM1 =
			le32_to_cpu(profile->dcefclk2gfxclk_a);
	param->ulDcefclk2GfxclkM2 =
			le32_to_cpu(profile->dcefclk2gfxclk_b);
	param->ulDcefclk2GfxclkB =
			le32_to_cpu(profile->dcefclk2gfxclk_c);
	param->ulPixelclk2GfxclkM1 =
			le32_to_cpu(profile->pixclk2gfxclk_a);
	param->ulPixelclk2GfxclkM2 =
			le32_to_cpu(profile->pixclk2gfxclk_b);
	param->ulPixelclk2GfxclkB =
			le32_to_cpu(profile->pixclk2gfxclk_c);
	param->ulPhyclk2GfxclkM1 =
			le32_to_cpu(profile->phyclk2gfxclk_a);
	param->ulPhyclk2GfxclkM2 =
			le32_to_cpu(profile->phyclk2gfxclk_b);
	param->ulPhyclk2GfxclkB =
			le32_to_cpu(profile->phyclk2gfxclk_c);

	return 0;
}

int pp_atomfwctrl_get_gpio_information(struct pp_hwmgr *hwmgr,
		struct pp_atomfwctrl_gpio_parameters *param)
{
	struct atom_smu_info_v3_1 *info;
	uint16_t idx;

	idx = GetIndexIntoMasterDataTable(smu_info);
	info = (struct atom_smu_info_v3_1 *)
		cgs_atom_get_data_table(hwmgr->device,
				idx, NULL, NULL, NULL);

	if (!info) {
		pr_info("Error retrieving BIOS smu_info Table Address!");
		return -1;
	}

	param->ucAcDcGpio       = info->ac_dc_gpio_bit;
	param->ucAcDcPolarity   = info->ac_dc_polarity;
	param->ucVR0HotGpio     = info->vr0hot_gpio_bit;
	param->ucVR0HotPolarity = info->vr0hot_polarity;
	param->ucVR1HotGpio     = info->vr1hot_gpio_bit;
	param->ucVR1HotPolarity = info->vr1hot_polarity;
	param->ucFwCtfGpio      = info->fw_ctf_gpio_bit;
	param->ucFwCtfPolarity  = info->fw_ctf_polarity;

	return 0;
}

int pp_atomfwctrl__get_clk_information_by_clkid(struct pp_hwmgr *hwmgr, BIOS_CLKID id, uint32_t *frequency)
{
	struct atom_get_smu_clock_info_parameters_v3_1   parameters;
	struct atom_get_smu_clock_info_output_parameters_v3_1 *output;
	uint32_t ix;

	parameters.clk_id = id;
	parameters.command = GET_SMU_CLOCK_INFO_V3_1_GET_CLOCK_FREQ;

	ix = GetIndexIntoMasterCmdTable(getsmuclockinfo);
	if (!cgs_atom_exec_cmd_table(hwmgr->device, ix, &parameters)) {
		output = (struct atom_get_smu_clock_info_output_parameters_v3_1 *)&parameters;
		*frequency = output->atom_smu_outputclkfreq.smu_clock_freq_hz / 10000;
	} else {
		pr_info("Error execute_table getsmuclockinfo!");
		return -1;
	}

	return 0;
}

int pp_atomfwctrl_get_vbios_bootup_values(struct pp_hwmgr *hwmgr,
			struct pp_atomfwctrl_bios_boot_up_values *boot_values)
{
	struct atom_firmware_info_v3_1 *info = NULL;
	uint16_t ix;
	uint32_t frequency = 0;

	ix = GetIndexIntoMasterDataTable(firmwareinfo);
	info = (struct atom_firmware_info_v3_1 *)
		cgs_atom_get_data_table(hwmgr->device,
				ix, NULL, NULL, NULL);

	if (!info) {
		pr_info("Error retrieving BIOS firmwareinfo!");
		return -EINVAL;
	}

	boot_values->ulRevision = info->firmware_revision;
	boot_values->ulGfxClk   = info->bootup_sclk_in10khz;
	boot_values->ulUClk     = info->bootup_mclk_in10khz;
	boot_values->usVddc     = info->bootup_vddc_mv;
	boot_values->usVddci    = info->bootup_vddci_mv;
	boot_values->usMvddc    = info->bootup_mvddc_mv;
	boot_values->usVddGfx   = info->bootup_vddgfx_mv;
	boot_values->ulSocClk   = 0;
	boot_values->ulDCEFClk   = 0;

	if (!pp_atomfwctrl__get_clk_information_by_clkid(hwmgr, SMU9_SYSPLL0_SOCCLK_ID, &frequency))
		boot_values->ulSocClk   = frequency;

	if (!pp_atomfwctrl__get_clk_information_by_clkid(hwmgr, SMU9_SYSPLL0_DCEFCLK_ID, &frequency))
		boot_values->ulDCEFClk   = frequency;

	return 0;
}