gk20a.c 5.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright (c) 2014, 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.
 */
22
#define gk20a_pmu(p) container_of((p), struct gk20a_pmu, base)
23 24
#include "priv.h"

25
#include <subdev/clk.h>
26 27 28 29 30 31
#include <subdev/timer.h>
#include <subdev/volt.h>

#define BUSY_SLOT	0
#define CLK_SLOT	7

32
struct gk20a_pmu_dvfs_data {
33 34 35 36 37 38
	int p_load_target;
	int p_load_max;
	int p_smooth;
	unsigned int avg_load;
};

B
Ben Skeggs 已提交
39
struct gk20a_pmu {
40 41
	struct nvkm_pmu base;
	struct nvkm_alarm alarm;
42
	struct gk20a_pmu_dvfs_data *data;
43 44
};

45
struct gk20a_pmu_dvfs_dev_status {
46 47
	u32 total;
	u32 busy;
48 49 50
};

static int
B
Ben Skeggs 已提交
51
gk20a_pmu_dvfs_target(struct gk20a_pmu *pmu, int *state)
52
{
53
	struct nvkm_clk *clk = pmu->base.subdev.device->clk;
54

55
	return nvkm_clk_astate(clk, *state, 0, false);
56 57
}

58
static void
B
Ben Skeggs 已提交
59
gk20a_pmu_dvfs_get_cur_state(struct gk20a_pmu *pmu, int *state)
60
{
61
	struct nvkm_clk *clk = pmu->base.subdev.device->clk;
62 63 64 65 66

	*state = clk->pstate;
}

static int
B
Ben Skeggs 已提交
67
gk20a_pmu_dvfs_get_target_state(struct gk20a_pmu *pmu,
68
				int *state, int load)
69
{
B
Ben Skeggs 已提交
70
	struct gk20a_pmu_dvfs_data *data = pmu->data;
71
	struct nvkm_clk *clk = pmu->base.subdev.device->clk;
72 73 74 75 76 77 78 79 80 81 82 83 84 85
	int cur_level, level;

	/* For GK20A, the performance level is directly mapped to pstate */
	level = cur_level = clk->pstate;

	if (load > data->p_load_max) {
		level = min(clk->state_nr - 1, level + (clk->state_nr / 3));
	} else {
		level += ((load - data->p_load_target) * 10 /
				data->p_load_target) / 2;
		level = max(0, level);
		level = min(clk->state_nr - 1, level);
	}

86 87
	nvkm_trace(&pmu->base.subdev, "cur level = %d, new level = %d\n",
		   cur_level, level);
88 89 90

	*state = level;

91
	return (level != cur_level);
92 93
}

94
static void
B
Ben Skeggs 已提交
95
gk20a_pmu_dvfs_get_dev_status(struct gk20a_pmu *pmu,
96
			      struct gk20a_pmu_dvfs_dev_status *status)
97
{
98 99 100 101
	struct nvkm_falcon *falcon = pmu->base.falcon;

	status->busy = nvkm_falcon_rd32(falcon, 0x508 + (BUSY_SLOT * 0x10));
	status->total= nvkm_falcon_rd32(falcon, 0x508 + (CLK_SLOT * 0x10));
102 103 104
}

static void
B
Ben Skeggs 已提交
105
gk20a_pmu_dvfs_reset_dev_status(struct gk20a_pmu *pmu)
106
{
107 108 109 110
	struct nvkm_falcon *falcon = pmu->base.falcon;

	nvkm_falcon_wr32(falcon, 0x508 + (BUSY_SLOT * 0x10), 0x80000000);
	nvkm_falcon_wr32(falcon, 0x508 + (CLK_SLOT * 0x10), 0x80000000);
111 112 113
}

