base.c 14.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright 2013 Red Hat Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: Ben Skeggs
 */
24
#include <subdev/clk.h>
25 26 27 28
#include <subdev/bios.h>
#include <subdev/bios/boost.h>
#include <subdev/bios/cstep.h>
#include <subdev/bios/perf.h>
29 30 31 32 33
#include <subdev/fb.h>
#include <subdev/therm.h>
#include <subdev/volt.h>

#include <core/option.h>
34 35 36 37 38

/******************************************************************************
 * misc
 *****************************************************************************/
static u32
39 40
nvkm_clk_adjust(struct nvkm_clk *clk, bool adjust,
		u8 pstate, u8 domain, u32 input)
41
{
42
	struct nvkm_bios *bios = nvkm_bios(clk);
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
	struct nvbios_boostE boostE;
	u8  ver, hdr, cnt, len;
	u16 data;

	data = nvbios_boostEm(bios, pstate, &ver, &hdr, &cnt, &len, &boostE);
	if (data) {
		struct nvbios_boostS boostS;
		u8  idx = 0, sver, shdr;
		u16 subd;

		input = max(boostE.min, input);
		input = min(boostE.max, input);
		do {
			sver = ver;
			shdr = hdr;
			subd = nvbios_boostSp(bios, idx++, data, &sver, &shdr,
					      cnt, len, &boostS);
			if (subd && boostS.domain == domain) {
				if (adjust)
					input = input * boostS.percent / 100;
				input = max(boostS.min, input);
				input = min(boostS.max, input);
				break;
			}
		} while (subd);
	}

	return input;
}

/******************************************************************************
 * C-States
 *****************************************************************************/
static int
77
nvkm_cstate_prog(struct nvkm_clk *clk, struct nvkm_pstate *pstate, int cstatei)
78
{
79 80 81
	struct nvkm_therm *ptherm = nvkm_therm(clk);
	struct nvkm_volt *volt = nvkm_volt(clk);
	struct nvkm_cstate *cstate;
82 83 84 85 86 87 88 89
	int ret;

	if (!list_empty(&pstate->list)) {
		cstate = list_entry(pstate->list.prev, typeof(*cstate), head);
	} else {
		cstate = &pstate->base;
	}

90
	if (ptherm) {
91
		ret = nvkm_therm_cstate(ptherm, pstate->fanspeed, +1);
92 93 94 95
		if (ret && ret != -ENODEV) {
			nv_error(clk, "failed to raise fan speed: %d\n", ret);
			return ret;
		}
96 97
	}

98 99 100 101 102 103
	if (volt) {
		ret = volt->set_id(volt, cstate->voltage, +1);
		if (ret && ret != -ENODEV) {
			nv_error(clk, "failed to raise voltage: %d\n", ret);
			return ret;
		}
104 105 106 107 108 109 110 111
	}

	ret = clk->calc(clk, cstate);
	if (ret == 0) {
		ret = clk->prog(clk);
		clk->tidy(clk);
	}

112 113 114 115 116
	if (volt) {
		ret = volt->set_id(volt, cstate->voltage, -1);
		if (ret && ret != -ENODEV)
			nv_error(clk, "failed to lower voltage: %d\n", ret);
	}
117

118
	if (ptherm) {
119
		ret = nvkm_therm_cstate(ptherm, pstate->fanspeed, -1);
120 121 122
		if (ret && ret != -ENODEV)
			nv_error(clk, "failed to lower fan speed: %d\n", ret);
	}
123

B
Ben Skeggs 已提交
124
	return ret;
125 126 127
}

static void
128
nvkm_cstate_del(struct nvkm_cstate *cstate)
129 130 131 132 133 134
{
	list_del(&cstate->head);
	kfree(cstate);
}

