Your GeForce runs AI at half speed. This fixes it for 20/30/40 series cards. Up to 2× FP16 / BF16 / TF32 tensor throughput
C
Unlock the cards again after resume from suspend
Zagittac274e74981d9
diff --git a/README.md b/README.mdindex b2d3e2e..42d3e87 100644--- a/README.md+++ b/README.md@@ -126,6 +126,18 @@ over.Loading the module by hand after boot does nothing to a card that `nvidia`has bound.+Suspend to RAM powers the cards off, which resets the speed selectors. After+resume the module unlocks every card it unlocked at boot again, before+`nvidia` resumes it: its suspend notifier runs just ahead of the one `nvidia`+resumes its cards from (`NVreg_UseKernelSuspendNotifiers=1`, the default in+current drivers; `nvidia-resume.service` runs later still). This adds about a+second to resume. A card that kept power, as in suspend to idle, is still+unlocked and is only read. A card that `nvidia` has already resumed, or that is+bound to another driver, is left as it is, and the log says why. Add+`gpu_unlock.resume=0` to the kernel command line, or write `0` to+`/sys/module/gpu_unlock/parameters/resume`, to turn this off. Hibernation is+not covered.+Check the result with:```shdiff --git a/kernel/gpu_unlock/main.c b/kernel/gpu_unlock/main.cindex a632896..0a6b950 100644--- a/kernel/gpu_unlock/main.c+++ b/kernel/gpu_unlock/main.c@@ -1,9 +1,12 @@// SPDX-License-Identifier: GPL-2.0-only#include <linux/async.h>#include <linux/init.h>+#include <linux/list.h>#include <linux/module.h>#include <linux/pci.h>+#include <linux/slab.h>#include <linux/string.h>+#include <linux/suspend.h>#include "unlock_610.h"@@ -11,6 +14,10 @@ static char *targets;module_param(targets, charp, 0444);MODULE_PARM_DESC(targets, "\"all\" or comma-separated PCI addresses of GA102/GA103/GA104/GA106 cards to unlock");+static bool resume = true;+module_param(resume, bool, 0644);+MODULE_PARM_DESC(resume, "Unlock the cards again after resume from suspend, which powers them off (default: on)");+/* GA102, GA103, GA104 and GA106 GPU functions in pci.ids, excluding audio* functions and the professional (GL) parts, which already run BF16 at full* rate. GA103 and GA106 also exclude mobile parts.@@ -31,6 +38,15 @@ static const unsigned short supported_ids[] = {0x2544,};+/* A target card. Those unlocked at boot stay listed, with a reference, to be+ * unlocked again after each resume. */+struct card {+ struct list_head node;+ struct pci_dev *pdev;+ bool unlocked;+};++static LIST_HEAD(cards);static ASYNC_DOMAIN_EXCLUSIVE(unlock_domain);static struct gpu_unlock_610_shared shared;@@ -47,22 +63,88 @@ static struct pci_dev *next_supported(struct pci_dev *from)return NULL;}-/* Runs concurrently for each card; consumes the reference it is given. */+/* At boot, before nvidia binds the card. Runs concurrently for each card. */static void unlock_one(void *data, async_cookie_t cookie){- struct pci_dev *pdev = data;+ struct card *card = data;+ struct pci_dev *pdev = card->pdev;int ret;pci_dev_lock(pdev);- ret = pdev->driver ? -EBUSY : gpu_unlock_610_unlock(pdev, &shared);+ ret = pdev->driver ? -EBUSY : gpu_unlock_610_unlock(pdev, &shared, false);pci_dev_unlock(pdev);if (ret)pci_err(pdev, "gpu_unlock: unlock failed (%d)\n", ret);elsepci_info(pdev, "gpu_unlock: unlocked\n");- pci_dev_put(pdev);+ card->unlocked = !ret;+}++/* After resume, before nvidia resumes the card it suspended. Losing power in+ * suspend to RAM reset the selectors. Runs concurrently for each card. */+static void unlock_again(void *data, async_cookie_t cookie)+{+ struct card *card = data;+ struct pci_dev *pdev = card->pdev;+ bool nvidia;+ int ret = 0;++ pci_dev_lock(pdev);+ nvidia = pdev->driver && !strcmp(pdev->driver->name, "nvidia");+ if (nvidia)+ ret = gpu_unlock_610_unlock(pdev, &shared, true);+ pci_dev_unlock(pdev);+ if (!nvidia)+ pci_info(pdev, "gpu_unlock: not bound to nvidia, left as is after resume\n");+ else if (ret == -EALREADY)+ pci_info(pdev, "gpu_unlock: still unlocked after resume\n");+ else if (ret)+ pci_err(pdev, "gpu_unlock: unlock after resume failed (%d)\n", ret);+ else+ pci_info(pdev, "gpu_unlock: unlocked again after resume\n");+}++/* Unlock every listed card concurrently, with the GSP firmware loaded once. */+static void unlock_cards(async_func_t unlock)+{+ struct card *card;+ int ret;++ list_for_each_entry(card, &cards, node) {+ if (!shared.gsp) {+ ret = gpu_unlock_610_load_shared(card->pdev, &shared);+ if (ret) {+ pci_err(card->pdev, "gpu_unlock: firmware load failed (%d)\n", ret);+ break;+ }+ }+ async_schedule_domain(unlock, card, &unlock_domain);+ }+ async_synchronize_full_domain(&unlock_domain);+ gpu_unlock_610_release_shared(&shared);+}++static void drop_card(struct card *card)+{+ list_del(&card->node);+ pci_dev_put(card->pdev);+ kfree(card);+}++/* nvidia resumes its cards from its own notifier, at the default priority 0,+ * once this returns: every card must be finished by then. */+static int gpu_unlock_pm_notify(struct notifier_block *nb, unsigned long event, void *unused)+{+ if (event == PM_POST_SUSPEND && resume)+ unlock_cards(unlock_again);+ return NOTIFY_DONE;}+static struct notifier_block pm_nb = {+ .notifier_call = gpu_unlock_pm_notify,+ .priority = 1,+};+/* True when the comma-separated targets list names this PCI device. */static bool listed(const char *name){@@ -99,8 +181,8 @@ static void report_unmatched(void)static int __init gpu_unlock_init(void){struct pci_dev *pdev = NULL;+ struct card *card, *next;bool all;- int ret;/* Loaded ahead of nvidia on every boot; the command line is the opt-in. */if (!targets || !*targets)@@ -111,24 +193,32 @@ static int __init gpu_unlock_init(void)while ((pdev = next_supported(pdev))) {if (!all && !listed(pci_name(pdev)))continue;- if (!shared.gsp) {- ret = gpu_unlock_610_load_shared(pdev, &shared);- if (ret) {- pci_err(pdev, "gpu_unlock: firmware load failed (%d)\n", ret);- pci_dev_put(pdev);- return 0;- }+ card = kzalloc(sizeof(*card), GFP_KERNEL);+ if (!card) {+ pci_dev_put(pdev);+ break;}- async_schedule_domain(unlock_one, pci_dev_get(pdev), &unlock_domain);+ card->pdev = pci_dev_get(pdev);+ list_add_tail(&card->node, &cards);}/* nvidia loads once this returns: every card must be finished by then. */- async_synchronize_full_domain(&unlock_domain);- gpu_unlock_610_release_shared(&shared);+ unlock_cards(unlock_one);+ list_for_each_entry_safe(card, next, &cards, node)+ if (!card->unlocked)+ drop_card(card);+ if (!list_empty(&cards))+ register_pm_notifier(&pm_nb);return 0;}static void __exit gpu_unlock_exit(void){+ struct card *card, *next;++ if (!list_empty(&cards))+ unregister_pm_notifier(&pm_nb);+ list_for_each_entry_safe(card, next, &cards, node)+ drop_card(card);}module_init(gpu_unlock_init);diff --git a/kernel/gpu_unlock/unlock_610.c b/kernel/gpu_unlock/unlock_610.cindex 5a93a8a..2d45133 100644--- a/kernel/gpu_unlock/unlock_610.c+++ b/kernel/gpu_unlock/unlock_610.c@@ -2,7 +2,8 @@/** Pre-driver V67 unlock, run before the stock driver binds: FWSEC-FRTS from* the board ROM, Booter with the V67 signature, the SS0/SS1 selector writes, then- * FLR so the stock driver starts from a clean, halted GSP.+ * FLR so the stock driver starts from a clean, halted GSP. After resume from+ * suspend it runs again on the card nvidia has suspended, before nvidia resumes it.*/#include <linux/aperture.h>#include <linux/delay.h>@@ -41,6 +42,7 @@ struct unlock_transaction_page {struct unlock_transaction_work {struct pci_dev *pdev;+ bool resume; /* bound to nvidia, which suspended it before sleep */void __iomem *bar;resource_size_t bar_len;u16 original_cmd;@@ -562,9 +564,10 @@ static int unlock_transaction_unwind(struct unlock_transaction_work *work)work->boot_fw = NULL;}/* Match the driver core's entry condition, not just BAR/mask cleanup.- * Never release unrelated resources to make this check pass.+ * Never release unrelated resources to make this check pass. A bound+ * driver keeps its own resources and is not probed again.*/- if (check_devres_empty(work, "handoff"))+ if (!work->resume && check_devres_empty(work, "handoff"))ret = -EBUSY;if (work->streaming_set && work->streaming_ptr) {if (work->pdev->dev.dma_mask != work->streaming_ptr)@@ -590,6 +593,10 @@ static int unlock_transaction_unwind(struct unlock_transaction_work *work)want = gpu_unlock_command_restore(work->original_cmd, now);if (want != now && pci_write_config_word(work->pdev, PCI_COMMAND, want))return GU_RESULT(-EIO);+ /* nvidia does not set BME again on resume. pci_clear_master() before the+ * FLR also cleared the PCI core's record of it; set both back. */+ if (work->resume && (want & PCI_COMMAND_MASTER))+ pci_set_master(work->pdev);if (diag_pci_word(work, false, PCI_COMMAND, &now))return GU_RESULT(-EIO);dev_info(&work->pdev->dev, "restore PCI command: actual=%#x original=%#x mask=%#x\n",@@ -605,9 +612,11 @@ static int unlock_transaction_assets(struct unlock_transaction_work *work)const struct firmware *desc;int ret;- ret = check_devres_empty(work, "entry");- if (ret)- return ret;+ if (!work->resume) {+ ret = check_devres_empty(work, "entry");+ if (ret)+ return ret;+ }ret = request_asset(work, &work->v67_fw, V67_FILE);if (ret)return GU_RESULT(ret);@@ -824,18 +833,24 @@ static int unlock_transaction_claim(struct unlock_transaction_work *work)diag_pci_word(work, false, PCI_COMMAND, &command) ||command == 0xffff || (command & PCI_COMMAND_MASTER))return GU_RESULT(-EBUSY);- dev_info(&work->pdev->dev, "claim: cleared firmware BME\n");- work->original_cmd = command;+ dev_info(&work->pdev->dev, "claim: cleared %s BME\n",+ work->resume ? "driver" : "firmware");+ /* Firmware's BME stays off; the suspended driver gets its own back. */+ if (!work->resume)+ work->original_cmd = command;}dev_info(&work->pdev->dev, "claim: command=%#x capability=%#x BAR_mask=%#x\n",command, cap, pci_select_bars(work->pdev, IORESOURCE_MEM));/* Only BAR0 is mapped. On the display card the firmware framebuffer keeps* BAR1 until just before the FLR, as it does while the stock driver boots.+ * After resume nvidia keeps the BAR0 region it requested at probe.*/- ret = pci_request_region(work->pdev, 0, "gpu_unlock");- if (ret)- return GU_RESULT(ret);- work->region = true;+ if (!work->resume) {+ ret = pci_request_region(work->pdev, 0, "gpu_unlock");+ if (ret)+ return GU_RESULT(ret);+ work->region = true;+ }if (!(command & PCI_COMMAND_MEMORY)) {ret = pci_enable_device_mem(work->pdev);if (ret)@@ -851,6 +866,8 @@ static int unlock_transaction_claim(struct unlock_transaction_work *work)ret = pci_save_state(work->pdev);if (ret)return GU_RESULT(ret);+ if (work->mapped)+ return 0;work->bar = pci_iomap(work->pdev, 0, 0);if (!work->bar)return GU_RESULT(-ENOMEM);@@ -1056,6 +1073,43 @@ static int unlock_transaction_gfw(struct unlock_transaction_work *work)}}+/* After resume nvidia has suspended the card: its GSP-RM is down and BAR0's+ * region stays nvidia's, so BAR0 is mapped without a request. Nothing is+ * written until the card is known to have lost power: WPR2 down, the selectors+ * back at their power-on values and GFW finished again.+ */+static int unlock_transaction_suspended(struct unlock_transaction_work *work)+{+ u32 wpr_hi, ss0, ss1;+ u16 command;++ if (diag_pci_word(work, false, PCI_COMMAND, &command) || command == 0xffff ||+ !(command & PCI_COMMAND_MEMORY))+ return GU_RESULT(-ENODEV);+ work->bar_len = pci_resource_len(work->pdev, 0);+ if (work->bar_len < SEC2_FALCON2 + 0x1000)+ return GU_RESULT(-EINVAL);+ work->bar = pci_iomap(work->pdev, 0, 0);+ if (!work->bar)+ return GU_RESULT(-ENOMEM);+ work->mapped = true;+ /* Read before GFW completes, so an unreadable value proves nothing yet. */+ if (read_bar(work, 0x001fa828, &wpr_hi) ||+ read_bar(work, GU_FEAT_SS0_OFFSET, &ss0) ||+ read_bar(work, GU_FEAT_SS1_OFFSET, &ss1))+ return GU_RESULT(-EIO);+ dev_info(&work->pdev->dev, "resume entry: wpr2_hi=%#x ss0=%#x ss1=%#x\n",+ wpr_hi, ss0, ss1);+ if (!inaccessible(wpr_hi) && (wpr_hi >> 4)) {+ dev_info(&work->pdev->dev, "resume: GSP-RM already running (WPR2 enabled); card left as is\n");+ return -EBUSY;+ }+ /* Sleep that kept the card powered (suspend to idle, an aborted suspend). */+ if (ss0 == GU_SS0_UNLOCK && ss1 == GU_SS1_UNLOCK)+ return -EALREADY;+ return unlock_transaction_gfw(work);+}+static int unlock_transaction_boot(struct unlock_transaction_work *work){static const struct falcon_eng sec2 = {@@ -1307,11 +1361,13 @@ static int unlock_transaction_reset(struct unlock_transaction_work *work)*//* FLR resets the display engine, so a firmware framebuffer (simpledrm,* efifb) on this card must go first; nvidia-drm replaces it later. This- * does nothing on a card without one.+ * does nothing on a card without one. After resume nvidia-drm already has.*/- ret = aperture_remove_conflicting_pci_devices(work->pdev, "gpu_unlock");- if (ret)- return GU_RESULT(ret);+ if (!work->resume) {+ ret = aperture_remove_conflicting_pci_devices(work->pdev, "gpu_unlock");+ if (ret)+ return GU_RESULT(ret);+ }dev_info(&work->pdev->dev, "PCIe FLR begin: BME=0 DMA retained, chip=%#x\n", chip);ret = pcie_flr(work->pdev);dev_info(&work->pdev->dev, "PCIe FLR returned: result=%d\n", ret);@@ -1384,7 +1440,8 @@ void gpu_unlock_610_release_shared(struct gpu_unlock_610_shared *shared)shared->gsp = NULL;}-int gpu_unlock_610_unlock(struct pci_dev *pdev, const struct gpu_unlock_610_shared *shared)+int gpu_unlock_610_unlock(struct pci_dev *pdev, const struct gpu_unlock_610_shared *shared,+ bool resume){struct unlock_transaction_work *work;int ret, cleanup;@@ -1394,7 +1451,12 @@ int gpu_unlock_610_unlock(struct pci_dev *pdev, const struct gpu_unlock_610_sharreturn -ENOMEM;work->pdev = pci_dev_get(pdev);work->gsp_fw = shared->gsp;- ret = unlock_transaction_assets(work);+ work->resume = resume;+ /* After resume GFW may still be running: check and wait before reading+ * the layout, and before loading assets for a card that kept power. */+ ret = resume ? unlock_transaction_suspended(work) : 0;+ if (!ret)+ ret = unlock_transaction_assets(work);if (!ret)ret = unlock_transaction_claim(work);if (!ret)diff --git a/kernel/gpu_unlock/unlock_610.h b/kernel/gpu_unlock/unlock_610.hindex dbca2d9..ff70a76 100644--- a/kernel/gpu_unlock/unlock_610.h+++ b/kernel/gpu_unlock/unlock_610.h@@ -2,6 +2,8 @@#ifndef GPU_UNLOCK_UNLOCK_610_H#define GPU_UNLOCK_UNLOCK_610_H+#include <linux/types.h>+struct firmware;struct pci_dev;@@ -20,11 +22,15 @@ int gpu_unlock_610_load_shared(struct pci_dev *pdev, struct gpu_unlock_610_sharevoid gpu_unlock_610_release_shared(struct gpu_unlock_610_shared *shared);/*- * Unlock one cold, unbound RTX 3090. The caller holds pci_dev_lock. Returns 0- * when the card is unlocked and handed back as found; otherwise a negative- * errno, with the card left locked. Unlocks of different cards may run- * concurrently with the same shared firmware.+ * Unlock one cold, unbound RTX 3090, or with resume one that nvidia suspended+ * before a suspend to RAM and resumes after this returns. The caller holds+ * pci_dev_lock. Returns 0 when the card is unlocked and handed back as found;+ * otherwise a negative errno, with the card left locked. After resume,+ * -EALREADY means the card kept power and is still unlocked, and nothing was+ * written. Unlocks of different cards may run concurrently with the same+ * shared firmware.*/-int gpu_unlock_610_unlock(struct pci_dev *pdev, const struct gpu_unlock_610_shared *shared);+int gpu_unlock_610_unlock(struct pci_dev *pdev, const struct gpu_unlock_610_shared *shared,+ bool resume);#endifdiff --git a/tests/main_shim/compat.h b/tests/main_shim/compat.hindex 8ae0e13..bc22ad2 100644--- a/tests/main_shim/compat.h+++ b/tests/main_shim/compat.h@@ -5,7 +5,9 @@#define MAIN_SHIM_H#include <pthread.h>#include <stdbool.h>+#include <stddef.h>#include <stdint.h>+#include <stdlib.h>#include <string.h>#include <strings.h>@@ -23,10 +25,13 @@#define PCI_VENDOR_ID_NVIDIA 0x10de#define PCI_ANY_ID (~0u)#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))+struct pci_driver {+ const char *name;+};struct pci_dev {const char *name;unsigned short vendor, device;- void *driver;+ struct pci_driver *driver;int refs;pthread_mutex_t lock;pthread_t owner;@@ -54,4 +59,47 @@ struct async_domain {#define ASYNC_DOMAIN_EXCLUSIVE(name) struct async_domain name = { .lock = PTHREAD_MUTEX_INITIALIZER }async_cookie_t async_schedule_domain(async_func_t, void *, struct async_domain *);void async_synchronize_full_domain(struct async_domain *);+#define GFP_KERNEL 0+static inline void *kzalloc(size_t size, int flags) { return calloc(1, size); }+#define kfree free++/* The list operations main.c uses. */+struct list_head {+ struct list_head *next, *prev;+};+#define LIST_HEAD(name) struct list_head name = { &(name), &(name) }+static inline bool list_empty(const struct list_head *head) { return head->next == head; }+static inline void list_add_tail(struct list_head *node, struct list_head *head)+{+ node->prev = head->prev;+ node->next = head;+ head->prev->next = node;+ head->prev = node;+}+static inline void list_del(struct list_head *node)+{+ node->prev->next = node->next;+ node->next->prev = node->prev;+ node->next = node->prev = NULL;+}+#define list_entry(ptr, type, member) ((type *)((char *)(ptr) - offsetof(type, member)))+#define list_for_each_entry(pos, head, member) \+ for (pos = list_entry((head)->next, __typeof__(*pos), member); \+ &pos->member != (head); \+ pos = list_entry(pos->member.next, __typeof__(*pos), member))+#define list_for_each_entry_safe(pos, n, head, member) \+ for (pos = list_entry((head)->next, __typeof__(*pos), member), \+ n = list_entry(pos->member.next, __typeof__(*pos), member); \+ &pos->member != (head); \+ pos = n, n = list_entry(n->member.next, __typeof__(*n), member))++#define PM_SUSPEND_PREPARE 0x0003+#define PM_POST_SUSPEND 0x0004+#define NOTIFY_DONE 0x0000+struct notifier_block {+ int (*notifier_call)(struct notifier_block *, unsigned long, void *);+ int priority;+};+int register_pm_notifier(struct notifier_block *);+int unregister_pm_notifier(struct notifier_block *);#endifdiff --git a/tests/main_shim/linux/list.h b/tests/main_shim/linux/list.hnew file mode 100644index 0000000..4715430--- /dev/null+++ b/tests/main_shim/linux/list.h@@ -0,0 +1,2 @@+/* SPDX-License-Identifier: GPL-2.0-only */+#include "../compat.h"diff --git a/tests/main_shim/linux/slab.h b/tests/main_shim/linux/slab.hnew file mode 100644index 0000000..4715430--- /dev/null+++ b/tests/main_shim/linux/slab.h@@ -0,0 +1,2 @@+/* SPDX-License-Identifier: GPL-2.0-only */+#include "../compat.h"diff --git a/tests/main_shim/linux/suspend.h b/tests/main_shim/linux/suspend.hnew file mode 100644index 0000000..4715430--- /dev/null+++ b/tests/main_shim/linux/suspend.h@@ -0,0 +1,2 @@+/* SPDX-License-Identifier: GPL-2.0-only */+#include "../compat.h"diff --git a/tests/test_main.c b/tests/test_main.cindex 063085b..d1c2a92 100644--- a/tests/test_main.c+++ b/tests/test_main.c@@ -1,6 +1,8 @@// SPDX-License-Identifier: GPL-2.0-only/* main.c against mocked PCI and a mocked unlock: which cards are unlocked,- * that they run concurrently, and that the shared firmware outlives them. */+ * that they run concurrently, that the shared firmware outlives them, and+ * that cards unlocked at boot are unlocked again after resume, before the+ * notifier chain reaches nvidia. */#define _GNU_SOURCE#include <assert.h>#include <errno.h>@@ -22,12 +24,17 @@ static char gsp_marker;#define fake_gsp ((const struct firmware *)&gsp_marker)static pthread_mutex_t fake_lock = PTHREAD_MUTEX_INITIALIZER;static pthread_cond_t fake_cond = PTHREAD_COND_INITIALIZER;+static struct pci_driver nvidia_driver = { "nvidia" }, vfio_driver = { "vfio-pci" };static struct {int loads, releases, fail_load;+ bool loaded;struct pci_dev *load_dev;int started, finished, in_flight, max_in_flight;int expected_concurrent; /* unlocks that must overlap */int unlocks[MAX_DEVICES], fail_unlock[MAX_DEVICES];+ int resumes[MAX_DEVICES], fail_resume[MAX_DEVICES];+ struct notifier_block *nb;+ int registrations;char log[16384];} fake;@@ -126,14 +133,15 @@ void async_synchronize_full_domain(struct async_domain *domain)int gpu_unlock_610_load_shared(struct pci_dev *pdev, struct gpu_unlock_610_shared *shared){pthread_mutex_lock(&fake_lock);- /* Loaded once, before any unlock can use it. */- assert(!fake.started && !shared->gsp);+ /* Loaded once per round, before any unlock can use it. */+ assert(!fake.in_flight && !shared->gsp);fake.loads++;fake.load_dev = pdev;pthread_mutex_unlock(&fake_lock);if (fake.fail_load)return fake.fail_load;shared->gsp = fake_gsp;+ fake.loaded = true;return 0;}@@ -142,24 +150,31 @@ void gpu_unlock_610_release_shared(struct gpu_unlock_610_shared *shared)pthread_mutex_lock(&fake_lock);/* Only after every unlock has returned. */assert(!fake.in_flight && fake.finished == fake.started);- assert(shared->gsp == (fake.loads && !fake.fail_load ? fake_gsp : NULL));+ assert(shared->gsp == (fake.loaded ? fake_gsp : NULL));fake.releases++;+ fake.loaded = false;shared->gsp = NULL;pthread_mutex_unlock(&fake_lock);}-int gpu_unlock_610_unlock(struct pci_dev *pdev, const struct gpu_unlock_610_shared *shared)+int gpu_unlock_610_unlock(struct pci_dev *pdev, const struct gpu_unlock_610_shared *shared,+ bool resume){unsigned int index = pdev - devices;struct timespec deadline;int ret;assert(shared->gsp == fake_gsp);- assert(pdev->locked && pthread_equal(pdev->owner, pthread_self()) && !pdev->driver);- /* The initial reference, the worker's, and nothing dropped early. */+ assert(pdev->locked && pthread_equal(pdev->owner, pthread_self()));+ /* Unbound at boot; after resume only a card nvidia suspended. */+ assert(resume ? pdev->driver == &nvidia_driver : !pdev->driver);+ /* The initial reference, the card list's, and nothing dropped early. */assert(__atomic_load_n(&pdev->refs, __ATOMIC_SEQ_CST) >= 2);pthread_mutex_lock(&fake_lock);- fake.unlocks[index]++;+ if (resume)+ fake.resumes[index]++;+ else+ fake.unlocks[index]++;fake.started++;if (++fake.in_flight > fake.max_in_flight)fake.max_in_flight = fake.in_flight;@@ -178,19 +193,32 @@ int gpu_unlock_610_unlock(struct pci_dev *pdev, const struct gpu_unlock_610_sharpthread_mutex_lock(&fake_lock);fake.in_flight--;fake.finished++;- ret = fake.fail_unlock[index];+ ret = resume ? fake.fail_resume[index] : fake.fail_unlock[index];pthread_mutex_unlock(&fake_lock);return ret;}-static int driver_marker;+int register_pm_notifier(struct notifier_block *nb)+{+ assert(!fake.nb);+ fake.nb = nb;+ fake.registrations++;+ return 0;+}++int unregister_pm_notifier(struct notifier_block *nb)+{+ assert(fake.nb == nb);+ fake.nb = NULL;+ return 0;+}static void add(const char *name, unsigned short device, bool bound){struct pci_dev *pdev = &devices[device_count++];*pdev = (struct pci_dev){ .name = name, .vendor = PCI_VENDOR_ID_NVIDIA, .device = device,- .driver = bound ? &driver_marker : NULL, .refs = 1 };+ .driver = bound ? &nvidia_driver : NULL, .refs = 1 };pthread_mutex_init(&pdev->lock, NULL);}@@ -199,18 +227,39 @@ static void reset(char *list)memset(&fake, 0, sizeof(fake));device_count = 0;targets = list;+ resume = true;}-/* Runs init, then checks what holds for every case. */-static void run(void)+static void boot(void){assert(!test_module_init());- test_module_exit();assert(!fake.in_flight && fake.finished == fake.started);- for (unsigned int i = 0; i < device_count; i++) {+ for (unsigned int i = 0; i < device_count; i++)+ assert(!devices[i].locked && fake.unlocks[i] <= 1);+}++/* Calls the notifier as the PM core does; returns once it has. */+static void notify(unsigned long event)+{+ assert(fake.nb && fake.nb->notifier_call(fake.nb, event, NULL) == NOTIFY_DONE);+ /* nvidia's notifier runs next: no unlock may still be running. */+ assert(!fake.in_flight && fake.finished == fake.started);+}++/* Unloads the module and checks every reference came back. */+static void shutdown(void)+{+ test_module_exit();+ assert(!fake.nb);+ for (unsigned int i = 0; i < device_count; i++)assert(devices[i].refs == 1 && !devices[i].locked);- assert(fake.unlocks[i] <= 1);- }+}++/* Runs init, then checks what holds for every case. */+static void run(void)+{+ boot();+ shutdown();assert(fake.loads <= 1 && fake.releases <= 1);}@@ -267,7 +316,7 @@ int main(void)add("0000:21:00.0", RTX_3090, false);fake.fail_load = -ENOENT;run();- assert(fake.loads == 1 && !fake.started && !fake.releases);+ assert(fake.loads == 1 && !fake.started && fake.releases == 1 && !fake.registrations);assert(logged("0000:01:00.0: gpu_unlock: firmware load failed (-2)"));puts("firmware load failure: nothing attempted PASS");@@ -276,7 +325,7 @@ int main(void)add("0000:01:00.0", RTX_3090, false);add("0000:31:00.0", RTX_A2000, false);run();- assert(!fake.loads && !fake.started && fake.releases == 1);+ assert(!fake.loads && !fake.started && fake.releases == 1 && !fake.registrations);assert(logged("gpu_unlock: no supported card at \"0000:99:00.0\""));assert(logged("gpu_unlock: no supported card at \"0000:31:00.0\""));puts("unmatched target: reported, nothing loaded PASS");@@ -287,7 +336,87 @@ int main(void)add("0000:01:00.0", RTX_3090, false);run();assert(!fake.loads && !fake.started && !fake.releases && !fake.log[0]);+ assert(!fake.registrations);}puts("no targets: idle PASS");++ /* After resume, the cards unlocked at boot, concurrently, and all of them+ * finished before the notifier returns. A card that failed at boot is not+ * tried again; suspending does nothing. */+ reset("all");+ add("0000:01:00.0", RTX_3090, false);+ add("0000:21:00.0", RTX_3070, false);+ add("0000:41:00.0", RTX_3090, false);+ fake.fail_unlock[2] = -EIO;+ boot();+ assert(fake.registrations == 1 && fake.nb->priority > 0);+ for (unsigned int i = 0; i < device_count; i++)+ devices[i].driver = &nvidia_driver;+ notify(PM_SUSPEND_PREPARE);+ assert(fake.loads == 1 && fake.started == 3);+ fake.expected_concurrent = 2;+ fake.max_in_flight = 0;+ notify(PM_POST_SUSPEND);+ assert(fake.max_in_flight == 2 && fake.started == 5);+ assert(fake.resumes[0] == 1 && fake.resumes[1] == 1 && !fake.resumes[2]);+ assert(fake.loads == 2 && fake.load_dev == &devices[0] && fake.releases == 2);+ assert(logged("0000:01:00.0: gpu_unlock: unlocked again after resume"));+ assert(logged("0000:21:00.0: gpu_unlock: unlocked again after resume"));+ assert(!logged("0000:41:00.0: gpu_unlock: unlocked again"));+ shutdown();+ assert(fake.unlocks[0] == 1 && fake.unlocks[1] == 1 && fake.unlocks[2] == 1);+ puts("resume: boot-unlocked cards unlocked again before nvidia PASS");++ /* Only cards nvidia suspended are touched. A card that kept power, or one+ * that fails, stays listed for the next resume. */+ reset("all");+ add("0000:01:00.0", RTX_3090, false);+ add("0000:21:00.0", RTX_3090, false);+ add("0000:31:00.0", RTX_3090, false);+ add("0000:61:00.0", RTX_3090, false);+ boot();+ devices[0].driver = &nvidia_driver;+ devices[1].driver = &nvidia_driver;+ devices[2].driver = &vfio_driver;+ fake.fail_resume[0] = -EALREADY;+ fake.fail_resume[1] = -EBUSY;+ notify(PM_POST_SUSPEND);+ assert(fake.resumes[0] == 1 && fake.resumes[1] == 1 && !fake.resumes[2] && !fake.resumes[3]);+ assert(logged("0000:01:00.0: gpu_unlock: still unlocked after resume"));+ assert(logged("0000:21:00.0: gpu_unlock: unlock after resume failed (-16)"));+ assert(logged("0000:31:00.0: gpu_unlock: not bound to nvidia, left as is after resume"));+ assert(logged("0000:61:00.0: gpu_unlock: not bound to nvidia, left as is after resume"));+ notify(PM_POST_SUSPEND);+ assert(fake.resumes[0] == 2 && fake.resumes[1] == 2 && !fake.resumes[2] && !fake.resumes[3]);+ shutdown();+ puts("resume: only nvidia-suspended cards, kept for the next resume PASS");++ /* gpu_unlock.resume=0, and a firmware load failure, leave the cards to nvidia. */+ reset("0000:01:00.0");+ add("0000:01:00.0", RTX_3090, false);+ boot();+ devices[0].driver = &nvidia_driver;+ resume = false;+ notify(PM_POST_SUSPEND);+ assert(fake.loads == 1 && !fake.resumes[0]);+ resume = true;+ fake.fail_load = -ENOENT;+ notify(PM_POST_SUSPEND);+ assert(fake.loads == 2 && fake.releases == 2 && !fake.resumes[0]);+ assert(logged("0000:01:00.0: gpu_unlock: firmware load failed (-2)"));+ fake.fail_load = 0;+ notify(PM_POST_SUSPEND);+ assert(fake.resumes[0] == 1);+ shutdown();+ puts("resume: switch off and firmware failure leave cards locked PASS");++ /* Nothing unlocked at boot: no notifier at all. */+ reset("all");+ add("0000:01:00.0", RTX_3090, false);+ fake.fail_unlock[0] = -EIO;+ boot();+ assert(!fake.registrations && !fake.nb);+ shutdown();+ puts("resume: no notifier without an unlocked card PASS");return 0;}diff --git a/tests/test_unlock_adapter.c b/tests/test_unlock_adapter.cindex 1b5d32d..778b251 100644--- a/tests/test_unlock_adapter.c+++ b/tests/test_unlock_adapter.c@@ -26,6 +26,8 @@ static struct {int post_gfw,post_gfw_reads,gsp_post_reset_writes;int fwsec_wpr;u32 fwsec_wpr_lo,fwsec_wpr_hi; /* WPR2 that FWSEC-FRTS enables */int apertures,regions;+ int bound,gfw_late,gfw_late_reads; /* resume: nvidia bound; GFW done after N reads */+ int busmaster; /* the PCI core's is_busmaster */char log[32768];} fake;void *kzalloc(size_t n,int flags) {@@ -71,6 +73,10 @@ void usleep_range(unsigned long a,unsigned long b) {fake.now+=a;}ktime_t ktime_get(void) {return fake.now;}u32 readl(const void *p) {size_t n=((const u8 *)p-(const u8 *)regs)/4;assert(n<ARRAY_SIZE(regs));+ /* Power-on GFW still running: the FB size appears only once it is done. */+ if(!fake.flrs && n==GU_GFW_BOOT_OFFSET/4 && fake.gfw_late && ++fake.gfw_late_reads==fake.gfw_late) {+ regs[n]=0xff;regs[0x1183a4/4]=0x6000;+ }if(fake.flrs && !n && fake.bar_ready_after) {fake.bar_ready_reads++;if(fake.bar_ready_after<0 || fake.bar_ready_reads<fake.bar_ready_after)@@ -172,13 +178,14 @@ int pci_is_pcie(struct pci_dev *p) {return 1;}int pci_select_bars(struct pci_dev *p,int r) {return 7;}int pci_request_region(struct pci_dev *p,int bar,const char *n) {assert(!bar); /* only BAR0: the display card's framebuffer keeps BAR1 */- if(fake.fail_claim)return -EBUSY;+ if(fake.fail_claim || fake.bound)return -EBUSY; /* nvidia holds it after resume */fake.regions++;return 0;}void pci_release_region(struct pci_dev *p,int bar) {assert(!bar && fake.regions==1);fake.regions--;}int aperture_remove_conflicting_pci_devices(struct pci_dev *p,const char *n) {- /* The framebuffer stays through FWSEC and Booter; it goes right before FLR. */- assert(!fake.flrs && !(fake.cmd&4));+ /* The framebuffer stays through FWSEC and Booter; it goes right before FLR.+ * After resume nvidia-drm already replaced it. */+ assert(!fake.flrs && !(fake.cmd&4) && !fake.bound);if(full_active)assert(fake.starts==1);fake.apertures++;return 0;}@@ -191,11 +198,11 @@ void *pci_map_rom(struct pci_dev *p,size_t *n) {return NULL;}void pci_unmap_rom(struct pci_dev *p,void *v) {}int pci_save_state(struct pci_dev *p) {fake.saved=fake.cmd;return fake.fail_save?-EIO:0;}void pci_restore_state(struct pci_dev *p) {assert(!(fake.saved&4));fake.cmd=fake.saved;}-void pci_set_master(struct pci_dev *p) {if(!fake.master_fail)fake.cmd|=4;}-void pci_clear_master(struct pci_dev *p) {fake.cmd&=~4;}+void pci_set_master(struct pci_dev *p) {if(!fake.master_fail){fake.cmd|=4;fake.busmaster=1;}}+void pci_clear_master(struct pci_dev *p) {fake.cmd&=~4;fake.busmaster=0;}int pcie_flr(struct pci_dev *p) {if(full_active)check_restored_signature();- if(full_active)assert(fake.apertures==1);+ if(full_active)assert(fake.apertures==!fake.bound);fake.flrs++;if(fake.late_devres)p->dev.devres_head.count++;/* FLR releases engine reset; post-FLR assertion must work independently. */@@ -545,7 +552,9 @@ int main(int argc, char **argv) {"post-gfw-late","post-gfw-timeout","post-gfw-inaccessible","post-gsp-resumed","wpr-frts-retained","firmware-group-oom-first","firmware-group-oom-later","devres-preexisting","devres-at-handoff","warm-wpr2","ss1-readback","ss1-lost","power-unknown","power-unknown-no-pm",- "firmware-bme","firmware-bme-stuck"};+ "firmware-bme","firmware-bme-stuck",+ "resume-success","resume-gfw-late","resume-kept-power","resume-gsp-running",+ "resume-gfw-timeout","resume-oom"};int total_allocations=0;for(unsigned scenario=0;scenario<ARRAY_SIZE(cases);scenario++) {setup(&w);@@ -611,24 +620,41 @@ int main(int argc, char **argv) {if(scenario==58)dev.pm_cap=0x60;if(scenario==60 || scenario==61)fake.cmd=6; /* firmware left BME set */if(scenario==61)fake.master_stuck=1;+ /* After resume: nvidia bound with its BME and device resources,+ * the card powered off and on again. */+ bool bound=!strncmp(cases[scenario],"resume-",7);+ if(bound){fake.bound=1;fake.cmd=6;fake.busmaster=1;dev.dev.devres_head.count=1;}+ if(!strcmp(cases[scenario],"resume-gfw-late")) {+ fake.gfw_late=5;regs[GU_GFW_BOOT_OFFSET/4]=0;regs[0x1183a4/4]=0;+ }+ if(!strcmp(cases[scenario],"resume-kept-power")) {+ regs[GU_FEAT_SS0_OFFSET/4]=0x88888888;regs[GU_FEAT_SS1_OFFSET/4]=0x8;+ }+ if(!strcmp(cases[scenario],"resume-gsp-running")) { /* GSP-RM up: not halted, WPR2 enabled */+ regs[0x1fa828/4]=fake.fwsec_wpr_hi;regs[GU_GFW_CPUCTL_OFFSET/4]=0;+ }+ if(!strcmp(cases[scenario],"resume-gfw-timeout"))regs[GU_GFW_BOOT_OFFSET/4]=0;+ if(!strcmp(cases[scenario],"resume-oom"))fake.fail_alloc=1;/* As main.c: the GSP file is loaded once, ahead of every card. */struct gpu_unlock_610_shared shared={0};last_work=NULL;int ret=gpu_unlock_610_load_shared(&dev,&shared);- if(!ret)ret=gpu_unlock_610_unlock(&dev,&shared);+ if(!ret)ret=gpu_unlock_610_unlock(&dev,&shared,bound);gpu_unlock_610_release_shared(&shared);bool expected=scenario<2 || (scenario>=33 && scenario<=35) || scenario==39 || scenario==40 || scenario==43 || scenario==46 ||- scenario==58 || scenario==60;+ scenario==58 || scenario==60 ||+ !strcmp(cases[scenario],"resume-success") || !strcmp(cases[scenario],"resume-gfw-late");bool retained=fake.pins;if((!ret)!=expected)fprintf(stderr,"%s\n%s: error=%d\n",fake.log,cases[scenario],ret);assert((!ret)==expected);if(expected) {assert(fake.gsp_starts==1 && fake.starts==1 && fake.flrs==1 && !fake.pins);- assert(fake.apertures==1 && !fake.regions);+ assert(fake.apertures==!bound && !fake.regions);assert(fake.selector_writes==(scenario==1?0:1) && fake.ss1_writes==(scenario==1?0:1));assert(regs[GU_FEAT_SS0_OFFSET/4]==0x88888888 && regs[GU_FEAT_SS1_OFFSET/4]==0x8);assert(mask==0xffffffffffffull && dev.dev.coherent_dma_mask==0xfffffffffull);- /* Mirror Linux really_probe: driver entry rejects nonempty devres. */- assert(list_empty(&dev.dev.devres_head));+ /* Mirror Linux really_probe: driver entry rejects nonempty devres.+ * A bound driver keeps its own. */+ assert(dev.dev.devres_head.count==(bound?1u:0u));assert(fake.cache_records==5 && fake.cache_released==5);/* Pinned RM checks GSP halt + readable completed GFW BEFORE* its own reset/bootstrap. Preserve that entry contract. */@@ -654,10 +680,22 @@ int main(int argc, char **argv) {assert(!unlock_transaction_unwind(last_work));pci_dev_put(last_work->pdev);free(last_work);}- /* A card whose BME will not clear is left as found. */- assert(!fake.live && !fake.fw_live && dev.refs==1 && (scenario==61 || !(fake.cmd&4)));+ /* A card whose BME will not clear is left as found; nvidia gets its own back. */+ assert(!fake.live && !fake.fw_live && dev.refs==1 && !!(fake.cmd&4)==(scenario==61 || bound));+ if(bound)assert(fake.busmaster);assert(!fake.group_live && fake.cache_records==fake.cache_released);- assert(dev.dev.devres_head.count==(scenario==53 || scenario==54?1u:0u));+ assert(dev.dev.devres_head.count==(scenario==53 || scenario==54 || bound?1u:0u));+ assert(dev.dev.dma_mask==&mask && mask==0xffffffffffffull && dev.dev.coherent_dma_mask==0xfffffffffull);+ if(!strcmp(cases[scenario],"resume-gfw-late"))assert(fake.gfw_late_reads>=fake.gfw_late);+ /* A card that kept power or runs GSP-RM is only read. */+ if(!strcmp(cases[scenario],"resume-kept-power") || !strcmp(cases[scenario],"resume-gsp-running")) {+ assert(ret==(cases[scenario][7]=='k'?-EALREADY:-EBUSY));+ assert(!fake.writes && !fake.allocs && fake.fw_requests==1 && fake.cmd==6 && !fake.now);+ }+ if(!strcmp(cases[scenario],"resume-gfw-timeout"))+ assert(ret==-ETIMEDOUT && !fake.writes && !fake.allocs && fake.cmd==6);+ if(!strcmp(cases[scenario],"resume-oom"))+ assert(ret==-ENOMEM && !fake.gsp_starts && !fake.flrs && fake.cmd==6);if(scenario>=51 && scenario<=53)assert(!fake.writes && !fake.allocs && !fake.flrs);if(scenario==53)assert(fake.fw_requests==1 && ret==-EBUSY); /* only the shared load */if(scenario==3 || scenario==51)assert(!last_work && fake.fw_requests==(scenario==3));