aboutsummaryrefslogblamecommitdiffstats
path: root/include/volt/volt_policy.c
blob: cc60730d0c4c2ec980ad9475209fb93021c48134 (plain) (tree)
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
































































































































































































































































































































































































































































































































                                                                                           
/*
 * Copyright (c) 2016-2018, NVIDIA CORPORATION.  All rights reserved.
 *
 * 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 AUTHORS OR COPYRIGHT HOLDERS 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 <nvgpu/bios.h>
#include <nvgpu/gk20a.h>

#include "boardobj/boardobjgrp.h"
#include "boardobj/boardobjgrp_e32.h"
#include "gp106/bios_gp106.h"
#include "ctrl/ctrlvolt.h"

#include "volt.h"

static int volt_policy_pmu_data_init_super(struct gk20a *g,
	struct boardobj *pboardobj, struct nv_pmu_boardobj *ppmudata)
{
	return boardobj_pmudatainit_super(g, pboardobj, ppmudata);
}

static int construct_volt_policy(struct gk20a *g,
	struct boardobj  **ppboardobj, u16 size, void *pArgs)
{
	struct voltage_policy *pvolt_policy = NULL;
	int status = 0;

	status = boardobj_construct_super(g, ppboardobj, size, pArgs);
	if (status) {
		return status;
	}

	pvolt_policy = (struct voltage_policy *)*ppboardobj;

	pvolt_policy->super.pmudatainit = volt_policy_pmu_data_init_super;

	return status;
}

static int construct_volt_policy_split_rail(struct gk20a *g,
	struct boardobj **ppboardobj, u16 size, void *pArgs)
{
	struct voltage_policy_split_rail *ptmp_policy  =
			(struct voltage_policy_split_rail *)pArgs;
	struct voltage_policy_split_rail *pvolt_policy = NULL;
	int status = 0;

	status = construct_volt_policy(g, ppboardobj, size, pArgs);
	if (status) {
		return status;
	}

	pvolt_policy = (struct voltage_policy_split_rail *)*ppboardobj;

	pvolt_policy->rail_idx_master = ptmp_policy->rail_idx_master;
	pvolt_policy->rail_idx_slave = ptmp_policy->rail_idx_slave;
	pvolt_policy->delta_min_vfe_equ_idx =
			ptmp_policy->delta_min_vfe_equ_idx;
	pvolt_policy->delta_max_vfe_equ_idx =
			ptmp_policy->delta_max_vfe_equ_idx;

	return status;
}

static int construct_volt_policy_single_rail(struct gk20a *g,
	struct boardobj **ppboardobj, u16 size, void *pArgs)
{
	struct voltage_policy_single_rail *ptmp_policy  =
			(struct voltage_policy_single_rail *)pArgs;
	struct voltage_policy_single_rail *pvolt_policy = NULL;
	int status = 0;

	status = construct_volt_policy(g, ppboardobj, size, pArgs);
	if (status) {
		return status;
	}

	pvolt_policy = (struct voltage_policy_single_rail *)*ppboardobj;

	pvolt_policy->rail_idx = ptmp_policy->rail_idx;

	return status;
}

static int volt_policy_pmu_data_init_single_rail(struct gk20a *g,
	struct boardobj *pboardobj, struct nv_pmu_boardobj *ppmudata)
{
	int status = 0;
	struct voltage_policy_single_rail *ppolicy;
	struct nv_pmu_volt_volt_policy_sr_boardobj_set *pset;

	status = volt_policy_pmu_data_init_super(g, pboardobj, ppmudata);
	if (status) {
		goto done;
	}

	ppolicy = (struct voltage_policy_single_rail *)pboardobj;
	pset = (struct nv_pmu_volt_volt_policy_sr_boardobj_set *)
				ppmudata;
	pset->rail_idx = ppolicy->rail_idx;

done:
	return status;
}

static int volt_policy_pmu_data_init_sr_multi_step(struct gk20a *g,
	struct boardobj *pboardobj, struct nv_pmu_boardobj *ppmudata)
{
	int status = 0;
	struct voltage_policy_single_rail_multi_step *ppolicy;
	struct nv_pmu_volt_volt_policy_sr_multi_step_boardobj_set *pset;

	status = volt_policy_pmu_data_init_single_rail(g, pboardobj, ppmudata);
	if (status) {
		goto done;
	}

	ppolicy = (struct voltage_policy_single_rail_multi_step *)pboardobj;
	pset = (struct nv_pmu_volt_volt_policy_sr_multi_step_boardobj_set *)
				ppmudata;

	pset->ramp_up_step_size_uv   = ppolicy->ramp_up_step_size_uv;
	pset->ramp_down_step_size_uv = ppolicy->ramp_down_step_size_uv;
	pset->inter_switch_delay_us = ppolicy->inter_switch_delay_us;

done:
	return status;
}

static int volt_construct_volt_policy_single_rail_multi_step(struct gk20a *g,
	struct boardobj **ppboardobj, u16 size, void *pargs)
{
	struct boardobj *pboardobj   = NULL;
	struct voltage_policy_single_rail_multi_step *p_volt_policy = NULL;
	struct voltage_policy_single_rail_multi_step *tmp_policy =
		(struct voltage_policy_single_rail_multi_step *)pargs;
	int status = 0;

	status = construct_volt_policy_single_rail(g, ppboardobj, size, pargs);
	if (status) {
		return status;
	}

	pboardobj = (*ppboardobj);
	p_volt_policy = (struct voltage_policy_single_rail_multi_step *)
						*ppboardobj;

	pboardobj->pmudatainit = volt_policy_pmu_data_init_sr_multi_step;

	p_volt_policy->ramp_up_step_size_uv =
		tmp_policy->ramp_up_step_size_uv;
	p_volt_policy->ramp_down_step_size_uv =
		tmp_policy->ramp_down_step_size_uv;
	p_volt_policy->inter_switch_delay_us =
		tmp_policy->inter_switch_delay_us;

	return status;
}

static int volt_policy_pmu_data_init_split_rail(struct gk20a *g,
	struct boardobj *pboardobj, struct nv_pmu_boardobj *ppmudata)
{
	int status = 0;
	struct voltage_policy_split_rail *ppolicy;
	struct nv_pmu_volt_volt_policy_splt_r_boardobj_set *pset;

	status = volt_policy_pmu_data_init_super(g, pboardobj, ppmudata);
	if (status) {
		goto done;
	}

	ppolicy = (struct voltage_policy_split_rail *)pboardobj;
	pset = (struct nv_pmu_volt_volt_policy_splt_r_boardobj_set *)
				ppmudata;

	pset->rail_idx_master = ppolicy->rail_idx_master;
	pset->rail_idx_slave = ppolicy->rail_idx_slave;
	pset->delta_min_vfe_equ_idx = ppolicy->delta_min_vfe_equ_idx;
	pset->delta_max_vfe_equ_idx = ppolicy->delta_max_vfe_equ_idx;
	pset->offset_delta_min_uv = ppolicy->offset_delta_min_uv;
	pset->offset_delta_max_uv = ppolicy->offset_delta_max_uv;

done:
	return status;
}

static int volt_construct_volt_policy_split_rail_single_step(struct gk20a *g,
	struct boardobj **ppboardobj, u16 size, void *pargs)
{
	struct boardobj *pboardobj   = NULL;
	int status = 0;

	status = construct_volt_policy_split_rail(g, ppboardobj, size, pargs);
	if (status) {
		return status;
	}

	pboardobj = (*ppboardobj);
	pboardobj->pmudatainit = volt_policy_pmu_data_init_split_rail;

	return status;
}

static struct voltage_policy *volt_volt_policy_construct(struct gk20a *g, void *pargs)
{
	struct boardobj *pboard_obj = NULL;
	int status = 0;

	switch (BOARDOBJ_GET_TYPE(pargs)) {
	case CTRL_VOLT_POLICY_TYPE_SR_SINGLE_STEP:
		status = volt_construct_volt_policy_split_rail_single_step(g,
			&pboard_obj,
			sizeof(struct voltage_policy_split_rail_single_step),
			pargs);
		if (status) {
			nvgpu_err(g,
				"Could not allocate memory for voltage_policy");
			pboard_obj = NULL;
		}
		break;
	case CTRL_VOLT_POLICY_TYPE_SINGLE_RAIL_MULTI_STEP:
		status = volt_construct_volt_policy_single_rail_multi_step(g,
			&pboard_obj,
			sizeof(struct voltage_policy_single_rail_multi_step),
			pargs);
		if (status) {
			nvgpu_err(g,
				"Could not allocate memory for voltage_policy");
			pboard_obj = NULL;
		}
		break;
	}

	return (struct voltage_policy *)pboard_obj;
}

static u8 volt_policy_type_convert(u8 vbios_type)
{
	switch (vbios_type) {
	case NV_VBIOS_VOLTAGE_POLICY_1X_ENTRY_TYPE_SINGLE_RAIL:
		return CTRL_VOLT_POLICY_TYPE_SINGLE_RAIL;

	case NV_VBIOS_VOLTAGE_POLICY_1X_ENTRY_TYPE_SR_MULTI_STEP:
		return CTRL_VOLT_POLICY_TYPE_SR_MULTI_STEP;

	case NV_VBIOS_VOLTAGE_POLICY_1X_ENTRY_TYPE_SR_SINGLE_STEP:
		return CTRL_VOLT_POLICY_TYPE_SR_SINGLE_STEP;
	case NV_VBIOS_VOLTAGE_POLICY_1X_ENTRY_TYPE_SINGLE_RAIL_MULTI_STEP:
		return CTRL_VOLT_POLICY_TYPE_SINGLE_RAIL_MULTI_STEP;
	}

	return CTRL_VOLT_POLICY_TYPE_INVALID;
}

static int volt_get_volt_policy_table(struct gk20a *g,
		struct voltage_policy_metadata *pvolt_policy_metadata)
{
	int status = 0;
	u8 *voltage_policy_table_ptr = NULL;
	struct voltage_policy *ppolicy = NULL;
	struct vbios_voltage_policy_table_1x_header header = { 0 };
	struct vbios_voltage_policy_table_1x_entry entry  = { 0 };
	u8 i;
	u8 policy_type = 0;
	u8 *entry_offset;
	union policy_type {
		struct boardobj		board_obj;
		struct voltage_policy	volt_policy;
		struct voltage_policy_split_rail	split_rail;
		struct voltage_policy_single_rail_multi_step single_rail_ms;
	} policy_type_data;

	voltage_policy_table_ptr =
		(u8 *)nvgpu_bios_get_perf_table_ptrs(g,
			g->bios.perf_token, VOLTAGE_POLICY_TABLE);
	if (voltage_policy_table_ptr == NULL) {
		status = -EINVAL;
		goto done;
	}

	memcpy(&header, voltage_policy_table_ptr,
			sizeof(struct vbios_voltage_policy_table_1x_header));

	/* Set Voltage Policy Table Index for Perf Core VF Sequence client. */
	pvolt_policy_metadata->perf_core_vf_seq_policy_idx =
		(u8)header.perf_core_vf_seq_policy_idx;

	/* Read in the entries. */
	for (i = 0; i < header.num_table_entries; i++) {
		entry_offset = (voltage_policy_table_ptr + header.header_size +
						i * header.table_entry_size);

		memcpy(&entry, entry_offset,
			sizeof(struct vbios_voltage_policy_table_1x_entry));

		memset(&policy_type_data, 0x0, sizeof(policy_type_data));

		policy_type = volt_policy_type_convert((u8)entry.type);

		switch (policy_type) {
		case  CTRL_VOLT_POLICY_TYPE_SR_SINGLE_STEP:
			policy_type_data.split_rail.rail_idx_master =
				(u8)BIOS_GET_FIELD(entry.param0,
				  NV_VBIOS_VPT_ENTRY_PARAM0_SR_VD_MASTER);

			policy_type_data.split_rail.rail_idx_slave =
				(u8)BIOS_GET_FIELD(entry.param0,
				  NV_VBIOS_VPT_ENTRY_PARAM0_SR_VD_SLAVE);

			policy_type_data.split_rail.delta_min_vfe_equ_idx =
				(u8)BIOS_GET_FIELD(entry.param0,
				  NV_VBIOS_VPT_ENTRY_PARAM0_SR_DELTA_SM_MIN);

			policy_type_data.split_rail.delta_max_vfe_equ_idx =
				(u8)BIOS_GET_FIELD(entry.param0,
				  NV_VBIOS_VPT_ENTRY_PARAM0_SR_DELTA_SM_MAX);
			break;
		case CTRL_VOLT_POLICY_TYPE_SINGLE_RAIL_MULTI_STEP:
			policy_type_data.single_rail_ms.inter_switch_delay_us =
			(u16)BIOS_GET_FIELD(entry.param1,
			NV_VBIOS_VPT_ENTRY_PARAM1_SR_SETTLE_TIME_INTERMEDIATE);
			policy_type_data.single_rail_ms.ramp_up_step_size_uv =
			(u32)BIOS_GET_FIELD(entry.param2,
			NV_VBIOS_VPT_ENTRY_PARAM2_SR_RAMP_UP_STEP_SIZE_UV);
			policy_type_data.single_rail_ms.ramp_down_step_size_uv =
			(u32)BIOS_GET_FIELD(entry.param3,
			NV_VBIOS_VPT_ENTRY_PARAM3_SR_RAMP_DOWN_STEP_SIZE_UV);
			break;
		}

		policy_type_data.board_obj.type = policy_type;

		ppolicy = volt_volt_policy_construct(g,
				(void *)&policy_type_data);
		if (ppolicy == NULL) {
			nvgpu_err(g,
				"Failure to construct VOLT_POLICY object.");
			status = -EINVAL;
			goto done;
		}

		status = boardobjgrp_objinsert(
				&pvolt_policy_metadata->volt_policies.super,
				(struct boardobj *)ppolicy, i);
		if (status) {
			nvgpu_err(g,
				"could not add volt_policy for entry %d into boardobjgrp ",
				i);
			goto done;
		}
	}

