xref: /aosp_15_r20/external/mesa3d/src/gfxstream/guest/android/include/gfxstream/guest/goldfish_sync.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright 2016 Google LLC
3  * SPDX-License-Identifier: MIT
4  */
5 
6 #ifndef ANDROID_INCLUDE_HARDWARE_GOLDFISH_SYNC_H
7 #define ANDROID_INCLUDE_HARDWARE_GOLDFISH_SYNC_H
8 
9 #define GOLDFISH_SYNC_VULKAN_SEMAPHORE_SYNC 0x00000001
10 #define GOLDFISH_SYNC_VULKAN_QSRI 0x00000002
11 
12 #include <errno.h>
13 #include <linux/ioctl.h>
14 #include <linux/types.h>
15 #include <sys/cdefs.h>
16 #include <sys/ioctl.h>
17 #include <sys/unistd.h>
18 #include <fcntl.h>
19 
20 // Make it conflict with ioctls that are not likely to be used
21 // in the emulator.
22 //
23 // '@'	00-0F	linux/radeonfb.h	conflict!
24 // '@'	00-0F	drivers/video/aty/aty128fb.c	conflict!
25 #define GOLDFISH_SYNC_IOC_MAGIC	'@'
26 
27 struct goldfish_sync_ioctl_info {
28     uint64_t host_glsync_handle_in;
29     uint64_t host_syncthread_handle_in;
30     int fence_fd_out;
31 };
32 
33 #define GOLDFISH_SYNC_IOC_QUEUE_WORK	_IOWR(GOLDFISH_SYNC_IOC_MAGIC, 0, struct goldfish_sync_ioctl_info)
34 #define GOLDFISH_SYNC_IOC_SIGNAL	_IOWR(GOLDFISH_SYNC_IOC_MAGIC, 1, struct goldfish_sync_ioctl_info)
35 
goldfish_sync_open()36 static __inline__ int goldfish_sync_open() {
37     return open("/dev/goldfish_sync", O_RDWR);
38 }
39 
goldfish_sync_close(int sync_fd)40 static __inline__ int goldfish_sync_close(int sync_fd) {
41     return close(sync_fd);
42 }
43 
44 static unsigned int sQueueWorkIoctlCmd = GOLDFISH_SYNC_IOC_QUEUE_WORK;
45 
46 // If we are running on a 64-bit kernel.
47 static unsigned int sQueueWorkIoctlCmd64Kernel = 0xc0184000;
48 
goldfish_sync_queue_work(int goldfish_sync_fd,uint64_t host_glsync,uint64_t host_thread,int * fd_out)49 static __inline__ int goldfish_sync_queue_work(int goldfish_sync_fd,
50                                                 uint64_t host_glsync,
51                                                 uint64_t host_thread,
52                                                 int* fd_out) {
53 
54     struct goldfish_sync_ioctl_info info;
55     int err;
56 
57     info.host_glsync_handle_in = host_glsync;
58     info.host_syncthread_handle_in = host_thread;
59     info.fence_fd_out = -1;
60 
61     err = ioctl(goldfish_sync_fd, sQueueWorkIoctlCmd, &info);
62 
63     if (err < 0 && errno == ENOTTY) {
64         sQueueWorkIoctlCmd = sQueueWorkIoctlCmd64Kernel;
65         err = ioctl(goldfish_sync_fd, sQueueWorkIoctlCmd, &info);
66         if (err < 0) {
67             sQueueWorkIoctlCmd = GOLDFISH_SYNC_IOC_QUEUE_WORK;
68         }
69     }
70 
71     if (fd_out) *fd_out = info.fence_fd_out;
72 
73     return err;
74 }
75 
goldfish_sync_signal(int goldfish_sync_fd)76 static __inline__ int goldfish_sync_signal(int goldfish_sync_fd) {
77     return ioctl(goldfish_sync_fd, GOLDFISH_SYNC_IOC_SIGNAL, 0);
78 }
79 
80 #endif
81