omega-gpu-driver
aien-dev/omega-gpu-driver
Brought by drakestapleton
omega-gpu-driver / tests / rme_consumer.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | * TEST ONLY fake kernel and vendor-ABI consumer. No real ioctl/device open. */ |
| 3 | #include <assert.h> |
| 4 | #include <stdint.h> |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
| 8 | #include <unistd.h> |
| 9 | #include <sys/socket.h> |
| 10 | #include <sys/wait.h> |
| 11 | #include <sys/ioctl.h> |
| 12 | #include <linux/fuse.h> |
| 13 | #include "nvos.h" |
| 14 | #include "nv-ioctl.h" |
| 15 | #include "nv-ioctl-numbers.h" |
| 16 | #include "nv_escape.h" |
| 17 | #include "class/cl0000.h" |
| 18 | #include "class/cl0080.h" |
| 19 | #include "class/cl2080.h" |
| 20 | #include "class/cl90f1.h" |
| 21 | #include "class/cla06c.h" |
| 22 | #include "class/cl9067.h" |
| 23 | #include "class/clc96f.h" |
| 24 | #include "class/clcec0.h" |
| 25 | #include "alloc/alloc_channel.h" |
| 26 | #include "ctrl/ctrla06c.h" |
| 27 | #include "ctrl/ctrlc36f.h" |
| 28 | static int fd; static uint64_t seq,fh; |
| 29 | static unsigned char reply[16384]; |
| 30 | static size_t frame(unsigned op,const void *body,size_t size) { |
| 31 | unsigned char buf[16384]={0};struct fuse_in_header h={.len=sizeof h+size,.opcode=op,.unique=++seq}; |
| 32 | memcpy(buf,&h,sizeof h);memcpy(buf+sizeof h,body,size); |
| 33 | assert(send(fd,buf,h.len,0)==h.len);ssize_t n=recv(fd,reply,sizeof reply,0);assert(n>=16); |
| 34 | struct fuse_out_header out;memcpy(&out,reply,sizeof out);assert(out.unique==seq&&out.len==(unsigned)n&&out.error==0);return n; |
| 35 | } |
| 36 | /* Kernel retry-iovec protocol copies only addresses explicitly supplied by this consumer. |
| 37 | * The endpoint never dereferences process VAs. */ |
| 38 | static void fake_ioctl(unsigned nr, void *arg, size_t hsize, void *params, size_t psz) { |
| 39 | unsigned char buf[8192]={0};struct fuse_ioctl_in in={.fh=fh,.flags=FUSE_IOCTL_UNRESTRICTED,.cmd=_IOC(_IOC_READ|_IOC_WRITE,'F',nr,hsize),.arg=(uintptr_t)arg}; |
| 40 | memcpy(buf,&in,sizeof in);size_t n=frame(FUSE_IOCTL,buf,sizeof in);assert(n==64); |
| 41 | struct fuse_ioctl_out out;memcpy(&out,reply+16,sizeof out);assert(out.flags==FUSE_IOCTL_RETRY&&out.in_iovs==1&&out.out_iovs==1); |
| 42 | struct fuse_ioctl_iovec v;memcpy(&v,reply+32,sizeof v);assert(v.base==(uintptr_t)arg&&v.len==hsize); |
| 43 | in.in_size=in.out_size=hsize;memcpy(buf,&in,sizeof in);memcpy(buf+sizeof in,arg,hsize); |
| 44 | n=frame(FUSE_IOCTL,buf,sizeof in+hsize);memcpy(&out,reply+16,sizeof out); |
| 45 | if(psz) { |
| 46 | assert(n==96&&out.flags==FUSE_IOCTL_RETRY&&out.in_iovs==2&&out.out_iovs==2); |
| 47 | memcpy(&v,reply+48,sizeof v);assert(v.base==(uintptr_t)params&&v.len==psz); |
| 48 | in.in_size=in.out_size=hsize+psz;memcpy(buf,&in,sizeof in);memcpy(buf+sizeof in+hsize,params,psz); |
| 49 | n=frame(FUSE_IOCTL,buf,sizeof in+hsize+psz);memcpy(&out,reply+16,sizeof out); |
| 50 | } |
| 51 | assert(out.flags==0&&out.result==0&&n==32+hsize+psz); |
| 52 | memcpy(arg,reply+32,hsize);if(psz)memcpy(params,reply+32+hsize,psz); |
| 53 | } |
| 54 | static uint32_t root; |
| 55 | static uint32_t allocobj(uint32_t parent,uint32_t obj,uint32_t cls,void *p,size_t n) { |
| 56 | NVOS21_PARAMETERS a={.hRoot=root,.hObjectParent=parent,.hObjectNew=obj,.hClass=cls,.pAllocParms=p,.paramsSize=n}; |
| 57 | fake_ioctl(NV_ESC_RM_ALLOC,&a,sizeof a,p,n);assert(a.status==0);return a.hObjectNew; |
| 58 | } |
| 59 | static void freeobj(uint32_t parent,uint32_t obj) { |
| 60 | NVOS00_PARAMETERS a={.hRoot=root,.hObjectParent=parent,.hObjectOld=obj};fake_ioctl(NV_ESC_RM_FREE,&a,sizeof a,0,0);assert(!a.status); |
| 61 | } |
| 62 | static void control(uint32_t obj,uint32_t cmd,void *p,size_t n) { |
| 63 | NVOS54_PARAMETERS a={.hClient=root,.hObject=obj,.cmd=cmd,.params=p,.paramsSize=n};fake_ioctl(NV_ESC_RM_CONTROL,&a,sizeof a,p,n);assert(!a.status); |
| 64 | } |
| 65 | int main(int argc,char **argv) { |
| 66 | assert(argc==3);int s[2];assert(!socketpair(AF_UNIX,SOCK_SEQPACKET,0,s));pid_t pid=fork();assert(pid>=0); |
| 67 | if(!pid){close(s[0]);assert(dup2(s[1],0)==0&&dup2(s[1],1)==1);close(s[1]);execl(argv[1],argv[1],"--fake","--host-model",argv[2],NULL);_exit(127);} |
| 68 | close(s[1]);fd=s[0];struct cuse_init_in init={.major=7,.minor=31,.flags=CUSE_UNRESTRICTED_IOCTL};frame(CUSE_INIT,&init,sizeof init); |
| 69 | struct fuse_open_in open={0};frame(FUSE_OPEN,&open,sizeof open);struct fuse_open_out opened;memcpy(&opened,reply+16,sizeof opened);fh=opened.fh; |
| 70 | root=allocobj(0,0,NV01_ROOT_CLIENT,0,0); |
| 71 | NV0080_ALLOC_PARAMETERS dp={.hClientShare=root,.vaMode=NV_DEVICE_ALLOCATION_VAMODE_OPTIONAL_MULTIPLE_VASPACES}; |
| 72 | uint32_t dev=allocobj(root,0xcf000001,NV01_DEVICE_0,&dp,sizeof dp); |
| 73 | NV2080_ALLOC_PARAMETERS sp={0};uint32_t sub=allocobj(dev,0xcf000002,NV20_SUBDEVICE_0,&sp,sizeof sp); |
| 74 | NV_VASPACE_ALLOCATION_PARAMETERS vp={.vaBase=0x1000,.vaSize=0x1fffffb000000ull,.flags=NV_VASPACE_ALLOCATION_FLAGS_ENABLE_PAGE_FAULTING|NV_VASPACE_ALLOCATION_FLAGS_IS_EXTERNALLY_OWNED}; |
| 75 | uint32_t vas=allocobj(dev,0xcf000003,FERMI_VASPACE_A,&vp,sizeof vp); |
| 76 | NV_CHANNEL_GROUP_ALLOCATION_PARAMETERS gp={.engineType=1};uint32_t tsg=allocobj(dev,0xcf000004,KEPLER_CHANNEL_GROUP_A,&gp,sizeof gp); |
| 77 | NV_CTXSHARE_ALLOCATION_PARAMETERS cp={.hVASpace=vas,.flags=1};uint32_t ctx=allocobj(tsg,0xcf000005,FERMI_CONTEXT_SHARE_A,&cp,sizeof cp); |
| 78 | NV_CHANNEL_ALLOC_PARAMS ch={.gpFifoOffset=0x100000,.gpFifoEntries=1024,.hContextShare=ctx}; |
| 79 | uint32_t chan=allocobj(tsg,0xcf000006,BLACKWELL_CHANNEL_GPFIFO_A,&ch,sizeof ch); |
| 80 | uint32_t comp=allocobj(chan,0xcf000007,BLACKWELL_COMPUTE_B,0,0); |
| 81 | NVC36F_CTRL_CMD_GPFIFO_GET_WORK_SUBMIT_TOKEN_PARAMS tok={.workSubmitToken=0xffffffff};control(chan,NVC36F_CTRL_CMD_GPFIFO_GET_WORK_SUBMIT_TOKEN,&tok,sizeof tok);assert(tok.workSubmitToken==chan); |
| 82 | NVA06C_CTRL_GPFIFO_SCHEDULE_PARAMS sc={.bEnable=1};control(tsg,NVA06C_CTRL_CMD_GPFIFO_SCHEDULE,&sc,sizeof sc); |
| 83 | freeobj(chan,comp);freeobj(tsg,chan);freeobj(tsg,ctx);freeobj(dev,tsg);freeobj(dev,vas);freeobj(dev,sub);freeobj(root,dev);freeobj(root,root); |
| 84 | struct fuse_release_in rel={.fh=fh};frame(FUSE_RELEASE,&rel,sizeof rel);close(fd);int status;assert(waitpid(pid,&status,0)==pid&&WIFEXITED(status)&&WEXITSTATUS(status)==0); |
| 85 | puts("CUDApp RM alloc/control/free fake consumer PASS");return 0; |
| 86 | } |