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.
24 #include <subdev/clk.h>
25 #include <subdev/bios.h>
26 #include <subdev/bios/boost.h>
27 #include <subdev/bios/cstep.h>
28 #include <subdev/bios/perf.h>
29 #include <subdev/fb.h>
30 #include <subdev/therm.h>
31 #include <subdev/volt.h>
33 #include <core/option.h>
35 /******************************************************************************
37 *****************************************************************************/
39 nvkm_clk_adjust(struct nvkm_clk *clk, bool adjust,
40 u8 pstate, u8 domain, u32 input)
42 struct nvkm_bios *bios = nvkm_bios(clk);
43 struct nvbios_boostE boostE;
44 u8 ver, hdr, cnt, len;
47 data = nvbios_boostEm(bios, pstate, &ver, &hdr, &cnt, &len, &boostE);
49 struct nvbios_boostS boostS;
50 u8 idx = 0, sver, shdr;
53 input = max(boostE.min, input);
54 input = min(boostE.max, input);
58 subd = nvbios_boostSp(bios, idx++, data, &sver, &shdr,
60 if (subd && boostS.domain == domain) {
62 input = input * boostS.percent / 100;
63 input = max(boostS.min, input);
64 input = min(boostS.max, input);
73 /******************************************************************************
75 *****************************************************************************/
77 nvkm_cstate_prog(struct nvkm_clk *clk, struct nvkm_pstate *pstate, int cstatei)
79 struct nvkm_therm *ptherm = nvkm_therm(clk);
80 struct nvkm_volt *volt = nvkm_volt(clk);
81 struct nvkm_cstate *cstate;
84 if (!list_empty(&pstate->list)) {
85 cstate = list_entry(pstate->list.prev, typeof(*cstate), head);
87 cstate = &pstate->base;
91 ret = nvkm_therm_cstate(ptherm, pstate->fanspeed, +1);
92 if (ret && ret != -ENODEV) {
93 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);
106 ret = clk->calc(clk, cstate);
108 ret = clk->prog(clk);
113 ret = volt->set_id(volt, cstate->voltage, -1);
114 if (ret && ret != -ENODEV)
115 nv_error(clk, "failed to lower voltage: %d\n", ret);
119 ret = nvkm_therm_cstate(ptherm, pstate->fanspeed, -1);
120 if (ret && ret != -ENODEV)
121 nv_error(clk, "failed to lower fan speed: %d\n", ret);
128 nvkm_cstate_del(struct nvkm_cstate *cstate)
130 list_del(&cstate->head);
135 nvkm_cstate_new(struct nvkm_clk *clk, int idx, struct nvkm_pstate *pstate)
137 struct nvkm_bios *bios = nvkm_bios(clk);
138 struct nvkm_domain *domain = clk->domains;
139 struct nvkm_cstate *cstate = NULL;
140 struct nvbios_cstepX cstepX;
144 data = nvbios_cstepXp(bios, idx, &ver, &hdr, &cstepX);
148 cstate = kzalloc(sizeof(*cstate), GFP_KERNEL);
152 *cstate = pstate->base;
153 cstate->voltage = cstepX.voltage;
155 while (domain && domain->name != nv_clk_src_max) {
156 if (domain->flags & NVKM_CLK_DOM_FLAG_CORE) {
157 u32 freq = nvkm_clk_adjust(clk, true, pstate->pstate,
158 domain->bios, cstepX.freq);
159 cstate->domain[domain->name] = freq;
164 list_add(&cstate->head, &pstate->list);
168 /******************************************************************************
170 *****************************************************************************/
172 nvkm_pstate_prog(struct nvkm_clk *clk, int pstatei)
174 struct nvkm_fb *pfb = nvkm_fb(clk);
175 struct nvkm_pstate *pstate;
178 list_for_each_entry(pstate, &clk->states, head) {
179 if (idx++ == pstatei)
183 nv_debug(clk, "setting performance state %d\n", pstatei);
184 clk->pstate = pstatei;
186 if (pfb->ram && pfb->ram->calc) {
187 int khz = pstate->base.domain[nv_clk_src_mem];
189 ret = pfb->ram->calc(pfb, khz);
191 ret = pfb->ram->prog(pfb);
196 return nvkm_cstate_prog(clk, pstate, 0);
200 nvkm_pstate_work(struct work_struct *work)
202 struct nvkm_clk *clk = container_of(work, typeof(*clk), work);
205 if (!atomic_xchg(&clk->waiting, 0))
207 clk->pwrsrc = power_supply_is_system_supplied();
209 nv_trace(clk, "P %d PWR %d U(AC) %d U(DC) %d A %d T %d D %d\n",
210 clk->pstate, clk->pwrsrc, clk->ustate_ac, clk->ustate_dc,
211 clk->astate, clk->tstate, clk->dstate);
213 pstate = clk->pwrsrc ? clk->ustate_ac : clk->ustate_dc;
214 if (clk->state_nr && pstate != -1) {
215 pstate = (pstate < 0) ? clk->astate : pstate;
216 pstate = min(pstate, clk->state_nr - 1 + clk->tstate);
217 pstate = max(pstate, clk->dstate);
219 pstate = clk->pstate = -1;
222 nv_trace(clk, "-> %d\n", pstate);
223 if (pstate != clk->pstate) {
224 int ret = nvkm_pstate_prog(clk, pstate);
226 nv_error(clk, "error setting pstate %d: %d\n",
231 wake_up_all(&clk->wait);
232 nvkm_notify_get(&clk->pwrsrc_ntfy);
236 nvkm_pstate_calc(struct nvkm_clk *clk, bool wait)
238 atomic_set(&clk->waiting, 1);
239 schedule_work(&clk->work);
241 wait_event(clk->wait, !atomic_read(&clk->waiting));
246 nvkm_pstate_info(struct nvkm_clk *clk, struct nvkm_pstate *pstate)
248 struct nvkm_domain *clock = clk->domains - 1;
249 struct nvkm_cstate *cstate;
250 char info[3][32] = { "", "", "" };
254 if (pstate->pstate != 0xff)
255 snprintf(name, sizeof(name), "%02x", pstate->pstate);
257 while ((++clock)->name != nv_clk_src_max) {
258 u32 lo = pstate->base.domain[clock->name];
263 nv_debug(clk, "%02x: %10d KHz\n", clock->name, lo);
264 list_for_each_entry(cstate, &pstate->list, head) {
265 u32 freq = cstate->domain[clock->name];
268 nv_debug(clk, "%10d KHz\n", freq);
271 if (clock->mname && ++i < ARRAY_SIZE(info)) {
275 snprintf(info[i], sizeof(info[i]), "%s %d MHz",
278 snprintf(info[i], sizeof(info[i]),
279 "%s %d-%d MHz", clock->mname, lo, hi);
284 nv_info(clk, "%s: %s %s %s\n", name, info[0], info[1], info[2]);
288 nvkm_pstate_del(struct nvkm_pstate *pstate)
290 struct nvkm_cstate *cstate, *temp;
292 list_for_each_entry_safe(cstate, temp, &pstate->list, head) {
293 nvkm_cstate_del(cstate);
296 list_del(&pstate->head);
301 nvkm_pstate_new(struct nvkm_clk *clk, int idx)
303 struct nvkm_bios *bios = nvkm_bios(clk);
304 struct nvkm_domain *domain = clk->domains - 1;
305 struct nvkm_pstate *pstate;
306 struct nvkm_cstate *cstate;
307 struct nvbios_cstepE cstepE;
308 struct nvbios_perfE perfE;
309 u8 ver, hdr, cnt, len;
312 data = nvbios_perfEp(bios, idx, &ver, &hdr, &cnt, &len, &perfE);
315 if (perfE.pstate == 0xff)
318 pstate = kzalloc(sizeof(*pstate), GFP_KERNEL);
319 cstate = &pstate->base;
323 INIT_LIST_HEAD(&pstate->list);
325 pstate->pstate = perfE.pstate;
326 pstate->fanspeed = perfE.fanspeed;
327 cstate->voltage = perfE.voltage;
328 cstate->domain[nv_clk_src_core] = perfE.core;
329 cstate->domain[nv_clk_src_shader] = perfE.shader;
330 cstate->domain[nv_clk_src_mem] = perfE.memory;
331 cstate->domain[nv_clk_src_vdec] = perfE.vdec;
332 cstate->domain[nv_clk_src_dom6] = perfE.disp;
334 while (ver >= 0x40 && (++domain)->name != nv_clk_src_max) {
335 struct nvbios_perfS perfS;
336 u8 sver = ver, shdr = hdr;
337 u32 perfSe = nvbios_perfSp(bios, data, domain->bios,
338 &sver, &shdr, cnt, len, &perfS);
339 if (perfSe == 0 || sver != 0x40)
342 if (domain->flags & NVKM_CLK_DOM_FLAG_CORE) {
343 perfS.v40.freq = nvkm_clk_adjust(clk, false,
349 cstate->domain[domain->name] = perfS.v40.freq;
352 data = nvbios_cstepEm(bios, pstate->pstate, &ver, &hdr, &cstepE);
354 int idx = cstepE.index;
356 nvkm_cstate_new(clk, idx, pstate);
360 nvkm_pstate_info(clk, pstate);
361 list_add_tail(&pstate->head, &clk->states);
366 /******************************************************************************
367 * Adjustment triggers
368 *****************************************************************************/
370 nvkm_clk_ustate_update(struct nvkm_clk *clk, int req)
372 struct nvkm_pstate *pstate;
375 if (!clk->allow_reclock)
378 if (req != -1 && req != -2) {
379 list_for_each_entry(pstate, &clk->states, head) {
380 if (pstate->pstate == req)
385 if (pstate->pstate != req)
394 nvkm_clk_nstate(struct nvkm_clk *clk, const char *mode, int arglen)
398 if (clk->allow_reclock && !strncasecmpz(mode, "auto", arglen))
401 if (strncasecmpz(mode, "disabled", arglen)) {
402 char save = mode[arglen];
405 ((char *)mode)[arglen] = '\0';
406 if (!kstrtol(mode, 0, &v)) {
407 ret = nvkm_clk_ustate_update(clk, v);
411 ((char *)mode)[arglen] = save;
418 nvkm_clk_ustate(struct nvkm_clk *clk, int req, int pwr)
420 int ret = nvkm_clk_ustate_update(clk, req);
422 if (ret -= 2, pwr) clk->ustate_ac = ret;
423 else clk->ustate_dc = ret;
424 return nvkm_pstate_calc(clk, true);
430 nvkm_clk_astate(struct nvkm_clk *clk, int req, int rel, bool wait)
432 if (!rel) clk->astate = req;
433 if ( rel) clk->astate += rel;
434 clk->astate = min(clk->astate, clk->state_nr - 1);
435 clk->astate = max(clk->astate, 0);
436 return nvkm_pstate_calc(clk, wait);
440 nvkm_clk_tstate(struct nvkm_clk *clk, int req, int rel)
442 if (!rel) clk->tstate = req;
443 if ( rel) clk->tstate += rel;
444 clk->tstate = min(clk->tstate, 0);
445 clk->tstate = max(clk->tstate, -(clk->state_nr - 1));
446 return nvkm_pstate_calc(clk, true);
450 nvkm_clk_dstate(struct nvkm_clk *clk, int req, int rel)
452 if (!rel) clk->dstate = req;
453 if ( rel) clk->dstate += rel;
454 clk->dstate = min(clk->dstate, clk->state_nr - 1);
455 clk->dstate = max(clk->dstate, 0);
456 return nvkm_pstate_calc(clk, true);
460 nvkm_clk_pwrsrc(struct nvkm_notify *notify)
462 struct nvkm_clk *clk =
463 container_of(notify, typeof(*clk), pwrsrc_ntfy);
464 nvkm_pstate_calc(clk, false);
465 return NVKM_NOTIFY_DROP;
468 /******************************************************************************
469 * subdev base class implementation
470 *****************************************************************************/
473 _nvkm_clk_fini(struct nvkm_object *object, bool suspend)
475 struct nvkm_clk *clk = (void *)object;
476 nvkm_notify_put(&clk->pwrsrc_ntfy);
477 return nvkm_subdev_fini(&clk->base, suspend);
481 _nvkm_clk_init(struct nvkm_object *object)
483 struct nvkm_clk *clk = (void *)object;
484 struct nvkm_domain *clock = clk->domains;
487 ret = nvkm_subdev_init(&clk->base);
491 memset(&clk->bstate, 0x00, sizeof(clk->bstate));
492 INIT_LIST_HEAD(&clk->bstate.list);
493 clk->bstate.pstate = 0xff;
495 while (clock->name != nv_clk_src_max) {
496 ret = clk->read(clk, clock->name);
498 nv_error(clk, "%02x freq unknown\n", clock->name);
501 clk->bstate.base.domain[clock->name] = ret;
505 nvkm_pstate_info(clk, &clk->bstate);
507 clk->astate = clk->state_nr - 1;
511 nvkm_pstate_calc(clk, true);
516 _nvkm_clk_dtor(struct nvkm_object *object)
518 struct nvkm_clk *clk = (void *)object;
519 struct nvkm_pstate *pstate, *temp;
521 nvkm_notify_fini(&clk->pwrsrc_ntfy);
523 list_for_each_entry_safe(pstate, temp, &clk->states, head) {
524 nvkm_pstate_del(pstate);
527 nvkm_subdev_destroy(&clk->base);
531 nvkm_clk_create_(struct nvkm_object *parent, struct nvkm_object *engine,
532 struct nvkm_oclass *oclass, struct nvkm_domain *clocks,
533 struct nvkm_pstate *pstates, int nb_pstates,
534 bool allow_reclock, int length, void **object)
536 struct nvkm_device *device = nv_device(parent);
537 struct nvkm_clk *clk;
538 int ret, idx, arglen;
541 ret = nvkm_subdev_create_(parent, engine, oclass, 0, "CLK",
542 "clock", length, object);
547 INIT_LIST_HEAD(&clk->states);
548 clk->domains = clocks;
552 INIT_WORK(&clk->work, nvkm_pstate_work);
553 init_waitqueue_head(&clk->wait);
554 atomic_set(&clk->waiting, 0);
556 /* If no pstates are provided, try and fetch them from the BIOS */
560 ret = nvkm_pstate_new(clk, idx++);
563 for (idx = 0; idx < nb_pstates; idx++)
564 list_add_tail(&pstates[idx].head, &clk->states);
565 clk->state_nr = nb_pstates;
568 clk->allow_reclock = allow_reclock;
570 ret = nvkm_notify_init(NULL, &device->event, nvkm_clk_pwrsrc, true,
571 NULL, 0, 0, &clk->pwrsrc_ntfy);
575 mode = nvkm_stropt(device->cfgopt, "NvClkMode", &arglen);
577 clk->ustate_ac = nvkm_clk_nstate(clk, mode, arglen);
578 clk->ustate_dc = nvkm_clk_nstate(clk, mode, arglen);
581 mode = nvkm_stropt(device->cfgopt, "NvClkModeAC", &arglen);
583 clk->ustate_ac = nvkm_clk_nstate(clk, mode, arglen);
585 mode = nvkm_stropt(device->cfgopt, "NvClkModeDC", &arglen);
587 clk->ustate_dc = nvkm_clk_nstate(clk, mode, arglen);