done:
	return status;
}
static int _volt_policy_devgrp_pmudata_instget(struct gk20a *g,
	struct nv_pmu_boardobjgrp *pmuboardobjgrp,
	struct nv_pmu_boardobj **ppboardobjpmudata, u8 idx)
{
	struct nv_pmu_volt_volt_policy_boardobj_grp_set *pgrp_set =
		(struct nv_pmu_volt_volt_policy_boardobj_grp_set *)
		pmuboardobjgrp;

	nvgpu_log_info(g, " ");

	/*check whether pmuboardobjgrp has a valid boardobj in index*/
	if (((u32)BIT(idx) &
		pgrp_set->hdr.data.super.obj_mask.super.data[0]) == 0U) {
		return -EINVAL;
	}

	*ppboardobjpmudata = (struct nv_pmu_boardobj *)
		&pgrp_set->objects[idx].data.board_obj;
	nvgpu_log_info(g, " Done");
	return 0;
}

static int _volt_policy_devgrp_pmustatus_instget(struct gk20a *g,
	void *pboardobjgrppmu,
	struct nv_pmu_boardobj_query **ppboardobjpmustatus, u8 idx)
{
	struct nv_pmu_volt_volt_policy_boardobj_grp_get_status *p_get_status =
		(struct nv_pmu_volt_volt_policy_boardobj_grp_get_status *)
		pboardobjgrppmu;

