1 1.1 riastrad /* $NetBSD: amdgpu_debugfs.c,v 1.2 2021/12/18 23:44:58 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /* 4 1.1 riastrad * Copyright 2008 Advanced Micro Devices, Inc. 5 1.1 riastrad * Copyright 2008 Red Hat Inc. 6 1.1 riastrad * Copyright 2009 Jerome Glisse. 7 1.1 riastrad * 8 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 9 1.1 riastrad * copy of this software and associated documentation files (the "Software"), 10 1.1 riastrad * to deal in the Software without restriction, including without limitation 11 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense, 12 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the 13 1.1 riastrad * Software is furnished to do so, subject to the following conditions: 14 1.1 riastrad * 15 1.1 riastrad * The above copyright notice and this permission notice shall be included in 16 1.1 riastrad * all copies or substantial portions of the Software. 17 1.1 riastrad * 18 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21 1.1 riastrad * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 22 1.1 riastrad * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 23 1.1 riastrad * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 1.1 riastrad * OTHER DEALINGS IN THE SOFTWARE. 25 1.1 riastrad * 26 1.1 riastrad */ 27 1.1 riastrad 28 1.1 riastrad #include <sys/cdefs.h> 29 1.1 riastrad __KERNEL_RCSID(0, "$NetBSD: amdgpu_debugfs.c,v 1.2 2021/12/18 23:44:58 riastradh Exp $"); 30 1.1 riastrad 31 1.1 riastrad #include <linux/kthread.h> 32 1.1 riastrad #include <linux/pci.h> 33 1.1 riastrad #include <linux/uaccess.h> 34 1.1 riastrad #include <linux/pm_runtime.h> 35 1.1 riastrad 36 1.1 riastrad #include <drm/drm_debugfs.h> 37 1.1 riastrad 38 1.1 riastrad #include "amdgpu.h" 39 1.1 riastrad 40 1.1 riastrad /** 41 1.1 riastrad * amdgpu_debugfs_add_files - Add simple debugfs entries 42 1.1 riastrad * 43 1.1 riastrad * @adev: Device to attach debugfs entries to 44 1.1 riastrad * @files: Array of function callbacks that respond to reads 45 1.1 riastrad * @nfiles: Number of callbacks to register 46 1.1 riastrad * 47 1.1 riastrad */ 48 1.1 riastrad int amdgpu_debugfs_add_files(struct amdgpu_device *adev, 49 1.1 riastrad const struct drm_info_list *files, 50 1.1 riastrad unsigned nfiles) 51 1.1 riastrad { 52 1.1 riastrad unsigned i; 53 1.1 riastrad 54 1.1 riastrad for (i = 0; i < adev->debugfs_count; i++) { 55 1.1 riastrad if (adev->debugfs[i].files == files) { 56 1.1 riastrad /* Already registered */ 57 1.1 riastrad return 0; 58 1.1 riastrad } 59 1.1 riastrad } 60 1.1 riastrad 61 1.1 riastrad i = adev->debugfs_count + 1; 62 1.1 riastrad if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) { 63 1.1 riastrad DRM_ERROR("Reached maximum number of debugfs components.\n"); 64 1.1 riastrad DRM_ERROR("Report so we increase " 65 1.1 riastrad "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n"); 66 1.1 riastrad return -EINVAL; 67 1.1 riastrad } 68 1.1 riastrad adev->debugfs[adev->debugfs_count].files = files; 69 1.1 riastrad adev->debugfs[adev->debugfs_count].num_files = nfiles; 70 1.1 riastrad adev->debugfs_count = i; 71 1.1 riastrad #if defined(CONFIG_DEBUG_FS) 72 1.1 riastrad drm_debugfs_create_files(files, nfiles, 73 1.1 riastrad adev->ddev->primary->debugfs_root, 74 1.1 riastrad adev->ddev->primary); 75 1.1 riastrad #endif 76 1.1 riastrad return 0; 77 1.1 riastrad } 78 1.1 riastrad 79 1.1 riastrad #if defined(CONFIG_DEBUG_FS) 80 1.1 riastrad 81 1.1 riastrad /** 82 1.1 riastrad * amdgpu_debugfs_process_reg_op - Handle MMIO register reads/writes 83 1.1 riastrad * 84 1.1 riastrad * @read: True if reading 85 1.1 riastrad * @f: open file handle 86 1.1 riastrad * @buf: User buffer to write/read to 87 1.1 riastrad * @size: Number of bytes to write/read 88 1.1 riastrad * @pos: Offset to seek to 89 1.1 riastrad * 90 1.1 riastrad * This debugfs entry has special meaning on the offset being sought. 91 1.1 riastrad * Various bits have different meanings: 92 1.1 riastrad * 93 1.1 riastrad * Bit 62: Indicates a GRBM bank switch is needed 94 1.1 riastrad * Bit 61: Indicates a SRBM bank switch is needed (implies bit 62 is 95 1.1 riastrad * zero) 96 1.1 riastrad * Bits 24..33: The SE or ME selector if needed 97 1.1 riastrad * Bits 34..43: The SH (or SA) or PIPE selector if needed 98 1.1 riastrad * Bits 44..53: The INSTANCE (or CU/WGP) or QUEUE selector if needed 99 1.1 riastrad * 100 1.1 riastrad * Bit 23: Indicates that the PM power gating lock should be held 101 1.1 riastrad * This is necessary to read registers that might be 102 1.1 riastrad * unreliable during a power gating transistion. 103 1.1 riastrad * 104 1.1 riastrad * The lower bits are the BYTE offset of the register to read. This 105 1.1 riastrad * allows reading multiple registers in a single call and having 106 1.1 riastrad * the returned size reflect that. 107 1.1 riastrad */ 108 1.1 riastrad static int amdgpu_debugfs_process_reg_op(bool read, struct file *f, 109 1.1 riastrad char __user *buf, size_t size, loff_t *pos) 110 1.1 riastrad { 111 1.1 riastrad struct amdgpu_device *adev = file_inode(f)->i_private; 112 1.1 riastrad ssize_t result = 0; 113 1.1 riastrad int r; 114 1.1 riastrad bool pm_pg_lock, use_bank, use_ring; 115 1.1 riastrad unsigned instance_bank, sh_bank, se_bank, me, pipe, queue, vmid; 116 1.1 riastrad 117 1.1 riastrad pm_pg_lock = use_bank = use_ring = false; 118 1.1 riastrad instance_bank = sh_bank = se_bank = me = pipe = queue = vmid = 0; 119 1.1 riastrad 120 1.1 riastrad if (size & 0x3 || *pos & 0x3 || 121 1.1 riastrad ((*pos & (1ULL << 62)) && (*pos & (1ULL << 61)))) 122 1.1 riastrad return -EINVAL; 123 1.1 riastrad 124 1.1 riastrad /* are we reading registers for which a PG lock is necessary? */ 125 1.1 riastrad pm_pg_lock = (*pos >> 23) & 1; 126 1.1 riastrad 127 1.1 riastrad if (*pos & (1ULL << 62)) { 128 1.1 riastrad se_bank = (*pos & GENMASK_ULL(33, 24)) >> 24; 129 1.1 riastrad sh_bank = (*pos & GENMASK_ULL(43, 34)) >> 34; 130 1.1 riastrad instance_bank = (*pos & GENMASK_ULL(53, 44)) >> 44; 131 1.1 riastrad 132 1.1 riastrad if (se_bank == 0x3FF) 133 1.1 riastrad se_bank = 0xFFFFFFFF; 134 1.1 riastrad if (sh_bank == 0x3FF) 135 1.1 riastrad sh_bank = 0xFFFFFFFF; 136 1.1 riastrad if (instance_bank == 0x3FF) 137 1.1 riastrad instance_bank = 0xFFFFFFFF; 138 1.1 riastrad use_bank = true; 139 1.1 riastrad } else if (*pos & (1ULL << 61)) { 140 1.1 riastrad 141 1.1 riastrad me = (*pos & GENMASK_ULL(33, 24)) >> 24; 142 1.1 riastrad pipe = (*pos & GENMASK_ULL(43, 34)) >> 34; 143 1.1 riastrad queue = (*pos & GENMASK_ULL(53, 44)) >> 44; 144 1.1 riastrad vmid = (*pos & GENMASK_ULL(58, 54)) >> 54; 145 1.1 riastrad 146 1.1 riastrad use_ring = true; 147 1.1 riastrad } else { 148 1.1 riastrad use_bank = use_ring = false; 149 1.1 riastrad } 150 1.1 riastrad 151 1.1 riastrad *pos &= (1UL << 22) - 1; 152 1.1 riastrad 153 1.1 riastrad r = pm_runtime_get_sync(adev->ddev->dev); 154 1.1 riastrad if (r < 0) 155 1.1 riastrad return r; 156 1.1 riastrad 157 1.1 riastrad if (use_bank) { 158 1.1 riastrad if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) || 159 1.1 riastrad (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines)) { 160 1.1 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 161 1.1 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 162 1.1 riastrad return -EINVAL; 163 1.1 riastrad } 164 1.1 riastrad mutex_lock(&adev->grbm_idx_mutex); 165 1.1 riastrad amdgpu_gfx_select_se_sh(adev, se_bank, 166 1.1 riastrad sh_bank, instance_bank); 167 1.1 riastrad } else if (use_ring) { 168 1.1 riastrad mutex_lock(&adev->srbm_mutex); 169 1.1 riastrad amdgpu_gfx_select_me_pipe_q(adev, me, pipe, queue, vmid); 170 1.1 riastrad } 171 1.1 riastrad 172 1.1 riastrad if (pm_pg_lock) 173 1.1 riastrad mutex_lock(&adev->pm.mutex); 174 1.1 riastrad 175 1.1 riastrad while (size) { 176 1.1 riastrad uint32_t value; 177 1.1 riastrad 178 1.1 riastrad if (read) { 179 1.1 riastrad value = RREG32(*pos >> 2); 180 1.1 riastrad r = put_user(value, (uint32_t *)buf); 181 1.1 riastrad } else { 182 1.1 riastrad r = get_user(value, (uint32_t *)buf); 183 1.1 riastrad if (!r) 184 1.1 riastrad WREG32(*pos >> 2, value); 185 1.1 riastrad } 186 1.1 riastrad if (r) { 187 1.1 riastrad result = r; 188 1.1 riastrad goto end; 189 1.1 riastrad } 190 1.1 riastrad 191 1.1 riastrad result += 4; 192 1.1 riastrad buf += 4; 193 1.1 riastrad *pos += 4; 194 1.1 riastrad size -= 4; 195 1.1 riastrad } 196 1.1 riastrad 197 1.1 riastrad end: 198 1.1 riastrad if (use_bank) { 199 1.1 riastrad amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff); 200 1.1 riastrad mutex_unlock(&adev->grbm_idx_mutex); 201 1.1 riastrad } else if (use_ring) { 202 1.1 riastrad amdgpu_gfx_select_me_pipe_q(adev, 0, 0, 0, 0); 203 1.1 riastrad mutex_unlock(&adev->srbm_mutex); 204 1.1 riastrad } 205 1.1 riastrad 206 1.1 riastrad if (pm_pg_lock) 207 1.1 riastrad mutex_unlock(&adev->pm.mutex); 208 1.1 riastrad 209 1.1 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 210 1.1 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 211 1.1 riastrad 212 1.1 riastrad return result; 213 1.1 riastrad } 214 1.1 riastrad 215 1.1 riastrad /** 216 1.1 riastrad * amdgpu_debugfs_regs_read - Callback for reading MMIO registers 217 1.1 riastrad */ 218 1.1 riastrad static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf, 219 1.1 riastrad size_t size, loff_t *pos) 220 1.1 riastrad { 221 1.1 riastrad return amdgpu_debugfs_process_reg_op(true, f, buf, size, pos); 222 1.1 riastrad } 223 1.1 riastrad 224 1.1 riastrad /** 225 1.1 riastrad * amdgpu_debugfs_regs_write - Callback for writing MMIO registers 226 1.1 riastrad */ 227 1.1 riastrad static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf, 228 1.1 riastrad size_t size, loff_t *pos) 229 1.1 riastrad { 230 1.1 riastrad return amdgpu_debugfs_process_reg_op(false, f, (char __user *)buf, size, pos); 231 1.1 riastrad } 232 1.1 riastrad 233 1.1 riastrad 234 1.1 riastrad /** 235 1.1 riastrad * amdgpu_debugfs_regs_pcie_read - Read from a PCIE register 236 1.1 riastrad * 237 1.1 riastrad * @f: open file handle 238 1.1 riastrad * @buf: User buffer to store read data in 239 1.1 riastrad * @size: Number of bytes to read 240 1.1 riastrad * @pos: Offset to seek to 241 1.1 riastrad * 242 1.1 riastrad * The lower bits are the BYTE offset of the register to read. This 243 1.1 riastrad * allows reading multiple registers in a single call and having 244 1.1 riastrad * the returned size reflect that. 245 1.1 riastrad */ 246 1.1 riastrad static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf, 247 1.1 riastrad size_t size, loff_t *pos) 248 1.1 riastrad { 249 1.1 riastrad struct amdgpu_device *adev = file_inode(f)->i_private; 250 1.1 riastrad ssize_t result = 0; 251 1.1 riastrad int r; 252 1.1 riastrad 253 1.1 riastrad if (size & 0x3 || *pos & 0x3) 254 1.1 riastrad return -EINVAL; 255 1.1 riastrad 256 1.1 riastrad r = pm_runtime_get_sync(adev->ddev->dev); 257 1.1 riastrad if (r < 0) 258 1.1 riastrad return r; 259 1.1 riastrad 260 1.1 riastrad while (size) { 261 1.1 riastrad uint32_t value; 262 1.1 riastrad 263 1.1 riastrad value = RREG32_PCIE(*pos >> 2); 264 1.1 riastrad r = put_user(value, (uint32_t *)buf); 265 1.1 riastrad if (r) { 266 1.1 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 267 1.1 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 268 1.1 riastrad return r; 269 1.1 riastrad } 270 1.1 riastrad 271 1.1 riastrad result += 4; 272 1.1 riastrad buf += 4; 273 1.1 riastrad *pos += 4; 274 1.1 riastrad size -= 4; 275 1.1 riastrad } 276 1.1 riastrad 277 1.1 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 278 1.1 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 279 1.1 riastrad 280 1.1 riastrad return result; 281 1.1 riastrad } 282 1.1 riastrad 283 1.1 riastrad /** 284 1.1 riastrad * amdgpu_debugfs_regs_pcie_write - Write to a PCIE register 285 1.1 riastrad * 286 1.1 riastrad * @f: open file handle 287 1.1 riastrad * @buf: User buffer to write data from 288 1.1 riastrad * @size: Number of bytes to write 289 1.1 riastrad * @pos: Offset to seek to 290 1.1 riastrad * 291 1.1 riastrad * The lower bits are the BYTE offset of the register to write. This 292 1.1 riastrad * allows writing multiple registers in a single call and having 293 1.1 riastrad * the returned size reflect that. 294 1.1 riastrad */ 295 1.1 riastrad static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf, 296 1.1 riastrad size_t size, loff_t *pos) 297 1.1 riastrad { 298 1.1 riastrad struct amdgpu_device *adev = file_inode(f)->i_private; 299 1.1 riastrad ssize_t result = 0; 300 1.1 riastrad int r; 301 1.1 riastrad 302 1.1 riastrad if (size & 0x3 || *pos & 0x3) 303 1.1 riastrad return -EINVAL; 304 1.1 riastrad 305 1.1 riastrad r = pm_runtime_get_sync(adev->ddev->dev); 306 1.1 riastrad if (r < 0) 307 1.1 riastrad return r; 308 1.1 riastrad 309 1.1 riastrad while (size) { 310 1.1 riastrad uint32_t value; 311 1.1 riastrad 312 1.1 riastrad r = get_user(value, (uint32_t *)buf); 313 1.1 riastrad if (r) { 314 1.1 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 315 1.1 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 316 1.1 riastrad return r; 317 1.1 riastrad } 318 1.1 riastrad 319 1.1 riastrad WREG32_PCIE(*pos >> 2, value); 320 1.1 riastrad 321 1.1 riastrad result += 4; 322 1.1 riastrad buf += 4; 323 1.1 riastrad *pos += 4; 324 1.1 riastrad size -= 4; 325 1.1 riastrad } 326 1.1 riastrad 327 1.1 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 328 1.1 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 329 1.1 riastrad 330 1.1 riastrad return result; 331 1.1 riastrad } 332 1.1 riastrad 333 1.1 riastrad /** 334 1.1 riastrad * amdgpu_debugfs_regs_didt_read - Read from a DIDT register 335 1.1 riastrad * 336 1.1 riastrad * @f: open file handle 337 1.1 riastrad * @buf: User buffer to store read data in 338 1.1 riastrad * @size: Number of bytes to read 339 1.1 riastrad * @pos: Offset to seek to 340 1.1 riastrad * 341 1.1 riastrad * The lower bits are the BYTE offset of the register to read. This 342 1.1 riastrad * allows reading multiple registers in a single call and having 343 1.1 riastrad * the returned size reflect that. 344 1.1 riastrad */ 345 1.1 riastrad static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf, 346 1.1 riastrad size_t size, loff_t *pos) 347 1.1 riastrad { 348 1.1 riastrad struct amdgpu_device *adev = file_inode(f)->i_private; 349 1.1 riastrad ssize_t result = 0; 350 1.1 riastrad int r; 351 1.1 riastrad 352 1.1 riastrad if (size & 0x3 || *pos & 0x3) 353 1.1 riastrad return -EINVAL; 354 1.1 riastrad 355 1.1 riastrad r = pm_runtime_get_sync(adev->ddev->dev); 356 1.1 riastrad if (r < 0) 357 1.1 riastrad return r; 358 1.1 riastrad 359 1.1 riastrad while (size) { 360 1.1 riastrad uint32_t value; 361 1.1 riastrad 362 1.1 riastrad value = RREG32_DIDT(*pos >> 2); 363 1.1 riastrad r = put_user(value, (uint32_t *)buf); 364 1.1 riastrad if (r) { 365 1.1 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 366 1.1 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 367 1.1 riastrad return r; 368 1.1 riastrad } 369 1.1 riastrad 370 1.1 riastrad result += 4; 371 1.1 riastrad buf += 4; 372 1.1 riastrad *pos += 4; 373 1.1 riastrad size -= 4; 374 1.1 riastrad } 375 1.1 riastrad 376 1.1 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 377 1.1 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 378 1.1 riastrad 379 1.1 riastrad return result; 380 1.1 riastrad } 381 1.1 riastrad 382 1.1 riastrad /** 383 1.1 riastrad * amdgpu_debugfs_regs_didt_write - Write to a DIDT register 384 1.1 riastrad * 385 1.1 riastrad * @f: open file handle 386 1.1 riastrad * @buf: User buffer to write data from 387 1.1 riastrad * @size: Number of bytes to write 388 1.1 riastrad * @pos: Offset to seek to 389 1.1 riastrad * 390 1.1 riastrad * The lower bits are the BYTE offset of the register to write. This 391 1.1 riastrad * allows writing multiple registers in a single call and having 392 1.1 riastrad * the returned size reflect that. 393 1.1 riastrad */ 394 1.1 riastrad static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf, 395 1.1 riastrad size_t size, loff_t *pos) 396 1.1 riastrad { 397 1.1 riastrad struct amdgpu_device *adev = file_inode(f)->i_private; 398 1.1 riastrad ssize_t result = 0; 399 1.1 riastrad int r; 400 1.1 riastrad 401 1.1 riastrad if (size & 0x3 || *pos & 0x3) 402 1.1 riastrad return -EINVAL; 403 1.1 riastrad 404 1.1 riastrad r = pm_runtime_get_sync(adev->ddev->dev); 405 1.1 riastrad if (r < 0) 406 1.1 riastrad return r; 407 1.1 riastrad 408 1.1 riastrad while (size) { 409 1.1 riastrad uint32_t value; 410 1.1 riastrad 411 1.1 riastrad r = get_user(value, (uint32_t *)buf); 412 1.1 riastrad if (r) { 413 1.1 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 414 1.1 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 415 1.1 riastrad return r; 416 1.1 riastrad } 417 1.1 riastrad 418 1.1 riastrad WREG32_DIDT(*pos >> 2, value); 419 1.1 riastrad 420 1.1 riastrad result += 4; 421 1.1 riastrad buf += 4; 422 1.1 riastrad *pos += 4; 423 1.1 riastrad size -= 4; 424 1.1 riastrad } 425 1.1 riastrad 426 1.1 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 427 1.1 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 428 1.1 riastrad 429 1.1 riastrad return result; 430 1.1 riastrad } 431 1.1 riastrad 432 1.1 riastrad /** 433 1.1 riastrad * amdgpu_debugfs_regs_smc_read - Read from a SMC register 434 1.1 riastrad * 435 1.1 riastrad * @f: open file handle 436 1.1 riastrad * @buf: User buffer to store read data in 437 1.1 riastrad * @size: Number of bytes to read 438 1.1 riastrad * @pos: Offset to seek to 439 1.1 riastrad * 440 1.1 riastrad * The lower bits are the BYTE offset of the register to read. This 441 1.1 riastrad * allows reading multiple registers in a single call and having 442 1.1 riastrad * the returned size reflect that. 443 1.1 riastrad */ 444 1.1 riastrad static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf, 445 1.1 riastrad size_t size, loff_t *pos) 446 1.1 riastrad { 447 1.1 riastrad struct amdgpu_device *adev = file_inode(f)->i_private; 448 1.1 riastrad ssize_t result = 0; 449 1.1 riastrad int r; 450 1.1 riastrad 451 1.1 riastrad if (size & 0x3 || *pos & 0x3) 452 1.1 riastrad return -EINVAL; 453 1.1 riastrad 454 1.1 riastrad r = pm_runtime_get_sync(adev->ddev->dev); 455 1.1 riastrad if (r < 0) 456 1.1 riastrad return r; 457 1.1 riastrad 458 1.1 riastrad while (size) { 459 1.1 riastrad uint32_t value; 460 1.1 riastrad 461 1.1 riastrad value = RREG32_SMC(*pos); 462 1.1 riastrad r = put_user(value, (uint32_t *)buf); 463 1.1 riastrad if (r) { 464 1.1 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 465 1.1 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 466 1.1 riastrad return r; 467 1.1 riastrad } 468 1.1 riastrad 469 1.1 riastrad result += 4; 470 1.1 riastrad buf += 4; 471 1.1 riastrad *pos += 4; 472 1.1 riastrad size -= 4; 473 1.1 riastrad } 474 1.1 riastrad 475 1.1 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 476 1.1 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 477 1.1 riastrad 478 1.1 riastrad return result; 479 1.1 riastrad } 480 1.1 riastrad 481 1.1 riastrad /** 482 1.1 riastrad * amdgpu_debugfs_regs_smc_write - Write to a SMC register 483 1.1 riastrad * 484 1.1 riastrad * @f: open file handle 485 1.1 riastrad * @buf: User buffer to write data from 486 1.1 riastrad * @size: Number of bytes to write 487 1.1 riastrad * @pos: Offset to seek to 488 1.1 riastrad * 489 1.1 riastrad * The lower bits are the BYTE offset of the register to write. This 490 1.1 riastrad * allows writing multiple registers in a single call and having 491 1.1 riastrad * the returned size reflect that. 492 1.1 riastrad */ 493 1.1 riastrad static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf, 494 1.1 riastrad size_t size, loff_t *pos) 495 1.1 riastrad { 496 1.1 riastrad struct amdgpu_device *adev = file_inode(f)->i_private; 497 1.1 riastrad ssize_t result = 0; 498 1.1 riastrad int r; 499 1.1 riastrad 500 1.1 riastrad if (size & 0x3 || *pos & 0x3) 501 1.1 riastrad return -EINVAL; 502 1.1 riastrad 503 1.1 riastrad r = pm_runtime_get_sync(adev->ddev->dev); 504 1.1 riastrad if (r < 0) 505 1.1 riastrad return r; 506 1.1 riastrad 507 1.1 riastrad while (size) { 508 1.1 riastrad uint32_t value; 509 1.1 riastrad 510 1.1 riastrad r = get_user(value, (uint32_t *)buf); 511 1.1 riastrad if (r) { 512 1.1 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 513 1.1 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 514 1.1 riastrad return r; 515 1.1 riastrad } 516 1.1 riastrad 517 1.1 riastrad WREG32_SMC(*pos, value); 518 1.1 riastrad 519 1.1 riastrad result += 4; 520 1.1 riastrad buf += 4; 521 1.1 riastrad *pos += 4; 522 1.1 riastrad size -= 4; 523 1.1 riastrad } 524 1.1 riastrad 525 1.1 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 526 1.1 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 527 1.1 riastrad 528 1.1 riastrad return result; 529 1.1 riastrad } 530 1.1 riastrad 531 1.1 riastrad /** 532 1.1 riastrad * amdgpu_debugfs_gca_config_read - Read from gfx config data 533 1.1 riastrad * 534 1.1 riastrad * @f: open file handle 535 1.1 riastrad * @buf: User buffer to store read data in 536 1.1 riastrad * @size: Number of bytes to read 537 1.1 riastrad * @pos: Offset to seek to 538 1.1 riastrad * 539 1.1 riastrad * This file is used to access configuration data in a somewhat 540 1.1 riastrad * stable fashion. The format is a series of DWORDs with the first 541 1.1 riastrad * indicating which revision it is. New content is appended to the 542 1.1 riastrad * end so that older software can still read the data. 543 1.1 riastrad */ 544 1.1 riastrad 545 1.1 riastrad static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf, 546 1.1 riastrad size_t size, loff_t *pos) 547 1.1 riastrad { 548 1.1 riastrad struct amdgpu_device *adev = file_inode(f)->i_private; 549 1.1 riastrad ssize_t result = 0; 550 1.1 riastrad int r; 551 1.1 riastrad uint32_t *config, no_regs = 0; 552 1.1 riastrad 553 1.1 riastrad if (size & 0x3 || *pos & 0x3) 554 1.1 riastrad return -EINVAL; 555 1.1 riastrad 556 1.1 riastrad config = kmalloc_array(256, sizeof(*config), GFP_KERNEL); 557 1.1 riastrad if (!config) 558 1.1 riastrad return -ENOMEM; 559 1.1 riastrad 560 1.1 riastrad /* version, increment each time something is added */ 561 1.1 riastrad config[no_regs++] = 3; 562 1.1 riastrad config[no_regs++] = adev->gfx.config.max_shader_engines; 563 1.1 riastrad config[no_regs++] = adev->gfx.config.max_tile_pipes; 564 1.1 riastrad config[no_regs++] = adev->gfx.config.max_cu_per_sh; 565 1.1 riastrad config[no_regs++] = adev->gfx.config.max_sh_per_se; 566 1.1 riastrad config[no_regs++] = adev->gfx.config.max_backends_per_se; 567 1.1 riastrad config[no_regs++] = adev->gfx.config.max_texture_channel_caches; 568 1.1 riastrad config[no_regs++] = adev->gfx.config.max_gprs; 569 1.1 riastrad config[no_regs++] = adev->gfx.config.max_gs_threads; 570 1.1 riastrad config[no_regs++] = adev->gfx.config.max_hw_contexts; 571 1.1 riastrad config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend; 572 1.1 riastrad config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend; 573 1.1 riastrad config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size; 574 1.1 riastrad config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size; 575 1.1 riastrad config[no_regs++] = adev->gfx.config.num_tile_pipes; 576 1.1 riastrad config[no_regs++] = adev->gfx.config.backend_enable_mask; 577 1.1 riastrad config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes; 578 1.1 riastrad config[no_regs++] = adev->gfx.config.mem_row_size_in_kb; 579 1.1 riastrad config[no_regs++] = adev->gfx.config.shader_engine_tile_size; 580 1.1 riastrad config[no_regs++] = adev->gfx.config.num_gpus; 581 1.1 riastrad config[no_regs++] = adev->gfx.config.multi_gpu_tile_size; 582 1.1 riastrad config[no_regs++] = adev->gfx.config.mc_arb_ramcfg; 583 1.1 riastrad config[no_regs++] = adev->gfx.config.gb_addr_config; 584 1.1 riastrad config[no_regs++] = adev->gfx.config.num_rbs; 585 1.1 riastrad 586 1.1 riastrad /* rev==1 */ 587 1.1 riastrad config[no_regs++] = adev->rev_id; 588 1.1 riastrad config[no_regs++] = adev->pg_flags; 589 1.1 riastrad config[no_regs++] = adev->cg_flags; 590 1.1 riastrad 591 1.1 riastrad /* rev==2 */ 592 1.1 riastrad config[no_regs++] = adev->family; 593 1.1 riastrad config[no_regs++] = adev->external_rev_id; 594 1.1 riastrad 595 1.1 riastrad /* rev==3 */ 596 1.1 riastrad config[no_regs++] = adev->pdev->device; 597 1.1 riastrad config[no_regs++] = adev->pdev->revision; 598 1.1 riastrad config[no_regs++] = adev->pdev->subsystem_device; 599 1.1 riastrad config[no_regs++] = adev->pdev->subsystem_vendor; 600 1.1 riastrad 601 1.1 riastrad while (size && (*pos < no_regs * 4)) { 602 1.1 riastrad uint32_t value; 603 1.1 riastrad 604 1.1 riastrad value = config[*pos >> 2]; 605 1.1 riastrad r = put_user(value, (uint32_t *)buf); 606 1.1 riastrad if (r) { 607 1.1 riastrad kfree(config); 608 1.1 riastrad return r; 609 1.1 riastrad } 610 1.1 riastrad 611 1.1 riastrad result += 4; 612 1.1 riastrad buf += 4; 613 1.1 riastrad *pos += 4; 614 1.1 riastrad size -= 4; 615 1.1 riastrad } 616 1.1 riastrad 617 1.1 riastrad kfree(config); 618 1.1 riastrad return result; 619 1.1 riastrad } 620 1.1 riastrad 621 1.1 riastrad /** 622 1.1 riastrad * amdgpu_debugfs_sensor_read - Read from the powerplay sensors 623 1.1 riastrad * 624 1.1 riastrad * @f: open file handle 625 1.1 riastrad * @buf: User buffer to store read data in 626 1.1 riastrad * @size: Number of bytes to read 627 1.1 riastrad * @pos: Offset to seek to 628 1.1 riastrad * 629 1.1 riastrad * The offset is treated as the BYTE address of one of the sensors 630 1.1 riastrad * enumerated in amd/include/kgd_pp_interface.h under the 631 1.1 riastrad * 'amd_pp_sensors' enumeration. For instance to read the UVD VCLK 632 1.1 riastrad * you would use the offset 3 * 4 = 12. 633 1.1 riastrad */ 634 1.1 riastrad static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf, 635 1.1 riastrad size_t size, loff_t *pos) 636 1.1 riastrad { 637 1.1 riastrad struct amdgpu_device *adev = file_inode(f)->i_private; 638 1.1 riastrad int idx, x, outsize, r, valuesize; 639 1.1 riastrad uint32_t values[16]; 640 1.1 riastrad 641 1.1 riastrad if (size & 3 || *pos & 0x3) 642 1.1 riastrad return -EINVAL; 643 1.1 riastrad 644 1.1 riastrad if (!adev->pm.dpm_enabled) 645 1.1 riastrad return -EINVAL; 646 1.1 riastrad 647 1.1 riastrad /* convert offset to sensor number */ 648 1.1 riastrad idx = *pos >> 2; 649 1.1 riastrad 650 1.1 riastrad valuesize = sizeof(values); 651 1.1 riastrad 652 1.1 riastrad r = pm_runtime_get_sync(adev->ddev->dev); 653 1.1 riastrad if (r < 0) 654 1.1 riastrad return r; 655 1.1 riastrad 656 1.1 riastrad r = amdgpu_dpm_read_sensor(adev, idx, &values[0], &valuesize); 657 1.1 riastrad 658 1.1 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 659 1.1 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 660 1.1 riastrad 661 1.1 riastrad if (r) 662 1.1 riastrad return r; 663 1.1 riastrad 664 1.1 riastrad if (size > valuesize) 665 1.1 riastrad return -EINVAL; 666 1.1 riastrad 667 1.1 riastrad outsize = 0; 668 1.1 riastrad x = 0; 669 1.1 riastrad if (!r) { 670 1.1 riastrad while (size) { 671 1.1 riastrad r = put_user(values[x++], (int32_t *)buf); 672 1.1 riastrad buf += 4; 673 1.1 riastrad size -= 4; 674 1.1 riastrad outsize += 4; 675 1.1 riastrad } 676 1.1 riastrad } 677 1.1 riastrad 678 1.1 riastrad return !r ? outsize : r; 679 1.1 riastrad } 680 1.1 riastrad 681 1.1 riastrad /** amdgpu_debugfs_wave_read - Read WAVE STATUS data 682 1.1 riastrad * 683 1.1 riastrad * @f: open file handle 684 1.1 riastrad * @buf: User buffer to store read data in 685 1.1 riastrad * @size: Number of bytes to read 686 1.1 riastrad * @pos: Offset to seek to 687 1.1 riastrad * 688 1.1 riastrad * The offset being sought changes which wave that the status data 689 1.1 riastrad * will be returned for. The bits are used as follows: 690 1.1 riastrad * 691 1.1 riastrad * Bits 0..6: Byte offset into data 692 1.1 riastrad * Bits 7..14: SE selector 693 1.1 riastrad * Bits 15..22: SH/SA selector 694 1.1 riastrad * Bits 23..30: CU/{WGP+SIMD} selector 695 1.1 riastrad * Bits 31..36: WAVE ID selector 696 1.1 riastrad * Bits 37..44: SIMD ID selector 697 1.1 riastrad * 698 1.1 riastrad * The returned data begins with one DWORD of version information 699 1.1 riastrad * Followed by WAVE STATUS registers relevant to the GFX IP version 700 1.1 riastrad * being used. See gfx_v8_0_read_wave_data() for an example output. 701 1.1 riastrad */ 702 1.1 riastrad static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf, 703 1.1 riastrad size_t size, loff_t *pos) 704 1.1 riastrad { 705 1.1 riastrad struct amdgpu_device *adev = f->f_inode->i_private; 706 1.1 riastrad int r, x; 707 1.1 riastrad ssize_t result=0; 708 1.1 riastrad uint32_t offset, se, sh, cu, wave, simd, data[32]; 709 1.1 riastrad 710 1.1 riastrad if (size & 3 || *pos & 3) 711 1.1 riastrad return -EINVAL; 712 1.1 riastrad 713 1.1 riastrad /* decode offset */ 714 1.1 riastrad offset = (*pos & GENMASK_ULL(6, 0)); 715 1.1 riastrad se = (*pos & GENMASK_ULL(14, 7)) >> 7; 716 1.1 riastrad sh = (*pos & GENMASK_ULL(22, 15)) >> 15; 717 1.1 riastrad cu = (*pos & GENMASK_ULL(30, 23)) >> 23; 718 1.1 riastrad wave = (*pos & GENMASK_ULL(36, 31)) >> 31; 719 1.1 riastrad simd = (*pos & GENMASK_ULL(44, 37)) >> 37; 720 1.1 riastrad 721 1.1 riastrad r = pm_runtime_get_sync(adev->ddev->dev); 722 1.1 riastrad if (r < 0) 723 1.1 riastrad return r; 724 1.1 riastrad 725 1.1 riastrad /* switch to the specific se/sh/cu */ 726 1.1 riastrad mutex_lock(&adev->grbm_idx_mutex); 727 1.1 riastrad amdgpu_gfx_select_se_sh(adev, se, sh, cu); 728 1.1 riastrad 729 1.1 riastrad x = 0; 730 1.1 riastrad if (adev->gfx.funcs->read_wave_data) 731 1.1 riastrad adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x); 732 1.1 riastrad 733 1.1 riastrad amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF); 734 1.1 riastrad mutex_unlock(&adev->grbm_idx_mutex); 735 1.1 riastrad 736 1.1 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 737 1.1 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 738 1.1 riastrad 739 1.1 riastrad if (!x) 740 1.1 riastrad return -EINVAL; 741 1.1 riastrad 742 1.1 riastrad while (size && (offset < x * 4)) { 743 1.1 riastrad uint32_t value; 744 1.1 riastrad 745 1.1 riastrad value = data[offset >> 2]; 746 1.1 riastrad r = put_user(value, (uint32_t *)buf); 747 1.1 riastrad if (r) 748 1.1 riastrad return r; 749 1.1 riastrad 750 1.1 riastrad result += 4; 751 1.1 riastrad buf += 4; 752 1.1 riastrad offset += 4; 753 1.1 riastrad size -= 4; 754 1.1 riastrad } 755 1.1 riastrad 756 1.1 riastrad return result; 757 1.1 riastrad } 758 1.1 riastrad 759 1.1 riastrad /** amdgpu_debugfs_gpr_read - Read wave gprs 760 1.1 riastrad * 761 1.1 riastrad * @f: open file handle 762 1.1 riastrad * @buf: User buffer to store read data in 763 1.1 riastrad * @size: Number of bytes to read 764 1.1 riastrad * @pos: Offset to seek to 765 1.1 riastrad * 766 1.1 riastrad * The offset being sought changes which wave that the status data 767 1.1 riastrad * will be returned for. The bits are used as follows: 768 1.1 riastrad * 769 1.1 riastrad * Bits 0..11: Byte offset into data 770 1.1 riastrad * Bits 12..19: SE selector 771 1.1 riastrad * Bits 20..27: SH/SA selector 772 1.1 riastrad * Bits 28..35: CU/{WGP+SIMD} selector 773 1.1 riastrad * Bits 36..43: WAVE ID selector 774 1.1 riastrad * Bits 37..44: SIMD ID selector 775 1.1 riastrad * Bits 52..59: Thread selector 776 1.1 riastrad * Bits 60..61: Bank selector (VGPR=0,SGPR=1) 777 1.1 riastrad * 778 1.1 riastrad * The return data comes from the SGPR or VGPR register bank for 779 1.1 riastrad * the selected operational unit. 780 1.1 riastrad */ 781 1.1 riastrad static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf, 782 1.1 riastrad size_t size, loff_t *pos) 783 1.1 riastrad { 784 1.1 riastrad struct amdgpu_device *adev = f->f_inode->i_private; 785 1.1 riastrad int r; 786 1.1 riastrad ssize_t result = 0; 787 1.1 riastrad uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data; 788 1.1 riastrad 789 1.1 riastrad if (size & 3 || *pos & 3) 790 1.1 riastrad return -EINVAL; 791 1.1 riastrad 792 1.1 riastrad /* decode offset */ 793 1.1 riastrad offset = *pos & GENMASK_ULL(11, 0); 794 1.1 riastrad se = (*pos & GENMASK_ULL(19, 12)) >> 12; 795 1.1 riastrad sh = (*pos & GENMASK_ULL(27, 20)) >> 20; 796 1.1 riastrad cu = (*pos & GENMASK_ULL(35, 28)) >> 28; 797 1.1 riastrad wave = (*pos & GENMASK_ULL(43, 36)) >> 36; 798 1.1 riastrad simd = (*pos & GENMASK_ULL(51, 44)) >> 44; 799 1.1 riastrad thread = (*pos & GENMASK_ULL(59, 52)) >> 52; 800 1.1 riastrad bank = (*pos & GENMASK_ULL(61, 60)) >> 60; 801 1.1 riastrad 802 1.1 riastrad data = kcalloc(1024, sizeof(*data), GFP_KERNEL); 803 1.1 riastrad if (!data) 804 1.1 riastrad return -ENOMEM; 805 1.1 riastrad 806 1.1 riastrad r = pm_runtime_get_sync(adev->ddev->dev); 807 1.1 riastrad if (r < 0) 808 1.1 riastrad return r; 809 1.1 riastrad 810 1.1 riastrad /* switch to the specific se/sh/cu */ 811 1.1 riastrad mutex_lock(&adev->grbm_idx_mutex); 812 1.1 riastrad amdgpu_gfx_select_se_sh(adev, se, sh, cu); 813 1.1 riastrad 814 1.1 riastrad if (bank == 0) { 815 1.1 riastrad if (adev->gfx.funcs->read_wave_vgprs) 816 1.1 riastrad adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data); 817 1.1 riastrad } else { 818 1.1 riastrad if (adev->gfx.funcs->read_wave_sgprs) 819 1.1 riastrad adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data); 820 1.1 riastrad } 821 1.1 riastrad 822 1.1 riastrad amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF); 823 1.1 riastrad mutex_unlock(&adev->grbm_idx_mutex); 824 1.1 riastrad 825 1.1 riastrad pm_runtime_mark_last_busy(adev->ddev->dev); 826 1.1 riastrad pm_runtime_put_autosuspend(adev->ddev->dev); 827 1.1 riastrad 828 1.1 riastrad while (size) { 829 1.1 riastrad uint32_t value; 830 1.1 riastrad 831 1.1 riastrad value = data[offset++]; 832 1.1 riastrad r = put_user(value, (uint32_t *)buf); 833 1.1 riastrad if (r) { 834 1.1 riastrad result = r; 835 1.1 riastrad goto err; 836 1.1 riastrad } 837 1.1 riastrad 838 1.1 riastrad result += 4; 839 1.1 riastrad buf += 4; 840 1.1 riastrad size -= 4; 841 1.1 riastrad } 842 1.1 riastrad 843 1.1 riastrad err: 844 1.1 riastrad kfree(data); 845 1.1 riastrad return result; 846 1.1 riastrad } 847 1.1 riastrad 848 1.1 riastrad static const struct file_operations amdgpu_debugfs_regs_fops = { 849 1.1 riastrad .owner = THIS_MODULE, 850 1.1 riastrad .read = amdgpu_debugfs_regs_read, 851 1.1 riastrad .write = amdgpu_debugfs_regs_write, 852 1.1 riastrad .llseek = default_llseek 853 1.1 riastrad }; 854 1.1 riastrad static const struct file_operations amdgpu_debugfs_regs_didt_fops = { 855 1.1 riastrad .owner = THIS_MODULE, 856 1.1 riastrad .read = amdgpu_debugfs_regs_didt_read, 857 1.1 riastrad .write = amdgpu_debugfs_regs_didt_write, 858 1.1 riastrad .llseek = default_llseek 859 1.1 riastrad }; 860 1.1 riastrad static const struct file_operations amdgpu_debugfs_regs_pcie_fops = { 861 1.1 riastrad .owner = THIS_MODULE, 862 1.1 riastrad .read = amdgpu_debugfs_regs_pcie_read, 863 1.1 riastrad .write = amdgpu_debugfs_regs_pcie_write, 864 1.1 riastrad .llseek = default_llseek 865 1.1 riastrad }; 866 1.1 riastrad static const struct file_operations amdgpu_debugfs_regs_smc_fops = { 867 1.1 riastrad .owner = THIS_MODULE, 868 1.1 riastrad .read = amdgpu_debugfs_regs_smc_read, 869 1.1 riastrad .write = amdgpu_debugfs_regs_smc_write, 870 1.1 riastrad .llseek = default_llseek 871 1.1 riastrad }; 872 1.1 riastrad 873 1.1 riastrad static const struct file_operations amdgpu_debugfs_gca_config_fops = { 874 1.1 riastrad .owner = THIS_MODULE, 875 1.1 riastrad .read = amdgpu_debugfs_gca_config_read, 876 1.1 riastrad .llseek = default_llseek 877 1.1 riastrad }; 878 1.1 riastrad 879 1.1 riastrad static const struct file_operations amdgpu_debugfs_sensors_fops = { 880 1.1 riastrad .owner = THIS_MODULE, 881 1.1 riastrad .read = amdgpu_debugfs_sensor_read, 882 1.1 riastrad .llseek = default_llseek 883 1.1 riastrad }; 884 1.1 riastrad 885 1.1 riastrad static const struct file_operations amdgpu_debugfs_wave_fops = { 886 1.1 riastrad .owner = THIS_MODULE, 887 1.1 riastrad .read = amdgpu_debugfs_wave_read, 888 1.1 riastrad .llseek = default_llseek 889 1.1 riastrad }; 890 1.1 riastrad static const struct file_operations amdgpu_debugfs_gpr_fops = { 891 1.1 riastrad .owner = THIS_MODULE, 892 1.1 riastrad .read = amdgpu_debugfs_gpr_read, 893 1.1 riastrad .llseek = default_llseek 894 1.1 riastrad }; 895 1.1 riastrad 896 1.1 riastrad static const struct file_operations *debugfs_regs[] = { 897 1.1 riastrad &amdgpu_debugfs_regs_fops, 898 1.1 riastrad &amdgpu_debugfs_regs_didt_fops, 899 1.1 riastrad &amdgpu_debugfs_regs_pcie_fops, 900 1.1 riastrad &amdgpu_debugfs_regs_smc_fops, 901 1.1 riastrad &amdgpu_debugfs_gca_config_fops, 902 1.1 riastrad &amdgpu_debugfs_sensors_fops, 903 1.1 riastrad &amdgpu_debugfs_wave_fops, 904 1.1 riastrad &amdgpu_debugfs_gpr_fops, 905 1.1 riastrad }; 906 1.1 riastrad 907 1.1 riastrad static const char *debugfs_regs_names[] = { 908 1.1 riastrad "amdgpu_regs", 909 1.1 riastrad "amdgpu_regs_didt", 910 1.1 riastrad "amdgpu_regs_pcie", 911 1.1 riastrad "amdgpu_regs_smc", 912 1.1 riastrad "amdgpu_gca_config", 913 1.1 riastrad "amdgpu_sensors", 914 1.1 riastrad "amdgpu_wave", 915 1.1 riastrad "amdgpu_gpr", 916 1.1 riastrad }; 917 1.1 riastrad 918 1.1 riastrad /** 919 1.1 riastrad * amdgpu_debugfs_regs_init - Initialize debugfs entries that provide 920 1.1 riastrad * register access. 921 1.1 riastrad * 922 1.1 riastrad * @adev: The device to attach the debugfs entries to 923 1.1 riastrad */ 924 1.1 riastrad int amdgpu_debugfs_regs_init(struct amdgpu_device *adev) 925 1.1 riastrad { 926 1.1 riastrad struct drm_minor *minor = adev->ddev->primary; 927 1.1 riastrad struct dentry *ent, *root = minor->debugfs_root; 928 1.1 riastrad unsigned int i; 929 1.1 riastrad 930 1.1 riastrad for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) { 931 1.1 riastrad ent = debugfs_create_file(debugfs_regs_names[i], 932 1.1 riastrad S_IFREG | S_IRUGO, root, 933 1.1 riastrad adev, debugfs_regs[i]); 934 1.1 riastrad if (!i && !IS_ERR_OR_NULL(ent)) 935 1.1 riastrad i_size_write(ent->d_inode, adev->rmmio_size); 936 1.1 riastrad adev->debugfs_regs[i] = ent; 937 1.1 riastrad } 938 1.1 riastrad 939 1.1 riastrad return 0; 940 1.1 riastrad } 941 1.1 riastrad 942 1.1 riastrad void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) 943 1.1 riastrad { 944 1.1 riastrad unsigned i; 945 1.1 riastrad 946 1.1 riastrad for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) { 947 1.1 riastrad if (adev->debugfs_regs[i]) { 948 1.1 riastrad debugfs_remove(adev->debugfs_regs[i]); 949 1.1 riastrad adev->debugfs_regs[i] = NULL; 950 1.1 riastrad } 951 1.1 riastrad } 952 1.1 riastrad } 953 1.1 riastrad 954 1.1 riastrad static int amdgpu_debugfs_test_ib(struct seq_file *m, void *data) 955 1.1 riastrad { 956 1.1 riastrad struct drm_info_node *node = (struct drm_info_node *) m->private; 957 1.1 riastrad struct drm_device *dev = node->minor->dev; 958 1.1 riastrad struct amdgpu_device *adev = dev->dev_private; 959 1.1 riastrad int r = 0, i; 960 1.1 riastrad 961 1.1 riastrad r = pm_runtime_get_sync(dev->dev); 962 1.1 riastrad if (r < 0) 963 1.1 riastrad return r; 964 1.1 riastrad 965 1.1 riastrad /* Avoid accidently unparking the sched thread during GPU reset */ 966 1.1 riastrad mutex_lock(&adev->lock_reset); 967 1.1 riastrad 968 1.1 riastrad /* hold on the scheduler */ 969 1.1 riastrad for (i = 0; i < AMDGPU_MAX_RINGS; i++) { 970 1.1 riastrad struct amdgpu_ring *ring = adev->rings[i]; 971 1.1 riastrad 972 1.1 riastrad if (!ring || !ring->sched.thread) 973 1.1 riastrad continue; 974 1.1 riastrad kthread_park(ring->sched.thread); 975 1.1 riastrad } 976 1.1 riastrad 977 1.1 riastrad seq_printf(m, "run ib test:\n"); 978 1.1 riastrad r = amdgpu_ib_ring_tests(adev); 979 1.1 riastrad if (r) 980 1.1 riastrad seq_printf(m, "ib ring tests failed (%d).\n", r); 981 1.1 riastrad else 982 1.1 riastrad seq_printf(m, "ib ring tests passed.\n"); 983 1.1 riastrad 984 1.1 riastrad /* go on the scheduler */ 985 1.1 riastrad for (i = 0; i < AMDGPU_MAX_RINGS; i++) { 986 1.1 riastrad struct amdgpu_ring *ring = adev->rings[i]; 987 1.1 riastrad 988 1.1 riastrad if (!ring || !ring->sched.thread) 989 1.1 riastrad continue; 990 1.1 riastrad kthread_unpark(ring->sched.thread); 991 1.1 riastrad } 992 1.1 riastrad 993 1.1 riastrad mutex_unlock(&adev->lock_reset); 994 1.1 riastrad 995 1.1 riastrad pm_runtime_mark_last_busy(dev->dev); 996 1.1 riastrad pm_runtime_put_autosuspend(dev->dev); 997 1.1 riastrad 998 1.1 riastrad return 0; 999 1.1 riastrad } 1000 1.1 riastrad 1001 1.1 riastrad static int amdgpu_debugfs_get_vbios_dump(struct seq_file *m, void *data) 1002 1.1 riastrad { 1003 1.1 riastrad struct drm_info_node *node = (struct drm_info_node *) m->private; 1004 1.1 riastrad struct drm_device *dev = node->minor->dev; 1005 1.1 riastrad struct amdgpu_device *adev = dev->dev_private; 1006 1.1 riastrad 1007 1.1 riastrad seq_write(m, adev->bios, adev->bios_size); 1008 1.1 riastrad return 0; 1009 1.1 riastrad } 1010 1.1 riastrad 1011 1.1 riastrad static int amdgpu_debugfs_evict_vram(struct seq_file *m, void *data) 1012 1.1 riastrad { 1013 1.1 riastrad struct drm_info_node *node = (struct drm_info_node *)m->private; 1014 1.1 riastrad struct drm_device *dev = node->minor->dev; 1015 1.1 riastrad struct amdgpu_device *adev = dev->dev_private; 1016 1.1 riastrad int r; 1017 1.1 riastrad 1018 1.1 riastrad r = pm_runtime_get_sync(dev->dev); 1019 1.1 riastrad if (r < 0) 1020 1.1 riastrad return r; 1021 1.1 riastrad 1022 1.1 riastrad seq_printf(m, "(%d)\n", amdgpu_bo_evict_vram(adev)); 1023 1.1 riastrad 1024 1.1 riastrad pm_runtime_mark_last_busy(dev->dev); 1025 1.1 riastrad pm_runtime_put_autosuspend(dev->dev); 1026 1.1 riastrad 1027 1.1 riastrad return 0; 1028 1.1 riastrad } 1029 1.1 riastrad 1030 1.1 riastrad static int amdgpu_debugfs_evict_gtt(struct seq_file *m, void *data) 1031 1.1 riastrad { 1032 1.1 riastrad struct drm_info_node *node = (struct drm_info_node *)m->private; 1033 1.1 riastrad struct drm_device *dev = node->minor->dev; 1034 1.1 riastrad struct amdgpu_device *adev = dev->dev_private; 1035 1.1 riastrad int r; 1036 1.1 riastrad 1037 1.1 riastrad r = pm_runtime_get_sync(dev->dev); 1038 1.1 riastrad if (r < 0) 1039 1.1 riastrad return r; 1040 1.1 riastrad 1041 1.1 riastrad seq_printf(m, "(%d)\n", ttm_bo_evict_mm(&adev->mman.bdev, TTM_PL_TT)); 1042 1.1 riastrad 1043 1.1 riastrad pm_runtime_mark_last_busy(dev->dev); 1044 1.1 riastrad pm_runtime_put_autosuspend(dev->dev); 1045 1.1 riastrad 1046 1.1 riastrad return 0; 1047 1.1 riastrad } 1048 1.1 riastrad 1049 1.1 riastrad static const struct drm_info_list amdgpu_debugfs_list[] = { 1050 1.1 riastrad {"amdgpu_vbios", amdgpu_debugfs_get_vbios_dump}, 1051 1.1 riastrad {"amdgpu_test_ib", &amdgpu_debugfs_test_ib}, 1052 1.1 riastrad {"amdgpu_evict_vram", &amdgpu_debugfs_evict_vram}, 1053 1.1 riastrad {"amdgpu_evict_gtt", &amdgpu_debugfs_evict_gtt}, 1054 1.1 riastrad }; 1055 1.1 riastrad 1056 1.1 riastrad static void amdgpu_ib_preempt_fences_swap(struct amdgpu_ring *ring, 1057 1.1 riastrad struct dma_fence **fences) 1058 1.1 riastrad { 1059 1.1 riastrad struct amdgpu_fence_driver *drv = &ring->fence_drv; 1060 1.1 riastrad uint32_t sync_seq, last_seq; 1061 1.1 riastrad 1062 1.1 riastrad last_seq = atomic_read(&ring->fence_drv.last_seq); 1063 1.1 riastrad sync_seq = ring->fence_drv.sync_seq; 1064 1.1 riastrad 1065 1.1 riastrad last_seq &= drv->num_fences_mask; 1066 1.1 riastrad sync_seq &= drv->num_fences_mask; 1067 1.1 riastrad 1068 1.1 riastrad do { 1069 1.1 riastrad struct dma_fence *fence, **ptr; 1070 1.1 riastrad 1071 1.1 riastrad ++last_seq; 1072 1.1 riastrad last_seq &= drv->num_fences_mask; 1073 1.1 riastrad ptr = &drv->fences[last_seq]; 1074 1.1 riastrad 1075 1.1 riastrad fence = rcu_dereference_protected(*ptr, 1); 1076 1.1 riastrad RCU_INIT_POINTER(*ptr, NULL); 1077 1.1 riastrad 1078 1.1 riastrad if (!fence) 1079 1.1 riastrad continue; 1080 1.1 riastrad 1081 1.1 riastrad fences[last_seq] = fence; 1082 1.1 riastrad 1083 1.1 riastrad } while (last_seq != sync_seq); 1084 1.1 riastrad } 1085 1.1 riastrad 1086 1.1 riastrad static void amdgpu_ib_preempt_signal_fences(struct dma_fence **fences, 1087 1.1 riastrad int length) 1088 1.1 riastrad { 1089 1.1 riastrad int i; 1090 1.1 riastrad struct dma_fence *fence; 1091 1.1 riastrad 1092 1.1 riastrad for (i = 0; i < length; i++) { 1093 1.1 riastrad fence = fences[i]; 1094 1.1 riastrad if (!fence) 1095 1.1 riastrad continue; 1096 1.1 riastrad dma_fence_signal(fence); 1097 1.1 riastrad dma_fence_put(fence); 1098 1.1 riastrad } 1099 1.1 riastrad } 1100 1.1 riastrad 1101 1.1 riastrad static void amdgpu_ib_preempt_job_recovery(struct drm_gpu_scheduler *sched) 1102 1.1 riastrad { 1103 1.1 riastrad struct drm_sched_job *s_job; 1104 1.1 riastrad struct dma_fence *fence; 1105 1.1 riastrad 1106 1.1 riastrad spin_lock(&sched->job_list_lock); 1107 1.1 riastrad list_for_each_entry(s_job, &sched->ring_mirror_list, node) { 1108 1.1 riastrad fence = sched->ops->run_job(s_job); 1109 1.1 riastrad dma_fence_put(fence); 1110 1.1 riastrad } 1111 1.1 riastrad spin_unlock(&sched->job_list_lock); 1112 1.1 riastrad } 1113 1.1 riastrad 1114 1.1 riastrad static void amdgpu_ib_preempt_mark_partial_job(struct amdgpu_ring *ring) 1115 1.1 riastrad { 1116 1.1 riastrad struct amdgpu_job *job; 1117 1.1 riastrad struct drm_sched_job *s_job; 1118 1.1 riastrad uint32_t preempt_seq; 1119 1.1 riastrad struct dma_fence *fence, **ptr; 1120 1.1 riastrad struct amdgpu_fence_driver *drv = &ring->fence_drv; 1121 1.1 riastrad struct drm_gpu_scheduler *sched = &ring->sched; 1122 1.1 riastrad 1123 1.1 riastrad if (ring->funcs->type != AMDGPU_RING_TYPE_GFX) 1124 1.1 riastrad return; 1125 1.1 riastrad 1126 1.1 riastrad preempt_seq = le32_to_cpu(*(drv->cpu_addr + 2)); 1127 1.1 riastrad if (preempt_seq <= atomic_read(&drv->last_seq)) 1128 1.1 riastrad return; 1129 1.1 riastrad 1130 1.1 riastrad preempt_seq &= drv->num_fences_mask; 1131 1.1 riastrad ptr = &drv->fences[preempt_seq]; 1132 1.1 riastrad fence = rcu_dereference_protected(*ptr, 1); 1133 1.1 riastrad 1134 1.1 riastrad spin_lock(&sched->job_list_lock); 1135 1.1 riastrad list_for_each_entry(s_job, &sched->ring_mirror_list, node) { 1136 1.1 riastrad job = to_amdgpu_job(s_job); 1137 1.1 riastrad if (job->fence == fence) 1138 1.1 riastrad /* mark the job as preempted */ 1139 1.1 riastrad job->preemption_status |= AMDGPU_IB_PREEMPTED; 1140 1.1 riastrad } 1141 1.1 riastrad spin_unlock(&sched->job_list_lock); 1142 1.1 riastrad } 1143 1.1 riastrad 1144 1.1 riastrad static int amdgpu_debugfs_ib_preempt(void *data, u64 val) 1145 1.1 riastrad { 1146 1.1 riastrad int r, resched, length; 1147 1.1 riastrad struct amdgpu_ring *ring; 1148 1.1 riastrad struct dma_fence **fences = NULL; 1149 1.1 riastrad struct amdgpu_device *adev = (struct amdgpu_device *)data; 1150 1.1 riastrad 1151 1.1 riastrad if (val >= AMDGPU_MAX_RINGS) 1152 1.1 riastrad return -EINVAL; 1153 1.1 riastrad 1154 1.1 riastrad ring = adev->rings[val]; 1155 1.1 riastrad 1156 1.1 riastrad if (!ring || !ring->funcs->preempt_ib || !ring->sched.thread) 1157 1.1 riastrad return -EINVAL; 1158 1.1 riastrad 1159 1.1 riastrad /* the last preemption failed */ 1160 1.1 riastrad if (ring->trail_seq != le32_to_cpu(*ring->trail_fence_cpu_addr)) 1161 1.1 riastrad return -EBUSY; 1162 1.1 riastrad 1163 1.1 riastrad length = ring->fence_drv.num_fences_mask + 1; 1164 1.1 riastrad fences = kcalloc(length, sizeof(void *), GFP_KERNEL); 1165 1.1 riastrad if (!fences) 1166 1.1 riastrad return -ENOMEM; 1167 1.1 riastrad 1168 1.1 riastrad /* Avoid accidently unparking the sched thread during GPU reset */ 1169 1.1 riastrad mutex_lock(&adev->lock_reset); 1170 1.1 riastrad 1171 1.1 riastrad /* stop the scheduler */ 1172 1.1 riastrad kthread_park(ring->sched.thread); 1173 1.1 riastrad 1174 1.1 riastrad resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev); 1175 1.1 riastrad 1176 1.1 riastrad /* preempt the IB */ 1177 1.1 riastrad r = amdgpu_ring_preempt_ib(ring); 1178 1.1 riastrad if (r) { 1179 1.1 riastrad DRM_WARN("failed to preempt ring %d\n", ring->idx); 1180 1.1 riastrad goto failure; 1181 1.1 riastrad } 1182 1.1 riastrad 1183 1.1 riastrad amdgpu_fence_process(ring); 1184 1.1 riastrad 1185 1.1 riastrad if (atomic_read(&ring->fence_drv.last_seq) != 1186 1.1 riastrad ring->fence_drv.sync_seq) { 1187 1.1 riastrad DRM_INFO("ring %d was preempted\n", ring->idx); 1188 1.1 riastrad 1189 1.1 riastrad amdgpu_ib_preempt_mark_partial_job(ring); 1190 1.1 riastrad 1191 1.1 riastrad /* swap out the old fences */ 1192 1.1 riastrad amdgpu_ib_preempt_fences_swap(ring, fences); 1193 1.1 riastrad 1194 1.1 riastrad amdgpu_fence_driver_force_completion(ring); 1195 1.1 riastrad 1196 1.1 riastrad /* resubmit unfinished jobs */ 1197 1.1 riastrad amdgpu_ib_preempt_job_recovery(&ring->sched); 1198 1.1 riastrad 1199 1.1 riastrad /* wait for jobs finished */ 1200 1.1 riastrad amdgpu_fence_wait_empty(ring); 1201 1.1 riastrad 1202 1.1 riastrad /* signal the old fences */ 1203 1.1 riastrad amdgpu_ib_preempt_signal_fences(fences, length); 1204 1.1 riastrad } 1205 1.1 riastrad 1206 1.1 riastrad failure: 1207 1.1 riastrad /* restart the scheduler */ 1208 1.1 riastrad kthread_unpark(ring->sched.thread); 1209 1.1 riastrad 1210 1.1 riastrad mutex_unlock(&adev->lock_reset); 1211 1.1 riastrad 1212 1.1 riastrad ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched); 1213 1.1 riastrad 1214 1.1 riastrad kfree(fences); 1215 1.1 riastrad 1216 1.1 riastrad return 0; 1217 1.1 riastrad } 1218 1.1 riastrad 1219 1.1 riastrad DEFINE_SIMPLE_ATTRIBUTE(fops_ib_preempt, NULL, 1220 1.1 riastrad amdgpu_debugfs_ib_preempt, "%llu\n"); 1221 1.1 riastrad 1222 1.1 riastrad int amdgpu_debugfs_init(struct amdgpu_device *adev) 1223 1.1 riastrad { 1224 1.1 riastrad adev->debugfs_preempt = 1225 1.1 riastrad debugfs_create_file("amdgpu_preempt_ib", 0600, 1226 1.1 riastrad adev->ddev->primary->debugfs_root, adev, 1227 1.1 riastrad &fops_ib_preempt); 1228 1.1 riastrad if (!(adev->debugfs_preempt)) { 1229 1.1 riastrad DRM_ERROR("unable to create amdgpu_preempt_ib debugsfs file\n"); 1230 1.1 riastrad return -EIO; 1231 1.1 riastrad } 1232 1.1 riastrad 1233 1.1 riastrad return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_list, 1234 1.1 riastrad ARRAY_SIZE(amdgpu_debugfs_list)); 1235 1.1 riastrad } 1236 1.1 riastrad 1237 1.1 riastrad void amdgpu_debugfs_preempt_cleanup(struct amdgpu_device *adev) 1238 1.1 riastrad { 1239 1.1 riastrad debugfs_remove(adev->debugfs_preempt); 1240 1.1 riastrad } 1241 1.1 riastrad 1242 1.1 riastrad #else 1243 1.1 riastrad int amdgpu_debugfs_init(struct amdgpu_device *adev) 1244 1.1 riastrad { 1245 1.1 riastrad return 0; 1246 1.1 riastrad } 1247 1.1 riastrad void amdgpu_debugfs_preempt_cleanup(struct amdgpu_device *adev) { } 1248 1.1 riastrad int amdgpu_debugfs_regs_init(struct amdgpu_device *adev) 1249 1.1 riastrad { 1250 1.1 riastrad return 0; 1251 1.1 riastrad } 1252 1.1 riastrad void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { } 1253 1.1 riastrad #endif 1254