Now About Social Code
summaryrefslogtreecommitdiff
path: root/src/drv.c
blob: e5737e88c71dc53537b8cdca480b08ba75955fff (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#include <stdio.h>
#include <string.h>
#include <assert.h>

#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <sys/ioctl.h>
#include <errno.h>

#include <xf86drm.h>

#define DRM_PVR_RESERVED1 (DRM_COMMAND_BASE + 0)
#define DRM_PVR_RESERVED2 (DRM_COMMAND_BASE + 1)
#define DRM_PVR_RESERVED3 (DRM_COMMAND_BASE + 2)
#define DRM_PVR_RESERVED4 (DRM_COMMAND_BASE + 3)
#define DRM_PVR_RESERVED5 (DRM_COMMAND_BASE + 4)
#define DRM_PVR_RESERVED6 (DRM_COMMAND_BASE + 5)

/* PVR includes */
#define SUPPORT_MEMINFO_IDS
#define SUPPORT_DRI_DRM_EXT
#include <config_kernel.h>
#include <sgxfeaturedefs.h>
#include <pvr_bridge.h>
#include <sgx_bridge.h>
#include <pvr_drm_shared.h>

#define DRM_IOCTL_PVR_SRVKM DRM_IOWR(PVR_DRM_SRVKM_CMD, PVRSRV_BRIDGE_PACKAGE)

#include <pprint.c>

#define UNUSED(x) (void)x

struct mem_alloc {
    PVRSRV_BRIDGE_OUT_ALLOCDEVICEMEM dev_mem;
    void *data;
};

struct driver_state {
    int fd;
    IMG_HANDLE kernel_services;
    IMG_UINT32 device_idx;
    IMG_HANDLE dev_cookie;
    IMG_HANDLE dev_mem_context;
    IMG_HANDLE global_eventobject;

    PVRSRV_HEAP_INFO kernel_heap;
    PVRSRV_HEAP_INFO perctx_3d_heap;
    PVRSRV_HEAP_INFO ta_data_heap;
    struct mem_alloc ccb_buffer;
    struct mem_alloc kernel_buffer_2;
    struct mem_alloc kernel_buffer_3;
    struct mem_alloc perctx_3d_buffer;
    struct mem_alloc ta_data_buffer;

    IMG_HANDLE hw_render_ctx;
    IMG_DEV_VIRTADDR hw_render_ctx_addr;
};

static void check_pvr_error(PVRSRV_ERROR error) {
    if(error != PVRSRV_OK) {
        printf("Got error %s\n", PPRINT(NULL, error, PVRSRV_ERROR));
        assert(false);
    }
}

static void check_ioctl(int fd, long request, void *data) {
    int ret = drmIoctl(fd, request, data);
    if(ret != 0) {
        printf("IOCTL ret value %d, %d, %s\n", ret, errno, strerror(errno));
        assert(ret == 0);
    }
}

static void connect_service(struct driver_state *state) {
    PVRSRV_BRIDGE_IN_CONNECT_SERVICES in = {
        .ui32BridgeFlags = 0xDEADBEEF
    };
    PVRSRV_BRIDGE_OUT_CONNECT_SERVICES out = {.eError = 0};
    PVRSRV_BRIDGE_PACKAGE data = {
        .ui32BridgeID = PVRSRV_BRIDGE_CONNECT_SERVICES,
        .ui32Size = sizeof(data),
        .pvParamIn = &in,
        .ui32InBufferSize = sizeof(in),
        .pvParamOut = &out,
        .ui32OutBufferSize = sizeof(out),
        .hKernelServices = 0,
    };

    check_ioctl(state->fd, DRM_IOCTL_PVR_SRVKM, &data);
    //PPRINT(stdout, &out, PVRSRV_BRIDGE_OUT_CONNECT_SERVICES);
    check_pvr_error(out.eError);
    state->kernel_services = out.hKernelServices;
}

static void enum_devices(struct driver_state *state) {
    PVRSRV_BRIDGE_OUT_ENUMDEVICE out;
    PVRSRV_BRIDGE_PACKAGE data = {
        .ui32BridgeID = PVRSRV_BRIDGE_ENUM_DEVICES,
        .ui32Size = sizeof(data),
        .pvParamIn = NULL,
        .ui32InBufferSize = 0,
        .pvParamOut = &out,
        .ui32OutBufferSize = sizeof(out),
        .hKernelServices = state->kernel_services
    };

    check_ioctl(state->fd, DRM_IOCTL_PVR_SRVKM, &data);
    check_pvr_error(out.eError);

    bool gpu_found = false;
    for(uint32_t i = 0; i < out.ui32NumDevices; i++) {
        PVRSRV_DEVICE_IDENTIFIER *di = &out.asDeviceIdentifier[i];
        if(di->eDeviceType == PVRSRV_DEVICE_TYPE_SGX && di->eDeviceClass == PVRSRV_DEVICE_CLASS_3D) {
            state->device_idx = di->ui32DeviceIndex;
            gpu_found = true;
            break;
        }
    }

    assert(gpu_found);
}

static void acquire_devinfo(struct driver_state *state) {
    PVRSRV_BRIDGE_IN_ACQUIRE_DEVICEINFO in = {
        .uiDevIndex = state->device_idx,
        .eDeviceType = PVRSRV_DEVICE_TYPE_UNKNOWN,
    };
    PVRSRV_BRIDGE_OUT_ACQUIRE_DEVICEINFO out;
    PVRSRV_BRIDGE_PACKAGE data = {
        .ui32BridgeID = PVRSRV_BRIDGE_ACQUIRE_DEVICEINFO,
        .ui32Size = sizeof(data),
        .pvParamIn = &in,
        .ui32InBufferSize = sizeof(in),
        .pvParamOut = &out,
        .ui32OutBufferSize = sizeof(out),
        .hKernelServices = state->kernel_services
    };

    check_ioctl(state->fd, DRM_IOCTL_PVR_SRVKM, &data);
    check_pvr_error(out.eError);

    state->dev_cookie = out.hDevCookie;
}

static void sgx_get_misc_info(struct driver_state *state) {
    SGX_MISC_INFO misc_info = {
        .eRequest = SGX_MISC_INFO_REQUEST_DRIVER_SGXREV,
    };
    PVRSRV_BRIDGE_IN_SGXGETMISCINFO in = {
        .hDevCookie = state->dev_cookie,
        .psMiscInfo = &misc_info,
    };
    PVRSRV_BRIDGE_RETURN out;
    PVRSRV_BRIDGE_PACKAGE data = {
        .ui32BridgeID = PVRSRV_BRIDGE_SGX_GETMISCINFO,
        .ui32Size = sizeof(data),
        .pvParamIn = &in,
        .ui32InBufferSize = sizeof(in),
        .pvParamOut = &out,
        .ui32OutBufferSize = sizeof(out),
        .hKernelServices = state->kernel_services,
    };

    check_ioctl(state->fd, DRM_IOCTL_PVR_SRVKM, &data);
    check_pvr_error(out.eError);

    /* TODO figure out what we do with misc info */
    PPRINT(stdout, &in, PVRSRV_BRIDGE_IN_SGXGETMISCINFO);
    fprintf(stdout, "\n");
    PPRINT(stdout, &in.psMiscInfo->uData.sSGXFeatures, PVRSRV_SGX_MISCINFO_FEATURES);
    fprintf(stdout, "\n");
}

static void create_devmemcontext(struct driver_state *state) {
    PVRSRV_BRIDGE_IN_CREATE_DEVMEMCONTEXT in = {
        .hDevCookie = state->dev_cookie
    };
    PVRSRV_BRIDGE_OUT_CREATE_DEVMEMCONTEXT out;
    PVRSRV_BRIDGE_PACKAGE data = {
        .ui32BridgeID = PVRSRV_BRIDGE_CREATE_DEVMEMCONTEXT,
        .ui32Size = sizeof(data),
        .pvParamIn = &in,
        .ui32InBufferSize = sizeof(in),
        .pvParamOut = &out,
        .ui32OutBufferSize = sizeof(out),
        .hKernelServices = state->kernel_services
    };

    check_ioctl(state->fd, DRM_IOCTL_PVR_SRVKM, &data);
    check_pvr_error(out.eError);
    PPRINT(stdout, &out, PVRSRV_BRIDGE_OUT_CREATE_DEVMEMCONTEXT);

    state->dev_mem_context = out.hDevMemContext;
}

static void get_clientinfo(struct driver_state *state) {
    PVRSRV_BRIDGE_IN_GETCLIENTINFO in = {
        .hDevCookie = state->dev_cookie,
    };
    PVRSRV_BRIDGE_OUT_GETCLIENTINFO out;
    PVRSRV_BRIDGE_PACKAGE data = {
        .ui32BridgeID = PVRSRV_BRIDGE_SGX_GETCLIENTINFO,
        .ui32Size = sizeof(data),
        .pvParamIn = &in,
        .ui32InBufferSize = sizeof(in),
        .pvParamOut = &out,
        .ui32OutBufferSize = sizeof(out),
        .hKernelServices = state->kernel_services
    };

    check_ioctl(state->fd, DRM_IOCTL_PVR_SRVKM, &data);
    check_pvr_error(out.eError);
    //PPRINT(stdout, &out, PVRSRV_BRIDGE_OUT_GETCLIENTINFO);
}

static void get_miscinfo(struct driver_state *state) {
    PVRSRV_BRIDGE_IN_GET_MISC_INFO in = {
        .sMiscInfo = {
            .ui32StateRequest = PVRSRV_MISC_INFO_GLOBALEVENTOBJECT_PRESENT | PVRSRV_MISC_INFO_TIMER_PRESENT,
        },
    };
    PVRSRV_BRIDGE_OUT_GET_MISC_INFO out;
    PVRSRV_BRIDGE_PACKAGE data = {
        .ui32BridgeID = PVRSRV_BRIDGE_GET_MISC_INFO,
        .ui32Size = sizeof(data),
        .pvParamIn = &in,
        .ui32InBufferSize = sizeof(in),
        .pvParamOut = &out,
        .ui32OutBufferSize = sizeof(out),
        .hKernelServices = state->kernel_services
    };

    check_ioctl(state->fd, DRM_IOCTL_PVR_SRVKM, &data);
    check_pvr_error(out.eError);

    state->global_eventobject = out.sMiscInfo.sGlobalEventObject.hOSEventKM;
}

static void open_eventobject(struct driver_state *state) {
    PVRSRV_BRIDGE_IN_EVENT_OBJECT_OPEN in = {
        .sEventObject = {
            .szName = "PVRSRV_GLOBAL_EVENTOBJECT",
            .hOSEventKM = state->global_eventobject,
        }
    };

    PVRSRV_BRIDGE_OUT_EVENT_OBJECT_OPEN out;
    PVRSRV_BRIDGE_PACKAGE data = {
        .ui32BridgeID = PVRSRV_BRIDGE_EVENT_OBJECT_OPEN,
        .ui32Size = sizeof(data),
        .pvParamIn = &in,
        .ui32InBufferSize = sizeof(in),
        .pvParamOut = &out,
        .ui32OutBufferSize = sizeof(out),
        .hKernelServices = state->kernel_services
    };
    check_ioctl(state->fd, DRM_IOCTL_PVR_SRVKM, &data);
    check_pvr_error(out.eError);
}

static void get_dev_mem_heapinfo(struct driver_state *state) {
    PVRSRV_BRIDGE_IN_GET_DEVMEM_HEAPINFO in = {
        .hDevCookie = state->dev_cookie,
        .hDevMemContext = state->dev_mem_context,
    };
    PVRSRV_BRIDGE_OUT_GET_DEVMEM_HEAPINFO out;
    PVRSRV_BRIDGE_PACKAGE data = {
        .ui32BridgeID = PVRSRV_BRIDGE_GET_DEVMEM_HEAPINFO,
        .ui32Size = sizeof(data),
        .pvParamIn = &in,
        .ui32InBufferSize = sizeof(in),
        .pvParamOut = &out,
        .ui32OutBufferSize = sizeof(out),
        .hKernelServices = state->kernel_services
    };

    check_ioctl(state->fd, DRM_IOCTL_PVR_SRVKM, &data);
    check_pvr_error(out.eError);

    for (int i = 0; i < out.ui32ClientHeapCount; i++) {
        //PPRINT(stdout, &out.sHeapInfo[i], PVRSRV_HEAP_INFO);
        if (out.sHeapInfo[i].ui32HeapID == HEAP_ID( PVRSRV_DEVICE_TYPE_SGX, SGX_KERNEL_DATA_HEAP_ID)) {
            state->kernel_heap = out.sHeapInfo[i];
        }

        if (out.sHeapInfo[i].ui32HeapID == HEAP_ID( PVRSRV_DEVICE_TYPE_SGX, SGX_TADATA_HEAP_ID)) {
            state->ta_data_heap = out.sHeapInfo[i];
        }

        if (out.sHeapInfo[i].ui32HeapID == HEAP_ID( PVRSRV_DEVICE_TYPE_SGX, SGX_PERCONTEXT_3DPARAMETERS_HEAP_ID)) {
            state->perctx_3d_heap = out.sHeapInfo[i];
        }
    }

    assert(state->kernel_heap.ui32HeapID != 0);
    assert(state->ta_data_heap.ui32HeapID != 0);
    assert(state->perctx_3d_heap.ui32HeapID != 0);
}

static struct mem_alloc allocate_memobj(struct driver_state *state, PVRSRV_BRIDGE_IN_ALLOCDEVICEMEM *alloc) {
    struct mem_alloc mem;
    PVRSRV_BRIDGE_OUT_ALLOCDEVICEMEM out;
    PVRSRV_BRIDGE_PACKAGE data = {
        .ui32BridgeID = PVRSRV_BRIDGE_ALLOC_DEVICEMEM,
        .ui32Size = sizeof(data),
        .pvParamIn = alloc,
        .ui32InBufferSize = sizeof(*alloc),
        .pvParamOut = &out,
        .ui32OutBufferSize = sizeof(out),
        .hKernelServices = state->kernel_services
    };

    printf("Allocating from %p %p 0x%x\n", alloc->hDevCookie, alloc->hDevMemHeap, alloc->uSize);
    check_ioctl(state->fd, DRM_IOCTL_PVR_SRVKM, &data);
    check_pvr_error(out.eError);

    {
        PVRSRV_BRIDGE_IN_MHANDLE_TO_MMAP_DATA in = {
            .hMHandle = out.sClientMemInfo.hKernelMemInfo,
        };
        PVRSRV_BRIDGE_OUT_MHANDLE_TO_MMAP_DATA out2;
        PVRSRV_BRIDGE_PACKAGE data = {
            .ui32BridgeID = PVRSRV_BRIDGE_MHANDLE_TO_MMAP_DATA,
            .ui32Size = sizeof(data),
            .pvParamIn = &in,
            .ui32InBufferSize = sizeof(in),
            .pvParamOut = &out2,
            .ui32OutBufferSize = sizeof(out2),
            .hKernelServices = state->kernel_services
        };

        check_ioctl(state->fd, DRM_IOCTL_PVR_SRVKM, &data);
        check_pvr_error(out2.eError);

        PPRINT(stdout, &out2, PVRSRV_BRIDGE_OUT_MHANDLE_TO_MMAP_DATA);

        printf("Attempting map 0x%x, 0x%x\n", (uint32_t)out2.uiRealByteSize, (uint32_t)out2.uiMMapOffset);
        mem.data = (void*)(uintptr_t)syscall(SYS_mmap2, NULL, out2.uiRealByteSize,
                                             PROT_READ | PROT_WRITE, MAP_SHARED,
                                             state->fd, out2.uiMMapOffset);

        mem.data += out2.uiByteOffset;
        assert(mem.data != MAP_FAILED);
        memset(mem.data, 0x00, out.sClientMemInfo.uAllocSize);
    }

    mem.dev_mem = out;
    return mem;
}

static void allocate_memory(struct driver_state *state) {
     PVRSRV_BRIDGE_IN_ALLOCDEVICEMEM alloc_CCB = {
         .hDevCookie = state->dev_cookie,
         .hDevMemHeap = state->kernel_heap.hDevMemHeap,
         .ui32Attribs = PVRSRV_MEM_READ | PVRSRV_MEM_WRITE | PVRSRV_MEM_CACHE_CONSISTENT | PVRSRV_MEM_NO_SYNCOBJ | PVRSRV_MEM_EDM_PROTECT,
         .uSize = 0x86a4,
         .uAlignment = 0x1000,
     };
     state->ccb_buffer = allocate_memobj(state, &alloc_CCB);

     PVRSRV_BRIDGE_IN_ALLOCDEVICEMEM alloc_kernel_buffer_2 = {
         .hDevCookie = state->dev_cookie,
         .hDevMemHeap = state->kernel_heap.hDevMemHeap,
         .ui32Attribs = PVRSRV_MEM_READ | PVRSRV_MEM_WRITE | PVRSRV_MEM_CACHE_CONSISTENT | PVRSRV_MEM_NO_SYNCOBJ | PVRSRV_MEM_EDM_PROTECT,
         .uSize = 0x8,
         .uAlignment = 0x20,
     };
     state->kernel_buffer_2 = allocate_memobj(state, &alloc_kernel_buffer_2);

     PVRSRV_BRIDGE_IN_ALLOCDEVICEMEM alloc_kernel_buffer_3 = {
         .hDevCookie = state->dev_cookie,
         .hDevMemHeap = state->kernel_heap.hDevMemHeap,
         .ui32Attribs = PVRSRV_MEM_READ | PVRSRV_MEM_WRITE | PVRSRV_MEM_CACHE_CONSISTENT | PVRSRV_MEM_NO_SYNCOBJ,
         .uSize = 0x44,
         .uAlignment = 0x20,
     };
     state->kernel_buffer_3 = allocate_memobj(state, &alloc_kernel_buffer_3);

     PVRSRV_BRIDGE_IN_ALLOCDEVICEMEM alloc_perctx_3d_buffer = {
         .hDevCookie = state->dev_cookie,
         .hDevMemHeap = state->perctx_3d_heap.hDevMemHeap,
         .ui32Attribs = PVRSRV_MEM_READ | PVRSRV_MEM_WRITE | PVRSRV_MEM_NO_SYNCOBJ,
         .uSize = 0x40,
         .uAlignment = 0x40,
     };
     state->perctx_3d_buffer = allocate_memobj(state, &alloc_perctx_3d_buffer);

     PVRSRV_BRIDGE_IN_ALLOCDEVICEMEM alloc_ta_data_buffer = {
         .hDevCookie = state->dev_cookie,
         .hDevMemHeap = state->ta_data_heap.hDevMemHeap,
         .ui32Attribs = PVRSRV_MEM_READ | PVRSRV_MEM_WRITE | PVRSRV_MEM_NO_SYNCOBJ,
         .uSize = 0xc0,
         .uAlignment = 0x40,
     };
     state->ta_data_buffer = allocate_memobj(state, &alloc_ta_data_buffer);
}

static void register_hw_render_ctx(struct driver_state *state) {
    /* Data copied directly from dump on hw
     * Not sure what other params mean, but the buffer
     * ones are obvious once you start offsetting buffer addresses
     */
    uint32_t render_ctx[] = {
        0x21,
        0x1,
        0x0,
        state->ccb_buffer.dev_mem.sClientMemInfo.sDevVAddr.uiAddr, // Needs to be patched
        state->kernel_buffer_2.dev_mem.sClientMemInfo.sDevVAddr.uiAddr, // All zeros
        0x0,
        state->kernel_buffer_3.dev_mem.sClientMemInfo.sDevVAddr.uiAddr, // All zeros
        state->perctx_3d_buffer.dev_mem.sClientMemInfo.sDevVAddr.uiAddr, // All zeros
        state->ta_data_buffer.dev_mem.sClientMemInfo.sDevVAddr.uiAddr, // All zeros
        0x0,
        0x0,
        0x0,
        0x0,
        0x0,
        0x0,
        0x0,
        0x0,
        0x0, // This value seems to change when dumping, but overriding to zero doesn't break anything
    };
    PVRSRV_BRIDGE_IN_SGX_REGISTER_HW_RENDER_CONTEXT in = {
        .hDevCookie = state->dev_cookie,
        .pHWRenderContextCpuVAddr = render_ctx,
        .ui32HWRenderContextSize = sizeof(render_ctx),
        .hDevMemContext = state->dev_mem_context,
    };
    PVRSRV_BRIDGE_OUT_SGX_REGISTER_HW_RENDER_CONTEXT out;
    PVRSRV_BRIDGE_PACKAGE data = {
        .ui32BridgeID = PVRSRV_BRIDGE_SGX_REGISTER_HW_RENDER_CONTEXT,
        .ui32Size = sizeof(data),
        .pvParamIn = &in,
        .ui32InBufferSize = sizeof(in),
        .pvParamOut = &out,
        .ui32OutBufferSize = sizeof(out),
        .hKernelServices = state->kernel_services
    };

    check_ioctl(state->fd, DRM_IOCTL_PVR_SRVKM, &data);
    check_pvr_error(out.eError);

    state->hw_render_ctx = out.hHWRenderContext;
    state->hw_render_ctx_addr = out.sHWRenderContextDevVAddr;
}

int main(int argc, char *argv[]) {
    UNUSED(argc);
    UNUSED(argv);
    printf("Hello world!\n");
    struct driver_state state;

    state.fd = open("/dev/dri/renderD128", O_RDWR);
    assert(state.fd != -1 && "Failed to device file");

    drmVersionPtr version = drmGetVersion(state.fd);
    assert(version && "drmGetVersion failed");

    printf("Driver: %s\n", version->name);
    printf("Version %d.%d.%d\n", version->version_major, version->version_minor, version->version_patchlevel);
    printf("Date: %s\n", version->date);
    printf("Desc: %s\n", version->desc);
    printf("Uniq: %s\n", drmGetBusid(state.fd));

    //printf("DRM_IOCTL_VERSION = 0x%x\n", DRM_IOCTL_VERSION);
    //printf("PVR_DRM_SRVKM_CMD = 0x%x\n", DRM_IOCTL_PVR_SRVKM);
    connect_service(&state);
    enum_devices(&state);
    acquire_devinfo(&state);
    sgx_get_misc_info(&state);
    create_devmemcontext(&state);
    get_clientinfo(&state);
    get_miscinfo(&state);
    open_eventobject(&state);
    get_dev_mem_heapinfo(&state);
    allocate_memory(&state);
    register_hw_render_ctx(&state);

    close(state.fd);

    return 0;
}