base.c 19.6 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 "priv.h"
25

26
#include <core/client.h>
27
#include <core/option.h>
28

29 30
#include <nvif/class.h>
#include <nvif/ioctl.h>
31
#include <nvif/unpack.h>
32

33
static u8
B
Ben Skeggs 已提交
34
nvkm_pm_count_perfdom(struct nvkm_pm *pm)
35 36 37 38
{
	struct nvkm_perfdom *dom;
	u8 domain_nr = 0;

B
Ben Skeggs 已提交
39
	list_for_each_entry(dom, &pm->domains, head)
40 41 42 43
		domain_nr++;
	return domain_nr;
}

44
static u16
45 46
nvkm_perfdom_count_perfsig(struct nvkm_perfdom *dom)
{
47
	u16 signal_nr = 0;
48 49 50 51 52 53 54 55 56 57 58 59
	int i;

	if (dom) {
		for (i = 0; i < dom->signal_nr; i++) {
			if (dom->signal[i].name)
				signal_nr++;
		}
	}
	return signal_nr;
}

static struct nvkm_perfdom *
B
Ben Skeggs 已提交
60
nvkm_perfdom_find(struct nvkm_pm *pm, int di)
61 62 63 64
{
	struct nvkm_perfdom *dom;
	int tmp = 0;

B
Ben Skeggs 已提交
65
	list_for_each_entry(dom, &pm->domains, head) {
66 67 68 69 70 71
		if (tmp++ == di)
			return dom;
	}
	return NULL;
}

72
struct nvkm_perfsig *
B
Ben Skeggs 已提交
73
nvkm_perfsig_find(struct nvkm_pm *pm, u8 di, u8 si, struct nvkm_perfdom **pdom)
74
{
75
	struct nvkm_perfdom *dom = *pdom;
76 77

	if (dom == NULL) {
B
Ben Skeggs 已提交
78
		dom = nvkm_perfdom_find(pm, di);
79 80 81
		if (dom == NULL)
			return NULL;
		*pdom = dom;
82 83
	}

84 85 86
	if (!dom->signal[si].name)
		return NULL;
	return &dom->signal[si];
87 88
}

89 90 91 92 93 94 95 96 97 98 99 100
static u8
nvkm_perfsig_count_perfsrc(struct nvkm_perfsig *sig)
{
	u8 source_nr = 0, i;

	for (i = 0; i < ARRAY_SIZE(sig->source); i++) {
		if (sig->source[i])
			source_nr++;
	}
	return source_nr;
}

101
static struct nvkm_perfsrc *
B
Ben Skeggs 已提交
102
nvkm_perfsrc_find(struct nvkm_pm *pm, struct nvkm_perfsig *sig, int si)
103 104 105 106 107 108 109 110 111 112 113 114 115 116
{
	struct nvkm_perfsrc *src;
	bool found = false;
	int tmp = 1; /* Sources ID start from 1 */
	u8 i;

	for (i = 0; i < ARRAY_SIZE(sig->source) && sig->source[i]; i++) {
		if (sig->source[i] == si) {
			found = true;
			break;
		}
	}

	if (found) {
B
Ben Skeggs 已提交
117
		list_for_each_entry(src, &pm->sources, head) {
118 119 120 121 122 123 124 125
			if (tmp++ == si)
				return src;
		}
	}

	return NULL;
}

