mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
279 lines
10 KiB
C
279 lines
10 KiB
C
#define _GNU_SOURCE
|
|
// SPDX-License-Identifier: MIT
|
|
// GP100 CE0 fence preparation through the normal RM allocation/control/map ABI.
|
|
// Build: cc -std=c11 -O2 -Wall -Wextra -Werror tools/pascal_rmapi_fence_probe.c -o target/pascal-rmapi-fence-probe
|
|
// Offline: target/pascal-rmapi-fence-probe --self-test
|
|
// Preparation does not install GPU PTEs, schedule, or write USERD.
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <inttypes.h>
|
|
#include <signal.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/ioctl.h>
|
|
#include <sys/mman.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
#define ESC(n,s) _IOC(_IOC_READ|_IOC_WRITE,'F',(n),(s))
|
|
struct alloc { uint32_t root,parent,object,klass; uint64_t params,rights; uint32_t size,flags,status,pad; };
|
|
struct ctrl { uint32_t client,object,cmd,flags; uint64_t params; uint32_t size,status; };
|
|
struct freereq { uint32_t root,parent,object,status; };
|
|
struct mapreq { uint32_t client,device,memory,pad; uint64_t offset,length,address; uint32_t status,flags; int32_t fd; uint32_t pad2; };
|
|
struct mem { uint32_t owner,type,flags,width,height,pitch,attr,attr2,format,compr,zcull,pad; uint64_t lo,hi,size,align,offset,limit,address; uint32_t ctag,space,internal,tag,node,pad2; };
|
|
struct card { uint8_t valid,pad[3]; uint32_t domain; uint8_t bus,slot,function,pad2; uint16_t vendor,device; uint32_t gpu_id; uint16_t irq,pad3; uint64_t reg,reg_size,fb,fb_size; uint32_t minor; uint8_t name[10],pad4[2]; };
|
|
struct version { uint32_t cmd,reply; char text[64]; };
|
|
_Static_assert(sizeof(struct alloc)==48&&sizeof(struct ctrl)==32&&sizeof(struct freereq)==16,"RM envelope ABI");
|
|
_Static_assert(sizeof(struct mapreq)==56&&sizeof(struct mem)==128,"RM memory ABI");
|
|
_Static_assert(sizeof(struct card)==72&&sizeof(struct version)==72,"RM identity ABI");
|
|
enum {
|
|
DEV = 0x5c100001, SUB = 0x5c100002, VA_SPACE = 0x5c100003,
|
|
MEMORY = 0x5c100010, GROUP = 0x5c100020, CHANNEL = 0x5c100021,
|
|
COPY = 0x5c100022, PAGE_BYTES = 4096, PREPARE_TIMEOUT_SEC = 30,
|
|
};
|
|
static const uint32_t fence_packet[] = {
|
|
0x20018000, 0xc0b5, 0x20038090, 0, 0x200200, 0x17, 0x200180c0, 0x0c,
|
|
};
|
|
static int ctl = -1;
|
|
static uint32_t client;
|
|
|
|
static void put32(void *where, uint32_t value)
|
|
{
|
|
uint8_t *bytes = where;
|
|
for (unsigned i = 0; i < 4; i++)
|
|
bytes[i] = (uint8_t)(value >> (8 * i));
|
|
}
|
|
|
|
static uint32_t get32(const volatile void *where)
|
|
{
|
|
const volatile uint8_t *p = where;
|
|
return (uint32_t)p[0] | (uint32_t)p[1] << 8 |
|
|
(uint32_t)p[2] << 16 | (uint32_t)p[3] << 24;
|
|
}
|
|
|
|
static void fill_fence_page(uint8_t *page)
|
|
{
|
|
memset(page, 0, PAGE_BYTES);
|
|
put32(page, 0x200100);
|
|
// Match the observed CUDA GPFIFO flag pattern: bit 9 set, bit 8 clear.
|
|
put32(page + 4, (1u << 9) | (8u << 10));
|
|
for (unsigned i = 0; i < sizeof fence_packet / sizeof *fence_packet; i++)
|
|
put32(page + 0x100 + 4 * i, fence_packet[i]);
|
|
}
|
|
|
|
static int self_test(void)
|
|
{
|
|
uint8_t page[PAGE_BYTES];
|
|
fill_fence_page(page);
|
|
return get32(page) != 0x200100 || get32(page + 4) != 0x2200 ||
|
|
get32(page + 0x100) != 0x20018000 || get32(page + 0x114) != 0x17 ||
|
|
get32(page + 0x11c) != 0x0c || get32(page + 0x200) != 0;
|
|
}
|
|
|
|
static int checked_ioctl(unsigned cmd, void *data, size_t size, uint32_t *status,
|
|
const char *phase)
|
|
{
|
|
if (ioctl(ctl, ESC(cmd, size), data) < 0) {
|
|
perror(phase);
|
|
return -1;
|
|
}
|
|
if (*status) {
|
|
fprintf(stderr, "%s: RM status %#x\n", phase, *status);
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int allocate(uint32_t parent, uint32_t handle, uint32_t klass,
|
|
void *params, uint32_t size, const char *phase)
|
|
{
|
|
struct alloc req = {client, parent, handle, klass, (uintptr_t)params, 0, size, 0, 0, 0};
|
|
return checked_ioctl(0x2b, &req, sizeof req, &req.status, phase);
|
|
}
|
|
|
|
static int control(uint32_t object, uint32_t cmd, void *params, uint32_t size,
|
|
const char *phase)
|
|
{
|
|
struct ctrl req = {client, object, cmd, 0, (uintptr_t)params, size, 0};
|
|
return checked_ioctl(0x2a, &req, sizeof req, &req.status, phase);
|
|
}
|
|
|
|
static int map_memory(uint32_t object, uint64_t length, int fd, uint64_t *address)
|
|
{
|
|
struct mapreq req = {client, DEV, object, 0, 0, length, 0, 0, 0, fd, 0};
|
|
if (checked_ioctl(0x4e, &req, sizeof req, &req.status, "map"))
|
|
return -1;
|
|
*address = req.address;
|
|
return 0;
|
|
}
|
|
|
|
static const struct card *find_card(const struct card *cards, const char *bdf)
|
|
{
|
|
unsigned domain, bus, slot, function;
|
|
char extra;
|
|
if (sscanf(bdf, "%x:%x:%x.%x%c", &domain, &bus, &slot, &function, &extra) != 4)
|
|
return NULL;
|
|
for (unsigned i = 0; i < 32; i++) {
|
|
const struct card *card = &cards[i];
|
|
if (card->valid && card->domain == domain && card->bus == bus &&
|
|
card->slot == slot && card->function == function)
|
|
return card->vendor == 0x10de && card->device == 0x15f8 ? card : NULL;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
static int prepare(const char *bdf, uint32_t instance)
|
|
{
|
|
static const char control_node[] = "/dev/nvidia-rs-rmapi-ctl";
|
|
int gpu = -1, mapfd = -1, userdfd = -1, outcome = 1;
|
|
void *cpu = MAP_FAILED, *userd = MAP_FAILED;
|
|
uint64_t cpu_address, userd_address;
|
|
ctl = open(control_node, O_RDWR | O_CLOEXEC);
|
|
if (ctl < 0) {
|
|
perror(control_node);
|
|
return 1;
|
|
}
|
|
|
|
struct version ver = {0, 0, "580.178.04"};
|
|
if (ioctl(ctl, ESC(0xd2, sizeof ver), &ver) || ver.reply != 1) {
|
|
fprintf(stderr, "Expected NVIDIA userspace version 580.178.04\n");
|
|
goto out;
|
|
}
|
|
struct card cards[32] = {0};
|
|
if (ioctl(ctl, ESC(0xc8, sizeof cards), cards)) {
|
|
perror("card info");
|
|
goto out;
|
|
}
|
|
const struct card *card = find_card(cards, bdf);
|
|
if (!card) {
|
|
fprintf(stderr, "Expected PCIe Tesla P100 at the requested BDF\n");
|
|
goto out;
|
|
}
|
|
|
|
char gpu_node[64];
|
|
snprintf(gpu_node, sizeof gpu_node, "/dev/nvidia-rs-rmapi-gpu%u", card->minor);
|
|
gpu = open(gpu_node, O_RDWR | O_CLOEXEC);
|
|
if (gpu < 0) {
|
|
perror(gpu_node);
|
|
goto out;
|
|
}
|
|
struct alloc root = {0};
|
|
if (checked_ioctl(0x2b, &root, sizeof root, &root.status, "root") || root.klass != 0x41)
|
|
goto out;
|
|
client = root.object;
|
|
uint32_t dev[14] = {instance, client};
|
|
dev[12] = 2;
|
|
uint32_t zero = 0;
|
|
if (allocate(client, DEV, 0x80, dev, sizeof dev, "device") ||
|
|
allocate(DEV, SUB, 0x2080, &zero, sizeof zero, "subdevice"))
|
|
goto out;
|
|
uint32_t gpu_id = 0, arch[4] = {0};
|
|
if (control(SUB, 0x20800142, &gpu_id, sizeof gpu_id, "GPU ID") ||
|
|
control(SUB, 0x20801701, arch, sizeof arch, "GPU architecture") ||
|
|
gpu_id != card->gpu_id || arch[0] != 0x130 || arch[1] != 0) {
|
|
fprintf(stderr, "RM device instance does not identify the selected GP100\n");
|
|
goto out;
|
|
}
|
|
|
|
uint8_t va[56] = {0};
|
|
uint32_t group[5] = {0, 0, VA_SPACE, 9, 0};
|
|
uint8_t channel[0x170] = {0};
|
|
put32(channel + 8, 0x200000);
|
|
put32(channel + 16, 512);
|
|
uint32_t copy[2] = {0, 0};
|
|
uint32_t class_engine[4] = {COPY, 0, 0, 0};
|
|
if (allocate(DEV, VA_SPACE, 0x90f1, va, sizeof va, "RM VA space") ||
|
|
allocate(DEV, GROUP, 0xa06c, group, sizeof group, "CE0 group") ||
|
|
allocate(GROUP, CHANNEL, 0xc06f, channel, sizeof channel, "GPFIFO channel") ||
|
|
allocate(CHANNEL, COPY, 0xc0b5, copy, sizeof copy, "CE0 object") ||
|
|
control(CHANNEL, 0x906f0101, class_engine, sizeof class_engine, "class engine") ||
|
|
class_engine[2] != 0xc0b5 || class_engine[3] != 9) {
|
|
fprintf(stderr, "Could not verify the CE0 channel route\n");
|
|
goto out;
|
|
}
|
|
|
|
struct mem memory = {.owner = client, .flags = 0xc001, .attr = 0x3a000000, .size = PAGE_BYTES};
|
|
if (allocate(DEV, MEMORY, 0x3e, &memory, sizeof memory, "system page"))
|
|
goto out;
|
|
mapfd = open(control_node, O_RDWR | O_CLOEXEC);
|
|
userdfd = open(gpu_node, O_RDWR | O_CLOEXEC);
|
|
if (mapfd < 0 || userdfd < 0 || map_memory(MEMORY, PAGE_BYTES, mapfd, &cpu_address) ||
|
|
map_memory(CHANNEL, 0x200, userdfd, &userd_address))
|
|
goto out;
|
|
cpu = mmap(NULL, PAGE_BYTES, PROT_READ | PROT_WRITE, MAP_SHARED, mapfd, 0);
|
|
userd = mmap(NULL, PAGE_BYTES, PROT_READ, MAP_SHARED, userdfd, 0);
|
|
size_t displacement = userd_address & (PAGE_BYTES - 1);
|
|
if (cpu == MAP_FAILED || userd == MAP_FAILED || displacement % 0x200)
|
|
goto out;
|
|
fill_fence_page(cpu);
|
|
const uint8_t *slot = (const uint8_t *)userd + displacement;
|
|
printf("{\"phase\":\"prepared_no_gpu_mapping_or_schedule\",\"gpu_id\":%u,"
|
|
"\"rm_engine\":9,\"gp_get\":%u,\"gp_put\":%u}\n",
|
|
gpu_id, get32(slot + 0x88), get32(slot + 0x8c));
|
|
outcome = 0;
|
|
out:
|
|
// The CPU page was never inserted in a GPU page table; nothing was armed.
|
|
if (userd != MAP_FAILED)
|
|
munmap(userd, PAGE_BYTES);
|
|
if (userdfd >= 0)
|
|
close(userdfd);
|
|
if (cpu != MAP_FAILED)
|
|
munmap(cpu, PAGE_BYTES);
|
|
if (mapfd >= 0)
|
|
close(mapfd);
|
|
if (client) {
|
|
struct freereq req = {client, 0, client, 0};
|
|
if (checked_ioctl(0x29, &req, sizeof req, &req.status, "free prepared root"))
|
|
outcome = 1;
|
|
}
|
|
if (gpu >= 0)
|
|
close(gpu);
|
|
close(ctl);
|
|
return outcome;
|
|
}
|
|
|
|
static void prepare_timeout(int signum)
|
|
{
|
|
(void)signum;
|
|
_exit(124);
|
|
}
|
|
|
|
static int arm_prepare_watchdog(unsigned seconds)
|
|
{
|
|
struct sigaction action = {.sa_handler = prepare_timeout};
|
|
sigset_t alarms;
|
|
|
|
if (sigemptyset(&action.sa_mask) < 0 ||
|
|
sigaction(SIGALRM, &action, NULL) < 0 ||
|
|
sigemptyset(&alarms) < 0 ||
|
|
sigaddset(&alarms, SIGALRM) < 0 ||
|
|
sigprocmask(SIG_UNBLOCK, &alarms, NULL) < 0)
|
|
return -1;
|
|
|
|
alarm(seconds);
|
|
return 0;
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
if (argc == 2 && !strcmp(argv[1], "--self-test"))
|
|
return self_test();
|
|
if (argc == 4 && !strcmp(argv[1], "--prepare-only")) {
|
|
char *end;
|
|
unsigned long instance = strtoul(argv[3], &end, 0);
|
|
if (end != argv[3] && !*end && instance <= 31) {
|
|
if (arm_prepare_watchdog(PREPARE_TIMEOUT_SEC) < 0) {
|
|
perror("arm preparation watchdog");
|
|
return 1;
|
|
}
|
|
int result = prepare(argv[2], (uint32_t)instance);
|
|
alarm(0);
|
|
return result;
|
|
}
|
|
}
|
|
fprintf(stderr, "usage: %s --self-test | --prepare-only BDF RM_DEVICE_INSTANCE\n", argv[0]);
|
|
return 2;
|
|
}
|