drm/nouveau: retain device pointer in nvkm_gsp_mem object
Subsystems (3)
PATCH v2 1/2 drm/nouveau: retain device pointer in nvkm_gsp_mem object
Store the struct device pointer used to allocate the DMA buffer in the nvkm_gsp_mem object. This allows nvkm_gsp_mem_dtor() to release the buffer without needing the nvkm_gsp. This is needed so that we can retain DMA buffers even after the nvkm_gsp object is deleted.
---
Notes:
v2:
added get/put_device calls
.../gpu/drm/nouveau/include/nvkm/subdev/gsp.h | 1 + .../gpu/drm/nouveau/nvkm/subdev/gsp/r535.c | 50 ++++++++++++------- 2 files changed, 34 insertions(+), 17 deletions(-)
drivers/gpu/drm/nouveau/include/nvkm/subdev/gsp.h +1 -0
@@ -9,6 +9,7 @@#define GSP_PAGE_SIZE BIT(GSP_PAGE_SHIFT)struct nvkm_gsp_mem { +struct device *dev;size_t size;void *data;dma_addr_t addr;
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c +33 -17
110 lines not on this page · raw message
base-commit: 21e97d3ca814ea59d5ddb6a734125bd006b66a60
prerequisite-patch-id: 6f7d619f67878dea2a3378e76e28d3c666921fbd
Signature · 1 line
2.34.1
PATCH v7 2/2 drm/nouveau: expose GSP-RM logging buffers via debugfs
The LOGINIT, LOGINTR, LOGRM, and LOGPMU buffers are circular buffers that have printf-like logs from GSP-RM and PMU encoded in them.
LOGINIT, LOGINTR, and LOGRM are allocated by Nouveau and their DMA addresses are passed to GSP-RM during initialization. The buffers are required for GSP-RM to initialize properly.
LOGPMU is also allocated by Nouveau, but its contents are updated when Nouveau receives an NV_VGPU_MSG_EVENT_UCODE_LIBOS_PRINT RPC from GSP-RM. Nouveau then copies the RPC to the buffer.
The messages are encoded as an array of variable-length structures that contain the parameters to an NV_PRINTF call. The format string and parameter count are stored in a special ELF image that contains only logging strings. This image is not currently shipped with the Nvidia driver.
There are two methods to extract the logs.
OpenRM tries to load the logging ELF, and if present, parses the log buffers in real time and outputs the strings to the kernel console.
Alternatively, and this is the method used by this patch, the buffers can be exposed to user space, and a user-space tool (along with the logging ELF image) can parse the buffer and dump the logs.
This method has the advantage that it allows the buffers to be parsed even when the logging ELF file is not available to the user. However, it has the disadvantage the debugfs entries need to remain until the driver is unloaded.
The buffers are exposed via debugfs. If GSP-RM fails to initialize, then Nouveau immediately shuts down the GSP interface. This would normally also deallocate the logging buffers, thereby preventing the user from capturing the debug logs.
To avoid this, introduce the keep-gsp-logging command line parameter. If specified, and if at least one logging buffer has content, then Nouveau will migrate these buffers into new debugfs entries that are retained until the driver unloads.
An end-user can capture the logs using the following commands:
cp /sys/kernel/debug/dri/<path>/loginit loginit cp /sys/kernel/debug/dri/<path>/logrm logrm cp /sys/kernel/debug/dri/<path>/logintr logintr cp /sys/kernel/debug/dri/<path>/logpmu logpmu
where (for a PCI device) <path> is the PCI ID of the GPU (e.g. 0000:65:00.0).
Since LOGPMU is not needed for normal GSP-RM operation, it is only created if debugfs is available. Otherwise, the NV_VGPU_MSG_EVENT_UCODE_LIBOS_PRINT RPCs are ignored.
A simple way to test the buffer migration feature is to have nvkm_gsp_init() return an error code.
---
Notes:
v7:
fixed PCI reference and typos in commit description replaced 'driver' with 'module' in comments added struct nvif_logs and related changes removed unnecessary drm_dev null check replaced nvkm_warn with nvkm_info
add null pointer check in is_empty()
fixed debugfs_lookup error check
drivers/gpu/drm/nouveau/include/nvif/log.h | 57 +++ .../gpu/drm/nouveau/include/nvkm/subdev/gsp.h | 13 + drivers/gpu/drm/nouveau/nouveau_drm.c | 12 + .../gpu/drm/nouveau/nvkm/subdev/gsp/r535.c | 397 +++++++++++++++++- 4 files changed, 478 insertions(+), 1 deletion(-) create mode 100644 drivers/gpu/drm/nouveau/include/nvif/log.h
drivers/gpu/drm/nouveau/include/nvif/log.h new file +57 -0
@@ -0,0 +1,57 @@ +/* SPDX-License-Identifier: MIT */ +/* SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. */ + +#ifndef __NVIF_LOG_H__ +#define __NVIF_LOG_H__ + +/** +* nvif_log - structure for tracking logging buffers +* @entry: an entry in a list of struct nvif_logs +* @shutdown: pointer to function to call to clean up +* +* Structure used to track logging buffers so that they can be cleaned up +* when the module exits. +* +* The @shutdown function is called when the module exits. It should free all +* backing resources, such as logging buffers. +*/ +struct nvif_log { +struct list_head entry; +void (*shutdown)(struct nvif_log *log); +}; + +/** +* nvif_logs - linked list of nvif_log objects +*/ +struct nvif_logs { +struct list_head head; +}; + +#define NVIF_LOGS_DECLARE (logs) \ + struct nvif_logs logs = { LIST_HEAD_INIT(logs.head) } + +static inline void nvif_log_shutdown (struct nvif_logs *logs) +{ +if (!list_empty (&logs->head)) { +struct nvif_log *log, *n; + +list_for_each_entry_safe (log, n, &logs->head, entry) { +/* shutdown() should also delete the log entry */ + log->shutdown (log); + } + } +} + +#ifdef CONFIG_DEBUG_FS +/** +* gsp_logs - list of nvif_log GSP-RM logging buffers +* +* Head pointer to a a list of nvif_log buffers that is created for each GPU +* upon GSP shutdown if the "keep_gsp_logging" command-line parameter is +* specified. This is used to track the alternative debugfs entries for the +* GSP-RM logs. +*/ +extern struct nvif_logs gsp_logs; +#endif + +#endif
drivers/gpu/drm/nouveau/include/nvkm/subdev/gsp.h +13 -0
@@ -5,6 +5,8 @@#include <core/falcon.h> #include <core/firmware.h> +#include <linux/debugfs.h> +#define GSP_PAGE_SHIFT 12#define GSP_PAGE_SIZE BIT(GSP_PAGE_SHIFT)
@@ -220,6 +222,17 @@ struct nvkm_gsp {/* The size of the registry RPC */ size_t registry_rpc_size; + +#ifdef CONFIG_DEBUG_FS +/* +* Logging buffers in debugfs. The wrapper objects need to remain +* in memory until the dentry is deleted. +*/ +struct debugfs_blob_wrapper blob_init; +struct debugfs_blob_wrapper blob_intr; +struct debugfs_blob_wrapper blob_rm; +struct debugfs_blob_wrapper blob_pmu; +#endif }; static inline bool
drivers/gpu/drm/nouveau/nouveau_drm.c +12 -0
@@ -46,6 +46,7 @@#include <nvif/fifo.h> #include <nvif/push006c.h> #include <nvif/user.h> +#include <nvif/log.h> #include <nvif/class.h> #include <nvif/cl0002.h>
@@ -112,6 +113,13 @@ static struct drm_driver driver_stub;static struct drm_driver driver_pci;static struct drm_driver driver_platform; +#ifdef CONFIG_DEBUG_FS +/** +* gsp_logs - list of GSP debugfs logging buffers +*/ +NVIF_LOGS_DECLARE (gsp_logs); +#endif +static u64 nouveau_pci_name (struct pci_dev *pdev) {
@@ -1461,6 +1469,10 @@ nouveau_drm_exit(void)#endif if (IS_ENABLED (CONFIG_DRM_NOUVEAU_SVM ))mmu_notifier_synchronize (); + +#ifdef CONFIG_DEBUG_FS +nvif_log_shutdown (&gsp_logs); +#endif }module_init (nouveau_drm_init);
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c +396 -1
433 lines not on this page · raw message
Signature · 1 line
2.34.1
Danilo Krummrich
maintainerRe: [PATCH 2/2] [v7] drm/nouveau: expose GSP-RM logging buffers via debugfs
On Fri, Aug 02, 2024 at 02:05:17PM -0500, Timur Tabi wrote:
I was about to apply this patch, but noticed a bug while smoke testing things.
Quoted 24 lines
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c +24
-0+/** +* r535_gsp_libos_debugfs_init - create logging debugfs entries +* @gsp: gsp pointer +* +* Create the debugfs entries. This exposes the log buffers to userspace so +* that an external tool can parse it. +* +* The 'logpmu' contains exception dumps from the PMU. It is written via an +* RPC sent from GSP-RM and must be only 4KB. We create it here because it's +* only useful if there is a debugfs entry to expose it. If we get the PMU +* logging RPC and there is no debugfs entry, the RPC is just ignored. +* +* The blob_init, blob_rm, and blob_pmu objects can't be transient +* because debugfs_create_blob doesn't copy them. +* +* NOTE: OpenRM loads the logging elf image and prints the log messages +* in real-time. We may add that capability in the future, but that +* requires loading an ELF images that are not distributed with the driver, +* and adding the parsing code to Nouveau. +* +* Ideally, this should be part of nouveau_debugfs_init(), but that function +* is called too late. We really want to create these debugfs entries before +* r535_gsp_booter_load() is called, so that if GSP-RM fails to initialize, +* there could still be a log to capture.
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c r535_gsp_rmargs_init(struct nvkm_gsp *gsp, bool resume) · line 2196
Quoted 10 lines
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c +10
-0+*/ +static void +r535_gsp_libos_debugfs_init (struct nvkm_gsp *gsp) +{ +struct dentry *dir, *dir_init; +struct dentry *dir_intr =NULL , *dir_rm =NULL , *dir_pmu =NULL ; +struct device *dev = gsp->subdev.device->dev; + +/* Each GPU has a subdir based on its device name, so find it */ +struct drm_device *drm_dev =dev_get_drvdata (dev);
In drm-misc-next dev_get_drvdata() returns a struct nouveau_drm. Interestingly, things do not blow up however.
Instead, I noticed that your debugfs entries are created in the debugfs root, instead of "dri/<PCI_ID>/".
I think we shouldn't try to use the DRI debugfs directory anyway to avoid DRM layer dependencies in nvkm. Let's create our own nouveau one, like you did in earlier versions of this patch.
Please also make sure to test your patches on a recent drm-misc-next branch.
Quoted 69 lines
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c +68
-0+ + dir = drm_dev->debugfs_root; + + gsp->blob_init.data = gsp->loginit.data; + gsp->blob_init.size = gsp->loginit.size; + gsp->blob_intr.data = gsp->logintr.data; + gsp->blob_intr.size = gsp->logintr.size; + gsp->blob_rm.data = gsp->logrm.data; + gsp->blob_rm.size = gsp->logrm.size; + + dir_init =debugfs_create_blob ("loginit" ,0444 , dir, &gsp->blob_init); +if (IS_ERR (dir_init)) { +nvkm_error (&gsp->subdev,"failed to create loginit debugfs entry\n" ); + goto error; + } + + dir_intr =debugfs_create_blob ("logintr" ,0444 , dir, &gsp->blob_intr); +if (IS_ERR (dir_intr)) { +nvkm_error (&gsp->subdev,"failed to create logintr debugfs entry\n" ); + goto error; + } + + dir_rm =debugfs_create_blob ("logrm" ,0444 , dir, &gsp->blob_rm); +if (IS_ERR (dir_rm)) { +nvkm_error (&gsp->subdev,"failed to create logrm debugfs entry\n" ); + goto error; + } + +/* +* Since the PMU buffer is copied from an RPC, it doesn't need to be +* a DMA buffer. +*/ + gsp->blob_pmu.size =GSP_PAGE_SIZE ; + gsp->blob_pmu.data =kzalloc (gsp->blob_pmu.size,GFP_KERNEL ); +if (!gsp->blob_pmu.data) + goto error; + + dir_pmu =debugfs_create_blob ("logpmu" ,0444 , dir, &gsp->blob_pmu); +if (IS_ERR (dir_pmu)) { +nvkm_error (&gsp->subdev,"failed to create logpmu debugfs entry\n" ); +kfree (gsp->blob_pmu.data); + goto error; + } + +i_size_write (d_inode (dir_init), gsp->blob_init.size); +i_size_write (d_inode (dir_intr), gsp->blob_intr.size); +i_size_write (d_inode (dir_rm), gsp->blob_rm.size); +i_size_write (d_inode (dir_pmu), gsp->blob_pmu.size); + +r535_gsp_msg_ntfy_add (gsp,NV_VGPU_MSG_EVENT_UCODE_LIBOS_PRINT , + r535_gsp_msg_libos_print, gsp); + +nvkm_debug (&gsp->subdev,"created debugfs GSP-RM logging entries\n" ); + +if (keep_gsp_logging) { +nvkm_info (&gsp->subdev, +"logging buffers will be retained on failure\n" ); + } + +return ; + +error: +debugfs_remove (dir_init); +debugfs_remove (dir_intr); + debugfs_remove(dir_rm); +} + +#endif }
Re: [PATCH 2/2] [v7] drm/nouveau: expose GSP-RM logging buffers via debugfs
Quoted 10 lines · Danilo Krummrich
Quoted 2 lines
+ /* Each GPU has a subdir based on its device name, so find it */ + struct
drm_device*drm_dev=dev_get_drvdata(dev);In drm-misc-next
dev_get_drvdata()returns a structnouveau_drm. Interestingly,
things do not blow up however.Instead, I noticed that your debugfs entries are created in the debugfs root,
instead of "dri/<PCI_ID>/".
Woah.
Quoted 5 lines
I think we shouldn't try to use the DRI debugfs directory anyway to avoid DRM
layer dependencies in nvkm. Let's create our own nouveau one, like you did in
earlier versions of this patch.
So what path should I use?
So what it's supposed to do is create a sort of "shadow" dri/<PCI_ID>/ path that overlays the existing one. So when the module deletes the original dri/<PCI_ID>/, the one I create replaces it automatically. I think it's a neat feature. The path to the debugfs entries stays the same whether or not they're migrated.
Quoted 1 line
Please also make sure to test your patches on a recent drm-misc-next branch.
Will do.
Danilo Krummrich
maintainerRe: [PATCH 2/2] [v7] drm/nouveau: expose GSP-RM logging buffers via debugfs
Quoted 14 lines · Timur Tabi
Quoted 10 lines · Danilo Krummrich
Quoted 2 lines
+ /* Each GPU has a subdir based on its device name, so find it */ + struct
drm_device*drm_dev=dev_get_drvdata(dev);In drm-misc-next
dev_get_drvdata()returns a structnouveau_drm. Interestingly,
things do not blow up however.Instead, I noticed that your debugfs entries are created in the debugfs root,
instead of "dri/<PCI_ID>/".Woah.
Quoted 7 lines
Quoted 5 lines
I think we shouldn't try to use the DRI debugfs directory anyway to avoid DRM
layer dependencies in nvkm. Let's create our own nouveau one, like you did in
earlier versions of this patch.So what path should I use?
I think we should just create "nouveau/<PCI_ID>/" instead and not mess with the DRI stuff at all.
Quoted 10 lines
So what it's supposed to do is create a sort of "shadow" dri/<PCI_ID>/ path that overlays the existing one. So when the module deletes the original dri/<PCI_ID>/, the one I create replaces it automatically. I think it's a neat feature. The path to the debugfs entries stays the same whether or not they're migrated.
Quoted 1 line
Please also make sure to test your patches on a recent drm-misc-next branch.
Will do.