1 /*
2  * Copyright 2010 Red Hat Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Ben Skeggs
23  */
24 
25 #include "drmP.h"
26 
27 #include "nouveau_drv.h"
28 #include "nouveau_pm.h"
29 #include "nouveau_gpio.h"
30 
31 static const enum dcb_gpio_tag vidtag[] = { 0x04, 0x05, 0x06, 0x1a, 0x73 };
32 static int nr_vidtag = sizeof(vidtag) / sizeof(vidtag[0]);
33 
34 int
nouveau_voltage_gpio_get(struct drm_device * dev)35 nouveau_voltage_gpio_get(struct drm_device *dev)
36 {
37 	struct drm_nouveau_private *dev_priv = dev->dev_private;
38 	struct nouveau_pm_voltage *volt = &dev_priv->engine.pm.voltage;
39 	u8 vid = 0;
40 	int i;
41 
42 	for (i = 0; i < nr_vidtag; i++) {
43 		if (!(volt->vid_mask & (1 << i)))
44 			continue;
45 
46 		vid |= nouveau_gpio_func_get(dev, vidtag[i]) << i;
47 	}
48 
49 	return nouveau_volt_lvl_lookup(dev, vid);
50 }
51 
52 int
nouveau_voltage_gpio_set(struct drm_device * dev,int voltage)53 nouveau_voltage_gpio_set(struct drm_device *dev, int voltage)
54 {
55 	struct drm_nouveau_private *dev_priv = dev->dev_private;
56 	struct nouveau_pm_voltage *volt = &dev_priv->engine.pm.voltage;
57 	int vid, i;
58 
59 	vid = nouveau_volt_vid_lookup(dev, voltage);
60 	if (vid < 0)
61 		return vid;
62 
63 	for (i = 0; i < nr_vidtag; i++) {
64 		if (!(volt->vid_mask & (1 << i)))
65 			continue;
66 
67 		nouveau_gpio_func_set(dev, vidtag[i], !!(vid & (1 << i)));
68 	}
69 
70 	return 0;
71 }
72 
73 int
nouveau_volt_vid_lookup(struct drm_device * dev,int voltage)74 nouveau_volt_vid_lookup(struct drm_device *dev, int voltage)
75 {
76 	struct drm_nouveau_private *dev_priv = dev->dev_private;
77 	struct nouveau_pm_voltage *volt = &dev_priv->engine.pm.voltage;
78 	int i;
79 
80 	for (i = 0; i < volt->nr_level; i++) {
81 		if (volt->level[i].voltage == voltage)
82 			return volt->level[i].vid;
83 	}
84 
85 	return -ENOENT;
86 }
87 
88 int
nouveau_volt_lvl_lookup(struct drm_device * dev,int vid)89 nouveau_volt_lvl_lookup(struct drm_device *dev, int vid)
90 {
91 	struct drm_nouveau_private *dev_priv = dev->dev_private;
92 	struct nouveau_pm_voltage *volt = &dev_priv->engine.pm.voltage;
93 	int i;
94 
95 	for (i = 0; i < volt->nr_level; i++) {
96 		if (volt->level[i].vid == vid)
97 			return volt->level[i].voltage;
98 	}
99 
100 	return -ENOENT;
101 }
102 
103 void
nouveau_volt_init(struct drm_device * dev)104 nouveau_volt_init(struct drm_device *dev)
105 {
106 	struct drm_nouveau_private *dev_priv = dev->dev_private;
107 	struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
108 	struct nouveau_pm_voltage *voltage = &pm->voltage;
109 	struct nvbios *bios = &dev_priv->vbios;
110 	struct bit_entry P;
111 	u8 *volt = NULL, *entry;
112 	int i, headerlen, recordlen, entries, vidmask, vidshift;
113 
114 	if (bios->type == NVBIOS_BIT) {
115 		if (bit_table(dev, 'P', &P))
116 			return;
117 
118 		if (P.version == 1)
119 			volt = ROMPTR(dev, P.data[16]);
120 		else
121 		if (P.version == 2)
122 			volt = ROMPTR(dev, P.data[12]);
123 		else {
124 			NV_WARN(dev, "unknown volt for BIT P %d\n", P.version);
125 		}
126 	} else {
127 		if (bios->data[bios->offset + 6] < 0x27) {
128 			NV_DEBUG(dev, "BMP version too old for voltage\n");
129 			return;
130 		}
131 
132 		volt = ROMPTR(dev, bios->data[bios->offset + 0x98]);
133 	}
134 
135 	if (!volt) {
136 		NV_DEBUG(dev, "voltage table pointer invalid\n");
137 		return;
138 	}
139 
140 	switch (volt[0]) {
141 	case 0x10:
142 	case 0x11:
143 	case 0x12:
144 		headerlen = 5;
145 		recordlen = volt[1];
146 		entries   = volt[2];
147 		vidshift  = 0;
148 		vidmask   = volt[4];
149 		break;
150 	case 0x20:
151 		headerlen = volt[1];
152 		recordlen = volt[3];
153 		entries   = volt[2];
154 		vidshift  = 0; /* could be vidshift like 0x30? */
155 		vidmask   = volt[5];
156 		break;
157 	case 0x30:
158 		headerlen = volt[1];
159 		recordlen = volt[2];
160 		entries   = volt[3];
161 		vidmask   = volt[4];
162 		/* no longer certain what volt[5] is, if it's related to
163 		 * the vid shift then it's definitely not a function of
164 		 * how many bits are set.
165 		 *
166 		 * after looking at a number of nva3+ vbios images, they
167 		 * all seem likely to have a static shift of 2.. lets
168 		 * go with that for now until proven otherwise.
169 		 */
170 		vidshift  = 2;
171 		break;
172 	case 0x40:
173 		headerlen = volt[1];
174 		recordlen = volt[2];
175 		entries   = volt[3]; /* not a clue what the entries are for.. */
176 		vidmask   = volt[11]; /* guess.. */
177 		vidshift  = 0;
178 		break;
179 	default:
180 		NV_WARN(dev, "voltage table 0x%02x unknown\n", volt[0]);
181 		return;
182 	}
183 
184 	/* validate vid mask */
185 	voltage->vid_mask = vidmask;
186 	if (!voltage->vid_mask)
187 		return;
188 
189 	i = 0;
190 	while (vidmask) {
191 		if (i > nr_vidtag) {
192 			NV_DEBUG(dev, "vid bit %d unknown\n", i);
193 			return;
194 		}
195 
196 		if (!nouveau_gpio_func_valid(dev, vidtag[i])) {
197 			NV_DEBUG(dev, "vid bit %d has no gpio tag\n", i);
198 			return;
199 		}
200 
201 		vidmask >>= 1;
202 		i++;
203 	}
204 
205 	/* parse vbios entries into common format */
206 	voltage->version = volt[0];
207 	if (voltage->version < 0x40) {
208 		voltage->nr_level = entries;
209 		voltage->level =
210 			kcalloc(entries, sizeof(*voltage->level), GFP_KERNEL);
211 		if (!voltage->level)
212 			return;
213 
214 		entry = volt + headerlen;
215 		for (i = 0; i < entries; i++, entry += recordlen) {
216 			voltage->level[i].voltage = entry[0] * 10000;
217 			voltage->level[i].vid     = entry[1] >> vidshift;
218 		}
219 	} else {
220 		u32 volt_uv = ROM32(volt[4]);
221 		s16 step_uv = ROM16(volt[8]);
222 		u8 vid;
223 
224 		voltage->nr_level = voltage->vid_mask + 1;
225 		voltage->level = kcalloc(voltage->nr_level,
226 					 sizeof(*voltage->level), GFP_KERNEL);
227 		if (!voltage->level)
228 			return;
229 
230 		for (vid = 0; vid <= voltage->vid_mask; vid++) {
231 			voltage->level[vid].voltage = volt_uv;
232 			voltage->level[vid].vid = vid;
233 			volt_uv += step_uv;
234 		}
235 	}
236 
237 	voltage->supported = true;
238 }
239 
240 void
nouveau_volt_fini(struct drm_device * dev)241 nouveau_volt_fini(struct drm_device *dev)
242 {
243 	struct drm_nouveau_private *dev_priv = dev->dev_private;
244 	struct nouveau_pm_voltage *volt = &dev_priv->engine.pm.voltage;
245 
246 	kfree(volt->level);
247 }
248