summaryrefslogtreecommitdiffstats
path: root/drivers/gpu/nvgpu/pmgr/pwrmonitor.c
blob: 4602a76efb35578f93161f1d561b0960f13ac1d9 (plain) (blame)
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
/*
 * Copyright (c) 2016-2017, NVIDIA CORPORATION.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 */

#include <nvgpu/bios.h>

#include "gk20a/gk20a.h"
#include "pwrdev.h"
#include "boardobj/boardobjgrp.h"
#include "boardobj/boardobjgrp_e32.h"
#include "gp106/bios_gp106.h"

static u32 _pwr_channel_pmudata_instget(struct gk20a *g,
			struct nv_pmu_boardobjgrp *pmuboardobjgrp,
			struct nv_pmu_boardobj **ppboardobjpmudata,
			u8 idx)
{
	struct nv_pmu_pmgr_pwr_channel_desc *ppmgrchannel =
		(struct nv_pmu_pmgr_pwr_channel_desc *)pmuboardobjgrp;

	gk20a_dbg_info("");

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

	*ppboardobjpmudata = (struct nv_pmu_boardobj *)
		&ppmgrchannel->channels[idx].data.board_obj;

	/* handle Global/common data here as we need index */
	ppmgrchannel->channels[idx].data.pwr_channel.ch_idx = idx;

	gk20a_dbg_info(" Done");

	return 0;
}

static u32 _pwr_channel_rels_pmudata_instget(struct gk20a *g,
			struct nv_pmu_boardobjgrp *pmuboardobjgrp,
			struct nv_pmu_boardobj **ppboardobjpmudata,
			u8 idx)
{
	struct nv_pmu_pmgr_pwr_chrelationship_desc *ppmgrchrels =
		(struct nv_pmu_pmgr_pwr_chrelationship_desc *)pmuboardobjgrp;

	gk20a_dbg_info("");

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

	*ppboardobjpmudata = (struct nv_pmu_boardobj *)
		&ppmgrchrels->ch_rels[idx].data.board_obj;

	gk20a_dbg_info(" Done");

	return 0;
}

static u32 _pwr_channel_state_init(struct gk20a *g)
{
	u8 indx = 0;
	struct pwr_channel *pchannel;
	u32 objmask =
		g->pmgr_pmu.pmgr_monitorobjs.pwr_channels.super.objmask;

	/* Initialize each PWR_CHANNEL's dependent channel mask */
	BOARDOBJGRP_FOR_EACH_INDEX_IN_MASK(32, indx, objmask) {
		pchannel = PMGR_PWR_MONITOR_GET_PWR_CHANNEL(g, indx);
		if (pchannel == NULL) {
			nvgpu_err(g,
				"PMGR_PWR_MONITOR_GET_PWR_CHANNEL-failed %d", indx);
			return -EINVAL;
		}
		pchannel->dependent_ch_mask =0;
	}
	BOARDOBJGRP_FOR_EACH_INDEX_IN_MASK_END

	return 0;
}

static bool _pwr_channel_implements(struct pwr_channel *pchannel,
			u8 type)
{
	return (type == BOARDOBJ_GET_TYPE(pchannel));
}

static u32 _pwr_domains_pmudatainit_sensor(struct gk20a *g,
					struct boardobj *board_obj_ptr,
					struct nv_pmu_boardobj *ppmudata)
{
	struct nv_pmu_pmgr_pwr_channel_sensor *pmu_sensor_data;
	struct pwr_channel_sensor *sensor;
	u32 status = 0;

	status = boardobj_pmudatainit_super(g, board_obj_ptr, ppmudata);
	if (status) {
		nvgpu_err(g,
			  "error updating pmu boardobjgrp for pwr sensor 0x%x",
			  status);
		goto done;
	}

	sensor = (struct pwr_channel_sensor *)board_obj_ptr;
	pmu_sensor_data = (struct nv_pmu_pmgr_pwr_channel_sensor *) ppmudata;

	pmu_sensor_data->super.pwr_rail = sensor->super.pwr_rail;
	pmu_sensor_data->super.volt_fixedu_v = sensor->super.volt_fixed_uv;
	pmu_sensor_data->super.pwr_corr_slope = sensor->super.pwr_corr_slope;
	pmu_sensor_data->super.pwr_corr_offsetm_w = sensor->super.pwr_corr_offset_mw;
	pmu_sensor_data->super.curr_corr_slope = sensor->super.curr_corr_slope;
	pmu_sensor_data->super.curr_corr_offsetm_a = sensor->super.curr_corr_offset_ma;
	pmu_sensor_data->super.dependent_ch_mask = sensor->super.dependent_ch_mask;
	pmu_sensor_data->super.ch_idx = 0;

