tailq: remove unneeded inclusions
[dpdk.git] / lib / librte_mempool / rte_dom0_mempool.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdint.h>
37 #include <unistd.h>
38 #include <stdarg.h>
39 #include <inttypes.h>
40 #include <errno.h>
41 #include <sys/queue.h>
42
43 #include <rte_common.h>
44 #include <rte_log.h>
45 #include <rte_debug.h>
46 #include <rte_memory.h>
47 #include <rte_memzone.h>
48 #include <rte_atomic.h>
49 #include <rte_launch.h>
50 #include <rte_eal.h>
51 #include <rte_eal_memconfig.h>
52 #include <rte_per_lcore.h>
53 #include <rte_lcore.h>
54 #include <rte_branch_prediction.h>
55 #include <rte_ring.h>
56 #include <rte_errno.h>
57 #include <rte_string_fns.h>
58 #include <rte_spinlock.h>
59
60 #include "rte_mempool.h"
61
62 static void
63 get_phys_map(void *va, phys_addr_t pa[], uint32_t pg_num,
64             uint32_t pg_sz, uint32_t memseg_id)
65 {
66     uint32_t i;
67     uint64_t virt_addr, mfn_id;
68     struct rte_mem_config *mcfg;
69     uint32_t page_size = getpagesize();
70
71     /* get pointer to global configuration */
72     mcfg = rte_eal_get_configuration()->mem_config;
73     virt_addr =(uintptr_t) mcfg->memseg[memseg_id].addr;
74
75     for (i = 0; i != pg_num; i++) {
76         mfn_id = ((uintptr_t)va + i * pg_sz - virt_addr) / RTE_PGSIZE_2M;
77         pa[i] = mcfg->memseg[memseg_id].mfn[mfn_id] * page_size;
78     }
79 }
80
81 /* create the mempool for supporting Dom0 */
82 struct rte_mempool *
83 rte_dom0_mempool_create(const char *name, unsigned elt_num, unsigned elt_size,
84            unsigned cache_size, unsigned private_data_size,
85            rte_mempool_ctor_t *mp_init, void *mp_init_arg,
86            rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
87            int socket_id, unsigned flags)
88 {
89         struct rte_mempool *mp = NULL;
90         phys_addr_t *pa;
91         char *va;
92         size_t sz;
93         uint32_t pg_num, pg_shift, pg_sz, total_size;
94         const struct rte_memzone *mz;
95         char mz_name[RTE_MEMZONE_NAMESIZE];
96         int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
97
98         pg_sz = RTE_PGSIZE_2M;
99
100         pg_shift = rte_bsf32(pg_sz);
101         total_size = rte_mempool_calc_obj_size(elt_size, flags, NULL);
102
103         /* calc max memory size and max number of pages needed. */
104         sz = rte_mempool_xmem_size(elt_num, total_size, pg_shift) +
105                 RTE_PGSIZE_2M;
106         pg_num = sz >> pg_shift;
107
108         /* extract physical mappings of the allocated memory. */
109         pa = calloc(pg_num, sizeof (*pa));
110         if (pa == NULL)
111                 return mp;
112
113         snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_OBJ_NAME, name);
114         mz = rte_memzone_reserve(mz_name, sz, socket_id, mz_flags);
115         if (mz == NULL) {
116                 free(pa);
117                 return mp;
118         }
119
120         va = (char *)RTE_ALIGN_CEIL((uintptr_t)mz->addr, RTE_PGSIZE_2M);
121         /* extract physical mappings of the allocated memory. */
122         get_phys_map(va, pa, pg_num, pg_sz, mz->memseg_id);
123
124         mp = rte_mempool_xmem_create(name, elt_num, elt_size,
125                 cache_size, private_data_size,
126                 mp_init, mp_init_arg,
127                 obj_init, obj_init_arg,
128                 socket_id, flags, va, pa, pg_num, pg_shift);
129
130         free(pa);
131
132         return (mp);
133 }