aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoshua Bakita <jbakita@cs.unc.edu>2024-04-11 13:42:54 -0400
committerJoshua Bakita <jbakita@cs.unc.edu>2024-04-11 13:42:54 -0400
commit7cfa24cebeaf144b446d07e15fb25e78bb14841e (patch)
tree639db3695c54a1e50b3f10e1abb2bfaf5b8dd906
parenta8fd5a8dee066d0008e7667b0c9e6a60cd5f3a2e (diff)
Linux 5.17+ support and allow including nvdebug.h independently
- Move Linux-specific functions to nvdebug_linux.h and .c - Workaround PDE_DATA() being pde_data() on Linux 5.17+
-rw-r--r--Makefile2
-rw-r--r--copy_topology_procfs.c2
-rw-r--r--device_info_procfs.c8
-rw-r--r--nvdebug.h118
-rw-r--r--nvdebug_entry.c6
-rw-r--r--nvdebug_linux.c61
-rw-r--r--nvdebug_linux.h49
-rw-r--r--runlist_procfs.c2
8 files changed, 140 insertions, 108 deletions
diff --git a/Makefile b/Makefile
index c2d2e96..972bc0b 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
1obj-m += nvdebug.o 1obj-m += nvdebug.o
2nvdebug-objs = runlist_procfs.o device_info_procfs.o runlist.o mmu.o \ 2nvdebug-objs = runlist_procfs.o device_info_procfs.o runlist.o mmu.o \
3 nvdebug_entry.o bus.o copy_topology_procfs.o 3 nvdebug_entry.o bus.o nvdebug_linux.o copy_topology_procfs.o
4KBUILD_CFLAGS += -DGIT_HASH=\"$(shell git --git-dir=$(PWD)/.git rev-parse --short HEAD)\" 4KBUILD_CFLAGS += -DGIT_HASH=\"$(shell git --git-dir=$(PWD)/.git rev-parse --short HEAD)\"
5# -mfentry above if not building due to mcount missing 5# -mfentry above if not building due to mcount missing
6 6
diff --git a/copy_topology_procfs.c b/copy_topology_procfs.c
index cfedce7..a1aa0ad 100644
--- a/copy_topology_procfs.c
+++ b/copy_topology_procfs.c
@@ -1,6 +1,6 @@
1// Copyright 2024 Joshua Bakita 1// Copyright 2024 Joshua Bakita
2 2
3#include "nvdebug.h" 3#include "nvdebug_linux.h"
4 4
5// Maximum number of LCEs that we will print 5// Maximum number of LCEs that we will print
6#define MAX_LCES 32 6#define MAX_LCES 32
diff --git a/device_info_procfs.c b/device_info_procfs.c
index 168905f..8fe9709 100644
--- a/device_info_procfs.c
+++ b/device_info_procfs.c
@@ -1,4 +1,4 @@
1#include "nvdebug.h" 1#include "nvdebug_linux.h"
2#include <linux/seq_file.h> // For seq_* functions and types 2#include <linux/seq_file.h> // For seq_* functions and types
3#include <linux/uaccess.h> // For copy_to_user() 3#include <linux/uaccess.h> // For copy_to_user()
4 4
@@ -16,7 +16,7 @@ static ssize_t nvdebug_reg32_read(struct file *f, char __user *buf, size_t size,
16 if (size < 16 || *off != 0) 16 if (size < 16 || *off != 0)
17 return 0; 17 return 0;
18 // 32 bit register will always take less than 16 characters to print 18 // 32 bit register will always take less than 16 characters to print
19 chars_written = scnprintf(out, 16, "%#0x\n", nvdebug_readl(g, (uintptr_t)PDE_DATA(file_inode(f)))); 19 chars_written = scnprintf(out, 16, "%#0x\n", nvdebug_readl(g, (uintptr_t)pde_data(file_inode(f))));
20 if (copy_to_user(buf, out, chars_written)) 20 if (copy_to_user(buf, out, chars_written))
21 printk(KERN_WARNING "Unable to copy all data for %s\n", file_dentry(f)->d_name.name); 21 printk(KERN_WARNING "Unable to copy all data for %s\n", file_dentry(f)->d_name.name);
22 *off += chars_written; 22 *off += chars_written;
@@ -30,7 +30,7 @@ static ssize_t nvdebug_reg_range_read(struct file *f, char __user *buf, size_t s
30 struct nvdebug_state *g = &g_nvdebug_state[file2parentgpuidx(f)]; 30 struct nvdebug_state *g = &g_nvdebug_state[file2parentgpuidx(f)];
31 // See comment in nvdebug_entry.c to understand `union reg_range` 31 // See comment in nvdebug_entry.c to understand `union reg_range`
32 union reg_range range; 32 union reg_range range;
33 range.raw = (uintptr_t)PDE_DATA(file_inode(f)); 33 range.raw = (uintptr_t)pde_data(file_inode(f));
34 34
35 // "0x" + up to 32-bit register as hex + "\n\0" is at most 12 characters 35 // "0x" + up to 32-bit register as hex + "\n\0" is at most 12 characters
36 if (size < 12 || *off != 0) 36 if (size < 12 || *off != 0)
@@ -55,7 +55,7 @@ struct file_operations nvdebug_read_reg32_file_ops = {
55}; 55};
56 56
57// Generic mechanism used for printing a subset of bits from a register 57// Generic mechanism used for printing a subset of bits from a register
58// Please store a `union reg_range` rather than a `uintptr_t` in the PDE_DATA 58// Please store a `union reg_range` rather than a `uintptr_t` in the pde_data
59struct file_operations nvdebug_read_reg_range_file_ops = { 59struct file_operations nvdebug_read_reg_range_file_ops = {
60 .read = nvdebug_reg_range_read, 60 .read = nvdebug_reg_range_read,
61 .llseek = default_llseek, 61 .llseek = default_llseek,
diff --git a/nvdebug.h b/nvdebug.h
index eff1470..ff35f70 100644
--- a/nvdebug.h
+++ b/nvdebug.h
@@ -7,13 +7,11 @@
7 * - Detailed GPU information (PTOP, FUSE, and CE) 7 * - Detailed GPU information (PTOP, FUSE, and CE)
8 * - PRAMIN, BAR1/2, and page table status 8 * - PRAMIN, BAR1/2, and page table status
9 * - Helper functions for nvdebug 9 * - Helper functions for nvdebug
10 *
11 * This function should not depend on any Linux-internal headers, and may be
12 * included outside of nvdebug.
10 */ 13 */
11 14#include <linux/types.h>
12#include <linux/device.h> // For dev_get_drvdata()
13#include <linux/seq_file.h> // For struct seq_file
14#include <linux/proc_fs.h> // For PDE_DATA() macro
15#include <linux/version.h> // For KERNEL_VERSION and LINUX_VERSION_CODE
16#include <asm/io.h>
17 15
18// Fully defined in include/nvgpu/gk20a.h. We only pass around pointers to 16// Fully defined in include/nvgpu/gk20a.h. We only pass around pointers to
19// this, so declare as incomplete type to avoid pulling in the nvgpu headers. 17// this, so declare as incomplete type to avoid pulling in the nvgpu headers.
@@ -1147,8 +1145,11 @@ typedef union {
1147} page_tbl_entry_v0_t; 1145} page_tbl_entry_v0_t;
1148*/ 1146*/
1149 1147
1150// TODO(jbakita): Maybe put the above GPU types in a different file.
1151 1148
1149
1150/* Begin nvdebug types and functions */
1151
1152// Vendor ID for PCI devices manufactured by NVIDIA
1152#define NV_PCI_VENDOR 0x10de 1153#define NV_PCI_VENDOR 0x10de
1153struct nvdebug_state { 1154struct nvdebug_state {
1154 // Pointer to the mapped base address of the GPU control registers (obtained 1155 // Pointer to the mapped base address of the GPU control registers (obtained
@@ -1195,7 +1196,6 @@ struct nvdebug_state {
1195 const struct gk110_runlist_ ## _ENTRY_TYPE *entry = (struct gk110_runlist_ ## _ENTRY_TYPE*)raw; \ 1196 const struct gk110_runlist_ ## _ENTRY_TYPE *entry = (struct gk110_runlist_ ## _ENTRY_TYPE*)raw; \
1196 return entry->prop; \ 1197 return entry->prop; \
1197 } else { \ 1198 } else { \
1198 printk(KERN_WARNING "[nvdebug] " #prop " unavailable on GPU ID %x, which is older than Kepler.\n", g->chip_id); \
1199 return (type)0; \ 1199 return (type)0; \
1200 } \ 1200 } \
1201 } 1201 }
@@ -1250,96 +1250,18 @@ uint64_t search_v1_page_directory(
1250 struct nvdebug_state *g, 1250 struct nvdebug_state *g,
1251 page_dir_config_t pd_config, 1251 page_dir_config_t pd_config,
1252 uint64_t addr_to_find); 1252 uint64_t addr_to_find);
1253
1254
1255static inline struct gk20a *get_gk20a(struct device *dev) {
1256 // XXX: Only works because gk20a* is the first member of gk20a_platform
1257 return *((struct gk20a**)dev_get_drvdata(dev));
1258}
1259
1260// We us the data field of the proc_dir_entry ("PDE" in this function) to store
1261// our index into the g_nvdebug_state array
1262static inline int seq2gpuidx(struct seq_file *s) {
1263 const struct file *f = s->file;
1264 return (uintptr_t)PDE_DATA(file_inode(f));
1265}
1266static inline int file2gpuidx(const struct file *f) {
1267 return (uintptr_t)PDE_DATA(file_inode(f));
1268}
1269static inline int file2parentgpuidx(const struct file *f) {
1270 // Should be safe to call on ProcFS entries, as our parent should (?)
1271 // still exist if we're called. If not, there are worse races in this
1272 // module.
1273 return (uintptr_t)PDE_DATA(file_dentry(f)->d_parent->d_inode);
1274}
1275
1276#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0)
1277// Commit 643eb158a3 in nvgpu moved the mapped registers to the second entry
1278// of the gk20a struct (after a function pointer). This change was made as L4T
1279// was upgraded from Linux 4.9 to 5.10 (r32 -> r34+)
1280// Note that this is wrong if nvgpu was built without CONFIG_NVGPU_NON_FUSA
1281// i.e. if FUSA was enabled, this is wrong.
1282#define gk20a_regs(gk20a) (*(void**)((void*)gk20a + sizeof(void(*)(void))))
1283#else
1284#include <os/linux/os_linux.h> // For struct nvgpu_os_linux, which holds regs
1285#define gk20a_regs(gk20a) (container_of(gk20a, struct nvgpu_os_linux, g)->regs)
1286#endif
1287
1288// Similar to nvgpu_readl()
1289// (except we don't try to resolve situations where regs is NULL)
1290static inline u32 nvdebug_readl(struct nvdebug_state *s, u32 r) {
1291 u32 ret;
1292 if (unlikely(!s->regs || (s->g && !gk20a_regs(s->g)))) {
1293 printk(KERN_ERR "[nvdebug] nvdebug_readl: Unable to read; registers unavailable. Is GPU on?\n");
1294 return -1;
1295 }
1296 ret = readl(s->regs + r);
1297 // It seems like the GPU returns this as a flag value for bad addresses
1298 if (ret == 0xbadf5040) {
1299 printk(KERN_ERR "[nvdebug] nvdebug_readl: Unable to read from register offset %#x; bad data\n", r);
1300 return -1;
1301 }
1302 return ret;
1303}
1304
1305// quadword version of nvdebug_readl()
1306static inline u64 nvdebug_readq(struct nvdebug_state *s, u32 r) {
1307 u64 ret;
1308 if (unlikely(!s->regs || (s->g && !gk20a_regs(s->g)))) {
1309 printk(KERN_ERR "[nvdebug] nvdebug_readq: Unable to read; registers unavailable. Is GPU on?\n");
1310 return -1;
1311 }
1312 // readq seems to always return the uppermost 32 bits as 0, so workaround with readl
1313 ret = readl(s->regs + r);
1314 ret |= ((u64)readl(s->regs + r + 4)) << 32;
1315 // It seems like the GPU returns this as a flag value for bad addresses
1316 if ((ret & 0xffffffffull) == 0xbadf5040ull) {
1317 printk(KERN_ERR "[nvdebug] nvdebug_readq: Unable to read from register offset %#x; bad data\n", r);
1318 return -1;
1319 }
1320 return ret;
1321}
1322
1323// Similar to nvgpu_writel()
1324static inline void nvdebug_writel(struct nvdebug_state *s, u32 r, u32 v) {
1325 if (unlikely(!s->regs || (s->g && !gk20a_regs(s->g)))) {
1326 printk(KERN_ERR "[nvdebug] nvdebug_writel: Unable to write; registers unavailable. Is GPU on?\n");
1327 return;
1328 }
1329 writel_relaxed(v, s->regs + r);
1330 wmb();
1331}
1332
1333// quadword version of nvdebug_writel()
1334// XXX: This probably doesn't work XXX: Untested
1335static inline void nvdebug_writeq(struct nvdebug_state *s, u32 r, u64 v) {
1336 if (unlikely(!s->regs || (s->g && !gk20a_regs(s->g)))) {
1337 printk(KERN_ERR "[nvdebug] nvdebug_writeq: Unable to write; registers unavailable. Is GPU on?\n");
1338 return;
1339 }
1340 writeq_relaxed(v, s->regs + r);
1341 wmb();
1342}
1343// Defined in bus.c 1253// Defined in bus.c
1344int addr_to_pramin_mut(struct nvdebug_state *g, uint64_t addr, enum INST_TARGET target);