xref: /dpdk/drivers/common/iavf/iavf_impl.c (revision e12a0166c80f65e35408f4715b2f3a60763c3741)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019-2021 Intel Corporation
3  */
4 
5 #include <stdio.h>
6 #include <inttypes.h>
7 
8 #include <rte_common.h>
9 #include <rte_malloc.h>
10 #include <rte_memzone.h>
11 
12 #include "iavf_type.h"
13 #include "iavf_prototype.h"
14 
15 enum iavf_status
iavf_allocate_dma_mem_d(__rte_unused struct iavf_hw * hw,struct iavf_dma_mem * mem,u64 size,u32 alignment)16 iavf_allocate_dma_mem_d(__rte_unused struct iavf_hw *hw,
17 			struct iavf_dma_mem *mem,
18 			u64 size,
19 			u32 alignment)
20 {
21 	static RTE_ATOMIC(uint64_t) iavf_dma_memzone_id;
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,
29 		rte_atomic_fetch_add_explicit(&iavf_dma_memzone_id, 1, rte_memory_order_relaxed));
30 	mz = rte_memzone_reserve_bounded(z_name, size, SOCKET_ID_ANY,
31 					 RTE_MEMZONE_IOVA_CONTIG, alignment,
32 					 RTE_PGSIZE_2M);
33 	if (!mz)
34 		return IAVF_ERR_NO_MEMORY;
35 
36 	mem->size = size;
37 	mem->va = mz->addr;
38 	mem->pa = mz->iova;
39 	mem->zone = (const void *)mz;
40 
41 	return IAVF_SUCCESS;
42 }
43 
44 enum iavf_status
iavf_free_dma_mem_d(__rte_unused struct iavf_hw * hw,struct iavf_dma_mem * mem)45 iavf_free_dma_mem_d(__rte_unused struct iavf_hw *hw,
46 		    struct iavf_dma_mem *mem)
47 {
48 	if (!mem)
49 		return IAVF_ERR_PARAM;
50 
51 	rte_memzone_free((const struct rte_memzone *)mem->zone);
52 	mem->zone = NULL;
53 	mem->va = NULL;
54 	mem->pa = (u64)0;
55 
56 	return IAVF_SUCCESS;
57 }
58 
59 enum iavf_status
iavf_allocate_virt_mem_d(__rte_unused struct iavf_hw * hw,struct iavf_virt_mem * mem,u32 size)60 iavf_allocate_virt_mem_d(__rte_unused struct iavf_hw *hw,
61 			 struct iavf_virt_mem *mem,
62 			 u32 size)
63 {
64 	if (!mem)
65 		return IAVF_ERR_PARAM;
66 
67 	mem->size = size;
68 	mem->va = rte_zmalloc("iavf", size, 0);
69 
70 	if (mem->va)
71 		return IAVF_SUCCESS;
72 	else
73 		return IAVF_ERR_NO_MEMORY;
74 }
75 
76 enum iavf_status
iavf_free_virt_mem_d(__rte_unused struct iavf_hw * hw,struct iavf_virt_mem * mem)77 iavf_free_virt_mem_d(__rte_unused struct iavf_hw *hw,
78 		     struct iavf_virt_mem *mem)
79 {
80 	if (!mem)
81 		return IAVF_ERR_PARAM;
82 
83 	rte_free(mem->va);
84 	mem->va = NULL;
85 
86 	return IAVF_SUCCESS;
87 }
88 
89 RTE_LOG_REGISTER_DEFAULT(iavf_common_logger, NOTICE);
90