Chris Wilson | c9f0173 | 2013-08-25 20:13:31 +0100 | [diff] [blame] | 1 | /* |
| 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 Wilson | c864279 | 2014-04-30 18:39:27 +0100 | [diff] [blame] | 32 | #include <ctype.h> |
Chris Wilson | c9f0173 | 2013-08-25 20:13:31 +0100 | [diff] [blame] | 33 | |
Tvrtko Ursulin | a688dec | 2017-09-13 17:38:16 +0100 | [diff] [blame] | 34 | #include "igt_perf.h" |
| 35 | |
Chris Wilson | c9f0173 | 2013-08-25 20:13:31 +0100 | [diff] [blame] | 36 | #include "gem-interrupts.h" |
| 37 | #include "debugfs.h" |
| 38 | |
Chris Wilson | c864279 | 2014-04-30 18:39:27 +0100 | [diff] [blame] | 39 | static long long debugfs_read(void) |
Chris Wilson | c9f0173 | 2013-08-25 20:13:31 +0100 | [diff] [blame] | 40 | { |
Chris Wilson | c864279 | 2014-04-30 18:39:27 +0100 | [diff] [blame] | 41 | char buf[8192], *b; |
| 42 | int fd, len; |
Chris Wilson | c9f0173 | 2013-08-25 20:13:31 +0100 | [diff] [blame] | 43 | |
Chris Wilson | c9f0173 | 2013-08-25 20:13:31 +0100 | [diff] [blame] | 44 | sprintf(buf, "%s/i915_gem_interrupt", debugfs_dri_path); |
Chris Wilson | c864279 | 2014-04-30 18:39:27 +0100 | [diff] [blame] | 45 | fd = open(buf, 0); |
| 46 | if (fd < 0) |
| 47 | return -1; |
Chris Wilson | c9f0173 | 2013-08-25 20:13:31 +0100 | [diff] [blame] | 48 | |
Chris Wilson | c864279 | 2014-04-30 18:39:27 +0100 | [diff] [blame] | 49 | 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 | |
| 64 | static 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 | |
| 102 | static 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 Wilson | c9f0173 | 2013-08-25 20:13:31 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Chris Wilson | d58aed1 | 2013-08-27 12:24:18 +0100 | [diff] [blame] | 112 | int gem_interrupts_init(struct gem_interrupts *irqs) |
| 113 | { |
| 114 | memset(irqs, 0, sizeof(*irqs)); |
| 115 | |
Tvrtko Ursulin | 0d8385a | 2017-09-13 18:28:24 +0100 | [diff] [blame] | 116 | irqs->fd = perf_i915_open(I915_PMU_INTERRUPTS); |
Chris Wilson | c864279 | 2014-04-30 18:39:27 +0100 | [diff] [blame] | 117 | if (irqs->fd < 0 && interrupts_read() < 0) |
| 118 | irqs->error = ENODEV; |
Chris Wilson | d58aed1 | 2013-08-27 12:24:18 +0100 | [diff] [blame] | 119 | |
| 120 | return irqs->error; |
| 121 | } |
| 122 | |
Chris Wilson | c9f0173 | 2013-08-25 20:13:31 +0100 | [diff] [blame] | 123 | int gem_interrupts_update(struct gem_interrupts *irqs) |
| 124 | { |
Chris Wilson | d58aed1 | 2013-08-27 12:24:18 +0100 | [diff] [blame] | 125 | uint64_t val; |
| 126 | int update; |
Chris Wilson | c9f0173 | 2013-08-25 20:13:31 +0100 | [diff] [blame] | 127 | |
| 128 | if (irqs->error) |
| 129 | return irqs->error; |
| 130 | |
Chris Wilson | d58aed1 | 2013-08-27 12:24:18 +0100 | [diff] [blame] | 131 | if (irqs->fd < 0) { |
Thomas Wood | 2643793 | 2015-11-02 10:18:27 +0000 | [diff] [blame] | 132 | long long ret; |
| 133 | ret = interrupts_read(); |
| 134 | if (ret < 0) |
Chris Wilson | c864279 | 2014-04-30 18:39:27 +0100 | [diff] [blame] | 135 | return irqs->error = ENODEV; |
Thomas Wood | 2643793 | 2015-11-02 10:18:27 +0000 | [diff] [blame] | 136 | else |
| 137 | val = ret; |
Chris Wilson | d58aed1 | 2013-08-27 12:24:18 +0100 | [diff] [blame] | 138 | } else { |
Tvrtko Ursulin | 19c9924 | 2017-09-13 18:48:48 +0100 | [diff] [blame] | 139 | uint64_t data[2]; |
| 140 | |
| 141 | if (read(irqs->fd, data, sizeof(data)) < 0) |
Chris Wilson | d58aed1 | 2013-08-27 12:24:18 +0100 | [diff] [blame] | 142 | return irqs->error = errno; |
Tvrtko Ursulin | 19c9924 | 2017-09-13 18:48:48 +0100 | [diff] [blame] | 143 | |
| 144 | val = data[0]; |
Chris Wilson | d58aed1 | 2013-08-27 12:24:18 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | update = irqs->last_count == 0; |
Chris Wilson | c9f0173 | 2013-08-25 20:13:31 +0100 | [diff] [blame] | 148 | irqs->last_count = irqs->count; |
Chris Wilson | d58aed1 | 2013-08-27 12:24:18 +0100 | [diff] [blame] | 149 | irqs->count = val; |
Chris Wilson | c9f0173 | 2013-08-25 20:13:31 +0100 | [diff] [blame] | 150 | irqs->delta = irqs->count - irqs->last_count; |
Chris Wilson | d58aed1 | 2013-08-27 12:24:18 +0100 | [diff] [blame] | 151 | return update ? EAGAIN : 0; |
Chris Wilson | c9f0173 | 2013-08-25 20:13:31 +0100 | [diff] [blame] | 152 | } |