summaryrefslogtreecommitdiffstats
path: root/tools/rmq/wpr2_reset.c
blob: 44358432f44559ea917669ecbb7f0e3afca9fe6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* wpr2_reset - clear GPU WPR2 registers via BAR0 MMIO to unstick GSP boot.
 *
 * When nvidia.ko is unloaded, GSP firmware can remain resident in WPR2
 * (write-protected region 2 in FB). The next RmInitAdapter then fails with
 * "unexpected WPR2 already up". FLR does not clear WPR2; only a cold (power)
 * reset does. This tool pokes WPR2_ADDR_LO/HI to zero directly through
 * /sys/bus/pci/devices/<dev>/resource0 while no driver is bound.
 *
 * Usage: sudo ./wpr2_reset [pci.bdf]   (default 0000:01:00.0)
 */
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>

#define NV_PFB_PRI_MMU_WPR2_ADDR_LO 0x1fa824
#define NV_PFB_PRI_MMU_WPR2_ADDR_HI 0x1fa828

int main(int argc, char **argv)
{
    const char *bdf = argc > 1 ? argv[1] : "0000:01:00.0";
    char path[256];
    snprintf(path, sizeof(path), "/sys/bus/pci/devices/%s/resource0", bdf);

    int fd = open(path, O_RDWR | O_SYNC);
    if (fd < 0) { perror(path); return 1; }
    void *bar = mmap(NULL, 0x200000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (bar == MAP_FAILED) { perror("mmap"); return 1; }

    volatile uint32_t *lo = (volatile uint32_t *)((char *)bar + NV_PFB_PRI_MMU_WPR2_ADDR_LO);
    volatile uint32_t *hi = (volatile uint32_t *)((char *)bar + NV_PFB_PRI_MMU_WPR2_ADDR_HI);

    printf("before: WPR2_ADDR_LO=0x%08x WPR2_ADDR_HI=0x%08x\n", *lo, *hi);
    *lo = 0;
    *hi = 0;
    /* read back */
    printf("after : WPR2_ADDR_LO=0x%08x WPR2_ADDR_HI=0x%08x\n", *lo, *hi);

    if (*hi == 0)
        printf("WPR2 cleared - driver load should now boot GSP cleanly\n");
    else
        printf("write dropped (PLM?) - WPR2 still up\n");

    munmap(bar, 0x200000);
    close(fd);
    return *hi ? 1 : 0;
}