126
static int
B
Ben Skeggs 已提交
127
nvkm_perfsrc_enable(struct nvkm_pm *pm, struct nvkm_perfctr *ctr)
128
{
129
	struct nvkm_device *device = pm->engine.subdev.device;
130 131 132 133 134 135
	struct nvkm_perfdom *dom = NULL;
	struct nvkm_perfsig *sig;
	struct nvkm_perfsrc *src;
	u32 mask, value;
	int i, j;

136
	for (i = 0; i < 4; i++) {
137
		for (j = 0; j < 8 && ctr->source[i][j]; j++) {
B
Ben Skeggs 已提交
138
			sig = nvkm_perfsig_find(pm, ctr->domain,
139 140 141 142
						ctr->signal[i], &dom);
			if (!sig)
				return -EINVAL;

B
Ben Skeggs 已提交
143
			src = nvkm_perfsrc_find(pm, sig, ctr->source[i][j]);
144 145 146 147 148 149 150 151 152 153 154
			if (!src)
				return -EINVAL;

			/* set enable bit if needed */
			mask = value = 0x00000000;
			if (src->enable)
				mask = value = 0x80000000;
			mask  |= (src->mask << src->shift);
			value |= ((ctr->source[i][j] >> 32) << src->shift);

			/* enable the source */
155
			nvkm_mask(device, src->addr, mask, value);
B
Ben Skeggs 已提交
156
			nv_debug(pm, "enabled source 0x%08x 0x%08x 0x%08x\n",
157
				 src->addr, mask, value);
158 159 160 161 162 163
		}
	}
	return 0;
}

static int
B
Ben Skeggs 已提交
164
nvkm_perfsrc_disable(struct nvkm_pm *pm, struct nvkm_perfctr *ctr)
165
{
166
	struct nvkm_device *device = pm->engine.subdev.device;
167 168 169
	struct nvkm_perfdom *dom = NULL;
	struct nvkm_perfsig *sig;
	struct nvkm_perfsrc *src;
170
	u32 mask;
171 172
	int i, j;

173
	for (i = 0; i < 4; i++) {
174
		for (j = 0; j < 8 && ctr->source[i][j]; j++) {
B
Ben Skeggs 已提交
175
			sig = nvkm_perfsig_find(pm, ctr->domain,
176 177 178 179
						ctr->signal[i], &dom);
			if (!sig)
				return -EINVAL;

B
Ben Skeggs 已提交
180
			src = nvkm_perfsrc_find(pm, sig, ctr->source[i][j]);
181 182 183
			if (!src)
				return -EINVAL;

184 185 186 187 188 189
			/* unset enable bit if needed */
			mask = 0x00000000;
			if (src->enable)
				mask = 0x80000000;
			mask |= (src->mask << src->shift);

190
			/* disable the source */
191
			nvkm_mask(device, src->addr, mask, 0);
B
Ben Skeggs 已提交
192
			nv_debug(pm, "disabled source 0x%08x 0x%08x\n",
193
				 src->addr, mask);
194 195 196 197 198
		}
	}
	return 0;
}

199
/*******************************************************************************
200
 * Perfdom object classes
201
 ******************************************************************************/
202
static int
203
nvkm_perfdom_init(struct nvkm_object *object, void *data, u32 size)
204 205
{
	union {
206
		struct nvif_perfdom_init none;
207
	} *args = data;
B
Ben Skeggs 已提交
208
	struct nvkm_pm *pm = (void *)object->engine;
209 210
	struct nvkm_perfdom *dom = (void *)object;
	int ret, i;
211

212
	nv_ioctl(object, "perfdom init size %d\n", size);
213
	if (nvif_unvers(args->none)) {
214
		nv_ioctl(object, "perfdom init\n");
215 216 217
	} else
		return ret;

218 219
	for (i = 0; i < 4; i++) {
		if (dom->ctr[i]) {
B
Ben Skeggs 已提交
220
			dom->func->init(pm, dom, dom->ctr[i]);
221

222
			/* enable sources */
B
Ben Skeggs 已提交
223
			nvkm_perfsrc_enable(pm, dom->ctr[i]);
224 225 226
		}
	}

227
	/* start next batch of counters for sampling */
B
Ben Skeggs 已提交
228
	dom->func->next(pm, dom);
229 230 231
	return 0;
}

