# Copyright 2022 syzkaller project authors. All rights reserved.
# Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

include <uapi/linux/sync_file.h>
include <uapi/asm/ioctl.h>
include <uapi/linux/fcntl.h>

resource fd_sync_file[fd]
resource fd_sw_sync[fd]

openat$sw_sync_info(fd const[AT_FDCWD], file ptr[in, string["/sys/kernel/debug/sync/info"]], flags flags[open_flags], mode const[0]) fd
openat$sw_sync(fd const[AT_FDCWD], file ptr[in, string["/sys/kernel/debug/sync/sw_sync"]], flags flags[open_flags], mode const[0]) fd_sw_sync

ioctl$SW_SYNC_IOC_CREATE_FENCE(fd fd_sw_sync, cmd const[SW_SYNC_IOC_CREATE_FENCE], arg ptr[inout, sw_sync_create_fence_data])
ioctl$SW_SYNC_IOC_INC(fd fd_sw_sync, cmd const[SW_SYNC_IOC_INC], arg ptr[in, int32])

ioctl$SYNC_IOC_MERGE(fd fd_sync_file, cmd const[SYNC_IOC_MERGE], arg ptr[inout, sync_merge_data])
ioctl$SYNC_IOC_FILE_INFO(fd fd_sync_file, cmd const[SYNC_IOC_FILE_INFO], arg ptr[inout, sync_file_info])

define SW_SYNC_IOC_CREATE_FENCE	_IOWR('W', 0, struct{__u32 value; char name[32]; __s32 fence;})
define SW_SYNC_IOC_INC	_IOW('W', 1, __u32)

sw_sync_create_fence_data {
	value	int32	(in)
	name	array[int8, 32]	(in)
	fence	fd_sync_file	(out)
}

sync_merge_data {
	name	array[int8, 32]	(in)
	fd2	fd_sync_file	(in)
	fence	fd_sync_file	(out)
	flags	const[0, int32]
	pad	const[0, int32]
}

sync_file_info {
	name		array[int8, 32]	(out)
	status		int32	(out)
	flags		int32	(out)
	num_fences	len[sync_fence_info, int32]	(in)
	pad		const[0, int32]
	sync_fence_info	ptr64[out, array[sync_fence_info]]
}

sync_fence_info {
	name		array[int8, 32]
	driver_name	array[int8, 32]
	status		int32
	flags		int32
	timestamp_ns	int64
}
