1/* $NetBSD: nouveau_engine_perfmon_base.c,v 1.3 2016/02/05 23:45:44 riastradh Exp $ */
2
3/*
4 * Copyright 2013 Red Hat Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Ben Skeggs
25 */
26
27#include <sys/cdefs.h>
28__KERNEL_RCSID(0, "$NetBSD: nouveau_engine_perfmon_base.c,v 1.3 2016/02/05 23:45:44 riastradh Exp $");
29
30#include <core/option.h>
31#include <core/class.h>
32
33#include <subdev/clock.h>
34
35#include "priv.h"
36
37#define QUAD_MASK 0x0f
38#define QUAD_FREE 0x01
39
40static struct nouveau_perfsig *
41nouveau_perfsig_find_(struct nouveau_perfdom *dom, const char *name, u32 size)
42{
43 char path[64];
44 int i;
45
46 if (name[0] != '/') {
47 for (i = 0; i < dom->signal_nr; i++) {
48 if ( dom->signal[i].name &&
49 !strncmp(name, dom->signal[i].name, size))
50 return &dom->signal[i];
51 }
52 } else {
53 for (i = 0; i < dom->signal_nr; i++) {
54 snprintf(path, sizeof(path), "/%s/%02x", dom->name, i);
55 if (!strncmp(name, path, size))
56 return &dom->signal[i];
57 }
58 }
59
60 return NULL;
61}
62
63static struct nouveau_perfsig *
64nouveau_perfsig_find(struct nouveau_perfmon *ppm, const char *name, u32 size,
65 struct nouveau_perfdom **pdom)
66{
67 struct nouveau_perfdom *dom = *pdom;
68 struct nouveau_perfsig *sig;
69
70 if (dom == NULL) {
71 list_for_each_entry(dom, &ppm->domains, head) {
72 sig = nouveau_perfsig_find_(dom, name, size);
73 if (sig) {
74 *pdom = dom;
75 return sig;
76 }
77 }
78
79 return NULL;
80 }
81
82 return nouveau_perfsig_find_(dom, name, size);
83}
84
85struct nouveau_perfctr *
86nouveau_perfsig_wrap(struct nouveau_perfmon *ppm, const char *name,
87 struct nouveau_perfdom **pdom)
88{
89 struct nouveau_perfsig *sig;
90 struct nouveau_perfctr *ctr;
91
92 sig = nouveau_perfsig_find(ppm, name, strlen(name), pdom);
93 if (!sig)
94 return NULL;
95
96 ctr = kzalloc(sizeof(*ctr), GFP_KERNEL);
97 if (ctr) {
98 ctr->signal[0] = sig;
99 ctr->logic_op = 0xaaaa;
100 }
101
102 return ctr;
103}
104
105/*******************************************************************************
106 * Perfmon object classes
107 ******************************************************************************/
108static int
109nouveau_perfctr_query(struct nouveau_object *object, u32 mthd,
110 void *data, u32 size)
111{
112 struct nouveau_device *device = nv_device(object);
113 struct nouveau_perfmon *ppm = (void *)object->engine;
114 struct nouveau_perfdom *dom = NULL, *chk;
115 struct nv_perfctr_query *args = data;
116 const bool all = nouveau_boolopt(device->cfgopt, "NvPmShowAll", false);
117 const bool raw = nouveau_boolopt(device->cfgopt, "NvPmUnnamed", all);
118 const char *name;
119 int tmp = 0, di, si;
120 char path[64];
121
122 if (size < sizeof(*args))
123 return -EINVAL;
124
125 di = (args->iter & 0xff000000) >> 24;
126 si = (args->iter & 0x00ffffff) - 1;
127
128 list_for_each_entry(chk, &ppm->domains, head) {
129 if (tmp++ == di) {
130 dom = chk;
131 break;
132 }
133 }
134
135 if (dom == NULL || si >= (int)dom->signal_nr)
136 return -EINVAL;
137
138 if (si >= 0) {
139 if (raw || !(name = dom->signal[si].name)) {
140 snprintf(path, sizeof(path), "/%s/%02x", dom->name, si);
141 name = path;
142 }
143
144 if (args->name)
145 strncpy(args->name, name, args->size);
146 args->size = strlen(name) + 1;
147 }
148
149 do {
150 while (++si < dom->signal_nr) {
151 if (all || dom->signal[si].name) {
152 args->iter = (di << 24) | ++si;
153 return 0;
154 }
155 }
156 si = -1;
157 di = di + 1;
158 dom = list_entry(dom->head.next, typeof(*dom), head);
159 } while (&dom->head != &ppm->domains);
160
161 args->iter = 0xffffffff;
162 return 0;
163}
164
165static int
166nouveau_perfctr_sample(struct nouveau_object *object, u32 mthd,
167 void *data, u32 size)
168{
169 struct nouveau_perfmon *ppm = (void *)object->engine;
170 struct nouveau_perfctr *ctr, *tmp;
171 struct nouveau_perfdom *dom;
172 struct nv_perfctr_sample *args = data;
173
174#if 1
175 CTASSERT(sizeof(*args) == 0);
176#else
177 if (size < sizeof(*args))
178 return -EINVAL;
179#endif
180 ppm->sequence++;
181
182 list_for_each_entry(dom, &ppm->domains, head) {
183 /* sample previous batch of counters */
184 if (dom->quad != QUAD_MASK) {
185 dom->func->next(ppm, dom);
186 tmp = NULL;
187 while (!list_empty(&dom->list)) {
188 ctr = list_first_entry(&dom->list,
189 typeof(*ctr), head);
190 if (ctr->slot < 0) break;
191 if ( tmp && tmp == ctr) break;
192 if (!tmp) tmp = ctr;
193 dom->func->read(ppm, dom, ctr);
194 ctr->slot = -1;
195 list_move_tail(&ctr->head, &dom->list);
196 }
197 }
198
199 dom->quad = QUAD_MASK;
200
201 /* setup next batch of counters for sampling */
202 list_for_each_entry(ctr, &dom->list, head) {
203 ctr->slot = ffs(dom->quad) - 1;
204 if (ctr->slot < 0)
205 break;
206 dom->quad &= ~(QUAD_FREE << ctr->slot);
207 dom->func->init(ppm, dom, ctr);
208 }
209
210 if (dom->quad != QUAD_MASK)
211 dom->func->next(ppm, dom);
212 }
213
214 return 0;
215}
216
217static int
218nouveau_perfctr_read(struct nouveau_object *object, u32 mthd,
219 void *data, u32 size)
220{
221 struct nouveau_perfctr *ctr = (void *)object;
222 struct nv_perfctr_read *args = data;
223
224 if (size < sizeof(*args))
225 return -EINVAL;
226 if (!ctr->clk)
227 return -EAGAIN;
228
229 args->clk = ctr->clk;
230 args->ctr = ctr->ctr;
231 return 0;
232}
233
234static void
235nouveau_perfctr_dtor(struct nouveau_object *object)
236{
237 struct nouveau_perfctr *ctr = (void *)object;
238 if (ctr->head.next)
239 list_del(&ctr->head);
240 nouveau_object_destroy(&ctr->base);
241}
242
243static int
244nouveau_perfctr_ctor(struct nouveau_object *parent,
245 struct nouveau_object *engine,
246 struct nouveau_oclass *oclass, void *data, u32 size,
247 struct nouveau_object **pobject)
248{
249 struct nouveau_perfmon *ppm = (void *)engine;
250 struct nouveau_perfdom *dom = NULL;
251 struct nouveau_perfsig *sig[4] = {};
252 struct nouveau_perfctr *ctr;
253 struct nv_perfctr_class *args = data;
254 int ret, i;
255
256 if (size < sizeof(*args))
257 return -EINVAL;
258
259 for (i = 0; i < ARRAY_SIZE(args->signal) && args->signal[i].name; i++) {
260 sig[i] = nouveau_perfsig_find(ppm, args->signal[i].name,
261 args->signal[i].size, &dom);
262 if (!sig[i])
263 return -EINVAL;
264 }
265
266 ret = nouveau_object_create(parent, engine, oclass, 0, &ctr);
267 *pobject = nv_object(ctr);
268 if (ret)
269 return ret;
270
271 ctr->slot = -1;
272 ctr->logic_op = args->logic_op;
273 ctr->signal[0] = sig[0];
274 ctr->signal[1] = sig[1];
275 ctr->signal[2] = sig[2];
276 ctr->signal[3] = sig[3];
277 if (dom)
278 list_add_tail(&ctr->head, &dom->list);
279 return 0;
280}
281
282static struct nouveau_ofuncs
283nouveau_perfctr_ofuncs = {
284 .ctor = nouveau_perfctr_ctor,
285 .dtor = nouveau_perfctr_dtor,
286 .init = nouveau_object_init,
287 .fini = nouveau_object_fini,
288};
289
290static struct nouveau_omthds
291nouveau_perfctr_omthds[] = {
292 { NV_PERFCTR_QUERY, NV_PERFCTR_QUERY, nouveau_perfctr_query },
293 { NV_PERFCTR_SAMPLE, NV_PERFCTR_SAMPLE, nouveau_perfctr_sample },
294 { NV_PERFCTR_READ, NV_PERFCTR_READ, nouveau_perfctr_read },
295 {}
296};
297
298struct nouveau_oclass
299nouveau_perfmon_sclass[] = {
300 { .handle = NV_PERFCTR_CLASS,
301 .ofuncs = &nouveau_perfctr_ofuncs,
302 .omthds = nouveau_perfctr_omthds,
303 },
304 {},
305};
306
307/*******************************************************************************
308 * PPM context
309 ******************************************************************************/
310static void
311nouveau_perfctx_dtor(struct nouveau_object *object)
312{
313 struct nouveau_perfmon *ppm = (void *)object->engine;
314 mutex_lock(&nv_subdev(ppm)->mutex);
315 ppm->context = NULL;
316 mutex_unlock(&nv_subdev(ppm)->mutex);
317}
318
319static int
320nouveau_perfctx_ctor(struct nouveau_object *parent,
321 struct nouveau_object *engine,
322 struct nouveau_oclass *oclass, void *data, u32 size,
323 struct nouveau_object **pobject)
324{
325 struct nouveau_perfmon *ppm = (void *)engine;
326 struct nouveau_perfctx *ctx;
327 int ret;
328
329 ret = nouveau_engctx_create(parent, engine, oclass, NULL,
330 0, 0, 0, &ctx);
331 *pobject = nv_object(ctx);
332 if (ret)
333 return ret;
334
335 mutex_lock(&nv_subdev(ppm)->mutex);
336 if (ppm->context == NULL)
337 ppm->context = ctx;
338 mutex_unlock(&nv_subdev(ppm)->mutex);
339
340 if (ctx != ppm->context)
341 return -EBUSY;
342
343 return 0;
344}
345
346struct nouveau_oclass
347nouveau_perfmon_cclass = {
348 .handle = NV_ENGCTX(PERFMON, 0x00),
349 .ofuncs = &(struct nouveau_ofuncs) {
350 .ctor = nouveau_perfctx_ctor,
351 .dtor = nouveau_perfctx_dtor,
352 .init = _nouveau_engctx_init,
353 .fini = _nouveau_engctx_fini,
354 },
355};
356
357/*******************************************************************************
358 * PPM engine/subdev functions
359 ******************************************************************************/
360int
361nouveau_perfdom_new(struct nouveau_perfmon *ppm, const char *name, u32 mask,
362 u32 base, u32 size_unit, u32 size_domain,
363 const struct nouveau_specdom *spec)
364{
365 const struct nouveau_specdom *sdom;
366 const struct nouveau_specsig *ssig;
367 struct nouveau_perfdom *dom;
368 int i;
369
370 for (i = 0; i == 0 || mask; i++) {
371 u32 addr = base + (i * size_unit);
372 if (i && !(mask & (1 << i)))
373 continue;
374
375 sdom = spec;
376 while (sdom->signal_nr) {
377 dom = kzalloc(sizeof(*dom) + sdom->signal_nr *
378 sizeof(*dom->signal), GFP_KERNEL);
379 if (!dom)
380 return -ENOMEM;
381
382 if (mask) {
383 snprintf(dom->name, sizeof(dom->name),
384 "%s/%02x/%02x", name, i,
385 (int)(sdom - spec));
386 } else {
387 snprintf(dom->name, sizeof(dom->name),
388 "%s/%02x", name, (int)(sdom - spec));
389 }
390
391 list_add_tail(&dom->head, &ppm->domains);
392 INIT_LIST_HEAD(&dom->list);
393 dom->func = sdom->func;
394 dom->addr = addr;
395 dom->quad = QUAD_MASK;
396 dom->signal_nr = sdom->signal_nr;
397
398 ssig = (sdom++)->signal;
399 while (ssig->name) {
400 dom->signal[ssig->signal].name = ssig->name;
401 ssig++;
402 }
403
404 addr += size_domain;
405 }
406
407 mask &= ~(1 << i);
408 }
409
410 return 0;
411}
412
413int
414_nouveau_perfmon_fini(struct nouveau_object *object, bool suspend)
415{
416 struct nouveau_perfmon *ppm = (void *)object;
417 return nouveau_engine_fini(&ppm->base, suspend);
418}
419
420int
421_nouveau_perfmon_init(struct nouveau_object *object)
422{
423 struct nouveau_perfmon *ppm = (void *)object;
424 return nouveau_engine_init(&ppm->base);
425}
426
427void
428_nouveau_perfmon_dtor(struct nouveau_object *object)
429{
430 struct nouveau_perfmon *ppm = (void *)object;
431 struct nouveau_perfdom *dom, *tmp;
432
433 list_for_each_entry_safe(dom, tmp, &ppm->domains, head) {
434 list_del(&dom->head);
435 kfree(dom);
436 }
437
438 nouveau_engine_destroy(&ppm->base);
439}
440
441int
442nouveau_perfmon_create_(struct nouveau_object *parent,
443 struct nouveau_object *engine,
444 struct nouveau_oclass *oclass,
445 int length, void **pobject)
446{
447 struct nouveau_perfmon *ppm;
448 int ret;
449
450 ret = nouveau_engine_create_(parent, engine, oclass, true, "PPM",
451 "perfmon", length, pobject);
452 ppm = *pobject;
453 if (ret)
454 return ret;
455
456 INIT_LIST_HEAD(&ppm->domains);
457 return 0;
458}
459