perf.c 5.9 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 24 25 26
/*
 * Copyright 2012 Nouveau Community
 *
 * 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: Martin Peres
 */
#include <subdev/bios.h>
#include <subdev/bios/bit.h>
#include <subdev/bios/perf.h>
27
#include <subdev/pci.h>
28

29
u16
30
nvbios_perf_table(struct nvkm_bios *bios, u8 *ver, u8 *hdr,
31
		  u8 *cnt, u8 *len, u8 *snr, u8 *ssz)
32 33 34 35 36 37
{
	struct bit_entry bit_P;
	u16 perf = 0x0000;

	if (!bit_entry(bios, 'P', &bit_P)) {
		if (bit_P.version <= 2) {
38
			perf = nvbios_rd16(bios, bit_P.offset + 0);
39
			if (perf) {
40 41
				*ver = nvbios_rd08(bios, perf + 0);
				*hdr = nvbios_rd08(bios, perf + 1);
42
				if (*ver >= 0x40 && *ver < 0x41) {
43 44 45 46
					*cnt = nvbios_rd08(bios, perf + 5);
					*len = nvbios_rd08(bios, perf + 2);
					*snr = nvbios_rd08(bios, perf + 4);
					*ssz = nvbios_rd08(bios, perf + 3);
47 48 49
					return perf;
				} else
				if (*ver >= 0x20 && *ver < 0x40) {
50 51 52 53
					*cnt = nvbios_rd08(bios, perf + 2);
					*len = nvbios_rd08(bios, perf + 3);
					*snr = nvbios_rd08(bios, perf + 4);
					*ssz = nvbios_rd08(bios, perf + 5);
54 55
					return perf;
				}
56
			}
57
		}
58 59 60
	}

	if (bios->bmp_offset) {
61 62
		if (nvbios_rd08(bios, bios->bmp_offset + 6) >= 0x25) {
			perf = nvbios_rd16(bios, bios->bmp_offset + 0x94);
63
			if (perf) {
64 65 66 67
				*hdr = nvbios_rd08(bios, perf + 0);
				*ver = nvbios_rd08(bios, perf + 1);
				*cnt = nvbios_rd08(bios, perf + 2);
				*len = nvbios_rd08(bios, perf + 3);
68 69 70
				*snr = 0;
				*ssz = 0;
				return perf;
71 72 73 74
			}
		}
	}

75 76 77 78
	return 0x0000;
}

u16
79
nvbios_perf_entry(struct nvkm_bios *bios, int idx,
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
		  u8 *ver, u8 *hdr, u8 *cnt, u8 *len)
{
	u8  snr, ssz;
	u16 perf = nvbios_perf_table(bios, ver, hdr, cnt, len, &snr, &ssz);
	if (perf && idx < *cnt) {
		perf = perf + *hdr + (idx * (*len + (snr * ssz)));
		*hdr = *len;
		*cnt = snr;
		*len = ssz;
		return perf;
	}
	return 0x0000;
}

u16
95 96
nvbios_perfEp(struct nvkm_bios *bios, int idx,
	      u8 *ver, u8 *hdr, u8 *cnt, u8 *len, struct nvbios_perfE *info)