	/*check whether pmuboardobjgrp has a valid boardobj in index*/
	if (((u32)BIT(idx) &
		p_get_status->hdr.data.super.obj_mask.super.data[0]) == 0U) {
		return -EINVAL;
	}

	*ppboardobjpmustatus = (struct nv_pmu_boardobj_query *)
			&p_get_status->objects[idx].data.board_obj;
	return 0;
}

static int _volt_policy_grp_pmudatainit_super(struct gk20a *g,
	struct boardobjgrp *pboardobjgrp,
	struct nv_pmu_boardobjgrp_super *pboardobjgrppmu)
{
	struct nv_pmu_volt_volt_policy_boardobjgrp_set_header *pset =
		(struct nv_pmu_volt_volt_policy_boardobjgrp_set_header *)
		pboardobjgrppmu;
	struct obj_volt *volt  = (struct obj_volt *)pboardobjgrp;
	int status = 0;

	status = boardobjgrp_pmudatainit_e32(g, pboardobjgrp, pboardobjgrppmu);
	if (status) {
		nvgpu_err(g,
			"error updating pmu boardobjgrp for volt policy 0x%x",
			 status);
		goto done;
	}
	pset->perf_core_vf_seq_policy_idx =
		volt->volt_policy_metadata.perf_core_vf_seq_policy_idx;

done:
	return status;
}

