/* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */
/*
 * Copyright (c) 2020 MediaTek Inc.
 */

#ifndef _MTK_ESPOOL_H_
#define _MTK_ESPOOL_H_

#include <linux/ioctl.h>

typedef struct ES_BufferControl_s {
	uint64_t PoolID;          // [INPUT], PoolID = hVideo << 32 | dmabuf_id
	uint32_t u32UnicID;       // [OUTPUT]
	uint32_t u32TotalSize;    // [OUTPUT]
	uint32_t u32Size;         // [OUTPUT] & [INPUT]
	uint32_t u32Offset;       // [OUTPUT]
	uint32_t u32FreeSize;     // [OUTPUT]
	int32_t fd;               // [OUTPUT]
	uint32_t u32Flags;        // [OUTPUT] & [INPUT]
} ES_Buffer_Info_t;

#define ESPOOL_DEVICE_NAME "mtk-es-buf-mgr"
#define ESPOOL_DEVICE_PATH_LINUX "/dev/mik/espool"
#define ESPOOL_DEVICE_PATH_ANDROID "/dev/mik!espool"

// Please use _IO(), _IOW(), _IOR(), _IOWR() to define ioctl command.
// Some ioctl command have been used in vfs_ioctl. Please see ioctl-number.txt.
#define ESPOOL_DEVICE_MAGIC		0x40  // should aligned with mi_dev.h

// Deprecated definition
#define VIDIOC_ESBUFC_OPEN             _IOW(ESPOOL_DEVICE_MAGIC, 0, ES_Buffer_Info_t)
#define VIDIOC_ESBUFC_QUERY_POOL_INFO  _IOWR(ESPOOL_DEVICE_MAGIC, 1, ES_Buffer_Info_t)
#define VIDIOC_ESBUFC_ALLOCATE         _IOWR(ESPOOL_DEVICE_MAGIC, 2, ES_Buffer_Info_t)
#define VIDIOC_ESBUFC_FREE_SIZE        _IO(ESPOOL_DEVICE_MAGIC, 3)
#define VIDIOC_ESBUFC_EXIT             _IO(ESPOOL_DEVICE_MAGIC, 4)
#define VIDIOC_ESBUFC_VACANCY          _IOWR(ESPOOL_DEVICE_MAGIC, 5, ES_Buffer_Info_t)
#define VIDIOC_ESBUFC_SET_SEQ_WRITE    _IO(ESPOOL_DEVICE_MAGIC, 6)

// Flags for ESPool_CreateParam_s.u32Flags(Low 16 bit)
/*
 *  When allocate ES buffer with IOMMUMD_FLAG_2XIOVA,
 *  user should set ESPOOL_FLAG_SEQWRITE_MODE when calling IOC_ESPOOL_CREATE.
 */
#define ESPOOL_FLAG_SEQWRITE_MODE    (1 << 0)

/*
 *  ES Buffer Manager will maintain an ES queue,
 *  User should call push and pop API itself by default.
 *
 *  ES Buffer Manager has an ability to push ES info into ES queue
 *  when user calling IOC_ESPOOL_GET_BUF. To enable this feature
 *  user should set ESPOOL_FLAG_AUTO_PUSH_ESQ  when call IOC_ESPOOL_CREATE.
 */
#define ESPOOL_FLAG_AUTO_PUSH_ESQ    (1 << 1)

typedef struct ESPool_CreateParam_s {
	uint64_t u64PoolId;
	uint32_t u32Size;
	uint32_t u32UnicId;
	uint32_t u32Flags;
} ESPool_CreateParam_t;

typedef struct ESPool_QueryPoolInfoParam_s {
	uint64_t u64PoolId;
	uint32_t u32TotalSize;
	uint32_t u32UnicId;
} ESPool_QueryPoolInfoParam_t;

typedef struct ESPool_GetBufParam_s {
	uint64_t u64PoolId;
	uint32_t u32Offset;
	uint32_t u32Size;
} ESPool_GetBufParam_t;

typedef struct ESPool_FreeBufParam_s {
	uint64_t u64PoolId;
	uint32_t u32Size;
} ESPool_FreeBufParam_t;

typedef struct ESPool_DestroyParam_s {
	uint64_t u64PoolId;
} ESPool_DestroyParam_t;

typedef struct ESPool_GetFreeSizeParam_s {
	uint64_t u64PoolId;
	uint32_t u32FreeSize;
} ESPool_GetFreeSizeParam_t;

typedef struct ESPool_SetSeqModeParam_s {
	uint64_t u64PoolId;
	uint32_t u32SeqMode;
} ESPool_SetSeqModeParam_t;

typedef struct ESPool_ESInfo_s {
	uint64_t u64PoolId;
	uint32_t u32Offset;
	uint32_t u32Size;
	uint64_t u64Reserved;
} ESPool_ESInfo_t;

#define IOC_ESPOOL_CREATE           _IOWR(ESPOOL_DEVICE_MAGIC, 10, ESPool_CreateParam_t)
#define IOC_ESPOOL_QUERY_POOL_INFO  _IOWR(ESPOOL_DEVICE_MAGIC, 11, ESPool_QueryPoolInfoParam_t)
#define IOC_ESPOOL_GET_BUF          _IOWR(ESPOOL_DEVICE_MAGIC, 12, ESPool_GetBufParam_t)
#define IOC_ESPOOL_FREE_BUF         _IOWR(ESPOOL_DEVICE_MAGIC, 13, ESPool_FreeBufParam_t)
#define IOC_ESPOOL_DESTROY          _IOWR(ESPOOL_DEVICE_MAGIC, 14, ESPool_DestroyParam_t)
#define IOC_ESPOOL_GET_FREE_SIZE    _IOWR(ESPOOL_DEVICE_MAGIC, 15, ESPool_GetFreeSizeParam_t)
#define IOC_ESPOOL_SET_SEQ_WRITE_MODE    _IOWR(ESPOOL_DEVICE_MAGIC, 16, ESPool_SetSeqModeParam_t)
#define IOC_ESPOOL_PUSH_ESQ         _IOWR(ESPOOL_DEVICE_MAGIC, 17, ESPool_ESInfo_t)
#define IOC_ESPOOL_POP_ESQ          _IOWR(ESPOOL_DEVICE_MAGIC, 18, ESPool_ESInfo_t)
#define IOC_ESPOOL_RESET            _IOWR(ESPOOL_DEVICE_MAGIC, 19, uint64_t)
#endif

