/***************************************************************************
 *
 *   SPDX-License-Identifier: BSD-3-Clause
 *   Copyright(c) 2007-2026 Intel Corporation
 * 
 *   These contents may have been developed with support from one or more
 *   Intel-operated generative artificial intelligence solutions.
 *
 ***************************************************************************/
/**
*****************************************************************************
* @file qae_mem.h
*
* This file provides linux kernel memory allocation for quick assist API
*
*****************************************************************************/
#ifndef QAE_MEM_H_
#define QAE_MEM_H_
#include "cpa.h"

/*define types which need to vary between 32 and 64 bit*/
#ifdef __x86_64__
#define QAE_UINT Cpa64U
#define QAE_INT Cpa64S
#else
#define QAE_UINT Cpa32U
#define QAE_INT Cpa32S
#endif

#define QAE_PHYS_ADDR CpaPhysicalAddr

/**
 *****************************************************************************
 * @ingroup perfCodeFramework
 *      Framework aligned memory structure.
 * @description
 *      This structure is used to assist the framework in allocating aligned
 *      memory
 ****************************************************************************/
typedef struct qae_mem_alloc_info_s
{
    void *mAllocMemPtr; /* memory addr returned by the kernel */
    Cpa32U mSize;       /* allocated size */

} qae_mem_alloc_info_t;

/**
 *****************************************************************************
 * @ingroup qaeMemUtils
 *      user space memory info.
 * @description
 *      This structure is used to store info on aligned user space memory
 *      memory
 ****************************************************************************/
#pragma pack(push)
#pragma pack(1)
typedef struct qae_dev_mem_info_s
{
    union {
        struct qae_dev_mem_info_s *pPrev;
        uint64_t padding_pPrev;
    };
    union {
        struct qae_dev_mem_info_s *pNext;
        uint64_t padding_pNext;
    };
    uint32_t id;
    /* Id of this block */
    uint32_t nodeId;
    /* Node id for NUMA */
    uint32_t size;
    /* Size of this block (bytes) */
    uint32_t available_size;
    /* Available size remained on the page */
    uint16_t allocations;
    /* Counter keeping track of number of allocations */
    union {
        void *kmalloc_ptr;
        uint64_t padding_kmalloc_ptr;
    };
    /* Pointer to mem originally returned by kmalloc */
    union {
        int32_t *kmalloc_area;
        uint64_t padding_kamalloc_area;
    };
    /* Pointer to kmalloc'd area rounded up to a page boundary */
    uint64_t phy_addr;
    /* Physical address of the kmalloced area */
    union {
        void *virt_addr;
        uint64_t padding_virt_addr;
    };
    /* Base address in user space - i.e. virtual address */
} qae_dev_mem_info_t;
#pragma pack(pop)

/**
 *****************************************************************************
 * @ingroup qaeMemUtils
 *      array structure
 * @description
 *      This structure is used to copy chunks of data read from files
 *      from user to kernel space
 ****************************************************************************/
typedef struct dev_mem_file_s
{
    unsigned char data[2048];
    unsigned int size;
} dev_mem_file_t;

/**
 *****************************************************************************
 * @ingroup qaeMemUtils
 *      user space memory list pointer structure.
 * @description
 *      This structure is used to assist in allocating aligned
 *      memory
 ****************************************************************************/
typedef struct user_proc_mem_list_s
{
    unsigned int pid;
    unsigned int allocs_nr;
    unsigned int max_id;
    qae_dev_mem_info_t *freed_head;
    qae_dev_mem_info_t *freed_tail;
    qae_dev_mem_info_t *head;
    qae_dev_mem_info_t *tail;
    struct user_proc_mem_list_s *pPrev;
    struct user_proc_mem_list_s *pNext;
} user_proc_mem_list_t;

/**
 *****************************************************************************
 * @ingroup qaeMemUtils
 *      user space memory list pointer structure.
 * @description
 *      This structure is used to assist in allocating aligned
 *      memory
 ****************************************************************************/
typedef struct user_mem_dev_s
{
    user_proc_mem_list_t *head;
    user_proc_mem_list_t *tail;
} user_mem_dev_t;

#define ENSURE_NOT_NULL(ptr, str, status)                                      \
    do                                                                         \
    {                                                                          \
        if (ptr == NULL)                                                       \
        {                                                                      \
            printk("%s\n", str);                                               \
            return status;                                                     \
        }                                                                      \
    } while (0);

#define ADD_ELEMENT_TO_END_OF_LIST(elementtoadd, currentptr, headptr)          \
    do                                                                         \
    {                                                                          \
        if (NULL == currentptr)                                                \
        {                                                                      \
            currentptr = elementtoadd;                                         \
            elementtoadd->pNext = NULL;                                        \
            elementtoadd->pPrev = NULL;                                        \
            headptr = currentptr;                                              \
        }                                                                      \
        else                                                                   \
        {                                                                      \
            elementtoadd->pPrev = currentptr;                                  \
            currentptr->pNext = elementtoadd;                                  \
            elementtoadd->pNext = NULL;                                        \
            currentptr = elementtoadd;                                         \
        }                                                                      \
    } while (0);