232
static int
233
nvkm_perfdom_sample(struct nvkm_object *object, void *data, u32 size)
234
{
235
	union {
236
		struct nvif_perfdom_sample none;
237
	} *args = data;
B
Ben Skeggs 已提交
238
	struct nvkm_pm *pm = (void *)object->engine;
239
	struct nvkm_perfdom *dom;
240
	int ret;
241

242
	nv_ioctl(object, "perfdom sample size %d\n", size);
243
	if (nvif_unvers(args->none)) {
244
		nv_ioctl(object, "perfdom sample\n");
245 246
	} else
		return ret;
B
Ben Skeggs 已提交
247
	pm->sequence++;
248

249
	/* sample previous batch of counters */
B
Ben Skeggs 已提交
250 251
	list_for_each_entry(dom, &pm->domains, head)
		dom->func->next(pm, dom);
252 253 254 255 256

	return 0;
}

static int
257
nvkm_perfdom_read(struct nvkm_object *object, void *data, u32 size)
258
{
259
	union {
260
		struct nvif_perfdom_read_v0 v0;
261
	} *args = data;
B
Ben Skeggs 已提交
262
	struct nvkm_pm *pm = (void *)object->engine;
263 264
	struct nvkm_perfdom *dom = (void *)object;
	int ret, i;
265

266
	nv_ioctl(object, "perfdom read size %d\n", size);
267
	if (nvif_unpack(args->v0, 0, 0, false)) {
268
		nv_ioctl(object, "perfdom read vers %d\n", args->v0.version);
269 270
	} else
		return ret;
271

272 273
	for (i = 0; i < 4; i++) {
		if (dom->ctr[i])
B
Ben Skeggs 已提交
274
			dom->func->read(pm, dom, dom->ctr[i]);
275 276 277
	}

	if (!dom->clk)
278 279
		return -EAGAIN;

280 281 282 283
	for (i = 0; i < 4; i++)
		if (dom->ctr[i])
			args->v0.ctr[i] = dom->ctr[i]->ctr;
	args->v0.clk = dom->clk;
284 285 286
	return 0;
}

287
static int
288
nvkm_perfdom_mthd(struct nvkm_object *object, u32 mthd, void *data, u32 size)
289 290
{
	switch (mthd) {
291 292 293 294 295 296
	case NVIF_PERFDOM_V0_INIT:
		return nvkm_perfdom_init(object, data, size);
	case NVIF_PERFDOM_V0_SAMPLE:
		return nvkm_perfdom_sample(object, data, size);
	case NVIF_PERFDOM_V0_READ:
		return nvkm_perfdom_read(object, data, size);
297 298 299 300 301 302
	default:
		break;
	}
	return -EINVAL;
}

303
static void
304
nvkm_perfdom_dtor(struct nvkm_object *object)
305
{
B
Ben Skeggs 已提交
306
	struct nvkm_pm *pm = (void *)object->engine;
307 308 309 310 311
	struct nvkm_perfdom *dom = (void *)object;
	int i;

	for (i = 0; i < 4; i++) {
		struct nvkm_perfctr *ctr = dom->ctr[i];
312
		if (ctr) {
B
Ben Skeggs 已提交
313
			nvkm_perfsrc_disable(pm, ctr);
314 315 316
			if (ctr->head.next)
				list_del(&ctr->head);
		}
317 318 319
		kfree(ctr);
	}
	nvkm_object_destroy(&dom->base);
320 321 322
}

static int
B
Ben Skeggs 已提交
323 324 325
nvkm_perfctr_new(struct nvkm_perfdom *dom, int slot, u8 domain,
		 struct nvkm_perfsig *signal[4], u64 source[4][8],
		 u16 logic_op, struct nvkm_perfctr **pctr)
