File: qae_mem_utils.h

package info (click to toggle)
qatlib 26.02.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,796 kB
  • sloc: ansic: 136,305; asm: 974; makefile: 407; sh: 401
file content (753 lines) | stat: -rw-r--r-- 27,980 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
/***************************************************************************
 *
 *   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_utils.h
*
* This file provides linux kernel memory allocation for quick assist API
*
*****************************************************************************/
#ifndef QAE_MEM_UTILS_H_
#define QAE_MEM_UTILS_H_
#if defined(__KERNEL__)
#include <asm/page.h>
#include <linux/io.h>
#include <linux/version.h>
#include <linux/mm.h>
#include <linux/atomic.h>

#if (KERNEL_VERSION(2, 6, 38) >= LINUX_VERSION_CODE)
#define kstrtoll strict_strtoll
#endif /* KERNEL_VERSION */
#endif /* __KERNEL__ */

#define USDM_MOD "usdm_drv: "

#define mm_err(...) pr_err(USDM_MOD __VA_ARGS__)

#define mm_info(...) pr_info(USDM_MOD __VA_ARGS__)

#define mm_warning(...) pr_warn(USDM_MOD __VA_ARGS__)

/*define types which need to vary between 32 and 64 bit*/
#define QAE_PAGE_SHIFT 12
#define QAE_PAGE_SIZE (1UL << QAE_PAGE_SHIFT)

/* QAE_NUM_PAGES_PER_ALLOC can be defined as 32 pages when library
is built, default is 512 */
#ifndef QAE_NUM_PAGES_PER_ALLOC
#define QAE_NUM_PAGES_PER_ALLOC 512
#endif

#define STATIC static
#define UNUSED(x) (void)(x)

#define QAE_PHYS_ADDR uint64_t

#define QAE_MEM_ZALLOC_GEN(size) kzalloc(size, GFP_KERNEL)
#define QAE_MEM_FREE(ptr)                                                      \
    do                                                                         \
    {                                                                          \
        if (ptr)                                                               \
        {                                                                      \
            kfree(ptr);                                                        \
            ptr = NULL;                                                        \
        }                                                                      \
    } while (0)

/* Defining Max Size limit to be used, to allocate using kmalloc as 4MB */
#define QAE_MEM_SIZE_LIMIT (1024 * 4096)

/**
 *****************************************************************************
 * @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 */
    size_t mSize;       /* allocated size */

} qae_mem_alloc_info_t;

enum slabType
{
    SMALL = 0,
    LARGE = 1,
    HUGE_PAGE = 2,
};

/* User space memory information structure. */
typedef struct dev_mem_info_s
{
    int64_t nodeId; /* shared b/w user/kernel */
    /* Node id for NUMA */
    uint64_t size; /* shared b/w user/kernel */
    /* Size of this block (bytes) */
    enum slabType type;
    /* Slab for normal memory or large memory */
    uint32_t allocations; /* user space only */
    /* Huge page file descriptor */
    int64_t hpg_fd; /* user space only */
    /* The huge page file descriptor of each slab */
    uint64_t phy_addr; /* shared b/w user/kernel */
    /* Physical address of the kmalloced area */
    union {
        void *virt_addr; /* user space only */
        uint64_t padding_virt;
    };
    /* Base address in user space - i.e. virtual address */
    union {
        struct dev_mem_info_s *pPrev_user; /* user space only */
        uint64_t padding_prevu;
    };
    union {
        struct dev_mem_info_s *pNext_user; /* user space only */
        uint64_t padding_nextu;
    };
    union {
        struct dev_mem_info_s *pPrev_user_hash; /* user space only */
        uint64_t padding_prevuh;
    };
    union {
        struct dev_mem_info_s *pNext_user_hash; /* user space only */
        uint64_t padding_nextuh;
    };
#ifdef ICP_THREAD_SPECIFIC_USDM
    /* These pointers are required for adding and deleting slabs
     * to/from TMP list. We cannot rely on existing pointers
     * because those are actually being filled with relevant
     * addresses given the slab was added to another lists.
     * Please see ADD_ELEMENT_TO_END_LIST, ADD_ELEMENT_TO_END_LIST
     * in usdm/include/qae_mem_utils.h
     */
    union {
        struct dev_mem_info_s *pPrev_user_vfiotmp; /* user space only */
        uint64_t padding_prevuvfio;
    };
    union {
        struct dev_mem_info_s *pNext_user_vfiotmp; /* user space only */
        uint64_t padding_nextuvfio;
    };
    uint32_t flag_pinned; /* required while using TMP list */
#endif                    /* ICP_THREAD_SPECIFIC_USDM */
} dev_mem_info_t;

