xref: /dpdk/drivers/common/iavf/iavf_impl.c (revision dede694cd58f6ee9c53f1e7d8d5960835bdc8fff)
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 int iavf_common_logger;
17 
18 enum iavf_status
19 iavf_allocate_dma_mem_d(__rte_unused struct iavf_hw *hw,
20 			struct iavf_dma_mem *mem,
21 			u64 size,
22 			u32 alignment)
23 {
24 	const struct rte_memzone *mz = NULL;
25 	char z_name[RTE_MEMZONE_NAMESIZE];
26 
27 	if (!mem)
28 		return IAVF_ERR_PARAM;
29 
30 	snprintf(z_name, sizeof(z_name), "iavf_dma_%"PRIu64, rte_rand());
31 	mz = rte_memzone_reserve_bounded(z_name, size, SOCKET_ID_ANY,
32 					 RTE_MEMZONE_IOVA_CONTIG, alignment,
33 					 RTE_PGSIZE_2M);
34 	if (!mz)
35 		return IAVF_ERR_NO_MEMORY;
36 
37 	mem->size = size;
38 	mem->va = mz->addr;
39 	mem->pa = mz->iova;
40 	mem->zone = (const void *)mz;
41 
42 	return IAVF_SUCCESS;
43 }
44 
45 enum iavf_status
46 iavf_free_dma_mem_d(__rte_unused struct iavf_hw *hw,
47 		    struct iavf_dma_mem *mem)
48 {
49 	if (!mem)
50 		return IAVF_ERR_PARAM;
51 
52 	rte_memzone_free((const struct rte_memzone *)mem->zone);
53 	mem->zone = NULL;
54 	mem->va = NULL;
55 	mem->pa = (u64)0;
56 
57 	return IAVF_SUCCESS;
58 }
59 
60 enum iavf_status
61 iavf_allocate_virt_mem_d(__rte_unused struct iavf_hw *hw,
62 			 struct iavf_virt_mem *mem,
63 			 u32 size)
64 {
65 	if (!mem)
66 		return IAVF_ERR_PARAM;
67 
68 	mem->size = size;
69 	mem->va = rte_zmalloc("iavf", size, 0);
70 
71 	if (mem->va)
72 		return IAVF_SUCCESS;
73 	else
74 		return IAVF_ERR_NO_MEMORY;
75 }
76 
77 enum iavf_status
78 iavf_free_virt_mem_d(__rte_unused struct iavf_hw *hw,
79 		     struct iavf_virt_mem *mem)
80 {
81 	if (!mem)
82 		return IAVF_ERR_PARAM;
83 
84 	rte_free(mem->va);
85 	mem->va = NULL;
86 
87 	return IAVF_SUCCESS;
88 }
89 
90 RTE_INIT(iavf_common_init_log)
91 {
92 	iavf_common_logger = rte_log_register("pmd.common.iavf");
93 	if (iavf_common_logger >= 0)
94 		rte_log_set_level(iavf_common_logger, RTE_LOG_NOTICE);
95 }
96