]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/dpdk/lib/librte_eal/bsdapp/contigmem/contigmem.c
update download target update for octopus release
[ceph.git] / ceph / src / seastar / dpdk / lib / librte_eal / bsdapp / contigmem / contigmem.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 <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <sys/param.h>
38 #include <sys/bio.h>
39 #include <sys/bus.h>
40 #include <sys/conf.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/proc.h>
45 #include <sys/rwlock.h>
46 #include <sys/systm.h>
47 #include <sys/sysctl.h>
48
49 #include <machine/bus.h>
50
51 #include <vm/vm.h>
52 #include <vm/pmap.h>
53 #include <vm/vm_object.h>
54 #include <vm/vm_page.h>
55 #include <vm/vm_pager.h>
56
57 static int contigmem_load(void);
58 static int contigmem_unload(void);
59 static int contigmem_physaddr(SYSCTL_HANDLER_ARGS);
60
61 static d_mmap_t contigmem_mmap;
62 static d_mmap_single_t contigmem_mmap_single;
63 static d_open_t contigmem_open;
64
65 static int contigmem_num_buffers = RTE_CONTIGMEM_DEFAULT_NUM_BUFS;
66 static int64_t contigmem_buffer_size = RTE_CONTIGMEM_DEFAULT_BUF_SIZE;
67
68 static eventhandler_tag contigmem_eh_tag;
69 static void *contigmem_buffers[RTE_CONTIGMEM_MAX_NUM_BUFS];
70 static struct cdev *contigmem_cdev = NULL;
71
72 TUNABLE_INT("hw.contigmem.num_buffers", &contigmem_num_buffers);
73 TUNABLE_QUAD("hw.contigmem.buffer_size", &contigmem_buffer_size);
74
75 static SYSCTL_NODE(_hw, OID_AUTO, contigmem, CTLFLAG_RD, 0, "contigmem");
76
77 SYSCTL_INT(_hw_contigmem, OID_AUTO, num_buffers, CTLFLAG_RD,
78 &contigmem_num_buffers, 0, "Number of contigmem buffers allocated");
79 SYSCTL_QUAD(_hw_contigmem, OID_AUTO, buffer_size, CTLFLAG_RD,
80 &contigmem_buffer_size, 0, "Size of each contiguous buffer");
81
82 static SYSCTL_NODE(_hw_contigmem, OID_AUTO, physaddr, CTLFLAG_RD, 0,
83 "physaddr");
84
85 MALLOC_DEFINE(M_CONTIGMEM, "contigmem", "contigmem(4) allocations");
86
87 static int contigmem_modevent(module_t mod, int type, void *arg)
88 {
89 int error = 0;
90
91 switch (type) {
92 case MOD_LOAD:
93 error = contigmem_load();
94 break;
95 case MOD_UNLOAD:
96 error = contigmem_unload();
97 break;
98 default:
99 break;
100 }
101
102 return error;
103 }
104
105 moduledata_t contigmem_mod = {
106 "contigmem",
107 (modeventhand_t)contigmem_modevent,
108 0
109 };
110
111 DECLARE_MODULE(contigmem, contigmem_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
112 MODULE_VERSION(contigmem, 1);
113
114 static struct cdevsw contigmem_ops = {
115 .d_name = "contigmem",
116 .d_version = D_VERSION,
117 .d_mmap = contigmem_mmap,
118 .d_mmap_single = contigmem_mmap_single,
119 .d_open = contigmem_open,
120 };
121
122 static int
123 contigmem_load()
124 {
125 char index_string[8], description[32];
126 int i;
127
128 if (contigmem_num_buffers > RTE_CONTIGMEM_MAX_NUM_BUFS) {
129 printf("%d buffers requested is greater than %d allowed\n",
130 contigmem_num_buffers, RTE_CONTIGMEM_MAX_NUM_BUFS);
131 return EINVAL;
132 }
133
134 if (contigmem_buffer_size < PAGE_SIZE ||
135 (contigmem_buffer_size & (contigmem_buffer_size - 1)) != 0) {
136 printf("buffer size 0x%lx is not greater than PAGE_SIZE and "
137 "power of two\n", contigmem_buffer_size);
138 return EINVAL;
139 }
140
141 for (i = 0; i < contigmem_num_buffers; i++) {
142 contigmem_buffers[i] =
143 contigmalloc(contigmem_buffer_size, M_CONTIGMEM, M_ZERO, 0,
144 BUS_SPACE_MAXADDR, contigmem_buffer_size, 0);
145
146 if (contigmem_buffers[i] == NULL) {
147 printf("contigmalloc failed for buffer %d\n", i);
148 return ENOMEM;
149 }
150
151 printf("%2u: virt=%p phys=%p\n", i, contigmem_buffers[i],
152 (void *)pmap_kextract((vm_offset_t)contigmem_buffers[i]));
153
154 snprintf(index_string, sizeof(index_string), "%d", i);
155 snprintf(description, sizeof(description),
156 "phys addr for buffer %d", i);
157 SYSCTL_ADD_PROC(NULL,
158 &SYSCTL_NODE_CHILDREN(_hw_contigmem, physaddr), OID_AUTO,
159 index_string, CTLTYPE_U64 | CTLFLAG_RD,
160 (void *)(uintptr_t)i, 0, contigmem_physaddr, "LU",
161 description);
162 }
163
164 contigmem_cdev = make_dev_credf(0, &contigmem_ops, 0, NULL, UID_ROOT,
165 GID_WHEEL, 0600, "contigmem");
166
167 return 0;
168 }
169
170 static int
171 contigmem_unload()
172 {
173 int i;
174
175 if (contigmem_cdev != NULL)
176 destroy_dev(contigmem_cdev);
177
178 if (contigmem_eh_tag != NULL)
179 EVENTHANDLER_DEREGISTER(process_exit, contigmem_eh_tag);
180
181 for (i = 0; i < RTE_CONTIGMEM_MAX_NUM_BUFS; i++)
182 if (contigmem_buffers[i] != NULL)
183 contigfree(contigmem_buffers[i], contigmem_buffer_size,
184 M_CONTIGMEM);
185
186 return 0;
187 }
188
189 static int
190 contigmem_physaddr(SYSCTL_HANDLER_ARGS)
191 {
192 uint64_t physaddr;
193 int index = (int)(uintptr_t)arg1;
194
195 physaddr = (uint64_t)vtophys(contigmem_buffers[index]);
196 return sysctl_handle_64(oidp, &physaddr, 0, req);
197 }
198
199 static int
200 contigmem_open(struct cdev *cdev, int fflags, int devtype,
201 struct thread *td)
202 {
203 return 0;
204 }
205
206 static int
207 contigmem_mmap(struct cdev *cdev, vm_ooffset_t offset, vm_paddr_t *paddr,
208 int prot, vm_memattr_t *memattr)
209 {
210
211 *paddr = offset;
212 return 0;
213 }
214
215 static int
216 contigmem_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, vm_size_t size,
217 struct vm_object **obj, int nprot)
218 {
219 uint64_t buffer_index;
220
221 /*
222 * The buffer index is encoded in the offset. Divide the offset by
223 * PAGE_SIZE to get the index of the buffer requested by the user
224 * app.
225 */
226 buffer_index = *offset / PAGE_SIZE;
227 if (buffer_index >= contigmem_num_buffers)
228 return EINVAL;
229
230 memset(contigmem_buffers[buffer_index], 0, contigmem_buffer_size);
231 *offset = (vm_ooffset_t)vtophys(contigmem_buffers[buffer_index]);
232 *obj = vm_pager_allocate(OBJT_DEVICE, cdev, size, nprot, *offset,
233 curthread->td_ucred);
234
235 return 0;
236 }