New upstream version 16.11.5
[deb_dpdk.git] / 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 #include <sys/vmmeter.h>
49
50 #include <machine/bus.h>
51
52 #include <vm/vm.h>
53 #include <vm/pmap.h>
54 #include <vm/vm_param.h>
55 #include <vm/vm_object.h>
56 #include <vm/vm_page.h>
57 #include <vm/vm_pager.h>
58 #include <vm/vm_phys.h>
59
60 struct contigmem_buffer {
61         void           *addr;
62         int             refcnt;
63         struct mtx      mtx;
64 };
65
66 struct contigmem_vm_handle {
67         int             buffer_index;
68 };
69
70 static int              contigmem_load(void);
71 static int              contigmem_unload(void);
72 static int              contigmem_physaddr(SYSCTL_HANDLER_ARGS);
73
74 static d_mmap_single_t  contigmem_mmap_single;
75 static d_open_t         contigmem_open;
76 static d_close_t        contigmem_close;
77
78 static int              contigmem_num_buffers = RTE_CONTIGMEM_DEFAULT_NUM_BUFS;
79 static int64_t          contigmem_buffer_size = RTE_CONTIGMEM_DEFAULT_BUF_SIZE;
80
81 static eventhandler_tag contigmem_eh_tag;
82 static struct contigmem_buffer contigmem_buffers[RTE_CONTIGMEM_MAX_NUM_BUFS];
83 static struct cdev     *contigmem_cdev = NULL;
84 static int              contigmem_refcnt;
85
86 TUNABLE_INT("hw.contigmem.num_buffers", &contigmem_num_buffers);
87 TUNABLE_QUAD("hw.contigmem.buffer_size", &contigmem_buffer_size);
88
89 static SYSCTL_NODE(_hw, OID_AUTO, contigmem, CTLFLAG_RD, 0, "contigmem");
90
91 SYSCTL_INT(_hw_contigmem, OID_AUTO, num_buffers, CTLFLAG_RD,
92         &contigmem_num_buffers, 0, "Number of contigmem buffers allocated");
93 SYSCTL_QUAD(_hw_contigmem, OID_AUTO, buffer_size, CTLFLAG_RD,
94         &contigmem_buffer_size, 0, "Size of each contiguous buffer");
95 SYSCTL_INT(_hw_contigmem, OID_AUTO, num_references, CTLFLAG_RD,
96         &contigmem_refcnt, 0, "Number of references to contigmem");
97
98 static SYSCTL_NODE(_hw_contigmem, OID_AUTO, physaddr, CTLFLAG_RD, 0,
99         "physaddr");
100
101 MALLOC_DEFINE(M_CONTIGMEM, "contigmem", "contigmem(4) allocations");
102
103 static int contigmem_modevent(module_t mod, int type, void *arg)
104 {
105         int error = 0;
106
107         switch (type) {
108         case MOD_LOAD:
109                 error = contigmem_load();
110                 break;
111         case MOD_UNLOAD:
112                 error = contigmem_unload();
113                 break;
114         default:
115                 break;
116         }
117
118         return error;
119 }
120
121 moduledata_t contigmem_mod = {
122         "contigmem",
123         (modeventhand_t)contigmem_modevent,
124         0
125 };
126
127 DECLARE_MODULE(contigmem, contigmem_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
128 MODULE_VERSION(contigmem, 1);
129
130 static struct cdevsw contigmem_ops = {
131         .d_name         = "contigmem",
132         .d_version      = D_VERSION,
133         .d_flags        = D_TRACKCLOSE,
134         .d_mmap_single  = contigmem_mmap_single,
135         .d_open         = contigmem_open,
136         .d_close        = contigmem_close,
137 };
138
139 static int
140 contigmem_load()
141 {
142         char index_string[8], description[32];
143         int  i, error = 0;
144         void *addr;
145
146         if (contigmem_num_buffers > RTE_CONTIGMEM_MAX_NUM_BUFS) {
147                 printf("%d buffers requested is greater than %d allowed\n",
148                                 contigmem_num_buffers, RTE_CONTIGMEM_MAX_NUM_BUFS);
149                 error = EINVAL;
150                 goto error;
151         }
152
153         if (contigmem_buffer_size < PAGE_SIZE ||
154                         (contigmem_buffer_size & (contigmem_buffer_size - 1)) != 0) {
155                 printf("buffer size 0x%lx is not greater than PAGE_SIZE and "
156                                 "power of two\n", contigmem_buffer_size);
157                 error = EINVAL;
158                 goto error;
159         }
160
161         for (i = 0; i < contigmem_num_buffers; i++) {
162                 addr = contigmalloc(contigmem_buffer_size, M_CONTIGMEM, M_ZERO,
163                         0, BUS_SPACE_MAXADDR, contigmem_buffer_size, 0);
164                 if (addr == NULL) {
165                         printf("contigmalloc failed for buffer %d\n", i);
166                         error = ENOMEM;
167                         goto error;
168                 }
169
170                 printf("%2u: virt=%p phys=%p\n", i, addr,
171                         (void *)pmap_kextract((vm_offset_t)addr));
172
173                 mtx_init(&contigmem_buffers[i].mtx, "contigmem", NULL, MTX_DEF);
174                 contigmem_buffers[i].addr = addr;
175                 contigmem_buffers[i].refcnt = 0;
176
177                 snprintf(index_string, sizeof(index_string), "%d", i);
178                 snprintf(description, sizeof(description),
179                                 "phys addr for buffer %d", i);
180                 SYSCTL_ADD_PROC(NULL,
181                                 &SYSCTL_NODE_CHILDREN(_hw_contigmem, physaddr), OID_AUTO,
182                                 index_string, CTLTYPE_U64 | CTLFLAG_RD,
183                                 (void *)(uintptr_t)i, 0, contigmem_physaddr, "LU",
184                                 description);
185         }
186
187         contigmem_cdev = make_dev_credf(0, &contigmem_ops, 0, NULL, UID_ROOT,
188                         GID_WHEEL, 0600, "contigmem");
189
190         return 0;
191
192 error:
193         for (i = 0; i < contigmem_num_buffers; i++) {
194                 if (contigmem_buffers[i].addr != NULL)
195                         contigfree(contigmem_buffers[i].addr,
196                                 contigmem_buffer_size, M_CONTIGMEM);
197                 if (mtx_initialized(&contigmem_buffers[i].mtx))
198                         mtx_destroy(&contigmem_buffers[i].mtx);
199         }
200
201         return error;
202 }
203
204 static int
205 contigmem_unload()
206 {
207         int i;
208
209         if (contigmem_refcnt > 0)
210                 return EBUSY;
211
212         if (contigmem_cdev != NULL)
213                 destroy_dev(contigmem_cdev);
214
215         if (contigmem_eh_tag != NULL)
216                 EVENTHANDLER_DEREGISTER(process_exit, contigmem_eh_tag);
217
218         for (i = 0; i < RTE_CONTIGMEM_MAX_NUM_BUFS; i++) {
219                 if (contigmem_buffers[i].addr != NULL)
220                         contigfree(contigmem_buffers[i].addr,
221                                 contigmem_buffer_size, M_CONTIGMEM);
222                 if (mtx_initialized(&contigmem_buffers[i].mtx))
223                         mtx_destroy(&contigmem_buffers[i].mtx);
224         }
225
226         return 0;
227 }
228
229 static int
230 contigmem_physaddr(SYSCTL_HANDLER_ARGS)
231 {
232         uint64_t        physaddr;
233         int             index = (int)(uintptr_t)arg1;
234
235         physaddr = (uint64_t)vtophys(contigmem_buffers[index].addr);
236         return sysctl_handle_64(oidp, &physaddr, 0, req);
237 }
238
239 static int
240 contigmem_open(struct cdev *cdev, int fflags, int devtype,
241                 struct thread *td)
242 {
243
244         atomic_add_int(&contigmem_refcnt, 1);
245
246         return 0;
247 }
248
249 static int
250 contigmem_close(struct cdev *cdev, int fflags, int devtype,
251                 struct thread *td)
252 {
253
254         atomic_subtract_int(&contigmem_refcnt, 1);
255
256         return 0;
257 }
258
259 static int
260 contigmem_cdev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
261                 vm_ooffset_t foff, struct ucred *cred, u_short *color)
262 {
263         struct contigmem_vm_handle *vmh = handle;
264         struct contigmem_buffer *buf;
265
266         buf = &contigmem_buffers[vmh->buffer_index];
267
268         atomic_add_int(&contigmem_refcnt, 1);
269
270         mtx_lock(&buf->mtx);
271         if (buf->refcnt == 0)
272                 memset(buf->addr, 0, contigmem_buffer_size);
273         buf->refcnt++;
274         mtx_unlock(&buf->mtx);
275
276         return 0;
277 }
278
279 static void
280 contigmem_cdev_pager_dtor(void *handle)
281 {
282         struct contigmem_vm_handle *vmh = handle;
283         struct contigmem_buffer *buf;
284
285         buf = &contigmem_buffers[vmh->buffer_index];
286
287         mtx_lock(&buf->mtx);
288         buf->refcnt--;
289         mtx_unlock(&buf->mtx);
290
291         free(vmh, M_CONTIGMEM);
292
293         atomic_subtract_int(&contigmem_refcnt, 1);
294 }
295
296 static int
297 contigmem_cdev_pager_fault(vm_object_t object, vm_ooffset_t offset, int prot,
298                 vm_page_t *mres)
299 {
300         vm_paddr_t paddr;
301         vm_page_t m_paddr, page;
302         vm_memattr_t memattr, memattr1;
303
304         memattr = object->memattr;
305
306         VM_OBJECT_WUNLOCK(object);
307
308         paddr = offset;
309
310         m_paddr = vm_phys_paddr_to_vm_page(paddr);
311         if (m_paddr != NULL) {
312                 memattr1 = pmap_page_get_memattr(m_paddr);
313                 if (memattr1 != memattr)
314                         memattr = memattr1;
315         }
316
317         if (((*mres)->flags & PG_FICTITIOUS) != 0) {
318                 /*
319                  * If the passed in result page is a fake page, update it with
320                  * the new physical address.
321                  */
322                 page = *mres;
323                 VM_OBJECT_WLOCK(object);
324                 vm_page_updatefake(page, paddr, memattr);
325         } else {
326                 vm_page_t mret;
327                 /*
328                  * Replace the passed in reqpage page with our own fake page and
329                  * free up the original page.
330                  */
331                 page = vm_page_getfake(paddr, memattr);
332                 VM_OBJECT_WLOCK(object);
333                 mret = vm_page_replace(page, object, (*mres)->pindex);
334                 KASSERT(mret == *mres,
335                     ("invalid page replacement, old=%p, ret=%p", *mres, mret));
336                 vm_page_lock(mret);
337                 vm_page_free(mret);
338                 vm_page_unlock(mret);
339                 *mres = page;
340         }
341
342         page->valid = VM_PAGE_BITS_ALL;
343
344         return VM_PAGER_OK;
345 }
346
347 static struct cdev_pager_ops contigmem_cdev_pager_ops = {
348         .cdev_pg_ctor = contigmem_cdev_pager_ctor,
349         .cdev_pg_dtor = contigmem_cdev_pager_dtor,
350         .cdev_pg_fault = contigmem_cdev_pager_fault,
351 };
352
353 static int
354 contigmem_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, vm_size_t size,
355                 struct vm_object **obj, int nprot)
356 {
357         struct contigmem_vm_handle *vmh;
358         uint64_t buffer_index;
359
360         /*
361          * The buffer index is encoded in the offset.  Divide the offset by
362          *  PAGE_SIZE to get the index of the buffer requested by the user
363          *  app.
364          */
365         buffer_index = *offset / PAGE_SIZE;
366         if (buffer_index >= contigmem_num_buffers)
367                 return EINVAL;
368
369         if (size > contigmem_buffer_size)
370                 return EINVAL;
371
372         vmh = malloc(sizeof(*vmh), M_CONTIGMEM, M_NOWAIT | M_ZERO);
373         if (vmh == NULL)
374                 return ENOMEM;
375         vmh->buffer_index = buffer_index;
376
377         *offset = (vm_ooffset_t)vtophys(contigmem_buffers[buffer_index].addr);
378         *obj = cdev_pager_allocate(vmh, OBJT_DEVICE, &contigmem_cdev_pager_ops,
379                         size, nprot, *offset, curthread->td_ucred);
380
381         return 0;
382 }