97 98 99
{
	u16 perf = nvbios_perf_entry(bios, idx, ver, hdr, cnt, len);
	memset(info, 0x00, sizeof(*info));
100
	info->pstate = nvbios_rd08(bios, perf + 0x00);
101 102 103 104
	switch (!!perf * *ver) {
	case 0x12:
	case 0x13:
	case 0x14:
105 106 107
		info->core     = nvbios_rd32(bios, perf + 0x01) * 10;
		info->memory   = nvbios_rd32(bios, perf + 0x05) * 20;
		info->fanspeed = nvbios_rd08(bios, perf + 0x37);
108
		if (*hdr > 0x38)
109
			info->voltage = nvbios_rd08(bios, perf + 0x38);
110 111 112 113
		break;
	case 0x21:
	case 0x23:
	case 0x24:
114 115 116
		info->fanspeed = nvbios_rd08(bios, perf + 0x04);
		info->voltage  = nvbios_rd08(bios, perf + 0x05);
		info->shader   = nvbios_rd16(bios, perf + 0x06) * 1000;
117
		info->core     = info->shader + (signed char)
118
				 nvbios_rd08(bios, perf + 0x08) * 1000;
119
		switch (bios->subdev.device->chipset) {
120 121
		case 0x49:
		case 0x4b:
122
			info->memory = nvbios_rd16(bios, perf + 0x0b) * 1000;
123 124
			break;
		default:
125
			info->memory = nvbios_rd16(bios, perf + 0x0b) * 2000;
126 127 128 129
			break;
		}
		break;
	case 0x25:
130 131 132 133 134
		info->fanspeed = nvbios_rd08(bios, perf + 0x04);
		info->voltage  = nvbios_rd08(bios, perf + 0x05);
		info->core     = nvbios_rd16(bios, perf + 0x06) * 1000;
		info->shader   = nvbios_rd16(bios, perf + 0x0a) * 1000;
		info->memory   = nvbios_rd16(bios, perf + 0x0c) * 1000;
135 136
		break;
	case 0x30:
137
		info->script   = nvbios_rd16(bios, perf + 0x02);
138
	case 0x35:
139 140 141 142 143 144 145
		info->fanspeed = nvbios_rd08(bios, perf + 0x06);
		info->voltage  = nvbios_rd08(bios, perf + 0x07);
		info->core     = nvbios_rd16(bios, perf + 0x08) * 1000;
		info->shader   = nvbios_rd16(bios, perf + 0x0a) * 1000;
		info->memory   = nvbios_rd16(bios, perf + 0x0c) * 1000;
		info->vdec     = nvbios_rd16(bios, perf + 0x10) * 1000;
		info->disp     = nvbios_rd16(bios, perf + 0x14) * 1000;
146 147
		break;
	case 0x40:
148
		info->voltage  = nvbios_rd08(bios, perf + 0x02);
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
		switch (nvbios_rd08(bios, perf + 0xb) & 0x3) {
		case 0:
			info->pcie_speed = NVKM_PCIE_SPEED_5_0;
			break;
		case 3:
		case 1:
			info->pcie_speed = NVKM_PCIE_SPEED_2_5;
			break;
		case 2:
			info->pcie_speed = NVKM_PCIE_SPEED_8_0;
			break;
		default:
			break;
		}
		info->pcie_width = 0xff;
164 165 166 167
		break;
	default:
		return 0x0000;
	}
168 169 170
	return perf;
}

171
u32
172
nvbios_perfSe(struct nvkm_bios *bios, u32 perfE, int idx,
173 174 175 176 177 178 179 180 181 182 183
	      u8 *ver, u8 *hdr, u8 cnt, u8 len)
{
	u32 data = 0x00000000;
	if (idx < cnt) {
		data = perfE + *hdr + (idx * len);
		*hdr = len;
	}
	return data;
}

u32
184
nvbios_perfSp(struct nvkm_bios *bios, u32 perfE, int idx,
185 186 187 188 189 190 191
	      u8 *ver, u8 *hdr, u8 cnt, u8 len,
	      struct nvbios_perfS *info)
{
	u32 data = nvbios_perfSe(bios, perfE, idx, ver, hdr, cnt, len);
	memset(info, 0x00, sizeof(*info));
	switch (!!data * *ver) {
	case 0x40:
192
		info->v40.freq = (nvbios_rd16(bios, data + 0x00) & 0x3fff) * 1000;
193 194 195 196 197 198 199
		break;
	default:
		break;
	}
	return data;
}

200
int
201
nvbios_perf_fan_parse(struct nvkm_bios *bios,
202 203
		      struct nvbios_perf_fan *fan)
{
204 205
	u8  ver, hdr, cnt, len, snr, ssz;
	u16 perf = nvbios_perf_table(bios, &ver, &hdr, &cnt, &len, &snr, &ssz);
206 207 208 209
	if (!perf)
		return -ENODEV;

	if (ver >= 0x20 && ver < 0x40 && hdr > 6)
210
		fan->pwm_divisor = nvbios_rd16(bios, perf + 6);
211 212 213 214 215
	else
		fan->pwm_divisor = 0;

	return 0;
}