326 327
{
	struct nvkm_perfctr *ctr;
328
	int i, j;
329 330 331 332 333 334 335 336

	if (!dom)
		return -EINVAL;

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

337
	ctr->domain   = domain;
338 339 340
	ctr->logic_op = logic_op;
	ctr->slot     = slot;
	for (i = 0; i < 4; i++) {
341
		if (signal[i]) {
342
			ctr->signal[i] = signal[i] - dom->signal;
343 344 345
			for (j = 0; j < 8; j++)
				ctr->source[i][j] = source[i][j];
		}
346 347 348 349 350 351 352 353
	}
	list_add_tail(&ctr->head, &dom->list);

	return 0;
}

static int
nvkm_perfdom_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
354 355
		  struct nvkm_oclass *oclass, void *data, u32 size,
		  struct nvkm_object **pobject)
356
{
357
	union {
358
		struct nvif_perfdom_v0 v0;
359
	} *args = data;
B
Ben Skeggs 已提交
360
	struct nvkm_pm *pm = (void *)engine;
361 362 363
	struct nvkm_perfdom *sdom = NULL;
	struct nvkm_perfctr *ctr[4] = {};
	struct nvkm_perfdom *dom;
364
	int c, s, m;
365
	int ret;
366

367
	nv_ioctl(parent, "create perfdom size %d\n", size);
368
	if (nvif_unpack(args->v0, 0, 0, false)) {
369 370
		nv_ioctl(parent, "create perfdom vers %d dom %d mode %02x\n",
			 args->v0.version, args->v0.domain, args->v0.mode);
371 372
	} else
		return ret;
373

374 375
	for (c = 0; c < ARRAY_SIZE(args->v0.ctr); c++) {
		struct nvkm_perfsig *sig[4] = {};
376
		u64 src[4][8] = {};
377

378
		for (s = 0; s < ARRAY_SIZE(args->v0.ctr[c].signal); s++) {
B
Ben Skeggs 已提交
379
			sig[s] = nvkm_perfsig_find(pm, args->v0.domain,
380 381 382 383
						   args->v0.ctr[c].signal[s],
						   &sdom);
			if (args->v0.ctr[c].signal[s] && !sig[s])
				return -EINVAL;
384 385 386

			for (m = 0; m < 8; m++) {
				src[s][m] = args->v0.ctr[c].source[s][m];
B
Ben Skeggs 已提交
387
				if (src[s][m] && !nvkm_perfsrc_find(pm, sig[s],
388 389 390
							            src[s][m]))
					return -EINVAL;
			}
391 392
		}

393
		ret = nvkm_perfctr_new(sdom, c, args->v0.domain, sig, src,
394 395 396
				       args->v0.ctr[c].logic_op, &ctr[c]);
		if (ret)
			return ret;
397 398
	}

399
	if (!sdom)
400 401
		return -EINVAL;

402 403
	ret = nvkm_object_create(parent, engine, oclass, 0, &dom);
	*pobject = nv_object(dom);
404 405 406
	if (ret)
		return ret;

407 408 409 410 411
	dom->func = sdom->func;
	dom->addr = sdom->addr;
	dom->mode = args->v0.mode;
	for (c = 0; c < ARRAY_SIZE(ctr); c++)
		dom->ctr[c] = ctr[c];
412 413 414
	return 0;
}

415
static struct nvkm_ofuncs
416 417 418
nvkm_perfdom_ofuncs = {
	.ctor = nvkm_perfdom_ctor,
	.dtor = nvkm_perfdom_dtor,
419 420
	.init = nvkm_object_init,
	.fini = nvkm_object_fini,
421
	.mthd = nvkm_perfdom_mthd,
422 423
};

424 425 426 427 428 429 430 431 432
/*******************************************************************************
 * Perfmon object classes
 ******************************************************************************/
