blob: 0233fbb0514b2851a3daafc4de8b4b79165dfad8 [file] [log] [blame]
Chris Wilsonc9f01732013-08-25 20:13:31 +01001/*
2 * Copyright © 2013 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 <sys/stat.h>
26#include <unistd.h>
27#include <fcntl.h>
28#include <errno.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
Chris Wilsonc8642792014-04-30 18:39:27 +010032#include <ctype.h>
Chris Wilsonc9f01732013-08-25 20:13:31 +010033
Tvrtko Ursulina688dec2017-09-13 17:38:16 +010034#include "igt_perf.h"
35
Chris Wilsonc9f01732013-08-25 20:13:31 +010036#include "gem-interrupts.h"
37#include "debugfs.h"
38
Chris Wilsonc8642792014-04-30 18:39:27 +010039static long long debugfs_read(void)
Chris Wilsonc9f01732013-08-25 20:13:31 +010040{
Chris Wilsonc8642792014-04-30 18:39:27 +010041 char buf[8192], *b;
42 int fd, len;
Chris Wilsonc9f01732013-08-25 20:13:31 +010043
Chris Wilsonc9f01732013-08-25 20:13:31 +010044 sprintf(buf, "%s/i915_gem_interrupt", debugfs_dri_path);
Chris Wilsonc8642792014-04-30 18:39:27 +010045 fd = open(buf, 0);
46 if (fd < 0)
47 return -1;
Chris Wilsonc9f01732013-08-25 20:13:31 +010048
Chris Wilsonc8642792014-04-30 18:39:27 +010049 len = read(fd, buf, sizeof(buf)-1);
50 close(fd);
51
52 if (len < 0)
53 return -1;
54
55 buf[len] = '\0';
56
57 b = strstr(buf, "Interrupts received:");
58 if (b == NULL)
59 return -1;
60
61 return strtoull(b + sizeof("Interrupts received:"), 0, 0);
62}
63
64static long long procfs_read(void)
65{
66 char buf[8192], *b;
67 int fd, len;
68 unsigned long long val;
69
70/* 44: 51 42446 0 0 PCI-MSI-edge i915*/
71 fd = open("/proc/interrupts", 0);
72 if (fd < 0)
73 return -1;
74
75 len = read(fd, buf, sizeof(buf)-1);
76 close(fd);
77
78 if (len < 0)
79 return -1;
80
81 buf[len] = '\0';
82
83 b = strstr(buf, "i915");
84 if (b == NULL)
85 return -1;
86 while (*--b != ':')
87 ;
88
89 val = 0;
90 do {
91 while (isspace(*++b))
92 ;
93 if (!isdigit(*b))
94 break;
95
96 val += strtoull(b, &b, 0);
97 } while(1);
98
99 return val;
100}
101
102static long long interrupts_read(void)
103{
104 long long val;
105
106 val = debugfs_read();
107 if (val < 0)
108 val = procfs_read();
109 return val;
Chris Wilsonc9f01732013-08-25 20:13:31 +0100110}
111
Chris Wilsond58aed12013-08-27 12:24:18 +0100112int gem_interrupts_init(struct gem_interrupts *irqs)
113{
114 memset(irqs, 0, sizeof(*irqs));
115
Tvrtko Ursulin0d8385a2017-09-13 18:28:24 +0100116 irqs->fd = perf_i915_open(I915_PMU_INTERRUPTS);
Chris Wilsonc8642792014-04-30 18:39:27 +0100117 if (irqs->fd < 0 && interrupts_read() < 0)
118 irqs->error = ENODEV;
Chris Wilsond58aed12013-08-27 12:24:18 +0100119
120 return irqs->error;
121}
122
Chris Wilsonc9f01732013-08-25 20:13:31 +0100123int gem_interrupts_update(struct gem_interrupts *irqs)
124{
Chris Wilsond58aed12013-08-27 12:24:18 +0100125 uint64_t val;
126 int update;
Chris Wilsonc9f01732013-08-25 20:13:31 +0100127
128 if (irqs->error)
129 return irqs->error;
130
Chris Wilsond58aed12013-08-27 12:24:18 +0100131 if (irqs->fd < 0) {
Thomas Wood26437932015-11-02 10:18:27 +0000132 long long ret;
133 ret = interrupts_read();
134 if (ret < 0)
Chris Wilsonc8642792014-04-30 18:39:27 +0100135 return irqs->error = ENODEV;
Thomas Wood26437932015-11-02 10:18:27 +0000136 else
137 val = ret;
Chris Wilsond58aed12013-08-27 12:24:18 +0100138 } else {
Tvrtko Ursulin19c99242017-09-13 18:48:48 +0100139 uint64_t data[2];
140
141 if (read(irqs->fd, data, sizeof(data)) < 0)
Chris Wilsond58aed12013-08-27 12:24:18 +0100142 return irqs->error = errno;
Tvrtko Ursulin19c99242017-09-13 18:48:48 +0100143
144 val = data[0];
Chris Wilsond58aed12013-08-27 12:24:18 +0100145 }
146
147 update = irqs->last_count == 0;
Chris Wilsonc9f01732013-08-25 20:13:31 +0100148 irqs->last_count = irqs->count;
Chris Wilsond58aed12013-08-27 12:24:18 +0100149 irqs->count = val;
Chris Wilsonc9f01732013-08-25 20:13:31 +0100150 irqs->delta = irqs->count - irqs->last_count;
Chris Wilsond58aed12013-08-27 12:24:18 +0100151 return update ? EAGAIN : 0;
Chris Wilsonc9f01732013-08-25 20:13:31 +0100152}