2 * Copyright 2013 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.
25 #include <core/option.h>
27 #include <subdev/clock.h>
28 #include <subdev/therm.h>
29 #include <subdev/volt.h>
30 #include <subdev/fb.h>
32 #include <subdev/bios.h>
33 #include <subdev/bios/boost.h>
34 #include <subdev/bios/cstep.h>
35 #include <subdev/bios/perf.h>
37 /******************************************************************************
39 *****************************************************************************/
41 nouveau_clock_adjust(struct nouveau_clock *clk, bool adjust,
42 u8 pstate, u8 domain, u32 input)
44 struct nouveau_bios *bios = nouveau_bios(clk);
45 struct nvbios_boostE boostE;
46 u8 ver, hdr, cnt, len;
49 data = nvbios_boostEm(bios, pstate, &ver, &hdr, &cnt, &len, &boostE);
51 struct nvbios_boostS boostS;
52 u8 idx = 0, sver, shdr;
55 input = max(boostE.min, input);
56 input = min(boostE.max, input);
60 subd = nvbios_boostSp(bios, idx++, data, &sver, &shdr,
62 if (subd && boostS.domain == domain) {
64 input = input * boostS.percent / 100;
65 input = max(boostS.min, input);
66 input = min(boostS.max, input);
75 /******************************************************************************
77 *****************************************************************************/
79 nouveau_cstate_prog(struct nouveau_clock *clk,
80 struct nouveau_pstate *pstate, int cstatei)
82 struct nouveau_therm *ptherm = nouveau_therm(clk);
83 struct nouveau_volt *volt = nouveau_volt(clk);
84 struct nouveau_cstate *cstate;
87 if (!list_empty(&pstate->list)) {
88 cstate = list_entry(pstate->list.prev, typeof(*cstate), head);
90 cstate = &pstate->base;
93 ret = nouveau_therm_cstate(ptherm, pstate->fanspeed, +1);
94 if (ret && ret != -ENODEV) {
95 nv_error(clk, "failed to raise fan speed: %d\n", ret);
99 ret = volt->set_id(volt, cstate->voltage, +1);
100 if (ret && ret != -ENODEV) {
101 nv_error(clk, "failed to raise voltage: %d\n", ret);
105 ret = clk->calc(clk, cstate);
107 ret = clk->prog(clk);
111 ret = volt->set_id(volt, cstate->voltage, -1);
112 if (ret && ret != -ENODEV)
113 nv_error(clk, "failed to lower voltage: %d\n", ret);
115 ret = nouveau_therm_cstate(ptherm, pstate->fanspeed, -1);
116 if (ret && ret != -ENODEV)
117 nv_error(clk, "failed to lower fan speed: %d\n", ret);
123 nouveau_cstate_del(struct nouveau_cstate *cstate)
125 list_del(&cstate->head);
130 nouveau_cstate_new(struct nouveau_clock *clk, int idx,
131 struct nouveau_pstate *pstate)
133 struct nouveau_bios *bios = nouveau_bios(clk);
134 struct nouveau_clocks *domain = clk->domains;
135 struct nouveau_cstate *cstate = NULL;
136 struct nvbios_cstepX cstepX;
140 data = nvbios_cstepXp(bios, idx, &ver, &hdr, &cstepX);
144 cstate = kzalloc(sizeof(*cstate), GFP_KERNEL);
148 *cstate = pstate->base;
149 cstate->voltage = cstepX.voltage;
151 while (domain && domain->name != nv_clk_src_max) {
152 if (domain->flags & NVKM_CLK_DOM_FLAG_CORE) {
153 u32 freq = nouveau_clock_adjust(clk, true,
157 cstate->domain[domain->name] = freq;
162 list_add(&cstate->head, &pstate->list);
166 /******************************************************************************
168 *****************************************************************************/
170 nouveau_pstate_prog(struct nouveau_clock *clk, int pstatei)
172 struct nouveau_fb *pfb = nouveau_fb(clk);
173 struct nouveau_pstate *pstate;
176 list_for_each_entry(pstate, &clk->states, head) {
177 if (idx++ == pstatei)
181 nv_debug(clk, "setting performance state %d\n", pstatei);
182 clk->pstate = pstatei;
184 if (pfb->ram->calc) {
185 ret = pfb->ram->calc(pfb, pstate->base.domain[nv_clk_src_mem]);
187 ret = pfb->ram->prog(pfb);
191 return nouveau_cstate_prog(clk, pstate, 0);
195 nouveau_pstate_calc(struct nouveau_clock *clk)
199 nv_trace(clk, "P %d U %d A %d T %d D %d\n", clk->pstate,
200 clk->ustate, clk->astate, clk->tstate, clk->dstate);
202 if (clk->state_nr && clk->ustate != -1) {
203 pstate = (clk->ustate < 0) ? clk->astate : clk->ustate;
204 pstate = min(pstate, clk->state_nr - 1 - clk->tstate);
205 pstate = max(pstate, clk->dstate);
207 pstate = clk->pstate = -1;
210 nv_trace(clk, "-> %d\n", pstate);
211 if (pstate != clk->pstate)
212 ret = nouveau_pstate_prog(clk, pstate);
217 nouveau_pstate_info(struct nouveau_clock *clk, struct nouveau_pstate *pstate)
219 struct nouveau_clocks *clock = clk->domains - 1;
220 struct nouveau_cstate *cstate;
221 char info[3][32] = { "", "", "" };
225 if (pstate->pstate != 0xff)
226 snprintf(name, sizeof(name), "%02x", pstate->pstate);
228 while ((++clock)->name != nv_clk_src_max) {
229 u32 lo = pstate->base.domain[clock->name];
234 nv_debug(clk, "%02x: %10d KHz\n", clock->name, lo);
235 list_for_each_entry(cstate, &pstate->list, head) {
236 u32 freq = cstate->domain[clock->name];
239 nv_debug(clk, "%10d KHz\n", freq);
242 if (clock->mname && ++i < ARRAY_SIZE(info)) {
246 snprintf(info[i], sizeof(info[i]), "%s %d MHz",
249 snprintf(info[i], sizeof(info[i]),
250 "%s %d-%d MHz", clock->mname, lo, hi);
255 nv_info(clk, "%s: %s %s %s\n", name, info[0], info[1], info[2]);
259 nouveau_pstate_del(struct nouveau_pstate *pstate)
261 struct nouveau_cstate *cstate, *temp;
263 list_for_each_entry_safe(cstate, temp, &pstate->list, head) {
264 nouveau_cstate_del(cstate);
267 list_del(&pstate->head);
272 nouveau_pstate_new(struct nouveau_clock *clk, int idx)
274 struct nouveau_bios *bios = nouveau_bios(clk);
275 struct nouveau_clocks *domain = clk->domains - 1;
276 struct nouveau_pstate *pstate;
277 struct nouveau_cstate *cstate;
278 struct nvbios_cstepE cstepE;
279 struct nvbios_perfE perfE;
280 u8 ver, hdr, cnt, len;
283 data = nvbios_perfEp(bios, idx, &ver, &hdr, &cnt, &len, &perfE);
286 if (perfE.pstate == 0xff)
289 pstate = kzalloc(sizeof(*pstate), GFP_KERNEL);
290 cstate = &pstate->base;
294 INIT_LIST_HEAD(&pstate->list);
296 pstate->pstate = perfE.pstate;
297 pstate->fanspeed = perfE.fanspeed;
298 cstate->voltage = perfE.voltage;
299 cstate->domain[nv_clk_src_core] = perfE.core;
300 cstate->domain[nv_clk_src_shader] = perfE.shader;
301 cstate->domain[nv_clk_src_mem] = perfE.memory;
302 cstate->domain[nv_clk_src_vdec] = perfE.vdec;
303 cstate->domain[nv_clk_src_dom6] = perfE.disp;
305 while (ver >= 0x40 && (++domain)->name != nv_clk_src_max) {
306 struct nvbios_perfS perfS;
307 u8 sver = ver, shdr = hdr;
308 u32 perfSe = nvbios_perfSp(bios, data, domain->bios,
309 &sver, &shdr, cnt, len, &perfS);
310 if (perfSe == 0 || sver != 0x40)
313 if (domain->flags & NVKM_CLK_DOM_FLAG_CORE) {
314 perfS.v40.freq = nouveau_clock_adjust(clk, false,
320 cstate->domain[domain->name] = perfS.v40.freq;
323 data = nvbios_cstepEm(bios, pstate->pstate, &ver, &hdr, &cstepE);
325 int idx = cstepE.index;
327 nouveau_cstate_new(clk, idx, pstate);
331 nouveau_pstate_info(clk, pstate);
332 list_add_tail(&pstate->head, &clk->states);
337 /******************************************************************************
338 * Adjustment triggers
339 *****************************************************************************/
341 nouveau_clock_ustate_update(struct nouveau_clock *clk, int req)
343 struct nouveau_pstate *pstate;
349 if (req != -1 && req != -2) {
350 list_for_each_entry(pstate, &clk->states, head) {
351 if (pstate->pstate == req)
356 if (pstate->pstate != req)
366 nouveau_clock_ustate(struct nouveau_clock *clk, int req)
368 int ret = nouveau_clock_ustate_update(clk, req);
371 return nouveau_pstate_calc(clk);
375 nouveau_clock_astate(struct nouveau_clock *clk, int req, int rel)
377 if (!rel) clk->astate = req;
378 if ( rel) clk->astate += rel;
379 clk->astate = min(clk->astate, clk->state_nr - 1);
380 clk->astate = max(clk->astate, 0);
381 return nouveau_pstate_calc(clk);
385 nouveau_clock_tstate(struct nouveau_clock *clk, int req, int rel)
387 if (!rel) clk->tstate = req;
388 if ( rel) clk->tstate += rel;
389 clk->tstate = min(clk->tstate, 0);
390 clk->tstate = max(clk->tstate, -(clk->state_nr - 1));
391 return nouveau_pstate_calc(clk);
395 nouveau_clock_dstate(struct nouveau_clock *clk, int req, int rel)
397 if (!rel) clk->dstate = req;
398 if ( rel) clk->dstate += rel;
399 clk->dstate = min(clk->dstate, clk->state_nr - 1);
400 clk->dstate = max(clk->dstate, 0);
401 return nouveau_pstate_calc(clk);
404 /******************************************************************************
405 * subdev base class implementation
406 *****************************************************************************/
408 _nouveau_clock_init(struct nouveau_object *object)
410 struct nouveau_clock *clk = (void *)object;
411 struct nouveau_clocks *clock = clk->domains;
414 memset(&clk->bstate, 0x00, sizeof(clk->bstate));
415 INIT_LIST_HEAD(&clk->bstate.list);
416 clk->bstate.pstate = 0xff;
418 while (clock->name != nv_clk_src_max) {
419 ret = clk->read(clk, clock->name);
421 nv_error(clk, "%02x freq unknown\n", clock->name);
424 clk->bstate.base.domain[clock->name] = ret;
428 nouveau_pstate_info(clk, &clk->bstate);
430 clk->astate = clk->state_nr - 1;
434 nouveau_pstate_calc(clk);
439 _nouveau_clock_dtor(struct nouveau_object *object)
441 struct nouveau_clock *clk = (void *)object;
442 struct nouveau_pstate *pstate, *temp;
444 list_for_each_entry_safe(pstate, temp, &clk->states, head) {
445 nouveau_pstate_del(pstate);
448 nouveau_subdev_destroy(&clk->base);
452 nouveau_clock_create_(struct nouveau_object *parent,
453 struct nouveau_object *engine,
454 struct nouveau_oclass *oclass,
455 struct nouveau_clocks *clocks,
456 int length, void **object)
458 struct nouveau_device *device = nv_device(parent);
459 struct nouveau_clock *clk;
460 int ret, idx, arglen;
463 ret = nouveau_subdev_create_(parent, engine, oclass, 0, "CLK",
464 "clock", length, object);
469 INIT_LIST_HEAD(&clk->states);
470 clk->domains = clocks;
475 ret = nouveau_pstate_new(clk, idx++);
478 mode = nouveau_stropt(device->cfgopt, "NvClkMode", &arglen);
480 if (!strncasecmpz(mode, "disabled", arglen)) {
483 char save = mode[arglen];
486 ((char *)mode)[arglen] = '\0';
487 if (!kstrtol(mode, 0, &v))
488 nouveau_clock_ustate_update(clk, v);
489 ((char *)mode)[arglen] = save;