static int
nvkm_perfmon_mthd_query_domain(struct nvkm_object *object, void *data, u32 size)
{
	union {
		struct nvif_perfmon_query_domain_v0 v0;
	} *args = data;
B
Ben Skeggs 已提交
433
	struct nvkm_pm *pm = (void *)object->engine;
434 435 436 437 438 439 440 441 442 443 444 445
	struct nvkm_perfdom *dom;
	u8 domain_nr;
	int di, ret;

	nv_ioctl(object, "perfmon query domain size %d\n", size);
	if (nvif_unpack(args->v0, 0, 0, false)) {
		nv_ioctl(object, "perfmon domain vers %d iter %02x\n",
			 args->v0.version, args->v0.iter);
		di = (args->v0.iter & 0xff) - 1;
	} else
		return ret;

B
Ben Skeggs 已提交
446
	domain_nr = nvkm_pm_count_perfdom(pm);
447 448 449 450
	if (di >= (int)domain_nr)
		return -EINVAL;

	if (di >= 0) {
B
Ben Skeggs 已提交
451
		dom = nvkm_perfdom_find(pm, di);
452 453 454 455 456
		if (dom == NULL)
			return -EINVAL;

		args->v0.id         = di;
		args->v0.signal_nr  = nvkm_perfdom_count_perfsig(dom);
457
		strncpy(args->v0.name, dom->name, sizeof(args->v0.name));
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479

		/* Currently only global counters (PCOUNTER) are implemented
		 * but this will be different for local counters (MP). */
		args->v0.counter_nr = 4;
	}

	if (++di < domain_nr) {
		args->v0.iter = ++di;
		return 0;
	}

	args->v0.iter = 0xff;
	return 0;
}

static int
nvkm_perfmon_mthd_query_signal(struct nvkm_object *object, void *data, u32 size)
{
	union {
		struct nvif_perfmon_query_signal_v0 v0;
	} *args = data;
	struct nvkm_device *device = nv_device(object);
B
Ben Skeggs 已提交
480
	struct nvkm_pm *pm = (void *)object->engine;
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
	struct nvkm_perfdom *dom;
	struct nvkm_perfsig *sig;
	const bool all = nvkm_boolopt(device->cfgopt, "NvPmShowAll", false);
	const bool raw = nvkm_boolopt(device->cfgopt, "NvPmUnnamed", all);
	int ret, si;

	nv_ioctl(object, "perfmon query signal size %d\n", size);
	if (nvif_unpack(args->v0, 0, 0, false)) {
		nv_ioctl(object,
			 "perfmon query signal vers %d dom %d iter %04x\n",
			 args->v0.version, args->v0.domain, args->v0.iter);
		si = (args->v0.iter & 0xffff) - 1;
	} else
		return ret;

B
Ben Skeggs 已提交
496
	dom = nvkm_perfdom_find(pm, args->v0.domain);
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
	if (dom == NULL || si >= (int)dom->signal_nr)
		return -EINVAL;

	if (si >= 0) {
		sig = &dom->signal[si];
		if (raw || !sig->name) {
			snprintf(args->v0.name, sizeof(args->v0.name),
				 "/%s/%02x", dom->name, si);
		} else {
			strncpy(args->v0.name, sig->name,
				sizeof(args->v0.name));
		}

		args->v0.signal = si;
		args->v0.source_nr = nvkm_perfsig_count_perfsrc(sig);
	}

	while (++si < dom->signal_nr) {
		if (all || dom->signal[si].name) {
			args->v0.iter = ++si;
			return 0;
		}
	}

	args->v0.iter = 0xffff;
	return 0;
}

