Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drm/amd/amdgpu: New debugfs interface for MMIO registers (v5)

This new debugfs interface uses an IOCTL interface in order to pass
along state information like SRBM and GRBM bank switching. This
new interface also allows a full 32-bit MMIO address range which
the previous didn't. With this new design we have room to grow
the flexibility of the file as need be.

(v2): Move read/write to .read/.write, fix style, add comment
for IOCTL data structure

(v3): C style comments

(v4): use u32 in struct and remove offset variable

(v5): Drop flag clearing in op function, use 0xFFFFFFFF for broadcast
instead of 0x3FF, use mutex for op/ioctl.

Signed-off-by: Tom St Denis <tom.stdenis@amd.com>
Reviewed-by: Christian König <christian.koenig@amd.com>
Signed-off-by: Alex Deucher <alexander.deucher@amd.com>

authored by

Tom St Denis and committed by
Alex Deucher
37df9560 f9e476c5

+203 -1
+152
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c
··· 36 36 #include "amdgpu_rap.h" 37 37 #include "amdgpu_securedisplay.h" 38 38 #include "amdgpu_fw_attestation.h" 39 + #include "amdgpu_umr.h" 39 40 40 41 int amdgpu_debugfs_wait_dump(struct amdgpu_device *adev) 41 42 { ··· 278 277 size_t size, loff_t *pos) 279 278 { 280 279 return amdgpu_debugfs_process_reg_op(false, f, (char __user *)buf, size, pos); 280 + } 281 + 282 + static int amdgpu_debugfs_regs2_open(struct inode *inode, struct file *file) 283 + { 284 + struct amdgpu_debugfs_regs2_data *rd; 285 + 286 + rd = kzalloc(sizeof *rd, GFP_KERNEL); 287 + if (!rd) 288 + return -ENOMEM; 289 + rd->adev = file_inode(file)->i_private; 290 + file->private_data = rd; 291 + mutex_init(&rd->lock); 292 + 293 + return 0; 294 + } 295 + 296 + static int amdgpu_debugfs_regs2_release(struct inode *inode, struct file *file) 297 + { 298 + struct amdgpu_debugfs_regs2_data *rd = file->private_data; 299 + mutex_destroy(&rd->lock); 300 + kfree(file->private_data); 301 + return 0; 302 + } 303 + 304 + static ssize_t amdgpu_debugfs_regs2_op(struct file *f, char __user *buf, u32 offset, size_t size, int write_en) 305 + { 306 + struct amdgpu_debugfs_regs2_data *rd = f->private_data; 307 + struct amdgpu_device *adev = rd->adev; 308 + ssize_t result = 0; 309 + int r; 310 + uint32_t value; 311 + 312 + if (size & 0x3 || offset & 0x3) 313 + return -EINVAL; 314 + 315 + r = pm_runtime_get_sync(adev_to_drm(adev)->dev); 316 + if (r < 0) { 317 + pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 318 + return r; 319 + } 320 + 321 + r = amdgpu_virt_enable_access_debugfs(adev); 322 + if (r < 0) { 323 + pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 324 + return r; 325 + } 326 + 327 + mutex_lock(&rd->lock); 328 + 329 + if (rd->id.use_grbm) { 330 + if ((rd->id.grbm.sh != 0xFFFFFFFF && rd->id.grbm.sh >= adev->gfx.config.max_sh_per_se) || 331 + (rd->id.grbm.se != 0xFFFFFFFF && rd->id.grbm.se >= adev->gfx.config.max_shader_engines)) { 332 + pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 333 + pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 334 + amdgpu_virt_disable_access_debugfs(adev); 335 + mutex_unlock(&rd->lock); 336 + return -EINVAL; 337 + } 338 + mutex_lock(&adev->grbm_idx_mutex); 339 + amdgpu_gfx_select_se_sh(adev, rd->id.grbm.se, 340 + rd->id.grbm.sh, 341 + rd->id.grbm.instance); 342 + } 343 + 344 + if (rd->id.use_srbm) { 345 + mutex_lock(&adev->srbm_mutex); 346 + amdgpu_gfx_select_me_pipe_q(adev, rd->id.srbm.me, rd->id.srbm.pipe, 347 + rd->id.srbm.queue, rd->id.srbm.vmid); 348 + } 349 + 350 + if (rd->id.pg_lock) 351 + mutex_lock(&adev->pm.mutex); 352 + 353 + while (size) { 354 + if (!write_en) { 355 + value = RREG32(offset >> 2); 356 + r = put_user(value, (uint32_t *)buf); 357 + } else { 358 + r = get_user(value, (uint32_t *)buf); 359 + if (!r) 360 + amdgpu_mm_wreg_mmio_rlc(adev, offset >> 2, value); 361 + } 362 + if (r) { 363 + result = r; 364 + goto end; 365 + } 366 + offset += 4; 367 + size -= 4; 368 + result += 4; 369 + buf += 4; 370 + } 371 + end: 372 + if (rd->id.use_grbm) { 373 + amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff); 374 + mutex_unlock(&adev->grbm_idx_mutex); 375 + } 376 + 377 + if (rd->id.use_srbm) { 378 + amdgpu_gfx_select_me_pipe_q(adev, 0, 0, 0, 0); 379 + mutex_unlock(&adev->srbm_mutex); 380 + } 381 + 382 + if (rd->id.pg_lock) 383 + mutex_unlock(&adev->pm.mutex); 384 + 385 + mutex_unlock(&rd->lock); 386 + 387 + pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 388 + pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 389 + 390 + amdgpu_virt_disable_access_debugfs(adev); 391 + return result; 392 + } 393 + 394 + static long amdgpu_debugfs_regs2_ioctl(struct file *f, unsigned int cmd, unsigned long data) 395 + { 396 + struct amdgpu_debugfs_regs2_data *rd = f->private_data; 397 + int r; 398 + 399 + switch (cmd) { 400 + case AMDGPU_DEBUGFS_REGS2_IOC_SET_STATE: 401 + mutex_lock(&rd->lock); 402 + r = copy_from_user(&rd->id, (struct amdgpu_debugfs_regs2_iocdata *)data, sizeof rd->id); 403 + mutex_unlock(&rd->lock); 404 + return r ? -EINVAL : 0; 405 + default: 406 + return -EINVAL; 407 + } 408 + return 0; 409 + } 410 + 411 + static ssize_t amdgpu_debugfs_regs2_read(struct file *f, char __user *buf, size_t size, loff_t *pos) 412 + { 413 + return amdgpu_debugfs_regs2_op(f, buf, *pos, size, 0); 414 + } 415 + 416 + static ssize_t amdgpu_debugfs_regs2_write(struct file *f, const char __user *buf, size_t size, loff_t *pos) 417 + { 418 + return amdgpu_debugfs_regs2_op(f, (char __user *)buf, *pos, size, 1); 281 419 } 282 420 283 421 ··· 1231 1091 return result; 1232 1092 } 1233 1093 1094 + static const struct file_operations amdgpu_debugfs_regs2_fops = { 1095 + .owner = THIS_MODULE, 1096 + .unlocked_ioctl = amdgpu_debugfs_regs2_ioctl, 1097 + .read = amdgpu_debugfs_regs2_read, 1098 + .write = amdgpu_debugfs_regs2_write, 1099 + .open = amdgpu_debugfs_regs2_open, 1100 + .release = amdgpu_debugfs_regs2_release, 1101 + .llseek = default_llseek 1102 + }; 1103 + 1234 1104 static const struct file_operations amdgpu_debugfs_regs_fops = { 1235 1105 .owner = THIS_MODULE, 1236 1106 .read = amdgpu_debugfs_regs_read, ··· 1298 1148 1299 1149 static const struct file_operations *debugfs_regs[] = { 1300 1150 &amdgpu_debugfs_regs_fops, 1151 + &amdgpu_debugfs_regs2_fops, 1301 1152 &amdgpu_debugfs_regs_didt_fops, 1302 1153 &amdgpu_debugfs_regs_pcie_fops, 1303 1154 &amdgpu_debugfs_regs_smc_fops, ··· 1311 1160 1312 1161 static const char *debugfs_regs_names[] = { 1313 1162 "amdgpu_regs", 1163 + "amdgpu_regs2", 1314 1164 "amdgpu_regs_didt", 1315 1165 "amdgpu_regs_pcie", 1316 1166 "amdgpu_regs_smc",
-1
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.h
··· 22 22 * OTHER DEALINGS IN THE SOFTWARE. 23 23 * 24 24 */ 25 - 26 25 /* 27 26 * Debugfs 28 27 */
+51
drivers/gpu/drm/amd/amdgpu/amdgpu_umr.h
··· 1 + /* 2 + * Copyright 2021 Advanced Micro Devices, Inc. 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 shall be included in 12 + * all copies or substantial portions of the Software. 13 + * 14 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 + * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 + * OTHER DEALINGS IN THE SOFTWARE. 21 + * 22 + */ 23 + #include <linux/ioctl.h> 24 + 25 + /* 26 + * MMIO debugfs IOCTL structure 27 + */ 28 + struct amdgpu_debugfs_regs2_iocdata { 29 + __u32 use_srbm, use_grbm, pg_lock; 30 + struct { 31 + __u32 se, sh, instance; 32 + } grbm; 33 + struct { 34 + __u32 me, pipe, queue, vmid; 35 + } srbm; 36 + }; 37 + 38 + /* 39 + * MMIO debugfs state data (per file* handle) 40 + */ 41 + struct amdgpu_debugfs_regs2_data { 42 + struct amdgpu_device *adev; 43 + struct mutex lock; 44 + struct amdgpu_debugfs_regs2_iocdata id; 45 + }; 46 + 47 + enum AMDGPU_DEBUGFS_REGS2_CMDS { 48 + AMDGPU_DEBUGFS_REGS2_CMD_SET_STATE=0, 49 + }; 50 + 51 + #define AMDGPU_DEBUGFS_REGS2_IOC_SET_STATE _IOWR(0x20, AMDGPU_DEBUGFS_REGS2_CMD_SET_STATE, struct amdgpu_debugfs_regs2_iocdata)