base.c 13.1 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/device.h>
28
#include <core/option.h>
29

30 31
#include <nvif/class.h>
#include <nvif/ioctl.h>
32
#include <nvif/unpack.h>
33 34 35 36

#define QUAD_MASK 0x0f
#define QUAD_FREE 0x01

37 38 39 40 41 42 43 44 45 46 47
static u8
nvkm_pm_count_perfdom(struct nvkm_pm *ppm)
{
	struct nvkm_perfdom *dom;
	u8 domain_nr = 0;

	list_for_each_entry(dom, &ppm->domains, head)
		domain_nr++;
	return domain_nr;
}

48
static u16
49 50
nvkm_perfdom_count_perfsig(struct nvkm_perfdom *dom)
{
51
	u16 signal_nr = 0;
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
	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 *
nvkm_perfdom_find(struct nvkm_pm *ppm, int di)
{
	struct nvkm_perfdom *dom;
	int tmp = 0;

	list_for_each_entry(dom, &ppm->domains, head) {
		if (tmp++ == di)
			return dom;
	}
	return NULL;
}

76 77
static struct nvkm_perfsig *
nvkm_perfsig_find_(struct nvkm_perfdom *dom, const char *name, u32 size)
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
{
	char path[64];
	int i;

	if (name[0] != '/') {
		for (i = 0; i < dom->signal_nr; i++) {
			if ( dom->signal[i].name &&
			    !strncmp(name, dom->signal[i].name, size))
				return &dom->signal[i];
		}
	} else {
		for (i = 0; i < dom->signal_nr; i++) {
			snprintf(path, sizeof(path), "/%s/%02x", dom->name, i);
			if (!strncmp(name, path, size))
				return &dom->signal[i];
		}
	}

	return NULL;
}

99 100 101
struct nvkm_perfsig *
nvkm_perfsig_find(struct nvkm_pm *ppm, const char *name, u32 size,
		  struct nvkm_perfdom **pdom)
102
{
103 104
	struct nvkm_perfdom *dom = *pdom;
	struct nvkm_perfsig *sig;
105 106 107

	if (dom == NULL) {
		list_for_each_entry(dom, &ppm->domains, head) {
108
			sig = nvkm_perfsig_find_(dom, name, size);
109 110 111 112 113 114 115 116 117
			if (sig) {
				*pdom = dom;
				return sig;
			}
		}

		return NULL;
	}

118
	return nvkm_perfsig_find_(dom, name, size);
119 120 121 122 123
}

/*******************************************************************************
 * Perfmon object classes
 ******************************************************************************/
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
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;
	struct nvkm_pm *ppm = (void *)object->engine;
	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;

	domain_nr = nvkm_pm_count_perfdom(ppm);
	if (di >= (int)domain_nr)
		return -EINVAL;

	if (di >= 0) {
		dom = nvkm_perfdom_find(ppm, di);
		if (dom == NULL)
			return -EINVAL;

		args->v0.id         = di;
		args->v0.signal_nr  = nvkm_perfdom_count_perfsig(dom);

		/* 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;
}

169
static int
170
nvkm_perfmon_mthd_query_signal(struct nvkm_object *object, void *data, u32 size)
171
{
172
	union {
173
		struct nvif_perfmon_query_signal_v0 v0;
174
	} *args = data;
175 176
	struct nvkm_device *device = nv_device(object);
	struct nvkm_pm *ppm = (void *)object->engine;
177
	struct nvkm_perfdom *dom;
178 179
	const bool all = nvkm_boolopt(device->cfgopt, "NvPmShowAll", false);
	const bool raw = nvkm_boolopt(device->cfgopt, "NvPmUnnamed", all);
180
	const char *name;
181
	int ret, si;
182

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

192
	dom = nvkm_perfdom_find(ppm, args->v0.domain);
193 194 195 196 197
	if (dom == NULL || si >= (int)dom->signal_nr)
		return -EINVAL;

	if (si >= 0) {
		if (raw || !(name = dom->signal[si].name)) {
198 199 200 201
			snprintf(args->v0.name, sizeof(args->v0.name),
				 "/%s/%02x", dom->name, si);
		} else {
			strncpy(args->v0.name, name, sizeof(args->v0.name));
202 203 204
		}
	}

205 206 207 208
	while (++si < dom->signal_nr) {
		if (all || dom->signal[si].name) {
			args->v0.iter = ++si;
			return 0;
209
		}
210
	}
211

212
	args->v0.iter = 0xffff;
213 214 215
	return 0;
}

216 217 218 219
static int
nvkm_perfmon_mthd(struct nvkm_object *object, u32 mthd, void *data, u32 size)
{
	switch (mthd) {
220 221
	case NVIF_PERFMON_V0_QUERY_DOMAIN:
		return nvkm_perfmon_mthd_query_domain(object, data, size);
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
	case NVIF_PERFMON_V0_QUERY_SIGNAL:
		return nvkm_perfmon_mthd_query_signal(object, data, size);
	default:
		break;
	}
	return -EINVAL;
}

static struct nvkm_ofuncs
nvkm_perfmon_ofuncs = {
	.ctor = _nvkm_object_ctor,
	.dtor = nvkm_object_destroy,
	.init = nvkm_object_init,
	.fini = nvkm_object_fini,
	.mthd = nvkm_perfmon_mthd,
};

/*******************************************************************************
 * Perfctr object classes
 ******************************************************************************/
242
static int
243
nvkm_perfctr_sample(struct nvkm_object *object, void *data, u32 size)
244
{
245 246 247
	union {
		struct nvif_perfctr_sample none;
	} *args = data;
248 249 250
	struct nvkm_pm *ppm = (void *)object->engine;
	struct nvkm_perfctr *ctr, *tmp;
	struct nvkm_perfdom *dom;
251
	int ret;
252

253 254 255 256 257
	nv_ioctl(object, "perfctr sample size %d\n", size);
	if (nvif_unvers(args->none)) {
		nv_ioctl(object, "perfctr sample\n");
	} else
		return ret;
258 259 260 261 262 263 264 265 266
	ppm->sequence++;

	list_for_each_entry(dom, &ppm->domains, head) {
		/* sample previous batch of counters */
		if (dom->quad != QUAD_MASK) {
			dom->func->next(ppm, dom);
			tmp = NULL;
			while (!list_empty(&dom->list)) {
				ctr = list_first_entry(&dom->list,
267
						       typeof(*ctr), head);
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
				if (ctr->slot < 0) break;
				if ( tmp && tmp == ctr) break;
				if (!tmp) tmp = ctr;
				dom->func->read(ppm, dom, ctr);
				ctr->slot  = -1;
				list_move_tail(&ctr->head, &dom->list);
			}
		}

		dom->quad = QUAD_MASK;

		/* setup next batch of counters for sampling */
		list_for_each_entry(ctr, &dom->list, head) {
			ctr->slot = ffs(dom->quad) - 1;
			if (ctr->slot < 0)
				break;
			dom->quad &= ~(QUAD_FREE << ctr->slot);
			dom->func->init(ppm, dom, ctr);
		}

		if (dom->quad != QUAD_MASK)
			dom->func->next(ppm, dom);
	}

	return 0;
}

static int
296
nvkm_perfctr_read(struct nvkm_object *object, void *data, u32 size)
297
{
298 299 300
	union {
		struct nvif_perfctr_read_v0 v0;
	} *args = data;
301
	struct nvkm_perfctr *ctr = (void *)object;
302 303 304 305 306 307 308
	int ret;

	nv_ioctl(object, "perfctr read size %d\n", size);
	if (nvif_unpack(args->v0, 0, 0, false)) {
		nv_ioctl(object, "perfctr read vers %d\n", args->v0.version);
	} else
		return ret;
309 310 311 312

	if (!ctr->clk)
		return -EAGAIN;

313 314
	args->v0.clk = ctr->clk;
	args->v0.ctr = ctr->ctr;
315 316 317
	return 0;
}

318
static int
319
nvkm_perfctr_mthd(struct nvkm_object *object, u32 mthd, void *data, u32 size)
320 321 322
{
	switch (mthd) {
	case NVIF_PERFCTR_V0_SAMPLE:
323
		return nvkm_perfctr_sample(object, data, size);
324
	case NVIF_PERFCTR_V0_READ:
325
		return nvkm_perfctr_read(object, data, size);
326 327 328 329 330 331
	default:
		break;
	}
	return -EINVAL;
}

332
static void
333
nvkm_perfctr_dtor(struct nvkm_object *object)
334
{
335
	struct nvkm_perfctr *ctr = (void *)object;
336 337
	if (ctr->head.next)
		list_del(&ctr->head);
338
	nvkm_object_destroy(&ctr->base);
339 340 341
}

static int
342 343 344
nvkm_perfctr_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
		  struct nvkm_oclass *oclass, void *data, u32 size,
		  struct nvkm_object **pobject)
345
{
346 347 348
	union {
		struct nvif_perfctr_v0 v0;
	} *args = data;
349 350 351 352
	struct nvkm_pm *ppm = (void *)engine;
	struct nvkm_perfdom *dom = NULL;
	struct nvkm_perfsig *sig[4] = {};
	struct nvkm_perfctr *ctr;
353 354
	int ret, i;

355 356 357 358 359 360
	nv_ioctl(parent, "create perfctr size %d\n", size);
	if (nvif_unpack(args->v0, 0, 0, false)) {
		nv_ioctl(parent, "create perfctr vers %d logic_op %04x\n",
			 args->v0.version, args->v0.logic_op);
	} else
		return ret;
361

362
	for (i = 0; i < ARRAY_SIZE(args->v0.name) && args->v0.name[i][0]; i++) {
363 364 365 366
		sig[i] = nvkm_perfsig_find(ppm, args->v0.name[i],
					   strnlen(args->v0.name[i],
						   sizeof(args->v0.name[i])),
					   &dom);
367 368 369 370
		if (!sig[i])
			return -EINVAL;
	}

371 372 373
	if (!dom)
		return -EINVAL;

374
	ret = nvkm_object_create(parent, engine, oclass, 0, &ctr);
375 376 377 378 379
	*pobject = nv_object(ctr);
	if (ret)
		return ret;

	ctr->slot = -1;
380
	ctr->logic_op = args->v0.logic_op;
381 382 383 384
	ctr->signal[0] = sig[0];
	ctr->signal[1] = sig[1];
	ctr->signal[2] = sig[2];
	ctr->signal[3] = sig[3];
385
	list_add_tail(&ctr->head, &dom->list);
386 387 388
	return 0;
}

389 390 391 392 393 394 395
static struct nvkm_ofuncs
nvkm_perfctr_ofuncs = {
	.ctor = nvkm_perfctr_ctor,
	.dtor = nvkm_perfctr_dtor,
	.init = nvkm_object_init,
	.fini = nvkm_object_fini,
	.mthd = nvkm_perfctr_mthd,
396 397
};

398 399
struct nvkm_oclass
nvkm_pm_sclass[] = {
400 401 402 403
	{
	  .handle = NVIF_IOCTL_NEW_V0_PERFMON,
	  .ofuncs = &nvkm_perfmon_ofuncs,
	},
404
	{ .handle = NVIF_IOCTL_NEW_V0_PERFCTR,
405
	  .ofuncs = &nvkm_perfctr_ofuncs,
406 407 408 409 410 411 412 413
	},
	{},
};

/*******************************************************************************
 * PPM context
 ******************************************************************************/
static void
414
nvkm_perfctx_dtor(struct nvkm_object *object)
415
{
416
	struct nvkm_pm *ppm = (void *)object->engine;
417 418
	struct nvkm_perfctx *ctx = (void *)object;

419
	mutex_lock(&nv_subdev(ppm)->mutex);
420 421 422
	nvkm_engctx_destroy(&ctx->base);
	if (ppm->context == ctx)
		ppm->context = NULL;
423 424 425 426
	mutex_unlock(&nv_subdev(ppm)->mutex);
}

static int
427 428 429
nvkm_perfctx_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
		  struct nvkm_oclass *oclass, void *data, u32 size,
		  struct nvkm_object **pobject)