#define REMOVE_ELEMENT_FROM_LIST(elementtoremove, currentptr, headptr)         \
    do                                                                         \
    {                                                                          \
        /*If the previous pointer is not NULL*/                                \
        if (NULL != elementtoremove->pPrev)                                    \
        {                                                                      \
            elementtoremove->pPrev->pNext = elementtoremove->pNext;            \
            if (elementtoremove->pNext)                                        \
            {                                                                  \
                elementtoremove->pNext->pPrev = elementtoremove->pPrev;        \
            }                                                                  \
            else                                                               \
            {                                                                  \
                currentptr = elementtoremove->pPrev;                           \
            }                                                                  \
        }                                                                      \
        else if (NULL != elementtoremove->pNext)                               \
        {                                                                      \
            elementtoremove->pNext->pPrev = NULL;                              \
            headptr = elementtoremove->pNext;                                  \
        }                                                                      \
        else                                                                   \
        {                                                                      \
            currentptr = NULL;                                                 \
            headptr = NULL;                                                    \
        }                                                                      \
    } while (0)

/* IOCTL number for use between the kernel and the user space application */
#define DEV_MEM_MAGIC 'q'
#define DEV_MEM_CMD_MEMALLOC (0)
#define DEV_MEM_CMD_MEMFREE (1)
#define DEV_MEM_CMD_CORPUS (2)

/* IOCTL commands for requesting kernel memory */
#define DEV_MEM_IOC_MEMALLOC                                                   \
    _IOWR(DEV_MEM_MAGIC, DEV_MEM_CMD_MEMALLOC, qae_dev_mem_info_t)

#define DEV_MEM_IOC_MEMFREE                                                    \
    _IOWR(DEV_MEM_MAGIC, DEV_MEM_CMD_MEMFREE, qae_dev_mem_info_t)

#define DEV_MEM_IOC_CORPUS                                                     \
    _IOWR(DEV_MEM_MAGIC, DEV_MEM_CMD_CORPUS, dev_mem_file_t)

#ifdef BLOCKOUT
qae_dev_mem_info_t *userMemAlloc(user_mem_dev_t *dev,
                                 Cpa32U sizeInBytes,
                                 Cpa32U node,
                                 Cpa32U pid);
qae_dev_mem_info_t *userMemGetInfo(user_mem_dev_t *dev, Cpa32U id, Cpa32U pid);
void userMemFree(user_mem_dev_t *dev, Cpa32U id, Cpa32U pid);

void userMemFreeAllPid(user_mem_dev_t *dev, Cpa32U pid);
void userMemFreeAll(user_mem_dev_t *dev);
#endif
/**
 *****************************************************************************
 * @ingroup quicksssistExtension_MemoryManagement
 *      qaeMemAlloc
 *
 * @description
 *      allocates memsize bytes of memory
 *
 *
 * @param[in] memsize, the amount of memory in bytes to be allocated
 *
 * @retval pointer to the allocated memory
 *
 * @pre
 *      none
 * @post
 *      memory is allocated
 *
 *****************************************************************************/
void *qaeMemAlloc(Cpa32U memsize);

/**
 *****************************************************************************
 * @ingroup quicksssistExtension_MemoryManagement
 *      qaeMemAllocNUMA
 *
 * @description
 *      allocates contiguous memory local to the cpu(node) of memsize bytes
 *
 *
 * @param[in] memsize, the amount of memory in bytes to be allocated
 *
 * @retval pointer to the allocated memory
 *
 * @pre
 *      none
 * @post
 *      memory is allocated
 *
 *****************************************************************************/
void *qaeMemAllocNUMA(Cpa32U size, Cpa32U node, Cpa32U alignment);

/**
 *****************************************************************************
 * @ingroup quicksssistExtension_MemoryManagement
 *      qaeVirtToPhysNUMA
 *
 * @description
 *      Converts a virtual address to a physical one
 *
 *
 * @param[in] pVirtAddr pointer to the virtual address
 *
 * @retval pointer to the physical address
 *
 * @pre
 *      none
 * @post
 *      memory is allocated
 *
 *****************************************************************************/

QAE_PHYS_ADDR qaeVirtToPhysNUMA(void *pVirtAddr);
/**
 *****************************************************************************
 * @ingroup quicksssistExtension_MemoryManagement
 *      qaeMemFreeNUMA
 *
 * @description
 *      frees memory allocated by the qaeMemAllocNUMA function
 *
 *
 * @param[in] pointer to the memory to be freed
 *
 * @retval none
 *
 * @pre
 *      none
 * @post
 *      memory is freed
 *
 *****************************************************************************/
void qaeMemFreeNUMA(void **ptr);

/**
 *****************************************************************************
 * @ingroup quicksssistExtension_MemoryManagement
 *      qaeMemFree
 *
 * @description
 *      frees memory allocated by the qaeMemAlloc function
 *
 *
 * @param[in] pointer to the memory to be freed
 *
 * @retval none
 *
 * @pre
 *      none
 * @post
 *      memory is freed
 *
 *****************************************************************************/
void qaeMemFree(void **ptr);

void printMemAllocations(void);
int register_mem_device_driver(void);
void unregister_mem_device_driver(void);
int qae_mem_init(void);
void qae_mem_exit(void);

#endif
