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 | |
40 | static struct nouveau_perfsig * |
41 | nouveau_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 | |
63 | static struct nouveau_perfsig * |
64 | nouveau_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 | |
85 | struct nouveau_perfctr * |
86 | nouveau_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 | ******************************************************************************/ |
108 | static int |
109 | nouveau_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 | |
165 | static int |
166 | nouveau_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 | |
217 | static int |
218 | nouveau_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 | |
234 | static void |
235 | nouveau_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 | |
243 | static int |
244 | nouveau_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 | |
282 | static struct nouveau_ofuncs |
283 | nouveau_perfctr_ofuncs = { |
284 | .ctor = nouveau_perfctr_ctor, |
285 | .dtor = nouveau_perfctr_dtor, |
286 | .init = nouveau_object_init, |
287 | .fini = nouveau_object_fini, |
288 | }; |
289 | |
290 | static struct nouveau_omthds |
291 | nouveau_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 | |
298 | struct nouveau_oclass |
299 | nouveau_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 | ******************************************************************************/ |
310 | static void |
311 | nouveau_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 | |
319 | static int |
320 | nouveau_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 | |
346 | struct nouveau_oclass |
347 | nouveau_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 | ******************************************************************************/ |
360 | int |
361 | nouveau_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 | |
413 | int |
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 | |
420 | int |
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 | |
427 | void |
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 | |
441 | int |
442 | nouveau_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 | |