1 /*
2 * Copyright 2024 Advanced Micro Devices, Inc.
3 * SPDX-License-Identifier: MIT
4 */
5
6 /* These are identical to libdrm functions drmCommandWrite* and drmIoctl,
7 * but unlike libdrm, these are inlinable.
8 */
9
10 #ifndef OS_DRM_H
11 #define OS_DRM_H
12
13 #ifdef _WIN32
14 #error "Windows shouldn't include this."
15 #endif
16
17 #include <sys/ioctl.h>
18 #include <errno.h>
19 #include <xf86drm.h>
20
21 static inline int
drm_ioctl(int fd,uint32_t request,void * arg)22 drm_ioctl(int fd, uint32_t request, void *arg)
23 {
24 int ret;
25
26 do {
27 ret = ioctl(fd, request, arg);
28 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
29
30 return ret ? -errno : 0;
31 }
32
33 static inline int
drm_ioctl_write(int fd,unsigned drm_command_index,void * data,unsigned size)34 drm_ioctl_write(int fd, unsigned drm_command_index, void *data, unsigned size)
35 {
36 uint32_t request = DRM_IOC(DRM_IOC_WRITE, DRM_IOCTL_BASE,
37 DRM_COMMAND_BASE + drm_command_index, size);
38 return drm_ioctl(fd, request, data);
39 }
40
41 static inline int
drm_ioctl_write_read(int fd,unsigned drm_command_index,void * data,unsigned size)42 drm_ioctl_write_read(int fd, unsigned drm_command_index, void *data,
43 unsigned size)
44 {
45 uint32_t request = DRM_IOC(DRM_IOC_READ | DRM_IOC_WRITE, DRM_IOCTL_BASE,
46 DRM_COMMAND_BASE + drm_command_index, size);
47 return drm_ioctl(fd, request, data);
48 }
49
50 #endif
51