1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
12 #include <sys/kernel.h>
13 #include <sys/malloc.h>
14 #include <sys/module.h>
16 #include <sys/rwlock.h>
17 #include <sys/systm.h>
18 #include <sys/sysctl.h>
20 #include <machine/bus.h>
24 #include <vm/vm_param.h>
25 #include <vm/vm_object.h>
26 #include <vm/vm_page.h>
27 #include <vm/vm_pager.h>
28 #include <vm/vm_phys.h>
30 struct contigmem_buffer {
36 struct contigmem_vm_handle {
40 static int contigmem_load(void);
41 static int contigmem_unload(void);
42 static int contigmem_physaddr(SYSCTL_HANDLER_ARGS);
44 static d_mmap_single_t contigmem_mmap_single;
45 static d_open_t contigmem_open;
46 static d_close_t contigmem_close;
48 static int contigmem_num_buffers = RTE_CONTIGMEM_DEFAULT_NUM_BUFS;
49 static int64_t contigmem_buffer_size = RTE_CONTIGMEM_DEFAULT_BUF_SIZE;
51 static eventhandler_tag contigmem_eh_tag;
52 static struct contigmem_buffer contigmem_buffers[RTE_CONTIGMEM_MAX_NUM_BUFS];
53 static struct cdev *contigmem_cdev = NULL;
54 static int contigmem_refcnt;
56 TUNABLE_INT("hw.contigmem.num_buffers", &contigmem_num_buffers);
57 TUNABLE_QUAD("hw.contigmem.buffer_size", &contigmem_buffer_size);
59 static SYSCTL_NODE(_hw, OID_AUTO, contigmem, CTLFLAG_RD, 0, "contigmem");
61 SYSCTL_INT(_hw_contigmem, OID_AUTO, num_buffers, CTLFLAG_RD,
62 &contigmem_num_buffers, 0, "Number of contigmem buffers allocated");
63 SYSCTL_QUAD(_hw_contigmem, OID_AUTO, buffer_size, CTLFLAG_RD,
64 &contigmem_buffer_size, 0, "Size of each contiguous buffer");
65 SYSCTL_INT(_hw_contigmem, OID_AUTO, num_references, CTLFLAG_RD,
66 &contigmem_refcnt, 0, "Number of references to contigmem");
68 static SYSCTL_NODE(_hw_contigmem, OID_AUTO, physaddr, CTLFLAG_RD, 0,
71 MALLOC_DEFINE(M_CONTIGMEM, "contigmem", "contigmem(4) allocations");
73 static int contigmem_modevent(module_t mod, int type, void *arg)
79 error = contigmem_load();
82 error = contigmem_unload();
91 moduledata_t contigmem_mod = {
93 (modeventhand_t)contigmem_modevent,
97 DECLARE_MODULE(contigmem, contigmem_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
98 MODULE_VERSION(contigmem, 1);
100 static struct cdevsw contigmem_ops = {
101 .d_name = "contigmem",
102 .d_version = D_VERSION,
103 .d_flags = D_TRACKCLOSE,
104 .d_mmap_single = contigmem_mmap_single,
105 .d_open = contigmem_open,
106 .d_close = contigmem_close,
112 char index_string[8], description[32];
116 if (contigmem_num_buffers > RTE_CONTIGMEM_MAX_NUM_BUFS) {
117 printf("%d buffers requested is greater than %d allowed\n",
118 contigmem_num_buffers, RTE_CONTIGMEM_MAX_NUM_BUFS);
123 if (contigmem_buffer_size < PAGE_SIZE ||
124 (contigmem_buffer_size & (contigmem_buffer_size - 1)) != 0) {
125 printf("buffer size 0x%lx is not greater than PAGE_SIZE and "
126 "power of two\n", contigmem_buffer_size);
131 for (i = 0; i < contigmem_num_buffers; i++) {
132 addr = contigmalloc(contigmem_buffer_size, M_CONTIGMEM, M_ZERO,
133 0, BUS_SPACE_MAXADDR, contigmem_buffer_size, 0);
135 printf("contigmalloc failed for buffer %d\n", i);
140 printf("%2u: virt=%p phys=%p\n", i, addr,
141 (void *)pmap_kextract((vm_offset_t)addr));
143 mtx_init(&contigmem_buffers[i].mtx, "contigmem", NULL, MTX_DEF);
144 contigmem_buffers[i].addr = addr;
145 contigmem_buffers[i].refcnt = 0;
147 snprintf(index_string, sizeof(index_string), "%d", i);
148 snprintf(description, sizeof(description),
149 "phys addr for buffer %d", i);
150 SYSCTL_ADD_PROC(NULL,
151 &SYSCTL_NODE_CHILDREN(_hw_contigmem, physaddr), OID_AUTO,
152 index_string, CTLTYPE_U64 | CTLFLAG_RD,
153 (void *)(uintptr_t)i, 0, contigmem_physaddr, "LU",
157 contigmem_cdev = make_dev_credf(0, &contigmem_ops, 0, NULL, UID_ROOT,
158 GID_WHEEL, 0600, "contigmem");
163 for (i = 0; i < contigmem_num_buffers; i++) {
164 if (contigmem_buffers[i].addr != NULL)
165 contigfree(contigmem_buffers[i].addr,
166 contigmem_buffer_size, M_CONTIGMEM);
167 if (mtx_initialized(&contigmem_buffers[i].mtx))
168 mtx_destroy(&contigmem_buffers[i].mtx);
179 if (contigmem_refcnt > 0)
182 if (contigmem_cdev != NULL)
183 destroy_dev(contigmem_cdev);
185 if (contigmem_eh_tag != NULL)
186 EVENTHANDLER_DEREGISTER(process_exit, contigmem_eh_tag);
188 for (i = 0; i < RTE_CONTIGMEM_MAX_NUM_BUFS; i++) {
189 if (contigmem_buffers[i].addr != NULL)
190 contigfree(contigmem_buffers[i].addr,
191 contigmem_buffer_size, M_CONTIGMEM);
192 if (mtx_initialized(&contigmem_buffers[i].mtx))
193 mtx_destroy(&contigmem_buffers[i].mtx);
200 contigmem_physaddr(SYSCTL_HANDLER_ARGS)
203 int index = (int)(uintptr_t)arg1;
205 physaddr = (uint64_t)vtophys(contigmem_buffers[index].addr);
206 return sysctl_handle_64(oidp, &physaddr, 0, req);
210 contigmem_open(struct cdev *cdev, int fflags, int devtype,
214 atomic_add_int(&contigmem_refcnt, 1);
220 contigmem_close(struct cdev *cdev, int fflags, int devtype,
224 atomic_subtract_int(&contigmem_refcnt, 1);
230 contigmem_cdev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
231 vm_ooffset_t foff, struct ucred *cred, u_short *color)
233 struct contigmem_vm_handle *vmh = handle;
234 struct contigmem_buffer *buf;
236 buf = &contigmem_buffers[vmh->buffer_index];
238 atomic_add_int(&contigmem_refcnt, 1);
241 if (buf->refcnt == 0)
242 memset(buf->addr, 0, contigmem_buffer_size);
244 mtx_unlock(&buf->mtx);
250 contigmem_cdev_pager_dtor(void *handle)
252 struct contigmem_vm_handle *vmh = handle;
253 struct contigmem_buffer *buf;
255 buf = &contigmem_buffers[vmh->buffer_index];
259 mtx_unlock(&buf->mtx);
261 free(vmh, M_CONTIGMEM);
263 atomic_subtract_int(&contigmem_refcnt, 1);
267 contigmem_cdev_pager_fault(vm_object_t object, vm_ooffset_t offset, int prot,
271 vm_page_t m_paddr, page;
272 vm_memattr_t memattr, memattr1;
274 memattr = object->memattr;
276 VM_OBJECT_WUNLOCK(object);
280 m_paddr = vm_phys_paddr_to_vm_page(paddr);
281 if (m_paddr != NULL) {
282 memattr1 = pmap_page_get_memattr(m_paddr);
283 if (memattr1 != memattr)
287 if (((*mres)->flags & PG_FICTITIOUS) != 0) {
289 * If the passed in result page is a fake page, update it with
290 * the new physical address.
293 VM_OBJECT_WLOCK(object);
294 vm_page_updatefake(page, paddr, memattr);
298 * Replace the passed in reqpage page with our own fake page and
299 * free up the original page.
301 page = vm_page_getfake(paddr, memattr);
302 VM_OBJECT_WLOCK(object);
303 mret = vm_page_replace(page, object, (*mres)->pindex);
304 KASSERT(mret == *mres,
305 ("invalid page replacement, old=%p, ret=%p", *mres, mret));
308 vm_page_unlock(mret);
312 page->valid = VM_PAGE_BITS_ALL;
317 static struct cdev_pager_ops contigmem_cdev_pager_ops = {
318 .cdev_pg_ctor = contigmem_cdev_pager_ctor,
319 .cdev_pg_dtor = contigmem_cdev_pager_dtor,
320 .cdev_pg_fault = contigmem_cdev_pager_fault,
324 contigmem_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, vm_size_t size,
325 struct vm_object **obj, int nprot)
327 struct contigmem_vm_handle *vmh;
328 uint64_t buffer_index;
331 * The buffer index is encoded in the offset. Divide the offset by
332 * PAGE_SIZE to get the index of the buffer requested by the user
335 buffer_index = *offset / PAGE_SIZE;
336 if (buffer_index >= contigmem_num_buffers)
339 if (size > contigmem_buffer_size)
342 vmh = malloc(sizeof(*vmh), M_CONTIGMEM, M_NOWAIT | M_ZERO);
345 vmh->buffer_index = buffer_index;
347 *offset = (vm_ooffset_t)vtophys(contigmem_buffers[buffer_index].addr);
348 *obj = cdev_pager_allocate(vmh, OBJT_DEVICE, &contigmem_cdev_pager_ops,
349 size, nprot, *offset, curthread->td_ucred);