typedef struct user_page_info_s
{
    /* Use 64-bit unsigned to support 32bit application on
     * a 64-bit kernel */
    uint64_t virt_addr;
    /* physical address shared b/w user/kernel */
    uint64_t phy_addr;
    uint64_t size;
} user_page_info_t;

/* size of allocation unit */
#define UNIT_SIZE 1024
#define QAE_KBYTE 1024
#define QWORD_WIDTH (8 * sizeof(uint64_t))
#define QWORD_ALL_ONE 0xFFFFFFFFFFFFFFFFULL

/*
Bitmap is used to keep track the allocation of each block
Each 1k block is represented by one bit allocated(1)/free(0)
BITMAP_LEN is a macro the represents the number of 64-bit quad words
that make up the bitmap
with 512 pages of 4k page and 1k units this value is 32
 */
#define CHUNK_SIZE (UNIT_SIZE * QWORD_WIDTH)

#define BITMAP_LEN (QAE_NUM_PAGES_PER_ALLOC * QAE_PAGE_SIZE / CHUNK_SIZE)

#define BLOCK_SIZES (BITMAP_LEN * QWORD_WIDTH)

/*block control structure */
typedef struct block_ctrl_s
{
    dev_mem_info_t mem_info; /* memory device info type */
    /* adding an extra element at the end to make a barrier */
    uint64_t bitmap[BITMAP_LEN + 1]; /* bitmap each bit represents a 1k block */
    uint16_t sizes[BLOCK_SIZES]; /* Holds the size of each allocated block */
} block_ctrl_t;

/**
 *****************************************************************************
 * @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;

#ifdef __KERNEL__
/* Kernel space memory information structure. */
typedef struct kdev_mem_info_s
{
    void *kmalloc_ptr; /* kernel space only (small slab) */
    /* Pointer to mem originally returned by kmalloc */
    void *huge_mem_ctrl;
    uint64_t size;
    /* Slab size */
    atomic_t mmap_ref;
    /* Mapped pages counter */
    uint64_t phy_addr; /* shared b/w user/kernel */
    /* Physical address of the kmalloc'ed area */
    struct kdev_mem_info_s *pPrev_kernel;
    struct kdev_mem_info_s *pNext_kernel;
    struct kdev_mem_info_s *pPrev_kernel_hash;
    struct kdev_mem_info_s *pNext_kernel_hash;
} kdev_mem_info_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
{
    int pid;
    uint64_t allocs_nr;
    uint64_t hugepages_nr;
    kdev_mem_info_t *head;
    kdev_mem_info_t *tail;
    kdev_mem_info_t *hugepage_head;
    kdev_mem_info_t *hugepage_tail;
    struct user_proc_mem_list_s *pPrev_user;
    struct user_proc_mem_list_s *pNext_user;
} 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;
#endif /* __KERNEL__ */

/*
 ******************************************************************************
 * @ingroup ADD_ELEMENT_TO_HEAD_LIST
 *      insert element at the head of a linked list
 * @description
 *      inserts a new element at the head of a
 *      double linked list in user or kernel mode
 *      depending on mode parameter
 *      elementToAdd - ptr to the new element
 *      headPtr - ptr to the first element in list
 *      tailPtr - ptr to the last element int the list
 *      mode - _kernel or _user
 ******************************************************************************/

