base.c 6.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright 2013 Red Hat 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.
 *
 * Authors: Ben Skeggs
 */
24 25
#include "priv.h"

26 27 28 29
#include <subdev/bios.h>
#include <subdev/bios/vmap.h>
#include <subdev/bios/volt.h>

30
int
31
nvkm_volt_get(struct nvkm_volt *volt)
32
{
33 34 35 36 37 38
	int ret, i;

	if (volt->func->volt_get)
		return volt->func->volt_get(volt);

	ret = volt->func->vid_get(volt);
39 40 41 42
	if (ret >= 0) {
		for (i = 0; i < volt->vid_nr; i++) {
			if (volt->vid[i].vid == ret)
				return volt->vid[i].uv;
43
		}
44
		ret = -EINVAL;
45
	}
46
	return ret;
47 48 49
}

static int
50
nvkm_volt_set(struct nvkm_volt *volt, u32 uv)
51
{
52
	struct nvkm_subdev *subdev = &volt->subdev;
53
	int i, ret = -EINVAL;
54 55 56 57

	if (volt->func->volt_set)
		return volt->func->volt_set(volt, uv);

58 59 60 61 62
	for (i = 0; i < volt->vid_nr; i++) {
		if (volt->vid[i].uv == uv) {
			ret = volt->func->vid_set(volt, volt->vid[i].vid);
			nvkm_debug(subdev, "set %duv: %d\n", uv, ret);
			break;
63 64
		}
	}
65
	return ret;
66 67
}

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
int
nvkm_volt_map_min(struct nvkm_volt *volt, u8 id)
{
	struct nvkm_bios *bios = volt->subdev.device->bios;
	struct nvbios_vmap_entry info;
	u8  ver, len;
	u16 vmap;

	vmap = nvbios_vmap_entry_parse(bios, id, &ver, &len, &info);
	if (vmap) {
		if (info.link != 0xff) {
			int ret = nvkm_volt_map_min(volt, info.link);
			if (ret < 0)
				return ret;
			info.min += ret;
		}
		return info.min;
	}

	return id ? id * 10000 : -ENODEV;
}

90
static int
91
nvkm_volt_map(struct nvkm_volt *volt, u8 id)
92
{
93
	struct nvkm_bios *bios = volt->subdev.device->bios;
94 95 96 97 98 99 100
	struct nvbios_vmap_entry info;
	u8  ver, len;
	u16 vmap;

	vmap = nvbios_vmap_entry_parse(bios, id, &ver, &len, &info);
	if (vmap) {
		if (info.link != 0xff) {
101
			int ret = nvkm_volt_map(volt, info.link);
102 103 104 105 106 107 108 109 110 111
			if (ret < 0)
				return ret;
			info.min += ret;
		}
		return info.min;
	}

	return id ? id * 10000 : -ENODEV;
}

112
int
113
nvkm_volt_set_id(struct nvkm_volt *volt, u8 id, u8 min_id, int condition)
114
{
115 116 117 118 119 120
	int ret;

	if (volt->func->set_id)
		return volt->func->set_id(volt, id, condition);

	ret = nvkm_volt_map(volt, id);
121
	if (ret >= 0) {
122
		int prev = nvkm_volt_get(volt);
123 124 125
		if (!condition || prev < 0 ||
		    (condition < 0 && ret < prev) ||
		    (condition > 0 && ret > prev)) {
126 127 128
			int min = nvkm_volt_map(volt, min_id);
			if (min >= 0)
				ret = max(min, ret);
129
			ret = nvkm_volt_set(volt, ret);
130 131 132 133 134 135 136
		} else {
			ret = 0;
		}
	}
	return ret;
}

