8758239d0f20fcbb70a3569e954c28b79e4d59f1
[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
49 #include <rte_common.h>
50 #include <rte_interrupts.h>
51 #include <rte_memory.h>
52 #include <rte_memzone.h>
53 #include <rte_launch.h>
54 #include <rte_eal.h>
55 #include <rte_per_lcore.h>
56 #include <rte_lcore.h>
57 #include <rte_atomic.h>
58 #include <rte_branch_prediction.h>
59 #include <rte_ring.h>
60 #include <rte_debug.h>
61 #include <rte_log.h>
62 #include <rte_mempool.h>
63 #include <rte_pci.h>
64 #include <rte_malloc.h>
65 #include <rte_errno.h>
66 #include <rte_spinlock.h>
67
68 #include "eal_private.h"
69 #include "eal_vfio.h"
70 #include "eal_thread.h"
71
72 #define EAL_INTR_EPOLL_WAIT_FOREVER (-1)
73 #define NB_OTHER_INTR               1
74
75 static RTE_DEFINE_PER_LCORE(int, _epfd) = -1; /**< epoll fd per thread */
76
77 /**
78  * union for pipe fds.
79  */
80 union intr_pipefds{
81         struct {
82                 int pipefd[2];
83         };
84         struct {
85                 int readfd;
86                 int writefd;
87         };
88 };
89
90 /**
91  * union buffer for reading on different devices
92  */
93 union rte_intr_read_buffer {
94         int uio_intr_count;              /* for uio device */
95 #ifdef VFIO_PRESENT
96         uint64_t vfio_intr_count;        /* for vfio device */
97 #endif
98         uint64_t timerfd_num;            /* for timerfd */
99         char charbuf[16];                /* for others */
100 };
101
102 TAILQ_HEAD(rte_intr_cb_list, rte_intr_callback);
103 TAILQ_HEAD(rte_intr_source_list, rte_intr_source);
104
105 struct rte_intr_callback {
106         TAILQ_ENTRY(rte_intr_callback) next;
107         rte_intr_callback_fn cb_fn;  /**< callback address */
108         void *cb_arg;                /**< parameter for callback */
109 };
110
111 struct rte_intr_source {
112         TAILQ_ENTRY(rte_intr_source) next;
113         struct rte_intr_handle intr_handle; /**< interrupt handle */
114         struct rte_intr_cb_list callbacks;  /**< user callbacks */
115         uint32_t active;
116 };
117
118 /* global spinlock for interrupt data operation */
119 static rte_spinlock_t intr_lock = RTE_SPINLOCK_INITIALIZER;
120
121 /* union buffer for pipe read/write */
122 static union intr_pipefds intr_pipe;
123
124 /* interrupt sources list */
125 static struct rte_intr_source_list intr_sources;
126
127 /* interrupt handling thread */
128 static pthread_t intr_thread;
129
130 /* VFIO interrupts */
131 #ifdef VFIO_PRESENT
132
133 #define IRQ_SET_BUF_LEN  (sizeof(struct vfio_irq_set) + sizeof(int))
134 /* irq set buffer length for queue interrupts and LSC interrupt */
135 #define MSIX_IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + \
136                               sizeof(int) * (RTE_MAX_RXTX_INTR_VEC_ID + 1))
137
138 /* enable legacy (INTx) interrupts */
139 static int
140 vfio_enable_intx(struct rte_intr_handle *intr_handle) {
141         struct vfio_irq_set *irq_set;
142         char irq_set_buf[IRQ_SET_BUF_LEN];
143         int len, ret;
144         int *fd_ptr;
145
146         len = sizeof(irq_set_buf);
147
148         /* enable INTx */
149         irq_set = (struct vfio_irq_set *) irq_set_buf;
150         irq_set->argsz = len;
151         irq_set->count = 1;
152         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
153         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
154         irq_set->start = 0;
155         fd_ptr = (int *) &irq_set->data;
156         *fd_ptr = intr_handle->fd;
157
158         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
159
160         if (ret) {
161                 RTE_LOG(ERR, EAL, "Error enabling INTx interrupts for fd %d\n",
162                                                 intr_handle->fd);
163                 return -1;
164         }
165
166         /* unmask INTx after enabling */
167         memset(irq_set, 0, len);
168         len = sizeof(struct vfio_irq_set);
169         irq_set->argsz = len;
170         irq_set->count = 1;
171         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
172         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
173         irq_set->start = 0;
174
175         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
176
177         if (ret) {
178                 RTE_LOG(ERR, EAL, "Error unmasking INTx interrupts for fd %d\n",
179                                                 intr_handle->fd);
180                 return -1;
181         }
182         return 0;
183 }
184
185 /* disable legacy (INTx) interrupts */
186 static int
187 vfio_disable_intx(struct rte_intr_handle *intr_handle) {
188         struct vfio_irq_set *irq_set;
189         char irq_set_buf[IRQ_SET_BUF_LEN];
190         int len, ret;
191
192         len = sizeof(struct vfio_irq_set);
193
194         /* mask interrupts before disabling */
195         irq_set = (struct vfio_irq_set *) irq_set_buf;
196         irq_set->argsz = len;
197         irq_set->count = 1;
198         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
199         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
200         irq_set->start = 0;
201
202         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
203
204         if (ret) {
205                 RTE_LOG(ERR, EAL, "Error unmasking INTx interrupts for fd %d\n",
206                                                 intr_handle->fd);
207                 return -1;
208         }
209
210         /* disable INTx*/
211         memset(irq_set, 0, len);
212         irq_set->argsz = len;
213         irq_set->count = 0;
214         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
215         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
216         irq_set->start = 0;
217
218         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
219
220         if (ret) {
221                 RTE_LOG(ERR, EAL,
222                         "Error disabling INTx interrupts for fd %d\n", intr_handle->fd);
223                 return -1;
224         }
225         return 0;
226 }
227
228 /* enable MSI interrupts */
229 static int
230 vfio_enable_msi(struct rte_intr_handle *intr_handle) {
231         int len, ret;
232         char irq_set_buf[IRQ_SET_BUF_LEN];
233         struct vfio_irq_set *irq_set;
234         int *fd_ptr;
235
236         len = sizeof(irq_set_buf);
237
238         irq_set = (struct vfio_irq_set *) irq_set_buf;
239         irq_set->argsz = len;
240         irq_set->count = 1;
241         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
242         irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
243         irq_set->start = 0;
244         fd_ptr = (int *) &irq_set->data;
245         *fd_ptr = intr_handle->fd;
246
247         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
248
249         if (ret) {
250                 RTE_LOG(ERR, EAL, "Error enabling MSI interrupts for fd %d\n",
251                                                 intr_handle->fd);
252                 return -1;
253         }
254         return 0;
255 }
256
257 /* disable MSI interrupts */
258 static int
259 vfio_disable_msi(struct rte_intr_handle *intr_handle) {
260         struct vfio_irq_set *irq_set;
261         char irq_set_buf[IRQ_SET_BUF_LEN];
262         int len, ret;
263
264         len = sizeof(struct vfio_irq_set);
265
266         irq_set = (struct vfio_irq_set *) irq_set_buf;
267         irq_set->argsz = len;
268         irq_set->count = 0;
269         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
270         irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
271         irq_set->start = 0;
272
273         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
274
275         if (ret)
276                 RTE_LOG(ERR, EAL,
277                         "Error disabling MSI interrupts for fd %d\n", intr_handle->fd);
278
279         return ret;
280 }
281
282 /* enable MSI-X interrupts */
283 static int
284 vfio_enable_msix(struct rte_intr_handle *intr_handle) {
285         int len, ret;
286         char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
287         struct vfio_irq_set *irq_set;
288         int *fd_ptr;
289
290         len = sizeof(irq_set_buf);
291
292         irq_set = (struct vfio_irq_set *) irq_set_buf;
293         irq_set->argsz = len;
294         if (!intr_handle->max_intr)
295                 intr_handle->max_intr = 1;
296         else if (intr_handle->max_intr > RTE_MAX_RXTX_INTR_VEC_ID)
297                 intr_handle->max_intr = RTE_MAX_RXTX_INTR_VEC_ID + 1;
298
299         irq_set->count = intr_handle->max_intr;
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(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(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(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(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(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(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(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(struct rte_intr_handle *intr_handle)
560 {
561         if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
562                 return -1;
563
564         switch (intr_handle->type){
565         /* write to the uio fd to enable the interrupt */
566         case RTE_INTR_HANDLE_UIO:
567                 if (uio_intr_enable(intr_handle))
568                         return -1;
569                 break;
570         case RTE_INTR_HANDLE_UIO_INTX:
571                 if (uio_intx_intr_enable(intr_handle))
572                         return -1;
573                 break;
574         /* not used at this moment */
575         case RTE_INTR_HANDLE_ALARM:
576                 return -1;
577 #ifdef VFIO_PRESENT
578         case RTE_INTR_HANDLE_VFIO_MSIX:
579                 if (vfio_enable_msix(intr_handle))
580                         return -1;
581                 break;
582         case RTE_INTR_HANDLE_VFIO_MSI:
583                 if (vfio_enable_msi(intr_handle))
584                         return -1;
585                 break;
586         case RTE_INTR_HANDLE_VFIO_LEGACY:
587                 if (vfio_enable_intx(intr_handle))
588                         return -1;
589                 break;
590 #endif
591         /* unknown handle type */
592         default:
593                 RTE_LOG(ERR, EAL,
594                         "Unknown handle type of fd %d\n",
595                                         intr_handle->fd);
596                 return -1;
597         }
598
599         return 0;
600 }
601
602 int
603 rte_intr_disable(struct rte_intr_handle *intr_handle)
604 {
605         if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
606                 return -1;
607
608         switch (intr_handle->type){
609         /* write to the uio fd to disable the interrupt */
610         case RTE_INTR_HANDLE_UIO:
611                 if (uio_intr_disable(intr_handle))
612                         return -1;
613                 break;
614         case RTE_INTR_HANDLE_UIO_INTX:
615                 if (uio_intx_intr_disable(intr_handle))
616                         return -1;
617                 break;
618         /* not used at this moment */
619         case RTE_INTR_HANDLE_ALARM:
620                 return -1;
621 #ifdef VFIO_PRESENT
622         case RTE_INTR_HANDLE_VFIO_MSIX:
623                 if (vfio_disable_msix(intr_handle))
624                         return -1;
625                 break;
626         case RTE_INTR_HANDLE_VFIO_MSI:
627                 if (vfio_disable_msi(intr_handle))
628                         return -1;
629                 break;
630         case RTE_INTR_HANDLE_VFIO_LEGACY:
631                 if (vfio_disable_intx(intr_handle))
632                         return -1;
633                 break;
634 #endif
635         /* unknown handle type */
636         default:
637                 RTE_LOG(ERR, EAL,
638                         "Unknown handle type of fd %d\n",
639                                         intr_handle->fd);
640                 return -1;
641         }
642
643         return 0;
644 }
645
646 static int
647 eal_intr_process_interrupts(struct epoll_event *events, int nfds)
648 {
649         int n, bytes_read;
650         struct rte_intr_source *src;
651         struct rte_intr_callback *cb;
652         union rte_intr_read_buffer buf;
653         struct rte_intr_callback active_cb;
654
655         for (n = 0; n < nfds; n++) {
656
657                 /**
658                  * if the pipe fd is ready to read, return out to
659                  * rebuild the wait list.
660                  */
661                 if (events[n].data.fd == intr_pipe.readfd){
662                         int r = read(intr_pipe.readfd, buf.charbuf,
663                                         sizeof(buf.charbuf));
664                         RTE_SET_USED(r);
665                         return -1;
666                 }
667                 rte_spinlock_lock(&intr_lock);
668                 TAILQ_FOREACH(src, &intr_sources, next)
669                         if (src->intr_handle.fd ==
670                                         events[n].data.fd)
671                                 break;
672                 if (src == NULL){
673                         rte_spinlock_unlock(&intr_lock);
674                         continue;
675                 }
676
677                 /* mark this interrupt source as active and release the lock. */
678                 src->active = 1;
679                 rte_spinlock_unlock(&intr_lock);
680
681                 /* set the length to be read dor different handle type */
682                 switch (src->intr_handle.type) {
683                 case RTE_INTR_HANDLE_UIO:
684                 case RTE_INTR_HANDLE_UIO_INTX:
685                         bytes_read = sizeof(buf.uio_intr_count);
686                         break;
687                 case RTE_INTR_HANDLE_ALARM:
688                         bytes_read = sizeof(buf.timerfd_num);
689                         break;
690 #ifdef VFIO_PRESENT
691                 case RTE_INTR_HANDLE_VFIO_MSIX:
692                 case RTE_INTR_HANDLE_VFIO_MSI:
693                 case RTE_INTR_HANDLE_VFIO_LEGACY:
694                         bytes_read = sizeof(buf.vfio_intr_count);
695                         break;
696 #endif
697                 case RTE_INTR_HANDLE_EXT:
698                 default:
699                         bytes_read = 1;
700                         break;
701                 }
702
703                 if (src->intr_handle.type != RTE_INTR_HANDLE_EXT) {
704                         /**
705                          * read out to clear the ready-to-be-read flag
706                          * for epoll_wait.
707                          */
708                         bytes_read = read(events[n].data.fd, &buf, bytes_read);
709                         if (bytes_read < 0) {
710                                 if (errno == EINTR || errno == EWOULDBLOCK)
711                                         continue;
712
713                                 RTE_LOG(ERR, EAL, "Error reading from file "
714                                         "descriptor %d: %s\n",
715                                         events[n].data.fd,
716                                         strerror(errno));
717                         } else if (bytes_read == 0)
718                                 RTE_LOG(ERR, EAL, "Read nothing from file "
719                                         "descriptor %d\n", events[n].data.fd);
720                 }
721
722                 /* grab a lock, again to call callbacks and update status. */
723                 rte_spinlock_lock(&intr_lock);
724
725                 if (bytes_read > 0) {
726
727                         /* Finally, call all callbacks. */
728                         TAILQ_FOREACH(cb, &src->callbacks, next) {
729
730                                 /* make a copy and unlock. */
731                                 active_cb = *cb;
732                                 rte_spinlock_unlock(&intr_lock);
733
734                                 /* call the actual callback */
735                                 active_cb.cb_fn(&src->intr_handle,
736                                         active_cb.cb_arg);
737
738                                 /*get the lock back. */
739                                 rte_spinlock_lock(&intr_lock);
740                         }
741                 }
742
743                 /* we done with that interrupt source, release it. */
744                 src->active = 0;
745                 rte_spinlock_unlock(&intr_lock);
746         }
747
748         return 0;
749 }
750
751 /**
752  * It handles all the interrupts.
753  *
754  * @param pfd
755  *  epoll file descriptor.
756  * @param totalfds
757  *  The number of file descriptors added in epoll.
758  *
759  * @return
760  *  void
761  */
762 static void
763 eal_intr_handle_interrupts(int pfd, unsigned totalfds)
764 {
765         struct epoll_event events[totalfds];
766         int nfds = 0;
767
768         for(;;) {
769                 nfds = epoll_wait(pfd, events, totalfds,
770                         EAL_INTR_EPOLL_WAIT_FOREVER);
771                 /* epoll_wait fail */
772                 if (nfds < 0) {
773                         if (errno == EINTR)
774                                 continue;
775                         RTE_LOG(ERR, EAL,
776                                 "epoll_wait returns with fail\n");
777                         return;
778                 }
779                 /* epoll_wait timeout, will never happens here */
780                 else if (nfds == 0)
781                         continue;
782                 /* epoll_wait has at least one fd ready to read */
783                 if (eal_intr_process_interrupts(events, nfds) < 0)
784                         return;
785         }
786 }
787
788 /**
789  * It builds/rebuilds up the epoll file descriptor with all the
790  * file descriptors being waited on. Then handles the interrupts.
791  *
792  * @param arg
793  *  pointer. (unused)
794  *
795  * @return
796  *  never return;
797  */
798 static __attribute__((noreturn)) void *
799 eal_intr_thread_main(__rte_unused void *arg)
800 {
801         struct epoll_event ev;
802
803         /* host thread, never break out */
804         for (;;) {
805                 /* build up the epoll fd with all descriptors we are to
806                  * wait on then pass it to the handle_interrupts function
807                  */
808                 static struct epoll_event pipe_event = {
809                         .events = EPOLLIN | EPOLLPRI,
810                 };
811                 struct rte_intr_source *src;
812                 unsigned numfds = 0;
813
814                 /* create epoll fd */
815                 int pfd = epoll_create(1);
816                 if (pfd < 0)
817                         rte_panic("Cannot create epoll instance\n");
818
819                 pipe_event.data.fd = intr_pipe.readfd;
820                 /**
821                  * add pipe fd into wait list, this pipe is used to
822                  * rebuild the wait list.
823                  */
824                 if (epoll_ctl(pfd, EPOLL_CTL_ADD, intr_pipe.readfd,
825                                                 &pipe_event) < 0) {
826                         rte_panic("Error adding fd to %d epoll_ctl, %s\n",
827                                         intr_pipe.readfd, strerror(errno));
828                 }
829                 numfds++;
830
831                 rte_spinlock_lock(&intr_lock);
832
833                 TAILQ_FOREACH(src, &intr_sources, next) {
834                         if (src->callbacks.tqh_first == NULL)
835                                 continue; /* skip those with no callbacks */
836                         ev.events = EPOLLIN | EPOLLPRI;
837                         ev.data.fd = src->intr_handle.fd;
838
839                         /**
840                          * add all the uio device file descriptor
841                          * into wait list.
842                          */
843                         if (epoll_ctl(pfd, EPOLL_CTL_ADD,
844                                         src->intr_handle.fd, &ev) < 0){
845                                 rte_panic("Error adding fd %d epoll_ctl, %s\n",
846                                         src->intr_handle.fd, strerror(errno));
847                         }
848                         else
849                                 numfds++;
850                 }
851                 rte_spinlock_unlock(&intr_lock);
852                 /* serve the interrupt */
853                 eal_intr_handle_interrupts(pfd, numfds);
854
855                 /**
856                  * when we return, we need to rebuild the
857                  * list of fds to monitor.
858                  */
859                 close(pfd);
860         }
861 }
862
863 int
864 rte_eal_intr_init(void)
865 {
866         int ret = 0, ret_1 = 0;
867         char thread_name[RTE_MAX_THREAD_NAME_LEN];
868
869         /* init the global interrupt source head */
870         TAILQ_INIT(&intr_sources);
871
872         /**
873          * create a pipe which will be waited by epoll and notified to
874          * rebuild the wait list of epoll.
875          */
876         if (pipe(intr_pipe.pipefd) < 0)
877                 return -1;
878
879         /* create the host thread to wait/handle the interrupt */
880         ret = pthread_create(&intr_thread, NULL,
881                         eal_intr_thread_main, NULL);
882         if (ret != 0) {
883                 RTE_LOG(ERR, EAL,
884                         "Failed to create thread for interrupt handling\n");
885         } else {
886                 /* Set thread_name for aid in debugging. */
887                 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN,
888                         "eal-intr-thread");
889                 ret_1 = pthread_setname_np(intr_thread, thread_name);
890                 if (ret_1 != 0)
891                         RTE_LOG(ERR, EAL,
892                         "Failed to set thread name for interrupt handling\n");
893         }
894
895         return -ret;
896 }
897
898 static void
899 eal_intr_proc_rxtx_intr(int fd, const struct rte_intr_handle *intr_handle)
900 {
901         union rte_intr_read_buffer buf;
902         int bytes_read = 1;
903
904         switch (intr_handle->type) {
905         case RTE_INTR_HANDLE_UIO:
906         case RTE_INTR_HANDLE_UIO_INTX:
907                 bytes_read = sizeof(buf.uio_intr_count);
908                 break;
909 #ifdef VFIO_PRESENT
910         case RTE_INTR_HANDLE_VFIO_MSIX:
911         case RTE_INTR_HANDLE_VFIO_MSI:
912         case RTE_INTR_HANDLE_VFIO_LEGACY:
913                 bytes_read = sizeof(buf.vfio_intr_count);
914                 break;
915 #endif
916         default:
917                 bytes_read = 1;
918                 RTE_LOG(INFO, EAL, "unexpected intr type\n");
919                 break;
920         }
921
922         /**
923          * read out to clear the ready-to-be-read flag
924          * for epoll_wait.
925          */
926         do {
927                 bytes_read = read(fd, &buf, bytes_read);
928                 if (bytes_read < 0) {
929                         if (errno == EINTR || errno == EWOULDBLOCK ||
930                             errno == EAGAIN)
931                                 continue;
932                         RTE_LOG(ERR, EAL,
933                                 "Error reading from fd %d: %s\n",
934                                 fd, strerror(errno));
935                 } else if (bytes_read == 0)
936                         RTE_LOG(ERR, EAL, "Read nothing from fd %d\n", fd);
937                 return;
938         } while (1);
939 }
940
941 static int
942 eal_epoll_process_event(struct epoll_event *evs, unsigned int n,
943                         struct rte_epoll_event *events)
944 {
945         unsigned int i, count = 0;
946         struct rte_epoll_event *rev;
947
948         for (i = 0; i < n; i++) {
949                 rev = evs[i].data.ptr;
950                 if (!rev || !rte_atomic32_cmpset(&rev->status, RTE_EPOLL_VALID,
951                                                  RTE_EPOLL_EXEC))
952                         continue;
953
954                 events[count].status        = RTE_EPOLL_VALID;
955                 events[count].fd            = rev->fd;
956                 events[count].epfd          = rev->epfd;
957                 events[count].epdata.event  = rev->epdata.event;
958                 events[count].epdata.data   = rev->epdata.data;
959                 if (rev->epdata.cb_fun)
960                         rev->epdata.cb_fun(rev->fd,
961                                            rev->epdata.cb_arg);
962
963                 rte_compiler_barrier();
964                 rev->status = RTE_EPOLL_VALID;
965                 count++;
966         }
967         return count;
968 }
969
970 static inline int
971 eal_init_tls_epfd(void)
972 {
973         int pfd = epoll_create(255);
974
975         if (pfd < 0) {
976                 RTE_LOG(ERR, EAL,
977                         "Cannot create epoll instance\n");
978                 return -1;
979         }
980         return pfd;
981 }
982
983 int
984 rte_intr_tls_epfd(void)
985 {
986         if (RTE_PER_LCORE(_epfd) == -1)
987                 RTE_PER_LCORE(_epfd) = eal_init_tls_epfd();
988
989         return RTE_PER_LCORE(_epfd);
990 }
991
992 int
993 rte_epoll_wait(int epfd, struct rte_epoll_event *events,
994                int maxevents, int timeout)
995 {
996         struct epoll_event evs[maxevents];
997         int rc;
998
999         if (!events) {
1000                 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1001                 return -1;
1002         }
1003
1004         /* using per thread epoll fd */
1005         if (epfd == RTE_EPOLL_PER_THREAD)
1006                 epfd = rte_intr_tls_epfd();
1007
1008         while (1) {
1009                 rc = epoll_wait(epfd, evs, maxevents, timeout);
1010                 if (likely(rc > 0)) {
1011                         /* epoll_wait has at least one fd ready to read */
1012                         rc = eal_epoll_process_event(evs, rc, events);
1013                         break;
1014                 } else if (rc < 0) {
1015                         if (errno == EINTR)
1016                                 continue;
1017                         /* epoll_wait fail */
1018                         RTE_LOG(ERR, EAL, "epoll_wait returns with fail %s\n",
1019                                 strerror(errno));
1020                         rc = -1;
1021                         break;
1022                 } else {
1023                         /* rc == 0, epoll_wait timed out */
1024                         break;
1025                 }
1026         }
1027
1028         return rc;
1029 }
1030
1031 static inline void
1032 eal_epoll_data_safe_free(struct rte_epoll_event *ev)
1033 {
1034         while (!rte_atomic32_cmpset(&ev->status, RTE_EPOLL_VALID,
1035                                     RTE_EPOLL_INVALID))
1036                 while (ev->status != RTE_EPOLL_VALID)
1037                         rte_pause();
1038         memset(&ev->epdata, 0, sizeof(ev->epdata));
1039         ev->fd = -1;
1040         ev->epfd = -1;
1041 }
1042
1043 int
1044 rte_epoll_ctl(int epfd, int op, int fd,
1045               struct rte_epoll_event *event)
1046 {
1047         struct epoll_event ev;
1048
1049         if (!event) {
1050                 RTE_LOG(ERR, EAL, "rte_epoll_event can't be NULL\n");
1051                 return -1;
1052         }
1053
1054         /* using per thread epoll fd */
1055         if (epfd == RTE_EPOLL_PER_THREAD)
1056                 epfd = rte_intr_tls_epfd();
1057
1058         if (op == EPOLL_CTL_ADD) {
1059                 event->status = RTE_EPOLL_VALID;
1060                 event->fd = fd;  /* ignore fd in event */
1061                 event->epfd = epfd;
1062                 ev.data.ptr = (void *)event;
1063         }
1064
1065         ev.events = event->epdata.event;
1066         if (epoll_ctl(epfd, op, fd, &ev) < 0) {
1067                 RTE_LOG(ERR, EAL, "Error op %d fd %d epoll_ctl, %s\n",
1068                         op, fd, strerror(errno));
1069                 if (op == EPOLL_CTL_ADD)
1070                         /* rollback status when CTL_ADD fail */
1071                         event->status = RTE_EPOLL_INVALID;
1072                 return -1;
1073         }
1074
1075         if (op == EPOLL_CTL_DEL && event->status != RTE_EPOLL_INVALID)
1076                 eal_epoll_data_safe_free(event);
1077
1078         return 0;
1079 }
1080
1081 int
1082 rte_intr_rx_ctl(struct rte_intr_handle *intr_handle, int epfd,
1083                 int op, unsigned int vec, void *data)
1084 {
1085         struct rte_epoll_event *rev;
1086         struct rte_epoll_data *epdata;
1087         int epfd_op;
1088         unsigned int efd_idx;
1089         int rc = 0;
1090
1091         efd_idx = (vec >= RTE_INTR_VEC_RXTX_OFFSET) ?
1092                 (vec - RTE_INTR_VEC_RXTX_OFFSET) : vec;
1093
1094         if (!intr_handle || intr_handle->nb_efd == 0 ||
1095             efd_idx >= intr_handle->nb_efd) {
1096                 RTE_LOG(ERR, EAL, "Wrong intr vector number.\n");
1097                 return -EPERM;
1098         }
1099
1100         switch (op) {
1101         case RTE_INTR_EVENT_ADD:
1102                 epfd_op = EPOLL_CTL_ADD;
1103                 rev = &intr_handle->elist[efd_idx];
1104                 if (rev->status != RTE_EPOLL_INVALID) {
1105                         RTE_LOG(INFO, EAL, "Event already been added.\n");
1106                         return -EEXIST;
1107                 }
1108
1109                 /* attach to intr vector fd */
1110                 epdata = &rev->epdata;
1111                 epdata->event  = EPOLLIN | EPOLLPRI | EPOLLET;
1112                 epdata->data   = data;
1113                 epdata->cb_fun = (rte_intr_event_cb_t)eal_intr_proc_rxtx_intr;
1114                 epdata->cb_arg = (void *)intr_handle;
1115                 rc = rte_epoll_ctl(epfd, epfd_op,
1116                                    intr_handle->efds[efd_idx], rev);
1117                 if (!rc)
1118                         RTE_LOG(DEBUG, EAL,
1119                                 "efd %d associated with vec %d added on epfd %d"
1120                                 "\n", rev->fd, vec, epfd);
1121                 else
1122                         rc = -EPERM;
1123                 break;
1124         case RTE_INTR_EVENT_DEL:
1125                 epfd_op = EPOLL_CTL_DEL;
1126                 rev = &intr_handle->elist[efd_idx];
1127                 if (rev->status == RTE_EPOLL_INVALID) {
1128                         RTE_LOG(INFO, EAL, "Event does not exist.\n");
1129                         return -EPERM;
1130                 }
1131
1132                 rc = rte_epoll_ctl(rev->epfd, epfd_op, rev->fd, rev);
1133                 if (rc)
1134                         rc = -EPERM;
1135                 break;
1136         default:
1137                 RTE_LOG(ERR, EAL, "event op type mismatch\n");
1138                 rc = -EPERM;
1139         }
1140
1141         return rc;
1142 }
1143
1144 int
1145 rte_intr_efd_enable(struct rte_intr_handle *intr_handle, uint32_t nb_efd)
1146 {
1147         uint32_t i;
1148         int fd;
1149         uint32_t n = RTE_MIN(nb_efd, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
1150
1151         if (intr_handle->type == RTE_INTR_HANDLE_VFIO_MSIX) {
1152                 for (i = 0; i < n; i++) {
1153                         fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
1154                         if (fd < 0) {
1155                                 RTE_LOG(ERR, EAL,
1156                                         "can't setup eventfd, error %i (%s)\n",
1157                                         errno, strerror(errno));
1158                                 return -1;
1159                         }
1160                         intr_handle->efds[i] = fd;
1161                 }
1162                 intr_handle->nb_efd   = n;
1163                 intr_handle->max_intr = NB_OTHER_INTR + n;
1164         } else {
1165                 intr_handle->efds[0]  = intr_handle->fd;
1166                 intr_handle->nb_efd   = RTE_MIN(nb_efd, 1U);
1167                 intr_handle->max_intr = NB_OTHER_INTR;
1168         }
1169
1170         return 0;
1171 }
1172
1173 void
1174 rte_intr_efd_disable(struct rte_intr_handle *intr_handle)
1175 {
1176         uint32_t i;
1177         struct rte_epoll_event *rev;
1178
1179         for (i = 0; i < intr_handle->nb_efd; i++) {
1180                 rev = &intr_handle->elist[i];
1181                 if (rev->status == RTE_EPOLL_INVALID)
1182                         continue;
1183                 if (rte_epoll_ctl(rev->epfd, EPOLL_CTL_DEL, rev->fd, rev)) {
1184                         /* force free if the entry valid */
1185                         eal_epoll_data_safe_free(rev);
1186                         rev->status = RTE_EPOLL_INVALID;
1187                 }
1188         }
1189
1190         if (intr_handle->max_intr > intr_handle->nb_efd) {
1191                 for (i = 0; i < intr_handle->nb_efd; i++)
1192                         close(intr_handle->efds[i]);
1193         }
1194         intr_handle->nb_efd = 0;
1195         intr_handle->max_intr = 0;
1196 }
1197
1198 int
1199 rte_intr_dp_is_en(struct rte_intr_handle *intr_handle)
1200 {
1201         return !(!intr_handle->nb_efd);
1202 }
1203
1204 int
1205 rte_intr_allow_others(struct rte_intr_handle *intr_handle)
1206 {
1207         return !!(intr_handle->max_intr - intr_handle->nb_efd);
1208 }