#define ADD_ELEMENT_TO_HEAD_LIST(elementToAdd, headPtr, tailPtr, mode)         \
    do                                                                         \
    {                                                                          \
        elementToAdd->pPrev##mode = NULL;                                      \
        if (NULL == headPtr)                                                   \
        {                                                                      \
            tailPtr = elementToAdd;                                            \
            elementToAdd->pNext##mode = NULL;                                  \
        }                                                                      \
        else                                                                   \
        {                                                                      \
            elementToAdd->pNext##mode = headPtr;                               \
            headPtr->pPrev##mode = elementToAdd;                               \
        }                                                                      \
        headPtr = elementToAdd;                                                \
    } while (0)

/*
 ******************************************************************************
 * @ingroup ADD_ELEMENT_TO_END_LIST
 *      insert element at the end of a linked list
 * @description
 *      inserts a new element at the head of a
 *      double linked list in user or kernel mode
 *      depending on mode parameter
 *      elementToAdd - ptr to the new element
 *      headPtr - ptr to the first element in list
 *      tailPtr - ptr to the last element int the list
 *      mode - _kernel or _user
 ******************************************************************************/

#define ADD_ELEMENT_TO_END_LIST(elementToAdd, headPtr, tailPtr, mode)          \
    do                                                                         \
    {                                                                          \
        elementToAdd->pNext##mode = NULL;                                      \
        if (NULL == tailPtr)                                                   \
        {                                                                      \
            headPtr = elementToAdd;                                            \
            elementToAdd->pPrev##mode = NULL;                                  \
        }                                                                      \
        else                                                                   \
        {                                                                      \
            elementToAdd->pPrev##mode = tailPtr;                               \
            tailPtr->pNext##mode = elementToAdd;                               \
        }                                                                      \
        tailPtr = elementToAdd;                                                \
    } while (0)

/*
 ******************************************************************************
 * @ingroup REMOVE_ELEMENT_FROM_LIST
 *      remove element at the end of a linked list
 * @description
 *      removes an element from a
 *      double linked list in user or kernel mode
 *      depending on mode parameter
 *      elementToREmove - ptr to the new element
 *      headPtr - ptr to the first element in list
 *      tailPtr - ptr to the last element int the list
 *      mode - _kernel or _user
 ******************************************************************************/

