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