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.
27 #include "nouveau_drm.h"
28 #include "nouveau_reg.h"
29 #include "nouveau_pm.h"
32 nouveau_perf_table(struct drm_device *dev, u8 *ver)
34 struct nouveau_drm *drm = nouveau_drm(dev);
35 struct nvbios *bios = &drm->vbios;
38 if (!bit_table(dev, 'P', &P) && P.version && P.version <= 2) {
39 u8 *perf = ROMPTR(dev, P.data[0]);
46 if (bios->type == NVBIOS_BMP) {
47 if (bios->data[bios->offset + 6] >= 0x25) {
48 u8 *perf = ROMPTR(dev, bios->data[bios->offset + 0x94]);
60 nouveau_perf_entry(struct drm_device *dev, int idx,
61 u8 *ver, u8 *hdr, u8 *cnt, u8 *len)
63 u8 *perf = nouveau_perf_table(dev, ver);
65 if (*ver >= 0x12 && *ver < 0x20 && idx < perf[2]) {
69 return perf + perf[0] + idx * perf[3];
71 if (*ver >= 0x20 && *ver < 0x40 && idx < perf[2]) {
75 return perf + perf[1] + idx * (*hdr + (*cnt * *len));
77 if (*ver >= 0x40 && *ver < 0x41 && idx < perf[5]) {
81 return perf + perf[1] + idx * (*hdr + (*cnt * *len));
88 nouveau_perf_rammap(struct drm_device *dev, u32 freq,
89 u8 *ver, u8 *hdr, u8 *cnt, u8 *len)
91 struct nouveau_drm *drm = nouveau_drm(dev);
95 if (!bit_table(dev, 'P', &P) && P.version == 2) {
96 u8 *rammap = ROMPTR(dev, P.data[4]);
98 u8 *ramcfg = rammap + rammap[1];
106 for (i = 0; i < rammap[5]; i++) {
107 if (freq >= ROM16(ramcfg[0]) &&
108 freq <= ROM16(ramcfg[2]))
111 ramcfg += *hdr + (*cnt * *len);
118 if (nv_device(drm->device)->chipset == 0x49 ||
119 nv_device(drm->device)->chipset == 0x4b)
122 while ((perf = nouveau_perf_entry(dev, i++, ver, hdr, cnt, len))) {
123 if (*ver >= 0x20 && *ver < 0x25) {
124 if (perf[0] != 0xff && freq <= ROM16(perf[11]) * 1000)
127 if (*ver >= 0x25 && *ver < 0x40) {
128 if (perf[0] != 0xff && freq <= ROM16(perf[12]) * 1000)
134 u8 *ramcfg = perf + *hdr;
144 nouveau_perf_ramcfg(struct drm_device *dev, u32 freq, u8 *ver, u8 *len)
146 struct nouveau_device *device = nouveau_dev(dev);
147 struct nouveau_drm *drm = nouveau_drm(dev);
148 struct nvbios *bios = &drm->vbios;
152 strap = (nv_rd32(device, 0x101000) & 0x0000003c) >> 2;
153 if (bios->ram_restrict_tbl_ptr)
154 strap = bios->data[bios->ram_restrict_tbl_ptr + strap];
156 rammap = nouveau_perf_rammap(dev, freq, ver, &hdr, &cnt, len);
157 if (rammap && strap < cnt)
158 return rammap + hdr + (strap * *len);
164 nouveau_perf_timing(struct drm_device *dev, u32 freq, u8 *ver, u8 *len)
166 struct nouveau_drm *drm = nouveau_drm(dev);
167 struct nvbios *bios = &drm->vbios;
169 u8 *perf, *timing = NULL;
172 if (bios->type == NVBIOS_BMP) {
173 while ((perf = nouveau_perf_entry(dev, i++, ver, &hdr, &cnt,
174 len)) && *ver == 0x15) {
175 if (freq <= ROM32(perf[5]) * 20) {
184 if (!bit_table(dev, 'P', &P)) {
186 timing = ROMPTR(dev, P.data[4]);
189 timing = ROMPTR(dev, P.data[8]);
192 if (timing && timing[0] == 0x10) {
193 u8 *ramcfg = nouveau_perf_ramcfg(dev, freq, ver, len);
194 if (ramcfg && ramcfg[1] < timing[2]) {
197 return timing + timing[1] + (ramcfg[1] * timing[3]);
205 legacy_perf_init(struct drm_device *dev)
207 struct nouveau_device *device = nouveau_dev(dev);
208 struct nouveau_drm *drm = nouveau_drm(dev);
209 struct nvbios *bios = &drm->vbios;
210 struct nouveau_pm *pm = nouveau_pm(dev);
211 char *perf, *entry, *bmp = &bios->data[bios->offset];
212 int headerlen, use_straps;
214 if (bmp[5] < 0x5 || bmp[6] < 0x14) {
215 NV_DEBUG(drm, "BMP version too old for perf\n");
219 perf = ROMPTR(dev, bmp[0x73]);
221 NV_DEBUG(drm, "No memclock table pointer found.\n");
233 use_straps = perf[1] & 1;
234 headerlen = (use_straps ? 8 : 2);
237 NV_WARN(drm, "Unknown memclock table version %x.\n", perf[0]);
241 entry = perf + headerlen;
243 entry += (nv_rd32(device, NV_PEXTDEV_BOOT_0) & 0x3c) >> 1;
245 sprintf(pm->perflvl[0].name, "performance_level_0");
246 pm->perflvl[0].memory = ROM16(entry[0]) * 20;
251 nouveau_perf_voltage(struct drm_device *dev, struct nouveau_pm_level *perflvl)
253 struct nouveau_drm *drm = nouveau_drm(dev);
258 id = perflvl->volt_min;
259 perflvl->volt_min = 0;
261 /* boards using voltage table version <0x40 store the voltage
262 * level directly in the perflvl entry as a multiple of 10mV
264 if (drm->pm->voltage.version < 0x40) {
265 perflvl->volt_min = id * 10000;
266 perflvl->volt_max = perflvl->volt_min;
270 /* on newer ones, the perflvl stores an index into yet another
271 * vbios table containing a min/max voltage value for the perflvl
273 if (bit_table(dev, 'P', &P) || P.version != 2 || P.length < 34) {
274 NV_DEBUG(drm, "where's our volt map table ptr? %d %d\n",
275 P.version, P.length);
279 vmap = ROMPTR(dev, P.data[32]);
281 NV_DEBUG(drm, "volt map table pointer invalid\n");
286 vmap += vmap[1] + (vmap[2] * id);
287 perflvl->volt_min = ROM32(vmap[0]);
288 perflvl->volt_max = ROM32(vmap[4]);
293 nouveau_perf_init(struct drm_device *dev)
295 struct nouveau_drm *drm = nouveau_drm(dev);
296 struct nouveau_pm *pm = nouveau_pm(dev);
297 struct nvbios *bios = &drm->vbios;
298 u8 *perf, ver, hdr, cnt, len;
299 int ret, vid, i = -1;
301 if (bios->type == NVBIOS_BMP && bios->data[bios->offset + 6] < 0x25) {
302 legacy_perf_init(dev);
306 perf = nouveau_perf_table(dev, &ver);
308 while ((perf = nouveau_perf_entry(dev, ++i, &ver, &hdr, &cnt, &len))) {
309 struct nouveau_pm_level *perflvl = &pm->perflvl[pm->nr_perflvl];
318 perflvl->fanspeed = perf[55];
320 perflvl->volt_min = perf[56];
321 perflvl->core = ROM32(perf[1]) * 10;
322 perflvl->memory = ROM32(perf[5]) * 20;
327 perflvl->fanspeed = perf[4];
328 perflvl->volt_min = perf[5];
329 perflvl->shader = ROM16(perf[6]) * 1000;
330 perflvl->core = perflvl->shader;
331 perflvl->core += (signed char)perf[8] * 1000;
332 if (nv_device(drm->device)->chipset == 0x49 ||
333 nv_device(drm->device)->chipset == 0x4b)
334 perflvl->memory = ROM16(perf[11]) * 1000;
336 perflvl->memory = ROM16(perf[11]) * 2000;
339 perflvl->fanspeed = perf[4];
340 perflvl->volt_min = perf[5];
341 perflvl->core = ROM16(perf[6]) * 1000;
342 perflvl->shader = ROM16(perf[10]) * 1000;
343 perflvl->memory = ROM16(perf[12]) * 1000;
346 perflvl->memscript = ROM16(perf[2]);
348 perflvl->fanspeed = perf[6];
349 perflvl->volt_min = perf[7];
350 perflvl->core = ROM16(perf[8]) * 1000;
351 perflvl->shader = ROM16(perf[10]) * 1000;
352 perflvl->memory = ROM16(perf[12]) * 1000;
353 perflvl->vdec = ROM16(perf[16]) * 1000;
354 perflvl->dom6 = ROM16(perf[20]) * 1000;
357 #define subent(n) ((ROM16(perf[hdr + (n) * len]) & 0xfff) * 1000)
358 perflvl->fanspeed = 0; /*XXX*/
359 perflvl->volt_min = perf[2];
360 if (nv_device(drm->device)->card_type == NV_50) {
361 perflvl->core = subent(0);
362 perflvl->shader = subent(1);
363 perflvl->memory = subent(2);
364 perflvl->vdec = subent(3);
365 perflvl->unka0 = subent(4);
367 perflvl->hub06 = subent(0);
368 perflvl->hub01 = subent(1);
369 perflvl->copy = subent(2);
370 perflvl->shader = subent(3);
371 perflvl->rop = subent(4);
372 perflvl->memory = subent(5);
373 perflvl->vdec = subent(6);
374 perflvl->daemon = subent(10);
375 perflvl->hub07 = subent(11);
376 perflvl->core = perflvl->shader / 2;
381 /* make sure vid is valid */
382 nouveau_perf_voltage(dev, perflvl);
383 if (pm->voltage.supported && perflvl->volt_min) {
384 vid = nouveau_volt_vid_lookup(dev, perflvl->volt_min);
386 NV_DEBUG(drm, "perflvl %d, bad vid\n", i);
391 /* get the corresponding memory timings */
392 ret = nouveau_mem_timing_calc(dev, perflvl->memory,
395 NV_DEBUG(drm, "perflvl %d, bad timing: %d\n", i, ret);
399 snprintf(perflvl->name, sizeof(perflvl->name),
400 "performance_level_%d", i);
403 snprintf(perflvl->profile.name, sizeof(perflvl->profile.name),
405 perflvl->profile.func = &nouveau_pm_static_profile_func;
406 list_add_tail(&perflvl->profile.head, &pm->profiles);
414 nouveau_perf_fini(struct drm_device *dev)