#define REMOVE_ELEMENT_FROM_LIST(elementToRemove, headPtr, tailPtr, mode)      \
    do                                                                         \
    {                                                                          \
        if (NULL != elementToRemove->pPrev##mode)                              \
        {                                                                      \
            elementToRemove->pPrev##mode->pNext##mode =                        \
                elementToRemove->pNext##mode;                                  \
            if (NULL != elementToRemove->pNext##mode)                          \
            {                                                                  \
                elementToRemove->pNext##mode->pPrev##mode =                    \
                    elementToRemove->pPrev##mode;                              \
            }                                                                  \
            else                                                               \
            {                                                                  \
                tailPtr = elementToRemove->pPrev##mode;                        \
            }                                                                  \
        }                                                                      \
        else                                                                   \
        {                                                                      \
            if (NULL != elementToRemove->pNext##mode)                          \
            {                                                                  \
                elementToRemove->pNext##mode->pPrev##mode = NULL;              \
                headPtr = elementToRemove->pNext##mode;                        \
            }                                                                  \
            else                                                               \
            {                                                                  \
                headPtr = NULL;                                                \
                tailPtr = 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_RELEASE (2)
#define DEV_MEM_CMD_UNREGISTER (3)
#define DEV_MEM_CMD_GET_NUM_HPT (4)
#define DEV_MEM_CMD_GET_USER_PAGE (5)
#define DEV_MEM_CMD_HUGEPAGE_IOMMU_UNMAP (6)

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

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

#define DEV_MEM_IOC_RELEASE _IO(DEV_MEM_MAGIC, DEV_MEM_CMD_RELEASE)

#define DEV_MEM_IOC_UNREGISTER                                                 \
    _IOWR(DEV_MEM_MAGIC, DEV_MEM_CMD_UNREGISTER, dev_mem_info_t)

#define DEV_MEM_IOC_GET_NUM_HPT                                                \
    _IOWR(DEV_MEM_MAGIC, DEV_MEM_CMD_GET_NUM_HPT, uint32_t)

#define DEV_MEM_IOC_GET_USER_PAGE                                              \
    _IOWR(DEV_MEM_MAGIC, DEV_MEM_CMD_GET_USER_PAGE, user_page_info_t)

#define DEV_MEM_IOC_HUGEPAGE_IOMMU_UNMAP                                       \
    _IOWR(DEV_MEM_MAGIC, DEV_MEM_CMD_HUGEPAGE_IOMMU_UNMAP, user_page_info_t)
/*****************************************************************************
 * * @ingroup CommonMemoryDriver
 *       qaeMemInit
 *
 * @description
 *        Initialize the user-space allocator, opening the device driver
 *        used to communicate with the kernel-space.
 *
 * @param[in] path - path to the specific device
 *
 * @retval 0 if the open of the device was successful and
 *         non-zero otherwise
 * @pre
 *       none
 * @post
 *       Allocator is initialized
 *
 ****************************************************************************/
int32_t qaeMemInit(void);

/*****************************************************************************
 * * @ingroup CommonMemoryDriver
 *      qaeMemDestroy
 *
 * @description
 *      Release the user-space allocator. It closes the file descriptor
 *      associated with the device driver
 *
 * @param[in] none
 *
 * @retval none
 *
 * @pre
 *        The user space allocator is initialized using qaeMemInit
 * @post
 *        The user-space allocator is released
 *
 ****************************************************************************/
void qaeMemDestroy(void);

/*****************************************************************************
 * * @ingroup CommonMemoryDriver
 *       qaeIOMMUInit
 *
 * @description
 *        Function creates iommu domain. Applicable when IOMMU is enabled
 *
 * @param[in] none
 *
 * @retval 0 - if successful.
 *         non-zero - otherwise
 *
 * @pre
 *       IOMMU is enabled.
 * @post
 *       iommu domain created
 *
 ****************************************************************************/
int32_t qaeIOMMUInit(void);

/*****************************************************************************
 * * @ingroup CommonMemoryDriver
 *       qaeIOMMUExit
 *
 * @description
 *        Function removes iommu domain. Applicable when IOMMU is enabled
 *
 * @param[in] none
 *
 * @retval none
 *
 * @pre
 *      IOMMU is enabled and an iommu domain is created using qaeIOMMUInit
 * @post
 *      iommu domain removed
 *
 ****************************************************************************/
void qaeIOMMUExit(void);

/*****************************************************************************
 * * @ingroup CommonMemoryDriver
 *      qaeIOMMUgetRemappingSize
 *
 * @description
 *      Function calculates size for remapping when IOMMU is enabled.
 *      Before calling any of the qaeMemAlloc functions, this function can be
 *      used to calculate the actual size of memory to be allocated.
 *      The remapping size is at least PAGE_SIZE.
 *
 * @param[in] size - Actual size of the memory to be allocated
 *
 * @retval Remapping size
 *
 * @pre
 *     IOMMU is enabled and an iommu domain is created using qaeIOMMUInit.
 * @post
 *     Remapping size provided.
 *
 ****************************************************************************/
size_t qaeIOMMUgetRemappingSize(size_t size);

/*****************************************************************************
 * * @ingroup CommonMemoryDriver
 *      qaeIOMMUMap
 *
 * @description
 *      Function adds mapping from io virtual address to a physical address.
 *      Applicable when IOMMU is enabled
 *
 * @param[in] phaddr - Host physical address.
 * @param[in] iova - IO virtual address.
 * @param[in] size - Memory size to be remapped obtained from
 *                   qaeIOMMUgetRemappingSize() function.
 *
 * @retval CPA_STATUS_SUCCESS - if successful.
 *         CPA_STATUS_UNSUPPORTED - if not supported
 *         CPA_STATUS_FAIL - otherwise
 *
 * @pre
 *      An iommu domain is created using qaeIOMMUInit. iova points to
 *      previously allocated memory. phaddr is already obtained using
 *      iova using virt_to_phys or similar functions. size is calculated
 *      using qaeIOMMUgetRemappingSize function.
 * @post
 *      IO virtual address mapped
 ****************************************************************************/
int32_t qaeIOMMUMap(uint64_t phaddr, uint64_t iova, size_t size);

/*****************************************************************************
 * * @ingroup CommonMemoryDriver
 *       qaeIOMMUUnmap
 *
 * @description
 *        Function removes mapping from io virtual address to a physical
 *        address. Applicable when IOMMU is enabled
 *
 * @param[in] iova - IO virtual address.
 * @param[in] size - Memory size to be unmapped
 *
 * @retval CPA_STATUS_SUCCESS - if successful.
 *         CPA_STATUS_UNSUPPORTED - if not supported
 *         CPA_STATUS_FAIL - otherwise
 *
 * @pre
 *      An iommu domain is created using qaeIOMMUInit. iova points to
 *      previously allocated memory.
 * @post
 *       IO virtual address unmapped
 ****************************************************************************/
int32_t qaeIOMMUUnmap(uint64_t iova, size_t size);

/*****************************************************************************
 * * @ingroup CommonMemoryDriver
 *      qaeIOMMUVirtToPhys
 *
 * @description
 *      Function translates io virtual address to a physical address.
 *      Applicable when IOMMU is enabled.
 *
 * @param[in] iova, IO virtual address
 *
 * @retval host physical address - if successful
 *         NULL Otherwise
 *
 * @pre
 *      An iommu domain is created using qaeIOMMUInit. iova points to
 *      previously allocated memory.
 * @post
 *       virtual address is translated to physical address
 *
 ****************************************************************************/
uint64_t qaeIOMMUVirtToPhys(uint64_t iova);

/*****************************************************************************
 * * @ingroup CommonMemoryDriver
 *       qaeIOMMUAttachDev
 *
 * @description
 *      This function attaches a pci dev (VF) to an iommu domain.
 *      Applicable when IOMMU/SRIOV are enabled and after the driver bringup
 *      in Host is successful.
 *
 * @param[in] dev, Device to be attached
 *
 * @retval CPA_STATUS_SUCCESS - if successful
 *         CPA_STATUS_UNSUPPORTED - if not supported
 *         CPA_STATUS_FAIL - otherwise
 *
 * @pre
 *      An iommu domain is created using qaeIOMMUInit. Driver bringup
 *      in Host is successful.
 * @post
 *       device is attached
 *
 ****************************************************************************/
int32_t qaeIOMMUAttachDev(void *dev);

/*****************************************************************************
 * * @ingroup CommonMemoryDriver
 *       qaeIOMMUDetachDev
 *
 * @description
 *        Function detaches pci dev to iommu domain
 *
 * @param[in] dev, Device to be detached
 *
 * @retval none
 *
 * @pre
 *      An iommu domain is created using qaeIOMMUInit, Driver bringup
 *      in Host is successful and dev is already
 *      attached using qaeIOMMUAttachDev
 * @post
 *      Device is detached
 *
 ****************************************************************************/
void qaeIOMMUDetachDev(void *dev);

/**
 *****************************************************************************
 * @ingroup CommonMemoryDriver
 *     printMemAllocations
 *
 *  @description
 *     Prints only the overall count of NUMA and non-NUMA memory allocations
 *     performed. This doesn't provide other details like the allocation
 *     sizes, pointers etc.
 *
 * @retval none
 *
 * @pre
 *       The user space allocator is initialized using qaeMemInit
 * @post
 *       memory allocation count printed
 *
 ****************************************************************************/
void printMemAllocations(void);

#ifndef __KERNEL__
#ifdef ICP_WITHOUT_THREAD
#define mem_mutex_lock(x) (0)
#define mem_mutex_unlock(x) (0)
#else
#define mem_mutex_lock(x) pthread_mutex_lock(x)
#define mem_mutex_unlock(x) pthread_mutex_unlock(x)
#endif

#define mem_ioctl(fd, cmd, pMemInfo) ioctl(fd, cmd, pMemInfo)
#define qae_open(file, options) open(file, options)
#define qae_close(fd) close(fd)
#define qae_lseek(fd, offset, whence) lseek(fd, offset, whence)
#define qae_read(fd, buf, nbytes) read(fd, buf, nbytes)
#define qae_mmap(addr, length, prot, flags, fd, offset)                        \
    mmap(addr, length, prot, flags, fd, offset)
#define qae_munmap(addr, length) munmap(addr, length)
#define qae_madvise(addr, len, advice) madvise(addr, len, advice)
#define qae_fopen(filename, operation) fopen(filename, operation)
#define qae_fgets(str, n, stream) fgets(str, n, stream)
#define qae_opendir(dirname) opendir(dirname)
#define qae_mkstemp(template) mkstemp(template)
#endif

#if defined(__KERNEL__)
int handle_other_ioctls(uint32_t cmd);
#if defined(ICP_ADF_IOMMU)
int icp_adf_iommu_map(void *iova, void *phaddr, size_t size);
int icp_adf_iommu_unmap(void *iova, size_t size);
size_t icp_adf_iommu_get_remapping_size(size_t size);
static inline int icp_iommu_map(void **iova, void *vaddr, size_t size)
{
    void *phaddr = (void *)virt_to_phys(vaddr);
    *iova = phaddr;
    return icp_adf_iommu_map(*iova, phaddr, size);
}
static inline int icp_iommu_unmap(void *iova, size_t size)
{
    return icp_adf_iommu_unmap(iova, size);
}
static inline size_t icp_iommu_get_remapping_size(size_t size)
{
    return icp_adf_iommu_get_remapping_size(size);
}
#elif defined(ICP_OSAL_IOMMU)
int osalIOMMUMap(uint64_t iova, uint64_t phaddr, size_t size);
static inline int icp_iommu_map(void **iova, void *vaddr, size_t size)
{
    void *phaddr = (void *)virt_to_phys(vaddr);
    *iova = phaddr;
    return osalIOMMUMap((uintptr_t)*iova, phaddr, size);
}

int osalIOMMUUnmap(uint64_t iova, size_t size);
static inline int icp_iommu_unmap(void *iova, size_t size)
{
    return osalIOMMUUnmap((uintptr_t)iova, size);
}
uint64_t osalIOMMUVirtToPhys(uint64_t iova);
static inline uint64_t icp_iommu_virt_to_phys(void *iova)
{
    return osalIOMMUVirtToPhys((uintptr_t)iova);
}
size_t osalIOMMUgetRemappingSize(size_t size);
static inline size_t icp_iommu_get_remapping_size(size_t size)
{
    return osalIOMMUgetRemappingSize(size);
}
#elif defined(ICP_QDM_IOMMU)
int qdm_iommu_map(void **iova, void *vaddr, size_t size);
int qdm_iommu_unmap(void *iova, size_t size);
int qdm_hugepage_iommu_map(void **iova, void *va_page, size_t size);
static inline int icp_iommu_map(void **iova, void *vaddr, size_t size)
{
    return qdm_iommu_map(iova, vaddr, size);
}
static inline int icp_iommu_unmap(void *iova, size_t size)
{
    return qdm_iommu_unmap(iova, size);
}
static inline int icp_hugepage_iommu_map(void **iova,
                                         void *va_page,
                                         size_t size)
{
    return qdm_hugepage_iommu_map(iova, va_page, size);
}
static inline int icp_hugepage_iommu_unmap(void *iova, size_t size)
{
    return qdm_iommu_unmap(iova, size);
}
static inline size_t icp_iommu_get_remapping_size(size_t size)
{
    return (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
}
#else
#define ICP_IOMMU_DISABLED
static inline int icp_iommu_map(void **iova, void *vaddr, size_t size)
{
    *iova = (void *)(uintptr_t)virt_to_phys(vaddr);
    return 0;
}

static inline int icp_iommu_unmap(void *iova, size_t size)
{
    return 0;
}

static inline int icp_hugepage_iommu_map(void **iova,
                                         void *va_page,
                                         size_t size)
{
    *iova = (void *)(uintptr_t)page_to_phys((struct page *)va_page);
    return 0;
}
static inline int icp_hugepage_iommu_unmap(void *iova, size_t size)
{
    return 0;
}
static inline size_t icp_iommu_get_remapping_size(size_t size)
{
    return (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
}
#endif
#endif
#endif