static int
135
nvkm_cstate_new(struct nvkm_clk *clk, int idx, struct nvkm_pstate *pstate)
136
{
137 138 139
	struct nvkm_bios *bios = nvkm_bios(clk);
	struct nvkm_domain *domain = clk->domains;
	struct nvkm_cstate *cstate = NULL;
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
	struct nvbios_cstepX cstepX;
	u8  ver, hdr;
	u16 data;

	data = nvbios_cstepXp(bios, idx, &ver, &hdr, &cstepX);
	if (!data)
		return -ENOENT;

	cstate = kzalloc(sizeof(*cstate), GFP_KERNEL);
	if (!cstate)
		return -ENOMEM;

	*cstate = pstate->base;
	cstate->voltage = cstepX.voltage;

	while (domain && domain->name != nv_clk_src_max) {
		if (domain->flags & NVKM_CLK_DOM_FLAG_CORE) {
157 158
			u32 freq = nvkm_clk_adjust(clk, true, pstate->pstate,
						   domain->bios, cstepX.freq);
159 160 161 162 163 164 165 166 167 168 169 170 171
			cstate->domain[domain->name] = freq;
		}
		domain++;
	}

	list_add(&cstate->head, &pstate->list);
	return 0;
}

/******************************************************************************
 * P-States
 *****************************************************************************/
static int
172
nvkm_pstate_prog(struct nvkm_clk *clk, int pstatei)
173
{
B
Ben Skeggs 已提交
174
	struct nvkm_fb *fb = nvkm_fb(clk);
175
	struct nvkm_pstate *pstate;
176 177 178 179 180 181 182 183 184 185
	int ret, idx = 0;

	list_for_each_entry(pstate, &clk->states, head) {
		if (idx++ == pstatei)
			break;
	}

	nv_debug(clk, "setting performance state %d\n", pstatei);
	clk->pstate = pstatei;

B
Ben Skeggs 已提交
186
	if (fb->ram && fb->ram->calc) {
187 188
		int khz = pstate->base.domain[nv_clk_src_mem];
		do {
B
Ben Skeggs 已提交
189
			ret = fb->ram->calc(fb, khz);
190
			if (ret == 0)
B
Ben Skeggs 已提交
191
				ret = fb->ram->prog(fb);
192
		} while (ret > 0);
B
Ben Skeggs 已提交
193
		fb->ram->tidy(fb);
194 195
	}

196
	return nvkm_cstate_prog(clk, pstate, 0);
197 198
}

199
static void
200
nvkm_pstate_work(struct work_struct *work)
201
{
202
	struct nvkm_clk *clk = container_of(work, typeof(*clk), work);
203 204 205 206
	int pstate;

	if (!atomic_xchg(&clk->waiting, 0))
		return;
207
	clk->pwrsrc = power_supply_is_system_supplied();
208

209 210 211
	nv_trace(clk, "P %d PWR %d U(AC) %d U(DC) %d A %d T %d D %d\n",
		 clk->pstate, clk->pwrsrc, clk->ustate_ac, clk->ustate_dc,
		 clk->astate, clk->tstate, clk->dstate);
212

213 214 215
	pstate = clk->pwrsrc ? clk->ustate_ac : clk->ustate_dc;
	if (clk->state_nr && pstate != -1) {
		pstate = (pstate < 0) ? clk->astate : pstate;
216
		pstate = min(pstate, clk->state_nr - 1 + clk->tstate);
217 218 219 220 221 222
		pstate = max(pstate, clk->dstate);
	} else {
		pstate = clk->pstate = -1;
	}

	nv_trace(clk, "-> %d\n", pstate);
223
	if (pstate != clk->pstate) {
224
		int ret = nvkm_pstate_prog(clk, pstate);
225 226 227 228 229 230 231
		if (ret) {
			nv_error(clk, "error setting pstate %d: %d\n",
				 pstate, ret);
		}
	}

	wake_up_all(&clk->wait);
232
	nvkm_notify_get(&clk->pwrsrc_ntfy);
233 234 235
}

static int
236
nvkm_pstate_calc(struct nvkm_clk *clk, bool wait)
237 238 239 240 241 242
{
	atomic_set(&clk->waiting, 1);
	schedule_work(&clk->work);
	if (wait)
		wait_event(clk->wait, !atomic_read(&clk->waiting));
	return 0;
243 244 245
}