	pmu_sensor_data->pwr_dev_idx = sensor->pwr_dev_idx;
	pmu_sensor_data->pwr_dev_prov_idx = sensor->pwr_dev_prov_idx;

done:
	return status;
}

static struct boardobj *construct_pwr_topology(struct gk20a *g,
				void *pargs, u16 pargs_size, u8 type)
{
	struct boardobj *board_obj_ptr = NULL;
	u32 status;
	struct pwr_channel_sensor *pwrchannel;
	struct pwr_channel_sensor *sensor = (struct pwr_channel_sensor*)pargs;

	status = boardobj_construct_super(g, &board_obj_ptr,
		pargs_size, pargs);
	if (status)
		return NULL;

	pwrchannel = (struct pwr_channel_sensor*)board_obj_ptr;

	/* Set Super class interfaces */
	board_obj_ptr->pmudatainit = _pwr_domains_pmudatainit_sensor;

	pwrchannel->super.pwr_rail = sensor->super.pwr_rail;
	pwrchannel->super.volt_fixed_uv = sensor->super.volt_fixed_uv;
	pwrchannel->super.pwr_corr_slope = sensor->super.pwr_corr_slope;
	pwrchannel->super.pwr_corr_offset_mw = sensor->super.pwr_corr_offset_mw;
	pwrchannel->super.curr_corr_slope = sensor->super.curr_corr_slope;
	pwrchannel->super.curr_corr_offset_ma = sensor->super.curr_corr_offset_ma;
	pwrchannel->super.dependent_ch_mask = 0;

	pwrchannel->pwr_dev_idx = sensor->pwr_dev_idx;
	pwrchannel->pwr_dev_prov_idx = sensor->pwr_dev_prov_idx;

	gk20a_dbg_info(" Done");

	return board_obj_ptr;
}

static u32 devinit_get_pwr_topology_table(struct gk20a *g,
				struct pmgr_pwr_monitor *ppwrmonitorobjs)
{
	u32 status = 0;
	u8 *pwr_topology_table_ptr = NULL;
	u8 *curr_pwr_topology_table_ptr = NULL;
	struct boardobj *boardobj;
	struct pwr_topology_2x_header pwr_topology_table_header = { 0 };
	struct pwr_topology_2x_entry pwr_topology_table_entry = { 0 };
	u32 index;
	u32 obj_index = 0;
	u16 pwr_topology_size;
	union {
		struct boardobj boardobj;
		struct pwr_channel pwrchannel;
		struct pwr_channel_sensor sensor;
	} pwr_topology_data;

	gk20a_dbg_info("");

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

	memcpy(&pwr_topology_table_header, pwr_topology_table_ptr,
		VBIOS_POWER_TOPOLOGY_2X_HEADER_SIZE_06);

	if (pwr_topology_table_header.version !=
			VBIOS_POWER_TOPOLOGY_VERSION_2X) {
		status = -EINVAL;
		goto done;
	}

	g->pmgr_pmu.pmgr_monitorobjs.b_is_topology_tbl_ver_1x = false;

	if (pwr_topology_table_header.header_size <
			VBIOS_POWER_TOPOLOGY_2X_HEADER_SIZE_06) {
		status = -EINVAL;
		goto done;
	}

	if (pwr_topology_table_header.table_entry_size !=
			VBIOS_POWER_TOPOLOGY_2X_ENTRY_SIZE_16) {
		status = -EINVAL;
		goto done;
	}

	curr_pwr_topology_table_ptr = (pwr_topology_table_ptr +
		VBIOS_POWER_TOPOLOGY_2X_HEADER_SIZE_06);

