tailq: remove unneeded inclusions
[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
48 #include <rte_common.h>
49 #include <rte_interrupts.h>
50 #include <rte_memory.h>
51 #include <rte_memzone.h>
52 #include <rte_launch.h>
53 #include <rte_eal.h>
54 #include <rte_per_lcore.h>
55 #include <rte_lcore.h>
56 #include <rte_atomic.h>
57 #include <rte_branch_prediction.h>
58 #include <rte_ring.h>
59 #include <rte_debug.h>
60 #include <rte_log.h>
61 #include <rte_mempool.h>
62 #include <rte_pci.h>
63 #include <rte_malloc.h>
64 #include <rte_errno.h>
65 #include <rte_spinlock.h>
66
67 #include "eal_private.h"
68 #include "eal_vfio.h"
69
70 #define EAL_INTR_EPOLL_WAIT_FOREVER (-1)
71
72 /**
73  * union for pipe fds.
74  */
75 union intr_pipefds{
76         struct {
77                 int pipefd[2];
78         };
79         struct {
80                 int readfd;
81                 int writefd;
82         };
83 };
84
85 /**
86  * union buffer for reading on different devices
87  */
88 union rte_intr_read_buffer {
89         int uio_intr_count;              /* for uio device */
90 #ifdef VFIO_PRESENT
91         uint64_t vfio_intr_count;        /* for vfio device */
92 #endif
93         uint64_t timerfd_num;            /* for timerfd */
94         char charbuf[16];                /* for others */
95 };
96
97 TAILQ_HEAD(rte_intr_cb_list, rte_intr_callback);
98 TAILQ_HEAD(rte_intr_source_list, rte_intr_source);
99
100 struct rte_intr_callback {
101         TAILQ_ENTRY(rte_intr_callback) next;
102         rte_intr_callback_fn cb_fn;  /**< callback address */
103         void *cb_arg;                /**< parameter for callback */
104 };
105
106 struct rte_intr_source {
107         TAILQ_ENTRY(rte_intr_source) next;
108         struct rte_intr_handle intr_handle; /**< interrupt handle */
109         struct rte_intr_cb_list callbacks;  /**< user callbacks */
110         uint32_t active;
111 };
112
113 /* global spinlock for interrupt data operation */
114 static rte_spinlock_t intr_lock = RTE_SPINLOCK_INITIALIZER;
115
116 /* union buffer for pipe read/write */
117 static union intr_pipefds intr_pipe;
118
119 /* interrupt sources list */
120 static struct rte_intr_source_list intr_sources;
121
122 /* interrupt handling thread */
123 static pthread_t intr_thread;
124
125 /* VFIO interrupts */
126 #ifdef VFIO_PRESENT
127
128 #define IRQ_SET_BUF_LEN  (sizeof(struct vfio_irq_set) + sizeof(int))
129
130 /* enable legacy (INTx) interrupts */
131 static int
132 vfio_enable_intx(struct rte_intr_handle *intr_handle) {
133         struct vfio_irq_set *irq_set;
134         char irq_set_buf[IRQ_SET_BUF_LEN];
135         int len, ret;
136         int *fd_ptr;
137
138         len = sizeof(irq_set_buf);
139
140         /* enable INTx */
141         irq_set = (struct vfio_irq_set *) irq_set_buf;
142         irq_set->argsz = len;
143         irq_set->count = 1;
144         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
145         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
146         irq_set->start = 0;
147         fd_ptr = (int *) &irq_set->data;
148         *fd_ptr = intr_handle->fd;
149
150         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
151
152         if (ret) {
153                 RTE_LOG(ERR, EAL, "Error enabling INTx interrupts for fd %d\n",
154                                                 intr_handle->fd);
155                 return -1;
156         }
157
158         /* unmask INTx after enabling */
159         memset(irq_set, 0, len);
160         len = sizeof(struct vfio_irq_set);
161         irq_set->argsz = len;
162         irq_set->count = 1;
163         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
164         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
165         irq_set->start = 0;
166
167         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
168
169         if (ret) {
170                 RTE_LOG(ERR, EAL, "Error unmasking INTx interrupts for fd %d\n",
171                                                 intr_handle->fd);
172                 return -1;
173         }
174         return 0;
175 }
176
177 /* disable legacy (INTx) interrupts */
178 static int
179 vfio_disable_intx(struct rte_intr_handle *intr_handle) {
180         struct vfio_irq_set *irq_set;
181         char irq_set_buf[IRQ_SET_BUF_LEN];
182         int len, ret;
183
184         len = sizeof(struct vfio_irq_set);
185
186         /* mask interrupts before disabling */
187         irq_set = (struct vfio_irq_set *) irq_set_buf;
188         irq_set->argsz = len;
189         irq_set->count = 1;
190         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK;
191         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
192         irq_set->start = 0;
193
194         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
195
196         if (ret) {
197                 RTE_LOG(ERR, EAL, "Error unmasking INTx interrupts for fd %d\n",
198                                                 intr_handle->fd);
199                 return -1;
200         }
201
202         /* disable INTx*/
203         memset(irq_set, 0, len);
204         irq_set->argsz = len;
205         irq_set->count = 0;
206         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
207         irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
208         irq_set->start = 0;
209
210         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
211
212         if (ret) {
213                 RTE_LOG(ERR, EAL,
214                         "Error disabling INTx interrupts for fd %d\n", intr_handle->fd);
215                 return -1;
216         }
217         return 0;
218 }
219
220 /* enable MSI-X interrupts */
221 static int
222 vfio_enable_msi(struct rte_intr_handle *intr_handle) {
223         int len, ret;
224         char irq_set_buf[IRQ_SET_BUF_LEN];
225         struct vfio_irq_set *irq_set;
226         int *fd_ptr;
227
228         len = sizeof(irq_set_buf);
229
230         irq_set = (struct vfio_irq_set *) irq_set_buf;
231         irq_set->argsz = len;
232         irq_set->count = 1;
233         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
234         irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
235         irq_set->start = 0;
236         fd_ptr = (int *) &irq_set->data;
237         *fd_ptr = intr_handle->fd;
238
239         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
240
241         if (ret) {
242                 RTE_LOG(ERR, EAL, "Error enabling MSI interrupts for fd %d\n",
243                                                 intr_handle->fd);
244                 return -1;
245         }
246
247         /* manually trigger interrupt to enable it */
248         memset(irq_set, 0, len);
249         len = sizeof(struct vfio_irq_set);
250         irq_set->argsz = len;
251         irq_set->count = 1;
252         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
253         irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
254         irq_set->start = 0;
255
256         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
257
258         if (ret) {
259                 RTE_LOG(ERR, EAL, "Error triggering MSI interrupts for fd %d\n",
260                                                 intr_handle->fd);
261                 return -1;
262         }
263         return 0;
264 }
265
266 /* disable MSI-X interrupts */
267 static int
268 vfio_disable_msi(struct rte_intr_handle *intr_handle) {
269         struct vfio_irq_set *irq_set;
270         char irq_set_buf[IRQ_SET_BUF_LEN];
271         int len, ret;
272
273         len = sizeof(struct vfio_irq_set);
274
275         irq_set = (struct vfio_irq_set *) irq_set_buf;
276         irq_set->argsz = len;
277         irq_set->count = 0;
278         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
279         irq_set->index = VFIO_PCI_MSI_IRQ_INDEX;
280         irq_set->start = 0;
281
282         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
283
284         if (ret)
285                 RTE_LOG(ERR, EAL,
286                         "Error disabling MSI interrupts for fd %d\n", intr_handle->fd);
287
288         return ret;
289 }
290
291 /* enable MSI-X interrupts */
292 static int
293 vfio_enable_msix(struct rte_intr_handle *intr_handle) {
294         int len, ret;
295         char irq_set_buf[IRQ_SET_BUF_LEN];
296         struct vfio_irq_set *irq_set;
297         int *fd_ptr;
298
299         len = sizeof(irq_set_buf);
300
301         irq_set = (struct vfio_irq_set *) irq_set_buf;
302         irq_set->argsz = len;
303         irq_set->count = 1;
304         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
305         irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
306         irq_set->start = 0;
307         fd_ptr = (int *) &irq_set->data;
308         *fd_ptr = intr_handle->fd;
309
310         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
311
312         if (ret) {
313                 RTE_LOG(ERR, EAL, "Error enabling MSI-X interrupts for fd %d\n",
314                                                 intr_handle->fd);
315                 return -1;
316         }
317
318         /* manually trigger interrupt to enable it */
319         memset(irq_set, 0, len);
320         len = sizeof(struct vfio_irq_set);
321         irq_set->argsz = len;
322         irq_set->count = 1;
323         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
324         irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
325         irq_set->start = 0;
326
327         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
328
329         if (ret) {
330                 RTE_LOG(ERR, EAL, "Error triggering MSI-X interrupts for fd %d\n",
331                                                 intr_handle->fd);
332                 return -1;
333         }
334         return 0;
335 }
336
337 /* disable MSI-X interrupts */
338 static int
339 vfio_disable_msix(struct rte_intr_handle *intr_handle) {
340         struct vfio_irq_set *irq_set;
341         char irq_set_buf[IRQ_SET_BUF_LEN];
342         int len, ret;
343
344         len = sizeof(struct vfio_irq_set);
345
346         irq_set = (struct vfio_irq_set *) irq_set_buf;
347         irq_set->argsz = len;
348         irq_set->count = 0;
349         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
350         irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
351         irq_set->start = 0;
352
353         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
354
355         if (ret)
356                 RTE_LOG(ERR, EAL,
357                         "Error disabling MSI-X interrupts for fd %d\n", intr_handle->fd);
358
359         return ret;
360 }
361 #endif
362
363 static int
364 uio_intr_disable(struct rte_intr_handle *intr_handle)
365 {
366         unsigned char command_high;
367
368         /* use UIO config file descriptor for uio_pci_generic */
369         if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
370                 RTE_LOG(ERR, EAL,
371                         "Error reading interrupts status for fd %d\n",
372                         intr_handle->uio_cfg_fd);
373                 return -1;
374         }
375         /* disable interrupts */
376         command_high |= 0x4;
377         if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
378                 RTE_LOG(ERR, EAL,
379                         "Error disabling interrupts for fd %d\n",
380                         intr_handle->uio_cfg_fd);
381                 return -1;
382         }
383
384         return 0;
385 }
386
387 static int
388 uio_intr_enable(struct rte_intr_handle *intr_handle)
389 {
390         unsigned char command_high;
391
392         /* use UIO config file descriptor for uio_pci_generic */
393         if (pread(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
394                 RTE_LOG(ERR, EAL,
395                         "Error reading interrupts status for fd %d\n",
396                         intr_handle->uio_cfg_fd);
397                 return -1;
398         }
399         /* enable interrupts */
400         command_high &= ~0x4;
401         if (pwrite(intr_handle->uio_cfg_fd, &command_high, 1, 5) != 1) {
402                 RTE_LOG(ERR, EAL,
403                         "Error enabling interrupts for fd %d\n",
404                         intr_handle->uio_cfg_fd);
405                 return -1;
406         }
407
408         return 0;
409 }
410
411 int
412 rte_intr_callback_register(struct rte_intr_handle *intr_handle,
413                         rte_intr_callback_fn cb, void *cb_arg)
414 {
415         int ret, wake_thread;
416         struct rte_intr_source *src;
417         struct rte_intr_callback *callback;
418
419         wake_thread = 0;
420
421         /* first do parameter checking */
422         if (intr_handle == NULL || intr_handle->fd < 0 || cb == NULL) {
423                 RTE_LOG(ERR, EAL,
424                         "Registering with invalid input parameter\n");
425                 return -EINVAL;
426         }
427
428         /* allocate a new interrupt callback entity */
429         callback = rte_zmalloc("interrupt callback list",
430                                 sizeof(*callback), 0);
431         if (callback == NULL) {
432                 RTE_LOG(ERR, EAL, "Can not allocate memory\n");
433                 return -ENOMEM;
434         }
435         callback->cb_fn = cb;
436         callback->cb_arg = cb_arg;
437
438         rte_spinlock_lock(&intr_lock);
439
440         /* check if there is at least one callback registered for the fd */
441         TAILQ_FOREACH(src, &intr_sources, next) {
442                 if (src->intr_handle.fd == intr_handle->fd) {
443                         /* we had no interrupts for this */
444                         if TAILQ_EMPTY(&src->callbacks)
445                                 wake_thread = 1;
446
447                         TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
448                         ret = 0;
449                         break;
450                 }
451         }
452
453         /* no existing callbacks for this - add new source */
454         if (src == NULL) {
455                 if ((src = rte_zmalloc("interrupt source list",
456                                 sizeof(*src), 0)) == NULL) {
457                         RTE_LOG(ERR, EAL, "Can not allocate memory\n");
458                         rte_free(callback);
459                         ret = -ENOMEM;
460                 } else {
461                         src->intr_handle = *intr_handle;
462                         TAILQ_INIT(&src->callbacks);
463                         TAILQ_INSERT_TAIL(&(src->callbacks), callback, next);
464                         TAILQ_INSERT_TAIL(&intr_sources, src, next);
465                         wake_thread = 1;
466                         ret = 0;
467                 }
468         }
469
470         rte_spinlock_unlock(&intr_lock);
471
472         /**
473          * check if need to notify the pipe fd waited by epoll_wait to
474          * rebuild the wait list.
475          */
476         if (wake_thread)
477                 if (write(intr_pipe.writefd, "1", 1) < 0)
478                         return -EPIPE;
479
480         return (ret);
481 }
482
483 int
484 rte_intr_callback_unregister(struct rte_intr_handle *intr_handle,
485                         rte_intr_callback_fn cb_fn, void *cb_arg)
486 {
487         int ret;
488         struct rte_intr_source *src;
489         struct rte_intr_callback *cb, *next;
490
491         /* do parameter checking first */
492         if (intr_handle == NULL || intr_handle->fd < 0) {
493                 RTE_LOG(ERR, EAL,
494                 "Unregistering with invalid input parameter\n");
495                 return -EINVAL;
496         }
497
498         rte_spinlock_lock(&intr_lock);
499
500         /* check if the insterrupt source for the fd is existent */
501         TAILQ_FOREACH(src, &intr_sources, next)
502                 if (src->intr_handle.fd == intr_handle->fd)
503                         break;
504
505         /* No interrupt source registered for the fd */
506         if (src == NULL) {
507                 ret = -ENOENT;
508
509         /* interrupt source has some active callbacks right now. */
510         } else if (src->active != 0) {
511                 ret = -EAGAIN;
512
513         /* ok to remove. */
514         } else {
515                 ret = 0;
516
517                 /*walk through the callbacks and remove all that match. */
518                 for (cb = TAILQ_FIRST(&src->callbacks); cb != NULL; cb = next) {
519
520                         next = TAILQ_NEXT(cb, next);
521
522                         if (cb->cb_fn == cb_fn && (cb_arg == (void *)-1 ||
523                                         cb->cb_arg == cb_arg)) {
524                                 TAILQ_REMOVE(&src->callbacks, cb, next);
525                                 rte_free(cb);
526                                 ret++;
527                         }
528                 }
529
530                 /* all callbacks for that source are removed. */
531                 if (TAILQ_EMPTY(&src->callbacks)) {
532                         TAILQ_REMOVE(&intr_sources, src, next);
533                         rte_free(src);
534                 }
535         }
536
537         rte_spinlock_unlock(&intr_lock);
538
539         /* notify the pipe fd waited by epoll_wait to rebuild the wait list */
540         if (ret >= 0 && write(intr_pipe.writefd, "1", 1) < 0) {
541                 ret = -EPIPE;
542         }
543
544         return (ret);
545 }
546
547 int
548 rte_intr_enable(struct rte_intr_handle *intr_handle)
549 {
550         if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
551                 return -1;
552
553         switch (intr_handle->type){
554         /* write to the uio fd to enable the interrupt */
555         case RTE_INTR_HANDLE_UIO:
556                 if (uio_intr_enable(intr_handle))
557                         return -1;
558                 break;
559         /* not used at this moment */
560         case RTE_INTR_HANDLE_ALARM:
561                 return -1;
562 #ifdef VFIO_PRESENT
563         case RTE_INTR_HANDLE_VFIO_MSIX:
564                 if (vfio_enable_msix(intr_handle))
565                         return -1;
566                 break;
567         case RTE_INTR_HANDLE_VFIO_MSI:
568                 if (vfio_enable_msi(intr_handle))
569                         return -1;
570                 break;
571         case RTE_INTR_HANDLE_VFIO_LEGACY:
572                 if (vfio_enable_intx(intr_handle))
573                         return -1;
574                 break;
575 #endif
576         /* unknown handle type */
577         default:
578                 RTE_LOG(ERR, EAL,
579                         "Unknown handle type of fd %d\n",
580                                         intr_handle->fd);
581                 return -1;
582         }
583
584         return 0;
585 }
586
587 int
588 rte_intr_disable(struct rte_intr_handle *intr_handle)
589 {
590         if (!intr_handle || intr_handle->fd < 0 || intr_handle->uio_cfg_fd < 0)
591                 return -1;
592
593         switch (intr_handle->type){
594         /* write to the uio fd to disable the interrupt */
595         case RTE_INTR_HANDLE_UIO:
596                 if (uio_intr_disable(intr_handle))
597                         return -1;
598                 break;
599         /* not used at this moment */
600         case RTE_INTR_HANDLE_ALARM:
601                 return -1;
602 #ifdef VFIO_PRESENT
603         case RTE_INTR_HANDLE_VFIO_MSIX:
604                 if (vfio_disable_msix(intr_handle))
605                         return -1;
606                 break;
607         case RTE_INTR_HANDLE_VFIO_MSI:
608                 if (vfio_disable_msi(intr_handle))
609                         return -1;
610                 break;
611         case RTE_INTR_HANDLE_VFIO_LEGACY:
612                 if (vfio_disable_intx(intr_handle))
613                         return -1;
614                 break;
615 #endif
616         /* unknown handle type */
617         default:
618                 RTE_LOG(ERR, EAL,
619                         "Unknown handle type of fd %d\n",
620                                         intr_handle->fd);
621                 return -1;
622         }
623
624         return 0;
625 }
626
627 static int
628 eal_intr_process_interrupts(struct epoll_event *events, int nfds)
629 {
630         int n, bytes_read;
631         struct rte_intr_source *src;
632         struct rte_intr_callback *cb;
633         union rte_intr_read_buffer buf;
634         struct rte_intr_callback active_cb;
635
636         for (n = 0; n < nfds; n++) {
637
638                 /**
639                  * if the pipe fd is ready to read, return out to
640                  * rebuild the wait list.
641                  */
642                 if (events[n].data.fd == intr_pipe.readfd){
643                         int r = read(intr_pipe.readfd, buf.charbuf,
644                                         sizeof(buf.charbuf));
645                         RTE_SET_USED(r);
646                         return -1;
647                 }
648                 rte_spinlock_lock(&intr_lock);
649                 TAILQ_FOREACH(src, &intr_sources, next)
650                         if (src->intr_handle.fd ==
651                                         events[n].data.fd)
652                                 break;
653                 if (src == NULL){
654                         rte_spinlock_unlock(&intr_lock);
655                         continue;
656                 }
657
658                 /* mark this interrupt source as active and release the lock. */
659                 src->active = 1;
660                 rte_spinlock_unlock(&intr_lock);
661
662                 /* set the length to be read dor different handle type */
663                 switch (src->intr_handle.type) {
664                 case RTE_INTR_HANDLE_UIO:
665                         bytes_read = sizeof(buf.uio_intr_count);
666                         break;
667                 case RTE_INTR_HANDLE_ALARM:
668                         bytes_read = sizeof(buf.timerfd_num);
669                         break;
670 #ifdef VFIO_PRESENT
671                 case RTE_INTR_HANDLE_VFIO_MSIX:
672                 case RTE_INTR_HANDLE_VFIO_MSI:
673                 case RTE_INTR_HANDLE_VFIO_LEGACY:
674                         bytes_read = sizeof(buf.vfio_intr_count);
675                         break;
676 #endif
677                 default:
678                         bytes_read = 1;
679                         break;
680                 }
681
682                 /**
683                  * read out to clear the ready-to-be-read flag
684                  * for epoll_wait.
685                  */
686                 bytes_read = read(events[n].data.fd, &buf, bytes_read);
687
688                 if (bytes_read < 0)
689                         RTE_LOG(ERR, EAL, "Error reading from file "
690                                 "descriptor %d: %s\n", events[n].data.fd,
691                                                         strerror(errno));
692                 else if (bytes_read == 0)
693                         RTE_LOG(ERR, EAL, "Read nothing from file "
694                                 "descriptor %d\n", events[n].data.fd);
695
696                 /* grab a lock, again to call callbacks and update status. */
697                 rte_spinlock_lock(&intr_lock);
698
699                 if (bytes_read > 0) {
700
701                         /* Finally, call all callbacks. */
702                         TAILQ_FOREACH(cb, &src->callbacks, next) {
703
704                                 /* make a copy and unlock. */
705                                 active_cb = *cb;
706                                 rte_spinlock_unlock(&intr_lock);
707
708                                 /* call the actual callback */
709                                 active_cb.cb_fn(&src->intr_handle,
710                                         active_cb.cb_arg);
711
712                                 /*get the lock back. */
713                                 rte_spinlock_lock(&intr_lock);
714                         }
715                 }
716
717                 /* we done with that interrupt source, release it. */
718                 src->active = 0;
719                 rte_spinlock_unlock(&intr_lock);
720         }
721
722         return 0;
723 }
724
725 /**
726  * It handles all the interrupts.
727  *
728  * @param pfd
729  *  epoll file descriptor.
730  * @param totalfds
731  *  The number of file descriptors added in epoll.
732  *
733  * @return
734  *  void
735  */
736 static void
737 eal_intr_handle_interrupts(int pfd, unsigned totalfds)
738 {
739         struct epoll_event events[totalfds];
740         int nfds = 0;
741
742         for(;;) {
743                 nfds = epoll_wait(pfd, events, totalfds,
744                         EAL_INTR_EPOLL_WAIT_FOREVER);
745                 /* epoll_wait fail */
746                 if (nfds < 0) {
747                         if (errno == EINTR)
748                                 continue;
749                         RTE_LOG(ERR, EAL,
750                                 "epoll_wait returns with fail\n");
751                         return;
752                 }
753                 /* epoll_wait timeout, will never happens here */
754                 else if (nfds == 0)
755                         continue;
756                 /* epoll_wait has at least one fd ready to read */
757                 if (eal_intr_process_interrupts(events, nfds) < 0)
758                         return;
759         }
760 }
761
762 /**
763  * It builds/rebuilds up the epoll file descriptor with all the
764  * file descriptors being waited on. Then handles the interrupts.
765  *
766  * @param arg
767  *  pointer. (unused)
768  *
769  * @return
770  *  never return;
771  */
772 static __attribute__((noreturn)) void *
773 eal_intr_thread_main(__rte_unused void *arg)
774 {
775         struct epoll_event ev;
776
777         /* host thread, never break out */
778         for (;;) {
779                 /* build up the epoll fd with all descriptors we are to
780                  * wait on then pass it to the handle_interrupts function
781                  */
782                 static struct epoll_event pipe_event = {
783                         .events = EPOLLIN | EPOLLPRI,
784                 };
785                 struct rte_intr_source *src;
786                 unsigned numfds = 0;
787
788                 /* create epoll fd */
789                 int pfd = epoll_create(1);
790                 if (pfd < 0)
791                         rte_panic("Cannot create epoll instance\n");
792
793                 pipe_event.data.fd = intr_pipe.readfd;
794                 /**
795                  * add pipe fd into wait list, this pipe is used to
796                  * rebuild the wait list.
797                  */
798                 if (epoll_ctl(pfd, EPOLL_CTL_ADD, intr_pipe.readfd,
799                                                 &pipe_event) < 0) {
800                         rte_panic("Error adding fd to %d epoll_ctl, %s\n",
801                                         intr_pipe.readfd, strerror(errno));
802                 }
803                 numfds++;
804
805                 rte_spinlock_lock(&intr_lock);
806
807                 TAILQ_FOREACH(src, &intr_sources, next) {
808                         if (src->callbacks.tqh_first == NULL)
809                                 continue; /* skip those with no callbacks */
810                         ev.events = EPOLLIN | EPOLLPRI;
811                         ev.data.fd = src->intr_handle.fd;
812
813                         /**
814                          * add all the uio device file descriptor
815                          * into wait list.
816                          */
817                         if (epoll_ctl(pfd, EPOLL_CTL_ADD,
818                                         src->intr_handle.fd, &ev) < 0){
819                                 rte_panic("Error adding fd %d epoll_ctl, %s\n",
820                                         src->intr_handle.fd, strerror(errno));
821                         }
822                         else
823                                 numfds++;
824                 }
825                 rte_spinlock_unlock(&intr_lock);
826                 /* serve the interrupt */
827                 eal_intr_handle_interrupts(pfd, numfds);
828
829                 /**
830                  * when we return, we need to rebuild the
831                  * list of fds to monitor.
832                  */
833                 close(pfd);
834         }
835 }
836
837 int
838 rte_eal_intr_init(void)
839 {
840         int ret = 0;
841
842         /* init the global interrupt source head */
843         TAILQ_INIT(&intr_sources);
844
845         /**
846          * create a pipe which will be waited by epoll and notified to
847          * rebuild the wait list of epoll.
848          */
849         if (pipe(intr_pipe.pipefd) < 0)
850                 return -1;
851
852         /* create the host thread to wait/handle the interrupt */
853         ret = pthread_create(&intr_thread, NULL,
854                         eal_intr_thread_main, NULL);
855         if (ret != 0)
856                 RTE_LOG(ERR, EAL,
857                         "Failed to create thread for interrupt handling\n");
858
859         return -ret;
860 }
861