summaryrefslogtreecommitdiffstats
path: root/drivers/gpu/nvgpu/therm/thrmdev.c
blob: 5c416e5021be2f605a31bc89e2790e0f64d0c2f7 (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
/*
 * Copyright (c) 2016-2017, 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/pmuif/nvgpu_gpmu_cmdif.h>

#include "gk20a/gk20a.h"
#include "thrmdev.h"
#include "boardobj/boardobjgrp.h"
#include "boardobj/boardobjgrp_e32.h"
#include "gp106/bios_gp106.h"
#include "ctrl/ctrltherm.h"

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

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

	gk20a_dbg_info(" Done");

	return board_obj_ptr;
}

static u32 _therm_device_pmudata_instget(struct gk20a *g,
			struct nv_pmu_boardobjgrp *pmuboardobjgrp,
			struct nv_pmu_boardobj **ppboardobjpmudata,
			u8 idx)
{
	struct nv_pmu_therm_therm_device_boardobj_grp_set *pgrp_set =
		(struct nv_pmu_therm_therm_device_boardobj_grp_set *)
		pmuboardobjgrp;

	gk20a_dbg_info("");

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

	*ppboardobjpmudata = (struct nv_pmu_boardobj *)
		&pgrp_set->objects[idx].data;

	gk20a_dbg_info(" Done");

	return 0;
}

static u32 devinit_get_therm_device_table(struct gk20a *g,
				struct therm_devices *pthermdeviceobjs)
{
	u32 status = 0;
	u8 *therm_device_table_ptr = NULL;
	u8 *curr_therm_device_table_ptr = NULL;
	struct boardobj *boardobj;
	struct therm_device_1x_header therm_device_table_header = { 0 };
	struct therm_device_1x_entry *therm_device_table_entry = NULL;
	u32 index;
	u32 obj_index = 0;
	u16 therm_device_size = 0;
	union {
		struct boardobj boardobj;
		struct therm_device therm_device;
	} therm_device_data;

	gk20a_dbg_info("");

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

	memcpy(&therm_device_table_header, therm_device_table_ptr,
		VBIOS_THERM_DEVICE_1X_HEADER_SIZE_04);

	if (therm_device_table_header.version !=
			VBIOS_THERM_DEVICE_VERSION_1X) {
		status = -EINVAL;
		goto done;
	}

	if (therm_device_table_header.header_size <
			VBIOS_THERM_DEVICE_1X_HEADER_SIZE_04) {
		status = -EINVAL;
		goto done;
	}

	curr_therm_device_table_ptr = (therm_device_table_ptr +
		VBIOS_THERM_DEVICE_1X_HEADER_SIZE_04);

	for (index = 0; index < therm_device_table_header.num_table_entries;
		index++) {
		therm_device_table_entry = (struct therm_device_1x_entry *)
			(curr_therm_device_table_ptr +
				(therm_device_table_header.table_entry_size * index));

		if (therm_device_table_entry->class_id !=
				NV_VBIOS_THERM_DEVICE_1X_ENTRY_CLASS_GPU) {
			continue;
		}

		therm_device_size = sizeof(struct therm_device);
		therm_device_data.boardobj.type = CTRL_THERMAL_THERM_DEVICE_CLASS_GPU;

		boardobj = construct_therm_device(g, &therm_device_data,
					therm_device_size, therm_device_data.boardobj.type);

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

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

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

		++obj_index;
	}

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

u32 therm_device_sw_setup(struct gk20a *g)
{
	u32 status;
	struct boardobjgrp *pboardobjgrp = NULL;
	struct therm_devices *pthermdeviceobjs;

	/* Construct the Super Class and override the Interfaces */
	status = boardobjgrpconstruct_e32(&g->therm_pmu.therm_deviceobjs.super);
	if (status) {
		nvgpu_err(g,
			  "error creating boardobjgrp for therm devices, status - 0x%x",
			  status);
		goto done;
	}

	pboardobjgrp = &g->therm_pmu.therm_deviceobjs.super.super;
	pthermdeviceobjs = &(g->therm_pmu.therm_deviceobjs);

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

	status = devinit_get_therm_device_table(g, pthermdeviceobjs);
	if (status)
		goto done;

	BOARDOBJGRP_PMU_CONSTRUCT(pboardobjgrp, THERM, THERM_DEVICE);

	status = BOARDOBJGRP_PMU_CMD_GRP_SET_CONSTRUCT(g, pboardobjgrp,
			therm, THERM, therm_device, THERM_DEVICE);
	if (status) {
		nvgpu_err(g,
			  "error constructing PMU_BOARDOBJ_CMD_GRP_SET interface - 0x%x",
			  status);
		goto done;
	}

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