	for (index = 0; index < pwr_topology_table_header.num_table_entries;
		index++) {
		u8 class_type;

		curr_pwr_topology_table_ptr += (pwr_topology_table_header.table_entry_size * index);

		pwr_topology_table_entry.flags0 = *curr_pwr_topology_table_ptr;
		pwr_topology_table_entry.pwr_rail = *(curr_pwr_topology_table_ptr + 1);

		memcpy(&pwr_topology_table_entry.param0,
			(curr_pwr_topology_table_ptr + 2),
			(VBIOS_POWER_TOPOLOGY_2X_ENTRY_SIZE_16 - 2));

		class_type = (u8)BIOS_GET_FIELD(
			pwr_topology_table_entry.flags0,
			NV_VBIOS_POWER_TOPOLOGY_2X_ENTRY_FLAGS0_CLASS);

		if (class_type == NV_VBIOS_POWER_TOPOLOGY_2X_ENTRY_FLAGS0_CLASS_SENSOR) {
			pwr_topology_data.sensor.pwr_dev_idx = (u8)BIOS_GET_FIELD(
				pwr_topology_table_entry.param1,
				NV_VBIOS_POWER_TOPOLOGY_2X_ENTRY_PARAM1_SENSOR_INDEX);
			pwr_topology_data.sensor.pwr_dev_prov_idx = (u8)BIOS_GET_FIELD(
				pwr_topology_table_entry.param1,
				NV_VBIOS_POWER_TOPOLOGY_2X_ENTRY_PARAM1_SENSOR_PROVIDER_INDEX);

			pwr_topology_size = sizeof(struct pwr_channel_sensor);
		} else
			continue;

		/* Initialize data for the parent class */
		pwr_topology_data.boardobj.type = CTRL_PMGR_PWR_CHANNEL_TYPE_SENSOR;
		pwr_topology_data.pwrchannel.pwr_rail = (u8)pwr_topology_table_entry.pwr_rail;
		pwr_topology_data.pwrchannel.volt_fixed_uv = pwr_topology_table_entry.param0;
		pwr_topology_data.pwrchannel.pwr_corr_slope = (1 << 12);
		pwr_topology_data.pwrchannel.pwr_corr_offset_mw = 0;
		pwr_topology_data.pwrchannel.curr_corr_slope  =
			(u32)pwr_topology_table_entry.curr_corr_slope;
		pwr_topology_data.pwrchannel.curr_corr_offset_ma =
			(s32)pwr_topology_table_entry.curr_corr_offset;

		boardobj = construct_pwr_topology(g, &pwr_topology_data,
					pwr_topology_size, pwr_topology_data.boardobj.type);

		if (!boardobj) {
			nvgpu_err(g,
				"unable to create pwr topology for %d type %d",
				index, pwr_topology_data.boardobj.type);
			status = -EINVAL;
			goto done;
		}

		status = boardobjgrp_objinsert(&ppwrmonitorobjs->pwr_channels.super,
				boardobj, obj_index);

		if (status) {
			nvgpu_err(g,
				"unable to insert pwr topology boardobj for %d", index);
			status = -EINVAL;
			goto done;
		}

		++obj_index;
	}

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

u32 pmgr_monitor_sw_setup(struct gk20a *g)
{
	u32 status;
	struct boardobjgrp *pboardobjgrp = NULL;
	struct pwr_channel *pchannel;
	struct pmgr_pwr_monitor *ppwrmonitorobjs;
	u8 indx = 0;

	/* Construct the Super Class and override the Interfaces */
	status = boardobjgrpconstruct_e32(
		&g->pmgr_pmu.pmgr_monitorobjs.pwr_channels);
	if (status) {
		nvgpu_err(g,
			"error creating boardobjgrp for pmgr channel, status - 0x%x",
			status);
		goto done;
	}

	pboardobjgrp = &(g->pmgr_pmu.pmgr_monitorobjs.pwr_channels.super);

	/* Override the Interfaces */
	pboardobjgrp->pmudatainstget = _pwr_channel_pmudata_instget;

	/* Construct the Super Class and override the Interfaces */
	status = boardobjgrpconstruct_e32(
			&g->pmgr_pmu.pmgr_monitorobjs.pwr_ch_rels);
	if (status) {
		nvgpu_err(g,
			"error creating boardobjgrp for pmgr channel relationship, status - 0x%x",
			status);
		goto done;
	}

	pboardobjgrp = &(g->pmgr_pmu.pmgr_monitorobjs.pwr_ch_rels.super);

	/* Override the Interfaces */
	pboardobjgrp->pmudatainstget = _pwr_channel_rels_pmudata_instget;

	/* Initialize the Total GPU Power Channel Mask to 0 */
	g->pmgr_pmu.pmgr_monitorobjs.pmu_data.channels.hdr.data.total_gpu_power_channel_mask = 0;
	g->pmgr_pmu.pmgr_monitorobjs.total_gpu_channel_idx =
			CTRL_PMGR_PWR_CHANNEL_INDEX_INVALID;

	/* Supported topology table version 1.0 */
	g->pmgr_pmu.pmgr_monitorobjs.b_is_topology_tbl_ver_1x = true;

	ppwrmonitorobjs = &(g->pmgr_pmu.pmgr_monitorobjs);

	status = devinit_get_pwr_topology_table(g, ppwrmonitorobjs);
	if (status)
		goto done;

	status = _pwr_channel_state_init(g);
	if (status)
		goto done;

	/* Initialise physicalChannelMask */
	g->pmgr_pmu.pmgr_monitorobjs.physical_channel_mask = 0;

	pboardobjgrp = &g->pmgr_pmu.pmgr_monitorobjs.pwr_channels.super;

	BOARDOBJGRP_FOR_EACH(pboardobjgrp, struct pwr_channel *, pchannel, indx) {
		if (_pwr_channel_implements(pchannel,
				CTRL_PMGR_PWR_CHANNEL_TYPE_SENSOR)) {
			g->pmgr_pmu.pmgr_monitorobjs.physical_channel_mask  |= BIT(indx);
		}
	}

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