Chris Wilson | 9537422 | 2010-04-08 11:56:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008 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 |
| 21 | * DEALINGS IN THE SOFTWARE. |
| 22 | * |
| 23 | * Authors: |
| 24 | * Eric Anholt <eric@anholt.net> |
Ben Widawsky | cac8f8b | 2011-07-28 13:40:19 -0700 | [diff] [blame] | 25 | * Ben Widawsky <ben@bwidawsk.net> |
Chris Wilson | 9537422 | 2010-04-08 11:56:57 +0100 | [diff] [blame] | 26 | * |
| 27 | */ |
| 28 | |
| 29 | #include <unistd.h> |
Alan Coopersmith | 4e3c85f | 2012-01-06 14:37:20 -0800 | [diff] [blame] | 30 | #include <fcntl.h> |
Chris Wilson | 9537422 | 2010-04-08 11:56:57 +0100 | [diff] [blame] | 31 | #include <stdio.h> |
Ben Widawsky | cac8f8b | 2011-07-28 13:40:19 -0700 | [diff] [blame] | 32 | #include <stdarg.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <stdint.h> |
| 35 | #include <stdbool.h> |
Chris Wilson | 9537422 | 2010-04-08 11:56:57 +0100 | [diff] [blame] | 36 | #include <string.h> |
| 37 | #include <errno.h> |
| 38 | #include <err.h> |
| 39 | #include <assert.h> |
| 40 | #include <sys/ioctl.h> |
Chris Wilson | 9537422 | 2010-04-08 11:56:57 +0100 | [diff] [blame] | 41 | #include <sys/stat.h> |
| 42 | #include <sys/mman.h> |
| 43 | |
| 44 | #include "intel_gpu_tools.h" |
| 45 | |
Ben Widawsky | 8904d29 | 2013-02-21 22:05:33 -0800 | [diff] [blame] | 46 | #define FAKEKEY 0x2468ace0 |
| 47 | |
Chris Wilson | 9537422 | 2010-04-08 11:56:57 +0100 | [diff] [blame] | 48 | void *mmio; |
| 49 | |
Ben Widawsky | cac8f8b | 2011-07-28 13:40:19 -0700 | [diff] [blame] | 50 | static struct _mmio_data { |
| 51 | int inited; |
| 52 | bool safe; |
| 53 | char debugfs_path[FILENAME_MAX]; |
| 54 | char debugfs_forcewake_path[FILENAME_MAX]; |
| 55 | uint32_t i915_devid; |
Ben Widawsky | abd7038 | 2011-07-28 13:42:45 -0700 | [diff] [blame] | 56 | struct intel_register_map map; |
Ben Widawsky | cac8f8b | 2011-07-28 13:40:19 -0700 | [diff] [blame] | 57 | int key; |
| 58 | } mmio_data; |
| 59 | |
Chris Wilson | 9537422 | 2010-04-08 11:56:57 +0100 | [diff] [blame] | 60 | void |
| 61 | intel_map_file(char *file) |
| 62 | { |
| 63 | int fd; |
| 64 | struct stat st; |
| 65 | |
| 66 | fd = open(file, O_RDWR); |
| 67 | if (fd == -1) { |
| 68 | fprintf(stderr, "Couldn't open %s: %s\n", file, |
| 69 | strerror(errno)); |
| 70 | exit(1); |
| 71 | } |
| 72 | fstat(fd, &st); |
| 73 | mmio = mmap(NULL, st.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); |
| 74 | if (mmio == MAP_FAILED) { |
| 75 | fprintf(stderr, "Couldn't mmap %s: %s\n", file, |
| 76 | strerror(errno)); |
| 77 | exit(1); |
| 78 | } |
| 79 | close(fd); |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | intel_get_mmio(struct pci_device *pci_dev) |
| 84 | { |
Chris Wilson | 7a02aa4 | 2012-08-26 21:22:15 +0100 | [diff] [blame] | 85 | uint32_t devid, gen; |
| 86 | int mmio_bar, mmio_size; |
Daniel Vetter | 1be3fd7 | 2012-01-09 23:42:19 +0100 | [diff] [blame] | 87 | int error; |
Chris Wilson | 9537422 | 2010-04-08 11:56:57 +0100 | [diff] [blame] | 88 | |
| 89 | devid = pci_dev->device_id; |
Chris Wilson | 41570d9 | 2011-02-14 15:56:14 +0000 | [diff] [blame] | 90 | if (IS_GEN2(devid)) |
Chris Wilson | 9537422 | 2010-04-08 11:56:57 +0100 | [diff] [blame] | 91 | mmio_bar = 1; |
Chris Wilson | 41570d9 | 2011-02-14 15:56:14 +0000 | [diff] [blame] | 92 | else |
| 93 | mmio_bar = 0; |
Chris Wilson | 9537422 | 2010-04-08 11:56:57 +0100 | [diff] [blame] | 94 | |
Chris Wilson | 7a02aa4 | 2012-08-26 21:22:15 +0100 | [diff] [blame] | 95 | gen = intel_gen(devid); |
| 96 | if (gen < 3) |
Daniel Vetter | 50d4675 | 2012-10-12 10:45:52 +0200 | [diff] [blame] | 97 | mmio_size = 512*1024; |
Chris Wilson | 7a02aa4 | 2012-08-26 21:22:15 +0100 | [diff] [blame] | 98 | else if (gen < 5) |
| 99 | mmio_size = 512*1024; |
| 100 | else |
| 101 | mmio_size = 2*1024*1024; |
| 102 | |
Daniel Vetter | 1be3fd7 | 2012-01-09 23:42:19 +0100 | [diff] [blame] | 103 | error = pci_device_map_range (pci_dev, |
Chris Wilson | 7a02aa4 | 2012-08-26 21:22:15 +0100 | [diff] [blame] | 104 | pci_dev->regions[mmio_bar].base_addr, |
| 105 | mmio_size, |
| 106 | PCI_DEV_MAP_FLAG_WRITABLE, |
| 107 | &mmio); |
Chris Wilson | 9537422 | 2010-04-08 11:56:57 +0100 | [diff] [blame] | 108 | |
Daniel Vetter | 1be3fd7 | 2012-01-09 23:42:19 +0100 | [diff] [blame] | 109 | if (error != 0) { |
Chris Wilson | 9537422 | 2010-04-08 11:56:57 +0100 | [diff] [blame] | 110 | fprintf(stderr, "Couldn't map MMIO region: %s\n", |
Daniel Vetter | 1be3fd7 | 2012-01-09 23:42:19 +0100 | [diff] [blame] | 111 | strerror(error)); |
Chris Wilson | 9537422 | 2010-04-08 11:56:57 +0100 | [diff] [blame] | 112 | exit(1); |
| 113 | } |
| 114 | } |
| 115 | |
Ben Widawsky | cac8f8b | 2011-07-28 13:40:19 -0700 | [diff] [blame] | 116 | /* |
| 117 | * If successful, i915_debugfs_path and i915_debugfs_forcewake_path are both |
| 118 | * updated with the correct path. |
| 119 | */ |
| 120 | static int |
Daniel Vetter | 1be3fd7 | 2012-01-09 23:42:19 +0100 | [diff] [blame] | 121 | find_debugfs_path(const char *dri_base) |
Ben Widawsky | cac8f8b | 2011-07-28 13:40:19 -0700 | [diff] [blame] | 122 | { |
| 123 | char buf[FILENAME_MAX]; |
| 124 | struct stat sb; |
| 125 | int i, ret; |
| 126 | |
| 127 | for (i = 0; i < 16; i++) { |
| 128 | snprintf(buf, FILENAME_MAX, "%s/%i/name", dri_base, i); |
| 129 | |
| 130 | snprintf(mmio_data.debugfs_path, FILENAME_MAX, |
| 131 | "%s/%i/", dri_base, i); |
| 132 | snprintf(mmio_data.debugfs_forcewake_path, FILENAME_MAX, |
| 133 | "%s/%i/i915_forcewake_user", dri_base, i); |
| 134 | |
| 135 | ret = stat(mmio_data.debugfs_forcewake_path, &sb); |
| 136 | if (ret) { |
| 137 | mmio_data.debugfs_path[0] = 0; |
| 138 | mmio_data.debugfs_forcewake_path[0] = 0; |
| 139 | } else |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | return -1; |
| 144 | } |
| 145 | |
| 146 | static int |
| 147 | get_forcewake_lock(void) |
| 148 | { |
| 149 | return open(mmio_data.debugfs_forcewake_path, 0); |
| 150 | } |
| 151 | |
| 152 | static void |
| 153 | release_forcewake_lock(int fd) |
| 154 | { |
| 155 | close(fd); |
| 156 | } |
| 157 | |
Ben Widawsky | 8904d29 | 2013-02-21 22:05:33 -0800 | [diff] [blame] | 158 | /* Dumb check to see if i915 was loaded */ |
| 159 | static bool |
| 160 | i915_loaded(void) |
| 161 | { |
| 162 | struct stat sb; |
| 163 | int ret; |
| 164 | |
| 165 | ret = stat("/sys/module/i915/", &sb); |
| 166 | if (ret) { |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | assert(S_ISDIR(sb.st_mode)); |
| 171 | return true; |
| 172 | } |
| 173 | |
Ben Widawsky | cac8f8b | 2011-07-28 13:40:19 -0700 | [diff] [blame] | 174 | /* |
| 175 | * Initialize register access library. |
| 176 | * |
| 177 | * @pci_dev: pci device we're mucking with |
| 178 | * @safe: use safe register access tables |
| 179 | */ |
| 180 | int |
Ben Widawsky | abd7038 | 2011-07-28 13:42:45 -0700 | [diff] [blame] | 181 | intel_register_access_init(struct pci_device *pci_dev, int safe) |
Ben Widawsky | cac8f8b | 2011-07-28 13:40:19 -0700 | [diff] [blame] | 182 | { |
| 183 | int ret; |
| 184 | |
| 185 | /* after old API is deprecated, remove this */ |
| 186 | if (mmio == NULL) |
| 187 | intel_get_mmio(pci_dev); |
| 188 | |
| 189 | assert(mmio != NULL); |
| 190 | |
| 191 | if (mmio_data.inited) |
| 192 | return -1; |
| 193 | |
Daniel Vetter | 16e44f5 | 2013-04-03 00:22:41 +0200 | [diff] [blame] | 194 | if (intel_gen(pci_dev->device_id) >= 6) |
| 195 | goto done; |
| 196 | |
Ben Widawsky | abd7038 | 2011-07-28 13:42:45 -0700 | [diff] [blame] | 197 | mmio_data.safe = safe != 0 ? true : false; |
Ben Widawsky | 4c20525 | 2012-01-25 21:04:16 -0800 | [diff] [blame] | 198 | mmio_data.i915_devid = pci_dev->device_id; |
Daniel Vetter | 16e44f5 | 2013-04-03 00:22:41 +0200 | [diff] [blame] | 199 | if (mmio_data.safe && intel_gen(pci_dev->device_id) >= 4) |
Ben Widawsky | 4c20525 | 2012-01-25 21:04:16 -0800 | [diff] [blame] | 200 | mmio_data.map = intel_get_register_map(mmio_data.i915_devid); |
| 201 | |
Daniel Vetter | 16e44f5 | 2013-04-03 00:22:41 +0200 | [diff] [blame] | 202 | if (intel_gen(pci_dev->device_id) >= 6) |
Ben Widawsky | 4c20525 | 2012-01-25 21:04:16 -0800 | [diff] [blame] | 203 | goto done; |
Ben Widawsky | abd7038 | 2011-07-28 13:42:45 -0700 | [diff] [blame] | 204 | |
Ben Widawsky | cac8f8b | 2011-07-28 13:40:19 -0700 | [diff] [blame] | 205 | /* Find where the forcewake lock is */ |
| 206 | ret = find_debugfs_path("/sys/kernel/debug/dri"); |
| 207 | if (ret) { |
| 208 | ret = find_debugfs_path("/debug/dri"); |
| 209 | if (ret) { |
| 210 | fprintf(stderr, "Couldn't find path to dri/debugfs entry\n"); |
Ben Widawsky | 8904d29 | 2013-02-21 22:05:33 -0800 | [diff] [blame] | 211 | if (i915_loaded()) { |
| 212 | fprintf(stderr, "i915 loaded; not proceeding.\n"); |
| 213 | return ret; |
| 214 | } |
Ben Widawsky | cac8f8b | 2011-07-28 13:40:19 -0700 | [diff] [blame] | 215 | } |
Ben Widawsky | 8904d29 | 2013-02-21 22:05:33 -0800 | [diff] [blame] | 216 | mmio_data.key = FAKEKEY; |
| 217 | } else |
| 218 | mmio_data.key = get_forcewake_lock(); |
Ben Widawsky | 4c20525 | 2012-01-25 21:04:16 -0800 | [diff] [blame] | 219 | |
| 220 | done: |
Ben Widawsky | cac8f8b | 2011-07-28 13:40:19 -0700 | [diff] [blame] | 221 | mmio_data.inited++; |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | void |
| 226 | intel_register_access_fini(void) |
| 227 | { |
Ben Widawsky | 8904d29 | 2013-02-21 22:05:33 -0800 | [diff] [blame] | 228 | if (mmio_data.key && mmio_data.key != FAKEKEY) |
Ben Widawsky | 4c20525 | 2012-01-25 21:04:16 -0800 | [diff] [blame] | 229 | release_forcewake_lock(mmio_data.key); |
Ben Widawsky | cac8f8b | 2011-07-28 13:40:19 -0700 | [diff] [blame] | 230 | mmio_data.inited--; |
| 231 | } |
| 232 | |
Ben Widawsky | f60dd51 | 2013-02-21 22:05:34 -0800 | [diff] [blame] | 233 | int |
| 234 | intel_register_access_needs_wake(void) |
| 235 | { |
| 236 | return mmio_data.key == FAKEKEY; |
| 237 | } |
| 238 | |
Ben Widawsky | cac8f8b | 2011-07-28 13:40:19 -0700 | [diff] [blame] | 239 | uint32_t |
| 240 | intel_register_read(uint32_t reg) |
| 241 | { |
| 242 | struct intel_register_range *range; |
| 243 | uint32_t ret; |
| 244 | |
| 245 | assert(mmio_data.inited); |
| 246 | |
Daniel Vetter | 137f4d4 | 2012-01-24 09:37:51 +0100 | [diff] [blame] | 247 | if (intel_gen(mmio_data.i915_devid) >= 6) |
Ben Widawsky | cac8f8b | 2011-07-28 13:40:19 -0700 | [diff] [blame] | 248 | assert(mmio_data.key != -1); |
Ben Widawsky | abd7038 | 2011-07-28 13:42:45 -0700 | [diff] [blame] | 249 | |
| 250 | if (!mmio_data.safe) |
| 251 | goto read_out; |
| 252 | |
| 253 | range = intel_get_register_range(mmio_data.map, |
| 254 | reg, |
| 255 | INTEL_RANGE_READ); |
| 256 | |
| 257 | if(!range) { |
| 258 | fprintf(stderr, "Register read blocked for safety " |
| 259 | "(*0x%08x)\n", reg); |
| 260 | ret = 0xffffffff; |
| 261 | goto out; |
| 262 | } |
| 263 | |
| 264 | read_out: |
| 265 | ret = *(volatile uint32_t *)((volatile char *)mmio + reg); |
| 266 | out: |
| 267 | return ret; |
Ben Widawsky | cac8f8b | 2011-07-28 13:40:19 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | void |
| 271 | intel_register_write(uint32_t reg, uint32_t val) |
| 272 | { |
| 273 | struct intel_register_range *range; |
| 274 | |
| 275 | assert(mmio_data.inited); |
| 276 | |
Daniel Vetter | 137f4d4 | 2012-01-24 09:37:51 +0100 | [diff] [blame] | 277 | if (intel_gen(mmio_data.i915_devid) >= 6) |
Ben Widawsky | cac8f8b | 2011-07-28 13:40:19 -0700 | [diff] [blame] | 278 | assert(mmio_data.key != -1); |
| 279 | |
Ben Widawsky | abd7038 | 2011-07-28 13:42:45 -0700 | [diff] [blame] | 280 | if (!mmio_data.safe) |
| 281 | goto write_out; |
| 282 | |
| 283 | range = intel_get_register_range(mmio_data.map, |
| 284 | reg, |
| 285 | INTEL_RANGE_WRITE); |
| 286 | |
| 287 | if (!range) { |
| 288 | fprintf(stderr, "Register write blocked for safety " |
| 289 | "(*0x%08x = 0x%x)\n", reg, val); |
| 290 | } |
| 291 | |
| 292 | write_out: |
Ben Widawsky | cac8f8b | 2011-07-28 13:40:19 -0700 | [diff] [blame] | 293 | *(volatile uint32_t *)((volatile char *)mmio + reg) = val; |
| 294 | } |