static int
nvkm_perfmon_mthd_query_source(struct nvkm_object *object, void *data, u32 size)
{
	union {
		struct nvif_perfmon_query_source_v0 v0;
	} *args = data;
B
Ben Skeggs 已提交
531
	struct nvkm_pm *pm = (void *)object->engine;
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
	struct nvkm_perfdom *dom = NULL;
	struct nvkm_perfsig *sig;
	struct nvkm_perfsrc *src;
	u8 source_nr = 0;
	int si, ret;

	nv_ioctl(object, "perfmon query source size %d\n", size);
	if (nvif_unpack(args->v0, 0, 0, false)) {
		nv_ioctl(object,
			 "perfmon source vers %d dom %d sig %02x iter %02x\n",
			 args->v0.version, args->v0.domain, args->v0.signal,
			 args->v0.iter);
		si = (args->v0.iter & 0xff) - 1;
	} else
		return ret;

B
Ben Skeggs 已提交
548
	sig = nvkm_perfsig_find(pm, args->v0.domain, args->v0.signal, &dom);
549 550 551 552 553 554 555 556
	if (!sig)
		return -EINVAL;

	source_nr = nvkm_perfsig_count_perfsrc(sig);
	if (si >= (int)source_nr)
		return -EINVAL;

	if (si >= 0) {
B
Ben Skeggs 已提交
557
		src = nvkm_perfsrc_find(pm, sig, sig->source[si]);
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
		if (!src)
			return -EINVAL;

		args->v0.source = sig->source[si];
		args->v0.mask   = src->mask;
		strncpy(args->v0.name, src->name, sizeof(args->v0.name));
	}

	if (++si < source_nr) {
		args->v0.iter = ++si;
		return 0;
	}

	args->v0.iter = 0xff;
	return 0;
}

static int
nvkm_perfmon_mthd(struct nvkm_object *object, u32 mthd, void *data, u32 size)
{
	switch (mthd) {
	case NVIF_PERFMON_V0_QUERY_DOMAIN:
		return nvkm_perfmon_mthd_query_domain(object, data, size);
	case NVIF_PERFMON_V0_QUERY_SIGNAL:
		return nvkm_perfmon_mthd_query_signal(object, data, size);
	case NVIF_PERFMON_V0_QUERY_SOURCE:
		return nvkm_perfmon_mthd_query_source(object, data, size);
	default:
		break;
	}
	return -EINVAL;
}

591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
static struct nvkm_oclass
nvkm_perfmon_sclass[] = {
	{ .handle = NVIF_IOCTL_NEW_V0_PERFDOM,
	  .ofuncs = &nvkm_perfdom_ofuncs,
	},
	{}
};

static int
nvkm_perfmon_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
		  struct nvkm_oclass *oclass, void *data, u32 size,
		  struct nvkm_object **pobject)
{
	struct nvkm_parent *perfmon;
	int ret = nvkm_parent_create(parent, engine, oclass, 0,
				     nvkm_perfmon_sclass, 0, &perfmon);
	*pobject = perfmon ? &perfmon->object : NULL;
	return ret;
}

611 612
static struct nvkm_ofuncs
nvkm_perfmon_ofuncs = {
613 614 615 616
	.ctor = nvkm_perfmon_ctor,
	.dtor = _nvkm_parent_dtor,
	.init = _nvkm_parent_init,
	.fini = _nvkm_parent_fini,
617 618 619
	.mthd = nvkm_perfmon_mthd,
};

620 621
struct nvkm_oclass
nvkm_pm_sclass[] = {
622 623 624 625
	{
	  .handle = NVIF_IOCTL_NEW_V0_PERFMON,
	  .ofuncs = &nvkm_perfmon_ofuncs,
	},
626 627 628 629 630 631 632
	{},
};

/*******************************************************************************
 * PPM context
 ******************************************************************************/
static void
633
nvkm_perfctx_dtor(struct nvkm_object *object)
634
{
B
Ben Skeggs 已提交
635
	struct nvkm_pm *pm = (void *)object->engine;
636 637
	struct nvkm_perfctx *ctx = (void *)object;

B
Ben Skeggs 已提交
638
	mutex_lock(&nv_subdev(pm)->mutex);
639
	nvkm_engctx_destroy(&ctx->base);
B
Ben Skeggs 已提交
640 641 642
	if (pm->context == ctx)
		pm->context = NULL;
	mutex_unlock(&nv_subdev(pm)->mutex);
643 644 645
}