static void
246
nvkm_pstate_info(struct nvkm_clk *clk, struct nvkm_pstate *pstate)
247
{
248 249
	struct nvkm_domain *clock = clk->domains - 1;
	struct nvkm_cstate *cstate;
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
	char info[3][32] = { "", "", "" };
	char name[4] = "--";
	int i = -1;

	if (pstate->pstate != 0xff)
		snprintf(name, sizeof(name), "%02x", pstate->pstate);

	while ((++clock)->name != nv_clk_src_max) {
		u32 lo = pstate->base.domain[clock->name];
		u32 hi = lo;
		if (hi == 0)
			continue;

		nv_debug(clk, "%02x: %10d KHz\n", clock->name, lo);
		list_for_each_entry(cstate, &pstate->list, head) {
			u32 freq = cstate->domain[clock->name];
			lo = min(lo, freq);
			hi = max(hi, freq);
			nv_debug(clk, "%10d KHz\n", freq);
		}

		if (clock->mname && ++i < ARRAY_SIZE(info)) {
			lo /= clock->mdiv;
			hi /= clock->mdiv;
			if (lo == hi) {
				snprintf(info[i], sizeof(info[i]), "%s %d MHz",
					 clock->mname, lo);
			} else {
				snprintf(info[i], sizeof(info[i]),
					 "%s %d-%d MHz", clock->mname, lo, hi);
			}
		}
	}

	nv_info(clk, "%s: %s %s %s\n", name, info[0], info[1], info[2]);
}

static void
288
nvkm_pstate_del(struct nvkm_pstate *pstate)
289
{
290
	struct nvkm_cstate *cstate, *temp;
291 292

	list_for_each_entry_safe(cstate, temp, &pstate->list, head) {
293
		nvkm_cstate_del(cstate);
294 295 296 297 298 299 300
	}

	list_del(&pstate->head);
	kfree(pstate);
}

static int
301
nvkm_pstate_new(struct nvkm_clk *clk, int idx)
302
{
303 304 305 306
	struct nvkm_bios *bios = nvkm_bios(clk);
	struct nvkm_domain *domain = clk->domains - 1;
	struct nvkm_pstate *pstate;
	struct nvkm_cstate *cstate;
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
	struct nvbios_cstepE cstepE;
	struct nvbios_perfE perfE;
	u8  ver, hdr, cnt, len;
	u16 data;

	data = nvbios_perfEp(bios, idx, &ver, &hdr, &cnt, &len, &perfE);
	if (!data)
		return -EINVAL;
	if (perfE.pstate == 0xff)
		return 0;

	pstate = kzalloc(sizeof(*pstate), GFP_KERNEL);
	cstate = &pstate->base;
	if (!pstate)
		return -ENOMEM;

	INIT_LIST_HEAD(&pstate->list);

	pstate->pstate = perfE.pstate;
	pstate->fanspeed = perfE.fanspeed;
	cstate->voltage = perfE.voltage;
	cstate->domain[nv_clk_src_core] = perfE.core;
	cstate->domain[nv_clk_src_shader] = perfE.shader;
	cstate->domain[nv_clk_src_mem] = perfE.memory;
	cstate->domain[nv_clk_src_vdec] = perfE.vdec;
	cstate->domain[nv_clk_src_dom6] = perfE.disp;

	while (ver >= 0x40 && (++domain)->name != nv_clk_src_max) {
		struct nvbios_perfS perfS;
		u8  sver = ver, shdr = hdr;
		u32 perfSe = nvbios_perfSp(bios, data, domain->bios,
					  &sver, &shdr, cnt, len, &perfS);
		if (perfSe == 0 || sver != 0x40)
			continue;

		if (domain->flags & NVKM_CLK_DOM_FLAG_CORE) {
343 344 345 346
			perfS.v40.freq = nvkm_clk_adjust(clk, false,
							 pstate->pstate,
							 domain->bios,
							 perfS.v40.freq);
347 348 349 350 351 352 353 354 355
		}

		cstate->domain[domain->name] = perfS.v40.freq;
	}

	data = nvbios_cstepEm(bios, pstate->pstate, &ver, &hdr, &cstepE);
	if (data) {
		int idx = cstepE.index;
		do {
356
			nvkm_cstate_new(clk, idx, pstate);
357 358 359
		} while(idx--);
	}

360
	nvkm_pstate_info(clk, pstate);
361 362 363 364 365 366 367 368 369
	list_add_tail(&pstate->head, &clk->states);
	clk->state_nr++;
	return 0;
}