137 138
static void
nvkm_volt_parse_bios(struct nvkm_bios *bios, struct nvkm_volt *volt)
139
{
140
	struct nvkm_subdev *subdev = &bios->subdev;
141 142 143 144 145 146 147
	struct nvbios_volt_entry ivid;
	struct nvbios_volt info;
	u8  ver, hdr, cnt, len;
	u16 data;
	int i;

	data = nvbios_volt_parse(bios, &ver, &hdr, &cnt, &len, &info);
148 149
	if (data && info.vidmask && info.base && info.step && info.ranged) {
		nvkm_debug(subdev, "found ranged based VIDs\n");
150 151
		volt->min_uv = info.min;
		volt->max_uv = info.max;
152 153 154 155 156 157 158 159 160 161
		for (i = 0; i < info.vidmask + 1; i++) {
			if (info.base >= info.min &&
				info.base <= info.max) {
				volt->vid[volt->vid_nr].uv = info.base;
				volt->vid[volt->vid_nr].vid = i;
				volt->vid_nr++;
			}
			info.base += info.step;
		}
		volt->vid_mask = info.vidmask;
162 163
	} else if (data && info.vidmask && !info.ranged) {
		nvkm_debug(subdev, "found entry based VIDs\n");
164 165
		volt->min_uv = 0xffffffff;
		volt->max_uv = 0;
166 167
		for (i = 0; i < cnt; i++) {
			data = nvbios_volt_entry_parse(bios, i, &ver, &hdr,
168
						       &ivid);
169 170 171 172
			if (data) {
				volt->vid[volt->vid_nr].uv = ivid.voltage;
				volt->vid[volt->vid_nr].vid = ivid.vid;
				volt->vid_nr++;
173 174
				volt->min_uv = min(volt->min_uv, ivid.voltage);
				volt->max_uv = max(volt->max_uv, ivid.voltage);
175 176 177
			}
		}
		volt->vid_mask = info.vidmask;
178 179 180
	} else if (data && info.type == NVBIOS_VOLT_PWM) {
		volt->min_uv = info.base;
		volt->max_uv = info.base + info.pwm_range;
181 182 183
	}
}

184 185
static int
nvkm_volt_init(struct nvkm_subdev *subdev)
186
{
187 188
	struct nvkm_volt *volt = nvkm_volt(subdev);
	int ret = nvkm_volt_get(volt);
189 190
	if (ret < 0) {
		if (ret != -ENODEV)
191
			nvkm_debug(subdev, "current voltage unknown\n");
192 193
		return 0;
	}
194
	nvkm_debug(subdev, "current voltage: %duv\n", ret);
195 196 197
	return 0;
}

198 199
static void *
nvkm_volt_dtor(struct nvkm_subdev *subdev)
200
{
201
	return nvkm_volt(subdev);
202 203
}

204 205 206 207 208 209 210 211 212
static const struct nvkm_subdev_func
nvkm_volt = {
	.dtor = nvkm_volt_dtor,
	.init = nvkm_volt_init,
};

void
nvkm_volt_ctor(const struct nvkm_volt_func *func, struct nvkm_device *device,
	       int index, struct nvkm_volt *volt)
213
{
214
	struct nvkm_bios *bios = device->bios;
215
	int i;
216

217
	nvkm_subdev_ctor(&nvkm_volt, device, index, &volt->subdev);
218
	volt->func = func;
219

220
	/* Assuming the non-bios device should build the voltage table later */
221
	if (bios) {
222 223 224
		u8 ver, hdr, cnt, len;
		struct nvbios_vmap vmap;

225
		nvkm_volt_parse_bios(bios, volt);
226 227
		nvkm_debug(&volt->subdev, "min: %iuv max: %iuv\n",
			   volt->min_uv, volt->max_uv);
228 229 230 231 232 233 234 235 236 237

		if (nvbios_vmap_parse(bios, &ver, &hdr, &cnt, &len, &vmap)) {
			volt->max0_id = vmap.max0;
			volt->max1_id = vmap.max1;
			volt->max2_id = vmap.max2;
		} else {
			volt->max0_id = 0xff;
			volt->max1_id = 0xff;
			volt->max2_id = 0xff;
		}
238
	}
239 240 241

	if (volt->vid_nr) {
		for (i = 0; i < volt->vid_nr; i++) {
242 243
			nvkm_debug(&volt->subdev, "VID %02x: %duv\n",
				   volt->vid[i].vid, volt->vid[i].uv);
244 245
		}
	}
246
}
247

248 249 250 251 252 253 254 255
int
nvkm_volt_new_(const struct nvkm_volt_func *func, struct nvkm_device *device,
	       int index, struct nvkm_volt **pvolt)
{
	if (!(*pvolt = kzalloc(sizeof(**pvolt), GFP_KERNEL)))
		return -ENOMEM;
	nvkm_volt_ctor(func, device, index, *pvolt);
	return 0;
256
}