430
{
431 432
	struct nvkm_pm *ppm = (void *)engine;
	struct nvkm_perfctx *ctx;
433 434
	int ret;

435
	ret = nvkm_engctx_create(parent, engine, oclass, NULL, 0, 0, 0, &ctx);
436 437 438 439 440 441 442 443
	*pobject = nv_object(ctx);
	if (ret)
		return ret;

	mutex_lock(&nv_subdev(ppm)->mutex);
	if (ppm->context == NULL)
		ppm->context = ctx;
	if (ctx != ppm->context)
444 445
		ret = -EBUSY;
	mutex_unlock(&nv_subdev(ppm)->mutex);
446

447
	return ret;
448 449
}

450 451
struct nvkm_oclass
nvkm_pm_cclass = {
452
	.handle = NV_ENGCTX(PM, 0x00),
453 454 455 456 457
	.ofuncs = &(struct nvkm_ofuncs) {
		.ctor = nvkm_perfctx_ctor,
		.dtor = nvkm_perfctx_dtor,
		.init = _nvkm_engctx_init,
		.fini = _nvkm_engctx_fini,
458 459 460 461 462 463 464
	},
};

/*******************************************************************************
 * PPM engine/subdev functions
 ******************************************************************************/
int
465 466 467
nvkm_perfdom_new(struct nvkm_pm *ppm, const char *name, u32 mask,
		 u32 base, u32 size_unit, u32 size_domain,
		 const struct nvkm_specdom *spec)