/******************************************************************************
 * Adjustment triggers
 *****************************************************************************/
static int
370
nvkm_clk_ustate_update(struct nvkm_clk *clk, int req)
371
{
372
	struct nvkm_pstate *pstate;
373 374
	int i = 0;

375 376
	if (!clk->allow_reclock)
		return -ENOSYS;
377 378 379 380 381 382 383 384 385 386 387 388 389

	if (req != -1 && req != -2) {
		list_for_each_entry(pstate, &clk->states, head) {
			if (pstate->pstate == req)
				break;
			i++;
		}

		if (pstate->pstate != req)
			return -EINVAL;
		req = i;
	}

390 391 392 393
	return req + 2;
}

static int
394
nvkm_clk_nstate(struct nvkm_clk *clk, const char *mode, int arglen)
395 396 397
{
	int ret = 1;

398 399 400
	if (clk->allow_reclock && !strncasecmpz(mode, "auto", arglen))
		return -2;

401 402 403 404 405 406
	if (strncasecmpz(mode, "disabled", arglen)) {
		char save = mode[arglen];
		long v;

		((char *)mode)[arglen] = '\0';
		if (!kstrtol(mode, 0, &v)) {
407
			ret = nvkm_clk_ustate_update(clk, v);
408 409 410 411 412 413 414
			if (ret < 0)
				ret = 1;
		}
		((char *)mode)[arglen] = save;
	}

	return ret - 2;
415 416 417
}

int
418
nvkm_clk_ustate(struct nvkm_clk *clk, int req, int pwr)
419
{
420
	int ret = nvkm_clk_ustate_update(clk, req);
421 422 423
	if (ret >= 0) {
		if (ret -= 2, pwr) clk->ustate_ac = ret;
		else		   clk->ustate_dc = ret;
424
		return nvkm_pstate_calc(clk, true);
425 426
	}
	return ret;
427 428 429
}

int
430
nvkm_clk_astate(struct nvkm_clk *clk, int req, int rel, bool wait)
431 432 433 434 435
{
	if (!rel) clk->astate  = req;
	if ( rel) clk->astate += rel;
	clk->astate = min(clk->astate, clk->state_nr - 1);
	clk->astate = max(clk->astate, 0);
436
	return nvkm_pstate_calc(clk, wait);
437 438 439
}

int
440
nvkm_clk_tstate(struct nvkm_clk *clk, int req, int rel)
441 442 443 444 445
{
	if (!rel) clk->tstate  = req;
	if ( rel) clk->tstate += rel;
	clk->tstate = min(clk->tstate, 0);
	clk->tstate = max(clk->tstate, -(clk->state_nr - 1));
446
	return nvkm_pstate_calc(clk, true);
447 448 449
}

int
450
nvkm_clk_dstate(struct nvkm_clk *clk, int req, int rel)
451 452 453 454 455
{
	if (!rel) clk->dstate  = req;
	if ( rel) clk->dstate += rel;
	clk->dstate = min(clk->dstate, clk->state_nr - 1);
	clk->dstate = max(clk->dstate, 0);
456
	return nvkm_pstate_calc(clk, true);
457 458
}

459
static int
460
nvkm_clk_pwrsrc(struct nvkm_notify *notify)
461
{
462
	struct nvkm_clk *clk =
463
		container_of(notify, typeof(*clk), pwrsrc_ntfy);
464
	nvkm_pstate_calc(clk, false);
465
	return NVKM_NOTIFY_DROP;
466 467
}

468 469 470
/******************************************************************************
 * subdev base class implementation
 *****************************************************************************/
471 472

int
473
_nvkm_clk_fini(struct nvkm_object *object, bool suspend)
474
{
475
	struct nvkm_clk *clk = (void *)object;
476
	nvkm_notify_put(&clk->pwrsrc_ntfy);
B
Ben Skeggs 已提交
477
	return nvkm_subdev_fini(&clk->subdev, suspend);
478 479
}