static int
646 647 648
nvkm_perfctx_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
		  struct nvkm_oclass *oclass, void *data, u32 size,
		  struct nvkm_object **pobject)
649
{
B
Ben Skeggs 已提交
650
	struct nvkm_pm *pm = (void *)engine;
651
	struct nvkm_perfctx *ctx;
652 653
	int ret;

654 655 656 657 658 659 660
	/* no context needed for perfdom objects... */
	if (nv_mclass(parent) != NV_DEVICE) {
		atomic_inc(&parent->refcount);
		*pobject = parent;
		return 1;
	}

661
	ret = nvkm_engctx_create(parent, engine, oclass, NULL, 0, 0, 0, &ctx);
662 663 664 665
	*pobject = nv_object(ctx);
	if (ret)
		return ret;

B
Ben Skeggs 已提交
666 667 668 669
	mutex_lock(&nv_subdev(pm)->mutex);
	if (pm->context == NULL)
		pm->context = ctx;
	if (ctx != pm->context)
670
		ret = -EBUSY;
B
Ben Skeggs 已提交
671
	mutex_unlock(&nv_subdev(pm)->mutex);
672

673
	return ret;
674 675
}

676 677
struct nvkm_oclass
nvkm_pm_cclass = {
678
	.handle = NV_ENGCTX(PM, 0x00),
679 680 681 682 683
	.ofuncs = &(struct nvkm_ofuncs) {
		.ctor = nvkm_perfctx_ctor,
		.dtor = nvkm_perfctx_dtor,
		.init = _nvkm_engctx_init,
		.fini = _nvkm_engctx_fini,
684 685 686 687 688 689
	},
};

/*******************************************************************************
 * PPM engine/subdev functions
 ******************************************************************************/
690
int
B
Ben Skeggs 已提交
691
nvkm_perfsrc_new(struct nvkm_pm *pm, struct nvkm_perfsig *sig,
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
		 const struct nvkm_specsrc *spec)
{
	const struct nvkm_specsrc *ssrc;
	const struct nvkm_specmux *smux;
	struct nvkm_perfsrc *src;
	u8 source_nr = 0;

	if (!spec) {
		/* No sources are defined for this signal. */
		return 0;
	}

	ssrc = spec;
	while (ssrc->name) {
		smux = ssrc->mux;
		while (smux->name) {
			bool found = false;
			u8 source_id = 0;
			u32 len;

B
Ben Skeggs 已提交
712
			list_for_each_entry(src, &pm->sources, head) {
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
				if (src->addr == ssrc->addr &&
				    src->shift == smux->shift) {
					found = true;
					break;
				}
				source_id++;
			}

			if (!found) {
				src = kzalloc(sizeof(*src), GFP_KERNEL);
				if (!src)
					return -ENOMEM;

				src->addr   = ssrc->addr;
				src->mask   = smux->mask;
				src->shift  = smux->shift;
				src->enable = smux->enable;

				len = strlen(ssrc->name) +
				      strlen(smux->name) + 2;
				src->name = kzalloc(len, GFP_KERNEL);
B
Ben Skeggs 已提交
734 735
				if (!src->name) {
					kfree(src);
736
					return -ENOMEM;
B
Ben Skeggs 已提交
737
				}
738 739 740
				snprintf(src->name, len, "%s_%s", ssrc->name,
					 smux->name);

B
Ben Skeggs 已提交
741
				list_add_tail(&src->head, &pm->sources);
742 743 744 745 746 747 748 749 750 751 752
			}

			sig->source[source_nr++] = source_id + 1;
			smux++;
		}
		ssrc++;
	}

	return 0;
}

753
int
B
Ben Skeggs 已提交
754
nvkm_perfdom_new(struct nvkm_pm *pm, const char *name, u32 mask,
755 756
		 u32 base, u32 size_unit, u32 size_domain,
		 const struct nvkm_specdom *spec)