468
{
469 470 471
	const struct nvkm_specdom *sdom;
	const struct nvkm_specsig *ssig;
	struct nvkm_perfdom *dom;
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
	int i;

	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));
			}

			list_add_tail(&dom->head, &ppm->domains);
			INIT_LIST_HEAD(&dom->list);
			dom->func = sdom->func;
			dom->addr = addr;
			dom->quad = QUAD_MASK;
			dom->signal_nr = sdom->signal_nr;

			ssig = (sdom++)->signal;
			while (ssig->name) {
				dom->signal[ssig->signal].name = ssig->name;
				ssig++;
			}

			addr += size_domain;
		}

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

	return 0;
}

int
518
_nvkm_pm_fini(struct nvkm_object *object, bool suspend)
519
{
520 521
	struct nvkm_pm *ppm = (void *)object;
	return nvkm_engine_fini(&ppm->base, suspend);
522 523 524
}

int
525
_nvkm_pm_init(struct nvkm_object *object)
526
{
527 528
	struct nvkm_pm *ppm = (void *)object;
	return nvkm_engine_init(&ppm->base);
529 530 531
}

void
532
_nvkm_pm_dtor(struct nvkm_object *object)
533
{
534 535
	struct nvkm_pm *ppm = (void *)object;
	struct nvkm_perfdom *dom, *tmp;
536 537 538 539 540 541

	list_for_each_entry_safe(dom, tmp, &ppm->domains, head) {
		list_del(&dom->head);
		kfree(dom);
	}

542
	nvkm_engine_destroy(&ppm->base);
543 544 545
}

int
546 547
nvkm_pm_create_(struct nvkm_object *parent, struct nvkm_object *engine,
		struct nvkm_oclass *oclass, int length, void **pobject)
548
{
549
	struct nvkm_pm *ppm;
550 551
	int ret;

552 553
	ret = nvkm_engine_create_(parent, engine, oclass, true, "PPM",
				  "pm", length, pobject);
554 555 556 557 558 559 560
	ppm = *pobject;
	if (ret)
		return ret;

	INIT_LIST_HEAD(&ppm->domains);
	return 0;
}