4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
45 #include <sys/rwlock.h>
46 #include <sys/systm.h>
47 #include <sys/sysctl.h>
48 #include <sys/vmmeter.h>
50 #include <machine/bus.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>
60 struct contigmem_buffer {
66 struct contigmem_vm_handle {
70 static int contigmem_load(void);
71 static int contigmem_unload(void);
72 static int contigmem_physaddr(SYSCTL_HANDLER_ARGS);
74 static d_mmap_single_t contigmem_mmap_single;
75 static d_open_t contigmem_open;
76 static d_close_t contigmem_close;
78 static int contigmem_num_buffers = RTE_CONTIGMEM_DEFAULT_NUM_BUFS;
79 static int64_t contigmem_buffer_size = RTE_CONTIGMEM_DEFAULT_BUF_SIZE;
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;
86 TUNABLE_INT("hw.contigmem.num_buffers", &contigmem_num_buffers);
87 TUNABLE_QUAD("hw.contigmem.buffer_size", &contigmem_buffer_size);
89 static SYSCTL_NODE(_hw, OID_AUTO, contigmem, CTLFLAG_RD, 0, "contigmem");
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");
98 static SYSCTL_NODE(_hw_contigmem, OID_AUTO, physaddr, CTLFLAG_RD, 0,
101 MALLOC_DEFINE(M_CONTIGMEM, "contigmem", "contigmem(4) allocations");
103 static int contigmem_modevent(module_t mod, int type, void *arg)
109 error = contigmem_load();
112 error = contigmem_unload();
121 moduledata_t contigmem_mod = {
123 (modeventhand_t)contigmem_modevent,
127 DECLARE_MODULE(contigmem, contigmem_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
128 MODULE_VERSION(contigmem, 1);
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,
142 char index_string[8], description[32];
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);
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);
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);
165 printf("contigmalloc failed for buffer %d\n", i);
170 printf("%2u: virt=%p phys=%p\n", i, addr,
171 (void *)pmap_kextract((vm_offset_t)addr));
173 mtx_init(&contigmem_buffers[i].mtx, "contigmem", NULL, MTX_DEF);
174 contigmem_buffers[i].addr = addr;
175 contigmem_buffers[i].refcnt = 0;
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",
187 contigmem_cdev = make_dev_credf(0, &contigmem_ops, 0, NULL, UID_ROOT,
188 GID_WHEEL, 0600, "contigmem");
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);
209 if (contigmem_refcnt > 0)
212 if (contigmem_cdev != NULL)
213 destroy_dev(contigmem_cdev);
215 if (contigmem_eh_tag != NULL)
216 EVENTHANDLER_DEREGISTER(process_exit, contigmem_eh_tag);
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);
230 contigmem_physaddr(SYSCTL_HANDLER_ARGS)
233 int index = (int)(uintptr_t)arg1;
235 physaddr = (uint64_t)vtophys(contigmem_buffers[index].addr);
236 return sysctl_handle_64(oidp, &physaddr, 0, req);
240 contigmem_open(struct cdev *cdev, int fflags, int devtype,
244 atomic_add_int(&contigmem_refcnt, 1);
250 contigmem_close(struct cdev *cdev, int fflags, int devtype,
254 atomic_subtract_int(&contigmem_refcnt, 1);
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)
263 struct contigmem_vm_handle *vmh = handle;
264 struct contigmem_buffer *buf;
266 buf = &contigmem_buffers[vmh->buffer_index];
268 atomic_add_int(&contigmem_refcnt, 1);
271 if (buf->refcnt == 0)
272 memset(buf->addr, 0, contigmem_buffer_size);
274 mtx_unlock(&buf->mtx);
280 contigmem_cdev_pager_dtor(void *handle)
282 struct contigmem_vm_handle *vmh = handle;
283 struct contigmem_buffer *buf;
285 buf = &contigmem_buffers[vmh->buffer_index];
289 mtx_unlock(&buf->mtx);
291 free(vmh, M_CONTIGMEM);
293 atomic_subtract_int(&contigmem_refcnt, 1);
297 contigmem_cdev_pager_fault(vm_object_t object, vm_ooffset_t offset, int prot,
301 vm_page_t m_paddr, page;
302 vm_memattr_t memattr, memattr1;
304 memattr = object->memattr;
306 VM_OBJECT_WUNLOCK(object);
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)
317 if (((*mres)->flags & PG_FICTITIOUS) != 0) {
319 * If the passed in result page is a fake page, update it with
320 * the new physical address.
323 VM_OBJECT_WLOCK(object);
324 vm_page_updatefake(page, paddr, memattr);
328 * Replace the passed in reqpage page with our own fake page and
329 * free up the original page.
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));
338 vm_page_unlock(mret);
342 page->valid = VM_PAGE_BITS_ALL;
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,
354 contigmem_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, vm_size_t size,
355 struct vm_object **obj, int nprot)
357 struct contigmem_vm_handle *vmh;
358 uint64_t buffer_index;
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
365 buffer_index = *offset / PAGE_SIZE;
366 if (buffer_index >= contigmem_num_buffers)
369 if (size > contigmem_buffer_size)
372 vmh = malloc(sizeof(*vmh), M_CONTIGMEM, M_NOWAIT | M_ZERO);
375 vmh->buffer_index = buffer_index;
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);