vfio: expose functions
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_interrupts.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 <stdio.h>
35 #include <stdint.h>
36 #include <stdlib.h>
37 #include <pthread.h>
38 #include <sys/queue.h>
39 #include <stdarg.h>
40 #include <unistd.h>
41 #include <string.h>
42 #include <errno.h>
43 #include <inttypes.h>
44 #include <sys/epoll.h>
45 #include <sys/signalfd.h>
46 #include <sys/ioctl.h>
47 #include <sys/eventfd.h>
48 #include <assert.h>
49 #include <stdbool.h>
50
51 #include <rte_common.h>
52 #include <rte_interrupts.h>
53 #include <rte_memory.h>
54 #include <rte_memzone.h>
55 #include <rte_launch.h>
56 #include <rte_eal.h>
57 #include <rte_per_lcore.h>
58 #include <rte_lcore.h>
59 #include <rte_atomic.h>
60 #include <rte_branch_prediction.h>
61 #include <rte_debug.h>
62 #include <rte_log.h>
63 #include <rte_pci.h>
64 #include <rte_malloc.h>
65 #include <rte_errno.h>
66 #include <rte_spinlock.h>
67 #include <rte_pause.h>
68 #include <rte_vfio.h>
69
70 #include "eal_private.h"
71 #include "eal_vfio.h"
72 #include "eal_thread.h"
73
74 #define EAL_INTR_EPOLL_WAIT_FOREVER (-1)
75 #define NB_OTHER_INTR               1
76
77 static RTE_DEFINE_PER_LCORE(int, _epfd) = -1; /**< epoll fd per thread */
78
79 /**
80  * union for pipe fds.
81  */
82 union intr_pipefds{
83         struct {
84                 int pipefd[2];
85         };
86         struct {
87                 int readfd;
88                 int writefd;
89         };
90 };
91
92 /**
93  * union buffer for reading on different devices
94  */
95 union rte_intr_read_buffer {
96         int uio_intr_count;              /* for uio device */
97 #ifdef VFIO_PRESENT
98         uint64_t vfio_intr_count;        /* for vfio device */
99 #endif
100         uint64_t timerfd_num;            /* for timerfd */
101         char charbuf[16];                /* for others */
102 };
103
104 TAILQ_HEAD(rte_intr_cb_list, rte_intr_callback);
105 TAILQ_HEAD(rte_intr_source_list, rte_intr_source);
106
107 struct rte_intr_callback {
108         TAILQ_ENTRY(rte_intr_callback) next;
109         rte_intr_callback_fn cb_fn;  /**< callback address */
110         void *cb_arg;                /**< parameter for callback */
111 };
112
113 struct rte_intr_source {
114         TAILQ_ENTRY(rte_intr_source) next;
115         struct rte_intr_handle intr_handle; /**< interrupt handle */
116         struct rte_intr_cb_list callbacks;  /**< user callbacks */
117         uint32_t active;
118 };
119
120 /* global spinlock for interrupt data operation */
121 static rte_spinlock_t intr_lock = RTE_SPINLOCK_INITIALIZER;
122
123 /* union buffer for pipe read/write */
124 static union intr_pipefds intr_pipe;
125
126 /* interrupt sources list */
127 static struct rte_intr_source_list intr_sources;
128
129 /* interrupt handling thread */
130 static pthread_t intr_thread;
131
132 /* VFIO interrupts */
133 #ifdef VFIO_PRESENT
134
135 #define IRQ_SET_BUF_LEN  (sizeof(struct vfio_irq_set) + sizeof(int))
136 /* irq set buffer length for queue interrupts and LSC interrupt */
137 #define MSIX_IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + \
138                               sizeof(int) * (RTE_MAX_RXTX_INTR_VEC_ID + 1))
139
140 /* enable legacy (INTx) interrupts */
141 static int
142 vfio_enable_intx(const struct rte_intr_handle *intr_handle) {
143         struct vfio_irq_set *irq_set;
144         char irq_set_buf[IRQ_SET_BUF_LEN];
145         int len, ret;
146         int *fd_ptr;
147
148         len = sizeof(irq_set_buf);
149
150         /* enable INTx */
151         irq_set = (struct vfio_irq_set *) irq_set_buf;
152         irq_set->argsz = len;
153         irq_set->count = 1;
154         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
155         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
156         irq_set->start = 0;
157         fd_ptr = (int *) &irq_set->data;
158         *fd_ptr = intr_handle->fd;
159
160         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
161
162         if (ret) {
163                 RTE_LOG(ERR, EAL, "Error enabling INTx interrupts for fd %d\n",
164                                                 intr_handle->fd);
165                 return -1;
166         }
167
168         /* unmask INTx after enabling */
169         memset(irq_set, 0, len);
170         len = sizeof(struct vfio_irq_set);
171         irq_set->argsz = len;
172         irq_set->count = 1;
173         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
174         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
175         irq_set->start = 0;
176
177         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
178
179         if (ret) {
180                 RTE_LOG(ERR, EAL, "Error unmasking INTx interrupts for fd %d\n",
181                                                 intr_handle->fd);
182                 return -1;
183         }
184         return 0;
185 }
186
187 /* disable legacy (INTx) interrupts */
188 static int
189 vfio_disable_intx(const struct rte_intr_handle *intr_handle) {
190         struct vfio_irq_set *irq_set;
191         char irq_set_buf[IRQ_SET_BUF_LEN];
192         int len, ret;
193
194         len = sizeof(struct vfio_irq_set);
195
196         /* mask interrupts before disabling */
197         irq_set = (struct vfio_irq_set *) irq_set_buf;
198         irq_set->argsz = len;
199         irq_set->count = 1;
200         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_MASK;
201         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
202         irq_set->start = 0;
203
204         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
205
206         if (ret) {
207                 RTE_LOG(ERR, EAL, "Error masking INTx interrupts for fd %d\n",
208                                                 intr_handle->fd);
209                 return -1;
210         }
211
212         /* disable INTx*/
213         memset(irq_set, 0, len);
214         irq_set->argsz = len;
215         irq_set->count = 0;
216         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
217         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
218         irq_set->start = 0;
219
220         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
221
222         if (ret) {
223                 RTE_LOG(ERR, EAL,
224                         "Error disabling INTx interrupts for fd %d\n", intr_handle->fd);
225                 return -1;
226         }
227         return 0;
228 }
229
230 /* enable MSI interrupts */
231 static int
232 vfio_enable_msi(const struct rte_intr_handle *intr_handle) {
233         int len, ret;
234         char irq_set_buf[IRQ_SET_BUF_LEN];
235         struct vfio_irq_set *irq_set;
236         int *fd_ptr;
237
238         len = sizeof(irq_set_buf);
239
240         irq_set = (struct vfio_irq_set *) irq_set_buf;
241         irq_set->argsz = len;
242         irq_set->count = 1;
243         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
244         irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
245         irq_set->start = 0;
246         fd_ptr = (int *) &irq_set->data;
247         *fd_ptr = intr_handle->fd;
248
249         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
250
251         if (ret) {
252                 RTE_LOG(ERR, EAL, "Error enabling MSI interrupts for fd %d\n",
253                                                 intr_handle->fd);
254                 return -1;
255         }
256         return 0;
257 }
258
259 /* disable MSI interrupts */
260 static int
261 vfio_disable_msi(const struct rte_intr_handle *intr_handle) {
262         struct vfio_irq_set *irq_set;
263         char irq_set_buf[IRQ_SET_BUF_LEN];
264         int len, ret;
265
266         len = sizeof(struct vfio_irq_set);
267
268         irq_set = (struct vfio_irq_set *) irq_set_buf;
269         irq_set->argsz = len;
270         irq_set->count = 0;
271         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
272         irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
273         irq_set->start = 0;
274
275         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
276
277         if (ret)
278                 RTE_LOG(ERR, EAL,
279                         "Error disabling MSI interrupts for fd %d\n", intr_handle->fd);
280
281         return ret;
282 }
283
284 /* enable MSI-X interrupts */
285 static int
286 vfio_enable_msix(const struct rte_intr_handle *intr_handle) {
287         int len, ret;
288         char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
289         struct vfio_irq_set *irq_set;
290         int *fd_ptr;
291
292         len = sizeof(irq_set_buf);
293
294         irq_set = (struct vfio_irq_set *) irq_set_buf;
295         irq_set->argsz = len;
296         /* 0 < irq_set->count < RTE_MAX_RXTX_INTR_VEC_ID + 1 */
297         irq_set->count = intr_handle->max_intr ?
298                 (intr_handle->max_intr > RTE_MAX_RXTX_INTR_VEC_ID + 1 ?
299                 RTE_MAX_RXTX_INTR_VEC_ID + 1 : intr_handle->max_intr) : 1;
300         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
301         irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
302         irq_set->start = 0;
303         fd_ptr = (int *) &irq_set->data;
304         /* INTR vector offset 0 reserve for non-efds mapping */
305         fd_ptr[RTE_INTR_VEC_ZERO_OFFSET] = intr_handle->fd;
306         memcpy(&fd_ptr[RTE_INTR_VEC_RXTX_OFFSET], intr_handle->efds,
307                 sizeof(*intr_handle->efds) * intr_handle->nb_efd);
308
309         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
310
311         if (ret) {
312                 RTE_LOG(ERR, EAL, "Error enabling MSI-X interrupts for fd %d\n",
313                                                 intr_handle->fd);
314                 return -1;
315         }
316
317         return 0;
318 }
319
320 /* disable MSI-X interrupts */
321 static int
322 vfio_disable_msix(const struct rte_intr_handle *intr_handle) {
323         struct vfio_irq_set *irq_set;
324         char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
325         int len, ret;
326
327         len = sizeof(struct vfio_irq_set);
328
329         irq_set = (struct vfio_irq_set *) irq_set_buf;
330         irq_set->argsz = len;
331         irq_set->count = 0;
332         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
333         irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
334         irq_set->start = 0;
335
336         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
337
338         if (ret)
339                 RTE_LOG(ERR, EAL,
340                         "Error disabling MSI-X interrupts for fd %d\n", intr_handle->fd);
341
342         return ret;
343 }
344 #endif
345
346 static int
347 uio_intx_intr_disable(const struct rte_intr_handle *intr_handle)
348 {
349         unsigned char command_high;
350
351         /* use UIO config file descriptor for uio_pci_generic */
352         if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
353                 RTE_LOG(ERR, EAL,
354                         "Error reading interrupts status for fd %d\n",
355                         intr_handle->uio_cfg_fd);
356                 return -1;
357         }
358         /* disable interrupts */
359         command_high |= 0x4;
360         if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
361                 RTE_LOG(ERR, EAL,
362                         "Error disabling interrupts for fd %d\n",
363                         intr_handle->uio_cfg_fd);
364                 return -1;
365         }
366
367         return 0;
368 }
369
370 static int
371 uio_intx_intr_enable(const struct rte_intr_handle *intr_handle)
372 {
373         unsigned char command_high;
374
375         /* use UIO config file descriptor for uio_pci_generic */
376         if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
377                 RTE_LOG(ERR, EAL,
378                         "Error reading interrupts status for fd %d\n",
379                         intr_handle->uio_cfg_fd);
380                 return -1;
381         }
382         /* enable interrupts */
383         command_high &= ~0x4;
384         if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
385                 RTE_LOG(ERR, EAL,
386                         "Error enabling interrupts for fd %d\n",
387                         intr_handle->uio_cfg_fd);
388                 return -1;
389         }
390
391         return 0;
392 }
393
394 static int
395 uio_intr_disable(const struct rte_intr_handle *intr_handle)
396 {
397         const int value = 0;
398
399         if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
400                 RTE_LOG(ERR, EAL,
401                         "Error disabling interrupts for fd %d (%s)\n",
402                         intr_handle->fd, strerror(errno));
403                 return -1;
404         }
405         return 0;
406 }
407
408 static int
409 uio_intr_enable(const struct rte_intr_handle *intr_handle)
410 {
411         const int value = 1;
412
413         if (write(intr_handle->fd, &value, sizeof(value)) < 0) {
414                 RTE_LOG(ERR, EAL,
415                         "Error enabling interrupts for fd %d (%s)\n",
416                         intr_handle->fd, strerror(errno));
417                 return -1;
418         }
419         return 0;
420 }
421
422 int
423 rte_intr_callback_register(const struct rte_intr_handle *intr_handle,
424                         rte_intr_callback_fn cb, void *cb_arg)
425 {
426         int ret, wake_thread;
427         struct rte_intr_source *src;
428         struct rte_intr_callback *callback;
429
430         wake_thread = 0;
431
432         /* first do parameter checking */
433         if (intr_handle == NULL || intr_handle->fd < 0 || cb == NULL) {
434                 RTE_LOG(ERR, EAL,
435                         "Registering with invalid input parameter\n");
436                 return -EINVAL;
437         }
438
439         /* allocate a new interrupt callback entity */
440         callback = rte_zmalloc("interrupt callback list",
441                                 sizeof(*callback), 0);
442         if (callback == NULL) {
443                 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
444                 return -ENOMEM;
445         }
446         callback->cb_fn = cb;
447         callback->cb_arg = cb_arg;
448
449         rte_spinlock_lock(&intr_lock);
450
451         /* check if there is at least one callback registered for the fd */
452         TAILQ_FOREACH(src, &intr_sources, next) {
453                 if (src->intr_handle.fd == intr_handle->fd) {
454                         /* we had no interrupts for this */
455                         if TAILQ_EMPTY(&src->callbacks)
456                                 wake_thread = 1;
457
458                         TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
459                         ret = 0;
460                         break;
461                 }
462         }
463
464         /* no existing callbacks for this - add new source */
465         if (src == NULL) {
466                 if ((src = rte_zmalloc("interrupt source list",
467                                 sizeof(*src), 0)) == NULL) {
468                         RTE_LOG(ERR, EAL, "Can not allocate memory\n");
469                         rte_free(callback);
470                         ret = -ENOMEM;
471                 } else {
472                         src->intr_handle = *intr_handle;
473                         TAILQ_INIT(&src->callbacks);
474                         TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
475                         TAILQ_INSERT_TAIL(&intr_sources, src, next);
476                         wake_thread = 1;
477                         ret = 0;
478                 }
479         }
480
481         rte_spinlock_unlock(&intr_lock);
482
483         /**
484          * check if need to notify the pipe fd waited by epoll_wait to
485          * rebuild the wait list.
486          */
487         if (wake_thread)
488                 if (write(intr_pipe.writefd, "1", 1) < 0)
489                         return -EPIPE;
490
491         return ret;
492 }
493
494 int
495 rte_intr_callback_unregister(const struct rte_intr_handle *intr_handle,
496                         rte_intr_callback_fn cb_fn, void *cb_arg)
497 {
498         int ret;
499         struct rte_intr_source *src;
500         struct rte_intr_callback *cb, *next;
501
502         /* do parameter checking first */
503         if (intr_handle == NULL || intr_handle->fd < 0) {
504                 RTE_LOG(ERR, EAL,
505                 "Unregistering with invalid input parameter\n");
506                 return -EINVAL;
507         }
508
509         rte_spinlock_lock(&intr_lock);
510
511         /* check if the insterrupt source for the fd is existent */
512         TAILQ_FOREACH(src, &intr_sources, next)
513                 if (src->intr_handle.fd == intr_handle->fd)
514                         break;
515
516         /* No interrupt source registered for the fd */
517         if (src == NULL) {
518                 ret = -ENOENT;
519
520         /* interrupt source has some active callbacks right now. */
521         } else if (src->active != 0) {
522                 ret = -EAGAIN;
523
524         /* ok to remove. */
525         } else {
526                 ret = 0;
527
528                 /*walk through the callbacks and remove all that match. */
529                 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
530
531                         next = TAILQ_NEXT(cb, next);
532
533                         if (cb->cb_fn == cb_fn && (cb_arg == (void *)-1 ||
534                                         cb->cb_arg == cb_arg)) {
535                                 TAILQ_REMOVE(&src->callbacks, cb, next);
536                                 rte_free(cb);
537                                 ret++;
538                         }
539                 }
540
541                 /* all callbacks for that source are removed. */
542                 if (TAILQ_EMPTY(&src->callbacks)) {
543                         TAILQ_REMOVE(&intr_sources, src, next);
544                         rte_free(src);
545                 }
546         }
547
548         rte_spinlock_unlock(&intr_lock);
549
550         /* notify the pipe fd waited by epoll_wait to rebuild the wait list */
551         if (ret >= 0 && write(intr_pipe.writefd, "1", 1) < 0) {
552                 ret = -EPIPE;
553         }
554
555         return ret;
556 }
557
558 int
559 rte_intr_enable(const struct rte_intr_handle *intr_handle)
560 {
561         if (intr_handle && intr_handle->type == RTE_INTR_HANDLE_VDEV)
562                 return 0;
563
564         if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
565                 return -1;
566
567         switch (intr_handle->type){
568         /* write to the uio fd to enable the interrupt */
569         case RTE_INTR_HANDLE_UIO:
570                 if (uio_intr_enable(intr_handle))
571                         return -1;
572                 break;
573         case RTE_INTR_HANDLE_UIO_INTX:
574                 if (uio_intx_intr_enable(intr_handle))
575                         return -1;
576                 break;
577         /* not used at this moment */
578         case RTE_INTR_HANDLE_ALARM:
579                 return -1;
580 #ifdef VFIO_PRESENT
581         case RTE_INTR_HANDLE_VFIO_MSIX:
582                 if (vfio_enable_msix(intr_handle))
583                         return -1;
584                 break;
585         case RTE_INTR_HANDLE_VFIO_MSI:
586                 if (vfio_enable_msi(intr_handle))
587                         return -1;
588                 break;
589         case RTE_INTR_HANDLE_VFIO_LEGACY:
590                 if (vfio_enable_intx(intr_handle))
591                         return -1;
592                 break;
593 #endif
594         /* unknown handle type */
595         default:
596                 RTE_LOG(ERR, EAL,
597                         "Unknown handle type of fd %d\n",
598                                         intr_handle->fd);
599                 return -1;
600         }
601
602         return 0;
603 }
604
605 int
606 rte_intr_disable(const struct rte_intr_handle *intr_handle)
607 {
608         if (intr_handle && intr_handle->type == RTE_INTR_HANDLE_VDEV)
609                 return 0;
610
611         if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
612                 return -1;
613
614         switch (intr_handle->type){
615         /* write to the uio fd to disable the interrupt */
616         case RTE_INTR_HANDLE_UIO:
617                 if (uio_intr_disable(intr_handle))
618                         return -1;
619                 break;
620         case RTE_INTR_HANDLE_UIO_INTX:
621                 if (uio_intx_intr_disable(intr_handle))
622                         return -1;
623                 break;
624         /* not used at this moment */
625         case RTE_INTR_HANDLE_ALARM:
626                 return -1;
627 #ifdef VFIO_PRESENT
628         case RTE_INTR_HANDLE_VFIO_MSIX:
629                 if (vfio_disable_msix(intr_handle))
630                         return -1;
631                 break;
632         case RTE_INTR_HANDLE_VFIO_MSI:
633                 if (vfio_disable_msi(intr_handle))
634                         return -1;
635                 break;
636         case RTE_INTR_HANDLE_VFIO_LEGACY:
637                 if (vfio_disable_intx(intr_handle))
638                         return -1;
639                 break;
640 #endif
641         /* unknown handle type */
642         default:
643                 RTE_LOG(ERR, EAL,
644                         "Unknown handle type of fd %d\n",
645                                         intr_handle->fd);
646                 return -1;
647         }
648
649         return 0;
650 }
651
652 static int
653 eal_intr_process_interrupts(struct epoll_event *events, int nfds)
654 {
655         bool call = false;
656         int n, bytes_read;
657         struct rte_intr_source *src;
658         struct rte_intr_callback *cb;
659         union rte_intr_read_buffer buf;
660         struct rte_intr_callback active_cb;
661
662         for (n = 0; n < nfds; n++) {
663
664                 /**
665                  * if the pipe fd is ready to read, return out to
666                  * rebuild the wait list.
667                  */
668                 if (events[n].data.fd == intr_pipe.readfd){
669                         int r = read(intr_pipe.readfd, buf.charbuf,
670                                         sizeof(buf.charbuf));
671                         RTE_SET_USED(r);
672                         return -1;
673                 }
674                 rte_spinlock_lock(&intr_lock);
675                 TAILQ_FOREACH(src, &intr_sources, next)
676                         if (src->intr_handle.fd ==
677                                         events[n].data.fd)
678                                 break;
679                 if (src == NULL){
680                         rte_spinlock_unlock(&intr_lock);
681                         continue;
682                 }
683
684                 /* mark this interrupt source as active and release the lock. */
685                 src->active = 1;
686                 rte_spinlock_unlock(&intr_lock);
687
688                 /* set the length to be read dor different handle type */
689                 switch (src->intr_handle.type) {
690                 case RTE_INTR_HANDLE_UIO:
691                 case RTE_INTR_HANDLE_UIO_INTX:
692                         bytes_read = sizeof(buf.uio_intr_count);
693                         break;
694                 case RTE_INTR_HANDLE_ALARM:
695                         bytes_read = sizeof(buf.timerfd_num);
696                         break;
697 #ifdef VFIO_PRESENT
698                 case RTE_INTR_HANDLE_VFIO_MSIX:
699                 case RTE_INTR_HANDLE_VFIO_MSI:
700                 case RTE_INTR_HANDLE_VFIO_LEGACY:
701                         bytes_read = sizeof(buf.vfio_intr_count);
702                         break;
703 #endif
704                 case RTE_INTR_HANDLE_VDEV:
705                 case RTE_INTR_HANDLE_EXT:
706                         bytes_read = 0;
707                         call = true;
708                         break;
709
710                 default:
711                         bytes_read = 1;
712                         break;
713                 }
714
715                 if (bytes_read > 0) {
716                         /**
717                          * read out to clear the ready-to-be-read flag
718                          * for epoll_wait.
719                          */
720                         bytes_read = read(events[n].data.fd, &buf, bytes_read);
721                         if (bytes_read < 0) {
722                                 if (errno == EINTR || errno == EWOULDBLOCK)
723                                         continue;
724
725                                 RTE_LOG(ERR, EAL, "Error reading from file "
726                                         "descriptor %d: %s\n",
727                                         events[n].data.fd,
728                                         strerror(errno));
729                         } else if (bytes_read == 0)
730                                 RTE_LOG(ERR, EAL, "Read nothing from file "
731                                         "descriptor %d\n", events[n].data.fd);
732                         else
733                                 call = true;
734                 }
735
736                 /* grab a lock, again to call callbacks and update status. */
737                 rte_spinlock_lock(&intr_lock);
738
739                 if (call) {
740
741                         /* Finally, call all callbacks. */
742                         TAILQ_FOREACH(cb, &src->callbacks, next) {
743
744                                 /* make a copy and unlock. */
745                                 active_cb = *cb;
746                                 rte_spinlock_unlock(&intr_lock);
747
748                                 /* call the actual callback */
749                                 active_cb.cb_fn(active_cb.cb_arg);
750
751                                 /*get the lock back. */
752                                 rte_spinlock_lock(&intr_lock);
753                         }
754                 }
755
756                 /* we done with that interrupt source, release it. */
757                 src->active = 0;
758                 rte_spinlock_unlock(&intr_lock);
759         }
760
761         return 0;
762 }
763
764 /**
765  * It handles all the interrupts.
766  *
767  * @param pfd
768  *  epoll file descriptor.
769  * @param totalfds
770  *  The number of file descriptors added in epoll.
771  *
772  * @return
773  *  void
774  */
775 static void
776 eal_intr_handle_interrupts(int pfd, unsigned totalfds)
777 {
778         struct epoll_event events[totalfds];
779         int nfds = 0;
780
781         for(;;) {
782                 nfds = epoll_wait(pfd, events, totalfds,
783                         EAL_INTR_EPOLL_WAIT_FOREVER);
784                 /* epoll_wait fail */
785                 if (nfds < 0) {
786                         if (errno == EINTR)
787                                 continue;
788                         RTE_LOG(ERR, EAL,
789                                 "epoll_wait returns with fail\n");
790                         return;
791                 }
792                 /* epoll_wait timeout, will never happens here */
793                 else if (nfds == 0)
794                         continue;
795                 /* epoll_wait has at least one fd ready to read */
796                 if (eal_intr_process_interrupts(events, nfds) < 0)
797                         return;
798         }
799 }
800
801 /**
802  * It builds/rebuilds up the epoll file descriptor with all the
803  * file descriptors being waited on. Then handles the interrupts.
804  *
805  * @param arg
806  *  pointer. (unused)
807  *
808  * @return
809  *  never return;
810  */
811 static __attribute__((noreturn)) void *
812 eal_intr_thread_main(__rte_unused void *arg)
813 {
814         struct epoll_event ev;
815
816         /* host thread, never break out */
817         for (;;) {
818                 /* build up the epoll fd with all descriptors we are to
819                  * wait on then pass it to the handle_interrupts function
820                  */
821                 static struct epoll_event pipe_event = {
822                         .events = EPOLLIN | EPOLLPRI,
823                 };
824                 struct rte_intr_source *src;
825                 unsigned numfds = 0;
826
827                 /* create epoll fd */
828                 int pfd = epoll_create(1);
829                 if (pfd < 0)
830                         rte_panic("Cannot create epoll instance\n");
831
832                 pipe_event.data.fd = intr_pipe.readfd;
833                 /**
834                  * add pipe fd into wait list, this pipe is used to
835                  * rebuild the wait list.
836                  */
837                 if (epoll_ctl(pfd, EPOLL_CTL_ADD, intr_pipe.readfd,
838                                                 &pipe_event) < 0) {
839                         rte_panic("Error adding fd to %d epoll_ctl, %s\n",
840                                         intr_pipe.readfd, strerror(errno));
841                 }
842                 numfds++;
843
844                 rte_spinlock_lock(&intr_lock);
845
846                 TAILQ_FOREACH(src, &intr_sources, next) {
847                         if (src->callbacks.tqh_first == NULL)
848                                 continue; /* skip those with no callbacks */
849                         ev.events = EPOLLIN | EPOLLPRI | EPOLLRDHUP | EPOLLHUP;
850                         ev.data.fd = src->intr_handle.fd;
851
852                         /**
853                          * add all the uio device file descriptor
854                          * into wait list.
855                          */
856                         if (epoll_ctl(pfd, EPOLL_CTL_ADD,
857                                         src->intr_handle.fd, &ev) < 0){
858                                 rte_panic("Error adding fd %d epoll_ctl, %s\n",
859                                         src->intr_handle.fd, strerror(errno));
860                         }
861                         else
862                                 numfds++;
863                 }
864                 rte_spinlock_unlock(&intr_lock);
865                 /* serve the interrupt */
866                 eal_intr_handle_interrupts(pfd, numfds);
867
868                 /**
869                  * when we return, we need to rebuild the
870                  * list of fds to monitor.
871                  */
872                 close(pfd);
873         }
874 }
875
876 int
877 rte_eal_intr_init(void)
878 {
879         int ret = 0, ret_1 = 0;
880         char thread_name[RTE_MAX_THREAD_NAME_LEN];
881
882         /* init the global interrupt source head */
883         TAILQ_INIT(&intr_sources);
884
885         /**
886          * create a pipe which will be waited by epoll and notified to
887          * rebuild the wait list of epoll.
888          */
889         if (pipe(intr_pipe.pipefd) < 0) {
890                 rte_errno = errno;
891                 return -1;
892         }
893
894         /* create the host thread to wait/handle the interrupt */
895         ret = pthread_create(&intr_thread, NULL,
896                         eal_intr_thread_main, NULL);
897         if (ret != 0) {
898                 rte_errno = ret;
899                 RTE_LOG(ERR, EAL,
900                         "Failed to create thread for interrupt handling\n");
901         } else {
902                 /* Set thread_name for aid in debugging. */
903                 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN,
904                         "eal-intr-thread");
905                 ret_1 = rte_thread_setname(intr_thread, thread_name);
906                 if (ret_1 != 0)
907                         RTE_LOG(DEBUG, EAL,
908                         "Failed to set thread name for interrupt handling\n");
909         }
910
911         return -ret;
912 }
913
914 static void
915 eal_intr_proc_rxtx_intr(int fd, const struct rte_intr_handle *intr_handle)
916 {
917         union rte_intr_read_buffer buf;
918         int bytes_read = 1;
919         int nbytes;
920
921         switch (intr_handle->type) {
922         case RTE_INTR_HANDLE_UIO:
923         case RTE_INTR_HANDLE_UIO_INTX:
924                 bytes_read = sizeof(buf.uio_intr_count);
925                 break;
926 #ifdef VFIO_PRESENT
927         case RTE_INTR_HANDLE_VFIO_MSIX:
928         case RTE_INTR_HANDLE_VFIO_MSI:
929         case RTE_INTR_HANDLE_VFIO_LEGACY:
930                 bytes_read = sizeof(buf.vfio_intr_count);
931                 break;
932 #endif
933         case RTE_INTR_HANDLE_VDEV:
934                 /* for vdev, fd points to:
935                  * a. eventfd which does not need to read out;
936                  * b. datapath fd which needs PMD to read out.
937                  */
938                 return;
939         case RTE_INTR_HANDLE_EXT:
940                 return;
941         default:
942                 bytes_read = 1;
943                 RTE_LOG(INFO, EAL, "unexpected intr type\n");
944                 break;
945         }
946
947         /**
948          * read out to clear the ready-to-be-read flag
949          * for epoll_wait.
950          */
951         do {
952                 nbytes = read(fd, &buf, bytes_read);
953                 if (nbytes < 0) {
954                         if (errno == EINTR || errno == EWOULDBLOCK ||
955                             errno == EAGAIN)
956                                 continue;
957                         RTE_LOG(ERR, EAL,
958                                 "Error reading from fd %d: %s\n",
959                                 fd, strerror(errno));
960                 } else if (nbytes == 0)
961                         RTE_LOG(ERR, EAL, "Read nothing from fd %d\n", fd);
962                 return;
963         } while (1);
964 }
965
966 static int
967 eal_epoll_process_event(struct epoll_event *evs, unsigned int n,
968                         struct rte_epoll_event *events)
969 {
970         unsigned int i, count = 0;
971         struct rte_epoll_event *rev;
972
973         for (i = 0; i < n; i++) {
974                 rev = evs[i].data.ptr;
975                 if (!rev || !rte_atomic32_cmpset(&rev->status, RTE_EPOLL_VALID,
976                                                  RTE_EPOLL_EXEC))
977                         continue;
978
979                 events[count].status        = RTE_EPOLL_VALID;
980                 events[count].fd            = rev->fd;
981                 events[count].epfd          = rev->epfd;
982                 events[count].epdata.event  = rev->epdata.event;
983                 events[count].epdata.data   = rev->epdata.data;
984                 if (rev->epdata.cb_fun)
985                         rev->epdata.cb_fun(rev->fd,
986                                            rev->epdata.cb_arg);
987
988                 rte_compiler_barrier();
989                 rev->status = RTE_EPOLL_VALID;
990                 count++;
991         }
992         return count;
993 }
994
995 static inline int
996 eal_init_tls_epfd(void)
997 {
998         int pfd = epoll_create(255);
999
1000         if (pfd < 0) {
1001                 RTE_LOG(ERR, EAL,
1002                         "Cannot create epoll instance\n");
1003                 return -1;
1004         }
1005         return pfd;
1006 }
1007
1008 int
1009 rte_intr_tls_epfd(void)
1010 {
1011         if (RTE_PER_LCORE(_epfd) == -1)
1012                 RTE_PER_LCORE(_epfd) = eal_init_tls_epfd();
1013
1014         return RTE_PER_LCORE(_epfd);
1015 }
1016
1017 int
1018 rte_epoll_wait(int epfd, struct rte_epoll_event *events,
1019                int maxevents, int timeout)
1020 {
1021         struct epoll_event evs[maxevents];
1022         int rc;
1023
1024         if (!events) {
1025                 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1026                 return -1;
1027         }
1028
1029         /* using per thread epoll fd */
1030         if (epfd == RTE_EPOLL_PER_THREAD)
1031                 epfd = rte_intr_tls_epfd();
1032
1033         while (1) {
1034                 rc = epoll_wait(epfd, evs, maxevents, timeout);
1035                 if (likely(rc > 0)) {
1036                         /* epoll_wait has at least one fd ready to read */
1037                         rc = eal_epoll_process_event(evs, rc, events);
1038                         break;
1039                 } else if (rc < 0) {
1040                         if (errno == EINTR)
1041                                 continue;
1042                         /* epoll_wait fail */
1043                         RTE_LOG(ERR, EAL, "epoll_wait returns with fail %s\n",
1044                                 strerror(errno));
1045                         rc = -1;
1046                         break;
1047                 } else {
1048                         /* rc == 0, epoll_wait timed out */
1049                         break;
1050                 }
1051         }
1052
1053         return rc;
1054 }
1055
1056 static inline void
1057 eal_epoll_data_safe_free(struct rte_epoll_event *ev)
1058 {
1059         while (!rte_atomic32_cmpset(&ev->status, RTE_EPOLL_VALID,
1060                                     RTE_EPOLL_INVALID))
1061                 while (ev->status != RTE_EPOLL_VALID)
1062                         rte_pause();
1063         memset(&ev->epdata, 0, sizeof(ev->epdata));
1064         ev->fd = -1;
1065         ev->epfd = -1;
1066 }
1067
1068 int
1069 rte_epoll_ctl(int epfd, int op, int fd,
1070               struct rte_epoll_event *event)
1071 {
1072         struct epoll_event ev;
1073
1074         if (!event) {
1075                 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1076                 return -1;
1077         }
1078
1079         /* using per thread epoll fd */
1080         if (epfd == RTE_EPOLL_PER_THREAD)
1081                 epfd = rte_intr_tls_epfd();
1082
1083         if (op == EPOLL_CTL_ADD) {
1084                 event->status = RTE_EPOLL_VALID;
1085                 event->fd = fd;  /* ignore fd in event */
1086                 event->epfd = epfd;
1087                 ev.data.ptr = (void *)event;
1088         }
1089
1090         ev.events = event->epdata.event;
1091         if (epoll_ctl(epfd, op, fd, &ev) < 0) {
1092                 RTE_LOG(ERR, EAL, "Error op %d fd %d epoll_ctl, %s\n",
1093                         op, fd, strerror(errno));
1094                 if (op == EPOLL_CTL_ADD)
1095                         /* rollback status when CTL_ADD fail */
1096                         event->status = RTE_EPOLL_INVALID;
1097                 return -1;
1098         }
1099
1100         if (op == EPOLL_CTL_DEL && event->status != RTE_EPOLL_INVALID)
1101                 eal_epoll_data_safe_free(event);
1102
1103         return 0;
1104 }
1105
1106 int
1107 rte_intr_rx_ctl(struct rte_intr_handle *intr_handle, int epfd,
1108                 int op, unsigned int vec, void *data)
1109 {
1110         struct rte_epoll_event *rev;
1111         struct rte_epoll_data *epdata;
1112         int epfd_op;
1113         unsigned int efd_idx;
1114         int rc = 0;
1115
1116         efd_idx = (vec >= RTE_INTR_VEC_RXTX_OFFSET) ?
1117                 (vec - RTE_INTR_VEC_RXTX_OFFSET) : vec;
1118
1119         if (!intr_handle || intr_handle->nb_efd == 0 ||
1120             efd_idx >= intr_handle->nb_efd) {
1121                 RTE_LOG(ERR, EAL, "Wrong intr vector number.\n");
1122                 return -EPERM;
1123         }
1124
1125         switch (op) {
1126         case RTE_INTR_EVENT_ADD:
1127                 epfd_op = EPOLL_CTL_ADD;
1128                 rev = &intr_handle->elist[efd_idx];
1129                 if (rev->status != RTE_EPOLL_INVALID) {
1130                         RTE_LOG(INFO, EAL, "Event already been added.\n");
1131                         return -EEXIST;
1132                 }
1133
1134                 /* attach to intr vector fd */
1135                 epdata = &rev->epdata;
1136                 epdata->event  = EPOLLIN | EPOLLPRI | EPOLLET;
1137                 epdata->data   = data;
1138                 epdata->cb_fun = (rte_intr_event_cb_t)eal_intr_proc_rxtx_intr;
1139                 epdata->cb_arg = (void *)intr_handle;
1140                 rc = rte_epoll_ctl(epfd, epfd_op,
1141                                    intr_handle->efds[efd_idx], rev);
1142                 if (!rc)
1143                         RTE_LOG(DEBUG, EAL,
1144                                 "efd %d associated with vec %d added on epfd %d"
1145                                 "\n", rev->fd, vec, epfd);
1146                 else
1147                         rc = -EPERM;
1148                 break;
1149         case RTE_INTR_EVENT_DEL:
1150                 epfd_op = EPOLL_CTL_DEL;
1151                 rev = &intr_handle->elist[efd_idx];
1152                 if (rev->status == RTE_EPOLL_INVALID) {
1153                         RTE_LOG(INFO, EAL, "Event does not exist.\n");
1154                         return -EPERM;
1155                 }
1156
1157                 rc = rte_epoll_ctl(rev->epfd, epfd_op, rev->fd, rev);
1158                 if (rc)
1159                         rc = -EPERM;
1160                 break;
1161         default:
1162                 RTE_LOG(ERR, EAL, "event op type mismatch\n");
1163                 rc = -EPERM;
1164         }
1165
1166         return rc;
1167 }
1168
1169 void
1170 rte_intr_free_epoll_fd(struct rte_intr_handle *intr_handle)
1171 {
1172         uint32_t i;
1173         struct rte_epoll_event *rev;
1174
1175         for (i = 0; i < intr_handle->nb_efd; i++) {
1176                 rev = &intr_handle->elist[i];
1177                 if (rev->status == RTE_EPOLL_INVALID)
1178                         continue;
1179                 if (rte_epoll_ctl(rev->epfd, EPOLL_CTL_DEL, rev->fd, rev)) {
1180                         /* force free if the entry valid */
1181                         eal_epoll_data_safe_free(rev);
1182                         rev->status = RTE_EPOLL_INVALID;
1183                 }
1184         }
1185 }
1186
1187 int
1188 rte_intr_efd_enable(struct rte_intr_handle *intr_handle, uint32_t nb_efd)
1189 {
1190         uint32_t i;
1191         int fd;
1192         uint32_t n = RTE_MIN(nb_efd, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
1193
1194         assert(nb_efd != 0);
1195
1196         if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX) {
1197                 for (i = 0; i < n; i++) {
1198                         fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
1199                         if (fd < 0) {
1200                                 RTE_LOG(ERR, EAL,
1201                                         "can't setup eventfd, error %i (%s)\n",
1202                                         errno, strerror(errno));
1203                                 return -errno;
1204                         }
1205                         intr_handle->efds[i] = fd;
1206                 }
1207                 intr_handle->nb_efd   = n;
1208                 intr_handle->max_intr = NB_OTHER_INTR + n;
1209         } else if (intr_handle->type == RTE_INTR_HANDLE_VDEV) {
1210                 /* do nothing, and let vdev driver to initialize this struct */
1211         } else {
1212                 intr_handle->efds[0]  = intr_handle->fd;
1213                 intr_handle->nb_efd   = RTE_MIN(nb_efd, 1U);
1214                 intr_handle->max_intr = NB_OTHER_INTR;
1215         }
1216
1217         return 0;
1218 }
1219
1220 void
1221 rte_intr_efd_disable(struct rte_intr_handle *intr_handle)
1222 {
1223         uint32_t i;
1224
1225         rte_intr_free_epoll_fd(intr_handle);
1226         if (intr_handle->max_intr > intr_handle->nb_efd) {
1227                 for (i = 0; i < intr_handle->nb_efd; i++)
1228                         close(intr_handle->efds[i]);
1229         }
1230         intr_handle->nb_efd = 0;
1231         intr_handle->max_intr = 0;
1232 }
1233
1234 int
1235 rte_intr_dp_is_en(struct rte_intr_handle *intr_handle)
1236 {
1237         return !(!intr_handle->nb_efd);
1238 }
1239
1240 int
1241 rte_intr_allow_others(struct rte_intr_handle *intr_handle)
1242 {
1243         if (!rte_intr_dp_is_en(intr_handle))
1244                 return 1;
1245         else
1246                 return !!(intr_handle->max_intr - intr_handle->nb_efd);
1247 }
1248
1249 int
1250 rte_intr_cap_multiple(struct rte_intr_handle *intr_handle)
1251 {
1252         if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX)
1253                 return 1;
1254
1255         if (intr_handle->type == RTE_INTR_HANDLE_VDEV)
1256                 return 1;
1257
1258         return 0;
1259 }