480
int
481
_nvkm_clk_init(struct nvkm_object *object)
482
{
483 484
	struct nvkm_clk *clk = (void *)object;
	struct nvkm_domain *clock = clk->domains;
485 486
	int ret;

B
Ben Skeggs 已提交
487
	ret = nvkm_subdev_init(&clk->subdev);
488 489 490
	if (ret)
		return ret;

491 492 493 494 495 496 497 498 499 500 501 502 503 504
	memset(&clk->bstate, 0x00, sizeof(clk->bstate));
	INIT_LIST_HEAD(&clk->bstate.list);
	clk->bstate.pstate = 0xff;

	while (clock->name != nv_clk_src_max) {
		ret = clk->read(clk, clock->name);
		if (ret < 0) {
			nv_error(clk, "%02x freq unknown\n", clock->name);
			return ret;
		}
		clk->bstate.base.domain[clock->name] = ret;
		clock++;
	}

505
	nvkm_pstate_info(clk, &clk->bstate);
506 507 508 509 510

	clk->astate = clk->state_nr - 1;
	clk->tstate = 0;
	clk->dstate = 0;
	clk->pstate = -1;
511
	nvkm_pstate_calc(clk, true);
512 513 514 515
	return 0;
}

void
516
_nvkm_clk_dtor(struct nvkm_object *object)
517
{
518 519
	struct nvkm_clk *clk = (void *)object;
	struct nvkm_pstate *pstate, *temp;
520

521
	nvkm_notify_fini(&clk->pwrsrc_ntfy);
522

523
	list_for_each_entry_safe(pstate, temp, &clk->states, head) {
524
		nvkm_pstate_del(pstate);
525 526
	}

B
Ben Skeggs 已提交
527
	nvkm_subdev_destroy(&clk->subdev);
528 529 530
}

int
531 532 533 534
nvkm_clk_create_(struct nvkm_object *parent, struct nvkm_object *engine,
		 struct nvkm_oclass *oclass, struct nvkm_domain *clocks,
		 struct nvkm_pstate *pstates, int nb_pstates,
		 bool allow_reclock, int length, void **object)
535
{
536 537
	struct nvkm_device *device = nv_device(parent);
	struct nvkm_clk *clk;
538 539 540
	int ret, idx, arglen;
	const char *mode;

541 542
	ret = nvkm_subdev_create_(parent, engine, oclass, 0, "CLK",
				  "clock", length, object);
543 544 545 546 547 548
	clk = *object;
	if (ret)
		return ret;

	INIT_LIST_HEAD(&clk->states);
	clk->domains = clocks;
549 550
	clk->ustate_ac = -1;
	clk->ustate_dc = -1;
551

552
	INIT_WORK(&clk->work, nvkm_pstate_work);
553 554 555
	init_waitqueue_head(&clk->wait);
	atomic_set(&clk->waiting, 0);

556 557 558 559
	/* If no pstates are provided, try and fetch them from the BIOS */
	if (!pstates) {
		idx = 0;
		do {
560
			ret = nvkm_pstate_new(clk, idx++);
561 562 563 564 565 566
		} while (ret == 0);
	} else {
		for (idx = 0; idx < nb_pstates; idx++)
			list_add_tail(&pstates[idx].head, &clk->states);
		clk->state_nr = nb_pstates;
	}
567

568 569
	clk->allow_reclock = allow_reclock;

570
	ret = nvkm_notify_init(NULL, &device->event, nvkm_clk_pwrsrc, true,
571
			       NULL, 0, 0, &clk->pwrsrc_ntfy);
572 573 574
	if (ret)
		return ret;

575
	mode = nvkm_stropt(device->cfgopt, "NvClkMode", &arglen);
576
	if (mode) {
577 578
		clk->ustate_ac = nvkm_clk_nstate(clk, mode, arglen);
		clk->ustate_dc = nvkm_clk_nstate(clk, mode, arglen);
579 580
	}

581
	mode = nvkm_stropt(device->cfgopt, "NvClkModeAC", &arglen);
582
	if (mode)
583
		clk->ustate_ac = nvkm_clk_nstate(clk, mode, arglen);
584

585
	mode = nvkm_stropt(device->cfgopt, "NvClkModeDC", &arglen);
586
	if (mode)
587
		clk->ustate_dc = nvkm_clk_nstate(clk, mode, arglen);
588

589 590
	return 0;
}