From bfb4dcf0e78954c0163f3a06a5a088c4d1b437a8 Mon Sep 17 00:00:00 2001 From: Benjamin Hadad IV Date: Thu, 13 Jul 2023 12:13:17 -0400 Subject: This commit is to update the repo for display during a meeting. - Added an Ampere version of the device info data. - Added Ampere versions of auxillary functions. - Modified display functions to accommodate Ampere data. - Made other various small modifications. --- device_info_procfs.c | 140 ++++++++++++++++++++++++++++++++++++++++++++++----- nvdebug.h | 37 ++++++-------- nvdebug_entry.c | 41 +++++++++++++-- 3 files changed, 179 insertions(+), 39 deletions(-) diff --git a/device_info_procfs.c b/device_info_procfs.c index 1fc0586..b1e58b1 100644 --- a/device_info_procfs.c +++ b/device_info_procfs.c @@ -31,7 +31,7 @@ struct file_operations nvdebug_read_reg32_file_ops = { // Called to start or resume a sequence. Prior to 4.19, *pos is unreliable. // Initializes iterator `idx` state and returns it. Ends sequence on NULL. -static void* device_info_file_seq_start(struct seq_file *s, loff_t *pos) { +static void* device_info_file_seq_start_previous(struct seq_file *s, loff_t *pos) { static int idx; // If start of sequence, reset `idx` if (*pos == 0) @@ -42,9 +42,21 @@ static void* device_info_file_seq_start(struct seq_file *s, loff_t *pos) { return &idx; } +static void* device_info_file_seq_start_ampere(struct seq_file *s, loff_t *pos) { + static int idx; + // If start of sequence, reset `idx` + if (*pos == 0) + idx = 0; + struct nvdebug_state *g = &g_nvdebug_state[seq2gpuidx(s)]; + // Number of possible info entries is fixed, and list is sparse + if (idx >= (nvdebug_readl(g, 0x0224fc) >> 20)) + return NULL; + return &idx; +} + // Steps to next record. Returns new value of `idx`. // Calls show() on non-NULL return -static void* device_info_file_seq_next(struct seq_file *s, void *idx, +static void* device_info_file_seq_next_previous(struct seq_file *s, void *idx, loff_t *pos) { (*pos)++; // Required by seq interface // Number of possible info entries is fixed, and list is sparse @@ -53,12 +65,50 @@ static void* device_info_file_seq_next(struct seq_file *s, void *idx, return idx; } +// Steps to next record. Returns new value of `idx`. +// Calls show() on non-NULL return +static void* device_info_file_seq_next_ampere(struct seq_file *s, void *idx, + loff_t *pos) { + (*pos)++; // Required by seq interface + // Number of possible info entries is fixed, and list is sparse + struct nvdebug_state *g = &g_nvdebug_state[seq2gpuidx(s)]; + if ((*(int*)idx)++ >= (nvdebug_readl(g, 0x0224fc) >> 20)) + return NULL; + return idx; +} +/* +// Steps to next record on Ampere GPUs. Returns new value of `idx`. +static void* device_info_file_seq_next_ampere(struct seq_file *s, void *idx, + loff_t *pos) { + (*pos)++; // Required by seq interface + // Number of possible info entries is fixed, and list is sparse + while(1) { + struct nvdebug_state *g = &g_nvdebug_state[seq2gpuidx(s)]; + if ((*(int*)idx)++ >= (nvdebug_readl(g, 0x0224fc) >> 20)) + return NULL; + ptop_device_info_t curr_info; + curr_info.raw = nvdebug_readl(g, NV_PTOP_DEVICE_INFO_AMPERE(*(int*)idx)); + if(!curr_info.raw && !*info_type) continue; + (*info_type)++; + break; + } + while(1) { + struct nvdebug_state *g = &g_nvdebug_state[seq2gpuidx(s)]; + if ((*(int*)idx)++ >= (nvdebug_readl(g, 0x0224fc) >> 20)) + return NULL; + ptop_device_info_t curr_info; + curr_info.raw = nvdebug_readl(g, NV_PTOP_DEVICE_INFO_AMPERE(*(int*)idx)); + if(curr_info.raw & 0x80000000) continue; + break; + } + return idx; +} +*/ // Print info at index *idx. Returns non-zero on error. -static int device_info_file_seq_show(struct seq_file *s, void *idx) { +static int device_info_file_seq_show_previous(struct seq_file *s, void *idx) { ptop_device_info_t curr_info; struct nvdebug_state *g = &g_nvdebug_state[seq2gpuidx(s)]; - - curr_info.raw = nvdebug_readl(g, NV_PTOP_DEVICE_INFO(*(int*)idx)); + curr_info.raw = nvdebug_readl(g, NV_PTOP_DEVICE_INFO_PREVIOUS(*(int*)idx)); // Check for read errors if (curr_info.raw == -1) return -EIO; @@ -104,24 +154,88 @@ static int device_info_file_seq_show(struct seq_file *s, void *idx) { return 0; } +// Print info at index *idx for Ampere GPUs. Returns non-zero on error. +static int device_info_file_seq_show_ampere(struct seq_file *s, void *idx) { + ptop_device_info_t curr_info; + struct nvdebug_state *g = &g_nvdebug_state[seq2gpuidx(s)]; + curr_info.raw = nvdebug_readl(g, NV_PTOP_DEVICE_INFO_AMPERE(*(int*)idx)); + // Check for read errors + if (curr_info.raw == -1) + return -EIO; + int info_type = -1; + if(curr_info.raw) { + if(*(int*)idx < 1 || !nvdebug_readl(g, NV_PTOP_DEVICE_INFO_AMPERE(*(int*)idx) - 1)) info_type = 0; + if(*(int*)idx < 2 || !nvdebug_readl(g, NV_PTOP_DEVICE_INFO_AMPERE(*(int*)idx) - 2)) info_type = 1; + if(*(int*)idx < 3 || !nvdebug_readl(g, NV_PTOP_DEVICE_INFO_AMPERE(*(int*)idx) - 3)) info_type = 2; + } + // Parse and print the data + switch(info_type) { + case 0: + seq_printf(s, "| instance %d\n", curr_info.inst_id_ampere); + seq_printf(s, "| Fault ID: %3d\n", curr_info.fault_id_ampere); + seq_printf(s, "| Engine Type: %2d (", curr_info.engine_type_ampere); + if (curr_info.engine_type_ampere < ENGINE_TYPES_LEN) + seq_printf(s, "%s)\n", ENGINE_TYPES_NAMES[curr_info.engine_type_ampere]); + else + seq_printf(s, "Unknown Engine, introduced post-Ampere)\n"); + break; + case 1: + seq_printf(s, "| BAR0 Base %#.8x\n", curr_info.pri_base_ampere << 12); + seq_printf(s, "| Reset ID: %2d\n", curr_info.reset_enum_ampere); + break; + case 2: + seq_printf(s, "| Host's Engine ID: %2d\n", curr_info.engine_enum_ampere); + seq_printf(s, "| Runlist ID: %2d\n", curr_info.runlist_enum_ampere); + break; + default: + // Device info records are sparse, so skip unset or unknown ones + return 0; + } + + // Draw a line between each device entry + if (!curr_info.has_next_entry_ampere) { + seq_printf(s, "+---------------------+\n"); + } + return 0; +} + + static void device_info_file_seq_stop(struct seq_file *s, void *idx) { // No cleanup needed } -static const struct seq_operations device_info_file_seq_ops = { - .start = device_info_file_seq_start, - .next = device_info_file_seq_next, +static const struct seq_operations device_info_file_seq_ops_previous = { + .start = device_info_file_seq_start_previous, + .next = device_info_file_seq_next_previous, .stop = device_info_file_seq_stop, - .show = device_info_file_seq_show, + .show = device_info_file_seq_show_previous, +}; + +static const struct seq_operations device_info_file_seq_ops_ampere = { + .start = device_info_file_seq_start_ampere, + .next = device_info_file_seq_next_ampere, + .stop = device_info_file_seq_stop, + .show = device_info_file_seq_show_ampere, }; -static int device_info_file_open(struct inode *inode, struct file *f) { - return seq_open(f, &device_info_file_seq_ops); +static int device_info_file_open_previous(struct inode *inode, struct file *f) { + return seq_open(f, &device_info_file_seq_ops_previous); } -struct file_operations device_info_file_ops = { - .open = device_info_file_open, +static int device_info_file_open_ampere(struct inode *inode, struct file *f) { + return seq_open(f, &device_info_file_seq_ops_ampere); +} + +struct file_operations device_info_file_ops_previous = { + .open = device_info_file_open_previous, .read = seq_read, .llseek = seq_lseek, .release = seq_release, }; + +struct file_operations device_info_file_ops_ampere = { + .open = device_info_file_open_ampere, + .read = seq_read, + .llseek = seq_lseek, + .release = seq_release, +}; diff --git a/nvdebug.h b/nvdebug.h index f6e057e..3ccdcfe 100644 --- a/nvdebug.h +++ b/nvdebug.h @@ -554,34 +554,30 @@ static const char* const ENGINE_TYPES_NAMES[ENGINE_TYPES_LEN] = { See dev_top.ref.txt of NVIDIA's open-gpu-doc for more info. */ -#ifdef NV_BUILD_FOR_AMPERE -#define NV_PTOP_DEVICE_INFO(i) (0x00022800+(i)*4) +#define NV_PTOP_DEVICE_INFO_AMPERE(i) (0x00022800+(i)*4) +#define NV_PTOP_DEVICE_INFO_PREVIOUS(i) (0x00022700+(i)*4) #define NV_PTOP_DEVICE_INFO__SIZE_1 64 typedef union { struct { - uint32_t fault_id:7; - uint32_t padding0:9; - uint32_t inst_id:4; - uint32_t padding1:4; - enum ENGINE_TYPES engine_type:7; - bool has_next_entry:1; + uint32_t fault_id_ampere:7; + uint32_t padding0_ampere:9; + uint32_t inst_id_ampere:4; + uint32_t padding1_ampere:4; + enum ENGINE_TYPES engine_type_ampere:7; + bool has_next_entry_ampere:1; } __attribute__((packed)); struct { - uint32_t reset_enum:5; - uint32_t padding2:7; - uint32_t pri_base:12; + uint32_t reset_enum_ampere:5; + uint32_t padding2_ampere:7; + uint32_t pri_base_ampere:12; + uint32_t padding3_ampere:8; } __attribute__((packed)); struct { - uint32_t engine_enum:2; - uint32_t padding3:4; - uint32_t runlist_enum:14; + uint32_t engine_enum_ampere:2; + uint32_t padding4_ampere:4; + uint32_t runlist_enum_ampere:14; + uint32_t padding5_ampere:12; } __attribute__((packed)); - uint32_t raw; -} ptop_device_info_t; -#else -#define NV_PTOP_DEVICE_INFO(i) (0x00022700+(i)*4) -#define NV_PTOP_DEVICE_INFO__SIZE_1 64 -typedef union { // DATA type fields struct { enum DEVICE_INFO_TYPE info_type:2; @@ -619,7 +615,6 @@ typedef union { } __attribute__((packed)); uint32_t raw; } ptop_device_info_t; -#endif #define NV_PTOP_SCAL_NUM_GPCS 0x00022430 #define NV_PTOP_SCAL_NUM_TPC_PER_GPC 0x00022434 diff --git a/nvdebug_entry.c b/nvdebug_entry.c index 8e8266c..d82c648 100644 --- a/nvdebug_entry.c +++ b/nvdebug_entry.c @@ -23,12 +23,14 @@ extern struct file_operations preempt_tsg_file_ops; extern struct file_operations disable_channel_file_ops; extern struct file_operations enable_channel_file_ops; extern struct file_operations switch_to_tsg_file_ops; -extern struct file_operations device_info_file_ops; +extern struct file_operations device_info_file_ops_previous; +extern struct file_operations device_info_file_ops_ampere; extern struct file_operations nvdebug_read_reg32_file_ops; // Bus types are global symbols in the kernel extern struct bus_type platform_bus_type; struct nvdebug_state g_nvdebug_state[NVDEBUG_MAX_DEVICES]; +int g_architectures[NVDEBUG_MAX_DEVICES]; unsigned int g_nvdebug_devices = 0; // Starting in Kernel 5.6, proc_ops is required instead of file_operations @@ -123,6 +125,7 @@ int probe_and_cache_device(void) { return -EADDRNOTAVAIL; } g_nvdebug_state[i].chip_id = ids.chip_id; + g_architectures[i] = ids.architecture; printk(KERN_INFO "[nvdebug] Chip ID %x (architecture %s) detected on PCI bus and initialized.", ids.chip_id, ARCH2NAME(ids.architecture)); // TEMP @@ -138,7 +141,7 @@ int probe_and_cache_device(void) { } // Create files `/proc/gpu#/runlist#`, world readable -int create_runlist_files(int device_id, struct proc_dir_entry *dir) { +int create_runlist_files_previous(int device_id, struct proc_dir_entry *dir) { ptop_device_info_t info; struct proc_dir_entry *rl_entry; int i, rl_id; @@ -148,7 +151,7 @@ int create_runlist_files(int device_id, struct proc_dir_entry *dir) { // registers. Runlists are always numbered sequentially, so we just have // to find the highest-valued one and add 1 to get the number of runlists. for (i = 0; i < NV_PTOP_DEVICE_INFO__SIZE_1; i++) { - info.raw = nvdebug_readl(&g_nvdebug_state[device_id], NV_PTOP_DEVICE_INFO(i)); + info.raw = nvdebug_readl(&g_nvdebug_state[device_id], NV_PTOP_DEVICE_INFO_PREVIOUS(i)); if (info.info_type != INFO_TYPE_ENUM || !info.runlist_is_valid) continue; if (info.runlist_enum > max_rl_id) @@ -167,6 +170,34 @@ int create_runlist_files(int device_id, struct proc_dir_entry *dir) { return 0; } +// Create files `/proc/gpu#/runlist#`, world readable +int create_runlist_files_ampere(int device_id, struct proc_dir_entry *dir) { + ptop_device_info_t info; + struct proc_dir_entry *rl_entry; + int i, rl_id; + char runlist_name[12]; + int max_rl_id = 0; // Always at least one runlist + // Figure out how many runlists there are by checking the device info + // registers. Runlists are always numbered sequentially, so we just have + // to find the highest-valued one and add 1 to get the number of runlists. + for (i = 0; i < (nvdebug_readl(&g_nvdebug_state[device_id], 0x0224fc) >> 20); i++) { + info.raw = nvdebug_readl(&g_nvdebug_state[device_id], NV_PTOP_DEVICE_INFO_AMPERE(i)); + if (info.runlist_enum_ampere > max_rl_id) + max_rl_id = info.runlist_enum; + } + // Create files to read each runlist. The read handling code looks at the + // PDE_DATA associated with the file to determine what the runlist ID is. + for (rl_id = 0; rl_id <= max_rl_id; rl_id++) { + snprintf(runlist_name, 12, "runlist%d", rl_id); + rl_entry = proc_create_data( + runlist_name, 0444, dir, compat_ops(&runlist_file_ops), + (void*)(uintptr_t)rl_id); + if (!rl_entry) + return -ENOMEM; + } + return 0; +} + // Create files /proc/gpu# // TODO: Don't run this on unsupported GPUs int create_tpc_mask_files(int device_id, struct proc_dir_entry *dir) { @@ -212,7 +243,7 @@ int __init nvdebug_init(void) { if (!(dir = proc_mkdir_data(device_id_str, 0555, NULL, (void*)device_id))) goto out_nomem; // Create files `/proc/gpu#/runlist#`, world readable - rl_create_err = create_runlist_files(device_id, dir); + rl_create_err = g_architectures[device_id] == 0x17 ? create_runlist_files_ampere(device_id, dir) : create_runlist_files_previous(device_id, dir); // Create files `/proc/gpu#/gpc#_tpc_mask`, world readable tpc_masks_create_err = create_tpc_mask_files(device_id, dir); // Create file `/proc/gpu#/preempt_tsg`, world writable @@ -233,7 +264,7 @@ int __init nvdebug_init(void) { (void*)device_id); // Create file `/proc/gpu#/device_info`, world readable device_info_entry = proc_create_data( - "device_info", 0444, dir, compat_ops(&device_info_file_ops), + "device_info", 0444, dir, compat_ops(g_architectures[device_id] == 0x17 ? &device_info_file_ops_previous : &device_info_file_ops_ampere), (void*)device_id); // Create file `/proc/gpu#/num_gpcs`, world readable num_gpcs_entry = proc_create_data( -- cgit v1.2.2