From 9e6cc03cdf736fbd817ed53fa9a7f506bc91a244 Mon Sep 17 00:00:00 2001 From: Benjamin Hadad IV Date: Wed, 16 Aug 2023 22:00:20 -0400 Subject: A variety of changes have been made as part of the code review. - Functions have been consolidated. - Code was clarified and tidied up overall. - Unnecessary elements were removed. --- Makefile | 1 - device_info_procfs.c | 57 +++++++++++++++------------------------------------- nvdebug.h | 4 ++-- nvdebug_entry.c | 9 +++------ 4 files changed, 21 insertions(+), 50 deletions(-) diff --git a/Makefile b/Makefile index 8e32bd0..2dc90c7 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,6 @@ KBUILD_CFLAGS += -DGIT_HASH=\"$(shell git --git-dir=$(PWD)/.git rev-parse --shor # TODO: Avoid needing to distribute NVIDIA's headers (at least they're MIT...) ccflags-y += -I$(PWD)/include -ccflags-y += -std=gnu99 #ccflags-y += -I/playpen/Linux_for_Tegra/source/public/kernel/nvgpu/drivers/gpu/nvgpu/include #ccflags-y += -I/playpen/Linux_for_Tegra/source/public/kernel/nvgpu/drivers/gpu/nvgpu #ccflags-y += -I/playpen/Linux_for_Tegra/source/public/kernel/nvgpu/include diff --git a/device_info_procfs.c b/device_info_procfs.c index 5ddf240..efef41a 100644 --- a/device_info_procfs.c +++ b/device_info_procfs.c @@ -35,31 +35,24 @@ typedef struct { //// ==v== PTOP_DEVICE_INFO ==v== //// -static void* device_info_seq_start_backend(struct seq_file *s, loff_t *pos, int initial_entry_value, int total_entries) { +// 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 device_info_iter idx; + struct nvdebug_state *g = &g_nvdebug_state[seq2gpuidx(s)]; + int is_ampere = g->chip_id == NV_CHIP_ID_AMPERE; // If start of sequence, reset `idx` if (*pos == 0) { idx.index = 0; - idx.type_of_next_entry = initial_entry_value; + idx.type_of_next_entry = is_ampere ? 0 : -1; } - idx.total_entries = total_entries; + idx.total_entries = is_ampere ? NV_PTOP_DEVICE_INFO__SIZE_1_PREVIOUS : NV_PTOP_DEVICE_INFO__SIZE_1_AMPERE(g); // Number of possible info entries is fixed, and list is sparse if (idx.index >= idx.total_entries) return NULL; return &idx; } -// 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_previous(struct seq_file *s, loff_t *pos) { - return device_info_seq_start_backend(s, pos, -1, NV_PTOP_DEVICE_INFO__SIZE_1_PREVIOUS); -} - -static void* device_info_file_seq_start_ampere(struct seq_file *s, loff_t *pos) { - struct nvdebug_state *g = &g_nvdebug_state[seq2gpuidx(s)]; - return device_info_seq_start_backend(s, pos, 0, NV_PTOP_DEVICE_INFO__SIZE_1_AMPERE(g)); -} - // 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, @@ -166,43 +159,25 @@ static int device_info_file_seq_show_ampere(struct seq_file *s, void *idx) { 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_previous = { - .start = device_info_file_seq_start_previous, +static struct seq_operations device_info_file_seq_ops = { + .start = device_info_file_seq_start, .next = device_info_file_seq_next, - .stop = device_info_file_seq_stop, - .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, - .stop = device_info_file_seq_stop, - .show = device_info_file_seq_show_ampere, + .stop = device_info_file_seq_stop }; -static int device_info_file_open_previous(struct inode *inode, struct file *f) { - return seq_open(f, &device_info_file_seq_ops_previous); +static int device_info_file_open(struct inode *inode, struct file *f) { + struct seq_operations file_seq_ops = device_info_file_seq_ops; + file_seq_ops.show = (g_nvdebug_state[file2parentgpuidx(f)].chip_id == NV_CHIP_ID_AMPERE) ? device_info_file_seq_show_ampere : device_info_file_seq_show_previous; + return seq_open(f, &file_seq_ops); } -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, +struct file_operations device_info_file_ops = { + .open = device_info_file_open, .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 c45e460..471adf9 100644 --- a/nvdebug.h +++ b/nvdebug.h @@ -9,8 +9,6 @@ #include // For KERNEL_VERSION and LINUX_VERSION_CODE #include -#define NV_BUILD_FOR_AMPERE - /* Runlist Channel A timeslice group (TSG) is composed of channels. Each channel is a FIFO queue of GPU commands. These commands are typically queued from userspace. @@ -382,6 +380,7 @@ typedef union { #define NV_CHIP_ID_GV11B 0x15B // Jetson Xavier embedded GPU #define NV_CHIP_ID_KEPLER 0x0E0 #define NV_CHIP_ID_VOLTA 0x140 +#define NV_CHIP_ID_AMPERE 0x170 inline static const char* ARCH2NAME(uint32_t arch) { switch (arch) { @@ -565,6 +564,7 @@ static const char* const ENGINE_TYPES_NAMES[ENGINE_TYPES_LEN] = { #define NV_PTOP_DEVICE_INFO__SIZE_1_AMPERE(g) (nvdebug_readl(g, 0x0224fc) >> 20) #define NV_PTOP_DEVICE_INFO__SIZE_1_PREVIOUS 64 #define NV_PTOP_DEVICE_INFO_TYPE_COUNT 3 +#define NV_CHIP_ID_AMPERE 0x170 typedef union { struct { uint32_t fault_id:7; diff --git a/nvdebug_entry.c b/nvdebug_entry.c index d3d934e..0560ead 100644 --- a/nvdebug_entry.c +++ b/nvdebug_entry.c @@ -23,14 +23,12 @@ 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_previous; -extern struct file_operations device_info_file_ops_ampere; +extern struct file_operations device_info_file_ops; 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 @@ -125,7 +123,6 @@ 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 @@ -243,7 +240,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 = g_architectures[device_id] == 0x17 ? create_runlist_files_ampere(device_id, dir) : create_runlist_files_previous(device_id, dir); + rl_create_err = (g_nvdebug_state[device_id].chip_id == NV_CHIP_ID_AMPERE) ? 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 @@ -264,7 +261,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(g_architectures[device_id] == 0x17 ? &device_info_file_ops_previous : &device_info_file_ops_ampere), + "device_info", 0444, dir, compat_ops(&device_info_file_ops), (void*)device_id); // Create file `/proc/gpu#/num_gpcs`, world readable num_gpcs_entry = proc_create_data( -- cgit v1.2.2