base.c 4.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 27 28 29
/*
 * 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
 */
#include <subdev/volt.h>
#include <subdev/bios.h>
#include <subdev/bios/vmap.h>
#include <subdev/bios/volt.h>

static int
30
nvkm_volt_get(struct nvkm_volt *volt)
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
{
	if (volt->vid_get) {
		int ret = volt->vid_get(volt), i;
		if (ret >= 0) {
			for (i = 0; i < volt->vid_nr; i++) {
				if (volt->vid[i].vid == ret)
					return volt->vid[i].uv;
			}
			ret = -EINVAL;
		}
		return ret;
	}
	return -ENODEV;
}

static int
47
nvkm_volt_set(struct nvkm_volt *volt, u32 uv)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
{
	if (volt->vid_set) {
		int i, ret = -EINVAL;
		for (i = 0; i < volt->vid_nr; i++) {
			if (volt->vid[i].uv == uv) {
				ret = volt->vid_set(volt, volt->vid[i].vid);
				nv_debug(volt, "set %duv: %d\n", uv, ret);
				break;
			}
		}
		return ret;
	}
	return -ENODEV;
}

static int
64
nvkm_volt_map(struct nvkm_volt *volt, u8 id)
65
{
66
	struct nvkm_bios *bios = nvkm_bios(volt);
67 68 69 70 71 72 73
	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) {
74
			int ret = nvkm_volt_map(volt, info.link);
75 76 77 78 79 80 81 82 83 84 85
			if (ret < 0)
				return ret;
			info.min += ret;
		}
		return info.min;
	}

	return id ? id * 10000 : -ENODEV;
}

static int
86
nvkm_volt_set_id(struct nvkm_volt *volt, u8 id, int condition)
87
{
88
	int ret = nvkm_volt_map(volt, id);
89
	if (ret >= 0) {
90
		int prev = nvkm_volt_get(volt);
91 92 93
		if (!condition || prev < 0 ||
		    (condition < 0 && ret < prev) ||
		    (condition > 0 && ret > prev)) {
94
			ret = nvkm_volt_set(volt, ret);
95 96 97 98 99 100 101
		} else {
			ret = 0;
		}
	}
	return ret;
}

102 103
static void
nvkm_volt_parse_bios(struct nvkm_bios *bios, struct nvkm_volt *volt)
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
{
	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);
	if (data && info.vidmask && info.base && info.step) {
		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;
	} else if (data && info.vidmask) {
		for (i = 0; i < cnt; i++) {
			data = nvbios_volt_entry_parse(bios, i, &ver, &hdr,
126
						       &ivid);
127 128 129 130 131 132 133 134 135 136
			if (data) {
				volt->vid[volt->vid_nr].uv = ivid.voltage;
				volt->vid[volt->vid_nr].vid = ivid.vid;
				volt->vid_nr++;
			}
		}
		volt->vid_mask = info.vidmask;
	}
}

137
int
138
_nvkm_volt_init(struct nvkm_object *object)
139
{
140
	struct nvkm_volt *volt = (void *)object;
141 142
	int ret;

B
Ben Skeggs 已提交
143
	ret = nvkm_subdev_init(&volt->subdev);
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
	if (ret)
		return ret;

	ret = volt->get(volt);
	if (ret < 0) {
		if (ret != -ENODEV)
			nv_debug(volt, "current voltage unknown\n");
		return 0;
	}

	nv_info(volt, "GPU voltage: %duv\n", ret);
	return 0;
}

void
159
_nvkm_volt_dtor(struct nvkm_object *object)
160
{
161
	struct nvkm_volt *volt = (void *)object;
B
Ben Skeggs 已提交
162
	nvkm_subdev_destroy(&volt->subdev);
163 164 165
}

int
166 167
nvkm_volt_create_(struct nvkm_object *parent, struct nvkm_object *engine,
		  struct nvkm_oclass *oclass, int length, void **pobject)
168
{
169 170
	struct nvkm_bios *bios = nvkm_bios(parent);
	struct nvkm_volt *volt;
171 172
	int ret, i;

173 174
	ret = nvkm_subdev_create_(parent, engine, oclass, 0, "VOLT",
				  "voltage", length, pobject);
175 176 177 178
	volt = *pobject;
	if (ret)
		return ret;

179 180 181
	volt->get = nvkm_volt_get;
	volt->set = nvkm_volt_set;
	volt->set_id = nvkm_volt_set_id;
182

183 184
	/* Assuming the non-bios device should build the voltage table later */
	if (bios)
185
		nvkm_volt_parse_bios(bios, volt);
186 187 188 189 190 191 192 193 194 195

	if (volt->vid_nr) {
		for (i = 0; i < volt->vid_nr; i++) {
			nv_debug(volt, "VID %02x: %duv\n",
				 volt->vid[i].vid, volt->vid[i].uv);
		}

		/*XXX: this is an assumption.. there probably exists boards
		 * out there with i2c-connected voltage controllers too..
		 */
196
		ret = nvkm_voltgpio_init(volt);
197
		if (ret == 0) {
198 199
			volt->vid_get = nvkm_voltgpio_get;
			volt->vid_set = nvkm_voltgpio_set;
200 201 202 203 204
		}
	}

	return ret;
}