static void
114
gk20a_pmu_dvfs_work(struct nvkm_alarm *alarm)
115
{
B
Ben Skeggs 已提交
116 117 118
	struct gk20a_pmu *pmu =
		container_of(alarm, struct gk20a_pmu, alarm);
	struct gk20a_pmu_dvfs_data *data = pmu->data;
119
	struct gk20a_pmu_dvfs_dev_status status;
120 121 122
	struct nvkm_subdev *subdev = &pmu->base.subdev;
	struct nvkm_device *device = subdev->device;
	struct nvkm_clk *clk = device->clk;
123
	struct nvkm_timer *tmr = device->timer;
124
	struct nvkm_volt *volt = device->volt;
125
	u32 utilization = 0;
126
	int state;
127 128

	/*
129
	 * The PMU is initialized before CLK and VOLT, so we have to make sure the
130 131 132 133 134
	 * CLK and VOLT are ready here.
	 */
	if (!clk || !volt)
		goto resched;

135
	gk20a_pmu_dvfs_get_dev_status(pmu, &status);
136 137 138 139 140 141

	if (status.total)
		utilization = div_u64((u64)status.busy * 100, status.total);

	data->avg_load = (data->p_smooth * data->avg_load) + utilization;
	data->avg_load /= data->p_smooth + 1;
142 143
	nvkm_trace(subdev, "utilization = %d %%, avg_load = %d %%\n",
		   utilization, data->avg_load);
144

145
	gk20a_pmu_dvfs_get_cur_state(pmu, &state);
146

B
Ben Skeggs 已提交
147
	if (gk20a_pmu_dvfs_get_target_state(pmu, &state, data->avg_load)) {
148
		nvkm_trace(subdev, "set new state to %d\n", state);
B
Ben Skeggs 已提交
149
		gk20a_pmu_dvfs_target(pmu, &state);
150 151 152
	}

resched:
B
Ben Skeggs 已提交
153
	gk20a_pmu_dvfs_reset_dev_status(pmu);
154
	nvkm_timer_alarm(tmr, 100000000, alarm);
155 156
}

157 158
static void
gk20a_pmu_fini(struct nvkm_pmu *pmu)
159
{
160
	struct gk20a_pmu *gpmu = gk20a_pmu(pmu);
161
	nvkm_timer_alarm(pmu->subdev.device->timer, 0, &gpmu->alarm);
162 163

	nvkm_falcon_put(pmu->falcon, &pmu->subdev);
164 165
}

166
static int
167
gk20a_pmu_init(struct nvkm_pmu *pmu)
168
{
169
	struct gk20a_pmu *gpmu = gk20a_pmu(pmu);
170
	struct nvkm_subdev *subdev = &pmu->subdev;
171
	struct nvkm_device *device = pmu->subdev.device;
172 173 174 175 176 177 178 179
	struct nvkm_falcon *falcon = pmu->falcon;
	int ret;

	ret = nvkm_falcon_get(falcon, subdev);
	if (ret) {
		nvkm_error(subdev, "cannot acquire %s falcon!\n", falcon->name);
		return ret;
	}
180 181

	/* init pwr perf counter */
182 183 184
	nvkm_falcon_wr32(falcon, 0x504 + (BUSY_SLOT * 0x10), 0x00200001);
	nvkm_falcon_wr32(falcon, 0x50c + (BUSY_SLOT * 0x10), 0x00000002);
	nvkm_falcon_wr32(falcon, 0x50c + (CLK_SLOT * 0x10), 0x00000003);
185

186
	nvkm_timer_alarm(device->timer, 2000000000, &gpmu->alarm);
187
	return 0;
188 189
}

190 191
static struct gk20a_pmu_dvfs_data
gk20a_dvfs_data= {
192 193 194 195 196
	.p_load_target = 70,
	.p_load_max = 90,
	.p_smooth = 1,
};

197
static const struct nvkm_pmu_func
198 199 200
gk20a_pmu = {
	.init = gk20a_pmu_init,
	.fini = gk20a_pmu_fini,
201
	.reset = gt215_pmu_reset,
202 203 204 205
};

int
gk20a_pmu_new(struct nvkm_device *device, int index, struct nvkm_pmu **ppmu)
206
{
B
Ben Skeggs 已提交
207
	struct gk20a_pmu *pmu;
208

209 210 211
	if (!(pmu = kzalloc(sizeof(*pmu), GFP_KERNEL)))
		return -ENOMEM;
	*ppmu = &pmu->base;
212

213 214
	nvkm_pmu_ctor(&gk20a_pmu, device, index, &pmu->base);

B
Ben Skeggs 已提交
215 216
	pmu->data = &gk20a_dvfs_data;
	nvkm_alarm_init(&pmu->alarm, gk20a_pmu_dvfs_work);
217

218 219
	return 0;
}