1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019-2020 Intel Corporation
3 */
4
5 #include <stdio.h>
6 #include <inttypes.h>
7
8 #include <rte_common.h>
9 #include <rte_random.h>
10 #include <rte_malloc.h>
11 #include <rte_memzone.h>
12
13 #include "iavf_type.h"
14 #include "iavf_prototype.h"
15
16 enum iavf_status
iavf_allocate_dma_mem_d(__rte_unused struct iavf_hw * hw,struct iavf_dma_mem * mem,u64 size,u32 alignment)17 iavf_allocate_dma_mem_d(__rte_unused struct iavf_hw *hw,
18 struct iavf_dma_mem *mem,
19 u64 size,
20 u32 alignment)
21 {
22 const struct rte_memzone *mz = NULL;
23 char z_name[RTE_MEMZONE_NAMESIZE];
24
25 if (!mem)
26 return IAVF_ERR_PARAM;
27
28 snprintf(z_name, sizeof(z_name), "iavf_dma_%"PRIu64, rte_rand());
29 mz = rte_memzone_reserve_bounded(z_name, size, SOCKET_ID_ANY,
30 RTE_MEMZONE_IOVA_CONTIG, alignment,
31 RTE_PGSIZE_2M);
32 if (!mz)
33 return IAVF_ERR_NO_MEMORY;
34
35 mem->size = size;
36 mem->va = mz->addr;
37 mem->pa = mz->iova;
38 mem->zone = (const void *)mz;
39
40 return IAVF_SUCCESS;
41 }
42
43 enum iavf_status
iavf_free_dma_mem_d(__rte_unused struct iavf_hw * hw,struct iavf_dma_mem * mem)44 iavf_free_dma_mem_d(__rte_unused struct iavf_hw *hw,
45 struct iavf_dma_mem *mem)
46 {
47 if (!mem)
48 return IAVF_ERR_PARAM;
49
50 rte_memzone_free((const struct rte_memzone *)mem->zone);
51 mem->zone = NULL;
52 mem->va = NULL;
53 mem->pa = (u64)0;
54
55 return IAVF_SUCCESS;
56 }
57
58 enum iavf_status
iavf_allocate_virt_mem_d(__rte_unused struct iavf_hw * hw,struct iavf_virt_mem * mem,u32 size)59 iavf_allocate_virt_mem_d(__rte_unused struct iavf_hw *hw,
60 struct iavf_virt_mem *mem,
61 u32 size)
62 {
63 if (!mem)
64 return IAVF_ERR_PARAM;
65
66 mem->size = size;
67 mem->va = rte_zmalloc("iavf", size, 0);
68
69 if (mem->va)
70 return IAVF_SUCCESS;
71 else
72 return IAVF_ERR_NO_MEMORY;
73 }
74
75 enum iavf_status
iavf_free_virt_mem_d(__rte_unused struct iavf_hw * hw,struct iavf_virt_mem * mem)76 iavf_free_virt_mem_d(__rte_unused struct iavf_hw *hw,
77 struct iavf_virt_mem *mem)
78 {
79 if (!mem)
80 return IAVF_ERR_PARAM;
81
82 rte_free(mem->va);
83 mem->va = NULL;
84
85 return IAVF_SUCCESS;
86 }
87
88 RTE_LOG_REGISTER(iavf_common_logger, pmd.common.iavf, NOTICE);
89