[S390] Remove error checking from copy_oldmem_page()
[linux/fpc-iii.git] / drivers / gpu / drm / nouveau / nouveau_volt.c
blob86d03e15735d8143b94ac61d665ecee7f422fcc4
1 /*
2 * Copyright 2010 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
22 * Authors: Ben Skeggs
25 #include "drmP.h"
27 #include "nouveau_drv.h"
28 #include "nouveau_pm.h"
30 static const enum dcb_gpio_tag vidtag[] = { 0x04, 0x05, 0x06, 0x1a, 0x73 };
31 static int nr_vidtag = sizeof(vidtag) / sizeof(vidtag[0]);
33 int
34 nouveau_voltage_gpio_get(struct drm_device *dev)
36 struct drm_nouveau_private *dev_priv = dev->dev_private;
37 struct nouveau_gpio_engine *gpio = &dev_priv->engine.gpio;
38 struct nouveau_pm_voltage *volt = &dev_priv->engine.pm.voltage;
39 u8 vid = 0;
40 int i;
42 for (i = 0; i < nr_vidtag; i++) {
43 if (!(volt->vid_mask & (1 << i)))
44 continue;
46 vid |= gpio->get(dev, vidtag[i]) << i;
49 return nouveau_volt_lvl_lookup(dev, vid);
52 int
53 nouveau_voltage_gpio_set(struct drm_device *dev, int voltage)
55 struct drm_nouveau_private *dev_priv = dev->dev_private;
56 struct nouveau_gpio_engine *gpio = &dev_priv->engine.gpio;
57 struct nouveau_pm_voltage *volt = &dev_priv->engine.pm.voltage;
58 int vid, i;
60 vid = nouveau_volt_vid_lookup(dev, voltage);
61 if (vid < 0)
62 return vid;
64 for (i = 0; i < nr_vidtag; i++) {
65 if (!(volt->vid_mask & (1 << i)))
66 continue;
68 gpio->set(dev, vidtag[i], !!(vid & (1 << i)));
71 return 0;
74 int
75 nouveau_volt_vid_lookup(struct drm_device *dev, int voltage)
77 struct drm_nouveau_private *dev_priv = dev->dev_private;
78 struct nouveau_pm_voltage *volt = &dev_priv->engine.pm.voltage;
79 int i;
81 for (i = 0; i < volt->nr_level; i++) {
82 if (volt->level[i].voltage == voltage)
83 return volt->level[i].vid;
86 return -ENOENT;
89 int
90 nouveau_volt_lvl_lookup(struct drm_device *dev, int vid)
92 struct drm_nouveau_private *dev_priv = dev->dev_private;
93 struct nouveau_pm_voltage *volt = &dev_priv->engine.pm.voltage;
94 int i;
96 for (i = 0; i < volt->nr_level; i++) {
97 if (volt->level[i].vid == vid)
98 return volt->level[i].voltage;
101 return -ENOENT;
104 void
105 nouveau_volt_init(struct drm_device *dev)
107 struct drm_nouveau_private *dev_priv = dev->dev_private;
108 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
109 struct nouveau_pm_voltage *voltage = &pm->voltage;
110 struct nvbios *bios = &dev_priv->vbios;
111 struct bit_entry P;
112 u8 *volt = NULL, *entry;
113 int i, headerlen, recordlen, entries, vidmask, vidshift;
115 if (bios->type == NVBIOS_BIT) {
116 if (bit_table(dev, 'P', &P))
117 return;
119 if (P.version == 1)
120 volt = ROMPTR(bios, P.data[16]);
121 else
122 if (P.version == 2)
123 volt = ROMPTR(bios, P.data[12]);
124 else {
125 NV_WARN(dev, "unknown volt for BIT P %d\n", P.version);
127 } else {
128 if (bios->data[bios->offset + 6] < 0x27) {
129 NV_DEBUG(dev, "BMP version too old for voltage\n");
130 return;
133 volt = ROMPTR(bios, bios->data[bios->offset + 0x98]);
136 if (!volt) {
137 NV_DEBUG(dev, "voltage table pointer invalid\n");
138 return;
141 switch (volt[0]) {
142 case 0x10:
143 case 0x11:
144 case 0x12:
145 headerlen = 5;
146 recordlen = volt[1];
147 entries = volt[2];
148 vidshift = 0;
149 vidmask = volt[4];
150 break;
151 case 0x20:
152 headerlen = volt[1];
153 recordlen = volt[3];
154 entries = volt[2];
155 vidshift = 0; /* could be vidshift like 0x30? */
156 vidmask = volt[5];
157 break;
158 case 0x30:
159 headerlen = volt[1];
160 recordlen = volt[2];
161 entries = volt[3];
162 vidmask = volt[4];
163 /* no longer certain what volt[5] is, if it's related to
164 * the vid shift then it's definitely not a function of
165 * how many bits are set.
167 * after looking at a number of nva3+ vbios images, they
168 * all seem likely to have a static shift of 2.. lets
169 * go with that for now until proven otherwise.
171 vidshift = 2;
172 break;
173 case 0x40:
174 headerlen = volt[1];
175 recordlen = volt[2];
176 entries = volt[3]; /* not a clue what the entries are for.. */
177 vidmask = volt[11]; /* guess.. */
178 vidshift = 0;
179 break;
180 default:
181 NV_WARN(dev, "voltage table 0x%02x unknown\n", volt[0]);
182 return;
185 /* validate vid mask */
186 voltage->vid_mask = vidmask;
187 if (!voltage->vid_mask)
188 return;
190 i = 0;
191 while (vidmask) {
192 if (i > nr_vidtag) {
193 NV_DEBUG(dev, "vid bit %d unknown\n", i);
194 return;
197 if (!nouveau_bios_gpio_entry(dev, vidtag[i])) {
198 NV_DEBUG(dev, "vid bit %d has no gpio tag\n", i);
199 return;
202 vidmask >>= 1;
203 i++;
206 /* parse vbios entries into common format */
207 voltage->version = volt[0];
208 if (voltage->version < 0x40) {
209 voltage->nr_level = entries;
210 voltage->level =
211 kcalloc(entries, sizeof(*voltage->level), GFP_KERNEL);
212 if (!voltage->level)
213 return;
215 entry = volt + headerlen;
216 for (i = 0; i < entries; i++, entry += recordlen) {
217 voltage->level[i].voltage = entry[0] * 10000;
218 voltage->level[i].vid = entry[1] >> vidshift;
220 } else {
221 u32 volt_uv = ROM32(volt[4]);
222 s16 step_uv = ROM16(volt[8]);
223 u8 vid;
225 voltage->nr_level = voltage->vid_mask + 1;
226 voltage->level = kcalloc(voltage->nr_level,
227 sizeof(*voltage->level), GFP_KERNEL);
228 if (!voltage->level)
229 return;
231 for (vid = 0; vid <= voltage->vid_mask; vid++) {
232 voltage->level[vid].voltage = volt_uv;
233 voltage->level[vid].vid = vid;
234 volt_uv += step_uv;
238 voltage->supported = true;
241 void
242 nouveau_volt_fini(struct drm_device *dev)
244 struct drm_nouveau_private *dev_priv = dev->dev_private;
245 struct nouveau_pm_voltage *volt = &dev_priv->engine.pm.voltage;
247 kfree(volt->level);