blob: 6a428e7218d2b9ee4272fd12725d13e6e8e74568 [file] [log] [blame]
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +00001/*
2 * Copyright © 2017 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
24
25#include <linux/perf_event.h>
26#include <linux/pm_runtime.h>
27
28#include "i915_drv.h"
29#include "i915_pmu.h"
30#include "intel_ringbuffer.h"
31
32/* Frequency for the sampling timer for events which need it. */
33#define FREQUENCY 200
34#define PERIOD max_t(u64, 10000, NSEC_PER_SEC / FREQUENCY)
35
36#define ENGINE_SAMPLE_MASK \
37 (BIT(I915_SAMPLE_BUSY) | \
38 BIT(I915_SAMPLE_WAIT) | \
39 BIT(I915_SAMPLE_SEMA))
40
41#define ENGINE_SAMPLE_BITS (1 << I915_PMU_SAMPLE_BITS)
42
43static cpumask_t i915_pmu_cpumask = CPU_MASK_NONE;
44
45static u8 engine_config_sample(u64 config)
46{
47 return config & I915_PMU_SAMPLE_MASK;
48}
49
50static u8 engine_event_sample(struct perf_event *event)
51{
52 return engine_config_sample(event->attr.config);
53}
54
55static u8 engine_event_class(struct perf_event *event)
56{
57 return (event->attr.config >> I915_PMU_CLASS_SHIFT) & 0xff;
58}
59
60static u8 engine_event_instance(struct perf_event *event)
61{
62 return (event->attr.config >> I915_PMU_SAMPLE_BITS) & 0xff;
63}
64
65static bool is_engine_config(u64 config)
66{
67 return config < __I915_PMU_OTHER(0);
68}
69
70static unsigned int config_enabled_bit(u64 config)
71{
72 if (is_engine_config(config))
73 return engine_config_sample(config);
74 else
75 return ENGINE_SAMPLE_BITS + (config - __I915_PMU_OTHER(0));
76}
77
78static u64 config_enabled_mask(u64 config)
79{
80 return BIT_ULL(config_enabled_bit(config));
81}
82
83static bool is_engine_event(struct perf_event *event)
84{
85 return is_engine_config(event->attr.config);
86}
87
88static unsigned int event_enabled_bit(struct perf_event *event)
89{
90 return config_enabled_bit(event->attr.config);
91}
92
Tvrtko Ursulinb3add012017-11-21 18:18:49 +000093static bool supports_busy_stats(struct drm_i915_private *i915)
94{
95 return INTEL_GEN(i915) >= 8;
96}
97
Tvrtko Ursulinfeff0dc2017-11-21 18:18:46 +000098static bool pmu_needs_timer(struct drm_i915_private *i915, bool gpu_active)
99{
100 u64 enable;
101
102 /*
103 * Only some counters need the sampling timer.
104 *
105 * We start with a bitmask of all currently enabled events.
106 */
107 enable = i915->pmu.enable;
108
109 /*
110 * Mask out all the ones which do not need the timer, or in
111 * other words keep all the ones that could need the timer.
112 */
113 enable &= config_enabled_mask(I915_PMU_ACTUAL_FREQUENCY) |
114 config_enabled_mask(I915_PMU_REQUESTED_FREQUENCY) |
115 ENGINE_SAMPLE_MASK;
116
117 /*
118 * When the GPU is idle per-engine counters do not need to be
119 * running so clear those bits out.
120 */
121 if (!gpu_active)
122 enable &= ~ENGINE_SAMPLE_MASK;
Tvrtko Ursulinb3add012017-11-21 18:18:49 +0000123 /*
124 * Also there is software busyness tracking available we do not
125 * need the timer for I915_SAMPLE_BUSY counter.
126 */
127 else if (supports_busy_stats(i915))
128 enable &= ~BIT(I915_SAMPLE_BUSY);
Tvrtko Ursulinfeff0dc2017-11-21 18:18:46 +0000129
130 /*
131 * If some bits remain it means we need the sampling timer running.
132 */
133 return enable;
134}
135
136void i915_pmu_gt_parked(struct drm_i915_private *i915)
137{
138 if (!i915->pmu.base.event_init)
139 return;
140
141 spin_lock_irq(&i915->pmu.lock);
142 /*
143 * Signal sampling timer to stop if only engine events are enabled and
144 * GPU went idle.
145 */
146 i915->pmu.timer_enabled = pmu_needs_timer(i915, false);
147 spin_unlock_irq(&i915->pmu.lock);
148}
149
150static void __i915_pmu_maybe_start_timer(struct drm_i915_private *i915)
151{
152 if (!i915->pmu.timer_enabled && pmu_needs_timer(i915, true)) {
153 i915->pmu.timer_enabled = true;
154 hrtimer_start_range_ns(&i915->pmu.timer,
155 ns_to_ktime(PERIOD), 0,
156 HRTIMER_MODE_REL_PINNED);
157 }
158}
159
160void i915_pmu_gt_unparked(struct drm_i915_private *i915)
161{
162 if (!i915->pmu.base.event_init)
163 return;
164
165 spin_lock_irq(&i915->pmu.lock);
166 /*
167 * Re-enable sampling timer when GPU goes active.
168 */
169 __i915_pmu_maybe_start_timer(i915);
170 spin_unlock_irq(&i915->pmu.lock);
171}
172
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000173static bool grab_forcewake(struct drm_i915_private *i915, bool fw)
174{
175 if (!fw)
176 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
177
178 return true;
179}
180
181static void
182update_sample(struct i915_pmu_sample *sample, u32 unit, u32 val)
183{
184 /*
185 * Since we are doing stochastic sampling for these counters,
186 * average the delta with the previous value for better accuracy.
187 */
188 sample->cur += div_u64(mul_u32_u32(sample->prev + val, unit), 2);
189 sample->prev = val;
190}
191
192static void engines_sample(struct drm_i915_private *dev_priv)
193{
194 struct intel_engine_cs *engine;
195 enum intel_engine_id id;
196 bool fw = false;
197
198 if ((dev_priv->pmu.enable & ENGINE_SAMPLE_MASK) == 0)
199 return;
200
201 if (!dev_priv->gt.awake)
202 return;
203
204 if (!intel_runtime_pm_get_if_in_use(dev_priv))
205 return;
206
207 for_each_engine(engine, dev_priv, id) {
208 u32 current_seqno = intel_engine_get_seqno(engine);
209 u32 last_seqno = intel_engine_last_submit(engine);
210 u32 val;
211
212 val = !i915_seqno_passed(current_seqno, last_seqno);
213
214 update_sample(&engine->pmu.sample[I915_SAMPLE_BUSY],
215 PERIOD, val);
216
217 if (val && (engine->pmu.enable &
218 (BIT(I915_SAMPLE_WAIT) | BIT(I915_SAMPLE_SEMA)))) {
219 fw = grab_forcewake(dev_priv, fw);
220
221 val = I915_READ_FW(RING_CTL(engine->mmio_base));
222 } else {
223 val = 0;
224 }
225
226 update_sample(&engine->pmu.sample[I915_SAMPLE_WAIT],
227 PERIOD, !!(val & RING_WAIT));
228
229 update_sample(&engine->pmu.sample[I915_SAMPLE_SEMA],
230 PERIOD, !!(val & RING_WAIT_SEMAPHORE));
231 }
232
233 if (fw)
234 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
235
236 intel_runtime_pm_put(dev_priv);
237}
238
239static void frequency_sample(struct drm_i915_private *dev_priv)
240{
241 if (dev_priv->pmu.enable &
242 config_enabled_mask(I915_PMU_ACTUAL_FREQUENCY)) {
243 u32 val;
244
245 val = dev_priv->gt_pm.rps.cur_freq;
246 if (dev_priv->gt.awake &&
247 intel_runtime_pm_get_if_in_use(dev_priv)) {
248 val = intel_get_cagf(dev_priv,
249 I915_READ_NOTRACE(GEN6_RPSTAT1));
250 intel_runtime_pm_put(dev_priv);
251 }
252
253 update_sample(&dev_priv->pmu.sample[__I915_SAMPLE_FREQ_ACT],
254 1, intel_gpu_freq(dev_priv, val));
255 }
256
257 if (dev_priv->pmu.enable &
258 config_enabled_mask(I915_PMU_REQUESTED_FREQUENCY)) {
259 update_sample(&dev_priv->pmu.sample[__I915_SAMPLE_FREQ_REQ], 1,
260 intel_gpu_freq(dev_priv,
261 dev_priv->gt_pm.rps.cur_freq));
262 }
263}
264
265static enum hrtimer_restart i915_sample(struct hrtimer *hrtimer)
266{
267 struct drm_i915_private *i915 =
268 container_of(hrtimer, struct drm_i915_private, pmu.timer);
269
Tvrtko Ursulinfeff0dc2017-11-21 18:18:46 +0000270 if (!READ_ONCE(i915->pmu.timer_enabled))
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000271 return HRTIMER_NORESTART;
272
273 engines_sample(i915);
274 frequency_sample(i915);
275
276 hrtimer_forward_now(hrtimer, ns_to_ktime(PERIOD));
277 return HRTIMER_RESTART;
278}
279
280static void i915_pmu_event_destroy(struct perf_event *event)
281{
282 WARN_ON(event->parent);
283}
284
285static int engine_event_init(struct perf_event *event)
286{
287 struct drm_i915_private *i915 =
288 container_of(event->pmu, typeof(*i915), pmu.base);
289
290 if (!intel_engine_lookup_user(i915, engine_event_class(event),
291 engine_event_instance(event)))
292 return -ENODEV;
293
294 switch (engine_event_sample(event)) {
295 case I915_SAMPLE_BUSY:
296 case I915_SAMPLE_WAIT:
297 break;
298 case I915_SAMPLE_SEMA:
299 if (INTEL_GEN(i915) < 6)
300 return -ENODEV;
301 break;
302 default:
303 return -ENOENT;
304 }
305
306 return 0;
307}
308
309static int i915_pmu_event_init(struct perf_event *event)
310{
311 struct drm_i915_private *i915 =
312 container_of(event->pmu, typeof(*i915), pmu.base);
313 int cpu, ret;
314
315 if (event->attr.type != event->pmu->type)
316 return -ENOENT;
317
318 /* unsupported modes and filters */
319 if (event->attr.sample_period) /* no sampling */
320 return -EINVAL;
321
322 if (has_branch_stack(event))
323 return -EOPNOTSUPP;
324
325 if (event->cpu < 0)
326 return -EINVAL;
327
328 cpu = cpumask_any_and(&i915_pmu_cpumask,
329 topology_sibling_cpumask(event->cpu));
330 if (cpu >= nr_cpu_ids)
331 return -ENODEV;
332
333 if (is_engine_event(event)) {
334 ret = engine_event_init(event);
335 } else {
336 ret = 0;
337 switch (event->attr.config) {
338 case I915_PMU_ACTUAL_FREQUENCY:
339 if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915))
340 /* Requires a mutex for sampling! */
341 ret = -ENODEV;
342 case I915_PMU_REQUESTED_FREQUENCY:
343 if (INTEL_GEN(i915) < 6)
344 ret = -ENODEV;
345 break;
346 default:
347 ret = -ENOENT;
348 break;
349 }
350 }
351 if (ret)
352 return ret;
353
354 event->cpu = cpu;
355 if (!event->parent)
356 event->destroy = i915_pmu_event_destroy;
357
358 return 0;
359}
360
361static u64 __i915_pmu_event_read(struct perf_event *event)
362{
363 struct drm_i915_private *i915 =
364 container_of(event->pmu, typeof(*i915), pmu.base);
365 u64 val = 0;
366
367 if (is_engine_event(event)) {
368 u8 sample = engine_event_sample(event);
369 struct intel_engine_cs *engine;
370
371 engine = intel_engine_lookup_user(i915,
372 engine_event_class(event),
373 engine_event_instance(event));
374
375 if (WARN_ON_ONCE(!engine)) {
376 /* Do nothing */
Tvrtko Ursulinb3add012017-11-21 18:18:49 +0000377 } else if (sample == I915_SAMPLE_BUSY &&
378 engine->pmu.busy_stats) {
379 val = ktime_to_ns(intel_engine_get_busy_time(engine));
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000380 } else {
381 val = engine->pmu.sample[sample].cur;
382 }
383 } else {
384 switch (event->attr.config) {
385 case I915_PMU_ACTUAL_FREQUENCY:
386 val =
387 div_u64(i915->pmu.sample[__I915_SAMPLE_FREQ_ACT].cur,
388 FREQUENCY);
389 break;
390 case I915_PMU_REQUESTED_FREQUENCY:
391 val =
392 div_u64(i915->pmu.sample[__I915_SAMPLE_FREQ_REQ].cur,
393 FREQUENCY);
394 break;
395 }
396 }
397
398 return val;
399}
400
401static void i915_pmu_event_read(struct perf_event *event)
402{
403 struct hw_perf_event *hwc = &event->hw;
404 u64 prev, new;
405
406again:
407 prev = local64_read(&hwc->prev_count);
408 new = __i915_pmu_event_read(event);
409
410 if (local64_cmpxchg(&hwc->prev_count, prev, new) != prev)
411 goto again;
412
413 local64_add(new - prev, &event->count);
414}
415
Tvrtko Ursulinb3add012017-11-21 18:18:49 +0000416static bool engine_needs_busy_stats(struct intel_engine_cs *engine)
417{
418 return supports_busy_stats(engine->i915) &&
419 (engine->pmu.enable & BIT(I915_SAMPLE_BUSY));
420}
421
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000422static void i915_pmu_enable(struct perf_event *event)
423{
424 struct drm_i915_private *i915 =
425 container_of(event->pmu, typeof(*i915), pmu.base);
426 unsigned int bit = event_enabled_bit(event);
427 unsigned long flags;
428
429 spin_lock_irqsave(&i915->pmu.lock, flags);
430
431 /*
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000432 * Update the bitmask of enabled events and increment
433 * the event reference counter.
434 */
435 GEM_BUG_ON(bit >= I915_PMU_MASK_BITS);
436 GEM_BUG_ON(i915->pmu.enable_count[bit] == ~0);
437 i915->pmu.enable |= BIT_ULL(bit);
438 i915->pmu.enable_count[bit]++;
439
440 /*
Tvrtko Ursulinfeff0dc2017-11-21 18:18:46 +0000441 * Start the sampling timer if needed and not already enabled.
442 */
443 __i915_pmu_maybe_start_timer(i915);
444
445 /*
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000446 * For per-engine events the bitmask and reference counting
447 * is stored per engine.
448 */
449 if (is_engine_event(event)) {
450 u8 sample = engine_event_sample(event);
451 struct intel_engine_cs *engine;
452
453 engine = intel_engine_lookup_user(i915,
454 engine_event_class(event),
455 engine_event_instance(event));
456 GEM_BUG_ON(!engine);
457 engine->pmu.enable |= BIT(sample);
458
459 GEM_BUG_ON(sample >= I915_PMU_SAMPLE_BITS);
460 GEM_BUG_ON(engine->pmu.enable_count[sample] == ~0);
Tvrtko Ursulinb3add012017-11-21 18:18:49 +0000461 if (engine->pmu.enable_count[sample]++ == 0) {
462 /*
463 * Enable engine busy stats tracking if needed or
464 * alternatively cancel the scheduled disable.
465 *
466 * If the delayed disable was pending, cancel it and
467 * in this case do not enable since it already is.
468 */
469 if (engine_needs_busy_stats(engine) &&
470 !engine->pmu.busy_stats) {
471 engine->pmu.busy_stats = true;
472 if (!cancel_delayed_work(&engine->pmu.disable_busy_stats))
473 intel_enable_engine_stats(engine);
474 }
475 }
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000476 }
477
478 /*
479 * Store the current counter value so we can report the correct delta
480 * for all listeners. Even when the event was already enabled and has
481 * an existing non-zero value.
482 */
483 local64_set(&event->hw.prev_count, __i915_pmu_event_read(event));
484
485 spin_unlock_irqrestore(&i915->pmu.lock, flags);
486}
487
Tvrtko Ursulinb3add012017-11-21 18:18:49 +0000488static void __disable_busy_stats(struct work_struct *work)
489{
490 struct intel_engine_cs *engine =
491 container_of(work, typeof(*engine), pmu.disable_busy_stats.work);
492
493 intel_disable_engine_stats(engine);
494}
495
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000496static void i915_pmu_disable(struct perf_event *event)
497{
498 struct drm_i915_private *i915 =
499 container_of(event->pmu, typeof(*i915), pmu.base);
500 unsigned int bit = event_enabled_bit(event);
501 unsigned long flags;
502
503 spin_lock_irqsave(&i915->pmu.lock, flags);
504
505 if (is_engine_event(event)) {
506 u8 sample = engine_event_sample(event);
507 struct intel_engine_cs *engine;
508
509 engine = intel_engine_lookup_user(i915,
510 engine_event_class(event),
511 engine_event_instance(event));
512 GEM_BUG_ON(!engine);
513 GEM_BUG_ON(sample >= I915_PMU_SAMPLE_BITS);
514 GEM_BUG_ON(engine->pmu.enable_count[sample] == 0);
515 /*
516 * Decrement the reference count and clear the enabled
517 * bitmask when the last listener on an event goes away.
518 */
Tvrtko Ursulinb3add012017-11-21 18:18:49 +0000519 if (--engine->pmu.enable_count[sample] == 0) {
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000520 engine->pmu.enable &= ~BIT(sample);
Tvrtko Ursulinb3add012017-11-21 18:18:49 +0000521 if (!engine_needs_busy_stats(engine) &&
522 engine->pmu.busy_stats) {
523 engine->pmu.busy_stats = false;
524 /*
525 * We request a delayed disable to handle the
526 * rapid on/off cycles on events, which can
527 * happen when tools like perf stat start, in a
528 * nicer way.
529 *
530 * In addition, this also helps with busy stats
531 * accuracy with background CPU offline/online
532 * migration events.
533 */
534 queue_delayed_work(system_wq,
535 &engine->pmu.disable_busy_stats,
536 round_jiffies_up_relative(HZ));
537 }
538 }
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000539 }
540
541 GEM_BUG_ON(bit >= I915_PMU_MASK_BITS);
542 GEM_BUG_ON(i915->pmu.enable_count[bit] == 0);
543 /*
544 * Decrement the reference count and clear the enabled
545 * bitmask when the last listener on an event goes away.
546 */
Tvrtko Ursulinfeff0dc2017-11-21 18:18:46 +0000547 if (--i915->pmu.enable_count[bit] == 0) {
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000548 i915->pmu.enable &= ~BIT_ULL(bit);
Tvrtko Ursulinfeff0dc2017-11-21 18:18:46 +0000549 i915->pmu.timer_enabled &= pmu_needs_timer(i915, true);
550 }
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000551
552 spin_unlock_irqrestore(&i915->pmu.lock, flags);
553}
554
555static void i915_pmu_event_start(struct perf_event *event, int flags)
556{
557 i915_pmu_enable(event);
558 event->hw.state = 0;
559}
560
561static void i915_pmu_event_stop(struct perf_event *event, int flags)
562{
563 if (flags & PERF_EF_UPDATE)
564 i915_pmu_event_read(event);
565 i915_pmu_disable(event);
566 event->hw.state = PERF_HES_STOPPED;
567}
568
569static int i915_pmu_event_add(struct perf_event *event, int flags)
570{
571 if (flags & PERF_EF_START)
572 i915_pmu_event_start(event, flags);
573
574 return 0;
575}
576
577static void i915_pmu_event_del(struct perf_event *event, int flags)
578{
579 i915_pmu_event_stop(event, PERF_EF_UPDATE);
580}
581
582static int i915_pmu_event_event_idx(struct perf_event *event)
583{
584 return 0;
585}
586
587static ssize_t i915_pmu_format_show(struct device *dev,
588 struct device_attribute *attr, char *buf)
589{
590 struct dev_ext_attribute *eattr;
591
592 eattr = container_of(attr, struct dev_ext_attribute, attr);
593 return sprintf(buf, "%s\n", (char *)eattr->var);
594}
595
596#define I915_PMU_FORMAT_ATTR(_name, _config) \
597 (&((struct dev_ext_attribute[]) { \
598 { .attr = __ATTR(_name, 0444, i915_pmu_format_show, NULL), \
599 .var = (void *)_config, } \
600 })[0].attr.attr)
601
602static struct attribute *i915_pmu_format_attrs[] = {
603 I915_PMU_FORMAT_ATTR(i915_eventid, "config:0-20"),
604 NULL,
605};
606
607static const struct attribute_group i915_pmu_format_attr_group = {
608 .name = "format",
609 .attrs = i915_pmu_format_attrs,
610};
611
612static ssize_t i915_pmu_event_show(struct device *dev,
613 struct device_attribute *attr, char *buf)
614{
615 struct dev_ext_attribute *eattr;
616
617 eattr = container_of(attr, struct dev_ext_attribute, attr);
618 return sprintf(buf, "config=0x%lx\n", (unsigned long)eattr->var);
619}
620
621#define I915_EVENT_ATTR(_name, _config) \
622 (&((struct dev_ext_attribute[]) { \
623 { .attr = __ATTR(_name, 0444, i915_pmu_event_show, NULL), \
624 .var = (void *)_config, } \
625 })[0].attr.attr)
626
627#define I915_EVENT_STR(_name, _str) \
628 (&((struct perf_pmu_events_attr[]) { \
629 { .attr = __ATTR(_name, 0444, perf_event_sysfs_show, NULL), \
630 .id = 0, \
631 .event_str = _str, } \
632 })[0].attr.attr)
633
634#define I915_EVENT(_name, _config, _unit) \
635 I915_EVENT_ATTR(_name, _config), \
636 I915_EVENT_STR(_name.unit, _unit)
637
638#define I915_ENGINE_EVENT(_name, _class, _instance, _sample) \
639 I915_EVENT_ATTR(_name, __I915_PMU_ENGINE(_class, _instance, _sample)), \
640 I915_EVENT_STR(_name.unit, "ns")
641
642#define I915_ENGINE_EVENTS(_name, _class, _instance) \
643 I915_ENGINE_EVENT(_name##_instance-busy, _class, _instance, I915_SAMPLE_BUSY), \
644 I915_ENGINE_EVENT(_name##_instance-sema, _class, _instance, I915_SAMPLE_SEMA), \
645 I915_ENGINE_EVENT(_name##_instance-wait, _class, _instance, I915_SAMPLE_WAIT)
646
647static struct attribute *i915_pmu_events_attrs[] = {
648 I915_ENGINE_EVENTS(rcs, I915_ENGINE_CLASS_RENDER, 0),
649 I915_ENGINE_EVENTS(bcs, I915_ENGINE_CLASS_COPY, 0),
650 I915_ENGINE_EVENTS(vcs, I915_ENGINE_CLASS_VIDEO, 0),
651 I915_ENGINE_EVENTS(vcs, I915_ENGINE_CLASS_VIDEO, 1),
652 I915_ENGINE_EVENTS(vecs, I915_ENGINE_CLASS_VIDEO_ENHANCE, 0),
653
654 I915_EVENT(actual-frequency, I915_PMU_ACTUAL_FREQUENCY, "MHz"),
655 I915_EVENT(requested-frequency, I915_PMU_REQUESTED_FREQUENCY, "MHz"),
656
657 NULL,
658};
659
660static const struct attribute_group i915_pmu_events_attr_group = {
661 .name = "events",
662 .attrs = i915_pmu_events_attrs,
663};
664
665static ssize_t
666i915_pmu_get_attr_cpumask(struct device *dev,
667 struct device_attribute *attr,
668 char *buf)
669{
670 return cpumap_print_to_pagebuf(true, buf, &i915_pmu_cpumask);
671}
672
673static DEVICE_ATTR(cpumask, 0444, i915_pmu_get_attr_cpumask, NULL);
674
675static struct attribute *i915_cpumask_attrs[] = {
676 &dev_attr_cpumask.attr,
677 NULL,
678};
679
680static struct attribute_group i915_pmu_cpumask_attr_group = {
681 .attrs = i915_cpumask_attrs,
682};
683
684static const struct attribute_group *i915_pmu_attr_groups[] = {
685 &i915_pmu_format_attr_group,
686 &i915_pmu_events_attr_group,
687 &i915_pmu_cpumask_attr_group,
688 NULL
689};
690
691#ifdef CONFIG_HOTPLUG_CPU
692static int i915_pmu_cpu_online(unsigned int cpu, struct hlist_node *node)
693{
694 struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), node);
695 unsigned int target;
696
697 GEM_BUG_ON(!pmu->base.event_init);
698
699 target = cpumask_any_and(&i915_pmu_cpumask, &i915_pmu_cpumask);
700 /* Select the first online CPU as a designated reader. */
701 if (target >= nr_cpu_ids)
702 cpumask_set_cpu(cpu, &i915_pmu_cpumask);
703
704 return 0;
705}
706
707static int i915_pmu_cpu_offline(unsigned int cpu, struct hlist_node *node)
708{
709 struct i915_pmu *pmu = hlist_entry_safe(node, typeof(*pmu), node);
710 unsigned int target;
711
712 GEM_BUG_ON(!pmu->base.event_init);
713
714 if (cpumask_test_and_clear_cpu(cpu, &i915_pmu_cpumask)) {
715 target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu);
716 /* Migrate events if there is a valid target */
717 if (target < nr_cpu_ids) {
718 cpumask_set_cpu(target, &i915_pmu_cpumask);
719 perf_pmu_migrate_context(&pmu->base, cpu, target);
720 }
721 }
722
723 return 0;
724}
725
726static enum cpuhp_state cpuhp_slot = CPUHP_INVALID;
727#endif
728
729static int i915_pmu_register_cpuhp_state(struct drm_i915_private *i915)
730{
731#ifdef CONFIG_HOTPLUG_CPU
732 enum cpuhp_state slot;
733 int ret;
734
735 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
736 "perf/x86/intel/i915:online",
737 i915_pmu_cpu_online,
738 i915_pmu_cpu_offline);
739 if (ret < 0)
740 return ret;
741
742 slot = ret;
743 ret = cpuhp_state_add_instance(slot, &i915->pmu.node);
744 if (ret) {
745 cpuhp_remove_multi_state(slot);
746 return ret;
747 }
748
749 cpuhp_slot = slot;
750#endif
751 return 0;
752}
753
754static void i915_pmu_unregister_cpuhp_state(struct drm_i915_private *i915)
755{
756#ifdef CONFIG_HOTPLUG_CPU
757 WARN_ON(cpuhp_slot == CPUHP_INVALID);
758 WARN_ON(cpuhp_state_remove_instance(cpuhp_slot, &i915->pmu.node));
759 cpuhp_remove_multi_state(cpuhp_slot);
760#endif
761}
762
763void i915_pmu_register(struct drm_i915_private *i915)
764{
Tvrtko Ursulinb3add012017-11-21 18:18:49 +0000765 struct intel_engine_cs *engine;
766 enum intel_engine_id id;
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000767 int ret;
768
769 if (INTEL_GEN(i915) <= 2) {
770 DRM_INFO("PMU not supported for this GPU.");
771 return;
772 }
773
774 i915->pmu.base.attr_groups = i915_pmu_attr_groups;
775 i915->pmu.base.task_ctx_nr = perf_invalid_context;
776 i915->pmu.base.event_init = i915_pmu_event_init;
777 i915->pmu.base.add = i915_pmu_event_add;
778 i915->pmu.base.del = i915_pmu_event_del;
779 i915->pmu.base.start = i915_pmu_event_start;
780 i915->pmu.base.stop = i915_pmu_event_stop;
781 i915->pmu.base.read = i915_pmu_event_read;
782 i915->pmu.base.event_idx = i915_pmu_event_event_idx;
783
784 spin_lock_init(&i915->pmu.lock);
785 hrtimer_init(&i915->pmu.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
786 i915->pmu.timer.function = i915_sample;
787
Tvrtko Ursulinb3add012017-11-21 18:18:49 +0000788 for_each_engine(engine, i915, id)
789 INIT_DELAYED_WORK(&engine->pmu.disable_busy_stats,
790 __disable_busy_stats);
791
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000792 ret = perf_pmu_register(&i915->pmu.base, "i915", -1);
793 if (ret)
794 goto err;
795
796 ret = i915_pmu_register_cpuhp_state(i915);
797 if (ret)
798 goto err_unreg;
799
800 return;
801
802err_unreg:
803 perf_pmu_unregister(&i915->pmu.base);
804err:
805 i915->pmu.base.event_init = NULL;
806 DRM_NOTE("Failed to register PMU! (err=%d)\n", ret);
807}
808
809void i915_pmu_unregister(struct drm_i915_private *i915)
810{
Tvrtko Ursulinb3add012017-11-21 18:18:49 +0000811 struct intel_engine_cs *engine;
812 enum intel_engine_id id;
813
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000814 if (!i915->pmu.base.event_init)
815 return;
816
817 WARN_ON(i915->pmu.enable);
818
819 hrtimer_cancel(&i915->pmu.timer);
820
Tvrtko Ursulinb3add012017-11-21 18:18:49 +0000821 for_each_engine(engine, i915, id) {
822 GEM_BUG_ON(engine->pmu.busy_stats);
823 flush_delayed_work(&engine->pmu.disable_busy_stats);
824 }
825
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +0000826 i915_pmu_unregister_cpuhp_state(i915);
827
828 perf_pmu_unregister(&i915->pmu.base);
829 i915->pmu.base.event_init = NULL;
830}