757
{
758 759 760
	const struct nvkm_specdom *sdom;
	const struct nvkm_specsig *ssig;
	struct nvkm_perfdom *dom;
761
	int ret, i;
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783

	for (i = 0; i == 0 || mask; i++) {
		u32 addr = base + (i * size_unit);
		if (i && !(mask & (1 << i)))
			continue;

		sdom = spec;
		while (sdom->signal_nr) {
			dom = kzalloc(sizeof(*dom) + sdom->signal_nr *
				      sizeof(*dom->signal), GFP_KERNEL);
			if (!dom)
				return -ENOMEM;

			if (mask) {
				snprintf(dom->name, sizeof(dom->name),
					 "%s/%02x/%02x", name, i,
					 (int)(sdom - spec));
			} else {
				snprintf(dom->name, sizeof(dom->name),
					 "%s/%02x", name, (int)(sdom - spec));
			}

B
Ben Skeggs 已提交
784
			list_add_tail(&dom->head, &pm->domains);
785 786 787 788 789 790 791
			INIT_LIST_HEAD(&dom->list);
			dom->func = sdom->func;
			dom->addr = addr;
			dom->signal_nr = sdom->signal_nr;

			ssig = (sdom++)->signal;
			while (ssig->name) {
792 793 794
				struct nvkm_perfsig *sig =
					&dom->signal[ssig->signal];
				sig->name = ssig->name;
B
Ben Skeggs 已提交
795
				ret = nvkm_perfsrc_new(pm, sig, ssig->source);
796 797
				if (ret)
					return ret;
798 799 800 801 802 803 804 805 806 807 808 809 810
				ssig++;
			}

			addr += size_domain;
		}

		mask &= ~(1 << i);
	}

	return 0;
}

int
811
_nvkm_pm_fini(struct nvkm_object *object, bool suspend)
812
{
B
Ben Skeggs 已提交
813 814
	struct nvkm_pm *pm = (void *)object;
	return nvkm_engine_fini(&pm->engine, suspend);
815 816 817
}

int
818
_nvkm_pm_init(struct nvkm_object *object)
819
{
B
Ben Skeggs 已提交
820 821
	struct nvkm_pm *pm = (void *)object;
	return nvkm_engine_init(&pm->engine);
822 823 824
}

void
825
_nvkm_pm_dtor(struct nvkm_object *object)
826
{
B
Ben Skeggs 已提交
827
	struct nvkm_pm *pm = (void *)object;
828 829
	struct nvkm_perfdom *dom, *next_dom;
	struct nvkm_perfsrc *src, *next_src;
830

B
Ben Skeggs 已提交
831
	list_for_each_entry_safe(dom, next_dom, &pm->domains, head) {
832 833 834 835
		list_del(&dom->head);
		kfree(dom);
	}

B
Ben Skeggs 已提交
836
	list_for_each_entry_safe(src, next_src, &pm->sources, head) {
837 838 839 840 841
		list_del(&src->head);
		kfree(src->name);
		kfree(src);
	}

B
Ben Skeggs 已提交
842
	nvkm_engine_destroy(&pm->engine);
843 844 845
}

int
846 847
nvkm_pm_create_(struct nvkm_object *parent, struct nvkm_object *engine,
		struct nvkm_oclass *oclass, int length, void **pobject)
848
{
B
Ben Skeggs 已提交
849
	struct nvkm_pm *pm;
850 851
	int ret;

852 853
	ret = nvkm_engine_create_(parent, engine, oclass, true, "PPM",
				  "pm", length, pobject);
B
Ben Skeggs 已提交
854
	pm = *pobject;
855 856 857
	if (ret)
		return ret;

B
Ben Skeggs 已提交
858 859
	INIT_LIST_HEAD(&pm->domains);
	INIT_LIST_HEAD(&pm->sources);
860 861
	return 0;
}