int volt_policy_pmu_setup(struct gk20a *g)
{
	int status;
	struct boardobjgrp *pboardobjgrp = NULL;

	nvgpu_log_info(g, " ");

	pboardobjgrp =
		&g->perf_pmu.volt.volt_policy_metadata.volt_policies.super;

	if (!pboardobjgrp->bconstructed) {
		return -EINVAL;
	}

	status = pboardobjgrp->pmuinithandle(g, pboardobjgrp);

	nvgpu_log_info(g, "Done");
	return status;
}

int volt_policy_sw_setup(struct gk20a *g)
{
	int status = 0;
	struct boardobjgrp *pboardobjgrp = NULL;

	nvgpu_log_info(g, " ");

	status = boardobjgrpconstruct_e32(g,
			&g->perf_pmu.volt.volt_policy_metadata.volt_policies);
	if (status) {
		nvgpu_err(g,
			"error creating boardobjgrp for volt rail, status - 0x%x",
			status);
		goto done;
	}

	pboardobjgrp =
		&g->perf_pmu.volt.volt_policy_metadata.volt_policies.super;

	pboardobjgrp->pmudatainstget  = _volt_policy_devgrp_pmudata_instget;
	pboardobjgrp->pmustatusinstget  = _volt_policy_devgrp_pmustatus_instget;
	pboardobjgrp->pmudatainit = _volt_policy_grp_pmudatainit_super;

	/* Obtain Voltage Rail Table from VBIOS */
	status = volt_get_volt_policy_table(g, &g->perf_pmu.volt.
			volt_policy_metadata);
	if (status) {
		goto done;
	}

	/* Populate data for the VOLT_RAIL PMU interface */
	BOARDOBJGRP_PMU_CONSTRUCT(pboardobjgrp, VOLT, VOLT_POLICY);

	status = BOARDOBJGRP_PMU_CMD_GRP_SET_CONSTRUCT(g, pboardobjgrp,
			volt, VOLT, volt_policy, VOLT_POLICY);
	if (status) {
		nvgpu_err(g,
			"error constructing PMU_BOARDOBJ_CMD_GRP_SET interface - 0x%x",
			status);
		goto done;
	}

	status = BOARDOBJGRP_PMU_CMD_GRP_GET_STATUS_CONSTRUCT(g,
		&g->perf_pmu.volt.volt_policy_metadata.volt_policies.super,
			volt, VOLT, volt_policy, VOLT_POLICY);
	if (status) {
		nvgpu_err(g,
			"error constructing PMU_BOARDOBJ_CMD_GRP_SET interface - 0x%x",
			status);
		goto done;
	}

done:
	nvgpu_log_info(g, " done status %x", status);
	return status;
}